2 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/opensslconf.h>
14 #include <openssl/quic.h>
15 #include <openssl/rand.h>
17 #include "helpers/ssltestlib.h"
18 #include "helpers/quictestlib.h"
20 #include "testutil/output.h"
21 #include "../ssl/ssl_local.h"
23 static OSSL_LIB_CTX
*libctx
= NULL
;
24 static OSSL_PROVIDER
*defctxnull
= NULL
;
25 static char *certsdir
= NULL
;
26 static char *cert
= NULL
;
27 static char *privkey
= NULL
;
28 static char *datadir
= NULL
;
30 static int is_fips
= 0;
32 /* The ssltrace test assumes some options are switched on/off */
33 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) \
34 && defined(OPENSSL_NO_ZLIB) && defined(OPENSSL_NO_BROTLI) \
35 && defined(OPENSSL_NO_ZSTD) && !defined(OPENSSL_NO_ECX) \
36 && !defined(OPENSSL_NO_DH)
37 # define DO_SSL_TRACE_TEST
41 * Test that we read what we've written.
42 * Test 0: Non-blocking
44 * Test 2: Blocking, introduce socket error, test error handling.
46 static int test_quic_write_read(int idx
)
48 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
50 SSL
*clientquic
= NULL
;
51 QUIC_TSERVER
*qtserv
= NULL
;
53 unsigned char buf
[20];
54 static char *msg
= "A test message";
55 size_t msglen
= strlen(msg
);
57 int ssock
= 0, csock
= 0;
58 uint64_t sid
= UINT64_MAX
;
59 SSL_SESSION
*sess
= NULL
;
61 if (idx
>= 1 && !qtest_supports_blocking())
62 return TEST_skip("Blocking tests not supported in this build");
64 for (k
= 0; k
< 2; k
++) {
66 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, sctx
,
73 || !TEST_true(SSL_set_tlsext_host_name(clientquic
, "localhost")))
76 if (sess
!= NULL
&& !TEST_true(SSL_set_session(clientquic
, sess
)))
79 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
83 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv
),
86 if (!TEST_int_gt(csock
= SSL_get_rfd(clientquic
), 0))
90 sid
= 0; /* client-initiated bidirectional stream */
92 for (j
= 0; j
< 2; j
++) {
93 /* Check that sending and receiving app data is ok */
94 if (!TEST_true(SSL_write_ex(clientquic
, msg
, msglen
, &numbytes
))
95 || !TEST_size_t_eq(numbytes
, msglen
))
99 if (!TEST_true(wait_until_sock_readable(ssock
)))
102 ossl_quic_tserver_tick(qtserv
);
104 if (!TEST_true(ossl_quic_tserver_read(qtserv
, sid
, buf
,
108 } while (numbytes
== 0);
110 if (!TEST_mem_eq(buf
, numbytes
, msg
, msglen
))
114 if (idx
>= 2 && j
> 0)
115 /* Introduce permanent socket error */
116 BIO_closesocket(csock
);
118 ossl_quic_tserver_tick(qtserv
);
119 if (!TEST_true(ossl_quic_tserver_write(qtserv
, sid
,
120 (unsigned char *)msg
,
123 ossl_quic_tserver_tick(qtserv
);
124 SSL_handle_events(clientquic
);
126 if (idx
>= 2 && j
> 0) {
127 if (!TEST_false(SSL_read_ex(clientquic
, buf
, 1, &numbytes
))
128 || !TEST_int_eq(SSL_get_error(clientquic
, 0),
130 || !TEST_false(SSL_write_ex(clientquic
, msg
, msglen
,
132 || !TEST_int_eq(SSL_get_error(clientquic
, 0),
139 * In blocking mode the SSL_read_ex call will block until the socket
140 * is readable and has our data. In non-blocking mode we're doing
141 * everything in memory, so it should be immediately available
143 if (!TEST_true(SSL_read_ex(clientquic
, buf
, 1, &numbytes
))
144 || !TEST_size_t_eq(numbytes
, 1)
145 || !TEST_true(SSL_has_pending(clientquic
))
146 || !TEST_int_eq(SSL_pending(clientquic
), msglen
- 1)
147 || !TEST_true(SSL_read_ex(clientquic
, buf
+ 1,
148 sizeof(buf
) - 1, &numbytes
))
149 || !TEST_mem_eq(buf
, numbytes
+ 1, msg
, msglen
))
154 /* We didn't supply a session so we're not expecting resumption */
155 if (!TEST_false(SSL_session_reused(clientquic
)))
157 /* We should have a session ticket by now */
158 sess
= SSL_get1_session(clientquic
);
162 /* We supplied a session so we should have resumed */
163 if (!TEST_true(SSL_session_reused(clientquic
)))
167 if (!TEST_true(qtest_shutdown(qtserv
, clientquic
)))
171 sctx
= ossl_quic_tserver_get0_ssl_ctx(qtserv
);
172 if (!TEST_true(SSL_CTX_up_ref(sctx
))) {
177 ossl_quic_tserver_free(qtserv
);
179 SSL_free(clientquic
);
189 SSL_SESSION_free(sess
);
190 ossl_quic_tserver_free(qtserv
);
191 SSL_free(clientquic
);
199 * Test that sending FIN with no data to a client blocking in SSL_read_ex() will
200 * wake up the client.
202 static int test_fin_only_blocking(void)
204 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
205 SSL_CTX
*sctx
= NULL
;
206 SSL
*clientquic
= NULL
;
207 QUIC_TSERVER
*qtserv
= NULL
;
208 const char *msg
= "Hello World";
211 unsigned char buf
[32];
213 OSSL_TIME timer
, timediff
;
215 if (!qtest_supports_blocking())
216 return TEST_skip("Blocking tests not supported in this build");
219 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, sctx
,
222 &qtserv
, &clientquic
,
224 || !TEST_true(SSL_set_tlsext_host_name(clientquic
, "localhost")))
227 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
230 if (!TEST_true(ossl_quic_tserver_stream_new(qtserv
, 0, &sid
))
231 || !TEST_true(ossl_quic_tserver_write(qtserv
, sid
,
232 (unsigned char *)msg
,
233 strlen(msg
), &numbytes
))
234 || !TEST_size_t_eq(strlen(msg
), numbytes
))
237 ossl_quic_tserver_tick(qtserv
);
239 if (!TEST_true(SSL_read_ex(clientquic
, buf
, sizeof(buf
), &numbytes
))
240 || !TEST_mem_eq(msg
, strlen(msg
), buf
, numbytes
))
245 if (!TEST_true(ossl_quic_tserver_conclude(qtserv
, sid
)))
248 timer
= ossl_time_now();
249 if (!TEST_false(SSL_read_ex(clientquic
, buf
, sizeof(buf
), &numbytes
)))
251 timediff
= ossl_time_subtract(ossl_time_now(), timer
);
253 if (!TEST_int_eq(SSL_get_error(clientquic
, 0), SSL_ERROR_ZERO_RETURN
)
255 * We expect the SSL_read_ex to not have blocked so this should
256 * be very fast. 20ms should be plenty.
258 || !TEST_uint64_t_le(ossl_time2ms(timediff
), 20))
261 if (!TEST_true(qtest_shutdown(qtserv
, clientquic
)))
267 ossl_quic_tserver_free(qtserv
);
268 SSL_free(clientquic
);
275 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
276 static int test_ciphersuites(void)
278 SSL_CTX
*ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
281 const STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
282 const SSL_CIPHER
*cipher
;
283 /* We expect this exact list of ciphersuites by default */
285 TLS1_3_CK_AES_256_GCM_SHA384
,
286 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
287 TLS1_3_CK_CHACHA20_POLY1305_SHA256
,
289 TLS1_3_CK_AES_128_GCM_SHA256
300 ciphers
= SSL_get_ciphers(ssl
);
302 for (i
= 0, j
= 0; i
< OSSL_NELEM(cipherids
); i
++) {
303 if (cipherids
[i
] == TLS1_3_CK_CHACHA20_POLY1305_SHA256
&& is_fips
)
305 cipher
= sk_SSL_CIPHER_value(ciphers
, j
++);
306 if (!TEST_ptr(cipher
))
308 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher
), cipherids
[i
]))
312 /* We should have checked all the ciphers in the stack */
313 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers
), j
))
324 static int test_cipher_find(void)
326 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
327 SSL
*clientquic
= NULL
;
329 const unsigned char *cipherbytes
;
332 { TLS13_AES_128_GCM_SHA256_BYTES
, 1 },
333 { TLS13_AES_256_GCM_SHA384_BYTES
, 1 },
334 { TLS13_CHACHA20_POLY1305_SHA256_BYTES
, 1 },
335 { TLS13_AES_128_CCM_SHA256_BYTES
, 0 },
336 { TLS13_AES_128_CCM_8_SHA256_BYTES
, 0 }
344 clientquic
= SSL_new(cctx
);
345 if (!TEST_ptr(clientquic
))
348 for (i
= 0; i
< OSSL_NELEM(testciphers
); i
++)
349 if (testciphers
[i
].ok
) {
350 if (!TEST_ptr(SSL_CIPHER_find(clientquic
,
351 testciphers
[i
].cipherbytes
)))
354 if (!TEST_ptr_null(SSL_CIPHER_find(clientquic
,
355 testciphers
[i
].cipherbytes
)))
361 SSL_free(clientquic
);
368 * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
369 * SSL_is_dtls return the expected results for a QUIC connection. Compare with
370 * test_version() in sslapitest.c which does the same thing for TLS/DTLS
373 static int test_version(void)
375 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
376 SSL
*clientquic
= NULL
;
377 QUIC_TSERVER
*qtserv
= NULL
;
381 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, cert
,
384 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
387 if (!TEST_int_eq(SSL_version(clientquic
), OSSL_QUIC1_VERSION
)
388 || !TEST_str_eq(SSL_get_version(clientquic
), "QUICv1"))
391 if (!TEST_true(SSL_is_quic(clientquic
))
392 || !TEST_false(SSL_is_tls(clientquic
))
393 || !TEST_false(SSL_is_dtls(clientquic
)))
399 ossl_quic_tserver_free(qtserv
);
400 SSL_free(clientquic
);
406 #if defined(DO_SSL_TRACE_TEST)
407 static void strip_line_ends(char *str
)
411 for (i
= strlen(str
);
412 i
> 0 && (str
[i
- 1] == '\n' || str
[i
- 1] == '\r');
418 static int compare_with_file(BIO
*membio
)
420 BIO
*file
= NULL
, *newfile
= NULL
;
421 char buf1
[512], buf2
[512];
426 reffile
= test_mk_file_path(datadir
, "ssltraceref.txt");
427 if (!TEST_ptr(reffile
))
430 file
= BIO_new_file(reffile
, "rb");
434 newfile
= BIO_new_file("ssltraceref-new.txt", "wb");
435 if (!TEST_ptr(newfile
))
438 while (BIO_gets(membio
, buf2
, sizeof(buf2
)) > 0)
439 if (BIO_puts(newfile
, buf2
) <= 0) {
440 TEST_error("Failed writing new file data");
444 if (!TEST_int_ge(BIO_seek(membio
, 0), 0))
447 while (BIO_gets(file
, buf1
, sizeof(buf1
)) > 0) {
448 if (BIO_gets(membio
, buf2
, sizeof(buf2
)) <= 0) {
449 TEST_error("Failed reading mem data");
452 strip_line_ends(buf1
);
453 strip_line_ends(buf2
);
454 if (strlen(buf1
) != strlen(buf2
)) {
455 TEST_error("Actual and ref line data length mismatch");
456 TEST_info("%s", buf1
);
457 TEST_info("%s", buf2
);
460 for (i
= 0; i
< strlen(buf1
); i
++) {
461 /* '?' is a wild card character in the reference text */
465 if (!TEST_str_eq(buf1
, buf2
))
468 if (!TEST_true(BIO_eof(file
))
469 || !TEST_true(BIO_eof(membio
)))
474 OPENSSL_free(reffile
);
481 * Tests that the SSL_trace() msg_callback works as expected with a QUIC
482 * connection. This also provides testing of the msg_callback at the same time.
484 static int test_ssl_trace(void)
486 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
487 SSL
*clientquic
= NULL
;
488 QUIC_TSERVER
*qtserv
= NULL
;
490 BIO
*bio
= BIO_new(BIO_s_mem());
493 * Ensure we only configure ciphersuites that are available with both the
494 * default and fips providers to get the same output in both cases
496 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx
, "TLS_AES_128_GCM_SHA256")))
501 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, cert
,
503 QTEST_FLAG_FAKE_TIME
,
508 SSL_set_msg_callback(clientquic
, SSL_trace
);
509 SSL_set_msg_callback_arg(clientquic
, bio
);
511 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
514 if (!TEST_true(compare_with_file(bio
)))
519 ossl_quic_tserver_free(qtserv
);
520 SSL_free(clientquic
);
528 static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER
) *ciphers
)
532 /* Ensure ciphersuite list is suitably subsetted. */
533 for (i
= 0; i
< (size_t)sk_SSL_CIPHER_num(ciphers
); ++i
) {
534 const SSL_CIPHER
*cipher
= sk_SSL_CIPHER_value(ciphers
, i
);
535 switch (SSL_CIPHER_get_id(cipher
)) {
536 case TLS1_3_CK_AES_128_GCM_SHA256
:
537 case TLS1_3_CK_AES_256_GCM_SHA384
:
538 case TLS1_3_CK_CHACHA20_POLY1305_SHA256
:
541 TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher
));
550 * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
552 static int test_quic_forbidden_apis_ctx(void)
557 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
560 #ifndef OPENSSL_NO_SRTP
561 /* This function returns 0 on success and 1 on error, and should fail. */
562 if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx
, "SRTP_AEAD_AES_128_GCM")))
567 * List of ciphersuites we do and don't allow in QUIC.
569 #define QUIC_CIPHERSUITES \
570 "TLS_AES_128_GCM_SHA256:" \
571 "TLS_AES_256_GCM_SHA384:" \
572 "TLS_CHACHA20_POLY1305_SHA256"
574 #define NON_QUIC_CIPHERSUITES \
575 "TLS_AES_128_CCM_SHA256:" \
576 "TLS_AES_256_CCM_SHA384:" \
577 "TLS_AES_128_CCM_8_SHA256"
579 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
580 if (!TEST_true(SSL_CTX_set_ciphersuites(ctx
,
581 QUIC_CIPHERSUITES
":"
582 NON_QUIC_CIPHERSUITES
)))
586 * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only
587 * filtered in SSL_get1_supported_ciphers, so we don't check for
588 * non-inclusion here.
597 static int test_quic_forbidden_apis(void)
602 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
604 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
607 if (!TEST_ptr(ssl
= SSL_new(ctx
)))
610 #ifndef OPENSSL_NO_SRTP
611 /* This function returns 0 on success and 1 on error, and should fail. */
612 if (!TEST_true(SSL_set_tlsext_use_srtp(ssl
, "SRTP_AEAD_AES_128_GCM")))
616 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
617 if (!TEST_true(SSL_set_ciphersuites(ssl
,
618 QUIC_CIPHERSUITES
":"
619 NON_QUIC_CIPHERSUITES
)))
622 /* Non-QUIC ciphersuites must not appear in supported ciphers list. */
623 if (!TEST_ptr(ciphers
= SSL_get1_supported_ciphers(ssl
))
624 || !TEST_true(ensure_valid_ciphers(ciphers
)))
629 sk_SSL_CIPHER_free(ciphers
);
635 static int test_quic_forbidden_options(void)
643 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
646 /* QUIC options restrictions do not affect SSL_CTX */
647 SSL_CTX_set_options(ctx
, UINT64_MAX
);
649 if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx
), UINT64_MAX
))
652 /* Set options on CTX which should not be inherited (tested below). */
653 SSL_CTX_set_read_ahead(ctx
, 1);
654 SSL_CTX_set_max_early_data(ctx
, 1);
655 SSL_CTX_set_recv_max_early_data(ctx
, 1);
656 SSL_CTX_set_quiet_shutdown(ctx
, 1);
658 if (!TEST_ptr(ssl
= SSL_new(ctx
)))
661 /* Only permitted options get transferred to SSL object */
662 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), OSSL_QUIC_PERMITTED_OPTIONS
))
665 /* Try again using SSL_set_options */
666 SSL_set_options(ssl
, UINT64_MAX
);
668 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), OSSL_QUIC_PERMITTED_OPTIONS
))
671 /* Clear everything */
672 SSL_clear_options(ssl
, UINT64_MAX
);
674 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), 0))
678 if (!TEST_false(SSL_get_read_ahead(ssl
)))
681 SSL_set_read_ahead(ssl
, 1);
682 if (!TEST_false(SSL_get_read_ahead(ssl
)))
686 if (!TEST_true(SSL_set_block_padding(ssl
, 0))
687 || !TEST_true(SSL_set_block_padding(ssl
, 1))
688 || !TEST_false(SSL_set_block_padding(ssl
, 2)))
691 /* Max fragment length */
692 if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl
, TLSEXT_max_fragment_length_DISABLED
))
693 || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl
, TLSEXT_max_fragment_length_512
)))
697 if (!TEST_false(SSL_set_recv_max_early_data(ssl
, 1))
698 || !TEST_false(SSL_set_max_early_data(ssl
, 1)))
702 if (!TEST_false(SSL_read_early_data(ssl
, buf
, sizeof(buf
), &len
))
703 || !TEST_false(SSL_write_early_data(ssl
, buf
, sizeof(buf
), &len
)))
706 /* Buffer Management */
707 if (!TEST_true(SSL_alloc_buffers(ssl
))
708 || !TEST_false(SSL_free_buffers(ssl
)))
712 if (!TEST_false(SSL_set_max_send_fragment(ssl
, 2))
713 || !TEST_false(SSL_set_split_send_fragment(ssl
, 2))
714 || !TEST_false(SSL_set_max_pipelines(ssl
, 2)))
718 if (!TEST_false(SSL_stateless(ssl
)))
722 if (!TEST_false(SSL_get_quiet_shutdown(ssl
)))
726 if (!TEST_ptr_null(SSL_dup(ssl
)))
730 if (!TEST_false(SSL_clear(ssl
)))
740 static int test_quic_set_fd(int idx
)
745 int fd
= -1, resfd
= -1;
748 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
751 if (!TEST_ptr(ssl
= SSL_new(ctx
)))
754 if (!TEST_int_ge(fd
= BIO_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, 0), 0))
758 if (!TEST_true(SSL_set_fd(ssl
, fd
)))
760 if (!TEST_ptr(bio
= SSL_get_rbio(ssl
)))
762 if (!TEST_ptr_eq(bio
, SSL_get_wbio(ssl
)))
764 } else if (idx
== 1) {
765 if (!TEST_true(SSL_set_rfd(ssl
, fd
)))
767 if (!TEST_ptr(bio
= SSL_get_rbio(ssl
)))
769 if (!TEST_ptr_null(SSL_get_wbio(ssl
)))
772 if (!TEST_true(SSL_set_wfd(ssl
, fd
)))
774 if (!TEST_ptr(bio
= SSL_get_wbio(ssl
)))
776 if (!TEST_ptr_null(SSL_get_rbio(ssl
)))
780 if (!TEST_int_eq(BIO_method_type(bio
), BIO_TYPE_DGRAM
))
783 if (!TEST_true(BIO_get_fd(bio
, &resfd
))
784 || !TEST_int_eq(resfd
, fd
))
796 #define MAXLOOPS 1000
798 static int test_bio_ssl(void)
801 * We just use OSSL_QUIC_client_method() rather than
802 * OSSL_QUIC_client_thread_method(). We will never leave the connection idle
803 * so we will always be implicitly handling time events anyway via other
806 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
807 SSL
*clientquic
= NULL
, *stream
= NULL
;
808 QUIC_TSERVER
*qtserv
= NULL
;
810 BIO
*cbio
= NULL
, *strbio
= NULL
, *thisbio
;
811 const char *msg
= "Hello world";
812 int abortctr
= 0, err
, clienterr
= 0, servererr
= 0, retc
= 0, rets
= 0;
813 size_t written
, readbytes
, msglen
;
815 unsigned char buf
[80];
820 cbio
= BIO_new_ssl(cctx
, 1);
825 * We must configure the ALPN/peer address etc so we get the SSL object in
826 * order to pass it to qtest_create_quic_objects for configuration.
828 if (!TEST_int_eq(BIO_get_ssl(cbio
, &clientquic
), 1))
831 if (!TEST_true(qtest_create_quic_objects(libctx
, NULL
, NULL
, cert
, privkey
,
832 0, &qtserv
, &clientquic
, NULL
)))
835 msglen
= strlen(msg
);
838 err
= BIO_FLAGS_WRITE
;
839 while (!clienterr
&& !retc
&& err
== BIO_FLAGS_WRITE
) {
840 retc
= BIO_write_ex(cbio
, msg
, msglen
, &written
);
842 if (BIO_should_retry(cbio
))
843 err
= BIO_retry_type(cbio
);
849 if (!clienterr
&& retc
<= 0 && err
!= BIO_FLAGS_READ
) {
850 TEST_info("BIO_write_ex() failed %d, %d", retc
, err
);
851 TEST_openssl_errors();
855 if (!servererr
&& rets
<= 0) {
856 ossl_quic_tserver_tick(qtserv
);
857 servererr
= ossl_quic_tserver_is_term_any(qtserv
);
859 rets
= ossl_quic_tserver_is_handshake_confirmed(qtserv
);
862 if (clienterr
&& servererr
)
865 if (++abortctr
== MAXLOOPS
) {
866 TEST_info("No progress made");
869 } while ((!retc
&& !clienterr
) || (rets
<= 0 && !servererr
));
872 * 2 loops: The first using the default stream, and the second using a new
873 * client initiated bidi stream.
875 for (i
= 0, thisbio
= cbio
; i
< 2; i
++) {
876 if (!TEST_true(ossl_quic_tserver_read(qtserv
, sid
, buf
, sizeof(buf
),
878 || !TEST_mem_eq(msg
, msglen
, buf
, readbytes
))
881 if (!TEST_true(ossl_quic_tserver_write(qtserv
, sid
, (unsigned char *)msg
,
884 ossl_quic_tserver_tick(qtserv
);
886 if (!TEST_true(BIO_read_ex(thisbio
, buf
, sizeof(buf
), &readbytes
))
887 || !TEST_mem_eq(msg
, msglen
, buf
, readbytes
))
894 * Now create a new stream and repeat. The bottom two bits of the stream
895 * id represents whether the stream is bidi and whether it is client
896 * initiated or not. For client initiated bidi they are both 0. So the
897 * first client initiated bidi stream is 0 and the next one is 4.
900 stream
= SSL_new_stream(clientquic
, 0);
901 if (!TEST_ptr(stream
))
904 thisbio
= strbio
= BIO_new(BIO_f_ssl());
905 if (!TEST_ptr(strbio
))
908 if (!TEST_int_eq(BIO_set_ssl(thisbio
, stream
, BIO_CLOSE
), 1))
912 if (!TEST_true(BIO_write_ex(thisbio
, msg
, msglen
, &written
)))
915 ossl_quic_tserver_tick(qtserv
);
921 BIO_free_all(strbio
);
923 ossl_quic_tserver_free(qtserv
);
929 #define BACK_PRESSURE_NUM_LOOPS 10000
931 * Test that sending data from the client to the server faster than the server
932 * can process it eventually results in back pressure on the client.
934 static int test_back_pressure(void)
936 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
937 SSL
*clientquic
= NULL
;
938 QUIC_TSERVER
*qtserv
= NULL
;
940 unsigned char *msg
= NULL
;
941 const size_t msglen
= 1024;
942 unsigned char buf
[64];
943 size_t readbytes
, written
;
947 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, cert
,
950 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
953 msg
= OPENSSL_malloc(msglen
);
956 if (!TEST_int_eq(RAND_bytes_ex(libctx
, msg
, msglen
, 0), 1))
960 * Limit to 10000 loops. If we've not seen any back pressure after that
961 * we're going to run out of memory, so abort.
963 for (i
= 0; i
< BACK_PRESSURE_NUM_LOOPS
; i
++) {
964 /* Send data from the client */
965 if (!SSL_write_ex(clientquic
, msg
, msglen
, &written
)) {
966 /* Check if we are seeing back pressure */
967 if (SSL_get_error(clientquic
, 0) == SSL_ERROR_WANT_WRITE
)
969 TEST_error("Unexpected client failure");
973 /* Receive data at the server */
974 ossl_quic_tserver_tick(qtserv
);
975 if (!TEST_true(ossl_quic_tserver_read(qtserv
, 0, buf
, sizeof(buf
),
980 if (i
== BACK_PRESSURE_NUM_LOOPS
) {
981 TEST_error("No back pressure seen");
987 SSL_free(clientquic
);
988 ossl_quic_tserver_free(qtserv
);
996 static int dgram_ctr
= 0;
998 static void dgram_cb(int write_p
, int version
, int content_type
,
999 const void *buf
, size_t msglen
, SSL
*ssl
, void *arg
)
1004 if (content_type
!= SSL3_RT_QUIC_DATAGRAM
)
1010 /* Test that we send multiple datagrams in one go when appropriate */
1011 static int test_multiple_dgrams(void)
1013 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
1014 SSL
*clientquic
= NULL
;
1015 QUIC_TSERVER
*qtserv
= NULL
;
1018 const size_t buflen
= 1400;
1021 buf
= OPENSSL_zalloc(buflen
);
1025 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, cert
,
1026 privkey
, 0, &qtserv
,
1028 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
1032 SSL_set_msg_callback(clientquic
, dgram_cb
);
1033 if (!TEST_true(SSL_write_ex(clientquic
, buf
, buflen
, &written
))
1034 || !TEST_size_t_eq(written
, buflen
)
1035 /* We wrote enough data for 2 datagrams */
1036 || !TEST_int_eq(dgram_ctr
, 2))
1042 SSL_free(clientquic
);
1043 ossl_quic_tserver_free(qtserv
);
1049 static int non_io_retry_cert_verify_cb(X509_STORE_CTX
*ctx
, void *arg
)
1051 int idx
= SSL_get_ex_data_X509_STORE_CTX_idx();
1053 int *ctr
= (int *)arg
;
1055 /* this should not happen but check anyway */
1057 || (ssl
= X509_STORE_CTX_get_ex_data(ctx
, idx
)) == NULL
)
1060 /* If this is the first time we've been called then retry */
1061 if (((*ctr
)++) == 0)
1062 return SSL_set_retry_verify(ssl
);
1064 /* Otherwise do nothing - verification succeeds. Continue as normal */
1068 /* Test that we can handle a non-io related retry error
1069 * Test 0: Non-blocking
1072 static int test_non_io_retry(int idx
)
1075 SSL
*clientquic
= NULL
;
1076 QUIC_TSERVER
*qtserv
= NULL
;
1078 int flags
= 0, ctr
= 0;
1080 if (idx
>= 1 && !qtest_supports_blocking())
1081 return TEST_skip("Blocking tests not supported in this build");
1083 cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
1084 if (!TEST_ptr(cctx
))
1087 SSL_CTX_set_cert_verify_callback(cctx
, non_io_retry_cert_verify_cb
, &ctr
);
1089 flags
= (idx
>= 1) ? QTEST_FLAG_BLOCK
: 0;
1090 if (!TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, cert
, privkey
,
1091 flags
, &qtserv
, &clientquic
, NULL
))
1092 || !TEST_true(qtest_create_quic_connection_ex(qtserv
, clientquic
,
1093 SSL_ERROR_WANT_RETRY_VERIFY
))
1094 || !TEST_int_eq(SSL_want(clientquic
), SSL_RETRY_VERIFY
)
1095 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
1100 SSL_free(clientquic
);
1101 ossl_quic_tserver_free(qtserv
);
1107 static int use_session_cb_cnt
= 0;
1108 static int find_session_cb_cnt
= 0;
1109 static const char *pskid
= "Identity";
1110 static SSL_SESSION
*serverpsk
= NULL
, *clientpsk
= NULL
;
1112 static int use_session_cb(SSL
*ssl
, const EVP_MD
*md
, const unsigned char **id
,
1113 size_t *idlen
, SSL_SESSION
**sess
)
1115 use_session_cb_cnt
++;
1117 if (clientpsk
== NULL
)
1120 SSL_SESSION_up_ref(clientpsk
);
1123 *id
= (const unsigned char *)pskid
;
1124 *idlen
= strlen(pskid
);
1129 static int find_session_cb(SSL
*ssl
, const unsigned char *identity
,
1130 size_t identity_len
, SSL_SESSION
**sess
)
1132 find_session_cb_cnt
++;
1134 if (serverpsk
== NULL
)
1137 /* Identity should match that set by the client */
1138 if (strlen(pskid
) != identity_len
1139 || strncmp(pskid
, (const char *)identity
, identity_len
) != 0)
1142 SSL_SESSION_up_ref(serverpsk
);
1148 static int test_quic_psk(void)
1150 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
1151 SSL
*clientquic
= NULL
;
1152 QUIC_TSERVER
*qtserv
= NULL
;
1156 /* No cert or private key for the server, i.e. PSK only */
1157 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, NULL
, NULL
,
1159 &clientquic
, NULL
)))
1162 SSL_set_psk_use_session_callback(clientquic
, use_session_cb
);
1163 ossl_quic_tserver_set_psk_find_session_cb(qtserv
, find_session_cb
);
1164 use_session_cb_cnt
= 0;
1165 find_session_cb_cnt
= 0;
1167 clientpsk
= serverpsk
= create_a_psk(clientquic
, SHA384_DIGEST_LENGTH
);
1168 if (!TEST_ptr(clientpsk
))
1170 /* We already had one ref. Add another one */
1171 SSL_SESSION_up_ref(clientpsk
);
1173 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
))
1174 || !TEST_int_eq(1, find_session_cb_cnt
)
1175 || !TEST_int_eq(1, use_session_cb_cnt
)
1176 /* Check that we actually used the PSK */
1177 || !TEST_true(SSL_session_reused(clientquic
)))
1183 SSL_free(clientquic
);
1184 ossl_quic_tserver_free(qtserv
);
1186 SSL_SESSION_free(clientpsk
);
1187 SSL_SESSION_free(serverpsk
);
1188 clientpsk
= serverpsk
= NULL
;
1193 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
1195 int setup_tests(void)
1200 libctx
= OSSL_LIB_CTX_new();
1201 if (!TEST_ptr(libctx
))
1204 defctxnull
= OSSL_PROVIDER_load(NULL
, "null");
1207 * Verify that the default and fips providers in the default libctx are not
1210 if (!TEST_false(OSSL_PROVIDER_available(NULL
, "default"))
1211 || !TEST_false(OSSL_PROVIDER_available(NULL
, "fips")))
1214 if (!test_skip_common_options()) {
1215 TEST_error("Error parsing test options\n");
1219 if (!TEST_ptr(modulename
= test_get_argument(0))
1220 || !TEST_ptr(configfile
= test_get_argument(1))
1221 || !TEST_ptr(certsdir
= test_get_argument(2))
1222 || !TEST_ptr(datadir
= test_get_argument(3)))
1225 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx
, configfile
)))
1228 /* Check we have the expected provider available */
1229 if (!TEST_true(OSSL_PROVIDER_available(libctx
, modulename
)))
1232 /* Check the default provider is not available */
1233 if (strcmp(modulename
, "default") != 0
1234 && !TEST_false(OSSL_PROVIDER_available(libctx
, "default")))
1237 if (strcmp(modulename
, "fips") == 0)
1240 cert
= test_mk_file_path(certsdir
, "servercert.pem");
1244 privkey
= test_mk_file_path(certsdir
, "serverkey.pem");
1245 if (privkey
== NULL
)
1248 ADD_ALL_TESTS(test_quic_write_read
, 3);
1249 ADD_TEST(test_fin_only_blocking
);
1250 ADD_TEST(test_ciphersuites
);
1251 ADD_TEST(test_cipher_find
);
1252 ADD_TEST(test_version
);
1253 #if defined(DO_SSL_TRACE_TEST)
1254 ADD_TEST(test_ssl_trace
);
1256 ADD_TEST(test_quic_forbidden_apis_ctx
);
1257 ADD_TEST(test_quic_forbidden_apis
);
1258 ADD_TEST(test_quic_forbidden_options
);
1259 ADD_ALL_TESTS(test_quic_set_fd
, 3);
1260 ADD_TEST(test_bio_ssl
);
1261 ADD_TEST(test_back_pressure
);
1262 ADD_TEST(test_multiple_dgrams
);
1263 ADD_ALL_TESTS(test_non_io_retry
, 2);
1264 ADD_TEST(test_quic_psk
);
1271 void cleanup_tests(void)
1274 OPENSSL_free(privkey
);
1275 OSSL_PROVIDER_unload(defctxnull
);
1276 OSSL_LIB_CTX_free(libctx
);