forked from refraction-networking/utls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quic_test.go
437 lines (398 loc) · 13.6 KB
/
quic_test.go
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright 2023 The Go 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 (
"context"
"errors"
"reflect"
"testing"
)
type testQUICConn struct {
t *testing.T
conn *QUICConn
readSecret map[QUICEncryptionLevel]suiteSecret
writeSecret map[QUICEncryptionLevel]suiteSecret
gotParams []byte
complete bool
}
func newTestQUICClient(t *testing.T, config *Config) *testQUICConn {
q := &testQUICConn{t: t}
q.conn = QUICClient(&QUICConfig{
TLSConfig: config,
})
t.Cleanup(func() {
q.conn.Close()
})
return q
}
func newTestQUICServer(t *testing.T, config *Config) *testQUICConn {
q := &testQUICConn{t: t}
q.conn = QUICServer(&QUICConfig{
TLSConfig: config,
})
t.Cleanup(func() {
q.conn.Close()
})
return q
}
type suiteSecret struct {
suite uint16
secret []byte
}
func (q *testQUICConn) setReadSecret(level QUICEncryptionLevel, suite uint16, secret []byte) {
if _, ok := q.writeSecret[level]; !ok {
q.t.Errorf("SetReadSecret for level %v called before SetWriteSecret", level)
}
if level == QUICEncryptionLevelApplication && !q.complete {
q.t.Errorf("SetReadSecret for level %v called before HandshakeComplete", level)
}
if _, ok := q.readSecret[level]; ok {
q.t.Errorf("SetReadSecret for level %v called twice", level)
}
if q.readSecret == nil {
q.readSecret = map[QUICEncryptionLevel]suiteSecret{}
}
switch level {
case QUICEncryptionLevelHandshake, QUICEncryptionLevelApplication:
q.readSecret[level] = suiteSecret{suite, secret}
default:
q.t.Errorf("SetReadSecret for unexpected level %v", level)
}
}
func (q *testQUICConn) setWriteSecret(level QUICEncryptionLevel, suite uint16, secret []byte) {
if _, ok := q.writeSecret[level]; ok {
q.t.Errorf("SetWriteSecret for level %v called twice", level)
}
if q.writeSecret == nil {
q.writeSecret = map[QUICEncryptionLevel]suiteSecret{}
}
switch level {
case QUICEncryptionLevelHandshake, QUICEncryptionLevelApplication:
q.writeSecret[level] = suiteSecret{suite, secret}
default:
q.t.Errorf("SetWriteSecret for unexpected level %v", level)
}
}
var errTransportParametersRequired = errors.New("transport parameters required")
func runTestQUICConnection(ctx context.Context, cli, srv *testQUICConn, onHandleCryptoData func()) error {
a, b := cli, srv
for _, c := range []*testQUICConn{a, b} {
if !c.conn.conn.quic.started {
if err := c.conn.Start(ctx); err != nil {
return err
}
}
}
idleCount := 0
for {
e := a.conn.NextEvent()
switch e.Kind {
case QUICNoEvent:
idleCount++
if idleCount == 2 {
if !a.complete || !b.complete {
return errors.New("handshake incomplete")
}
return nil
}
a, b = b, a
case QUICSetReadSecret:
a.setReadSecret(e.Level, e.Suite, e.Data)
case QUICSetWriteSecret:
a.setWriteSecret(e.Level, e.Suite, e.Data)
case QUICWriteData:
if err := b.conn.HandleData(e.Level, e.Data); err != nil {
return err
}
case QUICTransportParameters:
a.gotParams = e.Data
if a.gotParams == nil {
a.gotParams = []byte{}
}
case QUICTransportParametersRequired:
return errTransportParametersRequired
case QUICHandshakeDone:
a.complete = true
if a == srv {
opts := QUICSessionTicketOptions{}
if err := srv.conn.SendSessionTicket(opts); err != nil {
return err
}
}
}
if e.Kind != QUICNoEvent {
idleCount = 0
}
}
}
func TestQUICConnection(t *testing.T) {
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
if _, ok := cli.readSecret[QUICEncryptionLevelHandshake]; !ok {
t.Errorf("client has no Handshake secret")
}
if _, ok := cli.readSecret[QUICEncryptionLevelApplication]; !ok {
t.Errorf("client has no Application secret")
}
if _, ok := srv.readSecret[QUICEncryptionLevelHandshake]; !ok {
t.Errorf("server has no Handshake secret")
}
if _, ok := srv.readSecret[QUICEncryptionLevelApplication]; !ok {
t.Errorf("server has no Application secret")
}
for _, level := range []QUICEncryptionLevel{QUICEncryptionLevelHandshake, QUICEncryptionLevelApplication} {
if _, ok := cli.readSecret[level]; !ok {
t.Errorf("client has no %v read secret", level)
}
if _, ok := srv.readSecret[level]; !ok {
t.Errorf("server has no %v read secret", level)
}
if !reflect.DeepEqual(cli.readSecret[level], srv.writeSecret[level]) {
t.Errorf("client read secret does not match server write secret for level %v", level)
}
if !reflect.DeepEqual(cli.writeSecret[level], srv.readSecret[level]) {
t.Errorf("client write secret does not match server read secret for level %v", level)
}
}
}
func TestQUICSessionResumption(t *testing.T) {
clientConfig := testConfig.Clone()
clientConfig.MinVersion = VersionTLS13
clientConfig.ClientSessionCache = NewLRUClientSessionCache(1)
clientConfig.ServerName = "example.go.dev"
serverConfig := testConfig.Clone()
serverConfig.MinVersion = VersionTLS13
cli := newTestQUICClient(t, clientConfig)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, serverConfig)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during first connection handshake: %v", err)
}
if cli.conn.ConnectionState().DidResume {
t.Errorf("first connection unexpectedly used session resumption")
}
cli2 := newTestQUICClient(t, clientConfig)
cli2.conn.SetTransportParameters(nil)
srv2 := newTestQUICServer(t, serverConfig)
srv2.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli2, srv2, nil); err != nil {
t.Fatalf("error during second connection handshake: %v", err)
}
if !cli2.conn.ConnectionState().DidResume {
t.Errorf("second connection did not use session resumption")
}
}
func TestQUICPostHandshakeClientAuthentication(t *testing.T) {
// RFC 9001, Section 4.4.
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
certReq := new(certificateRequestMsgTLS13)
certReq.ocspStapling = true
certReq.scts = true
certReq.supportedSignatureAlgorithms = supportedSignatureAlgorithms()
certReqBytes, err := certReq.marshal()
if err != nil {
t.Fatal(err)
}
if err := cli.conn.HandleData(QUICEncryptionLevelApplication, append([]byte{
byte(typeCertificateRequest),
byte(0), byte(0), byte(len(certReqBytes)),
}, certReqBytes...)); err == nil {
t.Fatalf("post-handshake authentication request: got no error, want one")
}
}
func TestQUICPostHandshakeKeyUpdate(t *testing.T) {
// RFC 9001, Section 6.
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
keyUpdate := new(keyUpdateMsg)
keyUpdateBytes, err := keyUpdate.marshal()
if err != nil {
t.Fatal(err)
}
if err := cli.conn.HandleData(QUICEncryptionLevelApplication, append([]byte{
byte(typeKeyUpdate),
byte(0), byte(0), byte(len(keyUpdateBytes)),
}, keyUpdateBytes...)); !errors.Is(err, alertUnexpectedMessage) {
t.Fatalf("key update request: got error %v, want alertUnexpectedMessage", err)
}
}
func TestQUICHandshakeError(t *testing.T) {
clientConfig := testConfig.Clone()
clientConfig.MinVersion = VersionTLS13
clientConfig.InsecureSkipVerify = false
clientConfig.ServerName = "name"
serverConfig := testConfig.Clone()
serverConfig.MinVersion = VersionTLS13
cli := newTestQUICClient(t, clientConfig)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, serverConfig)
srv.conn.SetTransportParameters(nil)
err := runTestQUICConnection(context.Background(), cli, srv, nil)
if !errors.Is(err, AlertError(alertBadCertificate)) {
t.Errorf("connection handshake terminated with error %q, want alertBadCertificate", err)
}
var e *CertificateVerificationError
if !errors.As(err, &e) {
t.Errorf("connection handshake terminated with error %q, want CertificateVerificationError", err)
}
}
// Test that QUICConn.ConnectionState can be used during the handshake,
// and that it reports the application protocol as soon as it has been
// negotiated.
func TestQUICConnectionState(t *testing.T) {
config := testConfig.Clone()
config.MinVersion = VersionTLS13
config.NextProtos = []string{"h3"}
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
onHandleCryptoData := func() {
cliCS := cli.conn.ConnectionState()
cliWantALPN := ""
if _, ok := cli.readSecret[QUICEncryptionLevelApplication]; ok {
cliWantALPN = "h3"
}
if want, got := cliCS.NegotiatedProtocol, cliWantALPN; want != got {
t.Errorf("cli.ConnectionState().NegotiatedProtocol = %q, want %q", want, got)
}
srvCS := srv.conn.ConnectionState()
srvWantALPN := ""
if _, ok := srv.readSecret[QUICEncryptionLevelHandshake]; ok {
srvWantALPN = "h3"
}
if want, got := srvCS.NegotiatedProtocol, srvWantALPN; want != got {
t.Errorf("srv.ConnectionState().NegotiatedProtocol = %q, want %q", want, got)
}
}
if err := runTestQUICConnection(context.Background(), cli, srv, onHandleCryptoData); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
}
func TestQUICStartContextPropagation(t *testing.T) {
const key = "key"
const value = "value"
ctx := context.WithValue(context.Background(), key, value)
config := testConfig.Clone()
config.MinVersion = VersionTLS13
calls := 0
config.GetConfigForClient = func(info *ClientHelloInfo) (*Config, error) {
calls++
got, _ := info.Context().Value(key).(string)
if got != value {
t.Errorf("GetConfigForClient context key %q has value %q, want %q", key, got, value)
}
return nil, nil
}
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(ctx, cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
if calls != 1 {
t.Errorf("GetConfigForClient called %v times, want 1", calls)
}
}
func TestQUICDelayedTransportParameters(t *testing.T) {
clientConfig := testConfig.Clone()
clientConfig.MinVersion = VersionTLS13
clientConfig.ClientSessionCache = NewLRUClientSessionCache(1)
clientConfig.ServerName = "example.go.dev"
serverConfig := testConfig.Clone()
serverConfig.MinVersion = VersionTLS13
cliParams := "client params"
srvParams := "server params"
cli := newTestQUICClient(t, clientConfig)
srv := newTestQUICServer(t, serverConfig)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != errTransportParametersRequired {
t.Fatalf("handshake with no client parameters: %v; want errTransportParametersRequired", err)
}
cli.conn.SetTransportParameters([]byte(cliParams))
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != errTransportParametersRequired {
t.Fatalf("handshake with no server parameters: %v; want errTransportParametersRequired", err)
}
srv.conn.SetTransportParameters([]byte(srvParams))
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
if got, want := string(cli.gotParams), srvParams; got != want {
t.Errorf("client got transport params: %q, want %q", got, want)
}
if got, want := string(srv.gotParams), cliParams; got != want {
t.Errorf("server got transport params: %q, want %q", got, want)
}
}
func TestQUICEmptyTransportParameters(t *testing.T) {
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
srv := newTestQUICServer(t, config)
srv.conn.SetTransportParameters(nil)
if err := runTestQUICConnection(context.Background(), cli, srv, nil); err != nil {
t.Fatalf("error during connection handshake: %v", err)
}
if cli.gotParams == nil {
t.Errorf("client did not get transport params")
}
if srv.gotParams == nil {
t.Errorf("server did not get transport params")
}
if len(cli.gotParams) != 0 {
t.Errorf("client got transport params: %v, want empty", cli.gotParams)
}
if len(srv.gotParams) != 0 {
t.Errorf("server got transport params: %v, want empty", srv.gotParams)
}
}
func TestQUICCanceledWaitingForData(t *testing.T) {
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.SetTransportParameters(nil)
cli.conn.Start(context.Background())
for cli.conn.NextEvent().Kind != QUICNoEvent {
}
err := cli.conn.Close()
if !errors.Is(err, alertCloseNotify) {
t.Errorf("conn.Close() = %v, want alertCloseNotify", err)
}
}
func TestQUICCanceledWaitingForTransportParams(t *testing.T) {
config := testConfig.Clone()
config.MinVersion = VersionTLS13
cli := newTestQUICClient(t, config)
cli.conn.Start(context.Background())
for cli.conn.NextEvent().Kind != QUICTransportParametersRequired {
}
err := cli.conn.Close()
if !errors.Is(err, alertCloseNotify) {
t.Errorf("conn.Close() = %v, want alertCloseNotify", err)
}
}