My little Swift program on macOS 12.3.1 creates a cryptographic key for a symmetric cipher as follows:
let parameters = NSMutableDictionary()
var raw = 256
let num = CFNumberCreate(kCFAllocatorDefault, .sInt32Type, &raw)!
var optError: Unmanaged<CFError>?
parameters.setValue("Pix Cipher", forKey: kSecAttrLabel as String)
parameters.setValue(kSecAttrKeyTypeAES, forKey: kSecAttrKeyType as String)
parameters.setValue(num, forKey: kSecAttrKeySizeInBits as String)
parameters.setValue(kCFBooleanTrue, forKey: kSecAttrIsPermanent as String)
parameters.setValue(kCFBooleanTrue, forKey: kSecAttrCanEncrypt as String)
parameters.setValue(kCFBooleanTrue, forKey: kSecAttrCanDecrypt as String)
key = SecKeyGenerateSymmetric(parameters, &optError)
This key
can be stored in the Key Chain and works fine for encryption and decryption. But when I want to export it using
var error: Unmanaged<CFError>?
let cfData = SecKeyCopyExternalRepresentation(key!, &error)
, this fails, with error
set to something like
Error Domain=NSOSStatusErrorDomain Code=-4 "MacOS error: -4"
What does "MacOS error: -4" mean? (kCFMessagePortTransportError
/kCSIdentityDeletedErr
/unimpErr
?) Why does SecKeyCopyExternalRepresentation
not work? What is wrong with the key?
Kind regards, Jakob