使用openssl自签发泛域名证书
openssl自签发泛域名(通配符)证书
首先要有openssl工具,如果没有那么使用如下命令安装:
[root@sre ~]# yum install -y openssl openssl-devel
修改openssl.cnf配置文件
具体修改如下
[root@sre ~]# vim /etc/pki/tls/openssl.cnf
[ req ]
………………
# 将如下配置的注释放开
req_extensions = v3_req # The extensions to add to a certificate request
………………
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
# 添加如下行
subjectAltName = @SubjectAlternativeName
# 同时增加如下信息
[SubjectAlternativeName]
DNS.1 = test.com
DNS.2 = *.test.com
说明:本次我们以 *.test.com
泛域名为例。
创建根证书
- 创建CA私钥
[root@sre ~]# mkdir /root/ssl && cd /root/ssl
[root@sre ssl]# openssl genrsa -out CA.key 2048
- 免交互式制作CA公钥、
[root@sre ssl]# openssl req -sha256 -new -x509 -days 36500 -key CA.key -out CA.crt -config /etc/pki/tls/openssl.cnf -subj "/C=CN/ST=JS/L=NanJing/O=other/OU=other/CN=sre/emailAddress=ca@test.com"
subj内容详解:
1 C = Country Name (2 letter code)
2 ST = State or Province Name (full name)
3 L = Locality Name (eg, city) [Default City]
4 O = Organization Name (eg, company) [Default Company Ltd]
5 OU = Organizational Unit Name (eg, section)
6 CN = Common Name (eg, your name or your server's hostname)
7 emailAddress = Email Address
CA证书文件有:
[root@sre ssl]# ls -l
total 2
-rw-r--r-- 1 root root 1363 Apr 2 13:11 CA.crt
-rw-r--r-- 1 root root 1675 Apr 2 13:09 CA.key
自签发泛域名证书
- 操作步骤为:
生成域名私钥
生成证书签发请求文件
使用自签署的CA,生成域名公钥 - 生成域名私钥
[root@sre ssl]# openssl genrsa -out server.key 2048
- 免交互式生成证书签发请求文件
[root@sre ssl]# openssl genrsa -out server.key 2048 openssl req -new -sha256 -key server.key -out server.csr -config /etc/pki/tls/openssl.cnf -subj "/C=CN/ST=JS/L=NanJing/O=other/OU=other/CN=*.test.com/emailAddress=ca@test.com"
ps:
(1)、上面的Common Name 就是在这步填写 *.test.com ,表示的就是该证书支持泛域名,common name一定要在SubjectAlternativeName中包含
(2)、进行CA签名获取证书时,需要注意国家、省、单位需要与CA证书相同,否则会报异常
- 使用自签署的CA,生成域名公钥【这里证书有效时间为100年】
[root@sre ssl]# openssl genrsa -out server.key 2048 openssl ca -in server.csr -md sha256 -days 36500 -out server.crt -cert CA.crt -keyfile CA.key -extensions v3_req -config /etc/pki/tls/openssl.cnf
可能出现的问题1:
1 Using configuration from /etc/pki/tls/openssl.cnf
2 /etc/pki/CA/index.txt: No such file or directory
3 unable to open '/etc/pki/CA/index.txt'
4 140652962035600:error:02001002:system library:fopen:No such file or directory:bss_file.c:402:fopen('/etc/pki/CA/index.txt','r')
5 140652962035600:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:404:
处理方法:创建该文件即可
[root@sre ssl]# openssl genrsa -out server.key 2048 touch /etc/pki/CA/index.txt
可能出现的问题2:
然后我们继续使用 【自签署的CA,签署server.crt】;结果又出现新问题
1 Using configuration from /etc/pki/tls/openssl.cnf
2 /etc/pki/CA/serial: No such file or directory
3 error while loading serial number
4 140087163742096:error:02001002:system library:fopen:No such file or directory:bss_file.c:402:fopen('/etc/pki/CA/serial','r')
5 140087163742096:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:404:
处理方法:使用如下命令即可,表示:用来跟踪最后一次颁发证书的序列号。
[root@sre ssl]# openssl genrsa -out server.key 2048 echo "01" > /etc/pki/CA/serial
之后我们再次执行 【自签署的CA,签署server.crt 】 就正常了。详情如下:
[root@sre ssl]# openssl genrsa -out server.key 2048 openssl ca -in server.csr -md sha256 -days 36500 -out server.crt -cert CA.crt -keyfile CA.key -extensions v3_req -config /etc/pki/tls/openssl.cnf
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Apr 2 05:16:52 2022 GMT
Not After : Mar 9 05:16:52 2122 GMT
Subject:
countryName = CN
stateOrProvinceName = JS
organizationName = other
organizationalUnitName = other
commonName = *.test.com
emailAddress = ca@test.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment
X509v3 Subject Alternative Name:
DNS:test.com, DNS:*.test.com
Certificate is to be certified until Mar 9 05:16:52 2122 GMT (36500 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n] y [输入Y]
Write out database with 1 new entries
Data Base Updated
说明:此时我们再看,/etc/pki/CA/index.txt
和 /etc/pki/CA/serial
文件信息。如下:
[root@sre ssl]# cat /etc/pki/CA/index.txt
V 21220309051652Z 01 unknown /C=CN/ST=JS/O=other/OU=other/CN=*.test.com/emailAddress=ca@test.com
[root@sre ssl]# cat /etc/pki/CA/serial
02
由上可知:域名签署信息已经保存到index.txt文件;并且证书序列serial文件已经更新【从01变为了02】。
PS:
- 同一个域名不能签署多次;由于签署了
*.test.com
,且已经被记录,因此不能再次被签署。除非删除该记录。 - 注意index.txt文件和serial文件的关系。serial文件内容为index.txt文件内容行数加1。
- 查看证书信息
[root@sre ssl]# openssl x509 -in server.crt -text
- 验证签发证书是否有效
[root@sre ssl]# openssl verify -CAfile CA.crt server.crt
server.crt: OK
- 此时的文件有:
[root@sre ssl]# ls -l
total 5
-rw-r--r-- 1 root root 1387 Oct 2 10:25 CA.crt
-rw-r--r-- 1 root root 1679 Oct 2 10:04 CA.key
-rw-r--r-- 1 root root 4364 Oct 2 11:42 server.crt
-rw-r--r-- 1 root root 1151 Oct 2 10:48 server.csr
-rw-r--r-- 1 root root 1679 Oct 2 10:44 server.key
客户端导入ca.crt 公钥
客户端导入ca.crt 公钥, 这样本地浏览器就不会显示这个是个不受信任的证书
参考: https://jingyan.baidu.com/article/ca41422fda393f5faf99ed0d.html
本文参考:
- https://cloud.tencent.com/developer/article/1731631
- https://jingyan.baidu.com/article/ca41422fda393f5faf99ed0d.html
文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/6866