Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding private key into iOS Keychain
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>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, &amp;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, &amp;item); if(status) { NSLog(@"Keychain error occured: %ld (statuscode)", status); return NO; } </code></pre> <p>The debug output is the following:</p> <pre><code>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) </code></pre> <p>The first error code <code>-25300</code> represents <code>errSecItemNotFound</code>. So there is no value stored for this key. Then, when I try to add the private key into the Keychain I get <code>-25299</code> which means <code>errSecDuplicateItem</code>. I do not understand this. Why is this happening?</p> <p>Does anyone have a clue or hint on this?</p> <p>Apple's error codes:</p> <pre><code>errSecSuccess = 0, /* No error. */ errSecUnimplemented = -4, /* Function or operation not implemented. */ errSecParam = -50, /* One or more parameters passed to a function where not valid. */ errSecAllocate = -108, /* Failed to allocate memory. */ errSecNotAvailable = -25291, /* No keychain is available. You may need to restart your computer. */ errSecDuplicateItem = -25299, /* The specified item already exists in the keychain. */ errSecItemNotFound = -25300, /* The specified item could not be found in the keychain. */ errSecInteractionNotAllowed = -25308, /* User interaction is not allowed. */ errSecDecode = -26275, /* Unable to decode the provided data. */ errSecAuthFailed = -25293, /* The user name or passphrase you entered is not correct. */ </code></pre> <p>Thanks in advance!</p> <p><strong>Update #1: I've figured out that it works only for the first time. Even when data and key is different, after the first time stored into the keychain I cannot store further keys.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload