openssl双向认证自签名证书生成

编写配置文件openssl.cnf

bash 复制代码
[ req ]
distinguished_name = req_distinguished_name
req_extensions     = req_ext

[ req_distinguished_name ]
countryName            = Country Name (2 letter code)
countryName_default    = US
stateOrProvinceName    = State or Province Name (full name)
stateOrProvinceName_default = California
localityName           = Locality Name (eg, city)
localityName_default   = San Francisco
organizationName       = Organization Name (eg, company)
organizationName_default = YourOrg
organizationalUnitName = Organizational Unit Name (eg, section)
organizationalUnitName_default = YourOrg Unit
commonName             = Common Name (e.g. server FQDN or YOUR name)
commonName_default     = example.com
commonName_max         = 64

[ req_ext ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = example.com
DNS.2 = www.example.com
IP.1  = 192.168.1.1

依次执行下面的命令

bash 复制代码
# 生成 CA 私钥
openssl genrsa -out rootCA.key 2048

# 生成 CA 证书签署请求
openssl req -new -x509 -days 3650 -key rootCA.key -out rootCA.crt -config openssl.cnf

# 生成服务端私钥
openssl genrsa -out server.key 2048

# 生成服务端 CSR
openssl req -new -key server.key -out server.csr -config openssl.cnf

# 生成服务端证书
openssl x509 -req -in server.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out server.crt -days 3650 -extensions req_ext -extfile openssl.cnf

# 生成客户端私钥
openssl genrsa -out client.key 2048

# 生成客户端 CSR
openssl req -new -key client.key -out client.csr -config openssl.cnf

# 生成客户端证书
openssl x509 -req -in client.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out client.crt -days 3650 -extensions req_ext -extfile openssl.cnf

查看证书

bash 复制代码
openssl x509 -in server.crt -text -noout

打包证书

bash 复制代码
openssl pkcs12 -export -out client.p12 -inkey client.key -in client.crt -certfile rootCA.crt

openssl base64 -in client.p12 -out client.p12.b64

nginx启用双向认证

bash 复制代码
    sl_certificate /path/to/server.crt;
    ssl_certificate_key /path/to/server.key;
    ssl_client_certificate /path/to/rootCA.crt;   # 用于验证客户端证书的 CA 证书
    ssl_verify_client on;                     # 启用双向认证
相关推荐
爱上好庆祝36 分钟前
学习js的第五天
前端·css·学习·html·css3·js
C澒1 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼981 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴1 小时前
前端与后端的区别与联系
前端
EnCi Zheng2 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
kyriewen2 小时前
你的网页慢,用户不说直接走——前端性能监控教你“读心术”
前端·性能优化·监控
广州华水科技2 小时前
北斗GNSS变形监测在大坝安全监测中的应用与优势分析
前端
前端老石人2 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
CAE虚拟与现实2 小时前
五一假期闲来无事,来个前段、后端的说明吧
前端·后端·vtk·three.js·前后端
Sarvartha2 小时前
三目运算符
linux·服务器·前端