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"
21 static OSSL_LIB_CTX
*libctx
= NULL
;
22 static OSSL_PROVIDER
*defctxnull
= NULL
;
23 static char *certsdir
= NULL
;
24 static char *cert
= NULL
;
25 static char *privkey
= NULL
;
26 static char *datadir
= NULL
;
28 static int is_fips
= 0;
31 * Test that we read what we've written.
32 * Test 0: Non-blocking
35 static int test_quic_write_read(int idx
)
37 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
38 SSL
*clientquic
= NULL
;
39 QUIC_TSERVER
*qtserv
= NULL
;
41 unsigned char buf
[20];
42 static char *msg
= "A test message";
43 size_t msglen
= strlen(msg
);
45 int ssock
= 0, csock
= 0;
46 uint64_t sid
= UINT64_MAX
;
48 if (idx
== 1 && !qtest_supports_blocking())
49 return TEST_skip("Blocking tests not supported in this build");
52 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
53 idx
, &qtserv
, &clientquic
,
55 || !TEST_true(SSL_set_tlsext_host_name(clientquic
, "localhost"))
56 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
60 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv
), &ssock
)))
62 if (!TEST_int_gt(csock
= SSL_get_rfd(clientquic
), 0))
66 sid
= 0; /* client-initiated bidirectional stream */
68 for (j
= 0; j
< 2; j
++) {
69 /* Check that sending and receiving app data is ok */
70 if (!TEST_true(SSL_write_ex(clientquic
, msg
, msglen
, &numbytes
))
71 || !TEST_size_t_eq(numbytes
, msglen
))
75 if (!TEST_true(wait_until_sock_readable(ssock
)))
78 ossl_quic_tserver_tick(qtserv
);
80 if (!TEST_true(ossl_quic_tserver_read(qtserv
, sid
, buf
, sizeof(buf
),
83 } while (numbytes
== 0);
85 if (!TEST_mem_eq(buf
, numbytes
, msg
, msglen
))
89 ossl_quic_tserver_tick(qtserv
);
90 if (!TEST_true(ossl_quic_tserver_write(qtserv
, sid
, (unsigned char *)msg
,
93 ossl_quic_tserver_tick(qtserv
);
94 SSL_handle_events(clientquic
);
96 * In blocking mode the SSL_read_ex call will block until the socket is
97 * readable and has our data. In non-blocking mode we're doing everything
98 * in memory, so it should be immediately available
100 if (!TEST_true(SSL_read_ex(clientquic
, buf
, 1, &numbytes
))
101 || !TEST_size_t_eq(numbytes
, 1)
102 || !TEST_true(SSL_has_pending(clientquic
))
103 || !TEST_int_eq(SSL_pending(clientquic
), msglen
- 1)
104 || !TEST_true(SSL_read_ex(clientquic
, buf
+ 1, sizeof(buf
) - 1, &numbytes
))
105 || !TEST_mem_eq(buf
, numbytes
+ 1, msg
, msglen
))
109 if (!TEST_true(qtest_shutdown(qtserv
, clientquic
)))
115 ossl_quic_tserver_free(qtserv
);
116 SSL_free(clientquic
);
122 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
123 static int test_ciphersuites(void)
125 SSL_CTX
*ctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
128 const STACK_OF(SSL_CIPHER
) *ciphers
= NULL
;
129 const SSL_CIPHER
*cipher
;
130 /* We expect this exact list of ciphersuites by default */
132 TLS1_3_CK_AES_256_GCM_SHA384
,
133 #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
134 TLS1_3_CK_CHACHA20_POLY1305_SHA256
,
136 TLS1_3_CK_AES_128_GCM_SHA256
147 ciphers
= SSL_get_ciphers(ssl
);
149 for (i
= 0, j
= 0; i
< OSSL_NELEM(cipherids
); i
++) {
150 if (cipherids
[i
] == TLS1_3_CK_CHACHA20_POLY1305_SHA256
&& is_fips
)
152 cipher
= sk_SSL_CIPHER_value(ciphers
, j
++);
153 if (!TEST_ptr(cipher
))
155 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher
), cipherids
[i
]))
159 /* We should have checked all the ciphers in the stack */
160 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers
), j
))
172 * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
173 * SSL_is_dtls return the expected results for a QUIC connection. Compare with
174 * test_version() in sslapitest.c which does the same thing for TLS/DTLS
177 static int test_version(void)
179 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
180 SSL
*clientquic
= NULL
;
181 QUIC_TSERVER
*qtserv
= NULL
;
185 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
186 0, &qtserv
, &clientquic
,
188 || !TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
191 if (!TEST_int_eq(SSL_version(clientquic
), OSSL_QUIC1_VERSION
)
192 || !TEST_str_eq(SSL_get_version(clientquic
), "QUICv1"))
195 if (!TEST_true(SSL_is_quic(clientquic
))
196 || !TEST_false(SSL_is_tls(clientquic
))
197 || !TEST_false(SSL_is_dtls(clientquic
)))
203 ossl_quic_tserver_free(qtserv
);
204 SSL_free(clientquic
);
210 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
211 static void strip_line_ends(char *str
)
215 for (i
= strlen(str
);
216 i
> 0 && (str
[i
- 1] == '\n' || str
[i
- 1] == '\r');
222 static int compare_with_file(BIO
*membio
)
225 char buf1
[512], buf2
[512];
230 reffile
= test_mk_file_path(datadir
, "ssltraceref.txt");
231 if (!TEST_ptr(reffile
))
234 file
= BIO_new_file(reffile
, "rb");
238 while (BIO_gets(file
, buf1
, sizeof(buf1
)) > 0) {
239 if (BIO_gets(membio
, buf2
, sizeof(buf2
)) <= 0) {
240 TEST_error("Failed reading mem data");
243 strip_line_ends(buf1
);
244 strip_line_ends(buf2
);
245 if (strlen(buf1
) != strlen(buf2
)) {
246 TEST_error("Actual and ref line data length mismatch");
247 TEST_info("%s", buf1
);
248 TEST_info("%s", buf2
);
251 for (i
= 0; i
< strlen(buf1
); i
++) {
252 /* '?' is a wild card character in the reference text */
256 if (!TEST_str_eq(buf1
, buf2
))
259 if (!TEST_true(BIO_eof(file
))
260 || !TEST_true(BIO_eof(membio
)))
265 OPENSSL_free(reffile
);
271 * Tests that the SSL_trace() msg_callback works as expected with a QUIC
272 * connection. This also provides testing of the msg_callback at the same time.
274 static int test_ssl_trace(void)
276 SSL_CTX
*cctx
= SSL_CTX_new_ex(libctx
, NULL
, OSSL_QUIC_client_method());
277 SSL
*clientquic
= NULL
;
278 QUIC_TSERVER
*qtserv
= NULL
;
280 BIO
*bio
= BIO_new(BIO_s_mem());
283 * Ensure we only configure ciphersuites that are available with both the
284 * default and fips providers to get the same output in both cases
286 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx
, "TLS_AES_128_GCM_SHA256")))
291 || !TEST_true(qtest_create_quic_objects(libctx
, cctx
, cert
, privkey
,
292 0, &qtserv
, &clientquic
,
296 SSL_set_msg_callback(clientquic
, SSL_trace
);
297 SSL_set_msg_callback_arg(clientquic
, bio
);
299 if (!TEST_true(qtest_create_quic_connection(qtserv
, clientquic
)))
302 if (!TEST_true(compare_with_file(bio
)))
307 ossl_quic_tserver_free(qtserv
);
308 SSL_free(clientquic
);
316 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
318 int setup_tests(void)
323 libctx
= OSSL_LIB_CTX_new();
324 if (!TEST_ptr(libctx
))
327 defctxnull
= OSSL_PROVIDER_load(NULL
, "null");
330 * Verify that the default and fips providers in the default libctx are not
333 if (!TEST_false(OSSL_PROVIDER_available(NULL
, "default"))
334 || !TEST_false(OSSL_PROVIDER_available(NULL
, "fips")))
337 if (!test_skip_common_options()) {
338 TEST_error("Error parsing test options\n");
342 if (!TEST_ptr(modulename
= test_get_argument(0))
343 || !TEST_ptr(configfile
= test_get_argument(1))
344 || !TEST_ptr(certsdir
= test_get_argument(2))
345 || !TEST_ptr(datadir
= test_get_argument(3)))
348 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx
, configfile
)))
351 /* Check we have the expected provider available */
352 if (!TEST_true(OSSL_PROVIDER_available(libctx
, modulename
)))
355 /* Check the default provider is not available */
356 if (strcmp(modulename
, "default") != 0
357 && !TEST_false(OSSL_PROVIDER_available(libctx
, "default")))
360 if (strcmp(modulename
, "fips") == 0)
363 cert
= test_mk_file_path(certsdir
, "servercert.pem");
367 privkey
= test_mk_file_path(certsdir
, "serverkey.pem");
371 ADD_ALL_TESTS(test_quic_write_read
, 2);
372 ADD_TEST(test_ciphersuites
);
373 ADD_TEST(test_version
);
374 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
375 ADD_TEST(test_ssl_trace
);
384 void cleanup_tests(void)
387 OPENSSL_free(privkey
);
388 OSSL_PROVIDER_unload(defctxnull
);
389 OSSL_LIB_CTX_free(libctx
);