]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quicapitest.c
Add the ability to do client side tracing in quictestlib.c
[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, 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, 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, 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, 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 NULL)))
834 goto err;
835
836 msglen = strlen(msg);
837
838 do {
839 err = BIO_FLAGS_WRITE;
840 while (!clienterr && !retc && err == BIO_FLAGS_WRITE) {
841 retc = BIO_write_ex(cbio, msg, msglen, &written);
842 if (!retc) {
843 if (BIO_should_retry(cbio))
844 err = BIO_retry_type(cbio);
845 else
846 err = 0;
847 }
848 }
849
850 if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) {
851 TEST_info("BIO_write_ex() failed %d, %d", retc, err);
852 TEST_openssl_errors();
853 clienterr = 1;
854 }
855
856 if (!servererr && rets <= 0) {
857 ossl_quic_tserver_tick(qtserv);
858 servererr = ossl_quic_tserver_is_term_any(qtserv);
859 if (!servererr)
860 rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
861 }
862
863 if (clienterr && servererr)
864 goto err;
865
866 if (++abortctr == MAXLOOPS) {
867 TEST_info("No progress made");
868 goto err;
869 }
870 } while ((!retc && !clienterr) || (rets <= 0 && !servererr));
871
872 /*
873 * 2 loops: The first using the default stream, and the second using a new
874 * client initiated bidi stream.
875 */
876 for (i = 0, thisbio = cbio; i < 2; i++) {
877 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf),
878 &readbytes))
879 || !TEST_mem_eq(msg, msglen, buf, readbytes))
880 goto err;
881
882 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg,
883 msglen, &written)))
884 goto err;
885 ossl_quic_tserver_tick(qtserv);
886
887 if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes))
888 || !TEST_mem_eq(msg, msglen, buf, readbytes))
889 goto err;
890
891 if (i == 1)
892 break;
893
894 /*
895 * Now create a new stream and repeat. The bottom two bits of the stream
896 * id represents whether the stream is bidi and whether it is client
897 * initiated or not. For client initiated bidi they are both 0. So the
898 * first client initiated bidi stream is 0 and the next one is 4.
899 */
900 sid = 4;
901 stream = SSL_new_stream(clientquic, 0);
902 if (!TEST_ptr(stream))
903 goto err;
904
905 thisbio = strbio = BIO_new(BIO_f_ssl());
906 if (!TEST_ptr(strbio))
907 goto err;
908
909 if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1))
910 goto err;
911 stream = NULL;
912
913 if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written)))
914 goto err;
915
916 ossl_quic_tserver_tick(qtserv);
917 }
918
919 testresult = 1;
920 err:
921 BIO_free_all(cbio);
922 BIO_free_all(strbio);
923 SSL_free(stream);
924 ossl_quic_tserver_free(qtserv);
925 SSL_CTX_free(cctx);
926
927 return testresult;
928 }
929
930 #define BACK_PRESSURE_NUM_LOOPS 10000
931 /*
932 * Test that sending data from the client to the server faster than the server
933 * can process it eventually results in back pressure on the client.
934 */
935 static int test_back_pressure(void)
936 {
937 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
938 SSL *clientquic = NULL;
939 QUIC_TSERVER *qtserv = NULL;
940 int testresult = 0;
941 unsigned char *msg = NULL;
942 const size_t msglen = 1024;
943 unsigned char buf[64];
944 size_t readbytes, written;
945 int i;
946
947 if (!TEST_ptr(cctx)
948 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
949 privkey, 0, &qtserv,
950 &clientquic, NULL, NULL))
951 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
952 goto err;
953
954 msg = OPENSSL_malloc(msglen);
955 if (!TEST_ptr(msg))
956 goto err;
957 if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1))
958 goto err;
959
960 /*
961 * Limit to 10000 loops. If we've not seen any back pressure after that
962 * we're going to run out of memory, so abort.
963 */
964 for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) {
965 /* Send data from the client */
966 if (!SSL_write_ex(clientquic, msg, msglen, &written)) {
967 /* Check if we are seeing back pressure */
968 if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE)
969 break;
970 TEST_error("Unexpected client failure");
971 goto err;
972 }
973
974 /* Receive data at the server */
975 ossl_quic_tserver_tick(qtserv);
976 if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
977 &readbytes)))
978 goto err;
979 }
980
981 if (i == BACK_PRESSURE_NUM_LOOPS) {
982 TEST_error("No back pressure seen");
983 goto err;
984 }
985
986 testresult = 1;
987 err:
988 SSL_free(clientquic);
989 ossl_quic_tserver_free(qtserv);
990 SSL_CTX_free(cctx);
991 OPENSSL_free(msg);
992
993 return testresult;
994 }
995
996
997 static int dgram_ctr = 0;
998
999 static void dgram_cb(int write_p, int version, int content_type,
1000 const void *buf, size_t msglen, SSL *ssl, void *arg)
1001 {
1002 if (!write_p)
1003 return;
1004
1005 if (content_type != SSL3_RT_QUIC_DATAGRAM)
1006 return;
1007
1008 dgram_ctr++;
1009 }
1010
1011 /* Test that we send multiple datagrams in one go when appropriate */
1012 static int test_multiple_dgrams(void)
1013 {
1014 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1015 SSL *clientquic = NULL;
1016 QUIC_TSERVER *qtserv = NULL;
1017 int testresult = 0;
1018 unsigned char *buf;
1019 const size_t buflen = 1400;
1020 size_t written;
1021
1022 buf = OPENSSL_zalloc(buflen);
1023
1024 if (!TEST_ptr(cctx)
1025 || !TEST_ptr(buf)
1026 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1027 privkey, 0, &qtserv,
1028 &clientquic, NULL, NULL))
1029 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1030 goto err;
1031
1032 dgram_ctr = 0;
1033 SSL_set_msg_callback(clientquic, dgram_cb);
1034 if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written))
1035 || !TEST_size_t_eq(written, buflen)
1036 /* We wrote enough data for 2 datagrams */
1037 || !TEST_int_eq(dgram_ctr, 2))
1038 goto err;
1039
1040 testresult = 1;
1041 err:
1042 OPENSSL_free(buf);
1043 SSL_free(clientquic);
1044 ossl_quic_tserver_free(qtserv);
1045 SSL_CTX_free(cctx);
1046
1047 return testresult;
1048 }
1049
1050 static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg)
1051 {
1052 int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
1053 SSL *ssl;
1054 int *ctr = (int *)arg;
1055
1056 /* this should not happen but check anyway */
1057 if (idx < 0
1058 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
1059 return 0;
1060
1061 /* If this is the first time we've been called then retry */
1062 if (((*ctr)++) == 0)
1063 return SSL_set_retry_verify(ssl);
1064
1065 /* Otherwise do nothing - verification succeeds. Continue as normal */
1066 return 1;
1067 }
1068
1069 /* Test that we can handle a non-io related retry error
1070 * Test 0: Non-blocking
1071 * Test 1: Blocking
1072 */
1073 static int test_non_io_retry(int idx)
1074 {
1075 SSL_CTX *cctx;
1076 SSL *clientquic = NULL;
1077 QUIC_TSERVER *qtserv = NULL;
1078 int testresult = 0;
1079 int flags = 0, ctr = 0;
1080
1081 if (idx >= 1 && !qtest_supports_blocking())
1082 return TEST_skip("Blocking tests not supported in this build");
1083
1084 cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1085 if (!TEST_ptr(cctx))
1086 goto err;
1087
1088 SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &ctr);
1089
1090 flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0;
1091 if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey,
1092 flags, &qtserv, &clientquic, NULL,
1093 NULL))
1094 || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic,
1095 SSL_ERROR_WANT_RETRY_VERIFY))
1096 || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY)
1097 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1098 goto err;
1099
1100 testresult = 1;
1101 err:
1102 SSL_free(clientquic);
1103 ossl_quic_tserver_free(qtserv);
1104 SSL_CTX_free(cctx);
1105
1106 return testresult;
1107 }
1108
1109 static int use_session_cb_cnt = 0;
1110 static int find_session_cb_cnt = 0;
1111 static const char *pskid = "Identity";
1112 static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL;
1113
1114 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
1115 size_t *idlen, SSL_SESSION **sess)
1116 {
1117 use_session_cb_cnt++;
1118
1119 if (clientpsk == NULL)
1120 return 0;
1121
1122 SSL_SESSION_up_ref(clientpsk);
1123
1124 *sess = clientpsk;
1125 *id = (const unsigned char *)pskid;
1126 *idlen = strlen(pskid);
1127
1128 return 1;
1129 }
1130
1131 static int find_session_cb(SSL *ssl, const unsigned char *identity,
1132 size_t identity_len, SSL_SESSION **sess)
1133 {
1134 find_session_cb_cnt++;
1135
1136 if (serverpsk == NULL)
1137 return 0;
1138
1139 /* Identity should match that set by the client */
1140 if (strlen(pskid) != identity_len
1141 || strncmp(pskid, (const char *)identity, identity_len) != 0)
1142 return 0;
1143
1144 SSL_SESSION_up_ref(serverpsk);
1145 *sess = serverpsk;
1146
1147 return 1;
1148 }
1149
1150 static int test_quic_psk(void)
1151 {
1152 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1153 SSL *clientquic = NULL;
1154 QUIC_TSERVER *qtserv = NULL;
1155 int testresult = 0;
1156
1157 if (!TEST_ptr(cctx)
1158 /* No cert or private key for the server, i.e. PSK only */
1159 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL,
1160 NULL, 0, &qtserv,
1161 &clientquic, NULL, NULL)))
1162 goto end;
1163
1164 SSL_set_psk_use_session_callback(clientquic, use_session_cb);
1165 ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb);
1166 use_session_cb_cnt = 0;
1167 find_session_cb_cnt = 0;
1168
1169 clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH);
1170 if (!TEST_ptr(clientpsk))
1171 goto end;
1172 /* We already had one ref. Add another one */
1173 SSL_SESSION_up_ref(clientpsk);
1174
1175 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))
1176 || !TEST_int_eq(1, find_session_cb_cnt)
1177 || !TEST_int_eq(1, use_session_cb_cnt)
1178 /* Check that we actually used the PSK */
1179 || !TEST_true(SSL_session_reused(clientquic)))
1180 goto end;
1181
1182 testresult = 1;
1183
1184 end:
1185 SSL_free(clientquic);
1186 ossl_quic_tserver_free(qtserv);
1187 SSL_CTX_free(cctx);
1188 SSL_SESSION_free(clientpsk);
1189 SSL_SESSION_free(serverpsk);
1190 clientpsk = serverpsk = NULL;
1191
1192 return testresult;
1193 }
1194
1195 /*
1196 * Test that we correctly handle ALPN supplied by the application
1197 * Test 0: ALPN is provided
1198 * Test 1: No ALPN is provided
1199 */
1200 static int test_alpn(int idx)
1201 {
1202 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1203 SSL *clientquic = NULL;
1204 QUIC_TSERVER *qtserv = NULL;
1205 int testresult = 0;
1206 int ret;
1207
1208 /*
1209 * Ensure we only configure ciphersuites that are available with both the
1210 * default and fips providers to get the same output in both cases
1211 */
1212 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
1213 goto err;
1214
1215 if (!TEST_ptr(cctx)
1216 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1217 privkey,
1218 QTEST_FLAG_FAKE_TIME,
1219 &qtserv,
1220 &clientquic, NULL, NULL)))
1221 goto err;
1222
1223 if (idx == 0) {
1224 /*
1225 * Clear the ALPN we set in qtest_create_quic_objects. We use TEST_false
1226 * because SSL_set_alpn_protos returns 0 for success.
1227 */
1228 if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0)))
1229 goto err;
1230 }
1231
1232 ret = SSL_connect(clientquic);
1233 if (!TEST_int_le(ret, 0))
1234 goto err;
1235 if (idx == 0) {
1236 /* We expect an immediate error due to lack of ALPN */
1237 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL))
1238 goto err;
1239 } else {
1240 /* ALPN was provided so we expect the connection to succeed */
1241 if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ)
1242 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1243 goto err;
1244 }
1245
1246 testresult = 1;
1247 err:
1248 ossl_quic_tserver_free(qtserv);
1249 SSL_free(clientquic);
1250 SSL_CTX_free(cctx);
1251
1252 return testresult;
1253 }
1254
1255 #define MAX_LOOPS 2000
1256
1257 /*
1258 * Keep retrying SSL_read_ex until it succeeds or we give up. Accept a stream
1259 * if we don't already have one
1260 */
1261 static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf,
1262 size_t buflen, size_t *readbytes,
1263 QUIC_TSERVER *qtserv)
1264 {
1265 int abortctr;
1266
1267 /* We just do this in a loop with a sleep for simplicity */
1268 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
1269 if (*stream == NULL) {
1270 SSL_handle_events(clientquic);
1271 *stream = SSL_accept_stream(clientquic, 0);
1272 }
1273
1274 if (*stream != NULL) {
1275 if (SSL_read_ex(*stream, buf, buflen, readbytes))
1276 return 1;
1277 if (SSL_get_error(*stream, 0) != SSL_ERROR_WANT_READ)
1278 return 0;
1279 }
1280 ossl_quic_tserver_tick(qtserv);
1281 qtest_add_time(1);
1282 }
1283
1284 return 0;
1285 }
1286
1287 /* Keep retrying ossl_quic_tserver_read until it succeeds or we give up */
1288 static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid,
1289 void *buf, size_t buflen, size_t *readbytes,
1290 SSL *clientquic)
1291 {
1292 int abortctr;
1293
1294 /* We just do this in a loop with a sleep for simplicity */
1295 for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
1296 if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes))
1297 return 1;
1298 ossl_quic_tserver_tick(qtserv);
1299 SSL_handle_events(clientquic);
1300 qtest_add_time(1);
1301 }
1302
1303 return 0;
1304 }
1305
1306 /*
1307 * Create a connection and send data using an unreliable transport. We introduce
1308 * random noise to drop, delay and duplicate datagrams.
1309 * Test 0: Introduce random noise to datagrams
1310 * Test 1: As with test 0 but also split datagrams containing multiple packets
1311 * into individual datagrams so that individual packets can be affected
1312 * by noise - not just a whole datagram.
1313 */
1314 static int test_noisy_dgram(int idx)
1315 {
1316 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
1317 SSL *clientquic = NULL, *stream[2] = { NULL, NULL };
1318 QUIC_TSERVER *qtserv = NULL;
1319 int testresult = 0;
1320 uint64_t sid = 0;
1321 char *msg = "Hello world!";
1322 size_t msglen = strlen(msg), written, readbytes, i, j;
1323 unsigned char buf[80];
1324 int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME;
1325
1326 if (idx == 1)
1327 flags |= QTEST_FLAG_PACKET_SPLIT;
1328
1329 if (!TEST_ptr(cctx)
1330 || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
1331 privkey, flags,
1332 &qtserv,
1333 &clientquic, NULL, NULL)))
1334 goto err;
1335
1336 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
1337 goto err;
1338
1339 if (!TEST_true(SSL_set_incoming_stream_policy(clientquic,
1340 SSL_INCOMING_STREAM_POLICY_ACCEPT,
1341 0))
1342 || !TEST_true(SSL_set_default_stream_mode(clientquic,
1343 SSL_DEFAULT_STREAM_MODE_NONE)))
1344 goto err;
1345
1346 for (j = 0; j < 2; j++) {
1347 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)))
1348 goto err;
1349 ossl_quic_tserver_tick(qtserv);
1350 qtest_add_time(1);
1351
1352 /*
1353 * Send data from the server to the client. Some datagrams may get lost,
1354 * dropped or re-ordered. We repeat 10 times to ensure we are sending
1355 * enough datagrams for problems to be noticed.
1356 */
1357 for (i = 0; i < 10; i++) {
1358 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
1359 (unsigned char *)msg, msglen,
1360 &written))
1361 || !TEST_size_t_eq(msglen, written))
1362 goto err;
1363 ossl_quic_tserver_tick(qtserv);
1364 qtest_add_time(1);
1365
1366 /*
1367 * Since the underlying BIO is now noisy we may get failures that
1368 * need to be retried - so we use unreliable_client_read() to handle
1369 * that
1370 */
1371 if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf,
1372 sizeof(buf), &readbytes,
1373 qtserv))
1374 || !TEST_mem_eq(msg, msglen, buf, readbytes))
1375 goto err;
1376 }
1377
1378 /* Send data from the client to the server */
1379 for (i = 0; i < 10; i++) {
1380 if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg,
1381 msglen, &written))
1382 || !TEST_size_t_eq(msglen, written))
1383 goto err;
1384
1385 ossl_quic_tserver_tick(qtserv);
1386 qtest_add_time(1);
1387
1388 /*
1389 * Since the underlying BIO is now noisy we may get failures that
1390 * need to be retried - so we use unreliable_server_read() to handle
1391 * that
1392 */
1393 if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf),
1394 &readbytes, clientquic))
1395 || !TEST_mem_eq(msg, msglen, buf, readbytes))
1396 goto err;
1397 }
1398 }
1399
1400 testresult = 1;
1401 err:
1402 ossl_quic_tserver_free(qtserv);
1403 SSL_free(stream[0]);
1404 SSL_free(stream[1]);
1405 SSL_free(clientquic);
1406 SSL_CTX_free(cctx);
1407
1408 return testresult;
1409 }
1410
1411
1412 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
1413
1414 int setup_tests(void)
1415 {
1416 char *modulename;
1417 char *configfile;
1418
1419 libctx = OSSL_LIB_CTX_new();
1420 if (!TEST_ptr(libctx))
1421 return 0;
1422
1423 defctxnull = OSSL_PROVIDER_load(NULL, "null");
1424
1425 /*
1426 * Verify that the default and fips providers in the default libctx are not
1427 * available
1428 */
1429 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
1430 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
1431 goto err;
1432
1433 if (!test_skip_common_options()) {
1434 TEST_error("Error parsing test options\n");
1435 goto err;
1436 }
1437
1438 if (!TEST_ptr(modulename = test_get_argument(0))
1439 || !TEST_ptr(configfile = test_get_argument(1))
1440 || !TEST_ptr(certsdir = test_get_argument(2))
1441 || !TEST_ptr(datadir = test_get_argument(3)))
1442 goto err;
1443
1444 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
1445 goto err;
1446
1447 /* Check we have the expected provider available */
1448 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
1449 goto err;
1450
1451 /* Check the default provider is not available */
1452 if (strcmp(modulename, "default") != 0
1453 && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
1454 goto err;
1455
1456 if (strcmp(modulename, "fips") == 0)
1457 is_fips = 1;
1458
1459 cert = test_mk_file_path(certsdir, "servercert.pem");
1460 if (cert == NULL)
1461 goto err;
1462
1463 privkey = test_mk_file_path(certsdir, "serverkey.pem");
1464 if (privkey == NULL)
1465 goto err;
1466
1467 ADD_ALL_TESTS(test_quic_write_read, 3);
1468 ADD_TEST(test_fin_only_blocking);
1469 ADD_TEST(test_ciphersuites);
1470 ADD_TEST(test_cipher_find);
1471 ADD_TEST(test_version);
1472 #if defined(DO_SSL_TRACE_TEST)
1473 ADD_TEST(test_ssl_trace);
1474 #endif
1475 ADD_TEST(test_quic_forbidden_apis_ctx);
1476 ADD_TEST(test_quic_forbidden_apis);
1477 ADD_TEST(test_quic_forbidden_options);
1478 ADD_ALL_TESTS(test_quic_set_fd, 3);
1479 ADD_TEST(test_bio_ssl);
1480 ADD_TEST(test_back_pressure);
1481 ADD_TEST(test_multiple_dgrams);
1482 ADD_ALL_TESTS(test_non_io_retry, 2);
1483 ADD_TEST(test_quic_psk);
1484 ADD_ALL_TESTS(test_alpn, 2);
1485 ADD_ALL_TESTS(test_noisy_dgram, 2);
1486
1487 return 1;
1488 err:
1489 cleanup_tests();
1490 return 0;
1491 }
1492
1493 void cleanup_tests(void)
1494 {
1495 bio_f_noisy_dgram_filter_free();
1496 bio_f_pkt_split_dgram_filter_free();
1497 OPENSSL_free(cert);
1498 OPENSSL_free(privkey);
1499 OSSL_PROVIDER_unload(defctxnull);
1500 OSSL_LIB_CTX_free(libctx);
1501 }