以下为自签名生成v3 SAN证书过程-已测试

647 阅读1分钟

简介

openssl 默认生成v1的自签名证书,以下为生成v3 SAN自签名证书。
openssl Google等浏览器已升级,只信任SAN自签名证书。如果不是SAN自签名证书需要使用低版本的浏览才能被信任如IE

/**
* 以下为自签名生成v3 SAN证书过程
*每次使用openssl.cnf时,需改动其CN 和DNS.1 所在行
*/

#################根证书##################################
# 生成CA私钥,密码可以输入a1b2c3d4
$ openssl genrsa -des3 -out ca.key 2048
 
# 使用私钥来签名证书
$ openssl req -new -key ca.key -out ca.csr
 
# 使用私钥+证书来生成公钥
$ openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

###############中间证书################################

# 生成中间私钥,密码输入a1b2c3d4
$ openssl genpkey -algorithm RSA -out inter.key
 
# 使用私钥来签名证书
$ openssl req -new -nodes -key inter.key -out inter.csr -config openssl.cnf -extensions v3_req
 
# 生成SAN证书
$ openssl x509 -req -in inter.csr -out inter.pem -CA ca.crt -CAkey ca.key -CAcreateserial -extfile openssl.cnf -extensions v3_req

##################服务器证书############################

# 生成服务器私钥,密码输入a1b2c3d4
$ openssl genpkey -algorithm RSA -out server.key
 
# 使用私钥来签名证书
$ openssl req -new -nodes -key server.key -out server.csr -config openssl.cnf -extensions v3_req
 
# 生成SAN证书
$ openssl x509 -req -in server.csr -out server.pem -CA inter.crt -CAkey inter.key -CAcreateserial -extfile openssl.cnf -extensions v3_req

###########################其他指令######################
#PEMCRT
openssl x509 -outform der -in client.pem -out client.crt
#查看证书
openssl x509 -in server.crt -text -noout
# 查看证书链
openssl s_client -connect www.domain.com:443 -showcerts > file.txt
# 获取证书公钥
openssl x509 -in client.crt -pubkey -noout > client.key
#通过公钥查看证书链
###########openssl.cnf##################################

[ CA_default ]
copy_extensions = copy
 
[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
 
[req_distinguished_name]
# 国家
C = CH
# 省份
ST = Shenzhen
# 城市
L = Shenzhen
# 组织
O = Arvin
# 部门
OU = Arvin
# 域名
CN = test.example.com

#basicConstraints = CA:FALSE 默认生成终端证书
[v3_req]
basicConstraints=critical,CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
 
[alt_names]
# 解析域名,支持通配符域名
DNS.1 = test.example.com
#IP.1=127.0.0.1