| 1 |
import binascii |
| 2 |
import datetime |
| 3 |
import ssl |
| 4 |
from functools import partial |
| 5 |
from unittest import TestCase, skipIf |
| 6 |
from unittest.mock import patch |
| 7 |
|
| 8 |
from aioquic import tls |
| 9 |
from aioquic.buffer import Buffer, BufferReadError |
| 10 |
from aioquic.quic.configuration import QuicConfiguration |
| 11 |
from aioquic.tls import ( |
| 12 |
Certificate, |
| 13 |
CertificateRequest, |
| 14 |
CertificateVerify, |
| 15 |
ClientHello, |
| 16 |
Context, |
| 17 |
EncryptedExtensions, |
| 18 |
Finished, |
| 19 |
NewSessionTicket, |
| 20 |
ServerHello, |
| 21 |
State, |
| 22 |
load_pem_x509_certificates, |
| 23 |
pull_block, |
| 24 |
pull_certificate, |
| 25 |
pull_certificate_request, |
| 26 |
pull_certificate_verify, |
| 27 |
pull_client_hello, |
| 28 |
pull_encrypted_extensions, |
| 29 |
pull_finished, |
| 30 |
pull_new_session_ticket, |
| 31 |
pull_server_hello, |
| 32 |
pull_server_name, |
| 33 |
push_certificate, |
| 34 |
push_certificate_request, |
| 35 |
push_certificate_verify, |
| 36 |
push_client_hello, |
| 37 |
push_encrypted_extensions, |
| 38 |
push_finished, |
| 39 |
push_new_session_ticket, |
| 40 |
push_server_hello, |
| 41 |
push_server_name, |
| 42 |
verify_certificate, |
| 43 |
) |
| 44 |
from cryptography.exceptions import UnsupportedAlgorithm |
| 45 |
from cryptography.hazmat.primitives import serialization |
| 46 |
from cryptography.hazmat.primitives.asymmetric import ec |
| 47 |
|
| 48 |
from .utils import ( |
| 49 |
SERVER_CACERTFILE, |
| 50 |
SERVER_CERTFILE, |
| 51 |
SERVER_KEYFILE, |
| 52 |
SKIP_TESTS, |
| 53 |
generate_ec_certificate, |
| 54 |
generate_ed448_certificate, |
| 55 |
generate_ed25519_certificate, |
| 56 |
generate_rsa_certificate, |
| 57 |
load, |
| 58 |
) |
| 59 |
|
| 60 |
CERTIFICATE_DATA = load("tls_certificate.bin")[11:-2] |
| 61 |
CERTIFICATE_VERIFY_SIGNATURE = load("tls_certificate_verify.bin")[-384:] |
| 62 |
|
| 63 |
CLIENT_QUIC_TRANSPORT_PARAMETERS = binascii.unhexlify( |
| 64 |
b"ff0000110031000500048010000000060004801000000007000480100000000" |
| 65 |
b"4000481000000000100024258000800024064000a00010a" |
| 66 |
) |
| 67 |
|
| 68 |
SERVER_QUIC_TRANSPORT_PARAMETERS = binascii.unhexlify( |
| 69 |
b"ff00001104ff000011004500050004801000000006000480100000000700048" |
| 70 |
b"010000000040004810000000001000242580002001000000000000000000000" |
| 71 |
b"000000000000000800024064000a00010a" |
| 72 |
) |
| 73 |
|
| 74 |
SERVER_QUIC_TRANSPORT_PARAMETERS_2 = binascii.unhexlify( |
| 75 |
b"0057000600048000ffff000500048000ffff00020010c5ac410fbdd4fe6e2c1" |
| 76 |
b"42279f231e8e0000a000103000400048005fffa000b000119000100026710ff" |
| 77 |
b"42000c5c067f27e39321c63e28e7c90003000247e40008000106" |
| 78 |
) |
| 79 |
|
| 80 |
SERVER_QUIC_TRANSPORT_PARAMETERS_3 = binascii.unhexlify( |
| 81 |
b"0054000200100dcb50a442513295b4679baf04cb5effff8a0009c8afe72a6397" |
| 82 |
b"255407000600048000ffff0008000106000400048005fffa000500048000ffff" |
| 83 |
b"0003000247e4000a000103000100026710000b000119" |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
class BufferTest(TestCase): |
| 88 |
def test_pull_block_truncated(self): |
| 89 |
buf = Buffer(capacity=0) |
| 90 |
with self.assertRaises(BufferReadError): |
| 91 |
with pull_block(buf, 1): |
| 92 |
pass |
| 93 |
|
| 94 |
|
| 95 |
def corrupt_hello_version(data: bytes) -> bytes: |
| 96 |
""" |
| 97 |
Corrupt a ClientHello or ServerHello's protocol version. |
| 98 |
""" |
| 99 |
return data[:4] + b"\xff\xff" + data[6:] |
| 100 |
|
| 101 |
|
| 102 |
def create_buffers(): |
| 103 |
return { |
| 104 |
tls.Epoch.INITIAL: Buffer(capacity=4096), |
| 105 |
tls.Epoch.HANDSHAKE: Buffer(capacity=4096), |
| 106 |
tls.Epoch.ONE_RTT: Buffer(capacity=4096), |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
def merge_buffers(buffers): |
| 111 |
return b"".join(x.data for x in buffers.values()) |
| 112 |
|
| 113 |
|
| 114 |
def reset_buffers(buffers): |
| 115 |
for k in buffers.keys(): |
| 116 |
buffers[k].seek(0) |
| 117 |
|
| 118 |
|
| 119 |
class ContextTest(TestCase): |
| 120 |
def assertClientHello(self, data: bytes): |
| 121 |
self.assertEqual(data[0], tls.HandshakeType.CLIENT_HELLO) |
| 122 |
self.assertGreaterEqual(len(data), 189) |
| 123 |
self.assertLessEqual(len(data), 564) |
| 124 |
|
| 125 |
def create_client( |
| 126 |
self, alpn_protocols=None, cadata=None, cafile=SERVER_CACERTFILE, **kwargs |
| 127 |
): |
| 128 |
client = Context( |
| 129 |
alpn_protocols=alpn_protocols, |
| 130 |
cadata=cadata, |
| 131 |
cafile=cafile, |
| 132 |
is_client=True, |
| 133 |
**kwargs, |
| 134 |
) |
| 135 |
client.handshake_extensions = [ |
| 136 |
( |
| 137 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS, |
| 138 |
CLIENT_QUIC_TRANSPORT_PARAMETERS, |
| 139 |
) |
| 140 |
] |
| 141 |
self.assertEqual(client.state, State.CLIENT_HANDSHAKE_START) |
| 142 |
return client |
| 143 |
|
| 144 |
def create_server(self, alpn_protocols=None, **kwargs): |
| 145 |
configuration = QuicConfiguration(is_client=False) |
| 146 |
configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE) |
| 147 |
|
| 148 |
server = Context( |
| 149 |
alpn_protocols=alpn_protocols, |
| 150 |
is_client=False, |
| 151 |
max_early_data=0xFFFFFFFF, |
| 152 |
**kwargs, |
| 153 |
) |
| 154 |
server.certificate = configuration.certificate |
| 155 |
server.certificate_private_key = configuration.private_key |
| 156 |
server.handshake_extensions = [ |
| 157 |
( |
| 158 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS, |
| 159 |
SERVER_QUIC_TRANSPORT_PARAMETERS, |
| 160 |
) |
| 161 |
] |
| 162 |
self.assertEqual(server.state, State.SERVER_EXPECT_CLIENT_HELLO) |
| 163 |
return server |
| 164 |
|
| 165 |
def handshake_with_client_input_corruption( |
| 166 |
self, |
| 167 |
corrupt_client_input, |
| 168 |
expected_exception, |
| 169 |
): |
| 170 |
client = self.create_client() |
| 171 |
server = self.create_server() |
| 172 |
|
| 173 |
|
| 174 |
client_buf = create_buffers() |
| 175 |
client.handle_message(b"", client_buf) |
| 176 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 177 |
server_input = merge_buffers(client_buf) |
| 178 |
reset_buffers(client_buf) |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
server_buf = create_buffers() |
| 185 |
server.handle_message(server_input, server_buf) |
| 186 |
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED) |
| 187 |
client_input = merge_buffers(server_buf) |
| 188 |
reset_buffers(server_buf) |
| 189 |
|
| 190 |
|
| 191 |
client_input = corrupt_client_input(client_input) |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
with self.assertRaises(expected_exception.__class__) as cm: |
| 196 |
client.handle_message(client_input, client_buf) |
| 197 |
self.assertEqual(str(cm.exception), str(expected_exception)) |
| 198 |
|
| 199 |
def test_client_unexpected_message(self): |
| 200 |
client = self.create_client() |
| 201 |
|
| 202 |
client.state = State.CLIENT_EXPECT_SERVER_HELLO |
| 203 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 204 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 205 |
|
| 206 |
client.state = State.CLIENT_EXPECT_ENCRYPTED_EXTENSIONS |
| 207 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 208 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 209 |
|
| 210 |
client.state = State.CLIENT_EXPECT_CERTIFICATE_REQUEST_OR_CERTIFICATE |
| 211 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 212 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 213 |
|
| 214 |
client.state = State.CLIENT_EXPECT_CERTIFICATE |
| 215 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 216 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 217 |
|
| 218 |
client.state = State.CLIENT_EXPECT_CERTIFICATE_VERIFY |
| 219 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 220 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 221 |
|
| 222 |
client.state = State.CLIENT_EXPECT_FINISHED |
| 223 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 224 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 225 |
|
| 226 |
client.state = State.CLIENT_POST_HANDSHAKE |
| 227 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 228 |
client.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 229 |
|
| 230 |
def test_client_bad_hello_buffer_read_error(self): |
| 231 |
buf = Buffer(capacity=100) |
| 232 |
buf.push_uint8(tls.HandshakeType.SERVER_HELLO) |
| 233 |
with tls.push_block(buf, 3): |
| 234 |
pass |
| 235 |
|
| 236 |
self.handshake_with_client_input_corruption( |
| 237 |
|
| 238 |
lambda x: buf.data, |
| 239 |
tls.AlertDecodeError("Could not parse TLS message"), |
| 240 |
) |
| 241 |
|
| 242 |
def test_client_bad_hello_compression_method(self): |
| 243 |
self.handshake_with_client_input_corruption( |
| 244 |
|
| 245 |
lambda x: x[:41] + b"\xff" + x[42:], |
| 246 |
tls.AlertIllegalParameter( |
| 247 |
"ServerHello has a compression method we did not advertise" |
| 248 |
), |
| 249 |
) |
| 250 |
|
| 251 |
def test_client_bad_hello_version(self): |
| 252 |
self.handshake_with_client_input_corruption( |
| 253 |
|
| 254 |
lambda x: x[:48] + b"\xff\xff" + x[50:], |
| 255 |
tls.AlertIllegalParameter("ServerHello has a version we did not advertise"), |
| 256 |
) |
| 257 |
|
| 258 |
def test_client_bad_certificate_verify_algorithm(self): |
| 259 |
self.handshake_with_client_input_corruption( |
| 260 |
|
| 261 |
lambda x: x[:-440] + b"\xff\xff" + x[-438:], |
| 262 |
tls.AlertDecryptError( |
| 263 |
"CertificateVerify has a signature algorithm we did not advertise" |
| 264 |
), |
| 265 |
) |
| 266 |
|
| 267 |
def test_client_bad_certificate_verify_data(self): |
| 268 |
self.handshake_with_client_input_corruption( |
| 269 |
|
| 270 |
lambda x: x[:-56] + bytes(4) + x[-52:], |
| 271 |
tls.AlertDecryptError(), |
| 272 |
) |
| 273 |
|
| 274 |
def test_client_bad_finished_verify_data(self): |
| 275 |
self.handshake_with_client_input_corruption( |
| 276 |
|
| 277 |
lambda x: x[:-4] + bytes(4), |
| 278 |
tls.AlertDecryptError(), |
| 279 |
) |
| 280 |
|
| 281 |
def test_server_unexpected_message(self): |
| 282 |
server = self.create_server() |
| 283 |
|
| 284 |
server.state = State.SERVER_EXPECT_CLIENT_HELLO |
| 285 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 286 |
server.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 287 |
|
| 288 |
server.state = State.SERVER_EXPECT_CERTIFICATE |
| 289 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 290 |
server.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 291 |
|
| 292 |
server.state = State.SERVER_EXPECT_CERTIFICATE_VERIFY |
| 293 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 294 |
server.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 295 |
|
| 296 |
server.state = State.SERVER_EXPECT_FINISHED |
| 297 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 298 |
server.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 299 |
|
| 300 |
server.state = State.SERVER_POST_HANDSHAKE |
| 301 |
with self.assertRaises(tls.AlertUnexpectedMessage): |
| 302 |
server.handle_message(b"\x00\x00\x00\x00", create_buffers()) |
| 303 |
|
| 304 |
def _server_fail_hello(self, client, server): |
| 305 |
|
| 306 |
client_buf = create_buffers() |
| 307 |
client.handle_message(b"", client_buf) |
| 308 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 309 |
server_input = merge_buffers(client_buf) |
| 310 |
reset_buffers(client_buf) |
| 311 |
|
| 312 |
|
| 313 |
server_buf = create_buffers() |
| 314 |
server.handle_message(server_input, server_buf) |
| 315 |
|
| 316 |
def test_server_unsupported_cipher_suite(self): |
| 317 |
client = self.create_client(cipher_suites=[tls.CipherSuite.AES_128_GCM_SHA256]) |
| 318 |
|
| 319 |
server = self.create_server(cipher_suites=[tls.CipherSuite.AES_256_GCM_SHA384]) |
| 320 |
|
| 321 |
with self.assertRaises(tls.AlertHandshakeFailure) as cm: |
| 322 |
self._server_fail_hello(client, server) |
| 323 |
self.assertEqual(str(cm.exception), "No supported cipher suite") |
| 324 |
|
| 325 |
def test_server_unsupported_signature_algorithm(self): |
| 326 |
client = self.create_client() |
| 327 |
client._signature_algorithms = [tls.SignatureAlgorithm.ED448] |
| 328 |
|
| 329 |
server = self.create_server() |
| 330 |
|
| 331 |
with self.assertRaises(tls.AlertHandshakeFailure) as cm: |
| 332 |
self._server_fail_hello(client, server) |
| 333 |
self.assertEqual(str(cm.exception), "No supported signature algorithm") |
| 334 |
|
| 335 |
def test_server_unsupported_version(self): |
| 336 |
client = self.create_client() |
| 337 |
client._supported_versions = [tls.TLS_VERSION_1_2] |
| 338 |
|
| 339 |
server = self.create_server() |
| 340 |
|
| 341 |
with self.assertRaises(tls.AlertProtocolVersion) as cm: |
| 342 |
self._server_fail_hello(client, server) |
| 343 |
self.assertEqual(str(cm.exception), "No supported protocol version") |
| 344 |
|
| 345 |
def test_server_bad_finished_verify_data(self): |
| 346 |
client = self.create_client() |
| 347 |
server = self.create_server() |
| 348 |
|
| 349 |
|
| 350 |
client_buf = create_buffers() |
| 351 |
client.handle_message(b"", client_buf) |
| 352 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 353 |
server_input = merge_buffers(client_buf) |
| 354 |
reset_buffers(client_buf) |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
server_buf = create_buffers() |
| 361 |
server.handle_message(server_input, server_buf) |
| 362 |
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED) |
| 363 |
client_input = merge_buffers(server_buf) |
| 364 |
reset_buffers(server_buf) |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
client.handle_message(client_input, client_buf) |
| 371 |
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE) |
| 372 |
server_input = merge_buffers(client_buf) |
| 373 |
reset_buffers(client_buf) |
| 374 |
|
| 375 |
|
| 376 |
server_input = server_input[:-4] + bytes(4) |
| 377 |
|
| 378 |
|
| 379 |
with self.assertRaises(tls.AlertDecryptError): |
| 380 |
server.handle_message(server_input, server_buf) |
| 381 |
|
| 382 |
def _handshake(self, client, server): |
| 383 |
|
| 384 |
client_buf = create_buffers() |
| 385 |
client.handle_message(b"", client_buf) |
| 386 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 387 |
server_input = merge_buffers(client_buf) |
| 388 |
self.assertClientHello(server_input) |
| 389 |
reset_buffers(client_buf) |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
server_buf = create_buffers() |
| 396 |
server.handle_message(server_input, server_buf) |
| 397 |
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED) |
| 398 |
client_input = merge_buffers(server_buf) |
| 399 |
self.assertGreaterEqual(len(client_input), 587) |
| 400 |
self.assertLessEqual(len(client_input), 2316) |
| 401 |
|
| 402 |
reset_buffers(server_buf) |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
client.handle_message(client_input, client_buf) |
| 409 |
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE) |
| 410 |
server_input = merge_buffers(client_buf) |
| 411 |
self.assertEqual(len(server_input), 52) |
| 412 |
reset_buffers(client_buf) |
| 413 |
|
| 414 |
|
| 415 |
server.handle_message(server_input, server_buf) |
| 416 |
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE) |
| 417 |
client_input = merge_buffers(server_buf) |
| 418 |
self.assertEqual(len(client_input), 0) |
| 419 |
|
| 420 |
|
| 421 |
self.assertEqual(client._dec_key, server._enc_key) |
| 422 |
self.assertEqual(client._enc_key, server._dec_key) |
| 423 |
|
| 424 |
|
| 425 |
self.assertEqual( |
| 426 |
client.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 427 |
) |
| 428 |
self.assertEqual( |
| 429 |
server.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 430 |
) |
| 431 |
|
| 432 |
def test_handshake(self): |
| 433 |
client = self.create_client() |
| 434 |
server = self.create_server() |
| 435 |
|
| 436 |
self._handshake(client, server) |
| 437 |
|
| 438 |
|
| 439 |
self.assertEqual(client.alpn_negotiated, None) |
| 440 |
self.assertEqual(server.alpn_negotiated, None) |
| 441 |
|
| 442 |
def test_handshake_with_certificate_request_no_certificate(self): |
| 443 |
|
| 444 |
client = self.create_client() |
| 445 |
server = self.create_server() |
| 446 |
server._request_client_certificate = True |
| 447 |
|
| 448 |
|
| 449 |
client_buf = create_buffers() |
| 450 |
client.handle_message(b"", client_buf) |
| 451 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 452 |
server_input = merge_buffers(client_buf) |
| 453 |
self.assertClientHello(server_input) |
| 454 |
reset_buffers(client_buf) |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
server_buf = create_buffers() |
| 461 |
server.handle_message(server_input, server_buf) |
| 462 |
self.assertEqual(server.state, State.SERVER_EXPECT_CERTIFICATE) |
| 463 |
client_input = merge_buffers(server_buf) |
| 464 |
self.assertGreaterEqual(len(client_input), 587) |
| 465 |
self.assertLessEqual(len(client_input), 2316) |
| 466 |
|
| 467 |
reset_buffers(server_buf) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
client.handle_message(client_input, client_buf) |
| 474 |
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE) |
| 475 |
server_input = merge_buffers(client_buf) |
| 476 |
self.assertEqual(len(server_input), 60) |
| 477 |
reset_buffers(client_buf) |
| 478 |
|
| 479 |
|
| 480 |
server.handle_message(server_input, server_buf) |
| 481 |
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE) |
| 482 |
client_input = merge_buffers(server_buf) |
| 483 |
self.assertEqual(len(client_input), 0) |
| 484 |
|
| 485 |
|
| 486 |
self.assertEqual(client._dec_key, server._enc_key) |
| 487 |
self.assertEqual(client._enc_key, server._dec_key) |
| 488 |
|
| 489 |
|
| 490 |
self.assertEqual( |
| 491 |
client.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 492 |
) |
| 493 |
self.assertEqual( |
| 494 |
server.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 495 |
) |
| 496 |
|
| 497 |
def test_handshake_with_certificate_request_with_certificate(self): |
| 498 |
|
| 499 |
client = self.create_client() |
| 500 |
client.certificate, client.certificate_private_key = generate_rsa_certificate( |
| 501 |
common_name="client.example.com" |
| 502 |
) |
| 503 |
server = self.create_server() |
| 504 |
server._request_client_certificate = True |
| 505 |
|
| 506 |
|
| 507 |
client_buf = create_buffers() |
| 508 |
client.handle_message(b"", client_buf) |
| 509 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 510 |
server_input = merge_buffers(client_buf) |
| 511 |
self.assertClientHello(server_input) |
| 512 |
reset_buffers(client_buf) |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
server_buf = create_buffers() |
| 519 |
server.handle_message(server_input, server_buf) |
| 520 |
self.assertEqual(server.state, State.SERVER_EXPECT_CERTIFICATE) |
| 521 |
client_input = merge_buffers(server_buf) |
| 522 |
self.assertGreaterEqual(len(client_input), 587) |
| 523 |
self.assertLessEqual(len(client_input), 2316) |
| 524 |
|
| 525 |
reset_buffers(server_buf) |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
client.handle_message(client_input, client_buf) |
| 532 |
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE) |
| 533 |
server_input = merge_buffers(client_buf) |
| 534 |
self.assertGreaterEqual(len(server_input), 1042) |
| 535 |
self.assertLessEqual(len(server_input), 1043) |
| 536 |
reset_buffers(client_buf) |
| 537 |
|
| 538 |
|
| 539 |
server.handle_message(server_input, server_buf) |
| 540 |
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE) |
| 541 |
client_input = merge_buffers(server_buf) |
| 542 |
self.assertEqual(len(client_input), 0) |
| 543 |
|
| 544 |
|
| 545 |
self.assertEqual(client._dec_key, server._enc_key) |
| 546 |
self.assertEqual(client._enc_key, server._dec_key) |
| 547 |
|
| 548 |
|
| 549 |
self.assertEqual( |
| 550 |
client.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 551 |
) |
| 552 |
self.assertEqual( |
| 553 |
server.key_schedule.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384 |
| 554 |
) |
| 555 |
|
| 556 |
def _test_handshake_with_certificate(self, certificate, private_key): |
| 557 |
server = self.create_server() |
| 558 |
server.certificate = certificate |
| 559 |
server.certificate_private_key = private_key |
| 560 |
|
| 561 |
client = self.create_client( |
| 562 |
cadata=server.certificate.public_bytes(serialization.Encoding.PEM), |
| 563 |
cafile=None, |
| 564 |
) |
| 565 |
|
| 566 |
self._handshake(client, server) |
| 567 |
|
| 568 |
|
| 569 |
self.assertEqual(client.alpn_negotiated, None) |
| 570 |
self.assertEqual(server.alpn_negotiated, None) |
| 571 |
|
| 572 |
def test_handshake_with_ec_certificate_secp256r1(self): |
| 573 |
self._test_handshake_with_certificate( |
| 574 |
*generate_ec_certificate(common_name="example.com", curve=ec.SECP256R1) |
| 575 |
) |
| 576 |
|
| 577 |
def test_handshake_with_ec_certificate_secp384r1(self): |
| 578 |
self._test_handshake_with_certificate( |
| 579 |
*generate_ec_certificate(common_name="example.com", curve=ec.SECP384R1) |
| 580 |
) |
| 581 |
|
| 582 |
def test_handshake_with_ed25519_certificate(self): |
| 583 |
self._test_handshake_with_certificate( |
| 584 |
*generate_ed25519_certificate(common_name="example.com") |
| 585 |
) |
| 586 |
|
| 587 |
@skipIf("ed448" in SKIP_TESTS, "Skipping ed448 tests") |
| 588 |
def test_handshake_with_ed448_certificate(self): |
| 589 |
self._test_handshake_with_certificate( |
| 590 |
*generate_ed448_certificate(common_name="example.com") |
| 591 |
) |
| 592 |
|
| 593 |
def test_handshake_with_alpn(self): |
| 594 |
client = self.create_client(alpn_protocols=["hq-interop"]) |
| 595 |
server = self.create_server(alpn_protocols=["hq-interop", "h3"]) |
| 596 |
|
| 597 |
self._handshake(client, server) |
| 598 |
|
| 599 |
|
| 600 |
self.assertEqual(client.alpn_negotiated, "hq-interop") |
| 601 |
self.assertEqual(server.alpn_negotiated, "hq-interop") |
| 602 |
|
| 603 |
def test_handshake_with_alpn_fail(self): |
| 604 |
client = self.create_client(alpn_protocols=["hq-interop"]) |
| 605 |
server = self.create_server(alpn_protocols=["h3"]) |
| 606 |
|
| 607 |
with self.assertRaises(tls.AlertHandshakeFailure) as cm: |
| 608 |
self._handshake(client, server) |
| 609 |
self.assertEqual(str(cm.exception), "No common ALPN protocols") |
| 610 |
|
| 611 |
def test_handshake_with_rsa_pkcs1_sha1_signature(self): |
| 612 |
client = self.create_client() |
| 613 |
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PKCS1_SHA1] |
| 614 |
server = self.create_server() |
| 615 |
|
| 616 |
self._handshake(client, server) |
| 617 |
|
| 618 |
def test_handshake_with_rsa_pkcs1_sha256_signature(self): |
| 619 |
client = self.create_client() |
| 620 |
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PKCS1_SHA256] |
| 621 |
server = self.create_server() |
| 622 |
|
| 623 |
self._handshake(client, server) |
| 624 |
|
| 625 |
def test_handshake_with_rsa_pkcs1_sha384_signature(self): |
| 626 |
client = self.create_client() |
| 627 |
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PKCS1_SHA384] |
| 628 |
server = self.create_server() |
| 629 |
|
| 630 |
self._handshake(client, server) |
| 631 |
|
| 632 |
def test_handshake_with_rsa_pss_rsae_sha256_signature(self): |
| 633 |
client = self.create_client() |
| 634 |
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256] |
| 635 |
server = self.create_server() |
| 636 |
|
| 637 |
self._handshake(client, server) |
| 638 |
|
| 639 |
def test_handshake_with_rsa_pss_rsae_sha384_signature(self): |
| 640 |
client = self.create_client() |
| 641 |
client._signature_algorithms = [tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA384] |
| 642 |
server = self.create_server() |
| 643 |
|
| 644 |
self._handshake(client, server) |
| 645 |
|
| 646 |
def test_handshake_with_certificate_error(self): |
| 647 |
client = self.create_client(cafile=None) |
| 648 |
server = self.create_server() |
| 649 |
|
| 650 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 651 |
self._handshake(client, server) |
| 652 |
self.assertEqual(str(cm.exception), "unable to get local issuer certificate") |
| 653 |
|
| 654 |
def test_handshake_with_certificate_no_verify(self): |
| 655 |
client = self.create_client(cafile=None, verify_mode=ssl.CERT_NONE) |
| 656 |
server = self.create_server() |
| 657 |
|
| 658 |
self._handshake(client, server) |
| 659 |
|
| 660 |
def test_handshake_with_grease_group(self): |
| 661 |
client = self.create_client() |
| 662 |
client._supported_groups = [tls.Group.GREASE, tls.Group.SECP256R1] |
| 663 |
server = self.create_server() |
| 664 |
|
| 665 |
self._handshake(client, server) |
| 666 |
|
| 667 |
def test_handshake_with_secp256r1_group(self): |
| 668 |
client = self.create_client() |
| 669 |
client._supported_groups = [tls.Group.SECP256R1] |
| 670 |
server = self.create_server() |
| 671 |
|
| 672 |
self._handshake(client, server) |
| 673 |
|
| 674 |
def test_handshake_with_secp384r1_group(self): |
| 675 |
client = self.create_client() |
| 676 |
client._supported_groups = [tls.Group.SECP384R1] |
| 677 |
server = self.create_server() |
| 678 |
|
| 679 |
self._handshake(client, server) |
| 680 |
|
| 681 |
def test_handshake_with_x25519(self): |
| 682 |
client = self.create_client() |
| 683 |
client._supported_groups = [tls.Group.X25519] |
| 684 |
server = self.create_server() |
| 685 |
|
| 686 |
try: |
| 687 |
self._handshake(client, server) |
| 688 |
except UnsupportedAlgorithm as exc: |
| 689 |
self.skipTest(str(exc)) |
| 690 |
|
| 691 |
def test_handshake_with_x448(self): |
| 692 |
client = self.create_client() |
| 693 |
client._supported_groups = [tls.Group.X448] |
| 694 |
server = self.create_server() |
| 695 |
|
| 696 |
try: |
| 697 |
self._handshake(client, server) |
| 698 |
except UnsupportedAlgorithm as exc: |
| 699 |
self.skipTest(str(exc)) |
| 700 |
|
| 701 |
def test_session_ticket(self): |
| 702 |
client_tickets = [] |
| 703 |
server_tickets = [] |
| 704 |
|
| 705 |
def client_new_ticket(ticket): |
| 706 |
client_tickets.append(ticket) |
| 707 |
|
| 708 |
def server_get_ticket(label): |
| 709 |
for t in server_tickets: |
| 710 |
if t.ticket == label: |
| 711 |
return t |
| 712 |
return None |
| 713 |
|
| 714 |
def server_new_ticket(ticket): |
| 715 |
server_tickets.append(ticket) |
| 716 |
|
| 717 |
def first_handshake(): |
| 718 |
client = self.create_client() |
| 719 |
client.new_session_ticket_cb = client_new_ticket |
| 720 |
|
| 721 |
server = self.create_server() |
| 722 |
server.new_session_ticket_cb = server_new_ticket |
| 723 |
|
| 724 |
self._handshake(client, server) |
| 725 |
|
| 726 |
|
| 727 |
self.assertFalse(client.session_resumed) |
| 728 |
self.assertFalse(server.session_resumed) |
| 729 |
|
| 730 |
|
| 731 |
self.assertEqual(len(client_tickets), 1) |
| 732 |
self.assertEqual(len(server_tickets), 1) |
| 733 |
self.assertEqual(client_tickets[0].ticket, server_tickets[0].ticket) |
| 734 |
self.assertEqual( |
| 735 |
client_tickets[0].resumption_secret, server_tickets[0].resumption_secret |
| 736 |
) |
| 737 |
|
| 738 |
def second_handshake(): |
| 739 |
client = self.create_client() |
| 740 |
client.session_ticket = client_tickets[0] |
| 741 |
|
| 742 |
server = self.create_server() |
| 743 |
server.get_session_ticket_cb = server_get_ticket |
| 744 |
|
| 745 |
|
| 746 |
client_buf = create_buffers() |
| 747 |
client.handle_message(b"", client_buf) |
| 748 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 749 |
server_input = merge_buffers(client_buf) |
| 750 |
self.assertClientHello(server_input) |
| 751 |
reset_buffers(client_buf) |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
server_buf = create_buffers() |
| 757 |
server.handle_message(server_input, server_buf) |
| 758 |
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED) |
| 759 |
client_input = merge_buffers(server_buf) |
| 760 |
self.assertEqual(len(client_input), 275) |
| 761 |
reset_buffers(server_buf) |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
client.handle_message(client_input, client_buf) |
| 768 |
self.assertEqual(client.state, State.CLIENT_POST_HANDSHAKE) |
| 769 |
server_input = merge_buffers(client_buf) |
| 770 |
self.assertEqual(len(server_input), 52) |
| 771 |
reset_buffers(client_buf) |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
server.handle_message(server_input, server_buf) |
| 777 |
self.assertEqual(server.state, State.SERVER_POST_HANDSHAKE) |
| 778 |
client_input = merge_buffers(server_buf) |
| 779 |
self.assertEqual(len(client_input), 0) |
| 780 |
reset_buffers(server_buf) |
| 781 |
|
| 782 |
|
| 783 |
self.assertEqual(client._dec_key, server._enc_key) |
| 784 |
self.assertEqual(client._enc_key, server._dec_key) |
| 785 |
|
| 786 |
|
| 787 |
self.assertTrue(client.session_resumed) |
| 788 |
self.assertTrue(server.session_resumed) |
| 789 |
|
| 790 |
def second_handshake_bad_binder(): |
| 791 |
client = self.create_client() |
| 792 |
client.session_ticket = client_tickets[0] |
| 793 |
|
| 794 |
server = self.create_server() |
| 795 |
server.get_session_ticket_cb = server_get_ticket |
| 796 |
|
| 797 |
|
| 798 |
client_buf = create_buffers() |
| 799 |
client.handle_message(b"", client_buf) |
| 800 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 801 |
server_input = merge_buffers(client_buf) |
| 802 |
self.assertClientHello(server_input) |
| 803 |
reset_buffers(client_buf) |
| 804 |
|
| 805 |
|
| 806 |
server_input = server_input[:-4] + bytes(4) |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
server_buf = create_buffers() |
| 811 |
with self.assertRaises(tls.AlertHandshakeFailure) as cm: |
| 812 |
server.handle_message(server_input, server_buf) |
| 813 |
self.assertEqual(str(cm.exception), "PSK validation failed") |
| 814 |
|
| 815 |
def second_handshake_bad_pre_shared_key(): |
| 816 |
client = self.create_client() |
| 817 |
client.session_ticket = client_tickets[0] |
| 818 |
|
| 819 |
server = self.create_server() |
| 820 |
server.get_session_ticket_cb = server_get_ticket |
| 821 |
|
| 822 |
|
| 823 |
client_buf = create_buffers() |
| 824 |
client.handle_message(b"", client_buf) |
| 825 |
self.assertEqual(client.state, State.CLIENT_EXPECT_SERVER_HELLO) |
| 826 |
server_input = merge_buffers(client_buf) |
| 827 |
self.assertClientHello(server_input) |
| 828 |
reset_buffers(client_buf) |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
server_buf = create_buffers() |
| 833 |
server.handle_message(server_input, server_buf) |
| 834 |
self.assertEqual(server.state, State.SERVER_EXPECT_FINISHED) |
| 835 |
|
| 836 |
|
| 837 |
buf = server_buf[tls.Epoch.INITIAL] |
| 838 |
buf.seek(buf.tell() - 1) |
| 839 |
buf.push_uint8(1) |
| 840 |
client_input = merge_buffers(server_buf) |
| 841 |
self.assertEqual(len(client_input), 275) |
| 842 |
reset_buffers(server_buf) |
| 843 |
|
| 844 |
|
| 845 |
with self.assertRaises(tls.AlertIllegalParameter): |
| 846 |
client.handle_message(client_input, client_buf) |
| 847 |
|
| 848 |
first_handshake() |
| 849 |
second_handshake() |
| 850 |
second_handshake_bad_binder() |
| 851 |
second_handshake_bad_pre_shared_key() |
| 852 |
|
| 853 |
|
| 854 |
class TlsTest(TestCase): |
| 855 |
def test_pull_block_incomplete_read(self): |
| 856 |
""" |
| 857 |
If a block is not read until its end, an alert should be raised. |
| 858 |
""" |
| 859 |
buf = Buffer(data=bytes([2, 0, 0])) |
| 860 |
with self.assertRaises(tls.AlertDecodeError) as cm: |
| 861 |
with pull_block(buf, 1): |
| 862 |
buf.pull_bytes(1) |
| 863 |
self.assertEqual(str(cm.exception), "extra bytes at the end of a block") |
| 864 |
|
| 865 |
def test_pull_client_hello(self): |
| 866 |
buf = Buffer(data=load("tls_client_hello.bin")) |
| 867 |
hello = pull_client_hello(buf) |
| 868 |
self.assertTrue(buf.eof()) |
| 869 |
|
| 870 |
self.assertEqual( |
| 871 |
hello.random, |
| 872 |
binascii.unhexlify( |
| 873 |
"18b2b23bf3e44b5d52ccfe7aecbc5ff14eadc3d349fabf804d71f165ae76e7d5" |
| 874 |
), |
| 875 |
) |
| 876 |
self.assertEqual( |
| 877 |
hello.legacy_session_id, |
| 878 |
binascii.unhexlify( |
| 879 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 880 |
), |
| 881 |
) |
| 882 |
self.assertEqual( |
| 883 |
hello.cipher_suites, |
| 884 |
[ |
| 885 |
tls.CipherSuite.AES_256_GCM_SHA384, |
| 886 |
tls.CipherSuite.AES_128_GCM_SHA256, |
| 887 |
tls.CipherSuite.CHACHA20_POLY1305_SHA256, |
| 888 |
], |
| 889 |
) |
| 890 |
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL]) |
| 891 |
|
| 892 |
|
| 893 |
self.assertEqual(hello.alpn_protocols, None) |
| 894 |
self.assertEqual( |
| 895 |
hello.key_share, |
| 896 |
[ |
| 897 |
( |
| 898 |
tls.Group.SECP256R1, |
| 899 |
binascii.unhexlify( |
| 900 |
"047bfea344467535054263b75def60cffa82405a211b68d1eb8d1d944e67aef8" |
| 901 |
"93c7665a5473d032cfaf22a73da28eb4aacae0017ed12557b5791f98a1e84f15" |
| 902 |
"b0" |
| 903 |
), |
| 904 |
) |
| 905 |
], |
| 906 |
) |
| 907 |
self.assertEqual( |
| 908 |
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE] |
| 909 |
) |
| 910 |
self.assertEqual(hello.server_name, None) |
| 911 |
self.assertEqual( |
| 912 |
hello.signature_algorithms, |
| 913 |
[ |
| 914 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 915 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 916 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 917 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA1, |
| 918 |
], |
| 919 |
) |
| 920 |
self.assertEqual(hello.supported_groups, [tls.Group.SECP256R1]) |
| 921 |
self.assertEqual( |
| 922 |
hello.supported_versions, |
| 923 |
[ |
| 924 |
tls.TLS_VERSION_1_3, |
| 925 |
tls.TLS_VERSION_1_3_DRAFT_28, |
| 926 |
tls.TLS_VERSION_1_3_DRAFT_27, |
| 927 |
tls.TLS_VERSION_1_3_DRAFT_26, |
| 928 |
], |
| 929 |
) |
| 930 |
|
| 931 |
self.assertEqual( |
| 932 |
hello.other_extensions, |
| 933 |
[ |
| 934 |
( |
| 935 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 936 |
CLIENT_QUIC_TRANSPORT_PARAMETERS, |
| 937 |
) |
| 938 |
], |
| 939 |
) |
| 940 |
|
| 941 |
def test_pull_client_hello_with_alpn(self): |
| 942 |
buf = Buffer(data=load("tls_client_hello_with_alpn.bin")) |
| 943 |
hello = pull_client_hello(buf) |
| 944 |
self.assertTrue(buf.eof()) |
| 945 |
|
| 946 |
self.assertEqual( |
| 947 |
hello.random, |
| 948 |
binascii.unhexlify( |
| 949 |
"ed575c6fbd599c4dfaabd003dca6e860ccdb0e1782c1af02e57bf27cb6479b76" |
| 950 |
), |
| 951 |
) |
| 952 |
self.assertEqual(hello.legacy_session_id, b"") |
| 953 |
self.assertEqual( |
| 954 |
hello.cipher_suites, |
| 955 |
[ |
| 956 |
tls.CipherSuite.AES_128_GCM_SHA256, |
| 957 |
tls.CipherSuite.AES_256_GCM_SHA384, |
| 958 |
tls.CipherSuite.CHACHA20_POLY1305_SHA256, |
| 959 |
tls.CipherSuite.EMPTY_RENEGOTIATION_INFO_SCSV, |
| 960 |
], |
| 961 |
) |
| 962 |
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL]) |
| 963 |
|
| 964 |
|
| 965 |
self.assertEqual(hello.alpn_protocols, ["h3-19"]) |
| 966 |
self.assertEqual(hello.early_data, False) |
| 967 |
self.assertEqual( |
| 968 |
hello.key_share, |
| 969 |
[ |
| 970 |
( |
| 971 |
tls.Group.SECP256R1, |
| 972 |
binascii.unhexlify( |
| 973 |
"048842315c437bb0ce2929c816fee4e942ec5cb6db6a6b9bf622680188ebb0d4" |
| 974 |
"b652e69033f71686aa01cbc79155866e264c9f33f45aa16b0dfa10a222e3a669" |
| 975 |
"22" |
| 976 |
), |
| 977 |
) |
| 978 |
], |
| 979 |
) |
| 980 |
self.assertEqual( |
| 981 |
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE] |
| 982 |
) |
| 983 |
self.assertEqual(hello.server_name, "cloudflare-quic.com") |
| 984 |
self.assertEqual( |
| 985 |
hello.signature_algorithms, |
| 986 |
[ |
| 987 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 988 |
tls.SignatureAlgorithm.ECDSA_SECP384R1_SHA384, |
| 989 |
tls.SignatureAlgorithm.ECDSA_SECP521R1_SHA512, |
| 990 |
tls.SignatureAlgorithm.ED25519, |
| 991 |
tls.SignatureAlgorithm.ED448, |
| 992 |
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA256, |
| 993 |
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA384, |
| 994 |
tls.SignatureAlgorithm.RSA_PSS_PSS_SHA512, |
| 995 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 996 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA384, |
| 997 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA512, |
| 998 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 999 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA384, |
| 1000 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA512, |
| 1001 |
], |
| 1002 |
) |
| 1003 |
self.assertEqual( |
| 1004 |
hello.supported_groups, |
| 1005 |
[ |
| 1006 |
tls.Group.SECP256R1, |
| 1007 |
tls.Group.X25519, |
| 1008 |
tls.Group.SECP384R1, |
| 1009 |
tls.Group.SECP521R1, |
| 1010 |
], |
| 1011 |
) |
| 1012 |
self.assertEqual(hello.supported_versions, [tls.TLS_VERSION_1_3]) |
| 1013 |
|
| 1014 |
|
| 1015 |
buf = Buffer(1000) |
| 1016 |
push_client_hello(buf, hello) |
| 1017 |
self.assertEqual(len(buf.data), len(load("tls_client_hello_with_alpn.bin"))) |
| 1018 |
|
| 1019 |
def test_pull_client_hello_with_psk(self): |
| 1020 |
buf = Buffer(data=load("tls_client_hello_with_psk.bin")) |
| 1021 |
hello = pull_client_hello(buf) |
| 1022 |
|
| 1023 |
self.assertEqual(hello.early_data, True) |
| 1024 |
self.assertEqual( |
| 1025 |
hello.pre_shared_key, |
| 1026 |
tls.OfferedPsks( |
| 1027 |
identities=[ |
| 1028 |
( |
| 1029 |
binascii.unhexlify( |
| 1030 |
"fab3dc7d79f35ea53e9adf21150e601591a750b80cde0cd167fef6e0cdbc032a" |
| 1031 |
"c4161fc5c5b66679de49524bd5624c50d71ba3e650780a4bfe402d6a06a00525" |
| 1032 |
"0b5dc52085233b69d0dd13924cc5c713a396784ecafc59f5ea73c1585d79621b" |
| 1033 |
"8a94e4f2291b17427d5185abf4a994fca74ee7a7f993a950c71003fc7cf8" |
| 1034 |
), |
| 1035 |
2067156378, |
| 1036 |
) |
| 1037 |
], |
| 1038 |
binders=[ |
| 1039 |
binascii.unhexlify( |
| 1040 |
"1788ad43fdff37cfc628f24b6ce7c8c76180705380da17da32811b5bae4e78" |
| 1041 |
"d7aaaf65a9b713872f2bb28818ca1a6b01" |
| 1042 |
) |
| 1043 |
], |
| 1044 |
), |
| 1045 |
) |
| 1046 |
|
| 1047 |
self.assertTrue(buf.eof()) |
| 1048 |
|
| 1049 |
|
| 1050 |
buf = Buffer(1000) |
| 1051 |
push_client_hello(buf, hello) |
| 1052 |
self.assertEqual(buf.data, load("tls_client_hello_with_psk.bin")) |
| 1053 |
|
| 1054 |
def test_pull_client_hello_with_psk_and_other_extension(self): |
| 1055 |
buf = Buffer(capacity=1000) |
| 1056 |
|
| 1057 |
|
| 1058 |
psk_buf = Buffer(capacity=100) |
| 1059 |
tls.push_offered_psks( |
| 1060 |
psk_buf, |
| 1061 |
tls.OfferedPsks( |
| 1062 |
identities=[], |
| 1063 |
binders=[], |
| 1064 |
), |
| 1065 |
) |
| 1066 |
|
| 1067 |
|
| 1068 |
hello = ClientHello( |
| 1069 |
random=binascii.unhexlify( |
| 1070 |
"18b2b23bf3e44b5d52ccfe7aecbc5ff14eadc3d349fabf804d71f165ae76e7d5" |
| 1071 |
), |
| 1072 |
legacy_session_id=binascii.unhexlify( |
| 1073 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 1074 |
), |
| 1075 |
cipher_suites=[tls.CipherSuite.AES_256_GCM_SHA384], |
| 1076 |
legacy_compression_methods=[tls.CompressionMethod.NULL], |
| 1077 |
key_share=[ |
| 1078 |
( |
| 1079 |
tls.Group.SECP256R1, |
| 1080 |
binascii.unhexlify( |
| 1081 |
"047bfea344467535054263b75def60cffa82405a211b68d1eb8d1d944e67aef8" |
| 1082 |
"93c7665a5473d032cfaf22a73da28eb4aacae0017ed12557b5791f98a1e84f15" |
| 1083 |
"b0" |
| 1084 |
), |
| 1085 |
) |
| 1086 |
], |
| 1087 |
psk_key_exchange_modes=[tls.PskKeyExchangeMode.PSK_DHE_KE], |
| 1088 |
signature_algorithms=[tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256], |
| 1089 |
supported_groups=[tls.Group.SECP256R1], |
| 1090 |
supported_versions=[tls.TLS_VERSION_1_3], |
| 1091 |
other_extensions=[ |
| 1092 |
( |
| 1093 |
tls.ExtensionType.PRE_SHARED_KEY, |
| 1094 |
psk_buf.data, |
| 1095 |
), |
| 1096 |
( |
| 1097 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1098 |
CLIENT_QUIC_TRANSPORT_PARAMETERS, |
| 1099 |
), |
| 1100 |
], |
| 1101 |
) |
| 1102 |
push_client_hello(buf, hello) |
| 1103 |
|
| 1104 |
|
| 1105 |
buf.seek(0) |
| 1106 |
with self.assertRaises(tls.AlertIllegalParameter) as cm: |
| 1107 |
pull_client_hello(buf) |
| 1108 |
self.assertEqual(str(cm.exception), "PreSharedKey is not the last extension") |
| 1109 |
|
| 1110 |
def test_pull_client_hello_with_sni(self): |
| 1111 |
buf = Buffer(data=load("tls_client_hello_with_sni.bin")) |
| 1112 |
hello = pull_client_hello(buf) |
| 1113 |
self.assertTrue(buf.eof()) |
| 1114 |
|
| 1115 |
self.assertEqual( |
| 1116 |
hello.random, |
| 1117 |
binascii.unhexlify( |
| 1118 |
"987d8934140b0a42cc5545071f3f9f7f61963d7b6404eb674c8dbe513604346b" |
| 1119 |
), |
| 1120 |
) |
| 1121 |
self.assertEqual( |
| 1122 |
hello.legacy_session_id, |
| 1123 |
binascii.unhexlify( |
| 1124 |
"26b19bdd30dbf751015a3a16e13bd59002dfe420b799d2a5cd5e11b8fa7bcb66" |
| 1125 |
), |
| 1126 |
) |
| 1127 |
self.assertEqual( |
| 1128 |
hello.cipher_suites, |
| 1129 |
[ |
| 1130 |
tls.CipherSuite.AES_256_GCM_SHA384, |
| 1131 |
tls.CipherSuite.AES_128_GCM_SHA256, |
| 1132 |
tls.CipherSuite.CHACHA20_POLY1305_SHA256, |
| 1133 |
], |
| 1134 |
) |
| 1135 |
self.assertEqual(hello.legacy_compression_methods, [tls.CompressionMethod.NULL]) |
| 1136 |
|
| 1137 |
|
| 1138 |
self.assertEqual(hello.alpn_protocols, None) |
| 1139 |
self.assertEqual( |
| 1140 |
hello.key_share, |
| 1141 |
[ |
| 1142 |
( |
| 1143 |
tls.Group.SECP256R1, |
| 1144 |
binascii.unhexlify( |
| 1145 |
"04b62d70f907c814cd65d0f73b8b991f06b70c77153f548410a191d2b19764a2" |
| 1146 |
"ecc06065a480efa9e1f10c8da6e737d5bfc04be3f773e20a0c997f51b5621280" |
| 1147 |
"40" |
| 1148 |
), |
| 1149 |
) |
| 1150 |
], |
| 1151 |
) |
| 1152 |
self.assertEqual( |
| 1153 |
hello.psk_key_exchange_modes, [tls.PskKeyExchangeMode.PSK_DHE_KE] |
| 1154 |
) |
| 1155 |
self.assertEqual(hello.server_name, "cloudflare-quic.com") |
| 1156 |
self.assertEqual( |
| 1157 |
hello.signature_algorithms, |
| 1158 |
[ |
| 1159 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 1160 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 1161 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 1162 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA1, |
| 1163 |
], |
| 1164 |
) |
| 1165 |
self.assertEqual(hello.supported_groups, [tls.Group.SECP256R1]) |
| 1166 |
self.assertEqual( |
| 1167 |
hello.supported_versions, |
| 1168 |
[ |
| 1169 |
tls.TLS_VERSION_1_3, |
| 1170 |
tls.TLS_VERSION_1_3_DRAFT_28, |
| 1171 |
tls.TLS_VERSION_1_3_DRAFT_27, |
| 1172 |
tls.TLS_VERSION_1_3_DRAFT_26, |
| 1173 |
], |
| 1174 |
) |
| 1175 |
|
| 1176 |
self.assertEqual( |
| 1177 |
hello.other_extensions, |
| 1178 |
[ |
| 1179 |
( |
| 1180 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1181 |
CLIENT_QUIC_TRANSPORT_PARAMETERS, |
| 1182 |
) |
| 1183 |
], |
| 1184 |
) |
| 1185 |
|
| 1186 |
|
| 1187 |
buf = Buffer(1000) |
| 1188 |
push_client_hello(buf, hello) |
| 1189 |
self.assertEqual(buf.data, load("tls_client_hello_with_sni.bin")) |
| 1190 |
|
| 1191 |
def test_pull_client_hello_with_unexpected_version(self): |
| 1192 |
buf = Buffer(data=corrupt_hello_version(load("tls_client_hello.bin"))) |
| 1193 |
with self.assertRaises(tls.AlertDecodeError) as cm: |
| 1194 |
pull_client_hello(buf) |
| 1195 |
self.assertEqual(str(cm.exception), "ClientHello version is not 1.2") |
| 1196 |
|
| 1197 |
def test_push_client_hello(self): |
| 1198 |
hello = ClientHello( |
| 1199 |
random=binascii.unhexlify( |
| 1200 |
"18b2b23bf3e44b5d52ccfe7aecbc5ff14eadc3d349fabf804d71f165ae76e7d5" |
| 1201 |
), |
| 1202 |
legacy_session_id=binascii.unhexlify( |
| 1203 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 1204 |
), |
| 1205 |
cipher_suites=[ |
| 1206 |
tls.CipherSuite.AES_256_GCM_SHA384, |
| 1207 |
tls.CipherSuite.AES_128_GCM_SHA256, |
| 1208 |
tls.CipherSuite.CHACHA20_POLY1305_SHA256, |
| 1209 |
], |
| 1210 |
legacy_compression_methods=[tls.CompressionMethod.NULL], |
| 1211 |
key_share=[ |
| 1212 |
( |
| 1213 |
tls.Group.SECP256R1, |
| 1214 |
binascii.unhexlify( |
| 1215 |
"047bfea344467535054263b75def60cffa82405a211b68d1eb8d1d944e67aef8" |
| 1216 |
"93c7665a5473d032cfaf22a73da28eb4aacae0017ed12557b5791f98a1e84f15" |
| 1217 |
"b0" |
| 1218 |
), |
| 1219 |
) |
| 1220 |
], |
| 1221 |
psk_key_exchange_modes=[tls.PskKeyExchangeMode.PSK_DHE_KE], |
| 1222 |
signature_algorithms=[ |
| 1223 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 1224 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 1225 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 1226 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA1, |
| 1227 |
], |
| 1228 |
supported_groups=[tls.Group.SECP256R1], |
| 1229 |
supported_versions=[ |
| 1230 |
tls.TLS_VERSION_1_3, |
| 1231 |
tls.TLS_VERSION_1_3_DRAFT_28, |
| 1232 |
tls.TLS_VERSION_1_3_DRAFT_27, |
| 1233 |
tls.TLS_VERSION_1_3_DRAFT_26, |
| 1234 |
], |
| 1235 |
other_extensions=[ |
| 1236 |
( |
| 1237 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1238 |
CLIENT_QUIC_TRANSPORT_PARAMETERS, |
| 1239 |
) |
| 1240 |
], |
| 1241 |
) |
| 1242 |
|
| 1243 |
buf = Buffer(1000) |
| 1244 |
push_client_hello(buf, hello) |
| 1245 |
self.assertEqual(buf.data, load("tls_client_hello.bin")) |
| 1246 |
|
| 1247 |
def test_pull_server_hello(self): |
| 1248 |
buf = Buffer(data=load("tls_server_hello.bin")) |
| 1249 |
hello = pull_server_hello(buf) |
| 1250 |
self.assertTrue(buf.eof()) |
| 1251 |
|
| 1252 |
self.assertEqual( |
| 1253 |
hello.random, |
| 1254 |
binascii.unhexlify( |
| 1255 |
"ada85271d19680c615ea7336519e3fdf6f1e26f3b1075ee1de96ffa8884e8280" |
| 1256 |
), |
| 1257 |
) |
| 1258 |
self.assertEqual( |
| 1259 |
hello.legacy_session_id, |
| 1260 |
binascii.unhexlify( |
| 1261 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 1262 |
), |
| 1263 |
) |
| 1264 |
self.assertEqual(hello.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384) |
| 1265 |
self.assertEqual(hello.compression_method, tls.CompressionMethod.NULL) |
| 1266 |
self.assertEqual( |
| 1267 |
hello.key_share, |
| 1268 |
( |
| 1269 |
tls.Group.SECP256R1, |
| 1270 |
binascii.unhexlify( |
| 1271 |
"048b27d0282242d84b7fcc02a9c4f13eca0329e3c7029aa34a33794e6e7ba189" |
| 1272 |
"5cca1c503bf0378ac6937c354912116ff3251026bca1958d7f387316c83ae6cf" |
| 1273 |
"b2" |
| 1274 |
), |
| 1275 |
), |
| 1276 |
) |
| 1277 |
self.assertEqual(hello.pre_shared_key, None) |
| 1278 |
self.assertEqual(hello.supported_version, tls.TLS_VERSION_1_3) |
| 1279 |
|
| 1280 |
def test_pull_server_hello_with_psk(self): |
| 1281 |
buf = Buffer(data=load("tls_server_hello_with_psk.bin")) |
| 1282 |
hello = pull_server_hello(buf) |
| 1283 |
self.assertTrue(buf.eof()) |
| 1284 |
|
| 1285 |
self.assertEqual( |
| 1286 |
hello.random, |
| 1287 |
binascii.unhexlify( |
| 1288 |
"ccbaaf04fc1bd5143b2cc6b97520cf37d91470dbfc8127131a7bf0f941e3a137" |
| 1289 |
), |
| 1290 |
) |
| 1291 |
self.assertEqual( |
| 1292 |
hello.legacy_session_id, |
| 1293 |
binascii.unhexlify( |
| 1294 |
"9483e7e895d0f4cec17086b0849601c0632662cd764e828f2f892f4c4b7771b0" |
| 1295 |
), |
| 1296 |
) |
| 1297 |
self.assertEqual(hello.cipher_suite, tls.CipherSuite.AES_256_GCM_SHA384) |
| 1298 |
self.assertEqual(hello.compression_method, tls.CompressionMethod.NULL) |
| 1299 |
self.assertEqual( |
| 1300 |
hello.key_share, |
| 1301 |
( |
| 1302 |
tls.Group.SECP256R1, |
| 1303 |
binascii.unhexlify( |
| 1304 |
"0485d7cecbebfc548fc657bf51b8e8da842a4056b164a27f7702ca318c16e488" |
| 1305 |
"18b6409593b15c6649d6f459387a53128b164178adc840179aad01d36ce95d62" |
| 1306 |
"76" |
| 1307 |
), |
| 1308 |
), |
| 1309 |
) |
| 1310 |
self.assertEqual(hello.pre_shared_key, 0) |
| 1311 |
self.assertEqual(hello.supported_version, tls.TLS_VERSION_1_3) |
| 1312 |
|
| 1313 |
|
| 1314 |
buf = Buffer(1000) |
| 1315 |
push_server_hello(buf, hello) |
| 1316 |
self.assertEqual(buf.data, load("tls_server_hello_with_psk.bin")) |
| 1317 |
|
| 1318 |
def test_pull_server_hello_with_unexpected_version(self): |
| 1319 |
buf = Buffer(data=corrupt_hello_version(load("tls_server_hello.bin"))) |
| 1320 |
with self.assertRaises(tls.AlertDecodeError) as cm: |
| 1321 |
pull_server_hello(buf) |
| 1322 |
self.assertEqual(str(cm.exception), "ServerHello version is not 1.2") |
| 1323 |
|
| 1324 |
def test_pull_server_hello_with_unknown_extension(self): |
| 1325 |
buf = Buffer(data=load("tls_server_hello_with_unknown_extension.bin")) |
| 1326 |
hello = pull_server_hello(buf) |
| 1327 |
self.assertTrue(buf.eof()) |
| 1328 |
|
| 1329 |
self.assertEqual( |
| 1330 |
hello, |
| 1331 |
ServerHello( |
| 1332 |
random=binascii.unhexlify( |
| 1333 |
"ada85271d19680c615ea7336519e3fdf6f1e26f3b1075ee1de96ffa8884e8280" |
| 1334 |
), |
| 1335 |
legacy_session_id=binascii.unhexlify( |
| 1336 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 1337 |
), |
| 1338 |
cipher_suite=tls.CipherSuite.AES_256_GCM_SHA384, |
| 1339 |
compression_method=tls.CompressionMethod.NULL, |
| 1340 |
key_share=( |
| 1341 |
tls.Group.SECP256R1, |
| 1342 |
binascii.unhexlify( |
| 1343 |
"048b27d0282242d84b7fcc02a9c4f13eca0329e3c7029aa34a33794e6e7ba189" |
| 1344 |
"5cca1c503bf0378ac6937c354912116ff3251026bca1958d7f387316c83ae6cf" |
| 1345 |
"b2" |
| 1346 |
), |
| 1347 |
), |
| 1348 |
supported_version=tls.TLS_VERSION_1_3, |
| 1349 |
other_extensions=[(12345, b"foo")], |
| 1350 |
), |
| 1351 |
) |
| 1352 |
|
| 1353 |
|
| 1354 |
buf = Buffer(1000) |
| 1355 |
push_server_hello(buf, hello) |
| 1356 |
self.assertEqual(buf.data, load("tls_server_hello_with_unknown_extension.bin")) |
| 1357 |
|
| 1358 |
def test_push_server_hello(self): |
| 1359 |
hello = ServerHello( |
| 1360 |
random=binascii.unhexlify( |
| 1361 |
"ada85271d19680c615ea7336519e3fdf6f1e26f3b1075ee1de96ffa8884e8280" |
| 1362 |
), |
| 1363 |
legacy_session_id=binascii.unhexlify( |
| 1364 |
"9aee82a2d186c1cb32a329d9dcfe004a1a438ad0485a53c6bfcf55c132a23235" |
| 1365 |
), |
| 1366 |
cipher_suite=tls.CipherSuite.AES_256_GCM_SHA384, |
| 1367 |
compression_method=tls.CompressionMethod.NULL, |
| 1368 |
key_share=( |
| 1369 |
tls.Group.SECP256R1, |
| 1370 |
binascii.unhexlify( |
| 1371 |
"048b27d0282242d84b7fcc02a9c4f13eca0329e3c7029aa34a33794e6e7ba189" |
| 1372 |
"5cca1c503bf0378ac6937c354912116ff3251026bca1958d7f387316c83ae6cf" |
| 1373 |
"b2" |
| 1374 |
), |
| 1375 |
), |
| 1376 |
supported_version=tls.TLS_VERSION_1_3, |
| 1377 |
) |
| 1378 |
|
| 1379 |
buf = Buffer(1000) |
| 1380 |
push_server_hello(buf, hello) |
| 1381 |
self.assertEqual(buf.data, load("tls_server_hello.bin")) |
| 1382 |
|
| 1383 |
def test_pull_new_session_ticket(self): |
| 1384 |
buf = Buffer(data=load("tls_new_session_ticket.bin")) |
| 1385 |
new_session_ticket = pull_new_session_ticket(buf) |
| 1386 |
self.assertIsNotNone(new_session_ticket) |
| 1387 |
self.assertTrue(buf.eof()) |
| 1388 |
|
| 1389 |
self.assertEqual( |
| 1390 |
new_session_ticket, |
| 1391 |
NewSessionTicket( |
| 1392 |
ticket_lifetime=86400, |
| 1393 |
ticket_age_add=3303452425, |
| 1394 |
ticket_nonce=b"", |
| 1395 |
ticket=binascii.unhexlify( |
| 1396 |
"dbe6f1a77a78c0426bfa607cd0d02b350247d90618704709596beda7e962cc81" |
| 1397 |
), |
| 1398 |
max_early_data_size=0xFFFFFFFF, |
| 1399 |
), |
| 1400 |
) |
| 1401 |
|
| 1402 |
|
| 1403 |
buf = Buffer(100) |
| 1404 |
push_new_session_ticket(buf, new_session_ticket) |
| 1405 |
self.assertEqual(buf.data, load("tls_new_session_ticket.bin")) |
| 1406 |
|
| 1407 |
def test_pull_new_session_ticket_with_unknown_extension(self): |
| 1408 |
buf = Buffer(data=load("tls_new_session_ticket_with_unknown_extension.bin")) |
| 1409 |
new_session_ticket = pull_new_session_ticket(buf) |
| 1410 |
self.assertIsNotNone(new_session_ticket) |
| 1411 |
self.assertTrue(buf.eof()) |
| 1412 |
|
| 1413 |
self.assertEqual( |
| 1414 |
new_session_ticket, |
| 1415 |
NewSessionTicket( |
| 1416 |
ticket_lifetime=86400, |
| 1417 |
ticket_age_add=3303452425, |
| 1418 |
ticket_nonce=b"", |
| 1419 |
ticket=binascii.unhexlify( |
| 1420 |
"dbe6f1a77a78c0426bfa607cd0d02b350247d90618704709596beda7e962cc81" |
| 1421 |
), |
| 1422 |
max_early_data_size=0xFFFFFFFF, |
| 1423 |
other_extensions=[(12345, b"foo")], |
| 1424 |
), |
| 1425 |
) |
| 1426 |
|
| 1427 |
|
| 1428 |
buf = Buffer(100) |
| 1429 |
push_new_session_ticket(buf, new_session_ticket) |
| 1430 |
self.assertEqual( |
| 1431 |
buf.data, load("tls_new_session_ticket_with_unknown_extension.bin") |
| 1432 |
) |
| 1433 |
|
| 1434 |
def test_encrypted_extensions(self): |
| 1435 |
data = load("tls_encrypted_extensions.bin") |
| 1436 |
buf = Buffer(data=data) |
| 1437 |
extensions = pull_encrypted_extensions(buf) |
| 1438 |
self.assertIsNotNone(extensions) |
| 1439 |
self.assertTrue(buf.eof()) |
| 1440 |
|
| 1441 |
self.assertEqual( |
| 1442 |
extensions, |
| 1443 |
EncryptedExtensions( |
| 1444 |
other_extensions=[ |
| 1445 |
( |
| 1446 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1447 |
SERVER_QUIC_TRANSPORT_PARAMETERS, |
| 1448 |
) |
| 1449 |
] |
| 1450 |
), |
| 1451 |
) |
| 1452 |
|
| 1453 |
|
| 1454 |
buf = Buffer(capacity=100) |
| 1455 |
push_encrypted_extensions(buf, extensions) |
| 1456 |
self.assertEqual(buf.data, data) |
| 1457 |
|
| 1458 |
def test_encrypted_extensions_with_alpn(self): |
| 1459 |
data = load("tls_encrypted_extensions_with_alpn.bin") |
| 1460 |
buf = Buffer(data=data) |
| 1461 |
extensions = pull_encrypted_extensions(buf) |
| 1462 |
self.assertIsNotNone(extensions) |
| 1463 |
self.assertTrue(buf.eof()) |
| 1464 |
|
| 1465 |
self.assertEqual( |
| 1466 |
extensions, |
| 1467 |
EncryptedExtensions( |
| 1468 |
alpn_protocol="hq-20", |
| 1469 |
other_extensions=[ |
| 1470 |
(tls.ExtensionType.SERVER_NAME, b""), |
| 1471 |
( |
| 1472 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1473 |
SERVER_QUIC_TRANSPORT_PARAMETERS_2, |
| 1474 |
), |
| 1475 |
], |
| 1476 |
), |
| 1477 |
) |
| 1478 |
|
| 1479 |
|
| 1480 |
buf = Buffer(115) |
| 1481 |
push_encrypted_extensions(buf, extensions) |
| 1482 |
self.assertTrue(buf.eof()) |
| 1483 |
|
| 1484 |
def test_pull_encrypted_extensions_with_alpn_and_early_data(self): |
| 1485 |
buf = Buffer(data=load("tls_encrypted_extensions_with_alpn_and_early_data.bin")) |
| 1486 |
extensions = pull_encrypted_extensions(buf) |
| 1487 |
self.assertIsNotNone(extensions) |
| 1488 |
self.assertTrue(buf.eof()) |
| 1489 |
|
| 1490 |
self.assertEqual( |
| 1491 |
extensions, |
| 1492 |
EncryptedExtensions( |
| 1493 |
alpn_protocol="hq-20", |
| 1494 |
early_data=True, |
| 1495 |
other_extensions=[ |
| 1496 |
(tls.ExtensionType.SERVER_NAME, b""), |
| 1497 |
( |
| 1498 |
tls.ExtensionType.QUIC_TRANSPORT_PARAMETERS_DRAFT, |
| 1499 |
SERVER_QUIC_TRANSPORT_PARAMETERS_3, |
| 1500 |
), |
| 1501 |
], |
| 1502 |
), |
| 1503 |
) |
| 1504 |
|
| 1505 |
|
| 1506 |
buf = Buffer(116) |
| 1507 |
push_encrypted_extensions(buf, extensions) |
| 1508 |
self.assertTrue(buf.eof()) |
| 1509 |
|
| 1510 |
def test_pull_certificate(self): |
| 1511 |
buf = Buffer(data=load("tls_certificate.bin")) |
| 1512 |
certificate = pull_certificate(buf) |
| 1513 |
self.assertTrue(buf.eof()) |
| 1514 |
|
| 1515 |
self.assertEqual(certificate.request_context, b"") |
| 1516 |
self.assertEqual(certificate.certificates, [(CERTIFICATE_DATA, b"")]) |
| 1517 |
|
| 1518 |
def test_push_certificate(self): |
| 1519 |
certificate = Certificate( |
| 1520 |
request_context=b"", certificates=[(CERTIFICATE_DATA, b"")] |
| 1521 |
) |
| 1522 |
|
| 1523 |
buf = Buffer(1600) |
| 1524 |
push_certificate(buf, certificate) |
| 1525 |
self.assertEqual(buf.data, load("tls_certificate.bin")) |
| 1526 |
|
| 1527 |
def test_pull_certificate_request(self): |
| 1528 |
buf = Buffer(data=load("tls_certificate_request.bin")) |
| 1529 |
certificate_request = pull_certificate_request(buf) |
| 1530 |
self.assertTrue(buf.eof()) |
| 1531 |
|
| 1532 |
self.assertEqual(certificate_request.request_context, b"") |
| 1533 |
self.assertEqual( |
| 1534 |
certificate_request.signature_algorithms, |
| 1535 |
[ |
| 1536 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 1537 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 1538 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 1539 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA1, |
| 1540 |
], |
| 1541 |
) |
| 1542 |
self.assertEqual(certificate_request.other_extensions, [(12345, b"foo")]) |
| 1543 |
|
| 1544 |
def test_push_certificate_request(self): |
| 1545 |
certificate_request = CertificateRequest( |
| 1546 |
request_context=b"", |
| 1547 |
signature_algorithms=[ |
| 1548 |
tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 1549 |
tls.SignatureAlgorithm.ECDSA_SECP256R1_SHA256, |
| 1550 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA256, |
| 1551 |
tls.SignatureAlgorithm.RSA_PKCS1_SHA1, |
| 1552 |
], |
| 1553 |
other_extensions=[(12345, b"foo")], |
| 1554 |
) |
| 1555 |
|
| 1556 |
buf = Buffer(400) |
| 1557 |
push_certificate_request(buf, certificate_request) |
| 1558 |
self.assertEqual(buf.data, load("tls_certificate_request.bin")) |
| 1559 |
|
| 1560 |
def test_pull_certificate_verify(self): |
| 1561 |
buf = Buffer(data=load("tls_certificate_verify.bin")) |
| 1562 |
verify = pull_certificate_verify(buf) |
| 1563 |
self.assertTrue(buf.eof()) |
| 1564 |
|
| 1565 |
self.assertEqual(verify.algorithm, tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256) |
| 1566 |
self.assertEqual(verify.signature, CERTIFICATE_VERIFY_SIGNATURE) |
| 1567 |
|
| 1568 |
def test_push_certificate_verify(self): |
| 1569 |
verify = CertificateVerify( |
| 1570 |
algorithm=tls.SignatureAlgorithm.RSA_PSS_RSAE_SHA256, |
| 1571 |
signature=CERTIFICATE_VERIFY_SIGNATURE, |
| 1572 |
) |
| 1573 |
|
| 1574 |
buf = Buffer(400) |
| 1575 |
push_certificate_verify(buf, verify) |
| 1576 |
self.assertEqual(buf.data, load("tls_certificate_verify.bin")) |
| 1577 |
|
| 1578 |
def test_pull_finished(self): |
| 1579 |
buf = Buffer(data=load("tls_finished.bin")) |
| 1580 |
finished = pull_finished(buf) |
| 1581 |
self.assertTrue(buf.eof()) |
| 1582 |
|
| 1583 |
self.assertEqual( |
| 1584 |
finished.verify_data, |
| 1585 |
binascii.unhexlify( |
| 1586 |
"f157923234ff9a4921aadb2e0ec7b1a30fce73fb9ec0c4276f9af268f408ec68" |
| 1587 |
), |
| 1588 |
) |
| 1589 |
|
| 1590 |
def test_push_finished(self): |
| 1591 |
finished = Finished( |
| 1592 |
verify_data=binascii.unhexlify( |
| 1593 |
"f157923234ff9a4921aadb2e0ec7b1a30fce73fb9ec0c4276f9af268f408ec68" |
| 1594 |
) |
| 1595 |
) |
| 1596 |
|
| 1597 |
buf = Buffer(128) |
| 1598 |
push_finished(buf, finished) |
| 1599 |
self.assertEqual(buf.data, load("tls_finished.bin")) |
| 1600 |
|
| 1601 |
def test_pull_server_name(self): |
| 1602 |
buf = Buffer(data=b"\x00\x12\x00\x00\x0fwww.example.com") |
| 1603 |
self.assertEqual(pull_server_name(buf), "www.example.com") |
| 1604 |
|
| 1605 |
def test_pull_server_name_with_bad_name_type(self): |
| 1606 |
buf = Buffer(data=b"\x00\x12\xff\x00\x0fwww.example.com") |
| 1607 |
with self.assertRaises(tls.AlertIllegalParameter) as cm: |
| 1608 |
pull_server_name(buf) |
| 1609 |
self.assertEqual(str(cm.exception), "ServerName has an unknown name type 255") |
| 1610 |
|
| 1611 |
def test_push_server_name(self): |
| 1612 |
buf = Buffer(128) |
| 1613 |
push_server_name(buf, "www.example.com") |
| 1614 |
self.assertEqual(buf.data, b"\x00\x12\x00\x00\x0fwww.example.com") |
| 1615 |
|
| 1616 |
|
| 1617 |
class VerifyCertificateTest(TestCase): |
| 1618 |
def test_verify_certificate_chain(self): |
| 1619 |
with open(SERVER_CERTFILE, "rb") as fp: |
| 1620 |
certificate = load_pem_x509_certificates(fp.read())[0] |
| 1621 |
|
| 1622 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1623 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1624 |
|
| 1625 |
|
| 1626 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 1627 |
verify_certificate(certificate=certificate, server_name="localhost") |
| 1628 |
self.assertEqual( |
| 1629 |
str(cm.exception), "unable to get local issuer certificate" |
| 1630 |
) |
| 1631 |
|
| 1632 |
|
| 1633 |
verify_certificate( |
| 1634 |
cafile=SERVER_CACERTFILE, |
| 1635 |
certificate=certificate, |
| 1636 |
server_name="localhost", |
| 1637 |
) |
| 1638 |
|
| 1639 |
def test_verify_certificate_chain_self_signed(self): |
| 1640 |
certificate, _ = generate_ec_certificate( |
| 1641 |
alternative_names=["localhost"], common_name="localhost" |
| 1642 |
) |
| 1643 |
|
| 1644 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1645 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1646 |
|
| 1647 |
|
| 1648 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 1649 |
verify_certificate(certificate=certificate, server_name="localhost") |
| 1650 |
self.assertIn( |
| 1651 |
str(cm.exception), |
| 1652 |
( |
| 1653 |
"self signed certificate", |
| 1654 |
"self-signed certificate", |
| 1655 |
), |
| 1656 |
) |
| 1657 |
|
| 1658 |
|
| 1659 |
verify_certificate( |
| 1660 |
cadata=certificate.public_bytes(serialization.Encoding.PEM), |
| 1661 |
certificate=certificate, |
| 1662 |
server_name="localhost", |
| 1663 |
) |
| 1664 |
|
| 1665 |
def test_verify_dates(self): |
| 1666 |
certificate, _ = generate_ec_certificate( |
| 1667 |
alternative_names=["example.com"], common_name="example.com" |
| 1668 |
) |
| 1669 |
cadata = certificate.public_bytes(serialization.Encoding.PEM) |
| 1670 |
|
| 1671 |
y |
| 1672 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1673 |
mock_utcnow.return_value = ( |
| 1674 |
certificate.not_valid_before_utc - datetime.timedelta(seconds=1) |
| 1675 |
) |
| 1676 |
with self.assertRaises(tls.AlertCertificateExpired) as cm: |
| 1677 |
verify_certificate( |
| 1678 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1679 |
) |
| 1680 |
self.assertEqual(str(cm.exception), "Certificate is not valid yet") |
| 1681 |
|
| 1682 |
|
| 1683 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1684 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1685 |
verify_certificate( |
| 1686 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1687 |
) |
| 1688 |
|
| 1689 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1690 |
mock_utcnow.return_value = certificate.not_valid_after_utc |
| 1691 |
verify_certificate( |
| 1692 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1693 |
) |
| 1694 |
|
| 1695 |
|
| 1696 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1697 |
mock_utcnow.return_value = ( |
| 1698 |
certificate.not_valid_after_utc + datetime.timedelta(seconds=1) |
| 1699 |
) |
| 1700 |
with self.assertRaises(tls.AlertCertificateExpired) as cm: |
| 1701 |
verify_certificate( |
| 1702 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1703 |
) |
| 1704 |
self.assertEqual(str(cm.exception), "Certificate is no longer valid") |
| 1705 |
|
| 1706 |
def test_verify_subject_no_subjaltname(self): |
| 1707 |
certificate, _ = generate_ec_certificate(common_name="example.com") |
| 1708 |
cadata = certificate.public_bytes(serialization.Encoding.PEM) |
| 1709 |
|
| 1710 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1711 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1712 |
|
| 1713 |
|
| 1714 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 1715 |
verify_certificate( |
| 1716 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1717 |
) |
| 1718 |
self.assertEqual( |
| 1719 |
str(cm.exception), "Certificate does not contain any `subjectAltName`s." |
| 1720 |
) |
| 1721 |
|
| 1722 |
def test_verify_subject_with_subjaltname(self): |
| 1723 |
certificate, _ = generate_ec_certificate( |
| 1724 |
alternative_names=["*.example.com", "example.com"], |
| 1725 |
common_name="example.com", |
| 1726 |
) |
| 1727 |
cadata = certificate.public_bytes(serialization.Encoding.PEM) |
| 1728 |
|
| 1729 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1730 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1731 |
|
| 1732 |
|
| 1733 |
verify_certificate( |
| 1734 |
cadata=cadata, certificate=certificate, server_name="example.com" |
| 1735 |
) |
| 1736 |
verify_certificate( |
| 1737 |
cadata=cadata, certificate=certificate, server_name="test.example.com" |
| 1738 |
) |
| 1739 |
|
| 1740 |
|
| 1741 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 1742 |
verify_certificate( |
| 1743 |
cadata=cadata, certificate=certificate, server_name="acme.com" |
| 1744 |
) |
| 1745 |
self.assertEqual( |
| 1746 |
str(cm.exception), |
| 1747 |
"hostname 'acme.com' doesn't match either of " |
| 1748 |
"DNSPattern(pattern=b'*.example.com'), " |
| 1749 |
"DNSPattern(pattern=b'example.com')", |
| 1750 |
) |
| 1751 |
|
| 1752 |
def test_verify_subject_with_subjaltname_ipaddress(self): |
| 1753 |
certificate, _ = generate_ec_certificate( |
| 1754 |
alternative_names=["1.2.3.4"], |
| 1755 |
common_name="1.2.3.4", |
| 1756 |
) |
| 1757 |
cadata = certificate.public_bytes(serialization.Encoding.PEM) |
| 1758 |
|
| 1759 |
with patch("aioquic.tls.utcnow") as mock_utcnow: |
| 1760 |
mock_utcnow.return_value = certificate.not_valid_before_utc |
| 1761 |
|
| 1762 |
|
| 1763 |
verify_certificate( |
| 1764 |
cadata=cadata, certificate=certificate, server_name="1.2.3.4" |
| 1765 |
) |
| 1766 |
|
| 1767 |
|
| 1768 |
with self.assertRaises(tls.AlertBadCertificate) as cm: |
| 1769 |
verify_certificate( |
| 1770 |
cadata=cadata, certificate=certificate, server_name="8.8.8.8" |
| 1771 |
) |
| 1772 |
self.assertEqual( |
| 1773 |
str(cm.exception), |
| 1774 |
"hostname '8.8.8.8' doesn't match " |
| 1775 |
"IPAddressPattern(pattern=IPv4Address('1.2.3.4'))", |
| 1776 |
) |
| 1777 |
|
| 1778 |
def test_pull_greased_alpn_list(self): |
| 1779 |
"""Test pulling a list alpns with an ASCII item, an undecodable binary value |
| 1780 |
such as greasing might give us, a valid UTF-8 encoding, and another ASCII item. |
| 1781 |
We should only return the ASCII values. |
| 1782 |
|
| 1783 |
We currently only accept ASCII ALPNs, even though technically ALPNs are |
| 1784 |
arbitrary bytes values, as our API is a list of strings. |
| 1785 |
""" |
| 1786 |
|
| 1787 |
" |
| 1788 |
buf = Buffer(data=binascii.unhexlify("000c02483202ffff02c3a9024833")) |
| 1789 |
|
| 1790 |
self.assertEqual( |
| 1791 |
tls.pull_list(buf, 2, partial(tls.pull_alpn_protocol, buf)), ["H2", "H3"] |
| 1792 |
) |