2013年11月11日 星期一

SSL Certificates

OpenSSl Self-Signed Certificate Generation
底下將透過Ubuntu下的openssl指令產生數位憑證(Certificates),在產生憑證前,你需要用下面的指令產生一把private key,並要求你輸入密碼保護這把key。
$ openssl genrsa -des3 -out server.key 2048
但為了方便起見,最好不要為key設密碼,不然你每次啟動secure services都要輸入密碼,例如你架了一個SSL連線加密保護的Web Server (HTTPS),每次你啟動它的時候都要輸入密碼,萬一你的server主機出問題自動重開機,使用者就無法連到你的Web Server,除非你為key輸入密碼。底下的指令是透上面產生的key再產生一把沒有設密碼的key,至於你要用哪一把就看你囉!如果是我,我會選擇沒設密碼的private key。
$ openssl rsa -in server.key -out server.key.insecure
$ mv server.key server.key.secure
$ mv server.key.insecure server.key
接下來,要產生一張Certificate Signing Request (CSR),指令如下。
$ openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:TW
State or Province Name (full name) [Some-State]:Taiwan
Locality Name (eg, city) []:Kaohsiung City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Renee's Blog
Organizational Unit Name (eg, section) []:Owner
Common Name (e.g. server FQDN or YOUR name) []:reneeciou.blogspot.tw
Email Address []:reneeciou@example.com.tw

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
如果你是用於商業活動行為,例如你架了一個購物網站,需要SSL加密連線確保使用者與伺服器連線安全,最好找知名可靠且受信任的憑證管理中心 (Certification Authority, CA),將這張CSR與相關的申請書交給CA,CA審核完後確認無誤,會透過它的private key去sign你的CSR產生一張Certificate,表示你的網路身分生效,且具有法律效用,並將數位憑證交給你,之後你的伺服器就可以使用這張數位憑證,但是這是需要收費的,像國外知名的憑證品牌VeriSign,一年費用換算成台幣,一萬元是跑不掉。如果你只是測試或內部網路伺服器用,你不需要花錢去購買憑證,你可以用自己的private key去sign自己的CSR去產生憑證,只要透下面指令。
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
安裝憑證。
$ sudo cp server.crt /etc/ssl/certs
$ sudo cp server.key /etc/ssl/private
Certification Authority
如果你的內部網路需要多張憑證用於多樣的secure services,就好設置一個內部的Certification Authority來管理憑證,底下的步驟將建立一個Root CA。首先,先建立CA相關的資料夾。
$ sudo cp server.crt /etc/ssl/CA
$ sudo cp server.key /etc/ssl/newcerts
為了方便追蹤CA發出多少憑證與已發出憑證的相關資訊,需要建立兩個檔案來記錄。
$ sudo sh -c "echo '01' > /etc/ssl/CA/serial"
$ sudo touch /etc/ssl/CA/index.txt
接下來,設定一下CA,修改此檔案/etc/ssl/openssl.cnf中標示為[ CA_default ]部分。
$ sudo vim /etc/ssl/openssl.cnf
dir             = /etc/ssl/             # Where everything is kept
database        = $dir/CA/index.txt     # database index file.
certificate     = $dir/certs/cacert.pem # The CA certificate
serial          = $dir/CA/serial        # The current serial number
private_key     = $dir/private/cakey.pem# The private key
底下指令用來產生一張self-singed root certificate。
$ openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650
安裝CA的root certificate與private key。
$ sudo mv cakey.pem /etc/ssl/private/
$ sudo mv cacert.pem /etc/ssl/certs/
之後,你就可以透過此CA去認證跟你申請憑證的人,主要是透過CA的private key去sign CSR產生憑證,並將憑證交給你認可的申請者。透過下面的指令去sign server.csr檔案,產生的檔案會放在/etc/ssl/newcerts/01.pem,請將此檔的內容裡的 -----BEGIN CERTIFICATE----- 到 ----END CERTIFICATE----- 複製並另存新檔案為server.crt,此張就是申請者的憑證。之後,陸續sign第二與第三等申請者,產生對應的檔案分別為02.pem與03.pem等。
$ sudo openssl ca -in server.csr -config /etc/ssl/openssl.cnf
申請者拿到被CA sign過的憑證,預設安裝到/etc/ssl/certs裡,然後設置secure services使用這張憑證。

References
1. Ubuntu官方網站Certificates介紹
2. 作者:依瑪貓, http://www.imacat.idv.tw/tech/sslcerts.html#termcert
3. SSL Certificates HOWTO

沒有留言:

張貼留言