生成自签名ssl证书
?? 一般情況下,如果能找到可用的證書,就可以直接使用,只不過會因證書的某些信息不正確或與部署證書的主機(jī)不匹配而導(dǎo)致瀏覽器提示證書無效,但這并不影響使用。
需要手工生成證書的情況有:
- 找不到可用的證書
- 需要配置雙向SSL,但缺少客戶端證書
- 需要對證書作特別的定制
首先,無論是在Linux下還是在Windows下的Cygwin中,進(jìn)行下面的操作前都須確認(rèn)已安裝OpenSSL軟件包。
1. 創(chuàng)建根證書密鑰文件(自己做CA)root.key:
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: ← 重新輸入一遍密碼
2. 創(chuàng)建根證書的申請文件root.csr:
openssl req -new -key root.key -out root.csr輸出:
Enter pass phrase for root.key: ← 輸入前面創(chuàng)建的密碼 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. 創(chuàng)建一個自當(dāng)前日期起為期十年的根證書root.crt:
openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt輸出內(nèi)容為:
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: ← 輸入前面創(chuàng)建的密碼
4. 創(chuàng)建服務(wù)器證書密鑰server.key:
openssl genrsa -des3 -out server.key 2048輸出內(nèi)容為:
Generating RSA private key, 2048 bit long modulus ….+++
……………………………………………+++ e is 65537 (0×10001)
運(yùn)行時會提示輸入密碼,此密碼用于加密key文件(參數(shù)des3便是指加密算法,當(dāng)然也可以選用其他你認(rèn)為安全的算法.),以后每當(dāng)需讀取此文件(通過openssl提供的命令或API)都需輸入口令.如果覺得不方便,也可以去除這個口令,但一定要采取其他的保護(hù)措施!
去除key文件口令的命令:
openssl rsa -in server.key -out server.key
5.創(chuàng)建服務(wù)器證書的申請文件server.csr:
openssl req -new -key server.key -out server.csr輸出內(nèi)容為:
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
← 服務(wù)器主機(jī)名,若填寫不正確,瀏覽器會報告證書無效,但并不影響使用 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.創(chuàng)建自當(dāng)前日期起有效期為期兩年的服務(wù)器證書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輸出內(nèi)容為:
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: ← 輸入前面創(chuàng)建的密碼
7. 創(chuàng)建客戶端證書密鑰文件client.key:
openssl genrsa -des3 -out client.key 2048輸出內(nèi)容為:
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. 創(chuàng)建客戶端證書的申請文件client.csr:
openssl req -new -key client.key -out client.csr輸出內(nèi)容為:
Enter pass phrase for client.key: ← 輸入上一步中創(chuàng)建的密碼 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. 創(chuàng)建一個自當(dāng)前日期起有效期為兩年的客戶端證書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輸出內(nèi)容為:
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: ← 輸入上面創(chuàng)建的密碼
10. 將客戶端證書文件client.crt和客戶端證書密鑰文件client.key合并成客戶端證書安裝包client.pfx:
openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx輸出內(nèi)容為:
Enter pass phrase for client.key: ← 輸入上面創(chuàng)建的密碼 Enter Export Password: ←
輸入一個新的密碼,用作客戶端證書的保護(hù)密碼,在客戶端安裝證書時需要輸入此密碼 Verifying – Enter Export
Password: ← 確認(rèn)密碼
保存生成的文件備用,其中server.crt和server.key是配置單向SSL時需要使用的證書文件,client.crt是配置雙向SSL時需要使用的證書文件,client.pfx是配置雙向SSL時需要客戶端安裝的證書文件
.crt文件和.key可以合到一個文件里面,把2個文件合成了一個.pem文件(直接拷貝過去就行了)
11. 生成pem文件
cat server.crt server.key |tee server.pem[寶塔上部署證書](https://www.bt.cn/bbs/thread-704-1-1.html) [https://www.bt.cn/bbs/thread-704-1-1.html](https://www.bt.cn/bbs/thread-704-1-1.html) 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的生成自签名ssl证书的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注册树模式
- 下一篇: phpstrom 操作技巧