Repository navigation
Expand file tree
/
Copy pathu_ech_psk_test.go
More file actions
150 lines (142 loc) · 5.02 KB
/
Copy pathu_ech_psk_test.go
File metadata and controls
150 lines (142 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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"
"crypto/x509"
"encoding/hex"
"testing"
"time"
)
const utlsTestECHConfigsHex = "0045fe0d0041590020002092a01233db2218518ccbbbbc24df20686af417b37388de6460e94011974777090004000100010012636c6f7564666c6172652d6563682e636f6d0000"
const utlsTestCustomECHPSKError = "tls: PSK resumption with a custom ECH ClientHello is not supported"
func TestUTLSECHPreSharedKeyPrivacy(t *testing.T) {
// This is the supported X25519 ECHConfigList used in ech_test.go. A
// server private key is unnecessary when inspecting ClientHelloOuter.
echConfigs, err := hex.DecodeString(utlsTestECHConfigsHex)
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
realECH bool
populated bool
wantReject bool
}{
{name: "RealECHWithPSK", realECH: true, populated: true, wantReject: true},
{name: "RealECHWithEmptyPSK", realECH: true},
{name: "GREASEECHWithPSK", populated: true},
} {
t.Run(tc.name, func(t *testing.T) {
config := &Config{ServerName: "private.example", InsecureSkipVerify: true, OmitEmptyPsk: true}
if tc.realECH {
config.EncryptedClientHelloConfigList = echConfigs
}
client := UClient(nil, config, HelloChrome_120)
if err := client.BuildHandshakeStateWithoutSession(); err != nil {
t.Fatal(err)
}
label := []byte("private-session-ticket-visible-in-ech-outer")
psk := &UtlsPreSharedKeyExtension{OmitEmptyPsk: true}
if tc.populated {
// Model an initialized extension after the session controller has
// loaded a ticket, immediately before ClientHello serialization.
psk.PreSharedKeyCommon = PreSharedKeyCommon{
Session: &SessionState{version: VersionTLS13, cipherSuite: TLS_AES_128_GCM_SHA256},
Identities: []PskIdentity{{Label: label}},
Binders: [][]byte{make([]byte, 32)},
}
}
client.Extensions = append(client.Extensions, psk)
err := client.MarshalClientHello()
if tc.wantReject {
if err == nil {
t.Fatalf("real ECH with PSK must fail explicitly until inner binders are supported; ticket exposed in outer: %v", bytes.Contains(client.HandshakeState.Hello.Raw, label))
}
if err.Error() != utlsTestCustomECHPSKError {
t.Fatalf("expected explicit ECH/PSK error, got %v", err)
}
return
}
if err != nil {
t.Fatal(err)
}
outer := new(clientHelloMsg)
if !outer.unmarshal(client.HandshakeState.Hello.Raw) {
t.Fatal("invalid marshaled ClientHello")
}
if len(outer.encryptedClientHello) == 0 {
t.Fatal("missing ECH extension")
}
if tc.populated {
if len(outer.pskIdentities) != 1 || !bytes.Equal(outer.pskIdentities[0].label, label) {
t.Fatal("GREASE ECH removed the ordinary PSK identity")
}
} else if len(outer.pskIdentities) != 0 {
t.Fatal("empty PSK extension was not omitted")
}
})
}
}
func TestUTLSECHCachedSessionPrivacy(t *testing.T) {
echConfigs, err := hex.DecodeString(utlsTestECHConfigsHex)
if err != nil {
t.Fatal(err)
}
for _, realECH := range []bool{false, true} {
name := "GREASEECH"
if realECH {
name = "RealECH"
}
t.Run(name, func(t *testing.T) {
config := testConfigClient.Clone()
config.ServerName = "private.example"
config.InsecureSkipVerify = true
config.ClientSessionCache = NewLRUClientSessionCache(1)
config.OmitEmptyPsk = true
if realECH {
config.EncryptedClientHelloConfigList = echConfigs
}
label := []byte("cached-private-session-ticket")
config.ClientSessionCache.Put(config.ServerName, &ClientSessionState{session: &SessionState{
version: VersionTLS13,
cipherSuite: TLS_AES_128_GCM_SHA256,
createdAt: uint64(config.time().Unix()),
useBy: uint64(config.time().Add(time.Hour).Unix()),
secret: bytes.Repeat([]byte{1}, 32),
peerCertificates: []*x509.Certificate{testECDSAP256Cert.Leaf},
ticket: label,
}})
preset, err := UTLSIdToSpec(HelloChrome_120)
if err != nil {
t.Fatal(err)
}
preset.Extensions = append(preset.Extensions, &UtlsPreSharedKeyExtension{})
client := UClient(nil, config, HelloCustom)
if err := client.ApplyPreset(&preset); err != nil {
t.Fatal(err)
}
err = client.BuildHandshakeState()
if realECH {
if err == nil || err.Error() != utlsTestCustomECHPSKError {
t.Fatalf("cached session with real ECH must fail before patching binders, got %v", err)
}
return
}
if err != nil {
t.Fatal(err)
}
outer := new(clientHelloMsg)
if !outer.unmarshal(client.HandshakeState.Hello.Raw) {
t.Fatal("invalid GREASE ECH ClientHello")
}
if len(outer.pskIdentities) != 1 || !bytes.Equal(outer.pskIdentities[0].label, label) {
t.Fatal("GREASE ECH did not load the cached session")
}
if len(outer.pskBinders) != 1 || len(outer.pskBinders[0]) != 32 || bytes.Equal(outer.pskBinders[0], make([]byte, 32)) {
t.Fatal("GREASE ECH did not compute the cached session's binder")
}
})
}
}