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
hostname_cb(SSL * s,int * al,void * arg)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
client_keylog_callback(const SSL * ssl,const char * line)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
server_keylog_callback(const SSL * ssl,const char * line)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
compare_hex_encoded_buffer(const char * hex_encoded,size_t hex_length,const uint8_t * raw,size_t raw_length)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
test_keylog_output(char * buffer,const SSL * ssl,const SSL_SESSION * session,struct sslapitest_log_counts * expected)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)
test_keylog(void)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
test_keylog_no_master_key(void)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
verify_retry_cb(X509_STORE_CTX * ctx,void * arg)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
test_client_cert_verify_cb(void)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
test_ssl_build_cert_chain(void)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
get_password_cb(char * buf,int size,int rw_flag,void * userdata)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
test_ssl_ctx_build_cert_chain(void)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
full_client_hello_callback(SSL * s,int * al,void * arg)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
test_client_hello_cb(void)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
test_no_ems(void)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 */
test_ccs_change_cipher(void)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
execute_test_large_message(const SSL_METHOD * smeth,const SSL_METHOD * cmeth,int min_version,int max_version,int read_ahead)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 */
ktls_chk_platform(int sock)1048 static int ktls_chk_platform(int sock)
1049 {
1050 if (!ktls_enable(sock))
1051 return 0;
1052 return 1;
1053 }
1054
ping_pong_query(SSL * clientssl,SSL * serverssl)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
execute_test_ktls(int cis_ktls,int sis_ktls,int tls_version,const char * cipher)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
execute_test_ktls_sendfile(int tls_version,const char * cipher)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
test_ktls(int test)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
test_ktls_sendfile(int tst)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
test_large_message_tls(void)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
test_large_message_tls_read_ahead(void)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
test_large_message_dtls(void)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
execute_cleanse_plaintext(const SSL_METHOD * smeth,const SSL_METHOD * cmeth,int min_version,int max_version)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
test_cleanse_plaintext(void)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
ocsp_server_cb(SSL * s,void * arg)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
ocsp_client_cb(SSL * s,void * arg)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
test_tlsext_status_type(void)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
new_session_cb(SSL * ssl,SSL_SESSION * sess)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
remove_session_cb(SSL_CTX * ctx,SSL_SESSION * sess)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
get_session_cb(SSL * ssl,const unsigned char * id,int len,int * copy)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
execute_test_session(int maxprot,int use_int_cache,int use_ext_cache,long s_options)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
test_session_with_only_int_cache(void)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
test_session_with_only_ext_cache(void)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
test_session_with_both_cache(void)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
test_session_wo_ca_names(void)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
new_cachesession_cb(SSL * ssl,SSL_SESSION * sess)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
post_handshake_verify(SSL * sssl,SSL * cssl)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
setup_ticket_test(int stateful,int idx,SSL_CTX ** sctx,SSL_CTX ** cctx)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
check_resumption(int idx,SSL_CTX * sctx,SSL_CTX * cctx,int succ)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
test_tickets(int stateful,int idx)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
test_stateless_tickets(int idx)2465 static int test_stateless_tickets(int idx)
2466 {
2467 return test_tickets(0, idx);
2468 }
2469
test_stateful_tickets(int idx)2470 static int test_stateful_tickets(int idx)
2471 {
2472 return test_tickets(1, idx);
2473 }
2474
test_psk_tickets(void)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
test_extra_tickets(int idx)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
setupbio(BIO ** res,BIO * bio1,BIO * bio2,int type)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 */
test_ssl_set_bio(int idx)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
execute_test_ssl_bio(int pop_ssl,bio_change_t change_bio)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
test_ssl_bio_pop_next_bio(void)2915 static int test_ssl_bio_pop_next_bio(void)
2916 {
2917 return execute_test_ssl_bio(0, NO_BIO_CHANGE);
2918 }
2919
test_ssl_bio_pop_ssl_bio(void)2920 static int test_ssl_bio_pop_ssl_bio(void)
2921 {
2922 return execute_test_ssl_bio(1, NO_BIO_CHANGE);
2923 }
2924
test_ssl_bio_change_rbio(void)2925 static int test_ssl_bio_change_rbio(void)
2926 {
2927 return execute_test_ssl_bio(0, CHANGE_RBIO);
2928 }
2929
test_ssl_bio_change_wbio(void)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
test_set_sigalgs(int idx)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
use_session_cb(SSL * ssl,const EVP_MD * md,const unsigned char ** id,size_t * idlen,SSL_SESSION ** sess)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
psk_client_cb(SSL * ssl,const char * hint,char * id,unsigned int max_id_len,unsigned char * psk,unsigned int max_psk_len)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
find_session_cb(SSL * ssl,const unsigned char * identity,size_t identity_len,SSL_SESSION ** sess)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
psk_server_cb(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len)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
create_a_psk(SSL * ssl)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 */
setupearly_data_test(SSL_CTX ** cctx,SSL_CTX ** sctx,SSL ** clientssl,SSL ** serverssl,SSL_SESSION ** sess,int idx)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
test_early_data_read_write(int idx)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
allow_early_data_cb(SSL * s,void * arg)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 */
test_early_data_replay_int(int idx,int usecb,int confopt)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
test_early_data_replay(int idx)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 */
early_data_skip_helper(int testtype,int idx)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 */
test_early_data_skip(int idx)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 */
test_early_data_skip_hrr(int idx)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 */
test_early_data_skip_hrr_fail(int idx)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 */
test_early_data_skip_abort(int idx)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 */
test_early_data_not_sent(int idx)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
alpn_select_cb(SSL * ssl,const unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)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 */
test_early_data_psk(int idx)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 */
test_early_data_psk_with_all_ciphers(int idx)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 */
test_early_data_not_expected(int idx)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 */
test_early_data_tls1_2(int idx)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 */
test_set_ciphersuite(int idx)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
test_ciphersuite_change(void)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
test_key_exchange(int idx)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)
set_ssl_groups(SSL * serverssl,SSL * clientssl,int clientmulti,int isecdhe,int idx)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 */
test_negotiated_group(int idx)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 */
test_tls13_ciphersuite(int idx)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 */
test_tls13_psk(int idx)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
generate_cookie_callback(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len)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
verify_cookie_callback(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len)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
generate_stateless_cookie_callback(SSL * ssl,unsigned char * cookie,size_t * cookie_len)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
verify_stateless_cookie_callback(SSL * ssl,const unsigned char * cookie,size_t cookie_len)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
test_stateless(void)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
old_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * add_arg)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
old_free_cb(SSL * s,unsigned int ext_type,const unsigned char * out,void * add_arg)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
old_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * parse_arg)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
new_add_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char ** out,size_t * outlen,X509 * x,size_t chainidx,int * al,void * add_arg)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
new_free_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * out,void * add_arg)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
new_parse_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * in,size_t inlen,X509 * x,size_t chainidx,int * al,void * parse_arg)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
sni_cb(SSL * s,int * al,void * arg)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
verify_cb(int preverify_ok,X509_STORE_CTX * x509_ctx)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 */
test_custom_exts(int tst)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
serverinfo_custom_parse_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * in,size_t inlen,X509 * x,size_t chainidx,int * al,void * parse_arg)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
test_serverinfo_custom(const int idx)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
test_export_key_mat(int tst)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 */
test_export_key_mat_early(int idx)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 */
test_key_update(void)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 */
test_key_update_peer_in_write(int tst)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 */
test_key_update_peer_in_read(int tst)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 */
test_key_update_local_in_write(int tst)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 */
test_key_update_local_in_read(int tst)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
test_ssl_clear(int idx)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 */
get_MFL_from_client_hello(BIO * bio,int * mfl_codemfl_code)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
test_max_fragment_len_ext(int idx_tst)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
test_pha_key_update(void)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
ssl_srp_cb(SSL * s,int * ad,void * arg)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
create_new_vfile(char * userid,char * password,const char * filename)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
create_new_vbase(char * userid,char * password)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 */
test_srp(int tst)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
sslapi_info_callback(const SSL * s,int where,int ret)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 */
test_info_callback(int tst)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
test_ssl_pending(int tst)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
int_test_ssl_get_shared_ciphers(int tst,int clnt)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
test_ssl_get_shared_ciphers(int tst)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
gen_tick_cb(SSL * s,void * arg)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
dec_tick_cb(SSL * s,SSL_SESSION * ss,const unsigned char * keyname,size_t keyname_length,SSL_TICKET_STATUS status,void * arg)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
tick_key_cb(SSL * s,unsigned char key_name[16],unsigned char iv[EVP_MAX_IV_LENGTH],EVP_CIPHER_CTX * ctx,HMAC_CTX * hctx,int enc)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 = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7649 EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
7650 int ret;
7651
7652 tick_key_cb_called = 1;
7653 memset(iv, 0, AES_BLOCK_SIZE);
7654 memset(key_name, 0, 16);
7655 if (aes128cbc == NULL
7656 || sha256 == NULL
7657 || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7658 || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
7659 NULL))
7660 ret = -1;
7661 else
7662 ret = tick_key_renew ? 2 : 1;
7663
7664 EVP_CIPHER_free(aes128cbc);
7665 EVP_MD_free(sha256);
7666
7667 return ret;
7668 }
7669 #endif
7670
tick_key_evp_cb(SSL * s,unsigned char key_name[16],unsigned char iv[EVP_MAX_IV_LENGTH],EVP_CIPHER_CTX * ctx,EVP_MAC_CTX * hctx,int enc)7671 static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
7672 unsigned char iv[EVP_MAX_IV_LENGTH],
7673 EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
7674 {
7675 const unsigned char tick_aes_key[16] = "0123456789abcdef";
7676 unsigned char tick_hmac_key[16] = "0123456789abcdef";
7677 OSSL_PARAM params[2];
7678 EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7679 int ret;
7680
7681 tick_key_cb_called = 1;
7682 memset(iv, 0, AES_BLOCK_SIZE);
7683 memset(key_name, 0, 16);
7684 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
7685 "SHA256", 0);
7686 params[1] = OSSL_PARAM_construct_end();
7687 if (aes128cbc == NULL
7688 || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7689 || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
7690 params))
7691 ret = -1;
7692 else
7693 ret = tick_key_renew ? 2 : 1;
7694
7695 EVP_CIPHER_free(aes128cbc);
7696
7697 return ret;
7698 }
7699
7700 /*
7701 * Test the various ticket callbacks
7702 * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
7703 * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
7704 * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
7705 * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
7706 * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
7707 * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
7708 * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
7709 * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
7710 * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
7711 * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
7712 * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
7713 * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
7714 * Test 12: TLSv1.2, ticket key callback, ticket, no renewal
7715 * Test 13: TLSv1.3, ticket key callback, ticket, no renewal
7716 * Test 14: TLSv1.2, ticket key callback, ticket, renewal
7717 * Test 15: TLSv1.3, ticket key callback, ticket, renewal
7718 */
test_ticket_callbacks(int tst)7719 static int test_ticket_callbacks(int tst)
7720 {
7721 SSL_CTX *cctx = NULL, *sctx = NULL;
7722 SSL *clientssl = NULL, *serverssl = NULL;
7723 SSL_SESSION *clntsess = NULL;
7724 int testresult = 0;
7725
7726 #ifdef OPENSSL_NO_TLS1_2
7727 if (tst % 2 == 0)
7728 return 1;
7729 #endif
7730 #ifdef OSSL_NO_USABLE_TLS1_3
7731 if (tst % 2 == 1)
7732 return 1;
7733 #endif
7734 #ifdef OPENSSL_NO_DEPRECATED_3_0
7735 if (tst >= 8 && tst <= 11)
7736 return 1;
7737 #endif
7738
7739 gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
7740
7741 /* Which tests the ticket key callback should request renewal for */
7742 if (tst == 10 || tst == 11 || tst == 14 || tst == 15)
7743 tick_key_renew = 1;
7744 else
7745 tick_key_renew = 0;
7746
7747 /* Which tests the decrypt ticket callback should request renewal for */
7748 switch (tst) {
7749 case 0:
7750 case 1:
7751 tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
7752 break;
7753
7754 case 2:
7755 case 3:
7756 tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
7757 break;
7758
7759 case 4:
7760 case 5:
7761 tick_dec_ret = SSL_TICKET_RETURN_USE;
7762 break;
7763
7764 case 6:
7765 case 7:
7766 tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
7767 break;
7768
7769 default:
7770 tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7771 }
7772
7773 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7774 TLS_client_method(),
7775 TLS1_VERSION,
7776 ((tst % 2) == 0) ? TLS1_2_VERSION
7777 : TLS1_3_VERSION,
7778 &sctx, &cctx, cert, privkey)))
7779 goto end;
7780
7781 /*
7782 * We only want sessions to resume from tickets - not the session cache. So
7783 * switch the cache off.
7784 */
7785 if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
7786 goto end;
7787
7788 if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
7789 NULL)))
7790 goto end;
7791
7792 if (tst >= 12) {
7793 if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
7794 goto end;
7795 #ifndef OPENSSL_NO_DEPRECATED_3_0
7796 } else if (tst >= 8) {
7797 if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
7798 goto end;
7799 #endif
7800 }
7801
7802 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7803 NULL, NULL))
7804 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7805 SSL_ERROR_NONE)))
7806 goto end;
7807
7808 /*
7809 * The decrypt ticket key callback in TLSv1.2 should be called even though
7810 * we have no ticket yet, because it gets called with a status of
7811 * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
7812 * actually send any ticket data). This does not happen in TLSv1.3 because
7813 * it is not valid to send empty ticket data in TLSv1.3.
7814 */
7815 if (!TEST_int_eq(gen_tick_called, 1)
7816 || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
7817 goto end;
7818
7819 gen_tick_called = dec_tick_called = 0;
7820
7821 clntsess = SSL_get1_session(clientssl);
7822 SSL_shutdown(clientssl);
7823 SSL_shutdown(serverssl);
7824 SSL_free(serverssl);
7825 SSL_free(clientssl);
7826 serverssl = clientssl = NULL;
7827
7828 /* Now do a resumption */
7829 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7830 NULL))
7831 || !TEST_true(SSL_set_session(clientssl, clntsess))
7832 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7833 SSL_ERROR_NONE)))
7834 goto end;
7835
7836 if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
7837 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
7838 if (!TEST_false(SSL_session_reused(clientssl)))
7839 goto end;
7840 } else {
7841 if (!TEST_true(SSL_session_reused(clientssl)))
7842 goto end;
7843 }
7844
7845 if (!TEST_int_eq(gen_tick_called,
7846 (tick_key_renew
7847 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
7848 || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
7849 ? 1 : 0)
7850 || !TEST_int_eq(dec_tick_called, 1))
7851 goto end;
7852
7853 testresult = 1;
7854
7855 end:
7856 SSL_SESSION_free(clntsess);
7857 SSL_free(serverssl);
7858 SSL_free(clientssl);
7859 SSL_CTX_free(sctx);
7860 SSL_CTX_free(cctx);
7861
7862 return testresult;
7863 }
7864
7865 /*
7866 * Test incorrect shutdown.
7867 * Test 0: client does not shutdown properly,
7868 * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
7869 * server should get SSL_ERROR_SSL
7870 * Test 1: client does not shutdown properly,
7871 * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
7872 * server should get SSL_ERROR_ZERO_RETURN
7873 */
test_incorrect_shutdown(int tst)7874 static int test_incorrect_shutdown(int tst)
7875 {
7876 SSL_CTX *cctx = NULL, *sctx = NULL;
7877 SSL *clientssl = NULL, *serverssl = NULL;
7878 int testresult = 0;
7879 char buf[80];
7880 BIO *c2s;
7881
7882 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7883 TLS_client_method(), 0, 0,
7884 &sctx, &cctx, cert, privkey)))
7885 goto end;
7886
7887 if (tst == 1)
7888 SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
7889
7890 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7891 NULL, NULL)))
7892 goto end;
7893
7894 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7895 SSL_ERROR_NONE)))
7896 goto end;
7897
7898 c2s = SSL_get_rbio(serverssl);
7899 BIO_set_mem_eof_return(c2s, 0);
7900
7901 if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
7902 goto end;
7903
7904 if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
7905 goto end;
7906 if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
7907 goto end;
7908
7909 testresult = 1;
7910
7911 end:
7912 SSL_free(serverssl);
7913 SSL_free(clientssl);
7914 SSL_CTX_free(sctx);
7915 SSL_CTX_free(cctx);
7916
7917 return testresult;
7918 }
7919
7920 /*
7921 * Test bi-directional shutdown.
7922 * Test 0: TLSv1.2
7923 * Test 1: TLSv1.2, server continues to read/write after client shutdown
7924 * Test 2: TLSv1.3, no pending NewSessionTicket messages
7925 * Test 3: TLSv1.3, pending NewSessionTicket messages
7926 * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
7927 * sends key update, client reads it
7928 * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
7929 * sends CertificateRequest, client reads and ignores it
7930 * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
7931 * doesn't read it
7932 */
test_shutdown(int tst)7933 static int test_shutdown(int tst)
7934 {
7935 SSL_CTX *cctx = NULL, *sctx = NULL;
7936 SSL *clientssl = NULL, *serverssl = NULL;
7937 int testresult = 0;
7938 char msg[] = "A test message";
7939 char buf[80];
7940 size_t written, readbytes;
7941 SSL_SESSION *sess;
7942
7943 #ifdef OPENSSL_NO_TLS1_2
7944 if (tst <= 1)
7945 return 1;
7946 #endif
7947 #ifdef OSSL_NO_USABLE_TLS1_3
7948 if (tst >= 2)
7949 return 1;
7950 #endif
7951
7952 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7953 TLS_client_method(),
7954 TLS1_VERSION,
7955 (tst <= 1) ? TLS1_2_VERSION
7956 : TLS1_3_VERSION,
7957 &sctx, &cctx, cert, privkey)))
7958 goto end;
7959
7960 if (tst == 5)
7961 SSL_CTX_set_post_handshake_auth(cctx, 1);
7962
7963 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7964 NULL, NULL)))
7965 goto end;
7966
7967 if (tst == 3) {
7968 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
7969 SSL_ERROR_NONE, 1, 0))
7970 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
7971 || !TEST_false(SSL_SESSION_is_resumable(sess)))
7972 goto end;
7973 } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7974 SSL_ERROR_NONE))
7975 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
7976 || !TEST_true(SSL_SESSION_is_resumable(sess))) {
7977 goto end;
7978 }
7979
7980 if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
7981 goto end;
7982
7983 if (tst >= 4) {
7984 /*
7985 * Reading on the server after the client has sent close_notify should
7986 * fail and provide SSL_ERROR_ZERO_RETURN
7987 */
7988 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
7989 || !TEST_int_eq(SSL_get_error(serverssl, 0),
7990 SSL_ERROR_ZERO_RETURN)
7991 || !TEST_int_eq(SSL_get_shutdown(serverssl),
7992 SSL_RECEIVED_SHUTDOWN)
7993 /*
7994 * Even though we're shutdown on receive we should still be
7995 * able to write.
7996 */
7997 || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
7998 goto end;
7999 if (tst == 4
8000 && !TEST_true(SSL_key_update(serverssl,
8001 SSL_KEY_UPDATE_REQUESTED)))
8002 goto end;
8003 if (tst == 5) {
8004 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
8005 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
8006 goto end;
8007 }
8008 if ((tst == 4 || tst == 5)
8009 && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8010 goto end;
8011 if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
8012 goto end;
8013 if (tst == 4 || tst == 5) {
8014 /* Should still be able to read data from server */
8015 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8016 &readbytes))
8017 || !TEST_size_t_eq(readbytes, sizeof(msg))
8018 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
8019 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8020 &readbytes))
8021 || !TEST_size_t_eq(readbytes, sizeof(msg))
8022 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
8023 goto end;
8024 }
8025 }
8026
8027 /* Writing on the client after sending close_notify shouldn't be possible */
8028 if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
8029 goto end;
8030
8031 if (tst < 4) {
8032 /*
8033 * For these tests the client has sent close_notify but it has not yet
8034 * been received by the server. The server has not sent close_notify
8035 * yet.
8036 */
8037 if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
8038 /*
8039 * Writing on the server after sending close_notify shouldn't
8040 * be possible.
8041 */
8042 || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8043 || !TEST_int_eq(SSL_shutdown(clientssl), 1)
8044 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8045 || !TEST_true(SSL_SESSION_is_resumable(sess))
8046 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
8047 goto end;
8048 } else if (tst == 4 || tst == 5) {
8049 /*
8050 * In this test the client has sent close_notify and it has been
8051 * received by the server which has responded with a close_notify. The
8052 * client needs to read the close_notify sent by the server.
8053 */
8054 if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
8055 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8056 || !TEST_true(SSL_SESSION_is_resumable(sess)))
8057 goto end;
8058 } else {
8059 /*
8060 * tst == 6
8061 *
8062 * The client has sent close_notify and is expecting a close_notify
8063 * back, but instead there is application data first. The shutdown
8064 * should fail with a fatal error.
8065 */
8066 if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
8067 || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
8068 goto end;
8069 }
8070
8071 testresult = 1;
8072
8073 end:
8074 SSL_free(serverssl);
8075 SSL_free(clientssl);
8076 SSL_CTX_free(sctx);
8077 SSL_CTX_free(cctx);
8078
8079 return testresult;
8080 }
8081
8082 #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8083 static int cert_cb_cnt;
8084
cert_cb(SSL * s,void * arg)8085 static int cert_cb(SSL *s, void *arg)
8086 {
8087 SSL_CTX *ctx = (SSL_CTX *)arg;
8088 BIO *in = NULL;
8089 EVP_PKEY *pkey = NULL;
8090 X509 *x509 = NULL, *rootx = NULL;
8091 STACK_OF(X509) *chain = NULL;
8092 char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
8093 int ret = 0;
8094
8095 if (cert_cb_cnt == 0) {
8096 /* Suspend the handshake */
8097 cert_cb_cnt++;
8098 return -1;
8099 } else if (cert_cb_cnt == 1) {
8100 /*
8101 * Update the SSL_CTX, set the certificate and private key and then
8102 * continue the handshake normally.
8103 */
8104 if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
8105 return 0;
8106
8107 if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
8108 || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
8109 SSL_FILETYPE_PEM))
8110 || !TEST_true(SSL_check_private_key(s)))
8111 return 0;
8112 cert_cb_cnt++;
8113 return 1;
8114 } else if (cert_cb_cnt == 3) {
8115 int rv;
8116
8117 rootfile = test_mk_file_path(certsdir, "rootcert.pem");
8118 ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
8119 ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
8120 if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
8121 goto out;
8122 chain = sk_X509_new_null();
8123 if (!TEST_ptr(chain))
8124 goto out;
8125 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8126 || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
8127 || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
8128 || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
8129 || !TEST_true(sk_X509_push(chain, rootx)))
8130 goto out;
8131 rootx = NULL;
8132 BIO_free(in);
8133 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8134 || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
8135 || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
8136 || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
8137 goto out;
8138 BIO_free(in);
8139 if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8140 || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
8141 || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
8142 NULL, NULL,
8143 libctx, NULL)))
8144 goto out;
8145 rv = SSL_check_chain(s, x509, pkey, chain);
8146 /*
8147 * If the cert doesn't show as valid here (e.g., because we don't
8148 * have any shared sigalgs), then we will not set it, and there will
8149 * be no certificate at all on the SSL or SSL_CTX. This, in turn,
8150 * will cause tls_choose_sigalgs() to fail the connection.
8151 */
8152 if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
8153 == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
8154 if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
8155 goto out;
8156 }
8157
8158 ret = 1;
8159 }
8160
8161 /* Abort the handshake */
8162 out:
8163 OPENSSL_free(ecdsacert);
8164 OPENSSL_free(ecdsakey);
8165 OPENSSL_free(rootfile);
8166 BIO_free(in);
8167 EVP_PKEY_free(pkey);
8168 X509_free(x509);
8169 X509_free(rootx);
8170 OSSL_STACK_OF_X509_free(chain);
8171 return ret;
8172 }
8173
8174 /*
8175 * Test the certificate callback.
8176 * Test 0: Callback fails
8177 * Test 1: Success - no SSL_set_SSL_CTX() in the callback
8178 * Test 2: Success - SSL_set_SSL_CTX() in the callback
8179 * Test 3: Success - Call SSL_check_chain from the callback
8180 * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
8181 * chain
8182 * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
8183 */
test_cert_cb_int(int prot,int tst)8184 static int test_cert_cb_int(int prot, int tst)
8185 {
8186 SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
8187 SSL *clientssl = NULL, *serverssl = NULL;
8188 int testresult = 0, ret;
8189
8190 #ifdef OPENSSL_NO_EC
8191 /* We use an EC cert in these tests, so we skip in a no-ec build */
8192 if (tst >= 3)
8193 return 1;
8194 #endif
8195
8196 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8197 TLS_client_method(),
8198 TLS1_VERSION,
8199 prot,
8200 &sctx, &cctx, NULL, NULL)))
8201 goto end;
8202
8203 if (tst == 0)
8204 cert_cb_cnt = -1;
8205 else if (tst >= 3)
8206 cert_cb_cnt = 3;
8207 else
8208 cert_cb_cnt = 0;
8209
8210 if (tst == 2) {
8211 snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
8212 if (!TEST_ptr(snictx))
8213 goto end;
8214 }
8215
8216 SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
8217
8218 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8219 NULL, NULL)))
8220 goto end;
8221
8222 if (tst == 4) {
8223 /*
8224 * We cause SSL_check_chain() to fail by specifying sig_algs that
8225 * the chain doesn't meet (the root uses an RSA cert)
8226 */
8227 if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8228 "ecdsa_secp256r1_sha256")))
8229 goto end;
8230 } else if (tst == 5) {
8231 /*
8232 * We cause SSL_check_chain() to fail by specifying sig_algs that
8233 * the ee cert doesn't meet (the ee uses an ECDSA cert)
8234 */
8235 if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8236 "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
8237 goto end;
8238 }
8239
8240 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
8241 if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
8242 || (tst > 0
8243 && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
8244 goto end;
8245 }
8246
8247 testresult = 1;
8248
8249 end:
8250 SSL_free(serverssl);
8251 SSL_free(clientssl);
8252 SSL_CTX_free(sctx);
8253 SSL_CTX_free(cctx);
8254 SSL_CTX_free(snictx);
8255
8256 return testresult;
8257 }
8258 #endif
8259
test_cert_cb(int tst)8260 static int test_cert_cb(int tst)
8261 {
8262 int testresult = 1;
8263
8264 #ifndef OPENSSL_NO_TLS1_2
8265 testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
8266 #endif
8267 #ifndef OSSL_NO_USABLE_TLS1_3
8268 testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
8269 #endif
8270
8271 return testresult;
8272 }
8273
client_cert_cb(SSL * ssl,X509 ** x509,EVP_PKEY ** pkey)8274 static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
8275 {
8276 X509 *xcert;
8277 EVP_PKEY *privpkey;
8278 BIO *in = NULL;
8279 BIO *priv_in = NULL;
8280
8281 /* Check that SSL_get0_peer_certificate() returns something sensible */
8282 if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
8283 return 0;
8284
8285 in = BIO_new_file(cert, "r");
8286 if (!TEST_ptr(in))
8287 return 0;
8288
8289 if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
8290 || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
8291 || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
8292 || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
8293 NULL, NULL,
8294 libctx, NULL)))
8295 goto err;
8296
8297 *x509 = xcert;
8298 *pkey = privpkey;
8299
8300 BIO_free(in);
8301 BIO_free(priv_in);
8302 return 1;
8303 err:
8304 X509_free(xcert);
8305 BIO_free(in);
8306 BIO_free(priv_in);
8307 return 0;
8308 }
8309
test_client_cert_cb(int tst)8310 static int test_client_cert_cb(int tst)
8311 {
8312 SSL_CTX *cctx = NULL, *sctx = NULL;
8313 SSL *clientssl = NULL, *serverssl = NULL;
8314 int testresult = 0;
8315
8316 #ifdef OPENSSL_NO_TLS1_2
8317 if (tst == 0)
8318 return 1;
8319 #endif
8320 #ifdef OSSL_NO_USABLE_TLS1_3
8321 if (tst == 1)
8322 return 1;
8323 #endif
8324
8325 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8326 TLS_client_method(),
8327 TLS1_VERSION,
8328 tst == 0 ? TLS1_2_VERSION
8329 : TLS1_3_VERSION,
8330 &sctx, &cctx, cert, privkey)))
8331 goto end;
8332
8333 /*
8334 * Test that setting a client_cert_cb results in a client certificate being
8335 * sent.
8336 */
8337 SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
8338 SSL_CTX_set_verify(sctx,
8339 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
8340 verify_cb);
8341
8342 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8343 NULL, NULL))
8344 || !TEST_true(create_ssl_connection(serverssl, clientssl,
8345 SSL_ERROR_NONE)))
8346 goto end;
8347
8348 testresult = 1;
8349
8350 end:
8351 SSL_free(serverssl);
8352 SSL_free(clientssl);
8353 SSL_CTX_free(sctx);
8354 SSL_CTX_free(cctx);
8355
8356 return testresult;
8357 }
8358
8359 #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8360 /*
8361 * Test setting certificate authorities on both client and server.
8362 *
8363 * Test 0: SSL_CTX_set0_CA_list() only
8364 * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
8365 * Test 2: Only SSL_CTX_set_client_CA_list()
8366 */
test_ca_names_int(int prot,int tst)8367 static int test_ca_names_int(int prot, int tst)
8368 {
8369 SSL_CTX *cctx = NULL, *sctx = NULL;
8370 SSL *clientssl = NULL, *serverssl = NULL;
8371 int testresult = 0;
8372 size_t i;
8373 X509_NAME *name[] = { NULL, NULL, NULL, NULL };
8374 char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
8375 STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
8376 const STACK_OF(X509_NAME) *sktmp = NULL;
8377
8378 for (i = 0; i < OSSL_NELEM(name); i++) {
8379 name[i] = X509_NAME_new();
8380 if (!TEST_ptr(name[i])
8381 || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
8382 MBSTRING_ASC,
8383 (unsigned char *)
8384 strnames[i],
8385 -1, -1, 0)))
8386 goto end;
8387 }
8388
8389 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8390 TLS_client_method(),
8391 TLS1_VERSION,
8392 prot,
8393 &sctx, &cctx, cert, privkey)))
8394 goto end;
8395
8396 SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
8397
8398 if (tst == 0 || tst == 1) {
8399 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8400 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
8401 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
8402 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8403 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
8404 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
8405 goto end;
8406
8407 SSL_CTX_set0_CA_list(sctx, sk1);
8408 SSL_CTX_set0_CA_list(cctx, sk2);
8409 sk1 = sk2 = NULL;
8410 }
8411 if (tst == 1 || tst == 2) {
8412 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8413 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
8414 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
8415 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8416 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
8417 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
8418 goto end;
8419
8420 SSL_CTX_set_client_CA_list(sctx, sk1);
8421 SSL_CTX_set_client_CA_list(cctx, sk2);
8422 sk1 = sk2 = NULL;
8423 }
8424
8425 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8426 NULL, NULL))
8427 || !TEST_true(create_ssl_connection(serverssl, clientssl,
8428 SSL_ERROR_NONE)))
8429 goto end;
8430
8431 /*
8432 * We only expect certificate authorities to have been sent to the server
8433 * if we are using TLSv1.3 and SSL_set0_CA_list() was used
8434 */
8435 sktmp = SSL_get0_peer_CA_list(serverssl);
8436 if (prot == TLS1_3_VERSION
8437 && (tst == 0 || tst == 1)) {
8438 if (!TEST_ptr(sktmp)
8439 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8440 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8441 name[0]), 0)
8442 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8443 name[1]), 0))
8444 goto end;
8445 } else if (!TEST_ptr_null(sktmp)) {
8446 goto end;
8447 }
8448
8449 /*
8450 * In all tests we expect certificate authorities to have been sent to the
8451 * client. However, SSL_set_client_CA_list() should override
8452 * SSL_set0_CA_list()
8453 */
8454 sktmp = SSL_get0_peer_CA_list(clientssl);
8455 if (!TEST_ptr(sktmp)
8456 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8457 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8458 name[tst == 0 ? 0 : 2]), 0)
8459 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8460 name[tst == 0 ? 1 : 3]), 0))
8461 goto end;
8462
8463 testresult = 1;
8464
8465 end:
8466 SSL_free(serverssl);
8467 SSL_free(clientssl);
8468 SSL_CTX_free(sctx);
8469 SSL_CTX_free(cctx);
8470 for (i = 0; i < OSSL_NELEM(name); i++)
8471 X509_NAME_free(name[i]);
8472 sk_X509_NAME_pop_free(sk1, X509_NAME_free);
8473 sk_X509_NAME_pop_free(sk2, X509_NAME_free);
8474
8475 return testresult;
8476 }
8477 #endif
8478
test_ca_names(int tst)8479 static int test_ca_names(int tst)
8480 {
8481 int testresult = 1;
8482
8483 #ifndef OPENSSL_NO_TLS1_2
8484 testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
8485 #endif
8486 #ifndef OSSL_NO_USABLE_TLS1_3
8487 testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
8488 #endif
8489
8490 return testresult;
8491 }
8492
8493 #ifndef OPENSSL_NO_TLS1_2
8494 static const char *multiblock_cipherlist_data[]=
8495 {
8496 "AES128-SHA",
8497 "AES128-SHA256",
8498 "AES256-SHA",
8499 "AES256-SHA256",
8500 };
8501
8502 /* Reduce the fragment size - so the multiblock test buffer can be small */
8503 # define MULTIBLOCK_FRAGSIZE 512
8504
test_multiblock_write(int test_index)8505 static int test_multiblock_write(int test_index)
8506 {
8507 static const char *fetchable_ciphers[]=
8508 {
8509 "AES-128-CBC-HMAC-SHA1",
8510 "AES-128-CBC-HMAC-SHA256",
8511 "AES-256-CBC-HMAC-SHA1",
8512 "AES-256-CBC-HMAC-SHA256"
8513 };
8514 const char *cipherlist = multiblock_cipherlist_data[test_index];
8515 const SSL_METHOD *smeth = TLS_server_method();
8516 const SSL_METHOD *cmeth = TLS_client_method();
8517 int min_version = TLS1_VERSION;
8518 int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
8519 SSL_CTX *cctx = NULL, *sctx = NULL;
8520 SSL *clientssl = NULL, *serverssl = NULL;
8521 int testresult = 0;
8522
8523 /*
8524 * Choose a buffer large enough to perform a multi-block operation
8525 * i.e: write_len >= 4 * frag_size
8526 * 9 * is chosen so that multiple multiblocks are used + some leftover.
8527 */
8528 unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
8529 unsigned char buf[sizeof(msg)], *p = buf;
8530 size_t readbytes, written, len;
8531 EVP_CIPHER *ciph = NULL;
8532
8533 /*
8534 * Check if the cipher exists before attempting to use it since it only has
8535 * a hardware specific implementation.
8536 */
8537 ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
8538 if (ciph == NULL) {
8539 TEST_skip("Multiblock cipher is not available for %s", cipherlist);
8540 return 1;
8541 }
8542 EVP_CIPHER_free(ciph);
8543
8544 /* Set up a buffer with some data that will be sent to the client */
8545 RAND_bytes(msg, sizeof(msg));
8546
8547 if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
8548 max_version, &sctx, &cctx, cert,
8549 privkey)))
8550 goto end;
8551
8552 if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
8553 goto end;
8554
8555 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8556 NULL, NULL)))
8557 goto end;
8558
8559 /* settings to force it to use AES-CBC-HMAC_SHA */
8560 SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
8561 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
8562 goto end;
8563
8564 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8565 goto end;
8566
8567 if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8568 || !TEST_size_t_eq(written, sizeof(msg)))
8569 goto end;
8570
8571 len = written;
8572 while (len > 0) {
8573 if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
8574 goto end;
8575 p += readbytes;
8576 len -= readbytes;
8577 }
8578 if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
8579 goto end;
8580
8581 testresult = 1;
8582 end:
8583 SSL_free(serverssl);
8584 SSL_free(clientssl);
8585 SSL_CTX_free(sctx);
8586 SSL_CTX_free(cctx);
8587
8588 return testresult;
8589 }
8590 #endif /* OPENSSL_NO_TLS1_2 */
8591
test_session_timeout(int test)8592 static int test_session_timeout(int test)
8593 {
8594 /*
8595 * Test session ordering and timeout
8596 * Can't explicitly test performance of the new code,
8597 * but can test to see if the ordering of the sessions
8598 * are correct, and they they are removed as expected
8599 */
8600 SSL_SESSION *early = NULL;
8601 SSL_SESSION *middle = NULL;
8602 SSL_SESSION *late = NULL;
8603 SSL_CTX *ctx;
8604 int testresult = 0;
8605 long now = (long)time(NULL);
8606 #define TIMEOUT 10
8607
8608 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
8609 || !TEST_ptr(early = SSL_SESSION_new())
8610 || !TEST_ptr(middle = SSL_SESSION_new())
8611 || !TEST_ptr(late = SSL_SESSION_new()))
8612 goto end;
8613
8614 /* assign unique session ids */
8615 early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8616 memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
8617 middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8618 memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
8619 late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8620 memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
8621
8622 if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8623 || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8624 || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8625 goto end;
8626
8627 /* Make sure they are all added */
8628 if (!TEST_ptr(early->prev)
8629 || !TEST_ptr(middle->prev)
8630 || !TEST_ptr(late->prev))
8631 goto end;
8632
8633 if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
8634 || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
8635 || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
8636 goto end;
8637
8638 if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
8639 || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
8640 || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
8641 goto end;
8642
8643 /* Make sure they are all still there */
8644 if (!TEST_ptr(early->prev)
8645 || !TEST_ptr(middle->prev)
8646 || !TEST_ptr(late->prev))
8647 goto end;
8648
8649 /* Make sure they are in the expected order */
8650 if (!TEST_ptr_eq(late->next, middle)
8651 || !TEST_ptr_eq(middle->next, early)
8652 || !TEST_ptr_eq(early->prev, middle)
8653 || !TEST_ptr_eq(middle->prev, late))
8654 goto end;
8655
8656 /* This should remove "early" */
8657 SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
8658 if (!TEST_ptr_null(early->prev)
8659 || !TEST_ptr(middle->prev)
8660 || !TEST_ptr(late->prev))
8661 goto end;
8662
8663 /* This should remove "middle" */
8664 SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
8665 if (!TEST_ptr_null(early->prev)
8666 || !TEST_ptr_null(middle->prev)
8667 || !TEST_ptr(late->prev))
8668 goto end;
8669
8670 /* This should remove "late" */
8671 SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
8672 if (!TEST_ptr_null(early->prev)
8673 || !TEST_ptr_null(middle->prev)
8674 || !TEST_ptr_null(late->prev))
8675 goto end;
8676
8677 /* Add them back in again */
8678 if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8679 || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8680 || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8681 goto end;
8682
8683 /* Make sure they are all added */
8684 if (!TEST_ptr(early->prev)
8685 || !TEST_ptr(middle->prev)
8686 || !TEST_ptr(late->prev))
8687 goto end;
8688
8689 /* This should remove all of them */
8690 SSL_CTX_flush_sessions(ctx, 0);
8691 if (!TEST_ptr_null(early->prev)
8692 || !TEST_ptr_null(middle->prev)
8693 || !TEST_ptr_null(late->prev))
8694 goto end;
8695
8696 (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
8697 | SSL_CTX_get_session_cache_mode(ctx));
8698
8699 /* make sure |now| is NOT equal to the current time */
8700 now -= 10;
8701 if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
8702 || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8703 || !TEST_long_ne(SSL_SESSION_get_time(early), now))
8704 goto end;
8705
8706 testresult = 1;
8707 end:
8708 SSL_CTX_free(ctx);
8709 SSL_SESSION_free(early);
8710 SSL_SESSION_free(middle);
8711 SSL_SESSION_free(late);
8712 return testresult;
8713 }
8714
8715 /*
8716 * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
8717 * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
8718 * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
8719 * Test 3: Client does not set servername on initial handshake (TLSv1.2)
8720 * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
8721 * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
8722 * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
8723 * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
8724 * Test 8: Client does not set servername on initial handshake(TLSv1.3)
8725 * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
8726 */
test_servername(int tst)8727 static int test_servername(int tst)
8728 {
8729 SSL_CTX *cctx = NULL, *sctx = NULL;
8730 SSL *clientssl = NULL, *serverssl = NULL;
8731 int testresult = 0;
8732 SSL_SESSION *sess = NULL;
8733 const char *sexpectedhost = NULL, *cexpectedhost = NULL;
8734
8735 #ifdef OPENSSL_NO_TLS1_2
8736 if (tst <= 4)
8737 return 1;
8738 #endif
8739 #ifdef OSSL_NO_USABLE_TLS1_3
8740 if (tst >= 5)
8741 return 1;
8742 #endif
8743
8744 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8745 TLS_client_method(),
8746 TLS1_VERSION,
8747 (tst <= 4) ? TLS1_2_VERSION
8748 : TLS1_3_VERSION,
8749 &sctx, &cctx, cert, privkey))
8750 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8751 NULL, NULL)))
8752 goto end;
8753
8754 if (tst != 1 && tst != 6) {
8755 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
8756 hostname_cb)))
8757 goto end;
8758 }
8759
8760 if (tst != 3 && tst != 8) {
8761 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
8762 goto end;
8763 sexpectedhost = cexpectedhost = "goodhost";
8764 }
8765
8766 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8767 goto end;
8768
8769 if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
8770 cexpectedhost)
8771 || !TEST_str_eq(SSL_get_servername(serverssl,
8772 TLSEXT_NAMETYPE_host_name),
8773 sexpectedhost))
8774 goto end;
8775
8776 /* Now repeat with a resumption handshake */
8777
8778 if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
8779 || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
8780 || !TEST_true(SSL_SESSION_is_resumable(sess))
8781 || !TEST_int_eq(SSL_shutdown(serverssl), 0))
8782 goto end;
8783
8784 SSL_free(clientssl);
8785 SSL_free(serverssl);
8786 clientssl = serverssl = NULL;
8787
8788 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
8789 NULL)))
8790 goto end;
8791
8792 if (!TEST_true(SSL_set_session(clientssl, sess)))
8793 goto end;
8794
8795 sexpectedhost = cexpectedhost = "goodhost";
8796 if (tst == 2 || tst == 7) {
8797 /* Set an inconsistent hostname */
8798 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
8799 goto end;
8800 /*
8801 * In TLSv1.2 we expect the hostname from the original handshake, in
8802 * TLSv1.3 we expect the hostname from this handshake
8803 */
8804 if (tst == 7)
8805 sexpectedhost = cexpectedhost = "altgoodhost";
8806
8807 if (!TEST_str_eq(SSL_get_servername(clientssl,
8808 TLSEXT_NAMETYPE_host_name),
8809 "altgoodhost"))
8810 goto end;
8811 } else if (tst == 4 || tst == 9) {
8812 /*
8813 * A TLSv1.3 session does not associate a session with a servername,
8814 * but a TLSv1.2 session does.
8815 */
8816 if (tst == 9)
8817 sexpectedhost = cexpectedhost = NULL;
8818
8819 if (!TEST_str_eq(SSL_get_servername(clientssl,
8820 TLSEXT_NAMETYPE_host_name),
8821 cexpectedhost))
8822 goto end;
8823 } else {
8824 if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
8825 goto end;
8826 /*
8827 * In a TLSv1.2 resumption where the hostname was not acknowledged
8828 * we expect the hostname on the server to be empty. On the client we
8829 * return what was requested in this case.
8830 *
8831 * Similarly if the client didn't set a hostname on an original TLSv1.2
8832 * session but is now, the server hostname will be empty, but the client
8833 * is as we set it.
8834 */
8835 if (tst == 1 || tst == 3)
8836 sexpectedhost = NULL;
8837
8838 if (!TEST_str_eq(SSL_get_servername(clientssl,
8839 TLSEXT_NAMETYPE_host_name),
8840 "goodhost"))
8841 goto end;
8842 }
8843
8844 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8845 goto end;
8846
8847 if (!TEST_true(SSL_session_reused(clientssl))
8848 || !TEST_true(SSL_session_reused(serverssl))
8849 || !TEST_str_eq(SSL_get_servername(clientssl,
8850 TLSEXT_NAMETYPE_host_name),
8851 cexpectedhost)
8852 || !TEST_str_eq(SSL_get_servername(serverssl,
8853 TLSEXT_NAMETYPE_host_name),
8854 sexpectedhost))
8855 goto end;
8856
8857 testresult = 1;
8858
8859 end:
8860 SSL_SESSION_free(sess);
8861 SSL_free(serverssl);
8862 SSL_free(clientssl);
8863 SSL_CTX_free(sctx);
8864 SSL_CTX_free(cctx);
8865
8866 return testresult;
8867 }
8868
8869 #if !defined(OPENSSL_NO_EC) \
8870 && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
8871 /*
8872 * Test that if signature algorithms are not available, then we do not offer or
8873 * accept them.
8874 * Test 0: Two RSA sig algs available: both RSA sig algs shared
8875 * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
8876 * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
8877 * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
8878 * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
8879 * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
8880 */
test_sigalgs_available(int idx)8881 static int test_sigalgs_available(int idx)
8882 {
8883 SSL_CTX *cctx = NULL, *sctx = NULL;
8884 SSL *clientssl = NULL, *serverssl = NULL;
8885 int testresult = 0;
8886 OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
8887 OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
8888 OSSL_PROVIDER *filterprov = NULL;
8889 int sig, hash;
8890
8891 if (!TEST_ptr(tmpctx))
8892 goto end;
8893
8894 if (idx != 0 && idx != 3) {
8895 if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
8896 filter_provider_init)))
8897 goto end;
8898
8899 filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
8900 if (!TEST_ptr(filterprov))
8901 goto end;
8902
8903 if (idx < 3) {
8904 /*
8905 * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
8906 * or accepted for the peer that uses this libctx. Note that libssl
8907 * *requires* SHA2-256 to be available so we cannot disable that. We
8908 * also need SHA1 for our certificate.
8909 */
8910 if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
8911 "SHA2-256:SHA1")))
8912 goto end;
8913 } else {
8914 if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
8915 "ECDSA"))
8916 || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
8917 "EC:X25519:X448")))
8918 goto end;
8919 }
8920
8921 if (idx == 1 || idx == 4)
8922 clientctx = tmpctx;
8923 else
8924 serverctx = tmpctx;
8925 }
8926
8927 cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
8928 sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
8929 if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
8930 goto end;
8931
8932 if (idx != 5) {
8933 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8934 TLS_client_method(),
8935 TLS1_VERSION,
8936 0,
8937 &sctx, &cctx, cert, privkey)))
8938 goto end;
8939 } else {
8940 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8941 TLS_client_method(),
8942 TLS1_VERSION,
8943 0,
8944 &sctx, &cctx, cert2, privkey2)))
8945 goto end;
8946 }
8947
8948 /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
8949 if (idx < 4) {
8950 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
8951 "ECDHE-RSA-AES128-GCM-SHA256")))
8952 goto end;
8953 } else {
8954 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
8955 "ECDHE-ECDSA-AES128-GCM-SHA256")))
8956 goto end;
8957 }
8958
8959 if (idx < 3) {
8960 if (!SSL_CTX_set1_sigalgs_list(cctx,
8961 "rsa_pss_rsae_sha384"
8962 ":rsa_pss_rsae_sha256")
8963 || !SSL_CTX_set1_sigalgs_list(sctx,
8964 "rsa_pss_rsae_sha384"
8965 ":rsa_pss_rsae_sha256"))
8966 goto end;
8967 } else {
8968 if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
8969 || !SSL_CTX_set1_sigalgs_list(sctx,
8970 "rsa_pss_rsae_sha256:ECDSA+SHA256"))
8971 goto end;
8972 }
8973
8974 if (idx != 5
8975 && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
8976 SSL_FILETYPE_PEM), 1)
8977 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
8978 privkey2,
8979 SSL_FILETYPE_PEM), 1)
8980 || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
8981 goto end;
8982
8983 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8984 NULL, NULL)))
8985 goto end;
8986
8987 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8988 goto end;
8989
8990 /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
8991 if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
8992 NULL, NULL),
8993 (idx == 0 || idx == 3) ? 2 : 1))
8994 goto end;
8995
8996 if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
8997 goto end;
8998
8999 if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
9000 : NID_rsassaPss))
9001 goto end;
9002
9003 testresult = filter_provider_check_clean_finish();
9004
9005 end:
9006 SSL_free(serverssl);
9007 SSL_free(clientssl);
9008 SSL_CTX_free(sctx);
9009 SSL_CTX_free(cctx);
9010 OSSL_PROVIDER_unload(filterprov);
9011 OSSL_LIB_CTX_free(tmpctx);
9012
9013 return testresult;
9014 }
9015 #endif /*
9016 * !defined(OPENSSL_NO_EC) \
9017 * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9018 */
9019
9020 #ifndef OPENSSL_NO_TLS1_3
9021 /* This test can run in TLSv1.3 even if ec and dh are disabled */
test_pluggable_group(int idx)9022 static int test_pluggable_group(int idx)
9023 {
9024 SSL_CTX *cctx = NULL, *sctx = NULL;
9025 SSL *clientssl = NULL, *serverssl = NULL;
9026 int testresult = 0;
9027 OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
9028 /* Check that we are not impacted by a provider without any groups */
9029 OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
9030 const char *group_name = idx == 0 ? "xorgroup" : "xorkemgroup";
9031
9032 if (!TEST_ptr(tlsprov))
9033 goto end;
9034
9035 if (legacyprov == NULL) {
9036 /*
9037 * In this case we assume we've been built with "no-legacy" and skip
9038 * this test (there is no OPENSSL_NO_LEGACY)
9039 */
9040 testresult = 1;
9041 goto end;
9042 }
9043
9044 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9045 TLS_client_method(),
9046 TLS1_3_VERSION,
9047 TLS1_3_VERSION,
9048 &sctx, &cctx, cert, privkey))
9049 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9050 NULL, NULL)))
9051 goto end;
9052
9053 if (!TEST_true(SSL_set1_groups_list(serverssl, group_name))
9054 || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
9055 goto end;
9056
9057 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9058 goto end;
9059
9060 if (!TEST_str_eq(group_name,
9061 SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
9062 goto end;
9063
9064 testresult = 1;
9065
9066 end:
9067 SSL_free(serverssl);
9068 SSL_free(clientssl);
9069 SSL_CTX_free(sctx);
9070 SSL_CTX_free(cctx);
9071 OSSL_PROVIDER_unload(tlsprov);
9072 OSSL_PROVIDER_unload(legacyprov);
9073
9074 return testresult;
9075 }
9076 #endif
9077
9078 #ifndef OPENSSL_NO_TLS1_2
test_ssl_dup(void)9079 static int test_ssl_dup(void)
9080 {
9081 SSL_CTX *cctx = NULL, *sctx = NULL;
9082 SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
9083 int testresult = 0;
9084 BIO *rbio = NULL, *wbio = NULL;
9085
9086 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9087 TLS_client_method(),
9088 0,
9089 0,
9090 &sctx, &cctx, cert, privkey)))
9091 goto end;
9092
9093 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9094 NULL, NULL)))
9095 goto end;
9096
9097 if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
9098 || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
9099 goto end;
9100
9101 client2ssl = SSL_dup(clientssl);
9102 rbio = SSL_get_rbio(clientssl);
9103 if (!TEST_ptr(rbio)
9104 || !TEST_true(BIO_up_ref(rbio)))
9105 goto end;
9106 SSL_set0_rbio(client2ssl, rbio);
9107 rbio = NULL;
9108
9109 wbio = SSL_get_wbio(clientssl);
9110 if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
9111 goto end;
9112 SSL_set0_wbio(client2ssl, wbio);
9113 rbio = NULL;
9114
9115 if (!TEST_ptr(client2ssl)
9116 /* Handshake not started so pointers should be different */
9117 || !TEST_ptr_ne(clientssl, client2ssl))
9118 goto end;
9119
9120 if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
9121 || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
9122 goto end;
9123
9124 if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
9125 goto end;
9126
9127 SSL_free(clientssl);
9128 clientssl = SSL_dup(client2ssl);
9129 if (!TEST_ptr(clientssl)
9130 /* Handshake has finished so pointers should be the same */
9131 || !TEST_ptr_eq(clientssl, client2ssl))
9132 goto end;
9133
9134 testresult = 1;
9135
9136 end:
9137 SSL_free(serverssl);
9138 SSL_free(clientssl);
9139 SSL_free(client2ssl);
9140 SSL_CTX_free(sctx);
9141 SSL_CTX_free(cctx);
9142
9143 return testresult;
9144 }
9145
9146 # ifndef OPENSSL_NO_DH
9147
9148 static EVP_PKEY *tmp_dh_params = NULL;
9149
9150 /* Helper function for the test_set_tmp_dh() tests */
get_tmp_dh_params(void)9151 static EVP_PKEY *get_tmp_dh_params(void)
9152 {
9153 if (tmp_dh_params == NULL) {
9154 BIGNUM *p = NULL;
9155 OSSL_PARAM_BLD *tmpl = NULL;
9156 EVP_PKEY_CTX *pctx = NULL;
9157 OSSL_PARAM *params = NULL;
9158 EVP_PKEY *dhpkey = NULL;
9159
9160 p = BN_get_rfc3526_prime_2048(NULL);
9161 if (!TEST_ptr(p))
9162 goto end;
9163
9164 pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
9165 if (!TEST_ptr(pctx)
9166 || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
9167 goto end;
9168
9169 tmpl = OSSL_PARAM_BLD_new();
9170 if (!TEST_ptr(tmpl)
9171 || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
9172 OSSL_PKEY_PARAM_FFC_P,
9173 p))
9174 || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
9175 OSSL_PKEY_PARAM_FFC_G,
9176 2)))
9177 goto end;
9178
9179 params = OSSL_PARAM_BLD_to_param(tmpl);
9180 if (!TEST_ptr(params)
9181 || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
9182 EVP_PKEY_KEY_PARAMETERS,
9183 params), 1))
9184 goto end;
9185
9186 tmp_dh_params = dhpkey;
9187 end:
9188 BN_free(p);
9189 EVP_PKEY_CTX_free(pctx);
9190 OSSL_PARAM_BLD_free(tmpl);
9191 OSSL_PARAM_free(params);
9192 }
9193
9194 if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
9195 return NULL;
9196
9197 return tmp_dh_params;
9198 }
9199
9200 # ifndef OPENSSL_NO_DEPRECATED_3_0
9201 /* Callback used by test_set_tmp_dh() */
tmp_dh_callback(SSL * s,int is_export,int keylen)9202 static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
9203 {
9204 EVP_PKEY *dhpkey = get_tmp_dh_params();
9205 DH *ret = NULL;
9206
9207 if (!TEST_ptr(dhpkey))
9208 return NULL;
9209
9210 /*
9211 * libssl does not free the returned DH, so we free it now knowing that even
9212 * after we free dhpkey, there will still be a reference to the owning
9213 * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
9214 * of time we need it for.
9215 */
9216 ret = EVP_PKEY_get1_DH(dhpkey);
9217 DH_free(ret);
9218
9219 EVP_PKEY_free(dhpkey);
9220
9221 return ret;
9222 }
9223 # endif
9224
9225 /*
9226 * Test the various methods for setting temporary DH parameters
9227 *
9228 * Test 0: Default (no auto) setting
9229 * Test 1: Explicit SSL_CTX auto off
9230 * Test 2: Explicit SSL auto off
9231 * Test 3: Explicit SSL_CTX auto on
9232 * Test 4: Explicit SSL auto on
9233 * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
9234 * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
9235 *
9236 * The following are testing deprecated APIs, so we only run them if available
9237 * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
9238 * Test 8: Explicit SSL auto off, custom DH params via DH
9239 * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
9240 * Test 10: Explicit SSL auto off, custom DH params via callback
9241 */
test_set_tmp_dh(int idx)9242 static int test_set_tmp_dh(int idx)
9243 {
9244 SSL_CTX *cctx = NULL, *sctx = NULL;
9245 SSL *clientssl = NULL, *serverssl = NULL;
9246 int testresult = 0;
9247 int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
9248 int expected = (idx <= 2) ? 0 : 1;
9249 EVP_PKEY *dhpkey = NULL;
9250 # ifndef OPENSSL_NO_DEPRECATED_3_0
9251 DH *dh = NULL;
9252 # else
9253
9254 if (idx >= 7)
9255 return 1;
9256 # endif
9257
9258 if (idx >= 5 && idx <= 8) {
9259 dhpkey = get_tmp_dh_params();
9260 if (!TEST_ptr(dhpkey))
9261 goto end;
9262 }
9263 # ifndef OPENSSL_NO_DEPRECATED_3_0
9264 if (idx == 7 || idx == 8) {
9265 dh = EVP_PKEY_get1_DH(dhpkey);
9266 if (!TEST_ptr(dh))
9267 goto end;
9268 }
9269 # endif
9270
9271 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9272 TLS_client_method(),
9273 0,
9274 0,
9275 &sctx, &cctx, cert, privkey)))
9276 goto end;
9277
9278 if ((idx & 1) == 1) {
9279 if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
9280 goto end;
9281 }
9282
9283 if (idx == 5) {
9284 if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
9285 goto end;
9286 dhpkey = NULL;
9287 }
9288 # ifndef OPENSSL_NO_DEPRECATED_3_0
9289 else if (idx == 7) {
9290 if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
9291 goto end;
9292 } else if (idx == 9) {
9293 SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
9294 }
9295 # endif
9296
9297 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9298 NULL, NULL)))
9299 goto end;
9300
9301 if ((idx & 1) == 0 && idx != 0) {
9302 if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
9303 goto end;
9304 }
9305 if (idx == 6) {
9306 if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
9307 goto end;
9308 dhpkey = NULL;
9309 }
9310 # ifndef OPENSSL_NO_DEPRECATED_3_0
9311 else if (idx == 8) {
9312 if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
9313 goto end;
9314 } else if (idx == 10) {
9315 SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
9316 }
9317 # endif
9318
9319 if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9320 || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9321 || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
9322 goto end;
9323
9324 /*
9325 * If autoon then we should succeed. Otherwise we expect failure because
9326 * there are no parameters
9327 */
9328 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
9329 SSL_ERROR_NONE), expected))
9330 goto end;
9331
9332 testresult = 1;
9333
9334 end:
9335 # ifndef OPENSSL_NO_DEPRECATED_3_0
9336 DH_free(dh);
9337 # endif
9338 SSL_free(serverssl);
9339 SSL_free(clientssl);
9340 SSL_CTX_free(sctx);
9341 SSL_CTX_free(cctx);
9342 EVP_PKEY_free(dhpkey);
9343
9344 return testresult;
9345 }
9346
9347 /*
9348 * Test the auto DH keys are appropriately sized
9349 */
test_dh_auto(int idx)9350 static int test_dh_auto(int idx)
9351 {
9352 SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
9353 SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9354 SSL *clientssl = NULL, *serverssl = NULL;
9355 int testresult = 0;
9356 EVP_PKEY *tmpkey = NULL;
9357 char *thiscert = NULL, *thiskey = NULL;
9358 size_t expdhsize = 0;
9359 const char *ciphersuite = "DHE-RSA-AES128-SHA";
9360
9361 if (!TEST_ptr(sctx) || !TEST_ptr(cctx))
9362 goto end;
9363
9364 switch (idx) {
9365 case 0:
9366 /* The FIPS provider doesn't support this DH size - so we ignore it */
9367 if (is_fips) {
9368 testresult = 1;
9369 goto end;
9370 }
9371 thiscert = cert1024;
9372 thiskey = privkey1024;
9373 expdhsize = 1024;
9374 SSL_CTX_set_security_level(sctx, 1);
9375 SSL_CTX_set_security_level(cctx, 1);
9376 break;
9377 case 1:
9378 /* 2048 bit prime */
9379 thiscert = cert;
9380 thiskey = privkey;
9381 expdhsize = 2048;
9382 break;
9383 case 2:
9384 thiscert = cert3072;
9385 thiskey = privkey3072;
9386 expdhsize = 3072;
9387 break;
9388 case 3:
9389 thiscert = cert4096;
9390 thiskey = privkey4096;
9391 expdhsize = 4096;
9392 break;
9393 case 4:
9394 thiscert = cert8192;
9395 thiskey = privkey8192;
9396 expdhsize = 8192;
9397 break;
9398 /* No certificate cases */
9399 case 5:
9400 /* The FIPS provider doesn't support this DH size - so we ignore it */
9401 if (is_fips) {
9402 testresult = 1;
9403 goto end;
9404 }
9405 ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
9406 expdhsize = 1024;
9407 break;
9408 case 6:
9409 ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
9410 expdhsize = 3072;
9411 break;
9412 default:
9413 TEST_error("Invalid text index");
9414 goto end;
9415 }
9416
9417 if (!TEST_true(create_ssl_ctx_pair(libctx, NULL,
9418 NULL,
9419 0,
9420 0,
9421 &sctx, &cctx, thiscert, thiskey)))
9422 goto end;
9423
9424 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9425 NULL, NULL)))
9426 goto end;
9427
9428 if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
9429 || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9430 || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9431 || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
9432 || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
9433 goto end;
9434
9435 /*
9436 * Send the server's first flight. At this point the server has created the
9437 * temporary DH key but hasn't finished using it yet. Once used it is
9438 * removed, so we cannot test it.
9439 */
9440 if (!TEST_int_le(SSL_connect(clientssl), 0)
9441 || !TEST_int_le(SSL_accept(serverssl), 0))
9442 goto end;
9443
9444 if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
9445 goto end;
9446 if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
9447 goto end;
9448
9449 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9450 goto end;
9451
9452 testresult = 1;
9453
9454 end:
9455 SSL_free(serverssl);
9456 SSL_free(clientssl);
9457 SSL_CTX_free(sctx);
9458 SSL_CTX_free(cctx);
9459 EVP_PKEY_free(tmpkey);
9460
9461 return testresult;
9462
9463 }
9464 # endif /* OPENSSL_NO_DH */
9465 #endif /* OPENSSL_NO_TLS1_2 */
9466
9467 #ifndef OSSL_NO_USABLE_TLS1_3
9468 /*
9469 * Test that setting an SNI callback works with TLSv1.3. Specifically we check
9470 * that it works even without a certificate configured for the original
9471 * SSL_CTX
9472 */
test_sni_tls13(void)9473 static int test_sni_tls13(void)
9474 {
9475 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
9476 SSL *clientssl = NULL, *serverssl = NULL;
9477 int testresult = 0;
9478
9479 /* Reset callback counter */
9480 snicb = 0;
9481
9482 /* Create an initial SSL_CTX with no certificate configured */
9483 sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9484 if (!TEST_ptr(sctx))
9485 goto end;
9486 /* Require TLSv1.3 as a minimum */
9487 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9488 TLS_client_method(), TLS1_3_VERSION, 0,
9489 &sctx2, &cctx, cert, privkey)))
9490 goto end;
9491
9492 /* Set up SNI */
9493 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
9494 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
9495 goto end;
9496
9497 /*
9498 * Connection should still succeed because the final SSL_CTX has the right
9499 * certificates configured.
9500 */
9501 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9502 &clientssl, NULL, NULL))
9503 || !TEST_true(create_ssl_connection(serverssl, clientssl,
9504 SSL_ERROR_NONE)))
9505 goto end;
9506
9507 /* We should have had the SNI callback called exactly once */
9508 if (!TEST_int_eq(snicb, 1))
9509 goto end;
9510
9511 testresult = 1;
9512
9513 end:
9514 SSL_free(serverssl);
9515 SSL_free(clientssl);
9516 SSL_CTX_free(sctx2);
9517 SSL_CTX_free(sctx);
9518 SSL_CTX_free(cctx);
9519 return testresult;
9520 }
9521
9522 /*
9523 * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
9524 * 0 = TLSv1.2
9525 * 1 = TLSv1.3
9526 */
test_ticket_lifetime(int idx)9527 static int test_ticket_lifetime(int idx)
9528 {
9529 SSL_CTX *cctx = NULL, *sctx = NULL;
9530 SSL *clientssl = NULL, *serverssl = NULL;
9531 int testresult = 0;
9532 int version = TLS1_3_VERSION;
9533
9534 #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
9535 #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
9536
9537 if (idx == 0) {
9538 #ifdef OPENSSL_NO_TLS1_2
9539 return TEST_skip("TLS 1.2 is disabled.");
9540 #else
9541 version = TLS1_2_VERSION;
9542 #endif
9543 }
9544
9545 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9546 TLS_client_method(), version, version,
9547 &sctx, &cctx, cert, privkey)))
9548 goto end;
9549
9550 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9551 &clientssl, NULL, NULL)))
9552 goto end;
9553
9554 /*
9555 * Set the timeout to be more than 1 week
9556 * make sure the returned value is the default
9557 */
9558 if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
9559 SSL_get_default_timeout(serverssl)))
9560 goto end;
9561
9562 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9563 goto end;
9564
9565 if (idx == 0) {
9566 /* TLSv1.2 uses the set value */
9567 if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
9568 goto end;
9569 } else {
9570 /* TLSv1.3 uses the limited value */
9571 if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
9572 goto end;
9573 }
9574 testresult = 1;
9575
9576 end:
9577 SSL_free(serverssl);
9578 SSL_free(clientssl);
9579 SSL_CTX_free(sctx);
9580 SSL_CTX_free(cctx);
9581 return testresult;
9582 }
9583 #endif
9584 /*
9585 * Test that setting an ALPN does not violate RFC
9586 */
test_set_alpn(void)9587 static int test_set_alpn(void)
9588 {
9589 SSL_CTX *ctx = NULL;
9590 SSL *ssl = NULL;
9591 int testresult = 0;
9592
9593 unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
9594 unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
9595 unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
9596 unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
9597 unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
9598 unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
9599
9600 /* Create an initial SSL_CTX with no certificate configured */
9601 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9602 if (!TEST_ptr(ctx))
9603 goto end;
9604
9605 /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
9606 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
9607 goto end;
9608 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
9609 goto end;
9610 if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
9611 goto end;
9612 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
9613 goto end;
9614 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
9615 goto end;
9616 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
9617 goto end;
9618 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
9619 goto end;
9620 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
9621 goto end;
9622 if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
9623 goto end;
9624
9625 ssl = SSL_new(ctx);
9626 if (!TEST_ptr(ssl))
9627 goto end;
9628
9629 if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
9630 goto end;
9631 if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
9632 goto end;
9633 if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
9634 goto end;
9635 if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
9636 goto end;
9637 if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
9638 goto end;
9639 if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
9640 goto end;
9641 if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
9642 goto end;
9643 if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
9644 goto end;
9645 if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
9646 goto end;
9647
9648 testresult = 1;
9649
9650 end:
9651 SSL_free(ssl);
9652 SSL_CTX_free(ctx);
9653 return testresult;
9654 }
9655
9656 /*
9657 * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
9658 */
test_set_verify_cert_store_ssl_ctx(void)9659 static int test_set_verify_cert_store_ssl_ctx(void)
9660 {
9661 SSL_CTX *ctx = NULL;
9662 int testresult = 0;
9663 X509_STORE *store = NULL, *new_store = NULL,
9664 *cstore = NULL, *new_cstore = NULL;
9665
9666 /* Create an initial SSL_CTX. */
9667 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9668 if (!TEST_ptr(ctx))
9669 goto end;
9670
9671 /* Retrieve verify store pointer. */
9672 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9673 goto end;
9674
9675 /* Retrieve chain store pointer. */
9676 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9677 goto end;
9678
9679 /* We haven't set any yet, so this should be NULL. */
9680 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9681 goto end;
9682
9683 /* Create stores. We use separate stores so pointers are different. */
9684 new_store = X509_STORE_new();
9685 if (!TEST_ptr(new_store))
9686 goto end;
9687
9688 new_cstore = X509_STORE_new();
9689 if (!TEST_ptr(new_cstore))
9690 goto end;
9691
9692 /* Set stores. */
9693 if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
9694 goto end;
9695
9696 if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
9697 goto end;
9698
9699 /* Should be able to retrieve the same pointer. */
9700 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9701 goto end;
9702
9703 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9704 goto end;
9705
9706 if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
9707 goto end;
9708
9709 /* Should be able to unset again. */
9710 if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
9711 goto end;
9712
9713 if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
9714 goto end;
9715
9716 /* Should now be NULL. */
9717 if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
9718 goto end;
9719
9720 if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
9721 goto end;
9722
9723 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9724 goto end;
9725
9726 testresult = 1;
9727
9728 end:
9729 X509_STORE_free(new_store);
9730 X509_STORE_free(new_cstore);
9731 SSL_CTX_free(ctx);
9732 return testresult;
9733 }
9734
9735 /*
9736 * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
9737 */
test_set_verify_cert_store_ssl(void)9738 static int test_set_verify_cert_store_ssl(void)
9739 {
9740 SSL_CTX *ctx = NULL;
9741 SSL *ssl = NULL;
9742 int testresult = 0;
9743 X509_STORE *store = NULL, *new_store = NULL,
9744 *cstore = NULL, *new_cstore = NULL;
9745
9746 /* Create an initial SSL_CTX. */
9747 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9748 if (!TEST_ptr(ctx))
9749 goto end;
9750
9751 /* Create an SSL object. */
9752 ssl = SSL_new(ctx);
9753 if (!TEST_ptr(ssl))
9754 goto end;
9755
9756 /* Retrieve verify store pointer. */
9757 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9758 goto end;
9759
9760 /* Retrieve chain store pointer. */
9761 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9762 goto end;
9763
9764 /* We haven't set any yet, so this should be NULL. */
9765 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9766 goto end;
9767
9768 /* Create stores. We use separate stores so pointers are different. */
9769 new_store = X509_STORE_new();
9770 if (!TEST_ptr(new_store))
9771 goto end;
9772
9773 new_cstore = X509_STORE_new();
9774 if (!TEST_ptr(new_cstore))
9775 goto end;
9776
9777 /* Set stores. */
9778 if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
9779 goto end;
9780
9781 if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
9782 goto end;
9783
9784 /* Should be able to retrieve the same pointer. */
9785 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9786 goto end;
9787
9788 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9789 goto end;
9790
9791 if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
9792 goto end;
9793
9794 /* Should be able to unset again. */
9795 if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
9796 goto end;
9797
9798 if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
9799 goto end;
9800
9801 /* Should now be NULL. */
9802 if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
9803 goto end;
9804
9805 if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
9806 goto end;
9807
9808 if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
9809 goto end;
9810
9811 testresult = 1;
9812
9813 end:
9814 X509_STORE_free(new_store);
9815 X509_STORE_free(new_cstore);
9816 SSL_free(ssl);
9817 SSL_CTX_free(ctx);
9818 return testresult;
9819 }
9820
9821
test_inherit_verify_param(void)9822 static int test_inherit_verify_param(void)
9823 {
9824 int testresult = 0;
9825
9826 SSL_CTX *ctx = NULL;
9827 X509_VERIFY_PARAM *cp = NULL;
9828 SSL *ssl = NULL;
9829 X509_VERIFY_PARAM *sp = NULL;
9830 int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
9831
9832 ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9833 if (!TEST_ptr(ctx))
9834 goto end;
9835
9836 cp = SSL_CTX_get0_param(ctx);
9837 if (!TEST_ptr(cp))
9838 goto end;
9839 if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
9840 goto end;
9841
9842 X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
9843
9844 ssl = SSL_new(ctx);
9845 if (!TEST_ptr(ssl))
9846 goto end;
9847
9848 sp = SSL_get0_param(ssl);
9849 if (!TEST_ptr(sp))
9850 goto end;
9851 if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
9852 goto end;
9853
9854 testresult = 1;
9855
9856 end:
9857 SSL_free(ssl);
9858 SSL_CTX_free(ctx);
9859
9860 return testresult;
9861 }
9862
test_load_dhfile(void)9863 static int test_load_dhfile(void)
9864 {
9865 #ifndef OPENSSL_NO_DH
9866 int testresult = 0;
9867
9868 SSL_CTX *ctx = NULL;
9869 SSL_CONF_CTX *cctx = NULL;
9870
9871 if (dhfile == NULL)
9872 return 1;
9873
9874 if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
9875 || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
9876 goto end;
9877
9878 SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
9879 SSL_CONF_CTX_set_flags(cctx,
9880 SSL_CONF_FLAG_CERTIFICATE
9881 | SSL_CONF_FLAG_SERVER
9882 | SSL_CONF_FLAG_FILE);
9883
9884 if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
9885 goto end;
9886
9887 testresult = 1;
9888 end:
9889 SSL_CONF_CTX_free(cctx);
9890 SSL_CTX_free(ctx);
9891
9892 return testresult;
9893 #else
9894 return TEST_skip("DH not supported by this build");
9895 #endif
9896 }
9897
9898 #ifndef OSSL_NO_USABLE_TLS1_3
9899 /* Test that read_ahead works across a key change */
test_read_ahead_key_change(void)9900 static int test_read_ahead_key_change(void)
9901 {
9902 SSL_CTX *cctx = NULL, *sctx = NULL;
9903 SSL *clientssl = NULL, *serverssl = NULL;
9904 int testresult = 0;
9905 char *msg = "Hello World";
9906 size_t written, readbytes;
9907 char buf[80];
9908 int i;
9909
9910 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9911 TLS_client_method(), TLS1_3_VERSION, 0,
9912 &sctx, &cctx, cert, privkey)))
9913 goto end;
9914
9915 SSL_CTX_set_read_ahead(sctx, 1);
9916
9917 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9918 &clientssl, NULL, NULL)))
9919 goto end;
9920
9921 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9922 goto end;
9923
9924 /* Write some data, send a key update, write more data */
9925 if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
9926 || !TEST_size_t_eq(written, strlen(msg)))
9927 goto end;
9928
9929 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
9930 goto end;
9931
9932 if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
9933 || !TEST_size_t_eq(written, strlen(msg)))
9934 goto end;
9935
9936 /*
9937 * Since read_ahead is on the first read below should read the record with
9938 * the first app data, the second record with the key update message, and
9939 * the third record with the app data all in one go. We should be able to
9940 * still process the read_ahead data correctly even though it crosses
9941 * epochs
9942 */
9943 for (i = 0; i < 2; i++) {
9944 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
9945 &readbytes)))
9946 goto end;
9947
9948 buf[readbytes] = '\0';
9949 if (!TEST_str_eq(buf, msg))
9950 goto end;
9951 }
9952
9953 testresult = 1;
9954
9955 end:
9956 SSL_free(serverssl);
9957 SSL_free(clientssl);
9958 SSL_CTX_free(sctx);
9959 SSL_CTX_free(cctx);
9960 return testresult;
9961 }
9962 #endif /* OSSL_NO_USABLE_TLS1_3 */
9963
9964 OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
9965
setup_tests(void)9966 int setup_tests(void)
9967 {
9968 char *modulename;
9969 char *configfile;
9970
9971 libctx = OSSL_LIB_CTX_new();
9972 if (!TEST_ptr(libctx))
9973 return 0;
9974
9975 defctxnull = OSSL_PROVIDER_load(NULL, "null");
9976
9977 /*
9978 * Verify that the default and fips providers in the default libctx are not
9979 * available
9980 */
9981 if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
9982 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
9983 return 0;
9984
9985 if (!test_skip_common_options()) {
9986 TEST_error("Error parsing test options\n");
9987 return 0;
9988 }
9989
9990 if (!TEST_ptr(certsdir = test_get_argument(0))
9991 || !TEST_ptr(srpvfile = test_get_argument(1))
9992 || !TEST_ptr(tmpfilename = test_get_argument(2))
9993 || !TEST_ptr(modulename = test_get_argument(3))
9994 || !TEST_ptr(configfile = test_get_argument(4))
9995 || !TEST_ptr(dhfile = test_get_argument(5)))
9996 return 0;
9997
9998 if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
9999 return 0;
10000
10001 /* Check we have the expected provider available */
10002 if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
10003 return 0;
10004
10005 /* Check the default provider is not available */
10006 if (strcmp(modulename, "default") != 0
10007 && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
10008 return 0;
10009
10010 if (strcmp(modulename, "fips") == 0)
10011 is_fips = 1;
10012
10013 /*
10014 * We add, but don't load the test "tls-provider". We'll load it when we
10015 * need it.
10016 */
10017 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
10018 tls_provider_init)))
10019 return 0;
10020
10021
10022 if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
10023 #ifdef OPENSSL_NO_CRYPTO_MDEBUG
10024 TEST_error("not supported in this build");
10025 return 0;
10026 #else
10027 int i, mcount, rcount, fcount;
10028
10029 for (i = 0; i < 4; i++)
10030 test_export_key_mat(i);
10031 CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
10032 test_printf_stdout("malloc %d realloc %d free %d\n",
10033 mcount, rcount, fcount);
10034 return 1;
10035 #endif
10036 }
10037
10038 cert = test_mk_file_path(certsdir, "servercert.pem");
10039 if (cert == NULL)
10040 goto err;
10041
10042 privkey = test_mk_file_path(certsdir, "serverkey.pem");
10043 if (privkey == NULL)
10044 goto err;
10045
10046 cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
10047 if (cert2 == NULL)
10048 goto err;
10049
10050 privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
10051 if (privkey2 == NULL)
10052 goto err;
10053
10054 cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
10055 if (cert1024 == NULL)
10056 goto err;
10057
10058 privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
10059 if (privkey1024 == NULL)
10060 goto err;
10061
10062 cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
10063 if (cert3072 == NULL)
10064 goto err;
10065
10066 privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
10067 if (privkey3072 == NULL)
10068 goto err;
10069
10070 cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
10071 if (cert4096 == NULL)
10072 goto err;
10073
10074 privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
10075 if (privkey4096 == NULL)
10076 goto err;
10077
10078 cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
10079 if (cert8192 == NULL)
10080 goto err;
10081
10082 privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
10083 if (privkey8192 == NULL)
10084 goto err;
10085
10086 #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
10087 # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
10088 ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
10089 ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
10090 # endif
10091 #endif
10092 ADD_TEST(test_large_message_tls);
10093 ADD_TEST(test_large_message_tls_read_ahead);
10094 #ifndef OPENSSL_NO_DTLS
10095 ADD_TEST(test_large_message_dtls);
10096 #endif
10097 ADD_TEST(test_cleanse_plaintext);
10098 #ifndef OPENSSL_NO_OCSP
10099 ADD_TEST(test_tlsext_status_type);
10100 #endif
10101 ADD_TEST(test_session_with_only_int_cache);
10102 ADD_TEST(test_session_with_only_ext_cache);
10103 ADD_TEST(test_session_with_both_cache);
10104 ADD_TEST(test_session_wo_ca_names);
10105 #ifndef OSSL_NO_USABLE_TLS1_3
10106 ADD_ALL_TESTS(test_stateful_tickets, 3);
10107 ADD_ALL_TESTS(test_stateless_tickets, 3);
10108 ADD_TEST(test_psk_tickets);
10109 ADD_ALL_TESTS(test_extra_tickets, 6);
10110 #endif
10111 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
10112 ADD_TEST(test_ssl_bio_pop_next_bio);
10113 ADD_TEST(test_ssl_bio_pop_ssl_bio);
10114 ADD_TEST(test_ssl_bio_change_rbio);
10115 ADD_TEST(test_ssl_bio_change_wbio);
10116 #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
10117 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
10118 ADD_TEST(test_keylog);
10119 #endif
10120 #ifndef OSSL_NO_USABLE_TLS1_3
10121 ADD_TEST(test_keylog_no_master_key);
10122 #endif
10123 ADD_TEST(test_client_cert_verify_cb);
10124 ADD_TEST(test_ssl_build_cert_chain);
10125 ADD_TEST(test_ssl_ctx_build_cert_chain);
10126 #ifndef OPENSSL_NO_TLS1_2
10127 ADD_TEST(test_client_hello_cb);
10128 ADD_TEST(test_no_ems);
10129 ADD_TEST(test_ccs_change_cipher);
10130 #endif
10131 #ifndef OSSL_NO_USABLE_TLS1_3
10132 ADD_ALL_TESTS(test_early_data_read_write, 3);
10133 /*
10134 * We don't do replay tests for external PSK. Replay protection isn't used
10135 * in that scenario.
10136 */
10137 ADD_ALL_TESTS(test_early_data_replay, 2);
10138 ADD_ALL_TESTS(test_early_data_skip, 3);
10139 ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
10140 ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
10141 ADD_ALL_TESTS(test_early_data_skip_abort, 3);
10142 ADD_ALL_TESTS(test_early_data_not_sent, 3);
10143 ADD_ALL_TESTS(test_early_data_psk, 8);
10144 ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
10145 ADD_ALL_TESTS(test_early_data_not_expected, 3);
10146 # ifndef OPENSSL_NO_TLS1_2
10147 ADD_ALL_TESTS(test_early_data_tls1_2, 3);
10148 # endif
10149 #endif
10150 #ifndef OSSL_NO_USABLE_TLS1_3
10151 ADD_ALL_TESTS(test_set_ciphersuite, 10);
10152 ADD_TEST(test_ciphersuite_change);
10153 ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
10154 # ifdef OPENSSL_NO_PSK
10155 ADD_ALL_TESTS(test_tls13_psk, 1);
10156 # else
10157 ADD_ALL_TESTS(test_tls13_psk, 4);
10158 # endif /* OPENSSL_NO_PSK */
10159 # ifndef OPENSSL_NO_TLS1_2
10160 /* Test with both TLSv1.3 and 1.2 versions */
10161 ADD_ALL_TESTS(test_key_exchange, 14);
10162 # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
10163 ADD_ALL_TESTS(test_negotiated_group,
10164 4 * (OSSL_NELEM(ecdhe_kexch_groups)
10165 + OSSL_NELEM(ffdhe_kexch_groups)));
10166 # endif
10167 # else
10168 /* Test with only TLSv1.3 versions */
10169 ADD_ALL_TESTS(test_key_exchange, 12);
10170 # endif
10171 ADD_ALL_TESTS(test_custom_exts, 6);
10172 ADD_TEST(test_stateless);
10173 ADD_TEST(test_pha_key_update);
10174 #else
10175 ADD_ALL_TESTS(test_custom_exts, 3);
10176 #endif
10177 ADD_ALL_TESTS(test_export_key_mat, 6);
10178 #ifndef OSSL_NO_USABLE_TLS1_3
10179 ADD_ALL_TESTS(test_export_key_mat_early, 3);
10180 ADD_TEST(test_key_update);
10181 ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
10182 ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
10183 ADD_ALL_TESTS(test_key_update_local_in_write, 2);
10184 ADD_ALL_TESTS(test_key_update_local_in_read, 2);
10185 #endif
10186 ADD_ALL_TESTS(test_ssl_clear, 2);
10187 ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
10188 #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
10189 ADD_ALL_TESTS(test_srp, 6);
10190 #endif
10191 ADD_ALL_TESTS(test_info_callback, 6);
10192 ADD_ALL_TESTS(test_ssl_pending, 2);
10193 ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
10194 ADD_ALL_TESTS(test_ticket_callbacks, 16);
10195 ADD_ALL_TESTS(test_shutdown, 7);
10196 ADD_ALL_TESTS(test_incorrect_shutdown, 2);
10197 ADD_ALL_TESTS(test_cert_cb, 6);
10198 ADD_ALL_TESTS(test_client_cert_cb, 2);
10199 ADD_ALL_TESTS(test_ca_names, 3);
10200 #ifndef OPENSSL_NO_TLS1_2
10201 ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
10202 #endif
10203 ADD_ALL_TESTS(test_servername, 10);
10204 #if !defined(OPENSSL_NO_EC) \
10205 && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
10206 ADD_ALL_TESTS(test_sigalgs_available, 6);
10207 #endif
10208 #ifndef OPENSSL_NO_TLS1_3
10209 ADD_ALL_TESTS(test_pluggable_group, 2);
10210 #endif
10211 #ifndef OPENSSL_NO_TLS1_2
10212 ADD_TEST(test_ssl_dup);
10213 # ifndef OPENSSL_NO_DH
10214 ADD_ALL_TESTS(test_set_tmp_dh, 11);
10215 ADD_ALL_TESTS(test_dh_auto, 7);
10216 # endif
10217 #endif
10218 #ifndef OSSL_NO_USABLE_TLS1_3
10219 ADD_TEST(test_sni_tls13);
10220 ADD_ALL_TESTS(test_ticket_lifetime, 2);
10221 #endif
10222 ADD_TEST(test_inherit_verify_param);
10223 ADD_TEST(test_set_alpn);
10224 ADD_TEST(test_set_verify_cert_store_ssl_ctx);
10225 ADD_TEST(test_set_verify_cert_store_ssl);
10226 ADD_ALL_TESTS(test_session_timeout, 1);
10227 ADD_TEST(test_load_dhfile);
10228 #ifndef OSSL_NO_USABLE_TLS1_3
10229 ADD_TEST(test_read_ahead_key_change);
10230 #endif
10231 #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
10232 ADD_ALL_TESTS(test_serverinfo_custom, 4);
10233 #endif
10234 return 1;
10235
10236 err:
10237 OPENSSL_free(cert);
10238 OPENSSL_free(privkey);
10239 OPENSSL_free(cert2);
10240 OPENSSL_free(privkey2);
10241 return 0;
10242 }
10243
cleanup_tests(void)10244 void cleanup_tests(void)
10245 {
10246 # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
10247 EVP_PKEY_free(tmp_dh_params);
10248 #endif
10249 OPENSSL_free(cert);
10250 OPENSSL_free(privkey);
10251 OPENSSL_free(cert2);
10252 OPENSSL_free(privkey2);
10253 OPENSSL_free(cert1024);
10254 OPENSSL_free(privkey1024);
10255 OPENSSL_free(cert3072);
10256 OPENSSL_free(privkey3072);
10257 OPENSSL_free(cert4096);
10258 OPENSSL_free(privkey4096);
10259 OPENSSL_free(cert8192);
10260 OPENSSL_free(privkey8192);
10261 bio_s_mempacket_test_free();
10262 bio_s_always_retry_free();
10263 OSSL_PROVIDER_unload(defctxnull);
10264 OSSL_LIB_CTX_free(libctx);
10265 }
10266