]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quicapitest.c
Use fake time rather than real time in the noisy dgram test
[thirdparty/openssl.git] / test / quicapitest.c
1 /*
2 * Copyright 2022-2023 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
23 static OSSL_LIB_CTX *libctx = NULL;
24 static OSSL_PROVIDER *defctxnull = NULL;
25 static char *certsdir = NULL;
26 static char *cert = NULL;
27 static char *privkey = NULL;
28 static char *datadir = NULL;
29
30 static int is_fips = 0;
31
32 /* The ssltrace test assumes some options are switched on/off */
33 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) \
34 && defined(OPENSSL_NO_ZLIB) && defined(OPENSSL_NO_BROTLI) \
35 && defined(OPENSSL_NO_ZSTD) && !defined(OPENSSL_NO_ECX) \
36 && !defined(OPENSSL_NO_DH)
37 # define DO_SSL_TRACE_TEST
38 #endif
39
40 /*
41 * Test that we read what we've written.
42 * Test 0: Non-blocking
43 * Test 1: Blocking
44 * Test 2: Blocking, introduce socket error, test error handling.
45 */
46 static int test_quic_write_read(int idx)
47 {
48 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
49 SSL_CTX *sctx = NULL;
50 SSL *clientquic = NULL;
51 QUIC_TSERVER *qtserv = NULL;
52 int j, k, ret = 0;
53 unsigned char buf[20];
54 static char *msg = "A test message";
55 size_t msglen = strlen(msg);
56 size_t numbytes = 0;
57 int ssock = 0, csock = 0;
58 uint64_t sid = UINT64_MAX;
59 SSL_SESSION *sess = NULL;
60
61 if (idx >= 1 && !qtest_supports_blocking())
62 return TEST_skip("Blocking tests not supported in this build");
63
64 for (k = 0; k < 2; k++) {
65 if (!TEST_ptr(cctx)
66 || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
67 cert, privkey,
68 idx >= 1
69 ? QTEST_FLAG_BLOCK
70 : 0,
71 &qtserv, &clientquic,
72 NULL))
73 || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
74 goto end;
75
76 if (sess != NULL && !TEST_true(SSL_set_session(clientquic, sess)))
77 goto end;
78
79 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
80 goto end;
81
82 if (idx >= 1) {
83 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv),
84 &ssock)))
85 goto end;
86 if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0))
87 goto end;
88 }
89
90 sid = 0; /* client-initiated bidirectional stream */
91
92 for (j = 0; j < 2; j++) {
93 /* Check that sending and receiving app data is ok */
94 if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
95 || !TEST_size_t_eq(numbytes, msglen))
96 goto end;
97 if (idx >= 1) {
98 do {
99 if (!TEST_true(wait_until_sock_readable(ssock)))
100 goto end;
101
102 ossl_quic_tserver_tick(qtserv);
103
104 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf,
105 sizeof(buf),
106 &numbytes)))
107 goto end;
108 } while (numbytes == 0);
109
110 if (!TEST_mem_eq(buf, numbytes, msg, msglen))
111 goto end;
112 }
113
114 if (idx >= 2 && j > 0)
115 /* Introduce permanent socket error */
116 BIO_closesocket(csock);
117
118 ossl_quic_tserver_tick(qtserv);
119 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
120 (unsigned char *)msg,
121 msglen, &numbytes)))
122 goto end;
123 ossl_quic_tserver_tick(qtserv);
124 SSL_handle_events(clientquic);
125
126 if (idx >= 2 && j > 0) {
127 if (!TEST_false(SSL_read_ex(clientquic, buf, 1, &numbytes))
128 || !TEST_int_eq(SSL_get_error(clientquic, 0),
129 SSL_ERROR_SYSCALL)
130 || !TEST_false(SSL_write_ex(clientquic, msg, msglen,
131 &numbytes))
132 || !TEST_int_eq(SSL_get_error(clientquic, 0),
133 SSL_ERROR_SYSCALL))
134 goto end;
135 break;
136 }
137
138 /*
139 * In blocking mode the SSL_read_ex call will block until the socket
140 * is readable and has our data. In non-blocking mode we're doing
141 * everything in memory, so it should be immediately available
142 */
143 if (!TEST_true(SSL_read_ex(clientquic, buf, 1, &numbytes))
144 || !TEST_size_t_eq(numbytes, 1)
145 || !TEST_true(SSL_has_pending(clientquic))
146 || !TEST_int_eq(SSL_pending(clientquic), msglen - 1)
147 || !TEST_true(SSL_read_ex(clientquic, buf + 1,
148 sizeof(buf) - 1, &numbytes))
149 || !TEST_mem_eq(buf, numbytes + 1, msg, msglen))
150 goto end;
151 }
152
153 if (sess == NULL) {
154 /* We didn't supply a session so we're not expecting resumption */
155 if (!TEST_false(SSL_session_reused(clientquic)))
156 goto end;
157 /* We should have a session ticket by now */
158 sess = SSL_get1_session(clientquic);
159 if (!TEST_ptr(sess))
160 goto end;
161 } else {
162 /* We supplied a session so we should have resumed */
163 if (!TEST_true(SSL_session_reused(clientquic)))
164 goto end;
165 }
166
167 if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
168 goto end;
169
170 if (sctx == NULL) {
171 sctx = ossl_quic_tserver_get0_ssl_ctx(qtserv);
172 if (!TEST_true(SSL_CTX_up_ref(sctx))) {
173 sctx = NULL;
174 goto end;
175 }
176 }
177 ossl_quic_tserver_free(qtserv);
178 qtserv = NULL;
179 SSL_free(clientquic);
180 clientquic = NULL;
181
182 if (idx >= 2)
183 break;
184 }
185
186 ret = 1;
187
188 end:
189 SSL_SESSION_free(sess);
190 ossl_quic_tserver_free(qtserv);
191 SSL_free(clientquic);
192 SSL_CTX_free(cctx);
193 SSL_CTX_free(sctx);
194
195 return ret;
196 }
197
198 /*
199 * Test that sending FIN with no data to a client blocking in SSL_read_ex() will
200 * wake up the client.
201 */
202 static int test_fin_only_blocking(void)
203 {
204 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
205 SSL_CTX *sctx = NULL;
206 SSL *clientquic = NULL;
207 QUIC_TSERVER *qtserv = NULL;
208 const char *msg = "Hello World";
209 uint64_t sid;
210 size_t numbytes;
211 unsigned char buf[32];
212 int ret = 0;
213 OSSL_TIME timer, timediff;
214
215 if (!qtest_supports_blocking())
216 return TEST_skip("Blocking tests not supported in this build");
217
218 if (!TEST_ptr(cctx)
219 || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
220 cert, privkey,
221 QTEST_FLAG_BLOCK,
222 &qtserv, &clientquic,
223 NULL))
224 || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
225 goto end;
226
227 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
228 goto end;
229
230 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid))
231 || !TEST_true(ossl_quic_tserver_write(qtserv, sid,
232 (unsigned char *)msg,
233 strlen(msg), &numbytes))
234 || !TEST_size_t_eq(strlen(msg), numbytes))
235 goto end;
236
237 ossl_quic_tserver_tick(qtserv);
238
239 if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))
240 || !TEST_mem_eq(msg, strlen(msg), buf, numbytes))
241
242
243 goto end;
244
245 if (!TEST_true(ossl_quic_tserver_conclude(qtserv, sid)))
246 goto end;
247
248 timer = ossl_time_now();
249 if (!TEST_false(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)))
250 goto end;
251 timediff = ossl_time_subtract(ossl_time_now(), timer);
252
253 if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_ZERO_RETURN)
254 /*
255 * We expect the SSL_read_ex to not have blocked so this should
256 * be very fast. 20ms should be plenty.
257 */
258 || !TEST_uint64_t_le(ossl_time2ms(timediff), 20))
259 goto end;
260
261 if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
262 goto end;
263
264 ret = 1;
265
266 end:
267 ossl_quic_tserver_free(qtserv);
268 SSL_free(clientquic);
269 SSL_CTX_free(cctx);
270 SSL_CTX_free(sctx);
271
272 return ret;
273 }
274
275 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
276 static int test_ciphersuites(void)
277 {
278 SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
279 SSL *ssl;
280 int testresult = 0;
281 const STACK_OF(SSL_CIPHER) *ciphers = NULL;
282 const SSL_CIPHER *cipher;
283 /* We expect this exact list of ciphersuites by default */
284 int cipherids[] = {
285 TLS1_3_CK_AES_256_GCM_SHA384,
286 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
287 TLS1_3_CK_CHACHA20_POLY1305_SHA256,
288 #endif
289 TLS1_3_CK_AES_128_GCM_SHA256
290 };
291 size_t i, j;
292
293 if (!TEST_ptr(ctx))
294 return 0;
295
296 ssl = SSL_new(ctx);
297 if (!TEST_ptr(ssl))
298 goto err;
299
300 ciphers = SSL_get_ciphers(ssl);
301
302 for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) {
303 if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips)
304 continue;
305 cipher = sk_SSL_CIPHER_value(ciphers, j++);
306 if (!TEST_ptr(cipher))
307 goto err;
308 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i]))
309 goto err;
310 }
311
312 /* We should have checked all the ciphers in the stack */
313 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j))
314 goto err;
315
316 testresult = 1;
317 err:
318 SSL_free(ssl);
319 SSL_CTX_free(ctx);
320
321 return testresult;
322 }
323
324 static int test_cipher_find(void)
325 {
326 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
327 SSL *clientquic = NULL;
328 struct {
329 const unsigned char *cipherbytes;
330 int ok;
331 } testciphers[] = {
332 { TLS13_AES_128_GCM_SHA256_BYTES, 1 },
333 { TLS13_AES_256_GCM_SHA384_BYTES, 1 },
334 { TLS13_CHACHA20_POLY1305_SHA256_BYTES, 1 },
335 { TLS13_AES_128_CCM_SHA256_BYTES, 0 },
336 { TLS13_AES_128_CCM_8_SHA256_BYTES, 0 }
337 };
338 size_t i;
339 int testresult = 0;
340
341 if (!TEST_ptr(cctx))
342 goto err;
343
344 clientquic = SSL_new(cctx);
345 if (!TEST_ptr(clientquic))
346 goto err;
347
348 for (i = 0; i < OSSL_NELEM(testciphers); i++)
349 if (testciphers[i].ok) {
350 if (!TEST_ptr(SSL_CIPHER_find(clientquic,
351 testciphers[i].cipherbytes)))
352 goto err;
353 } else {
354 if (!TEST_ptr_null(SSL_CIPHER_find(clientquic,
355 testciphers[i].cipherbytes)))
356 goto err;
357 }
358
359 testresult = 1;
360 err:
361 SSL_free(clientquic);
362 SSL_CTX_free(cctx);
363
364 return testresult;
365 }
366
367 /*
368 * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
369 * SSL_is_dtls return the expected results for a QUIC connection. Compare with
370 * test_version() in sslapitest.c which does the same thing for TLS/DTLS
371 * connections.
372 */
373 static int test_version(void)
374 {
375 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
376 SSL *clientquic = NULL;
377 QUIC_TSERVER *qtserv = NULL;
378 int testresult = 0;
379
380 if (!TEST_ptr(cctx)
381 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
382 privkey, 0, &qtserv,
383 &clientquic, NULL))
384 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
385 goto err;
386
387 if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION)
388 || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1"))
389 goto err;
390
391 if (!TEST_true(SSL_is_quic(clientquic))
392 || !TEST_false(SSL_is_tls(clientquic))
393 || !TEST_false(SSL_is_dtls(clientquic)))
394 goto err;
395
396
397 testresult = 1;
398 err:
399 ossl_quic_tserver_free(qtserv);
400 SSL_free(clientquic);
401 SSL_CTX_free(cctx);
402
403 return testresult;
404 }
405
406 #if defined(DO_SSL_TRACE_TEST)
407 static void strip_line_ends(char *str)
408 {
409 size_t i;
410
411 for (i = strlen(str);
412 i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r');
413 i--);
414
415 str[i] = '\0';
416 }
417
418 static int compare_with_file(BIO *membio)
419 {
420 BIO *file = NULL, *newfile = NULL;
421 char buf1[512], buf2[512];
422 char *reffile;
423 int ret = 0;
424 size_t i;
425
426 reffile = test_mk_file_path(datadir, "ssltraceref.txt");
427 if (!TEST_ptr(reffile))
428 goto err;
429
430 file = BIO_new_file(reffile, "rb");
431 if (!TEST_ptr(file))
432 goto err;
433
434 newfile = BIO_new_file("ssltraceref-new.txt", "wb");
435 if (!TEST_ptr(newfile))
436 goto err;
437
438 while (BIO_gets(membio, buf2, sizeof(buf2)) > 0)
439 if (BIO_puts(newfile, buf2) <= 0) {
440 TEST_error("Failed writing new file data");
441 goto err;
442 }
443
444 if (!TEST_int_ge(BIO_seek(membio, 0), 0))
445 goto err;
446
447 while (BIO_gets(file, buf1, sizeof(buf1)) > 0) {
448 if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) {
449 TEST_error("Failed reading mem data");
450 goto err;
451 }
452 strip_line_ends(buf1);
453 strip_line_ends(buf2);
454 if (strlen(buf1) != strlen(buf2)) {
455 TEST_error("Actual and ref line data length mismatch");
456 TEST_info("%s", buf1);
457 TEST_info("%s", buf2);
458 goto err;
459 }
460 for (i = 0; i < strlen(buf1); i++) {
461 /* '?' is a wild card character in the reference text */
462 if (buf1[i] == '?')
463 buf2[i] = '?';
464 }
465 if (!TEST_str_eq(buf1, buf2))
466 goto err;
467 }
468 if (!TEST_true(BIO_eof(file))
469 || !TEST_true(BIO_eof(membio)))
470 goto err;
471
472 ret = 1;
473 err:
474 OPENSSL_free(reffile);
475 BIO_free(file);
476 BIO_free(newfile);
477 return ret;
478 }
479
480 /*
481 * Tests that the SSL_trace() msg_callback works as expected with a QUIC
482 * connection. This also provides testing of the msg_callback at the same time.
483 */
484 static int test_ssl_trace(void)
485 {
486 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
487 SSL *clientquic = NULL;
488 QUIC_TSERVER *qtserv = NULL;
489 int testresult = 0;
490 BIO *bio = BIO_new(BIO_s_mem());
491
492 /*
493 * Ensure we only configure ciphersuites that are available with both the
494 * default and fips providers to get the same output in both cases
495 */
496 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
497 goto err;
498
499 if (!TEST_ptr(cctx)
500 || !TEST_ptr(bio)
501 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
502 privkey,
503 QTEST_FLAG_FAKE_TIME,
504 &qtserv,
505 &clientquic, NULL)))
506 goto err;
507
508 SSL_set_msg_callback(clientquic, SSL_trace);
509 SSL_set_msg_callback_arg(clientquic, bio);
510
511 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
512 goto err;
513
514 if (!TEST_true(compare_with_file(bio)))
515 goto err;
516
517 testresult = 1;
518 err:
519 ossl_quic_tserver_free(qtserv);
520 SSL_free(clientquic);
521 SSL_CTX_free(cctx);
522 BIO_free(bio);
523
524 return testresult;
525 }
526 #endif
527
528 static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers)
529 {
530 size_t i;
531
532 /* Ensure ciphersuite list is suitably subsetted. */
533 for (i = 0; i < (size_t)sk_SSL_CIPHER_num(ciphers); ++i) {
534 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
535 switch (SSL_CIPHER_get_id(cipher)) {
536 case TLS1_3_CK_AES_128_GCM_SHA256:
537 case TLS1_3_CK_AES_256_GCM_SHA384:
538 case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
539 break;
540 default:
541 TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher));
542 return 0;
543 }
544 }
545
546 return 1;
547 }
548
549 /*
550 * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
551 */
552 static int test_quic_forbidden_apis_ctx(void)
553 {
554 int testresult = 0;
555 SSL_CTX *ctx = NULL;
556
557 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
558 goto err;
559
560 #ifndef OPENSSL_NO_SRTP
561 /* This function returns 0 on success and 1 on error, and should fail. */
562 if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx, "SRTP_AEAD_AES_128_GCM")))
563 goto err;
564 #endif
565
566 /*
567 * List of ciphersuites we do and don't allow in QUIC.
568 */
569 #define QUIC_CIPHERSUITES \
570 "TLS_AES_128_GCM_SHA256:" \
571 "TLS_AES_256_GCM_SHA384:" \
572 "TLS_CHACHA20_POLY1305_SHA256"
573
574 #define NON_QUIC_CIPHERSUITES \
575 "TLS_AES_128_CCM_SHA256:" \
576 "TLS_AES_256_CCM_SHA384:" \
577 "TLS_AES_128_CCM_8_SHA256"
578
579 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
580 if (!TEST_true(SSL_CTX_set_ciphersuites(ctx,
581 QUIC_CIPHERSUITES ":"
582 NON_QUIC_CIPHERSUITES)))
583 goto err;
584
585 /*
586 * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only
587 * filtered in SSL_get1_supported_ciphers, so we don't check for
588 * non-inclusion here.
589 */
590
591 testresult = 1;
592 err:
593 SSL_CTX_free(ctx);
594 return testresult;
595 }
596
597 static int test_quic_forbidden_apis(void)
598 {
599 int testresult = 0;
600 SSL_CTX *ctx = NULL;
601 SSL *ssl = NULL;
602 STACK_OF(SSL_CIPHER) *ciphers = NULL;
603
604 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
605 goto err;
606
607 if (!TEST_ptr(ssl = SSL_new(ctx)))
608 goto err;
609
610 #ifndef OPENSSL_NO_SRTP
611 /* This function returns 0 on success and 1 on error, and should fail. */
612 if (!TEST_true(SSL_set_tlsext_use_srtp(ssl, "SRTP_AEAD_AES_128_GCM")))
613 goto err;
614 #endif
615
616 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
617 if (!TEST_true(SSL_set_ciphersuites(ssl,
618 QUIC_CIPHERSUITES ":"
619 NON_QUIC_CIPHERSUITES)))
620 goto err;
621
622 /* Non-QUIC ciphersuites must not appear in supported ciphers list. */
623 if (!TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))
624 || !TEST_true(ensure_valid_ciphers(ciphers)))
625 goto err;
626
627 testresult = 1;
628 err:
629 sk_SSL_CIPHER_free(ciphers);
630 SSL_free(ssl);
631 SSL_CTX_free(ctx);
632 return testresult;
633 }
634
635 static int test_quic_forbidden_options(void)
636 {
637 int testresult = 0;
638 SSL_CTX *ctx = NULL;
639 SSL *ssl = NULL;
640 char buf[16];
641 size_t len;
642
643 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
644 goto err;
645
646 /* QUIC options restrictions do not affect SSL_CTX */
647 SSL_CTX_set_options(ctx, UINT64_MAX);
648
649 if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX))
650 goto err;
651
652 /* Set options on CTX which should not be inherited (tested below). */
653 SSL_CTX_set_read_ahead(ctx, 1);
654 SSL_CTX_set_max_early_data(ctx, 1);
655 SSL_CTX_set_recv_max_early_data(ctx, 1);
656 SSL_CTX_set_quiet_shutdown(ctx, 1);
657
658 if (!TEST_ptr(ssl = SSL_new(ctx)))
659 goto err;
660
661 /* Only permitted options get transferred to SSL object */
662 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
663 goto err;
664
665 /* Try again using SSL_set_options */
666 SSL_set_options(ssl, UINT64_MAX);
667
668 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
669 goto err;
670
671 /* Clear everything */
672 SSL_clear_options(ssl, UINT64_MAX);
673
674 if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0))
675 goto err;
676
677 /* Readahead */
678 if (!TEST_false(SSL_get_read_ahead(ssl)))
679 goto err;
680
681 SSL_set_read_ahead(ssl, 1);
682 if (!TEST_false(SSL_get_read_ahead(ssl)))
683 goto err;
684
685 /* Block padding */
686 if (!TEST_true(SSL_set_block_padding(ssl, 0))
687 || !TEST_true(SSL_set_block_padding(ssl, 1))
688 || !TEST_false(SSL_set_block_padding(ssl, 2)))
689 goto err;
690
691 /* Max fragment length */
692 if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_DISABLED))
693 || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_512)))
694 goto err;
695
696 /* Max early data */
697 if (!TEST_false(SSL_set_recv_max_early_data(ssl, 1))
698 || !TEST_false(SSL_set_max_early_data(ssl, 1)))
699 goto err;
700
701 /* Read/Write */
702 if (!TEST_false(SSL_read_early_data(ssl, buf, sizeof(buf), &len))
703 || !TEST_false(SSL_write_early_data(ssl, buf, sizeof(buf), &len)))
704 goto err;
705
706 /* Buffer Management */
707 if (!TEST_true(SSL_alloc_buffers(ssl))
708 || !TEST_false(SSL_free_buffers(ssl)))
709 goto err;
710
711 /* Pipelining */
712 if (!TEST_false(SSL_set_max_send_fragment(ssl, 2))
713 || !TEST_false(SSL_set_split_send_fragment(ssl, 2))
714 || !TEST_false(SSL_set_max_pipelines(ssl, 2)))
715 goto err;
716
717 /* HRR */
718 if (!TEST_false(SSL_stateless(ssl)))
719 goto err;
720
721 /* Quiet Shutdown */
722 if (!TEST_false(SSL_get_quiet_shutdown(ssl)))
723 goto err;
724
725 /* No duplication */
726 if (!TEST_ptr_null(SSL_dup(ssl)))
727 goto err;
728
729 /* No clear */
730 if (!TEST_false(SSL_clear(ssl)))
731 goto err;
732
733 testresult = 1;
734 err:
735 SSL_free(ssl);
736 SSL_CTX_free(ctx);
737 return testresult;
738 }
739
740 static int test_quic_set_fd(int idx)
741 {
742 int testresult = 0;
743 SSL_CTX *ctx = NULL;
744 SSL *ssl = NULL;
745 int fd = -1, resfd = -1;
746 BIO *bio = NULL;
747
748 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
749 goto err;
750
751 if (!TEST_ptr(ssl = SSL_new(ctx)))
752 goto err;
753
754 if (!TEST_int_ge(fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0), 0))
755 goto err;
756
757 if (idx == 0) {
758 if (!TEST_true(SSL_set_fd(ssl, fd)))
759 goto err;
760 if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
761 goto err;
762 if (!TEST_ptr_eq(bio, SSL_get_wbio(ssl)))
763 goto err;
764 } else if (idx == 1) {
765 if (!TEST_true(SSL_set_rfd(ssl, fd)))
766 goto err;
767 if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
768 goto err;
769 if (!TEST_ptr_null(SSL_get_wbio(ssl)))
770 goto err;
771 } else {
772 if (!TEST_true(SSL_set_wfd(ssl, fd)))
773 goto err;
774 if (!TEST_ptr(bio = SSL_get_wbio(ssl)))
775 goto err;
776 if (!TEST_ptr_null(SSL_get_rbio(ssl)))
777 goto err;
778 }
779
780 if (!TEST_int_eq(BIO_method_type(bio), BIO_TYPE_DGRAM))
781 goto err;
782
783 if (!TEST_true(BIO_get_fd(bio, &resfd))
784 || !TEST_int_eq(resfd, fd))
785 goto err;
786
787 testresult = 1;
788 err:
789 SSL_free(ssl);
790 SSL_CTX_free(ctx);
791 if (fd >= 0)
792 BIO_closesocket(fd);
793 return testresult;
794 }
795
796 #define MAXLOOPS 1000
797
798 static int test_bio_ssl(void)
799 {
800 /*
801 * We just use OSSL_QUIC_client_method() rather than
802 * OSSL_QUIC_client_thread_method(). We will never leave the connection idle
803 * so we will always be implicitly handling time events anyway via other
804 * IO calls.
805 */
806 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
807 SSL *clientquic = NULL, *stream = NULL;
808 QUIC_TSERVER *qtserv = NULL;
809 int testresult = 0;
810 BIO *cbio = NULL, *strbio = NULL, *thisbio;
811 const char *msg = "Hello world";
812 int abortctr = 0, err, clienterr = 0, servererr = 0, retc = 0, rets = 0;
813 size_t written, readbytes, msglen;
814 int sid = 0, i;
815 unsigned char buf[80];
816
817 if (!TEST_ptr(cctx))
818 goto err;
819
820 cbio = BIO_new_ssl(cctx, 1);
821 if (!TEST_ptr(cbio))
822 goto err;
823
824 /*
825 * We must configure the ALPN/peer address etc so we get the SSL object in
826 * order to pass it to qtest_create_quic_objects for configuration.
827 */
828 if (!TEST_int_eq(BIO_get_ssl(cbio, &clientquic), 1))
829 goto err;
830
831 if (!TEST_true(qtest_create_quic_objects(libctx, NULL, NULL, cert, privkey,
832 0, &qtserv, &clientquic, NULL)))
833 goto err;
834
835 msglen = strlen(msg);
836
837 do {
838 err = BIO_FLAGS_WRITE;
839 while (!clienterr && !retc && err == BIO_FLAGS_WRITE) {
840 retc = BIO_write_ex(cbio, msg, msglen, &written);
841 if (!retc) {
842 if (BIO_should_retry(cbio))
843 err = BIO_retry_type(cbio);
844 else
845 err = 0;
846 }
847 }
848
849 if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) {
850 TEST_info("BIO_write_ex() failed %d, %d", retc, err);
851 TEST_openssl_errors();
852 clienterr = 1;
853 }
854
855 if (!servererr && rets <= 0) {
856 ossl_quic_tserver_tick(qtserv);
857 servererr = ossl_quic_tserver_is_term_any(qtserv);
858 if (!servererr)
859 rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
860 }
861
862 if (clienterr && servererr)
863 goto err;
864
865 if (++abortctr == MAXLOOPS) {
866 TEST_info("No progress made");
867 goto err;
868 }
869 } while ((!retc && !clienterr) || (rets <= 0 && !servererr));
870
871 /*
872 * 2 loops: The first using the default stream, and the second using a new
873 * client initiated bidi stream.
874 */
875 for (i = 0, thisbio = cbio; i < 2; i++) {
876 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf),
877 &readbytes))
878 || !TEST_mem_eq(msg, msglen, buf, readbytes))
879 goto err;
880
881 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg,
882 msglen, &written)))
883 goto err;
884 ossl_quic_tserver_tick(qtserv);
885
886 if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes))
887 || !TEST_mem_eq(msg, msglen, buf, readbytes))
888 goto err;
889
890 if (i == 1)
891 break;
892
893 /*
894 * Now create a new stream and repeat. The bottom two bits of the stream
895 * id represents whether the stream is bidi and whether it is client
896 * initiated or not. For client initiated bidi they are both 0. So the
897 * first client initiated bidi stream is 0 and the next one is 4.
898 */
899 sid = 4;
900 stream = SSL_new_stream(clientquic, 0);
901 if (!TEST_ptr(stream))
902 goto err;
903
904 thisbio = strbio = BIO_new(BIO_f_ssl());
905 if (!TEST_ptr(strbio))
906 goto err;
907
908 if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1))
909 goto err;
910 stream = NULL;
911
912 if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written)))
913 goto err;
914
915 ossl_quic_tserver_tick(qtserv);
916 }
917
918 testresult = 1;
919 err:
920 BIO_free_all(cbio);
921 BIO_free_all(strbio);
922 SSL_free(stream);
923 ossl_quic_tserver_free(qtserv);
924 SSL_CTX_free(cctx);
925
926 return testresult;
927 }
928
929 #define BACK_PRESSURE_NUM_LOOPS 10000
930 /*
931 * Test that sending data from the client to the server faster than the server
932 * can process it eventually results in back pressure on the client.
933 */
934 static int test_back_pressure(void)
935 {
936 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
937 SSL *clientquic = NULL;
938 QUIC_TSERVER *qtserv = NULL;
939 int testresult = 0;
940 unsigned char *msg = NULL;
941 const size_t msglen = 1024;
942 unsigned char buf[64];
943 size_t readbytes, written;
944 int i;
945
946 if (!TEST_ptr(cctx)
947 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
948 privkey, 0, &qtserv,
949 &clientquic, NULL))
950 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
951 goto err;
952
953 msg = OPENSSL_malloc(msglen);
954 if (!TEST_ptr(msg))
955 goto err;
956 if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1))
957 goto err;
958
959 /*
960 * Limit to 10000 loops. If we've not seen any back pressure after that
961 * we're going to run out of memory, so abort.
962 */
963 for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) {
964 /* Send data from the client */
965 if (!SSL_write_ex(clientquic, msg, msglen, &written)) {
966 /* Check if we are seeing back pressure */
967 if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE)
968 break;
969 TEST_error("Unexpected client failure");
970 goto err;
971 }
972
973 /* Receive data at the server */
974 ossl_quic_tserver_tick(qtserv);
975 if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
976 &readbytes)))
977 goto err;
978 }
979
980 if (i == BACK_PRESSURE_NUM_LOOPS) {
981 TEST_error("No back pressure seen");
982 goto err;
983 }
984
985 testresult = 1;
986 err:
987 SSL_free(clientquic);
988 ossl_quic_tserver_free(qtserv);
989 SSL_CTX_free(cctx);
990 OPENSSL_free(msg);
991
992 return testresult;
993 }
994
995
996 static int dgram_ctr = 0;
997
998 static void dgram_cb(int write_p, int version, int content_type,
999 const void *buf, size_t msglen, SSL *ssl, void *arg)
1000 {
1001 if (!write_p)
1002 return;
1003
1004 if (content_type != SSL3_RT_QUIC_DATAGRAM)
1005 return;
1006
1007 dgram_ctr++;
1008 }
1009
1010 /* Test that we send multiple datagrams in one go when appropriate */
1011 static int test_multiple_dgrams(void)
1012 {
1013 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1014 SSL *clientquic = NULL;
1015 QUIC_TSERVER *qtserv = NULL;
1016 int testresult = 0;
1017 unsigned char *buf;
1018 const size_t buflen = 1400;
1019 size_t written;
1020
1021 buf = OPENSSL_zalloc(buflen);
1022
1023 if (!TEST_ptr(cctx)
1024 || !TEST_ptr(buf)
1025 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1026 privkey, 0, &qtserv,
1027 &clientquic, NULL))
1028 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1029 goto err;
1030
1031 dgram_ctr = 0;
1032 SSL_set_msg_callback(clientquic, dgram_cb);
1033 if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written))
1034 || !TEST_size_t_eq(written, buflen)
1035 /* We wrote enough data for 2 datagrams */
1036 || !TEST_int_eq(dgram_ctr, 2))
1037 goto err;
1038
1039 testresult = 1;
1040 err:
1041 OPENSSL_free(buf);
1042 SSL_free(clientquic);
1043 ossl_quic_tserver_free(qtserv);
1044 SSL_CTX_free(cctx);
1045
1046 return testresult;
1047 }
1048
1049 static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg)
1050 {
1051 int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
1052 SSL *ssl;
1053 int *ctr = (int *)arg;
1054
1055 /* this should not happen but check anyway */
1056 if (idx < 0
1057 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
1058 return 0;
1059
1060 /* If this is the first time we've been called then retry */
1061 if (((*ctr)++) == 0)
1062 return SSL_set_retry_verify(ssl);
1063
1064 /* Otherwise do nothing - verification succeeds. Continue as normal */
1065 return 1;
1066 }
1067
1068 /* Test that we can handle a non-io related retry error
1069 * Test 0: Non-blocking
1070 * Test 1: Blocking
1071 */
1072 static int test_non_io_retry(int idx)
1073 {
1074 SSL_CTX *cctx;
1075 SSL *clientquic = NULL;
1076 QUIC_TSERVER *qtserv = NULL;
1077 int testresult = 0;
1078 int flags = 0, ctr = 0;
1079
1080 if (idx >= 1 && !qtest_supports_blocking())
1081 return TEST_skip("Blocking tests not supported in this build");
1082
1083 cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1084 if (!TEST_ptr(cctx))
1085 goto err;
1086
1087 SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &ctr);
1088
1089 flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0;
1090 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey,
1091 flags, &qtserv, &clientquic, NULL))
1092 || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic,
1093 SSL_ERROR_WANT_RETRY_VERIFY))
1094 || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY)
1095 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1096 goto err;
1097
1098 testresult = 1;
1099 err:
1100 SSL_free(clientquic);
1101 ossl_quic_tserver_free(qtserv);
1102 SSL_CTX_free(cctx);
1103
1104 return testresult;
1105 }
1106
1107 static int use_session_cb_cnt = 0;
1108 static int find_session_cb_cnt = 0;
1109 static const char *pskid = "Identity";
1110 static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL;
1111
1112 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
1113 size_t *idlen, SSL_SESSION **sess)
1114 {
1115 use_session_cb_cnt++;
1116
1117 if (clientpsk == NULL)
1118 return 0;
1119
1120 SSL_SESSION_up_ref(clientpsk);
1121
1122 *sess = clientpsk;
1123 *id = (const unsigned char *)pskid;
1124 *idlen = strlen(pskid);
1125
1126 return 1;
1127 }
1128
1129 static int find_session_cb(SSL *ssl, const unsigned char *identity,
1130 size_t identity_len, SSL_SESSION **sess)
1131 {
1132 find_session_cb_cnt++;
1133
1134 if (serverpsk == NULL)
1135 return 0;
1136
1137 /* Identity should match that set by the client */
1138 if (strlen(pskid) != identity_len
1139 || strncmp(pskid, (const char *)identity, identity_len) != 0)
1140 return 0;
1141
1142 SSL_SESSION_up_ref(serverpsk);
1143 *sess = serverpsk;
1144
1145 return 1;
1146 }
1147
1148 static int test_quic_psk(void)
1149 {
1150 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1151 SSL *clientquic = NULL;
1152 QUIC_TSERVER *qtserv = NULL;
1153 int testresult = 0;
1154
1155 if (!TEST_ptr(cctx)
1156 /* No cert or private key for the server, i.e. PSK only */
1157 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL,
1158 NULL, 0, &qtserv,
1159 &clientquic, NULL)))
1160 goto end;
1161
1162 SSL_set_psk_use_session_callback(clientquic, use_session_cb);
1163 ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb);
1164 use_session_cb_cnt = 0;
1165 find_session_cb_cnt = 0;
1166
1167 clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH);
1168 if (!TEST_ptr(clientpsk))
1169 goto end;
1170 /* We already had one ref. Add another one */
1171 SSL_SESSION_up_ref(clientpsk);
1172
1173 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))
1174 || !TEST_int_eq(1, find_session_cb_cnt)
1175 || !TEST_int_eq(1, use_session_cb_cnt)
1176 /* Check that we actually used the PSK */
1177 || !TEST_true(SSL_session_reused(clientquic)))
1178 goto end;
1179
1180 testresult = 1;
1181
1182 end:
1183 SSL_free(clientquic);
1184 ossl_quic_tserver_free(qtserv);
1185 SSL_CTX_free(cctx);
1186 SSL_SESSION_free(clientpsk);
1187 SSL_SESSION_free(serverpsk);
1188 clientpsk = serverpsk = NULL;
1189
1190 return testresult;
1191 }
1192
1193 /*
1194 * Test that we correctly handle ALPN supplied by the application
1195 * Test 0: ALPN is provided
1196 * Test 1: No ALPN is provided
1197 */
1198 static int test_alpn(int idx)
1199 {
1200 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1201 SSL *clientquic = NULL;
1202 QUIC_TSERVER *qtserv = NULL;
1203 int testresult = 0;
1204 int ret;
1205
1206 /*
1207 * Ensure we only configure ciphersuites that are available with both the
1208 * default and fips providers to get the same output in both cases
1209 */
1210 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
1211 goto err;
1212
1213 if (!TEST_ptr(cctx)
1214 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1215 privkey,
1216 QTEST_FLAG_FAKE_TIME,
1217 &qtserv,
1218 &clientquic, NULL)))
1219 goto err;
1220
1221 if (idx == 0) {
1222 /*
1223 * Clear the ALPN we set in qtest_create_quic_objects. We use TEST_false
1224 * because SSL_set_alpn_protos returns 0 for success.
1225 */
1226 if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0)))
1227 goto err;
1228 }
1229
1230 ret = SSL_connect(clientquic);
1231 if (!TEST_int_le(ret, 0))
1232 goto err;
1233 if (idx == 0) {
1234 /* We expect an immediate error due to lack of ALPN */
1235 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL))
1236 goto err;
1237 } else {
1238 /* ALPN was provided so we expect the connection to succeed */
1239 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ)
1240 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1241 goto err;
1242 }
1243
1244 testresult = 1;
1245 err:
1246 ossl_quic_tserver_free(qtserv);
1247 SSL_free(clientquic);
1248 SSL_CTX_free(cctx);
1249
1250 return testresult;
1251 }
1252
1253 #define MAX_LOOPS 2000
1254
1255 /*
1256 * Keep retrying SSL_read_ex until it succeeds or we give up. Accept a stream
1257 * if we don't already have one
1258 */
1259 static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf,
1260 size_t buflen, size_t *readbytes,
1261 QUIC_TSERVER *qtserv)
1262 {
1263 int abortctr;
1264
1265 /* We just do this in a loop with a sleep for simplicity */
1266 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
1267 if (*stream == NULL) {
1268 SSL_handle_events(clientquic);
1269 *stream = SSL_accept_stream(clientquic, 0);
1270 }
1271
1272 if (*stream != NULL) {
1273 if (SSL_read_ex(*stream, buf, buflen, readbytes))
1274 return 1;
1275 if (SSL_get_error(*stream, 0) != SSL_ERROR_WANT_READ)
1276 return 0;
1277 }
1278 ossl_quic_tserver_tick(qtserv);
1279 qtest_add_time(1);
1280 }
1281
1282 return 0;
1283 }
1284
1285 /* Keep retrying ossl_quic_tserver_read until it succeeds or we give up */
1286 static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid,
1287 void *buf, size_t buflen, size_t *readbytes,
1288 SSL *clientquic)
1289 {
1290 int abortctr;
1291
1292 /* We just do this in a loop with a sleep for simplicity */
1293 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
1294 if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes))
1295 return 1;
1296 ossl_quic_tserver_tick(qtserv);
1297 SSL_handle_events(clientquic);
1298 qtest_add_time(1);
1299 }
1300
1301 return 0;
1302 }
1303
1304 static int test_noisy_dgram(void)
1305 {
1306 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1307 SSL *clientquic = NULL, *stream[2] = { NULL, NULL };
1308 QUIC_TSERVER *qtserv = NULL;
1309 int testresult = 0;
1310 uint64_t sid = 0;
1311 char *msg = "Hello world!";
1312 size_t msglen = strlen(msg), written, readbytes, i, j;
1313 unsigned char buf[80];
1314
1315 if (!TEST_ptr(cctx)
1316 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1317 privkey,
1318 QTEST_FLAG_NOISE
1319 | QTEST_FLAG_FAKE_TIME,
1320 &qtserv,
1321 &clientquic, NULL)))
1322 goto err;
1323
1324 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1325 goto err;
1326
1327 if (!TEST_true(SSL_set_incoming_stream_policy(clientquic,
1328 SSL_INCOMING_STREAM_POLICY_ACCEPT,
1329 0))
1330 || !TEST_true(SSL_set_default_stream_mode(clientquic,
1331 SSL_DEFAULT_STREAM_MODE_NONE)))
1332 goto err;
1333
1334 for (j = 0; j < 2; j++) {
1335 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)))
1336 goto err;
1337 ossl_quic_tserver_tick(qtserv);
1338 qtest_add_time(1);
1339
1340 /*
1341 * Send data from the server to the client. Some datagrams may get lost,
1342 * dropped or re-ordered. We repeat 10 times to ensure we are sending
1343 * enough datagrams for problems to be noticed.
1344 */
1345 for (i = 0; i < 10; i++) {
1346 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
1347 (unsigned char *)msg, msglen,
1348 &written))
1349 || !TEST_size_t_eq(msglen, written))
1350 goto err;
1351 ossl_quic_tserver_tick(qtserv);
1352 qtest_add_time(1);
1353
1354 /*
1355 * Since the underlying BIO is now noisy we may get failures that
1356 * need to be retried - so we use unreliable_client_read() to handle
1357 * that
1358 */
1359 if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf,
1360 sizeof(buf), &readbytes,
1361 qtserv))
1362 || !TEST_mem_eq(msg, msglen, buf, readbytes))
1363 goto err;
1364 }
1365
1366 /* Send data from the client to the server */
1367 for (i = 0; i < 10; i++) {
1368 if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg,
1369 msglen, &written))
1370 || !TEST_size_t_eq(msglen, written))
1371 goto err;
1372
1373 ossl_quic_tserver_tick(qtserv);
1374 qtest_add_time(1);
1375
1376 /*
1377 * Since the underlying BIO is now noisy we may get failures that
1378 * need to be retried - so we use unreliable_server_read() to handle
1379 * that
1380 */
1381 if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf),
1382 &readbytes, clientquic))
1383 || !TEST_mem_eq(msg, msglen, buf, readbytes))
1384 goto err;
1385 }
1386 }
1387
1388 testresult = 1;
1389 err:
1390 ossl_quic_tserver_free(qtserv);
1391 SSL_free(stream[0]);
1392 SSL_free(stream[1]);
1393 SSL_free(clientquic);
1394 SSL_CTX_free(cctx);
1395
1396 return testresult;
1397 }
1398
1399
1400 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
1401
1402 int setup_tests(void)
1403 {
1404 char *modulename;
1405 char *configfile;
1406
1407 libctx = OSSL_LIB_CTX_new();
1408 if (!TEST_ptr(libctx))
1409 return 0;
1410
1411 defctxnull = OSSL_PROVIDER_load(NULL, "null");
1412
1413 /*
1414 * Verify that the default and fips providers in the default libctx are not
1415 * available
1416 */
1417 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
1418 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
1419 goto err;
1420
1421 if (!test_skip_common_options()) {
1422 TEST_error("Error parsing test options\n");
1423 goto err;
1424 }
1425
1426 if (!TEST_ptr(modulename = test_get_argument(0))
1427 || !TEST_ptr(configfile = test_get_argument(1))
1428 || !TEST_ptr(certsdir = test_get_argument(2))
1429 || !TEST_ptr(datadir = test_get_argument(3)))
1430 goto err;
1431
1432 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
1433 goto err;
1434
1435 /* Check we have the expected provider available */
1436 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
1437 goto err;
1438
1439 /* Check the default provider is not available */
1440 if (strcmp(modulename, "default") != 0
1441 && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
1442 goto err;
1443
1444 if (strcmp(modulename, "fips") == 0)
1445 is_fips = 1;
1446
1447 cert = test_mk_file_path(certsdir, "servercert.pem");
1448 if (cert == NULL)
1449 goto err;
1450
1451 privkey = test_mk_file_path(certsdir, "serverkey.pem");
1452 if (privkey == NULL)
1453 goto err;
1454
1455 ADD_ALL_TESTS(test_quic_write_read, 3);
1456 ADD_TEST(test_fin_only_blocking);
1457 ADD_TEST(test_ciphersuites);
1458 ADD_TEST(test_cipher_find);
1459 ADD_TEST(test_version);
1460 #if defined(DO_SSL_TRACE_TEST)
1461 ADD_TEST(test_ssl_trace);
1462 #endif
1463 ADD_TEST(test_quic_forbidden_apis_ctx);
1464 ADD_TEST(test_quic_forbidden_apis);
1465 ADD_TEST(test_quic_forbidden_options);
1466 ADD_ALL_TESTS(test_quic_set_fd, 3);
1467 ADD_TEST(test_bio_ssl);
1468 ADD_TEST(test_back_pressure);
1469 ADD_TEST(test_multiple_dgrams);
1470 ADD_ALL_TESTS(test_non_io_retry, 2);
1471 ADD_TEST(test_quic_psk);
1472 ADD_ALL_TESTS(test_alpn, 2);
1473 ADD_TEST(test_noisy_dgram);
1474
1475 return 1;
1476 err:
1477 cleanup_tests();
1478 return 0;
1479 }
1480
1481 void cleanup_tests(void)
1482 {
1483 bio_f_noisy_dgram_filter_free();
1484 OPENSSL_free(cert);
1485 OPENSSL_free(privkey);
1486 OSSL_PROVIDER_unload(defctxnull);
1487 OSSL_LIB_CTX_free(libctx);
1488 }