Hi there,
I need to generate a temporary RSA key pair to be used internally in my application, then trashed. I.e., it should never be stored in Keychain.
Since I must support macOSen back at least to 10.10, I am using the SecKeyGeneratePair API (for
SecKeyCreateRandomKey is 10.12+). My first attemptSecKeyGeneratePair((__bridge CFDictionaryRef)@{
(id)kSecAttrKeyType:(id)kSecAttrKeyTypeRSA,
(id)kSecAttrKeySizeInBits:@2048,
}, &pubkey, &privkey);worked well, but in my login keychain, two anonymous keys occurred. Thus, I have tried very explicitly
SecKeyGeneratePair((__bridge CFDictionaryRef)@{
(id)kSecAttrKeyType:(id)kSecAttrKeyTypeRSA,
(id)kSecAttrKeySizeInBits:@2048,
(id)kSecPublicKeyAttrs:@{(id)kSecAttrIsPermanent:(id)kCFBooleanFalse,(id)kSecAttrLabel:@"My test, Pub"},
(id)kSecPrivateKeyAttrs:@{(id)kSecAttrIsPermanent:(id)kCFBooleanFalse,(id)kSecAttrLabel:@"My test, Priv"},
}, &pubkey, &privkey);(note permanent:NO's) — but alas, again, those two keys did occur in my keychain!
How do I create a temporary key pair, whose existence is limited to my application and which never makes it to Keychain? Thanks!
I don’t think that’s possible on older versions of macOS. Support for
kSecAttrIsPermanent in
SecKeyGeneratePair has always been a bit inconsistent between macOS and iOS-based platforms, which is the main reason we introduced
SecKeyCreateRandomKey back in 10.12. If you have to support older platforms you’ll need to find a place to put the keys.
One option is to create a temporary keychain (
SecKeychainCreate) and store the keys there.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"