SSL證書生成方法

通常狀況下,若是能找到可用的證書,就能夠直接使用,只不過會因證書的某些信息不正確或與部署證書的主機不匹配而致使瀏覽器提示證書無效,但這並不影響使用。html

須要手工生成證書的狀況有:git

  1. 找不到可用的證書web

  2. 須要配置雙向SSL,但缺乏客戶端證書算法

  3. 須要對證書做特別的定製瀏覽器

首先,不管是在Linux下仍是在Windows下的Cygwin中,進行下面的操做前都須確認已安裝OpenSSL軟件包。安全

1. 建立根證書密鑰文件(本身作CA)root.key服務器

openssl genrsa -des3 -out root.keyide

輸出內容爲:測試

[lenin@archer ~]$ openssl genrsa -des3 -out root.key      
Generating RSA private key, 512 bit long modulus      
……………..++++++++++++      
..++++++++++++      
e is 65537 (0×10001)      
Enter pass phrase for root.key: ← 輸入一個新密碼      
Verifying – Enter pass phrase for root.key: ← 從新輸入一遍密碼ui

2. 建立根證書的申請文件root.csr

openssl req -new -key root.key -out root.csr

輸出內容爲:

[lenin@archer ~]$ openssl req -new -key root.key -out root.csr      
Enter pass phrase for root.key: ← 輸入前面建立的密碼      
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]:CN ← 國家代號,中國輸入CN      
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音      
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音      
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名      
Organizational Unit Name (eg, section) []: ← 能夠不輸入      
Common Name (eg, YOUR name) []: ← 此時不輸入      
Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填

Please enter the following ‘extra’ attributes      
to be sent with your certificate request      
A challenge password []: ← 能夠不輸入      
An optional company name []: ← 能夠不輸入

3. 建立一個自當前日期起爲期十年的根證書root.crt

openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.req -out root.crt

輸出內容爲:

[lenin@archer ~]$ openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt      
Signature ok      
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com      
Getting Private key      
Enter pass phrase for root.key: ← 輸入前面建立的密碼

4. 建立服務器證書密鑰server.key

openssl genrsa –des3 -out server.key 2048

輸出內容爲:

[lenin@archer ~]$ openssl genrsa -out server.key 2048      
Generating RSA private key, 2048 bit long modulus      
….+++      
…………………………………………..+++      
e is 65537 (0×10001)

運 行時會提示輸入密碼,此密碼用於加密key文件(參數des3即是指加密算法,固然也能夠選用其餘你認爲安全的算法.),之後每當需讀取此文件(經過 openssl提供的命令或API)都需輸入口令.若是以爲不方便,也能夠去除這個口令,但必定要採起其餘的保護措施!      
去除key文件口令的命令:      
openssl rsa -in server.key -out server.key

5.建立服務器證書的申請文件server.csr

openssl req -new -key server.key -out server.csr

輸出內容爲:

[lenin@archer ~]$ openssl req -new -key server.key -out server.req      
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]:CN ← 國家名稱,中國輸入CN      
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音      
Locality Name (eg, city) []:BeiJing ← 市名,拼音      
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名      
Organizational Unit Name (eg, section) []: ← 能夠不輸入      
Common Name (eg, YOUR name) []:www.mycompany.com ← 服務器主機名,若填寫不正確,瀏覽器會報告證書無效,但並不影響使用      
Email Address []:admin@mycompany.com ← 電子郵箱,可隨便填

Please enter the following ‘extra’ attributes      
to be sent with your certificate request      
A challenge password []: ← 能夠不輸入      
An optional company name []: ← 能夠不輸入

6. 建立自當前日期起有效期爲期兩年的服務器證書server.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.crt

輸出內容爲:

