OpenSSL 常见操作手册
生成证书
生成私钥
1
| openssl genrsa -out private.key 2048
|
genrsa:生成 RSA 私钥。
-out private.key:指定输出文件名为 private.key。
2048:指定密钥长度为 2048 位(推荐最低长度,越长安全性越高但生成慢)。
生成证书签名请求(CSR)
1
| openssl req -new -key private.key -out request.csr
|
req:生成证书请求(CSR)或自签证书。
-new:生成新的证书请求。
-key private.key:使用已有私钥生成 CSR。
-out request.csr:指定生成的 CSR 文件名。
执行时会提示填写:
Country Name (C):国家代码,如 CN
State or Province Name (ST):省份
Locality Name (L):城市
Organization Name (O):公司或组织名称
Organizational Unit Name (OU):部门
Common Name (CN):域名或个人名字
Email Address:邮箱
生成自签证书(有效期 365 天)
1
| openssl req -x509 -days 365 -key private.key -in request.csr -out cert.crt
|
-x509:生成自签证书,而不是 CSR。
-days 365:证书有效期 365 天。
-key private.key:使用私钥签发证书。
-in request.csr:指定 CSR 文件。
-out cert.crt:输出证书文件名。
一步生成自签证书(省略 CSR)
1
| openssl req -x509 -newkey rsa:2048 -keyout private.key -out cert.crt -days 365 -nodes
|
-newkey rsa:2048:同时生成新的 2048 位 RSA 私钥。
-keyout private.key:输出私钥文件。
-out cert.crt:输出证书文件。
-days 365:证书有效期 365 天。
-nodes:不加密私钥(不需要输入密码),便于自动化使用。
安装证书
Linux 系统(以 Debian/Ubuntu 为例)
拷贝证书到系统目录:
1
| sudo cp cert.crt /usr/local/share/ca-certificates/mycert.crt
|
更新系统信任库:
1
| sudo update-ca-certificates
|
macOS
1
| sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert.crt
|
security add-trusted-cert:将证书添加到 macOS 系统钥匙串。
-d:证书适用于系统域。
-r trustRoot:设置为受信任的根证书。
-k /Library/Keychains/System.keychain:指定系统钥匙串路径。
Windows
双击 .crt 文件 → 选择 安装证书 → 选中 受信任的根证书颁发机构。
卸载证书
Linux(Debian/Ubuntu)
删除证书文件:
1
| sudo rm /usr/local/share/ca-certificates/mycert.crt
|
更新信任库:
1
| sudo update-ca-certificates --fresh
|
macOS
1
| sudo security delete-certificate -c "证书名称" /Library/Keychains/System.keychain
|
Windows
控制面板 → 管理用户证书 → 受信任的根证书颁发机构 → 删除对应证书。
格式转换
PEM ↔ DER
1 2 3 4 5
| # PEM 转 DER openssl x509 -outform der -in cert.pem -out cert.der
# DER 转 PEM openssl x509 -inform der -in cert.der -out cert.pem
|
x509:处理 X.509 证书。
-outform der:指定输出格式为 DER(二进制)。
-in cert.pem:输入 PEM 格式证书。
-out cert.der:输出 DER 格式证书。
-inform der:输入格式为 DER。
-out cert.pem:输出 PEM 格式证书。
PEM ↔ PKCS12 (.p12/.pfx)
1 2 3 4 5
| # PEM (cert + key) 转 PKCS12 openssl pkcs12 -export -in cert.pem -inkey private.key -out cert.p12 -name mycert
# PKCS12 转 PEM openssl pkcs12 -in cert.p12 -out cert.pem -nodes
|
pkcs12:操作 PKCS#12 文件。
-export:导出为 PFX/P12 文件。
-in cert.pem:输入证书文件。
-inkey private.key:输入私钥文件。
-out cert.p12:输出 P12 文件。
-name mycert:给证书设置别名。
-in cert.p12:输入 P12 文件。
-out cert.pem:输出 PEM 文件。
-nodes:不加密私钥。
CRT/KEY → PKCS12
1
| openssl pkcs12 -export -in cert.crt -inkey private.key -out keystore.p12 -name tomcat
|
PKCS12 → JKS (给 Java/Spring Boot 用)
1 2 3 4
| keytool -importkeystore \ -deststorepass changeit -destkeypass changeit -destkeystore keystore.jks \ -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass 你的密码 \ -alias tomcat
|
| 参数 |
作用 |
示例值说明 |
-importkeystore |
告诉 keytool 这是导入密钥库操作 |
无值 |
-deststorepass |
目标 JKS 密钥库的密码 |
changeit → 你可以改成自己的密码 |
-destkeypass |
目标 JKS 中条目的私钥密码 |
changeit → 通常可以和 storepass 相同或不同 |
-destkeystore |
输出的 JKS 文件名 |
keystore.jks |
-srckeystore |
源密钥库文件名 |
keystore.p12 |
-srcstoretype |
源密钥库类型 |
PKCS12 |
-srcstorepass |
源密钥库密码 |
你的密码 → 替换成 P12 文件实际密码 |
-alias |
指定条目别名 |
tomcat → 你想给条目取的名字 |
查看证书信息
1 2 3 4 5
| # 查看证书内容 openssl x509 -in cert.crt -text -noout
# 查看私钥信息 openssl rsa -in private.key -check
|
-text:以可读格式显示证书信息。
-noout:不输出原始证书内容。
rsa:操作 RSA 私钥。
-check:检查私钥完整性。