Files
Linux/资源安装/OpenSSL搭建CA证书颁发机构Genpkey.md
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

87 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# OpenSSL搭建CA证书颁发机构Genpkey
[官网文档](https://www.openssl.org/docs/man3.0/man1/openssl-genpkey.html)
## 基础环境准备
1. 安装 OpenSSL
- Centos
```bash
yum install openssl -y
```
- Ubuntu
```bash
apt install openssl -y
```
## 创建根证书颁发机构
1. 生成 CA 私钥
```bash
openssl genpkey -algorithm RSA -out RootCA.key
```
> -pkeyopt rsa_keygen_bits:4096 -aes256
2. 生成 CA 自签名证书
```bash
openssl req -key RootCA.key -new -x509 -days 3650 -out RootCA.pem -subj "/C=CN/ST=Beijing/L=Beijing/O=Linux/OU=Linux/CN=*.com"
```
`-subj`后面跟着的是一个字符串用于指定证书的主题Subject字段。这些字段包含了证书所有者的识别信息通常用于证书显示和验证过程中。各个字段的意义如下
- `/C=CN`:国家名称,使用两位国家代码,例如 `CN` 代表中国。
- `/ST=YourState`:州或省份名称,根据你的地理位置填写。
- `/L=YourCity`:城市名称,根据你的地理位置填写。
- `/O=YourOrganization`:组织名称,表示证书所有者所属的公司或组织。
- `/OU=YourOrganizationalUnit`:组织单位名称,是组织内部的一个部门或单位。
- `/CN=YourCommonName`:常用名称,通常是域名或个人的名称。对于根 CA这里可以是机构的名称或描述。
- `/emailAddress=your.email@example.com`:电子邮件地址,用于联系证书的所有者。
## 创建并签署子证书(这里演示为泛域名证书)
1. 生成子证书密钥
```bash
openssl genpkey -algorithm RSA -out wildcard.key
```
2. 创建证书签名请求(CSR)
> 使用子证书的私钥创建一个 CSR。CSR 包含证书的相关信息。
```bash
openssl req -new -key wildcard.key -out wildcard.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=Linux/OU=Linux/CN=*.com"
```
3. 使用根证书签署子证书
```bash
openssl x509 -req -days 3650 -in wildcard.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -out wildcard.crt -sha256
```
## 分发证书
> 将子证书server.pem和根证书RootCA.pem分发给需要验证身份的用户或系统。
### 注意事项:
- **安全性**:确保私钥文件(如 RootCA.key 和 server.key安全存储避免泄露。
- **有效期**:根证书通常具有较长的有效期(如 10 年),而子证书具有较短的有效期(如 1 年)。根据你的安全政策调整这些设置。
- **撤销列表CRL和在线证书状态协议OCSP**:为了提高安全性,建立机制以撤销或检查证书的有效性。
> 这只是创建 CA 和管理证书的基本概念。实际操作中,可能需要考虑更多的安全措施和复杂的配置选项。
```bash
openssl pkcs12 -export -out RootCA.pfx -inkey RootCA.key -in RootCA.pem
```