sslapitest.c revision 1.2 1 1.1 christos /*
2 1.2 christos * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 christos *
4 1.2 christos * Licensed under the Apache License 2.0 (the "License"). You may not use
5 1.1 christos * this file except in compliance with the License. You can obtain a copy
6 1.1 christos * in the file LICENSE in the source distribution or at
7 1.1 christos * https://www.openssl.org/source/license.html
8 1.1 christos */
9 1.1 christos
10 1.2 christos /*
11 1.2 christos * We need access to the deprecated low level HMAC APIs for legacy purposes
12 1.2 christos * when the deprecated calls are not hidden
13 1.2 christos */
14 1.2 christos #ifndef OPENSSL_NO_DEPRECATED_3_0
15 1.2 christos # define OPENSSL_SUPPRESS_DEPRECATED
16 1.2 christos #endif
17 1.2 christos
18 1.2 christos #include <stdio.h>
19 1.1 christos #include <string.h>
20 1.1 christos
21 1.1 christos #include <openssl/opensslconf.h>
22 1.1 christos #include <openssl/bio.h>
23 1.1 christos #include <openssl/crypto.h>
24 1.1 christos #include <openssl/ssl.h>
25 1.1 christos #include <openssl/ocsp.h>
26 1.2 christos #include <openssl/srp.h>
27 1.2 christos #include <openssl/txt_db.h>
28 1.2 christos #include <openssl/aes.h>
29 1.2 christos #include <openssl/rand.h>
30 1.2 christos #include <openssl/core_names.h>
31 1.2 christos #include <openssl/core_dispatch.h>
32 1.2 christos #include <openssl/provider.h>
33 1.2 christos #include <openssl/param_build.h>
34 1.2 christos #include <openssl/x509v3.h>
35 1.2 christos #include <openssl/dh.h>
36 1.2 christos #include <openssl/engine.h>
37 1.1 christos
38 1.2 christos #include "helpers/ssltestlib.h"
39 1.1 christos #include "testutil.h"
40 1.2 christos #include "testutil/output.h"
41 1.2 christos #include "internal/nelem.h"
42 1.2 christos #include "internal/ktls.h"
43 1.2 christos #include "../ssl/ssl_local.h"
44 1.2 christos #include "filterprov.h"
45 1.2 christos
46 1.2 christos #undef OSSL_NO_USABLE_TLS1_3
47 1.2 christos #if defined(OPENSSL_NO_TLS1_3) \
48 1.2 christos || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
49 1.2 christos /*
50 1.2 christos * If we don't have ec or dh then there are no built-in groups that are usable
51 1.2 christos * with TLSv1.3
52 1.2 christos */
53 1.2 christos # define OSSL_NO_USABLE_TLS1_3
54 1.2 christos #endif
55 1.2 christos
56 1.2 christos /* Defined in tls-provider.c */
57 1.2 christos int tls_provider_init(const OSSL_CORE_HANDLE *handle,
58 1.2 christos const OSSL_DISPATCH *in,
59 1.2 christos const OSSL_DISPATCH **out,
60 1.2 christos void **provctx);
61 1.2 christos
62 1.2 christos static OSSL_LIB_CTX *libctx = NULL;
63 1.2 christos static OSSL_PROVIDER *defctxnull = NULL;
64 1.2 christos
65 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
66 1.2 christos
67 1.2 christos static SSL_SESSION *clientpsk = NULL;
68 1.2 christos static SSL_SESSION *serverpsk = NULL;
69 1.2 christos static const char *pskid = "Identity";
70 1.2 christos static const char *srvid;
71 1.2 christos
72 1.2 christos static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
73 1.2 christos size_t *idlen, SSL_SESSION **sess);
74 1.2 christos static int find_session_cb(SSL *ssl, const unsigned char *identity,
75 1.2 christos size_t identity_len, SSL_SESSION **sess);
76 1.1 christos
77 1.2 christos static int use_session_cb_cnt = 0;
78 1.2 christos static int find_session_cb_cnt = 0;
79 1.2 christos
80 1.2 christos static SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize);
81 1.2 christos #endif
82 1.2 christos
83 1.2 christos static char *certsdir = NULL;
84 1.1 christos static char *cert = NULL;
85 1.1 christos static char *privkey = NULL;
86 1.2 christos static char *cert2 = NULL;
87 1.2 christos static char *privkey2 = NULL;
88 1.2 christos static char *cert1024 = NULL;
89 1.2 christos static char *privkey1024 = NULL;
90 1.2 christos static char *cert3072 = NULL;
91 1.2 christos static char *privkey3072 = NULL;
92 1.2 christos static char *cert4096 = NULL;
93 1.2 christos static char *privkey4096 = NULL;
94 1.2 christos static char *cert8192 = NULL;
95 1.2 christos static char *privkey8192 = NULL;
96 1.2 christos static char *srpvfile = NULL;
97 1.2 christos static char *tmpfilename = NULL;
98 1.2 christos static char *dhfile = NULL;
99 1.2 christos
100 1.2 christos static int is_fips = 0;
101 1.2 christos
102 1.2 christos #define LOG_BUFFER_SIZE 2048
103 1.2 christos static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
104 1.2 christos static size_t server_log_buffer_index = 0;
105 1.2 christos static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
106 1.2 christos static size_t client_log_buffer_index = 0;
107 1.2 christos static int error_writing_log = 0;
108 1.1 christos
109 1.1 christos #ifndef OPENSSL_NO_OCSP
110 1.1 christos static const unsigned char orespder[] = "Dummy OCSP Response";
111 1.1 christos static int ocsp_server_called = 0;
112 1.1 christos static int ocsp_client_called = 0;
113 1.1 christos
114 1.1 christos static int cdummyarg = 1;
115 1.1 christos static X509 *ocspcert = NULL;
116 1.1 christos #endif
117 1.1 christos
118 1.1 christos #define NUM_EXTRA_CERTS 40
119 1.2 christos #define CLIENT_VERSION_LEN 2
120 1.2 christos
121 1.2 christos /*
122 1.2 christos * This structure is used to validate that the correct number of log messages
123 1.2 christos * of various types are emitted when emitting secret logs.
124 1.2 christos */
125 1.2 christos struct sslapitest_log_counts {
126 1.2 christos unsigned int rsa_key_exchange_count;
127 1.2 christos unsigned int master_secret_count;
128 1.2 christos unsigned int client_early_secret_count;
129 1.2 christos unsigned int client_handshake_secret_count;
130 1.2 christos unsigned int server_handshake_secret_count;
131 1.2 christos unsigned int client_application_secret_count;
132 1.2 christos unsigned int server_application_secret_count;
133 1.2 christos unsigned int early_exporter_secret_count;
134 1.2 christos unsigned int exporter_secret_count;
135 1.2 christos };
136 1.2 christos
137 1.2 christos
138 1.2 christos static int hostname_cb(SSL *s, int *al, void *arg)
139 1.2 christos {
140 1.2 christos const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
141 1.2 christos
142 1.2 christos if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
143 1.2 christos || strcmp(hostname, "altgoodhost") == 0))
144 1.2 christos return SSL_TLSEXT_ERR_OK;
145 1.2 christos
146 1.2 christos return SSL_TLSEXT_ERR_NOACK;
147 1.2 christos }
148 1.1 christos
149 1.2 christos static void client_keylog_callback(const SSL *ssl, const char *line)
150 1.1 christos {
151 1.2 christos int line_length = strlen(line);
152 1.1 christos
153 1.2 christos /* If the log doesn't fit, error out. */
154 1.2 christos if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
155 1.2 christos TEST_info("Client log too full");
156 1.2 christos error_writing_log = 1;
157 1.2 christos return;
158 1.1 christos }
159 1.2 christos
160 1.2 christos strcat(client_log_buffer, line);
161 1.2 christos client_log_buffer_index += line_length;
162 1.2 christos client_log_buffer[client_log_buffer_index++] = '\n';
163 1.2 christos }
164 1.2 christos
165 1.2 christos static void server_keylog_callback(const SSL *ssl, const char *line)
166 1.2 christos {
167 1.2 christos int line_length = strlen(line);
168 1.2 christos
169 1.2 christos /* If the log doesn't fit, error out. */
170 1.2 christos if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
171 1.2 christos TEST_info("Server log too full");
172 1.2 christos error_writing_log = 1;
173 1.2 christos return;
174 1.1 christos }
175 1.1 christos
176 1.2 christos strcat(server_log_buffer, line);
177 1.2 christos server_log_buffer_index += line_length;
178 1.2 christos server_log_buffer[server_log_buffer_index++] = '\n';
179 1.2 christos }
180 1.2 christos
181 1.2 christos static int compare_hex_encoded_buffer(const char *hex_encoded,
182 1.2 christos size_t hex_length,
183 1.2 christos const uint8_t *raw,
184 1.2 christos size_t raw_length)
185 1.2 christos {
186 1.2 christos size_t i, j;
187 1.2 christos char hexed[3];
188 1.2 christos
189 1.2 christos if (!TEST_size_t_eq(raw_length * 2, hex_length))
190 1.2 christos return 1;
191 1.2 christos
192 1.2 christos for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
193 1.2 christos sprintf(hexed, "%02x", raw[i]);
194 1.2 christos if (!TEST_int_eq(hexed[0], hex_encoded[j])
195 1.2 christos || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
196 1.2 christos return 1;
197 1.1 christos }
198 1.1 christos
199 1.2 christos return 0;
200 1.2 christos }
201 1.1 christos
202 1.2 christos static int test_keylog_output(char *buffer, const SSL *ssl,
203 1.2 christos const SSL_SESSION *session,
204 1.2 christos struct sslapitest_log_counts *expected)
205 1.2 christos {
206 1.2 christos char *token = NULL;
207 1.2 christos unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
208 1.2 christos size_t client_random_size = SSL3_RANDOM_SIZE;
209 1.2 christos unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
210 1.2 christos size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
211 1.2 christos unsigned int rsa_key_exchange_count = 0;
212 1.2 christos unsigned int master_secret_count = 0;
213 1.2 christos unsigned int client_early_secret_count = 0;
214 1.2 christos unsigned int client_handshake_secret_count = 0;
215 1.2 christos unsigned int server_handshake_secret_count = 0;
216 1.2 christos unsigned int client_application_secret_count = 0;
217 1.2 christos unsigned int server_application_secret_count = 0;
218 1.2 christos unsigned int early_exporter_secret_count = 0;
219 1.2 christos unsigned int exporter_secret_count = 0;
220 1.2 christos
221 1.2 christos for (token = strtok(buffer, " \n"); token != NULL;
222 1.2 christos token = strtok(NULL, " \n")) {
223 1.2 christos if (strcmp(token, "RSA") == 0) {
224 1.2 christos /*
225 1.2 christos * Premaster secret. Tokens should be: 16 ASCII bytes of
226 1.2 christos * hex-encoded encrypted secret, then the hex-encoded pre-master
227 1.2 christos * secret.
228 1.2 christos */
229 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
230 1.2 christos return 0;
231 1.2 christos if (!TEST_size_t_eq(strlen(token), 16))
232 1.2 christos return 0;
233 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
234 1.2 christos return 0;
235 1.2 christos /*
236 1.2 christos * We can't sensibly check the log because the premaster secret is
237 1.2 christos * transient, and OpenSSL doesn't keep hold of it once the master
238 1.2 christos * secret is generated.
239 1.2 christos */
240 1.2 christos rsa_key_exchange_count++;
241 1.2 christos } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
242 1.2 christos /*
243 1.2 christos * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
244 1.2 christos * client random, then the hex-encoded master secret.
245 1.2 christos */
246 1.2 christos client_random_size = SSL_get_client_random(ssl,
247 1.2 christos actual_client_random,
248 1.2 christos SSL3_RANDOM_SIZE);
249 1.2 christos if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
250 1.2 christos return 0;
251 1.2 christos
252 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
253 1.2 christos return 0;
254 1.2 christos if (!TEST_size_t_eq(strlen(token), 64))
255 1.2 christos return 0;
256 1.2 christos if (!TEST_false(compare_hex_encoded_buffer(token, 64,
257 1.2 christos actual_client_random,
258 1.2 christos client_random_size)))
259 1.2 christos return 0;
260 1.2 christos
261 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
262 1.2 christos return 0;
263 1.2 christos master_key_size = SSL_SESSION_get_master_key(session,
264 1.2 christos actual_master_key,
265 1.2 christos master_key_size);
266 1.2 christos if (!TEST_size_t_ne(master_key_size, 0))
267 1.2 christos return 0;
268 1.2 christos if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
269 1.2 christos actual_master_key,
270 1.2 christos master_key_size)))
271 1.2 christos return 0;
272 1.2 christos master_secret_count++;
273 1.2 christos } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
274 1.2 christos || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
275 1.2 christos || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
276 1.2 christos || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
277 1.2 christos || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
278 1.2 christos || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
279 1.2 christos || strcmp(token, "EXPORTER_SECRET") == 0) {
280 1.2 christos /*
281 1.2 christos * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
282 1.2 christos * client random, and then the hex-encoded secret. In this case,
283 1.2 christos * we treat all of these secrets identically and then just
284 1.2 christos * distinguish between them when counting what we saw.
285 1.2 christos */
286 1.2 christos if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
287 1.2 christos client_early_secret_count++;
288 1.2 christos else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
289 1.2 christos client_handshake_secret_count++;
290 1.2 christos else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
291 1.2 christos server_handshake_secret_count++;
292 1.2 christos else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
293 1.2 christos client_application_secret_count++;
294 1.2 christos else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
295 1.2 christos server_application_secret_count++;
296 1.2 christos else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
297 1.2 christos early_exporter_secret_count++;
298 1.2 christos else if (strcmp(token, "EXPORTER_SECRET") == 0)
299 1.2 christos exporter_secret_count++;
300 1.2 christos
301 1.2 christos client_random_size = SSL_get_client_random(ssl,
302 1.2 christos actual_client_random,
303 1.2 christos SSL3_RANDOM_SIZE);
304 1.2 christos if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
305 1.2 christos return 0;
306 1.2 christos
307 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
308 1.2 christos return 0;
309 1.2 christos if (!TEST_size_t_eq(strlen(token), 64))
310 1.2 christos return 0;
311 1.2 christos if (!TEST_false(compare_hex_encoded_buffer(token, 64,
312 1.2 christos actual_client_random,
313 1.2 christos client_random_size)))
314 1.2 christos return 0;
315 1.2 christos
316 1.2 christos if (!TEST_ptr(token = strtok(NULL, " \n")))
317 1.2 christos return 0;
318 1.2 christos } else {
319 1.2 christos TEST_info("Unexpected token %s\n", token);
320 1.2 christos return 0;
321 1.1 christos }
322 1.1 christos }
323 1.1 christos
324 1.2 christos /* Got what we expected? */
325 1.2 christos if (!TEST_size_t_eq(rsa_key_exchange_count,
326 1.2 christos expected->rsa_key_exchange_count)
327 1.2 christos || !TEST_size_t_eq(master_secret_count,
328 1.2 christos expected->master_secret_count)
329 1.2 christos || !TEST_size_t_eq(client_early_secret_count,
330 1.2 christos expected->client_early_secret_count)
331 1.2 christos || !TEST_size_t_eq(client_handshake_secret_count,
332 1.2 christos expected->client_handshake_secret_count)
333 1.2 christos || !TEST_size_t_eq(server_handshake_secret_count,
334 1.2 christos expected->server_handshake_secret_count)
335 1.2 christos || !TEST_size_t_eq(client_application_secret_count,
336 1.2 christos expected->client_application_secret_count)
337 1.2 christos || !TEST_size_t_eq(server_application_secret_count,
338 1.2 christos expected->server_application_secret_count)
339 1.2 christos || !TEST_size_t_eq(early_exporter_secret_count,
340 1.2 christos expected->early_exporter_secret_count)
341 1.2 christos || !TEST_size_t_eq(exporter_secret_count,
342 1.2 christos expected->exporter_secret_count))
343 1.2 christos return 0;
344 1.2 christos return 1;
345 1.2 christos }
346 1.2 christos
347 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
348 1.2 christos static int test_keylog(void)
349 1.2 christos {
350 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
351 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
352 1.2 christos int testresult = 0;
353 1.2 christos struct sslapitest_log_counts expected;
354 1.2 christos
355 1.2 christos /* Clean up logging space */
356 1.2 christos memset(&expected, 0, sizeof(expected));
357 1.2 christos memset(client_log_buffer, 0, sizeof(client_log_buffer));
358 1.2 christos memset(server_log_buffer, 0, sizeof(server_log_buffer));
359 1.2 christos client_log_buffer_index = 0;
360 1.2 christos server_log_buffer_index = 0;
361 1.2 christos error_writing_log = 0;
362 1.2 christos
363 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
364 1.2 christos TLS_client_method(),
365 1.2 christos TLS1_VERSION, 0,
366 1.2 christos &sctx, &cctx, cert, privkey)))
367 1.2 christos return 0;
368 1.1 christos
369 1.2 christos /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
370 1.2 christos SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
371 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
372 1.2 christos
373 1.2 christos /* We also want to ensure that we use RSA-based key exchange. */
374 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
375 1.2 christos goto end;
376 1.2 christos
377 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
378 1.2 christos || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
379 1.2 christos goto end;
380 1.2 christos SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
381 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
382 1.2 christos == client_keylog_callback))
383 1.2 christos goto end;
384 1.2 christos SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
385 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
386 1.2 christos == server_keylog_callback))
387 1.2 christos goto end;
388 1.2 christos
389 1.2 christos /* Now do a handshake and check that the logs have been written to. */
390 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
391 1.2 christos &clientssl, NULL, NULL))
392 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
393 1.2 christos SSL_ERROR_NONE))
394 1.2 christos || !TEST_false(error_writing_log)
395 1.2 christos || !TEST_int_gt(client_log_buffer_index, 0)
396 1.2 christos || !TEST_int_gt(server_log_buffer_index, 0))
397 1.1 christos goto end;
398 1.1 christos
399 1.1 christos /*
400 1.2 christos * Now we want to test that our output data was vaguely sensible. We
401 1.2 christos * do that by using strtok and confirming that we have more or less the
402 1.2 christos * data we expect. For both client and server, we expect to see one master
403 1.2 christos * secret. The client should also see an RSA key exchange.
404 1.1 christos */
405 1.2 christos expected.rsa_key_exchange_count = 1;
406 1.2 christos expected.master_secret_count = 1;
407 1.2 christos if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
408 1.2 christos SSL_get_session(clientssl), &expected)))
409 1.2 christos goto end;
410 1.2 christos
411 1.2 christos expected.rsa_key_exchange_count = 0;
412 1.2 christos if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
413 1.2 christos SSL_get_session(serverssl), &expected)))
414 1.1 christos goto end;
415 1.1 christos
416 1.1 christos testresult = 1;
417 1.2 christos
418 1.2 christos end:
419 1.1 christos SSL_free(serverssl);
420 1.1 christos SSL_free(clientssl);
421 1.1 christos SSL_CTX_free(sctx);
422 1.1 christos SSL_CTX_free(cctx);
423 1.1 christos
424 1.1 christos return testresult;
425 1.1 christos }
426 1.2 christos #endif
427 1.1 christos
428 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
429 1.2 christos static int test_keylog_no_master_key(void)
430 1.1 christos {
431 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
432 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
433 1.2 christos SSL_SESSION *sess = NULL;
434 1.2 christos int testresult = 0;
435 1.2 christos struct sslapitest_log_counts expected;
436 1.2 christos unsigned char buf[1];
437 1.2 christos size_t readbytes, written;
438 1.2 christos
439 1.2 christos /* Clean up logging space */
440 1.2 christos memset(&expected, 0, sizeof(expected));
441 1.2 christos memset(client_log_buffer, 0, sizeof(client_log_buffer));
442 1.2 christos memset(server_log_buffer, 0, sizeof(server_log_buffer));
443 1.2 christos client_log_buffer_index = 0;
444 1.2 christos server_log_buffer_index = 0;
445 1.2 christos error_writing_log = 0;
446 1.2 christos
447 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
448 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
449 1.2 christos &sctx, &cctx, cert, privkey))
450 1.2 christos || !TEST_true(SSL_CTX_set_max_early_data(sctx,
451 1.2 christos SSL3_RT_MAX_PLAIN_LENGTH)))
452 1.2 christos return 0;
453 1.2 christos
454 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
455 1.2 christos || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
456 1.2 christos goto end;
457 1.2 christos
458 1.2 christos SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
459 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
460 1.2 christos == client_keylog_callback))
461 1.2 christos goto end;
462 1.2 christos
463 1.2 christos SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
464 1.2 christos if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
465 1.2 christos == server_keylog_callback))
466 1.2 christos goto end;
467 1.1 christos
468 1.2 christos /* Now do a handshake and check that the logs have been written to. */
469 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
470 1.2 christos &clientssl, NULL, NULL))
471 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
472 1.2 christos SSL_ERROR_NONE))
473 1.2 christos || !TEST_false(error_writing_log))
474 1.2 christos goto end;
475 1.1 christos
476 1.1 christos /*
477 1.2 christos * Now we want to test that our output data was vaguely sensible. For this
478 1.2 christos * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
479 1.2 christos * TLSv1.3, but we do expect both client and server to emit keys.
480 1.1 christos */
481 1.2 christos expected.client_handshake_secret_count = 1;
482 1.2 christos expected.server_handshake_secret_count = 1;
483 1.2 christos expected.client_application_secret_count = 1;
484 1.2 christos expected.server_application_secret_count = 1;
485 1.2 christos expected.exporter_secret_count = 1;
486 1.2 christos if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
487 1.2 christos SSL_get_session(clientssl), &expected))
488 1.2 christos || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
489 1.2 christos SSL_get_session(serverssl),
490 1.2 christos &expected)))
491 1.2 christos goto end;
492 1.2 christos
493 1.2 christos /* Terminate old session and resume with early data. */
494 1.2 christos sess = SSL_get1_session(clientssl);
495 1.2 christos SSL_shutdown(clientssl);
496 1.2 christos SSL_shutdown(serverssl);
497 1.2 christos SSL_free(serverssl);
498 1.2 christos SSL_free(clientssl);
499 1.2 christos serverssl = clientssl = NULL;
500 1.2 christos
501 1.2 christos /* Reset key log */
502 1.2 christos memset(client_log_buffer, 0, sizeof(client_log_buffer));
503 1.2 christos memset(server_log_buffer, 0, sizeof(server_log_buffer));
504 1.2 christos client_log_buffer_index = 0;
505 1.2 christos server_log_buffer_index = 0;
506 1.2 christos
507 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
508 1.2 christos &clientssl, NULL, NULL))
509 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess))
510 1.2 christos /* Here writing 0 length early data is enough. */
511 1.2 christos || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
512 1.2 christos || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
513 1.2 christos &readbytes),
514 1.2 christos SSL_READ_EARLY_DATA_ERROR)
515 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
516 1.2 christos SSL_EARLY_DATA_ACCEPTED)
517 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
518 1.2 christos SSL_ERROR_NONE))
519 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
520 1.2 christos goto end;
521 1.2 christos
522 1.2 christos /* In addition to the previous entries, expect early secrets. */
523 1.2 christos expected.client_early_secret_count = 1;
524 1.2 christos expected.early_exporter_secret_count = 1;
525 1.2 christos if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
526 1.2 christos SSL_get_session(clientssl), &expected))
527 1.2 christos || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
528 1.2 christos SSL_get_session(serverssl),
529 1.2 christos &expected)))
530 1.2 christos goto end;
531 1.2 christos
532 1.2 christos testresult = 1;
533 1.2 christos
534 1.2 christos end:
535 1.2 christos SSL_SESSION_free(sess);
536 1.2 christos SSL_free(serverssl);
537 1.2 christos SSL_free(clientssl);
538 1.2 christos SSL_CTX_free(sctx);
539 1.2 christos SSL_CTX_free(cctx);
540 1.2 christos
541 1.2 christos return testresult;
542 1.1 christos }
543 1.1 christos #endif
544 1.1 christos
545 1.2 christos static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
546 1.2 christos {
547 1.2 christos int res = X509_verify_cert(ctx);
548 1.2 christos int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
549 1.2 christos SSL *ssl;
550 1.2 christos
551 1.2 christos /* this should not happen but check anyway */
552 1.2 christos if (idx < 0
553 1.2 christos || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
554 1.2 christos return 0;
555 1.2 christos
556 1.2 christos if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
557 1.2 christos X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
558 1.2 christos /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
559 1.2 christos return SSL_set_retry_verify(ssl);
560 1.2 christos
561 1.2 christos return res;
562 1.2 christos }
563 1.2 christos
564 1.2 christos static int test_client_cert_verify_cb(void)
565 1.1 christos {
566 1.2 christos /* server key, cert, chain, and root */
567 1.2 christos char *skey = test_mk_file_path(certsdir, "leaf.key");
568 1.2 christos char *leaf = test_mk_file_path(certsdir, "leaf.pem");
569 1.2 christos char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
570 1.2 christos char *int1 = test_mk_file_path(certsdir, "interCA.pem");
571 1.2 christos char *root = test_mk_file_path(certsdir, "rootCA.pem");
572 1.2 christos X509 *crt1 = NULL, *crt2 = NULL;
573 1.2 christos STACK_OF(X509) *server_chain;
574 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
575 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
576 1.2 christos int testresult = 0;
577 1.2 christos
578 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
579 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
580 1.2 christos &sctx, &cctx, NULL, NULL)))
581 1.2 christos goto end;
582 1.2 christos if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
583 1.2 christos || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
584 1.2 christos SSL_FILETYPE_PEM), 1)
585 1.2 christos || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
586 1.2 christos goto end;
587 1.2 christos if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
588 1.2 christos goto end;
589 1.2 christos SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
590 1.2 christos SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
591 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
592 1.2 christos &clientssl, NULL, NULL)))
593 1.2 christos goto end;
594 1.2 christos
595 1.2 christos /* attempt SSL_connect() with incomplete server chain */
596 1.2 christos if (!TEST_false(create_ssl_connection(serverssl, clientssl,
597 1.2 christos SSL_ERROR_WANT_RETRY_VERIFY)))
598 1.2 christos goto end;
599 1.2 christos
600 1.2 christos /* application provides intermediate certs needed to verify server cert */
601 1.2 christos if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
602 1.2 christos || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
603 1.2 christos || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
604 1.2 christos goto end;
605 1.2 christos /* add certs in reverse order to demonstrate real chain building */
606 1.2 christos if (!TEST_true(sk_X509_push(server_chain, crt1)))
607 1.2 christos goto end;
608 1.2 christos crt1 = NULL;
609 1.2 christos if (!TEST_true(sk_X509_push(server_chain, crt2)))
610 1.2 christos goto end;
611 1.2 christos crt2 = NULL;
612 1.2 christos
613 1.2 christos /* continue SSL_connect(), must now succeed with completed server chain */
614 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
615 1.2 christos SSL_ERROR_NONE)))
616 1.2 christos goto end;
617 1.1 christos
618 1.2 christos testresult = 1;
619 1.1 christos
620 1.2 christos end:
621 1.2 christos X509_free(crt1);
622 1.2 christos X509_free(crt2);
623 1.2 christos if (clientssl != NULL) {
624 1.2 christos SSL_shutdown(clientssl);
625 1.2 christos SSL_free(clientssl);
626 1.2 christos }
627 1.2 christos if (serverssl != NULL) {
628 1.2 christos SSL_shutdown(serverssl);
629 1.2 christos SSL_free(serverssl);
630 1.1 christos }
631 1.2 christos SSL_CTX_free(sctx);
632 1.2 christos SSL_CTX_free(cctx);
633 1.2 christos
634 1.2 christos OPENSSL_free(skey);
635 1.2 christos OPENSSL_free(leaf);
636 1.2 christos OPENSSL_free(int2);
637 1.2 christos OPENSSL_free(int1);
638 1.2 christos OPENSSL_free(root);
639 1.2 christos
640 1.2 christos return testresult;
641 1.2 christos }
642 1.2 christos
643 1.2 christos static int test_ssl_build_cert_chain(void)
644 1.2 christos {
645 1.2 christos int ret = 0;
646 1.2 christos SSL_CTX *ssl_ctx = NULL;
647 1.2 christos SSL *ssl = NULL;
648 1.2 christos char *skey = test_mk_file_path(certsdir, "leaf.key");
649 1.2 christos char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
650 1.1 christos
651 1.2 christos if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
652 1.2 christos goto end;
653 1.2 christos if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
654 1.2 christos goto end;
655 1.2 christos /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
656 1.2 christos if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
657 1.2 christos || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
658 1.2 christos || !TEST_int_eq(SSL_check_private_key(ssl), 1))
659 1.2 christos goto end;
660 1.2 christos if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
661 1.2 christos | SSL_BUILD_CHAIN_FLAG_CHECK)))
662 1.2 christos goto end;
663 1.2 christos ret = 1;
664 1.2 christos end:
665 1.2 christos SSL_free(ssl);
666 1.2 christos SSL_CTX_free(ssl_ctx);
667 1.2 christos OPENSSL_free(leaf_chain);
668 1.2 christos OPENSSL_free(skey);
669 1.2 christos return ret;
670 1.2 christos }
671 1.1 christos
672 1.2 christos static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
673 1.2 christos {
674 1.2 christos static const char pass[] = "testpass";
675 1.1 christos
676 1.2 christos if (!TEST_int_eq(size, PEM_BUFSIZE))
677 1.2 christos return -1;
678 1.1 christos
679 1.2 christos memcpy(buf, pass, sizeof(pass) - 1);
680 1.2 christos return sizeof(pass) - 1;
681 1.2 christos }
682 1.1 christos
683 1.2 christos static int test_ssl_ctx_build_cert_chain(void)
684 1.2 christos {
685 1.2 christos int ret = 0;
686 1.2 christos SSL_CTX *ctx = NULL;
687 1.2 christos char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
688 1.2 christos char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
689 1.2 christos
690 1.2 christos if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
691 1.2 christos goto end;
692 1.2 christos SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
693 1.2 christos /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
694 1.2 christos if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
695 1.2 christos || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
696 1.2 christos SSL_FILETYPE_PEM), 1)
697 1.2 christos || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
698 1.2 christos goto end;
699 1.2 christos if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
700 1.2 christos | SSL_BUILD_CHAIN_FLAG_CHECK)))
701 1.2 christos goto end;
702 1.2 christos ret = 1;
703 1.2 christos end:
704 1.2 christos SSL_CTX_free(ctx);
705 1.2 christos OPENSSL_free(leaf_chain);
706 1.2 christos OPENSSL_free(skey);
707 1.2 christos return ret;
708 1.1 christos }
709 1.1 christos
710 1.2 christos #ifndef OPENSSL_NO_TLS1_2
711 1.2 christos static int full_client_hello_callback(SSL *s, int *al, void *arg)
712 1.1 christos {
713 1.2 christos int *ctr = arg;
714 1.2 christos const unsigned char *p;
715 1.2 christos int *exts;
716 1.2 christos /* We only configure two ciphers, but the SCSV is added automatically. */
717 1.2 christos #ifdef OPENSSL_NO_EC
718 1.2 christos const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
719 1.2 christos #else
720 1.2 christos const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
721 1.2 christos 0x2c, 0x00, 0xff};
722 1.2 christos #endif
723 1.2 christos const int expected_extensions[] = {
724 1.2 christos #ifndef OPENSSL_NO_EC
725 1.2 christos 11, 10,
726 1.2 christos #endif
727 1.2 christos 35, 22, 23, 13};
728 1.1 christos size_t len;
729 1.1 christos
730 1.2 christos /* Make sure we can defer processing and get called back. */
731 1.2 christos if ((*ctr)++ == 0)
732 1.2 christos return SSL_CLIENT_HELLO_RETRY;
733 1.2 christos
734 1.2 christos len = SSL_client_hello_get0_ciphers(s, &p);
735 1.2 christos if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
736 1.2 christos || !TEST_size_t_eq(
737 1.2 christos SSL_client_hello_get0_compression_methods(s, &p), 1)
738 1.2 christos || !TEST_int_eq(*p, 0))
739 1.2 christos return SSL_CLIENT_HELLO_ERROR;
740 1.2 christos if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
741 1.2 christos return SSL_CLIENT_HELLO_ERROR;
742 1.2 christos if (len != OSSL_NELEM(expected_extensions) ||
743 1.2 christos memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
744 1.2 christos printf("ClientHello callback expected extensions mismatch\n");
745 1.2 christos OPENSSL_free(exts);
746 1.2 christos return SSL_CLIENT_HELLO_ERROR;
747 1.2 christos }
748 1.2 christos OPENSSL_free(exts);
749 1.2 christos return SSL_CLIENT_HELLO_SUCCESS;
750 1.2 christos }
751 1.2 christos
752 1.2 christos static int test_client_hello_cb(void)
753 1.2 christos {
754 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
755 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
756 1.2 christos int testctr = 0, testresult = 0;
757 1.1 christos
758 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
759 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
760 1.2 christos &sctx, &cctx, cert, privkey)))
761 1.2 christos goto end;
762 1.2 christos SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
763 1.2 christos
764 1.2 christos /* The gimpy cipher list we configure can't do TLS 1.3. */
765 1.2 christos SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
766 1.2 christos
767 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
768 1.2 christos "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
769 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
770 1.2 christos &clientssl, NULL, NULL))
771 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
772 1.2 christos SSL_ERROR_WANT_CLIENT_HELLO_CB))
773 1.2 christos /*
774 1.2 christos * Passing a -1 literal is a hack since
775 1.2 christos * the real value was lost.
776 1.2 christos * */
777 1.2 christos || !TEST_int_eq(SSL_get_error(serverssl, -1),
778 1.2 christos SSL_ERROR_WANT_CLIENT_HELLO_CB)
779 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
780 1.2 christos SSL_ERROR_NONE)))
781 1.2 christos goto end;
782 1.1 christos
783 1.2 christos testresult = 1;
784 1.1 christos
785 1.2 christos end:
786 1.2 christos SSL_free(serverssl);
787 1.2 christos SSL_free(clientssl);
788 1.2 christos SSL_CTX_free(sctx);
789 1.2 christos SSL_CTX_free(cctx);
790 1.1 christos
791 1.2 christos return testresult;
792 1.1 christos }
793 1.1 christos
794 1.2 christos static int test_no_ems(void)
795 1.1 christos {
796 1.1 christos SSL_CTX *cctx = NULL, *sctx = NULL;
797 1.1 christos SSL *clientssl = NULL, *serverssl = NULL;
798 1.1 christos int testresult = 0;
799 1.1 christos
800 1.2 christos if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
801 1.2 christos TLS1_VERSION, TLS1_2_VERSION,
802 1.2 christos &sctx, &cctx, cert, privkey)) {
803 1.1 christos printf("Unable to create SSL_CTX pair\n");
804 1.2 christos goto end;
805 1.1 christos }
806 1.1 christos
807 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
808 1.2 christos
809 1.2 christos if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
810 1.2 christos printf("Unable to create SSL objects\n");
811 1.1 christos goto end;
812 1.1 christos }
813 1.1 christos
814 1.2 christos if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
815 1.2 christos printf("Creating SSL connection failed\n");
816 1.1 christos goto end;
817 1.1 christos }
818 1.1 christos
819 1.2 christos if (SSL_get_extms_support(serverssl)) {
820 1.2 christos printf("Server reports Extended Master Secret support\n");
821 1.1 christos goto end;
822 1.1 christos }
823 1.1 christos
824 1.2 christos if (SSL_get_extms_support(clientssl)) {
825 1.2 christos printf("Client reports Extended Master Secret support\n");
826 1.1 christos goto end;
827 1.1 christos }
828 1.2 christos testresult = 1;
829 1.1 christos
830 1.2 christos end:
831 1.2 christos SSL_free(serverssl);
832 1.1 christos SSL_free(clientssl);
833 1.2 christos SSL_CTX_free(sctx);
834 1.2 christos SSL_CTX_free(cctx);
835 1.1 christos
836 1.2 christos return testresult;
837 1.2 christos }
838 1.1 christos
839 1.2 christos /*
840 1.2 christos * Very focused test to exercise a single case in the server-side state
841 1.2 christos * machine, when the ChangeCipherState message needs to actually change
842 1.2 christos * from one cipher to a different cipher (i.e., not changing from null
843 1.2 christos * encryption to real encryption).
844 1.2 christos */
845 1.2 christos static int test_ccs_change_cipher(void)
846 1.2 christos {
847 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
848 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
849 1.2 christos SSL_SESSION *sess = NULL, *sesspre, *sesspost;
850 1.2 christos int testresult = 0;
851 1.2 christos int i;
852 1.2 christos unsigned char buf;
853 1.2 christos size_t readbytes;
854 1.2 christos
855 1.2 christos /*
856 1.2 christos * Create a conection so we can resume and potentially (but not) use
857 1.2 christos * a different cipher in the second connection.
858 1.2 christos */
859 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
860 1.2 christos TLS_client_method(),
861 1.2 christos TLS1_VERSION, TLS1_2_VERSION,
862 1.2 christos &sctx, &cctx, cert, privkey))
863 1.2 christos || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
864 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
865 1.2 christos NULL, NULL))
866 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
867 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
868 1.2 christos SSL_ERROR_NONE))
869 1.2 christos || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
870 1.2 christos || !TEST_ptr(sess = SSL_get1_session(clientssl)))
871 1.1 christos goto end;
872 1.1 christos
873 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
874 1.2 christos serverssl = clientssl = NULL;
875 1.1 christos
876 1.2 christos /* Resume, preferring a different cipher. Our server will force the
877 1.2 christos * same cipher to be used as the initial handshake. */
878 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
879 1.2 christos NULL, NULL))
880 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess))
881 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
882 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
883 1.2 christos SSL_ERROR_NONE))
884 1.2 christos || !TEST_true(SSL_session_reused(clientssl))
885 1.2 christos || !TEST_true(SSL_session_reused(serverssl))
886 1.2 christos || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
887 1.2 christos || !TEST_ptr_eq(sesspre, sesspost)
888 1.2 christos || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
889 1.2 christos SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
890 1.1 christos goto end;
891 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
892 1.2 christos serverssl = clientssl = NULL;
893 1.1 christos
894 1.1 christos /*
895 1.2 christos * Now create a fresh connection and try to renegotiate a different
896 1.2 christos * cipher on it.
897 1.1 christos */
898 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
899 1.2 christos NULL, NULL))
900 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
901 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
902 1.2 christos SSL_ERROR_NONE))
903 1.2 christos || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
904 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
905 1.2 christos || !TEST_true(SSL_renegotiate(clientssl))
906 1.2 christos || !TEST_true(SSL_renegotiate_pending(clientssl)))
907 1.2 christos goto end;
908 1.2 christos /* Actually drive the renegotiation. */
909 1.2 christos for (i = 0; i < 3; i++) {
910 1.2 christos if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
911 1.2 christos if (!TEST_ulong_eq(readbytes, 0))
912 1.2 christos goto end;
913 1.2 christos } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
914 1.2 christos SSL_ERROR_WANT_READ)) {
915 1.2 christos goto end;
916 1.2 christos }
917 1.2 christos if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
918 1.2 christos if (!TEST_ulong_eq(readbytes, 0))
919 1.2 christos goto end;
920 1.2 christos } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
921 1.2 christos SSL_ERROR_WANT_READ)) {
922 1.2 christos goto end;
923 1.2 christos }
924 1.2 christos }
925 1.2 christos /* sesspre and sesspost should be different since the cipher changed. */
926 1.2 christos if (!TEST_false(SSL_renegotiate_pending(clientssl))
927 1.2 christos || !TEST_false(SSL_session_reused(clientssl))
928 1.2 christos || !TEST_false(SSL_session_reused(serverssl))
929 1.2 christos || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
930 1.2 christos || !TEST_ptr_ne(sesspre, sesspost)
931 1.2 christos || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
932 1.2 christos SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
933 1.1 christos goto end;
934 1.1 christos
935 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
936 1.2 christos serverssl = clientssl = NULL;
937 1.1 christos
938 1.2 christos testresult = 1;
939 1.1 christos
940 1.2 christos end:
941 1.1 christos SSL_free(serverssl);
942 1.1 christos SSL_free(clientssl);
943 1.2 christos SSL_CTX_free(sctx);
944 1.2 christos SSL_CTX_free(cctx);
945 1.2 christos SSL_SESSION_free(sess);
946 1.2 christos
947 1.2 christos return testresult;
948 1.2 christos }
949 1.2 christos #endif
950 1.1 christos
951 1.2 christos static int add_large_cert_chain(SSL_CTX *sctx)
952 1.2 christos {
953 1.2 christos BIO *certbio = NULL;
954 1.2 christos X509 *chaincert = NULL;
955 1.2 christos int certlen;
956 1.2 christos int ret = 0;
957 1.2 christos int i;
958 1.1 christos
959 1.2 christos if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
960 1.1 christos goto end;
961 1.1 christos
962 1.2 christos if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
963 1.1 christos goto end;
964 1.1 christos
965 1.2 christos if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
966 1.1 christos goto end;
967 1.2 christos BIO_free(certbio);
968 1.2 christos certbio = NULL;
969 1.2 christos
970 1.2 christos /*
971 1.2 christos * We assume the supplied certificate is big enough so that if we add
972 1.2 christos * NUM_EXTRA_CERTS it will make the overall message large enough. The
973 1.2 christos * default buffer size is requested to be 16k, but due to the way BUF_MEM
974 1.2 christos * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
975 1.2 christos * test we need to have a message larger than that.
976 1.2 christos */
977 1.2 christos certlen = i2d_X509(chaincert, NULL);
978 1.2 christos OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
979 1.2 christos (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
980 1.2 christos for (i = 0; i < NUM_EXTRA_CERTS; i++) {
981 1.2 christos if (!X509_up_ref(chaincert))
982 1.2 christos goto end;
983 1.2 christos if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
984 1.2 christos X509_free(chaincert);
985 1.2 christos goto end;
986 1.2 christos }
987 1.1 christos }
988 1.1 christos
989 1.2 christos ret = 1;
990 1.2 christos end:
991 1.2 christos BIO_free(certbio);
992 1.2 christos X509_free(chaincert);
993 1.2 christos return ret;
994 1.2 christos }
995 1.1 christos
996 1.2 christos static int execute_test_large_message(const SSL_METHOD *smeth,
997 1.2 christos const SSL_METHOD *cmeth,
998 1.2 christos int min_version, int max_version,
999 1.2 christos int read_ahead)
1000 1.2 christos {
1001 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1002 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1003 1.2 christos int testresult = 0;
1004 1.1 christos
1005 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
1006 1.2 christos max_version, &sctx, &cctx, cert,
1007 1.2 christos privkey)))
1008 1.1 christos goto end;
1009 1.2 christos
1010 1.2 christos #ifdef OPENSSL_NO_DTLS1_2
1011 1.2 christos if (smeth == DTLS_server_method()) {
1012 1.2 christos /*
1013 1.2 christos * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1014 1.2 christos * level 0
1015 1.2 christos */
1016 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1017 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1018 1.2 christos "DEFAULT:@SECLEVEL=0")))
1019 1.2 christos goto end;
1020 1.1 christos }
1021 1.2 christos #endif
1022 1.1 christos
1023 1.2 christos if (read_ahead) {
1024 1.2 christos /*
1025 1.2 christos * Test that read_ahead works correctly when dealing with large
1026 1.2 christos * records
1027 1.2 christos */
1028 1.2 christos SSL_CTX_set_read_ahead(cctx, 1);
1029 1.1 christos }
1030 1.2 christos
1031 1.2 christos if (!add_large_cert_chain(sctx))
1032 1.1 christos goto end;
1033 1.1 christos
1034 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1035 1.2 christos NULL, NULL))
1036 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
1037 1.2 christos SSL_ERROR_NONE)))
1038 1.1 christos goto end;
1039 1.1 christos
1040 1.2 christos /*
1041 1.2 christos * Calling SSL_clear() first is not required but this tests that SSL_clear()
1042 1.2 christos * doesn't leak.
1043 1.2 christos */
1044 1.2 christos if (!TEST_true(SSL_clear(serverssl)))
1045 1.1 christos goto end;
1046 1.1 christos
1047 1.1 christos testresult = 1;
1048 1.1 christos end:
1049 1.1 christos SSL_free(serverssl);
1050 1.1 christos SSL_free(clientssl);
1051 1.1 christos SSL_CTX_free(sctx);
1052 1.1 christos SSL_CTX_free(cctx);
1053 1.1 christos
1054 1.1 christos return testresult;
1055 1.1 christos }
1056 1.1 christos
1057 1.2 christos #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
1058 1.2 christos !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
1059 1.2 christos /* sock must be connected */
1060 1.2 christos static int ktls_chk_platform(int sock)
1061 1.1 christos {
1062 1.2 christos if (!ktls_enable(sock))
1063 1.2 christos return 0;
1064 1.2 christos return 1;
1065 1.1 christos }
1066 1.1 christos
1067 1.2 christos static int ping_pong_query(SSL *clientssl, SSL *serverssl)
1068 1.1 christos {
1069 1.2 christos static char count = 1;
1070 1.2 christos unsigned char cbuf[16000] = {0};
1071 1.2 christos unsigned char sbuf[16000];
1072 1.2 christos size_t err = 0;
1073 1.2 christos char crec_wseq_before[SEQ_NUM_SIZE];
1074 1.2 christos char crec_wseq_after[SEQ_NUM_SIZE];
1075 1.2 christos char crec_rseq_before[SEQ_NUM_SIZE];
1076 1.2 christos char crec_rseq_after[SEQ_NUM_SIZE];
1077 1.2 christos char srec_wseq_before[SEQ_NUM_SIZE];
1078 1.2 christos char srec_wseq_after[SEQ_NUM_SIZE];
1079 1.2 christos char srec_rseq_before[SEQ_NUM_SIZE];
1080 1.2 christos char srec_rseq_after[SEQ_NUM_SIZE];
1081 1.2 christos
1082 1.2 christos cbuf[0] = count++;
1083 1.2 christos memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1084 1.2 christos memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1085 1.2 christos memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1086 1.2 christos memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1087 1.1 christos
1088 1.2 christos if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
1089 1.2 christos goto end;
1090 1.1 christos
1091 1.2 christos while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
1092 1.2 christos if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
1093 1.2 christos goto end;
1094 1.2 christos }
1095 1.2 christos }
1096 1.1 christos
1097 1.2 christos if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
1098 1.2 christos goto end;
1099 1.1 christos
1100 1.2 christos while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
1101 1.2 christos if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
1102 1.2 christos goto end;
1103 1.2 christos }
1104 1.2 christos }
1105 1.1 christos
1106 1.2 christos memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1107 1.2 christos memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1108 1.2 christos memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1109 1.2 christos memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1110 1.1 christos
1111 1.2 christos /* verify the payload */
1112 1.2 christos if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1113 1.2 christos goto end;
1114 1.1 christos
1115 1.2 christos /*
1116 1.2 christos * If ktls is used then kernel sequences are used instead of
1117 1.2 christos * OpenSSL sequences
1118 1.2 christos */
1119 1.2 christos if (!BIO_get_ktls_send(clientssl->wbio)) {
1120 1.2 christos if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
1121 1.2 christos crec_wseq_after, SEQ_NUM_SIZE))
1122 1.2 christos goto end;
1123 1.2 christos } else {
1124 1.2 christos if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
1125 1.2 christos crec_wseq_after, SEQ_NUM_SIZE))
1126 1.2 christos goto end;
1127 1.1 christos }
1128 1.2 christos
1129 1.2 christos if (!BIO_get_ktls_send(serverssl->wbio)) {
1130 1.2 christos if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
1131 1.2 christos srec_wseq_after, SEQ_NUM_SIZE))
1132 1.2 christos goto end;
1133 1.1 christos } else {
1134 1.2 christos if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
1135 1.2 christos srec_wseq_after, SEQ_NUM_SIZE))
1136 1.2 christos goto end;
1137 1.1 christos }
1138 1.1 christos
1139 1.2 christos if (!BIO_get_ktls_recv(clientssl->wbio)) {
1140 1.2 christos if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
1141 1.2 christos crec_rseq_after, SEQ_NUM_SIZE))
1142 1.2 christos goto end;
1143 1.2 christos } else {
1144 1.2 christos if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
1145 1.2 christos crec_rseq_after, SEQ_NUM_SIZE))
1146 1.2 christos goto end;
1147 1.1 christos }
1148 1.1 christos
1149 1.2 christos if (!BIO_get_ktls_recv(serverssl->wbio)) {
1150 1.2 christos if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
1151 1.2 christos srec_rseq_after, SEQ_NUM_SIZE))
1152 1.2 christos goto end;
1153 1.2 christos } else {
1154 1.2 christos if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
1155 1.2 christos srec_rseq_after, SEQ_NUM_SIZE))
1156 1.2 christos goto end;
1157 1.1 christos }
1158 1.2 christos
1159 1.2 christos return 1;
1160 1.2 christos end:
1161 1.2 christos return 0;
1162 1.2 christos }
1163 1.2 christos
1164 1.2 christos static int execute_test_ktls(int cis_ktls, int sis_ktls,
1165 1.2 christos int tls_version, const char *cipher)
1166 1.2 christos {
1167 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1168 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1169 1.2 christos int ktls_used = 0, testresult = 0;
1170 1.2 christos int cfd = -1, sfd = -1;
1171 1.2 christos int rx_supported;
1172 1.2 christos
1173 1.2 christos if (!TEST_true(create_test_sockets(&cfd, &sfd)))
1174 1.1 christos goto end;
1175 1.1 christos
1176 1.2 christos /* Skip this test if the platform does not support ktls */
1177 1.2 christos if (!ktls_chk_platform(cfd)) {
1178 1.2 christos testresult = TEST_skip("Kernel does not support KTLS");
1179 1.1 christos goto end;
1180 1.1 christos }
1181 1.1 christos
1182 1.2 christos if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1183 1.2 christos testresult = TEST_skip("CHACHA is not supported in FIPS");
1184 1.1 christos goto end;
1185 1.1 christos }
1186 1.1 christos
1187 1.2 christos /* Create a session based on SHA-256 */
1188 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1189 1.2 christos TLS_client_method(),
1190 1.2 christos tls_version, tls_version,
1191 1.2 christos &sctx, &cctx, cert, privkey)))
1192 1.1 christos goto end;
1193 1.2 christos
1194 1.2 christos if (tls_version == TLS1_3_VERSION) {
1195 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1196 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1197 1.2 christos goto end;
1198 1.2 christos } else {
1199 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1200 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1201 1.2 christos goto end;
1202 1.1 christos }
1203 1.1 christos
1204 1.2 christos if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1205 1.2 christos &clientssl, sfd, cfd)))
1206 1.1 christos goto end;
1207 1.2 christos
1208 1.2 christos if (cis_ktls) {
1209 1.2 christos if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
1210 1.2 christos goto end;
1211 1.1 christos }
1212 1.1 christos
1213 1.2 christos if (sis_ktls) {
1214 1.2 christos if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1215 1.2 christos goto end;
1216 1.1 christos }
1217 1.1 christos
1218 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1219 1.1 christos goto end;
1220 1.1 christos
1221 1.1 christos /*
1222 1.2 christos * The running kernel may not support a given cipher suite
1223 1.2 christos * or direction, so just check that KTLS isn't used when it
1224 1.2 christos * isn't enabled.
1225 1.1 christos */
1226 1.2 christos if (!cis_ktls) {
1227 1.2 christos if (!TEST_false(BIO_get_ktls_send(clientssl->wbio)))
1228 1.2 christos goto end;
1229 1.2 christos } else {
1230 1.2 christos if (BIO_get_ktls_send(clientssl->wbio))
1231 1.2 christos ktls_used = 1;
1232 1.1 christos }
1233 1.1 christos
1234 1.2 christos if (!sis_ktls) {
1235 1.2 christos if (!TEST_false(BIO_get_ktls_send(serverssl->wbio)))
1236 1.2 christos goto end;
1237 1.2 christos } else {
1238 1.2 christos if (BIO_get_ktls_send(serverssl->wbio))
1239 1.2 christos ktls_used = 1;
1240 1.1 christos }
1241 1.1 christos
1242 1.2 christos #if defined(OPENSSL_NO_KTLS_RX)
1243 1.2 christos rx_supported = 0;
1244 1.2 christos #else
1245 1.2 christos rx_supported = (tls_version != TLS1_3_VERSION);
1246 1.2 christos #endif
1247 1.2 christos if (!cis_ktls || !rx_supported) {
1248 1.2 christos if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio)))
1249 1.2 christos goto end;
1250 1.2 christos } else {
1251 1.2 christos if (BIO_get_ktls_send(clientssl->rbio))
1252 1.2 christos ktls_used = 1;
1253 1.1 christos }
1254 1.1 christos
1255 1.2 christos if (!sis_ktls || !rx_supported) {
1256 1.2 christos if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio)))
1257 1.1 christos goto end;
1258 1.2 christos } else {
1259 1.2 christos if (BIO_get_ktls_send(serverssl->rbio))
1260 1.2 christos ktls_used = 1;
1261 1.1 christos }
1262 1.1 christos
1263 1.2 christos if ((cis_ktls || sis_ktls) && !ktls_used) {
1264 1.2 christos testresult = TEST_skip("KTLS not supported for %s cipher %s",
1265 1.2 christos tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1266 1.2 christos "TLS 1.2", cipher);
1267 1.1 christos goto end;
1268 1.1 christos }
1269 1.1 christos
1270 1.2 christos if (!TEST_true(ping_pong_query(clientssl, serverssl)))
1271 1.1 christos goto end;
1272 1.2 christos
1273 1.2 christos testresult = 1;
1274 1.2 christos end:
1275 1.2 christos if (clientssl) {
1276 1.2 christos SSL_shutdown(clientssl);
1277 1.2 christos SSL_free(clientssl);
1278 1.2 christos }
1279 1.2 christos if (serverssl) {
1280 1.2 christos SSL_shutdown(serverssl);
1281 1.2 christos SSL_free(serverssl);
1282 1.1 christos }
1283 1.2 christos SSL_CTX_free(sctx);
1284 1.2 christos SSL_CTX_free(cctx);
1285 1.2 christos serverssl = clientssl = NULL;
1286 1.2 christos if (cfd != -1)
1287 1.2 christos close(cfd);
1288 1.2 christos if (sfd != -1)
1289 1.2 christos close(sfd);
1290 1.2 christos return testresult;
1291 1.2 christos }
1292 1.1 christos
1293 1.2 christos #define SENDFILE_SZ (16 * 4096)
1294 1.2 christos #define SENDFILE_CHUNK (4 * 4096)
1295 1.2 christos #define min(a,b) ((a) > (b) ? (b) : (a))
1296 1.2 christos
1297 1.2 christos static int execute_test_ktls_sendfile(int tls_version, const char *cipher)
1298 1.2 christos {
1299 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1300 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1301 1.2 christos unsigned char *buf, *buf_dst;
1302 1.2 christos BIO *out = NULL, *in = NULL;
1303 1.2 christos int cfd = -1, sfd = -1, ffd, err;
1304 1.2 christos ssize_t chunk_size = 0;
1305 1.2 christos off_t chunk_off = 0;
1306 1.2 christos int testresult = 0;
1307 1.2 christos FILE *ffdp;
1308 1.1 christos
1309 1.2 christos buf = OPENSSL_zalloc(SENDFILE_SZ);
1310 1.2 christos buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
1311 1.2 christos if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
1312 1.2 christos || !TEST_true(create_test_sockets(&cfd, &sfd)))
1313 1.1 christos goto end;
1314 1.1 christos
1315 1.2 christos /* Skip this test if the platform does not support ktls */
1316 1.2 christos if (!ktls_chk_platform(sfd)) {
1317 1.2 christos testresult = TEST_skip("Kernel does not support KTLS");
1318 1.1 christos goto end;
1319 1.1 christos }
1320 1.1 christos
1321 1.2 christos if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1322 1.2 christos testresult = TEST_skip("CHACHA is not supported in FIPS");
1323 1.1 christos goto end;
1324 1.1 christos }
1325 1.1 christos
1326 1.2 christos /* Create a session based on SHA-256 */
1327 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1328 1.2 christos TLS_client_method(),
1329 1.2 christos tls_version, tls_version,
1330 1.2 christos &sctx, &cctx, cert, privkey)))
1331 1.2 christos goto end;
1332 1.2 christos
1333 1.2 christos if (tls_version == TLS1_3_VERSION) {
1334 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1335 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1336 1.2 christos goto end;
1337 1.2 christos } else {
1338 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1339 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1340 1.2 christos goto end;
1341 1.2 christos }
1342 1.2 christos
1343 1.2 christos if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1344 1.2 christos &clientssl, sfd, cfd)))
1345 1.2 christos goto end;
1346 1.1 christos
1347 1.2 christos if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1348 1.2 christos goto end;
1349 1.2 christos
1350 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1351 1.2 christos SSL_ERROR_NONE)))
1352 1.2 christos goto end;
1353 1.2 christos
1354 1.2 christos if (!BIO_get_ktls_send(serverssl->wbio)) {
1355 1.2 christos testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
1356 1.2 christos tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1357 1.2 christos "TLS 1.2", cipher);
1358 1.1 christos goto end;
1359 1.1 christos }
1360 1.1 christos
1361 1.2 christos if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
1362 1.2 christos goto end;
1363 1.2 christos
1364 1.2 christos out = BIO_new_file(tmpfilename, "wb");
1365 1.2 christos if (!TEST_ptr(out))
1366 1.2 christos goto end;
1367 1.2 christos
1368 1.2 christos if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
1369 1.1 christos goto end;
1370 1.2 christos
1371 1.2 christos BIO_free(out);
1372 1.2 christos out = NULL;
1373 1.2 christos in = BIO_new_file(tmpfilename, "rb");
1374 1.2 christos BIO_get_fp(in, &ffdp);
1375 1.2 christos ffd = fileno(ffdp);
1376 1.2 christos
1377 1.2 christos while (chunk_off < SENDFILE_SZ) {
1378 1.2 christos chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
1379 1.2 christos while ((err = SSL_sendfile(serverssl,
1380 1.2 christos ffd,
1381 1.2 christos chunk_off,
1382 1.2 christos chunk_size,
1383 1.2 christos 0)) != chunk_size) {
1384 1.2 christos if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
1385 1.2 christos goto end;
1386 1.2 christos }
1387 1.2 christos while ((err = SSL_read(clientssl,
1388 1.2 christos buf_dst + chunk_off,
1389 1.2 christos chunk_size)) != chunk_size) {
1390 1.2 christos if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
1391 1.2 christos goto end;
1392 1.2 christos }
1393 1.2 christos
1394 1.2 christos /* verify the payload */
1395 1.2 christos if (!TEST_mem_eq(buf_dst + chunk_off,
1396 1.2 christos chunk_size,
1397 1.2 christos buf + chunk_off,
1398 1.2 christos chunk_size))
1399 1.2 christos goto end;
1400 1.2 christos
1401 1.2 christos chunk_off += chunk_size;
1402 1.1 christos }
1403 1.1 christos
1404 1.1 christos testresult = 1;
1405 1.2 christos end:
1406 1.2 christos if (clientssl) {
1407 1.2 christos SSL_shutdown(clientssl);
1408 1.2 christos SSL_free(clientssl);
1409 1.2 christos }
1410 1.2 christos if (serverssl) {
1411 1.2 christos SSL_shutdown(serverssl);
1412 1.2 christos SSL_free(serverssl);
1413 1.2 christos }
1414 1.1 christos SSL_CTX_free(sctx);
1415 1.1 christos SSL_CTX_free(cctx);
1416 1.2 christos serverssl = clientssl = NULL;
1417 1.2 christos BIO_free(out);
1418 1.2 christos BIO_free(in);
1419 1.2 christos if (cfd != -1)
1420 1.2 christos close(cfd);
1421 1.2 christos if (sfd != -1)
1422 1.2 christos close(sfd);
1423 1.2 christos OPENSSL_free(buf);
1424 1.2 christos OPENSSL_free(buf_dst);
1425 1.1 christos return testresult;
1426 1.1 christos }
1427 1.1 christos
1428 1.2 christos static struct ktls_test_cipher {
1429 1.2 christos int tls_version;
1430 1.2 christos const char *cipher;
1431 1.2 christos } ktls_test_ciphers[] = {
1432 1.2 christos # if !defined(OPENSSL_NO_TLS1_2)
1433 1.2 christos # ifdef OPENSSL_KTLS_AES_GCM_128
1434 1.2 christos { TLS1_2_VERSION, "AES128-GCM-SHA256" },
1435 1.2 christos # endif
1436 1.2 christos # ifdef OPENSSL_KTLS_AES_CCM_128
1437 1.2 christos { TLS1_2_VERSION, "AES128-CCM"},
1438 1.2 christos # endif
1439 1.2 christos # ifdef OPENSSL_KTLS_AES_GCM_256
1440 1.2 christos { TLS1_2_VERSION, "AES256-GCM-SHA384"},
1441 1.2 christos # endif
1442 1.2 christos # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1443 1.2 christos # ifndef OPENSSL_NO_EC
1444 1.2 christos { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
1445 1.2 christos # endif
1446 1.2 christos # endif
1447 1.2 christos # endif
1448 1.2 christos # if !defined(OSSL_NO_USABLE_TLS1_3)
1449 1.2 christos # ifdef OPENSSL_KTLS_AES_GCM_128
1450 1.2 christos { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
1451 1.2 christos # endif
1452 1.2 christos # ifdef OPENSSL_KTLS_AES_CCM_128
1453 1.2 christos { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
1454 1.2 christos # endif
1455 1.2 christos # ifdef OPENSSL_KTLS_AES_GCM_256
1456 1.2 christos { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
1457 1.2 christos # endif
1458 1.2 christos # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1459 1.2 christos { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
1460 1.2 christos # endif
1461 1.2 christos # endif
1462 1.2 christos };
1463 1.2 christos
1464 1.2 christos #define NUM_KTLS_TEST_CIPHERS \
1465 1.2 christos (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
1466 1.2 christos
1467 1.2 christos static int test_ktls(int test)
1468 1.1 christos {
1469 1.2 christos struct ktls_test_cipher *cipher;
1470 1.2 christos int cis_ktls, sis_ktls;
1471 1.1 christos
1472 1.2 christos OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
1473 1.2 christos cipher = &ktls_test_ciphers[test / 4];
1474 1.1 christos
1475 1.2 christos cis_ktls = (test & 1) != 0;
1476 1.2 christos sis_ktls = (test & 2) != 0;
1477 1.2 christos
1478 1.2 christos return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
1479 1.2 christos cipher->cipher);
1480 1.1 christos }
1481 1.1 christos
1482 1.2 christos static int test_ktls_sendfile(int tst)
1483 1.1 christos {
1484 1.2 christos struct ktls_test_cipher *cipher;
1485 1.1 christos
1486 1.2 christos OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
1487 1.2 christos cipher = &ktls_test_ciphers[tst];
1488 1.2 christos
1489 1.2 christos return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher);
1490 1.2 christos }
1491 1.2 christos #endif
1492 1.1 christos
1493 1.2 christos static int test_large_message_tls(void)
1494 1.2 christos {
1495 1.2 christos return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1496 1.2 christos TLS1_VERSION, 0, 0);
1497 1.1 christos }
1498 1.1 christos
1499 1.2 christos static int test_large_message_tls_read_ahead(void)
1500 1.1 christos {
1501 1.2 christos return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1502 1.2 christos TLS1_VERSION, 0, 1);
1503 1.2 christos }
1504 1.1 christos
1505 1.2 christos #ifndef OPENSSL_NO_DTLS
1506 1.2 christos static int test_large_message_dtls(void)
1507 1.2 christos {
1508 1.2 christos # ifdef OPENSSL_NO_DTLS1_2
1509 1.2 christos /* Not supported in the FIPS provider */
1510 1.2 christos if (is_fips)
1511 1.2 christos return 1;
1512 1.2 christos # endif
1513 1.2 christos /*
1514 1.2 christos * read_ahead is not relevant to DTLS because DTLS always acts as if
1515 1.2 christos * read_ahead is set.
1516 1.2 christos */
1517 1.2 christos return execute_test_large_message(DTLS_server_method(),
1518 1.2 christos DTLS_client_method(),
1519 1.2 christos DTLS1_VERSION, 0, 0);
1520 1.1 christos }
1521 1.2 christos #endif
1522 1.2 christos
1523 1.2 christos /*
1524 1.2 christos * Test we can successfully send the maximum amount of application data. We
1525 1.2 christos * test each protocol version individually, each with and without EtM enabled.
1526 1.2 christos * TLSv1.3 doesn't use EtM so technically it is redundant to test both but it is
1527 1.2 christos * simpler this way. We also test all combinations with and without the
1528 1.2 christos * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS option which affects the size of the
1529 1.2 christos * underlying buffer.
1530 1.2 christos */
1531 1.2 christos static int test_large_app_data(int tst)
1532 1.2 christos {
1533 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1534 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1535 1.2 christos int testresult = 0, prot;
1536 1.2 christos unsigned char *msg, *buf = NULL;
1537 1.2 christos size_t written, readbytes;
1538 1.2 christos const SSL_METHOD *smeth = TLS_server_method();
1539 1.2 christos const SSL_METHOD *cmeth = TLS_client_method();
1540 1.2 christos
1541 1.2 christos switch (tst >> 2) {
1542 1.2 christos case 0:
1543 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
1544 1.2 christos prot = TLS1_3_VERSION;
1545 1.2 christos break;
1546 1.2 christos #else
1547 1.2 christos return 1;
1548 1.2 christos #endif
1549 1.2 christos
1550 1.2 christos case 1:
1551 1.2 christos #ifndef OPENSSL_NO_TLS1_2
1552 1.2 christos prot = TLS1_2_VERSION;
1553 1.2 christos break;
1554 1.2 christos #else
1555 1.2 christos return 1;
1556 1.2 christos #endif
1557 1.1 christos
1558 1.2 christos case 2:
1559 1.2 christos #ifndef OPENSSL_NO_TLS1_1
1560 1.2 christos prot = TLS1_1_VERSION;
1561 1.2 christos break;
1562 1.2 christos #else
1563 1.2 christos return 1;
1564 1.2 christos #endif
1565 1.1 christos
1566 1.2 christos case 3:
1567 1.2 christos #ifndef OPENSSL_NO_TLS1
1568 1.2 christos prot = TLS1_VERSION;
1569 1.2 christos break;
1570 1.2 christos #else
1571 1.2 christos return 1;
1572 1.2 christos #endif
1573 1.1 christos
1574 1.2 christos case 4:
1575 1.2 christos #ifndef OPENSSL_NO_SSL3
1576 1.2 christos prot = SSL3_VERSION;
1577 1.1 christos break;
1578 1.2 christos #else
1579 1.2 christos return 1;
1580 1.2 christos #endif
1581 1.2 christos
1582 1.2 christos case 5:
1583 1.2 christos #ifndef OPENSSL_NO_DTLS1_2
1584 1.2 christos prot = DTLS1_2_VERSION;
1585 1.2 christos smeth = DTLS_server_method();
1586 1.2 christos cmeth = DTLS_client_method();
1587 1.1 christos break;
1588 1.2 christos #else
1589 1.2 christos return 1;
1590 1.2 christos #endif
1591 1.2 christos
1592 1.2 christos case 6:
1593 1.2 christos #ifndef OPENSSL_NO_DTLS1
1594 1.2 christos prot = DTLS1_VERSION;
1595 1.2 christos smeth = DTLS_server_method();
1596 1.2 christos cmeth = DTLS_client_method();
1597 1.1 christos break;
1598 1.2 christos #else
1599 1.2 christos return 1;
1600 1.2 christos #endif
1601 1.2 christos
1602 1.2 christos default:
1603 1.2 christos /* Shouldn't happen */
1604 1.2 christos return 0;
1605 1.1 christos }
1606 1.1 christos
1607 1.2 christos if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips)
1608 1.2 christos return 1;
1609 1.2 christos
1610 1.2 christos /* Maximal sized message of zeros */
1611 1.2 christos msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH);
1612 1.2 christos if (!TEST_ptr(msg))
1613 1.2 christos goto end;
1614 1.2 christos
1615 1.2 christos buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1);
1616 1.2 christos if (!TEST_ptr(buf))
1617 1.2 christos goto end;
1618 1.2 christos /* Set whole buffer to all bits set */
1619 1.2 christos memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1);
1620 1.1 christos
1621 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot,
1622 1.2 christos &sctx, &cctx, cert, privkey)))
1623 1.1 christos goto end;
1624 1.1 christos
1625 1.2 christos if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) {
1626 1.2 christos /* Older protocol versions need SECLEVEL=0 due to SHA1 usage */
1627 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))
1628 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(sctx,
1629 1.2 christos "DEFAULT:@SECLEVEL=0")))
1630 1.1 christos goto end;
1631 1.1 christos }
1632 1.1 christos
1633 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1634 1.2 christos &clientssl, NULL, NULL)))
1635 1.2 christos goto end;
1636 1.1 christos
1637 1.2 christos if ((tst & 1) != 0) {
1638 1.2 christos /* Setting this option gives us a minimally sized underlying buffer */
1639 1.2 christos if (!TEST_true(SSL_set_options(serverssl,
1640 1.2 christos SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
1641 1.2 christos || !TEST_true(SSL_set_options(clientssl,
1642 1.2 christos SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)))
1643 1.1 christos goto end;
1644 1.1 christos }
1645 1.1 christos
1646 1.2 christos if ((tst & 2) != 0) {
1647 1.2 christos /*
1648 1.2 christos * Setting this option means the MAC is added before encryption
1649 1.2 christos * giving us a larger record for the encryption process
1650 1.2 christos */
1651 1.2 christos if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC))
1652 1.2 christos || !TEST_true(SSL_set_options(clientssl,
1653 1.2 christos SSL_OP_NO_ENCRYPT_THEN_MAC)))
1654 1.1 christos goto end;
1655 1.1 christos }
1656 1.1 christos
1657 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1658 1.2 christos goto end;
1659 1.1 christos
1660 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH,
1661 1.2 christos &written))
1662 1.2 christos || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH))
1663 1.2 christos goto end;
1664 1.1 christos
1665 1.2 christos /* We provide a buffer slightly larger than what we are actually expecting */
1666 1.2 christos if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1,
1667 1.2 christos &readbytes)))
1668 1.2 christos goto end;
1669 1.1 christos
1670 1.2 christos if (!TEST_mem_eq(msg, written, buf, readbytes))
1671 1.2 christos goto end;
1672 1.1 christos
1673 1.1 christos testresult = 1;
1674 1.2 christos end:
1675 1.2 christos OPENSSL_free(msg);
1676 1.2 christos OPENSSL_free(buf);
1677 1.2 christos SSL_free(serverssl);
1678 1.2 christos SSL_free(clientssl);
1679 1.2 christos SSL_CTX_free(sctx);
1680 1.2 christos SSL_CTX_free(cctx);
1681 1.1 christos return testresult;
1682 1.1 christos }
1683 1.1 christos
1684 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) \
1685 1.2 christos || !defined(OPENSSL_NO_DTLS)
1686 1.2 christos static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
1687 1.2 christos const SSL_METHOD *cmeth,
1688 1.2 christos int min_version, int max_version)
1689 1.2 christos {
1690 1.2 christos size_t i;
1691 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1692 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1693 1.2 christos int testresult = 0;
1694 1.2 christos SSL3_RECORD *rr;
1695 1.2 christos void *zbuf;
1696 1.1 christos
1697 1.2 christos static unsigned char cbuf[16000];
1698 1.2 christos static unsigned char sbuf[16000];
1699 1.1 christos
1700 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx,
1701 1.2 christos smeth, cmeth,
1702 1.2 christos min_version, max_version,
1703 1.2 christos &sctx, &cctx, cert,
1704 1.2 christos privkey)))
1705 1.2 christos goto end;
1706 1.2 christos
1707 1.2 christos # ifdef OPENSSL_NO_DTLS1_2
1708 1.2 christos if (smeth == DTLS_server_method()) {
1709 1.2 christos /* Not supported in the FIPS provider */
1710 1.2 christos if (is_fips) {
1711 1.2 christos testresult = 1;
1712 1.2 christos goto end;
1713 1.2 christos };
1714 1.2 christos /*
1715 1.2 christos * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1716 1.2 christos * level 0
1717 1.2 christos */
1718 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1719 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1720 1.2 christos "DEFAULT:@SECLEVEL=0")))
1721 1.2 christos goto end;
1722 1.2 christos }
1723 1.2 christos # endif
1724 1.1 christos
1725 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1726 1.2 christos NULL, NULL)))
1727 1.2 christos goto end;
1728 1.1 christos
1729 1.2 christos if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
1730 1.2 christos goto end;
1731 1.1 christos
1732 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1733 1.2 christos SSL_ERROR_NONE)))
1734 1.2 christos goto end;
1735 1.1 christos
1736 1.2 christos for (i = 0; i < sizeof(cbuf); i++) {
1737 1.2 christos cbuf[i] = i & 0xff;
1738 1.1 christos }
1739 1.1 christos
1740 1.2 christos if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
1741 1.1 christos goto end;
1742 1.1 christos
1743 1.2 christos if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1744 1.2 christos goto end;
1745 1.1 christos
1746 1.2 christos if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1747 1.1 christos goto end;
1748 1.1 christos
1749 1.2 christos /*
1750 1.2 christos * Since we called SSL_peek(), we know the data in the record
1751 1.2 christos * layer is a plaintext record. We can gather the pointer to check
1752 1.2 christos * for zeroization after SSL_read().
1753 1.2 christos */
1754 1.2 christos rr = serverssl->rlayer.rrec;
1755 1.2 christos zbuf = &rr->data[rr->off];
1756 1.2 christos if (!TEST_int_eq(rr->length, sizeof(cbuf)))
1757 1.2 christos goto end;
1758 1.1 christos
1759 1.1 christos /*
1760 1.2 christos * After SSL_peek() the plaintext must still be stored in the
1761 1.2 christos * record.
1762 1.1 christos */
1763 1.2 christos if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1764 1.2 christos goto end;
1765 1.2 christos
1766 1.2 christos memset(sbuf, 0, sizeof(sbuf));
1767 1.2 christos if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1768 1.2 christos goto end;
1769 1.1 christos
1770 1.2 christos if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
1771 1.2 christos goto end;
1772 1.1 christos
1773 1.2 christos /* Check if rbuf is cleansed */
1774 1.2 christos memset(cbuf, 0, sizeof(cbuf));
1775 1.2 christos if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1776 1.2 christos goto end;
1777 1.1 christos
1778 1.1 christos testresult = 1;
1779 1.1 christos end:
1780 1.2 christos SSL_free(serverssl);
1781 1.2 christos SSL_free(clientssl);
1782 1.2 christos SSL_CTX_free(sctx);
1783 1.2 christos SSL_CTX_free(cctx);
1784 1.1 christos
1785 1.1 christos return testresult;
1786 1.1 christos }
1787 1.2 christos #endif /*
1788 1.2 christos * !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
1789 1.2 christos * || !defined(OPENSSL_NO_DTLS)
1790 1.2 christos */
1791 1.1 christos
1792 1.2 christos static int test_cleanse_plaintext(void)
1793 1.1 christos {
1794 1.2 christos #if !defined(OPENSSL_NO_TLS1_2)
1795 1.2 christos if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1796 1.2 christos TLS_client_method(),
1797 1.2 christos TLS1_2_VERSION,
1798 1.2 christos TLS1_2_VERSION)))
1799 1.2 christos return 0;
1800 1.1 christos
1801 1.2 christos #endif
1802 1.1 christos
1803 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3)
1804 1.2 christos if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1805 1.2 christos TLS_client_method(),
1806 1.2 christos TLS1_3_VERSION,
1807 1.2 christos TLS1_3_VERSION)))
1808 1.2 christos return 0;
1809 1.2 christos #endif
1810 1.1 christos
1811 1.2 christos #if !defined(OPENSSL_NO_DTLS)
1812 1.1 christos
1813 1.2 christos if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
1814 1.2 christos DTLS_client_method(),
1815 1.2 christos DTLS1_VERSION,
1816 1.2 christos 0)))
1817 1.2 christos return 0;
1818 1.2 christos #endif
1819 1.2 christos return 1;
1820 1.1 christos }
1821 1.1 christos
1822 1.2 christos #ifndef OPENSSL_NO_OCSP
1823 1.2 christos static int ocsp_server_cb(SSL *s, void *arg)
1824 1.1 christos {
1825 1.2 christos int *argi = (int *)arg;
1826 1.2 christos unsigned char *copy = NULL;
1827 1.2 christos STACK_OF(OCSP_RESPID) *ids = NULL;
1828 1.2 christos OCSP_RESPID *id = NULL;
1829 1.1 christos
1830 1.2 christos if (*argi == 2) {
1831 1.2 christos /* In this test we are expecting exactly 1 OCSP_RESPID */
1832 1.2 christos SSL_get_tlsext_status_ids(s, &ids);
1833 1.2 christos if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
1834 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
1835 1.1 christos
1836 1.2 christos id = sk_OCSP_RESPID_value(ids, 0);
1837 1.2 christos if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
1838 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
1839 1.2 christos } else if (*argi != 1) {
1840 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
1841 1.2 christos }
1842 1.1 christos
1843 1.2 christos if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
1844 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
1845 1.1 christos
1846 1.2 christos if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
1847 1.2 christos sizeof(orespder)))) {
1848 1.2 christos OPENSSL_free(copy);
1849 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
1850 1.2 christos }
1851 1.2 christos ocsp_server_called = 1;
1852 1.2 christos return SSL_TLSEXT_ERR_OK;
1853 1.1 christos }
1854 1.1 christos
1855 1.2 christos static int ocsp_client_cb(SSL *s, void *arg)
1856 1.2 christos {
1857 1.2 christos int *argi = (int *)arg;
1858 1.2 christos const unsigned char *respderin;
1859 1.2 christos size_t len;
1860 1.2 christos
1861 1.2 christos if (*argi != 1 && *argi != 2)
1862 1.2 christos return 0;
1863 1.2 christos
1864 1.2 christos len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
1865 1.2 christos if (!TEST_mem_eq(orespder, len, respderin, len))
1866 1.2 christos return 0;
1867 1.2 christos
1868 1.2 christos ocsp_client_called = 1;
1869 1.2 christos return 1;
1870 1.2 christos }
1871 1.2 christos
1872 1.2 christos static int test_tlsext_status_type(void)
1873 1.2 christos {
1874 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
1875 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
1876 1.2 christos int testresult = 0;
1877 1.2 christos STACK_OF(OCSP_RESPID) *ids = NULL;
1878 1.2 christos OCSP_RESPID *id = NULL;
1879 1.2 christos BIO *certbio = NULL;
1880 1.2 christos
1881 1.2 christos if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
1882 1.2 christos TLS1_VERSION, 0,
1883 1.2 christos &sctx, &cctx, cert, privkey))
1884 1.2 christos return 0;
1885 1.2 christos
1886 1.2 christos if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
1887 1.2 christos goto end;
1888 1.2 christos
1889 1.2 christos /* First just do various checks getting and setting tlsext_status_type */
1890 1.2 christos
1891 1.2 christos clientssl = SSL_new(cctx);
1892 1.2 christos if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
1893 1.2 christos || !TEST_true(SSL_set_tlsext_status_type(clientssl,
1894 1.2 christos TLSEXT_STATUSTYPE_ocsp))
1895 1.2 christos || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
1896 1.2 christos TLSEXT_STATUSTYPE_ocsp))
1897 1.2 christos goto end;
1898 1.2 christos
1899 1.2 christos SSL_free(clientssl);
1900 1.2 christos clientssl = NULL;
1901 1.2 christos
1902 1.2 christos if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
1903 1.2 christos || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
1904 1.2 christos goto end;
1905 1.2 christos
1906 1.2 christos clientssl = SSL_new(cctx);
1907 1.2 christos if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
1908 1.2 christos goto end;
1909 1.2 christos SSL_free(clientssl);
1910 1.2 christos clientssl = NULL;
1911 1.2 christos
1912 1.2 christos /*
1913 1.2 christos * Now actually do a handshake and check OCSP information is exchanged and
1914 1.2 christos * the callbacks get called
1915 1.2 christos */
1916 1.2 christos SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
1917 1.2 christos SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
1918 1.2 christos SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
1919 1.2 christos SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
1920 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1921 1.2 christos &clientssl, NULL, NULL))
1922 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
1923 1.2 christos SSL_ERROR_NONE))
1924 1.2 christos || !TEST_true(ocsp_client_called)
1925 1.2 christos || !TEST_true(ocsp_server_called))
1926 1.2 christos goto end;
1927 1.2 christos SSL_free(serverssl);
1928 1.2 christos SSL_free(clientssl);
1929 1.2 christos serverssl = NULL;
1930 1.2 christos clientssl = NULL;
1931 1.2 christos
1932 1.2 christos /* Try again but this time force the server side callback to fail */
1933 1.2 christos ocsp_client_called = 0;
1934 1.2 christos ocsp_server_called = 0;
1935 1.2 christos cdummyarg = 0;
1936 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1937 1.2 christos &clientssl, NULL, NULL))
1938 1.2 christos /* This should fail because the callback will fail */
1939 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
1940 1.2 christos SSL_ERROR_NONE))
1941 1.2 christos || !TEST_false(ocsp_client_called)
1942 1.2 christos || !TEST_false(ocsp_server_called))
1943 1.2 christos goto end;
1944 1.2 christos SSL_free(serverssl);
1945 1.2 christos SSL_free(clientssl);
1946 1.2 christos serverssl = NULL;
1947 1.2 christos clientssl = NULL;
1948 1.2 christos
1949 1.2 christos /*
1950 1.2 christos * This time we'll get the client to send an OCSP_RESPID that it will
1951 1.2 christos * accept.
1952 1.2 christos */
1953 1.2 christos ocsp_client_called = 0;
1954 1.2 christos ocsp_server_called = 0;
1955 1.2 christos cdummyarg = 2;
1956 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1957 1.2 christos &clientssl, NULL, NULL)))
1958 1.2 christos goto end;
1959 1.2 christos
1960 1.2 christos /*
1961 1.2 christos * We'll just use any old cert for this test - it doesn't have to be an OCSP
1962 1.2 christos * specific one. We'll use the server cert.
1963 1.2 christos */
1964 1.2 christos if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
1965 1.2 christos || !TEST_ptr(id = OCSP_RESPID_new())
1966 1.2 christos || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
1967 1.2 christos || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
1968 1.2 christos || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
1969 1.2 christos || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
1970 1.2 christos || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
1971 1.2 christos goto end;
1972 1.2 christos id = NULL;
1973 1.2 christos SSL_set_tlsext_status_ids(clientssl, ids);
1974 1.2 christos /* Control has been transferred */
1975 1.2 christos ids = NULL;
1976 1.2 christos
1977 1.2 christos BIO_free(certbio);
1978 1.2 christos certbio = NULL;
1979 1.2 christos
1980 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1981 1.2 christos SSL_ERROR_NONE))
1982 1.2 christos || !TEST_true(ocsp_client_called)
1983 1.2 christos || !TEST_true(ocsp_server_called))
1984 1.2 christos goto end;
1985 1.2 christos
1986 1.2 christos testresult = 1;
1987 1.2 christos
1988 1.2 christos end:
1989 1.2 christos SSL_free(serverssl);
1990 1.2 christos SSL_free(clientssl);
1991 1.2 christos SSL_CTX_free(sctx);
1992 1.2 christos SSL_CTX_free(cctx);
1993 1.2 christos sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
1994 1.2 christos OCSP_RESPID_free(id);
1995 1.2 christos BIO_free(certbio);
1996 1.2 christos X509_free(ocspcert);
1997 1.2 christos ocspcert = NULL;
1998 1.2 christos
1999 1.2 christos return testresult;
2000 1.2 christos }
2001 1.2 christos #endif
2002 1.2 christos
2003 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
2004 1.2 christos static int new_called, remove_called, get_called;
2005 1.2 christos
2006 1.2 christos static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
2007 1.2 christos {
2008 1.2 christos new_called++;
2009 1.2 christos /*
2010 1.2 christos * sess has been up-refed for us, but we don't actually need it so free it
2011 1.2 christos * immediately.
2012 1.2 christos */
2013 1.2 christos SSL_SESSION_free(sess);
2014 1.2 christos return 1;
2015 1.2 christos }
2016 1.2 christos
2017 1.2 christos static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
2018 1.2 christos {
2019 1.2 christos remove_called++;
2020 1.2 christos }
2021 1.2 christos
2022 1.2 christos static SSL_SESSION *get_sess_val = NULL;
2023 1.2 christos
2024 1.2 christos static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
2025 1.2 christos int *copy)
2026 1.2 christos {
2027 1.2 christos get_called++;
2028 1.2 christos *copy = 1;
2029 1.2 christos return get_sess_val;
2030 1.2 christos }
2031 1.2 christos
2032 1.2 christos static int execute_test_session(int maxprot, int use_int_cache,
2033 1.2 christos int use_ext_cache, long s_options)
2034 1.2 christos {
2035 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
2036 1.2 christos SSL *serverssl1 = NULL, *clientssl1 = NULL;
2037 1.2 christos SSL *serverssl2 = NULL, *clientssl2 = NULL;
2038 1.2 christos # ifndef OPENSSL_NO_TLS1_1
2039 1.2 christos SSL *serverssl3 = NULL, *clientssl3 = NULL;
2040 1.2 christos # endif
2041 1.2 christos SSL_SESSION *sess1 = NULL, *sess2 = NULL;
2042 1.2 christos int testresult = 0, numnewsesstick = 1;
2043 1.2 christos
2044 1.2 christos new_called = remove_called = 0;
2045 1.2 christos
2046 1.2 christos /* TLSv1.3 sends 2 NewSessionTickets */
2047 1.2 christos if (maxprot == TLS1_3_VERSION)
2048 1.2 christos numnewsesstick = 2;
2049 1.2 christos
2050 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2051 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
2052 1.2 christos &sctx, &cctx, cert, privkey)))
2053 1.2 christos return 0;
2054 1.2 christos
2055 1.2 christos /*
2056 1.2 christos * Only allow the max protocol version so we can force a connection failure
2057 1.2 christos * later
2058 1.2 christos */
2059 1.2 christos SSL_CTX_set_min_proto_version(cctx, maxprot);
2060 1.2 christos SSL_CTX_set_max_proto_version(cctx, maxprot);
2061 1.2 christos
2062 1.2 christos /* Set up session cache */
2063 1.2 christos if (use_ext_cache) {
2064 1.2 christos SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2065 1.2 christos SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
2066 1.2 christos }
2067 1.2 christos if (use_int_cache) {
2068 1.2 christos /* Also covers instance where both are set */
2069 1.2 christos SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
2070 1.2 christos } else {
2071 1.2 christos SSL_CTX_set_session_cache_mode(cctx,
2072 1.2 christos SSL_SESS_CACHE_CLIENT
2073 1.2 christos | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2074 1.2 christos }
2075 1.2 christos
2076 1.2 christos if (s_options) {
2077 1.2 christos SSL_CTX_set_options(sctx, s_options);
2078 1.2 christos }
2079 1.2 christos
2080 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2081 1.2 christos NULL, NULL))
2082 1.2 christos || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2083 1.2 christos SSL_ERROR_NONE))
2084 1.2 christos || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
2085 1.2 christos goto end;
2086 1.2 christos
2087 1.2 christos /* Should fail because it should already be in the cache */
2088 1.2 christos if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
2089 1.2 christos goto end;
2090 1.2 christos if (use_ext_cache
2091 1.2 christos && (!TEST_int_eq(new_called, numnewsesstick)
2092 1.2 christos
2093 1.2 christos || !TEST_int_eq(remove_called, 0)))
2094 1.2 christos goto end;
2095 1.2 christos
2096 1.2 christos new_called = remove_called = 0;
2097 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2098 1.2 christos &clientssl2, NULL, NULL))
2099 1.2 christos || !TEST_true(SSL_set_session(clientssl2, sess1))
2100 1.2 christos || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2101 1.2 christos SSL_ERROR_NONE))
2102 1.2 christos || !TEST_true(SSL_session_reused(clientssl2)))
2103 1.2 christos goto end;
2104 1.2 christos
2105 1.2 christos if (maxprot == TLS1_3_VERSION) {
2106 1.2 christos /*
2107 1.2 christos * In TLSv1.3 we should have created a new session even though we have
2108 1.2 christos * resumed. Since we attempted a resume we should also have removed the
2109 1.2 christos * old ticket from the cache so that we try to only use tickets once.
2110 1.2 christos */
2111 1.2 christos if (use_ext_cache
2112 1.2 christos && (!TEST_int_eq(new_called, 1)
2113 1.2 christos || !TEST_int_eq(remove_called, 1)))
2114 1.2 christos goto end;
2115 1.2 christos } else {
2116 1.2 christos /*
2117 1.2 christos * In TLSv1.2 we expect to have resumed so no sessions added or
2118 1.2 christos * removed.
2119 1.2 christos */
2120 1.2 christos if (use_ext_cache
2121 1.2 christos && (!TEST_int_eq(new_called, 0)
2122 1.2 christos || !TEST_int_eq(remove_called, 0)))
2123 1.2 christos goto end;
2124 1.2 christos }
2125 1.2 christos
2126 1.2 christos SSL_SESSION_free(sess1);
2127 1.2 christos if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
2128 1.2 christos goto end;
2129 1.2 christos shutdown_ssl_connection(serverssl2, clientssl2);
2130 1.2 christos serverssl2 = clientssl2 = NULL;
2131 1.2 christos
2132 1.2 christos new_called = remove_called = 0;
2133 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2134 1.2 christos &clientssl2, NULL, NULL))
2135 1.2 christos || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2136 1.2 christos SSL_ERROR_NONE)))
2137 1.2 christos goto end;
2138 1.2 christos
2139 1.2 christos if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
2140 1.2 christos goto end;
2141 1.2 christos
2142 1.2 christos if (use_ext_cache
2143 1.2 christos && (!TEST_int_eq(new_called, numnewsesstick)
2144 1.2 christos || !TEST_int_eq(remove_called, 0)))
2145 1.2 christos goto end;
2146 1.2 christos
2147 1.2 christos new_called = remove_called = 0;
2148 1.2 christos /*
2149 1.2 christos * This should clear sess2 from the cache because it is a "bad" session.
2150 1.2 christos * See SSL_set_session() documentation.
2151 1.2 christos */
2152 1.2 christos if (!TEST_true(SSL_set_session(clientssl2, sess1)))
2153 1.2 christos goto end;
2154 1.2 christos if (use_ext_cache
2155 1.2 christos && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2156 1.2 christos goto end;
2157 1.2 christos if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
2158 1.2 christos goto end;
2159 1.2 christos
2160 1.2 christos if (use_int_cache) {
2161 1.2 christos /* Should succeeded because it should not already be in the cache */
2162 1.2 christos if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
2163 1.2 christos || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
2164 1.2 christos goto end;
2165 1.2 christos }
2166 1.2 christos
2167 1.2 christos new_called = remove_called = 0;
2168 1.2 christos /* This shouldn't be in the cache so should fail */
2169 1.2 christos if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
2170 1.2 christos goto end;
2171 1.2 christos
2172 1.2 christos if (use_ext_cache
2173 1.2 christos && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2174 1.2 christos goto end;
2175 1.2 christos
2176 1.2 christos # if !defined(OPENSSL_NO_TLS1_1)
2177 1.2 christos new_called = remove_called = 0;
2178 1.2 christos /* Force a connection failure */
2179 1.2 christos SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
2180 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
2181 1.2 christos &clientssl3, NULL, NULL))
2182 1.2 christos || !TEST_true(SSL_set_session(clientssl3, sess1))
2183 1.2 christos /* This should fail because of the mismatched protocol versions */
2184 1.2 christos || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
2185 1.2 christos SSL_ERROR_NONE)))
2186 1.2 christos goto end;
2187 1.2 christos
2188 1.2 christos /* We should have automatically removed the session from the cache */
2189 1.2 christos if (use_ext_cache
2190 1.2 christos && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2191 1.2 christos goto end;
2192 1.2 christos
2193 1.2 christos /* Should succeed because it should not already be in the cache */
2194 1.2 christos if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
2195 1.2 christos goto end;
2196 1.2 christos # endif
2197 1.2 christos
2198 1.2 christos /* Now do some tests for server side caching */
2199 1.2 christos if (use_ext_cache) {
2200 1.2 christos SSL_CTX_sess_set_new_cb(cctx, NULL);
2201 1.2 christos SSL_CTX_sess_set_remove_cb(cctx, NULL);
2202 1.2 christos SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2203 1.2 christos SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
2204 1.2 christos SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
2205 1.2 christos get_sess_val = NULL;
2206 1.2 christos }
2207 1.2 christos
2208 1.2 christos SSL_CTX_set_session_cache_mode(cctx, 0);
2209 1.2 christos /* Internal caching is the default on the server side */
2210 1.2 christos if (!use_int_cache)
2211 1.2 christos SSL_CTX_set_session_cache_mode(sctx,
2212 1.2 christos SSL_SESS_CACHE_SERVER
2213 1.2 christos | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2214 1.2 christos
2215 1.2 christos SSL_free(serverssl1);
2216 1.2 christos SSL_free(clientssl1);
2217 1.2 christos serverssl1 = clientssl1 = NULL;
2218 1.2 christos SSL_free(serverssl2);
2219 1.2 christos SSL_free(clientssl2);
2220 1.2 christos serverssl2 = clientssl2 = NULL;
2221 1.2 christos SSL_SESSION_free(sess1);
2222 1.2 christos sess1 = NULL;
2223 1.2 christos SSL_SESSION_free(sess2);
2224 1.2 christos sess2 = NULL;
2225 1.2 christos
2226 1.2 christos SSL_CTX_set_max_proto_version(sctx, maxprot);
2227 1.2 christos if (maxprot == TLS1_2_VERSION)
2228 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
2229 1.2 christos new_called = remove_called = get_called = 0;
2230 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2231 1.2 christos NULL, NULL))
2232 1.2 christos || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2233 1.2 christos SSL_ERROR_NONE))
2234 1.2 christos || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
2235 1.2 christos || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
2236 1.2 christos goto end;
2237 1.2 christos
2238 1.2 christos if (use_int_cache) {
2239 1.2 christos if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
2240 1.2 christos /*
2241 1.2 christos * In TLSv1.3 it should not have been added to the internal cache,
2242 1.2 christos * except in the case where we also have an external cache (in that
2243 1.2 christos * case it gets added to the cache in order to generate remove
2244 1.2 christos * events after timeout).
2245 1.2 christos */
2246 1.2 christos if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
2247 1.2 christos goto end;
2248 1.2 christos } else {
2249 1.2 christos /* Should fail because it should already be in the cache */
2250 1.2 christos if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
2251 1.2 christos goto end;
2252 1.2 christos }
2253 1.2 christos }
2254 1.2 christos
2255 1.2 christos if (use_ext_cache) {
2256 1.2 christos SSL_SESSION *tmp = sess2;
2257 1.2 christos
2258 1.2 christos if (!TEST_int_eq(new_called, numnewsesstick)
2259 1.2 christos || !TEST_int_eq(remove_called, 0)
2260 1.2 christos || !TEST_int_eq(get_called, 0))
2261 1.2 christos goto end;
2262 1.2 christos /*
2263 1.2 christos * Delete the session from the internal cache to force a lookup from
2264 1.2 christos * the external cache. We take a copy first because
2265 1.2 christos * SSL_CTX_remove_session() also marks the session as non-resumable.
2266 1.2 christos */
2267 1.2 christos if (use_int_cache && maxprot != TLS1_3_VERSION) {
2268 1.2 christos if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
2269 1.2 christos || !TEST_true(sess2->owner != NULL)
2270 1.2 christos || !TEST_true(tmp->owner == NULL)
2271 1.2 christos || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
2272 1.2 christos goto end;
2273 1.2 christos SSL_SESSION_free(sess2);
2274 1.2 christos }
2275 1.2 christos sess2 = tmp;
2276 1.2 christos }
2277 1.2 christos
2278 1.2 christos new_called = remove_called = get_called = 0;
2279 1.2 christos get_sess_val = sess2;
2280 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2281 1.2 christos &clientssl2, NULL, NULL))
2282 1.2 christos || !TEST_true(SSL_set_session(clientssl2, sess1))
2283 1.2 christos || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2284 1.2 christos SSL_ERROR_NONE))
2285 1.2 christos || !TEST_true(SSL_session_reused(clientssl2)))
2286 1.2 christos goto end;
2287 1.2 christos
2288 1.2 christos if (use_ext_cache) {
2289 1.2 christos if (!TEST_int_eq(remove_called, 0))
2290 1.2 christos goto end;
2291 1.2 christos
2292 1.2 christos if (maxprot == TLS1_3_VERSION) {
2293 1.2 christos if (!TEST_int_eq(new_called, 1)
2294 1.2 christos || !TEST_int_eq(get_called, 0))
2295 1.2 christos goto end;
2296 1.2 christos } else {
2297 1.2 christos if (!TEST_int_eq(new_called, 0)
2298 1.2 christos || !TEST_int_eq(get_called, 1))
2299 1.2 christos goto end;
2300 1.2 christos }
2301 1.2 christos }
2302 1.2 christos /*
2303 1.2 christos * Make a small cache, force out all other sessions but
2304 1.2 christos * sess2, try to add sess1, which should succeed. Then
2305 1.2 christos * make sure it's there by checking the owners. Despite
2306 1.2 christos * the timeouts, sess1 should have kicked out sess2
2307 1.2 christos */
2308 1.2 christos
2309 1.2 christos /* Make sess1 expire before sess2 */
2310 1.2 christos if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
2311 1.2 christos || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
2312 1.2 christos || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
2313 1.2 christos || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
2314 1.2 christos goto end;
2315 1.2 christos
2316 1.2 christos if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
2317 1.2 christos goto end;
2318 1.2 christos
2319 1.2 christos /* Don't care about results - cache should only be sess2 at end */
2320 1.2 christos SSL_CTX_add_session(sctx, sess1);
2321 1.2 christos SSL_CTX_add_session(sctx, sess2);
2322 1.2 christos
2323 1.2 christos /* Now add sess1, and make sure it remains, despite timeout */
2324 1.2 christos if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
2325 1.2 christos || !TEST_ptr(sess1->owner)
2326 1.2 christos || !TEST_ptr_null(sess2->owner))
2327 1.2 christos goto end;
2328 1.2 christos
2329 1.2 christos testresult = 1;
2330 1.2 christos
2331 1.2 christos end:
2332 1.2 christos SSL_free(serverssl1);
2333 1.2 christos SSL_free(clientssl1);
2334 1.2 christos SSL_free(serverssl2);
2335 1.2 christos SSL_free(clientssl2);
2336 1.2 christos # ifndef OPENSSL_NO_TLS1_1
2337 1.2 christos SSL_free(serverssl3);
2338 1.2 christos SSL_free(clientssl3);
2339 1.2 christos # endif
2340 1.2 christos SSL_SESSION_free(sess1);
2341 1.2 christos SSL_SESSION_free(sess2);
2342 1.2 christos SSL_CTX_free(sctx);
2343 1.2 christos SSL_CTX_free(cctx);
2344 1.2 christos
2345 1.2 christos return testresult;
2346 1.2 christos }
2347 1.2 christos #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
2348 1.2 christos
2349 1.2 christos static int test_session_with_only_int_cache(void)
2350 1.2 christos {
2351 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
2352 1.2 christos if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
2353 1.2 christos return 0;
2354 1.2 christos #endif
2355 1.2 christos
2356 1.2 christos #ifndef OPENSSL_NO_TLS1_2
2357 1.2 christos return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
2358 1.2 christos #else
2359 1.2 christos return 1;
2360 1.2 christos #endif
2361 1.2 christos }
2362 1.2 christos
2363 1.2 christos static int test_session_with_only_ext_cache(void)
2364 1.2 christos {
2365 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
2366 1.2 christos if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
2367 1.2 christos return 0;
2368 1.2 christos #endif
2369 1.2 christos
2370 1.2 christos #ifndef OPENSSL_NO_TLS1_2
2371 1.2 christos return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
2372 1.2 christos #else
2373 1.2 christos return 1;
2374 1.2 christos #endif
2375 1.2 christos }
2376 1.2 christos
2377 1.2 christos static int test_session_with_both_cache(void)
2378 1.2 christos {
2379 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
2380 1.2 christos if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
2381 1.2 christos return 0;
2382 1.2 christos #endif
2383 1.2 christos
2384 1.2 christos #ifndef OPENSSL_NO_TLS1_2
2385 1.2 christos return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
2386 1.2 christos #else
2387 1.2 christos return 1;
2388 1.2 christos #endif
2389 1.2 christos }
2390 1.2 christos
2391 1.2 christos static int test_session_wo_ca_names(void)
2392 1.2 christos {
2393 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
2394 1.2 christos if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
2395 1.2 christos return 0;
2396 1.2 christos #endif
2397 1.2 christos
2398 1.2 christos #ifndef OPENSSL_NO_TLS1_2
2399 1.2 christos return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
2400 1.2 christos #else
2401 1.2 christos return 1;
2402 1.2 christos #endif
2403 1.2 christos }
2404 1.2 christos
2405 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
2406 1.2 christos static SSL_SESSION *sesscache[6];
2407 1.2 christos static int do_cache;
2408 1.2 christos
2409 1.2 christos static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
2410 1.2 christos {
2411 1.2 christos if (do_cache) {
2412 1.2 christos sesscache[new_called] = sess;
2413 1.2 christos } else {
2414 1.2 christos /* We don't need the reference to the session, so free it */
2415 1.2 christos SSL_SESSION_free(sess);
2416 1.2 christos }
2417 1.2 christos new_called++;
2418 1.2 christos
2419 1.2 christos return 1;
2420 1.2 christos }
2421 1.2 christos
2422 1.2 christos static int post_handshake_verify(SSL *sssl, SSL *cssl)
2423 1.2 christos {
2424 1.2 christos SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
2425 1.2 christos if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
2426 1.2 christos return 0;
2427 1.2 christos
2428 1.2 christos /* Start handshake on the server and client */
2429 1.2 christos if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
2430 1.2 christos || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
2431 1.2 christos || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
2432 1.2 christos || !TEST_true(create_ssl_connection(sssl, cssl,
2433 1.2 christos SSL_ERROR_NONE)))
2434 1.2 christos return 0;
2435 1.2 christos
2436 1.2 christos return 1;
2437 1.2 christos }
2438 1.2 christos
2439 1.2 christos static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
2440 1.2 christos SSL_CTX **cctx)
2441 1.2 christos {
2442 1.2 christos int sess_id_ctx = 1;
2443 1.2 christos
2444 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2445 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
2446 1.2 christos sctx, cctx, cert, privkey))
2447 1.2 christos || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
2448 1.2 christos || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
2449 1.2 christos (void *)&sess_id_ctx,
2450 1.2 christos sizeof(sess_id_ctx))))
2451 1.2 christos return 0;
2452 1.2 christos
2453 1.2 christos if (stateful)
2454 1.2 christos SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
2455 1.2 christos
2456 1.2 christos SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
2457 1.2 christos | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2458 1.2 christos SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
2459 1.2 christos
2460 1.2 christos return 1;
2461 1.2 christos }
2462 1.2 christos
2463 1.2 christos static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
2464 1.2 christos {
2465 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
2466 1.2 christos int i;
2467 1.2 christos
2468 1.2 christos /* Test that we can resume with all the tickets we got given */
2469 1.2 christos for (i = 0; i < idx * 2; i++) {
2470 1.2 christos new_called = 0;
2471 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2472 1.2 christos &clientssl, NULL, NULL))
2473 1.2 christos || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
2474 1.2 christos goto end;
2475 1.2 christos
2476 1.2 christos SSL_set_post_handshake_auth(clientssl, 1);
2477 1.2 christos
2478 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2479 1.2 christos SSL_ERROR_NONE)))
2480 1.2 christos goto end;
2481 1.2 christos
2482 1.2 christos /*
2483 1.2 christos * Following a successful resumption we only get 1 ticket. After a
2484 1.2 christos * failed one we should get idx tickets.
2485 1.2 christos */
2486 1.2 christos if (succ) {
2487 1.2 christos if (!TEST_true(SSL_session_reused(clientssl))
2488 1.2 christos || !TEST_int_eq(new_called, 1))
2489 1.2 christos goto end;
2490 1.2 christos } else {
2491 1.2 christos if (!TEST_false(SSL_session_reused(clientssl))
2492 1.2 christos || !TEST_int_eq(new_called, idx))
2493 1.2 christos goto end;
2494 1.2 christos }
2495 1.2 christos
2496 1.2 christos new_called = 0;
2497 1.2 christos /* After a post-handshake authentication we should get 1 new ticket */
2498 1.2 christos if (succ
2499 1.2 christos && (!post_handshake_verify(serverssl, clientssl)
2500 1.2 christos || !TEST_int_eq(new_called, 1)))
2501 1.2 christos goto end;
2502 1.2 christos
2503 1.2 christos SSL_shutdown(clientssl);
2504 1.2 christos SSL_shutdown(serverssl);
2505 1.2 christos SSL_free(serverssl);
2506 1.2 christos SSL_free(clientssl);
2507 1.2 christos serverssl = clientssl = NULL;
2508 1.2 christos SSL_SESSION_free(sesscache[i]);
2509 1.2 christos sesscache[i] = NULL;
2510 1.2 christos }
2511 1.2 christos
2512 1.2 christos return 1;
2513 1.2 christos
2514 1.2 christos end:
2515 1.2 christos SSL_free(clientssl);
2516 1.2 christos SSL_free(serverssl);
2517 1.2 christos return 0;
2518 1.2 christos }
2519 1.2 christos
2520 1.2 christos static int test_tickets(int stateful, int idx)
2521 1.2 christos {
2522 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
2523 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
2524 1.2 christos int testresult = 0;
2525 1.2 christos size_t j;
2526 1.2 christos
2527 1.2 christos /* idx is the test number, but also the number of tickets we want */
2528 1.2 christos
2529 1.2 christos new_called = 0;
2530 1.2 christos do_cache = 1;
2531 1.2 christos
2532 1.2 christos if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2533 1.2 christos goto end;
2534 1.2 christos
2535 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2536 1.2 christos &clientssl, NULL, NULL)))
2537 1.2 christos goto end;
2538 1.2 christos
2539 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2540 1.2 christos SSL_ERROR_NONE))
2541 1.2 christos /* Check we got the number of tickets we were expecting */
2542 1.2 christos || !TEST_int_eq(idx, new_called))
2543 1.2 christos goto end;
2544 1.2 christos
2545 1.2 christos SSL_shutdown(clientssl);
2546 1.2 christos SSL_shutdown(serverssl);
2547 1.2 christos SSL_free(serverssl);
2548 1.2 christos SSL_free(clientssl);
2549 1.2 christos SSL_CTX_free(sctx);
2550 1.2 christos SSL_CTX_free(cctx);
2551 1.2 christos clientssl = serverssl = NULL;
2552 1.2 christos sctx = cctx = NULL;
2553 1.2 christos
2554 1.2 christos /*
2555 1.2 christos * Now we try to resume with the tickets we previously created. The
2556 1.2 christos * resumption attempt is expected to fail (because we're now using a new
2557 1.2 christos * SSL_CTX). We should see idx number of tickets issued again.
2558 1.2 christos */
2559 1.2 christos
2560 1.2 christos /* Stop caching sessions - just count them */
2561 1.2 christos do_cache = 0;
2562 1.2 christos
2563 1.2 christos if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2564 1.2 christos goto end;
2565 1.2 christos
2566 1.2 christos if (!check_resumption(idx, sctx, cctx, 0))
2567 1.2 christos goto end;
2568 1.2 christos
2569 1.2 christos /* Start again with caching sessions */
2570 1.2 christos new_called = 0;
2571 1.2 christos do_cache = 1;
2572 1.2 christos SSL_CTX_free(sctx);
2573 1.2 christos SSL_CTX_free(cctx);
2574 1.2 christos sctx = cctx = NULL;
2575 1.2 christos
2576 1.2 christos if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2577 1.2 christos goto end;
2578 1.2 christos
2579 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2580 1.2 christos &clientssl, NULL, NULL)))
2581 1.2 christos goto end;
2582 1.2 christos
2583 1.2 christos SSL_set_post_handshake_auth(clientssl, 1);
2584 1.2 christos
2585 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2586 1.2 christos SSL_ERROR_NONE))
2587 1.2 christos /* Check we got the number of tickets we were expecting */
2588 1.2 christos || !TEST_int_eq(idx, new_called))
2589 1.2 christos goto end;
2590 1.2 christos
2591 1.2 christos /* After a post-handshake authentication we should get new tickets issued */
2592 1.2 christos if (!post_handshake_verify(serverssl, clientssl)
2593 1.2 christos || !TEST_int_eq(idx * 2, new_called))
2594 1.2 christos goto end;
2595 1.2 christos
2596 1.2 christos SSL_shutdown(clientssl);
2597 1.2 christos SSL_shutdown(serverssl);
2598 1.2 christos SSL_free(serverssl);
2599 1.2 christos SSL_free(clientssl);
2600 1.2 christos serverssl = clientssl = NULL;
2601 1.2 christos
2602 1.2 christos /* Stop caching sessions - just count them */
2603 1.2 christos do_cache = 0;
2604 1.2 christos
2605 1.2 christos /*
2606 1.2 christos * Check we can resume with all the tickets we created. This time around the
2607 1.2 christos * resumptions should all be successful.
2608 1.2 christos */
2609 1.2 christos if (!check_resumption(idx, sctx, cctx, 1))
2610 1.2 christos goto end;
2611 1.2 christos
2612 1.2 christos testresult = 1;
2613 1.2 christos
2614 1.2 christos end:
2615 1.2 christos SSL_free(serverssl);
2616 1.2 christos SSL_free(clientssl);
2617 1.2 christos for (j = 0; j < OSSL_NELEM(sesscache); j++) {
2618 1.2 christos SSL_SESSION_free(sesscache[j]);
2619 1.2 christos sesscache[j] = NULL;
2620 1.2 christos }
2621 1.2 christos SSL_CTX_free(sctx);
2622 1.2 christos SSL_CTX_free(cctx);
2623 1.2 christos
2624 1.2 christos return testresult;
2625 1.2 christos }
2626 1.2 christos
2627 1.2 christos static int test_stateless_tickets(int idx)
2628 1.2 christos {
2629 1.2 christos return test_tickets(0, idx);
2630 1.2 christos }
2631 1.2 christos
2632 1.2 christos static int test_stateful_tickets(int idx)
2633 1.2 christos {
2634 1.2 christos return test_tickets(1, idx);
2635 1.2 christos }
2636 1.2 christos
2637 1.2 christos static int test_psk_tickets(void)
2638 1.2 christos {
2639 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
2640 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
2641 1.2 christos int testresult = 0;
2642 1.2 christos int sess_id_ctx = 1;
2643 1.2 christos
2644 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2645 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
2646 1.2 christos &sctx, &cctx, NULL, NULL))
2647 1.2 christos || !TEST_true(SSL_CTX_set_session_id_context(sctx,
2648 1.2 christos (void *)&sess_id_ctx,
2649 1.2 christos sizeof(sess_id_ctx))))
2650 1.2 christos goto end;
2651 1.2 christos
2652 1.2 christos SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
2653 1.2 christos | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2654 1.2 christos SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
2655 1.2 christos SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
2656 1.2 christos SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2657 1.2 christos use_session_cb_cnt = 0;
2658 1.2 christos find_session_cb_cnt = 0;
2659 1.2 christos srvid = pskid;
2660 1.2 christos new_called = 0;
2661 1.2 christos
2662 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2663 1.2 christos NULL, NULL)))
2664 1.2 christos goto end;
2665 1.2 christos clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH);
2666 1.2 christos if (!TEST_ptr(clientpsk))
2667 1.2 christos goto end;
2668 1.2 christos SSL_SESSION_up_ref(clientpsk);
2669 1.2 christos
2670 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2671 1.2 christos SSL_ERROR_NONE))
2672 1.2 christos || !TEST_int_eq(1, find_session_cb_cnt)
2673 1.2 christos || !TEST_int_eq(1, use_session_cb_cnt)
2674 1.2 christos /* We should always get 1 ticket when using external PSK */
2675 1.2 christos || !TEST_int_eq(1, new_called))
2676 1.2 christos goto end;
2677 1.2 christos
2678 1.2 christos testresult = 1;
2679 1.2 christos
2680 1.2 christos end:
2681 1.2 christos SSL_free(serverssl);
2682 1.2 christos SSL_free(clientssl);
2683 1.2 christos SSL_CTX_free(sctx);
2684 1.2 christos SSL_CTX_free(cctx);
2685 1.2 christos SSL_SESSION_free(clientpsk);
2686 1.2 christos SSL_SESSION_free(serverpsk);
2687 1.2 christos clientpsk = serverpsk = NULL;
2688 1.2 christos
2689 1.2 christos return testresult;
2690 1.2 christos }
2691 1.2 christos
2692 1.2 christos static int test_extra_tickets(int idx)
2693 1.2 christos {
2694 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
2695 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
2696 1.2 christos BIO *bretry = BIO_new(bio_s_always_retry());
2697 1.2 christos BIO *tmp = NULL;
2698 1.2 christos int testresult = 0;
2699 1.2 christos int stateful = 0;
2700 1.2 christos size_t nbytes;
2701 1.2 christos unsigned char c, buf[1];
2702 1.2 christos
2703 1.2 christos new_called = 0;
2704 1.2 christos do_cache = 1;
2705 1.2 christos
2706 1.2 christos if (idx >= 3) {
2707 1.2 christos idx -= 3;
2708 1.2 christos stateful = 1;
2709 1.2 christos }
2710 1.2 christos
2711 1.2 christos if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
2712 1.2 christos goto end;
2713 1.2 christos SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2714 1.2 christos /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
2715 1.2 christos SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2716 1.2 christos
2717 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2718 1.2 christos &clientssl, NULL, NULL)))
2719 1.2 christos goto end;
2720 1.2 christos
2721 1.2 christos /*
2722 1.2 christos * Note that we have new_session_cb on both sctx and cctx, so new_called is
2723 1.2 christos * incremented by both client and server.
2724 1.2 christos */
2725 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2726 1.2 christos SSL_ERROR_NONE))
2727 1.2 christos /* Check we got the number of tickets we were expecting */
2728 1.2 christos || !TEST_int_eq(idx * 2, new_called)
2729 1.2 christos || !TEST_true(SSL_new_session_ticket(serverssl))
2730 1.2 christos || !TEST_true(SSL_new_session_ticket(serverssl))
2731 1.2 christos || !TEST_int_eq(idx * 2, new_called))
2732 1.2 christos goto end;
2733 1.2 christos
2734 1.2 christos /* Now try a (real) write to actually send the tickets */
2735 1.2 christos c = '1';
2736 1.2 christos if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2737 1.2 christos || !TEST_size_t_eq(1, nbytes)
2738 1.2 christos || !TEST_int_eq(idx * 2 + 2, new_called)
2739 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2740 1.2 christos || !TEST_int_eq(idx * 2 + 4, new_called)
2741 1.2 christos || !TEST_int_eq(sizeof(buf), nbytes)
2742 1.2 christos || !TEST_int_eq(c, buf[0])
2743 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2744 1.2 christos goto end;
2745 1.2 christos
2746 1.2 christos /* Try with only requesting one new ticket, too */
2747 1.2 christos c = '2';
2748 1.2 christos new_called = 0;
2749 1.2 christos if (!TEST_true(SSL_new_session_ticket(serverssl))
2750 1.2 christos || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
2751 1.2 christos || !TEST_size_t_eq(sizeof(c), nbytes)
2752 1.2 christos || !TEST_int_eq(1, new_called)
2753 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2754 1.2 christos || !TEST_int_eq(2, new_called)
2755 1.2 christos || !TEST_size_t_eq(sizeof(buf), nbytes)
2756 1.2 christos || !TEST_int_eq(c, buf[0]))
2757 1.2 christos goto end;
2758 1.2 christos
2759 1.2 christos /* Do it again but use dummy writes to drive the ticket generation */
2760 1.2 christos c = '3';
2761 1.2 christos new_called = 0;
2762 1.2 christos if (!TEST_true(SSL_new_session_ticket(serverssl))
2763 1.2 christos || !TEST_true(SSL_new_session_ticket(serverssl))
2764 1.2 christos || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
2765 1.2 christos || !TEST_size_t_eq(0, nbytes)
2766 1.2 christos || !TEST_int_eq(2, new_called)
2767 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2768 1.2 christos || !TEST_int_eq(4, new_called))
2769 1.2 christos goto end;
2770 1.2 christos
2771 1.2 christos /* Once more, but with SSL_do_handshake() to drive the ticket generation */
2772 1.2 christos c = '4';
2773 1.2 christos new_called = 0;
2774 1.2 christos if (!TEST_true(SSL_new_session_ticket(serverssl))
2775 1.2 christos || !TEST_true(SSL_new_session_ticket(serverssl))
2776 1.2 christos || !TEST_true(SSL_do_handshake(serverssl))
2777 1.2 christos || !TEST_int_eq(2, new_called)
2778 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2779 1.2 christos || !TEST_int_eq(4, new_called))
2780 1.2 christos goto end;
2781 1.2 christos
2782 1.2 christos /*
2783 1.2 christos * Use the always-retry BIO to exercise the logic that forces ticket
2784 1.2 christos * generation to wait until a record boundary.
2785 1.2 christos */
2786 1.2 christos c = '5';
2787 1.2 christos new_called = 0;
2788 1.2 christos tmp = SSL_get_wbio(serverssl);
2789 1.2 christos if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
2790 1.2 christos tmp = NULL;
2791 1.2 christos goto end;
2792 1.2 christos }
2793 1.2 christos SSL_set0_wbio(serverssl, bretry);
2794 1.2 christos bretry = NULL;
2795 1.2 christos if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
2796 1.2 christos || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
2797 1.2 christos || !TEST_size_t_eq(nbytes, 0))
2798 1.2 christos goto end;
2799 1.2 christos /* Restore a BIO that will let the write succeed */
2800 1.2 christos SSL_set0_wbio(serverssl, tmp);
2801 1.2 christos tmp = NULL;
2802 1.2 christos /*
2803 1.2 christos * These calls should just queue the request and not send anything
2804 1.2 christos * even if we explicitly try to hit the state machine.
2805 1.2 christos */
2806 1.2 christos if (!TEST_true(SSL_new_session_ticket(serverssl))
2807 1.2 christos || !TEST_true(SSL_new_session_ticket(serverssl))
2808 1.2 christos || !TEST_int_eq(0, new_called)
2809 1.2 christos || !TEST_true(SSL_do_handshake(serverssl))
2810 1.2 christos || !TEST_int_eq(0, new_called))
2811 1.2 christos goto end;
2812 1.2 christos /* Re-do the write; still no tickets sent */
2813 1.2 christos if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2814 1.2 christos || !TEST_size_t_eq(1, nbytes)
2815 1.2 christos || !TEST_int_eq(0, new_called)
2816 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2817 1.2 christos || !TEST_int_eq(0, new_called)
2818 1.2 christos || !TEST_int_eq(sizeof(buf), nbytes)
2819 1.2 christos || !TEST_int_eq(c, buf[0])
2820 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2821 1.2 christos goto end;
2822 1.2 christos /* Even trying to hit the state machine now will still not send tickets */
2823 1.2 christos if (!TEST_true(SSL_do_handshake(serverssl))
2824 1.2 christos || !TEST_int_eq(0, new_called))
2825 1.2 christos goto end;
2826 1.2 christos /* Now the *next* write should send the tickets */
2827 1.2 christos c = '6';
2828 1.2 christos if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2829 1.2 christos || !TEST_size_t_eq(1, nbytes)
2830 1.2 christos || !TEST_int_eq(2, new_called)
2831 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2832 1.2 christos || !TEST_int_eq(4, new_called)
2833 1.2 christos || !TEST_int_eq(sizeof(buf), nbytes)
2834 1.2 christos || !TEST_int_eq(c, buf[0])
2835 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2836 1.2 christos goto end;
2837 1.2 christos
2838 1.2 christos SSL_shutdown(clientssl);
2839 1.2 christos SSL_shutdown(serverssl);
2840 1.2 christos testresult = 1;
2841 1.2 christos
2842 1.2 christos end:
2843 1.2 christos BIO_free(bretry);
2844 1.2 christos BIO_free(tmp);
2845 1.2 christos SSL_free(serverssl);
2846 1.2 christos SSL_free(clientssl);
2847 1.2 christos SSL_CTX_free(sctx);
2848 1.2 christos SSL_CTX_free(cctx);
2849 1.2 christos clientssl = serverssl = NULL;
2850 1.2 christos sctx = cctx = NULL;
2851 1.2 christos return testresult;
2852 1.2 christos }
2853 1.2 christos #endif
2854 1.2 christos
2855 1.2 christos #define USE_NULL 0
2856 1.2 christos #define USE_BIO_1 1
2857 1.2 christos #define USE_BIO_2 2
2858 1.2 christos #define USE_DEFAULT 3
2859 1.2 christos
2860 1.2 christos #define CONNTYPE_CONNECTION_SUCCESS 0
2861 1.2 christos #define CONNTYPE_CONNECTION_FAIL 1
2862 1.2 christos #define CONNTYPE_NO_CONNECTION 2
2863 1.2 christos
2864 1.2 christos #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
2865 1.2 christos #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
2866 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
2867 1.2 christos # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
2868 1.2 christos #else
2869 1.2 christos # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
2870 1.2 christos #endif
2871 1.2 christos
2872 1.2 christos #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
2873 1.2 christos + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
2874 1.2 christos + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
2875 1.2 christos
2876 1.2 christos static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
2877 1.2 christos {
2878 1.2 christos switch (type) {
2879 1.2 christos case USE_NULL:
2880 1.2 christos *res = NULL;
2881 1.2 christos break;
2882 1.2 christos case USE_BIO_1:
2883 1.2 christos *res = bio1;
2884 1.2 christos break;
2885 1.2 christos case USE_BIO_2:
2886 1.2 christos *res = bio2;
2887 1.2 christos break;
2888 1.2 christos }
2889 1.2 christos }
2890 1.2 christos
2891 1.2 christos
2892 1.2 christos /*
2893 1.2 christos * Tests calls to SSL_set_bio() under various conditions.
2894 1.2 christos *
2895 1.2 christos * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
2896 1.2 christos * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
2897 1.2 christos * then do more tests where we create a successful connection first using our
2898 1.2 christos * standard connection setup functions, and then call SSL_set_bio() with
2899 1.2 christos * various combinations of valid BIOs or NULL. We then repeat these tests
2900 1.2 christos * following a failed connection. In this last case we are looking to check that
2901 1.2 christos * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
2902 1.2 christos */
2903 1.2 christos static int test_ssl_set_bio(int idx)
2904 1.2 christos {
2905 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
2906 1.2 christos BIO *bio1 = NULL;
2907 1.2 christos BIO *bio2 = NULL;
2908 1.2 christos BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
2909 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
2910 1.2 christos int initrbio, initwbio, newrbio, newwbio, conntype;
2911 1.2 christos int testresult = 0;
2912 1.2 christos
2913 1.2 christos if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
2914 1.2 christos initrbio = idx % 3;
2915 1.2 christos idx /= 3;
2916 1.2 christos initwbio = idx % 3;
2917 1.2 christos idx /= 3;
2918 1.2 christos newrbio = idx % 3;
2919 1.2 christos idx /= 3;
2920 1.2 christos newwbio = idx % 3;
2921 1.2 christos conntype = CONNTYPE_NO_CONNECTION;
2922 1.2 christos } else {
2923 1.2 christos idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
2924 1.2 christos initrbio = initwbio = USE_DEFAULT;
2925 1.2 christos newrbio = idx % 2;
2926 1.2 christos idx /= 2;
2927 1.2 christos newwbio = idx % 2;
2928 1.2 christos idx /= 2;
2929 1.2 christos conntype = idx % 2;
2930 1.2 christos }
2931 1.2 christos
2932 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2933 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
2934 1.2 christos &sctx, &cctx, cert, privkey)))
2935 1.2 christos goto end;
2936 1.2 christos
2937 1.2 christos if (conntype == CONNTYPE_CONNECTION_FAIL) {
2938 1.2 christos /*
2939 1.2 christos * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
2940 1.2 christos * because we reduced the number of tests in the definition of
2941 1.2 christos * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
2942 1.2 christos * mismatched protocol versions we will force a connection failure.
2943 1.2 christos */
2944 1.2 christos SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
2945 1.2 christos SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2946 1.2 christos }
2947 1.2 christos
2948 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2949 1.2 christos NULL, NULL)))
2950 1.2 christos goto end;
2951 1.2 christos
2952 1.2 christos if (initrbio == USE_BIO_1
2953 1.2 christos || initwbio == USE_BIO_1
2954 1.2 christos || newrbio == USE_BIO_1
2955 1.2 christos || newwbio == USE_BIO_1) {
2956 1.2 christos if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
2957 1.2 christos goto end;
2958 1.2 christos }
2959 1.2 christos
2960 1.2 christos if (initrbio == USE_BIO_2
2961 1.2 christos || initwbio == USE_BIO_2
2962 1.2 christos || newrbio == USE_BIO_2
2963 1.2 christos || newwbio == USE_BIO_2) {
2964 1.2 christos if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
2965 1.2 christos goto end;
2966 1.2 christos }
2967 1.2 christos
2968 1.2 christos if (initrbio != USE_DEFAULT) {
2969 1.2 christos setupbio(&irbio, bio1, bio2, initrbio);
2970 1.2 christos setupbio(&iwbio, bio1, bio2, initwbio);
2971 1.2 christos SSL_set_bio(clientssl, irbio, iwbio);
2972 1.2 christos
2973 1.2 christos /*
2974 1.2 christos * We want to maintain our own refs to these BIO, so do an up ref for
2975 1.2 christos * each BIO that will have ownership transferred in the SSL_set_bio()
2976 1.2 christos * call
2977 1.2 christos */
2978 1.2 christos if (irbio != NULL)
2979 1.2 christos BIO_up_ref(irbio);
2980 1.2 christos if (iwbio != NULL && iwbio != irbio)
2981 1.2 christos BIO_up_ref(iwbio);
2982 1.2 christos }
2983 1.2 christos
2984 1.2 christos if (conntype != CONNTYPE_NO_CONNECTION
2985 1.2 christos && !TEST_true(create_ssl_connection(serverssl, clientssl,
2986 1.2 christos SSL_ERROR_NONE)
2987 1.2 christos == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
2988 1.2 christos goto end;
2989 1.2 christos
2990 1.2 christos setupbio(&nrbio, bio1, bio2, newrbio);
2991 1.2 christos setupbio(&nwbio, bio1, bio2, newwbio);
2992 1.2 christos
2993 1.2 christos /*
2994 1.2 christos * We will (maybe) transfer ownership again so do more up refs.
2995 1.2 christos * SSL_set_bio() has some really complicated ownership rules where BIOs have
2996 1.2 christos * already been set!
2997 1.2 christos */
2998 1.2 christos if (nrbio != NULL
2999 1.2 christos && nrbio != irbio
3000 1.2 christos && (nwbio != iwbio || nrbio != nwbio))
3001 1.2 christos BIO_up_ref(nrbio);
3002 1.2 christos if (nwbio != NULL
3003 1.2 christos && nwbio != nrbio
3004 1.2 christos && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
3005 1.2 christos BIO_up_ref(nwbio);
3006 1.2 christos
3007 1.2 christos SSL_set_bio(clientssl, nrbio, nwbio);
3008 1.2 christos
3009 1.2 christos testresult = 1;
3010 1.2 christos
3011 1.2 christos end:
3012 1.2 christos BIO_free(bio1);
3013 1.2 christos BIO_free(bio2);
3014 1.2 christos
3015 1.2 christos /*
3016 1.2 christos * This test is checking that the ref counting for SSL_set_bio is correct.
3017 1.2 christos * If we get here and we did too many frees then we will fail in the above
3018 1.2 christos * functions.
3019 1.2 christos */
3020 1.2 christos SSL_free(serverssl);
3021 1.2 christos SSL_free(clientssl);
3022 1.2 christos SSL_CTX_free(sctx);
3023 1.2 christos SSL_CTX_free(cctx);
3024 1.2 christos return testresult;
3025 1.2 christos }
3026 1.2 christos
3027 1.2 christos typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
3028 1.2 christos
3029 1.2 christos static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
3030 1.2 christos {
3031 1.2 christos BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
3032 1.2 christos SSL_CTX *ctx;
3033 1.2 christos SSL *ssl = NULL;
3034 1.2 christos int testresult = 0;
3035 1.2 christos
3036 1.2 christos if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
3037 1.2 christos || !TEST_ptr(ssl = SSL_new(ctx))
3038 1.2 christos || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
3039 1.2 christos || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
3040 1.2 christos goto end;
3041 1.2 christos
3042 1.2 christos BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
3043 1.2 christos
3044 1.2 christos /*
3045 1.2 christos * If anything goes wrong here then we could leak memory.
3046 1.2 christos */
3047 1.2 christos BIO_push(sslbio, membio1);
3048 1.2 christos
3049 1.2 christos /* Verify changing the rbio/wbio directly does not cause leaks */
3050 1.2 christos if (change_bio != NO_BIO_CHANGE) {
3051 1.2 christos if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
3052 1.2 christos ssl = NULL;
3053 1.2 christos goto end;
3054 1.2 christos }
3055 1.2 christos if (change_bio == CHANGE_RBIO)
3056 1.2 christos SSL_set0_rbio(ssl, membio2);
3057 1.2 christos else
3058 1.2 christos SSL_set0_wbio(ssl, membio2);
3059 1.2 christos }
3060 1.2 christos ssl = NULL;
3061 1.2 christos
3062 1.2 christos if (pop_ssl)
3063 1.2 christos BIO_pop(sslbio);
3064 1.2 christos else
3065 1.2 christos BIO_pop(membio1);
3066 1.2 christos
3067 1.2 christos testresult = 1;
3068 1.2 christos end:
3069 1.2 christos BIO_free(membio1);
3070 1.2 christos BIO_free(sslbio);
3071 1.2 christos SSL_free(ssl);
3072 1.2 christos SSL_CTX_free(ctx);
3073 1.2 christos
3074 1.2 christos return testresult;
3075 1.2 christos }
3076 1.2 christos
3077 1.2 christos static int test_ssl_bio_pop_next_bio(void)
3078 1.2 christos {
3079 1.2 christos return execute_test_ssl_bio(0, NO_BIO_CHANGE);
3080 1.2 christos }
3081 1.2 christos
3082 1.2 christos static int test_ssl_bio_pop_ssl_bio(void)
3083 1.2 christos {
3084 1.2 christos return execute_test_ssl_bio(1, NO_BIO_CHANGE);
3085 1.2 christos }
3086 1.2 christos
3087 1.2 christos static int test_ssl_bio_change_rbio(void)
3088 1.2 christos {
3089 1.2 christos return execute_test_ssl_bio(0, CHANGE_RBIO);
3090 1.2 christos }
3091 1.2 christos
3092 1.2 christos static int test_ssl_bio_change_wbio(void)
3093 1.2 christos {
3094 1.2 christos return execute_test_ssl_bio(0, CHANGE_WBIO);
3095 1.2 christos }
3096 1.2 christos
3097 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
3098 1.2 christos typedef struct {
3099 1.2 christos /* The list of sig algs */
3100 1.2 christos const int *list;
3101 1.2 christos /* The length of the list */
3102 1.2 christos size_t listlen;
3103 1.2 christos /* A sigalgs list in string format */
3104 1.2 christos const char *liststr;
3105 1.2 christos /* Whether setting the list should succeed */
3106 1.2 christos int valid;
3107 1.2 christos /* Whether creating a connection with the list should succeed */
3108 1.2 christos int connsuccess;
3109 1.2 christos } sigalgs_list;
3110 1.2 christos
3111 1.2 christos static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
3112 1.2 christos # ifndef OPENSSL_NO_EC
3113 1.2 christos static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
3114 1.2 christos static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
3115 1.2 christos # endif
3116 1.2 christos static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
3117 1.2 christos static const int invalidlist2[] = {NID_sha256, NID_undef};
3118 1.2 christos static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
3119 1.2 christos static const int invalidlist4[] = {NID_sha256};
3120 1.2 christos static const sigalgs_list testsigalgs[] = {
3121 1.2 christos {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
3122 1.2 christos # ifndef OPENSSL_NO_EC
3123 1.2 christos {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
3124 1.2 christos {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
3125 1.2 christos # endif
3126 1.2 christos {NULL, 0, "RSA+SHA256", 1, 1},
3127 1.2 christos # ifndef OPENSSL_NO_EC
3128 1.2 christos {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
3129 1.2 christos {NULL, 0, "ECDSA+SHA512", 1, 0},
3130 1.2 christos # endif
3131 1.2 christos {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
3132 1.2 christos {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
3133 1.2 christos {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
3134 1.2 christos {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
3135 1.2 christos {NULL, 0, "RSA", 0, 0},
3136 1.2 christos {NULL, 0, "SHA256", 0, 0},
3137 1.2 christos {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
3138 1.2 christos {NULL, 0, "Invalid", 0, 0}
3139 1.2 christos };
3140 1.2 christos
3141 1.2 christos static int test_set_sigalgs(int idx)
3142 1.2 christos {
3143 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
3144 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
3145 1.2 christos int testresult = 0;
3146 1.2 christos const sigalgs_list *curr;
3147 1.2 christos int testctx;
3148 1.2 christos
3149 1.2 christos /* Should never happen */
3150 1.2 christos if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
3151 1.2 christos return 0;
3152 1.2 christos
3153 1.2 christos testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
3154 1.2 christos curr = testctx ? &testsigalgs[idx]
3155 1.2 christos : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
3156 1.2 christos
3157 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3158 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
3159 1.2 christos &sctx, &cctx, cert, privkey)))
3160 1.2 christos return 0;
3161 1.2 christos
3162 1.2 christos SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
3163 1.2 christos
3164 1.2 christos if (testctx) {
3165 1.2 christos int ret;
3166 1.2 christos
3167 1.2 christos if (curr->list != NULL)
3168 1.2 christos ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
3169 1.2 christos else
3170 1.2 christos ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
3171 1.2 christos
3172 1.2 christos if (!ret) {
3173 1.2 christos if (curr->valid)
3174 1.2 christos TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
3175 1.2 christos else
3176 1.2 christos testresult = 1;
3177 1.2 christos goto end;
3178 1.2 christos }
3179 1.2 christos if (!curr->valid) {
3180 1.2 christos TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
3181 1.2 christos goto end;
3182 1.2 christos }
3183 1.2 christos }
3184 1.2 christos
3185 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3186 1.2 christos &clientssl, NULL, NULL)))
3187 1.2 christos goto end;
3188 1.2 christos
3189 1.2 christos if (!testctx) {
3190 1.2 christos int ret;
3191 1.2 christos
3192 1.2 christos if (curr->list != NULL)
3193 1.2 christos ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
3194 1.2 christos else
3195 1.2 christos ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
3196 1.2 christos if (!ret) {
3197 1.2 christos if (curr->valid)
3198 1.2 christos TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
3199 1.2 christos else
3200 1.2 christos testresult = 1;
3201 1.2 christos goto end;
3202 1.2 christos }
3203 1.2 christos if (!curr->valid)
3204 1.2 christos goto end;
3205 1.2 christos }
3206 1.2 christos
3207 1.2 christos if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
3208 1.2 christos SSL_ERROR_NONE),
3209 1.2 christos curr->connsuccess))
3210 1.2 christos goto end;
3211 1.2 christos
3212 1.2 christos testresult = 1;
3213 1.2 christos
3214 1.2 christos end:
3215 1.2 christos SSL_free(serverssl);
3216 1.2 christos SSL_free(clientssl);
3217 1.2 christos SSL_CTX_free(sctx);
3218 1.2 christos SSL_CTX_free(cctx);
3219 1.2 christos
3220 1.2 christos return testresult;
3221 1.2 christos }
3222 1.2 christos #endif
3223 1.2 christos
3224 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
3225 1.2 christos static int psk_client_cb_cnt = 0;
3226 1.2 christos static int psk_server_cb_cnt = 0;
3227 1.2 christos
3228 1.2 christos static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
3229 1.2 christos size_t *idlen, SSL_SESSION **sess)
3230 1.2 christos {
3231 1.2 christos switch (++use_session_cb_cnt) {
3232 1.2 christos case 1:
3233 1.2 christos /* The first call should always have a NULL md */
3234 1.2 christos if (md != NULL)
3235 1.2 christos return 0;
3236 1.2 christos break;
3237 1.2 christos
3238 1.2 christos case 2:
3239 1.2 christos /* The second call should always have an md */
3240 1.2 christos if (md == NULL)
3241 1.2 christos return 0;
3242 1.2 christos break;
3243 1.2 christos
3244 1.2 christos default:
3245 1.2 christos /* We should only be called a maximum of twice */
3246 1.2 christos return 0;
3247 1.2 christos }
3248 1.2 christos
3249 1.2 christos if (clientpsk != NULL)
3250 1.2 christos SSL_SESSION_up_ref(clientpsk);
3251 1.2 christos
3252 1.2 christos *sess = clientpsk;
3253 1.2 christos *id = (const unsigned char *)pskid;
3254 1.2 christos *idlen = strlen(pskid);
3255 1.2 christos
3256 1.2 christos return 1;
3257 1.2 christos }
3258 1.2 christos
3259 1.2 christos #ifndef OPENSSL_NO_PSK
3260 1.2 christos static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
3261 1.2 christos unsigned int max_id_len,
3262 1.2 christos unsigned char *psk,
3263 1.2 christos unsigned int max_psk_len)
3264 1.2 christos {
3265 1.2 christos unsigned int psklen = 0;
3266 1.2 christos
3267 1.2 christos psk_client_cb_cnt++;
3268 1.2 christos
3269 1.2 christos if (strlen(pskid) + 1 > max_id_len)
3270 1.2 christos return 0;
3271 1.2 christos
3272 1.2 christos /* We should only ever be called a maximum of twice per connection */
3273 1.2 christos if (psk_client_cb_cnt > 2)
3274 1.2 christos return 0;
3275 1.2 christos
3276 1.2 christos if (clientpsk == NULL)
3277 1.2 christos return 0;
3278 1.2 christos
3279 1.2 christos /* We'll reuse the PSK we set up for TLSv1.3 */
3280 1.2 christos if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
3281 1.2 christos return 0;
3282 1.2 christos psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
3283 1.2 christos strncpy(id, pskid, max_id_len);
3284 1.2 christos
3285 1.2 christos return psklen;
3286 1.2 christos }
3287 1.2 christos #endif /* OPENSSL_NO_PSK */
3288 1.2 christos
3289 1.2 christos static int find_session_cb(SSL *ssl, const unsigned char *identity,
3290 1.2 christos size_t identity_len, SSL_SESSION **sess)
3291 1.2 christos {
3292 1.2 christos find_session_cb_cnt++;
3293 1.2 christos
3294 1.2 christos /* We should only ever be called a maximum of twice per connection */
3295 1.2 christos if (find_session_cb_cnt > 2)
3296 1.2 christos return 0;
3297 1.2 christos
3298 1.2 christos if (serverpsk == NULL)
3299 1.2 christos return 0;
3300 1.2 christos
3301 1.2 christos /* Identity should match that set by the client */
3302 1.2 christos if (strlen(srvid) != identity_len
3303 1.2 christos || strncmp(srvid, (const char *)identity, identity_len) != 0) {
3304 1.2 christos /* No PSK found, continue but without a PSK */
3305 1.2 christos *sess = NULL;
3306 1.2 christos return 1;
3307 1.2 christos }
3308 1.2 christos
3309 1.2 christos SSL_SESSION_up_ref(serverpsk);
3310 1.2 christos *sess = serverpsk;
3311 1.2 christos
3312 1.2 christos return 1;
3313 1.2 christos }
3314 1.2 christos
3315 1.2 christos #ifndef OPENSSL_NO_PSK
3316 1.2 christos static unsigned int psk_server_cb(SSL *ssl, const char *identity,
3317 1.2 christos unsigned char *psk, unsigned int max_psk_len)
3318 1.2 christos {
3319 1.2 christos unsigned int psklen = 0;
3320 1.2 christos
3321 1.2 christos psk_server_cb_cnt++;
3322 1.2 christos
3323 1.2 christos /* We should only ever be called a maximum of twice per connection */
3324 1.2 christos if (find_session_cb_cnt > 2)
3325 1.2 christos return 0;
3326 1.2 christos
3327 1.2 christos if (serverpsk == NULL)
3328 1.2 christos return 0;
3329 1.2 christos
3330 1.2 christos /* Identity should match that set by the client */
3331 1.2 christos if (strcmp(srvid, identity) != 0) {
3332 1.2 christos return 0;
3333 1.2 christos }
3334 1.2 christos
3335 1.2 christos /* We'll reuse the PSK we set up for TLSv1.3 */
3336 1.2 christos if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
3337 1.2 christos return 0;
3338 1.2 christos psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
3339 1.2 christos
3340 1.2 christos return psklen;
3341 1.2 christos }
3342 1.2 christos #endif /* OPENSSL_NO_PSK */
3343 1.2 christos
3344 1.2 christos #define MSG1 "Hello"
3345 1.2 christos #define MSG2 "World."
3346 1.2 christos #define MSG3 "This"
3347 1.2 christos #define MSG4 "is"
3348 1.2 christos #define MSG5 "a"
3349 1.2 christos #define MSG6 "test"
3350 1.2 christos #define MSG7 "message."
3351 1.2 christos
3352 1.2 christos #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
3353 1.2 christos #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
3354 1.2 christos #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
3355 1.2 christos #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
3356 1.2 christos #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
3357 1.2 christos
3358 1.2 christos
3359 1.2 christos static SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize)
3360 1.2 christos {
3361 1.2 christos const SSL_CIPHER *cipher = NULL;
3362 1.2 christos const unsigned char key[] = {
3363 1.2 christos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
3364 1.2 christos 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
3365 1.2 christos 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
3366 1.2 christos 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
3367 1.2 christos 0x2c, 0x2d, 0x2e, 0x2f /* SHA384_DIGEST_LENGTH bytes */
3368 1.2 christos };
3369 1.2 christos SSL_SESSION *sess = NULL;
3370 1.2 christos
3371 1.2 christos if (mdsize == SHA384_DIGEST_LENGTH) {
3372 1.2 christos cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
3373 1.2 christos } else if (mdsize == SHA256_DIGEST_LENGTH) {
3374 1.2 christos /*
3375 1.2 christos * Any ciphersuite using SHA256 will do - it will be compatible with
3376 1.2 christos * the actual ciphersuite selected as long as it too is based on SHA256
3377 1.2 christos */
3378 1.2 christos cipher = SSL_CIPHER_find(ssl, TLS13_AES_128_GCM_SHA256_BYTES);
3379 1.2 christos } else {
3380 1.2 christos /* Should not happen */
3381 1.2 christos return NULL;
3382 1.2 christos }
3383 1.2 christos sess = SSL_SESSION_new();
3384 1.2 christos if (!TEST_ptr(sess)
3385 1.2 christos || !TEST_ptr(cipher)
3386 1.2 christos || !TEST_true(SSL_SESSION_set1_master_key(sess, key, mdsize))
3387 1.2 christos || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
3388 1.2 christos || !TEST_true(
3389 1.2 christos SSL_SESSION_set_protocol_version(sess,
3390 1.2 christos TLS1_3_VERSION))) {
3391 1.2 christos SSL_SESSION_free(sess);
3392 1.2 christos return NULL;
3393 1.2 christos }
3394 1.2 christos return sess;
3395 1.2 christos }
3396 1.2 christos
3397 1.2 christos /*
3398 1.2 christos * Helper method to setup objects for early data test. Caller frees objects on
3399 1.2 christos * error.
3400 1.2 christos */
3401 1.2 christos static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
3402 1.2 christos SSL **serverssl, SSL_SESSION **sess, int idx,
3403 1.2 christos size_t mdsize)
3404 1.2 christos {
3405 1.2 christos if (*sctx == NULL
3406 1.2 christos && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3407 1.2 christos TLS_client_method(),
3408 1.2 christos TLS1_VERSION, 0,
3409 1.2 christos sctx, cctx, cert, privkey)))
3410 1.2 christos return 0;
3411 1.2 christos
3412 1.2 christos if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
3413 1.2 christos return 0;
3414 1.2 christos
3415 1.2 christos if (idx == 1) {
3416 1.2 christos /* When idx == 1 we repeat the tests with read_ahead set */
3417 1.2 christos SSL_CTX_set_read_ahead(*cctx, 1);
3418 1.2 christos SSL_CTX_set_read_ahead(*sctx, 1);
3419 1.2 christos } else if (idx == 2) {
3420 1.2 christos /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
3421 1.2 christos SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
3422 1.2 christos SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
3423 1.2 christos use_session_cb_cnt = 0;
3424 1.2 christos find_session_cb_cnt = 0;
3425 1.2 christos srvid = pskid;
3426 1.2 christos }
3427 1.2 christos
3428 1.2 christos if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
3429 1.2 christos NULL, NULL)))
3430 1.2 christos return 0;
3431 1.2 christos
3432 1.2 christos /*
3433 1.2 christos * For one of the run throughs (doesn't matter which one), we'll try sending
3434 1.2 christos * some SNI data in the initial ClientHello. This will be ignored (because
3435 1.2 christos * there is no SNI cb set up by the server), so it should not impact
3436 1.2 christos * early_data.
3437 1.2 christos */
3438 1.2 christos if (idx == 1
3439 1.2 christos && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
3440 1.2 christos return 0;
3441 1.2 christos
3442 1.2 christos if (idx == 2) {
3443 1.2 christos clientpsk = create_a_psk(*clientssl, mdsize);
3444 1.2 christos if (!TEST_ptr(clientpsk)
3445 1.2 christos /*
3446 1.2 christos * We just choose an arbitrary value for max_early_data which
3447 1.2 christos * should be big enough for testing purposes.
3448 1.2 christos */
3449 1.2 christos || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
3450 1.2 christos 0x100))
3451 1.2 christos || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3452 1.2 christos SSL_SESSION_free(clientpsk);
3453 1.2 christos clientpsk = NULL;
3454 1.2 christos return 0;
3455 1.2 christos }
3456 1.2 christos serverpsk = clientpsk;
3457 1.2 christos
3458 1.2 christos if (sess != NULL) {
3459 1.2 christos if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3460 1.2 christos SSL_SESSION_free(clientpsk);
3461 1.2 christos SSL_SESSION_free(serverpsk);
3462 1.2 christos clientpsk = serverpsk = NULL;
3463 1.2 christos return 0;
3464 1.2 christos }
3465 1.2 christos *sess = clientpsk;
3466 1.2 christos }
3467 1.2 christos return 1;
3468 1.2 christos }
3469 1.2 christos
3470 1.2 christos if (sess == NULL)
3471 1.2 christos return 1;
3472 1.2 christos
3473 1.2 christos if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
3474 1.2 christos SSL_ERROR_NONE)))
3475 1.2 christos return 0;
3476 1.2 christos
3477 1.2 christos *sess = SSL_get1_session(*clientssl);
3478 1.2 christos SSL_shutdown(*clientssl);
3479 1.2 christos SSL_shutdown(*serverssl);
3480 1.2 christos SSL_free(*serverssl);
3481 1.2 christos SSL_free(*clientssl);
3482 1.2 christos *serverssl = *clientssl = NULL;
3483 1.2 christos
3484 1.2 christos if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
3485 1.2 christos clientssl, NULL, NULL))
3486 1.2 christos || !TEST_true(SSL_set_session(*clientssl, *sess)))
3487 1.2 christos return 0;
3488 1.2 christos
3489 1.2 christos return 1;
3490 1.2 christos }
3491 1.2 christos
3492 1.2 christos static int check_early_data_timeout(time_t timer)
3493 1.2 christos {
3494 1.2 christos int res = 0;
3495 1.2 christos
3496 1.2 christos /*
3497 1.2 christos * Early data is time sensitive. We have an approx 8 second allowance
3498 1.2 christos * between writing the early data and reading it. If we exceed that time
3499 1.2 christos * then this test will fail. This can sometimes (rarely) occur in normal CI
3500 1.2 christos * operation. We can try and detect this and just ignore the result of this
3501 1.2 christos * test if it has taken too long. We assume anything over 7 seconds is too
3502 1.2 christos * long
3503 1.2 christos */
3504 1.2 christos timer = time(NULL) - timer;
3505 1.2 christos if (timer >= 7)
3506 1.2 christos res = TEST_skip("Test took too long, ignoring result");
3507 1.2 christos
3508 1.2 christos return res;
3509 1.2 christos }
3510 1.2 christos
3511 1.2 christos static int test_early_data_read_write(int idx)
3512 1.2 christos {
3513 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
3514 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
3515 1.2 christos int testresult = 0;
3516 1.2 christos SSL_SESSION *sess = NULL;
3517 1.2 christos unsigned char buf[20], data[1024];
3518 1.2 christos size_t readbytes, written, eoedlen, rawread, rawwritten;
3519 1.2 christos BIO *rbio;
3520 1.2 christos time_t timer;
3521 1.2 christos
3522 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3523 1.2 christos &serverssl, &sess, idx,
3524 1.2 christos SHA384_DIGEST_LENGTH)))
3525 1.2 christos goto end;
3526 1.2 christos
3527 1.2 christos /* Write and read some early data */
3528 1.2 christos timer = time(NULL);
3529 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3530 1.2 christos &written))
3531 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1)))
3532 1.2 christos goto end;
3533 1.2 christos
3534 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3535 1.2 christos &readbytes),
3536 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)) {
3537 1.2 christos testresult = check_early_data_timeout(timer);
3538 1.2 christos goto end;
3539 1.2 christos }
3540 1.2 christos
3541 1.2 christos if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
3542 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3543 1.2 christos SSL_EARLY_DATA_ACCEPTED))
3544 1.2 christos goto end;
3545 1.2 christos
3546 1.2 christos /*
3547 1.2 christos * Server should be able to write data, and client should be able to
3548 1.2 christos * read it.
3549 1.2 christos */
3550 1.2 christos if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
3551 1.2 christos &written))
3552 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2))
3553 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3554 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3555 1.2 christos goto end;
3556 1.2 christos
3557 1.2 christos /* Even after reading normal data, client should be able write early data */
3558 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
3559 1.2 christos &written))
3560 1.2 christos || !TEST_size_t_eq(written, strlen(MSG3)))
3561 1.2 christos goto end;
3562 1.2 christos
3563 1.2 christos /* Server should still be able read early data after writing data */
3564 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3565 1.2 christos &readbytes),
3566 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)
3567 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
3568 1.2 christos goto end;
3569 1.2 christos
3570 1.2 christos /* Write more data from server and read it from client */
3571 1.2 christos if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
3572 1.2 christos &written))
3573 1.2 christos || !TEST_size_t_eq(written, strlen(MSG4))
3574 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3575 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
3576 1.2 christos goto end;
3577 1.2 christos
3578 1.2 christos /*
3579 1.2 christos * If client writes normal data it should mean writing early data is no
3580 1.2 christos * longer possible.
3581 1.2 christos */
3582 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3583 1.2 christos || !TEST_size_t_eq(written, strlen(MSG5))
3584 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3585 1.2 christos SSL_EARLY_DATA_ACCEPTED))
3586 1.2 christos goto end;
3587 1.2 christos
3588 1.2 christos /*
3589 1.2 christos * At this point the client has written EndOfEarlyData, ClientFinished and
3590 1.2 christos * normal (fully protected) data. We are going to cause a delay between the
3591 1.2 christos * arrival of EndOfEarlyData and ClientFinished. We read out all the data
3592 1.2 christos * in the read BIO, and then just put back the EndOfEarlyData message.
3593 1.2 christos */
3594 1.2 christos rbio = SSL_get_rbio(serverssl);
3595 1.2 christos if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
3596 1.2 christos || !TEST_size_t_lt(rawread, sizeof(data))
3597 1.2 christos || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
3598 1.2 christos goto end;
3599 1.2 christos
3600 1.2 christos /* Record length is in the 4th and 5th bytes of the record header */
3601 1.2 christos eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
3602 1.2 christos if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
3603 1.2 christos || !TEST_size_t_eq(rawwritten, eoedlen))
3604 1.2 christos goto end;
3605 1.2 christos
3606 1.2 christos /* Server should be told that there is no more early data */
3607 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3608 1.2 christos &readbytes),
3609 1.2 christos SSL_READ_EARLY_DATA_FINISH)
3610 1.2 christos || !TEST_size_t_eq(readbytes, 0))
3611 1.2 christos goto end;
3612 1.2 christos
3613 1.2 christos /*
3614 1.2 christos * Server has not finished init yet, so should still be able to write early
3615 1.2 christos * data.
3616 1.2 christos */
3617 1.2 christos if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
3618 1.2 christos &written))
3619 1.2 christos || !TEST_size_t_eq(written, strlen(MSG6)))
3620 1.2 christos goto end;
3621 1.2 christos
3622 1.2 christos /* Push the ClientFinished and the normal data back into the server rbio */
3623 1.2 christos if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
3624 1.2 christos &rawwritten))
3625 1.2 christos || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
3626 1.2 christos goto end;
3627 1.2 christos
3628 1.2 christos /* Server should be able to read normal data */
3629 1.2 christos if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3630 1.2 christos || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3631 1.2 christos goto end;
3632 1.2 christos
3633 1.2 christos /* Client and server should not be able to write/read early data now */
3634 1.2 christos if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3635 1.2 christos &written)))
3636 1.2 christos goto end;
3637 1.2 christos ERR_clear_error();
3638 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3639 1.2 christos &readbytes),
3640 1.2 christos SSL_READ_EARLY_DATA_ERROR))
3641 1.2 christos goto end;
3642 1.2 christos ERR_clear_error();
3643 1.2 christos
3644 1.2 christos /* Client should be able to read the data sent by the server */
3645 1.2 christos if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3646 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
3647 1.2 christos goto end;
3648 1.2 christos
3649 1.2 christos /*
3650 1.2 christos * Make sure we process the two NewSessionTickets. These arrive
3651 1.2 christos * post-handshake. We attempt reads which we do not expect to return any
3652 1.2 christos * data.
3653 1.2 christos */
3654 1.2 christos if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3655 1.2 christos || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
3656 1.2 christos &readbytes)))
3657 1.2 christos goto end;
3658 1.2 christos
3659 1.2 christos /* Server should be able to write normal data */
3660 1.2 christos if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
3661 1.2 christos || !TEST_size_t_eq(written, strlen(MSG7))
3662 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3663 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
3664 1.2 christos goto end;
3665 1.2 christos
3666 1.2 christos SSL_SESSION_free(sess);
3667 1.2 christos sess = SSL_get1_session(clientssl);
3668 1.2 christos use_session_cb_cnt = 0;
3669 1.2 christos find_session_cb_cnt = 0;
3670 1.2 christos
3671 1.2 christos SSL_shutdown(clientssl);
3672 1.2 christos SSL_shutdown(serverssl);
3673 1.2 christos SSL_free(serverssl);
3674 1.2 christos SSL_free(clientssl);
3675 1.2 christos serverssl = clientssl = NULL;
3676 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3677 1.2 christos &clientssl, NULL, NULL))
3678 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess)))
3679 1.2 christos goto end;
3680 1.2 christos
3681 1.2 christos /* Write and read some early data */
3682 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3683 1.2 christos &written))
3684 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1))
3685 1.2 christos || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3686 1.2 christos &readbytes),
3687 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)
3688 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
3689 1.2 christos goto end;
3690 1.2 christos
3691 1.2 christos if (!TEST_int_gt(SSL_connect(clientssl), 0)
3692 1.2 christos || !TEST_int_gt(SSL_accept(serverssl), 0))
3693 1.2 christos goto end;
3694 1.2 christos
3695 1.2 christos /* Client and server should not be able to write/read early data now */
3696 1.2 christos if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3697 1.2 christos &written)))
3698 1.2 christos goto end;
3699 1.2 christos ERR_clear_error();
3700 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3701 1.2 christos &readbytes),
3702 1.2 christos SSL_READ_EARLY_DATA_ERROR))
3703 1.2 christos goto end;
3704 1.2 christos ERR_clear_error();
3705 1.2 christos
3706 1.2 christos /* Client and server should be able to write/read normal data */
3707 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3708 1.2 christos || !TEST_size_t_eq(written, strlen(MSG5))
3709 1.2 christos || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3710 1.2 christos || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3711 1.2 christos goto end;
3712 1.2 christos
3713 1.2 christos testresult = 1;
3714 1.2 christos
3715 1.2 christos end:
3716 1.2 christos SSL_SESSION_free(sess);
3717 1.2 christos SSL_SESSION_free(clientpsk);
3718 1.2 christos SSL_SESSION_free(serverpsk);
3719 1.2 christos clientpsk = serverpsk = NULL;
3720 1.2 christos SSL_free(serverssl);
3721 1.2 christos SSL_free(clientssl);
3722 1.2 christos SSL_CTX_free(sctx);
3723 1.2 christos SSL_CTX_free(cctx);
3724 1.2 christos return testresult;
3725 1.2 christos }
3726 1.2 christos
3727 1.2 christos static int allow_ed_cb_called = 0;
3728 1.2 christos
3729 1.2 christos static int allow_early_data_cb(SSL *s, void *arg)
3730 1.2 christos {
3731 1.2 christos int *usecb = (int *)arg;
3732 1.2 christos
3733 1.2 christos allow_ed_cb_called++;
3734 1.2 christos
3735 1.2 christos if (*usecb == 1)
3736 1.2 christos return 0;
3737 1.2 christos
3738 1.2 christos return 1;
3739 1.2 christos }
3740 1.2 christos
3741 1.2 christos /*
3742 1.2 christos * idx == 0: Standard early_data setup
3743 1.2 christos * idx == 1: early_data setup using read_ahead
3744 1.2 christos * usecb == 0: Don't use a custom early data callback
3745 1.2 christos * usecb == 1: Use a custom early data callback and reject the early data
3746 1.2 christos * usecb == 2: Use a custom early data callback and accept the early data
3747 1.2 christos * confopt == 0: Configure anti-replay directly
3748 1.2 christos * confopt == 1: Configure anti-replay using SSL_CONF
3749 1.2 christos */
3750 1.2 christos static int test_early_data_replay_int(int idx, int usecb, int confopt)
3751 1.2 christos {
3752 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
3753 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
3754 1.2 christos int testresult = 0;
3755 1.2 christos SSL_SESSION *sess = NULL;
3756 1.2 christos size_t readbytes, written;
3757 1.2 christos unsigned char buf[20];
3758 1.2 christos time_t timer;
3759 1.2 christos
3760 1.2 christos allow_ed_cb_called = 0;
3761 1.2 christos
3762 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3763 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
3764 1.2 christos &sctx, &cctx, cert, privkey)))
3765 1.2 christos return 0;
3766 1.2 christos
3767 1.2 christos if (usecb > 0) {
3768 1.2 christos if (confopt == 0) {
3769 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
3770 1.2 christos } else {
3771 1.2 christos SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
3772 1.2 christos
3773 1.2 christos if (!TEST_ptr(confctx))
3774 1.2 christos goto end;
3775 1.2 christos SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
3776 1.2 christos | SSL_CONF_FLAG_SERVER);
3777 1.2 christos SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
3778 1.2 christos if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
3779 1.2 christos 2)) {
3780 1.2 christos SSL_CONF_CTX_free(confctx);
3781 1.2 christos goto end;
3782 1.2 christos }
3783 1.2 christos SSL_CONF_CTX_free(confctx);
3784 1.2 christos }
3785 1.2 christos SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
3786 1.2 christos }
3787 1.2 christos
3788 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3789 1.2 christos &serverssl, &sess, idx,
3790 1.2 christos SHA384_DIGEST_LENGTH)))
3791 1.2 christos goto end;
3792 1.2 christos
3793 1.2 christos /*
3794 1.2 christos * The server is configured to accept early data. Create a connection to
3795 1.2 christos * "use up" the ticket
3796 1.2 christos */
3797 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3798 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
3799 1.2 christos goto end;
3800 1.2 christos
3801 1.2 christos SSL_shutdown(clientssl);
3802 1.2 christos SSL_shutdown(serverssl);
3803 1.2 christos SSL_free(serverssl);
3804 1.2 christos SSL_free(clientssl);
3805 1.2 christos serverssl = clientssl = NULL;
3806 1.2 christos
3807 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3808 1.2 christos &clientssl, NULL, NULL))
3809 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess)))
3810 1.2 christos goto end;
3811 1.2 christos
3812 1.2 christos /* Write and read some early data */
3813 1.2 christos timer = time(NULL);
3814 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3815 1.2 christos &written))
3816 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1)))
3817 1.2 christos goto end;
3818 1.2 christos
3819 1.2 christos if (usecb <= 1) {
3820 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3821 1.2 christos &readbytes),
3822 1.2 christos SSL_READ_EARLY_DATA_FINISH)
3823 1.2 christos /*
3824 1.2 christos * The ticket was reused, so the we should have rejected the
3825 1.2 christos * early data
3826 1.2 christos */
3827 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3828 1.2 christos SSL_EARLY_DATA_REJECTED))
3829 1.2 christos goto end;
3830 1.2 christos } else {
3831 1.2 christos /* In this case the callback decides to accept the early data */
3832 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3833 1.2 christos &readbytes),
3834 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)) {
3835 1.2 christos testresult = check_early_data_timeout(timer);
3836 1.2 christos goto end;
3837 1.2 christos }
3838 1.2 christos if (!TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
3839 1.2 christos /*
3840 1.2 christos * Server will have sent its flight so client can now send
3841 1.2 christos * end of early data and complete its half of the handshake
3842 1.2 christos */
3843 1.2 christos || !TEST_int_gt(SSL_connect(clientssl), 0)
3844 1.2 christos || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3845 1.2 christos &readbytes),
3846 1.2 christos SSL_READ_EARLY_DATA_FINISH)
3847 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3848 1.2 christos SSL_EARLY_DATA_ACCEPTED))
3849 1.2 christos goto end;
3850 1.2 christos }
3851 1.2 christos
3852 1.2 christos /* Complete the connection */
3853 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3854 1.2 christos || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
3855 1.2 christos || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
3856 1.2 christos goto end;
3857 1.2 christos
3858 1.2 christos testresult = 1;
3859 1.2 christos
3860 1.2 christos end:
3861 1.2 christos SSL_SESSION_free(sess);
3862 1.2 christos SSL_SESSION_free(clientpsk);
3863 1.2 christos SSL_SESSION_free(serverpsk);
3864 1.2 christos clientpsk = serverpsk = NULL;
3865 1.2 christos SSL_free(serverssl);
3866 1.2 christos SSL_free(clientssl);
3867 1.2 christos SSL_CTX_free(sctx);
3868 1.2 christos SSL_CTX_free(cctx);
3869 1.2 christos return testresult;
3870 1.2 christos }
3871 1.2 christos
3872 1.2 christos static int test_early_data_replay(int idx)
3873 1.2 christos {
3874 1.2 christos int ret = 1, usecb, confopt;
3875 1.2 christos
3876 1.2 christos for (usecb = 0; usecb < 3; usecb++) {
3877 1.2 christos for (confopt = 0; confopt < 2; confopt++)
3878 1.2 christos ret &= test_early_data_replay_int(idx, usecb, confopt);
3879 1.2 christos }
3880 1.2 christos
3881 1.2 christos return ret;
3882 1.2 christos }
3883 1.2 christos
3884 1.2 christos static const char *ciphersuites[] = {
3885 1.2 christos "TLS_AES_128_CCM_8_SHA256",
3886 1.2 christos "TLS_AES_128_GCM_SHA256",
3887 1.2 christos "TLS_AES_256_GCM_SHA384",
3888 1.2 christos "TLS_AES_128_CCM_SHA256",
3889 1.2 christos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
3890 1.2 christos "TLS_CHACHA20_POLY1305_SHA256"
3891 1.2 christos #endif
3892 1.2 christos };
3893 1.2 christos
3894 1.2 christos /*
3895 1.2 christos * Helper function to test that a server attempting to read early data can
3896 1.2 christos * handle a connection from a client where the early data should be skipped.
3897 1.2 christos * testtype: 0 == No HRR
3898 1.2 christos * testtype: 1 == HRR
3899 1.2 christos * testtype: 2 == HRR, invalid early_data sent after HRR
3900 1.2 christos * testtype: 3 == recv_max_early_data set to 0
3901 1.2 christos */
3902 1.2 christos static int early_data_skip_helper(int testtype, int cipher, int idx)
3903 1.2 christos {
3904 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
3905 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
3906 1.2 christos int testresult = 0;
3907 1.2 christos SSL_SESSION *sess = NULL;
3908 1.2 christos unsigned char buf[20];
3909 1.2 christos size_t readbytes, written;
3910 1.2 christos
3911 1.2 christos if (is_fips && cipher == 4)
3912 1.2 christos return 1;
3913 1.2 christos
3914 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3915 1.2 christos TLS_client_method(),
3916 1.2 christos TLS1_VERSION, 0,
3917 1.2 christos &sctx, &cctx, cert, privkey)))
3918 1.2 christos goto end;
3919 1.2 christos
3920 1.2 christos if (cipher == 0) {
3921 1.2 christos SSL_CTX_set_security_level(sctx, 0);
3922 1.2 christos SSL_CTX_set_security_level(cctx, 0);
3923 1.2 christos }
3924 1.2 christos
3925 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher]))
3926 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher])))
3927 1.2 christos goto end;
3928 1.2 christos
3929 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3930 1.2 christos &serverssl, &sess, idx,
3931 1.2 christos cipher == 2 ? SHA384_DIGEST_LENGTH
3932 1.2 christos : SHA256_DIGEST_LENGTH)))
3933 1.2 christos goto end;
3934 1.2 christos
3935 1.2 christos if (testtype == 1 || testtype == 2) {
3936 1.2 christos /* Force an HRR to occur */
3937 1.2 christos #if defined(OPENSSL_NO_EC)
3938 1.2 christos if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
3939 1.2 christos goto end;
3940 1.2 christos #else
3941 1.2 christos if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
3942 1.2 christos goto end;
3943 1.2 christos #endif
3944 1.2 christos } else if (idx == 2) {
3945 1.2 christos /*
3946 1.2 christos * We force early_data rejection by ensuring the PSK identity is
3947 1.2 christos * unrecognised
3948 1.2 christos */
3949 1.2 christos srvid = "Dummy Identity";
3950 1.2 christos } else {
3951 1.2 christos /*
3952 1.2 christos * Deliberately corrupt the creation time. We take 20 seconds off the
3953 1.2 christos * time. It could be any value as long as it is not within tolerance.
3954 1.2 christos * This should mean the ticket is rejected.
3955 1.2 christos */
3956 1.2 christos if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
3957 1.2 christos goto end;
3958 1.2 christos }
3959 1.2 christos
3960 1.2 christos if (testtype == 3
3961 1.2 christos && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
3962 1.2 christos goto end;
3963 1.2 christos
3964 1.2 christos /* Write some early data */
3965 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3966 1.2 christos &written))
3967 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1)))
3968 1.2 christos goto end;
3969 1.2 christos
3970 1.2 christos /* Server should reject the early data */
3971 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3972 1.2 christos &readbytes),
3973 1.2 christos SSL_READ_EARLY_DATA_FINISH)
3974 1.2 christos || !TEST_size_t_eq(readbytes, 0)
3975 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3976 1.2 christos SSL_EARLY_DATA_REJECTED))
3977 1.2 christos goto end;
3978 1.2 christos
3979 1.2 christos switch (testtype) {
3980 1.2 christos case 0:
3981 1.2 christos /* Nothing to do */
3982 1.2 christos break;
3983 1.2 christos
3984 1.2 christos case 1:
3985 1.2 christos /*
3986 1.2 christos * Finish off the handshake. We perform the same writes and reads as
3987 1.2 christos * further down but we expect them to fail due to the incomplete
3988 1.2 christos * handshake.
3989 1.2 christos */
3990 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3991 1.2 christos || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
3992 1.2 christos &readbytes)))
3993 1.2 christos goto end;
3994 1.2 christos break;
3995 1.2 christos
3996 1.2 christos case 2:
3997 1.2 christos {
3998 1.2 christos BIO *wbio = SSL_get_wbio(clientssl);
3999 1.2 christos /* A record that will appear as bad early_data */
4000 1.2 christos const unsigned char bad_early_data[] = {
4001 1.2 christos 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
4002 1.2 christos };
4003 1.2 christos
4004 1.2 christos /*
4005 1.2 christos * We force the client to attempt a write. This will fail because
4006 1.2 christos * we're still in the handshake. It will cause the second
4007 1.2 christos * ClientHello to be sent.
4008 1.2 christos */
4009 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
4010 1.2 christos &written)))
4011 1.2 christos goto end;
4012 1.2 christos
4013 1.2 christos /*
4014 1.2 christos * Inject some early_data after the second ClientHello. This should
4015 1.2 christos * cause the server to fail
4016 1.2 christos */
4017 1.2 christos if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
4018 1.2 christos sizeof(bad_early_data), &written)))
4019 1.2 christos goto end;
4020 1.2 christos }
4021 1.2 christos /* fallthrough */
4022 1.2 christos
4023 1.2 christos case 3:
4024 1.2 christos /*
4025 1.2 christos * This client has sent more early_data than we are willing to skip
4026 1.2 christos * (case 3) or sent invalid early_data (case 2) so the connection should
4027 1.2 christos * abort.
4028 1.2 christos */
4029 1.2 christos if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4030 1.2 christos || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
4031 1.2 christos goto end;
4032 1.2 christos
4033 1.2 christos /* Connection has failed - nothing more to do */
4034 1.2 christos testresult = 1;
4035 1.2 christos goto end;
4036 1.2 christos
4037 1.2 christos default:
4038 1.2 christos TEST_error("Invalid test type");
4039 1.2 christos goto end;
4040 1.2 christos }
4041 1.2 christos
4042 1.2 christos ERR_clear_error();
4043 1.2 christos /*
4044 1.2 christos * Should be able to send normal data despite rejection of early data. The
4045 1.2 christos * early_data should be skipped.
4046 1.2 christos */
4047 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4048 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2))
4049 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4050 1.2 christos SSL_EARLY_DATA_REJECTED)
4051 1.2 christos || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4052 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4053 1.2 christos goto end;
4054 1.2 christos
4055 1.2 christos /*
4056 1.2 christos * Failure to decrypt early data records should not leave spurious errors
4057 1.2 christos * on the error stack
4058 1.2 christos */
4059 1.2 christos if (!TEST_long_eq(ERR_peek_error(), 0))
4060 1.2 christos goto end;
4061 1.2 christos
4062 1.2 christos testresult = 1;
4063 1.2 christos
4064 1.2 christos end:
4065 1.2 christos SSL_SESSION_free(clientpsk);
4066 1.2 christos SSL_SESSION_free(serverpsk);
4067 1.2 christos clientpsk = serverpsk = NULL;
4068 1.2 christos SSL_SESSION_free(sess);
4069 1.2 christos SSL_free(serverssl);
4070 1.2 christos SSL_free(clientssl);
4071 1.2 christos SSL_CTX_free(sctx);
4072 1.2 christos SSL_CTX_free(cctx);
4073 1.2 christos return testresult;
4074 1.2 christos }
4075 1.2 christos
4076 1.2 christos /*
4077 1.2 christos * Test that a server attempting to read early data can handle a connection
4078 1.2 christos * from a client where the early data is not acceptable.
4079 1.2 christos */
4080 1.2 christos static int test_early_data_skip(int idx)
4081 1.2 christos {
4082 1.2 christos return early_data_skip_helper(0,
4083 1.2 christos idx % OSSL_NELEM(ciphersuites),
4084 1.2 christos idx / OSSL_NELEM(ciphersuites));
4085 1.2 christos }
4086 1.2 christos
4087 1.2 christos /*
4088 1.2 christos * Test that a server attempting to read early data can handle a connection
4089 1.2 christos * from a client where an HRR occurs.
4090 1.2 christos */
4091 1.2 christos static int test_early_data_skip_hrr(int idx)
4092 1.2 christos {
4093 1.2 christos return early_data_skip_helper(1,
4094 1.2 christos idx % OSSL_NELEM(ciphersuites),
4095 1.2 christos idx / OSSL_NELEM(ciphersuites));
4096 1.2 christos }
4097 1.2 christos
4098 1.2 christos /*
4099 1.2 christos * Test that a server attempting to read early data can handle a connection
4100 1.2 christos * from a client where an HRR occurs and correctly fails if early_data is sent
4101 1.2 christos * after the HRR
4102 1.2 christos */
4103 1.2 christos static int test_early_data_skip_hrr_fail(int idx)
4104 1.2 christos {
4105 1.2 christos return early_data_skip_helper(2,
4106 1.2 christos idx % OSSL_NELEM(ciphersuites),
4107 1.2 christos idx / OSSL_NELEM(ciphersuites));
4108 1.2 christos }
4109 1.2 christos
4110 1.2 christos /*
4111 1.2 christos * Test that a server attempting to read early data will abort if it tries to
4112 1.2 christos * skip over too much.
4113 1.2 christos */
4114 1.2 christos static int test_early_data_skip_abort(int idx)
4115 1.2 christos {
4116 1.2 christos return early_data_skip_helper(3,
4117 1.2 christos idx % OSSL_NELEM(ciphersuites),
4118 1.2 christos idx / OSSL_NELEM(ciphersuites));
4119 1.2 christos }
4120 1.2 christos
4121 1.2 christos /*
4122 1.2 christos * Test that a server attempting to read early data can handle a connection
4123 1.2 christos * from a client that doesn't send any.
4124 1.2 christos */
4125 1.2 christos static int test_early_data_not_sent(int idx)
4126 1.2 christos {
4127 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4128 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4129 1.2 christos int testresult = 0;
4130 1.2 christos SSL_SESSION *sess = NULL;
4131 1.2 christos unsigned char buf[20];
4132 1.2 christos size_t readbytes, written;
4133 1.2 christos
4134 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4135 1.2 christos &serverssl, &sess, idx,
4136 1.2 christos SHA384_DIGEST_LENGTH)))
4137 1.2 christos goto end;
4138 1.2 christos
4139 1.2 christos /* Write some data - should block due to handshake with server */
4140 1.2 christos SSL_set_connect_state(clientssl);
4141 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4142 1.2 christos goto end;
4143 1.2 christos
4144 1.2 christos /* Server should detect that early data has not been sent */
4145 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4146 1.2 christos &readbytes),
4147 1.2 christos SSL_READ_EARLY_DATA_FINISH)
4148 1.2 christos || !TEST_size_t_eq(readbytes, 0)
4149 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4150 1.2 christos SSL_EARLY_DATA_NOT_SENT)
4151 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4152 1.2 christos SSL_EARLY_DATA_NOT_SENT))
4153 1.2 christos goto end;
4154 1.2 christos
4155 1.2 christos /* Continue writing the message we started earlier */
4156 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4157 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1))
4158 1.2 christos || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4159 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4160 1.2 christos || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
4161 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2)))
4162 1.2 christos goto end;
4163 1.2 christos
4164 1.2 christos if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
4165 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4166 1.2 christos goto end;
4167 1.2 christos
4168 1.2 christos testresult = 1;
4169 1.2 christos
4170 1.2 christos end:
4171 1.2 christos SSL_SESSION_free(sess);
4172 1.2 christos SSL_SESSION_free(clientpsk);
4173 1.2 christos SSL_SESSION_free(serverpsk);
4174 1.2 christos clientpsk = serverpsk = NULL;
4175 1.2 christos SSL_free(serverssl);
4176 1.2 christos SSL_free(clientssl);
4177 1.2 christos SSL_CTX_free(sctx);
4178 1.2 christos SSL_CTX_free(cctx);
4179 1.2 christos return testresult;
4180 1.2 christos }
4181 1.2 christos
4182 1.2 christos static const char *servalpn;
4183 1.2 christos
4184 1.2 christos static int alpn_select_cb(SSL *ssl, const unsigned char **out,
4185 1.2 christos unsigned char *outlen, const unsigned char *in,
4186 1.2 christos unsigned int inlen, void *arg)
4187 1.2 christos {
4188 1.2 christos unsigned int protlen = 0;
4189 1.2 christos const unsigned char *prot;
4190 1.2 christos
4191 1.2 christos for (prot = in; prot < in + inlen; prot += protlen) {
4192 1.2 christos protlen = *prot++;
4193 1.2 christos if (in + inlen < prot + protlen)
4194 1.2 christos return SSL_TLSEXT_ERR_NOACK;
4195 1.2 christos
4196 1.2 christos if (protlen == strlen(servalpn)
4197 1.2 christos && memcmp(prot, servalpn, protlen) == 0) {
4198 1.2 christos *out = prot;
4199 1.2 christos *outlen = protlen;
4200 1.2 christos return SSL_TLSEXT_ERR_OK;
4201 1.2 christos }
4202 1.2 christos }
4203 1.2 christos
4204 1.2 christos return SSL_TLSEXT_ERR_NOACK;
4205 1.2 christos }
4206 1.2 christos
4207 1.2 christos /* Test that a PSK can be used to send early_data */
4208 1.2 christos static int test_early_data_psk(int idx)
4209 1.2 christos {
4210 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4211 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4212 1.2 christos int testresult = 0;
4213 1.2 christos SSL_SESSION *sess = NULL;
4214 1.2 christos unsigned char alpnlist[] = {
4215 1.2 christos 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
4216 1.2 christos 'l', 'p', 'n'
4217 1.2 christos };
4218 1.2 christos #define GOODALPNLEN 9
4219 1.2 christos #define BADALPNLEN 8
4220 1.2 christos #define GOODALPN (alpnlist)
4221 1.2 christos #define BADALPN (alpnlist + GOODALPNLEN)
4222 1.2 christos int err = 0;
4223 1.2 christos unsigned char buf[20];
4224 1.2 christos size_t readbytes, written;
4225 1.2 christos int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
4226 1.2 christos int edstatus = SSL_EARLY_DATA_ACCEPTED;
4227 1.2 christos
4228 1.2 christos /* We always set this up with a final parameter of "2" for PSK */
4229 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4230 1.2 christos &serverssl, &sess, 2,
4231 1.2 christos SHA384_DIGEST_LENGTH)))
4232 1.2 christos goto end;
4233 1.2 christos
4234 1.2 christos servalpn = "goodalpn";
4235 1.2 christos
4236 1.2 christos /*
4237 1.2 christos * Note: There is no test for inconsistent SNI with late client detection.
4238 1.2 christos * This is because servers do not acknowledge SNI even if they are using
4239 1.2 christos * it in a resumption handshake - so it is not actually possible for a
4240 1.2 christos * client to detect a problem.
4241 1.2 christos */
4242 1.2 christos switch (idx) {
4243 1.2 christos case 0:
4244 1.2 christos /* Set inconsistent SNI (early client detection) */
4245 1.2 christos err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
4246 1.2 christos if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4247 1.2 christos || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
4248 1.2 christos goto end;
4249 1.2 christos break;
4250 1.2 christos
4251 1.2 christos case 1:
4252 1.2 christos /* Set inconsistent ALPN (early client detection) */
4253 1.2 christos err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
4254 1.2 christos /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
4255 1.2 christos if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
4256 1.2 christos GOODALPNLEN))
4257 1.2 christos || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
4258 1.2 christos BADALPNLEN)))
4259 1.2 christos goto end;
4260 1.2 christos break;
4261 1.2 christos
4262 1.2 christos case 2:
4263 1.2 christos /*
4264 1.2 christos * Set invalid protocol version. Technically this affects PSKs without
4265 1.2 christos * early_data too, but we test it here because it is similar to the
4266 1.2 christos * SNI/ALPN consistency tests.
4267 1.2 christos */
4268 1.2 christos err = SSL_R_BAD_PSK;
4269 1.2 christos if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
4270 1.2 christos goto end;
4271 1.2 christos break;
4272 1.2 christos
4273 1.2 christos case 3:
4274 1.2 christos /*
4275 1.2 christos * Set inconsistent SNI (server side). In this case the connection
4276 1.2 christos * will succeed and accept early_data. In TLSv1.3 on the server side SNI
4277 1.2 christos * is associated with each handshake - not the session. Therefore it
4278 1.2 christos * should not matter that we used a different server name last time.
4279 1.2 christos */
4280 1.2 christos SSL_SESSION_free(serverpsk);
4281 1.2 christos serverpsk = SSL_SESSION_dup(clientpsk);
4282 1.2 christos if (!TEST_ptr(serverpsk)
4283 1.2 christos || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
4284 1.2 christos goto end;
4285 1.2 christos /* Fall through */
4286 1.2 christos case 4:
4287 1.2 christos /* Set consistent SNI */
4288 1.2 christos if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4289 1.2 christos || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
4290 1.2 christos || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
4291 1.2 christos hostname_cb)))
4292 1.2 christos goto end;
4293 1.2 christos break;
4294 1.2 christos
4295 1.2 christos case 5:
4296 1.2 christos /*
4297 1.2 christos * Set inconsistent ALPN (server detected). In this case the connection
4298 1.2 christos * will succeed but reject early_data.
4299 1.2 christos */
4300 1.2 christos servalpn = "badalpn";
4301 1.2 christos edstatus = SSL_EARLY_DATA_REJECTED;
4302 1.2 christos readearlyres = SSL_READ_EARLY_DATA_FINISH;
4303 1.2 christos /* Fall through */
4304 1.2 christos case 6:
4305 1.2 christos /*
4306 1.2 christos * Set consistent ALPN.
4307 1.2 christos * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
4308 1.2 christos * accepts a list of protos (each one length prefixed).
4309 1.2 christos * SSL_set1_alpn_selected accepts a single protocol (not length
4310 1.2 christos * prefixed)
4311 1.2 christos */
4312 1.2 christos if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
4313 1.2 christos GOODALPNLEN - 1))
4314 1.2 christos || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
4315 1.2 christos GOODALPNLEN)))
4316 1.2 christos goto end;
4317 1.2 christos
4318 1.2 christos SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4319 1.2 christos break;
4320 1.2 christos
4321 1.2 christos case 7:
4322 1.2 christos /* Set inconsistent ALPN (late client detection) */
4323 1.2 christos SSL_SESSION_free(serverpsk);
4324 1.2 christos serverpsk = SSL_SESSION_dup(clientpsk);
4325 1.2 christos if (!TEST_ptr(serverpsk)
4326 1.2 christos || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
4327 1.2 christos BADALPN + 1,
4328 1.2 christos BADALPNLEN - 1))
4329 1.2 christos || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
4330 1.2 christos GOODALPN + 1,
4331 1.2 christos GOODALPNLEN - 1))
4332 1.2 christos || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
4333 1.2 christos sizeof(alpnlist))))
4334 1.2 christos goto end;
4335 1.2 christos SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4336 1.2 christos edstatus = SSL_EARLY_DATA_ACCEPTED;
4337 1.2 christos readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
4338 1.2 christos /* SSL_connect() call should fail */
4339 1.2 christos connectres = -1;
4340 1.2 christos break;
4341 1.2 christos
4342 1.2 christos default:
4343 1.2 christos TEST_error("Bad test index");
4344 1.2 christos goto end;
4345 1.2 christos }
4346 1.2 christos
4347 1.2 christos SSL_set_connect_state(clientssl);
4348 1.2 christos if (err != 0) {
4349 1.2 christos if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4350 1.2 christos &written))
4351 1.2 christos || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
4352 1.2 christos || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
4353 1.2 christos goto end;
4354 1.2 christos } else {
4355 1.2 christos time_t timer = time(NULL);
4356 1.2 christos
4357 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4358 1.2 christos &written)))
4359 1.2 christos goto end;
4360 1.2 christos
4361 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4362 1.2 christos &readbytes), readearlyres)) {
4363 1.2 christos testresult = check_early_data_timeout(timer);
4364 1.2 christos goto end;
4365 1.2 christos }
4366 1.2 christos
4367 1.2 christos if ((readearlyres == SSL_READ_EARLY_DATA_SUCCESS
4368 1.2 christos && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
4369 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
4370 1.2 christos || !TEST_int_eq(SSL_connect(clientssl), connectres))
4371 1.2 christos goto end;
4372 1.2 christos }
4373 1.2 christos
4374 1.2 christos testresult = 1;
4375 1.2 christos
4376 1.2 christos end:
4377 1.2 christos SSL_SESSION_free(sess);
4378 1.2 christos SSL_SESSION_free(clientpsk);
4379 1.2 christos SSL_SESSION_free(serverpsk);
4380 1.2 christos clientpsk = serverpsk = NULL;
4381 1.2 christos SSL_free(serverssl);
4382 1.2 christos SSL_free(clientssl);
4383 1.2 christos SSL_CTX_free(sctx);
4384 1.2 christos SSL_CTX_free(cctx);
4385 1.2 christos return testresult;
4386 1.2 christos }
4387 1.2 christos
4388 1.2 christos /*
4389 1.2 christos * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
4390 1.2 christos * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
4391 1.2 christos * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
4392 1.2 christos * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4393 1.2 christos * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
4394 1.2 christos * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
4395 1.2 christos */
4396 1.2 christos static int test_early_data_psk_with_all_ciphers(int idx)
4397 1.2 christos {
4398 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4399 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4400 1.2 christos int testresult = 0;
4401 1.2 christos SSL_SESSION *sess = NULL;
4402 1.2 christos unsigned char buf[20];
4403 1.2 christos size_t readbytes, written;
4404 1.2 christos const SSL_CIPHER *cipher;
4405 1.2 christos time_t timer;
4406 1.2 christos const char *cipher_str[] = {
4407 1.2 christos TLS1_3_RFC_AES_128_GCM_SHA256,
4408 1.2 christos TLS1_3_RFC_AES_256_GCM_SHA384,
4409 1.2 christos # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4410 1.2 christos TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4411 1.2 christos # else
4412 1.2 christos NULL,
4413 1.2 christos # endif
4414 1.2 christos TLS1_3_RFC_AES_128_CCM_SHA256,
4415 1.2 christos TLS1_3_RFC_AES_128_CCM_8_SHA256
4416 1.2 christos };
4417 1.2 christos const unsigned char *cipher_bytes[] = {
4418 1.2 christos TLS13_AES_128_GCM_SHA256_BYTES,
4419 1.2 christos TLS13_AES_256_GCM_SHA384_BYTES,
4420 1.2 christos # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4421 1.2 christos TLS13_CHACHA20_POLY1305_SHA256_BYTES,
4422 1.2 christos # else
4423 1.2 christos NULL,
4424 1.2 christos # endif
4425 1.2 christos TLS13_AES_128_CCM_SHA256_BYTES,
4426 1.2 christos TLS13_AES_128_CCM_8_SHA256_BYTES
4427 1.2 christos };
4428 1.2 christos
4429 1.2 christos if (cipher_str[idx] == NULL)
4430 1.2 christos return 1;
4431 1.2 christos /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
4432 1.2 christos if (idx == 2 && is_fips == 1)
4433 1.2 christos return 1;
4434 1.2 christos
4435 1.2 christos /* We always set this up with a final parameter of "2" for PSK */
4436 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4437 1.2 christos &serverssl, &sess, 2,
4438 1.2 christos SHA384_DIGEST_LENGTH)))
4439 1.2 christos goto end;
4440 1.2 christos
4441 1.2 christos if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
4442 1.2 christos || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
4443 1.2 christos goto end;
4444 1.2 christos
4445 1.2 christos /*
4446 1.2 christos * 'setupearly_data_test' creates only one instance of SSL_SESSION
4447 1.2 christos * and assigns to both client and server with incremented reference
4448 1.2 christos * and the same instance is updated in 'sess'.
4449 1.2 christos * So updating ciphersuite in 'sess' which will get reflected in
4450 1.2 christos * PSK handshake using psk use sess and find sess cb.
4451 1.2 christos */
4452 1.2 christos cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
4453 1.2 christos if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
4454 1.2 christos goto end;
4455 1.2 christos
4456 1.2 christos SSL_set_connect_state(clientssl);
4457 1.2 christos timer = time(NULL);
4458 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4459 1.2 christos &written)))
4460 1.2 christos goto end;
4461 1.2 christos
4462 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4463 1.2 christos &readbytes),
4464 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)) {
4465 1.2 christos testresult = check_early_data_timeout(timer);
4466 1.2 christos goto end;
4467 1.2 christos }
4468 1.2 christos
4469 1.2 christos if (!TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4470 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4471 1.2 christos SSL_EARLY_DATA_ACCEPTED)
4472 1.2 christos || !TEST_int_eq(SSL_connect(clientssl), 1)
4473 1.2 christos || !TEST_int_eq(SSL_accept(serverssl), 1))
4474 1.2 christos goto end;
4475 1.2 christos
4476 1.2 christos /* Send some normal data from client to server */
4477 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4478 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2)))
4479 1.2 christos goto end;
4480 1.2 christos
4481 1.2 christos if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4482 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4483 1.2 christos goto end;
4484 1.2 christos
4485 1.2 christos testresult = 1;
4486 1.2 christos end:
4487 1.2 christos SSL_SESSION_free(sess);
4488 1.2 christos SSL_SESSION_free(clientpsk);
4489 1.2 christos SSL_SESSION_free(serverpsk);
4490 1.2 christos clientpsk = serverpsk = NULL;
4491 1.2 christos if (clientssl != NULL)
4492 1.2 christos SSL_shutdown(clientssl);
4493 1.2 christos if (serverssl != NULL)
4494 1.2 christos SSL_shutdown(serverssl);
4495 1.2 christos SSL_free(serverssl);
4496 1.2 christos SSL_free(clientssl);
4497 1.2 christos SSL_CTX_free(sctx);
4498 1.2 christos SSL_CTX_free(cctx);
4499 1.2 christos return testresult;
4500 1.2 christos }
4501 1.2 christos
4502 1.2 christos /*
4503 1.2 christos * Test that a server that doesn't try to read early data can handle a
4504 1.2 christos * client sending some.
4505 1.2 christos */
4506 1.2 christos static int test_early_data_not_expected(int idx)
4507 1.2 christos {
4508 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4509 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4510 1.2 christos int testresult = 0;
4511 1.2 christos SSL_SESSION *sess = NULL;
4512 1.2 christos unsigned char buf[20];
4513 1.2 christos size_t readbytes, written;
4514 1.2 christos
4515 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4516 1.2 christos &serverssl, &sess, idx,
4517 1.2 christos SHA384_DIGEST_LENGTH)))
4518 1.2 christos goto end;
4519 1.2 christos
4520 1.2 christos /* Write some early data */
4521 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4522 1.2 christos &written)))
4523 1.2 christos goto end;
4524 1.2 christos
4525 1.2 christos /*
4526 1.2 christos * Server should skip over early data and then block waiting for client to
4527 1.2 christos * continue handshake
4528 1.2 christos */
4529 1.2 christos if (!TEST_int_le(SSL_accept(serverssl), 0)
4530 1.2 christos || !TEST_int_gt(SSL_connect(clientssl), 0)
4531 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4532 1.2 christos SSL_EARLY_DATA_REJECTED)
4533 1.2 christos || !TEST_int_gt(SSL_accept(serverssl), 0)
4534 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4535 1.2 christos SSL_EARLY_DATA_REJECTED))
4536 1.2 christos goto end;
4537 1.2 christos
4538 1.2 christos /* Send some normal data from client to server */
4539 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4540 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2)))
4541 1.2 christos goto end;
4542 1.2 christos
4543 1.2 christos if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4544 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4545 1.2 christos goto end;
4546 1.2 christos
4547 1.2 christos testresult = 1;
4548 1.2 christos
4549 1.2 christos end:
4550 1.2 christos SSL_SESSION_free(sess);
4551 1.2 christos SSL_SESSION_free(clientpsk);
4552 1.2 christos SSL_SESSION_free(serverpsk);
4553 1.2 christos clientpsk = serverpsk = NULL;
4554 1.2 christos SSL_free(serverssl);
4555 1.2 christos SSL_free(clientssl);
4556 1.2 christos SSL_CTX_free(sctx);
4557 1.2 christos SSL_CTX_free(cctx);
4558 1.2 christos return testresult;
4559 1.2 christos }
4560 1.2 christos
4561 1.2 christos
4562 1.2 christos # ifndef OPENSSL_NO_TLS1_2
4563 1.2 christos /*
4564 1.2 christos * Test that a server attempting to read early data can handle a connection
4565 1.2 christos * from a TLSv1.2 client.
4566 1.2 christos */
4567 1.2 christos static int test_early_data_tls1_2(int idx)
4568 1.2 christos {
4569 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4570 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4571 1.2 christos int testresult = 0;
4572 1.2 christos unsigned char buf[20];
4573 1.2 christos size_t readbytes, written;
4574 1.2 christos
4575 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4576 1.2 christos &serverssl, NULL, idx,
4577 1.2 christos SHA384_DIGEST_LENGTH)))
4578 1.2 christos goto end;
4579 1.2 christos
4580 1.2 christos /* Write some data - should block due to handshake with server */
4581 1.2 christos SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
4582 1.2 christos SSL_set_connect_state(clientssl);
4583 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4584 1.2 christos goto end;
4585 1.2 christos
4586 1.2 christos /*
4587 1.2 christos * Server should do TLSv1.2 handshake. First it will block waiting for more
4588 1.2 christos * messages from client after ServerDone. Then SSL_read_early_data should
4589 1.2 christos * finish and detect that early data has not been sent
4590 1.2 christos */
4591 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4592 1.2 christos &readbytes),
4593 1.2 christos SSL_READ_EARLY_DATA_ERROR))
4594 1.2 christos goto end;
4595 1.2 christos
4596 1.2 christos /*
4597 1.2 christos * Continue writing the message we started earlier. Will still block waiting
4598 1.2 christos * for the CCS/Finished from server
4599 1.2 christos */
4600 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4601 1.2 christos || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4602 1.2 christos &readbytes),
4603 1.2 christos SSL_READ_EARLY_DATA_FINISH)
4604 1.2 christos || !TEST_size_t_eq(readbytes, 0)
4605 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4606 1.2 christos SSL_EARLY_DATA_NOT_SENT))
4607 1.2 christos goto end;
4608 1.2 christos
4609 1.2 christos /* Continue writing the message we started earlier */
4610 1.2 christos if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4611 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1))
4612 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4613 1.2 christos SSL_EARLY_DATA_NOT_SENT)
4614 1.2 christos || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4615 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4616 1.2 christos || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
4617 1.2 christos || !TEST_size_t_eq(written, strlen(MSG2))
4618 1.2 christos || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
4619 1.2 christos || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4620 1.2 christos goto end;
4621 1.2 christos
4622 1.2 christos testresult = 1;
4623 1.2 christos
4624 1.2 christos end:
4625 1.2 christos SSL_SESSION_free(clientpsk);
4626 1.2 christos SSL_SESSION_free(serverpsk);
4627 1.2 christos clientpsk = serverpsk = NULL;
4628 1.2 christos SSL_free(serverssl);
4629 1.2 christos SSL_free(clientssl);
4630 1.2 christos SSL_CTX_free(sctx);
4631 1.2 christos SSL_CTX_free(cctx);
4632 1.2 christos
4633 1.2 christos return testresult;
4634 1.2 christos }
4635 1.2 christos # endif /* OPENSSL_NO_TLS1_2 */
4636 1.2 christos
4637 1.2 christos /*
4638 1.2 christos * Test configuring the TLSv1.3 ciphersuites
4639 1.2 christos *
4640 1.2 christos * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
4641 1.2 christos * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
4642 1.2 christos * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
4643 1.2 christos * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
4644 1.2 christos * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4645 1.2 christos * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4646 1.2 christos * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
4647 1.2 christos * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
4648 1.2 christos * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
4649 1.2 christos * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
4650 1.2 christos */
4651 1.2 christos static int test_set_ciphersuite(int idx)
4652 1.2 christos {
4653 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4654 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4655 1.2 christos int testresult = 0;
4656 1.2 christos
4657 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4658 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
4659 1.2 christos &sctx, &cctx, cert, privkey))
4660 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4661 1.2 christos "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
4662 1.2 christos goto end;
4663 1.2 christos
4664 1.2 christos if (idx >=4 && idx <= 7) {
4665 1.2 christos /* SSL_CTX explicit cipher list */
4666 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
4667 1.2 christos goto end;
4668 1.2 christos }
4669 1.2 christos
4670 1.2 christos if (idx == 0 || idx == 4) {
4671 1.2 christos /* Default ciphersuite */
4672 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4673 1.2 christos "TLS_AES_128_GCM_SHA256")))
4674 1.2 christos goto end;
4675 1.2 christos } else if (idx == 1 || idx == 5) {
4676 1.2 christos /* Non default ciphersuite */
4677 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4678 1.2 christos "TLS_AES_128_CCM_SHA256")))
4679 1.2 christos goto end;
4680 1.2 christos }
4681 1.2 christos
4682 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4683 1.2 christos &clientssl, NULL, NULL)))
4684 1.2 christos goto end;
4685 1.2 christos
4686 1.2 christos if (idx == 8 || idx == 9) {
4687 1.2 christos /* SSL explicit cipher list */
4688 1.2 christos if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
4689 1.2 christos goto end;
4690 1.2 christos }
4691 1.2 christos
4692 1.2 christos if (idx == 2 || idx == 6 || idx == 8) {
4693 1.2 christos /* Default ciphersuite */
4694 1.2 christos if (!TEST_true(SSL_set_ciphersuites(clientssl,
4695 1.2 christos "TLS_AES_128_GCM_SHA256")))
4696 1.2 christos goto end;
4697 1.2 christos } else if (idx == 3 || idx == 7 || idx == 9) {
4698 1.2 christos /* Non default ciphersuite */
4699 1.2 christos if (!TEST_true(SSL_set_ciphersuites(clientssl,
4700 1.2 christos "TLS_AES_128_CCM_SHA256")))
4701 1.2 christos goto end;
4702 1.2 christos }
4703 1.2 christos
4704 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4705 1.2 christos goto end;
4706 1.2 christos
4707 1.2 christos testresult = 1;
4708 1.2 christos
4709 1.2 christos end:
4710 1.2 christos SSL_free(serverssl);
4711 1.2 christos SSL_free(clientssl);
4712 1.2 christos SSL_CTX_free(sctx);
4713 1.2 christos SSL_CTX_free(cctx);
4714 1.2 christos
4715 1.2 christos return testresult;
4716 1.2 christos }
4717 1.2 christos
4718 1.2 christos static int test_ciphersuite_change(void)
4719 1.2 christos {
4720 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
4721 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
4722 1.2 christos SSL_SESSION *clntsess = NULL;
4723 1.2 christos int testresult = 0;
4724 1.2 christos const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
4725 1.2 christos
4726 1.2 christos /* Create a session based on SHA-256 */
4727 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4728 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
4729 1.2 christos &sctx, &cctx, cert, privkey))
4730 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4731 1.2 christos "TLS_AES_128_GCM_SHA256:"
4732 1.2 christos "TLS_AES_256_GCM_SHA384:"
4733 1.2 christos "TLS_AES_128_CCM_SHA256"))
4734 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4735 1.2 christos "TLS_AES_128_GCM_SHA256"))
4736 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4737 1.2 christos &clientssl, NULL, NULL))
4738 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
4739 1.2 christos SSL_ERROR_NONE)))
4740 1.2 christos goto end;
4741 1.2 christos
4742 1.2 christos clntsess = SSL_get1_session(clientssl);
4743 1.2 christos /* Save for later */
4744 1.2 christos aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
4745 1.2 christos SSL_shutdown(clientssl);
4746 1.2 christos SSL_shutdown(serverssl);
4747 1.2 christos SSL_free(serverssl);
4748 1.2 christos SSL_free(clientssl);
4749 1.2 christos serverssl = clientssl = NULL;
4750 1.2 christos
4751 1.2 christos /* Check we can resume a session with a different SHA-256 ciphersuite */
4752 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4753 1.2 christos "TLS_AES_128_CCM_SHA256"))
4754 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4755 1.2 christos &clientssl, NULL, NULL))
4756 1.2 christos || !TEST_true(SSL_set_session(clientssl, clntsess))
4757 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
4758 1.2 christos SSL_ERROR_NONE))
4759 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
4760 1.2 christos goto end;
4761 1.2 christos
4762 1.2 christos SSL_SESSION_free(clntsess);
4763 1.2 christos clntsess = SSL_get1_session(clientssl);
4764 1.2 christos SSL_shutdown(clientssl);
4765 1.2 christos SSL_shutdown(serverssl);
4766 1.2 christos SSL_free(serverssl);
4767 1.2 christos SSL_free(clientssl);
4768 1.2 christos serverssl = clientssl = NULL;
4769 1.2 christos
4770 1.2 christos /*
4771 1.2 christos * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
4772 1.2 christos * succeeds but does not resume.
4773 1.2 christos */
4774 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4775 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4776 1.2 christos NULL, NULL))
4777 1.2 christos || !TEST_true(SSL_set_session(clientssl, clntsess))
4778 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
4779 1.2 christos SSL_ERROR_SSL))
4780 1.2 christos || !TEST_false(SSL_session_reused(clientssl)))
4781 1.2 christos goto end;
4782 1.2 christos
4783 1.2 christos SSL_SESSION_free(clntsess);
4784 1.2 christos clntsess = NULL;
4785 1.2 christos SSL_shutdown(clientssl);
4786 1.2 christos SSL_shutdown(serverssl);
4787 1.2 christos SSL_free(serverssl);
4788 1.2 christos SSL_free(clientssl);
4789 1.2 christos serverssl = clientssl = NULL;
4790 1.2 christos
4791 1.2 christos /* Create a session based on SHA384 */
4792 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4793 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4794 1.2 christos &clientssl, NULL, NULL))
4795 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
4796 1.2 christos SSL_ERROR_NONE)))
4797 1.2 christos goto end;
4798 1.2 christos
4799 1.2 christos clntsess = SSL_get1_session(clientssl);
4800 1.2 christos SSL_shutdown(clientssl);
4801 1.2 christos SSL_shutdown(serverssl);
4802 1.2 christos SSL_free(serverssl);
4803 1.2 christos SSL_free(clientssl);
4804 1.2 christos serverssl = clientssl = NULL;
4805 1.2 christos
4806 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4807 1.2 christos "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
4808 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4809 1.2 christos "TLS_AES_256_GCM_SHA384"))
4810 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4811 1.2 christos NULL, NULL))
4812 1.2 christos || !TEST_true(SSL_set_session(clientssl, clntsess))
4813 1.2 christos /*
4814 1.2 christos * We use SSL_ERROR_WANT_READ below so that we can pause the
4815 1.2 christos * connection after the initial ClientHello has been sent to
4816 1.2 christos * enable us to make some session changes.
4817 1.2 christos */
4818 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
4819 1.2 christos SSL_ERROR_WANT_READ)))
4820 1.2 christos goto end;
4821 1.2 christos
4822 1.2 christos /* Trick the client into thinking this session is for a different digest */
4823 1.2 christos clntsess->cipher = aes_128_gcm_sha256;
4824 1.2 christos clntsess->cipher_id = clntsess->cipher->id;
4825 1.2 christos
4826 1.2 christos /*
4827 1.2 christos * Continue the previously started connection. Server has selected a SHA-384
4828 1.2 christos * ciphersuite, but client thinks the session is for SHA-256, so it should
4829 1.2 christos * bail out.
4830 1.2 christos */
4831 1.2 christos if (!TEST_false(create_ssl_connection(serverssl, clientssl,
4832 1.2 christos SSL_ERROR_SSL))
4833 1.2 christos || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
4834 1.2 christos SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
4835 1.2 christos goto end;
4836 1.2 christos
4837 1.2 christos testresult = 1;
4838 1.2 christos
4839 1.2 christos end:
4840 1.2 christos SSL_SESSION_free(clntsess);
4841 1.2 christos SSL_free(serverssl);
4842 1.2 christos SSL_free(clientssl);
4843 1.2 christos SSL_CTX_free(sctx);
4844 1.2 christos SSL_CTX_free(cctx);
4845 1.2 christos
4846 1.2 christos return testresult;
4847 1.2 christos }
4848 1.2 christos
4849 1.2 christos /*
4850 1.2 christos * Test TLSv1.3 Key exchange
4851 1.2 christos * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
4852 1.2 christos * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
4853 1.2 christos * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
4854 1.2 christos * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
4855 1.2 christos * Test 4 = Test NID_X25519 with TLSv1.3 client and server
4856 1.2 christos * Test 5 = Test NID_X448 with TLSv1.3 client and server
4857 1.2 christos * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
4858 1.2 christos * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
4859 1.2 christos * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
4860 1.2 christos * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
4861 1.2 christos * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
4862 1.2 christos * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
4863 1.2 christos * Test 12 = Test all ECDHE with TLSv1.2 client and server
4864 1.2 christos * Test 13 = Test all FFDHE with TLSv1.2 client and server
4865 1.2 christos */
4866 1.2 christos # ifndef OPENSSL_NO_EC
4867 1.2 christos static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
4868 1.2 christos NID_secp521r1, NID_X25519, NID_X448};
4869 1.2 christos # endif
4870 1.2 christos # ifndef OPENSSL_NO_DH
4871 1.2 christos static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
4872 1.2 christos NID_ffdhe6144, NID_ffdhe8192};
4873 1.2 christos # endif
4874 1.2 christos static int test_key_exchange(int idx)
4875 1.2 christos {
4876 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
4877 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
4878 1.2 christos int testresult = 0;
4879 1.2 christos int kexch_alg;
4880 1.2 christos int *kexch_groups = &kexch_alg;
4881 1.2 christos int kexch_groups_size = 1;
4882 1.2 christos int max_version = TLS1_3_VERSION;
4883 1.2 christos char *kexch_name0 = NULL;
4884 1.2 christos
4885 1.2 christos switch (idx) {
4886 1.2 christos # ifndef OPENSSL_NO_EC
4887 1.2 christos # ifndef OPENSSL_NO_TLS1_2
4888 1.2 christos case 12:
4889 1.2 christos max_version = TLS1_2_VERSION;
4890 1.2 christos # endif
4891 1.2 christos /* Fall through */
4892 1.2 christos case 0:
4893 1.2 christos kexch_groups = ecdhe_kexch_groups;
4894 1.2 christos kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
4895 1.2 christos kexch_name0 = "secp256r1";
4896 1.2 christos break;
4897 1.2 christos case 1:
4898 1.2 christos kexch_alg = NID_X9_62_prime256v1;
4899 1.2 christos kexch_name0 = "secp256r1";
4900 1.2 christos break;
4901 1.2 christos case 2:
4902 1.2 christos kexch_alg = NID_secp384r1;
4903 1.2 christos kexch_name0 = "secp384r1";
4904 1.2 christos break;
4905 1.2 christos case 3:
4906 1.2 christos kexch_alg = NID_secp521r1;
4907 1.2 christos kexch_name0 = "secp521r1";
4908 1.2 christos break;
4909 1.2 christos case 4:
4910 1.2 christos if (is_fips)
4911 1.2 christos return TEST_skip("X25519 might not be supported by fips provider.");
4912 1.2 christos kexch_alg = NID_X25519;
4913 1.2 christos kexch_name0 = "x25519";
4914 1.2 christos break;
4915 1.2 christos case 5:
4916 1.2 christos if (is_fips)
4917 1.2 christos return TEST_skip("X448 might not be supported by fips provider.");
4918 1.2 christos kexch_alg = NID_X448;
4919 1.2 christos kexch_name0 = "x448";
4920 1.2 christos break;
4921 1.2 christos # endif
4922 1.2 christos # ifndef OPENSSL_NO_DH
4923 1.2 christos # ifndef OPENSSL_NO_TLS1_2
4924 1.2 christos case 13:
4925 1.2 christos max_version = TLS1_2_VERSION;
4926 1.2 christos kexch_name0 = "ffdhe2048";
4927 1.2 christos # endif
4928 1.2 christos /* Fall through */
4929 1.2 christos case 6:
4930 1.2 christos kexch_groups = ffdhe_kexch_groups;
4931 1.2 christos kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
4932 1.2 christos kexch_name0 = "ffdhe2048";
4933 1.2 christos break;
4934 1.2 christos case 7:
4935 1.2 christos kexch_alg = NID_ffdhe2048;
4936 1.2 christos kexch_name0 = "ffdhe2048";
4937 1.2 christos break;
4938 1.2 christos case 8:
4939 1.2 christos kexch_alg = NID_ffdhe3072;
4940 1.2 christos kexch_name0 = "ffdhe3072";
4941 1.2 christos break;
4942 1.2 christos case 9:
4943 1.2 christos kexch_alg = NID_ffdhe4096;
4944 1.2 christos kexch_name0 = "ffdhe4096";
4945 1.2 christos break;
4946 1.2 christos case 10:
4947 1.2 christos kexch_alg = NID_ffdhe6144;
4948 1.2 christos kexch_name0 = "ffdhe6144";
4949 1.2 christos break;
4950 1.2 christos case 11:
4951 1.2 christos kexch_alg = NID_ffdhe8192;
4952 1.2 christos kexch_name0 = "ffdhe8192";
4953 1.2 christos break;
4954 1.2 christos # endif
4955 1.2 christos default:
4956 1.2 christos /* We're skipping this test */
4957 1.2 christos return 1;
4958 1.2 christos }
4959 1.2 christos
4960 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4961 1.2 christos TLS_client_method(), TLS1_VERSION,
4962 1.2 christos max_version, &sctx, &cctx, cert,
4963 1.2 christos privkey)))
4964 1.2 christos goto end;
4965 1.2 christos
4966 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
4967 1.2 christos TLS1_3_RFC_AES_128_GCM_SHA256)))
4968 1.2 christos goto end;
4969 1.2 christos
4970 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4971 1.2 christos TLS1_3_RFC_AES_128_GCM_SHA256)))
4972 1.2 christos goto end;
4973 1.2 christos
4974 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
4975 1.2 christos TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4976 1.2 christos TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
4977 1.2 christos || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
4978 1.2 christos goto end;
4979 1.2 christos
4980 1.2 christos /*
4981 1.2 christos * Must include an EC ciphersuite so that we send supported groups in
4982 1.2 christos * TLSv1.2
4983 1.2 christos */
4984 1.2 christos # ifndef OPENSSL_NO_TLS1_2
4985 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4986 1.2 christos TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4987 1.2 christos TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
4988 1.2 christos goto end;
4989 1.2 christos # endif
4990 1.2 christos
4991 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4992 1.2 christos NULL, NULL)))
4993 1.2 christos goto end;
4994 1.2 christos
4995 1.2 christos if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
4996 1.2 christos || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
4997 1.2 christos goto end;
4998 1.2 christos
4999 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
5000 1.2 christos goto end;
5001 1.2 christos
5002 1.2 christos /*
5003 1.2 christos * If Handshake succeeds the negotiated kexch alg should be the first one in
5004 1.2 christos * configured, except in the case of FFDHE groups (idx 13), which are
5005 1.2 christos * TLSv1.3 only so we expect no shared group to exist.
5006 1.2 christos */
5007 1.2 christos if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
5008 1.2 christos idx == 13 ? 0 : kexch_groups[0]))
5009 1.2 christos goto end;
5010 1.2 christos
5011 1.2 christos if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
5012 1.2 christos kexch_name0))
5013 1.2 christos goto end;
5014 1.2 christos
5015 1.2 christos /* We don't implement RFC 7919 named groups for TLS 1.2. */
5016 1.2 christos if (idx != 13) {
5017 1.2 christos if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
5018 1.2 christos goto end;
5019 1.2 christos if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
5020 1.2 christos goto end;
5021 1.2 christos }
5022 1.2 christos
5023 1.2 christos testresult = 1;
5024 1.2 christos end:
5025 1.2 christos SSL_free(serverssl);
5026 1.2 christos SSL_free(clientssl);
5027 1.2 christos SSL_CTX_free(sctx);
5028 1.2 christos SSL_CTX_free(cctx);
5029 1.2 christos return testresult;
5030 1.2 christos }
5031 1.2 christos
5032 1.2 christos # if !defined(OPENSSL_NO_TLS1_2) \
5033 1.2 christos && !defined(OPENSSL_NO_EC) \
5034 1.2 christos && !defined(OPENSSL_NO_DH)
5035 1.2 christos static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
5036 1.2 christos int isecdhe, int idx)
5037 1.2 christos {
5038 1.2 christos int kexch_alg;
5039 1.2 christos int *kexch_groups = &kexch_alg;
5040 1.2 christos int numec, numff;
5041 1.2 christos
5042 1.2 christos numec = OSSL_NELEM(ecdhe_kexch_groups);
5043 1.2 christos numff = OSSL_NELEM(ffdhe_kexch_groups);
5044 1.2 christos if (isecdhe)
5045 1.2 christos kexch_alg = ecdhe_kexch_groups[idx];
5046 1.2 christos else
5047 1.2 christos kexch_alg = ffdhe_kexch_groups[idx];
5048 1.2 christos
5049 1.2 christos if (clientmulti) {
5050 1.2 christos if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
5051 1.2 christos return 0;
5052 1.2 christos if (isecdhe) {
5053 1.2 christos if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
5054 1.2 christos numec)))
5055 1.2 christos return 0;
5056 1.2 christos } else {
5057 1.2 christos if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
5058 1.2 christos numff)))
5059 1.2 christos return 0;
5060 1.2 christos }
5061 1.2 christos } else {
5062 1.2 christos if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
5063 1.2 christos return 0;
5064 1.2 christos if (isecdhe) {
5065 1.2 christos if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
5066 1.2 christos numec)))
5067 1.2 christos return 0;
5068 1.2 christos } else {
5069 1.2 christos if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
5070 1.2 christos numff)))
5071 1.2 christos return 0;
5072 1.2 christos }
5073 1.2 christos }
5074 1.2 christos return 1;
5075 1.2 christos }
5076 1.2 christos
5077 1.2 christos /*-
5078 1.2 christos * Test the SSL_get_negotiated_group() API across a battery of scenarios.
5079 1.2 christos * Run through both the ECDHE and FFDHE group lists used in the previous
5080 1.2 christos * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
5081 1.2 christos * confirming the expected result; then perform a resumption handshake
5082 1.2 christos * while offering the same group list, and another resumption handshake
5083 1.2 christos * offering a different group list. The returned value should be the
5084 1.2 christos * negotiated group for the initial handshake; for TLS 1.3 resumption
5085 1.2 christos * handshakes the returned value will be negotiated on the resumption
5086 1.2 christos * handshake itself, but for TLS 1.2 resumption handshakes the value will
5087 1.2 christos * be cached in the session from the original handshake, regardless of what
5088 1.2 christos * was offered in the resumption ClientHello.
5089 1.2 christos *
5090 1.2 christos * Using E for the number of EC groups and F for the number of FF groups:
5091 1.2 christos * E tests of ECDHE with TLS 1.3, server only has one group
5092 1.2 christos * F tests of FFDHE with TLS 1.3, server only has one group
5093 1.2 christos * E tests of ECDHE with TLS 1.2, server only has one group
5094 1.2 christos * F tests of FFDHE with TLS 1.2, server only has one group
5095 1.2 christos * E tests of ECDHE with TLS 1.3, client sends only one group
5096 1.2 christos * F tests of FFDHE with TLS 1.3, client sends only one group
5097 1.2 christos * E tests of ECDHE with TLS 1.2, client sends only one group
5098 1.2 christos * F tests of FFDHE with TLS 1.2, client sends only one group
5099 1.2 christos */
5100 1.2 christos static int test_negotiated_group(int idx)
5101 1.2 christos {
5102 1.2 christos int clientmulti, istls13, isecdhe, numec, numff, numgroups;
5103 1.2 christos int expectednid;
5104 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
5105 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
5106 1.2 christos SSL_SESSION *origsess = NULL;
5107 1.2 christos int testresult = 0;
5108 1.2 christos int kexch_alg;
5109 1.2 christos int max_version = TLS1_3_VERSION;
5110 1.2 christos
5111 1.2 christos numec = OSSL_NELEM(ecdhe_kexch_groups);
5112 1.2 christos numff = OSSL_NELEM(ffdhe_kexch_groups);
5113 1.2 christos numgroups = numec + numff;
5114 1.2 christos clientmulti = (idx < 2 * numgroups);
5115 1.2 christos idx = idx % (2 * numgroups);
5116 1.2 christos istls13 = (idx < numgroups);
5117 1.2 christos idx = idx % numgroups;
5118 1.2 christos isecdhe = (idx < numec);
5119 1.2 christos if (!isecdhe)
5120 1.2 christos idx -= numec;
5121 1.2 christos /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
5122 1.2 christos if (isecdhe)
5123 1.2 christos kexch_alg = ecdhe_kexch_groups[idx];
5124 1.2 christos else
5125 1.2 christos kexch_alg = ffdhe_kexch_groups[idx];
5126 1.2 christos /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
5127 1.2 christos if (!istls13 && !isecdhe)
5128 1.2 christos expectednid = NID_undef;
5129 1.2 christos else
5130 1.2 christos expectednid = kexch_alg;
5131 1.2 christos
5132 1.2 christos if (is_fips && (kexch_alg == NID_X25519 || kexch_alg == NID_X448))
5133 1.2 christos return TEST_skip("X25519 and X448 might not be available in fips provider.");
5134 1.2 christos
5135 1.2 christos if (!istls13)
5136 1.2 christos max_version = TLS1_2_VERSION;
5137 1.2 christos
5138 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5139 1.2 christos TLS_client_method(), TLS1_VERSION,
5140 1.2 christos max_version, &sctx, &cctx, cert,
5141 1.2 christos privkey)))
5142 1.2 christos goto end;
5143 1.2 christos
5144 1.2 christos /*
5145 1.2 christos * Force (EC)DHE ciphers for TLS 1.2.
5146 1.2 christos * Be sure to enable auto tmp DH so that FFDHE can succeed.
5147 1.2 christos */
5148 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
5149 1.2 christos TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5150 1.2 christos TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
5151 1.2 christos || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
5152 1.2 christos goto end;
5153 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
5154 1.2 christos TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5155 1.2 christos TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
5156 1.2 christos goto end;
5157 1.2 christos
5158 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5159 1.2 christos NULL, NULL)))
5160 1.2 christos goto end;
5161 1.2 christos
5162 1.2 christos if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
5163 1.2 christos idx)))
5164 1.2 christos goto end;
5165 1.2 christos
5166 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
5167 1.2 christos goto end;
5168 1.2 christos
5169 1.2 christos /* Initial handshake; always the configured one */
5170 1.2 christos if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5171 1.2 christos || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5172 1.2 christos goto end;
5173 1.2 christos
5174 1.2 christos if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
5175 1.2 christos goto end;
5176 1.2 christos
5177 1.2 christos SSL_shutdown(clientssl);
5178 1.2 christos SSL_shutdown(serverssl);
5179 1.2 christos SSL_free(serverssl);
5180 1.2 christos SSL_free(clientssl);
5181 1.2 christos serverssl = clientssl = NULL;
5182 1.2 christos
5183 1.2 christos /* First resumption attempt; use the same config as initial handshake */
5184 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5185 1.2 christos NULL, NULL))
5186 1.2 christos || !TEST_true(SSL_set_session(clientssl, origsess))
5187 1.2 christos || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5188 1.2 christos isecdhe, idx)))
5189 1.2 christos goto end;
5190 1.2 christos
5191 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5192 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
5193 1.2 christos goto end;
5194 1.2 christos
5195 1.2 christos /* Still had better agree, since nothing changed... */
5196 1.2 christos if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5197 1.2 christos || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5198 1.2 christos goto end;
5199 1.2 christos
5200 1.2 christos SSL_shutdown(clientssl);
5201 1.2 christos SSL_shutdown(serverssl);
5202 1.2 christos SSL_free(serverssl);
5203 1.2 christos SSL_free(clientssl);
5204 1.2 christos serverssl = clientssl = NULL;
5205 1.2 christos
5206 1.2 christos /*-
5207 1.2 christos * Second resumption attempt
5208 1.2 christos * The party that picks one group changes it, which we effectuate by
5209 1.2 christos * changing 'idx' and updating what we expect.
5210 1.2 christos */
5211 1.2 christos if (idx == 0)
5212 1.2 christos idx = 1;
5213 1.2 christos else
5214 1.2 christos idx--;
5215 1.2 christos if (istls13) {
5216 1.2 christos if (isecdhe)
5217 1.2 christos expectednid = ecdhe_kexch_groups[idx];
5218 1.2 christos else
5219 1.2 christos expectednid = ffdhe_kexch_groups[idx];
5220 1.2 christos /* Verify that we are changing what we expect. */
5221 1.2 christos if (!TEST_int_ne(expectednid, kexch_alg))
5222 1.2 christos goto end;
5223 1.2 christos } else {
5224 1.2 christos /* TLS 1.2 only supports named groups for ECDHE. */
5225 1.2 christos if (isecdhe)
5226 1.2 christos expectednid = kexch_alg;
5227 1.2 christos else
5228 1.2 christos expectednid = 0;
5229 1.2 christos }
5230 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5231 1.2 christos NULL, NULL))
5232 1.2 christos || !TEST_true(SSL_set_session(clientssl, origsess))
5233 1.2 christos || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5234 1.2 christos isecdhe, idx)))
5235 1.2 christos goto end;
5236 1.2 christos
5237 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5238 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
5239 1.2 christos goto end;
5240 1.2 christos
5241 1.2 christos /* Check that we get what we expected */
5242 1.2 christos if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5243 1.2 christos || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5244 1.2 christos goto end;
5245 1.2 christos
5246 1.2 christos testresult = 1;
5247 1.2 christos end:
5248 1.2 christos SSL_free(serverssl);
5249 1.2 christos SSL_free(clientssl);
5250 1.2 christos SSL_CTX_free(sctx);
5251 1.2 christos SSL_CTX_free(cctx);
5252 1.2 christos SSL_SESSION_free(origsess);
5253 1.2 christos return testresult;
5254 1.2 christos }
5255 1.2 christos # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
5256 1.2 christos
5257 1.2 christos /*
5258 1.2 christos * Test TLSv1.3 Cipher Suite
5259 1.2 christos * Test 0 = Set TLS1.3 cipher on context
5260 1.2 christos * Test 1 = Set TLS1.3 cipher on SSL
5261 1.2 christos * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
5262 1.2 christos * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
5263 1.2 christos */
5264 1.2 christos static int test_tls13_ciphersuite(int idx)
5265 1.2 christos {
5266 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
5267 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
5268 1.2 christos static const struct {
5269 1.2 christos const char *ciphername;
5270 1.2 christos int fipscapable;
5271 1.2 christos } t13_ciphers[] = {
5272 1.2 christos { TLS1_3_RFC_AES_128_GCM_SHA256, 1 },
5273 1.2 christos { TLS1_3_RFC_AES_256_GCM_SHA384, 1 },
5274 1.2 christos { TLS1_3_RFC_AES_128_CCM_SHA256, 1 },
5275 1.2 christos # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
5276 1.2 christos { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5277 1.2 christos { TLS1_3_RFC_AES_256_GCM_SHA384
5278 1.2 christos ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5279 1.2 christos # endif
5280 1.2 christos { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1 }
5281 1.2 christos };
5282 1.2 christos const char *t13_cipher = NULL;
5283 1.2 christos const char *t12_cipher = NULL;
5284 1.2 christos const char *negotiated_scipher;
5285 1.2 christos const char *negotiated_ccipher;
5286 1.2 christos int set_at_ctx = 0;
5287 1.2 christos int set_at_ssl = 0;
5288 1.2 christos int testresult = 0;
5289 1.2 christos int max_ver;
5290 1.2 christos size_t i;
5291 1.2 christos
5292 1.2 christos switch (idx) {
5293 1.2 christos case 0:
5294 1.2 christos set_at_ctx = 1;
5295 1.2 christos break;
5296 1.2 christos case 1:
5297 1.2 christos set_at_ssl = 1;
5298 1.2 christos break;
5299 1.2 christos case 2:
5300 1.2 christos set_at_ctx = 1;
5301 1.2 christos t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5302 1.2 christos break;
5303 1.2 christos case 3:
5304 1.2 christos set_at_ssl = 1;
5305 1.2 christos t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5306 1.2 christos break;
5307 1.2 christos }
5308 1.2 christos
5309 1.2 christos for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
5310 1.2 christos # ifdef OPENSSL_NO_TLS1_2
5311 1.2 christos if (max_ver == TLS1_2_VERSION)
5312 1.2 christos continue;
5313 1.2 christos # endif
5314 1.2 christos for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
5315 1.2 christos if (is_fips && !t13_ciphers[i].fipscapable)
5316 1.2 christos continue;
5317 1.2 christos t13_cipher = t13_ciphers[i].ciphername;
5318 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5319 1.2 christos TLS_client_method(),
5320 1.2 christos TLS1_VERSION, max_ver,
5321 1.2 christos &sctx, &cctx, cert, privkey)))
5322 1.2 christos goto end;
5323 1.2 christos
5324 1.2 christos if (set_at_ctx) {
5325 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
5326 1.2 christos || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
5327 1.2 christos goto end;
5328 1.2 christos if (t12_cipher != NULL) {
5329 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
5330 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(cctx,
5331 1.2 christos t12_cipher)))
5332 1.2 christos goto end;
5333 1.2 christos }
5334 1.2 christos }
5335 1.2 christos
5336 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5337 1.2 christos &clientssl, NULL, NULL)))
5338 1.2 christos goto end;
5339 1.2 christos
5340 1.2 christos if (set_at_ssl) {
5341 1.2 christos if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
5342 1.2 christos || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
5343 1.2 christos goto end;
5344 1.2 christos if (t12_cipher != NULL) {
5345 1.2 christos if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
5346 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl,
5347 1.2 christos t12_cipher)))
5348 1.2 christos goto end;
5349 1.2 christos }
5350 1.2 christos }
5351 1.2 christos
5352 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5353 1.2 christos SSL_ERROR_NONE)))
5354 1.2 christos goto end;
5355 1.2 christos
5356 1.2 christos negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5357 1.2 christos serverssl));
5358 1.2 christos negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5359 1.2 christos clientssl));
5360 1.2 christos if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
5361 1.2 christos goto end;
5362 1.2 christos
5363 1.2 christos /*
5364 1.2 christos * TEST_strn_eq is used below because t13_cipher can contain
5365 1.2 christos * multiple ciphersuites
5366 1.2 christos */
5367 1.2 christos if (max_ver == TLS1_3_VERSION
5368 1.2 christos && !TEST_strn_eq(t13_cipher, negotiated_scipher,
5369 1.2 christos strlen(negotiated_scipher)))
5370 1.2 christos goto end;
5371 1.2 christos
5372 1.2 christos # ifndef OPENSSL_NO_TLS1_2
5373 1.2 christos /* Below validation is not done when t12_cipher is NULL */
5374 1.2 christos if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
5375 1.2 christos && !TEST_str_eq(t12_cipher, negotiated_scipher))
5376 1.2 christos goto end;
5377 1.2 christos # endif
5378 1.2 christos
5379 1.2 christos SSL_free(serverssl);
5380 1.2 christos serverssl = NULL;
5381 1.2 christos SSL_free(clientssl);
5382 1.2 christos clientssl = NULL;
5383 1.2 christos SSL_CTX_free(sctx);
5384 1.2 christos sctx = NULL;
5385 1.2 christos SSL_CTX_free(cctx);
5386 1.2 christos cctx = NULL;
5387 1.2 christos }
5388 1.2 christos }
5389 1.2 christos
5390 1.2 christos testresult = 1;
5391 1.2 christos end:
5392 1.2 christos SSL_free(serverssl);
5393 1.2 christos SSL_free(clientssl);
5394 1.2 christos SSL_CTX_free(sctx);
5395 1.2 christos SSL_CTX_free(cctx);
5396 1.2 christos return testresult;
5397 1.2 christos }
5398 1.2 christos
5399 1.2 christos /*
5400 1.2 christos * Test TLSv1.3 PSKs
5401 1.2 christos * Test 0 = Test new style callbacks
5402 1.2 christos * Test 1 = Test both new and old style callbacks
5403 1.2 christos * Test 2 = Test old style callbacks
5404 1.2 christos * Test 3 = Test old style callbacks with no certificate
5405 1.2 christos */
5406 1.2 christos static int test_tls13_psk(int idx)
5407 1.2 christos {
5408 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
5409 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
5410 1.2 christos const SSL_CIPHER *cipher = NULL;
5411 1.2 christos const unsigned char key[] = {
5412 1.2 christos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
5413 1.2 christos 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
5414 1.2 christos 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
5415 1.2 christos 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
5416 1.2 christos };
5417 1.2 christos int testresult = 0;
5418 1.2 christos
5419 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5420 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
5421 1.2 christos &sctx, &cctx, idx == 3 ? NULL : cert,
5422 1.2 christos idx == 3 ? NULL : privkey)))
5423 1.2 christos goto end;
5424 1.2 christos
5425 1.2 christos if (idx != 3) {
5426 1.2 christos /*
5427 1.2 christos * We use a ciphersuite with SHA256 to ease testing old style PSK
5428 1.2 christos * callbacks which will always default to SHA256. This should not be
5429 1.2 christos * necessary if we have no cert/priv key. In that case the server should
5430 1.2 christos * prefer SHA256 automatically.
5431 1.2 christos */
5432 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5433 1.2 christos "TLS_AES_128_GCM_SHA256")))
5434 1.2 christos goto end;
5435 1.2 christos } else {
5436 1.2 christos /*
5437 1.2 christos * As noted above the server should prefer SHA256 automatically. However
5438 1.2 christos * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
5439 1.2 christos * code works even if we are testing with only the FIPS provider loaded.
5440 1.2 christos */
5441 1.2 christos if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5442 1.2 christos "TLS_AES_256_GCM_SHA384:"
5443 1.2 christos "TLS_AES_128_GCM_SHA256")))
5444 1.2 christos goto end;
5445 1.2 christos }
5446 1.2 christos
5447 1.2 christos /*
5448 1.2 christos * Test 0: New style callbacks only
5449 1.2 christos * Test 1: New and old style callbacks (only the new ones should be used)
5450 1.2 christos * Test 2: Old style callbacks only
5451 1.2 christos */
5452 1.2 christos if (idx == 0 || idx == 1) {
5453 1.2 christos SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
5454 1.2 christos SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
5455 1.2 christos }
5456 1.2 christos #ifndef OPENSSL_NO_PSK
5457 1.2 christos if (idx >= 1) {
5458 1.2 christos SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
5459 1.2 christos SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
5460 1.2 christos }
5461 1.2 christos #endif
5462 1.2 christos srvid = pskid;
5463 1.2 christos use_session_cb_cnt = 0;
5464 1.2 christos find_session_cb_cnt = 0;
5465 1.2 christos psk_client_cb_cnt = 0;
5466 1.2 christos psk_server_cb_cnt = 0;
5467 1.2 christos
5468 1.2 christos if (idx != 3) {
5469 1.2 christos /*
5470 1.2 christos * Check we can create a connection if callback decides not to send a
5471 1.2 christos * PSK
5472 1.2 christos */
5473 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5474 1.2 christos NULL, NULL))
5475 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
5476 1.2 christos SSL_ERROR_NONE))
5477 1.2 christos || !TEST_false(SSL_session_reused(clientssl))
5478 1.2 christos || !TEST_false(SSL_session_reused(serverssl)))
5479 1.2 christos goto end;
5480 1.2 christos
5481 1.2 christos if (idx == 0 || idx == 1) {
5482 1.2 christos if (!TEST_true(use_session_cb_cnt == 1)
5483 1.2 christos || !TEST_true(find_session_cb_cnt == 0)
5484 1.2 christos /*
5485 1.2 christos * If no old style callback then below should be 0
5486 1.2 christos * otherwise 1
5487 1.2 christos */
5488 1.2 christos || !TEST_true(psk_client_cb_cnt == idx)
5489 1.2 christos || !TEST_true(psk_server_cb_cnt == 0))
5490 1.2 christos goto end;
5491 1.2 christos } else {
5492 1.2 christos if (!TEST_true(use_session_cb_cnt == 0)
5493 1.2 christos || !TEST_true(find_session_cb_cnt == 0)
5494 1.2 christos || !TEST_true(psk_client_cb_cnt == 1)
5495 1.2 christos || !TEST_true(psk_server_cb_cnt == 0))
5496 1.2 christos goto end;
5497 1.2 christos }
5498 1.2 christos
5499 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
5500 1.2 christos serverssl = clientssl = NULL;
5501 1.2 christos use_session_cb_cnt = psk_client_cb_cnt = 0;
5502 1.2 christos }
5503 1.2 christos
5504 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5505 1.2 christos NULL, NULL)))
5506 1.2 christos goto end;
5507 1.2 christos
5508 1.2 christos /* Create the PSK */
5509 1.2 christos cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
5510 1.2 christos clientpsk = SSL_SESSION_new();
5511 1.2 christos if (!TEST_ptr(clientpsk)
5512 1.2 christos || !TEST_ptr(cipher)
5513 1.2 christos || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
5514 1.2 christos sizeof(key)))
5515 1.2 christos || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
5516 1.2 christos || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
5517 1.2 christos TLS1_3_VERSION))
5518 1.2 christos || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
5519 1.2 christos goto end;
5520 1.2 christos serverpsk = clientpsk;
5521 1.2 christos
5522 1.2 christos /* Check we can create a connection and the PSK is used */
5523 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5524 1.2 christos || !TEST_true(SSL_session_reused(clientssl))
5525 1.2 christos || !TEST_true(SSL_session_reused(serverssl)))
5526 1.2 christos goto end;
5527 1.2 christos
5528 1.2 christos if (idx == 0 || idx == 1) {
5529 1.2 christos if (!TEST_true(use_session_cb_cnt == 1)
5530 1.2 christos || !TEST_true(find_session_cb_cnt == 1)
5531 1.2 christos || !TEST_true(psk_client_cb_cnt == 0)
5532 1.2 christos || !TEST_true(psk_server_cb_cnt == 0))
5533 1.2 christos goto end;
5534 1.2 christos } else {
5535 1.2 christos if (!TEST_true(use_session_cb_cnt == 0)
5536 1.2 christos || !TEST_true(find_session_cb_cnt == 0)
5537 1.2 christos || !TEST_true(psk_client_cb_cnt == 1)
5538 1.2 christos || !TEST_true(psk_server_cb_cnt == 1))
5539 1.2 christos goto end;
5540 1.2 christos }
5541 1.2 christos
5542 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
5543 1.2 christos serverssl = clientssl = NULL;
5544 1.2 christos use_session_cb_cnt = find_session_cb_cnt = 0;
5545 1.2 christos psk_client_cb_cnt = psk_server_cb_cnt = 0;
5546 1.2 christos
5547 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5548 1.2 christos NULL, NULL)))
5549 1.2 christos goto end;
5550 1.2 christos
5551 1.2 christos /* Force an HRR */
5552 1.2 christos #if defined(OPENSSL_NO_EC)
5553 1.2 christos if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
5554 1.2 christos goto end;
5555 1.2 christos #else
5556 1.2 christos if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
5557 1.2 christos goto end;
5558 1.2 christos #endif
5559 1.2 christos
5560 1.2 christos /*
5561 1.2 christos * Check we can create a connection, the PSK is used and the callbacks are
5562 1.2 christos * called twice.
5563 1.2 christos */
5564 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5565 1.2 christos || !TEST_true(SSL_session_reused(clientssl))
5566 1.2 christos || !TEST_true(SSL_session_reused(serverssl)))
5567 1.2 christos goto end;
5568 1.2 christos
5569 1.2 christos if (idx == 0 || idx == 1) {
5570 1.2 christos if (!TEST_true(use_session_cb_cnt == 2)
5571 1.2 christos || !TEST_true(find_session_cb_cnt == 2)
5572 1.2 christos || !TEST_true(psk_client_cb_cnt == 0)
5573 1.2 christos || !TEST_true(psk_server_cb_cnt == 0))
5574 1.2 christos goto end;
5575 1.2 christos } else {
5576 1.2 christos if (!TEST_true(use_session_cb_cnt == 0)
5577 1.2 christos || !TEST_true(find_session_cb_cnt == 0)
5578 1.2 christos || !TEST_true(psk_client_cb_cnt == 2)
5579 1.2 christos || !TEST_true(psk_server_cb_cnt == 2))
5580 1.2 christos goto end;
5581 1.2 christos }
5582 1.2 christos
5583 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
5584 1.2 christos serverssl = clientssl = NULL;
5585 1.2 christos use_session_cb_cnt = find_session_cb_cnt = 0;
5586 1.2 christos psk_client_cb_cnt = psk_server_cb_cnt = 0;
5587 1.2 christos
5588 1.2 christos if (idx != 3) {
5589 1.2 christos /*
5590 1.2 christos * Check that if the server rejects the PSK we can still connect, but with
5591 1.2 christos * a full handshake
5592 1.2 christos */
5593 1.2 christos srvid = "Dummy Identity";
5594 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5595 1.2 christos NULL, NULL))
5596 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
5597 1.2 christos SSL_ERROR_NONE))
5598 1.2 christos || !TEST_false(SSL_session_reused(clientssl))
5599 1.2 christos || !TEST_false(SSL_session_reused(serverssl)))
5600 1.2 christos goto end;
5601 1.2 christos
5602 1.2 christos if (idx == 0 || idx == 1) {
5603 1.2 christos if (!TEST_true(use_session_cb_cnt == 1)
5604 1.2 christos || !TEST_true(find_session_cb_cnt == 1)
5605 1.2 christos || !TEST_true(psk_client_cb_cnt == 0)
5606 1.2 christos /*
5607 1.2 christos * If no old style callback then below should be 0
5608 1.2 christos * otherwise 1
5609 1.2 christos */
5610 1.2 christos || !TEST_true(psk_server_cb_cnt == idx))
5611 1.2 christos goto end;
5612 1.2 christos } else {
5613 1.2 christos if (!TEST_true(use_session_cb_cnt == 0)
5614 1.2 christos || !TEST_true(find_session_cb_cnt == 0)
5615 1.2 christos || !TEST_true(psk_client_cb_cnt == 1)
5616 1.2 christos || !TEST_true(psk_server_cb_cnt == 1))
5617 1.2 christos goto end;
5618 1.2 christos }
5619 1.2 christos
5620 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
5621 1.2 christos serverssl = clientssl = NULL;
5622 1.2 christos }
5623 1.2 christos testresult = 1;
5624 1.2 christos
5625 1.2 christos end:
5626 1.2 christos SSL_SESSION_free(clientpsk);
5627 1.2 christos SSL_SESSION_free(serverpsk);
5628 1.2 christos clientpsk = serverpsk = NULL;
5629 1.2 christos SSL_free(serverssl);
5630 1.2 christos SSL_free(clientssl);
5631 1.2 christos SSL_CTX_free(sctx);
5632 1.2 christos SSL_CTX_free(cctx);
5633 1.2 christos return testresult;
5634 1.2 christos }
5635 1.2 christos
5636 1.2 christos static unsigned char cookie_magic_value[] = "cookie magic";
5637 1.2 christos
5638 1.2 christos static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
5639 1.2 christos unsigned int *cookie_len)
5640 1.2 christos {
5641 1.2 christos /*
5642 1.2 christos * Not suitable as a real cookie generation function but good enough for
5643 1.2 christos * testing!
5644 1.2 christos */
5645 1.2 christos memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
5646 1.2 christos *cookie_len = sizeof(cookie_magic_value) - 1;
5647 1.2 christos
5648 1.2 christos return 1;
5649 1.2 christos }
5650 1.2 christos
5651 1.2 christos static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
5652 1.2 christos unsigned int cookie_len)
5653 1.2 christos {
5654 1.2 christos if (cookie_len == sizeof(cookie_magic_value) - 1
5655 1.2 christos && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
5656 1.2 christos return 1;
5657 1.2 christos
5658 1.2 christos return 0;
5659 1.2 christos }
5660 1.2 christos
5661 1.2 christos static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
5662 1.2 christos size_t *cookie_len)
5663 1.2 christos {
5664 1.2 christos unsigned int temp;
5665 1.2 christos int res = generate_cookie_callback(ssl, cookie, &temp);
5666 1.2 christos *cookie_len = temp;
5667 1.2 christos return res;
5668 1.2 christos }
5669 1.2 christos
5670 1.2 christos static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
5671 1.2 christos size_t cookie_len)
5672 1.2 christos {
5673 1.2 christos return verify_cookie_callback(ssl, cookie, cookie_len);
5674 1.2 christos }
5675 1.2 christos
5676 1.2 christos static int test_stateless(void)
5677 1.2 christos {
5678 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
5679 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
5680 1.2 christos int testresult = 0;
5681 1.2 christos
5682 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5683 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
5684 1.2 christos &sctx, &cctx, cert, privkey)))
5685 1.2 christos goto end;
5686 1.2 christos
5687 1.2 christos /* The arrival of CCS messages can confuse the test */
5688 1.2 christos SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
5689 1.2 christos
5690 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5691 1.2 christos NULL, NULL))
5692 1.2 christos /* Send the first ClientHello */
5693 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
5694 1.2 christos SSL_ERROR_WANT_READ))
5695 1.2 christos /*
5696 1.2 christos * This should fail with a -1 return because we have no callbacks
5697 1.2 christos * set up
5698 1.2 christos */
5699 1.2 christos || !TEST_int_eq(SSL_stateless(serverssl), -1))
5700 1.2 christos goto end;
5701 1.2 christos
5702 1.2 christos /* Fatal error so abandon the connection from this client */
5703 1.2 christos SSL_free(clientssl);
5704 1.2 christos clientssl = NULL;
5705 1.2 christos
5706 1.2 christos /* Set up the cookie generation and verification callbacks */
5707 1.2 christos SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
5708 1.2 christos SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
5709 1.2 christos
5710 1.2 christos /*
5711 1.2 christos * Create a new connection from the client (we can reuse the server SSL
5712 1.2 christos * object).
5713 1.2 christos */
5714 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5715 1.2 christos NULL, NULL))
5716 1.2 christos /* Send the first ClientHello */
5717 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
5718 1.2 christos SSL_ERROR_WANT_READ))
5719 1.2 christos /* This should fail because there is no cookie */
5720 1.2 christos || !TEST_int_eq(SSL_stateless(serverssl), 0))
5721 1.2 christos goto end;
5722 1.2 christos
5723 1.2 christos /* Abandon the connection from this client */
5724 1.2 christos SSL_free(clientssl);
5725 1.2 christos clientssl = NULL;
5726 1.2 christos
5727 1.2 christos /*
5728 1.2 christos * Now create a connection from a new client but with the same server SSL
5729 1.2 christos * object
5730 1.2 christos */
5731 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5732 1.2 christos NULL, NULL))
5733 1.2 christos /* Send the first ClientHello */
5734 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
5735 1.2 christos SSL_ERROR_WANT_READ))
5736 1.2 christos /* This should fail because there is no cookie */
5737 1.2 christos || !TEST_int_eq(SSL_stateless(serverssl), 0)
5738 1.2 christos /* Send the second ClientHello */
5739 1.2 christos || !TEST_false(create_ssl_connection(serverssl, clientssl,
5740 1.2 christos SSL_ERROR_WANT_READ))
5741 1.2 christos /* This should succeed because a cookie is now present */
5742 1.2 christos || !TEST_int_eq(SSL_stateless(serverssl), 1)
5743 1.2 christos /* Complete the connection */
5744 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
5745 1.2 christos SSL_ERROR_NONE)))
5746 1.2 christos goto end;
5747 1.2 christos
5748 1.2 christos shutdown_ssl_connection(serverssl, clientssl);
5749 1.2 christos serverssl = clientssl = NULL;
5750 1.2 christos testresult = 1;
5751 1.2 christos
5752 1.2 christos end:
5753 1.2 christos SSL_free(serverssl);
5754 1.2 christos SSL_free(clientssl);
5755 1.2 christos SSL_CTX_free(sctx);
5756 1.2 christos SSL_CTX_free(cctx);
5757 1.2 christos return testresult;
5758 1.2 christos
5759 1.2 christos }
5760 1.2 christos #endif /* OSSL_NO_USABLE_TLS1_3 */
5761 1.2 christos
5762 1.2 christos static int clntaddoldcb = 0;
5763 1.2 christos static int clntparseoldcb = 0;
5764 1.2 christos static int srvaddoldcb = 0;
5765 1.2 christos static int srvparseoldcb = 0;
5766 1.2 christos static int clntaddnewcb = 0;
5767 1.2 christos static int clntparsenewcb = 0;
5768 1.2 christos static int srvaddnewcb = 0;
5769 1.2 christos static int srvparsenewcb = 0;
5770 1.2 christos static int snicb = 0;
5771 1.2 christos
5772 1.2 christos #define TEST_EXT_TYPE1 0xff00
5773 1.2 christos
5774 1.2 christos static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
5775 1.2 christos size_t *outlen, int *al, void *add_arg)
5776 1.2 christos {
5777 1.2 christos int *server = (int *)add_arg;
5778 1.2 christos unsigned char *data;
5779 1.2 christos
5780 1.2 christos if (SSL_is_server(s))
5781 1.2 christos srvaddoldcb++;
5782 1.2 christos else
5783 1.2 christos clntaddoldcb++;
5784 1.2 christos
5785 1.2 christos if (*server != SSL_is_server(s)
5786 1.2 christos || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5787 1.2 christos return -1;
5788 1.2 christos
5789 1.2 christos *data = 1;
5790 1.2 christos *out = data;
5791 1.2 christos *outlen = sizeof(char);
5792 1.2 christos return 1;
5793 1.2 christos }
5794 1.2 christos
5795 1.2 christos static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
5796 1.2 christos void *add_arg)
5797 1.2 christos {
5798 1.2 christos OPENSSL_free((unsigned char *)out);
5799 1.2 christos }
5800 1.2 christos
5801 1.2 christos static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
5802 1.2 christos size_t inlen, int *al, void *parse_arg)
5803 1.2 christos {
5804 1.2 christos int *server = (int *)parse_arg;
5805 1.2 christos
5806 1.2 christos if (SSL_is_server(s))
5807 1.2 christos srvparseoldcb++;
5808 1.2 christos else
5809 1.2 christos clntparseoldcb++;
5810 1.2 christos
5811 1.2 christos if (*server != SSL_is_server(s)
5812 1.2 christos || inlen != sizeof(char)
5813 1.2 christos || *in != 1)
5814 1.2 christos return -1;
5815 1.2 christos
5816 1.2 christos return 1;
5817 1.2 christos }
5818 1.2 christos
5819 1.2 christos static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
5820 1.2 christos const unsigned char **out, size_t *outlen, X509 *x,
5821 1.2 christos size_t chainidx, int *al, void *add_arg)
5822 1.2 christos {
5823 1.2 christos int *server = (int *)add_arg;
5824 1.2 christos unsigned char *data;
5825 1.2 christos
5826 1.2 christos if (SSL_is_server(s))
5827 1.2 christos srvaddnewcb++;
5828 1.2 christos else
5829 1.2 christos clntaddnewcb++;
5830 1.2 christos
5831 1.2 christos if (*server != SSL_is_server(s)
5832 1.2 christos || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5833 1.2 christos return -1;
5834 1.2 christos
5835 1.2 christos *data = 1;
5836 1.2 christos *out = data;
5837 1.2 christos *outlen = sizeof(*data);
5838 1.2 christos return 1;
5839 1.2 christos }
5840 1.2 christos
5841 1.2 christos static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
5842 1.2 christos const unsigned char *out, void *add_arg)
5843 1.2 christos {
5844 1.2 christos OPENSSL_free((unsigned char *)out);
5845 1.2 christos }
5846 1.2 christos
5847 1.2 christos static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
5848 1.2 christos const unsigned char *in, size_t inlen, X509 *x,
5849 1.2 christos size_t chainidx, int *al, void *parse_arg)
5850 1.2 christos {
5851 1.2 christos int *server = (int *)parse_arg;
5852 1.2 christos
5853 1.2 christos if (SSL_is_server(s))
5854 1.2 christos srvparsenewcb++;
5855 1.2 christos else
5856 1.2 christos clntparsenewcb++;
5857 1.2 christos
5858 1.2 christos if (*server != SSL_is_server(s)
5859 1.2 christos || inlen != sizeof(char) || *in != 1)
5860 1.2 christos return -1;
5861 1.2 christos
5862 1.2 christos return 1;
5863 1.2 christos }
5864 1.2 christos
5865 1.2 christos static int sni_cb(SSL *s, int *al, void *arg)
5866 1.2 christos {
5867 1.2 christos SSL_CTX *ctx = (SSL_CTX *)arg;
5868 1.2 christos
5869 1.2 christos if (SSL_set_SSL_CTX(s, ctx) == NULL) {
5870 1.2 christos *al = SSL_AD_INTERNAL_ERROR;
5871 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
5872 1.2 christos }
5873 1.2 christos snicb++;
5874 1.2 christos return SSL_TLSEXT_ERR_OK;
5875 1.2 christos }
5876 1.2 christos
5877 1.2 christos static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
5878 1.2 christos {
5879 1.2 christos return 1;
5880 1.2 christos }
5881 1.2 christos
5882 1.2 christos /*
5883 1.2 christos * Custom call back tests.
5884 1.2 christos * Test 0: Old style callbacks in TLSv1.2
5885 1.2 christos * Test 1: New style callbacks in TLSv1.2
5886 1.2 christos * Test 2: New style callbacks in TLSv1.2 with SNI
5887 1.2 christos * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
5888 1.2 christos * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
5889 1.2 christos * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
5890 1.2 christos */
5891 1.2 christos static int test_custom_exts(int tst)
5892 1.2 christos {
5893 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
5894 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
5895 1.2 christos int testresult = 0;
5896 1.2 christos static int server = 1;
5897 1.2 christos static int client = 0;
5898 1.2 christos SSL_SESSION *sess = NULL;
5899 1.2 christos unsigned int context;
5900 1.2 christos
5901 1.2 christos #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
5902 1.2 christos /* Skip tests for TLSv1.2 and below in this case */
5903 1.2 christos if (tst < 3)
5904 1.2 christos return 1;
5905 1.2 christos #endif
5906 1.2 christos
5907 1.2 christos /* Reset callback counters */
5908 1.2 christos clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
5909 1.2 christos clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
5910 1.2 christos snicb = 0;
5911 1.2 christos
5912 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5913 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
5914 1.2 christos &sctx, &cctx, cert, privkey)))
5915 1.2 christos goto end;
5916 1.2 christos
5917 1.2 christos if (tst == 2
5918 1.2 christos && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
5919 1.2 christos TLS1_VERSION, 0,
5920 1.2 christos &sctx2, NULL, cert, privkey)))
5921 1.2 christos goto end;
5922 1.2 christos
5923 1.2 christos
5924 1.2 christos if (tst < 3) {
5925 1.2 christos SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
5926 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
5927 1.2 christos if (sctx2 != NULL)
5928 1.2 christos SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
5929 1.2 christos }
5930 1.2 christos
5931 1.2 christos if (tst == 5) {
5932 1.2 christos context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
5933 1.2 christos | SSL_EXT_TLS1_3_CERTIFICATE;
5934 1.2 christos SSL_CTX_set_verify(sctx,
5935 1.2 christos SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
5936 1.2 christos verify_cb);
5937 1.2 christos if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
5938 1.2 christos SSL_FILETYPE_PEM), 1)
5939 1.2 christos || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
5940 1.2 christos SSL_FILETYPE_PEM), 1)
5941 1.2 christos || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
5942 1.2 christos goto end;
5943 1.2 christos } else if (tst == 4) {
5944 1.2 christos context = SSL_EXT_CLIENT_HELLO
5945 1.2 christos | SSL_EXT_TLS1_2_SERVER_HELLO
5946 1.2 christos | SSL_EXT_TLS1_3_SERVER_HELLO
5947 1.2 christos | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
5948 1.2 christos | SSL_EXT_TLS1_3_CERTIFICATE
5949 1.2 christos | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
5950 1.2 christos } else {
5951 1.2 christos context = SSL_EXT_CLIENT_HELLO
5952 1.2 christos | SSL_EXT_TLS1_2_SERVER_HELLO
5953 1.2 christos | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
5954 1.2 christos }
5955 1.2 christos
5956 1.2 christos /* Create a client side custom extension */
5957 1.2 christos if (tst == 0) {
5958 1.2 christos if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5959 1.2 christos old_add_cb, old_free_cb,
5960 1.2 christos &client, old_parse_cb,
5961 1.2 christos &client)))
5962 1.2 christos goto end;
5963 1.2 christos } else {
5964 1.2 christos if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
5965 1.2 christos new_add_cb, new_free_cb,
5966 1.2 christos &client, new_parse_cb, &client)))
5967 1.2 christos goto end;
5968 1.2 christos }
5969 1.2 christos
5970 1.2 christos /* Should not be able to add duplicates */
5971 1.2 christos if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5972 1.2 christos old_add_cb, old_free_cb,
5973 1.2 christos &client, old_parse_cb,
5974 1.2 christos &client))
5975 1.2 christos || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
5976 1.2 christos context, new_add_cb,
5977 1.2 christos new_free_cb, &client,
5978 1.2 christos new_parse_cb, &client)))
5979 1.2 christos goto end;
5980 1.2 christos
5981 1.2 christos /* Create a server side custom extension */
5982 1.2 christos if (tst == 0) {
5983 1.2 christos if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5984 1.2 christos old_add_cb, old_free_cb,
5985 1.2 christos &server, old_parse_cb,
5986 1.2 christos &server)))
5987 1.2 christos goto end;
5988 1.2 christos } else {
5989 1.2 christos if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
5990 1.2 christos new_add_cb, new_free_cb,
5991 1.2 christos &server, new_parse_cb, &server)))
5992 1.2 christos goto end;
5993 1.2 christos if (sctx2 != NULL
5994 1.2 christos && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
5995 1.2 christos context, new_add_cb,
5996 1.2 christos new_free_cb, &server,
5997 1.2 christos new_parse_cb, &server)))
5998 1.2 christos goto end;
5999 1.2 christos }
6000 1.2 christos
6001 1.2 christos /* Should not be able to add duplicates */
6002 1.2 christos if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
6003 1.2 christos old_add_cb, old_free_cb,
6004 1.2 christos &server, old_parse_cb,
6005 1.2 christos &server))
6006 1.2 christos || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
6007 1.2 christos context, new_add_cb,
6008 1.2 christos new_free_cb, &server,
6009 1.2 christos new_parse_cb, &server)))
6010 1.2 christos goto end;
6011 1.2 christos
6012 1.2 christos if (tst == 2) {
6013 1.2 christos /* Set up SNI */
6014 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
6015 1.2 christos || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
6016 1.2 christos goto end;
6017 1.2 christos }
6018 1.2 christos
6019 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6020 1.2 christos &clientssl, NULL, NULL))
6021 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6022 1.2 christos SSL_ERROR_NONE)))
6023 1.2 christos goto end;
6024 1.2 christos
6025 1.2 christos if (tst == 0) {
6026 1.2 christos if (clntaddoldcb != 1
6027 1.2 christos || clntparseoldcb != 1
6028 1.2 christos || srvaddoldcb != 1
6029 1.2 christos || srvparseoldcb != 1)
6030 1.2 christos goto end;
6031 1.2 christos } else if (tst == 1 || tst == 2 || tst == 3) {
6032 1.2 christos if (clntaddnewcb != 1
6033 1.2 christos || clntparsenewcb != 1
6034 1.2 christos || srvaddnewcb != 1
6035 1.2 christos || srvparsenewcb != 1
6036 1.2 christos || (tst != 2 && snicb != 0)
6037 1.2 christos || (tst == 2 && snicb != 1))
6038 1.2 christos goto end;
6039 1.2 christos } else if (tst == 5) {
6040 1.2 christos if (clntaddnewcb != 1
6041 1.2 christos || clntparsenewcb != 1
6042 1.2 christos || srvaddnewcb != 1
6043 1.2 christos || srvparsenewcb != 1)
6044 1.2 christos goto end;
6045 1.2 christos } else {
6046 1.2 christos /* In this case there 2 NewSessionTicket messages created */
6047 1.2 christos if (clntaddnewcb != 1
6048 1.2 christos || clntparsenewcb != 5
6049 1.2 christos || srvaddnewcb != 5
6050 1.2 christos || srvparsenewcb != 1)
6051 1.2 christos goto end;
6052 1.2 christos }
6053 1.2 christos
6054 1.2 christos sess = SSL_get1_session(clientssl);
6055 1.2 christos SSL_shutdown(clientssl);
6056 1.2 christos SSL_shutdown(serverssl);
6057 1.2 christos SSL_free(serverssl);
6058 1.2 christos SSL_free(clientssl);
6059 1.2 christos serverssl = clientssl = NULL;
6060 1.2 christos
6061 1.2 christos if (tst == 3 || tst == 5) {
6062 1.2 christos /* We don't bother with the resumption aspects for these tests */
6063 1.2 christos testresult = 1;
6064 1.2 christos goto end;
6065 1.2 christos }
6066 1.2 christos
6067 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6068 1.2 christos NULL, NULL))
6069 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess))
6070 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6071 1.2 christos SSL_ERROR_NONE)))
6072 1.2 christos goto end;
6073 1.2 christos
6074 1.2 christos /*
6075 1.2 christos * For a resumed session we expect to add the ClientHello extension. For the
6076 1.2 christos * old style callbacks we ignore it on the server side because they set
6077 1.2 christos * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
6078 1.2 christos * them.
6079 1.2 christos */
6080 1.2 christos if (tst == 0) {
6081 1.2 christos if (clntaddoldcb != 2
6082 1.2 christos || clntparseoldcb != 1
6083 1.2 christos || srvaddoldcb != 1
6084 1.2 christos || srvparseoldcb != 1)
6085 1.2 christos goto end;
6086 1.2 christos } else if (tst == 1 || tst == 2 || tst == 3) {
6087 1.2 christos if (clntaddnewcb != 2
6088 1.2 christos || clntparsenewcb != 2
6089 1.2 christos || srvaddnewcb != 2
6090 1.2 christos || srvparsenewcb != 2)
6091 1.2 christos goto end;
6092 1.2 christos } else {
6093 1.2 christos /*
6094 1.2 christos * No Certificate message extensions in the resumption handshake,
6095 1.2 christos * 2 NewSessionTickets in the initial handshake, 1 in the resumption
6096 1.2 christos */
6097 1.2 christos if (clntaddnewcb != 2
6098 1.2 christos || clntparsenewcb != 8
6099 1.2 christos || srvaddnewcb != 8
6100 1.2 christos || srvparsenewcb != 2)
6101 1.2 christos goto end;
6102 1.2 christos }
6103 1.2 christos
6104 1.2 christos testresult = 1;
6105 1.2 christos
6106 1.2 christos end:
6107 1.2 christos SSL_SESSION_free(sess);
6108 1.2 christos SSL_free(serverssl);
6109 1.2 christos SSL_free(clientssl);
6110 1.2 christos SSL_CTX_free(sctx2);
6111 1.2 christos SSL_CTX_free(sctx);
6112 1.2 christos SSL_CTX_free(cctx);
6113 1.2 christos return testresult;
6114 1.2 christos }
6115 1.2 christos
6116 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
6117 1.2 christos
6118 1.2 christos #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
6119 1.2 christos | SSL_EXT_CLIENT_HELLO \
6120 1.2 christos | SSL_EXT_TLS1_2_SERVER_HELLO \
6121 1.2 christos | SSL_EXT_IGNORE_ON_RESUMPTION)
6122 1.2 christos
6123 1.2 christos #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
6124 1.2 christos | SSL_EXT_TLS1_2_SERVER_HELLO \
6125 1.2 christos | SSL_EXT_CLIENT_HELLO)
6126 1.2 christos
6127 1.2 christos #define SERVERINFO_CUSTOM \
6128 1.2 christos 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
6129 1.2 christos 0x00, 0x03, \
6130 1.2 christos 0x04, 0x05, 0x06 \
6131 1.2 christos
6132 1.2 christos static const unsigned char serverinfo_custom_tls13[] = {
6133 1.2 christos 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
6134 1.2 christos SERVERINFO_CUSTOM
6135 1.2 christos };
6136 1.2 christos static const unsigned char serverinfo_custom_v2[] = {
6137 1.2 christos 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff,
6138 1.2 christos SERVERINFO_CUSTOM
6139 1.2 christos };
6140 1.2 christos static const unsigned char serverinfo_custom_v1[] = {
6141 1.2 christos SERVERINFO_CUSTOM
6142 1.2 christos };
6143 1.2 christos static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
6144 1.2 christos static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
6145 1.2 christos static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
6146 1.2 christos
6147 1.2 christos static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
6148 1.2 christos unsigned int context,
6149 1.2 christos const unsigned char *in,
6150 1.2 christos size_t inlen, X509 *x,
6151 1.2 christos size_t chainidx, int *al,
6152 1.2 christos void *parse_arg)
6153 1.2 christos {
6154 1.2 christos const size_t len = serverinfo_custom_v1_len;
6155 1.2 christos const unsigned char *si = &serverinfo_custom_v1[len - 3];
6156 1.2 christos int *p_cb_result = (int*)parse_arg;
6157 1.2 christos *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
6158 1.2 christos return 1;
6159 1.2 christos }
6160 1.2 christos
6161 1.2 christos static int test_serverinfo_custom(const int idx)
6162 1.2 christos {
6163 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
6164 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6165 1.2 christos int testresult = 0;
6166 1.2 christos int cb_result = 0;
6167 1.2 christos
6168 1.2 christos /*
6169 1.2 christos * Following variables are set in the switch statement
6170 1.2 christos * according to the test iteration.
6171 1.2 christos * Default values do not make much sense: test would fail with them.
6172 1.2 christos */
6173 1.2 christos int serverinfo_version = 0;
6174 1.2 christos int protocol_version = 0;
6175 1.2 christos unsigned int extension_context = 0;
6176 1.2 christos const unsigned char *si = NULL;
6177 1.2 christos size_t si_len = 0;
6178 1.2 christos
6179 1.2 christos const int call_use_serverinfo_ex = idx > 0;
6180 1.2 christos switch (idx) {
6181 1.2 christos case 0: /* FALLTHROUGH */
6182 1.2 christos case 1:
6183 1.2 christos serverinfo_version = SSL_SERVERINFOV1;
6184 1.2 christos protocol_version = TLS1_2_VERSION;
6185 1.2 christos extension_context = SYNTHV1CONTEXT;
6186 1.2 christos si = serverinfo_custom_v1;
6187 1.2 christos si_len = serverinfo_custom_v1_len;
6188 1.2 christos break;
6189 1.2 christos case 2:
6190 1.2 christos serverinfo_version = SSL_SERVERINFOV2;
6191 1.2 christos protocol_version = TLS1_2_VERSION;
6192 1.2 christos extension_context = SYNTHV1CONTEXT;
6193 1.2 christos si = serverinfo_custom_v2;
6194 1.2 christos si_len = serverinfo_custom_v2_len;
6195 1.2 christos break;
6196 1.2 christos case 3:
6197 1.2 christos serverinfo_version = SSL_SERVERINFOV2;
6198 1.2 christos protocol_version = TLS1_3_VERSION;
6199 1.2 christos extension_context = TLS13CONTEXT;
6200 1.2 christos si = serverinfo_custom_tls13;
6201 1.2 christos si_len = serverinfo_custom_tls13_len;
6202 1.2 christos break;
6203 1.2 christos }
6204 1.2 christos
6205 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx,
6206 1.2 christos TLS_method(),
6207 1.2 christos TLS_method(),
6208 1.2 christos protocol_version,
6209 1.2 christos protocol_version,
6210 1.2 christos &sctx, &cctx, cert, privkey)))
6211 1.2 christos goto end;
6212 1.2 christos
6213 1.2 christos if (call_use_serverinfo_ex) {
6214 1.2 christos if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
6215 1.2 christos si, si_len)))
6216 1.2 christos goto end;
6217 1.2 christos } else {
6218 1.2 christos if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
6219 1.2 christos goto end;
6220 1.2 christos }
6221 1.2 christos
6222 1.2 christos if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
6223 1.2 christos extension_context,
6224 1.2 christos NULL, NULL, NULL,
6225 1.2 christos serverinfo_custom_parse_cb,
6226 1.2 christos &cb_result))
6227 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6228 1.2 christos NULL, NULL))
6229 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6230 1.2 christos SSL_ERROR_NONE))
6231 1.2 christos || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
6232 1.2 christos goto end;
6233 1.2 christos
6234 1.2 christos if (!TEST_true(cb_result))
6235 1.2 christos goto end;
6236 1.2 christos
6237 1.2 christos testresult = 1;
6238 1.2 christos
6239 1.2 christos end:
6240 1.2 christos SSL_free(serverssl);
6241 1.2 christos SSL_free(clientssl);
6242 1.2 christos SSL_CTX_free(sctx);
6243 1.2 christos SSL_CTX_free(cctx);
6244 1.2 christos
6245 1.2 christos return testresult;
6246 1.2 christos }
6247 1.2 christos #endif
6248 1.2 christos
6249 1.2 christos /*
6250 1.2 christos * Test that SSL_export_keying_material() produces expected results. There are
6251 1.2 christos * no test vectors so all we do is test that both sides of the communication
6252 1.2 christos * produce the same results for different protocol versions.
6253 1.2 christos */
6254 1.2 christos #define SMALL_LABEL_LEN 10
6255 1.2 christos #define LONG_LABEL_LEN 249
6256 1.2 christos static int test_export_key_mat(int tst)
6257 1.2 christos {
6258 1.2 christos int testresult = 0;
6259 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
6260 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6261 1.2 christos const char label[LONG_LABEL_LEN + 1] = "test label";
6262 1.2 christos const unsigned char context[] = "context";
6263 1.2 christos const unsigned char *emptycontext = NULL;
6264 1.2 christos unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
6265 1.2 christos unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
6266 1.2 christos size_t labellen;
6267 1.2 christos const int protocols[] = {
6268 1.2 christos TLS1_VERSION,
6269 1.2 christos TLS1_1_VERSION,
6270 1.2 christos TLS1_2_VERSION,
6271 1.2 christos TLS1_3_VERSION,
6272 1.2 christos TLS1_3_VERSION,
6273 1.2 christos TLS1_3_VERSION
6274 1.2 christos };
6275 1.2 christos
6276 1.2 christos #ifdef OPENSSL_NO_TLS1
6277 1.2 christos if (tst == 0)
6278 1.2 christos return 1;
6279 1.2 christos #endif
6280 1.2 christos #ifdef OPENSSL_NO_TLS1_1
6281 1.2 christos if (tst == 1)
6282 1.2 christos return 1;
6283 1.2 christos #endif
6284 1.2 christos if (is_fips && (tst == 0 || tst == 1))
6285 1.2 christos return 1;
6286 1.2 christos #ifdef OPENSSL_NO_TLS1_2
6287 1.2 christos if (tst == 2)
6288 1.2 christos return 1;
6289 1.2 christos #endif
6290 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
6291 1.2 christos if (tst >= 3)
6292 1.2 christos return 1;
6293 1.2 christos #endif
6294 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6295 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
6296 1.2 christos &sctx, &cctx, cert, privkey)))
6297 1.2 christos goto end;
6298 1.2 christos
6299 1.2 christos OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
6300 1.2 christos SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
6301 1.2 christos SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
6302 1.2 christos if ((protocols[tst] < TLS1_2_VERSION) &&
6303 1.2 christos (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
6304 1.2 christos || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
6305 1.2 christos goto end;
6306 1.2 christos
6307 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
6308 1.2 christos NULL)))
6309 1.2 christos goto end;
6310 1.2 christos
6311 1.2 christos /*
6312 1.2 christos * Premature call of SSL_export_keying_material should just fail.
6313 1.2 christos */
6314 1.2 christos if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6315 1.2 christos sizeof(ckeymat1), label,
6316 1.2 christos SMALL_LABEL_LEN + 1, context,
6317 1.2 christos sizeof(context) - 1, 1), 0))
6318 1.2 christos goto end;
6319 1.2 christos
6320 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6321 1.2 christos SSL_ERROR_NONE)))
6322 1.2 christos goto end;
6323 1.2 christos
6324 1.2 christos if (tst == 5) {
6325 1.2 christos /*
6326 1.2 christos * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
6327 1.2 christos * go over that.
6328 1.2 christos */
6329 1.2 christos if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6330 1.2 christos sizeof(ckeymat1), label,
6331 1.2 christos LONG_LABEL_LEN + 1, context,
6332 1.2 christos sizeof(context) - 1, 1), 0))
6333 1.2 christos goto end;
6334 1.2 christos
6335 1.2 christos testresult = 1;
6336 1.2 christos goto end;
6337 1.2 christos } else if (tst == 4) {
6338 1.2 christos labellen = LONG_LABEL_LEN;
6339 1.2 christos } else {
6340 1.2 christos labellen = SMALL_LABEL_LEN;
6341 1.2 christos }
6342 1.2 christos
6343 1.2 christos if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
6344 1.2 christos sizeof(ckeymat1), label,
6345 1.2 christos labellen, context,
6346 1.2 christos sizeof(context) - 1, 1), 1)
6347 1.2 christos || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
6348 1.2 christos sizeof(ckeymat2), label,
6349 1.2 christos labellen,
6350 1.2 christos emptycontext,
6351 1.2 christos 0, 1), 1)
6352 1.2 christos || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
6353 1.2 christos sizeof(ckeymat3), label,
6354 1.2 christos labellen,
6355 1.2 christos NULL, 0, 0), 1)
6356 1.2 christos || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
6357 1.2 christos sizeof(skeymat1), label,
6358 1.2 christos labellen,
6359 1.2 christos context,
6360 1.2 christos sizeof(context) -1, 1),
6361 1.2 christos 1)
6362 1.2 christos || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
6363 1.2 christos sizeof(skeymat2), label,
6364 1.2 christos labellen,
6365 1.2 christos emptycontext,
6366 1.2 christos 0, 1), 1)
6367 1.2 christos || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
6368 1.2 christos sizeof(skeymat3), label,
6369 1.2 christos labellen,
6370 1.2 christos NULL, 0, 0), 1)
6371 1.2 christos /*
6372 1.2 christos * Check that both sides created the same key material with the
6373 1.2 christos * same context.
6374 1.2 christos */
6375 1.2 christos || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6376 1.2 christos sizeof(skeymat1))
6377 1.2 christos /*
6378 1.2 christos * Check that both sides created the same key material with an
6379 1.2 christos * empty context.
6380 1.2 christos */
6381 1.2 christos || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6382 1.2 christos sizeof(skeymat2))
6383 1.2 christos /*
6384 1.2 christos * Check that both sides created the same key material without a
6385 1.2 christos * context.
6386 1.2 christos */
6387 1.2 christos || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
6388 1.2 christos sizeof(skeymat3))
6389 1.2 christos /* Different contexts should produce different results */
6390 1.2 christos || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6391 1.2 christos sizeof(ckeymat2)))
6392 1.2 christos goto end;
6393 1.2 christos
6394 1.2 christos /*
6395 1.2 christos * Check that an empty context and no context produce different results in
6396 1.2 christos * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
6397 1.2 christos */
6398 1.2 christos if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
6399 1.2 christos sizeof(ckeymat3)))
6400 1.2 christos || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
6401 1.2 christos sizeof(ckeymat3))))
6402 1.2 christos goto end;
6403 1.2 christos
6404 1.2 christos testresult = 1;
6405 1.2 christos
6406 1.2 christos end:
6407 1.2 christos SSL_free(serverssl);
6408 1.2 christos SSL_free(clientssl);
6409 1.2 christos SSL_CTX_free(sctx2);
6410 1.2 christos SSL_CTX_free(sctx);
6411 1.2 christos SSL_CTX_free(cctx);
6412 1.2 christos
6413 1.2 christos return testresult;
6414 1.2 christos }
6415 1.2 christos
6416 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
6417 1.2 christos /*
6418 1.2 christos * Test that SSL_export_keying_material_early() produces expected
6419 1.2 christos * results. There are no test vectors so all we do is test that both
6420 1.2 christos * sides of the communication produce the same results for different
6421 1.2 christos * protocol versions.
6422 1.2 christos */
6423 1.2 christos static int test_export_key_mat_early(int idx)
6424 1.2 christos {
6425 1.2 christos static const char label[] = "test label";
6426 1.2 christos static const unsigned char context[] = "context";
6427 1.2 christos int testresult = 0;
6428 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6429 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6430 1.2 christos SSL_SESSION *sess = NULL;
6431 1.2 christos const unsigned char *emptycontext = NULL;
6432 1.2 christos unsigned char ckeymat1[80], ckeymat2[80];
6433 1.2 christos unsigned char skeymat1[80], skeymat2[80];
6434 1.2 christos unsigned char buf[1];
6435 1.2 christos size_t readbytes, written;
6436 1.2 christos
6437 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
6438 1.2 christos &sess, idx, SHA384_DIGEST_LENGTH)))
6439 1.2 christos goto end;
6440 1.2 christos
6441 1.2 christos /* Here writing 0 length early data is enough. */
6442 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
6443 1.2 christos || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
6444 1.2 christos &readbytes),
6445 1.2 christos SSL_READ_EARLY_DATA_ERROR)
6446 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
6447 1.2 christos SSL_EARLY_DATA_ACCEPTED))
6448 1.2 christos goto end;
6449 1.2 christos
6450 1.2 christos if (!TEST_int_eq(SSL_export_keying_material_early(
6451 1.2 christos clientssl, ckeymat1, sizeof(ckeymat1), label,
6452 1.2 christos sizeof(label) - 1, context, sizeof(context) - 1), 1)
6453 1.2 christos || !TEST_int_eq(SSL_export_keying_material_early(
6454 1.2 christos clientssl, ckeymat2, sizeof(ckeymat2), label,
6455 1.2 christos sizeof(label) - 1, emptycontext, 0), 1)
6456 1.2 christos || !TEST_int_eq(SSL_export_keying_material_early(
6457 1.2 christos serverssl, skeymat1, sizeof(skeymat1), label,
6458 1.2 christos sizeof(label) - 1, context, sizeof(context) - 1), 1)
6459 1.2 christos || !TEST_int_eq(SSL_export_keying_material_early(
6460 1.2 christos serverssl, skeymat2, sizeof(skeymat2), label,
6461 1.2 christos sizeof(label) - 1, emptycontext, 0), 1)
6462 1.2 christos /*
6463 1.2 christos * Check that both sides created the same key material with the
6464 1.2 christos * same context.
6465 1.2 christos */
6466 1.2 christos || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6467 1.2 christos sizeof(skeymat1))
6468 1.2 christos /*
6469 1.2 christos * Check that both sides created the same key material with an
6470 1.2 christos * empty context.
6471 1.2 christos */
6472 1.2 christos || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6473 1.2 christos sizeof(skeymat2))
6474 1.2 christos /* Different contexts should produce different results */
6475 1.2 christos || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6476 1.2 christos sizeof(ckeymat2)))
6477 1.2 christos goto end;
6478 1.2 christos
6479 1.2 christos testresult = 1;
6480 1.2 christos
6481 1.2 christos end:
6482 1.2 christos SSL_SESSION_free(sess);
6483 1.2 christos SSL_SESSION_free(clientpsk);
6484 1.2 christos SSL_SESSION_free(serverpsk);
6485 1.2 christos clientpsk = serverpsk = NULL;
6486 1.2 christos SSL_free(serverssl);
6487 1.2 christos SSL_free(clientssl);
6488 1.2 christos SSL_CTX_free(sctx);
6489 1.2 christos SSL_CTX_free(cctx);
6490 1.2 christos
6491 1.2 christos return testresult;
6492 1.2 christos }
6493 1.2 christos
6494 1.2 christos #define NUM_KEY_UPDATE_MESSAGES 40
6495 1.2 christos /*
6496 1.2 christos * Test KeyUpdate.
6497 1.2 christos */
6498 1.2 christos static int test_key_update(void)
6499 1.2 christos {
6500 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6501 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6502 1.2 christos int testresult = 0, i, j;
6503 1.2 christos char buf[20];
6504 1.2 christos static char *mess = "A test message";
6505 1.2 christos
6506 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6507 1.2 christos TLS_client_method(),
6508 1.2 christos TLS1_3_VERSION,
6509 1.2 christos 0,
6510 1.2 christos &sctx, &cctx, cert, privkey))
6511 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6512 1.2 christos NULL, NULL))
6513 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6514 1.2 christos SSL_ERROR_NONE)))
6515 1.2 christos goto end;
6516 1.2 christos
6517 1.2 christos for (j = 0; j < 2; j++) {
6518 1.2 christos /* Send lots of KeyUpdate messages */
6519 1.2 christos for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
6520 1.2 christos if (!TEST_true(SSL_key_update(clientssl,
6521 1.2 christos (j == 0)
6522 1.2 christos ? SSL_KEY_UPDATE_NOT_REQUESTED
6523 1.2 christos : SSL_KEY_UPDATE_REQUESTED))
6524 1.2 christos || !TEST_true(SSL_do_handshake(clientssl)))
6525 1.2 christos goto end;
6526 1.2 christos }
6527 1.2 christos
6528 1.2 christos /* Check that sending and receiving app data is ok */
6529 1.2 christos if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
6530 1.2 christos || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
6531 1.2 christos strlen(mess)))
6532 1.2 christos goto end;
6533 1.2 christos
6534 1.2 christos if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
6535 1.2 christos || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
6536 1.2 christos strlen(mess)))
6537 1.2 christos goto end;
6538 1.2 christos }
6539 1.2 christos
6540 1.2 christos testresult = 1;
6541 1.2 christos
6542 1.2 christos end:
6543 1.2 christos SSL_free(serverssl);
6544 1.2 christos SSL_free(clientssl);
6545 1.2 christos SSL_CTX_free(sctx);
6546 1.2 christos SSL_CTX_free(cctx);
6547 1.2 christos
6548 1.2 christos return testresult;
6549 1.2 christos }
6550 1.2 christos
6551 1.2 christos /*
6552 1.2 christos * Test we can handle a KeyUpdate (update requested) message while
6553 1.2 christos * write data is pending in peer.
6554 1.2 christos * Test 0: Client sends KeyUpdate while Server is writing
6555 1.2 christos * Test 1: Server sends KeyUpdate while Client is writing
6556 1.2 christos */
6557 1.2 christos static int test_key_update_peer_in_write(int tst)
6558 1.2 christos {
6559 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6560 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6561 1.2 christos int testresult = 0;
6562 1.2 christos char buf[20];
6563 1.2 christos static char *mess = "A test message";
6564 1.2 christos BIO *bretry = BIO_new(bio_s_always_retry());
6565 1.2 christos BIO *tmp = NULL;
6566 1.2 christos SSL *peerupdate = NULL, *peerwrite = NULL;
6567 1.2 christos
6568 1.2 christos if (!TEST_ptr(bretry)
6569 1.2 christos || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6570 1.2 christos TLS_client_method(),
6571 1.2 christos TLS1_3_VERSION,
6572 1.2 christos 0,
6573 1.2 christos &sctx, &cctx, cert, privkey))
6574 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6575 1.2 christos NULL, NULL))
6576 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6577 1.2 christos SSL_ERROR_NONE)))
6578 1.2 christos goto end;
6579 1.2 christos
6580 1.2 christos peerupdate = tst == 0 ? clientssl : serverssl;
6581 1.2 christos peerwrite = tst == 0 ? serverssl : clientssl;
6582 1.2 christos
6583 1.2 christos if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
6584 1.2 christos || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
6585 1.2 christos goto end;
6586 1.2 christos
6587 1.2 christos /* Swap the writing endpoint's write BIO to force a retry */
6588 1.2 christos tmp = SSL_get_wbio(peerwrite);
6589 1.2 christos if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6590 1.2 christos tmp = NULL;
6591 1.2 christos goto end;
6592 1.2 christos }
6593 1.2 christos SSL_set0_wbio(peerwrite, bretry);
6594 1.2 christos bretry = NULL;
6595 1.2 christos
6596 1.2 christos /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
6597 1.2 christos if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
6598 1.2 christos || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
6599 1.2 christos goto end;
6600 1.2 christos
6601 1.2 christos /* Reinstate the original writing endpoint's write BIO */
6602 1.2 christos SSL_set0_wbio(peerwrite, tmp);
6603 1.2 christos tmp = NULL;
6604 1.2 christos
6605 1.2 christos /* Now read some data - we will read the key update */
6606 1.2 christos if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
6607 1.2 christos || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
6608 1.2 christos goto end;
6609 1.2 christos
6610 1.2 christos /*
6611 1.2 christos * Complete the write we started previously and read it from the other
6612 1.2 christos * endpoint
6613 1.2 christos */
6614 1.2 christos if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6615 1.2 christos || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6616 1.2 christos goto end;
6617 1.2 christos
6618 1.2 christos /* Write more data to ensure we send the KeyUpdate message back */
6619 1.2 christos if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6620 1.2 christos || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6621 1.2 christos goto end;
6622 1.2 christos
6623 1.2 christos testresult = 1;
6624 1.2 christos
6625 1.2 christos end:
6626 1.2 christos SSL_free(serverssl);
6627 1.2 christos SSL_free(clientssl);
6628 1.2 christos SSL_CTX_free(sctx);
6629 1.2 christos SSL_CTX_free(cctx);
6630 1.2 christos BIO_free(bretry);
6631 1.2 christos BIO_free(tmp);
6632 1.2 christos
6633 1.2 christos return testresult;
6634 1.2 christos }
6635 1.2 christos
6636 1.2 christos /*
6637 1.2 christos * Test we can handle a KeyUpdate (update requested) message while
6638 1.2 christos * peer read data is pending after peer accepted keyupdate(the msg header
6639 1.2 christos * had been read 5 bytes).
6640 1.2 christos * Test 0: Client sends KeyUpdate while Server is reading
6641 1.2 christos * Test 1: Server sends KeyUpdate while Client is reading
6642 1.2 christos */
6643 1.2 christos static int test_key_update_peer_in_read(int tst)
6644 1.2 christos {
6645 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6646 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6647 1.2 christos int testresult = 0;
6648 1.2 christos char prbuf[515], lwbuf[515] = {0};
6649 1.2 christos static char *mess = "A test message";
6650 1.2 christos BIO *lbio = NULL, *pbio = NULL;
6651 1.2 christos SSL *local = NULL, *peer = NULL;
6652 1.2 christos
6653 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6654 1.2 christos TLS_client_method(),
6655 1.2 christos TLS1_3_VERSION,
6656 1.2 christos 0,
6657 1.2 christos &sctx, &cctx, cert, privkey))
6658 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6659 1.2 christos NULL, NULL))
6660 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6661 1.2 christos SSL_ERROR_NONE)))
6662 1.2 christos goto end;
6663 1.2 christos
6664 1.2 christos local = tst == 0 ? clientssl : serverssl;
6665 1.2 christos peer = tst == 0 ? serverssl : clientssl;
6666 1.2 christos
6667 1.2 christos if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6668 1.2 christos goto end;
6669 1.2 christos
6670 1.2 christos SSL_set_bio(local, lbio, lbio);
6671 1.2 christos SSL_set_bio(peer, pbio, pbio);
6672 1.2 christos
6673 1.2 christos /*
6674 1.2 christos * we first write keyupdate msg then appdata in local
6675 1.2 christos * write data in local will fail with SSL_ERROR_WANT_WRITE,because
6676 1.2 christos * lwbuf app data msg size + key updata msg size > 512(the size of
6677 1.2 christos * the bio pair buffer)
6678 1.2 christos */
6679 1.2 christos if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6680 1.2 christos || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
6681 1.2 christos || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6682 1.2 christos goto end;
6683 1.2 christos
6684 1.2 christos /*
6685 1.2 christos * first read keyupdate msg in peer in peer
6686 1.2 christos * then read appdata that we know will fail with SSL_ERROR_WANT_READ
6687 1.2 christos */
6688 1.2 christos if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
6689 1.2 christos || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
6690 1.2 christos goto end;
6691 1.2 christos
6692 1.2 christos /* Now write some data in peer - we will write the key update */
6693 1.2 christos if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
6694 1.2 christos goto end;
6695 1.2 christos
6696 1.2 christos /*
6697 1.2 christos * write data in local previously that we will complete
6698 1.2 christos * read data in peer previously that we will complete
6699 1.2 christos */
6700 1.2 christos if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
6701 1.2 christos || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
6702 1.2 christos goto end;
6703 1.2 christos
6704 1.2 christos /* check that sending and receiving appdata ok */
6705 1.2 christos if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6706 1.2 christos || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6707 1.2 christos goto end;
6708 1.2 christos
6709 1.2 christos testresult = 1;
6710 1.2 christos
6711 1.2 christos end:
6712 1.2 christos SSL_free(serverssl);
6713 1.2 christos SSL_free(clientssl);
6714 1.2 christos SSL_CTX_free(sctx);
6715 1.2 christos SSL_CTX_free(cctx);
6716 1.2 christos
6717 1.2 christos return testresult;
6718 1.2 christos }
6719 1.2 christos
6720 1.2 christos /*
6721 1.2 christos * Test we can't send a KeyUpdate (update requested) message while
6722 1.2 christos * local write data is pending.
6723 1.2 christos * Test 0: Client sends KeyUpdate while Client is writing
6724 1.2 christos * Test 1: Server sends KeyUpdate while Server is writing
6725 1.2 christos */
6726 1.2 christos static int test_key_update_local_in_write(int tst)
6727 1.2 christos {
6728 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6729 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6730 1.2 christos int testresult = 0;
6731 1.2 christos char buf[20];
6732 1.2 christos static char *mess = "A test message";
6733 1.2 christos BIO *bretry = BIO_new(bio_s_always_retry());
6734 1.2 christos BIO *tmp = NULL;
6735 1.2 christos SSL *local = NULL, *peer = NULL;
6736 1.2 christos
6737 1.2 christos if (!TEST_ptr(bretry)
6738 1.2 christos || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6739 1.2 christos TLS_client_method(),
6740 1.2 christos TLS1_3_VERSION,
6741 1.2 christos 0,
6742 1.2 christos &sctx, &cctx, cert, privkey))
6743 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6744 1.2 christos NULL, NULL))
6745 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6746 1.2 christos SSL_ERROR_NONE)))
6747 1.2 christos goto end;
6748 1.2 christos
6749 1.2 christos local = tst == 0 ? clientssl : serverssl;
6750 1.2 christos peer = tst == 0 ? serverssl : clientssl;
6751 1.2 christos
6752 1.2 christos /* Swap the writing endpoint's write BIO to force a retry */
6753 1.2 christos tmp = SSL_get_wbio(local);
6754 1.2 christos if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6755 1.2 christos tmp = NULL;
6756 1.2 christos goto end;
6757 1.2 christos }
6758 1.2 christos SSL_set0_wbio(local, bretry);
6759 1.2 christos bretry = NULL;
6760 1.2 christos
6761 1.2 christos /* write data in local will fail with SSL_ERROR_WANT_WRITE */
6762 1.2 christos if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
6763 1.2 christos || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6764 1.2 christos goto end;
6765 1.2 christos
6766 1.2 christos /* Reinstate the original writing endpoint's write BIO */
6767 1.2 christos SSL_set0_wbio(local, tmp);
6768 1.2 christos tmp = NULL;
6769 1.2 christos
6770 1.2 christos /* SSL_key_update will fail, because writing in local*/
6771 1.2 christos if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6772 1.2 christos || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
6773 1.2 christos goto end;
6774 1.2 christos
6775 1.2 christos ERR_clear_error();
6776 1.2 christos /* write data in local previously that we will complete */
6777 1.2 christos if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
6778 1.2 christos goto end;
6779 1.2 christos
6780 1.2 christos /* SSL_key_update will succeed because there is no pending write data */
6781 1.2 christos if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6782 1.2 christos || !TEST_int_eq(SSL_do_handshake(local), 1))
6783 1.2 christos goto end;
6784 1.2 christos
6785 1.2 christos /*
6786 1.2 christos * we write some appdata in local
6787 1.2 christos * read data in peer - we will read the keyupdate msg
6788 1.2 christos */
6789 1.2 christos if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6790 1.2 christos || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
6791 1.2 christos goto end;
6792 1.2 christos
6793 1.2 christos /* Write more peer more data to ensure we send the keyupdate message back */
6794 1.2 christos if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6795 1.2 christos || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
6796 1.2 christos goto end;
6797 1.2 christos
6798 1.2 christos testresult = 1;
6799 1.2 christos
6800 1.2 christos end:
6801 1.2 christos SSL_free(serverssl);
6802 1.2 christos SSL_free(clientssl);
6803 1.2 christos SSL_CTX_free(sctx);
6804 1.2 christos SSL_CTX_free(cctx);
6805 1.2 christos BIO_free(bretry);
6806 1.2 christos BIO_free(tmp);
6807 1.2 christos
6808 1.2 christos return testresult;
6809 1.2 christos }
6810 1.2 christos
6811 1.2 christos /*
6812 1.2 christos * Test we can handle a KeyUpdate (update requested) message while
6813 1.2 christos * local read data is pending(the msg header had been read 5 bytes).
6814 1.2 christos * Test 0: Client sends KeyUpdate while Client is reading
6815 1.2 christos * Test 1: Server sends KeyUpdate while Server is reading
6816 1.2 christos */
6817 1.2 christos static int test_key_update_local_in_read(int tst)
6818 1.2 christos {
6819 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6820 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6821 1.2 christos int testresult = 0;
6822 1.2 christos char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
6823 1.2 christos static char *mess = "A test message";
6824 1.2 christos BIO *lbio = NULL, *pbio = NULL;
6825 1.2 christos SSL *local = NULL, *peer = NULL;
6826 1.2 christos
6827 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6828 1.2 christos TLS_client_method(),
6829 1.2 christos TLS1_3_VERSION,
6830 1.2 christos 0,
6831 1.2 christos &sctx, &cctx, cert, privkey))
6832 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6833 1.2 christos NULL, NULL))
6834 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6835 1.2 christos SSL_ERROR_NONE)))
6836 1.2 christos goto end;
6837 1.2 christos
6838 1.2 christos local = tst == 0 ? clientssl : serverssl;
6839 1.2 christos peer = tst == 0 ? serverssl : clientssl;
6840 1.2 christos
6841 1.2 christos if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6842 1.2 christos goto end;
6843 1.2 christos
6844 1.2 christos SSL_set_bio(local, lbio, lbio);
6845 1.2 christos SSL_set_bio(peer, pbio, pbio);
6846 1.2 christos
6847 1.2 christos /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
6848 1.2 christos if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
6849 1.2 christos || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
6850 1.2 christos goto end;
6851 1.2 christos
6852 1.2 christos /* read appdata in local will fail with SSL_ERROR_WANT_READ */
6853 1.2 christos if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
6854 1.2 christos || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
6855 1.2 christos goto end;
6856 1.2 christos
6857 1.2 christos /* SSL_do_handshake will send keyupdate msg */
6858 1.2 christos if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6859 1.2 christos || !TEST_int_eq(SSL_do_handshake(local), 1))
6860 1.2 christos goto end;
6861 1.2 christos
6862 1.2 christos /*
6863 1.2 christos * write data in peer previously that we will complete
6864 1.2 christos * read data in local previously that we will complete
6865 1.2 christos */
6866 1.2 christos if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
6867 1.2 christos || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
6868 1.2 christos goto end;
6869 1.2 christos
6870 1.2 christos /*
6871 1.2 christos * write data in local
6872 1.2 christos * read data in peer - we will read the key update
6873 1.2 christos */
6874 1.2 christos if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6875 1.2 christos || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6876 1.2 christos goto end;
6877 1.2 christos
6878 1.2 christos /* Write more peer data to ensure we send the keyupdate message back */
6879 1.2 christos if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6880 1.2 christos || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
6881 1.2 christos goto end;
6882 1.2 christos
6883 1.2 christos testresult = 1;
6884 1.2 christos
6885 1.2 christos end:
6886 1.2 christos SSL_free(serverssl);
6887 1.2 christos SSL_free(clientssl);
6888 1.2 christos SSL_CTX_free(sctx);
6889 1.2 christos SSL_CTX_free(cctx);
6890 1.2 christos
6891 1.2 christos return testresult;
6892 1.2 christos }
6893 1.2 christos #endif /* OSSL_NO_USABLE_TLS1_3 */
6894 1.2 christos
6895 1.2 christos static int test_ssl_clear(int idx)
6896 1.2 christos {
6897 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
6898 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
6899 1.2 christos int testresult = 0;
6900 1.2 christos
6901 1.2 christos #ifdef OPENSSL_NO_TLS1_2
6902 1.2 christos if (idx == 1)
6903 1.2 christos return 1;
6904 1.2 christos #endif
6905 1.2 christos
6906 1.2 christos /* Create an initial connection */
6907 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6908 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
6909 1.2 christos &sctx, &cctx, cert, privkey))
6910 1.2 christos || (idx == 1
6911 1.2 christos && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
6912 1.2 christos TLS1_2_VERSION)))
6913 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6914 1.2 christos &clientssl, NULL, NULL))
6915 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6916 1.2 christos SSL_ERROR_NONE)))
6917 1.2 christos goto end;
6918 1.2 christos
6919 1.2 christos SSL_shutdown(clientssl);
6920 1.2 christos SSL_shutdown(serverssl);
6921 1.2 christos SSL_free(serverssl);
6922 1.2 christos serverssl = NULL;
6923 1.2 christos
6924 1.2 christos /* Clear clientssl - we're going to reuse the object */
6925 1.2 christos if (!TEST_true(SSL_clear(clientssl)))
6926 1.2 christos goto end;
6927 1.2 christos
6928 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6929 1.2 christos NULL, NULL))
6930 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
6931 1.2 christos SSL_ERROR_NONE))
6932 1.2 christos || !TEST_true(SSL_session_reused(clientssl)))
6933 1.2 christos goto end;
6934 1.2 christos
6935 1.2 christos SSL_shutdown(clientssl);
6936 1.2 christos SSL_shutdown(serverssl);
6937 1.2 christos
6938 1.2 christos testresult = 1;
6939 1.2 christos
6940 1.2 christos end:
6941 1.2 christos SSL_free(serverssl);
6942 1.2 christos SSL_free(clientssl);
6943 1.2 christos SSL_CTX_free(sctx);
6944 1.2 christos SSL_CTX_free(cctx);
6945 1.2 christos
6946 1.2 christos return testresult;
6947 1.2 christos }
6948 1.2 christos
6949 1.2 christos /* Parse CH and retrieve any MFL extension value if present */
6950 1.2 christos static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
6951 1.2 christos {
6952 1.2 christos long len;
6953 1.2 christos unsigned char *data;
6954 1.2 christos PACKET pkt, pkt2, pkt3;
6955 1.2 christos unsigned int MFL_code = 0, type = 0;
6956 1.2 christos
6957 1.2 christos if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
6958 1.2 christos goto end;
6959 1.2 christos
6960 1.2 christos memset(&pkt, 0, sizeof(pkt));
6961 1.2 christos memset(&pkt2, 0, sizeof(pkt2));
6962 1.2 christos memset(&pkt3, 0, sizeof(pkt3));
6963 1.2 christos
6964 1.2 christos if (!TEST_long_gt(len, 0)
6965 1.2 christos || !TEST_true( PACKET_buf_init( &pkt, data, len ) )
6966 1.2 christos /* Skip the record header */
6967 1.2 christos || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
6968 1.2 christos /* Skip the handshake message header */
6969 1.2 christos || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
6970 1.2 christos /* Skip client version and random */
6971 1.2 christos || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
6972 1.2 christos + SSL3_RANDOM_SIZE))
6973 1.2 christos /* Skip session id */
6974 1.2 christos || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6975 1.2 christos /* Skip ciphers */
6976 1.2 christos || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
6977 1.2 christos /* Skip compression */
6978 1.2 christos || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6979 1.2 christos /* Extensions len */
6980 1.2 christos || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6981 1.2 christos goto end;
6982 1.2 christos
6983 1.2 christos /* Loop through all extensions */
6984 1.2 christos while (PACKET_remaining(&pkt2)) {
6985 1.2 christos if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
6986 1.2 christos || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
6987 1.2 christos goto end;
6988 1.2 christos
6989 1.2 christos if (type == TLSEXT_TYPE_max_fragment_length) {
6990 1.2 christos if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
6991 1.2 christos || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
6992 1.2 christos goto end;
6993 1.2 christos
6994 1.2 christos *mfl_codemfl_code = MFL_code;
6995 1.2 christos return 1;
6996 1.2 christos }
6997 1.2 christos }
6998 1.2 christos
6999 1.2 christos end:
7000 1.2 christos return 0;
7001 1.2 christos }
7002 1.2 christos
7003 1.2 christos /* Maximum-Fragment-Length TLS extension mode to test */
7004 1.2 christos static const unsigned char max_fragment_len_test[] = {
7005 1.2 christos TLSEXT_max_fragment_length_512,
7006 1.2 christos TLSEXT_max_fragment_length_1024,
7007 1.2 christos TLSEXT_max_fragment_length_2048,
7008 1.2 christos TLSEXT_max_fragment_length_4096
7009 1.2 christos };
7010 1.2 christos
7011 1.2 christos static int test_max_fragment_len_ext(int idx_tst)
7012 1.2 christos {
7013 1.2 christos SSL_CTX *ctx = NULL;
7014 1.2 christos SSL *con = NULL;
7015 1.2 christos int testresult = 0, MFL_mode = 0;
7016 1.2 christos BIO *rbio, *wbio;
7017 1.2 christos
7018 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
7019 1.2 christos TLS1_VERSION, 0, NULL, &ctx, NULL,
7020 1.2 christos NULL)))
7021 1.2 christos return 0;
7022 1.2 christos
7023 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
7024 1.2 christos ctx, max_fragment_len_test[idx_tst])))
7025 1.2 christos goto end;
7026 1.2 christos
7027 1.2 christos con = SSL_new(ctx);
7028 1.2 christos if (!TEST_ptr(con))
7029 1.2 christos goto end;
7030 1.2 christos
7031 1.2 christos rbio = BIO_new(BIO_s_mem());
7032 1.2 christos wbio = BIO_new(BIO_s_mem());
7033 1.2 christos if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
7034 1.2 christos BIO_free(rbio);
7035 1.2 christos BIO_free(wbio);
7036 1.2 christos goto end;
7037 1.2 christos }
7038 1.2 christos
7039 1.2 christos SSL_set_bio(con, rbio, wbio);
7040 1.2 christos
7041 1.2 christos if (!TEST_int_le(SSL_connect(con), 0)) {
7042 1.2 christos /* This shouldn't succeed because we don't have a server! */
7043 1.2 christos goto end;
7044 1.2 christos }
7045 1.2 christos
7046 1.2 christos if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
7047 1.2 christos /* no MFL in client hello */
7048 1.2 christos goto end;
7049 1.2 christos if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
7050 1.2 christos goto end;
7051 1.2 christos
7052 1.2 christos testresult = 1;
7053 1.2 christos
7054 1.2 christos end:
7055 1.2 christos SSL_free(con);
7056 1.2 christos SSL_CTX_free(ctx);
7057 1.2 christos
7058 1.2 christos return testresult;
7059 1.2 christos }
7060 1.2 christos
7061 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
7062 1.2 christos static int test_pha_key_update(void)
7063 1.2 christos {
7064 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
7065 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
7066 1.2 christos int testresult = 0;
7067 1.2 christos
7068 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7069 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
7070 1.2 christos &sctx, &cctx, cert, privkey)))
7071 1.2 christos return 0;
7072 1.2 christos
7073 1.2 christos if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
7074 1.2 christos || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
7075 1.2 christos || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
7076 1.2 christos || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
7077 1.2 christos goto end;
7078 1.2 christos
7079 1.2 christos SSL_CTX_set_post_handshake_auth(cctx, 1);
7080 1.2 christos
7081 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7082 1.2 christos NULL, NULL)))
7083 1.2 christos goto end;
7084 1.2 christos
7085 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7086 1.2 christos SSL_ERROR_NONE)))
7087 1.2 christos goto end;
7088 1.2 christos
7089 1.2 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
7090 1.2 christos if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
7091 1.2 christos goto end;
7092 1.2 christos
7093 1.2 christos if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
7094 1.2 christos goto end;
7095 1.2 christos
7096 1.2 christos /* Start handshake on the server */
7097 1.2 christos if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
7098 1.2 christos goto end;
7099 1.2 christos
7100 1.2 christos /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
7101 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7102 1.2 christos SSL_ERROR_NONE)))
7103 1.2 christos goto end;
7104 1.2 christos
7105 1.2 christos SSL_shutdown(clientssl);
7106 1.2 christos SSL_shutdown(serverssl);
7107 1.2 christos
7108 1.2 christos testresult = 1;
7109 1.2 christos
7110 1.2 christos end:
7111 1.2 christos SSL_free(serverssl);
7112 1.2 christos SSL_free(clientssl);
7113 1.2 christos SSL_CTX_free(sctx);
7114 1.2 christos SSL_CTX_free(cctx);
7115 1.2 christos return testresult;
7116 1.2 christos }
7117 1.2 christos #endif
7118 1.2 christos
7119 1.2 christos #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
7120 1.2 christos
7121 1.2 christos static SRP_VBASE *vbase = NULL;
7122 1.2 christos
7123 1.2 christos static int ssl_srp_cb(SSL *s, int *ad, void *arg)
7124 1.2 christos {
7125 1.2 christos int ret = SSL3_AL_FATAL;
7126 1.2 christos char *username;
7127 1.2 christos SRP_user_pwd *user = NULL;
7128 1.2 christos
7129 1.2 christos username = SSL_get_srp_username(s);
7130 1.2 christos if (username == NULL) {
7131 1.2 christos *ad = SSL_AD_INTERNAL_ERROR;
7132 1.2 christos goto err;
7133 1.2 christos }
7134 1.2 christos
7135 1.2 christos user = SRP_VBASE_get1_by_user(vbase, username);
7136 1.2 christos if (user == NULL) {
7137 1.2 christos *ad = SSL_AD_INTERNAL_ERROR;
7138 1.2 christos goto err;
7139 1.2 christos }
7140 1.2 christos
7141 1.2 christos if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
7142 1.2 christos user->info) <= 0) {
7143 1.2 christos *ad = SSL_AD_INTERNAL_ERROR;
7144 1.2 christos goto err;
7145 1.2 christos }
7146 1.2 christos
7147 1.2 christos ret = 0;
7148 1.2 christos
7149 1.2 christos err:
7150 1.2 christos SRP_user_pwd_free(user);
7151 1.2 christos return ret;
7152 1.2 christos }
7153 1.2 christos
7154 1.2 christos static int create_new_vfile(char *userid, char *password, const char *filename)
7155 1.2 christos {
7156 1.2 christos char *gNid = NULL;
7157 1.2 christos OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
7158 1.2 christos TXT_DB *db = NULL;
7159 1.2 christos int ret = 0;
7160 1.2 christos BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
7161 1.2 christos size_t i;
7162 1.2 christos
7163 1.2 christos if (!TEST_ptr(dummy) || !TEST_ptr(row))
7164 1.2 christos goto end;
7165 1.2 christos
7166 1.2 christos gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
7167 1.2 christos &row[DB_srpverifier], NULL, NULL, libctx, NULL);
7168 1.2 christos if (!TEST_ptr(gNid))
7169 1.2 christos goto end;
7170 1.2 christos
7171 1.2 christos /*
7172 1.2 christos * The only way to create an empty TXT_DB is to provide a BIO with no data
7173 1.2 christos * in it!
7174 1.2 christos */
7175 1.2 christos db = TXT_DB_read(dummy, DB_NUMBER);
7176 1.2 christos if (!TEST_ptr(db))
7177 1.2 christos goto end;
7178 1.2 christos
7179 1.2 christos out = BIO_new_file(filename, "w");
7180 1.2 christos if (!TEST_ptr(out))
7181 1.2 christos goto end;
7182 1.2 christos
7183 1.2 christos row[DB_srpid] = OPENSSL_strdup(userid);
7184 1.2 christos row[DB_srptype] = OPENSSL_strdup("V");
7185 1.2 christos row[DB_srpgN] = OPENSSL_strdup(gNid);
7186 1.2 christos
7187 1.2 christos if (!TEST_ptr(row[DB_srpid])
7188 1.2 christos || !TEST_ptr(row[DB_srptype])
7189 1.2 christos || !TEST_ptr(row[DB_srpgN])
7190 1.2 christos || !TEST_true(TXT_DB_insert(db, row)))
7191 1.2 christos goto end;
7192 1.2 christos
7193 1.2 christos row = NULL;
7194 1.2 christos
7195 1.2 christos if (TXT_DB_write(out, db) <= 0)
7196 1.2 christos goto end;
7197 1.2 christos
7198 1.2 christos ret = 1;
7199 1.2 christos end:
7200 1.2 christos if (row != NULL) {
7201 1.2 christos for (i = 0; i < DB_NUMBER; i++)
7202 1.2 christos OPENSSL_free(row[i]);
7203 1.2 christos }
7204 1.2 christos OPENSSL_free(row);
7205 1.2 christos BIO_free(dummy);
7206 1.2 christos BIO_free(out);
7207 1.2 christos TXT_DB_free(db);
7208 1.2 christos
7209 1.2 christos return ret;
7210 1.2 christos }
7211 1.2 christos
7212 1.2 christos static int create_new_vbase(char *userid, char *password)
7213 1.2 christos {
7214 1.2 christos BIGNUM *verifier = NULL, *salt = NULL;
7215 1.2 christos const SRP_gN *lgN = NULL;
7216 1.2 christos SRP_user_pwd *user_pwd = NULL;
7217 1.2 christos int ret = 0;
7218 1.2 christos
7219 1.2 christos lgN = SRP_get_default_gN(NULL);
7220 1.2 christos if (!TEST_ptr(lgN))
7221 1.2 christos goto end;
7222 1.2 christos
7223 1.2 christos if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
7224 1.2 christos lgN->N, lgN->g, libctx, NULL)))
7225 1.2 christos goto end;
7226 1.2 christos
7227 1.2 christos user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
7228 1.2 christos if (!TEST_ptr(user_pwd))
7229 1.2 christos goto end;
7230 1.2 christos
7231 1.2 christos user_pwd->N = lgN->N;
7232 1.2 christos user_pwd->g = lgN->g;
7233 1.2 christos user_pwd->id = OPENSSL_strdup(userid);
7234 1.2 christos if (!TEST_ptr(user_pwd->id))
7235 1.2 christos goto end;
7236 1.2 christos
7237 1.2 christos user_pwd->v = verifier;
7238 1.2 christos user_pwd->s = salt;
7239 1.2 christos verifier = salt = NULL;
7240 1.2 christos
7241 1.2 christos if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
7242 1.2 christos goto end;
7243 1.2 christos user_pwd = NULL;
7244 1.2 christos
7245 1.2 christos ret = 1;
7246 1.2 christos end:
7247 1.2 christos SRP_user_pwd_free(user_pwd);
7248 1.2 christos BN_free(salt);
7249 1.2 christos BN_free(verifier);
7250 1.2 christos
7251 1.2 christos return ret;
7252 1.2 christos }
7253 1.2 christos
7254 1.2 christos /*
7255 1.2 christos * SRP tests
7256 1.2 christos *
7257 1.2 christos * Test 0: Simple successful SRP connection, new vbase
7258 1.2 christos * Test 1: Connection failure due to bad password, new vbase
7259 1.2 christos * Test 2: Simple successful SRP connection, vbase loaded from existing file
7260 1.2 christos * Test 3: Connection failure due to bad password, vbase loaded from existing
7261 1.2 christos * file
7262 1.2 christos * Test 4: Simple successful SRP connection, vbase loaded from new file
7263 1.2 christos * Test 5: Connection failure due to bad password, vbase loaded from new file
7264 1.2 christos */
7265 1.2 christos static int test_srp(int tst)
7266 1.2 christos {
7267 1.2 christos char *userid = "test", *password = "password", *tstsrpfile;
7268 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
7269 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
7270 1.2 christos int ret, testresult = 0;
7271 1.2 christos
7272 1.2 christos vbase = SRP_VBASE_new(NULL);
7273 1.2 christos if (!TEST_ptr(vbase))
7274 1.2 christos goto end;
7275 1.2 christos
7276 1.2 christos if (tst == 0 || tst == 1) {
7277 1.2 christos if (!TEST_true(create_new_vbase(userid, password)))
7278 1.2 christos goto end;
7279 1.2 christos } else {
7280 1.2 christos if (tst == 4 || tst == 5) {
7281 1.2 christos if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
7282 1.2 christos goto end;
7283 1.2 christos tstsrpfile = tmpfilename;
7284 1.2 christos } else {
7285 1.2 christos tstsrpfile = srpvfile;
7286 1.2 christos }
7287 1.2 christos if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
7288 1.2 christos goto end;
7289 1.2 christos }
7290 1.2 christos
7291 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7292 1.2 christos TLS_client_method(), TLS1_VERSION, 0,
7293 1.2 christos &sctx, &cctx, cert, privkey)))
7294 1.2 christos goto end;
7295 1.2 christos
7296 1.2 christos if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
7297 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
7298 1.2 christos || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
7299 1.2 christos || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
7300 1.2 christos || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
7301 1.2 christos goto end;
7302 1.2 christos
7303 1.2 christos if (tst % 2 == 1) {
7304 1.2 christos if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
7305 1.2 christos goto end;
7306 1.2 christos } else {
7307 1.2 christos if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
7308 1.2 christos goto end;
7309 1.2 christos }
7310 1.2 christos
7311 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7312 1.2 christos NULL, NULL)))
7313 1.2 christos goto end;
7314 1.2 christos
7315 1.2 christos ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
7316 1.2 christos if (ret) {
7317 1.2 christos if (!TEST_true(tst % 2 == 0))
7318 1.2 christos goto end;
7319 1.2 christos } else {
7320 1.2 christos if (!TEST_true(tst % 2 == 1))
7321 1.2 christos goto end;
7322 1.2 christos }
7323 1.2 christos
7324 1.2 christos testresult = 1;
7325 1.2 christos
7326 1.2 christos end:
7327 1.2 christos SRP_VBASE_free(vbase);
7328 1.2 christos vbase = NULL;
7329 1.2 christos SSL_free(serverssl);
7330 1.2 christos SSL_free(clientssl);
7331 1.2 christos SSL_CTX_free(sctx);
7332 1.2 christos SSL_CTX_free(cctx);
7333 1.2 christos
7334 1.2 christos return testresult;
7335 1.2 christos }
7336 1.2 christos #endif
7337 1.2 christos
7338 1.2 christos static int info_cb_failed = 0;
7339 1.2 christos static int info_cb_offset = 0;
7340 1.2 christos static int info_cb_this_state = -1;
7341 1.2 christos
7342 1.2 christos static struct info_cb_states_st {
7343 1.2 christos int where;
7344 1.2 christos const char *statestr;
7345 1.2 christos } info_cb_states[][60] = {
7346 1.2 christos {
7347 1.2 christos /* TLSv1.2 server followed by resumption */
7348 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7349 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7350 1.2 christos {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
7351 1.2 christos {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
7352 1.2 christos {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
7353 1.2 christos {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7354 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7355 1.2 christos {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7356 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
7357 1.2 christos {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7358 1.2 christos {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
7359 1.2 christos {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7360 1.2 christos {SSL_CB_EXIT, NULL}, {0, NULL},
7361 1.2 christos }, {
7362 1.2 christos /* TLSv1.2 client followed by resumption */
7363 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7364 1.2 christos {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7365 1.2 christos {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
7366 1.2 christos {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
7367 1.2 christos {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
7368 1.2 christos {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7369 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7370 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7371 1.2 christos {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7372 1.2 christos {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7373 1.2 christos {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7374 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
7375 1.2 christos }, {
7376 1.2 christos /* TLSv1.3 server followed by resumption */
7377 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7378 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7379 1.2 christos {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
7380 1.2 christos {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
7381 1.2 christos {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
7382 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7383 1.2 christos {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7384 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7385 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7386 1.2 christos {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7387 1.2 christos {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
7388 1.2 christos {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7389 1.2 christos {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7390 1.2 christos }, {
7391 1.2 christos /* TLSv1.3 client followed by resumption */
7392 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7393 1.2 christos {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7394 1.2 christos {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
7395 1.2 christos {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
7396 1.2 christos {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7397 1.2 christos {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7398 1.2 christos {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
7399 1.2 christos {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
7400 1.2 christos {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7401 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
7402 1.2 christos {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7403 1.2 christos {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7404 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7405 1.2 christos {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
7406 1.2 christos {SSL_CB_EXIT, NULL}, {0, NULL},
7407 1.2 christos }, {
7408 1.2 christos /* TLSv1.3 server, early_data */
7409 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7410 1.2 christos {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7411 1.2 christos {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7412 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7413 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7414 1.2 christos {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
7415 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7416 1.2 christos {SSL_CB_EXIT, NULL}, {0, NULL},
7417 1.2 christos }, {
7418 1.2 christos /* TLSv1.3 client, early_data */
7419 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7420 1.2 christos {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
7421 1.2 christos {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7422 1.2 christos {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7423 1.2 christos {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7424 1.2 christos {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
7425 1.2 christos {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7426 1.2 christos {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7427 1.2 christos {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7428 1.2 christos }, {
7429 1.2 christos {0, NULL},
7430 1.2 christos }
7431 1.2 christos };
7432 1.2 christos
7433 1.2 christos static void sslapi_info_callback(const SSL *s, int where, int ret)
7434 1.2 christos {
7435 1.2 christos struct info_cb_states_st *state = info_cb_states[info_cb_offset];
7436 1.2 christos
7437 1.2 christos /* We do not ever expect a connection to fail in this test */
7438 1.2 christos if (!TEST_false(ret == 0)) {
7439 1.2 christos info_cb_failed = 1;
7440 1.2 christos return;
7441 1.2 christos }
7442 1.2 christos
7443 1.2 christos /*
7444 1.2 christos * Do some sanity checks. We never expect these things to happen in this
7445 1.2 christos * test
7446 1.2 christos */
7447 1.2 christos if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
7448 1.2 christos || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
7449 1.2 christos || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
7450 1.2 christos info_cb_failed = 1;
7451 1.2 christos return;
7452 1.2 christos }
7453 1.2 christos
7454 1.2 christos /* Now check we're in the right state */
7455 1.2 christos if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
7456 1.2 christos info_cb_failed = 1;
7457 1.2 christos return;
7458 1.2 christos }
7459 1.2 christos if ((where & SSL_CB_LOOP) != 0
7460 1.2 christos && !TEST_int_eq(strcmp(SSL_state_string(s),
7461 1.2 christos state[info_cb_this_state].statestr), 0)) {
7462 1.2 christos info_cb_failed = 1;
7463 1.2 christos return;
7464 1.2 christos }
7465 1.2 christos
7466 1.2 christos /*
7467 1.2 christos * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
7468 1.2 christos */
7469 1.2 christos if ((where & SSL_CB_HANDSHAKE_DONE)
7470 1.2 christos && SSL_in_init((SSL *)s) != 0) {
7471 1.2 christos info_cb_failed = 1;
7472 1.2 christos return;
7473 1.2 christos }
7474 1.2 christos }
7475 1.2 christos
7476 1.2 christos /*
7477 1.2 christos * Test the info callback gets called when we expect it to.
7478 1.2 christos *
7479 1.2 christos * Test 0: TLSv1.2, server
7480 1.2 christos * Test 1: TLSv1.2, client
7481 1.2 christos * Test 2: TLSv1.3, server
7482 1.2 christos * Test 3: TLSv1.3, client
7483 1.2 christos * Test 4: TLSv1.3, server, early_data
7484 1.2 christos * Test 5: TLSv1.3, client, early_data
7485 1.2 christos */
7486 1.2 christos static int test_info_callback(int tst)
7487 1.2 christos {
7488 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
7489 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
7490 1.2 christos SSL_SESSION *clntsess = NULL;
7491 1.2 christos int testresult = 0;
7492 1.2 christos int tlsvers;
7493 1.2 christos
7494 1.2 christos if (tst < 2) {
7495 1.2 christos /* We need either ECDHE or DHE for the TLSv1.2 test to work */
7496 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
7497 1.2 christos || !defined(OPENSSL_NO_DH))
7498 1.2 christos tlsvers = TLS1_2_VERSION;
7499 1.2 christos #else
7500 1.2 christos return 1;
7501 1.2 christos #endif
7502 1.2 christos } else {
7503 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
7504 1.2 christos tlsvers = TLS1_3_VERSION;
7505 1.2 christos #else
7506 1.2 christos return 1;
7507 1.2 christos #endif
7508 1.2 christos }
7509 1.2 christos
7510 1.2 christos /* Reset globals */
7511 1.2 christos info_cb_failed = 0;
7512 1.2 christos info_cb_this_state = -1;
7513 1.2 christos info_cb_offset = tst;
7514 1.2 christos
7515 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
7516 1.2 christos if (tst >= 4) {
7517 1.2 christos SSL_SESSION *sess = NULL;
7518 1.2 christos size_t written, readbytes;
7519 1.2 christos unsigned char buf[80];
7520 1.2 christos time_t timer;
7521 1.2 christos
7522 1.2 christos /* early_data tests */
7523 1.2 christos if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
7524 1.2 christos &serverssl, &sess, 0,
7525 1.2 christos SHA384_DIGEST_LENGTH)))
7526 1.2 christos goto end;
7527 1.2 christos
7528 1.2 christos /* We don't actually need this reference */
7529 1.2 christos SSL_SESSION_free(sess);
7530 1.2 christos
7531 1.2 christos SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
7532 1.2 christos sslapi_info_callback);
7533 1.2 christos
7534 1.2 christos /* Write and read some early data and then complete the connection */
7535 1.2 christos timer = time(NULL);
7536 1.2 christos if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
7537 1.2 christos &written))
7538 1.2 christos || !TEST_size_t_eq(written, strlen(MSG1)))
7539 1.2 christos goto end;
7540 1.2 christos
7541 1.2 christos if (!TEST_int_eq(SSL_read_early_data(serverssl, buf,
7542 1.2 christos sizeof(buf), &readbytes),
7543 1.2 christos SSL_READ_EARLY_DATA_SUCCESS)) {
7544 1.2 christos testresult = check_early_data_timeout(timer);
7545 1.2 christos goto end;
7546 1.2 christos }
7547 1.2 christos
7548 1.2 christos if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
7549 1.2 christos || !TEST_int_eq(SSL_get_early_data_status(serverssl),
7550 1.2 christos SSL_EARLY_DATA_ACCEPTED)
7551 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
7552 1.2 christos SSL_ERROR_NONE))
7553 1.2 christos || !TEST_false(info_cb_failed))
7554 1.2 christos goto end;
7555 1.2 christos
7556 1.2 christos testresult = 1;
7557 1.2 christos goto end;
7558 1.2 christos }
7559 1.2 christos #endif
7560 1.2 christos
7561 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7562 1.2 christos TLS_client_method(),
7563 1.2 christos tlsvers, tlsvers, &sctx, &cctx, cert,
7564 1.2 christos privkey)))
7565 1.2 christos goto end;
7566 1.2 christos
7567 1.2 christos if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
7568 1.2 christos goto end;
7569 1.2 christos
7570 1.2 christos /*
7571 1.2 christos * For even numbered tests we check the server callbacks. For odd numbers we
7572 1.2 christos * check the client.
7573 1.2 christos */
7574 1.2 christos SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
7575 1.2 christos sslapi_info_callback);
7576 1.2 christos
7577 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
7578 1.2 christos &clientssl, NULL, NULL))
7579 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
7580 1.2 christos SSL_ERROR_NONE))
7581 1.2 christos || !TEST_false(info_cb_failed))
7582 1.2 christos goto end;
7583 1.2 christos
7584 1.2 christos
7585 1.2 christos
7586 1.2 christos clntsess = SSL_get1_session(clientssl);
7587 1.2 christos SSL_shutdown(clientssl);
7588 1.2 christos SSL_shutdown(serverssl);
7589 1.2 christos SSL_free(serverssl);
7590 1.2 christos SSL_free(clientssl);
7591 1.2 christos serverssl = clientssl = NULL;
7592 1.2 christos
7593 1.2 christos /* Now do a resumption */
7594 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7595 1.2 christos NULL))
7596 1.2 christos || !TEST_true(SSL_set_session(clientssl, clntsess))
7597 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
7598 1.2 christos SSL_ERROR_NONE))
7599 1.2 christos || !TEST_true(SSL_session_reused(clientssl))
7600 1.2 christos || !TEST_false(info_cb_failed))
7601 1.2 christos goto end;
7602 1.2 christos
7603 1.2 christos testresult = 1;
7604 1.2 christos
7605 1.2 christos end:
7606 1.2 christos SSL_free(serverssl);
7607 1.2 christos SSL_free(clientssl);
7608 1.2 christos SSL_SESSION_free(clntsess);
7609 1.2 christos SSL_CTX_free(sctx);
7610 1.2 christos SSL_CTX_free(cctx);
7611 1.2 christos return testresult;
7612 1.2 christos }
7613 1.2 christos
7614 1.2 christos static int test_ssl_pending(int tst)
7615 1.2 christos {
7616 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
7617 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
7618 1.2 christos int testresult = 0;
7619 1.2 christos char msg[] = "A test message";
7620 1.2 christos char buf[5];
7621 1.2 christos size_t written, readbytes;
7622 1.2 christos
7623 1.2 christos if (tst == 0) {
7624 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7625 1.2 christos TLS_client_method(),
7626 1.2 christos TLS1_VERSION, 0,
7627 1.2 christos &sctx, &cctx, cert, privkey)))
7628 1.2 christos goto end;
7629 1.2 christos } else {
7630 1.2 christos #ifndef OPENSSL_NO_DTLS
7631 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
7632 1.2 christos DTLS_client_method(),
7633 1.2 christos DTLS1_VERSION, 0,
7634 1.2 christos &sctx, &cctx, cert, privkey)))
7635 1.2 christos goto end;
7636 1.2 christos
7637 1.2 christos # ifdef OPENSSL_NO_DTLS1_2
7638 1.2 christos /* Not supported in the FIPS provider */
7639 1.2 christos if (is_fips) {
7640 1.2 christos testresult = 1;
7641 1.2 christos goto end;
7642 1.2 christos };
7643 1.2 christos /*
7644 1.2 christos * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
7645 1.2 christos * level 0
7646 1.2 christos */
7647 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
7648 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(cctx,
7649 1.2 christos "DEFAULT:@SECLEVEL=0")))
7650 1.2 christos goto end;
7651 1.2 christos # endif
7652 1.2 christos #else
7653 1.2 christos return 1;
7654 1.2 christos #endif
7655 1.2 christos }
7656 1.2 christos
7657 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7658 1.2 christos NULL, NULL))
7659 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
7660 1.2 christos SSL_ERROR_NONE)))
7661 1.2 christos goto end;
7662 1.2 christos
7663 1.2 christos if (!TEST_int_eq(SSL_pending(clientssl), 0)
7664 1.2 christos || !TEST_false(SSL_has_pending(clientssl))
7665 1.2 christos || !TEST_int_eq(SSL_pending(serverssl), 0)
7666 1.2 christos || !TEST_false(SSL_has_pending(serverssl))
7667 1.2 christos || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
7668 1.2 christos || !TEST_size_t_eq(written, sizeof(msg))
7669 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
7670 1.2 christos || !TEST_size_t_eq(readbytes, sizeof(buf))
7671 1.2 christos || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
7672 1.2 christos || !TEST_true(SSL_has_pending(clientssl)))
7673 1.2 christos goto end;
7674 1.2 christos
7675 1.2 christos testresult = 1;
7676 1.2 christos
7677 1.2 christos end:
7678 1.2 christos SSL_free(serverssl);
7679 1.2 christos SSL_free(clientssl);
7680 1.2 christos SSL_CTX_free(sctx);
7681 1.2 christos SSL_CTX_free(cctx);
7682 1.2 christos
7683 1.2 christos return testresult;
7684 1.2 christos }
7685 1.2 christos
7686 1.2 christos static struct {
7687 1.2 christos unsigned int maxprot;
7688 1.2 christos const char *clntciphers;
7689 1.2 christos const char *clnttls13ciphers;
7690 1.2 christos const char *srvrciphers;
7691 1.2 christos const char *srvrtls13ciphers;
7692 1.2 christos const char *shared;
7693 1.2 christos const char *fipsshared;
7694 1.2 christos } shared_ciphers_data[] = {
7695 1.2 christos /*
7696 1.2 christos * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
7697 1.2 christos * TLSv1.3 is enabled but TLSv1.2 is disabled.
7698 1.2 christos */
7699 1.2 christos #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
7700 1.2 christos {
7701 1.2 christos TLS1_2_VERSION,
7702 1.2 christos "AES128-SHA:AES256-SHA",
7703 1.2 christos NULL,
7704 1.2 christos "AES256-SHA:DHE-RSA-AES128-SHA",
7705 1.2 christos NULL,
7706 1.2 christos "AES256-SHA",
7707 1.2 christos "AES256-SHA"
7708 1.2 christos },
7709 1.2 christos # if !defined(OPENSSL_NO_CHACHA) \
7710 1.2 christos && !defined(OPENSSL_NO_POLY1305) \
7711 1.2 christos && !defined(OPENSSL_NO_EC)
7712 1.2 christos {
7713 1.2 christos TLS1_2_VERSION,
7714 1.2 christos "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7715 1.2 christos NULL,
7716 1.2 christos "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7717 1.2 christos NULL,
7718 1.2 christos "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7719 1.2 christos "AES128-SHA"
7720 1.2 christos },
7721 1.2 christos # endif
7722 1.2 christos {
7723 1.2 christos TLS1_2_VERSION,
7724 1.2 christos "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
7725 1.2 christos NULL,
7726 1.2 christos "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
7727 1.2 christos NULL,
7728 1.2 christos "AES128-SHA:AES256-SHA",
7729 1.2 christos "AES128-SHA:AES256-SHA"
7730 1.2 christos },
7731 1.2 christos {
7732 1.2 christos TLS1_2_VERSION,
7733 1.2 christos "AES128-SHA:AES256-SHA",
7734 1.2 christos NULL,
7735 1.2 christos "AES128-SHA:DHE-RSA-AES128-SHA",
7736 1.2 christos NULL,
7737 1.2 christos "AES128-SHA",
7738 1.2 christos "AES128-SHA"
7739 1.2 christos },
7740 1.2 christos #endif
7741 1.2 christos /*
7742 1.2 christos * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
7743 1.2 christos * enabled.
7744 1.2 christos */
7745 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
7746 1.2 christos && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
7747 1.2 christos {
7748 1.2 christos TLS1_3_VERSION,
7749 1.2 christos "AES128-SHA:AES256-SHA",
7750 1.2 christos NULL,
7751 1.2 christos "AES256-SHA:AES128-SHA256",
7752 1.2 christos NULL,
7753 1.2 christos "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
7754 1.2 christos "TLS_AES_128_GCM_SHA256:AES256-SHA",
7755 1.2 christos "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
7756 1.2 christos },
7757 1.2 christos #endif
7758 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
7759 1.2 christos {
7760 1.2 christos TLS1_3_VERSION,
7761 1.2 christos "AES128-SHA",
7762 1.2 christos "TLS_AES_256_GCM_SHA384",
7763 1.2 christos "AES256-SHA",
7764 1.2 christos "TLS_AES_256_GCM_SHA384",
7765 1.2 christos "TLS_AES_256_GCM_SHA384",
7766 1.2 christos "TLS_AES_256_GCM_SHA384"
7767 1.2 christos },
7768 1.2 christos #endif
7769 1.2 christos };
7770 1.2 christos
7771 1.2 christos static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
7772 1.2 christos {
7773 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
7774 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
7775 1.2 christos int testresult = 0;
7776 1.2 christos char buf[1024];
7777 1.2 christos OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
7778 1.2 christos
7779 1.2 christos if (!TEST_ptr(tmplibctx))
7780 1.2 christos goto end;
7781 1.2 christos
7782 1.2 christos /*
7783 1.2 christos * Regardless of whether we're testing with the FIPS provider loaded into
7784 1.2 christos * libctx, we want one peer to always use the full set of ciphersuites
7785 1.2 christos * available. Therefore we use a separate libctx with the default provider
7786 1.2 christos * loaded into it. We run the same tests twice - once with the client side
7787 1.2 christos * having the full set of ciphersuites and once with the server side.
7788 1.2 christos */
7789 1.2 christos if (clnt) {
7790 1.2 christos cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
7791 1.2 christos if (!TEST_ptr(cctx))
7792 1.2 christos goto end;
7793 1.2 christos } else {
7794 1.2 christos sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
7795 1.2 christos if (!TEST_ptr(sctx))
7796 1.2 christos goto end;
7797 1.2 christos }
7798 1.2 christos
7799 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7800 1.2 christos TLS_client_method(),
7801 1.2 christos TLS1_VERSION,
7802 1.2 christos shared_ciphers_data[tst].maxprot,
7803 1.2 christos &sctx, &cctx, cert, privkey)))
7804 1.2 christos goto end;
7805 1.2 christos
7806 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
7807 1.2 christos shared_ciphers_data[tst].clntciphers))
7808 1.2 christos || (shared_ciphers_data[tst].clnttls13ciphers != NULL
7809 1.2 christos && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
7810 1.2 christos shared_ciphers_data[tst].clnttls13ciphers)))
7811 1.2 christos || !TEST_true(SSL_CTX_set_cipher_list(sctx,
7812 1.2 christos shared_ciphers_data[tst].srvrciphers))
7813 1.2 christos || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
7814 1.2 christos && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
7815 1.2 christos shared_ciphers_data[tst].srvrtls13ciphers))))
7816 1.2 christos goto end;
7817 1.2 christos
7818 1.2 christos
7819 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7820 1.2 christos NULL, NULL))
7821 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
7822 1.2 christos SSL_ERROR_NONE)))
7823 1.2 christos goto end;
7824 1.2 christos
7825 1.2 christos if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
7826 1.2 christos || !TEST_int_eq(strcmp(buf,
7827 1.2 christos is_fips
7828 1.2 christos ? shared_ciphers_data[tst].fipsshared
7829 1.2 christos : shared_ciphers_data[tst].shared),
7830 1.2 christos 0)) {
7831 1.2 christos TEST_info("Shared ciphers are: %s\n", buf);
7832 1.2 christos goto end;
7833 1.2 christos }
7834 1.2 christos
7835 1.2 christos testresult = 1;
7836 1.2 christos
7837 1.2 christos end:
7838 1.2 christos SSL_free(serverssl);
7839 1.2 christos SSL_free(clientssl);
7840 1.2 christos SSL_CTX_free(sctx);
7841 1.2 christos SSL_CTX_free(cctx);
7842 1.2 christos OSSL_LIB_CTX_free(tmplibctx);
7843 1.2 christos
7844 1.2 christos return testresult;
7845 1.2 christos }
7846 1.2 christos
7847 1.2 christos static int test_ssl_get_shared_ciphers(int tst)
7848 1.2 christos {
7849 1.2 christos return int_test_ssl_get_shared_ciphers(tst, 0)
7850 1.2 christos && int_test_ssl_get_shared_ciphers(tst, 1);
7851 1.2 christos }
7852 1.2 christos
7853 1.2 christos
7854 1.2 christos static const char *appdata = "Hello World";
7855 1.2 christos static int gen_tick_called, dec_tick_called, tick_key_cb_called;
7856 1.2 christos static int tick_key_renew = 0;
7857 1.2 christos static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7858 1.2 christos
7859 1.2 christos static int gen_tick_cb(SSL *s, void *arg)
7860 1.2 christos {
7861 1.2 christos gen_tick_called = 1;
7862 1.2 christos
7863 1.2 christos return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
7864 1.2 christos strlen(appdata));
7865 1.2 christos }
7866 1.2 christos
7867 1.2 christos static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
7868 1.2 christos const unsigned char *keyname,
7869 1.2 christos size_t keyname_length,
7870 1.2 christos SSL_TICKET_STATUS status,
7871 1.2 christos void *arg)
7872 1.2 christos {
7873 1.2 christos void *tickdata;
7874 1.2 christos size_t tickdlen;
7875 1.2 christos
7876 1.2 christos dec_tick_called = 1;
7877 1.2 christos
7878 1.2 christos if (status == SSL_TICKET_EMPTY)
7879 1.2 christos return SSL_TICKET_RETURN_IGNORE_RENEW;
7880 1.2 christos
7881 1.2 christos if (!TEST_true(status == SSL_TICKET_SUCCESS
7882 1.2 christos || status == SSL_TICKET_SUCCESS_RENEW))
7883 1.2 christos return SSL_TICKET_RETURN_ABORT;
7884 1.2 christos
7885 1.2 christos if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
7886 1.2 christos &tickdlen))
7887 1.2 christos || !TEST_size_t_eq(tickdlen, strlen(appdata))
7888 1.2 christos || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
7889 1.2 christos return SSL_TICKET_RETURN_ABORT;
7890 1.2 christos
7891 1.2 christos if (tick_key_cb_called) {
7892 1.2 christos /* Don't change what the ticket key callback wanted to do */
7893 1.2 christos switch (status) {
7894 1.2 christos case SSL_TICKET_NO_DECRYPT:
7895 1.2 christos return SSL_TICKET_RETURN_IGNORE_RENEW;
7896 1.2 christos
7897 1.2 christos case SSL_TICKET_SUCCESS:
7898 1.2 christos return SSL_TICKET_RETURN_USE;
7899 1.2 christos
7900 1.2 christos case SSL_TICKET_SUCCESS_RENEW:
7901 1.2 christos return SSL_TICKET_RETURN_USE_RENEW;
7902 1.2 christos
7903 1.2 christos default:
7904 1.2 christos return SSL_TICKET_RETURN_ABORT;
7905 1.2 christos }
7906 1.2 christos }
7907 1.2 christos return tick_dec_ret;
7908 1.2 christos
7909 1.2 christos }
7910 1.2 christos
7911 1.2 christos #ifndef OPENSSL_NO_DEPRECATED_3_0
7912 1.2 christos static int tick_key_cb(SSL *s, unsigned char key_name[16],
7913 1.2 christos unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
7914 1.2 christos HMAC_CTX *hctx, int enc)
7915 1.2 christos {
7916 1.2 christos const unsigned char tick_aes_key[16] = "0123456789abcdef";
7917 1.2 christos const unsigned char tick_hmac_key[16] = "0123456789abcdef";
7918 1.2 christos EVP_CIPHER *aes128cbc;
7919 1.2 christos EVP_MD *sha256;
7920 1.2 christos int ret;
7921 1.2 christos
7922 1.2 christos tick_key_cb_called = 1;
7923 1.2 christos
7924 1.2 christos if (tick_key_renew == -1)
7925 1.2 christos return 0;
7926 1.2 christos
7927 1.2 christos aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7928 1.2 christos if (!TEST_ptr(aes128cbc))
7929 1.2 christos return 0;
7930 1.2 christos sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
7931 1.2 christos if (!TEST_ptr(sha256)) {
7932 1.2 christos EVP_CIPHER_free(aes128cbc);
7933 1.2 christos return 0;
7934 1.2 christos }
7935 1.2 christos
7936 1.2 christos memset(iv, 0, AES_BLOCK_SIZE);
7937 1.2 christos memset(key_name, 0, 16);
7938 1.2 christos if (aes128cbc == NULL
7939 1.2 christos || sha256 == NULL
7940 1.2 christos || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7941 1.2 christos || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
7942 1.2 christos NULL))
7943 1.2 christos ret = -1;
7944 1.2 christos else
7945 1.2 christos ret = tick_key_renew ? 2 : 1;
7946 1.2 christos
7947 1.2 christos EVP_CIPHER_free(aes128cbc);
7948 1.2 christos EVP_MD_free(sha256);
7949 1.2 christos
7950 1.2 christos return ret;
7951 1.2 christos }
7952 1.2 christos #endif
7953 1.2 christos
7954 1.2 christos static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
7955 1.2 christos unsigned char iv[EVP_MAX_IV_LENGTH],
7956 1.2 christos EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
7957 1.2 christos {
7958 1.2 christos const unsigned char tick_aes_key[16] = "0123456789abcdef";
7959 1.2 christos unsigned char tick_hmac_key[16] = "0123456789abcdef";
7960 1.2 christos OSSL_PARAM params[2];
7961 1.2 christos EVP_CIPHER *aes128cbc;
7962 1.2 christos int ret;
7963 1.2 christos
7964 1.2 christos tick_key_cb_called = 1;
7965 1.2 christos
7966 1.2 christos if (tick_key_renew == -1)
7967 1.2 christos return 0;
7968 1.2 christos
7969 1.2 christos aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7970 1.2 christos if (!TEST_ptr(aes128cbc))
7971 1.2 christos return 0;
7972 1.2 christos
7973 1.2 christos memset(iv, 0, AES_BLOCK_SIZE);
7974 1.2 christos memset(key_name, 0, 16);
7975 1.2 christos params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
7976 1.2 christos "SHA256", 0);
7977 1.2 christos params[1] = OSSL_PARAM_construct_end();
7978 1.2 christos if (aes128cbc == NULL
7979 1.2 christos || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7980 1.2 christos || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
7981 1.2 christos params))
7982 1.2 christos ret = -1;
7983 1.2 christos else
7984 1.2 christos ret = tick_key_renew ? 2 : 1;
7985 1.2 christos
7986 1.2 christos EVP_CIPHER_free(aes128cbc);
7987 1.2 christos
7988 1.2 christos return ret;
7989 1.2 christos }
7990 1.2 christos
7991 1.2 christos /*
7992 1.2 christos * Test the various ticket callbacks
7993 1.2 christos * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
7994 1.2 christos * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
7995 1.2 christos * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
7996 1.2 christos * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
7997 1.2 christos * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
7998 1.2 christos * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
7999 1.2 christos * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
8000 1.2 christos * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
8001 1.2 christos * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
8002 1.2 christos * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
8003 1.2 christos * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
8004 1.2 christos * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
8005 1.2 christos * Test 12: TLSv1.2, old ticket key callback, no ticket
8006 1.2 christos * Test 13: TLSv1.3, old ticket key callback, no ticket
8007 1.2 christos * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
8008 1.2 christos * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
8009 1.2 christos * Test 16: TLSv1.2, ticket key callback, ticket, renewal
8010 1.2 christos * Test 17: TLSv1.3, ticket key callback, ticket, renewal
8011 1.2 christos * Test 18: TLSv1.2, ticket key callback, no ticket
8012 1.2 christos * Test 19: TLSv1.3, ticket key callback, no ticket
8013 1.2 christos */
8014 1.2 christos static int test_ticket_callbacks(int tst)
8015 1.2 christos {
8016 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8017 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8018 1.2 christos SSL_SESSION *clntsess = NULL;
8019 1.2 christos int testresult = 0;
8020 1.2 christos
8021 1.2 christos #ifdef OPENSSL_NO_TLS1_2
8022 1.2 christos if (tst % 2 == 0)
8023 1.2 christos return 1;
8024 1.2 christos #endif
8025 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
8026 1.2 christos if (tst % 2 == 1)
8027 1.2 christos return 1;
8028 1.2 christos #endif
8029 1.2 christos #ifdef OPENSSL_NO_DEPRECATED_3_0
8030 1.2 christos if (tst >= 8 && tst <= 13)
8031 1.2 christos return 1;
8032 1.2 christos #endif
8033 1.2 christos
8034 1.2 christos gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
8035 1.2 christos
8036 1.2 christos /* Which tests the ticket key callback should request renewal for */
8037 1.2 christos
8038 1.2 christos if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
8039 1.2 christos tick_key_renew = 1;
8040 1.2 christos else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
8041 1.2 christos tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
8042 1.2 christos else
8043 1.2 christos tick_key_renew = 0;
8044 1.2 christos
8045 1.2 christos /* Which tests the decrypt ticket callback should request renewal for */
8046 1.2 christos switch (tst) {
8047 1.2 christos case 0:
8048 1.2 christos case 1:
8049 1.2 christos tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
8050 1.2 christos break;
8051 1.2 christos
8052 1.2 christos case 2:
8053 1.2 christos case 3:
8054 1.2 christos tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
8055 1.2 christos break;
8056 1.2 christos
8057 1.2 christos case 4:
8058 1.2 christos case 5:
8059 1.2 christos tick_dec_ret = SSL_TICKET_RETURN_USE;
8060 1.2 christos break;
8061 1.2 christos
8062 1.2 christos case 6:
8063 1.2 christos case 7:
8064 1.2 christos tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
8065 1.2 christos break;
8066 1.2 christos
8067 1.2 christos default:
8068 1.2 christos tick_dec_ret = SSL_TICKET_RETURN_ABORT;
8069 1.2 christos }
8070 1.2 christos
8071 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8072 1.2 christos TLS_client_method(),
8073 1.2 christos TLS1_VERSION,
8074 1.2 christos ((tst % 2) == 0) ? TLS1_2_VERSION
8075 1.2 christos : TLS1_3_VERSION,
8076 1.2 christos &sctx, &cctx, cert, privkey)))
8077 1.2 christos goto end;
8078 1.2 christos
8079 1.2 christos /*
8080 1.2 christos * We only want sessions to resume from tickets - not the session cache. So
8081 1.2 christos * switch the cache off.
8082 1.2 christos */
8083 1.2 christos if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
8084 1.2 christos goto end;
8085 1.2 christos
8086 1.2 christos if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
8087 1.2 christos NULL)))
8088 1.2 christos goto end;
8089 1.2 christos
8090 1.2 christos if (tst >= 14) {
8091 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
8092 1.2 christos goto end;
8093 1.2 christos #ifndef OPENSSL_NO_DEPRECATED_3_0
8094 1.2 christos } else if (tst >= 8) {
8095 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
8096 1.2 christos goto end;
8097 1.2 christos #endif
8098 1.2 christos }
8099 1.2 christos
8100 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8101 1.2 christos NULL, NULL))
8102 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
8103 1.2 christos SSL_ERROR_NONE)))
8104 1.2 christos goto end;
8105 1.2 christos
8106 1.2 christos /*
8107 1.2 christos * The decrypt ticket key callback in TLSv1.2 should be called even though
8108 1.2 christos * we have no ticket yet, because it gets called with a status of
8109 1.2 christos * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
8110 1.2 christos * actually send any ticket data). This does not happen in TLSv1.3 because
8111 1.2 christos * it is not valid to send empty ticket data in TLSv1.3.
8112 1.2 christos */
8113 1.2 christos if (!TEST_int_eq(gen_tick_called, 1)
8114 1.2 christos || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
8115 1.2 christos goto end;
8116 1.2 christos
8117 1.2 christos gen_tick_called = dec_tick_called = 0;
8118 1.2 christos
8119 1.2 christos clntsess = SSL_get1_session(clientssl);
8120 1.2 christos SSL_shutdown(clientssl);
8121 1.2 christos SSL_shutdown(serverssl);
8122 1.2 christos SSL_free(serverssl);
8123 1.2 christos SSL_free(clientssl);
8124 1.2 christos serverssl = clientssl = NULL;
8125 1.2 christos
8126 1.2 christos /* Now do a resumption */
8127 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
8128 1.2 christos NULL))
8129 1.2 christos || !TEST_true(SSL_set_session(clientssl, clntsess))
8130 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
8131 1.2 christos SSL_ERROR_NONE)))
8132 1.2 christos goto end;
8133 1.2 christos
8134 1.2 christos if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
8135 1.2 christos || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8136 1.2 christos || tick_key_renew == -1) {
8137 1.2 christos if (!TEST_false(SSL_session_reused(clientssl)))
8138 1.2 christos goto end;
8139 1.2 christos } else {
8140 1.2 christos if (!TEST_true(SSL_session_reused(clientssl)))
8141 1.2 christos goto end;
8142 1.2 christos }
8143 1.2 christos
8144 1.2 christos if (!TEST_int_eq(gen_tick_called,
8145 1.2 christos (tick_key_renew
8146 1.2 christos || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8147 1.2 christos || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
8148 1.2 christos ? 1 : 0)
8149 1.2 christos /* There is no ticket to decrypt in tests 13 and 19 */
8150 1.2 christos || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
8151 1.2 christos goto end;
8152 1.2 christos
8153 1.2 christos testresult = 1;
8154 1.2 christos
8155 1.2 christos end:
8156 1.2 christos SSL_SESSION_free(clntsess);
8157 1.2 christos SSL_free(serverssl);
8158 1.2 christos SSL_free(clientssl);
8159 1.2 christos SSL_CTX_free(sctx);
8160 1.2 christos SSL_CTX_free(cctx);
8161 1.2 christos
8162 1.2 christos return testresult;
8163 1.2 christos }
8164 1.2 christos
8165 1.2 christos /*
8166 1.2 christos * Test incorrect shutdown.
8167 1.2 christos * Test 0: client does not shutdown properly,
8168 1.2 christos * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
8169 1.2 christos * server should get SSL_ERROR_SSL
8170 1.2 christos * Test 1: client does not shutdown properly,
8171 1.2 christos * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
8172 1.2 christos * server should get SSL_ERROR_ZERO_RETURN
8173 1.2 christos */
8174 1.2 christos static int test_incorrect_shutdown(int tst)
8175 1.2 christos {
8176 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8177 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8178 1.2 christos int testresult = 0;
8179 1.2 christos char buf[80];
8180 1.2 christos BIO *c2s;
8181 1.2 christos
8182 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8183 1.2 christos TLS_client_method(), 0, 0,
8184 1.2 christos &sctx, &cctx, cert, privkey)))
8185 1.2 christos goto end;
8186 1.2 christos
8187 1.2 christos if (tst == 1)
8188 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
8189 1.2 christos
8190 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8191 1.2 christos NULL, NULL)))
8192 1.2 christos goto end;
8193 1.2 christos
8194 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8195 1.2 christos SSL_ERROR_NONE)))
8196 1.2 christos goto end;
8197 1.2 christos
8198 1.2 christos c2s = SSL_get_rbio(serverssl);
8199 1.2 christos BIO_set_mem_eof_return(c2s, 0);
8200 1.2 christos
8201 1.2 christos if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
8202 1.2 christos goto end;
8203 1.2 christos
8204 1.2 christos if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
8205 1.2 christos goto end;
8206 1.2 christos if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
8207 1.2 christos goto end;
8208 1.2 christos
8209 1.2 christos testresult = 1;
8210 1.2 christos
8211 1.2 christos end:
8212 1.2 christos SSL_free(serverssl);
8213 1.2 christos SSL_free(clientssl);
8214 1.2 christos SSL_CTX_free(sctx);
8215 1.2 christos SSL_CTX_free(cctx);
8216 1.2 christos
8217 1.2 christos return testresult;
8218 1.2 christos }
8219 1.2 christos
8220 1.2 christos /*
8221 1.2 christos * Test bi-directional shutdown.
8222 1.2 christos * Test 0: TLSv1.2
8223 1.2 christos * Test 1: TLSv1.2, server continues to read/write after client shutdown
8224 1.2 christos * Test 2: TLSv1.3, no pending NewSessionTicket messages
8225 1.2 christos * Test 3: TLSv1.3, pending NewSessionTicket messages
8226 1.2 christos * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
8227 1.2 christos * sends key update, client reads it
8228 1.2 christos * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
8229 1.2 christos * sends CertificateRequest, client reads and ignores it
8230 1.2 christos * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
8231 1.2 christos * doesn't read it
8232 1.2 christos */
8233 1.2 christos static int test_shutdown(int tst)
8234 1.2 christos {
8235 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8236 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8237 1.2 christos int testresult = 0;
8238 1.2 christos char msg[] = "A test message";
8239 1.2 christos char buf[80];
8240 1.2 christos size_t written, readbytes;
8241 1.2 christos SSL_SESSION *sess;
8242 1.2 christos
8243 1.2 christos #ifdef OPENSSL_NO_TLS1_2
8244 1.2 christos if (tst <= 1)
8245 1.2 christos return 1;
8246 1.2 christos #endif
8247 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
8248 1.2 christos if (tst >= 2)
8249 1.2 christos return 1;
8250 1.2 christos #endif
8251 1.2 christos
8252 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8253 1.2 christos TLS_client_method(),
8254 1.2 christos TLS1_VERSION,
8255 1.2 christos (tst <= 1) ? TLS1_2_VERSION
8256 1.2 christos : TLS1_3_VERSION,
8257 1.2 christos &sctx, &cctx, cert, privkey)))
8258 1.2 christos goto end;
8259 1.2 christos
8260 1.2 christos if (tst == 5)
8261 1.2 christos SSL_CTX_set_post_handshake_auth(cctx, 1);
8262 1.2 christos
8263 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8264 1.2 christos NULL, NULL)))
8265 1.2 christos goto end;
8266 1.2 christos
8267 1.2 christos if (tst == 3) {
8268 1.2 christos if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
8269 1.2 christos SSL_ERROR_NONE, 1))
8270 1.2 christos || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8271 1.2 christos || !TEST_false(SSL_SESSION_is_resumable(sess)))
8272 1.2 christos goto end;
8273 1.2 christos } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8274 1.2 christos SSL_ERROR_NONE))
8275 1.2 christos || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8276 1.2 christos || !TEST_true(SSL_SESSION_is_resumable(sess))) {
8277 1.2 christos goto end;
8278 1.2 christos }
8279 1.2 christos
8280 1.2 christos if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
8281 1.2 christos goto end;
8282 1.2 christos
8283 1.2 christos if (tst >= 4) {
8284 1.2 christos /*
8285 1.2 christos * Reading on the server after the client has sent close_notify should
8286 1.2 christos * fail and provide SSL_ERROR_ZERO_RETURN
8287 1.2 christos */
8288 1.2 christos if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
8289 1.2 christos || !TEST_int_eq(SSL_get_error(serverssl, 0),
8290 1.2 christos SSL_ERROR_ZERO_RETURN)
8291 1.2 christos || !TEST_int_eq(SSL_get_shutdown(serverssl),
8292 1.2 christos SSL_RECEIVED_SHUTDOWN)
8293 1.2 christos /*
8294 1.2 christos * Even though we're shutdown on receive we should still be
8295 1.2 christos * able to write.
8296 1.2 christos */
8297 1.2 christos || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8298 1.2 christos goto end;
8299 1.2 christos if (tst == 4
8300 1.2 christos && !TEST_true(SSL_key_update(serverssl,
8301 1.2 christos SSL_KEY_UPDATE_REQUESTED)))
8302 1.2 christos goto end;
8303 1.2 christos if (tst == 5) {
8304 1.2 christos SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
8305 1.2 christos if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
8306 1.2 christos goto end;
8307 1.2 christos }
8308 1.2 christos if ((tst == 4 || tst == 5)
8309 1.2 christos && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8310 1.2 christos goto end;
8311 1.2 christos if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
8312 1.2 christos goto end;
8313 1.2 christos if (tst == 4 || tst == 5) {
8314 1.2 christos /* Should still be able to read data from server */
8315 1.2 christos if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8316 1.2 christos &readbytes))
8317 1.2 christos || !TEST_size_t_eq(readbytes, sizeof(msg))
8318 1.2 christos || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
8319 1.2 christos || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8320 1.2 christos &readbytes))
8321 1.2 christos || !TEST_size_t_eq(readbytes, sizeof(msg))
8322 1.2 christos || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
8323 1.2 christos goto end;
8324 1.2 christos }
8325 1.2 christos }
8326 1.2 christos
8327 1.2 christos /* Writing on the client after sending close_notify shouldn't be possible */
8328 1.2 christos if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
8329 1.2 christos goto end;
8330 1.2 christos
8331 1.2 christos if (tst < 4) {
8332 1.2 christos /*
8333 1.2 christos * For these tests the client has sent close_notify but it has not yet
8334 1.2 christos * been received by the server. The server has not sent close_notify
8335 1.2 christos * yet.
8336 1.2 christos */
8337 1.2 christos if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
8338 1.2 christos /*
8339 1.2 christos * Writing on the server after sending close_notify shouldn't
8340 1.2 christos * be possible.
8341 1.2 christos */
8342 1.2 christos || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8343 1.2 christos || !TEST_int_eq(SSL_shutdown(clientssl), 1)
8344 1.2 christos || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8345 1.2 christos || !TEST_true(SSL_SESSION_is_resumable(sess))
8346 1.2 christos || !TEST_int_eq(SSL_shutdown(serverssl), 1))
8347 1.2 christos goto end;
8348 1.2 christos } else if (tst == 4 || tst == 5) {
8349 1.2 christos /*
8350 1.2 christos * In this test the client has sent close_notify and it has been
8351 1.2 christos * received by the server which has responded with a close_notify. The
8352 1.2 christos * client needs to read the close_notify sent by the server.
8353 1.2 christos */
8354 1.2 christos if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
8355 1.2 christos || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8356 1.2 christos || !TEST_true(SSL_SESSION_is_resumable(sess)))
8357 1.2 christos goto end;
8358 1.2 christos } else {
8359 1.2 christos /*
8360 1.2 christos * tst == 6
8361 1.2 christos *
8362 1.2 christos * The client has sent close_notify and is expecting a close_notify
8363 1.2 christos * back, but instead there is application data first. The shutdown
8364 1.2 christos * should fail with a fatal error.
8365 1.2 christos */
8366 1.2 christos if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
8367 1.2 christos || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
8368 1.2 christos goto end;
8369 1.2 christos }
8370 1.2 christos
8371 1.2 christos testresult = 1;
8372 1.2 christos
8373 1.2 christos end:
8374 1.2 christos SSL_free(serverssl);
8375 1.2 christos SSL_free(clientssl);
8376 1.2 christos SSL_CTX_free(sctx);
8377 1.2 christos SSL_CTX_free(cctx);
8378 1.2 christos
8379 1.2 christos return testresult;
8380 1.2 christos }
8381 1.2 christos
8382 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8383 1.2 christos static int cert_cb_cnt;
8384 1.2 christos
8385 1.2 christos static int cert_cb(SSL *s, void *arg)
8386 1.2 christos {
8387 1.2 christos SSL_CTX *ctx = (SSL_CTX *)arg;
8388 1.2 christos BIO *in = NULL;
8389 1.2 christos EVP_PKEY *pkey = NULL;
8390 1.2 christos X509 *x509 = NULL, *rootx = NULL;
8391 1.2 christos STACK_OF(X509) *chain = NULL;
8392 1.2 christos char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
8393 1.2 christos int ret = 0;
8394 1.2 christos
8395 1.2 christos if (cert_cb_cnt == 0) {
8396 1.2 christos /* Suspend the handshake */
8397 1.2 christos cert_cb_cnt++;
8398 1.2 christos return -1;
8399 1.2 christos } else if (cert_cb_cnt == 1) {
8400 1.2 christos /*
8401 1.2 christos * Update the SSL_CTX, set the certificate and private key and then
8402 1.2 christos * continue the handshake normally.
8403 1.2 christos */
8404 1.2 christos if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
8405 1.2 christos return 0;
8406 1.2 christos
8407 1.2 christos if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
8408 1.2 christos || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
8409 1.2 christos SSL_FILETYPE_PEM))
8410 1.2 christos || !TEST_true(SSL_check_private_key(s)))
8411 1.2 christos return 0;
8412 1.2 christos cert_cb_cnt++;
8413 1.2 christos return 1;
8414 1.2 christos } else if (cert_cb_cnt == 3) {
8415 1.2 christos int rv;
8416 1.2 christos
8417 1.2 christos rootfile = test_mk_file_path(certsdir, "rootcert.pem");
8418 1.2 christos ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
8419 1.2 christos ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
8420 1.2 christos if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
8421 1.2 christos goto out;
8422 1.2 christos chain = sk_X509_new_null();
8423 1.2 christos if (!TEST_ptr(chain))
8424 1.2 christos goto out;
8425 1.2 christos if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8426 1.2 christos || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
8427 1.2 christos || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
8428 1.2 christos || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
8429 1.2 christos || !TEST_true(sk_X509_push(chain, rootx)))
8430 1.2 christos goto out;
8431 1.2 christos rootx = NULL;
8432 1.2 christos BIO_free(in);
8433 1.2 christos if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8434 1.2 christos || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
8435 1.2 christos || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
8436 1.2 christos || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
8437 1.2 christos goto out;
8438 1.2 christos BIO_free(in);
8439 1.2 christos if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8440 1.2 christos || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
8441 1.2 christos || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
8442 1.2 christos NULL, NULL,
8443 1.2 christos libctx, NULL)))
8444 1.2 christos goto out;
8445 1.2 christos rv = SSL_check_chain(s, x509, pkey, chain);
8446 1.2 christos /*
8447 1.2 christos * If the cert doesn't show as valid here (e.g., because we don't
8448 1.2 christos * have any shared sigalgs), then we will not set it, and there will
8449 1.2 christos * be no certificate at all on the SSL or SSL_CTX. This, in turn,
8450 1.2 christos * will cause tls_choose_sigalgs() to fail the connection.
8451 1.2 christos */
8452 1.2 christos if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
8453 1.2 christos == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
8454 1.2 christos if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
8455 1.2 christos goto out;
8456 1.2 christos }
8457 1.2 christos
8458 1.2 christos ret = 1;
8459 1.2 christos }
8460 1.2 christos
8461 1.2 christos /* Abort the handshake */
8462 1.2 christos out:
8463 1.2 christos OPENSSL_free(ecdsacert);
8464 1.2 christos OPENSSL_free(ecdsakey);
8465 1.2 christos OPENSSL_free(rootfile);
8466 1.2 christos BIO_free(in);
8467 1.2 christos EVP_PKEY_free(pkey);
8468 1.2 christos X509_free(x509);
8469 1.2 christos X509_free(rootx);
8470 1.2 christos sk_X509_pop_free(chain, X509_free);
8471 1.2 christos return ret;
8472 1.2 christos }
8473 1.2 christos
8474 1.2 christos /*
8475 1.2 christos * Test the certificate callback.
8476 1.2 christos * Test 0: Callback fails
8477 1.2 christos * Test 1: Success - no SSL_set_SSL_CTX() in the callback
8478 1.2 christos * Test 2: Success - SSL_set_SSL_CTX() in the callback
8479 1.2 christos * Test 3: Success - Call SSL_check_chain from the callback
8480 1.2 christos * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
8481 1.2 christos * chain
8482 1.2 christos * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
8483 1.2 christos */
8484 1.2 christos static int test_cert_cb_int(int prot, int tst)
8485 1.2 christos {
8486 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
8487 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8488 1.2 christos int testresult = 0, ret;
8489 1.2 christos
8490 1.2 christos #ifdef OPENSSL_NO_EC
8491 1.2 christos /* We use an EC cert in these tests, so we skip in a no-ec build */
8492 1.2 christos if (tst >= 3)
8493 1.2 christos return 1;
8494 1.2 christos #endif
8495 1.2 christos
8496 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8497 1.2 christos TLS_client_method(),
8498 1.2 christos TLS1_VERSION,
8499 1.2 christos prot,
8500 1.2 christos &sctx, &cctx, NULL, NULL)))
8501 1.2 christos goto end;
8502 1.2 christos
8503 1.2 christos if (tst == 0)
8504 1.2 christos cert_cb_cnt = -1;
8505 1.2 christos else if (tst >= 3)
8506 1.2 christos cert_cb_cnt = 3;
8507 1.2 christos else
8508 1.2 christos cert_cb_cnt = 0;
8509 1.2 christos
8510 1.2 christos if (tst == 2) {
8511 1.2 christos snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
8512 1.2 christos if (!TEST_ptr(snictx))
8513 1.2 christos goto end;
8514 1.2 christos }
8515 1.2 christos
8516 1.2 christos SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
8517 1.2 christos
8518 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8519 1.2 christos NULL, NULL)))
8520 1.2 christos goto end;
8521 1.2 christos
8522 1.2 christos if (tst == 4) {
8523 1.2 christos /*
8524 1.2 christos * We cause SSL_check_chain() to fail by specifying sig_algs that
8525 1.2 christos * the chain doesn't meet (the root uses an RSA cert)
8526 1.2 christos */
8527 1.2 christos if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8528 1.2 christos "ecdsa_secp256r1_sha256")))
8529 1.2 christos goto end;
8530 1.2 christos } else if (tst == 5) {
8531 1.2 christos /*
8532 1.2 christos * We cause SSL_check_chain() to fail by specifying sig_algs that
8533 1.2 christos * the ee cert doesn't meet (the ee uses an ECDSA cert)
8534 1.2 christos */
8535 1.2 christos if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8536 1.2 christos "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
8537 1.2 christos goto end;
8538 1.2 christos }
8539 1.2 christos
8540 1.2 christos ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
8541 1.2 christos if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
8542 1.2 christos || (tst > 0
8543 1.2 christos && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
8544 1.2 christos goto end;
8545 1.2 christos }
8546 1.2 christos
8547 1.2 christos testresult = 1;
8548 1.2 christos
8549 1.2 christos end:
8550 1.2 christos SSL_free(serverssl);
8551 1.2 christos SSL_free(clientssl);
8552 1.2 christos SSL_CTX_free(sctx);
8553 1.2 christos SSL_CTX_free(cctx);
8554 1.2 christos SSL_CTX_free(snictx);
8555 1.2 christos
8556 1.2 christos return testresult;
8557 1.2 christos }
8558 1.2 christos #endif
8559 1.2 christos
8560 1.2 christos static int test_cert_cb(int tst)
8561 1.2 christos {
8562 1.2 christos int testresult = 1;
8563 1.2 christos
8564 1.2 christos #ifndef OPENSSL_NO_TLS1_2
8565 1.2 christos testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
8566 1.2 christos #endif
8567 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
8568 1.2 christos testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
8569 1.2 christos #endif
8570 1.2 christos
8571 1.2 christos return testresult;
8572 1.2 christos }
8573 1.2 christos
8574 1.2 christos static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
8575 1.2 christos {
8576 1.2 christos X509 *xcert;
8577 1.2 christos EVP_PKEY *privpkey;
8578 1.2 christos BIO *in = NULL;
8579 1.2 christos BIO *priv_in = NULL;
8580 1.2 christos
8581 1.2 christos /* Check that SSL_get0_peer_certificate() returns something sensible */
8582 1.2 christos if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
8583 1.2 christos return 0;
8584 1.2 christos
8585 1.2 christos in = BIO_new_file(cert, "r");
8586 1.2 christos if (!TEST_ptr(in))
8587 1.2 christos return 0;
8588 1.2 christos
8589 1.2 christos if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
8590 1.2 christos || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
8591 1.2 christos || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
8592 1.2 christos || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
8593 1.2 christos NULL, NULL,
8594 1.2 christos libctx, NULL)))
8595 1.2 christos goto err;
8596 1.2 christos
8597 1.2 christos *x509 = xcert;
8598 1.2 christos *pkey = privpkey;
8599 1.2 christos
8600 1.2 christos BIO_free(in);
8601 1.2 christos BIO_free(priv_in);
8602 1.2 christos return 1;
8603 1.2 christos err:
8604 1.2 christos X509_free(xcert);
8605 1.2 christos BIO_free(in);
8606 1.2 christos BIO_free(priv_in);
8607 1.2 christos return 0;
8608 1.2 christos }
8609 1.2 christos
8610 1.2 christos static int test_client_cert_cb(int tst)
8611 1.2 christos {
8612 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8613 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8614 1.2 christos int testresult = 0;
8615 1.2 christos
8616 1.2 christos #ifdef OPENSSL_NO_TLS1_2
8617 1.2 christos if (tst == 0)
8618 1.2 christos return 1;
8619 1.2 christos #endif
8620 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
8621 1.2 christos if (tst == 1)
8622 1.2 christos return 1;
8623 1.2 christos #endif
8624 1.2 christos
8625 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8626 1.2 christos TLS_client_method(),
8627 1.2 christos TLS1_VERSION,
8628 1.2 christos tst == 0 ? TLS1_2_VERSION
8629 1.2 christos : TLS1_3_VERSION,
8630 1.2 christos &sctx, &cctx, cert, privkey)))
8631 1.2 christos goto end;
8632 1.2 christos
8633 1.2 christos /*
8634 1.2 christos * Test that setting a client_cert_cb results in a client certificate being
8635 1.2 christos * sent.
8636 1.2 christos */
8637 1.2 christos SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
8638 1.2 christos SSL_CTX_set_verify(sctx,
8639 1.2 christos SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
8640 1.2 christos verify_cb);
8641 1.2 christos
8642 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8643 1.2 christos NULL, NULL))
8644 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
8645 1.2 christos SSL_ERROR_NONE)))
8646 1.2 christos goto end;
8647 1.2 christos
8648 1.2 christos testresult = 1;
8649 1.2 christos
8650 1.2 christos end:
8651 1.2 christos SSL_free(serverssl);
8652 1.2 christos SSL_free(clientssl);
8653 1.2 christos SSL_CTX_free(sctx);
8654 1.2 christos SSL_CTX_free(cctx);
8655 1.2 christos
8656 1.2 christos return testresult;
8657 1.2 christos }
8658 1.2 christos
8659 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8660 1.2 christos /*
8661 1.2 christos * Test setting certificate authorities on both client and server.
8662 1.2 christos *
8663 1.2 christos * Test 0: SSL_CTX_set0_CA_list() only
8664 1.2 christos * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
8665 1.2 christos * Test 2: Only SSL_CTX_set_client_CA_list()
8666 1.2 christos */
8667 1.2 christos static int test_ca_names_int(int prot, int tst)
8668 1.2 christos {
8669 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8670 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8671 1.2 christos int testresult = 0;
8672 1.2 christos size_t i;
8673 1.2 christos X509_NAME *name[] = { NULL, NULL, NULL, NULL };
8674 1.2 christos char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
8675 1.2 christos STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
8676 1.2 christos const STACK_OF(X509_NAME) *sktmp = NULL;
8677 1.2 christos
8678 1.2 christos for (i = 0; i < OSSL_NELEM(name); i++) {
8679 1.2 christos name[i] = X509_NAME_new();
8680 1.2 christos if (!TEST_ptr(name[i])
8681 1.2 christos || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
8682 1.2 christos MBSTRING_ASC,
8683 1.2 christos (unsigned char *)
8684 1.2 christos strnames[i],
8685 1.2 christos -1, -1, 0)))
8686 1.2 christos goto end;
8687 1.2 christos }
8688 1.2 christos
8689 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8690 1.2 christos TLS_client_method(),
8691 1.2 christos TLS1_VERSION,
8692 1.2 christos prot,
8693 1.2 christos &sctx, &cctx, cert, privkey)))
8694 1.2 christos goto end;
8695 1.2 christos
8696 1.2 christos SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
8697 1.2 christos
8698 1.2 christos if (tst == 0 || tst == 1) {
8699 1.2 christos if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8700 1.2 christos || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
8701 1.2 christos || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
8702 1.2 christos || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8703 1.2 christos || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
8704 1.2 christos || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
8705 1.2 christos goto end;
8706 1.2 christos
8707 1.2 christos SSL_CTX_set0_CA_list(sctx, sk1);
8708 1.2 christos SSL_CTX_set0_CA_list(cctx, sk2);
8709 1.2 christos sk1 = sk2 = NULL;
8710 1.2 christos }
8711 1.2 christos if (tst == 1 || tst == 2) {
8712 1.2 christos if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8713 1.2 christos || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
8714 1.2 christos || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
8715 1.2 christos || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8716 1.2 christos || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
8717 1.2 christos || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
8718 1.2 christos goto end;
8719 1.2 christos
8720 1.2 christos SSL_CTX_set_client_CA_list(sctx, sk1);
8721 1.2 christos SSL_CTX_set_client_CA_list(cctx, sk2);
8722 1.2 christos sk1 = sk2 = NULL;
8723 1.2 christos }
8724 1.2 christos
8725 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8726 1.2 christos NULL, NULL))
8727 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
8728 1.2 christos SSL_ERROR_NONE)))
8729 1.2 christos goto end;
8730 1.2 christos
8731 1.2 christos /*
8732 1.2 christos * We only expect certificate authorities to have been sent to the server
8733 1.2 christos * if we are using TLSv1.3 and SSL_set0_CA_list() was used
8734 1.2 christos */
8735 1.2 christos sktmp = SSL_get0_peer_CA_list(serverssl);
8736 1.2 christos if (prot == TLS1_3_VERSION
8737 1.2 christos && (tst == 0 || tst == 1)) {
8738 1.2 christos if (!TEST_ptr(sktmp)
8739 1.2 christos || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8740 1.2 christos || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8741 1.2 christos name[0]), 0)
8742 1.2 christos || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8743 1.2 christos name[1]), 0))
8744 1.2 christos goto end;
8745 1.2 christos } else if (!TEST_ptr_null(sktmp)) {
8746 1.2 christos goto end;
8747 1.2 christos }
8748 1.2 christos
8749 1.2 christos /*
8750 1.2 christos * In all tests we expect certificate authorities to have been sent to the
8751 1.2 christos * client. However, SSL_set_client_CA_list() should override
8752 1.2 christos * SSL_set0_CA_list()
8753 1.2 christos */
8754 1.2 christos sktmp = SSL_get0_peer_CA_list(clientssl);
8755 1.2 christos if (!TEST_ptr(sktmp)
8756 1.2 christos || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8757 1.2 christos || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8758 1.2 christos name[tst == 0 ? 0 : 2]), 0)
8759 1.2 christos || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8760 1.2 christos name[tst == 0 ? 1 : 3]), 0))
8761 1.2 christos goto end;
8762 1.2 christos
8763 1.2 christos testresult = 1;
8764 1.2 christos
8765 1.2 christos end:
8766 1.2 christos SSL_free(serverssl);
8767 1.2 christos SSL_free(clientssl);
8768 1.2 christos SSL_CTX_free(sctx);
8769 1.2 christos SSL_CTX_free(cctx);
8770 1.2 christos for (i = 0; i < OSSL_NELEM(name); i++)
8771 1.2 christos X509_NAME_free(name[i]);
8772 1.2 christos sk_X509_NAME_pop_free(sk1, X509_NAME_free);
8773 1.2 christos sk_X509_NAME_pop_free(sk2, X509_NAME_free);
8774 1.2 christos
8775 1.2 christos return testresult;
8776 1.2 christos }
8777 1.2 christos #endif
8778 1.2 christos
8779 1.2 christos static int test_ca_names(int tst)
8780 1.2 christos {
8781 1.2 christos int testresult = 1;
8782 1.2 christos
8783 1.2 christos #ifndef OPENSSL_NO_TLS1_2
8784 1.2 christos testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
8785 1.2 christos #endif
8786 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
8787 1.2 christos testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
8788 1.2 christos #endif
8789 1.2 christos
8790 1.2 christos return testresult;
8791 1.2 christos }
8792 1.2 christos
8793 1.2 christos #ifndef OPENSSL_NO_TLS1_2
8794 1.2 christos static const char *multiblock_cipherlist_data[]=
8795 1.2 christos {
8796 1.2 christos "AES128-SHA",
8797 1.2 christos "AES128-SHA256",
8798 1.2 christos "AES256-SHA",
8799 1.2 christos "AES256-SHA256",
8800 1.2 christos };
8801 1.2 christos
8802 1.2 christos /* Reduce the fragment size - so the multiblock test buffer can be small */
8803 1.2 christos # define MULTIBLOCK_FRAGSIZE 512
8804 1.2 christos
8805 1.2 christos static int test_multiblock_write(int test_index)
8806 1.2 christos {
8807 1.2 christos static const char *fetchable_ciphers[]=
8808 1.2 christos {
8809 1.2 christos "AES-128-CBC-HMAC-SHA1",
8810 1.2 christos "AES-128-CBC-HMAC-SHA256",
8811 1.2 christos "AES-256-CBC-HMAC-SHA1",
8812 1.2 christos "AES-256-CBC-HMAC-SHA256"
8813 1.2 christos };
8814 1.2 christos const char *cipherlist = multiblock_cipherlist_data[test_index];
8815 1.2 christos const SSL_METHOD *smeth = TLS_server_method();
8816 1.2 christos const SSL_METHOD *cmeth = TLS_client_method();
8817 1.2 christos int min_version = TLS1_VERSION;
8818 1.2 christos int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
8819 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
8820 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
8821 1.2 christos int testresult = 0;
8822 1.2 christos
8823 1.2 christos /*
8824 1.2 christos * Choose a buffer large enough to perform a multi-block operation
8825 1.2 christos * i.e: write_len >= 4 * frag_size
8826 1.2 christos * 9 * is chosen so that multiple multiblocks are used + some leftover.
8827 1.2 christos */
8828 1.2 christos unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
8829 1.2 christos unsigned char buf[sizeof(msg)], *p = buf;
8830 1.2 christos size_t readbytes, written, len;
8831 1.2 christos EVP_CIPHER *ciph = NULL;
8832 1.2 christos
8833 1.2 christos /*
8834 1.2 christos * Check if the cipher exists before attempting to use it since it only has
8835 1.2 christos * a hardware specific implementation.
8836 1.2 christos */
8837 1.2 christos ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
8838 1.2 christos if (ciph == NULL) {
8839 1.2 christos TEST_skip("Multiblock cipher is not available for %s", cipherlist);
8840 1.2 christos return 1;
8841 1.2 christos }
8842 1.2 christos EVP_CIPHER_free(ciph);
8843 1.2 christos
8844 1.2 christos /* Set up a buffer with some data that will be sent to the client */
8845 1.2 christos RAND_bytes(msg, sizeof(msg));
8846 1.2 christos
8847 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
8848 1.2 christos max_version, &sctx, &cctx, cert,
8849 1.2 christos privkey)))
8850 1.2 christos goto end;
8851 1.2 christos
8852 1.2 christos if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
8853 1.2 christos goto end;
8854 1.2 christos
8855 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8856 1.2 christos NULL, NULL)))
8857 1.2 christos goto end;
8858 1.2 christos
8859 1.2 christos /* settings to force it to use AES-CBC-HMAC_SHA */
8860 1.2 christos SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
8861 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
8862 1.2 christos goto end;
8863 1.2 christos
8864 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8865 1.2 christos goto end;
8866 1.2 christos
8867 1.2 christos if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8868 1.2 christos || !TEST_size_t_eq(written, sizeof(msg)))
8869 1.2 christos goto end;
8870 1.2 christos
8871 1.2 christos len = written;
8872 1.2 christos while (len > 0) {
8873 1.2 christos if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
8874 1.2 christos goto end;
8875 1.2 christos p += readbytes;
8876 1.2 christos len -= readbytes;
8877 1.2 christos }
8878 1.2 christos if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
8879 1.2 christos goto end;
8880 1.2 christos
8881 1.2 christos testresult = 1;
8882 1.2 christos end:
8883 1.2 christos SSL_free(serverssl);
8884 1.2 christos SSL_free(clientssl);
8885 1.2 christos SSL_CTX_free(sctx);
8886 1.2 christos SSL_CTX_free(cctx);
8887 1.2 christos
8888 1.2 christos return testresult;
8889 1.2 christos }
8890 1.2 christos #endif /* OPENSSL_NO_TLS1_2 */
8891 1.2 christos
8892 1.2 christos static int test_session_timeout(int test)
8893 1.2 christos {
8894 1.2 christos /*
8895 1.2 christos * Test session ordering and timeout
8896 1.2 christos * Can't explicitly test performance of the new code,
8897 1.2 christos * but can test to see if the ordering of the sessions
8898 1.2 christos * are correct, and they they are removed as expected
8899 1.2 christos */
8900 1.2 christos SSL_SESSION *early = NULL;
8901 1.2 christos SSL_SESSION *middle = NULL;
8902 1.2 christos SSL_SESSION *late = NULL;
8903 1.2 christos SSL_CTX *ctx;
8904 1.2 christos int testresult = 0;
8905 1.2 christos long now = (long)time(NULL);
8906 1.2 christos #define TIMEOUT 10
8907 1.2 christos
8908 1.2 christos if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
8909 1.2 christos || !TEST_ptr(early = SSL_SESSION_new())
8910 1.2 christos || !TEST_ptr(middle = SSL_SESSION_new())
8911 1.2 christos || !TEST_ptr(late = SSL_SESSION_new()))
8912 1.2 christos goto end;
8913 1.2 christos
8914 1.2 christos /* assign unique session ids */
8915 1.2 christos early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8916 1.2 christos memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
8917 1.2 christos middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8918 1.2 christos memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
8919 1.2 christos late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8920 1.2 christos memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
8921 1.2 christos
8922 1.2 christos if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8923 1.2 christos || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8924 1.2 christos || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8925 1.2 christos goto end;
8926 1.2 christos
8927 1.2 christos /* Make sure they are all added */
8928 1.2 christos if (!TEST_ptr(early->prev)
8929 1.2 christos || !TEST_ptr(middle->prev)
8930 1.2 christos || !TEST_ptr(late->prev))
8931 1.2 christos goto end;
8932 1.2 christos
8933 1.2 christos if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
8934 1.2 christos || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
8935 1.2 christos || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
8936 1.2 christos goto end;
8937 1.2 christos
8938 1.2 christos if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
8939 1.2 christos || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
8940 1.2 christos || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
8941 1.2 christos goto end;
8942 1.2 christos
8943 1.2 christos /* Make sure they are all still there */
8944 1.2 christos if (!TEST_ptr(early->prev)
8945 1.2 christos || !TEST_ptr(middle->prev)
8946 1.2 christos || !TEST_ptr(late->prev))
8947 1.2 christos goto end;
8948 1.2 christos
8949 1.2 christos /* Make sure they are in the expected order */
8950 1.2 christos if (!TEST_ptr_eq(late->next, middle)
8951 1.2 christos || !TEST_ptr_eq(middle->next, early)
8952 1.2 christos || !TEST_ptr_eq(early->prev, middle)
8953 1.2 christos || !TEST_ptr_eq(middle->prev, late))
8954 1.2 christos goto end;
8955 1.2 christos
8956 1.2 christos /* This should remove "early" */
8957 1.2 christos SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
8958 1.2 christos if (!TEST_ptr_null(early->prev)
8959 1.2 christos || !TEST_ptr(middle->prev)
8960 1.2 christos || !TEST_ptr(late->prev))
8961 1.2 christos goto end;
8962 1.2 christos
8963 1.2 christos /* This should remove "middle" */
8964 1.2 christos SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
8965 1.2 christos if (!TEST_ptr_null(early->prev)
8966 1.2 christos || !TEST_ptr_null(middle->prev)
8967 1.2 christos || !TEST_ptr(late->prev))
8968 1.2 christos goto end;
8969 1.2 christos
8970 1.2 christos /* This should remove "late" */
8971 1.2 christos SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
8972 1.2 christos if (!TEST_ptr_null(early->prev)
8973 1.2 christos || !TEST_ptr_null(middle->prev)
8974 1.2 christos || !TEST_ptr_null(late->prev))
8975 1.2 christos goto end;
8976 1.2 christos
8977 1.2 christos /* Add them back in again */
8978 1.2 christos if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8979 1.2 christos || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8980 1.2 christos || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8981 1.2 christos goto end;
8982 1.2 christos
8983 1.2 christos /* Make sure they are all added */
8984 1.2 christos if (!TEST_ptr(early->prev)
8985 1.2 christos || !TEST_ptr(middle->prev)
8986 1.2 christos || !TEST_ptr(late->prev))
8987 1.2 christos goto end;
8988 1.2 christos
8989 1.2 christos /* This should remove all of them */
8990 1.2 christos SSL_CTX_flush_sessions(ctx, 0);
8991 1.2 christos if (!TEST_ptr_null(early->prev)
8992 1.2 christos || !TEST_ptr_null(middle->prev)
8993 1.2 christos || !TEST_ptr_null(late->prev))
8994 1.2 christos goto end;
8995 1.2 christos
8996 1.2 christos (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
8997 1.2 christos | SSL_CTX_get_session_cache_mode(ctx));
8998 1.2 christos
8999 1.2 christos /* make sure |now| is NOT equal to the current time */
9000 1.2 christos now -= 10;
9001 1.2 christos if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
9002 1.2 christos || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
9003 1.2 christos || !TEST_long_ne(SSL_SESSION_get_time(early), now))
9004 1.2 christos goto end;
9005 1.2 christos
9006 1.2 christos testresult = 1;
9007 1.2 christos end:
9008 1.2 christos SSL_CTX_free(ctx);
9009 1.2 christos SSL_SESSION_free(early);
9010 1.2 christos SSL_SESSION_free(middle);
9011 1.2 christos SSL_SESSION_free(late);
9012 1.2 christos return testresult;
9013 1.2 christos }
9014 1.2 christos
9015 1.2 christos /*
9016 1.2 christos * Test that a session cache overflow works as expected
9017 1.2 christos * Test 0: TLSv1.3, timeout on new session later than old session
9018 1.2 christos * Test 1: TLSv1.2, timeout on new session later than old session
9019 1.2 christos * Test 2: TLSv1.3, timeout on new session earlier than old session
9020 1.2 christos * Test 3: TLSv1.2, timeout on new session earlier than old session
9021 1.2 christos */
9022 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
9023 1.2 christos static int test_session_cache_overflow(int idx)
9024 1.2 christos {
9025 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
9026 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
9027 1.2 christos int testresult = 0;
9028 1.2 christos SSL_SESSION *sess = NULL;
9029 1.2 christos
9030 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
9031 1.2 christos /* If no TLSv1.3 available then do nothing in this case */
9032 1.2 christos if (idx % 2 == 0)
9033 1.2 christos return TEST_skip("No TLSv1.3 available");
9034 1.2 christos #endif
9035 1.2 christos #ifdef OPENSSL_NO_TLS1_2
9036 1.2 christos /* If no TLSv1.2 available then do nothing in this case */
9037 1.2 christos if (idx % 2 == 1)
9038 1.2 christos return TEST_skip("No TLSv1.2 available");
9039 1.2 christos #endif
9040 1.2 christos
9041 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9042 1.2 christos TLS_client_method(), TLS1_VERSION,
9043 1.2 christos (idx % 2 == 0) ? TLS1_3_VERSION
9044 1.2 christos : TLS1_2_VERSION,
9045 1.2 christos &sctx, &cctx, cert, privkey))
9046 1.2 christos || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)))
9047 1.2 christos goto end;
9048 1.2 christos
9049 1.2 christos SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
9050 1.2 christos get_sess_val = NULL;
9051 1.2 christos
9052 1.2 christos SSL_CTX_sess_set_cache_size(sctx, 1);
9053 1.2 christos
9054 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9055 1.2 christos NULL, NULL)))
9056 1.2 christos goto end;
9057 1.2 christos
9058 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9059 1.2 christos goto end;
9060 1.2 christos
9061 1.2 christos if (idx > 1) {
9062 1.2 christos sess = SSL_get_session(serverssl);
9063 1.2 christos if (!TEST_ptr(sess))
9064 1.2 christos goto end;
9065 1.2 christos
9066 1.2 christos /*
9067 1.2 christos * Cause this session to have a longer timeout than the next session to
9068 1.2 christos * be added.
9069 1.2 christos */
9070 1.2 christos if (!TEST_true(SSL_SESSION_set_timeout(sess, LONG_MAX / 2))) {
9071 1.2 christos sess = NULL;
9072 1.2 christos goto end;
9073 1.2 christos }
9074 1.2 christos sess = NULL;
9075 1.2 christos }
9076 1.2 christos
9077 1.2 christos SSL_shutdown(serverssl);
9078 1.2 christos SSL_shutdown(clientssl);
9079 1.2 christos SSL_free(serverssl);
9080 1.2 christos SSL_free(clientssl);
9081 1.2 christos serverssl = clientssl = NULL;
9082 1.2 christos
9083 1.2 christos /*
9084 1.2 christos * Session cache size is 1 and we already populated the cache with a session
9085 1.2 christos * so the next connection should cause an overflow.
9086 1.2 christos */
9087 1.2 christos
9088 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9089 1.2 christos NULL, NULL)))
9090 1.2 christos goto end;
9091 1.2 christos
9092 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9093 1.2 christos goto end;
9094 1.2 christos
9095 1.2 christos /*
9096 1.2 christos * The session we just negotiated may have been already removed from the
9097 1.2 christos * internal cache - but we will return it anyway from our external cache.
9098 1.2 christos */
9099 1.2 christos get_sess_val = SSL_get_session(serverssl);
9100 1.2 christos if (!TEST_ptr(get_sess_val))
9101 1.2 christos goto end;
9102 1.2 christos sess = SSL_get1_session(clientssl);
9103 1.2 christos if (!TEST_ptr(sess))
9104 1.2 christos goto end;
9105 1.2 christos
9106 1.2 christos SSL_shutdown(serverssl);
9107 1.2 christos SSL_shutdown(clientssl);
9108 1.2 christos SSL_free(serverssl);
9109 1.2 christos SSL_free(clientssl);
9110 1.2 christos serverssl = clientssl = NULL;
9111 1.2 christos
9112 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9113 1.2 christos NULL, NULL)))
9114 1.2 christos goto end;
9115 1.2 christos
9116 1.2 christos if (!TEST_true(SSL_set_session(clientssl, sess)))
9117 1.2 christos goto end;
9118 1.2 christos
9119 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9120 1.2 christos goto end;
9121 1.2 christos
9122 1.2 christos testresult = 1;
9123 1.2 christos
9124 1.2 christos end:
9125 1.2 christos SSL_free(serverssl);
9126 1.2 christos SSL_free(clientssl);
9127 1.2 christos SSL_CTX_free(sctx);
9128 1.2 christos SSL_CTX_free(cctx);
9129 1.2 christos SSL_SESSION_free(sess);
9130 1.2 christos
9131 1.2 christos return testresult;
9132 1.2 christos }
9133 1.2 christos #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
9134 1.2 christos
9135 1.2 christos /*
9136 1.2 christos * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
9137 1.2 christos * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
9138 1.2 christos * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
9139 1.2 christos * Test 3: Client does not set servername on initial handshake (TLSv1.2)
9140 1.2 christos * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
9141 1.2 christos * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
9142 1.2 christos * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
9143 1.2 christos * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
9144 1.2 christos * Test 8: Client does not set servername on initial handshake(TLSv1.3)
9145 1.2 christos * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
9146 1.2 christos */
9147 1.2 christos static int test_servername(int tst)
9148 1.2 christos {
9149 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9150 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9151 1.2 christos int testresult = 0;
9152 1.2 christos SSL_SESSION *sess = NULL;
9153 1.2 christos const char *sexpectedhost = NULL, *cexpectedhost = NULL;
9154 1.2 christos
9155 1.2 christos #ifdef OPENSSL_NO_TLS1_2
9156 1.2 christos if (tst <= 4)
9157 1.2 christos return 1;
9158 1.2 christos #endif
9159 1.2 christos #ifdef OSSL_NO_USABLE_TLS1_3
9160 1.2 christos if (tst >= 5)
9161 1.2 christos return 1;
9162 1.2 christos #endif
9163 1.2 christos
9164 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9165 1.2 christos TLS_client_method(),
9166 1.2 christos TLS1_VERSION,
9167 1.2 christos (tst <= 4) ? TLS1_2_VERSION
9168 1.2 christos : TLS1_3_VERSION,
9169 1.2 christos &sctx, &cctx, cert, privkey))
9170 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9171 1.2 christos NULL, NULL)))
9172 1.2 christos goto end;
9173 1.2 christos
9174 1.2 christos if (tst != 1 && tst != 6) {
9175 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
9176 1.2 christos hostname_cb)))
9177 1.2 christos goto end;
9178 1.2 christos }
9179 1.2 christos
9180 1.2 christos if (tst != 3 && tst != 8) {
9181 1.2 christos if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9182 1.2 christos goto end;
9183 1.2 christos sexpectedhost = cexpectedhost = "goodhost";
9184 1.2 christos }
9185 1.2 christos
9186 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9187 1.2 christos goto end;
9188 1.2 christos
9189 1.2 christos if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
9190 1.2 christos cexpectedhost)
9191 1.2 christos || !TEST_str_eq(SSL_get_servername(serverssl,
9192 1.2 christos TLSEXT_NAMETYPE_host_name),
9193 1.2 christos sexpectedhost))
9194 1.2 christos goto end;
9195 1.2 christos
9196 1.2 christos /* Now repeat with a resumption handshake */
9197 1.2 christos
9198 1.2 christos if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
9199 1.2 christos || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
9200 1.2 christos || !TEST_true(SSL_SESSION_is_resumable(sess))
9201 1.2 christos || !TEST_int_eq(SSL_shutdown(serverssl), 0))
9202 1.2 christos goto end;
9203 1.2 christos
9204 1.2 christos SSL_free(clientssl);
9205 1.2 christos SSL_free(serverssl);
9206 1.2 christos clientssl = serverssl = NULL;
9207 1.2 christos
9208 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
9209 1.2 christos NULL)))
9210 1.2 christos goto end;
9211 1.2 christos
9212 1.2 christos if (!TEST_true(SSL_set_session(clientssl, sess)))
9213 1.2 christos goto end;
9214 1.2 christos
9215 1.2 christos sexpectedhost = cexpectedhost = "goodhost";
9216 1.2 christos if (tst == 2 || tst == 7) {
9217 1.2 christos /* Set an inconsistent hostname */
9218 1.2 christos if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
9219 1.2 christos goto end;
9220 1.2 christos /*
9221 1.2 christos * In TLSv1.2 we expect the hostname from the original handshake, in
9222 1.2 christos * TLSv1.3 we expect the hostname from this handshake
9223 1.2 christos */
9224 1.2 christos if (tst == 7)
9225 1.2 christos sexpectedhost = cexpectedhost = "altgoodhost";
9226 1.2 christos
9227 1.2 christos if (!TEST_str_eq(SSL_get_servername(clientssl,
9228 1.2 christos TLSEXT_NAMETYPE_host_name),
9229 1.2 christos "altgoodhost"))
9230 1.2 christos goto end;
9231 1.2 christos } else if (tst == 4 || tst == 9) {
9232 1.2 christos /*
9233 1.2 christos * A TLSv1.3 session does not associate a session with a servername,
9234 1.2 christos * but a TLSv1.2 session does.
9235 1.2 christos */
9236 1.2 christos if (tst == 9)
9237 1.2 christos sexpectedhost = cexpectedhost = NULL;
9238 1.2 christos
9239 1.2 christos if (!TEST_str_eq(SSL_get_servername(clientssl,
9240 1.2 christos TLSEXT_NAMETYPE_host_name),
9241 1.2 christos cexpectedhost))
9242 1.2 christos goto end;
9243 1.2 christos } else {
9244 1.2 christos if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9245 1.2 christos goto end;
9246 1.2 christos /*
9247 1.2 christos * In a TLSv1.2 resumption where the hostname was not acknowledged
9248 1.2 christos * we expect the hostname on the server to be empty. On the client we
9249 1.2 christos * return what was requested in this case.
9250 1.2 christos *
9251 1.2 christos * Similarly if the client didn't set a hostname on an original TLSv1.2
9252 1.2 christos * session but is now, the server hostname will be empty, but the client
9253 1.2 christos * is as we set it.
9254 1.2 christos */
9255 1.2 christos if (tst == 1 || tst == 3)
9256 1.2 christos sexpectedhost = NULL;
9257 1.2 christos
9258 1.2 christos if (!TEST_str_eq(SSL_get_servername(clientssl,
9259 1.2 christos TLSEXT_NAMETYPE_host_name),
9260 1.2 christos "goodhost"))
9261 1.2 christos goto end;
9262 1.2 christos }
9263 1.2 christos
9264 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9265 1.2 christos goto end;
9266 1.2 christos
9267 1.2 christos if (!TEST_true(SSL_session_reused(clientssl))
9268 1.2 christos || !TEST_true(SSL_session_reused(serverssl))
9269 1.2 christos || !TEST_str_eq(SSL_get_servername(clientssl,
9270 1.2 christos TLSEXT_NAMETYPE_host_name),
9271 1.2 christos cexpectedhost)
9272 1.2 christos || !TEST_str_eq(SSL_get_servername(serverssl,
9273 1.2 christos TLSEXT_NAMETYPE_host_name),
9274 1.2 christos sexpectedhost))
9275 1.2 christos goto end;
9276 1.2 christos
9277 1.2 christos testresult = 1;
9278 1.2 christos
9279 1.2 christos end:
9280 1.2 christos SSL_SESSION_free(sess);
9281 1.2 christos SSL_free(serverssl);
9282 1.2 christos SSL_free(clientssl);
9283 1.2 christos SSL_CTX_free(sctx);
9284 1.2 christos SSL_CTX_free(cctx);
9285 1.2 christos
9286 1.2 christos return testresult;
9287 1.2 christos }
9288 1.2 christos
9289 1.2 christos #if !defined(OPENSSL_NO_EC) \
9290 1.2 christos && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9291 1.2 christos /*
9292 1.2 christos * Test that if signature algorithms are not available, then we do not offer or
9293 1.2 christos * accept them.
9294 1.2 christos * Test 0: Two RSA sig algs available: both RSA sig algs shared
9295 1.2 christos * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
9296 1.2 christos * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
9297 1.2 christos * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
9298 1.2 christos * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
9299 1.2 christos * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
9300 1.2 christos */
9301 1.2 christos static int test_sigalgs_available(int idx)
9302 1.2 christos {
9303 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9304 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9305 1.2 christos int testresult = 0;
9306 1.2 christos OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
9307 1.2 christos OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
9308 1.2 christos OSSL_PROVIDER *filterprov = NULL;
9309 1.2 christos int sig, hash;
9310 1.2 christos
9311 1.2 christos if (!TEST_ptr(tmpctx))
9312 1.2 christos goto end;
9313 1.2 christos
9314 1.2 christos if (idx != 0 && idx != 3) {
9315 1.2 christos if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
9316 1.2 christos filter_provider_init)))
9317 1.2 christos goto end;
9318 1.2 christos
9319 1.2 christos filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
9320 1.2 christos if (!TEST_ptr(filterprov))
9321 1.2 christos goto end;
9322 1.2 christos
9323 1.2 christos if (idx < 3) {
9324 1.2 christos /*
9325 1.2 christos * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
9326 1.2 christos * or accepted for the peer that uses this libctx. Note that libssl
9327 1.2 christos * *requires* SHA2-256 to be available so we cannot disable that. We
9328 1.2 christos * also need SHA1 for our certificate.
9329 1.2 christos */
9330 1.2 christos if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
9331 1.2 christos "SHA2-256:SHA1")))
9332 1.2 christos goto end;
9333 1.2 christos } else {
9334 1.2 christos if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
9335 1.2 christos "ECDSA"))
9336 1.2 christos || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
9337 1.2 christos "EC:X25519:X448")))
9338 1.2 christos goto end;
9339 1.2 christos }
9340 1.2 christos
9341 1.2 christos if (idx == 1 || idx == 4)
9342 1.2 christos clientctx = tmpctx;
9343 1.2 christos else
9344 1.2 christos serverctx = tmpctx;
9345 1.2 christos }
9346 1.2 christos
9347 1.2 christos cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
9348 1.2 christos sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
9349 1.2 christos if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
9350 1.2 christos goto end;
9351 1.2 christos
9352 1.2 christos if (idx != 5) {
9353 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9354 1.2 christos TLS_client_method(),
9355 1.2 christos TLS1_VERSION,
9356 1.2 christos 0,
9357 1.2 christos &sctx, &cctx, cert, privkey)))
9358 1.2 christos goto end;
9359 1.2 christos } else {
9360 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9361 1.2 christos TLS_client_method(),
9362 1.2 christos TLS1_VERSION,
9363 1.2 christos 0,
9364 1.2 christos &sctx, &cctx, cert2, privkey2)))
9365 1.2 christos goto end;
9366 1.2 christos }
9367 1.2 christos
9368 1.2 christos /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
9369 1.2 christos if (idx < 4) {
9370 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9371 1.2 christos "ECDHE-RSA-AES128-GCM-SHA256")))
9372 1.2 christos goto end;
9373 1.2 christos } else {
9374 1.2 christos if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9375 1.2 christos "ECDHE-ECDSA-AES128-GCM-SHA256")))
9376 1.2 christos goto end;
9377 1.2 christos }
9378 1.2 christos
9379 1.2 christos if (idx < 3) {
9380 1.2 christos if (!SSL_CTX_set1_sigalgs_list(cctx,
9381 1.2 christos "rsa_pss_rsae_sha384"
9382 1.2 christos ":rsa_pss_rsae_sha256")
9383 1.2 christos || !SSL_CTX_set1_sigalgs_list(sctx,
9384 1.2 christos "rsa_pss_rsae_sha384"
9385 1.2 christos ":rsa_pss_rsae_sha256"))
9386 1.2 christos goto end;
9387 1.2 christos } else {
9388 1.2 christos if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
9389 1.2 christos || !SSL_CTX_set1_sigalgs_list(sctx,
9390 1.2 christos "rsa_pss_rsae_sha256:ECDSA+SHA256"))
9391 1.2 christos goto end;
9392 1.2 christos }
9393 1.2 christos
9394 1.2 christos if (idx != 5
9395 1.2 christos && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
9396 1.2 christos SSL_FILETYPE_PEM), 1)
9397 1.2 christos || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
9398 1.2 christos privkey2,
9399 1.2 christos SSL_FILETYPE_PEM), 1)
9400 1.2 christos || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
9401 1.2 christos goto end;
9402 1.2 christos
9403 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9404 1.2 christos NULL, NULL)))
9405 1.2 christos goto end;
9406 1.2 christos
9407 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9408 1.2 christos goto end;
9409 1.2 christos
9410 1.2 christos /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
9411 1.2 christos if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
9412 1.2 christos NULL, NULL),
9413 1.2 christos (idx == 0 || idx == 3) ? 2 : 1))
9414 1.2 christos goto end;
9415 1.2 christos
9416 1.2 christos if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
9417 1.2 christos goto end;
9418 1.2 christos
9419 1.2 christos if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
9420 1.2 christos : NID_rsassaPss))
9421 1.2 christos goto end;
9422 1.2 christos
9423 1.2 christos testresult = filter_provider_check_clean_finish();
9424 1.2 christos
9425 1.2 christos end:
9426 1.2 christos SSL_free(serverssl);
9427 1.2 christos SSL_free(clientssl);
9428 1.2 christos SSL_CTX_free(sctx);
9429 1.2 christos SSL_CTX_free(cctx);
9430 1.2 christos OSSL_PROVIDER_unload(filterprov);
9431 1.2 christos OSSL_LIB_CTX_free(tmpctx);
9432 1.2 christos
9433 1.2 christos return testresult;
9434 1.2 christos }
9435 1.2 christos #endif /*
9436 1.2 christos * !defined(OPENSSL_NO_EC) \
9437 1.2 christos * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9438 1.2 christos */
9439 1.2 christos
9440 1.2 christos #ifndef OPENSSL_NO_TLS1_3
9441 1.2 christos /* This test can run in TLSv1.3 even if ec and dh are disabled */
9442 1.2 christos static int test_pluggable_group(int idx)
9443 1.2 christos {
9444 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9445 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9446 1.2 christos int testresult = 0;
9447 1.2 christos OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
9448 1.2 christos /* Check that we are not impacted by a provider without any groups */
9449 1.2 christos OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
9450 1.2 christos const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup";
9451 1.2 christos
9452 1.2 christos if (!TEST_ptr(tlsprov))
9453 1.2 christos goto end;
9454 1.2 christos
9455 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9456 1.2 christos TLS_client_method(),
9457 1.2 christos TLS1_3_VERSION,
9458 1.2 christos TLS1_3_VERSION,
9459 1.2 christos &sctx, &cctx, cert, privkey))
9460 1.2 christos || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9461 1.2 christos NULL, NULL)))
9462 1.2 christos goto end;
9463 1.2 christos
9464 1.2 christos /* ensure GROUPLIST_INCREMENT (=40) logic triggers: */
9465 1.2 christos if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup:xorkemgroup:dummy1:dummy2:dummy3:dummy4:dummy5:dummy6:dummy7:dummy8:dummy9:dummy10:dummy11:dummy12:dummy13:dummy14:dummy15:dummy16:dummy17:dummy18:dummy19:dummy20:dummy21:dummy22:dummy23:dummy24:dummy25:dummy26:dummy27:dummy28:dummy29:dummy30:dummy31:dummy32:dummy33:dummy34:dummy35:dummy36:dummy37:dummy38:dummy39:dummy40:dummy41:dummy42:dummy43"))
9466 1.2 christos /* removing a single algorithm from the list makes the test pass */
9467 1.2 christos || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
9468 1.2 christos goto end;
9469 1.2 christos
9470 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9471 1.2 christos goto end;
9472 1.2 christos
9473 1.2 christos if (!TEST_str_eq(group_name,
9474 1.2 christos SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
9475 1.2 christos goto end;
9476 1.2 christos
9477 1.2 christos testresult = 1;
9478 1.2 christos
9479 1.2 christos end:
9480 1.2 christos SSL_free(serverssl);
9481 1.2 christos SSL_free(clientssl);
9482 1.2 christos SSL_CTX_free(sctx);
9483 1.2 christos SSL_CTX_free(cctx);
9484 1.2 christos OSSL_PROVIDER_unload(tlsprov);
9485 1.2 christos OSSL_PROVIDER_unload(legacyprov);
9486 1.2 christos
9487 1.2 christos return testresult;
9488 1.2 christos }
9489 1.2 christos #endif
9490 1.2 christos
9491 1.2 christos #ifndef OPENSSL_NO_TLS1_2
9492 1.2 christos static int test_ssl_dup(void)
9493 1.2 christos {
9494 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9495 1.2 christos SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
9496 1.2 christos int testresult = 0;
9497 1.2 christos BIO *rbio = NULL, *wbio = NULL;
9498 1.2 christos
9499 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9500 1.2 christos TLS_client_method(),
9501 1.2 christos 0,
9502 1.2 christos 0,
9503 1.2 christos &sctx, &cctx, cert, privkey)))
9504 1.2 christos goto end;
9505 1.2 christos
9506 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9507 1.2 christos NULL, NULL)))
9508 1.2 christos goto end;
9509 1.2 christos
9510 1.2 christos if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
9511 1.2 christos || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
9512 1.2 christos goto end;
9513 1.2 christos
9514 1.2 christos client2ssl = SSL_dup(clientssl);
9515 1.2 christos rbio = SSL_get_rbio(clientssl);
9516 1.2 christos if (!TEST_ptr(rbio)
9517 1.2 christos || !TEST_true(BIO_up_ref(rbio)))
9518 1.2 christos goto end;
9519 1.2 christos SSL_set0_rbio(client2ssl, rbio);
9520 1.2 christos rbio = NULL;
9521 1.2 christos
9522 1.2 christos wbio = SSL_get_wbio(clientssl);
9523 1.2 christos if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
9524 1.2 christos goto end;
9525 1.2 christos SSL_set0_wbio(client2ssl, wbio);
9526 1.2 christos rbio = NULL;
9527 1.2 christos
9528 1.2 christos if (!TEST_ptr(client2ssl)
9529 1.2 christos /* Handshake not started so pointers should be different */
9530 1.2 christos || !TEST_ptr_ne(clientssl, client2ssl))
9531 1.2 christos goto end;
9532 1.2 christos
9533 1.2 christos if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
9534 1.2 christos || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
9535 1.2 christos goto end;
9536 1.2 christos
9537 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
9538 1.2 christos goto end;
9539 1.2 christos
9540 1.2 christos SSL_free(clientssl);
9541 1.2 christos clientssl = SSL_dup(client2ssl);
9542 1.2 christos if (!TEST_ptr(clientssl)
9543 1.2 christos /* Handshake has finished so pointers should be the same */
9544 1.2 christos || !TEST_ptr_eq(clientssl, client2ssl))
9545 1.2 christos goto end;
9546 1.2 christos
9547 1.2 christos testresult = 1;
9548 1.2 christos
9549 1.2 christos end:
9550 1.2 christos SSL_free(serverssl);
9551 1.2 christos SSL_free(clientssl);
9552 1.2 christos SSL_free(client2ssl);
9553 1.2 christos SSL_CTX_free(sctx);
9554 1.2 christos SSL_CTX_free(cctx);
9555 1.2 christos
9556 1.2 christos return testresult;
9557 1.2 christos }
9558 1.2 christos
9559 1.2 christos # ifndef OPENSSL_NO_DH
9560 1.2 christos
9561 1.2 christos static EVP_PKEY *tmp_dh_params = NULL;
9562 1.2 christos
9563 1.2 christos /* Helper function for the test_set_tmp_dh() tests */
9564 1.2 christos static EVP_PKEY *get_tmp_dh_params(void)
9565 1.2 christos {
9566 1.2 christos if (tmp_dh_params == NULL) {
9567 1.2 christos BIGNUM *p = NULL;
9568 1.2 christos OSSL_PARAM_BLD *tmpl = NULL;
9569 1.2 christos EVP_PKEY_CTX *pctx = NULL;
9570 1.2 christos OSSL_PARAM *params = NULL;
9571 1.2 christos EVP_PKEY *dhpkey = NULL;
9572 1.2 christos
9573 1.2 christos p = BN_get_rfc3526_prime_2048(NULL);
9574 1.2 christos if (!TEST_ptr(p))
9575 1.2 christos goto end;
9576 1.2 christos
9577 1.2 christos pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
9578 1.2 christos if (!TEST_ptr(pctx)
9579 1.2 christos || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
9580 1.2 christos goto end;
9581 1.2 christos
9582 1.2 christos tmpl = OSSL_PARAM_BLD_new();
9583 1.2 christos if (!TEST_ptr(tmpl)
9584 1.2 christos || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
9585 1.2 christos OSSL_PKEY_PARAM_FFC_P,
9586 1.2 christos p))
9587 1.2 christos || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
9588 1.2 christos OSSL_PKEY_PARAM_FFC_G,
9589 1.2 christos 2)))
9590 1.2 christos goto end;
9591 1.2 christos
9592 1.2 christos params = OSSL_PARAM_BLD_to_param(tmpl);
9593 1.2 christos if (!TEST_ptr(params)
9594 1.2 christos || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
9595 1.2 christos EVP_PKEY_KEY_PARAMETERS,
9596 1.2 christos params), 1))
9597 1.2 christos goto end;
9598 1.2 christos
9599 1.2 christos tmp_dh_params = dhpkey;
9600 1.2 christos end:
9601 1.2 christos BN_free(p);
9602 1.2 christos EVP_PKEY_CTX_free(pctx);
9603 1.2 christos OSSL_PARAM_BLD_free(tmpl);
9604 1.2 christos OSSL_PARAM_free(params);
9605 1.2 christos }
9606 1.2 christos
9607 1.2 christos if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
9608 1.2 christos return NULL;
9609 1.2 christos
9610 1.2 christos return tmp_dh_params;
9611 1.2 christos }
9612 1.2 christos
9613 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9614 1.2 christos /* Callback used by test_set_tmp_dh() */
9615 1.2 christos static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
9616 1.2 christos {
9617 1.2 christos EVP_PKEY *dhpkey = get_tmp_dh_params();
9618 1.2 christos DH *ret = NULL;
9619 1.2 christos
9620 1.2 christos if (!TEST_ptr(dhpkey))
9621 1.2 christos return NULL;
9622 1.2 christos
9623 1.2 christos /*
9624 1.2 christos * libssl does not free the returned DH, so we free it now knowing that even
9625 1.2 christos * after we free dhpkey, there will still be a reference to the owning
9626 1.2 christos * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
9627 1.2 christos * of time we need it for.
9628 1.2 christos */
9629 1.2 christos ret = EVP_PKEY_get1_DH(dhpkey);
9630 1.2 christos DH_free(ret);
9631 1.2 christos
9632 1.2 christos EVP_PKEY_free(dhpkey);
9633 1.2 christos
9634 1.2 christos return ret;
9635 1.2 christos }
9636 1.2 christos # endif
9637 1.2 christos
9638 1.2 christos /*
9639 1.2 christos * Test the various methods for setting temporary DH parameters
9640 1.2 christos *
9641 1.2 christos * Test 0: Default (no auto) setting
9642 1.2 christos * Test 1: Explicit SSL_CTX auto off
9643 1.2 christos * Test 2: Explicit SSL auto off
9644 1.2 christos * Test 3: Explicit SSL_CTX auto on
9645 1.2 christos * Test 4: Explicit SSL auto on
9646 1.2 christos * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
9647 1.2 christos * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
9648 1.2 christos *
9649 1.2 christos * The following are testing deprecated APIs, so we only run them if available
9650 1.2 christos * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
9651 1.2 christos * Test 8: Explicit SSL auto off, custom DH params via DH
9652 1.2 christos * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
9653 1.2 christos * Test 10: Explicit SSL auto off, custom DH params via callback
9654 1.2 christos */
9655 1.2 christos static int test_set_tmp_dh(int idx)
9656 1.2 christos {
9657 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9658 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9659 1.2 christos int testresult = 0;
9660 1.2 christos int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
9661 1.2 christos int expected = (idx <= 2) ? 0 : 1;
9662 1.2 christos EVP_PKEY *dhpkey = NULL;
9663 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9664 1.2 christos DH *dh = NULL;
9665 1.2 christos # else
9666 1.2 christos
9667 1.2 christos if (idx >= 7)
9668 1.2 christos return 1;
9669 1.2 christos # endif
9670 1.2 christos
9671 1.2 christos if (idx >= 5 && idx <= 8) {
9672 1.2 christos dhpkey = get_tmp_dh_params();
9673 1.2 christos if (!TEST_ptr(dhpkey))
9674 1.2 christos goto end;
9675 1.2 christos }
9676 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9677 1.2 christos if (idx == 7 || idx == 8) {
9678 1.2 christos dh = EVP_PKEY_get1_DH(dhpkey);
9679 1.2 christos if (!TEST_ptr(dh))
9680 1.2 christos goto end;
9681 1.2 christos }
9682 1.2 christos # endif
9683 1.2 christos
9684 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9685 1.2 christos TLS_client_method(),
9686 1.2 christos 0,
9687 1.2 christos 0,
9688 1.2 christos &sctx, &cctx, cert, privkey)))
9689 1.2 christos goto end;
9690 1.2 christos
9691 1.2 christos if ((idx & 1) == 1) {
9692 1.2 christos if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
9693 1.2 christos goto end;
9694 1.2 christos }
9695 1.2 christos
9696 1.2 christos if (idx == 5) {
9697 1.2 christos if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
9698 1.2 christos goto end;
9699 1.2 christos dhpkey = NULL;
9700 1.2 christos }
9701 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9702 1.2 christos else if (idx == 7) {
9703 1.2 christos if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
9704 1.2 christos goto end;
9705 1.2 christos } else if (idx == 9) {
9706 1.2 christos SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
9707 1.2 christos }
9708 1.2 christos # endif
9709 1.2 christos
9710 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9711 1.2 christos NULL, NULL)))
9712 1.2 christos goto end;
9713 1.2 christos
9714 1.2 christos if ((idx & 1) == 0 && idx != 0) {
9715 1.2 christos if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
9716 1.2 christos goto end;
9717 1.2 christos }
9718 1.2 christos if (idx == 6) {
9719 1.2 christos if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
9720 1.2 christos goto end;
9721 1.2 christos dhpkey = NULL;
9722 1.2 christos }
9723 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9724 1.2 christos else if (idx == 8) {
9725 1.2 christos if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
9726 1.2 christos goto end;
9727 1.2 christos } else if (idx == 10) {
9728 1.2 christos SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
9729 1.2 christos }
9730 1.2 christos # endif
9731 1.2 christos
9732 1.2 christos if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9733 1.2 christos || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9734 1.2 christos || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
9735 1.2 christos goto end;
9736 1.2 christos
9737 1.2 christos /*
9738 1.2 christos * If autoon then we should succeed. Otherwise we expect failure because
9739 1.2 christos * there are no parameters
9740 1.2 christos */
9741 1.2 christos if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
9742 1.2 christos SSL_ERROR_NONE), expected))
9743 1.2 christos goto end;
9744 1.2 christos
9745 1.2 christos testresult = 1;
9746 1.2 christos
9747 1.2 christos end:
9748 1.2 christos # ifndef OPENSSL_NO_DEPRECATED_3_0
9749 1.2 christos DH_free(dh);
9750 1.2 christos # endif
9751 1.2 christos SSL_free(serverssl);
9752 1.2 christos SSL_free(clientssl);
9753 1.2 christos SSL_CTX_free(sctx);
9754 1.2 christos SSL_CTX_free(cctx);
9755 1.2 christos EVP_PKEY_free(dhpkey);
9756 1.2 christos
9757 1.2 christos return testresult;
9758 1.2 christos }
9759 1.2 christos
9760 1.2 christos /*
9761 1.2 christos * Test the auto DH keys are appropriately sized
9762 1.2 christos */
9763 1.2 christos static int test_dh_auto(int idx)
9764 1.2 christos {
9765 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9766 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9767 1.2 christos int testresult = 0;
9768 1.2 christos EVP_PKEY *tmpkey = NULL;
9769 1.2 christos char *thiscert = NULL, *thiskey = NULL;
9770 1.2 christos size_t expdhsize = 0;
9771 1.2 christos const char *ciphersuite = "DHE-RSA-AES128-SHA";
9772 1.2 christos
9773 1.2 christos switch (idx) {
9774 1.2 christos case 0:
9775 1.2 christos /* The FIPS provider doesn't support this DH size - so we ignore it */
9776 1.2 christos if (is_fips)
9777 1.2 christos return 1;
9778 1.2 christos thiscert = cert1024;
9779 1.2 christos thiskey = privkey1024;
9780 1.2 christos expdhsize = 1024;
9781 1.2 christos break;
9782 1.2 christos case 1:
9783 1.2 christos /* 2048 bit prime */
9784 1.2 christos thiscert = cert;
9785 1.2 christos thiskey = privkey;
9786 1.2 christos expdhsize = 2048;
9787 1.2 christos break;
9788 1.2 christos case 2:
9789 1.2 christos thiscert = cert3072;
9790 1.2 christos thiskey = privkey3072;
9791 1.2 christos expdhsize = 3072;
9792 1.2 christos break;
9793 1.2 christos case 3:
9794 1.2 christos thiscert = cert4096;
9795 1.2 christos thiskey = privkey4096;
9796 1.2 christos expdhsize = 4096;
9797 1.2 christos break;
9798 1.2 christos case 4:
9799 1.2 christos thiscert = cert8192;
9800 1.2 christos thiskey = privkey8192;
9801 1.2 christos expdhsize = 8192;
9802 1.2 christos break;
9803 1.2 christos /* No certificate cases */
9804 1.2 christos case 5:
9805 1.2 christos /* The FIPS provider doesn't support this DH size - so we ignore it */
9806 1.2 christos if (is_fips)
9807 1.2 christos return 1;
9808 1.2 christos ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
9809 1.2 christos expdhsize = 1024;
9810 1.2 christos break;
9811 1.2 christos case 6:
9812 1.2 christos ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
9813 1.2 christos expdhsize = 3072;
9814 1.2 christos break;
9815 1.2 christos default:
9816 1.2 christos TEST_error("Invalid text index");
9817 1.2 christos goto end;
9818 1.2 christos }
9819 1.2 christos
9820 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9821 1.2 christos TLS_client_method(),
9822 1.2 christos 0,
9823 1.2 christos 0,
9824 1.2 christos &sctx, &cctx, thiscert, thiskey)))
9825 1.2 christos goto end;
9826 1.2 christos
9827 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9828 1.2 christos NULL, NULL)))
9829 1.2 christos goto end;
9830 1.2 christos
9831 1.2 christos if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
9832 1.2 christos || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9833 1.2 christos || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9834 1.2 christos || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
9835 1.2 christos || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
9836 1.2 christos goto end;
9837 1.2 christos
9838 1.2 christos /*
9839 1.2 christos * Send the server's first flight. At this point the server has created the
9840 1.2 christos * temporary DH key but hasn't finished using it yet. Once used it is
9841 1.2 christos * removed, so we cannot test it.
9842 1.2 christos */
9843 1.2 christos if (!TEST_int_le(SSL_connect(clientssl), 0)
9844 1.2 christos || !TEST_int_le(SSL_accept(serverssl), 0))
9845 1.2 christos goto end;
9846 1.2 christos
9847 1.2 christos if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
9848 1.2 christos goto end;
9849 1.2 christos if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
9850 1.2 christos goto end;
9851 1.2 christos
9852 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9853 1.2 christos goto end;
9854 1.2 christos
9855 1.2 christos testresult = 1;
9856 1.2 christos
9857 1.2 christos end:
9858 1.2 christos SSL_free(serverssl);
9859 1.2 christos SSL_free(clientssl);
9860 1.2 christos SSL_CTX_free(sctx);
9861 1.2 christos SSL_CTX_free(cctx);
9862 1.2 christos EVP_PKEY_free(tmpkey);
9863 1.2 christos
9864 1.2 christos return testresult;
9865 1.2 christos
9866 1.2 christos }
9867 1.2 christos # endif /* OPENSSL_NO_DH */
9868 1.2 christos #endif /* OPENSSL_NO_TLS1_2 */
9869 1.2 christos
9870 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
9871 1.2 christos /*
9872 1.2 christos * Test that setting an SNI callback works with TLSv1.3. Specifically we check
9873 1.2 christos * that it works even without a certificate configured for the original
9874 1.2 christos * SSL_CTX
9875 1.2 christos */
9876 1.2 christos static int test_sni_tls13(void)
9877 1.2 christos {
9878 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
9879 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9880 1.2 christos int testresult = 0;
9881 1.2 christos
9882 1.2 christos /* Reset callback counter */
9883 1.2 christos snicb = 0;
9884 1.2 christos
9885 1.2 christos /* Create an initial SSL_CTX with no certificate configured */
9886 1.2 christos sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9887 1.2 christos if (!TEST_ptr(sctx))
9888 1.2 christos goto end;
9889 1.2 christos /* Require TLSv1.3 as a minimum */
9890 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9891 1.2 christos TLS_client_method(), TLS1_3_VERSION, 0,
9892 1.2 christos &sctx2, &cctx, cert, privkey)))
9893 1.2 christos goto end;
9894 1.2 christos
9895 1.2 christos /* Set up SNI */
9896 1.2 christos if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
9897 1.2 christos || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
9898 1.2 christos goto end;
9899 1.2 christos
9900 1.2 christos /*
9901 1.2 christos * Connection should still succeed because the final SSL_CTX has the right
9902 1.2 christos * certificates configured.
9903 1.2 christos */
9904 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9905 1.2 christos &clientssl, NULL, NULL))
9906 1.2 christos || !TEST_true(create_ssl_connection(serverssl, clientssl,
9907 1.2 christos SSL_ERROR_NONE)))
9908 1.2 christos goto end;
9909 1.2 christos
9910 1.2 christos /* We should have had the SNI callback called exactly once */
9911 1.2 christos if (!TEST_int_eq(snicb, 1))
9912 1.2 christos goto end;
9913 1.2 christos
9914 1.2 christos testresult = 1;
9915 1.2 christos
9916 1.2 christos end:
9917 1.2 christos SSL_free(serverssl);
9918 1.2 christos SSL_free(clientssl);
9919 1.2 christos SSL_CTX_free(sctx2);
9920 1.2 christos SSL_CTX_free(sctx);
9921 1.2 christos SSL_CTX_free(cctx);
9922 1.2 christos return testresult;
9923 1.2 christos }
9924 1.2 christos
9925 1.2 christos /*
9926 1.2 christos * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
9927 1.2 christos * 0 = TLSv1.2
9928 1.2 christos * 1 = TLSv1.3
9929 1.2 christos */
9930 1.2 christos static int test_ticket_lifetime(int idx)
9931 1.2 christos {
9932 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
9933 1.2 christos SSL *clientssl = NULL, *serverssl = NULL;
9934 1.2 christos int testresult = 0;
9935 1.2 christos int version = TLS1_3_VERSION;
9936 1.2 christos
9937 1.2 christos #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
9938 1.2 christos #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
9939 1.2 christos
9940 1.2 christos if (idx == 0) {
9941 1.2 christos #ifdef OPENSSL_NO_TLS1_2
9942 1.2 christos return TEST_skip("TLS 1.2 is disabled.");
9943 1.2 christos #else
9944 1.2 christos version = TLS1_2_VERSION;
9945 1.2 christos #endif
9946 1.2 christos }
9947 1.2 christos
9948 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9949 1.2 christos TLS_client_method(), version, version,
9950 1.2 christos &sctx, &cctx, cert, privkey)))
9951 1.2 christos goto end;
9952 1.2 christos
9953 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9954 1.2 christos &clientssl, NULL, NULL)))
9955 1.2 christos goto end;
9956 1.2 christos
9957 1.2 christos /*
9958 1.2 christos * Set the timeout to be more than 1 week
9959 1.2 christos * make sure the returned value is the default
9960 1.2 christos */
9961 1.2 christos if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
9962 1.2 christos SSL_get_default_timeout(serverssl)))
9963 1.2 christos goto end;
9964 1.2 christos
9965 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9966 1.2 christos goto end;
9967 1.2 christos
9968 1.2 christos if (idx == 0) {
9969 1.2 christos /* TLSv1.2 uses the set value */
9970 1.2 christos if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
9971 1.2 christos goto end;
9972 1.2 christos } else {
9973 1.2 christos /* TLSv1.3 uses the limited value */
9974 1.2 christos if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
9975 1.2 christos goto end;
9976 1.2 christos }
9977 1.2 christos testresult = 1;
9978 1.2 christos
9979 1.2 christos end:
9980 1.2 christos SSL_free(serverssl);
9981 1.2 christos SSL_free(clientssl);
9982 1.2 christos SSL_CTX_free(sctx);
9983 1.2 christos SSL_CTX_free(cctx);
9984 1.2 christos return testresult;
9985 1.2 christos }
9986 1.2 christos #endif
9987 1.2 christos /*
9988 1.2 christos * Test that setting an ALPN does not violate RFC
9989 1.2 christos */
9990 1.2 christos static int test_set_alpn(void)
9991 1.2 christos {
9992 1.2 christos SSL_CTX *ctx = NULL;
9993 1.2 christos SSL *ssl = NULL;
9994 1.2 christos int testresult = 0;
9995 1.2 christos
9996 1.2 christos unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
9997 1.2 christos unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
9998 1.2 christos unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
9999 1.2 christos unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
10000 1.2 christos unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
10001 1.2 christos unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
10002 1.2 christos
10003 1.2 christos /* Create an initial SSL_CTX with no certificate configured */
10004 1.2 christos ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10005 1.2 christos if (!TEST_ptr(ctx))
10006 1.2 christos goto end;
10007 1.2 christos
10008 1.2 christos /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
10009 1.2 christos if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
10010 1.2 christos goto end;
10011 1.2 christos if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
10012 1.2 christos goto end;
10013 1.2 christos if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
10014 1.2 christos goto end;
10015 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
10016 1.2 christos goto end;
10017 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
10018 1.2 christos goto end;
10019 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
10020 1.2 christos goto end;
10021 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
10022 1.2 christos goto end;
10023 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
10024 1.2 christos goto end;
10025 1.2 christos if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
10026 1.2 christos goto end;
10027 1.2 christos
10028 1.2 christos ssl = SSL_new(ctx);
10029 1.2 christos if (!TEST_ptr(ssl))
10030 1.2 christos goto end;
10031 1.2 christos
10032 1.2 christos if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
10033 1.2 christos goto end;
10034 1.2 christos if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
10035 1.2 christos goto end;
10036 1.2 christos if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
10037 1.2 christos goto end;
10038 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
10039 1.2 christos goto end;
10040 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
10041 1.2 christos goto end;
10042 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
10043 1.2 christos goto end;
10044 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
10045 1.2 christos goto end;
10046 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
10047 1.2 christos goto end;
10048 1.2 christos if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
10049 1.2 christos goto end;
10050 1.2 christos
10051 1.2 christos testresult = 1;
10052 1.2 christos
10053 1.2 christos end:
10054 1.2 christos SSL_free(ssl);
10055 1.2 christos SSL_CTX_free(ctx);
10056 1.2 christos return testresult;
10057 1.2 christos }
10058 1.2 christos
10059 1.2 christos /*
10060 1.2 christos * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
10061 1.2 christos */
10062 1.2 christos static int test_set_verify_cert_store_ssl_ctx(void)
10063 1.2 christos {
10064 1.2 christos SSL_CTX *ctx = NULL;
10065 1.2 christos int testresult = 0;
10066 1.2 christos X509_STORE *store = NULL, *new_store = NULL,
10067 1.2 christos *cstore = NULL, *new_cstore = NULL;
10068 1.2 christos
10069 1.2 christos /* Create an initial SSL_CTX. */
10070 1.2 christos ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10071 1.2 christos if (!TEST_ptr(ctx))
10072 1.2 christos goto end;
10073 1.2 christos
10074 1.2 christos /* Retrieve verify store pointer. */
10075 1.2 christos if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10076 1.2 christos goto end;
10077 1.2 christos
10078 1.2 christos /* Retrieve chain store pointer. */
10079 1.2 christos if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10080 1.2 christos goto end;
10081 1.2 christos
10082 1.2 christos /* We haven't set any yet, so this should be NULL. */
10083 1.2 christos if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10084 1.2 christos goto end;
10085 1.2 christos
10086 1.2 christos /* Create stores. We use separate stores so pointers are different. */
10087 1.2 christos new_store = X509_STORE_new();
10088 1.2 christos if (!TEST_ptr(new_store))
10089 1.2 christos goto end;
10090 1.2 christos
10091 1.2 christos new_cstore = X509_STORE_new();
10092 1.2 christos if (!TEST_ptr(new_cstore))
10093 1.2 christos goto end;
10094 1.2 christos
10095 1.2 christos /* Set stores. */
10096 1.2 christos if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
10097 1.2 christos goto end;
10098 1.2 christos
10099 1.2 christos if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
10100 1.2 christos goto end;
10101 1.2 christos
10102 1.2 christos /* Should be able to retrieve the same pointer. */
10103 1.2 christos if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10104 1.2 christos goto end;
10105 1.2 christos
10106 1.2 christos if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10107 1.2 christos goto end;
10108 1.2 christos
10109 1.2 christos if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10110 1.2 christos goto end;
10111 1.2 christos
10112 1.2 christos /* Should be able to unset again. */
10113 1.2 christos if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
10114 1.2 christos goto end;
10115 1.2 christos
10116 1.2 christos if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
10117 1.2 christos goto end;
10118 1.2 christos
10119 1.2 christos /* Should now be NULL. */
10120 1.2 christos if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10121 1.2 christos goto end;
10122 1.2 christos
10123 1.2 christos if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10124 1.2 christos goto end;
10125 1.2 christos
10126 1.2 christos if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10127 1.2 christos goto end;
10128 1.2 christos
10129 1.2 christos testresult = 1;
10130 1.2 christos
10131 1.2 christos end:
10132 1.2 christos X509_STORE_free(new_store);
10133 1.2 christos X509_STORE_free(new_cstore);
10134 1.2 christos SSL_CTX_free(ctx);
10135 1.2 christos return testresult;
10136 1.2 christos }
10137 1.2 christos
10138 1.2 christos /*
10139 1.2 christos * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
10140 1.2 christos */
10141 1.2 christos static int test_set_verify_cert_store_ssl(void)
10142 1.2 christos {
10143 1.2 christos SSL_CTX *ctx = NULL;
10144 1.2 christos SSL *ssl = NULL;
10145 1.2 christos int testresult = 0;
10146 1.2 christos X509_STORE *store = NULL, *new_store = NULL,
10147 1.2 christos *cstore = NULL, *new_cstore = NULL;
10148 1.2 christos
10149 1.2 christos /* Create an initial SSL_CTX. */
10150 1.2 christos ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10151 1.2 christos if (!TEST_ptr(ctx))
10152 1.2 christos goto end;
10153 1.2 christos
10154 1.2 christos /* Create an SSL object. */
10155 1.2 christos ssl = SSL_new(ctx);
10156 1.2 christos if (!TEST_ptr(ssl))
10157 1.2 christos goto end;
10158 1.2 christos
10159 1.2 christos /* Retrieve verify store pointer. */
10160 1.2 christos if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10161 1.2 christos goto end;
10162 1.2 christos
10163 1.2 christos /* Retrieve chain store pointer. */
10164 1.2 christos if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10165 1.2 christos goto end;
10166 1.2 christos
10167 1.2 christos /* We haven't set any yet, so this should be NULL. */
10168 1.2 christos if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10169 1.2 christos goto end;
10170 1.2 christos
10171 1.2 christos /* Create stores. We use separate stores so pointers are different. */
10172 1.2 christos new_store = X509_STORE_new();
10173 1.2 christos if (!TEST_ptr(new_store))
10174 1.2 christos goto end;
10175 1.2 christos
10176 1.2 christos new_cstore = X509_STORE_new();
10177 1.2 christos if (!TEST_ptr(new_cstore))
10178 1.2 christos goto end;
10179 1.2 christos
10180 1.2 christos /* Set stores. */
10181 1.2 christos if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
10182 1.2 christos goto end;
10183 1.2 christos
10184 1.2 christos if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
10185 1.2 christos goto end;
10186 1.2 christos
10187 1.2 christos /* Should be able to retrieve the same pointer. */
10188 1.2 christos if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10189 1.2 christos goto end;
10190 1.2 christos
10191 1.2 christos if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10192 1.2 christos goto end;
10193 1.2 christos
10194 1.2 christos if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10195 1.2 christos goto end;
10196 1.2 christos
10197 1.2 christos /* Should be able to unset again. */
10198 1.2 christos if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
10199 1.2 christos goto end;
10200 1.2 christos
10201 1.2 christos if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
10202 1.2 christos goto end;
10203 1.2 christos
10204 1.2 christos /* Should now be NULL. */
10205 1.2 christos if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10206 1.2 christos goto end;
10207 1.2 christos
10208 1.2 christos if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10209 1.2 christos goto end;
10210 1.2 christos
10211 1.2 christos if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10212 1.2 christos goto end;
10213 1.2 christos
10214 1.2 christos testresult = 1;
10215 1.2 christos
10216 1.2 christos end:
10217 1.2 christos X509_STORE_free(new_store);
10218 1.2 christos X509_STORE_free(new_cstore);
10219 1.2 christos SSL_free(ssl);
10220 1.2 christos SSL_CTX_free(ctx);
10221 1.2 christos return testresult;
10222 1.2 christos }
10223 1.2 christos
10224 1.2 christos
10225 1.2 christos static int test_inherit_verify_param(void)
10226 1.2 christos {
10227 1.2 christos int testresult = 0;
10228 1.2 christos
10229 1.2 christos SSL_CTX *ctx = NULL;
10230 1.2 christos X509_VERIFY_PARAM *cp = NULL;
10231 1.2 christos SSL *ssl = NULL;
10232 1.2 christos X509_VERIFY_PARAM *sp = NULL;
10233 1.2 christos int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
10234 1.2 christos
10235 1.2 christos ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10236 1.2 christos if (!TEST_ptr(ctx))
10237 1.2 christos goto end;
10238 1.2 christos
10239 1.2 christos cp = SSL_CTX_get0_param(ctx);
10240 1.2 christos if (!TEST_ptr(cp))
10241 1.2 christos goto end;
10242 1.2 christos if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
10243 1.2 christos goto end;
10244 1.2 christos
10245 1.2 christos X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
10246 1.2 christos
10247 1.2 christos ssl = SSL_new(ctx);
10248 1.2 christos if (!TEST_ptr(ssl))
10249 1.2 christos goto end;
10250 1.2 christos
10251 1.2 christos sp = SSL_get0_param(ssl);
10252 1.2 christos if (!TEST_ptr(sp))
10253 1.2 christos goto end;
10254 1.2 christos if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
10255 1.2 christos goto end;
10256 1.2 christos
10257 1.2 christos testresult = 1;
10258 1.2 christos
10259 1.2 christos end:
10260 1.2 christos SSL_free(ssl);
10261 1.2 christos SSL_CTX_free(ctx);
10262 1.2 christos
10263 1.2 christos return testresult;
10264 1.2 christos }
10265 1.2 christos
10266 1.2 christos static int test_load_dhfile(void)
10267 1.2 christos {
10268 1.2 christos #ifndef OPENSSL_NO_DH
10269 1.2 christos int testresult = 0;
10270 1.2 christos
10271 1.2 christos SSL_CTX *ctx = NULL;
10272 1.2 christos SSL_CONF_CTX *cctx = NULL;
10273 1.2 christos
10274 1.2 christos if (dhfile == NULL)
10275 1.2 christos return 1;
10276 1.2 christos
10277 1.2 christos if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
10278 1.2 christos || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
10279 1.2 christos goto end;
10280 1.2 christos
10281 1.2 christos SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
10282 1.2 christos SSL_CONF_CTX_set_flags(cctx,
10283 1.2 christos SSL_CONF_FLAG_CERTIFICATE
10284 1.2 christos | SSL_CONF_FLAG_SERVER
10285 1.2 christos | SSL_CONF_FLAG_FILE);
10286 1.2 christos
10287 1.2 christos if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
10288 1.2 christos goto end;
10289 1.2 christos
10290 1.2 christos testresult = 1;
10291 1.2 christos end:
10292 1.2 christos SSL_CONF_CTX_free(cctx);
10293 1.2 christos SSL_CTX_free(ctx);
10294 1.2 christos
10295 1.2 christos return testresult;
10296 1.2 christos #else
10297 1.2 christos return TEST_skip("DH not supported by this build");
10298 1.2 christos #endif
10299 1.2 christos }
10300 1.2 christos
10301 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
10302 1.2 christos /*
10303 1.2 christos * Test TLSv1.2 with a pipeline capable cipher. TLSv1.3 and DTLS do not
10304 1.2 christos * support this yet. The only pipeline capable cipher that we have is in the
10305 1.2 christos * dasync engine (providers don't support this yet), so we have to use
10306 1.2 christos * deprecated APIs for this test.
10307 1.2 christos *
10308 1.2 christos * Test 0: Client has pipelining enabled, server does not
10309 1.2 christos * Test 1: Server has pipelining enabled, client does not
10310 1.2 christos * Test 2: Client has pipelining enabled, server does not: not enough data to
10311 1.2 christos * fill all the pipelines
10312 1.2 christos * Test 3: Client has pipelining enabled, server does not: not enough data to
10313 1.2 christos * fill all the pipelines by more than a full pipeline's worth
10314 1.2 christos * Test 4: Client has pipelining enabled, server does not: more data than all
10315 1.2 christos * the available pipelines can take
10316 1.2 christos * Test 5: Client has pipelining enabled, server does not: Maximum size pipeline
10317 1.2 christos * Test 6: Repeat of test 0, but the engine is loaded late (after the SSL_CTX
10318 1.2 christos * is created)
10319 1.2 christos */
10320 1.2 christos static int test_pipelining(int idx)
10321 1.2 christos {
10322 1.2 christos SSL_CTX *cctx = NULL, *sctx = NULL;
10323 1.2 christos SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb;
10324 1.2 christos int testresult = 0, numreads;
10325 1.2 christos /* A 55 byte message */
10326 1.2 christos unsigned char *msg = (unsigned char *)
10327 1.2 christos "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123";
10328 1.2 christos size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5;
10329 1.2 christos size_t expectedreads;
10330 1.2 christos unsigned char *buf = NULL;
10331 1.2 christos ENGINE *e = NULL;
10332 1.2 christos
10333 1.2 christos if (idx != 6) {
10334 1.2 christos e = load_dasync();
10335 1.2 christos if (e == NULL)
10336 1.2 christos return 0;
10337 1.2 christos }
10338 1.2 christos
10339 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10340 1.2 christos TLS_client_method(), 0,
10341 1.2 christos TLS1_2_VERSION, &sctx, &cctx, cert,
10342 1.2 christos privkey)))
10343 1.2 christos goto end;
10344 1.2 christos
10345 1.2 christos if (idx == 6) {
10346 1.2 christos e = load_dasync();
10347 1.2 christos if (e == NULL)
10348 1.2 christos goto end;
10349 1.2 christos /* Now act like test 0 */
10350 1.2 christos idx = 0;
10351 1.2 christos }
10352 1.2 christos
10353 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10354 1.2 christos &clientssl, NULL, NULL)))
10355 1.2 christos goto end;
10356 1.2 christos
10357 1.2 christos if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA")))
10358 1.2 christos goto end;
10359 1.2 christos
10360 1.2 christos /* peera is always configured for pipelining, while peerb is not. */
10361 1.2 christos if (idx == 1) {
10362 1.2 christos peera = serverssl;
10363 1.2 christos peerb = clientssl;
10364 1.2 christos
10365 1.2 christos } else {
10366 1.2 christos peera = clientssl;
10367 1.2 christos peerb = serverssl;
10368 1.2 christos }
10369 1.2 christos
10370 1.2 christos if (idx == 5) {
10371 1.2 christos numpipes = 2;
10372 1.2 christos /* Maximum allowed fragment size */
10373 1.2 christos fragsize = SSL3_RT_MAX_PLAIN_LENGTH;
10374 1.2 christos msglen = fragsize * numpipes;
10375 1.2 christos msg = OPENSSL_malloc(msglen);
10376 1.2 christos if (!TEST_ptr(msg))
10377 1.2 christos goto end;
10378 1.2 christos if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0))
10379 1.2 christos goto end;
10380 1.2 christos } else if (idx == 4) {
10381 1.2 christos msglen = 55;
10382 1.2 christos } else {
10383 1.2 christos msglen = 50;
10384 1.2 christos }
10385 1.2 christos if (idx == 2)
10386 1.2 christos msglen -= 2; /* Send 2 less bytes */
10387 1.2 christos else if (idx == 3)
10388 1.2 christos msglen -= 12; /* Send 12 less bytes */
10389 1.2 christos
10390 1.2 christos buf = OPENSSL_malloc(msglen);
10391 1.2 christos if (!TEST_ptr(buf))
10392 1.2 christos goto end;
10393 1.2 christos
10394 1.2 christos if (idx == 5) {
10395 1.2 christos /*
10396 1.2 christos * Test that setting a split send fragment longer than the maximum
10397 1.2 christos * allowed fails
10398 1.2 christos */
10399 1.2 christos if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1)))
10400 1.2 christos goto end;
10401 1.2 christos }
10402 1.2 christos
10403 1.2 christos /*
10404 1.2 christos * In the normal case. We have 5 pipelines with 10 bytes per pipeline
10405 1.2 christos * (50 bytes in total). This is a ridiculously small number of bytes -
10406 1.2 christos * but sufficient for our purposes
10407 1.2 christos */
10408 1.2 christos if (!TEST_true(SSL_set_max_pipelines(peera, numpipes))
10409 1.2 christos || !TEST_true(SSL_set_split_send_fragment(peera, fragsize)))
10410 1.2 christos goto end;
10411 1.2 christos
10412 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10413 1.2 christos goto end;
10414 1.2 christos
10415 1.2 christos /* Write some data from peera to peerb */
10416 1.2 christos if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written))
10417 1.2 christos || !TEST_size_t_eq(written, msglen))
10418 1.2 christos goto end;
10419 1.2 christos
10420 1.2 christos /*
10421 1.2 christos * If the pipelining code worked, then we expect all |numpipes| pipelines to
10422 1.2 christos * have been used - except in test 3 where only |numpipes - 1| pipelines
10423 1.2 christos * will be used. This will result in |numpipes| records (|numpipes - 1| for
10424 1.2 christos * test 3) having been sent to peerb. Since peerb is not using read_ahead we
10425 1.2 christos * expect this to be read in |numpipes| or |numpipes - 1| separate
10426 1.2 christos * SSL_read_ex calls. In the case of test 4, there is then one additional
10427 1.2 christos * read for left over data that couldn't fit in the previous pipelines
10428 1.2 christos */
10429 1.2 christos for (offset = 0, numreads = 0;
10430 1.2 christos offset < msglen;
10431 1.2 christos offset += readbytes, numreads++) {
10432 1.2 christos if (!TEST_true(SSL_read_ex(peerb, buf + offset,
10433 1.2 christos msglen - offset, &readbytes)))
10434 1.2 christos goto end;
10435 1.2 christos }
10436 1.2 christos
10437 1.2 christos expectedreads = idx == 4 ? numpipes + 1
10438 1.2 christos : (idx == 3 ? numpipes - 1 : numpipes);
10439 1.2 christos if (!TEST_mem_eq(msg, msglen, buf, offset)
10440 1.2 christos || !TEST_int_eq(numreads, expectedreads))
10441 1.2 christos goto end;
10442 1.2 christos
10443 1.2 christos /*
10444 1.2 christos * Write some data from peerb to peera. We do this in up to |numpipes + 1|
10445 1.2 christos * chunks to exercise the read pipelining code on peera.
10446 1.2 christos */
10447 1.2 christos for (offset = 0; offset < msglen; offset += fragsize) {
10448 1.2 christos size_t sendlen = msglen - offset;
10449 1.2 christos
10450 1.2 christos if (sendlen > fragsize)
10451 1.2 christos sendlen = fragsize;
10452 1.2 christos if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written))
10453 1.2 christos || !TEST_size_t_eq(written, sendlen))
10454 1.2 christos goto end;
10455 1.2 christos }
10456 1.2 christos
10457 1.2 christos /*
10458 1.2 christos * The data was written in |numpipes|, |numpipes - 1| or |numpipes + 1|
10459 1.2 christos * separate chunks (depending on which test we are running). If the
10460 1.2 christos * pipelining is working then we expect peera to read up to numpipes chunks
10461 1.2 christos * and process them in parallel, giving back the complete result in a single
10462 1.2 christos * call to SSL_read_ex
10463 1.2 christos */
10464 1.2 christos if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes))
10465 1.2 christos || !TEST_size_t_le(readbytes, msglen))
10466 1.2 christos goto end;
10467 1.2 christos
10468 1.2 christos if (idx == 4) {
10469 1.2 christos size_t readbytes2;
10470 1.2 christos
10471 1.2 christos if (!TEST_true(SSL_read_ex(peera, buf + readbytes,
10472 1.2 christos msglen - readbytes, &readbytes2)))
10473 1.2 christos goto end;
10474 1.2 christos readbytes += readbytes2;
10475 1.2 christos if (!TEST_size_t_le(readbytes, msglen))
10476 1.2 christos goto end;
10477 1.2 christos }
10478 1.2 christos
10479 1.2 christos if (!TEST_mem_eq(msg, msglen, buf, readbytes))
10480 1.2 christos goto end;
10481 1.1 christos
10482 1.2 christos testresult = 1;
10483 1.2 christos end:
10484 1.2 christos SSL_free(serverssl);
10485 1.2 christos SSL_free(clientssl);
10486 1.2 christos SSL_CTX_free(sctx);
10487 1.2 christos SSL_CTX_free(cctx);
10488 1.2 christos if (e != NULL) {
10489 1.2 christos ENGINE_unregister_ciphers(e);
10490 1.2 christos ENGINE_finish(e);
10491 1.2 christos ENGINE_free(e);
10492 1.2 christos }
10493 1.2 christos OPENSSL_free(buf);
10494 1.2 christos if (fragsize == SSL3_RT_MAX_PLAIN_LENGTH)
10495 1.2 christos OPENSSL_free(msg);
10496 1.2 christos return testresult;
10497 1.2 christos }
10498 1.2 christos #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
10499 1.1 christos
10500 1.2 christos /*
10501 1.2 christos * Force a write retry during handshaking. We test various combinations of
10502 1.2 christos * scenarios. We test a large certificate message which will fill the buffering
10503 1.2 christos * BIO used in the handshake. We try with client auth on and off. Finally we
10504 1.2 christos * also try a BIO that indicates retry via a 0 return. BIO_write() is documented
10505 1.2 christos * to indicate retry via -1 - but sometimes BIOs don't do that.
10506 1.2 christos *
10507 1.2 christos * Test 0: Standard certificate message
10508 1.2 christos * Test 1: Large certificate message
10509 1.2 christos * Test 2: Standard cert, verify peer
10510 1.2 christos * Test 3: Large cert, verify peer
10511 1.2 christos * Test 4: Standard cert, BIO returns 0 on retry
10512 1.2 christos * Test 5: Large cert, BIO returns 0 on retry
10513 1.2 christos * Test 6: Standard cert, verify peer, BIO returns 0 on retry
10514 1.2 christos * Test 7: Large cert, verify peer, BIO returns 0 on retry
10515 1.2 christos * Test 8-15: Repeat of above with TLSv1.2
10516 1.2 christos */
10517 1.2 christos static int test_handshake_retry(int idx)
10518 1.1 christos {
10519 1.1 christos SSL_CTX *cctx = NULL, *sctx = NULL;
10520 1.1 christos SSL *clientssl = NULL, *serverssl = NULL;
10521 1.1 christos int testresult = 0;
10522 1.2 christos BIO *tmp = NULL, *bretry = BIO_new(bio_s_always_retry());
10523 1.2 christos int maxversion = 0;
10524 1.2 christos
10525 1.2 christos if (!TEST_ptr(bretry))
10526 1.2 christos goto end;
10527 1.2 christos
10528 1.2 christos #ifndef OPENSSL_NO_TLS1_2
10529 1.2 christos if ((idx & 8) == 8)
10530 1.2 christos maxversion = TLS1_2_VERSION;
10531 1.2 christos #else
10532 1.2 christos if ((idx & 8) == 8)
10533 1.2 christos return TEST_skip("No TLSv1.2");
10534 1.2 christos #endif
10535 1.2 christos
10536 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10537 1.2 christos TLS_client_method(), 0, maxversion,
10538 1.2 christos &sctx, &cctx, cert, privkey)))
10539 1.2 christos goto end;
10540 1.2 christos
10541 1.2 christos /*
10542 1.2 christos * Add a large amount of data to fill the buffering BIO used by the SSL
10543 1.2 christos * object
10544 1.2 christos */
10545 1.2 christos if ((idx & 1) == 1 && !add_large_cert_chain(sctx))
10546 1.2 christos goto end;
10547 1.2 christos
10548 1.2 christos /*
10549 1.2 christos * We don't actually configure a client cert, but neither do we fail if one
10550 1.2 christos * isn't present.
10551 1.2 christos */
10552 1.2 christos if ((idx & 2) == 2)
10553 1.2 christos SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
10554 1.2 christos
10555 1.2 christos if ((idx & 4) == 4)
10556 1.2 christos set_always_retry_err_val(0);
10557 1.2 christos
10558 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10559 1.2 christos &clientssl, NULL, NULL)))
10560 1.2 christos goto end;
10561 1.2 christos
10562 1.2 christos tmp = SSL_get_wbio(serverssl);
10563 1.2 christos if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
10564 1.2 christos tmp = NULL;
10565 1.2 christos goto end;
10566 1.2 christos }
10567 1.2 christos SSL_set0_wbio(serverssl, bretry);
10568 1.2 christos bretry = NULL;
10569 1.2 christos
10570 1.2 christos if (!TEST_int_eq(SSL_connect(clientssl), -1))
10571 1.2 christos goto end;
10572 1.2 christos
10573 1.2 christos if (!TEST_int_eq(SSL_accept(serverssl), -1)
10574 1.2 christos || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
10575 1.2 christos goto end;
10576 1.2 christos
10577 1.2 christos /* Restore a BIO that will let the write succeed */
10578 1.2 christos SSL_set0_wbio(serverssl, tmp);
10579 1.2 christos tmp = NULL;
10580 1.2 christos
10581 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10582 1.2 christos goto end;
10583 1.2 christos
10584 1.2 christos testresult = 1;
10585 1.2 christos end:
10586 1.2 christos SSL_free(serverssl);
10587 1.2 christos SSL_free(clientssl);
10588 1.2 christos SSL_CTX_free(sctx);
10589 1.2 christos SSL_CTX_free(cctx);
10590 1.2 christos BIO_free(bretry);
10591 1.2 christos BIO_free(tmp);
10592 1.2 christos set_always_retry_err_val(-1);
10593 1.2 christos return testresult;
10594 1.2 christos }
10595 1.2 christos
10596 1.2 christos struct resume_servername_cb_data {
10597 1.2 christos int i;
10598 1.2 christos SSL_CTX *cctx;
10599 1.2 christos SSL_CTX *sctx;
10600 1.2 christos SSL_SESSION *sess;
10601 1.2 christos int recurse;
10602 1.2 christos };
10603 1.2 christos
10604 1.2 christos /*
10605 1.2 christos * Servername callback. We use it here to run another complete handshake using
10606 1.2 christos * the same session - and mark the session as not_resuamble at the end
10607 1.2 christos */
10608 1.2 christos static int resume_servername_cb(SSL *s, int *ad, void *arg)
10609 1.2 christos {
10610 1.2 christos struct resume_servername_cb_data *cbdata = arg;
10611 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
10612 1.2 christos int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
10613 1.2 christos
10614 1.2 christos if (cbdata->recurse)
10615 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
10616 1.2 christos
10617 1.2 christos if ((cbdata->i % 3) != 1)
10618 1.2 christos return SSL_TLSEXT_ERR_OK;
10619 1.1 christos
10620 1.2 christos cbdata->recurse = 1;
10621 1.1 christos
10622 1.2 christos if (!TEST_true(create_ssl_objects(cbdata->sctx, cbdata->cctx, &serverssl,
10623 1.2 christos &clientssl, NULL, NULL))
10624 1.2 christos || !TEST_true(SSL_set_session(clientssl, cbdata->sess)))
10625 1.2 christos goto end;
10626 1.1 christos
10627 1.2 christos ERR_set_mark();
10628 1.2 christos /*
10629 1.2 christos * We expect this to fail - because the servername cb will fail. This will
10630 1.2 christos * mark the session as not_resumable.
10631 1.2 christos */
10632 1.2 christos if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
10633 1.2 christos ERR_clear_last_mark();
10634 1.2 christos goto end;
10635 1.1 christos }
10636 1.2 christos ERR_pop_to_mark();
10637 1.1 christos
10638 1.2 christos ret = SSL_TLSEXT_ERR_OK;
10639 1.2 christos end:
10640 1.2 christos SSL_free(serverssl);
10641 1.2 christos SSL_free(clientssl);
10642 1.2 christos cbdata->recurse = 0;
10643 1.2 christos return ret;
10644 1.2 christos }
10645 1.1 christos
10646 1.2 christos /*
10647 1.2 christos * Test multiple resumptions and cache size handling
10648 1.2 christos * Test 0: TLSv1.3 (max_early_data set)
10649 1.2 christos * Test 1: TLSv1.3 (SSL_OP_NO_TICKET set)
10650 1.2 christos * Test 2: TLSv1.3 (max_early_data and SSL_OP_NO_TICKET set)
10651 1.2 christos * Test 3: TLSv1.3 (SSL_OP_NO_TICKET, simultaneous resumes)
10652 1.2 christos * Test 4: TLSv1.2
10653 1.2 christos */
10654 1.2 christos static int test_multi_resume(int idx)
10655 1.2 christos {
10656 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
10657 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
10658 1.2 christos SSL_SESSION *sess = NULL;
10659 1.2 christos int max_version = TLS1_3_VERSION;
10660 1.2 christos int i, testresult = 0;
10661 1.2 christos struct resume_servername_cb_data cbdata;
10662 1.2 christos
10663 1.2 christos #if defined(OPENSSL_NO_TLS1_2)
10664 1.2 christos if (idx == 4)
10665 1.2 christos return TEST_skip("TLSv1.2 is disabled in this build");
10666 1.2 christos #else
10667 1.2 christos if (idx == 4)
10668 1.2 christos max_version = TLS1_2_VERSION;
10669 1.2 christos #endif
10670 1.2 christos #if defined(OSSL_NO_USABLE_TLS1_3)
10671 1.2 christos if (idx != 4)
10672 1.2 christos return TEST_skip("No usable TLSv1.3 in this build");
10673 1.2 christos #endif
10674 1.1 christos
10675 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10676 1.2 christos TLS_client_method(), TLS1_VERSION,
10677 1.2 christos max_version, &sctx, &cctx, cert,
10678 1.2 christos privkey)))
10679 1.1 christos goto end;
10680 1.2 christos
10681 1.2 christos /*
10682 1.2 christos * TLSv1.3 only uses a session cache if either max_early_data > 0 (used for
10683 1.2 christos * replay protection), or if SSL_OP_NO_TICKET is in use
10684 1.2 christos */
10685 1.2 christos if (idx == 0 || idx == 2) {
10686 1.2 christos if (!TEST_true(SSL_CTX_set_max_early_data(sctx, 1024)))
10687 1.2 christos goto end;
10688 1.1 christos }
10689 1.2 christos if (idx == 1 || idx == 2 || idx == 3)
10690 1.2 christos SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
10691 1.1 christos
10692 1.2 christos SSL_CTX_sess_set_cache_size(sctx, 5);
10693 1.1 christos
10694 1.2 christos if (idx == 3) {
10695 1.2 christos SSL_CTX_set_tlsext_servername_callback(sctx, resume_servername_cb);
10696 1.2 christos SSL_CTX_set_tlsext_servername_arg(sctx, &cbdata);
10697 1.2 christos cbdata.cctx = cctx;
10698 1.2 christos cbdata.sctx = sctx;
10699 1.2 christos cbdata.recurse = 0;
10700 1.2 christos }
10701 1.2 christos
10702 1.2 christos for (i = 0; i < 30; i++) {
10703 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
10704 1.2 christos NULL, NULL))
10705 1.2 christos || !TEST_true(SSL_set_session(clientssl, sess)))
10706 1.1 christos goto end;
10707 1.2 christos
10708 1.2 christos /*
10709 1.2 christos * Check simultaneous resumes. We pause the connection part way through
10710 1.2 christos * the handshake by (mis)using the servername_cb. The pause occurs after
10711 1.2 christos * session resumption has already occurred, but before any session
10712 1.2 christos * tickets have been issued. While paused we run another complete
10713 1.2 christos * handshake resuming the same session.
10714 1.2 christos */
10715 1.2 christos if (idx == 3) {
10716 1.2 christos cbdata.i = i;
10717 1.2 christos cbdata.sess = sess;
10718 1.1 christos }
10719 1.2 christos
10720 1.2 christos /*
10721 1.2 christos * Recreate a bug where dynamically changing the max_early_data value
10722 1.2 christos * can cause sessions in the session cache which cannot be deleted.
10723 1.2 christos */
10724 1.2 christos if ((idx == 0 || idx == 2) && (i % 3) == 2)
10725 1.2 christos SSL_set_max_early_data(serverssl, 0);
10726 1.2 christos
10727 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10728 1.1 christos goto end;
10729 1.2 christos
10730 1.2 christos if (sess == NULL || (idx == 0 && (i % 3) == 2)) {
10731 1.2 christos if (!TEST_false(SSL_session_reused(clientssl)))
10732 1.2 christos goto end;
10733 1.2 christos } else {
10734 1.2 christos if (!TEST_true(SSL_session_reused(clientssl)))
10735 1.2 christos goto end;
10736 1.2 christos }
10737 1.2 christos SSL_SESSION_free(sess);
10738 1.2 christos
10739 1.2 christos /* Do a full handshake, followed by two resumptions */
10740 1.2 christos if ((i % 3) == 2) {
10741 1.2 christos sess = NULL;
10742 1.2 christos } else {
10743 1.2 christos if (!TEST_ptr((sess = SSL_get1_session(clientssl))))
10744 1.2 christos goto end;
10745 1.1 christos }
10746 1.2 christos
10747 1.2 christos SSL_shutdown(clientssl);
10748 1.2 christos SSL_shutdown(serverssl);
10749 1.2 christos SSL_free(serverssl);
10750 1.2 christos SSL_free(clientssl);
10751 1.2 christos serverssl = clientssl = NULL;
10752 1.1 christos }
10753 1.1 christos
10754 1.2 christos /* We should never exceed the session cache size limit */
10755 1.2 christos if (!TEST_long_le(SSL_CTX_sess_number(sctx), 5))
10756 1.1 christos goto end;
10757 1.1 christos
10758 1.1 christos testresult = 1;
10759 1.1 christos end:
10760 1.1 christos SSL_free(serverssl);
10761 1.1 christos SSL_free(clientssl);
10762 1.1 christos SSL_CTX_free(sctx);
10763 1.1 christos SSL_CTX_free(cctx);
10764 1.2 christos SSL_SESSION_free(sess);
10765 1.1 christos return testresult;
10766 1.1 christos }
10767 1.1 christos
10768 1.2 christos static struct next_proto_st {
10769 1.2 christos int serverlen;
10770 1.2 christos unsigned char server[40];
10771 1.2 christos int clientlen;
10772 1.2 christos unsigned char client[40];
10773 1.2 christos int expected_ret;
10774 1.2 christos size_t selectedlen;
10775 1.2 christos unsigned char selected[40];
10776 1.2 christos } next_proto_tests[] = {
10777 1.2 christos {
10778 1.2 christos 4, { 3, 'a', 'b', 'c' },
10779 1.2 christos 4, { 3, 'a', 'b', 'c' },
10780 1.2 christos OPENSSL_NPN_NEGOTIATED,
10781 1.2 christos 3, { 'a', 'b', 'c' }
10782 1.2 christos },
10783 1.2 christos {
10784 1.2 christos 7, { 3, 'a', 'b', 'c', 2, 'a', 'b' },
10785 1.2 christos 4, { 3, 'a', 'b', 'c' },
10786 1.2 christos OPENSSL_NPN_NEGOTIATED,
10787 1.2 christos 3, { 'a', 'b', 'c' }
10788 1.2 christos },
10789 1.2 christos {
10790 1.2 christos 7, { 2, 'a', 'b', 3, 'a', 'b', 'c', },
10791 1.2 christos 4, { 3, 'a', 'b', 'c' },
10792 1.2 christos OPENSSL_NPN_NEGOTIATED,
10793 1.2 christos 3, { 'a', 'b', 'c' }
10794 1.2 christos },
10795 1.2 christos {
10796 1.2 christos 4, { 3, 'a', 'b', 'c' },
10797 1.2 christos 7, { 3, 'a', 'b', 'c', 2, 'a', 'b', },
10798 1.2 christos OPENSSL_NPN_NEGOTIATED,
10799 1.2 christos 3, { 'a', 'b', 'c' }
10800 1.2 christos },
10801 1.2 christos {
10802 1.2 christos 4, { 3, 'a', 'b', 'c' },
10803 1.2 christos 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10804 1.2 christos OPENSSL_NPN_NEGOTIATED,
10805 1.2 christos 3, { 'a', 'b', 'c' }
10806 1.2 christos },
10807 1.2 christos {
10808 1.2 christos 7, { 2, 'b', 'c', 3, 'a', 'b', 'c' },
10809 1.2 christos 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10810 1.2 christos OPENSSL_NPN_NEGOTIATED,
10811 1.2 christos 3, { 'a', 'b', 'c' }
10812 1.2 christos },
10813 1.2 christos {
10814 1.2 christos 10, { 2, 'b', 'c', 3, 'a', 'b', 'c', 2, 'a', 'b' },
10815 1.2 christos 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10816 1.2 christos OPENSSL_NPN_NEGOTIATED,
10817 1.2 christos 3, { 'a', 'b', 'c' }
10818 1.2 christos },
10819 1.2 christos {
10820 1.2 christos 4, { 3, 'b', 'c', 'd' },
10821 1.2 christos 4, { 3, 'a', 'b', 'c' },
10822 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10823 1.2 christos 3, { 'a', 'b', 'c' }
10824 1.2 christos },
10825 1.2 christos {
10826 1.2 christos 0, { 0 },
10827 1.2 christos 4, { 3, 'a', 'b', 'c' },
10828 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10829 1.2 christos 3, { 'a', 'b', 'c' }
10830 1.2 christos },
10831 1.2 christos {
10832 1.2 christos -1, { 0 },
10833 1.2 christos 4, { 3, 'a', 'b', 'c' },
10834 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10835 1.2 christos 3, { 'a', 'b', 'c' }
10836 1.2 christos },
10837 1.2 christos {
10838 1.2 christos 4, { 3, 'a', 'b', 'c' },
10839 1.2 christos 0, { 0 },
10840 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10841 1.2 christos 0, { 0 }
10842 1.2 christos },
10843 1.2 christos {
10844 1.2 christos 4, { 3, 'a', 'b', 'c' },
10845 1.2 christos -1, { 0 },
10846 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10847 1.2 christos 0, { 0 }
10848 1.2 christos },
10849 1.2 christos {
10850 1.2 christos 3, { 3, 'a', 'b', 'c' },
10851 1.2 christos 4, { 3, 'a', 'b', 'c' },
10852 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10853 1.2 christos 3, { 'a', 'b', 'c' }
10854 1.2 christos },
10855 1.2 christos {
10856 1.2 christos 4, { 3, 'a', 'b', 'c' },
10857 1.2 christos 3, { 3, 'a', 'b', 'c' },
10858 1.2 christos OPENSSL_NPN_NO_OVERLAP,
10859 1.2 christos 0, { 0 }
10860 1.2 christos }
10861 1.2 christos };
10862 1.1 christos
10863 1.2 christos static int test_select_next_proto(int idx)
10864 1.1 christos {
10865 1.2 christos struct next_proto_st *np = &next_proto_tests[idx];
10866 1.2 christos int ret = 0;
10867 1.2 christos unsigned char *out, *client, *server;
10868 1.2 christos unsigned char outlen;
10869 1.2 christos unsigned int clientlen, serverlen;
10870 1.2 christos
10871 1.2 christos if (np->clientlen == -1) {
10872 1.2 christos client = NULL;
10873 1.2 christos clientlen = 0;
10874 1.2 christos } else {
10875 1.2 christos client = np->client;
10876 1.2 christos clientlen = (unsigned int)np->clientlen;
10877 1.2 christos }
10878 1.2 christos if (np->serverlen == -1) {
10879 1.2 christos server = NULL;
10880 1.2 christos serverlen = 0;
10881 1.2 christos } else {
10882 1.2 christos server = np->server;
10883 1.2 christos serverlen = (unsigned int)np->serverlen;
10884 1.2 christos }
10885 1.1 christos
10886 1.2 christos if (!TEST_int_eq(SSL_select_next_proto(&out, &outlen, server, serverlen,
10887 1.2 christos client, clientlen),
10888 1.2 christos np->expected_ret))
10889 1.2 christos goto err;
10890 1.2 christos
10891 1.2 christos if (np->selectedlen == 0) {
10892 1.2 christos if (!TEST_ptr_null(out) || !TEST_uchar_eq(outlen, 0))
10893 1.2 christos goto err;
10894 1.2 christos } else {
10895 1.2 christos if (!TEST_mem_eq(out, outlen, np->selected, np->selectedlen))
10896 1.2 christos goto err;
10897 1.2 christos }
10898 1.1 christos
10899 1.2 christos ret = 1;
10900 1.2 christos err:
10901 1.2 christos return ret;
10902 1.1 christos }
10903 1.1 christos
10904 1.2 christos static const unsigned char fooprot[] = {3, 'f', 'o', 'o' };
10905 1.2 christos static const unsigned char barprot[] = {3, 'b', 'a', 'r' };
10906 1.1 christos
10907 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
10908 1.2 christos static int npn_advert_cb(SSL *ssl, const unsigned char **out,
10909 1.2 christos unsigned int *outlen, void *arg)
10910 1.1 christos {
10911 1.2 christos int *idx = (int *)arg;
10912 1.1 christos
10913 1.2 christos switch (*idx) {
10914 1.2 christos default:
10915 1.2 christos case 0:
10916 1.2 christos *out = fooprot;
10917 1.2 christos *outlen = sizeof(fooprot);
10918 1.2 christos return SSL_TLSEXT_ERR_OK;
10919 1.2 christos
10920 1.2 christos case 1:
10921 1.2 christos *outlen = 0;
10922 1.2 christos return SSL_TLSEXT_ERR_OK;
10923 1.1 christos
10924 1.2 christos case 2:
10925 1.2 christos return SSL_TLSEXT_ERR_NOACK;
10926 1.2 christos }
10927 1.1 christos }
10928 1.1 christos
10929 1.2 christos static int npn_select_cb(SSL *s, unsigned char **out, unsigned char *outlen,
10930 1.2 christos const unsigned char *in, unsigned int inlen, void *arg)
10931 1.1 christos {
10932 1.2 christos int *idx = (int *)arg;
10933 1.2 christos
10934 1.2 christos switch (*idx) {
10935 1.2 christos case 0:
10936 1.2 christos case 1:
10937 1.2 christos *out = (unsigned char *)(fooprot + 1);
10938 1.2 christos *outlen = *fooprot;
10939 1.2 christos return SSL_TLSEXT_ERR_OK;
10940 1.2 christos
10941 1.2 christos case 3:
10942 1.2 christos *out = (unsigned char *)(barprot + 1);
10943 1.2 christos *outlen = *barprot;
10944 1.2 christos return SSL_TLSEXT_ERR_OK;
10945 1.2 christos
10946 1.2 christos case 4:
10947 1.2 christos *outlen = 0;
10948 1.2 christos return SSL_TLSEXT_ERR_OK;
10949 1.1 christos
10950 1.2 christos default:
10951 1.2 christos case 2:
10952 1.1 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
10953 1.1 christos }
10954 1.1 christos }
10955 1.1 christos
10956 1.1 christos /*
10957 1.2 christos * Test the NPN callbacks
10958 1.2 christos * Test 0: advert = foo, select = foo
10959 1.2 christos * Test 1: advert = <empty>, select = foo
10960 1.2 christos * Test 2: no advert
10961 1.2 christos * Test 3: advert = foo, select = bar
10962 1.2 christos * Test 4: advert = foo, select = <empty> (should fail)
10963 1.1 christos */
10964 1.2 christos static int test_npn(int idx)
10965 1.1 christos {
10966 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
10967 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
10968 1.1 christos int testresult = 0;
10969 1.1 christos
10970 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10971 1.2 christos TLS_client_method(), 0, TLS1_2_VERSION,
10972 1.2 christos &sctx, &cctx, cert, privkey)))
10973 1.1 christos goto end;
10974 1.1 christos
10975 1.2 christos SSL_CTX_set_next_protos_advertised_cb(sctx, npn_advert_cb, &idx);
10976 1.2 christos SSL_CTX_set_next_proto_select_cb(cctx, npn_select_cb, &idx);
10977 1.1 christos
10978 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
10979 1.2 christos NULL)))
10980 1.1 christos goto end;
10981 1.1 christos
10982 1.2 christos if (idx == 4) {
10983 1.2 christos /* We don't allow empty selection of NPN, so this should fail */
10984 1.2 christos if (!TEST_false(create_ssl_connection(serverssl, clientssl,
10985 1.2 christos SSL_ERROR_NONE)))
10986 1.2 christos goto end;
10987 1.2 christos } else {
10988 1.2 christos const unsigned char *prot;
10989 1.2 christos unsigned int protlen;
10990 1.1 christos
10991 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
10992 1.2 christos SSL_ERROR_NONE)))
10993 1.2 christos goto end;
10994 1.1 christos
10995 1.2 christos SSL_get0_next_proto_negotiated(serverssl, &prot, &protlen);
10996 1.2 christos switch (idx) {
10997 1.2 christos case 0:
10998 1.2 christos case 1:
10999 1.2 christos if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
11000 1.2 christos goto end;
11001 1.2 christos break;
11002 1.2 christos case 2:
11003 1.2 christos if (!TEST_uint_eq(protlen, 0))
11004 1.2 christos goto end;
11005 1.2 christos break;
11006 1.2 christos case 3:
11007 1.2 christos if (!TEST_mem_eq(prot, protlen, barprot + 1, *barprot))
11008 1.2 christos goto end;
11009 1.2 christos break;
11010 1.2 christos default:
11011 1.2 christos TEST_error("Should not get here");
11012 1.1 christos goto end;
11013 1.1 christos }
11014 1.1 christos }
11015 1.1 christos
11016 1.2 christos testresult = 1;
11017 1.2 christos end:
11018 1.2 christos SSL_free(serverssl);
11019 1.2 christos SSL_free(clientssl);
11020 1.2 christos SSL_CTX_free(sctx);
11021 1.2 christos SSL_CTX_free(cctx);
11022 1.2 christos
11023 1.2 christos return testresult;
11024 1.2 christos }
11025 1.2 christos #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG) */
11026 1.2 christos
11027 1.2 christos static int alpn_select_cb2(SSL *ssl, const unsigned char **out,
11028 1.2 christos unsigned char *outlen, const unsigned char *in,
11029 1.2 christos unsigned int inlen, void *arg)
11030 1.2 christos {
11031 1.2 christos int *idx = (int *)arg;
11032 1.2 christos
11033 1.2 christos switch (*idx) {
11034 1.2 christos case 0:
11035 1.2 christos *out = (unsigned char *)(fooprot + 1);
11036 1.2 christos *outlen = *fooprot;
11037 1.2 christos return SSL_TLSEXT_ERR_OK;
11038 1.2 christos
11039 1.2 christos case 2:
11040 1.2 christos *out = (unsigned char *)(barprot + 1);
11041 1.2 christos *outlen = *barprot;
11042 1.2 christos return SSL_TLSEXT_ERR_OK;
11043 1.2 christos
11044 1.2 christos case 3:
11045 1.2 christos *outlen = 0;
11046 1.2 christos return SSL_TLSEXT_ERR_OK;
11047 1.2 christos
11048 1.2 christos default:
11049 1.2 christos case 1:
11050 1.2 christos return SSL_TLSEXT_ERR_ALERT_FATAL;
11051 1.1 christos }
11052 1.2 christos return 0;
11053 1.2 christos }
11054 1.2 christos
11055 1.2 christos /*
11056 1.2 christos * Test the ALPN callbacks
11057 1.2 christos * Test 0: client = foo, select = foo
11058 1.2 christos * Test 1: client = <empty>, select = none
11059 1.2 christos * Test 2: client = foo, select = bar (should fail)
11060 1.2 christos * Test 3: client = foo, select = <empty> (should fail)
11061 1.2 christos */
11062 1.2 christos static int test_alpn(int idx)
11063 1.2 christos {
11064 1.2 christos SSL_CTX *sctx = NULL, *cctx = NULL;
11065 1.2 christos SSL *serverssl = NULL, *clientssl = NULL;
11066 1.2 christos int testresult = 0;
11067 1.2 christos const unsigned char *prots = fooprot;
11068 1.2 christos unsigned int protslen = sizeof(fooprot);
11069 1.1 christos
11070 1.2 christos if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
11071 1.2 christos TLS_client_method(), 0, 0,
11072 1.2 christos &sctx, &cctx, cert, privkey)))
11073 1.1 christos goto end;
11074 1.1 christos
11075 1.2 christos SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb2, &idx);
11076 1.1 christos
11077 1.2 christos if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
11078 1.2 christos NULL)))
11079 1.1 christos goto end;
11080 1.2 christos
11081 1.2 christos if (idx == 1) {
11082 1.2 christos prots = NULL;
11083 1.2 christos protslen = 0;
11084 1.1 christos }
11085 1.1 christos
11086 1.2 christos /* SSL_set_alpn_protos returns 0 for success! */
11087 1.2 christos if (!TEST_false(SSL_set_alpn_protos(clientssl, prots, protslen)))
11088 1.1 christos goto end;
11089 1.1 christos
11090 1.2 christos if (idx == 2 || idx == 3) {
11091 1.2 christos /* We don't allow empty selection of NPN, so this should fail */
11092 1.2 christos if (!TEST_false(create_ssl_connection(serverssl, clientssl,
11093 1.2 christos SSL_ERROR_NONE)))
11094 1.2 christos goto end;
11095 1.2 christos } else {
11096 1.2 christos const unsigned char *prot;
11097 1.2 christos unsigned int protlen;
11098 1.2 christos
11099 1.2 christos if (!TEST_true(create_ssl_connection(serverssl, clientssl,
11100 1.2 christos SSL_ERROR_NONE)))
11101 1.2 christos goto end;
11102 1.2 christos
11103 1.2 christos SSL_get0_alpn_selected(clientssl, &prot, &protlen);
11104 1.2 christos switch (idx) {
11105 1.2 christos case 0:
11106 1.2 christos if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
11107 1.2 christos goto end;
11108 1.2 christos break;
11109 1.2 christos case 1:
11110 1.2 christos if (!TEST_uint_eq(protlen, 0))
11111 1.2 christos goto end;
11112 1.2 christos break;
11113 1.2 christos default:
11114 1.2 christos TEST_error("Should not get here");
11115 1.2 christos goto end;
11116 1.2 christos }
11117 1.1 christos }
11118 1.1 christos
11119 1.1 christos testresult = 1;
11120 1.2 christos end:
11121 1.1 christos SSL_free(serverssl);
11122 1.1 christos SSL_free(clientssl);
11123 1.1 christos SSL_CTX_free(sctx);
11124 1.1 christos SSL_CTX_free(cctx);
11125 1.2 christos
11126 1.1 christos return testresult;
11127 1.1 christos }
11128 1.1 christos
11129 1.2 christos OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
11130 1.2 christos
11131 1.2 christos int setup_tests(void)
11132 1.1 christos {
11133 1.2 christos char *modulename;
11134 1.2 christos char *configfile;
11135 1.2 christos
11136 1.2 christos libctx = OSSL_LIB_CTX_new();
11137 1.2 christos if (!TEST_ptr(libctx))
11138 1.2 christos return 0;
11139 1.2 christos
11140 1.2 christos defctxnull = OSSL_PROVIDER_load(NULL, "null");
11141 1.2 christos
11142 1.2 christos /*
11143 1.2 christos * Verify that the default and fips providers in the default libctx are not
11144 1.2 christos * available
11145 1.2 christos */
11146 1.2 christos if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
11147 1.2 christos || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
11148 1.2 christos return 0;
11149 1.1 christos
11150 1.2 christos if (!test_skip_common_options()) {
11151 1.2 christos TEST_error("Error parsing test options\n");
11152 1.2 christos return 0;
11153 1.1 christos }
11154 1.1 christos
11155 1.2 christos if (!TEST_ptr(certsdir = test_get_argument(0))
11156 1.2 christos || !TEST_ptr(srpvfile = test_get_argument(1))
11157 1.2 christos || !TEST_ptr(tmpfilename = test_get_argument(2))
11158 1.2 christos || !TEST_ptr(modulename = test_get_argument(3))
11159 1.2 christos || !TEST_ptr(configfile = test_get_argument(4))
11160 1.2 christos || !TEST_ptr(dhfile = test_get_argument(5)))
11161 1.2 christos return 0;
11162 1.2 christos
11163 1.2 christos if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
11164 1.2 christos return 0;
11165 1.2 christos
11166 1.2 christos /* Check we have the expected provider available */
11167 1.2 christos if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
11168 1.2 christos return 0;
11169 1.2 christos
11170 1.2 christos /* Check the default provider is not available */
11171 1.2 christos if (strcmp(modulename, "default") != 0
11172 1.2 christos && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
11173 1.2 christos return 0;
11174 1.2 christos
11175 1.2 christos if (strcmp(modulename, "fips") == 0)
11176 1.2 christos is_fips = 1;
11177 1.2 christos
11178 1.2 christos /*
11179 1.2 christos * We add, but don't load the test "tls-provider". We'll load it when we
11180 1.2 christos * need it.
11181 1.2 christos */
11182 1.2 christos if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
11183 1.2 christos tls_provider_init)))
11184 1.2 christos return 0;
11185 1.1 christos
11186 1.1 christos
11187 1.2 christos if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
11188 1.2 christos #ifdef OPENSSL_NO_CRYPTO_MDEBUG
11189 1.2 christos TEST_error("not supported in this build");
11190 1.2 christos return 0;
11191 1.2 christos #else
11192 1.2 christos int i, mcount, rcount, fcount;
11193 1.2 christos
11194 1.2 christos for (i = 0; i < 4; i++)
11195 1.2 christos test_export_key_mat(i);
11196 1.2 christos CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
11197 1.2 christos test_printf_stdout("malloc %d realloc %d free %d\n",
11198 1.2 christos mcount, rcount, fcount);
11199 1.2 christos return 1;
11200 1.2 christos #endif
11201 1.2 christos }
11202 1.1 christos
11203 1.2 christos cert = test_mk_file_path(certsdir, "servercert.pem");
11204 1.2 christos if (cert == NULL)
11205 1.2 christos goto err;
11206 1.2 christos
11207 1.2 christos privkey = test_mk_file_path(certsdir, "serverkey.pem");
11208 1.2 christos if (privkey == NULL)
11209 1.2 christos goto err;
11210 1.2 christos
11211 1.2 christos cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
11212 1.2 christos if (cert2 == NULL)
11213 1.2 christos goto err;
11214 1.2 christos
11215 1.2 christos privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
11216 1.2 christos if (privkey2 == NULL)
11217 1.2 christos goto err;
11218 1.2 christos
11219 1.2 christos cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
11220 1.2 christos if (cert1024 == NULL)
11221 1.2 christos goto err;
11222 1.2 christos
11223 1.2 christos privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
11224 1.2 christos if (privkey1024 == NULL)
11225 1.2 christos goto err;
11226 1.2 christos
11227 1.2 christos cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
11228 1.2 christos if (cert3072 == NULL)
11229 1.2 christos goto err;
11230 1.2 christos
11231 1.2 christos privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
11232 1.2 christos if (privkey3072 == NULL)
11233 1.2 christos goto err;
11234 1.2 christos
11235 1.2 christos cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
11236 1.2 christos if (cert4096 == NULL)
11237 1.2 christos goto err;
11238 1.2 christos
11239 1.2 christos privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
11240 1.2 christos if (privkey4096 == NULL)
11241 1.2 christos goto err;
11242 1.2 christos
11243 1.2 christos cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
11244 1.2 christos if (cert8192 == NULL)
11245 1.2 christos goto err;
11246 1.2 christos
11247 1.2 christos privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
11248 1.2 christos if (privkey8192 == NULL)
11249 1.2 christos goto err;
11250 1.2 christos
11251 1.2 christos #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
11252 1.2 christos # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
11253 1.2 christos ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
11254 1.2 christos ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
11255 1.2 christos # endif
11256 1.2 christos #endif
11257 1.1 christos ADD_TEST(test_large_message_tls);
11258 1.1 christos ADD_TEST(test_large_message_tls_read_ahead);
11259 1.1 christos #ifndef OPENSSL_NO_DTLS
11260 1.1 christos ADD_TEST(test_large_message_dtls);
11261 1.1 christos #endif
11262 1.2 christos ADD_ALL_TESTS(test_large_app_data, 28);
11263 1.2 christos ADD_TEST(test_cleanse_plaintext);
11264 1.1 christos #ifndef OPENSSL_NO_OCSP
11265 1.1 christos ADD_TEST(test_tlsext_status_type);
11266 1.1 christos #endif
11267 1.1 christos ADD_TEST(test_session_with_only_int_cache);
11268 1.1 christos ADD_TEST(test_session_with_only_ext_cache);
11269 1.1 christos ADD_TEST(test_session_with_both_cache);
11270 1.2 christos ADD_TEST(test_session_wo_ca_names);
11271 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11272 1.2 christos ADD_ALL_TESTS(test_stateful_tickets, 3);
11273 1.2 christos ADD_ALL_TESTS(test_stateless_tickets, 3);
11274 1.2 christos ADD_TEST(test_psk_tickets);
11275 1.2 christos ADD_ALL_TESTS(test_extra_tickets, 6);
11276 1.2 christos #endif
11277 1.1 christos ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
11278 1.1 christos ADD_TEST(test_ssl_bio_pop_next_bio);
11279 1.1 christos ADD_TEST(test_ssl_bio_pop_ssl_bio);
11280 1.1 christos ADD_TEST(test_ssl_bio_change_rbio);
11281 1.1 christos ADD_TEST(test_ssl_bio_change_wbio);
11282 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
11283 1.1 christos ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
11284 1.2 christos ADD_TEST(test_keylog);
11285 1.2 christos #endif
11286 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11287 1.2 christos ADD_TEST(test_keylog_no_master_key);
11288 1.2 christos #endif
11289 1.2 christos ADD_TEST(test_client_cert_verify_cb);
11290 1.2 christos ADD_TEST(test_ssl_build_cert_chain);
11291 1.2 christos ADD_TEST(test_ssl_ctx_build_cert_chain);
11292 1.2 christos #ifndef OPENSSL_NO_TLS1_2
11293 1.2 christos ADD_TEST(test_client_hello_cb);
11294 1.2 christos ADD_TEST(test_no_ems);
11295 1.2 christos ADD_TEST(test_ccs_change_cipher);
11296 1.2 christos #endif
11297 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11298 1.2 christos ADD_ALL_TESTS(test_early_data_read_write, 6);
11299 1.2 christos /*
11300 1.2 christos * We don't do replay tests for external PSK. Replay protection isn't used
11301 1.2 christos * in that scenario.
11302 1.2 christos */
11303 1.2 christos ADD_ALL_TESTS(test_early_data_replay, 2);
11304 1.2 christos ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3);
11305 1.2 christos ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3);
11306 1.2 christos ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3);
11307 1.2 christos ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3);
11308 1.2 christos ADD_ALL_TESTS(test_early_data_not_sent, 3);
11309 1.2 christos ADD_ALL_TESTS(test_early_data_psk, 8);
11310 1.2 christos ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
11311 1.2 christos ADD_ALL_TESTS(test_early_data_not_expected, 3);
11312 1.2 christos # ifndef OPENSSL_NO_TLS1_2
11313 1.2 christos ADD_ALL_TESTS(test_early_data_tls1_2, 3);
11314 1.2 christos # endif
11315 1.2 christos #endif
11316 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11317 1.2 christos ADD_ALL_TESTS(test_set_ciphersuite, 10);
11318 1.2 christos ADD_TEST(test_ciphersuite_change);
11319 1.2 christos ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
11320 1.2 christos # ifdef OPENSSL_NO_PSK
11321 1.2 christos ADD_ALL_TESTS(test_tls13_psk, 1);
11322 1.2 christos # else
11323 1.2 christos ADD_ALL_TESTS(test_tls13_psk, 4);
11324 1.2 christos # endif /* OPENSSL_NO_PSK */
11325 1.2 christos # ifndef OPENSSL_NO_TLS1_2
11326 1.2 christos /* Test with both TLSv1.3 and 1.2 versions */
11327 1.2 christos ADD_ALL_TESTS(test_key_exchange, 14);
11328 1.2 christos # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
11329 1.2 christos ADD_ALL_TESTS(test_negotiated_group,
11330 1.2 christos 4 * (OSSL_NELEM(ecdhe_kexch_groups)
11331 1.2 christos + OSSL_NELEM(ffdhe_kexch_groups)));
11332 1.2 christos # endif
11333 1.2 christos # else
11334 1.2 christos /* Test with only TLSv1.3 versions */
11335 1.2 christos ADD_ALL_TESTS(test_key_exchange, 12);
11336 1.2 christos # endif
11337 1.2 christos ADD_ALL_TESTS(test_custom_exts, 6);
11338 1.2 christos ADD_TEST(test_stateless);
11339 1.2 christos ADD_TEST(test_pha_key_update);
11340 1.2 christos #else
11341 1.2 christos ADD_ALL_TESTS(test_custom_exts, 3);
11342 1.2 christos #endif
11343 1.2 christos ADD_ALL_TESTS(test_export_key_mat, 6);
11344 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11345 1.2 christos ADD_ALL_TESTS(test_export_key_mat_early, 3);
11346 1.2 christos ADD_TEST(test_key_update);
11347 1.2 christos ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
11348 1.2 christos ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
11349 1.2 christos ADD_ALL_TESTS(test_key_update_local_in_write, 2);
11350 1.2 christos ADD_ALL_TESTS(test_key_update_local_in_read, 2);
11351 1.2 christos #endif
11352 1.2 christos ADD_ALL_TESTS(test_ssl_clear, 2);
11353 1.2 christos ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
11354 1.2 christos #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
11355 1.2 christos ADD_ALL_TESTS(test_srp, 6);
11356 1.2 christos #endif
11357 1.2 christos ADD_ALL_TESTS(test_info_callback, 6);
11358 1.2 christos ADD_ALL_TESTS(test_ssl_pending, 2);
11359 1.2 christos ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
11360 1.2 christos ADD_ALL_TESTS(test_ticket_callbacks, 20);
11361 1.2 christos ADD_ALL_TESTS(test_shutdown, 7);
11362 1.2 christos ADD_ALL_TESTS(test_incorrect_shutdown, 2);
11363 1.2 christos ADD_ALL_TESTS(test_cert_cb, 6);
11364 1.2 christos ADD_ALL_TESTS(test_client_cert_cb, 2);
11365 1.2 christos ADD_ALL_TESTS(test_ca_names, 3);
11366 1.2 christos #ifndef OPENSSL_NO_TLS1_2
11367 1.2 christos ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
11368 1.2 christos #endif
11369 1.2 christos ADD_ALL_TESTS(test_servername, 10);
11370 1.2 christos #if !defined(OPENSSL_NO_EC) \
11371 1.2 christos && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
11372 1.2 christos ADD_ALL_TESTS(test_sigalgs_available, 6);
11373 1.2 christos #endif
11374 1.2 christos #ifndef OPENSSL_NO_TLS1_3
11375 1.2 christos ADD_ALL_TESTS(test_pluggable_group, 2);
11376 1.2 christos #endif
11377 1.2 christos #ifndef OPENSSL_NO_TLS1_2
11378 1.2 christos ADD_TEST(test_ssl_dup);
11379 1.2 christos # ifndef OPENSSL_NO_DH
11380 1.2 christos ADD_ALL_TESTS(test_set_tmp_dh, 11);
11381 1.2 christos ADD_ALL_TESTS(test_dh_auto, 7);
11382 1.2 christos # endif
11383 1.2 christos #endif
11384 1.2 christos #ifndef OSSL_NO_USABLE_TLS1_3
11385 1.2 christos ADD_TEST(test_sni_tls13);
11386 1.2 christos ADD_ALL_TESTS(test_ticket_lifetime, 2);
11387 1.2 christos #endif
11388 1.2 christos ADD_TEST(test_inherit_verify_param);
11389 1.2 christos ADD_TEST(test_set_alpn);
11390 1.2 christos ADD_TEST(test_set_verify_cert_store_ssl_ctx);
11391 1.2 christos ADD_TEST(test_set_verify_cert_store_ssl);
11392 1.2 christos ADD_ALL_TESTS(test_session_timeout, 1);
11393 1.2 christos #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
11394 1.2 christos ADD_ALL_TESTS(test_session_cache_overflow, 4);
11395 1.2 christos #endif
11396 1.2 christos ADD_TEST(test_load_dhfile);
11397 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
11398 1.2 christos ADD_ALL_TESTS(test_serverinfo_custom, 4);
11399 1.2 christos #endif
11400 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
11401 1.2 christos ADD_ALL_TESTS(test_pipelining, 7);
11402 1.2 christos #endif
11403 1.2 christos ADD_ALL_TESTS(test_handshake_retry, 16);
11404 1.2 christos ADD_ALL_TESTS(test_multi_resume, 5);
11405 1.2 christos ADD_ALL_TESTS(test_select_next_proto, OSSL_NELEM(next_proto_tests));
11406 1.2 christos #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
11407 1.2 christos ADD_ALL_TESTS(test_npn, 5);
11408 1.2 christos #endif
11409 1.2 christos ADD_ALL_TESTS(test_alpn, 4);
11410 1.2 christos return 1;
11411 1.1 christos
11412 1.2 christos err:
11413 1.2 christos OPENSSL_free(cert);
11414 1.2 christos OPENSSL_free(privkey);
11415 1.2 christos OPENSSL_free(cert2);
11416 1.2 christos OPENSSL_free(privkey2);
11417 1.2 christos return 0;
11418 1.2 christos }
11419 1.1 christos
11420 1.2 christos void cleanup_tests(void)
11421 1.2 christos {
11422 1.2 christos # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
11423 1.2 christos EVP_PKEY_free(tmp_dh_params);
11424 1.2 christos #endif
11425 1.2 christos OPENSSL_free(cert);
11426 1.2 christos OPENSSL_free(privkey);
11427 1.2 christos OPENSSL_free(cert2);
11428 1.2 christos OPENSSL_free(privkey2);
11429 1.2 christos OPENSSL_free(cert1024);
11430 1.2 christos OPENSSL_free(privkey1024);
11431 1.2 christos OPENSSL_free(cert3072);
11432 1.2 christos OPENSSL_free(privkey3072);
11433 1.2 christos OPENSSL_free(cert4096);
11434 1.2 christos OPENSSL_free(privkey4096);
11435 1.2 christos OPENSSL_free(cert8192);
11436 1.2 christos OPENSSL_free(privkey8192);
11437 1.1 christos bio_s_mempacket_test_free();
11438 1.2 christos bio_s_always_retry_free();
11439 1.2 christos OSSL_PROVIDER_unload(defctxnull);
11440 1.2 christos OSSL_LIB_CTX_free(libctx);
11441 1.1 christos }
11442