自签发 SSL/TLS 证书的方法以及遇到的一些问题 - yanbin's Blog

自签发 SSL/TLS 证书的方法以及遇到的一些问题

yanbin posted @ 2015年3月20日 18:05 in Networking with tags linux Networking , 5966 阅读
PKI (Public Key Infrastructure, 公开密钥基础架构)简介
0. 目的
PKI 旨在创造、管理、分配、使用、存储以及撤销数字证书
Unix/Linux 平台上常用的 PKI 软件是 openssl.
 
1. 数字证书是什么?
在密码学上视数字证书为一种身份凭证,是一种电子文件,用于证明公开密钥的所有者的身份。
数字证书文件中包含所有者的身份信息、公开密钥信息以及数字证书本身的数字签章;
数字签章用于验证证书文件内容正确无误——没有损坏或者没有被修改过——。
如果签章是正确的,而用户可以相信签署者,之后用户就知道他们可以使用这个密钥,来与密钥的拥有者进行通信。在X.509中规范了这个认证的过程与标准。①
认证过程中会包含「用 CA 的根(roo)证书验证这份数字证书是由该 CA 签发的」。
用户可以没有自己的数字证书,但是一定需要 CA 的根证书。比如:https 通信。
 
2. 谁负责创造数字证书?
CA(Certificate Authority) 负责签发数字证书。
CA 由受信任第三方担任,签发数字证书给通信双方使用;有时只签发给服务方,比如: https 通信。
这样理解或许比较简单,互联网上的数字证书签发实际上要复杂。
我们自己签发证书给自己的程序使用,不公开业务也不公开 CA root 证书,可以不用关心太多。
 
大公司的内部业务也可能足够复杂的到需要 CA,RA 之类的。参见维基百科公开密钥基础建设
 
也有 12306 或者银行这样的公司要求用户自己下载它的 CA 的 root 证书。
 
3. 证书的层级结构。
 
设置 CA 信息和签发 CA root 证书
0.修改 openssl.conf 文件里的某些域的值。
req_distingushed_name section 可以考虑修改为:
[ req_distinguished_name ]
   countryName = Country Name (2 letter code)
   countryName_default = CN
   stateOrProvinceName = State or Province Name (full name)
   stateOrProvinceName_default = ShangHai

   localityName = Locality Name (eg, city)
   localityName_default = ShangHai
   0.organizationName = Organization Name (eg, company)
   0.organizationName_default = My Company Name

   organizationalUnitName = Organizational Unit Name (eg, section)
   organizationalUnitName_default = May Project

1. 生成 CA root key (根密钥); CA root key 非常重要。
a) 不仅签发或更新 CA root 证书需要。
b) 签发通信双方数字证书时也需要。

Create the keypair:
$ openssl genrsa -des3 -out root-ca.key 1024
 

0) 指定了 -des3 选项,openssl 要求输入 pass phrase;
pass phraseroot key 的密码;
CA 签发数字证书时需要输入这个密码,算是一种防范措施吧。

1) FIXME: 这种密码也可以用到普通证书的 key,
程序使用证书和 key 时, 一般会要求输入密码,可以作为用户密码用吗?

2) FIXME: 1024 是 key  的长度目前也支持 2048, 这两个值有很大的区别吗?

key 的长度为 2048 会耗费更多的 CPU 资源吧。

2. 自签发 CA 根证书(root certificat)
Use the key to sign itself:
$ openssl req -new -x509 -days 3650 -key root-ca.key -out root-ca.crt  -config openssl.cnf
0) 关键参数是 -x509:
生成的 root-ca.crt 是一个 self-signed X.509 certificate;

1) 这条命令将两个步骤合二为一。
a) 使用 private key 生成 certificate request; 需要填写证书持有者的信息;
b) 使用 certificate request 签发证书(singed)或自签发证书(self-signed);

2) 这个自签发证书为什么能做为 CA root certificate 使用?
因为指定了参数 -x509, openssl 配置文件 req section 指定了 x509_extensions = v3_ca;
而 v3_ca section 里则有:basicConstraints = CA:true
 
3) openssl.cnf 以及 CA directory 都不是系统中唯一存在的;
任意创建一个目录,创建配置文件(比如: openssl.cnf),也可以签发根证书。
$ mkdir demoCA && cd demoCA
$ touch index.txt
$ echo 01 > serial
$ mkdir private #指定 private_key=/path/to/demoCA/myCA/private/root-ca.key
$ mkdir myCA # 指定 CA_default:dir=/path/to/demoCA/myCA
 
