KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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 does not work. First I am checking if the current key (=key in case of that the Keychain is a key/value store) is 'free' in the Keychain. Then I am going to add the private key. CFStringRef labelstring = CFStringCreateWithCString(NULL, [key cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8); NSArray* keys = [NSArray arrayWithObjects:(__bridge id)kSecClass,kSecAttrLabel,kSecReturnData,kSecAttrAccessible,nil]; NSArray* values = [NSArray arrayWithObjects:(__bridge id)kSecClassKey,labelstring,kCFBooleanTrue,kSecAttrAccessibleWhenUnlocked,nil]; NSMutableDictionary* searchdict = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys]; CFRelease(labelstring); NSMutableDictionary *query = searchdict; CFTypeRef item = NULL; OSStatus error = SecItemCopyMatching((__bridge_retained CFDictionaryRef) query, &item); if (error) { NSLog(@"Error: %ld (statuscode)", error); } if(error != errSecItemNotFound) { SecItemDelete((__bridge_retained CFDictionaryRef) query); } [query setObject:(id)data forKey:(__bridge id)kSecValueData]; OSStatus status = SecItemAdd((__bridge_retained CFDictionaryRef) query, &item); if(status) { NSLog(@"Keychain error occured: %ld (statuscode)", status); return NO; } The debug output is the following: 2012-07-26 15:33:03.772 App[15529:1b03] Error: -25300 (statuscode) 2012-07-26 15:33:11.195 App[15529:1b03] Keychain error occured: -25299 (statuscode) The first error code -25300 represents errSecItemNotFound . So there is no value stored for this key. Then, when I try to add the private key into the Keychain I get -25299 which means errSecDuplicateItem . I do not understand this. Why is this happening? <
Tags (comma-separated)
Save Edits
Cancel