-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlsparse_test.go
445 lines (374 loc) · 9.88 KB
/
tlsparse_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
438
439
440
441
442
443
444
445
package netem
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
// TLSHandshakeBytes13 contains a TLSv1.3 handshake obtained
// from https://tls13.xargs.org/#client-hello.
var TLSHandshakeBytes13 = []byte{
// [0:5] record header
0x16, 0x03, 0x01, 0x00, 0xf8,
// [5:9] handshake header
0x01, 0x00, 0x00, 0xf4,
// [9:11] client version
0x03, 0x03,
// [11:43] client random
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
// [43:44] session ID length
0x20,
// [44:76] session ID
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
// [76:78] cipher suites length
0x00, 0x08,
// [78:86] cipher suites
0x13, 0x02, 0x13, 0x03, 0x13, 0x01, 0x00, 0xff,
// [86:87] legacy compression methods length
0x01,
// [87:88] legacy compression methods
0x00,
// [88:90] extensions length
0x00, 0xa3,
// [90:253] extensions
// [90:118] server name extension
// [90:92] type
0x00, 0x00,
// [92:94] length
0x00, 0x18,
// [94:118] value
0x00, 0x16, 0x00, 0x00, 0x13, 0x65, 0x78, 0x61,
0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x75, 0x6c, 0x66,
0x68, 0x65, 0x69, 0x6d, 0x2e, 0x6e, 0x65, 0x74,
// [118:126] ec point formats extension
// [118:120] type
0x00, 0x0b,
// [120:122] length
0x00, 0x04,
// [122:126] value
0x03, 0x00, 0x01, 0x02,
// [126:152] supported groups ext
// [126:128] type
0x00, 0x0a,
// [128:130] length
0x00, 0x16,
// [130:152] value
0x00, 0x14, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x1e,
0x00, 0x19, 0x00, 0x18, 0x01, 0x00, 0x01, 0x01,
0x01, 0x02, 0x01, 0x03, 0x01, 0x04,
// [152:156] session ticket ext
// [152:154] type
0x00, 0x23,
// [154:156] length
0x00, 0x00,
// [156:160] encrypt-then-MAC ext
// [156:158] type
0x00, 0x16,
// [158:160] length
0x00, 0x00,
// [160:164] extended master secret ext
// [160:162] type
0x00, 0x17,
// [162:164] length
0x00, 0x00,
// [164:198] signature algorithsm ext
// [164:166] type
0x00, 0x0d,
// [166:168] length
0x00, 0x1e,
// [168:198] value
0x00, 0x1c, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03,
0x08, 0x07, 0x08, 0x08, 0x08, 0x09, 0x08, 0x0a,
0x08, 0x0b, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06,
0x04, 0x01, 0x05, 0x01, 0x06, 0x01,
// [198:205] supported versions ext
// [198:200] type
0x00, 0x2b,
// [200:202] length
0x00, 0x03,
// [202:205] value
0x02, 0x03, 0x04,
// [205:211] PSK key exchange modes ext
// [205:207] type
0x00, 0x2d,
// [207:209] length
0x00, 0x02,
// [209:211] value
0x01, 0x01,
// [211:253] key share ext
// [211:213] type
0x00, 0x33,
// [213:215] length
0x00, 0x26,
// [215:253]
0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x35, 0x80,
0x72, 0xd6, 0x36, 0x58, 0x80, 0xd1, 0xae, 0xea,
0x32, 0x9a, 0xdf, 0x91, 0x21, 0x38, 0x38, 0x51,
0xed, 0x21, 0xa2, 0x8e, 0x3b, 0x75, 0xe9, 0x65,
0xd0, 0xd2, 0xcd, 0x16, 0x62, 0x54,
}
func TestTLSHandshakesLength(t *testing.T) {
t.Run("for TLSv1.3 handshake", func(t *testing.T) {
if length := len(TLSHandshakeBytes13); length != 253 {
t.Fatal("expected 253 got", length)
}
})
}
func TestUnmarshalTLSRecordHeader(t *testing.T) {
type testcase struct {
// name is the test case name
name string
// rawInput is the raw input to use
rawInput []byte
// expectRecordHeader is the expected record header
expectRecordHeader *TLSRecordHeader
// expectRest is what we expect rest to contain
expectRest []byte
// expectErr is the expected error
expectErr error
}
var testcases = []testcase{{
name: "for TLSv1.3 handshake",
rawInput: TLSHandshakeBytes13,
expectRecordHeader: &TLSRecordHeader{
ContentType: 0x16,
ProtocolVersion: 0x0301,
Rest: TLSHandshakeBytes13[5:],
},
expectRest: nil,
expectErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
rh, rest, err := UnmarshalTLSRecordHeader(tc.rawInput)
// check the error
switch {
case err == nil && tc.expectErr == nil:
case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)
default:
if !errors.Is(err, tc.expectErr) {
t.Fatal("unexpected error", err)
}
}
// check whether rest is empty
switch {
case len(tc.expectRest) <= 0 && rest.Empty():
case len(tc.expectRest) > 0 && !rest.Empty():
if diff := cmp.Diff(tc.expectRest, rest); diff != "" {
t.Fatal(diff)
}
default:
t.Fatal("expected", tc.expectRest, "got", rest.Empty())
}
// check the result
if diff := cmp.Diff(tc.expectRecordHeader, rh); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestUnmarshalTLSHandshakeMsg(t *testing.T) {
type testcase struct {
// name is the test case name
name string
// rawInput is the raw input to use
rawInput []byte
// expectHandshake is the expected handshake
expectHandshake *TLSHandshakeMsg
// expectErr is the expected error
expectErr error
}
var testcases = []testcase{{
name: "for TLSv1.3 handshake",
rawInput: TLSHandshakeBytes13[5:],
expectHandshake: &TLSHandshakeMsg{
HandshakeType: 0x01,
ClientHello: &TLSClientHello{
ProtocolVersion: 0x0303,
Random: TLSHandshakeBytes13[11:43],
LegacySessionID: TLSHandshakeBytes13[44:76],
CipherSuites: TLSHandshakeBytes13[78:86],
LegacyCompressionMethods: TLSHandshakeBytes13[87:88],
Extensions: TLSHandshakeBytes13[90:253],
},
},
expectErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
hx, err := UnmarshalTLSHandshakeMsg(tc.rawInput)
// check the error
switch {
case err == nil && tc.expectErr == nil:
case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)
default:
if !errors.Is(err, tc.expectErr) {
t.Fatal("unexpected error", err)
}
}
// check the result
if diff := cmp.Diff(tc.expectHandshake, hx); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestUnmarshalTLSExtensions(t *testing.T) {
type testcase struct {
// name is the test case name
name string
// rawInput is the raw input to use
rawInput []byte
// expectExtensions is the expected extensions
expectExtensions []*TLSExtension
// expectErr is the expected error
expectErr error
}
var testcases = []testcase{{
name: "for TLSv1.3 handshake",
rawInput: TLSHandshakeBytes13[90:253],
expectExtensions: []*TLSExtension{{
Type: 0,
Data: TLSHandshakeBytes13[94:118],
}, {
Type: 11,
Data: TLSHandshakeBytes13[122:126],
}, {
Type: 10,
Data: TLSHandshakeBytes13[130:152],
}, {
Type: 35,
Data: []byte{},
}, {
Type: 22,
Data: []byte{},
}, {
Type: 23,
Data: []byte{},
}, {
Type: 13,
Data: TLSHandshakeBytes13[168:198],
}, {
Type: 43,
Data: TLSHandshakeBytes13[202:205],
}, {
Type: 45,
Data: TLSHandshakeBytes13[209:211],
}, {
Type: 51,
Data: TLSHandshakeBytes13[215:253],
}},
expectErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
exts, err := UnmarshalTLSExtensions(tc.rawInput)
// check the error
switch {
case err == nil && tc.expectErr == nil:
case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)
default:
if !errors.Is(err, tc.expectErr) {
t.Fatal("unexpected error", err)
}
}
// check the result
if diff := cmp.Diff(tc.expectExtensions, exts); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestFindTLSServerNameExtension(t *testing.T) {
type testcase struct {
// name is the test case name
name string
// rawInput is the raw input to use
rawInput []byte
// expectExtension is the expected extension
expectExtension *TLSExtension
// expectFound is whether we expect to find it
expectFound bool
}
var testcases = []testcase{{
name: "for TLSv1.3 handshake",
rawInput: TLSHandshakeBytes13[90:253],
expectExtension: &TLSExtension{
Type: 0,
Data: TLSHandshakeBytes13[94:118],
},
expectFound: true,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
exts, err := UnmarshalTLSExtensions(tc.rawInput)
if err != nil {
t.Fatal(err)
}
ext, good := FindTLSServerNameExtension(exts)
switch {
case tc.expectFound && good:
case !tc.expectFound && !good:
default:
t.Fatal("expected", tc.expectFound, "got", good)
}
// check the result
if diff := cmp.Diff(tc.expectExtension, ext); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestUnmarshalTLSServerNameExtension(t *testing.T) {
type testcase struct {
// name is the test case name
name string
// rawInput is the raw input to use
rawInput []byte
// expectSNI is the expected extensions
expectSNI string
// expectErr is the expected error
expectErr error
}
var testcases = []testcase{{
name: "for TLSv1.3 handshake",
rawInput: TLSHandshakeBytes13[94:118],
expectSNI: "example.ulfheim.net",
expectErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
sni, err := UnmarshalTLSServerNameExtension(tc.rawInput)
// check the error
switch {
case err == nil && tc.expectErr == nil:
case err != nil && tc.expectErr == nil:
t.Fatal("expected", tc.expectErr, "got", err)
case err == nil && tc.expectErr != nil:
t.Fatal("expected", tc.expectErr, "got", err)
default:
if !errors.Is(err, tc.expectErr) {
t.Fatal("unexpected error", err)
}
}
// check the result
if diff := cmp.Diff(tc.expectSNI, sni); diff != "" {
t.Fatal(diff)
}
})
}
}