iOS 提取证书身份 : EXC_BAD_ACCESS

标签 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 开发站点批准的解决方案。

最佳答案

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

 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/

相关文章:

iphone - iphone 中的 UIButton 事件

ios - 如何将数据传递给模型类并从模型类中获取数据 - swift

ios - 使用 APNS HTTP/2 测试开发中的推送通知

ios - 消耗品应用内购买已购买

android - 在 android 中使用 UNKNOWN CA 签名证书(如 pkcs12)的任何好例子

ios - steroids.js 如何在默认情况下和加载页面上禁用导航栏

javascript - React native application release build install on Simulator 但不是设备

ios - iOS Xcode plist 警告是什么意思?

android - Google App Engine 教程,无法获取签名证书指纹(SHA1)

ssl - 客户端-服务器 SSL 通信 + 自签名证书