1 /* 2 * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <stdio.h> 11 #include <string.h> 12 13 #include <openssl/opensslconf.h> 14 #include <openssl/quic.h> 15 #include <openssl/rand.h> 16 17 #include "helpers/ssltestlib.h" 18 #include "helpers/quictestlib.h" 19 #include "testutil.h" 20 #include "testutil/output.h" 21 #include "../ssl/ssl_local.h" 22 #include "internal/quic_error.h" 23 24 static OSSL_LIB_CTX *libctx = NULL; 25 static OSSL_PROVIDER *defctxnull = NULL; 26 static char *certsdir = NULL; 27 static char *cert = NULL; 28 static char *ccert = NULL; 29 static char *cauthca = NULL; 30 static char *privkey = NULL; 31 static char *cprivkey = NULL; 32 static char *datadir = NULL; 33 34 static int is_fips = 0; 35 36 /* The ssltrace test assumes some options are switched on/off */ 37 #if !defined(OPENSSL_NO_SSL_TRACE) \ 38 && defined(OPENSSL_NO_BROTLI) && defined(OPENSSL_NO_ZSTD) \ 39 && !defined(OPENSSL_NO_ECX) && !defined(OPENSSL_NO_DH) \ 40 && !defined(OPENSSL_NO_ML_DSA) && !defined(OPENSSL_NO_ML_KEM) 41 #define DO_SSL_TRACE_TEST 42 #endif 43 44 /* 45 * Test that we read what we've written. 46 * Test 0: Non-blocking 47 * Test 1: Blocking 48 * Test 2: Blocking, introduce socket error, test error handling. 49 */ 50 static int test_quic_write_read(int idx) 51 { 52 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 53 SSL_CTX *sctx = NULL; 54 SSL *clientquic = NULL; 55 QUIC_TSERVER *qtserv = NULL; 56 int j, k, ret = 0; 57 unsigned char buf[20], scratch[64]; 58 static char *msg = "A test message"; 59 size_t msglen = strlen(msg); 60 size_t numbytes = 0; 61 int ssock = 0, csock = 0; 62 uint64_t sid = UINT64_MAX; 63 SSL_SESSION *sess = NULL; 64 65 if (idx >= 1 && !qtest_supports_blocking()) 66 return TEST_skip("Blocking tests not supported in this build"); 67 68 for (k = 0; k < 2; k++) { 69 if (!TEST_ptr(cctx) 70 || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, 71 cert, privkey, 72 idx >= 1 73 ? QTEST_FLAG_BLOCK 74 : 0, 75 &qtserv, &clientquic, 76 NULL, NULL)) 77 || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost"))) 78 goto end; 79 80 if (sess != NULL && !TEST_true(SSL_set_session(clientquic, sess))) 81 goto end; 82 83 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 84 goto end; 85 86 if (idx >= 1) { 87 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv), 88 &ssock))) 89 goto end; 90 if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0)) 91 goto end; 92 } 93 94 sid = 0; /* client-initiated bidirectional stream */ 95 96 for (j = 0; j < 2; j++) { 97 /* Check that sending and receiving app data is ok */ 98 if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes)) 99 || !TEST_size_t_eq(numbytes, msglen)) 100 goto end; 101 if (idx >= 1) { 102 do { 103 if (!TEST_true(wait_until_sock_readable(ssock))) 104 goto end; 105 106 ossl_quic_tserver_tick(qtserv); 107 108 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, 109 sizeof(buf), 110 &numbytes))) 111 goto end; 112 } while (numbytes == 0); 113 114 if (!TEST_mem_eq(buf, numbytes, msg, msglen)) 115 goto end; 116 } 117 118 if (idx >= 2 && j > 0) 119 /* Introduce permanent socket error */ 120 BIO_closesocket(csock); 121 122 ossl_quic_tserver_tick(qtserv); 123 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, 124 (unsigned char *)msg, 125 msglen, &numbytes))) 126 goto end; 127 ossl_quic_tserver_tick(qtserv); 128 SSL_handle_events(clientquic); 129 130 if (idx >= 2 && j > 0) { 131 if (!TEST_false(SSL_read_ex(clientquic, buf, 1, &numbytes)) 132 || !TEST_int_eq(SSL_get_error(clientquic, 0), 133 SSL_ERROR_SYSCALL) 134 || !TEST_false(SSL_write_ex(clientquic, msg, msglen, 135 &numbytes)) 136 || !TEST_int_eq(SSL_get_error(clientquic, 0), 137 SSL_ERROR_SYSCALL)) 138 goto end; 139 break; 140 } 141 142 /* 143 * In blocking mode the SSL_read_ex call will block until the socket 144 * is readable and has our data. In non-blocking mode we're doing 145 * everything in memory, so it should be immediately available 146 */ 147 if (!TEST_true(SSL_read_ex(clientquic, buf, 1, &numbytes)) 148 || !TEST_size_t_eq(numbytes, 1) 149 || !TEST_true(SSL_has_pending(clientquic)) 150 || !TEST_int_eq(SSL_pending(clientquic), msglen - 1) 151 || !TEST_true(SSL_read_ex(clientquic, buf + 1, 152 sizeof(buf) - 1, &numbytes)) 153 || !TEST_mem_eq(buf, numbytes + 1, msg, msglen)) 154 goto end; 155 } 156 157 /* Test that exporters work. */ 158 if (!TEST_true(SSL_export_keying_material(clientquic, scratch, 159 sizeof(scratch), "test", 4, (unsigned char *)"ctx", 3, 160 1))) 161 goto end; 162 163 if (sess == NULL) { 164 /* We didn't supply a session so we're not expecting resumption */ 165 if (!TEST_false(SSL_session_reused(clientquic))) 166 goto end; 167 /* We should have a session ticket by now */ 168 sess = SSL_get1_session(clientquic); 169 if (!TEST_ptr(sess)) 170 goto end; 171 } else { 172 /* We supplied a session so we should have resumed */ 173 if (!TEST_true(SSL_session_reused(clientquic))) 174 goto end; 175 } 176 177 if (!TEST_true(qtest_shutdown(qtserv, clientquic))) 178 goto end; 179 180 if (sctx == NULL) { 181 sctx = ossl_quic_tserver_get0_ssl_ctx(qtserv); 182 if (!TEST_true(SSL_CTX_up_ref(sctx))) { 183 sctx = NULL; 184 goto end; 185 } 186 } 187 ossl_quic_tserver_free(qtserv); 188 qtserv = NULL; 189 SSL_free(clientquic); 190 clientquic = NULL; 191 192 if (idx >= 2) 193 break; 194 } 195 196 ret = 1; 197 198 end: 199 SSL_SESSION_free(sess); 200 ossl_quic_tserver_free(qtserv); 201 SSL_free(clientquic); 202 SSL_CTX_free(cctx); 203 SSL_CTX_free(sctx); 204 205 return ret; 206 } 207 208 /* 209 * Test that sending FIN with no data to a client blocking in SSL_read_ex() will 210 * wake up the client. 211 */ 212 static int test_fin_only_blocking(void) 213 { 214 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 215 SSL_CTX *sctx = NULL; 216 SSL *clientquic = NULL; 217 QUIC_TSERVER *qtserv = NULL; 218 const char *msg = "Hello World"; 219 uint64_t sid; 220 size_t numbytes; 221 unsigned char buf[32]; 222 int ret = 0; 223 OSSL_TIME timer, timediff; 224 225 if (!qtest_supports_blocking()) 226 return TEST_skip("Blocking tests not supported in this build"); 227 228 if (!TEST_ptr(cctx) 229 || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, 230 cert, privkey, 231 QTEST_FLAG_BLOCK, 232 &qtserv, &clientquic, 233 NULL, NULL)) 234 || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost"))) 235 goto end; 236 237 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 238 goto end; 239 240 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)) 241 || !TEST_true(ossl_quic_tserver_write(qtserv, sid, 242 (unsigned char *)msg, 243 strlen(msg), &numbytes)) 244 || !TEST_size_t_eq(strlen(msg), numbytes)) 245 goto end; 246 247 ossl_quic_tserver_tick(qtserv); 248 249 if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)) 250 || !TEST_mem_eq(msg, strlen(msg), buf, numbytes)) 251 252 goto end; 253 254 if (!TEST_true(ossl_quic_tserver_conclude(qtserv, sid))) 255 goto end; 256 257 timer = ossl_time_now(); 258 if (!TEST_false(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))) 259 goto end; 260 timediff = ossl_time_subtract(ossl_time_now(), timer); 261 262 if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_ZERO_RETURN) 263 /* 264 * We expect the SSL_read_ex to not have blocked so this should 265 * be very fast. 40ms should be plenty. 266 */ 267 || !TEST_uint64_t_le(ossl_time2ms(timediff), 40)) 268 goto end; 269 270 if (!TEST_true(qtest_shutdown(qtserv, clientquic))) 271 goto end; 272 273 ret = 1; 274 275 end: 276 ossl_quic_tserver_free(qtserv); 277 SSL_free(clientquic); 278 SSL_CTX_free(cctx); 279 SSL_CTX_free(sctx); 280 281 return ret; 282 } 283 284 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */ 285 static int test_ciphersuites(void) 286 { 287 SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 288 SSL *ssl = NULL; 289 int testresult = 0; 290 const STACK_OF(SSL_CIPHER) *ciphers = NULL; 291 const SSL_CIPHER *cipher; 292 /* We expect this exact list of ciphersuites by default */ 293 int cipherids[] = { 294 TLS1_3_CK_AES_256_GCM_SHA384, 295 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) 296 TLS1_3_CK_CHACHA20_POLY1305_SHA256, 297 #endif 298 TLS1_3_CK_AES_128_GCM_SHA256 299 }; 300 size_t i, j; 301 302 if (!TEST_ptr(ctx)) 303 return 0; 304 305 /* 306 * Attempting to set TLSv1.2 ciphersuites should succeed, even though they 307 * aren't used in QUIC. 308 */ 309 if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "DEFAULT"))) 310 goto err; 311 312 ssl = SSL_new(ctx); 313 if (!TEST_ptr(ssl)) 314 goto err; 315 316 if (!TEST_true(SSL_set_cipher_list(ssl, "DEFAULT"))) 317 goto err; 318 319 ciphers = SSL_get_ciphers(ssl); 320 321 for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) { 322 if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips) 323 continue; 324 cipher = sk_SSL_CIPHER_value(ciphers, j++); 325 if (!TEST_ptr(cipher)) 326 goto err; 327 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i])) 328 goto err; 329 } 330 331 /* We should have checked all the ciphers in the stack */ 332 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j)) 333 goto err; 334 335 testresult = 1; 336 err: 337 SSL_free(ssl); 338 SSL_CTX_free(ctx); 339 340 return testresult; 341 } 342 343 static int test_cipher_find(void) 344 { 345 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 346 SSL *clientquic = NULL; 347 struct { 348 const unsigned char *cipherbytes; 349 int ok; 350 } testciphers[] = { 351 { TLS13_AES_128_GCM_SHA256_BYTES, 1 }, 352 { TLS13_AES_256_GCM_SHA384_BYTES, 1 }, 353 { TLS13_CHACHA20_POLY1305_SHA256_BYTES, 1 }, 354 { TLS13_AES_128_CCM_SHA256_BYTES, 0 }, 355 { TLS13_AES_128_CCM_8_SHA256_BYTES, 0 }, 356 #if !defined(OPENSSL_NO_INTEGRITY_ONLY_CIPHERS) 357 { TLS13_SHA256_SHA256_BYTES, 0 }, 358 { TLS13_SHA384_SHA384_BYTES, 0 } 359 #endif 360 }; 361 size_t i; 362 int testresult = 0; 363 364 if (!TEST_ptr(cctx)) 365 goto err; 366 367 clientquic = SSL_new(cctx); 368 if (!TEST_ptr(clientquic)) 369 goto err; 370 371 for (i = 0; i < OSSL_NELEM(testciphers); i++) 372 if (testciphers[i].ok) { 373 if (!TEST_ptr(SSL_CIPHER_find(clientquic, 374 testciphers[i].cipherbytes))) 375 goto err; 376 } else { 377 if (!TEST_ptr_null(SSL_CIPHER_find(clientquic, 378 testciphers[i].cipherbytes))) 379 goto err; 380 } 381 382 testresult = 1; 383 err: 384 SSL_free(clientquic); 385 SSL_CTX_free(cctx); 386 387 return testresult; 388 } 389 390 /* 391 * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and 392 * SSL_is_dtls return the expected results for a QUIC connection. Compare with 393 * test_version() in sslapitest.c which does the same thing for TLS/DTLS 394 * connections. 395 */ 396 static int test_version(void) 397 { 398 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 399 SSL *clientquic = NULL; 400 QUIC_TSERVER *qtserv = NULL; 401 int testresult = 0; 402 403 if (!TEST_ptr(cctx) 404 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 405 privkey, 0, &qtserv, 406 &clientquic, NULL, NULL)) 407 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 408 goto err; 409 410 if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION) 411 || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1")) 412 goto err; 413 414 if (!TEST_true(SSL_is_quic(clientquic)) 415 || !TEST_false(SSL_is_tls(clientquic)) 416 || !TEST_false(SSL_is_dtls(clientquic))) 417 goto err; 418 419 testresult = 1; 420 err: 421 ossl_quic_tserver_free(qtserv); 422 SSL_free(clientquic); 423 SSL_CTX_free(cctx); 424 425 return testresult; 426 } 427 428 #if defined(DO_SSL_TRACE_TEST) 429 /* 430 * Tests that the SSL_trace() msg_callback works as expected with a QUIC 431 * connection. This also provides testing of the msg_callback at the same time. 432 */ 433 static int test_ssl_trace(void) 434 { 435 SSL_CTX *cctx = NULL; 436 SSL *clientquic = NULL; 437 QUIC_TSERVER *qtserv = NULL; 438 int testresult = 0; 439 BIO *bio = NULL; 440 char *reffile = NULL; 441 442 if (!TEST_ptr(cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())) 443 || !TEST_ptr(bio = BIO_new(BIO_s_mem())) 444 || !TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")) 445 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 446 privkey, 447 QTEST_FLAG_FAKE_TIME, 448 &qtserv, 449 &clientquic, NULL, NULL))) 450 goto err; 451 452 SSL_set_msg_callback(clientquic, SSL_trace); 453 SSL_set_msg_callback_arg(clientquic, bio); 454 455 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 456 goto err; 457 458 /* Skip the comparison of the trace when the fips provider is used. */ 459 if (is_fips) { 460 /* Check whether there was something written. */ 461 if (!TEST_int_gt(BIO_pending(bio), 0)) 462 goto err; 463 } else { 464 465 #ifdef OPENSSL_NO_ZLIB 466 reffile = test_mk_file_path(datadir, "ssltraceref.txt"); 467 #else 468 reffile = test_mk_file_path(datadir, "ssltraceref-zlib.txt"); 469 #endif 470 if (!TEST_true(compare_with_reference_file(bio, reffile))) 471 goto err; 472 } 473 474 testresult = 1; 475 err: 476 ossl_quic_tserver_free(qtserv); 477 SSL_free(clientquic); 478 SSL_CTX_free(cctx); 479 BIO_free(bio); 480 OPENSSL_free(reffile); 481 482 return testresult; 483 } 484 #endif 485 486 #ifndef OPENSSL_NO_SSL_TRACE 487 enum { 488 INITIAL = 0, 489 GATHER_TOKEN = 1, 490 CHECK_TOKEN = 2, 491 SUCCESS = 3, 492 FAILED = 4 493 }; 494 495 static int find_new_token_data(BIO *membio) 496 { 497 char buf[1024]; 498 int state = INITIAL; 499 char *tmpstring; 500 char *tokenval = NULL; 501 /* 502 * This is a state machine, in which we traverse the ssl trace 503 * looking for a sequence of items 504 * The states are: 505 * +---Current State---|----------Action-------------|---Next State---+ 506 * | INITIAL | "Received Frame: New token" | GATHER_TOKEN | 507 * | | !"Received Frame: New token"| INITIAL | 508 * |-------------------|-----------------------------|----------------| 509 * | GATHER_TOKEN | "Token: <TOKENVAL>" | CHECK_TOKEN | 510 * | | !"Token: <TOKENVAL>" | FAILED | 511 * |-------------------|-----------------------------|----------------| 512 * | CHECK_TOKEN | "Token: <TOKENVAL>" | SUCCESS | 513 * | | EOF | FAILED | 514 * +-------------------|-----------------------------|----------------| 515 */ 516 517 while (state != SUCCESS 518 && state != FAILED 519 && BIO_gets(membio, buf, sizeof(buf)) > 0) { 520 switch (state) { 521 case INITIAL: 522 if (strstr(buf, "Received Frame: New token")) 523 state = GATHER_TOKEN; 524 break; 525 case GATHER_TOKEN: 526 TEST_info("Found New Token Marker\n"); 527 tmpstring = strstr(buf, "Token: "); 528 if (tmpstring == NULL) { 529 TEST_info("Next line did not contain a new token\n"); 530 state = FAILED; 531 } else { 532 if (!TEST_ptr(tokenval = OPENSSL_strdup(tmpstring))) 533 return 0; 534 state = CHECK_TOKEN; 535 TEST_info("Recorded Token %s\n", tokenval); 536 } 537 break; 538 case CHECK_TOKEN: 539 tmpstring = strstr(buf, "Token: "); 540 if (tmpstring != NULL 541 && !strcmp(tmpstring, tokenval)) { 542 state = SUCCESS; 543 TEST_info("Matched next connection token %s\n", tmpstring); 544 } 545 default: 546 break; 547 } 548 } 549 550 OPENSSL_free(tokenval); 551 return (state == SUCCESS); 552 } 553 554 static int test_new_token(void) 555 { 556 SSL_CTX *cctx = NULL; 557 SSL *clientquic = NULL; 558 SSL *clientquic2 = NULL; 559 QUIC_TSERVER *qtserv = NULL; 560 QUIC_TSERVER *qtserv2 = NULL; 561 int testresult = 0; 562 BIO *bio = NULL; 563 char msg[] = "The Quic Brown Fox"; 564 size_t written; 565 566 if (!TEST_ptr(cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())) 567 || !TEST_ptr(bio = BIO_new(BIO_s_mem())) 568 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 569 privkey, 570 QTEST_FLAG_FAKE_TIME, 571 &qtserv, 572 &clientquic, NULL, NULL))) 573 574 goto err; 575 576 SSL_set_msg_callback(clientquic, SSL_trace); 577 SSL_set_msg_callback_arg(clientquic, bio); 578 579 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 580 goto err; 581 582 /* Send data from the client */ 583 if (!SSL_write_ex(clientquic, msg, sizeof(msg), &written)) 584 goto err; 585 586 if (written != sizeof(msg)) 587 goto err; 588 589 /* Receive data at the server */ 590 ossl_quic_tserver_tick(qtserv); 591 592 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 593 privkey, 594 QTEST_FLAG_FAKE_TIME, 595 &qtserv2, 596 &clientquic2, NULL, NULL))) 597 goto err; 598 599 SSL_set_msg_callback(clientquic2, SSL_trace); 600 SSL_set_msg_callback_arg(clientquic2, bio); 601 602 /* once we have our new token, create the subsequent connection */ 603 if (!TEST_true(qtest_create_quic_connection(qtserv2, clientquic2))) 604 goto err; 605 606 /* Skip the comparison of the trace when the fips provider is used. */ 607 if (!TEST_true(find_new_token_data(bio))) 608 goto err; 609 610 testresult = 1; 611 err: 612 ossl_quic_tserver_free(qtserv); 613 ossl_quic_tserver_free(qtserv2); 614 SSL_free(clientquic); 615 SSL_free(clientquic2); 616 SSL_CTX_free(cctx); 617 BIO_free(bio); 618 619 return testresult; 620 } 621 #endif 622 623 static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers) 624 { 625 size_t i; 626 627 /* Ensure ciphersuite list is suitably subsetted. */ 628 for (i = 0; i < (size_t)sk_SSL_CIPHER_num(ciphers); ++i) { 629 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); 630 switch (SSL_CIPHER_get_id(cipher)) { 631 case TLS1_3_CK_AES_128_GCM_SHA256: 632 case TLS1_3_CK_AES_256_GCM_SHA384: 633 case TLS1_3_CK_CHACHA20_POLY1305_SHA256: 634 break; 635 default: 636 TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher)); 637 return 0; 638 } 639 } 640 641 return 1; 642 } 643 644 /* 645 * Test that handshake-layer APIs which shouldn't work don't work with QUIC. 646 */ 647 static int test_quic_forbidden_apis_ctx(void) 648 { 649 int testresult = 0; 650 SSL_CTX *ctx = NULL; 651 652 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) 653 goto err; 654 655 #ifndef OPENSSL_NO_SRTP 656 /* This function returns 0 on success and 1 on error, and should fail. */ 657 if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx, "SRTP_AEAD_AES_128_GCM"))) 658 goto err; 659 #endif 660 661 /* 662 * List of ciphersuites we do and don't allow in QUIC. 663 */ 664 #define QUIC_CIPHERSUITES \ 665 "TLS_AES_128_GCM_SHA256:" \ 666 "TLS_AES_256_GCM_SHA384:" \ 667 "TLS_CHACHA20_POLY1305_SHA256" 668 669 #define NON_QUIC_CIPHERSUITES \ 670 "TLS_AES_128_CCM_SHA256:" \ 671 "TLS_AES_256_CCM_SHA384:" \ 672 "TLS_AES_128_CCM_8_SHA256:" \ 673 "TLS_SHA256_SHA256:" \ 674 "TLS_SHA384_SHA384" 675 676 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */ 677 if (!TEST_true(SSL_CTX_set_ciphersuites(ctx, 678 QUIC_CIPHERSUITES ":" NON_QUIC_CIPHERSUITES))) 679 goto err; 680 681 /* 682 * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only 683 * filtered in SSL_get1_supported_ciphers, so we don't check for 684 * non-inclusion here. 685 */ 686 687 testresult = 1; 688 err: 689 SSL_CTX_free(ctx); 690 return testresult; 691 } 692 693 static int test_quic_forbidden_apis(void) 694 { 695 int testresult = 0; 696 SSL_CTX *ctx = NULL; 697 SSL *ssl = NULL; 698 STACK_OF(SSL_CIPHER) *ciphers = NULL; 699 700 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) 701 goto err; 702 703 if (!TEST_ptr(ssl = SSL_new(ctx))) 704 goto err; 705 706 #ifndef OPENSSL_NO_SRTP 707 /* This function returns 0 on success and 1 on error, and should fail. */ 708 if (!TEST_true(SSL_set_tlsext_use_srtp(ssl, "SRTP_AEAD_AES_128_GCM"))) 709 goto err; 710 #endif 711 712 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */ 713 if (!TEST_true(SSL_set_ciphersuites(ssl, 714 QUIC_CIPHERSUITES ":" NON_QUIC_CIPHERSUITES))) 715 goto err; 716 717 /* Non-QUIC ciphersuites must not appear in supported ciphers list. */ 718 if (!TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)) 719 || !TEST_true(ensure_valid_ciphers(ciphers))) 720 goto err; 721 722 testresult = 1; 723 err: 724 sk_SSL_CIPHER_free(ciphers); 725 SSL_free(ssl); 726 SSL_CTX_free(ctx); 727 return testresult; 728 } 729 730 static int test_quic_forbidden_options(void) 731 { 732 int testresult = 0; 733 SSL_CTX *ctx = NULL; 734 SSL *ssl = NULL; 735 char buf[16]; 736 size_t len; 737 738 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) 739 goto err; 740 741 /* QUIC options restrictions do not affect SSL_CTX */ 742 SSL_CTX_set_options(ctx, UINT64_MAX); 743 744 if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX)) 745 goto err; 746 747 /* Set options on CTX which should not be inherited (tested below). */ 748 SSL_CTX_set_read_ahead(ctx, 1); 749 SSL_CTX_set_max_early_data(ctx, 1); 750 SSL_CTX_set_recv_max_early_data(ctx, 1); 751 SSL_CTX_set_quiet_shutdown(ctx, 1); 752 753 if (!TEST_ptr(ssl = SSL_new(ctx))) 754 goto err; 755 756 /* Only permitted options get transferred to SSL object */ 757 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS)) 758 goto err; 759 760 /* Try again using SSL_set_options */ 761 SSL_set_options(ssl, UINT64_MAX); 762 763 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS)) 764 goto err; 765 766 /* Clear everything */ 767 SSL_clear_options(ssl, UINT64_MAX); 768 769 if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0)) 770 goto err; 771 772 /* Readahead */ 773 if (!TEST_false(SSL_get_read_ahead(ssl))) 774 goto err; 775 776 SSL_set_read_ahead(ssl, 1); 777 if (!TEST_false(SSL_get_read_ahead(ssl))) 778 goto err; 779 780 /* Block padding */ 781 if (!TEST_true(SSL_set_block_padding(ssl, 0)) 782 || !TEST_true(SSL_set_block_padding(ssl, 1)) 783 || !TEST_false(SSL_set_block_padding(ssl, 2))) 784 goto err; 785 786 /* Max fragment length */ 787 if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_DISABLED)) 788 || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_512))) 789 goto err; 790 791 /* Max early data */ 792 if (!TEST_false(SSL_set_recv_max_early_data(ssl, 1)) 793 || !TEST_false(SSL_set_max_early_data(ssl, 1))) 794 goto err; 795 796 /* Read/Write */ 797 if (!TEST_false(SSL_read_early_data(ssl, buf, sizeof(buf), &len)) 798 || !TEST_false(SSL_write_early_data(ssl, buf, sizeof(buf), &len))) 799 goto err; 800 801 /* Buffer Management */ 802 if (!TEST_true(SSL_alloc_buffers(ssl)) 803 || !TEST_false(SSL_free_buffers(ssl))) 804 goto err; 805 806 /* Pipelining */ 807 if (!TEST_false(SSL_set_max_send_fragment(ssl, 2)) 808 || !TEST_false(SSL_set_split_send_fragment(ssl, 2)) 809 || !TEST_false(SSL_set_max_pipelines(ssl, 2))) 810 goto err; 811 812 /* HRR */ 813 if (!TEST_false(SSL_stateless(ssl))) 814 goto err; 815 816 /* Quiet Shutdown */ 817 if (!TEST_false(SSL_get_quiet_shutdown(ssl))) 818 goto err; 819 820 /* No duplication */ 821 if (!TEST_ptr_null(SSL_dup(ssl))) 822 goto err; 823 824 /* No clear */ 825 if (!TEST_false(SSL_clear(ssl))) 826 goto err; 827 828 testresult = 1; 829 err: 830 SSL_free(ssl); 831 SSL_CTX_free(ctx); 832 return testresult; 833 } 834 835 static int test_quic_set_fd(int idx) 836 { 837 int testresult = 0; 838 SSL_CTX *ctx = NULL; 839 SSL *ssl = NULL; 840 int fd = -1, resfd = -1; 841 BIO *bio = NULL; 842 843 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) 844 goto err; 845 846 if (!TEST_ptr(ssl = SSL_new(ctx))) 847 goto err; 848 849 if (!TEST_int_ge(fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0), 0)) 850 goto err; 851 852 if (idx == 0) { 853 if (!TEST_true(SSL_set_fd(ssl, fd))) 854 goto err; 855 if (!TEST_ptr(bio = SSL_get_rbio(ssl))) 856 goto err; 857 if (!TEST_ptr_eq(bio, SSL_get_wbio(ssl))) 858 goto err; 859 } else if (idx == 1) { 860 if (!TEST_true(SSL_set_rfd(ssl, fd))) 861 goto err; 862 if (!TEST_ptr(bio = SSL_get_rbio(ssl))) 863 goto err; 864 if (!TEST_ptr_null(SSL_get_wbio(ssl))) 865 goto err; 866 } else { 867 if (!TEST_true(SSL_set_wfd(ssl, fd))) 868 goto err; 869 if (!TEST_ptr(bio = SSL_get_wbio(ssl))) 870 goto err; 871 if (!TEST_ptr_null(SSL_get_rbio(ssl))) 872 goto err; 873 } 874 875 if (!TEST_int_eq(BIO_method_type(bio), BIO_TYPE_DGRAM)) 876 goto err; 877 878 if (!TEST_true(BIO_get_fd(bio, &resfd)) 879 || !TEST_int_eq(resfd, fd)) 880 goto err; 881 882 testresult = 1; 883 err: 884 SSL_free(ssl); 885 SSL_CTX_free(ctx); 886 if (fd >= 0) 887 BIO_closesocket(fd); 888 return testresult; 889 } 890 891 #define MAXLOOPS 1000 892 893 static int test_bio_ssl(void) 894 { 895 /* 896 * We just use OSSL_QUIC_client_method() rather than 897 * OSSL_QUIC_client_thread_method(). We will never leave the connection idle 898 * so we will always be implicitly handling time events anyway via other 899 * IO calls. 900 */ 901 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 902 SSL *clientquic = NULL, *stream = NULL; 903 QUIC_TSERVER *qtserv = NULL; 904 int testresult = 0; 905 BIO *cbio = NULL, *strbio = NULL, *thisbio; 906 const char *msg = "Hello world"; 907 int abortctr = 0, err, clienterr = 0, servererr = 0, retc = 0, rets = 0; 908 size_t written, readbytes, msglen; 909 int sid = 0, i; 910 unsigned char buf[80]; 911 912 if (!TEST_ptr(cctx)) 913 goto err; 914 915 cbio = BIO_new_ssl(cctx, 1); 916 if (!TEST_ptr(cbio)) 917 goto err; 918 919 /* 920 * We must configure the ALPN/peer address etc so we get the SSL object in 921 * order to pass it to qtest_create_quic_objects for configuration. 922 */ 923 if (!TEST_int_eq(BIO_get_ssl(cbio, &clientquic), 1)) 924 goto err; 925 926 if (!TEST_true(qtest_create_quic_objects(libctx, NULL, NULL, cert, privkey, 927 QTEST_FLAG_FAKE_TIME, &qtserv, 928 &clientquic, NULL, NULL))) 929 goto err; 930 931 msglen = strlen(msg); 932 933 do { 934 err = BIO_FLAGS_WRITE; 935 while (!clienterr && !retc && err == BIO_FLAGS_WRITE) { 936 retc = BIO_write_ex(cbio, msg, msglen, &written); 937 if (!retc) { 938 if (BIO_should_retry(cbio)) 939 err = BIO_retry_type(cbio); 940 else 941 err = 0; 942 } 943 } 944 945 if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) { 946 TEST_info("BIO_write_ex() failed %d, %d", retc, err); 947 TEST_openssl_errors(); 948 clienterr = 1; 949 } 950 951 if (!servererr && rets <= 0) { 952 ossl_quic_tserver_tick(qtserv); 953 qtest_add_time(100); 954 servererr = ossl_quic_tserver_is_term_any(qtserv); 955 if (!servererr) 956 rets = ossl_quic_tserver_is_handshake_confirmed(qtserv); 957 } 958 959 if (clienterr && servererr) 960 goto err; 961 962 if (++abortctr == MAXLOOPS) { 963 TEST_info("No progress made"); 964 goto err; 965 } 966 } while ((!retc && !clienterr) || (rets <= 0 && !servererr)); 967 968 /* 969 * 2 loops: The first using the default stream, and the second using a new 970 * client initiated bidi stream. 971 */ 972 for (i = 0, thisbio = cbio; i < 2; i++) { 973 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf), 974 &readbytes)) 975 || !TEST_mem_eq(msg, msglen, buf, readbytes)) 976 goto err; 977 978 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg, 979 msglen, &written))) 980 goto err; 981 ossl_quic_tserver_tick(qtserv); 982 983 if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes)) 984 || !TEST_mem_eq(msg, msglen, buf, readbytes)) 985 goto err; 986 987 if (i == 1) 988 break; 989 990 if (!TEST_true(SSL_set_mode(clientquic, 0))) 991 goto err; 992 993 /* 994 * Now create a new stream and repeat. The bottom two bits of the stream 995 * id represents whether the stream is bidi and whether it is client 996 * initiated or not. For client initiated bidi they are both 0. So the 997 * first client initiated bidi stream is 0 and the next one is 4. 998 */ 999 sid = 4; 1000 stream = SSL_new_stream(clientquic, 0); 1001 if (!TEST_ptr(stream)) 1002 goto err; 1003 1004 if (!TEST_true(SSL_set_mode(stream, 0))) 1005 goto err; 1006 1007 thisbio = strbio = BIO_new(BIO_f_ssl()); 1008 if (!TEST_ptr(strbio)) 1009 goto err; 1010 1011 if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1)) 1012 goto err; 1013 stream = NULL; 1014 1015 if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written))) 1016 goto err; 1017 1018 ossl_quic_tserver_tick(qtserv); 1019 } 1020 1021 testresult = 1; 1022 err: 1023 BIO_free_all(cbio); 1024 BIO_free_all(strbio); 1025 SSL_free(stream); 1026 ossl_quic_tserver_free(qtserv); 1027 SSL_CTX_free(cctx); 1028 1029 return testresult; 1030 } 1031 1032 #define BACK_PRESSURE_NUM_LOOPS 10000 1033 /* 1034 * Test that sending data from the client to the server faster than the server 1035 * can process it eventually results in back pressure on the client. 1036 */ 1037 static int test_back_pressure(void) 1038 { 1039 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1040 SSL *clientquic = NULL; 1041 QUIC_TSERVER *qtserv = NULL; 1042 int testresult = 0; 1043 unsigned char *msg = NULL; 1044 const size_t msglen = 1024; 1045 unsigned char buf[64]; 1046 size_t readbytes, written; 1047 int i; 1048 1049 if (!TEST_ptr(cctx) 1050 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1051 privkey, 0, &qtserv, 1052 &clientquic, NULL, NULL)) 1053 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1054 goto err; 1055 1056 msg = OPENSSL_malloc(msglen); 1057 if (!TEST_ptr(msg)) 1058 goto err; 1059 if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1)) 1060 goto err; 1061 1062 /* 1063 * Limit to 10000 loops. If we've not seen any back pressure after that 1064 * we're going to run out of memory, so abort. 1065 */ 1066 for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) { 1067 /* Send data from the client */ 1068 if (!SSL_write_ex(clientquic, msg, msglen, &written)) { 1069 /* Check if we are seeing back pressure */ 1070 if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE) 1071 break; 1072 TEST_error("Unexpected client failure"); 1073 goto err; 1074 } 1075 1076 /* Receive data at the server */ 1077 ossl_quic_tserver_tick(qtserv); 1078 if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf), 1079 &readbytes))) 1080 goto err; 1081 } 1082 1083 if (i == BACK_PRESSURE_NUM_LOOPS) { 1084 TEST_error("No back pressure seen"); 1085 goto err; 1086 } 1087 1088 testresult = 1; 1089 err: 1090 SSL_free(clientquic); 1091 ossl_quic_tserver_free(qtserv); 1092 SSL_CTX_free(cctx); 1093 OPENSSL_free(msg); 1094 1095 return testresult; 1096 } 1097 1098 static int dgram_ctr = 0; 1099 1100 static void dgram_cb(int write_p, int version, int content_type, 1101 const void *buf, size_t msglen, SSL *ssl, void *arg) 1102 { 1103 if (!write_p) 1104 return; 1105 1106 if (content_type != SSL3_RT_QUIC_DATAGRAM) 1107 return; 1108 1109 dgram_ctr++; 1110 } 1111 1112 /* Test that we send multiple datagrams in one go when appropriate */ 1113 static int test_multiple_dgrams(void) 1114 { 1115 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1116 SSL *clientquic = NULL; 1117 QUIC_TSERVER *qtserv = NULL; 1118 int testresult = 0; 1119 unsigned char *buf; 1120 const size_t buflen = 1400; 1121 size_t written; 1122 1123 buf = OPENSSL_zalloc(buflen); 1124 1125 if (!TEST_ptr(cctx) 1126 || !TEST_ptr(buf) 1127 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1128 privkey, 0, &qtserv, 1129 &clientquic, NULL, NULL)) 1130 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1131 goto err; 1132 1133 dgram_ctr = 0; 1134 SSL_set_msg_callback(clientquic, dgram_cb); 1135 if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written)) 1136 || !TEST_size_t_eq(written, buflen) 1137 /* We wrote enough data for 2 datagrams */ 1138 || !TEST_int_eq(dgram_ctr, 2)) 1139 goto err; 1140 1141 testresult = 1; 1142 err: 1143 OPENSSL_free(buf); 1144 SSL_free(clientquic); 1145 ossl_quic_tserver_free(qtserv); 1146 SSL_CTX_free(cctx); 1147 1148 return testresult; 1149 } 1150 1151 static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg) 1152 { 1153 int idx = SSL_get_ex_data_X509_STORE_CTX_idx(); 1154 SSL *ssl; 1155 const int *allow = (int *)arg; 1156 1157 /* this should not happen but check anyway */ 1158 if (idx < 0 1159 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL) 1160 return 0; 1161 1162 /* If this is our first attempt then retry */ 1163 if (*allow == 0) 1164 return SSL_set_retry_verify(ssl); 1165 1166 /* Otherwise do nothing - verification succeeds. Continue as normal */ 1167 return 1; 1168 } 1169 1170 /* Test that we can handle a non-io related retry error 1171 * Test 0: Non-blocking 1172 * Test 1: Blocking 1173 */ 1174 static int test_non_io_retry(int idx) 1175 { 1176 SSL_CTX *cctx; 1177 SSL *clientquic = NULL; 1178 QUIC_TSERVER *qtserv = NULL; 1179 int testresult = 0; 1180 int flags = 0, allow = 0; 1181 1182 if (idx >= 1 && !qtest_supports_blocking()) 1183 return TEST_skip("Blocking tests not supported in this build"); 1184 1185 cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1186 if (!TEST_ptr(cctx)) 1187 goto err; 1188 1189 SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &allow); 1190 1191 flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0; 1192 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey, 1193 flags, &qtserv, &clientquic, NULL, 1194 NULL)) 1195 || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic, 1196 SSL_ERROR_WANT_RETRY_VERIFY)) 1197 || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY)) 1198 goto err; 1199 1200 allow = 1; 1201 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1202 goto err; 1203 1204 testresult = 1; 1205 err: 1206 SSL_free(clientquic); 1207 ossl_quic_tserver_free(qtserv); 1208 SSL_CTX_free(cctx); 1209 1210 return testresult; 1211 } 1212 1213 static int use_session_cb_cnt = 0; 1214 static int find_session_cb_cnt = 0; 1215 static const char *pskid = "Identity"; 1216 static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL; 1217 1218 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, 1219 size_t *idlen, SSL_SESSION **sess) 1220 { 1221 use_session_cb_cnt++; 1222 1223 if (clientpsk == NULL || !SSL_SESSION_up_ref(clientpsk)) 1224 return 0; 1225 1226 *sess = clientpsk; 1227 *id = (const unsigned char *)pskid; 1228 *idlen = strlen(pskid); 1229 1230 return 1; 1231 } 1232 1233 static int find_session_cb(SSL *ssl, const unsigned char *identity, 1234 size_t identity_len, SSL_SESSION **sess) 1235 { 1236 find_session_cb_cnt++; 1237 1238 if (serverpsk == NULL || !SSL_SESSION_up_ref(serverpsk)) 1239 return 0; 1240 1241 /* Identity should match that set by the client */ 1242 if (strlen(pskid) != identity_len 1243 || strncmp(pskid, (const char *)identity, identity_len) != 0) { 1244 SSL_SESSION_free(serverpsk); 1245 return 0; 1246 } 1247 1248 *sess = serverpsk; 1249 1250 return 1; 1251 } 1252 1253 static int test_quic_psk(void) 1254 { 1255 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1256 SSL *clientquic = NULL; 1257 QUIC_TSERVER *qtserv = NULL; 1258 int testresult = 0; 1259 1260 if (!TEST_ptr(cctx) 1261 /* No cert or private key for the server, i.e. PSK only */ 1262 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL, 1263 NULL, 0, &qtserv, 1264 &clientquic, NULL, NULL))) 1265 goto end; 1266 1267 SSL_set_psk_use_session_callback(clientquic, use_session_cb); 1268 ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb); 1269 use_session_cb_cnt = 0; 1270 find_session_cb_cnt = 0; 1271 1272 clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH); 1273 /* We already had one ref. Add another one */ 1274 if (!TEST_ptr(clientpsk) || !TEST_true(SSL_SESSION_up_ref(clientpsk))) 1275 goto end; 1276 1277 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)) 1278 || !TEST_int_eq(1, find_session_cb_cnt) 1279 || !TEST_int_eq(1, use_session_cb_cnt) 1280 /* Check that we actually used the PSK */ 1281 || !TEST_true(SSL_session_reused(clientquic))) 1282 goto end; 1283 1284 testresult = 1; 1285 1286 end: 1287 SSL_free(clientquic); 1288 ossl_quic_tserver_free(qtserv); 1289 SSL_CTX_free(cctx); 1290 SSL_SESSION_free(clientpsk); 1291 SSL_SESSION_free(serverpsk); 1292 clientpsk = serverpsk = NULL; 1293 1294 return testresult; 1295 } 1296 1297 static int test_client_auth(int idx) 1298 { 1299 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1300 SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()); 1301 SSL *clientquic = NULL; 1302 QUIC_TSERVER *qtserv = NULL; 1303 int testresult = 0; 1304 unsigned char buf[20]; 1305 static char *msg = "A test message"; 1306 size_t msglen = strlen(msg); 1307 size_t numbytes = 0; 1308 1309 if (!TEST_ptr(cctx) || !TEST_ptr(sctx)) 1310 goto err; 1311 1312 SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE, NULL); 1313 1314 if (!TEST_true(SSL_CTX_load_verify_file(sctx, cauthca))) 1315 goto err; 1316 1317 if (idx > 0 1318 && (!TEST_true(SSL_CTX_use_certificate_chain_file(cctx, ccert)) 1319 || !TEST_true(SSL_CTX_use_PrivateKey_file(cctx, cprivkey, 1320 SSL_FILETYPE_PEM)))) 1321 goto err; 1322 1323 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert, 1324 privkey, 0, &qtserv, 1325 &clientquic, NULL, NULL))) 1326 goto err; 1327 1328 if (idx > 1) { 1329 if (!TEST_true(ssl_ctx_add_large_cert_chain(libctx, cctx, ccert)) 1330 || !TEST_true(ssl_ctx_add_large_cert_chain(libctx, sctx, cert))) 1331 goto err; 1332 } 1333 1334 if (idx == 0) { 1335 if (!TEST_false(qtest_create_quic_connection(qtserv, clientquic))) 1336 goto err; 1337 1338 /* negative test passed */ 1339 testresult = 1; 1340 goto err; 1341 } 1342 1343 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1344 goto err; 1345 1346 /* Check that sending and receiving app data is ok */ 1347 if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes)) 1348 || !TEST_size_t_eq(numbytes, msglen)) 1349 goto err; 1350 1351 ossl_quic_tserver_tick(qtserv); 1352 if (!TEST_true(ossl_quic_tserver_write(qtserv, 0, 1353 (unsigned char *)msg, 1354 msglen, &numbytes))) 1355 goto err; 1356 1357 ossl_quic_tserver_tick(qtserv); 1358 SSL_handle_events(clientquic); 1359 1360 if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)) 1361 || !TEST_size_t_eq(numbytes, msglen) 1362 || !TEST_mem_eq(buf, numbytes, msg, msglen)) 1363 goto err; 1364 1365 if (!TEST_true(qtest_shutdown(qtserv, clientquic))) 1366 goto err; 1367 1368 testresult = 1; 1369 1370 err: 1371 SSL_free(clientquic); 1372 ossl_quic_tserver_free(qtserv); 1373 SSL_CTX_free(sctx); 1374 SSL_CTX_free(cctx); 1375 1376 return testresult; 1377 } 1378 1379 /* 1380 * Test that we correctly handle ALPN supplied by the application 1381 * Test 0: ALPN is provided 1382 * Test 1: No ALPN is provided 1383 */ 1384 static int test_alpn(int idx) 1385 { 1386 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1387 SSL *clientquic = NULL; 1388 QUIC_TSERVER *qtserv = NULL; 1389 int testresult = 0; 1390 int ret; 1391 1392 /* 1393 * Ensure we only configure ciphersuites that are available with both the 1394 * default and fips providers to get the same output in both cases 1395 */ 1396 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256"))) 1397 goto err; 1398 1399 if (!TEST_ptr(cctx) 1400 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1401 privkey, 1402 QTEST_FLAG_FAKE_TIME, 1403 &qtserv, 1404 &clientquic, NULL, NULL))) 1405 goto err; 1406 1407 if (idx == 0) { 1408 /* 1409 * Clear the ALPN we set in qtest_create_quic_objects. We use TEST_false 1410 * because SSL_set_alpn_protos returns 0 for success. 1411 */ 1412 if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0))) 1413 goto err; 1414 } 1415 1416 ret = SSL_connect(clientquic); 1417 if (!TEST_int_le(ret, 0)) 1418 goto err; 1419 if (idx == 0) { 1420 /* We expect an immediate error due to lack of ALPN */ 1421 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL)) 1422 goto err; 1423 } else { 1424 /* ALPN was provided so we expect the connection to succeed */ 1425 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ) 1426 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1427 goto err; 1428 } 1429 1430 testresult = 1; 1431 err: 1432 ossl_quic_tserver_free(qtserv); 1433 SSL_free(clientquic); 1434 SSL_CTX_free(cctx); 1435 1436 return testresult; 1437 } 1438 1439 /* 1440 * Test SSL_get_shutdown() behavior. 1441 */ 1442 static int test_get_shutdown(void) 1443 { 1444 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1445 SSL *clientquic = NULL; 1446 QUIC_TSERVER *qtserv = NULL; 1447 int testresult = 0; 1448 1449 if (!TEST_ptr(cctx) 1450 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1451 privkey, 1452 QTEST_FLAG_FAKE_TIME, 1453 &qtserv, &clientquic, 1454 NULL, NULL)) 1455 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1456 goto err; 1457 1458 if (!TEST_int_eq(SSL_get_shutdown(clientquic), 0)) 1459 goto err; 1460 1461 if (!TEST_int_eq(SSL_shutdown(clientquic), 0)) 1462 goto err; 1463 1464 if (!TEST_int_eq(SSL_get_shutdown(clientquic), SSL_SENT_SHUTDOWN)) 1465 goto err; 1466 1467 do { 1468 ossl_quic_tserver_tick(qtserv); 1469 qtest_add_time(100); 1470 } while (SSL_shutdown(clientquic) == 0); 1471 1472 if (!TEST_int_eq(SSL_get_shutdown(clientquic), 1473 SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN)) 1474 goto err; 1475 1476 testresult = 1; 1477 err: 1478 ossl_quic_tserver_free(qtserv); 1479 SSL_free(clientquic); 1480 SSL_CTX_free(cctx); 1481 1482 return testresult; 1483 } 1484 1485 #define MAX_LOOPS 2000 1486 1487 /* 1488 * Keep retrying SSL_read_ex until it succeeds or we give up. Accept a stream 1489 * if we don't already have one 1490 */ 1491 static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf, 1492 size_t buflen, size_t *readbytes, 1493 QUIC_TSERVER *qtserv) 1494 { 1495 int abortctr; 1496 1497 /* We just do this in a loop with a sleep for simplicity */ 1498 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) { 1499 if (*stream == NULL) { 1500 SSL_handle_events(clientquic); 1501 *stream = SSL_accept_stream(clientquic, 0); 1502 } 1503 1504 if (*stream != NULL) { 1505 if (SSL_read_ex(*stream, buf, buflen, readbytes)) 1506 return 1; 1507 if (!TEST_int_eq(SSL_get_error(*stream, 0), SSL_ERROR_WANT_READ)) 1508 return 0; 1509 } 1510 ossl_quic_tserver_tick(qtserv); 1511 qtest_add_time(1); 1512 qtest_wait_for_timeout(clientquic, qtserv); 1513 } 1514 1515 TEST_error("No progress made"); 1516 return 0; 1517 } 1518 1519 /* Keep retrying ossl_quic_tserver_read until it succeeds or we give up */ 1520 static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid, 1521 void *buf, size_t buflen, size_t *readbytes, 1522 SSL *clientquic) 1523 { 1524 int abortctr; 1525 1526 /* We just do this in a loop with a sleep for simplicity */ 1527 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) { 1528 if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes) 1529 && *readbytes > 1) 1530 return 1; 1531 ossl_quic_tserver_tick(qtserv); 1532 SSL_handle_events(clientquic); 1533 qtest_add_time(1); 1534 qtest_wait_for_timeout(clientquic, qtserv); 1535 } 1536 1537 TEST_error("No progress made"); 1538 return 0; 1539 } 1540 1541 /* 1542 * Create a connection and send data using an unreliable transport. We introduce 1543 * random noise to drop, delay and duplicate datagrams. 1544 * Test 0: Introduce random noise to datagrams 1545 * Test 1: As with test 0 but also split datagrams containing multiple packets 1546 * into individual datagrams so that individual packets can be affected 1547 * by noise - not just a whole datagram. 1548 */ 1549 static int test_noisy_dgram(int idx) 1550 { 1551 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1552 SSL *clientquic = NULL, *stream[2] = { NULL, NULL }; 1553 QUIC_TSERVER *qtserv = NULL; 1554 int testresult = 0; 1555 uint64_t sid = 0; 1556 char *msg = "Hello world!"; 1557 size_t msglen = strlen(msg), written, readbytes, i, j; 1558 unsigned char buf[80]; 1559 int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME; 1560 QTEST_FAULT *fault = NULL; 1561 1562 if (idx == 1) 1563 flags |= QTEST_FLAG_PACKET_SPLIT; 1564 1565 if (!TEST_ptr(cctx) 1566 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1567 privkey, flags, 1568 &qtserv, 1569 &clientquic, &fault, NULL))) 1570 goto err; 1571 1572 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1573 goto err; 1574 1575 if (!TEST_true(SSL_set_incoming_stream_policy(clientquic, 1576 SSL_INCOMING_STREAM_POLICY_ACCEPT, 1577 0)) 1578 || !TEST_true(SSL_set_default_stream_mode(clientquic, 1579 SSL_DEFAULT_STREAM_MODE_NONE))) 1580 goto err; 1581 1582 for (j = 0; j < 2; j++) { 1583 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid))) 1584 goto err; 1585 ossl_quic_tserver_tick(qtserv); 1586 qtest_add_time(1); 1587 1588 /* 1589 * Send data from the server to the client. Some datagrams may get 1590 * lost, modified, dropped or re-ordered. We repeat 20 times to ensure 1591 * we are sending enough datagrams for problems to be noticed. 1592 */ 1593 for (i = 0; i < 20; i++) { 1594 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, 1595 (unsigned char *)msg, msglen, 1596 &written)) 1597 || !TEST_size_t_eq(msglen, written)) 1598 goto err; 1599 ossl_quic_tserver_tick(qtserv); 1600 qtest_add_time(1); 1601 1602 /* 1603 * Since the underlying BIO is now noisy we may get failures that 1604 * need to be retried - so we use unreliable_client_read() to 1605 * handle that 1606 */ 1607 if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf, 1608 sizeof(buf), &readbytes, 1609 qtserv)) 1610 || !TEST_mem_eq(msg, msglen, buf, readbytes)) 1611 goto err; 1612 } 1613 1614 /* Send data from the client to the server */ 1615 for (i = 0; i < 20; i++) { 1616 if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg, 1617 msglen, &written)) 1618 || !TEST_size_t_eq(msglen, written)) 1619 goto err; 1620 1621 ossl_quic_tserver_tick(qtserv); 1622 qtest_add_time(1); 1623 1624 /* 1625 * Since the underlying BIO is now noisy we may get failures that 1626 * need to be retried - so we use unreliable_server_read() to 1627 * handle that 1628 */ 1629 if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf), 1630 &readbytes, clientquic)) 1631 || !TEST_mem_eq(msg, msglen, buf, readbytes)) 1632 goto err; 1633 } 1634 } 1635 1636 testresult = 1; 1637 err: 1638 ossl_quic_tserver_free(qtserv); 1639 SSL_free(stream[0]); 1640 SSL_free(stream[1]); 1641 SSL_free(clientquic); 1642 SSL_CTX_free(cctx); 1643 qtest_fault_free(fault); 1644 1645 return testresult; 1646 } 1647 1648 /* 1649 * Create a connection and send some big data using a transport with limited bandwidth. 1650 */ 1651 1652 #define TEST_TRANSFER_DATA_SIZE (2 * 1024 * 1024) /* 2 MBytes */ 1653 #define TEST_SINGLE_WRITE_SIZE (16 * 1024) /* 16 kBytes */ 1654 #define TEST_BW_LIMIT 1000 /* 1000 Bytes/ms */ 1655 static int test_bw_limit(void) 1656 { 1657 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 1658 SSL *clientquic = NULL; 1659 QUIC_TSERVER *qtserv = NULL; 1660 int testresult = 0; 1661 unsigned char *msg = NULL, *recvbuf = NULL; 1662 size_t sendlen = TEST_TRANSFER_DATA_SIZE; 1663 size_t recvlen = TEST_TRANSFER_DATA_SIZE; 1664 size_t written, readbytes; 1665 int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME; 1666 QTEST_FAULT *fault = NULL; 1667 uint64_t real_bw; 1668 1669 if (!TEST_ptr(cctx) 1670 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 1671 privkey, flags, 1672 &qtserv, 1673 &clientquic, &fault, NULL))) 1674 goto err; 1675 1676 if (!TEST_ptr(msg = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE)) 1677 || !TEST_ptr(recvbuf = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE))) 1678 goto err; 1679 1680 /* Set BW to 1000 Bytes/ms -> 1MByte/s both ways */ 1681 if (!TEST_true(qtest_fault_set_bw_limit(fault, 1000, 1000, 0))) 1682 goto err; 1683 1684 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 1685 goto err; 1686 1687 qtest_start_stopwatch(); 1688 1689 while (recvlen > 0) { 1690 qtest_add_time(1); 1691 1692 if (sendlen > 0) { 1693 if (!SSL_write_ex(clientquic, msg, 1694 sendlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE 1695 : sendlen, 1696 &written)) { 1697 TEST_info("Retrying to send: %llu", (unsigned long long)sendlen); 1698 if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_WANT_WRITE)) 1699 goto err; 1700 } else { 1701 sendlen -= written; 1702 TEST_info("Remaining to send: %llu", (unsigned long long)sendlen); 1703 } 1704 } else { 1705 SSL_handle_events(clientquic); 1706 } 1707 1708 if (ossl_quic_tserver_read(qtserv, 0, recvbuf, 1709 recvlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE 1710 : recvlen, 1711 &readbytes) 1712 && readbytes > 1) { 1713 recvlen -= readbytes; 1714 TEST_info("Remaining to recv: %llu", (unsigned long long)recvlen); 1715 } else { 1716 TEST_info("No progress on recv: %llu", (unsigned long long)recvlen); 1717 } 1718 ossl_quic_tserver_tick(qtserv); 1719 } 1720 real_bw = TEST_TRANSFER_DATA_SIZE / qtest_get_stopwatch_time(); 1721 1722 TEST_info("BW limit: %d Bytes/ms Real bandwidth reached: %llu Bytes/ms", 1723 TEST_BW_LIMIT, (unsigned long long)real_bw); 1724 1725 if (!TEST_uint64_t_lt(real_bw, TEST_BW_LIMIT)) 1726 goto err; 1727 1728 testresult = 1; 1729 err: 1730 OPENSSL_free(msg); 1731 OPENSSL_free(recvbuf); 1732 ossl_quic_tserver_free(qtserv); 1733 SSL_free(clientquic); 1734 SSL_CTX_free(cctx); 1735 qtest_fault_free(fault); 1736 1737 return testresult; 1738 } 1739 1740 enum { 1741 TPARAM_OP_DUP, 1742 TPARAM_OP_DROP, 1743 TPARAM_OP_INJECT, 1744 TPARAM_OP_INJECT_TWICE, 1745 TPARAM_OP_INJECT_RAW, 1746 TPARAM_OP_DROP_INJECT, 1747 TPARAM_OP_MUTATE 1748 }; 1749 1750 #define TPARAM_CHECK_DUP(name, reason) \ 1751 { QUIC_TPARAM_##name, TPARAM_OP_DUP, (reason) }, 1752 #define TPARAM_CHECK_DROP(name, reason) \ 1753 { QUIC_TPARAM_##name, TPARAM_OP_DROP, (reason) }, 1754 #define TPARAM_CHECK_INJECT(name, buf, buf_len, reason) \ 1755 { QUIC_TPARAM_##name, TPARAM_OP_INJECT, (reason), \ 1756 (buf), (buf_len) }, 1757 #define TPARAM_CHECK_INJECT_A(name, buf, reason) \ 1758 TPARAM_CHECK_INJECT(name, buf, sizeof(buf), reason) 1759 #define TPARAM_CHECK_DROP_INJECT(name, buf, buf_len, reason) \ 1760 { QUIC_TPARAM_##name, TPARAM_OP_DROP_INJECT, (reason), \ 1761 (buf), (buf_len) }, 1762 #define TPARAM_CHECK_DROP_INJECT_A(name, buf, reason) \ 1763 TPARAM_CHECK_DROP_INJECT(name, buf, sizeof(buf), reason) 1764 #define TPARAM_CHECK_INJECT_TWICE(name, buf, buf_len, reason) \ 1765 { QUIC_TPARAM_##name, TPARAM_OP_INJECT_TWICE, (reason), \ 1766 (buf), (buf_len) }, 1767 #define TPARAM_CHECK_INJECT_TWICE_A(name, buf, reason) \ 1768 TPARAM_CHECK_INJECT_TWICE(name, buf, sizeof(buf), reason) 1769 #define TPARAM_CHECK_INJECT_RAW(buf, buf_len, reason) \ 1770 { 0, TPARAM_OP_INJECT_RAW, (reason), \ 1771 (buf), (buf_len) }, 1772 #define TPARAM_CHECK_INJECT_RAW_A(buf, reason) \ 1773 TPARAM_CHECK_INJECT_RAW(buf, sizeof(buf), reason) 1774 #define TPARAM_CHECK_MUTATE(name, reason) \ 1775 { QUIC_TPARAM_##name, TPARAM_OP_MUTATE, (reason) }, 1776 #define TPARAM_CHECK_INT(name, reason) \ 1777 TPARAM_CHECK_DROP_INJECT(name, NULL, 0, reason) \ 1778 TPARAM_CHECK_DROP_INJECT_A(name, bogus_int, reason) \ 1779 TPARAM_CHECK_DROP_INJECT_A(name, int_with_trailer, reason) 1780 1781 struct tparam_test { 1782 uint64_t id; 1783 int op; 1784 const char *expect_fail; /* substring to expect in reason */ 1785 const void *buf; 1786 size_t buf_len; 1787 }; 1788 1789 static const unsigned char disable_active_migration_1[] = { 1790 0x00 1791 }; 1792 1793 static const unsigned char malformed_stateless_reset_token_1[] = { 1794 0x02, 0xff 1795 }; 1796 1797 static const unsigned char malformed_stateless_reset_token_2[] = { 1798 0x01 1799 }; 1800 1801 static const unsigned char malformed_stateless_reset_token_3[15] = { 0 }; 1802 1803 static const unsigned char malformed_stateless_reset_token_4[17] = { 0 }; 1804 1805 static const unsigned char malformed_preferred_addr_1[] = { 1806 0x0d, 0xff 1807 }; 1808 1809 static const unsigned char malformed_preferred_addr_2[42] = { 1810 0x0d, 1811 0x28, /* too short */ 1812 }; 1813 1814 static const unsigned char malformed_preferred_addr_3[64] = { 1815 0x0d, 1816 0x3e, /* too long */ 1817 }; 1818 1819 static const unsigned char malformed_preferred_addr_4[] = { 1820 /* TPARAM too short for CID length indicated */ 1821 0x0d, 1822 0x29, 1823 0x00, 1824 0x00, 1825 0x00, 1826 0x00, 1827 0x00, 1828 0x00, 1829 0x00, 1830 0x00, 1831 0x00, 1832 0x00, 1833 0x00, 1834 0x00, 1835 0x00, 1836 0x00, 1837 0x00, 1838 0x00, 1839 0x00, 1840 0x00, 1841 0x00, 1842 0x00, 1843 0x00, 1844 0x00, 1845 0x00, 1846 0x00, 1847 0x01, 1848 0x55, 1849 0x00, 1850 0x00, 1851 0x00, 1852 0x00, 1853 0x00, 1854 0x00, 1855 0x00, 1856 0x00, 1857 0x00, 1858 0x00, 1859 0x00, 1860 0x00, 1861 0x00, 1862 0x00, 1863 0x00, 1864 0x00, 1865 }; 1866 1867 static const unsigned char malformed_unknown_1[] = { 1868 0xff 1869 }; 1870 1871 static const unsigned char malformed_unknown_2[] = { 1872 0x55, 1873 0x55, 1874 }; 1875 1876 static const unsigned char malformed_unknown_3[] = { 1877 0x55, 1878 0x55, 1879 0x01, 1880 }; 1881 1882 static const unsigned char ack_delay_exp[] = { 1883 0x03 1884 }; 1885 1886 static const unsigned char stateless_reset_token[16] = { 0x42 }; 1887 1888 static const unsigned char preferred_addr[] = { 1889 0x44, 1890 0x44, 1891 0x44, 1892 0x44, 1893 0x55, 1894 0x55, 1895 0x66, 1896 0x66, 1897 0x66, 1898 0x66, 1899 0x66, 1900 0x66, 1901 0x66, 1902 0x66, 1903 0x66, 1904 0x66, 1905 0x66, 1906 0x66, 1907 0x66, 1908 0x66, 1909 0x66, 1910 0x66, 1911 0x77, 1912 0x77, 1913 0x02, 1914 0xAA, 1915 0xBB, 1916 0x99, 1917 0x99, 1918 0x99, 1919 0x99, 1920 0x99, 1921 0x99, 1922 0x99, 1923 0x99, 1924 0x99, 1925 0x99, 1926 0x99, 1927 0x99, 1928 0x99, 1929 0x99, 1930 0x99, 1931 0x99, 1932 }; 1933 1934 static const unsigned char long_cid[21] = { 0x42 }; 1935 1936 static const unsigned char excess_ack_delay_exp[] = { 1937 0x15, 1938 }; 1939 1940 static const unsigned char excess_max_ack_delay[] = { 1941 0xC0, 1942 0x00, 1943 0x00, 1944 0x00, 1945 0x00, 1946 0x00, 1947 0x40, 1948 0x00, 1949 }; 1950 1951 static const unsigned char excess_initial_max_streams[] = { 1952 0xD0, 1953 0x00, 1954 0x00, 1955 0x00, 1956 0x00, 1957 0x00, 1958 0x00, 1959 0x01, 1960 }; 1961 1962 static const unsigned char undersize_udp_payload_size[] = { 1963 0xC0, 1964 0x00, 1965 0x00, 1966 0x00, 1967 0x00, 1968 0x00, 1969 0x04, 1970 0xaf, 1971 }; 1972 1973 static const unsigned char undersize_active_conn_id_limit[] = { 1974 0xC0, 1975 0x00, 1976 0x00, 1977 0x00, 1978 0x00, 1979 0x00, 1980 0x00, 1981 0x01, 1982 }; 1983 1984 static const unsigned char bogus_int[9] = { 0 }; 1985 1986 static const unsigned char int_with_trailer[2] = { 0x01 }; 1987 1988 #define QUIC_TPARAM_UNKNOWN_1 0xf1f1 1989 1990 static const struct tparam_test tparam_tests[] = { 1991 TPARAM_CHECK_DUP(ORIG_DCID, 1992 "ORIG_DCID appears multiple times") 1993 TPARAM_CHECK_DUP(INITIAL_SCID, 1994 "INITIAL_SCID appears multiple times") 1995 TPARAM_CHECK_DUP(INITIAL_MAX_DATA, 1996 "INITIAL_MAX_DATA appears multiple times") 1997 TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, 1998 "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL appears multiple times") 1999 TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, 2000 "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE appears multiple times") 2001 TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_UNI, 2002 "INITIAL_MAX_STREAM_DATA_UNI appears multiple times") 2003 TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_BIDI, 2004 "INITIAL_MAX_STREAMS_BIDI appears multiple times") 2005 TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_UNI, 2006 "INITIAL_MAX_STREAMS_UNI appears multiple times") 2007 TPARAM_CHECK_DUP(MAX_IDLE_TIMEOUT, 2008 "MAX_IDLE_TIMEOUT appears multiple times") 2009 TPARAM_CHECK_DUP(MAX_UDP_PAYLOAD_SIZE, 2010 "MAX_UDP_PAYLOAD_SIZE appears multiple times") 2011 TPARAM_CHECK_DUP(ACTIVE_CONN_ID_LIMIT, 2012 "ACTIVE_CONN_ID_LIMIT appears multiple times") 2013 TPARAM_CHECK_DUP(DISABLE_ACTIVE_MIGRATION, 2014 "DISABLE_ACTIVE_MIGRATION appears multiple times") 2015 2016 TPARAM_CHECK_DROP(INITIAL_SCID, 2017 "INITIAL_SCID was not sent but is required") 2018 TPARAM_CHECK_DROP(ORIG_DCID, 2019 "ORIG_DCID was not sent but is required") 2020 2021 TPARAM_CHECK_DROP_INJECT_A(DISABLE_ACTIVE_MIGRATION, disable_active_migration_1, 2022 "DISABLE_ACTIVE_MIGRATION is malformed") 2023 TPARAM_CHECK_INJECT(UNKNOWN_1, NULL, 0, 2024 NULL) 2025 TPARAM_CHECK_INJECT_RAW_A(malformed_stateless_reset_token_1, 2026 "STATELESS_RESET_TOKEN is malformed") 2027 TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, 2028 malformed_stateless_reset_token_2, 2029 "STATELESS_RESET_TOKEN is malformed") 2030 TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, 2031 malformed_stateless_reset_token_3, 2032 "STATELESS_RESET_TOKEN is malformed") 2033 TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN, 2034 malformed_stateless_reset_token_4, 2035 "STATELESS_RESET_TOKEN is malformed") 2036 TPARAM_CHECK_INJECT(STATELESS_RESET_TOKEN, 2037 NULL, 0, 2038 "STATELESS_RESET_TOKEN is malformed") 2039 TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_1, 2040 "PREFERRED_ADDR is malformed") 2041 TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_2, 2042 "PREFERRED_ADDR is malformed") 2043 TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_3, 2044 "PREFERRED_ADDR is malformed") 2045 TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_4, 2046 "PREFERRED_ADDR is malformed") 2047 TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_1, 2048 "bad transport parameter") 2049 TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_2, 2050 "bad transport parameter") 2051 TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_3, 2052 "bad transport parameter") 2053 2054 TPARAM_CHECK_INJECT_A(ACK_DELAY_EXP, excess_ack_delay_exp, 2055 "ACK_DELAY_EXP is malformed") 2056 TPARAM_CHECK_INJECT_A(MAX_ACK_DELAY, excess_max_ack_delay, 2057 "MAX_ACK_DELAY is malformed") 2058 TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_BIDI, excess_initial_max_streams, 2059 "INITIAL_MAX_STREAMS_BIDI is malformed") 2060 TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_UNI, excess_initial_max_streams, 2061 "INITIAL_MAX_STREAMS_UNI is malformed") 2062 2063 TPARAM_CHECK_DROP_INJECT_A(MAX_UDP_PAYLOAD_SIZE, undersize_udp_payload_size, 2064 "MAX_UDP_PAYLOAD_SIZE is malformed") 2065 TPARAM_CHECK_DROP_INJECT_A(ACTIVE_CONN_ID_LIMIT, undersize_active_conn_id_limit, 2066 "ACTIVE_CONN_ID_LIMIT is malformed") 2067 2068 TPARAM_CHECK_INJECT_TWICE_A(ACK_DELAY_EXP, ack_delay_exp, 2069 "ACK_DELAY_EXP appears multiple times") 2070 TPARAM_CHECK_INJECT_TWICE_A(MAX_ACK_DELAY, ack_delay_exp, 2071 "MAX_ACK_DELAY appears multiple times") 2072 TPARAM_CHECK_INJECT_TWICE_A(STATELESS_RESET_TOKEN, stateless_reset_token, 2073 "STATELESS_RESET_TOKEN appears multiple times") 2074 TPARAM_CHECK_INJECT_TWICE_A(PREFERRED_ADDR, preferred_addr, 2075 "PREFERRED_ADDR appears multiple times") 2076 2077 TPARAM_CHECK_MUTATE(ORIG_DCID, 2078 "ORIG_DCID does not match expected value") 2079 TPARAM_CHECK_MUTATE(INITIAL_SCID, 2080 "INITIAL_SCID does not match expected value") 2081 2082 TPARAM_CHECK_DROP_INJECT_A(ORIG_DCID, long_cid, 2083 "ORIG_DCID is malformed") 2084 TPARAM_CHECK_DROP_INJECT_A(INITIAL_SCID, long_cid, 2085 "INITIAL_SCID is malformed") 2086 2087 TPARAM_CHECK_INT(INITIAL_MAX_DATA, 2088 "INITIAL_MAX_DATA is malformed") 2089 TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL, 2090 "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL is malformed") 2091 TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE, 2092 "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE is malformed") 2093 TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_UNI, 2094 "INITIAL_MAX_STREAM_DATA_UNI is malformed") 2095 TPARAM_CHECK_INT(ACK_DELAY_EXP, 2096 "ACK_DELAY_EXP is malformed") 2097 TPARAM_CHECK_INT(MAX_ACK_DELAY, 2098 "MAX_ACK_DELAY is malformed") 2099 TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_BIDI, 2100 "INITIAL_MAX_STREAMS_BIDI is malformed") 2101 TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_UNI, 2102 "INITIAL_MAX_STREAMS_UNI is malformed") 2103 TPARAM_CHECK_INT(MAX_IDLE_TIMEOUT, 2104 "MAX_IDLE_TIMEOUT is malformed") 2105 TPARAM_CHECK_INT(MAX_UDP_PAYLOAD_SIZE, 2106 "MAX_UDP_PAYLOAD_SIZE is malformed") 2107 TPARAM_CHECK_INT(ACTIVE_CONN_ID_LIMIT, 2108 "ACTIVE_CONN_ID_LIMIT is malformed") 2109 }; 2110 2111 struct tparam_ctx { 2112 const struct tparam_test *t; 2113 }; 2114 2115 static int tparam_handle(struct tparam_ctx *ctx, 2116 uint64_t id, unsigned char *data, 2117 size_t data_len, 2118 WPACKET *wpkt) 2119 { 2120 const struct tparam_test *t = ctx->t; 2121 2122 switch (t->op) { 2123 case TPARAM_OP_DUP: 2124 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, 2125 data, data_len))) 2126 return 0; 2127 2128 /* 2129 * If this is the matching ID, write it again, duplicating the TPARAM. 2130 */ 2131 if (id == t->id 2132 && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, 2133 data, data_len))) 2134 return 0; 2135 2136 return 1; 2137 2138 case TPARAM_OP_DROP: 2139 case TPARAM_OP_DROP_INJECT: 2140 /* Pass through unless ID matches. */ 2141 if (id != t->id 2142 && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, 2143 data, data_len))) 2144 return 0; 2145 2146 return 1; 2147 2148 case TPARAM_OP_INJECT: 2149 case TPARAM_OP_INJECT_TWICE: 2150 case TPARAM_OP_INJECT_RAW: 2151 /* Always pass through. */ 2152 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, 2153 data, data_len))) 2154 return 0; 2155 2156 return 1; 2157 2158 case TPARAM_OP_MUTATE: 2159 if (id == t->id) { 2160 if (!TEST_size_t_gt(data_len, 0)) 2161 return 0; 2162 2163 data[0] ^= 1; 2164 } 2165 2166 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id, 2167 data, data_len))) 2168 return 0; 2169 2170 if (id == t->id) 2171 data[0] ^= 1; 2172 2173 return 1; 2174 2175 default: 2176 return 0; 2177 } 2178 } 2179 2180 static int tparam_on_enc_ext(QTEST_FAULT *qtf, QTEST_ENCRYPTED_EXTENSIONS *ee, 2181 size_t ee_len, void *arg) 2182 { 2183 int rc = 0; 2184 struct tparam_ctx *ctx = arg; 2185 PACKET pkt = { 0 }; 2186 WPACKET wpkt; 2187 int have_wpkt = 0; 2188 BUF_MEM *old_bufm = NULL, *new_bufm = NULL; 2189 unsigned char *tp_p; 2190 size_t tp_len, written, old_len, eb_len; 2191 uint64_t id; 2192 2193 if (!TEST_ptr(old_bufm = BUF_MEM_new())) 2194 goto err; 2195 2196 /* 2197 * Delete transport parameters TLS extension and capture the contents of the 2198 * extension which was removed. 2199 */ 2200 if (!TEST_true(qtest_fault_delete_extension(qtf, TLSEXT_TYPE_quic_transport_parameters, 2201 ee->extensions, &ee->extensionslen, 2202 old_bufm))) 2203 goto err; 2204 2205 if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char *)old_bufm->data, old_bufm->length)) 2206 || !TEST_ptr(new_bufm = BUF_MEM_new()) 2207 || !TEST_true(WPACKET_init(&wpkt, new_bufm))) 2208 goto err; 2209 2210 have_wpkt = 1; 2211 2212 /* 2213 * Open transport parameters TLS extension: 2214 * 2215 * u16 Extension ID (quic_transport_parameters) 2216 * u16 Extension Data Length 2217 * ... Extension Data 2218 * 2219 */ 2220 if (!TEST_true(WPACKET_put_bytes_u16(&wpkt, 2221 TLSEXT_TYPE_quic_transport_parameters)) 2222 || !TEST_true(WPACKET_start_sub_packet_u16(&wpkt))) 2223 goto err; 2224 2225 for (; PACKET_remaining(&pkt) > 0;) { 2226 tp_p = (unsigned char *)ossl_quic_wire_decode_transport_param_bytes(&pkt, 2227 &id, 2228 &tp_len); 2229 if (!TEST_ptr(tp_p)) { 2230 TEST_mem_eq(PACKET_data(&pkt), PACKET_remaining(&pkt), NULL, 0); 2231 goto err; 2232 } 2233 2234 if (!TEST_true(tparam_handle(ctx, id, tp_p, tp_len, &wpkt))) 2235 goto err; 2236 } 2237 2238 if (ctx->t->op == TPARAM_OP_INJECT || ctx->t->op == TPARAM_OP_DROP_INJECT 2239 || ctx->t->op == TPARAM_OP_INJECT_TWICE) { 2240 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id, 2241 ctx->t->buf, 2242 ctx->t->buf_len))) 2243 goto err; 2244 2245 if (ctx->t->op == TPARAM_OP_INJECT_TWICE 2246 && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id, 2247 ctx->t->buf, 2248 ctx->t->buf_len))) 2249 goto err; 2250 } else if (ctx->t->op == TPARAM_OP_INJECT_RAW) { 2251 if (!TEST_true(WPACKET_memcpy(&wpkt, ctx->t->buf, ctx->t->buf_len))) 2252 goto err; 2253 } 2254 2255 if (!TEST_true(WPACKET_close(&wpkt))) /* end extension data, set length */ 2256 goto err; 2257 2258 if (!TEST_true(WPACKET_get_total_written(&wpkt, &written))) 2259 goto err; 2260 2261 WPACKET_finish(&wpkt); 2262 have_wpkt = 0; 2263 2264 /* 2265 * Append the constructed extension blob to the extension block. 2266 */ 2267 old_len = ee->extensionslen; 2268 2269 if (!qtest_fault_resize_message(qtf, ee->extensionslen + written)) 2270 goto err; 2271 2272 memcpy(ee->extensions + old_len, new_bufm->data, written); 2273 2274 /* Fixup the extension block header (u16 length of entire block). */ 2275 eb_len = (((uint16_t)ee->extensions[0]) << 8) + (uint16_t)ee->extensions[1]; 2276 eb_len += written; 2277 ee->extensions[0] = (unsigned char)((eb_len >> 8) & 0xFF); 2278 ee->extensions[1] = (unsigned char)(eb_len & 0xFF); 2279 2280 rc = 1; 2281 err: 2282 if (have_wpkt) 2283 WPACKET_cleanup(&wpkt); 2284 BUF_MEM_free(old_bufm); 2285 BUF_MEM_free(new_bufm); 2286 return rc; 2287 } 2288 2289 static int test_tparam(int idx) 2290 { 2291 int testresult = 0; 2292 SSL_CTX *c_ctx = NULL; 2293 SSL *c_ssl = NULL; 2294 QUIC_TSERVER *s = NULL; 2295 QTEST_FAULT *qtf = NULL; 2296 struct tparam_ctx ctx = { 0 }; 2297 2298 ctx.t = &tparam_tests[idx]; 2299 2300 if (!TEST_ptr(c_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) 2301 goto err; 2302 2303 if (!TEST_true(qtest_create_quic_objects(libctx, c_ctx, NULL, cert, 2304 privkey, 0, &s, 2305 &c_ssl, &qtf, NULL))) 2306 goto err; 2307 2308 if (!TEST_true(qtest_fault_set_hand_enc_ext_listener(qtf, tparam_on_enc_ext, 2309 &ctx))) 2310 goto err; 2311 2312 if (!TEST_true(qtest_create_quic_connection_ex(s, c_ssl, 2313 ctx.t->expect_fail != NULL))) 2314 goto err; 2315 2316 if (ctx.t->expect_fail != NULL) { 2317 SSL_CONN_CLOSE_INFO info = { 0 }; 2318 2319 if (!TEST_true(SSL_get_conn_close_info(c_ssl, &info, sizeof(info)))) 2320 goto err; 2321 2322 if (!TEST_true((info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0) 2323 || !TEST_uint64_t_eq(info.error_code, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR) 2324 || !TEST_ptr(strstr(info.reason, ctx.t->expect_fail))) { 2325 TEST_error("expected connection closure information mismatch" 2326 " during TPARAM test: flags=%llu ec=%llu reason='%s'", 2327 (unsigned long long)info.flags, 2328 (unsigned long long)info.error_code, 2329 info.reason); 2330 goto err; 2331 } 2332 } 2333 2334 testresult = 1; 2335 err: 2336 if (!testresult) { 2337 if (ctx.t->expect_fail != NULL) 2338 TEST_info("failed during test for id=%llu, op=%d, bl=%zu, " 2339 "expected failure='%s'", 2340 (unsigned long long)ctx.t->id, 2341 ctx.t->op, ctx.t->buf_len, ctx.t->expect_fail); 2342 else 2343 TEST_info("failed during test for id=%llu, op=%d, bl=%zu", 2344 (unsigned long long)ctx.t->id, ctx.t->op, ctx.t->buf_len); 2345 } 2346 2347 ossl_quic_tserver_free(s); 2348 SSL_free(c_ssl); 2349 SSL_CTX_free(c_ctx); 2350 qtest_fault_free(qtf); 2351 return testresult; 2352 } 2353 2354 static int new_called = 0; 2355 static SSL *cbssl = NULL; 2356 2357 static int new_session_cb(SSL *ssl, SSL_SESSION *sess) 2358 { 2359 new_called++; 2360 /* 2361 * Remember the SSL ref we were called with. No need to up-ref this. It 2362 * should remain valid for the duration of the test. 2363 */ 2364 cbssl = ssl; 2365 /* 2366 * sess has been up-refed for us, but we don't actually need it so free it 2367 * immediately. 2368 */ 2369 SSL_SESSION_free(sess); 2370 return 1; 2371 } 2372 2373 /* Test using a new_session_cb with a QUIC SSL object works as expected */ 2374 static int test_session_cb(void) 2375 { 2376 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 2377 SSL *clientquic = NULL; 2378 QUIC_TSERVER *qtserv = NULL; 2379 int testresult = 0; 2380 2381 if (!TEST_ptr(cctx)) 2382 goto err; 2383 2384 new_called = 0; 2385 cbssl = NULL; 2386 SSL_CTX_sess_set_new_cb(cctx, new_session_cb); 2387 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT); 2388 2389 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 2390 privkey, 2391 QTEST_FLAG_FAKE_TIME, 2392 &qtserv, &clientquic, 2393 NULL, NULL))) 2394 goto err; 2395 2396 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 2397 goto err; 2398 2399 /* Process the pending NewSessionTickets */ 2400 if (!TEST_true(SSL_handle_events(clientquic))) 2401 goto err; 2402 2403 if (!TEST_int_eq(SSL_shutdown(clientquic), 0)) 2404 goto err; 2405 2406 /* 2407 * Check the callback was called twice (we expect 2 tickets), and with the 2408 * correct SSL reference 2409 */ 2410 if (!TEST_int_eq(new_called, 2) 2411 || !TEST_ptr_eq(clientquic, cbssl)) 2412 goto err; 2413 2414 testresult = 1; 2415 err: 2416 cbssl = NULL; 2417 ossl_quic_tserver_free(qtserv); 2418 SSL_free(clientquic); 2419 SSL_CTX_free(cctx); 2420 2421 return testresult; 2422 } 2423 2424 static int test_domain_flags(void) 2425 { 2426 int testresult = 0; 2427 SSL_CTX *ctx = NULL; 2428 SSL *domain = NULL, *listener = NULL, *other_conn = NULL; 2429 uint64_t domain_flags = 0; 2430 2431 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())) 2432 || !TEST_true(SSL_CTX_get_domain_flags(ctx, &domain_flags)) 2433 || !TEST_uint64_t_ne(domain_flags, 0) 2434 || !TEST_uint64_t_ne(domain_flags & (SSL_DOMAIN_FLAG_SINGLE_THREAD | SSL_DOMAIN_FLAG_MULTI_THREAD), 0) 2435 || !TEST_uint64_t_ne(domain_flags & SSL_DOMAIN_FLAG_LEGACY_BLOCKING, 0) 2436 || !TEST_true(SSL_CTX_set_domain_flags(ctx, SSL_DOMAIN_FLAG_SINGLE_THREAD)) 2437 || !TEST_true(SSL_CTX_get_domain_flags(ctx, &domain_flags)) 2438 || !TEST_uint64_t_eq(domain_flags, SSL_DOMAIN_FLAG_SINGLE_THREAD) 2439 || !TEST_ptr(domain = SSL_new_domain(ctx, 0)) 2440 || !TEST_true(SSL_get_domain_flags(domain, &domain_flags)) 2441 || !TEST_uint64_t_eq(domain_flags, SSL_DOMAIN_FLAG_SINGLE_THREAD) 2442 || !TEST_true(other_conn = SSL_new(ctx)) 2443 || !TEST_true(SSL_get_domain_flags(other_conn, &domain_flags)) 2444 || !TEST_uint64_t_eq(domain_flags, SSL_DOMAIN_FLAG_SINGLE_THREAD) 2445 || !TEST_true(SSL_is_domain(domain)) 2446 || !TEST_false(SSL_is_domain(other_conn)) 2447 || !TEST_ptr_eq(SSL_get0_domain(domain), domain) 2448 || !TEST_ptr_null(SSL_get0_domain(other_conn)) 2449 || !TEST_ptr(listener = SSL_new_listener_from(domain, 0)) 2450 || !TEST_true(SSL_is_listener(listener)) 2451 || !TEST_false(SSL_is_domain(listener)) 2452 || !TEST_ptr_eq(SSL_get0_domain(listener), domain) 2453 || !TEST_ptr_eq(SSL_get0_listener(listener), listener)) 2454 goto err; 2455 2456 testresult = 1; 2457 err: 2458 SSL_free(domain); 2459 SSL_free(listener); 2460 SSL_free(other_conn); 2461 SSL_CTX_free(ctx); 2462 return testresult; 2463 } 2464 2465 /* 2466 * Test that calling SSL_handle_events() early behaves as expected 2467 */ 2468 static int test_early_ticks(void) 2469 { 2470 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()); 2471 SSL *clientquic = NULL; 2472 QUIC_TSERVER *qtserv = NULL; 2473 int testresult = 0; 2474 struct timeval tv; 2475 int inf = 0; 2476 2477 if (!TEST_ptr(cctx) 2478 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, 2479 privkey, QTEST_FLAG_FAKE_TIME, 2480 &qtserv, 2481 &clientquic, NULL, NULL))) 2482 goto err; 2483 2484 if (!TEST_true(SSL_in_before(clientquic))) 2485 goto err; 2486 2487 if (!TEST_true(SSL_handle_events(clientquic))) 2488 goto err; 2489 2490 if (!TEST_true(SSL_get_event_timeout(clientquic, &tv, &inf)) 2491 || !TEST_true(inf)) 2492 goto err; 2493 2494 if (!TEST_false(SSL_has_pending(clientquic)) 2495 || !TEST_int_eq(SSL_pending(clientquic), 0)) 2496 goto err; 2497 2498 if (!TEST_true(SSL_in_before(clientquic))) 2499 goto err; 2500 2501 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))) 2502 goto err; 2503 2504 if (!TEST_false(SSL_in_before(clientquic))) 2505 goto err; 2506 2507 testresult = 1; 2508 err: 2509 SSL_free(clientquic); 2510 SSL_CTX_free(cctx); 2511 ossl_quic_tserver_free(qtserv); 2512 return testresult; 2513 } 2514 2515 static int select_alpn(SSL *ssl, const unsigned char **out, 2516 unsigned char *out_len, const unsigned char *in, 2517 unsigned int in_len, void *arg) 2518 { 2519 static unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' }; 2520 2521 if (SSL_select_next_proto((unsigned char **)out, out_len, alpn, sizeof(alpn), 2522 in, in_len) 2523 == OPENSSL_NPN_NEGOTIATED) 2524 return SSL_TLSEXT_ERR_OK; 2525 return SSL_TLSEXT_ERR_ALERT_FATAL; 2526 } 2527 2528 static SSL_CTX *create_client_ctx(void) 2529 { 2530 SSL_CTX *ssl_ctx; 2531 2532 if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))) { 2533 SSL_CTX_free(ssl_ctx); 2534 ssl_ctx = NULL; 2535 } 2536 2537 return ssl_ctx; 2538 } 2539 2540 static SSL_CTX *create_server_ctx(void) 2541 { 2542 SSL_CTX *ssl_ctx; 2543 2544 if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_server_method())) 2545 || !TEST_true(SSL_CTX_use_certificate_file(ssl_ctx, cert, SSL_FILETYPE_PEM)) 2546 || !TEST_true(SSL_CTX_use_PrivateKey_file(ssl_ctx, privkey, SSL_FILETYPE_PEM))) { 2547 SSL_CTX_free(ssl_ctx); 2548 ssl_ctx = NULL; 2549 } else { 2550 SSL_CTX_set_alpn_select_cb(ssl_ctx, select_alpn, NULL); 2551 SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, NULL); 2552 } 2553 2554 return ssl_ctx; 2555 } 2556 2557 static BIO_ADDR *create_addr(struct in_addr *ina, short int port) 2558 { 2559 BIO_ADDR *addr = NULL; 2560 2561 if (!TEST_ptr(addr = BIO_ADDR_new())) 2562 return NULL; 2563 2564 if (!TEST_true(BIO_ADDR_rawmake(addr, AF_INET, ina, sizeof(struct in_addr), 2565 htons(port)))) { 2566 BIO_ADDR_free(addr); 2567 return NULL; 2568 } 2569 2570 return addr; 2571 } 2572 2573 static int bio_addr_bind(BIO *bio, BIO_ADDR *addr) 2574 { 2575 int bio_caps = BIO_DGRAM_CAP_HANDLES_DST_ADDR | BIO_DGRAM_CAP_HANDLES_SRC_ADDR; 2576 2577 if (!TEST_true(BIO_dgram_set_caps(bio, bio_caps))) 2578 return 0; 2579 2580 if (!TEST_int_eq(BIO_dgram_set0_local_addr(bio, addr), 1)) 2581 return 0; 2582 2583 return 1; 2584 } 2585 2586 static SSL *ql_create(SSL_CTX *ssl_ctx, BIO *bio) 2587 { 2588 SSL *qserver; 2589 2590 if (!TEST_ptr(qserver = SSL_new_listener(ssl_ctx, 0))) { 2591 BIO_free(bio); 2592 return NULL; 2593 } 2594 2595 SSL_set_bio(qserver, bio, bio); 2596 2597 if (!TEST_true(SSL_listen(qserver))) { 2598 SSL_free(qserver); 2599 return NULL; 2600 } 2601 2602 return qserver; 2603 } 2604 2605 static int qc_init(SSL *qconn, BIO_ADDR *dst_addr) 2606 { 2607 static unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' }; 2608 2609 if (!TEST_true(SSL_set1_initial_peer_addr(qconn, dst_addr))) 2610 return 0; 2611 2612 if (!TEST_false(SSL_set_alpn_protos(qconn, alpn, sizeof(alpn)))) 2613 return 0; 2614 2615 return 1; 2616 } 2617 2618 static int test_ssl_new_from_listener(void) 2619 { 2620 SSL_CTX *lctx = NULL, *sctx = NULL; 2621 SSL *qlistener = NULL, *qserver = NULL, *qconn = 0; 2622 int testresult = 0; 2623 int chk; 2624 BIO *lbio = NULL, *sbio = NULL; 2625 BIO_ADDR *addr = NULL; 2626 struct in_addr ina; 2627 2628 ina.s_addr = htonl(0x1f000001); 2629 if (!TEST_ptr(lctx = create_server_ctx()) 2630 || !TEST_ptr(sctx = create_server_ctx()) 2631 || !TEST_true(BIO_new_bio_dgram_pair(&lbio, 0, &sbio, 0))) 2632 goto err; 2633 2634 if (!TEST_ptr(addr = create_addr(&ina, 8040))) 2635 goto err; 2636 2637 if (!TEST_true(bio_addr_bind(lbio, addr))) 2638 goto err; 2639 addr = NULL; 2640 2641 if (!TEST_ptr(addr = create_addr(&ina, 4080))) 2642 goto err; 2643 2644 if (!TEST_true(bio_addr_bind(sbio, addr))) 2645 goto err; 2646 addr = NULL; 2647 2648 qlistener = ql_create(lctx, lbio); 2649 lbio = NULL; 2650 if (!TEST_ptr(qlistener)) 2651 goto err; 2652 2653 qserver = ql_create(sctx, sbio); 2654 sbio = NULL; 2655 if (!TEST_ptr(qserver)) 2656 goto err; 2657 2658 if (!TEST_ptr(qconn = SSL_new_from_listener(qlistener, 0))) 2659 goto err; 2660 2661 if (!TEST_ptr(addr = create_addr(&ina, 4080))) 2662 goto err; 2663 2664 chk = qc_init(qconn, addr); 2665 if (!TEST_true(chk)) 2666 goto err; 2667 2668 while ((chk = SSL_do_handshake(qconn)) == -1) { 2669 SSL_handle_events(qserver); 2670 SSL_handle_events(qlistener); 2671 } 2672 2673 if (!TEST_int_gt(chk, 0)) { 2674 TEST_info("SSL_do_handshake() failed\n"); 2675 goto err; 2676 } 2677 2678 testresult = 1; 2679 err: 2680 SSL_free(qconn); 2681 SSL_free(qlistener); 2682 SSL_free(qserver); 2683 BIO_free(lbio); 2684 BIO_free(sbio); 2685 SSL_CTX_free(sctx); 2686 SSL_CTX_free(lctx); 2687 BIO_ADDR_free(addr); 2688 2689 return testresult; 2690 } 2691 2692 static int test_server_method_with_ssl_new(void) 2693 { 2694 SSL_CTX *ctx = NULL; 2695 SSL *ssl = NULL; 2696 int ret = 0; 2697 unsigned long err; 2698 2699 /* Create a new SSL_CTX using the QUIC server method */ 2700 ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_server_method()); 2701 if (!TEST_ptr(ctx)) 2702 goto end; 2703 2704 /* Try to create a new SSL object - this should fail */ 2705 ssl = SSL_new(ctx); 2706 2707 /* Check that SSL_new() returned NULL */ 2708 if (!TEST_ptr_null(ssl)) 2709 goto end; 2710 2711 /* Check for the expected error */ 2712 err = ERR_peek_error(); 2713 if (!TEST_true(ERR_GET_LIB(err) == ERR_LIB_SSL && ERR_GET_REASON(err) == ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED)) 2714 goto end; 2715 2716 ret = 1; 2717 2718 end: 2719 SSL_free(ssl); 2720 SSL_CTX_free(ctx); 2721 return ret; 2722 } 2723 2724 static int create_quic_ssl_objects(SSL_CTX *sctx, SSL_CTX *cctx, 2725 SSL **lssl, SSL **cssl) 2726 { 2727 BIO_ADDR *addr = NULL; 2728 struct in_addr ina; 2729 BIO *cbio = NULL, *sbio = NULL; 2730 int ret = 0; 2731 2732 *cssl = *lssl = NULL; 2733 ina.s_addr = htonl(0x1f000001); 2734 2735 if (!TEST_true(BIO_new_bio_dgram_pair(&cbio, 0, &sbio, 0))) 2736 goto err; 2737 2738 if (!TEST_ptr(addr = create_addr(&ina, 8040))) 2739 goto err; 2740 2741 if (!TEST_true(bio_addr_bind(sbio, addr))) 2742 goto err; 2743 addr = NULL; 2744 2745 *lssl = ql_create(sctx, sbio); 2746 sbio = NULL; 2747 if (!TEST_ptr(*lssl)) 2748 goto err; 2749 2750 if (!TEST_ptr(*cssl = SSL_new(cctx))) 2751 goto err; 2752 2753 if (!TEST_ptr(addr = create_addr(&ina, 8040))) 2754 goto err; 2755 if (!TEST_true(bio_addr_bind(cbio, addr))) 2756 goto err; 2757 2758 if (!TEST_true(qc_init(*cssl, addr))) { 2759 addr = NULL; 2760 goto err; 2761 } 2762 addr = NULL; 2763 SSL_set_bio(*cssl, cbio, cbio); 2764 cbio = NULL; 2765 2766 ret = 1; 2767 2768 err: 2769 if (!ret) { 2770 SSL_free(*cssl); 2771 SSL_free(*lssl); 2772 *cssl = *lssl = NULL; 2773 } 2774 BIO_free(cbio); 2775 BIO_free(sbio); 2776 BIO_ADDR_free(addr); 2777 2778 return ret; 2779 } 2780 2781 static int test_ssl_accept_connection(void) 2782 { 2783 SSL_CTX *cctx = NULL, *sctx = NULL; 2784 SSL *clientssl = NULL, *serverssl = NULL, *qlistener = NULL; 2785 int testresult = 0; 2786 int ret, i; 2787 2788 if (!TEST_ptr(sctx = create_server_ctx()) 2789 || !TEST_ptr(cctx = create_client_ctx())) 2790 goto err; 2791 2792 if (!create_quic_ssl_objects(sctx, cctx, &qlistener, &clientssl)) 2793 goto err; 2794 2795 /* Calling SSL_accept() on a listener is expected to fail */ 2796 ret = SSL_accept(qlistener); 2797 if (!TEST_int_le(ret, 0) 2798 || !TEST_int_eq(SSL_get_error(qlistener, ret), SSL_ERROR_SSL)) 2799 goto err; 2800 2801 /* Send ClientHello and server retry */ 2802 for (i = 0; i < 2; i++) { 2803 ret = SSL_connect(clientssl); 2804 if (!TEST_int_le(ret, 0) 2805 || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_READ)) 2806 goto err; 2807 SSL_handle_events(qlistener); 2808 } 2809 2810 /* We expect a server SSL object which has not yet completed its handshake */ 2811 serverssl = SSL_accept_connection(qlistener, 0); 2812 if (!TEST_ptr(serverssl) || !TEST_false(SSL_is_init_finished(serverssl))) 2813 goto err; 2814 2815 /* Call SSL_accept() and SSL_connect() until we are connected */ 2816 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, 2817 SSL_ERROR_NONE, 0, 0))) 2818 goto err; 2819 2820 testresult = 1; 2821 2822 err: 2823 SSL_free(serverssl); 2824 SSL_free(clientssl); 2825 SSL_free(qlistener); 2826 SSL_CTX_free(sctx); 2827 SSL_CTX_free(cctx); 2828 2829 return testresult; 2830 } 2831 2832 static SSL *quic_verify_ssl = NULL; 2833 2834 static int quic_verify_cb(int ok, X509_STORE_CTX *ctx) 2835 { 2836 SSL *cssl = (SSL *)X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); 2837 2838 /* Confirm we got the SSL object we were expecting */ 2839 return TEST_ptr_eq(cssl, quic_verify_ssl); 2840 } 2841 2842 static int test_ssl_set_verify(void) 2843 { 2844 SSL_CTX *cctx = NULL, *sctx = NULL; 2845 SSL *clientssl = NULL, *serverssl = NULL, *qlistener = NULL; 2846 int testresult = 0; 2847 int ret, i; 2848 2849 if (!TEST_ptr(sctx = create_server_ctx()) 2850 || !TEST_ptr(cctx = create_client_ctx())) 2851 goto err; 2852 2853 if (!create_quic_ssl_objects(sctx, cctx, &qlistener, &clientssl)) 2854 goto err; 2855 2856 quic_verify_ssl = clientssl; 2857 SSL_set_verify(clientssl, SSL_VERIFY_PEER, quic_verify_cb); 2858 2859 /* Send ClientHello and server retry */ 2860 for (i = 0; i < 2; i++) { 2861 ret = SSL_connect(clientssl); 2862 if (!TEST_int_le(ret, 0) 2863 || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_READ)) 2864 goto err; 2865 SSL_handle_events(qlistener); 2866 } 2867 2868 /* We expect a server SSL object which has not yet completed its handshake */ 2869 serverssl = SSL_accept_connection(qlistener, 0); 2870 2871 /* Call SSL_accept() and SSL_connect() until we are connected */ 2872 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, 2873 SSL_ERROR_NONE, 0, 0))) 2874 goto err; 2875 2876 testresult = 1; 2877 2878 err: 2879 SSL_free(serverssl); 2880 SSL_free(clientssl); 2881 SSL_free(qlistener); 2882 SSL_CTX_free(sctx); 2883 SSL_CTX_free(cctx); 2884 2885 return testresult; 2886 } 2887 2888 /* 2889 * When the server has a different primary group than the client, the server 2890 * should not fail on the client hello retry. 2891 */ 2892 static int test_client_hello_retry(void) 2893 { 2894 #if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_ECX) 2895 SSL_CTX *cctx = NULL, *sctx = NULL; 2896 SSL *clientssl = NULL, *serverssl = NULL, *qlistener = NULL; 2897 int testresult = 0, i = 0, ret = 0; 2898 2899 if (!TEST_ptr(sctx = create_server_ctx()) 2900 || !TEST_ptr(cctx = create_client_ctx())) 2901 goto err; 2902 /* 2903 * set the specific groups for the test 2904 */ 2905 if (!TEST_true(SSL_CTX_set1_groups_list(cctx, "secp384r1:secp256r1"))) 2906 goto err; 2907 if (!TEST_true(SSL_CTX_set1_groups_list(sctx, "secp256r1"))) 2908 goto err; 2909 2910 if (!create_quic_ssl_objects(sctx, cctx, &qlistener, &clientssl)) 2911 goto err; 2912 2913 /* Send ClientHello and server retry */ 2914 for (i = 0; i < 2; i++) { 2915 ret = SSL_connect(clientssl); 2916 if (!TEST_int_le(ret, 0) 2917 || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_READ)) 2918 goto err; 2919 SSL_handle_events(qlistener); 2920 } 2921 2922 /* We expect a server SSL object which has not yet completed its handshake */ 2923 serverssl = SSL_accept_connection(qlistener, 0); 2924 2925 /* Call SSL_accept() and SSL_connect() until we are connected */ 2926 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, 2927 SSL_ERROR_NONE, 0, 0))) 2928 goto err; 2929 2930 testresult = 1; 2931 2932 err: 2933 SSL_CTX_free(cctx); 2934 SSL_CTX_free(sctx); 2935 SSL_free(clientssl); 2936 SSL_free(serverssl); 2937 SSL_free(qlistener); 2938 2939 return testresult; 2940 #else 2941 return TEST_skip("EC(X) keys are not supported in this build"); 2942 #endif 2943 } 2944 /***********************************************************************************/ 2945 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n") 2946 2947 int setup_tests(void) 2948 { 2949 char *modulename; 2950 char *configfile; 2951 2952 libctx = OSSL_LIB_CTX_new(); 2953 if (!TEST_ptr(libctx)) 2954 return 0; 2955 2956 defctxnull = OSSL_PROVIDER_load(NULL, "null"); 2957 2958 /* 2959 * Verify that the default and fips providers in the default libctx are not 2960 * available 2961 */ 2962 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default")) 2963 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips"))) 2964 goto err; 2965 2966 if (!test_skip_common_options()) { 2967 TEST_error("Error parsing test options\n"); 2968 goto err; 2969 } 2970 2971 if (!TEST_ptr(modulename = test_get_argument(0)) 2972 || !TEST_ptr(configfile = test_get_argument(1)) 2973 || !TEST_ptr(certsdir = test_get_argument(2)) 2974 || !TEST_ptr(datadir = test_get_argument(3))) 2975 goto err; 2976 2977 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile))) 2978 goto err; 2979 2980 /* Check we have the expected provider available */ 2981 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename))) 2982 goto err; 2983 2984 /* Check the default provider is not available */ 2985 if (strcmp(modulename, "default") != 0 2986 && !TEST_false(OSSL_PROVIDER_available(libctx, "default"))) 2987 goto err; 2988 2989 if (strcmp(modulename, "fips") == 0) 2990 is_fips = 1; 2991 2992 cert = test_mk_file_path(certsdir, "servercert.pem"); 2993 if (cert == NULL) 2994 goto err; 2995 2996 ccert = test_mk_file_path(certsdir, "ee-client-chain.pem"); 2997 if (ccert == NULL) 2998 goto err; 2999 3000 cauthca = test_mk_file_path(certsdir, "root-cert.pem"); 3001 if (cauthca == NULL) 3002 goto err; 3003 3004 privkey = test_mk_file_path(certsdir, "serverkey.pem"); 3005 if (privkey == NULL) 3006 goto err; 3007 3008 cprivkey = test_mk_file_path(certsdir, "ee-key.pem"); 3009 if (privkey == NULL) 3010 goto err; 3011 3012 ADD_ALL_TESTS(test_quic_write_read, 3); 3013 ADD_TEST(test_fin_only_blocking); 3014 ADD_TEST(test_ciphersuites); 3015 ADD_TEST(test_cipher_find); 3016 ADD_TEST(test_version); 3017 #if defined(DO_SSL_TRACE_TEST) 3018 ADD_TEST(test_ssl_trace); 3019 #endif 3020 ADD_TEST(test_quic_forbidden_apis_ctx); 3021 ADD_TEST(test_quic_forbidden_apis); 3022 ADD_TEST(test_quic_forbidden_options); 3023 ADD_ALL_TESTS(test_quic_set_fd, 3); 3024 ADD_TEST(test_bio_ssl); 3025 ADD_TEST(test_back_pressure); 3026 ADD_TEST(test_multiple_dgrams); 3027 ADD_ALL_TESTS(test_non_io_retry, 2); 3028 ADD_TEST(test_quic_psk); 3029 ADD_ALL_TESTS(test_client_auth, 3); 3030 ADD_ALL_TESTS(test_alpn, 2); 3031 ADD_ALL_TESTS(test_noisy_dgram, 2); 3032 ADD_TEST(test_bw_limit); 3033 ADD_TEST(test_get_shutdown); 3034 ADD_ALL_TESTS(test_tparam, OSSL_NELEM(tparam_tests)); 3035 ADD_TEST(test_session_cb); 3036 ADD_TEST(test_domain_flags); 3037 ADD_TEST(test_early_ticks); 3038 ADD_TEST(test_ssl_new_from_listener); 3039 #ifndef OPENSSL_NO_SSL_TRACE 3040 ADD_TEST(test_new_token); 3041 #endif 3042 ADD_TEST(test_server_method_with_ssl_new); 3043 ADD_TEST(test_ssl_accept_connection); 3044 ADD_TEST(test_ssl_set_verify); 3045 ADD_TEST(test_client_hello_retry); 3046 return 1; 3047 err: 3048 cleanup_tests(); 3049 return 0; 3050 } 3051 3052 void cleanup_tests(void) 3053 { 3054 bio_f_noisy_dgram_filter_free(); 3055 bio_f_pkt_split_dgram_filter_free(); 3056 OPENSSL_free(cert); 3057 OPENSSL_free(privkey); 3058 OPENSSL_free(ccert); 3059 OPENSSL_free(cauthca); 3060 OPENSSL_free(cprivkey); 3061 OSSL_PROVIDER_unload(defctxnull); 3062 OSSL_LIB_CTX_free(libctx); 3063 } 3064