基于OpenSSL的CA建立及证书签发(签发多域名/IP)

自签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证书,就可以看到如下,就是签发成功了


————————————————
版权声明:本文为CSDN博主「hobby云说」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_24601199/article/details/106634771

posted @ 2023-11-12 12:00  CNHK19  阅读(216)  评论(0编辑  收藏  举报