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;                     # 启用双向认证
相关推荐
Jason-河山1 小时前
「铭记历史 珍爱和平」勿忘9.18!
前端
国王不在家1 小时前
keyof和infer
前端
猿大撒1 小时前
Spring-data-redis
前端·bootstrap·html
帅过二硕ฅ2 小时前
uniapp点击跳转到对应位置
前端·javascript·uni-app
正小安5 小时前
URL.createObjectURL 与 FileReader:Web 文件处理两大法宝的对比
前端·javascript
赵广陆5 小时前
SprinBoot+Vue宠物寄养系统的设计与实现
前端·vue.js·宠物
A黄俊辉A6 小时前
vue3中把封装svg图标为全局组件
前端·javascript·vue.js
老贾爱编程6 小时前
VUE实现刻度尺进度条
前端·javascript·vue.js
F2E_Zhangmo6 小时前
vue如何做到计算属性传参?
前端·javascript·vue.js
繁依Fanyi7 小时前
828华为云征文|华为Flexus云服务器搭建OnlyOffice私有化在线办公套件
服务器·开发语言·前端·python·算法·华为·华为云