Repository navigation
Expand file tree
/
Copy pathu_go127_test.go
More file actions
367 lines (350 loc) · 12.8 KB
/
Copy pathu_go127_test.go
File metadata and controls
367 lines (350 loc) · 12.8 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// 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"
"context"
"crypto/ecdh"
"crypto/fips140"
"crypto/mldsa"
"crypto/rand"
"fmt"
"io"
"net"
"slices"
"testing"
"golang.org/x/crypto/cryptobyte"
)
func TestUQUICDefaultMinVersion(t *testing.T) {
for _, helloID := range []ClientHelloID{HelloGolang, HelloCustom} {
t.Run(helloID.Str(), func(t *testing.T) {
infoConn, peerConn := net.Pipe()
defer infoConn.Close()
defer peerConn.Close()
clientConfig := testConfigClient.Clone()
clientConfig.NextProtos = []string{"h3"}
client := UQUICClient(&QUICConfig{
TLSConfig: clientConfig,
ClientHelloInfoConn: infoConn,
}, helloID)
defer client.Close()
if client.conn.quic.clientHelloInfoConn != infoConn {
t.Fatal("UQUICClient did not retain ClientHelloInfoConn")
}
if helloID == HelloCustom {
preset := go127ClientHelloSpec([]SignatureScheme{ECDSAWithP256AndSHA256})
preset.Extensions = append(preset.Extensions,
&ALPNExtension{AlpnProtocols: []string{"h3"}},
&QUICTransportParametersExtension{},
)
if err := client.ApplyPreset(preset); err != nil {
t.Fatal(err)
}
}
client.SetTransportParameters(nil)
serverConfig := testConfigServer.Clone()
serverConfig.NextProtos = []string{"h3"}
var callbackCalled bool
serverConfig.GetConfigForClient = func(info *ClientHelloInfo) (*Config, error) {
callbackCalled = true
if info.Conn != infoConn {
t.Error("ClientHelloInfo.Conn did not use ClientHelloInfoConn")
}
if !slices.Equal(info.SupportedVersions, []uint16{VersionTLS13}) {
t.Errorf("QUIC advertised versions %x, want TLS 1.3 only", info.SupportedVersions)
}
return nil, nil
}
server := QUICServer(&QUICConfig{
TLSConfig: serverConfig,
ClientHelloInfoConn: infoConn,
})
defer server.Close()
server.SetTransportParameters(nil)
// Exercise UQUICConn's event methods as well as its custom handshake.
type quicEndpoint interface {
Start(context.Context) error
NextEvent() QUICEvent
HandleData(QUICEncryptionLevel, []byte) error
ConnectionState() ConnectionState
}
endpoints := []quicEndpoint{client, server}
for _, endpoint := range endpoints {
if err := endpoint.Start(t.Context()); err != nil {
t.Fatal(err)
}
}
var complete, gotParams [2]bool
for current, idle := 0, 0; idle < 2; {
event := endpoints[current].NextEvent()
if event.Kind == QUICNoEvent {
idle++
current = 1 - current
continue
}
idle = 0
switch event.Kind {
case QUICWriteData:
if err := endpoints[1-current].HandleData(event.Level, event.Data); err != nil {
t.Fatal(err)
}
case QUICTransportParameters:
gotParams[current] = true
case QUICHandshakeDone:
complete[current] = true
case QUICSetReadSecret, QUICSetWriteSecret:
default:
t.Fatalf("unexpected QUIC event: %+v", event)
}
}
for i, endpoint := range endpoints {
state := endpoint.ConnectionState()
if !complete[i] || !state.HandshakeComplete || state.Version != VersionTLS13 || state.NegotiatedProtocol != "h3" || !gotParams[i] {
t.Errorf("endpoint %d: complete=%v, state=%+v, got transport parameters=%v", i, complete[i], state, gotParams[i])
}
}
if !callbackCalled {
t.Error("GetConfigForClient was not called")
}
})
}
}
func TestUTLSMLDSAAndLocalCertificate(t *testing.T) {
if fips140.Version() == "v1.0.0" {
t.Skip("ML-DSA is unavailable in FIPS 140-3 module v1.0.0")
}
for _, tc := range []struct {
scheme SignatureScheme
serverCert Certificate
clientCert Certificate
}{
{MLDSA44, testMLDSA44Cert, testClientMLDSA44Cert},
{MLDSA65, testMLDSA65Cert, testClientMLDSA65Cert},
{MLDSA87, testMLDSA87Cert, testClientMLDSA87Cert},
} {
t.Run(tc.scheme.String(), func(t *testing.T) {
clientConfig := testConfigClient.Clone()
clientConfig.Certificates = []Certificate{tc.clientCert}
serverConfig := testConfigServer.Clone()
serverConfig.Certificates = []Certificate{tc.serverCert}
serverConfig.ClientAuth = RequireAndVerifyClientCert
serverState, clientState, err := testUtlsHandshake(t, clientConfig, serverConfig,
go127ClientHelloSpec([]SignatureScheme{tc.scheme, ECDSAWithP256AndSHA256}))
if err != nil {
t.Fatalf("custom ML-DSA handshake: %v", err)
}
for _, state := range []ConnectionState{clientState, serverState} {
if !state.HandshakeComplete || state.Version != VersionTLS13 {
t.Fatalf("handshake did not complete with TLS 1.3: %+v", state)
}
if len(state.PeerCertificates) == 0 {
t.Fatal("missing peer certificate")
}
if _, ok := state.PeerCertificates[0].PublicKey.(*mldsa.PublicKey); !ok {
t.Errorf("peer public key = %T, want *mldsa.PublicKey", state.PeerCertificates[0].PublicKey)
}
}
if !slices.EqualFunc(clientState.LocalCertificate, tc.clientCert.Certificate, bytes.Equal) {
t.Error("UConn.ConnectionState.LocalCertificate does not match the sent client certificate")
}
if !slices.EqualFunc(serverState.LocalCertificate, tc.serverCert.Certificate, bytes.Equal) {
t.Error("server ConnectionState.LocalCertificate does not match the sent server certificate")
}
})
}
}
func TestUTLSMLKEM1024(t *testing.T) {
for _, helloID := range []ClientHelloID{HelloGolang, HelloCustom} {
for _, retry := range []bool{false, true} {
name := helloID.Str() + "/Direct"
if retry {
name = helloID.Str() + "/HelloRetryRequest"
}
t.Run(name, func(t *testing.T) {
clientConfig := testConfigClient.Clone()
clientConfig.MinVersion = VersionTLS13
clientConfig.CurvePreferences = []CurveID{MLKEM1024}
initialGroup := MLKEM1024
if retry {
initialGroup = X25519
clientConfig.CurvePreferences = []CurveID{X25519}
}
serverConfig := testConfigServer.Clone()
serverConfig.CurvePreferences = []CurveID{MLKEM1024}
serverConfig.SessionTicketsDisabled = true
clientConn, serverConn := localPipe(t)
defer clientConn.Close()
defer serverConn.Close()
client := UClient(clientConn, clientConfig, helloID)
if helloID == HelloCustom {
preset := go127ClientHelloSpec([]SignatureScheme{ECDSAWithP256AndSHA256})
for _, extension := range preset.Extensions {
switch extension := extension.(type) {
case *SupportedCurvesExtension:
extension.Curves = []CurveID{initialGroup}
if retry {
extension.Curves = append(extension.Curves, MLKEM1024)
}
case *KeyShareExtension:
extension.KeyShares = []KeyShare{{Group: initialGroup}}
}
}
if err := client.ApplyPreset(preset); err != nil {
t.Fatal(err)
}
}
if err := client.BuildHandshakeState(); err != nil {
t.Fatal(err)
}
if retry {
// Go prefers ML-KEM to X25519. Advertise ML-KEM after building
// the initial X25519 share to exercise the retry path explicitly.
clientConfig.CurvePreferences = []CurveID{X25519, MLKEM1024}
if helloID == HelloGolang {
client.HandshakeState.Hello.SupportedCurves = append(client.HandshakeState.Hello.SupportedCurves, MLKEM1024)
}
}
if shares := client.HandshakeState.Hello.KeyShares; len(shares) != 1 || shares[0].Group != initialGroup {
t.Fatalf("initial key shares = %+v, want only %v", shares, initialGroup)
}
server := Server(serverConn, serverConfig)
clientResult := make(chan error, 1)
go func() { clientResult <- client.Handshake() }()
serverErr := server.Handshake()
if serverErr != nil {
serverConn.Close()
}
clientErr := <-clientResult
if clientErr != nil || serverErr != nil {
t.Fatalf("MLKEM1024 handshake: client=%v, server=%v", clientErr, serverErr)
}
for _, state := range []ConnectionState{client.ConnectionState(), server.ConnectionState()} {
if !state.HandshakeComplete || state.Version != VersionTLS13 || state.CurveID != MLKEM1024 || state.HelloRetryRequest != retry {
t.Errorf("unexpected MLKEM1024 connection state: %+v", state)
}
}
})
}
}
}
func TestUTLSPSKResumptionWithHelloRetryRequest(t *testing.T) {
clientConfig := testConfigClient.Clone()
clientConfig.ClientSessionCache = NewLRUClientSessionCache(1)
clientConfig.OmitEmptyPsk = true
serverConfig := testConfigServer.Clone()
serverConfig.CurvePreferences = []CurveID{CurveP256}
for _, resume := range []bool{false, true} {
preset := go127ClientHelloSpec([]SignatureScheme{ECDSAWithP256AndSHA256})
for _, extension := range preset.Extensions {
if curves, ok := extension.(*SupportedCurvesExtension); ok {
curves.Curves = []CurveID{X25519, CurveP256}
}
}
preset.Extensions = append(preset.Extensions,
&PSKKeyExchangeModesExtension{Modes: []uint8{pskModeDHE}},
&UtlsPreSharedKeyExtension{},
)
// The helper reads application data after the handshake, allowing the
// first connection's NewSessionTicket to populate the shared cache.
serverState, clientState, err := testUtlsHandshake(t, clientConfig, serverConfig, preset)
if err != nil {
t.Fatalf("resume=%v: handshake: %v", resume, err)
}
for _, state := range []ConnectionState{clientState, serverState} {
if state.DidResume != resume || !state.HelloRetryRequest || state.CurveID != CurveP256 {
t.Errorf("resume=%v: unexpected connection state: %+v", resume, state)
}
}
if session, ok := clientConfig.ClientSessionCache.Get(clientConfig.ServerName); !ok || session == nil {
t.Fatal("session ticket was not stored in the client cache")
}
}
}
func TestUTLSHelloGolangECHResumption(t *testing.T) {
echKey, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
builder := cryptobyte.NewBuilder(nil)
builder.AddUint16(extensionEncryptedClientHello)
builder.AddUint16LengthPrefixed(func(builder *cryptobyte.Builder) {
builder.AddUint8(123) // config_id
builder.AddUint16(0x0020) // DHKEM(X25519, HKDF-SHA256)
builder.AddUint16LengthPrefixed(func(builder *cryptobyte.Builder) {
builder.AddBytes(echKey.PublicKey().Bytes())
})
builder.AddUint16LengthPrefixed(func(builder *cryptobyte.Builder) {
builder.AddUint16(0x0001) // HKDF-SHA256
builder.AddUint16(0x0001) // AES-128-GCM
})
builder.AddUint8(32) // maximum_name_length
builder.AddUint8LengthPrefixed(func(builder *cryptobyte.Builder) {
builder.AddBytes([]byte("public.example"))
})
builder.AddUint16(0) // extensions
})
echConfig := builder.BytesOrPanic()
builder = cryptobyte.NewBuilder(nil)
builder.AddUint16LengthPrefixed(func(builder *cryptobyte.Builder) {
builder.AddBytes(echConfig)
})
clientConfig := testConfigClient.Clone()
clientConfig.EncryptedClientHelloConfigList = builder.BytesOrPanic()
clientConfig.ClientSessionCache = NewLRUClientSessionCache(1)
serverConfig := testConfigServer.Clone()
serverConfig.EncryptedClientHelloKeys = []EncryptedClientHelloKey{{
Config: echConfig, PrivateKey: echKey.Bytes(), SendAsRetry: true,
}}
for _, resume := range []bool{false, true} {
clientConn, serverConn := localPipe(t)
defer clientConn.Close()
defer serverConn.Close()
client := UClient(clientConn, clientConfig, HelloGolang)
server := Server(serverConn, serverConfig)
serverResult := make(chan error, 1)
go func() {
defer serverConn.Close()
if err := server.Handshake(); err != nil {
serverResult <- err
return
}
_, err := io.WriteString(server, "ok")
serverResult <- err
}()
clientErr := client.Handshake()
if clientErr == nil {
var applicationData [2]byte
_, clientErr = io.ReadFull(client, applicationData[:])
if clientErr == nil && string(applicationData[:]) != "ok" {
clientErr = fmt.Errorf("unexpected application data: %q", applicationData)
}
} else {
clientConn.Close()
}
serverErr := <-serverResult
if clientErr != nil || serverErr != nil {
t.Fatalf("ECH resume=%v: client=%v, server=%v", resume, clientErr, serverErr)
}
for _, state := range []ConnectionState{client.ConnectionState(), server.ConnectionState()} {
if !state.ECHAccepted || state.DidResume != resume || state.ServerName != clientConfig.ServerName {
t.Errorf("ECH resume=%v: unexpected connection state: %+v", resume, state)
}
}
if session, ok := clientConfig.ClientSessionCache.Get(clientConfig.ServerName); !ok || session == nil {
t.Fatal("ECH session ticket was not stored in the client cache")
}
}
}
func go127ClientHelloSpec(signatureSchemes []SignatureScheme) *ClientHelloSpec {
return &ClientHelloSpec{
CipherSuites: []uint16{TLS_AES_128_GCM_SHA256},
Extensions: []TLSExtension{
&SNIExtension{},
&SupportedVersionsExtension{Versions: []uint16{VersionTLS13}},
&SupportedCurvesExtension{Curves: []CurveID{X25519}},
&SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: signatureSchemes},
&KeyShareExtension{KeyShares: []KeyShare{{Group: X25519}}},
},
}
}