协议相关
https://tls.ulfheim.net/
展示了TLS1.2的流程。具体的代码在https://github.com/syncsynchalt/illustrated-tls
首先client和server都生成一串随机值,并让对方知晓
- client random
- server random
然后client和server各生成一套临时的公钥私钥对,只把公钥让对方知晓
- client public key
- server public key
这样client根据下面的参数计算出一个加密密钥:
- client random
- server random
- server public key
- client private key (只有client知晓)
然后server根据下面的餐宿计算出一个加密密钥,这个密钥与client算出的应该相同
- server random
- client random
- client public key
- server private key (只有server知晓)
TLS 1.3的流程看https://tls13.ulfheim.net/。
TLS 1.3更早进入加密传输。TLS 1.3可以模拟部分1.2的消息,用以欺骗中间节点。TLS 1.3的服务端可以发放Ticket,以便下次可以快速建立连接。
参考
- Does https prevent man in the middle attacks by proxy server?
- https://github.com/avaiyang/TLS-MITM-attack
- How does SSL/TLS work?
- TLS Handshake : Under The Hood
What is mutual TLS (mTLS)?
传统web应用只验证服务端,而mTLS需要验证客户端。
参考
- https://en.wikipedia.org/wiki/Mutual_authentication
- https://freedomben.medium.com/what-is-mtls-and-how-does-it-work-9dcdbf6c1e41
证书相关
X.509 数字证书的基本原理及应用
非对称加密有公钥和私钥之分,常用算法有RSA和DSA等。
摘要算法用于展示数据特征,常用算法有MD5,SHA-1,SHA-256等。
签名是指把摘要通过非对称加密得出的一个密文。重复同样过程若能得到同样结果,则签名得到验证。
X.509证书结构
- 版本号 (1, 2, 3)
- 序列号
- 算法
- 参数 (加上以上两项用于签名验证)
- 发行者名称
- 起始时间
- 终止时间 (证书的发行者信息)
- 受用者名称
- 算法
- 参数
- 公开密钥(证书的所有者信息)(第一版参数到此为止)
- 发行者唯一识别符
- 受用者唯一识别符 (第二版参数到此位置)
- 扩充与(第三版参数到此为止)
- 算法
- 参数
- 签名 (此三项未所有版本共有)
认证是一环套一环的,最上的一环成为根认证机构,必须通过其他方式被大家所熟知。
根认证机构生成:
- ca_KeyPub, ca_keyPri, ca_Info
- Hash(ca_KeyPub + ca_info) = ca_Hash
- ca_KeyPri(ca_Hash) = enc_ca_Hash
- ca_KeyPub + ca_Info + enc_ca_Hash = ca_Cert
被认证机构:
- s_KeyPub, s_KeyPri,s_Info
- Hash(s_KeyPub + s_Info + ca_Info) = s_Hash
- ca_KeyPri(s_Hash) = enc_s_Hash
- s_KeyPub + s_Info + ca_Info + enc_s_Hash = s_Cert
- s_Cert不可用于签署下一级证书
次级认证机构
- ca2_KeyPub, ca2_KeyPri, ca2_Info
- Hash(Ca2_KeyPub + ca2_Info + ca_Info) = ca2_Hash
- ca_KeyPri(ca2_Hash) = enc_ca2_Hash
- (ca2_KeyPub + ca2_Info + ca_Info + enc_ca2_Hash) = ca2_Cert
服务器单方验证,服务器需要具有:
- s_Cert
- CA证书链,除非客户端已经具有某些这些链上的证书了(通常客户端有CA证书)
客户端验证过程(假设s_Cert由根CA验证,客户端具有根证书ca_Cert)
- 获取s_Cert
- 提取s_Cert中的ca_Info
- 提取ca_Cert中的ca_KeyPub,对s_Cert中的enc_s_Hash进行解密得到s_Hash
- 对(s_KeyPub + S_Info + ca_Info)进行散列,得到s_Hash_tmp
- 判断s_Hash_tmp和s_Hash是否相等
客户端验证过程(假设s_Cert由二级CA验证)
- 获取s2_Cert,ca2_Cert
- 从s2_Cert中提取ca2_Info和enc_S2_Hash
- 从ca2_Cert中提取ca2_PubKey
- 用ca2_PubKey解密enc_S2_Hash并重新生成验证之
- 从ca_Cert中提取ca_KeyPub
- 从ca2_Cert中提取ca_Info和enc_ca2_Hash
- 使用ca_KeyPub解密enc_ca2_Hash并重新生成验证之
openssl可以用于证书的生成,非对称算法可以是RAS4096,摘要算法可以是SHA512。
第一步,创建自签名的根证书:
openssl req -x509 -nodes -days 3650 -newkey rsa:4096 -keyout ca_key.pem -out ca_cert.pem -new -sha512
生成ca_cert.pem以及ca_key.pem。
第二步生成用户证书请求(csr)以及证书私钥:
openssl req -nodes -new -newkey rsa:4096 -keyout cert1_key.pem -out cert1_csr.pem
生成cert1_csr.pem以及cert1_key.pem。
递交csr以及cnf来获取证书:
openssl ca -in cert1_csr.pem -out cert1_cert.pem -config test_pki.cnf
其中test.pki.cnf内容如下:
[ ca ]
default_ca = CA
[ CA ]
dir = .
database = \$dir/index.txt
new_certs_dir = \$dir
certificate = \$dir/ca_cert.pem
serial = \$dir/serial
private_key = \$dir/ca_key.pem
RANDFILE = \$dir/.rand
default_bits = 4096
default_days = 365
default_crl_days = 30
default_md = sha512
unique_subject = no
policy = policy_anything
[ policy_anything ]
countryName = supplied
stateOrProvinceName = supplied
localityName = supplied
organizationName = supplied
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
以上执行过程中,执行openssl ca -in cert1_csr.pem -out cert1_cert.pem -config test_pki.cnf
发生问题:
Can't open $dir/ca_key.pem for reading, No such file or directory
WikiPedia X.509
证书有可能被撤销(参考https://en.wikipedia.org/wiki/Revocation_list),一种查询是否被撤销的方式是使用https://en.wikipedia.org/wiki/Online_Certificate_Status_Protocol)。
X.509v3证书结构(以ASN.1描述):
- Certificate
- Version Number
- Serial Number
- Signature Algorithm ID
- Issuer Name
- Validity period
- Not Before
- Not After
- Subject name
- Subject Public Key Info
- Public Key Algorithm
- Subject Public Key
- Issuer Unique Identifier (optional)
- Subject Unique Identifier (optional)
- Extensions (optional)
- …
- Certificate Signature Algorithm
- Certificate Signature
[Certificate chains and cross-certification]一节暂略。
参考:
What are certificate formats and what is the difference between them?
X.509 certificate encoding formats and extensions
- Base64
- PEM(Privacy Enhanced Mail)
- .pem
- .crt
- .ca-bundle
- PKCS#7
- .p7b
- .p7s
- Binary
- DER(Distinguished Encoding Rule)
- .der
- .cer
- PKCS#12
- .pfx(Personal Information Exchange)
- .p12
- DER(Distinguished Encoding Rule)
- PEM(Privacy Enhanced Mail)
参考What are the different formats of SSL certificates and how we can upload a certificate to NetScaler
其他参考
使用openssl查看pem内容:
openssl x509 -in certificate.pem -text
来源How to view the contents of a .pem certificate?。
- https://en.wikipedia.org/wiki/Certificate_Management_Protocol
- https://www.itu.int/rec/T-REC-X.509
- https://www.samltool.com/format_x509cert.php
- The relationship of the X.500 Directory and X.509v3 digital certificates
- Firefox中包含的CA列表https://wiki.mozilla.org/CA/Included_Certificates
SSH相关
- https://en.wikipedia.org/wiki/Secure_Shell
- Why doesn’t SSH use TLS?
- SSH key authentication using LDAP
- Accessing LDAP through SSH tunnel
(本篇完)
2022-06-25更新
Internet X.509 Public Key Infrastructure Certificate Management Protocol (CMP)
EE(end entity),证书颁与者,也就是证书的subject(主体);CA,证书颁发者;RA,可选的证书注册主管机构。假设RA不独立存在,那么CA也扮演RA的角色。
EE所需的基本信息
- Name
- 私钥
- CA的公钥 (或者公钥的指纹)
PKI Management Requirements看得有点头胀。
安全存储这些信息的地方叫做PSE (Personal Security Environment)。
还是参考Wikipedia吧。
(更新完)