]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/sslapitest.c
Test a 0 return from the ticket key callback
[thirdparty/openssl.git] / test / sslapitest.c
1 /*
2 * Copyright 2016-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 /*
11 * We need access to the deprecated low level HMAC APIs for legacy purposes
12 * when the deprecated calls are not hidden
13 */
14 #ifndef OPENSSL_NO_DEPRECATED_3_0
15 # define OPENSSL_SUPPRESS_DEPRECATED
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include <openssl/opensslconf.h>
22 #include <openssl/bio.h>
23 #include <openssl/crypto.h>
24 #include <openssl/ssl.h>
25 #include <openssl/ocsp.h>
26 #include <openssl/srp.h>
27 #include <openssl/txt_db.h>
28 #include <openssl/aes.h>
29 #include <openssl/rand.h>
30 #include <openssl/core_names.h>
31 #include <openssl/core_dispatch.h>
32 #include <openssl/provider.h>
33 #include <openssl/param_build.h>
34 #include <openssl/x509v3.h>
35 #include <openssl/dh.h>
36
37 #include "helpers/ssltestlib.h"
38 #include "testutil.h"
39 #include "testutil/output.h"
40 #include "internal/nelem.h"
41 #include "internal/ktls.h"
42 #include "../ssl/ssl_local.h"
43 #include "../ssl/record/methods/recmethod_local.h"
44 #include "filterprov.h"
45
46 #undef OSSL_NO_USABLE_TLS1_3
47 #if defined(OPENSSL_NO_TLS1_3) \
48 || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
49 /*
50 * If we don't have ec or dh then there are no built-in groups that are usable
51 * with TLSv1.3
52 */
53 # define OSSL_NO_USABLE_TLS1_3
54 #endif
55
56 /* Defined in tls-provider.c */
57 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
58 const OSSL_DISPATCH *in,
59 const OSSL_DISPATCH **out,
60 void **provctx);
61
62 static OSSL_LIB_CTX *libctx = NULL;
63 static OSSL_PROVIDER *defctxnull = NULL;
64
65 #ifndef OSSL_NO_USABLE_TLS1_3
66
67 static SSL_SESSION *clientpsk = NULL;
68 static SSL_SESSION *serverpsk = NULL;
69 static const char *pskid = "Identity";
70 static const char *srvid;
71
72 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
73 size_t *idlen, SSL_SESSION **sess);
74 static int find_session_cb(SSL *ssl, const unsigned char *identity,
75 size_t identity_len, SSL_SESSION **sess);
76
77 static int use_session_cb_cnt = 0;
78 static int find_session_cb_cnt = 0;
79
80 static SSL_SESSION *create_a_psk(SSL *ssl);
81 #endif
82
83 static char *certsdir = NULL;
84 static char *cert = NULL;
85 static char *privkey = NULL;
86 static char *cert2 = NULL;
87 static char *privkey2 = NULL;
88 static char *cert1024 = NULL;
89 static char *privkey1024 = NULL;
90 static char *cert3072 = NULL;
91 static char *privkey3072 = NULL;
92 static char *cert4096 = NULL;
93 static char *privkey4096 = NULL;
94 static char *cert8192 = NULL;
95 static char *privkey8192 = NULL;
96 static char *srpvfile = NULL;
97 static char *tmpfilename = NULL;
98 static char *dhfile = NULL;
99
100 static int is_fips = 0;
101
102 #define LOG_BUFFER_SIZE 2048
103 static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
104 static size_t server_log_buffer_index = 0;
105 static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
106 static size_t client_log_buffer_index = 0;
107 static int error_writing_log = 0;
108
109 #ifndef OPENSSL_NO_OCSP
110 static const unsigned char orespder[] = "Dummy OCSP Response";
111 static int ocsp_server_called = 0;
112 static int ocsp_client_called = 0;
113
114 static int cdummyarg = 1;
115 static X509 *ocspcert = NULL;
116 #endif
117
118 #define NUM_EXTRA_CERTS 40
119 #define CLIENT_VERSION_LEN 2
120
121 /*
122 * This structure is used to validate that the correct number of log messages
123 * of various types are emitted when emitting secret logs.
124 */
125 struct sslapitest_log_counts {
126 unsigned int rsa_key_exchange_count;
127 unsigned int master_secret_count;
128 unsigned int client_early_secret_count;
129 unsigned int client_handshake_secret_count;
130 unsigned int server_handshake_secret_count;
131 unsigned int client_application_secret_count;
132 unsigned int server_application_secret_count;
133 unsigned int early_exporter_secret_count;
134 unsigned int exporter_secret_count;
135 };
136
137
138 static int hostname_cb(SSL *s, int *al, void *arg)
139 {
140 const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
141
142 if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
143 || strcmp(hostname, "altgoodhost") == 0))
144 return SSL_TLSEXT_ERR_OK;
145
146 return SSL_TLSEXT_ERR_NOACK;
147 }
148
149 static void client_keylog_callback(const SSL *ssl, const char *line)
150 {
151 int line_length = strlen(line);
152
153 /* If the log doesn't fit, error out. */
154 if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
155 TEST_info("Client log too full");
156 error_writing_log = 1;
157 return;
158 }
159
160 strcat(client_log_buffer, line);
161 client_log_buffer_index += line_length;
162 client_log_buffer[client_log_buffer_index++] = '\n';
163 }
164
165 static void server_keylog_callback(const SSL *ssl, const char *line)
166 {
167 int line_length = strlen(line);
168
169 /* If the log doesn't fit, error out. */
170 if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
171 TEST_info("Server log too full");
172 error_writing_log = 1;
173 return;
174 }
175
176 strcat(server_log_buffer, line);
177 server_log_buffer_index += line_length;
178 server_log_buffer[server_log_buffer_index++] = '\n';
179 }
180
181 static int compare_hex_encoded_buffer(const char *hex_encoded,
182 size_t hex_length,
183 const uint8_t *raw,
184 size_t raw_length)
185 {
186 size_t i, j;
187 char hexed[3];
188
189 if (!TEST_size_t_eq(raw_length * 2, hex_length))
190 return 1;
191
192 for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
193 sprintf(hexed, "%02x", raw[i]);
194 if (!TEST_int_eq(hexed[0], hex_encoded[j])
195 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
196 return 1;
197 }
198
199 return 0;
200 }
201
202 static int test_keylog_output(char *buffer, const SSL *ssl,
203 const SSL_SESSION *session,
204 struct sslapitest_log_counts *expected)
205 {
206 char *token = NULL;
207 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
208 size_t client_random_size = SSL3_RANDOM_SIZE;
209 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
210 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
211 unsigned int rsa_key_exchange_count = 0;
212 unsigned int master_secret_count = 0;
213 unsigned int client_early_secret_count = 0;
214 unsigned int client_handshake_secret_count = 0;
215 unsigned int server_handshake_secret_count = 0;
216 unsigned int client_application_secret_count = 0;
217 unsigned int server_application_secret_count = 0;
218 unsigned int early_exporter_secret_count = 0;
219 unsigned int exporter_secret_count = 0;
220
221 for (token = strtok(buffer, " \n"); token != NULL;
222 token = strtok(NULL, " \n")) {
223 if (strcmp(token, "RSA") == 0) {
224 /*
225 * Premaster secret. Tokens should be: 16 ASCII bytes of
226 * hex-encoded encrypted secret, then the hex-encoded pre-master
227 * secret.
228 */
229 if (!TEST_ptr(token = strtok(NULL, " \n")))
230 return 0;
231 if (!TEST_size_t_eq(strlen(token), 16))
232 return 0;
233 if (!TEST_ptr(token = strtok(NULL, " \n")))
234 return 0;
235 /*
236 * We can't sensibly check the log because the premaster secret is
237 * transient, and OpenSSL doesn't keep hold of it once the master
238 * secret is generated.
239 */
240 rsa_key_exchange_count++;
241 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
242 /*
243 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
244 * client random, then the hex-encoded master secret.
245 */
246 client_random_size = SSL_get_client_random(ssl,
247 actual_client_random,
248 SSL3_RANDOM_SIZE);
249 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
250 return 0;
251
252 if (!TEST_ptr(token = strtok(NULL, " \n")))
253 return 0;
254 if (!TEST_size_t_eq(strlen(token), 64))
255 return 0;
256 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
257 actual_client_random,
258 client_random_size)))
259 return 0;
260
261 if (!TEST_ptr(token = strtok(NULL, " \n")))
262 return 0;
263 master_key_size = SSL_SESSION_get_master_key(session,
264 actual_master_key,
265 master_key_size);
266 if (!TEST_size_t_ne(master_key_size, 0))
267 return 0;
268 if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
269 actual_master_key,
270 master_key_size)))
271 return 0;
272 master_secret_count++;
273 } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
274 || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
275 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
276 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
277 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
278 || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
279 || strcmp(token, "EXPORTER_SECRET") == 0) {
280 /*
281 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
282 * client random, and then the hex-encoded secret. In this case,
283 * we treat all of these secrets identically and then just
284 * distinguish between them when counting what we saw.
285 */
286 if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
287 client_early_secret_count++;
288 else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
289 client_handshake_secret_count++;
290 else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
291 server_handshake_secret_count++;
292 else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
293 client_application_secret_count++;
294 else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
295 server_application_secret_count++;
296 else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
297 early_exporter_secret_count++;
298 else if (strcmp(token, "EXPORTER_SECRET") == 0)
299 exporter_secret_count++;
300
301 client_random_size = SSL_get_client_random(ssl,
302 actual_client_random,
303 SSL3_RANDOM_SIZE);
304 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
305 return 0;
306
307 if (!TEST_ptr(token = strtok(NULL, " \n")))
308 return 0;
309 if (!TEST_size_t_eq(strlen(token), 64))
310 return 0;
311 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
312 actual_client_random,
313 client_random_size)))
314 return 0;
315
316 if (!TEST_ptr(token = strtok(NULL, " \n")))
317 return 0;
318 } else {
319 TEST_info("Unexpected token %s\n", token);
320 return 0;
321 }
322 }
323
324 /* Got what we expected? */
325 if (!TEST_size_t_eq(rsa_key_exchange_count,
326 expected->rsa_key_exchange_count)
327 || !TEST_size_t_eq(master_secret_count,
328 expected->master_secret_count)
329 || !TEST_size_t_eq(client_early_secret_count,
330 expected->client_early_secret_count)
331 || !TEST_size_t_eq(client_handshake_secret_count,
332 expected->client_handshake_secret_count)
333 || !TEST_size_t_eq(server_handshake_secret_count,
334 expected->server_handshake_secret_count)
335 || !TEST_size_t_eq(client_application_secret_count,
336 expected->client_application_secret_count)
337 || !TEST_size_t_eq(server_application_secret_count,
338 expected->server_application_secret_count)
339 || !TEST_size_t_eq(early_exporter_secret_count,
340 expected->early_exporter_secret_count)
341 || !TEST_size_t_eq(exporter_secret_count,
342 expected->exporter_secret_count))
343 return 0;
344 return 1;
345 }
346
347 #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
348 static int test_keylog(void)
349 {
350 SSL_CTX *cctx = NULL, *sctx = NULL;
351 SSL *clientssl = NULL, *serverssl = NULL;
352 int testresult = 0;
353 struct sslapitest_log_counts expected;
354
355 /* Clean up logging space */
356 memset(&expected, 0, sizeof(expected));
357 memset(client_log_buffer, 0, sizeof(client_log_buffer));
358 memset(server_log_buffer, 0, sizeof(server_log_buffer));
359 client_log_buffer_index = 0;
360 server_log_buffer_index = 0;
361 error_writing_log = 0;
362
363 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
364 TLS_client_method(),
365 TLS1_VERSION, 0,
366 &sctx, &cctx, cert, privkey)))
367 return 0;
368
369 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
370 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
371 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
372
373 /* We also want to ensure that we use RSA-based key exchange. */
374 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
375 goto end;
376
377 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
378 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
379 goto end;
380 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
381 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
382 == client_keylog_callback))
383 goto end;
384 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
385 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
386 == server_keylog_callback))
387 goto end;
388
389 /* Now do a handshake and check that the logs have been written to. */
390 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
391 &clientssl, NULL, NULL))
392 || !TEST_true(create_ssl_connection(serverssl, clientssl,
393 SSL_ERROR_NONE))
394 || !TEST_false(error_writing_log)
395 || !TEST_int_gt(client_log_buffer_index, 0)
396 || !TEST_int_gt(server_log_buffer_index, 0))
397 goto end;
398
399 /*
400 * Now we want to test that our output data was vaguely sensible. We
401 * do that by using strtok and confirming that we have more or less the
402 * data we expect. For both client and server, we expect to see one master
403 * secret. The client should also see a RSA key exchange.
404 */
405 expected.rsa_key_exchange_count = 1;
406 expected.master_secret_count = 1;
407 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
408 SSL_get_session(clientssl), &expected)))
409 goto end;
410
411 expected.rsa_key_exchange_count = 0;
412 if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
413 SSL_get_session(serverssl), &expected)))
414 goto end;
415
416 testresult = 1;
417
418 end:
419 SSL_free(serverssl);
420 SSL_free(clientssl);
421 SSL_CTX_free(sctx);
422 SSL_CTX_free(cctx);
423
424 return testresult;
425 }
426 #endif
427
428 #ifndef OSSL_NO_USABLE_TLS1_3
429 static int test_keylog_no_master_key(void)
430 {
431 SSL_CTX *cctx = NULL, *sctx = NULL;
432 SSL *clientssl = NULL, *serverssl = NULL;
433 SSL_SESSION *sess = NULL;
434 int testresult = 0;
435 struct sslapitest_log_counts expected;
436 unsigned char buf[1];
437 size_t readbytes, written;
438
439 /* Clean up logging space */
440 memset(&expected, 0, sizeof(expected));
441 memset(client_log_buffer, 0, sizeof(client_log_buffer));
442 memset(server_log_buffer, 0, sizeof(server_log_buffer));
443 client_log_buffer_index = 0;
444 server_log_buffer_index = 0;
445 error_writing_log = 0;
446
447 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
448 TLS_client_method(), TLS1_VERSION, 0,
449 &sctx, &cctx, cert, privkey))
450 || !TEST_true(SSL_CTX_set_max_early_data(sctx,
451 SSL3_RT_MAX_PLAIN_LENGTH)))
452 return 0;
453
454 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
455 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
456 goto end;
457
458 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
459 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
460 == client_keylog_callback))
461 goto end;
462
463 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
464 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
465 == server_keylog_callback))
466 goto end;
467
468 /* Now do a handshake and check that the logs have been written to. */
469 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
470 &clientssl, NULL, NULL))
471 || !TEST_true(create_ssl_connection(serverssl, clientssl,
472 SSL_ERROR_NONE))
473 || !TEST_false(error_writing_log))
474 goto end;
475
476 /*
477 * Now we want to test that our output data was vaguely sensible. For this
478 * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
479 * TLSv1.3, but we do expect both client and server to emit keys.
480 */
481 expected.client_handshake_secret_count = 1;
482 expected.server_handshake_secret_count = 1;
483 expected.client_application_secret_count = 1;
484 expected.server_application_secret_count = 1;
485 expected.exporter_secret_count = 1;
486 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
487 SSL_get_session(clientssl), &expected))
488 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
489 SSL_get_session(serverssl),
490 &expected)))
491 goto end;
492
493 /* Terminate old session and resume with early data. */
494 sess = SSL_get1_session(clientssl);
495 SSL_shutdown(clientssl);
496 SSL_shutdown(serverssl);
497 SSL_free(serverssl);
498 SSL_free(clientssl);
499 serverssl = clientssl = NULL;
500
501 /* Reset key log */
502 memset(client_log_buffer, 0, sizeof(client_log_buffer));
503 memset(server_log_buffer, 0, sizeof(server_log_buffer));
504 client_log_buffer_index = 0;
505 server_log_buffer_index = 0;
506
507 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
508 &clientssl, NULL, NULL))
509 || !TEST_true(SSL_set_session(clientssl, sess))
510 /* Here writing 0 length early data is enough. */
511 || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
512 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
513 &readbytes),
514 SSL_READ_EARLY_DATA_ERROR)
515 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
516 SSL_EARLY_DATA_ACCEPTED)
517 || !TEST_true(create_ssl_connection(serverssl, clientssl,
518 SSL_ERROR_NONE))
519 || !TEST_true(SSL_session_reused(clientssl)))
520 goto end;
521
522 /* In addition to the previous entries, expect early secrets. */
523 expected.client_early_secret_count = 1;
524 expected.early_exporter_secret_count = 1;
525 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
526 SSL_get_session(clientssl), &expected))
527 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
528 SSL_get_session(serverssl),
529 &expected)))
530 goto end;
531
532 testresult = 1;
533
534 end:
535 SSL_SESSION_free(sess);
536 SSL_free(serverssl);
537 SSL_free(clientssl);
538 SSL_CTX_free(sctx);
539 SSL_CTX_free(cctx);
540
541 return testresult;
542 }
543 #endif
544
545 static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
546 {
547 int res = X509_verify_cert(ctx);
548 int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
549 SSL *ssl;
550
551 /* this should not happen but check anyway */
552 if (idx < 0
553 || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
554 return 0;
555
556 if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
557 X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
558 /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
559 return SSL_set_retry_verify(ssl);
560
561 return res;
562 }
563
564 static int test_client_cert_verify_cb(void)
565 {
566 /* server key, cert, chain, and root */
567 char *skey = test_mk_file_path(certsdir, "leaf.key");
568 char *leaf = test_mk_file_path(certsdir, "leaf.pem");
569 char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
570 char *int1 = test_mk_file_path(certsdir, "interCA.pem");
571 char *root = test_mk_file_path(certsdir, "rootCA.pem");
572 X509 *crt1 = NULL, *crt2 = NULL;
573 STACK_OF(X509) *server_chain;
574 SSL_CTX *cctx = NULL, *sctx = NULL;
575 SSL *clientssl = NULL, *serverssl = NULL;
576 int testresult = 0;
577
578 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
579 TLS_client_method(), TLS1_VERSION, 0,
580 &sctx, &cctx, NULL, NULL)))
581 goto end;
582 if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
583 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
584 SSL_FILETYPE_PEM), 1)
585 || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
586 goto end;
587 if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
588 goto end;
589 SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
590 SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
591 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
592 &clientssl, NULL, NULL)))
593 goto end;
594
595 /* attempt SSL_connect() with incomplete server chain */
596 if (!TEST_false(create_ssl_connection(serverssl, clientssl,
597 SSL_ERROR_WANT_RETRY_VERIFY)))
598 goto end;
599
600 /* application provides intermediate certs needed to verify server cert */
601 if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
602 || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
603 || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
604 goto end;
605 /* add certs in reverse order to demonstrate real chain building */
606 if (!TEST_true(sk_X509_push(server_chain, crt1)))
607 goto end;
608 crt1 = NULL;
609 if (!TEST_true(sk_X509_push(server_chain, crt2)))
610 goto end;
611 crt2 = NULL;
612
613 /* continue SSL_connect(), must now succeed with completed server chain */
614 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
615 SSL_ERROR_NONE)))
616 goto end;
617
618 testresult = 1;
619
620 end:
621 X509_free(crt1);
622 X509_free(crt2);
623 if (clientssl != NULL) {
624 SSL_shutdown(clientssl);
625 SSL_free(clientssl);
626 }
627 if (serverssl != NULL) {
628 SSL_shutdown(serverssl);
629 SSL_free(serverssl);
630 }
631 SSL_CTX_free(sctx);
632 SSL_CTX_free(cctx);
633
634 OPENSSL_free(skey);
635 OPENSSL_free(leaf);
636 OPENSSL_free(int2);
637 OPENSSL_free(int1);
638 OPENSSL_free(root);
639
640 return testresult;
641 }
642
643 static int test_ssl_build_cert_chain(void)
644 {
645 int ret = 0;
646 SSL_CTX *ssl_ctx = NULL;
647 SSL *ssl = NULL;
648 char *skey = test_mk_file_path(certsdir, "leaf.key");
649 char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
650
651 if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
652 goto end;
653 if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
654 goto end;
655 /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
656 if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
657 || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
658 || !TEST_int_eq(SSL_check_private_key(ssl), 1))
659 goto end;
660 if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
661 | SSL_BUILD_CHAIN_FLAG_CHECK)))
662 goto end;
663 ret = 1;
664 end:
665 SSL_free(ssl);
666 SSL_CTX_free(ssl_ctx);
667 OPENSSL_free(leaf_chain);
668 OPENSSL_free(skey);
669 return ret;
670 }
671
672 static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
673 {
674 static const char pass[] = "testpass";
675
676 if (!TEST_int_eq(size, PEM_BUFSIZE))
677 return -1;
678
679 memcpy(buf, pass, sizeof(pass) - 1);
680 return sizeof(pass) - 1;
681 }
682
683 static int test_ssl_ctx_build_cert_chain(void)
684 {
685 int ret = 0;
686 SSL_CTX *ctx = NULL;
687 char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
688 char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
689
690 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
691 goto end;
692 SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
693 /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
694 if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
695 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
696 SSL_FILETYPE_PEM), 1)
697 || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
698 goto end;
699 if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
700 | SSL_BUILD_CHAIN_FLAG_CHECK)))
701 goto end;
702 ret = 1;
703 end:
704 SSL_CTX_free(ctx);
705 OPENSSL_free(leaf_chain);
706 OPENSSL_free(skey);
707 return ret;
708 }
709
710 #ifndef OPENSSL_NO_TLS1_2
711 static int full_client_hello_callback(SSL *s, int *al, void *arg)
712 {
713 int *ctr = arg;
714 const unsigned char *p;
715 int *exts;
716 /* We only configure two ciphers, but the SCSV is added automatically. */
717 #ifdef OPENSSL_NO_EC
718 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
719 #else
720 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
721 0x2c, 0x00, 0xff};
722 #endif
723 const int expected_extensions[] = {
724 #ifndef OPENSSL_NO_EC
725 11, 10,
726 #endif
727 35, 22, 23, 13};
728 size_t len;
729
730 /* Make sure we can defer processing and get called back. */
731 if ((*ctr)++ == 0)
732 return SSL_CLIENT_HELLO_RETRY;
733
734 len = SSL_client_hello_get0_ciphers(s, &p);
735 if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
736 || !TEST_size_t_eq(
737 SSL_client_hello_get0_compression_methods(s, &p), 1)
738 || !TEST_int_eq(*p, 0))
739 return SSL_CLIENT_HELLO_ERROR;
740 if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
741 return SSL_CLIENT_HELLO_ERROR;
742 if (len != OSSL_NELEM(expected_extensions) ||
743 memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
744 printf("ClientHello callback expected extensions mismatch\n");
745 OPENSSL_free(exts);
746 return SSL_CLIENT_HELLO_ERROR;
747 }
748 OPENSSL_free(exts);
749 return SSL_CLIENT_HELLO_SUCCESS;
750 }
751
752 static int test_client_hello_cb(void)
753 {
754 SSL_CTX *cctx = NULL, *sctx = NULL;
755 SSL *clientssl = NULL, *serverssl = NULL;
756 int testctr = 0, testresult = 0;
757
758 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
759 TLS_client_method(), TLS1_VERSION, 0,
760 &sctx, &cctx, cert, privkey)))
761 goto end;
762 SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
763
764 /* The gimpy cipher list we configure can't do TLS 1.3. */
765 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
766
767 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
768 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
769 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
770 &clientssl, NULL, NULL))
771 || !TEST_false(create_ssl_connection(serverssl, clientssl,
772 SSL_ERROR_WANT_CLIENT_HELLO_CB))
773 /*
774 * Passing a -1 literal is a hack since
775 * the real value was lost.
776 * */
777 || !TEST_int_eq(SSL_get_error(serverssl, -1),
778 SSL_ERROR_WANT_CLIENT_HELLO_CB)
779 || !TEST_true(create_ssl_connection(serverssl, clientssl,
780 SSL_ERROR_NONE)))
781 goto end;
782
783 testresult = 1;
784
785 end:
786 SSL_free(serverssl);
787 SSL_free(clientssl);
788 SSL_CTX_free(sctx);
789 SSL_CTX_free(cctx);
790
791 return testresult;
792 }
793
794 static int test_no_ems(void)
795 {
796 SSL_CTX *cctx = NULL, *sctx = NULL;
797 SSL *clientssl = NULL, *serverssl = NULL;
798 int testresult = 0;
799
800 if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
801 TLS1_VERSION, TLS1_2_VERSION,
802 &sctx, &cctx, cert, privkey)) {
803 printf("Unable to create SSL_CTX pair\n");
804 goto end;
805 }
806
807 SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
808
809 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
810 printf("Unable to create SSL objects\n");
811 goto end;
812 }
813
814 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
815 printf("Creating SSL connection failed\n");
816 goto end;
817 }
818
819 if (SSL_get_extms_support(serverssl)) {
820 printf("Server reports Extended Master Secret support\n");
821 goto end;
822 }
823
824 if (SSL_get_extms_support(clientssl)) {
825 printf("Client reports Extended Master Secret support\n");
826 goto end;
827 }
828 testresult = 1;
829
830 end:
831 SSL_free(serverssl);
832 SSL_free(clientssl);
833 SSL_CTX_free(sctx);
834 SSL_CTX_free(cctx);
835
836 return testresult;
837 }
838
839 /*
840 * Very focused test to exercise a single case in the server-side state
841 * machine, when the ChangeCipherState message needs to actually change
842 * from one cipher to a different cipher (i.e., not changing from null
843 * encryption to real encryption).
844 */
845 static int test_ccs_change_cipher(void)
846 {
847 SSL_CTX *cctx = NULL, *sctx = NULL;
848 SSL *clientssl = NULL, *serverssl = NULL;
849 SSL_SESSION *sess = NULL, *sesspre, *sesspost;
850 int testresult = 0;
851 int i;
852 unsigned char buf;
853 size_t readbytes;
854
855 /*
856 * Create a connection so we can resume and potentially (but not) use
857 * a different cipher in the second connection.
858 */
859 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
860 TLS_client_method(),
861 TLS1_VERSION, TLS1_2_VERSION,
862 &sctx, &cctx, cert, privkey))
863 || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
864 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
865 NULL, NULL))
866 || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
867 || !TEST_true(create_ssl_connection(serverssl, clientssl,
868 SSL_ERROR_NONE))
869 || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
870 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
871 goto end;
872
873 shutdown_ssl_connection(serverssl, clientssl);
874 serverssl = clientssl = NULL;
875
876 /* Resume, preferring a different cipher. Our server will force the
877 * same cipher to be used as the initial handshake. */
878 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
879 NULL, NULL))
880 || !TEST_true(SSL_set_session(clientssl, sess))
881 || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
882 || !TEST_true(create_ssl_connection(serverssl, clientssl,
883 SSL_ERROR_NONE))
884 || !TEST_true(SSL_session_reused(clientssl))
885 || !TEST_true(SSL_session_reused(serverssl))
886 || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
887 || !TEST_ptr_eq(sesspre, sesspost)
888 || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
889 SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
890 goto end;
891 shutdown_ssl_connection(serverssl, clientssl);
892 serverssl = clientssl = NULL;
893
894 /*
895 * Now create a fresh connection and try to renegotiate a different
896 * cipher on it.
897 */
898 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
899 NULL, NULL))
900 || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
901 || !TEST_true(create_ssl_connection(serverssl, clientssl,
902 SSL_ERROR_NONE))
903 || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
904 || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
905 || !TEST_true(SSL_renegotiate(clientssl))
906 || !TEST_true(SSL_renegotiate_pending(clientssl)))
907 goto end;
908 /* Actually drive the renegotiation. */
909 for (i = 0; i < 3; i++) {
910 if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
911 if (!TEST_ulong_eq(readbytes, 0))
912 goto end;
913 } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
914 SSL_ERROR_WANT_READ)) {
915 goto end;
916 }
917 if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
918 if (!TEST_ulong_eq(readbytes, 0))
919 goto end;
920 } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
921 SSL_ERROR_WANT_READ)) {
922 goto end;
923 }
924 }
925 /* sesspre and sesspost should be different since the cipher changed. */
926 if (!TEST_false(SSL_renegotiate_pending(clientssl))
927 || !TEST_false(SSL_session_reused(clientssl))
928 || !TEST_false(SSL_session_reused(serverssl))
929 || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
930 || !TEST_ptr_ne(sesspre, sesspost)
931 || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
932 SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
933 goto end;
934
935 shutdown_ssl_connection(serverssl, clientssl);
936 serverssl = clientssl = NULL;
937
938 testresult = 1;
939
940 end:
941 SSL_free(serverssl);
942 SSL_free(clientssl);
943 SSL_CTX_free(sctx);
944 SSL_CTX_free(cctx);
945 SSL_SESSION_free(sess);
946
947 return testresult;
948 }
949 #endif
950
951 static int execute_test_large_message(const SSL_METHOD *smeth,
952 const SSL_METHOD *cmeth,
953 int min_version, int max_version,
954 int read_ahead)
955 {
956 SSL_CTX *cctx = NULL, *sctx = NULL;
957 SSL *clientssl = NULL, *serverssl = NULL;
958 int testresult = 0;
959 int i;
960 BIO *certbio = NULL;
961 X509 *chaincert = NULL;
962 int certlen;
963
964 if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
965 goto end;
966
967 if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
968 goto end;
969
970 if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
971 goto end;
972 BIO_free(certbio);
973 certbio = NULL;
974
975 if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
976 max_version, &sctx, &cctx, cert,
977 privkey)))
978 goto end;
979
980 #ifdef OPENSSL_NO_DTLS1_2
981 if (smeth == DTLS_server_method()) {
982 /*
983 * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
984 * level 0
985 */
986 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
987 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
988 "DEFAULT:@SECLEVEL=0")))
989 goto end;
990 }
991 #endif
992
993 if (read_ahead) {
994 /*
995 * Test that read_ahead works correctly when dealing with large
996 * records
997 */
998 SSL_CTX_set_read_ahead(cctx, 1);
999 }
1000
1001 /*
1002 * We assume the supplied certificate is big enough so that if we add
1003 * NUM_EXTRA_CERTS it will make the overall message large enough. The
1004 * default buffer size is requested to be 16k, but due to the way BUF_MEM
1005 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
1006 * test we need to have a message larger than that.
1007 */
1008 certlen = i2d_X509(chaincert, NULL);
1009 OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
1010 (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
1011 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
1012 if (!X509_up_ref(chaincert))
1013 goto end;
1014 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
1015 X509_free(chaincert);
1016 goto end;
1017 }
1018 }
1019
1020 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1021 NULL, NULL))
1022 || !TEST_true(create_ssl_connection(serverssl, clientssl,
1023 SSL_ERROR_NONE)))
1024 goto end;
1025
1026 /*
1027 * Calling SSL_clear() first is not required but this tests that SSL_clear()
1028 * doesn't leak.
1029 */
1030 if (!TEST_true(SSL_clear(serverssl)))
1031 goto end;
1032
1033 testresult = 1;
1034 end:
1035 BIO_free(certbio);
1036 X509_free(chaincert);
1037 SSL_free(serverssl);
1038 SSL_free(clientssl);
1039 SSL_CTX_free(sctx);
1040 SSL_CTX_free(cctx);
1041
1042 return testresult;
1043 }
1044
1045 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
1046 !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
1047 /* sock must be connected */
1048 static int ktls_chk_platform(int sock)
1049 {
1050 if (!ktls_enable(sock))
1051 return 0;
1052 return 1;
1053 }
1054
1055 static int ping_pong_query(SSL *clientssl, SSL *serverssl)
1056 {
1057 static char count = 1;
1058 unsigned char cbuf[16000] = {0};
1059 unsigned char sbuf[16000];
1060 size_t err = 0;
1061 char crec_wseq_before[SEQ_NUM_SIZE];
1062 char crec_wseq_after[SEQ_NUM_SIZE];
1063 char crec_rseq_before[SEQ_NUM_SIZE];
1064 char crec_rseq_after[SEQ_NUM_SIZE];
1065 char srec_wseq_before[SEQ_NUM_SIZE];
1066 char srec_wseq_after[SEQ_NUM_SIZE];
1067 char srec_rseq_before[SEQ_NUM_SIZE];
1068 char srec_rseq_after[SEQ_NUM_SIZE];
1069 SSL_CONNECTION *clientsc, *serversc;
1070
1071 if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
1072 || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
1073 goto end;
1074
1075 cbuf[0] = count++;
1076 memcpy(crec_wseq_before, &clientsc->rlayer.write_sequence, SEQ_NUM_SIZE);
1077 memcpy(crec_rseq_before, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
1078 memcpy(srec_wseq_before, &serversc->rlayer.write_sequence, SEQ_NUM_SIZE);
1079 memcpy(srec_rseq_before, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
1080
1081 if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
1082 goto end;
1083
1084 while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
1085 if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
1086 goto end;
1087 }
1088 }
1089
1090 if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
1091 goto end;
1092
1093 while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
1094 if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
1095 goto end;
1096 }
1097 }
1098
1099 memcpy(crec_wseq_after, &clientsc->rlayer.write_sequence, SEQ_NUM_SIZE);
1100 memcpy(crec_rseq_after, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
1101 memcpy(srec_wseq_after, &serversc->rlayer.write_sequence, SEQ_NUM_SIZE);
1102 memcpy(srec_rseq_after, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
1103
1104 /* verify the payload */
1105 if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1106 goto end;
1107
1108 /*
1109 * If ktls is used then kernel sequences are used instead of
1110 * OpenSSL sequences
1111 */
1112 if (!BIO_get_ktls_send(clientsc->wbio)) {
1113 if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
1114 crec_wseq_after, SEQ_NUM_SIZE))
1115 goto end;
1116 } else {
1117 if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
1118 crec_wseq_after, SEQ_NUM_SIZE))
1119 goto end;
1120 }
1121
1122 if (!BIO_get_ktls_send(serversc->wbio)) {
1123 if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
1124 srec_wseq_after, SEQ_NUM_SIZE))
1125 goto end;
1126 } else {
1127 if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
1128 srec_wseq_after, SEQ_NUM_SIZE))
1129 goto end;
1130 }
1131
1132 if (!BIO_get_ktls_recv(clientsc->wbio)) {
1133 if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
1134 crec_rseq_after, SEQ_NUM_SIZE))
1135 goto end;
1136 } else {
1137 if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
1138 crec_rseq_after, SEQ_NUM_SIZE))
1139 goto end;
1140 }
1141
1142 if (!BIO_get_ktls_recv(serversc->wbio)) {
1143 if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
1144 srec_rseq_after, SEQ_NUM_SIZE))
1145 goto end;
1146 } else {
1147 if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
1148 srec_rseq_after, SEQ_NUM_SIZE))
1149 goto end;
1150 }
1151
1152 return 1;
1153 end:
1154 return 0;
1155 }
1156
1157 static int execute_test_ktls(int cis_ktls, int sis_ktls,
1158 int tls_version, const char *cipher)
1159 {
1160 SSL_CTX *cctx = NULL, *sctx = NULL;
1161 SSL *clientssl = NULL, *serverssl = NULL;
1162 int ktls_used = 0, testresult = 0;
1163 int cfd = -1, sfd = -1;
1164 int rx_supported;
1165 SSL_CONNECTION *clientsc, *serversc;
1166
1167 if (!TEST_true(create_test_sockets(&cfd, &sfd)))
1168 goto end;
1169
1170 /* Skip this test if the platform does not support ktls */
1171 if (!ktls_chk_platform(cfd)) {
1172 testresult = TEST_skip("Kernel does not support KTLS");
1173 goto end;
1174 }
1175
1176 if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1177 testresult = TEST_skip("CHACHA is not supported in FIPS");
1178 goto end;
1179 }
1180
1181 /* Create a session based on SHA-256 */
1182 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1183 TLS_client_method(),
1184 tls_version, tls_version,
1185 &sctx, &cctx, cert, privkey)))
1186 goto end;
1187
1188 if (tls_version == TLS1_3_VERSION) {
1189 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1190 || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1191 goto end;
1192 } else {
1193 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1194 || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1195 goto end;
1196 }
1197
1198 if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1199 &clientssl, sfd, cfd)))
1200 goto end;
1201
1202 if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
1203 || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
1204 goto end;
1205
1206 if (cis_ktls) {
1207 if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
1208 goto end;
1209 }
1210
1211 if (sis_ktls) {
1212 if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1213 goto end;
1214 }
1215
1216 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1217 goto end;
1218
1219 /*
1220 * The running kernel may not support a given cipher suite
1221 * or direction, so just check that KTLS isn't used when it
1222 * isn't enabled.
1223 */
1224 if (!cis_ktls) {
1225 if (!TEST_false(BIO_get_ktls_send(clientsc->wbio)))
1226 goto end;
1227 } else {
1228 if (BIO_get_ktls_send(clientsc->wbio))
1229 ktls_used = 1;
1230 }
1231
1232 if (!sis_ktls) {
1233 if (!TEST_false(BIO_get_ktls_send(serversc->wbio)))
1234 goto end;
1235 } else {
1236 if (BIO_get_ktls_send(serversc->wbio))
1237 ktls_used = 1;
1238 }
1239
1240 #if defined(OPENSSL_NO_KTLS_RX)
1241 rx_supported = 0;
1242 #else
1243 rx_supported = 1;
1244 #endif
1245 if (!cis_ktls || !rx_supported) {
1246 if (!TEST_false(BIO_get_ktls_recv(clientsc->rbio)))
1247 goto end;
1248 } else {
1249 if (BIO_get_ktls_send(clientsc->rbio))
1250 ktls_used = 1;
1251 }
1252
1253 if (!sis_ktls || !rx_supported) {
1254 if (!TEST_false(BIO_get_ktls_recv(serversc->rbio)))
1255 goto end;
1256 } else {
1257 if (BIO_get_ktls_send(serversc->rbio))
1258 ktls_used = 1;
1259 }
1260
1261 if ((cis_ktls || sis_ktls) && !ktls_used) {
1262 testresult = TEST_skip("KTLS not supported for %s cipher %s",
1263 tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1264 "TLS 1.2", cipher);
1265 goto end;
1266 }
1267
1268 if (!TEST_true(ping_pong_query(clientssl, serverssl)))
1269 goto end;
1270
1271 testresult = 1;
1272 end:
1273 if (clientssl) {
1274 SSL_shutdown(clientssl);
1275 SSL_free(clientssl);
1276 }
1277 if (serverssl) {
1278 SSL_shutdown(serverssl);
1279 SSL_free(serverssl);
1280 }
1281 SSL_CTX_free(sctx);
1282 SSL_CTX_free(cctx);
1283 serverssl = clientssl = NULL;
1284 if (cfd != -1)
1285 close(cfd);
1286 if (sfd != -1)
1287 close(sfd);
1288 return testresult;
1289 }
1290
1291 #define SENDFILE_SZ (16 * 4096)
1292 #define SENDFILE_CHUNK (4 * 4096)
1293 #define min(a,b) ((a) > (b) ? (b) : (a))
1294
1295 static int execute_test_ktls_sendfile(int tls_version, const char *cipher)
1296 {
1297 SSL_CTX *cctx = NULL, *sctx = NULL;
1298 SSL *clientssl = NULL, *serverssl = NULL;
1299 unsigned char *buf, *buf_dst;
1300 BIO *out = NULL, *in = NULL;
1301 int cfd = -1, sfd = -1, ffd, err;
1302 ssize_t chunk_size = 0;
1303 off_t chunk_off = 0;
1304 int testresult = 0;
1305 FILE *ffdp;
1306 SSL_CONNECTION *serversc;
1307
1308 buf = OPENSSL_zalloc(SENDFILE_SZ);
1309 buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
1310 if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
1311 || !TEST_true(create_test_sockets(&cfd, &sfd)))
1312 goto end;
1313
1314 /* Skip this test if the platform does not support ktls */
1315 if (!ktls_chk_platform(sfd)) {
1316 testresult = TEST_skip("Kernel does not support KTLS");
1317 goto end;
1318 }
1319
1320 if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1321 testresult = TEST_skip("CHACHA is not supported in FIPS");
1322 goto end;
1323 }
1324
1325 /* Create a session based on SHA-256 */
1326 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1327 TLS_client_method(),
1328 tls_version, tls_version,
1329 &sctx, &cctx, cert, privkey)))
1330 goto end;
1331
1332 if (tls_version == TLS1_3_VERSION) {
1333 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1334 || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1335 goto end;
1336 } else {
1337 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1338 || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1339 goto end;
1340 }
1341
1342 if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1343 &clientssl, sfd, cfd)))
1344 goto end;
1345
1346 if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
1347 goto end;
1348
1349 if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1350 goto end;
1351
1352 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1353 SSL_ERROR_NONE)))
1354 goto end;
1355
1356 if (!BIO_get_ktls_send(serversc->wbio)) {
1357 testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
1358 tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1359 "TLS 1.2", cipher);
1360 goto end;
1361 }
1362
1363 if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
1364 goto end;
1365
1366 out = BIO_new_file(tmpfilename, "wb");
1367 if (!TEST_ptr(out))
1368 goto end;
1369
1370 if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
1371 goto end;
1372
1373 BIO_free(out);
1374 out = NULL;
1375 in = BIO_new_file(tmpfilename, "rb");
1376 BIO_get_fp(in, &ffdp);
1377 ffd = fileno(ffdp);
1378
1379 while (chunk_off < SENDFILE_SZ) {
1380 chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
1381 while ((err = SSL_sendfile(serverssl,
1382 ffd,
1383 chunk_off,
1384 chunk_size,
1385 0)) != chunk_size) {
1386 if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
1387 goto end;
1388 }
1389 while ((err = SSL_read(clientssl,
1390 buf_dst + chunk_off,
1391 chunk_size)) != chunk_size) {
1392 if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
1393 goto end;
1394 }
1395
1396 /* verify the payload */
1397 if (!TEST_mem_eq(buf_dst + chunk_off,
1398 chunk_size,
1399 buf + chunk_off,
1400 chunk_size))
1401 goto end;
1402
1403 chunk_off += chunk_size;
1404 }
1405
1406 testresult = 1;
1407 end:
1408 if (clientssl) {
1409 SSL_shutdown(clientssl);
1410 SSL_free(clientssl);
1411 }
1412 if (serverssl) {
1413 SSL_shutdown(serverssl);
1414 SSL_free(serverssl);
1415 }
1416 SSL_CTX_free(sctx);
1417 SSL_CTX_free(cctx);
1418 serverssl = clientssl = NULL;
1419 BIO_free(out);
1420 BIO_free(in);
1421 if (cfd != -1)
1422 close(cfd);
1423 if (sfd != -1)
1424 close(sfd);
1425 OPENSSL_free(buf);
1426 OPENSSL_free(buf_dst);
1427 return testresult;
1428 }
1429
1430 static struct ktls_test_cipher {
1431 int tls_version;
1432 const char *cipher;
1433 } ktls_test_ciphers[] = {
1434 # if !defined(OPENSSL_NO_TLS1_2)
1435 # ifdef OPENSSL_KTLS_AES_GCM_128
1436 { TLS1_2_VERSION, "AES128-GCM-SHA256" },
1437 # endif
1438 # ifdef OPENSSL_KTLS_AES_CCM_128
1439 { TLS1_2_VERSION, "AES128-CCM"},
1440 # endif
1441 # ifdef OPENSSL_KTLS_AES_GCM_256
1442 { TLS1_2_VERSION, "AES256-GCM-SHA384"},
1443 # endif
1444 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1445 { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
1446 # endif
1447 # endif
1448 # if !defined(OSSL_NO_USABLE_TLS1_3)
1449 # ifdef OPENSSL_KTLS_AES_GCM_128
1450 { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
1451 # endif
1452 # ifdef OPENSSL_KTLS_AES_CCM_128
1453 { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
1454 # endif
1455 # ifdef OPENSSL_KTLS_AES_GCM_256
1456 { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
1457 # endif
1458 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1459 { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
1460 # endif
1461 # endif
1462 };
1463
1464 #define NUM_KTLS_TEST_CIPHERS \
1465 (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
1466
1467 static int test_ktls(int test)
1468 {
1469 struct ktls_test_cipher *cipher;
1470 int cis_ktls, sis_ktls;
1471
1472 OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
1473 cipher = &ktls_test_ciphers[test / 4];
1474
1475 cis_ktls = (test & 1) != 0;
1476 sis_ktls = (test & 2) != 0;
1477
1478 return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
1479 cipher->cipher);
1480 }
1481
1482 static int test_ktls_sendfile(int tst)
1483 {
1484 struct ktls_test_cipher *cipher;
1485
1486 OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
1487 cipher = &ktls_test_ciphers[tst];
1488
1489 return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher);
1490 }
1491 #endif
1492
1493 static int test_large_message_tls(void)
1494 {
1495 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1496 TLS1_VERSION, 0, 0);
1497 }
1498
1499 static int test_large_message_tls_read_ahead(void)
1500 {
1501 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1502 TLS1_VERSION, 0, 1);
1503 }
1504
1505 #ifndef OPENSSL_NO_DTLS
1506 static int test_large_message_dtls(void)
1507 {
1508 # ifdef OPENSSL_NO_DTLS1_2
1509 /* Not supported in the FIPS provider */
1510 if (is_fips)
1511 return 1;
1512 # endif
1513 /*
1514 * read_ahead is not relevant to DTLS because DTLS always acts as if
1515 * read_ahead is set.
1516 */
1517 return execute_test_large_message(DTLS_server_method(),
1518 DTLS_client_method(),
1519 DTLS1_VERSION, 0, 0);
1520 }
1521 #endif
1522
1523 static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
1524 const SSL_METHOD *cmeth,
1525 int min_version, int max_version)
1526 {
1527 size_t i;
1528 SSL_CTX *cctx = NULL, *sctx = NULL;
1529 SSL *clientssl = NULL, *serverssl = NULL;
1530 int testresult = 0;
1531 void *zbuf;
1532 SSL_CONNECTION *serversc;
1533 TLS_RECORD *rr;
1534
1535 static unsigned char cbuf[16000];
1536 static unsigned char sbuf[16000];
1537
1538 if (!TEST_true(create_ssl_ctx_pair(libctx,
1539 smeth, cmeth,
1540 min_version, max_version,
1541 &sctx, &cctx, cert,
1542 privkey)))
1543 goto end;
1544
1545 #ifdef OPENSSL_NO_DTLS1_2
1546 if (smeth == DTLS_server_method()) {
1547 # ifdef OPENSSL_NO_DTLS1_2
1548 /* Not supported in the FIPS provider */
1549 if (is_fips) {
1550 testresult = 1;
1551 goto end;
1552 };
1553 # endif
1554 /*
1555 * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1556 * level 0
1557 */
1558 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1559 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1560 "DEFAULT:@SECLEVEL=0")))
1561 goto end;
1562 }
1563 #endif
1564
1565 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1566 NULL, NULL)))
1567 goto end;
1568
1569 if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
1570 goto end;
1571
1572 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1573 SSL_ERROR_NONE)))
1574 goto end;
1575
1576 for (i = 0; i < sizeof(cbuf); i++) {
1577 cbuf[i] = i & 0xff;
1578 }
1579
1580 if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
1581 goto end;
1582
1583 if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1584 goto end;
1585
1586 if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1587 goto end;
1588
1589 /*
1590 * Since we called SSL_peek(), we know the data in the record
1591 * layer is a plaintext record. We can gather the pointer to check
1592 * for zeroization after SSL_read().
1593 */
1594 if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
1595 goto end;
1596 rr = serversc->rlayer.tlsrecs;
1597
1598 zbuf = &rr->data[rr->off];
1599 if (!TEST_int_eq(rr->length, sizeof(cbuf)))
1600 goto end;
1601
1602 /*
1603 * After SSL_peek() the plaintext must still be stored in the
1604 * record.
1605 */
1606 if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1607 goto end;
1608
1609 memset(sbuf, 0, sizeof(sbuf));
1610 if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1611 goto end;
1612
1613 if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
1614 goto end;
1615
1616 /* Check if rbuf is cleansed */
1617 memset(cbuf, 0, sizeof(cbuf));
1618 if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1619 goto end;
1620
1621 testresult = 1;
1622 end:
1623 SSL_free(serverssl);
1624 SSL_free(clientssl);
1625 SSL_CTX_free(sctx);
1626 SSL_CTX_free(cctx);
1627
1628 return testresult;
1629 }
1630
1631 static int test_cleanse_plaintext(void)
1632 {
1633 #if !defined(OPENSSL_NO_TLS1_2)
1634 if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1635 TLS_client_method(),
1636 TLS1_2_VERSION,
1637 TLS1_2_VERSION)))
1638 return 0;
1639
1640 #endif
1641
1642 #if !defined(OSSL_NO_USABLE_TLS1_3)
1643 if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1644 TLS_client_method(),
1645 TLS1_3_VERSION,
1646 TLS1_3_VERSION)))
1647 return 0;
1648 #endif
1649
1650 #if !defined(OPENSSL_NO_DTLS)
1651
1652 if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
1653 DTLS_client_method(),
1654 DTLS1_VERSION,
1655 0)))
1656 return 0;
1657 #endif
1658 return 1;
1659 }
1660
1661 #ifndef OPENSSL_NO_OCSP
1662 static int ocsp_server_cb(SSL *s, void *arg)
1663 {
1664 int *argi = (int *)arg;
1665 unsigned char *copy = NULL;
1666 STACK_OF(OCSP_RESPID) *ids = NULL;
1667 OCSP_RESPID *id = NULL;
1668
1669 if (*argi == 2) {
1670 /* In this test we are expecting exactly 1 OCSP_RESPID */
1671 SSL_get_tlsext_status_ids(s, &ids);
1672 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
1673 return SSL_TLSEXT_ERR_ALERT_FATAL;
1674
1675 id = sk_OCSP_RESPID_value(ids, 0);
1676 if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
1677 return SSL_TLSEXT_ERR_ALERT_FATAL;
1678 } else if (*argi != 1) {
1679 return SSL_TLSEXT_ERR_ALERT_FATAL;
1680 }
1681
1682 if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
1683 return SSL_TLSEXT_ERR_ALERT_FATAL;
1684
1685 if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
1686 sizeof(orespder)))) {
1687 OPENSSL_free(copy);
1688 return SSL_TLSEXT_ERR_ALERT_FATAL;
1689 }
1690 ocsp_server_called = 1;
1691 return SSL_TLSEXT_ERR_OK;
1692 }
1693
1694 static int ocsp_client_cb(SSL *s, void *arg)
1695 {
1696 int *argi = (int *)arg;
1697 const unsigned char *respderin;
1698 size_t len;
1699
1700 if (*argi != 1 && *argi != 2)
1701 return 0;
1702
1703 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
1704 if (!TEST_mem_eq(orespder, len, respderin, len))
1705 return 0;
1706
1707 ocsp_client_called = 1;
1708 return 1;
1709 }
1710
1711 static int test_tlsext_status_type(void)
1712 {
1713 SSL_CTX *cctx = NULL, *sctx = NULL;
1714 SSL *clientssl = NULL, *serverssl = NULL;
1715 int testresult = 0;
1716 STACK_OF(OCSP_RESPID) *ids = NULL;
1717 OCSP_RESPID *id = NULL;
1718 BIO *certbio = NULL;
1719
1720 if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
1721 TLS1_VERSION, 0,
1722 &sctx, &cctx, cert, privkey))
1723 return 0;
1724
1725 if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
1726 goto end;
1727
1728 /* First just do various checks getting and setting tlsext_status_type */
1729
1730 clientssl = SSL_new(cctx);
1731 if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
1732 || !TEST_true(SSL_set_tlsext_status_type(clientssl,
1733 TLSEXT_STATUSTYPE_ocsp))
1734 || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
1735 TLSEXT_STATUSTYPE_ocsp))
1736 goto end;
1737
1738 SSL_free(clientssl);
1739 clientssl = NULL;
1740
1741 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
1742 || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
1743 goto end;
1744
1745 clientssl = SSL_new(cctx);
1746 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
1747 goto end;
1748 SSL_free(clientssl);
1749 clientssl = NULL;
1750
1751 /*
1752 * Now actually do a handshake and check OCSP information is exchanged and
1753 * the callbacks get called
1754 */
1755 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
1756 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
1757 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
1758 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
1759 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1760 &clientssl, NULL, NULL))
1761 || !TEST_true(create_ssl_connection(serverssl, clientssl,
1762 SSL_ERROR_NONE))
1763 || !TEST_true(ocsp_client_called)
1764 || !TEST_true(ocsp_server_called))
1765 goto end;
1766 SSL_free(serverssl);
1767 SSL_free(clientssl);
1768 serverssl = NULL;
1769 clientssl = NULL;
1770
1771 /* Try again but this time force the server side callback to fail */
1772 ocsp_client_called = 0;
1773 ocsp_server_called = 0;
1774 cdummyarg = 0;
1775 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1776 &clientssl, NULL, NULL))
1777 /* This should fail because the callback will fail */
1778 || !TEST_false(create_ssl_connection(serverssl, clientssl,
1779 SSL_ERROR_NONE))
1780 || !TEST_false(ocsp_client_called)
1781 || !TEST_false(ocsp_server_called))
1782 goto end;
1783 SSL_free(serverssl);
1784 SSL_free(clientssl);
1785 serverssl = NULL;
1786 clientssl = NULL;
1787
1788 /*
1789 * This time we'll get the client to send an OCSP_RESPID that it will
1790 * accept.
1791 */
1792 ocsp_client_called = 0;
1793 ocsp_server_called = 0;
1794 cdummyarg = 2;
1795 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1796 &clientssl, NULL, NULL)))
1797 goto end;
1798
1799 /*
1800 * We'll just use any old cert for this test - it doesn't have to be an OCSP
1801 * specific one. We'll use the server cert.
1802 */
1803 if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
1804 || !TEST_ptr(id = OCSP_RESPID_new())
1805 || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
1806 || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
1807 || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
1808 || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
1809 || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
1810 goto end;
1811 id = NULL;
1812 SSL_set_tlsext_status_ids(clientssl, ids);
1813 /* Control has been transferred */
1814 ids = NULL;
1815
1816 BIO_free(certbio);
1817 certbio = NULL;
1818
1819 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1820 SSL_ERROR_NONE))
1821 || !TEST_true(ocsp_client_called)
1822 || !TEST_true(ocsp_server_called))
1823 goto end;
1824
1825 testresult = 1;
1826
1827 end:
1828 SSL_free(serverssl);
1829 SSL_free(clientssl);
1830 SSL_CTX_free(sctx);
1831 SSL_CTX_free(cctx);
1832 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
1833 OCSP_RESPID_free(id);
1834 BIO_free(certbio);
1835 X509_free(ocspcert);
1836 ocspcert = NULL;
1837
1838 return testresult;
1839 }
1840 #endif
1841
1842 #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
1843 static int new_called, remove_called, get_called;
1844
1845 static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
1846 {
1847 new_called++;
1848 /*
1849 * sess has been up-refed for us, but we don't actually need it so free it
1850 * immediately.
1851 */
1852 SSL_SESSION_free(sess);
1853 return 1;
1854 }
1855
1856 static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
1857 {
1858 remove_called++;
1859 }
1860
1861 static SSL_SESSION *get_sess_val = NULL;
1862
1863 static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
1864 int *copy)
1865 {
1866 get_called++;
1867 *copy = 1;
1868 return get_sess_val;
1869 }
1870
1871 static int execute_test_session(int maxprot, int use_int_cache,
1872 int use_ext_cache, long s_options)
1873 {
1874 SSL_CTX *sctx = NULL, *cctx = NULL;
1875 SSL *serverssl1 = NULL, *clientssl1 = NULL;
1876 SSL *serverssl2 = NULL, *clientssl2 = NULL;
1877 # ifndef OPENSSL_NO_TLS1_1
1878 SSL *serverssl3 = NULL, *clientssl3 = NULL;
1879 # endif
1880 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
1881 int testresult = 0, numnewsesstick = 1;
1882
1883 new_called = remove_called = 0;
1884
1885 /* TLSv1.3 sends 2 NewSessionTickets */
1886 if (maxprot == TLS1_3_VERSION)
1887 numnewsesstick = 2;
1888
1889 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1890 TLS_client_method(), TLS1_VERSION, 0,
1891 &sctx, &cctx, cert, privkey)))
1892 return 0;
1893
1894 /*
1895 * Only allow the max protocol version so we can force a connection failure
1896 * later
1897 */
1898 SSL_CTX_set_min_proto_version(cctx, maxprot);
1899 SSL_CTX_set_max_proto_version(cctx, maxprot);
1900
1901 /* Set up session cache */
1902 if (use_ext_cache) {
1903 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
1904 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
1905 }
1906 if (use_int_cache) {
1907 /* Also covers instance where both are set */
1908 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
1909 } else {
1910 SSL_CTX_set_session_cache_mode(cctx,
1911 SSL_SESS_CACHE_CLIENT
1912 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1913 }
1914
1915 if (s_options) {
1916 SSL_CTX_set_options(sctx, s_options);
1917 }
1918
1919 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
1920 NULL, NULL))
1921 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
1922 SSL_ERROR_NONE))
1923 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
1924 goto end;
1925
1926 /* Should fail because it should already be in the cache */
1927 if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
1928 goto end;
1929 if (use_ext_cache
1930 && (!TEST_int_eq(new_called, numnewsesstick)
1931
1932 || !TEST_int_eq(remove_called, 0)))
1933 goto end;
1934
1935 new_called = remove_called = 0;
1936 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1937 &clientssl2, NULL, NULL))
1938 || !TEST_true(SSL_set_session(clientssl2, sess1))
1939 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1940 SSL_ERROR_NONE))
1941 || !TEST_true(SSL_session_reused(clientssl2)))
1942 goto end;
1943
1944 if (maxprot == TLS1_3_VERSION) {
1945 /*
1946 * In TLSv1.3 we should have created a new session even though we have
1947 * resumed. Since we attempted a resume we should also have removed the
1948 * old ticket from the cache so that we try to only use tickets once.
1949 */
1950 if (use_ext_cache
1951 && (!TEST_int_eq(new_called, 1)
1952 || !TEST_int_eq(remove_called, 1)))
1953 goto end;
1954 } else {
1955 /*
1956 * In TLSv1.2 we expect to have resumed so no sessions added or
1957 * removed.
1958 */
1959 if (use_ext_cache
1960 && (!TEST_int_eq(new_called, 0)
1961 || !TEST_int_eq(remove_called, 0)))
1962 goto end;
1963 }
1964
1965 SSL_SESSION_free(sess1);
1966 if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
1967 goto end;
1968 shutdown_ssl_connection(serverssl2, clientssl2);
1969 serverssl2 = clientssl2 = NULL;
1970
1971 new_called = remove_called = 0;
1972 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1973 &clientssl2, NULL, NULL))
1974 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1975 SSL_ERROR_NONE)))
1976 goto end;
1977
1978 if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
1979 goto end;
1980
1981 if (use_ext_cache
1982 && (!TEST_int_eq(new_called, numnewsesstick)
1983 || !TEST_int_eq(remove_called, 0)))
1984 goto end;
1985
1986 new_called = remove_called = 0;
1987 /*
1988 * This should clear sess2 from the cache because it is a "bad" session.
1989 * See SSL_set_session() documentation.
1990 */
1991 if (!TEST_true(SSL_set_session(clientssl2, sess1)))
1992 goto end;
1993 if (use_ext_cache
1994 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
1995 goto end;
1996 if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
1997 goto end;
1998
1999 if (use_int_cache) {
2000 /* Should succeeded because it should not already be in the cache */
2001 if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
2002 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
2003 goto end;
2004 }
2005
2006 new_called = remove_called = 0;
2007 /* This shouldn't be in the cache so should fail */
2008 if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
2009 goto end;
2010
2011 if (use_ext_cache
2012 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2013 goto end;
2014
2015 # if !defined(OPENSSL_NO_TLS1_1)
2016 new_called = remove_called = 0;
2017 /* Force a connection failure */
2018 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
2019 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
2020 &clientssl3, NULL, NULL))
2021 || !TEST_true(SSL_set_session(clientssl3, sess1))
2022 /* This should fail because of the mismatched protocol versions */
2023 || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
2024 SSL_ERROR_NONE)))
2025 goto end;
2026
2027 /* We should have automatically removed the session from the cache */
2028 if (use_ext_cache
2029 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2030 goto end;
2031
2032 /* Should succeed because it should not already be in the cache */
2033 if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
2034 goto end;
2035 # endif
2036
2037 /* Now do some tests for server side caching */
2038 if (use_ext_cache) {
2039 SSL_CTX_sess_set_new_cb(cctx, NULL);
2040 SSL_CTX_sess_set_remove_cb(cctx, NULL);
2041 SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2042 SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
2043 SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
2044 get_sess_val = NULL;
2045 }
2046
2047 SSL_CTX_set_session_cache_mode(cctx, 0);
2048 /* Internal caching is the default on the server side */
2049 if (!use_int_cache)
2050 SSL_CTX_set_session_cache_mode(sctx,
2051 SSL_SESS_CACHE_SERVER
2052 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2053
2054 SSL_free(serverssl1);
2055 SSL_free(clientssl1);
2056 serverssl1 = clientssl1 = NULL;
2057 SSL_free(serverssl2);
2058 SSL_free(clientssl2);
2059 serverssl2 = clientssl2 = NULL;
2060 SSL_SESSION_free(sess1);
2061 sess1 = NULL;
2062 SSL_SESSION_free(sess2);
2063 sess2 = NULL;
2064
2065 SSL_CTX_set_max_proto_version(sctx, maxprot);
2066 if (maxprot == TLS1_2_VERSION)
2067 SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
2068 new_called = remove_called = get_called = 0;
2069 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2070 NULL, NULL))
2071 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2072 SSL_ERROR_NONE))
2073 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
2074 || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
2075 goto end;
2076
2077 if (use_int_cache) {
2078 if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
2079 /*
2080 * In TLSv1.3 it should not have been added to the internal cache,
2081 * except in the case where we also have an external cache (in that
2082 * case it gets added to the cache in order to generate remove
2083 * events after timeout).
2084 */
2085 if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
2086 goto end;
2087 } else {
2088 /* Should fail because it should already be in the cache */
2089 if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
2090 goto end;
2091 }
2092 }
2093
2094 if (use_ext_cache) {
2095 SSL_SESSION *tmp = sess2;
2096
2097 if (!TEST_int_eq(new_called, numnewsesstick)
2098 || !TEST_int_eq(remove_called, 0)
2099 || !TEST_int_eq(get_called, 0))
2100 goto end;
2101 /*
2102 * Delete the session from the internal cache to force a lookup from
2103 * the external cache. We take a copy first because
2104 * SSL_CTX_remove_session() also marks the session as non-resumable.
2105 */
2106 if (use_int_cache && maxprot != TLS1_3_VERSION) {
2107 if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
2108 || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
2109 goto end;
2110 SSL_SESSION_free(sess2);
2111 }
2112 sess2 = tmp;
2113 }
2114
2115 new_called = remove_called = get_called = 0;
2116 get_sess_val = sess2;
2117 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2118 &clientssl2, NULL, NULL))
2119 || !TEST_true(SSL_set_session(clientssl2, sess1))
2120 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2121 SSL_ERROR_NONE))
2122 || !TEST_true(SSL_session_reused(clientssl2)))
2123 goto end;
2124
2125 if (use_ext_cache) {
2126 if (!TEST_int_eq(remove_called, 0))
2127 goto end;
2128
2129 if (maxprot == TLS1_3_VERSION) {
2130 if (!TEST_int_eq(new_called, 1)
2131 || !TEST_int_eq(get_called, 0))
2132 goto end;
2133 } else {
2134 if (!TEST_int_eq(new_called, 0)
2135 || !TEST_int_eq(get_called, 1))
2136 goto end;
2137 }
2138 }
2139 /*
2140 * Make a small cache, force out all other sessions but
2141 * sess2, try to add sess1, which should succeed. Then
2142 * make sure it's there by checking the owners. Despite
2143 * the timeouts, sess1 should have kicked out sess2
2144 */
2145
2146 /* Make sess1 expire before sess2 */
2147 if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
2148 || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
2149 || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
2150 || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
2151 goto end;
2152
2153 if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
2154 goto end;
2155
2156 /* Don't care about results - cache should only be sess2 at end */
2157 SSL_CTX_add_session(sctx, sess1);
2158 SSL_CTX_add_session(sctx, sess2);
2159
2160 /* Now add sess1, and make sure it remains, despite timeout */
2161 if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
2162 || !TEST_ptr(sess1->owner)
2163 || !TEST_ptr_null(sess2->owner))
2164 goto end;
2165
2166 testresult = 1;
2167
2168 end:
2169 SSL_free(serverssl1);
2170 SSL_free(clientssl1);
2171 SSL_free(serverssl2);
2172 SSL_free(clientssl2);
2173 # ifndef OPENSSL_NO_TLS1_1
2174 SSL_free(serverssl3);
2175 SSL_free(clientssl3);
2176 # endif
2177 SSL_SESSION_free(sess1);
2178 SSL_SESSION_free(sess2);
2179 SSL_CTX_free(sctx);
2180 SSL_CTX_free(cctx);
2181
2182 return testresult;
2183 }
2184 #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
2185
2186 static int test_session_with_only_int_cache(void)
2187 {
2188 #ifndef OSSL_NO_USABLE_TLS1_3
2189 if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
2190 return 0;
2191 #endif
2192
2193 #ifndef OPENSSL_NO_TLS1_2
2194 return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
2195 #else
2196 return 1;
2197 #endif
2198 }
2199
2200 static int test_session_with_only_ext_cache(void)
2201 {
2202 #ifndef OSSL_NO_USABLE_TLS1_3
2203 if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
2204 return 0;
2205 #endif
2206
2207 #ifndef OPENSSL_NO_TLS1_2
2208 return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
2209 #else
2210 return 1;
2211 #endif
2212 }
2213
2214 static int test_session_with_both_cache(void)
2215 {
2216 #ifndef OSSL_NO_USABLE_TLS1_3
2217 if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
2218 return 0;
2219 #endif
2220
2221 #ifndef OPENSSL_NO_TLS1_2
2222 return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
2223 #else
2224 return 1;
2225 #endif
2226 }
2227
2228 static int test_session_wo_ca_names(void)
2229 {
2230 #ifndef OSSL_NO_USABLE_TLS1_3
2231 if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
2232 return 0;
2233 #endif
2234
2235 #ifndef OPENSSL_NO_TLS1_2
2236 return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
2237 #else
2238 return 1;
2239 #endif
2240 }
2241
2242
2243 #ifndef OSSL_NO_USABLE_TLS1_3
2244 static SSL_SESSION *sesscache[6];
2245 static int do_cache;
2246
2247 static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
2248 {
2249 if (do_cache) {
2250 sesscache[new_called] = sess;
2251 } else {
2252 /* We don't need the reference to the session, so free it */
2253 SSL_SESSION_free(sess);
2254 }
2255 new_called++;
2256
2257 return 1;
2258 }
2259
2260 static int post_handshake_verify(SSL *sssl, SSL *cssl)
2261 {
2262 SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
2263 if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
2264 return 0;
2265
2266 /* Start handshake on the server and client */
2267 if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
2268 || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
2269 || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
2270 || !TEST_true(create_ssl_connection(sssl, cssl,
2271 SSL_ERROR_NONE)))
2272 return 0;
2273
2274 return 1;
2275 }
2276
2277 static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
2278 SSL_CTX **cctx)
2279 {
2280 int sess_id_ctx = 1;
2281
2282 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2283 TLS_client_method(), TLS1_VERSION, 0,
2284 sctx, cctx, cert, privkey))
2285 || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
2286 || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
2287 (void *)&sess_id_ctx,
2288 sizeof(sess_id_ctx))))
2289 return 0;
2290
2291 if (stateful)
2292 SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
2293
2294 SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
2295 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2296 SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
2297
2298 return 1;
2299 }
2300
2301 static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
2302 {
2303 SSL *serverssl = NULL, *clientssl = NULL;
2304 int i;
2305
2306 /* Test that we can resume with all the tickets we got given */
2307 for (i = 0; i < idx * 2; i++) {
2308 new_called = 0;
2309 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2310 &clientssl, NULL, NULL))
2311 || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
2312 goto end;
2313
2314 SSL_set_post_handshake_auth(clientssl, 1);
2315
2316 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2317 SSL_ERROR_NONE)))
2318 goto end;
2319
2320 /*
2321 * Following a successful resumption we only get 1 ticket. After a
2322 * failed one we should get idx tickets.
2323 */
2324 if (succ) {
2325 if (!TEST_true(SSL_session_reused(clientssl))
2326 || !TEST_int_eq(new_called, 1))
2327 goto end;
2328 } else {
2329 if (!TEST_false(SSL_session_reused(clientssl))
2330 || !TEST_int_eq(new_called, idx))
2331 goto end;
2332 }
2333
2334 new_called = 0;
2335 /* After a post-handshake authentication we should get 1 new ticket */
2336 if (succ
2337 && (!post_handshake_verify(serverssl, clientssl)
2338 || !TEST_int_eq(new_called, 1)))
2339 goto end;
2340
2341 SSL_shutdown(clientssl);
2342 SSL_shutdown(serverssl);
2343 SSL_free(serverssl);
2344 SSL_free(clientssl);
2345 serverssl = clientssl = NULL;
2346 SSL_SESSION_free(sesscache[i]);
2347 sesscache[i] = NULL;
2348 }
2349
2350 return 1;
2351
2352 end:
2353 SSL_free(clientssl);
2354 SSL_free(serverssl);
2355 return 0;
2356 }
2357
2358 static int test_tickets(int stateful, int idx)
2359 {
2360 SSL_CTX *sctx = NULL, *cctx = NULL;
2361 SSL *serverssl = NULL, *clientssl = NULL;
2362 int testresult = 0;
2363 size_t j;
2364
2365 /* idx is the test number, but also the number of tickets we want */
2366
2367 new_called = 0;
2368 do_cache = 1;
2369
2370 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2371 goto end;
2372
2373 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2374 &clientssl, NULL, NULL)))
2375 goto end;
2376
2377 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2378 SSL_ERROR_NONE))
2379 /* Check we got the number of tickets we were expecting */
2380 || !TEST_int_eq(idx, new_called))
2381 goto end;
2382
2383 SSL_shutdown(clientssl);
2384 SSL_shutdown(serverssl);
2385 SSL_free(serverssl);
2386 SSL_free(clientssl);
2387 SSL_CTX_free(sctx);
2388 SSL_CTX_free(cctx);
2389 clientssl = serverssl = NULL;
2390 sctx = cctx = NULL;
2391
2392 /*
2393 * Now we try to resume with the tickets we previously created. The
2394 * resumption attempt is expected to fail (because we're now using a new
2395 * SSL_CTX). We should see idx number of tickets issued again.
2396 */
2397
2398 /* Stop caching sessions - just count them */
2399 do_cache = 0;
2400
2401 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2402 goto end;
2403
2404 if (!check_resumption(idx, sctx, cctx, 0))
2405 goto end;
2406
2407 /* Start again with caching sessions */
2408 new_called = 0;
2409 do_cache = 1;
2410 SSL_CTX_free(sctx);
2411 SSL_CTX_free(cctx);
2412 sctx = cctx = NULL;
2413
2414 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2415 goto end;
2416
2417 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2418 &clientssl, NULL, NULL)))
2419 goto end;
2420
2421 SSL_set_post_handshake_auth(clientssl, 1);
2422
2423 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2424 SSL_ERROR_NONE))
2425 /* Check we got the number of tickets we were expecting */
2426 || !TEST_int_eq(idx, new_called))
2427 goto end;
2428
2429 /* After a post-handshake authentication we should get new tickets issued */
2430 if (!post_handshake_verify(serverssl, clientssl)
2431 || !TEST_int_eq(idx * 2, new_called))
2432 goto end;
2433
2434 SSL_shutdown(clientssl);
2435 SSL_shutdown(serverssl);
2436 SSL_free(serverssl);
2437 SSL_free(clientssl);
2438 serverssl = clientssl = NULL;
2439
2440 /* Stop caching sessions - just count them */
2441 do_cache = 0;
2442
2443 /*
2444 * Check we can resume with all the tickets we created. This time around the
2445 * resumptions should all be successful.
2446 */
2447 if (!check_resumption(idx, sctx, cctx, 1))
2448 goto end;
2449
2450 testresult = 1;
2451
2452 end:
2453 SSL_free(serverssl);
2454 SSL_free(clientssl);
2455 for (j = 0; j < OSSL_NELEM(sesscache); j++) {
2456 SSL_SESSION_free(sesscache[j]);
2457 sesscache[j] = NULL;
2458 }
2459 SSL_CTX_free(sctx);
2460 SSL_CTX_free(cctx);
2461
2462 return testresult;
2463 }
2464
2465 static int test_stateless_tickets(int idx)
2466 {
2467 return test_tickets(0, idx);
2468 }
2469
2470 static int test_stateful_tickets(int idx)
2471 {
2472 return test_tickets(1, idx);
2473 }
2474
2475 static int test_psk_tickets(void)
2476 {
2477 SSL_CTX *sctx = NULL, *cctx = NULL;
2478 SSL *serverssl = NULL, *clientssl = NULL;
2479 int testresult = 0;
2480 int sess_id_ctx = 1;
2481
2482 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2483 TLS_client_method(), TLS1_VERSION, 0,
2484 &sctx, &cctx, NULL, NULL))
2485 || !TEST_true(SSL_CTX_set_session_id_context(sctx,
2486 (void *)&sess_id_ctx,
2487 sizeof(sess_id_ctx))))
2488 goto end;
2489
2490 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
2491 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2492 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
2493 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
2494 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2495 use_session_cb_cnt = 0;
2496 find_session_cb_cnt = 0;
2497 srvid = pskid;
2498 new_called = 0;
2499
2500 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2501 NULL, NULL)))
2502 goto end;
2503 clientpsk = serverpsk = create_a_psk(clientssl);
2504 if (!TEST_ptr(clientpsk))
2505 goto end;
2506 SSL_SESSION_up_ref(clientpsk);
2507
2508 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2509 SSL_ERROR_NONE))
2510 || !TEST_int_eq(1, find_session_cb_cnt)
2511 || !TEST_int_eq(1, use_session_cb_cnt)
2512 /* We should always get 1 ticket when using external PSK */
2513 || !TEST_int_eq(1, new_called))
2514 goto end;
2515
2516 testresult = 1;
2517
2518 end:
2519 SSL_free(serverssl);
2520 SSL_free(clientssl);
2521 SSL_CTX_free(sctx);
2522 SSL_CTX_free(cctx);
2523 SSL_SESSION_free(clientpsk);
2524 SSL_SESSION_free(serverpsk);
2525 clientpsk = serverpsk = NULL;
2526
2527 return testresult;
2528 }
2529
2530 static int test_extra_tickets(int idx)
2531 {
2532 SSL_CTX *sctx = NULL, *cctx = NULL;
2533 SSL *serverssl = NULL, *clientssl = NULL;
2534 BIO *bretry = BIO_new(bio_s_always_retry());
2535 BIO *tmp = NULL;
2536 int testresult = 0;
2537 int stateful = 0;
2538 size_t nbytes;
2539 unsigned char c, buf[1];
2540
2541 new_called = 0;
2542 do_cache = 1;
2543
2544 if (idx >= 3) {
2545 idx -= 3;
2546 stateful = 1;
2547 }
2548
2549 if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
2550 goto end;
2551 SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2552 /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
2553 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2554
2555 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2556 &clientssl, NULL, NULL)))
2557 goto end;
2558
2559 /*
2560 * Note that we have new_session_cb on both sctx and cctx, so new_called is
2561 * incremented by both client and server.
2562 */
2563 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2564 SSL_ERROR_NONE))
2565 /* Check we got the number of tickets we were expecting */
2566 || !TEST_int_eq(idx * 2, new_called)
2567 || !TEST_true(SSL_new_session_ticket(serverssl))
2568 || !TEST_true(SSL_new_session_ticket(serverssl))
2569 || !TEST_int_eq(idx * 2, new_called))
2570 goto end;
2571
2572 /* Now try a (real) write to actually send the tickets */
2573 c = '1';
2574 if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2575 || !TEST_size_t_eq(1, nbytes)
2576 || !TEST_int_eq(idx * 2 + 2, new_called)
2577 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2578 || !TEST_int_eq(idx * 2 + 4, new_called)
2579 || !TEST_int_eq(sizeof(buf), nbytes)
2580 || !TEST_int_eq(c, buf[0])
2581 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2582 goto end;
2583
2584 /* Try with only requesting one new ticket, too */
2585 c = '2';
2586 new_called = 0;
2587 if (!TEST_true(SSL_new_session_ticket(serverssl))
2588 || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
2589 || !TEST_size_t_eq(sizeof(c), nbytes)
2590 || !TEST_int_eq(1, new_called)
2591 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2592 || !TEST_int_eq(2, new_called)
2593 || !TEST_size_t_eq(sizeof(buf), nbytes)
2594 || !TEST_int_eq(c, buf[0]))
2595 goto end;
2596
2597 /* Do it again but use dummy writes to drive the ticket generation */
2598 c = '3';
2599 new_called = 0;
2600 if (!TEST_true(SSL_new_session_ticket(serverssl))
2601 || !TEST_true(SSL_new_session_ticket(serverssl))
2602 || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
2603 || !TEST_size_t_eq(0, nbytes)
2604 || !TEST_int_eq(2, new_called)
2605 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2606 || !TEST_int_eq(4, new_called))
2607 goto end;
2608
2609 /* Once more, but with SSL_do_handshake() to drive the ticket generation */
2610 c = '4';
2611 new_called = 0;
2612 if (!TEST_true(SSL_new_session_ticket(serverssl))
2613 || !TEST_true(SSL_new_session_ticket(serverssl))
2614 || !TEST_true(SSL_do_handshake(serverssl))
2615 || !TEST_int_eq(2, new_called)
2616 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2617 || !TEST_int_eq(4, new_called))
2618 goto end;
2619
2620 /*
2621 * Use the always-retry BIO to exercise the logic that forces ticket
2622 * generation to wait until a record boundary.
2623 */
2624 c = '5';
2625 new_called = 0;
2626 tmp = SSL_get_wbio(serverssl);
2627 if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
2628 tmp = NULL;
2629 goto end;
2630 }
2631 SSL_set0_wbio(serverssl, bretry);
2632 bretry = NULL;
2633 if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
2634 || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
2635 || !TEST_size_t_eq(nbytes, 0))
2636 goto end;
2637 /* Restore a BIO that will let the write succeed */
2638 SSL_set0_wbio(serverssl, tmp);
2639 tmp = NULL;
2640 /*
2641 * These calls should just queue the request and not send anything
2642 * even if we explicitly try to hit the state machine.
2643 */
2644 if (!TEST_true(SSL_new_session_ticket(serverssl))
2645 || !TEST_true(SSL_new_session_ticket(serverssl))
2646 || !TEST_int_eq(0, new_called)
2647 || !TEST_true(SSL_do_handshake(serverssl))
2648 || !TEST_int_eq(0, new_called))
2649 goto end;
2650 /* Re-do the write; still no tickets sent */
2651 if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2652 || !TEST_size_t_eq(1, nbytes)
2653 || !TEST_int_eq(0, new_called)
2654 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2655 || !TEST_int_eq(0, new_called)
2656 || !TEST_int_eq(sizeof(buf), nbytes)
2657 || !TEST_int_eq(c, buf[0])
2658 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2659 goto end;
2660 /* Even trying to hit the state machine now will still not send tickets */
2661 if (!TEST_true(SSL_do_handshake(serverssl))
2662 || !TEST_int_eq(0, new_called))
2663 goto end;
2664 /* Now the *next* write should send the tickets */
2665 c = '6';
2666 if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2667 || !TEST_size_t_eq(1, nbytes)
2668 || !TEST_int_eq(2, new_called)
2669 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2670 || !TEST_int_eq(4, new_called)
2671 || !TEST_int_eq(sizeof(buf), nbytes)
2672 || !TEST_int_eq(c, buf[0])
2673 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2674 goto end;
2675
2676 SSL_shutdown(clientssl);
2677 SSL_shutdown(serverssl);
2678 testresult = 1;
2679
2680 end:
2681 BIO_free(bretry);
2682 BIO_free(tmp);
2683 SSL_free(serverssl);
2684 SSL_free(clientssl);
2685 SSL_CTX_free(sctx);
2686 SSL_CTX_free(cctx);
2687 clientssl = serverssl = NULL;
2688 sctx = cctx = NULL;
2689 return testresult;
2690 }
2691 #endif
2692
2693 #define USE_NULL 0
2694 #define USE_BIO_1 1
2695 #define USE_BIO_2 2
2696 #define USE_DEFAULT 3
2697
2698 #define CONNTYPE_CONNECTION_SUCCESS 0
2699 #define CONNTYPE_CONNECTION_FAIL 1
2700 #define CONNTYPE_NO_CONNECTION 2
2701
2702 #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
2703 #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
2704 #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
2705 # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
2706 #else
2707 # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
2708 #endif
2709
2710 #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
2711 + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
2712 + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
2713
2714 static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
2715 {
2716 switch (type) {
2717 case USE_NULL:
2718 *res = NULL;
2719 break;
2720 case USE_BIO_1:
2721 *res = bio1;
2722 break;
2723 case USE_BIO_2:
2724 *res = bio2;
2725 break;
2726 }
2727 }
2728
2729
2730 /*
2731 * Tests calls to SSL_set_bio() under various conditions.
2732 *
2733 * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
2734 * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
2735 * then do more tests where we create a successful connection first using our
2736 * standard connection setup functions, and then call SSL_set_bio() with
2737 * various combinations of valid BIOs or NULL. We then repeat these tests
2738 * following a failed connection. In this last case we are looking to check that
2739 * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
2740 */
2741 static int test_ssl_set_bio(int idx)
2742 {
2743 SSL_CTX *sctx = NULL, *cctx = NULL;
2744 BIO *bio1 = NULL;
2745 BIO *bio2 = NULL;
2746 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
2747 SSL *serverssl = NULL, *clientssl = NULL;
2748 int initrbio, initwbio, newrbio, newwbio, conntype;
2749 int testresult = 0;
2750
2751 if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
2752 initrbio = idx % 3;
2753 idx /= 3;
2754 initwbio = idx % 3;
2755 idx /= 3;
2756 newrbio = idx % 3;
2757 idx /= 3;
2758 newwbio = idx % 3;
2759 conntype = CONNTYPE_NO_CONNECTION;
2760 } else {
2761 idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
2762 initrbio = initwbio = USE_DEFAULT;
2763 newrbio = idx % 2;
2764 idx /= 2;
2765 newwbio = idx % 2;
2766 idx /= 2;
2767 conntype = idx % 2;
2768 }
2769
2770 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2771 TLS_client_method(), TLS1_VERSION, 0,
2772 &sctx, &cctx, cert, privkey)))
2773 goto end;
2774
2775 if (conntype == CONNTYPE_CONNECTION_FAIL) {
2776 /*
2777 * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
2778 * because we reduced the number of tests in the definition of
2779 * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
2780 * mismatched protocol versions we will force a connection failure.
2781 */
2782 SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
2783 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2784 }
2785
2786 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2787 NULL, NULL)))
2788 goto end;
2789
2790 if (initrbio == USE_BIO_1
2791 || initwbio == USE_BIO_1
2792 || newrbio == USE_BIO_1
2793 || newwbio == USE_BIO_1) {
2794 if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
2795 goto end;
2796 }
2797
2798 if (initrbio == USE_BIO_2
2799 || initwbio == USE_BIO_2
2800 || newrbio == USE_BIO_2
2801 || newwbio == USE_BIO_2) {
2802 if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
2803 goto end;
2804 }
2805
2806 if (initrbio != USE_DEFAULT) {
2807 setupbio(&irbio, bio1, bio2, initrbio);
2808 setupbio(&iwbio, bio1, bio2, initwbio);
2809 SSL_set_bio(clientssl, irbio, iwbio);
2810
2811 /*
2812 * We want to maintain our own refs to these BIO, so do an up ref for
2813 * each BIO that will have ownership transferred in the SSL_set_bio()
2814 * call
2815 */
2816 if (irbio != NULL)
2817 BIO_up_ref(irbio);
2818 if (iwbio != NULL && iwbio != irbio)
2819 BIO_up_ref(iwbio);
2820 }
2821
2822 if (conntype != CONNTYPE_NO_CONNECTION
2823 && !TEST_true(create_ssl_connection(serverssl, clientssl,
2824 SSL_ERROR_NONE)
2825 == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
2826 goto end;
2827
2828 setupbio(&nrbio, bio1, bio2, newrbio);
2829 setupbio(&nwbio, bio1, bio2, newwbio);
2830
2831 /*
2832 * We will (maybe) transfer ownership again so do more up refs.
2833 * SSL_set_bio() has some really complicated ownership rules where BIOs have
2834 * already been set!
2835 */
2836 if (nrbio != NULL
2837 && nrbio != irbio
2838 && (nwbio != iwbio || nrbio != nwbio))
2839 BIO_up_ref(nrbio);
2840 if (nwbio != NULL
2841 && nwbio != nrbio
2842 && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
2843 BIO_up_ref(nwbio);
2844
2845 SSL_set_bio(clientssl, nrbio, nwbio);
2846
2847 testresult = 1;
2848
2849 end:
2850 BIO_free(bio1);
2851 BIO_free(bio2);
2852
2853 /*
2854 * This test is checking that the ref counting for SSL_set_bio is correct.
2855 * If we get here and we did too many frees then we will fail in the above
2856 * functions.
2857 */
2858 SSL_free(serverssl);
2859 SSL_free(clientssl);
2860 SSL_CTX_free(sctx);
2861 SSL_CTX_free(cctx);
2862 return testresult;
2863 }
2864
2865 typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
2866
2867 static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
2868 {
2869 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
2870 SSL_CTX *ctx;
2871 SSL *ssl = NULL;
2872 int testresult = 0;
2873
2874 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
2875 || !TEST_ptr(ssl = SSL_new(ctx))
2876 || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
2877 || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
2878 goto end;
2879
2880 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
2881
2882 /*
2883 * If anything goes wrong here then we could leak memory.
2884 */
2885 BIO_push(sslbio, membio1);
2886
2887 /* Verify changing the rbio/wbio directly does not cause leaks */
2888 if (change_bio != NO_BIO_CHANGE) {
2889 if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
2890 ssl = NULL;
2891 goto end;
2892 }
2893 if (change_bio == CHANGE_RBIO)
2894 SSL_set0_rbio(ssl, membio2);
2895 else
2896 SSL_set0_wbio(ssl, membio2);
2897 }
2898 ssl = NULL;
2899
2900 if (pop_ssl)
2901 BIO_pop(sslbio);
2902 else
2903 BIO_pop(membio1);
2904
2905 testresult = 1;
2906 end:
2907 BIO_free(membio1);
2908 BIO_free(sslbio);
2909 SSL_free(ssl);
2910 SSL_CTX_free(ctx);
2911
2912 return testresult;
2913 }
2914
2915 static int test_ssl_bio_pop_next_bio(void)
2916 {
2917 return execute_test_ssl_bio(0, NO_BIO_CHANGE);
2918 }
2919
2920 static int test_ssl_bio_pop_ssl_bio(void)
2921 {
2922 return execute_test_ssl_bio(1, NO_BIO_CHANGE);
2923 }
2924
2925 static int test_ssl_bio_change_rbio(void)
2926 {
2927 return execute_test_ssl_bio(0, CHANGE_RBIO);
2928 }
2929
2930 static int test_ssl_bio_change_wbio(void)
2931 {
2932 return execute_test_ssl_bio(0, CHANGE_WBIO);
2933 }
2934
2935 #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
2936 typedef struct {
2937 /* The list of sig algs */
2938 const int *list;
2939 /* The length of the list */
2940 size_t listlen;
2941 /* A sigalgs list in string format */
2942 const char *liststr;
2943 /* Whether setting the list should succeed */
2944 int valid;
2945 /* Whether creating a connection with the list should succeed */
2946 int connsuccess;
2947 } sigalgs_list;
2948
2949 static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
2950 # ifndef OPENSSL_NO_EC
2951 static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
2952 static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
2953 # endif
2954 static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
2955 static const int invalidlist2[] = {NID_sha256, NID_undef};
2956 static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
2957 static const int invalidlist4[] = {NID_sha256};
2958 static const sigalgs_list testsigalgs[] = {
2959 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
2960 # ifndef OPENSSL_NO_EC
2961 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
2962 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
2963 # endif
2964 {NULL, 0, "RSA+SHA256", 1, 1},
2965 # ifndef OPENSSL_NO_EC
2966 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
2967 {NULL, 0, "ECDSA+SHA512", 1, 0},
2968 # endif
2969 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
2970 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
2971 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
2972 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
2973 {NULL, 0, "RSA", 0, 0},
2974 {NULL, 0, "SHA256", 0, 0},
2975 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
2976 {NULL, 0, "Invalid", 0, 0}
2977 };
2978
2979 static int test_set_sigalgs(int idx)
2980 {
2981 SSL_CTX *cctx = NULL, *sctx = NULL;
2982 SSL *clientssl = NULL, *serverssl = NULL;
2983 int testresult = 0;
2984 const sigalgs_list *curr;
2985 int testctx;
2986
2987 /* Should never happen */
2988 if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
2989 return 0;
2990
2991 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
2992 curr = testctx ? &testsigalgs[idx]
2993 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
2994
2995 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2996 TLS_client_method(), TLS1_VERSION, 0,
2997 &sctx, &cctx, cert, privkey)))
2998 return 0;
2999
3000 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
3001
3002 if (testctx) {
3003 int ret;
3004
3005 if (curr->list != NULL)
3006 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
3007 else
3008 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
3009
3010 if (!ret) {
3011 if (curr->valid)
3012 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
3013 else
3014 testresult = 1;
3015 goto end;
3016 }
3017 if (!curr->valid) {
3018 TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
3019 goto end;
3020 }
3021 }
3022
3023 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3024 &clientssl, NULL, NULL)))
3025 goto end;
3026
3027 if (!testctx) {
3028 int ret;
3029
3030 if (curr->list != NULL)
3031 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
3032 else
3033 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
3034 if (!ret) {
3035 if (curr->valid)
3036 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
3037 else
3038 testresult = 1;
3039 goto end;
3040 }
3041 if (!curr->valid)
3042 goto end;
3043 }
3044
3045 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
3046 SSL_ERROR_NONE),
3047 curr->connsuccess))
3048 goto end;
3049
3050 testresult = 1;
3051
3052 end:
3053 SSL_free(serverssl);
3054 SSL_free(clientssl);
3055 SSL_CTX_free(sctx);
3056 SSL_CTX_free(cctx);
3057
3058 return testresult;
3059 }
3060 #endif
3061
3062 #ifndef OSSL_NO_USABLE_TLS1_3
3063 static int psk_client_cb_cnt = 0;
3064 static int psk_server_cb_cnt = 0;
3065
3066 static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
3067 size_t *idlen, SSL_SESSION **sess)
3068 {
3069 switch (++use_session_cb_cnt) {
3070 case 1:
3071 /* The first call should always have a NULL md */
3072 if (md != NULL)
3073 return 0;
3074 break;
3075
3076 case 2:
3077 /* The second call should always have an md */
3078 if (md == NULL)
3079 return 0;
3080 break;
3081
3082 default:
3083 /* We should only be called a maximum of twice */
3084 return 0;
3085 }
3086
3087 if (clientpsk != NULL)
3088 SSL_SESSION_up_ref(clientpsk);
3089
3090 *sess = clientpsk;
3091 *id = (const unsigned char *)pskid;
3092 *idlen = strlen(pskid);
3093
3094 return 1;
3095 }
3096
3097 #ifndef OPENSSL_NO_PSK
3098 static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
3099 unsigned int max_id_len,
3100 unsigned char *psk,
3101 unsigned int max_psk_len)
3102 {
3103 unsigned int psklen = 0;
3104
3105 psk_client_cb_cnt++;
3106
3107 if (strlen(pskid) + 1 > max_id_len)
3108 return 0;
3109
3110 /* We should only ever be called a maximum of twice per connection */
3111 if (psk_client_cb_cnt > 2)
3112 return 0;
3113
3114 if (clientpsk == NULL)
3115 return 0;
3116
3117 /* We'll reuse the PSK we set up for TLSv1.3 */
3118 if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
3119 return 0;
3120 psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
3121 strncpy(id, pskid, max_id_len);
3122
3123 return psklen;
3124 }
3125 #endif /* OPENSSL_NO_PSK */
3126
3127 static int find_session_cb(SSL *ssl, const unsigned char *identity,
3128 size_t identity_len, SSL_SESSION **sess)
3129 {
3130 find_session_cb_cnt++;
3131
3132 /* We should only ever be called a maximum of twice per connection */
3133 if (find_session_cb_cnt > 2)
3134 return 0;
3135
3136 if (serverpsk == NULL)
3137 return 0;
3138
3139 /* Identity should match that set by the client */
3140 if (strlen(srvid) != identity_len
3141 || strncmp(srvid, (const char *)identity, identity_len) != 0) {
3142 /* No PSK found, continue but without a PSK */
3143 *sess = NULL;
3144 return 1;
3145 }
3146
3147 SSL_SESSION_up_ref(serverpsk);
3148 *sess = serverpsk;
3149
3150 return 1;
3151 }
3152
3153 #ifndef OPENSSL_NO_PSK
3154 static unsigned int psk_server_cb(SSL *ssl, const char *identity,
3155 unsigned char *psk, unsigned int max_psk_len)
3156 {
3157 unsigned int psklen = 0;
3158
3159 psk_server_cb_cnt++;
3160
3161 /* We should only ever be called a maximum of twice per connection */
3162 if (find_session_cb_cnt > 2)
3163 return 0;
3164
3165 if (serverpsk == NULL)
3166 return 0;
3167
3168 /* Identity should match that set by the client */
3169 if (strcmp(srvid, identity) != 0) {
3170 return 0;
3171 }
3172
3173 /* We'll reuse the PSK we set up for TLSv1.3 */
3174 if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
3175 return 0;
3176 psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
3177
3178 return psklen;
3179 }
3180 #endif /* OPENSSL_NO_PSK */
3181
3182 #define MSG1 "Hello"
3183 #define MSG2 "World."
3184 #define MSG3 "This"
3185 #define MSG4 "is"
3186 #define MSG5 "a"
3187 #define MSG6 "test"
3188 #define MSG7 "message."
3189
3190 #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
3191 #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
3192 #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
3193 #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
3194 #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
3195
3196
3197 static SSL_SESSION *create_a_psk(SSL *ssl)
3198 {
3199 const SSL_CIPHER *cipher = NULL;
3200 const unsigned char key[] = {
3201 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
3202 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
3203 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
3204 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
3205 0x2c, 0x2d, 0x2e, 0x2f
3206 };
3207 SSL_SESSION *sess = NULL;
3208
3209 cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
3210 sess = SSL_SESSION_new();
3211 if (!TEST_ptr(sess)
3212 || !TEST_ptr(cipher)
3213 || !TEST_true(SSL_SESSION_set1_master_key(sess, key,
3214 sizeof(key)))
3215 || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
3216 || !TEST_true(
3217 SSL_SESSION_set_protocol_version(sess,
3218 TLS1_3_VERSION))) {
3219 SSL_SESSION_free(sess);
3220 return NULL;
3221 }
3222 return sess;
3223 }
3224
3225 /*
3226 * Helper method to setup objects for early data test. Caller frees objects on
3227 * error.
3228 */
3229 static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
3230 SSL **serverssl, SSL_SESSION **sess, int idx)
3231 {
3232 if (*sctx == NULL
3233 && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3234 TLS_client_method(),
3235 TLS1_VERSION, 0,
3236 sctx, cctx, cert, privkey)))
3237 return 0;
3238
3239 if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
3240 return 0;
3241
3242 if (idx == 1) {
3243 /* When idx == 1 we repeat the tests with read_ahead set */
3244 SSL_CTX_set_read_ahead(*cctx, 1);
3245 SSL_CTX_set_read_ahead(*sctx, 1);
3246 } else if (idx == 2) {
3247 /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
3248 SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
3249 SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
3250 use_session_cb_cnt = 0;
3251 find_session_cb_cnt = 0;
3252 srvid = pskid;
3253 }
3254
3255 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
3256 NULL, NULL)))
3257 return 0;
3258
3259 /*
3260 * For one of the run throughs (doesn't matter which one), we'll try sending
3261 * some SNI data in the initial ClientHello. This will be ignored (because
3262 * there is no SNI cb set up by the server), so it should not impact
3263 * early_data.
3264 */
3265 if (idx == 1
3266 && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
3267 return 0;
3268
3269 if (idx == 2) {
3270 clientpsk = create_a_psk(*clientssl);
3271 if (!TEST_ptr(clientpsk)
3272 /*
3273 * We just choose an arbitrary value for max_early_data which
3274 * should be big enough for testing purposes.
3275 */
3276 || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
3277 0x100))
3278 || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3279 SSL_SESSION_free(clientpsk);
3280 clientpsk = NULL;
3281 return 0;
3282 }
3283 serverpsk = clientpsk;
3284
3285 if (sess != NULL) {
3286 if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3287 SSL_SESSION_free(clientpsk);
3288 SSL_SESSION_free(serverpsk);
3289 clientpsk = serverpsk = NULL;
3290 return 0;
3291 }
3292 *sess = clientpsk;
3293 }
3294 return 1;
3295 }
3296
3297 if (sess == NULL)
3298 return 1;
3299
3300 if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
3301 SSL_ERROR_NONE)))
3302 return 0;
3303
3304 *sess = SSL_get1_session(*clientssl);
3305 SSL_shutdown(*clientssl);
3306 SSL_shutdown(*serverssl);
3307 SSL_free(*serverssl);
3308 SSL_free(*clientssl);
3309 *serverssl = *clientssl = NULL;
3310
3311 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
3312 clientssl, NULL, NULL))
3313 || !TEST_true(SSL_set_session(*clientssl, *sess)))
3314 return 0;
3315
3316 return 1;
3317 }
3318
3319 static int test_early_data_read_write(int idx)
3320 {
3321 SSL_CTX *cctx = NULL, *sctx = NULL;
3322 SSL *clientssl = NULL, *serverssl = NULL;
3323 int testresult = 0;
3324 SSL_SESSION *sess = NULL;
3325 unsigned char buf[20], data[1024];
3326 size_t readbytes, written, eoedlen, rawread, rawwritten;
3327 BIO *rbio;
3328
3329 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3330 &serverssl, &sess, idx)))
3331 goto end;
3332
3333 /* Write and read some early data */
3334 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3335 &written))
3336 || !TEST_size_t_eq(written, strlen(MSG1))
3337 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
3338 sizeof(buf), &readbytes),
3339 SSL_READ_EARLY_DATA_SUCCESS)
3340 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
3341 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3342 SSL_EARLY_DATA_ACCEPTED))
3343 goto end;
3344
3345 /*
3346 * Server should be able to write data, and client should be able to
3347 * read it.
3348 */
3349 if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
3350 &written))
3351 || !TEST_size_t_eq(written, strlen(MSG2))
3352 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3353 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3354 goto end;
3355
3356 /* Even after reading normal data, client should be able write early data */
3357 if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
3358 &written))
3359 || !TEST_size_t_eq(written, strlen(MSG3)))
3360 goto end;
3361
3362 /* Server should still be able read early data after writing data */
3363 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3364 &readbytes),
3365 SSL_READ_EARLY_DATA_SUCCESS)
3366 || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
3367 goto end;
3368
3369 /* Write more data from server and read it from client */
3370 if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
3371 &written))
3372 || !TEST_size_t_eq(written, strlen(MSG4))
3373 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3374 || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
3375 goto end;
3376
3377 /*
3378 * If client writes normal data it should mean writing early data is no
3379 * longer possible.
3380 */
3381 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3382 || !TEST_size_t_eq(written, strlen(MSG5))
3383 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3384 SSL_EARLY_DATA_ACCEPTED))
3385 goto end;
3386
3387 /*
3388 * At this point the client has written EndOfEarlyData, ClientFinished and
3389 * normal (fully protected) data. We are going to cause a delay between the
3390 * arrival of EndOfEarlyData and ClientFinished. We read out all the data
3391 * in the read BIO, and then just put back the EndOfEarlyData message.
3392 */
3393 rbio = SSL_get_rbio(serverssl);
3394 if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
3395 || !TEST_size_t_lt(rawread, sizeof(data))
3396 || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
3397 goto end;
3398
3399 /* Record length is in the 4th and 5th bytes of the record header */
3400 eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
3401 if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
3402 || !TEST_size_t_eq(rawwritten, eoedlen))
3403 goto end;
3404
3405 /* Server should be told that there is no more early data */
3406 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3407 &readbytes),
3408 SSL_READ_EARLY_DATA_FINISH)
3409 || !TEST_size_t_eq(readbytes, 0))
3410 goto end;
3411
3412 /*
3413 * Server has not finished init yet, so should still be able to write early
3414 * data.
3415 */
3416 if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
3417 &written))
3418 || !TEST_size_t_eq(written, strlen(MSG6)))
3419 goto end;
3420
3421 /* Push the ClientFinished and the normal data back into the server rbio */
3422 if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
3423 &rawwritten))
3424 || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
3425 goto end;
3426
3427 /* Server should be able to read normal data */
3428 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3429 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3430 goto end;
3431
3432 /* Client and server should not be able to write/read early data now */
3433 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3434 &written)))
3435 goto end;
3436 ERR_clear_error();
3437 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3438 &readbytes),
3439 SSL_READ_EARLY_DATA_ERROR))
3440 goto end;
3441 ERR_clear_error();
3442
3443 /* Client should be able to read the data sent by the server */
3444 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3445 || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
3446 goto end;
3447
3448 /*
3449 * Make sure we process the two NewSessionTickets. These arrive
3450 * post-handshake. We attempt reads which we do not expect to return any
3451 * data.
3452 */
3453 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3454 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
3455 &readbytes)))
3456 goto end;
3457
3458 /* Server should be able to write normal data */
3459 if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
3460 || !TEST_size_t_eq(written, strlen(MSG7))
3461 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3462 || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
3463 goto end;
3464
3465 SSL_SESSION_free(sess);
3466 sess = SSL_get1_session(clientssl);
3467 use_session_cb_cnt = 0;
3468 find_session_cb_cnt = 0;
3469
3470 SSL_shutdown(clientssl);
3471 SSL_shutdown(serverssl);
3472 SSL_free(serverssl);
3473 SSL_free(clientssl);
3474 serverssl = clientssl = NULL;
3475 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3476 &clientssl, NULL, NULL))
3477 || !TEST_true(SSL_set_session(clientssl, sess)))
3478 goto end;
3479
3480 /* Write and read some early data */
3481 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3482 &written))
3483 || !TEST_size_t_eq(written, strlen(MSG1))
3484 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3485 &readbytes),
3486 SSL_READ_EARLY_DATA_SUCCESS)
3487 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
3488 goto end;
3489
3490 if (!TEST_int_gt(SSL_connect(clientssl), 0)
3491 || !TEST_int_gt(SSL_accept(serverssl), 0))
3492 goto end;
3493
3494 /* Client and server should not be able to write/read early data now */
3495 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3496 &written)))
3497 goto end;
3498 ERR_clear_error();
3499 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3500 &readbytes),
3501 SSL_READ_EARLY_DATA_ERROR))
3502 goto end;
3503 ERR_clear_error();
3504
3505 /* Client and server should be able to write/read normal data */
3506 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3507 || !TEST_size_t_eq(written, strlen(MSG5))
3508 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3509 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3510 goto end;
3511
3512 testresult = 1;
3513
3514 end:
3515 SSL_SESSION_free(sess);
3516 SSL_SESSION_free(clientpsk);
3517 SSL_SESSION_free(serverpsk);
3518 clientpsk = serverpsk = NULL;
3519 SSL_free(serverssl);
3520 SSL_free(clientssl);
3521 SSL_CTX_free(sctx);
3522 SSL_CTX_free(cctx);
3523 return testresult;
3524 }
3525
3526 static int allow_ed_cb_called = 0;
3527
3528 static int allow_early_data_cb(SSL *s, void *arg)
3529 {
3530 int *usecb = (int *)arg;
3531
3532 allow_ed_cb_called++;
3533
3534 if (*usecb == 1)
3535 return 0;
3536
3537 return 1;
3538 }
3539
3540 /*
3541 * idx == 0: Standard early_data setup
3542 * idx == 1: early_data setup using read_ahead
3543 * usecb == 0: Don't use a custom early data callback
3544 * usecb == 1: Use a custom early data callback and reject the early data
3545 * usecb == 2: Use a custom early data callback and accept the early data
3546 * confopt == 0: Configure anti-replay directly
3547 * confopt == 1: Configure anti-replay using SSL_CONF
3548 */
3549 static int test_early_data_replay_int(int idx, int usecb, int confopt)
3550 {
3551 SSL_CTX *cctx = NULL, *sctx = NULL;
3552 SSL *clientssl = NULL, *serverssl = NULL;
3553 int testresult = 0;
3554 SSL_SESSION *sess = NULL;
3555 size_t readbytes, written;
3556 unsigned char buf[20];
3557
3558 allow_ed_cb_called = 0;
3559
3560 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3561 TLS_client_method(), TLS1_VERSION, 0,
3562 &sctx, &cctx, cert, privkey)))
3563 return 0;
3564
3565 if (usecb > 0) {
3566 if (confopt == 0) {
3567 SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
3568 } else {
3569 SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
3570
3571 if (!TEST_ptr(confctx))
3572 goto end;
3573 SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
3574 | SSL_CONF_FLAG_SERVER);
3575 SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
3576 if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
3577 2)) {
3578 SSL_CONF_CTX_free(confctx);
3579 goto end;
3580 }
3581 SSL_CONF_CTX_free(confctx);
3582 }
3583 SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
3584 }
3585
3586 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3587 &serverssl, &sess, idx)))
3588 goto end;
3589
3590 /*
3591 * The server is configured to accept early data. Create a connection to
3592 * "use up" the ticket
3593 */
3594 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3595 || !TEST_true(SSL_session_reused(clientssl)))
3596 goto end;
3597
3598 SSL_shutdown(clientssl);
3599 SSL_shutdown(serverssl);
3600 SSL_free(serverssl);
3601 SSL_free(clientssl);
3602 serverssl = clientssl = NULL;
3603
3604 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3605 &clientssl, NULL, NULL))
3606 || !TEST_true(SSL_set_session(clientssl, sess)))
3607 goto end;
3608
3609 /* Write and read some early data */
3610 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3611 &written))
3612 || !TEST_size_t_eq(written, strlen(MSG1)))
3613 goto end;
3614
3615 if (usecb <= 1) {
3616 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3617 &readbytes),
3618 SSL_READ_EARLY_DATA_FINISH)
3619 /*
3620 * The ticket was reused, so the we should have rejected the
3621 * early data
3622 */
3623 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3624 SSL_EARLY_DATA_REJECTED))
3625 goto end;
3626 } else {
3627 /* In this case the callback decides to accept the early data */
3628 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3629 &readbytes),
3630 SSL_READ_EARLY_DATA_SUCCESS)
3631 || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
3632 /*
3633 * Server will have sent its flight so client can now send
3634 * end of early data and complete its half of the handshake
3635 */
3636 || !TEST_int_gt(SSL_connect(clientssl), 0)
3637 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3638 &readbytes),
3639 SSL_READ_EARLY_DATA_FINISH)
3640 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3641 SSL_EARLY_DATA_ACCEPTED))
3642 goto end;
3643 }
3644
3645 /* Complete the connection */
3646 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3647 || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
3648 || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
3649 goto end;
3650
3651 testresult = 1;
3652
3653 end:
3654 SSL_SESSION_free(sess);
3655 SSL_SESSION_free(clientpsk);
3656 SSL_SESSION_free(serverpsk);
3657 clientpsk = serverpsk = NULL;
3658 SSL_free(serverssl);
3659 SSL_free(clientssl);
3660 SSL_CTX_free(sctx);
3661 SSL_CTX_free(cctx);
3662 return testresult;
3663 }
3664
3665 static int test_early_data_replay(int idx)
3666 {
3667 int ret = 1, usecb, confopt;
3668
3669 for (usecb = 0; usecb < 3; usecb++) {
3670 for (confopt = 0; confopt < 2; confopt++)
3671 ret &= test_early_data_replay_int(idx, usecb, confopt);
3672 }
3673
3674 return ret;
3675 }
3676
3677 /*
3678 * Helper function to test that a server attempting to read early data can
3679 * handle a connection from a client where the early data should be skipped.
3680 * testtype: 0 == No HRR
3681 * testtype: 1 == HRR
3682 * testtype: 2 == HRR, invalid early_data sent after HRR
3683 * testtype: 3 == recv_max_early_data set to 0
3684 */
3685 static int early_data_skip_helper(int testtype, int idx)
3686 {
3687 SSL_CTX *cctx = NULL, *sctx = NULL;
3688 SSL *clientssl = NULL, *serverssl = NULL;
3689 int testresult = 0;
3690 SSL_SESSION *sess = NULL;
3691 unsigned char buf[20];
3692 size_t readbytes, written;
3693
3694 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3695 &serverssl, &sess, idx)))
3696 goto end;
3697
3698 if (testtype == 1 || testtype == 2) {
3699 /* Force an HRR to occur */
3700 #if defined(OPENSSL_NO_EC)
3701 if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
3702 goto end;
3703 #else
3704 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
3705 goto end;
3706 #endif
3707 } else if (idx == 2) {
3708 /*
3709 * We force early_data rejection by ensuring the PSK identity is
3710 * unrecognised
3711 */
3712 srvid = "Dummy Identity";
3713 } else {
3714 /*
3715 * Deliberately corrupt the creation time. We take 20 seconds off the
3716 * time. It could be any value as long as it is not within tolerance.
3717 * This should mean the ticket is rejected.
3718 */
3719 if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
3720 goto end;
3721 }
3722
3723 if (testtype == 3
3724 && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
3725 goto end;
3726
3727 /* Write some early data */
3728 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3729 &written))
3730 || !TEST_size_t_eq(written, strlen(MSG1)))
3731 goto end;
3732
3733 /* Server should reject the early data */
3734 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3735 &readbytes),
3736 SSL_READ_EARLY_DATA_FINISH)
3737 || !TEST_size_t_eq(readbytes, 0)
3738 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3739 SSL_EARLY_DATA_REJECTED))
3740 goto end;
3741
3742 switch (testtype) {
3743 case 0:
3744 /* Nothing to do */
3745 break;
3746
3747 case 1:
3748 /*
3749 * Finish off the handshake. We perform the same writes and reads as
3750 * further down but we expect them to fail due to the incomplete
3751 * handshake.
3752 */
3753 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3754 || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
3755 &readbytes)))
3756 goto end;
3757 break;
3758
3759 case 2:
3760 {
3761 BIO *wbio = SSL_get_wbio(clientssl);
3762 /* A record that will appear as bad early_data */
3763 const unsigned char bad_early_data[] = {
3764 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
3765 };
3766
3767 /*
3768 * We force the client to attempt a write. This will fail because
3769 * we're still in the handshake. It will cause the second
3770 * ClientHello to be sent.
3771 */
3772 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
3773 &written)))
3774 goto end;
3775
3776 /*
3777 * Inject some early_data after the second ClientHello. This should
3778 * cause the server to fail
3779 */
3780 if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
3781 sizeof(bad_early_data), &written)))
3782 goto end;
3783 }
3784 /* fallthrough */
3785
3786 case 3:
3787 /*
3788 * This client has sent more early_data than we are willing to skip
3789 * (case 3) or sent invalid early_data (case 2) so the connection should
3790 * abort.
3791 */
3792 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3793 || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
3794 goto end;
3795
3796 /* Connection has failed - nothing more to do */
3797 testresult = 1;
3798 goto end;
3799
3800 default:
3801 TEST_error("Invalid test type");
3802 goto end;
3803 }
3804
3805 /*
3806 * Should be able to send normal data despite rejection of early data. The
3807 * early_data should be skipped.
3808 */
3809 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3810 || !TEST_size_t_eq(written, strlen(MSG2))
3811 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3812 SSL_EARLY_DATA_REJECTED)
3813 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3814 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3815 goto end;
3816
3817 testresult = 1;
3818
3819 end:
3820 SSL_SESSION_free(clientpsk);
3821 SSL_SESSION_free(serverpsk);
3822 clientpsk = serverpsk = NULL;
3823 SSL_SESSION_free(sess);
3824 SSL_free(serverssl);
3825 SSL_free(clientssl);
3826 SSL_CTX_free(sctx);
3827 SSL_CTX_free(cctx);
3828 return testresult;
3829 }
3830
3831 /*
3832 * Test that a server attempting to read early data can handle a connection
3833 * from a client where the early data is not acceptable.
3834 */
3835 static int test_early_data_skip(int idx)
3836 {
3837 return early_data_skip_helper(0, idx);
3838 }
3839
3840 /*
3841 * Test that a server attempting to read early data can handle a connection
3842 * from a client where an HRR occurs.
3843 */
3844 static int test_early_data_skip_hrr(int idx)
3845 {
3846 return early_data_skip_helper(1, idx);
3847 }
3848
3849 /*
3850 * Test that a server attempting to read early data can handle a connection
3851 * from a client where an HRR occurs and correctly fails if early_data is sent
3852 * after the HRR
3853 */
3854 static int test_early_data_skip_hrr_fail(int idx)
3855 {
3856 return early_data_skip_helper(2, idx);
3857 }
3858
3859 /*
3860 * Test that a server attempting to read early data will abort if it tries to
3861 * skip over too much.
3862 */
3863 static int test_early_data_skip_abort(int idx)
3864 {
3865 return early_data_skip_helper(3, idx);
3866 }
3867
3868 /*
3869 * Test that a server attempting to read early data can handle a connection
3870 * from a client that doesn't send any.
3871 */
3872 static int test_early_data_not_sent(int idx)
3873 {
3874 SSL_CTX *cctx = NULL, *sctx = NULL;
3875 SSL *clientssl = NULL, *serverssl = NULL;
3876 int testresult = 0;
3877 SSL_SESSION *sess = NULL;
3878 unsigned char buf[20];
3879 size_t readbytes, written;
3880
3881 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3882 &serverssl, &sess, idx)))
3883 goto end;
3884
3885 /* Write some data - should block due to handshake with server */
3886 SSL_set_connect_state(clientssl);
3887 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
3888 goto end;
3889
3890 /* Server should detect that early data has not been sent */
3891 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3892 &readbytes),
3893 SSL_READ_EARLY_DATA_FINISH)
3894 || !TEST_size_t_eq(readbytes, 0)
3895 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3896 SSL_EARLY_DATA_NOT_SENT)
3897 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3898 SSL_EARLY_DATA_NOT_SENT))
3899 goto end;
3900
3901 /* Continue writing the message we started earlier */
3902 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
3903 || !TEST_size_t_eq(written, strlen(MSG1))
3904 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3905 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
3906 || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
3907 || !TEST_size_t_eq(written, strlen(MSG2)))
3908 goto end;
3909
3910 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3911 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3912 goto end;
3913
3914 testresult = 1;
3915
3916 end:
3917 SSL_SESSION_free(sess);
3918 SSL_SESSION_free(clientpsk);
3919 SSL_SESSION_free(serverpsk);
3920 clientpsk = serverpsk = NULL;
3921 SSL_free(serverssl);
3922 SSL_free(clientssl);
3923 SSL_CTX_free(sctx);
3924 SSL_CTX_free(cctx);
3925 return testresult;
3926 }
3927
3928 static const char *servalpn;
3929
3930 static int alpn_select_cb(SSL *ssl, const unsigned char **out,
3931 unsigned char *outlen, const unsigned char *in,
3932 unsigned int inlen, void *arg)
3933 {
3934 unsigned int protlen = 0;
3935 const unsigned char *prot;
3936
3937 for (prot = in; prot < in + inlen; prot += protlen) {
3938 protlen = *prot++;
3939 if (in + inlen < prot + protlen)
3940 return SSL_TLSEXT_ERR_NOACK;
3941
3942 if (protlen == strlen(servalpn)
3943 && memcmp(prot, servalpn, protlen) == 0) {
3944 *out = prot;
3945 *outlen = protlen;
3946 return SSL_TLSEXT_ERR_OK;
3947 }
3948 }
3949
3950 return SSL_TLSEXT_ERR_NOACK;
3951 }
3952
3953 /* Test that a PSK can be used to send early_data */
3954 static int test_early_data_psk(int idx)
3955 {
3956 SSL_CTX *cctx = NULL, *sctx = NULL;
3957 SSL *clientssl = NULL, *serverssl = NULL;
3958 int testresult = 0;
3959 SSL_SESSION *sess = NULL;
3960 unsigned char alpnlist[] = {
3961 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
3962 'l', 'p', 'n'
3963 };
3964 #define GOODALPNLEN 9
3965 #define BADALPNLEN 8
3966 #define GOODALPN (alpnlist)
3967 #define BADALPN (alpnlist + GOODALPNLEN)
3968 int err = 0;
3969 unsigned char buf[20];
3970 size_t readbytes, written;
3971 int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
3972 int edstatus = SSL_EARLY_DATA_ACCEPTED;
3973
3974 /* We always set this up with a final parameter of "2" for PSK */
3975 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3976 &serverssl, &sess, 2)))
3977 goto end;
3978
3979 servalpn = "goodalpn";
3980
3981 /*
3982 * Note: There is no test for inconsistent SNI with late client detection.
3983 * This is because servers do not acknowledge SNI even if they are using
3984 * it in a resumption handshake - so it is not actually possible for a
3985 * client to detect a problem.
3986 */
3987 switch (idx) {
3988 case 0:
3989 /* Set inconsistent SNI (early client detection) */
3990 err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
3991 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
3992 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
3993 goto end;
3994 break;
3995
3996 case 1:
3997 /* Set inconsistent ALPN (early client detection) */
3998 err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
3999 /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
4000 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
4001 GOODALPNLEN))
4002 || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
4003 BADALPNLEN)))
4004 goto end;
4005 break;
4006
4007 case 2:
4008 /*
4009 * Set invalid protocol version. Technically this affects PSKs without
4010 * early_data too, but we test it here because it is similar to the
4011 * SNI/ALPN consistency tests.
4012 */
4013 err = SSL_R_BAD_PSK;
4014 if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
4015 goto end;
4016 break;
4017
4018 case 3:
4019 /*
4020 * Set inconsistent SNI (server side). In this case the connection
4021 * will succeed and accept early_data. In TLSv1.3 on the server side SNI
4022 * is associated with each handshake - not the session. Therefore it
4023 * should not matter that we used a different server name last time.
4024 */
4025 SSL_SESSION_free(serverpsk);
4026 serverpsk = SSL_SESSION_dup(clientpsk);
4027 if (!TEST_ptr(serverpsk)
4028 || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
4029 goto end;
4030 /* Fall through */
4031 case 4:
4032 /* Set consistent SNI */
4033 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4034 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
4035 || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
4036 hostname_cb)))
4037 goto end;
4038 break;
4039
4040 case 5:
4041 /*
4042 * Set inconsistent ALPN (server detected). In this case the connection
4043 * will succeed but reject early_data.
4044 */
4045 servalpn = "badalpn";
4046 edstatus = SSL_EARLY_DATA_REJECTED;
4047 readearlyres = SSL_READ_EARLY_DATA_FINISH;
4048 /* Fall through */
4049 case 6:
4050 /*
4051 * Set consistent ALPN.
4052 * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
4053 * accepts a list of protos (each one length prefixed).
4054 * SSL_set1_alpn_selected accepts a single protocol (not length
4055 * prefixed)
4056 */
4057 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
4058 GOODALPNLEN - 1))
4059 || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
4060 GOODALPNLEN)))
4061 goto end;
4062
4063 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4064 break;
4065
4066 case 7:
4067 /* Set inconsistent ALPN (late client detection) */
4068 SSL_SESSION_free(serverpsk);
4069 serverpsk = SSL_SESSION_dup(clientpsk);
4070 if (!TEST_ptr(serverpsk)
4071 || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
4072 BADALPN + 1,
4073 BADALPNLEN - 1))
4074 || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
4075 GOODALPN + 1,
4076 GOODALPNLEN - 1))
4077 || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
4078 sizeof(alpnlist))))
4079 goto end;
4080 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4081 edstatus = SSL_EARLY_DATA_ACCEPTED;
4082 readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
4083 /* SSL_connect() call should fail */
4084 connectres = -1;
4085 break;
4086
4087 default:
4088 TEST_error("Bad test index");
4089 goto end;
4090 }
4091
4092 SSL_set_connect_state(clientssl);
4093 if (err != 0) {
4094 if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4095 &written))
4096 || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
4097 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
4098 goto end;
4099 } else {
4100 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4101 &written)))
4102 goto end;
4103
4104 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4105 &readbytes), readearlyres)
4106 || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
4107 && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
4108 || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
4109 || !TEST_int_eq(SSL_connect(clientssl), connectres))
4110 goto end;
4111 }
4112
4113 testresult = 1;
4114
4115 end:
4116 SSL_SESSION_free(sess);
4117 SSL_SESSION_free(clientpsk);
4118 SSL_SESSION_free(serverpsk);
4119 clientpsk = serverpsk = NULL;
4120 SSL_free(serverssl);
4121 SSL_free(clientssl);
4122 SSL_CTX_free(sctx);
4123 SSL_CTX_free(cctx);
4124 return testresult;
4125 }
4126
4127 /*
4128 * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
4129 * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
4130 * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
4131 * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4132 * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
4133 * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
4134 */
4135 static int test_early_data_psk_with_all_ciphers(int idx)
4136 {
4137 SSL_CTX *cctx = NULL, *sctx = NULL;
4138 SSL *clientssl = NULL, *serverssl = NULL;
4139 int testresult = 0;
4140 SSL_SESSION *sess = NULL;
4141 unsigned char buf[20];
4142 size_t readbytes, written;
4143 const SSL_CIPHER *cipher;
4144 const char *cipher_str[] = {
4145 TLS1_3_RFC_AES_128_GCM_SHA256,
4146 TLS1_3_RFC_AES_256_GCM_SHA384,
4147 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4148 TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4149 # else
4150 NULL,
4151 # endif
4152 TLS1_3_RFC_AES_128_CCM_SHA256,
4153 TLS1_3_RFC_AES_128_CCM_8_SHA256
4154 };
4155 const unsigned char *cipher_bytes[] = {
4156 TLS13_AES_128_GCM_SHA256_BYTES,
4157 TLS13_AES_256_GCM_SHA384_BYTES,
4158 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4159 TLS13_CHACHA20_POLY1305_SHA256_BYTES,
4160 # else
4161 NULL,
4162 # endif
4163 TLS13_AES_128_CCM_SHA256_BYTES,
4164 TLS13_AES_128_CCM_8_SHA256_BYTES
4165 };
4166
4167 if (cipher_str[idx] == NULL)
4168 return 1;
4169 /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
4170 if (idx == 2 && is_fips == 1)
4171 return 1;
4172
4173 /* We always set this up with a final parameter of "2" for PSK */
4174 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4175 &serverssl, &sess, 2)))
4176 goto end;
4177
4178 if (idx == 4) {
4179 /* CCM8 ciphers are considered low security due to their short tag */
4180 SSL_set_security_level(clientssl, 0);
4181 SSL_set_security_level(serverssl, 0);
4182 }
4183
4184 if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
4185 || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
4186 goto end;
4187
4188 /*
4189 * 'setupearly_data_test' creates only one instance of SSL_SESSION
4190 * and assigns to both client and server with incremented reference
4191 * and the same instance is updated in 'sess'.
4192 * So updating ciphersuite in 'sess' which will get reflected in
4193 * PSK handshake using psk use sess and find sess cb.
4194 */
4195 cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
4196 if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
4197 goto end;
4198
4199 SSL_set_connect_state(clientssl);
4200 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4201 &written)))
4202 goto end;
4203
4204 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4205 &readbytes),
4206 SSL_READ_EARLY_DATA_SUCCESS)
4207 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4208 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4209 SSL_EARLY_DATA_ACCEPTED)
4210 || !TEST_int_eq(SSL_connect(clientssl), 1)
4211 || !TEST_int_eq(SSL_accept(serverssl), 1))
4212 goto end;
4213
4214 /* Send some normal data from client to server */
4215 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4216 || !TEST_size_t_eq(written, strlen(MSG2)))
4217 goto end;
4218
4219 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4220 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4221 goto end;
4222
4223 testresult = 1;
4224 end:
4225 SSL_SESSION_free(sess);
4226 SSL_SESSION_free(clientpsk);
4227 SSL_SESSION_free(serverpsk);
4228 clientpsk = serverpsk = NULL;
4229 if (clientssl != NULL)
4230 SSL_shutdown(clientssl);
4231 if (serverssl != NULL)
4232 SSL_shutdown(serverssl);
4233 SSL_free(serverssl);
4234 SSL_free(clientssl);
4235 SSL_CTX_free(sctx);
4236 SSL_CTX_free(cctx);
4237 return testresult;
4238 }
4239
4240 /*
4241 * Test that a server that doesn't try to read early data can handle a
4242 * client sending some.
4243 */
4244 static int test_early_data_not_expected(int idx)
4245 {
4246 SSL_CTX *cctx = NULL, *sctx = NULL;
4247 SSL *clientssl = NULL, *serverssl = NULL;
4248 int testresult = 0;
4249 SSL_SESSION *sess = NULL;
4250 unsigned char buf[20];
4251 size_t readbytes, written;
4252
4253 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4254 &serverssl, &sess, idx)))
4255 goto end;
4256
4257 /* Write some early data */
4258 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4259 &written)))
4260 goto end;
4261
4262 /*
4263 * Server should skip over early data and then block waiting for client to
4264 * continue handshake
4265 */
4266 if (!TEST_int_le(SSL_accept(serverssl), 0)
4267 || !TEST_int_gt(SSL_connect(clientssl), 0)
4268 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4269 SSL_EARLY_DATA_REJECTED)
4270 || !TEST_int_gt(SSL_accept(serverssl), 0)
4271 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4272 SSL_EARLY_DATA_REJECTED))
4273 goto end;
4274
4275 /* Send some normal data from client to server */
4276 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4277 || !TEST_size_t_eq(written, strlen(MSG2)))
4278 goto end;
4279
4280 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4281 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4282 goto end;
4283
4284 testresult = 1;
4285
4286 end:
4287 SSL_SESSION_free(sess);
4288 SSL_SESSION_free(clientpsk);
4289 SSL_SESSION_free(serverpsk);
4290 clientpsk = serverpsk = NULL;
4291 SSL_free(serverssl);
4292 SSL_free(clientssl);
4293 SSL_CTX_free(sctx);
4294 SSL_CTX_free(cctx);
4295 return testresult;
4296 }
4297
4298
4299 # ifndef OPENSSL_NO_TLS1_2
4300 /*
4301 * Test that a server attempting to read early data can handle a connection
4302 * from a TLSv1.2 client.
4303 */
4304 static int test_early_data_tls1_2(int idx)
4305 {
4306 SSL_CTX *cctx = NULL, *sctx = NULL;
4307 SSL *clientssl = NULL, *serverssl = NULL;
4308 int testresult = 0;
4309 unsigned char buf[20];
4310 size_t readbytes, written;
4311
4312 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4313 &serverssl, NULL, idx)))
4314 goto end;
4315
4316 /* Write some data - should block due to handshake with server */
4317 SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
4318 SSL_set_connect_state(clientssl);
4319 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4320 goto end;
4321
4322 /*
4323 * Server should do TLSv1.2 handshake. First it will block waiting for more
4324 * messages from client after ServerDone. Then SSL_read_early_data should
4325 * finish and detect that early data has not been sent
4326 */
4327 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4328 &readbytes),
4329 SSL_READ_EARLY_DATA_ERROR))
4330 goto end;
4331
4332 /*
4333 * Continue writing the message we started earlier. Will still block waiting
4334 * for the CCS/Finished from server
4335 */
4336 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4337 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4338 &readbytes),
4339 SSL_READ_EARLY_DATA_FINISH)
4340 || !TEST_size_t_eq(readbytes, 0)
4341 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4342 SSL_EARLY_DATA_NOT_SENT))
4343 goto end;
4344
4345 /* Continue writing the message we started earlier */
4346 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4347 || !TEST_size_t_eq(written, strlen(MSG1))
4348 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4349 SSL_EARLY_DATA_NOT_SENT)
4350 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4351 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4352 || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
4353 || !TEST_size_t_eq(written, strlen(MSG2))
4354 || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
4355 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4356 goto end;
4357
4358 testresult = 1;
4359
4360 end:
4361 SSL_SESSION_free(clientpsk);
4362 SSL_SESSION_free(serverpsk);
4363 clientpsk = serverpsk = NULL;
4364 SSL_free(serverssl);
4365 SSL_free(clientssl);
4366 SSL_CTX_free(sctx);
4367 SSL_CTX_free(cctx);
4368
4369 return testresult;
4370 }
4371 # endif /* OPENSSL_NO_TLS1_2 */
4372
4373 /*
4374 * Test configuring the TLSv1.3 ciphersuites
4375 *
4376 * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
4377 * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
4378 * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
4379 * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
4380 * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4381 * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4382 * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
4383 * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
4384 * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
4385 * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
4386 */
4387 static int test_set_ciphersuite(int idx)
4388 {
4389 SSL_CTX *cctx = NULL, *sctx = NULL;
4390 SSL *clientssl = NULL, *serverssl = NULL;
4391 int testresult = 0;
4392
4393 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4394 TLS_client_method(), TLS1_VERSION, 0,
4395 &sctx, &cctx, cert, privkey))
4396 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4397 "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
4398 goto end;
4399
4400 if (idx >=4 && idx <= 7) {
4401 /* SSL_CTX explicit cipher list */
4402 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
4403 goto end;
4404 }
4405
4406 if (idx == 0 || idx == 4) {
4407 /* Default ciphersuite */
4408 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4409 "TLS_AES_128_GCM_SHA256")))
4410 goto end;
4411 } else if (idx == 1 || idx == 5) {
4412 /* Non default ciphersuite */
4413 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4414 "TLS_AES_128_CCM_SHA256")))
4415 goto end;
4416 }
4417
4418 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4419 &clientssl, NULL, NULL)))
4420 goto end;
4421
4422 if (idx == 8 || idx == 9) {
4423 /* SSL explicit cipher list */
4424 if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
4425 goto end;
4426 }
4427
4428 if (idx == 2 || idx == 6 || idx == 8) {
4429 /* Default ciphersuite */
4430 if (!TEST_true(SSL_set_ciphersuites(clientssl,
4431 "TLS_AES_128_GCM_SHA256")))
4432 goto end;
4433 } else if (idx == 3 || idx == 7 || idx == 9) {
4434 /* Non default ciphersuite */
4435 if (!TEST_true(SSL_set_ciphersuites(clientssl,
4436 "TLS_AES_128_CCM_SHA256")))
4437 goto end;
4438 }
4439
4440 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4441 goto end;
4442
4443 testresult = 1;
4444
4445 end:
4446 SSL_free(serverssl);
4447 SSL_free(clientssl);
4448 SSL_CTX_free(sctx);
4449 SSL_CTX_free(cctx);
4450
4451 return testresult;
4452 }
4453
4454 static int test_ciphersuite_change(void)
4455 {
4456 SSL_CTX *cctx = NULL, *sctx = NULL;
4457 SSL *clientssl = NULL, *serverssl = NULL;
4458 SSL_SESSION *clntsess = NULL;
4459 int testresult = 0;
4460 const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
4461
4462 /* Create a session based on SHA-256 */
4463 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4464 TLS_client_method(), TLS1_VERSION, 0,
4465 &sctx, &cctx, cert, privkey))
4466 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4467 "TLS_AES_128_GCM_SHA256:"
4468 "TLS_AES_256_GCM_SHA384:"
4469 "TLS_AES_128_CCM_SHA256"))
4470 || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4471 "TLS_AES_128_GCM_SHA256")))
4472 goto end;
4473
4474 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4475 NULL, NULL))
4476 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4477 SSL_ERROR_NONE)))
4478 goto end;
4479
4480 clntsess = SSL_get1_session(clientssl);
4481 /* Save for later */
4482 aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
4483 SSL_shutdown(clientssl);
4484 SSL_shutdown(serverssl);
4485 SSL_free(serverssl);
4486 SSL_free(clientssl);
4487 serverssl = clientssl = NULL;
4488
4489 /* Check we can resume a session with a different SHA-256 ciphersuite */
4490 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4491 "TLS_AES_128_CCM_SHA256"))
4492 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4493 &clientssl, NULL, NULL))
4494 || !TEST_true(SSL_set_session(clientssl, clntsess))
4495 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4496 SSL_ERROR_NONE))
4497 || !TEST_true(SSL_session_reused(clientssl)))
4498 goto end;
4499
4500 SSL_SESSION_free(clntsess);
4501 clntsess = SSL_get1_session(clientssl);
4502 SSL_shutdown(clientssl);
4503 SSL_shutdown(serverssl);
4504 SSL_free(serverssl);
4505 SSL_free(clientssl);
4506 serverssl = clientssl = NULL;
4507
4508 /*
4509 * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
4510 * succeeds but does not resume.
4511 */
4512 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4513 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4514 NULL, NULL))
4515 || !TEST_true(SSL_set_session(clientssl, clntsess))
4516 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4517 SSL_ERROR_SSL))
4518 || !TEST_false(SSL_session_reused(clientssl)))
4519 goto end;
4520
4521 SSL_SESSION_free(clntsess);
4522 clntsess = NULL;
4523 SSL_shutdown(clientssl);
4524 SSL_shutdown(serverssl);
4525 SSL_free(serverssl);
4526 SSL_free(clientssl);
4527 serverssl = clientssl = NULL;
4528
4529 /* Create a session based on SHA384 */
4530 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4531 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4532 &clientssl, NULL, NULL))
4533 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4534 SSL_ERROR_NONE)))
4535 goto end;
4536
4537 clntsess = SSL_get1_session(clientssl);
4538 SSL_shutdown(clientssl);
4539 SSL_shutdown(serverssl);
4540 SSL_free(serverssl);
4541 SSL_free(clientssl);
4542 serverssl = clientssl = NULL;
4543
4544 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4545 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
4546 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4547 "TLS_AES_256_GCM_SHA384"))
4548 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4549 NULL, NULL))
4550 || !TEST_true(SSL_set_session(clientssl, clntsess))
4551 /*
4552 * We use SSL_ERROR_WANT_READ below so that we can pause the
4553 * connection after the initial ClientHello has been sent to
4554 * enable us to make some session changes.
4555 */
4556 || !TEST_false(create_ssl_connection(serverssl, clientssl,
4557 SSL_ERROR_WANT_READ)))
4558 goto end;
4559
4560 /* Trick the client into thinking this session is for a different digest */
4561 clntsess->cipher = aes_128_gcm_sha256;
4562 clntsess->cipher_id = clntsess->cipher->id;
4563
4564 /*
4565 * Continue the previously started connection. Server has selected a SHA-384
4566 * ciphersuite, but client thinks the session is for SHA-256, so it should
4567 * bail out.
4568 */
4569 if (!TEST_false(create_ssl_connection(serverssl, clientssl,
4570 SSL_ERROR_SSL))
4571 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
4572 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
4573 goto end;
4574
4575 testresult = 1;
4576
4577 end:
4578 SSL_SESSION_free(clntsess);
4579 SSL_free(serverssl);
4580 SSL_free(clientssl);
4581 SSL_CTX_free(sctx);
4582 SSL_CTX_free(cctx);
4583
4584 return testresult;
4585 }
4586
4587 /*
4588 * Test TLSv1.3 Key exchange
4589 * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
4590 * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
4591 * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
4592 * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
4593 * Test 4 = Test NID_X25519 with TLSv1.3 client and server
4594 * Test 5 = Test NID_X448 with TLSv1.3 client and server
4595 * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
4596 * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
4597 * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
4598 * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
4599 * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
4600 * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
4601 * Test 12 = Test all ECDHE with TLSv1.2 client and server
4602 * Test 13 = Test all FFDHE with TLSv1.2 client and server
4603 */
4604 # ifndef OPENSSL_NO_EC
4605 static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
4606 NID_secp521r1, NID_X25519, NID_X448};
4607 # endif
4608 # ifndef OPENSSL_NO_DH
4609 static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
4610 NID_ffdhe6144, NID_ffdhe8192};
4611 # endif
4612 static int test_key_exchange(int idx)
4613 {
4614 SSL_CTX *sctx = NULL, *cctx = NULL;
4615 SSL *serverssl = NULL, *clientssl = NULL;
4616 int testresult = 0;
4617 int kexch_alg;
4618 int *kexch_groups = &kexch_alg;
4619 int kexch_groups_size = 1;
4620 int max_version = TLS1_3_VERSION;
4621 char *kexch_name0 = NULL;
4622
4623 switch (idx) {
4624 # ifndef OPENSSL_NO_EC
4625 # ifndef OPENSSL_NO_TLS1_2
4626 case 12:
4627 max_version = TLS1_2_VERSION;
4628 # endif
4629 /* Fall through */
4630 case 0:
4631 kexch_groups = ecdhe_kexch_groups;
4632 kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
4633 kexch_name0 = "secp256r1";
4634 break;
4635 case 1:
4636 kexch_alg = NID_X9_62_prime256v1;
4637 kexch_name0 = "secp256r1";
4638 break;
4639 case 2:
4640 kexch_alg = NID_secp384r1;
4641 kexch_name0 = "secp384r1";
4642 break;
4643 case 3:
4644 kexch_alg = NID_secp521r1;
4645 kexch_name0 = "secp521r1";
4646 break;
4647 case 4:
4648 kexch_alg = NID_X25519;
4649 kexch_name0 = "x25519";
4650 break;
4651 case 5:
4652 kexch_alg = NID_X448;
4653 kexch_name0 = "x448";
4654 break;
4655 # endif
4656 # ifndef OPENSSL_NO_DH
4657 # ifndef OPENSSL_NO_TLS1_2
4658 case 13:
4659 max_version = TLS1_2_VERSION;
4660 kexch_name0 = "ffdhe2048";
4661 # endif
4662 /* Fall through */
4663 case 6:
4664 kexch_groups = ffdhe_kexch_groups;
4665 kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
4666 kexch_name0 = "ffdhe2048";
4667 break;
4668 case 7:
4669 kexch_alg = NID_ffdhe2048;
4670 kexch_name0 = "ffdhe2048";
4671 break;
4672 case 8:
4673 kexch_alg = NID_ffdhe3072;
4674 kexch_name0 = "ffdhe3072";
4675 break;
4676 case 9:
4677 kexch_alg = NID_ffdhe4096;
4678 kexch_name0 = "ffdhe4096";
4679 break;
4680 case 10:
4681 kexch_alg = NID_ffdhe6144;
4682 kexch_name0 = "ffdhe6144";
4683 break;
4684 case 11:
4685 kexch_alg = NID_ffdhe8192;
4686 kexch_name0 = "ffdhe8192";
4687 break;
4688 # endif
4689 default:
4690 /* We're skipping this test */
4691 return 1;
4692 }
4693
4694 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4695 TLS_client_method(), TLS1_VERSION,
4696 max_version, &sctx, &cctx, cert,
4697 privkey)))
4698 goto end;
4699
4700 if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
4701 TLS1_3_RFC_AES_128_GCM_SHA256)))
4702 goto end;
4703
4704 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4705 TLS1_3_RFC_AES_128_GCM_SHA256)))
4706 goto end;
4707
4708 if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
4709 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4710 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
4711 || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
4712 goto end;
4713
4714 /*
4715 * Must include an EC ciphersuite so that we send supported groups in
4716 * TLSv1.2
4717 */
4718 # ifndef OPENSSL_NO_TLS1_2
4719 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4720 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4721 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
4722 goto end;
4723 # endif
4724
4725 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4726 NULL, NULL)))
4727 goto end;
4728
4729 if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
4730 || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
4731 goto end;
4732
4733 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4734 goto end;
4735
4736 /*
4737 * If Handshake succeeds the negotiated kexch alg should be the first one in
4738 * configured, except in the case of FFDHE groups (idx 13), which are
4739 * TLSv1.3 only so we expect no shared group to exist.
4740 */
4741 if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
4742 idx == 13 ? 0 : kexch_groups[0]))
4743 goto end;
4744
4745 if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
4746 kexch_name0))
4747 goto end;
4748
4749 /* We don't implement RFC 7919 named groups for TLS 1.2. */
4750 if (idx != 13) {
4751 if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
4752 goto end;
4753 if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
4754 goto end;
4755 }
4756
4757 testresult = 1;
4758 end:
4759 SSL_free(serverssl);
4760 SSL_free(clientssl);
4761 SSL_CTX_free(sctx);
4762 SSL_CTX_free(cctx);
4763 return testresult;
4764 }
4765
4766 # if !defined(OPENSSL_NO_TLS1_2) \
4767 && !defined(OPENSSL_NO_EC) \
4768 && !defined(OPENSSL_NO_DH)
4769 static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
4770 int isecdhe, int idx)
4771 {
4772 int kexch_alg;
4773 int *kexch_groups = &kexch_alg;
4774 int numec, numff;
4775
4776 numec = OSSL_NELEM(ecdhe_kexch_groups);
4777 numff = OSSL_NELEM(ffdhe_kexch_groups);
4778 if (isecdhe)
4779 kexch_alg = ecdhe_kexch_groups[idx];
4780 else
4781 kexch_alg = ffdhe_kexch_groups[idx];
4782
4783 if (clientmulti) {
4784 if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
4785 return 0;
4786 if (isecdhe) {
4787 if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
4788 numec)))
4789 return 0;
4790 } else {
4791 if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
4792 numff)))
4793 return 0;
4794 }
4795 } else {
4796 if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
4797 return 0;
4798 if (isecdhe) {
4799 if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
4800 numec)))
4801 return 0;
4802 } else {
4803 if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
4804 numff)))
4805 return 0;
4806 }
4807 }
4808 return 1;
4809 }
4810
4811 /*-
4812 * Test the SSL_get_negotiated_group() API across a battery of scenarios.
4813 * Run through both the ECDHE and FFDHE group lists used in the previous
4814 * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
4815 * confirming the expected result; then perform a resumption handshake
4816 * while offering the same group list, and another resumption handshake
4817 * offering a different group list. The returned value should be the
4818 * negotiated group for the initial handshake; for TLS 1.3 resumption
4819 * handshakes the returned value will be negotiated on the resumption
4820 * handshake itself, but for TLS 1.2 resumption handshakes the value will
4821 * be cached in the session from the original handshake, regardless of what
4822 * was offered in the resumption ClientHello.
4823 *
4824 * Using E for the number of EC groups and F for the number of FF groups:
4825 * E tests of ECDHE with TLS 1.3, server only has one group
4826 * F tests of FFDHE with TLS 1.3, server only has one group
4827 * E tests of ECDHE with TLS 1.2, server only has one group
4828 * F tests of FFDHE with TLS 1.2, server only has one group
4829 * E tests of ECDHE with TLS 1.3, client sends only one group
4830 * F tests of FFDHE with TLS 1.3, client sends only one group
4831 * E tests of ECDHE with TLS 1.2, client sends only one group
4832 * F tests of FFDHE with TLS 1.2, client sends only one group
4833 */
4834 static int test_negotiated_group(int idx)
4835 {
4836 int clientmulti, istls13, isecdhe, numec, numff, numgroups;
4837 int expectednid;
4838 SSL_CTX *sctx = NULL, *cctx = NULL;
4839 SSL *serverssl = NULL, *clientssl = NULL;
4840 SSL_SESSION *origsess = NULL;
4841 int testresult = 0;
4842 int kexch_alg;
4843 int max_version = TLS1_3_VERSION;
4844
4845 numec = OSSL_NELEM(ecdhe_kexch_groups);
4846 numff = OSSL_NELEM(ffdhe_kexch_groups);
4847 numgroups = numec + numff;
4848 clientmulti = (idx < 2 * numgroups);
4849 idx = idx % (2 * numgroups);
4850 istls13 = (idx < numgroups);
4851 idx = idx % numgroups;
4852 isecdhe = (idx < numec);
4853 if (!isecdhe)
4854 idx -= numec;
4855 /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
4856 if (isecdhe)
4857 kexch_alg = ecdhe_kexch_groups[idx];
4858 else
4859 kexch_alg = ffdhe_kexch_groups[idx];
4860 /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
4861 if (!istls13 && !isecdhe)
4862 expectednid = NID_undef;
4863 else
4864 expectednid = kexch_alg;
4865
4866 if (!istls13)
4867 max_version = TLS1_2_VERSION;
4868
4869 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4870 TLS_client_method(), TLS1_VERSION,
4871 max_version, &sctx, &cctx, cert,
4872 privkey)))
4873 goto end;
4874
4875 /*
4876 * Force (EC)DHE ciphers for TLS 1.2.
4877 * Be sure to enable auto tmp DH so that FFDHE can succeed.
4878 */
4879 if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
4880 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4881 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
4882 || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
4883 goto end;
4884 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4885 TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4886 TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
4887 goto end;
4888
4889 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4890 NULL, NULL)))
4891 goto end;
4892
4893 if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
4894 idx)))
4895 goto end;
4896
4897 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4898 goto end;
4899
4900 /* Initial handshake; always the configured one */
4901 if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
4902 || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
4903 goto end;
4904
4905 if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
4906 goto end;
4907
4908 SSL_shutdown(clientssl);
4909 SSL_shutdown(serverssl);
4910 SSL_free(serverssl);
4911 SSL_free(clientssl);
4912 serverssl = clientssl = NULL;
4913
4914 /* First resumption attempt; use the same config as initial handshake */
4915 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4916 NULL, NULL))
4917 || !TEST_true(SSL_set_session(clientssl, origsess))
4918 || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
4919 isecdhe, idx)))
4920 goto end;
4921
4922 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
4923 || !TEST_true(SSL_session_reused(clientssl)))
4924 goto end;
4925
4926 /* Still had better agree, since nothing changed... */
4927 if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
4928 || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
4929 goto end;
4930
4931 SSL_shutdown(clientssl);
4932 SSL_shutdown(serverssl);
4933 SSL_free(serverssl);
4934 SSL_free(clientssl);
4935 serverssl = clientssl = NULL;
4936
4937 /*-
4938 * Second resumption attempt
4939 * The party that picks one group changes it, which we effectuate by
4940 * changing 'idx' and updating what we expect.
4941 */
4942 if (idx == 0)
4943 idx = 1;
4944 else
4945 idx--;
4946 if (istls13) {
4947 if (isecdhe)
4948 expectednid = ecdhe_kexch_groups[idx];
4949 else
4950 expectednid = ffdhe_kexch_groups[idx];
4951 /* Verify that we are changing what we expect. */
4952 if (!TEST_int_ne(expectednid, kexch_alg))
4953 goto end;
4954 } else {
4955 /* TLS 1.2 only supports named groups for ECDHE. */
4956 if (isecdhe)
4957 expectednid = kexch_alg;
4958 else
4959 expectednid = 0;
4960 }
4961 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4962 NULL, NULL))
4963 || !TEST_true(SSL_set_session(clientssl, origsess))
4964 || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
4965 isecdhe, idx)))
4966 goto end;
4967
4968 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
4969 || !TEST_true(SSL_session_reused(clientssl)))
4970 goto end;
4971
4972 /* Check that we get what we expected */
4973 if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
4974 || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
4975 goto end;
4976
4977 testresult = 1;
4978 end:
4979 SSL_free(serverssl);
4980 SSL_free(clientssl);
4981 SSL_CTX_free(sctx);
4982 SSL_CTX_free(cctx);
4983 SSL_SESSION_free(origsess);
4984 return testresult;
4985 }
4986 # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
4987
4988 /*
4989 * Test TLSv1.3 Cipher Suite
4990 * Test 0 = Set TLS1.3 cipher on context
4991 * Test 1 = Set TLS1.3 cipher on SSL
4992 * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
4993 * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
4994 */
4995 static int test_tls13_ciphersuite(int idx)
4996 {
4997 SSL_CTX *sctx = NULL, *cctx = NULL;
4998 SSL *serverssl = NULL, *clientssl = NULL;
4999 static const struct {
5000 const char *ciphername;
5001 int fipscapable;
5002 int low_security;
5003 } t13_ciphers[] = {
5004 { TLS1_3_RFC_AES_128_GCM_SHA256, 1, 0 },
5005 { TLS1_3_RFC_AES_256_GCM_SHA384, 1, 0 },
5006 { TLS1_3_RFC_AES_128_CCM_SHA256, 1, 0 },
5007 # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
5008 { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
5009 { TLS1_3_RFC_AES_256_GCM_SHA384
5010 ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
5011 # endif
5012 /* CCM8 ciphers are considered low security due to their short tag */
5013 { TLS1_3_RFC_AES_128_CCM_8_SHA256
5014 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1, 1 }
5015 };
5016 const char *t13_cipher = NULL;
5017 const char *t12_cipher = NULL;
5018 const char *negotiated_scipher;
5019 const char *negotiated_ccipher;
5020 int set_at_ctx = 0;
5021 int set_at_ssl = 0;
5022 int testresult = 0;
5023 int max_ver;
5024 size_t i;
5025
5026 switch (idx) {
5027 case 0:
5028 set_at_ctx = 1;
5029 break;
5030 case 1:
5031 set_at_ssl = 1;
5032 break;
5033 case 2:
5034 set_at_ctx = 1;
5035 t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5036 break;
5037 case 3:
5038 set_at_ssl = 1;
5039 t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5040 break;
5041 }
5042
5043 for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
5044 # ifdef OPENSSL_NO_TLS1_2
5045 if (max_ver == TLS1_2_VERSION)
5046 continue;
5047 # endif
5048 for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
5049 if (is_fips && !t13_ciphers[i].fipscapable)
5050 continue;
5051 t13_cipher = t13_ciphers[i].ciphername;
5052 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5053 TLS_client_method(),
5054 TLS1_VERSION, max_ver,
5055 &sctx, &cctx, cert, privkey)))
5056 goto end;
5057
5058 if (t13_ciphers[i].low_security) {
5059 SSL_CTX_set_security_level(sctx, 0);
5060 SSL_CTX_set_security_level(cctx, 0);
5061 }
5062
5063 if (set_at_ctx) {
5064 if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
5065 || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
5066 goto end;
5067 if (t12_cipher != NULL) {
5068 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
5069 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
5070 t12_cipher)))
5071 goto end;
5072 }
5073 }
5074
5075 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5076 &clientssl, NULL, NULL)))
5077 goto end;
5078
5079 if (set_at_ssl) {
5080 if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
5081 || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
5082 goto end;
5083 if (t12_cipher != NULL) {
5084 if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
5085 || !TEST_true(SSL_set_cipher_list(clientssl,
5086 t12_cipher)))
5087 goto end;
5088 }
5089 }
5090
5091 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5092 SSL_ERROR_NONE)))
5093 goto end;
5094
5095 negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5096 serverssl));
5097 negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5098 clientssl));
5099 if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
5100 goto end;
5101
5102 /*
5103 * TEST_strn_eq is used below because t13_cipher can contain
5104 * multiple ciphersuites
5105 */
5106 if (max_ver == TLS1_3_VERSION
5107 && !TEST_strn_eq(t13_cipher, negotiated_scipher,
5108 strlen(negotiated_scipher)))
5109 goto end;
5110
5111 # ifndef OPENSSL_NO_TLS1_2
5112 /* Below validation is not done when t12_cipher is NULL */
5113 if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
5114 && !TEST_str_eq(t12_cipher, negotiated_scipher))
5115 goto end;
5116 # endif
5117
5118 SSL_free(serverssl);
5119 serverssl = NULL;
5120 SSL_free(clientssl);
5121 clientssl = NULL;
5122 SSL_CTX_free(sctx);
5123 sctx = NULL;
5124 SSL_CTX_free(cctx);
5125 cctx = NULL;
5126 }
5127 }
5128
5129 testresult = 1;
5130 end:
5131 SSL_free(serverssl);
5132 SSL_free(clientssl);
5133 SSL_CTX_free(sctx);
5134 SSL_CTX_free(cctx);
5135 return testresult;
5136 }
5137
5138 /*
5139 * Test TLSv1.3 PSKs
5140 * Test 0 = Test new style callbacks
5141 * Test 1 = Test both new and old style callbacks
5142 * Test 2 = Test old style callbacks
5143 * Test 3 = Test old style callbacks with no certificate
5144 */
5145 static int test_tls13_psk(int idx)
5146 {
5147 SSL_CTX *sctx = NULL, *cctx = NULL;
5148 SSL *serverssl = NULL, *clientssl = NULL;
5149 const SSL_CIPHER *cipher = NULL;
5150 const unsigned char key[] = {
5151 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
5152 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
5153 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
5154 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
5155 };
5156 int testresult = 0;
5157
5158 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5159 TLS_client_method(), TLS1_VERSION, 0,
5160 &sctx, &cctx, idx == 3 ? NULL : cert,
5161 idx == 3 ? NULL : privkey)))
5162 goto end;
5163
5164 if (idx != 3) {
5165 /*
5166 * We use a ciphersuite with SHA256 to ease testing old style PSK
5167 * callbacks which will always default to SHA256. This should not be
5168 * necessary if we have no cert/priv key. In that case the server should
5169 * prefer SHA256 automatically.
5170 */
5171 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5172 "TLS_AES_128_GCM_SHA256")))
5173 goto end;
5174 } else {
5175 /*
5176 * As noted above the server should prefer SHA256 automatically. However
5177 * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
5178 * code works even if we are testing with only the FIPS provider loaded.
5179 */
5180 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5181 "TLS_AES_256_GCM_SHA384:"
5182 "TLS_AES_128_GCM_SHA256")))
5183 goto end;
5184 }
5185
5186 /*
5187 * Test 0: New style callbacks only
5188 * Test 1: New and old style callbacks (only the new ones should be used)
5189 * Test 2: Old style callbacks only
5190 */
5191 if (idx == 0 || idx == 1) {
5192 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
5193 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
5194 }
5195 #ifndef OPENSSL_NO_PSK
5196 if (idx >= 1) {
5197 SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
5198 SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
5199 }
5200 #endif
5201 srvid = pskid;
5202 use_session_cb_cnt = 0;
5203 find_session_cb_cnt = 0;
5204 psk_client_cb_cnt = 0;
5205 psk_server_cb_cnt = 0;
5206
5207 if (idx != 3) {
5208 /*
5209 * Check we can create a connection if callback decides not to send a
5210 * PSK
5211 */
5212 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5213 NULL, NULL))
5214 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5215 SSL_ERROR_NONE))
5216 || !TEST_false(SSL_session_reused(clientssl))
5217 || !TEST_false(SSL_session_reused(serverssl)))
5218 goto end;
5219
5220 if (idx == 0 || idx == 1) {
5221 if (!TEST_true(use_session_cb_cnt == 1)
5222 || !TEST_true(find_session_cb_cnt == 0)
5223 /*
5224 * If no old style callback then below should be 0
5225 * otherwise 1
5226 */
5227 || !TEST_true(psk_client_cb_cnt == idx)
5228 || !TEST_true(psk_server_cb_cnt == 0))
5229 goto end;
5230 } else {
5231 if (!TEST_true(use_session_cb_cnt == 0)
5232 || !TEST_true(find_session_cb_cnt == 0)
5233 || !TEST_true(psk_client_cb_cnt == 1)
5234 || !TEST_true(psk_server_cb_cnt == 0))
5235 goto end;
5236 }
5237
5238 shutdown_ssl_connection(serverssl, clientssl);
5239 serverssl = clientssl = NULL;
5240 use_session_cb_cnt = psk_client_cb_cnt = 0;
5241 }
5242
5243 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5244 NULL, NULL)))
5245 goto end;
5246
5247 /* Create the PSK */
5248 cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
5249 clientpsk = SSL_SESSION_new();
5250 if (!TEST_ptr(clientpsk)
5251 || !TEST_ptr(cipher)
5252 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
5253 sizeof(key)))
5254 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
5255 || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
5256 TLS1_3_VERSION))
5257 || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
5258 goto end;
5259 serverpsk = clientpsk;
5260
5261 /* Check we can create a connection and the PSK is used */
5262 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5263 || !TEST_true(SSL_session_reused(clientssl))
5264 || !TEST_true(SSL_session_reused(serverssl)))
5265 goto end;
5266
5267 if (idx == 0 || idx == 1) {
5268 if (!TEST_true(use_session_cb_cnt == 1)
5269 || !TEST_true(find_session_cb_cnt == 1)
5270 || !TEST_true(psk_client_cb_cnt == 0)
5271 || !TEST_true(psk_server_cb_cnt == 0))
5272 goto end;
5273 } else {
5274 if (!TEST_true(use_session_cb_cnt == 0)
5275 || !TEST_true(find_session_cb_cnt == 0)
5276 || !TEST_true(psk_client_cb_cnt == 1)
5277 || !TEST_true(psk_server_cb_cnt == 1))
5278 goto end;
5279 }
5280
5281 shutdown_ssl_connection(serverssl, clientssl);
5282 serverssl = clientssl = NULL;
5283 use_session_cb_cnt = find_session_cb_cnt = 0;
5284 psk_client_cb_cnt = psk_server_cb_cnt = 0;
5285
5286 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5287 NULL, NULL)))
5288 goto end;
5289
5290 /* Force an HRR */
5291 #if defined(OPENSSL_NO_EC)
5292 if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
5293 goto end;
5294 #else
5295 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
5296 goto end;
5297 #endif
5298
5299 /*
5300 * Check we can create a connection, the PSK is used and the callbacks are
5301 * called twice.
5302 */
5303 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5304 || !TEST_true(SSL_session_reused(clientssl))
5305 || !TEST_true(SSL_session_reused(serverssl)))
5306 goto end;
5307
5308 if (idx == 0 || idx == 1) {
5309 if (!TEST_true(use_session_cb_cnt == 2)
5310 || !TEST_true(find_session_cb_cnt == 2)
5311 || !TEST_true(psk_client_cb_cnt == 0)
5312 || !TEST_true(psk_server_cb_cnt == 0))
5313 goto end;
5314 } else {
5315 if (!TEST_true(use_session_cb_cnt == 0)
5316 || !TEST_true(find_session_cb_cnt == 0)
5317 || !TEST_true(psk_client_cb_cnt == 2)
5318 || !TEST_true(psk_server_cb_cnt == 2))
5319 goto end;
5320 }
5321
5322 shutdown_ssl_connection(serverssl, clientssl);
5323 serverssl = clientssl = NULL;
5324 use_session_cb_cnt = find_session_cb_cnt = 0;
5325 psk_client_cb_cnt = psk_server_cb_cnt = 0;
5326
5327 if (idx != 3) {
5328 /*
5329 * Check that if the server rejects the PSK we can still connect, but with
5330 * a full handshake
5331 */
5332 srvid = "Dummy Identity";
5333 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5334 NULL, NULL))
5335 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5336 SSL_ERROR_NONE))
5337 || !TEST_false(SSL_session_reused(clientssl))
5338 || !TEST_false(SSL_session_reused(serverssl)))
5339 goto end;
5340
5341 if (idx == 0 || idx == 1) {
5342 if (!TEST_true(use_session_cb_cnt == 1)
5343 || !TEST_true(find_session_cb_cnt == 1)
5344 || !TEST_true(psk_client_cb_cnt == 0)
5345 /*
5346 * If no old style callback then below should be 0
5347 * otherwise 1
5348 */
5349 || !TEST_true(psk_server_cb_cnt == idx))
5350 goto end;
5351 } else {
5352 if (!TEST_true(use_session_cb_cnt == 0)
5353 || !TEST_true(find_session_cb_cnt == 0)
5354 || !TEST_true(psk_client_cb_cnt == 1)
5355 || !TEST_true(psk_server_cb_cnt == 1))
5356 goto end;
5357 }
5358
5359 shutdown_ssl_connection(serverssl, clientssl);
5360 serverssl = clientssl = NULL;
5361 }
5362 testresult = 1;
5363
5364 end:
5365 SSL_SESSION_free(clientpsk);
5366 SSL_SESSION_free(serverpsk);
5367 clientpsk = serverpsk = NULL;
5368 SSL_free(serverssl);
5369 SSL_free(clientssl);
5370 SSL_CTX_free(sctx);
5371 SSL_CTX_free(cctx);
5372 return testresult;
5373 }
5374
5375 static unsigned char cookie_magic_value[] = "cookie magic";
5376
5377 static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
5378 unsigned int *cookie_len)
5379 {
5380 /*
5381 * Not suitable as a real cookie generation function but good enough for
5382 * testing!
5383 */
5384 memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
5385 *cookie_len = sizeof(cookie_magic_value) - 1;
5386
5387 return 1;
5388 }
5389
5390 static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
5391 unsigned int cookie_len)
5392 {
5393 if (cookie_len == sizeof(cookie_magic_value) - 1
5394 && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
5395 return 1;
5396
5397 return 0;
5398 }
5399
5400 static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
5401 size_t *cookie_len)
5402 {
5403 unsigned int temp;
5404 int res = generate_cookie_callback(ssl, cookie, &temp);
5405 *cookie_len = temp;
5406 return res;
5407 }
5408
5409 static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
5410 size_t cookie_len)
5411 {
5412 return verify_cookie_callback(ssl, cookie, cookie_len);
5413 }
5414
5415 static int test_stateless(void)
5416 {
5417 SSL_CTX *sctx = NULL, *cctx = NULL;
5418 SSL *serverssl = NULL, *clientssl = NULL;
5419 int testresult = 0;
5420
5421 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5422 TLS_client_method(), TLS1_VERSION, 0,
5423 &sctx, &cctx, cert, privkey)))
5424 goto end;
5425
5426 /* The arrival of CCS messages can confuse the test */
5427 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
5428
5429 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5430 NULL, NULL))
5431 /* Send the first ClientHello */
5432 || !TEST_false(create_ssl_connection(serverssl, clientssl,
5433 SSL_ERROR_WANT_READ))
5434 /*
5435 * This should fail with a -1 return because we have no callbacks
5436 * set up
5437 */
5438 || !TEST_int_eq(SSL_stateless(serverssl), -1))
5439 goto end;
5440
5441 /* Fatal error so abandon the connection from this client */
5442 SSL_free(clientssl);
5443 clientssl = NULL;
5444
5445 /* Set up the cookie generation and verification callbacks */
5446 SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
5447 SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
5448
5449 /*
5450 * Create a new connection from the client (we can reuse the server SSL
5451 * object).
5452 */
5453 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5454 NULL, NULL))
5455 /* Send the first ClientHello */
5456 || !TEST_false(create_ssl_connection(serverssl, clientssl,
5457 SSL_ERROR_WANT_READ))
5458 /* This should fail because there is no cookie */
5459 || !TEST_int_eq(SSL_stateless(serverssl), 0))
5460 goto end;
5461
5462 /* Abandon the connection from this client */
5463 SSL_free(clientssl);
5464 clientssl = NULL;
5465
5466 /*
5467 * Now create a connection from a new client but with the same server SSL
5468 * object
5469 */
5470 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5471 NULL, NULL))
5472 /* Send the first ClientHello */
5473 || !TEST_false(create_ssl_connection(serverssl, clientssl,
5474 SSL_ERROR_WANT_READ))
5475 /* This should fail because there is no cookie */
5476 || !TEST_int_eq(SSL_stateless(serverssl), 0)
5477 /* Send the second ClientHello */
5478 || !TEST_false(create_ssl_connection(serverssl, clientssl,
5479 SSL_ERROR_WANT_READ))
5480 /* This should succeed because a cookie is now present */
5481 || !TEST_int_eq(SSL_stateless(serverssl), 1)
5482 /* Complete the connection */
5483 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5484 SSL_ERROR_NONE)))
5485 goto end;
5486
5487 shutdown_ssl_connection(serverssl, clientssl);
5488 serverssl = clientssl = NULL;
5489 testresult = 1;
5490
5491 end:
5492 SSL_free(serverssl);
5493 SSL_free(clientssl);
5494 SSL_CTX_free(sctx);
5495 SSL_CTX_free(cctx);
5496 return testresult;
5497
5498 }
5499 #endif /* OSSL_NO_USABLE_TLS1_3 */
5500
5501 static int clntaddoldcb = 0;
5502 static int clntparseoldcb = 0;
5503 static int srvaddoldcb = 0;
5504 static int srvparseoldcb = 0;
5505 static int clntaddnewcb = 0;
5506 static int clntparsenewcb = 0;
5507 static int srvaddnewcb = 0;
5508 static int srvparsenewcb = 0;
5509 static int snicb = 0;
5510
5511 #define TEST_EXT_TYPE1 0xff00
5512
5513 static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
5514 size_t *outlen, int *al, void *add_arg)
5515 {
5516 int *server = (int *)add_arg;
5517 unsigned char *data;
5518
5519 if (SSL_is_server(s))
5520 srvaddoldcb++;
5521 else
5522 clntaddoldcb++;
5523
5524 if (*server != SSL_is_server(s)
5525 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5526 return -1;
5527
5528 *data = 1;
5529 *out = data;
5530 *outlen = sizeof(char);
5531 return 1;
5532 }
5533
5534 static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
5535 void *add_arg)
5536 {
5537 OPENSSL_free((unsigned char *)out);
5538 }
5539
5540 static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
5541 size_t inlen, int *al, void *parse_arg)
5542 {
5543 int *server = (int *)parse_arg;
5544
5545 if (SSL_is_server(s))
5546 srvparseoldcb++;
5547 else
5548 clntparseoldcb++;
5549
5550 if (*server != SSL_is_server(s)
5551 || inlen != sizeof(char)
5552 || *in != 1)
5553 return -1;
5554
5555 return 1;
5556 }
5557
5558 static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
5559 const unsigned char **out, size_t *outlen, X509 *x,
5560 size_t chainidx, int *al, void *add_arg)
5561 {
5562 int *server = (int *)add_arg;
5563 unsigned char *data;
5564
5565 if (SSL_is_server(s))
5566 srvaddnewcb++;
5567 else
5568 clntaddnewcb++;
5569
5570 if (*server != SSL_is_server(s)
5571 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5572 return -1;
5573
5574 *data = 1;
5575 *out = data;
5576 *outlen = sizeof(*data);
5577 return 1;
5578 }
5579
5580 static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
5581 const unsigned char *out, void *add_arg)
5582 {
5583 OPENSSL_free((unsigned char *)out);
5584 }
5585
5586 static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
5587 const unsigned char *in, size_t inlen, X509 *x,
5588 size_t chainidx, int *al, void *parse_arg)
5589 {
5590 int *server = (int *)parse_arg;
5591
5592 if (SSL_is_server(s))
5593 srvparsenewcb++;
5594 else
5595 clntparsenewcb++;
5596
5597 if (*server != SSL_is_server(s)
5598 || inlen != sizeof(char) || *in != 1)
5599 return -1;
5600
5601 return 1;
5602 }
5603
5604 static int sni_cb(SSL *s, int *al, void *arg)
5605 {
5606 SSL_CTX *ctx = (SSL_CTX *)arg;
5607
5608 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
5609 *al = SSL_AD_INTERNAL_ERROR;
5610 return SSL_TLSEXT_ERR_ALERT_FATAL;
5611 }
5612 snicb++;
5613 return SSL_TLSEXT_ERR_OK;
5614 }
5615
5616 static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
5617 {
5618 return 1;
5619 }
5620
5621 /*
5622 * Custom call back tests.
5623 * Test 0: Old style callbacks in TLSv1.2
5624 * Test 1: New style callbacks in TLSv1.2
5625 * Test 2: New style callbacks in TLSv1.2 with SNI
5626 * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
5627 * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
5628 * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
5629 */
5630 static int test_custom_exts(int tst)
5631 {
5632 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
5633 SSL *clientssl = NULL, *serverssl = NULL;
5634 int testresult = 0;
5635 static int server = 1;
5636 static int client = 0;
5637 SSL_SESSION *sess = NULL;
5638 unsigned int context;
5639
5640 #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
5641 /* Skip tests for TLSv1.2 and below in this case */
5642 if (tst < 3)
5643 return 1;
5644 #endif
5645
5646 /* Reset callback counters */
5647 clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
5648 clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
5649 snicb = 0;
5650
5651 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5652 TLS_client_method(), TLS1_VERSION, 0,
5653 &sctx, &cctx, cert, privkey)))
5654 goto end;
5655
5656 if (tst == 2
5657 && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
5658 TLS1_VERSION, 0,
5659 &sctx2, NULL, cert, privkey)))
5660 goto end;
5661
5662
5663 if (tst < 3) {
5664 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
5665 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
5666 if (sctx2 != NULL)
5667 SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
5668 }
5669
5670 if (tst == 5) {
5671 context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
5672 | SSL_EXT_TLS1_3_CERTIFICATE;
5673 SSL_CTX_set_verify(sctx,
5674 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
5675 verify_cb);
5676 if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
5677 SSL_FILETYPE_PEM), 1)
5678 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
5679 SSL_FILETYPE_PEM), 1)
5680 || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
5681 goto end;
5682 } else if (tst == 4) {
5683 context = SSL_EXT_CLIENT_HELLO
5684 | SSL_EXT_TLS1_2_SERVER_HELLO
5685 | SSL_EXT_TLS1_3_SERVER_HELLO
5686 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
5687 | SSL_EXT_TLS1_3_CERTIFICATE
5688 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
5689 } else {
5690 context = SSL_EXT_CLIENT_HELLO
5691 | SSL_EXT_TLS1_2_SERVER_HELLO
5692 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
5693 }
5694
5695 /* Create a client side custom extension */
5696 if (tst == 0) {
5697 if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5698 old_add_cb, old_free_cb,
5699 &client, old_parse_cb,
5700 &client)))
5701 goto end;
5702 } else {
5703 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
5704 new_add_cb, new_free_cb,
5705 &client, new_parse_cb, &client)))
5706 goto end;
5707 }
5708
5709 /* Should not be able to add duplicates */
5710 if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5711 old_add_cb, old_free_cb,
5712 &client, old_parse_cb,
5713 &client))
5714 || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
5715 context, new_add_cb,
5716 new_free_cb, &client,
5717 new_parse_cb, &client)))
5718 goto end;
5719
5720 /* Create a server side custom extension */
5721 if (tst == 0) {
5722 if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5723 old_add_cb, old_free_cb,
5724 &server, old_parse_cb,
5725 &server)))
5726 goto end;
5727 } else {
5728 if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
5729 new_add_cb, new_free_cb,
5730 &server, new_parse_cb, &server)))
5731 goto end;
5732 if (sctx2 != NULL
5733 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
5734 context, new_add_cb,
5735 new_free_cb, &server,
5736 new_parse_cb, &server)))
5737 goto end;
5738 }
5739
5740 /* Should not be able to add duplicates */
5741 if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5742 old_add_cb, old_free_cb,
5743 &server, old_parse_cb,
5744 &server))
5745 || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
5746 context, new_add_cb,
5747 new_free_cb, &server,
5748 new_parse_cb, &server)))
5749 goto end;
5750
5751 if (tst == 2) {
5752 /* Set up SNI */
5753 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
5754 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
5755 goto end;
5756 }
5757
5758 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5759 &clientssl, NULL, NULL))
5760 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5761 SSL_ERROR_NONE)))
5762 goto end;
5763
5764 if (tst == 0) {
5765 if (clntaddoldcb != 1
5766 || clntparseoldcb != 1
5767 || srvaddoldcb != 1
5768 || srvparseoldcb != 1)
5769 goto end;
5770 } else if (tst == 1 || tst == 2 || tst == 3) {
5771 if (clntaddnewcb != 1
5772 || clntparsenewcb != 1
5773 || srvaddnewcb != 1
5774 || srvparsenewcb != 1
5775 || (tst != 2 && snicb != 0)
5776 || (tst == 2 && snicb != 1))
5777 goto end;
5778 } else if (tst == 5) {
5779 if (clntaddnewcb != 1
5780 || clntparsenewcb != 1
5781 || srvaddnewcb != 1
5782 || srvparsenewcb != 1)
5783 goto end;
5784 } else {
5785 /* In this case there 2 NewSessionTicket messages created */
5786 if (clntaddnewcb != 1
5787 || clntparsenewcb != 5
5788 || srvaddnewcb != 5
5789 || srvparsenewcb != 1)
5790 goto end;
5791 }
5792
5793 sess = SSL_get1_session(clientssl);
5794 SSL_shutdown(clientssl);
5795 SSL_shutdown(serverssl);
5796 SSL_free(serverssl);
5797 SSL_free(clientssl);
5798 serverssl = clientssl = NULL;
5799
5800 if (tst == 3 || tst == 5) {
5801 /* We don't bother with the resumption aspects for these tests */
5802 testresult = 1;
5803 goto end;
5804 }
5805
5806 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5807 NULL, NULL))
5808 || !TEST_true(SSL_set_session(clientssl, sess))
5809 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5810 SSL_ERROR_NONE)))
5811 goto end;
5812
5813 /*
5814 * For a resumed session we expect to add the ClientHello extension. For the
5815 * old style callbacks we ignore it on the server side because they set
5816 * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
5817 * them.
5818 */
5819 if (tst == 0) {
5820 if (clntaddoldcb != 2
5821 || clntparseoldcb != 1
5822 || srvaddoldcb != 1
5823 || srvparseoldcb != 1)
5824 goto end;
5825 } else if (tst == 1 || tst == 2 || tst == 3) {
5826 if (clntaddnewcb != 2
5827 || clntparsenewcb != 2
5828 || srvaddnewcb != 2
5829 || srvparsenewcb != 2)
5830 goto end;
5831 } else {
5832 /*
5833 * No Certificate message extensions in the resumption handshake,
5834 * 2 NewSessionTickets in the initial handshake, 1 in the resumption
5835 */
5836 if (clntaddnewcb != 2
5837 || clntparsenewcb != 8
5838 || srvaddnewcb != 8
5839 || srvparsenewcb != 2)
5840 goto end;
5841 }
5842
5843 testresult = 1;
5844
5845 end:
5846 SSL_SESSION_free(sess);
5847 SSL_free(serverssl);
5848 SSL_free(clientssl);
5849 SSL_CTX_free(sctx2);
5850 SSL_CTX_free(sctx);
5851 SSL_CTX_free(cctx);
5852 return testresult;
5853 }
5854
5855 #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
5856
5857 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
5858 | SSL_EXT_CLIENT_HELLO \
5859 | SSL_EXT_TLS1_2_SERVER_HELLO \
5860 | SSL_EXT_IGNORE_ON_RESUMPTION)
5861
5862 #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
5863 | SSL_EXT_TLS1_2_SERVER_HELLO \
5864 | SSL_EXT_CLIENT_HELLO)
5865
5866 #define SERVERINFO_CUSTOM \
5867 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
5868 0x00, 0x03, \
5869 0x04, 0x05, 0x06 \
5870
5871 static const unsigned char serverinfo_custom_tls13[] = {
5872 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
5873 SERVERINFO_CUSTOM
5874 };
5875 static const unsigned char serverinfo_custom_v2[] = {
5876 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff,
5877 SERVERINFO_CUSTOM
5878 };
5879 static const unsigned char serverinfo_custom_v1[] = {
5880 SERVERINFO_CUSTOM
5881 };
5882 static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
5883 static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
5884 static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
5885
5886 static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
5887 unsigned int context,
5888 const unsigned char *in,
5889 size_t inlen, X509 *x,
5890 size_t chainidx, int *al,
5891 void *parse_arg)
5892 {
5893 const size_t len = serverinfo_custom_v1_len;
5894 const unsigned char *si = &serverinfo_custom_v1[len - 3];
5895 int *p_cb_result = (int*)parse_arg;
5896 *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
5897 return 1;
5898 }
5899
5900 static int test_serverinfo_custom(const int idx)
5901 {
5902 SSL_CTX *sctx = NULL, *cctx = NULL;
5903 SSL *clientssl = NULL, *serverssl = NULL;
5904 int testresult = 0;
5905 int cb_result = 0;
5906
5907 /*
5908 * Following variables are set in the switch statement
5909 * according to the test iteration.
5910 * Default values do not make much sense: test would fail with them.
5911 */
5912 int serverinfo_version = 0;
5913 int protocol_version = 0;
5914 unsigned int extension_context = 0;
5915 const unsigned char *si = NULL;
5916 size_t si_len = 0;
5917
5918 const int call_use_serverinfo_ex = idx > 0;
5919 switch (idx) {
5920 case 0: /* FALLTHROUGH */
5921 case 1:
5922 serverinfo_version = SSL_SERVERINFOV1;
5923 protocol_version = TLS1_2_VERSION;
5924 extension_context = SYNTHV1CONTEXT;
5925 si = serverinfo_custom_v1;
5926 si_len = serverinfo_custom_v1_len;
5927 break;
5928 case 2:
5929 serverinfo_version = SSL_SERVERINFOV2;
5930 protocol_version = TLS1_2_VERSION;
5931 extension_context = SYNTHV1CONTEXT;
5932 si = serverinfo_custom_v2;
5933 si_len = serverinfo_custom_v2_len;
5934 break;
5935 case 3:
5936 serverinfo_version = SSL_SERVERINFOV2;
5937 protocol_version = TLS1_3_VERSION;
5938 extension_context = TLS13CONTEXT;
5939 si = serverinfo_custom_tls13;
5940 si_len = serverinfo_custom_tls13_len;
5941 break;
5942 }
5943
5944 if (!TEST_true(create_ssl_ctx_pair(libctx,
5945 TLS_method(),
5946 TLS_method(),
5947 protocol_version,
5948 protocol_version,
5949 &sctx, &cctx, cert, privkey)))
5950 goto end;
5951
5952 if (call_use_serverinfo_ex) {
5953 if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
5954 si, si_len)))
5955 goto end;
5956 } else {
5957 if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
5958 goto end;
5959 }
5960
5961 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
5962 extension_context,
5963 NULL, NULL, NULL,
5964 serverinfo_custom_parse_cb,
5965 &cb_result))
5966 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5967 NULL, NULL))
5968 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5969 SSL_ERROR_NONE))
5970 || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
5971 goto end;
5972
5973 if (!TEST_true(cb_result))
5974 goto end;
5975
5976 testresult = 1;
5977
5978 end:
5979 SSL_free(serverssl);
5980 SSL_free(clientssl);
5981 SSL_CTX_free(sctx);
5982 SSL_CTX_free(cctx);
5983
5984 return testresult;
5985 }
5986 #endif
5987
5988 /*
5989 * Test that SSL_export_keying_material() produces expected results. There are
5990 * no test vectors so all we do is test that both sides of the communication
5991 * produce the same results for different protocol versions.
5992 */
5993 #define SMALL_LABEL_LEN 10
5994 #define LONG_LABEL_LEN 249
5995 static int test_export_key_mat(int tst)
5996 {
5997 int testresult = 0;
5998 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
5999 SSL *clientssl = NULL, *serverssl = NULL;
6000 const char label[LONG_LABEL_LEN + 1] = "test label";
6001 const unsigned char context[] = "context";
6002 const unsigned char *emptycontext = NULL;
6003 unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
6004 unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
6005 size_t labellen;
6006 const int protocols[] = {
6007 TLS1_VERSION,
6008 TLS1_1_VERSION,
6009 TLS1_2_VERSION,
6010 TLS1_3_VERSION,
6011 TLS1_3_VERSION,
6012 TLS1_3_VERSION
6013 };
6014
6015 #ifdef OPENSSL_NO_TLS1
6016 if (tst == 0)
6017 return 1;
6018 #endif
6019 #ifdef OPENSSL_NO_TLS1_1
6020 if (tst == 1)
6021 return 1;
6022 #endif
6023 if (is_fips && (tst == 0 || tst == 1))
6024 return 1;
6025 #ifdef OPENSSL_NO_TLS1_2
6026 if (tst == 2)
6027 return 1;
6028 #endif
6029 #ifdef OSSL_NO_USABLE_TLS1_3
6030 if (tst >= 3)
6031 return 1;
6032 #endif
6033 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6034 TLS_client_method(), TLS1_VERSION, 0,
6035 &sctx, &cctx, cert, privkey)))
6036 goto end;
6037
6038 OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
6039 SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
6040 SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
6041 if ((protocols[tst] < TLS1_2_VERSION) &&
6042 (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
6043 || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
6044 goto end;
6045
6046 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
6047 NULL)))
6048 goto end;
6049
6050 /*
6051 * Premature call of SSL_export_keying_material should just fail.
6052 */
6053 if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6054 sizeof(ckeymat1), label,
6055 SMALL_LABEL_LEN + 1, context,
6056 sizeof(context) - 1, 1), 0))
6057 goto end;
6058
6059 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6060 SSL_ERROR_NONE)))
6061 goto end;
6062
6063 if (tst == 5) {
6064 /*
6065 * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
6066 * go over that.
6067 */
6068 if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6069 sizeof(ckeymat1), label,
6070 LONG_LABEL_LEN + 1, context,
6071 sizeof(context) - 1, 1), 0))
6072 goto end;
6073
6074 testresult = 1;
6075 goto end;
6076 } else if (tst == 4) {
6077 labellen = LONG_LABEL_LEN;
6078 } else {
6079 labellen = SMALL_LABEL_LEN;
6080 }
6081
6082 if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
6083 sizeof(ckeymat1), label,
6084 labellen, context,
6085 sizeof(context) - 1, 1), 1)
6086 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
6087 sizeof(ckeymat2), label,
6088 labellen,
6089 emptycontext,
6090 0, 1), 1)
6091 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
6092 sizeof(ckeymat3), label,
6093 labellen,
6094 NULL, 0, 0), 1)
6095 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
6096 sizeof(skeymat1), label,
6097 labellen,
6098 context,
6099 sizeof(context) -1, 1),
6100 1)
6101 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
6102 sizeof(skeymat2), label,
6103 labellen,
6104 emptycontext,
6105 0, 1), 1)
6106 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
6107 sizeof(skeymat3), label,
6108 labellen,
6109 NULL, 0, 0), 1)
6110 /*
6111 * Check that both sides created the same key material with the
6112 * same context.
6113 */
6114 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6115 sizeof(skeymat1))
6116 /*
6117 * Check that both sides created the same key material with an
6118 * empty context.
6119 */
6120 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6121 sizeof(skeymat2))
6122 /*
6123 * Check that both sides created the same key material without a
6124 * context.
6125 */
6126 || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
6127 sizeof(skeymat3))
6128 /* Different contexts should produce different results */
6129 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6130 sizeof(ckeymat2)))
6131 goto end;
6132
6133 /*
6134 * Check that an empty context and no context produce different results in
6135 * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
6136 */
6137 if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
6138 sizeof(ckeymat3)))
6139 || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
6140 sizeof(ckeymat3))))
6141 goto end;
6142
6143 testresult = 1;
6144
6145 end:
6146 SSL_free(serverssl);
6147 SSL_free(clientssl);
6148 SSL_CTX_free(sctx2);
6149 SSL_CTX_free(sctx);
6150 SSL_CTX_free(cctx);
6151
6152 return testresult;
6153 }
6154
6155 #ifndef OSSL_NO_USABLE_TLS1_3
6156 /*
6157 * Test that SSL_export_keying_material_early() produces expected
6158 * results. There are no test vectors so all we do is test that both
6159 * sides of the communication produce the same results for different
6160 * protocol versions.
6161 */
6162 static int test_export_key_mat_early(int idx)
6163 {
6164 static const char label[] = "test label";
6165 static const unsigned char context[] = "context";
6166 int testresult = 0;
6167 SSL_CTX *cctx = NULL, *sctx = NULL;
6168 SSL *clientssl = NULL, *serverssl = NULL;
6169 SSL_SESSION *sess = NULL;
6170 const unsigned char *emptycontext = NULL;
6171 unsigned char ckeymat1[80], ckeymat2[80];
6172 unsigned char skeymat1[80], skeymat2[80];
6173 unsigned char buf[1];
6174 size_t readbytes, written;
6175
6176 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
6177 &sess, idx)))
6178 goto end;
6179
6180 /* Here writing 0 length early data is enough. */
6181 if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
6182 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
6183 &readbytes),
6184 SSL_READ_EARLY_DATA_ERROR)
6185 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
6186 SSL_EARLY_DATA_ACCEPTED))
6187 goto end;
6188
6189 if (!TEST_int_eq(SSL_export_keying_material_early(
6190 clientssl, ckeymat1, sizeof(ckeymat1), label,
6191 sizeof(label) - 1, context, sizeof(context) - 1), 1)
6192 || !TEST_int_eq(SSL_export_keying_material_early(
6193 clientssl, ckeymat2, sizeof(ckeymat2), label,
6194 sizeof(label) - 1, emptycontext, 0), 1)
6195 || !TEST_int_eq(SSL_export_keying_material_early(
6196 serverssl, skeymat1, sizeof(skeymat1), label,
6197 sizeof(label) - 1, context, sizeof(context) - 1), 1)
6198 || !TEST_int_eq(SSL_export_keying_material_early(
6199 serverssl, skeymat2, sizeof(skeymat2), label,
6200 sizeof(label) - 1, emptycontext, 0), 1)
6201 /*
6202 * Check that both sides created the same key material with the
6203 * same context.
6204 */
6205 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6206 sizeof(skeymat1))
6207 /*
6208 * Check that both sides created the same key material with an
6209 * empty context.
6210 */
6211 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6212 sizeof(skeymat2))
6213 /* Different contexts should produce different results */
6214 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6215 sizeof(ckeymat2)))
6216 goto end;
6217
6218 testresult = 1;
6219
6220 end:
6221 SSL_SESSION_free(sess);
6222 SSL_SESSION_free(clientpsk);
6223 SSL_SESSION_free(serverpsk);
6224 clientpsk = serverpsk = NULL;
6225 SSL_free(serverssl);
6226 SSL_free(clientssl);
6227 SSL_CTX_free(sctx);
6228 SSL_CTX_free(cctx);
6229
6230 return testresult;
6231 }
6232
6233 #define NUM_KEY_UPDATE_MESSAGES 40
6234 /*
6235 * Test KeyUpdate.
6236 */
6237 static int test_key_update(void)
6238 {
6239 SSL_CTX *cctx = NULL, *sctx = NULL;
6240 SSL *clientssl = NULL, *serverssl = NULL;
6241 int testresult = 0, i, j;
6242 char buf[20];
6243 static char *mess = "A test message";
6244
6245 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6246 TLS_client_method(),
6247 TLS1_3_VERSION,
6248 0,
6249 &sctx, &cctx, cert, privkey))
6250 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6251 NULL, NULL))
6252 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6253 SSL_ERROR_NONE)))
6254 goto end;
6255
6256 for (j = 0; j < 2; j++) {
6257 /* Send lots of KeyUpdate messages */
6258 for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
6259 if (!TEST_true(SSL_key_update(clientssl,
6260 (j == 0)
6261 ? SSL_KEY_UPDATE_NOT_REQUESTED
6262 : SSL_KEY_UPDATE_REQUESTED))
6263 || !TEST_true(SSL_do_handshake(clientssl)))
6264 goto end;
6265 }
6266
6267 /* Check that sending and receiving app data is ok */
6268 if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
6269 || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
6270 strlen(mess)))
6271 goto end;
6272
6273 if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
6274 || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
6275 strlen(mess)))
6276 goto end;
6277 }
6278
6279 testresult = 1;
6280
6281 end:
6282 SSL_free(serverssl);
6283 SSL_free(clientssl);
6284 SSL_CTX_free(sctx);
6285 SSL_CTX_free(cctx);
6286
6287 return testresult;
6288 }
6289
6290 /*
6291 * Test we can handle a KeyUpdate (update requested) message while
6292 * write data is pending in peer.
6293 * Test 0: Client sends KeyUpdate while Server is writing
6294 * Test 1: Server sends KeyUpdate while Client is writing
6295 */
6296 static int test_key_update_peer_in_write(int tst)
6297 {
6298 SSL_CTX *cctx = NULL, *sctx = NULL;
6299 SSL *clientssl = NULL, *serverssl = NULL;
6300 int testresult = 0;
6301 char buf[20];
6302 static char *mess = "A test message";
6303 BIO *bretry = BIO_new(bio_s_always_retry());
6304 BIO *tmp = NULL;
6305 SSL *peerupdate = NULL, *peerwrite = NULL;
6306
6307 if (!TEST_ptr(bretry)
6308 || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6309 TLS_client_method(),
6310 TLS1_3_VERSION,
6311 0,
6312 &sctx, &cctx, cert, privkey))
6313 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6314 NULL, NULL))
6315 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6316 SSL_ERROR_NONE)))
6317 goto end;
6318
6319 peerupdate = tst == 0 ? clientssl : serverssl;
6320 peerwrite = tst == 0 ? serverssl : clientssl;
6321
6322 if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
6323 || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
6324 goto end;
6325
6326 /* Swap the writing endpoint's write BIO to force a retry */
6327 tmp = SSL_get_wbio(peerwrite);
6328 if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6329 tmp = NULL;
6330 goto end;
6331 }
6332 SSL_set0_wbio(peerwrite, bretry);
6333 bretry = NULL;
6334
6335 /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
6336 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
6337 || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
6338 goto end;
6339
6340 /* Reinstate the original writing endpoint's write BIO */
6341 SSL_set0_wbio(peerwrite, tmp);
6342 tmp = NULL;
6343
6344 /* Now read some data - we will read the key update */
6345 if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
6346 || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
6347 goto end;
6348
6349 /*
6350 * Complete the write we started previously and read it from the other
6351 * endpoint
6352 */
6353 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6354 || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6355 goto end;
6356
6357 /* Write more data to ensure we send the KeyUpdate message back */
6358 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6359 || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6360 goto end;
6361
6362 testresult = 1;
6363
6364 end:
6365 SSL_free(serverssl);
6366 SSL_free(clientssl);
6367 SSL_CTX_free(sctx);
6368 SSL_CTX_free(cctx);
6369 BIO_free(bretry);
6370 BIO_free(tmp);
6371
6372 return testresult;
6373 }
6374
6375 /*
6376 * Test we can handle a KeyUpdate (update requested) message while
6377 * peer read data is pending after peer accepted keyupdate(the msg header
6378 * had been read 5 bytes).
6379 * Test 0: Client sends KeyUpdate while Server is reading
6380 * Test 1: Server sends KeyUpdate while Client is reading
6381 */
6382 static int test_key_update_peer_in_read(int tst)
6383 {
6384 SSL_CTX *cctx = NULL, *sctx = NULL;
6385 SSL *clientssl = NULL, *serverssl = NULL;
6386 int testresult = 0;
6387 char prbuf[515], lwbuf[515] = {0};
6388 static char *mess = "A test message";
6389 BIO *lbio = NULL, *pbio = NULL;
6390 SSL *local = NULL, *peer = NULL;
6391
6392 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6393 TLS_client_method(),
6394 TLS1_3_VERSION,
6395 0,
6396 &sctx, &cctx, cert, privkey))
6397 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6398 NULL, NULL))
6399 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6400 SSL_ERROR_NONE)))
6401 goto end;
6402
6403 local = tst == 0 ? clientssl : serverssl;
6404 peer = tst == 0 ? serverssl : clientssl;
6405
6406 if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6407 goto end;
6408
6409 SSL_set_bio(local, lbio, lbio);
6410 SSL_set_bio(peer, pbio, pbio);
6411
6412 /*
6413 * we first write keyupdate msg then appdata in local
6414 * write data in local will fail with SSL_ERROR_WANT_WRITE,because
6415 * lwbuf app data msg size + key updata msg size > 512(the size of
6416 * the bio pair buffer)
6417 */
6418 if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6419 || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
6420 || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6421 goto end;
6422
6423 /*
6424 * first read keyupdate msg in peer in peer
6425 * then read appdata that we know will fail with SSL_ERROR_WANT_READ
6426 */
6427 if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
6428 || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
6429 goto end;
6430
6431 /* Now write some data in peer - we will write the key update */
6432 if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
6433 goto end;
6434
6435 /*
6436 * write data in local previously that we will complete
6437 * read data in peer previously that we will complete
6438 */
6439 if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
6440 || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
6441 goto end;
6442
6443 /* check that sending and receiving appdata ok */
6444 if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6445 || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6446 goto end;
6447
6448 testresult = 1;
6449
6450 end:
6451 SSL_free(serverssl);
6452 SSL_free(clientssl);
6453 SSL_CTX_free(sctx);
6454 SSL_CTX_free(cctx);
6455
6456 return testresult;
6457 }
6458
6459 /*
6460 * Test we can't send a KeyUpdate (update requested) message while
6461 * local write data is pending.
6462 * Test 0: Client sends KeyUpdate while Client is writing
6463 * Test 1: Server sends KeyUpdate while Server is writing
6464 */
6465 static int test_key_update_local_in_write(int tst)
6466 {
6467 SSL_CTX *cctx = NULL, *sctx = NULL;
6468 SSL *clientssl = NULL, *serverssl = NULL;
6469 int testresult = 0;
6470 char buf[20];
6471 static char *mess = "A test message";
6472 BIO *bretry = BIO_new(bio_s_always_retry());
6473 BIO *tmp = NULL;
6474 SSL *local = NULL, *peer = NULL;
6475
6476 if (!TEST_ptr(bretry)
6477 || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6478 TLS_client_method(),
6479 TLS1_3_VERSION,
6480 0,
6481 &sctx, &cctx, cert, privkey))
6482 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6483 NULL, NULL))
6484 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6485 SSL_ERROR_NONE)))
6486 goto end;
6487
6488 local = tst == 0 ? clientssl : serverssl;
6489 peer = tst == 0 ? serverssl : clientssl;
6490
6491 /* Swap the writing endpoint's write BIO to force a retry */
6492 tmp = SSL_get_wbio(local);
6493 if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6494 tmp = NULL;
6495 goto end;
6496 }
6497 SSL_set0_wbio(local, bretry);
6498 bretry = NULL;
6499
6500 /* write data in local will fail with SSL_ERROR_WANT_WRITE */
6501 if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
6502 || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6503 goto end;
6504
6505 /* Reinstate the original writing endpoint's write BIO */
6506 SSL_set0_wbio(local, tmp);
6507 tmp = NULL;
6508
6509 /* SSL_key_update will fail, because writing in local*/
6510 if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6511 || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
6512 goto end;
6513
6514 ERR_clear_error();
6515 /* write data in local previously that we will complete */
6516 if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
6517 goto end;
6518
6519 /* SSL_key_update will succeed because there is no pending write data */
6520 if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6521 || !TEST_int_eq(SSL_do_handshake(local), 1))
6522 goto end;
6523
6524 /*
6525 * we write some appdata in local
6526 * read data in peer - we will read the keyupdate msg
6527 */
6528 if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6529 || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
6530 goto end;
6531
6532 /* Write more peer more data to ensure we send the keyupdate message back */
6533 if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6534 || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
6535 goto end;
6536
6537 testresult = 1;
6538
6539 end:
6540 SSL_free(serverssl);
6541 SSL_free(clientssl);
6542 SSL_CTX_free(sctx);
6543 SSL_CTX_free(cctx);
6544 BIO_free(bretry);
6545 BIO_free(tmp);
6546
6547 return testresult;
6548 }
6549
6550 /*
6551 * Test we can handle a KeyUpdate (update requested) message while
6552 * local read data is pending(the msg header had been read 5 bytes).
6553 * Test 0: Client sends KeyUpdate while Client is reading
6554 * Test 1: Server sends KeyUpdate while Server is reading
6555 */
6556 static int test_key_update_local_in_read(int tst)
6557 {
6558 SSL_CTX *cctx = NULL, *sctx = NULL;
6559 SSL *clientssl = NULL, *serverssl = NULL;
6560 int testresult = 0;
6561 char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
6562 static char *mess = "A test message";
6563 BIO *lbio = NULL, *pbio = NULL;
6564 SSL *local = NULL, *peer = NULL;
6565
6566 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6567 TLS_client_method(),
6568 TLS1_3_VERSION,
6569 0,
6570 &sctx, &cctx, cert, privkey))
6571 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6572 NULL, NULL))
6573 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6574 SSL_ERROR_NONE)))
6575 goto end;
6576
6577 local = tst == 0 ? clientssl : serverssl;
6578 peer = tst == 0 ? serverssl : clientssl;
6579
6580 if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6581 goto end;
6582
6583 SSL_set_bio(local, lbio, lbio);
6584 SSL_set_bio(peer, pbio, pbio);
6585
6586 /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
6587 if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
6588 || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
6589 goto end;
6590
6591 /* read appdata in local will fail with SSL_ERROR_WANT_READ */
6592 if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
6593 || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
6594 goto end;
6595
6596 /* SSL_do_handshake will send keyupdate msg */
6597 if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6598 || !TEST_int_eq(SSL_do_handshake(local), 1))
6599 goto end;
6600
6601 /*
6602 * write data in peer previously that we will complete
6603 * read data in local previously that we will complete
6604 */
6605 if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
6606 || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
6607 goto end;
6608
6609 /*
6610 * write data in local
6611 * read data in peer - we will read the key update
6612 */
6613 if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6614 || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6615 goto end;
6616
6617 /* Write more peer data to ensure we send the keyupdate message back */
6618 if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6619 || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
6620 goto end;
6621
6622 testresult = 1;
6623
6624 end:
6625 SSL_free(serverssl);
6626 SSL_free(clientssl);
6627 SSL_CTX_free(sctx);
6628 SSL_CTX_free(cctx);
6629
6630 return testresult;
6631 }
6632 #endif /* OSSL_NO_USABLE_TLS1_3 */
6633
6634 static int test_ssl_clear(int idx)
6635 {
6636 SSL_CTX *cctx = NULL, *sctx = NULL;
6637 SSL *clientssl = NULL, *serverssl = NULL;
6638 int testresult = 0;
6639
6640 #ifdef OPENSSL_NO_TLS1_2
6641 if (idx == 1)
6642 return 1;
6643 #endif
6644
6645 /* Create an initial connection */
6646 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6647 TLS_client_method(), TLS1_VERSION, 0,
6648 &sctx, &cctx, cert, privkey))
6649 || (idx == 1
6650 && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
6651 TLS1_2_VERSION)))
6652 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6653 &clientssl, NULL, NULL))
6654 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6655 SSL_ERROR_NONE)))
6656 goto end;
6657
6658 SSL_shutdown(clientssl);
6659 SSL_shutdown(serverssl);
6660 SSL_free(serverssl);
6661 serverssl = NULL;
6662
6663 /* Clear clientssl - we're going to reuse the object */
6664 if (!TEST_true(SSL_clear(clientssl)))
6665 goto end;
6666
6667 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6668 NULL, NULL))
6669 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6670 SSL_ERROR_NONE))
6671 || !TEST_true(SSL_session_reused(clientssl)))
6672 goto end;
6673
6674 SSL_shutdown(clientssl);
6675 SSL_shutdown(serverssl);
6676
6677 testresult = 1;
6678
6679 end:
6680 SSL_free(serverssl);
6681 SSL_free(clientssl);
6682 SSL_CTX_free(sctx);
6683 SSL_CTX_free(cctx);
6684
6685 return testresult;
6686 }
6687
6688 /* Parse CH and retrieve any MFL extension value if present */
6689 static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
6690 {
6691 long len;
6692 unsigned char *data;
6693 PACKET pkt, pkt2, pkt3;
6694 unsigned int MFL_code = 0, type = 0;
6695
6696 if (!TEST_uint_gt(len = BIO_get_mem_data(bio, (char **) &data), 0))
6697 goto end;
6698
6699 memset(&pkt, 0, sizeof(pkt));
6700 memset(&pkt2, 0, sizeof(pkt2));
6701 memset(&pkt3, 0, sizeof(pkt3));
6702
6703 if (!TEST_long_gt(len, 0)
6704 || !TEST_true(PACKET_buf_init(&pkt, data, len))
6705 /* Skip the record header */
6706 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
6707 /* Skip the handshake message header */
6708 || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
6709 /* Skip client version and random */
6710 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
6711 + SSL3_RANDOM_SIZE))
6712 /* Skip session id */
6713 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6714 /* Skip ciphers */
6715 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
6716 /* Skip compression */
6717 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6718 /* Extensions len */
6719 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6720 goto end;
6721
6722 /* Loop through all extensions */
6723 while (PACKET_remaining(&pkt2)) {
6724 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
6725 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
6726 goto end;
6727
6728 if (type == TLSEXT_TYPE_max_fragment_length) {
6729 if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
6730 || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
6731 goto end;
6732
6733 *mfl_codemfl_code = MFL_code;
6734 return 1;
6735 }
6736 }
6737
6738 end:
6739 return 0;
6740 }
6741
6742 /* Maximum-Fragment-Length TLS extension mode to test */
6743 static const unsigned char max_fragment_len_test[] = {
6744 TLSEXT_max_fragment_length_512,
6745 TLSEXT_max_fragment_length_1024,
6746 TLSEXT_max_fragment_length_2048,
6747 TLSEXT_max_fragment_length_4096
6748 };
6749
6750 static int test_max_fragment_len_ext(int idx_tst)
6751 {
6752 SSL_CTX *ctx = NULL;
6753 SSL *con = NULL;
6754 int testresult = 0, MFL_mode = 0;
6755 BIO *rbio, *wbio;
6756
6757 if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
6758 TLS1_VERSION, 0, NULL, &ctx, NULL,
6759 NULL)))
6760 return 0;
6761
6762 if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
6763 ctx, max_fragment_len_test[idx_tst])))
6764 goto end;
6765
6766 con = SSL_new(ctx);
6767 if (!TEST_ptr(con))
6768 goto end;
6769
6770 rbio = BIO_new(BIO_s_mem());
6771 wbio = BIO_new(BIO_s_mem());
6772 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
6773 BIO_free(rbio);
6774 BIO_free(wbio);
6775 goto end;
6776 }
6777
6778 SSL_set_bio(con, rbio, wbio);
6779
6780 if (!TEST_int_le(SSL_connect(con), 0)) {
6781 /* This shouldn't succeed because we don't have a server! */
6782 goto end;
6783 }
6784
6785 if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
6786 /* no MFL in client hello */
6787 goto end;
6788 if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
6789 goto end;
6790
6791 testresult = 1;
6792
6793 end:
6794 SSL_free(con);
6795 SSL_CTX_free(ctx);
6796
6797 return testresult;
6798 }
6799
6800 #ifndef OSSL_NO_USABLE_TLS1_3
6801 static int test_pha_key_update(void)
6802 {
6803 SSL_CTX *cctx = NULL, *sctx = NULL;
6804 SSL *clientssl = NULL, *serverssl = NULL;
6805 int testresult = 0;
6806
6807 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6808 TLS_client_method(), TLS1_VERSION, 0,
6809 &sctx, &cctx, cert, privkey)))
6810 return 0;
6811
6812 if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
6813 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
6814 || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
6815 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
6816 goto end;
6817
6818 SSL_CTX_set_post_handshake_auth(cctx, 1);
6819
6820 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6821 NULL, NULL)))
6822 goto end;
6823
6824 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6825 SSL_ERROR_NONE)))
6826 goto end;
6827
6828 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
6829 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
6830 goto end;
6831
6832 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
6833 goto end;
6834
6835 /* Start handshake on the server */
6836 if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
6837 goto end;
6838
6839 /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
6840 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6841 SSL_ERROR_NONE)))
6842 goto end;
6843
6844 SSL_shutdown(clientssl);
6845 SSL_shutdown(serverssl);
6846
6847 testresult = 1;
6848
6849 end:
6850 SSL_free(serverssl);
6851 SSL_free(clientssl);
6852 SSL_CTX_free(sctx);
6853 SSL_CTX_free(cctx);
6854 return testresult;
6855 }
6856 #endif
6857
6858 #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
6859
6860 static SRP_VBASE *vbase = NULL;
6861
6862 static int ssl_srp_cb(SSL *s, int *ad, void *arg)
6863 {
6864 int ret = SSL3_AL_FATAL;
6865 char *username;
6866 SRP_user_pwd *user = NULL;
6867
6868 username = SSL_get_srp_username(s);
6869 if (username == NULL) {
6870 *ad = SSL_AD_INTERNAL_ERROR;
6871 goto err;
6872 }
6873
6874 user = SRP_VBASE_get1_by_user(vbase, username);
6875 if (user == NULL) {
6876 *ad = SSL_AD_INTERNAL_ERROR;
6877 goto err;
6878 }
6879
6880 if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
6881 user->info) <= 0) {
6882 *ad = SSL_AD_INTERNAL_ERROR;
6883 goto err;
6884 }
6885
6886 ret = 0;
6887
6888 err:
6889 SRP_user_pwd_free(user);
6890 return ret;
6891 }
6892
6893 static int create_new_vfile(char *userid, char *password, const char *filename)
6894 {
6895 char *gNid = NULL;
6896 OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
6897 TXT_DB *db = NULL;
6898 int ret = 0;
6899 BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
6900 size_t i;
6901
6902 if (!TEST_ptr(dummy) || !TEST_ptr(row))
6903 goto end;
6904
6905 gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
6906 &row[DB_srpverifier], NULL, NULL, libctx, NULL);
6907 if (!TEST_ptr(gNid))
6908 goto end;
6909
6910 /*
6911 * The only way to create an empty TXT_DB is to provide a BIO with no data
6912 * in it!
6913 */
6914 db = TXT_DB_read(dummy, DB_NUMBER);
6915 if (!TEST_ptr(db))
6916 goto end;
6917
6918 out = BIO_new_file(filename, "w");
6919 if (!TEST_ptr(out))
6920 goto end;
6921
6922 row[DB_srpid] = OPENSSL_strdup(userid);
6923 row[DB_srptype] = OPENSSL_strdup("V");
6924 row[DB_srpgN] = OPENSSL_strdup(gNid);
6925
6926 if (!TEST_ptr(row[DB_srpid])
6927 || !TEST_ptr(row[DB_srptype])
6928 || !TEST_ptr(row[DB_srpgN])
6929 || !TEST_true(TXT_DB_insert(db, row)))
6930 goto end;
6931
6932 row = NULL;
6933
6934 if (TXT_DB_write(out, db) <= 0)
6935 goto end;
6936
6937 ret = 1;
6938 end:
6939 if (row != NULL) {
6940 for (i = 0; i < DB_NUMBER; i++)
6941 OPENSSL_free(row[i]);
6942 }
6943 OPENSSL_free(row);
6944 BIO_free(dummy);
6945 BIO_free(out);
6946 TXT_DB_free(db);
6947
6948 return ret;
6949 }
6950
6951 static int create_new_vbase(char *userid, char *password)
6952 {
6953 BIGNUM *verifier = NULL, *salt = NULL;
6954 const SRP_gN *lgN = NULL;
6955 SRP_user_pwd *user_pwd = NULL;
6956 int ret = 0;
6957
6958 lgN = SRP_get_default_gN(NULL);
6959 if (!TEST_ptr(lgN))
6960 goto end;
6961
6962 if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
6963 lgN->N, lgN->g, libctx, NULL)))
6964 goto end;
6965
6966 user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
6967 if (!TEST_ptr(user_pwd))
6968 goto end;
6969
6970 user_pwd->N = lgN->N;
6971 user_pwd->g = lgN->g;
6972 user_pwd->id = OPENSSL_strdup(userid);
6973 if (!TEST_ptr(user_pwd->id))
6974 goto end;
6975
6976 user_pwd->v = verifier;
6977 user_pwd->s = salt;
6978 verifier = salt = NULL;
6979
6980 if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
6981 goto end;
6982 user_pwd = NULL;
6983
6984 ret = 1;
6985 end:
6986 SRP_user_pwd_free(user_pwd);
6987 BN_free(salt);
6988 BN_free(verifier);
6989
6990 return ret;
6991 }
6992
6993 /*
6994 * SRP tests
6995 *
6996 * Test 0: Simple successful SRP connection, new vbase
6997 * Test 1: Connection failure due to bad password, new vbase
6998 * Test 2: Simple successful SRP connection, vbase loaded from existing file
6999 * Test 3: Connection failure due to bad password, vbase loaded from existing
7000 * file
7001 * Test 4: Simple successful SRP connection, vbase loaded from new file
7002 * Test 5: Connection failure due to bad password, vbase loaded from new file
7003 */
7004 static int test_srp(int tst)
7005 {
7006 char *userid = "test", *password = "password", *tstsrpfile;
7007 SSL_CTX *cctx = NULL, *sctx = NULL;
7008 SSL *clientssl = NULL, *serverssl = NULL;
7009 int ret, testresult = 0;
7010
7011 vbase = SRP_VBASE_new(NULL);
7012 if (!TEST_ptr(vbase))
7013 goto end;
7014
7015 if (tst == 0 || tst == 1) {
7016 if (!TEST_true(create_new_vbase(userid, password)))
7017 goto end;
7018 } else {
7019 if (tst == 4 || tst == 5) {
7020 if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
7021 goto end;
7022 tstsrpfile = tmpfilename;
7023 } else {
7024 tstsrpfile = srpvfile;
7025 }
7026 if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
7027 goto end;
7028 }
7029
7030 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7031 TLS_client_method(), TLS1_VERSION, 0,
7032 &sctx, &cctx, cert, privkey)))
7033 goto end;
7034
7035 if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
7036 || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
7037 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
7038 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
7039 || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
7040 goto end;
7041
7042 if (tst % 2 == 1) {
7043 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
7044 goto end;
7045 } else {
7046 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
7047 goto end;
7048 }
7049
7050 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7051 NULL, NULL)))
7052 goto end;
7053
7054 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
7055 if (ret) {
7056 if (!TEST_true(tst % 2 == 0))
7057 goto end;
7058 } else {
7059 if (!TEST_true(tst % 2 == 1))
7060 goto end;
7061 }
7062
7063 testresult = 1;
7064
7065 end:
7066 SRP_VBASE_free(vbase);
7067 vbase = NULL;
7068 SSL_free(serverssl);
7069 SSL_free(clientssl);
7070 SSL_CTX_free(sctx);
7071 SSL_CTX_free(cctx);
7072
7073 return testresult;
7074 }
7075 #endif
7076
7077 static int info_cb_failed = 0;
7078 static int info_cb_offset = 0;
7079 static int info_cb_this_state = -1;
7080
7081 static struct info_cb_states_st {
7082 int where;
7083 const char *statestr;
7084 } info_cb_states[][60] = {
7085 {
7086 /* TLSv1.2 server followed by resumption */
7087 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7088 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7089 {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
7090 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
7091 {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
7092 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7093 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7094 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7095 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
7096 {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7097 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
7098 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7099 {SSL_CB_EXIT, NULL}, {0, NULL},
7100 }, {
7101 /* TLSv1.2 client followed by resumption */
7102 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7103 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7104 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
7105 {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
7106 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
7107 {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7108 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7109 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7110 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7111 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7112 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7113 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
7114 }, {
7115 /* TLSv1.3 server followed by resumption */
7116 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7117 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7118 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
7119 {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
7120 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
7121 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7122 {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7123 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7124 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7125 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7126 {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
7127 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7128 {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7129 }, {
7130 /* TLSv1.3 client followed by resumption */
7131 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7132 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7133 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
7134 {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
7135 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7136 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7137 {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
7138 {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
7139 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7140 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
7141 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7142 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7143 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7144 {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
7145 {SSL_CB_EXIT, NULL}, {0, NULL},
7146 }, {
7147 /* TLSv1.3 server, early_data */
7148 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7149 {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7150 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7151 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7152 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7153 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
7154 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7155 {SSL_CB_EXIT, NULL}, {0, NULL},
7156 }, {
7157 /* TLSv1.3 client, early_data */
7158 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7159 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
7160 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7161 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7162 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7163 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
7164 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7165 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7166 {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7167 }, {
7168 {0, NULL},
7169 }
7170 };
7171
7172 static void sslapi_info_callback(const SSL *s, int where, int ret)
7173 {
7174 struct info_cb_states_st *state = info_cb_states[info_cb_offset];
7175
7176 /* We do not ever expect a connection to fail in this test */
7177 if (!TEST_false(ret == 0)) {
7178 info_cb_failed = 1;
7179 return;
7180 }
7181
7182 /*
7183 * Do some sanity checks. We never expect these things to happen in this
7184 * test
7185 */
7186 if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
7187 || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
7188 || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
7189 info_cb_failed = 1;
7190 return;
7191 }
7192
7193 /* Now check we're in the right state */
7194 if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
7195 info_cb_failed = 1;
7196 return;
7197 }
7198 if ((where & SSL_CB_LOOP) != 0
7199 && !TEST_int_eq(strcmp(SSL_state_string(s),
7200 state[info_cb_this_state].statestr), 0)) {
7201 info_cb_failed = 1;
7202 return;
7203 }
7204
7205 /*
7206 * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
7207 */
7208 if ((where & SSL_CB_HANDSHAKE_DONE)
7209 && SSL_in_init((SSL *)s) != 0) {
7210 info_cb_failed = 1;
7211 return;
7212 }
7213 }
7214
7215 /*
7216 * Test the info callback gets called when we expect it to.
7217 *
7218 * Test 0: TLSv1.2, server
7219 * Test 1: TLSv1.2, client
7220 * Test 2: TLSv1.3, server
7221 * Test 3: TLSv1.3, client
7222 * Test 4: TLSv1.3, server, early_data
7223 * Test 5: TLSv1.3, client, early_data
7224 */
7225 static int test_info_callback(int tst)
7226 {
7227 SSL_CTX *cctx = NULL, *sctx = NULL;
7228 SSL *clientssl = NULL, *serverssl = NULL;
7229 SSL_SESSION *clntsess = NULL;
7230 int testresult = 0;
7231 int tlsvers;
7232
7233 if (tst < 2) {
7234 /* We need either ECDHE or DHE for the TLSv1.2 test to work */
7235 #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
7236 || !defined(OPENSSL_NO_DH))
7237 tlsvers = TLS1_2_VERSION;
7238 #else
7239 return 1;
7240 #endif
7241 } else {
7242 #ifndef OSSL_NO_USABLE_TLS1_3
7243 tlsvers = TLS1_3_VERSION;
7244 #else
7245 return 1;
7246 #endif
7247 }
7248
7249 /* Reset globals */
7250 info_cb_failed = 0;
7251 info_cb_this_state = -1;
7252 info_cb_offset = tst;
7253
7254 #ifndef OSSL_NO_USABLE_TLS1_3
7255 if (tst >= 4) {
7256 SSL_SESSION *sess = NULL;
7257 size_t written, readbytes;
7258 unsigned char buf[80];
7259
7260 /* early_data tests */
7261 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
7262 &serverssl, &sess, 0)))
7263 goto end;
7264
7265 /* We don't actually need this reference */
7266 SSL_SESSION_free(sess);
7267
7268 SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
7269 sslapi_info_callback);
7270
7271 /* Write and read some early data and then complete the connection */
7272 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
7273 &written))
7274 || !TEST_size_t_eq(written, strlen(MSG1))
7275 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
7276 sizeof(buf), &readbytes),
7277 SSL_READ_EARLY_DATA_SUCCESS)
7278 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
7279 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
7280 SSL_EARLY_DATA_ACCEPTED)
7281 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7282 SSL_ERROR_NONE))
7283 || !TEST_false(info_cb_failed))
7284 goto end;
7285
7286 testresult = 1;
7287 goto end;
7288 }
7289 #endif
7290
7291 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7292 TLS_client_method(),
7293 tlsvers, tlsvers, &sctx, &cctx, cert,
7294 privkey)))
7295 goto end;
7296
7297 if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
7298 goto end;
7299
7300 /*
7301 * For even numbered tests we check the server callbacks. For odd numbers we
7302 * check the client.
7303 */
7304 SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
7305 sslapi_info_callback);
7306
7307 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
7308 &clientssl, NULL, NULL))
7309 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7310 SSL_ERROR_NONE))
7311 || !TEST_false(info_cb_failed))
7312 goto end;
7313
7314
7315
7316 clntsess = SSL_get1_session(clientssl);
7317 SSL_shutdown(clientssl);
7318 SSL_shutdown(serverssl);
7319 SSL_free(serverssl);
7320 SSL_free(clientssl);
7321 serverssl = clientssl = NULL;
7322
7323 /* Now do a resumption */
7324 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7325 NULL))
7326 || !TEST_true(SSL_set_session(clientssl, clntsess))
7327 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7328 SSL_ERROR_NONE))
7329 || !TEST_true(SSL_session_reused(clientssl))
7330 || !TEST_false(info_cb_failed))
7331 goto end;
7332
7333 testresult = 1;
7334
7335 end:
7336 SSL_free(serverssl);
7337 SSL_free(clientssl);
7338 SSL_SESSION_free(clntsess);
7339 SSL_CTX_free(sctx);
7340 SSL_CTX_free(cctx);
7341 return testresult;
7342 }
7343
7344 static int test_ssl_pending(int tst)
7345 {
7346 SSL_CTX *cctx = NULL, *sctx = NULL;
7347 SSL *clientssl = NULL, *serverssl = NULL;
7348 int testresult = 0;
7349 char msg[] = "A test message";
7350 char buf[5];
7351 size_t written, readbytes;
7352
7353 if (tst == 0) {
7354 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7355 TLS_client_method(),
7356 TLS1_VERSION, 0,
7357 &sctx, &cctx, cert, privkey)))
7358 goto end;
7359 } else {
7360 #ifndef OPENSSL_NO_DTLS
7361 if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
7362 DTLS_client_method(),
7363 DTLS1_VERSION, 0,
7364 &sctx, &cctx, cert, privkey)))
7365 goto end;
7366
7367 # ifdef OPENSSL_NO_DTLS1_2
7368 /* Not supported in the FIPS provider */
7369 if (is_fips) {
7370 testresult = 1;
7371 goto end;
7372 };
7373 /*
7374 * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
7375 * level 0
7376 */
7377 if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
7378 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
7379 "DEFAULT:@SECLEVEL=0")))
7380 goto end;
7381 # endif
7382 #else
7383 return 1;
7384 #endif
7385 }
7386
7387 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7388 NULL, NULL))
7389 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7390 SSL_ERROR_NONE)))
7391 goto end;
7392
7393 if (!TEST_int_eq(SSL_pending(clientssl), 0)
7394 || !TEST_false(SSL_has_pending(clientssl))
7395 || !TEST_int_eq(SSL_pending(serverssl), 0)
7396 || !TEST_false(SSL_has_pending(serverssl))
7397 || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
7398 || !TEST_size_t_eq(written, sizeof(msg))
7399 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
7400 || !TEST_size_t_eq(readbytes, sizeof(buf))
7401 || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
7402 || !TEST_true(SSL_has_pending(clientssl)))
7403 goto end;
7404
7405 testresult = 1;
7406
7407 end:
7408 SSL_free(serverssl);
7409 SSL_free(clientssl);
7410 SSL_CTX_free(sctx);
7411 SSL_CTX_free(cctx);
7412
7413 return testresult;
7414 }
7415
7416 static struct {
7417 unsigned int maxprot;
7418 const char *clntciphers;
7419 const char *clnttls13ciphers;
7420 const char *srvrciphers;
7421 const char *srvrtls13ciphers;
7422 const char *shared;
7423 const char *fipsshared;
7424 } shared_ciphers_data[] = {
7425 /*
7426 * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
7427 * TLSv1.3 is enabled but TLSv1.2 is disabled.
7428 */
7429 #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
7430 {
7431 TLS1_2_VERSION,
7432 "AES128-SHA:AES256-SHA",
7433 NULL,
7434 "AES256-SHA:DHE-RSA-AES128-SHA",
7435 NULL,
7436 "AES256-SHA",
7437 "AES256-SHA"
7438 },
7439 # if !defined(OPENSSL_NO_CHACHA) \
7440 && !defined(OPENSSL_NO_POLY1305) \
7441 && !defined(OPENSSL_NO_EC)
7442 {
7443 TLS1_2_VERSION,
7444 "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7445 NULL,
7446 "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7447 NULL,
7448 "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7449 "AES128-SHA"
7450 },
7451 # endif
7452 {
7453 TLS1_2_VERSION,
7454 "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
7455 NULL,
7456 "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
7457 NULL,
7458 "AES128-SHA:AES256-SHA",
7459 "AES128-SHA:AES256-SHA"
7460 },
7461 {
7462 TLS1_2_VERSION,
7463 "AES128-SHA:AES256-SHA",
7464 NULL,
7465 "AES128-SHA:DHE-RSA-AES128-SHA",
7466 NULL,
7467 "AES128-SHA",
7468 "AES128-SHA"
7469 },
7470 #endif
7471 /*
7472 * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
7473 * enabled.
7474 */
7475 #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
7476 && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
7477 {
7478 TLS1_3_VERSION,
7479 "AES128-SHA:AES256-SHA",
7480 NULL,
7481 "AES256-SHA:AES128-SHA256",
7482 NULL,
7483 "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
7484 "TLS_AES_128_GCM_SHA256:AES256-SHA",
7485 "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
7486 },
7487 #endif
7488 #ifndef OSSL_NO_USABLE_TLS1_3
7489 {
7490 TLS1_3_VERSION,
7491 "AES128-SHA",
7492 "TLS_AES_256_GCM_SHA384",
7493 "AES256-SHA",
7494 "TLS_AES_256_GCM_SHA384",
7495 "TLS_AES_256_GCM_SHA384",
7496 "TLS_AES_256_GCM_SHA384"
7497 },
7498 #endif
7499 };
7500
7501 static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
7502 {
7503 SSL_CTX *cctx = NULL, *sctx = NULL;
7504 SSL *clientssl = NULL, *serverssl = NULL;
7505 int testresult = 0;
7506 char buf[1024];
7507 OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
7508
7509 if (!TEST_ptr(tmplibctx))
7510 goto end;
7511
7512 /*
7513 * Regardless of whether we're testing with the FIPS provider loaded into
7514 * libctx, we want one peer to always use the full set of ciphersuites
7515 * available. Therefore we use a separate libctx with the default provider
7516 * loaded into it. We run the same tests twice - once with the client side
7517 * having the full set of ciphersuites and once with the server side.
7518 */
7519 if (clnt) {
7520 cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
7521 if (!TEST_ptr(cctx))
7522 goto end;
7523 } else {
7524 sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
7525 if (!TEST_ptr(sctx))
7526 goto end;
7527 }
7528
7529 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7530 TLS_client_method(),
7531 TLS1_VERSION,
7532 shared_ciphers_data[tst].maxprot,
7533 &sctx, &cctx, cert, privkey)))
7534 goto end;
7535
7536 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
7537 shared_ciphers_data[tst].clntciphers))
7538 || (shared_ciphers_data[tst].clnttls13ciphers != NULL
7539 && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
7540 shared_ciphers_data[tst].clnttls13ciphers)))
7541 || !TEST_true(SSL_CTX_set_cipher_list(sctx,
7542 shared_ciphers_data[tst].srvrciphers))
7543 || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
7544 && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
7545 shared_ciphers_data[tst].srvrtls13ciphers))))
7546 goto end;
7547
7548
7549 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7550 NULL, NULL))
7551 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7552 SSL_ERROR_NONE)))
7553 goto end;
7554
7555 if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
7556 || !TEST_int_eq(strcmp(buf,
7557 is_fips
7558 ? shared_ciphers_data[tst].fipsshared
7559 : shared_ciphers_data[tst].shared),
7560 0)) {
7561 TEST_info("Shared ciphers are: %s\n", buf);
7562 goto end;
7563 }
7564
7565 testresult = 1;
7566
7567 end:
7568 SSL_free(serverssl);
7569 SSL_free(clientssl);
7570 SSL_CTX_free(sctx);
7571 SSL_CTX_free(cctx);
7572 OSSL_LIB_CTX_free(tmplibctx);
7573
7574 return testresult;
7575 }
7576
7577 static int test_ssl_get_shared_ciphers(int tst)
7578 {
7579 return int_test_ssl_get_shared_ciphers(tst, 0)
7580 && int_test_ssl_get_shared_ciphers(tst, 1);
7581 }
7582
7583
7584 static const char *appdata = "Hello World";
7585 static int gen_tick_called, dec_tick_called, tick_key_cb_called;
7586 static int tick_key_renew = 0;
7587 static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7588
7589 static int gen_tick_cb(SSL *s, void *arg)
7590 {
7591 gen_tick_called = 1;
7592
7593 return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
7594 strlen(appdata));
7595 }
7596
7597 static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
7598 const unsigned char *keyname,
7599 size_t keyname_length,
7600 SSL_TICKET_STATUS status,
7601 void *arg)
7602 {
7603 void *tickdata;
7604 size_t tickdlen;
7605
7606 dec_tick_called = 1;
7607
7608 if (status == SSL_TICKET_EMPTY)
7609 return SSL_TICKET_RETURN_IGNORE_RENEW;
7610
7611 if (!TEST_true(status == SSL_TICKET_SUCCESS
7612 || status == SSL_TICKET_SUCCESS_RENEW))
7613 return SSL_TICKET_RETURN_ABORT;
7614
7615 if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
7616 &tickdlen))
7617 || !TEST_size_t_eq(tickdlen, strlen(appdata))
7618 || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
7619 return SSL_TICKET_RETURN_ABORT;
7620
7621 if (tick_key_cb_called) {
7622 /* Don't change what the ticket key callback wanted to do */
7623 switch (status) {
7624 case SSL_TICKET_NO_DECRYPT:
7625 return SSL_TICKET_RETURN_IGNORE_RENEW;
7626
7627 case SSL_TICKET_SUCCESS:
7628 return SSL_TICKET_RETURN_USE;
7629
7630 case SSL_TICKET_SUCCESS_RENEW:
7631 return SSL_TICKET_RETURN_USE_RENEW;
7632
7633 default:
7634 return SSL_TICKET_RETURN_ABORT;
7635 }
7636 }
7637 return tick_dec_ret;
7638
7639 }
7640
7641 #ifndef OPENSSL_NO_DEPRECATED_3_0
7642 static int tick_key_cb(SSL *s, unsigned char key_name[16],
7643 unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
7644 HMAC_CTX *hctx, int enc)
7645 {
7646 const unsigned char tick_aes_key[16] = "0123456789abcdef";
7647 const unsigned char tick_hmac_key[16] = "0123456789abcdef";
7648 EVP_CIPHER *aes128cbc;
7649 EVP_MD *sha256;
7650 int ret;
7651
7652 tick_key_cb_called = 1;
7653
7654 if (tick_key_renew == -1)
7655 return 0;
7656
7657 aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7658 if (!TEST_ptr(aes128cbc))
7659 return 0;
7660 sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
7661 if (!TEST_ptr(sha256)) {
7662 EVP_CIPHER_free(aes128cbc);
7663 return 0;
7664 }
7665
7666 memset(iv, 0, AES_BLOCK_SIZE);
7667 memset(key_name, 0, 16);
7668 if (aes128cbc == NULL
7669 || sha256 == NULL
7670 || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7671 || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
7672 NULL))
7673 ret = -1;
7674 else
7675 ret = tick_key_renew ? 2 : 1;
7676
7677 EVP_CIPHER_free(aes128cbc);
7678 EVP_MD_free(sha256);
7679
7680 return ret;
7681 }
7682 #endif
7683
7684 static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
7685 unsigned char iv[EVP_MAX_IV_LENGTH],
7686 EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
7687 {
7688 const unsigned char tick_aes_key[16] = "0123456789abcdef";
7689 unsigned char tick_hmac_key[16] = "0123456789abcdef";
7690 OSSL_PARAM params[2];
7691 EVP_CIPHER *aes128cbc;
7692 int ret;
7693
7694 tick_key_cb_called = 1;
7695
7696 if (tick_key_renew == -1)
7697 return 0;
7698
7699 aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7700 if (!TEST_ptr(aes128cbc))
7701 return 0;
7702
7703 memset(iv, 0, AES_BLOCK_SIZE);
7704 memset(key_name, 0, 16);
7705 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
7706 "SHA256", 0);
7707 params[1] = OSSL_PARAM_construct_end();
7708 if (aes128cbc == NULL
7709 || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7710 || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
7711 params))
7712 ret = -1;
7713 else
7714 ret = tick_key_renew ? 2 : 1;
7715
7716 EVP_CIPHER_free(aes128cbc);
7717
7718 return ret;
7719 }
7720
7721 /*
7722 * Test the various ticket callbacks
7723 * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
7724 * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
7725 * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
7726 * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
7727 * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
7728 * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
7729 * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
7730 * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
7731 * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
7732 * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
7733 * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
7734 * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
7735 * Test 12: TLSv1.2, old ticket key callback, no ticket
7736 * Test 13: TLSv1.3, old ticket key callback, no ticket
7737 * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
7738 * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
7739 * Test 16: TLSv1.2, ticket key callback, ticket, renewal
7740 * Test 17: TLSv1.3, ticket key callback, ticket, renewal
7741 * Test 18: TLSv1.2, ticket key callback, no ticket
7742 * Test 19: TLSv1.3, ticket key callback, no ticket
7743 */
7744 static int test_ticket_callbacks(int tst)
7745 {
7746 SSL_CTX *cctx = NULL, *sctx = NULL;
7747 SSL *clientssl = NULL, *serverssl = NULL;
7748 SSL_SESSION *clntsess = NULL;
7749 int testresult = 0;
7750
7751 #ifdef OPENSSL_NO_TLS1_2
7752 if (tst % 2 == 0)
7753 return 1;
7754 #endif
7755 #ifdef OSSL_NO_USABLE_TLS1_3
7756 if (tst % 2 == 1)
7757 return 1;
7758 #endif
7759 #ifdef OPENSSL_NO_DEPRECATED_3_0
7760 if (tst >= 8 && tst <= 13)
7761 return 1;
7762 #endif
7763
7764 gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
7765
7766 /* Which tests the ticket key callback should request renewal for */
7767
7768 if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
7769 tick_key_renew = 1;
7770 else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
7771 tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
7772 else
7773 tick_key_renew = 0;
7774
7775 /* Which tests the decrypt ticket callback should request renewal for */
7776 switch (tst) {
7777 case 0:
7778 case 1:
7779 tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
7780 break;
7781
7782 case 2:
7783 case 3:
7784 tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
7785 break;
7786
7787 case 4:
7788 case 5:
7789 tick_dec_ret = SSL_TICKET_RETURN_USE;
7790 break;
7791
7792 case 6:
7793 case 7:
7794 tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
7795 break;
7796
7797 default:
7798 tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7799 }
7800
7801 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7802 TLS_client_method(),
7803 TLS1_VERSION,
7804 ((tst % 2) == 0) ? TLS1_2_VERSION
7805 : TLS1_3_VERSION,
7806 &sctx, &cctx, cert, privkey)))
7807 goto end;
7808
7809 /*
7810 * We only want sessions to resume from tickets - not the session cache. So
7811 * switch the cache off.
7812 */
7813 if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
7814 goto end;
7815
7816 if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
7817 NULL)))
7818 goto end;
7819
7820 if (tst >= 14) {
7821 if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
7822 goto end;
7823 #ifndef OPENSSL_NO_DEPRECATED_3_0
7824 } else if (tst >= 8) {
7825 if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
7826 goto end;
7827 #endif
7828 }
7829
7830 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7831 NULL, NULL))
7832 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7833 SSL_ERROR_NONE)))
7834 goto end;
7835
7836 /*
7837 * The decrypt ticket key callback in TLSv1.2 should be called even though
7838 * we have no ticket yet, because it gets called with a status of
7839 * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
7840 * actually send any ticket data). This does not happen in TLSv1.3 because
7841 * it is not valid to send empty ticket data in TLSv1.3.
7842 */
7843 if (!TEST_int_eq(gen_tick_called, 1)
7844 || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
7845 goto end;
7846
7847 gen_tick_called = dec_tick_called = 0;
7848
7849 clntsess = SSL_get1_session(clientssl);
7850 SSL_shutdown(clientssl);
7851 SSL_shutdown(serverssl);
7852 SSL_free(serverssl);
7853 SSL_free(clientssl);
7854 serverssl = clientssl = NULL;
7855
7856 /* Now do a resumption */
7857 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7858 NULL))
7859 || !TEST_true(SSL_set_session(clientssl, clntsess))
7860 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7861 SSL_ERROR_NONE)))
7862 goto end;
7863
7864 if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
7865 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
7866 || tick_key_renew == -1) {
7867 if (!TEST_false(SSL_session_reused(clientssl)))
7868 goto end;
7869 } else {
7870 if (!TEST_true(SSL_session_reused(clientssl)))
7871 goto end;
7872 }
7873
7874 if (!TEST_int_eq(gen_tick_called,
7875 (tick_key_renew
7876 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
7877 || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
7878 ? 1 : 0)
7879 /* There is no ticket to decrypt in tests 13 and 19 */
7880 || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
7881 goto end;
7882
7883 testresult = 1;
7884
7885 end:
7886 SSL_SESSION_free(clntsess);
7887 SSL_free(serverssl);
7888 SSL_free(clientssl);
7889 SSL_CTX_free(sctx);
7890 SSL_CTX_free(cctx);
7891
7892 return testresult;
7893 }
7894
7895 /*
7896 * Test incorrect shutdown.
7897 * Test 0: client does not shutdown properly,
7898 * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
7899 * server should get SSL_ERROR_SSL
7900 * Test 1: client does not shutdown properly,
7901 * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
7902 * server should get SSL_ERROR_ZERO_RETURN
7903 */
7904 static int test_incorrect_shutdown(int tst)
7905 {
7906 SSL_CTX *cctx = NULL, *sctx = NULL;
7907 SSL *clientssl = NULL, *serverssl = NULL;
7908 int testresult = 0;
7909 char buf[80];
7910 BIO *c2s;
7911
7912 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7913 TLS_client_method(), 0, 0,
7914 &sctx, &cctx, cert, privkey)))
7915 goto end;
7916
7917 if (tst == 1)
7918 SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
7919
7920 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7921 NULL, NULL)))
7922 goto end;
7923
7924 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7925 SSL_ERROR_NONE)))
7926 goto end;
7927
7928 c2s = SSL_get_rbio(serverssl);
7929 BIO_set_mem_eof_return(c2s, 0);
7930
7931 if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
7932 goto end;
7933
7934 if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
7935 goto end;
7936 if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
7937 goto end;
7938
7939 testresult = 1;
7940
7941 end:
7942 SSL_free(serverssl);
7943 SSL_free(clientssl);
7944 SSL_CTX_free(sctx);
7945 SSL_CTX_free(cctx);
7946
7947 return testresult;
7948 }
7949
7950 /*
7951 * Test bi-directional shutdown.
7952 * Test 0: TLSv1.2
7953 * Test 1: TLSv1.2, server continues to read/write after client shutdown
7954 * Test 2: TLSv1.3, no pending NewSessionTicket messages
7955 * Test 3: TLSv1.3, pending NewSessionTicket messages
7956 * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
7957 * sends key update, client reads it
7958 * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
7959 * sends CertificateRequest, client reads and ignores it
7960 * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
7961 * doesn't read it
7962 */
7963 static int test_shutdown(int tst)
7964 {
7965 SSL_CTX *cctx = NULL, *sctx = NULL;
7966 SSL *clientssl = NULL, *serverssl = NULL;
7967 int testresult = 0;
7968 char msg[] = "A test message";
7969 char buf[80];
7970 size_t written, readbytes;
7971 SSL_SESSION *sess;
7972
7973 #ifdef OPENSSL_NO_TLS1_2
7974 if (tst <= 1)
7975 return 1;
7976 #endif
7977 #ifdef OSSL_NO_USABLE_TLS1_3
7978 if (tst >= 2)
7979 return 1;
7980 #endif
7981
7982 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7983 TLS_client_method(),
7984 TLS1_VERSION,
7985 (tst <= 1) ? TLS1_2_VERSION
7986 : TLS1_3_VERSION,
7987 &sctx, &cctx, cert, privkey)))
7988 goto end;
7989
7990 if (tst == 5)
7991 SSL_CTX_set_post_handshake_auth(cctx, 1);
7992
7993 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7994 NULL, NULL)))
7995 goto end;
7996
7997 if (tst == 3) {
7998 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
7999 SSL_ERROR_NONE, 1, 0))
8000 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8001 || !TEST_false(SSL_SESSION_is_resumable(sess)))
8002 goto end;
8003 } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8004 SSL_ERROR_NONE))
8005 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8006 || !TEST_true(SSL_SESSION_is_resumable(sess))) {
8007 goto end;
8008 }
8009
8010 if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
8011 goto end;
8012
8013 if (tst >= 4) {
8014 /*
8015 * Reading on the server after the client has sent close_notify should
8016 * fail and provide SSL_ERROR_ZERO_RETURN
8017 */
8018 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
8019 || !TEST_int_eq(SSL_get_error(serverssl, 0),
8020 SSL_ERROR_ZERO_RETURN)
8021 || !TEST_int_eq(SSL_get_shutdown(serverssl),
8022 SSL_RECEIVED_SHUTDOWN)
8023 /*
8024 * Even though we're shutdown on receive we should still be
8025 * able to write.
8026 */
8027 || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8028 goto end;
8029 if (tst == 4
8030 && !TEST_true(SSL_key_update(serverssl,
8031 SSL_KEY_UPDATE_REQUESTED)))
8032 goto end;
8033 if (tst == 5) {
8034 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
8035 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
8036 goto end;
8037 }
8038 if ((tst == 4 || tst == 5)
8039 && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8040 goto end;
8041 if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
8042 goto end;
8043 if (tst == 4 || tst == 5) {
8044 /* Should still be able to read data from server */
8045 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8046 &readbytes))
8047 || !TEST_size_t_eq(readbytes, sizeof(msg))
8048 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
8049 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8050 &readbytes))
8051 || !TEST_size_t_eq(readbytes, sizeof(msg))
8052 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
8053 goto end;
8054 }
8055 }
8056
8057 /* Writing on the client after sending close_notify shouldn't be possible */
8058 if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
8059 goto end;
8060
8061 if (tst < 4) {
8062 /*
8063 * For these tests the client has sent close_notify but it has not yet
8064 * been received by the server. The server has not sent close_notify
8065 * yet.
8066 */
8067 if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
8068 /*
8069 * Writing on the server after sending close_notify shouldn't
8070 * be possible.
8071 */
8072 || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8073 || !TEST_int_eq(SSL_shutdown(clientssl), 1)
8074 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8075 || !TEST_true(SSL_SESSION_is_resumable(sess))
8076 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
8077 goto end;
8078 } else if (tst == 4 || tst == 5) {
8079 /*
8080 * In this test the client has sent close_notify and it has been
8081 * received by the server which has responded with a close_notify. The
8082 * client needs to read the close_notify sent by the server.
8083 */
8084 if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
8085 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8086 || !TEST_true(SSL_SESSION_is_resumable(sess)))
8087 goto end;
8088 } else {
8089 /*
8090 * tst == 6
8091 *
8092 * The client has sent close_notify and is expecting a close_notify
8093 * back, but instead there is application data first. The shutdown
8094 * should fail with a fatal error.
8095 */
8096 if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
8097 || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
8098 goto end;
8099 }
8100
8101 testresult = 1;
8102
8103 end:
8104 SSL_free(serverssl);
8105 SSL_free(clientssl);
8106 SSL_CTX_free(sctx);
8107 SSL_CTX_free(cctx);
8108
8109 return testresult;
8110 }
8111
8112 #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8113 static int cert_cb_cnt;
8114
8115 static int cert_cb(SSL *s, void *arg)
8116 {
8117 SSL_CTX *ctx = (SSL_CTX *)arg;
8118 BIO *in = NULL;
8119 EVP_PKEY *pkey = NULL;
8120 X509 *x509 = NULL, *rootx = NULL;
8121 STACK_OF(X509) *chain = NULL;
8122 char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
8123 int ret = 0;
8124
8125 if (cert_cb_cnt == 0) {
8126 /* Suspend the handshake */
8127 cert_cb_cnt++;
8128 return -1;
8129 } else if (cert_cb_cnt == 1) {
8130 /*
8131 * Update the SSL_CTX, set the certificate and private key and then
8132 * continue the handshake normally.
8133 */
8134 if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
8135 return 0;
8136
8137 if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
8138 || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
8139 SSL_FILETYPE_PEM))
8140 || !TEST_true(SSL_check_private_key(s)))
8141 return 0;
8142 cert_cb_cnt++;
8143 return 1;
8144 } else if (cert_cb_cnt == 3) {
8145 int rv;
8146
8147 rootfile = test_mk_file_path(certsdir, "rootcert.pem");
8148 ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
8149 ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
8150 if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
8151 goto out;
8152 chain = sk_X509_new_null();
8153 if (!TEST_ptr(chain))
8154 goto out;
8155 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8156 || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
8157 || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
8158 || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
8159 || !TEST_true(sk_X509_push(chain, rootx)))
8160 goto out;
8161 rootx = NULL;
8162 BIO_free(in);
8163 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8164 || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
8165 || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
8166 || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
8167 goto out;
8168 BIO_free(in);
8169 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8170 || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
8171 || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
8172 NULL, NULL,
8173 libctx, NULL)))
8174 goto out;
8175 rv = SSL_check_chain(s, x509, pkey, chain);
8176 /*
8177 * If the cert doesn't show as valid here (e.g., because we don't
8178 * have any shared sigalgs), then we will not set it, and there will
8179 * be no certificate at all on the SSL or SSL_CTX. This, in turn,
8180 * will cause tls_choose_sigalgs() to fail the connection.
8181 */
8182 if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
8183 == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
8184 if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
8185 goto out;
8186 }
8187
8188 ret = 1;
8189 }
8190
8191 /* Abort the handshake */
8192 out:
8193 OPENSSL_free(ecdsacert);
8194 OPENSSL_free(ecdsakey);
8195 OPENSSL_free(rootfile);
8196 BIO_free(in);
8197 EVP_PKEY_free(pkey);
8198 X509_free(x509);
8199 X509_free(rootx);
8200 OSSL_STACK_OF_X509_free(chain);
8201 return ret;
8202 }
8203
8204 /*
8205 * Test the certificate callback.
8206 * Test 0: Callback fails
8207 * Test 1: Success - no SSL_set_SSL_CTX() in the callback
8208 * Test 2: Success - SSL_set_SSL_CTX() in the callback
8209 * Test 3: Success - Call SSL_check_chain from the callback
8210 * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
8211 * chain
8212 * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
8213 */
8214 static int test_cert_cb_int(int prot, int tst)
8215 {
8216 SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
8217 SSL *clientssl = NULL, *serverssl = NULL;
8218 int testresult = 0, ret;
8219
8220 #ifdef OPENSSL_NO_EC
8221 /* We use an EC cert in these tests, so we skip in a no-ec build */
8222 if (tst >= 3)
8223 return 1;
8224 #endif
8225
8226 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8227 TLS_client_method(),
8228 TLS1_VERSION,
8229 prot,
8230 &sctx, &cctx, NULL, NULL)))
8231 goto end;
8232
8233 if (tst == 0)
8234 cert_cb_cnt = -1;
8235 else if (tst >= 3)
8236 cert_cb_cnt = 3;
8237 else
8238 cert_cb_cnt = 0;
8239
8240 if (tst == 2) {
8241 snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
8242 if (!TEST_ptr(snictx))
8243 goto end;
8244 }
8245
8246 SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
8247
8248 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8249 NULL, NULL)))
8250 goto end;
8251
8252 if (tst == 4) {
8253 /*
8254 * We cause SSL_check_chain() to fail by specifying sig_algs that
8255 * the chain doesn't meet (the root uses an RSA cert)
8256 */
8257 if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8258 "ecdsa_secp256r1_sha256")))
8259 goto end;
8260 } else if (tst == 5) {
8261 /*
8262 * We cause SSL_check_chain() to fail by specifying sig_algs that
8263 * the ee cert doesn't meet (the ee uses an ECDSA cert)
8264 */
8265 if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8266 "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
8267 goto end;
8268 }
8269
8270 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
8271 if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
8272 || (tst > 0
8273 && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
8274 goto end;
8275 }
8276
8277 testresult = 1;
8278
8279 end:
8280 SSL_free(serverssl);
8281 SSL_free(clientssl);
8282 SSL_CTX_free(sctx);
8283 SSL_CTX_free(cctx);
8284 SSL_CTX_free(snictx);
8285
8286 return testresult;
8287 }
8288 #endif
8289
8290 static int test_cert_cb(int tst)
8291 {
8292 int testresult = 1;
8293
8294 #ifndef OPENSSL_NO_TLS1_2
8295 testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
8296 #endif
8297 #ifndef OSSL_NO_USABLE_TLS1_3
8298 testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
8299 #endif
8300
8301 return testresult;
8302 }
8303
8304 static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
8305 {
8306 X509 *xcert;
8307 EVP_PKEY *privpkey;
8308 BIO *in = NULL;
8309 BIO *priv_in = NULL;
8310
8311 /* Check that SSL_get0_peer_certificate() returns something sensible */
8312 if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
8313 return 0;
8314
8315 in = BIO_new_file(cert, "r");
8316 if (!TEST_ptr(in))
8317 return 0;
8318
8319 if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
8320 || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
8321 || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
8322 || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
8323 NULL, NULL,
8324 libctx, NULL)))
8325 goto err;
8326
8327 *x509 = xcert;
8328 *pkey = privpkey;
8329
8330 BIO_free(in);
8331 BIO_free(priv_in);
8332 return 1;
8333 err:
8334 X509_free(xcert);
8335 BIO_free(in);
8336 BIO_free(priv_in);
8337 return 0;
8338 }
8339
8340 static int test_client_cert_cb(int tst)
8341 {
8342 SSL_CTX *cctx = NULL, *sctx = NULL;
8343 SSL *clientssl = NULL, *serverssl = NULL;
8344 int testresult = 0;
8345
8346 #ifdef OPENSSL_NO_TLS1_2
8347 if (tst == 0)
8348 return 1;
8349 #endif
8350 #ifdef OSSL_NO_USABLE_TLS1_3
8351 if (tst == 1)
8352 return 1;
8353 #endif
8354
8355 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8356 TLS_client_method(),
8357 TLS1_VERSION,
8358 tst == 0 ? TLS1_2_VERSION
8359 : TLS1_3_VERSION,
8360 &sctx, &cctx, cert, privkey)))
8361 goto end;
8362
8363 /*
8364 * Test that setting a client_cert_cb results in a client certificate being
8365 * sent.
8366 */
8367 SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
8368 SSL_CTX_set_verify(sctx,
8369 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
8370 verify_cb);
8371
8372 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8373 NULL, NULL))
8374 || !TEST_true(create_ssl_connection(serverssl, clientssl,
8375 SSL_ERROR_NONE)))
8376 goto end;
8377
8378 testresult = 1;
8379
8380 end:
8381 SSL_free(serverssl);
8382 SSL_free(clientssl);
8383 SSL_CTX_free(sctx);
8384 SSL_CTX_free(cctx);
8385
8386 return testresult;
8387 }
8388
8389 #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8390 /*
8391 * Test setting certificate authorities on both client and server.
8392 *
8393 * Test 0: SSL_CTX_set0_CA_list() only
8394 * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
8395 * Test 2: Only SSL_CTX_set_client_CA_list()
8396 */
8397 static int test_ca_names_int(int prot, int tst)
8398 {
8399 SSL_CTX *cctx = NULL, *sctx = NULL;
8400 SSL *clientssl = NULL, *serverssl = NULL;
8401 int testresult = 0;
8402 size_t i;
8403 X509_NAME *name[] = { NULL, NULL, NULL, NULL };
8404 char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
8405 STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
8406 const STACK_OF(X509_NAME) *sktmp = NULL;
8407
8408 for (i = 0; i < OSSL_NELEM(name); i++) {
8409 name[i] = X509_NAME_new();
8410 if (!TEST_ptr(name[i])
8411 || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
8412 MBSTRING_ASC,
8413 (unsigned char *)
8414 strnames[i],
8415 -1, -1, 0)))
8416 goto end;
8417 }
8418
8419 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8420 TLS_client_method(),
8421 TLS1_VERSION,
8422 prot,
8423 &sctx, &cctx, cert, privkey)))
8424 goto end;
8425
8426 SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
8427
8428 if (tst == 0 || tst == 1) {
8429 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8430 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
8431 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
8432 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8433 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
8434 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
8435 goto end;
8436
8437 SSL_CTX_set0_CA_list(sctx, sk1);
8438 SSL_CTX_set0_CA_list(cctx, sk2);
8439 sk1 = sk2 = NULL;
8440 }
8441 if (tst == 1 || tst == 2) {
8442 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8443 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
8444 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
8445 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8446 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
8447 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
8448 goto end;
8449
8450 SSL_CTX_set_client_CA_list(sctx, sk1);
8451 SSL_CTX_set_client_CA_list(cctx, sk2);
8452 sk1 = sk2 = NULL;
8453 }
8454
8455 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8456 NULL, NULL))
8457 || !TEST_true(create_ssl_connection(serverssl, clientssl,
8458 SSL_ERROR_NONE)))
8459 goto end;
8460
8461 /*
8462 * We only expect certificate authorities to have been sent to the server
8463 * if we are using TLSv1.3 and SSL_set0_CA_list() was used
8464 */
8465 sktmp = SSL_get0_peer_CA_list(serverssl);
8466 if (prot == TLS1_3_VERSION
8467 && (tst == 0 || tst == 1)) {
8468 if (!TEST_ptr(sktmp)
8469 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8470 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8471 name[0]), 0)
8472 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8473 name[1]), 0))
8474 goto end;
8475 } else if (!TEST_ptr_null(sktmp)) {
8476 goto end;
8477 }
8478
8479 /*
8480 * In all tests we expect certificate authorities to have been sent to the
8481 * client. However, SSL_set_client_CA_list() should override
8482 * SSL_set0_CA_list()
8483 */
8484 sktmp = SSL_get0_peer_CA_list(clientssl);
8485 if (!TEST_ptr(sktmp)
8486 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8487 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8488 name[tst == 0 ? 0 : 2]), 0)
8489 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8490 name[tst == 0 ? 1 : 3]), 0))
8491 goto end;
8492
8493 testresult = 1;
8494
8495 end:
8496 SSL_free(serverssl);
8497 SSL_free(clientssl);
8498 SSL_CTX_free(sctx);
8499 SSL_CTX_free(cctx);
8500 for (i = 0; i < OSSL_NELEM(name); i++)
8501 X509_NAME_free(name[i]);
8502 sk_X509_NAME_pop_free(sk1, X509_NAME_free);
8503 sk_X509_NAME_pop_free(sk2, X509_NAME_free);
8504
8505 return testresult;
8506 }
8507 #endif
8508
8509 static int test_ca_names(int tst)
8510 {
8511 int testresult = 1;
8512
8513 #ifndef OPENSSL_NO_TLS1_2
8514 testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
8515 #endif
8516 #ifndef OSSL_NO_USABLE_TLS1_3
8517 testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
8518 #endif
8519
8520 return testresult;
8521 }
8522
8523 #ifndef OPENSSL_NO_TLS1_2
8524 static const char *multiblock_cipherlist_data[]=
8525 {
8526 "AES128-SHA",
8527 "AES128-SHA256",
8528 "AES256-SHA",
8529 "AES256-SHA256",
8530 };
8531
8532 /* Reduce the fragment size - so the multiblock test buffer can be small */
8533 # define MULTIBLOCK_FRAGSIZE 512
8534
8535 static int test_multiblock_write(int test_index)
8536 {
8537 static const char *fetchable_ciphers[]=
8538 {
8539 "AES-128-CBC-HMAC-SHA1",
8540 "AES-128-CBC-HMAC-SHA256",
8541 "AES-256-CBC-HMAC-SHA1",
8542 "AES-256-CBC-HMAC-SHA256"
8543 };
8544 const char *cipherlist = multiblock_cipherlist_data[test_index];
8545 const SSL_METHOD *smeth = TLS_server_method();
8546 const SSL_METHOD *cmeth = TLS_client_method();
8547 int min_version = TLS1_VERSION;
8548 int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
8549 SSL_CTX *cctx = NULL, *sctx = NULL;
8550 SSL *clientssl = NULL, *serverssl = NULL;
8551 int testresult = 0;
8552
8553 /*
8554 * Choose a buffer large enough to perform a multi-block operation
8555 * i.e: write_len >= 4 * frag_size
8556 * 9 * is chosen so that multiple multiblocks are used + some leftover.
8557 */
8558 unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
8559 unsigned char buf[sizeof(msg)], *p = buf;
8560 size_t readbytes, written, len;
8561 EVP_CIPHER *ciph = NULL;
8562
8563 /*
8564 * Check if the cipher exists before attempting to use it since it only has
8565 * a hardware specific implementation.
8566 */
8567 ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
8568 if (ciph == NULL) {
8569 TEST_skip("Multiblock cipher is not available for %s", cipherlist);
8570 return 1;
8571 }
8572 EVP_CIPHER_free(ciph);
8573
8574 /* Set up a buffer with some data that will be sent to the client */
8575 RAND_bytes(msg, sizeof(msg));
8576
8577 if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
8578 max_version, &sctx, &cctx, cert,
8579 privkey)))
8580 goto end;
8581
8582 if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
8583 goto end;
8584
8585 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8586 NULL, NULL)))
8587 goto end;
8588
8589 /* settings to force it to use AES-CBC-HMAC_SHA */
8590 SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
8591 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
8592 goto end;
8593
8594 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8595 goto end;
8596
8597 if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8598 || !TEST_size_t_eq(written, sizeof(msg)))
8599 goto end;
8600
8601 len = written;
8602 while (len > 0) {
8603 if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
8604 goto end;
8605 p += readbytes;
8606 len -= readbytes;
8607 }
8608 if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
8609 goto end;
8610
8611 testresult = 1;
8612 end:
8613 SSL_free(serverssl);
8614 SSL_free(clientssl);
8615 SSL_CTX_free(sctx);
8616 SSL_CTX_free(cctx);
8617
8618 return testresult;
8619 }
8620 #endif /* OPENSSL_NO_TLS1_2 */
8621
8622 static int test_session_timeout(int test)
8623 {
8624 /*
8625 * Test session ordering and timeout
8626 * Can't explicitly test performance of the new code,
8627 * but can test to see if the ordering of the sessions
8628 * are correct, and they they are removed as expected
8629 */
8630 SSL_SESSION *early = NULL;
8631 SSL_SESSION *middle = NULL;
8632 SSL_SESSION *late = NULL;
8633 SSL_CTX *ctx;
8634 int testresult = 0;
8635 long now = (long)time(NULL);
8636 #define TIMEOUT 10
8637
8638 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
8639 || !TEST_ptr(early = SSL_SESSION_new())
8640 || !TEST_ptr(middle = SSL_SESSION_new())
8641 || !TEST_ptr(late = SSL_SESSION_new()))
8642 goto end;
8643
8644 /* assign unique session ids */
8645 early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8646 memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
8647 middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8648 memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
8649 late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8650 memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
8651
8652 if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8653 || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8654 || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8655 goto end;
8656
8657 /* Make sure they are all added */
8658 if (!TEST_ptr(early->prev)
8659 || !TEST_ptr(middle->prev)
8660 || !TEST_ptr(late->prev))
8661 goto end;
8662
8663 if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
8664 || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
8665 || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
8666 goto end;
8667
8668 if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
8669 || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
8670 || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
8671 goto end;
8672
8673 /* Make sure they are all still there */
8674 if (!TEST_ptr(early->prev)
8675 || !TEST_ptr(middle->prev)
8676 || !TEST_ptr(late->prev))
8677 goto end;
8678
8679 /* Make sure they are in the expected order */
8680 if (!TEST_ptr_eq(late->next, middle)
8681 || !TEST_ptr_eq(middle->next, early)
8682 || !TEST_ptr_eq(early->prev, middle)
8683 || !TEST_ptr_eq(middle->prev, late))
8684 goto end;
8685
8686 /* This should remove "early" */
8687 SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
8688 if (!TEST_ptr_null(early->prev)
8689 || !TEST_ptr(middle->prev)
8690 || !TEST_ptr(late->prev))
8691 goto end;
8692
8693 /* This should remove "middle" */
8694 SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
8695 if (!TEST_ptr_null(early->prev)
8696 || !TEST_ptr_null(middle->prev)
8697 || !TEST_ptr(late->prev))
8698 goto end;
8699
8700 /* This should remove "late" */
8701 SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
8702 if (!TEST_ptr_null(early->prev)
8703 || !TEST_ptr_null(middle->prev)
8704 || !TEST_ptr_null(late->prev))
8705 goto end;
8706
8707 /* Add them back in again */
8708 if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8709 || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8710 || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8711 goto end;
8712
8713 /* Make sure they are all added */
8714 if (!TEST_ptr(early->prev)
8715 || !TEST_ptr(middle->prev)
8716 || !TEST_ptr(late->prev))
8717 goto end;
8718
8719 /* This should remove all of them */
8720 SSL_CTX_flush_sessions(ctx, 0);
8721 if (!TEST_ptr_null(early->prev)
8722 || !TEST_ptr_null(middle->prev)
8723 || !TEST_ptr_null(late->prev))
8724 goto end;
8725
8726 (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
8727 | SSL_CTX_get_session_cache_mode(ctx));
8728
8729 /* make sure |now| is NOT equal to the current time */
8730 now -= 10;
8731 if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
8732 || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8733 || !TEST_long_ne(SSL_SESSION_get_time(early), now))
8734 goto end;
8735
8736 testresult = 1;
8737 end:
8738 SSL_CTX_free(ctx);
8739 SSL_SESSION_free(early);
8740 SSL_SESSION_free(middle);
8741 SSL_SESSION_free(late);
8742 return testresult;
8743 }
8744
8745 /*
8746 * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
8747 * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
8748 * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
8749 * Test 3: Client does not set servername on initial handshake (TLSv1.2)
8750 * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
8751 * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
8752 * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
8753 * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
8754 * Test 8: Client does not set servername on initial handshake(TLSv1.3)
8755 * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
8756 */
8757 static int test_servername(int tst)
8758 {
8759 SSL_CTX *cctx = NULL, *sctx = NULL;
8760 SSL *clientssl = NULL, *serverssl = NULL;
8761 int testresult = 0;
8762 SSL_SESSION *sess = NULL;
8763 const char *sexpectedhost = NULL, *cexpectedhost = NULL;
8764
8765 #ifdef OPENSSL_NO_TLS1_2
8766 if (tst <= 4)
8767 return 1;
8768 #endif
8769 #ifdef OSSL_NO_USABLE_TLS1_3
8770 if (tst >= 5)
8771 return 1;
8772 #endif
8773
8774 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8775 TLS_client_method(),
8776 TLS1_VERSION,
8777 (tst <= 4) ? TLS1_2_VERSION
8778 : TLS1_3_VERSION,
8779 &sctx, &cctx, cert, privkey))
8780 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8781 NULL, NULL)))
8782 goto end;
8783
8784 if (tst != 1 && tst != 6) {
8785 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
8786 hostname_cb)))
8787 goto end;
8788 }
8789
8790 if (tst != 3 && tst != 8) {
8791 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
8792 goto end;
8793 sexpectedhost = cexpectedhost = "goodhost";
8794 }
8795
8796 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8797 goto end;
8798
8799 if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
8800 cexpectedhost)
8801 || !TEST_str_eq(SSL_get_servername(serverssl,
8802 TLSEXT_NAMETYPE_host_name),
8803 sexpectedhost))
8804 goto end;
8805
8806 /* Now repeat with a resumption handshake */
8807
8808 if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
8809 || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
8810 || !TEST_true(SSL_SESSION_is_resumable(sess))
8811 || !TEST_int_eq(SSL_shutdown(serverssl), 0))
8812 goto end;
8813
8814 SSL_free(clientssl);
8815 SSL_free(serverssl);
8816 clientssl = serverssl = NULL;
8817
8818 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
8819 NULL)))
8820 goto end;
8821
8822 if (!TEST_true(SSL_set_session(clientssl, sess)))
8823 goto end;
8824
8825 sexpectedhost = cexpectedhost = "goodhost";
8826 if (tst == 2 || tst == 7) {
8827 /* Set an inconsistent hostname */
8828 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
8829 goto end;
8830 /*
8831 * In TLSv1.2 we expect the hostname from the original handshake, in
8832 * TLSv1.3 we expect the hostname from this handshake
8833 */
8834 if (tst == 7)
8835 sexpectedhost = cexpectedhost = "altgoodhost";
8836
8837 if (!TEST_str_eq(SSL_get_servername(clientssl,
8838 TLSEXT_NAMETYPE_host_name),
8839 "altgoodhost"))
8840 goto end;
8841 } else if (tst == 4 || tst == 9) {
8842 /*
8843 * A TLSv1.3 session does not associate a session with a servername,
8844 * but a TLSv1.2 session does.
8845 */
8846 if (tst == 9)
8847 sexpectedhost = cexpectedhost = NULL;
8848
8849 if (!TEST_str_eq(SSL_get_servername(clientssl,
8850 TLSEXT_NAMETYPE_host_name),
8851 cexpectedhost))
8852 goto end;
8853 } else {
8854 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
8855 goto end;
8856 /*
8857 * In a TLSv1.2 resumption where the hostname was not acknowledged
8858 * we expect the hostname on the server to be empty. On the client we
8859 * return what was requested in this case.
8860 *
8861 * Similarly if the client didn't set a hostname on an original TLSv1.2
8862 * session but is now, the server hostname will be empty, but the client
8863 * is as we set it.
8864 */
8865 if (tst == 1 || tst == 3)
8866 sexpectedhost = NULL;
8867
8868 if (!TEST_str_eq(SSL_get_servername(clientssl,
8869 TLSEXT_NAMETYPE_host_name),
8870 "goodhost"))
8871 goto end;
8872 }
8873
8874 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8875 goto end;
8876
8877 if (!TEST_true(SSL_session_reused(clientssl))
8878 || !TEST_true(SSL_session_reused(serverssl))
8879 || !TEST_str_eq(SSL_get_servername(clientssl,
8880 TLSEXT_NAMETYPE_host_name),
8881 cexpectedhost)
8882 || !TEST_str_eq(SSL_get_servername(serverssl,
8883 TLSEXT_NAMETYPE_host_name),
8884 sexpectedhost))
8885 goto end;
8886
8887 testresult = 1;
8888
8889 end:
8890 SSL_SESSION_free(sess);
8891 SSL_free(serverssl);
8892 SSL_free(clientssl);
8893 SSL_CTX_free(sctx);
8894 SSL_CTX_free(cctx);
8895
8896 return testresult;
8897 }
8898
8899 #if !defined(OPENSSL_NO_EC) \
8900 && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
8901 /*
8902 * Test that if signature algorithms are not available, then we do not offer or
8903 * accept them.
8904 * Test 0: Two RSA sig algs available: both RSA sig algs shared
8905 * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
8906 * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
8907 * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
8908 * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
8909 * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
8910 */
8911 static int test_sigalgs_available(int idx)
8912 {
8913 SSL_CTX *cctx = NULL, *sctx = NULL;
8914 SSL *clientssl = NULL, *serverssl = NULL;
8915 int testresult = 0;
8916 OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
8917 OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
8918 OSSL_PROVIDER *filterprov = NULL;
8919 int sig, hash;
8920
8921 if (!TEST_ptr(tmpctx))
8922 goto end;
8923
8924 if (idx != 0 && idx != 3) {
8925 if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
8926 filter_provider_init)))
8927 goto end;
8928
8929 filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
8930 if (!TEST_ptr(filterprov))
8931 goto end;
8932
8933 if (idx < 3) {
8934 /*
8935 * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
8936 * or accepted for the peer that uses this libctx. Note that libssl
8937 * *requires* SHA2-256 to be available so we cannot disable that. We
8938 * also need SHA1 for our certificate.
8939 */
8940 if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
8941 "SHA2-256:SHA1")))
8942 goto end;
8943 } else {
8944 if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
8945 "ECDSA"))
8946 || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
8947 "EC:X25519:X448")))
8948 goto end;
8949 }
8950
8951 if (idx == 1 || idx == 4)
8952 clientctx = tmpctx;
8953 else
8954 serverctx = tmpctx;
8955 }
8956
8957 cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
8958 sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
8959 if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
8960 goto end;
8961
8962 if (idx != 5) {
8963 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8964 TLS_client_method(),
8965 TLS1_VERSION,
8966 0,
8967 &sctx, &cctx, cert, privkey)))
8968 goto end;
8969 } else {
8970 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8971 TLS_client_method(),
8972 TLS1_VERSION,
8973 0,
8974 &sctx, &cctx, cert2, privkey2)))
8975 goto end;
8976 }
8977
8978 /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
8979 if (idx < 4) {
8980 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
8981 "ECDHE-RSA-AES128-GCM-SHA256")))
8982 goto end;
8983 } else {
8984 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
8985 "ECDHE-ECDSA-AES128-GCM-SHA256")))
8986 goto end;
8987 }
8988
8989 if (idx < 3) {
8990 if (!SSL_CTX_set1_sigalgs_list(cctx,
8991 "rsa_pss_rsae_sha384"
8992 ":rsa_pss_rsae_sha256")
8993 || !SSL_CTX_set1_sigalgs_list(sctx,
8994 "rsa_pss_rsae_sha384"
8995 ":rsa_pss_rsae_sha256"))
8996 goto end;
8997 } else {
8998 if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
8999 || !SSL_CTX_set1_sigalgs_list(sctx,
9000 "rsa_pss_rsae_sha256:ECDSA+SHA256"))
9001 goto end;
9002 }
9003
9004 if (idx != 5
9005 && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
9006 SSL_FILETYPE_PEM), 1)
9007 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
9008 privkey2,
9009 SSL_FILETYPE_PEM), 1)
9010 || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
9011 goto end;
9012
9013 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9014 NULL, NULL)))
9015 goto end;
9016
9017 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9018 goto end;
9019
9020 /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
9021 if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
9022 NULL, NULL),
9023 (idx == 0 || idx == 3) ? 2 : 1))
9024 goto end;
9025
9026 if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
9027 goto end;
9028
9029 if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
9030 : NID_rsassaPss))
9031 goto end;
9032
9033 testresult = filter_provider_check_clean_finish();
9034
9035 end:
9036 SSL_free(serverssl);
9037 SSL_free(clientssl);
9038 SSL_CTX_free(sctx);
9039 SSL_CTX_free(cctx);
9040 OSSL_PROVIDER_unload(filterprov);
9041 OSSL_LIB_CTX_free(tmpctx);
9042
9043 return testresult;
9044 }
9045 #endif /*
9046 * !defined(OPENSSL_NO_EC) \
9047 * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9048 */
9049
9050 #ifndef OPENSSL_NO_TLS1_3
9051 /* This test can run in TLSv1.3 even if ec and dh are disabled */
9052 static int test_pluggable_group(int idx)
9053 {
9054 SSL_CTX *cctx = NULL, *sctx = NULL;
9055 SSL *clientssl = NULL, *serverssl = NULL;
9056 int testresult = 0;
9057 OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
9058 /* Check that we are not impacted by a provider without any groups */
9059 OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
9060 const char *group_name = idx == 0 ? "xorgroup" : "xorkemgroup";
9061
9062 if (!TEST_ptr(tlsprov))
9063 goto end;
9064
9065 if (legacyprov == NULL) {
9066 /*
9067 * In this case we assume we've been built with "no-legacy" and skip
9068 * this test (there is no OPENSSL_NO_LEGACY)
9069 */
9070 testresult = 1;
9071 goto end;
9072 }
9073
9074 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9075 TLS_client_method(),
9076 TLS1_3_VERSION,
9077 TLS1_3_VERSION,
9078 &sctx, &cctx, cert, privkey))
9079 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9080 NULL, NULL)))
9081 goto end;
9082
9083 if (!TEST_true(SSL_set1_groups_list(serverssl, group_name))
9084 || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
9085 goto end;
9086
9087 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9088 goto end;
9089
9090 if (!TEST_str_eq(group_name,
9091 SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
9092 goto end;
9093
9094 testresult = 1;
9095
9096 end:
9097 SSL_free(serverssl);
9098 SSL_free(clientssl);
9099 SSL_CTX_free(sctx);
9100 SSL_CTX_free(cctx);
9101 OSSL_PROVIDER_unload(tlsprov);
9102 OSSL_PROVIDER_unload(legacyprov);
9103
9104 return testresult;
9105 }
9106 #endif
9107
9108 #ifndef OPENSSL_NO_TLS1_2
9109 static int test_ssl_dup(void)
9110 {
9111 SSL_CTX *cctx = NULL, *sctx = NULL;
9112 SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
9113 int testresult = 0;
9114 BIO *rbio = NULL, *wbio = NULL;
9115
9116 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9117 TLS_client_method(),
9118 0,
9119 0,
9120 &sctx, &cctx, cert, privkey)))
9121 goto end;
9122
9123 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9124 NULL, NULL)))
9125 goto end;
9126
9127 if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
9128 || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
9129 goto end;
9130
9131 client2ssl = SSL_dup(clientssl);
9132 rbio = SSL_get_rbio(clientssl);
9133 if (!TEST_ptr(rbio)
9134 || !TEST_true(BIO_up_ref(rbio)))
9135 goto end;
9136 SSL_set0_rbio(client2ssl, rbio);
9137 rbio = NULL;
9138
9139 wbio = SSL_get_wbio(clientssl);
9140 if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
9141 goto end;
9142 SSL_set0_wbio(client2ssl, wbio);
9143 rbio = NULL;
9144
9145 if (!TEST_ptr(client2ssl)
9146 /* Handshake not started so pointers should be different */
9147 || !TEST_ptr_ne(clientssl, client2ssl))
9148 goto end;
9149
9150 if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
9151 || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
9152 goto end;
9153
9154 if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
9155 goto end;
9156
9157 SSL_free(clientssl);
9158 clientssl = SSL_dup(client2ssl);
9159 if (!TEST_ptr(clientssl)
9160 /* Handshake has finished so pointers should be the same */
9161 || !TEST_ptr_eq(clientssl, client2ssl))
9162 goto end;
9163
9164 testresult = 1;
9165
9166 end:
9167 SSL_free(serverssl);
9168 SSL_free(clientssl);
9169 SSL_free(client2ssl);
9170 SSL_CTX_free(sctx);
9171 SSL_CTX_free(cctx);
9172
9173 return testresult;
9174 }
9175
9176 # ifndef OPENSSL_NO_DH
9177
9178 static EVP_PKEY *tmp_dh_params = NULL;
9179
9180 /* Helper function for the test_set_tmp_dh() tests */
9181 static EVP_PKEY *get_tmp_dh_params(void)
9182 {
9183 if (tmp_dh_params == NULL) {
9184 BIGNUM *p = NULL;
9185 OSSL_PARAM_BLD *tmpl = NULL;
9186 EVP_PKEY_CTX *pctx = NULL;
9187 OSSL_PARAM *params = NULL;
9188 EVP_PKEY *dhpkey = NULL;
9189
9190 p = BN_get_rfc3526_prime_2048(NULL);
9191 if (!TEST_ptr(p))
9192 goto end;
9193
9194 pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
9195 if (!TEST_ptr(pctx)
9196 || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
9197 goto end;
9198
9199 tmpl = OSSL_PARAM_BLD_new();
9200 if (!TEST_ptr(tmpl)
9201 || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
9202 OSSL_PKEY_PARAM_FFC_P,
9203 p))
9204 || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
9205 OSSL_PKEY_PARAM_FFC_G,
9206 2)))
9207 goto end;
9208
9209 params = OSSL_PARAM_BLD_to_param(tmpl);
9210 if (!TEST_ptr(params)
9211 || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
9212 EVP_PKEY_KEY_PARAMETERS,
9213 params), 1))
9214 goto end;
9215
9216 tmp_dh_params = dhpkey;
9217 end:
9218 BN_free(p);
9219 EVP_PKEY_CTX_free(pctx);
9220 OSSL_PARAM_BLD_free(tmpl);
9221 OSSL_PARAM_free(params);
9222 }
9223
9224 if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
9225 return NULL;
9226
9227 return tmp_dh_params;
9228 }
9229
9230 # ifndef OPENSSL_NO_DEPRECATED_3_0
9231 /* Callback used by test_set_tmp_dh() */
9232 static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
9233 {
9234 EVP_PKEY *dhpkey = get_tmp_dh_params();
9235 DH *ret = NULL;
9236
9237 if (!TEST_ptr(dhpkey))
9238 return NULL;
9239
9240 /*
9241 * libssl does not free the returned DH, so we free it now knowing that even
9242 * after we free dhpkey, there will still be a reference to the owning
9243 * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
9244 * of time we need it for.
9245 */
9246 ret = EVP_PKEY_get1_DH(dhpkey);
9247 DH_free(ret);
9248
9249 EVP_PKEY_free(dhpkey);
9250
9251 return ret;
9252 }
9253 # endif
9254
9255 /*
9256 * Test the various methods for setting temporary DH parameters
9257 *
9258 * Test 0: Default (no auto) setting
9259 * Test 1: Explicit SSL_CTX auto off
9260 * Test 2: Explicit SSL auto off
9261 * Test 3: Explicit SSL_CTX auto on
9262 * Test 4: Explicit SSL auto on
9263 * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
9264 * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
9265 *
9266 * The following are testing deprecated APIs, so we only run them if available
9267 * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
9268 * Test 8: Explicit SSL auto off, custom DH params via DH
9269 * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
9270 * Test 10: Explicit SSL auto off, custom DH params via callback
9271 */
9272 static int test_set_tmp_dh(int idx)
9273 {
9274 SSL_CTX *cctx = NULL, *sctx = NULL;
9275 SSL *clientssl = NULL, *serverssl = NULL;
9276 int testresult = 0;
9277 int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
9278 int expected = (idx <= 2) ? 0 : 1;
9279 EVP_PKEY *dhpkey = NULL;
9280 # ifndef OPENSSL_NO_DEPRECATED_3_0
9281 DH *dh = NULL;
9282 # else
9283
9284 if (idx >= 7)
9285 return 1;
9286 # endif
9287
9288 if (idx >= 5 && idx <= 8) {
9289 dhpkey = get_tmp_dh_params();
9290 if (!TEST_ptr(dhpkey))
9291 goto end;
9292 }
9293 # ifndef OPENSSL_NO_DEPRECATED_3_0
9294 if (idx == 7 || idx == 8) {
9295 dh = EVP_PKEY_get1_DH(dhpkey);
9296 if (!TEST_ptr(dh))
9297 goto end;
9298 }
9299 # endif
9300
9301 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9302 TLS_client_method(),
9303 0,
9304 0,
9305 &sctx, &cctx, cert, privkey)))
9306 goto end;
9307
9308 if ((idx & 1) == 1) {
9309 if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
9310 goto end;
9311 }
9312
9313 if (idx == 5) {
9314 if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
9315 goto end;
9316 dhpkey = NULL;
9317 }
9318 # ifndef OPENSSL_NO_DEPRECATED_3_0
9319 else if (idx == 7) {
9320 if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
9321 goto end;
9322 } else if (idx == 9) {
9323 SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
9324 }
9325 # endif
9326
9327 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9328 NULL, NULL)))
9329 goto end;
9330
9331 if ((idx & 1) == 0 && idx != 0) {
9332 if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
9333 goto end;
9334 }
9335 if (idx == 6) {
9336 if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
9337 goto end;
9338 dhpkey = NULL;
9339 }
9340 # ifndef OPENSSL_NO_DEPRECATED_3_0
9341 else if (idx == 8) {
9342 if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
9343 goto end;
9344 } else if (idx == 10) {
9345 SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
9346 }
9347 # endif
9348
9349 if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9350 || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9351 || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
9352 goto end;
9353
9354 /*
9355 * If autoon then we should succeed. Otherwise we expect failure because
9356 * there are no parameters
9357 */
9358 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
9359 SSL_ERROR_NONE), expected))
9360 goto end;
9361
9362 testresult = 1;
9363
9364 end:
9365 # ifndef OPENSSL_NO_DEPRECATED_3_0
9366 DH_free(dh);
9367 # endif
9368 SSL_free(serverssl);
9369 SSL_free(clientssl);
9370 SSL_CTX_free(sctx);
9371 SSL_CTX_free(cctx);
9372 EVP_PKEY_free(dhpkey);
9373
9374 return testresult;
9375 }
9376
9377 /*
9378 * Test the auto DH keys are appropriately sized
9379 */
9380 static int test_dh_auto(int idx)
9381 {
9382 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
9383 SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9384 SSL *clientssl = NULL, *serverssl = NULL;
9385 int testresult = 0;
9386 EVP_PKEY *tmpkey = NULL;
9387 char *thiscert = NULL, *thiskey = NULL;
9388 size_t expdhsize = 0;
9389 const char *ciphersuite = "DHE-RSA-AES128-SHA";
9390
9391 if (!TEST_ptr(sctx) || !TEST_ptr(cctx))
9392 goto end;
9393
9394 switch (idx) {
9395 case 0:
9396 /* The FIPS provider doesn't support this DH size - so we ignore it */
9397 if (is_fips) {
9398 testresult = 1;
9399 goto end;
9400 }
9401 thiscert = cert1024;
9402 thiskey = privkey1024;
9403 expdhsize = 1024;
9404 SSL_CTX_set_security_level(sctx, 1);
9405 SSL_CTX_set_security_level(cctx, 1);
9406 break;
9407 case 1:
9408 /* 2048 bit prime */
9409 thiscert = cert;
9410 thiskey = privkey;
9411 expdhsize = 2048;
9412 break;
9413 case 2:
9414 thiscert = cert3072;
9415 thiskey = privkey3072;
9416 expdhsize = 3072;
9417 break;
9418 case 3:
9419 thiscert = cert4096;
9420 thiskey = privkey4096;
9421 expdhsize = 4096;
9422 break;
9423 case 4:
9424 thiscert = cert8192;
9425 thiskey = privkey8192;
9426 expdhsize = 8192;
9427 break;
9428 /* No certificate cases */
9429 case 5:
9430 /* The FIPS provider doesn't support this DH size - so we ignore it */
9431 if (is_fips) {
9432 testresult = 1;
9433 goto end;
9434 }
9435 ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
9436 expdhsize = 1024;
9437 break;
9438 case 6:
9439 ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
9440 expdhsize = 3072;
9441 break;
9442 default:
9443 TEST_error("Invalid text index");
9444 goto end;
9445 }
9446
9447 if (!TEST_true(create_ssl_ctx_pair(libctx, NULL,
9448 NULL,
9449 0,
9450 0,
9451 &sctx, &cctx, thiscert, thiskey)))
9452 goto end;
9453
9454 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9455 NULL, NULL)))
9456 goto end;
9457
9458 if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
9459 || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9460 || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9461 || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
9462 || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
9463 goto end;
9464
9465 /*
9466 * Send the server's first flight. At this point the server has created the
9467 * temporary DH key but hasn't finished using it yet. Once used it is
9468 * removed, so we cannot test it.
9469 */
9470 if (!TEST_int_le(SSL_connect(clientssl), 0)
9471 || !TEST_int_le(SSL_accept(serverssl), 0))
9472 goto end;
9473
9474 if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
9475 goto end;
9476 if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
9477 goto end;
9478
9479 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9480 goto end;
9481
9482 testresult = 1;
9483
9484 end:
9485 SSL_free(serverssl);
9486 SSL_free(clientssl);
9487 SSL_CTX_free(sctx);
9488 SSL_CTX_free(cctx);
9489 EVP_PKEY_free(tmpkey);
9490
9491 return testresult;
9492
9493 }
9494 # endif /* OPENSSL_NO_DH */
9495 #endif /* OPENSSL_NO_TLS1_2 */
9496
9497 #ifndef OSSL_NO_USABLE_TLS1_3
9498 /*
9499 * Test that setting an SNI callback works with TLSv1.3. Specifically we check
9500 * that it works even without a certificate configured for the original
9501 * SSL_CTX
9502 */
9503 static int test_sni_tls13(void)
9504 {
9505 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
9506 SSL *clientssl = NULL, *serverssl = NULL;
9507 int testresult = 0;
9508
9509 /* Reset callback counter */
9510 snicb = 0;
9511
9512 /* Create an initial SSL_CTX with no certificate configured */
9513 sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9514 if (!TEST_ptr(sctx))
9515 goto end;
9516 /* Require TLSv1.3 as a minimum */
9517 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9518 TLS_client_method(), TLS1_3_VERSION, 0,
9519 &sctx2, &cctx, cert, privkey)))
9520 goto end;
9521
9522 /* Set up SNI */
9523 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
9524 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
9525 goto end;
9526
9527 /*
9528 * Connection should still succeed because the final SSL_CTX has the right
9529 * certificates configured.
9530 */
9531 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9532 &clientssl, NULL, NULL))
9533 || !TEST_true(create_ssl_connection(serverssl, clientssl,
9534 SSL_ERROR_NONE)))
9535 goto end;
9536
9537 /* We should have had the SNI callback called exactly once */
9538 if (!TEST_int_eq(snicb, 1))
9539 goto end;
9540
9541 testresult = 1;
9542
9543 end:
9544 SSL_free(serverssl);
9545 SSL_free(clientssl);
9546 SSL_CTX_free(sctx2);
9547 SSL_CTX_free(sctx);
9548 SSL_CTX_free(cctx);
9549 return testresult;
9550 }
9551
9552 /*
9553 * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
9554 * 0 = TLSv1.2
9555 * 1 = TLSv1.3
9556 */
9557 static int test_ticket_lifetime(int idx)
9558 {
9559 SSL_CTX *cctx = NULL, *sctx = NULL;
9560 SSL *clientssl = NULL, *serverssl = NULL;
9561 int testresult = 0;
9562 int version = TLS1_3_VERSION;
9563
9564 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
9565 #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
9566
9567 if (idx == 0) {
9568 #ifdef OPENSSL_NO_TLS1_2
9569 return TEST_skip("TLS 1.2 is disabled.");
9570 #else
9571 version = TLS1_2_VERSION;
9572 #endif
9573 }
9574
9575 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9576 TLS_client_method(), version, version,
9577 &sctx, &cctx, cert, privkey)))
9578 goto end;
9579
9580 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9581 &clientssl, NULL, NULL)))
9582 goto end;
9583
9584 /*
9585 * Set the timeout to be more than 1 week
9586 * make sure the returned value is the default
9587 */
9588 if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
9589 SSL_get_default_timeout(serverssl)))
9590 goto end;
9591
9592 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9593 goto end;
9594
9595 if (idx == 0) {
9596 /* TLSv1.2 uses the set value */
9597 if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
9598 goto end;
9599 } else {
9600 /* TLSv1.3 uses the limited value */
9601 if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
9602 goto end;
9603 }
9604 testresult = 1;
9605
9606 end:
9607 SSL_free(serverssl);
9608 SSL_free(clientssl);
9609 SSL_CTX_free(sctx);
9610 SSL_CTX_free(cctx);
9611 return testresult;
9612 }
9613 #endif
9614 /*
9615 * Test that setting an ALPN does not violate RFC
9616 */
9617 static int test_set_alpn(void)
9618 {
9619 SSL_CTX *ctx = NULL;
9620 SSL *ssl = NULL;
9621 int testresult = 0;
9622
9623 unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
9624 unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
9625 unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
9626 unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
9627 unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
9628 unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
9629
9630 /* Create an initial SSL_CTX with no certificate configured */
9631 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9632 if (!TEST_ptr(ctx))
9633 goto end;
9634
9635 /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
9636 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
9637 goto end;
9638 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
9639 goto end;
9640 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
9641 goto end;
9642 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
9643 goto end;
9644 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
9645 goto end;
9646 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
9647 goto end;
9648 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
9649 goto end;
9650 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
9651 goto end;
9652 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
9653 goto end;
9654
9655 ssl = SSL_new(ctx);
9656 if (!TEST_ptr(ssl))
9657 goto end;
9658
9659 if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
9660 goto end;
9661 if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
9662 goto end;
9663 if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
9664 goto end;
9665 if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
9666 goto end;
9667 if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
9668 goto end;
9669 if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
9670 goto end;
9671 if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
9672 goto end;
9673 if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
9674 goto end;
9675 if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
9676 goto end;
9677
9678 testresult = 1;
9679
9680 end:
9681 SSL_free(ssl);
9682 SSL_CTX_free(ctx);
9683 return testresult;
9684 }
9685
9686 /*
9687 * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
9688 */
9689 static int test_set_verify_cert_store_ssl_ctx(void)
9690 {
9691 SSL_CTX *ctx = NULL;
9692 int testresult = 0;
9693 X509_STORE *store = NULL, *new_store = NULL,
9694 *cstore = NULL, *new_cstore = NULL;
9695
9696 /* Create an initial SSL_CTX. */
9697 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9698 if (!TEST_ptr(ctx))
9699 goto end;
9700
9701 /* Retrieve verify store pointer. */
9702 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9703 goto end;
9704
9705 /* Retrieve chain store pointer. */
9706 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9707 goto end;
9708
9709 /* We haven't set any yet, so this should be NULL. */
9710 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9711 goto end;
9712
9713 /* Create stores. We use separate stores so pointers are different. */
9714 new_store = X509_STORE_new();
9715 if (!TEST_ptr(new_store))
9716 goto end;
9717
9718 new_cstore = X509_STORE_new();
9719 if (!TEST_ptr(new_cstore))
9720 goto end;
9721
9722 /* Set stores. */
9723 if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
9724 goto end;
9725
9726 if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
9727 goto end;
9728
9729 /* Should be able to retrieve the same pointer. */
9730 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9731 goto end;
9732
9733 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9734 goto end;
9735
9736 if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
9737 goto end;
9738
9739 /* Should be able to unset again. */
9740 if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
9741 goto end;
9742
9743 if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
9744 goto end;
9745
9746 /* Should now be NULL. */
9747 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9748 goto end;
9749
9750 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9751 goto end;
9752
9753 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9754 goto end;
9755
9756 testresult = 1;
9757
9758 end:
9759 X509_STORE_free(new_store);
9760 X509_STORE_free(new_cstore);
9761 SSL_CTX_free(ctx);
9762 return testresult;
9763 }
9764
9765 /*
9766 * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
9767 */
9768 static int test_set_verify_cert_store_ssl(void)
9769 {
9770 SSL_CTX *ctx = NULL;
9771 SSL *ssl = NULL;
9772 int testresult = 0;
9773 X509_STORE *store = NULL, *new_store = NULL,
9774 *cstore = NULL, *new_cstore = NULL;
9775
9776 /* Create an initial SSL_CTX. */
9777 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9778 if (!TEST_ptr(ctx))
9779 goto end;
9780
9781 /* Create an SSL object. */
9782 ssl = SSL_new(ctx);
9783 if (!TEST_ptr(ssl))
9784 goto end;
9785
9786 /* Retrieve verify store pointer. */
9787 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9788 goto end;
9789
9790 /* Retrieve chain store pointer. */
9791 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9792 goto end;
9793
9794 /* We haven't set any yet, so this should be NULL. */
9795 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9796 goto end;
9797
9798 /* Create stores. We use separate stores so pointers are different. */
9799 new_store = X509_STORE_new();
9800 if (!TEST_ptr(new_store))
9801 goto end;
9802
9803 new_cstore = X509_STORE_new();
9804 if (!TEST_ptr(new_cstore))
9805 goto end;
9806
9807 /* Set stores. */
9808 if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
9809 goto end;
9810
9811 if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
9812 goto end;
9813
9814 /* Should be able to retrieve the same pointer. */
9815 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9816 goto end;
9817
9818 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9819 goto end;
9820
9821 if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
9822 goto end;
9823
9824 /* Should be able to unset again. */
9825 if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
9826 goto end;
9827
9828 if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
9829 goto end;
9830
9831 /* Should now be NULL. */
9832 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9833 goto end;
9834
9835 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9836 goto end;
9837
9838 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9839 goto end;
9840
9841 testresult = 1;
9842
9843 end:
9844 X509_STORE_free(new_store);
9845 X509_STORE_free(new_cstore);
9846 SSL_free(ssl);
9847 SSL_CTX_free(ctx);
9848 return testresult;
9849 }
9850
9851
9852 static int test_inherit_verify_param(void)
9853 {
9854 int testresult = 0;
9855
9856 SSL_CTX *ctx = NULL;
9857 X509_VERIFY_PARAM *cp = NULL;
9858 SSL *ssl = NULL;
9859 X509_VERIFY_PARAM *sp = NULL;
9860 int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
9861
9862 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9863 if (!TEST_ptr(ctx))
9864 goto end;
9865
9866 cp = SSL_CTX_get0_param(ctx);
9867 if (!TEST_ptr(cp))
9868 goto end;
9869 if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
9870 goto end;
9871
9872 X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
9873
9874 ssl = SSL_new(ctx);
9875 if (!TEST_ptr(ssl))
9876 goto end;
9877
9878 sp = SSL_get0_param(ssl);
9879 if (!TEST_ptr(sp))
9880 goto end;
9881 if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
9882 goto end;
9883
9884 testresult = 1;
9885
9886 end:
9887 SSL_free(ssl);
9888 SSL_CTX_free(ctx);
9889
9890 return testresult;
9891 }
9892
9893 static int test_load_dhfile(void)
9894 {
9895 #ifndef OPENSSL_NO_DH
9896 int testresult = 0;
9897
9898 SSL_CTX *ctx = NULL;
9899 SSL_CONF_CTX *cctx = NULL;
9900
9901 if (dhfile == NULL)
9902 return 1;
9903
9904 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
9905 || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
9906 goto end;
9907
9908 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
9909 SSL_CONF_CTX_set_flags(cctx,
9910 SSL_CONF_FLAG_CERTIFICATE
9911 | SSL_CONF_FLAG_SERVER
9912 | SSL_CONF_FLAG_FILE);
9913
9914 if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
9915 goto end;
9916
9917 testresult = 1;
9918 end:
9919 SSL_CONF_CTX_free(cctx);
9920 SSL_CTX_free(ctx);
9921
9922 return testresult;
9923 #else
9924 return TEST_skip("DH not supported by this build");
9925 #endif
9926 }
9927
9928 #ifndef OSSL_NO_USABLE_TLS1_3
9929 /* Test that read_ahead works across a key change */
9930 static int test_read_ahead_key_change(void)
9931 {
9932 SSL_CTX *cctx = NULL, *sctx = NULL;
9933 SSL *clientssl = NULL, *serverssl = NULL;
9934 int testresult = 0;
9935 char *msg = "Hello World";
9936 size_t written, readbytes;
9937 char buf[80];
9938 int i;
9939
9940 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9941 TLS_client_method(), TLS1_3_VERSION, 0,
9942 &sctx, &cctx, cert, privkey)))
9943 goto end;
9944
9945 SSL_CTX_set_read_ahead(sctx, 1);
9946
9947 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9948 &clientssl, NULL, NULL)))
9949 goto end;
9950
9951 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9952 goto end;
9953
9954 /* Write some data, send a key update, write more data */
9955 if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
9956 || !TEST_size_t_eq(written, strlen(msg)))
9957 goto end;
9958
9959 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
9960 goto end;
9961
9962 if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
9963 || !TEST_size_t_eq(written, strlen(msg)))
9964 goto end;
9965
9966 /*
9967 * Since read_ahead is on the first read below should read the record with
9968 * the first app data, the second record with the key update message, and
9969 * the third record with the app data all in one go. We should be able to
9970 * still process the read_ahead data correctly even though it crosses
9971 * epochs
9972 */
9973 for (i = 0; i < 2; i++) {
9974 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
9975 &readbytes)))
9976 goto end;
9977
9978 buf[readbytes] = '\0';
9979 if (!TEST_str_eq(buf, msg))
9980 goto end;
9981 }
9982
9983 testresult = 1;
9984
9985 end:
9986 SSL_free(serverssl);
9987 SSL_free(clientssl);
9988 SSL_CTX_free(sctx);
9989 SSL_CTX_free(cctx);
9990 return testresult;
9991 }
9992
9993 static size_t record_pad_cb(SSL *s, int type, size_t len, void *arg)
9994 {
9995 int *called = arg;
9996
9997 switch ((*called)++) {
9998 case 0:
9999 /* Add some padding to first record */
10000 return 512;
10001 case 1:
10002 /* Maximally pad the second record */
10003 return SSL3_RT_MAX_PLAIN_LENGTH - len;
10004 case 2:
10005 /*
10006 * Exceeding the maximum padding should be fine. It should just pad to
10007 * the maximum anyway
10008 */
10009 return SSL3_RT_MAX_PLAIN_LENGTH + 1 - len;
10010 case 3:
10011 /*
10012 * Very large padding should also be ok. Should just pad to the maximum
10013 * allowed
10014 */
10015 return SIZE_MAX;
10016 default:
10017 return 0;
10018 }
10019 }
10020
10021 /*
10022 * Test that setting record padding in TLSv1.3 works as expected
10023 * Test 0: Record padding callback on the SSL_CTX
10024 * Test 1: Record padding callback on the SSL
10025 * Test 2: Record block padding on the SSL_CTX
10026 * Test 3: Record block padding on the SSL
10027 */
10028 static int test_tls13_record_padding(int idx)
10029 {
10030 SSL_CTX *cctx = NULL, *sctx = NULL;
10031 SSL *clientssl = NULL, *serverssl = NULL;
10032 int testresult = 0;
10033 char *msg = "Hello World";
10034 size_t written, readbytes;
10035 char buf[80];
10036 int i;
10037 int called = 0;
10038
10039 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10040 TLS_client_method(), TLS1_3_VERSION, 0,
10041 &sctx, &cctx, cert, privkey)))
10042 goto end;
10043
10044 if (idx == 0) {
10045 SSL_CTX_set_record_padding_callback(cctx, record_pad_cb);
10046 SSL_CTX_set_record_padding_callback_arg(cctx, &called);
10047 if (!TEST_ptr_eq(SSL_CTX_get_record_padding_callback_arg(cctx), &called))
10048 goto end;
10049 } else if (idx == 2) {
10050 /* Exceeding the max plain length should fail */
10051 if (!TEST_false(SSL_CTX_set_block_padding(cctx,
10052 SSL3_RT_MAX_PLAIN_LENGTH + 1)))
10053 goto end;
10054 if (!TEST_true(SSL_CTX_set_block_padding(cctx, 512)))
10055 goto end;
10056 }
10057
10058 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10059 &clientssl, NULL, NULL)))
10060 goto end;
10061
10062 if (idx == 1) {
10063 SSL_set_record_padding_callback(clientssl, record_pad_cb);
10064 SSL_set_record_padding_callback_arg(clientssl, &called);
10065 if (!TEST_ptr_eq(SSL_get_record_padding_callback_arg(clientssl), &called))
10066 goto end;
10067 } else if (idx == 3) {
10068 /* Exceeding the max plain length should fail */
10069 if (!TEST_false(SSL_set_block_padding(clientssl,
10070 SSL3_RT_MAX_PLAIN_LENGTH + 1)))
10071 goto end;
10072 if (!TEST_true(SSL_set_block_padding(clientssl, 512)))
10073 goto end;
10074 }
10075
10076 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10077 goto end;
10078
10079 called = 0;
10080 /*
10081 * Write some data, then check we can read it. Do this four times to check
10082 * we can continue to write and read padded data after the initial record
10083 * padding has been added. We don't actually check that the padding has
10084 * been applied to the record - just that we can continue to communicate
10085 * normally and that the callback has been called (if appropriate).
10086 */
10087 for (i = 0; i < 4; i++) {
10088 if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
10089 || !TEST_size_t_eq(written, strlen(msg)))
10090 goto end;
10091
10092 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
10093 &readbytes))
10094 || !TEST_size_t_eq(written, readbytes))
10095 goto end;
10096
10097 buf[readbytes] = '\0';
10098 if (!TEST_str_eq(buf, msg))
10099 goto end;
10100 }
10101
10102 if ((idx == 0 || idx == 1) && !TEST_int_eq(called, 4))
10103 goto end;
10104
10105 testresult = 1;
10106 end:
10107 SSL_free(serverssl);
10108 SSL_free(clientssl);
10109 SSL_CTX_free(sctx);
10110 SSL_CTX_free(cctx);
10111 return testresult;
10112 }
10113 #endif /* OSSL_NO_USABLE_TLS1_3 */
10114
10115 OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
10116
10117 int setup_tests(void)
10118 {
10119 char *modulename;
10120 char *configfile;
10121
10122 libctx = OSSL_LIB_CTX_new();
10123 if (!TEST_ptr(libctx))
10124 return 0;
10125
10126 defctxnull = OSSL_PROVIDER_load(NULL, "null");
10127
10128 /*
10129 * Verify that the default and fips providers in the default libctx are not
10130 * available
10131 */
10132 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
10133 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
10134 return 0;
10135
10136 if (!test_skip_common_options()) {
10137 TEST_error("Error parsing test options\n");
10138 return 0;
10139 }
10140
10141 if (!TEST_ptr(certsdir = test_get_argument(0))
10142 || !TEST_ptr(srpvfile = test_get_argument(1))
10143 || !TEST_ptr(tmpfilename = test_get_argument(2))
10144 || !TEST_ptr(modulename = test_get_argument(3))
10145 || !TEST_ptr(configfile = test_get_argument(4))
10146 || !TEST_ptr(dhfile = test_get_argument(5)))
10147 return 0;
10148
10149 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
10150 return 0;
10151
10152 /* Check we have the expected provider available */
10153 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
10154 return 0;
10155
10156 /* Check the default provider is not available */
10157 if (strcmp(modulename, "default") != 0
10158 && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
10159 return 0;
10160
10161 if (strcmp(modulename, "fips") == 0)
10162 is_fips = 1;
10163
10164 /*
10165 * We add, but don't load the test "tls-provider". We'll load it when we
10166 * need it.
10167 */
10168 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
10169 tls_provider_init)))
10170 return 0;
10171
10172
10173 if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
10174 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
10175 TEST_error("not supported in this build");
10176 return 0;
10177 #else
10178 int i, mcount, rcount, fcount;
10179
10180 for (i = 0; i < 4; i++)
10181 test_export_key_mat(i);
10182 CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
10183 test_printf_stdout("malloc %d realloc %d free %d\n",
10184 mcount, rcount, fcount);
10185 return 1;
10186 #endif
10187 }
10188
10189 cert = test_mk_file_path(certsdir, "servercert.pem");
10190 if (cert == NULL)
10191 goto err;
10192
10193 privkey = test_mk_file_path(certsdir, "serverkey.pem");
10194 if (privkey == NULL)
10195 goto err;
10196
10197 cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
10198 if (cert2 == NULL)
10199 goto err;
10200
10201 privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
10202 if (privkey2 == NULL)
10203 goto err;
10204
10205 cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
10206 if (cert1024 == NULL)
10207 goto err;
10208
10209 privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
10210 if (privkey1024 == NULL)
10211 goto err;
10212
10213 cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
10214 if (cert3072 == NULL)
10215 goto err;
10216
10217 privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
10218 if (privkey3072 == NULL)
10219 goto err;
10220
10221 cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
10222 if (cert4096 == NULL)
10223 goto err;
10224
10225 privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
10226 if (privkey4096 == NULL)
10227 goto err;
10228
10229 cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
10230 if (cert8192 == NULL)
10231 goto err;
10232
10233 privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
10234 if (privkey8192 == NULL)
10235 goto err;
10236
10237 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
10238 # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
10239 ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
10240 ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
10241 # endif
10242 #endif
10243 ADD_TEST(test_large_message_tls);
10244 ADD_TEST(test_large_message_tls_read_ahead);
10245 #ifndef OPENSSL_NO_DTLS
10246 ADD_TEST(test_large_message_dtls);
10247 #endif
10248 ADD_TEST(test_cleanse_plaintext);
10249 #ifndef OPENSSL_NO_OCSP
10250 ADD_TEST(test_tlsext_status_type);
10251 #endif
10252 ADD_TEST(test_session_with_only_int_cache);
10253 ADD_TEST(test_session_with_only_ext_cache);
10254 ADD_TEST(test_session_with_both_cache);
10255 ADD_TEST(test_session_wo_ca_names);
10256 #ifndef OSSL_NO_USABLE_TLS1_3
10257 ADD_ALL_TESTS(test_stateful_tickets, 3);
10258 ADD_ALL_TESTS(test_stateless_tickets, 3);
10259 ADD_TEST(test_psk_tickets);
10260 ADD_ALL_TESTS(test_extra_tickets, 6);
10261 #endif
10262 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
10263 ADD_TEST(test_ssl_bio_pop_next_bio);
10264 ADD_TEST(test_ssl_bio_pop_ssl_bio);
10265 ADD_TEST(test_ssl_bio_change_rbio);
10266 ADD_TEST(test_ssl_bio_change_wbio);
10267 #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
10268 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
10269 ADD_TEST(test_keylog);
10270 #endif
10271 #ifndef OSSL_NO_USABLE_TLS1_3
10272 ADD_TEST(test_keylog_no_master_key);
10273 #endif
10274 ADD_TEST(test_client_cert_verify_cb);
10275 ADD_TEST(test_ssl_build_cert_chain);
10276 ADD_TEST(test_ssl_ctx_build_cert_chain);
10277 #ifndef OPENSSL_NO_TLS1_2
10278 ADD_TEST(test_client_hello_cb);
10279 ADD_TEST(test_no_ems);
10280 ADD_TEST(test_ccs_change_cipher);
10281 #endif
10282 #ifndef OSSL_NO_USABLE_TLS1_3
10283 ADD_ALL_TESTS(test_early_data_read_write, 3);
10284 /*
10285 * We don't do replay tests for external PSK. Replay protection isn't used
10286 * in that scenario.
10287 */
10288 ADD_ALL_TESTS(test_early_data_replay, 2);
10289 ADD_ALL_TESTS(test_early_data_skip, 3);
10290 ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
10291 ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
10292 ADD_ALL_TESTS(test_early_data_skip_abort, 3);
10293 ADD_ALL_TESTS(test_early_data_not_sent, 3);
10294 ADD_ALL_TESTS(test_early_data_psk, 8);
10295 ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
10296 ADD_ALL_TESTS(test_early_data_not_expected, 3);
10297 # ifndef OPENSSL_NO_TLS1_2
10298 ADD_ALL_TESTS(test_early_data_tls1_2, 3);
10299 # endif
10300 #endif
10301 #ifndef OSSL_NO_USABLE_TLS1_3
10302 ADD_ALL_TESTS(test_set_ciphersuite, 10);
10303 ADD_TEST(test_ciphersuite_change);
10304 ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
10305 # ifdef OPENSSL_NO_PSK
10306 ADD_ALL_TESTS(test_tls13_psk, 1);
10307 # else
10308 ADD_ALL_TESTS(test_tls13_psk, 4);
10309 # endif /* OPENSSL_NO_PSK */
10310 # ifndef OPENSSL_NO_TLS1_2
10311 /* Test with both TLSv1.3 and 1.2 versions */
10312 ADD_ALL_TESTS(test_key_exchange, 14);
10313 # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
10314 ADD_ALL_TESTS(test_negotiated_group,
10315 4 * (OSSL_NELEM(ecdhe_kexch_groups)
10316 + OSSL_NELEM(ffdhe_kexch_groups)));
10317 # endif
10318 # else
10319 /* Test with only TLSv1.3 versions */
10320 ADD_ALL_TESTS(test_key_exchange, 12);
10321 # endif
10322 ADD_ALL_TESTS(test_custom_exts, 6);
10323 ADD_TEST(test_stateless);
10324 ADD_TEST(test_pha_key_update);
10325 #else
10326 ADD_ALL_TESTS(test_custom_exts, 3);
10327 #endif
10328 ADD_ALL_TESTS(test_export_key_mat, 6);
10329 #ifndef OSSL_NO_USABLE_TLS1_3
10330 ADD_ALL_TESTS(test_export_key_mat_early, 3);
10331 ADD_TEST(test_key_update);
10332 ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
10333 ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
10334 ADD_ALL_TESTS(test_key_update_local_in_write, 2);
10335 ADD_ALL_TESTS(test_key_update_local_in_read, 2);
10336 #endif
10337 ADD_ALL_TESTS(test_ssl_clear, 2);
10338 ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
10339 #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
10340 ADD_ALL_TESTS(test_srp, 6);
10341 #endif
10342 ADD_ALL_TESTS(test_info_callback, 6);
10343 ADD_ALL_TESTS(test_ssl_pending, 2);
10344 ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
10345 ADD_ALL_TESTS(test_ticket_callbacks, 20);
10346 ADD_ALL_TESTS(test_shutdown, 7);
10347 ADD_ALL_TESTS(test_incorrect_shutdown, 2);
10348 ADD_ALL_TESTS(test_cert_cb, 6);
10349 ADD_ALL_TESTS(test_client_cert_cb, 2);
10350 ADD_ALL_TESTS(test_ca_names, 3);
10351 #ifndef OPENSSL_NO_TLS1_2
10352 ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
10353 #endif
10354 ADD_ALL_TESTS(test_servername, 10);
10355 #if !defined(OPENSSL_NO_EC) \
10356 && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
10357 ADD_ALL_TESTS(test_sigalgs_available, 6);
10358 #endif
10359 #ifndef OPENSSL_NO_TLS1_3
10360 ADD_ALL_TESTS(test_pluggable_group, 2);
10361 #endif
10362 #ifndef OPENSSL_NO_TLS1_2
10363 ADD_TEST(test_ssl_dup);
10364 # ifndef OPENSSL_NO_DH
10365 ADD_ALL_TESTS(test_set_tmp_dh, 11);
10366 ADD_ALL_TESTS(test_dh_auto, 7);
10367 # endif
10368 #endif
10369 #ifndef OSSL_NO_USABLE_TLS1_3
10370 ADD_TEST(test_sni_tls13);
10371 ADD_ALL_TESTS(test_ticket_lifetime, 2);
10372 #endif
10373 ADD_TEST(test_inherit_verify_param);
10374 ADD_TEST(test_set_alpn);
10375 ADD_TEST(test_set_verify_cert_store_ssl_ctx);
10376 ADD_TEST(test_set_verify_cert_store_ssl);
10377 ADD_ALL_TESTS(test_session_timeout, 1);
10378 ADD_TEST(test_load_dhfile);
10379 #ifndef OSSL_NO_USABLE_TLS1_3
10380 ADD_TEST(test_read_ahead_key_change);
10381 ADD_ALL_TESTS(test_tls13_record_padding, 4);
10382 #endif
10383 #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
10384 ADD_ALL_TESTS(test_serverinfo_custom, 4);
10385 #endif
10386 return 1;
10387
10388 err:
10389 OPENSSL_free(cert);
10390 OPENSSL_free(privkey);
10391 OPENSSL_free(cert2);
10392 OPENSSL_free(privkey2);
10393 return 0;
10394 }
10395
10396 void cleanup_tests(void)
10397 {
10398 # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
10399 EVP_PKEY_free(tmp_dh_params);
10400 #endif
10401 OPENSSL_free(cert);
10402 OPENSSL_free(privkey);
10403 OPENSSL_free(cert2);
10404 OPENSSL_free(privkey2);
10405 OPENSSL_free(cert1024);
10406 OPENSSL_free(privkey1024);
10407 OPENSSL_free(cert3072);
10408 OPENSSL_free(privkey3072);
10409 OPENSSL_free(cert4096);
10410 OPENSSL_free(privkey4096);
10411 OPENSSL_free(cert8192);
10412 OPENSSL_free(privkey8192);
10413 bio_s_mempacket_test_free();
10414 bio_s_always_retry_free();
10415 OSSL_PROVIDER_unload(defctxnull);
10416 OSSL_LIB_CTX_free(libctx);
10417 }