2013-03-13 423 views
3

我已經創建了證書頒發機構,需要生成並簽署50個以上的證書。我想編寫這個過程。我不想手動輸入密碼100次以上!腳本openssl生成許多證書,而無需手動輸入密碼?

下面是我漸漸掛了命令:

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: 

當我剛剛被要求要輸入密碼,我可以使用openssl-passin pass:mypass命令行選項。但是這似乎不適用於創建密碼。

而且,它似乎很奇怪的是,當後來我剛剛結束了與刪除它需要密碼:

openssl rsa <tempkey.pem> server_key.pem 

我試圖創建一個簡單的Ruby腳本:

require 'open3' 

Open3.popen2("openssl req -newkey rsa:1024 -keyout ~/myCA/tempkey.pem -keyform PEM -out ~/myCA/tempreq.pem -outform PEM") {|i,o,t| 
    i.puts "mySecretPassword" 
    i.puts "mySecretPassword" 
} 

但這並不似乎也有效。我仍然以手動提示要求我創建密碼。

+0

在ruby的標準庫中有一個OpenSSL類。嘗試使用:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html我現在不能寫一個完整的示例,但我會嘗試這樣做以後如果我找到時間的話。 – Speed 2013-03-13 08:28:17

回答

2

問題是大多數需要密碼的實用程序都需要交互式終端。所以,如果你試圖僞造它(就像你使用Ruby腳本一樣),它將無法工作。您也可以嘗試:

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

雖然這可以在某些程序中使用,但那些需要interative 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腳本」,它真的取決於你的環境 - 什麼應用要求。如果它只是一個密碼,那應該很簡單。下面是更復雜的例子: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"} 

祝你好運!

+0

是的,這工作! – 2013-03-13 10:03:10

3

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"