Skip to content

生成根证书和自签名证书


生成根证书

  1. 生成根证书的密钥
sh
openssl genrsa -out rootCA.key 2048
  1. 生成根证书,支持中文属性,-days 36500 表示证书有效期为 100 年
sh
openssl req -x509 -new -utf8 -nameopt multiline,utf8 -key rootCA.key -sha256 -days 36500 -out rootCA.crt

生成一个用根证书签名的多域名证书,以域名 example.com 为例

  1. 写好配置文件, 命名为 example.com.cnf
cnf
[req]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = v3_req
distinguished_name = dn

[ dn ]
C = CN
O = 广东某某计算机有限公司
OU = 软件研发部
L = 广州
CN = example.com

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = example.com
DNS.2 = *.example.com

可以在 alt_names 中添加多个域名。

  1. 生成证书的密钥
sh
openssl genrsa -out example.com.key 2048

你也可以使用椭圆曲线加密方法(和上面的二选一):

sh
openssl ecparam -genkey -name secp384r1 -out example.com.key
  1. 生成csr证书请求
sh
openssl req -new -utf8 -nameopt multiline,utf8 -key example.com.key -out example.com.csr -config <(cat example.com.cnf)
  1. 用根证书签名
sh
# 只输出单证书
openssl x509 -req -in example.com.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out example.com.crt -extfile example.com.cnf -extensions v3_req -days 365

# 合并证书链
cat example.com.crt rootCA.crt > example.com-full.crt
  1. 导出证书,带密钥(可选)
sh
openssl pkcs12 -export -out example.com.pfx -inkey example.com.key -in example.com.crt

Last updated: