OpenSSL 实践

简介

数字证书(digital certificate)是公钥基础设施(public key infrastructure, PKI)的一个重要组成部分。证书包括公用钥匙和标的物识别名称(distinguished name, DN)以及其他内容。DN 是证书所携带的公用钥匙的实体的标识符。这个实体可以是任何需要加密服务的实体,如个人、组织或计算机程序。证书签发者(certificate issuer)使用其私人密钥签署证书。证书拥有者将他的证书提供给他人,以便他人可以安全地与他/她通信。证书中没有任何秘密,因此可以使用任何传递方式。但是,证书接受者在使用所附的公钥之前,必须验证证书的真实性。要做到这一点,就要获得证书颁发者的公钥,并用它来验证证书的签名。发证者的公钥也是通过证书获得的。该证书必须由链中更高一级的另一个发证者验证,或者出于特定的原因信任其真实性。

认证机构(certification authority, CA)是一个人,或者更可能是一个组织,它得到许多用户的信任。由于商业或实用性的原因,CA 可以按等级组织起来。最底层的是最终用户,他们要求中间 CA(如区域 CA)签署他们的证书。中间 CA 的证书由更高一级的 CA(如国家 CA)签署。最终,达到最高级别的 CA。这个 CA 被称为根 CA,其证书称为根证书。根证书是由根 CA 自签的,因为没有更高一级的 CA 可以证明根 CA 的身份。完整的证书验证意味着从根证书以下的级别到终端用户级别的所有证书都得到验证。不这样做是一个严重的安全风险。根证书不能以同样的方式进行验证。相反,它们是通过声誉来信任的,并且通常包含在运给用户的产品中(例如,在 Web 浏览器或嵌入式设备中)。

CA 有两类:public 和 private 。

公共 CA(如 Verisign)在公众中建立了信任,因为它们在为个人和组织提供认证服务方面有经济利益。如果公众知道签发的证书是假的(无论是故意还是错误),就会对 CA 所享有的信任产生灾难性影响,从而对 CA 从认证服务中获得的利润产生灾难性影响。

私人 CA 负责在一个组织内签发证书,比如在企业内部。在这种情况下,信任来源于雇主和雇员的关系。一个理性的雇主非常希望维护数字资源的安全访问。因此,实施适当的安全程序,例如为员工管理数字证书,是符合他们的最佳利益的。来自私人 CA 的证书可以保护内网网站的安全,或允许客户端对服务器进行认证。由于私人 CA 在其组织之外不受信任,因此除了组织成员外,其他人不能安全地使用其证书。

数字证书的事实上的标准格式是 X.509。这一标准的最初版本由国际电联于 1988 年发布。1995 年,IETF 成立了 Public-Key Infrastructure x.509 Working Group(PKIX-WG),以规范互联网上的 PKI 使用,其中包括如何使用 X.509 的指南(记录在 RFC 42101 中)。X.509 的初始版本有许多缺陷,并导致了后续版本的发布,X.509v3 是最新版本。

X.509v3 最显著的变化是对扩展的支持,它们允许证书包含 X.509v1 中定义以外的附加字段。一些扩展在 X.509v3 中因其有用性而被标准化,但非标准的扩展也是可能的。

扩展名由一个名称(字段名称)、一个分配给字段的值和一个表示该扩展名是否关键的布尔标志组成。如果任何关键扩展不被识别,那么处理具有关键扩展的证书的应用程序必须拒绝证书的有效性。不被应用程序识别的非关键扩展可以被忽略。

实践指南

OpenSSL 是一个免费的开源加密库,它提供了处理数字证书的命令行工具。其中一些工具可以用来充当证书颁发机构。

最佳做法是创建一个根 CA 和一个或多个中间 CA。根 CA 的私钥由组织中一位值得信赖的(通常是既得利益者)成员离线保存。该成员可以将签发证书的责任委托给组织中的其他成员,这些成员将充当中间 CA。受信任的员工除了是根 CA 之外,还可以扮演中间 CA 的角色。为每个中间 CA 创建密钥对和证书。中间 CA 的证书用根 CA 的私钥签署。

