- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将我的iPhone应用程序与系统集成,他们需要通过给定的公钥加密数据,有3个文件,3种不同的格式.xml .der和.pem,我研究并找到了一些关于从 DER/PEM 获取 SecKeyRef,但它们总是返回 nil。下面是我的代码:
NSString *pkFilePath = [[NSBundle mainBundle] pathForResource:@"PKFile" ofType:@"der"];
NSData *pkData = [NSData dataWithContentsOfFile:pkFilePath];
SecCertificateRef cert;
cert = SecCertificateCreateWithData(NULL, (CFDataRef) pkData);
assert(cert != NULL);
OSStatus err;
if (cert != NULL) {
err = SecItemAdd(
(CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
(id) kSecClassCertificate, kSecClass,
(id) cert, kSecValueRef,
nil
],
NULL
);
if ( (err == errSecSuccess) || (err == errSecDuplicateItem) ) {
CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **) &cert, 1, NULL);
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
SecTrustCreateWithCertificates(certs, policy, &trust);
SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
if (certs) {
CFRelease(certs);
}
if (trust) {
CFRelease(trust);
}
return SecTrustCopyPublicKey(trust);
}
}
return NULL;
问题发生在 SecCertificateCreateWithData 处,即使读取文件正常,它也总是返回 nil。有人做过这个请帮帮我,谢谢!
编辑:证书文件是 MD5 签名。
最佳答案
我为同样的问题苦苦挣扎,终于找到了解决方案。我的问题是,我需要使用外部私钥和公钥来加密/解密 iOS 应用程序中的数据,并且不想使用钥匙串(keychain)。事实证明,您还需要 iOS 安全库的签名证书才能读取 key 数据,当然文件必须采用正确的格式。流程基本如下:
假设您有一个 PEM 格式的私钥(带有 -----BEGIN RSA PRIVATE KEY----- 和 -----END RSA PRIVATE KEY----- 标记):rsaPrivate.pem
//Create a certificate signing request with the private key
openssl req -new -key rsaPrivate.pem -out rsaCertReq.csr
//Create a self-signed certificate with the private key and signing request
openssl x509 -req -days 3650 -in rsaCertReq.csr -signkey rsaPrivate.pem -out rsaCert.crt
//Convert the certificate to DER format: the certificate contains the public key
openssl x509 -outform der -in rsaCert.crt -out rsaCert.der
//Export the private key and certificate to p12 file
openssl pkcs12 -export -out rsaPrivate.p12 -inkey rsaPrivate.pem -in rsaCert.crt
现在您有两个与 iOS 安全框架兼容的文件:rsaCert.der(公钥)和 rsaPrivate.p12(私钥)。假设文件已添加到您的包中,下面的代码会读取公钥:
- (SecKeyRef)getPublicKeyRef {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"rsaCert" ofType:@"der"];
NSData *certData = [NSData dataWithContentsOfFile:resourcePath];
SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef)certData);
SecKeyRef key = NULL;
SecTrustRef trust = NULL;
SecPolicyRef policy = NULL;
if (cert != NULL) {
policy = SecPolicyCreateBasicX509();
if (policy) {
if (SecTrustCreateWithCertificates((CFTypeRef)cert, policy, &trust) == noErr) {
SecTrustResultType result;
OSStatus res = SecTrustEvaluate(trust, &result);
//Check the result of the trust evaluation rather than the result of the API invocation.
if (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified) {
key = SecTrustCopyPublicKey(trust);
}
}
}
}
if (policy) CFRelease(policy);
if (trust) CFRelease(trust);
if (cert) CFRelease(cert);
return key;
}
要读取私钥,请使用以下代码:
SecKeyRef getPrivateKeyRef() {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"rsaPrivate" ofType:@"p12"];
NSData *p12Data = [NSData dataWithContentsOfFile:resourcePath];
NSMutableDictionary * options = [[NSMutableDictionary alloc] init];
SecKeyRef privateKeyRef = NULL;
//change to the actual password you used here
[options setObject:@"password_for_the_key" forKey:(id)kSecImportExportPassphrase];
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
OSStatus securityError = SecPKCS12Import((CFDataRef) p12Data,
(CFDictionaryRef)options, &items);
if (securityError == noErr && CFArrayGetCount(items) > 0) {
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
SecIdentityRef identityApp =
(SecIdentityRef)CFDictionaryGetValue(identityDict,
kSecImportItemIdentity);
securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
if (securityError != noErr) {
privateKeyRef = NULL;
}
}
[options release];
CFRelease(items);
return privateKeyRef;
}
关于iphone - 如何从 DER/PEM 文件获取 SecKeyRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579985/
如何从一个 PEM 中确定 SSL 证书到期日期,该 PEM 在一个文件 (pem) 上串联了很多证书? 例子: # cat cert.pem -----BEGIN CERTIFICATE-----
我想让运行测试更容易。为了实现这一点,我想提供项目内部测试中使用的 secret 。 我使用以下命令创建了“fullchain.pem”、“privkey.pem”和“dhparams.pem”: o
注意图片中证书的图标图像上写有标准或个人。有什么不同?最近一次我在处理它,我只制作了带有“个人”的图标。我不知道为什么。有什么不同?两者是如何产生的? 最佳答案 我会这样说,因为您的 .p12 文件包
我正在尝试安装这个项目:https://github.com/versatica/mediasoup-demo 它需要 fullchain.pem和 privkey.pem文件。 如何在 Ubuntu
我正在使用 curl 使用公共(public)证书文件从 https 站点下载数据。 系统信息: 操作系统:软呢帽 14 curl : curl 7.30.0 openssl: OpenSSL 1.0
我正在尝试使用 openssl 加密短文本,但我不知道如何继续。更具体地说:我发现一段代码似乎可以工作,但必须从终端执行指令。是的,我知道,存在 system() 函数,但我想避免它,因为它需要文件夹
我正在使用 OpenSSL 将“me.p12”转换为 PEM。当我生成“me.p12”时,我为其设置了密码。 “me.p12”包含私钥和证书。 当我将其转换为 PEM 时,我运行命令: openssl
我正在使用 Amazon 的 EC2 集群来运行 GraphLab。他们想要我的 .pem 文件的位置,这是我的私钥。 搜索后我仍然在ubuntu中找不到该文件。我正在使用 PuTTY。 最佳答案 .
我正在尝试了解 .pem 文件的布局方式。密码短语是如何验证的? .pem 文件如何检查您尝试打开它时使用的密码? 最佳答案 多种不同类型的数据存储在扩展名为.pem 的文件中。所以你的问题不准确。但
我需要生成一个 cert.pem 和 key.pem 文件来使用 Nginx 加密 http 请求。 在 Linux 上,我会执行以下 OpenSSL 命令: openssl req -x509 -n
我必须以 pem 格式提交 CSR。 我已经使用 OpenSSL 生成了 CSR,但在将其转换为 PEM 格式时遇到了困难。 我必须从 example.csr 获取示例 - csr.pem。如何实现?
我知道这是一个与许多其他问题非常相似的问题,但他们都没有给出直接的答案或对我有用的答案...... 我从 Let's encrypt 获得了两个文件: 证书.pem key.pem 我需要让他们进入
首先,我看过this thread但它不相关并且有不同的问题。 我的 Kafka 属性文件中有以下设置片段: ssl.keystore.type=PEM ssl.keystore.key=/path/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我想下载 Cpanel 自动生成的 SSL 作为 key.pem 和 cert.pem,我在 Cpanel 中找不到任何下载链接,有什么建议吗? 我曾尝试将证书文本复制到文本文件中并使用它,但没有成功
这个问题在这里已经有了答案: Converting pfx to pem using openssl (5 个答案) 关闭 6 年前。
我想通过 AWS Lambda 使用 pysftp 库连接 EC2。我使用以下代码进行连接。 mysftp = pysftp.Connection( host=Constants.MY_HOS
我有这两个文件(certificate.pem 和private_key.pem),它们由我使用的 API 提供。我需要使用这些文件在 Swift 项目中签署我的 http 请求。 我可以用 Post
我的 Pem 文件就是这种格式。 Bag Attributes localKeyID: friendlyName: test subject=/C=GB/ST=London/L=Soho/O=Righ
我从证书颁发机构获得了这些文件: domain.com.p7b domain.com.crt domain.com.ca-bundle 我尝试了这个小代码: var express = require
我是一名优秀的程序员,十分优秀!