4) 可以使用如下命令查看 root-ca.crt 的信息
$ openssl x509 -noout -text -in root-ca.crt

 

签发服务器证书
0.生成一个 private key, 并且直接用新生成 private key 创建一个 certificate request.
NOTE: -nodes 参数,使用这个参数生成的 key 不需要密码

$ openssl req -newkey rsa:1024 -keyout server01.key -nodes -config openssl.cnf -out server01.req

Country Name (2 letter code) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [My Company Name]:
Second Organization Name (eg, company) [My Company Name]:
Organizational Unit Name (eg, section) [My Project]: 
Common Name (eg, YOUR name) []:*.example.com #CN
Email Address []:server01@example.com

NOTE: CN(common name) 可以是 hostname,IP 或者 domain, 与主机信息对应;
      也可以用这种形式: *.example.com
      CN 指的是  common name, 别与中国的代码弄混了。

 

1.使用新生成的 certificate request 签发数字证书。
$ openssl ca -config openssl.cnf -out server01.crt -infiles server01.req
# Using configuration from openssl.cnf

Enter pass phrase for /etc/pki/CA/private/root-ca.key: # 输入root key 文件的密码
DEBUG[load_index]: unique_subject = "yes"
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity # FIXME: 默认是一年的有效期吗?
# 默认是 365 days 的有效期, 润年会是 366 days.
Not Before: Apr 29 15:18:20 2015 GMT
Not After : Apr 29 15:18:20 2016 GMT
Subject:
countryName = CN
stateOrProvinceName = ShangHai
localityName = ShangHai
organizationName = My Company Name
organizationName = My Company Name
organizationalUnitName = My Project
commonName = *.example.com
emailAddress = server01@example.com
.......
Certificate is to be certified until Apr 29 15:18:20 2015 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

 
签发客户端数字证书
0.为什么需要签发 client 证书?
a)client 可以用证书作为身份标识;
b)签发 client 数字证书与签发 server 数字证书相比,并没有特别的不同。
c)-nodes 不需要 key 文件有密码
 
1. 生成 client private key, 并生成 certificate request.
$ openssl req -newkey rsa:1024 -keyout client001.key -nodes3  -config openssl.cnf -out client001.req
Country Name (2 letter code) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [My Company Name]:
Second Organization Name (eg, company) [My Company Name]:
Organizational Unit Name (eg, section) [My Project]: 
Common Name (eg, YOUR name) []:client001
EmailAddress []: client001@example.com
   
2. 使用新生成的 certificate request 签发证书
$ openssl ca -config openssl.cnf -out client001.crt -infiles client001.req

Enter pass phrase for /etc/pki/CA/private/root-ca.key:
DEBUG[load_index]: unique_subject = "yes"
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 2 (0x2)
Validity:
Not Before: Apr 29 15:18:20 2015 GMT
Not After : Apr 29 15:18:20 2016 GMT
Subject:
countryName = CN
stateOrProvinceName = ShangHai
localityName = ShangHai
organizationName = My Company Name
organizationName = My Company Name
organizationalUnitName = My Project
commonName = client001
emailAddress = client001@example.com
.......
Certificate is to be certified until Apr 29 15:18:20 2015 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
 
需要注意的几个问题
拖延症来袭,明天再更新

参考:
 

 

Avatar_small
earth day coloring p 说:
2018年3月26日 17:56

This is also a very good post which I really enjoyed reading.

Avatar_small
Five Nights at Fredd 说:
2018年11月07日 16:41

你在这里分享的文章非常好。

Avatar_small
reese 说:
2022年11月29日 17:03

One problem that can be encountered when using a self-signed SSL/TLS certificate is that some browsers will display a warning message when visiting a website that is using one. This warning message can be confusing for users and may cause them to avoid using the site. Additionally, self-signed cbd oil for sleep certificates are not properly vetted and so there is no guarantee that they are actually secure. This can be a serious problem if sensitive data is

Avatar_small
charlly 说:
2022年12月29日 15:12

When using a self-signed SSL/TLS certificate, one issue that can arise is that some browsers will display a warning message when accessing a website that is using one. Users may find this warning notice unclear and decide not to use the site as a result. Self-signed certificates are moreover not thoroughly reviewed, diamond rings thus there is no assurance that they are genuinely secure.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee