自签证书在一些内部服务经常用到,下面使用openssl生成。
注意:自签证书默认不受浏览器和操作系统信任。
一、生成根CA证书
1.1 生成根密钥
使用RSA算法生成,这个算法各种设备兼容性最好,但逐渐被ECC算法替代。
1 | openssl genrsa -des3 -out rootCA.key 2048 |
1.2 生成根证书
1 | openssl req -x509 -new -key rootCA.key -sha256 -days 3650 -out rootCA.crt |
二、创建服务器证书
2.1 创建服务器证书私钥
1 | openssl genrsa -out server.key 2048 |
1.2 使用CA证书生成服务器证书
1 | openssl req -new -key server.key -out server.csr |
1.3 使用根证书签发服务器证书
server_ext.cnf配置
1 | [ req ] |
1 | openssl x509 -req -in server.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out server.crt -days 365 -extfile server_ext.cnf -extensions v3_req |
三、创建客户端证书
3.1 生成客户端私钥
1 | openssl genrsa -out client.key 2048 |
3.2 生成客户端证书
1 | openssl req -new -key client.key -out client.csr |
3.3 使用根证书签发客户端证书
client_ext.cnf配置
1 | [ req ] |
1 | openssl x509 -req -in client.csr -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -out client.crt -days 365 -extfile client_ext.cnf -extensions v3_req |
四、后缀说明
| 后缀 | 说明 |
|---|---|
| crt | 存储公钥信息 |
| key | 私钥文件,不可泄露 |
| csr | 全称:证书签名请求(Certificate Signing Request) |
| cnf | 配置文件,通过extfile参数指定,为证书添加扩展信息(SAN) |
| pem | 证书存储格式,使用base64编码和证书头尾标识存储 |
| der | 证书存储格式,使用二进制存储 |
五、优点和缺点
5.1 优点
- 完全免费
- 支持mTLS(双向TLS)
5.2 缺点
- 默认不被浏览器和操作系统信任
- 需维护和管理证书,会比较麻烦
六、参考链接
- 本文作者: 暮秋人
- 本文链接: https://muqiuren.pages.dev/2024/09/self-signed-certificate/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!