为 *.localhost ('wildcard') 生成自签名证书的正确方法是什么

What is the correct way to generate a selfsigned cert for *.localhost ('wildcard')

我这辈子都无法 chrome 使用通配符证书 ("*.localhost")

这是我生成上述证书的方法。

首先我生成我的 ca

openssl genrsa -out priv/cert/ca.key 2048
openssl req -new -x509 -nodes -subj "/C=US/O=_Development \
CA/CN=Development certificates" -key priv/cert/ca.key -sha256 \
-days 3650 -out priv/cert/ca.crt

然后我生成我的本地主机

openssl genrsa -out priv/cert/localhost.key 2048
openssl req -new -subj "/C=US/O=Local Development/CN=*.localhost" -key \
priv/cert/localhost.key -out priv/cert/localhost.csr

然后我制作我的 ext 文件

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @alt_names
[req]
req_extensions = req_ext
[req_distinguished_name]
commonName_default = localhost
[req_ext]
subjectAltName = @alt_names
[alt_names]
DNS.1 = *.localhost
DNS.2 = foo.localhost
DNS.3 = localhost

那我签。

openssl x509 -req \
    -in priv/cert/localhost.csr \
    -extfile priv/cert/localhost.ext \
    -CA priv/cert/ca.crt \
    -CAkey priv/cert/ca.key \
    -CAcreateserial \
    -out priv/cert/localhost.crt \
    -days 365 \
    -sha256

在信任我的 crt 并在本地导航说 localhost 或 foo.localhost 之后,我全都变绿了,但是如果我说 bar.localhost 通配符不起作用,我得到 NET::ERR_CERT_COMMON_NAME_INVALID 在 chrome.

我在这里错过了什么。我已经解决了主题备用名称,我已经排除了通过 foo 工作的 SAN。从我读到的内容来看,通用名称甚至不再重要。我很茫然。

TL;DR:浏览器不会接受证书中的 *.localhost

*.localhost 中,localhost 后缀被视为顶级域 (TLD),不允许在 TLD 正下方使用通配符。这背后的想法基本上是,没有一个组织真正拥有像 com 这样的 TLD,因此允许 *.com 的证书是非常危险的。因此 *.localhost 不会被接受,而 *.foo.localhost 会被接受。

有关此的更多信息,请参阅 Wildcard *.localhost SSL with Nginx and Chrome and Can a wildcard SSL certificate be issued for a second level domain?