我创建了一个证书(p12 类型),并使用 Apple Configurator 将其安装到 iPad 上。我现在想在我编写的应用程序中访问该证书,但我似乎找不到它的源示例。有很多关于如何使用证书的示例,但我无法找到有关如何将其导入应用程序的任何信息。
有人能指出我正确的方向吗?提前致谢。
* 新 *
所以我尝试按照此处的教程 https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13使用 list 2-1 和 list 2-2。现在我放弃了从钥匙串(keychain)获取证书,而是从主 bundle 加载它。
NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myfile.com" ofType:@"pfx"]];
CFDataRef myCertData = (__bridge_retained CFDataRef)(certData);
我不确定我的加载是否正确,但当我调试时 certData 不为 NULL 并且包含字节。
接下来,我修改了两个 list 中的代码,因为方法签名与 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 的方法签名。我还必须在函数内添加“identity”变量的引用。
- (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;
}
最后,在我的调用函数中,我创建了一个传递的身份和信任变量:
[self extractIdentityAndTrust:myCertData withIdentity:identity withTrust:trust withPassword:CFSTR("supersecret")];
[self copySummaryString:identity];
当我尝试运行它时,出现 Thread 1: EXC_BAD_ACCESS(code=1, address=0x2b) 错误并且 XCode 在以下行暂停:
CFStringRef certSummary = SecCertificateCopySubjectSummary
这段代码没有引用我创建的任何变量,所以我真的很惊讶它会在这里崩溃。加载此证书的最终目标是将其用于 HTTPS。我希望我至少走在正确的道路上。
最佳答案
以下是有关通过移动应用查找和使用预装证书和身份的文档和示例。
*新
我检查了你的更新。 这两种方法都很有效。
我使用下面的源代码来调用您的方法,并且能够正确提取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];
关于ios - iOS App 中的自定义证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852186/