以编程方式从 Mac 钥匙串中存储和检索私钥

发布于 2024-11-17 20:22:50 字数 354 浏览 1 评论 0原文

在 Mac 应用程序中,我需要以安全的方式存储从服务器发送的登录用户的私钥,并在需要时以编程方式检索它。我知道钥匙扣是存储私钥的最佳位置。是否有任何示例代码可以实现此目的?

我可以使用“Security.framework”的“SecKeychainItemImport”方法将私钥添加到钥匙串中,但从钥匙串中检索私钥时遇到问题。我尝试使用“SecKeychainItemCopyAttributesAndData”和“SecKeychainItemCopyContent”方法从钥匙串中获取私钥。但到目前为止还没有运气。

我还在博客中读到提到“.ssh”隐藏文件夹中的私钥存储。但我认为将私钥存储在钥匙串中可以提供更高级别的安全性,以便其他人无法轻松访问私钥。

In a Mac application, I have a requirement to store the private key sent from the server for logged in user in a secure way and retrieve it back whenever needed programmatically. I know that keychain is the best place to store the private key. Is there any sample code available to achieve this?

I am able to add the private key to the keychain using "SecKeychainItemImport" method of "Security.framework" but having issues retrieving back the private key from the keychain. I have tried using "SecKeychainItemCopyAttributesAndData" and "SecKeychainItemCopyContent" methods for getting private key back from the keychain. But no luck so far.

I have also read in blogs mentioning private key storage inside ".ssh" hidden folder. But I feel that storing the private key inside the keychain provides one more level of security so that someone else can not have an easy access to the private key.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2024-11-24 20:22:50

钥匙串的目的之一是通过不将私钥数据暴露给应用程序来保护私钥。为了防止意外泄露私钥,这些项目被标记为CSSM_KEYATTR_EXTRACTABLE |默认情况下CSSM_KEYATTR_SENSITIVE;即,只能使用 SecKeychainItemExport 获取其数据,并且只能采用受密码保护的格式。

安全框架中的 API 可以使用提供的密钥项对数据进行加密/解密/签名/验证等,而无需将原始密钥数据放入应用程序的地址空间中。 (这些操作通常由单独的特权进程完成。)

如果由于某种原因您确实需要访问私钥的原始位,则需要在将私钥导入钥匙串时为此做好准备。您需要在SecKeychainItemImportkeyParams参数中将keyAttributes设置为CSSM_KEYATTR_EXTRACTABLE(即不带敏感位)。

One purpose of the Keychain is to keep private keys protected by not exposing their data to the application. To prevent accidentally exposing a private key, these items are flagged CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_SENSITIVE by default; i.e., it is only possible to get their data using SecKeychainItemExport, and only in a passphrase-protected format.

There are APIs in the Security framework that encrypt/decrypt/sign/verify etc. data using a supplied key item without ever putting the raw key data in the application's address space. (These operations are normally done by a separate, privileged process.)

If for some reason you do need access to the private key's raw bits, you need to prepare for this at the time you import the private key to the keychain. You need to set keyAttributes to CSSM_KEYATTR_EXTRACTABLE (i.e., without the sensitive bit) in the keyParams parameter of SecKeychainItemImport.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文