iOS提取证书身份:EXC_BAD_ACCESS

时间:2012-10-30 18:39:05

标签: ios xcode ssl

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

1 个答案:

答案 0 :(得分: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

我认为问题可能在于证书或您传递的选项。