认证机构(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 在其组织之外不受信任,因此除了组织成员外,其他人不能安全地使用其证书。
最佳做法是创建一个根 CA 和一个或多个中间 CA。根 CA 的私钥由组织中一位值得信赖的(通常是既得利益者)成员离线保存。该成员可以将签发证书的责任委托给组织中的其他成员,这些成员将充当中间 CA。受信任的员工除了是根 CA 之外,还可以扮演中间 CA 的角色。为每个中间 CA 创建密钥对和证书。中间 CA 的证书用根 CA 的私钥签署。
如果怀疑中间 CA 的私钥被泄露,那么根 CA 可以撤销中间 CA 的证书。这就有效地破坏了证书链,并间接地使所有由泄露的CA签署的证书无效。当代理中间 CA 的个人离开组织时,可以采用同样的撤销程序。这将确保此人无法再代表组织签署任何证书。
$ 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
$ 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
$ 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
$ 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 []:
$ 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
如果你正在创建一个用于 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)
$ 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 []:
$ 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
$ 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:
$ 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)
$ 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 []:
$ 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: