TLS协议梳理
|<- 1 ->|<------ 2 ------>|<----- 2 ---->| +--------+----------------+---------------+ | 类型 | 版本号 | 长度 | +--------+----------------+---------------+16 03 01 01 4a // 第一个 ClientHello 的记录层头
- 特性PKCS v1.5 签名RSA-PSS
- 特性PKCS v1.5 加密PKCS OAEP
type DigitalSigner struct { privateKey *rsa.PrivateKey publicKey *rsa.PublicKey } // 签名数据 func (ds *DigitalSigner) SignData(data []byte) ([]byte, error) { // 1. 计算数据的哈希值 hashed := sha256.Sum256(data) // 2. 生成签名 signature, err := rsa.SignPKCS1v15( rand.Reader, ds.privateKey, crypto.SHA256, hashed[:], ) if err != nil { return nil, err } return signature, nil } // 验证签名 func (ds *DigitalSigner) VerifySignature(data, signature []byte) error { // 1. 计算数据的哈希值 hashed := sha256.Sum256(data) // 2. 验证签名 return rsa.VerifyPKCS1v15( ds.publicKey, crypto.SHA256, hashed[:], signature, ) }
(EC)DHE 密钥交换 ↓ Shared Secret ↓ Early Secret (可选,用于 0-RTT) ↓ Handshake Secret (握手密钥) ↓ Master Secret (主密钥)Master Secret ↓ HKDF-Expand-Label( secret: Master Secret, label: "res master", context: Transcript-Hash(Handshake Context), length: Hash.length ) ↓ Resumption Master SecretEarly Secret ↓ HKDF-Expand-Label( secret: Early Secret, label: "e exp master", context: Transcript-Hash(Handshake Context), length: Hash.length ) ↓ Early Exporter Master SecretMaster Secret ↓ HKDF-Expand-Label( secret: Master Secret, label: "exp master", context: Transcript-Hash(Handshake Context), length: Hash.length ) ↓ Exporter Master Secret
CA签名时: 计算摘要: h = SHA256(证书信息) 用私钥加密: signature = h^d mod n # d是私钥指数,n是模数(两个大素数的乘积) 验证签名时: 用公钥解密: h' = signature^e mod n # n是模数,与私钥中的n相同 计算摘要: h = SHA256(证书信息) 比较 h 和 h' 是否相等 -------------------------------------------------------- 证书信息摘要: 123 CA签名: 私钥d = 3 n = 33 signature = 123^3 mod 33 = 9 验证: 公钥e = 7 n = 33 9^7 mod 33 = 123 (得到原始摘要) 比对: 计算当前证书信息的摘要是否等于123
openssl x509 -inform der -in cert.der -out cert.pemopenssl rsa -in private.key -outform pem -out private.pemcat cert.pem private.pem > combined.pem
# 连接到example.org服务器 Connecting to 93.184.215.14 CONNECTED(00000005) # 证书链验证过程 # 验证根证书 # 根证书信息: C(国家)=美国, O(组织)=DigiCert公司, OU(组织单位)=www.digicert.com, CN(通用名称)=DigiCert全球根证书G2 depth=2 C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert Global Root G2 # 验证通过,返回值为1表示验证成功 verify return:1 # 验证中间证书 depth=1 C=US, O=DigiCert Inc, CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1 verify return:1 # 验证服务器证书 depth=0 C=US, ST=California, L=Los Angeles, O=Internet Corporation for Assigned Names and Numbers, CN=www.example.org verify return:1 # 证书链详细信息 --- Certificate chain # 服务器证书信息 0 s:C=US, ST=California, L=Los Angeles, O=Internet Corporation for Assigned Names and Numbers, CN=www.example.org i:C=US, O=DigiCert Inc, CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1 a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA256 v:NotBefore: Jan 30 00:00:00 2024 GMT; NotAfter: Mar 1 23:59:59 2025 GMT # 服务器证书内容 -----BEGIN CERTIFICATE----- MIIHbjCCBlagAwIBAgIQB1vO8waJyK3fE+Ua9K/hhzANBgkqhkiG9w0BAQsFADBZ [... 证书内容已省略 ...] -----END CERTIFICATE----- # 中间证书信息 1 s:C=US, O=DigiCert Inc, CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1 i:C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert Global Root G2 a:PKEY: rsaEncryption, 2048 (bit); sigalg: RSA-SHA256 v:NotBefore: Mar 30 00:00:00 2021 GMT; NotAfter: Mar 29 23:59:59 2031 GMT # 中间证书内容 -----BEGIN CERTIFICATE----- MIIEyDCCA7CgAwIBAgIQDPW9BitWAvR6uFAsI8zwZjANBgkqhkiG9w0BAQsFADBh [... 证书内容已省略 ...] -----END CERTIFICATE----- # 服务器证书摘要 --- Server certificate subject=C=US, ST=California, L=Los Angeles, O=Internet Corporation for Assigned Names and Numbers, CN=www.example.org issuer=C=US, O=DigiCert Inc, CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1 # TLS连接信息: # - 未发送客户端证书CA名称列表 # - 对等方使用的摘要算法: SHA256 # - 对等方签名类型: RSA-PSS # - 服务器临时密钥: ECDH算法,使用prime256v1曲线,密钥长度256位 --- No client certificate CA names sent Peer signing digest: SHA256 Peer signature type: RSA-PSS Server Temp Key: ECDH, prime256v1, 256 bits # 握手统计 --- SSL handshake has read 3821 bytes and written 765 bytes Verification: OK # TLS会话参数 --- New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384 Protocol: TLSv1.3 Server public key is 2048 bit This TLS version forbids renegotiation. Compression: NONE Expansion: NONE No ALPN negotiated Early data was not sent Verify return code: 0 (ok) # 会话票据信息 --- Post-Handshake New Session Ticket arrived: SSL-Session: Protocol : TLSv1.3 Cipher : TLS_AES_256_GCM_SHA384 Session-ID: 9348876C0D58ABFB6EB4E6C4B80695A6F32EC23C689F4260E76DD018734F7F01 [... 会话票据详细信息已省略 ...] # 连接关闭 read R BLOCK closed
Client Server | | | 1. ClientHello | |---------------------------------------->| | | | 2. ServerHello | | 3. Certificate* | | 4. ServerKeyExchange* | | 5. CertificateRequest* | | 6. ServerHelloDone | |<----------------------------------------| | | | 7. Certificate* | | 8. ClientKeyExchange | | 9. CertificateVerify* | | 10.[ChangeCipherSpec] | | 11.Finished | |---------------------------------------->| | | | 12.[ChangeCipherSpec] | | 13.Finished | |<----------------------------------------| | | | Application Encrypted Data | |<--------------------------------------->|- 算法类型ServerKeyExchangeClientKeyExchange安全特性使用场景
Frame 412: 373 bytes on wire (2984 bits), 373 bytes captured (2984 bits) on interface en0, id 0 Ethernet II, Src: 26:67:51:4a:0e:17 (26:67:51:4a:0e:17), Dst: DongguanHuar_7c:22:fb (3c:c7:86:7c:22:fb) Internet Protocol Version 4, Src: 192.168.19.50, Dst: 153.3.238.110 Transmission Control Protocol, Src Port: 52623, Dst Port: 443, Seq: 1, Ack: 1, Len: 319 Transport Layer Security TLSv1.2 Record Layer: Handshake Protocol: Client Hello Content Type: Handshake (22) Version: TLS 1.2 (0x0303) Length: 314 Handshake Protocol: Client Hello Handshake Type: Client Hello (1) Length: 310 Version: TLS 1.2 (0x0303) Random: 080d07ab668fee92d5283979cfd677feb1458f13bd01c0d1a696eb12fba386c0 GMT Unix Time: Apr 13, 1974 15:54:19.000000000 CST Random Bytes: 668fee92d5283979cfd677feb1458f13bd01c0d1a696eb12fba386c0 Session ID Length: 32 Session ID: 495c98be895dc023dba9cb5a8b6469a79bdc0d40ca160e5798bcd5784579943f Cipher Suites Length: 98 Cipher Suites (49 suites) Cipher Suite: TLS_CHACHA20_POLY1305_SHA256 (0x1303) Cipher Suite: TLS_AES_256_GCM_SHA384 (0x1302) /** 省略部分 **/ Cipher Suite: TLS_EMPTY_RENEGOTIATION_INFO_SCSV (0x00ff) Compression Methods Length: 1 Compression Methods (1 method) Compression Method: null (0) Extensions Length: 139 Extension: supported_versions (len=5) TLS 1.3, TLS 1.2 Type: supported_versions (43) Length: 5 Supported Versions length: 4 Supported Version: TLS 1.3 (0x0304) Supported Version: TLS 1.2 (0x0303) Extension: key_share (len=38) x25519 Type: key_share (51) Length: 38 Key Share extension Client Key Share Length: 36 Key Share Entry: Group: x25519, Key Exchange length: 32 Group: x25519 (29) Key Exchange Length: 32 Key Exchange: 6be2999b6a58d7a19f9c4464f2d482201d4a1bc6bd7bc17a09b5bc9bf804ec48 Extension: server_name (len=18) name=www.baidu.com Type: server_name (0) Length: 18 Server Name Indication extension Server Name list length: 16 Server Name Type: host_name (0) Server Name length: 13 Server Name: www.baidu.com Extension: ec_point_formats (len=2) Type: ec_point_formats (11) Length: 2 EC point formats Length: 1 Elliptic curves point formats (1) EC point format: uncompressed (0) Extension: supported_groups (len=10) Type: supported_groups (10) Length: 10 Supported Groups List Length: 8 Supported Groups (4 groups) Supported Group: x25519 (0x001d) Supported Group: secp256r1 (0x0017) Supported Group: secp384r1 (0x0018) Supported Group: secp521r1 (0x0019) Extension: signature_algorithms (len=24) Type: signature_algorithms (13) Length: 24 Signature Hash Algorithms Length: 22 Signature Hash Algorithms (11 algorithms) Signature Algorithm: rsa_pss_rsae_sha512 (0x0806) Signature Hash Algorithm Hash: Unknown (8) Signature Hash Algorithm Signature: Unknown (6) /** 省略部分 **/ Signature Hash Algorithm Hash: SHA1 (2) Signature Hash Algorithm Signature: ECDSA (3) Extension: application_layer_protocol_negotiation (len=14) Type: application_layer_protocol_negotiation (16) Length: 14 ALPN Extension Length: 12 ALPN Protocol ALPN string length: 2 ALPN Next Protocol: h2 ALPN string length: 8 ALPN Next Protocol: http/1.1 [JA4: t13d4907h2_0d8feac7bc37_7395dae3b2f3] [JA4_r [truncated]: t13d4907h2_0004,0005,000a,0016,002f,0033,0035,0039,003c,003d,0041,0045,0067,006b,0081,0084,0088,009c,009d,009e,009f,00ba,00be,00c0,00c4,00ff,1301,1302,1303,c007,c008,c009,c00a,c011,c012,c013,c014,c023,c024,c027,c028,c02b] [JA3 Fullstring [truncated]: 771,4867-4866-4865-52393-52392-52394-49200-49196-49192-49188-49172-49162-159-107-57-65413-196-136-129-157-61-53-192-132-49199-49195-49191-49187-49171-49161-158-103-51-190-69-156-60-47-186-65-49169-49159-5-4-4917] [JA3: 375c6162a492dfbf2795909110ce8424]Frame 416: 1334 bytes on wire (10672 bits), 1334 bytes captured (10672 bits) on interface en0, id 0 Ethernet II, Src: DongguanHuar_7c:22:fb (3c:c7:86:7c:22:fb), Dst: 26:67:51:4a:0e:17 (26:67:51:4a:0e:17) Internet Protocol Version 4, Src: 153.3.238.110, Dst: 192.168.19.50 Transmission Control Protocol, Src Port: 443, Dst Port: 52623, Seq: 1, Ack: 320, Len: 1280 Transport Layer Security TLSv1.2 Record Layer: Handshake Protocol: Server Hello Content Type: Handshake (22) Version: TLS 1.2 (0x0303) Length: 102 Handshake Protocol: Server Hello Handshake Type: Server Hello (2) Length: 98 Version: TLS 1.2 (0x0303) Random: 67738e87efe82702b2befa953b7447d0378458bfcedc3b50f3167ab4d29b8196 GMT Unix Time: Dec 31, 2024 14:26:15.000000000 CST Random Bytes: efe82702b2befa953b7447d0378458bfcedc3b50f3167ab4d29b8196 Session ID Length: 32 Session ID: 754d94678cd10b5f302a8909fa3faf0619f3ee1a4cd9b6ea5a6c468ec2204558 Cipher Suite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f) Compression Method: null (0) Extensions Length: 26 Extension: renegotiation_info (len=1) Type: renegotiation_info (65281) Length: 1 Renegotiation Info extension Renegotiation info extension length: 0 Extension: application_layer_protocol_negotiation (len=11) Type: application_layer_protocol_negotiation (16) Length: 11 ALPN Extension Length: 9 ALPN Protocol ALPN string length: 8 ALPN Next Protocol: http/1.1 Extension: ec_point_formats (len=2) Type: ec_point_formats (11) Length: 2 EC point formats Length: 1 Elliptic curves point formats (1) EC point format: uncompressed (0) [JA3S Fullstring: 771,49199,65281-16-11] [JA3S: 2de81c22ea32a57162df5cb08d4a2795] TLS segment data (1173 bytes)
Client Server Key ^ ClientHello Exch | + key_share* | + signature_algorithms* | + psk_key_exchange_modes* v + pre_shared_key* --------> ServerHello ^ Key + key_share* | Exch + pre_shared_key* v {EncryptedExtensions} ^ Server {CertificateRequest*} v Params {Certificate*} ^ {CertificateVerify*} | Auth {Finished} v <-------- [Application Data*] ^ {Certificate*} Auth | {CertificateVerify*} v {Finished} --------> [Application Data] <-------> [Application Data] + 表示在前面提到的消息中发送的重要扩展。 * 表示可选的或依情况而定的消息/扩展,不是总是会发送。 {} 表示使用从[发送方]_handshake_traffic_secret派生的密钥保护的消息。 [] 表示使用从[发送方]_application_traffic_secret_N派生的密钥保护的消息。HMAC_Value = HMAC( finished_key, "client finished" + Handshake Context Hash // 客户端 OR "server finished" + Handshake Context Hash // 服务器 )
# 第一次 Extension: key_share (len=38) x25519 Type: key_share (51) Length: 38 Key Share extension Client Key Share Length: 36 Key Share Entry: Group: x25519, Key Exchange length: 32 Group: x25519 (29) Key Exchange Length: 32 Key Exchange: 6fc363b9fc781bc9fec7b366a3fe99e88025fe87d16bc1acefee7098458c3101 # HelloRetry Request Extension: key_share (len=2) secp256r1 Type: key_share (51) Length: 2 Key Share extension Selected Group: secp256r1 (23) # 第二次 Extension: key_share (len=71) secp256r1 Type: key_share (51) Length: 71 Key Share extension Client Key Share Length: 69 Key Share Entry: Group: secp256r1, Key Exchange length: 65 Group: secp256r1 (23) Key Exchange Length: 65 Key Exchange: 040f6a84376a715cf75e734e20b21062f72edd9034ac3d82456db978542d41a10f2e1fb991fdf2f293a6c450c5f3531e6fc4467422c6f4d68b48e685a1ae10322fFrame 26024: 354 bytes on wire (2832 bits), 354 bytes captured (2832 bits) on interface en0, id 0 Ethernet II, Src: 26:67:51:4a:0e:17 (26:67:51:4a:0e:17), Dst: DongguanHuar_7c:22:fb (3c:c7:86:7c:22:fb) Internet Protocol Version 4, Src: 192.168.19.50, Dst: 93.184.215.14 Transmission Control Protocol, Src Port: 60820, Dst Port: 443, Seq: 250, Ack: 100, Len: 288 Transport Layer Security TLSv1.3 Record Layer: Change Cipher Spec Protocol: Change Cipher Spec Content Type: Change Cipher Spec (20) Version: TLS 1.2 (0x0303) Length: 1 Change Cipher Spec Message TLSv1.3 Record Layer: Handshake Protocol: Client Hello Content Type: Handshake (22) Version: TLS 1.2 (0x0303) Length: 277 Handshake Protocol: Client Hello Handshake Type: Client Hello (1) Length: 273 Version: TLS 1.2 (0x0303) Random: 9a9b927033f057ee12c42a3641d96cfbff2d7ca419a52e1786e3d0d21a2cbba0 Session ID Length: 32 Session ID: 8b6f8d83cd756d328a9cc6ae04909084a424d15c5635ac23cc6f65d3e2cba63e Cipher Suites Length: 6 Cipher Suites (3 suites) Cipher Suite: TLS_AES_256_GCM_SHA384 (0x1302) Cipher Suite: TLS_CHACHA20_POLY1305_SHA256 (0x1303) Cipher Suite: TLS_AES_128_GCM_SHA256 (0x1301) Compression Methods Length: 1 Compression Methods (1 method) Compression Method: null (0) Extensions Length: 194 Extension: server_name (len=16) name=example.com Type: server_name (0) Length: 16 Server Name Indication extension Server Name list length: 14 Server Name Type: host_name (0) Server Name length: 11 Server Name: example.com Extension: ec_point_formats (len=4) Type: ec_point_formats (11) Length: 4 EC point formats Length: 3 Elliptic curves point formats (3) EC point format: uncompressed (0) EC point format: ansiX962_compressed_prime (1) EC point format: ansiX962_compressed_char2 (2) Extension: supported_groups (len=22) Type: supported_groups (10) Length: 22 Supported Groups List Length: 20 Supported Groups (10 groups) Supported Group: x25519 (0x001d) Supported Group: secp256r1 (0x0017) Supported Group: x448 (0x001e) Supported Group: secp521r1 (0x0019) Supported Group: secp384r1 (0x0018) /*省略部分*/ Extension: session_ticket (len=0) Type: session_ticket (35) Length: 0 Session Ticket: <MISSING> Extension: encrypt_then_mac (len=0) Type: encrypt_then_mac (22) Length: 0 Extension: extended_master_secret (len=0) Type: extended_master_secret (23) Length: 0 Extension: signature_algorithms (len=36) Type: signature_algorithms (13) Length: 36 Signature Hash Algorithms Length: 34 Signature Hash Algorithms (17 algorithms) Signature Algorithm: ecdsa_secp256r1_sha256 (0x0403) Signature Hash Algorithm Hash: SHA256 (4) Signature Hash Algorithm Signature: ECDSA (3) /**省略部分 **/ Signature Algorithm: rsa_pkcs1_sha512 (0x0601) Signature Hash Algorithm Hash: SHA512 (6) Signature Hash Algorithm Signature: RSA (1) Extension: supported_versions (len=3) TLS 1.3 Type: supported_versions (43) Length: 3 Supported Versions length: 2 Supported Version: TLS 1.3 (0x0304) Extension: psk_key_exchange_modes (len=2) Type: psk_key_exchange_modes (45) Length: 2 PSK Key Exchange Modes Length: 1 PSK Key Exchange Mode: PSK with (EC)DHE key establishment (psk_dhe_ke) (1) Extension: key_share (len=71) secp256r1 Type: key_share (51) Length: 71 Key Share extension Client Key Share Length: 69 Key Share Entry: Group: secp256r1, Key Exchange length: 65 Group: secp256r1 (23) Key Exchange Length: 65 Key Exchange: 040f6a84376a715cf75e734e20b21062f72edd9034ac3d82456db978542d41a10f2e1fb991fdf2f293a6c450c5f3531e6fc4467422c6f4d68b48e685a1ae10322f [JA4: t13d031000_55b375c5d22e_3eb3b556ea2c] [JA4_r: t13d031000_1301,1302,1303_000a,000b,000d,0016,0017,0023,002b,002d,0033_0403,0503,0603,0807,0808,081a,081b,081c,0809,080a,080b,0804,0805,0806,0401,0501,0601] [JA3 Fullstring: 771,4866-4867-4865,0-11-10-35-22-23-13-43-45-51,29-23-30-25-24-256-257-258-259-260,0-1-2] [JA3: c3eddff4f56c6811c9b3be93e9b13273]
Transport Layer Security TLSv1.3 Record Layer: Handshake Protocol: Server Hello Content Type: Handshake (22) Version: TLS 1.2 (0x0303) Length: 155 Handshake Protocol: Server Hello Handshake Type: Server Hello (2) Length: 151 Version: TLS 1.2 (0x0303) Random: 8e38c96f0707cd108a8f0b39fb72f3cd64287a22ee95f13ec5de8642195914b7 Session ID Length: 32 Session ID: 3015c3341bcadcc0f9010249980594d3674d086b11f333cd8d001da5156bf2a0 Cipher Suite: TLS_AES_256_GCM_SHA384 (0x1302) Compression Method: null (0) Extensions Length: 79 Extension: supported_versions (len=2) TLS 1.3 Type: supported_versions (43) Length: 2 Supported Version: TLS 1.3 (0x0304) Extension: key_share (len=69) secp256r1 Type: key_share (51) Length: 69 Key Share extension Key Share Entry: Group: secp256r1, Key Exchange length: 65 Group: secp256r1 (23) Key Exchange Length: 65 Key Exchange: 0491cc138fd8dc8b88f247177607a1be67b32be79ea55fe2ebeaa731bd6adcba980edef70c56ba0ea85161dccd28d853a6f5b31035a74b8bec6d9f3feeefe94faa [JA3S Fullstring: 771,4866,43-51] [JA3S: 15af977ce25de452b96affa2addb1036] TLSv1.3 Record Layer: Application Data Protocol: Hypertext Transfer Protocol Opaque Type: Application Data (23) Version: TLS 1.2 (0x0303) Length: 32 Encrypted Application Data: 2839ea62c00bb9cf01c106d1e8ae90f8d016b630d75e1e78d37145e0dc2f4bc7 [Application Data Protocol: Hypertext Transfer Protocol] TLS segment data (1071 bytes)
ClientHello + key_share* + pre_shared_key --------> ServerHello + pre_shared_key + key_share* {EncryptedExtensions} {Finished} <-------- [Application Data*] {Finished} --------> [Application Data] <-------> [Application Data + 表示在前面提到的消息中发送的重要扩展。 * 表示可选的或依情况而定的消息/扩展,不是总是会发送。 {} 表示使用从[发送方]_handshake_traffic_secret派生的密钥保护的消息。 [] 表示使用从[发送方]_application_traffic_secret_N派生的密钥保护的消息。Client Server ClientHello + early_data + key_share* + psk_key_exchange_modes + pre_shared_key (Application Data*) --------> ServerHello + pre_shared_key + key_share* {EncryptedExtensions} + early_data* {Finished} <-------- [Application Data*] (EndOfEarlyData) {Finished} --------> [Application Data] <-------> [Application Data] + 表示在之前提到的消息中发送的值得注意的扩展 * 表示可选的或情况相关的消息/扩展,不是每次都会发送 () 表示使用从 client_early_traffic_secret 派生的密钥保护的消息 {} 表示使用从 [发送方]_handshake_traffic_secret 派生的密钥保护的消息 [] 表示使用从 [发送方]_application_traffic_secret_N 派生的密钥保护的消息
最后更新于

