SecureEnclave
我正在尝试在with中创建一个私钥/公钥对CryptoKit
,然后保存对私钥的引用以KeyChain
供进一步使用。密钥生成完全正常:
let accessControl = SecAccessControlCreateWithFlags(
kCFAllocatorDefault,
kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
[.privateKeyUsage],
nil
)!
let privateKey = try SecureEnclave.P256.Signing.PrivateKey(accessControl: accessControl)
// Describe the key.
let attributes = [
kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass: kSecAttrKeyClassPrivate
] as [String: Any]
但是当我尝试将刚刚创建的密钥转换为SecKey
稍后将其存储在钥匙串中时:
// Get a SecKey representation.
var error: Unmanaged<CFError>?
guard let secKey = SecKeyCreateWithData(key.dataRepresentation as CFData, attributes as CFDictionary, nil) else {
throw error!.takeRetainedValue()
}
它失败并出现错误:
The operation couldn’t be completed. (OSStatus error -50 - EC private key creation from data failed)
另一方面,当我删除SecureEnclave
元素并使用时,x963Representation
我能够将私钥转换为SecKey
对象:
let privateKey = P256.Signing.PrivateKey()
guard let secKey = SecKeyCreateWithData(privateKey.x963Representation as CFData, attributes as CFDictionary, nil) else {
throw error!.takeRetainedValue()
}
关于为什么会发生这种情况或我该如何解决这个问题的任何想法?