关于证书生成,各软件推荐用的命令行十分庞杂,遂汇总单篇。
1 、联合证书使用
1.1 、证书配置模板部分
根 CA 是整个信任链条的起点,它是自签名的。
# 创建证书生成使用的临时文件夹
mkdir -p pki/{root,intermediate,server}
cd pki
注意,之后所有的 openssl genrsa 均默认不带密码,如需携带密码保护私钥,使用 -aes256 参数补充在 genrsa 后面
1.2 、 CA 部分
# 创建根 CA 配置文件 `root/root.cnf`:
cat << 'EOF' > root/root.cnf
[ req ]
default_bits = 4096
distinguished_name = req_dn
string_mask = utf8only
prompt = no
[ req_dn ]
C = CN
O = My Rigorous Enterprise
CN = My Root CA
[ v3_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
EOF
# 生成根 CA 的 4096 位高强度私钥:
openssl genrsa -out root/root.key 4096
# 签发根 CA 证书(自签名,有效期 10 年):
openssl req -x509 -new -nodes -key root/root.key -sha256 -days 3650 -config root/root.cnf -extensions v3_ca -out root/root.crt
1.3 、中间证书部分
中级 CA 作为核心控权层,参数 basicConstraints 中的 pathlen:0 极其重要,它限制了该中级 CA
只能签发最终的终端证书,绝不允许它再往下签发下一级中级 CA 。
创建中级 CA 配置文件 `intermediate/intermediate.cnf`:
cat << 'EOF' > intermediate/intermediate.cnf
[ req ]
default_bits = 2048
distinguished_name = req_dn
string_mask = utf8only
prompt = no
[ req_dn ]
C = CN
O = My Rigorous Enterprise
CN = My Intermediate CA
[ v3_intermediate_ca ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
# 生产级核心:指定吊销列表 (CRL) 与在线验证 (OCSP) 地址
# crlDistributionPoints = URI:http://crl.secure.local/root.crl
# authorityInfoAccess = OCSP;URI:http://ocsp.secure.local,CA Issuers;URI:http://crt.secure.local/root.crt
EOF
生成中级 CA 的 2048 位私钥:
openssl genrsa -out intermediate/intermediate.key 2048
生成中级 CA 的证书签名请求(CSR):
openssl req -new -config intermediate/intermediate.cnf -key intermediate/intermediate.key -out intermediate/intermediate.csr
使用【根 CA】的证书和私钥为中级 CA 签署证书(有效期 5 年):
openssl x509 -req -in intermediate/intermediate.csr -CA root/root.crt -CAkey root/root.key -CAcreateserial -days 1825 -sha256 -extfile intermediate/intermediate.cnf -extensions v3_intermediate_ca -out intermediate/intermediate.crt
1.4 、客户证书部分
具体的业务应用层证书。明确声明 CA:FALSE,并注入 EKU(服务端/客户端认证)和 SAN 域名/IP 扩展。
创建服务器证书配置文件 `server/server.cnf`:
(根据需要修改 [ alt_names ] 中的域名和 IP):
cat << 'EOF' > server/server.cnf
[ req ]
default_bits = 2048
distinguished_name = req_dn
string_mask = utf8only
prompt = no
[ req_dn ]
C = CN
O = My Rigorous Enterprise
CN = app.secure.local
[ v3_leaf ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
crlDistributionPoints = URI:http://crl.secure.local/intermediate.crl
authorityInfoAccess = OCSP;URI:http://ocsp.secure.local,CA Issuers;URI:http://crt.secure.local/intermediate.crt
[ alt_names ]
DNS.1 = secure.local
DNS.2 = *.secure.local
IP.1 = 192.168.1.100
EOF
生成服务器终端的 2048 位私钥:
openssl genrsa -out server/server.key 2048
生成服务器终端的证书签名请求(CSR):
openssl req -new -config server/server.cnf -key server/server.key -out server/server.csr
使用【二级中级 CA】的证书和私钥为服务器签署证书(符合现代规范,有效期 1 年):
openssl x509 -req -in server/server.csr -CA intermediate/intermediate.crt -CAkey intermediate/intermediate.key -CAcreateserial -days 365 -sha256 -extfile server/server.cnf -extensions v3_leaf -out server/server.crt
最后别忘记检查链条完整性
openssl verify -CAfile root/root.crt -untrusted intermediate/intermediate.crt server/server.crt
如果遇到 Nginx 可能还需要合并证书使用
合并证书链
cat server/server.crt intermediate/intermediate.crt > server/fullchain.pem
2 、单证书使用
自签名,CA 就是他自己,参考链接 [ 链接 ]
创建配置文件 server.cnf
[ req ]
default_bits = 2048
distinguished_name = req_dn
x509_extensions = v3_self_signed
prompt = no
string_mask = utf8only
# 对应上一轮命令中的 -subj 身份信息
[ req_dn ]
C = CN
ST = ZheJiang
L = HangZhou
O = Temporary Test
CN = test.local
# 对应上一轮命令中的 -addext 扩展信息
[ v3_self_signed ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, keyEncipherment, keyCertSign
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
# 对应你的 SAN 域名和 IP 列表
[ alt_names ]
DNS.1 = test.local
DNS.2 = www.test.local
IP.1 = 192.168.1.100
# 生成证书对
openssl req -x509 -nodes -days 365 -sha256 \
-config server.cnf \
-newkey rsa:2048 \
-keyout server.key \
-out server.crt
在 Console 查看证书内容
openssl x509 -in server.crt -text -noout
或者使用单行命令直接创建证书对
openssl req -new -x509 -nodes -sha256 -days 365 \
-newkey rsa:2048 \
-keyout server.key \
-out server.crt \
-subj "/C=CN/ST=ZheJiang/L=HangZhou/O=Temporary Test/CN=test.local" \
-addext "subjectAltName=DNS:test.local,DNS:www.test.local,IP:192.168.1.100" \
-addext "extendedKeyUsage=serverAuth"