如果怀疑中间 CA 的私钥被泄露,那么根 CA 可以撤销中间 CA 的证书。这就有效地破坏了证书链,并间接地使所有由泄露的CA签署的证书无效。当代理中间 CA 的个人离开组织时,可以采用同样的撤销程序。这将确保此人无法再代表组织签署任何证书。

环境

系统:Ubuntu 18.04

创建主目录用于存放根证书:

1
2
3
4
5
6
7
8
$ mkdir root_ca
$ cd root_ca/
$ mkdir certs crl newcerts private
$ chmod 700 private/
$ touch index.txt
$ echo 1000 > serial
$ pwd
/home/root_ca

我们创建了四个子目录。当一个证书由根 CA 签发时,它会自动作为一个文件放在 newcerts 目录下。文件名是所发证书的序列号。这并不总是那么实用 —— 例如,有时你想通过所有者的名字来查找证书。因此,我们在 certs 目录下存储了每个由根 CA 发行的证书的副本。crl 目录下将包含有废止证书的列表。最后,私钥存储在私有目录下。私钥在任何时候都必须是保密的。这就是为什么我们设置了目录的权限位,只允许目录的所有者访问它的内容。

OpenSSL 将使用 index.txt 文件来跟踪所有签发的证书。每个证书必须有一个唯一的值,这个值是从序列文件中获得的。OpenSSL 为每一个创建的证书递增这个文件中的值。在第 6 行,我们提供了初始值。OpenSSL 将该文件的内容解释为十六进制值,所以 1000 实际上是 0x1000,也就是十进制的 4096

接着复制默认的配置文件到目录下:

1
cp /etc/ssl/openssl.cnf /home/root_ca/

修改用于创建根证书的配置文件

这里需要修改拷贝过来的配置文件 openssl.cnf

  1. 编辑 [ CA_default ] ,修改几个变量:

    修改路径

    1
    dir         = /home/root_ca

    修改保存的名称,<name><type><format>

    1
    2
    3
    certificate = $dir/certs/root.cert.pem
    crl = $dir/crl/root.crl.pem
    private_key = $dir/private/root.key.pem

    找到 default_md 并确保设置成 sha256 ,默认的 sha1 已经被放弃使用。

    1
    default_md  = sha256

    找到并注释 x509_extensions 变量。因为用于特定目的的扩展都集中在一个共同的部分中。我们将在命令行中选择所需的扩展部分。

  2. 编辑 [ req ] 部分,主要在创建证书或证书签署请求时使用。

    添加变量

    1
    default_md  = sha256
  3. 编辑 [ req_distinguished_name ] 部分,本节定义了建立区分名称(distinguished name, DN)所需的元素,区分名称是唯一识别证书所有者的方法。

    1
    2
    3
    4
    countryName_default = CN
    stateOrProvince_default = Beijing
    localityName_default = Chaoyang
    0.organizationName_default = the masses
  4. 编辑 [ v3_ca ] 部分,本节主要在创建根证书时使用。

    取消注释 keyUsage 变量,并增加值

    1
    keyUsage = critical, digitalSignature, cRLSign, keyCertSign
  5. 新增 [ v3_intermediate_ca ] 部分

    1
    2
    3
    4
    5
    [ v3_intermediate_ca ]
    subjectKeyIdentifier = hash
    authorityKeyIdentifier = keyid:always,issuer
    basicConstraints = critical, CA:true, pathlen:0
    keyUsage = critical, digitalSignature, cRLSign, keyCertSign

    pathlen:0 确保中间 CA 下面不能再有其他证书机构。

  6. 编辑 [ usr_cert ] 部分,该部分被用来签发客户证书。

    取消注释并修改两个变量

    1
    2
    keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
    extendedKeyUsage = clientAuth, emailProtection
  7. 新增 [ server_cert ] 部分,主要用于签发服务器证书。

    1
    2
    3
    4
    5
    6
    [ server_cert ]
    basicConstraints = CA:FALSE
    subjectKeyIdentifier = hash
    authorityKeyIdentifier = keyid,issuer:always
    keyUsage = critical, digitalSignature, keyEncipherment
    extendedKeyUsage = serverAuth

修改用于创建中间人证书的配置文件

为方便起见,在根证书目录下创建中间人证书目录:

