脚本openssl生成许多证书而无需手动输入密码?

时间:2013-03-13 07:10:40

标签: ruby command-line input openssl popen3

我创建了一个证书颁发机构,需要生成并签署50多个证书。我想编写这个过程的脚本。我不想手动输入密码100次以上!

这是我被挂断的命令:

openssl req -newkey rsa:1024 -keyout ~/myCA/tempkey.pem -keyform PEM -out ~/myCA/tempreq.pem -outform PEM

问题是,它要我用这些提示创建一个密码:

Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:

当我被要求输入密码时,我可以使用-passin pass:mypass的{​​{1}}命令行选项。但这似乎不适用于创建密码。

另外,稍后我需要输入密码似乎很奇怪,我最终将其删除:

openssl

我尝试创建一个简单的Ruby脚本:

openssl rsa < tempkey.pem > server_key.pem

但这似乎也不起作用。我最后还是手动提示,要求我创建一个密码。

2 个答案:

答案 0 :(得分:4)

this answer中所述,您可以使用-passout pass:foobar选项通过命令行设置密码。例如:

openssl req \
  -newkey rsa:1024 -keyout ~/myCA/tempkey.pem -keyform PEM \
  -out ~/myCA/tempreq.pem -outform PEM \
  -passout pass:foobar \
  -subj "/C=US/ST=Test/L=Test/O=Test/CN=localhost"

答案 1 :(得分:3)

问题是大多数希望密码确实需要交互式终端的实用程序。因此,如果你试图伪造它(就像你使用Ruby脚本那样)它将无法工作。你也可以尝试:

echo -n "pass\npass\n" | openssl req ....

虽然这适用于某些程序,但那些需要交互式shell的程序将不起作用。

您正在搜索名为expect的工具。将它安装在UNIX / Linux / MacOS上,然后查看手册页:

man expect
...
Expect is a program that "talks" to other interactive programs according to a script.  Following the script, Expect
knows what can be expected from a program and what the correct response should be.  An  interpreted  language  pro‐
vides  branching  and high-level control structures to direct the dialogue.  In addition, the user can take control
and interact directly when desired, afterward returning control to the script.
...

您需要创建“expect script”,它实际上取决于您的环境 - 应用程序要求的内容。如果它只是一个密码,它应该很简单。这是一个更复杂的例子:http://fixunix.com/openssl/159046-expect-script-doesnt-create-newreq-pem.html

我认为这应该有用(你可能需要稍微修改一下):

#!/usr/bin/expect -f
spawn -console openssl req blah blah blah blah
expect "Enter PEM pass phrase:*" {send "password\r"}
expect "Verifying - Enter PEM pass phrase:*" {send "password\r"}
祝你好运!