]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/sslapitest.c
Write a test for receiving a KeyUpdate (update requested) while writing
[thirdparty/openssl.git] / test / sslapitest.c
CommitLineData
2cb4b5f6 1/*
6738bf14 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
2cb4b5f6 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2cb4b5f6
MC
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
7c3a7561 10#include <stdio.h>
ba881d3b
MC
11#include <string.h>
12
2cb4b5f6
MC
13#include <openssl/opensslconf.h>
14#include <openssl/bio.h>
15#include <openssl/crypto.h>
16#include <openssl/ssl.h>
ba881d3b 17#include <openssl/ocsp.h>
76fd7a1d
MC
18#include <openssl/srp.h>
19#include <openssl/txt_db.h>
d0191fe0 20#include <openssl/aes.h>
7c3a7561 21#include <openssl/rand.h>
2cb4b5f6
MC
22
23#include "ssltestlib.h"
c887104f 24#include "testutil.h"
f297e4ec 25#include "testutil/output.h"
176db6dc 26#include "internal/nelem.h"
fe5d9450 27#include "internal/ktls.h"
ca0413ae 28#include "../ssl/ssl_locl.h"
2cb4b5f6 29
8614a4eb
MC
30#ifndef OPENSSL_NO_TLS1_3
31
32static SSL_SESSION *clientpsk = NULL;
33static SSL_SESSION *serverpsk = NULL;
34static const char *pskid = "Identity";
35static const char *srvid;
36
37static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
38 size_t *idlen, SSL_SESSION **sess);
39static int find_session_cb(SSL *ssl, const unsigned char *identity,
40 size_t identity_len, SSL_SESSION **sess);
41
42static int use_session_cb_cnt = 0;
43static int find_session_cb_cnt = 0;
44
45static SSL_SESSION *create_a_psk(SSL *ssl);
46#endif
47
2cb4b5f6
MC
48static char *cert = NULL;
49static char *privkey = NULL;
76fd7a1d
MC
50static char *srpvfile = NULL;
51static char *tmpfilename = NULL;
2cb4b5f6 52
cffe973c 53#define LOG_BUFFER_SIZE 2048
6acdd3e5 54static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
710756a9 55static size_t server_log_buffer_index = 0;
6acdd3e5 56static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
710756a9 57static size_t client_log_buffer_index = 0;
6acdd3e5
CB
58static int error_writing_log = 0;
59
8f8c11d8 60#ifndef OPENSSL_NO_OCSP
ba881d3b
MC
61static const unsigned char orespder[] = "Dummy OCSP Response";
62static int ocsp_server_called = 0;
63static int ocsp_client_called = 0;
64
65static int cdummyarg = 1;
66static X509 *ocspcert = NULL;
8f8c11d8 67#endif
ba881d3b 68
84d5549e 69#define NUM_EXTRA_CERTS 40
cf72c757 70#define CLIENT_VERSION_LEN 2
84d5549e 71
f1a5939f
CB
72/*
73 * This structure is used to validate that the correct number of log messages
74 * of various types are emitted when emitting secret logs.
75 */
76struct sslapitest_log_counts {
77 unsigned int rsa_key_exchange_count;
78 unsigned int master_secret_count;
cffe973c 79 unsigned int client_early_secret_count;
f1a5939f
CB
80 unsigned int client_handshake_secret_count;
81 unsigned int server_handshake_secret_count;
82 unsigned int client_application_secret_count;
83 unsigned int server_application_secret_count;
01a2a654 84 unsigned int early_exporter_secret_count;
6329ce8f 85 unsigned int exporter_secret_count;
f1a5939f
CB
86};
87
16afd71c
MC
88
89static unsigned char serverinfov1[] = {
90 0xff, 0xff, /* Dummy extension type */
91 0x00, 0x01, /* Extension length is 1 byte */
92 0xff /* Dummy extension data */
93};
94
95static unsigned char serverinfov2[] = {
96 0x00, 0x00, 0x00,
97 (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
98 0xff, 0xff, /* Dummy extension type */
99 0x00, 0x01, /* Extension length is 1 byte */
100 0xff /* Dummy extension data */
101};
102
710756a9
RS
103static void client_keylog_callback(const SSL *ssl, const char *line)
104{
6acdd3e5
CB
105 int line_length = strlen(line);
106
107 /* If the log doesn't fit, error out. */
710756a9
RS
108 if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
109 TEST_info("Client log too full");
6acdd3e5
CB
110 error_writing_log = 1;
111 return;
112 }
113
114 strcat(client_log_buffer, line);
115 client_log_buffer_index += line_length;
710756a9 116 client_log_buffer[client_log_buffer_index++] = '\n';
6acdd3e5
CB
117}
118
710756a9
RS
119static void server_keylog_callback(const SSL *ssl, const char *line)
120{
6acdd3e5
CB
121 int line_length = strlen(line);
122
123 /* If the log doesn't fit, error out. */
710756a9 124 if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
bdcacd93 125 TEST_info("Server log too full");
6acdd3e5
CB
126 error_writing_log = 1;
127 return;
128 }
129
130 strcat(server_log_buffer, line);
131 server_log_buffer_index += line_length;
710756a9 132 server_log_buffer[server_log_buffer_index++] = '\n';
6acdd3e5
CB
133}
134
135static int compare_hex_encoded_buffer(const char *hex_encoded,
136 size_t hex_length,
137 const uint8_t *raw,
710756a9
RS
138 size_t raw_length)
139{
140 size_t i, j;
141 char hexed[3];
6acdd3e5 142
710756a9 143 if (!TEST_size_t_eq(raw_length * 2, hex_length))
6acdd3e5 144 return 1;
6acdd3e5 145
710756a9 146 for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
6acdd3e5 147 sprintf(hexed, "%02x", raw[i]);
710756a9
RS
148 if (!TEST_int_eq(hexed[0], hex_encoded[j])
149 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
6acdd3e5 150 return 1;
6acdd3e5
CB
151 }
152
153 return 0;
154}
155
156static int test_keylog_output(char *buffer, const SSL *ssl,
f1a5939f 157 const SSL_SESSION *session,
710756a9
RS
158 struct sslapitest_log_counts *expected)
159{
6acdd3e5
CB
160 char *token = NULL;
161 unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
162 size_t client_random_size = SSL3_RANDOM_SIZE;
163 unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
164 size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
f1a5939f
CB
165 unsigned int rsa_key_exchange_count = 0;
166 unsigned int master_secret_count = 0;
cffe973c 167 unsigned int client_early_secret_count = 0;
f1a5939f
CB
168 unsigned int client_handshake_secret_count = 0;
169 unsigned int server_handshake_secret_count = 0;
170 unsigned int client_application_secret_count = 0;
171 unsigned int server_application_secret_count = 0;
01a2a654 172 unsigned int early_exporter_secret_count = 0;
6329ce8f 173 unsigned int exporter_secret_count = 0;
6acdd3e5 174
710756a9
RS
175 for (token = strtok(buffer, " \n"); token != NULL;
176 token = strtok(NULL, " \n")) {
6acdd3e5 177 if (strcmp(token, "RSA") == 0) {
f1a5939f
CB
178 /*
179 * Premaster secret. Tokens should be: 16 ASCII bytes of
6acdd3e5
CB
180 * hex-encoded encrypted secret, then the hex-encoded pre-master
181 * secret.
182 */
710756a9 183 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 184 return 0;
710756a9 185 if (!TEST_size_t_eq(strlen(token), 16))
f1a5939f 186 return 0;
710756a9 187 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 188 return 0;
f1a5939f
CB
189 /*
190 * We can't sensibly check the log because the premaster secret is
191 * transient, and OpenSSL doesn't keep hold of it once the master
192 * secret is generated.
193 */
194 rsa_key_exchange_count++;
6acdd3e5 195 } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
f1a5939f
CB
196 /*
197 * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
6acdd3e5
CB
198 * client random, then the hex-encoded master secret.
199 */
200 client_random_size = SSL_get_client_random(ssl,
201 actual_client_random,
202 SSL3_RANDOM_SIZE);
710756a9 203 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
f1a5939f 204 return 0;
6acdd3e5 205
710756a9 206 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 207 return 0;
710756a9 208 if (!TEST_size_t_eq(strlen(token), 64))
f1a5939f 209 return 0;
710756a9
RS
210 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
211 actual_client_random,
212 client_random_size)))
f1a5939f 213 return 0;
6acdd3e5 214
710756a9 215 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 216 return 0;
6acdd3e5
CB
217 master_key_size = SSL_SESSION_get_master_key(session,
218 actual_master_key,
219 master_key_size);
710756a9 220 if (!TEST_size_t_ne(master_key_size, 0))
f1a5939f 221 return 0;
710756a9
RS
222 if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
223 actual_master_key,
224 master_key_size)))
f1a5939f 225 return 0;
f1a5939f 226 master_secret_count++;
cffe973c
PW
227 } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
228 || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
710756a9
RS
229 || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
230 || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
6329ce8f 231 || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
01a2a654 232 || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
6329ce8f 233 || strcmp(token, "EXPORTER_SECRET") == 0) {
f1a5939f
CB
234 /*
235 * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
236 * client random, and then the hex-encoded secret. In this case,
237 * we treat all of these secrets identically and then just
238 * distinguish between them when counting what we saw.
239 */
cffe973c
PW
240 if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
241 client_early_secret_count++;
242 else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
f1a5939f
CB
243 client_handshake_secret_count++;
244 else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
245 server_handshake_secret_count++;
246 else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
247 client_application_secret_count++;
248 else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
249 server_application_secret_count++;
01a2a654
PW
250 else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
251 early_exporter_secret_count++;
6329ce8f
PW
252 else if (strcmp(token, "EXPORTER_SECRET") == 0)
253 exporter_secret_count++;
f1a5939f
CB
254
255 client_random_size = SSL_get_client_random(ssl,
256 actual_client_random,
257 SSL3_RANDOM_SIZE);
710756a9 258 if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
f1a5939f 259 return 0;
f1a5939f 260
710756a9 261 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 262 return 0;
710756a9 263 if (!TEST_size_t_eq(strlen(token), 64))
f1a5939f 264 return 0;
710756a9
RS
265 if (!TEST_false(compare_hex_encoded_buffer(token, 64,
266 actual_client_random,
267 client_random_size)))
f1a5939f 268 return 0;
6acdd3e5 269
710756a9 270 if (!TEST_ptr(token = strtok(NULL, " \n")))
f1a5939f 271 return 0;
f1a5939f
CB
272
273 /*
274 * TODO(TLS1.3): test that application traffic secrets are what
275 * we expect */
6acdd3e5 276 } else {
710756a9 277 TEST_info("Unexpected token %s\n", token);
f1a5939f 278 return 0;
6acdd3e5 279 }
6acdd3e5
CB
280 }
281
710756a9
RS
282 /* Got what we expected? */
283 if (!TEST_size_t_eq(rsa_key_exchange_count,
284 expected->rsa_key_exchange_count)
285 || !TEST_size_t_eq(master_secret_count,
286 expected->master_secret_count)
cffe973c
PW
287 || !TEST_size_t_eq(client_early_secret_count,
288 expected->client_early_secret_count)
710756a9
RS
289 || !TEST_size_t_eq(client_handshake_secret_count,
290 expected->client_handshake_secret_count)
291 || !TEST_size_t_eq(server_handshake_secret_count,
292 expected->server_handshake_secret_count)
293 || !TEST_size_t_eq(client_application_secret_count,
294 expected->client_application_secret_count)
295 || !TEST_size_t_eq(server_application_secret_count,
6329ce8f 296 expected->server_application_secret_count)
01a2a654
PW
297 || !TEST_size_t_eq(early_exporter_secret_count,
298 expected->early_exporter_secret_count)
6329ce8f
PW
299 || !TEST_size_t_eq(exporter_secret_count,
300 expected->exporter_secret_count))
710756a9
RS
301 return 0;
302 return 1;
6acdd3e5
CB
303}
304
c423ecaa 305#if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
710756a9
RS
306static int test_keylog(void)
307{
6acdd3e5
CB
308 SSL_CTX *cctx = NULL, *sctx = NULL;
309 SSL *clientssl = NULL, *serverssl = NULL;
310 int testresult = 0;
6fc1e624 311 struct sslapitest_log_counts expected;
6acdd3e5
CB
312
313 /* Clean up logging space */
6fc1e624 314 memset(&expected, 0, sizeof(expected));
710756a9
RS
315 memset(client_log_buffer, 0, sizeof(client_log_buffer));
316 memset(server_log_buffer, 0, sizeof(server_log_buffer));
6acdd3e5
CB
317 client_log_buffer_index = 0;
318 server_log_buffer_index = 0;
319 error_writing_log = 0;
320
710756a9
RS
321 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
322 TLS_client_method(),
5c587fb6 323 TLS1_VERSION, 0,
710756a9 324 &sctx, &cctx, cert, privkey)))
6acdd3e5 325 return 0;
6acdd3e5
CB
326
327 /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
328 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
329 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
330
f0deb4d3 331 /* We also want to ensure that we use RSA-based key exchange. */
710756a9 332 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
f0deb4d3 333 goto end;
f0deb4d3 334
cf10df81
RS
335 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
336 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
6acdd3e5 337 goto end;
6acdd3e5 338 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
cf10df81
RS
339 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
340 == client_keylog_callback))
6acdd3e5 341 goto end;
710756a9 342 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
cf10df81
RS
343 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
344 == server_keylog_callback))
6acdd3e5 345 goto end;
6acdd3e5 346
710756a9
RS
347 /* Now do a handshake and check that the logs have been written to. */
348 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
349 &clientssl, NULL, NULL))
350 || !TEST_true(create_ssl_connection(serverssl, clientssl,
351 SSL_ERROR_NONE))
352 || !TEST_false(error_writing_log)
353 || !TEST_int_gt(client_log_buffer_index, 0)
354 || !TEST_int_gt(server_log_buffer_index, 0))
6acdd3e5 355 goto end;
6acdd3e5 356
f1a5939f
CB
357 /*
358 * Now we want to test that our output data was vaguely sensible. We
6acdd3e5 359 * do that by using strtok and confirming that we have more or less the
f1a5939f
CB
360 * data we expect. For both client and server, we expect to see one master
361 * secret. The client should also see a RSA key exchange.
6acdd3e5 362 */
f1a5939f
CB
363 expected.rsa_key_exchange_count = 1;
364 expected.master_secret_count = 1;
710756a9
RS
365 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
366 SSL_get_session(clientssl), &expected)))
6acdd3e5 367 goto end;
f1a5939f
CB
368
369 expected.rsa_key_exchange_count = 0;
710756a9
RS
370 if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
371 SSL_get_session(serverssl), &expected)))
6acdd3e5 372 goto end;
6acdd3e5
CB
373
374 testresult = 1;
375
376end:
377 SSL_free(serverssl);
378 SSL_free(clientssl);
379 SSL_CTX_free(sctx);
380 SSL_CTX_free(cctx);
381
382 return testresult;
383}
c423ecaa 384#endif
6acdd3e5
CB
385
386#ifndef OPENSSL_NO_TLS1_3
710756a9
RS
387static int test_keylog_no_master_key(void)
388{
6acdd3e5
CB
389 SSL_CTX *cctx = NULL, *sctx = NULL;
390 SSL *clientssl = NULL, *serverssl = NULL;
cffe973c 391 SSL_SESSION *sess = NULL;
6acdd3e5 392 int testresult = 0;
6fc1e624 393 struct sslapitest_log_counts expected;
cffe973c
PW
394 unsigned char buf[1];
395 size_t readbytes, written;
6acdd3e5
CB
396
397 /* Clean up logging space */
6fc1e624 398 memset(&expected, 0, sizeof(expected));
710756a9
RS
399 memset(client_log_buffer, 0, sizeof(client_log_buffer));
400 memset(server_log_buffer, 0, sizeof(server_log_buffer));
6acdd3e5
CB
401 client_log_buffer_index = 0;
402 server_log_buffer_index = 0;
403 error_writing_log = 0;
404
7d7f6834 405 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 406 TLS1_VERSION, 0,
cffe973c
PW
407 &sctx, &cctx, cert, privkey))
408 || !TEST_true(SSL_CTX_set_max_early_data(sctx,
cffe973c 409 SSL3_RT_MAX_PLAIN_LENGTH)))
6acdd3e5 410 return 0;
6acdd3e5 411
cf10df81
RS
412 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
413 || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
6acdd3e5 414 goto end;
6acdd3e5
CB
415
416 SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
cf10df81
RS
417 if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
418 == client_keylog_callback))
6acdd3e5 419 goto end;
6acdd3e5 420
710756a9 421 SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
cf10df81
RS
422 if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
423 == server_keylog_callback))
6acdd3e5 424 goto end;
6acdd3e5 425
710756a9
RS
426 /* Now do a handshake and check that the logs have been written to. */
427 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
428 &clientssl, NULL, NULL))
429 || !TEST_true(create_ssl_connection(serverssl, clientssl,
430 SSL_ERROR_NONE))
431 || !TEST_false(error_writing_log))
6acdd3e5 432 goto end;
6acdd3e5 433
f1a5939f
CB
434 /*
435 * Now we want to test that our output data was vaguely sensible. For this
69687aa8 436 * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
f1a5939f 437 * TLSv1.3, but we do expect both client and server to emit keys.
6acdd3e5 438 */
f1a5939f
CB
439 expected.client_handshake_secret_count = 1;
440 expected.server_handshake_secret_count = 1;
441 expected.client_application_secret_count = 1;
442 expected.server_application_secret_count = 1;
6329ce8f 443 expected.exporter_secret_count = 1;
710756a9
RS
444 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
445 SSL_get_session(clientssl), &expected))
446 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
447 SSL_get_session(serverssl),
448 &expected)))
6acdd3e5 449 goto end;
6acdd3e5 450
cffe973c
PW
451 /* Terminate old session and resume with early data. */
452 sess = SSL_get1_session(clientssl);
453 SSL_shutdown(clientssl);
454 SSL_shutdown(serverssl);
455 SSL_free(serverssl);
456 SSL_free(clientssl);
457 serverssl = clientssl = NULL;
458
459 /* Reset key log */
460 memset(client_log_buffer, 0, sizeof(client_log_buffer));
461 memset(server_log_buffer, 0, sizeof(server_log_buffer));
462 client_log_buffer_index = 0;
463 server_log_buffer_index = 0;
464
465 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
466 &clientssl, NULL, NULL))
467 || !TEST_true(SSL_set_session(clientssl, sess))
468 /* Here writing 0 length early data is enough. */
469 || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
470 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
471 &readbytes),
472 SSL_READ_EARLY_DATA_ERROR)
473 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
474 SSL_EARLY_DATA_ACCEPTED)
475 || !TEST_true(create_ssl_connection(serverssl, clientssl,
476 SSL_ERROR_NONE))
477 || !TEST_true(SSL_session_reused(clientssl)))
478 goto end;
479
480 /* In addition to the previous entries, expect early secrets. */
481 expected.client_early_secret_count = 1;
01a2a654 482 expected.early_exporter_secret_count = 1;
cffe973c
PW
483 if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
484 SSL_get_session(clientssl), &expected))
485 || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
486 SSL_get_session(serverssl),
487 &expected)))
488 goto end;
489
6acdd3e5
CB
490 testresult = 1;
491
492end:
cffe973c 493 SSL_SESSION_free(sess);
6acdd3e5
CB
494 SSL_free(serverssl);
495 SSL_free(clientssl);
496 SSL_CTX_free(sctx);
497 SSL_CTX_free(cctx);
498
499 return testresult;
500}
501#endif
502
e9ee6536 503#ifndef OPENSSL_NO_TLS1_2
a9c0d8be 504static int full_client_hello_callback(SSL *s, int *al, void *arg)
2afaee51
BK
505{
506 int *ctr = arg;
507 const unsigned char *p;
8ea404fb 508 int *exts;
2afaee51
BK
509 /* We only configure two ciphers, but the SCSV is added automatically. */
510#ifdef OPENSSL_NO_EC
511 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
512#else
513 const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
514 0x2c, 0x00, 0xff};
515#endif
8ea404fb
BK
516 const int expected_extensions[] = {
517#ifndef OPENSSL_NO_EC
518 11, 10,
519#endif
10ed1b72 520 35, 22, 23, 13};
2afaee51
BK
521 size_t len;
522
523 /* Make sure we can defer processing and get called back. */
524 if ((*ctr)++ == 0)
f1b97da1 525 return SSL_CLIENT_HELLO_RETRY;
2afaee51 526
a9c0d8be 527 len = SSL_client_hello_get0_ciphers(s, &p);
710756a9 528 if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
a9c0d8be
DB
529 || !TEST_size_t_eq(
530 SSL_client_hello_get0_compression_methods(s, &p), 1)
710756a9 531 || !TEST_int_eq(*p, 0))
f1b97da1 532 return SSL_CLIENT_HELLO_ERROR;
a9c0d8be 533 if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
f1b97da1 534 return SSL_CLIENT_HELLO_ERROR;
8ea404fb
BK
535 if (len != OSSL_NELEM(expected_extensions) ||
536 memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
a9c0d8be 537 printf("ClientHello callback expected extensions mismatch\n");
8ea404fb 538 OPENSSL_free(exts);
f1b97da1 539 return SSL_CLIENT_HELLO_ERROR;
8ea404fb
BK
540 }
541 OPENSSL_free(exts);
f1b97da1 542 return SSL_CLIENT_HELLO_SUCCESS;
2afaee51
BK
543}
544
a9c0d8be 545static int test_client_hello_cb(void)
710756a9 546{
2afaee51
BK
547 SSL_CTX *cctx = NULL, *sctx = NULL;
548 SSL *clientssl = NULL, *serverssl = NULL;
549 int testctr = 0, testresult = 0;
550
7d7f6834 551 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 552 TLS1_VERSION, 0,
7d7f6834 553 &sctx, &cctx, cert, privkey)))
2afaee51 554 goto end;
a9c0d8be 555 SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
710756a9 556
2afaee51
BK
557 /* The gimpy cipher list we configure can't do TLS 1.3. */
558 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2afaee51 559
710756a9
RS
560 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
561 "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
562 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
563 &clientssl, NULL, NULL))
564 || !TEST_false(create_ssl_connection(serverssl, clientssl,
a9c0d8be 565 SSL_ERROR_WANT_CLIENT_HELLO_CB))
710756a9
RS
566 /*
567 * Passing a -1 literal is a hack since
568 * the real value was lost.
569 * */
a9c0d8be
DB
570 || !TEST_int_eq(SSL_get_error(serverssl, -1),
571 SSL_ERROR_WANT_CLIENT_HELLO_CB)
710756a9
RS
572 || !TEST_true(create_ssl_connection(serverssl, clientssl,
573 SSL_ERROR_NONE)))
2afaee51 574 goto end;
2afaee51
BK
575
576 testresult = 1;
577
088dfa13
TS
578end:
579 SSL_free(serverssl);
580 SSL_free(clientssl);
581 SSL_CTX_free(sctx);
582 SSL_CTX_free(cctx);
583
584 return testresult;
585}
586
587static int test_no_ems(void)
588{
589 SSL_CTX *cctx = NULL, *sctx = NULL;
590 SSL *clientssl = NULL, *serverssl = NULL;
591 int testresult = 0;
592
593 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
594 TLS1_VERSION, TLS1_2_VERSION,
595 &sctx, &cctx, cert, privkey)) {
596 printf("Unable to create SSL_CTX pair\n");
597 goto end;
598 }
599
600 SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
601
602 if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
603 printf("Unable to create SSL objects\n");
604 goto end;
605 }
606
607 if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
608 printf("Creating SSL connection failed\n");
609 goto end;
610 }
611
612 if (SSL_get_extms_support(serverssl)) {
613 printf("Server reports Extended Master Secret support\n");
614 goto end;
615 }
616
617 if (SSL_get_extms_support(clientssl)) {
618 printf("Client reports Extended Master Secret support\n");
619 goto end;
620 }
621 testresult = 1;
622
2afaee51
BK
623end:
624 SSL_free(serverssl);
625 SSL_free(clientssl);
626 SSL_CTX_free(sctx);
627 SSL_CTX_free(cctx);
628
629 return testresult;
630}
e9ee6536 631#endif
2afaee51 632
84d5549e 633static int execute_test_large_message(const SSL_METHOD *smeth,
7d7f6834
RL
634 const SSL_METHOD *cmeth,
635 int min_version, int max_version,
636 int read_ahead)
84d5549e
MC
637{
638 SSL_CTX *cctx = NULL, *sctx = NULL;
639 SSL *clientssl = NULL, *serverssl = NULL;
640 int testresult = 0;
641 int i;
710756a9 642 BIO *certbio = NULL;
84d5549e
MC
643 X509 *chaincert = NULL;
644 int certlen;
645
710756a9 646 if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
84d5549e 647 goto end;
84d5549e 648 chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
fa454945
MC
649 BIO_free(certbio);
650 certbio = NULL;
710756a9 651 if (!TEST_ptr(chaincert))
fa454945 652 goto end;
84d5549e 653
7d7f6834
RL
654 if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, min_version, max_version,
655 &sctx, &cctx, cert, privkey)))
84d5549e 656 goto end;
84d5549e 657
710756a9 658 if (read_ahead) {
7856332e
MC
659 /*
660 * Test that read_ahead works correctly when dealing with large
661 * records
662 */
663 SSL_CTX_set_read_ahead(cctx, 1);
664 }
665
84d5549e
MC
666 /*
667 * We assume the supplied certificate is big enough so that if we add
668 * NUM_EXTRA_CERTS it will make the overall message large enough. The
669 * default buffer size is requested to be 16k, but due to the way BUF_MEM
710756a9
RS
670 * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
671 * test we need to have a message larger than that.
84d5549e
MC
672 */
673 certlen = i2d_X509(chaincert, NULL);
710756a9
RS
674 OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
675 (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
84d5549e 676 for (i = 0; i < NUM_EXTRA_CERTS; i++) {
710756a9 677 if (!X509_up_ref(chaincert))
84d5549e 678 goto end;
84d5549e 679 if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
84d5549e
MC
680 X509_free(chaincert);
681 goto end;
682 }
683 }
684
710756a9
RS
685 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
686 NULL, NULL))
687 || !TEST_true(create_ssl_connection(serverssl, clientssl,
688 SSL_ERROR_NONE)))
84d5549e 689 goto end;
84d5549e 690
4bf08600
MC
691 /*
692 * Calling SSL_clear() first is not required but this tests that SSL_clear()
693 * doesn't leak (when using enable-crypto-mdebug).
694 */
710756a9 695 if (!TEST_true(SSL_clear(serverssl)))
4bf08600 696 goto end;
84d5549e 697
4bf08600 698 testresult = 1;
84d5549e
MC
699 end:
700 X509_free(chaincert);
701 SSL_free(serverssl);
702 SSL_free(clientssl);
703 SSL_CTX_free(sctx);
704 SSL_CTX_free(cctx);
705
706 return testresult;
707}
708
5e9072ed
MC
709#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_KTLS) \
710 && !defined(OPENSSL_NO_SOCK)
fe5d9450
BP
711
712/* sock must be connected */
713static int ktls_chk_platform(int sock)
714{
715 if (!ktls_enable(sock))
716 return 0;
717 return 1;
718}
719
720static int ping_pong_query(SSL *clientssl, SSL *serverssl, int cfd, int sfd)
721{
722 static char count = 1;
723 unsigned char cbuf[16000] = {0};
724 unsigned char sbuf[16000];
725 size_t err = 0;
726 char crec_wseq_before[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
727 char crec_wseq_after[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
2fab79af
BP
728 char crec_rseq_before[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
729 char crec_rseq_after[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
fe5d9450
BP
730 char srec_wseq_before[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
731 char srec_wseq_after[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
732 char srec_rseq_before[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
733 char srec_rseq_after[TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE];
734
735 cbuf[0] = count++;
736 memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence,
737 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
2fab79af
BP
738 memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence,
739 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
fe5d9450
BP
740 memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence,
741 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
742 memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence,
743 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
744
745 if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
746 goto end;
747
748 while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
749 if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
750 goto end;
751 }
752 }
753
754 if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
755 goto end;
756
757 while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
758 if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
759 goto end;
760 }
761 }
762
763 memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence,
764 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
2fab79af
BP
765 memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence,
766 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
fe5d9450
BP
767 memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence,
768 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
769 memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence,
770 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
771
772 /* verify the payload */
773 if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
774 goto end;
775
776 /* ktls is used then kernel sequences are used instead of OpenSSL sequences */
777 if (clientssl->mode & SSL_MODE_NO_KTLS_TX) {
778 if (!TEST_mem_ne(crec_wseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
779 crec_wseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
780 goto end;
781 } else {
782 if (!TEST_mem_eq(crec_wseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
783 crec_wseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
784 goto end;
785 }
786
787 if (serverssl->mode & SSL_MODE_NO_KTLS_TX) {
788 if (!TEST_mem_ne(srec_wseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
789 srec_wseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
790 goto end;
791 } else {
792 if (!TEST_mem_eq(srec_wseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
793 srec_wseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
794 goto end;
795 }
796
2fab79af
BP
797 if (clientssl->mode & SSL_MODE_NO_KTLS_RX) {
798 if (!TEST_mem_ne(crec_rseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
799 crec_rseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
800 goto end;
801 } else {
802 if (!TEST_mem_eq(crec_rseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
803 crec_rseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
804 goto end;
805 }
806
807 if (serverssl->mode & SSL_MODE_NO_KTLS_RX) {
808 if (!TEST_mem_ne(srec_rseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
809 srec_rseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
810 goto end;
811 } else {
812 if (!TEST_mem_eq(srec_rseq_before, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE,
813 srec_rseq_after, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE))
814 goto end;
815 }
fe5d9450
BP
816
817 return 1;
818end:
819 return 0;
820}
821
2fab79af
BP
822static int execute_test_ktls(int cis_ktls_tx, int cis_ktls_rx,
823 int sis_ktls_tx, int sis_ktls_rx)
fe5d9450
BP
824{
825 SSL_CTX *cctx = NULL, *sctx = NULL;
826 SSL *clientssl = NULL, *serverssl = NULL;
827 int testresult = 0;
828 int cfd, sfd;
829
830 if (!TEST_true(create_test_sockets(&cfd, &sfd)))
831 goto end;
832
833 /* Skip this test if the platform does not support ktls */
834 if (!ktls_chk_platform(cfd))
835 return 1;
836
837 /* Create a session based on SHA-256 */
838 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
839 TLS_client_method(),
840 TLS1_2_VERSION, TLS1_2_VERSION,
841 &sctx, &cctx, cert, privkey))
842 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
843 "AES128-GCM-SHA256"))
844 || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
845 &clientssl, sfd, cfd)))
846 goto end;
847
848 if (!cis_ktls_tx) {
849 if (!TEST_true(SSL_set_mode(clientssl, SSL_MODE_NO_KTLS_TX)))
850 goto end;
851 }
852
853 if (!sis_ktls_tx) {
854 if (!TEST_true(SSL_set_mode(serverssl, SSL_MODE_NO_KTLS_TX)))
855 goto end;
856 }
857
2fab79af
BP
858 if (!cis_ktls_rx) {
859 if (!TEST_true(SSL_set_mode(clientssl, SSL_MODE_NO_KTLS_RX)))
860 goto end;
861 }
862
863 if (!sis_ktls_rx) {
864 if (!TEST_true(SSL_set_mode(serverssl, SSL_MODE_NO_KTLS_RX)))
865 goto end;
866 }
867
fe5d9450
BP
868 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
869 SSL_ERROR_NONE)))
870 goto end;
871
872 if (!cis_ktls_tx) {
873 if (!TEST_false(BIO_get_ktls_send(clientssl->wbio)))
874 goto end;
875 } else {
876 if (!TEST_true(BIO_get_ktls_send(clientssl->wbio)))
877 goto end;
878 }
879
880 if (!sis_ktls_tx) {
881 if (!TEST_false(BIO_get_ktls_send(serverssl->wbio)))
882 goto end;
883 } else {
884 if (!TEST_true(BIO_get_ktls_send(serverssl->wbio)))
885 goto end;
886 }
887
2fab79af
BP
888 if (!cis_ktls_rx) {
889 if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio)))
890 goto end;
891 } else {
892 if (!TEST_true(BIO_get_ktls_recv(clientssl->rbio)))
893 goto end;
894 }
895
896 if (!sis_ktls_rx) {
897 if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio)))
898 goto end;
899 } else {
900 if (!TEST_true(BIO_get_ktls_recv(serverssl->rbio)))
901 goto end;
902 }
903
fe5d9450
BP
904 if (!TEST_true(ping_pong_query(clientssl, serverssl, cfd, sfd)))
905 goto end;
906
907 testresult = 1;
908end:
909 if (clientssl) {
910 SSL_shutdown(clientssl);
911 SSL_free(clientssl);
912 }
913 if (serverssl) {
914 SSL_shutdown(serverssl);
915 SSL_free(serverssl);
916 }
917 SSL_CTX_free(sctx);
918 SSL_CTX_free(cctx);
919 serverssl = clientssl = NULL;
920 return testresult;
921}
922
7c3a7561
BP
923#define SENDFILE_SZ (16 * 4096)
924#define SENDFILE_CHUNK (4 * 4096)
925#define min(a,b) ((a) > (b) ? (b) : (a))
926
927static int test_ktls_sendfile(void)
928{
929 SSL_CTX *cctx = NULL, *sctx = NULL;
930 SSL *clientssl = NULL, *serverssl = NULL;
931 unsigned char *buf, *buf_dst;
932 BIO *out = NULL, *in = NULL;
933 int cfd, sfd, ffd, err;
934 ssize_t chunk_size = 0;
935 off_t chunk_off = 0;
936 int testresult = 0;
937 FILE *ffdp;
938
939 buf = OPENSSL_zalloc(SENDFILE_SZ);
940 buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
941 if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
942 || !TEST_true(create_test_sockets(&cfd, &sfd)))
943 goto end;
944
945 /* Skip this test if the platform does not support ktls */
946 if (!ktls_chk_platform(sfd)) {
947 testresult = 1;
948 goto end;
949 }
950
951 /* Create a session based on SHA-256 */
952 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
953 TLS_client_method(),
954 TLS1_2_VERSION, TLS1_2_VERSION,
955 &sctx, &cctx, cert, privkey))
956 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
957 "AES128-GCM-SHA256"))
958 || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
959 &clientssl, sfd, cfd)))
960 goto end;
961
962 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
963 SSL_ERROR_NONE))
964 || !TEST_true(BIO_get_ktls_send(serverssl->wbio)))
965 goto end;
966
967 RAND_bytes(buf, SENDFILE_SZ);
968 out = BIO_new_file(tmpfilename, "wb");
969 if (!TEST_ptr(out))
970 goto end;
971
972 if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
973 goto end;
974
975 BIO_free(out);
976 out = NULL;
977 in = BIO_new_file(tmpfilename, "rb");
978 BIO_get_fp(in, &ffdp);
979 ffd = fileno(ffdp);
980
981 while (chunk_off < SENDFILE_SZ) {
982 chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
983 while ((err = SSL_sendfile(serverssl,
984 ffd,
985 chunk_off,
986 chunk_size,
987 0)) != chunk_size) {
988 if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
989 goto end;
990 }
991 while ((err = SSL_read(clientssl,
992 buf_dst + chunk_off,
993 chunk_size)) != chunk_size) {
994 if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
995 goto end;
996 }
997
998 /* verify the payload */
999 if (!TEST_mem_eq(buf_dst + chunk_off,
1000 chunk_size,
1001 buf + chunk_off,
1002 chunk_size))
1003 goto end;
1004
1005 chunk_off += chunk_size;
1006 }
1007
1008 testresult = 1;
1009end:
1010 if (clientssl) {
1011 SSL_shutdown(clientssl);
1012 SSL_free(clientssl);
1013 }
1014 if (serverssl) {
1015 SSL_shutdown(serverssl);
1016 SSL_free(serverssl);
1017 }
1018 SSL_CTX_free(sctx);
1019 SSL_CTX_free(cctx);
1020 serverssl = clientssl = NULL;
1021 BIO_free(out);
1022 BIO_free(in);
1023 OPENSSL_free(buf);
1024 OPENSSL_free(buf_dst);
1025 return testresult;
1026}
1027
2fab79af
BP
1028static int test_ktls_no_txrx_client_no_txrx_server(void)
1029{
1030 return execute_test_ktls(0, 0, 0, 0);
1031}
1032
1033static int test_ktls_no_rx_client_no_txrx_server(void)
1034{
1035 return execute_test_ktls(1, 0, 0, 0);
1036}
1037
1038static int test_ktls_no_tx_client_no_txrx_server(void)
1039{
1040 return execute_test_ktls(0, 1, 0, 0);
1041}
1042
1043static int test_ktls_client_no_txrx_server(void)
1044{
1045 return execute_test_ktls(1, 1, 0, 0);
1046}
1047
1048static int test_ktls_no_txrx_client_no_rx_server(void)
1049{
1050 return execute_test_ktls(0, 0, 1, 0);
1051}
1052
1053static int test_ktls_no_rx_client_no_rx_server(void)
1054{
1055 return execute_test_ktls(1, 0, 1, 0);
1056}
1057
1058static int test_ktls_no_tx_client_no_rx_server(void)
1059{
1060 return execute_test_ktls(0, 1, 1, 0);
1061}
1062
1063static int test_ktls_client_no_rx_server(void)
fe5d9450 1064{
2fab79af 1065 return execute_test_ktls(1, 1, 1, 0);
fe5d9450
BP
1066}
1067
2fab79af 1068static int test_ktls_no_txrx_client_no_tx_server(void)
fe5d9450 1069{
2fab79af 1070 return execute_test_ktls(0, 0, 0, 1);
fe5d9450
BP
1071}
1072
2fab79af 1073static int test_ktls_no_rx_client_no_tx_server(void)
fe5d9450 1074{
2fab79af 1075 return execute_test_ktls(1, 0, 0, 1);
fe5d9450
BP
1076}
1077
2fab79af
BP
1078static int test_ktls_no_tx_client_no_tx_server(void)
1079{
1080 return execute_test_ktls(0, 1, 0, 1);
1081}
1082
1083static int test_ktls_client_no_tx_server(void)
1084{
1085 return execute_test_ktls(1, 1, 0, 1);
1086}
1087
1088static int test_ktls_no_txrx_client_server(void)
1089{
1090 return execute_test_ktls(0, 0, 1, 1);
1091}
1092
1093static int test_ktls_no_rx_client_server(void)
1094{
1095 return execute_test_ktls(1, 0, 1, 1);
1096}
1097
1098static int test_ktls_no_tx_client_server(void)
1099{
1100 return execute_test_ktls(0, 1, 1, 1);
1101}
1102
1103static int test_ktls_client_server(void)
fe5d9450 1104{
2fab79af 1105 return execute_test_ktls(1, 1, 1, 1);
fe5d9450 1106}
fe5d9450
BP
1107#endif
1108
84d5549e
MC
1109static int test_large_message_tls(void)
1110{
7856332e 1111 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
5c587fb6 1112 TLS1_VERSION, 0, 0);
7856332e
MC
1113}
1114
1115static int test_large_message_tls_read_ahead(void)
1116{
1117 return execute_test_large_message(TLS_server_method(), TLS_client_method(),
5c587fb6 1118 TLS1_VERSION, 0, 1);
84d5549e
MC
1119}
1120
55386bef 1121#ifndef OPENSSL_NO_DTLS
84d5549e
MC
1122static int test_large_message_dtls(void)
1123{
7856332e
MC
1124 /*
1125 * read_ahead is not relevant to DTLS because DTLS always acts as if
1126 * read_ahead is set.
1127 */
84d5549e 1128 return execute_test_large_message(DTLS_server_method(),
7d7f6834 1129 DTLS_client_method(),
5c587fb6 1130 DTLS1_VERSION, 0, 0);
84d5549e 1131}
55386bef 1132#endif
84d5549e 1133
8f8c11d8 1134#ifndef OPENSSL_NO_OCSP
ba881d3b
MC
1135static int ocsp_server_cb(SSL *s, void *arg)
1136{
1137 int *argi = (int *)arg;
710756a9 1138 unsigned char *copy = NULL;
ba881d3b
MC
1139 STACK_OF(OCSP_RESPID) *ids = NULL;
1140 OCSP_RESPID *id = NULL;
1141
1142 if (*argi == 2) {
1143 /* In this test we are expecting exactly 1 OCSP_RESPID */
1144 SSL_get_tlsext_status_ids(s, &ids);
1145 if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
1146 return SSL_TLSEXT_ERR_ALERT_FATAL;
1147
1148 id = sk_OCSP_RESPID_value(ids, 0);
1149 if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
1150 return SSL_TLSEXT_ERR_ALERT_FATAL;
1151 } else if (*argi != 1) {
1152 return SSL_TLSEXT_ERR_ALERT_FATAL;
1153 }
1154
710756a9 1155 if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
ba881d3b
MC
1156 return SSL_TLSEXT_ERR_ALERT_FATAL;
1157
710756a9 1158 SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
ba881d3b 1159 ocsp_server_called = 1;
ba881d3b
MC
1160 return SSL_TLSEXT_ERR_OK;
1161}
1162
1163static int ocsp_client_cb(SSL *s, void *arg)
1164{
1165 int *argi = (int *)arg;
1166 const unsigned char *respderin;
1167 size_t len;
1168
1169 if (*argi != 1 && *argi != 2)
1170 return 0;
1171
1172 len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
710756a9 1173 if (!TEST_mem_eq(orespder, len, respderin, len))
ba881d3b
MC
1174 return 0;
1175
1176 ocsp_client_called = 1;
ba881d3b
MC
1177 return 1;
1178}
1179
2cb4b5f6
MC
1180static int test_tlsext_status_type(void)
1181{
ba881d3b
MC
1182 SSL_CTX *cctx = NULL, *sctx = NULL;
1183 SSL *clientssl = NULL, *serverssl = NULL;
2cb4b5f6 1184 int testresult = 0;
ba881d3b
MC
1185 STACK_OF(OCSP_RESPID) *ids = NULL;
1186 OCSP_RESPID *id = NULL;
1187 BIO *certbio = NULL;
2cb4b5f6 1188
7d7f6834 1189 if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 1190 TLS1_VERSION, 0,
7d7f6834 1191 &sctx, &cctx, cert, privkey))
ba881d3b 1192 return 0;
2cb4b5f6 1193
710756a9 1194 if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
2cb4b5f6 1195 goto end;
2cb4b5f6 1196
ba881d3b 1197 /* First just do various checks getting and setting tlsext_status_type */
2cb4b5f6 1198
ba881d3b 1199 clientssl = SSL_new(cctx);
710756a9
RS
1200 if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
1201 || !TEST_true(SSL_set_tlsext_status_type(clientssl,
1202 TLSEXT_STATUSTYPE_ocsp))
1203 || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
1204 TLSEXT_STATUSTYPE_ocsp))
2cb4b5f6 1205 goto end;
2cb4b5f6 1206
ba881d3b
MC
1207 SSL_free(clientssl);
1208 clientssl = NULL;
2cb4b5f6 1209
710756a9
RS
1210 if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
1211 || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
2cb4b5f6 1212 goto end;
2cb4b5f6 1213
ba881d3b 1214 clientssl = SSL_new(cctx);
710756a9 1215 if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
2cb4b5f6 1216 goto end;
ba881d3b
MC
1217 SSL_free(clientssl);
1218 clientssl = NULL;
1219
1220 /*
1221 * Now actually do a handshake and check OCSP information is exchanged and
1222 * the callbacks get called
1223 */
ba881d3b
MC
1224 SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
1225 SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
1226 SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
1227 SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
710756a9
RS
1228 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1229 &clientssl, NULL, NULL))
1230 || !TEST_true(create_ssl_connection(serverssl, clientssl,
1231 SSL_ERROR_NONE))
1232 || !TEST_true(ocsp_client_called)
1233 || !TEST_true(ocsp_server_called))
ba881d3b 1234 goto end;
ba881d3b
MC
1235 SSL_free(serverssl);
1236 SSL_free(clientssl);
1237 serverssl = NULL;
1238 clientssl = NULL;
1239
1240 /* Try again but this time force the server side callback to fail */
1241 ocsp_client_called = 0;
1242 ocsp_server_called = 0;
1243 cdummyarg = 0;
710756a9
RS
1244 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1245 &clientssl, NULL, NULL))
1246 /* This should fail because the callback will fail */
1247 || !TEST_false(create_ssl_connection(serverssl, clientssl,
1248 SSL_ERROR_NONE))
1249 || !TEST_false(ocsp_client_called)
1250 || !TEST_false(ocsp_server_called))
ba881d3b 1251 goto end;
ba881d3b
MC
1252 SSL_free(serverssl);
1253 SSL_free(clientssl);
1254 serverssl = NULL;
1255 clientssl = NULL;
1256
1257 /*
1258 * This time we'll get the client to send an OCSP_RESPID that it will
1259 * accept.
1260 */
1261 ocsp_client_called = 0;
1262 ocsp_server_called = 0;
1263 cdummyarg = 2;
710756a9
RS
1264 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1265 &clientssl, NULL, NULL)))
ba881d3b 1266 goto end;
ba881d3b
MC
1267
1268 /*
1269 * We'll just use any old cert for this test - it doesn't have to be an OCSP
69687aa8 1270 * specific one. We'll use the server cert.
ba881d3b 1271 */
710756a9
RS
1272 if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
1273 || !TEST_ptr(id = OCSP_RESPID_new())
1274 || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
1275 || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
1276 NULL, NULL, NULL))
1277 || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
1278 || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
ba881d3b 1279 goto end;
ba881d3b
MC
1280 id = NULL;
1281 SSL_set_tlsext_status_ids(clientssl, ids);
1282 /* Control has been transferred */
1283 ids = NULL;
1284
1285 BIO_free(certbio);
1286 certbio = NULL;
1287
710756a9
RS
1288 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1289 SSL_ERROR_NONE))
1290 || !TEST_true(ocsp_client_called)
1291 || !TEST_true(ocsp_server_called))
ba881d3b 1292 goto end;
ba881d3b 1293
2cb4b5f6
MC
1294 testresult = 1;
1295
1296 end:
ba881d3b
MC
1297 SSL_free(serverssl);
1298 SSL_free(clientssl);
1299 SSL_CTX_free(sctx);
1300 SSL_CTX_free(cctx);
1301 sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
1302 OCSP_RESPID_free(id);
1303 BIO_free(certbio);
1304 X509_free(ocspcert);
1305 ocspcert = NULL;
2cb4b5f6
MC
1306
1307 return testresult;
1308}
8f8c11d8 1309#endif
2cb4b5f6 1310
bf208d95 1311#if !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
0afca811 1312static int new_called, remove_called, get_called;
eaa776da 1313
eaa776da
MC
1314static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
1315{
1316 new_called++;
c0537ebd
MC
1317 /*
1318 * sess has been up-refed for us, but we don't actually need it so free it
1319 * immediately.
1320 */
1321 SSL_SESSION_free(sess);
eaa776da
MC
1322 return 1;
1323}
1324
1325static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
1326{
1327 remove_called++;
1328}
1329
7a301f08
MC
1330static SSL_SESSION *get_sess_val = NULL;
1331
1332static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
1333 int *copy)
1334{
0afca811 1335 get_called++;
7a301f08
MC
1336 *copy = 1;
1337 return get_sess_val;
1338}
1339
7a301f08
MC
1340static int execute_test_session(int maxprot, int use_int_cache,
1341 int use_ext_cache)
2cb4b5f6
MC
1342{
1343 SSL_CTX *sctx = NULL, *cctx = NULL;
1344 SSL *serverssl1 = NULL, *clientssl1 = NULL;
1345 SSL *serverssl2 = NULL, *clientssl2 = NULL;
bf208d95 1346# ifndef OPENSSL_NO_TLS1_1
eaa776da 1347 SSL *serverssl3 = NULL, *clientssl3 = NULL;
bf208d95 1348# endif
2cb4b5f6 1349 SSL_SESSION *sess1 = NULL, *sess2 = NULL;
36ff232c 1350 int testresult = 0, numnewsesstick = 1;
2cb4b5f6 1351
7e885b7b
P
1352 new_called = remove_called = 0;
1353
36ff232c
MC
1354 /* TLSv1.3 sends 2 NewSessionTickets */
1355 if (maxprot == TLS1_3_VERSION)
1356 numnewsesstick = 2;
1357
7d7f6834 1358 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 1359 TLS1_VERSION, 0,
7d7f6834 1360 &sctx, &cctx, cert, privkey)))
2cb4b5f6 1361 return 0;
2cb4b5f6 1362
7a301f08
MC
1363 /*
1364 * Only allow the max protocol version so we can force a connection failure
1365 * later
1366 */
1367 SSL_CTX_set_min_proto_version(cctx, maxprot);
1368 SSL_CTX_set_max_proto_version(cctx, maxprot);
eaa776da
MC
1369
1370 /* Set up session cache */
7e885b7b 1371 if (use_ext_cache) {
eaa776da
MC
1372 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
1373 SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
1374 }
7e885b7b 1375 if (use_int_cache) {
eaa776da
MC
1376 /* Also covers instance where both are set */
1377 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
1378 } else {
1379 SSL_CTX_set_session_cache_mode(cctx,
1380 SSL_SESS_CACHE_CLIENT
1381 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1382 }
2cb4b5f6 1383
710756a9
RS
1384 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
1385 NULL, NULL))
1386 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
1387 SSL_ERROR_NONE))
1388 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
2cb4b5f6 1389 goto end;
2cb4b5f6 1390
710756a9 1391 /* Should fail because it should already be in the cache */
7e885b7b 1392 if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
eaa776da 1393 goto end;
7a301f08 1394 if (use_ext_cache
36ff232c
MC
1395 && (!TEST_int_eq(new_called, numnewsesstick)
1396
1397 || !TEST_int_eq(remove_called, 0)))
b4982125 1398 goto end;
b4982125 1399
c0537ebd
MC
1400 new_called = remove_called = 0;
1401 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1402 &clientssl2, NULL, NULL))
1403 || !TEST_true(SSL_set_session(clientssl2, sess1))
1404 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1405 SSL_ERROR_NONE))
1406 || !TEST_true(SSL_session_reused(clientssl2)))
1407 goto end;
1408
7a301f08
MC
1409 if (maxprot == TLS1_3_VERSION) {
1410 /*
1411 * In TLSv1.3 we should have created a new session even though we have
4cb00457
MC
1412 * resumed. Since we attempted a resume we should also have removed the
1413 * old ticket from the cache so that we try to only use tickets once.
7a301f08
MC
1414 */
1415 if (use_ext_cache
1416 && (!TEST_int_eq(new_called, 1)
4cb00457 1417 || !TEST_int_eq(remove_called, 1)))
7a301f08
MC
1418 goto end;
1419 } else {
1420 /*
1421 * In TLSv1.2 we expect to have resumed so no sessions added or
1422 * removed.
1423 */
1424 if (use_ext_cache
1425 && (!TEST_int_eq(new_called, 0)
1426 || !TEST_int_eq(remove_called, 0)))
1427 goto end;
1428 }
c0537ebd
MC
1429
1430 SSL_SESSION_free(sess1);
1431 if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
1432 goto end;
1433 shutdown_ssl_connection(serverssl2, clientssl2);
1434 serverssl2 = clientssl2 = NULL;
c0537ebd
MC
1435
1436 new_called = remove_called = 0;
710756a9
RS
1437 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1438 &clientssl2, NULL, NULL))
1439 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1440 SSL_ERROR_NONE)))
2cb4b5f6 1441 goto end;
2cb4b5f6 1442
710756a9 1443 if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
2cb4b5f6 1444 goto end;
2cb4b5f6 1445
7a301f08 1446 if (use_ext_cache
36ff232c
MC
1447 && (!TEST_int_eq(new_called, numnewsesstick)
1448 || !TEST_int_eq(remove_called, 0)))
eaa776da 1449 goto end;
eaa776da 1450
c0537ebd 1451 new_called = remove_called = 0;
2cb4b5f6 1452 /*
710756a9
RS
1453 * This should clear sess2 from the cache because it is a "bad" session.
1454 * See SSL_set_session() documentation.
2cb4b5f6 1455 */
710756a9 1456 if (!TEST_true(SSL_set_session(clientssl2, sess1)))
2cb4b5f6 1457 goto end;
7a301f08
MC
1458 if (use_ext_cache
1459 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
eaa776da 1460 goto end;
710756a9 1461 if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
2cb4b5f6 1462 goto end;
2cb4b5f6 1463
7e885b7b 1464 if (use_int_cache) {
710756a9
RS
1465 /* Should succeeded because it should not already be in the cache */
1466 if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
1467 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
eaa776da 1468 goto end;
eaa776da
MC
1469 }
1470
c0537ebd 1471 new_called = remove_called = 0;
eaa776da 1472 /* This shouldn't be in the cache so should fail */
710756a9 1473 if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
2cb4b5f6 1474 goto end;
2cb4b5f6 1475
7a301f08
MC
1476 if (use_ext_cache
1477 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2cb4b5f6 1478 goto end;
2cb4b5f6 1479
bf208d95 1480# if !defined(OPENSSL_NO_TLS1_1)
c0537ebd 1481 new_called = remove_called = 0;
eaa776da
MC
1482 /* Force a connection failure */
1483 SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
710756a9
RS
1484 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
1485 &clientssl3, NULL, NULL))
1486 || !TEST_true(SSL_set_session(clientssl3, sess1))
c0537ebd 1487 /* This should fail because of the mismatched protocol versions */
710756a9
RS
1488 || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
1489 SSL_ERROR_NONE)))
eaa776da 1490 goto end;
b4982125 1491
eaa776da 1492 /* We should have automatically removed the session from the cache */
7a301f08
MC
1493 if (use_ext_cache
1494 && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2cb4b5f6 1495 goto end;
2cb4b5f6 1496
710756a9 1497 /* Should succeed because it should not already be in the cache */
7e885b7b 1498 if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
eaa776da 1499 goto end;
bf208d95 1500# endif
eaa776da 1501
7a301f08
MC
1502 /* Now do some tests for server side caching */
1503 if (use_ext_cache) {
1504 SSL_CTX_sess_set_new_cb(cctx, NULL);
1505 SSL_CTX_sess_set_remove_cb(cctx, NULL);
1506 SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
1507 SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
1508 SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
1509 get_sess_val = NULL;
1510 }
1511
1512 SSL_CTX_set_session_cache_mode(cctx, 0);
1513 /* Internal caching is the default on the server side */
1514 if (!use_int_cache)
1515 SSL_CTX_set_session_cache_mode(sctx,
1516 SSL_SESS_CACHE_SERVER
1517 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1518
1519 SSL_free(serverssl1);
1520 SSL_free(clientssl1);
1521 serverssl1 = clientssl1 = NULL;
1522 SSL_free(serverssl2);
1523 SSL_free(clientssl2);
1524 serverssl2 = clientssl2 = NULL;
1525 SSL_SESSION_free(sess1);
1526 sess1 = NULL;
1527 SSL_SESSION_free(sess2);
1528 sess2 = NULL;
1529
1530 SSL_CTX_set_max_proto_version(sctx, maxprot);
6cc0b3c2
MC
1531 if (maxprot == TLS1_2_VERSION)
1532 SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
0afca811 1533 new_called = remove_called = get_called = 0;
7a301f08
MC
1534 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
1535 NULL, NULL))
1536 || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
1537 SSL_ERROR_NONE))
1538 || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
1539 || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
1540 goto end;
1541
ee94ec2e
MC
1542 if (use_int_cache) {
1543 if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
1544 /*
1545 * In TLSv1.3 it should not have been added to the internal cache,
1546 * except in the case where we also have an external cache (in that
1547 * case it gets added to the cache in order to generate remove
1548 * events after timeout).
1549 */
1550 if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
1551 goto end;
1552 } else {
1553 /* Should fail because it should already be in the cache */
1554 if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
1555 goto end;
1556 }
1557 }
7a301f08
MC
1558
1559 if (use_ext_cache) {
1560 SSL_SESSION *tmp = sess2;
1561
36ff232c 1562 if (!TEST_int_eq(new_called, numnewsesstick)
0afca811
KY
1563 || !TEST_int_eq(remove_called, 0)
1564 || !TEST_int_eq(get_called, 0))
7a301f08
MC
1565 goto end;
1566 /*
1567 * Delete the session from the internal cache to force a lookup from
1568 * the external cache. We take a copy first because
1569 * SSL_CTX_remove_session() also marks the session as non-resumable.
1570 */
ee94ec2e 1571 if (use_int_cache && maxprot != TLS1_3_VERSION) {
3cb6a4d6
BK
1572 if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
1573 || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
1574 goto end;
1575 SSL_SESSION_free(sess2);
1576 }
7a301f08
MC
1577 sess2 = tmp;
1578 }
1579
0afca811 1580 new_called = remove_called = get_called = 0;
7a301f08
MC
1581 get_sess_val = sess2;
1582 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
1583 &clientssl2, NULL, NULL))
1584 || !TEST_true(SSL_set_session(clientssl2, sess1))
1585 || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
1586 SSL_ERROR_NONE))
1587 || !TEST_true(SSL_session_reused(clientssl2)))
1588 goto end;
1589
0afca811 1590 if (use_ext_cache) {
32305f88 1591 if (!TEST_int_eq(remove_called, 0))
0afca811
KY
1592 goto end;
1593
1594 if (maxprot == TLS1_3_VERSION) {
32305f88
MC
1595 if (!TEST_int_eq(new_called, 1)
1596 || !TEST_int_eq(get_called, 0))
0afca811
KY
1597 goto end;
1598 } else {
32305f88
MC
1599 if (!TEST_int_eq(new_called, 0)
1600 || !TEST_int_eq(get_called, 1))
0afca811
KY
1601 goto end;
1602 }
1603 }
7a301f08 1604
2cb4b5f6 1605 testresult = 1;
eaa776da 1606
2cb4b5f6
MC
1607 end:
1608 SSL_free(serverssl1);
1609 SSL_free(clientssl1);
1610 SSL_free(serverssl2);
1611 SSL_free(clientssl2);
bf208d95 1612# ifndef OPENSSL_NO_TLS1_1
eaa776da
MC
1613 SSL_free(serverssl3);
1614 SSL_free(clientssl3);
bf208d95 1615# endif
2cb4b5f6
MC
1616 SSL_SESSION_free(sess1);
1617 SSL_SESSION_free(sess2);
1618 SSL_CTX_free(sctx);
1619 SSL_CTX_free(cctx);
1620
1621 return testresult;
1622}
bf208d95 1623#endif /* !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
2cb4b5f6 1624
7fb4c820
MC
1625static int test_session_with_only_int_cache(void)
1626{
7a301f08
MC
1627#ifndef OPENSSL_NO_TLS1_3
1628 if (!execute_test_session(TLS1_3_VERSION, 1, 0))
1629 return 0;
1630#endif
1631
1632#ifndef OPENSSL_NO_TLS1_2
1633 return execute_test_session(TLS1_2_VERSION, 1, 0);
1634#else
1635 return 1;
1636#endif
eaa776da
MC
1637}
1638
7fb4c820
MC
1639static int test_session_with_only_ext_cache(void)
1640{
7a301f08
MC
1641#ifndef OPENSSL_NO_TLS1_3
1642 if (!execute_test_session(TLS1_3_VERSION, 0, 1))
1643 return 0;
1644#endif
1645
1646#ifndef OPENSSL_NO_TLS1_2
1647 return execute_test_session(TLS1_2_VERSION, 0, 1);
1648#else
1649 return 1;
1650#endif
eaa776da
MC
1651}
1652
7fb4c820
MC
1653static int test_session_with_both_cache(void)
1654{
7a301f08
MC
1655#ifndef OPENSSL_NO_TLS1_3
1656 if (!execute_test_session(TLS1_3_VERSION, 1, 1))
1657 return 0;
1658#endif
1659
1660#ifndef OPENSSL_NO_TLS1_2
1661 return execute_test_session(TLS1_2_VERSION, 1, 1);
1662#else
1663 return 1;
1664#endif
eaa776da
MC
1665}
1666
04225915 1667#ifndef OPENSSL_NO_TLS1_3
c22365b3
MC
1668static SSL_SESSION *sesscache[6];
1669static int do_cache;
36ff232c
MC
1670
1671static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
1672{
c22365b3
MC
1673 if (do_cache) {
1674 sesscache[new_called] = sess;
1675 } else {
1676 /* We don't need the reference to the session, so free it */
1677 SSL_SESSION_free(sess);
1678 }
1679 new_called++;
1680
1681 return 1;
1682}
1683
1684static int post_handshake_verify(SSL *sssl, SSL *cssl)
1685{
1686 SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
1687 if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
1688 return 0;
1689
1690 /* Start handshake on the server and client */
1691 if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
1692 || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
1693 || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
1694 || !TEST_true(create_ssl_connection(sssl, cssl,
1695 SSL_ERROR_NONE)))
1696 return 0;
36ff232c
MC
1697
1698 return 1;
1699}
1700
fbe9dafd 1701static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
03cdf559
MC
1702 SSL_CTX **cctx)
1703{
1704 int sess_id_ctx = 1;
1705
1706 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 1707 TLS1_VERSION, 0, sctx,
03cdf559
MC
1708 cctx, cert, privkey))
1709 || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
1710 || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
1711 (void *)&sess_id_ctx,
1712 sizeof(sess_id_ctx))))
1713 return 0;
1714
1715 if (stateful)
1716 SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
1717
1718 SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
1719 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1720 SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
1721
1722 return 1;
1723}
1724
1725static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
1726{
1727 SSL *serverssl = NULL, *clientssl = NULL;
1728 int i;
1729
1730 /* Test that we can resume with all the tickets we got given */
1731 for (i = 0; i < idx * 2; i++) {
1732 new_called = 0;
1733 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1734 &clientssl, NULL, NULL))
1735 || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
1736 goto end;
1737
32097b33 1738 SSL_set_post_handshake_auth(clientssl, 1);
03cdf559
MC
1739
1740 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1741 SSL_ERROR_NONE)))
1742 goto end;
1743
1744 /*
1745 * Following a successful resumption we only get 1 ticket. After a
1746 * failed one we should get idx tickets.
1747 */
1748 if (succ) {
1749 if (!TEST_true(SSL_session_reused(clientssl))
1750 || !TEST_int_eq(new_called, 1))
1751 goto end;
1752 } else {
1753 if (!TEST_false(SSL_session_reused(clientssl))
1754 || !TEST_int_eq(new_called, idx))
1755 goto end;
1756 }
1757
1758 new_called = 0;
1759 /* After a post-handshake authentication we should get 1 new ticket */
1760 if (succ
1761 && (!post_handshake_verify(serverssl, clientssl)
1762 || !TEST_int_eq(new_called, 1)))
1763 goto end;
1764
1765 SSL_shutdown(clientssl);
1766 SSL_shutdown(serverssl);
1767 SSL_free(serverssl);
1768 SSL_free(clientssl);
1769 serverssl = clientssl = NULL;
1770 SSL_SESSION_free(sesscache[i]);
1771 sesscache[i] = NULL;
1772 }
1773
1774 return 1;
1775
1776 end:
1777 SSL_free(clientssl);
1778 SSL_free(serverssl);
1779 return 0;
1780}
1781
04d7814a 1782static int test_tickets(int stateful, int idx)
36ff232c
MC
1783{
1784 SSL_CTX *sctx = NULL, *cctx = NULL;
1785 SSL *serverssl = NULL, *clientssl = NULL;
03cdf559 1786 int testresult = 0;
36ff232c
MC
1787 size_t j;
1788
1789 /* idx is the test number, but also the number of tickets we want */
1790
1791 new_called = 0;
c22365b3 1792 do_cache = 1;
36ff232c 1793
fbe9dafd 1794 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
36ff232c
MC
1795 goto end;
1796
03cdf559
MC
1797 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1798 &clientssl, NULL, NULL)))
1799 goto end;
1800
1801 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1802 SSL_ERROR_NONE))
1803 /* Check we got the number of tickets we were expecting */
1804 || !TEST_int_eq(idx, new_called))
1805 goto end;
04d7814a 1806
03cdf559
MC
1807 SSL_shutdown(clientssl);
1808 SSL_shutdown(serverssl);
1809 SSL_free(serverssl);
1810 SSL_free(clientssl);
1811 SSL_CTX_free(sctx);
1812 SSL_CTX_free(cctx);
1813 clientssl = serverssl = NULL;
1814 sctx = cctx = NULL;
1815
1816 /*
1817 * Now we try to resume with the tickets we previously created. The
1818 * resumption attempt is expected to fail (because we're now using a new
1819 * SSL_CTX). We should see idx number of tickets issued again.
1820 */
1821
1822 /* Stop caching sessions - just count them */
1823 do_cache = 0;
1824
fbe9dafd 1825 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
03cdf559
MC
1826 goto end;
1827
1828 if (!check_resumption(idx, sctx, cctx, 0))
1829 goto end;
1830
1831 /* Start again with caching sessions */
1832 new_called = 0;
1833 do_cache = 1;
fbe9dafd
MC
1834 SSL_CTX_free(sctx);
1835 SSL_CTX_free(cctx);
1836 sctx = cctx = NULL;
03cdf559 1837
fbe9dafd 1838 if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
03cdf559 1839 goto end;
36ff232c
MC
1840
1841 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1842 &clientssl, NULL, NULL)))
1843 goto end;
1844
32097b33 1845 SSL_set_post_handshake_auth(clientssl, 1);
36ff232c
MC
1846
1847 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1848 SSL_ERROR_NONE))
1849 /* Check we got the number of tickets we were expecting */
1850 || !TEST_int_eq(idx, new_called))
1851 goto end;
1852
1853 /* After a post-handshake authentication we should get new tickets issued */
c22365b3 1854 if (!post_handshake_verify(serverssl, clientssl)
36ff232c
MC
1855 || !TEST_int_eq(idx * 2, new_called))
1856 goto end;
1857
36ff232c
MC
1858 SSL_shutdown(clientssl);
1859 SSL_shutdown(serverssl);
1860 SSL_free(serverssl);
1861 SSL_free(clientssl);
1862 serverssl = clientssl = NULL;
1863
c22365b3
MC
1864 /* Stop caching sessions - just count them */
1865 do_cache = 0;
1866
03cdf559
MC
1867 /*
1868 * Check we can resume with all the tickets we created. This time around the
1869 * resumptions should all be successful.
1870 */
1871 if (!check_resumption(idx, sctx, cctx, 1))
1872 goto end;
36ff232c
MC
1873
1874 testresult = 1;
1875
1876 end:
1877 SSL_free(serverssl);
1878 SSL_free(clientssl);
c22365b3 1879 for (j = 0; j < OSSL_NELEM(sesscache); j++) {
36ff232c 1880 SSL_SESSION_free(sesscache[j]);
c22365b3
MC
1881 sesscache[j] = NULL;
1882 }
36ff232c
MC
1883 SSL_CTX_free(sctx);
1884 SSL_CTX_free(cctx);
1885
1886 return testresult;
1887}
04d7814a
MC
1888
1889static int test_stateless_tickets(int idx)
1890{
1891 return test_tickets(0, idx);
1892}
1893
1894static int test_stateful_tickets(int idx)
1895{
1896 return test_tickets(1, idx);
1897}
8614a4eb
MC
1898
1899static int test_psk_tickets(void)
1900{
1901 SSL_CTX *sctx = NULL, *cctx = NULL;
1902 SSL *serverssl = NULL, *clientssl = NULL;
1903 int testresult = 0;
1904 int sess_id_ctx = 1;
1905
1906 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 1907 TLS1_VERSION, 0, &sctx,
8614a4eb
MC
1908 &cctx, NULL, NULL))
1909 || !TEST_true(SSL_CTX_set_session_id_context(sctx,
1910 (void *)&sess_id_ctx,
1911 sizeof(sess_id_ctx))))
1912 goto end;
1913
1914 SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
1915 | SSL_SESS_CACHE_NO_INTERNAL_STORE);
1916 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
1917 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
1918 SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
1919 use_session_cb_cnt = 0;
1920 find_session_cb_cnt = 0;
1921 srvid = pskid;
1922 new_called = 0;
1923
1924 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1925 NULL, NULL)))
1926 goto end;
1927 clientpsk = serverpsk = create_a_psk(clientssl);
1928 if (!TEST_ptr(clientpsk))
1929 goto end;
1930 SSL_SESSION_up_ref(clientpsk);
1931
1932 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1933 SSL_ERROR_NONE))
1934 || !TEST_int_eq(1, find_session_cb_cnt)
1935 || !TEST_int_eq(1, use_session_cb_cnt)
1936 /* We should always get 1 ticket when using external PSK */
1937 || !TEST_int_eq(1, new_called))
1938 goto end;
1939
1940 testresult = 1;
1941
1942 end:
1943 SSL_free(serverssl);
1944 SSL_free(clientssl);
1945 SSL_CTX_free(sctx);
1946 SSL_CTX_free(cctx);
1947 SSL_SESSION_free(clientpsk);
1948 SSL_SESSION_free(serverpsk);
1949 clientpsk = serverpsk = NULL;
1950
1951 return testresult;
1952}
04225915 1953#endif
36ff232c 1954
7d4488bb
MC
1955#define USE_NULL 0
1956#define USE_BIO_1 1
1957#define USE_BIO_2 2
1958#define USE_DEFAULT 3
1959
1960#define CONNTYPE_CONNECTION_SUCCESS 0
1961#define CONNTYPE_CONNECTION_FAIL 1
1962#define CONNTYPE_NO_CONNECTION 2
1963
1964#define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
1965#define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
1966#if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
1967# define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
1968#else
1969# define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
1970#endif
1971
7d4488bb
MC
1972#define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
1973 + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
1974 + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
7fb4c820
MC
1975
1976static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
1977{
1978 switch (type) {
1979 case USE_NULL:
1980 *res = NULL;
1981 break;
1982 case USE_BIO_1:
1983 *res = bio1;
1984 break;
1985 case USE_BIO_2:
1986 *res = bio2;
1987 break;
1988 }
1989}
1990
7d4488bb
MC
1991
1992/*
1993 * Tests calls to SSL_set_bio() under various conditions.
1994 *
1995 * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
1996 * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
1997 * then do more tests where we create a successful connection first using our
1998 * standard connection setup functions, and then call SSL_set_bio() with
1999 * various combinations of valid BIOs or NULL. We then repeat these tests
2000 * following a failed connection. In this last case we are looking to check that
2001 * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
2002 */
7fb4c820
MC
2003static int test_ssl_set_bio(int idx)
2004{
7d4488bb 2005 SSL_CTX *sctx = NULL, *cctx = NULL;
7fb4c820
MC
2006 BIO *bio1 = NULL;
2007 BIO *bio2 = NULL;
0fae8150 2008 BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
7d4488bb
MC
2009 SSL *serverssl = NULL, *clientssl = NULL;
2010 int initrbio, initwbio, newrbio, newwbio, conntype;
7fb4c820
MC
2011 int testresult = 0;
2012
7d4488bb
MC
2013 if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
2014 initrbio = idx % 3;
2015 idx /= 3;
2016 initwbio = idx % 3;
2017 idx /= 3;
2018 newrbio = idx % 3;
2019 idx /= 3;
2020 newwbio = idx % 3;
2021 conntype = CONNTYPE_NO_CONNECTION;
2022 } else {
2023 idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
2024 initrbio = initwbio = USE_DEFAULT;
2025 newrbio = idx % 2;
2026 idx /= 2;
2027 newwbio = idx % 2;
2028 idx /= 2;
2029 conntype = idx % 2;
2030 }
710756a9 2031
7d4488bb 2032 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 2033 TLS1_VERSION, 0,
7d4488bb
MC
2034 &sctx, &cctx, cert, privkey)))
2035 goto end;
2036
2037 if (conntype == CONNTYPE_CONNECTION_FAIL) {
2038 /*
2039 * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
2040 * because we reduced the number of tests in the definition of
2041 * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
2042 * mismatched protocol versions we will force a connection failure.
2043 */
2044 SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
2045 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2046 }
2047
2048 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2049 NULL, NULL)))
710756a9 2050 goto end;
7fb4c820 2051
710756a9
RS
2052 if (initrbio == USE_BIO_1
2053 || initwbio == USE_BIO_1
2054 || newrbio == USE_BIO_1
7fb4c820 2055 || newwbio == USE_BIO_1) {
710756a9 2056 if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
7fb4c820 2057 goto end;
7fb4c820
MC
2058 }
2059
710756a9
RS
2060 if (initrbio == USE_BIO_2
2061 || initwbio == USE_BIO_2
2062 || newrbio == USE_BIO_2
7fb4c820 2063 || newwbio == USE_BIO_2) {
710756a9 2064 if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
7fb4c820 2065 goto end;
7fb4c820
MC
2066 }
2067
7d4488bb
MC
2068 if (initrbio != USE_DEFAULT) {
2069 setupbio(&irbio, bio1, bio2, initrbio);
2070 setupbio(&iwbio, bio1, bio2, initwbio);
2071 SSL_set_bio(clientssl, irbio, iwbio);
7fb4c820 2072
7d4488bb
MC
2073 /*
2074 * We want to maintain our own refs to these BIO, so do an up ref for
2075 * each BIO that will have ownership transferred in the SSL_set_bio()
2076 * call
2077 */
2078 if (irbio != NULL)
2079 BIO_up_ref(irbio);
2080 if (iwbio != NULL && iwbio != irbio)
2081 BIO_up_ref(iwbio);
2082 }
7fb4c820 2083
7d4488bb
MC
2084 if (conntype != CONNTYPE_NO_CONNECTION
2085 && !TEST_true(create_ssl_connection(serverssl, clientssl,
2086 SSL_ERROR_NONE)
2087 == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
2088 goto end;
7fb4c820
MC
2089
2090 setupbio(&nrbio, bio1, bio2, newrbio);
2091 setupbio(&nwbio, bio1, bio2, newwbio);
2092
2093 /*
2094 * We will (maybe) transfer ownership again so do more up refs.
2095 * SSL_set_bio() has some really complicated ownership rules where BIOs have
2096 * already been set!
2097 */
710756a9
RS
2098 if (nrbio != NULL
2099 && nrbio != irbio
2100 && (nwbio != iwbio || nrbio != nwbio))
7fb4c820 2101 BIO_up_ref(nrbio);
710756a9
RS
2102 if (nwbio != NULL
2103 && nwbio != nrbio
2104 && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
7fb4c820
MC
2105 BIO_up_ref(nwbio);
2106
7d4488bb 2107 SSL_set_bio(clientssl, nrbio, nwbio);
7fb4c820
MC
2108
2109 testresult = 1;
2110
2111 end:
7fb4c820
MC
2112 BIO_free(bio1);
2113 BIO_free(bio2);
710756a9 2114
7fb4c820
MC
2115 /*
2116 * This test is checking that the ref counting for SSL_set_bio is correct.
2117 * If we get here and we did too many frees then we will fail in the above
2118 * functions. If we haven't done enough then this will only be detected in
2119 * a crypto-mdebug build
2120 */
7d4488bb
MC
2121 SSL_free(serverssl);
2122 SSL_free(clientssl);
2123 SSL_CTX_free(sctx);
2124 SSL_CTX_free(cctx);
7fb4c820
MC
2125 return testresult;
2126}
2127
7e885b7b 2128typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
9a716987 2129
7e885b7b 2130static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
9a716987
MC
2131{
2132 BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
710756a9 2133 SSL_CTX *ctx;
9a716987
MC
2134 SSL *ssl = NULL;
2135 int testresult = 0;
2136
710756a9
RS
2137 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
2138 || !TEST_ptr(ssl = SSL_new(ctx))
2139 || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
2140 || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
9a716987 2141 goto end;
9a716987
MC
2142
2143 BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
2144
2145 /*
2146 * If anything goes wrong here then we could leak memory, so this will
2147 * be caught in a crypto-mdebug build
2148 */
2149 BIO_push(sslbio, membio1);
2150
69687aa8 2151 /* Verify changing the rbio/wbio directly does not cause leaks */
7e885b7b 2152 if (change_bio != NO_BIO_CHANGE) {
710756a9 2153 if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem())))
9a716987 2154 goto end;
7e885b7b 2155 if (change_bio == CHANGE_RBIO)
65e2d672 2156 SSL_set0_rbio(ssl, membio2);
9a716987 2157 else
65e2d672 2158 SSL_set0_wbio(ssl, membio2);
9a716987
MC
2159 }
2160 ssl = NULL;
2161
7e885b7b 2162 if (pop_ssl)
9a716987
MC
2163 BIO_pop(sslbio);
2164 else
2165 BIO_pop(membio1);
2166
2167 testresult = 1;
2168 end:
2169 BIO_free(membio1);
2170 BIO_free(sslbio);
2171 SSL_free(ssl);
2172 SSL_CTX_free(ctx);
2173
2174 return testresult;
2175}
2176
2177static int test_ssl_bio_pop_next_bio(void)
2178{
7e885b7b 2179 return execute_test_ssl_bio(0, NO_BIO_CHANGE);
9a716987
MC
2180}
2181
2182static int test_ssl_bio_pop_ssl_bio(void)
2183{
7e885b7b 2184 return execute_test_ssl_bio(1, NO_BIO_CHANGE);
9a716987
MC
2185}
2186
2187static int test_ssl_bio_change_rbio(void)
2188{
7e885b7b 2189 return execute_test_ssl_bio(0, CHANGE_RBIO);
9a716987
MC
2190}
2191
2192static int test_ssl_bio_change_wbio(void)
2193{
7e885b7b 2194 return execute_test_ssl_bio(0, CHANGE_WBIO);
9a716987
MC
2195}
2196
c423ecaa 2197#if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
f1b25aae
MC
2198typedef struct {
2199 /* The list of sig algs */
2200 const int *list;
2201 /* The length of the list */
2202 size_t listlen;
2203 /* A sigalgs list in string format */
2204 const char *liststr;
2205 /* Whether setting the list should succeed */
2206 int valid;
2207 /* Whether creating a connection with the list should succeed */
2208 int connsuccess;
2209} sigalgs_list;
2210
2211static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
c423ecaa 2212# ifndef OPENSSL_NO_EC
f1b25aae
MC
2213static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
2214static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
c423ecaa 2215# endif
f1b25aae
MC
2216static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
2217static const int invalidlist2[] = {NID_sha256, NID_undef};
2218static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
2219static const int invalidlist4[] = {NID_sha256};
2220static const sigalgs_list testsigalgs[] = {
2221 {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
c423ecaa 2222# ifndef OPENSSL_NO_EC
f1b25aae
MC
2223 {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
2224 {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
c423ecaa 2225# endif
f1b25aae 2226 {NULL, 0, "RSA+SHA256", 1, 1},
c423ecaa 2227# ifndef OPENSSL_NO_EC
f1b25aae
MC
2228 {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
2229 {NULL, 0, "ECDSA+SHA512", 1, 0},
c423ecaa 2230# endif
f1b25aae
MC
2231 {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
2232 {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
2233 {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
2234 {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
2235 {NULL, 0, "RSA", 0, 0},
2236 {NULL, 0, "SHA256", 0, 0},
2237 {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
710756a9
RS
2238 {NULL, 0, "Invalid", 0, 0}
2239};
f1b25aae
MC
2240
2241static int test_set_sigalgs(int idx)
2242{
2243 SSL_CTX *cctx = NULL, *sctx = NULL;
2244 SSL *clientssl = NULL, *serverssl = NULL;
2245 int testresult = 0;
2246 const sigalgs_list *curr;
2247 int testctx;
2248
2249 /* Should never happen */
710756a9 2250 if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
f1b25aae
MC
2251 return 0;
2252
2253 testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
2254 curr = testctx ? &testsigalgs[idx]
2255 : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
2256
7d7f6834 2257 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 2258 TLS1_VERSION, 0,
7d7f6834 2259 &sctx, &cctx, cert, privkey)))
f1b25aae 2260 return 0;
f1b25aae 2261
d2e491f2
MC
2262 /*
2263 * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
2264 * for TLSv1.2 for now until we add a new API.
2265 */
2266 SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2267
f1b25aae
MC
2268 if (testctx) {
2269 int ret;
710756a9 2270
f1b25aae
MC
2271 if (curr->list != NULL)
2272 ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
2273 else
2274 ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
2275
2276 if (!ret) {
2277 if (curr->valid)
710756a9 2278 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
f1b25aae
MC
2279 else
2280 testresult = 1;
2281 goto end;
2282 }
2283 if (!curr->valid) {
710756a9 2284 TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
f1b25aae
MC
2285 goto end;
2286 }
2287 }
2288
710756a9
RS
2289 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2290 &clientssl, NULL, NULL)))
f1b25aae 2291 goto end;
f1b25aae
MC
2292
2293 if (!testctx) {
2294 int ret;
2295
2296 if (curr->list != NULL)
2297 ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
2298 else
2299 ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
2300 if (!ret) {
2301 if (curr->valid)
710756a9 2302 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
f1b25aae
MC
2303 else
2304 testresult = 1;
2305 goto end;
2306 }
710756a9 2307 if (!curr->valid)
f1b25aae 2308 goto end;
f1b25aae
MC
2309 }
2310
710756a9
RS
2311 if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
2312 SSL_ERROR_NONE),
2313 curr->connsuccess))
f1b25aae 2314 goto end;
f1b25aae
MC
2315
2316 testresult = 1;
2317
2318 end:
2319 SSL_free(serverssl);
2320 SSL_free(clientssl);
2321 SSL_CTX_free(sctx);
2322 SSL_CTX_free(cctx);
2323
2324 return testresult;
2325}
c423ecaa 2326#endif
f1b25aae 2327
fff202e5 2328#ifndef OPENSSL_NO_TLS1_3
532f9578
MC
2329static int psk_client_cb_cnt = 0;
2330static int psk_server_cb_cnt = 0;
02a3ed5a
MC
2331
2332static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
2333 size_t *idlen, SSL_SESSION **sess)
2334{
2335 switch (++use_session_cb_cnt) {
2336 case 1:
2337 /* The first call should always have a NULL md */
2338 if (md != NULL)
2339 return 0;
2340 break;
2341
2342 case 2:
2343 /* The second call should always have an md */
2344 if (md == NULL)
2345 return 0;
2346 break;
2347
2348 default:
2349 /* We should only be called a maximum of twice */
2350 return 0;
2351 }
2352
57dee9bb
MC
2353 if (clientpsk != NULL)
2354 SSL_SESSION_up_ref(clientpsk);
02a3ed5a 2355
57dee9bb 2356 *sess = clientpsk;
02a3ed5a
MC
2357 *id = (const unsigned char *)pskid;
2358 *idlen = strlen(pskid);
2359
2360 return 1;
2361}
2362
c2b290c3 2363#ifndef OPENSSL_NO_PSK
532f9578
MC
2364static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
2365 unsigned int max_id_len,
2366 unsigned char *psk,
2367 unsigned int max_psk_len)
2368{
2369 unsigned int psklen = 0;
2370
2371 psk_client_cb_cnt++;
2372
2373 if (strlen(pskid) + 1 > max_id_len)
2374 return 0;
2375
2376 /* We should only ever be called a maximum of twice per connection */
2377 if (psk_client_cb_cnt > 2)
2378 return 0;
2379
2380 if (clientpsk == NULL)
2381 return 0;
2382
2383 /* We'll reuse the PSK we set up for TLSv1.3 */
2384 if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
2385 return 0;
2386 psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
2387 strncpy(id, pskid, max_id_len);
2388
2389 return psklen;
2390}
c2b290c3 2391#endif /* OPENSSL_NO_PSK */
532f9578 2392
02a3ed5a
MC
2393static int find_session_cb(SSL *ssl, const unsigned char *identity,
2394 size_t identity_len, SSL_SESSION **sess)
2395{
2396 find_session_cb_cnt++;
2397
2398 /* We should only ever be called a maximum of twice per connection */
2399 if (find_session_cb_cnt > 2)
2400 return 0;
2401
57dee9bb 2402 if (serverpsk == NULL)
02a3ed5a
MC
2403 return 0;
2404
2405 /* Identity should match that set by the client */
2406 if (strlen(srvid) != identity_len
2407 || strncmp(srvid, (const char *)identity, identity_len) != 0) {
2408 /* No PSK found, continue but without a PSK */
2409 *sess = NULL;
2410 return 1;
2411 }
2412
57dee9bb
MC
2413 SSL_SESSION_up_ref(serverpsk);
2414 *sess = serverpsk;
02a3ed5a
MC
2415
2416 return 1;
2417}
2418
c2b290c3 2419#ifndef OPENSSL_NO_PSK
532f9578
MC
2420static unsigned int psk_server_cb(SSL *ssl, const char *identity,
2421 unsigned char *psk, unsigned int max_psk_len)
2422{
2423 unsigned int psklen = 0;
2424
2425 psk_server_cb_cnt++;
2426
2427 /* We should only ever be called a maximum of twice per connection */
2428 if (find_session_cb_cnt > 2)
2429 return 0;
2430
2431 if (serverpsk == NULL)
2432 return 0;
2433
2434 /* Identity should match that set by the client */
2435 if (strcmp(srvid, identity) != 0) {
2436 return 0;
2437 }
2438
2439 /* We'll reuse the PSK we set up for TLSv1.3 */
2440 if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
2441 return 0;
2442 psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
2443
2444 return psklen;
2445}
c2b290c3 2446#endif /* OPENSSL_NO_PSK */
532f9578 2447
5f982038
MC
2448#define MSG1 "Hello"
2449#define MSG2 "World."
2450#define MSG3 "This"
2451#define MSG4 "is"
2452#define MSG5 "a"
90049cea
MC
2453#define MSG6 "test"
2454#define MSG7 "message."
5f982038 2455
02a3ed5a 2456#define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
532f9578 2457#define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
02a3ed5a 2458
8614a4eb
MC
2459
2460static SSL_SESSION *create_a_psk(SSL *ssl)
2461{
2462 const SSL_CIPHER *cipher = NULL;
2463 const unsigned char key[] = {
2464 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
2465 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
2466 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
2467 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
2468 0x2c, 0x2d, 0x2e, 0x2f
2469 };
2470 SSL_SESSION *sess = NULL;
2471
2472 cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
2473 sess = SSL_SESSION_new();
2474 if (!TEST_ptr(sess)
2475 || !TEST_ptr(cipher)
2476 || !TEST_true(SSL_SESSION_set1_master_key(sess, key,
2477 sizeof(key)))
2478 || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
2479 || !TEST_true(
2480 SSL_SESSION_set_protocol_version(sess,
2481 TLS1_3_VERSION))) {
2482 SSL_SESSION_free(sess);
2483 return NULL;
2484 }
2485 return sess;
2486}
2487
5f982038
MC
2488/*
2489 * Helper method to setup objects for early data test. Caller frees objects on
2490 * error.
2491 */
2492static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
3cb47b4e 2493 SSL **serverssl, SSL_SESSION **sess, int idx)
5f982038 2494{
5a421415
MC
2495 if (*sctx == NULL
2496 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(),
2497 TLS_client_method(),
5c587fb6 2498 TLS1_VERSION, 0,
5a421415
MC
2499 sctx, cctx, cert, privkey)))
2500 return 0;
2501
2502 if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
5f982038 2503 return 0;
5f982038 2504
02a3ed5a
MC
2505 if (idx == 1) {
2506 /* When idx == 1 we repeat the tests with read_ahead set */
3cb47b4e
MC
2507 SSL_CTX_set_read_ahead(*cctx, 1);
2508 SSL_CTX_set_read_ahead(*sctx, 1);
02a3ed5a
MC
2509 } else if (idx == 2) {
2510 /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
2511 SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
2512 SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
2513 use_session_cb_cnt = 0;
2514 find_session_cb_cnt = 0;
2515 srvid = pskid;
3cb47b4e
MC
2516 }
2517
710756a9 2518 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
02a3ed5a
MC
2519 NULL, NULL)))
2520 return 0;
2521
141e4709
MC
2522 /*
2523 * For one of the run throughs (doesn't matter which one), we'll try sending
2524 * some SNI data in the initial ClientHello. This will be ignored (because
2525 * there is no SNI cb set up by the server), so it should not impact
2526 * early_data.
2527 */
2528 if (idx == 1
2529 && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
2530 return 0;
2531
02a3ed5a 2532 if (idx == 2) {
8614a4eb 2533 clientpsk = create_a_psk(*clientssl);
57dee9bb 2534 if (!TEST_ptr(clientpsk)
02a3ed5a
MC
2535 /*
2536 * We just choose an arbitrary value for max_early_data which
2537 * should be big enough for testing purposes.
2538 */
57dee9bb
MC
2539 || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
2540 0x100))
2541 || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
2542 SSL_SESSION_free(clientpsk);
2543 clientpsk = NULL;
02a3ed5a
MC
2544 return 0;
2545 }
57dee9bb 2546 serverpsk = clientpsk;
02a3ed5a 2547
c20e3b28
MC
2548 if (sess != NULL) {
2549 if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
2550 SSL_SESSION_free(clientpsk);
2551 SSL_SESSION_free(serverpsk);
2552 clientpsk = serverpsk = NULL;
2553 return 0;
2554 }
57dee9bb 2555 *sess = clientpsk;
c20e3b28 2556 }
02a3ed5a
MC
2557 return 1;
2558 }
2559
2560 if (sess == NULL)
2561 return 1;
2562
2563 if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
2564 SSL_ERROR_NONE)))
5f982038 2565 return 0;
5f982038
MC
2566
2567 *sess = SSL_get1_session(*clientssl);
5f982038
MC
2568 SSL_shutdown(*clientssl);
2569 SSL_shutdown(*serverssl);
5f982038
MC
2570 SSL_free(*serverssl);
2571 SSL_free(*clientssl);
2572 *serverssl = *clientssl = NULL;
2573
710756a9
RS
2574 if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
2575 clientssl, NULL, NULL))
2576 || !TEST_true(SSL_set_session(*clientssl, *sess)))
5f982038 2577 return 0;
5f982038
MC
2578
2579 return 1;
2580}
2581
3cb47b4e 2582static int test_early_data_read_write(int idx)
5f982038
MC
2583{
2584 SSL_CTX *cctx = NULL, *sctx = NULL;
2585 SSL *clientssl = NULL, *serverssl = NULL;
2586 int testresult = 0;
2587 SSL_SESSION *sess = NULL;
9b5c865d
MC
2588 unsigned char buf[20], data[1024];
2589 size_t readbytes, written, eoedlen, rawread, rawwritten;
2590 BIO *rbio;
5f982038 2591
710756a9
RS
2592 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2593 &serverssl, &sess, idx)))
5f982038
MC
2594 goto end;
2595
2596 /* Write and read some early data */
710756a9
RS
2597 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2598 &written))
2599 || !TEST_size_t_eq(written, strlen(MSG1))
2600 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
2601 sizeof(buf), &readbytes),
2602 SSL_READ_EARLY_DATA_SUCCESS)
2603 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
2604 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2605 SSL_EARLY_DATA_ACCEPTED))
5f982038 2606 goto end;
5f982038
MC
2607
2608 /*
09f28874 2609 * Server should be able to write data, and client should be able to
5f982038
MC
2610 * read it.
2611 */
710756a9
RS
2612 if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
2613 &written))
2614 || !TEST_size_t_eq(written, strlen(MSG2))
2615 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2616 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
5f982038 2617 goto end;
5f982038
MC
2618
2619 /* Even after reading normal data, client should be able write early data */
710756a9
RS
2620 if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
2621 &written))
2622 || !TEST_size_t_eq(written, strlen(MSG3)))
5f982038 2623 goto end;
5f982038 2624
09f28874 2625 /* Server should still be able read early data after writing data */
710756a9
RS
2626 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2627 &readbytes),
2628 SSL_READ_EARLY_DATA_SUCCESS)
2629 || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
5f982038 2630 goto end;
5f982038 2631
09f28874 2632 /* Write more data from server and read it from client */
710756a9
RS
2633 if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
2634 &written))
2635 || !TEST_size_t_eq(written, strlen(MSG4))
2636 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2637 || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
5f982038 2638 goto end;
5f982038
MC
2639
2640 /*
2641 * If client writes normal data it should mean writing early data is no
2642 * longer possible.
2643 */
710756a9
RS
2644 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
2645 || !TEST_size_t_eq(written, strlen(MSG5))
2646 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
2647 SSL_EARLY_DATA_ACCEPTED))
5f982038 2648 goto end;
5f982038 2649
9b5c865d
MC
2650 /*
2651 * At this point the client has written EndOfEarlyData, ClientFinished and
2652 * normal (fully protected) data. We are going to cause a delay between the
2653 * arrival of EndOfEarlyData and ClientFinished. We read out all the data
2654 * in the read BIO, and then just put back the EndOfEarlyData message.
2655 */
2656 rbio = SSL_get_rbio(serverssl);
710756a9
RS
2657 if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
2658 || !TEST_size_t_lt(rawread, sizeof(data))
2659 || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
9b5c865d 2660 goto end;
710756a9 2661
9b5c865d
MC
2662 /* Record length is in the 4th and 5th bytes of the record header */
2663 eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
710756a9
RS
2664 if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
2665 || !TEST_size_t_eq(rawwritten, eoedlen))
9b5c865d 2666 goto end;
9b5c865d 2667
5f982038 2668 /* Server should be told that there is no more early data */
710756a9
RS
2669 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2670 &readbytes),
2671 SSL_READ_EARLY_DATA_FINISH)
2672 || !TEST_size_t_eq(readbytes, 0))
5f982038 2673 goto end;
5f982038 2674
9b5c865d
MC
2675 /*
2676 * Server has not finished init yet, so should still be able to write early
2677 * data.
2678 */
710756a9
RS
2679 if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
2680 &written))
2681 || !TEST_size_t_eq(written, strlen(MSG6)))
9b5c865d 2682 goto end;
9b5c865d 2683
f8a303fa 2684 /* Push the ClientFinished and the normal data back into the server rbio */
710756a9
RS
2685 if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
2686 &rawwritten))
2687 || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
f8a303fa 2688 goto end;
f8a303fa 2689
5f982038 2690 /* Server should be able to read normal data */
710756a9
RS
2691 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2692 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
5f982038 2693 goto end;
5f982038 2694
09f28874 2695 /* Client and server should not be able to write/read early data now */
710756a9
RS
2696 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
2697 &written)))
5f982038 2698 goto end;
5f982038 2699 ERR_clear_error();
710756a9
RS
2700 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2701 &readbytes),
2702 SSL_READ_EARLY_DATA_ERROR))
5f982038 2703 goto end;
5f982038
MC
2704 ERR_clear_error();
2705
9b5c865d 2706 /* Client should be able to read the data sent by the server */
710756a9
RS
2707 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2708 || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
9b5c865d 2709 goto end;
710756a9 2710
f8a303fa 2711 /*
36ff232c
MC
2712 * Make sure we process the two NewSessionTickets. These arrive
2713 * post-handshake. We attempt reads which we do not expect to return any
2714 * data.
f8a303fa 2715 */
36ff232c
MC
2716 if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2717 || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
2718 &readbytes)))
f8a303fa 2719 goto end;
5f982038 2720
90049cea 2721 /* Server should be able to write normal data */
710756a9
RS
2722 if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
2723 || !TEST_size_t_eq(written, strlen(MSG7))
2724 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
2725 || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
90049cea 2726 goto end;
90049cea 2727
c20e3b28 2728 SSL_SESSION_free(sess);
5f982038 2729 sess = SSL_get1_session(clientssl);
02a3ed5a
MC
2730 use_session_cb_cnt = 0;
2731 find_session_cb_cnt = 0;
5f982038
MC
2732
2733 SSL_shutdown(clientssl);
2734 SSL_shutdown(serverssl);
5f982038
MC
2735 SSL_free(serverssl);
2736 SSL_free(clientssl);
2737 serverssl = clientssl = NULL;
710756a9
RS
2738 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2739 &clientssl, NULL, NULL))
2740 || !TEST_true(SSL_set_session(clientssl, sess)))
5f982038 2741 goto end;
5f982038
MC
2742
2743 /* Write and read some early data */
710756a9
RS
2744 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2745 &written))
2746 || !TEST_size_t_eq(written, strlen(MSG1))
2747 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2748 &readbytes),
2749 SSL_READ_EARLY_DATA_SUCCESS)
2750 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
5f982038 2751 goto end;
5f982038 2752
710756a9
RS
2753 if (!TEST_int_gt(SSL_connect(clientssl), 0)
2754 || !TEST_int_gt(SSL_accept(serverssl), 0))
5f982038 2755 goto end;
5f982038 2756
09f28874 2757 /* Client and server should not be able to write/read early data now */
710756a9
RS
2758 if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
2759 &written)))
5f982038 2760 goto end;
5f982038 2761 ERR_clear_error();
710756a9
RS
2762 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2763 &readbytes),
2764 SSL_READ_EARLY_DATA_ERROR))
5f982038 2765 goto end;
5f982038
MC
2766 ERR_clear_error();
2767
2768 /* Client and server should be able to write/read normal data */
710756a9
RS
2769 if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
2770 || !TEST_size_t_eq(written, strlen(MSG5))
2771 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
2772 || !TEST_size_t_eq(readbytes, strlen(MSG5)))
5f982038 2773 goto end;
5f982038
MC
2774
2775 testresult = 1;
2776
2777 end:
c20e3b28 2778 SSL_SESSION_free(sess);
57dee9bb
MC
2779 SSL_SESSION_free(clientpsk);
2780 SSL_SESSION_free(serverpsk);
2781 clientpsk = serverpsk = NULL;
5f982038
MC
2782 SSL_free(serverssl);
2783 SSL_free(clientssl);
2784 SSL_CTX_free(sctx);
2785 SSL_CTX_free(cctx);
5f982038
MC
2786 return testresult;
2787}
2788
5a421415
MC
2789static int allow_ed_cb_called = 0;
2790
2791static int allow_early_data_cb(SSL *s, void *arg)
2792{
2793 int *usecb = (int *)arg;
2794
2795 allow_ed_cb_called++;
2796
2797 if (*usecb == 1)
2798 return 0;
2799
2800 return 1;
2801}
2802
2803/*
2804 * idx == 0: Standard early_data setup
2805 * idx == 1: early_data setup using read_ahead
2806 * usecb == 0: Don't use a custom early data callback
2807 * usecb == 1: Use a custom early data callback and reject the early data
2808 * usecb == 2: Use a custom early data callback and accept the early data
3bb5e5b0
MC
2809 * confopt == 0: Configure anti-replay directly
2810 * confopt == 1: Configure anti-replay using SSL_CONF
5a421415 2811 */
3bb5e5b0 2812static int test_early_data_replay_int(int idx, int usecb, int confopt)
78fb5374
MC
2813{
2814 SSL_CTX *cctx = NULL, *sctx = NULL;
2815 SSL *clientssl = NULL, *serverssl = NULL;
2816 int testresult = 0;
2817 SSL_SESSION *sess = NULL;
5a421415
MC
2818 size_t readbytes, written;
2819 unsigned char buf[20];
2820
2821 allow_ed_cb_called = 0;
2822
2823 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 2824 TLS1_VERSION, 0, &sctx,
5a421415
MC
2825 &cctx, cert, privkey)))
2826 return 0;
2827
2828 if (usecb > 0) {
3bb5e5b0
MC
2829 if (confopt == 0) {
2830 SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
2831 } else {
2832 SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
2833
2834 if (!TEST_ptr(confctx))
2835 goto end;
2836 SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
2837 | SSL_CONF_FLAG_SERVER);
2838 SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
2839 if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
2840 2)) {
2841 SSL_CONF_CTX_free(confctx);
2842 goto end;
2843 }
2844 SSL_CONF_CTX_free(confctx);
2845 }
5a421415
MC
2846 SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
2847 }
78fb5374
MC
2848
2849 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2850 &serverssl, &sess, idx)))
2851 goto end;
2852
2853 /*
2854 * The server is configured to accept early data. Create a connection to
2855 * "use up" the ticket
2856 */
2857 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2858 || !TEST_true(SSL_session_reused(clientssl)))
2859 goto end;
2860
2861 SSL_shutdown(clientssl);
2862 SSL_shutdown(serverssl);
2863 SSL_free(serverssl);
2864 SSL_free(clientssl);
2865 serverssl = clientssl = NULL;
2866
2867 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2868 &clientssl, NULL, NULL))
5a421415
MC
2869 || !TEST_true(SSL_set_session(clientssl, sess)))
2870 goto end;
2871
2872 /* Write and read some early data */
2873 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2874 &written))
2875 || !TEST_size_t_eq(written, strlen(MSG1)))
2876 goto end;
2877
2878 if (usecb <= 1) {
2879 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2880 &readbytes),
2881 SSL_READ_EARLY_DATA_FINISH)
2882 /*
2883 * The ticket was reused, so the we should have rejected the
2884 * early data
2885 */
2886 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2887 SSL_EARLY_DATA_REJECTED))
2888 goto end;
2889 } else {
2890 /* In this case the callback decides to accept the early data */
2891 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2892 &readbytes),
2893 SSL_READ_EARLY_DATA_SUCCESS)
2894 || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
2895 /*
2896 * Server will have sent its flight so client can now send
2897 * end of early data and complete its half of the handshake
2898 */
2899 || !TEST_int_gt(SSL_connect(clientssl), 0)
2900 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2901 &readbytes),
2902 SSL_READ_EARLY_DATA_FINISH)
2903 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2904 SSL_EARLY_DATA_ACCEPTED))
2905 goto end;
2906 }
2907
2908 /* Complete the connection */
2909 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
2910 || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
2911 || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
78fb5374
MC
2912 goto end;
2913
2914 testresult = 1;
2915
2916 end:
c20e3b28 2917 SSL_SESSION_free(sess);
78fb5374
MC
2918 SSL_SESSION_free(clientpsk);
2919 SSL_SESSION_free(serverpsk);
2920 clientpsk = serverpsk = NULL;
2921 SSL_free(serverssl);
2922 SSL_free(clientssl);
2923 SSL_CTX_free(sctx);
2924 SSL_CTX_free(cctx);
2925 return testresult;
2926}
2927
5a421415
MC
2928static int test_early_data_replay(int idx)
2929{
3bb5e5b0 2930 int ret = 1, usecb, confopt;
5a421415 2931
3bb5e5b0
MC
2932 for (usecb = 0; usecb < 3; usecb++) {
2933 for (confopt = 0; confopt < 2; confopt++)
2934 ret &= test_early_data_replay_int(idx, usecb, confopt);
2935 }
5a421415
MC
2936
2937 return ret;
2938}
2939
710756a9 2940/*
6b84e6bf
MC
2941 * Helper function to test that a server attempting to read early data can
2942 * handle a connection from a client where the early data should be skipped.
0d1b7789
MC
2943 * testtype: 0 == No HRR
2944 * testtype: 1 == HRR
0efa0ba4
MC
2945 * testtype: 2 == HRR, invalid early_data sent after HRR
2946 * testtype: 3 == recv_max_early_data set to 0
710756a9 2947 */
0d1b7789 2948static int early_data_skip_helper(int testtype, int idx)
5f982038
MC
2949{
2950 SSL_CTX *cctx = NULL, *sctx = NULL;
2951 SSL *clientssl = NULL, *serverssl = NULL;
2952 int testresult = 0;
018fcbec 2953 SSL_SESSION *sess = NULL;
5f982038
MC
2954 unsigned char buf[20];
2955 size_t readbytes, written;
2956
710756a9
RS
2957 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
2958 &serverssl, &sess, idx)))
5f982038
MC
2959 goto end;
2960
0efa0ba4 2961 if (testtype == 1 || testtype == 2) {
6b84e6bf
MC
2962 /* Force an HRR to occur */
2963 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
2964 goto end;
02a3ed5a
MC
2965 } else if (idx == 2) {
2966 /*
2967 * We force early_data rejection by ensuring the PSK identity is
2968 * unrecognised
2969 */
2970 srvid = "Dummy Identity";
6b84e6bf
MC
2971 } else {
2972 /*
2973 * Deliberately corrupt the creation time. We take 20 seconds off the
2974 * time. It could be any value as long as it is not within tolerance.
2975 * This should mean the ticket is rejected.
2976 */
5d99881e 2977 if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
6b84e6bf
MC
2978 goto end;
2979 }
5f982038 2980
0efa0ba4 2981 if (testtype == 3
0d1b7789
MC
2982 && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
2983 goto end;
2984
5f982038 2985 /* Write some early data */
710756a9
RS
2986 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
2987 &written))
2988 || !TEST_size_t_eq(written, strlen(MSG1)))
5f982038 2989 goto end;
5f982038 2990
0d1b7789 2991 /* Server should reject the early data */
710756a9
RS
2992 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
2993 &readbytes),
2994 SSL_READ_EARLY_DATA_FINISH)
2995 || !TEST_size_t_eq(readbytes, 0)
2996 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
2997 SSL_EARLY_DATA_REJECTED))
5f982038 2998 goto end;
5f982038 2999
0efa0ba4
MC
3000 switch (testtype) {
3001 case 0:
3002 /* Nothing to do */
3003 break;
3004
3005 case 1:
6b84e6bf
MC
3006 /*
3007 * Finish off the handshake. We perform the same writes and reads as
3008 * further down but we expect them to fail due to the incomplete
3009 * handshake.
3010 */
3011 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3012 || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
3013 &readbytes)))
3014 goto end;
0efa0ba4
MC
3015 break;
3016
3017 case 2:
3018 {
3019 BIO *wbio = SSL_get_wbio(clientssl);
3020 /* A record that will appear as bad early_data */
3021 const unsigned char bad_early_data[] = {
3022 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
3023 };
3024
3025 /*
3026 * We force the client to attempt a write. This will fail because
3027 * we're still in the handshake. It will cause the second
3028 * ClientHello to be sent.
3029 */
3030 if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
3031 &written)))
3032 goto end;
3033
3034 /*
3035 * Inject some early_data after the second ClientHello. This should
3036 * cause the server to fail
3037 */
3038 if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
3039 sizeof(bad_early_data), &written)))
3040 goto end;
3041 }
3042 /* fallthrough */
3043
3044 case 3:
0d1b7789 3045 /*
0efa0ba4
MC
3046 * This client has sent more early_data than we are willing to skip
3047 * (case 3) or sent invalid early_data (case 2) so the connection should
3048 * abort.
0d1b7789
MC
3049 */
3050 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3051 || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
3052 goto end;
3053
3054 /* Connection has failed - nothing more to do */
3055 testresult = 1;
3056 goto end;
0efa0ba4
MC
3057
3058 default:
3059 TEST_error("Invalid test type");
3060 goto end;
6b84e6bf
MC
3061 }
3062
0d1b7789
MC
3063 /*
3064 * Should be able to send normal data despite rejection of early data. The
3065 * early_data should be skipped.
3066 */
710756a9
RS
3067 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3068 || !TEST_size_t_eq(written, strlen(MSG2))
3069 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3070 SSL_EARLY_DATA_REJECTED)
3071 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3072 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
5f982038 3073 goto end;
5f982038
MC
3074
3075 testresult = 1;
3076
3077 end:
c20e3b28 3078 SSL_SESSION_free(clientpsk);
57dee9bb
MC
3079 SSL_SESSION_free(serverpsk);
3080 clientpsk = serverpsk = NULL;
5f982038
MC
3081 SSL_SESSION_free(sess);
3082 SSL_free(serverssl);
3083 SSL_free(clientssl);
3084 SSL_CTX_free(sctx);
3085 SSL_CTX_free(cctx);
5f982038
MC
3086 return testresult;
3087}
3088
6b84e6bf
MC
3089/*
3090 * Test that a server attempting to read early data can handle a connection
3091 * from a client where the early data is not acceptable.
3092 */
3093static int test_early_data_skip(int idx)
3094{
3095 return early_data_skip_helper(0, idx);
3096}
3097
3098/*
3099 * Test that a server attempting to read early data can handle a connection
3100 * from a client where an HRR occurs.
3101 */
3102static int test_early_data_skip_hrr(int idx)
3103{
3104 return early_data_skip_helper(1, idx);
3105}
3106
0efa0ba4
MC
3107/*
3108 * Test that a server attempting to read early data can handle a connection
3109 * from a client where an HRR occurs and correctly fails if early_data is sent
3110 * after the HRR
3111 */
3112static int test_early_data_skip_hrr_fail(int idx)
3113{
3114 return early_data_skip_helper(2, idx);
3115}
3116
0d1b7789
MC
3117/*
3118 * Test that a server attempting to read early data will abort if it tries to
3119 * skip over too much.
3120 */
3121static int test_early_data_skip_abort(int idx)
3122{
0efa0ba4 3123 return early_data_skip_helper(3, idx);
0d1b7789
MC
3124}
3125
710756a9
RS
3126/*
3127 * Test that a server attempting to read early data can handle a connection
3128 * from a client that doesn't send any.
3129 */
3cb47b4e 3130static int test_early_data_not_sent(int idx)
5f982038
MC
3131{
3132 SSL_CTX *cctx = NULL, *sctx = NULL;
3133 SSL *clientssl = NULL, *serverssl = NULL;
3134 int testresult = 0;
018fcbec 3135 SSL_SESSION *sess = NULL;
5f982038
MC
3136 unsigned char buf[20];
3137 size_t readbytes, written;
3138
710756a9
RS
3139 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3140 &serverssl, &sess, idx)))
5f982038
MC
3141 goto end;
3142
3143 /* Write some data - should block due to handshake with server */
3144 SSL_set_connect_state(clientssl);
710756a9 3145 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
5f982038 3146 goto end;
5f982038
MC
3147
3148 /* Server should detect that early data has not been sent */
710756a9
RS
3149 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3150 &readbytes),
3151 SSL_READ_EARLY_DATA_FINISH)
3152 || !TEST_size_t_eq(readbytes, 0)
3153 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3154 SSL_EARLY_DATA_NOT_SENT)
3155 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3156 SSL_EARLY_DATA_NOT_SENT))
5f982038 3157 goto end;
5f982038
MC
3158
3159 /* Continue writing the message we started earlier */
710756a9
RS
3160 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
3161 || !TEST_size_t_eq(written, strlen(MSG1))
3162 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3163 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
3164 || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
3165 || !TEST_size_t_eq(written, strlen(MSG2)))
5f982038 3166 goto end;
5f982038 3167
710756a9
RS
3168 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3169 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
5f982038 3170 goto end;
5f982038
MC
3171
3172 testresult = 1;
3173
3174 end:
5f982038 3175 SSL_SESSION_free(sess);
c20e3b28 3176 SSL_SESSION_free(clientpsk);
57dee9bb
MC
3177 SSL_SESSION_free(serverpsk);
3178 clientpsk = serverpsk = NULL;
5f982038
MC
3179 SSL_free(serverssl);
3180 SSL_free(clientssl);
3181 SSL_CTX_free(sctx);
3182 SSL_CTX_free(cctx);
5f982038
MC
3183 return testresult;
3184}
3185
976e5323
MC
3186static int hostname_cb(SSL *s, int *al, void *arg)
3187{
3188 const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
3189
281bf233 3190 if (hostname != NULL && strcmp(hostname, "goodhost") == 0)
976e5323
MC
3191 return SSL_TLSEXT_ERR_OK;
3192
3193 return SSL_TLSEXT_ERR_NOACK;
3194}
3195
3196static const char *servalpn;
3197
c7558d5b
PY
3198static int alpn_select_cb(SSL *ssl, const unsigned char **out,
3199 unsigned char *outlen, const unsigned char *in,
3200 unsigned int inlen, void *arg)
976e5323 3201{
c7558d5b 3202 unsigned int protlen = 0;
976e5323
MC
3203 const unsigned char *prot;
3204
c7558d5b
PY
3205 for (prot = in; prot < in + inlen; prot += protlen) {
3206 protlen = *prot++;
5d99881e 3207 if (in + inlen < prot + protlen)
976e5323
MC
3208 return SSL_TLSEXT_ERR_NOACK;
3209
3210 if (protlen == strlen(servalpn)
0bd42fde 3211 && memcmp(prot, servalpn, protlen) == 0) {
976e5323
MC
3212 *out = prot;
3213 *outlen = protlen;
3214 return SSL_TLSEXT_ERR_OK;
3215 }
3216 }
3217
3218 return SSL_TLSEXT_ERR_NOACK;
3219}
3220
57dee9bb 3221/* Test that a PSK can be used to send early_data */
976e5323
MC
3222static int test_early_data_psk(int idx)
3223{
3224 SSL_CTX *cctx = NULL, *sctx = NULL;
3225 SSL *clientssl = NULL, *serverssl = NULL;
3226 int testresult = 0;
3227 SSL_SESSION *sess = NULL;
57dee9bb
MC
3228 unsigned char alpnlist[] = {
3229 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
3230 'l', 'p', 'n'
3231 };
3232#define GOODALPNLEN 9
3233#define BADALPNLEN 8
3234#define GOODALPN (alpnlist)
3235#define BADALPN (alpnlist + GOODALPNLEN)
3236 int err = 0;
976e5323
MC
3237 unsigned char buf[20];
3238 size_t readbytes, written;
57dee9bb 3239 int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
976e5323
MC
3240 int edstatus = SSL_EARLY_DATA_ACCEPTED;
3241
3242 /* We always set this up with a final parameter of "2" for PSK */
3243 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3244 &serverssl, &sess, 2)))
3245 goto end;
3246
976e5323
MC
3247 servalpn = "goodalpn";
3248
57dee9bb
MC
3249 /*
3250 * Note: There is no test for inconsistent SNI with late client detection.
3251 * This is because servers do not acknowledge SNI even if they are using
3252 * it in a resumption handshake - so it is not actually possible for a
3253 * client to detect a problem.
3254 */
976e5323
MC
3255 switch (idx) {
3256 case 0:
57dee9bb 3257 /* Set inconsistent SNI (early client detection) */
976e5323
MC
3258 err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
3259 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
3260 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
3261 goto end;
3262 break;
3263
3264 case 1:
57dee9bb 3265 /* Set inconsistent ALPN (early client detection) */
976e5323
MC
3266 err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
3267 /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
57dee9bb
MC
3268 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
3269 GOODALPNLEN))
3270 || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
3271 BADALPNLEN)))
976e5323
MC
3272 goto end;
3273 break;
3274
3275 case 2:
3276 /*
3277 * Set invalid protocol version. Technically this affects PSKs without
3278 * early_data too, but we test it here because it is similar to the
3279 * SNI/ALPN consistency tests.
3280 */
3281 err = SSL_R_BAD_PSK;
3282 if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
3283 goto end;
3284 break;
3285
3286 case 3:
3287 /*
3288 * Set inconsistent SNI (server detected). In this case the connection
3289 * will succeed but reject early_data.
3290 */
281bf233
MC
3291 SSL_SESSION_free(serverpsk);
3292 serverpsk = SSL_SESSION_dup(clientpsk);
3293 if (!TEST_ptr(serverpsk)
3294 || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
3295 goto end;
976e5323
MC
3296 edstatus = SSL_EARLY_DATA_REJECTED;
3297 readearlyres = SSL_READ_EARLY_DATA_FINISH;
3298 /* Fall through */
3299 case 4:
3300 /* Set consistent SNI */
976e5323
MC
3301 if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
3302 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
3303 || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
3304 hostname_cb)))
3305 goto end;
3306 break;
3307
3308 case 5:
3309 /*
3310 * Set inconsistent ALPN (server detected). In this case the connection
3311 * will succeed but reject early_data.
3312 */
3313 servalpn = "badalpn";
3314 edstatus = SSL_EARLY_DATA_REJECTED;
3315 readearlyres = SSL_READ_EARLY_DATA_FINISH;
3316 /* Fall through */
3317 case 6:
976e5323 3318 /*
57dee9bb 3319 * Set consistent ALPN.
976e5323
MC
3320 * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
3321 * accepts a list of protos (each one length prefixed).
3322 * SSL_set1_alpn_selected accepts a single protocol (not length
3323 * prefixed)
3324 */
57dee9bb
MC
3325 if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
3326 GOODALPNLEN - 1))
3327 || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
3328 GOODALPNLEN)))
976e5323
MC
3329 goto end;
3330
3331 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
3332 break;
3333
57dee9bb
MC
3334 case 7:
3335 /* Set inconsistent ALPN (late client detection) */
3336 SSL_SESSION_free(serverpsk);
3337 serverpsk = SSL_SESSION_dup(clientpsk);
3338 if (!TEST_ptr(serverpsk)
3339 || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
3340 BADALPN + 1,
3341 BADALPNLEN - 1))
3342 || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
3343 GOODALPN + 1,
3344 GOODALPNLEN - 1))
3345 || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
3346 sizeof(alpnlist))))
3347 goto end;
3348 SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
3349 edstatus = SSL_EARLY_DATA_ACCEPTED;
3350 readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
3351 /* SSL_connect() call should fail */
3352 connectres = -1;
3353 break;
3354
976e5323
MC
3355 default:
3356 TEST_error("Bad test index");
3357 goto end;
3358 }
3359
3360 SSL_set_connect_state(clientssl);
3361 if (err != 0) {
3362 if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3363 &written))
3364 || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
3365 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
3366 goto end;
3367 } else {
3368 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
57dee9bb
MC
3369 &written)))
3370 goto end;
3371
3372 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3373 &readbytes), readearlyres)
976e5323
MC
3374 || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
3375 && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
57dee9bb
MC
3376 || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
3377 || !TEST_int_eq(SSL_connect(clientssl), connectres))
976e5323
MC
3378 goto end;
3379 }
3380
3381 testresult = 1;
3382
3383 end:
c20e3b28 3384 SSL_SESSION_free(sess);
57dee9bb
MC
3385 SSL_SESSION_free(clientpsk);
3386 SSL_SESSION_free(serverpsk);
3387 clientpsk = serverpsk = NULL;
976e5323
MC
3388 SSL_free(serverssl);
3389 SSL_free(clientssl);
3390 SSL_CTX_free(sctx);
3391 SSL_CTX_free(cctx);
3392 return testresult;
3393}
3394
710756a9
RS
3395/*
3396 * Test that a server that doesn't try to read early data can handle a
3397 * client sending some.
3398 */
3cb47b4e 3399static int test_early_data_not_expected(int idx)
5f982038
MC
3400{
3401 SSL_CTX *cctx = NULL, *sctx = NULL;
3402 SSL *clientssl = NULL, *serverssl = NULL;
3403 int testresult = 0;
018fcbec 3404 SSL_SESSION *sess = NULL;
5f982038
MC
3405 unsigned char buf[20];
3406 size_t readbytes, written;
3407
710756a9
RS
3408 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3409 &serverssl, &sess, idx)))
5f982038
MC
3410 goto end;
3411
3412 /* Write some early data */
710756a9
RS
3413 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3414 &written)))
5f982038 3415 goto end;
5f982038
MC
3416
3417 /*
3418 * Server should skip over early data and then block waiting for client to
3419 * continue handshake
3420 */
710756a9
RS
3421 if (!TEST_int_le(SSL_accept(serverssl), 0)
3422 || !TEST_int_gt(SSL_connect(clientssl), 0)
3423 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3424 SSL_EARLY_DATA_REJECTED)
3425 || !TEST_int_gt(SSL_accept(serverssl), 0)
3426 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3427 SSL_EARLY_DATA_REJECTED))
5f982038 3428 goto end;
5f982038
MC
3429
3430 /* Send some normal data from client to server */
710756a9
RS
3431 if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3432 || !TEST_size_t_eq(written, strlen(MSG2)))
5f982038 3433 goto end;
5f982038 3434
710756a9
RS
3435 if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3436 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
5f982038 3437 goto end;
5f982038
MC
3438
3439 testresult = 1;
3440
3441 end:
5f982038 3442 SSL_SESSION_free(sess);
c20e3b28 3443 SSL_SESSION_free(clientpsk);
57dee9bb
MC
3444 SSL_SESSION_free(serverpsk);
3445 clientpsk = serverpsk = NULL;
5f982038
MC
3446 SSL_free(serverssl);
3447 SSL_free(clientssl);
3448 SSL_CTX_free(sctx);
3449 SSL_CTX_free(cctx);
5f982038
MC
3450 return testresult;
3451}
3452
3453
3454# ifndef OPENSSL_NO_TLS1_2
710756a9
RS
3455/*
3456 * Test that a server attempting to read early data can handle a connection
3457 * from a TLSv1.2 client.
3458 */
3cb47b4e 3459static int test_early_data_tls1_2(int idx)
5f982038
MC
3460{
3461 SSL_CTX *cctx = NULL, *sctx = NULL;
3462 SSL *clientssl = NULL, *serverssl = NULL;
3463 int testresult = 0;
3464 unsigned char buf[20];
3465 size_t readbytes, written;
3466
02a3ed5a
MC
3467 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3468 &serverssl, NULL, idx)))
5f982038 3469 goto end;
5f982038
MC
3470
3471 /* Write some data - should block due to handshake with server */
3472 SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
3473 SSL_set_connect_state(clientssl);
710756a9 3474 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
5f982038 3475 goto end;
5f982038
MC
3476
3477 /*
3478 * Server should do TLSv1.2 handshake. First it will block waiting for more
f533fbd4
MC
3479 * messages from client after ServerDone. Then SSL_read_early_data should
3480 * finish and detect that early data has not been sent
5f982038 3481 */
710756a9
RS
3482 if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3483 &readbytes),
3484 SSL_READ_EARLY_DATA_ERROR))
5f982038 3485 goto end;
5f982038
MC
3486
3487 /*
3488 * Continue writing the message we started earlier. Will still block waiting
3489 * for the CCS/Finished from server
3490 */
710756a9
RS
3491 if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
3492 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3493 &readbytes),
3494 SSL_READ_EARLY_DATA_FINISH)
3495 || !TEST_size_t_eq(readbytes, 0)
3496 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3497 SSL_EARLY_DATA_NOT_SENT))
5f982038 3498 goto end;
5f982038
MC
3499
3500 /* Continue writing the message we started earlier */
710756a9
RS
3501 if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
3502 || !TEST_size_t_eq(written, strlen(MSG1))
3503 || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3504 SSL_EARLY_DATA_NOT_SENT)
3505 || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3506 || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
3507 || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
3508 || !TEST_size_t_eq(written, strlen(MSG2))
3509 || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
3510 || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
5f982038 3511 goto end;
5f982038
MC
3512
3513 testresult = 1;
3514
3515 end:
57dee9bb
MC
3516 SSL_SESSION_free(clientpsk);
3517 SSL_SESSION_free(serverpsk);
3518 clientpsk = serverpsk = NULL;
5f982038
MC
3519 SSL_free(serverssl);
3520 SSL_free(clientssl);
3521 SSL_CTX_free(sctx);
3522 SSL_CTX_free(cctx);
3523
3524 return testresult;
3525}
ca0413ae
MC
3526# endif /* OPENSSL_NO_TLS1_2 */
3527
034cb87b
MC
3528/*
3529 * Test configuring the TLSv1.3 ciphersuites
3530 *
3531 * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
3532 * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
3533 * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
3534 * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
3535 * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
3536 * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
3537 * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
3538 * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
3539 * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
3540 * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
3541 */
3542static int test_set_ciphersuite(int idx)
3543{
3544 SSL_CTX *cctx = NULL, *sctx = NULL;
3545 SSL *clientssl = NULL, *serverssl = NULL;
3546 int testresult = 0;
3547
3548 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 3549 TLS1_VERSION, 0,
034cb87b
MC
3550 &sctx, &cctx, cert, privkey))
3551 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
3552 "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
3553 goto end;
3554
3555 if (idx >=4 && idx <= 7) {
3556 /* SSL_CTX explicit cipher list */
3557 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
3558 goto end;
3559 }
3560
3561 if (idx == 0 || idx == 4) {
3562 /* Default ciphersuite */
3563 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3564 "TLS_AES_128_GCM_SHA256")))
3565 goto end;
3566 } else if (idx == 1 || idx == 5) {
3567 /* Non default ciphersuite */
3568 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3569 "TLS_AES_128_CCM_SHA256")))
3570 goto end;
3571 }
3572
3573 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3574 &clientssl, NULL, NULL)))
3575 goto end;
3576
3577 if (idx == 8 || idx == 9) {
3578 /* SSL explicit cipher list */
3579 if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
3580 goto end;
3581 }
3582
3583 if (idx == 2 || idx == 6 || idx == 8) {
3584 /* Default ciphersuite */
3585 if (!TEST_true(SSL_set_ciphersuites(clientssl,
3586 "TLS_AES_128_GCM_SHA256")))
3587 goto end;
3588 } else if (idx == 3 || idx == 7 || idx == 9) {
3589 /* Non default ciphersuite */
3590 if (!TEST_true(SSL_set_ciphersuites(clientssl,
3591 "TLS_AES_128_CCM_SHA256")))
3592 goto end;
3593 }
3594
3595 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
3596 goto end;
3597
3598 testresult = 1;
3599
3600 end:
3601 SSL_free(serverssl);
3602 SSL_free(clientssl);
3603 SSL_CTX_free(sctx);
3604 SSL_CTX_free(cctx);
3605
3606 return testresult;
3607}
3608
ca0413ae
MC
3609static int test_ciphersuite_change(void)
3610{
3611 SSL_CTX *cctx = NULL, *sctx = NULL;
3612 SSL *clientssl = NULL, *serverssl = NULL;
3613 SSL_SESSION *clntsess = NULL;
3614 int testresult = 0;
3615 const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
3616
3617 /* Create a session based on SHA-256 */
7d7f6834 3618 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 3619 TLS1_VERSION, 0,
7d7f6834 3620 &sctx, &cctx, cert, privkey))
f865b081
MC
3621 || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
3622 "TLS_AES_128_GCM_SHA256"))
ca0413ae
MC
3623 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3624 &clientssl, NULL, NULL))
3625 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3626 SSL_ERROR_NONE)))
3627 goto end;
3628
3629 clntsess = SSL_get1_session(clientssl);
3630 /* Save for later */
3631 aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
3632 SSL_shutdown(clientssl);
3633 SSL_shutdown(serverssl);
3634 SSL_free(serverssl);
3635 SSL_free(clientssl);
3636 serverssl = clientssl = NULL;
3637
71cff963 3638# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
ca0413ae 3639 /* Check we can resume a session with a different SHA-256 ciphersuite */
f865b081
MC
3640 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3641 "TLS_CHACHA20_POLY1305_SHA256"))
ca0413ae
MC
3642 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3643 NULL, NULL))
3644 || !TEST_true(SSL_set_session(clientssl, clntsess))
3645 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3646 SSL_ERROR_NONE))
3647 || !TEST_true(SSL_session_reused(clientssl)))
3648 goto end;
3649
3650 SSL_SESSION_free(clntsess);
3651 clntsess = SSL_get1_session(clientssl);
3652 SSL_shutdown(clientssl);
3653 SSL_shutdown(serverssl);
3654 SSL_free(serverssl);
3655 SSL_free(clientssl);
3656 serverssl = clientssl = NULL;
71cff963 3657# endif
ca0413ae
MC
3658
3659 /*
3660 * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
0de6d66d 3661 * succeeds but does not resume.
ca0413ae 3662 */
f865b081 3663 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
ca0413ae
MC
3664 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3665 NULL, NULL))
3666 || !TEST_true(SSL_set_session(clientssl, clntsess))
0de6d66d 3667 || !TEST_true(create_ssl_connection(serverssl, clientssl,
ca0413ae 3668 SSL_ERROR_SSL))
0de6d66d 3669 || !TEST_false(SSL_session_reused(clientssl)))
ca0413ae
MC
3670 goto end;
3671
3672 SSL_SESSION_free(clntsess);
3673 clntsess = NULL;
3674 SSL_shutdown(clientssl);
3675 SSL_shutdown(serverssl);
3676 SSL_free(serverssl);
3677 SSL_free(clientssl);
3678 serverssl = clientssl = NULL;
3679
3680 /* Create a session based on SHA384 */
f865b081 3681 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
ca0413ae
MC
3682 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3683 &clientssl, NULL, NULL))
3684 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3685 SSL_ERROR_NONE)))
3686 goto end;
3687
3688 clntsess = SSL_get1_session(clientssl);
3689 SSL_shutdown(clientssl);
3690 SSL_shutdown(serverssl);
3691 SSL_free(serverssl);
3692 SSL_free(clientssl);
3693 serverssl = clientssl = NULL;
3694
f865b081
MC
3695 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3696 "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
3697 || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
3698 "TLS_AES_256_GCM_SHA384"))
ca0413ae
MC
3699 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3700 NULL, NULL))
3701 || !TEST_true(SSL_set_session(clientssl, clntsess))
3b0e88d3
MC
3702 /*
3703 * We use SSL_ERROR_WANT_READ below so that we can pause the
3704 * connection after the initial ClientHello has been sent to
3705 * enable us to make some session changes.
3706 */
ca0413ae
MC
3707 || !TEST_false(create_ssl_connection(serverssl, clientssl,
3708 SSL_ERROR_WANT_READ)))
3709 goto end;
3710
3711 /* Trick the client into thinking this session is for a different digest */
3712 clntsess->cipher = aes_128_gcm_sha256;
3713 clntsess->cipher_id = clntsess->cipher->id;
3714
3715 /*
3b0e88d3
MC
3716 * Continue the previously started connection. Server has selected a SHA-384
3717 * ciphersuite, but client thinks the session is for SHA-256, so it should
3718 * bail out.
ca0413ae
MC
3719 */
3720 if (!TEST_false(create_ssl_connection(serverssl, clientssl,
3721 SSL_ERROR_SSL))
3722 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
3723 SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
3724 goto end;
3725
3726 testresult = 1;
3727
3728 end:
3729 SSL_SESSION_free(clntsess);
3730 SSL_free(serverssl);
3731 SSL_free(clientssl);
3732 SSL_CTX_free(sctx);
3733 SSL_CTX_free(cctx);
3734
3735 return testresult;
3736}
3737
0d8da779
MC
3738/*
3739 * Test TLSv1.3 PSKs
3740 * Test 0 = Test new style callbacks
3741 * Test 1 = Test both new and old style callbacks
3742 * Test 2 = Test old style callbacks
3743 * Test 3 = Test old style callbacks with no certificate
3744 */
532f9578 3745static int test_tls13_psk(int idx)
ca8c71ba
MC
3746{
3747 SSL_CTX *sctx = NULL, *cctx = NULL;
3748 SSL *serverssl = NULL, *clientssl = NULL;
3749 const SSL_CIPHER *cipher = NULL;
3750 const unsigned char key[] = {
3751 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
3752 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
de2f409e
MC
3753 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
3754 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
ca8c71ba
MC
3755 };
3756 int testresult = 0;
3757
7d7f6834 3758 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 3759 TLS1_VERSION, 0,
0d8da779
MC
3760 &sctx, &cctx, idx == 3 ? NULL : cert,
3761 idx == 3 ? NULL : privkey)))
ca8c71ba
MC
3762 goto end;
3763
0d8da779
MC
3764 if (idx != 3) {
3765 /*
3766 * We use a ciphersuite with SHA256 to ease testing old style PSK
3767 * callbacks which will always default to SHA256. This should not be
3768 * necessary if we have no cert/priv key. In that case the server should
3769 * prefer SHA256 automatically.
3770 */
3771 if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
3772 "TLS_AES_128_GCM_SHA256")))
3773 goto end;
3774 }
532f9578
MC
3775
3776 /*
3777 * Test 0: New style callbacks only
3778 * Test 1: New and old style callbacks (only the new ones should be used)
3779 * Test 2: Old style callbacks only
3780 */
3781 if (idx == 0 || idx == 1) {
3782 SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
3783 SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
3784 }
c2b290c3 3785#ifndef OPENSSL_NO_PSK
0d8da779 3786 if (idx >= 1) {
532f9578
MC
3787 SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
3788 SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
3789 }
c2b290c3 3790#endif
ca8c71ba 3791 srvid = pskid;
02a3ed5a
MC
3792 use_session_cb_cnt = 0;
3793 find_session_cb_cnt = 0;
532f9578
MC
3794 psk_client_cb_cnt = 0;
3795 psk_server_cb_cnt = 0;
ca8c71ba 3796
0d8da779
MC
3797 if (idx != 3) {
3798 /*
3799 * Check we can create a connection if callback decides not to send a
3800 * PSK
3801 */
3802 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3803 NULL, NULL))
3804 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3805 SSL_ERROR_NONE))
3806 || !TEST_false(SSL_session_reused(clientssl))
3807 || !TEST_false(SSL_session_reused(serverssl)))
532f9578 3808 goto end;
532f9578 3809
0d8da779
MC
3810 if (idx == 0 || idx == 1) {
3811 if (!TEST_true(use_session_cb_cnt == 1)
3812 || !TEST_true(find_session_cb_cnt == 0)
3813 /*
3814 * If no old style callback then below should be 0
3815 * otherwise 1
3816 */
3817 || !TEST_true(psk_client_cb_cnt == idx)
3818 || !TEST_true(psk_server_cb_cnt == 0))
3819 goto end;
3820 } else {
3821 if (!TEST_true(use_session_cb_cnt == 0)
3822 || !TEST_true(find_session_cb_cnt == 0)
3823 || !TEST_true(psk_client_cb_cnt == 1)
3824 || !TEST_true(psk_server_cb_cnt == 0))
3825 goto end;
3826 }
3827
3828 shutdown_ssl_connection(serverssl, clientssl);
3829 serverssl = clientssl = NULL;
3830 use_session_cb_cnt = psk_client_cb_cnt = 0;
3831 }
ca8c71ba
MC
3832
3833 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3834 NULL, NULL)))
3835 goto end;
3836
3837 /* Create the PSK */
532f9578 3838 cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
57dee9bb
MC
3839 clientpsk = SSL_SESSION_new();
3840 if (!TEST_ptr(clientpsk)
ca8c71ba 3841 || !TEST_ptr(cipher)
57dee9bb
MC
3842 || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
3843 sizeof(key)))
3844 || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
3845 || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
3846 TLS1_3_VERSION))
3847 || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
ca8c71ba 3848 goto end;
57dee9bb 3849 serverpsk = clientpsk;
ca8c71ba
MC
3850
3851 /* Check we can create a connection and the PSK is used */
3852 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3853 || !TEST_true(SSL_session_reused(clientssl))
532f9578 3854 || !TEST_true(SSL_session_reused(serverssl)))
ca8c71ba
MC
3855 goto end;
3856
532f9578
MC
3857 if (idx == 0 || idx == 1) {
3858 if (!TEST_true(use_session_cb_cnt == 1)
3859 || !TEST_true(find_session_cb_cnt == 1)
3860 || !TEST_true(psk_client_cb_cnt == 0)
3861 || !TEST_true(psk_server_cb_cnt == 0))
3862 goto end;
3863 } else {
3864 if (!TEST_true(use_session_cb_cnt == 0)
3865 || !TEST_true(find_session_cb_cnt == 0)
3866 || !TEST_true(psk_client_cb_cnt == 1)
3867 || !TEST_true(psk_server_cb_cnt == 1))
3868 goto end;
3869 }
3870
ca8c71ba
MC
3871 shutdown_ssl_connection(serverssl, clientssl);
3872 serverssl = clientssl = NULL;
3873 use_session_cb_cnt = find_session_cb_cnt = 0;
532f9578 3874 psk_client_cb_cnt = psk_server_cb_cnt = 0;
ca8c71ba
MC
3875
3876 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3877 NULL, NULL)))
3878 goto end;
3879
3880 /* Force an HRR */
3881 if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
3882 goto end;
3883
3884 /*
3885 * Check we can create a connection, the PSK is used and the callbacks are
3886 * called twice.
3887 */
3888 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3889 || !TEST_true(SSL_session_reused(clientssl))
532f9578 3890 || !TEST_true(SSL_session_reused(serverssl)))
ca8c71ba
MC
3891 goto end;
3892
532f9578
MC
3893 if (idx == 0 || idx == 1) {
3894 if (!TEST_true(use_session_cb_cnt == 2)
3895 || !TEST_true(find_session_cb_cnt == 2)
3896 || !TEST_true(psk_client_cb_cnt == 0)
3897 || !TEST_true(psk_server_cb_cnt == 0))
3898 goto end;
3899 } else {
3900 if (!TEST_true(use_session_cb_cnt == 0)
3901 || !TEST_true(find_session_cb_cnt == 0)
3902 || !TEST_true(psk_client_cb_cnt == 2)
3903 || !TEST_true(psk_server_cb_cnt == 2))
3904 goto end;
3905 }
3906
ca8c71ba
MC
3907 shutdown_ssl_connection(serverssl, clientssl);
3908 serverssl = clientssl = NULL;
3909 use_session_cb_cnt = find_session_cb_cnt = 0;
532f9578 3910 psk_client_cb_cnt = psk_server_cb_cnt = 0;
ca8c71ba 3911
0d8da779
MC
3912 if (idx != 3) {
3913 /*
3914 * Check that if the server rejects the PSK we can still connect, but with
3915 * a full handshake
3916 */
3917 srvid = "Dummy Identity";
3918 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
3919 NULL, NULL))
3920 || !TEST_true(create_ssl_connection(serverssl, clientssl,
3921 SSL_ERROR_NONE))
3922 || !TEST_false(SSL_session_reused(clientssl))
3923 || !TEST_false(SSL_session_reused(serverssl)))
532f9578 3924 goto end;
532f9578 3925
0d8da779
MC
3926 if (idx == 0 || idx == 1) {
3927 if (!TEST_true(use_session_cb_cnt == 1)
3928 || !TEST_true(find_session_cb_cnt == 1)
3929 || !TEST_true(psk_client_cb_cnt == 0)
3930 /*
3931 * If no old style callback then below should be 0
3932 * otherwise 1
3933 */
3934 || !TEST_true(psk_server_cb_cnt == idx))
3935 goto end;
3936 } else {
3937 if (!TEST_true(use_session_cb_cnt == 0)
3938 || !TEST_true(find_session_cb_cnt == 0)
3939 || !TEST_true(psk_client_cb_cnt == 1)
3940 || !TEST_true(psk_server_cb_cnt == 1))
3941 goto end;
3942 }
3943
3944 shutdown_ssl_connection(serverssl, clientssl);
3945 serverssl = clientssl = NULL;
3946 }
ca8c71ba
MC
3947 testresult = 1;
3948
3949 end:
57dee9bb
MC
3950 SSL_SESSION_free(clientpsk);
3951 SSL_SESSION_free(serverpsk);
3952 clientpsk = serverpsk = NULL;
ca8c71ba
MC
3953 SSL_free(serverssl);
3954 SSL_free(clientssl);
3955 SSL_CTX_free(sctx);
3956 SSL_CTX_free(cctx);
3957 return testresult;
3958}
3959
c7b8ff25
MC
3960static unsigned char cookie_magic_value[] = "cookie magic";
3961
3962static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
3963 unsigned int *cookie_len)
3964{
3965 /*
3966 * Not suitable as a real cookie generation function but good enough for
3967 * testing!
3968 */
97ea1e7f
MC
3969 memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
3970 *cookie_len = sizeof(cookie_magic_value) - 1;
c7b8ff25
MC
3971
3972 return 1;
3973}
3974
3975static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
3976 unsigned int cookie_len)
3977{
97ea1e7f 3978 if (cookie_len == sizeof(cookie_magic_value) - 1
c7b8ff25
MC
3979 && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
3980 return 1;
3981
3982 return 0;
3983}
3984
3fa2812f
BS
3985static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
3986 size_t *cookie_len)
3987{
3988 unsigned int temp;
3989 int res = generate_cookie_callback(ssl, cookie, &temp);
3990 *cookie_len = temp;
3991 return res;
3992}
3993
3994static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
3995 size_t cookie_len)
3996{
3997 return verify_cookie_callback(ssl, cookie, cookie_len);
3998}
3999
c7b8ff25
MC
4000static int test_stateless(void)
4001{
4002 SSL_CTX *sctx = NULL, *cctx = NULL;
4003 SSL *serverssl = NULL, *clientssl = NULL;
4004 int testresult = 0;
4005
7d7f6834 4006 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 4007 TLS1_VERSION, 0,
7d7f6834 4008 &sctx, &cctx, cert, privkey)))
c7b8ff25
MC
4009 goto end;
4010
e440f513
MC
4011 /* The arrival of CCS messages can confuse the test */
4012 SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
4013
4014 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4015 NULL, NULL))
4016 /* Send the first ClientHello */
4017 || !TEST_false(create_ssl_connection(serverssl, clientssl,
4018 SSL_ERROR_WANT_READ))
4019 /*
4020 * This should fail with a -1 return because we have no callbacks
4021 * set up
4022 */
4023 || !TEST_int_eq(SSL_stateless(serverssl), -1))
4024 goto end;
4025
4026 /* Fatal error so abandon the connection from this client */
4027 SSL_free(clientssl);
4028 clientssl = NULL;
4029
c7b8ff25 4030 /* Set up the cookie generation and verification callbacks */
3fa2812f
BS
4031 SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
4032 SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
c7b8ff25 4033
e440f513
MC
4034 /*
4035 * Create a new connection from the client (we can reuse the server SSL
4036 * object).
4037 */
c7b8ff25
MC
4038 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4039 NULL, NULL))
4040 /* Send the first ClientHello */
4041 || !TEST_false(create_ssl_connection(serverssl, clientssl,
4042 SSL_ERROR_WANT_READ))
4043 /* This should fail because there is no cookie */
e440f513 4044 || !TEST_int_eq(SSL_stateless(serverssl), 0))
c7b8ff25
MC
4045 goto end;
4046
4047 /* Abandon the connection from this client */
4048 SSL_free(clientssl);
4049 clientssl = NULL;
4050
4051 /*
4052 * Now create a connection from a new client but with the same server SSL
4053 * object
4054 */
4055 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4056 NULL, NULL))
4057 /* Send the first ClientHello */
4058 || !TEST_false(create_ssl_connection(serverssl, clientssl,
4059 SSL_ERROR_WANT_READ))
4060 /* This should fail because there is no cookie */
e440f513 4061 || !TEST_int_eq(SSL_stateless(serverssl), 0)
c7b8ff25
MC
4062 /* Send the second ClientHello */
4063 || !TEST_false(create_ssl_connection(serverssl, clientssl,
4064 SSL_ERROR_WANT_READ))
4065 /* This should succeed because a cookie is now present */
e440f513 4066 || !TEST_int_eq(SSL_stateless(serverssl), 1)
c7b8ff25
MC
4067 /* Complete the connection */
4068 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4069 SSL_ERROR_NONE)))
4070 goto end;
4071
4072 shutdown_ssl_connection(serverssl, clientssl);
4073 serverssl = clientssl = NULL;
4074 testresult = 1;
4075
4076 end:
4077 SSL_free(serverssl);
4078 SSL_free(clientssl);
4079 SSL_CTX_free(sctx);
4080 SSL_CTX_free(cctx);
4081 return testresult;
4082
4083}
ca0413ae 4084#endif /* OPENSSL_NO_TLS1_3 */
5f982038 4085
a37008d9
MC
4086static int clntaddoldcb = 0;
4087static int clntparseoldcb = 0;
4088static int srvaddoldcb = 0;
4089static int srvparseoldcb = 0;
4090static int clntaddnewcb = 0;
4091static int clntparsenewcb = 0;
4092static int srvaddnewcb = 0;
4093static int srvparsenewcb = 0;
bb01ef3f 4094static int snicb = 0;
a37008d9
MC
4095
4096#define TEST_EXT_TYPE1 0xff00
4097
4098static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
4099 size_t *outlen, int *al, void *add_arg)
4100{
4101 int *server = (int *)add_arg;
4102 unsigned char *data;
4103
4104 if (SSL_is_server(s))
4105 srvaddoldcb++;
4106 else
4107 clntaddoldcb++;
4108
710756a9
RS
4109 if (*server != SSL_is_server(s)
4110 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
a37008d9
MC
4111 return -1;
4112
4113 *data = 1;
4114 *out = data;
4115 *outlen = sizeof(char);
a37008d9
MC
4116 return 1;
4117}
4118
4119static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
4120 void *add_arg)
4121{
4122 OPENSSL_free((unsigned char *)out);
4123}
4124
4125static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
4126 size_t inlen, int *al, void *parse_arg)
4127{
4128 int *server = (int *)parse_arg;
4129
4130 if (SSL_is_server(s))
4131 srvparseoldcb++;
4132 else
4133 clntparseoldcb++;
4134
710756a9
RS
4135 if (*server != SSL_is_server(s)
4136 || inlen != sizeof(char)
4137 || *in != 1)
a37008d9
MC
4138 return -1;
4139
4140 return 1;
4141}
4142
4143static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
4144 const unsigned char **out, size_t *outlen, X509 *x,
4145 size_t chainidx, int *al, void *add_arg)
4146{
4147 int *server = (int *)add_arg;
4148 unsigned char *data;
4149
4150 if (SSL_is_server(s))
4151 srvaddnewcb++;
4152 else
4153 clntaddnewcb++;
4154
710756a9
RS
4155 if (*server != SSL_is_server(s)
4156 || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
a37008d9
MC
4157 return -1;
4158
4159 *data = 1;
4160 *out = data;
710756a9 4161 *outlen = sizeof(*data);
a37008d9
MC
4162 return 1;
4163}
4164
4165static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
4166 const unsigned char *out, void *add_arg)
4167{
4168 OPENSSL_free((unsigned char *)out);
4169}
4170
4171static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
4172 const unsigned char *in, size_t inlen, X509 *x,
4173 size_t chainidx, int *al, void *parse_arg)
4174{
4175 int *server = (int *)parse_arg;
4176
4177 if (SSL_is_server(s))
4178 srvparsenewcb++;
4179 else
4180 clntparsenewcb++;
4181
710756a9
RS
4182 if (*server != SSL_is_server(s)
4183 || inlen != sizeof(char) || *in != 1)
a37008d9
MC
4184 return -1;
4185
4186 return 1;
4187}
bb01ef3f
MC
4188
4189static int sni_cb(SSL *s, int *al, void *arg)
4190{
4191 SSL_CTX *ctx = (SSL_CTX *)arg;
4192
4193 if (SSL_set_SSL_CTX(s, ctx) == NULL) {
4194 *al = SSL_AD_INTERNAL_ERROR;
4195 return SSL_TLSEXT_ERR_ALERT_FATAL;
4196 }
4197 snicb++;
4198 return SSL_TLSEXT_ERR_OK;
4199}
4200
a37008d9
MC
4201/*
4202 * Custom call back tests.
4203 * Test 0: Old style callbacks in TLSv1.2
4204 * Test 1: New style callbacks in TLSv1.2
bb01ef3f
MC
4205 * Test 2: New style callbacks in TLSv1.2 with SNI
4206 * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
4207 * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
a37008d9 4208 */
710756a9
RS
4209static int test_custom_exts(int tst)
4210{
bb01ef3f 4211 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
a37008d9
MC
4212 SSL *clientssl = NULL, *serverssl = NULL;
4213 int testresult = 0;
4214 static int server = 1;
4215 static int client = 0;
4216 SSL_SESSION *sess = NULL;
4217 unsigned int context;
4218
c423ecaa
MC
4219#if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)
4220 /* Skip tests for TLSv1.2 and below in this case */
4221 if (tst < 3)
4222 return 1;
4223#endif
4224
a37008d9
MC
4225 /* Reset callback counters */
4226 clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
4227 clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
bb01ef3f 4228 snicb = 0;
a37008d9 4229
7d7f6834 4230 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 4231 TLS1_VERSION, 0,
7d7f6834 4232 &sctx, &cctx, cert, privkey)))
710756a9 4233 goto end;
a37008d9 4234
bb01ef3f 4235 if (tst == 2
7d7f6834 4236 && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL,
5c587fb6 4237 TLS1_VERSION, 0,
7d7f6834 4238 &sctx2, NULL, cert, privkey)))
bb01ef3f
MC
4239 goto end;
4240
4241
4242 if (tst < 3) {
a37008d9
MC
4243 SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
4244 SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
bb01ef3f
MC
4245 if (sctx2 != NULL)
4246 SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
a37008d9
MC
4247 }
4248
bb01ef3f 4249 if (tst == 4) {
710756a9
RS
4250 context = SSL_EXT_CLIENT_HELLO
4251 | SSL_EXT_TLS1_2_SERVER_HELLO
a37008d9
MC
4252 | SSL_EXT_TLS1_3_SERVER_HELLO
4253 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
4254 | SSL_EXT_TLS1_3_CERTIFICATE
4255 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
4256 } else {
710756a9
RS
4257 context = SSL_EXT_CLIENT_HELLO
4258 | SSL_EXT_TLS1_2_SERVER_HELLO
a37008d9
MC
4259 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
4260 }
4261
4262 /* Create a client side custom extension */
4263 if (tst == 0) {
710756a9
RS
4264 if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
4265 old_add_cb, old_free_cb,
4266 &client, old_parse_cb,
4267 &client)))
4268 goto end;
a37008d9 4269 } else {
710756a9
RS
4270 if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
4271 new_add_cb, new_free_cb,
4272 &client, new_parse_cb, &client)))
4273 goto end;
a37008d9
MC
4274 }
4275
4276 /* Should not be able to add duplicates */
710756a9
RS
4277 if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
4278 old_add_cb, old_free_cb,
4279 &client, old_parse_cb,
4280 &client))
4281 || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
4282 context, new_add_cb,
4283 new_free_cb, &client,
4284 new_parse_cb, &client)))
4285 goto end;
a37008d9
MC
4286
4287 /* Create a server side custom extension */
4288 if (tst == 0) {
710756a9
RS
4289 if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
4290 old_add_cb, old_free_cb,
4291 &server, old_parse_cb,
4292 &server)))
4293 goto end;
a37008d9 4294 } else {
710756a9
RS
4295 if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
4296 new_add_cb, new_free_cb,
4297 &server, new_parse_cb, &server)))
4298 goto end;
bb01ef3f
MC
4299 if (sctx2 != NULL
4300 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
4301 context, new_add_cb,
4302 new_free_cb, &server,
4303 new_parse_cb, &server)))
4304 goto end;
a37008d9
MC
4305 }
4306
4307 /* Should not be able to add duplicates */
710756a9
RS
4308 if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
4309 old_add_cb, old_free_cb,
4310 &server, old_parse_cb,
4311 &server))
4312 || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
4313 context, new_add_cb,
4314 new_free_cb, &server,
4315 new_parse_cb, &server)))
a37008d9 4316 goto end;
a37008d9 4317
bb01ef3f
MC
4318 if (tst == 2) {
4319 /* Set up SNI */
4320 if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
4321 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
4322 goto end;
4323 }
4324
710756a9
RS
4325 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4326 &clientssl, NULL, NULL))
4327 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4328 SSL_ERROR_NONE)))
a37008d9 4329 goto end;
a37008d9
MC
4330
4331 if (tst == 0) {
710756a9
RS
4332 if (clntaddoldcb != 1
4333 || clntparseoldcb != 1
4334 || srvaddoldcb != 1
4335 || srvparseoldcb != 1)
a37008d9 4336 goto end;
bb01ef3f 4337 } else if (tst == 1 || tst == 2 || tst == 3) {
710756a9
RS
4338 if (clntaddnewcb != 1
4339 || clntparsenewcb != 1
4340 || srvaddnewcb != 1
bb01ef3f
MC
4341 || srvparsenewcb != 1
4342 || (tst != 2 && snicb != 0)
4343 || (tst == 2 && snicb != 1))
a37008d9 4344 goto end;
a37008d9 4345 } else {
36ff232c 4346 /* In this case there 2 NewSessionTicket messages created */
710756a9 4347 if (clntaddnewcb != 1
36ff232c
MC
4348 || clntparsenewcb != 5
4349 || srvaddnewcb != 5
710756a9 4350 || srvparsenewcb != 1)
a37008d9 4351 goto end;
a37008d9
MC
4352 }
4353
4354 sess = SSL_get1_session(clientssl);
a37008d9
MC
4355 SSL_shutdown(clientssl);
4356 SSL_shutdown(serverssl);
a37008d9
MC
4357 SSL_free(serverssl);
4358 SSL_free(clientssl);
4359 serverssl = clientssl = NULL;
4360
bb01ef3f
MC
4361 if (tst == 3) {
4362 /* We don't bother with the resumption aspects for this test */
4363 testresult = 1;
4364 goto end;
4365 }
4366
710756a9
RS
4367 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4368 NULL, NULL))
4369 || !TEST_true(SSL_set_session(clientssl, sess))
4370 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4371 SSL_ERROR_NONE)))
a37008d9 4372 goto end;
a37008d9
MC
4373
4374 /*
4375 * For a resumed session we expect to add the ClientHello extension. For the
4376 * old style callbacks we ignore it on the server side because they set
4377 * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
4378 * them.
4379 */
4380 if (tst == 0) {
710756a9
RS
4381 if (clntaddoldcb != 2
4382 || clntparseoldcb != 1
4383 || srvaddoldcb != 1
4384 || srvparseoldcb != 1)
a37008d9 4385 goto end;
bb01ef3f 4386 } else if (tst == 1 || tst == 2 || tst == 3) {
710756a9
RS
4387 if (clntaddnewcb != 2
4388 || clntparsenewcb != 2
4389 || srvaddnewcb != 2
4390 || srvparsenewcb != 2)
a37008d9 4391 goto end;
a37008d9 4392 } else {
36ff232c
MC
4393 /*
4394 * No Certificate message extensions in the resumption handshake,
4395 * 2 NewSessionTickets in the initial handshake, 1 in the resumption
4396 */
710756a9 4397 if (clntaddnewcb != 2
36ff232c
MC
4398 || clntparsenewcb != 8
4399 || srvaddnewcb != 8
710756a9 4400 || srvparsenewcb != 2)
a37008d9 4401 goto end;
a37008d9
MC
4402 }
4403
4404 testresult = 1;
4405
4406end:
4407 SSL_SESSION_free(sess);
4408 SSL_free(serverssl);
4409 SSL_free(clientssl);
bb01ef3f 4410 SSL_CTX_free(sctx2);
a37008d9
MC
4411 SSL_CTX_free(sctx);
4412 SSL_CTX_free(cctx);
a37008d9
MC
4413 return testresult;
4414}
4415
16afd71c
MC
4416/*
4417 * Test loading of serverinfo data in various formats. test_sslmessages actually
4418 * tests to make sure the extensions appear in the handshake
4419 */
4420static int test_serverinfo(int tst)
4421{
4422 unsigned int version;
4423 unsigned char *sibuf;
4424 size_t sibuflen;
4425 int ret, expected, testresult = 0;
4426 SSL_CTX *ctx;
4427
4428 ctx = SSL_CTX_new(TLS_method());
4429 if (!TEST_ptr(ctx))
4430 goto end;
4431
4432 if ((tst & 0x01) == 0x01)
4433 version = SSL_SERVERINFOV2;
4434 else
4435 version = SSL_SERVERINFOV1;
4436
4437 if ((tst & 0x02) == 0x02) {
4438 sibuf = serverinfov2;
4439 sibuflen = sizeof(serverinfov2);
4440 expected = (version == SSL_SERVERINFOV2);
4441 } else {
4442 sibuf = serverinfov1;
4443 sibuflen = sizeof(serverinfov1);
4444 expected = (version == SSL_SERVERINFOV1);
4445 }
4446
4447 if ((tst & 0x04) == 0x04) {
4448 ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
4449 } else {
4450 ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
4451
4452 /*
4453 * The version variable is irrelevant in this case - it's what is in the
4454 * buffer that matters
4455 */
4456 if ((tst & 0x02) == 0x02)
4457 expected = 0;
4458 else
4459 expected = 1;
4460 }
4461
4462 if (!TEST_true(ret == expected))
4463 goto end;
4464
4465 testresult = 1;
4466
4467 end:
4468 SSL_CTX_free(ctx);
4469
4470 return testresult;
4471}
4472
2197d1df
MC
4473/*
4474 * Test that SSL_export_keying_material() produces expected results. There are
4475 * no test vectors so all we do is test that both sides of the communication
4476 * produce the same results for different protocol versions.
4477 */
0fb2815b
MC
4478#define SMALL_LABEL_LEN 10
4479#define LONG_LABEL_LEN 249
2197d1df
MC
4480static int test_export_key_mat(int tst)
4481{
a599574b 4482 int testresult = 0;
2197d1df
MC
4483 SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
4484 SSL *clientssl = NULL, *serverssl = NULL;
0fb2815b 4485 const char label[LONG_LABEL_LEN + 1] = "test label";
2197d1df
MC
4486 const unsigned char context[] = "context";
4487 const unsigned char *emptycontext = NULL;
4488 unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
4489 unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
0fb2815b 4490 size_t labellen;
a599574b
MC
4491 const int protocols[] = {
4492 TLS1_VERSION,
4493 TLS1_1_VERSION,
4494 TLS1_2_VERSION,
0fb2815b
MC
4495 TLS1_3_VERSION,
4496 TLS1_3_VERSION,
a599574b
MC
4497 TLS1_3_VERSION
4498 };
2197d1df
MC
4499
4500#ifdef OPENSSL_NO_TLS1
4501 if (tst == 0)
4502 return 1;
4503#endif
4504#ifdef OPENSSL_NO_TLS1_1
4505 if (tst == 1)
4506 return 1;
4507#endif
4508#ifdef OPENSSL_NO_TLS1_2
4509 if (tst == 2)
4510 return 1;
4511#endif
4512#ifdef OPENSSL_NO_TLS1_3
0fb2815b 4513 if (tst >= 3)
2197d1df
MC
4514 return 1;
4515#endif
7d7f6834 4516 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 4517 TLS1_VERSION, 0,
7d7f6834 4518 &sctx, &cctx, cert, privkey)))
2197d1df
MC
4519 goto end;
4520
a599574b
MC
4521 OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
4522 SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
4523 SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
2197d1df
MC
4524
4525 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
4526 NULL))
4527 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4528 SSL_ERROR_NONE)))
4529 goto end;
4530
0fb2815b
MC
4531 if (tst == 5) {
4532 /*
4533 * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
4534 * go over that.
4535 */
4536 if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
4537 sizeof(ckeymat1), label,
4538 LONG_LABEL_LEN + 1, context,
4539 sizeof(context) - 1, 1), 0))
4540 goto end;
4541
4542 testresult = 1;
4543 goto end;
4544 } else if (tst == 4) {
4545 labellen = LONG_LABEL_LEN;
4546 } else {
4547 labellen = SMALL_LABEL_LEN;
4548 }
4549
2197d1df
MC
4550 if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
4551 sizeof(ckeymat1), label,
0fb2815b 4552 labellen, context,
2197d1df
MC
4553 sizeof(context) - 1, 1), 1)
4554 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
4555 sizeof(ckeymat2), label,
0fb2815b 4556 labellen,
2197d1df
MC
4557 emptycontext,
4558 0, 1), 1)
4559 || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
4560 sizeof(ckeymat3), label,
0fb2815b 4561 labellen,
2197d1df
MC
4562 NULL, 0, 0), 1)
4563 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
4564 sizeof(skeymat1), label,
0fb2815b 4565 labellen,
2197d1df
MC
4566 context,
4567 sizeof(context) -1, 1),
4568 1)
4569 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
4570 sizeof(skeymat2), label,
0fb2815b 4571 labellen,
2197d1df
MC
4572 emptycontext,
4573 0, 1), 1)
4574 || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
4575 sizeof(skeymat3), label,
0fb2815b 4576 labellen,
2197d1df
MC
4577 NULL, 0, 0), 1)
4578 /*
4579 * Check that both sides created the same key material with the
4580 * same context.
4581 */
4582 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
4583 sizeof(skeymat1))
4584 /*
4585 * Check that both sides created the same key material with an
4586 * empty context.
4587 */
4588 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
4589 sizeof(skeymat2))
4590 /*
4591 * Check that both sides created the same key material without a
4592 * context.
4593 */
4594 || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
4595 sizeof(skeymat3))
4596 /* Different contexts should produce different results */
4597 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
4598 sizeof(ckeymat2)))
4599 goto end;
4600
4601 /*
4602 * Check that an empty context and no context produce different results in
4603 * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
4604 */
0fb2815b 4605 if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
2197d1df 4606 sizeof(ckeymat3)))
0fb2815b
MC
4607 || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
4608 sizeof(ckeymat3))))
2197d1df
MC
4609 goto end;
4610
4611 testresult = 1;
4612
4613 end:
4614 SSL_free(serverssl);
4615 SSL_free(clientssl);
4616 SSL_CTX_free(sctx2);
4617 SSL_CTX_free(sctx);
4618 SSL_CTX_free(cctx);
4619
4620 return testresult;
4621}
4622
b38ede80
TT
4623#ifndef OPENSSL_NO_TLS1_3
4624/*
4625 * Test that SSL_export_keying_material_early() produces expected
4626 * results. There are no test vectors so all we do is test that both
4627 * sides of the communication produce the same results for different
4628 * protocol versions.
4629 */
4630static int test_export_key_mat_early(int idx)
4631{
4632 static const char label[] = "test label";
4633 static const unsigned char context[] = "context";
4634 int testresult = 0;
4635 SSL_CTX *cctx = NULL, *sctx = NULL;
4636 SSL *clientssl = NULL, *serverssl = NULL;
4637 SSL_SESSION *sess = NULL;
4638 const unsigned char *emptycontext = NULL;
4639 unsigned char ckeymat1[80], ckeymat2[80];
4640 unsigned char skeymat1[80], skeymat2[80];
4641 unsigned char buf[1];
4642 size_t readbytes, written;
4643
4644 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
4645 &sess, idx)))
4646 goto end;
4647
4648 /* Here writing 0 length early data is enough. */
4649 if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
4650 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4651 &readbytes),
4652 SSL_READ_EARLY_DATA_ERROR)
4653 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4654 SSL_EARLY_DATA_ACCEPTED))
4655 goto end;
4656
4657 if (!TEST_int_eq(SSL_export_keying_material_early(
4658 clientssl, ckeymat1, sizeof(ckeymat1), label,
4659 sizeof(label) - 1, context, sizeof(context) - 1), 1)
4660 || !TEST_int_eq(SSL_export_keying_material_early(
4661 clientssl, ckeymat2, sizeof(ckeymat2), label,
4662 sizeof(label) - 1, emptycontext, 0), 1)
4663 || !TEST_int_eq(SSL_export_keying_material_early(
4664 serverssl, skeymat1, sizeof(skeymat1), label,
4665 sizeof(label) - 1, context, sizeof(context) - 1), 1)
4666 || !TEST_int_eq(SSL_export_keying_material_early(
4667 serverssl, skeymat2, sizeof(skeymat2), label,
4668 sizeof(label) - 1, emptycontext, 0), 1)
4669 /*
4670 * Check that both sides created the same key material with the
4671 * same context.
4672 */
4673 || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
4674 sizeof(skeymat1))
4675 /*
4676 * Check that both sides created the same key material with an
4677 * empty context.
4678 */
4679 || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
4680 sizeof(skeymat2))
4681 /* Different contexts should produce different results */
4682 || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
4683 sizeof(ckeymat2)))
4684 goto end;
4685
4686 testresult = 1;
4687
4688 end:
c20e3b28 4689 SSL_SESSION_free(sess);
b38ede80
TT
4690 SSL_SESSION_free(clientpsk);
4691 SSL_SESSION_free(serverpsk);
34ff74eb 4692 clientpsk = serverpsk = NULL;
b38ede80
TT
4693 SSL_free(serverssl);
4694 SSL_free(clientssl);
4695 SSL_CTX_free(sctx);
4696 SSL_CTX_free(cctx);
4697
4698 return testresult;
4699}
3409a5ff
MC
4700
4701#define NUM_KEY_UPDATE_MESSAGES 40
4702/*
4703 * Test KeyUpdate.
4704 */
4705static int test_key_update(void)
4706{
4707 SSL_CTX *cctx = NULL, *sctx = NULL;
4708 SSL *clientssl = NULL, *serverssl = NULL;
4709 int testresult = 0, i, j;
4710 char buf[20];
4711 static char *mess = "A test message";
4712
4713 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4714 TLS_client_method(),
4715 TLS1_3_VERSION,
4716 0,
4717 &sctx, &cctx, cert, privkey))
4718 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4719 NULL, NULL))
4720 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4721 SSL_ERROR_NONE)))
4722 goto end;
4723
4724 for (j = 0; j < 2; j++) {
4725 /* Send lots of KeyUpdate messages */
4726 for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
4727 if (!TEST_true(SSL_key_update(clientssl,
4728 (j == 0)
4729 ? SSL_KEY_UPDATE_NOT_REQUESTED
4730 : SSL_KEY_UPDATE_REQUESTED))
4731 || !TEST_true(SSL_do_handshake(clientssl)))
4732 goto end;
4733 }
4734
4735 /* Check that sending and receiving app data is ok */
4736 if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
4737 || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
4738 strlen(mess)))
4739 goto end;
a77b4dba
MC
4740
4741 if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
4742 || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
4743 strlen(mess)))
4744 goto end;
3409a5ff
MC
4745 }
4746
4747 testresult = 1;
4748
4749 end:
4750 SSL_free(serverssl);
4751 SSL_free(clientssl);
4752 SSL_CTX_free(sctx);
4753 SSL_CTX_free(cctx);
4754
4755 return testresult;
4756}
a77b4dba
MC
4757
4758/*
4759 * Test we can handle a KeyUpdate (update requested) message while write data
4760 * is pending.
4761 * Test 0: Client sends KeyUpdate while Server is writing
4762 * Test 1: Server sends KeyUpdate while Client is writing
4763 */
4764static int test_key_update_in_write(int tst)
4765{
4766 SSL_CTX *cctx = NULL, *sctx = NULL;
4767 SSL *clientssl = NULL, *serverssl = NULL;
4768 int testresult = 0;
4769 char buf[20];
4770 static char *mess = "A test message";
4771 BIO *bretry = BIO_new(bio_s_always_retry());
4772 BIO *tmp = NULL;
4773 SSL *peerupdate = NULL, *peerwrite = NULL;
4774
4775 if (!TEST_ptr(bretry)
4776 || !TEST_true(create_ssl_ctx_pair(TLS_server_method(),
4777 TLS_client_method(),
4778 TLS1_3_VERSION,
4779 0,
4780 &sctx, &cctx, cert, privkey))
4781 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4782 NULL, NULL))
4783 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4784 SSL_ERROR_NONE)))
4785 goto end;
4786
4787 peerupdate = tst == 0 ? clientssl : serverssl;
4788 peerwrite = tst == 0 ? serverssl : clientssl;
4789
4790 if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
4791 || !TEST_true(SSL_do_handshake(peerupdate)))
4792 goto end;
4793
4794 /* Swap the writing endpoint's write BIO to force a retry */
4795 tmp = SSL_get_wbio(peerwrite);
4796 if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
4797 tmp = NULL;
4798 goto end;
4799 }
4800 SSL_set0_wbio(peerwrite, bretry);
4801 bretry = NULL;
4802
4803 /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
4804 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
4805 || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
4806 goto end;
4807
4808 /* Reinstate the original writing endpoint's write BIO */
4809 SSL_set0_wbio(peerwrite, tmp);
4810 tmp = NULL;
4811
4812 /* Now read some data - we will read the key update */
4813 if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
4814 || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
4815 goto end;
4816
4817 /*
4818 * Complete the write we started previously and read it from the other
4819 * endpoint
4820 */
4821 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
4822 || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
4823 goto end;
4824
4825 /* Write more data to ensure we send the KeyUpdate message back */
4826 if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
4827 || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
4828 goto end;
4829
4830 testresult = 1;
4831
4832 end:
4833 SSL_free(serverssl);
4834 SSL_free(clientssl);
4835 SSL_CTX_free(sctx);
4836 SSL_CTX_free(cctx);
4837 BIO_free(bretry);
4838 BIO_free(tmp);
4839
4840 return testresult;
4841}
b38ede80
TT
4842#endif /* OPENSSL_NO_TLS1_3 */
4843
e11b6aa4
MC
4844static int test_ssl_clear(int idx)
4845{
4846 SSL_CTX *cctx = NULL, *sctx = NULL;
4847 SSL *clientssl = NULL, *serverssl = NULL;
4848 int testresult = 0;
4849
4850#ifdef OPENSSL_NO_TLS1_2
4851 if (idx == 1)
4852 return 1;
4853#endif
4854
4855 /* Create an initial connection */
7d7f6834 4856 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 4857 TLS1_VERSION, 0,
7d7f6834 4858 &sctx, &cctx, cert, privkey))
e11b6aa4
MC
4859 || (idx == 1
4860 && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
4861 TLS1_2_VERSION)))
4862 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4863 &clientssl, NULL, NULL))
4864 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4865 SSL_ERROR_NONE)))
4866 goto end;
4867
4868 SSL_shutdown(clientssl);
4869 SSL_shutdown(serverssl);
4870 SSL_free(serverssl);
4871 serverssl = NULL;
4872
4873 /* Clear clientssl - we're going to reuse the object */
4874 if (!TEST_true(SSL_clear(clientssl)))
4875 goto end;
4876
4877 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4878 NULL, NULL))
4879 || !TEST_true(create_ssl_connection(serverssl, clientssl,
4880 SSL_ERROR_NONE))
4881 || !TEST_true(SSL_session_reused(clientssl)))
4882 goto end;
4883
4884 SSL_shutdown(clientssl);
4885 SSL_shutdown(serverssl);
4886
4887 testresult = 1;
4888
4889 end:
4890 SSL_free(serverssl);
4891 SSL_free(clientssl);
4892 SSL_CTX_free(sctx);
4893 SSL_CTX_free(cctx);
4894
4895 return testresult;
4896}
4897
cf72c757
F
4898/* Parse CH and retrieve any MFL extension value if present */
4899static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
4900{
4901 long len;
4902 unsigned char *data;
72962d02 4903 PACKET pkt, pkt2, pkt3;
cf72c757
F
4904 unsigned int MFL_code = 0, type = 0;
4905
4906 if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
4907 goto end;
4908
72962d02
P
4909 memset(&pkt, 0, sizeof(pkt));
4910 memset(&pkt2, 0, sizeof(pkt2));
4911 memset(&pkt3, 0, sizeof(pkt3));
4912
cf72c757
F
4913 if (!TEST_true( PACKET_buf_init( &pkt, data, len ) )
4914 /* Skip the record header */
4915 || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
4916 /* Skip the handshake message header */
4917 || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
4918 /* Skip client version and random */
4919 || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
4920 + SSL3_RANDOM_SIZE))
4921 /* Skip session id */
4922 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
4923 /* Skip ciphers */
4924 || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
4925 /* Skip compression */
4926 || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
4927 /* Extensions len */
4928 || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
4929 goto end;
4930
4931 /* Loop through all extensions */
4932 while (PACKET_remaining(&pkt2)) {
4933 if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
4934 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
4935 goto end;
4936
4937 if (type == TLSEXT_TYPE_max_fragment_length) {
4938 if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
4939 || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
4940 goto end;
4941
4942 *mfl_codemfl_code = MFL_code;
4943 return 1;
4944 }
4945 }
4946
4947 end:
4948 return 0;
4949}
4950
4951/* Maximum-Fragment-Length TLS extension mode to test */
4952static const unsigned char max_fragment_len_test[] = {
4953 TLSEXT_max_fragment_length_512,
4954 TLSEXT_max_fragment_length_1024,
4955 TLSEXT_max_fragment_length_2048,
4956 TLSEXT_max_fragment_length_4096
4957};
4958
4959static int test_max_fragment_len_ext(int idx_tst)
4960{
4961 SSL_CTX *ctx;
4962 SSL *con = NULL;
4963 int testresult = 0, MFL_mode = 0;
4964 BIO *rbio, *wbio;
4965
4966 ctx = SSL_CTX_new(TLS_method());
4967 if (!TEST_ptr(ctx))
4968 goto end;
4969
4970 if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
4971 ctx, max_fragment_len_test[idx_tst])))
4972 goto end;
4973
4974 con = SSL_new(ctx);
4975 if (!TEST_ptr(con))
4976 goto end;
4977
4978 rbio = BIO_new(BIO_s_mem());
4979 wbio = BIO_new(BIO_s_mem());
4980 if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
4981 BIO_free(rbio);
4982 BIO_free(wbio);
4983 goto end;
4984 }
4985
4986 SSL_set_bio(con, rbio, wbio);
4987 SSL_set_connect_state(con);
4988
4989 if (!TEST_int_le(SSL_connect(con), 0)) {
4990 /* This shouldn't succeed because we don't have a server! */
4991 goto end;
4992 }
4993
4994 if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
4995 /* no MFL in client hello */
4996 goto end;
4997 if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
4998 goto end;
4999
5000 testresult = 1;
5001
5002end:
5003 SSL_free(con);
5004 SSL_CTX_free(ctx);
5005
5006 return testresult;
5007}
5008
9d75dce3
TS
5009#ifndef OPENSSL_NO_TLS1_3
5010static int test_pha_key_update(void)
5011{
5012 SSL_CTX *cctx = NULL, *sctx = NULL;
5013 SSL *clientssl = NULL, *serverssl = NULL;
5014 int testresult = 0;
5015
7d7f6834 5016 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 5017 TLS1_VERSION, 0,
9d75dce3
TS
5018 &sctx, &cctx, cert, privkey)))
5019 return 0;
5020
5021 if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
5022 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
5023 || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
5024 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
5025 goto end;
5026
e97be718 5027 SSL_CTX_set_post_handshake_auth(cctx, 1);
9d75dce3
TS
5028
5029 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5030 NULL, NULL)))
5031 goto end;
5032
9d75dce3
TS
5033 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5034 SSL_ERROR_NONE)))
5035 goto end;
5036
5037 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
5038 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
5039 goto end;
5040
5041 if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
5042 goto end;
5043
5044 /* Start handshake on the server */
5045 if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
5046 goto end;
5047
5048 /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
5049 if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5050 SSL_ERROR_NONE)))
5051 goto end;
5052
5053 SSL_shutdown(clientssl);
5054 SSL_shutdown(serverssl);
5055
5056 testresult = 1;
5057
5058 end:
5059 SSL_free(serverssl);
5060 SSL_free(clientssl);
5061 SSL_CTX_free(sctx);
5062 SSL_CTX_free(cctx);
5063 return testresult;
5064}
5065#endif
5066
76fd7a1d
MC
5067#if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
5068
5069static SRP_VBASE *vbase = NULL;
5070
5071static int ssl_srp_cb(SSL *s, int *ad, void *arg)
5072{
5073 int ret = SSL3_AL_FATAL;
5074 char *username;
5075 SRP_user_pwd *user = NULL;
5076
5077 username = SSL_get_srp_username(s);
5078 if (username == NULL) {
5079 *ad = SSL_AD_INTERNAL_ERROR;
5080 goto err;
5081 }
5082
5083 user = SRP_VBASE_get1_by_user(vbase, username);
5084 if (user == NULL) {
5085 *ad = SSL_AD_INTERNAL_ERROR;
5086 goto err;
5087 }
5088
5089 if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
5090 user->info) <= 0) {
5091 *ad = SSL_AD_INTERNAL_ERROR;
5092 goto err;
5093 }
5094
5095 ret = 0;
5096
5097 err:
5098 SRP_user_pwd_free(user);
5099 return ret;
5100}
5101
5102static int create_new_vfile(char *userid, char *password, const char *filename)
5103{
5104 char *gNid = NULL;
5105 OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
5106 TXT_DB *db = NULL;
5107 int ret = 0;
5108 BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
5109 size_t i;
5110
5111 if (!TEST_ptr(dummy) || !TEST_ptr(row))
5112 goto end;
5113
5114 gNid = SRP_create_verifier(userid, password, &row[DB_srpsalt],
5115 &row[DB_srpverifier], NULL, NULL);
5116 if (!TEST_ptr(gNid))
5117 goto end;
5118
5119 /*
5120 * The only way to create an empty TXT_DB is to provide a BIO with no data
5121 * in it!
5122 */
5123 db = TXT_DB_read(dummy, DB_NUMBER);
5124 if (!TEST_ptr(db))
5125 goto end;
5126
5127 out = BIO_new_file(filename, "w");
5128 if (!TEST_ptr(out))
5129 goto end;
5130
5131 row[DB_srpid] = OPENSSL_strdup(userid);
5132 row[DB_srptype] = OPENSSL_strdup("V");
5133 row[DB_srpgN] = OPENSSL_strdup(gNid);
5134
5135 if (!TEST_ptr(row[DB_srpid])
5136 || !TEST_ptr(row[DB_srptype])
5137 || !TEST_ptr(row[DB_srpgN])
5138 || !TEST_true(TXT_DB_insert(db, row)))
5139 goto end;
5140
5141 row = NULL;
5142
5143 if (!TXT_DB_write(out, db))
5144 goto end;
5145
5146 ret = 1;
5147 end:
5148 if (row != NULL) {
5149 for (i = 0; i < DB_NUMBER; i++)
5150 OPENSSL_free(row[i]);
5151 }
5152 OPENSSL_free(row);
5153 BIO_free(dummy);
5154 BIO_free(out);
5155 TXT_DB_free(db);
5156
5157 return ret;
5158}
5159
5160static int create_new_vbase(char *userid, char *password)
5161{
5162 BIGNUM *verifier = NULL, *salt = NULL;
5163 const SRP_gN *lgN = NULL;
5164 SRP_user_pwd *user_pwd = NULL;
5165 int ret = 0;
5166
5167 lgN = SRP_get_default_gN(NULL);
5168 if (!TEST_ptr(lgN))
5169 goto end;
5170
5171 if (!TEST_true(SRP_create_verifier_BN(userid, password, &salt, &verifier,
5172 lgN->N, lgN->g)))
5173 goto end;
5174
5175 user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
5176 if (!TEST_ptr(user_pwd))
5177 goto end;
5178
5179 user_pwd->N = lgN->N;
5180 user_pwd->g = lgN->g;
5181 user_pwd->id = OPENSSL_strdup(userid);
5182 if (!TEST_ptr(user_pwd->id))
5183 goto end;
5184
5185 user_pwd->v = verifier;
5186 user_pwd->s = salt;
5187 verifier = salt = NULL;
5188
5189 if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
5190 goto end;
5191 user_pwd = NULL;
5192
5193 ret = 1;
5194end:
5195 SRP_user_pwd_free(user_pwd);
5196 BN_free(salt);
5197 BN_free(verifier);
5198
5199 return ret;
5200}
5201
5202/*
5203 * SRP tests
5204 *
5205 * Test 0: Simple successful SRP connection, new vbase
5206 * Test 1: Connection failure due to bad password, new vbase
5207 * Test 2: Simple successful SRP connection, vbase loaded from existing file
5208 * Test 3: Connection failure due to bad password, vbase loaded from existing
5209 * file
5210 * Test 4: Simple successful SRP connection, vbase loaded from new file
5211 * Test 5: Connection failure due to bad password, vbase loaded from new file
5212 */
5213static int test_srp(int tst)
5214{
5215 char *userid = "test", *password = "password", *tstsrpfile;
5216 SSL_CTX *cctx = NULL, *sctx = NULL;
5217 SSL *clientssl = NULL, *serverssl = NULL;
5218 int ret, testresult = 0;
5219
5220 vbase = SRP_VBASE_new(NULL);
5221 if (!TEST_ptr(vbase))
5222 goto end;
5223
5224 if (tst == 0 || tst == 1) {
5225 if (!TEST_true(create_new_vbase(userid, password)))
5226 goto end;
5227 } else {
5228 if (tst == 4 || tst == 5) {
5229 if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
5230 goto end;
5231 tstsrpfile = tmpfilename;
5232 } else {
5233 tstsrpfile = srpvfile;
5234 }
5235 if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
5236 goto end;
5237 }
5238
5239 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
5c587fb6 5240 TLS1_VERSION, 0,
76fd7a1d
MC
5241 &sctx, &cctx, cert, privkey)))
5242 goto end;
5243
5244 if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
5245 || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
5246 || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
5247 || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
5248 || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
5249 goto end;
5250
5251 if (tst % 2 == 1) {
5252 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
5253 goto end;
5254 } else {
5255 if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
5256 goto end;
5257 }
5258
5259 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5260 NULL, NULL)))
5261 goto end;
5262
5263 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
5264 if (ret) {
5265 if (!TEST_true(tst % 2 == 0))
5266 goto end;
5267 } else {
5268 if (!TEST_true(tst % 2 == 1))
5269 goto end;
5270 }
5271
5272 testresult = 1;
5273
5274 end:
5275 SRP_VBASE_free(vbase);
5276 vbase = NULL;
5277 SSL_free(serverssl);
5278 SSL_free(clientssl);
5279 SSL_CTX_free(sctx);
5280 SSL_CTX_free(cctx);
5281
5282 return testresult;
5283}
5284#endif
5285
5718fe45
MC
5286static int info_cb_failed = 0;
5287static int info_cb_offset = 0;
5288static int info_cb_this_state = -1;
5289
5290static struct info_cb_states_st {
5291 int where;
5292 const char *statestr;
5293} info_cb_states[][60] = {
5294 {
5295 /* TLSv1.2 server followed by resumption */
5296 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5297 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
5298 {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
5299 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
5300 {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
5301 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
5302 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
5303 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
5304 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"},
5305 {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
5306 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
5307 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
5308 {SSL_CB_EXIT, NULL}, {0, NULL},
5309 }, {
5310 /* TLSv1.2 client followed by resumption */
5311 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5312 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
5313 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
5314 {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
5315 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
5316 {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
5317 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
5318 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5319 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
5320 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
5321 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
5322 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
5323 }, {
5324 /* TLSv1.3 server followed by resumption */
5325 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5326 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
5327 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
5328 {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
5329 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
4af5836b
MC
5330 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
5331 {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
5332 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5333 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
5334 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
5335 {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
5336 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
5337 {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
5718fe45
MC
5338 }, {
5339 /* TLSv1.3 client followed by resumption */
5340 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5341 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
5342 {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
5343 {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
5344 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4af5836b
MC
5345 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
5346 {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "},
5347 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
5718fe45
MC
5348 {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
5349 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
5350 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
5351 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
5352 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
4af5836b
MC
5353 {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
5354 {SSL_CB_EXIT, NULL}, {0, NULL},
5718fe45
MC
5355 }, {
5356 /* TLSv1.3 server, early_data */
5357 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5358 {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
5359 {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
5360 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
5361 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
5362 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
4af5836b 5363 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
5718fe45
MC
5364 {SSL_CB_EXIT, NULL}, {0, NULL},
5365 }, {
5366 /* TLSv1.3 client, early_data */
5367 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
5368 {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
5369 {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
5370 {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
5371 {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
5372 {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
5373 {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
4af5836b
MC
5374 {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
5375 {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
5718fe45
MC
5376 }, {
5377 {0, NULL},
5378 }
5379};
5380
5381static void sslapi_info_callback(const SSL *s, int where, int ret)
5382{
5383 struct info_cb_states_st *state = info_cb_states[info_cb_offset];
5384
5385 /* We do not ever expect a connection to fail in this test */
5386 if (!TEST_false(ret == 0)) {
5387 info_cb_failed = 1;
5388 return;
5389 }
5390
5391 /*
5392 * Do some sanity checks. We never expect these things to happen in this
5393 * test
5394 */
5395 if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
5396 || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
5397 || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
5398 info_cb_failed = 1;
5399 return;
5400 }
5401
5402 /* Now check we're in the right state */
5403 if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
5404 info_cb_failed = 1;
5405 return;
5406 }
5407 if ((where & SSL_CB_LOOP) != 0
5408 && !TEST_int_eq(strcmp(SSL_state_string(s),
5409 state[info_cb_this_state].statestr), 0)) {
5410 info_cb_failed = 1;
5411 return;
5412 }
033c181b 5413
4af5836b
MC
5414 /*
5415 * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
5416 */
5417 if ((where & SSL_CB_HANDSHAKE_DONE)
5418 && SSL_in_init((SSL *)s) != 0) {
033c181b
MC
5419 info_cb_failed = 1;
5420 return;
5421 }
5718fe45
MC
5422}
5423
5424/*
5425 * Test the info callback gets called when we expect it to.
5426 *
5427 * Test 0: TLSv1.2, server
5428 * Test 1: TLSv1.2, client
5429 * Test 2: TLSv1.3, server
5430 * Test 3: TLSv1.3, client
5431 * Test 4: TLSv1.3, server, early_data
5432 * Test 5: TLSv1.3, client, early_data
5433 */
5434static int test_info_callback(int tst)
5435{
5436 SSL_CTX *cctx = NULL, *sctx = NULL;
5437 SSL *clientssl = NULL, *serverssl = NULL;
5438 SSL_SESSION *clntsess = NULL;
5439 int testresult = 0;
5440 int tlsvers;
5441
5442 if (tst < 2) {
1aac20f5
MC
5443/* We need either ECDHE or DHE for the TLSv1.2 test to work */
5444#if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
5445 || !defined(OPENSSL_NO_DH))
5718fe45
MC
5446 tlsvers = TLS1_2_VERSION;
5447#else
5448 return 1;
5449#endif
5450 } else {
5451#ifndef OPENSSL_NO_TLS1_3
5452 tlsvers = TLS1_3_VERSION;
5453#else
5454 return 1;
5455#endif
5456 }
5457
5458 /* Reset globals */
5459 info_cb_failed = 0;
5460 info_cb_this_state = -1;
5461 info_cb_offset = tst;
5462
6e07834c 5463#ifndef OPENSSL_NO_TLS1_3
5718fe45
MC
5464 if (tst >= 4) {
5465 SSL_SESSION *sess = NULL;
5466 size_t written, readbytes;
5467 unsigned char buf[80];
5468
5469 /* early_data tests */
5470 if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
5471 &serverssl, &sess, 0)))
5472 goto end;
5473
5474 /* We don't actually need this reference */
5475 SSL_SESSION_free(sess);
5476
5477 SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
5478 sslapi_info_callback);
5479
5480 /* Write and read some early data and then complete the connection */
5481 if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
5482 &written))
5483 || !TEST_size_t_eq(written, strlen(MSG1))
5484 || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
5485 sizeof(buf), &readbytes),
5486 SSL_READ_EARLY_DATA_SUCCESS)
5487 || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
5488 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
5489 SSL_EARLY_DATA_ACCEPTED)
5490 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5491 SSL_ERROR_NONE))
5492 || !TEST_false(info_cb_failed))
5493 goto end;
5494
5495 testresult = 1;
5496 goto end;
5497 }
6e07834c 5498#endif
5718fe45
MC
5499
5500 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5501 TLS_client_method(),
5502 tlsvers, tlsvers, &sctx, &cctx, cert,
5503 privkey)))
5504 goto end;
5505
5506 /*
5507 * For even numbered tests we check the server callbacks. For odd numbers we
5508 * check the client.
5509 */
5510 SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
5511 sslapi_info_callback);
5512
5513 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5514 &clientssl, NULL, NULL))
5515 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5516 SSL_ERROR_NONE))
5517 || !TEST_false(info_cb_failed))
5518 goto end;
5519
5520
5521
5522 clntsess = SSL_get1_session(clientssl);
5523 SSL_shutdown(clientssl);
5524 SSL_shutdown(serverssl);
5525 SSL_free(serverssl);
5526 SSL_free(clientssl);
5527 serverssl = clientssl = NULL;
5528
5529 /* Now do a resumption */
5530 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
5531 NULL))
5532 || !TEST_true(SSL_set_session(clientssl, clntsess))
5533 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5534 SSL_ERROR_NONE))
5535 || !TEST_true(SSL_session_reused(clientssl))
5536 || !TEST_false(info_cb_failed))
5537 goto end;
5538
5539 testresult = 1;
5540
5541 end:
5542 SSL_free(serverssl);
5543 SSL_free(clientssl);
5544 SSL_SESSION_free(clntsess);
5545 SSL_CTX_free(sctx);
5546 SSL_CTX_free(cctx);
5547 return testresult;
5548}
5549
4a432af8
MC
5550static int test_ssl_pending(int tst)
5551{
5552 SSL_CTX *cctx = NULL, *sctx = NULL;
5553 SSL *clientssl = NULL, *serverssl = NULL;
5554 int testresult = 0;
5555 char msg[] = "A test message";
5556 char buf[5];
5557 size_t written, readbytes;
5558
5559 if (tst == 0) {
5560 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5561 TLS_client_method(),
5c587fb6 5562 TLS1_VERSION, 0,
4a432af8
MC
5563 &sctx, &cctx, cert, privkey)))
5564 goto end;
5565 } else {
5566#ifndef OPENSSL_NO_DTLS
5567 if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
5568 DTLS_client_method(),
5c587fb6 5569 DTLS1_VERSION, 0,
4a432af8
MC
5570 &sctx, &cctx, cert, privkey)))
5571 goto end;
5572#else
5573 return 1;
5574#endif
5575 }
5576
5577 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5578 NULL, NULL))
5579 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5580 SSL_ERROR_NONE)))
5581 goto end;
5582
e8251092
MC
5583 if (!TEST_int_eq(SSL_pending(clientssl), 0)
5584 || !TEST_false(SSL_has_pending(clientssl))
5585 || !TEST_int_eq(SSL_pending(serverssl), 0)
5586 || !TEST_false(SSL_has_pending(serverssl))
5587 || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
4a432af8
MC
5588 || !TEST_size_t_eq(written, sizeof(msg))
5589 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
5590 || !TEST_size_t_eq(readbytes, sizeof(buf))
e8251092
MC
5591 || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
5592 || !TEST_true(SSL_has_pending(clientssl)))
4a432af8
MC
5593 goto end;
5594
5595 testresult = 1;
5596
5597 end:
5598 SSL_free(serverssl);
5599 SSL_free(clientssl);
5600 SSL_CTX_free(sctx);
5601 SSL_CTX_free(cctx);
5602
5603 return testresult;
5604}
5605
e401389a
MC
5606static struct {
5607 unsigned int maxprot;
5608 const char *clntciphers;
5609 const char *clnttls13ciphers;
5610 const char *srvrciphers;
5611 const char *srvrtls13ciphers;
5612 const char *shared;
5613} shared_ciphers_data[] = {
60155b9a
MC
5614/*
5615 * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
5616 * TLSv1.3 is enabled but TLSv1.2 is disabled.
5617 */
5618#if defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
e401389a
MC
5619 {
5620 TLS1_2_VERSION,
5621 "AES128-SHA:AES256-SHA",
5622 NULL,
5623 "AES256-SHA:DHE-RSA-AES128-SHA",
5624 NULL,
5625 "AES256-SHA"
5626 },
5627 {
5628 TLS1_2_VERSION,
5629 "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
5630 NULL,
5631 "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
5632 NULL,
5633 "AES128-SHA:AES256-SHA"
5634 },
5635 {
5636 TLS1_2_VERSION,
5637 "AES128-SHA:AES256-SHA",
5638 NULL,
5639 "AES128-SHA:DHE-RSA-AES128-SHA",
5640 NULL,
5641 "AES128-SHA"
5642 },
60155b9a
MC
5643#endif
5644/*
5645 * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
5646 * enabled.
5647 */
5648#if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
5649 && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
e401389a
MC
5650 {
5651 TLS1_3_VERSION,
5652 "AES128-SHA:AES256-SHA",
5653 NULL,
5654 "AES256-SHA:AES128-SHA256",
5655 NULL,
5656 "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
5657 "TLS_AES_128_GCM_SHA256:AES256-SHA"
5658 },
60155b9a
MC
5659#endif
5660#ifndef OPENSSL_NO_TLS1_3
e401389a
MC
5661 {
5662 TLS1_3_VERSION,
5663 "AES128-SHA",
5664 "TLS_AES_256_GCM_SHA384",
5665 "AES256-SHA",
5666 "TLS_AES_256_GCM_SHA384",
5667 "TLS_AES_256_GCM_SHA384"
5668 },
5669#endif
5670};
5671
5672static int test_ssl_get_shared_ciphers(int tst)
5673{
5674 SSL_CTX *cctx = NULL, *sctx = NULL;
5675 SSL *clientssl = NULL, *serverssl = NULL;
5676 int testresult = 0;
5677 char buf[1024];
5678
5679 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5680 TLS_client_method(),
5681 TLS1_VERSION,
5682 shared_ciphers_data[tst].maxprot,
5683 &sctx, &cctx, cert, privkey)))
5684 goto end;
5685
5686 if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
5687 shared_ciphers_data[tst].clntciphers))
5688 || (shared_ciphers_data[tst].clnttls13ciphers != NULL
5689 && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
5690 shared_ciphers_data[tst].clnttls13ciphers)))
5691 || !TEST_true(SSL_CTX_set_cipher_list(sctx,
5692 shared_ciphers_data[tst].srvrciphers))
5693 || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
5694 && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
5695 shared_ciphers_data[tst].srvrtls13ciphers))))
5696 goto end;
5697
5698
5699 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5700 NULL, NULL))
5701 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5702 SSL_ERROR_NONE)))
5703 goto end;
5704
5705 if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
5706 || !TEST_int_eq(strcmp(buf, shared_ciphers_data[tst].shared), 0)) {
5707 TEST_info("Shared ciphers are: %s\n", buf);
5708 goto end;
5709 }
5710
5711 testresult = 1;
5712
5713 end:
5714 SSL_free(serverssl);
5715 SSL_free(clientssl);
5716 SSL_CTX_free(sctx);
5717 SSL_CTX_free(cctx);
5718
5719 return testresult;
5720}
5721
d0191fe0 5722static const char *appdata = "Hello World";
61fb5923
MC
5723static int gen_tick_called, dec_tick_called, tick_key_cb_called;
5724static int tick_key_renew = 0;
5725static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
d0191fe0
MC
5726
5727static int gen_tick_cb(SSL *s, void *arg)
5728{
5729 gen_tick_called = 1;
5730
5731 return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
5732 strlen(appdata));
5733}
5734
5735static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
5736 const unsigned char *keyname,
5737 size_t keyname_length,
61fb5923
MC
5738 SSL_TICKET_STATUS status,
5739 void *arg)
d0191fe0
MC
5740{
5741 void *tickdata;
5742 size_t tickdlen;
5743
5744 dec_tick_called = 1;
5745
61fb5923
MC
5746 if (status == SSL_TICKET_EMPTY)
5747 return SSL_TICKET_RETURN_IGNORE_RENEW;
d0191fe0 5748
61fb5923
MC
5749 if (!TEST_true(status == SSL_TICKET_SUCCESS
5750 || status == SSL_TICKET_SUCCESS_RENEW))
5751 return SSL_TICKET_RETURN_ABORT;
5752
5753 if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
5754 &tickdlen))
d0191fe0
MC
5755 || !TEST_size_t_eq(tickdlen, strlen(appdata))
5756 || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
61fb5923 5757 return SSL_TICKET_RETURN_ABORT;
d0191fe0 5758
61fb5923
MC
5759 if (tick_key_cb_called) {
5760 /* Don't change what the ticket key callback wanted to do */
5761 switch (status) {
5762 case SSL_TICKET_NO_DECRYPT:
5763 return SSL_TICKET_RETURN_IGNORE_RENEW;
5764
5765 case SSL_TICKET_SUCCESS:
5766 return SSL_TICKET_RETURN_USE;
d0191fe0 5767
61fb5923
MC
5768 case SSL_TICKET_SUCCESS_RENEW:
5769 return SSL_TICKET_RETURN_USE_RENEW;
5770
5771 default:
5772 return SSL_TICKET_RETURN_ABORT;
5773 }
5774 }
5775 return tick_dec_ret;
5776
5777}
d0191fe0
MC
5778
5779static int tick_key_cb(SSL *s, unsigned char key_name[16],
5780 unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
5781 HMAC_CTX *hctx, int enc)
5782{
5783 const unsigned char tick_aes_key[16] = "0123456789abcdef";
5784 const unsigned char tick_hmac_key[16] = "0123456789abcdef";
5785
61fb5923 5786 tick_key_cb_called = 1;
d0191fe0
MC
5787 memset(iv, 0, AES_BLOCK_SIZE);
5788 memset(key_name, 0, 16);
5789 if (!EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, tick_aes_key, iv, enc)
5790 || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key),
5791 EVP_sha256(), NULL))
5792 return -1;
5793
61fb5923 5794 return tick_key_renew ? 2 : 1;
d0191fe0
MC
5795}
5796
5797/*
5798 * Test the various ticket callbacks
61fb5923
MC
5799 * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
5800 * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
5801 * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
5802 * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
5803 * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
5804 * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
5805 * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
5806 * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
5807 * Test 8: TLSv1.2, ticket key callback, ticket, no renewal
5808 * Test 9: TLSv1.3, ticket key callback, ticket, no renewal
5809 * Test 10: TLSv1.2, ticket key callback, ticket, renewal
5810 * Test 11: TLSv1.3, ticket key callback, ticket, renewal
d0191fe0
MC
5811 */
5812static int test_ticket_callbacks(int tst)
5813{
5814 SSL_CTX *cctx = NULL, *sctx = NULL;
5815 SSL *clientssl = NULL, *serverssl = NULL;
5816 SSL_SESSION *clntsess = NULL;
5817 int testresult = 0;
5818
5819#ifdef OPENSSL_NO_TLS1_2
ba8b48e9 5820 if (tst % 2 == 0)
d0191fe0
MC
5821 return 1;
5822#endif
5823#ifdef OPENSSL_NO_TLS1_3
ba8b48e9 5824 if (tst % 2 == 1)
d0191fe0
MC
5825 return 1;
5826#endif
5827
61fb5923 5828 gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
d0191fe0
MC
5829
5830 /* Which tests the ticket key callback should request renewal for */
61fb5923
MC
5831 if (tst == 10 || tst == 11)
5832 tick_key_renew = 1;
d0191fe0 5833 else
61fb5923
MC
5834 tick_key_renew = 0;
5835
5836 /* Which tests the decrypt ticket callback should request renewal for */
5837 switch (tst) {
5838 case 0:
5839 case 1:
5840 tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
5841 break;
5842
5843 case 2:
5844 case 3:
5845 tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
5846 break;
5847
5848 case 4:
5849 case 5:
5850 tick_dec_ret = SSL_TICKET_RETURN_USE;
5851 break;
5852
5853 case 6:
5854 case 7:
5855 tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
5856 break;
5857
5858 default:
5859 tick_dec_ret = SSL_TICKET_RETURN_ABORT;
5860 }
d0191fe0
MC
5861
5862 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5863 TLS_client_method(),
5864 TLS1_VERSION,
61fb5923
MC
5865 ((tst % 2) == 0) ? TLS1_2_VERSION
5866 : TLS1_3_VERSION,
d0191fe0
MC
5867 &sctx, &cctx, cert, privkey)))
5868 goto end;
5869
61fb5923
MC
5870 /*
5871 * We only want sessions to resume from tickets - not the session cache. So
5872 * switch the cache off.
5873 */
5874 if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
5875 goto end;
5876
d0191fe0
MC
5877 if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
5878 NULL)))
5879 goto end;
5880
61fb5923 5881 if (tst >= 8
d0191fe0
MC
5882 && !TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
5883 goto end;
5884
5885 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5886 NULL, NULL))
5887 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5888 SSL_ERROR_NONE)))
5889 goto end;
5890
61fb5923
MC
5891 /*
5892 * The decrypt ticket key callback in TLSv1.2 should be called even though
5893 * we have no ticket yet, because it gets called with a status of
5894 * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
5895 * actually send any ticket data). This does not happen in TLSv1.3 because
5896 * it is not valid to send empty ticket data in TLSv1.3.
5897 */
d0191fe0 5898 if (!TEST_int_eq(gen_tick_called, 1)
61fb5923 5899 || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
d0191fe0
MC
5900 goto end;
5901
5902 gen_tick_called = dec_tick_called = 0;
5903
5904 clntsess = SSL_get1_session(clientssl);
5905 SSL_shutdown(clientssl);
5906 SSL_shutdown(serverssl);
5907 SSL_free(serverssl);
5908 SSL_free(clientssl);
5909 serverssl = clientssl = NULL;
5910
5911 /* Now do a resumption */
5912 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
5913 NULL))
5914 || !TEST_true(SSL_set_session(clientssl, clntsess))
5915 || !TEST_true(create_ssl_connection(serverssl, clientssl,
61fb5923 5916 SSL_ERROR_NONE)))
d0191fe0
MC
5917 goto end;
5918
61fb5923
MC
5919 if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
5920 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
5921 if (!TEST_false(SSL_session_reused(clientssl)))
5922 goto end;
5923 } else {
5924 if (!TEST_true(SSL_session_reused(clientssl)))
5925 goto end;
5926 }
5927
d0191fe0 5928 if (!TEST_int_eq(gen_tick_called,
61fb5923
MC
5929 (tick_key_renew
5930 || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
5931 || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
5932 ? 1 : 0)
d0191fe0
MC
5933 || !TEST_int_eq(dec_tick_called, 1))
5934 goto end;
5935
5936 testresult = 1;
5937
5938 end:
5939 SSL_SESSION_free(clntsess);
5940 SSL_free(serverssl);
5941 SSL_free(clientssl);
5942 SSL_CTX_free(sctx);
5943 SSL_CTX_free(cctx);
5944
5945 return testresult;
5946}
5947
c748834f
MC
5948/*
5949 * Test bi-directional shutdown.
5950 * Test 0: TLSv1.2
5951 * Test 1: TLSv1.2, server continues to read/write after client shutdown
5952 * Test 2: TLSv1.3, no pending NewSessionTicket messages
5953 * Test 3: TLSv1.3, pending NewSessionTicket messages
80eff008
KR
5954 * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
5955 * sends key update, client reads it
57d7b988
MC
5956 * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
5957 * sends CertificateRequest, client reads and ignores it
5958 * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
c748834f
MC
5959 * doesn't read it
5960 */
5961static int test_shutdown(int tst)
5962{
5963 SSL_CTX *cctx = NULL, *sctx = NULL;
5964 SSL *clientssl = NULL, *serverssl = NULL;
5965 int testresult = 0;
5966 char msg[] = "A test message";
5967 char buf[80];
5968 size_t written, readbytes;
80eff008 5969 SSL_SESSION *sess;
c748834f
MC
5970
5971#ifdef OPENSSL_NO_TLS1_2
a97d19f7 5972 if (tst <= 1)
c748834f
MC
5973 return 1;
5974#endif
5975#ifdef OPENSSL_NO_TLS1_3
a97d19f7 5976 if (tst >= 2)
c748834f
MC
5977 return 1;
5978#endif
5979
5980 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
5981 TLS_client_method(),
5982 TLS1_VERSION,
5983 (tst <= 1) ? TLS1_2_VERSION
5984 : TLS1_3_VERSION,
57d7b988
MC
5985 &sctx, &cctx, cert, privkey)))
5986 goto end;
5987
5988 if (tst == 5)
5989 SSL_CTX_set_post_handshake_auth(cctx, 1);
5990
5991 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
c748834f
MC
5992 NULL, NULL)))
5993 goto end;
5994
5995 if (tst == 3) {
5996 if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
80c455d5 5997 SSL_ERROR_NONE, 1))
80eff008
KR
5998 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
5999 || !TEST_false(SSL_SESSION_is_resumable(sess)))
c748834f
MC
6000 goto end;
6001 } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
80eff008
KR
6002 SSL_ERROR_NONE))
6003 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
6004 || !TEST_true(SSL_SESSION_is_resumable(sess))) {
c748834f
MC
6005 goto end;
6006 }
6007
6008 if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
6009 goto end;
6010
6011 if (tst >= 4) {
6012 /*
6013 * Reading on the server after the client has sent close_notify should
6014 * fail and provide SSL_ERROR_ZERO_RETURN
6015 */
6016 if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
6017 || !TEST_int_eq(SSL_get_error(serverssl, 0),
6018 SSL_ERROR_ZERO_RETURN)
6019 || !TEST_int_eq(SSL_get_shutdown(serverssl),
6020 SSL_RECEIVED_SHUTDOWN)
6021 /*
6022 * Even though we're shutdown on receive we should still be
6023 * able to write.
6024 */
80eff008
KR
6025 || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
6026 goto end;
57d7b988
MC
6027 if (tst == 4
6028 && !TEST_true(SSL_key_update(serverssl,
6029 SSL_KEY_UPDATE_REQUESTED)))
6030 goto end;
6031 if (tst == 5) {
6032 SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
6033 if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
6034 goto end;
6035 }
6036 if ((tst == 4 || tst == 5)
6037 && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
80eff008
KR
6038 goto end;
6039 if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
c748834f 6040 goto end;
57d7b988 6041 if (tst == 4 || tst == 5) {
80eff008 6042 /* Should still be able to read data from server */
c748834f 6043 if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
80eff008
KR
6044 &readbytes))
6045 || !TEST_size_t_eq(readbytes, sizeof(msg))
6046 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
6047 || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
6048 &readbytes))
c748834f
MC
6049 || !TEST_size_t_eq(readbytes, sizeof(msg))
6050 || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
6051 goto end;
6052 }
6053 }
6054
6055 /* Writing on the client after sending close_notify shouldn't be possible */
ba709049 6056 if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
c748834f
MC
6057 goto end;
6058
6059 if (tst < 4) {
6060 /*
6061 * For these tests the client has sent close_notify but it has not yet
6062 * been received by the server. The server has not sent close_notify
6063 * yet.
6064 */
6065 if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
ba709049
MC
6066 /*
6067 * Writing on the server after sending close_notify shouldn't
6068 * be possible.
6069 */
6070 || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
c748834f 6071 || !TEST_int_eq(SSL_shutdown(clientssl), 1)
80eff008
KR
6072 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
6073 || !TEST_true(SSL_SESSION_is_resumable(sess))
c748834f
MC
6074 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
6075 goto end;
57d7b988 6076 } else if (tst == 4 || tst == 5) {
c748834f
MC
6077 /*
6078 * In this test the client has sent close_notify and it has been
6079 * received by the server which has responded with a close_notify. The
358ffa05 6080 * client needs to read the close_notify sent by the server.
c748834f 6081 */
80eff008
KR
6082 if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
6083 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
6084 || !TEST_true(SSL_SESSION_is_resumable(sess)))
c748834f 6085 goto end;
358ffa05
MC
6086 } else {
6087 /*
57d7b988 6088 * tst == 6
358ffa05
MC
6089 *
6090 * The client has sent close_notify and is expecting a close_notify
6091 * back, but instead there is application data first. The shutdown
6092 * should fail with a fatal error.
6093 */
6094 if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
6095 || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
6096 goto end;
c748834f
MC
6097 }
6098
6099 testresult = 1;
6100
6101 end:
6102 SSL_free(serverssl);
6103 SSL_free(clientssl);
6104 SSL_CTX_free(sctx);
6105 SSL_CTX_free(cctx);
6106
6107 return testresult;
6108}
6109
7f1d923a 6110#if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3)
cd6fe29f
MC
6111static int cert_cb_cnt;
6112
6113static int cert_cb(SSL *s, void *arg)
6114{
6115 SSL_CTX *ctx = (SSL_CTX *)arg;
6116
6117 if (cert_cb_cnt == 0) {
6118 /* Suspend the handshake */
6119 cert_cb_cnt++;
6120 return -1;
6121 } else if (cert_cb_cnt == 1) {
6122 /*
6123 * Update the SSL_CTX, set the certificate and private key and then
6124 * continue the handshake normally.
6125 */
6126 if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
6127 return 0;
6128
6129 if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
6130 || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
6131 SSL_FILETYPE_PEM))
6132 || !TEST_true(SSL_check_private_key(s)))
6133 return 0;
6134 cert_cb_cnt++;
6135 return 1;
6136 }
6137
6138 /* Abort the handshake */
6139 return 0;
6140}
6141
6142/*
6143 * Test the certificate callback.
6144 * Test 0: Callback fails
6145 * Test 1: Success - no SSL_set_SSL_CTX() in the callback
6146 * Test 2: Success - SSL_set_SSL_CTX() in the callback
6147 */
6148static int test_cert_cb_int(int prot, int tst)
6149{
6150 SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
6151 SSL *clientssl = NULL, *serverssl = NULL;
6152 int testresult = 0, ret;
6153
6154 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
6155 TLS_client_method(),
6156 TLS1_VERSION,
6157 prot,
6158 &sctx, &cctx, NULL, NULL)))
6159 goto end;
6160
6161 if (tst == 0)
6162 cert_cb_cnt = -1;
6163 else
6164 cert_cb_cnt = 0;
6165 if (tst == 2)
6166 snictx = SSL_CTX_new(TLS_server_method());
6167 SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
6168
6169 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6170 NULL, NULL)))
6171 goto end;
6172
6173 ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
6174 if (!TEST_true(tst == 0 ? !ret : ret)
6175 || (tst > 0 && !TEST_int_eq(cert_cb_cnt, 2))) {
6176 goto end;
6177 }
6178
6179 testresult = 1;
6180
6181 end:
6182 SSL_free(serverssl);
6183 SSL_free(clientssl);
6184 SSL_CTX_free(sctx);
6185 SSL_CTX_free(cctx);
6186 SSL_CTX_free(snictx);
6187
6188 return testresult;
6189}
7f1d923a 6190#endif
cd6fe29f
MC
6191
6192static int test_cert_cb(int tst)
6193{
6194 int testresult = 1;
6195
6196#ifndef OPENSSL_NO_TLS1_2
6197 testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
6198#endif
7f1d923a 6199#ifndef OPENSSL_NO_TLS1_3
cd6fe29f
MC
6200 testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
6201#endif
6202
6203 return testresult;
6204}
6205
6e46c065
MC
6206static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
6207{
6208 X509 *xcert, *peer;
6209 EVP_PKEY *privpkey;
6210 BIO *in = NULL;
6211
6212 /* Check that SSL_get_peer_certificate() returns something sensible */
6213 peer = SSL_get_peer_certificate(ssl);
6214 if (!TEST_ptr(peer))
6215 return 0;
6216 X509_free(peer);
6217
6218 in = BIO_new_file(cert, "r");
6219 if (!TEST_ptr(in))
6220 return 0;
6221
6222 xcert = PEM_read_bio_X509(in, NULL, NULL, NULL);
6223 BIO_free(in);
6224 if (!TEST_ptr(xcert))
6225 return 0;
6226
6227 in = BIO_new_file(privkey, "r");
6228 if (!TEST_ptr(in)) {
6229 X509_free(xcert);
6230 return 0;
6231 }
6232
6233 privpkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
6234 BIO_free(in);
6235 if (!TEST_ptr(privpkey)) {
6236 X509_free(xcert);
6237 return 0;
6238 }
6239
6240 *x509 = xcert;
6241 *pkey = privpkey;
6242
6243 return 1;
6244}
6245
6246static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
6247{
6248 return 1;
6249}
6250
6251static int test_client_cert_cb(int tst)
6252{
6253 SSL_CTX *cctx = NULL, *sctx = NULL;
6254 SSL *clientssl = NULL, *serverssl = NULL;
6255 int testresult = 0;
6256
6257#ifdef OPENSSL_NO_TLS1_2
6258 if (tst == 0)
6259 return 1;
6260#endif
6261#ifdef OPENSSL_NO_TLS1_3
6262 if (tst == 1)
6263 return 1;
6264#endif
6265
6266 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
6267 TLS_client_method(),
6268 TLS1_VERSION,
6269 tst == 0 ? TLS1_2_VERSION
6270 : TLS1_3_VERSION,
6271 &sctx, &cctx, cert, privkey)))
6272 goto end;
6273
6274 /*
6275 * Test that setting a client_cert_cb results in a client certificate being
6276 * sent.
6277 */
6278 SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
6279 SSL_CTX_set_verify(sctx,
6280 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
6281 verify_cb);
fb8c8359
MC
6282
6283 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6284 NULL, NULL))
6285 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6286 SSL_ERROR_NONE)))
6287 goto end;
6288
6289 testresult = 1;
6290
6291 end:
6292 SSL_free(serverssl);
6293 SSL_free(clientssl);
6294 SSL_CTX_free(sctx);
6295 SSL_CTX_free(cctx);
6296
6297 return testresult;
6298}
6299
6300#if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3)
6301/*
6302 * Test setting certificate authorities on both client and server.
6303 *
6304 * Test 0: SSL_CTX_set0_CA_list() only
6305 * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
6306 * Test 2: Only SSL_CTX_set_client_CA_list()
6307 */
6308static int test_ca_names_int(int prot, int tst)
6309{
6310 SSL_CTX *cctx = NULL, *sctx = NULL;
6311 SSL *clientssl = NULL, *serverssl = NULL;
6312 int testresult = 0;
6313 size_t i;
6314 X509_NAME *name[] = { NULL, NULL, NULL, NULL };
6315 char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
6316 STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
6317 const STACK_OF(X509_NAME) *sktmp = NULL;
6318
6319 for (i = 0; i < OSSL_NELEM(name); i++) {
6320 name[i] = X509_NAME_new();
6321 if (!TEST_ptr(name[i])
6322 || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
6323 MBSTRING_ASC,
6324 (unsigned char *)
6325 strnames[i],
6326 -1, -1, 0)))
6327 goto end;
6328 }
6329
6330 if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
6331 TLS_client_method(),
6332 TLS1_VERSION,
6333 prot,
6334 &sctx, &cctx, cert, privkey)))
6335 goto end;
6336
6337 SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
6338
6339 if (tst == 0 || tst == 1) {
6340 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
6341 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
6342 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
6343 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
6344 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
6345 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
6346 goto end;
6347
6348 SSL_CTX_set0_CA_list(sctx, sk1);
6349 SSL_CTX_set0_CA_list(cctx, sk2);
6350 sk1 = sk2 = NULL;
6351 }
6352 if (tst == 1 || tst == 2) {
6353 if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
6354 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
6355 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
6356 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
6357 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
6358 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
6359 goto end;
6360
6361 SSL_CTX_set_client_CA_list(sctx, sk1);
6362 SSL_CTX_set_client_CA_list(cctx, sk2);
6363 sk1 = sk2 = NULL;
6364 }
6365
6e46c065
MC
6366 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6367 NULL, NULL))
6368 || !TEST_true(create_ssl_connection(serverssl, clientssl,
6369 SSL_ERROR_NONE)))
6370 goto end;
6371
fb8c8359
MC
6372 /*
6373 * We only expect certificate authorities to have been sent to the server
6374 * if we are using TLSv1.3 and SSL_set0_CA_list() was used
6375 */
6376 sktmp = SSL_get0_peer_CA_list(serverssl);
6377 if (prot == TLS1_3_VERSION
6378 && (tst == 0 || tst == 1)) {
6379 if (!TEST_ptr(sktmp)
6380 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
6381 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
6382 name[0]), 0)
6383 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
6384 name[1]), 0))
6385 goto end;
6386 } else if (!TEST_ptr_null(sktmp)) {
6387 goto end;
6388 }
6389
6390 /*
6391 * In all tests we expect certificate authorities to have been sent to the
6392 * client. However, SSL_set_client_CA_list() should override
6393 * SSL_set0_CA_list()
6394 */
6395 sktmp = SSL_get0_peer_CA_list(clientssl);
6396 if (!TEST_ptr(sktmp)
6397 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
6398 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
6399 name[tst == 0 ? 0 : 2]), 0)
6400 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
6401 name[tst == 0 ? 1 : 3]), 0))
6402 goto end;
6403
6e46c065
MC
6404 testresult = 1;
6405
6406 end:
6407 SSL_free(serverssl);
6408 SSL_free(clientssl);
6409 SSL_CTX_free(sctx);
6410 SSL_CTX_free(cctx);
fb8c8359
MC
6411 for (i = 0; i < OSSL_NELEM(name); i++)
6412 X509_NAME_free(name[i]);
6413 sk_X509_NAME_pop_free(sk1, X509_NAME_free);
6414 sk_X509_NAME_pop_free(sk2, X509_NAME_free);
6415
6416 return testresult;
6417}
6418#endif
6419
6420static int test_ca_names(int tst)
6421{
6422 int testresult = 1;
6423
6424#ifndef OPENSSL_NO_TLS1_2
6425 testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
6426#endif
6427#ifndef OPENSSL_NO_TLS1_3
6428 testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
6429#endif
6e46c065
MC
6430
6431 return testresult;
6432}
6433
a43ce58f
SL
6434
6435OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile\n")
6436
ad887416 6437int setup_tests(void)
2cb4b5f6 6438{
ad887416 6439 if (!TEST_ptr(cert = test_get_argument(0))
76fd7a1d
MC
6440 || !TEST_ptr(privkey = test_get_argument(1))
6441 || !TEST_ptr(srpvfile = test_get_argument(2))
6442 || !TEST_ptr(tmpfilename = test_get_argument(3)))
710756a9 6443 return 0;
2cb4b5f6 6444
f297e4ec
RS
6445 if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
6446#ifdef OPENSSL_NO_CRYPTO_MDEBUG
6447 TEST_error("not supported in this build");
6448 return 0;
6449#else
6450 int i, mcount, rcount, fcount;
6451
6452 for (i = 0; i < 4; i++)
6453 test_export_key_mat(i);
6454 CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
6455 test_printf_stdout("malloc %d realloc %d free %d\n",
6456 mcount, rcount, fcount);
6457 return 1;
6458#endif
6459 }
6460
5e9072ed
MC
6461#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_KTLS) \
6462 && !defined(OPENSSL_NO_SOCK)
2fab79af
BP
6463 ADD_TEST(test_ktls_no_txrx_client_no_txrx_server);
6464 ADD_TEST(test_ktls_no_rx_client_no_txrx_server);
6465 ADD_TEST(test_ktls_no_tx_client_no_txrx_server);
6466 ADD_TEST(test_ktls_client_no_txrx_server);
6467 ADD_TEST(test_ktls_no_txrx_client_no_rx_server);
6468 ADD_TEST(test_ktls_no_rx_client_no_rx_server);
6469 ADD_TEST(test_ktls_no_tx_client_no_rx_server);
6470 ADD_TEST(test_ktls_client_no_rx_server);
6471 ADD_TEST(test_ktls_no_txrx_client_no_tx_server);
6472 ADD_TEST(test_ktls_no_rx_client_no_tx_server);
6473 ADD_TEST(test_ktls_no_tx_client_no_tx_server);
6474 ADD_TEST(test_ktls_client_no_tx_server);
6475 ADD_TEST(test_ktls_no_txrx_client_server);
6476 ADD_TEST(test_ktls_no_rx_client_server);
6477 ADD_TEST(test_ktls_no_tx_client_server);
fe5d9450 6478 ADD_TEST(test_ktls_client_server);
7c3a7561 6479 ADD_TEST(test_ktls_sendfile);
fe5d9450 6480#endif
84d5549e 6481 ADD_TEST(test_large_message_tls);
7856332e 6482 ADD_TEST(test_large_message_tls_read_ahead);
55386bef 6483#ifndef OPENSSL_NO_DTLS
84d5549e 6484 ADD_TEST(test_large_message_dtls);
55386bef 6485#endif
8f8c11d8 6486#ifndef OPENSSL_NO_OCSP
c887104f 6487 ADD_TEST(test_tlsext_status_type);
8f8c11d8 6488#endif
eaa776da
MC
6489 ADD_TEST(test_session_with_only_int_cache);
6490 ADD_TEST(test_session_with_only_ext_cache);
6491 ADD_TEST(test_session_with_both_cache);
36ff232c 6492#ifndef OPENSSL_NO_TLS1_3
04d7814a
MC
6493 ADD_ALL_TESTS(test_stateful_tickets, 3);
6494 ADD_ALL_TESTS(test_stateless_tickets, 3);
8614a4eb 6495 ADD_TEST(test_psk_tickets);
36ff232c 6496#endif
7fb4c820 6497 ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
9a716987
MC
6498 ADD_TEST(test_ssl_bio_pop_next_bio);
6499 ADD_TEST(test_ssl_bio_pop_ssl_bio);
6500 ADD_TEST(test_ssl_bio_change_rbio);
6501 ADD_TEST(test_ssl_bio_change_wbio);
c423ecaa 6502#if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
f1b25aae 6503 ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
6acdd3e5 6504 ADD_TEST(test_keylog);
c423ecaa 6505#endif
6acdd3e5
CB
6506#ifndef OPENSSL_NO_TLS1_3
6507 ADD_TEST(test_keylog_no_master_key);
6508#endif
e9ee6536 6509#ifndef OPENSSL_NO_TLS1_2
a9c0d8be 6510 ADD_TEST(test_client_hello_cb);
088dfa13 6511 ADD_TEST(test_no_ems);
5f982038
MC
6512#endif
6513#ifndef OPENSSL_NO_TLS1_3
02a3ed5a 6514 ADD_ALL_TESTS(test_early_data_read_write, 3);
78fb5374
MC
6515 /*
6516 * We don't do replay tests for external PSK. Replay protection isn't used
6517 * in that scenario.
6518 */
6519 ADD_ALL_TESTS(test_early_data_replay, 2);
02a3ed5a
MC
6520 ADD_ALL_TESTS(test_early_data_skip, 3);
6521 ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
0efa0ba4 6522 ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
0d1b7789 6523 ADD_ALL_TESTS(test_early_data_skip_abort, 3);
02a3ed5a 6524 ADD_ALL_TESTS(test_early_data_not_sent, 3);
57dee9bb 6525 ADD_ALL_TESTS(test_early_data_psk, 8);
02a3ed5a 6526 ADD_ALL_TESTS(test_early_data_not_expected, 3);
5f982038 6527# ifndef OPENSSL_NO_TLS1_2
02a3ed5a 6528 ADD_ALL_TESTS(test_early_data_tls1_2, 3);
5f982038 6529# endif
e9ee6536 6530#endif
a273157a 6531#ifndef OPENSSL_NO_TLS1_3
034cb87b 6532 ADD_ALL_TESTS(test_set_ciphersuite, 10);
ca0413ae 6533 ADD_TEST(test_ciphersuite_change);
c2b290c3
MC
6534#ifdef OPENSSL_NO_PSK
6535 ADD_ALL_TESTS(test_tls13_psk, 1);
6536#else
0d8da779 6537 ADD_ALL_TESTS(test_tls13_psk, 4);
c2b290c3 6538#endif /* OPENSSL_NO_PSK */
bb01ef3f 6539 ADD_ALL_TESTS(test_custom_exts, 5);
c7b8ff25 6540 ADD_TEST(test_stateless);
9d75dce3 6541 ADD_TEST(test_pha_key_update);
a273157a 6542#else
bb01ef3f 6543 ADD_ALL_TESTS(test_custom_exts, 3);
a273157a 6544#endif
16afd71c 6545 ADD_ALL_TESTS(test_serverinfo, 8);
0fb2815b 6546 ADD_ALL_TESTS(test_export_key_mat, 6);
b38ede80
TT
6547#ifndef OPENSSL_NO_TLS1_3
6548 ADD_ALL_TESTS(test_export_key_mat_early, 3);
3409a5ff 6549 ADD_TEST(test_key_update);
a77b4dba 6550 ADD_ALL_TESTS(test_key_update_in_write, 2);
b38ede80 6551#endif
e11b6aa4 6552 ADD_ALL_TESTS(test_ssl_clear, 2);
cf72c757 6553 ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
76fd7a1d
MC
6554#if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
6555 ADD_ALL_TESTS(test_srp, 6);
6556#endif
5718fe45 6557 ADD_ALL_TESTS(test_info_callback, 6);
4a432af8 6558 ADD_ALL_TESTS(test_ssl_pending, 2);
e401389a 6559 ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
61fb5923 6560 ADD_ALL_TESTS(test_ticket_callbacks, 12);
57d7b988 6561 ADD_ALL_TESTS(test_shutdown, 7);
cd6fe29f 6562 ADD_ALL_TESTS(test_cert_cb, 3);
6e46c065 6563 ADD_ALL_TESTS(test_client_cert_cb, 2);
fb8c8359 6564 ADD_ALL_TESTS(test_ca_names, 3);
ad887416
P
6565 return 1;
6566}
2cb4b5f6 6567
ad887416
P
6568void cleanup_tests(void)
6569{
fa454945 6570 bio_s_mempacket_test_free();
a77b4dba 6571 bio_s_always_retry_free();
2cb4b5f6 6572}