1
2
3
4
5
6
7
8
mkdir inter_ca
cd inter_ca/
mkdir certs crl newcerts private csr
chmod 700 private/
touch index.txt
echo 2000 > serial
echo 2000 > crlnumber
cp ../openssl.cnf .

我们创建一个 csr 目录来存储所有传入的证书签名请求(certificate signature requests, CSR)。创建文件 crlnumber 用于跟踪 CRLs 。

如同上文一样,我们需要修改 openssl.cnf 中的路径,文件名等。

启用 x509_extensions 并设置成 usr_cert ,同时设置 policy 的值为 policy_anything

为 root 和中间人创建密钥和证书

使 PKI 正常运行的下一步是分别为 root 和中间人生成密钥对和相应的证书。

为 root 创建密钥

1
2
3
4
5
6
7
8
9
$ cd root_ca/
$ openssl genrsa -aes256 -out private/root.key.pem 4096
Generating RSA private key, 4096 bit long modulus
.......................................................................................................................................................................++
............................................................................................++
e is 65537 (0x010001)
Enter pass phrase for private/root.key.pem: root # 仅作为示例使用,请勿模仿!
Verifying - Enter pass phrase for private/root.key.pem: root
$ chmod 400 private/root.key.pem

如果想要获得公钥,可以使用如下命令:

1
openssl rsa -in private/root.key.pem -pubout -out root.pub.pem

这里我们不这么直接使用,因为公钥将在被嵌入证书后间接使用。

为 root 生成自签名证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ openssl req -config openssl.cnf -key private/root.key.pem -new -x509 -days 7300 -sha256 -extensions v3_ca -out certs/root.cert.pem
Enter pass phrase for private/root.key.pem:
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) [CN]:
State or Province Name (full name) [Beijing]:
Locality Name (eg, city) [Chaoyang]:
Organization Name (eg, company) [the masses]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []: cyqz_Root
Email Address []:
$ chmod 444 certs/root.cert.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$ openssl x509 -noout -text -in certs/root.cert.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
8f:4f:94:a4:7d:21:49:d0
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = " cyqz_Root"
Validity
Not Before: Dec 22 08:48:28 2020 GMT
Not After : Dec 17 08:48:28 2040 GMT
Subject: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = " cyqz_Root"
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:c9:37:6b:a7:b0:b4:a5:2f:83:5e:65:ec:47:aa:
92:9d:a5:20:f3:3a:c1:22:08:b9:a8:1e:eb:2b:49:
2c:89:b5:a0:00:ae:f0:1b:48:2c:b0:24:83:10:fb:
72:82:3b:af:38:c8:7d:46:48:90:17:76:64:ff:ca:
...
5d:fb:6f:4c:fb:65:b8:af:72:98:e8:84:e6:f0:fb:
4b:c7:5e:13:23:7c:5d:10:ff:fb:92:19:d8:09:19:
94:15:da:cb:b7:82:f1:0f:16:f8:45:2a:8b:43:b0:
65:7e:53
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
CE:45:6D:DA:CB:11:46:F4:79:0A:D9:70:22:0A:95:92:49:FB:D1:37
X509v3 Authority Key Identifier:
keyid:CE:45:6D:DA:CB:11:46:F4:79:0A:D9:70:22:0A:95:92:49:FB:D1:37

X509v3 Basic Constraints: critical
CA:TRUE
X509v3 Key Usage: critical
Digital Signature, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
77:5f:fe:43:c8:e3:37:51:68:1a:f8:e7:bd:43:bc:db:97:41:
a3:14:6e:92:7f:e6:90:a1:42:aa:f0:d4:e0:a2:d4:ab:ea:83:
94:5d:00:3b:d9:aa:9b:1d:f7:3b:7a:ae:30:08:1d:12:c0:ec:
d0:3b:af:1d:0d:47:45:d5:58:ed:b5:68:02:7d:db:2b:84:49:
...
16:67:9d:53:d5:88:23:7b:4e:d0:34:b2:38:2e:9a:2f:20:ed:
64:97:ab:79:98:4f:fb:f4:a5:ef:e4:fb:98:66:26:9c:8f:ab:
74:f6:4d:eb:21:e5:6c:89

为中间人创建密钥

