在 iPhone 上使用 SecKeyRawSign
我正在尝试使用 SecKeyRawSign 签署一些数据,但我不断收到 -4 errSecUnimplemented。这看起来很奇怪,因为文档指出它在 iPhone OS2.0 及更高版本中可用。
有人用过这个功能吗?如果是的话,有没有什么技巧呢?
〜内特
I'm trying to sign some data using SecKeyRawSign but I keep getting a -4 errSecUnimplemented. That seems strange since the documentation states that it is available in iPhone OS2.0 and later.
Has anyone been able to use this function? If so, are there any tricks involved?
~Nate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您遇到此问题,很可能是因为您生成的私钥实际上并未保存到钥匙串中。当停止并重新启动应用程序并且签署消息不起作用时,我发现了这一点。
以下是我实现这项工作的方法。
这个生成密钥对
** 编辑 **
iOS 9 引入了一项名为“Secure Enclave”的新功能。如果您想生成一个仅存储在此处的密钥,则需要使用 256 位 EC 密钥,因为这是 enclave 支持的唯一类型。 keyPairDict 看起来像这样:
我知道参数是正确的,但我自己还没有测试过 Secure Enclave,所以如果不正确请告诉我由于某种原因工作。
另外,仅供参考:
256 位 EC
密钥相当于3072 位 RSA
密钥。用于检索下面密钥的查询也将有所不同:
因为Secure Enclave非常安全,因此您很可能无法检索私钥位。最有可能的是,您只能生成参考。但无论如何您都不需要处理私钥数据。
** END EDIT **
此方法从钥匙串中检索实际位,而不仅仅是引用
此方法将这些位保存到钥匙串
然后您像这样签名:
If you're having this problem, most likely it is because the private key you generated isn't actually being saved into the keychain. I figured this out when stopping and restarting the application and signing the message wasn't working.
So here are my methods to make this work.
This one generates the key pair
** EDIT **
iOS 9 introduced a new feature called the Secure Enclave. If you want to generate a key that will be stored there and only there, you will be required to use a
256-bit EC
key, as that is the only type supported by the enclave. ThekeyPairDict
will look like this instead:I know the parameters are correct, but I haven't myself tested the Secure Enclave yet, so let me know if this doesn't work for some reason.
Also, for reference: a
256-bit EC
key is equivalent to a3072-bit RSA
key.The query used to retrieve the key below would also be different:
Because the Secure Enclave is, well, secure, you most likely won't be able to retrieve the private key bits. Most likely, you'll only be able to generate a reference. But you shouldn't need to handle the private key data anyway.
** END EDIT **
This method retrieves the actual bits from the keychain and not just the reference
This method saves the bits to the keychain
And then you sign like so:
-4 errSecUnimplemented 错误是由对用于签署数据的私钥的错误引用引起的。在这种情况下会出现令人困惑的错误。如果有 errSecParam 就更好了。
~NAte
The -4 errSecUnimplemented error was being caused by a bad reference to the private key used to sign the data. Confusing error for that situation. A errSecParam would have been nicer.
~NAte