在linux系统通过OpenSSL工具自签https证书

工具介绍

  • OpenSSL是SSL/TLS协议的实现工具

  • key是私钥文件,用于对发送给客户端的数据加密,以及对从客户端接收的数据进行解密。

  • csr是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名。

  • crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息

操作步骤

1、生成证书

生成私钥

openssl genrsa -des3 -out myCA.key 2048
##openssl genrsa 用于生成RSA私钥,不会生成公钥,因为公钥提取自私钥 
## -des3为加密方式 
## 2048为生成秘钥长度 
## 可以加上-nodes参数,禁止进行加密,即可不运行下面的消除密码

消除私钥key的密码
openssl rsa -in myCA.key -out myCA.key
生成pem文件
openssl req -utf8 -x509 -new -nodes -key myCA.key -sha256 -days 825 -out myCA.pem

2、创建CA签名证书

生成私钥
openssl genrsa -out server.key 2048

创建证书签名请求

openssl req -new -key server.key -out server.csr
##Common Name应该与域名保持一致,否则会引起浏览器警告

为扩展创建一个配置文件

>server.ext cat <<-EOF 
authorityKeyIdentifier=keyid,issuer 
basicConstraints=CA:FALSE 
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment 
subjectAltName = @alt_names 
[alt_names] 
DNS.1 = www.baidu.com # Be sure to include the domain name here because Common Name is not so commonly honoured by itself 
DNS.2 = www.sougou.com # Optionally, add additional domains (I've added a subdomain here) 
IP.1 = 192.168.1.1  # Optionally, add an IP address (if the connection which you have planned requires it) EOF 
## chrome 会查看当前域名是否在证书中声明,该声明由 subjectAltName 字段设置。上述的生成步骤默认未设置该字段。

创建签名证书
openssl x509 -req -in server.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial -out server.crt -days 3650 -sha256 -extfile server.ext

得到所需证书文件
server.crt
server.key

posted @ 2022-02-23 16:06  熊仔其人  阅读(809)  评论(2编辑  收藏  举报