在localhost上实现SSL的正确方法

Tar*_*ada 13 ssl openssl nginx certificates

任何人都可以提出一种在本地主机上实现的生成自签名证书的现代方法,Chrome 和 Mozilla 会接受这种方法吗?

我尝试了 openssl 代,但是 Mozilla 抱怨发行者不受信任。

Centos 7、nginx

gar*_*Red 11

警告:在您潜入运行您自己的证书颁发机构的雷区之前,您可能需要研究安全隐患!

但是,如果您必须这样做,请继续阅读快速而肮脏的 CA,它将在https://localhost/没有警告消息的情况下为您提供...

创建以下文本文件:

# OpenSSL configuration for Root CA

[ req ]

prompt             = no
string_mask        = default

# The size of the keys in bits:
default_bits       = 2048
distinguished_name = req_distinguished_name
x509_extensions    = x509_ext

[ req_distinguished_name ]

# Note that the following are in 'reverse order' to what you'd expect to see.

countryName = gb
organizationName = Test
commonName = Test Root CA

[ x509_ext ]

basicConstraints=critical,CA:true,pathlen:0
keyUsage=critical,keyCertSign,cRLSign
Run Code Online (Sandbox Code Playgroud)

另存为root.cnf然后生成请求:

$ openssl req -x509 -new -keyout root.key -out root.cer -config root.cnf
Run Code Online (Sandbox Code Playgroud)

这将创建您的根 CA 证书 ( root.cer) 和您root.key必须保密的根 CA 私钥 ( )。它将提示输入私钥的密码 - 确保您选择一个强密码。

现在为服务器证书创建一个配置文件:

# OpenSSL configuration for end-entity cert

[ req ]

prompt             = no
string_mask        = default

# The size of the keys in bits:
default_bits       = 2048
distinguished_name = req_distinguished_name

x509_extensions    = x509_ext

[ req_distinguished_name ]

# Note that the following are in 'reverse order' to what you'd expect to see.

countryName = gb
organizationName = Test
commonName = localhost

[ x509_ext ]

keyUsage=critical,digitalSignature,keyAgreement

subjectAltName = @alt_names

# Multiple Alternate Names are possible
[alt_names]
DNS.1 = localhost
# DNS.2 = altName.example.com
Run Code Online (Sandbox Code Playgroud)

将其另存为server.cnf并使用以下命令生成请求:

openssl req -nodes -new -keyout server.key -out server.csr -config server.cnf
Run Code Online (Sandbox Code Playgroud)

以上将生成另一个server.key您必须保护的私钥 ( )。在这种情况下,密钥不受密码保护,但您可以通过删除该-nodes选项来添加密码。

最后,使用文件中的新根 CA 和扩展名对请求进行签名server.cnf(为方便起见):

$ openssl x509 -req -in server.csr -CA root.cer -CAkey root.key -set_serial 123 -out server.cer -extfile server.cnf -extensions x509_ext
Run Code Online (Sandbox Code Playgroud)

注意:为-set_serial选项选择任何随机数。

它会询问您在生成根 CA 时输入的密码。

server.cer将生成服务器证书 ( )。

现在,将根 CA 证书 ( root.cer)添加到 Firefox 的信任锚库,以便浏览器信任您的新 CA。

使用 OpenSSL 作为临时 Web 服务器运行测试:

$ sudo openssl s_server -key server.key -cert server.cer -accept 443 -www
Run Code Online (Sandbox Code Playgroud)

注意:如果您已经在端口 443 上运行了服务器,则可能会出现错误。在这种情况下,请停止正在运行的服务器或通过将结尾更改为(例如)将上面的端口号更改为另一个未使用的端口 -accept 8443 -www

当您使用 Firefox导航到https://localhost(或者https://localhost:8443如果您更改了上面的端口号)时,您现在应该不会看到任何警告,并且会看到您安装的 OpenSSL 可以提供的密码列表。

一旦您对结果感到满意,请将server.key和添加server.cer到您的原始网络服务器并进行相应配置。