1
2
3
4
5
6
7
8
$ openssl genrsa -aes256 -out inter_ca/private/inter.key.pem 4096
Generating RSA private key, 4096 bit long modulus
...............................................................................++
.......................................................................................................................................................................................................................................................................................................................................................++
e is 65537 (0x010001)
Enter pass phrase for inter_ca/private/inter.key.pem: inter # 仅作为示例使用,请勿模仿!
Verifying - Enter pass phrase for inter_ca/private/inter.key.pem: inter
$ chmod 400 inter_ca/private/inter.key.pem

创建 CSR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ openssl req -config inter_ca/openssl.cnf -new -sha256 -key inter_ca/private/inter.key.pem -out inter_ca/csr/inter.csr.pem
Enter pass phrase for inter_ca/private/inter.key.pem:
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) [CN]:
State or Province Name (full name) [Beijing]:
Locality Name (eg, city) [Chaoyang]:
Organization Name (eg, company) [the masses]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:cyqz_Inter
Email Address []:

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

验证 CSR

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
$ openssl req -text -noout -verify -in inter_ca/csr/inter.csr.pem
verify OK
Certificate Request:
Data:
Version: 1 (0x0)
Subject: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = cyqz_Inter
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:b2:80:b6:b8:c5:d6:6a:37:56:da:40:c1:86:80:
6d:cb:b4:01:a3:8d:9d:3e:1d:81:59:74:4c:e4:dd:
39:10:94:f6:e9:f8:8e:fc:96:2b:18:fd:6a:54:23:
70:80:8f:20:f3:f7:ce:c5:a1:58:43:de:af:80:36:
...
a1:fa:e2:fb:cf:30:04:69:88:a5:3a:63:5d:65:bd:
03:ba:c8:54:8b:12:f6:3e:6f:36:fe:e8:e7:e3:6a:
a8:8d:b6:74:ea:2c:47:9f:5b:2e:19:8a:1b:7f:22:
83:c5:3d
Exponent: 65537 (0x10001)
Attributes:
a0:00
Signature Algorithm: sha256WithRSAEncryption
23:31:74:a6:3e:a4:87:82:c3:4e:7f:2d:50:bd:44:e3:1f:b7:
ae:2c:02:58:c8:b5:73:e3:0c:6e:c1:06:89:90:c2:42:42:2a:
f5:61:9c:7f:bf:6e:58:1b:4b:02:37:2c:ce:ac:e1:36:a4:09:
c8:47:43:ed:1f:81:04:e5:63:da:64:35:2a:20:0d:24:fa:48:
...
83:b5:53:93:0f:97:27:bf:69:e8:9a:2d:92:6e:9e:d5:5b:99:
ab:d7:d9:e6:57:77:9b:ba:3f:ae:c0:df:02:11:b2:00:50:1d:
8e:49:9f:5b:c6:b4:83:f4:84:b8:9b:c8:64:18:8f:02:06:84:
98:4c:8d:c9:4b:d1:0a:61

创建中间人证书

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
$ openssl ca -config openssl.cnf -extensions v3_intermediate_ca -days 3650 -notext -md sha256 -in inter_ca/csr/inter.csr.pem -out inter_ca/certs/inter.cert.pem
Using configuration from openssl.cnf
Enter pass phrase for /root_ca/private/root.key.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 4096 (0x1000)
Validity
Not Before: Dec 22 11:49:10 2020 GMT
Not After : Dec 20 11:49:10 2030 GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
organizationName = the masses
commonName = cyqz_Inter
X509v3 extensions:
X509v3 Subject Key Identifier:
4E:ED:71:D7:0E:F5:67:42:78:57:6B:77:DB:D1:0F:69:65:81:E2:40
X509v3 Authority Key Identifier:
keyid:CE:45:6D:DA:CB:11:46:F4:79:0A:D9:70:22:0A:95:92:49:FB:D1:37

