forked from bifurcation/mint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handshake-layer_test.go
389 lines (324 loc) · 12.2 KB
/
handshake-layer_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
package mint
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
)
type ErrorReadWriter struct{}
func (e ErrorReadWriter) Read(p []byte) (n int, err error) {
return 0, fmt.Errorf("Unknown read error")
}
func (e ErrorReadWriter) Write(p []byte) (n int, err error) {
return 0, fmt.Errorf("Unknown write error")
}
func recordHeaderHex(data []byte) string {
dataLen := len(data)
return hex.EncodeToString([]byte{0x16, 0x03, 0x01, byte(dataLen >> 8), byte(dataLen)})
}
var (
messageType = HandshakeTypeClientHello
tinyMessageIn = &HandshakeMessage{
msgType: messageType,
body: []byte{0, 0, 0, 0},
length: 4,
}
tinyMessageHex = "0100000400000000"
// short: 0x000040
// long: 0x007fe0 = 0x4000 + 0x3fe0
shortMessageLen = 64
longMessageLen = 2*maxFragmentLen - (shortMessageLen / 2)
shortMessageHeader = []byte{byte(messageType), 0x00, 0x00, byte(shortMessageLen)}
shortMessageBody = bytes.Repeat([]byte{0xab}, shortMessageLen)
shortMessage = append(shortMessageHeader, shortMessageBody...)
longMessageHeader = []byte{byte(messageType), 0x00, byte(longMessageLen >> 8), byte(longMessageLen)}
longMessageBody = bytes.Repeat([]byte{0xcd}, longMessageLen)
longMessage = append(longMessageHeader, longMessageBody...)
shortLongMessage = append(shortMessage, longMessage...)
shortLongShortMessage = append(shortLongMessage, shortMessage...)
shortHex = recordHeaderHex(shortMessage) + hex.EncodeToString(shortMessage)
shortMessageIn = &HandshakeMessage{
msgType: messageType,
body: shortMessageBody,
length: uint32(len(shortMessageBody)),
}
longMessageIn = &HandshakeMessage{
msgType: messageType,
body: longMessageBody,
length: uint32(len(longMessageBody)),
}
tooLongMessageIn = &HandshakeMessage{
msgType: messageType,
body: bytes.Repeat([]byte{0xef}, maxHandshakeMessageLen+1),
}
longFragment1 = longMessage[:maxFragmentLen]
longFragment2 = longMessage[maxFragmentLen:]
longHex = recordHeaderHex(longFragment1) + hex.EncodeToString(longFragment1) +
recordHeaderHex(longFragment2) + hex.EncodeToString(longFragment2)
slsFragment1 = shortLongShortMessage[:maxFragmentLen]
slsFragment2 = shortLongShortMessage[maxFragmentLen : 2*maxFragmentLen]
slsFragment3 = shortLongShortMessage[2*maxFragmentLen:]
shortLongShortHex = recordHeaderHex(slsFragment1) + hex.EncodeToString(slsFragment1) +
recordHeaderHex(slsFragment2) + hex.EncodeToString(slsFragment2) +
recordHeaderHex(slsFragment3) + hex.EncodeToString(slsFragment3)
insufficientDataHex = "1603010004" + "01000004" + "1603010002" + "0000"
nonHandshakeHex = "15030100020000"
)
func TestMessageMarshal(t *testing.T) {
tinyMessage := unhex(tinyMessageHex)
out := tinyMessageIn.Marshal()
assertByteEquals(t, out, tinyMessage)
}
func newTestHandshakeMessage(t HandshakeType, m []byte) HandshakeMessage {
return HandshakeMessage{
msgType: t,
body: m,
}
}
func TestMessageToBody(t *testing.T) {
// Borrowing serialized bodies from handshake-messages_test.go
chValid := unhex(chValidHex)
shValid := unhex(shValidHex)
finValid := unhex(finValidHex)
encExtValid := unhex(encExtValidHex)
certValid := unhex(certValidHex)
certVerifyValid := unhex(certVerifyValidHex)
ticketValid := unhex(ticketValidHex)
// Test successful marshal of ClientHello
hm := newTestHandshakeMessage(HandshakeTypeClientHello, chValid)
_, err := hm.ToBody()
assertNotError(t, err, "Failed to convert ClientHello body")
// Test successful marshal of ServerHello
hm = newTestHandshakeMessage(HandshakeTypeServerHello, shValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert ServerHello body")
// Test successful marshal of EncryptedExtensions
hm = newTestHandshakeMessage(HandshakeTypeEncryptedExtensions, encExtValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert EncryptedExtensions body")
// Test successful marshal of Certificate
hm = newTestHandshakeMessage(HandshakeTypeCertificate, certValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert Certificate body")
// Test successful marshal of CertificateVerify
hm = newTestHandshakeMessage(HandshakeTypeCertificateVerify, certVerifyValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert CertificateVerify body")
// Test successful marshal of Finished
hm = newTestHandshakeMessage(HandshakeTypeFinished, finValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert Finished body")
// Test successful marshal of NewSessionTicket
hm = newTestHandshakeMessage(HandshakeTypeNewSessionTicket, ticketValid)
_, err = hm.ToBody()
assertNotError(t, err, "Failed to convert NewSessionTicket body")
// Test failure on unsupported body type
hm = newTestHandshakeMessage(HandshakeTypeHelloRetryRequest, []byte{})
_, err = hm.ToBody()
assertError(t, err, "Converted an unsupported message")
// Test failure on marshal failure
hm = newTestHandshakeMessage(HandshakeTypeClientHello, []byte{})
_, err = hm.ToBody()
assertError(t, err, "Converted an empty message")
}
func TestMessageFromBody(t *testing.T) {
chValid := unhex(chValidHex)
b := bytes.NewBuffer(nil)
h := NewHandshakeLayerTLS(&HandshakeContext{}, NewRecordLayerTLS(b, DirectionRead))
// Test successful conversion
hm, err := h.HandshakeMessageFromBody(&chValidIn)
assertNotError(t, err, "Failed to convert ClientHello body to message")
assertEquals(t, hm.msgType, chValidIn.Type())
assertByteEquals(t, hm.body, chValid)
// Test conversion failure on marshal failure
chValidIn.CipherSuites = []CipherSuite{}
hm, err = h.HandshakeMessageFromBody(&chValidIn)
assertError(t, err, "Converted a ClientHello that should not have marshaled")
chValidIn.CipherSuites = chCipherSuites
}
func newHandshakeLayerFromBytes(d []byte) *HandshakeLayer {
hc := &HandshakeContext{}
b := bytes.NewBuffer(d)
hc.hIn = NewHandshakeLayerTLS(hc, NewRecordLayerTLS(b, DirectionRead))
return hc.hIn
}
func TestReadHandshakeMessage(t *testing.T) {
short := unhex(shortHex)
long := unhex(longHex)
shortLongShort := unhex(shortLongShortHex)
insufficientData := unhex(insufficientDataHex)
nonHandshake := unhex(nonHandshakeHex)
// Test successful read of a message in a single record
h := newHandshakeLayerFromBytes(short)
hm, err := h.ReadMessage()
assertNotError(t, err, "Failed to read a short handshake message")
assertDeepEquals(t, hm, shortMessageIn)
// Test successful read of a message split across records
h = newHandshakeLayerFromBytes(long)
hm, err = h.ReadMessage()
assertNotError(t, err, "Failed to read a long handshake message")
assertDeepEquals(t, hm, longMessageIn)
// Test successful read of multiple messages sequentially
h = newHandshakeLayerFromBytes(shortLongShort)
hm1, err := h.ReadMessage()
assertNotError(t, err, "Failed to read first handshake message")
assertDeepEquals(t, hm1, shortMessageIn)
hm2, err := h.ReadMessage()
assertNotError(t, err, "Failed to read second handshake message")
assertDeepEquals(t, hm2, longMessageIn)
hm3, err := h.ReadMessage()
assertNotError(t, err, "Failed to read third handshake message")
assertDeepEquals(t, hm3, shortMessageIn)
// Test read failure on inability to read header
h = newHandshakeLayerFromBytes(short[:handshakeHeaderLenTLS-1])
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message with an incomplete header")
// Test read failure on inability to read body
h = newHandshakeLayerFromBytes(insufficientData)
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message with an incomplete body")
// Test read failure on receiving a non-handshake record
h = newHandshakeLayerFromBytes(nonHandshake)
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message from a non-handshake record")
}
func testWriteHandshakeMessage(h *HandshakeLayer, hm *HandshakeMessage) error {
hm.cipher = h.conn.(*DefaultRecordLayer).cipher
_, err := h.WriteMessage(hm)
return err
}
func TestWriteHandshakeMessage(t *testing.T) {
short := unhex(shortHex)
long := unhex(longHex)
// Test successful write of single message
b := bytes.NewBuffer(nil)
h := NewHandshakeLayerTLS(&HandshakeContext{}, NewRecordLayerTLS(b, DirectionWrite))
err := testWriteHandshakeMessage(h, shortMessageIn)
assertNotError(t, err, "Failed to write valid short message")
assertByteEquals(t, b.Bytes(), short)
// Test successful write of single long message
b = bytes.NewBuffer(nil)
h = NewHandshakeLayerTLS(&HandshakeContext{}, NewRecordLayerTLS(b, DirectionWrite))
err = testWriteHandshakeMessage(h, longMessageIn)
assertNotError(t, err, "Failed to write valid long message")
assertByteEquals(t, b.Bytes(), long)
// Test write failure on message too large
b = bytes.NewBuffer(nil)
h = NewHandshakeLayerTLS(&HandshakeContext{}, NewRecordLayerTLS(b, DirectionWrite))
err = testWriteHandshakeMessage(h, tooLongMessageIn)
assertError(t, err, "Wrote a message exceeding the length bound")
// Test write failure on underlying write failure
h = NewHandshakeLayerTLS(&HandshakeContext{}, NewRecordLayerTLS(ErrorReadWriter{}, DirectionWrite))
err = testWriteHandshakeMessage(h, longMessageIn)
assertError(t, err, "Write succeeded despite error in full fragment send")
err = testWriteHandshakeMessage(h, shortMessageIn)
assertError(t, err, "Write succeeded despite error in last fragment send")
}
type testReassembleFixture struct {
t *testing.T
c HandshakeContext
h *HandshakeLayer
r *DefaultRecordLayer
rd *pipeConn
wr *pipeConn
m0 *HandshakeMessage
m0f0 *HandshakeMessage
m0f1 *HandshakeMessage
m0f2 *HandshakeMessage
m0f1x *HandshakeMessage
m0f1y *HandshakeMessage
m1 *HandshakeMessage
}
func newTestReassembleFixture(t *testing.T) *testReassembleFixture {
f := testReassembleFixture{t: t}
// Make two messages, m0 and m1, with m0 fragmented
m0 := make([]byte, 2048)
for i := range m0 {
m0[i] = byte(i % 13)
}
f.m0 = newHsFragment(m0, 0, 0, 2048)
f.m0f0 = newHsFragment(m0, 0, 0, 1024)
f.m0f1 = newHsFragment(m0, 0, 1024, 512)
f.m0f2 = newHsFragment(m0, 0, 1536, 512)
f.m0f1x = newHsFragment(m0, 0, 512, 1000)
f.m0f1y = newHsFragment(m0, 0, 512, 1048)
m1 := make([]byte, 2048)
for i := range m1 {
m1[i] = byte(i % 23)
}
f.m1 = newHsFragment(m1, 1, 0, 2048)
f.rd, f.wr = pipe()
f.r = NewRecordLayerDTLS(f.rd, DirectionRead)
f.h = NewHandshakeLayerDTLS(&f.c, f.r)
f.c.hIn = f.h
f.c.timers = newTimerSet()
f.h.nonblocking = true
return &f
}
func newHsFragment(full []byte, seq uint32, offset uint32, fragLen uint32) *HandshakeMessage {
return &HandshakeMessage{
HandshakeTypeClientHello,
seq,
full[offset : offset+fragLen],
true,
offset,
uint32(len(full)),
nil,
}
}
func (f *testReassembleFixture) addFragment(in *HandshakeMessage, expected *HandshakeMessage) {
if in != nil {
b := in.Marshal()
r := []byte{byte(RecordTypeHandshake), 0xfe, 0xff,
0, 0, 0, 0, 0, 0, 0, 0,
byte((len(b) >> 8) & 0xff), byte(len(b) & 0xff)}
r = append(r, b...)
f.wr.Write(r)
}
h2, err := f.h.ReadMessage()
if expected == nil {
assertEquals(f.t, (*HandshakeMessage)(nil), h2)
assertEquals(f.t, nil, err)
} else {
assertNotError(f.t, err, "Error reading handshake")
assertEquals(f.t, expected.seq, h2.seq)
assertByteEquals(f.t, expected.body, h2.body)
}
}
func TestHandshakeDTLSInOrder(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m0, f.m0)
f.addFragment(f.m0, nil)
f.addFragment(f.m1, f.m1)
}
func TestHandshakeDTLSOutOfOrder(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m1, nil)
f.addFragment(f.m0, f.m0)
f.addFragment(nil, f.m1)
}
func TestHandshakeDTLSNonOverlappingFragments(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m0f0, nil)
f.addFragment(f.m0f1, nil)
f.addFragment(f.m0f2, f.m0)
}
func TestHandshakeDTLSNonOverlappingFragmentsOO(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m0f0, nil)
f.addFragment(f.m0f2, nil)
f.addFragment(f.m0f1, f.m0)
}
func TestHandshakeDTLSOverlappingFragments1(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m0f0, nil)
f.addFragment(f.m0f1, nil)
f.addFragment(f.m0f1x, nil)
f.addFragment(f.m0f2, f.m0)
}
func TestHandshakeDTLSOverlappingFragments2(t *testing.T) {
f := newTestReassembleFixture(t)
f.addFragment(f.m0f0, nil)
f.addFragment(f.m0f1y, nil)
f.addFragment(f.m0f2, f.m0)
}