forked from bifurcation/mint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handshake-layer_test.go
324 lines (274 loc) · 12.1 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
package mint
import (
"bytes"
"encoding/hex"
"testing"
)
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},
}
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,
}
longMessageIn = &handshakeMessage{
msgType: messageType,
body: 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"
// Also borrow ClientHello and ServerHello inputs from handshake-messages_test.go
finishedHex = "1603010006" + "14000002" + "0000"
chLen = len(chValidHex) / 2
shLen = len(shValidHex) / 2
chMessageHeaderHex = hex.EncodeToString([]byte{0x01, 0x00, 0x00, byte(chLen)})
shMessageHeaderHex = hex.EncodeToString([]byte{0x02, 0x00, 0x00, byte(shLen)})
chRecordLen = len(chMessageHeaderHex)/2 + chLen
chRecordLenHex = hex.EncodeToString([]byte{byte(chRecordLen)})
chValidMessageHex = "16030100" + chRecordLenHex + chMessageHeaderHex + chValidHex
chshRecordLen = chRecordLen + len(shMessageHeaderHex)/2 + shLen
chshRecordLenHex = hex.EncodeToString([]byte{byte(chshRecordLen)})
chshValidMessageHex = "16030100" + chshRecordLenHex + chMessageHeaderHex + chValidHex +
shMessageHeaderHex + shValidHex
)
func TestMessageMarshal(t *testing.T) {
tinyMessage, _ := hex.DecodeString(tinyMessageHex)
out := tinyMessageIn.Marshal()
assertByteEquals(t, out, tinyMessage)
}
func TestMessageToBody(t *testing.T) {
// Borrowing serialized bodies from handshake-messages_test.go
chValid, _ := hex.DecodeString(chValidHex)
shValid, _ := hex.DecodeString(shValidHex)
finValid, _ := hex.DecodeString(finValidHex)
encExtValid, _ := hex.DecodeString(encExtValidHex)
certValid, _ := hex.DecodeString(certValidHex)
certVerifyValid, _ := hex.DecodeString(certVerifyValidHex)
// Test successful marshal of ClientHello
hm := handshakeMessage{handshakeTypeClientHello, chValid}
_, err := hm.toBody()
assertNotError(t, err, "Failed to convert ClientHello body")
// Test successful marshal of ServerHello
hm = handshakeMessage{handshakeTypeServerHello, shValid}
_, err = hm.toBody()
assertNotError(t, err, "Failed to convert ServerHello body")
// Test successful marshal of EncryptedExtensions
hm = handshakeMessage{handshakeTypeEncryptedExtensions, encExtValid}
_, err = hm.toBody()
assertNotError(t, err, "Failed to convert EncryptedExtensions body")
// Test successful marshal of Certificate
hm = handshakeMessage{handshakeTypeCertificate, certValid}
_, err = hm.toBody()
assertNotError(t, err, "Failed to convert Certificate body")
// Test successful marshal of CertificateVerify
hm = handshakeMessage{handshakeTypeCertificateVerify, certVerifyValid}
_, err = hm.toBody()
assertNotError(t, err, "Failed to convert CertificateVerify body")
// Test successful marshal of Finished
hm = handshakeMessage{handshakeTypeFinished, finValid}
_, err = hm.toBody()
assertNotError(t, err, "Failed to convert Finished body")
// Test failure on unsupported body type
hm = handshakeMessage{handshakeTypeSessionTicket, []byte{}}
_, err = hm.toBody()
assertError(t, err, "Converted an empty message")
// Test failure on marshal failure
hm = handshakeMessage{handshakeTypeClientHello, []byte{}}
_, err = hm.toBody()
assertError(t, err, "Converted an empty message")
}
func TestMessageFromBody(t *testing.T) {
chValid, _ := hex.DecodeString(chValidHex)
// Test successful conversion
hm, err := 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 = handshakeMessageFromBody(&chValidIn)
assertError(t, err, "Converted a ClientHello that should not have marshaled")
chValidIn.cipherSuites = chCipherSuites
}
func TestReadHandshakeMessage(t *testing.T) {
short, _ := hex.DecodeString(shortHex)
long, _ := hex.DecodeString(longHex)
shortLongShort, _ := hex.DecodeString(shortLongShortHex)
insufficientData, _ := hex.DecodeString(insufficientDataHex)
nonHandshake, _ := hex.DecodeString(nonHandshakeHex)
// Test successful read of a message in a single record
b := bytes.NewBuffer(short)
h := newHandshakeLayer(newRecordLayer(b))
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
b = bytes.NewBuffer(long)
h = newHandshakeLayer(newRecordLayer(b))
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
b = bytes.NewBuffer(shortLongShort)
h = newHandshakeLayer(newRecordLayer(b))
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
b = bytes.NewBuffer(short[:handshakeHeaderLen-1])
h = newHandshakeLayer(newRecordLayer(b))
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message with an incomplete header")
// Test read failure on inability to read body
b = bytes.NewBuffer(insufficientData)
h = newHandshakeLayer(newRecordLayer(b))
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message with an incomplete body")
// Test read failure on receiving a non-handshake record
b = bytes.NewBuffer(nonHandshake)
h = newHandshakeLayer(newRecordLayer(b))
hm, err = h.ReadMessage()
assertError(t, err, "Read handshake message from a non-handshake record")
}
func TestReadHandshakeMessageBody(t *testing.T) {
finished, _ := hex.DecodeString(finishedHex)
// Test successful read
fin := finishedBody{verifyDataLen: 2}
b := bytes.NewBuffer(finished)
h := newHandshakeLayer(newRecordLayer(b))
hm, err := h.ReadMessageBody(&fin)
assertNotError(t, err, "Failed to read a valid finished body")
assertByteEquals(t, fin.verifyData, []byte{0, 0})
assertEquals(t, hm.msgType, handshakeTypeFinished)
assertByteEquals(t, hm.body, []byte{0, 0})
// Test read failure on underlying failure
b = bytes.NewBuffer(finished[:len(finished)-1])
h = newHandshakeLayer(newRecordLayer(b))
_, err = h.ReadMessageBody(&fin)
assertError(t, err, "Read message body despite unmarshal failure")
// Test read failure on wrong body type
ch := clientHelloBody{}
b = bytes.NewBuffer(finished)
h = newHandshakeLayer(newRecordLayer(b))
_, err = h.ReadMessageBody(&ch)
assertError(t, err, "Read message body with the wrong type")
// Test read failure on unmarshal failure
fin.verifyDataLen = 3
b = bytes.NewBuffer(finished)
h = newHandshakeLayer(newRecordLayer(b))
_, err = h.ReadMessageBody(&fin)
assertError(t, err, "Read message body despite unmarshal failure")
// Test read failure on left-over data
fin.verifyDataLen = 1
b = bytes.NewBuffer(finished)
h = newHandshakeLayer(newRecordLayer(b))
_, err = h.ReadMessageBody(&fin)
assertError(t, err, "Read message body despite extra data")
}
func TestWriteHandshakeMessage(t *testing.T) {
short, _ := hex.DecodeString(shortHex)
long, _ := hex.DecodeString(longHex)
shortLongShort, _ := hex.DecodeString(shortLongShortHex)
// Test successful write of single message
b := bytes.NewBuffer(nil)
h := newHandshakeLayer(newRecordLayer(b))
err := h.WriteMessage(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 = newHandshakeLayer(newRecordLayer(b))
err = h.WriteMessage(longMessageIn)
assertNotError(t, err, "Failed to write valid long message")
assertByteEquals(t, b.Bytes(), long)
// Test successful write of multiple messages sequentially
b = bytes.NewBuffer(nil)
h = newHandshakeLayer(newRecordLayer(b))
err = h.WriteMessages([]*handshakeMessage{shortMessageIn, longMessageIn, shortMessageIn})
assertNotError(t, err, "Failed to write valid long message")
assertByteEquals(t, b.Bytes(), shortLongShort)
// Test write failure on message too large
b = bytes.NewBuffer(nil)
h = newHandshakeLayer(newRecordLayer(b))
err = h.WriteMessage(tooLongMessageIn)
assertError(t, err, "Wrote a message exceeding the length bound")
// Test write failure on underlying write failure
h = newHandshakeLayer(newRecordLayer(errorReadWriter{}))
err = h.WriteMessage(longMessageIn)
assertError(t, err, "Write succeeded despite error in full fragment send")
err = h.WriteMessage(shortMessageIn)
assertError(t, err, "Write succeeded despite error in last fragment send")
}
func TestWriteHandshakeMessageBody(t *testing.T) {
chValid, _ := hex.DecodeString(chValidHex)
shValid, _ := hex.DecodeString(shValidHex)
chValidMessage, _ := hex.DecodeString(chValidMessageHex)
chshValidMessage, _ := hex.DecodeString(chshValidMessageHex)
// Test succesful write
b := bytes.NewBuffer(nil)
h := newHandshakeLayer(newRecordLayer(b))
hm, err := h.WriteMessageBody(&chValidIn)
assertNotError(t, err, "Failed to write valid short message")
assertByteEquals(t, b.Bytes(), chValidMessage)
assertEquals(t, hm.msgType, handshakeTypeClientHello)
assertByteEquals(t, hm.body, chValid)
// Test succesful write of multiple messages
b = bytes.NewBuffer(nil)
h = newHandshakeLayer(newRecordLayer(b))
hms, err := h.WriteMessageBodies([]handshakeMessageBody{&chValidIn, &shValidIn})
assertNotError(t, err, "Failed to write valid short message")
assertByteEquals(t, b.Bytes(), chshValidMessage)
assertEquals(t, len(hms), 2)
assertEquals(t, hms[0].msgType, handshakeTypeClientHello)
assertByteEquals(t, hms[0].body, chValid)
assertEquals(t, hms[1].msgType, handshakeTypeServerHello)
assertByteEquals(t, hms[1].body, shValid)
// Test write failure on marshal failure
chValidIn.cipherSuites = []cipherSuite{}
b = bytes.NewBuffer(nil)
h = newHandshakeLayer(newRecordLayer(b))
_, err = h.WriteMessageBody(&chValidIn)
assertError(t, err, "Wrote a message body despite a marshal failure")
chValidIn.cipherSuites = chCipherSuites
}