Wednesday, June 17, 2015

Extracting and Displaying Certificate information from a p12 file

On iOS the code is bit simple, like given below. 

-(IBAction) loadCertificate:(id)sender
{
    NSString *str=[[NSBundle mainBundle] pathForResource:@“cert1” ofType:@"p12"];
    NSData *fileData = [NSData dataWithContentsOfFile:str];
    SecIdentityRef identityRef;
    SecTrustRef outTrust;
    CFStringRef passwordRef = (__bridge CFStringRef)@“pass123”;
    CFDataRef cfdata = CFDataCreate(NULL, [fileData bytes], [fileData length]);
    OSStatus status = extractIdentityAndTrust(cfdata,&identityRef,&outTrust,passwordRef);
    
    NSLog(@"--status of import is :%d",(int)status);
    
    NSString *summaryStr = copySummaryString(identityRef);
    NSLog(@"--Summary :%@",summaryStr);
    
}

OSStatus extractIdentityAndTrust(CFDataRef inPKCS12Data,
                                 SecIdentityRef *outIdentity,
                                 SecTrustRef *outTrust,
                                 CFStringRef keyPassword)
{
    OSStatus securityError = errSecSuccess;
    
    
    const void *keys[] =   { kSecImportExportPassphrase };
    const void *values[] = { keyPassword };
    CFDictionaryRef optionsDictionary = NULL;
    
    /* Create a dictionary containing the passphrase if one
     was specified.  Otherwise, create an empty dictionary. */
    optionsDictionary = CFDictionaryCreate(
                                           NULL, keys,
                                           values, (keyPassword ? 1 : 0),
                                           NULL, NULL);  // 1
    
    CFArrayRef items = NULL;
    securityError = SecPKCS12Import(inPKCS12Data,
                                    optionsDictionary,
                                    &items);                    // 2
    
    
    //
    if (securityError == 0) {                                   // 3
        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)                                      // 4
        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);  // 1
    
    if (status) {
        NSLog(@"SecIdentityCopyCertificate failed.\n");
        return NULL;
    }
    
    CFStringRef certSummary = SecCertificateCopySubjectSummary
    (myReturnedCertificate);  // 2
    
    NSString* summaryString = [[NSString alloc]
                               initWithString:(__bridge NSString *)certSummary];  // 3
    
    CFRelease(certSummary);
    
    return summaryString;
}

References:

No comments:

Post a Comment