TLDR
-
创建文件generate.sh
#!/usr/bin/env bash
find . \( -name "$1.*" -o -name "*.srl" \) -type f -delete
cp /usr/lib/ssl/openssl.cnf $1.cnf
python <(
cat << "END"
import sys
from ConfigParser import ConfigParser
from StringIO import StringIO
domain = sys.argv[1]
config = ConfigParser()
config.optionxform = lambda option: option
name = "{}.cnf".format(domain)
with open(name, "rb") as stream:
config.readfp(StringIO("[top]\n" + stream.read()))
config.set(" v3_ca ", "subjectKeyIdentifier", "hash")
config.set(" v3_ca ", "authorityKeyIdentifier", "keyid:always,issuer")
config.set(" v3_ca ", "basicConstraints", "critical, CA:TRUE, pathlen:3")
config.set(" v3_ca ", "keyUsage", "critical, cRLSign, keyCertSign")
config.set(" v3_ca ", "nsCertType", "sslCA, emailCA")
config.set(" v3_req ", "basicConstraints", "CA:FALSE")
config.set(" v3_req ", "keyUsage", "nonRepudiation, digitalSignature, keyEncipherment")
config.set(" v3_req ", "subjectAltName", "@alt_names")
config.remove_option(" v3_req ", "extendedKeyUsage")
config.add_section(" alt_names ")
config.set(" alt_names ", "DNS.1", domain)
config.set(" alt_names ", "DNS.2", "*.{}".format(domain))
config.set(" req ", "req_extensions", "v3_req")
with open(name, "wb") as stream:
config.write(stream)
END
) $1
tail -n +2 $1.cnf > $1.cnf.tmp && mv $1.cnf.tmp $1.cnf
echo "$1\n" | openssl genrsa -aes256 -out $1.ca.key 2048
chmod 400 $1.ca.key
openssl req -new -x509 -subj "/CN=$1" -extensions v3_ca -days 3650 -key $1.ca.key -sha256 -out $1.ca.crt -config $1.cnf
openssl genrsa -out $1.key 2048
openssl req -subj "/CN=$1" -extensions v3_req -sha256 -new -key $1.key -out $1.csr
openssl x509 -req -extensions v3_req -days 3650 -sha256 -in $1.csr -CA $1.ca.crt -CAkey $1.ca.key -CAcreateserial -out $1.crt -extfile $1.cnf
openssl x509 -in $1.crt -text -noout
-
致电./generate.sh example.com
需要 Python 2
所有学分都转到 Fabian Lee 的 this excellent article。
使用 OpenSSL 创建受信任的 CA 和 SAN 证书
- 自定义 openssl.cnf
- 创建 CA 证书
- 使用由 CA 签名的 SAN 创建服务器证书
先决条件
作为先决条件,请确保已安装 SSL 包:
$ sudo apt install libssl1.0.0 -y
自定义 openssl.cnf
第一步是获取系统上可用的openssl.cnf 模板。在 Ubuntu 上,这可以在 /usr/lib/ssl/openssl.cnf 找到。您可以在 MacOS 上的 /System/Library/OpenSSL/ 和 Redhat 变体上的 /etc/pki/tls 中找到它。
export prefix="mydomain"
cp /usr/lib/ssl/openssl.cnf $prefix.cnf
$prefix.cnf 需要使用我们要生成的证书的具体信息进行修改。
在[ v3_ca ] 部分下,添加以下值。对于 CA,这意味着我们正在创建一个将用于密钥签名的 CA。
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = critical, CA:TRUE, pathlen:3
keyUsage = critical, cRLSign, keyCertSign
nsCertType = sslCA, emailCA
然后在 [ v3_req ] 部分下,设置以下内容以及此证书的所有有效替代名称。
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
#extendedKeyUsage=serverAuth
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = mydomain.com
DNS.2 = *.dydomain.com
同时取消注释 [ req ] 部分下的以下行,以便使用 v3 扩展创建证书请求。
req_extensions = v3_req
当我们生成每种类型的密钥时,我们会指定要使用的扩展部分,这就是为什么我们可以共享 $prefix.cnf 来创建 CA 和 SAN 证书。
创建 CA 证书
现在我们将开始使用 OpenSSL 创建必要的密钥和证书。首先生成私钥/公钥 RSA 密钥对:
openssl genrsa -aes256 -out ca.key.pem 2048
chmod 400 ca.key.pem
这使用基于 AES256 的密码对密钥文件进行编码。
然后我们需要创建自签名根 CA 证书。
openssl req -new -x509 -subj "/CN=myca" -extensions v3_ca -days 3650 -key ca.key.pem -sha256 -out ca.pem -config $prefix.cnf
您可以使用以下方法验证此根 CA 证书:
openssl x509 -in ca.pem -text -noout
这将显示根 CA 证书,Issuer 和 Subject 将是相同的,因为这是自签名的。这被标记为CA:TRUE,这意味着它将被识别为根 CA 证书;这意味着浏览器和操作系统将允许将其导入到其受信任的根证书存储中。
Issuer: CN=myca
...
Subject: CN=myca
...
X509v3 Basic Constraints:
critical CA:TRUE, pathlen:3
X509v3 Key Usage:
critical Certificate Sign, CRL Sign
Netscape Cert Type:
SSL CA, S/MIME CA
创建由 CA 签名的服务器证书
现在创建了根 CA,我们切换到服务器证书。首先生成私钥/公钥 RSA 密钥对:
openssl genrsa -out $prefix.key.pem 2048
我们没有在此密钥上设置密码,只是因为 CA 是更有价值的目标,我们始终可以重新生成服务器证书,但请随时采取这种额外的预防措施。
然后创建服务器证书签名请求:
openssl req -subj "/CN=$prefix" -extensions v3_req -sha256 -new -key $prefix.key.pem -out $prefix.csr
然后使用以下内容生成服务器证书:服务器签名请求、CA 签名密钥和 CA 证书。
openssl x509 -req -extensions v3_req -days 3650 -sha256 -in $prefix.csr -CA ca.pem -CAkey ca.key.pem -CAcreateserial -out $prefix.crt -extfile $prefix.cnf
$prefix.key.pem 是服务器私钥,$prefix.crt 是服务器证书。验证证书:
openssl x509 -in $prefix.crt -text -noout
这将显示证书,Issuer 将是 CA 名称,而 Subject 是前缀。这未设置为 CA,Subject Alternative Name 字段包含将被浏览器视为有效的 URL。
Issuer:
CN=myca
...
Subject:
CN=mydomain
...
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment
X509v3 Subject Alternative Name:
DNS:mydomain.com, DNS:*.mydomain.com
浏览器评估
当您第一次使用带有 CA 签名的 SAN 证书将 Chrome 或 Firefox 指向站点时,它会抛出与自签名 SAN 证书相同类型的异常。这是因为根 CA 证书不是签名证书的可信来源。
铬
Linux
在 Linux 上,Chrome 管理自己的证书存储区,您应该再次将 ca.pem 导入 Authorities。这现在应该使安全图标变为绿色。
Windows
在 Chrome 设置 (chrome://settings) 中,搜索 certificates 并点击 Manage Certificates。在 Windows 上,这将打开 Windows 证书管理器,您应该在 Trusted Root Certification Authorities 选项卡中导入 ca.pem 文件。这相当于通过mmc.exe 将其添加到local user 受信任的根存储(不是计算机级别)中。
火狐
在 Firefox 选项about:preferences 中,搜索certificates 并单击View Certificates。转到Authorities 选项卡并导入ca.pem。选中该框以使其信任网站,现在当您访问该页面时锁定图标应变为绿色。