生成符合chrome要求的自签名HTTPS证书

按照文章给Nginx配置一个自签名的SSL证书中给出的代码生成自签名证书后,发现使用chrome浏览器访问会报 Invalid self signed SSL cert - “Subject Alternative Name Missing” 的错误,原因是 "没有备份的主题名称",因而需要在生成的证书中加入备份的主题名称,于是基于liaoxuefeng老师的代码稍微修改了一下,使其能生成包含备份主题名称的证书了。代码如下:

#!/bin/bash

# create self-signed server certificate:

read -p "Enter your domain [www.example.com]: " DOMAIN

echo "Create server key..."

openssl genrsa -des3 -out $DOMAIN.key 1024

echo "Create server certificate signing request..."

SUBJECT="/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=$DOMAIN"

openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr

echo "Remove password..."

mv $DOMAIN.key $DOMAIN.origin.key
openssl rsa -in $DOMAIN.origin.key -out $DOMAIN.key

echo "Sign SSL certificate..."

echo "Gen subject ext file..."
echo "keyUsage = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName=@SubjectAlternativeName

[ SubjectAlternativeName ]
IP.1 = $DOMAIN" > $DOMAIN.ext

openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt -extfile $DOMAIN.ext

echo "TODO:"
echo "Copy $DOMAIN.crt to /etc/nginx/ssl/$DOMAIN.crt"
echo "Copy $DOMAIN.key to /etc/nginx/ssl/$DOMAIN.key"
echo "Add configuration in nginx:"
echo "server {"
echo "    ..."
echo "    listen 443 ssl;"
echo "    ssl_certificate     /etc/nginx/ssl/$DOMAIN.crt;"
echo "    ssl_certificate_key /etc/nginx/ssl/$DOMAIN.key;"
echo "}"

转载请注明出处,原文地址: https://www.cnblogs.com/AcAc-t/p/generate_self_signed_ip_certificate.html

参考

给Nginx配置一个自签名的SSL证书: https://www.liaoxuefeng.com/article/990311924891552
正确使用 OpenSSL 自签发代码|邮件|域名|IP 证书: https://vircloud.net/operations/sign-ip-crt.html/comment-page-1

posted @ 2021-10-24 22:53  及时  阅读(1007)  评论(0编辑  收藏  举报