X509v3 Basic Constraints: critical
CA:TRUE, pathlen:0
X509v3 Key Usage: critical
Digital Signature, Certificate Sign, CRL Sign
Certificate is to be certified until Dec 20 11:49:10 2030 GMT (3650 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
$ chmod 444 inter_ca/certs/inter.cert.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$ openssl x509 -noout -text -in inter_ca/certs/inter.cert.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 4096 (0x1000)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = " cyqz_Root"
Validity
Not Before: Dec 22 11:49:10 2020 GMT
Not After : Dec 20 11:49:10 2030 GMT
Subject: C = CN, ST = Beijing, O = the masses, CN = cyqz_Inter
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:b2:80:b6:b8:c5:d6:6a:37:56:da:40:c1:86:80:
6d:cb:b4:01:a3:8d:9d:3e:1d:81:59:74:4c:e4:dd:
39:10:94:f6:e9:f8:8e:fc:96:2b:18:fd:6a:54:23:
70:80:8f:20:f3:f7:ce:c5:a1:58:43:de:af:80:36:
...
11:b4:43:5d:da:30:b0:b9:47:2f:8d:cf:d3:7e:f6:
a1:fa:e2:fb:cf:30:04:69:88:a5:3a:63:5d:65:bd:
03:ba:c8:54:8b:12:f6:3e:6f:36:fe:e8:e7:e3:6a:
a8:8d:b6:74:ea:2c:47:9f:5b:2e:19:8a:1b:7f:22:
83:c5:3d
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
4E:ED:71:D7:0E:F5:67:42:78:57:6B:77:DB:D1:0F:69:65:81:E2:40
X509v3 Authority Key Identifier:
keyid:CE:45:6D:DA:CB:11:46:F4:79:0A:D9:70:22:0A:95:92:49:FB:D1:37

X509v3 Basic Constraints: critical
CA:TRUE, pathlen:0
X509v3 Key Usage: critical
Digital Signature, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
9d:6d:32:00:ab:1f:cd:b0:df:5d:fd:43:80:b4:0c:83:91:66:
7b:b7:f2:af:da:9a:b5:f7:3a:40:f0:ea:1c:b6:f1:05:a0:c9:
fa:29:57:75:d0:cb:fe:c3:e3:00:30:e5:bd:3c:da:c6:e0:09:
03:90:ed:d3:79:ee:86:bd:7c:7e:c0:b5:98:13:40:2e:d9:92:
...
f6:fd:a1:e5:da:11:53:e0:e5:14:ba:95:15:a8:3a:e1:6d:ec:
37:1c:94:ae:7d:97:e4:52:56:40:23:cf:cb:36:89:24:56:29:
f3:2e:be:5a:0d:6b:64:ea:64:f0:3a:de:3e:cb:f4:96:fd:ce:
a8:ce:02:24:b6:b3:64:90:e3:57:5a:47:a2:59:3b:da:59:31:
a4:1e:be:26:d4:c7:c6:f5

将中间证书与根证书进行核对。

1
2
$ openssl verify -CAfile certs/root.cert.pem inter_ca/certs/inter.cert.pem
inter_ca/certs/inter.cert.pem: OK

创建证书链

当应用程序(例如 Web 浏览器)尝试验证由中间 CA 签名的证书时,它还必须对照根证书验证中间证书。 要完成信任链,请创建一个 CA 证书链以呈现给应用程序。

1
2
cat inter_ca/certs/inter.cert.pem certs/root.cert.pem > inter_ca/certs/ca-chain.cert.pem
chmod 444 inter_ca/certs/ca-chain.cert.pem

创建服务器证书

使用中间 CA 来为服务器签署证书。

创建私钥

如果你正在创建一个用于 Web 服务器的加密对(例如,Apache),你需要在每次重新启动 Web 服务器时输入这个密码。你可能想省略 -aes256 选项来创建一个没有密码的密钥,虽然这样会导致安全性降低。

1
2
3
4
5
$ openssl genrsa -out inter_ca/private/server.key.pem 2048
Generating RSA private key, 2048 bit long modulus
.......................................................................................+++
....................................................................+++
e is 65537 (0x010001)

创建 CSR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ openssl req -config inter_ca/openssl.cnf -key inter_ca/private/server.key.pem -new -sha256 -out inter_ca/csr/server.csr.pem
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) [CN]:
State or Province Name (full name) [Beijing]:
Locality Name (eg, city) [Chaoyang]:
Organization Name (eg, company) [the masses]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:

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

