2

I have an certificate with p12-format and wanted to get some information off it.

I have the following two functions

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

and

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

I call these two methods in my viewDidLoad in the following lines

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);

But in my function copySummaryString I got the error at the following line

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

I didn't find any good example for it. How do I call these function correctly, why I get this error and what does this error mean?

I also found post like https://stackoverflow.com/a/20913426/5629933, https://stackoverflow.com/a/19219932/5629933 or Custom Certificate in iOS App.

I also read the documentation of Apple on https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13 but it's not very helpful.

Community
  • 1
  • 1
Premox
  • 323
  • 10
  • 25

1 Answers1

0

Solved my problem:

The issue was at the lines in my viewDidLoad. I hadn't set the password. The variable securityError in the function extractIdentityAndTrust wasn't 0

Premox
  • 323
  • 10
  • 25