Search code examples
iosmacoscocoasslkeychain

Why does SecPKCS12Import automatically add SecIdentities to the Keychain?


The documentation on SecPKCS12Import states the following:

[…] You can then use the Keychain Services API (see Keychain Services Reference) to put the identities and associated certificates in the keychain.

This means that the items returned in the “items” argument (3rd argument of that function) should not be automatically added to the keychain. However, I have found that those items are automatically added to the keychain when using that function. If I try to add them using SecItemAdd, I get errSecDuplicateItem.

Is this a bug or should it be this way? Why are the items automatically added?

Here is some sample code:

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:@"password", (id)kSecImportExportPassphrase, nil];
CFArrayRef items_ = NULL;
OSStatus ret = SecPKCS12Import((CFDataRef)pkcs12data /* get this from somewhere … */, (CFDictionaryRef)options, &items_);

If you use that code and then open Keychain Access, you’ll see that the certificate and the private key have been added to the keychain.

Regards, David.


Solution

  • It seems like Apple's documentation may be out of date for that link (SecPKCS12Import), because this link https://developer.apple.com/library/ios/qa/qa1745/_index.html mentions that "reading in a PKCS#12-formatted blob and then importing the contents of the blob into the app's keychain using the function SecPKCS12Import..."

    Going by the document revision dates, QA1745 is more recent than the Certificate, Key, and Trust Services Reference.