简体   繁体   English

Apple,iOS 13,CryptoKit,Secure Enclave - 在使用私钥之前实施生物特征认证

[英]Apple, iOS 13, CryptoKit, Secure Enclave - Enforce biometric authentication ahead of private key usage

I am working with Apple's new cryptokit library and am trying to get a basic use case to work.我正在使用 Apple 的新 cryptokit 库,并试图让一个基本的用例正常工作。

Goal : I would like to create a private key in the secure enclave via the cryptokit, store the key's reference in the iOS device's key chain and ensure that the key can only be reinitialized in the secure enclave after the user has authenticated himself via some biometric authentication method.目标:我想通过 cryptokit 在安全飞地中创建一个私钥,将密钥的引用存储在 iOS 设备的密钥链中,并确保只有在用户通过某些生物特征验证自己之后才能在安全飞地中重新初始化密钥身份验证方法。

Current state : So far, I am able to initialize a private key in the secure enclave via the following code:当前 state :到目前为止,我可以通过以下代码在安全飞地中初始化私钥:

var privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init();

Furthermore, I can store and retrieve the corresponding private key's reference from the key chain.此外,我可以从钥匙链中存储和检索相应的私钥的引用。 After retrieving the reference, I can reinitialize the private key in the secure enclave with the following code:检索到引用后,我可以使用以下代码重新初始化安全飞地中的私钥:

var privateKeyReference = getPrivateKeyReferenceFromKeyChain();
var privateKey = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
   dataRepresentation: privateKeyReference
);

So far everything works as expected and all cryptographic operations with the private key succeed.到目前为止,一切都按预期工作,并且所有使用私钥的加密操作都成功了。

Now, as far as I understand the spare documentation by Apple , I should be able to modify the first initialization of the private key to something as follows.现在,据我了解Apple 的备用文档,我应该能够将私钥的第一次初始化修改为如下内容。

let authContext = LAContext();
let accessCtrl = SecAccessControlCreateWithFlags(
   kCFAllocatorDefault,
   kSecAttrAccesibleWhenUnlockedThisDeviceOnly,
   [.privateKeyUsage, .userPresence, .biometryCurrentSet],
   nil
);
var privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
   accessControl: accessCtrl!,
   authenticationContext: authContext
);

Thereby, ensuring that the private key can only be reinitialized, when the user authenticates himself via some biometric authentication method.从而确保只有当用户通过某种生物特征认证方法对自己进行认证时,才能重新初始化私钥。 The initial initialization stil works without any errors.初始初始化仍然没有任何错误。

Problem : However, adding the previous code, I do not get any biometric authentication prompt and can not use the private key at all after reinitialization.问题:但是,添加之前的代码后,我没有得到任何生物认证提示,并且在重新初始化后根本无法使用私钥。 The following error is logged whenever I try to execute some cryptographic operation with the reinitialized key, here for example some signing:每当我尝试使用重新初始化的密钥执行一些加密操作时,都会记录以下错误,例如一些签名:

Error Domain=CryptoTokenKit Code=-9 "setoken: unable to sign digest" UserInfo={NSLocalizedDescription=setoken: unable to sign digest})

As far as I could guess from here , I think that Code=-9 refers to the "authenticationNeeded" error.据我从这里猜到的,我认为Code=-9指的是“authenticationNeeded”错误。

Question : Can someone point me to some documentation or tutorial how to achieve what I am looking for or explain to me what I am missing?问题:有人可以向我指出一些文档或教程如何实现我正在寻找的东西或向我解释我所缺少的东西吗?

Thanks!谢谢!

Cross-Post: https://forums.developer.apple.com/message/387746交叉发布: https://forums.developer.apple.com/message/387746

After a couple of days of patience I was able to obtain an answer from the Apple development support.经过几天的耐心,我能够从 Apple 开发支持处获得答案。 They suggested the following method which only differs a little bit from my approach:他们建议了以下方法,它与我的方法略有不同:

var error: Unmanaged<CFError>? = nil;
let accessCtrl = SecAccessControlCreateWithFlags(
   nil,
   kSecAttrAccesibleAfterFirstUnlockThisDeviceOnly,
   [.privateKeyUsage, .biometryCurrentSet],
   &error
);
var privateKeyReference = try CryptoKit.SecureEnclave.P256.KeyAgreement.PrivateKey.init(
   accessControl: accessCtrl
);

Additionally, in the meantime iOS version 13.1.3 was released and, after upgrading my device, the above code started working.此外,与此同时 iOS 版本13.1.3发布,升级我的设备后,上述代码开始工作。 So either there is a subtle difference between mine and Apple's code or it is related to the update.因此,要么我的代码与 Apple 的代码之间存在细微差别,要么与更新有关。 Nevertheless, it is working now.尽管如此,它现在正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用来自Secure Enclave的EC私钥对数据签名的身份验证错误 - Authentication error attempting to sign data with EC private key from Secure Enclave iOS:存储在安全飞地中的密钥不支持解密 - iOS: Decryption not supported on key stored in secure enclave Safari 浏览器的 IOS 安全飞地密钥存储 - IOS secure enclave key storage for Safari Browser IOS:无法从 CryptoKit SecureEnclave 私钥创建 SecKey 表示(OSStatus 错误 -50 - 从数据创建 EC 私钥失败) - IOS: Unable to create SecKey representation from CryptoKit SecureEnclave Private Key (OSStatus error -50 - EC private key creation from data failed)) 安全区域:在密钥创建后更新SecAccessControlCreateFlags - Secure Enclave: update SecAccessControlCreateFlags after key creation iOS:应用扩展之间是否共享安全飞地? - iOS: Is secure enclave shared between app extensions? iOS Swift Diffie-Hellman 密钥交换加密和解密消息? 使用安全飞地 - iOS Swift Diffie-Hellman key exchange to encrypt and decrypt messages? using Secure Enclave CryptoKit 在 iOS 版本低于 13 的手机上抓取应用程序 - CryptoKit craches app on phones with iOS version below 13 iOS Secure Enclave 中可以存储多少个密钥? - How many keys can be stored in the iOS Secure Enclave? Openssl 验证失败,iOS Secure Enclave 创建签名 - Openssl verify fails with iOS Secure Enclave created signature
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM