我想将某些用于 TLS 验证的 CA 证书添加到我的 iOS 6 应用程序的钥匙串中。这些证书包含在应用程序包中。我不想添加几个示例中描述的任何身份(私钥/证书组合)。
该SecPKCS12Import调用不会返回任何错误,但不幸的是它也不会返回任何证书。
为了让您重现我的步骤,我以 Google 中级证书(“Google Internet Authority”)为例,并对下载的 PEM 证书运行以下命令:
。
#convert PEM certificate to PKCS12
openssl pkcs12 -export -in google.pem -nokeys -out google.p12 -passout "pass:google"
#verification
openssl pkcs12 -in google.p12 -passin "pass:google"
MAC verified OK
Bag Attributes: <No Attributes>
subject=/C=US/O=Google Inc/CN=Google Internet Authority
issuer=/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
-----BEGIN CERTIFICATE-----
MIICsDCCAhmgAwIBAgIDFXfhMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT
MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0
[...]
ARlIjNvrPq86fpVg0NOTawALkSqOUMl3MynBQO+spR7EHcRbADQ/JemfTEh2Ycfl
vZqhEFBfurZkX0eTANq98ZvVfpg=
-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)
之后,我将该文件捆绑到我的应用程序中,其中执行了以下代码:
NSMutableDictionary * options = [[[NSMutableDictionary alloc] init] autorelease];
[options setObject:@"google" forKey:(id)kSecImportExportPassphrase];
CFArrayRef items = NULL;
NSData *certData = [NSData dataWithContentsOfFile:[NSBundle pathForResource:@"google" ofType:@"p12" inDirectory:[[NSBundle mainBundle] bundlePath]]];
OSStatus result = SecPKCS12Import((CFDataRef)certData, (CFDictionaryRef)options, &items);
assert(result == errSecSuccess);
CFIndex count = CFArrayGetCount(items);
NSLog(@"Certificates found: %ld",count);
Run Code Online (Sandbox Code Playgroud)
控制台结果输出为“找到证书:0”。certData变量填充正确的字节数,如果我更改提供的密码,结果将更改为errSecAuthFailed。
您知道问题可能是什么吗?
小智 5
我想说这是一个错误,请参阅相关问题SSL Identity Certificate to run an HTTPS Server on iOS和错误SecPKCS12Import returnsempty array whencertificate expires after Jan 1st 10000。
由于您只需要一个证书,没有私钥,因此我会从 DER 格式文件导入证书。
$ openssl x509 -in google.pem -out google.der -outform DER
$ openssl x509 -in google.der -noout -text
Run Code Online (Sandbox Code Playgroud)
捆绑 DER 证书文件并导入:
NSString *path = [[NSBundle mainBundle] pathForResource:@"google" ofType:@"der"];
NSData *derData = [NSData dataWithContentsOfFile:path];
SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef)derData);
// add cert to KeyChain or use it as you need
CFRelease(cert);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3807 次 |
| 最近记录: |