OpenSSL生成自签名证书

概述

OpenSSL 是一个开源的 SSL/TLS 工具包,用于生成和管理数字证书。本笔记涵盖自签名证书、多级证书链、PKCS#12 格式的完整操作流程。


一、基础概念

1.1 证书机构类型

类型 说明
CA Certificate Authority,证书授权机构,负责发放和管理数字证书
RootCA 根证书,权威机构持有的证书,安装根证书意味着信任该 CA
SubCA 中间证书机构,由根证书签发,可继续签发下级证书

1.2 CSR

CSR(Certificate Signing Request)是证书签名请求文件,生成私钥时同时生成。

1.3 常用文件格式

格式 说明
.crt / .cer 证书文件
.key 私钥文件
.csr 证书签名请求文件
.pem Base64 编码证书,可包含证书或密钥
.der 二进制证书文件,常见于 Windows
.p12 / .pfx PKCS#12 格式,包含私钥和证书链

[!NOTE]
CA 机构的证书可以给其他域名证书签名,但域名证书不可以。


二、证书链结构

典型三级结构:根证书 → 中间证书 → 服务器证书

1
2
3
4
5
6
7
8
服务器证书(最底端)
├── 域名、公钥、签名值

中间证书
├── 可由多张组合

根证书(最顶端)
└── 浏览器内置信任

验证过程:从服务器证书向上逐级验证签名直到根证书。


三、生成根证书

3.1 生成根私钥

1
2
3
4
5
# 生成带密码的私钥(需要输入两次密码)
openssl genrsa -des3 -out root-ca.priv.key 4096

# 去除私钥密码(需输入上一步密码)
openssl rsa -in root-ca.priv.key -out root-ca.key

3.2 生成自签名根证书

1
2
3
4
# -x509:使用 X.509 证书结构
# -days:证书有效期(按天计算)
openssl req -new -x509 -key root-ca.key -out root-ca.crt -days 3650 \
-subj "/C=CN/ST=Tianjin/L=Tianjin/O=Example/OU=DEV/CN=Example Root"

四、生成中间证书

[!TIP]
非复杂场景可跳过此步骤,使用根证书直接签发用户证书。

4.1 生成中间证书私钥

1
2
3
4
5
6
# 方式1:参考根证书私钥
openssl genrsa -des3 -out mid-ca.priv.key 4096
openssl rsa -in mid-ca.priv.key -out mid-ca.key

# 方式2:直接生成
openssl genpkey -algorithm RSA -out mid-ca.key -pkeyopt rsa_keygen_bits:4096

4.2 生成中间证书请求

1
2
openssl req -new -key mid-ca.key -out mid-ca.csr \
-subj "/C=CN/ST=Tianjin/L=Tianjin/O=Example/OU=DEV/CN=Example Mid"

4.3 使用根证书签发中间证书

1
2
3
4
5
openssl x509 -req \
-extfile <(printf "subjectKeyIdentifier=hash\nauthorityKeyIdentifier=keyid:always,issuer:always") \
-days 3650 -in mid-ca.csr \
-CA root-ca.crt -CAkey root-ca.key \
-CAcreateserial -out mid-ca.crt

4.4 验证中间证书

1
openssl verify -CAfile root-ca.crt mid-ca.crt

五、生成终端证书(服务器证书)

假设服务器域名为 example.io

5.1 生成私钥

1
2
3
4
5
6
# 方式1
openssl genrsa -des3 -out example.io.priv.key 4096
openssl rsa -in example.io.priv.key -out example.io.key

# 方式2
openssl genpkey -algorithm RSA -out example.io.key -pkeyopt rsa_keygen_bits:4096

5.2 生成证书签名请求

1
2
openssl req -new -key example.io.key -out example.io.csr \
-subj "/CN=example.io"

5.3 生成证书

1
2
3
4
5
6
7
8
9
10
11
# 使用根证书签发
openssl x509 -req -days 3650 \
-extfile v3.ext \
-CA root-ca.crt -CAkey root-ca.key -CAcreateserial \
-in example.io.csr -out example.io.crt

