本文最后更新于:2023年12月5日 下午

https 协议需要服务器部署证书方可正常工作,本文记录 SSL证书获取方法。

SSL 证书

  • SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secure socketlayer(SSL),SSL安全协议主要用来提供对用户和服务器的认证;对传送的数据进行加密和隐藏;确保数据在传送中不被改变,即数据的完整性,现已成为该领域中全球化的标准。由于SSL技术已建立到所有主要的浏览器和WEB服务器程序中,因此,仅需安装服务器证书就可以激活该功能了)。即通过它可以激活SSL协议,实现数据信息在客户端和服务器之间的加密传输,可以防止数据信息的泄露。保证了双方传递信息的安全性,而且用户可以通过服务器证书验证他所访问的网站是否是真实可靠。
  • SSL网站不同于一般的Web站点,它使用的是“HTTPS”协议,而不是普通的“HTTP”协议。因此它的URL(统一资源定位器)格式为“https://www.baidu.com”。
  • openssl是目前最流行的SSL密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS协议的实现。

x509证书链

  • x509证书一般会用到三类文件,key,csr,crt。
  • Key是私用密钥,openssl格式,通常是rsa算法。
  • csr是证书请求文件,用于申请证书。在制作csr文件的时候,必须使用自己的私钥来签署申请,还可以设定一个密钥。
  • crt是CA认证后的证书文件(windows下面的csr,其实是crt),签署人用自己的key给你签署的凭证。

概念

  • 首先要有一个CA根证书,然后用CA根证书来签发用户证书。

  • 用户证书申请流程:一般先生成一个私钥,然后用私钥生成证书请求(证书请求里应含有公钥信息),再利用证书服务器的CA根证书来签发证书。

openssl中有如下后缀名的文件

格式 含义
.key 私有的密钥
.csr 证书签名请求(证书请求文件),含有公钥信息,certificate signing request的缩写
.crt 证书文件,certificate 的缩写
.crl 证书吊销列表,Certificate Revocation List的缩写
.pem 用于导出,导入证书时候的证书的格式,有证书开头,结尾的格式

CA根证书的生成

流程一

生成CA私钥(.key)–>生成CA证书请求(.csr)–>自签名得到根证书(.crt)(CA给自已颁发的证书)。

在实际的软件开发工作中,往往服务器就采用这种自签名的方式,因为毕竟找第三方签名机构是要给钱的,也是需要花时间的。

  • 服务器端用户证书:
1
2
3
4
5
6
# private key  
$openssl genrsa -des3 -out server.key 2048
# generate csr
$openssl req -new -key server.key -out server.csr
# generate certificate
$openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
  • 客户端用户证书:
1
2
3
$openssl genrsa -des3 -out client.key 2048   
$openssl req -new -key client.key -out client.csr
$openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key
  • 生成pem格式证书:

有时需要用到pem格式的证书,可以用以下方式合并证书文件(crt)和私钥文件(key)来生成

1
2
$cat client.crt client.key> client.pem 
$cat server.crt server.key > server.pem
  • 得到证书

服务端证书:ca.crt, server.key, server.crt, server.pem

客户端证书:ca.crt, client.key, client.crt, client.pem

  • 在执行$openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key时可能会出错:
1
Using configuration from /usr/share/ssl/openssl.cfg I am unable to access the ./demoCA/newcerts directory ./demoCA/newcerts: No such file or directory 

解决方法:

1
2
3
4
mkdir -p ./demoCA/newcerts 
touch demoCA/index.txt
touch demoCA/serial
echo 01 > demoCA/serial

流程二

  • 生成秘钥
1
openssl genrsa -out ca.key 2048
  • 生成证书请求文件
1
openssl req -new -key ca.key -out ca.csr
  • 此时需要填写各种信息,其中 Common Name 为证书绑定的域名,必须正确填写,其余可以为空
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:ipv6.zywvvd.com
Email Address []:

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

  • 生成CA根证书 (公钥证书)
