微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

openssl – 在localhost上实现SSL的正确方法

任何人都可以建议一种现代的方法生成在localhost上实现的自签名证书,这将被Chrome和Mozilla接受吗?

我试过openssl一代,但Mozilla抱怨发行人是不受信任的.

Centos 7,Nginx

解决方法:

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

但是,如果必须,请继续阅读快速而脏的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

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

$openssl req -x509 -new -keyout root.key -out root.cer -config root.cnf

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

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

# 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

将其保存为server.cnf并生成请求:

openssl req -nodes -new -keyout server.key -out server.csr -config server.cnf

以上将生成一个您必须保护的私钥(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

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

它将询问您在生成根CA时输入的密码.

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

现在,将根CA证书(root.cer)添加到Firefox的trust-anchor商店并运行测试:

$sudo openssl s_server -key server.key -cert server.cer -accept 443 -www

注意:如果您已在端口443上运行服务器,则可能会出现错误.在这种情况下,请停止正在运行的服务器或将上面的端口号更改为另一个未使用的端口.

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

对结果感到满意后,将server.key和server.cer添加到Web服务器并进行相应配置.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