如何使用“openssl x509 -req”将 csr 文件中的 AltName 添加到 crt 文件?

信息安全 证书 openssl 证书颁发机构 铬合金 windows-10
2021-08-13 10:07:39

我是一名网络开发人员,想使用自签名 SSL 证书在本地测试我的网站。直到几天前,当 chrome 开始抱怨缺少AltName属性时,一切都运行良好。

OpenSSL CA

我使用以下方法创建了自己的权限:

openssl req
    -x509
    -sha256
    -new
    -out dev.root.ca.crt
    -keyout dev.root.ca.key
    -days 3650

CNF

openssl.cnf通过将这些值添加到默认值来创建一个文件:

[ CA_default ]
copy_extensions = copy

[req]
req_extensions = v3_req

[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = $ENV::ALTNAME

通过外壳的 ALTNAME

然后我使用这个命令生成一个.csr.key文件:

set ALTNAME=DNS:dev.example.com

openssl req
    -newkey rsa:2048
    -out dev.example.com.csr
    -pubkey
    -new
    -keyout dev.example.com.key
    -sha256
    -config openssl.cnf

生成的csr文件包含预期的替代名称。

Altname 无法从 CSR 变为 CRT

然后我使用这个命令来生成.crt.key文件:

openssl x509
    -req
    -in dev.example.com.csr
    -CA dev.root.ca.crt
    -CAkey dev.root.ca.key
    -CAcreateserial
    -out dev.example.com.crt
    -days 3650
    -sha256

crt但是生成的文件中不再存在替代名称。

现在怎么办?

我需要在openssl x509 -req命令中添加其他参数吗?

1个回答

使用“-extfile”参数修复它。

附加配置文件

我添加了一个openssl-ext.cnf文件,其中包含:

basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = $ENV::ALTNAME

使用“-extfile”参数

并使用-extfile参数将该新配置文件添加到 openssl 命令

openssl x509
    -req
    -in dev.example.com.csr
    -CA dev.root.ca.crt
    -CAkey dev.root.ca.key
    -CAcreateserial
    -out dev.example.com.crt
    -days 3650
    -sha256
    -extfile openssl-ext.cnf