自籤SSL證書(多域名/IP)
本文基於以下環境:
內核信息:Linux zabbix 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
系統版本:CentOS Linux release 7.6.1810 (Core)
OpenSSL版本:OpenSSL 1.0.2k-fips 26 Jan 2017
【前言】
在基於OpenSSL的CA建立及證書籤發(簽發單域名/IP)一文中(後面統一稱作上文中),我已經有詳細的介紹如何用openssl自籤一個根證書以如何用自籤的根證書去簽發一個SSL證書。在上文中的前言裏我有說到做nginx的https代理需要一個自籤SSL證書,其實這個自籤SSL證書是用來跟七牛雲進行傳輸的,衆所周知,七牛雲的上傳和下載是走到兩個域名,具體的的這裏就不展開說了,後面有時間再單獨出一篇關於七牛雲的文章吧。那麼顯然在上文中籤發的單一域名不滿足這個需求。解決的辦法有兩種,第一就是再簽發一個SSL證書,第二種就是簽發多域名SSL證書。好了,廢話不多說,我們來看看要怎麼操作吧。
【OpenSSL自籤多域名/IP證書】
大致流程如下
一、創建index.txt、serial等文件
二、生成CA根證書
1.創建根證書私鑰
2.使用根證書私鑰創建一個自籤ca根證書的申請
3.使用申請和私鑰簽發ca根證書
三、修改openssl配置文件
四、用修改後的配置文件生成SSL證書
1.創建自簽證書私鑰
2.創建一個自簽證書申請
3.使用自籤的根證書對自簽證書申請進行簽署
如果看了我寫的單域名簽發就會發現在這裏多了一步修改openssl配置文件,這一步就是爲自簽發多域名做準備的。這裏要提及一個新名詞SubjectAltName,簡稱SAN。於我個人理解,它就是X509數字證書中的一個擴展項,用來添加多個簽發的域名/ip的一個擴展項,在openssl的默認配置中是沒有打開的,詳細解說移步這裏:(https://blog.csdn.net/henter/article/details/91351800)。接下來是正式的操作步驟。
一、創建index.txt、serial等文件
[root@zabbix ca]# cd /etc/pki/CA
[root@zabbix CA]# touch index.txt
[root@zabbix CA]# echo 00 > serial
二、生成CA根證書
1、創建根證書私鑰
[root@zabbix ca]# openssl genrsa -out ca.key 2048
2、創建根證書申請證書(切記,生成CA根證書的時候不能用修改的openssl.conf文件)
[root@zabbix ca]# openssl req -new -out ca.csr -key ca.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) [XX]:CN
State or Province Name (full name) []:GD
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:scwipe.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3、生成ca根證書
[root@zabbix ca]# openssl x509 -req -days 36500 -in ca.csr -signkey ca.key -out ca.crt
三、修改openssl配置文件
1、複製openssl配置文件
爲了不破壞原始文件,我們使用帶配置文件的方式來生成證書,先把證書複製到另外一個地方,這裏我保存到/root/ca目錄下
[root@zabbix ca]# mkdir -p /root/ca && cp /etc/pki/tls/openssl.cnf /root/ca/
2、修改[req]段落
爲了讓openssl處理證書請求(csr)時,帶上拓展項,所以需要配置req_extensions項,此時[req]必須包含下面兩行
[req]
basicConstraints = CA:TRUE
distinguished_name= req_distinguished_name
req_extensions = v3_req
3、修改[v3_req]段落
增加subjectAltName行如下
subjectAltName = @alt_names
4、增加[alt_names]模塊
此處填寫你需要簽發的域名/ip,如下
[alt_names]
DNS.1=*scwipe.com
DNS.2=*.scwipe.cn
四、用自籤ca證書籤發ssl證書
1、創建ssl證書私鑰
[root@zabbix ca]# openssl genrsa -out scwipe.key 2048
2、創建證書請求文件csr
(此處需要注意國家、省份、城市需要和ca根證書保持一致,當然也可以修改配置文件,具體見簽發 基於OpenSSL的CA建立及證書籤發(簽發單域名/IP))
[root@zabbix ca]# openssl req -new -key scwipe.key -out scwipe.csr -config /root/ca/openssl.cnf -extensions v3_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) [XX]:CN
State or Province Name (full name) []:GD
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:*.scwipe.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
3、用ca根證書籤發ssl證書
openssl ca -in scwipe.csr -out scwipe.crt -cert ca.crt -keyfile ca.key -extensions v3_req -days 36500 -config /root/ca/openssl.cnf
4、驗證自籤SSL證書是否ok
openssl verify -verbose -CAfile ca.crt scwipe.crt
scwipeserver.crt: OK
將簽發的ca根證書和SSL證書導出到win上面查看,先信任自己簽發的根證書,雙擊ca.crt
安裝證書-->選擇本地計算機-->將所有
證書都放入下列存儲-->瀏覽-->受信任的根證書頒發機構-->下一步-->完成
完成上面的操作後,再雙擊簽發的SSL證書,我這裏是scwiper.crt證書,就可以看到如下,就是簽發成功了