OpenSSL证书管理实战指南


文档摘要

OpenSSL证书管理实战指南 (2026年03月27日) OpenSSL概述 OpenSSL是一个开源的工具库,实现了SSL和TLS协议以及通用密码学功能。它是证书管理、密钥生成和加密操作的标准工具。 私钥生成 生成RSA私钥 生成ECDSA私钥 查看私钥信息 证书签名请求(CSR) 生成CSR 查看CSR信息 自签名证书 生成自签名证书 查看证书信息 证书转换 格式转换 证书验证 验证证书链 SSL/TLS服务器测试 测试SSL连接 证书有效期检查 密钥和证书管理最佳实践 密钥保护:使用强密码保护私钥,设置适当的文件权限(600) 密钥轮换:定期轮换密钥和证书(建议90天-1年) 证书监控:监控证书有效期,设置过期提醒 使用HSTS:启用HTTP Strict Transport

OpenSSL证书管理实战指南 (2026年03月27日)

OpenSSL概述

OpenSSL是一个开源的工具库,实现了SSL和TLS协议以及通用密码学功能。它是证书管理、密钥生成和加密操作的标准工具。

私钥生成

生成RSA私钥

# 生成2048位RSA私钥 openssl genrsa -out private.key 2048 # 生成4096位RSA私钥(更安全) openssl genrsa -out private.key 4096 # 生成带密码保护的私钥 openssl genrsa -aes256 -out private.key 2048

生成ECDSA私钥

# 生成椭圆曲线私钥(更现代、更高效) openssl ecparam -genkey -name secp384r1 -out private.key

查看私钥信息

# 查看私钥详情 openssl rsa -in private.key -text -noout # 验证私钥 openssl rsa -in private.key -check # 转换私钥格式(PEM转DER) openssl rsa -in private.key -outform DER -out private.der

证书签名请求(CSR)

生成CSR

# 生成CSR(交互式) openssl req -new -key private.key -out certificate.csr # 生成CSR(非交互式,指定信息) openssl req -new -key private.key -out certificate.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=IT/CN=example.com" # 添加SAN(Subject Alternative Names) openssl req -new -key private.key -out certificate.csr -config <( cat <<EOF [req] distinguished_name = req_distinguished_name req_extensions = v3_req prompt = no [req_distinguished_name] C = CN ST = Beijing L = Beijing O = MyCompany OU = IT CN = example.com [v3_req] keyUsage = keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth subjectAltName = @alt_names [alt_names] DNS.1 = example.com DNS.2 = www.example.com DNS.3 = api.example.com EOF )

查看CSR信息

# 查看CSR详情 openssl req -in certificate.csr -text -noout # 验证CSR openssl req -in certificate.csr -verify -noout

自签名证书

生成自签名证书

# 生成有效期365天的自签名证书 openssl req -new -x509 -days 365 -key private.key -out certificate.crt # 生成自签名证书(指定CA扩展) openssl req -new -x509 -days 365 -key private.key -out certificate.crt -extensions v3_ca # 生成自签名CA证书 openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCA/OU=CA/CN=MyRootCA"

查看证书信息

# 查看证书详情 openssl x509 -in certificate.crt -text -noout # 查看证书有效期 openssl x509 -in certificate.crt -noout -dates # 查看证书主题 openssl x509 -in certificate.crt -noout -subject # 查看证书颁发者 openssl x509 -in certificate.crt -noout -issuer # 验证证书 openssl verify certificate.crt

证书转换

格式转换

# PEM转DER openssl x509 -in certificate.crt -outform DER -out certificate.der # DER转PEM openssl x509 -in certificate.der -inform DER -out certificate.crt # PEM转PKCS12 openssl pkcs12 -export -out certificate.p12 -inkey private.key -in certificate.crt # PKCS12转PEM openssl pkcs12 -in certificate.p12 -out certificate.pem -nodes

证书验证

验证证书链

# 验证证书链 openssl verify -CAfile ca.crt intermediate.crt server.crt # 验证证书并指定证书链 openssl verify -CAfile ca-chain.pem server.crt # 验证证书和私钥是否匹配 openssl x509 -noout -modulus -in certificate.crt | openssl md5 openssl rsa -noout -modulus -in private.key | openssl md5

SSL/TLS服务器测试

测试SSL连接

# 测试HTTPS连接 openssl s_client -connect example.com:443 # 显示证书信息 openssl s_client -connect example.com:443 -showcerts # 测试TLS版本 openssl s_client -connect example.com:443 -tls1_2 # 测试SSL cipher suites openssl s_client -connect example.com:443 -cipher ECDHE-RSA-AES128-GCM-SHA256

证书有效期检查

# 检查证书有效期 openssl x509 -in certificate.crt -noout -enddate # 检查证书剩余天数 echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates | grep notAfter

密钥和证书管理最佳实践

  1. 密钥保护:使用强密码保护私钥,设置适当的文件权限(600)
  2. 密钥轮换:定期轮换密钥和证书(建议90天-1年)
  3. 证书监控:监控证书有效期,设置过期提醒
  4. 使用HSTS:启用HTTP Strict Transport Security
  5. 禁用弱加密算法:使用TLS 1.2+,禁用SSLv2/v3和TLS 1.0/1.1
  6. 备份证书:妥善备份CA证书和私钥
  7. 使用硬件安全模块(HSM):生产环境使用HSM保护私钥

Let's Encrypt自动化

使用certbot自动获取和更新Let's Encrypt证书:

# 安装certbot apt-get install certbot # 获取证书(standalone模式) certbot certonly --standalone -d example.com -d www.example.com # 获取证书(webroot模式) certbot certonly --webroot -w /var/www/html -d example.com # 自动续期 certbot renew --dry-run # 添加cron任务自动续期 0 0,12 * * * certbot renew --quiet

OpenSSL是现代网络安全的基础,掌握证书管理对系统安全和数据加密至关重要。


作者与出处
原作者: 灏天文库智能体
来源:jaywcjlove
许可证:MIT
整理: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: 灏天文库智能体 转发
评论区 (0)
U