2 * Copyright 2022 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>
16 #include "helpers/ssltestlib.h"
17 #include "helpers/quictestlib.h"
19 #include "testutil/output.h"
20 #include "../ssl/ssl_local.h"
22 static OSSL_LIB_CTX
*libctx
= NULL
;
23 static OSSL_PROVIDER
*defctxnull
= NULL
;
24 static char *certsdir
= NULL
;
25 static char *cert
= NULL
;
26 static char *privkey
= NULL
;
27 static char *datadir
= NULL
;
29 static int is_fips
= 0;
32 * Test that we read what we've written.
33 * Test 0: Non-blocking
36 static int test_quic_write_read(int idx
)
38 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
39 SSL
*clientquic
= NULL
;
40 QUIC_TSERVER
*qtserv
= NULL
;
42 unsigned char buf
[20];
43 static char *msg
= "A test message";
44 size_t msglen
= strlen(msg
);
46 int ssock
= 0, csock
= 0;
47 uint64_t sid
= UINT64_MAX
;
49 if (idx
== 1 && !qtest_supports_blocking())
50 return TEST_skip("Blocking tests not supported in this build");
53 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
54 idx
, &qtserv
, &clientquic
,
56 || !TEST_true(SSL_set_tlsext_host_name(clientquic
, "localhost"))
57 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
61 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv
), &ssock
)))
63 if (!TEST_int_gt(csock
= SSL_get_rfd(clientquic
), 0))
67 sid
= 0; /* client-initiated bidirectional stream */
69 for (j
= 0; j
< 2; j
++) {
70 /* Check that sending and receiving app data is ok */
71 if (!TEST_true(SSL_write_ex(clientquic
, msg
, msglen
, &numbytes
))
72 || !TEST_size_t_eq(numbytes
, msglen
))
76 if (!TEST_true(wait_until_sock_readable(ssock
)))
79 ossl_quic_tserver_tick(qtserv
);
81 if (!TEST_true(ossl_quic_tserver_read(qtserv
, sid
, buf
, sizeof(buf
),
84 } while (numbytes
== 0);
86 if (!TEST_mem_eq(buf
, numbytes
, msg
, msglen
))
90 ossl_quic_tserver_tick(qtserv
);
91 if (!TEST_true(ossl_quic_tserver_write(qtserv
, sid
, (unsigned char *)msg
,
94 ossl_quic_tserver_tick(qtserv
);
95 SSL_handle_events(clientquic
);
97 * In blocking mode the SSL_read_ex call will block until the socket is
98 * readable and has our data. In non-blocking mode we're doing everything
99 * in memory, so it should be immediately available
101 if (!TEST_true(SSL_read_ex(clientquic
, buf
, 1, &numbytes
))
102 || !TEST_size_t_eq(numbytes
, 1)
103 || !TEST_true(SSL_has_pending(clientquic
))
104 || !TEST_int_eq(SSL_pending(clientquic
), msglen
- 1)
105 || !TEST_true(SSL_read_ex(clientquic
, buf
+ 1, sizeof(buf
) - 1, &numbytes
))
106 || !TEST_mem_eq(buf
, numbytes
+ 1, msg
, msglen
))
110 if (!TEST_true(qtest_shutdown(qtserv
, clientquic
)))
116 ossl_quic_tserver_free(qtserv
);
117 SSL_free(clientquic
);
123 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
124 static int test_ciphersuites(void)
126 SSL_CTX
*ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
129 const STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
130 const SSL_CIPHER
*cipher
;
131 /* We expect this exact list of ciphersuites by default */
133 TLS1_3_CK_AES_256_GCM_SHA384
,
134 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
135 TLS1_3_CK_CHACHA20_POLY1305_SHA256
,
137 TLS1_3_CK_AES_128_GCM_SHA256
148 ciphers
= SSL_get_ciphers(ssl
);
150 for (i
= 0, j
= 0; i
< OSSL_NELEM(cipherids
); i
++) {
151 if (cipherids
[i
] == TLS1_3_CK_CHACHA20_POLY1305_SHA256
&& is_fips
)
153 cipher
= sk_SSL_CIPHER_value(ciphers
, j
++);
154 if (!TEST_ptr(cipher
))
156 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher
), cipherids
[i
]))
160 /* We should have checked all the ciphers in the stack */
161 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers
), j
))
173 * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
174 * SSL_is_dtls return the expected results for a QUIC connection. Compare with
175 * test_version() in sslapitest.c which does the same thing for TLS/DTLS
178 static int test_version(void)
180 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
181 SSL
*clientquic
= NULL
;
182 QUIC_TSERVER
*qtserv
= NULL
;
186 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
187 0, &qtserv
, &clientquic
,
189 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
192 if (!TEST_int_eq(SSL_version(clientquic
), OSSL_QUIC1_VERSION
)
193 || !TEST_str_eq(SSL_get_version(clientquic
), "QUICv1"))
196 if (!TEST_true(SSL_is_quic(clientquic
))
197 || !TEST_false(SSL_is_tls(clientquic
))
198 || !TEST_false(SSL_is_dtls(clientquic
)))
204 ossl_quic_tserver_free(qtserv
);
205 SSL_free(clientquic
);
211 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
212 static void strip_line_ends(char *str
)
216 for (i
= strlen(str
);
217 i
> 0 && (str
[i
- 1] == '\n' || str
[i
- 1] == '\r');
223 static int compare_with_file(BIO
*membio
)
226 char buf1
[512], buf2
[512];
231 reffile
= test_mk_file_path(datadir
, "ssltraceref.txt");
232 if (!TEST_ptr(reffile
))
235 file
= BIO_new_file(reffile
, "rb");
239 while (BIO_gets(file
, buf1
, sizeof(buf1
)) > 0) {
240 if (BIO_gets(membio
, buf2
, sizeof(buf2
)) <= 0) {
241 TEST_error("Failed reading mem data");
244 strip_line_ends(buf1
);
245 strip_line_ends(buf2
);
246 if (strlen(buf1
) != strlen(buf2
)) {
247 TEST_error("Actual and ref line data length mismatch");
248 TEST_info("%s", buf1
);
249 TEST_info("%s", buf2
);
252 for (i
= 0; i
< strlen(buf1
); i
++) {
253 /* '?' is a wild card character in the reference text */
257 if (!TEST_str_eq(buf1
, buf2
))
260 if (!TEST_true(BIO_eof(file
))
261 || !TEST_true(BIO_eof(membio
)))
266 OPENSSL_free(reffile
);
272 * Tests that the SSL_trace() msg_callback works as expected with a QUIC
273 * connection. This also provides testing of the msg_callback at the same time.
275 static int test_ssl_trace(void)
277 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
278 SSL
*clientquic
= NULL
;
279 QUIC_TSERVER
*qtserv
= NULL
;
281 BIO
*bio
= BIO_new(BIO_s_mem());
284 * Ensure we only configure ciphersuites that are available with both the
285 * default and fips providers to get the same output in both cases
287 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx
, "TLS_AES_128_GCM_SHA256")))
292 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
293 0, &qtserv
, &clientquic
,
297 SSL_set_msg_callback(clientquic
, SSL_trace
);
298 SSL_set_msg_callback_arg(clientquic
, bio
);
300 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
303 if (!TEST_true(compare_with_file(bio
)))
308 ossl_quic_tserver_free(qtserv
);
309 SSL_free(clientquic
);
317 static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER
) *ciphers
)
321 /* Ensure ciphersuite list is suitably subsetted. */
322 for (i
= 0; i
< (size_t)sk_SSL_CIPHER_num(ciphers
); ++i
) {
323 const SSL_CIPHER
*cipher
= sk_SSL_CIPHER_value(ciphers
, i
);
324 switch (SSL_CIPHER_get_id(cipher
)) {
325 case TLS1_3_CK_AES_128_GCM_SHA256
:
326 case TLS1_3_CK_AES_256_GCM_SHA384
:
327 case TLS1_3_CK_CHACHA20_POLY1305_SHA256
:
330 TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher
));
339 * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
341 static int test_quic_forbidden_apis_ctx(void)
346 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
349 /* This function returns 0 on success and 1 on error, and should fail. */
350 if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx
, "SRTP_AEAD_AES_128_GCM")))
354 * List of ciphersuites we do and don't allow in QUIC.
356 #define QUIC_CIPHERSUITES \
357 "TLS_AES_128_GCM_SHA256:" \
358 "TLS_AES_256_GCM_SHA384:" \
359 "TLS_CHACHA20_POLY1305_SHA256"
361 #define NON_QUIC_CIPHERSUITES \
362 "TLS_AES_128_CCM_SHA256:" \
363 "TLS_AES_256_CCM_SHA384:" \
364 "TLS_AES_128_CCM_8_SHA256"
366 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
367 if (!TEST_true(SSL_CTX_set_ciphersuites(ctx
,
368 QUIC_CIPHERSUITES
":"
369 NON_QUIC_CIPHERSUITES
)))
373 * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only
374 * filtered in SSL_get1_supported_ciphers, so we don't check for
375 * non-inclusion here.
384 static int test_quic_forbidden_apis(void)
389 STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
391 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
394 if (!TEST_ptr(ssl
= SSL_new(ctx
)))
397 /* This function returns 0 on success and 1 on error, and should fail. */
398 if (!TEST_true(SSL_set_tlsext_use_srtp(ssl
, "SRTP_AEAD_AES_128_GCM")))
401 /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
402 if (!TEST_true(SSL_set_ciphersuites(ssl
,
403 QUIC_CIPHERSUITES
":"
404 NON_QUIC_CIPHERSUITES
)))
407 /* Non-QUIC ciphersuites must not appear in supported ciphers list. */
408 if (!TEST_ptr(ciphers
= SSL_get1_supported_ciphers(ssl
))
409 || !TEST_true(ensure_valid_ciphers(ciphers
)))
414 sk_SSL_CIPHER_free(ciphers
);
420 static int test_quic_forbidden_options(void)
428 if (!TEST_ptr(ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method())))
431 /* QUIC options restrictions do not affect SSL_CTX */
432 SSL_CTX_set_options(ctx
, UINT64_MAX
);
434 if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx
), UINT64_MAX
))
437 /* Set options on CTX which should not be inherited (tested below). */
438 SSL_CTX_set_read_ahead(ctx
, 1);
439 SSL_CTX_set_max_early_data(ctx
, 1);
440 SSL_CTX_set_recv_max_early_data(ctx
, 1);
442 if (!TEST_ptr(ssl
= SSL_new(ctx
)))
445 /* Only permitted options get transferred to SSL object */
446 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), OSSL_QUIC_PERMITTED_OPTIONS
))
449 /* Try again using SSL_set_options */
450 SSL_set_options(ssl
, UINT64_MAX
);
452 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), OSSL_QUIC_PERMITTED_OPTIONS
))
455 /* Clear everything */
456 SSL_clear_options(ssl
, UINT64_MAX
);
458 if (!TEST_uint64_t_eq(SSL_get_options(ssl
), 0))
462 if (!TEST_false(SSL_get_read_ahead(ssl
)))
465 SSL_set_read_ahead(ssl
, 1);
466 if (!TEST_false(SSL_get_read_ahead(ssl
)))
470 if (!TEST_true(SSL_set_block_padding(ssl
, 0))
471 || !TEST_true(SSL_set_block_padding(ssl
, 1))
472 || !TEST_false(SSL_set_block_padding(ssl
, 2)))
475 /* Max fragment length */
476 if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl
, TLSEXT_max_fragment_length_DISABLED
))
477 || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl
, TLSEXT_max_fragment_length_512
)))
481 if (!TEST_false(SSL_get_recv_max_early_data(ssl
))
482 || !TEST_false(SSL_get_max_early_data(ssl
))
483 || !TEST_false(SSL_set_recv_max_early_data(ssl
, 1))
484 || !TEST_false(SSL_set_max_early_data(ssl
, 1)))
488 if (!TEST_false(SSL_read_early_data(ssl
, buf
, sizeof(buf
), &len
))
489 || !TEST_false(SSL_write_early_data(ssl
, buf
, sizeof(buf
), &len
)))
492 /* Buffer Management */
493 if (!TEST_true(SSL_allocate_buffers(ssl
))
494 || !TEST_false(SSL_free_buffers(ssl
)))
504 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
506 int setup_tests(void)
511 libctx
= OSSL_LIB_CTX_new();
512 if (!TEST_ptr(libctx
))
515 defctxnull
= OSSL_PROVIDER_load(NULL
, "null");
518 * Verify that the default and fips providers in the default libctx are not
521 if (!TEST_false(OSSL_PROVIDER_available(NULL
, "default"))
522 || !TEST_false(OSSL_PROVIDER_available(NULL
, "fips")))
525 if (!test_skip_common_options()) {
526 TEST_error("Error parsing test options\n");
530 if (!TEST_ptr(modulename
= test_get_argument(0))
531 || !TEST_ptr(configfile
= test_get_argument(1))
532 || !TEST_ptr(certsdir
= test_get_argument(2))
533 || !TEST_ptr(datadir
= test_get_argument(3)))
536 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx
, configfile
)))
539 /* Check we have the expected provider available */
540 if (!TEST_true(OSSL_PROVIDER_available(libctx
, modulename
)))
543 /* Check the default provider is not available */
544 if (strcmp(modulename
, "default") != 0
545 && !TEST_false(OSSL_PROVIDER_available(libctx
, "default")))
548 if (strcmp(modulename
, "fips") == 0)
551 cert
= test_mk_file_path(certsdir
, "servercert.pem");
555 privkey
= test_mk_file_path(certsdir
, "serverkey.pem");
559 ADD_ALL_TESTS(test_quic_write_read
, 2);
560 ADD_TEST(test_ciphersuites
);
561 ADD_TEST(test_version
);
562 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
563 ADD_TEST(test_ssl_trace
);
565 ADD_TEST(test_quic_forbidden_apis_ctx
);
566 ADD_TEST(test_quic_forbidden_apis
);
567 ADD_TEST(test_quic_forbidden_options
);
574 void cleanup_tests(void)
577 OPENSSL_free(privkey
);
578 OSSL_PROVIDER_unload(defctxnull
);
579 OSSL_LIB_CTX_free(libctx
);