什么是CA

CA(Certification Authority),内容包括:电子签证机关的信息、公钥用户信息、公钥、权威机构的签字和有效期等。

TSL/SSL

传输层安全性协议(英语:Transport Layer Security,缩写:TLS)及其前身安全套接层(英语:Secure Sockets Layer,缩写:SSL)是一种安全协议,目的是为互联网通信提供安全及数据完整性保障。

OpenSSL

OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信,避免窃听,同时确认另一端连线者的身份。其主要库是以C语言所写成,实现了基本的加密功能,实现了SSL与TLS协议。

CA证书的搭建

CA 自签名证书(构造根CA)

CA配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
####################################################################
[ ca ]
default_ca= CA_default      #默认CA
####################################################################
[ CA_default ]

dir=/etc/pki/CA            # CA的工作目录这里其实是定义了一个变量
certs= $dir/certs          # 证书存储路径
crl_dir= $dir/crl          # 证书吊销列表
database= $dir/index.txt   # 证书数据库列表
#unique_subject = no        # 设置为no允许同一个sbject可创建多个证书

new_certs_dir= $dir/newcerts       #新的证书路径

certificate = $dir/cacert.pem  # CA自己的证书(The CA certificate)
serial= $dir/serial        #当前证书的编号(The current serial number)
crlnumber= $dir/crlnumber  #要被吊销的证书编号(The current crl number)
crl = $dir/crl.pem         # The current CRL
private_key = $dir/private/cakey.pem # CA 的私钥
RANDFILE= $dir/private/.rand   # private random number file

x509_extensions = usr_cert      # The extentions to add to the cert

# Comment out the following two lines for the "traditional"
# (and highly broken) format.
name_opt = ca_default       # 命名方式
cert_opt = ca_default       # CA的选项

# Extension copying option: use with caution.
# copy_extensions = copy

# Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs
# so this is commented out by default to leave a V1 CRL.
# crlnumber must also be commented out to leave a V1 CRL.
# crl_extensions= crl_ext

default_days= 365           # 默认证书的有效期限
default_crl_days= 30            # how long before next CRL
default_md= default     # use public key default MD
preserve= no            # keep passed DN ordering

# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
# 策略,这里记录的是 将来CA在搭建的时候,以及客户端申请证书的时候,需要提交的信息的匹配程度。

policy= policy_match   
# For the CA policy
[ policy_match ]                # match意味着CA以及子CA必须一致
countryName = match             # 国家
stateOrProvinceName= match      # 州或者省
organizationName= match         #组织公司,可设置为optinal
organizationalUnitName  = optional
commonName= supplied
emailAddress= optional

# For the 'anything' policy
# At this point in time, you must list all acceptable 'object'
# types.
[ policy_anything ]             #可以对外提供证书申请,这时,证书的匹配就可以不用那么严格
countryName = optional
stateOrProvinceName = optional
localityName= optional
organizationName= optional
organizationalUnitName  = optional
commonName  = supplied
emailAddress= optional
  • 配置文件位于/etc/pki/tls/openssl.cnf下
  • 生成证书索引数据库文件 touch /etc/pki/CA/index.txt
  • 指定第一个颁发证书的序列号 echo 01 > /etc/pki/CA/serial

数字证书中主题(Subject)中字段的含义

  • 基本信息

| 字段名 | 说明 | | —————————– | ———————————————————— | | 公用名称(Common Name) | 简称:CN字段,对于SSL证书一般为网站域名,对于代码签名证书则为申请单位,对于客户端证书则为申请者姓名。 | | 单位名称(Organization Name) | 简称:O字段,对于SSL证书一般为网站域名,对于代码签名证书则为申请单位名称,对于客户端单位证书则为证书申请者所在单位名称。 |

  • 证书申请单位所在地

| 字段名 | 字段说明 | | ————————- | ——————————————— | | 所在城市(Locality) | 简称:L字段 | | 所在省份(State/Provice) | 简称:S字段 | | 所在国家(Contry) | 简称:C字段,只能是国家字母缩写。例如中国:CN |

  • 其它字段

| 字段名 | 字段说明 | | —————– | ————————————————– | | 电子邮件(Email) | 简称:E字段 | | 多个姓名字段 | 简称:G字段 | | 介绍 | Description字段 | | 电话号码 | Phone字段,格式要求:+国家区号 城市区号 电话号码。 | | 地址 | STREET字段 | | 邮政编码 | PostalCode字段 | | 其它内容 | 简称:OU字段 |