[lenin@archer ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt      
Signature ok      
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com      
Getting CA Private Key      
Enter pass phrase for root.key: ← 輸入前面建立的密碼

7. 建立客戶端證書密鑰文件client.key

openssl genrsa -des3 -out client.key 2048

輸出內容爲:

[lenin@archer ~]$ openssl genrsa -des3 -out client.key 2048      
Generating RSA private key, 2048 bit long modulus      
……………………………………………………………………………..+++      
……………………………………………………………………………………………………….+++      
e is 65537 (0×10001)      
Enter pass phrase for client.key: ← 輸入一個新密碼      
Verifying – Enter pass phrase for client.key: ← 從新輸入一遍密碼

8. 建立客戶端證書的申請文件client.csr

openssl req -new -key client.key -out client.csr

輸出內容爲:

[lenin@archer ~]$ openssl req -new -key client.key -out client.csr      
Enter pass phrase for client.key: ← 輸入上一步中建立的密碼      
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]:CN ← 國家名稱,中國輸入CN      
State or Province Name (full name) [Some-State]:BeiJing ← 省名稱,拼音      
Locality Name (eg, city) []:BeiJing ← 市名稱,拼音      
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名      
Organizational Unit Name (eg, section) []: ← 能夠不填      
Common Name (eg, YOUR name) []:Lenin ← 本身的英文名,能夠隨便填      
Email Address []:admin@mycompany.com ← 電子郵箱,能夠隨便填

Please enter the following ‘extra’ attributes      
to be sent with your certificate request      
A challenge password []: ← 能夠不填      
An optional company name []: ← 能夠不填

9. 建立一個自當前日期起有效期爲兩年的客戶端證書client.crt

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.csr -out client.crt

輸出內容爲:

[lenin@archer ~]$ openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt      
Signature ok      
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com      
Getting CA Private Key      
Enter pass phrase for root.key: ← 輸入上面建立的密碼

10. 將客戶端證書文件client.crt和客戶端證書密鑰文件client.key合併成客戶端證書安裝包client.pfx

openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx

輸出內容爲:

[lenin@archer ~]$ openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx      
Enter pass phrase for client.key: ← 輸入上面建立的密碼      
Enter Export Password: ← 輸入一個新的密碼,用做客戶端證書的保護密碼,在客戶端安裝證書時須要輸入此密碼      
Verifying – Enter Export Password: ← 確認密碼

11. 保存生成的文件備用,其中server.crtserver.key是配置單向SSL時須要使用的證書文件,client.crt是配置雙向SSL時須要使用的證書文件,client.pfx是配置雙向SSL時須要客戶端安裝的證書文件

     .crt文件和.key能夠合到一個文件裏面,把2個文件合成了一個.pem文件(直接拷貝過去就好了)



生成沒有加密的證書:

B2B的安全性離不開數據憑證,但一個專業CA認證的憑證挺花銀子的,一個2048位的Verisign SSL證書大概一年700美金吧,因此研究了下用OpenSSL怎麼製做一個SSL證書,如下是製做步驟給你們分享一下。
固然,要求安全性高的話最好仍是向專業的CA公司申請,本身製做的能夠在內部使用或者是在測試環境中使用。

  1. 下載安裝OpenSSL,配置OpenSSL_HOME\bin環境變量到Path

http://www.slproweb.com/products/Win32OpenSSL.html(已編譯版本)

  1. 製做自簽名的CA證書

opensslgenrsa -out ca.key.pem 2048   (生成一個沒有加密的CA私鑰)

openssl req-new -key ca.key.pem -out ca.csr(生成CA對應的CSR文件)

opensslx509 -in ca.csr -out ca.cer -req -signkey ca.key.pem -days 7300 -extensionsv3_ca(自簽名)

opensslpkcs8 -topk8 -inform PEM -outform DER -in ca.key.pem -out ca.private.der -nocrypt(獲得DER格式的私鑰)

 

  1. 製做Server證書(也能夠利用webMethods Certificate Toolkit 產生)

opensslgenrsa -out server.key.pem 2048   (生成一個沒有加密的Server私鑰)

opensslpkcs8 -topk8 -inform PEM -outform DER -in server.key.pem -out server.private.der-nocrypt(獲得DER格式的私鑰)

opensslreq -new -key server.key.pem -out server.csr (生成Server對應的CSR文件)

 

  1. 利用CAServer證書認證

openssl x509 -req -in server.csr -out server.cer -CA ca.cer -CAkey ca.key.pem-CAserial ca.srl-CAcreateserial -days3650 -extensions v3_req(頒發證書)

 

  1. 生成發給客戶端的P7B證書

openssl crl2pkcs7 -nocrl-certfile server.cer -out server.p7b -certfile ca.cer

:

opensslpkcs8 -inform DER -in server.der -out server.pem –nocrypt

(將私鑰轉換為DER格式)

相關文章
相關標籤/搜索