如何使浏览器信任localhost SSL证书?

x-y*_*uri 9 openssl localhost ca self-signed ssl-certificate

虽然,有 类似 的问题,甚至是很好的 答案,他们要么不与本地主机过问具体而言,或询问有关一个特定的选项/溶液(自签署VS CA).

有什么选择?他们如何比较?我这样做吗?

x-y*_*uri 12

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

这是我发现的.在我错的地方纠正我.

有CA(证书颁发机构).他们为其他CA(中间CA)或服务器(最终实体证书)颁发证书(签署CSR).其中一些是根权威.他们有自己签发的自签名证书.也就是说,通常存在从服务器证书到根证书的信任链.并且没有人可以担保根证书.因此,OS具有根证书存储(或信任策略存储),系统范围的受信任根证书列表.浏览器有自己的可信证书列表,其中包括系统范围列表以及用户信任的证书.

在Chromium中,您可以在chrome:// settings/certificates上管理证书.在Firefox中Preferences > Privacy & Security > Certificates > View Certificates.两者都有"权限"选项卡,该选项卡是受信任的根证书列表.和Servers选项卡,可信服务器证书列表.

要获取证书,请创建CSR(证书签名请求),然后将其发送给CA. CA签署CSR,在此过程中将其转换为可信证书.

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

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

全系统信任证书

当您更改操作系统的根证书存储区时,您必须重新启动浏览器.你改变它:

# trust anchor path/to/cert.crt
# trust anchor --remove path/to/cert.crt
Run Code Online (Sandbox Code Playgroud)

trust将CA证书置于"权限"类别(trust list)或"其他条目"类别下.CA证书显示在浏览器的"权限"选项卡中,或"服务器"选项卡中.

Firefox不信任来自操作系统根证书存储区的服务器证书,而不是Chromium.两者都信任来自OS的根证书存储区的CA证书.

在浏览器中信任证书

在Chromium和Firefox中,您可以向"授权"选项卡添加(导入)证书.如果您尝试导入非CA证书,则会收到"非证书颁发机构"消息.选择文件后,会出现一个对话框,您可以在其中指定信任设置(何时信任证书).使网站正常工作的相关设置是"信任此证书以识别网站".

在Chromium中,您可以在"服务器"选项卡上添加(导入)证书.但它们最终会出现在"权限"选项卡上(CA证书,选择文件后未显示信任设置对话框),或者在"其他"选项卡上(如果是非CA证书).

在Firefox中,您无法将证书完全添加到"服务器"选项卡.您添加例外.你可以相信一个没有扩展的证书(穷人).

自签名证书扩展

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

basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
Run Code Online (Sandbox Code Playgroud)

取自/etc/ssl/openssl.cnf,第v3_ca节.更多关于它在这里.

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

非自签名证书扩展

从部分[ usr_cert ]/etc/ssl/openssl.cnf:

basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
Run Code Online (Sandbox Code Playgroud)

当浏览器信任自签名证书时

要让Chromium信任自签名证书,就必须拥有basicConstraints = CA:true,并且subjectAltName = DNS:$domain.对于Firefox而言,这还不够:

basicConstraints = critical,CA:true
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
subjectAltName = DNS:$domain
Run Code Online (Sandbox Code Playgroud)

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

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

openssl 备忘单

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

openssl req -x509 -key "$domain".key -out "$domain".crt- 生成自签名证书()

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

要添加subjectAltName扩展,您必须具有指定all的配置,或者添加一个部分来配置并openssl使用-extensionsswitch 告诉其名称:

    -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
Run Code Online (Sandbox Code Playgroud)

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()

不行不通-CAcreateserial.它创建一个ca.srl文件,保存最后生成的证书的序列号.要添加subjectAltName,您需要-extfile切换:

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

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

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

生成自签名证书

(在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
Run Code Online (Sandbox Code Playgroud)

生成由自己的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
Run Code Online (Sandbox Code Playgroud)

Web服务器配置

Nginx的:

server {
    listen  443  ssl;
    ssl_certificate  ssl/localhost.crt;
    ssl_certificate_key  ssl/localhost.key;
    ...
Run Code Online (Sandbox Code Playgroud)

Morbo:

carton exec morbo --listen='https://*:3000?cert=localhost.crt&key=localhost.key' \
    site.pl
Run Code Online (Sandbox Code Playgroud)

PS我正在运行Chromium 65.0.3325.162,Firefox 59.0和openssl-1.1.0.g.

视窗

显然,Windows没有trust实用程序.在Windows下,有一个存储:本地计算机和当前用户证书存储.没有必要使用本地机器证书存储,因为我们只为我们当前的用户工作.然后,有子库.其中最受关注的是两个预定义的:受信任的根证书颁发机构和中间证书颁发机构商店.在命令行通常称为根和CA.

您可以按照chrome:// settings /?search = Manage%20certificates,然后点击管理证书来访问Chrome的证书管理器.最感兴趣的是受信任的根证书颁发机构和中间证书颁发机构选项卡.

管理证书的一种方法是通过命令行:

>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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

其他选项包括在资源管理器中双击证书,从Chrome的证书管理器导入证书,使用证书MMC管理单元(运行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"
Run Code Online (Sandbox Code Playgroud)

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

补充阅读

OpenSSL的
genpkey
REQ
X509
OpenSSL的证书颁发机构
的证书为本地主机
iamaCA -成为自己的证书颁发机构和分配认证
Firefox和自签名的证书
在Chrome绕过证书错误页面