OpenSSL实用指南:密钥生成与证书管理

OpenSSL实用指南:密钥生成与证书管理

OpenSSL工具使用指南

1. 基础知识

1.1 OpenSSL简介

OpenSSL是一个开源的SSL/TLS工具包,提供:

  • 密钥和证书管理
  • 加密解密功能
  • SSL/TLS协议实现
  • 各种密码学算法

1.2 OpenSSL生态系统

1.2.1 OpenSSL、libssl和LibreSSL的关系
  1. OpenSSL项目组成

    • openssl命令行工具: 用于密钥管理、证书签发等操作的终端工具
    • libssl: SSL/TLS协议的具体实现库
    • libcrypto: 基础密码学算法库,提供各种加密、解密、签名等功能
  2. LibreSSL的由来

    • 2014年由OpenBSD团队从OpenSSL分支而来
    • 起因是对OpenSSL代码质量和安全性的担忧(如Heartbleed漏洞)
    • 主要改进:
      • 删除了大量过时和不安全的代码
      • 改进了内存管理
      • 增强了跨平台兼容性
      • 提供更严格的安全默认配置
  3. 三者的区别和联系

    • OpenSSL:

      • 最广泛使用的SSL/TLS实现
      • 包含完整的工具链和库
      • 持续更新和维护
      • 向后兼容性好
    • libssl:

      • OpenSSL项目的一部分
      • 专注于SSL/TLS协议实现
      • 依赖libcrypto提供的基础密码学功能
      • 被许多应用程序直接使用
    • LibreSSL:

      • OpenSSL的安全强化分支
      • API与OpenSSL基本兼容
      • 更注重代码质量和安全性
      • 主要用于OpenBSD等注重安全的系统
  4. 使用建议

    • 一般Linux系统:使用OpenSSL
    • 对安全性有特殊要求:考虑LibreSSL
    • 开发应用程序:
      • 使用libssl的高层API
      • 避免直接使用底层密码学函数
      • 注意版本兼容性
  5. 版本选择

# 查看系统使用的SSL库版本
ldd $(which curl) | grep ssl

# 查看OpenSSL版本
openssl version -a

# 查看系统SSL库
ls -l /usr/lib*/libssl.so*

1.3 版本信息

# 查看版本
openssl version
# OpenSSL 1.1.1f  31 Mar 2020

# 查看详细信息
openssl version -a

2. RSA密钥对管理

2.1 生成RSA私钥

# 生成2048位RSA私钥
openssl genrsa -out private.key 2048

# 生成带密码保护的私钥
openssl genrsa -aes256 -out private_encrypted.key 2048

# 移除私钥密码保护
openssl rsa -in private_encrypted.key -out private_decrypted.key

2.2 提取公钥

# 从私钥中提取公钥
openssl rsa -in private.key -pubout -out public.pem

# 查看公钥信息
openssl rsa -pubin -in public.pem -text -noout

2.3 密钥格式转换

# PEM转DER格式
openssl rsa -in private.key -outform DER -out private.der

# DER转PEM格式
openssl rsa -in private.der -inform DER -out private.pem

3. 证书操作

3.1 生成自签名证书

# 生成证书签名请求(CSR)
openssl req -new -key private.key -out server.csr \
    -subj "/C=CN/ST=Beijing/L=Beijing/O=Company/OU=IT/CN=example.com"

# 生成自签名证书
openssl x509 -req -days 365 -in server.csr \
    -signkey private.key -out server.crt

3.2 证书查看和验证

# 查看证书信息
openssl x509 -in server.crt -text -noout

# 验证证书
openssl verify -CAfile ca.crt server.crt

# 查看证书链
openssl crl2pkcs7 -nocrl -certfile chain.pem | \
    openssl pkcs7 -print_certs -text -noout

4. 加密解密操作

4.1 文件加密

# 使用公钥加密
openssl rsautl -encrypt -pubin \
    -inkey public.pem \
    -in plaintext.txt \
    -out encrypted.bin

# 使用私钥解密
openssl rsautl -decrypt \
    -inkey private.key \
    -in encrypted.bin \
    -out decrypted.txt

4.2 消息摘要

# 生成SHA256摘要
openssl dgst -sha256 file.txt

# 使用私钥签名
openssl dgst -sha256 -sign private.key \
    -out signature.bin file.txt

# 使用公钥验证
openssl dgst -sha256 -verify public.pem \
    -signature signature.bin file.txt

5. SSL/TLS配置

5.1 生成Diffie-Hellman参数

# 生成DH参数
openssl dhparam -out dhparam.pem 2048

5.2 测试SSL连接

# 测试SSL服务器
openssl s_client -connect example.com:443 -servername example.com

# 显示证书链
openssl s_client -connect example.com:443 \
    -showcerts -servername example.com

6. 最佳实践

6.1 密钥管理

  • 使用足够长的密钥长度(RSA至少2048位)
  • 私钥文件权限设置为600
  • 定期轮换密钥和证书
  • 使用密码保护重要私钥

6.2 安全建议

# 设置私钥文件权限
chmod 600 private.key

# 验证私钥和证书是否匹配
openssl x509 -noout -modulus -in server.crt | openssl md5
openssl rsa -noout -modulus -in private.key | openssl md5

7. 故障排查

7.1 常见问题

  1. 证书链不完整

    # 验证证书链
    openssl verify -verbose -CAfile chain.pem server.crt
    
  2. 私钥不匹配

    # 比对私钥和证书的模数
    openssl rsa -noout -modulus -in private.key | openssl md5
    openssl x509 -noout -modulus -in server.crt | openssl md5
    

7.2 调试技巧

# SSL连接调试
openssl s_client -connect example.com:443 \
    -debug -msg -servername example.com

# 证书验证调试
openssl verify -verbose -CAfile ca.crt \
    -verify_email user@example.com server.crt

openssl生成rsa key和crt

date: 2022-07-10T18:05:07+08:00
tags: ["openssl","crt"]

openssl生成rsa 密钥对,用于少量数据的加密解密

  1. 先生成rsa私钥
  openssl genrsa -out rsa_private.key 2048
  1. 再生成公钥
  openssl rsa -in rsa_private.key -pubout -out rsa_public_key.pem

openssl版本号 OpenSSL 1.1.1f 31 Mar 2020

常用方式: 公钥解密,私钥加密