Click here to Skip to main content
15,887,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm building an iOS app which requires use of certificate.
I've used the following function to extract the identity from the certificate (which i've added into my app bundle).

Objective-C
(SecIdentityRef)getClientCertificate
 {
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Certificates" ofType:@"p12"];
    if(!thePath)
    {
        NSLog(@"NULL");
        return NULL;
    }
    NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath] ;
    CFDataRef inPKCS12Data = (CFDataRef)CFBridgingRetain(PKCS12Data);
    CFStringRef password = CFSTR("********");
    const void *keys[] = { kSecImportExportPassphrase };
    const void *values[] = { password };
    CFDictionaryRef optionsDictionary = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
    OSStatus ret = SecPKCS12Import(inPKCS12Data, optionsDictionary, &items);
    if (ret != errSecSuccess)
    {
        // TODO: handle error.
        NSLog(@"-> SecPKCS12Import error (%d)", (int)ret);
    }
    CFRelease(optionsDictionary);
    
    CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
    SecIdentityRef identityApp = nil;
    if(!identityDict)
        return nil;
    identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
    
    SecIdentityRef      identity;
    SecCertificateRef   cert;
    OSStatus            err;
    CFStringRef         certName;
    
    identity = identityApp;
    assert( (identity != NULL) && (CFGetTypeID(identity) == SecIdentityGetTypeID()) );
    cert = NULL;
    err = SecIdentityCopyCertificate(identity, &cert);
    assert(err == noErr);
    assert(cert != NULL);
    
    certName = SecCertificateCopySubjectSummary(cert);
    assert(certName != NULL);
    
    NSLog(@"%@" , (id) CFBridgingRelease(certName));
    //NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:(id)CFBridgingRelease(certName),@"USERID",nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UserInfoReceived" object:nil];
    
    //CFRelease(cert);
    //CFRelease(certName);
    return identityApp;
}



I've also used the following function to get information from the certificate.

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




Now i need to extract the public/private key pair from the certificate, i've searched a lot and i even read the "Certificate, Key, and Trust Services Programming Guide", and i've found some functions that generates a key pair for use on the mobile device. But i want the key-pair of the my certificate (the one which i've retrieved from the above function).

how can i do this?

Best Regards.
Posted
Updated 2-Mar-15 8:30am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900