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