Debian Codegraph @

handshake-messages_test.go

raw symbols SCIP
1 package mint
2
3 import (
4 "bytes"
5 "crypto/x509"
6 "encoding/hex"
7 "testing"
8 )
9
10 const (
11 fixedClientHelloBodyLen = 39
12 fixedServerHelloBodyLen = 36
13 maxCipherSuites = 1 << 15
14 maxExtensionDataLen = (1 << 16) - 1
15 maxCertRequestContextLen = 255
16 maxTicketLen = (1 << 16) - 1
17 )
18
19 var (
20 supportedVersionHex = hex.EncodeToString([]byte{
21 byte(tls13Version >> 8),
22 byte(tls13Version & 0xff),
23 })
24 tls12VersionHex = hex.EncodeToString([]byte{
25 byte(tls12Version >> 8),
26 byte(tls12Version & 0xff),
27 })
28
29 // ClientHello test cases
30 // NB: Borrowing some values from extensions_test.go
31 helloRandom = [32]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
32 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
33 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
34 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37}
35 chCipherSuites = []CipherSuite{0x0001, 0x0002, 0x0003}
36 chValidIn = ClientHelloBody{
37 LegacyVersion: tls12Version,
38 Random: helloRandom,
39 CipherSuites: chCipherSuites,
40 Extensions: extListValidIn,
41 LegacySessionID: []byte{},
42 }
43 chValidHex = "0303" + hex.EncodeToString(helloRandom[:]) + "00" +
44 "0006000100020003" + "0100" + extListValidHex
45 chOverflowHex = "0303" + hex.EncodeToString(helloRandom[:]) + "00" +
46 "0006000100020003" + "0100" + extListOverflowOuterHex
47
48 // ClientHello truncation test cases
49 chTruncPSKData = unhex(pskClientHex)
50 chTruncHex = "01000062" + "0303" + hex.EncodeToString(helloRandom[:]) +
51 "00" + "0006000100020003" + "0100" + "00330029002f000a00040102030405060708"
52 chTruncValid = ClientHelloBody{
53 LegacyVersion: tls12Version,
54 Random: helloRandom,
55 CipherSuites: chCipherSuites,
56 Extensions: []Extension{
57 {
58 ExtensionType: ExtensionTypePreSharedKey,
59 ExtensionData: chTruncPSKData,
60 },
61 },
62 }
63 chTruncInvalid = ClientHelloBody{}
64 chTruncNoExt = ClientHelloBody{
65 LegacyVersion: tls12Version,
66 Random: helloRandom,
67 CipherSuites: chCipherSuites,
68 Extensions: []Extension{},
69 }
70 chTruncNoPSK = ClientHelloBody{
71 LegacyVersion: tls12Version,
72 Random: helloRandom,
73 CipherSuites: chCipherSuites,
74 Extensions: []Extension{
75 {ExtensionType: ExtensionTypeEarlyData},
76 },
77 }
78 chTruncBadPSK = ClientHelloBody{
79 LegacyVersion: tls12Version,
80 Random: helloRandom,
81 CipherSuites: chCipherSuites,
82 Extensions: []Extension{
83 {ExtensionType: ExtensionTypePreSharedKey},
84 },
85 }
86
87 // ServerHello test cases
88 shValidIn = ServerHelloBody{
89 Version: tls12Version,
90 Random: helloRandom,
91 LegacySessionID: []byte{},
92 CipherSuite: CipherSuite(0x0001),
93 Extensions: extListValidIn,
94 }
95 shEmptyIn = ServerHelloBody{
96 Version: tls12Version,
97 Random: helloRandom,
98 CipherSuite: CipherSuite(0x0001),
99 }
100 shValidHex = tls12VersionHex + hex.EncodeToString(helloRandom[:]) + "00" + "0001" + "00" + extListValidHex
101 shEmptyHex = tls12VersionHex + hex.EncodeToString(helloRandom[:]) + "00" + "0001" + "00" + "0000"
102 shOverflowHex = tls12VersionHex + hex.EncodeToString(helloRandom[:]) + "0001" + extListOverflowOuterHex
103
104 // Finished test cases
105 finValidIn = FinishedBody{
106 VerifyDataLen: len(helloRandom),
107 VerifyData: helloRandom[:],
108 }
109 finValidHex = hex.EncodeToString(helloRandom[:])
110
111 // EncryptedExtensions test cases
112 encExtValidIn = EncryptedExtensionsBody{extListValidIn}
113 encExtValidHex = extListValidHex
114
115 // Certificate test cases
116 cert1Hex = "308201653082010ba003020102020500a0a0a0a0300a0608" +
117 "2a8648ce3d0403023017311530130603550403130c657861" +
118 "6d706c65312e636f6d3022180f3030303130313031303030" +
119 "3030305a180f30303031303130313030303030305a301731" +
120 "1530130603550403130c6578616d706c65312e636f6d3059" +
121 "301306072a8648ce3d020106082a8648ce3d030107034200" +
122 "044460e6de2a170e0c7c8d1306c82386db31980bd76647bd" +
123 "e9b96055d075fc64ea7d8d3864afcf0ff16da73c68df6880" +
124 "a597303243410016ef2e36f5962584d187a340303e300e06" +
125 "03551d0f0101ff0404030203a830130603551d25040c300a" +
126 "06082b0601050507030130170603551d110410300e820c65" +
127 "78616d706c65312e636f6d300a06082a8648ce3d04030203" +
128 "48003045022005937d0bf7a7cb4589715bb83dddd2505335" +
129 "829e6305b75cfeae6f2dcc2230b6022100f6f0e75436cd59" +
130 "b94ceedffb18bcf5bb2f161260a282f7b63d1376e5805c51" +
131 "b6"
132 cert2Hex = "308201643082010ba003020102020500a0a0a0a0300a0608" +
133 "2a8648ce3d0403043017311530130603550403130c657861" +
134 "6d706c65322e636f6d3022180f3030303130313031303030" +
135 "3030305a180f30303031303130313030303030305a301731" +
136 "1530130603550403130c6578616d706c65322e636f6d3059" +
137 "301306072a8648ce3d020106082a8648ce3d030107034200" +
138 "044460e6de2a170e0c7c8d1306c82386db31980bd76647bd" +
139 "e9b96055d075fc64ea7d8d3864afcf0ff16da73c68df6880" +
140 "a597303243410016ef2e36f5962584d187a340303e300e06" +
141 "03551d0f0101ff0404030203a830130603551d25040c300a" +
142 "06082b0601050507030130170603551d110410300e820c65" +
143 "78616d706c65322e636f6d300a06082a8648ce3d04030403" +
144 "470030440220718254f2b3c1cc0fa4c53bf43182f8acbc19" +
145 "04e45ee1a3abdc8bc50a155712b4022010664cc29b80fae9" +
146 "150027726da5b144df764a76007eee2a52b6ae0c995395fb"
147 cert1Bytes = unhex(cert1Hex)
148 cert2Bytes = unhex(cert2Hex)
149 cert1, _ = x509.ParseCertificate(cert1Bytes)
150 cert2, _ = x509.ParseCertificate(cert2Bytes)
151
152 certValidIn = CertificateBody{
153 CertificateRequestContext: []byte{0, 0, 0, 0},
154 CertificateList: []CertificateEntry{
155 {
156 CertData: cert1,
157 Extensions: extListValidIn,
158 },
159 {
160 CertData: cert2,
161 Extensions: extListValidIn,
162 },
163 },
164 }
165 certOverflowIn = CertificateBody{
166 CertificateRequestContext: []byte{0, 0, 0, 0},
167 CertificateList: []CertificateEntry{
168 {
169 CertData: cert1,
170 Extensions: extListSingleTooLongIn,
171 },
172 },
173 }
174 certValidHex = "0400000000" +
175 "0002f5" +
176 "000169" + cert1Hex + extListValidHex +
177 "000168" + cert2Hex + extListValidHex
178 certTooShortHex = "000000023081"
179
180 // CertificateVerify test cases
181 certVerifyValidIn = CertificateVerifyBody{
182 Algorithm: ECDSA_P256_SHA256,
183 Signature: []byte{0, 0, 0, 0},
184 }
185 certVerifyValidHex = "0403000400000000"
186 certVerifyCipherSuite = TLS_AES_128_GCM_SHA256
187
188 // CertificateRequest test cases
189 certReqValidIn = CertificateRequestBody{
190 CertificateRequestContext: []byte{0, 1, 2, 3, 4, 5, 6, 7},
191 Extensions: []Extension{
192 {
193 ExtensionType: ExtensionTypeSignatureAlgorithms,
194 ExtensionData: unhex("000404030503"),
195 },
196 },
197 }
198 certReqValidHex = "080001020304050607" + // context
199 "000a000d0006000404030503" // extensions
200
201 // NewSessionTicket test cases
202 ticketValidHex = "00010203" + "04050607" + "0408090a0b" + "00040c0d0e0f" + "0006eeff00021122"
203 ticketValidIn = NewSessionTicketBody{
204 TicketLifetime: 0x00010203,
205 TicketAgeAdd: 0x04050607,
206 TicketNonce: []byte{0x08, 0x09, 0x0a, 0x0b},
207 Ticket: []byte{0x0c, 0x0d, 0x0e, 0x0f},
208 Extensions: []Extension{
209 {
210 ExtensionType: 0xeeff,
211 ExtensionData: []byte{0x11, 0x22},
212 },
213 },
214 }
215 ticketTooBigIn = NewSessionTicketBody{
216 TicketLifetime: 0x00010203,
217 Ticket: make([]byte, maxTicketLen+1),
218 }
219 ticketExtensionsTooBigIn = NewSessionTicketBody{
220 Extensions: extListSingleTooLongIn,
221 }
222
223 // KeyUpdate test cases
224 keyUpdateValidHex = "01"
225 keyUpdateValidIn = KeyUpdateBody{
226 KeyUpdateRequest: KeyUpdateRequested,
227 }
228
229 // EndOfEarlyData test cases
230 endOfEarlyDataValidHex = ""
231 endOfEarlyDataValidIn = EndOfEarlyDataBody{}
232 )
233
234 func TestHandshakeMessageTypes(t *testing.T) {
235 assertEquals(t, ClientHelloBody{}.Type(), HandshakeTypeClientHello)
236 assertEquals(t, ServerHelloBody{}.Type(), HandshakeTypeServerHello)
237 assertEquals(t, FinishedBody{}.Type(), HandshakeTypeFinished)
238 assertEquals(t, EncryptedExtensionsBody{}.Type(), HandshakeTypeEncryptedExtensions)
239 assertEquals(t, CertificateBody{}.Type(), HandshakeTypeCertificate)
240 assertEquals(t, CertificateVerifyBody{}.Type(), HandshakeTypeCertificateVerify)
241 }
242
243 func TestClientHelloMarshalUnmarshal(t *testing.T) {
244 chValid := unhex(chValidHex)
245 chOverflow := unhex(chOverflowHex)
246
247 // Test correctness of handshake type
248 assertEquals(t, (ClientHelloBody{}).Type(), HandshakeTypeClientHello)
249
250 // Test successful marshal
251 out, err := chValidIn.Marshal()
252 assertNotError(t, err, "Failed to marshal a valid ClientHello")
253 assertByteEquals(t, out, chValid)
254
255 // Test marshal failure on empty ciphersuites
256 chValidIn.CipherSuites = []CipherSuite{}
257 out, err = chValidIn.Marshal()
258 assertError(t, err, "Marshaled a ClientHello with no CipherSuites")
259 chValidIn.CipherSuites = chCipherSuites
260
261 // Test marshal failure on too many ciphersuites
262 tooManyCipherSuites := make([]CipherSuite, maxCipherSuites+1)
263 for i := range tooManyCipherSuites {
264 tooManyCipherSuites[i] = CipherSuite(0x0001)
265 }
266 chValidIn.CipherSuites = tooManyCipherSuites
267 out, err = chValidIn.Marshal()
268 assertError(t, err, "Marshaled a ClientHello with too many CipherSuites")
269 chValidIn.CipherSuites = chCipherSuites
270
271 // Test marshal failure on extension list marshal failure
272 chValidIn.Extensions = extListTooLongIn
273 out, err = chValidIn.Marshal()
274 assertError(t, err, "Marshaled a ClientHello with bad extensions")
275 chValidIn.Extensions = extListValidIn
276
277 // Test successful unmarshal
278 var ch ClientHelloBody
279 read, err := ch.Unmarshal(chValid)
280 assertNotError(t, err, "Failed to unmarshal a valid ClientHello")
281 assertEquals(t, read, len(chValid))
282 assertDeepEquals(t, ch, chValidIn)
283
284 // Test unmarshal failure on too-short ClientHello
285 _, err = ch.Unmarshal(chValid[:fixedClientHelloBodyLen-1])
286 assertError(t, err, "Unmarshaled a ClientHello below the min length")
287
288 // Test unmarshal failure on ciphersuite size overflow
289 chValid[35] = 0xFF
290 _, err = ch.Unmarshal(chValid)
291 assertError(t, err, "Unmarshaled a ClientHello an overflowing cipherSuite list")
292 chValid[35] = 0x00
293
294 // Test unmarshal failure on odd ciphersuite size
295 chValid[36] ^= 0x01
296 _, err = ch.Unmarshal(chValid)
297 assertError(t, err, "Unmarshaled a ClientHello an odd cipherSuite list length")
298 chValid[36] ^= 0x01
299
300 // Test unmarshal failure on missing compression methods
301 _, err = ch.Unmarshal(chValid[:37+6])
302 assertError(t, err, "Unmarshaled a ClientHello truncated before the compression methods")
303
304 // Test unmarshal failure on incorrect compression methods
305 chValid[37+6] = 0x03
306 _, err = ch.Unmarshal(chValid)
307 assertError(t, err, "Unmarshaled a ClientHello more than one compression method")
308 chValid[37+6] = 0x01
309 chValid[37+7] = 0x01
310 _, err = ch.Unmarshal(chValid)
311 assertError(t, err, "Unmarshaled a ClientHello the wrong compression method")
312 chValid[37+7] = 0x00
313
314 // Test unmarshal failure on extension list unmarshal failure
315 _, err = ch.Unmarshal(chOverflow)
316 assertError(t, err, "Unmarshaled a ClientHello with invalid extensions")
317 }
318
319 func TestClientHelloTruncate(t *testing.T) {
320 chTrunc := unhex(chTruncHex)
321
322 // Test success
323 trunc, err := chTruncValid.Truncated()
324 assertNotError(t, err, "Error truncating valid ClientHello")
325 assertByteEquals(t, trunc, chTrunc)
326
327 // Test failure on marshal failure
328 _, err = chTruncInvalid.Truncated()
329 assertError(t, err, "Truncated a ClientHello that should not have marshaled")
330
331 // Test failure on no extensions
332 _, err = chTruncNoExt.Truncated()
333 assertError(t, err, "Truncated a ClientHello with no extensions")
334
335 // Test failure on last extension not PSK
336 _, err = chTruncNoPSK.Truncated()
337 assertError(t, err, "Truncated a ClientHello whose last extension was not a PSK")
338
339 // Test failiure on last extension malformed PSK
340 _, err = chTruncBadPSK.Truncated()
341 assertError(t, err, "Truncated a ClientHello with a mal-formed PSK")
342 }
343
344 func TestServerHelloMarshalUnmarshal(t *testing.T) {
345 shValid := unhex(shValidHex)
346 shEmpty := unhex(shEmptyHex)
347 shOverflow := unhex(shOverflowHex)
348
349 // Test correctness of handshake type
350 assertEquals(t, (ServerHelloBody{}).Type(), HandshakeTypeServerHello)
351
352 // Test successful marshal
353 out, err := shValidIn.Marshal()
354 assertNotError(t, err, "Failed to marshal a valid ServerHello")
355 assertByteEquals(t, out, shValid)
356
357 // Test successful marshal with no extensions present
358 out, err = shEmptyIn.Marshal()
359 assertNotError(t, err, "Failed to marshal a valid ServerHello with no extensions")
360 assertByteEquals(t, out, shEmpty)
361
362 // Test marshal failure on extension list marshal failure
363 shValidIn.Extensions = extListTooLongIn
364 out, err = shValidIn.Marshal()
365 assertError(t, err, "Marshaled a ServerHello with bad extensions")
366 shValidIn.Extensions = extListValidIn
367
368 // Test successful unmarshal
369 var sh ServerHelloBody
370 read, err := sh.Unmarshal(shValid)
371 assertNotError(t, err, "Failed to unmarshal a valid ServerHello")
372 assertEquals(t, read, len(shValid))
373 assertDeepEquals(t, sh, shValidIn)
374
375 // Test successful unmarshal with no extensions present
376 read, err = sh.Unmarshal(shEmpty)
377 assertNotError(t, err, "Failed to unmarshal a valid ServerHello")
378 assertEquals(t, read, len(shEmpty))
379 assertByteEquals(t, sh.Random[:], shEmptyIn.Random[:])
380 assertEquals(t, sh.CipherSuite, shEmptyIn.CipherSuite)
381 assertEquals(t, len(sh.Extensions), 0)
382
383 // Test unmarshal failure on too-short ServerHello
384 _, err = sh.Unmarshal(shValid[:fixedServerHelloBodyLen-1])
385 assertError(t, err, "Unmarshaled a too-short ServerHello")
386
387 // Test unmarshal failure on extension list unmarshal failure
388 _, err = sh.Unmarshal(shOverflow)
389 assertError(t, err, "Unmarshaled a ServerHello with invalid extensions")
390 }
391
392 func TestFinishedMarshalUnmarshal(t *testing.T) {
393 finValid := unhex(finValidHex)
394
395 // Test correctness of handshake type
396 assertEquals(t, (FinishedBody{}).Type(), HandshakeTypeFinished)
397
398 // Test successful marshal
399 out, err := finValidIn.Marshal()
400 assertNotError(t, err, "Failed to marshal a valid Finished")
401 assertByteEquals(t, out, finValid)
402
403 // Test marshal failure on incorrect data length
404 finValidIn.VerifyDataLen--
405 out, err = finValidIn.Marshal()
406 assertError(t, err, "Marshaled a Finished with the wrong data length")
407 finValidIn.VerifyDataLen++
408
409 // Test successful unmarshal
410 var fin FinishedBody
411 fin.VerifyDataLen = len(finValid)
412 read, err := fin.Unmarshal(finValid)
413 assertNotError(t, err, "Failed to unmarshal a valid Finished")
414 assertEquals(t, read, len(finValid))
415 assertDeepEquals(t, fin, finValidIn)
416
417 // Test unmarshal failure on insufficient data
418 fin.VerifyDataLen++
419 _, err = fin.Unmarshal(finValid)
420 assertError(t, err, "Unmarshaled a Finished with too little data")
421 fin.VerifyDataLen--
422 }
423
424 // This one is a little brief because it is just an extensionList
425 func TestEncrypteExtensionsMarshalUnmarshal(t *testing.T) {
426 encExtValid := unhex(encExtValidHex)
427
428 // Test correctness of handshake type
429 assertEquals(t, (EncryptedExtensionsBody{}).Type(), HandshakeTypeEncryptedExtensions)
430
431 // Test successful marshal
432 out, err := encExtValidIn.Marshal()
433 assertNotError(t, err, "Failed to marshal a valid EncryptedExtensions")
434 assertByteEquals(t, out, encExtValid)
435
436 // Test successful unmarshal
437 var ee EncryptedExtensionsBody
438 read, err := ee.Unmarshal(encExtValid)
439 assertNotError(t, err, "Failed to unmarshal a valid EncryptedExtensions")
440 assertEquals(t, read, len(encExtValid))
441 assertDeepEquals(t, ee, encExtValidIn)
442 }
443
444 func TestCertificateMarshalUnmarshal(t *testing.T) {
445 // Create a couple of certificates and manually encode
446 certValid := unhex(certValidHex)
447 certTooShort := unhex(certTooShortHex)
448
449 // Test correctness of handshake type
450 assertEquals(t, (CertificateBody{}).Type(), HandshakeTypeCertificate)
451
452 // Test successful marshal
453 out, err := certValidIn.Marshal()
454 assertNotError(t, err, "Failed to marshal valid Certificate")
455 assertByteEquals(t, out, certValid)
456
457 // Test marshal failure on context too long
458 originalContext := certValidIn.CertificateRequestContext
459 certValidIn.CertificateRequestContext = bytes.Repeat([]byte{0}, maxCertRequestContextLen+1)
460 out, err = certValidIn.Marshal()
461 assertError(t, err, "Marshaled a Certificate with a too-long context")
462 certValidIn.CertificateRequestContext = originalContext
463
464 // Test marshal failure on no raw certa
465 originalRaw := cert1.Raw
466 cert1.Raw = []byte{}
467 out, err = certValidIn.Marshal()
468 assertError(t, err, "Marshaled a Certificate with an empty cert")
469 cert1.Raw = originalRaw
470
471 // Test marshal failure on extension list marshal failure
472 out, err = certOverflowIn.Marshal()
473 assertError(t, err, "Marshaled a Certificate with an too-long extension list")
474
475 // Test successful unmarshal
476 cert := CertificateBody{}
477 read, err := cert.Unmarshal(certValid)
478 assertNotError(t, err, "Failed to unmarshal valid Certificate")
479 assertEquals(t, read, len(certValid))
480 assertDeepEquals(t, cert, certValidIn)
481
482 // Test unmarshal failure on truncated header
483 _, err = cert.Unmarshal(certValid[:0])
484 assertError(t, err, "Unmarshaled a Certificate with a truncated header")
485
486 // Test unmarshal failure on truncated context
487 _, err = cert.Unmarshal(certValid[:7])
488 assertError(t, err, "Unmarshaled a Certificate with a truncated context")
489
490 // Test unmarshal failure on truncated certificates
491 _, err = cert.Unmarshal(certValid[:12])
492 assertError(t, err, "Unmarshaled a Certificate with truncated certificates")
493
494 // Test unmarshal failure on a too-short certificates field
495 _, err = cert.Unmarshal(certTooShort)
496 assertError(t, err, "Unmarshaled a Certificate with truncated certificate length")
497
498 // Test unmarshal failure on truncated certificate
499 certValid[8] ^= 0xFF // Make length of first cert huge
500 _, err = cert.Unmarshal(certValid)
501 assertError(t, err, "Unmarshaled a Certificate with truncated certificates")
502 certValid[8] ^= 0xFF
503
504 // Test unmarshal failure on malformed certificate
505 certValid[11] ^= 0xFF // Clobber first octet of first cert
506 _, err = cert.Unmarshal(certValid)
507 assertError(t, err, "Unmarshaled a Certificate with truncated certificates")
508 certValid[11] ^= 0xFF
509 }
510
511 func TestCertificateVerifyMarshalUnmarshal(t *testing.T) {
512 certVerifyValid := unhex(certVerifyValidHex)
513
514 handshakeHash := []byte{0, 1, 2, 3}
515
516 privRSA, err := newSigningKey(RSA_PSS_SHA256)
517 assertNotError(t, err, "failed to generate RSA private key")
518
519 // Test correctness of handshake type
520 assertEquals(t, (CertificateVerifyBody{}).Type(), HandshakeTypeCertificateVerify)
521
522 // Test successful marshal
523 out, err := certVerifyValidIn.Marshal()
524 assertNotError(t, err, "Failed to marshal a valid CertificateVerify")
525 assertByteEquals(t, out, certVerifyValid)
526
527 // Test successful unmarshal
528 var cv CertificateVerifyBody
529 read, err := cv.Unmarshal(certVerifyValid)
530 assertNotError(t, err, "Failed to unmarshal a valid CertificateVerify")
531 assertEquals(t, read, len(certVerifyValid))
532 assertDeepEquals(t, cv, certVerifyValidIn)
533
534 // Test unmarshal failure on truncated header
535 _, err = cv.Unmarshal(certVerifyValid[:1])
536 assertError(t, err, "Unmarshaled a CertificateVerify with no header")
537
538 // Test unmarshal failure on truncated signature
539 _, err = cv.Unmarshal(certVerifyValid[:5])
540 assertError(t, err, "Unmarshaled a CertificateVerify with no header")
541
542 // Test successful sign / verify round-trip
543 certVerifyValidIn.Algorithm = RSA_PSS_SHA256
544 err = certVerifyValidIn.Sign(privRSA, handshakeHash)
545 assertNotError(t, err, "Failed to sign CertificateVerify")
546
547 // Test sign failure on algorithm
548 originalAlg := certVerifyValidIn.Algorithm
549 certVerifyValidIn.Algorithm = SignatureScheme(0)
550 err = certVerifyValidIn.Sign(privRSA, handshakeHash)
551 assertError(t, err, "Signed CertificateVerify despite bad algorithm")
552 certVerifyValidIn.Algorithm = originalAlg
553
554 // Test successful verify
555 certVerifyValidIn = CertificateVerifyBody{Algorithm: RSA_PSS_SHA256}
556 err = certVerifyValidIn.Sign(privRSA, handshakeHash)
557 assertNotError(t, err, "Failed to sign CertificateVerify")
558 err = certVerifyValidIn.Verify(privRSA.Public(), handshakeHash)
559 assertNotError(t, err, "Failed to verify CertificateVerify")
560
561 // Test verify failure on bad algorithm
562 originalAlg = certVerifyValidIn.Algorithm
563 certVerifyValidIn.Algorithm = SignatureScheme(0)
564 err = certVerifyValidIn.Verify(privRSA.Public(), handshakeHash)
565 assertError(t, err, "Verified CertificateVerify despite bad hash algorithm")
566 certVerifyValidIn.Algorithm = originalAlg
567 }
568
569 func TestCertificateRequestMarshalUnmarshal(t *testing.T) {
570 certReqValid := unhex(certReqValidHex)
571
572 // Test correctness of handshake type
573 assertEquals(t, (CertificateRequestBody{}).Type(), HandshakeTypeCertificateRequest)
574
575 // Test successful marshal
576 out, err := certReqValidIn.Marshal()
577 assertNotError(t, err, "Failed to marshal a valid CertificateRequest")
578 assertByteEquals(t, out, certReqValid)
579
580 // Test successful unmarshal
581 var cv CertificateRequestBody
582 read, err := cv.Unmarshal(certReqValid)
583 assertNotError(t, err, "Failed to unmarshal a valid CertificateRequest")
584 assertEquals(t, read, len(certReqValid))
585 assertDeepEquals(t, cv, certReqValidIn)
586
587 }
588
589 func TestNewSessionTicketMarshalUnmarshal(t *testing.T) {
590 ticketValid := unhex(ticketValidHex)
591
592 // Test correctness of handshake type
593 assertEquals(t, (NewSessionTicketBody{}).Type(), HandshakeTypeNewSessionTicket)
594
595 // Test creation of a new random ticket
596 tkt, err := NewSessionTicket(16, 3)
597 assertNotError(t, err, "Failed to create session ticket")
598 assertEquals(t, tkt.TicketLifetime, uint32(3))
599 assertEquals(t, len(tkt.Ticket), 16)
600
601 // Test successful marshal
602 out, err := ticketValidIn.Marshal()
603 assertNotError(t, err, "Failed to marshal a valid NewSessionTicket")
604 assertByteEquals(t, out, ticketValid)
605
606 // Test marshal failure on a ticket that's too large
607 out, err = ticketTooBigIn.Marshal()
608 assertError(t, err, "Marshaled a NewSessionTicket with an invalid data length")
609
610 // Test marshal failure on extensions too large
611 out, err = ticketExtensionsTooBigIn.Marshal()
612 assertError(t, err, "Marshaled a NewSessionTicket with extensions that are too big")
613
614 // Test successful unmarshal
615 read, err := tkt.Unmarshal(ticketValid)
616 assertNotError(t, err, "Failed to unmarshal a valid NewSessionTicket")
617 assertEquals(t, read, len(ticketValid))
618 assertDeepEquals(t, *tkt, ticketValidIn)
619
620 // Test unmarshal failure on insufficient data
621 _, err = tkt.Unmarshal(ticketValid[:4])
622 assertError(t, err, "Unmarshaled a NewSessionTicket with an incomplete header")
623
624 _, err = tkt.Unmarshal(ticketValid[:13])
625 assertError(t, err, "Unmarshaled a NewSessionTicket with an incomplete ticket")
626
627 _, err = tkt.Unmarshal(ticketValid[:20])
628 assertError(t, err, "Unmarshaled a NewSessionTicket with incomplete extensions")
629 }
630
631 func TestKeyUpdateMarshalUnmarshal(t *testing.T) {
632 keyUpdateValid := unhex(keyUpdateValidHex)
633
634 // Test correctness of handshake type
635 assertEquals(t, (KeyUpdateBody{}).Type(), HandshakeTypeKeyUpdate)
636
637 // Test successful marshal
638 out, err := keyUpdateValidIn.Marshal()
639 assertNotError(t, err, "Failed to marshal a valid KeyUpdate")
640 assertByteEquals(t, out, keyUpdateValid)
641
642 // Test successful unmarshal
643 var ku KeyUpdateBody
644 read, err := ku.Unmarshal(keyUpdateValid)
645 assertNotError(t, err, "Failed to unmarshal a valid KeyUpdate")
646 assertEquals(t, read, len(keyUpdateValid))
647 assertDeepEquals(t, ku, keyUpdateValidIn)
648 }
649
650 func TestEndOfEarlyDataMarshalUnmarshal(t *testing.T) {
651 endOfEarlyDataValid := unhex(endOfEarlyDataValidHex)
652
653 // Test correctness of handshake type
654 assertEquals(t, (EndOfEarlyDataBody{}).Type(), HandshakeTypeEndOfEarlyData)
655
656 // Test successful marshal
657 out, err := endOfEarlyDataValidIn.Marshal()
658 assertNotError(t, err, "Failed to marshal a valid KeyUpdate")
659 assertByteEquals(t, out, endOfEarlyDataValid)
660
661 // Test successful unmarshal
662 var eoed EndOfEarlyDataBody
663 read, err := eoed.Unmarshal(endOfEarlyDataValid)
664 assertNotError(t, err, "Failed to unmarshal a valid KeyUpdate")
665 assertEquals(t, read, len(endOfEarlyDataValid))
666 assertDeepEquals(t, eoed, endOfEarlyDataValidIn)
667 }
668
669 func TestsafeUnmarshal(t *testing.T) {
670 chValid := unhex(chValidHex)
671 tooLong := append(chValid, 0)
672 var ch ClientHelloBody
673
674 // Check that safeUnmarshal works normally
675 err := safeUnmarshal(&ch, chValid)
676 assertNotError(t, err, "Failed to unmarshal ClientHello")
677
678 // Test successful unmarshal
679 read, err := ch.Unmarshal(tooLong)
680 assertNotError(t, err, "Failed to unmarshal a too long ClientHello")
681 assertEquals(t, read, len(chValid))
682 assertDeepEquals(t, ch, chValidIn)
683
684 // Now test that safeUnmarshal barfs
685 err = safeUnmarshal(&ch, tooLong)
686 assertError(t, err, "Unmarshalled something too long")
687 }