# 使用中间证书签发
openssl x509 -req -days 3650 \
-extfile v3.ext \
-CA mid-ca.crt -CAkey mid-ca.key -CAcreateserial \
-in example.io.csr -out example.io.crt

[!CAUTION]
v3.ext 中 basicConstraints=CA:FALSE 为必选项,否则生成证书无法使用。

5.4 导出 PFX 格式

1
openssl pkcs12 -export -out example.io.pfx -inkey example.io.key -in example.io.crt

5.5 验证终端证书

1
openssl verify -CAfile root-ca.crt example.io.crt

六、查看证书信息

1
2
3
4
5
6
7
8
9
10
11
# 查看私钥内容
openssl rsa -noout -text -in cakey.key

# 查看证书内容
openssl x509 -noout -text -in cacert.crt

# PEM 转为 DER
openssl x509 -in cacert.crt -outform der -out cacert.der

# DER 转为 PEM
openssl x509 -in cert.crt -inform der -outform pem -out cacert.pem

七、证书有效性验证

启动测试服务器(需先导入根证书到浏览器信任库):

1
openssl s_server -cert example.io.crt -key example.io.key -debug -HTTP -accept 443

然后访问 https://服务器IP 查看证书是否有效。


八、吊销证书

1
2
3
4
5
6
7
8
# 吊销证书
openssl ca -revoke cert.pem

# 生成吊销列表
openssl ca -gencrl -out testca.crl

# 查看吊销列表
openssl crl -in testca.crl -text -noout

九、附件

9.1 CSR 参数说明

参数 说明 示例
/C 国家代码 CN
/ST 州或省 Tianjin
/L 城市 Tianjin
/O 组织名称 Example
/OU 部门名称 DEV
/CN 通用名称(域名) example.com

9.2 X.509 扩展配置 v3.ext

1
2
3
4
5
6
7
8
9
10
11
authorityKeyIdentifier=keyid,issuer
subjectKeyIdentifier=hash
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment

subjectAltName = @alt_names

[alt_names]
DNS.1=example.io
DNS.2=*.example.io
IP.3=192.168.0.2

扩展用途说明

  • serverAuth:保证远程计算机的身份
  • clientAuth:向远程计算机证明身份
  • codeSigning:确保软件来自发布者
  • emailProtection:保护电子邮件

十、PKCS#12 操作

10.1 生成 PKCS#12 证书

1
2
3
4
5
# 包含证书和私钥
openssl pkcs12 -export -inkey server.key -in server.crt -out server.p12

# 包含证书、私钥和 CA 证书链
openssl pkcs12 -export -inkey server.key -in server.crt -chain -CAfile ca.crt -out server.p12

10.2 从 PKCS#12 提取内容

1
2
3
4
5
6
7
8
9
10
11
# 提取私钥
openssl pkcs12 -in server.p12 -nocerts -out server.key -nodes

# 提取证书
openssl pkcs12 -in server.p12 -nokeys -clcerts -out server.crt

# 提取 CA 证书
openssl pkcs12 -in server.p12 -nokeys -cacerts -out ca.crt

# 提取所有内容
openssl pkcs12 -in server.p12 -nodes -out all-in-one.pem

10.3 验证 PKCS#12 文件

1
openssl pkcs12 -info -in server.p12

十一、一键命令速查

自签名证书

1
2
3
openssl genrsa -out server.key 2048
openssl req -subj "/C=CN/ST=Tianjin/O=Mocha/CN=example.io" -new -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

私有 CA 签名

1
2
3
4
5
openssl genrsa -out ca.key 2048
openssl req -subj "/C=CN/ST=Tianjin/O=Mocha/CN=Server CA" -new -x509 -days 3650 -key ca.key -out ca.crt
openssl genrsa -out server.key 2048
openssl req -subj "/C=CN/ST=Tianjin/O=Mocha/CN=example.io" -new -key server.key -out server.csr
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt