iOS应用程序中的自定义证书

我创build了一个使用Apple Configurator安装到iPad上的证书(p12types)。 我现在想在我写的应用程序中访问证书,但是我似乎无法find它的源代码示例。 有很多如何使用证书的例子,但我一直无法find任何关于如何将其导入到应用程序。

有人能指出我正确的方向吗? 提前致谢。

*新*

所以我试着按照这里的教程https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13使用列表2-1和列表2-2。 现在我放弃了从钥匙链获取证书,而是从主Bundle中加载。

NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myfile.com" ofType:@"pfx"]]; CFDataRef myCertData = (__bridge_retained CFDataRef)(certData); 

我不知道我的加载是否正确,但是当我debuggingcertData不是NULL,并包含字节。

接下来我修改了两个列表中的代码,因为方法签名并不真正匹配Objective C方法签名。 有人帮我在这里为什么苹果会这样显示它? 该函数内没有代码被修改。

 - (OSStatus) extractIdentityAndTrust: (CFDataRef) inPKCS12Data withIdentity:(SecIdentityRef *) outIdentity withTrust:(SecTrustRef *) outTrust withPassword:(CFStringRef) keyPassword { OSStatus securityError = errSecSuccess; const void *keys[] = { kSecImportExportPassphrase }; const void *values[] = { keyPassword }; CFDictionaryRef optionsDictionary = NULL; optionsDictionary = CFDictionaryCreate(NULL, keys, values, (keyPassword ? 1 : 0), NULL, NULL); CFArrayRef items = NULL; securityError = SecPKCS12Import(inPKCS12Data, optionsDictionary, &items); if (securityError == 0) { CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0); const void *tempIdentity = NULL; tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity); CFRetain(tempIdentity); *outIdentity = (SecIdentityRef)tempIdentity; const void *tempTrust = NULL; tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust); CFRetain(tempTrust); *outTrust = (SecTrustRef)tempTrust; } if (optionsDictionary) CFRelease(optionsDictionary); if (items) CFRelease(items); return securityError; } 

接下来我修改了copySummaryString的方法签名。 我还必须在“身份”variables的函数中添加一个约定。

 - (NSString *)copySummaryString:(SecIdentityRef *) identity { // Get the certificate from the identity. SecCertificateRef myReturnedCertificate = NULL; OSStatus status = SecIdentityCopyCertificate (*identity, &myReturnedCertificate); if (status) { NSLog(@"SecIdentityCopyCertificate failed.\n"); return NULL; } CFStringRef certSummary = SecCertificateCopySubjectSummary (myReturnedCertificate); NSString* summaryString = [[NSString alloc] initWithString:(__bridge NSString *)certSummary]; CFRelease(certSummary); return summaryString; } 

最后,在我的调用函数中,我创build了一个我传递的身份和信任variables:

 [self extractIdentityAndTrust:myCertData withIdentity:identity withTrust:trust withPassword:CFSTR("supersecret")]; [self copySummaryString:identity]; 

当我尝试运行这个时,我得到一个线程1:EXC_BAD_ACCESS(code = 1,地址= 0x2b)错误和XCode暂停在下面的行上:

 CFStringRef certSummary = SecCertificateCopySubjectSummary 

这段代码并没有引用任何我创build的variables,所以我真的很惊讶它在这里崩溃。 加载此证书的最终目的是将其用于HTTPS。 我希望我至less在这条正确的道路上。

以下是通过您的移动应用程序查找和使用预先安装的证书和身份的文档和示例。

https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-DontLinkElementID_10

*新

我检查了你的更新。 你的两种方法都像魅力一样。

我用下面的源代码来调用你的方法,并能够正确地提取SummaryString。

 SecIdentityRef identity = nil; SecTrustRef trust = nil; NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[Dev] InHouse_Certificates" ofType:@"p12"]]; CFDataRef myCertData = (__bridge_retained CFDataRef)(certData); [self extractIdentityAndTrust:myCertData withIdentity:&identity withTrust:&trust withPassword:CFSTR("1234")]; NSString* summaryString = [self copySummaryString:&identity];