From 540608d73ec927c4a53e4aecf25f9d0176152d6a Mon Sep 17 00:00:00 2001 From: Nemu-x Date: Wed, 11 Nov 2026 00:10:18 +0300 Subject: [PATCH] feat: add HelloChrome_155 and make it HelloChrome_Auto Port the Chrome 155 profile from refraction-networking/utls 6ebdceb2 ("Add Chrome 155 fingerprint verified against a fresh browser capture"), verified there against Chrome 155.0.8059.40. Compared with Chrome 133 the ClientHello gains a GREASE value and the three ML-DSA schemes (0x0904/0x0905/0x0906) in signature_algorithms and the trust_anchors extension (0xca34) with 28 Chrome Root Store IDs. - HelloChrome_155 and its spec; HelloChrome_Auto now points to it, as upstream does. HelloChrome_133 is unchanged. - TrustAnchorsExtension (typed, JSON, Fingerprinter via ExtensionFromID). - GREASE in signature_algorithms drawn from its own BoringSSL seed slot; the internal seed grows to 8 slots while GetBoringGREASEValue keeps its [5]uint16 signature. - MLDSA44/65/87 SignatureScheme constants and dicttls names. Unlike upstream (go 1.27 sync) this fork does not implement ML-DSA, so the codepoints are only advertised. - Upstream tests and the captured ClientHello fixture. Not ported: the ML-DSA handshake test (needs crypto/mldsa) and the preset ownership test (needs upstream's ClientHelloSpec.clone). --- README.md | 8 + common.go | 6 + common_string.go | 8 + dicttls/exttype_values.go | 23 ++- dicttls/signaturescheme.go | 9 + testdata/chrome155_clienthello.hex | 1 + testdata/chrome155_clienthello.md | 52 +++++ u_chrome155.go | 38 ++++ u_chrome155_test.go | 320 +++++++++++++++++++++++++++++ u_common.go | 6 +- u_conn.go | 2 +- u_grease_api_test.go | 21 ++ u_mldsa_json_test.go | 54 +++++ u_parrots.go | 100 ++++++++- u_tls_extensions.go | 13 +- u_trust_anchors.go | 103 ++++++++++ u_trust_anchors_test.go | 173 ++++++++++++++++ 17 files changed, 916 insertions(+), 21 deletions(-) create mode 100644 testdata/chrome155_clienthello.hex create mode 100644 testdata/chrome155_clienthello.md create mode 100644 u_chrome155.go create mode 100644 u_chrome155_test.go create mode 100644 u_grease_api_test.go create mode 100644 u_mldsa_json_test.go create mode 100644 u_trust_anchors.go create mode 100644 u_trust_anchors_test.go diff --git a/README.md b/README.md index f30b35baf8d..1514bfc5c9a 100644 --- a/README.md +++ b/README.md @@ -251,8 +251,16 @@ will prepare ClientHello with empty uconn.Extensions so you can fill it with TLS 4. The rest will will parrot given browser. Such parrots include, for example: * `utls.HelloChrome_Auto`- parrots recommended(usually latest) Google Chrome version * `utls.HelloChrome_58` - parrots Google Chrome 58 + * `utls.HelloChrome_155` - parrots a fresh Chrome 155.0.8059.40 connection on macOS * `utls.HelloFirefox_Auto` - parrots recommended(usually latest) Firefox version * `utls.HelloFirefox_55` - parrots Firefox 55 + +`HelloChrome_Auto` currently selects `HelloChrome_155`. This profile includes +GREASE and ML-DSA signature schemes and the 28 trust anchor IDs observed in a +[fresh Chrome capture](testdata/chrome155_clienthello.md). The IDs are a snapshot +of that build's Chrome Root Store; component updates and browser feature settings +can change the ClientHello. `TrustAnchorsExtension` preserves these IDs when +fingerprinting a capture and does not change `Config.RootCAs`. # Usage ## Examples diff --git a/common.go b/common.go index 347d2c5d574..228fe5ad929 100644 --- a/common.go +++ b/common.go @@ -422,6 +422,12 @@ const ( // EdDSA algorithms. Ed25519 SignatureScheme = 0x0807 + // ML-DSA algorithms (draft-ietf-tls-mldsa). uTLS only advertises these + // codepoints for ClientHello parroting; it cannot verify ML-DSA signatures. + MLDSA44 SignatureScheme = 0x0904 + MLDSA65 SignatureScheme = 0x0905 + MLDSA87 SignatureScheme = 0x0906 + // Legacy signature and hash algorithms for TLS 1.2. PKCS1WithSHA1 SignatureScheme = 0x0201 ECDSAWithSHA1 SignatureScheme = 0x0203 diff --git a/common_string.go b/common_string.go index e15dd48838b..25df3964833 100644 --- a/common_string.go +++ b/common_string.go @@ -18,6 +18,9 @@ func _() { _ = x[ECDSAWithP384AndSHA384-1283] _ = x[ECDSAWithP521AndSHA512-1539] _ = x[Ed25519-2055] + _ = x[MLDSA44-2308] + _ = x[MLDSA65-2309] + _ = x[MLDSA87-2310] _ = x[PKCS1WithSHA1-513] _ = x[ECDSAWithSHA1-515] } @@ -32,10 +35,12 @@ const ( _SignatureScheme_name_6 = "PKCS1WithSHA512" _SignatureScheme_name_7 = "ECDSAWithP521AndSHA512" _SignatureScheme_name_8 = "PSSWithSHA256PSSWithSHA384PSSWithSHA512Ed25519" + _SignatureScheme_name_9 = "MLDSA44MLDSA65MLDSA87" ) var ( _SignatureScheme_index_8 = [...]uint8{0, 13, 26, 39, 46} + _SignatureScheme_index_9 = [...]uint8{0, 7, 14, 21} ) func (i SignatureScheme) String() string { @@ -59,6 +64,9 @@ func (i SignatureScheme) String() string { case 2052 <= i && i <= 2055: i -= 2052 return _SignatureScheme_name_8[_SignatureScheme_index_8[i]:_SignatureScheme_index_8[i+1]] + case 2308 <= i && i <= 2310: + i -= 2308 + return _SignatureScheme_name_9[_SignatureScheme_index_9[i]:_SignatureScheme_index_9[i+1]] default: return "SignatureScheme(" + strconv.FormatInt(int64(i), 10) + ")" } diff --git a/dicttls/exttype_values.go b/dicttls/exttype_values.go index cb0ef403fcd..30cbea89389 100644 --- a/dicttls/exttype_values.go +++ b/dicttls/exttype_values.go @@ -67,11 +67,12 @@ const ( // Not IANA assigned const ( - ExtType_next_protocol_negotiation uint16 = 13172 // https://datatracker.ietf.org/doc/html/draft-agl-tls-nextprotoneg-04 - ExtType_application_settings uint16 = 17513 // https://www.ietf.org/archive/id/draft-vvv-tls-alps-01.html - ExtType_application_settings_new uint16 = 17613 // https://www.ietf.org/archive/id/draft-vvv-tls-alps-01.html - ExtType_channel_id_old uint16 = 30031 // https://datatracker.ietf.org/doc/html/draft-balfanz-tls-channelid-01 - ExtType_channel_id uint16 = 30032 // https://datatracker.ietf.org/doc/html/draft-balfanz-tls-channelid-01 + ExtType_next_protocol_negotiation uint16 = 13172 // https://datatracker.ietf.org/doc/html/draft-agl-tls-nextprotoneg-04 + ExtType_application_settings uint16 = 17513 // https://www.ietf.org/archive/id/draft-vvv-tls-alps-01.html + ExtType_application_settings_new uint16 = 17613 // https://www.ietf.org/archive/id/draft-vvv-tls-alps-01.html + ExtType_channel_id_old uint16 = 30031 // https://datatracker.ietf.org/doc/html/draft-balfanz-tls-channelid-01 + ExtType_channel_id uint16 = 30032 // https://datatracker.ietf.org/doc/html/draft-balfanz-tls-channelid-01 + ExtType_trust_anchors uint16 = 0xca34 // draft-ietf-tls-trust-anchor-ids ) var DictExtTypeValueIndexed = map[uint16]string{ @@ -135,11 +136,12 @@ var DictExtTypeValueIndexed = map[uint16]string{ 59: "dnssec_chain", 65281: "renegotiation_info", - 13172: "next_protocol_negotiation", - 17513: "application_settings", - 17613: "application_settings_new", - 30031: "channel_id_old", - 30032: "channel_id", + 13172: "next_protocol_negotiation", + 17513: "application_settings", + 17613: "application_settings_new", + 30031: "channel_id_old", + 30032: "channel_id", + 0xca34: "trust_anchors", } var DictExtTypeNameIndexed = map[string]uint16{ @@ -209,4 +211,5 @@ var DictExtTypeNameIndexed = map[string]uint16{ "application_settings_new": 17613, "channel_id_old": 30031, "channel_id": 30032, + "trust_anchors": 0xca34, } diff --git a/dicttls/signaturescheme.go b/dicttls/signaturescheme.go index d0b9803f8e5..70e5e3dc501 100644 --- a/dicttls/signaturescheme.go +++ b/dicttls/signaturescheme.go @@ -38,6 +38,9 @@ const ( SigScheme_ecdsa_brainpoolP256r1tls13_sha256 uint16 = 0x081A SigScheme_ecdsa_brainpoolP384r1tls13_sha384 uint16 = 0x081B SigScheme_ecdsa_brainpoolP512r1tls13_sha512 uint16 = 0x081C + SigScheme_mldsa44 uint16 = 0x0904 + SigScheme_mldsa65 uint16 = 0x0905 + SigScheme_mldsa87 uint16 = 0x0906 ) var DictSignatureSchemeValueIndexed = map[uint16]string{ @@ -75,6 +78,9 @@ var DictSignatureSchemeValueIndexed = map[uint16]string{ 0x081A: "ecdsa_brainpoolP256r1tls13_sha256", 0x081B: "ecdsa_brainpoolP384r1tls13_sha384", 0x081C: "ecdsa_brainpoolP512r1tls13_sha512", + 0x0904: "mldsa44", + 0x0905: "mldsa65", + 0x0906: "mldsa87", } var DictSignatureSchemeNameIndexed = map[string]uint16{ @@ -113,4 +119,7 @@ var DictSignatureSchemeNameIndexed = map[string]uint16{ "ecdsa_brainpoolP256r1tls13_sha256": 0x081A, "ecdsa_brainpoolP384r1tls13_sha384": 0x081B, "ecdsa_brainpoolP512r1tls13_sha512": 0x081C, + "mldsa44": 0x0904, + "mldsa65": 0x0905, + "mldsa87": 0x0906, } diff --git a/testdata/chrome155_clienthello.hex b/testdata/chrome155_clienthello.hex new file mode 100644 index 00000000000..1d31499a844 --- /dev/null +++ b/testdata/chrome155_clienthello.hex @@ -0,0 +1 @@ +160301079e0100079a030397ba84e6911f26dc308204a2e65a2264ea769abc71617e9832e8a05327e2c03f20130452c827a0b862598e1f621324a0887d19f386284186f43e136844ed39fbb200201a1a130113021303c02bc02fc02cc030cca9cca8c013c014009c009d002f003501000731fafa000000120000ca3400ba00b80582df1302010582df1302060582df13020d0582df13020e0582df13020f0582df1302120582df1302130582df13021408839a648c9b2d010708839a648c9b2d010808839a648c9b2d010908839a648c9b2d010a08839a648c9b2d010b08839a648c9b2d010c08839a648c9b2d010d08839a648c9b2d011208839a648c9b2d011304d679090104d679090404d679090504d679090604d679090704d679090804d679090a04d679090b04d679090c04d679090d04d679090ffe0d00da00000100014200200b81d7a0c9bb3d4482028d5f90d7ed4f702aa257f7621d379be6d154251e745b00b0573e4731d509ac8e562a21f30236277f71fef7f0bd518c2c9916022a74afcb7d4d6145d72ee33f2f3720f85df5d6840bf3ce4dde5bc3efdacda6d4116add75ced6fa8c993692791f9437400ea835ff5f57776025cc7ff2828a93fff832dc24a1e394cf503813d67c4f45b0580f68d16fcaf5d774ac28a4bedac83733318c7cca132bebd041bba4077e53f51a2e30018559b2ed802ecf7398d369ab3090b8c4eb257b197631fad183682d83c8b40f82be001b0003020002000a000c000a7a7a11ec001d00170018ff0100010000230000002b0007065a5a030403030010000e000c02683208687474702f312e31000b00020100003304ef04ed7a7a00010011ec04c049c251fae6095e3992e4517d82ba97a8c7a789c66f450b0d9c3579870445f17856d8e0c53dfb643fe54032044236154d1fd722e7f6c3bf3606e84a68775638e765a7fdb85672dc3768941eaf232a55b229fe58cb41e35683b0532ff873249acd08ac19f79091c1acb54c53459d9ca31ac998ca6a67f172a1ef9b839151797e78589ca4aee75056be454adb070f6622577abc664b353fbc2272bf4c8cea7724dad8bfa0617d21555e3d0b69de388505b01d00ec263334c535c99699b20d8cd741f48646aea8c519334623ab33ea990701d7c400e48415da30a468953d5bbdc684aa78f1aca5c591a1805b69a786257099f5a02a833339bb9794dccc40b9188dd189596039623d76534ac1481e695b38974c5738a8107a16f5d4c19cd51d52c70bde8566b8500c73bba0e55ba09e067c6d3c76ddb99ae7da0fb7950d344bc86b1ac36827ae483a0ded32be2f897f66b96100440f6e148be9c19a3c1c7504e01eef443d4a674043e41f4f3c70029801a756227e5c95041053138472b19a103d95807ec57c02316499e72362642fe0c418c2306cdfb44dee570bc2783596a1464d5a16e4d59cb1bcb967e4459ce0430787141b547abeda15c418acd2f095addac18866cd99b2c70b3087c1b070f95320853a33b3d4bf9edca3cdf2b4a96c74154b57aab782d3e97d1801b95c2b586b7a0ca38bafc4d987ee600585ac8c359bc9c89997ed1231d87b28c0e32f90e16778a29cc2d3c4dbea79db389df497cf2210bd4dd46d7d21a6b3b683eb040bfca87019ec921b3a3ae7f20eb90a95449b70c2740fe4ea2c16e1c01ba5367676b48f58a6d7d657a52c61737b1671214b1210252107b441cc5c968a998616a77047a3d7516f28ac92438287b6bb178024566ce4a1b6f11efe9abb73894605b313567c60693c13f156a6fb55304134007c69128249a381281099a50cd18b0873a1697045bc8224bec2f87e4b09002703766e87b2c8845f56a7b77385562f41644710a36f9388fb4ab1311482b61c056b171919b3ac3b8cbf38e1971686ceead326e4654ded168b8ed8064b3032f23a7793c51700b9ba0cfbbc39390f3c82256898bb1933c1fec6cae242772e7b4076a6cf0d996c7cb6960930bea5a384083a18c00883ced91206c6401fb45b75fb44d073726d5bcaeffabd077705816c7ec7b03b0468a1f7238c660ac5f34b86b12968338c2817f45197cb04c5db396c4a13dc6a104663b57f43bad5063591c63d9cac2a199885a048809246bbd20191f211080d7286c7b61525382dada5ab9c175913cc0c9a011c4217575d1756d1e4b6b1da94b8cc1321a269d0310eb0374fe4bb39e41673d7742f335191290aaf3ec846a0d209546a97707938b3a307fe191599b59b26e1cfc92b6c69818a8de06c6e6215ce20c1057a8f7ed6912a5a89d64447aaa5b3b5a27cd41b8715fb44a502697e3185ca005077a194cfd2ce4b44cc3d5c7538e60718e90247ac1912329bf8e7c7e03078601b93a11163d5b0c1b34b1358608dfc282ebc18bd20f5a35c610e59c09f200875f05c10de1c7f27dc050296a49d4a03469c365f16a9b9a6c5def522832718e8022440019ad0d662237bb3535b912fab358c9a89fc944c201afe4a3c4d65837b31e2eb034759da63d960226e2669b646b549d3161427956172e60d876d0353ab787ac5cad550790a65ef062efbbf97f65f1cf62d001d0020d6b7a093831d73daf8e2aa6d7106321215cce52e6d14c408a0dafe217a207654000d001a0018aaaa0904090509060403080404010503080505010806060100170000002d0002010100050005010000000000000014001200000f7777772e6578616d706c652e636f6d44cd00050003026832caca000100 diff --git a/testdata/chrome155_clienthello.md b/testdata/chrome155_clienthello.md new file mode 100644 index 00000000000..9ecc533c7cf --- /dev/null +++ b/testdata/chrome155_clienthello.md @@ -0,0 +1,52 @@ +# Chrome 155 ClientHello fixture + +`chrome155_clienthello.hex` is the complete first TLS record from a fresh HTTPS +connection made by official, branded Google Chrome 155.0.8059.40 on macOS arm64 +on October 6, 2026. The browser version was checked in `chrome://version`. +The browser ran with a new dedicated profile, no TLS feature overrides, and QUIC +disabled so that the HTTPS connection used TCP. The page was opened through +native computer use at `https://www.example.com/`. + +Wireshark's `dumpcap` recorded the connection, and `tshark` extracted TCP stream +0's ClientHello. The original TLS keylog was used separately to verify that the +connection completed a full TLS 1.3 handshake. Neither the keylog nor the +certificate/application traffic is included in this fixture. + +The retained record is 1,955 bytes; its ClientHello handshake is 1,950 bytes. +SHA-256 of the decoded ClientHello handshake (excluding the five-byte TLS record +header): + +``` +235618e3922e52a71a9f6593a71ec4ca182dab6b3ab4e90a2a1733133e97439a +``` + +The original capture showed a new TCP SYN in frame 1, SYN/ACK in frame 3, and +ClientHello in frame 6. The ClientHello contains an empty `session_ticket` and +no `pre_shared_key`. The ServerHello in frame 13 selected TLS 1.3 and +`TLS_AES_128_GCM_SHA256`, without selecting a PSK. The decrypted server flight +in frame 19 contains EncryptedExtensions, CompressedCertificate, +CertificateVerify, and Finished; the client Finished follows in frame 21. +NewSessionTickets appear later in frame 35 and do not indicate resumption of +this connection. Chrome also opened an independent, fresh parallel connection +in stream 1, which completed a full handshake. + +The profile advertises 28 trust anchor IDs in the captured order, three ML-DSA +signature schemes preceded by GREASE, X25519MLKEM768 and X25519 key shares, and +the new ALPS codepoint. The TLS server-padding extension is absent from this +normal Chrome capture. + +Extraction command, using the original capture filename: + +```sh +tshark -r chrome155-example.pcapng \ + -Y 'tcp.stream == 0 && tls.handshake.type == 1' \ + -T fields -e tcp.reassembled.data > chrome155_clienthello.hex +``` + +`u_chrome155_test.go` compares this independent browser capture with the generated +profile. It ignores only randomized extension permutation, GREASE codepoints, +ephemeral key material, and GREASE ECH config/key/payload randomness. The ECH +payload length must belong to BoringSSL's 144/176/208/240-byte family. Cipher, +signature, group, version, ALPN, key-share group/length, and trust-anchor list +order remain significant; other extension bodies are compared exactly. The +same fixture also exercises default Fingerprinter parsing and regeneration. diff --git a/u_chrome155.go b/u_chrome155.go new file mode 100644 index 00000000000..c41b1c8495c --- /dev/null +++ b/u_chrome155.go @@ -0,0 +1,38 @@ +package tls + +// chrome155TrustAnchorIDs returns the Chrome Root Store IDs advertised by a fresh +// Google Chrome 155.0.8059.40 (macOS) connection. Chrome encodes the IDs in +// lexicographic order. Return fresh slices because ClientHelloSpec is mutable. +// Source: https://chromium.googlesource.com/chromium/src/+/refs/tags/155.0.8059.40/net/data/ssl/chrome_root_store/root_store.textproto +func chrome155TrustAnchorIDs() [][]byte { + return [][]byte{ + {0x82, 0xdf, 0x13, 0x02, 0x01}, + {0x82, 0xdf, 0x13, 0x02, 0x06}, + {0x82, 0xdf, 0x13, 0x02, 0x0d}, + {0x82, 0xdf, 0x13, 0x02, 0x0e}, + {0x82, 0xdf, 0x13, 0x02, 0x0f}, + {0x82, 0xdf, 0x13, 0x02, 0x12}, + {0x82, 0xdf, 0x13, 0x02, 0x13}, + {0x82, 0xdf, 0x13, 0x02, 0x14}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x07}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x08}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x09}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x0a}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x0b}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x0c}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x0d}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x12}, + {0x83, 0x9a, 0x64, 0x8c, 0x9b, 0x2d, 0x01, 0x13}, + {0xd6, 0x79, 0x09, 0x01}, + {0xd6, 0x79, 0x09, 0x04}, + {0xd6, 0x79, 0x09, 0x05}, + {0xd6, 0x79, 0x09, 0x06}, + {0xd6, 0x79, 0x09, 0x07}, + {0xd6, 0x79, 0x09, 0x08}, + {0xd6, 0x79, 0x09, 0x0a}, + {0xd6, 0x79, 0x09, 0x0b}, + {0xd6, 0x79, 0x09, 0x0c}, + {0xd6, 0x79, 0x09, 0x0d}, + {0xd6, 0x79, 0x09, 0x0f}, + } +} diff --git a/u_chrome155_test.go b/u_chrome155_test.go new file mode 100644 index 00000000000..cf1f491c188 --- /dev/null +++ b/u_chrome155_test.go @@ -0,0 +1,320 @@ +// Copyright 2026 The uTLS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package tls + +import ( + "bytes" + "encoding/hex" + "fmt" + "net" + "os" + "reflect" + "slices" + "sort" + "strings" + "testing" +) + +// This parser is deliberately independent of Fingerprinter: comparing the +// captured browser bytes against generated bytes must not hide a parser bug. +// Only extension permutation, GREASE codepoints, ephemeral public keys, and +// GREASE ECH randomness are normalized. Ordered capability lists and trust +// anchor IDs retain their original order and bytes. +type chrome155WireHello struct { + Version uint16 + RandomLength int + SessionIDLength int + CipherSuites []uint16 + CompressionMethods []byte + Extensions map[uint16][]string +} + +type chrome155Reader struct { + t *testing.T + b []byte + label string +} + +func (r *chrome155Reader) take(n int) []byte { + r.t.Helper() + if n < 0 || n > len(r.b) { + r.t.Fatalf("%s: truncated field: need %d bytes, have %d", r.label, n, len(r.b)) + } + b := r.b[:n] + r.b = r.b[n:] + return b +} + +func (r *chrome155Reader) number(n int) int { + r.t.Helper() + v := 0 + for _, b := range r.take(n) { + v = v<<8 | int(b) + } + return v +} + +func (r *chrome155Reader) vector(n int) []byte { + r.t.Helper() + return r.take(r.number(n)) +} + +func (r *chrome155Reader) end() { + r.t.Helper() + if len(r.b) != 0 { + r.t.Fatalf("%s: %d trailing bytes", r.label, len(r.b)) + } +} + +func chrome155NormalizeGREASE(v uint16) uint16 { + if isGREASEUint16(v) { + return GREASE_PLACEHOLDER + } + return v +} + +func chrome155Uint16s(t *testing.T, b []byte) []uint16 { + t.Helper() + if len(b)%2 != 0 { + t.Fatalf("odd uint16 vector length: %d", len(b)) + } + out := make([]uint16, len(b)/2) + for i := range out { + out[i] = chrome155NormalizeGREASE(uint16(b[2*i])<<8 | uint16(b[2*i+1])) + } + return out +} + +func chrome155NormalizeExtension(t *testing.T, id uint16, body []byte) string { + t.Helper() + r := chrome155Reader{t, body, fmt.Sprintf("extension %#04x", id)} + var value string + switch id { + case 10, 13: // supported_groups, signature_algorithms + value = fmt.Sprint(chrome155Uint16s(t, r.vector(2))) + case 43: // supported_versions + value = fmt.Sprint(chrome155Uint16s(t, r.vector(1))) + case 51: // key_share + shares := chrome155Reader{t, r.vector(2), "key_share entries"} + var entries []string + for len(shares.b) > 0 { + group := uint16(shares.number(2)) + key := shares.vector(2) + entry := fmt.Sprintf("%04x/%d", chrome155NormalizeGREASE(group), len(key)) + if isGREASEUint16(group) { + entry += "/" + hex.EncodeToString(key) + } + entries = append(entries, entry) + } + value = strings.Join(entries, ",") + case 0xfe0d: // GREASE encrypted_client_hello + outer, kdf, aead := r.number(1), r.number(2), r.number(2) + r.take(1) // randomized config_id + enc, payload := r.vector(2), r.vector(2) + if outer != 0 || !slices.Contains([]int{144, 176, 208, 240}, len(payload)) { + t.Fatalf("unexpected GREASE ECH shape: outer=%d, payload length=%d", outer, len(payload)) + } + value = fmt.Sprintf("%d/%d/%d/enc=%d/payload=144,176,208,240", outer, kdf, aead, len(enc)) + case 0xca34: // trust_anchors + ids := chrome155Reader{t, r.vector(2), "trust anchor IDs"} + for len(ids.b) > 0 { + if len(ids.vector(1)) == 0 { + t.Fatal("empty trust anchor ID in fixture") + } + } + value = hex.EncodeToString(body) + default: + value = hex.EncodeToString(r.take(len(r.b))) + } + r.end() + return value +} + +func chrome155ParseWire(t *testing.T, data []byte) (chrome155WireHello, []uint16, map[uint16][]byte) { + t.Helper() + r := chrome155Reader{t, data, "ClientHello"} + if len(data) > 0 && data[0] == byte(recordTypeHandshake) { + if r.number(1) != int(recordTypeHandshake) || r.number(2) != int(VersionTLS10) { + t.Fatal("unexpected ClientHello TLS record header") + } + record := r.vector(2) + r.end() + r = chrome155Reader{t, record, "ClientHello handshake"} + } + if r.number(1) != 1 { + t.Fatal("fixture is not a ClientHello") + } + body := r.vector(3) + r.end() + r = chrome155Reader{t, body, "ClientHello body"} + version := uint16(r.number(2)) + random := r.take(32) + sid := r.vector(1) + ciphers := chrome155Uint16s(t, r.vector(2)) + compression := bytes.Clone(r.vector(1)) + extensions := r.vector(2) + r.end() + out := chrome155WireHello{version, len(random), len(sid), ciphers, compression, make(map[uint16][]string)} + r = chrome155Reader{t, extensions, "ClientHello extensions"} + seen, raw := make(map[uint16]bool), make(map[uint16][]byte) + var order []uint16 + for len(r.b) > 0 { + id := uint16(r.number(2)) + body := r.vector(2) + if seen[id] { + t.Fatalf("duplicate extension %#04x", id) + } + seen[id] = true + raw[id] = bytes.Clone(body) + normalizedID := chrome155NormalizeGREASE(id) + order = append(order, normalizedID) + out.Extensions[normalizedID] = append(out.Extensions[normalizedID], chrome155NormalizeExtension(t, id, body)) + } + for _, values := range out.Extensions { + sort.Strings(values) // distinguish the two GREASE bodies independently of their values + } + return out, order, raw +} + +func chrome155Fixture(t *testing.T) []byte { + t.Helper() + text, err := os.ReadFile("testdata/chrome155_clienthello.hex") + if err != nil { + t.Fatal(err) + } + data, err := hex.DecodeString(strings.Join(strings.Fields(string(text)), "")) + if err != nil { + t.Fatal(err) + } + return data +} + +func chrome155FixtureServerName(t *testing.T, raw map[uint16][]byte) string { + t.Helper() + r := chrome155Reader{t, raw[0], "server_name"} + names := chrome155Reader{t, r.vector(2), "server_name list"} + r.end() + if names.number(1) != 0 { + t.Fatal("fixture SNI is not a host name") + } + name := string(names.vector(2)) + names.end() + return name +} + +func chrome155Build(t *testing.T, id ClientHelloID, serverName string, spec *ClientHelloSpec) *UConn { + t.Helper() + client := UClient(&net.TCPConn{}, &Config{ServerName: serverName, ClientSessionCache: nil}, id) + if spec != nil { + if err := client.ApplyPreset(spec); err != nil { + t.Fatal(err) + } + } + if err := client.BuildHandshakeState(); err != nil { + t.Fatal(err) + } + return client +} + +func chrome155CompareWire(t *testing.T, want, got chrome155WireHello) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Errorf("generated ClientHello differs from fresh Chrome 155 capture:\n got: %+v\nwant: %+v", got, want) + } +} + +func TestChrome155CapturedClientHello(t *testing.T) { + if HelloChrome_Auto != HelloChrome_155 { + t.Fatalf("HelloChrome_Auto = %s, want %s", HelloChrome_Auto.Str(), HelloChrome_155.Str()) + } + want, _, captured := chrome155ParseWire(t, chrome155Fixture(t)) + client := chrome155Build(t, HelloChrome_155, chrome155FixtureServerName(t, captured), nil) + got, _, generated := chrome155ParseWire(t, client.HandshakeState.Hello.Raw) + chrome155CompareWire(t, want, got) + if ticket, ok := generated[35]; !ok || len(ticket) != 0 { + t.Fatal("fresh profile must advertise an empty session_ticket extension") + } + if _, ok := generated[41]; ok { + t.Fatal("fresh profile unexpectedly offers pre_shared_key") + } +} + +func TestChrome155DefaultFingerprintRoundTrip(t *testing.T) { + fixture := chrome155Fixture(t) + want, wantOrder, raw := chrome155ParseWire(t, fixture) + spec, err := (&Fingerprinter{}).FingerprintClientHello(fixture) + if err != nil { + t.Fatalf("default Fingerprinter must parse captured Chrome 155 ClientHello: %v", err) + } + client := chrome155Build(t, HelloCustom, chrome155FixtureServerName(t, raw), spec) + got, gotOrder, _ := chrome155ParseWire(t, client.HandshakeState.Hello.Raw) + chrome155CompareWire(t, want, got) + if !slices.Equal(gotOrder, wantOrder) { + t.Errorf("fingerprint roundtrip reordered extensions: got %x, want %x", gotOrder, wantOrder) + } + if chrome155TrustAnchors(t, spec.Extensions) == nil { + t.Fatal("fingerprinted trust anchors must use the typed extension") + } +} + +func chrome155TrustAnchors(t *testing.T, extensions []TLSExtension) *TrustAnchorsExtension { + t.Helper() + for _, extension := range extensions { + if anchors, ok := extension.(*TrustAnchorsExtension); ok { + return anchors + } + } + t.Fatal("missing typed trust_anchors extension") + return nil +} + +func TestChrome155SignatureGREASESeed(t *testing.T) { + client := UClient(&net.TCPConn{}, &Config{ + ServerName: "example.com", + Rand: &incrementingSource{next: 0x81}, + }, HelloChrome_155) + if err := client.BuildHandshakeState(); err != nil { + t.Fatal(err) + } + // BoringSSL reserves seed slot 7 for signature algorithms, independently + // of the cipher, group, extension, and version GREASE slots. + const signatureSeedSlot = 7 + seed := client.greaseSeed[signatureSeedSlot] + want := (seed & 0xf0) | 0x0a + want |= want << 8 + if want == GREASE_PLACEHOLDER { + t.Fatal("deterministic test seed must distinguish generated GREASE from the placeholder") + } + _, _, raw := chrome155ParseWire(t, client.HandshakeState.Hello.Raw) + r := chrome155Reader{t, raw[13], "signature_algorithms"} + signatures := chrome155Reader{t, r.vector(2), "signature schemes"} + r.end() + got := uint16(signatures.number(2)) + if got != want || got == GREASE_PLACEHOLDER { + t.Fatalf("signature GREASE = %#04x, want %#04x from BoringSSL seed slot 7", got, want) + } + if gotState := uint16(client.HandshakeState.Hello.SupportedSignatureAlgorithms[0]); gotState != want { + t.Fatalf("signature GREASE in handshake state = %#04x, want %#04x", gotState, want) + } +} + +func TestChrome133CapabilitiesPreserved(t *testing.T) { + client := chrome155Build(t, HelloChrome_133, "example.com", nil) + _, _, raw := chrome155ParseWire(t, client.HandshakeState.Hello.Raw) + r := chrome155Reader{t, raw[13], "Chrome 133 signature_algorithms"} + got := chrome155Uint16s(t, r.vector(2)) + r.end() + want := []uint16{ + uint16(ECDSAWithP256AndSHA256), uint16(PSSWithSHA256), uint16(PKCS1WithSHA256), + uint16(ECDSAWithP384AndSHA384), uint16(PSSWithSHA384), uint16(PKCS1WithSHA384), + uint16(PSSWithSHA512), uint16(PKCS1WithSHA512), + } + if !slices.Equal(got, want) { + t.Fatalf("Chrome 133 signature schemes changed: got %x, want original classical schemes %x", got, want) + } + if _, ok := raw[0xca34]; ok { + t.Fatal("Chrome 155 trust anchor IDs leaked into the Chrome 133 profile") + } +} diff --git a/u_common.go b/u_common.go index a4db5df94a7..dd134984964 100644 --- a/u_common.go +++ b/u_common.go @@ -41,6 +41,7 @@ const ( utlsFakeExtensionCustom uint16 = 1234 // not IANA assigned, for ALPS utlsExtensionECH uint16 = 0xfe0d // draft-ietf-tls-esni-17 utlsExtensionECHOuterExtensions uint16 = 0xfd00 // draft-ietf-tls-esni-17 + utlsExtensionTrustAnchors uint16 = 0xca34 // draft-ietf-tls-trust-anchor-ids // extensions with 'fake' prefix break connection, if server echoes them back fakeExtensionEncryptThenMAC uint16 = 22 @@ -626,7 +627,7 @@ var ( HelloFirefox_120 = ClientHelloID{helloFirefox, "120", nil, nil} HelloFirefox_148 = ClientHelloID{helloFirefox, "148", nil, nil} - HelloChrome_Auto = HelloChrome_133 + HelloChrome_Auto = HelloChrome_155 HelloChrome_58 = ClientHelloID{helloChrome, "58", nil, nil} HelloChrome_62 = ClientHelloID{helloChrome, "62", nil, nil} HelloChrome_70 = ClientHelloID{helloChrome, "70", nil, nil} @@ -658,6 +659,9 @@ var ( HelloChrome_131 = ClientHelloID{helloChrome, "131", nil, nil} // Chrome w/ New ALPS codepoint HelloChrome_133 = ClientHelloID{helloChrome, "133", nil, nil} + // Chrome 155.0.8059.40 on macOS, with ML-DSA signatures and a snapshot of + // the Chrome Root Store trust anchor IDs. See testdata/chrome155_clienthello.md. + HelloChrome_155 = ClientHelloID{helloChrome, "155", nil, nil} HelloIOS_Auto = HelloIOS_14 HelloIOS_11_1 = ClientHelloID{helloIOS, "111", nil, nil} // legacy "111" means 11.1 diff --git a/u_conn.go b/u_conn.go index d831406f1e4..4898c447965 100644 --- a/u_conn.go +++ b/u_conn.go @@ -38,7 +38,7 @@ type UConn struct { HandshakeState PubClientHandshakeState - greaseSeed [ssl_grease_last_index]uint16 + greaseSeed [ssl_grease_seed_count]uint16 omitSNIExtension bool diff --git a/u_grease_api_test.go b/u_grease_api_test.go new file mode 100644 index 00000000000..d2088bb6283 --- /dev/null +++ b/u_grease_api_test.go @@ -0,0 +1,21 @@ +package tls_test + +import ( + "testing" + + tls "github.com/metacubex/utls" +) + +// The public helper's five-element array remains source-compatible when the +// internal GREASE seed gains slots for additional ClientHello fields. +var _ func([5]uint16, int) uint16 = tls.GetBoringGREASEValue + +func TestBoringGREASEPublicAPI(t *testing.T) { + seed := [5]uint16{0x0000, 0x0012, 0x0080, 0x00f0, 0xffe1} + want := [5]uint16{0x0a0a, 0x1a1a, 0x8a8a, 0xfafa, 0xeaea} + for i, expected := range want { + if got := tls.GetBoringGREASEValue(seed, i); got != expected { + t.Fatalf("GREASE index %d = %#04x; want %#04x", i, got, expected) + } + } +} diff --git a/u_mldsa_json_test.go b/u_mldsa_json_test.go new file mode 100644 index 00000000000..a660e89eca5 --- /dev/null +++ b/u_mldsa_json_test.go @@ -0,0 +1,54 @@ +package tls + +import ( + "encoding/json" + "fmt" + "slices" + "testing" + + "github.com/metacubex/utls/dicttls" +) + +func TestMLDSASignatureSchemeJSON(t *testing.T) { + for _, tc := range []struct { + name string + scheme SignatureScheme + }{ + {"mldsa44", MLDSA44}, + {"mldsa65", MLDSA65}, + {"mldsa87", MLDSA87}, + } { + if got := dicttls.DictSignatureSchemeNameIndexed[tc.name]; got != uint16(tc.scheme) { + t.Fatalf("signature scheme %s = %#04x, want %#04x", tc.name, got, tc.scheme) + } + if got := dicttls.DictSignatureSchemeValueIndexed[uint16(tc.scheme)]; got != tc.name { + t.Fatalf("signature scheme %#04x = %s, want %s", tc.scheme, got, tc.name) + } + } + for _, name := range []string{"signature_algorithms", "signature_algorithms_cert"} { + t.Run(name, func(t *testing.T) { + data := fmt.Sprintf(`[{"name":%q,"supported_signature_algorithms":["GREASE","mldsa44","mldsa65","mldsa87"]}]`, name) + var extensions TLSExtensionsJSONUnmarshaler + if err := json.Unmarshal([]byte(data), &extensions); err != nil { + t.Fatal(err) + } + exts := extensions.Extensions() + if len(exts) != 1 { + t.Fatalf("parsed %d extensions, want 1", len(exts)) + } + var got []SignatureScheme + switch ext := exts[0].(type) { + case *SignatureAlgorithmsExtension: + got = ext.SupportedSignatureAlgorithms + case *SignatureAlgorithmsCertExtension: + got = ext.SupportedSignatureAlgorithms + default: + t.Fatalf("unexpected extension type %T", ext) + } + want := []SignatureScheme{GREASE_PLACEHOLDER, MLDSA44, MLDSA65, MLDSA87} + if !slices.Equal(got, want) { + t.Fatalf("signature algorithms = %x, want %x", got, want) + } + }) + } +} diff --git a/u_parrots.go b/u_parrots.go index 968ad08e612..bab56d632b9 100644 --- a/u_parrots.go +++ b/u_parrots.go @@ -969,6 +969,84 @@ func utlsIdToSpec(id ClientHelloID) (ClientHelloSpec, error) { &UtlsGREASEExtension{}, }), }, nil + case HelloChrome_155: + return ClientHelloSpec{ + CipherSuites: []uint16{ + GREASE_PLACEHOLDER, + TLS_AES_128_GCM_SHA256, + TLS_AES_256_GCM_SHA384, + TLS_CHACHA20_POLY1305_SHA256, + TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, + TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, + TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, + TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, + TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, + TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, + TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, + TLS_RSA_WITH_AES_128_GCM_SHA256, + TLS_RSA_WITH_AES_256_GCM_SHA384, + TLS_RSA_WITH_AES_128_CBC_SHA, + TLS_RSA_WITH_AES_256_CBC_SHA, + }, + CompressionMethods: []byte{ + 0x00, // compressionNone + }, + Extensions: ShuffleChromeTLSExtensions([]TLSExtension{ + &UtlsGREASEExtension{}, + &SNIExtension{}, + &ExtendedMasterSecretExtension{}, + &RenegotiationInfoExtension{Renegotiation: RenegotiateOnceAsClient}, + &SupportedCurvesExtension{[]CurveID{ + GREASE_PLACEHOLDER, + X25519MLKEM768, + X25519, + CurveP256, + CurveP384, + }}, + &SupportedPointsExtension{SupportedPoints: []byte{ + 0x00, // pointFormatUncompressed + }}, + &SessionTicketExtension{}, + &ALPNExtension{AlpnProtocols: []string{"h2", "http/1.1"}}, + &StatusRequestExtension{}, + &SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []SignatureScheme{ + GREASE_PLACEHOLDER, + MLDSA44, + MLDSA65, + MLDSA87, + ECDSAWithP256AndSHA256, + PSSWithSHA256, + PKCS1WithSHA256, + ECDSAWithP384AndSHA384, + PSSWithSHA384, + PKCS1WithSHA384, + PSSWithSHA512, + PKCS1WithSHA512, + }}, + &SCTExtension{}, + &KeyShareExtension{[]KeyShare{ + {Group: CurveID(GREASE_PLACEHOLDER), Data: []byte{0}}, + {Group: X25519MLKEM768}, + {Group: X25519}, + }}, + &PSKKeyExchangeModesExtension{[]uint8{ + PskModeDHE, + }}, + &SupportedVersionsExtension{[]uint16{ + GREASE_PLACEHOLDER, + VersionTLS13, + VersionTLS12, + }}, + &UtlsCompressCertExtension{[]CertCompressionAlgo{ + CertCompressionBrotli, + }}, + &ApplicationSettingsExtensionNew{SupportedProtocols: []string{"h2"}}, + &TrustAnchorsExtension{TrustAnchorIDs: chrome155TrustAnchorIDs()}, + BoringGREASEECH(), + &UtlsGREASEExtension{}, + }), + }, nil case HelloFirefox_55, HelloFirefox_56: return ClientHelloSpec{ TLSVersMax: VersionTLS12, @@ -3052,7 +3130,7 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { } // Currently, GREASE is assumed to come from BoringSSL - grease_bytes := make([]byte, 2*ssl_grease_last_index) + grease_bytes := make([]byte, 2*ssl_grease_seed_count) grease_extensions_seen := 0 _, err = io.ReadFull(uconn.config.rand(), grease_bytes) if err != nil { @@ -3061,7 +3139,7 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { for i := range uconn.greaseSeed { uconn.greaseSeed[i] = binary.LittleEndian.Uint16(grease_bytes[2*i : 2*i+2]) } - if GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_extension1) == GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_extension2) { + if getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_extension1) == getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_extension2) { uconn.greaseSeed[ssl_grease_extension2] ^= 0x1010 } @@ -3069,7 +3147,7 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { copy(hello.CipherSuites, p.CipherSuites) for i := range hello.CipherSuites { if isGREASEUint16(hello.CipherSuites[i]) { // just in case the user set a GREASE value instead of unGREASEd - hello.CipherSuites[i] = GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_cipher) + hello.CipherSuites[i] = getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_cipher) } } @@ -3106,9 +3184,9 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { case *UtlsGREASEExtension: switch grease_extensions_seen { case 0: - ext.Value = GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_extension1) + ext.Value = getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_extension1) case 1: - ext.Value = GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_extension2) + ext.Value = getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_extension2) ext.Body = []byte{0} default: return errors.New("at most 2 grease extensions are supported") @@ -3117,7 +3195,13 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { case *SupportedCurvesExtension: for i := range ext.Curves { if isGREASEUint16(uint16(ext.Curves[i])) { - ext.Curves[i] = CurveID(GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_group)) + ext.Curves[i] = CurveID(getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_group)) + } + } + case *SignatureAlgorithmsExtension: + for i, scheme := range ext.SupportedSignatureAlgorithms { + if isGREASEUint16(uint16(scheme)) { + ext.SupportedSignatureAlgorithms[i] = SignatureScheme(getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_signature_algorithm)) } } case *KeyShareExtension: @@ -3126,7 +3210,7 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { for i := range ext.KeyShares { curveID := ext.KeyShares[i].Group if isGREASEUint16(uint16(curveID)) { // just in case the user set a GREASE value instead of unGREASEd - ext.KeyShares[i].Group = CurveID(GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_group)) + ext.KeyShares[i].Group = CurveID(getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_group)) continue } if len(ext.KeyShares[i].Data) > 1 { @@ -3215,7 +3299,7 @@ func (uconn *UConn) ApplyPreset(p *ClientHelloSpec) error { case *SupportedVersionsExtension: for i := range ext.Versions { if isGREASEUint16(ext.Versions[i]) { // just in case the user set a GREASE value instead of unGREASEd - ext.Versions[i] = GetBoringGREASEValue(uconn.greaseSeed, ssl_grease_version) + ext.Versions[i] = getBoringGREASEValue(uconn.greaseSeed[:], ssl_grease_version) } } case *NPNExtension: diff --git a/u_tls_extensions.go b/u_tls_extensions.go index 77ee10ce684..8efe37b3e88 100644 --- a/u_tls_extensions.go +++ b/u_tls_extensions.go @@ -79,6 +79,8 @@ func ExtensionFromID(id uint16) TLSExtension { return &FakeChannelIDExtension{} case utlsExtensionECH: return &GREASEEncryptedClientHelloExtension{} + case utlsExtensionTrustAnchors: + return &TrustAnchorsExtension{} case extensionRenegotiationInfo: return &RenegotiationInfoExtension{} default: @@ -966,9 +968,14 @@ const ( ssl_grease_extension2 ssl_grease_version ssl_grease_ticket_extension - ssl_grease_last_index = ssl_grease_ticket_extension + ssl_grease_ech_config_id + ssl_grease_signature_algorithm + ssl_grease_seed_count ) +// Preserve the array size accepted by the exported GetBoringGREASEValue helper. +const ssl_grease_last_index = ssl_grease_ticket_extension + // it is responsibility of user not to generate multiple grease extensions with same value type UtlsGREASEExtension struct { Value uint16 @@ -981,6 +988,10 @@ func (e *UtlsGREASEExtension) writeToUConn(uc *UConn) error { // will panic if ssl_grease_last_index[index] is out of bounds. func GetBoringGREASEValue(greaseSeed [ssl_grease_last_index]uint16, index int) uint16 { + return getBoringGREASEValue(greaseSeed[:], index) +} + +func getBoringGREASEValue(greaseSeed []uint16, index int) uint16 { // GREASE value is back from deterministic to random. // https://github.com/google/boringssl/blob/a365138ac60f38b64bfc608b493e0f879845cb88/ssl/handshake_client.c#L530 ret := uint16(greaseSeed[index]) diff --git a/u_trust_anchors.go b/u_trust_anchors.go new file mode 100644 index 00000000000..4fc9fb4e989 --- /dev/null +++ b/u_trust_anchors.go @@ -0,0 +1,103 @@ +package tls + +import ( + "encoding/json" + "errors" + "fmt" + "io" + + "golang.org/x/crypto/cryptobyte" +) + +// TrustAnchorsExtension implements the draft trust_anchors ClientHello extension +// (0xca34). Each TrustAnchorID contains 1 to 255 bytes; an empty list is valid. +// The IDs are advertised to the server and do not change Config.RootCAs. +type TrustAnchorsExtension struct { + TrustAnchorIDs [][]byte `json:"trust_anchor_ids"` +} + +func (e *TrustAnchorsExtension) writeToUConn(_ *UConn) error { + return nil +} + +func (e *TrustAnchorsExtension) Len() int { + n := 6 // Extension header and RequestedTrustAnchorList length. + for _, id := range e.TrustAnchorIDs { + n += 1 + len(id) + } + return n +} + +func (e *TrustAnchorsExtension) validate() error { + listLen := 0 + for _, id := range e.TrustAnchorIDs { + if len(id) == 0 || len(id) > 255 { + return fmt.Errorf("tls: trust anchor ID length %d is outside 1..255", len(id)) + } + listLen += 1 + len(id) + // The extension_data length also includes the two-byte list length. + if listLen > (1<<16)-3 { + return errors.New("tls: trust anchors extension data exceeds 65535 bytes") + } + } + return nil +} + +func (e *TrustAnchorsExtension) Read(b []byte) (int, error) { + if err := e.validate(); err != nil { + return 0, err + } + if len(b) < e.Len() { + return 0, io.ErrShortBuffer + } + listLen := e.Len() - 6 + b[0] = byte(utlsExtensionTrustAnchors >> 8) + b[1] = byte(utlsExtensionTrustAnchors & 0xff) + b[2] = byte((2 + listLen) >> 8) + b[3] = byte(2 + listLen) + b[4] = byte(listLen >> 8) + b[5] = byte(listLen) + offset := 6 + for _, id := range e.TrustAnchorIDs { + b[offset] = byte(len(id)) + offset++ + offset += copy(b[offset:], id) + } + return offset, io.EOF +} + +func (e *TrustAnchorsExtension) Write(b []byte) (int, error) { + if len(b) > (1<<16)-1 { + return 0, errors.New("tls: trust anchors extension data exceeds 65535 bytes") + } + extData := cryptobyte.String(b) + var list cryptobyte.String + if !extData.ReadUint16LengthPrefixed(&list) || !extData.Empty() { + return 0, errors.New("tls: unable to read trust anchors extension data") + } + ids := make([][]byte, 0) + for !list.Empty() { + var id cryptobyte.String + if !list.ReadUint8LengthPrefixed(&id) || id.Empty() { + return 0, errors.New("tls: unable to read trust anchor ID") + } + ids = append(ids, append([]byte(nil), id...)) + } + e.TrustAnchorIDs = ids + return len(b), nil +} + +func (e *TrustAnchorsExtension) UnmarshalJSON(data []byte) error { + var value struct { + TrustAnchorIDs [][]byte `json:"trust_anchor_ids"` + } + if err := json.Unmarshal(data, &value); err != nil { + return err + } + parsed := TrustAnchorsExtension{TrustAnchorIDs: value.TrustAnchorIDs} + if err := parsed.validate(); err != nil { + return err + } + e.TrustAnchorIDs = parsed.TrustAnchorIDs + return nil +} diff --git a/u_trust_anchors_test.go b/u_trust_anchors_test.go new file mode 100644 index 00000000000..e1271fb1300 --- /dev/null +++ b/u_trust_anchors_test.go @@ -0,0 +1,173 @@ +package tls + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "reflect" + "testing" +) + +func TestTrustAnchorsExtensionRoundTrip(t *testing.T) { + for _, tc := range []struct { + name string + ids [][]byte + wire []byte + }{ + {"empty", nil, []byte{0xca, 0x34, 0, 2, 0, 0}}, + {"single", [][]byte{{0x42}}, []byte{0xca, 0x34, 0, 4, 0, 2, 1, 0x42}}, + {"multiple", [][]byte{{1, 2}, {3}}, []byte{0xca, 0x34, 0, 7, 0, 5, 2, 1, 2, 1, 3}}, + {"maximum ID", [][]byte{bytes.Repeat([]byte{0x81}, 255)}, nil}, + } { + t.Run(tc.name, func(t *testing.T) { + ext := &TrustAnchorsExtension{TrustAnchorIDs: tc.ids} + wire := make([]byte, ext.Len()) + if n, err := ext.Read(wire); n != len(wire) || err != io.EOF { + t.Fatalf("Read = %d, %v; want %d, EOF", n, err, len(wire)) + } + if tc.wire != nil && !bytes.Equal(wire, tc.wire) { + t.Fatalf("wire = %x; want %x", wire, tc.wire) + } + parsed, ok := ExtensionFromID(utlsExtensionTrustAnchors).(*TrustAnchorsExtension) + if !ok { + t.Fatal("trust_anchors is not registered with the fingerprint parser") + } + if n, err := parsed.Write(wire[4:]); n != len(wire)-4 || err != nil { + t.Fatalf("Write = %d, %v; want %d, nil", n, err, len(wire)-4) + } + if len(parsed.TrustAnchorIDs) != len(tc.ids) { + t.Fatalf("parsed %d IDs; want %d", len(parsed.TrustAnchorIDs), len(tc.ids)) + } + for i, id := range tc.ids { + if !bytes.Equal(parsed.TrustAnchorIDs[i], id) { + t.Fatalf("ID %d = %x; want %x", i, parsed.TrustAnchorIDs[i], id) + } + } + encoded := make([]byte, parsed.Len()) + if _, err := parsed.Read(encoded); err != io.EOF { + t.Fatal(err) + } + if !bytes.Equal(encoded, wire) { + t.Fatalf("roundtrip changed wire: %x != %x", encoded, wire) + } + }) + } +} + +func TestTrustAnchorsExtensionParseDoesNotAliasInput(t *testing.T) { + data := []byte{0, 5, 2, 1, 2, 1, 3} + ext := &TrustAnchorsExtension{} + if _, err := ext.Write(data); err != nil { + t.Fatal(err) + } + data[3] = 0xff + if ext.TrustAnchorIDs[0][0] != 1 { + t.Fatal("parsed ID aliases input") + } + ext.TrustAnchorIDs[1][0] = 0xfe + if data[6] != 3 { + t.Fatal("input aliases parsed ID") + } +} + +func TestTrustAnchorsExtensionRejectsMalformedData(t *testing.T) { + for _, data := range [][]byte{ + nil, + {0}, // Truncated list length. + {0, 1}, // Truncated list. + {0, 3, 1, 1}, // List length exceeds available data. + {0, 1, 0}, // Empty ID. + {0, 2, 2, 1}, // Truncated ID. + {0, 0, 1}, // Trailing bytes after the list. + {0, 2, 1, 1, 2}, // Trailing bytes after a nonempty list. + } { + t.Run(fmt.Sprintf("%x", data), func(t *testing.T) { + ext := &TrustAnchorsExtension{TrustAnchorIDs: [][]byte{{0x42}}} + if n, err := ext.Write(data); n != 0 || err == nil { + t.Fatalf("Write = %d, %v; want 0, error", n, err) + } + if !reflect.DeepEqual(ext.TrustAnchorIDs, [][]byte{{0x42}}) { + t.Fatal("failed parsing modified existing IDs") + } + }) + } +} + +func TestTrustAnchorsExtensionEncodingLimits(t *testing.T) { + ids := make([][]byte, 256) + for i := range ids[:255] { + ids[i] = bytes.Repeat([]byte{byte(i)}, 255) + } + ids[255] = bytes.Repeat([]byte{0xff}, 252) + ext := &TrustAnchorsExtension{TrustAnchorIDs: ids} + wire := make([]byte, ext.Len()) + if _, err := ext.Read(wire); err != io.EOF { + t.Fatalf("maximum extension_data failed: %v", err) + } + if len(wire)-4 != 65535 || wire[2] != 0xff || wire[3] != 0xff { + t.Fatalf("maximum extension_data length = %d; header = %x", len(wire)-4, wire[:4]) + } + parsed := &TrustAnchorsExtension{} + if _, err := parsed.Write(wire[4:]); err != nil { + t.Fatalf("maximum extension_data parsing failed: %v", err) + } + for _, ids := range [][][]byte{ + {nil}, + {{}}, + {make([]byte, 256)}, + append(ids[:255:255], make([]byte, 253)), // One byte over the extension_data limit. + } { + ext := &TrustAnchorsExtension{TrustAnchorIDs: ids} + if n, err := ext.Read(make([]byte, ext.Len())); n != 0 || err == nil { + t.Fatalf("invalid IDs Read = %d, %v; want 0, error", n, err) + } + } + if n, err := parsed.Write(make([]byte, 65536)); n != 0 || err == nil { + t.Fatalf("oversize input Write = %d, %v; want 0, error", n, err) + } + if n, err := ext.Read(make([]byte, ext.Len()-1)); n != 0 || err != io.ErrShortBuffer { + t.Fatalf("short buffer Read = %d, %v; want 0, ErrShortBuffer", n, err) + } +} + +func TestTrustAnchorsExtensionJSON(t *testing.T) { + var extensions TLSExtensionsJSONUnmarshaler + if err := json.Unmarshal([]byte(`[{"name":"trust_anchors","trust_anchor_ids":["AQI=","/w=="]}]`), &extensions); err != nil { + t.Fatal(err) + } + exts := extensions.Extensions() + if len(exts) != 1 { + t.Fatalf("parsed %d extensions; want 1", len(exts)) + } + ext, ok := exts[0].(*TrustAnchorsExtension) + if !ok || !reflect.DeepEqual(ext.TrustAnchorIDs, [][]byte{{1, 2}, {0xff}}) { + t.Fatalf("JSON extension = %#v", exts[0]) + } + encoded, err := json.Marshal(ext) + if err != nil { + t.Fatal(err) + } + var roundtrip TrustAnchorsExtension + if err := json.Unmarshal(encoded, &roundtrip); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(roundtrip.TrustAnchorIDs, ext.TrustAnchorIDs) { + t.Fatalf("JSON roundtrip changed IDs: got %x, want %x", roundtrip.TrustAnchorIDs, ext.TrustAnchorIDs) + } + for _, data := range []string{ + `{"trust_anchor_ids":[""]}`, + `{"trust_anchor_ids":[null]}`, + `{"trust_anchor_ids":["invalid base64"]}`, + } { + if err := json.Unmarshal([]byte(data), ext); err == nil { + t.Fatalf("invalid JSON accepted: %s", data) + } + if !reflect.DeepEqual(ext.TrustAnchorIDs, [][]byte{{1, 2}, {0xff}}) { + t.Fatal("failed JSON parsing modified existing IDs") + } + } + if err := json.Unmarshal([]byte(`{"trust_anchor_ids":[]}`), ext); err != nil || len(ext.TrustAnchorIDs) != 0 { + t.Fatalf("empty JSON list = %#v, %v", ext.TrustAnchorIDs, err) + } +}