如何让浏览器信任本地主机 SSL 证书?

How to make browser trust localhost SSL certificate?

尽管 are similar questions, and even good answers,他们要么不特别关心本地主机,要么询问一个特定的 option/solution(自签名与 CA)。

有哪些选择?他们如何比较?我该怎么做?

tl;dr 生成由自己的 CA 颁发的证书(见下面的脚本)

这是我的发现。不对的地方指正。

有 CA(证书颁发机构)。他们为其他 CA(中间 CA)或服务器(终端实体证书)颁发证书(签署 CSR)。其中一些是根权限。他们有 self-signed 证书,由他们自己颁发。也就是说,通常存在从服务器证书到根证书的信任链。而且没有人可以担保根证书。因此,OS'es 有一个根证书库(或信任策略库),一个系统范围的受信任根证书列表。浏览器有自己的受信任证书列表,其中包括系统范围列表和用户信任的证书。

在 Chromium 中,您可以在 chrome://settings/certificates 管理证书。在 Firefox 中,Preferences > Privacy & Security > Certificates > View Certificates。两者都有 Authorities 选项卡,这是一个受信任的根证书列表。和服务器选项卡,可信服务器证书列表。

要获得您创建 CSR(证书签名请求)的证书,请将其发送给 CA。 CA 签署 CSR,在此过程中将其转化为受信任的证书。

证书和 CSR 是一堆包含信息和 public 键的字段。一些字段称为扩展。 CA证书是带有basicConstraints = CA:true.

的证书

您可以在 Developer Tools > Security 中检查 Chromium 中的证书错误。

在系统范围内信任证书

当您更改 OS' 根证书存储区时,您必须重新启动浏览器。您将其更改为:

# trust anchor path/to/cert.crt
# trust anchor --remove path/to/cert.crt

trust 将 CA 证书置于 "authority" 类别 (trust list) 下,否则为 "other-entry" 类别。 CA 证书出现在浏览器的“权限”选项卡中,或者出现在“服务器”选项卡中。

Firefox 不信任来自 OS 根证书存储的服务器证书,这与 Chromium 不同。两者都信任来自 OS 根证书库的 CA 证书。

在浏览器中信任证书

在 Chromium 和 Firefox 中,您可以将证书添加(导入)到 Authorities 选项卡。如果您尝试导入 non-CA 证书,您会收到 "Not a Certificate Authority" 消息。选择文件后,会出现一个对话框,您可以在其中指定信任设置(何时信任证书)。使站点工作的相关设置是 "Trust this certificate for identifying websites."

在 Chromium 中,您可以在服务器选项卡上添加(导入)证书。但它们最终要么出现在 Authorities 选项卡上(CA 证书,并且在选择文件后不会显示信任设置对话框),要么出现在 Others 选项卡上(如果 non-CA 证书)。

在 Firefox 中,您无法准确地将证书添加到“服务器”选项卡。您添加例外。你可以信任一个完全没有扩展名的证书(差)。

Self-signed 证书扩展

我的系统带有以下证书的默认设置(要添加的扩展名):

basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer

摘自 /etc/ssl/openssl.cnf, section v3_ca. More on it here.

此外,Chromium 认为证书无效,当它没有 subjectAltName = DNS:$domain.

非self-signed 证书扩展

来自第 [ usr_cert ] of /etc/ssl/openssl.cnf 部分:

basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer

当浏览器信任 self-signed 证书时

要让 Chromium 信任 self-signed 证书,它必须有 basicConstraints = CA:truesubjectAltName = DNS:$domain。对于 Firefox,这还不够:

basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
subjectAltName = DNS:$domain

当浏览器信任由自己的 CA 颁发的证书时

Firefox 不需要扩展,但 Chromium 需要 subjectAltName

openssl作弊sheet

openssl genpkey -algorithm RSA -out "$domain".key - 生成私钥 (man)

openssl req -x509 -key "$domain".key -out "$domain".crt - 生成 self-signed 证书 (man)

如果没有 -subj,它将询问有关专有名称 (DN) 的问题,例如通用名称 (CN)、组织 (O)、地点 (L)。你可以回答他们 "in advance": -subj "/CN=$domain/O=$org".

要添加 subjectAltName 扩展,您必须有一个指定所有内容的配置,或者在配置中添加一个部分并告诉 openssl 它的名称 -extensions开关:

    -config <(cat /etc/ssl/openssl.cnf - <<END
[ x509_ext ]
basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
subjectAltName = DNS:$domain
END
    ) -extensions x509_ext

openssl req -new -key "$domain".key -out "$domain".csr - 生成 CSR,它可以采用 -subj 选项 (man)

openssl x509 -req -in "$domain".csr -days 365 -out "$domain".crt \ -CA ca.crt -CAkey ca.key -CAcreateserial - 签署 CSR (man)

没有 -CAcreateserial 就无法工作。它创建一个 ca.srl 文件,其中保存最后生成的证书的序列号。要添加 subjectAltName,您需要 -extfile 开关:

    -extfile <(cat <<END
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
subjectAltName = DNS:$domain
END
    )

openssl req -in $domain.csr -text -noout - 查看 CSR (man)

openssl x509 -in $domain.crt -text -noout - 查看证书 (man)

