Apple 安全工具:如何从钥匙串中删除私钥?

Apple security tool: how to delete a private key from a keychain?

我正在寻找一种使用 Apple 的安全工具从钥匙串中删除私钥的方法。在这种特定情况下,私钥还有一个与之关联的证书,它形成一个身份(证书 + 私钥),因此您可以单击钥匙串访问的 "certificates" 部分,右键单击 "Imported Private Key" 在下拉菜单中单击 "Delete":

然而,据我所知,使用 security 工具无法做到这一点(参见 https://ss64.com/osx/security.html);可用于删除的命令是

> security help | grep delete
    delete-keychain                      Delete keychains and remove them from the search list.
    delete-generic-password              Delete a generic password item.
    delete-internet-password             Delete an internet password item.
    delete-certificate                   Delete a certificate from a keychain.
    delete-identity                      Delete an identity (certificate + private key) from a keychain.

delete-identity 也会删除证书。有什么方法可以通过编程实现与 Keychain Access 命令相同的效果吗?

(我确实注意到 Delete Private Key from Keychain and Delete Private key from keychain mac programmatically 引用了 Swift 和 Objective-C 解决方案,但我更愿意使用 Go 或 Apple 命令行工具)。

我最终通过使用 security delete-identity 删除证书和私钥然后使用 security import 仅重新导入证书来迂回地执行此操作。