验证 CSR

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
$ openssl req -text -noout -verify -in inter_ca/csr/server.csr.pem
verify OK
Certificate Request:
Data:
Version: 1 (0x0)
Subject: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = localhost
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:bc:da:6d:7b:73:e1:bf:1a:8e:b2:32:d2:39:7f:
e4:67:9e:a3:3c:f8:38:d9:45:13:75:26:22:c5:54:
ae:9e:76:7a:f7:1a:91:c3:01:49:bc:8a:b3:8a:d6:
6f:9c:f4:b7:0a:65:ac:cf:fa:f9:8d:07:2b:18:e4:
...
a0:cd:70:48:3e:11:e9:26:14:11:91:4c:36:5a:c6:
ed:e4:19:14:77:32:e5:1b:64:a7:dc:8b:3f:3b:94:
5f:ed:db:50:32:c8:2d:25:6b:bc:67:af:37:2e:80:
80:77:df:04:66:e1:f6:8a:19:a1:94:37:ab:73:a1:
57:73
Exponent: 65537 (0x10001)
Attributes:
a0:00
Signature Algorithm: sha256WithRSAEncryption
b9:12:34:af:a6:46:38:e7:2b:3f:01:79:d6:8c:20:3b:77:b8:
76:d2:1f:d3:5c:6b:b6:c0:15:f0:2d:90:30:80:46:a6:ae:69:
f8:21:f1:20:a8:c7:b7:52:73:22:0e:b3:cf:52:ba:2e:a5:02:
f6:72:e0:58:cc:28:36:ff:cf:9b:7e:49:6c:8d:06:da:90:05:
...
a6:d4:4c:74:2b:8a:67:19:f0:8e:bc:b8:3d:3a:4b:75:00:36:
48:cd:9e:c1:a6:70:1e:b1:ec:b7:d1:bf:79:8a:88:fa:79:a3:
58:cd:9e:91

创建证书

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
$ openssl ca -config inter_ca/openssl.cnf -extensions server_cert -days 375 -notext -md sha256 -in inter_ca/csr/server.csr.pem -out inter_ca/certs/server.cert.pem
Using configuration from inter_ca/openssl.cnf
Enter pass phrase for /home/root_ca/inter_ca/private/inter.key.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 8192 (0x2000)
Validity
Not Before: Dec 22 12:17:44 2020 GMT
Not After : Jan 1 12:17:44 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
localityName = Chaoyang
organizationName = the masses
commonName = localhost
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Subject Key Identifier:
6B:F6:40:0A:6D:93:7C:A2:23:19:E3:6A:7B:DF:DF:6D:5F:63:49:EF
X509v3 Authority Key Identifier:
keyid:4E:ED:71:D7:0E:F5:67:42:78:57:6B:77:DB:D1:0F:69:65:81:E2:40
DirName:/C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN= cyqz_Root
serial:10:00

