CA 证书生成h2
创建 CA 配置文件h3
首先,创建用于生成根 CA 的配置文件:
cat > ca.conf << EOF[ req ]default_bits = 2048default_md = sha256prompt = noencrypt_key = nodistinguished_name = dn
[ dn ]C = CNST = BeijingL = BeijingO = DevOU = Dev Root CACN = Dev Root CAemailAddress = admin@example.comEOF生成 CA 证书h3
执行以下命令生成 CA 私钥和根证书:
# 生成 CA 私钥(使用 prime256v1 椭圆曲线)openssl ecparam -out ca.key -name prime256v1 -genkey# 生成证书签名请求(CSR)openssl req -new -sha256 -key ca.key -out ca.csr -config ca.conf# 使用私钥自签名生成根证书,有效期 10 年openssl x509 -req -sha256 -days 3650 -in ca.csr -signkey ca.key -out ca.crt签发 SAN 证书h2
创建证书配置文件h3
创建包含 Subject Alternative Name (SAN) 扩展的证书配置文件:
cat > domain.conf << EOF[ req ]default_bits = 2048default_md = sha256prompt = noencrypt_key = nodistinguished_name = dnreq_extensions = v3_req
[ dn ]C = CNST = BeijingL = BeijingO = OrganizationOU = Dev TeamCN = example.comemailAddress = admin@example.com
[ v3_req ]basicConstraints = CA:FALSEkeyUsage = nonRepudiation, digitalSignature, keyEnciphermentsubjectAltName = @alt_names
[alt_names]DNS.1 = example.comDNS.2 = *.example.comEOF签发证书h3
使用前面创建的 CA 签发 SAN 证书:
# 生成域名私钥和 CSRopenssl genrsa -out example.com.key 2048openssl req -new -nodes -key example.com.key -out example.com.csr -config domain.conf
# 使用 CA 签发证书(有效期 10 年)openssl x509 -req -in example.com.csr \ -CA ca.crt -CAkey ca.key -CAcreateserial \ -out example.com.crt -days 3650 -sha256 \ -extensions v3_req -extfile domain.conf
# 验证生成的证书信息openssl x509 -in example.com.crt -text -noout补全完整的证书链h3
将服务器证书和 CA 证书合并,形成完整的证书链文件:
cat example.com.crt ca.crt > example.com.fullchain.crt文件说明h2
- ca.conf: CA 的配置文件,定义了生成根证书时使用的参数。
- ca.key: CA 的私钥文件,需严格保密。
- ca.crt: 自签名的根证书,用于签发后续的服务器证书。
- domain.conf: 签发服务器证书时的配置文件,定义了 SAN 等扩展信息。
- example.com.key: 服务器证书对应的私钥。
- example.com.csr: 证书签名请求文件,包含了申请证书的公钥和主体信息。
- example.com.crt: 由 CA 签发的服务器证书。
- example.com.fullchain.crt: 完整的证书链文件,包含服务器证书和 CA 证书。