I am trying to add a private key into the iOS keychain. The certificate (public key) works fine but the private key refuses... I am totally confused why the following code d
The following code worked for me:
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
[query setObject:(id)kSecClassKey forKey:(id)kSecClass];
[query setObject:(id)kSecAttrAccessibleWhenUnlocked forKey:(id)kSecAttrAccessible];
[query setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];
//adding access key
[query setObject:(id)key forKey:(id)kSecAttrApplicationTag];
//removing item if it exists
SecItemDelete((CFDictionaryRef)query);
//setting data (private key)
[query setObject:(id)data forKey:(id)kSecValueData];
CFTypeRef persistKey; OSStatus status = SecItemAdd((CFDictionaryRef)query, &persistKey);
if(status) {
NSLog(@"Keychain error occured: %ld (statuscode)", status);
return NO;
}