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