使用OpenSSL生成根证书并自签署证书

生成根CA的私钥和证书

bash 复制代码
# 生成根 CA 的私钥
[root@developer ssl]# openssl genrsa -out rootCA.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
...+++++
............................................................+++++
e is 65537 (0x010001)

# 使用私钥生成根 CA 的证书
[root@developer ssl]# openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt
Country Name (2 letter code) [XX]:CN  # 国家名称(2个字母代码)
State or Province Name (full name) []:JS  # 州或省名
Locality Name (eg, city) [Default City]:KS  # 地点名称
Organization Name (eg, company) [Default Company Ltd]:local  # 组织名称
Organizational Unit Name (eg, section) []:developer  # 组织单位名称
Common Name (eg, your name or your server's hostname) []:Local Developer CA  # 通用名称
Email Address []:

为hello-world.test生成私钥和证书请求文件(CSR)

bash 复制代码
# 生成 hello-world.test 的私钥
[root@developer ssl]# openssl genrsa -out hello-world.test.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..........................................................................+++++
.....+++++
e is 65537 (0x010001)

# 使用私钥生成证书请求文件
[root@developer ssl]# openssl req -new -key hello-world.test.key -out hello-world.test.csr
Country Name (2 letter code) [XX]:CN  # 国家名称(2个字母代码)
State or Province Name (full name) []:JS  # 州或省名
Locality Name (eg, city) [Default City]:KS  # 地点名称
Organization Name (eg, company) [Default Company Ltd]:local  # 组织名称
Organizational Unit Name (eg, section) []:developer  # 组织单位名称
Common Name (eg, your name or your server's hostname) []:*.hello-world.test  # 域名
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

创建证书扩展文件

为了确保为 hello-world.test 签名的证书能够用作服务器身份验证,需要为它创建一个扩展文件。创建一个名为 v3.ext 的文件,并添加以下内容:

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

[alt_names]
DNS.1 = *.hello-world.test
# 如果是IP就使用 IP.1 = 你的IP地址

使用根 CA 的证书为 hello-world.test 签名证书

bash 复制代码
[root@developer ssl]# openssl x509 -req -in hello-world.test.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out hello-world.test.crt -days 500 -sha256  -extfile v3.ext
Signature ok
subject=C = CN, ST = JS, L = KS, O = local, OU = developer, CN = *.hello-world.test
Getting CA Private Key

此时文件夹内应该有以下文件:

bash 复制代码
[root@developer ssl]# ll
-rw-r--r--. 1 root root 1318 5月  23 09:59 rootCA.crt
-rw-------. 1 root root 1675 5月  23 09:59 rootCA.key
-rw-r--r--. 1 root root   41 5月  23 10:14 rootCA.srl
-rw-r--r--. 1 root root  253 5月  23 10:13 v3.ext
-rw-r--r--. 1 root root 1322 5月  23 10:14 hello-world.crt
-rw-r--r--. 1 root root  997 5月  23 10:02 hello-world.csr
-rw-------. 1 root root 1679 5月  23 10:00 hello-world.key

宝塔部署

宝塔网站设置中SSL选项选择当前证书,密钥(KEY)中填入hello-world.key的内容,证书(PEM格式)中填入hello-world.crt的内容,最后将rootCA.crt安装到电脑的本地受信任的根证书颁发机构中。

相关推荐
Arwen3039 小时前
解密国密 SSL 证书:SM2、SM3、SM4 算法的协同安全效应
算法·安全·ssl
bksheng11 小时前
【SSL证书校验问题】通过 monkey-patch 关掉 SSL 证书校验
网络·爬虫·python·网络协议·ssl
lixzest13 小时前
/usr/bin/ld:找不到 -lssl
ssl
MediaTea14 小时前
Python 库手册:ssl 加密通信模块
开发语言·网络·python·网络协议·ssl
猫头虎16 小时前
[精选] 2025最新生成 SSH 密钥和 SSL 证书的标准流程(Linux/macOS/Windows系统服务器通用方案)
linux·服务器·开发语言·macos·ssh·ssl·ai编程
DreamJia9116 小时前
Nginx 服务器 SSL 证书安装部署并配置(已实测)
服务器·nginx·ssl
Arwen3031 天前
从 “http” 到 “https”:只差一张 SSL
http·https·ssl
lvchaoq2 天前
简单讲解HTTPS如何保证安全性和可靠性
网络协议·https·ssl
Arwen3032 天前
常见 SSL 证书问题解答:从报错到维护全涵盖
服务器·网络·网络协议·http·https·ssl·html5
五岁小孩3 天前
教师资格证(教资)笔试如何备考?|三色笔记+万能模板+历年真题高效备考法
证书·职称·教师资格证·教资