SecPKCS12Import不返回任何项目

我想为我的iOS 6应用程序的钥匙串添加一些TLSvalidation的CA证书。 证书包含在应用程序包中。 我不想添加几个例子中描述的任何身份 (私钥/证书组合)。

SecPKCS12Import调用不会返回任何错误,但不幸的是它也不会返回任何证书。

为了让您重现我的步骤,我以Google中间证书(“Google Internet Authority”)为例,在下载的PEM证书上运行以下命令:

COT

 #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----- 

之后,我在下面的代码执行的应用程序中捆绑了文件:

 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); 

控制台结果输出是'证书find:0'。 certDatavariables被填充正确的字节数,如果我更改提供的密码,结果将更改为errSecAuthFailed

你有什么想法可能是什么问题?

我会说这是一个错误,请参阅相关的问题SSL身份证书在iOS上运行HTTPS服务器和错误SecPKCS12Import在1月1日10000后证书过期时返回空数组 。

因为你只需要一个证书,没有私钥,我会从DER格式文件导入证书。

 $ openssl x509 -in google.pem -out google.der -outform DER $ openssl x509 -in google.der -noout -text 

打包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);