0

我正在开发一个 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 开发网站批准的解决方案。

4

1 回答 1

0

当您尝试获取其第一个元素时,您的数组仍然是空的......

 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

我认为问题可能出在证书或您传入的选项上。

于 2012-10-30T18:40:38.207 回答