QUIC: Control SSL option setting
[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
426     if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
427         goto err;
428
429     /* QUIC options restrictions do not affect SSL_CTX */
430     SSL_CTX_set_options(ctx, UINT64_MAX);
431
432     if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX))
433         goto err;
434
435     if (!TEST_ptr(ssl = SSL_new(ctx)))
436         goto err;
437
438     /* Only permitted options get transferred to SSL object */
439     if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
440         goto err;
441
442     /* Try again using SSL_set_options */
443     SSL_set_options(ssl, UINT64_MAX);
444
445     if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
446         goto err;
447
448     /* Clear everything */
449     SSL_clear_options(ssl, UINT64_MAX);
450
451     if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0))
452         goto err;
453
454     testresult = 1;
455 err:
456     SSL_free(ssl);
457     SSL_CTX_free(ctx);
458     return testresult;
459 }
460
461 OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
462
463 int setup_tests(void)
464 {
465     char *modulename;
466     char *configfile;
467
468     libctx = OSSL_LIB_CTX_new();
469     if (!TEST_ptr(libctx))
470         return 0;
471
472     defctxnull = OSSL_PROVIDER_load(NULL, "null");
473
474     /*
475      * Verify that the default and fips providers in the default libctx are not
476      * available
477      */
478     if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
479             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
480         goto err;
481
482     if (!test_skip_common_options()) {
483         TEST_error("Error parsing test options\n");
484         goto err;
485     }
486
487     if (!TEST_ptr(modulename = test_get_argument(0))
488             || !TEST_ptr(configfile = test_get_argument(1))
489             || !TEST_ptr(certsdir = test_get_argument(2))
490             || !TEST_ptr(datadir = test_get_argument(3)))
491         goto err;
492
493     if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
494         goto err;
495
496     /* Check we have the expected provider available */
497     if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
498         goto err;
499
500     /* Check the default provider is not available */
501     if (strcmp(modulename, "default") != 0
502             && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
503         goto err;
504
505     if (strcmp(modulename, "fips") == 0)
506         is_fips = 1;
507
508     cert = test_mk_file_path(certsdir, "servercert.pem");
509     if (cert == NULL)
510         goto err;
511
512     privkey = test_mk_file_path(certsdir, "serverkey.pem");
513     if (privkey == NULL)
514         goto err;
515
516     ADD_ALL_TESTS(test_quic_write_read, 2);
517     ADD_TEST(test_ciphersuites);
518     ADD_TEST(test_version);
519 #if !defined(OPENSSL_NO_SSL_TRACE) && !defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_ZLIB)
520     ADD_TEST(test_ssl_trace);
521 #endif
522     ADD_TEST(test_quic_forbidden_apis_ctx);
523     ADD_TEST(test_quic_forbidden_apis);
524     ADD_TEST(test_quic_forbidden_options);
525     return 1;
526  err:
527     cleanup_tests();
528     return 0;
529 }
530
531 void cleanup_tests(void)
532 {
533     OPENSSL_free(cert);
534     OPENSSL_free(privkey);
535     OSSL_PROVIDER_unload(defctxnull);
536     OSSL_LIB_CTX_free(libctx);
537 }