1
openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca_public.crt
  • 生成秘钥(服务器秘钥)
1
openssl genrsa -out server_private.key 2048
  • 根据服务器私钥生成公钥文件
1
openssl rsa -in server_private.key -pubout -out server_public.pem
  • 服务器向CA机构申请签名证书,申请前自己的证书签名请求文件
1
openssl req -new -key server_private.key -out server.csr
  • 同样,到此处需要填写各种信息,同样需要正确填写 Common Name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ openssl req -new -key server_private.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:ipv6.zywvvd.com
Email Address []:

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

  • 根据服务器端server.csr 文件向CA申请证书

  • 签名过程需要 CA公钥证书和私钥参与

  • 最终颁发一个CA签名证书服务器端

1
openssl x509 -req -days 3650 -CA ca_public.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt

days 设置证书有效时间

  • 得到所有的文件
1
2
$ ls
ca.csr ca.key ca_public.crt ca_public.srl server.crt server.csr server_private.key server_public.pem
  • server.crt , server_private.key 为需要使用的文件

自签名SSL证书生成

流程一

1
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout nginx.key -out nginx.crt
  • 同样需要填写相同信息,注意填准确的 Common Name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Generating a RSA private key
...+++++
.....................................+++++
writing new private key to 'nginx.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:ipv6.zywvvd.com
Email Address []:

  • 生成 nginx.key nginx.crt 文件

流程二

  • 设置server.key,这里需要设置两遍密码:
1
openssl genrsa -des3 -out server.key 2048 
  • 参数设置,首先这里需要输入之前设置的密码:
1
openssl req -new -key server.key -out server.csr

然后需要输入如下的信息,Common Name 需要正确填写

1
2
3
4
5
6
7
8
9
10
11
12
Country Name (2 letter code) [AU]: 国家名称
State or Province Name (full name) [Some-State]: 省
Locality Name (eg, city) []: 城市
Organization Name (eg, company) [Internet Widgits Pty Ltd]: 公司名
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []: 网站域名
Email Address []: 邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 这里要求输入密码
An optional company name []:
  • 写RSA秘钥(这里也要求输入之前设置的密码):
1
openssl rsa -in server.key -out server_nopwd.key
  • 获取私钥:
1
openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt

days 可以设置证书有效时长

  • 生成的证书文件和私钥:
1
server.crt server.key

申请 Trusted SSL 证书

  • 可以在各大云服务运营商端申请
  • 以百度智能云为例: 参考流程

特别说明

  • 自签名证书(一般用于顶级证书、根证书): 证书的名称和认证机构的名称相同.
  • 根证书:根证书是CA认证中心给自己颁发的证书,是信任链的起始点。任何安装CA根证书的服务器都意味着对这个CA认证中心是信任的。
  • 数字证书则是由证书认证机构(CA)对证书申请者真实身份验证之后,用CA的根证书对申请人的一些基本信息以及申请人的公钥进行签名(相当于加盖发证书机构的公章)后形成的一个数字文件。
  • 数字证书包含证书中所标识的实体的公钥(就是说你的证书里有你的公钥),由于证书将公钥与特定的个人匹配,并且该证书的真实性由颁发机构保证(就是说可以让大家相信你的证书是真的),因此,数字证书为如何找到用户的公钥并知道它是否有效这一问题提供了解决方案。
  • 新版Linux 发行版要求密钥长度最少2048位,1024位的可能报错
1
nginx:SSL: error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too small
  • CA 机构颁发的证书才会正常访问 https,自签名的需要手动添加信任证书

事实上我没有成功生成 CA 证书,最终用到还是在百度智能云下载的证书

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/internet/gen-ssh-cert/gen-ssh-cert/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

SSL 证书生成
https://www.zywvvd.com/notes/coding/internet/gen-ssh-cert/gen-ssh-cert/
作者
Yiwei Zhang
发布于
2022年1月24日
许可协议