OpenSSL生成自签SSL证书

Subject信息字段

  • C:Country(国家) - 表示证书所有者所在的国家,使用两个字母的国家代码(例如,CN代表中国)。
  • ST:State or Province Name(州或省名) - 表示证书所有者所在的州或省的名称。
  • L:Locality Name(地区名) - 表示证书所有者所在的城市或地区。
  • O:Organization Name(组织名) - 表示证书所有者所属的组织或公司名称。(也用来标识租户信息
  • CN:Common Name(通用名称) - 通常是证书所有者的完全合格域名(FQDN),也可以是个人姓名或其他标识。(也用来标识用户信息

生成CA证书

方式一:直接生成私钥和公钥

$ openssl req -newkey rsa:2048 -x509 -nodes -keyout ca.key -out ca.crt -days 3650 -subj "/CN=China CA"

方式二:已有私钥生成公钥

$ openssl genrsa -out ca.key 2048
$ openssl req -x509 -new -key ca.key -out ca.crt -days 3650 -subj "/CN=China CA"

方式三:公钥添加 X509V3 扩展

$ openssl genrsa -out ca.key 2048

$ openssl req -new -out ca.csr -key ca.key -subj "/CN=China CA"

$ cat <<-EOF | sudo tee ca-csr.conf > /dev/null
[ alt_names ]
DNS.1 = etcd-ca

[ v3_ext ]
keyUsage=Digital Signature, Key Encipherment, Certificate Sign
basicConstraints=CA:TRUE
subjectKeyIdentifier=hash
subjectAltName=@alt_names
EOF

$ openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt -days 3650 -extfile ca-csr.conf -extensions v3_ext

生成服务端证书

方法一:(无需CA签发)

$ openssl genrsa -out server.key 2048

$ openssl req -new -key server.key -out server.csr -subj "/"

$ openssl x509 -req -in server.csr -signkey server.key -out server.crt -days 3650 -extfile <(printf "subjectAltName=IP:127.0.0.1,DNS:example.com,DNS:www.example.com")

方法二:(需要CA签发)

$ openssl genrsa -out server.key 2048

$ openssl req -new -key server.key -out server.csr -subj "/"

$ openssl x509 -req -CA ca.crt -CAkey ca.key -in server.csr -out server.crt -CAcreateserial -days 3650 -extfile <(printf "subjectAltName=IP:127.0.0.1,DNS:example.com,DNS:www.example.com")

生成客户端证书

$ openssl genrsa -out client.key 2048

$ openssl req -new -key client.key -out client.csr -subj "/O=system:master/CN=jixzeng"

$ openssl x509 -req -CA ca.crt -CAkey ca.key -in client.csr -out client.crt -CAcreateserial -days 3650
posted @   jiaxzeng  阅读(361)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示