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).
(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)
{
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));
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserInfoReceived" object:nil];
return identityApp;
}
I've also used the following function to get information from the certificate.
-(NSString *)copySummaryString:(SecIdentityRef) 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;
}
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.