]>
Commit | Line | Data |
---|---|---|
2cb4b5f6 | 1 | /* |
33388b44 | 2 | * Copyright 2016-2020 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 | ||
a76ce286 P |
10 | /* |
11 | * We need access to the deprecated low level HMAC APIs for legacy purposes | |
12 | * when the deprecated calls are not hidden | |
13 | */ | |
14 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
15 | # define OPENSSL_SUPPRESS_DEPRECATED | |
16 | #endif | |
17 | ||
7c3a7561 | 18 | #include <stdio.h> |
ba881d3b MC |
19 | #include <string.h> |
20 | ||
2cb4b5f6 MC |
21 | #include <openssl/opensslconf.h> |
22 | #include <openssl/bio.h> | |
23 | #include <openssl/crypto.h> | |
24 | #include <openssl/ssl.h> | |
ba881d3b | 25 | #include <openssl/ocsp.h> |
76fd7a1d MC |
26 | #include <openssl/srp.h> |
27 | #include <openssl/txt_db.h> | |
d0191fe0 | 28 | #include <openssl/aes.h> |
7c3a7561 | 29 | #include <openssl/rand.h> |
a76ce286 | 30 | #include <openssl/core_names.h> |
23c48d94 | 31 | #include <openssl/core_dispatch.h> |
5e30f2fd | 32 | #include <openssl/provider.h> |
2cb4b5f6 MC |
33 | |
34 | #include "ssltestlib.h" | |
c887104f | 35 | #include "testutil.h" |
f297e4ec | 36 | #include "testutil/output.h" |
176db6dc | 37 | #include "internal/nelem.h" |
fe5d9450 | 38 | #include "internal/ktls.h" |
706457b7 | 39 | #include "../ssl/ssl_local.h" |
2cb4b5f6 | 40 | |
b3842539 MC |
41 | /* Defined in filterprov.c */ |
42 | OSSL_provider_init_fn filter_provider_init; | |
43 | int filter_provider_set_filter(int operation, const char *name); | |
44 | ||
0c13cdf8 MC |
45 | /* Defined in tls-provider.c */ |
46 | int tls_provider_init(const OSSL_CORE_HANDLE *handle, | |
47 | const OSSL_DISPATCH *in, | |
48 | const OSSL_DISPATCH **out, | |
49 | void **provctx); | |
852c2ed2 RS |
50 | DEFINE_STACK_OF(OCSP_RESPID) |
51 | DEFINE_STACK_OF(X509) | |
52 | DEFINE_STACK_OF(X509_NAME) | |
53 | ||
5e30f2fd MC |
54 | static OPENSSL_CTX *libctx = NULL; |
55 | static OSSL_PROVIDER *defctxnull = NULL; | |
56 | ||
8614a4eb MC |
57 | #ifndef OPENSSL_NO_TLS1_3 |
58 | ||
59 | static SSL_SESSION *clientpsk = NULL; | |
60 | static SSL_SESSION *serverpsk = NULL; | |
61 | static const char *pskid = "Identity"; | |
62 | static const char *srvid; | |
63 | ||
64 | static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, | |
65 | size_t *idlen, SSL_SESSION **sess); | |
66 | static int find_session_cb(SSL *ssl, const unsigned char *identity, | |
67 | size_t identity_len, SSL_SESSION **sess); | |
68 | ||
69 | static int use_session_cb_cnt = 0; | |
70 | static int find_session_cb_cnt = 0; | |
71 | ||
72 | static SSL_SESSION *create_a_psk(SSL *ssl); | |
73 | #endif | |
74 | ||
1a2a3a42 | 75 | static char *certsdir = NULL; |
2cb4b5f6 MC |
76 | static char *cert = NULL; |
77 | static char *privkey = NULL; | |
b3842539 MC |
78 | static char *cert2 = NULL; |
79 | static char *privkey2 = NULL; | |
76fd7a1d MC |
80 | static char *srpvfile = NULL; |
81 | static char *tmpfilename = NULL; | |
2cb4b5f6 | 82 | |
4f6c7044 MC |
83 | static int is_fips = 0; |
84 | ||
cffe973c | 85 | #define LOG_BUFFER_SIZE 2048 |
6acdd3e5 | 86 | static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; |
710756a9 | 87 | static size_t server_log_buffer_index = 0; |
6acdd3e5 | 88 | static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0}; |
710756a9 | 89 | static size_t client_log_buffer_index = 0; |
6acdd3e5 CB |
90 | static int error_writing_log = 0; |
91 | ||
8f8c11d8 | 92 | #ifndef OPENSSL_NO_OCSP |
ba881d3b MC |
93 | static const unsigned char orespder[] = "Dummy OCSP Response"; |
94 | static int ocsp_server_called = 0; | |
95 | static int ocsp_client_called = 0; | |
96 | ||
97 | static int cdummyarg = 1; | |
98 | static X509 *ocspcert = NULL; | |
8f8c11d8 | 99 | #endif |
ba881d3b | 100 | |
84d5549e | 101 | #define NUM_EXTRA_CERTS 40 |
cf72c757 | 102 | #define CLIENT_VERSION_LEN 2 |
84d5549e | 103 | |
f1a5939f CB |
104 | /* |
105 | * This structure is used to validate that the correct number of log messages | |
106 | * of various types are emitted when emitting secret logs. | |
107 | */ | |
108 | struct sslapitest_log_counts { | |
109 | unsigned int rsa_key_exchange_count; | |
110 | unsigned int master_secret_count; | |
cffe973c | 111 | unsigned int client_early_secret_count; |
f1a5939f CB |
112 | unsigned int client_handshake_secret_count; |
113 | unsigned int server_handshake_secret_count; | |
114 | unsigned int client_application_secret_count; | |
115 | unsigned int server_application_secret_count; | |
01a2a654 | 116 | unsigned int early_exporter_secret_count; |
6329ce8f | 117 | unsigned int exporter_secret_count; |
f1a5939f CB |
118 | }; |
119 | ||
16afd71c MC |
120 | |
121 | static unsigned char serverinfov1[] = { | |
122 | 0xff, 0xff, /* Dummy extension type */ | |
123 | 0x00, 0x01, /* Extension length is 1 byte */ | |
124 | 0xff /* Dummy extension data */ | |
125 | }; | |
126 | ||
127 | static unsigned char serverinfov2[] = { | |
128 | 0x00, 0x00, 0x00, | |
129 | (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */ | |
130 | 0xff, 0xff, /* Dummy extension type */ | |
131 | 0x00, 0x01, /* Extension length is 1 byte */ | |
132 | 0xff /* Dummy extension data */ | |
133 | }; | |
134 | ||
104a733d MC |
135 | static int hostname_cb(SSL *s, int *al, void *arg) |
136 | { | |
137 | const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); | |
138 | ||
139 | if (hostname != NULL && (strcmp(hostname, "goodhost") == 0 | |
140 | || strcmp(hostname, "altgoodhost") == 0)) | |
141 | return SSL_TLSEXT_ERR_OK; | |
142 | ||
143 | return SSL_TLSEXT_ERR_NOACK; | |
144 | } | |
145 | ||
710756a9 RS |
146 | static void client_keylog_callback(const SSL *ssl, const char *line) |
147 | { | |
6acdd3e5 CB |
148 | int line_length = strlen(line); |
149 | ||
150 | /* If the log doesn't fit, error out. */ | |
710756a9 RS |
151 | if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) { |
152 | TEST_info("Client log too full"); | |
6acdd3e5 CB |
153 | error_writing_log = 1; |
154 | return; | |
155 | } | |
156 | ||
157 | strcat(client_log_buffer, line); | |
158 | client_log_buffer_index += line_length; | |
710756a9 | 159 | client_log_buffer[client_log_buffer_index++] = '\n'; |
6acdd3e5 CB |
160 | } |
161 | ||
710756a9 RS |
162 | static void server_keylog_callback(const SSL *ssl, const char *line) |
163 | { | |
6acdd3e5 CB |
164 | int line_length = strlen(line); |
165 | ||
166 | /* If the log doesn't fit, error out. */ | |
710756a9 | 167 | if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) { |
bdcacd93 | 168 | TEST_info("Server log too full"); |
6acdd3e5 CB |
169 | error_writing_log = 1; |
170 | return; | |
171 | } | |
172 | ||
173 | strcat(server_log_buffer, line); | |
174 | server_log_buffer_index += line_length; | |
710756a9 | 175 | server_log_buffer[server_log_buffer_index++] = '\n'; |
6acdd3e5 CB |
176 | } |
177 | ||
178 | static int compare_hex_encoded_buffer(const char *hex_encoded, | |
179 | size_t hex_length, | |
180 | const uint8_t *raw, | |
710756a9 RS |
181 | size_t raw_length) |
182 | { | |
183 | size_t i, j; | |
184 | char hexed[3]; | |
6acdd3e5 | 185 | |
710756a9 | 186 | if (!TEST_size_t_eq(raw_length * 2, hex_length)) |
6acdd3e5 | 187 | return 1; |
6acdd3e5 | 188 | |
710756a9 | 189 | for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) { |
6acdd3e5 | 190 | sprintf(hexed, "%02x", raw[i]); |
710756a9 RS |
191 | if (!TEST_int_eq(hexed[0], hex_encoded[j]) |
192 | || !TEST_int_eq(hexed[1], hex_encoded[j + 1])) | |
6acdd3e5 | 193 | return 1; |
6acdd3e5 CB |
194 | } |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
199 | static int test_keylog_output(char *buffer, const SSL *ssl, | |
f1a5939f | 200 | const SSL_SESSION *session, |
710756a9 RS |
201 | struct sslapitest_log_counts *expected) |
202 | { | |
6acdd3e5 CB |
203 | char *token = NULL; |
204 | unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0}; | |
205 | size_t client_random_size = SSL3_RANDOM_SIZE; | |
206 | unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0}; | |
207 | size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH; | |
f1a5939f CB |
208 | unsigned int rsa_key_exchange_count = 0; |
209 | unsigned int master_secret_count = 0; | |
cffe973c | 210 | unsigned int client_early_secret_count = 0; |
f1a5939f CB |
211 | unsigned int client_handshake_secret_count = 0; |
212 | unsigned int server_handshake_secret_count = 0; | |
213 | unsigned int client_application_secret_count = 0; | |
214 | unsigned int server_application_secret_count = 0; | |
01a2a654 | 215 | unsigned int early_exporter_secret_count = 0; |
6329ce8f | 216 | unsigned int exporter_secret_count = 0; |
6acdd3e5 | 217 | |
710756a9 RS |
218 | for (token = strtok(buffer, " \n"); token != NULL; |
219 | token = strtok(NULL, " \n")) { | |
6acdd3e5 | 220 | if (strcmp(token, "RSA") == 0) { |
f1a5939f CB |
221 | /* |
222 | * Premaster secret. Tokens should be: 16 ASCII bytes of | |
6acdd3e5 CB |
223 | * hex-encoded encrypted secret, then the hex-encoded pre-master |
224 | * secret. | |
225 | */ | |
710756a9 | 226 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 227 | return 0; |
710756a9 | 228 | if (!TEST_size_t_eq(strlen(token), 16)) |
f1a5939f | 229 | return 0; |
710756a9 | 230 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 231 | return 0; |
f1a5939f CB |
232 | /* |
233 | * We can't sensibly check the log because the premaster secret is | |
234 | * transient, and OpenSSL doesn't keep hold of it once the master | |
235 | * secret is generated. | |
236 | */ | |
237 | rsa_key_exchange_count++; | |
6acdd3e5 | 238 | } else if (strcmp(token, "CLIENT_RANDOM") == 0) { |
f1a5939f CB |
239 | /* |
240 | * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded | |
6acdd3e5 CB |
241 | * client random, then the hex-encoded master secret. |
242 | */ | |
243 | client_random_size = SSL_get_client_random(ssl, | |
244 | actual_client_random, | |
245 | SSL3_RANDOM_SIZE); | |
710756a9 | 246 | if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) |
f1a5939f | 247 | return 0; |
6acdd3e5 | 248 | |
710756a9 | 249 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 250 | return 0; |
710756a9 | 251 | if (!TEST_size_t_eq(strlen(token), 64)) |
f1a5939f | 252 | return 0; |
710756a9 RS |
253 | if (!TEST_false(compare_hex_encoded_buffer(token, 64, |
254 | actual_client_random, | |
255 | client_random_size))) | |
f1a5939f | 256 | return 0; |
6acdd3e5 | 257 | |
710756a9 | 258 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 259 | return 0; |
6acdd3e5 CB |
260 | master_key_size = SSL_SESSION_get_master_key(session, |
261 | actual_master_key, | |
262 | master_key_size); | |
710756a9 | 263 | if (!TEST_size_t_ne(master_key_size, 0)) |
f1a5939f | 264 | return 0; |
710756a9 RS |
265 | if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token), |
266 | actual_master_key, | |
267 | master_key_size))) | |
f1a5939f | 268 | return 0; |
f1a5939f | 269 | master_secret_count++; |
cffe973c PW |
270 | } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0 |
271 | || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0 | |
710756a9 RS |
272 | || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0 |
273 | || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0 | |
6329ce8f | 274 | || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0 |
01a2a654 | 275 | || strcmp(token, "EARLY_EXPORTER_SECRET") == 0 |
6329ce8f | 276 | || strcmp(token, "EXPORTER_SECRET") == 0) { |
f1a5939f CB |
277 | /* |
278 | * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded | |
279 | * client random, and then the hex-encoded secret. In this case, | |
280 | * we treat all of these secrets identically and then just | |
281 | * distinguish between them when counting what we saw. | |
282 | */ | |
cffe973c PW |
283 | if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0) |
284 | client_early_secret_count++; | |
285 | else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0) | |
f1a5939f CB |
286 | client_handshake_secret_count++; |
287 | else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0) | |
288 | server_handshake_secret_count++; | |
289 | else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0) | |
290 | client_application_secret_count++; | |
291 | else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) | |
292 | server_application_secret_count++; | |
01a2a654 PW |
293 | else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0) |
294 | early_exporter_secret_count++; | |
6329ce8f PW |
295 | else if (strcmp(token, "EXPORTER_SECRET") == 0) |
296 | exporter_secret_count++; | |
f1a5939f CB |
297 | |
298 | client_random_size = SSL_get_client_random(ssl, | |
299 | actual_client_random, | |
300 | SSL3_RANDOM_SIZE); | |
710756a9 | 301 | if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE)) |
f1a5939f | 302 | return 0; |
f1a5939f | 303 | |
710756a9 | 304 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 305 | return 0; |
710756a9 | 306 | if (!TEST_size_t_eq(strlen(token), 64)) |
f1a5939f | 307 | return 0; |
710756a9 RS |
308 | if (!TEST_false(compare_hex_encoded_buffer(token, 64, |
309 | actual_client_random, | |
310 | client_random_size))) | |
f1a5939f | 311 | return 0; |
6acdd3e5 | 312 | |
710756a9 | 313 | if (!TEST_ptr(token = strtok(NULL, " \n"))) |
f1a5939f | 314 | return 0; |
f1a5939f CB |
315 | |
316 | /* | |
317 | * TODO(TLS1.3): test that application traffic secrets are what | |
318 | * we expect */ | |
6acdd3e5 | 319 | } else { |
710756a9 | 320 | TEST_info("Unexpected token %s\n", token); |
f1a5939f | 321 | return 0; |
6acdd3e5 | 322 | } |
6acdd3e5 CB |
323 | } |
324 | ||
710756a9 RS |
325 | /* Got what we expected? */ |
326 | if (!TEST_size_t_eq(rsa_key_exchange_count, | |
327 | expected->rsa_key_exchange_count) | |
328 | || !TEST_size_t_eq(master_secret_count, | |
329 | expected->master_secret_count) | |
cffe973c PW |
330 | || !TEST_size_t_eq(client_early_secret_count, |
331 | expected->client_early_secret_count) | |
710756a9 RS |
332 | || !TEST_size_t_eq(client_handshake_secret_count, |
333 | expected->client_handshake_secret_count) | |
334 | || !TEST_size_t_eq(server_handshake_secret_count, | |
335 | expected->server_handshake_secret_count) | |
336 | || !TEST_size_t_eq(client_application_secret_count, | |
337 | expected->client_application_secret_count) | |
338 | || !TEST_size_t_eq(server_application_secret_count, | |
6329ce8f | 339 | expected->server_application_secret_count) |
01a2a654 PW |
340 | || !TEST_size_t_eq(early_exporter_secret_count, |
341 | expected->early_exporter_secret_count) | |
6329ce8f PW |
342 | || !TEST_size_t_eq(exporter_secret_count, |
343 | expected->exporter_secret_count)) | |
710756a9 RS |
344 | return 0; |
345 | return 1; | |
6acdd3e5 CB |
346 | } |
347 | ||
c423ecaa | 348 | #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) |
710756a9 RS |
349 | static int test_keylog(void) |
350 | { | |
6acdd3e5 CB |
351 | SSL_CTX *cctx = NULL, *sctx = NULL; |
352 | SSL *clientssl = NULL, *serverssl = NULL; | |
353 | int testresult = 0; | |
6fc1e624 | 354 | struct sslapitest_log_counts expected; |
6acdd3e5 CB |
355 | |
356 | /* Clean up logging space */ | |
6fc1e624 | 357 | memset(&expected, 0, sizeof(expected)); |
710756a9 RS |
358 | memset(client_log_buffer, 0, sizeof(client_log_buffer)); |
359 | memset(server_log_buffer, 0, sizeof(server_log_buffer)); | |
6acdd3e5 CB |
360 | client_log_buffer_index = 0; |
361 | server_log_buffer_index = 0; | |
362 | error_writing_log = 0; | |
363 | ||
5e30f2fd | 364 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
710756a9 | 365 | TLS_client_method(), |
5c587fb6 | 366 | TLS1_VERSION, 0, |
710756a9 | 367 | &sctx, &cctx, cert, privkey))) |
6acdd3e5 | 368 | return 0; |
6acdd3e5 CB |
369 | |
370 | /* We cannot log the master secret for TLSv1.3, so we should forbid it. */ | |
371 | SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3); | |
372 | SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3); | |
373 | ||
f0deb4d3 | 374 | /* We also want to ensure that we use RSA-based key exchange. */ |
710756a9 | 375 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA"))) |
f0deb4d3 | 376 | goto end; |
f0deb4d3 | 377 | |
cf10df81 RS |
378 | if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL) |
379 | || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL)) | |
6acdd3e5 | 380 | goto end; |
6acdd3e5 | 381 | SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); |
cf10df81 RS |
382 | if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) |
383 | == client_keylog_callback)) | |
6acdd3e5 | 384 | goto end; |
710756a9 | 385 | SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); |
cf10df81 RS |
386 | if (!TEST_true(SSL_CTX_get_keylog_callback(sctx) |
387 | == server_keylog_callback)) | |
6acdd3e5 | 388 | goto end; |
6acdd3e5 | 389 | |
710756a9 RS |
390 | /* Now do a handshake and check that the logs have been written to. */ |
391 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
392 | &clientssl, NULL, NULL)) | |
393 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
394 | SSL_ERROR_NONE)) | |
395 | || !TEST_false(error_writing_log) | |
396 | || !TEST_int_gt(client_log_buffer_index, 0) | |
397 | || !TEST_int_gt(server_log_buffer_index, 0)) | |
6acdd3e5 | 398 | goto end; |
6acdd3e5 | 399 | |
f1a5939f CB |
400 | /* |
401 | * Now we want to test that our output data was vaguely sensible. We | |
6acdd3e5 | 402 | * do that by using strtok and confirming that we have more or less the |
f1a5939f CB |
403 | * data we expect. For both client and server, we expect to see one master |
404 | * secret. The client should also see a RSA key exchange. | |
6acdd3e5 | 405 | */ |
f1a5939f CB |
406 | expected.rsa_key_exchange_count = 1; |
407 | expected.master_secret_count = 1; | |
710756a9 RS |
408 | if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, |
409 | SSL_get_session(clientssl), &expected))) | |
6acdd3e5 | 410 | goto end; |
f1a5939f CB |
411 | |
412 | expected.rsa_key_exchange_count = 0; | |
710756a9 RS |
413 | if (!TEST_true(test_keylog_output(server_log_buffer, serverssl, |
414 | SSL_get_session(serverssl), &expected))) | |
6acdd3e5 | 415 | goto end; |
6acdd3e5 CB |
416 | |
417 | testresult = 1; | |
418 | ||
419 | end: | |
420 | SSL_free(serverssl); | |
421 | SSL_free(clientssl); | |
422 | SSL_CTX_free(sctx); | |
423 | SSL_CTX_free(cctx); | |
424 | ||
425 | return testresult; | |
426 | } | |
c423ecaa | 427 | #endif |
6acdd3e5 CB |
428 | |
429 | #ifndef OPENSSL_NO_TLS1_3 | |
710756a9 RS |
430 | static int test_keylog_no_master_key(void) |
431 | { | |
6acdd3e5 CB |
432 | SSL_CTX *cctx = NULL, *sctx = NULL; |
433 | SSL *clientssl = NULL, *serverssl = NULL; | |
cffe973c | 434 | SSL_SESSION *sess = NULL; |
6acdd3e5 | 435 | int testresult = 0; |
6fc1e624 | 436 | struct sslapitest_log_counts expected; |
cffe973c PW |
437 | unsigned char buf[1]; |
438 | size_t readbytes, written; | |
6acdd3e5 CB |
439 | |
440 | /* Clean up logging space */ | |
6fc1e624 | 441 | memset(&expected, 0, sizeof(expected)); |
710756a9 RS |
442 | memset(client_log_buffer, 0, sizeof(client_log_buffer)); |
443 | memset(server_log_buffer, 0, sizeof(server_log_buffer)); | |
6acdd3e5 CB |
444 | client_log_buffer_index = 0; |
445 | server_log_buffer_index = 0; | |
446 | error_writing_log = 0; | |
447 | ||
5e30f2fd MC |
448 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
449 | TLS_client_method(), TLS1_VERSION, 0, | |
cffe973c PW |
450 | &sctx, &cctx, cert, privkey)) |
451 | || !TEST_true(SSL_CTX_set_max_early_data(sctx, | |
cffe973c | 452 | SSL3_RT_MAX_PLAIN_LENGTH))) |
6acdd3e5 | 453 | return 0; |
6acdd3e5 | 454 | |
cf10df81 RS |
455 | if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL) |
456 | || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL)) | |
6acdd3e5 | 457 | goto end; |
6acdd3e5 CB |
458 | |
459 | SSL_CTX_set_keylog_callback(cctx, client_keylog_callback); | |
cf10df81 RS |
460 | if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) |
461 | == client_keylog_callback)) | |
6acdd3e5 | 462 | goto end; |
6acdd3e5 | 463 | |
710756a9 | 464 | SSL_CTX_set_keylog_callback(sctx, server_keylog_callback); |
cf10df81 RS |
465 | if (!TEST_true(SSL_CTX_get_keylog_callback(sctx) |
466 | == server_keylog_callback)) | |
6acdd3e5 | 467 | goto end; |
6acdd3e5 | 468 | |
710756a9 RS |
469 | /* Now do a handshake and check that the logs have been written to. */ |
470 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
471 | &clientssl, NULL, NULL)) | |
472 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
473 | SSL_ERROR_NONE)) | |
474 | || !TEST_false(error_writing_log)) | |
6acdd3e5 | 475 | goto end; |
6acdd3e5 | 476 | |
f1a5939f CB |
477 | /* |
478 | * Now we want to test that our output data was vaguely sensible. For this | |
69687aa8 | 479 | * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for |
f1a5939f | 480 | * TLSv1.3, but we do expect both client and server to emit keys. |
6acdd3e5 | 481 | */ |
f1a5939f CB |
482 | expected.client_handshake_secret_count = 1; |
483 | expected.server_handshake_secret_count = 1; | |
484 | expected.client_application_secret_count = 1; | |
485 | expected.server_application_secret_count = 1; | |
6329ce8f | 486 | expected.exporter_secret_count = 1; |
710756a9 RS |
487 | if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, |
488 | SSL_get_session(clientssl), &expected)) | |
489 | || !TEST_true(test_keylog_output(server_log_buffer, serverssl, | |
490 | SSL_get_session(serverssl), | |
491 | &expected))) | |
6acdd3e5 | 492 | goto end; |
6acdd3e5 | 493 | |
cffe973c PW |
494 | /* Terminate old session and resume with early data. */ |
495 | sess = SSL_get1_session(clientssl); | |
496 | SSL_shutdown(clientssl); | |
497 | SSL_shutdown(serverssl); | |
498 | SSL_free(serverssl); | |
499 | SSL_free(clientssl); | |
500 | serverssl = clientssl = NULL; | |
501 | ||
502 | /* Reset key log */ | |
503 | memset(client_log_buffer, 0, sizeof(client_log_buffer)); | |
504 | memset(server_log_buffer, 0, sizeof(server_log_buffer)); | |
505 | client_log_buffer_index = 0; | |
506 | server_log_buffer_index = 0; | |
507 | ||
508 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
509 | &clientssl, NULL, NULL)) | |
510 | || !TEST_true(SSL_set_session(clientssl, sess)) | |
511 | /* Here writing 0 length early data is enough. */ | |
512 | || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written)) | |
513 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
514 | &readbytes), | |
515 | SSL_READ_EARLY_DATA_ERROR) | |
516 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
517 | SSL_EARLY_DATA_ACCEPTED) | |
518 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
519 | SSL_ERROR_NONE)) | |
520 | || !TEST_true(SSL_session_reused(clientssl))) | |
521 | goto end; | |
522 | ||
523 | /* In addition to the previous entries, expect early secrets. */ | |
524 | expected.client_early_secret_count = 1; | |
01a2a654 | 525 | expected.early_exporter_secret_count = 1; |
cffe973c PW |
526 | if (!TEST_true(test_keylog_output(client_log_buffer, clientssl, |
527 | SSL_get_session(clientssl), &expected)) | |
528 | || !TEST_true(test_keylog_output(server_log_buffer, serverssl, | |
529 | SSL_get_session(serverssl), | |
530 | &expected))) | |
531 | goto end; | |
532 | ||
6acdd3e5 CB |
533 | testresult = 1; |
534 | ||
535 | end: | |
cffe973c | 536 | SSL_SESSION_free(sess); |
6acdd3e5 CB |
537 | SSL_free(serverssl); |
538 | SSL_free(clientssl); | |
539 | SSL_CTX_free(sctx); | |
540 | SSL_CTX_free(cctx); | |
541 | ||
542 | return testresult; | |
543 | } | |
544 | #endif | |
545 | ||
e9ee6536 | 546 | #ifndef OPENSSL_NO_TLS1_2 |
a9c0d8be | 547 | static int full_client_hello_callback(SSL *s, int *al, void *arg) |
2afaee51 BK |
548 | { |
549 | int *ctr = arg; | |
550 | const unsigned char *p; | |
8ea404fb | 551 | int *exts; |
2afaee51 BK |
552 | /* We only configure two ciphers, but the SCSV is added automatically. */ |
553 | #ifdef OPENSSL_NO_EC | |
554 | const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff}; | |
555 | #else | |
556 | const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0, | |
557 | 0x2c, 0x00, 0xff}; | |
558 | #endif | |
8ea404fb BK |
559 | const int expected_extensions[] = { |
560 | #ifndef OPENSSL_NO_EC | |
561 | 11, 10, | |
562 | #endif | |
10ed1b72 | 563 | 35, 22, 23, 13}; |
2afaee51 BK |
564 | size_t len; |
565 | ||
566 | /* Make sure we can defer processing and get called back. */ | |
567 | if ((*ctr)++ == 0) | |
f1b97da1 | 568 | return SSL_CLIENT_HELLO_RETRY; |
2afaee51 | 569 | |
a9c0d8be | 570 | len = SSL_client_hello_get0_ciphers(s, &p); |
710756a9 | 571 | if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers)) |
a9c0d8be DB |
572 | || !TEST_size_t_eq( |
573 | SSL_client_hello_get0_compression_methods(s, &p), 1) | |
710756a9 | 574 | || !TEST_int_eq(*p, 0)) |
f1b97da1 | 575 | return SSL_CLIENT_HELLO_ERROR; |
a9c0d8be | 576 | if (!SSL_client_hello_get1_extensions_present(s, &exts, &len)) |
f1b97da1 | 577 | return SSL_CLIENT_HELLO_ERROR; |
8ea404fb BK |
578 | if (len != OSSL_NELEM(expected_extensions) || |
579 | memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) { | |
a9c0d8be | 580 | printf("ClientHello callback expected extensions mismatch\n"); |
8ea404fb | 581 | OPENSSL_free(exts); |
f1b97da1 | 582 | return SSL_CLIENT_HELLO_ERROR; |
8ea404fb BK |
583 | } |
584 | OPENSSL_free(exts); | |
f1b97da1 | 585 | return SSL_CLIENT_HELLO_SUCCESS; |
2afaee51 BK |
586 | } |
587 | ||
a9c0d8be | 588 | static int test_client_hello_cb(void) |
710756a9 | 589 | { |
2afaee51 BK |
590 | SSL_CTX *cctx = NULL, *sctx = NULL; |
591 | SSL *clientssl = NULL, *serverssl = NULL; | |
592 | int testctr = 0, testresult = 0; | |
593 | ||
5e30f2fd MC |
594 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
595 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 596 | &sctx, &cctx, cert, privkey))) |
2afaee51 | 597 | goto end; |
a9c0d8be | 598 | SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr); |
710756a9 | 599 | |
2afaee51 BK |
600 | /* The gimpy cipher list we configure can't do TLS 1.3. */ |
601 | SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); | |
2afaee51 | 602 | |
710756a9 RS |
603 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, |
604 | "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384")) | |
605 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
606 | &clientssl, NULL, NULL)) | |
607 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
a9c0d8be | 608 | SSL_ERROR_WANT_CLIENT_HELLO_CB)) |
710756a9 RS |
609 | /* |
610 | * Passing a -1 literal is a hack since | |
611 | * the real value was lost. | |
612 | * */ | |
a9c0d8be DB |
613 | || !TEST_int_eq(SSL_get_error(serverssl, -1), |
614 | SSL_ERROR_WANT_CLIENT_HELLO_CB) | |
710756a9 RS |
615 | || !TEST_true(create_ssl_connection(serverssl, clientssl, |
616 | SSL_ERROR_NONE))) | |
2afaee51 | 617 | goto end; |
2afaee51 BK |
618 | |
619 | testresult = 1; | |
620 | ||
088dfa13 TS |
621 | end: |
622 | SSL_free(serverssl); | |
623 | SSL_free(clientssl); | |
624 | SSL_CTX_free(sctx); | |
625 | SSL_CTX_free(cctx); | |
626 | ||
627 | return testresult; | |
628 | } | |
629 | ||
630 | static int test_no_ems(void) | |
631 | { | |
632 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
633 | SSL *clientssl = NULL, *serverssl = NULL; | |
634 | int testresult = 0; | |
635 | ||
5e30f2fd | 636 | if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), |
088dfa13 TS |
637 | TLS1_VERSION, TLS1_2_VERSION, |
638 | &sctx, &cctx, cert, privkey)) { | |
639 | printf("Unable to create SSL_CTX pair\n"); | |
640 | goto end; | |
641 | } | |
642 | ||
643 | SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET); | |
644 | ||
645 | if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) { | |
646 | printf("Unable to create SSL objects\n"); | |
647 | goto end; | |
648 | } | |
649 | ||
650 | if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) { | |
651 | printf("Creating SSL connection failed\n"); | |
652 | goto end; | |
653 | } | |
654 | ||
655 | if (SSL_get_extms_support(serverssl)) { | |
656 | printf("Server reports Extended Master Secret support\n"); | |
657 | goto end; | |
658 | } | |
659 | ||
660 | if (SSL_get_extms_support(clientssl)) { | |
661 | printf("Client reports Extended Master Secret support\n"); | |
662 | goto end; | |
663 | } | |
664 | testresult = 1; | |
665 | ||
2afaee51 BK |
666 | end: |
667 | SSL_free(serverssl); | |
668 | SSL_free(clientssl); | |
669 | SSL_CTX_free(sctx); | |
670 | SSL_CTX_free(cctx); | |
671 | ||
672 | return testresult; | |
673 | } | |
674 | ||
3cd14e5e BK |
675 | /* |
676 | * Very focused test to exercise a single case in the server-side state | |
677 | * machine, when the ChangeCipherState message needs to actually change | |
678 | * from one cipher to a different cipher (i.e., not changing from null | |
b3e6d666 | 679 | * encryption to real encryption). |
3cd14e5e BK |
680 | */ |
681 | static int test_ccs_change_cipher(void) | |
682 | { | |
683 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
684 | SSL *clientssl = NULL, *serverssl = NULL; | |
685 | SSL_SESSION *sess = NULL, *sesspre, *sesspost; | |
686 | int testresult = 0; | |
687 | int i; | |
688 | unsigned char buf; | |
689 | size_t readbytes; | |
690 | ||
691 | /* | |
692 | * Create a conection so we can resume and potentially (but not) use | |
693 | * a different cipher in the second connection. | |
694 | */ | |
5e30f2fd | 695 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
3cd14e5e BK |
696 | TLS_client_method(), |
697 | TLS1_VERSION, TLS1_2_VERSION, | |
698 | &sctx, &cctx, cert, privkey)) | |
699 | || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)) | |
700 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
701 | NULL, NULL)) | |
702 | || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256")) | |
703 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
704 | SSL_ERROR_NONE)) | |
705 | || !TEST_ptr(sesspre = SSL_get0_session(serverssl)) | |
706 | || !TEST_ptr(sess = SSL_get1_session(clientssl))) | |
707 | goto end; | |
708 | ||
709 | shutdown_ssl_connection(serverssl, clientssl); | |
710 | serverssl = clientssl = NULL; | |
711 | ||
712 | /* Resume, preferring a different cipher. Our server will force the | |
713 | * same cipher to be used as the initial handshake. */ | |
714 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
715 | NULL, NULL)) | |
716 | || !TEST_true(SSL_set_session(clientssl, sess)) | |
717 | || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256")) | |
718 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
719 | SSL_ERROR_NONE)) | |
720 | || !TEST_true(SSL_session_reused(clientssl)) | |
721 | || !TEST_true(SSL_session_reused(serverssl)) | |
722 | || !TEST_ptr(sesspost = SSL_get0_session(serverssl)) | |
723 | || !TEST_ptr_eq(sesspre, sesspost) | |
724 | || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256, | |
725 | SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl)))) | |
726 | goto end; | |
727 | shutdown_ssl_connection(serverssl, clientssl); | |
728 | serverssl = clientssl = NULL; | |
729 | ||
730 | /* | |
731 | * Now create a fresh connection and try to renegotiate a different | |
732 | * cipher on it. | |
733 | */ | |
b3e6d666 BK |
734 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
735 | NULL, NULL)) | |
3cd14e5e BK |
736 | || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256")) |
737 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
738 | SSL_ERROR_NONE)) | |
739 | || !TEST_ptr(sesspre = SSL_get0_session(serverssl)) | |
740 | || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")) | |
741 | || !TEST_true(SSL_renegotiate(clientssl)) | |
742 | || !TEST_true(SSL_renegotiate_pending(clientssl))) | |
743 | goto end; | |
744 | /* Actually drive the renegotiation. */ | |
745 | for (i = 0; i < 3; i++) { | |
746 | if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) { | |
747 | if (!TEST_ulong_eq(readbytes, 0)) | |
748 | goto end; | |
749 | } else if (!TEST_int_eq(SSL_get_error(clientssl, 0), | |
750 | SSL_ERROR_WANT_READ)) { | |
751 | goto end; | |
752 | } | |
753 | if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) { | |
754 | if (!TEST_ulong_eq(readbytes, 0)) | |
755 | goto end; | |
756 | } else if (!TEST_int_eq(SSL_get_error(serverssl, 0), | |
757 | SSL_ERROR_WANT_READ)) { | |
758 | goto end; | |
759 | } | |
760 | } | |
761 | /* sesspre and sesspost should be different since the cipher changed. */ | |
762 | if (!TEST_false(SSL_renegotiate_pending(clientssl)) | |
763 | || !TEST_false(SSL_session_reused(clientssl)) | |
764 | || !TEST_false(SSL_session_reused(serverssl)) | |
765 | || !TEST_ptr(sesspost = SSL_get0_session(serverssl)) | |
766 | || !TEST_ptr_ne(sesspre, sesspost) | |
767 | || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384, | |
768 | SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl)))) | |
769 | goto end; | |
770 | ||
771 | shutdown_ssl_connection(serverssl, clientssl); | |
772 | serverssl = clientssl = NULL; | |
773 | ||
774 | testresult = 1; | |
775 | ||
776 | end: | |
777 | SSL_free(serverssl); | |
778 | SSL_free(clientssl); | |
779 | SSL_CTX_free(sctx); | |
780 | SSL_CTX_free(cctx); | |
781 | SSL_SESSION_free(sess); | |
782 | ||
783 | return testresult; | |
784 | } | |
fb121631 | 785 | #endif |
3cd14e5e | 786 | |
84d5549e | 787 | static int execute_test_large_message(const SSL_METHOD *smeth, |
7d7f6834 RL |
788 | const SSL_METHOD *cmeth, |
789 | int min_version, int max_version, | |
790 | int read_ahead) | |
84d5549e MC |
791 | { |
792 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
793 | SSL *clientssl = NULL, *serverssl = NULL; | |
794 | int testresult = 0; | |
795 | int i; | |
710756a9 | 796 | BIO *certbio = NULL; |
84d5549e MC |
797 | X509 *chaincert = NULL; |
798 | int certlen; | |
799 | ||
710756a9 | 800 | if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))) |
84d5549e | 801 | goto end; |
84d5549e | 802 | chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL); |
fa454945 MC |
803 | BIO_free(certbio); |
804 | certbio = NULL; | |
710756a9 | 805 | if (!TEST_ptr(chaincert)) |
fa454945 | 806 | goto end; |
84d5549e | 807 | |
5e30f2fd MC |
808 | if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version, |
809 | max_version, &sctx, &cctx, cert, | |
810 | privkey))) | |
84d5549e | 811 | goto end; |
84d5549e | 812 | |
710756a9 | 813 | if (read_ahead) { |
7856332e MC |
814 | /* |
815 | * Test that read_ahead works correctly when dealing with large | |
816 | * records | |
817 | */ | |
818 | SSL_CTX_set_read_ahead(cctx, 1); | |
819 | } | |
820 | ||
84d5549e MC |
821 | /* |
822 | * We assume the supplied certificate is big enough so that if we add | |
823 | * NUM_EXTRA_CERTS it will make the overall message large enough. The | |
824 | * default buffer size is requested to be 16k, but due to the way BUF_MEM | |
710756a9 RS |
825 | * works, it ends up allocating a little over 21k (16 * 4/3). So, in this |
826 | * test we need to have a message larger than that. | |
84d5549e MC |
827 | */ |
828 | certlen = i2d_X509(chaincert, NULL); | |
710756a9 RS |
829 | OPENSSL_assert(certlen * NUM_EXTRA_CERTS > |
830 | (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3); | |
84d5549e | 831 | for (i = 0; i < NUM_EXTRA_CERTS; i++) { |
710756a9 | 832 | if (!X509_up_ref(chaincert)) |
84d5549e | 833 | goto end; |
84d5549e | 834 | if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) { |
84d5549e MC |
835 | X509_free(chaincert); |
836 | goto end; | |
837 | } | |
838 | } | |
839 | ||
710756a9 RS |
840 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
841 | NULL, NULL)) | |
842 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
843 | SSL_ERROR_NONE))) | |
84d5549e | 844 | goto end; |
84d5549e | 845 | |
4bf08600 MC |
846 | /* |
847 | * Calling SSL_clear() first is not required but this tests that SSL_clear() | |
742ccab3 | 848 | * doesn't leak. |
4bf08600 | 849 | */ |
710756a9 | 850 | if (!TEST_true(SSL_clear(serverssl))) |
4bf08600 | 851 | goto end; |
84d5549e | 852 | |
4bf08600 | 853 | testresult = 1; |
84d5549e MC |
854 | end: |
855 | X509_free(chaincert); | |
856 | SSL_free(serverssl); | |
857 | SSL_free(clientssl); | |
858 | SSL_CTX_free(sctx); | |
859 | SSL_CTX_free(cctx); | |
860 | ||
861 | return testresult; | |
862 | } | |
863 | ||
da4db160 VF |
864 | #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \ |
865 | !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)) | |
866 | #define TLS_CIPHER_MAX_REC_SEQ_SIZE 8 | |
fe5d9450 BP |
867 | /* sock must be connected */ |
868 | static int ktls_chk_platform(int sock) | |
869 | { | |
870 | if (!ktls_enable(sock)) | |
871 | return 0; | |
872 | return 1; | |
873 | } | |
874 | ||
da4db160 | 875 | static int ping_pong_query(SSL *clientssl, SSL *serverssl, int cfd, int sfd, int rec_seq_size) |
fe5d9450 BP |
876 | { |
877 | static char count = 1; | |
878 | unsigned char cbuf[16000] = {0}; | |
879 | unsigned char sbuf[16000]; | |
880 | size_t err = 0; | |
da4db160 VF |
881 | char crec_wseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE]; |
882 | char crec_wseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
883 | char crec_rseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
884 | char crec_rseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
885 | char srec_wseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
886 | char srec_wseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
887 | char srec_rseq_before[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
888 | char srec_rseq_after[TLS_CIPHER_MAX_REC_SEQ_SIZE]; | |
fe5d9450 BP |
889 | |
890 | cbuf[0] = count++; | |
da4db160 VF |
891 | memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence, rec_seq_size); |
892 | memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence, rec_seq_size); | |
893 | memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence, rec_seq_size); | |
894 | memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence, rec_seq_size); | |
fe5d9450 BP |
895 | |
896 | if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf))) | |
897 | goto end; | |
898 | ||
899 | while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) { | |
900 | if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) { | |
901 | goto end; | |
902 | } | |
903 | } | |
904 | ||
905 | if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf))) | |
906 | goto end; | |
907 | ||
908 | while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) { | |
909 | if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) { | |
910 | goto end; | |
911 | } | |
912 | } | |
913 | ||
da4db160 VF |
914 | memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence, rec_seq_size); |
915 | memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence, rec_seq_size); | |
916 | memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence, rec_seq_size); | |
917 | memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence, rec_seq_size); | |
fe5d9450 BP |
918 | |
919 | /* verify the payload */ | |
920 | if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf))) | |
921 | goto end; | |
922 | ||
923 | /* ktls is used then kernel sequences are used instead of OpenSSL sequences */ | |
924 | if (clientssl->mode & SSL_MODE_NO_KTLS_TX) { | |
da4db160 VF |
925 | if (!TEST_mem_ne(crec_wseq_before, rec_seq_size, |
926 | crec_wseq_after, rec_seq_size)) | |
fe5d9450 BP |
927 | goto end; |
928 | } else { | |
da4db160 VF |
929 | if (!TEST_mem_eq(crec_wseq_before, rec_seq_size, |
930 | crec_wseq_after, rec_seq_size)) | |
fe5d9450 BP |
931 | goto end; |
932 | } | |
933 | ||
934 | if (serverssl->mode & SSL_MODE_NO_KTLS_TX) { | |
da4db160 VF |
935 | if (!TEST_mem_ne(srec_wseq_before, rec_seq_size, |
936 | srec_wseq_after, rec_seq_size)) | |
fe5d9450 BP |
937 | goto end; |
938 | } else { | |
da4db160 VF |
939 | if (!TEST_mem_eq(srec_wseq_before, rec_seq_size, |
940 | srec_wseq_after, rec_seq_size)) | |
fe5d9450 BP |
941 | goto end; |
942 | } | |
943 | ||
2fab79af | 944 | if (clientssl->mode & SSL_MODE_NO_KTLS_RX) { |
da4db160 VF |
945 | if (!TEST_mem_ne(crec_rseq_before, rec_seq_size, |
946 | crec_rseq_after, rec_seq_size)) | |
2fab79af BP |
947 | goto end; |
948 | } else { | |
da4db160 VF |
949 | if (!TEST_mem_eq(crec_rseq_before, rec_seq_size, |
950 | crec_rseq_after, rec_seq_size)) | |
2fab79af BP |
951 | goto end; |
952 | } | |
953 | ||
954 | if (serverssl->mode & SSL_MODE_NO_KTLS_RX) { | |
da4db160 VF |
955 | if (!TEST_mem_ne(srec_rseq_before, rec_seq_size, |
956 | srec_rseq_after, rec_seq_size)) | |
2fab79af BP |
957 | goto end; |
958 | } else { | |
da4db160 VF |
959 | if (!TEST_mem_eq(srec_rseq_before, rec_seq_size, |
960 | srec_rseq_after, rec_seq_size)) | |
2fab79af BP |
961 | goto end; |
962 | } | |
fe5d9450 BP |
963 | |
964 | return 1; | |
965 | end: | |
966 | return 0; | |
967 | } | |
968 | ||
2fab79af | 969 | static int execute_test_ktls(int cis_ktls_tx, int cis_ktls_rx, |
da4db160 VF |
970 | int sis_ktls_tx, int sis_ktls_rx, |
971 | int tls_version, const char *cipher, | |
972 | int rec_seq_size) | |
fe5d9450 BP |
973 | { |
974 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
975 | SSL *clientssl = NULL, *serverssl = NULL; | |
976 | int testresult = 0; | |
977 | int cfd, sfd; | |
978 | ||
979 | if (!TEST_true(create_test_sockets(&cfd, &sfd))) | |
980 | goto end; | |
981 | ||
982 | /* Skip this test if the platform does not support ktls */ | |
983 | if (!ktls_chk_platform(cfd)) | |
984 | return 1; | |
985 | ||
986 | /* Create a session based on SHA-256 */ | |
5e30f2fd | 987 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
fe5d9450 | 988 | TLS_client_method(), |
da4db160 | 989 | tls_version, tls_version, |
fe5d9450 | 990 | &sctx, &cctx, cert, privkey)) |
da4db160 | 991 | || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher)) |
fe5d9450 BP |
992 | || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl, |
993 | &clientssl, sfd, cfd))) | |
994 | goto end; | |
995 | ||
996 | if (!cis_ktls_tx) { | |
997 | if (!TEST_true(SSL_set_mode(clientssl, SSL_MODE_NO_KTLS_TX))) | |
998 | goto end; | |
999 | } | |
1000 | ||
1001 | if (!sis_ktls_tx) { | |
1002 | if (!TEST_true(SSL_set_mode(serverssl, SSL_MODE_NO_KTLS_TX))) | |
1003 | goto end; | |
1004 | } | |
1005 | ||
2fab79af BP |
1006 | if (!cis_ktls_rx) { |
1007 | if (!TEST_true(SSL_set_mode(clientssl, SSL_MODE_NO_KTLS_RX))) | |
1008 | goto end; | |
1009 | } | |
1010 | ||
1011 | if (!sis_ktls_rx) { | |
1012 | if (!TEST_true(SSL_set_mode(serverssl, SSL_MODE_NO_KTLS_RX))) | |
1013 | goto end; | |
1014 | } | |
1015 | ||
fe5d9450 BP |
1016 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, |
1017 | SSL_ERROR_NONE))) | |
1018 | goto end; | |
1019 | ||
1020 | if (!cis_ktls_tx) { | |
1021 | if (!TEST_false(BIO_get_ktls_send(clientssl->wbio))) | |
1022 | goto end; | |
1023 | } else { | |
1024 | if (!TEST_true(BIO_get_ktls_send(clientssl->wbio))) | |
1025 | goto end; | |
1026 | } | |
1027 | ||
1028 | if (!sis_ktls_tx) { | |
1029 | if (!TEST_false(BIO_get_ktls_send(serverssl->wbio))) | |
1030 | goto end; | |
1031 | } else { | |
1032 | if (!TEST_true(BIO_get_ktls_send(serverssl->wbio))) | |
1033 | goto end; | |
1034 | } | |
1035 | ||
2fab79af BP |
1036 | if (!cis_ktls_rx) { |
1037 | if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio))) | |
1038 | goto end; | |
1039 | } else { | |
1040 | if (!TEST_true(BIO_get_ktls_recv(clientssl->rbio))) | |
1041 | goto end; | |
1042 | } | |
1043 | ||
1044 | if (!sis_ktls_rx) { | |
1045 | if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio))) | |
1046 | goto end; | |
1047 | } else { | |
1048 | if (!TEST_true(BIO_get_ktls_recv(serverssl->rbio))) | |
1049 | goto end; | |
1050 | } | |
1051 | ||
da4db160 VF |
1052 | if (!TEST_true(ping_pong_query(clientssl, serverssl, cfd, sfd, |
1053 | rec_seq_size))) | |
fe5d9450 BP |
1054 | goto end; |
1055 | ||
1056 | testresult = 1; | |
1057 | end: | |
1058 | if (clientssl) { | |
1059 | SSL_shutdown(clientssl); | |
1060 | SSL_free(clientssl); | |
1061 | } | |
1062 | if (serverssl) { | |
1063 | SSL_shutdown(serverssl); | |
1064 | SSL_free(serverssl); | |
1065 | } | |
1066 | SSL_CTX_free(sctx); | |
1067 | SSL_CTX_free(cctx); | |
1068 | serverssl = clientssl = NULL; | |
1069 | return testresult; | |
1070 | } | |
1071 | ||
7c3a7561 BP |
1072 | #define SENDFILE_SZ (16 * 4096) |
1073 | #define SENDFILE_CHUNK (4 * 4096) | |
1074 | #define min(a,b) ((a) > (b) ? (b) : (a)) | |
1075 | ||
da4db160 | 1076 | static int test_ktls_sendfile(int tls_version, const char *cipher) |
7c3a7561 BP |
1077 | { |
1078 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
1079 | SSL *clientssl = NULL, *serverssl = NULL; | |
1080 | unsigned char *buf, *buf_dst; | |
1081 | BIO *out = NULL, *in = NULL; | |
1082 | int cfd, sfd, ffd, err; | |
1083 | ssize_t chunk_size = 0; | |
1084 | off_t chunk_off = 0; | |
1085 | int testresult = 0; | |
1086 | FILE *ffdp; | |
1087 | ||
1088 | buf = OPENSSL_zalloc(SENDFILE_SZ); | |
1089 | buf_dst = OPENSSL_zalloc(SENDFILE_SZ); | |
1090 | if (!TEST_ptr(buf) || !TEST_ptr(buf_dst) | |
1091 | || !TEST_true(create_test_sockets(&cfd, &sfd))) | |
1092 | goto end; | |
1093 | ||
1094 | /* Skip this test if the platform does not support ktls */ | |
1095 | if (!ktls_chk_platform(sfd)) { | |
1096 | testresult = 1; | |
1097 | goto end; | |
1098 | } | |
1099 | ||
1100 | /* Create a session based on SHA-256 */ | |
5e30f2fd | 1101 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
7c3a7561 | 1102 | TLS_client_method(), |
da4db160 | 1103 | tls_version, tls_version, |
7c3a7561 | 1104 | &sctx, &cctx, cert, privkey)) |
da4db160 | 1105 | || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher)) |
7c3a7561 BP |
1106 | || !TEST_true(create_ssl_objects2(sctx, cctx, &serverssl, |
1107 | &clientssl, sfd, cfd))) | |
1108 | goto end; | |
1109 | ||
1110 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
1111 | SSL_ERROR_NONE)) | |
1112 | || !TEST_true(BIO_get_ktls_send(serverssl->wbio))) | |
1113 | goto end; | |
1114 | ||
1115 | RAND_bytes(buf, SENDFILE_SZ); | |
1116 | out = BIO_new_file(tmpfilename, "wb"); | |
1117 | if (!TEST_ptr(out)) | |
1118 | goto end; | |
1119 | ||
1120 | if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ) | |
1121 | goto end; | |
1122 | ||
1123 | BIO_free(out); | |
1124 | out = NULL; | |
1125 | in = BIO_new_file(tmpfilename, "rb"); | |
1126 | BIO_get_fp(in, &ffdp); | |
1127 | ffd = fileno(ffdp); | |
1128 | ||
1129 | while (chunk_off < SENDFILE_SZ) { | |
1130 | chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off); | |
1131 | while ((err = SSL_sendfile(serverssl, | |
1132 | ffd, | |
1133 | chunk_off, | |
1134 | chunk_size, | |
1135 | 0)) != chunk_size) { | |
1136 | if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE) | |
1137 | goto end; | |
1138 | } | |
1139 | while ((err = SSL_read(clientssl, | |
1140 | buf_dst + chunk_off, | |
1141 | chunk_size)) != chunk_size) { | |
1142 | if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) | |
1143 | goto end; | |
1144 | } | |
1145 | ||
1146 | /* verify the payload */ | |
1147 | if (!TEST_mem_eq(buf_dst + chunk_off, | |
1148 | chunk_size, | |
1149 | buf + chunk_off, | |
1150 | chunk_size)) | |
1151 | goto end; | |
1152 | ||
1153 | chunk_off += chunk_size; | |
1154 | } | |
1155 | ||
1156 | testresult = 1; | |
1157 | end: | |
1158 | if (clientssl) { | |
1159 | SSL_shutdown(clientssl); | |
1160 | SSL_free(clientssl); | |
1161 | } | |
1162 | if (serverssl) { | |
1163 | SSL_shutdown(serverssl); | |
1164 | SSL_free(serverssl); | |
1165 | } | |
1166 | SSL_CTX_free(sctx); | |
1167 | SSL_CTX_free(cctx); | |
1168 | serverssl = clientssl = NULL; | |
1169 | BIO_free(out); | |
1170 | BIO_free(in); | |
1171 | OPENSSL_free(buf); | |
1172 | OPENSSL_free(buf_dst); | |
1173 | return testresult; | |
1174 | } | |
1175 | ||
da4db160 | 1176 | static int test_ktls_no_txrx_client_no_txrx_server(int tlsver) |
2fab79af | 1177 | { |
da4db160 VF |
1178 | int testresult = 1; |
1179 | ||
1180 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1181 | testresult &= execute_test_ktls(0, 0, 0, 0, tlsver, | |
1182 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1183 | #endif | |
1184 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1185 | testresult &= execute_test_ktls(0, 0, 0, 0, tlsver, | |
1186 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1187 | #endif | |
1188 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1189 | testresult &= execute_test_ktls(0, 0, 0, 0, tlsver, | |
1190 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1191 | #endif | |
1192 | return testresult; | |
2fab79af BP |
1193 | } |
1194 | ||
da4db160 | 1195 | static int test_ktls_no_rx_client_no_txrx_server(int tlsver) |
2fab79af | 1196 | { |
da4db160 VF |
1197 | int testresult = 1; |
1198 | ||
1199 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1200 | testresult &= execute_test_ktls(1, 0, 0, 0, tlsver, | |
1201 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1202 | #endif | |
1203 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1204 | testresult &= execute_test_ktls(1, 0, 0, 0, tlsver, | |
1205 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1206 | #endif | |
1207 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1208 | testresult &= execute_test_ktls(1, 0, 0, 0, tlsver, | |
1209 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1210 | #endif | |
1211 | return testresult; | |
2fab79af BP |
1212 | } |
1213 | ||
da4db160 | 1214 | static int test_ktls_no_tx_client_no_txrx_server(int tlsver) |
2fab79af | 1215 | { |
da4db160 VF |
1216 | int testresult = 1; |
1217 | ||
1218 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1219 | testresult &= execute_test_ktls(0, 1, 0, 0, tlsver, | |
1220 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1221 | #endif | |
1222 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1223 | testresult &= execute_test_ktls(0, 1, 0, 0, tlsver, | |
1224 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1225 | #endif | |
1226 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1227 | testresult &= execute_test_ktls(0, 1, 0, 0, tlsver, | |
1228 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1229 | #endif | |
1230 | return testresult; | |
2fab79af BP |
1231 | } |
1232 | ||
da4db160 | 1233 | static int test_ktls_client_no_txrx_server(int tlsver) |
2fab79af | 1234 | { |
da4db160 VF |
1235 | int testresult = 1; |
1236 | ||
1237 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1238 | testresult &= execute_test_ktls(1, 1, 0, 0, tlsver, | |
1239 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1240 | #endif | |
1241 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1242 | testresult &= execute_test_ktls(1, 1, 0, 0, tlsver, | |
1243 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1244 | #endif | |
1245 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1246 | testresult &= execute_test_ktls(1, 1, 0, 0, tlsver, | |
1247 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1248 | #endif | |
1249 | return testresult; | |
2fab79af BP |
1250 | } |
1251 | ||
da4db160 | 1252 | static int test_ktls_no_txrx_client_no_rx_server(int tlsver) |
2fab79af | 1253 | { |
da4db160 VF |
1254 | int testresult = 1; |
1255 | ||
1256 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1257 | testresult &= execute_test_ktls(0, 0, 1, 0, tlsver, | |
1258 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1259 | #endif | |
1260 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1261 | testresult &= execute_test_ktls(0, 0, 1, 0, tlsver, | |
1262 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1263 | #endif | |
1264 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1265 | testresult &= execute_test_ktls(0, 0, 1, 0, tlsver, | |
1266 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1267 | #endif | |
1268 | return testresult; | |
2fab79af BP |
1269 | } |
1270 | ||
da4db160 | 1271 | static int test_ktls_no_rx_client_no_rx_server(int tlsver) |
2fab79af | 1272 | { |
da4db160 VF |
1273 | int testresult = 1; |
1274 | ||
1275 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1276 | testresult &= execute_test_ktls(1, 0, 1, 0, tlsver, | |
1277 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1278 | #endif | |
1279 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1280 | testresult &= execute_test_ktls(1, 0, 1, 0, tlsver, | |
1281 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1282 | #endif | |
1283 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1284 | testresult &= execute_test_ktls(1, 0, 1, 0, tlsver, | |
1285 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1286 | #endif | |
1287 | return testresult; | |
2fab79af BP |
1288 | } |
1289 | ||
da4db160 | 1290 | static int test_ktls_no_tx_client_no_rx_server(int tlsver) |
2fab79af | 1291 | { |
da4db160 VF |
1292 | int testresult = 1; |
1293 | ||
1294 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1295 | testresult &= execute_test_ktls(0, 1, 1, 0, tlsver, | |
1296 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1297 | #endif | |
1298 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1299 | testresult &= execute_test_ktls(0, 1, 1, 0, tlsver, | |
1300 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1301 | #endif | |
1302 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1303 | testresult &= execute_test_ktls(0, 1, 1, 0, tlsver, | |
1304 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1305 | #endif | |
1306 | return testresult; | |
2fab79af BP |
1307 | } |
1308 | ||
da4db160 | 1309 | static int test_ktls_client_no_rx_server(int tlsver) |
fe5d9450 | 1310 | { |
da4db160 VF |
1311 | int testresult = 1; |
1312 | ||
1313 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1314 | testresult &= execute_test_ktls(1, 1, 1, 0, tlsver, | |
1315 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1316 | #endif | |
1317 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1318 | testresult &= execute_test_ktls(1, 1, 1, 0, tlsver, | |
1319 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1320 | #endif | |
1321 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1322 | testresult &= execute_test_ktls(1, 1, 1, 0, tlsver, | |
1323 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1324 | #endif | |
1325 | return testresult; | |
fe5d9450 BP |
1326 | } |
1327 | ||
da4db160 | 1328 | static int test_ktls_no_txrx_client_no_tx_server(int tlsver) |
fe5d9450 | 1329 | { |
da4db160 VF |
1330 | int testresult = 1; |
1331 | ||
1332 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1333 | testresult &= execute_test_ktls(0, 0, 0, 1, tlsver, | |
1334 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1335 | #endif | |
1336 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1337 | testresult &= execute_test_ktls(0, 0, 0, 1, tlsver, | |
1338 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1339 | #endif | |
1340 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1341 | testresult &= execute_test_ktls(0, 0, 0, 1, tlsver, | |
1342 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1343 | #endif | |
1344 | return testresult; | |
fe5d9450 BP |
1345 | } |
1346 | ||
da4db160 | 1347 | static int test_ktls_no_rx_client_no_tx_server(int tlsver) |
fe5d9450 | 1348 | { |
da4db160 VF |
1349 | int testresult = 1; |
1350 | ||
1351 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1352 | testresult &= execute_test_ktls(1, 0, 0, 1, tlsver, | |
1353 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1354 | #endif | |
1355 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1356 | testresult &= execute_test_ktls(1, 0, 0, 1, tlsver, | |
1357 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1358 | #endif | |
1359 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1360 | testresult &= execute_test_ktls(1, 0, 0, 1, tlsver, | |
1361 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1362 | #endif | |
1363 | return testresult; | |
fe5d9450 BP |
1364 | } |
1365 | ||
da4db160 | 1366 | static int test_ktls_no_tx_client_no_tx_server(int tlsver) |
2fab79af | 1367 | { |
da4db160 VF |
1368 | int testresult = 1; |
1369 | ||
1370 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1371 | testresult &= execute_test_ktls(0, 1, 0, 1, tlsver, | |
1372 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1373 | #endif | |
1374 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1375 | testresult &= execute_test_ktls(0, 1, 0, 1, tlsver, | |
1376 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1377 | #endif | |
1378 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1379 | testresult &= execute_test_ktls(0, 1, 0, 1, tlsver, | |
1380 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1381 | #endif | |
1382 | return testresult; | |
2fab79af BP |
1383 | } |
1384 | ||
da4db160 | 1385 | static int test_ktls_client_no_tx_server(int tlsver) |
2fab79af | 1386 | { |
da4db160 VF |
1387 | int testresult = 1; |
1388 | ||
1389 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1390 | testresult &= execute_test_ktls(1, 1, 0, 1, tlsver, | |
1391 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1392 | #endif | |
1393 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1394 | testresult &= execute_test_ktls(1, 1, 0, 1, tlsver, | |
1395 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1396 | #endif | |
1397 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1398 | testresult &= execute_test_ktls(1, 1, 0, 1, tlsver, | |
1399 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1400 | #endif | |
1401 | return testresult; | |
2fab79af BP |
1402 | } |
1403 | ||
da4db160 | 1404 | static int test_ktls_no_txrx_client_server(int tlsver) |
2fab79af | 1405 | { |
da4db160 VF |
1406 | int testresult = 1; |
1407 | ||
1408 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1409 | testresult &= execute_test_ktls(0, 0, 1, 1, tlsver, | |
1410 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1411 | #endif | |
1412 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1413 | testresult &= execute_test_ktls(0, 0, 1, 1, tlsver, | |
1414 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1415 | #endif | |
1416 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1417 | testresult &= execute_test_ktls(0, 0, 1, 1, tlsver, | |
1418 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1419 | #endif | |
1420 | return testresult; | |
2fab79af BP |
1421 | } |
1422 | ||
da4db160 | 1423 | static int test_ktls_no_rx_client_server(int tlsver) |
2fab79af | 1424 | { |
da4db160 VF |
1425 | int testresult = 1; |
1426 | ||
1427 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1428 | testresult &= execute_test_ktls(1, 0, 1, 1, tlsver, | |
1429 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1430 | #endif | |
1431 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1432 | testresult &= execute_test_ktls(1, 0, 1, 1, tlsver, | |
1433 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1434 | #endif | |
1435 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1436 | testresult &= execute_test_ktls(1, 0, 1, 1, tlsver, | |
1437 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1438 | #endif | |
1439 | return testresult; | |
2fab79af BP |
1440 | } |
1441 | ||
da4db160 | 1442 | static int test_ktls_no_tx_client_server(int tlsver) |
2fab79af | 1443 | { |
da4db160 VF |
1444 | int testresult = 1; |
1445 | ||
1446 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1447 | testresult &= execute_test_ktls(0, 1, 1, 1, tlsver, | |
1448 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1449 | #endif | |
1450 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1451 | testresult &= execute_test_ktls(0, 1, 1, 1, tlsver, | |
1452 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1453 | #endif | |
1454 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1455 | testresult &= execute_test_ktls(0, 1, 1, 1, tlsver, | |
1456 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1457 | #endif | |
1458 | return testresult; | |
1459 | } | |
1460 | ||
1461 | static int test_ktls_client_server(int tlsver) | |
1462 | { | |
1463 | int testresult = 1; | |
1464 | ||
1465 | #ifdef OPENSSL_KTLS_AES_GCM_128 | |
1466 | testresult &= execute_test_ktls(1, 1, 1, 1, tlsver, | |
1467 | "AES128-GCM-SHA256", TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); | |
1468 | #endif | |
1469 | #ifdef OPENSSL_KTLS_AES_CCM_128 | |
1470 | testresult &= execute_test_ktls(1, 1, 1, 1, tlsver, | |
1471 | "AES128-CCM-SHA256", TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); | |
1472 | #endif | |
1473 | #ifdef OPENSSL_KTLS_AES_GCM_256 | |
1474 | testresult &= execute_test_ktls(1, 1, 1, 1, tlsver, | |
1475 | "AES256-GCM-SHA384", TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); | |
1476 | #endif | |
1477 | return testresult; | |
1478 | } | |
1479 | ||
1480 | #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3) | |
1481 | static int test_ktls(int test) | |
1482 | { | |
1483 | int tlsver; | |
1484 | ||
1485 | if (test > 15) { | |
1486 | #if defined(OPENSSL_NO_TLS1_3) | |
1487 | return 1; | |
1488 | #else | |
1489 | test -= 16; | |
1490 | tlsver = TLS1_3_VERSION; | |
1491 | #endif | |
1492 | } else { | |
1493 | #if defined(OPENSSL_NO_TLS1_2) | |
1494 | return 1; | |
1495 | #else | |
1496 | tlsver = TLS1_2_VERSION; | |
1497 | #endif | |
1498 | } | |
1499 | ||
1500 | switch(test) { | |
1501 | case 0: | |
1502 | return test_ktls_no_txrx_client_no_txrx_server(tlsver); | |
1503 | case 1: | |
1504 | return test_ktls_no_rx_client_no_txrx_server(tlsver); | |
1505 | case 2: | |
1506 | return test_ktls_no_tx_client_no_txrx_server(tlsver); | |
1507 | case 3: | |
1508 | return test_ktls_client_no_txrx_server(tlsver); | |
1509 | case 4: | |
1510 | return test_ktls_no_txrx_client_no_rx_server(tlsver); | |
1511 | case 5: | |
1512 | return test_ktls_no_rx_client_no_rx_server(tlsver); | |
1513 | case 6: | |
1514 | return test_ktls_no_tx_client_no_rx_server(tlsver); | |
1515 | case 7: | |
1516 | return test_ktls_client_no_rx_server(tlsver); | |
1517 | case 8: | |
1518 | return test_ktls_no_txrx_client_no_tx_server(tlsver); | |
1519 | case 9: | |
1520 | return test_ktls_no_rx_client_no_tx_server(tlsver); | |
1521 | case 10: | |
1522 | return test_ktls_no_tx_client_no_tx_server(tlsver); | |
1523 | case 11: | |
1524 | return test_ktls_client_no_tx_server(tlsver); | |
1525 | case 12: | |
1526 | return test_ktls_no_txrx_client_server(tlsver); | |
1527 | case 13: | |
1528 | return test_ktls_no_rx_client_server(tlsver); | |
1529 | case 14: | |
1530 | return test_ktls_no_tx_client_server(tlsver); | |
1531 | case 15: | |
1532 | return test_ktls_client_server(tlsver); | |
1533 | default: | |
1534 | return 0; | |
1535 | } | |
2fab79af BP |
1536 | } |
1537 | ||
da4db160 | 1538 | static int test_ktls_sendfile_anytls(int tst) |
fe5d9450 | 1539 | { |
da4db160 VF |
1540 | char *cipher[] = {"AES128-GCM-SHA256","AES128-CCM-SHA256","AES256-GCM-SHA384"}; |
1541 | int tlsver; | |
1542 | ||
1543 | if (tst > 2) { | |
1544 | #if defined(OPENSSL_NO_TLS1_3) | |
1545 | return 1; | |
1546 | #else | |
1547 | tst -= 3; | |
1548 | tlsver = TLS1_3_VERSION; | |
1549 | #endif | |
1550 | } else { | |
1551 | #if defined(OPENSSL_NO_TLS1_2) | |
1552 | return 1; | |
1553 | #else | |
1554 | tlsver = TLS1_2_VERSION; | |
1555 | #endif | |
1556 | } | |
1557 | ||
1558 | #ifndef OPENSSL_KTLS_AES_GCM_128 | |
1559 | if(tst == 0) return 1; | |
1560 | #endif | |
1561 | #ifndef OPENSSL_KTLS_AES_CCM_128 | |
1562 | if(tst == 1) return 1; | |
1563 | #endif | |
1564 | #ifndef OPENSSL_KTLS_AES_GCM_256 | |
1565 | if(tst == 2) return 1; | |
1566 | #endif | |
1567 | return test_ktls_sendfile(tlsver, cipher[tst]); | |
fe5d9450 | 1568 | } |
da4db160 VF |
1569 | |
1570 | #endif | |
fe5d9450 BP |
1571 | #endif |
1572 | ||
84d5549e MC |
1573 | static int test_large_message_tls(void) |
1574 | { | |
7856332e | 1575 | return execute_test_large_message(TLS_server_method(), TLS_client_method(), |
5c587fb6 | 1576 | TLS1_VERSION, 0, 0); |
7856332e MC |
1577 | } |
1578 | ||
1579 | static int test_large_message_tls_read_ahead(void) | |
1580 | { | |
1581 | return execute_test_large_message(TLS_server_method(), TLS_client_method(), | |
5c587fb6 | 1582 | TLS1_VERSION, 0, 1); |
84d5549e MC |
1583 | } |
1584 | ||
55386bef | 1585 | #ifndef OPENSSL_NO_DTLS |
84d5549e MC |
1586 | static int test_large_message_dtls(void) |
1587 | { | |
7856332e MC |
1588 | /* |
1589 | * read_ahead is not relevant to DTLS because DTLS always acts as if | |
1590 | * read_ahead is set. | |
1591 | */ | |
84d5549e | 1592 | return execute_test_large_message(DTLS_server_method(), |
7d7f6834 | 1593 | DTLS_client_method(), |
5c587fb6 | 1594 | DTLS1_VERSION, 0, 0); |
84d5549e | 1595 | } |
55386bef | 1596 | #endif |
84d5549e | 1597 | |
8f8c11d8 | 1598 | #ifndef OPENSSL_NO_OCSP |
ba881d3b MC |
1599 | static int ocsp_server_cb(SSL *s, void *arg) |
1600 | { | |
1601 | int *argi = (int *)arg; | |
710756a9 | 1602 | unsigned char *copy = NULL; |
ba881d3b MC |
1603 | STACK_OF(OCSP_RESPID) *ids = NULL; |
1604 | OCSP_RESPID *id = NULL; | |
1605 | ||
1606 | if (*argi == 2) { | |
1607 | /* In this test we are expecting exactly 1 OCSP_RESPID */ | |
1608 | SSL_get_tlsext_status_ids(s, &ids); | |
1609 | if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1) | |
1610 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
1611 | ||
1612 | id = sk_OCSP_RESPID_value(ids, 0); | |
5e30f2fd | 1613 | if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL)) |
ba881d3b MC |
1614 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
1615 | } else if (*argi != 1) { | |
1616 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
1617 | } | |
1618 | ||
710756a9 | 1619 | if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder)))) |
ba881d3b MC |
1620 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
1621 | ||
710756a9 | 1622 | SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder)); |
ba881d3b | 1623 | ocsp_server_called = 1; |
ba881d3b MC |
1624 | return SSL_TLSEXT_ERR_OK; |
1625 | } | |
1626 | ||
1627 | static int ocsp_client_cb(SSL *s, void *arg) | |
1628 | { | |
1629 | int *argi = (int *)arg; | |
1630 | const unsigned char *respderin; | |
1631 | size_t len; | |
1632 | ||
1633 | if (*argi != 1 && *argi != 2) | |
1634 | return 0; | |
1635 | ||
1636 | len = SSL_get_tlsext_status_ocsp_resp(s, &respderin); | |
710756a9 | 1637 | if (!TEST_mem_eq(orespder, len, respderin, len)) |
ba881d3b MC |
1638 | return 0; |
1639 | ||
1640 | ocsp_client_called = 1; | |
ba881d3b MC |
1641 | return 1; |
1642 | } | |
1643 | ||
2cb4b5f6 MC |
1644 | static int test_tlsext_status_type(void) |
1645 | { | |
ba881d3b MC |
1646 | SSL_CTX *cctx = NULL, *sctx = NULL; |
1647 | SSL *clientssl = NULL, *serverssl = NULL; | |
2cb4b5f6 | 1648 | int testresult = 0; |
ba881d3b MC |
1649 | STACK_OF(OCSP_RESPID) *ids = NULL; |
1650 | OCSP_RESPID *id = NULL; | |
1651 | BIO *certbio = NULL; | |
2cb4b5f6 | 1652 | |
5e30f2fd | 1653 | if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(), |
5c587fb6 | 1654 | TLS1_VERSION, 0, |
7d7f6834 | 1655 | &sctx, &cctx, cert, privkey)) |
ba881d3b | 1656 | return 0; |
2cb4b5f6 | 1657 | |
710756a9 | 1658 | if (SSL_CTX_get_tlsext_status_type(cctx) != -1) |
2cb4b5f6 | 1659 | goto end; |
2cb4b5f6 | 1660 | |
ba881d3b | 1661 | /* First just do various checks getting and setting tlsext_status_type */ |
2cb4b5f6 | 1662 | |
ba881d3b | 1663 | clientssl = SSL_new(cctx); |
710756a9 RS |
1664 | if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1) |
1665 | || !TEST_true(SSL_set_tlsext_status_type(clientssl, | |
1666 | TLSEXT_STATUSTYPE_ocsp)) | |
1667 | || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl), | |
1668 | TLSEXT_STATUSTYPE_ocsp)) | |
2cb4b5f6 | 1669 | goto end; |
2cb4b5f6 | 1670 | |
ba881d3b MC |
1671 | SSL_free(clientssl); |
1672 | clientssl = NULL; | |
2cb4b5f6 | 1673 | |
710756a9 RS |
1674 | if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp) |
1675 | || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp) | |
2cb4b5f6 | 1676 | goto end; |
2cb4b5f6 | 1677 | |
ba881d3b | 1678 | clientssl = SSL_new(cctx); |
710756a9 | 1679 | if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp) |
2cb4b5f6 | 1680 | goto end; |
ba881d3b MC |
1681 | SSL_free(clientssl); |
1682 | clientssl = NULL; | |
1683 | ||
1684 | /* | |
1685 | * Now actually do a handshake and check OCSP information is exchanged and | |
1686 | * the callbacks get called | |
1687 | */ | |
ba881d3b MC |
1688 | SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb); |
1689 | SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg); | |
1690 | SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb); | |
1691 | SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg); | |
710756a9 RS |
1692 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
1693 | &clientssl, NULL, NULL)) | |
1694 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
1695 | SSL_ERROR_NONE)) | |
1696 | || !TEST_true(ocsp_client_called) | |
1697 | || !TEST_true(ocsp_server_called)) | |
ba881d3b | 1698 | goto end; |
ba881d3b MC |
1699 | SSL_free(serverssl); |
1700 | SSL_free(clientssl); | |
1701 | serverssl = NULL; | |
1702 | clientssl = NULL; | |
1703 | ||
1704 | /* Try again but this time force the server side callback to fail */ | |
1705 | ocsp_client_called = 0; | |
1706 | ocsp_server_called = 0; | |
1707 | cdummyarg = 0; | |
710756a9 RS |
1708 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
1709 | &clientssl, NULL, NULL)) | |
1710 | /* This should fail because the callback will fail */ | |
1711 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
1712 | SSL_ERROR_NONE)) | |
1713 | || !TEST_false(ocsp_client_called) | |
1714 | || !TEST_false(ocsp_server_called)) | |
ba881d3b | 1715 | goto end; |
ba881d3b MC |
1716 | SSL_free(serverssl); |
1717 | SSL_free(clientssl); | |
1718 | serverssl = NULL; | |
1719 | clientssl = NULL; | |
1720 | ||
1721 | /* | |
1722 | * This time we'll get the client to send an OCSP_RESPID that it will | |
1723 | * accept. | |
1724 | */ | |
1725 | ocsp_client_called = 0; | |
1726 | ocsp_server_called = 0; | |
1727 | cdummyarg = 2; | |
710756a9 RS |
1728 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
1729 | &clientssl, NULL, NULL))) | |
ba881d3b | 1730 | goto end; |
ba881d3b MC |
1731 | |
1732 | /* | |
1733 | * We'll just use any old cert for this test - it doesn't have to be an OCSP | |
69687aa8 | 1734 | * specific one. We'll use the server cert. |
ba881d3b | 1735 | */ |
710756a9 RS |
1736 | if (!TEST_ptr(certbio = BIO_new_file(cert, "r")) |
1737 | || !TEST_ptr(id = OCSP_RESPID_new()) | |
1738 | || !TEST_ptr(ids = sk_OCSP_RESPID_new_null()) | |
1739 | || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio, | |
1740 | NULL, NULL, NULL)) | |
5e30f2fd | 1741 | || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL)) |
710756a9 | 1742 | || !TEST_true(sk_OCSP_RESPID_push(ids, id))) |
ba881d3b | 1743 | goto end; |
ba881d3b MC |
1744 | id = NULL; |
1745 | SSL_set_tlsext_status_ids(clientssl, ids); | |
1746 | /* Control has been transferred */ | |
1747 | ids = NULL; | |
1748 | ||
1749 | BIO_free(certbio); | |
1750 | certbio = NULL; | |
1751 | ||
710756a9 RS |
1752 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, |
1753 | SSL_ERROR_NONE)) | |
1754 | || !TEST_true(ocsp_client_called) | |
1755 | || !TEST_true(ocsp_server_called)) | |
ba881d3b | 1756 | goto end; |
ba881d3b | 1757 | |
2cb4b5f6 MC |
1758 | testresult = 1; |
1759 | ||
1760 | end: | |
ba881d3b MC |
1761 | SSL_free(serverssl); |
1762 | SSL_free(clientssl); | |
1763 | SSL_CTX_free(sctx); | |
1764 | SSL_CTX_free(cctx); | |
1765 | sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free); | |
1766 | OCSP_RESPID_free(id); | |
1767 | BIO_free(certbio); | |
1768 | X509_free(ocspcert); | |
1769 | ocspcert = NULL; | |
2cb4b5f6 MC |
1770 | |
1771 | return testresult; | |
1772 | } | |
8f8c11d8 | 1773 | #endif |
2cb4b5f6 | 1774 | |
bf208d95 | 1775 | #if !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) |
0afca811 | 1776 | static int new_called, remove_called, get_called; |
eaa776da | 1777 | |
eaa776da MC |
1778 | static int new_session_cb(SSL *ssl, SSL_SESSION *sess) |
1779 | { | |
1780 | new_called++; | |
c0537ebd MC |
1781 | /* |
1782 | * sess has been up-refed for us, but we don't actually need it so free it | |
1783 | * immediately. | |
1784 | */ | |
1785 | SSL_SESSION_free(sess); | |
eaa776da MC |
1786 | return 1; |
1787 | } | |
1788 | ||
1789 | static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess) | |
1790 | { | |
1791 | remove_called++; | |
1792 | } | |
1793 | ||
7a301f08 MC |
1794 | static SSL_SESSION *get_sess_val = NULL; |
1795 | ||
1796 | static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len, | |
1797 | int *copy) | |
1798 | { | |
0afca811 | 1799 | get_called++; |
7a301f08 MC |
1800 | *copy = 1; |
1801 | return get_sess_val; | |
1802 | } | |
1803 | ||
7a301f08 | 1804 | static int execute_test_session(int maxprot, int use_int_cache, |
90fc2c26 | 1805 | int use_ext_cache, long s_options) |
2cb4b5f6 MC |
1806 | { |
1807 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
1808 | SSL *serverssl1 = NULL, *clientssl1 = NULL; | |
1809 | SSL *serverssl2 = NULL, *clientssl2 = NULL; | |
bf208d95 | 1810 | # ifndef OPENSSL_NO_TLS1_1 |
eaa776da | 1811 | SSL *serverssl3 = NULL, *clientssl3 = NULL; |
bf208d95 | 1812 | # endif |
2cb4b5f6 | 1813 | SSL_SESSION *sess1 = NULL, *sess2 = NULL; |
36ff232c | 1814 | int testresult = 0, numnewsesstick = 1; |
2cb4b5f6 | 1815 | |
7e885b7b P |
1816 | new_called = remove_called = 0; |
1817 | ||
36ff232c MC |
1818 | /* TLSv1.3 sends 2 NewSessionTickets */ |
1819 | if (maxprot == TLS1_3_VERSION) | |
1820 | numnewsesstick = 2; | |
1821 | ||
5e30f2fd MC |
1822 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
1823 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 1824 | &sctx, &cctx, cert, privkey))) |
2cb4b5f6 | 1825 | return 0; |
2cb4b5f6 | 1826 | |
7a301f08 MC |
1827 | /* |
1828 | * Only allow the max protocol version so we can force a connection failure | |
1829 | * later | |
1830 | */ | |
1831 | SSL_CTX_set_min_proto_version(cctx, maxprot); | |
1832 | SSL_CTX_set_max_proto_version(cctx, maxprot); | |
eaa776da MC |
1833 | |
1834 | /* Set up session cache */ | |
7e885b7b | 1835 | if (use_ext_cache) { |
eaa776da MC |
1836 | SSL_CTX_sess_set_new_cb(cctx, new_session_cb); |
1837 | SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb); | |
1838 | } | |
7e885b7b | 1839 | if (use_int_cache) { |
eaa776da MC |
1840 | /* Also covers instance where both are set */ |
1841 | SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT); | |
1842 | } else { | |
1843 | SSL_CTX_set_session_cache_mode(cctx, | |
1844 | SSL_SESS_CACHE_CLIENT | |
1845 | | SSL_SESS_CACHE_NO_INTERNAL_STORE); | |
1846 | } | |
2cb4b5f6 | 1847 | |
90fc2c26 NM |
1848 | if (s_options) { |
1849 | SSL_CTX_set_options(sctx, s_options); | |
1850 | } | |
1851 | ||
710756a9 RS |
1852 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, |
1853 | NULL, NULL)) | |
1854 | || !TEST_true(create_ssl_connection(serverssl1, clientssl1, | |
1855 | SSL_ERROR_NONE)) | |
1856 | || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))) | |
2cb4b5f6 | 1857 | goto end; |
2cb4b5f6 | 1858 | |
710756a9 | 1859 | /* Should fail because it should already be in the cache */ |
7e885b7b | 1860 | if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1))) |
eaa776da | 1861 | goto end; |
7a301f08 | 1862 | if (use_ext_cache |
36ff232c MC |
1863 | && (!TEST_int_eq(new_called, numnewsesstick) |
1864 | ||
1865 | || !TEST_int_eq(remove_called, 0))) | |
b4982125 | 1866 | goto end; |
b4982125 | 1867 | |
c0537ebd MC |
1868 | new_called = remove_called = 0; |
1869 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, | |
1870 | &clientssl2, NULL, NULL)) | |
1871 | || !TEST_true(SSL_set_session(clientssl2, sess1)) | |
1872 | || !TEST_true(create_ssl_connection(serverssl2, clientssl2, | |
1873 | SSL_ERROR_NONE)) | |
1874 | || !TEST_true(SSL_session_reused(clientssl2))) | |
1875 | goto end; | |
1876 | ||
7a301f08 MC |
1877 | if (maxprot == TLS1_3_VERSION) { |
1878 | /* | |
1879 | * In TLSv1.3 we should have created a new session even though we have | |
4cb00457 MC |
1880 | * resumed. Since we attempted a resume we should also have removed the |
1881 | * old ticket from the cache so that we try to only use tickets once. | |
7a301f08 MC |
1882 | */ |
1883 | if (use_ext_cache | |
1884 | && (!TEST_int_eq(new_called, 1) | |
4cb00457 | 1885 | || !TEST_int_eq(remove_called, 1))) |
7a301f08 MC |
1886 | goto end; |
1887 | } else { | |
1888 | /* | |
1889 | * In TLSv1.2 we expect to have resumed so no sessions added or | |
1890 | * removed. | |
1891 | */ | |
1892 | if (use_ext_cache | |
1893 | && (!TEST_int_eq(new_called, 0) | |
1894 | || !TEST_int_eq(remove_called, 0))) | |
1895 | goto end; | |
1896 | } | |
c0537ebd MC |
1897 | |
1898 | SSL_SESSION_free(sess1); | |
1899 | if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2))) | |
1900 | goto end; | |
1901 | shutdown_ssl_connection(serverssl2, clientssl2); | |
1902 | serverssl2 = clientssl2 = NULL; | |
c0537ebd MC |
1903 | |
1904 | new_called = remove_called = 0; | |
710756a9 RS |
1905 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, |
1906 | &clientssl2, NULL, NULL)) | |
1907 | || !TEST_true(create_ssl_connection(serverssl2, clientssl2, | |
1908 | SSL_ERROR_NONE))) | |
2cb4b5f6 | 1909 | goto end; |
2cb4b5f6 | 1910 | |
710756a9 | 1911 | if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2))) |
2cb4b5f6 | 1912 | goto end; |
2cb4b5f6 | 1913 | |
7a301f08 | 1914 | if (use_ext_cache |
36ff232c MC |
1915 | && (!TEST_int_eq(new_called, numnewsesstick) |
1916 | || !TEST_int_eq(remove_called, 0))) | |
eaa776da | 1917 | goto end; |
eaa776da | 1918 | |
c0537ebd | 1919 | new_called = remove_called = 0; |
2cb4b5f6 | 1920 | /* |
710756a9 RS |
1921 | * This should clear sess2 from the cache because it is a "bad" session. |
1922 | * See SSL_set_session() documentation. | |
2cb4b5f6 | 1923 | */ |
710756a9 | 1924 | if (!TEST_true(SSL_set_session(clientssl2, sess1))) |
2cb4b5f6 | 1925 | goto end; |
7a301f08 MC |
1926 | if (use_ext_cache |
1927 | && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) | |
eaa776da | 1928 | goto end; |
710756a9 | 1929 | if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1)) |
2cb4b5f6 | 1930 | goto end; |
2cb4b5f6 | 1931 | |
7e885b7b | 1932 | if (use_int_cache) { |
710756a9 RS |
1933 | /* Should succeeded because it should not already be in the cache */ |
1934 | if (!TEST_true(SSL_CTX_add_session(cctx, sess2)) | |
1935 | || !TEST_true(SSL_CTX_remove_session(cctx, sess2))) | |
eaa776da | 1936 | goto end; |
eaa776da MC |
1937 | } |
1938 | ||
c0537ebd | 1939 | new_called = remove_called = 0; |
eaa776da | 1940 | /* This shouldn't be in the cache so should fail */ |
710756a9 | 1941 | if (!TEST_false(SSL_CTX_remove_session(cctx, sess2))) |
2cb4b5f6 | 1942 | goto end; |
2cb4b5f6 | 1943 | |
7a301f08 MC |
1944 | if (use_ext_cache |
1945 | && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) | |
2cb4b5f6 | 1946 | goto end; |
2cb4b5f6 | 1947 | |
bf208d95 | 1948 | # if !defined(OPENSSL_NO_TLS1_1) |
c0537ebd | 1949 | new_called = remove_called = 0; |
eaa776da MC |
1950 | /* Force a connection failure */ |
1951 | SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION); | |
710756a9 RS |
1952 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3, |
1953 | &clientssl3, NULL, NULL)) | |
1954 | || !TEST_true(SSL_set_session(clientssl3, sess1)) | |
c0537ebd | 1955 | /* This should fail because of the mismatched protocol versions */ |
710756a9 RS |
1956 | || !TEST_false(create_ssl_connection(serverssl3, clientssl3, |
1957 | SSL_ERROR_NONE))) | |
eaa776da | 1958 | goto end; |
b4982125 | 1959 | |
eaa776da | 1960 | /* We should have automatically removed the session from the cache */ |
7a301f08 MC |
1961 | if (use_ext_cache |
1962 | && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1))) | |
2cb4b5f6 | 1963 | goto end; |
2cb4b5f6 | 1964 | |
710756a9 | 1965 | /* Should succeed because it should not already be in the cache */ |
7e885b7b | 1966 | if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2))) |
eaa776da | 1967 | goto end; |
bf208d95 | 1968 | # endif |
eaa776da | 1969 | |
7a301f08 MC |
1970 | /* Now do some tests for server side caching */ |
1971 | if (use_ext_cache) { | |
1972 | SSL_CTX_sess_set_new_cb(cctx, NULL); | |
1973 | SSL_CTX_sess_set_remove_cb(cctx, NULL); | |
1974 | SSL_CTX_sess_set_new_cb(sctx, new_session_cb); | |
1975 | SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb); | |
1976 | SSL_CTX_sess_set_get_cb(sctx, get_session_cb); | |
1977 | get_sess_val = NULL; | |
1978 | } | |
1979 | ||
1980 | SSL_CTX_set_session_cache_mode(cctx, 0); | |
1981 | /* Internal caching is the default on the server side */ | |
1982 | if (!use_int_cache) | |
1983 | SSL_CTX_set_session_cache_mode(sctx, | |
1984 | SSL_SESS_CACHE_SERVER | |
1985 | | SSL_SESS_CACHE_NO_INTERNAL_STORE); | |
1986 | ||
1987 | SSL_free(serverssl1); | |
1988 | SSL_free(clientssl1); | |
1989 | serverssl1 = clientssl1 = NULL; | |
1990 | SSL_free(serverssl2); | |
1991 | SSL_free(clientssl2); | |
1992 | serverssl2 = clientssl2 = NULL; | |
1993 | SSL_SESSION_free(sess1); | |
1994 | sess1 = NULL; | |
1995 | SSL_SESSION_free(sess2); | |
1996 | sess2 = NULL; | |
1997 | ||
1998 | SSL_CTX_set_max_proto_version(sctx, maxprot); | |
6cc0b3c2 MC |
1999 | if (maxprot == TLS1_2_VERSION) |
2000 | SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET); | |
0afca811 | 2001 | new_called = remove_called = get_called = 0; |
7a301f08 MC |
2002 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, |
2003 | NULL, NULL)) | |
2004 | || !TEST_true(create_ssl_connection(serverssl1, clientssl1, | |
2005 | SSL_ERROR_NONE)) | |
2006 | || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)) | |
2007 | || !TEST_ptr(sess2 = SSL_get1_session(serverssl1))) | |
2008 | goto end; | |
2009 | ||
ee94ec2e MC |
2010 | if (use_int_cache) { |
2011 | if (maxprot == TLS1_3_VERSION && !use_ext_cache) { | |
2012 | /* | |
2013 | * In TLSv1.3 it should not have been added to the internal cache, | |
2014 | * except in the case where we also have an external cache (in that | |
2015 | * case it gets added to the cache in order to generate remove | |
2016 | * events after timeout). | |
2017 | */ | |
2018 | if (!TEST_false(SSL_CTX_remove_session(sctx, sess2))) | |
2019 | goto end; | |
2020 | } else { | |
2021 | /* Should fail because it should already be in the cache */ | |
2022 | if (!TEST_false(SSL_CTX_add_session(sctx, sess2))) | |
2023 | goto end; | |
2024 | } | |
2025 | } | |
7a301f08 MC |
2026 | |
2027 | if (use_ext_cache) { | |
2028 | SSL_SESSION *tmp = sess2; | |
2029 | ||
36ff232c | 2030 | if (!TEST_int_eq(new_called, numnewsesstick) |
0afca811 KY |
2031 | || !TEST_int_eq(remove_called, 0) |
2032 | || !TEST_int_eq(get_called, 0)) | |
7a301f08 MC |
2033 | goto end; |
2034 | /* | |
2035 | * Delete the session from the internal cache to force a lookup from | |
2036 | * the external cache. We take a copy first because | |
2037 | * SSL_CTX_remove_session() also marks the session as non-resumable. | |
2038 | */ | |
ee94ec2e | 2039 | if (use_int_cache && maxprot != TLS1_3_VERSION) { |
3cb6a4d6 BK |
2040 | if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2)) |
2041 | || !TEST_true(SSL_CTX_remove_session(sctx, sess2))) | |
2042 | goto end; | |
2043 | SSL_SESSION_free(sess2); | |
2044 | } | |
7a301f08 MC |
2045 | sess2 = tmp; |
2046 | } | |
2047 | ||
0afca811 | 2048 | new_called = remove_called = get_called = 0; |
7a301f08 MC |
2049 | get_sess_val = sess2; |
2050 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2, | |
2051 | &clientssl2, NULL, NULL)) | |
2052 | || !TEST_true(SSL_set_session(clientssl2, sess1)) | |
2053 | || !TEST_true(create_ssl_connection(serverssl2, clientssl2, | |
2054 | SSL_ERROR_NONE)) | |
2055 | || !TEST_true(SSL_session_reused(clientssl2))) | |
2056 | goto end; | |
2057 | ||
0afca811 | 2058 | if (use_ext_cache) { |
32305f88 | 2059 | if (!TEST_int_eq(remove_called, 0)) |
0afca811 KY |
2060 | goto end; |
2061 | ||
2062 | if (maxprot == TLS1_3_VERSION) { | |
32305f88 MC |
2063 | if (!TEST_int_eq(new_called, 1) |
2064 | || !TEST_int_eq(get_called, 0)) | |
0afca811 KY |
2065 | goto end; |
2066 | } else { | |
32305f88 MC |
2067 | if (!TEST_int_eq(new_called, 0) |
2068 | || !TEST_int_eq(get_called, 1)) | |
0afca811 KY |
2069 | goto end; |
2070 | } | |
2071 | } | |
7a301f08 | 2072 | |
2cb4b5f6 | 2073 | testresult = 1; |
eaa776da | 2074 | |
2cb4b5f6 MC |
2075 | end: |
2076 | SSL_free(serverssl1); | |
2077 | SSL_free(clientssl1); | |
2078 | SSL_free(serverssl2); | |
2079 | SSL_free(clientssl2); | |
bf208d95 | 2080 | # ifndef OPENSSL_NO_TLS1_1 |
eaa776da MC |
2081 | SSL_free(serverssl3); |
2082 | SSL_free(clientssl3); | |
bf208d95 | 2083 | # endif |
2cb4b5f6 MC |
2084 | SSL_SESSION_free(sess1); |
2085 | SSL_SESSION_free(sess2); | |
2086 | SSL_CTX_free(sctx); | |
2087 | SSL_CTX_free(cctx); | |
2088 | ||
2089 | return testresult; | |
2090 | } | |
bf208d95 | 2091 | #endif /* !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */ |
2cb4b5f6 | 2092 | |
7fb4c820 MC |
2093 | static int test_session_with_only_int_cache(void) |
2094 | { | |
7a301f08 | 2095 | #ifndef OPENSSL_NO_TLS1_3 |
90fc2c26 | 2096 | if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0)) |
7a301f08 MC |
2097 | return 0; |
2098 | #endif | |
2099 | ||
2100 | #ifndef OPENSSL_NO_TLS1_2 | |
90fc2c26 | 2101 | return execute_test_session(TLS1_2_VERSION, 1, 0, 0); |
7a301f08 MC |
2102 | #else |
2103 | return 1; | |
2104 | #endif | |
eaa776da MC |
2105 | } |
2106 | ||
7fb4c820 MC |
2107 | static int test_session_with_only_ext_cache(void) |
2108 | { | |
7a301f08 | 2109 | #ifndef OPENSSL_NO_TLS1_3 |
90fc2c26 | 2110 | if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0)) |
7a301f08 MC |
2111 | return 0; |
2112 | #endif | |
2113 | ||
2114 | #ifndef OPENSSL_NO_TLS1_2 | |
90fc2c26 | 2115 | return execute_test_session(TLS1_2_VERSION, 0, 1, 0); |
7a301f08 MC |
2116 | #else |
2117 | return 1; | |
2118 | #endif | |
eaa776da MC |
2119 | } |
2120 | ||
7fb4c820 MC |
2121 | static int test_session_with_both_cache(void) |
2122 | { | |
7a301f08 | 2123 | #ifndef OPENSSL_NO_TLS1_3 |
90fc2c26 NM |
2124 | if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0)) |
2125 | return 0; | |
2126 | #endif | |
2127 | ||
2128 | #ifndef OPENSSL_NO_TLS1_2 | |
2129 | return execute_test_session(TLS1_2_VERSION, 1, 1, 0); | |
2130 | #else | |
2131 | return 1; | |
2132 | #endif | |
2133 | } | |
2134 | ||
2135 | static int test_session_wo_ca_names(void) | |
2136 | { | |
2137 | #ifndef OPENSSL_NO_TLS1_3 | |
2138 | if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES)) | |
7a301f08 MC |
2139 | return 0; |
2140 | #endif | |
2141 | ||
2142 | #ifndef OPENSSL_NO_TLS1_2 | |
90fc2c26 | 2143 | return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES); |
7a301f08 MC |
2144 | #else |
2145 | return 1; | |
2146 | #endif | |
eaa776da MC |
2147 | } |
2148 | ||
90fc2c26 | 2149 | |
04225915 | 2150 | #ifndef OPENSSL_NO_TLS1_3 |
c22365b3 MC |
2151 | static SSL_SESSION *sesscache[6]; |
2152 | static int do_cache; | |
36ff232c MC |
2153 | |
2154 | static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess) | |
2155 | { | |
c22365b3 MC |
2156 | if (do_cache) { |
2157 | sesscache[new_called] = sess; | |
2158 | } else { | |
2159 | /* We don't need the reference to the session, so free it */ | |
2160 | SSL_SESSION_free(sess); | |
2161 | } | |
2162 | new_called++; | |
2163 | ||
2164 | return 1; | |
2165 | } | |
2166 | ||
2167 | static int post_handshake_verify(SSL *sssl, SSL *cssl) | |
2168 | { | |
2169 | SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL); | |
2170 | if (!TEST_true(SSL_verify_client_post_handshake(sssl))) | |
2171 | return 0; | |
2172 | ||
2173 | /* Start handshake on the server and client */ | |
2174 | if (!TEST_int_eq(SSL_do_handshake(sssl), 1) | |
2175 | || !TEST_int_le(SSL_read(cssl, NULL, 0), 0) | |
2176 | || !TEST_int_le(SSL_read(sssl, NULL, 0), 0) | |
2177 | || !TEST_true(create_ssl_connection(sssl, cssl, | |
2178 | SSL_ERROR_NONE))) | |
2179 | return 0; | |
36ff232c MC |
2180 | |
2181 | return 1; | |
2182 | } | |
2183 | ||
fbe9dafd | 2184 | static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx, |
03cdf559 MC |
2185 | SSL_CTX **cctx) |
2186 | { | |
2187 | int sess_id_ctx = 1; | |
2188 | ||
5e30f2fd MC |
2189 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
2190 | TLS_client_method(), TLS1_VERSION, 0, | |
2191 | sctx, cctx, cert, privkey)) | |
03cdf559 MC |
2192 | || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx)) |
2193 | || !TEST_true(SSL_CTX_set_session_id_context(*sctx, | |
2194 | (void *)&sess_id_ctx, | |
2195 | sizeof(sess_id_ctx)))) | |
2196 | return 0; | |
2197 | ||
2198 | if (stateful) | |
2199 | SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET); | |
2200 | ||
2201 | SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT | |
2202 | | SSL_SESS_CACHE_NO_INTERNAL_STORE); | |
2203 | SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb); | |
2204 | ||
2205 | return 1; | |
2206 | } | |
2207 | ||
2208 | static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ) | |
2209 | { | |
2210 | SSL *serverssl = NULL, *clientssl = NULL; | |
2211 | int i; | |
2212 | ||
2213 | /* Test that we can resume with all the tickets we got given */ | |
2214 | for (i = 0; i < idx * 2; i++) { | |
2215 | new_called = 0; | |
2216 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
2217 | &clientssl, NULL, NULL)) | |
2218 | || !TEST_true(SSL_set_session(clientssl, sesscache[i]))) | |
2219 | goto end; | |
2220 | ||
32097b33 | 2221 | SSL_set_post_handshake_auth(clientssl, 1); |
03cdf559 MC |
2222 | |
2223 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
2224 | SSL_ERROR_NONE))) | |
2225 | goto end; | |
2226 | ||
2227 | /* | |
2228 | * Following a successful resumption we only get 1 ticket. After a | |
2229 | * failed one we should get idx tickets. | |
2230 | */ | |
2231 | if (succ) { | |
2232 | if (!TEST_true(SSL_session_reused(clientssl)) | |
2233 | || !TEST_int_eq(new_called, 1)) | |
2234 | goto end; | |
2235 | } else { | |
2236 | if (!TEST_false(SSL_session_reused(clientssl)) | |
2237 | || !TEST_int_eq(new_called, idx)) | |
2238 | goto end; | |
2239 | } | |
2240 | ||
2241 | new_called = 0; | |
2242 | /* After a post-handshake authentication we should get 1 new ticket */ | |
2243 | if (succ | |
2244 | && (!post_handshake_verify(serverssl, clientssl) | |
2245 | || !TEST_int_eq(new_called, 1))) | |
2246 | goto end; | |
2247 | ||
2248 | SSL_shutdown(clientssl); | |
2249 | SSL_shutdown(serverssl); | |
2250 | SSL_free(serverssl); | |
2251 | SSL_free(clientssl); | |
2252 | serverssl = clientssl = NULL; | |
2253 | SSL_SESSION_free(sesscache[i]); | |
2254 | sesscache[i] = NULL; | |
2255 | } | |
2256 | ||
2257 | return 1; | |
2258 | ||
2259 | end: | |
2260 | SSL_free(clientssl); | |
2261 | SSL_free(serverssl); | |
2262 | return 0; | |
2263 | } | |
2264 | ||
04d7814a | 2265 | static int test_tickets(int stateful, int idx) |
36ff232c MC |
2266 | { |
2267 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
2268 | SSL *serverssl = NULL, *clientssl = NULL; | |
03cdf559 | 2269 | int testresult = 0; |
36ff232c MC |
2270 | size_t j; |
2271 | ||
2272 | /* idx is the test number, but also the number of tickets we want */ | |
2273 | ||
2274 | new_called = 0; | |
c22365b3 | 2275 | do_cache = 1; |
36ff232c | 2276 | |
fbe9dafd | 2277 | if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) |
36ff232c MC |
2278 | goto end; |
2279 | ||
03cdf559 MC |
2280 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
2281 | &clientssl, NULL, NULL))) | |
2282 | goto end; | |
2283 | ||
2284 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
2285 | SSL_ERROR_NONE)) | |
2286 | /* Check we got the number of tickets we were expecting */ | |
2287 | || !TEST_int_eq(idx, new_called)) | |
2288 | goto end; | |
04d7814a | 2289 | |
03cdf559 MC |
2290 | SSL_shutdown(clientssl); |
2291 | SSL_shutdown(serverssl); | |
2292 | SSL_free(serverssl); | |
2293 | SSL_free(clientssl); | |
2294 | SSL_CTX_free(sctx); | |
2295 | SSL_CTX_free(cctx); | |
2296 | clientssl = serverssl = NULL; | |
2297 | sctx = cctx = NULL; | |
2298 | ||
2299 | /* | |
2300 | * Now we try to resume with the tickets we previously created. The | |
2301 | * resumption attempt is expected to fail (because we're now using a new | |
2302 | * SSL_CTX). We should see idx number of tickets issued again. | |
2303 | */ | |
2304 | ||
2305 | /* Stop caching sessions - just count them */ | |
2306 | do_cache = 0; | |
2307 | ||
fbe9dafd | 2308 | if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) |
03cdf559 MC |
2309 | goto end; |
2310 | ||
2311 | if (!check_resumption(idx, sctx, cctx, 0)) | |
2312 | goto end; | |
2313 | ||
2314 | /* Start again with caching sessions */ | |
2315 | new_called = 0; | |
2316 | do_cache = 1; | |
fbe9dafd MC |
2317 | SSL_CTX_free(sctx); |
2318 | SSL_CTX_free(cctx); | |
2319 | sctx = cctx = NULL; | |
03cdf559 | 2320 | |
fbe9dafd | 2321 | if (!setup_ticket_test(stateful, idx, &sctx, &cctx)) |
03cdf559 | 2322 | goto end; |
36ff232c MC |
2323 | |
2324 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
2325 | &clientssl, NULL, NULL))) | |
2326 | goto end; | |
2327 | ||
32097b33 | 2328 | SSL_set_post_handshake_auth(clientssl, 1); |
36ff232c MC |
2329 | |
2330 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
2331 | SSL_ERROR_NONE)) | |
2332 | /* Check we got the number of tickets we were expecting */ | |
2333 | || !TEST_int_eq(idx, new_called)) | |
2334 | goto end; | |
2335 | ||
2336 | /* After a post-handshake authentication we should get new tickets issued */ | |
c22365b3 | 2337 | if (!post_handshake_verify(serverssl, clientssl) |
36ff232c MC |
2338 | || !TEST_int_eq(idx * 2, new_called)) |
2339 | goto end; | |
2340 | ||
36ff232c MC |
2341 | SSL_shutdown(clientssl); |
2342 | SSL_shutdown(serverssl); | |
2343 | SSL_free(serverssl); | |
2344 | SSL_free(clientssl); | |
2345 | serverssl = clientssl = NULL; | |
2346 | ||
c22365b3 MC |
2347 | /* Stop caching sessions - just count them */ |
2348 | do_cache = 0; | |
2349 | ||
03cdf559 MC |
2350 | /* |
2351 | * Check we can resume with all the tickets we created. This time around the | |
2352 | * resumptions should all be successful. | |
2353 | */ | |
2354 | if (!check_resumption(idx, sctx, cctx, 1)) | |
2355 | goto end; | |
36ff232c MC |
2356 | |
2357 | testresult = 1; | |
2358 | ||
2359 | end: | |
2360 | SSL_free(serverssl); | |
2361 | SSL_free(clientssl); | |
c22365b3 | 2362 | for (j = 0; j < OSSL_NELEM(sesscache); j++) { |
36ff232c | 2363 | SSL_SESSION_free(sesscache[j]); |
c22365b3 MC |
2364 | sesscache[j] = NULL; |
2365 | } | |
36ff232c MC |
2366 | SSL_CTX_free(sctx); |
2367 | SSL_CTX_free(cctx); | |
2368 | ||
2369 | return testresult; | |
2370 | } | |
04d7814a MC |
2371 | |
2372 | static int test_stateless_tickets(int idx) | |
2373 | { | |
2374 | return test_tickets(0, idx); | |
2375 | } | |
2376 | ||
2377 | static int test_stateful_tickets(int idx) | |
2378 | { | |
2379 | return test_tickets(1, idx); | |
2380 | } | |
8614a4eb MC |
2381 | |
2382 | static int test_psk_tickets(void) | |
2383 | { | |
2384 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
2385 | SSL *serverssl = NULL, *clientssl = NULL; | |
2386 | int testresult = 0; | |
2387 | int sess_id_ctx = 1; | |
2388 | ||
5e30f2fd MC |
2389 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
2390 | TLS_client_method(), TLS1_VERSION, 0, | |
2391 | &sctx, &cctx, NULL, NULL)) | |
8614a4eb MC |
2392 | || !TEST_true(SSL_CTX_set_session_id_context(sctx, |
2393 | (void *)&sess_id_ctx, | |
2394 | sizeof(sess_id_ctx)))) | |
2395 | goto end; | |
2396 | ||
2397 | SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT | |
2398 | | SSL_SESS_CACHE_NO_INTERNAL_STORE); | |
2399 | SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb); | |
2400 | SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb); | |
2401 | SSL_CTX_sess_set_new_cb(cctx, new_session_cb); | |
2402 | use_session_cb_cnt = 0; | |
2403 | find_session_cb_cnt = 0; | |
2404 | srvid = pskid; | |
2405 | new_called = 0; | |
2406 | ||
2407 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
2408 | NULL, NULL))) | |
2409 | goto end; | |
2410 | clientpsk = serverpsk = create_a_psk(clientssl); | |
2411 | if (!TEST_ptr(clientpsk)) | |
2412 | goto end; | |
2413 | SSL_SESSION_up_ref(clientpsk); | |
2414 | ||
2415 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
2416 | SSL_ERROR_NONE)) | |
2417 | || !TEST_int_eq(1, find_session_cb_cnt) | |
2418 | || !TEST_int_eq(1, use_session_cb_cnt) | |
2419 | /* We should always get 1 ticket when using external PSK */ | |
2420 | || !TEST_int_eq(1, new_called)) | |
2421 | goto end; | |
2422 | ||
2423 | testresult = 1; | |
2424 | ||
2425 | end: | |
2426 | SSL_free(serverssl); | |
2427 | SSL_free(clientssl); | |
2428 | SSL_CTX_free(sctx); | |
2429 | SSL_CTX_free(cctx); | |
2430 | SSL_SESSION_free(clientpsk); | |
2431 | SSL_SESSION_free(serverpsk); | |
2432 | clientpsk = serverpsk = NULL; | |
2433 | ||
2434 | return testresult; | |
2435 | } | |
f0049b86 BK |
2436 | |
2437 | static int test_extra_tickets(int idx) | |
2438 | { | |
2439 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
2440 | SSL *serverssl = NULL, *clientssl = NULL; | |
2441 | BIO *bretry = BIO_new(bio_s_always_retry()); | |
2442 | BIO *tmp = NULL; | |
2443 | int testresult = 0; | |
2444 | int stateful = 0; | |
2445 | size_t nbytes; | |
2446 | unsigned char c, buf[1]; | |
2447 | ||
2448 | new_called = 0; | |
2449 | do_cache = 1; | |
2450 | ||
2451 | if (idx >= 3) { | |
2452 | idx -= 3; | |
2453 | stateful = 1; | |
2454 | } | |
2455 | ||
2456 | if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx)) | |
2457 | goto end; | |
2458 | SSL_CTX_sess_set_new_cb(sctx, new_session_cb); | |
2459 | /* setup_ticket_test() uses new_cachesession_cb which we don't need. */ | |
2460 | SSL_CTX_sess_set_new_cb(cctx, new_session_cb); | |
2461 | ||
2462 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
2463 | &clientssl, NULL, NULL))) | |
2464 | goto end; | |
2465 | ||
2466 | /* | |
2467 | * Note that we have new_session_cb on both sctx and cctx, so new_called is | |
2468 | * incremented by both client and server. | |
2469 | */ | |
2470 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
2471 | SSL_ERROR_NONE)) | |
2472 | /* Check we got the number of tickets we were expecting */ | |
2473 | || !TEST_int_eq(idx * 2, new_called) | |
2474 | || !TEST_true(SSL_new_session_ticket(serverssl)) | |
2475 | || !TEST_true(SSL_new_session_ticket(serverssl)) | |
2476 | || !TEST_int_eq(idx * 2, new_called)) | |
2477 | goto end; | |
2478 | ||
2479 | /* Now try a (real) write to actually send the tickets */ | |
2480 | c = '1'; | |
2481 | if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) | |
2482 | || !TEST_size_t_eq(1, nbytes) | |
2483 | || !TEST_int_eq(idx * 2 + 2, new_called) | |
2484 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) | |
2485 | || !TEST_int_eq(idx * 2 + 4, new_called) | |
2486 | || !TEST_int_eq(sizeof(buf), nbytes) | |
2487 | || !TEST_int_eq(c, buf[0]) | |
2488 | || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) | |
2489 | goto end; | |
2490 | ||
2491 | /* Try with only requesting one new ticket, too */ | |
2492 | c = '2'; | |
2493 | new_called = 0; | |
2494 | if (!TEST_true(SSL_new_session_ticket(serverssl)) | |
2495 | || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes)) | |
2496 | || !TEST_size_t_eq(sizeof(c), nbytes) | |
2497 | || !TEST_int_eq(1, new_called) | |
2498 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) | |
2499 | || !TEST_int_eq(2, new_called) | |
2500 | || !TEST_size_t_eq(sizeof(buf), nbytes) | |
2501 | || !TEST_int_eq(c, buf[0])) | |
2502 | goto end; | |
2503 | ||
2504 | /* Do it again but use dummy writes to drive the ticket generation */ | |
2505 | c = '3'; | |
2506 | new_called = 0; | |
2507 | if (!TEST_true(SSL_new_session_ticket(serverssl)) | |
2508 | || !TEST_true(SSL_new_session_ticket(serverssl)) | |
2509 | || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes)) | |
2510 | || !TEST_size_t_eq(0, nbytes) | |
2511 | || !TEST_int_eq(2, new_called) | |
2512 | || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) | |
2513 | || !TEST_int_eq(4, new_called)) | |
2514 | goto end; | |
2515 | ||
2516 | /* | |
2517 | * Use the always-retry BIO to exercise the logic that forces ticket | |
2518 | * generation to wait until a record boundary. | |
2519 | */ | |
2520 | c = '4'; | |
2521 | new_called = 0; | |
2522 | tmp = SSL_get_wbio(serverssl); | |
2523 | if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { | |
2524 | tmp = NULL; | |
2525 | goto end; | |
2526 | } | |
2527 | SSL_set0_wbio(serverssl, bretry); | |
2528 | bretry = NULL; | |
2529 | if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes)) | |
2530 | || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE) | |
2531 | || !TEST_size_t_eq(nbytes, 0)) | |
2532 | goto end; | |
2533 | /* Restore a BIO that will let the write succeed */ | |
2534 | SSL_set0_wbio(serverssl, tmp); | |
2535 | tmp = NULL; | |
2536 | /* These calls should just queue the request and not send anything. */ | |
2537 | if (!TEST_true(SSL_new_session_ticket(serverssl)) | |
2538 | || !TEST_true(SSL_new_session_ticket(serverssl)) | |
2539 | || !TEST_int_eq(0, new_called)) | |
2540 | goto end; | |
2541 | /* Re-do the write; still no tickets sent */ | |
2542 | if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) | |
2543 | || !TEST_size_t_eq(1, nbytes) | |
2544 | || !TEST_int_eq(0, new_called) | |
2545 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) | |
2546 | || !TEST_int_eq(0, new_called) | |
2547 | || !TEST_int_eq(sizeof(buf), nbytes) | |
2548 | || !TEST_int_eq(c, buf[0]) | |
2549 | || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) | |
2550 | goto end; | |
2551 | /* Now the *next* write should send the tickets */ | |
2552 | c = '5'; | |
2553 | if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes)) | |
2554 | || !TEST_size_t_eq(1, nbytes) | |
2555 | || !TEST_int_eq(2, new_called) | |
2556 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)) | |
2557 | || !TEST_int_eq(4, new_called) | |
2558 | || !TEST_int_eq(sizeof(buf), nbytes) | |
2559 | || !TEST_int_eq(c, buf[0]) | |
2560 | || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))) | |
2561 | goto end; | |
2562 | ||
2563 | SSL_shutdown(clientssl); | |
2564 | SSL_shutdown(serverssl); | |
2565 | testresult = 1; | |
2566 | ||
2567 | end: | |
2568 | BIO_free(bretry); | |
2569 | BIO_free(tmp); | |
2570 | SSL_free(serverssl); | |
2571 | SSL_free(clientssl); | |
2572 | SSL_CTX_free(sctx); | |
2573 | SSL_CTX_free(cctx); | |
2574 | clientssl = serverssl = NULL; | |
2575 | sctx = cctx = NULL; | |
2576 | return testresult; | |
2577 | } | |
04225915 | 2578 | #endif |
36ff232c | 2579 | |
7d4488bb MC |
2580 | #define USE_NULL 0 |
2581 | #define USE_BIO_1 1 | |
2582 | #define USE_BIO_2 2 | |
2583 | #define USE_DEFAULT 3 | |
2584 | ||
2585 | #define CONNTYPE_CONNECTION_SUCCESS 0 | |
2586 | #define CONNTYPE_CONNECTION_FAIL 1 | |
2587 | #define CONNTYPE_NO_CONNECTION 2 | |
2588 | ||
2589 | #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3) | |
2590 | #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2) | |
2591 | #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) | |
2592 | # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2) | |
2593 | #else | |
2594 | # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0 | |
2595 | #endif | |
2596 | ||
7d4488bb MC |
2597 | #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \ |
2598 | + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \ | |
2599 | + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS | |
7fb4c820 MC |
2600 | |
2601 | static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type) | |
2602 | { | |
2603 | switch (type) { | |
2604 | case USE_NULL: | |
2605 | *res = NULL; | |
2606 | break; | |
2607 | case USE_BIO_1: | |
2608 | *res = bio1; | |
2609 | break; | |
2610 | case USE_BIO_2: | |
2611 | *res = bio2; | |
2612 | break; | |
2613 | } | |
2614 | } | |
2615 | ||
7d4488bb MC |
2616 | |
2617 | /* | |
2618 | * Tests calls to SSL_set_bio() under various conditions. | |
2619 | * | |
2620 | * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with | |
2621 | * various combinations of valid BIOs or NULL being set for the rbio/wbio. We | |
2622 | * then do more tests where we create a successful connection first using our | |
2623 | * standard connection setup functions, and then call SSL_set_bio() with | |
2624 | * various combinations of valid BIOs or NULL. We then repeat these tests | |
2625 | * following a failed connection. In this last case we are looking to check that | |
2626 | * SSL_set_bio() functions correctly in the case where s->bbio is not NULL. | |
2627 | */ | |
7fb4c820 MC |
2628 | static int test_ssl_set_bio(int idx) |
2629 | { | |
7d4488bb | 2630 | SSL_CTX *sctx = NULL, *cctx = NULL; |
7fb4c820 MC |
2631 | BIO *bio1 = NULL; |
2632 | BIO *bio2 = NULL; | |
0fae8150 | 2633 | BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL; |
7d4488bb MC |
2634 | SSL *serverssl = NULL, *clientssl = NULL; |
2635 | int initrbio, initwbio, newrbio, newwbio, conntype; | |
7fb4c820 MC |
2636 | int testresult = 0; |
2637 | ||
7d4488bb MC |
2638 | if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) { |
2639 | initrbio = idx % 3; | |
2640 | idx /= 3; | |
2641 | initwbio = idx % 3; | |
2642 | idx /= 3; | |
2643 | newrbio = idx % 3; | |
2644 | idx /= 3; | |
2645 | newwbio = idx % 3; | |
2646 | conntype = CONNTYPE_NO_CONNECTION; | |
2647 | } else { | |
2648 | idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS; | |
2649 | initrbio = initwbio = USE_DEFAULT; | |
2650 | newrbio = idx % 2; | |
2651 | idx /= 2; | |
2652 | newwbio = idx % 2; | |
2653 | idx /= 2; | |
2654 | conntype = idx % 2; | |
2655 | } | |
710756a9 | 2656 | |
5e30f2fd MC |
2657 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
2658 | TLS_client_method(), TLS1_VERSION, 0, | |
7d4488bb MC |
2659 | &sctx, &cctx, cert, privkey))) |
2660 | goto end; | |
2661 | ||
2662 | if (conntype == CONNTYPE_CONNECTION_FAIL) { | |
2663 | /* | |
2664 | * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled | |
2665 | * because we reduced the number of tests in the definition of | |
2666 | * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting | |
2667 | * mismatched protocol versions we will force a connection failure. | |
2668 | */ | |
2669 | SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION); | |
2670 | SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); | |
2671 | } | |
2672 | ||
2673 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
2674 | NULL, NULL))) | |
710756a9 | 2675 | goto end; |
7fb4c820 | 2676 | |
710756a9 RS |
2677 | if (initrbio == USE_BIO_1 |
2678 | || initwbio == USE_BIO_1 | |
2679 | || newrbio == USE_BIO_1 | |
7fb4c820 | 2680 | || newwbio == USE_BIO_1) { |
710756a9 | 2681 | if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem()))) |
7fb4c820 | 2682 | goto end; |
7fb4c820 MC |
2683 | } |
2684 | ||
710756a9 RS |
2685 | if (initrbio == USE_BIO_2 |
2686 | || initwbio == USE_BIO_2 | |
2687 | || newrbio == USE_BIO_2 | |
7fb4c820 | 2688 | || newwbio == USE_BIO_2) { |
710756a9 | 2689 | if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem()))) |
7fb4c820 | 2690 | goto end; |
7fb4c820 MC |
2691 | } |
2692 | ||
7d4488bb MC |
2693 | if (initrbio != USE_DEFAULT) { |
2694 | setupbio(&irbio, bio1, bio2, initrbio); | |
2695 | setupbio(&iwbio, bio1, bio2, initwbio); | |
2696 | SSL_set_bio(clientssl, irbio, iwbio); | |
7fb4c820 | 2697 | |
7d4488bb MC |
2698 | /* |
2699 | * We want to maintain our own refs to these BIO, so do an up ref for | |
2700 | * each BIO that will have ownership transferred in the SSL_set_bio() | |
2701 | * call | |
2702 | */ | |
2703 | if (irbio != NULL) | |
2704 | BIO_up_ref(irbio); | |
2705 | if (iwbio != NULL && iwbio != irbio) | |
2706 | BIO_up_ref(iwbio); | |
2707 | } | |
7fb4c820 | 2708 | |
7d4488bb MC |
2709 | if (conntype != CONNTYPE_NO_CONNECTION |
2710 | && !TEST_true(create_ssl_connection(serverssl, clientssl, | |
2711 | SSL_ERROR_NONE) | |
2712 | == (conntype == CONNTYPE_CONNECTION_SUCCESS))) | |
2713 | goto end; | |
7fb4c820 MC |
2714 | |
2715 | setupbio(&nrbio, bio1, bio2, newrbio); | |
2716 | setupbio(&nwbio, bio1, bio2, newwbio); | |
2717 | ||
2718 | /* | |
2719 | * We will (maybe) transfer ownership again so do more up refs. | |
2720 | * SSL_set_bio() has some really complicated ownership rules where BIOs have | |
2721 | * already been set! | |
2722 | */ | |
710756a9 RS |
2723 | if (nrbio != NULL |
2724 | && nrbio != irbio | |
2725 | && (nwbio != iwbio || nrbio != nwbio)) | |
7fb4c820 | 2726 | BIO_up_ref(nrbio); |
710756a9 RS |
2727 | if (nwbio != NULL |
2728 | && nwbio != nrbio | |
2729 | && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio))) | |
7fb4c820 MC |
2730 | BIO_up_ref(nwbio); |
2731 | ||
7d4488bb | 2732 | SSL_set_bio(clientssl, nrbio, nwbio); |
7fb4c820 MC |
2733 | |
2734 | testresult = 1; | |
2735 | ||
2736 | end: | |
7fb4c820 MC |
2737 | BIO_free(bio1); |
2738 | BIO_free(bio2); | |
710756a9 | 2739 | |
7fb4c820 MC |
2740 | /* |
2741 | * This test is checking that the ref counting for SSL_set_bio is correct. | |
2742 | * If we get here and we did too many frees then we will fail in the above | |
742ccab3 | 2743 | * functions. |
7fb4c820 | 2744 | */ |
7d4488bb MC |
2745 | SSL_free(serverssl); |
2746 | SSL_free(clientssl); | |
2747 | SSL_CTX_free(sctx); | |
2748 | SSL_CTX_free(cctx); | |
7fb4c820 MC |
2749 | return testresult; |
2750 | } | |
2751 | ||
7e885b7b | 2752 | typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t; |
9a716987 | 2753 | |
7e885b7b | 2754 | static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio) |
9a716987 MC |
2755 | { |
2756 | BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL; | |
710756a9 | 2757 | SSL_CTX *ctx; |
9a716987 MC |
2758 | SSL *ssl = NULL; |
2759 | int testresult = 0; | |
2760 | ||
5e30f2fd | 2761 | if (!TEST_ptr(ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_method())) |
710756a9 RS |
2762 | || !TEST_ptr(ssl = SSL_new(ctx)) |
2763 | || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl())) | |
2764 | || !TEST_ptr(membio1 = BIO_new(BIO_s_mem()))) | |
9a716987 | 2765 | goto end; |
9a716987 MC |
2766 | |
2767 | BIO_set_ssl(sslbio, ssl, BIO_CLOSE); | |
2768 | ||
2769 | /* | |
742ccab3 | 2770 | * If anything goes wrong here then we could leak memory. |
9a716987 MC |
2771 | */ |
2772 | BIO_push(sslbio, membio1); | |
2773 | ||
69687aa8 | 2774 | /* Verify changing the rbio/wbio directly does not cause leaks */ |
7e885b7b | 2775 | if (change_bio != NO_BIO_CHANGE) { |
710756a9 | 2776 | if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) |
9a716987 | 2777 | goto end; |
7e885b7b | 2778 | if (change_bio == CHANGE_RBIO) |
65e2d672 | 2779 | SSL_set0_rbio(ssl, membio2); |
9a716987 | 2780 | else |
65e2d672 | 2781 | SSL_set0_wbio(ssl, membio2); |
9a716987 MC |
2782 | } |
2783 | ssl = NULL; | |
2784 | ||
7e885b7b | 2785 | if (pop_ssl) |
9a716987 MC |
2786 | BIO_pop(sslbio); |
2787 | else | |
2788 | BIO_pop(membio1); | |
2789 | ||
2790 | testresult = 1; | |
2791 | end: | |
2792 | BIO_free(membio1); | |
2793 | BIO_free(sslbio); | |
2794 | SSL_free(ssl); | |
2795 | SSL_CTX_free(ctx); | |
2796 | ||
2797 | return testresult; | |
2798 | } | |
2799 | ||
2800 | static int test_ssl_bio_pop_next_bio(void) | |
2801 | { | |
7e885b7b | 2802 | return execute_test_ssl_bio(0, NO_BIO_CHANGE); |
9a716987 MC |
2803 | } |
2804 | ||
2805 | static int test_ssl_bio_pop_ssl_bio(void) | |
2806 | { | |
7e885b7b | 2807 | return execute_test_ssl_bio(1, NO_BIO_CHANGE); |
9a716987 MC |
2808 | } |
2809 | ||
2810 | static int test_ssl_bio_change_rbio(void) | |
2811 | { | |
7e885b7b | 2812 | return execute_test_ssl_bio(0, CHANGE_RBIO); |
9a716987 MC |
2813 | } |
2814 | ||
2815 | static int test_ssl_bio_change_wbio(void) | |
2816 | { | |
7e885b7b | 2817 | return execute_test_ssl_bio(0, CHANGE_WBIO); |
9a716987 MC |
2818 | } |
2819 | ||
c423ecaa | 2820 | #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) |
f1b25aae MC |
2821 | typedef struct { |
2822 | /* The list of sig algs */ | |
2823 | const int *list; | |
2824 | /* The length of the list */ | |
2825 | size_t listlen; | |
2826 | /* A sigalgs list in string format */ | |
2827 | const char *liststr; | |
2828 | /* Whether setting the list should succeed */ | |
2829 | int valid; | |
2830 | /* Whether creating a connection with the list should succeed */ | |
2831 | int connsuccess; | |
2832 | } sigalgs_list; | |
2833 | ||
2834 | static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA}; | |
c423ecaa | 2835 | # ifndef OPENSSL_NO_EC |
f1b25aae MC |
2836 | static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC}; |
2837 | static const int validlist3[] = {NID_sha512, EVP_PKEY_EC}; | |
c423ecaa | 2838 | # endif |
f1b25aae MC |
2839 | static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA}; |
2840 | static const int invalidlist2[] = {NID_sha256, NID_undef}; | |
2841 | static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256}; | |
2842 | static const int invalidlist4[] = {NID_sha256}; | |
2843 | static const sigalgs_list testsigalgs[] = { | |
2844 | {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1}, | |
c423ecaa | 2845 | # ifndef OPENSSL_NO_EC |
f1b25aae MC |
2846 | {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1}, |
2847 | {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0}, | |
c423ecaa | 2848 | # endif |
f1b25aae | 2849 | {NULL, 0, "RSA+SHA256", 1, 1}, |
c423ecaa | 2850 | # ifndef OPENSSL_NO_EC |
f1b25aae MC |
2851 | {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1}, |
2852 | {NULL, 0, "ECDSA+SHA512", 1, 0}, | |
c423ecaa | 2853 | # endif |
f1b25aae MC |
2854 | {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0}, |
2855 | {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0}, | |
2856 | {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0}, | |
2857 | {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0}, | |
2858 | {NULL, 0, "RSA", 0, 0}, | |
2859 | {NULL, 0, "SHA256", 0, 0}, | |
2860 | {NULL, 0, "RSA+SHA256:SHA256", 0, 0}, | |
710756a9 RS |
2861 | {NULL, 0, "Invalid", 0, 0} |
2862 | }; | |
f1b25aae MC |
2863 | |
2864 | static int test_set_sigalgs(int idx) | |
2865 | { | |
2866 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
2867 | SSL *clientssl = NULL, *serverssl = NULL; | |
2868 | int testresult = 0; | |
2869 | const sigalgs_list *curr; | |
2870 | int testctx; | |
2871 | ||
2872 | /* Should never happen */ | |
710756a9 | 2873 | if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2)) |
f1b25aae MC |
2874 | return 0; |
2875 | ||
2876 | testctx = ((size_t)idx < OSSL_NELEM(testsigalgs)); | |
2877 | curr = testctx ? &testsigalgs[idx] | |
2878 | : &testsigalgs[idx - OSSL_NELEM(testsigalgs)]; | |
2879 | ||
5e30f2fd MC |
2880 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
2881 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 2882 | &sctx, &cctx, cert, privkey))) |
f1b25aae | 2883 | return 0; |
f1b25aae | 2884 | |
d2e491f2 MC |
2885 | /* |
2886 | * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it | |
2887 | * for TLSv1.2 for now until we add a new API. | |
2888 | */ | |
2889 | SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION); | |
2890 | ||
f1b25aae MC |
2891 | if (testctx) { |
2892 | int ret; | |
710756a9 | 2893 | |
f1b25aae MC |
2894 | if (curr->list != NULL) |
2895 | ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen); | |
2896 | else | |
2897 | ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr); | |
2898 | ||
2899 | if (!ret) { | |
2900 | if (curr->valid) | |
710756a9 | 2901 | TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx); |
f1b25aae MC |
2902 | else |
2903 | testresult = 1; | |
2904 | goto end; | |
2905 | } | |
2906 | if (!curr->valid) { | |
710756a9 | 2907 | TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx); |
f1b25aae MC |
2908 | goto end; |
2909 | } | |
2910 | } | |
2911 | ||
710756a9 RS |
2912 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
2913 | &clientssl, NULL, NULL))) | |
f1b25aae | 2914 | goto end; |
f1b25aae MC |
2915 | |
2916 | if (!testctx) { | |
2917 | int ret; | |
2918 | ||
2919 | if (curr->list != NULL) | |
2920 | ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen); | |
2921 | else | |
2922 | ret = SSL_set1_sigalgs_list(clientssl, curr->liststr); | |
2923 | if (!ret) { | |
2924 | if (curr->valid) | |
710756a9 | 2925 | TEST_info("Failure setting sigalgs in SSL (%d)\n", idx); |
f1b25aae MC |
2926 | else |
2927 | testresult = 1; | |
2928 | goto end; | |
2929 | } | |
710756a9 | 2930 | if (!curr->valid) |
f1b25aae | 2931 | goto end; |
f1b25aae MC |
2932 | } |
2933 | ||
710756a9 RS |
2934 | if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl, |
2935 | SSL_ERROR_NONE), | |
2936 | curr->connsuccess)) | |
f1b25aae | 2937 | goto end; |
f1b25aae MC |
2938 | |
2939 | testresult = 1; | |
2940 | ||
2941 | end: | |
2942 | SSL_free(serverssl); | |
2943 | SSL_free(clientssl); | |
2944 | SSL_CTX_free(sctx); | |
2945 | SSL_CTX_free(cctx); | |
2946 | ||
2947 | return testresult; | |
2948 | } | |
c423ecaa | 2949 | #endif |
f1b25aae | 2950 | |
fff202e5 | 2951 | #ifndef OPENSSL_NO_TLS1_3 |
532f9578 MC |
2952 | static int psk_client_cb_cnt = 0; |
2953 | static int psk_server_cb_cnt = 0; | |
02a3ed5a MC |
2954 | |
2955 | static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id, | |
2956 | size_t *idlen, SSL_SESSION **sess) | |
2957 | { | |
2958 | switch (++use_session_cb_cnt) { | |
2959 | case 1: | |
2960 | /* The first call should always have a NULL md */ | |
2961 | if (md != NULL) | |
2962 | return 0; | |
2963 | break; | |
2964 | ||
2965 | case 2: | |
2966 | /* The second call should always have an md */ | |
2967 | if (md == NULL) | |
2968 | return 0; | |
2969 | break; | |
2970 | ||
2971 | default: | |
2972 | /* We should only be called a maximum of twice */ | |
2973 | return 0; | |
2974 | } | |
2975 | ||
57dee9bb MC |
2976 | if (clientpsk != NULL) |
2977 | SSL_SESSION_up_ref(clientpsk); | |
02a3ed5a | 2978 | |
57dee9bb | 2979 | *sess = clientpsk; |
02a3ed5a MC |
2980 | *id = (const unsigned char *)pskid; |
2981 | *idlen = strlen(pskid); | |
2982 | ||
2983 | return 1; | |
2984 | } | |
2985 | ||
c2b290c3 | 2986 | #ifndef OPENSSL_NO_PSK |
532f9578 MC |
2987 | static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id, |
2988 | unsigned int max_id_len, | |
2989 | unsigned char *psk, | |
2990 | unsigned int max_psk_len) | |
2991 | { | |
2992 | unsigned int psklen = 0; | |
2993 | ||
2994 | psk_client_cb_cnt++; | |
2995 | ||
2996 | if (strlen(pskid) + 1 > max_id_len) | |
2997 | return 0; | |
2998 | ||
2999 | /* We should only ever be called a maximum of twice per connection */ | |
3000 | if (psk_client_cb_cnt > 2) | |
3001 | return 0; | |
3002 | ||
3003 | if (clientpsk == NULL) | |
3004 | return 0; | |
3005 | ||
3006 | /* We'll reuse the PSK we set up for TLSv1.3 */ | |
3007 | if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len) | |
3008 | return 0; | |
3009 | psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len); | |
3010 | strncpy(id, pskid, max_id_len); | |
3011 | ||
3012 | return psklen; | |
3013 | } | |
c2b290c3 | 3014 | #endif /* OPENSSL_NO_PSK */ |
532f9578 | 3015 | |
02a3ed5a MC |
3016 | static int find_session_cb(SSL *ssl, const unsigned char *identity, |
3017 | size_t identity_len, SSL_SESSION **sess) | |
3018 | { | |
3019 | find_session_cb_cnt++; | |
3020 | ||
3021 | /* We should only ever be called a maximum of twice per connection */ | |
3022 | if (find_session_cb_cnt > 2) | |
3023 | return 0; | |
3024 | ||
57dee9bb | 3025 | if (serverpsk == NULL) |
02a3ed5a MC |
3026 | return 0; |
3027 | ||
3028 | /* Identity should match that set by the client */ | |
3029 | if (strlen(srvid) != identity_len | |
3030 | || strncmp(srvid, (const char *)identity, identity_len) != 0) { | |
3031 | /* No PSK found, continue but without a PSK */ | |
3032 | *sess = NULL; | |
3033 | return 1; | |
3034 | } | |
3035 | ||
57dee9bb MC |
3036 | SSL_SESSION_up_ref(serverpsk); |
3037 | *sess = serverpsk; | |
02a3ed5a MC |
3038 | |
3039 | return 1; | |
3040 | } | |
3041 | ||
c2b290c3 | 3042 | #ifndef OPENSSL_NO_PSK |
532f9578 MC |
3043 | static unsigned int psk_server_cb(SSL *ssl, const char *identity, |
3044 | unsigned char *psk, unsigned int max_psk_len) | |
3045 | { | |
3046 | unsigned int psklen = 0; | |
3047 | ||
3048 | psk_server_cb_cnt++; | |
3049 | ||
3050 | /* We should only ever be called a maximum of twice per connection */ | |
3051 | if (find_session_cb_cnt > 2) | |
3052 | return 0; | |
3053 | ||
3054 | if (serverpsk == NULL) | |
3055 | return 0; | |
3056 | ||
3057 | /* Identity should match that set by the client */ | |
3058 | if (strcmp(srvid, identity) != 0) { | |
3059 | return 0; | |
3060 | } | |
3061 | ||
3062 | /* We'll reuse the PSK we set up for TLSv1.3 */ | |
3063 | if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len) | |
3064 | return 0; | |
3065 | psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len); | |
3066 | ||
3067 | return psklen; | |
3068 | } | |
c2b290c3 | 3069 | #endif /* OPENSSL_NO_PSK */ |
532f9578 | 3070 | |
5f982038 MC |
3071 | #define MSG1 "Hello" |
3072 | #define MSG2 "World." | |
3073 | #define MSG3 "This" | |
3074 | #define MSG4 "is" | |
3075 | #define MSG5 "a" | |
90049cea MC |
3076 | #define MSG6 "test" |
3077 | #define MSG7 "message." | |
5f982038 | 3078 | |
532f9578 | 3079 | #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01") |
0b2b0be9 | 3080 | #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02") |
3081 | #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03") | |
3082 | #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04") | |
3083 | #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05") | |
02a3ed5a | 3084 | |
8614a4eb MC |
3085 | |
3086 | static SSL_SESSION *create_a_psk(SSL *ssl) | |
3087 | { | |
3088 | const SSL_CIPHER *cipher = NULL; | |
3089 | const unsigned char key[] = { | |
3090 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, | |
3091 | 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, | |
3092 | 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, | |
3093 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, | |
3094 | 0x2c, 0x2d, 0x2e, 0x2f | |
3095 | }; | |
3096 | SSL_SESSION *sess = NULL; | |
3097 | ||
3098 | cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES); | |
3099 | sess = SSL_SESSION_new(); | |
3100 | if (!TEST_ptr(sess) | |
3101 | || !TEST_ptr(cipher) | |
3102 | || !TEST_true(SSL_SESSION_set1_master_key(sess, key, | |
3103 | sizeof(key))) | |
3104 | || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)) | |
3105 | || !TEST_true( | |
3106 | SSL_SESSION_set_protocol_version(sess, | |
3107 | TLS1_3_VERSION))) { | |
3108 | SSL_SESSION_free(sess); | |
3109 | return NULL; | |
3110 | } | |
3111 | return sess; | |
3112 | } | |
3113 | ||
5f982038 MC |
3114 | /* |
3115 | * Helper method to setup objects for early data test. Caller frees objects on | |
3116 | * error. | |
3117 | */ | |
3118 | static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl, | |
3cb47b4e | 3119 | SSL **serverssl, SSL_SESSION **sess, int idx) |
5f982038 | 3120 | { |
5a421415 | 3121 | if (*sctx == NULL |
5e30f2fd | 3122 | && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5a421415 | 3123 | TLS_client_method(), |
5c587fb6 | 3124 | TLS1_VERSION, 0, |
5a421415 MC |
3125 | sctx, cctx, cert, privkey))) |
3126 | return 0; | |
3127 | ||
3128 | if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH))) | |
5f982038 | 3129 | return 0; |
5f982038 | 3130 | |
02a3ed5a MC |
3131 | if (idx == 1) { |
3132 | /* When idx == 1 we repeat the tests with read_ahead set */ | |
3cb47b4e MC |
3133 | SSL_CTX_set_read_ahead(*cctx, 1); |
3134 | SSL_CTX_set_read_ahead(*sctx, 1); | |
02a3ed5a MC |
3135 | } else if (idx == 2) { |
3136 | /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */ | |
3137 | SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb); | |
3138 | SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb); | |
3139 | use_session_cb_cnt = 0; | |
3140 | find_session_cb_cnt = 0; | |
3141 | srvid = pskid; | |
3cb47b4e MC |
3142 | } |
3143 | ||
710756a9 | 3144 | if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl, |
02a3ed5a MC |
3145 | NULL, NULL))) |
3146 | return 0; | |
3147 | ||
141e4709 MC |
3148 | /* |
3149 | * For one of the run throughs (doesn't matter which one), we'll try sending | |
3150 | * some SNI data in the initial ClientHello. This will be ignored (because | |
3151 | * there is no SNI cb set up by the server), so it should not impact | |
3152 | * early_data. | |
3153 | */ | |
3154 | if (idx == 1 | |
3155 | && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost"))) | |
3156 | return 0; | |
3157 | ||
02a3ed5a | 3158 | if (idx == 2) { |
8614a4eb | 3159 | clientpsk = create_a_psk(*clientssl); |
57dee9bb | 3160 | if (!TEST_ptr(clientpsk) |
02a3ed5a MC |
3161 | /* |
3162 | * We just choose an arbitrary value for max_early_data which | |
3163 | * should be big enough for testing purposes. | |
3164 | */ | |
57dee9bb MC |
3165 | || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk, |
3166 | 0x100)) | |
3167 | || !TEST_true(SSL_SESSION_up_ref(clientpsk))) { | |
3168 | SSL_SESSION_free(clientpsk); | |
3169 | clientpsk = NULL; | |
02a3ed5a MC |
3170 | return 0; |
3171 | } | |
57dee9bb | 3172 | serverpsk = clientpsk; |
02a3ed5a | 3173 | |
c20e3b28 MC |
3174 | if (sess != NULL) { |
3175 | if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) { | |
3176 | SSL_SESSION_free(clientpsk); | |
3177 | SSL_SESSION_free(serverpsk); | |
3178 | clientpsk = serverpsk = NULL; | |
3179 | return 0; | |
3180 | } | |
57dee9bb | 3181 | *sess = clientpsk; |
c20e3b28 | 3182 | } |
02a3ed5a MC |
3183 | return 1; |
3184 | } | |
3185 | ||
3186 | if (sess == NULL) | |
3187 | return 1; | |
3188 | ||
3189 | if (!TEST_true(create_ssl_connection(*serverssl, *clientssl, | |
3190 | SSL_ERROR_NONE))) | |
5f982038 | 3191 | return 0; |
5f982038 MC |
3192 | |
3193 | *sess = SSL_get1_session(*clientssl); | |
5f982038 MC |
3194 | SSL_shutdown(*clientssl); |
3195 | SSL_shutdown(*serverssl); | |
5f982038 MC |
3196 | SSL_free(*serverssl); |
3197 | SSL_free(*clientssl); | |
3198 | *serverssl = *clientssl = NULL; | |
3199 | ||
710756a9 RS |
3200 | if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, |
3201 | clientssl, NULL, NULL)) | |
3202 | || !TEST_true(SSL_set_session(*clientssl, *sess))) | |
5f982038 | 3203 | return 0; |
5f982038 MC |
3204 | |
3205 | return 1; | |
3206 | } | |
3207 | ||
3cb47b4e | 3208 | static int test_early_data_read_write(int idx) |
5f982038 MC |
3209 | { |
3210 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
3211 | SSL *clientssl = NULL, *serverssl = NULL; | |
3212 | int testresult = 0; | |
3213 | SSL_SESSION *sess = NULL; | |
9b5c865d MC |
3214 | unsigned char buf[20], data[1024]; |
3215 | size_t readbytes, written, eoedlen, rawread, rawwritten; | |
3216 | BIO *rbio; | |
5f982038 | 3217 | |
710756a9 RS |
3218 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, |
3219 | &serverssl, &sess, idx))) | |
5f982038 MC |
3220 | goto end; |
3221 | ||
3222 | /* Write and read some early data */ | |
710756a9 RS |
3223 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), |
3224 | &written)) | |
3225 | || !TEST_size_t_eq(written, strlen(MSG1)) | |
3226 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, | |
3227 | sizeof(buf), &readbytes), | |
3228 | SSL_READ_EARLY_DATA_SUCCESS) | |
3229 | || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1)) | |
3230 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
3231 | SSL_EARLY_DATA_ACCEPTED)) | |
5f982038 | 3232 | goto end; |
5f982038 MC |
3233 | |
3234 | /* | |
09f28874 | 3235 | * Server should be able to write data, and client should be able to |
5f982038 MC |
3236 | * read it. |
3237 | */ | |
710756a9 RS |
3238 | if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2), |
3239 | &written)) | |
3240 | || !TEST_size_t_eq(written, strlen(MSG2)) | |
3241 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) | |
3242 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
5f982038 | 3243 | goto end; |
5f982038 MC |
3244 | |
3245 | /* Even after reading normal data, client should be able write early data */ | |
710756a9 RS |
3246 | if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3), |
3247 | &written)) | |
3248 | || !TEST_size_t_eq(written, strlen(MSG3))) | |
5f982038 | 3249 | goto end; |
5f982038 | 3250 | |
09f28874 | 3251 | /* Server should still be able read early data after writing data */ |
710756a9 RS |
3252 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3253 | &readbytes), | |
3254 | SSL_READ_EARLY_DATA_SUCCESS) | |
3255 | || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3))) | |
5f982038 | 3256 | goto end; |
5f982038 | 3257 | |
09f28874 | 3258 | /* Write more data from server and read it from client */ |
710756a9 RS |
3259 | if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4), |
3260 | &written)) | |
3261 | || !TEST_size_t_eq(written, strlen(MSG4)) | |
3262 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) | |
3263 | || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4))) | |
5f982038 | 3264 | goto end; |
5f982038 MC |
3265 | |
3266 | /* | |
3267 | * If client writes normal data it should mean writing early data is no | |
3268 | * longer possible. | |
3269 | */ | |
710756a9 RS |
3270 | if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written)) |
3271 | || !TEST_size_t_eq(written, strlen(MSG5)) | |
3272 | || !TEST_int_eq(SSL_get_early_data_status(clientssl), | |
3273 | SSL_EARLY_DATA_ACCEPTED)) | |
5f982038 | 3274 | goto end; |
5f982038 | 3275 | |
9b5c865d MC |
3276 | /* |
3277 | * At this point the client has written EndOfEarlyData, ClientFinished and | |
3278 | * normal (fully protected) data. We are going to cause a delay between the | |
3279 | * arrival of EndOfEarlyData and ClientFinished. We read out all the data | |
3280 | * in the read BIO, and then just put back the EndOfEarlyData message. | |
3281 | */ | |
3282 | rbio = SSL_get_rbio(serverssl); | |
710756a9 RS |
3283 | if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread)) |
3284 | || !TEST_size_t_lt(rawread, sizeof(data)) | |
3285 | || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH)) | |
9b5c865d | 3286 | goto end; |
710756a9 | 3287 | |
9b5c865d MC |
3288 | /* Record length is in the 4th and 5th bytes of the record header */ |
3289 | eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]); | |
710756a9 RS |
3290 | if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten)) |
3291 | || !TEST_size_t_eq(rawwritten, eoedlen)) | |
9b5c865d | 3292 | goto end; |
9b5c865d | 3293 | |
5f982038 | 3294 | /* Server should be told that there is no more early data */ |
710756a9 RS |
3295 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3296 | &readbytes), | |
3297 | SSL_READ_EARLY_DATA_FINISH) | |
3298 | || !TEST_size_t_eq(readbytes, 0)) | |
5f982038 | 3299 | goto end; |
5f982038 | 3300 | |
9b5c865d MC |
3301 | /* |
3302 | * Server has not finished init yet, so should still be able to write early | |
3303 | * data. | |
3304 | */ | |
710756a9 RS |
3305 | if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6), |
3306 | &written)) | |
3307 | || !TEST_size_t_eq(written, strlen(MSG6))) | |
9b5c865d | 3308 | goto end; |
9b5c865d | 3309 | |
f8a303fa | 3310 | /* Push the ClientFinished and the normal data back into the server rbio */ |
710756a9 RS |
3311 | if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen, |
3312 | &rawwritten)) | |
3313 | || !TEST_size_t_eq(rawwritten, rawread - eoedlen)) | |
f8a303fa | 3314 | goto end; |
f8a303fa | 3315 | |
5f982038 | 3316 | /* Server should be able to read normal data */ |
710756a9 RS |
3317 | if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) |
3318 | || !TEST_size_t_eq(readbytes, strlen(MSG5))) | |
5f982038 | 3319 | goto end; |
5f982038 | 3320 | |
09f28874 | 3321 | /* Client and server should not be able to write/read early data now */ |
710756a9 RS |
3322 | if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6), |
3323 | &written))) | |
5f982038 | 3324 | goto end; |
5f982038 | 3325 | ERR_clear_error(); |
710756a9 RS |
3326 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3327 | &readbytes), | |
3328 | SSL_READ_EARLY_DATA_ERROR)) | |
5f982038 | 3329 | goto end; |
5f982038 MC |
3330 | ERR_clear_error(); |
3331 | ||
9b5c865d | 3332 | /* Client should be able to read the data sent by the server */ |
710756a9 RS |
3333 | if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) |
3334 | || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6))) | |
9b5c865d | 3335 | goto end; |
710756a9 | 3336 | |
f8a303fa | 3337 | /* |
36ff232c MC |
3338 | * Make sure we process the two NewSessionTickets. These arrive |
3339 | * post-handshake. We attempt reads which we do not expect to return any | |
3340 | * data. | |
f8a303fa | 3341 | */ |
36ff232c MC |
3342 | if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) |
3343 | || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), | |
3344 | &readbytes))) | |
f8a303fa | 3345 | goto end; |
5f982038 | 3346 | |
90049cea | 3347 | /* Server should be able to write normal data */ |
710756a9 RS |
3348 | if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written)) |
3349 | || !TEST_size_t_eq(written, strlen(MSG7)) | |
3350 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) | |
3351 | || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7))) | |
90049cea | 3352 | goto end; |
90049cea | 3353 | |
c20e3b28 | 3354 | SSL_SESSION_free(sess); |
5f982038 | 3355 | sess = SSL_get1_session(clientssl); |
02a3ed5a MC |
3356 | use_session_cb_cnt = 0; |
3357 | find_session_cb_cnt = 0; | |
5f982038 MC |
3358 | |
3359 | SSL_shutdown(clientssl); | |
3360 | SSL_shutdown(serverssl); | |
5f982038 MC |
3361 | SSL_free(serverssl); |
3362 | SSL_free(clientssl); | |
3363 | serverssl = clientssl = NULL; | |
710756a9 RS |
3364 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
3365 | &clientssl, NULL, NULL)) | |
3366 | || !TEST_true(SSL_set_session(clientssl, sess))) | |
5f982038 | 3367 | goto end; |
5f982038 MC |
3368 | |
3369 | /* Write and read some early data */ | |
710756a9 RS |
3370 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), |
3371 | &written)) | |
3372 | || !TEST_size_t_eq(written, strlen(MSG1)) | |
3373 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
3374 | &readbytes), | |
3375 | SSL_READ_EARLY_DATA_SUCCESS) | |
3376 | || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))) | |
5f982038 | 3377 | goto end; |
5f982038 | 3378 | |
710756a9 RS |
3379 | if (!TEST_int_gt(SSL_connect(clientssl), 0) |
3380 | || !TEST_int_gt(SSL_accept(serverssl), 0)) | |
5f982038 | 3381 | goto end; |
5f982038 | 3382 | |
09f28874 | 3383 | /* Client and server should not be able to write/read early data now */ |
710756a9 RS |
3384 | if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6), |
3385 | &written))) | |
5f982038 | 3386 | goto end; |
5f982038 | 3387 | ERR_clear_error(); |
710756a9 RS |
3388 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3389 | &readbytes), | |
3390 | SSL_READ_EARLY_DATA_ERROR)) | |
5f982038 | 3391 | goto end; |
5f982038 MC |
3392 | ERR_clear_error(); |
3393 | ||
3394 | /* Client and server should be able to write/read normal data */ | |
710756a9 RS |
3395 | if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written)) |
3396 | || !TEST_size_t_eq(written, strlen(MSG5)) | |
3397 | || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
3398 | || !TEST_size_t_eq(readbytes, strlen(MSG5))) | |
5f982038 | 3399 | goto end; |
5f982038 MC |
3400 | |
3401 | testresult = 1; | |
3402 | ||
3403 | end: | |
c20e3b28 | 3404 | SSL_SESSION_free(sess); |
57dee9bb MC |
3405 | SSL_SESSION_free(clientpsk); |
3406 | SSL_SESSION_free(serverpsk); | |
3407 | clientpsk = serverpsk = NULL; | |
5f982038 MC |
3408 | SSL_free(serverssl); |
3409 | SSL_free(clientssl); | |
3410 | SSL_CTX_free(sctx); | |
3411 | SSL_CTX_free(cctx); | |
5f982038 MC |
3412 | return testresult; |
3413 | } | |
3414 | ||
5a421415 MC |
3415 | static int allow_ed_cb_called = 0; |
3416 | ||
3417 | static int allow_early_data_cb(SSL *s, void *arg) | |
3418 | { | |
3419 | int *usecb = (int *)arg; | |
3420 | ||
3421 | allow_ed_cb_called++; | |
3422 | ||
3423 | if (*usecb == 1) | |
3424 | return 0; | |
3425 | ||
3426 | return 1; | |
3427 | } | |
3428 | ||
3429 | /* | |
3430 | * idx == 0: Standard early_data setup | |
3431 | * idx == 1: early_data setup using read_ahead | |
3432 | * usecb == 0: Don't use a custom early data callback | |
3433 | * usecb == 1: Use a custom early data callback and reject the early data | |
3434 | * usecb == 2: Use a custom early data callback and accept the early data | |
3bb5e5b0 MC |
3435 | * confopt == 0: Configure anti-replay directly |
3436 | * confopt == 1: Configure anti-replay using SSL_CONF | |
5a421415 | 3437 | */ |
3bb5e5b0 | 3438 | static int test_early_data_replay_int(int idx, int usecb, int confopt) |
78fb5374 MC |
3439 | { |
3440 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
3441 | SSL *clientssl = NULL, *serverssl = NULL; | |
3442 | int testresult = 0; | |
3443 | SSL_SESSION *sess = NULL; | |
5a421415 MC |
3444 | size_t readbytes, written; |
3445 | unsigned char buf[20]; | |
3446 | ||
3447 | allow_ed_cb_called = 0; | |
3448 | ||
5e30f2fd MC |
3449 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
3450 | TLS_client_method(), TLS1_VERSION, 0, | |
3451 | &sctx, &cctx, cert, privkey))) | |
5a421415 MC |
3452 | return 0; |
3453 | ||
3454 | if (usecb > 0) { | |
3bb5e5b0 MC |
3455 | if (confopt == 0) { |
3456 | SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY); | |
3457 | } else { | |
3458 | SSL_CONF_CTX *confctx = SSL_CONF_CTX_new(); | |
3459 | ||
3460 | if (!TEST_ptr(confctx)) | |
3461 | goto end; | |
3462 | SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE | |
3463 | | SSL_CONF_FLAG_SERVER); | |
3464 | SSL_CONF_CTX_set_ssl_ctx(confctx, sctx); | |
3465 | if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"), | |
3466 | 2)) { | |
3467 | SSL_CONF_CTX_free(confctx); | |
3468 | goto end; | |
3469 | } | |
3470 | SSL_CONF_CTX_free(confctx); | |
3471 | } | |
5a421415 MC |
3472 | SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb); |
3473 | } | |
78fb5374 MC |
3474 | |
3475 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, | |
3476 | &serverssl, &sess, idx))) | |
3477 | goto end; | |
3478 | ||
3479 | /* | |
3480 | * The server is configured to accept early data. Create a connection to | |
3481 | * "use up" the ticket | |
3482 | */ | |
3483 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) | |
3484 | || !TEST_true(SSL_session_reused(clientssl))) | |
3485 | goto end; | |
3486 | ||
3487 | SSL_shutdown(clientssl); | |
3488 | SSL_shutdown(serverssl); | |
3489 | SSL_free(serverssl); | |
3490 | SSL_free(clientssl); | |
3491 | serverssl = clientssl = NULL; | |
3492 | ||
3493 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
3494 | &clientssl, NULL, NULL)) | |
5a421415 MC |
3495 | || !TEST_true(SSL_set_session(clientssl, sess))) |
3496 | goto end; | |
3497 | ||
3498 | /* Write and read some early data */ | |
3499 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), | |
3500 | &written)) | |
3501 | || !TEST_size_t_eq(written, strlen(MSG1))) | |
3502 | goto end; | |
3503 | ||
3504 | if (usecb <= 1) { | |
3505 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
3506 | &readbytes), | |
3507 | SSL_READ_EARLY_DATA_FINISH) | |
3508 | /* | |
3509 | * The ticket was reused, so the we should have rejected the | |
3510 | * early data | |
3511 | */ | |
3512 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
3513 | SSL_EARLY_DATA_REJECTED)) | |
3514 | goto end; | |
3515 | } else { | |
3516 | /* In this case the callback decides to accept the early data */ | |
3517 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
3518 | &readbytes), | |
3519 | SSL_READ_EARLY_DATA_SUCCESS) | |
3520 | || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes) | |
3521 | /* | |
3522 | * Server will have sent its flight so client can now send | |
3523 | * end of early data and complete its half of the handshake | |
3524 | */ | |
3525 | || !TEST_int_gt(SSL_connect(clientssl), 0) | |
3526 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
3527 | &readbytes), | |
3528 | SSL_READ_EARLY_DATA_FINISH) | |
3529 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
3530 | SSL_EARLY_DATA_ACCEPTED)) | |
3531 | goto end; | |
3532 | } | |
3533 | ||
3534 | /* Complete the connection */ | |
3535 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) | |
3536 | || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0) | |
3537 | || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0)) | |
78fb5374 MC |
3538 | goto end; |
3539 | ||
3540 | testresult = 1; | |
3541 | ||
3542 | end: | |
c20e3b28 | 3543 | SSL_SESSION_free(sess); |
78fb5374 MC |
3544 | SSL_SESSION_free(clientpsk); |
3545 | SSL_SESSION_free(serverpsk); | |
3546 | clientpsk = serverpsk = NULL; | |
3547 | SSL_free(serverssl); | |
3548 | SSL_free(clientssl); | |
3549 | SSL_CTX_free(sctx); | |
3550 | SSL_CTX_free(cctx); | |
3551 | return testresult; | |
3552 | } | |
3553 | ||
5a421415 MC |
3554 | static int test_early_data_replay(int idx) |
3555 | { | |
3bb5e5b0 | 3556 | int ret = 1, usecb, confopt; |
5a421415 | 3557 | |
3bb5e5b0 MC |
3558 | for (usecb = 0; usecb < 3; usecb++) { |
3559 | for (confopt = 0; confopt < 2; confopt++) | |
3560 | ret &= test_early_data_replay_int(idx, usecb, confopt); | |
3561 | } | |
5a421415 MC |
3562 | |
3563 | return ret; | |
3564 | } | |
3565 | ||
710756a9 | 3566 | /* |
6b84e6bf MC |
3567 | * Helper function to test that a server attempting to read early data can |
3568 | * handle a connection from a client where the early data should be skipped. | |
0d1b7789 MC |
3569 | * testtype: 0 == No HRR |
3570 | * testtype: 1 == HRR | |
0efa0ba4 MC |
3571 | * testtype: 2 == HRR, invalid early_data sent after HRR |
3572 | * testtype: 3 == recv_max_early_data set to 0 | |
710756a9 | 3573 | */ |
0d1b7789 | 3574 | static int early_data_skip_helper(int testtype, int idx) |
5f982038 MC |
3575 | { |
3576 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
3577 | SSL *clientssl = NULL, *serverssl = NULL; | |
3578 | int testresult = 0; | |
018fcbec | 3579 | SSL_SESSION *sess = NULL; |
5f982038 MC |
3580 | unsigned char buf[20]; |
3581 | size_t readbytes, written; | |
3582 | ||
710756a9 RS |
3583 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, |
3584 | &serverssl, &sess, idx))) | |
5f982038 MC |
3585 | goto end; |
3586 | ||
0efa0ba4 | 3587 | if (testtype == 1 || testtype == 2) { |
6b84e6bf | 3588 | /* Force an HRR to occur */ |
dbc6268f MC |
3589 | #if defined(OPENSSL_NO_EC) |
3590 | if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072"))) | |
3591 | goto end; | |
3592 | #else | |
6b84e6bf MC |
3593 | if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256"))) |
3594 | goto end; | |
dbc6268f | 3595 | #endif |
02a3ed5a MC |
3596 | } else if (idx == 2) { |
3597 | /* | |
3598 | * We force early_data rejection by ensuring the PSK identity is | |
3599 | * unrecognised | |
3600 | */ | |
3601 | srvid = "Dummy Identity"; | |
6b84e6bf MC |
3602 | } else { |
3603 | /* | |
3604 | * Deliberately corrupt the creation time. We take 20 seconds off the | |
3605 | * time. It could be any value as long as it is not within tolerance. | |
3606 | * This should mean the ticket is rejected. | |
3607 | */ | |
5d99881e | 3608 | if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20)))) |
6b84e6bf MC |
3609 | goto end; |
3610 | } | |
5f982038 | 3611 | |
0efa0ba4 | 3612 | if (testtype == 3 |
0d1b7789 MC |
3613 | && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0))) |
3614 | goto end; | |
3615 | ||
5f982038 | 3616 | /* Write some early data */ |
710756a9 RS |
3617 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), |
3618 | &written)) | |
3619 | || !TEST_size_t_eq(written, strlen(MSG1))) | |
5f982038 | 3620 | goto end; |
5f982038 | 3621 | |
0d1b7789 | 3622 | /* Server should reject the early data */ |
710756a9 RS |
3623 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3624 | &readbytes), | |
3625 | SSL_READ_EARLY_DATA_FINISH) | |
3626 | || !TEST_size_t_eq(readbytes, 0) | |
3627 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
3628 | SSL_EARLY_DATA_REJECTED)) | |
5f982038 | 3629 | goto end; |
5f982038 | 3630 | |
0efa0ba4 MC |
3631 | switch (testtype) { |
3632 | case 0: | |
3633 | /* Nothing to do */ | |
3634 | break; | |
3635 | ||
3636 | case 1: | |
6b84e6bf MC |
3637 | /* |
3638 | * Finish off the handshake. We perform the same writes and reads as | |
3639 | * further down but we expect them to fail due to the incomplete | |
3640 | * handshake. | |
3641 | */ | |
3642 | if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) | |
3643 | || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), | |
3644 | &readbytes))) | |
3645 | goto end; | |
0efa0ba4 MC |
3646 | break; |
3647 | ||
3648 | case 2: | |
3649 | { | |
3650 | BIO *wbio = SSL_get_wbio(clientssl); | |
3651 | /* A record that will appear as bad early_data */ | |
3652 | const unsigned char bad_early_data[] = { | |
3653 | 0x17, 0x03, 0x03, 0x00, 0x01, 0x00 | |
3654 | }; | |
3655 | ||
3656 | /* | |
3657 | * We force the client to attempt a write. This will fail because | |
3658 | * we're still in the handshake. It will cause the second | |
3659 | * ClientHello to be sent. | |
3660 | */ | |
3661 | if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), | |
3662 | &written))) | |
3663 | goto end; | |
3664 | ||
3665 | /* | |
3666 | * Inject some early_data after the second ClientHello. This should | |
3667 | * cause the server to fail | |
3668 | */ | |
3669 | if (!TEST_true(BIO_write_ex(wbio, bad_early_data, | |
3670 | sizeof(bad_early_data), &written))) | |
3671 | goto end; | |
3672 | } | |
3673 | /* fallthrough */ | |
3674 | ||
3675 | case 3: | |
0d1b7789 | 3676 | /* |
0efa0ba4 MC |
3677 | * This client has sent more early_data than we are willing to skip |
3678 | * (case 3) or sent invalid early_data (case 2) so the connection should | |
3679 | * abort. | |
0d1b7789 MC |
3680 | */ |
3681 | if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
3682 | || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL)) | |
3683 | goto end; | |
3684 | ||
3685 | /* Connection has failed - nothing more to do */ | |
3686 | testresult = 1; | |
3687 | goto end; | |
0efa0ba4 MC |
3688 | |
3689 | default: | |
3690 | TEST_error("Invalid test type"); | |
3691 | goto end; | |
6b84e6bf MC |
3692 | } |
3693 | ||
0d1b7789 MC |
3694 | /* |
3695 | * Should be able to send normal data despite rejection of early data. The | |
3696 | * early_data should be skipped. | |
3697 | */ | |
710756a9 RS |
3698 | if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) |
3699 | || !TEST_size_t_eq(written, strlen(MSG2)) | |
3700 | || !TEST_int_eq(SSL_get_early_data_status(clientssl), | |
3701 | SSL_EARLY_DATA_REJECTED) | |
3702 | || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
3703 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
5f982038 | 3704 | goto end; |
5f982038 MC |
3705 | |
3706 | testresult = 1; | |
3707 | ||
3708 | end: | |
c20e3b28 | 3709 | SSL_SESSION_free(clientpsk); |
57dee9bb MC |
3710 | SSL_SESSION_free(serverpsk); |
3711 | clientpsk = serverpsk = NULL; | |
5f982038 MC |
3712 | SSL_SESSION_free(sess); |
3713 | SSL_free(serverssl); | |
3714 | SSL_free(clientssl); | |
3715 | SSL_CTX_free(sctx); | |
3716 | SSL_CTX_free(cctx); | |
5f982038 MC |
3717 | return testresult; |
3718 | } | |
3719 | ||
6b84e6bf MC |
3720 | /* |
3721 | * Test that a server attempting to read early data can handle a connection | |
3722 | * from a client where the early data is not acceptable. | |
3723 | */ | |
3724 | static int test_early_data_skip(int idx) | |
3725 | { | |
3726 | return early_data_skip_helper(0, idx); | |
3727 | } | |
3728 | ||
3729 | /* | |
3730 | * Test that a server attempting to read early data can handle a connection | |
3731 | * from a client where an HRR occurs. | |
3732 | */ | |
3733 | static int test_early_data_skip_hrr(int idx) | |
3734 | { | |
3735 | return early_data_skip_helper(1, idx); | |
3736 | } | |
3737 | ||
0efa0ba4 MC |
3738 | /* |
3739 | * Test that a server attempting to read early data can handle a connection | |
3740 | * from a client where an HRR occurs and correctly fails if early_data is sent | |
3741 | * after the HRR | |
3742 | */ | |
3743 | static int test_early_data_skip_hrr_fail(int idx) | |
3744 | { | |
3745 | return early_data_skip_helper(2, idx); | |
3746 | } | |
3747 | ||
0d1b7789 MC |
3748 | /* |
3749 | * Test that a server attempting to read early data will abort if it tries to | |
3750 | * skip over too much. | |
3751 | */ | |
3752 | static int test_early_data_skip_abort(int idx) | |
3753 | { | |
0efa0ba4 | 3754 | return early_data_skip_helper(3, idx); |
0d1b7789 MC |
3755 | } |
3756 | ||
710756a9 RS |
3757 | /* |
3758 | * Test that a server attempting to read early data can handle a connection | |
3759 | * from a client that doesn't send any. | |
3760 | */ | |
3cb47b4e | 3761 | static int test_early_data_not_sent(int idx) |
5f982038 MC |
3762 | { |
3763 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
3764 | SSL *clientssl = NULL, *serverssl = NULL; | |
3765 | int testresult = 0; | |
018fcbec | 3766 | SSL_SESSION *sess = NULL; |
5f982038 MC |
3767 | unsigned char buf[20]; |
3768 | size_t readbytes, written; | |
3769 | ||
710756a9 RS |
3770 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, |
3771 | &serverssl, &sess, idx))) | |
5f982038 MC |
3772 | goto end; |
3773 | ||
3774 | /* Write some data - should block due to handshake with server */ | |
3775 | SSL_set_connect_state(clientssl); | |
710756a9 | 3776 | if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))) |
5f982038 | 3777 | goto end; |
5f982038 MC |
3778 | |
3779 | /* Server should detect that early data has not been sent */ | |
710756a9 RS |
3780 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
3781 | &readbytes), | |
3782 | SSL_READ_EARLY_DATA_FINISH) | |
3783 | || !TEST_size_t_eq(readbytes, 0) | |
3784 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
3785 | SSL_EARLY_DATA_NOT_SENT) | |
3786 | || !TEST_int_eq(SSL_get_early_data_status(clientssl), | |
3787 | SSL_EARLY_DATA_NOT_SENT)) | |
5f982038 | 3788 | goto end; |
5f982038 MC |
3789 | |
3790 | /* Continue writing the message we started earlier */ | |
710756a9 RS |
3791 | if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) |
3792 | || !TEST_size_t_eq(written, strlen(MSG1)) | |
3793 | || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
3794 | || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) | |
3795 | || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written) | |
3796 | || !TEST_size_t_eq(written, strlen(MSG2))) | |
5f982038 | 3797 | goto end; |
5f982038 | 3798 | |
710756a9 RS |
3799 | if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) |
3800 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
5f982038 | 3801 | goto end; |
5f982038 MC |
3802 | |
3803 | testresult = 1; | |
3804 | ||
3805 | end: | |
5f982038 | 3806 | SSL_SESSION_free(sess); |
c20e3b28 | 3807 | SSL_SESSION_free(clientpsk); |
57dee9bb MC |
3808 | SSL_SESSION_free(serverpsk); |
3809 | clientpsk = serverpsk = NULL; | |
5f982038 MC |
3810 | SSL_free(serverssl); |
3811 | SSL_free(clientssl); | |
3812 | SSL_CTX_free(sctx); | |
3813 | SSL_CTX_free(cctx); | |
5f982038 MC |
3814 | return testresult; |
3815 | } | |
3816 | ||
976e5323 MC |
3817 | static const char *servalpn; |
3818 | ||
c7558d5b PY |
3819 | static int alpn_select_cb(SSL *ssl, const unsigned char **out, |
3820 | unsigned char *outlen, const unsigned char *in, | |
3821 | unsigned int inlen, void *arg) | |
976e5323 | 3822 | { |
c7558d5b | 3823 | unsigned int protlen = 0; |
976e5323 MC |
3824 | const unsigned char *prot; |
3825 | ||
c7558d5b PY |
3826 | for (prot = in; prot < in + inlen; prot += protlen) { |
3827 | protlen = *prot++; | |
5d99881e | 3828 | if (in + inlen < prot + protlen) |
976e5323 MC |
3829 | return SSL_TLSEXT_ERR_NOACK; |
3830 | ||
3831 | if (protlen == strlen(servalpn) | |
0bd42fde | 3832 | && memcmp(prot, servalpn, protlen) == 0) { |
976e5323 MC |
3833 | *out = prot; |
3834 | *outlen = protlen; | |
3835 | return SSL_TLSEXT_ERR_OK; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | return SSL_TLSEXT_ERR_NOACK; | |
3840 | } | |
3841 | ||
57dee9bb | 3842 | /* Test that a PSK can be used to send early_data */ |
976e5323 MC |
3843 | static int test_early_data_psk(int idx) |
3844 | { | |
3845 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
3846 | SSL *clientssl = NULL, *serverssl = NULL; | |
3847 | int testresult = 0; | |
3848 | SSL_SESSION *sess = NULL; | |
57dee9bb MC |
3849 | unsigned char alpnlist[] = { |
3850 | 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a', | |
3851 | 'l', 'p', 'n' | |
3852 | }; | |
3853 | #define GOODALPNLEN 9 | |
3854 | #define BADALPNLEN 8 | |
3855 | #define GOODALPN (alpnlist) | |
3856 | #define BADALPN (alpnlist + GOODALPNLEN) | |
3857 | int err = 0; | |
976e5323 MC |
3858 | unsigned char buf[20]; |
3859 | size_t readbytes, written; | |
57dee9bb | 3860 | int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1; |
976e5323 MC |
3861 | int edstatus = SSL_EARLY_DATA_ACCEPTED; |
3862 | ||
3863 | /* We always set this up with a final parameter of "2" for PSK */ | |
3864 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, | |
3865 | &serverssl, &sess, 2))) | |
3866 | goto end; | |
3867 | ||
976e5323 MC |
3868 | servalpn = "goodalpn"; |
3869 | ||
57dee9bb MC |
3870 | /* |
3871 | * Note: There is no test for inconsistent SNI with late client detection. | |
3872 | * This is because servers do not acknowledge SNI even if they are using | |
3873 | * it in a resumption handshake - so it is not actually possible for a | |
3874 | * client to detect a problem. | |
3875 | */ | |
976e5323 MC |
3876 | switch (idx) { |
3877 | case 0: | |
57dee9bb | 3878 | /* Set inconsistent SNI (early client detection) */ |
976e5323 MC |
3879 | err = SSL_R_INCONSISTENT_EARLY_DATA_SNI; |
3880 | if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost")) | |
3881 | || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost"))) | |
3882 | goto end; | |
3883 | break; | |
3884 | ||
3885 | case 1: | |
57dee9bb | 3886 | /* Set inconsistent ALPN (early client detection) */ |
976e5323 MC |
3887 | err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN; |
3888 | /* SSL_set_alpn_protos returns 0 for success and 1 for failure */ | |
57dee9bb MC |
3889 | if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN, |
3890 | GOODALPNLEN)) | |
3891 | || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN, | |
3892 | BADALPNLEN))) | |
976e5323 MC |
3893 | goto end; |
3894 | break; | |
3895 | ||
3896 | case 2: | |
3897 | /* | |
3898 | * Set invalid protocol version. Technically this affects PSKs without | |
3899 | * early_data too, but we test it here because it is similar to the | |
3900 | * SNI/ALPN consistency tests. | |
3901 | */ | |
3902 | err = SSL_R_BAD_PSK; | |
3903 | if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION))) | |
3904 | goto end; | |
3905 | break; | |
3906 | ||
3907 | case 3: | |
3908 | /* | |
49ef3d07 MC |
3909 | * Set inconsistent SNI (server side). In this case the connection |
3910 | * will succeed and accept early_data. In TLSv1.3 on the server side SNI | |
3911 | * is associated with each handshake - not the session. Therefore it | |
3912 | * should not matter that we used a different server name last time. | |
976e5323 | 3913 | */ |
281bf233 MC |
3914 | SSL_SESSION_free(serverpsk); |
3915 | serverpsk = SSL_SESSION_dup(clientpsk); | |
3916 | if (!TEST_ptr(serverpsk) | |
3917 | || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost"))) | |
3918 | goto end; | |
976e5323 MC |
3919 | /* Fall through */ |
3920 | case 4: | |
3921 | /* Set consistent SNI */ | |
976e5323 MC |
3922 | if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost")) |
3923 | || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")) | |
3924 | || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, | |
3925 | hostname_cb))) | |
3926 | goto end; | |
3927 | break; | |
3928 | ||
3929 | case 5: | |
3930 | /* | |
3931 | * Set inconsistent ALPN (server detected). In this case the connection | |
3932 | * will succeed but reject early_data. | |
3933 | */ | |
3934 | servalpn = "badalpn"; | |
3935 | edstatus = SSL_EARLY_DATA_REJECTED; | |
3936 | readearlyres = SSL_READ_EARLY_DATA_FINISH; | |
3937 | /* Fall through */ | |
3938 | case 6: | |
976e5323 | 3939 | /* |
57dee9bb | 3940 | * Set consistent ALPN. |
976e5323 MC |
3941 | * SSL_set_alpn_protos returns 0 for success and 1 for failure. It |
3942 | * accepts a list of protos (each one length prefixed). | |
3943 | * SSL_set1_alpn_selected accepts a single protocol (not length | |
3944 | * prefixed) | |
3945 | */ | |
57dee9bb MC |
3946 | if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1, |
3947 | GOODALPNLEN - 1)) | |
3948 | || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN, | |
3949 | GOODALPNLEN))) | |
976e5323 MC |
3950 | goto end; |
3951 | ||
3952 | SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL); | |
3953 | break; | |
3954 | ||
57dee9bb MC |
3955 | case 7: |
3956 | /* Set inconsistent ALPN (late client detection) */ | |
3957 | SSL_SESSION_free(serverpsk); | |
3958 | serverpsk = SSL_SESSION_dup(clientpsk); | |
3959 | if (!TEST_ptr(serverpsk) | |
3960 | || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk, | |
3961 | BADALPN + 1, | |
3962 | BADALPNLEN - 1)) | |
3963 | || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk, | |
3964 | GOODALPN + 1, | |
3965 | GOODALPNLEN - 1)) | |
3966 | || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist, | |
3967 | sizeof(alpnlist)))) | |
3968 | goto end; | |
3969 | SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL); | |
3970 | edstatus = SSL_EARLY_DATA_ACCEPTED; | |
3971 | readearlyres = SSL_READ_EARLY_DATA_SUCCESS; | |
3972 | /* SSL_connect() call should fail */ | |
3973 | connectres = -1; | |
3974 | break; | |
3975 | ||
976e5323 MC |
3976 | default: |
3977 | TEST_error("Bad test index"); | |
3978 | goto end; | |
3979 | } | |
3980 | ||
3981 | SSL_set_connect_state(clientssl); | |
3982 | if (err != 0) { | |
3983 | if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), | |
3984 | &written)) | |
3985 | || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL) | |
3986 | || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err)) | |
3987 | goto end; | |
3988 | } else { | |
3989 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), | |
57dee9bb MC |
3990 | &written))) |
3991 | goto end; | |
3992 | ||
3993 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
3994 | &readbytes), readearlyres) | |
976e5323 MC |
3995 | || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS |
3996 | && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))) | |
57dee9bb MC |
3997 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus) |
3998 | || !TEST_int_eq(SSL_connect(clientssl), connectres)) | |
976e5323 MC |
3999 | goto end; |
4000 | } | |
4001 | ||
4002 | testresult = 1; | |
4003 | ||
4004 | end: | |
c20e3b28 | 4005 | SSL_SESSION_free(sess); |
57dee9bb MC |
4006 | SSL_SESSION_free(clientpsk); |
4007 | SSL_SESSION_free(serverpsk); | |
4008 | clientpsk = serverpsk = NULL; | |
976e5323 MC |
4009 | SSL_free(serverssl); |
4010 | SSL_free(clientssl); | |
4011 | SSL_CTX_free(sctx); | |
4012 | SSL_CTX_free(cctx); | |
4013 | return testresult; | |
4014 | } | |
4015 | ||
0b2b0be9 | 4016 | /* |
4017 | * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites | |
4018 | * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256 | |
4019 | * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384 | |
4020 | * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256, | |
4021 | * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256 | |
4022 | * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256 | |
4023 | */ | |
4024 | static int test_early_data_psk_with_all_ciphers(int idx) | |
4025 | { | |
4026 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
4027 | SSL *clientssl = NULL, *serverssl = NULL; | |
4028 | int testresult = 0; | |
4029 | SSL_SESSION *sess = NULL; | |
4030 | unsigned char buf[20]; | |
4031 | size_t readbytes, written; | |
4032 | const SSL_CIPHER *cipher; | |
4033 | const char *cipher_str[] = { | |
4034 | TLS1_3_RFC_AES_128_GCM_SHA256, | |
4035 | TLS1_3_RFC_AES_256_GCM_SHA384, | |
4036 | # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) | |
4037 | TLS1_3_RFC_CHACHA20_POLY1305_SHA256, | |
4038 | # else | |
4039 | NULL, | |
4040 | # endif | |
4041 | TLS1_3_RFC_AES_128_CCM_SHA256, | |
4042 | TLS1_3_RFC_AES_128_CCM_8_SHA256 | |
4043 | }; | |
4044 | const unsigned char *cipher_bytes[] = { | |
4045 | TLS13_AES_128_GCM_SHA256_BYTES, | |
4046 | TLS13_AES_256_GCM_SHA384_BYTES, | |
4047 | # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) | |
4048 | TLS13_CHACHA20_POLY1305_SHA256_BYTES, | |
4049 | # else | |
4050 | NULL, | |
4051 | # endif | |
4052 | TLS13_AES_128_CCM_SHA256_BYTES, | |
4053 | TLS13_AES_128_CCM_8_SHA256_BYTES | |
4054 | }; | |
4055 | ||
4056 | if (cipher_str[idx] == NULL) | |
4057 | return 1; | |
4058 | /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */ | |
4059 | if (idx == 2 && is_fips == 1) | |
4060 | return 1; | |
4061 | ||
4062 | /* We always set this up with a final parameter of "2" for PSK */ | |
4063 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, | |
4064 | &serverssl, &sess, 2))) | |
4065 | goto end; | |
4066 | ||
4067 | if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx])) | |
4068 | || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx]))) | |
4069 | goto end; | |
4070 | ||
4071 | /* | |
4072 | * 'setupearly_data_test' creates only one instance of SSL_SESSION | |
4073 | * and assigns to both client and server with incremented reference | |
4074 | * and the same instance is updated in 'sess'. | |
4075 | * So updating ciphersuite in 'sess' which will get reflected in | |
4076 | * PSK handshake using psk use sess and find sess cb. | |
4077 | */ | |
4078 | cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]); | |
4079 | if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))) | |
4080 | goto end; | |
4081 | ||
4082 | SSL_set_connect_state(clientssl); | |
4083 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), | |
4084 | &written))) | |
4085 | goto end; | |
4086 | ||
4087 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
4088 | &readbytes), | |
4089 | SSL_READ_EARLY_DATA_SUCCESS) | |
4090 | || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) | |
4091 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
4092 | SSL_EARLY_DATA_ACCEPTED) | |
4093 | || !TEST_int_eq(SSL_connect(clientssl), 1) | |
4094 | || !TEST_int_eq(SSL_accept(serverssl), 1)) | |
4095 | goto end; | |
4096 | ||
4097 | /* Send some normal data from client to server */ | |
4098 | if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) | |
4099 | || !TEST_size_t_eq(written, strlen(MSG2))) | |
4100 | goto end; | |
4101 | ||
4102 | if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
4103 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
4104 | goto end; | |
4105 | ||
4106 | testresult = 1; | |
4107 | end: | |
4108 | SSL_SESSION_free(sess); | |
4109 | SSL_SESSION_free(clientpsk); | |
4110 | SSL_SESSION_free(serverpsk); | |
4111 | clientpsk = serverpsk = NULL; | |
4112 | if (clientssl != NULL) | |
4113 | SSL_shutdown(clientssl); | |
4114 | if (serverssl != NULL) | |
4115 | SSL_shutdown(serverssl); | |
4116 | SSL_free(serverssl); | |
4117 | SSL_free(clientssl); | |
4118 | SSL_CTX_free(sctx); | |
4119 | SSL_CTX_free(cctx); | |
4120 | return testresult; | |
4121 | } | |
4122 | ||
710756a9 RS |
4123 | /* |
4124 | * Test that a server that doesn't try to read early data can handle a | |
4125 | * client sending some. | |
4126 | */ | |
3cb47b4e | 4127 | static int test_early_data_not_expected(int idx) |
5f982038 MC |
4128 | { |
4129 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
4130 | SSL *clientssl = NULL, *serverssl = NULL; | |
4131 | int testresult = 0; | |
018fcbec | 4132 | SSL_SESSION *sess = NULL; |
5f982038 MC |
4133 | unsigned char buf[20]; |
4134 | size_t readbytes, written; | |
4135 | ||
710756a9 RS |
4136 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, |
4137 | &serverssl, &sess, idx))) | |
5f982038 MC |
4138 | goto end; |
4139 | ||
4140 | /* Write some early data */ | |
710756a9 RS |
4141 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), |
4142 | &written))) | |
5f982038 | 4143 | goto end; |
5f982038 MC |
4144 | |
4145 | /* | |
4146 | * Server should skip over early data and then block waiting for client to | |
4147 | * continue handshake | |
4148 | */ | |
710756a9 RS |
4149 | if (!TEST_int_le(SSL_accept(serverssl), 0) |
4150 | || !TEST_int_gt(SSL_connect(clientssl), 0) | |
4151 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
4152 | SSL_EARLY_DATA_REJECTED) | |
4153 | || !TEST_int_gt(SSL_accept(serverssl), 0) | |
4154 | || !TEST_int_eq(SSL_get_early_data_status(clientssl), | |
4155 | SSL_EARLY_DATA_REJECTED)) | |
5f982038 | 4156 | goto end; |
5f982038 MC |
4157 | |
4158 | /* Send some normal data from client to server */ | |
710756a9 RS |
4159 | if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)) |
4160 | || !TEST_size_t_eq(written, strlen(MSG2))) | |
5f982038 | 4161 | goto end; |
5f982038 | 4162 | |
710756a9 RS |
4163 | if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) |
4164 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
5f982038 | 4165 | goto end; |
5f982038 MC |
4166 | |
4167 | testresult = 1; | |
4168 | ||
4169 | end: | |
5f982038 | 4170 | SSL_SESSION_free(sess); |
c20e3b28 | 4171 | SSL_SESSION_free(clientpsk); |
57dee9bb MC |
4172 | SSL_SESSION_free(serverpsk); |
4173 | clientpsk = serverpsk = NULL; | |
5f982038 MC |
4174 | SSL_free(serverssl); |
4175 | SSL_free(clientssl); | |
4176 | SSL_CTX_free(sctx); | |
4177 | SSL_CTX_free(cctx); | |
5f982038 MC |
4178 | return testresult; |
4179 | } | |
4180 | ||
4181 | ||
4182 | # ifndef OPENSSL_NO_TLS1_2 | |
710756a9 RS |
4183 | /* |
4184 | * Test that a server attempting to read early data can handle a connection | |
4185 | * from a TLSv1.2 client. | |
4186 | */ | |
3cb47b4e | 4187 | static int test_early_data_tls1_2(int idx) |
5f982038 MC |
4188 | { |
4189 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
4190 | SSL *clientssl = NULL, *serverssl = NULL; | |
4191 | int testresult = 0; | |
4192 | unsigned char buf[20]; | |
4193 | size_t readbytes, written; | |
4194 | ||
02a3ed5a MC |
4195 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, |
4196 | &serverssl, NULL, idx))) | |
5f982038 | 4197 | goto end; |
5f982038 MC |
4198 | |
4199 | /* Write some data - should block due to handshake with server */ | |
4200 | SSL_set_max_proto_version(clientssl, TLS1_2_VERSION); | |
4201 | SSL_set_connect_state(clientssl); | |
710756a9 | 4202 | if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))) |
5f982038 | 4203 | goto end; |
5f982038 MC |
4204 | |
4205 | /* | |
4206 | * Server should do TLSv1.2 handshake. First it will block waiting for more | |
f533fbd4 MC |
4207 | * messages from client after ServerDone. Then SSL_read_early_data should |
4208 | * finish and detect that early data has not been sent | |
5f982038 | 4209 | */ |
710756a9 RS |
4210 | if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), |
4211 | &readbytes), | |
4212 | SSL_READ_EARLY_DATA_ERROR)) | |
5f982038 | 4213 | goto end; |
5f982038 MC |
4214 | |
4215 | /* | |
4216 | * Continue writing the message we started earlier. Will still block waiting | |
4217 | * for the CCS/Finished from server | |
4218 | */ | |
710756a9 RS |
4219 | if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) |
4220 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
4221 | &readbytes), | |
4222 | SSL_READ_EARLY_DATA_FINISH) | |
4223 | || !TEST_size_t_eq(readbytes, 0) | |
4224 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
4225 | SSL_EARLY_DATA_NOT_SENT)) | |
5f982038 | 4226 | goto end; |
5f982038 MC |
4227 | |
4228 | /* Continue writing the message we started earlier */ | |
710756a9 RS |
4229 | if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) |
4230 | || !TEST_size_t_eq(written, strlen(MSG1)) | |
4231 | || !TEST_int_eq(SSL_get_early_data_status(clientssl), | |
4232 | SSL_EARLY_DATA_NOT_SENT) | |
4233 | || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
4234 | || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)) | |
4235 | || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)) | |
4236 | || !TEST_size_t_eq(written, strlen(MSG2)) | |
4237 | || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes) | |
4238 | || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2))) | |
5f982038 | 4239 | goto end; |
5f982038 MC |
4240 | |
4241 | testresult = 1; | |
4242 | ||
4243 | end: | |
57dee9bb MC |
4244 | SSL_SESSION_free(clientpsk); |
4245 | SSL_SESSION_free(serverpsk); | |
4246 | clientpsk = serverpsk = NULL; | |
5f982038 MC |
4247 | SSL_free(serverssl); |
4248 | SSL_free(clientssl); | |
4249 | SSL_CTX_free(sctx); | |
4250 | SSL_CTX_free(cctx); | |
4251 | ||
4252 | return testresult; | |
4253 | } | |
ca0413ae MC |
4254 | # endif /* OPENSSL_NO_TLS1_2 */ |
4255 | ||
034cb87b MC |
4256 | /* |
4257 | * Test configuring the TLSv1.3 ciphersuites | |
4258 | * | |
4259 | * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list) | |
4260 | * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list) | |
4261 | * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list) | |
4262 | * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list) | |
4263 | * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list) | |
4264 | * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list) | |
4265 | * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list) | |
4266 | * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list) | |
4267 | * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list) | |
4268 | * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list) | |
4269 | */ | |
4270 | static int test_set_ciphersuite(int idx) | |
4271 | { | |
4272 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
4273 | SSL *clientssl = NULL, *serverssl = NULL; | |
4274 | int testresult = 0; | |
4275 | ||
5e30f2fd MC |
4276 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
4277 | TLS_client_method(), TLS1_VERSION, 0, | |
034cb87b MC |
4278 | &sctx, &cctx, cert, privkey)) |
4279 | || !TEST_true(SSL_CTX_set_ciphersuites(sctx, | |
4280 | "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256"))) | |
4281 | goto end; | |
4282 | ||
4283 | if (idx >=4 && idx <= 7) { | |
4284 | /* SSL_CTX explicit cipher list */ | |
4285 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384"))) | |
4286 | goto end; | |
4287 | } | |
4288 | ||
4289 | if (idx == 0 || idx == 4) { | |
4290 | /* Default ciphersuite */ | |
4291 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, | |
4292 | "TLS_AES_128_GCM_SHA256"))) | |
4293 | goto end; | |
4294 | } else if (idx == 1 || idx == 5) { | |
4295 | /* Non default ciphersuite */ | |
4296 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, | |
4297 | "TLS_AES_128_CCM_SHA256"))) | |
4298 | goto end; | |
4299 | } | |
4300 | ||
4301 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
4302 | &clientssl, NULL, NULL))) | |
4303 | goto end; | |
4304 | ||
4305 | if (idx == 8 || idx == 9) { | |
4306 | /* SSL explicit cipher list */ | |
4307 | if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))) | |
4308 | goto end; | |
4309 | } | |
4310 | ||
4311 | if (idx == 2 || idx == 6 || idx == 8) { | |
4312 | /* Default ciphersuite */ | |
4313 | if (!TEST_true(SSL_set_ciphersuites(clientssl, | |
4314 | "TLS_AES_128_GCM_SHA256"))) | |
4315 | goto end; | |
4316 | } else if (idx == 3 || idx == 7 || idx == 9) { | |
4317 | /* Non default ciphersuite */ | |
4318 | if (!TEST_true(SSL_set_ciphersuites(clientssl, | |
4319 | "TLS_AES_128_CCM_SHA256"))) | |
4320 | goto end; | |
4321 | } | |
4322 | ||
4323 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
4324 | goto end; | |
4325 | ||
4326 | testresult = 1; | |
4327 | ||
4328 | end: | |
4329 | SSL_free(serverssl); | |
4330 | SSL_free(clientssl); | |
4331 | SSL_CTX_free(sctx); | |
4332 | SSL_CTX_free(cctx); | |
4333 | ||
4334 | return testresult; | |
4335 | } | |
4336 | ||
ca0413ae MC |
4337 | static int test_ciphersuite_change(void) |
4338 | { | |
4339 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
4340 | SSL *clientssl = NULL, *serverssl = NULL; | |
4341 | SSL_SESSION *clntsess = NULL; | |
4342 | int testresult = 0; | |
4343 | const SSL_CIPHER *aes_128_gcm_sha256 = NULL; | |
4344 | ||
4345 | /* Create a session based on SHA-256 */ | |
5e30f2fd MC |
4346 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
4347 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 4348 | &sctx, &cctx, cert, privkey)) |
4f6c7044 MC |
4349 | || !TEST_true(SSL_CTX_set_ciphersuites(sctx, |
4350 | "TLS_AES_128_GCM_SHA256:" | |
4351 | "TLS_AES_256_GCM_SHA384:" | |
4352 | "TLS_AES_128_CCM_SHA256")) | |
f865b081 MC |
4353 | || !TEST_true(SSL_CTX_set_ciphersuites(cctx, |
4354 | "TLS_AES_128_GCM_SHA256")) | |
ca0413ae MC |
4355 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
4356 | &clientssl, NULL, NULL)) | |
4357 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
4358 | SSL_ERROR_NONE))) | |
4359 | goto end; | |
4360 | ||
4361 | clntsess = SSL_get1_session(clientssl); | |
4362 | /* Save for later */ | |
4363 | aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess); | |
4364 | SSL_shutdown(clientssl); | |
4365 | SSL_shutdown(serverssl); | |
4366 | SSL_free(serverssl); | |
4367 | SSL_free(clientssl); | |
4368 | serverssl = clientssl = NULL; | |
4369 | ||
4370 | /* Check we can resume a session with a different SHA-256 ciphersuite */ | |
f865b081 | 4371 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, |
4f6c7044 MC |
4372 | "TLS_AES_128_CCM_SHA256")) |
4373 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
4374 | &clientssl, NULL, NULL)) | |
ca0413ae MC |
4375 | || !TEST_true(SSL_set_session(clientssl, clntsess)) |
4376 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
4377 | SSL_ERROR_NONE)) | |
4378 | || !TEST_true(SSL_session_reused(clientssl))) | |
4379 | goto end; | |
4380 | ||
4381 | SSL_SESSION_free(clntsess); | |
4382 | clntsess = SSL_get1_session(clientssl); | |
4383 | SSL_shutdown(clientssl); | |
4384 | SSL_shutdown(serverssl); | |
4385 | SSL_free(serverssl); | |
4386 | SSL_free(clientssl); | |
4387 | serverssl = clientssl = NULL; | |
4388 | ||
4389 | /* | |
4390 | * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites | |
0de6d66d | 4391 | * succeeds but does not resume. |
ca0413ae | 4392 | */ |
f865b081 | 4393 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384")) |
ca0413ae MC |
4394 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
4395 | NULL, NULL)) | |
4396 | || !TEST_true(SSL_set_session(clientssl, clntsess)) | |
0de6d66d | 4397 | || !TEST_true(create_ssl_connection(serverssl, clientssl, |
ca0413ae | 4398 | SSL_ERROR_SSL)) |
0de6d66d | 4399 | || !TEST_false(SSL_session_reused(clientssl))) |
ca0413ae MC |
4400 | goto end; |
4401 | ||
4402 | SSL_SESSION_free(clntsess); | |
4403 | clntsess = NULL; | |
4404 | SSL_shutdown(clientssl); | |
4405 | SSL_shutdown(serverssl); | |
4406 | SSL_free(serverssl); | |
4407 | SSL_free(clientssl); | |
4408 | serverssl = clientssl = NULL; | |
4409 | ||
4410 | /* Create a session based on SHA384 */ | |
f865b081 | 4411 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384")) |
ca0413ae MC |
4412 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
4413 | &clientssl, NULL, NULL)) | |
4414 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
4415 | SSL_ERROR_NONE))) | |
4416 | goto end; | |
4417 | ||
4418 | clntsess = SSL_get1_session(clientssl); | |
4419 | SSL_shutdown(clientssl); | |
4420 | SSL_shutdown(serverssl); | |
4421 | SSL_free(serverssl); | |
4422 | SSL_free(clientssl); | |
4423 | serverssl = clientssl = NULL; | |
4424 | ||
f865b081 MC |
4425 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, |
4426 | "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384")) | |
4427 | || !TEST_true(SSL_CTX_set_ciphersuites(sctx, | |
4428 | "TLS_AES_256_GCM_SHA384")) | |
ca0413ae MC |
4429 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
4430 | NULL, NULL)) | |
4431 | || !TEST_true(SSL_set_session(clientssl, clntsess)) | |
3b0e88d3 MC |
4432 | /* |
4433 | * We use SSL_ERROR_WANT_READ below so that we can pause the | |
4434 | * connection after the initial ClientHello has been sent to | |
4435 | * enable us to make some session changes. | |
4436 | */ | |
ca0413ae MC |
4437 | || !TEST_false(create_ssl_connection(serverssl, clientssl, |
4438 | SSL_ERROR_WANT_READ))) | |
4439 | goto end; | |
4440 | ||
4441 | /* Trick the client into thinking this session is for a different digest */ | |
4442 | clntsess->cipher = aes_128_gcm_sha256; | |
4443 | clntsess->cipher_id = clntsess->cipher->id; | |
4444 | ||
4445 | /* | |
3b0e88d3 MC |
4446 | * Continue the previously started connection. Server has selected a SHA-384 |
4447 | * ciphersuite, but client thinks the session is for SHA-256, so it should | |
4448 | * bail out. | |
ca0413ae MC |
4449 | */ |
4450 | if (!TEST_false(create_ssl_connection(serverssl, clientssl, | |
4451 | SSL_ERROR_SSL)) | |
4452 | || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), | |
4453 | SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED)) | |
4454 | goto end; | |
4455 | ||
4456 | testresult = 1; | |
4457 | ||
4458 | end: | |
4459 | SSL_SESSION_free(clntsess); | |
4460 | SSL_free(serverssl); | |
4461 | SSL_free(clientssl); | |
4462 | SSL_CTX_free(sctx); | |
4463 | SSL_CTX_free(cctx); | |
4464 | ||
4465 | return testresult; | |
4466 | } | |
4467 | ||
8e63900a | 4468 | /* |
5bf2eade | 4469 | * Test TLSv1.3 Key exchange |
4470 | * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server | |
4471 | * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server | |
4472 | * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server | |
4473 | * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server | |
4474 | * Test 4 = Test NID_X25519 with TLSv1.3 client and server | |
4475 | * Test 5 = Test NID_X448 with TLSv1.3 client and server | |
4476 | * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server | |
4477 | * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server | |
4478 | * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server | |
4479 | * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server | |
4480 | * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server | |
4481 | * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server | |
4482 | * Test 12 = Test all ECDHE with TLSv1.2 client and server | |
4483 | * Test 13 = Test all FFDHE with TLSv1.2 client and server | |
8e63900a | 4484 | */ |
db26ec80 | 4485 | static int test_key_exchange(int idx) |
8e63900a | 4486 | { |
4487 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
4488 | SSL *serverssl = NULL, *clientssl = NULL; | |
4489 | int testresult = 0; | |
5bf2eade | 4490 | # ifndef OPENSSL_NO_EC |
4491 | int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1, | |
4492 | NID_secp521r1, NID_X25519, NID_X448}; | |
4493 | # endif | |
4494 | # ifndef OPENSSL_NO_DH | |
8e63900a | 4495 | int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096, |
4496 | NID_ffdhe6144, NID_ffdhe8192}; | |
5bf2eade | 4497 | # endif |
fb8c6db4 | 4498 | int kexch_alg; |
4499 | int *kexch_groups = &kexch_alg; | |
4500 | int kexch_groups_size = 1; | |
8e63900a | 4501 | int max_version = TLS1_3_VERSION; |
8e63900a | 4502 | |
4503 | switch (idx) { | |
5bf2eade | 4504 | # ifndef OPENSSL_NO_EC |
db26ec80 | 4505 | # ifndef OPENSSL_NO_TLS1_2 |
5bf2eade | 4506 | case 12: |
fb8c6db4 | 4507 | max_version = TLS1_2_VERSION; |
db26ec80 | 4508 | # endif |
fb8c6db4 | 4509 | /* Fall through */ |
4510 | case 0: | |
4511 | kexch_groups = ecdhe_kexch_groups; | |
4512 | kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups); | |
4513 | break; | |
5bf2eade | 4514 | case 1: |
fb8c6db4 | 4515 | kexch_alg = NID_X9_62_prime256v1; |
4516 | break; | |
5bf2eade | 4517 | case 2: |
fb8c6db4 | 4518 | kexch_alg = NID_secp384r1; |
4519 | break; | |
5bf2eade | 4520 | case 3: |
fb8c6db4 | 4521 | kexch_alg = NID_secp521r1; |
4522 | break; | |
5bf2eade | 4523 | case 4: |
fb8c6db4 | 4524 | kexch_alg = NID_X25519; |
4525 | break; | |
5bf2eade | 4526 | case 5: |
fb8c6db4 | 4527 | kexch_alg = NID_X448; |
4528 | break; | |
5bf2eade | 4529 | # endif |
4530 | # ifndef OPENSSL_NO_DH | |
db26ec80 | 4531 | # ifndef OPENSSL_NO_TLS1_2 |
5bf2eade | 4532 | case 13: |
dbc6268f | 4533 | max_version = TLS1_2_VERSION; |
db26ec80 | 4534 | # endif |
dbc6268f | 4535 | /* Fall through */ |
5bf2eade | 4536 | case 6: |
8e63900a | 4537 | kexch_groups = ffdhe_kexch_groups; |
4538 | kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups); | |
4539 | break; | |
5bf2eade | 4540 | case 7: |
fb8c6db4 | 4541 | kexch_alg = NID_ffdhe2048; |
4542 | break; | |
5bf2eade | 4543 | case 8: |
fb8c6db4 | 4544 | kexch_alg = NID_ffdhe3072; |
4545 | break; | |
5bf2eade | 4546 | case 9: |
fb8c6db4 | 4547 | kexch_alg = NID_ffdhe4096; |
4548 | break; | |
5bf2eade | 4549 | case 10: |
fb8c6db4 | 4550 | kexch_alg = NID_ffdhe6144; |
4551 | break; | |
5bf2eade | 4552 | case 11: |
fb8c6db4 | 4553 | kexch_alg = NID_ffdhe8192; |
8e63900a | 4554 | break; |
5bf2eade | 4555 | # endif |
6597d62b MC |
4556 | default: |
4557 | /* We're skipping this test */ | |
4558 | return 1; | |
8e63900a | 4559 | } |
4560 | ||
5e30f2fd MC |
4561 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
4562 | TLS_client_method(), TLS1_VERSION, | |
4563 | max_version, &sctx, &cctx, cert, | |
4564 | privkey))) | |
8e63900a | 4565 | goto end; |
4566 | ||
5bf2eade | 4567 | if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, |
4568 | TLS1_3_RFC_AES_128_GCM_SHA256))) | |
8e63900a | 4569 | goto end; |
4570 | ||
5bf2eade | 4571 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, |
4572 | TLS1_3_RFC_AES_128_GCM_SHA256))) | |
8e63900a | 4573 | goto end; |
4574 | ||
5bf2eade | 4575 | if (!TEST_true(SSL_CTX_set_cipher_list(sctx, |
4576 | TLS1_TXT_RSA_WITH_AES_128_SHA))) | |
8e63900a | 4577 | goto end; |
4578 | ||
dbc6268f MC |
4579 | /* |
4580 | * Must include an EC ciphersuite so that we send supported groups in | |
4581 | * TLSv1.2 | |
4582 | */ | |
5bf2eade | 4583 | # ifndef OPENSSL_NO_TLS1_2 |
dbc6268f MC |
4584 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, |
4585 | TLS1_TXT_ECDHE_ECDSA_WITH_AES_128_CCM ":" | |
4586 | TLS1_TXT_RSA_WITH_AES_128_SHA))) | |
8e63900a | 4587 | goto end; |
5bf2eade | 4588 | # endif |
8e63900a | 4589 | |
4590 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
4591 | NULL, NULL))) | |
4592 | goto end; | |
4593 | ||
4594 | if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size)) | |
4595 | || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size))) | |
4596 | goto end; | |
4597 | ||
5bf2eade | 4598 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) |
8e63900a | 4599 | goto end; |
8e63900a | 4600 | |
dbc6268f | 4601 | /* |
5bf2eade | 4602 | * If Handshake succeeds the negotiated kexch alg should be the first one in |
4603 | * configured, except in the case of FFDHE groups (idx 13), which are | |
4604 | * TLSv1.3 only so we expect no shared group to exist. | |
dbc6268f MC |
4605 | */ |
4606 | if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0), | |
5bf2eade | 4607 | idx == 13 ? 0 : kexch_groups[0])) |
8e63900a | 4608 | goto end; |
fb8c6db4 | 4609 | if (max_version == TLS1_3_VERSION) { |
4610 | if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0])) | |
4611 | goto end; | |
4612 | if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0])) | |
4613 | goto end; | |
4614 | } | |
8e63900a | 4615 | |
4616 | testresult = 1; | |
4617 | end: | |
4618 | SSL_free(serverssl); | |
4619 | SSL_free(clientssl); | |
4620 | SSL_CTX_free(sctx); | |
4621 | SSL_CTX_free(cctx); | |
4622 | return testresult; | |
4623 | } | |
4624 | ||
5bf2eade | 4625 | /* |
4626 | * Test TLSv1.3 Cipher Suite | |
4627 | * Test 0 = Set TLS1.3 cipher on context | |
4628 | * Test 1 = Set TLS1.3 cipher on SSL | |
4629 | * Test 2 = Set TLS1.3 and TLS1.2 cipher on context | |
4630 | * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL | |
4631 | */ | |
4632 | static int test_tls13_ciphersuite(int idx) | |
4633 | { | |
4634 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
4635 | SSL *serverssl = NULL, *clientssl = NULL; | |
4f6c7044 MC |
4636 | static const struct { |
4637 | const char *ciphername; | |
4638 | int fipscapable; | |
4639 | } t13_ciphers[] = { | |
4640 | { TLS1_3_RFC_AES_128_GCM_SHA256, 1 }, | |
4641 | { TLS1_3_RFC_AES_256_GCM_SHA384, 1 }, | |
4642 | { TLS1_3_RFC_AES_128_CCM_SHA256, 1 }, | |
5bf2eade | 4643 | # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) |
4f6c7044 MC |
4644 | { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 }, |
4645 | { TLS1_3_RFC_AES_256_GCM_SHA384 | |
4646 | ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 }, | |
5bf2eade | 4647 | # endif |
4f6c7044 | 4648 | { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1 } |
5bf2eade | 4649 | }; |
4650 | const char *t13_cipher = NULL; | |
4651 | const char *t12_cipher = NULL; | |
4652 | const char *negotiated_scipher; | |
4653 | const char *negotiated_ccipher; | |
4654 | int set_at_ctx = 0; | |
4655 | int set_at_ssl = 0; | |
4656 | int testresult = 0; | |
4657 | int max_ver; | |
4658 | size_t i; | |
4659 | ||
4660 | switch (idx) { | |
4661 | case 0: | |
4662 | set_at_ctx = 1; | |
4663 | break; | |
4664 | case 1: | |
4665 | set_at_ssl = 1; | |
4666 | break; | |
4667 | case 2: | |
4668 | set_at_ctx = 1; | |
2d900758 | 4669 | t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256; |
5bf2eade | 4670 | break; |
4671 | case 3: | |
4672 | set_at_ssl = 1; | |
2d900758 | 4673 | t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256; |
5bf2eade | 4674 | break; |
4675 | } | |
4676 | ||
4677 | for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) { | |
4678 | # ifdef OPENSSL_NO_TLS1_2 | |
4679 | if (max_ver == TLS1_2_VERSION) | |
4680 | continue; | |
4681 | # endif | |
4682 | for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) { | |
4f6c7044 MC |
4683 | if (is_fips && !t13_ciphers[i].fipscapable) |
4684 | continue; | |
4685 | t13_cipher = t13_ciphers[i].ciphername; | |
5e30f2fd | 4686 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5bf2eade | 4687 | TLS_client_method(), |
4688 | TLS1_VERSION, max_ver, | |
4689 | &sctx, &cctx, cert, privkey))) | |
4690 | goto end; | |
4691 | ||
4692 | if (set_at_ctx) { | |
4693 | if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher)) | |
4694 | || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher))) | |
4695 | goto end; | |
4696 | if (t12_cipher != NULL) { | |
4697 | if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher)) | |
4698 | || !TEST_true(SSL_CTX_set_cipher_list(cctx, | |
4699 | t12_cipher))) | |
4700 | goto end; | |
4701 | } | |
4702 | } | |
4703 | ||
4704 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
4705 | &clientssl, NULL, NULL))) | |
4706 | goto end; | |
4707 | ||
4708 | if (set_at_ssl) { | |
4709 | if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher)) | |
4710 | || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher))) | |
4711 | goto end; | |
4712 | if (t12_cipher != NULL) { | |
4713 | if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher)) | |
4714 | || !TEST_true(SSL_set_cipher_list(clientssl, | |
4715 | t12_cipher))) | |
4716 | goto end; | |
4717 | } | |
4718 | } | |
4719 | ||
4720 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
4721 | SSL_ERROR_NONE))) | |
4722 | goto end; | |
4723 | ||
4724 | negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher( | |
4725 | serverssl)); | |
4726 | negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher( | |
4727 | clientssl)); | |
4728 | if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher)) | |
4729 | goto end; | |
4730 | ||
4731 | /* | |
4732 | * TEST_strn_eq is used below because t13_cipher can contain | |
4733 | * multiple ciphersuites | |
4734 | */ | |
4735 | if (max_ver == TLS1_3_VERSION | |
4736 | && !TEST_strn_eq(t13_cipher, negotiated_scipher, | |
4737 | strlen(negotiated_scipher))) | |
4738 | goto end; | |
4739 | ||
4740 | # ifndef OPENSSL_NO_TLS1_2 | |
4741 | /* Below validation is not done when t12_cipher is NULL */ | |
4742 | if (max_ver == TLS1_2_VERSION && t12_cipher != NULL | |
4743 | && !TEST_str_eq(t12_cipher, negotiated_scipher)) | |
4744 | goto end; | |
4745 | # endif | |
4746 | ||
4747 | SSL_free(serverssl); | |
4748 | serverssl = NULL; | |
4749 | SSL_free(clientssl); | |
4750 | clientssl = NULL; | |
4751 | SSL_CTX_free(sctx); | |
4752 | sctx = NULL; | |
4753 | SSL_CTX_free(cctx); | |
4754 | cctx = NULL; | |
4755 | } | |
4756 | } | |
4757 | ||
4758 | testresult = 1; | |
4759 | end: | |
4760 | SSL_free(serverssl); | |
4761 | SSL_free(clientssl); | |
4762 | SSL_CTX_free(sctx); | |
4763 | SSL_CTX_free(cctx); | |
4764 | return testresult; | |
4765 | } | |
4766 | ||
0d8da779 MC |
4767 | /* |
4768 | * Test TLSv1.3 PSKs | |
4769 | * Test 0 = Test new style callbacks | |
4770 | * Test 1 = Test both new and old style callbacks | |
4771 | * Test 2 = Test old style callbacks | |
4772 | * Test 3 = Test old style callbacks with no certificate | |
4773 | */ | |
532f9578 | 4774 | static int test_tls13_psk(int idx) |
ca8c71ba MC |
4775 | { |
4776 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
4777 | SSL *serverssl = NULL, *clientssl = NULL; | |
4778 | const SSL_CIPHER *cipher = NULL; | |
4779 | const unsigned char key[] = { | |
4780 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, | |
4781 | 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, | |
de2f409e MC |
4782 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, |
4783 | 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f | |
ca8c71ba MC |
4784 | }; |
4785 | int testresult = 0; | |
4786 | ||
5e30f2fd MC |
4787 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
4788 | TLS_client_method(), TLS1_VERSION, 0, | |
0d8da779 MC |
4789 | &sctx, &cctx, idx == 3 ? NULL : cert, |
4790 | idx == 3 ? NULL : privkey))) | |
ca8c71ba MC |
4791 | goto end; |
4792 | ||
0d8da779 MC |
4793 | if (idx != 3) { |
4794 | /* | |
4795 | * We use a ciphersuite with SHA256 to ease testing old style PSK | |
4796 | * callbacks which will always default to SHA256. This should not be | |
4797 | * necessary if we have no cert/priv key. In that case the server should | |
4798 | * prefer SHA256 automatically. | |
4799 | */ | |
4800 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, | |
4801 | "TLS_AES_128_GCM_SHA256"))) | |
4802 | goto end; | |
4f6c7044 MC |
4803 | } else { |
4804 | /* | |
4805 | * As noted above the server should prefer SHA256 automatically. However | |
4806 | * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same | |
4807 | * code works even if we are testing with only the FIPS provider loaded. | |
4808 | */ | |
4809 | if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, | |
4810 | "TLS_AES_256_GCM_SHA384:" | |
4811 | "TLS_AES_128_GCM_SHA256"))) | |
4812 | goto end; | |
0d8da779 | 4813 | } |
532f9578 MC |
4814 | |
4815 | /* | |
4816 | * Test 0: New style callbacks only | |
4817 | * Test 1: New and old style callbacks (only the new ones should be used) | |
4818 | * Test 2: Old style callbacks only | |
4819 | */ | |
4820 | if (idx == 0 || idx == 1) { | |
4821 | SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb); | |
4822 | SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb); | |
4823 | } | |
c2b290c3 | 4824 | #ifndef OPENSSL_NO_PSK |
0d8da779 | 4825 | if (idx >= 1) { |
532f9578 MC |
4826 | SSL_CTX_set_psk_client_callback(cctx, psk_client_cb); |
4827 | SSL_CTX_set_psk_server_callback(sctx, psk_server_cb); | |
4828 | } | |
c2b290c3 | 4829 | #endif |
ca8c71ba | 4830 | srvid = pskid; |
02a3ed5a MC |
4831 | use_session_cb_cnt = 0; |
4832 | find_session_cb_cnt = 0; | |
532f9578 MC |
4833 | psk_client_cb_cnt = 0; |
4834 | psk_server_cb_cnt = 0; | |
ca8c71ba | 4835 | |
0d8da779 MC |
4836 | if (idx != 3) { |
4837 | /* | |
4838 | * Check we can create a connection if callback decides not to send a | |
4839 | * PSK | |
4840 | */ | |
4841 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
4842 | NULL, NULL)) | |
4843 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
4844 | SSL_ERROR_NONE)) | |
4845 | || !TEST_false(SSL_session_reused(clientssl)) | |
4846 | || !TEST_false(SSL_session_reused(serverssl))) | |
532f9578 | 4847 | goto end; |
532f9578 | 4848 | |
0d8da779 MC |
4849 | if (idx == 0 || idx == 1) { |
4850 | if (!TEST_true(use_session_cb_cnt == 1) | |
4851 | || !TEST_true(find_session_cb_cnt == 0) | |
4852 | /* | |
4853 | * If no old style callback then below should be 0 | |
4854 | * otherwise 1 | |
4855 | */ | |
4856 | || !TEST_true(psk_client_cb_cnt == idx) | |
4857 | || !TEST_true(psk_server_cb_cnt == 0)) | |
4858 | goto end; | |
4859 | } else { | |
4860 | if (!TEST_true(use_session_cb_cnt == 0) | |
4861 | || !TEST_true(find_session_cb_cnt == 0) | |
4862 | || !TEST_true(psk_client_cb_cnt == 1) | |
4863 | || !TEST_true(psk_server_cb_cnt == 0)) | |
4864 | goto end; | |
4865 | } | |
4866 | ||
4867 | shutdown_ssl_connection(serverssl, clientssl); | |
4868 | serverssl = clientssl = NULL; | |
4869 | use_session_cb_cnt = psk_client_cb_cnt = 0; | |
4870 | } | |
ca8c71ba MC |
4871 | |
4872 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
4873 | NULL, NULL))) | |
4874 | goto end; | |
4875 | ||
4876 | /* Create the PSK */ | |
532f9578 | 4877 | cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES); |
57dee9bb MC |
4878 | clientpsk = SSL_SESSION_new(); |
4879 | if (!TEST_ptr(clientpsk) | |
ca8c71ba | 4880 | || !TEST_ptr(cipher) |
57dee9bb MC |
4881 | || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key, |
4882 | sizeof(key))) | |
4883 | || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher)) | |
4884 | || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk, | |
4885 | TLS1_3_VERSION)) | |
4886 | || !TEST_true(SSL_SESSION_up_ref(clientpsk))) | |
ca8c71ba | 4887 | goto end; |
57dee9bb | 4888 | serverpsk = clientpsk; |
ca8c71ba MC |
4889 | |
4890 | /* Check we can create a connection and the PSK is used */ | |
4891 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) | |
4892 | || !TEST_true(SSL_session_reused(clientssl)) | |
532f9578 | 4893 | || !TEST_true(SSL_session_reused(serverssl))) |
ca8c71ba MC |
4894 | goto end; |
4895 | ||
532f9578 MC |
4896 | if (idx == 0 || idx == 1) { |
4897 | if (!TEST_true(use_session_cb_cnt == 1) | |
4898 | || !TEST_true(find_session_cb_cnt == 1) | |
4899 | || !TEST_true(psk_client_cb_cnt == 0) | |
4900 | || !TEST_true(psk_server_cb_cnt == 0)) | |
4901 | goto end; | |
4902 | } else { | |
4903 | if (!TEST_true(use_session_cb_cnt == 0) | |
4904 | || !TEST_true(find_session_cb_cnt == 0) | |
4905 | || !TEST_true(psk_client_cb_cnt == 1) | |
4906 | || !TEST_true(psk_server_cb_cnt == 1)) | |
4907 | goto end; | |
4908 | } | |
4909 | ||
ca8c71ba MC |
4910 | shutdown_ssl_connection(serverssl, clientssl); |
4911 | serverssl = clientssl = NULL; | |
4912 | use_session_cb_cnt = find_session_cb_cnt = 0; | |
532f9578 | 4913 | psk_client_cb_cnt = psk_server_cb_cnt = 0; |
ca8c71ba MC |
4914 | |
4915 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
4916 | NULL, NULL))) | |
4917 | goto end; | |
4918 | ||
4919 | /* Force an HRR */ | |
dbc6268f MC |
4920 | #if defined(OPENSSL_NO_EC) |
4921 | if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072"))) | |
4922 | goto end; | |
4923 | #else | |
ca8c71ba MC |
4924 | if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256"))) |
4925 | goto end; | |
dbc6268f | 4926 | #endif |
ca8c71ba MC |
4927 | |
4928 | /* | |
4929 | * Check we can create a connection, the PSK is used and the callbacks are | |
4930 | * called twice. | |
4931 | */ | |
4932 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) | |
4933 | || !TEST_true(SSL_session_reused(clientssl)) | |
532f9578 | 4934 | || !TEST_true(SSL_session_reused(serverssl))) |
ca8c71ba MC |
4935 | goto end; |
4936 | ||
532f9578 MC |
4937 | if (idx == 0 || idx == 1) { |
4938 | if (!TEST_true(use_session_cb_cnt == 2) | |
4939 | || !TEST_true(find_session_cb_cnt == 2) | |
4940 | || !TEST_true(psk_client_cb_cnt == 0) | |
4941 | || !TEST_true(psk_server_cb_cnt == 0)) | |
4942 | goto end; | |
4943 | } else { | |
4944 | if (!TEST_true(use_session_cb_cnt == 0) | |
4945 | || !TEST_true(find_session_cb_cnt == 0) | |
4946 | || !TEST_true(psk_client_cb_cnt == 2) | |
4947 | || !TEST_true(psk_server_cb_cnt == 2)) | |
4948 | goto end; | |
4949 | } | |
4950 | ||
ca8c71ba MC |
4951 | shutdown_ssl_connection(serverssl, clientssl); |
4952 | serverssl = clientssl = NULL; | |
4953 | use_session_cb_cnt = find_session_cb_cnt = 0; | |
532f9578 | 4954 | psk_client_cb_cnt = psk_server_cb_cnt = 0; |
ca8c71ba | 4955 | |
0d8da779 MC |
4956 | if (idx != 3) { |
4957 | /* | |
4958 | * Check that if the server rejects the PSK we can still connect, but with | |
4959 | * a full handshake | |
4960 | */ | |
4961 | srvid = "Dummy Identity"; | |
4962 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
4963 | NULL, NULL)) | |
4964 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
4965 | SSL_ERROR_NONE)) | |
4966 | || !TEST_false(SSL_session_reused(clientssl)) | |
4967 | || !TEST_false(SSL_session_reused(serverssl))) | |
532f9578 | 4968 | goto end; |
532f9578 | 4969 | |
0d8da779 MC |
4970 | if (idx == 0 || idx == 1) { |
4971 | if (!TEST_true(use_session_cb_cnt == 1) | |
4972 | || !TEST_true(find_session_cb_cnt == 1) | |
4973 | || !TEST_true(psk_client_cb_cnt == 0) | |
4974 | /* | |
4975 | * If no old style callback then below should be 0 | |
4976 | * otherwise 1 | |
4977 | */ | |
4978 | || !TEST_true(psk_server_cb_cnt == idx)) | |
4979 | goto end; | |
4980 | } else { | |
4981 | if (!TEST_true(use_session_cb_cnt == 0) | |
4982 | || !TEST_true(find_session_cb_cnt == 0) | |
4983 | || !TEST_true(psk_client_cb_cnt == 1) | |
4984 | || !TEST_true(psk_server_cb_cnt == 1)) | |
4985 | goto end; | |
4986 | } | |
4987 | ||
4988 | shutdown_ssl_connection(serverssl, clientssl); | |
4989 | serverssl = clientssl = NULL; | |
4990 | } | |
ca8c71ba MC |
4991 | testresult = 1; |
4992 | ||
4993 | end: | |
57dee9bb MC |
4994 | SSL_SESSION_free(clientpsk); |
4995 | SSL_SESSION_free(serverpsk); | |
4996 | clientpsk = serverpsk = NULL; | |
ca8c71ba MC |
4997 | SSL_free(serverssl); |
4998 | SSL_free(clientssl); | |
4999 | SSL_CTX_free(sctx); | |
5000 | SSL_CTX_free(cctx); | |
5001 | return testresult; | |
5002 | } | |
5003 | ||
c7b8ff25 MC |
5004 | static unsigned char cookie_magic_value[] = "cookie magic"; |
5005 | ||
5006 | static int generate_cookie_callback(SSL *ssl, unsigned char *cookie, | |
5007 | unsigned int *cookie_len) | |
5008 | { | |
5009 | /* | |
5010 | * Not suitable as a real cookie generation function but good enough for | |
5011 | * testing! | |
5012 | */ | |
97ea1e7f MC |
5013 | memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1); |
5014 | *cookie_len = sizeof(cookie_magic_value) - 1; | |
c7b8ff25 MC |
5015 | |
5016 | return 1; | |
5017 | } | |
5018 | ||
5019 | static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie, | |
5020 | unsigned int cookie_len) | |
5021 | { | |
97ea1e7f | 5022 | if (cookie_len == sizeof(cookie_magic_value) - 1 |
c7b8ff25 MC |
5023 | && memcmp(cookie, cookie_magic_value, cookie_len) == 0) |
5024 | return 1; | |
5025 | ||
5026 | return 0; | |
5027 | } | |
5028 | ||
3fa2812f BS |
5029 | static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie, |
5030 | size_t *cookie_len) | |
5031 | { | |
5032 | unsigned int temp; | |
5033 | int res = generate_cookie_callback(ssl, cookie, &temp); | |
5034 | *cookie_len = temp; | |
5035 | return res; | |
5036 | } | |
5037 | ||
5038 | static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie, | |
5039 | size_t cookie_len) | |
5040 | { | |
5041 | return verify_cookie_callback(ssl, cookie, cookie_len); | |
5042 | } | |
5043 | ||
c7b8ff25 MC |
5044 | static int test_stateless(void) |
5045 | { | |
5046 | SSL_CTX *sctx = NULL, *cctx = NULL; | |
5047 | SSL *serverssl = NULL, *clientssl = NULL; | |
5048 | int testresult = 0; | |
5049 | ||
5e30f2fd MC |
5050 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5051 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 5052 | &sctx, &cctx, cert, privkey))) |
c7b8ff25 MC |
5053 | goto end; |
5054 | ||
e440f513 MC |
5055 | /* The arrival of CCS messages can confuse the test */ |
5056 | SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT); | |
5057 | ||
5058 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
5059 | NULL, NULL)) | |
5060 | /* Send the first ClientHello */ | |
5061 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
5062 | SSL_ERROR_WANT_READ)) | |
5063 | /* | |
5064 | * This should fail with a -1 return because we have no callbacks | |
5065 | * set up | |
5066 | */ | |
5067 | || !TEST_int_eq(SSL_stateless(serverssl), -1)) | |
5068 | goto end; | |
5069 | ||
5070 | /* Fatal error so abandon the connection from this client */ | |
5071 | SSL_free(clientssl); | |
5072 | clientssl = NULL; | |
5073 | ||
c7b8ff25 | 5074 | /* Set up the cookie generation and verification callbacks */ |
3fa2812f BS |
5075 | SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback); |
5076 | SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback); | |
c7b8ff25 | 5077 | |
e440f513 MC |
5078 | /* |
5079 | * Create a new connection from the client (we can reuse the server SSL | |
5080 | * object). | |
5081 | */ | |
c7b8ff25 MC |
5082 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
5083 | NULL, NULL)) | |
5084 | /* Send the first ClientHello */ | |
5085 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
5086 | SSL_ERROR_WANT_READ)) | |
5087 | /* This should fail because there is no cookie */ | |
e440f513 | 5088 | || !TEST_int_eq(SSL_stateless(serverssl), 0)) |
c7b8ff25 MC |
5089 | goto end; |
5090 | ||
5091 | /* Abandon the connection from this client */ | |
5092 | SSL_free(clientssl); | |
5093 | clientssl = NULL; | |
5094 | ||
5095 | /* | |
5096 | * Now create a connection from a new client but with the same server SSL | |
5097 | * object | |
5098 | */ | |
5099 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
5100 | NULL, NULL)) | |
5101 | /* Send the first ClientHello */ | |
5102 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
5103 | SSL_ERROR_WANT_READ)) | |
5104 | /* This should fail because there is no cookie */ | |
e440f513 | 5105 | || !TEST_int_eq(SSL_stateless(serverssl), 0) |
c7b8ff25 MC |
5106 | /* Send the second ClientHello */ |
5107 | || !TEST_false(create_ssl_connection(serverssl, clientssl, | |
5108 | SSL_ERROR_WANT_READ)) | |
5109 | /* This should succeed because a cookie is now present */ | |
e440f513 | 5110 | || !TEST_int_eq(SSL_stateless(serverssl), 1) |
c7b8ff25 MC |
5111 | /* Complete the connection */ |
5112 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5113 | SSL_ERROR_NONE))) | |
5114 | goto end; | |
5115 | ||
5116 | shutdown_ssl_connection(serverssl, clientssl); | |
5117 | serverssl = clientssl = NULL; | |
5118 | testresult = 1; | |
5119 | ||
5120 | end: | |
5121 | SSL_free(serverssl); | |
5122 | SSL_free(clientssl); | |
5123 | SSL_CTX_free(sctx); | |
5124 | SSL_CTX_free(cctx); | |
5125 | return testresult; | |
5126 | ||
5127 | } | |
ca0413ae | 5128 | #endif /* OPENSSL_NO_TLS1_3 */ |
5f982038 | 5129 | |
a37008d9 MC |
5130 | static int clntaddoldcb = 0; |
5131 | static int clntparseoldcb = 0; | |
5132 | static int srvaddoldcb = 0; | |
5133 | static int srvparseoldcb = 0; | |
5134 | static int clntaddnewcb = 0; | |
5135 | static int clntparsenewcb = 0; | |
5136 | static int srvaddnewcb = 0; | |
5137 | static int srvparsenewcb = 0; | |
bb01ef3f | 5138 | static int snicb = 0; |
a37008d9 MC |
5139 | |
5140 | #define TEST_EXT_TYPE1 0xff00 | |
5141 | ||
5142 | static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out, | |
5143 | size_t *outlen, int *al, void *add_arg) | |
5144 | { | |
5145 | int *server = (int *)add_arg; | |
5146 | unsigned char *data; | |
5147 | ||
5148 | if (SSL_is_server(s)) | |
5149 | srvaddoldcb++; | |
5150 | else | |
5151 | clntaddoldcb++; | |
5152 | ||
710756a9 RS |
5153 | if (*server != SSL_is_server(s) |
5154 | || (data = OPENSSL_malloc(sizeof(*data))) == NULL) | |
a37008d9 MC |
5155 | return -1; |
5156 | ||
5157 | *data = 1; | |
5158 | *out = data; | |
5159 | *outlen = sizeof(char); | |
a37008d9 MC |
5160 | return 1; |
5161 | } | |
5162 | ||
5163 | static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out, | |
5164 | void *add_arg) | |
5165 | { | |
5166 | OPENSSL_free((unsigned char *)out); | |
5167 | } | |
5168 | ||
5169 | static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, | |
5170 | size_t inlen, int *al, void *parse_arg) | |
5171 | { | |
5172 | int *server = (int *)parse_arg; | |
5173 | ||
5174 | if (SSL_is_server(s)) | |
5175 | srvparseoldcb++; | |
5176 | else | |
5177 | clntparseoldcb++; | |
5178 | ||
710756a9 RS |
5179 | if (*server != SSL_is_server(s) |
5180 | || inlen != sizeof(char) | |
5181 | || *in != 1) | |
a37008d9 MC |
5182 | return -1; |
5183 | ||
5184 | return 1; | |
5185 | } | |
5186 | ||
5187 | static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context, | |
5188 | const unsigned char **out, size_t *outlen, X509 *x, | |
5189 | size_t chainidx, int *al, void *add_arg) | |
5190 | { | |
5191 | int *server = (int *)add_arg; | |
5192 | unsigned char *data; | |
5193 | ||
5194 | if (SSL_is_server(s)) | |
5195 | srvaddnewcb++; | |
5196 | else | |
5197 | clntaddnewcb++; | |
5198 | ||
710756a9 RS |
5199 | if (*server != SSL_is_server(s) |
5200 | || (data = OPENSSL_malloc(sizeof(*data))) == NULL) | |
a37008d9 MC |
5201 | return -1; |
5202 | ||
5203 | *data = 1; | |
5204 | *out = data; | |
710756a9 | 5205 | *outlen = sizeof(*data); |
a37008d9 MC |
5206 | return 1; |
5207 | } | |
5208 | ||
5209 | static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context, | |
5210 | const unsigned char *out, void *add_arg) | |
5211 | { | |
5212 | OPENSSL_free((unsigned char *)out); | |
5213 | } | |
5214 | ||
5215 | static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context, | |
5216 | const unsigned char *in, size_t inlen, X509 *x, | |
5217 | size_t chainidx, int *al, void *parse_arg) | |
5218 | { | |
5219 | int *server = (int *)parse_arg; | |
5220 | ||
5221 | if (SSL_is_server(s)) | |
5222 | srvparsenewcb++; | |
5223 | else | |
5224 | clntparsenewcb++; | |
5225 | ||
710756a9 RS |
5226 | if (*server != SSL_is_server(s) |
5227 | || inlen != sizeof(char) || *in != 1) | |
a37008d9 MC |
5228 | return -1; |
5229 | ||
5230 | return 1; | |
5231 | } | |
bb01ef3f MC |
5232 | |
5233 | static int sni_cb(SSL *s, int *al, void *arg) | |
5234 | { | |
5235 | SSL_CTX *ctx = (SSL_CTX *)arg; | |
5236 | ||
5237 | if (SSL_set_SSL_CTX(s, ctx) == NULL) { | |
5238 | *al = SSL_AD_INTERNAL_ERROR; | |
5239 | return SSL_TLSEXT_ERR_ALERT_FATAL; | |
5240 | } | |
5241 | snicb++; | |
5242 | return SSL_TLSEXT_ERR_OK; | |
5243 | } | |
5244 | ||
a37008d9 MC |
5245 | /* |
5246 | * Custom call back tests. | |
5247 | * Test 0: Old style callbacks in TLSv1.2 | |
5248 | * Test 1: New style callbacks in TLSv1.2 | |
bb01ef3f MC |
5249 | * Test 2: New style callbacks in TLSv1.2 with SNI |
5250 | * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE | |
5251 | * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST | |
a37008d9 | 5252 | */ |
710756a9 RS |
5253 | static int test_custom_exts(int tst) |
5254 | { | |
bb01ef3f | 5255 | SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL; |
a37008d9 MC |
5256 | SSL *clientssl = NULL, *serverssl = NULL; |
5257 | int testresult = 0; | |
5258 | static int server = 1; | |
5259 | static int client = 0; | |
5260 | SSL_SESSION *sess = NULL; | |
5261 | unsigned int context; | |
5262 | ||
c423ecaa MC |
5263 | #if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3) |
5264 | /* Skip tests for TLSv1.2 and below in this case */ | |
5265 | if (tst < 3) | |
5266 | return 1; | |
5267 | #endif | |
5268 | ||
a37008d9 MC |
5269 | /* Reset callback counters */ |
5270 | clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0; | |
5271 | clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0; | |
bb01ef3f | 5272 | snicb = 0; |
a37008d9 | 5273 | |
5e30f2fd MC |
5274 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5275 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 5276 | &sctx, &cctx, cert, privkey))) |
710756a9 | 5277 | goto end; |
a37008d9 | 5278 | |
bb01ef3f | 5279 | if (tst == 2 |
5e30f2fd | 5280 | && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL, |
5c587fb6 | 5281 | TLS1_VERSION, 0, |
7d7f6834 | 5282 | &sctx2, NULL, cert, privkey))) |
bb01ef3f MC |
5283 | goto end; |
5284 | ||
5285 | ||
5286 | if (tst < 3) { | |
a37008d9 MC |
5287 | SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3); |
5288 | SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3); | |
bb01ef3f MC |
5289 | if (sctx2 != NULL) |
5290 | SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3); | |
a37008d9 MC |
5291 | } |
5292 | ||
bb01ef3f | 5293 | if (tst == 4) { |
710756a9 RS |
5294 | context = SSL_EXT_CLIENT_HELLO |
5295 | | SSL_EXT_TLS1_2_SERVER_HELLO | |
a37008d9 MC |
5296 | | SSL_EXT_TLS1_3_SERVER_HELLO |
5297 | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | |
5298 | | SSL_EXT_TLS1_3_CERTIFICATE | |
5299 | | SSL_EXT_TLS1_3_NEW_SESSION_TICKET; | |
5300 | } else { | |
710756a9 RS |
5301 | context = SSL_EXT_CLIENT_HELLO |
5302 | | SSL_EXT_TLS1_2_SERVER_HELLO | |
a37008d9 MC |
5303 | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS; |
5304 | } | |
5305 | ||
5306 | /* Create a client side custom extension */ | |
5307 | if (tst == 0) { | |
710756a9 RS |
5308 | if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, |
5309 | old_add_cb, old_free_cb, | |
5310 | &client, old_parse_cb, | |
5311 | &client))) | |
5312 | goto end; | |
a37008d9 | 5313 | } else { |
710756a9 RS |
5314 | if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context, |
5315 | new_add_cb, new_free_cb, | |
5316 | &client, new_parse_cb, &client))) | |
5317 | goto end; | |
a37008d9 MC |
5318 | } |
5319 | ||
5320 | /* Should not be able to add duplicates */ | |
710756a9 RS |
5321 | if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1, |
5322 | old_add_cb, old_free_cb, | |
5323 | &client, old_parse_cb, | |
5324 | &client)) | |
5325 | || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, | |
5326 | context, new_add_cb, | |
5327 | new_free_cb, &client, | |
5328 | new_parse_cb, &client))) | |
5329 | goto end; | |
a37008d9 MC |
5330 | |
5331 | /* Create a server side custom extension */ | |
5332 | if (tst == 0) { | |
710756a9 RS |
5333 | if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, |
5334 | old_add_cb, old_free_cb, | |
5335 | &server, old_parse_cb, | |
5336 | &server))) | |
5337 | goto end; | |
a37008d9 | 5338 | } else { |
710756a9 RS |
5339 | if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context, |
5340 | new_add_cb, new_free_cb, | |
5341 | &server, new_parse_cb, &server))) | |
5342 | goto end; | |
bb01ef3f MC |
5343 | if (sctx2 != NULL |
5344 | && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1, | |
5345 | context, new_add_cb, | |
5346 | new_free_cb, &server, | |
5347 | new_parse_cb, &server))) | |
5348 | goto end; | |
a37008d9 MC |
5349 | } |
5350 | ||
5351 | /* Should not be able to add duplicates */ | |
710756a9 RS |
5352 | if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1, |
5353 | old_add_cb, old_free_cb, | |
5354 | &server, old_parse_cb, | |
5355 | &server)) | |
5356 | || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, | |
5357 | context, new_add_cb, | |
5358 | new_free_cb, &server, | |
5359 | new_parse_cb, &server))) | |
a37008d9 | 5360 | goto end; |
a37008d9 | 5361 | |
bb01ef3f MC |
5362 | if (tst == 2) { |
5363 | /* Set up SNI */ | |
5364 | if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb)) | |
5365 | || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2))) | |
5366 | goto end; | |
5367 | } | |
5368 | ||
710756a9 RS |
5369 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, |
5370 | &clientssl, NULL, NULL)) | |
5371 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5372 | SSL_ERROR_NONE))) | |
a37008d9 | 5373 | goto end; |
a37008d9 MC |
5374 | |
5375 | if (tst == 0) { | |
710756a9 RS |
5376 | if (clntaddoldcb != 1 |
5377 | || clntparseoldcb != 1 | |
5378 | || srvaddoldcb != 1 | |
5379 | || srvparseoldcb != 1) | |
a37008d9 | 5380 | goto end; |
bb01ef3f | 5381 | } else if (tst == 1 || tst == 2 || tst == 3) { |
710756a9 RS |
5382 | if (clntaddnewcb != 1 |
5383 | || clntparsenewcb != 1 | |
5384 | || srvaddnewcb != 1 | |
bb01ef3f MC |
5385 | || srvparsenewcb != 1 |
5386 | || (tst != 2 && snicb != 0) | |
5387 | || (tst == 2 && snicb != 1)) | |
a37008d9 | 5388 | goto end; |
a37008d9 | 5389 | } else { |
36ff232c | 5390 | /* In this case there 2 NewSessionTicket messages created */ |
710756a9 | 5391 | if (clntaddnewcb != 1 |
36ff232c MC |
5392 | || clntparsenewcb != 5 |
5393 | || srvaddnewcb != 5 | |
710756a9 | 5394 | || srvparsenewcb != 1) |
a37008d9 | 5395 | goto end; |
a37008d9 MC |
5396 | } |
5397 | ||
5398 | sess = SSL_get1_session(clientssl); | |
a37008d9 MC |
5399 | SSL_shutdown(clientssl); |
5400 | SSL_shutdown(serverssl); | |
a37008d9 MC |
5401 | SSL_free(serverssl); |
5402 | SSL_free(clientssl); | |
5403 | serverssl = clientssl = NULL; | |
5404 | ||
bb01ef3f MC |
5405 | if (tst == 3) { |
5406 | /* We don't bother with the resumption aspects for this test */ | |
5407 | testresult = 1; | |
5408 | goto end; | |
5409 | } | |
5410 | ||
710756a9 RS |
5411 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
5412 | NULL, NULL)) | |
5413 | || !TEST_true(SSL_set_session(clientssl, sess)) | |
5414 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5415 | SSL_ERROR_NONE))) | |
a37008d9 | 5416 | goto end; |
a37008d9 MC |
5417 | |
5418 | /* | |
5419 | * For a resumed session we expect to add the ClientHello extension. For the | |
5420 | * old style callbacks we ignore it on the server side because they set | |
5421 | * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore | |
5422 | * them. | |
5423 | */ | |
5424 | if (tst == 0) { | |
710756a9 RS |
5425 | if (clntaddoldcb != 2 |
5426 | || clntparseoldcb != 1 | |
5427 | || srvaddoldcb != 1 | |
5428 | || srvparseoldcb != 1) | |
a37008d9 | 5429 | goto end; |
bb01ef3f | 5430 | } else if (tst == 1 || tst == 2 || tst == 3) { |
710756a9 RS |
5431 | if (clntaddnewcb != 2 |
5432 | || clntparsenewcb != 2 | |
5433 | || srvaddnewcb != 2 | |
5434 | || srvparsenewcb != 2) | |
a37008d9 | 5435 | goto end; |
a37008d9 | 5436 | } else { |
36ff232c MC |
5437 | /* |
5438 | * No Certificate message extensions in the resumption handshake, | |
5439 | * 2 NewSessionTickets in the initial handshake, 1 in the resumption | |
5440 | */ | |
710756a9 | 5441 | if (clntaddnewcb != 2 |
36ff232c MC |
5442 | || clntparsenewcb != 8 |
5443 | || srvaddnewcb != 8 | |
710756a9 | 5444 | || srvparsenewcb != 2) |
a37008d9 | 5445 | goto end; |
a37008d9 MC |
5446 | } |
5447 | ||
5448 | testresult = 1; | |
5449 | ||
5450 | end: | |
5451 | SSL_SESSION_free(sess); | |
5452 | SSL_free(serverssl); | |
5453 | SSL_free(clientssl); | |
bb01ef3f | 5454 | SSL_CTX_free(sctx2); |
a37008d9 MC |
5455 | SSL_CTX_free(sctx); |
5456 | SSL_CTX_free(cctx); | |
a37008d9 MC |
5457 | return testresult; |
5458 | } | |
5459 | ||
16afd71c MC |
5460 | /* |
5461 | * Test loading of serverinfo data in various formats. test_sslmessages actually | |
5462 | * tests to make sure the extensions appear in the handshake | |
5463 | */ | |
5464 | static int test_serverinfo(int tst) | |
5465 | { | |
5466 | unsigned int version; | |
5467 | unsigned char *sibuf; | |
5468 | size_t sibuflen; | |
5469 | int ret, expected, testresult = 0; | |
5470 | SSL_CTX *ctx; | |
5471 | ||
5e30f2fd | 5472 | ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_method()); |
16afd71c MC |
5473 | if (!TEST_ptr(ctx)) |
5474 | goto end; | |
5475 | ||
5476 | if ((tst & 0x01) == 0x01) | |
5477 | version = SSL_SERVERINFOV2; | |
5478 | else | |
5479 | version = SSL_SERVERINFOV1; | |
5480 | ||
5481 | if ((tst & 0x02) == 0x02) { | |
5482 | sibuf = serverinfov2; | |
5483 | sibuflen = sizeof(serverinfov2); | |
5484 | expected = (version == SSL_SERVERINFOV2); | |
5485 | } else { | |
5486 | sibuf = serverinfov1; | |
5487 | sibuflen = sizeof(serverinfov1); | |
5488 | expected = (version == SSL_SERVERINFOV1); | |
5489 | } | |
5490 | ||
5491 | if ((tst & 0x04) == 0x04) { | |
5492 | ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen); | |
5493 | } else { | |
5494 | ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen); | |
5495 | ||
5496 | /* | |
5497 | * The version variable is irrelevant in this case - it's what is in the | |
5498 | * buffer that matters | |
5499 | */ | |
5500 | if ((tst & 0x02) == 0x02) | |
5501 | expected = 0; | |
5502 | else | |
5503 | expected = 1; | |
5504 | } | |
5505 | ||
5506 | if (!TEST_true(ret == expected)) | |
5507 | goto end; | |
5508 | ||
5509 | testresult = 1; | |
5510 | ||
5511 | end: | |
5512 | SSL_CTX_free(ctx); | |
5513 | ||
5514 | return testresult; | |
5515 | } | |
5516 | ||
2197d1df MC |
5517 | /* |
5518 | * Test that SSL_export_keying_material() produces expected results. There are | |
5519 | * no test vectors so all we do is test that both sides of the communication | |
5520 | * produce the same results for different protocol versions. | |
5521 | */ | |
0fb2815b MC |
5522 | #define SMALL_LABEL_LEN 10 |
5523 | #define LONG_LABEL_LEN 249 | |
2197d1df MC |
5524 | static int test_export_key_mat(int tst) |
5525 | { | |
a599574b | 5526 | int testresult = 0; |
2197d1df MC |
5527 | SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL; |
5528 | SSL *clientssl = NULL, *serverssl = NULL; | |
0fb2815b | 5529 | const char label[LONG_LABEL_LEN + 1] = "test label"; |
2197d1df MC |
5530 | const unsigned char context[] = "context"; |
5531 | const unsigned char *emptycontext = NULL; | |
5532 | unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80]; | |
5533 | unsigned char skeymat1[80], skeymat2[80], skeymat3[80]; | |
0fb2815b | 5534 | size_t labellen; |
a599574b MC |
5535 | const int protocols[] = { |
5536 | TLS1_VERSION, | |
5537 | TLS1_1_VERSION, | |
5538 | TLS1_2_VERSION, | |
0fb2815b MC |
5539 | TLS1_3_VERSION, |
5540 | TLS1_3_VERSION, | |
a599574b MC |
5541 | TLS1_3_VERSION |
5542 | }; | |
2197d1df MC |
5543 | |
5544 | #ifdef OPENSSL_NO_TLS1 | |
5545 | if (tst == 0) | |
5546 | return 1; | |
5547 | #endif | |
5548 | #ifdef OPENSSL_NO_TLS1_1 | |
5549 | if (tst == 1) | |
5550 | return 1; | |
5551 | #endif | |
4f6c7044 MC |
5552 | if (is_fips && (tst == 0 || tst == 1)) |
5553 | return 1; | |
2197d1df MC |
5554 | #ifdef OPENSSL_NO_TLS1_2 |
5555 | if (tst == 2) | |
5556 | return 1; | |
5557 | #endif | |
5558 | #ifdef OPENSSL_NO_TLS1_3 | |
0fb2815b | 5559 | if (tst >= 3) |
2197d1df MC |
5560 | return 1; |
5561 | #endif | |
5e30f2fd MC |
5562 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5563 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 5564 | &sctx, &cctx, cert, privkey))) |
2197d1df MC |
5565 | goto end; |
5566 | ||
a599574b MC |
5567 | OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols)); |
5568 | SSL_CTX_set_max_proto_version(cctx, protocols[tst]); | |
5569 | SSL_CTX_set_min_proto_version(cctx, protocols[tst]); | |
aba03ae5 KR |
5570 | if ((protocols[tst] < TLS1_2_VERSION) && |
5571 | (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0") | |
5572 | || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))) | |
5573 | goto end; | |
2197d1df MC |
5574 | |
5575 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, | |
5576 | NULL)) | |
5577 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5578 | SSL_ERROR_NONE))) | |
5579 | goto end; | |
5580 | ||
0fb2815b MC |
5581 | if (tst == 5) { |
5582 | /* | |
5583 | * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we | |
5584 | * go over that. | |
5585 | */ | |
5586 | if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1, | |
5587 | sizeof(ckeymat1), label, | |
5588 | LONG_LABEL_LEN + 1, context, | |
5589 | sizeof(context) - 1, 1), 0)) | |
5590 | goto end; | |
5591 | ||
5592 | testresult = 1; | |
5593 | goto end; | |
5594 | } else if (tst == 4) { | |
5595 | labellen = LONG_LABEL_LEN; | |
5596 | } else { | |
5597 | labellen = SMALL_LABEL_LEN; | |
5598 | } | |
5599 | ||
2197d1df MC |
5600 | if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1, |
5601 | sizeof(ckeymat1), label, | |
0fb2815b | 5602 | labellen, context, |
2197d1df MC |
5603 | sizeof(context) - 1, 1), 1) |
5604 | || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2, | |
5605 | sizeof(ckeymat2), label, | |
0fb2815b | 5606 | labellen, |
2197d1df MC |
5607 | emptycontext, |
5608 | 0, 1), 1) | |
5609 | || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3, | |
5610 | sizeof(ckeymat3), label, | |
0fb2815b | 5611 | labellen, |
2197d1df MC |
5612 | NULL, 0, 0), 1) |
5613 | || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1, | |
5614 | sizeof(skeymat1), label, | |
0fb2815b | 5615 | labellen, |
2197d1df MC |
5616 | context, |
5617 | sizeof(context) -1, 1), | |
5618 | 1) | |
5619 | || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2, | |
5620 | sizeof(skeymat2), label, | |
0fb2815b | 5621 | labellen, |
2197d1df MC |
5622 | emptycontext, |
5623 | 0, 1), 1) | |
5624 | || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3, | |
5625 | sizeof(skeymat3), label, | |
0fb2815b | 5626 | labellen, |
2197d1df MC |
5627 | NULL, 0, 0), 1) |
5628 | /* | |
5629 | * Check that both sides created the same key material with the | |
5630 | * same context. | |
5631 | */ | |
5632 | || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1, | |
5633 | sizeof(skeymat1)) | |
5634 | /* | |
5635 | * Check that both sides created the same key material with an | |
5636 | * empty context. | |
5637 | */ | |
5638 | || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2, | |
5639 | sizeof(skeymat2)) | |
5640 | /* | |
5641 | * Check that both sides created the same key material without a | |
5642 | * context. | |
5643 | */ | |
5644 | || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3, | |
5645 | sizeof(skeymat3)) | |
5646 | /* Different contexts should produce different results */ | |
5647 | || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2, | |
5648 | sizeof(ckeymat2))) | |
5649 | goto end; | |
5650 | ||
5651 | /* | |
5652 | * Check that an empty context and no context produce different results in | |
5653 | * protocols less than TLSv1.3. In TLSv1.3 they should be the same. | |
5654 | */ | |
0fb2815b | 5655 | if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3, |
2197d1df | 5656 | sizeof(ckeymat3))) |
0fb2815b MC |
5657 | || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3, |
5658 | sizeof(ckeymat3)))) | |
2197d1df MC |
5659 | goto end; |
5660 | ||
5661 | testresult = 1; | |
5662 | ||
5663 | end: | |
5664 | SSL_free(serverssl); | |
5665 | SSL_free(clientssl); | |
5666 | SSL_CTX_free(sctx2); | |
5667 | SSL_CTX_free(sctx); | |
5668 | SSL_CTX_free(cctx); | |
5669 | ||
5670 | return testresult; | |
5671 | } | |
5672 | ||
b38ede80 TT |
5673 | #ifndef OPENSSL_NO_TLS1_3 |
5674 | /* | |
5675 | * Test that SSL_export_keying_material_early() produces expected | |
5676 | * results. There are no test vectors so all we do is test that both | |
5677 | * sides of the communication produce the same results for different | |
5678 | * protocol versions. | |
5679 | */ | |
5680 | static int test_export_key_mat_early(int idx) | |
5681 | { | |
5682 | static const char label[] = "test label"; | |
5683 | static const unsigned char context[] = "context"; | |
5684 | int testresult = 0; | |
5685 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
5686 | SSL *clientssl = NULL, *serverssl = NULL; | |
5687 | SSL_SESSION *sess = NULL; | |
5688 | const unsigned char *emptycontext = NULL; | |
5689 | unsigned char ckeymat1[80], ckeymat2[80]; | |
5690 | unsigned char skeymat1[80], skeymat2[80]; | |
5691 | unsigned char buf[1]; | |
5692 | size_t readbytes, written; | |
5693 | ||
5694 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, | |
5695 | &sess, idx))) | |
5696 | goto end; | |
5697 | ||
5698 | /* Here writing 0 length early data is enough. */ | |
5699 | if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written)) | |
5700 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf), | |
5701 | &readbytes), | |
5702 | SSL_READ_EARLY_DATA_ERROR) | |
5703 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
5704 | SSL_EARLY_DATA_ACCEPTED)) | |
5705 | goto end; | |
5706 | ||
5707 | if (!TEST_int_eq(SSL_export_keying_material_early( | |
5708 | clientssl, ckeymat1, sizeof(ckeymat1), label, | |
5709 | sizeof(label) - 1, context, sizeof(context) - 1), 1) | |
5710 | || !TEST_int_eq(SSL_export_keying_material_early( | |
5711 | clientssl, ckeymat2, sizeof(ckeymat2), label, | |
5712 | sizeof(label) - 1, emptycontext, 0), 1) | |
5713 | || !TEST_int_eq(SSL_export_keying_material_early( | |
5714 | serverssl, skeymat1, sizeof(skeymat1), label, | |
5715 | sizeof(label) - 1, context, sizeof(context) - 1), 1) | |
5716 | || !TEST_int_eq(SSL_export_keying_material_early( | |
5717 | serverssl, skeymat2, sizeof(skeymat2), label, | |
5718 | sizeof(label) - 1, emptycontext, 0), 1) | |
5719 | /* | |
5720 | * Check that both sides created the same key material with the | |
5721 | * same context. | |
5722 | */ | |
5723 | || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1, | |
5724 | sizeof(skeymat1)) | |
5725 | /* | |
5726 | * Check that both sides created the same key material with an | |
5727 | * empty context. | |
5728 | */ | |
5729 | || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2, | |
5730 | sizeof(skeymat2)) | |
5731 | /* Different contexts should produce different results */ | |
5732 | || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2, | |
5733 | sizeof(ckeymat2))) | |
5734 | goto end; | |
5735 | ||
5736 | testresult = 1; | |
5737 | ||
5738 | end: | |
c20e3b28 | 5739 | SSL_SESSION_free(sess); |
b38ede80 TT |
5740 | SSL_SESSION_free(clientpsk); |
5741 | SSL_SESSION_free(serverpsk); | |
34ff74eb | 5742 | clientpsk = serverpsk = NULL; |
b38ede80 TT |
5743 | SSL_free(serverssl); |
5744 | SSL_free(clientssl); | |
5745 | SSL_CTX_free(sctx); | |
5746 | SSL_CTX_free(cctx); | |
5747 | ||
5748 | return testresult; | |
5749 | } | |
3409a5ff MC |
5750 | |
5751 | #define NUM_KEY_UPDATE_MESSAGES 40 | |
5752 | /* | |
5753 | * Test KeyUpdate. | |
5754 | */ | |
5755 | static int test_key_update(void) | |
5756 | { | |
5757 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
5758 | SSL *clientssl = NULL, *serverssl = NULL; | |
5759 | int testresult = 0, i, j; | |
5760 | char buf[20]; | |
5761 | static char *mess = "A test message"; | |
5762 | ||
5e30f2fd | 5763 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
3409a5ff MC |
5764 | TLS_client_method(), |
5765 | TLS1_3_VERSION, | |
5766 | 0, | |
5767 | &sctx, &cctx, cert, privkey)) | |
5768 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
5769 | NULL, NULL)) | |
5770 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5771 | SSL_ERROR_NONE))) | |
5772 | goto end; | |
5773 | ||
5774 | for (j = 0; j < 2; j++) { | |
5775 | /* Send lots of KeyUpdate messages */ | |
5776 | for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) { | |
5777 | if (!TEST_true(SSL_key_update(clientssl, | |
5778 | (j == 0) | |
5779 | ? SSL_KEY_UPDATE_NOT_REQUESTED | |
5780 | : SSL_KEY_UPDATE_REQUESTED)) | |
5781 | || !TEST_true(SSL_do_handshake(clientssl))) | |
5782 | goto end; | |
5783 | } | |
5784 | ||
5785 | /* Check that sending and receiving app data is ok */ | |
5786 | if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess)) | |
5787 | || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)), | |
5788 | strlen(mess))) | |
5789 | goto end; | |
a77b4dba MC |
5790 | |
5791 | if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess)) | |
5792 | || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)), | |
5793 | strlen(mess))) | |
5794 | goto end; | |
3409a5ff MC |
5795 | } |
5796 | ||
5797 | testresult = 1; | |
5798 | ||
5799 | end: | |
5800 | SSL_free(serverssl); | |
5801 | SSL_free(clientssl); | |
5802 | SSL_CTX_free(sctx); | |
5803 | SSL_CTX_free(cctx); | |
5804 | ||
5805 | return testresult; | |
5806 | } | |
a77b4dba MC |
5807 | |
5808 | /* | |
5809 | * Test we can handle a KeyUpdate (update requested) message while write data | |
5810 | * is pending. | |
5811 | * Test 0: Client sends KeyUpdate while Server is writing | |
5812 | * Test 1: Server sends KeyUpdate while Client is writing | |
5813 | */ | |
5814 | static int test_key_update_in_write(int tst) | |
5815 | { | |
5816 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
5817 | SSL *clientssl = NULL, *serverssl = NULL; | |
5818 | int testresult = 0; | |
5819 | char buf[20]; | |
5820 | static char *mess = "A test message"; | |
5821 | BIO *bretry = BIO_new(bio_s_always_retry()); | |
5822 | BIO *tmp = NULL; | |
5823 | SSL *peerupdate = NULL, *peerwrite = NULL; | |
5824 | ||
5825 | if (!TEST_ptr(bretry) | |
5e30f2fd | 5826 | || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
a77b4dba MC |
5827 | TLS_client_method(), |
5828 | TLS1_3_VERSION, | |
5829 | 0, | |
5830 | &sctx, &cctx, cert, privkey)) | |
5831 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
5832 | NULL, NULL)) | |
5833 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5834 | SSL_ERROR_NONE))) | |
5835 | goto end; | |
5836 | ||
5837 | peerupdate = tst == 0 ? clientssl : serverssl; | |
5838 | peerwrite = tst == 0 ? serverssl : clientssl; | |
5839 | ||
5840 | if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED)) | |
5841 | || !TEST_true(SSL_do_handshake(peerupdate))) | |
5842 | goto end; | |
5843 | ||
5844 | /* Swap the writing endpoint's write BIO to force a retry */ | |
5845 | tmp = SSL_get_wbio(peerwrite); | |
5846 | if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) { | |
5847 | tmp = NULL; | |
5848 | goto end; | |
5849 | } | |
5850 | SSL_set0_wbio(peerwrite, bretry); | |
5851 | bretry = NULL; | |
5852 | ||
5853 | /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */ | |
5854 | if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1) | |
5855 | || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE)) | |
5856 | goto end; | |
5857 | ||
5858 | /* Reinstate the original writing endpoint's write BIO */ | |
5859 | SSL_set0_wbio(peerwrite, tmp); | |
5860 | tmp = NULL; | |
5861 | ||
5862 | /* Now read some data - we will read the key update */ | |
5863 | if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1) | |
5864 | || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ)) | |
5865 | goto end; | |
5866 | ||
5867 | /* | |
5868 | * Complete the write we started previously and read it from the other | |
5869 | * endpoint | |
5870 | */ | |
5871 | if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess)) | |
5872 | || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess))) | |
5873 | goto end; | |
5874 | ||
5875 | /* Write more data to ensure we send the KeyUpdate message back */ | |
5876 | if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess)) | |
5877 | || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess))) | |
5878 | goto end; | |
5879 | ||
5880 | testresult = 1; | |
5881 | ||
5882 | end: | |
5883 | SSL_free(serverssl); | |
5884 | SSL_free(clientssl); | |
5885 | SSL_CTX_free(sctx); | |
5886 | SSL_CTX_free(cctx); | |
5887 | BIO_free(bretry); | |
5888 | BIO_free(tmp); | |
5889 | ||
5890 | return testresult; | |
5891 | } | |
b38ede80 TT |
5892 | #endif /* OPENSSL_NO_TLS1_3 */ |
5893 | ||
e11b6aa4 MC |
5894 | static int test_ssl_clear(int idx) |
5895 | { | |
5896 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
5897 | SSL *clientssl = NULL, *serverssl = NULL; | |
5898 | int testresult = 0; | |
5899 | ||
5900 | #ifdef OPENSSL_NO_TLS1_2 | |
5901 | if (idx == 1) | |
5902 | return 1; | |
5903 | #endif | |
5904 | ||
5905 | /* Create an initial connection */ | |
5e30f2fd MC |
5906 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5907 | TLS_client_method(), TLS1_VERSION, 0, | |
7d7f6834 | 5908 | &sctx, &cctx, cert, privkey)) |
e11b6aa4 MC |
5909 | || (idx == 1 |
5910 | && !TEST_true(SSL_CTX_set_max_proto_version(cctx, | |
5911 | TLS1_2_VERSION))) | |
5912 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
5913 | &clientssl, NULL, NULL)) | |
5914 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5915 | SSL_ERROR_NONE))) | |
5916 | goto end; | |
5917 | ||
5918 | SSL_shutdown(clientssl); | |
5919 | SSL_shutdown(serverssl); | |
5920 | SSL_free(serverssl); | |
5921 | serverssl = NULL; | |
5922 | ||
5923 | /* Clear clientssl - we're going to reuse the object */ | |
5924 | if (!TEST_true(SSL_clear(clientssl))) | |
5925 | goto end; | |
5926 | ||
5927 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
5928 | NULL, NULL)) | |
5929 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
5930 | SSL_ERROR_NONE)) | |
5931 | || !TEST_true(SSL_session_reused(clientssl))) | |
5932 | goto end; | |
5933 | ||
5934 | SSL_shutdown(clientssl); | |
5935 | SSL_shutdown(serverssl); | |
5936 | ||
5937 | testresult = 1; | |
5938 | ||
5939 | end: | |
5940 | SSL_free(serverssl); | |
5941 | SSL_free(clientssl); | |
5942 | SSL_CTX_free(sctx); | |
5943 | SSL_CTX_free(cctx); | |
5944 | ||
5945 | return testresult; | |
5946 | } | |
5947 | ||
cf72c757 F |
5948 | /* Parse CH and retrieve any MFL extension value if present */ |
5949 | static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code) | |
5950 | { | |
5951 | long len; | |
5952 | unsigned char *data; | |
72962d02 | 5953 | PACKET pkt, pkt2, pkt3; |
cf72c757 F |
5954 | unsigned int MFL_code = 0, type = 0; |
5955 | ||
5956 | if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) ) | |
5957 | goto end; | |
5958 | ||
72962d02 P |
5959 | memset(&pkt, 0, sizeof(pkt)); |
5960 | memset(&pkt2, 0, sizeof(pkt2)); | |
5961 | memset(&pkt3, 0, sizeof(pkt3)); | |
5962 | ||
cf72c757 F |
5963 | if (!TEST_true( PACKET_buf_init( &pkt, data, len ) ) |
5964 | /* Skip the record header */ | |
5965 | || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH) | |
5966 | /* Skip the handshake message header */ | |
5967 | || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH)) | |
5968 | /* Skip client version and random */ | |
5969 | || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN | |
5970 | + SSL3_RANDOM_SIZE)) | |
5971 | /* Skip session id */ | |
5972 | || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) | |
5973 | /* Skip ciphers */ | |
5974 | || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2)) | |
5975 | /* Skip compression */ | |
5976 | || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2)) | |
5977 | /* Extensions len */ | |
5978 | || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2))) | |
5979 | goto end; | |
5980 | ||
5981 | /* Loop through all extensions */ | |
5982 | while (PACKET_remaining(&pkt2)) { | |
5983 | if (!TEST_true(PACKET_get_net_2(&pkt2, &type)) | |
5984 | || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3))) | |
5985 | goto end; | |
5986 | ||
5987 | if (type == TLSEXT_TYPE_max_fragment_length) { | |
5988 | if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0) | |
5989 | || !TEST_true(PACKET_get_1(&pkt3, &MFL_code))) | |
5990 | goto end; | |
5991 | ||
5992 | *mfl_codemfl_code = MFL_code; | |
5993 | return 1; | |
5994 | } | |
5995 | } | |
5996 | ||
5997 | end: | |
5998 | return 0; | |
5999 | } | |
6000 | ||
6001 | /* Maximum-Fragment-Length TLS extension mode to test */ | |
6002 | static const unsigned char max_fragment_len_test[] = { | |
6003 | TLSEXT_max_fragment_length_512, | |
6004 | TLSEXT_max_fragment_length_1024, | |
6005 | TLSEXT_max_fragment_length_2048, | |
6006 | TLSEXT_max_fragment_length_4096 | |
6007 | }; | |
6008 | ||
6009 | static int test_max_fragment_len_ext(int idx_tst) | |
6010 | { | |
6011 | SSL_CTX *ctx; | |
6012 | SSL *con = NULL; | |
6013 | int testresult = 0, MFL_mode = 0; | |
6014 | BIO *rbio, *wbio; | |
6015 | ||
5e30f2fd | 6016 | ctx = SSL_CTX_new_with_libctx(libctx, NULL, TLS_method()); |
cf72c757 F |
6017 | if (!TEST_ptr(ctx)) |
6018 | goto end; | |
6019 | ||
6020 | if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length( | |
6021 | ctx, max_fragment_len_test[idx_tst]))) | |
6022 | goto end; | |
6023 | ||
6024 | con = SSL_new(ctx); | |
6025 | if (!TEST_ptr(con)) | |
6026 | goto end; | |
6027 | ||
6028 | rbio = BIO_new(BIO_s_mem()); | |
6029 | wbio = BIO_new(BIO_s_mem()); | |
6030 | if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) { | |
6031 | BIO_free(rbio); | |
6032 | BIO_free(wbio); | |
6033 | goto end; | |
6034 | } | |
6035 | ||
6036 | SSL_set_bio(con, rbio, wbio); | |
6037 | SSL_set_connect_state(con); | |
6038 | ||
6039 | if (!TEST_int_le(SSL_connect(con), 0)) { | |
6040 | /* This shouldn't succeed because we don't have a server! */ | |
6041 | goto end; | |
6042 | } | |
6043 | ||
6044 | if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode))) | |
6045 | /* no MFL in client hello */ | |
6046 | goto end; | |
6047 | if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode)) | |
6048 | goto end; | |
6049 | ||
6050 | testresult = 1; | |
6051 | ||
6052 | end: | |
6053 | SSL_free(con); | |
6054 | SSL_CTX_free(ctx); | |
6055 | ||
6056 | return testresult; | |
6057 | } | |
6058 | ||
9d75dce3 TS |
6059 | #ifndef OPENSSL_NO_TLS1_3 |
6060 | static int test_pha_key_update(void) | |
6061 | { | |
6062 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6063 | SSL *clientssl = NULL, *serverssl = NULL; | |
6064 | int testresult = 0; | |
6065 | ||
5e30f2fd MC |
6066 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
6067 | TLS_client_method(), TLS1_VERSION, 0, | |
9d75dce3 TS |
6068 | &sctx, &cctx, cert, privkey))) |
6069 | return 0; | |
6070 | ||
6071 | if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION)) | |
6072 | || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION)) | |
6073 | || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION)) | |
6074 | || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION))) | |
6075 | goto end; | |
6076 | ||
e97be718 | 6077 | SSL_CTX_set_post_handshake_auth(cctx, 1); |
9d75dce3 TS |
6078 | |
6079 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
6080 | NULL, NULL))) | |
6081 | goto end; | |
6082 | ||
9d75dce3 TS |
6083 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, |
6084 | SSL_ERROR_NONE))) | |
6085 | goto end; | |
6086 | ||
6087 | SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); | |
6088 | if (!TEST_true(SSL_verify_client_post_handshake(serverssl))) | |
6089 | goto end; | |
6090 | ||
6091 | if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED))) | |
6092 | goto end; | |
6093 | ||
6094 | /* Start handshake on the server */ | |
6095 | if (!TEST_int_eq(SSL_do_handshake(serverssl), 1)) | |
6096 | goto end; | |
6097 | ||
6098 | /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */ | |
6099 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
6100 | SSL_ERROR_NONE))) | |
6101 | goto end; | |
6102 | ||
6103 | SSL_shutdown(clientssl); | |
6104 | SSL_shutdown(serverssl); | |
6105 | ||
6106 | testresult = 1; | |
6107 | ||
6108 | end: | |
6109 | SSL_free(serverssl); | |
6110 | SSL_free(clientssl); | |
6111 | SSL_CTX_free(sctx); | |
6112 | SSL_CTX_free(cctx); | |
6113 | return testresult; | |
6114 | } | |
6115 | #endif | |
6116 | ||
76fd7a1d MC |
6117 | #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2) |
6118 | ||
6119 | static SRP_VBASE *vbase = NULL; | |
6120 | ||
852c2ed2 RS |
6121 | DEFINE_STACK_OF(SRP_user_pwd) |
6122 | ||
76fd7a1d MC |
6123 | static int ssl_srp_cb(SSL *s, int *ad, void *arg) |
6124 | { | |
6125 | int ret = SSL3_AL_FATAL; | |
6126 | char *username; | |
6127 | SRP_user_pwd *user = NULL; | |
6128 | ||
6129 | username = SSL_get_srp_username(s); | |
6130 | if (username == NULL) { | |
6131 | *ad = SSL_AD_INTERNAL_ERROR; | |
6132 | goto err; | |
6133 | } | |
6134 | ||
6135 | user = SRP_VBASE_get1_by_user(vbase, username); | |
6136 | if (user == NULL) { | |
6137 | *ad = SSL_AD_INTERNAL_ERROR; | |
6138 | goto err; | |
6139 | } | |
6140 | ||
6141 | if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v, | |
6142 | user->info) <= 0) { | |
6143 | *ad = SSL_AD_INTERNAL_ERROR; | |
6144 | goto err; | |
6145 | } | |
6146 | ||
6147 | ret = 0; | |
6148 | ||
6149 | err: | |
6150 | SRP_user_pwd_free(user); | |
6151 | return ret; | |
6152 | } | |
6153 | ||
6154 | static int create_new_vfile(char *userid, char *password, const char *filename) | |
6155 | { | |
6156 | char *gNid = NULL; | |
6157 | OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1)); | |
6158 | TXT_DB *db = NULL; | |
6159 | int ret = 0; | |
6160 | BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0); | |
6161 | size_t i; | |
6162 | ||
6163 | if (!TEST_ptr(dummy) || !TEST_ptr(row)) | |
6164 | goto end; | |
6165 | ||
20c00d0a | 6166 | gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt], |
5e30f2fd | 6167 | &row[DB_srpverifier], NULL, NULL, libctx, NULL); |
76fd7a1d MC |
6168 | if (!TEST_ptr(gNid)) |
6169 | goto end; | |
6170 | ||
6171 | /* | |
6172 | * The only way to create an empty TXT_DB is to provide a BIO with no data | |
6173 | * in it! | |
6174 | */ | |
6175 | db = TXT_DB_read(dummy, DB_NUMBER); | |
6176 | if (!TEST_ptr(db)) | |
6177 | goto end; | |
6178 | ||
6179 | out = BIO_new_file(filename, "w"); | |
6180 | if (!TEST_ptr(out)) | |
6181 | goto end; | |
6182 | ||
6183 | row[DB_srpid] = OPENSSL_strdup(userid); | |
6184 | row[DB_srptype] = OPENSSL_strdup("V"); | |
6185 | row[DB_srpgN] = OPENSSL_strdup(gNid); | |
6186 | ||
6187 | if (!TEST_ptr(row[DB_srpid]) | |
6188 | || !TEST_ptr(row[DB_srptype]) | |
6189 | || !TEST_ptr(row[DB_srpgN]) | |
6190 | || !TEST_true(TXT_DB_insert(db, row))) | |
6191 | goto end; | |
6192 | ||
6193 | row = NULL; | |
6194 | ||
6195 | if (!TXT_DB_write(out, db)) | |
6196 | goto end; | |
6197 | ||
6198 | ret = 1; | |
6199 | end: | |
6200 | if (row != NULL) { | |
6201 | for (i = 0; i < DB_NUMBER; i++) | |
6202 | OPENSSL_free(row[i]); | |
6203 | } | |
6204 | OPENSSL_free(row); | |
6205 | BIO_free(dummy); | |
6206 | BIO_free(out); | |
6207 | TXT_DB_free(db); | |
6208 | ||
6209 | return ret; | |
6210 | } | |
6211 | ||
6212 | static int create_new_vbase(char *userid, char *password) | |
6213 | { | |
6214 | BIGNUM *verifier = NULL, *salt = NULL; | |
6215 | const SRP_gN *lgN = NULL; | |
6216 | SRP_user_pwd *user_pwd = NULL; | |
6217 | int ret = 0; | |
6218 | ||
6219 | lgN = SRP_get_default_gN(NULL); | |
6220 | if (!TEST_ptr(lgN)) | |
6221 | goto end; | |
6222 | ||
20c00d0a | 6223 | if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier, |
5e30f2fd | 6224 | lgN->N, lgN->g, libctx, NULL))) |
76fd7a1d MC |
6225 | goto end; |
6226 | ||
6227 | user_pwd = OPENSSL_zalloc(sizeof(*user_pwd)); | |
6228 | if (!TEST_ptr(user_pwd)) | |
6229 | goto end; | |
6230 | ||
6231 | user_pwd->N = lgN->N; | |
6232 | user_pwd->g = lgN->g; | |
6233 | user_pwd->id = OPENSSL_strdup(userid); | |
6234 | if (!TEST_ptr(user_pwd->id)) | |
6235 | goto end; | |
6236 | ||
6237 | user_pwd->v = verifier; | |
6238 | user_pwd->s = salt; | |
6239 | verifier = salt = NULL; | |
6240 | ||
6241 | if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0) | |
6242 | goto end; | |
6243 | user_pwd = NULL; | |
6244 | ||
6245 | ret = 1; | |
6246 | end: | |
6247 | SRP_user_pwd_free(user_pwd); | |
6248 | BN_free(salt); | |
6249 | BN_free(verifier); | |
6250 | ||
6251 | return ret; | |
6252 | } | |
6253 | ||
6254 | /* | |
6255 | * SRP tests | |
6256 | * | |
6257 | * Test 0: Simple successful SRP connection, new vbase | |
6258 | * Test 1: Connection failure due to bad password, new vbase | |
6259 | * Test 2: Simple successful SRP connection, vbase loaded from existing file | |
6260 | * Test 3: Connection failure due to bad password, vbase loaded from existing | |
6261 | * file | |
6262 | * Test 4: Simple successful SRP connection, vbase loaded from new file | |
6263 | * Test 5: Connection failure due to bad password, vbase loaded from new file | |
6264 | */ | |
6265 | static int test_srp(int tst) | |
6266 | { | |
6267 | char *userid = "test", *password = "password", *tstsrpfile; | |
6268 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6269 | SSL *clientssl = NULL, *serverssl = NULL; | |
6270 | int ret, testresult = 0; | |
6271 | ||
6272 | vbase = SRP_VBASE_new(NULL); | |
6273 | if (!TEST_ptr(vbase)) | |
6274 | goto end; | |
6275 | ||
6276 | if (tst == 0 || tst == 1) { | |
6277 | if (!TEST_true(create_new_vbase(userid, password))) | |
6278 | goto end; | |
6279 | } else { | |
6280 | if (tst == 4 || tst == 5) { | |
6281 | if (!TEST_true(create_new_vfile(userid, password, tmpfilename))) | |
6282 | goto end; | |
6283 | tstsrpfile = tmpfilename; | |
6284 | } else { | |
6285 | tstsrpfile = srpvfile; | |
6286 | } | |
6287 | if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR)) | |
6288 | goto end; | |
6289 | } | |
6290 | ||
5e30f2fd MC |
6291 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
6292 | TLS_client_method(), TLS1_VERSION, 0, | |
76fd7a1d MC |
6293 | &sctx, &cctx, cert, privkey))) |
6294 | goto end; | |
6295 | ||
6296 | if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0) | |
6297 | || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA")) | |
6298 | || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION)) | |
6299 | || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)) | |
6300 | || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0)) | |
6301 | goto end; | |
6302 | ||
6303 | if (tst % 2 == 1) { | |
6304 | if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0)) | |
6305 | goto end; | |
6306 | } else { | |
6307 | if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0)) | |
6308 | goto end; | |
6309 | } | |
6310 | ||
6311 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
6312 | NULL, NULL))) | |
6313 | goto end; | |
6314 | ||
6315 | ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); | |
6316 | if (ret) { | |
6317 | if (!TEST_true(tst % 2 == 0)) | |
6318 | goto end; | |
6319 | } else { | |
6320 | if (!TEST_true(tst % 2 == 1)) | |
6321 | goto end; | |
6322 | } | |
6323 | ||
6324 | testresult = 1; | |
6325 | ||
6326 | end: | |
6327 | SRP_VBASE_free(vbase); | |
6328 | vbase = NULL; | |
6329 | SSL_free(serverssl); | |
6330 | SSL_free(clientssl); | |
6331 | SSL_CTX_free(sctx); | |
6332 | SSL_CTX_free(cctx); | |
6333 | ||
6334 | return testresult; | |
6335 | } | |
6336 | #endif | |
6337 | ||
5718fe45 MC |
6338 | static int info_cb_failed = 0; |
6339 | static int info_cb_offset = 0; | |
6340 | static int info_cb_this_state = -1; | |
6341 | ||
6342 | static struct info_cb_states_st { | |
6343 | int where; | |
6344 | const char *statestr; | |
6345 | } info_cb_states[][60] = { | |
6346 | { | |
6347 | /* TLSv1.2 server followed by resumption */ | |
6348 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6349 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, | |
6350 | {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"}, | |
6351 | {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"}, | |
6352 | {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"}, | |
6353 | {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, | |
6354 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, | |
6355 | {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, | |
6356 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, | |
6357 | {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, | |
6358 | {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"}, | |
6359 | {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, | |
6360 | {SSL_CB_EXIT, NULL}, {0, NULL}, | |
6361 | }, { | |
6362 | /* TLSv1.2 client followed by resumption */ | |
6363 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6364 | {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, | |
6365 | {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"}, | |
6366 | {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"}, | |
6367 | {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, | |
6368 | {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, | |
6369 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, | |
6370 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6371 | {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, | |
6372 | {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, | |
6373 | {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, | |
6374 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL}, | |
6375 | }, { | |
6376 | /* TLSv1.3 server followed by resumption */ | |
6377 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6378 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, | |
6379 | {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"}, | |
6380 | {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"}, | |
6381 | {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"}, | |
4af5836b MC |
6382 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, |
6383 | {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL}, | |
6384 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6385 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, | |
6386 | {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"}, | |
6387 | {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, | |
6388 | {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, | |
6389 | {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, | |
5718fe45 MC |
6390 | }, { |
6391 | /* TLSv1.3 client followed by resumption */ | |
6392 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6393 | {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"}, | |
6394 | {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"}, | |
6395 | {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, | |
6396 | {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, | |
4af5836b MC |
6397 | {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, |
6398 | {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, | |
6399 | {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, | |
5718fe45 MC |
6400 | {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL}, |
6401 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, | |
6402 | {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, | |
6403 | {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"}, | |
6404 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, | |
4af5836b MC |
6405 | {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"}, |
6406 | {SSL_CB_EXIT, NULL}, {0, NULL}, | |
5718fe45 MC |
6407 | }, { |
6408 | /* TLSv1.3 server, early_data */ | |
6409 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6410 | {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"}, | |
6411 | {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"}, | |
6412 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, | |
6413 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"}, | |
6414 | {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"}, | |
4af5836b | 6415 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"}, |
5718fe45 MC |
6416 | {SSL_CB_EXIT, NULL}, {0, NULL}, |
6417 | }, { | |
6418 | /* TLSv1.3 client, early_data */ | |
6419 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "}, | |
6420 | {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"}, | |
6421 | {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, | |
6422 | {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"}, | |
6423 | {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, | |
6424 | {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"}, | |
6425 | {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL}, | |
4af5836b MC |
6426 | {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, |
6427 | {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL}, | |
5718fe45 MC |
6428 | }, { |
6429 | {0, NULL}, | |
6430 | } | |
6431 | }; | |
6432 | ||
6433 | static void sslapi_info_callback(const SSL *s, int where, int ret) | |
6434 | { | |
6435 | struct info_cb_states_st *state = info_cb_states[info_cb_offset]; | |
6436 | ||
6437 | /* We do not ever expect a connection to fail in this test */ | |
6438 | if (!TEST_false(ret == 0)) { | |
6439 | info_cb_failed = 1; | |
6440 | return; | |
6441 | } | |
6442 | ||
6443 | /* | |
6444 | * Do some sanity checks. We never expect these things to happen in this | |
6445 | * test | |
6446 | */ | |
6447 | if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0)) | |
6448 | || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0) | |
6449 | || !TEST_int_ne(state[++info_cb_this_state].where, 0)) { | |
6450 | info_cb_failed = 1; | |
6451 | return; | |
6452 | } | |
6453 | ||
6454 | /* Now check we're in the right state */ | |
6455 | if (!TEST_true((where & state[info_cb_this_state].where) != 0)) { | |
6456 | info_cb_failed = 1; | |
6457 | return; | |
6458 | } | |
6459 | if ((where & SSL_CB_LOOP) != 0 | |
6460 | && !TEST_int_eq(strcmp(SSL_state_string(s), | |
6461 | state[info_cb_this_state].statestr), 0)) { | |
6462 | info_cb_failed = 1; | |
6463 | return; | |
6464 | } | |
033c181b | 6465 | |
4af5836b MC |
6466 | /* |
6467 | * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init | |
6468 | */ | |
6469 | if ((where & SSL_CB_HANDSHAKE_DONE) | |
6470 | && SSL_in_init((SSL *)s) != 0) { | |
033c181b MC |
6471 | info_cb_failed = 1; |
6472 | return; | |
6473 | } | |
5718fe45 MC |
6474 | } |
6475 | ||
6476 | /* | |
6477 | * Test the info callback gets called when we expect it to. | |
6478 | * | |
6479 | * Test 0: TLSv1.2, server | |
6480 | * Test 1: TLSv1.2, client | |
6481 | * Test 2: TLSv1.3, server | |
6482 | * Test 3: TLSv1.3, client | |
6483 | * Test 4: TLSv1.3, server, early_data | |
6484 | * Test 5: TLSv1.3, client, early_data | |
6485 | */ | |
6486 | static int test_info_callback(int tst) | |
6487 | { | |
6488 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6489 | SSL *clientssl = NULL, *serverssl = NULL; | |
6490 | SSL_SESSION *clntsess = NULL; | |
6491 | int testresult = 0; | |
6492 | int tlsvers; | |
6493 | ||
6494 | if (tst < 2) { | |
1aac20f5 MC |
6495 | /* We need either ECDHE or DHE for the TLSv1.2 test to work */ |
6496 | #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \ | |
6497 | || !defined(OPENSSL_NO_DH)) | |
5718fe45 MC |
6498 | tlsvers = TLS1_2_VERSION; |
6499 | #else | |
6500 | return 1; | |
6501 | #endif | |
6502 | } else { | |
6503 | #ifndef OPENSSL_NO_TLS1_3 | |
6504 | tlsvers = TLS1_3_VERSION; | |
6505 | #else | |
6506 | return 1; | |
6507 | #endif | |
6508 | } | |
6509 | ||
6510 | /* Reset globals */ | |
6511 | info_cb_failed = 0; | |
6512 | info_cb_this_state = -1; | |
6513 | info_cb_offset = tst; | |
6514 | ||
6e07834c | 6515 | #ifndef OPENSSL_NO_TLS1_3 |
5718fe45 MC |
6516 | if (tst >= 4) { |
6517 | SSL_SESSION *sess = NULL; | |
6518 | size_t written, readbytes; | |
6519 | unsigned char buf[80]; | |
6520 | ||
6521 | /* early_data tests */ | |
6522 | if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, | |
6523 | &serverssl, &sess, 0))) | |
6524 | goto end; | |
6525 | ||
6526 | /* We don't actually need this reference */ | |
6527 | SSL_SESSION_free(sess); | |
6528 | ||
6529 | SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl, | |
6530 | sslapi_info_callback); | |
6531 | ||
6532 | /* Write and read some early data and then complete the connection */ | |
6533 | if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1), | |
6534 | &written)) | |
6535 | || !TEST_size_t_eq(written, strlen(MSG1)) | |
6536 | || !TEST_int_eq(SSL_read_early_data(serverssl, buf, | |
6537 | sizeof(buf), &readbytes), | |
6538 | SSL_READ_EARLY_DATA_SUCCESS) | |
6539 | || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1)) | |
6540 | || !TEST_int_eq(SSL_get_early_data_status(serverssl), | |
6541 | SSL_EARLY_DATA_ACCEPTED) | |
6542 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
6543 | SSL_ERROR_NONE)) | |
6544 | || !TEST_false(info_cb_failed)) | |
6545 | goto end; | |
6546 | ||
6547 | testresult = 1; | |
6548 | goto end; | |
6549 | } | |
6e07834c | 6550 | #endif |
5718fe45 | 6551 | |
5e30f2fd | 6552 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
5718fe45 MC |
6553 | TLS_client_method(), |
6554 | tlsvers, tlsvers, &sctx, &cctx, cert, | |
6555 | privkey))) | |
6556 | goto end; | |
6557 | ||
6558 | /* | |
6559 | * For even numbered tests we check the server callbacks. For odd numbers we | |
6560 | * check the client. | |
6561 | */ | |
6562 | SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx, | |
6563 | sslapi_info_callback); | |
6564 | ||
6565 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, | |
6566 | &clientssl, NULL, NULL)) | |
6567 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
6568 | SSL_ERROR_NONE)) | |
6569 | || !TEST_false(info_cb_failed)) | |
6570 | goto end; | |
6571 | ||
6572 | ||
6573 | ||
6574 | clntsess = SSL_get1_session(clientssl); | |
6575 | SSL_shutdown(clientssl); | |
6576 | SSL_shutdown(serverssl); | |
6577 | SSL_free(serverssl); | |
6578 | SSL_free(clientssl); | |
6579 | serverssl = clientssl = NULL; | |
6580 | ||
6581 | /* Now do a resumption */ | |
6582 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, | |
6583 | NULL)) | |
6584 | || !TEST_true(SSL_set_session(clientssl, clntsess)) | |
6585 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
6586 | SSL_ERROR_NONE)) | |
6587 | || !TEST_true(SSL_session_reused(clientssl)) | |
6588 | || !TEST_false(info_cb_failed)) | |
6589 | goto end; | |
6590 | ||
6591 | testresult = 1; | |
6592 | ||
6593 | end: | |
6594 | SSL_free(serverssl); | |
6595 | SSL_free(clientssl); | |
6596 | SSL_SESSION_free(clntsess); | |
6597 | SSL_CTX_free(sctx); | |
6598 | SSL_CTX_free(cctx); | |
6599 | return testresult; | |
6600 | } | |
6601 | ||
4a432af8 MC |
6602 | static int test_ssl_pending(int tst) |
6603 | { | |
6604 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6605 | SSL *clientssl = NULL, *serverssl = NULL; | |
6606 | int testresult = 0; | |
6607 | char msg[] = "A test message"; | |
6608 | char buf[5]; | |
6609 | size_t written, readbytes; | |
6610 | ||
6611 | if (tst == 0) { | |
5e30f2fd | 6612 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
4a432af8 | 6613 | TLS_client_method(), |
5c587fb6 | 6614 | TLS1_VERSION, 0, |
4a432af8 MC |
6615 | &sctx, &cctx, cert, privkey))) |
6616 | goto end; | |
6617 | } else { | |
6618 | #ifndef OPENSSL_NO_DTLS | |
5e30f2fd | 6619 | if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(), |
4a432af8 | 6620 | DTLS_client_method(), |
5c587fb6 | 6621 | DTLS1_VERSION, 0, |
4a432af8 MC |
6622 | &sctx, &cctx, cert, privkey))) |
6623 | goto end; | |
6624 | #else | |
6625 | return 1; | |
6626 | #endif | |
6627 | } | |
6628 | ||
6629 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
6630 | NULL, NULL)) | |
6631 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
6632 | SSL_ERROR_NONE))) | |
6633 | goto end; | |
6634 | ||
e8251092 MC |
6635 | if (!TEST_int_eq(SSL_pending(clientssl), 0) |
6636 | || !TEST_false(SSL_has_pending(clientssl)) | |
6637 | || !TEST_int_eq(SSL_pending(serverssl), 0) | |
6638 | || !TEST_false(SSL_has_pending(serverssl)) | |
6639 | || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) | |
4a432af8 MC |
6640 | || !TEST_size_t_eq(written, sizeof(msg)) |
6641 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) | |
6642 | || !TEST_size_t_eq(readbytes, sizeof(buf)) | |
e8251092 MC |
6643 | || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes)) |
6644 | || !TEST_true(SSL_has_pending(clientssl))) | |
4a432af8 MC |
6645 | goto end; |
6646 | ||
6647 | testresult = 1; | |
6648 | ||
6649 | end: | |
6650 | SSL_free(serverssl); | |
6651 | SSL_free(clientssl); | |
6652 | SSL_CTX_free(sctx); | |
6653 | SSL_CTX_free(cctx); | |
6654 | ||
6655 | return testresult; | |
6656 | } | |
6657 | ||
e401389a MC |
6658 | static struct { |
6659 | unsigned int maxprot; | |
6660 | const char *clntciphers; | |
6661 | const char *clnttls13ciphers; | |
6662 | const char *srvrciphers; | |
6663 | const char *srvrtls13ciphers; | |
6664 | const char *shared; | |
a96e6c34 | 6665 | const char *fipsshared; |
e401389a | 6666 | } shared_ciphers_data[] = { |
60155b9a MC |
6667 | /* |
6668 | * We can't establish a connection (even in TLSv1.1) with these ciphersuites if | |
6669 | * TLSv1.3 is enabled but TLSv1.2 is disabled. | |
6670 | */ | |
6671 | #if defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) | |
e401389a MC |
6672 | { |
6673 | TLS1_2_VERSION, | |
6674 | "AES128-SHA:AES256-SHA", | |
6675 | NULL, | |
6676 | "AES256-SHA:DHE-RSA-AES128-SHA", | |
6677 | NULL, | |
a96e6c34 | 6678 | "AES256-SHA", |
e401389a MC |
6679 | "AES256-SHA" |
6680 | }, | |
a96e6c34 MC |
6681 | # if !defined(OPENSSL_NO_CHACHA) \ |
6682 | && !defined(OPENSSL_NO_POLY1305) \ | |
6683 | && !defined(OPENSSL_NO_EC) | |
6684 | { | |
6685 | TLS1_2_VERSION, | |
6686 | "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", | |
6687 | NULL, | |
6688 | "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", | |
6689 | NULL, | |
6690 | "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305", | |
6691 | "AES128-SHA" | |
6692 | }, | |
6693 | # endif | |
e401389a MC |
6694 | { |
6695 | TLS1_2_VERSION, | |
6696 | "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA", | |
6697 | NULL, | |
6698 | "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA", | |
6699 | NULL, | |
a96e6c34 | 6700 | "AES128-SHA:AES256-SHA", |
e401389a MC |
6701 | "AES128-SHA:AES256-SHA" |
6702 | }, | |
6703 | { | |
6704 | TLS1_2_VERSION, | |
6705 | "AES128-SHA:AES256-SHA", | |
6706 | NULL, | |
6707 | "AES128-SHA:DHE-RSA-AES128-SHA", | |
6708 | NULL, | |
a96e6c34 | 6709 | "AES128-SHA", |
e401389a MC |
6710 | "AES128-SHA" |
6711 | }, | |
60155b9a MC |
6712 | #endif |
6713 | /* | |
6714 | * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be | |
6715 | * enabled. | |
6716 | */ | |
6717 | #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \ | |
6718 | && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) | |
e401389a MC |
6719 | { |
6720 | TLS1_3_VERSION, | |
6721 | "AES128-SHA:AES256-SHA", | |
6722 | NULL, | |
6723 | "AES256-SHA:AES128-SHA256", | |
6724 | NULL, | |
6725 | "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:" | |
a96e6c34 MC |
6726 | "TLS_AES_128_GCM_SHA256:AES256-SHA", |
6727 | "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA" | |
e401389a | 6728 | }, |
60155b9a MC |
6729 | #endif |
6730 | #ifndef OPENSSL_NO_TLS1_3 | |
e401389a MC |
6731 | { |
6732 | TLS1_3_VERSION, | |
6733 | "AES128-SHA", | |
6734 | "TLS_AES_256_GCM_SHA384", | |
6735 | "AES256-SHA", | |
6736 | "TLS_AES_256_GCM_SHA384", | |
a96e6c34 | 6737 | "TLS_AES_256_GCM_SHA384", |
e401389a MC |
6738 | "TLS_AES_256_GCM_SHA384" |
6739 | }, | |
6740 | #endif | |
6741 | }; | |
6742 | ||
a96e6c34 | 6743 | static int int_test_ssl_get_shared_ciphers(int tst, int clnt) |
e401389a MC |
6744 | { |
6745 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6746 | SSL *clientssl = NULL, *serverssl = NULL; | |
6747 | int testresult = 0; | |
6748 | char buf[1024]; | |
a96e6c34 MC |
6749 | OPENSSL_CTX *tmplibctx = OPENSSL_CTX_new(); |
6750 | ||
6751 | if (!TEST_ptr(tmplibctx)) | |
6752 | goto end; | |
6753 | ||
6754 | /* | |
6755 | * Regardless of whether we're testing with the FIPS provider loaded into | |
6756 | * libctx, we want one peer to always use the full set of ciphersuites | |
6757 | * available. Therefore we use a separate libctx with the default provider | |
6758 | * loaded into it. We run the same tests twice - once with the client side | |
6759 | * having the full set of ciphersuites and once with the server side. | |
6760 | */ | |
6761 | if (clnt) { | |
6762 | cctx = SSL_CTX_new_with_libctx(tmplibctx, NULL, TLS_client_method()); | |
6763 | if (!TEST_ptr(cctx)) | |
6764 | goto end; | |
6765 | } else { | |
6766 | sctx = SSL_CTX_new_with_libctx(tmplibctx, NULL, TLS_server_method()); | |
6767 | if (!TEST_ptr(sctx)) | |
6768 | goto end; | |
6769 | } | |
e401389a | 6770 | |
5e30f2fd | 6771 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
e401389a MC |
6772 | TLS_client_method(), |
6773 | TLS1_VERSION, | |
6774 | shared_ciphers_data[tst].maxprot, | |
6775 | &sctx, &cctx, cert, privkey))) | |
6776 | goto end; | |
6777 | ||
6778 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, | |
6779 | shared_ciphers_data[tst].clntciphers)) | |
6780 | || (shared_ciphers_data[tst].clnttls13ciphers != NULL | |
6781 | && !TEST_true(SSL_CTX_set_ciphersuites(cctx, | |
6782 | shared_ciphers_data[tst].clnttls13ciphers))) | |
6783 | || !TEST_true(SSL_CTX_set_cipher_list(sctx, | |
6784 | shared_ciphers_data[tst].srvrciphers)) | |
6785 | || (shared_ciphers_data[tst].srvrtls13ciphers != NULL | |
6786 | && !TEST_true(SSL_CTX_set_ciphersuites(sctx, | |
6787 | shared_ciphers_data[tst].srvrtls13ciphers)))) | |
6788 | goto end; | |
6789 | ||
6790 | ||
6791 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
6792 | NULL, NULL)) | |
6793 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
6794 | SSL_ERROR_NONE))) | |
6795 | goto end; | |
6796 | ||
6797 | if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf))) | |
a96e6c34 MC |
6798 | || !TEST_int_eq(strcmp(buf, |
6799 | is_fips | |
6800 | ? shared_ciphers_data[tst].fipsshared | |
6801 | : shared_ciphers_data[tst].shared), | |
6802 | 0)) { | |
e401389a MC |
6803 | TEST_info("Shared ciphers are: %s\n", buf); |
6804 | goto end; | |
6805 | } | |
6806 | ||
6807 | testresult = 1; | |
6808 | ||
6809 | end: | |
6810 | SSL_free(serverssl); | |
6811 | SSL_free(clientssl); | |
6812 | SSL_CTX_free(sctx); | |
6813 | SSL_CTX_free(cctx); | |
a96e6c34 | 6814 | OPENSSL_CTX_free(tmplibctx); |
e401389a MC |
6815 | |
6816 | return testresult; | |
6817 | } | |
6818 | ||
a96e6c34 MC |
6819 | static int test_ssl_get_shared_ciphers(int tst) |
6820 | { | |
6821 | return int_test_ssl_get_shared_ciphers(tst, 0) | |
6822 | && int_test_ssl_get_shared_ciphers(tst, 1); | |
6823 | } | |
6824 | ||
6825 | ||
d0191fe0 | 6826 | static const char *appdata = "Hello World"; |
61fb5923 MC |
6827 | static int gen_tick_called, dec_tick_called, tick_key_cb_called; |
6828 | static int tick_key_renew = 0; | |
6829 | static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT; | |
d0191fe0 MC |
6830 | |
6831 | static int gen_tick_cb(SSL *s, void *arg) | |
6832 | { | |
6833 | gen_tick_called = 1; | |
6834 | ||
6835 | return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata, | |
6836 | strlen(appdata)); | |
6837 | } | |
6838 | ||
6839 | static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss, | |
6840 | const unsigned char *keyname, | |
6841 | size_t keyname_length, | |
61fb5923 MC |
6842 | SSL_TICKET_STATUS status, |
6843 | void *arg) | |
d0191fe0 MC |
6844 | { |
6845 | void *tickdata; | |
6846 | size_t tickdlen; | |
6847 | ||
6848 | dec_tick_called = 1; | |
6849 | ||
61fb5923 MC |
6850 | if (status == SSL_TICKET_EMPTY) |
6851 | return SSL_TICKET_RETURN_IGNORE_RENEW; | |
d0191fe0 | 6852 | |
61fb5923 MC |
6853 | if (!TEST_true(status == SSL_TICKET_SUCCESS |
6854 | || status == SSL_TICKET_SUCCESS_RENEW)) | |
6855 | return SSL_TICKET_RETURN_ABORT; | |
6856 | ||
6857 | if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata, | |
6858 | &tickdlen)) | |
d0191fe0 MC |
6859 | || !TEST_size_t_eq(tickdlen, strlen(appdata)) |
6860 | || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0)) | |
61fb5923 | 6861 | return SSL_TICKET_RETURN_ABORT; |
d0191fe0 | 6862 | |
61fb5923 MC |
6863 | if (tick_key_cb_called) { |
6864 | /* Don't change what the ticket key callback wanted to do */ | |
6865 | switch (status) { | |
6866 | case SSL_TICKET_NO_DECRYPT: | |
6867 | return SSL_TICKET_RETURN_IGNORE_RENEW; | |
6868 | ||
6869 | case SSL_TICKET_SUCCESS: | |
6870 | return SSL_TICKET_RETURN_USE; | |
d0191fe0 | 6871 | |
61fb5923 MC |
6872 | case SSL_TICKET_SUCCESS_RENEW: |
6873 | return SSL_TICKET_RETURN_USE_RENEW; | |
6874 | ||
6875 | default: | |
6876 | return SSL_TICKET_RETURN_ABORT; | |
6877 | } | |
6878 | } | |
6879 | return tick_dec_ret; | |
6880 | ||
6881 | } | |
d0191fe0 | 6882 | |
a76ce286 | 6883 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
d0191fe0 MC |
6884 | static int tick_key_cb(SSL *s, unsigned char key_name[16], |
6885 | unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx, | |
6886 | HMAC_CTX *hctx, int enc) | |
6887 | { | |
6888 | const unsigned char tick_aes_key[16] = "0123456789abcdef"; | |
6889 | const unsigned char tick_hmac_key[16] = "0123456789abcdef"; | |
5e30f2fd MC |
6890 | EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL); |
6891 | EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL); | |
6892 | int ret; | |
d0191fe0 | 6893 | |
61fb5923 | 6894 | tick_key_cb_called = 1; |
d0191fe0 MC |
6895 | memset(iv, 0, AES_BLOCK_SIZE); |
6896 | memset(key_name, 0, 16); | |
5e30f2fd MC |
6897 | if (aes128cbc == NULL |
6898 | || sha256 == NULL | |
6899 | || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc) | |
6900 | || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256, | |
6901 | NULL)) | |
6902 | ret = -1; | |
6903 | else | |
6904 | ret = tick_key_renew ? 2 : 1; | |
d0191fe0 | 6905 | |
5e30f2fd MC |
6906 | EVP_CIPHER_free(aes128cbc); |
6907 | EVP_MD_free(sha256); | |
6908 | ||
6909 | return ret; | |
d0191fe0 | 6910 | } |
a76ce286 P |
6911 | #endif |
6912 | ||
6913 | static int tick_key_evp_cb(SSL *s, unsigned char key_name[16], | |
6914 | unsigned char iv[EVP_MAX_IV_LENGTH], | |
6915 | EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc) | |
6916 | { | |
6917 | const unsigned char tick_aes_key[16] = "0123456789abcdef"; | |
6918 | unsigned char tick_hmac_key[16] = "0123456789abcdef"; | |
6919 | OSSL_PARAM params[3]; | |
5e30f2fd MC |
6920 | EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL); |
6921 | int ret; | |
a76ce286 P |
6922 | |
6923 | tick_key_cb_called = 1; | |
6924 | memset(iv, 0, AES_BLOCK_SIZE); | |
6925 | memset(key_name, 0, 16); | |
6926 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, | |
6927 | "SHA256", 0); | |
6928 | params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, | |
6929 | tick_hmac_key, | |
6930 | sizeof(tick_hmac_key)); | |
6931 | params[2] = OSSL_PARAM_construct_end(); | |
5e30f2fd MC |
6932 | if (aes128cbc == NULL |
6933 | || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc) | |
d9c2fd51 | 6934 | || !EVP_MAC_set_ctx_params(hctx, params) |
a76ce286 | 6935 | || !EVP_MAC_init(hctx)) |
5e30f2fd MC |
6936 | ret = -1; |
6937 | else | |
6938 | ret = tick_key_renew ? 2 : 1; | |
6939 | ||
6940 | EVP_CIPHER_free(aes128cbc); | |
a76ce286 | 6941 | |
5e30f2fd | 6942 | return ret; |
a76ce286 | 6943 | } |
d0191fe0 MC |
6944 | |
6945 | /* | |
6946 | * Test the various ticket callbacks | |
61fb5923 MC |
6947 | * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal |
6948 | * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal | |
6949 | * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal | |
6950 | * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal | |
6951 | * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal | |
6952 | * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal | |
6953 | * Test 6: TLSv1.2, no ticket key callback, ticket, renewal | |
6954 | * Test 7: TLSv1.3, no ticket key callback, ticket, renewal | |
a76ce286 P |
6955 | * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal |
6956 | * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal | |
6957 | * Test 10: TLSv1.2, old ticket key callback, ticket, renewal | |
6958 | * Test 11: TLSv1.3, old ticket key callback, ticket, renewal | |
6959 | * Test 12: TLSv1.2, ticket key callback, ticket, no renewal | |
6960 | * Test 13: TLSv1.3, ticket key callback, ticket, no renewal | |
6961 | * Test 14: TLSv1.2, ticket key callback, ticket, renewal | |
6962 | * Test 15: TLSv1.3, ticket key callback, ticket, renewal | |
d0191fe0 MC |
6963 | */ |
6964 | static int test_ticket_callbacks(int tst) | |
6965 | { | |
6966 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
6967 | SSL *clientssl = NULL, *serverssl = NULL; | |
6968 | SSL_SESSION *clntsess = NULL; | |
6969 | int testresult = 0; | |
6970 | ||
6971 | #ifdef OPENSSL_NO_TLS1_2 | |
ba8b48e9 | 6972 | if (tst % 2 == 0) |
d0191fe0 MC |
6973 | return 1; |
6974 | #endif | |
6975 | #ifdef OPENSSL_NO_TLS1_3 | |
ba8b48e9 | 6976 | if (tst % 2 == 1) |
d0191fe0 MC |
6977 | return 1; |
6978 | #endif | |
a76ce286 P |
6979 | #ifdef OPENSSL_NO_DEPRECATED_3_0 |
6980 | if (tst >= 8 && tst <= 11) | |
6981 | return 1; | |
6982 | #endif | |
d0191fe0 | 6983 | |
61fb5923 | 6984 | gen_tick_called = dec_tick_called = tick_key_cb_called = 0; |
d0191fe0 MC |
6985 | |
6986 | /* Which tests the ticket key callback should request renewal for */ | |
a76ce286 | 6987 | if (tst == 10 || tst == 11 || tst == 14 || tst == 15) |
61fb5923 | 6988 | tick_key_renew = 1; |
d0191fe0 | 6989 | else |
61fb5923 MC |
6990 | tick_key_renew = 0; |
6991 | ||
6992 | /* Which tests the decrypt ticket callback should request renewal for */ | |
6993 | switch (tst) { | |
6994 | case 0: | |
6995 | case 1: | |
6996 | tick_dec_ret = SSL_TICKET_RETURN_IGNORE; | |
6997 | break; | |
6998 | ||
6999 | case 2: | |
7000 | case 3: | |
7001 | tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW; | |
7002 | break; | |
7003 | ||
7004 | case 4: | |
7005 | case 5: | |
7006 | tick_dec_ret = SSL_TICKET_RETURN_USE; | |
7007 | break; | |
7008 | ||
7009 | case 6: | |
7010 | case 7: | |
7011 | tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW; | |
7012 | break; | |
7013 | ||
7014 | default: | |
7015 | tick_dec_ret = SSL_TICKET_RETURN_ABORT; | |
7016 | } | |
d0191fe0 | 7017 | |
5e30f2fd | 7018 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
d0191fe0 MC |
7019 | TLS_client_method(), |
7020 | TLS1_VERSION, | |
61fb5923 MC |
7021 | ((tst % 2) == 0) ? TLS1_2_VERSION |
7022 | : TLS1_3_VERSION, | |
d0191fe0 MC |
7023 | &sctx, &cctx, cert, privkey))) |
7024 | goto end; | |
7025 | ||
61fb5923 MC |
7026 | /* |
7027 | * We only want sessions to resume from tickets - not the session cache. So | |
7028 | * switch the cache off. | |
7029 | */ | |
7030 | if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF))) | |
7031 | goto end; | |
7032 | ||
d0191fe0 MC |
7033 | if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb, |
7034 | NULL))) | |
7035 | goto end; | |
7036 | ||
a76ce286 P |
7037 | if (tst >= 12) { |
7038 | if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb))) | |
7039 | goto end; | |
7040 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
7041 | } else if (tst >= 8) { | |
7042 | if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb))) | |
7043 | goto end; | |
7044 | #endif | |
7045 | } | |
d0191fe0 MC |
7046 | |
7047 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7048 | NULL, NULL)) | |
7049 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
7050 | SSL_ERROR_NONE))) | |
7051 | goto end; | |
7052 | ||
61fb5923 MC |
7053 | /* |
7054 | * The decrypt ticket key callback in TLSv1.2 should be called even though | |
7055 | * we have no ticket yet, because it gets called with a status of | |
7056 | * SSL_TICKET_EMPTY (the client indicates support for tickets but does not | |
7057 | * actually send any ticket data). This does not happen in TLSv1.3 because | |
7058 | * it is not valid to send empty ticket data in TLSv1.3. | |
7059 | */ | |
d0191fe0 | 7060 | if (!TEST_int_eq(gen_tick_called, 1) |
61fb5923 | 7061 | || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0)) |
d0191fe0 MC |
7062 | goto end; |
7063 | ||
7064 | gen_tick_called = dec_tick_called = 0; | |
7065 | ||
7066 | clntsess = SSL_get1_session(clientssl); | |
7067 | SSL_shutdown(clientssl); | |
7068 | SSL_shutdown(serverssl); | |
7069 | SSL_free(serverssl); | |
7070 | SSL_free(clientssl); | |
7071 | serverssl = clientssl = NULL; | |
7072 | ||
7073 | /* Now do a resumption */ | |
7074 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, | |
7075 | NULL)) | |
7076 | || !TEST_true(SSL_set_session(clientssl, clntsess)) | |
7077 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
61fb5923 | 7078 | SSL_ERROR_NONE))) |
d0191fe0 MC |
7079 | goto end; |
7080 | ||
61fb5923 MC |
7081 | if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE |
7082 | || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) { | |
7083 | if (!TEST_false(SSL_session_reused(clientssl))) | |
7084 | goto end; | |
7085 | } else { | |
7086 | if (!TEST_true(SSL_session_reused(clientssl))) | |
7087 | goto end; | |
7088 | } | |
7089 | ||
d0191fe0 | 7090 | if (!TEST_int_eq(gen_tick_called, |
61fb5923 MC |
7091 | (tick_key_renew |
7092 | || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW | |
7093 | || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW) | |
7094 | ? 1 : 0) | |
d0191fe0 MC |
7095 | || !TEST_int_eq(dec_tick_called, 1)) |
7096 | goto end; | |
7097 | ||
7098 | testresult = 1; | |
7099 | ||
7100 | end: | |
7101 | SSL_SESSION_free(clntsess); | |
7102 | SSL_free(serverssl); | |
7103 | SSL_free(clientssl); | |
7104 | SSL_CTX_free(sctx); | |
7105 | SSL_CTX_free(cctx); | |
7106 | ||
7107 | return testresult; | |
7108 | } | |
7109 | ||
e638112e DB |
7110 | /* |
7111 | * Test incorrect shutdown. | |
7112 | * Test 0: client does not shutdown properly, | |
7113 | * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF, | |
7114 | * server should get SSL_ERROR_SSL | |
7115 | * Test 1: client does not shutdown properly, | |
7116 | * server sets SSL_OP_IGNORE_UNEXPECTED_EOF, | |
7117 | * server should get SSL_ERROR_ZERO_RETURN | |
7118 | */ | |
7119 | static int test_incorrect_shutdown(int tst) | |
7120 | { | |
7121 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7122 | SSL *clientssl = NULL, *serverssl = NULL; | |
7123 | int testresult = 0; | |
7124 | char buf[80]; | |
7125 | BIO *c2s; | |
7126 | ||
7127 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), | |
7128 | TLS_client_method(), 0, 0, | |
7129 | &sctx, &cctx, cert, privkey))) | |
7130 | goto end; | |
7131 | ||
7132 | if (tst == 1) | |
7133 | SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF); | |
7134 | ||
7135 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7136 | NULL, NULL))) | |
7137 | goto end; | |
7138 | ||
7139 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
7140 | SSL_ERROR_NONE))) | |
7141 | goto end; | |
7142 | ||
7143 | c2s = SSL_get_rbio(serverssl); | |
7144 | BIO_set_mem_eof_return(c2s, 0); | |
7145 | ||
7146 | if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf)))) | |
7147 | goto end; | |
7148 | ||
7149 | if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) ) | |
7150 | goto end; | |
7151 | if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) ) | |
7152 | goto end; | |
7153 | ||
7154 | testresult = 1; | |
7155 | ||
7156 | end: | |
7157 | SSL_free(serverssl); | |
7158 | SSL_free(clientssl); | |
7159 | SSL_CTX_free(sctx); | |
7160 | SSL_CTX_free(cctx); | |
7161 | ||
7162 | return testresult; | |
7163 | } | |
7164 | ||
c748834f MC |
7165 | /* |
7166 | * Test bi-directional shutdown. | |
7167 | * Test 0: TLSv1.2 | |
7168 | * Test 1: TLSv1.2, server continues to read/write after client shutdown | |
7169 | * Test 2: TLSv1.3, no pending NewSessionTicket messages | |
7170 | * Test 3: TLSv1.3, pending NewSessionTicket messages | |
80eff008 KR |
7171 | * Test 4: TLSv1.3, server continues to read/write after client shutdown, server |
7172 | * sends key update, client reads it | |
57d7b988 MC |
7173 | * Test 5: TLSv1.3, server continues to read/write after client shutdown, server |
7174 | * sends CertificateRequest, client reads and ignores it | |
7175 | * Test 6: TLSv1.3, server continues to read/write after client shutdown, client | |
c748834f MC |
7176 | * doesn't read it |
7177 | */ | |
7178 | static int test_shutdown(int tst) | |
7179 | { | |
7180 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7181 | SSL *clientssl = NULL, *serverssl = NULL; | |
7182 | int testresult = 0; | |
7183 | char msg[] = "A test message"; | |
7184 | char buf[80]; | |
7185 | size_t written, readbytes; | |
80eff008 | 7186 | SSL_SESSION *sess; |
c748834f MC |
7187 | |
7188 | #ifdef OPENSSL_NO_TLS1_2 | |
a97d19f7 | 7189 | if (tst <= 1) |
c748834f MC |
7190 | return 1; |
7191 | #endif | |
7192 | #ifdef OPENSSL_NO_TLS1_3 | |
a97d19f7 | 7193 | if (tst >= 2) |
c748834f MC |
7194 | return 1; |
7195 | #endif | |
7196 | ||
5e30f2fd | 7197 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
c748834f MC |
7198 | TLS_client_method(), |
7199 | TLS1_VERSION, | |
7200 | (tst <= 1) ? TLS1_2_VERSION | |
7201 | : TLS1_3_VERSION, | |
57d7b988 MC |
7202 | &sctx, &cctx, cert, privkey))) |
7203 | goto end; | |
7204 | ||
7205 | if (tst == 5) | |
7206 | SSL_CTX_set_post_handshake_auth(cctx, 1); | |
7207 | ||
7208 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
c748834f MC |
7209 | NULL, NULL))) |
7210 | goto end; | |
7211 | ||
7212 | if (tst == 3) { | |
7213 | if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl, | |
80c455d5 | 7214 | SSL_ERROR_NONE, 1)) |
80eff008 KR |
7215 | || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) |
7216 | || !TEST_false(SSL_SESSION_is_resumable(sess))) | |
c748834f MC |
7217 | goto end; |
7218 | } else if (!TEST_true(create_ssl_connection(serverssl, clientssl, | |
80eff008 KR |
7219 | SSL_ERROR_NONE)) |
7220 | || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) | |
7221 | || !TEST_true(SSL_SESSION_is_resumable(sess))) { | |
c748834f MC |
7222 | goto end; |
7223 | } | |
7224 | ||
7225 | if (!TEST_int_eq(SSL_shutdown(clientssl), 0)) | |
7226 | goto end; | |
7227 | ||
7228 | if (tst >= 4) { | |
7229 | /* | |
7230 | * Reading on the server after the client has sent close_notify should | |
7231 | * fail and provide SSL_ERROR_ZERO_RETURN | |
7232 | */ | |
7233 | if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)) | |
7234 | || !TEST_int_eq(SSL_get_error(serverssl, 0), | |
7235 | SSL_ERROR_ZERO_RETURN) | |
7236 | || !TEST_int_eq(SSL_get_shutdown(serverssl), | |
7237 | SSL_RECEIVED_SHUTDOWN) | |
7238 | /* | |
7239 | * Even though we're shutdown on receive we should still be | |
7240 | * able to write. | |
7241 | */ | |
80eff008 KR |
7242 | || !TEST_true(SSL_write(serverssl, msg, sizeof(msg)))) |
7243 | goto end; | |
57d7b988 MC |
7244 | if (tst == 4 |
7245 | && !TEST_true(SSL_key_update(serverssl, | |
7246 | SSL_KEY_UPDATE_REQUESTED))) | |
7247 | goto end; | |
7248 | if (tst == 5) { | |
7249 | SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL); | |
7250 | if (!TEST_true(SSL_verify_client_post_handshake(serverssl))) | |
7251 | goto end; | |
7252 | } | |
7253 | if ((tst == 4 || tst == 5) | |
7254 | && !TEST_true(SSL_write(serverssl, msg, sizeof(msg)))) | |
80eff008 KR |
7255 | goto end; |
7256 | if (!TEST_int_eq(SSL_shutdown(serverssl), 1)) | |
c748834f | 7257 | goto end; |
57d7b988 | 7258 | if (tst == 4 || tst == 5) { |
80eff008 | 7259 | /* Should still be able to read data from server */ |
c748834f | 7260 | if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), |
80eff008 KR |
7261 | &readbytes)) |
7262 | || !TEST_size_t_eq(readbytes, sizeof(msg)) | |
7263 | || !TEST_int_eq(memcmp(msg, buf, readbytes), 0) | |
7264 | || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), | |
7265 | &readbytes)) | |
c748834f MC |
7266 | || !TEST_size_t_eq(readbytes, sizeof(msg)) |
7267 | || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)) | |
7268 | goto end; | |
7269 | } | |
7270 | } | |
7271 | ||
7272 | /* Writing on the client after sending close_notify shouldn't be possible */ | |
ba709049 | 7273 | if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written))) |
c748834f MC |
7274 | goto end; |
7275 | ||
7276 | if (tst < 4) { | |
7277 | /* | |
7278 | * For these tests the client has sent close_notify but it has not yet | |
7279 | * been received by the server. The server has not sent close_notify | |
7280 | * yet. | |
7281 | */ | |
7282 | if (!TEST_int_eq(SSL_shutdown(serverssl), 0) | |
ba709049 MC |
7283 | /* |
7284 | * Writing on the server after sending close_notify shouldn't | |
7285 | * be possible. | |
7286 | */ | |
7287 | || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) | |
c748834f | 7288 | || !TEST_int_eq(SSL_shutdown(clientssl), 1) |
80eff008 KR |
7289 | || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) |
7290 | || !TEST_true(SSL_SESSION_is_resumable(sess)) | |
c748834f MC |
7291 | || !TEST_int_eq(SSL_shutdown(serverssl), 1)) |
7292 | goto end; | |
57d7b988 | 7293 | } else if (tst == 4 || tst == 5) { |
c748834f MC |
7294 | /* |
7295 | * In this test the client has sent close_notify and it has been | |
7296 | * received by the server which has responded with a close_notify. The | |
358ffa05 | 7297 | * client needs to read the close_notify sent by the server. |
c748834f | 7298 | */ |
80eff008 KR |
7299 | if (!TEST_int_eq(SSL_shutdown(clientssl), 1) |
7300 | || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL) | |
7301 | || !TEST_true(SSL_SESSION_is_resumable(sess))) | |
c748834f | 7302 | goto end; |
358ffa05 MC |
7303 | } else { |
7304 | /* | |
57d7b988 | 7305 | * tst == 6 |
358ffa05 MC |
7306 | * |
7307 | * The client has sent close_notify and is expecting a close_notify | |
7308 | * back, but instead there is application data first. The shutdown | |
7309 | * should fail with a fatal error. | |
7310 | */ | |
7311 | if (!TEST_int_eq(SSL_shutdown(clientssl), -1) | |
7312 | || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL)) | |
7313 | goto end; | |
c748834f MC |
7314 | } |
7315 | ||
7316 | testresult = 1; | |
7317 | ||
7318 | end: | |
7319 | SSL_free(serverssl); | |
7320 | SSL_free(clientssl); | |
7321 | SSL_CTX_free(sctx); | |
7322 | SSL_CTX_free(cctx); | |
7323 | ||
7324 | return testresult; | |
7325 | } | |
7326 | ||
7f1d923a | 7327 | #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3) |
cd6fe29f MC |
7328 | static int cert_cb_cnt; |
7329 | ||
7330 | static int cert_cb(SSL *s, void *arg) | |
7331 | { | |
7332 | SSL_CTX *ctx = (SSL_CTX *)arg; | |
7cb8fb07 BK |
7333 | BIO *in = NULL; |
7334 | EVP_PKEY *pkey = NULL; | |
1a2a3a42 MC |
7335 | X509 *x509 = NULL, *rootx = NULL; |
7336 | STACK_OF(X509) *chain = NULL; | |
7337 | char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL; | |
7338 | int ret = 0; | |
cd6fe29f MC |
7339 | |
7340 | if (cert_cb_cnt == 0) { | |
7341 | /* Suspend the handshake */ | |
7342 | cert_cb_cnt++; | |
7343 | return -1; | |
7344 | } else if (cert_cb_cnt == 1) { | |
7345 | /* | |
7346 | * Update the SSL_CTX, set the certificate and private key and then | |
7347 | * continue the handshake normally. | |
7348 | */ | |
7349 | if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx))) | |
7350 | return 0; | |
7351 | ||
7352 | if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM)) | |
7353 | || !TEST_true(SSL_use_PrivateKey_file(s, privkey, | |
7354 | SSL_FILETYPE_PEM)) | |
7355 | || !TEST_true(SSL_check_private_key(s))) | |
7356 | return 0; | |
7357 | cert_cb_cnt++; | |
7358 | return 1; | |
7cb8fb07 BK |
7359 | } else if (cert_cb_cnt == 3) { |
7360 | int rv; | |
1a2a3a42 MC |
7361 | |
7362 | rootfile = test_mk_file_path(certsdir, "rootcert.pem"); | |
7363 | ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem"); | |
7364 | ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem"); | |
7365 | if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey)) | |
7366 | goto out; | |
7367 | chain = sk_X509_new_null(); | |
7368 | if (!TEST_ptr(chain)) | |
7369 | goto out; | |
7cb8fb07 | 7370 | if (!TEST_ptr(in = BIO_new(BIO_s_file())) |
1a2a3a42 MC |
7371 | || !TEST_int_ge(BIO_read_filename(in, rootfile), 0) |
7372 | || !TEST_ptr(rootx = PEM_read_bio_X509(in, NULL, NULL, NULL)) | |
7373 | || !TEST_true(sk_X509_push(chain, rootx))) | |
7374 | goto out; | |
7375 | rootx = NULL; | |
7376 | BIO_free(in); | |
7377 | if (!TEST_ptr(in = BIO_new(BIO_s_file())) | |
7378 | || !TEST_int_ge(BIO_read_filename(in, ecdsacert), 0) | |
7cb8fb07 BK |
7379 | || !TEST_ptr(x509 = PEM_read_bio_X509(in, NULL, NULL, NULL))) |
7380 | goto out; | |
7381 | BIO_free(in); | |
7382 | if (!TEST_ptr(in = BIO_new(BIO_s_file())) | |
1a2a3a42 | 7383 | || !TEST_int_ge(BIO_read_filename(in, ecdsakey), 0) |
7cb8fb07 BK |
7384 | || !TEST_ptr(pkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL))) |
7385 | goto out; | |
1a2a3a42 | 7386 | rv = SSL_check_chain(s, x509, pkey, chain); |
7cb8fb07 BK |
7387 | /* |
7388 | * If the cert doesn't show as valid here (e.g., because we don't | |
7389 | * have any shared sigalgs), then we will not set it, and there will | |
7390 | * be no certificate at all on the SSL or SSL_CTX. This, in turn, | |
7391 | * will cause tls_choose_sigalgs() to fail the connection. | |
7392 | */ | |
1a2a3a42 MC |
7393 | if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) |
7394 | == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) { | |
7cb8fb07 BK |
7395 | if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1)) |
7396 | goto out; | |
7397 | } | |
1a2a3a42 MC |
7398 | |
7399 | ret = 1; | |
cd6fe29f MC |
7400 | } |
7401 | ||
7402 | /* Abort the handshake */ | |
7cb8fb07 | 7403 | out: |
1a2a3a42 MC |
7404 | OPENSSL_free(ecdsacert); |
7405 | OPENSSL_free(ecdsakey); | |
7406 | OPENSSL_free(rootfile); | |
7cb8fb07 BK |
7407 | BIO_free(in); |
7408 | EVP_PKEY_free(pkey); | |
7409 | X509_free(x509); | |
1a2a3a42 MC |
7410 | X509_free(rootx); |
7411 | sk_X509_pop_free(chain, X509_free); | |
7412 | return ret; | |
cd6fe29f MC |
7413 | } |
7414 | ||
7415 | /* | |
7416 | * Test the certificate callback. | |
7417 | * Test 0: Callback fails | |
7418 | * Test 1: Success - no SSL_set_SSL_CTX() in the callback | |
7419 | * Test 2: Success - SSL_set_SSL_CTX() in the callback | |
1a2a3a42 MC |
7420 | * Test 3: Success - Call SSL_check_chain from the callback |
7421 | * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the | |
7422 | * chain | |
7423 | * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert | |
cd6fe29f MC |
7424 | */ |
7425 | static int test_cert_cb_int(int prot, int tst) | |
7426 | { | |
7427 | SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL; | |
7428 | SSL *clientssl = NULL, *serverssl = NULL; | |
7429 | int testresult = 0, ret; | |
7430 | ||
1a2a3a42 MC |
7431 | #ifdef OPENSSL_NO_EC |
7432 | /* We use an EC cert in these tests, so we skip in a no-ec build */ | |
7433 | if (tst >= 3) | |
7434 | return 1; | |
7435 | #endif | |
7436 | ||
5e30f2fd | 7437 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
cd6fe29f MC |
7438 | TLS_client_method(), |
7439 | TLS1_VERSION, | |
7440 | prot, | |
7441 | &sctx, &cctx, NULL, NULL))) | |
7442 | goto end; | |
7443 | ||
7444 | if (tst == 0) | |
7445 | cert_cb_cnt = -1; | |
1a2a3a42 | 7446 | else if (tst >= 3) |
7cb8fb07 | 7447 | cert_cb_cnt = 3; |
cd6fe29f MC |
7448 | else |
7449 | cert_cb_cnt = 0; | |
1a2a3a42 | 7450 | |
cd6fe29f MC |
7451 | if (tst == 2) |
7452 | snictx = SSL_CTX_new(TLS_server_method()); | |
7453 | SSL_CTX_set_cert_cb(sctx, cert_cb, snictx); | |
7454 | ||
7455 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7456 | NULL, NULL))) | |
7457 | goto end; | |
7458 | ||
1a2a3a42 MC |
7459 | if (tst == 4) { |
7460 | /* | |
7461 | * We cause SSL_check_chain() to fail by specifying sig_algs that | |
7462 | * the chain doesn't meet (the root uses an RSA cert) | |
7463 | */ | |
7464 | if (!TEST_true(SSL_set1_sigalgs_list(clientssl, | |
7465 | "ecdsa_secp256r1_sha256"))) | |
7466 | goto end; | |
7467 | } else if (tst == 5) { | |
7468 | /* | |
7469 | * We cause SSL_check_chain() to fail by specifying sig_algs that | |
7470 | * the ee cert doesn't meet (the ee uses an ECDSA cert) | |
7471 | */ | |
7472 | if (!TEST_true(SSL_set1_sigalgs_list(clientssl, | |
7473 | "rsa_pss_rsae_sha256:rsa_pkcs1_sha256"))) | |
7474 | goto end; | |
7475 | } | |
7476 | ||
cd6fe29f | 7477 | ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE); |
1a2a3a42 | 7478 | if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret) |
7cb8fb07 BK |
7479 | || (tst > 0 |
7480 | && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) { | |
cd6fe29f MC |
7481 | goto end; |
7482 | } | |
7483 | ||
7484 | testresult = 1; | |
7485 | ||
7486 | end: | |
7487 | SSL_free(serverssl); | |
7488 | SSL_free(clientssl); | |
7489 | SSL_CTX_free(sctx); | |
7490 | SSL_CTX_free(cctx); | |
7491 | SSL_CTX_free(snictx); | |
7492 | ||
7493 | return testresult; | |
7494 | } | |
7f1d923a | 7495 | #endif |
cd6fe29f MC |
7496 | |
7497 | static int test_cert_cb(int tst) | |
7498 | { | |
7499 | int testresult = 1; | |
7500 | ||
7501 | #ifndef OPENSSL_NO_TLS1_2 | |
7502 | testresult &= test_cert_cb_int(TLS1_2_VERSION, tst); | |
7503 | #endif | |
7f1d923a | 7504 | #ifndef OPENSSL_NO_TLS1_3 |
cd6fe29f MC |
7505 | testresult &= test_cert_cb_int(TLS1_3_VERSION, tst); |
7506 | #endif | |
7507 | ||
7508 | return testresult; | |
7509 | } | |
7510 | ||
6e46c065 MC |
7511 | static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey) |
7512 | { | |
7513 | X509 *xcert, *peer; | |
7514 | EVP_PKEY *privpkey; | |
7515 | BIO *in = NULL; | |
7516 | ||
7517 | /* Check that SSL_get_peer_certificate() returns something sensible */ | |
7518 | peer = SSL_get_peer_certificate(ssl); | |
7519 | if (!TEST_ptr(peer)) | |
7520 | return 0; | |
7521 | X509_free(peer); | |
7522 | ||
7523 | in = BIO_new_file(cert, "r"); | |
7524 | if (!TEST_ptr(in)) | |
7525 | return 0; | |
7526 | ||
7527 | xcert = PEM_read_bio_X509(in, NULL, NULL, NULL); | |
7528 | BIO_free(in); | |
7529 | if (!TEST_ptr(xcert)) | |
7530 | return 0; | |
7531 | ||
7532 | in = BIO_new_file(privkey, "r"); | |
7533 | if (!TEST_ptr(in)) { | |
7534 | X509_free(xcert); | |
7535 | return 0; | |
7536 | } | |
7537 | ||
7538 | privpkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); | |
7539 | BIO_free(in); | |
7540 | if (!TEST_ptr(privpkey)) { | |
7541 | X509_free(xcert); | |
7542 | return 0; | |
7543 | } | |
7544 | ||
7545 | *x509 = xcert; | |
7546 | *pkey = privpkey; | |
7547 | ||
7548 | return 1; | |
7549 | } | |
7550 | ||
7551 | static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) | |
7552 | { | |
7553 | return 1; | |
7554 | } | |
7555 | ||
7556 | static int test_client_cert_cb(int tst) | |
7557 | { | |
7558 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7559 | SSL *clientssl = NULL, *serverssl = NULL; | |
7560 | int testresult = 0; | |
7561 | ||
7562 | #ifdef OPENSSL_NO_TLS1_2 | |
7563 | if (tst == 0) | |
7564 | return 1; | |
7565 | #endif | |
7566 | #ifdef OPENSSL_NO_TLS1_3 | |
7567 | if (tst == 1) | |
7568 | return 1; | |
7569 | #endif | |
7570 | ||
5e30f2fd | 7571 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
6e46c065 MC |
7572 | TLS_client_method(), |
7573 | TLS1_VERSION, | |
7574 | tst == 0 ? TLS1_2_VERSION | |
7575 | : TLS1_3_VERSION, | |
7576 | &sctx, &cctx, cert, privkey))) | |
7577 | goto end; | |
7578 | ||
7579 | /* | |
7580 | * Test that setting a client_cert_cb results in a client certificate being | |
7581 | * sent. | |
7582 | */ | |
7583 | SSL_CTX_set_client_cert_cb(cctx, client_cert_cb); | |
7584 | SSL_CTX_set_verify(sctx, | |
7585 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, | |
7586 | verify_cb); | |
fb8c8359 MC |
7587 | |
7588 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7589 | NULL, NULL)) | |
7590 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
7591 | SSL_ERROR_NONE))) | |
7592 | goto end; | |
7593 | ||
7594 | testresult = 1; | |
7595 | ||
7596 | end: | |
7597 | SSL_free(serverssl); | |
7598 | SSL_free(clientssl); | |
7599 | SSL_CTX_free(sctx); | |
7600 | SSL_CTX_free(cctx); | |
7601 | ||
7602 | return testresult; | |
7603 | } | |
7604 | ||
7605 | #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3) | |
7606 | /* | |
7607 | * Test setting certificate authorities on both client and server. | |
7608 | * | |
7609 | * Test 0: SSL_CTX_set0_CA_list() only | |
7610 | * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list() | |
7611 | * Test 2: Only SSL_CTX_set_client_CA_list() | |
7612 | */ | |
7613 | static int test_ca_names_int(int prot, int tst) | |
7614 | { | |
7615 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7616 | SSL *clientssl = NULL, *serverssl = NULL; | |
7617 | int testresult = 0; | |
7618 | size_t i; | |
7619 | X509_NAME *name[] = { NULL, NULL, NULL, NULL }; | |
7620 | char *strnames[] = { "Jack", "Jill", "John", "Joanne" }; | |
7621 | STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL; | |
7622 | const STACK_OF(X509_NAME) *sktmp = NULL; | |
7623 | ||
7624 | for (i = 0; i < OSSL_NELEM(name); i++) { | |
7625 | name[i] = X509_NAME_new(); | |
7626 | if (!TEST_ptr(name[i]) | |
7627 | || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN", | |
7628 | MBSTRING_ASC, | |
7629 | (unsigned char *) | |
7630 | strnames[i], | |
7631 | -1, -1, 0))) | |
7632 | goto end; | |
7633 | } | |
7634 | ||
5e30f2fd | 7635 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
fb8c8359 MC |
7636 | TLS_client_method(), |
7637 | TLS1_VERSION, | |
7638 | prot, | |
7639 | &sctx, &cctx, cert, privkey))) | |
7640 | goto end; | |
7641 | ||
7642 | SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL); | |
7643 | ||
7644 | if (tst == 0 || tst == 1) { | |
7645 | if (!TEST_ptr(sk1 = sk_X509_NAME_new_null()) | |
7646 | || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0]))) | |
7647 | || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1]))) | |
7648 | || !TEST_ptr(sk2 = sk_X509_NAME_new_null()) | |
7649 | || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0]))) | |
7650 | || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1])))) | |
7651 | goto end; | |
7652 | ||
7653 | SSL_CTX_set0_CA_list(sctx, sk1); | |
7654 | SSL_CTX_set0_CA_list(cctx, sk2); | |
7655 | sk1 = sk2 = NULL; | |
7656 | } | |
7657 | if (tst == 1 || tst == 2) { | |
7658 | if (!TEST_ptr(sk1 = sk_X509_NAME_new_null()) | |
7659 | || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2]))) | |
7660 | || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3]))) | |
7661 | || !TEST_ptr(sk2 = sk_X509_NAME_new_null()) | |
7662 | || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2]))) | |
7663 | || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3])))) | |
7664 | goto end; | |
7665 | ||
7666 | SSL_CTX_set_client_CA_list(sctx, sk1); | |
7667 | SSL_CTX_set_client_CA_list(cctx, sk2); | |
7668 | sk1 = sk2 = NULL; | |
7669 | } | |
7670 | ||
6e46c065 MC |
7671 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, |
7672 | NULL, NULL)) | |
7673 | || !TEST_true(create_ssl_connection(serverssl, clientssl, | |
7674 | SSL_ERROR_NONE))) | |
7675 | goto end; | |
7676 | ||
fb8c8359 MC |
7677 | /* |
7678 | * We only expect certificate authorities to have been sent to the server | |
7679 | * if we are using TLSv1.3 and SSL_set0_CA_list() was used | |
7680 | */ | |
7681 | sktmp = SSL_get0_peer_CA_list(serverssl); | |
7682 | if (prot == TLS1_3_VERSION | |
7683 | && (tst == 0 || tst == 1)) { | |
7684 | if (!TEST_ptr(sktmp) | |
7685 | || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2) | |
7686 | || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0), | |
7687 | name[0]), 0) | |
7688 | || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1), | |
7689 | name[1]), 0)) | |
7690 | goto end; | |
7691 | } else if (!TEST_ptr_null(sktmp)) { | |
7692 | goto end; | |
7693 | } | |
7694 | ||
7695 | /* | |
7696 | * In all tests we expect certificate authorities to have been sent to the | |
7697 | * client. However, SSL_set_client_CA_list() should override | |
7698 | * SSL_set0_CA_list() | |
7699 | */ | |
7700 | sktmp = SSL_get0_peer_CA_list(clientssl); | |
7701 | if (!TEST_ptr(sktmp) | |
7702 | || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2) | |
7703 | || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0), | |
7704 | name[tst == 0 ? 0 : 2]), 0) | |
7705 | || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1), | |
7706 | name[tst == 0 ? 1 : 3]), 0)) | |
7707 | goto end; | |
7708 | ||
6e46c065 MC |
7709 | testresult = 1; |
7710 | ||
7711 | end: | |
7712 | SSL_free(serverssl); | |
7713 | SSL_free(clientssl); | |
7714 | SSL_CTX_free(sctx); | |
7715 | SSL_CTX_free(cctx); | |
fb8c8359 MC |
7716 | for (i = 0; i < OSSL_NELEM(name); i++) |
7717 | X509_NAME_free(name[i]); | |
7718 | sk_X509_NAME_pop_free(sk1, X509_NAME_free); | |
7719 | sk_X509_NAME_pop_free(sk2, X509_NAME_free); | |
7720 | ||
7721 | return testresult; | |
7722 | } | |
7723 | #endif | |
7724 | ||
7725 | static int test_ca_names(int tst) | |
7726 | { | |
7727 | int testresult = 1; | |
7728 | ||
7729 | #ifndef OPENSSL_NO_TLS1_2 | |
7730 | testresult &= test_ca_names_int(TLS1_2_VERSION, tst); | |
7731 | #endif | |
7732 | #ifndef OPENSSL_NO_TLS1_3 | |
7733 | testresult &= test_ca_names_int(TLS1_3_VERSION, tst); | |
7734 | #endif | |
6e46c065 MC |
7735 | |
7736 | return testresult; | |
7737 | } | |
7738 | ||
0d2bfe52 SL |
7739 | #ifndef OPENSSL_NO_TLS1_2 |
7740 | static const char *multiblock_cipherlist_data[]= | |
7741 | { | |
7742 | "AES128-SHA", | |
7743 | "AES128-SHA256", | |
7744 | "AES256-SHA", | |
7745 | "AES256-SHA256", | |
7746 | }; | |
7747 | ||
7748 | /* Reduce the fragment size - so the multiblock test buffer can be small */ | |
7749 | # define MULTIBLOCK_FRAGSIZE 512 | |
7750 | ||
7751 | static int test_multiblock_write(int test_index) | |
7752 | { | |
7753 | static const char *fetchable_ciphers[]= | |
7754 | { | |
7755 | "AES-128-CBC-HMAC-SHA1", | |
7756 | "AES-128-CBC-HMAC-SHA256", | |
7757 | "AES-256-CBC-HMAC-SHA1", | |
7758 | "AES-256-CBC-HMAC-SHA256" | |
7759 | }; | |
7760 | const char *cipherlist = multiblock_cipherlist_data[test_index]; | |
7761 | const SSL_METHOD *smeth = TLS_server_method(); | |
7762 | const SSL_METHOD *cmeth = TLS_client_method(); | |
7763 | int min_version = TLS1_VERSION; | |
7764 | int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */ | |
7765 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7766 | SSL *clientssl = NULL, *serverssl = NULL; | |
7767 | int testresult = 0; | |
7768 | ||
7769 | /* | |
7770 | * Choose a buffer large enough to perform a multi-block operation | |
7771 | * i.e: write_len >= 4 * frag_size | |
7772 | * 9 * is chosen so that multiple multiblocks are used + some leftover. | |
7773 | */ | |
7774 | unsigned char msg[MULTIBLOCK_FRAGSIZE * 9]; | |
7775 | unsigned char buf[sizeof(msg)], *p = buf; | |
7776 | size_t readbytes, written, len; | |
7777 | EVP_CIPHER *ciph = NULL; | |
7778 | ||
7779 | /* | |
7780 | * Check if the cipher exists before attempting to use it since it only has | |
7781 | * a hardware specific implementation. | |
7782 | */ | |
7783 | ciph = EVP_CIPHER_fetch(NULL, fetchable_ciphers[test_index], ""); | |
7784 | if (ciph == NULL) { | |
7785 | TEST_skip("Multiblock cipher is not available for %s", cipherlist); | |
7786 | return 1; | |
7787 | } | |
7788 | EVP_CIPHER_free(ciph); | |
7789 | ||
7790 | /* Set up a buffer with some data that will be sent to the client */ | |
7791 | RAND_bytes(msg, sizeof(msg)); | |
7792 | ||
5e30f2fd MC |
7793 | if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version, |
7794 | max_version, &sctx, &cctx, cert, | |
7795 | privkey))) | |
0d2bfe52 SL |
7796 | goto end; |
7797 | ||
7798 | if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE))) | |
7799 | goto end; | |
7800 | ||
7801 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7802 | NULL, NULL))) | |
7803 | goto end; | |
7804 | ||
7805 | /* settings to force it to use AES-CBC-HMAC_SHA */ | |
7806 | SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC); | |
7807 | if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist))) | |
7808 | goto end; | |
7809 | ||
7810 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
7811 | goto end; | |
7812 | ||
7813 | if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written)) | |
7814 | || !TEST_size_t_eq(written, sizeof(msg))) | |
7815 | goto end; | |
7816 | ||
7817 | len = written; | |
7818 | while (len > 0) { | |
7819 | if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes))) | |
7820 | goto end; | |
7821 | p += readbytes; | |
7822 | len -= readbytes; | |
7823 | } | |
7824 | if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf))) | |
7825 | goto end; | |
7826 | ||
7827 | testresult = 1; | |
7828 | end: | |
7829 | SSL_free(serverssl); | |
7830 | SSL_free(clientssl); | |
7831 | SSL_CTX_free(sctx); | |
7832 | SSL_CTX_free(cctx); | |
7833 | ||
7834 | return testresult; | |
7835 | } | |
7836 | #endif /* OPENSSL_NO_TLS1_2 */ | |
a43ce58f | 7837 | |
49ef3d07 MC |
7838 | /* |
7839 | * Test 0: Client sets servername and server acknowledges it (TLSv1.2) | |
7840 | * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2) | |
7841 | * Test 2: Client sets inconsistent servername on resumption (TLSv1.2) | |
7842 | * Test 3: Client does not set servername on initial handshake (TLSv1.2) | |
7843 | * Test 4: Client does not set servername on resumption handshake (TLSv1.2) | |
7844 | * Test 5: Client sets servername and server acknowledges it (TLSv1.3) | |
7845 | * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3) | |
7846 | * Test 7: Client sets inconsistent servername on resumption (TLSv1.3) | |
7847 | * Test 8: Client does not set servername on initial handshake(TLSv1.3) | |
7848 | * Test 9: Client does not set servername on resumption handshake (TLSv1.3) | |
7849 | */ | |
7850 | static int test_servername(int tst) | |
7851 | { | |
7852 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
7853 | SSL *clientssl = NULL, *serverssl = NULL; | |
7854 | int testresult = 0; | |
7855 | SSL_SESSION *sess = NULL; | |
7856 | const char *sexpectedhost = NULL, *cexpectedhost = NULL; | |
7857 | ||
7858 | #ifdef OPENSSL_NO_TLS1_2 | |
7859 | if (tst <= 4) | |
7860 | return 1; | |
7861 | #endif | |
7862 | #ifdef OPENSSL_NO_TLS1_3 | |
7863 | if (tst >= 5) | |
7864 | return 1; | |
7865 | #endif | |
7866 | ||
5e30f2fd | 7867 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), |
49ef3d07 MC |
7868 | TLS_client_method(), |
7869 | TLS1_VERSION, | |
7870 | (tst <= 4) ? TLS1_2_VERSION | |
7871 | : TLS1_3_VERSION, | |
7872 | &sctx, &cctx, cert, privkey)) | |
7873 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
7874 | NULL, NULL))) | |
7875 | goto end; | |
7876 | ||
7877 | if (tst != 1 && tst != 6) { | |
7878 | if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, | |
7879 | hostname_cb))) | |
7880 | goto end; | |
7881 | } | |
7882 | ||
7883 | if (tst != 3 && tst != 8) { | |
7884 | if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))) | |
7885 | goto end; | |
7886 | sexpectedhost = cexpectedhost = "goodhost"; | |
7887 | } | |
7888 | ||
7889 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
7890 | goto end; | |
7891 | ||
7892 | if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name), | |
7893 | cexpectedhost) | |
7894 | || !TEST_str_eq(SSL_get_servername(serverssl, | |
7895 | TLSEXT_NAMETYPE_host_name), | |
7896 | sexpectedhost)) | |
7897 | goto end; | |
7898 | ||
7899 | /* Now repeat with a resumption handshake */ | |
7900 | ||
7901 | if (!TEST_int_eq(SSL_shutdown(clientssl), 0) | |
7902 | || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL) | |
7903 | || !TEST_true(SSL_SESSION_is_resumable(sess)) | |
7904 | || !TEST_int_eq(SSL_shutdown(serverssl), 0)) | |
7905 | goto end; | |
7906 | ||
7907 | SSL_free(clientssl); | |
7908 | SSL_free(serverssl); | |
7909 | clientssl = serverssl = NULL; | |
7910 | ||
7911 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, | |
7912 | NULL))) | |
7913 | goto end; | |
7914 | ||
7915 | if (!TEST_true(SSL_set_session(clientssl, sess))) | |
7916 | goto end; | |
7917 | ||
7918 | sexpectedhost = cexpectedhost = "goodhost"; | |
7919 | if (tst == 2 || tst == 7) { | |
7920 | /* Set an inconsistent hostname */ | |
7921 | if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost"))) | |
7922 | goto end; | |
7923 | /* | |
7924 | * In TLSv1.2 we expect the hostname from the original handshake, in | |
7925 | * TLSv1.3 we expect the hostname from this handshake | |
7926 | */ | |
7927 | if (tst == 7) | |
7928 | sexpectedhost = cexpectedhost = "altgoodhost"; | |
7929 | ||
7930 | if (!TEST_str_eq(SSL_get_servername(clientssl, | |
7931 | TLSEXT_NAMETYPE_host_name), | |
7932 | "altgoodhost")) | |
7933 | goto end; | |
7934 | } else if (tst == 4 || tst == 9) { | |
7935 | /* | |
7936 | * A TLSv1.3 session does not associate a session with a servername, | |
7937 | * but a TLSv1.2 session does. | |
7938 | */ | |
7939 | if (tst == 9) | |
7940 | sexpectedhost = cexpectedhost = NULL; | |
7941 | ||
7942 | if (!TEST_str_eq(SSL_get_servername(clientssl, | |
7943 | TLSEXT_NAMETYPE_host_name), | |
7944 | cexpectedhost)) | |
7945 | goto end; | |
7946 | } else { | |
7947 | if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))) | |
7948 | goto end; | |
7949 | /* | |
7950 | * In a TLSv1.2 resumption where the hostname was not acknowledged | |
7951 | * we expect the hostname on the server to be empty. On the client we | |
7952 | * return what was requested in this case. | |
7953 | * | |
7954 | * Similarly if the client didn't set a hostname on an original TLSv1.2 | |
7955 | * session but is now, the server hostname will be empty, but the client | |
7956 | * is as we set it. | |
7957 | */ | |
7958 | if (tst == 1 || tst == 3) | |
7959 | sexpectedhost = NULL; | |
7960 | ||
7961 | if (!TEST_str_eq(SSL_get_servername(clientssl, | |
7962 | TLSEXT_NAMETYPE_host_name), | |
7963 | "goodhost")) | |
7964 | goto end; | |
7965 | } | |
7966 | ||
7967 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
7968 | goto end; | |
7969 | ||
7970 | if (!TEST_true(SSL_session_reused(clientssl)) | |
7971 | || !TEST_true(SSL_session_reused(serverssl)) | |
7972 | || !TEST_str_eq(SSL_get_servername(clientssl, | |
7973 | TLSEXT_NAMETYPE_host_name), | |
7974 | cexpectedhost) | |
7975 | || !TEST_str_eq(SSL_get_servername(serverssl, | |
7976 | TLSEXT_NAMETYPE_host_name), | |
7977 | sexpectedhost)) | |
7978 | goto end; | |
7979 | ||
7980 | testresult = 1; | |
7981 | ||
7982 | end: | |
7983 | SSL_SESSION_free(sess); | |
7984 | SSL_free(serverssl); | |
7985 | SSL_free(clientssl); | |
7986 | SSL_CTX_free(sctx); | |
7987 | SSL_CTX_free(cctx); | |
7988 | ||
7989 | return testresult; | |
7990 | } | |
7991 | ||
b3842539 MC |
7992 | #ifndef OPENSSL_NO_EC |
7993 | /* | |
7994 | * Test that if signature algorithms are not available, then we do not offer or | |
7995 | * accept them. | |
7996 | * Test 0: Two RSA sig algs available: both RSA sig algs shared | |
7997 | * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared | |
7998 | * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared | |
7999 | * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared | |
8000 | * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared | |
8001 | * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared | |
8002 | */ | |
8003 | static int test_sigalgs_available(int idx) | |
8004 | { | |
8005 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
8006 | SSL *clientssl = NULL, *serverssl = NULL; | |
8007 | int testresult = 0; | |
8008 | OPENSSL_CTX *tmpctx = OPENSSL_CTX_new(); | |
8009 | OPENSSL_CTX *clientctx = libctx, *serverctx = libctx; | |
8010 | OSSL_PROVIDER *filterprov = NULL; | |
8011 | int sig, hash; | |
8012 | ||
8013 | if (!TEST_ptr(tmpctx)) | |
8014 | goto end; | |
8015 | ||
8016 | if (idx != 0 && idx != 3) { | |
8017 | if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter", | |
8018 | filter_provider_init))) | |
8019 | goto end; | |
8020 | ||
8021 | filterprov = OSSL_PROVIDER_load(tmpctx, "filter"); | |
8022 | if (!TEST_ptr(filterprov)) | |
8023 | goto end; | |
8024 | ||
8025 | if (idx < 3) { | |
8026 | /* | |
8027 | * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered | |
8028 | * or accepted for the peer that uses this libctx. Note that libssl | |
8029 | * *requires* SHA2-256 to be available so we cannot disable that. We | |
8030 | * also need SHA1 for our certificate. | |
8031 | */ | |
8032 | if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST, | |
8033 | "SHA2-256:SHA1"))) | |
8034 | goto end; | |
8035 | } else { | |
8036 | if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE, | |
8037 | "ECDSA")) | |
8038 | || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT, | |
8039 | "EC:X25519:X448"))) | |
8040 | goto end; | |
8041 | } | |
8042 | ||
8043 | if (idx == 1 || idx == 4) | |
8044 | clientctx = tmpctx; | |
8045 | else | |
8046 | serverctx = tmpctx; | |
8047 | } | |
8048 | ||
8049 | cctx = SSL_CTX_new_with_libctx(clientctx, NULL, TLS_client_method()); | |
8050 | sctx = SSL_CTX_new_with_libctx(serverctx, NULL, TLS_server_method()); | |
9d2d857f MC |
8051 | if (!TEST_ptr(cctx) || !TEST_ptr(sctx)) |
8052 | goto end; | |
b3842539 MC |
8053 | |
8054 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), | |
8055 | TLS_client_method(), | |
8056 | TLS1_VERSION, | |
8057 | 0, | |
8058 | &sctx, &cctx, cert, privkey))) | |
8059 | goto end; | |
8060 | ||
8061 | if (idx < 3) { | |
8062 | if (!SSL_CTX_set1_sigalgs_list(cctx, | |
8063 | "rsa_pss_rsae_sha384" | |
8064 | ":rsa_pss_rsae_sha256") | |
8065 | || !SSL_CTX_set1_sigalgs_list(sctx, | |
8066 | "rsa_pss_rsae_sha384" | |
8067 | ":rsa_pss_rsae_sha256")) | |
8068 | goto end; | |
8069 | } else { | |
8070 | if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256") | |
8071 | || !SSL_CTX_set1_sigalgs_list(sctx, | |
8072 | "rsa_pss_rsae_sha256:ECDSA+SHA256")) | |
8073 | goto end; | |
8074 | } | |
8075 | ||
8076 | if (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2, | |
8077 | SSL_FILETYPE_PEM), 1) | |
8078 | || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, | |
8079 | privkey2, | |
8080 | SSL_FILETYPE_PEM), 1) | |
8081 | || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)) | |
8082 | goto end; | |
8083 | ||
8084 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
8085 | NULL, NULL))) | |
8086 | goto end; | |
8087 | ||
8088 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
8089 | goto end; | |
8090 | ||
8091 | /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */ | |
8092 | if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL, | |
8093 | NULL, NULL), | |
8094 | (idx == 0 || idx == 3) ? 2 : 1)) | |
8095 | goto end; | |
8096 | ||
8097 | if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256)) | |
8098 | goto end; | |
8099 | ||
8100 | if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC | |
8101 | : NID_rsassaPss)) | |
8102 | goto end; | |
8103 | ||
8104 | testresult = 1; | |
8105 | ||
8106 | end: | |
8107 | SSL_free(serverssl); | |
8108 | SSL_free(clientssl); | |
8109 | SSL_CTX_free(sctx); | |
8110 | SSL_CTX_free(cctx); | |
8111 | OSSL_PROVIDER_unload(filterprov); | |
8112 | OPENSSL_CTX_free(tmpctx); | |
8113 | ||
8114 | return testresult; | |
8115 | } | |
8116 | #endif /* OPENSSL_NO_EC */ | |
8117 | ||
0c13cdf8 MC |
8118 | #ifndef OPENSSL_NO_TLS1_3 |
8119 | static int test_pluggable_group(void) | |
8120 | { | |
8121 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
8122 | SSL *clientssl = NULL, *serverssl = NULL; | |
8123 | int testresult = 0; | |
8124 | OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider"); | |
8125 | ||
8126 | if (!TEST_ptr(tlsprov)) | |
8127 | goto end; | |
8128 | ||
8129 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), | |
8130 | TLS_client_method(), | |
8131 | TLS1_3_VERSION, | |
8132 | TLS1_3_VERSION, | |
8133 | &sctx, &cctx, cert, privkey)) | |
8134 | || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
8135 | NULL, NULL))) | |
8136 | goto end; | |
8137 | ||
8138 | if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup")) | |
8139 | || !TEST_true(SSL_set1_groups_list(clientssl, "xorgroup"))) | |
8140 | goto end; | |
8141 | ||
8142 | if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) | |
8143 | goto end; | |
8144 | ||
8145 | testresult = 1; | |
8146 | ||
8147 | end: | |
8148 | SSL_free(serverssl); | |
8149 | SSL_free(clientssl); | |
8150 | SSL_CTX_free(sctx); | |
8151 | SSL_CTX_free(cctx); | |
8152 | OSSL_PROVIDER_unload(tlsprov); | |
8153 | ||
8154 | return testresult; | |
8155 | } | |
8156 | #endif | |
b3842539 | 8157 | |
49a36a52 MC |
8158 | #ifndef OPENSSL_NO_TLS1_2 |
8159 | static int test_ssl_dup(void) | |
8160 | { | |
8161 | SSL_CTX *cctx = NULL, *sctx = NULL; | |
8162 | SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL; | |
8163 | int testresult = 0; | |
8164 | BIO *rbio = NULL, *wbio = NULL; | |
8165 | ||
8166 | if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), | |
8167 | TLS_client_method(), | |
8168 | 0, | |
8169 | 0, | |
8170 | &sctx, &cctx, cert, privkey))) | |
8171 | goto end; | |
8172 | ||
8173 | if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, | |
8174 | NULL, NULL))) | |
8175 | goto end; | |
8176 | ||
8177 | if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION)) | |
8178 | || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION))) | |
8179 | goto end; | |
8180 | ||
8181 | client2ssl = SSL_dup(clientssl); | |
8182 | rbio = SSL_get_rbio(clientssl); | |
8183 | if (!TEST_ptr(rbio) | |
8184 | || !TEST_true(BIO_up_ref(rbio))) | |
8185 | goto end; | |
8186 | SSL_set0_rbio(client2ssl, rbio); | |
8187 | rbio = NULL; | |
8188 | ||
8189 | wbio = SSL_get_wbio(clientssl); | |
8190 | if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio))) | |
8191 | goto end; | |
8192 | SSL_set0_wbio(client2ssl, wbio); | |
8193 | rbio = NULL; | |
8194 | ||
8195 | if (!TEST_ptr(client2ssl) | |
8196 | /* Handshake not started so pointers should be different */ | |
8197 | || !TEST_ptr_ne(clientssl, client2ssl)) | |
8198 | goto end; | |
8199 | ||
8200 | if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION) | |
8201 | || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION)) | |
8202 | goto end; | |
8203 | ||
8204 | if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE))) | |
8205 | goto end; | |
8206 | ||
8207 | SSL_free(clientssl); | |
8208 | clientssl = SSL_dup(client2ssl); | |
8209 | if (!TEST_ptr(clientssl) | |
8210 | /* Handshake has finished so pointers should be the same */ | |
8211 | || !TEST_ptr_eq(clientssl, client2ssl)) | |
8212 | goto end; | |
8213 | ||
8214 | testresult = 1; | |
8215 | ||
8216 | end: | |
8217 | SSL_free(serverssl); | |
8218 | SSL_free(clientssl); | |
8219 | SSL_free(client2ssl); | |
8220 | SSL_CTX_free(sctx); | |
8221 | SSL_CTX_free(cctx); | |
8222 | ||
8223 | return testresult; | |
8224 | } | |
8225 | #endif | |
8226 | ||
5e30f2fd | 8227 | OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config\n") |
a43ce58f | 8228 | |
ad887416 | 8229 | int setup_tests(void) |
2cb4b5f6 | 8230 | { |
5e30f2fd MC |
8231 | char *modulename; |
8232 | char *configfile; | |
8233 | ||
8234 | libctx = OPENSSL_CTX_new(); | |
8235 | if (!TEST_ptr(libctx)) | |
8236 | return 0; | |
8237 | ||
8238 | defctxnull = OSSL_PROVIDER_load(NULL, "null"); | |
8239 | ||
8240 | /* | |
8241 | * Verify that the default and fips providers in the default libctx are not | |
8242 | * available | |
8243 | */ | |
8244 | if (!TEST_false(OSSL_PROVIDER_available(NULL, "default")) | |
8245 | || !TEST_false(OSSL_PROVIDER_available(NULL, "fips"))) | |
8246 | return 0; | |
8247 | ||
8d242823 MC |
8248 | if (!test_skip_common_options()) { |
8249 | TEST_error("Error parsing test options\n"); | |
8250 | return 0; | |
8251 | } | |
8252 | ||
1a2a3a42 MC |
8253 | if (!TEST_ptr(certsdir = test_get_argument(0)) |
8254 | || !TEST_ptr(srpvfile = test_get_argument(1)) | |
5e30f2fd MC |
8255 | || !TEST_ptr(tmpfilename = test_get_argument(2)) |
8256 | || !TEST_ptr(modulename = test_get_argument(3)) | |
8257 | || !TEST_ptr(configfile = test_get_argument(4))) | |
8258 | return 0; | |
8259 | ||
8260 | if (!TEST_true(OPENSSL_CTX_load_config(libctx, configfile))) | |
8261 | return 0; | |
8262 | ||
8263 | /* Check we have the expected provider available */ | |
8264 | if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename))) | |
8265 | return 0; | |
8266 | ||
8267 | /* Check the default provider is not available */ | |
8268 | if (strcmp(modulename, "default") != 0 | |
8269 | && !TEST_false(OSSL_PROVIDER_available(libctx, "default"))) | |
710756a9 | 8270 | return 0; |
2cb4b5f6 | 8271 | |
4f6c7044 MC |
8272 | if (strcmp(modulename, "fips") == 0) |
8273 | is_fips = 1; | |
8274 | ||
0c13cdf8 MC |
8275 | /* |
8276 | * We add, but don't load the test "tls-provider". We'll load it when we | |
8277 | * need it. | |
8278 | */ | |
8279 | if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider", | |
8280 | tls_provider_init))) | |
8281 | return 0; | |
8282 | ||
8283 | ||
f297e4ec RS |
8284 | if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) { |
8285 | #ifdef OPENSSL_NO_CRYPTO_MDEBUG | |
8286 | TEST_error("not supported in this build"); | |
8287 | return 0; | |
8288 | #else | |
8289 | int i, mcount, rcount, fcount; | |
8290 | ||
8291 | for (i = 0; i < 4; i++) | |
8292 | test_export_key_mat(i); | |
8293 | CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount); | |
8294 | test_printf_stdout("malloc %d realloc %d free %d\n", | |
8295 | mcount, rcount, fcount); | |
8296 | return 1; | |
8297 | #endif | |
8298 | } | |
8299 | ||
1a2a3a42 MC |
8300 | cert = test_mk_file_path(certsdir, "servercert.pem"); |
8301 | if (cert == NULL) | |
b3842539 | 8302 | goto err; |
1a2a3a42 MC |
8303 | |
8304 | privkey = test_mk_file_path(certsdir, "serverkey.pem"); | |
b3842539 MC |
8305 | if (privkey == NULL) |
8306 | goto err; | |
8307 | ||
8308 | cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem"); | |
8309 | if (cert2 == NULL) | |
8310 | goto err; | |
8311 | ||
8312 | privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem"); | |
8313 | if (privkey2 == NULL) | |
8314 | goto err; | |
1a2a3a42 | 8315 | |
da4db160 VF |
8316 | #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK) |
8317 | #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3) | |
8318 | ADD_ALL_TESTS(test_ktls, 32); | |
8319 | ADD_ALL_TESTS(test_ktls_sendfile_anytls, 6); | |
8320 | #endif | |
fe5d9450 | 8321 | #endif |
84d5549e | 8322 | ADD_TEST(test_large_message_tls); |
7856332e | 8323 | ADD_TEST(test_large_message_tls_read_ahead); |
55386bef | 8324 | #ifndef OPENSSL_NO_DTLS |
84d5549e | 8325 | ADD_TEST(test_large_message_dtls); |
55386bef | 8326 | #endif |
8f8c11d8 | 8327 | #ifndef OPENSSL_NO_OCSP |
c887104f | 8328 | ADD_TEST(test_tlsext_status_type); |
8f8c11d8 | 8329 | #endif |
eaa776da MC |
8330 | ADD_TEST(test_session_with_only_int_cache); |
8331 | ADD_TEST(test_session_with_only_ext_cache); | |
8332 | ADD_TEST(test_session_with_both_cache); | |
90fc2c26 | 8333 | ADD_TEST(test_session_wo_ca_names); |
36ff232c | 8334 | #ifndef OPENSSL_NO_TLS1_3 |
04d7814a MC |
8335 | ADD_ALL_TESTS(test_stateful_tickets, 3); |
8336 | ADD_ALL_TESTS(test_stateless_tickets, 3); | |
8614a4eb | 8337 | ADD_TEST(test_psk_tickets); |
f0049b86 | 8338 | ADD_ALL_TESTS(test_extra_tickets, 6); |
36ff232c | 8339 | #endif |
7fb4c820 | 8340 | ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS); |
9a716987 MC |
8341 | ADD_TEST(test_ssl_bio_pop_next_bio); |
8342 | ADD_TEST(test_ssl_bio_pop_ssl_bio); | |
8343 | ADD_TEST(test_ssl_bio_change_rbio); | |
8344 | ADD_TEST(test_ssl_bio_change_wbio); | |
c423ecaa | 8345 | #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) |
f1b25aae | 8346 | ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2); |
6acdd3e5 | 8347 | ADD_TEST(test_keylog); |
c423ecaa | 8348 | #endif |
6acdd3e5 CB |
8349 | #ifndef OPENSSL_NO_TLS1_3 |
8350 | ADD_TEST(test_keylog_no_master_key); | |
8351 | #endif | |
e9ee6536 | 8352 | #ifndef OPENSSL_NO_TLS1_2 |
a9c0d8be | 8353 | ADD_TEST(test_client_hello_cb); |
088dfa13 | 8354 | ADD_TEST(test_no_ems); |
3cd14e5e | 8355 | ADD_TEST(test_ccs_change_cipher); |
5f982038 MC |
8356 | #endif |
8357 | #ifndef OPENSSL_NO_TLS1_3 | |
02a3ed5a | 8358 | ADD_ALL_TESTS(test_early_data_read_write, 3); |
78fb5374 MC |
8359 | /* |
8360 | * We don't do replay tests for external PSK. Replay protection isn't used | |
8361 | * in that scenario. | |
8362 | */ | |
8363 | ADD_ALL_TESTS(test_early_data_replay, 2); | |
02a3ed5a MC |
8364 | ADD_ALL_TESTS(test_early_data_skip, 3); |
8365 | ADD_ALL_TESTS(test_early_data_skip_hrr, 3); | |
0efa0ba4 | 8366 | ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3); |
0d1b7789 | 8367 | ADD_ALL_TESTS(test_early_data_skip_abort, 3); |
02a3ed5a | 8368 | ADD_ALL_TESTS(test_early_data_not_sent, 3); |
57dee9bb | 8369 | ADD_ALL_TESTS(test_early_data_psk, 8); |
0b2b0be9 | 8370 | ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5); |
02a3ed5a | 8371 | ADD_ALL_TESTS(test_early_data_not_expected, 3); |
5f982038 | 8372 | # ifndef OPENSSL_NO_TLS1_2 |
02a3ed5a | 8373 | ADD_ALL_TESTS(test_early_data_tls1_2, 3); |
5f982038 | 8374 | # endif |
e9ee6536 | 8375 | #endif |
a273157a | 8376 | #ifndef OPENSSL_NO_TLS1_3 |
034cb87b | 8377 | ADD_ALL_TESTS(test_set_ciphersuite, 10); |
ca0413ae | 8378 | ADD_TEST(test_ciphersuite_change); |
5bf2eade | 8379 | ADD_ALL_TESTS(test_tls13_ciphersuite, 4); |
8380 | # ifdef OPENSSL_NO_PSK | |
c2b290c3 | 8381 | ADD_ALL_TESTS(test_tls13_psk, 1); |
5bf2eade | 8382 | # else |
0d8da779 | 8383 | ADD_ALL_TESTS(test_tls13_psk, 4); |
5bf2eade | 8384 | # endif /* OPENSSL_NO_PSK */ |
8385 | # ifndef OPENSSL_NO_TLS1_2 | |
8386 | /* Test with both TLSv1.3 and 1.2 versions */ | |
db26ec80 | 8387 | ADD_ALL_TESTS(test_key_exchange, 14); |
5bf2eade | 8388 | # else |
8389 | /* Test with only TLSv1.3 versions */ | |
8390 | ADD_ALL_TESTS(test_key_exchange, 12); | |
8391 | # endif | |
bb01ef3f | 8392 | ADD_ALL_TESTS(test_custom_exts, 5); |
c7b8ff25 | 8393 | ADD_TEST(test_stateless); |
9d75dce3 | 8394 | ADD_TEST(test_pha_key_update); |
a273157a | 8395 | #else |
bb01ef3f | 8396 | ADD_ALL_TESTS(test_custom_exts, 3); |
a273157a | 8397 | #endif |
16afd71c | 8398 | ADD_ALL_TESTS(test_serverinfo, 8); |
0fb2815b | 8399 | ADD_ALL_TESTS(test_export_key_mat, 6); |
b38ede80 TT |
8400 | #ifndef OPENSSL_NO_TLS1_3 |
8401 | ADD_ALL_TESTS(test_export_key_mat_early, 3); | |
3409a5ff | 8402 | ADD_TEST(test_key_update); |
a77b4dba | 8403 | ADD_ALL_TESTS(test_key_update_in_write, 2); |
b38ede80 | 8404 | #endif |
e11b6aa4 | 8405 | ADD_ALL_TESTS(test_ssl_clear, 2); |
cf72c757 | 8406 | ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test)); |
76fd7a1d MC |
8407 | #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2) |
8408 | ADD_ALL_TESTS(test_srp, 6); | |
8409 | #endif | |
5718fe45 | 8410 | ADD_ALL_TESTS(test_info_callback, 6); |
4a432af8 | 8411 | ADD_ALL_TESTS(test_ssl_pending, 2); |
e401389a | 8412 | ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data)); |
a76ce286 | 8413 | ADD_ALL_TESTS(test_ticket_callbacks, 16); |
57d7b988 | 8414 | ADD_ALL_TESTS(test_shutdown, 7); |
e638112e | 8415 | ADD_ALL_TESTS(test_incorrect_shutdown, 2); |
1a2a3a42 | 8416 | ADD_ALL_TESTS(test_cert_cb, 6); |
6e46c065 | 8417 | ADD_ALL_TESTS(test_client_cert_cb, 2); |
fb8c8359 | 8418 | ADD_ALL_TESTS(test_ca_names, 3); |
0d2bfe52 SL |
8419 | #ifndef OPENSSL_NO_TLS1_2 |
8420 | ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data)); | |
8421 | #endif | |
49ef3d07 | 8422 | ADD_ALL_TESTS(test_servername, 10); |
b3842539 MC |
8423 | #ifndef OPENSSL_NO_EC |
8424 | ADD_ALL_TESTS(test_sigalgs_available, 6); | |
0c13cdf8 MC |
8425 | #endif |
8426 | #ifndef OPENSSL_NO_TLS1_3 | |
8427 | ADD_TEST(test_pluggable_group); | |
49a36a52 MC |
8428 | #endif |
8429 | #ifndef OPENSSL_NO_TLS1_2 | |
8430 | ADD_TEST(test_ssl_dup); | |
b3842539 | 8431 | #endif |
ad887416 | 8432 | return 1; |
b3842539 MC |
8433 | |
8434 | err: | |
8435 | OPENSSL_free(cert); | |
8436 | OPENSSL_free(privkey); | |
8437 | OPENSSL_free(cert2); | |
8438 | OPENSSL_free(privkey2); | |
8439 | return 0; | |
ad887416 | 8440 | } |
2cb4b5f6 | 8441 | |
ad887416 P |
8442 | void cleanup_tests(void) |
8443 | { | |
1a2a3a42 MC |
8444 | OPENSSL_free(cert); |
8445 | OPENSSL_free(privkey); | |
b3842539 MC |
8446 | OPENSSL_free(cert2); |
8447 | OPENSSL_free(privkey2); | |
fa454945 | 8448 | bio_s_mempacket_test_free(); |
a77b4dba | 8449 | bio_s_always_retry_free(); |
5e30f2fd MC |
8450 | OSSL_PROVIDER_unload(defctxnull); |
8451 | OPENSSL_CTX_free(libctx); | |
2cb4b5f6 | 8452 | } |