线程 1:EXC_BAD_ACCESS(代码=1,地址=0x10)错误

Thread 1: EXC_BAD_ACCESS (code=1, address=0x10) error

我有一个 certificatep12-format,想从中获取一些信息。

我有以下两个功能

-(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;
}

-(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;
}

我在我的 viewDidLoad 中的以下几行中调用了这两个方法

SecIdentityRef identity = nil;
SecTrustRef trust = nil;
NSData *certPath = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"p12"]];
CFDataRef certData = (__bridge_retained CFDataRef)(certPath);
[self extractIdentityAndTrust:certData withIdentity:&identity withTrust:&trust withPassword:CFSTR("")];
NSString* summaryString = [self copySummaryString:&identity];
NSLog(@"%@", summaryString);

但是在我的函数中copySummaryString我在下面一行得到了错误

OSStatus status = SecIdentityCopyCertificate (*identity, &myReturnedCertificate);

我没有找到任何好的例子。如何正确调用这些函数,为什么会出现此错误以及此错误是什么意思?

我还发现 post 喜欢 , or

我也在 https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13 上阅读了 Apple 的文档,但它不是很有帮助。

解决了我的问题:

问题出在我 viewDidLoad 的行中。我没有设置密码。函数 extractIdentityAndTrust 中的变量 securityError 不是 0