X509v3 Key Usage: critical
Digital Signature, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Server Authentication
Certificate is to be certified until Jan 1 12:17:44 2022 GMT (375 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

验证证书

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
$ openssl x509 -noout -text -in inter_ca/certs/server.cert.pem
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 8192 (0x2000)
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = Beijing, O = the masses, CN = cyqz_Inter
Validity
Not Before: Dec 22 12:17:44 2020 GMT
Not After : Jan 1 12:17:44 2022 GMT
Subject: C = CN, ST = Beijing, L = Chaoyang, O = the masses, CN = localhost
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
00:bc:da:6d:7b:73:e1:bf:1a:8e:b2:32:d2:39:7f:
e4:67:9e:a3:3c:f8:38:d9:45:13:75:26:22:c5:54:
ae:9e:76:7a:f7:1a:91:c3:01:49:bc:8a:b3:8a:d6:
6f:9c:f4:b7:0a:65:ac:cf:fa:f9:8d:07:2b:18:e4:
...
ed:e4:19:14:77:32:e5:1b:64:a7:dc:8b:3f:3b:94:
5f:ed:db:50:32:c8:2d:25:6b:bc:67:af:37:2e:80:
80:77:df:04:66:e1:f6:8a:19:a1:94:37:ab:73:a1:
57:73
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Subject Key Identifier:
6B:F6:40:0A:6D:93:7C:A2:23:19:E3:6A:7B:DF:DF:6D:5F:63:49:EF
X509v3 Authority Key Identifier:
keyid:4E:ED:71:D7:0E:F5:67:42:78:57:6B:77:DB:D1:0F:69:65:81:E2:40
DirName:/C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN= cyqz_Root
serial:10:00

X509v3 Key Usage: critical
Digital Signature, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Server Authentication
Signature Algorithm: sha256WithRSAEncryption
1c:4b:7b:16:c5:32:79:8c:f1:c4:1c:68:8d:17:02:7d:b6:f9:
ba:22:ca:f9:17:40:2d:3a:82:c7:ae:74:96:12:5b:9c:f8:0d:
03:c5:e7:fc:55:46:6c:92:f4:3f:91:24:66:ad:be:fb:73:d7:
d4:dc:fa:35:c5:d3:4d:97:7c:bb:45:94:24:d5:62:de:46:c4:
29:dd:9d:7b:90:ac:5b:29:ef:00:b8:58:e2:2e:fa:e8:27:d8:
...
70:56:96:fe:b3:6d:4b:2d:d6:6c:23:76:b4:fa:1b:7d:aa:e3:
41:15:91:cc:0c:f1:d0:05:39:11:ee:c7:f3:70:fc:91:35:1f:
97:bb:2d:59:2a:68:45:13:12:9a:a3:ce:01:f1:66:dc:f3:f6:
c2:2d:2b:cc:fd:5c:88:04:f1:ac:f3:07:52:09:55:d6:ac:f3:
e2:d1:33:d0:26:3f:40:7d
1
2
$ openssl verify -CAfile inter_ca/certs/ca-chain.cert.pem inter_ca/certs/server.cert.pem
inter_ca/certs/server.cert.pem: OK

使用 CRL 吊销证书

编辑 /root_ca/inter_ca/openssl.cnf ,并在 server_cert 部分添加:

1
crlDistributionPoints = URI:https://localhost/inter.crl.pem

创建 CRL

1
2
3
$ openssl ca -config inter_ca/openssl.cnf -gencrl -out inter_ca/crl/inter.crl.pem
Using configuration from inter_ca/openssl.cnf
Enter pass phrase for /home/root_ca/inter_ca/private/inter.key.pem:

检查内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ openssl crl -in inter_ca/crl/inter.crl.pem -noout -text
Certificate Revocation List (CRL):
Version 2 (0x1)
Signature Algorithm: sha256WithRSAEncryption
Issuer: /C=CN/ST=Beijing/O=the masses/CN=cyqz_Inter
Last Update: Dec 22 12:37:35 2020 GMT
Next Update: Jan 21 12:37:35 2021 GMT
CRL extensions:
X509v3 CRL Number:
8192
No Revoked Certificates.
Signature Algorithm: sha256WithRSAEncryption
55:78:44:16:c4:a6:15:f4:71:ad:e9:0b:9f:9f:72:a0:2a:8f:
96:67:8a:c2:7e:f4:49:ab:9f:34:73:b7:13:04:e0:b4:b0:60:
50:07:16:e3:d5:d9:61:cc:70:2f:6f:3d:91:fa:52:9b:96:96:
2d:93:7b:85:f4:4c:a0:b1:16:e1:a9:02:67:1d:0c:de:eb:44:
...
35:4a:3b:a7:2a:59:c6:78:99:5e:f3:54:9f:8a:af:2b:da:38:
a1:5a:3f:2c:65:86:56:02:3c:5e:f9:c1:f8:12:8d:41:d0:d8:
a3:2b:52:af:00:f2:52:93

此时还没有证书被吊销,接下来就创建一个证书并吊销它。

吊销证书

创建私钥

1
2
3
4
5
$ openssl genrsa -out private/jack@jack.com.key.pem 2048
Generating RSA private key, 2048 bit long modulus
........................................................+++
.....................................+++
e is 65537 (0x010001)

创建 CSR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ mkdir csr
$ openssl req -config inter_ca/openssl.cnf -key private/jack@jack.com.key.pem -new -sha256 -out csr/jack@jack.com.csr.pem
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) [CN]:
State or Province Name (full name) [Beijing]:
Locality Name (eg, city) [Chaoyang]:
Organization Name (eg, company) [the masses]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:jack@jack.com
Email Address []:

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

