openssl 生成自签名证书

1、openssl生成CA根证书

1.1、生成CA私钥

openssl genrsa -out root_ca.key 2048

注意: 私钥必须妥善保管,既不能丢失,也不能泄露。如果发生丢失和泄露,必须马上重新

生成,以使旧的证书失效。

1.2、通过ca私钥生成pem格式的ca根证书

openssl req -x509 -new -nodes -key root_ca.key -sha256 -days 36500 -out root_ca.pem

接下来需要填写:

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:ShannXi

Locality Name (eg, city) [Default City]:XiAn

Organization Name (eg, company) [Default Company Ltd]:xxx

Organizational Unit Name (eg, section) []:Test

Common Name (eg, your name or your server's hostname) []:xxx

Email Address []:

注意: '-days 36500'代表生成的根证书的有效期为36500天,下面的参数根据实际填写即可。

2、用根证书签发server证书

2.1、生成服务端私钥:

openssl genrsa -out http_server.key 2048

2.2、新建server_req.conf文件:

[req]

default_bits = 2048

distinguished_name = req_distinguished_name

req_extensions = req_ext

x509_extensions = v3_req

prompt = no

[req_distinguished_name]

countryName = CN

stateOrProvinceName = ShannXi

localityName = XiAn

organizationName = xxx

commonName = *.xxx.com #修改 设备连接用的域名或ip

[req_ext]

subjectAltName = @alt_names

[v3_req]

subjectAltName = @alt_names

[alt_names]

IP.1 = 0.0.0.0

IP.2 = 127.0.0.1

IP.3 = xxxxx

IP.4 = xxxxx

DNS.1 = *.xxx.com

DNS.2 = localhost

注意:

1、alt_names里的IP列填设备要访问的服务端的IP地址。

2、alt_names里的DNS列填设备要访问的服务端的域名地址。

3、commonName 填设备要访问的服务端的IP或域名地址。

4、当客户端访问服务端的地址为IP地址,那么只会在x509的扩展项subjectAltName 中寻找IP进行 匹配,也就是到alt_names 里面的IP列去寻找IP进行匹配。

5、当客户端访问服务端的地址为域名地址,则除了在 subjectAltName 中寻找域名进行匹配(也就是到alt_names里面的DNS列去匹配)外,还会检查req_distinguished_name中的 commonName 是否和域名匹配。

2.3、生成证书签名请求:

openssl req -new -key ./http_server.key -config server_req.conf -out http_server.csr

2.4、生成最终服务端证书

用请求文件、根证书、私钥、server_req.conf,签发新的服务端证书:http_server.pem,

设置有效期为36500天。

openssl x509 -req -in ./http_server.csr -CA root_ca.pem -CAkey root_ca.key -CAcreateserial -out http_server.pem -days 36500 -sha256 -extensions v3_req -extfile server_req.conf

3、用根证给签发client证书

3.1生成客户端私钥

openssl genrsa -out xxxxx.key 2048

注意:文件名为:xxxxx。

3.2、生成客户端证书签名请

openssl req -new -key xxxxx.key -out xxxxx.csr -subj "/C=CN/ST=Shanghai/L=Shanghai/O=Shanghai/CN=xxxxx"

注意:文件名、参数CN值,应该保持一致,都为xxxxx。

3.3、签发客户端证书

用证书请求文件、根证书、私钥,签发客户端证书:xxxxx.pem,设置有效期为36500天。文件名为:xxxxx。

openssl x509 -req -days 36500 -in xxxxx.csr -CA root_ca.pem -CAkey root_ca.key -CAcreateserial -out xxxxx.pem

4、当更换服务端的IP或者域名时

当迁移服务器,导致服务端IP变更,或者客户端访问服务端的域名发生变更时,我们该如何更换自签名证书?

4.1、当服务端的IP发生变更时:

只需要将新的IP 地址更新到server_req.conf 文件里alt_names 的IP列里面,然后用server_req.conf文件重新生成服务端证书签名请求,再重新生成服务端证书,用新证书替换原有的服务端旧证书,再重启ac-gateway-http服务,设备端不用变更证书,就可以直接访问新服务器了。

4.2、当服务端的域名发生变更时:

当服务端是以域名地址提供对外服务时,当域名地址变更时,我们除了要将新的域名地址更新到server_req.conf 文件里alt_names 的DNS列里面,还要将req_distinguished_name中的 commonName值更新成新的域名地址,然后用server_req.conf文件重新生成服务端证书签名请求,再重新生成服务端证书,用新证书替换原有的服务端旧证书,然后重启ac-gateway-http服务,设备端不用变更证书,就可以直接访问新服务器了。

相关推荐
Karoku0664 分钟前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen016 分钟前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦37 分钟前
linux 解压缩
linux·运维·服务器
牧小七2 小时前
Linux命令---查看端口是否被占用
linux
鸡鸭扣3 小时前
虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
linux·运维·ubuntu
友友马3 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http
记得开心一点嘛4 小时前
在Linux系统上使用Docker部署javaweb项目
linux·运维·docker
Tak1Na4 小时前
2024.9.18
linux·运维·服务器
Yana.nice4 小时前
linux中将文本转为unix格式
linux
安得权5 小时前
Ubuntu 20.04 部署 NET8 Web - Systemd 的方式 达到外网访问的目的
linux·前端·ubuntu