我正在使用https://code.tutsplus.com/tutorials/creating-digital-signatures-with-swift--cms-29287来创建数字签名,接下来是ECDSA部分。
我创建了以下代码:
@available(iOS 9.0, *)
class User
{
public var publicKey : SecKey?
private var privateKey : SecKey?
private var recipient : User?
init(withUserID id : String)
{
if let access = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenUnlockedThisDeviceOnly, [.privateKeyUsage], nil) //Keep private key on device
{
let privateTagString = "com.example.privateKey." + id
let privateTag = privateTagString.data(using: .utf8)! //Store it as Data, not as a String
let privateKeyParameters : [String : AnyObject] = [kSecAttrIsPermanent as String : true as AnyObject,
kSecAttrAccessControl as String : access as AnyObject,
kSecAttrApplicationTag as String : privateTag as AnyObject,
]
let publicTagString = "com.example.publicKey." + id
let publicTag = publicTagString.data(using: .utf8)! //Data, not String
let publicKeyParameters : [String : AnyObject] = [kSecAttrIsPermanent as String : false as AnyObject,
kSecAttrApplicationTag as String : publicTag as AnyObject,
]
let keyPairParameters : [String : AnyObject] = [kSecAttrKeySizeInBits as String : 256 as AnyObject,
kSecAttrKeyType as String : kSecAttrKeyTypeEC,
kSecPrivateKeyAttrs as String : privateKeyParameters as AnyObject,
kSecAttrTokenID as String : kSecAttrTokenIDSecureEnclave as AnyObject, //Store in Secure Enclave
kSecPublicKeyAttrs as String : publicKeyParameters as AnyObject]
let status = SecKeyGeneratePair(keyPairParameters as CFDictionary, &publicKey, &privateKey)
if status != noErr
{
print("Key generation error \(status)")
}
}
}
}
现在,每次出现错误-4时,我都认为该错误是https://opensource.apple.com/source/Security/Security-55471/sec/Security/SecBase.h.auto.html函数或未实现的错误。
如何创建公钥和私钥?我尝试通过以下方式手动设置公钥和私钥:
let keyBase64 = "MIIEpAIBAAKCAQEA5B7lqLrwVCFNUiCmwMr5Q48iuArOolxb7DAuclGnoZVX0SaJ8mrvCOtd6qY/VeBw227txWEPH7840qX/yGxxqTngdNCuDATqYrrbxFOGV30GZmg6NpZYKShTlsftkhiCsoXW0A7m5MCZUkH2/sNB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Z7SuVhIqECgYBMsS+IovrJ9KhkFZWp5FFFRo4XLqDcXkWcQq87HZ66L03xGwCmV/PPdPMkKWKjFELpebnwbl1Zuv5QrZhfaUfFFsW5uF/RPuS7ezo+rb7jYYTmDlB3DYUTeLbHalMoEeV16xPK1yDlxeMDaFx+3sK0MBKBAsqurvP58txQ7RPMbQKBgQCzRcURopG0DF4VF4+xQJS8FpTcnQsQnO/2MJR35npA2iUb+ffs+0lgEdeWs4W46kvaF1iVEPbr6She+aKROzE9Bs25ZCgGLv97oUxDQo0IPvURX7ucN+xOUU1hw9oQDVdGKl1JZh93fn+bjtMTe+26asGLmM0r9YQX1P8qaw3KOg=="
let keyData = Data(base64Encoded: keyBase64)!
let key = SecKeyCreateWithData(keyData as NSData, [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
] as NSDictionary, nil)!
print(key)
但这真的没有意义,因为SecKeyGeneratePair应该生成密钥对!
答案 0 :(得分:1)
错误状态-4不仅具有errSecUnimplemented
,而且具有errSecUnsupportedAlgorithm
和errSecUnsupportedOperation
的多种含义,但是,在这种情况下,很可能是{{1 }}。
这样做的原因是您尝试从“安全区域”检索私钥,这是“不可能的”。 Secure Enclave的部分安全性是它无法将私钥公开给您。这样就可以了,这就是为什么它向您抛出errSecUnimplemented
错误。
因此,如果您想使用Secure Enclave,我建议您从Apple提供的security documentation中查找更多内容。
我可以为您提供的解决方案如下:
errSecUnimplemented
这会将私钥存储在KeyChain中。希望这会有所帮助!