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