I am trying to retrieve public key and private key from Mac key chain and convert it into PEM format, which will be used by another process.
I am using the following code snippet to retrieve the private key:
OSStatus status;
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassKey forKey:(id)kSecClass];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnRef];
[query setObject:(id)kSecMatchLimitAll forKey:(id)kSecMatchLimit];
[query setObject:@"<label> forKey:(id)kSecAttrLabel]
SecKeyRef keyRef = NULL;
status = SecItemCopyMatching((_bridge CFDictionaryRef)query, (void *)&keyRef);
if (status != errSecSuccess) {
CFStringRef errorRef = SecCopyErrorMessageString(status, NULL);
NSLog(@"%s: %@", FUNCTION, (bridge NSString *)errorRef);
CFRelease(errorRef);
return ;
}
I got a Non-Null SecKeyRef object, and no error. I verified the label previously by printing attributes.
But I cannot use this SecKeyRef for any other operations like getting public key using SecKeyCopyPublicKey, or I cannot convert into openssl format using SecKeyCopyExternalRepresentation or SecItemExport.
For SecKeyCopyExternalRepresentation, I am getting the error that "export not implemented for key" error
And with SecItemExport I am getting -25260 error.
NSData *data = NULL;
SecItemImportExportKeyParameters params;
params.version = SECKEYIMPORTEXPORTPARAMSVERSION;
params.passphrase = CFSTR("<pwd>");
status = SecItemExport(keyRef, kSecFormatPEMSequence, kSecItemPemArmour, ¶ms, (__bridge CFDataRef)&data);
Could you please let me know If I am missing some other code causing the issue.
I am using the following code snippet to retrieve the private key:
OSStatus status;
NSMutableDictionary *query = [NSMutableDictionary dictionary];
[query setObject:(id)kSecClassKey forKey:(id)kSecClass];
[query setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnRef];
[query setObject:(id)kSecMatchLimitAll forKey:(id)kSecMatchLimit];
[query setObject:@"<label> forKey:(id)kSecAttrLabel]
SecKeyRef keyRef = NULL;
status = SecItemCopyMatching((_bridge CFDictionaryRef)query, (void *)&keyRef);
if (status != errSecSuccess) {
CFStringRef errorRef = SecCopyErrorMessageString(status, NULL);
NSLog(@"%s: %@", FUNCTION, (bridge NSString *)errorRef);
CFRelease(errorRef);
return ;
}
I got a Non-Null SecKeyRef object, and no error. I verified the label previously by printing attributes.
But I cannot use this SecKeyRef for any other operations like getting public key using SecKeyCopyPublicKey, or I cannot convert into openssl format using SecKeyCopyExternalRepresentation or SecItemExport.
For SecKeyCopyExternalRepresentation, I am getting the error that "export not implemented for key" error
And with SecItemExport I am getting -25260 error.
NSData *data = NULL;
SecItemImportExportKeyParameters params;
params.version = SECKEYIMPORTEXPORTPARAMSVERSION;
params.passphrase = CFSTR("<pwd>");
status = SecItemExport(keyRef, kSecFormatPEMSequence, kSecItemPemArmour, ¶ms, (__bridge CFDataRef)&data);
Could you please let me know If I am missing some other code causing the issue.