创建证书

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
$ openssl ca -config inter_ca/openssl.cnf -extensions usr_cert -days 375 -notext -md sha256 -in csr/jack@jack.com.csr.pem -out certs/jack@jack.com.cert.pem
Using configuration from inter_ca/openssl.cnf
Enter pass phrase for /home/root_ca/inter_ca/private/inter.key.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 8193 (0x2001)
Validity
Not Before: Dec 22 12:49:47 2020 GMT
Not After : Jan 1 12:49:47 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
localityName = Chaoyang
organizationName = the masses
commonName = jack@jack.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage: critical
Digital Signature, Non Repudiation, Key Encipherment
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
F9:43:1B:77:29:B4:5D:E3:69:C8:7F:09:71:94:CE:C8:91:24:6F:F9
X509v3 Authority Key Identifier:
keyid:4E:ED:71:D7:0E:F5:67:42:78:57:6B:77:DB:D1:0F:69:65:81:E2:40

X509v3 Extended Key Usage:
TLS Web Client Authentication, E-mail Protection
Certificate is to be certified until Jan 1 12:49:47 2022 GMT (375 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

验证证书

1
2
$ openssl verify -CAfile inter_ca/certs/ca-chain.cert.pem certs/jack@jack.com.cert.pem
certs/jack@jack.com.cert.pem: OK

查看 index.txt

1
2
3
$ cat inter_ca/index.txt
V 220101121744Z 2000 unknown /C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN=localhost
V 220101124947Z 2001 unknown /C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN=jack@jack.com

吊销证书

1
2
3
4
5
$ openssl ca -config inter_ca/openssl.cnf -revoke certs/jack@jack.com.cert.pem
Using configuration from inter_ca/openssl.cnf
Enter pass phrase for /home/root_ca/inter_ca/private/inter.key.pem:
Revoking Certificate 2001.
Data Base Updated

再次查看 index.txt

1
2
3
$ cat inter_ca/index.txt
V 220101121744Z 2000 unknown /C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN=localhost
R 220101124947Z 201222125325Z 2001 unknown /C=CN/ST=Beijing/L=Chaoyang/O=the masses/CN=jack@jack.com

此时证书已经被吊销

重新创建 CRL

1
2
3
$ openssl ca -config inter_ca/openssl.cnf -gencrl -out inter_ca/crl/inter.crl.pem
Using configuration from inter_ca/openssl.cnf
Enter pass phrase for /home/root_ca/inter_ca/private/inter.key.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
$ openssl crl -in inter_ca/crl/inter.crl.pem -noout -text
Certificate Revocation List (CRL):
Version 2 (0x1)
Signature Algorithm: sha256WithRSAEncryption
Issuer: /C=CN/ST=Beijing/O=the masses/CN=cyqz_Inter
Last Update: Dec 22 12:55:52 2020 GMT
Next Update: Jan 21 12:55:52 2021 GMT
CRL extensions:
X509v3 CRL Number:
8193
Revoked Certificates:
Serial Number: 2001
Revocation Date: Dec 22 12:53:25 2020 GMT
Signature Algorithm: sha256WithRSAEncryption
19:ae:6a:a5:fb:dd:7e:52:cf:d0:f3:73:99:9e:8c:a6:ff:33:
f0:fe:f7:12:49:03:58:27:26:69:1a:27:a1:c6:10:f4:50:29:
f7:52:62:d3:e0:c9:ca:b4:1b:ce:28:98:6e:25:d6:0e:d7:d8:
99:86:7a:21:0a:61:04:e6:9a:21:00:a6:cf:4f:41:8d:12:a3:
ce:2a:b6:6e:8b:65:29:d4:34:be:17:45:77:ff:5d:f3:1e:78:
...
dd:f5:5a:06:ab:2f:bc:62:29:49:12:65:24:a1:79:67:ac:82:
11:34:52:73:78:c6:1b:aa:ff:da:a4:9a:88:06:7f:52:f8:90:
88:32:a9:08:64:5d:68:65:9e:64:fd:56:d8:2d:0e:cb:4a:ef:
58:16:77:17:cb:2c:15:cf:90:01:ac:60:87:bd:2f:7b:f1:37:
4c:33:41:0a:8a:c2:40:b0

吊销的证书会出现说明。

参考