Hello! I am trying to persist my RSA keypair generated with SecKeyCreateRandomKey. I am able to create the keypair with and retrieve the keys while the app is open:
var err: Unmanaged<CFError>?
let privateKeyAttr = [
kSecClass: kSecClassKey,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 2048,
kSecPrivateKeyAttrs: [
kSecAttrIsPermanent: true,
kSecAttrApplicationTag: "tag"
]
] as CFDictionary
guard let privateKey = SecKeyCreateRandomKey(privateKeyAttr, &err) else {
return
}
but when the app is killed and opened again, I am unable to retrieve the private key and SecItemCopyMatching returns -25300 errSecItemNotFound :
let storeAttr = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: "tag",
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecReturnRef: true] as CFDictionary
var item: CFTypeRef?
var key: SecKey
let res = SecItemCopyMatching(storeAttr , &item)
if (res == errSecSuccess) {
return item as! SecKey
} else {
return nil
}