通过脚本以安全的方式生成 SSH 密钥对
我需要生成一个 SSH 密钥对,稍后我将在程序中使用它,因此需要它们作为字符串。不幸的是,ssh-keygen
实用程序不支持将密钥写入 STDOUT 或类似内容。
因此,“下一个最好的事情”是让 ssh-keygen 将其输出写入临时文件,然后我可以将其读回到程序中。然而,这会带来系统上其他人读取私钥文件的风险(该脚本将由 Web 应用程序运行)。
如何以安全的方式生成密钥对?
I need to generate an SSH key pair that I'll be working with later in the program, and therefore need them as strings. Unfortunately, the ssh-keygen
utility doesn't support writing the keys to STDOUT or something the like.
So, the "next best thing" would be to have ssh-keygen
write its output to temporary files, which I can then read back into the program. This however poses the risk of somebody else on the system reading the private keyfile (the script will be run by a web app).
How can I generate a key pair in a way that is secure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有完全安全的方法来生成用于分发的私钥;最安全的方法是让每个客户端生成其密钥对并仅传输公钥。
但是,如果您处于这种情况根本行不通,并且您致力于集中生成密钥对,那么您确实有一些选择。
有硬件安全模块 (HSM) 可以为您执行此操作。如果你能负担得起,这是最好的选择。
如果失败:
如果您真的很偏执,您还可以通过让您的 Web 应用程序使用密钥来确保密钥在磁盘上进行加密它自己的对称密钥在写入时加密并在发送给用户时解密。然而,这会导致更多的密钥管理失败,所以我不推荐它。
如果您从不将该密码存储到磁盘,则在密钥上设置密码也会有很大帮助。如果有人确实设法恢复受密码保护的密钥,他们仍然必须猜测密码才能使密钥有用。然而,这也意味着您的用户在使用密钥时必须提供密码——这可能可接受也可能不可接受。
There is no perfectly secure way to generate private keys for distribution; the most secure way is to have each client generate its keypairs and only transmit the public key.
However, if you're in a situation where that simply won't work, and you're committed to generating keypairs centrally, you do have a few options.
There are Hardware Security Modules (HSMs) that do this for you. If you can afford it, this is the best way to go.
Failing that:
If you're really paranoid, you can also ensure that the keys are encrypted while on disk by having your web application use its own symmetric key to encrypt on writing and decrypt when sending to the user. However, that opens up more key-management to screw up, so I don't recommend it.
Setting a passphrase on the key will help a lot as well, if you never store that passphrase to disk. If someone does manage to recover a passphrase-protected key, they still have to guess the passphrase for the key to be useful. However, this also means your users will have to provide a passphrase when they use the key -- this may or may not be acceptable.