1、openssl核心功能
1. 证书管理:生成 CSR、签发证书、证书转换
2. 密钥操作:生成 RSA/ECC 密钥、密钥转换
3. 加密/解密:支持 AES、DES、3DES 等算法
4. 摘要计算:SHA-256、MD5 等哈希值生成
5. SSL/TLS 测试:模拟客户端/服务器端连接
2、常用命令及示例
1、生成密钥
RSA私钥 (2048位)
openssl genrsa -out private.key 2048
ECC 私钥(secp384r1曲线)
openssl ecparam -genkey -name secp384r1 -out ecc.key
2、生成证书签名请求 (CSR)
openssl req -new -key private.key -out request.csr \
-subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=example.com"
-subj
参数指定证书主题(国家/省/市/组织/域名)
3、自签名证书
openssl req -x509 -newkey rsa:2048 -keyout selfsigned.key -out selfsigned.crt -days 365 -nodes
-nodes
:不加密私钥-days 365
:有效期 1 年
4、查看证书信息
openssl x509 -in certificate.crt -text -noout # 查看证书内容
openssl req -in request.csr -text -noout # 查看 CSR 内容
5、格式转换
PEM转PKCS12
openssl pkcs12 -export -in cert.crt -inkey private.key -out bundle.p12
DER转PEM
openssl x509 -inform der -in cert.der -out cert.pem
6、验证证书链
openssl verify -CAfile ca.crt server.crt
3、实际应用场景
场景1:为nginx配置https证书
1、生成密钥和CSR
openssl req -newkey rsa:2048 -nodes -keyout nginx.key -out nginx.csr
2、从CA获取签名证书
openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt
3、nginx配置
server {
listen 443 ssl;
ssl_certificate /path/to/nginx.crt;
ssl_certificate_key /path/to/nginx.key;
}
场景2:为opensearch配置TLS证书
opensearch所有节点要求使用相同CA签名的证书
1、生成CA证书
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 365 -key ca.key -out ca.crt \
-subj "/C=US/O=MyCA/CN=opensearch-ca"
2、生成节点证书
# 生成私钥
openssl genrsa -out node1.key 2048
# 创建 CSR (包含 SAN 扩展)
openssl req -new -key node1.key -out node1.csr \
-subj "/C=US/ST=CA/O=MyCluster/CN=node1" \
-config <(printf "[req]\ndistinguished_name=req\n[ SAN ]\nsubjectAltName=DNS:node1,DNS:localhost,IP:192.168.1.100")
# 用 CA 签名证书
openssl x509 -req -days 365 -in node1.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out node1.crt -extensions SAN -extfile <(printf "[SAN]\nsubjectAltName=DNS:node1,DNS:localhost,IP:192.168.1.100")
3、配置opensearch
plugins.security.ssl.transport.pemcert_filepath: node1.crt
plugins.security.ssl.transport.pemkey_filepath: node1.key
plugins.security.ssl.transport.pemtrustedcas_filepath: ca.crt
4、安全最佳实践
1、密钥保护
openssl genrsa -aes256 -out encrypted.key 2048 # 加密私钥
2、证书扩展
使用
subjectAltName
包含域名/IP设置
keyUsage
和extendedKeyUsage
3、证书吊销
openssl ca -revoke bad.crt -keyfile ca.key -cert ca.crt
openssl ca -gencrl -out ca.crl -keyfile ca.key -cert ca.crt
5、调试
测试SSL连接
openssl s_client -connect example.com:443 -showcerts
检查证书链
openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -text