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

时间:2016-06-06 10:20:02

标签: ios certificate exc-bad-access

我有一个certificate p12-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);

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

我还发现了https://stackoverflow.com/a/20913426/5629933https://stackoverflow.com/a/19219932/5629933Custom Certificate in iOS App等帖子。

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

1 个答案:

答案 0 :(得分:0)

解决了我的问题:

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