我正在开发 iOS 应用程序并努力从 .p12 证书中提取身份。我还是 objective-c 的新手,所以我确定缺少一些重要的东西。这是代码:
@implementation P12Extractor
-(SecIdentityRef)getIdentity{
NSString *path = [[NSBundle mainBundle] pathForResource:@"ServerCert" ofType:@"p12"];
NSData *p12data = [NSData dataWithContentsOfFile:path];
CFDataRef inP12Data = (__bridge CFDataRef)p12data;
SecIdentityRef myIdentity;
OSStatus status = extractIdentity(inP12Data, &myIdentity);
if (status != 0) {
NSLog(@"%@",status);
}
return myIdentity;
}
OSStatus extractIdentity(CFDataRef inP12Data, SecIdentityRef *identity){
OSStatus securityError = errSecSuccess;
CFStringRef password = CFSTR("password");
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { password };
CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import(inP12Data, options, &items);
if (securityError == 0) {
CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); // <<<at this point i get an EXC_BAD_ACCESS(code=2,adress=0x0) error
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity);
*identity = (SecIdentityRef)tempIdentity;
}
if (options) {
CFRelease(options);
}
return securityError;
}
@end
我已经用评论标记了错误点,我真的不知道为什么我一直收到这个。此代码应该是 Apple 开发站点批准的解决方案。
最佳答案
当您尝试获取它的第一个元素时,您的数组仍然是空的...
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import(inP12Data, options, &items);
if (securityError == 0) {
CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); // <<<at this point i get an EXC_BAD_ACCESS(code=2,adress=0x0) error
我认为问题可能在于证书或您传入的选项。
关于iOS 提取证书身份 : EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145582/