创建 CA 证书

生成 CA 私钥

openssl genrsa -out ca.key 2048

虽然标题是生成私钥,但其实是生成密钥对,公钥和私钥都保存在文件中,可以通过以下指令查看公钥:openssl rsa -pubout -in ca.key

使用 CA 私钥生成 CA 证书

openssl req -x509 -new -nodes -key ca.key -days 36500 -out ca.crt

会提示你输入以下内容:

Country Name (2 letter code) [AU]: CN
State or Province Name (full name) [Some-State]: SC
Locality Name (eg, city) []: CD
Organization Name (eg, company) [Internet Widgits Pty Ltd]: HOME
Organizational Unit Name (eg, section) []: LAB
Common Name (e.g. server FQDN or YOUR name) []: FISSSSSSH
Email Address []:

创建域名证书并使用 CA 证书签名

我现在有一个本地域名 lab.home,我想为其签发一张证书用于 HTTPS

创建域名证书私钥

openssl genrsa -out lab.home.key 2048

创建域名证书请求

openssl req -new -key lab.home.key -out lab.home.csr

会提示你输入以下内容:

Country Name (2 letter code) [AU]: CN
State or Province Name (full name) [Some-State]: SC
Locality Name (eg, city) []: CD
Organization Name (eg, company) [Internet Widgits Pty Ltd]: HOME
Organizational Unit Name (eg, section) []: LAB
Common Name (e.g. server FQDN or YOUR name) []: lab.home
Email Address []:

创建 X509 V3 证书扩展配置文件

我们需要在证书嵌入域名信息

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = lab.home
DNS.2 = *.lab.home

将以上内容保存到文件lab.home.conf

生成域名证书并签名

openssl x509 -req -in lab.home.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out lab.home.crt -days 36500 -sha256 -extfile lab.home.conf

客户端安装证书

将 CA 证书安装在客户端,以后由 CA 签发的证书都是可信任证书