forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tls.py
1403 lines (1205 loc) · 50.3 KB
/
test_tls.py
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import binascii
import datetime
import ssl
from unittest import TestCase
from unittest.mock import patch
from aioquic import tls
from aioquic.buffer import Buffer, BufferReadError
from aioquic.quic.configuration import QuicConfiguration
from aioquic.tls import (
Certificate,
CertificateVerify,
ClientHello,
Context,
EncryptedExtensions,
Finished,
NewSessionTicket,
ServerHello,
State,
load_pem_x509_certificates,
pull_block,
pull_certificate,
pull_certificate_verify,
pull_client_hello,
pull_encrypted_extensions,
pull_finished,
pull_new_session_ticket,
pull_server_hello,
push_certificate,
push_certificate_verify,
push_client_hello,
push_encrypted_extensions,
push_finished,
push_new_session_ticket,
push_server_hello,
verify_certificate,
)
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.primitives import serialization
from .utils import (
SERVER_CACERTFILE,
SERVER_CERTFILE,
SERVER_KEYFILE,
generate_ec_certificate,
generate_ed448_certificate,
generate_ed25519_certificate,
load,
)
CERTIFICATE_DATA = load("tls_certificate.bin")[11:-2]
CERTIFICATE_VERIFY_SIGNATURE = load("tls_certificate_verify.bin")[-384:]
CLIENT_QUIC_TRANSPORT_PARAMETERS = binascii.unhexlify(
b"ff0000110031000500048010000000060004801000000007000480100000000"
b"4000481000000000100024258000800024064000a00010a"
)
SERVER_QUIC_TRANSPORT_PARAMETERS = binascii.unhexlify(
b"ff00001104ff000011004500050004801000000006000480100000000700048"
b"010000000040004810000000001000242580002001000000000000000000000"
b"000000000000000800024064000a00010a"
)
SERVER_QUIC_TRANSPORT_PARAMETERS_2 = binascii.unhexlify(
b"0057000600048000ffff000500048000ffff00020010c5ac410fbdd4fe6e2c1"
b"42279f231e8e0000a000103000400048005fffa000b000119000100026710ff"
b"42000c5c067f27e39321c63e28e7c90003000247e40008000106"
)
SERVER_QUIC_TRANSPORT_PARAMETERS_3 = binascii.unhexlify(
b"0054000200100dcb50a442513295b4679baf04cb5effff8a0009c8afe72a6397"
b"255407000600048000ffff0008000106000400048005fffa000500048000ffff"
b"0003000247e4000a000103000100026710000b000119"
)
class BufferTest(TestCase):
def test_pull_block_truncated(self):
buf = Buffer(capacity=0)
with self.assertRaises(BufferReadError):
with pull_block(buf, 1):
pass
def create_buffers():
return {
tls.Epoch.INITIAL: Buffer(capacity=4096),
tls.Epoch.HANDSHAKE: Buffer(capacity=4096),
tls.Epoch.ONE_RTT: Buffer(capacity=4096),
}
def merge_buffers(buffers):
return b"".join(x.data for x in buffers.values())
def reset_buffers(buffers):
for k in buffers.keys():
buffers[k].seek(0)
class ContextTest(TestCase):
def create_client(
self, alpn_protocols=None, cadata=None, cafile=SERVER_CACERTFILE, **kwargs
):
client = Context(
alpn_protocols=alpn_protocols,
cadata=cadata,
cafile=cafile,
is_client=True,
**kwargs,
)
client.handshake_extensions = [
(
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS,
CLIENT_QUIC_TRANSPORT_PARAMETERS,
)
]
self.assertEqual(client.state, State.CLIENT_HANDSHAKE_START)
return client
def create_server(self, alpn_protocols=None, **kwargs):
configuration = QuicConfiguration(is_client=False)
configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE)
server = Context(
alpn_protocols=alpn_protocols,
is_client=False,
max_early_data=0xFFFFFFFF,
**kwargs,
)
server.certificate = configuration.certificate
server.certificate_private_key = configuration.private_key
server.handshake_extensions = [
(
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS,
SERVER_QUIC_TRANSPORT_PARAMETERS,
)
]
self.assertEqual(server.state, State.SERVER_EXPECT_CLIENT_HELLO)
return server
def test_client_unexpected_message(self):
client = self.create_client()
client.state = State.CLIENT_EXPECT_SERVER_HELLO
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
client.state = State.CLIENT_EXPECT_ENCRYPTED_EXTENSIONS
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
client.state = State.CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
client.state = State.CLIENT_EXPECT_CERTIFICATE_VERIFY
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
client.state = State.CLIENT_EXPECT_FINISHED
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
client.state = State.CLIENT_POST_HANDSHAKE
with self.assertRaises(tls.AlertUnexpectedMessage):
client.handle_message(b"\x00\x00\x00\x00", create_buffers())
def test_client_bad_certificate_verify_data(self):
client = self.create_client()
server = self.create_server()
# Send client hello.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
reset_buffers(client_buf)
# Handle client hello.
#
# send server hello, encrypted extensions, certificate, certificate verify,
# finished.
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
client_input = merge_buffers(server_buf)
reset_buffers(server_buf)
# Mess with certificate verify.
client_input = client_input[:-56] + bytes(4) + client_input[-52:]
# Handle server hello, encrypted extensions, certificate, certificate verify,
# finished.
with self.assertRaises(tls.AlertDecryptError):
client.handle_message(client_input, client_buf)
def test_client_bad_finished_verify_data(self):
client = self.create_client()
server = self.create_server()
# Send client hello.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
reset_buffers(client_buf)
# Handle client hello.
#
# Send server hello, encrypted extensions, certificate, certificate verify,
# finished.
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
client_input = merge_buffers(server_buf)
reset_buffers(server_buf)
# Mess with finished verify data.
client_input = client_input[:-4] + bytes(4)
# Handle server hello, encrypted extensions, certificate, certificate verify,
# finished.
with self.assertRaises(tls.AlertDecryptError):
client.handle_message(client_input, client_buf)
def test_server_unexpected_message(self):
server = self.create_server()
server.state = State.SERVER_EXPECT_CLIENT_HELLO
with self.assertRaises(tls.AlertUnexpectedMessage):
server.handle_message(b"\x00\x00\x00\x00", create_buffers())
server.state = State.SERVER_EXPECT_FINISHED
with self.assertRaises(tls.AlertUnexpectedMessage):
server.handle_message(b"\x00\x00\x00\x00", create_buffers())
server.state = State.SERVER_POST_HANDSHAKE
with self.assertRaises(tls.AlertUnexpectedMessage):
server.handle_message(b"\x00\x00\x00\x00", create_buffers())
def _server_fail_hello(self, client, server):
# Send client hello.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
reset_buffers(client_buf)
# Handle client hello.
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
def test_server_unsupported_cipher_suite(self):
client = self.create_client(cipher_suites=[tls.CipherSuite.AES_128_GCM_SHA256])
server = self.create_server(cipher_suites=[tls.CipherSuite.AES_256_GCM_SHA384])
with self.assertRaises(tls.AlertHandshakeFailure) as cm:
self._server_fail_hello(client, server)
self.assertEqual(str(cm.exception), "No supported cipher suite")
def test_server_unsupported_signature_algorithm(self):
client = self.create_client()
client._signature_algorithms = [tls.SignatureAlgorithm.ED448]
server = self.create_server()
with self.assertRaises(tls.AlertHandshakeFailure) as cm:
self._server_fail_hello(client, server)
self.assertEqual(str(cm.exception), "No supported signature algorithm")
def test_server_unsupported_version(self):
client = self.create_client()
client._supported_versions = [tls.TLS_VERSION_1_2]
server = self.create_server()
with self.assertRaises(tls.AlertProtocolVersion) as cm:
self._server_fail_hello(client, server)
self.assertEqual(str(cm.exception), "No supported protocol version")
def test_server_bad_finished_verify_data(self):
client = self.create_client()
server = self.create_server()
# Send client hello.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
reset_buffers(client_buf)
# Handle client hello.
#
# Send server hello, encrypted extensions, certificate, certificate verify,
# finished.
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
client_input = merge_buffers(server_buf)
reset_buffers(server_buf)
# Handle server hello, encrypted extensions, certificate, certificate verify,
# finished.
#
# Send finished.
client.handle_message(client_input, client_buf)
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE)
server_input = merge_buffers(client_buf)
reset_buffers(client_buf)
# Mess with finished verify data.
server_input = server_input[:-4] + bytes(4)
# Handle finished.
with self.assertRaises(tls.AlertDecryptError):
server.handle_message(server_input, server_buf)
def _handshake(self, client, server):
# Send client hello.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
self.assertGreaterEqual(len(server_input), 181)
self.assertLessEqual(len(server_input), 358)
reset_buffers(client_buf)
# Handle client hello.
#
# Send server hello, encrypted extensions, certificate, certificate verify,
# finished, (session ticket).
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
client_input = merge_buffers(server_buf)
self.assertGreaterEqual(len(client_input), 587)
self.assertLessEqual(len(client_input), 2316)
reset_buffers(server_buf)
# Handle server hello, encrypted extensions, certificate, certificate verify,
# finished, (session ticket).
#
# Send finished.
client.handle_message(client_input, client_buf)
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE)
server_input = merge_buffers(client_buf)
self.assertEqual(len(server_input), 52)
reset_buffers(client_buf)
# Handle finished.
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE)
client_input = merge_buffers(server_buf)
self.assertEqual(len(client_input), 0)
# check keys match
self.assertEqual(client._dec_key, server._enc_key)
self.assertEqual(client._enc_key, server._dec_key)
# check cipher suite
self.assertEqual(
client.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384
)
self.assertEqual(
server.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384
)
def test_handshake(self):
client = self.create_client()
server = self.create_server()
self._handshake(client, server)
# check ALPN matches
self.assertEqual(client.alpn_negotiated, None)
self.assertEqual(server.alpn_negotiated, None)
def _test_handshake_with_certificate(self, certificate, private_key):
server = self.create_server()
server.certificate = certificate
server.certificate_private_key = private_key
client = self.create_client(
cadata=server.certificate.public_bytes(serialization.Encoding.PEM),
cafile=None,
)
self._handshake(client, server)
# check ALPN matches
self.assertEqual(client.alpn_negotiated, None)
self.assertEqual(server.alpn_negotiated, None)
def test_handshake_with_ec_certificate(self):
self._test_handshake_with_certificate(
*generate_ec_certificate(common_name="example.com")
)
def test_handshake_with_ed25519_certificate(self):
self._test_handshake_with_certificate(
*generate_ed25519_certificate(common_name="example.com")
)
def test_handshake_with_ed448_certificate(self):
self._test_handshake_with_certificate(
*generate_ed448_certificate(common_name="example.com")
)
def test_handshake_with_alpn(self):
client = self.create_client(alpn_protocols=["hq-20"])
server = self.create_server(alpn_protocols=["hq-20", "h3-20"])
self._handshake(client, server)
# check ALPN matches
self.assertEqual(client.alpn_negotiated, "hq-20")
self.assertEqual(server.alpn_negotiated, "hq-20")
def test_handshake_with_alpn_fail(self):
client = self.create_client(alpn_protocols=["hq-20"])
server = self.create_server(alpn_protocols=["h3-20"])
with self.assertRaises(tls.AlertHandshakeFailure) as cm:
self._handshake(client, server)
self.assertEqual(str(cm.exception), "No common ALPN protocols")
def test_handshake_with_rsa_pkcs1_sha256_signature(self):
client = self.create_client()
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PKCS1_SHA256]
server = self.create_server()
self._handshake(client, server)
def test_handshake_with_certificate_error(self):
client = self.create_client(cafile=None)
server = self.create_server()
with self.assertRaises(tls.AlertBadCertificate) as cm:
self._handshake(client, server)
self.assertEqual(str(cm.exception), "unable to get local issuer certificate")
def test_handshake_with_certificate_no_verify(self):
client = self.create_client(cafile=None, verify_mode=ssl.CERT_NONE)
server = self.create_server()
self._handshake(client, server)
def test_handshake_with_grease_group(self):
client = self.create_client()
client._supported_groups = [tls.Group.GREASE, tls.Group.SECP256R1]
server = self.create_server()
self._handshake(client, server)
def test_handshake_with_x25519(self):
client = self.create_client()
client._supported_groups = [tls.Group.X25519]
server = self.create_server()
try:
self._handshake(client, server)
except UnsupportedAlgorithm as exc:
self.skipTest(str(exc))
def test_handshake_with_x448(self):
client = self.create_client()
client._supported_groups = [tls.Group.X448]
server = self.create_server()
try:
self._handshake(client, server)
except UnsupportedAlgorithm as exc:
self.skipTest(str(exc))
def test_session_ticket(self):
client_tickets = []
server_tickets = []
def client_new_ticket(ticket):
client_tickets.append(ticket)
def server_get_ticket(label):
for t in server_tickets:
if t.ticket == label:
return t
return None
def server_new_ticket(ticket):
server_tickets.append(ticket)
def first_handshake():
client = self.create_client()
client.new_session_ticket_cb = client_new_ticket
server = self.create_server()
server.new_session_ticket_cb = server_new_ticket
self._handshake(client, server)
# check session resumption was not used
self.assertFalse(client.session_resumed)
self.assertFalse(server.session_resumed)
# check tickets match
self.assertEqual(len(client_tickets), 1)
self.assertEqual(len(server_tickets), 1)
self.assertEqual(client_tickets[0].ticket, server_tickets[0].ticket)
self.assertEqual(
client_tickets[0].resumption_secret, server_tickets[0].resumption_secret
)
def second_handshake():
client = self.create_client()
client.session_ticket = client_tickets[0]
server = self.create_server()
server.get_session_ticket_cb = server_get_ticket
# Send client hello with pre_shared_key.
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
self.assertGreaterEqual(len(server_input), 383)
self.assertLessEqual(len(server_input), 483)
reset_buffers(client_buf)
# Handle client hello.
#
# Send server hello, encrypted extensions, finished.
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
client_input = merge_buffers(server_buf)
self.assertEqual(len(client_input), 275)
reset_buffers(server_buf)
# Handle server hello, encrypted extensions, certificate,
# certificate verify, finished.
#
# Send finished.
client.handle_message(client_input, client_buf)
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE)
server_input = merge_buffers(client_buf)
self.assertEqual(len(server_input), 52)
reset_buffers(client_buf)
# Handle finished.
#
# Send new_session_ticket.
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE)
client_input = merge_buffers(server_buf)
self.assertEqual(len(client_input), 0)
reset_buffers(server_buf)
# check keys match
self.assertEqual(client._dec_key, server._enc_key)
self.assertEqual(client._enc_key, server._dec_key)
# check session resumption was used
self.assertTrue(client.session_resumed)
self.assertTrue(server.session_resumed)
def second_handshake_bad_binder():
client = self.create_client()
client.session_ticket = client_tickets[0]
server = self.create_server()
server.get_session_ticket_cb = server_get_ticket
# send client hello with pre_shared_key
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
self.assertGreaterEqual(len(server_input), 383)
self.assertLessEqual(len(server_input), 483)
reset_buffers(client_buf)
# tamper with binder
server_input = server_input[:-4] + bytes(4)
# handle client hello
# send server hello, encrypted extensions, finished
server_buf = create_buffers()
with self.assertRaises(tls.AlertHandshakeFailure) as cm:
server.handle_message(server_input, server_buf)
self.assertEqual(str(cm.exception), "PSK validation failed")
def second_handshake_bad_pre_shared_key():
client = self.create_client()
client.session_ticket = client_tickets[0]
server = self.create_server()
server.get_session_ticket_cb = server_get_ticket
# send client hello with pre_shared_key
client_buf = create_buffers()
client.handle_message(b"", client_buf)
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO)
server_input = merge_buffers(client_buf)
self.assertGreaterEqual(len(server_input), 383)
self.assertLessEqual(len(server_input), 483)
reset_buffers(client_buf)
# handle client hello
# send server hello, encrypted extensions, finished
server_buf = create_buffers()
server.handle_message(server_input, server_buf)
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED)
# tamper with pre_share_key index
buf = server_buf[tls.Epoch.INITIAL]
buf.seek(buf.tell() - 1)
buf.push_uint8(1)
client_input = merge_buffers(server_buf)
self.assertEqual(len(client_input), 275)
reset_buffers(server_buf)
# handle server hello and bomb
with self.assertRaises(tls.AlertIllegalParameter):
client.handle_message(client_input, client_buf)
first_handshake()
second_handshake()
second_handshake_bad_binder()
second_handshake_bad_pre_shared_key()
class TlsTest(TestCase):
def test_pull_client_hello(self):
buf = Buffer(data=load("tls_client_hello.bin"))
hello = pull_client_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"18b2b23bf3e44b5d52ccfe7aecbc5ff14eadc3d349fabf804d71f165ae76e7d5"
),
)
self.assertEqual(
hello.legacy_session_id,
binascii.unhexlify(
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235"
),
)
self.assertEqual(
hello.cipher_suites,
[
tls.CipherSuite.AES_256_GCM_SHA384,
tls.CipherSuite.AES_128_GCM_SHA256,
tls.CipherSuite.CHACHA20_POLY1305_SHA256,
],
)
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL])
# extensions
self.assertEqual(hello.alpn_protocols, None)
self.assertEqual(
hello.key_share,
[
(
tls.Group.SECP256R1,
binascii.unhexlify(
"047bfea344467535054263b75def60cffa82405a211b68d1eb8d1d944e67aef8"
"93c7665a5473d032cfaf22a73da28eb4aacae0017ed12557b5791f98a1e84f15"
"b0"
),
)
],
)
self.assertEqual(
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE]
)
self.assertEqual(hello.server_name, None)
self.assertEqual(
hello.signature_algorithms,
[
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256,
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA1,
],
)
self.assertEqual(hello.supported_groups, [tls.Group.SECP256R1])
self.assertEqual(
hello.supported_versions,
[
tls.TLS_VERSION_1_3,
tls.TLS_VERSION_1_3_DRAFT_28,
tls.TLS_VERSION_1_3_DRAFT_27,
tls.TLS_VERSION_1_3_DRAFT_26,
],
)
self.assertEqual(
hello.other_extensions,
[
(
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT,
CLIENT_QUIC_TRANSPORT_PARAMETERS,
)
],
)
def test_pull_client_hello_with_alpn(self):
buf = Buffer(data=load("tls_client_hello_with_alpn.bin"))
hello = pull_client_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"ed575c6fbd599c4dfaabd003dca6e860ccdb0e1782c1af02e57bf27cb6479b76"
),
)
self.assertEqual(hello.legacy_session_id, b"")
self.assertEqual(
hello.cipher_suites,
[
tls.CipherSuite.AES_128_GCM_SHA256,
tls.CipherSuite.AES_256_GCM_SHA384,
tls.CipherSuite.CHACHA20_POLY1305_SHA256,
tls.CipherSuite.EMPTY_RENEGOTIATION_INFO_SCSV,
],
)
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL])
# extensions
self.assertEqual(hello.alpn_protocols, ["h3-19"])
self.assertEqual(hello.early_data, False)
self.assertEqual(
hello.key_share,
[
(
tls.Group.SECP256R1,
binascii.unhexlify(
"048842315c437bb0ce2929c816fee4e942ec5cb6db6a6b9bf622680188ebb0d4"
"b652e69033f71686aa01cbc79155866e264c9f33f45aa16b0dfa10a222e3a669"
"22"
),
)
],
)
self.assertEqual(
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE]
)
self.assertEqual(hello.server_name, "cloudflare-quic.com")
self.assertEqual(
hello.signature_algorithms,
[
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256,
tls.SignatureAlgorithm.ECDSA_SECP384R1_SHA384,
tls.SignatureAlgorithm.ECDSA_SECP521R1_SHA512,
tls.SignatureAlgorithm.ED25519,
tls.SignatureAlgorithm.ED448,
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA256,
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA384,
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA512,
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256,
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA384,
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA512,
tls.SignatureAlgorithm.RSA_PKCS1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA384,
tls.SignatureAlgorithm.RSA_PKCS1_SHA512,
],
)
self.assertEqual(
hello.supported_groups,
[
tls.Group.SECP256R1,
tls.Group.X25519,
tls.Group.SECP384R1,
tls.Group.SECP521R1,
],
)
self.assertEqual(hello.supported_versions, [tls.TLS_VERSION_1_3])
# serialize
buf = Buffer(1000)
push_client_hello(buf, hello)
self.assertEqual(len(buf.data), len(load("tls_client_hello_with_alpn.bin")))
def test_pull_client_hello_with_psk(self):
buf = Buffer(data=load("tls_client_hello_with_psk.bin"))
hello = pull_client_hello(buf)
self.assertEqual(hello.early_data, True)
self.assertEqual(
hello.pre_shared_key,
tls.OfferedPsks(
identities=[
(
binascii.unhexlify(
"fab3dc7d79f35ea53e9adf21150e601591a750b80cde0cd167fef6e0cdbc032a"
"c4161fc5c5b66679de49524bd5624c50d71ba3e650780a4bfe402d6a06a00525"
"0b5dc52085233b69d0dd13924cc5c713a396784ecafc59f5ea73c1585d79621b"
"8a94e4f2291b17427d5185abf4a994fca74ee7a7f993a950c71003fc7cf8"
),
2067156378,
)
],
binders=[
binascii.unhexlify(
"1788ad43fdff37cfc628f24b6ce7c8c76180705380da17da32811b5bae4e78"
"d7aaaf65a9b713872f2bb28818ca1a6b01"
)
],
),
)
self.assertTrue(buf.eof())
# serialize
buf = Buffer(1000)
push_client_hello(buf, hello)
self.assertEqual(buf.data, load("tls_client_hello_with_psk.bin"))
def test_pull_client_hello_with_sni(self):
buf = Buffer(data=load("tls_client_hello_with_sni.bin"))
hello = pull_client_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"987d8934140b0a42cc5545071f3f9f7f61963d7b6404eb674c8dbe513604346b"
),
)
self.assertEqual(
hello.legacy_session_id,
binascii.unhexlify(
"26b19bdd30dbf751015a3a16e13bd59002dfe420b799d2a5cd5e11b8fa7bcb66"
),
)
self.assertEqual(
hello.cipher_suites,
[
tls.CipherSuite.AES_256_GCM_SHA384,
tls.CipherSuite.AES_128_GCM_SHA256,
tls.CipherSuite.CHACHA20_POLY1305_SHA256,
],
)
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL])
# extensions
self.assertEqual(hello.alpn_protocols, None)
self.assertEqual(
hello.key_share,
[
(
tls.Group.SECP256R1,
binascii.unhexlify(
"04b62d70f907c814cd65d0f73b8b991f06b70c77153f548410a191d2b19764a2"
"ecc06065a480efa9e1f10c8da6e737d5bfc04be3f773e20a0c997f51b5621280"
"40"
),
)
],
)
self.assertEqual(
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE]
)
self.assertEqual(hello.server_name, "cloudflare-quic.com")
self.assertEqual(
hello.signature_algorithms,
[
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256,
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA1,
],
)
self.assertEqual(hello.supported_groups, [tls.Group.SECP256R1])
self.assertEqual(
hello.supported_versions,
[
tls.TLS_VERSION_1_3,
tls.TLS_VERSION_1_3_DRAFT_28,
tls.TLS_VERSION_1_3_DRAFT_27,
tls.TLS_VERSION_1_3_DRAFT_26,
],
)
self.assertEqual(
hello.other_extensions,
[
(
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT,
CLIENT_QUIC_TRANSPORT_PARAMETERS,
)
],
)
# serialize
buf = Buffer(1000)
push_client_hello(buf, hello)
self.assertEqual(buf.data, load("tls_client_hello_with_sni.bin"))
def test_push_client_hello(self):
hello = ClientHello(
random=binascii.unhexlify(
"18b2b23bf3e44b5d52ccfe7aecbc5ff14eadc3d349fabf804d71f165ae76e7d5"
),
legacy_session_id=binascii.unhexlify(
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235"
),
cipher_suites=[
tls.CipherSuite.AES_256_GCM_SHA384,
tls.CipherSuite.AES_128_GCM_SHA256,
tls.CipherSuite.CHACHA20_POLY1305_SHA256,
],
legacy_compression_methods=[tls.CompressionMethod.NULL],
key_share=[
(
tls.Group.SECP256R1,
binascii.unhexlify(
"047bfea344467535054263b75def60cffa82405a211b68d1eb8d1d944e67aef8"
"93c7665a5473d032cfaf22a73da28eb4aacae0017ed12557b5791f98a1e84f15"
"b0"
),
)
],
psk_key_exchange_modes=[tls.PskKeyExchangeMode.PSK_DHE_KE],
signature_algorithms=[
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256,
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA256,
tls.SignatureAlgorithm.RSA_PKCS1_SHA1,
],
supported_groups=[tls.Group.SECP256R1],
supported_versions=[
tls.TLS_VERSION_1_3,
tls.TLS_VERSION_1_3_DRAFT_28,
tls.TLS_VERSION_1_3_DRAFT_27,
tls.TLS_VERSION_1_3_DRAFT_26,
],
other_extensions=[
(
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT,
CLIENT_QUIC_TRANSPORT_PARAMETERS,
)
],
)
buf = Buffer(1000)
push_client_hello(buf, hello)
self.assertEqual(buf.data, load("tls_client_hello.bin"))
def test_pull_server_hello(self):
buf = Buffer(data=load("tls_server_hello.bin"))
hello = pull_server_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"ada85271d19680c615ea7336519e3fdf6f1e26f3b1075ee1de96ffa8884e8280"
),
)
self.assertEqual(
hello.legacy_session_id,
binascii.unhexlify(
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235"
),
)
self.assertEqual(hello.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384)
self.assertEqual(hello.compression_method, tls.CompressionMethod.NULL)
self.assertEqual(
hello.key_share,
(
tls.Group.SECP256R1,
binascii.unhexlify(
"048b27d0282242d84b7fcc02a9c4f13eca0329e3c7029aa34a33794e6e7ba189"
"5cca1c503bf0378ac6937c354912116ff3251026bca1958d7f387316c83ae6cf"
"b2"
),
),
)
self.assertEqual(hello.pre_shared_key, None)
self.assertEqual(hello.supported_version, tls.TLS_VERSION_1_3)
def test_pull_server_hello_with_psk(self):
buf = Buffer(data=load("tls_server_hello_with_psk.bin"))
hello = pull_server_hello(buf)
self.assertTrue(buf.eof())
self.assertEqual(
hello.random,
binascii.unhexlify(
"ccbaaf04fc1bd5143b2cc6b97520cf37d91470dbfc8127131a7bf0f941e3a137"
),
)