]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quicapitest.c
QUIC SSL: Buffer Management
[thirdparty/openssl.git] / test / quicapitest.c
1 /*
2 * Copyright 2022 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
16 #include "helpers/ssltestlib.h"
17 #include "helpers/quictestlib.h"
18 #include "testutil.h"
19 #include "testutil/output.h"
20 #include "../ssl/ssl_local.h"
21
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;
28
29 static int is_fips = 0;
30
31 /*
32 * Test that we read what we've written.
33 * Test 0: Non-blocking
34 * Test 1: Blocking
35 */
36 static int test_quic_write_read(int idx)
37 {
38 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
39 SSL *clientquic = NULL;
40 QUIC_TSERVER *qtserv = NULL;
41 int j, ret = 0;
42 unsigned char buf[20];
43 static char *msg = "A test message";
44 size_t msglen = strlen(msg);
45 size_t numbytes = 0;
46 int ssock = 0, csock = 0;
47 uint64_t sid = UINT64_MAX;
48
49 if (idx == 1 && !qtest_supports_blocking())
50 return TEST_skip("Blocking tests not supported in this build");
51
52 if (!TEST_ptr(cctx)
53 || !TEST_true(qtest_create_quic_objects(libctx, cctx, cert, privkey,
54 idx, &qtserv, &clientquic,
55 NULL))
56 || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost"))
57 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
58 goto end;
59
60 if (idx == 1) {
61 if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv), &ssock)))
62 goto end;
63 if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0))
64 goto end;
65 }
66
67 sid = 0; /* client-initiated bidirectional stream */
68
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))
73 goto end;
74 if (idx == 1) {
75 do {
76 if (!TEST_true(wait_until_sock_readable(ssock)))
77 goto end;
78
79 ossl_quic_tserver_tick(qtserv);
80
81 if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf),
82 &numbytes)))
83 goto end;
84 } while (numbytes == 0);
85
86 if (!TEST_mem_eq(buf, numbytes, msg, msglen))
87 goto end;
88 }
89
90 ossl_quic_tserver_tick(qtserv);
91 if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg,
92 msglen, &numbytes)))
93 goto end;
94 ossl_quic_tserver_tick(qtserv);
95 SSL_handle_events(clientquic);
96 /*
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
100 */
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))
107 goto end;
108 }
109
110 if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
111 goto end;
112
113 ret = 1;
114
115 end:
116 ossl_quic_tserver_free(qtserv);
117 SSL_free(clientquic);
118 SSL_CTX_free(cctx);
119
120 return ret;
121 }
122
123 /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
124 static int test_ciphersuites(void)
125 {
126 SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
127 SSL *ssl;
128 int testresult = 0;
129 const STACK_OF(SSL_CIPHER) *ciphers = NULL;
130 const SSL_CIPHER *cipher;
131 /* We expect this exact list of ciphersuites by default */
132 int cipherids[] = {
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,
136 #endif
137 TLS1_3_CK_AES_128_GCM_SHA256
138 };
139 size_t i, j;
140
141 if (!TEST_ptr(ctx))
142 return 0;
143
144 ssl = SSL_new(ctx);
145 if (!TEST_ptr(ssl))
146 goto err;
147
148 ciphers = SSL_get_ciphers(ssl);
149
150 for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) {
151 if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips)
152 continue;
153 cipher = sk_SSL_CIPHER_value(ciphers, j++);
154 if (!TEST_ptr(cipher))
155 goto err;
156 if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i]))
157 goto err;
158 }
159
160 /* We should have checked all the ciphers in the stack */
161 if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j))
162 goto err;
163
164 testresult = 1;
165 err:
166 SSL_free(ssl);
167 SSL_CTX_free(ctx);
168
169 return testresult;
170 }
171
172 /*
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
176 * connections.
177 */
178 static int test_version(void)
179 {
180 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
181 SSL *clientquic = NULL;
182 QUIC_TSERVER *qtserv = NULL;
183 int testresult = 0;
184
185 if (!TEST_ptr(cctx)
186 || !TEST_true(qtest_create_quic_objects(libctx, cctx, cert, privkey,
187 0, &qtserv, &clientquic,
188 NULL))
189 || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
190 goto err;
191
192 if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION)
193 || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1"))
194 goto err;
195
196 if (!TEST_true(SSL_is_quic(clientquic))
197 || !TEST_false(SSL_is_tls(clientquic))
198 || !TEST_false(SSL_is_dtls(clientquic)))
199 goto err;
200
201
202 testresult = 1;
203 err:
204 ossl_quic_tserver_free(qtserv);
205 SSL_free(clientquic);
206 SSL_CTX_free(cctx);
207
208 return testresult;
209 }
210
211 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
212 static void strip_line_ends(char *str)
213 {
214 size_t i;
215
216 for (i = strlen(str);
217 i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r');
218 i--);
219
220 str[i] = '\0';
221 }
222
223 static int compare_with_file(BIO *membio)
224 {
225 BIO *file = NULL;
226 char buf1[512], buf2[512];
227 char *reffile;
228 int ret = 0;
229 size_t i;
230
231 reffile = test_mk_file_path(datadir, "ssltraceref.txt");
232 if (!TEST_ptr(reffile))
233 goto err;
234
235 file = BIO_new_file(reffile, "rb");
236 if (!TEST_ptr(file))
237 goto err;
238
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");
242 goto err;
243 }
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);
250 goto err;
251 }
252 for (i = 0; i < strlen(buf1); i++) {
253 /* '?' is a wild card character in the reference text */
254 if (buf1[i] == '?')
255 buf2[i] = '?';
256 }
257 if (!TEST_str_eq(buf1, buf2))
258 goto err;
259 }
260 if (!TEST_true(BIO_eof(file))
261 || !TEST_true(BIO_eof(membio)))
262 goto err;
263
264 ret = 1;
265 err:
266 OPENSSL_free(reffile);
267 BIO_free(file);
268 return ret;
269 }
270
271 /*
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.
274 */
275 static int test_ssl_trace(void)
276 {
277 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
278 SSL *clientquic = NULL;
279 QUIC_TSERVER *qtserv = NULL;
280 int testresult = 0;
281 BIO *bio = BIO_new(BIO_s_mem());
282
283 /*
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
286 */
287 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
288 goto err;
289
290 if (!TEST_ptr(cctx)
291 || !TEST_ptr(bio)
292 || !TEST_true(qtest_create_quic_objects(libctx, cctx, cert, privkey,
293 0, &qtserv, &clientquic,
294 NULL)))
295 goto err;
296
297 SSL_set_msg_callback(clientquic, SSL_trace);
298 SSL_set_msg_callback_arg(clientquic, bio);
299
300 if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
301 goto err;
302
303 if (!TEST_true(compare_with_file(bio)))
304 goto err;
305
306 testresult = 1;
307 err:
308 ossl_quic_tserver_free(qtserv);
309 SSL_free(clientquic);
310 SSL_CTX_free(cctx);
311 BIO_free(bio);
312
313 return testresult;
314 }
315 #endif
316
317 static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers)
318 {
319 size_t i;
320
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:
328 break;
329 default:
330 TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher));
331 return 0;
332 }
333 }
334
335 return 1;
336 }
337
338 /*
339 * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
340 */
341 static int test_quic_forbidden_apis_ctx(void)
342 {
343 int testresult = 0;
344 SSL_CTX *ctx = NULL;
345
346 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
347 goto err;
348
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")))
351 goto err;
352
353 /*
354 * List of ciphersuites we do and don't allow in QUIC.
355 */
356 #define QUIC_CIPHERSUITES \
357 "TLS_AES_128_GCM_SHA256:" \
358 "TLS_AES_256_GCM_SHA384:" \
359 "TLS_CHACHA20_POLY1305_SHA256"
360
361 #define NON_QUIC_CIPHERSUITES \
362 "TLS_AES_128_CCM_SHA256:" \
363 "TLS_AES_256_CCM_SHA384:" \
364 "TLS_AES_128_CCM_8_SHA256"
365
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)))
370 goto err;
371
372 /*
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.
376 */
377
378 testresult = 1;
379 err:
380 SSL_CTX_free(ctx);
381 return testresult;
382 }
383
384 static int test_quic_forbidden_apis(void)
385 {
386 int testresult = 0;
387 SSL_CTX *ctx = NULL;
388 SSL *ssl = NULL;
389 STACK_OF(SSL_CIPHER) *ciphers = NULL;
390
391 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
392 goto err;
393
394 if (!TEST_ptr(ssl = SSL_new(ctx)))
395 goto err;
396
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")))
399 goto err;
400
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)))
405 goto err;
406
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)))
410 goto err;
411
412 testresult = 1;
413 err:
414 sk_SSL_CIPHER_free(ciphers);
415 SSL_free(ssl);
416 SSL_CTX_free(ctx);
417 return testresult;
418 }
419
420 static int test_quic_forbidden_options(void)
421 {
422 int testresult = 0;
423 SSL_CTX *ctx = NULL;
424 SSL *ssl = NULL;
425 char buf[16];
426 size_t len;
427
428 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
429 goto err;
430
431 /* QUIC options restrictions do not affect SSL_CTX */
432 SSL_CTX_set_options(ctx, UINT64_MAX);
433
434 if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX))
435 goto err;
436
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);
441
442 if (!TEST_ptr(ssl = SSL_new(ctx)))
443 goto err;
444
445 /* Only permitted options get transferred to SSL object */
446 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
447 goto err;
448
449 /* Try again using SSL_set_options */
450 SSL_set_options(ssl, UINT64_MAX);
451
452 if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
453 goto err;
454
455 /* Clear everything */
456 SSL_clear_options(ssl, UINT64_MAX);
457
458 if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0))
459 goto err;
460
461 /* Readahead */
462 if (!TEST_false(SSL_get_read_ahead(ssl)))
463 goto err;
464
465 SSL_set_read_ahead(ssl, 1);
466 if (!TEST_false(SSL_get_read_ahead(ssl)))
467 goto err;
468
469 /* Block padding */
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)))
473 goto err;
474
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)))
478 goto err;
479
480 /* Max early data */
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)))
485 goto err;
486
487 /* Read/Write */
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)))
490 goto err;
491
492 /* Buffer Management */
493 if (!TEST_true(SSL_allocate_buffers(ssl))
494 || !TEST_false(SSL_free_buffers(ssl)))
495 goto err;
496
497 testresult = 1;
498 err:
499 SSL_free(ssl);
500 SSL_CTX_free(ctx);
501 return testresult;
502 }
503
504 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
505
506 int setup_tests(void)
507 {
508 char *modulename;
509 char *configfile;
510
511 libctx = OSSL_LIB_CTX_new();
512 if (!TEST_ptr(libctx))
513 return 0;
514
515 defctxnull = OSSL_PROVIDER_load(NULL, "null");
516
517 /*
518 * Verify that the default and fips providers in the default libctx are not
519 * available
520 */
521 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
522 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
523 goto err;
524
525 if (!test_skip_common_options()) {
526 TEST_error("Error parsing test options\n");
527 goto err;
528 }
529
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)))
534 goto err;
535
536 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
537 goto err;
538
539 /* Check we have the expected provider available */
540 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
541 goto err;
542
543 /* Check the default provider is not available */
544 if (strcmp(modulename, "default") != 0
545 && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
546 goto err;
547
548 if (strcmp(modulename, "fips") == 0)
549 is_fips = 1;
550
551 cert = test_mk_file_path(certsdir, "servercert.pem");
552 if (cert == NULL)
553 goto err;
554
555 privkey = test_mk_file_path(certsdir, "serverkey.pem");
556 if (privkey == NULL)
557 goto err;
558
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);
564 #endif
565 ADD_TEST(test_quic_forbidden_apis_ctx);
566 ADD_TEST(test_quic_forbidden_apis);
567 ADD_TEST(test_quic_forbidden_options);
568 return 1;
569 err:
570 cleanup_tests();
571 return 0;
572 }
573
574 void cleanup_tests(void)
575 {
576 OPENSSL_free(cert);
577 OPENSSL_free(privkey);
578 OSSL_PROVIDER_unload(defctxnull);
579 OSSL_LIB_CTX_free(libctx);
580 }