网上关于数字证书的资料已经非常多了,本篇只做记录。方便以后复习
CA又称为证书颁发机构,主要用于颁发数字证书,用一个无法篡改的数字证书来表明身份,防止数据在通讯过程中被篡改和窥探,数字证书通常分为以下几种:
- 个人身份证书。
- 企业或机构身份证书。
- 支付网关证书。
- 服务器证书。
- 企业或机构代码签名证书。
- 安全电子邮件证书。
- 个人代码签名证书。
数字证书的生成
@startuml
actor 申请者
actor CA
申请者 -> CA : 发送自己的各种身份信息,营业执照,域名,到期时间
CA -> CA : 生成一对公钥私钥给申请者。
note right:审核申请者信息
CA -> CA : 生成证书
note right
证书包含CA的签发人、地址、签发时间、过期失效时间等信息,
以及申请者的基本信息DNS、域名、公钥等基本信息整合到一起
end note
CA -> CA : 生成摘要
CA -> CA : 签名证书
note right
把信息摘要附加到生成的证书之中。
end note
CA -> 申请者 : 证书颁发
@enduml
数字证书的验证
@startuml
actor 客户端
actor 服务器
客户端 -> 服务器 : 发起请求
服务器 -> 客户端 : 数字证书
客户端 -> 客户端 : 解析证书
note left
获取到元数据和被私钥加密过的摘要密文
end note
客户端 -> 客户端 : 用CA公钥解密,获取摘要
note left
CA的公钥是公开的,任何人都可以获取(会内置在操作系统里面,
或者在CA的官方网站中可以获取),解密后拿到摘要的原文。
end note
客户端 -> 客户端 : 对比摘要,查看信息是否被篡改。
note left
对元数据再次执行摘要算法,拿到摘要信息,然后和第三步的结果进行对比,
只有当结果一致的时候,才认为证书是有效的。
end note
@enduml
问题来了,那谁来保证CA 证书的有效性?
证书链
通常在一个证书链中包含以下三种结构:
- end-user。终端用户,也就是https中真正用来加密通信的证书。
- intermediates。给end-user签发证书的CA的证书,主要用来校验end-user的证书是否合法的证书。
- root。root也是CA证书,区别在于,root证书是给intermediates签发证书的,用来校验intermediates的合法性。
在safari 打开github的官网,点击小🔒 。可以看到上面提到的三种类型的证书。
证书链的签发
- 首先,root自己先生成一对公钥和私钥,然后用自己的私钥给自己自签名,因为root的绝对信任。
- 二级CA向root申请证书,root按照上面提到的数字证书的生成方式,先给CA生成一对公钥和私钥,然后
- 把CA的各种信息算出摘要,再用自己的私钥加密,加上给二级CA生成的公钥,就组成了一张CA的证书。
*然后有用户向二级CA申请证书时,按照这个步骤一步步签发,就形成了证书链。
证书一级一级签发,中间无法伪造,因为root证书的绝对安全,保证了整个证书链的安全。 - 证书链是有长度限制的,root颁发证书的时候会添加此字段,所以证书申请者无法再为别人签发证书。
一般来说,root CAs不会直接颁布end-user certificates的,而是授权给多个二级CA,而二级CA又可以授权给多个三级CA,这些中间的CA就是intermediates CAs,它们才会颁布end-user certificates。如果某个intermediates的私钥被窃取了,那么用root的私钥再签发一个intermediates证书即可,不会威胁到整个root证书下所有人的安全。
那么最终的root 证书的安全性就是最重要的!一般来说,root证书都会被隔离,被保护起来的。如果这也泄漏了,那就没办法了。
root 证书的获取
- 首先,成为一个CA的一个前提是你要向微软等操作系统厂商申请加入他们的白名单,就是把自己的root证书内置在操作系统里面,随着系统的安装和升级的过程就已经被安装到操作系统里面。
- 浏览器等终端在验证的时候,就直接调用操作系统的接口,获取证书,用来验证,例如chrome和ie。
*有些浏览器类似firefox有一套自己的证书系统,是随着安装firefox的时候,安装到本地,这样,即使操作系统的证书被篡改了,firefox依然有能力验证证书的有效性。
root证书并不会很多,目前主流的基本就只有:Symantec(VeriSign/GeoTrust)、Comodo、GoDaddy。
现在才知道,整个互联网都建立在对CA信任的基础上,如果CA机构不再可信。那么。。。。
使用openssl 查看证书
➜ ~ openssl s_client -connect www.baidu.com:443
CONNECTED(00000003)
depth=2 /C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
verify error:num=20:unable to get local issuer certificate
verify return:0
/*
其中0、1、2是证书链中每一级证书的序号。0是要被验证的网站所用的证书。其CN应该对应网站域名。
每一个序号后面,s开头的一行是指证书,i开头的一行是指此证书由谁签发。
*/
---
Certificate chain
0 s:/C=CN/ST=beijing/L=beijing/O=BeiJing Baidu Netcom Science Technology Co., Ltd/OU=service operation department./CN=baidu.com
i:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
1 s:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
2 s:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
i:/C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIHOjCCBiKgAwIBAgIPAaAMWZKgoUJvdRQzvVy/MA0GCSqGSIb3DQEBCwUAMH4x
CzAJBgNVBAYTAlVTMR0wGwYDVQQKExRTeW1hbnRlYyBDb3Jwb3JhdGlvbjEfMB0G
A1UECxMWU3ltYW50ZWMgVHJ1c3QgTmV0d29yazEvMC0GA1UEAxMmU3ltYW50ZWMg
Q2xhc3MgMyBTZWN1cmUgU2VydmVyIENBIC0gRzQwHhcNMTYwODE1MDAwMDAwWhcN
MTcwODE2MjM1OTU5WjCBqDELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB2JlaWppbmcx
EDAOBgNVBAcMB2JlaWppbmcxOTA3BgNVBAoMMEJlaUppbmcgQmFpZHUgTmV0Y29t
IFNjaWVuY2UgVGVjaG5vbG9neSBDby4sIEx0ZDEmMCQGA1UECwwdc2VydmljZSBv
cGVyYXRpb24gZGVwYXJ0bWVudC4xEjAQBgNVBAMMCWJhaWR1LmNvbTCCASIwDQYJ
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAMsV6Ty0lfOjGN04Pmd8m0OuU0aWrFnm
KqsvkcxEtFfT9lgyhrZe3Tbyq7xjMsNluuJFeRlvxgszGgS/LNxfpkaY6sg67R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kLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3Nz
LnN5bWNiLmNvbS9zcy5jcnQwggEFBgorBgEEAdZ5AgQCBIH2BIHzAPEAdwDd6x0r
eg1PpiCLga2BaHB+Lo6dAdVciI09EcTNtuy+zAAAAVaQfkc6AAAEAwBIMEYCIQCT
LTzo3DYr1v3qgw30B/37GzgST+iXqZtWj1FjVrawzQIhAMxUeruo6bLLmlqsE4hW
YcDOu8hivJK4TefT1NDhb07DAHYApLkJkLQYWBSHuxOizGdwCjw1mAT5G9+443fN
DsgN3BAAAAFWkH5HWgAABAMARzBFAiAfRCG9WGKLQhLcPg5Dv+8MqUslHec5awM8
ahTB4Y0ddwIhAIzuy0JBUsTmk+Eg5ArobPqgXYFc1llXRceY4uTZiASZMA0GCSqG
SIb3DQEBCwUAA4IBAQBBLDatqFCdrwIrDoenQ7eCPpp1H0G9BvHI+5JAXQiI6Und
iSkaq7A9WvgAsVGNw1wGZIjysBQaf2VhJpbSFZwz9D9t3pmTAApVSY0uUvqiFg7X
NqDoqu3iyBBNw5hGzP/AzU2RoT1Sd7bf57xBEW5W1nxogMSE1ACNf/8WZwixZk5t
5RcW6zvGQiJ8+3OA+NA8tyUcGj1mhDBb7Z4yjbn8cily5DA34nXpiYXnHGic5TLn
QkaFIbZ9z7GqroE8oLrTUhOy+ZFNsrwanNO+gqf6c59V+1hQzRvkKN24JzdWjwHx
jsl/tY61DHvNFsmc683ggLjI+ZWMya1UcDmxADhN
-----END CERTIFICATE-----
subject=/C=CN/ST=beijing/L=beijing/O=BeiJing Baidu Netcom Science Technology Co., Ltd/OU=service operation department./CN=baidu.com
issuer=/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
---
No client certificate CA names sent
---
SSL handshake has read 4605 bytes and written 456 bytes
---
New, TLSv1/SSLv3, Cipher is AES128-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : AES128-SHA
Session-ID: 3FD71082386636BAE52F881DC52AD33949DC262AB285FCCDA1B31054F52C19A2
Session-ID-ctx:
Master-Key: 4C08A4E6C3A1B2238014B82F855219D46B320075D3E7B2B64F2FD7B9220D646F7141FFB3621DB9CA0D930E0C2464197B
Key-Arg : None
Start Time: 1499916305
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
closed
把从-----BEGIN CERTIFICATE----- 到 -----END CERTIFICATE----- 的内容复制保存到本地:cert.crt 。然后用
openssl x509 -noout -text -in cert.crt
可以查看证书的内容。
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
01:a0:0c:59:92:a0:a1:42:6f:75:14:33:bd:5c:bf
Signature Algorithm: sha256WithRSAEncryption
Issuer: C=US, O=Symantec Corporation, OU=Symantec Trust Network, CN=Symantec Class 3 Secure Server CA - G4
Validity
Not Before: Aug 15 00:00:00 2016 GMT
Not After : Aug 16 23:59:59 2017 GMT
Subject: C=CN, ST=beijing, L=beijing, O=BeiJing Baidu Netcom Science Technology Co., Ltd, OU=service operation department., CN=baidu.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)
Modulus (2048 bit):
00:cb:15:e9:3c:b4:95:f3:a3:18:dd:38:3e:67:7c:
9b:43:ae:53:46:96:ac:59:e6:2a:ab:2f:91:cc:44:
b4:57:d3:f6:58:32:86:b6:5e:dd:36:f2:ab:bc:63:
32:c3:65:ba:e2:45:79:19:6f:c6:0b:33:1a:04:bf:
2c:dc:5f:a6:46:98:ea:c8:3a:ed:1f:ed:bd:ac:41:
f0:b9:ae:20:99:fd:7b:b4:48:dd:d6:54:a3:26:43:
4c:11:56:4e:8b:d5:b1:da:0e:96:71:74:d1:84:51:
28:df:6d:6c:aa:35:ac:ae:2b:ce:ec:7e:82:a4:6d:
7f:81:ec:93:e9:a3:4c:18:4f:91:8d:09:ff:8a:a8:
71:41:9e:19:8c:4f:8c:79:4f:20:90:01:04:de:54:
b6:d5:2f:dc:17:b5:d1:f8:54:cc:fd:4e:1c:7c:a3:
fa:26:2a:2b:f3:ec:30:8a:3e:3e:86:5c:95:bd:28:
c7:52:b8:56:f8:37:73:38:f2:f0:10:05:ef:ab:30:
fa:6a:ab:ff:0f:ef:d7:a5:9e:9a:31:40:0f:22:2a:
a1:45:ee:05:0c:a1:a7:78:78:16:68:26:89:74:91:
25:e2:7c:8a:4f:6d:cf:38:dd:21:bd:0a:86:b4:5b:
76:34:09:c8:0e:29:0e:e8:d8:2a:4d:a0:fb:81:ed:
d9:1d
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Alternative Name:
DNS:*.baidu.com, DNS:*.baifubao.com, DNS:*.bdstatic.com, DNS:*.hao123.com, DNS:*.nuomi.com, DNS:*.bce.baidu.com, DNS:*.eyun.baidu.com, DNS:*.map.baidu.com, DNS:baidu.com, DNS:baifubao.com, DNS:www.baidu.cn, DNS:www.baidu.com.cn, DNS:click.hm.baidu.com, DNS:log.hm.baidu.com, DNS:cm.pos.baidu.com, DNS:wn.pos.baidu.com, DNS:update.pan.baidu.com, DNS:mct.y.nuomi.com
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage: critical
Digital Signature, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Server Authentication, TLS Web Client Authentication
X509v3 Certificate Policies:
Policy: 2.23.140.1.2.2
CPS: https://d.symcb.com/cps
User Notice:
Explicit Text: https://d.symcb.com/rpa
X509v3 Authority Key Identifier:
keyid:5F:60:CF:61:90:55:DF:84:43:14:8A:60:2A:B2:F5:7A:F4:43:18:EF
X509v3 CRL Distribution Points:
URI:http://ss.symcb.com/ss.crl
Authority Information Access:
OCSP - URI:http://ss.symcd.com
CA Issuers - URI:http://ss.symcb.com/ss.crt
1.3.6.1.4.1.11129.2.4.2:
.....8.O....V.QcV....!..Tz......Z...Va....b...M.....oN..v.......X......gp
.....V.~GZ.....G0E. .D!.Xb.B..>.C....K%..9k.<j.....w.!....BAR.... .
.l..].\.YWE........
Signature Algorithm: sha256WithRSAEncryption
41:2c:36:ad:a8:50:9d:af:02:2b:0e:87:a7:43:b7:82:3e:9a:
75:1f:41:bd:06:f1:c8:fb:92:40:5d:08:88:e9:49:dd:89:29:
1a:ab:b0:3d:5a:f8:00:b1:51:8d:c3:5c:06:64:88:f2:b0:14:
1a:7f:65:61:26:96:d2:15:9c:33:f4:3f:6d:de:99:93:00:0a:
55:49:8d:2e:52:fa:a2:16:0e:d7:36:a0:e8:aa:ed:e2:c8:10:
4d:c3:98:46:cc:ff:c0:cd:4d:91:a1:3d:52:77:b6:df:e7:bc:
41:11:6e:56:d6:7c:68:80:c4:84:d4:00:8d:7f:ff:16:67:08:
b1:66:4e:6d:e5:17:16:eb:3b:c6:42:22:7c:fb:73:80:f8:d0:
3c:b7:25:1c:1a:3d:66:84:30:5b:ed:9e:32:8d:b9:fc:72:29:
72:e4:30:37:e2:75:e9:89:85:e7:1c:68:9c:e5:32:e7:42:46:
85:21:b6:7d:cf:b1:aa:ae:81:3c:a0:ba:d3:52:13:b2:f9:91:
4d:b2:bc:1a:9c:d3:be:82:a7:fa:73:9f:55:fb:58:50:cd:1b:
e4:28:dd:b8:27:37:56:8f:01:f1:8e:c9:7f:b5:8e:b5:0c:7b:
cd:16:c9:9c:eb:cd:e0:80:b8:c8:f9:95:8c:c9:ad:54:70:39:
b1:00:38:4d
FYI:
PlantUML 真好用!