生成self-signed证书

(您需要在 Firefox 中设置例外才能正常工作)

#!/usr/bin/env bash
set -eu
org=localhost
domain=localhost

sudo trust anchor --remove "$domain".crt || true

openssl genpkey -algorithm RSA -out "$domain".key
openssl req -x509 -key "$domain".key -out "$domain".crt \
    -subj "/CN=$domain/O=$org" \
    -config <(cat /etc/ssl/openssl.cnf - <<END
[ x509_ext ]
basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
subjectAltName = DNS:$domain
END
    ) -extensions x509_ext

sudo trust anchor "$domain".crt

生成由自己的 CA 颁发的证书

#!/usr/bin/env bash
set -eu
org=localhost-ca
domain=localhost

sudo trust anchor --remove ca.crt || true

openssl genpkey -algorithm RSA -out ca.key
openssl req -x509 -key ca.key -out ca.crt \
    -subj "/CN=$org/O=$org"

openssl genpkey -algorithm RSA -out "$domain".key
openssl req -new -key "$domain".key -out "$domain".csr \
    -subj "/CN=$domain/O=$org"

openssl x509 -req -in "$domain".csr -days 365 -out "$domain".crt \
    -CA ca.crt -CAkey ca.key -CAcreateserial \
    -extfile <(cat <<END
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
subjectAltName = DNS:$domain
END
    )

sudo trust anchor ca.crt

网络服务器配置

Nginx:

server {
    listen  443  ssl;
    ssl_certificate  ssl/localhost.crt;
    ssl_certificate_key  ssl/localhost.key;
    ...

莫尔博:

carton exec morbo --listen='https://*:3000?cert=localhost.crt&key=localhost.key' \
    site.pl

P.S。我正在 运行ning Chromium 65.0.3325.162、Firefox 59.0 和 openssl-1.1.0.g.

Windows

显然,Windows 没有 trust 实用程序。在 Windows 下有 two stores: Local Machine and Current User Certificate stores. No point in using Local Machine Certificate Store, since we're making it work just for our current user. Then, there are substores. With two predefined of them being of most interest: Trusted Root Certification Authorities and Intermediate Certification Authorities Stores. Commonly referred in command line as root and CA.

您可以通过以下 chrome://settin 访问 Chrome 的证书管理器s/?search=Manage%20certificates,然后单击管理证书。最感兴趣的是受信任的根证书颁发机构和中间证书颁发机构选项卡。

管理证书的一种方法是通过 command line:

>rem list Current User > Trusted Root Certification Authorities store
>certutil.exe -store -user root

>rem list Local Machine > Intermediate Certification Authorities store
>certutil.exe -store -enterprise CA

>rem GUI version of -store command
>certutil.exe -viewstore -user CA

>rem add certificate to Current User > Trusted Root Certification Authorities store
>certutil.exe -addstore -user root path\to\file.crt

>rem delete certificate from Current User > Trusted Root Certification Authorities store by serial number
>certutil.exe -delstore -user root 03259fa1

>rem GUI version of -delstore command
>certutil.exe -viewdelstore -user CA

结果如下(对于本地计算机和当前用户证书存储):

root
    localhost.crt
        error
    ca.crt
        appears in Trusted Root Certification Authorities tab
CA
    localhost.crt
        doesn't work, appears in Other People tab
    ca.crt
        doesn't work, appears in Intermediate Certification Authorities tab

其他选项是 double-clicking 在资源管理器中的证书上,从 Chrome 的证书管理器导入证书,使用证书 MMC Snap-in (运行 certmgr.msc), 或者使用 CertMgr.exe.

对于安装了 grep 的用户,以下是快速检查证书位置的方法:

>certutil.exe -store -user root | grep "localhost\|^root\|^CA" ^
& certutil.exe -store -user CA | grep "locahost\|^root\|^CA" ^
& certutil.exe -store -enterprise root | grep "localhost\|^root\|^CA" ^
& certutil.exe -store -enterprise CA | grep "localhost\|^root\|^CA"

因此,将 CA 证书安装到当前用户 > 受信任的根证书颁发机构存储中似乎是最佳选择。 确保不要忘记

补充阅读

OpenSSL
genpkey
req
x509
OpenSSL Certificate Authority
Certificates for localhost
iamaCA - Become your own certificate authority and dispense certifications
Firefox and Self-Signed Certs
Bypassing certificate error page in Chrome

在 chrome 上,可以浏览到 chrome://flags/#allow-insecure-localhost 并启用

允许从本地主机加载的资源使用无效证书。 选项

在浏览器中打开 link 并启用它: chrome://flags/#temporary-unexpire-flags-m87

然后重新启动 chrome 并搜索本地主机,您会在那里找到它。

您可以通过 Microsoft 管理控制台在 Windows 中添加根 CA 证书。请按照以下步骤进行添加。

  • 登录到 Windows 服务器。

  • 打开 MMC。

  • 选择文件 > Add/Remove Snap-ins.

  • 选择证书,然后选择添加。

  • 选择我的用户帐户。

  • 再次选择添加,这次 select 计算机帐户。

  • 将新证书从 Certificates-Current 用户 > 受信任的根证书颁发机构移动到证书(本地计算机)> 受信任的根证书颁发机构。