1.生成私钥文件

1
openssl genrsa -des3 -out /etc/pki/CA/private/cakey.pem 2048

使用 3-DES 对称加密算法加密密钥对,该参数需要用户在密钥生成过程中输入一个口令用于加密。今后使用该密钥对时,需要输入相应的口令。如果不加该选项,则不对密钥进行加密。

同时临时指定了umask ,使得生成的私钥文件只对自己具有读写权限。

1
2
3
4
5
6
> (umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem -des3 2048 )
> ```
>
> 

#### 2. 生成自签名证书

openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
生成证书的过程中需要输入之前设定的私钥的密码*

> -new: 生成新证书签署请求*
>
> *-x509: 专用于CA生成自签证书*
>
> *-key: 生成请求时用到的私钥文件*
>
> *-days n:证书的有效期限*
>
>  *-out /PATH/TO/SOMECERTFILE: 证书的保存路径*

交互信息

> Country Name (2 letter code) [XX]:CN
> State or Province Name (full name) []:shandong
> Locality Name (eg, city) [Default City]:qingdao
> Organization Name (eg, company) [Default Company Ltd]:pojun.tech
> Organizational Unit Name (eg, section) []:opt
> Common Name (eg, your name or your server's hostname) []:ca.pojun.tech
>
> Email Address []:

---

等价于下面两个步骤:

1 生成请求文件

openssl req -new -days 365 -key /etc/pki/CA/private/cakey.pem -out careq.pem

1
2
3
4
5
6
7
8
9
> -new 
> 生成一个新的证书请求。该参数将令 OpenSSL 在证书请求生成过程中要求用户填写一些相应的字段。
> -days 365 
> 从生成之时算起,证书时效为 365 天。
> -key ./demoCA/private/cakey.pem 
> 指定 ./demoCA/private/cakey.pem 为证书所使用的密钥对文件。
> -out careq.pem 

2 签发证书

openssl ca -selfsign -in careq.pem -out cacert.pem

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
> -selfsign 
> 使用对证书请求进行签名的密钥对来签发证书。
>
> -in careq.pem 
> 指定 careq.pem 为证书请求文件。
>
> -out cacert.pem 
> 指定 cacert.pem 为输出的CA根证书。



#### 3.颁发证书

分两种情况:

* 子CA证书机构向根CA申请证书
* 普通用户向CA或子CA机构申请证书

**在需要使用证书的主机上生成证书请求**

##### 3.1 生成私钥

openssl genrsa -out /etc/pki/CA/private/cakey.pem 1024

openssl genrsa -des3 -passout pass:123456 -out userkey.pem 1024

1
2
3
> -passout arg: arg为对称加密的密码(使用这个参数就省去了console交互提示输入密码的环节)

##### 3.2 生成证书 请求文件

openssl req -new -day 3650 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/tls/subca.csr

1
##### 3.3 CA颁发证书

openssl ca -in /etc/pki/CA/subca.csr -out /etc/pki/CA/certs/subca.crt -days 3650

1
2
3
> *作为子CA机构存在的证书文件必须是cacert.pem,否则子CA将不能够给其他用户颁发证书。*

## 查看证书状态

openssl x509 -in /etc/pki/CA/cacert.pem -noout -text|issuer|subject|serial|dates

1
2
3
4
5
6
7
> *-text   证书的内容*
> *-issuer 证书颁发者的信息*
> *-subject  证书主体的信息*
> *-serial  证书的序列号信息*
> *-dates 查看证书的时间*

例如查看证书颁发者的信息

openssl x509 -in /etc/pki/CA/cacert.pem -noout -issuer

1
2
3
## 吊销证书

### 1 首先在(子)CA主机上获取到要吊销的证书的serial

openssl x509 -in /etc/pki/CA/certs/subca.crt -noout -serial -subject

输出如下

serial=01 subject= /C=CN/ST=shandong/O=pojun.tech/OU=opt/CN=subca.pojun.tech

1
### 2  吊销证书

openssl ca -revoke /etc/pki/CA/newcerts/01.pem

1
2
3
> **在根CA上根据客户提交的serial与subject信息,对比检验是否与index.txt文件中的信息一致**

### 3 更新证书吊销列表

指定下一个证书吊销时的编号

echo 01 > /etc/pki/CA/crlnumber

更新

openssl ca -gencrl -out /etc/pki/CA/crl/crl.pem

1
### 4 查看证书吊销列表的文件

openssl crl -in /etc/pki/CA/crl/crl.pem -noout -text ```