xref: /freebsd/crypto/openssl/test/sslapitest.c (revision 0d0c8621fd181e507f0fb50ffcca606faf66a8c2)
1e0c4386eSCy Schubert /*
2*0d0c8621SEnji Cooper  * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert  *
4e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert  */
9e0c4386eSCy Schubert 
10e0c4386eSCy Schubert /*
11e0c4386eSCy Schubert  * We need access to the deprecated low level HMAC APIs for legacy purposes
12e0c4386eSCy Schubert  * when the deprecated calls are not hidden
13e0c4386eSCy Schubert  */
14e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
15e0c4386eSCy Schubert # define OPENSSL_SUPPRESS_DEPRECATED
16e0c4386eSCy Schubert #endif
17e0c4386eSCy Schubert 
18e0c4386eSCy Schubert #include <stdio.h>
19e0c4386eSCy Schubert #include <string.h>
20e0c4386eSCy Schubert 
21e0c4386eSCy Schubert #include <openssl/opensslconf.h>
22e0c4386eSCy Schubert #include <openssl/bio.h>
23e0c4386eSCy Schubert #include <openssl/crypto.h>
24e0c4386eSCy Schubert #include <openssl/ssl.h>
25e0c4386eSCy Schubert #include <openssl/ocsp.h>
26e0c4386eSCy Schubert #include <openssl/srp.h>
27e0c4386eSCy Schubert #include <openssl/txt_db.h>
28e0c4386eSCy Schubert #include <openssl/aes.h>
29e0c4386eSCy Schubert #include <openssl/rand.h>
30e0c4386eSCy Schubert #include <openssl/core_names.h>
31e0c4386eSCy Schubert #include <openssl/core_dispatch.h>
32e0c4386eSCy Schubert #include <openssl/provider.h>
33e0c4386eSCy Schubert #include <openssl/param_build.h>
34e0c4386eSCy Schubert #include <openssl/x509v3.h>
35e0c4386eSCy Schubert #include <openssl/dh.h>
36e0c4386eSCy Schubert #include <openssl/engine.h>
37e0c4386eSCy Schubert 
38e0c4386eSCy Schubert #include "helpers/ssltestlib.h"
39e0c4386eSCy Schubert #include "testutil.h"
40e0c4386eSCy Schubert #include "testutil/output.h"
41e0c4386eSCy Schubert #include "internal/nelem.h"
42e0c4386eSCy Schubert #include "internal/ktls.h"
43e0c4386eSCy Schubert #include "../ssl/ssl_local.h"
44e0c4386eSCy Schubert #include "filterprov.h"
45e0c4386eSCy Schubert 
46e0c4386eSCy Schubert #undef OSSL_NO_USABLE_TLS1_3
47e0c4386eSCy Schubert #if defined(OPENSSL_NO_TLS1_3) \
48e0c4386eSCy Schubert     || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
49e0c4386eSCy Schubert /*
50e0c4386eSCy Schubert  * If we don't have ec or dh then there are no built-in groups that are usable
51e0c4386eSCy Schubert  * with TLSv1.3
52e0c4386eSCy Schubert  */
53e0c4386eSCy Schubert # define OSSL_NO_USABLE_TLS1_3
54e0c4386eSCy Schubert #endif
55e0c4386eSCy Schubert 
56e0c4386eSCy Schubert /* Defined in tls-provider.c */
57e0c4386eSCy Schubert int tls_provider_init(const OSSL_CORE_HANDLE *handle,
58e0c4386eSCy Schubert                       const OSSL_DISPATCH *in,
59e0c4386eSCy Schubert                       const OSSL_DISPATCH **out,
60e0c4386eSCy Schubert                       void **provctx);
61e0c4386eSCy Schubert 
62e0c4386eSCy Schubert static OSSL_LIB_CTX *libctx = NULL;
63e0c4386eSCy Schubert static OSSL_PROVIDER *defctxnull = NULL;
64e0c4386eSCy Schubert 
65e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
66e0c4386eSCy Schubert 
67e0c4386eSCy Schubert static SSL_SESSION *clientpsk = NULL;
68e0c4386eSCy Schubert static SSL_SESSION *serverpsk = NULL;
69e0c4386eSCy Schubert static const char *pskid = "Identity";
70e0c4386eSCy Schubert static const char *srvid;
71e0c4386eSCy Schubert 
72e0c4386eSCy Schubert static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
73e0c4386eSCy Schubert                           size_t *idlen, SSL_SESSION **sess);
74e0c4386eSCy Schubert static int find_session_cb(SSL *ssl, const unsigned char *identity,
75e0c4386eSCy Schubert                            size_t identity_len, SSL_SESSION **sess);
76e0c4386eSCy Schubert 
77e0c4386eSCy Schubert static int use_session_cb_cnt = 0;
78e0c4386eSCy Schubert static int find_session_cb_cnt = 0;
79e0c4386eSCy Schubert 
80e0c4386eSCy Schubert static SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize);
81e0c4386eSCy Schubert #endif
82e0c4386eSCy Schubert 
83e0c4386eSCy Schubert static char *certsdir = NULL;
84e0c4386eSCy Schubert static char *cert = NULL;
85e0c4386eSCy Schubert static char *privkey = NULL;
86e0c4386eSCy Schubert static char *cert2 = NULL;
87e0c4386eSCy Schubert static char *privkey2 = NULL;
88e0c4386eSCy Schubert static char *cert1024 = NULL;
89e0c4386eSCy Schubert static char *privkey1024 = NULL;
90e0c4386eSCy Schubert static char *cert3072 = NULL;
91e0c4386eSCy Schubert static char *privkey3072 = NULL;
92e0c4386eSCy Schubert static char *cert4096 = NULL;
93e0c4386eSCy Schubert static char *privkey4096 = NULL;
94e0c4386eSCy Schubert static char *cert8192 = NULL;
95e0c4386eSCy Schubert static char *privkey8192 = NULL;
96e0c4386eSCy Schubert static char *srpvfile = NULL;
97e0c4386eSCy Schubert static char *tmpfilename = NULL;
98e0c4386eSCy Schubert static char *dhfile = NULL;
99e0c4386eSCy Schubert 
100e0c4386eSCy Schubert static int is_fips = 0;
101e0c4386eSCy Schubert 
102e0c4386eSCy Schubert #define LOG_BUFFER_SIZE 2048
103e0c4386eSCy Schubert static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
104e0c4386eSCy Schubert static size_t server_log_buffer_index = 0;
105e0c4386eSCy Schubert static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
106e0c4386eSCy Schubert static size_t client_log_buffer_index = 0;
107e0c4386eSCy Schubert static int error_writing_log = 0;
108e0c4386eSCy Schubert 
109e0c4386eSCy Schubert #ifndef OPENSSL_NO_OCSP
110e0c4386eSCy Schubert static const unsigned char orespder[] = "Dummy OCSP Response";
111e0c4386eSCy Schubert static int ocsp_server_called = 0;
112e0c4386eSCy Schubert static int ocsp_client_called = 0;
113e0c4386eSCy Schubert 
114e0c4386eSCy Schubert static int cdummyarg = 1;
115e0c4386eSCy Schubert static X509 *ocspcert = NULL;
116e0c4386eSCy Schubert #endif
117e0c4386eSCy Schubert 
118e0c4386eSCy Schubert #define NUM_EXTRA_CERTS 40
119e0c4386eSCy Schubert #define CLIENT_VERSION_LEN      2
120e0c4386eSCy Schubert 
121e0c4386eSCy Schubert /*
122e0c4386eSCy Schubert  * This structure is used to validate that the correct number of log messages
123e0c4386eSCy Schubert  * of various types are emitted when emitting secret logs.
124e0c4386eSCy Schubert  */
125e0c4386eSCy Schubert struct sslapitest_log_counts {
126e0c4386eSCy Schubert     unsigned int rsa_key_exchange_count;
127e0c4386eSCy Schubert     unsigned int master_secret_count;
128e0c4386eSCy Schubert     unsigned int client_early_secret_count;
129e0c4386eSCy Schubert     unsigned int client_handshake_secret_count;
130e0c4386eSCy Schubert     unsigned int server_handshake_secret_count;
131e0c4386eSCy Schubert     unsigned int client_application_secret_count;
132e0c4386eSCy Schubert     unsigned int server_application_secret_count;
133e0c4386eSCy Schubert     unsigned int early_exporter_secret_count;
134e0c4386eSCy Schubert     unsigned int exporter_secret_count;
135e0c4386eSCy Schubert };
136e0c4386eSCy Schubert 
137e0c4386eSCy Schubert 
hostname_cb(SSL * s,int * al,void * arg)138e0c4386eSCy Schubert static int hostname_cb(SSL *s, int *al, void *arg)
139e0c4386eSCy Schubert {
140e0c4386eSCy Schubert     const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
141e0c4386eSCy Schubert 
142e0c4386eSCy Schubert     if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
143e0c4386eSCy Schubert                              || strcmp(hostname, "altgoodhost") == 0))
144e0c4386eSCy Schubert         return  SSL_TLSEXT_ERR_OK;
145e0c4386eSCy Schubert 
146e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_NOACK;
147e0c4386eSCy Schubert }
148e0c4386eSCy Schubert 
client_keylog_callback(const SSL * ssl,const char * line)149e0c4386eSCy Schubert static void client_keylog_callback(const SSL *ssl, const char *line)
150e0c4386eSCy Schubert {
151e0c4386eSCy Schubert     int line_length = strlen(line);
152e0c4386eSCy Schubert 
153e0c4386eSCy Schubert     /* If the log doesn't fit, error out. */
154e0c4386eSCy Schubert     if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
155e0c4386eSCy Schubert         TEST_info("Client log too full");
156e0c4386eSCy Schubert         error_writing_log = 1;
157e0c4386eSCy Schubert         return;
158e0c4386eSCy Schubert     }
159e0c4386eSCy Schubert 
160e0c4386eSCy Schubert     strcat(client_log_buffer, line);
161e0c4386eSCy Schubert     client_log_buffer_index += line_length;
162e0c4386eSCy Schubert     client_log_buffer[client_log_buffer_index++] = '\n';
163e0c4386eSCy Schubert }
164e0c4386eSCy Schubert 
server_keylog_callback(const SSL * ssl,const char * line)165e0c4386eSCy Schubert static void server_keylog_callback(const SSL *ssl, const char *line)
166e0c4386eSCy Schubert {
167e0c4386eSCy Schubert     int line_length = strlen(line);
168e0c4386eSCy Schubert 
169e0c4386eSCy Schubert     /* If the log doesn't fit, error out. */
170e0c4386eSCy Schubert     if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
171e0c4386eSCy Schubert         TEST_info("Server log too full");
172e0c4386eSCy Schubert         error_writing_log = 1;
173e0c4386eSCy Schubert         return;
174e0c4386eSCy Schubert     }
175e0c4386eSCy Schubert 
176e0c4386eSCy Schubert     strcat(server_log_buffer, line);
177e0c4386eSCy Schubert     server_log_buffer_index += line_length;
178e0c4386eSCy Schubert     server_log_buffer[server_log_buffer_index++] = '\n';
179e0c4386eSCy Schubert }
180e0c4386eSCy Schubert 
compare_hex_encoded_buffer(const char * hex_encoded,size_t hex_length,const uint8_t * raw,size_t raw_length)181e0c4386eSCy Schubert static int compare_hex_encoded_buffer(const char *hex_encoded,
182e0c4386eSCy Schubert                                       size_t hex_length,
183e0c4386eSCy Schubert                                       const uint8_t *raw,
184e0c4386eSCy Schubert                                       size_t raw_length)
185e0c4386eSCy Schubert {
186e0c4386eSCy Schubert     size_t i, j;
187e0c4386eSCy Schubert     char hexed[3];
188e0c4386eSCy Schubert 
189e0c4386eSCy Schubert     if (!TEST_size_t_eq(raw_length * 2, hex_length))
190e0c4386eSCy Schubert         return 1;
191e0c4386eSCy Schubert 
192e0c4386eSCy Schubert     for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
193*0d0c8621SEnji Cooper         BIO_snprintf(hexed, sizeof(hexed), "%02x", raw[i]);
194e0c4386eSCy Schubert         if (!TEST_int_eq(hexed[0], hex_encoded[j])
195e0c4386eSCy Schubert                 || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
196e0c4386eSCy Schubert             return 1;
197e0c4386eSCy Schubert     }
198e0c4386eSCy Schubert 
199e0c4386eSCy Schubert     return 0;
200e0c4386eSCy Schubert }
201e0c4386eSCy Schubert 
test_keylog_output(char * buffer,const SSL * ssl,const SSL_SESSION * session,struct sslapitest_log_counts * expected)202e0c4386eSCy Schubert static int test_keylog_output(char *buffer, const SSL *ssl,
203e0c4386eSCy Schubert                               const SSL_SESSION *session,
204e0c4386eSCy Schubert                               struct sslapitest_log_counts *expected)
205e0c4386eSCy Schubert {
206e0c4386eSCy Schubert     char *token = NULL;
207e0c4386eSCy Schubert     unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
208e0c4386eSCy Schubert     size_t client_random_size = SSL3_RANDOM_SIZE;
209e0c4386eSCy Schubert     unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
210e0c4386eSCy Schubert     size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
211e0c4386eSCy Schubert     unsigned int rsa_key_exchange_count = 0;
212e0c4386eSCy Schubert     unsigned int master_secret_count = 0;
213e0c4386eSCy Schubert     unsigned int client_early_secret_count = 0;
214e0c4386eSCy Schubert     unsigned int client_handshake_secret_count = 0;
215e0c4386eSCy Schubert     unsigned int server_handshake_secret_count = 0;
216e0c4386eSCy Schubert     unsigned int client_application_secret_count = 0;
217e0c4386eSCy Schubert     unsigned int server_application_secret_count = 0;
218e0c4386eSCy Schubert     unsigned int early_exporter_secret_count = 0;
219e0c4386eSCy Schubert     unsigned int exporter_secret_count = 0;
220e0c4386eSCy Schubert 
221e0c4386eSCy Schubert     for (token = strtok(buffer, " \n"); token != NULL;
222e0c4386eSCy Schubert          token = strtok(NULL, " \n")) {
223e0c4386eSCy Schubert         if (strcmp(token, "RSA") == 0) {
224e0c4386eSCy Schubert             /*
225e0c4386eSCy Schubert              * Premaster secret. Tokens should be: 16 ASCII bytes of
226e0c4386eSCy Schubert              * hex-encoded encrypted secret, then the hex-encoded pre-master
227e0c4386eSCy Schubert              * secret.
228e0c4386eSCy Schubert              */
229e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
230e0c4386eSCy Schubert                 return 0;
231e0c4386eSCy Schubert             if (!TEST_size_t_eq(strlen(token), 16))
232e0c4386eSCy Schubert                 return 0;
233e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
234e0c4386eSCy Schubert                 return 0;
235e0c4386eSCy Schubert             /*
236e0c4386eSCy Schubert              * We can't sensibly check the log because the premaster secret is
237e0c4386eSCy Schubert              * transient, and OpenSSL doesn't keep hold of it once the master
238e0c4386eSCy Schubert              * secret is generated.
239e0c4386eSCy Schubert              */
240e0c4386eSCy Schubert             rsa_key_exchange_count++;
241e0c4386eSCy Schubert         } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
242e0c4386eSCy Schubert             /*
243e0c4386eSCy Schubert              * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
244e0c4386eSCy Schubert              * client random, then the hex-encoded master secret.
245e0c4386eSCy Schubert              */
246e0c4386eSCy Schubert             client_random_size = SSL_get_client_random(ssl,
247e0c4386eSCy Schubert                                                        actual_client_random,
248e0c4386eSCy Schubert                                                        SSL3_RANDOM_SIZE);
249e0c4386eSCy Schubert             if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
250e0c4386eSCy Schubert                 return 0;
251e0c4386eSCy Schubert 
252e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
253e0c4386eSCy Schubert                 return 0;
254e0c4386eSCy Schubert             if (!TEST_size_t_eq(strlen(token), 64))
255e0c4386eSCy Schubert                 return 0;
256e0c4386eSCy Schubert             if (!TEST_false(compare_hex_encoded_buffer(token, 64,
257e0c4386eSCy Schubert                                                        actual_client_random,
258e0c4386eSCy Schubert                                                        client_random_size)))
259e0c4386eSCy Schubert                 return 0;
260e0c4386eSCy Schubert 
261e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
262e0c4386eSCy Schubert                 return 0;
263e0c4386eSCy Schubert             master_key_size = SSL_SESSION_get_master_key(session,
264e0c4386eSCy Schubert                                                          actual_master_key,
265e0c4386eSCy Schubert                                                          master_key_size);
266e0c4386eSCy Schubert             if (!TEST_size_t_ne(master_key_size, 0))
267e0c4386eSCy Schubert                 return 0;
268e0c4386eSCy Schubert             if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
269e0c4386eSCy Schubert                                                        actual_master_key,
270e0c4386eSCy Schubert                                                        master_key_size)))
271e0c4386eSCy Schubert                 return 0;
272e0c4386eSCy Schubert             master_secret_count++;
273e0c4386eSCy Schubert         } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
274e0c4386eSCy Schubert                     || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
275e0c4386eSCy Schubert                     || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
276e0c4386eSCy Schubert                     || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
277e0c4386eSCy Schubert                     || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
278e0c4386eSCy Schubert                     || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
279e0c4386eSCy Schubert                     || strcmp(token, "EXPORTER_SECRET") == 0) {
280e0c4386eSCy Schubert             /*
281e0c4386eSCy Schubert              * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
282e0c4386eSCy Schubert              * client random, and then the hex-encoded secret. In this case,
283e0c4386eSCy Schubert              * we treat all of these secrets identically and then just
284e0c4386eSCy Schubert              * distinguish between them when counting what we saw.
285e0c4386eSCy Schubert              */
286e0c4386eSCy Schubert             if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
287e0c4386eSCy Schubert                 client_early_secret_count++;
288e0c4386eSCy Schubert             else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
289e0c4386eSCy Schubert                 client_handshake_secret_count++;
290e0c4386eSCy Schubert             else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
291e0c4386eSCy Schubert                 server_handshake_secret_count++;
292e0c4386eSCy Schubert             else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
293e0c4386eSCy Schubert                 client_application_secret_count++;
294e0c4386eSCy Schubert             else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
295e0c4386eSCy Schubert                 server_application_secret_count++;
296e0c4386eSCy Schubert             else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
297e0c4386eSCy Schubert                 early_exporter_secret_count++;
298e0c4386eSCy Schubert             else if (strcmp(token, "EXPORTER_SECRET") == 0)
299e0c4386eSCy Schubert                 exporter_secret_count++;
300e0c4386eSCy Schubert 
301e0c4386eSCy Schubert             client_random_size = SSL_get_client_random(ssl,
302e0c4386eSCy Schubert                                                        actual_client_random,
303e0c4386eSCy Schubert                                                        SSL3_RANDOM_SIZE);
304e0c4386eSCy Schubert             if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
305e0c4386eSCy Schubert                 return 0;
306e0c4386eSCy Schubert 
307e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
308e0c4386eSCy Schubert                 return 0;
309e0c4386eSCy Schubert             if (!TEST_size_t_eq(strlen(token), 64))
310e0c4386eSCy Schubert                 return 0;
311e0c4386eSCy Schubert             if (!TEST_false(compare_hex_encoded_buffer(token, 64,
312e0c4386eSCy Schubert                                                        actual_client_random,
313e0c4386eSCy Schubert                                                        client_random_size)))
314e0c4386eSCy Schubert                 return 0;
315e0c4386eSCy Schubert 
316e0c4386eSCy Schubert             if (!TEST_ptr(token = strtok(NULL, " \n")))
317e0c4386eSCy Schubert                 return 0;
318e0c4386eSCy Schubert         } else {
319e0c4386eSCy Schubert             TEST_info("Unexpected token %s\n", token);
320e0c4386eSCy Schubert             return 0;
321e0c4386eSCy Schubert         }
322e0c4386eSCy Schubert     }
323e0c4386eSCy Schubert 
324e0c4386eSCy Schubert     /* Got what we expected? */
325e0c4386eSCy Schubert     if (!TEST_size_t_eq(rsa_key_exchange_count,
326e0c4386eSCy Schubert                         expected->rsa_key_exchange_count)
327e0c4386eSCy Schubert             || !TEST_size_t_eq(master_secret_count,
328e0c4386eSCy Schubert                                expected->master_secret_count)
329e0c4386eSCy Schubert             || !TEST_size_t_eq(client_early_secret_count,
330e0c4386eSCy Schubert                                expected->client_early_secret_count)
331e0c4386eSCy Schubert             || !TEST_size_t_eq(client_handshake_secret_count,
332e0c4386eSCy Schubert                                expected->client_handshake_secret_count)
333e0c4386eSCy Schubert             || !TEST_size_t_eq(server_handshake_secret_count,
334e0c4386eSCy Schubert                                expected->server_handshake_secret_count)
335e0c4386eSCy Schubert             || !TEST_size_t_eq(client_application_secret_count,
336e0c4386eSCy Schubert                                expected->client_application_secret_count)
337e0c4386eSCy Schubert             || !TEST_size_t_eq(server_application_secret_count,
338e0c4386eSCy Schubert                                expected->server_application_secret_count)
339e0c4386eSCy Schubert             || !TEST_size_t_eq(early_exporter_secret_count,
340e0c4386eSCy Schubert                                expected->early_exporter_secret_count)
341e0c4386eSCy Schubert             || !TEST_size_t_eq(exporter_secret_count,
342e0c4386eSCy Schubert                                expected->exporter_secret_count))
343e0c4386eSCy Schubert         return 0;
344e0c4386eSCy Schubert     return 1;
345e0c4386eSCy Schubert }
346e0c4386eSCy Schubert 
347e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
test_keylog(void)348e0c4386eSCy Schubert static int test_keylog(void)
349e0c4386eSCy Schubert {
350e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
351e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
352e0c4386eSCy Schubert     int testresult = 0;
353e0c4386eSCy Schubert     struct sslapitest_log_counts expected;
354e0c4386eSCy Schubert 
355e0c4386eSCy Schubert     /* Clean up logging space */
356e0c4386eSCy Schubert     memset(&expected, 0, sizeof(expected));
357e0c4386eSCy Schubert     memset(client_log_buffer, 0, sizeof(client_log_buffer));
358e0c4386eSCy Schubert     memset(server_log_buffer, 0, sizeof(server_log_buffer));
359e0c4386eSCy Schubert     client_log_buffer_index = 0;
360e0c4386eSCy Schubert     server_log_buffer_index = 0;
361e0c4386eSCy Schubert     error_writing_log = 0;
362e0c4386eSCy Schubert 
363e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
364e0c4386eSCy Schubert                                        TLS_client_method(),
365e0c4386eSCy Schubert                                        TLS1_VERSION, 0,
366e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
367e0c4386eSCy Schubert         return 0;
368e0c4386eSCy Schubert 
369e0c4386eSCy Schubert     /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
370e0c4386eSCy Schubert     SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
371e0c4386eSCy Schubert     SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
372e0c4386eSCy Schubert 
373e0c4386eSCy Schubert     /* We also want to ensure that we use RSA-based key exchange. */
374e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
375e0c4386eSCy Schubert         goto end;
376e0c4386eSCy Schubert 
377e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
378e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
379e0c4386eSCy Schubert         goto end;
380e0c4386eSCy Schubert     SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
381e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
382e0c4386eSCy Schubert                    == client_keylog_callback))
383e0c4386eSCy Schubert         goto end;
384e0c4386eSCy Schubert     SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
385e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
386e0c4386eSCy Schubert                    == server_keylog_callback))
387e0c4386eSCy Schubert         goto end;
388e0c4386eSCy Schubert 
389e0c4386eSCy Schubert     /* Now do a handshake and check that the logs have been written to. */
390e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
391e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
392e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
393e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
394e0c4386eSCy Schubert             || !TEST_false(error_writing_log)
395e0c4386eSCy Schubert             || !TEST_int_gt(client_log_buffer_index, 0)
396e0c4386eSCy Schubert             || !TEST_int_gt(server_log_buffer_index, 0))
397e0c4386eSCy Schubert         goto end;
398e0c4386eSCy Schubert 
399e0c4386eSCy Schubert     /*
400e0c4386eSCy Schubert      * Now we want to test that our output data was vaguely sensible. We
401e0c4386eSCy Schubert      * do that by using strtok and confirming that we have more or less the
402e0c4386eSCy Schubert      * data we expect. For both client and server, we expect to see one master
403e0c4386eSCy Schubert      * secret. The client should also see an RSA key exchange.
404e0c4386eSCy Schubert      */
405e0c4386eSCy Schubert     expected.rsa_key_exchange_count = 1;
406e0c4386eSCy Schubert     expected.master_secret_count = 1;
407e0c4386eSCy Schubert     if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
408e0c4386eSCy Schubert                                       SSL_get_session(clientssl), &expected)))
409e0c4386eSCy Schubert         goto end;
410e0c4386eSCy Schubert 
411e0c4386eSCy Schubert     expected.rsa_key_exchange_count = 0;
412e0c4386eSCy Schubert     if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
413e0c4386eSCy Schubert                                       SSL_get_session(serverssl), &expected)))
414e0c4386eSCy Schubert         goto end;
415e0c4386eSCy Schubert 
416e0c4386eSCy Schubert     testresult = 1;
417e0c4386eSCy Schubert 
418e0c4386eSCy Schubert end:
419e0c4386eSCy Schubert     SSL_free(serverssl);
420e0c4386eSCy Schubert     SSL_free(clientssl);
421e0c4386eSCy Schubert     SSL_CTX_free(sctx);
422e0c4386eSCy Schubert     SSL_CTX_free(cctx);
423e0c4386eSCy Schubert 
424e0c4386eSCy Schubert     return testresult;
425e0c4386eSCy Schubert }
426e0c4386eSCy Schubert #endif
427e0c4386eSCy Schubert 
428e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
test_keylog_no_master_key(void)429e0c4386eSCy Schubert static int test_keylog_no_master_key(void)
430e0c4386eSCy Schubert {
431e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
432e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
433e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
434e0c4386eSCy Schubert     int testresult = 0;
435e0c4386eSCy Schubert     struct sslapitest_log_counts expected;
436e0c4386eSCy Schubert     unsigned char buf[1];
437e0c4386eSCy Schubert     size_t readbytes, written;
438e0c4386eSCy Schubert 
439e0c4386eSCy Schubert     /* Clean up logging space */
440e0c4386eSCy Schubert     memset(&expected, 0, sizeof(expected));
441e0c4386eSCy Schubert     memset(client_log_buffer, 0, sizeof(client_log_buffer));
442e0c4386eSCy Schubert     memset(server_log_buffer, 0, sizeof(server_log_buffer));
443e0c4386eSCy Schubert     client_log_buffer_index = 0;
444e0c4386eSCy Schubert     server_log_buffer_index = 0;
445e0c4386eSCy Schubert     error_writing_log = 0;
446e0c4386eSCy Schubert 
447e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
448e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
449e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
450e0c4386eSCy Schubert         || !TEST_true(SSL_CTX_set_max_early_data(sctx,
451e0c4386eSCy Schubert                                                  SSL3_RT_MAX_PLAIN_LENGTH)))
452e0c4386eSCy Schubert         return 0;
453e0c4386eSCy Schubert 
454e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
455e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
456e0c4386eSCy Schubert         goto end;
457e0c4386eSCy Schubert 
458e0c4386eSCy Schubert     SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
459e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
460e0c4386eSCy Schubert                    == client_keylog_callback))
461e0c4386eSCy Schubert         goto end;
462e0c4386eSCy Schubert 
463e0c4386eSCy Schubert     SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
464e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
465e0c4386eSCy Schubert                    == server_keylog_callback))
466e0c4386eSCy Schubert         goto end;
467e0c4386eSCy Schubert 
468e0c4386eSCy Schubert     /* Now do a handshake and check that the logs have been written to. */
469e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
470e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
471e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
472e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
473e0c4386eSCy Schubert             || !TEST_false(error_writing_log))
474e0c4386eSCy Schubert         goto end;
475e0c4386eSCy Schubert 
476e0c4386eSCy Schubert     /*
477e0c4386eSCy Schubert      * Now we want to test that our output data was vaguely sensible. For this
478e0c4386eSCy Schubert      * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
479e0c4386eSCy Schubert      * TLSv1.3, but we do expect both client and server to emit keys.
480e0c4386eSCy Schubert      */
481e0c4386eSCy Schubert     expected.client_handshake_secret_count = 1;
482e0c4386eSCy Schubert     expected.server_handshake_secret_count = 1;
483e0c4386eSCy Schubert     expected.client_application_secret_count = 1;
484e0c4386eSCy Schubert     expected.server_application_secret_count = 1;
485e0c4386eSCy Schubert     expected.exporter_secret_count = 1;
486e0c4386eSCy Schubert     if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
487e0c4386eSCy Schubert                                       SSL_get_session(clientssl), &expected))
488e0c4386eSCy Schubert             || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
489e0c4386eSCy Schubert                                              SSL_get_session(serverssl),
490e0c4386eSCy Schubert                                              &expected)))
491e0c4386eSCy Schubert         goto end;
492e0c4386eSCy Schubert 
493e0c4386eSCy Schubert     /* Terminate old session and resume with early data. */
494e0c4386eSCy Schubert     sess = SSL_get1_session(clientssl);
495e0c4386eSCy Schubert     SSL_shutdown(clientssl);
496e0c4386eSCy Schubert     SSL_shutdown(serverssl);
497e0c4386eSCy Schubert     SSL_free(serverssl);
498e0c4386eSCy Schubert     SSL_free(clientssl);
499e0c4386eSCy Schubert     serverssl = clientssl = NULL;
500e0c4386eSCy Schubert 
501e0c4386eSCy Schubert     /* Reset key log */
502e0c4386eSCy Schubert     memset(client_log_buffer, 0, sizeof(client_log_buffer));
503e0c4386eSCy Schubert     memset(server_log_buffer, 0, sizeof(server_log_buffer));
504e0c4386eSCy Schubert     client_log_buffer_index = 0;
505e0c4386eSCy Schubert     server_log_buffer_index = 0;
506e0c4386eSCy Schubert 
507e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
508e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
509e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, sess))
510e0c4386eSCy Schubert             /* Here writing 0 length early data is enough. */
511e0c4386eSCy Schubert             || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
512e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
513e0c4386eSCy Schubert                                                 &readbytes),
514e0c4386eSCy Schubert                             SSL_READ_EARLY_DATA_ERROR)
515e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
516e0c4386eSCy Schubert                             SSL_EARLY_DATA_ACCEPTED)
517e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
518e0c4386eSCy Schubert                           SSL_ERROR_NONE))
519e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
520e0c4386eSCy Schubert         goto end;
521e0c4386eSCy Schubert 
522e0c4386eSCy Schubert     /* In addition to the previous entries, expect early secrets. */
523e0c4386eSCy Schubert     expected.client_early_secret_count = 1;
524e0c4386eSCy Schubert     expected.early_exporter_secret_count = 1;
525e0c4386eSCy Schubert     if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
526e0c4386eSCy Schubert                                       SSL_get_session(clientssl), &expected))
527e0c4386eSCy Schubert             || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
528e0c4386eSCy Schubert                                              SSL_get_session(serverssl),
529e0c4386eSCy Schubert                                              &expected)))
530e0c4386eSCy Schubert         goto end;
531e0c4386eSCy Schubert 
532e0c4386eSCy Schubert     testresult = 1;
533e0c4386eSCy Schubert 
534e0c4386eSCy Schubert end:
535e0c4386eSCy Schubert     SSL_SESSION_free(sess);
536e0c4386eSCy Schubert     SSL_free(serverssl);
537e0c4386eSCy Schubert     SSL_free(clientssl);
538e0c4386eSCy Schubert     SSL_CTX_free(sctx);
539e0c4386eSCy Schubert     SSL_CTX_free(cctx);
540e0c4386eSCy Schubert 
541e0c4386eSCy Schubert     return testresult;
542e0c4386eSCy Schubert }
543e0c4386eSCy Schubert #endif
544e0c4386eSCy Schubert 
verify_retry_cb(X509_STORE_CTX * ctx,void * arg)545e0c4386eSCy Schubert static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
546e0c4386eSCy Schubert {
547e0c4386eSCy Schubert     int res = X509_verify_cert(ctx);
548e0c4386eSCy Schubert     int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
549e0c4386eSCy Schubert     SSL *ssl;
550e0c4386eSCy Schubert 
551e0c4386eSCy Schubert     /* this should not happen but check anyway */
552e0c4386eSCy Schubert     if (idx < 0
553e0c4386eSCy Schubert         || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
554e0c4386eSCy Schubert         return 0;
555e0c4386eSCy Schubert 
556e0c4386eSCy Schubert     if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
557e0c4386eSCy Schubert         X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
558e0c4386eSCy Schubert         /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
559e0c4386eSCy Schubert         return SSL_set_retry_verify(ssl);
560e0c4386eSCy Schubert 
561e0c4386eSCy Schubert     return res;
562e0c4386eSCy Schubert }
563e0c4386eSCy Schubert 
test_client_cert_verify_cb(void)564e0c4386eSCy Schubert static int test_client_cert_verify_cb(void)
565e0c4386eSCy Schubert {
566e0c4386eSCy Schubert     /* server key, cert, chain, and root */
567e0c4386eSCy Schubert     char *skey = test_mk_file_path(certsdir, "leaf.key");
568e0c4386eSCy Schubert     char *leaf = test_mk_file_path(certsdir, "leaf.pem");
569e0c4386eSCy Schubert     char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
570e0c4386eSCy Schubert     char *int1 = test_mk_file_path(certsdir, "interCA.pem");
571e0c4386eSCy Schubert     char *root = test_mk_file_path(certsdir, "rootCA.pem");
572e0c4386eSCy Schubert     X509 *crt1 = NULL, *crt2 = NULL;
573e0c4386eSCy Schubert     STACK_OF(X509) *server_chain;
574e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
575e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
576e0c4386eSCy Schubert     int testresult = 0;
577e0c4386eSCy Schubert 
578e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
579e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
580e0c4386eSCy Schubert                                        &sctx, &cctx, NULL, NULL)))
581e0c4386eSCy Schubert         goto end;
582e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
583e0c4386eSCy Schubert             || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
584e0c4386eSCy Schubert                                                         SSL_FILETYPE_PEM), 1)
585e0c4386eSCy Schubert             || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
586e0c4386eSCy Schubert         goto end;
587e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
588e0c4386eSCy Schubert         goto end;
589e0c4386eSCy Schubert     SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
590e0c4386eSCy Schubert     SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
591e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
592e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
593e0c4386eSCy Schubert         goto end;
594e0c4386eSCy Schubert 
595e0c4386eSCy Schubert     /* attempt SSL_connect() with incomplete server chain */
596e0c4386eSCy Schubert     if (!TEST_false(create_ssl_connection(serverssl, clientssl,
597e0c4386eSCy Schubert                                           SSL_ERROR_WANT_RETRY_VERIFY)))
598e0c4386eSCy Schubert         goto end;
599e0c4386eSCy Schubert 
600e0c4386eSCy Schubert     /* application provides intermediate certs needed to verify server cert */
601e0c4386eSCy Schubert     if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
602e0c4386eSCy Schubert         || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
603e0c4386eSCy Schubert         || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
604e0c4386eSCy Schubert         goto end;
605e0c4386eSCy Schubert     /* add certs in reverse order to demonstrate real chain building */
606e0c4386eSCy Schubert     if (!TEST_true(sk_X509_push(server_chain, crt1)))
607e0c4386eSCy Schubert         goto end;
608e0c4386eSCy Schubert     crt1 = NULL;
609e0c4386eSCy Schubert     if (!TEST_true(sk_X509_push(server_chain, crt2)))
610e0c4386eSCy Schubert         goto end;
611e0c4386eSCy Schubert     crt2 = NULL;
612e0c4386eSCy Schubert 
613e0c4386eSCy Schubert     /* continue SSL_connect(), must now succeed with completed server chain */
614e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
615e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
616e0c4386eSCy Schubert         goto end;
617e0c4386eSCy Schubert 
618e0c4386eSCy Schubert     testresult = 1;
619e0c4386eSCy Schubert 
620e0c4386eSCy Schubert end:
621e0c4386eSCy Schubert     X509_free(crt1);
622e0c4386eSCy Schubert     X509_free(crt2);
623e0c4386eSCy Schubert     if (clientssl != NULL) {
624e0c4386eSCy Schubert         SSL_shutdown(clientssl);
625e0c4386eSCy Schubert         SSL_free(clientssl);
626e0c4386eSCy Schubert     }
627e0c4386eSCy Schubert     if (serverssl != NULL) {
628e0c4386eSCy Schubert         SSL_shutdown(serverssl);
629e0c4386eSCy Schubert         SSL_free(serverssl);
630e0c4386eSCy Schubert     }
631e0c4386eSCy Schubert     SSL_CTX_free(sctx);
632e0c4386eSCy Schubert     SSL_CTX_free(cctx);
633e0c4386eSCy Schubert 
634e0c4386eSCy Schubert     OPENSSL_free(skey);
635e0c4386eSCy Schubert     OPENSSL_free(leaf);
636e0c4386eSCy Schubert     OPENSSL_free(int2);
637e0c4386eSCy Schubert     OPENSSL_free(int1);
638e0c4386eSCy Schubert     OPENSSL_free(root);
639e0c4386eSCy Schubert 
640e0c4386eSCy Schubert     return testresult;
641e0c4386eSCy Schubert }
642e0c4386eSCy Schubert 
test_ssl_build_cert_chain(void)643e0c4386eSCy Schubert static int test_ssl_build_cert_chain(void)
644e0c4386eSCy Schubert {
645e0c4386eSCy Schubert     int ret = 0;
646e0c4386eSCy Schubert     SSL_CTX *ssl_ctx = NULL;
647e0c4386eSCy Schubert     SSL *ssl = NULL;
648e0c4386eSCy Schubert     char *skey = test_mk_file_path(certsdir, "leaf.key");
649e0c4386eSCy Schubert     char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
650e0c4386eSCy Schubert 
651e0c4386eSCy Schubert     if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
652e0c4386eSCy Schubert         goto end;
653e0c4386eSCy Schubert     if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
654e0c4386eSCy Schubert         goto end;
655e0c4386eSCy Schubert     /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
656e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
657e0c4386eSCy Schubert         || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
658e0c4386eSCy Schubert         || !TEST_int_eq(SSL_check_private_key(ssl), 1))
659e0c4386eSCy Schubert         goto end;
660e0c4386eSCy Schubert     if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
661e0c4386eSCy Schubert                                              | SSL_BUILD_CHAIN_FLAG_CHECK)))
662e0c4386eSCy Schubert         goto end;
663e0c4386eSCy Schubert     ret = 1;
664e0c4386eSCy Schubert end:
665e0c4386eSCy Schubert     SSL_free(ssl);
666e0c4386eSCy Schubert     SSL_CTX_free(ssl_ctx);
667e0c4386eSCy Schubert     OPENSSL_free(leaf_chain);
668e0c4386eSCy Schubert     OPENSSL_free(skey);
669e0c4386eSCy Schubert     return ret;
670e0c4386eSCy Schubert }
671e0c4386eSCy Schubert 
get_password_cb(char * buf,int size,int rw_flag,void * userdata)672e0c4386eSCy Schubert static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
673e0c4386eSCy Schubert {
674e0c4386eSCy Schubert     static const char pass[] = "testpass";
675e0c4386eSCy Schubert 
676e0c4386eSCy Schubert     if (!TEST_int_eq(size, PEM_BUFSIZE))
677e0c4386eSCy Schubert         return -1;
678e0c4386eSCy Schubert 
679e0c4386eSCy Schubert     memcpy(buf, pass, sizeof(pass) - 1);
680e0c4386eSCy Schubert     return sizeof(pass) - 1;
681e0c4386eSCy Schubert }
682e0c4386eSCy Schubert 
test_ssl_ctx_build_cert_chain(void)683e0c4386eSCy Schubert static int test_ssl_ctx_build_cert_chain(void)
684e0c4386eSCy Schubert {
685e0c4386eSCy Schubert     int ret = 0;
686e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
687e0c4386eSCy Schubert     char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
688e0c4386eSCy Schubert     char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
689e0c4386eSCy Schubert 
690e0c4386eSCy Schubert     if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
691e0c4386eSCy Schubert         goto end;
692e0c4386eSCy Schubert     SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
693e0c4386eSCy Schubert     /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
694e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
695e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
696e0c4386eSCy Schubert                                                     SSL_FILETYPE_PEM), 1)
697e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
698e0c4386eSCy Schubert         goto end;
699e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
700e0c4386eSCy Schubert                                                 | SSL_BUILD_CHAIN_FLAG_CHECK)))
701e0c4386eSCy Schubert         goto end;
702e0c4386eSCy Schubert     ret = 1;
703e0c4386eSCy Schubert end:
704e0c4386eSCy Schubert     SSL_CTX_free(ctx);
705e0c4386eSCy Schubert     OPENSSL_free(leaf_chain);
706e0c4386eSCy Schubert     OPENSSL_free(skey);
707e0c4386eSCy Schubert     return ret;
708e0c4386eSCy Schubert }
709e0c4386eSCy Schubert 
710e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
full_client_hello_callback(SSL * s,int * al,void * arg)711e0c4386eSCy Schubert static int full_client_hello_callback(SSL *s, int *al, void *arg)
712e0c4386eSCy Schubert {
713e0c4386eSCy Schubert     int *ctr = arg;
714e0c4386eSCy Schubert     const unsigned char *p;
715e0c4386eSCy Schubert     int *exts;
716e0c4386eSCy Schubert     /* We only configure two ciphers, but the SCSV is added automatically. */
717e0c4386eSCy Schubert #ifdef OPENSSL_NO_EC
718e0c4386eSCy Schubert     const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
719e0c4386eSCy Schubert #else
720e0c4386eSCy Schubert     const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
721e0c4386eSCy Schubert                                               0x2c, 0x00, 0xff};
722e0c4386eSCy Schubert #endif
723e0c4386eSCy Schubert     const int expected_extensions[] = {
724e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC
725e0c4386eSCy Schubert                                        11, 10,
726e0c4386eSCy Schubert #endif
727e0c4386eSCy Schubert                                        35, 22, 23, 13};
728e0c4386eSCy Schubert     size_t len;
729e0c4386eSCy Schubert 
730e0c4386eSCy Schubert     /* Make sure we can defer processing and get called back. */
731e0c4386eSCy Schubert     if ((*ctr)++ == 0)
732e0c4386eSCy Schubert         return SSL_CLIENT_HELLO_RETRY;
733e0c4386eSCy Schubert 
734e0c4386eSCy Schubert     len = SSL_client_hello_get0_ciphers(s, &p);
735e0c4386eSCy Schubert     if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
736e0c4386eSCy Schubert             || !TEST_size_t_eq(
737e0c4386eSCy Schubert                        SSL_client_hello_get0_compression_methods(s, &p), 1)
738e0c4386eSCy Schubert             || !TEST_int_eq(*p, 0))
739e0c4386eSCy Schubert         return SSL_CLIENT_HELLO_ERROR;
740e0c4386eSCy Schubert     if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
741e0c4386eSCy Schubert         return SSL_CLIENT_HELLO_ERROR;
742e0c4386eSCy Schubert     if (len != OSSL_NELEM(expected_extensions) ||
743e0c4386eSCy Schubert         memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
744e0c4386eSCy Schubert         printf("ClientHello callback expected extensions mismatch\n");
745e0c4386eSCy Schubert         OPENSSL_free(exts);
746e0c4386eSCy Schubert         return SSL_CLIENT_HELLO_ERROR;
747e0c4386eSCy Schubert     }
748e0c4386eSCy Schubert     OPENSSL_free(exts);
749e0c4386eSCy Schubert     return SSL_CLIENT_HELLO_SUCCESS;
750e0c4386eSCy Schubert }
751e0c4386eSCy Schubert 
test_client_hello_cb(void)752e0c4386eSCy Schubert static int test_client_hello_cb(void)
753e0c4386eSCy Schubert {
754e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
755e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
756e0c4386eSCy Schubert     int testctr = 0, testresult = 0;
757e0c4386eSCy Schubert 
758e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
759e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
760e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
761e0c4386eSCy Schubert         goto end;
762e0c4386eSCy Schubert     SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
763e0c4386eSCy Schubert 
764e0c4386eSCy Schubert     /* The gimpy cipher list we configure can't do TLS 1.3. */
765e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
766e0c4386eSCy Schubert 
767e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
768e0c4386eSCy Schubert                         "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
769e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
770e0c4386eSCy Schubert                                              &clientssl, NULL, NULL))
771e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
772e0c4386eSCy Schubert                         SSL_ERROR_WANT_CLIENT_HELLO_CB))
773e0c4386eSCy Schubert                 /*
774e0c4386eSCy Schubert                  * Passing a -1 literal is a hack since
775e0c4386eSCy Schubert                  * the real value was lost.
776e0c4386eSCy Schubert                  * */
777e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(serverssl, -1),
778e0c4386eSCy Schubert                             SSL_ERROR_WANT_CLIENT_HELLO_CB)
779e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
780e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
781e0c4386eSCy Schubert         goto end;
782e0c4386eSCy Schubert 
783e0c4386eSCy Schubert     testresult = 1;
784e0c4386eSCy Schubert 
785e0c4386eSCy Schubert end:
786e0c4386eSCy Schubert     SSL_free(serverssl);
787e0c4386eSCy Schubert     SSL_free(clientssl);
788e0c4386eSCy Schubert     SSL_CTX_free(sctx);
789e0c4386eSCy Schubert     SSL_CTX_free(cctx);
790e0c4386eSCy Schubert 
791e0c4386eSCy Schubert     return testresult;
792e0c4386eSCy Schubert }
793e0c4386eSCy Schubert 
test_no_ems(void)794e0c4386eSCy Schubert static int test_no_ems(void)
795e0c4386eSCy Schubert {
796e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
797e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
798e0c4386eSCy Schubert     int testresult = 0;
799e0c4386eSCy Schubert 
800e0c4386eSCy Schubert     if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
801e0c4386eSCy Schubert                              TLS1_VERSION, TLS1_2_VERSION,
802e0c4386eSCy Schubert                              &sctx, &cctx, cert, privkey)) {
803e0c4386eSCy Schubert         printf("Unable to create SSL_CTX pair\n");
804e0c4386eSCy Schubert         goto end;
805e0c4386eSCy Schubert     }
806e0c4386eSCy Schubert 
807e0c4386eSCy Schubert     SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
808e0c4386eSCy Schubert 
809e0c4386eSCy Schubert     if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
810e0c4386eSCy Schubert         printf("Unable to create SSL objects\n");
811e0c4386eSCy Schubert         goto end;
812e0c4386eSCy Schubert     }
813e0c4386eSCy Schubert 
814e0c4386eSCy Schubert     if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
815e0c4386eSCy Schubert         printf("Creating SSL connection failed\n");
816e0c4386eSCy Schubert         goto end;
817e0c4386eSCy Schubert     }
818e0c4386eSCy Schubert 
819e0c4386eSCy Schubert     if (SSL_get_extms_support(serverssl)) {
820e0c4386eSCy Schubert         printf("Server reports Extended Master Secret support\n");
821e0c4386eSCy Schubert         goto end;
822e0c4386eSCy Schubert     }
823e0c4386eSCy Schubert 
824e0c4386eSCy Schubert     if (SSL_get_extms_support(clientssl)) {
825e0c4386eSCy Schubert         printf("Client reports Extended Master Secret support\n");
826e0c4386eSCy Schubert         goto end;
827e0c4386eSCy Schubert     }
828e0c4386eSCy Schubert     testresult = 1;
829e0c4386eSCy Schubert 
830e0c4386eSCy Schubert end:
831e0c4386eSCy Schubert     SSL_free(serverssl);
832e0c4386eSCy Schubert     SSL_free(clientssl);
833e0c4386eSCy Schubert     SSL_CTX_free(sctx);
834e0c4386eSCy Schubert     SSL_CTX_free(cctx);
835e0c4386eSCy Schubert 
836e0c4386eSCy Schubert     return testresult;
837e0c4386eSCy Schubert }
838e0c4386eSCy Schubert 
839e0c4386eSCy Schubert /*
840e0c4386eSCy Schubert  * Very focused test to exercise a single case in the server-side state
841e0c4386eSCy Schubert  * machine, when the ChangeCipherState message needs to actually change
842e0c4386eSCy Schubert  * from one cipher to a different cipher (i.e., not changing from null
843e0c4386eSCy Schubert  * encryption to real encryption).
844e0c4386eSCy Schubert  */
test_ccs_change_cipher(void)845e0c4386eSCy Schubert static int test_ccs_change_cipher(void)
846e0c4386eSCy Schubert {
847e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
848e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
849e0c4386eSCy Schubert     SSL_SESSION *sess = NULL, *sesspre, *sesspost;
850e0c4386eSCy Schubert     int testresult = 0;
851e0c4386eSCy Schubert     int i;
852e0c4386eSCy Schubert     unsigned char buf;
853e0c4386eSCy Schubert     size_t readbytes;
854e0c4386eSCy Schubert 
855e0c4386eSCy Schubert     /*
856e0c4386eSCy Schubert      * Create a conection so we can resume and potentially (but not) use
857e0c4386eSCy Schubert      * a different cipher in the second connection.
858e0c4386eSCy Schubert      */
859e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
860e0c4386eSCy Schubert                                        TLS_client_method(),
861e0c4386eSCy Schubert                                        TLS1_VERSION, TLS1_2_VERSION,
862e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
863e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
864e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
865e0c4386eSCy Schubert                           NULL, NULL))
866e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
867e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
868e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
869e0c4386eSCy Schubert             || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
870e0c4386eSCy Schubert             || !TEST_ptr(sess = SSL_get1_session(clientssl)))
871e0c4386eSCy Schubert         goto end;
872e0c4386eSCy Schubert 
873e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
874e0c4386eSCy Schubert     serverssl = clientssl = NULL;
875e0c4386eSCy Schubert 
876e0c4386eSCy Schubert     /* Resume, preferring a different cipher. Our server will force the
877e0c4386eSCy Schubert      * same cipher to be used as the initial handshake. */
878e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
879e0c4386eSCy Schubert                           NULL, NULL))
880e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, sess))
881e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
882e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
883e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
884e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl))
885e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(serverssl))
886e0c4386eSCy Schubert             || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
887e0c4386eSCy Schubert             || !TEST_ptr_eq(sesspre, sesspost)
888e0c4386eSCy Schubert             || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
889e0c4386eSCy Schubert                             SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
890e0c4386eSCy Schubert         goto end;
891e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
892e0c4386eSCy Schubert     serverssl = clientssl = NULL;
893e0c4386eSCy Schubert 
894e0c4386eSCy Schubert     /*
895e0c4386eSCy Schubert      * Now create a fresh connection and try to renegotiate a different
896e0c4386eSCy Schubert      * cipher on it.
897e0c4386eSCy Schubert      */
898e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
899e0c4386eSCy Schubert                                       NULL, NULL))
900e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
901e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
902e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
903e0c4386eSCy Schubert             || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
904e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
905e0c4386eSCy Schubert             || !TEST_true(SSL_renegotiate(clientssl))
906e0c4386eSCy Schubert             || !TEST_true(SSL_renegotiate_pending(clientssl)))
907e0c4386eSCy Schubert         goto end;
908e0c4386eSCy Schubert     /* Actually drive the renegotiation. */
909e0c4386eSCy Schubert     for (i = 0; i < 3; i++) {
910e0c4386eSCy Schubert         if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
911e0c4386eSCy Schubert             if (!TEST_ulong_eq(readbytes, 0))
912e0c4386eSCy Schubert                 goto end;
913e0c4386eSCy Schubert         } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
914e0c4386eSCy Schubert                                 SSL_ERROR_WANT_READ)) {
915e0c4386eSCy Schubert             goto end;
916e0c4386eSCy Schubert         }
917e0c4386eSCy Schubert         if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
918e0c4386eSCy Schubert             if (!TEST_ulong_eq(readbytes, 0))
919e0c4386eSCy Schubert                 goto end;
920e0c4386eSCy Schubert         } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
921e0c4386eSCy Schubert                                 SSL_ERROR_WANT_READ)) {
922e0c4386eSCy Schubert             goto end;
923e0c4386eSCy Schubert         }
924e0c4386eSCy Schubert     }
925e0c4386eSCy Schubert     /* sesspre and sesspost should be different since the cipher changed. */
926e0c4386eSCy Schubert     if (!TEST_false(SSL_renegotiate_pending(clientssl))
927e0c4386eSCy Schubert             || !TEST_false(SSL_session_reused(clientssl))
928e0c4386eSCy Schubert             || !TEST_false(SSL_session_reused(serverssl))
929e0c4386eSCy Schubert             || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
930e0c4386eSCy Schubert             || !TEST_ptr_ne(sesspre, sesspost)
931e0c4386eSCy Schubert             || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
932e0c4386eSCy Schubert                             SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
933e0c4386eSCy Schubert         goto end;
934e0c4386eSCy Schubert 
935e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
936e0c4386eSCy Schubert     serverssl = clientssl = NULL;
937e0c4386eSCy Schubert 
938e0c4386eSCy Schubert     testresult = 1;
939e0c4386eSCy Schubert 
940e0c4386eSCy Schubert end:
941e0c4386eSCy Schubert     SSL_free(serverssl);
942e0c4386eSCy Schubert     SSL_free(clientssl);
943e0c4386eSCy Schubert     SSL_CTX_free(sctx);
944e0c4386eSCy Schubert     SSL_CTX_free(cctx);
945e0c4386eSCy Schubert     SSL_SESSION_free(sess);
946e0c4386eSCy Schubert 
947e0c4386eSCy Schubert     return testresult;
948e0c4386eSCy Schubert }
949e0c4386eSCy Schubert #endif
950e0c4386eSCy Schubert 
add_large_cert_chain(SSL_CTX * sctx)951e0c4386eSCy Schubert static int add_large_cert_chain(SSL_CTX *sctx)
952e0c4386eSCy Schubert {
953e0c4386eSCy Schubert     BIO *certbio = NULL;
954e0c4386eSCy Schubert     X509 *chaincert = NULL;
955e0c4386eSCy Schubert     int certlen;
956e0c4386eSCy Schubert     int ret = 0;
957e0c4386eSCy Schubert     int i;
958e0c4386eSCy Schubert 
959e0c4386eSCy Schubert     if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
960e0c4386eSCy Schubert         goto end;
961e0c4386eSCy Schubert 
962e0c4386eSCy Schubert     if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
963e0c4386eSCy Schubert         goto end;
964e0c4386eSCy Schubert 
965e0c4386eSCy Schubert     if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
966e0c4386eSCy Schubert         goto end;
967e0c4386eSCy Schubert     BIO_free(certbio);
968e0c4386eSCy Schubert     certbio = NULL;
969e0c4386eSCy Schubert 
970e0c4386eSCy Schubert     /*
971e0c4386eSCy Schubert      * We assume the supplied certificate is big enough so that if we add
972e0c4386eSCy Schubert      * NUM_EXTRA_CERTS it will make the overall message large enough. The
973e0c4386eSCy Schubert      * default buffer size is requested to be 16k, but due to the way BUF_MEM
974e0c4386eSCy Schubert      * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
975e0c4386eSCy Schubert      * test we need to have a message larger than that.
976e0c4386eSCy Schubert      */
977e0c4386eSCy Schubert     certlen = i2d_X509(chaincert, NULL);
978e0c4386eSCy Schubert     OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
979e0c4386eSCy Schubert                    (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
980e0c4386eSCy Schubert     for (i = 0; i < NUM_EXTRA_CERTS; i++) {
981e0c4386eSCy Schubert         if (!X509_up_ref(chaincert))
982e0c4386eSCy Schubert             goto end;
983e0c4386eSCy Schubert         if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
984e0c4386eSCy Schubert             X509_free(chaincert);
985e0c4386eSCy Schubert             goto end;
986e0c4386eSCy Schubert         }
987e0c4386eSCy Schubert     }
988e0c4386eSCy Schubert 
989e0c4386eSCy Schubert     ret = 1;
990e0c4386eSCy Schubert  end:
991e0c4386eSCy Schubert     BIO_free(certbio);
992e0c4386eSCy Schubert     X509_free(chaincert);
993e0c4386eSCy Schubert     return ret;
994e0c4386eSCy Schubert }
995e0c4386eSCy Schubert 
execute_test_large_message(const SSL_METHOD * smeth,const SSL_METHOD * cmeth,int min_version,int max_version,int read_ahead)996e0c4386eSCy Schubert static int execute_test_large_message(const SSL_METHOD *smeth,
997e0c4386eSCy Schubert                                       const SSL_METHOD *cmeth,
998e0c4386eSCy Schubert                                       int min_version, int max_version,
999e0c4386eSCy Schubert                                       int read_ahead)
1000e0c4386eSCy Schubert {
1001e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1002e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1003e0c4386eSCy Schubert     int testresult = 0;
1004e0c4386eSCy Schubert 
1005e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
1006e0c4386eSCy Schubert                                        max_version, &sctx, &cctx, cert,
1007e0c4386eSCy Schubert                                        privkey)))
1008e0c4386eSCy Schubert         goto end;
1009e0c4386eSCy Schubert 
1010e0c4386eSCy Schubert #ifdef OPENSSL_NO_DTLS1_2
1011e0c4386eSCy Schubert     if (smeth == DTLS_server_method()) {
1012e0c4386eSCy Schubert         /*
1013e0c4386eSCy Schubert          * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1014e0c4386eSCy Schubert          * level 0
1015e0c4386eSCy Schubert          */
1016e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1017e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1018e0c4386eSCy Schubert                                                     "DEFAULT:@SECLEVEL=0")))
1019e0c4386eSCy Schubert             goto end;
1020e0c4386eSCy Schubert     }
1021e0c4386eSCy Schubert #endif
1022e0c4386eSCy Schubert 
1023e0c4386eSCy Schubert     if (read_ahead) {
1024e0c4386eSCy Schubert         /*
1025e0c4386eSCy Schubert          * Test that read_ahead works correctly when dealing with large
1026e0c4386eSCy Schubert          * records
1027e0c4386eSCy Schubert          */
1028e0c4386eSCy Schubert         SSL_CTX_set_read_ahead(cctx, 1);
1029e0c4386eSCy Schubert     }
1030e0c4386eSCy Schubert 
1031e0c4386eSCy Schubert     if (!add_large_cert_chain(sctx))
1032e0c4386eSCy Schubert         goto end;
1033e0c4386eSCy Schubert 
1034e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1035e0c4386eSCy Schubert                                       NULL, NULL))
1036e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
1037e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
1038e0c4386eSCy Schubert         goto end;
1039e0c4386eSCy Schubert 
1040e0c4386eSCy Schubert     /*
1041e0c4386eSCy Schubert      * Calling SSL_clear() first is not required but this tests that SSL_clear()
1042e0c4386eSCy Schubert      * doesn't leak.
1043e0c4386eSCy Schubert      */
1044e0c4386eSCy Schubert     if (!TEST_true(SSL_clear(serverssl)))
1045e0c4386eSCy Schubert         goto end;
1046e0c4386eSCy Schubert 
1047e0c4386eSCy Schubert     testresult = 1;
1048e0c4386eSCy Schubert  end:
1049e0c4386eSCy Schubert     SSL_free(serverssl);
1050e0c4386eSCy Schubert     SSL_free(clientssl);
1051e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1052e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1053e0c4386eSCy Schubert 
1054e0c4386eSCy Schubert     return testresult;
1055e0c4386eSCy Schubert }
1056e0c4386eSCy Schubert 
1057e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
1058e0c4386eSCy Schubert     !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
1059e0c4386eSCy Schubert /* sock must be connected */
ktls_chk_platform(int sock)1060e0c4386eSCy Schubert static int ktls_chk_platform(int sock)
1061e0c4386eSCy Schubert {
1062e0c4386eSCy Schubert     if (!ktls_enable(sock))
1063e0c4386eSCy Schubert         return 0;
1064e0c4386eSCy Schubert     return 1;
1065e0c4386eSCy Schubert }
1066e0c4386eSCy Schubert 
ping_pong_query(SSL * clientssl,SSL * serverssl)1067e0c4386eSCy Schubert static int ping_pong_query(SSL *clientssl, SSL *serverssl)
1068e0c4386eSCy Schubert {
1069e0c4386eSCy Schubert     static char count = 1;
1070e0c4386eSCy Schubert     unsigned char cbuf[16000] = {0};
1071e0c4386eSCy Schubert     unsigned char sbuf[16000];
1072e0c4386eSCy Schubert     size_t err = 0;
1073e0c4386eSCy Schubert     char crec_wseq_before[SEQ_NUM_SIZE];
1074e0c4386eSCy Schubert     char crec_wseq_after[SEQ_NUM_SIZE];
1075e0c4386eSCy Schubert     char crec_rseq_before[SEQ_NUM_SIZE];
1076e0c4386eSCy Schubert     char crec_rseq_after[SEQ_NUM_SIZE];
1077e0c4386eSCy Schubert     char srec_wseq_before[SEQ_NUM_SIZE];
1078e0c4386eSCy Schubert     char srec_wseq_after[SEQ_NUM_SIZE];
1079e0c4386eSCy Schubert     char srec_rseq_before[SEQ_NUM_SIZE];
1080e0c4386eSCy Schubert     char srec_rseq_after[SEQ_NUM_SIZE];
1081e0c4386eSCy Schubert 
1082e0c4386eSCy Schubert     cbuf[0] = count++;
1083e0c4386eSCy Schubert     memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1084e0c4386eSCy Schubert     memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1085e0c4386eSCy Schubert     memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1086e0c4386eSCy Schubert     memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1087e0c4386eSCy Schubert 
1088e0c4386eSCy Schubert     if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
1089e0c4386eSCy Schubert         goto end;
1090e0c4386eSCy Schubert 
1091e0c4386eSCy Schubert     while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
1092e0c4386eSCy Schubert         if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
1093e0c4386eSCy Schubert             goto end;
1094e0c4386eSCy Schubert         }
1095e0c4386eSCy Schubert     }
1096e0c4386eSCy Schubert 
1097e0c4386eSCy Schubert     if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
1098e0c4386eSCy Schubert         goto end;
1099e0c4386eSCy Schubert 
1100e0c4386eSCy Schubert     while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
1101e0c4386eSCy Schubert         if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
1102e0c4386eSCy Schubert             goto end;
1103e0c4386eSCy Schubert         }
1104e0c4386eSCy Schubert     }
1105e0c4386eSCy Schubert 
1106e0c4386eSCy Schubert     memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1107e0c4386eSCy Schubert     memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1108e0c4386eSCy Schubert     memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1109e0c4386eSCy Schubert     memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1110e0c4386eSCy Schubert 
1111e0c4386eSCy Schubert     /* verify the payload */
1112e0c4386eSCy Schubert     if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1113e0c4386eSCy Schubert         goto end;
1114e0c4386eSCy Schubert 
1115e0c4386eSCy Schubert     /*
1116e0c4386eSCy Schubert      * If ktls is used then kernel sequences are used instead of
1117e0c4386eSCy Schubert      * OpenSSL sequences
1118e0c4386eSCy Schubert      */
1119e0c4386eSCy Schubert     if (!BIO_get_ktls_send(clientssl->wbio)) {
1120e0c4386eSCy Schubert         if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
1121e0c4386eSCy Schubert                          crec_wseq_after, SEQ_NUM_SIZE))
1122e0c4386eSCy Schubert             goto end;
1123e0c4386eSCy Schubert     } else {
1124e0c4386eSCy Schubert         if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
1125e0c4386eSCy Schubert                          crec_wseq_after, SEQ_NUM_SIZE))
1126e0c4386eSCy Schubert             goto end;
1127e0c4386eSCy Schubert     }
1128e0c4386eSCy Schubert 
1129e0c4386eSCy Schubert     if (!BIO_get_ktls_send(serverssl->wbio)) {
1130e0c4386eSCy Schubert         if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
1131e0c4386eSCy Schubert                          srec_wseq_after, SEQ_NUM_SIZE))
1132e0c4386eSCy Schubert             goto end;
1133e0c4386eSCy Schubert     } else {
1134e0c4386eSCy Schubert         if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
1135e0c4386eSCy Schubert                          srec_wseq_after, SEQ_NUM_SIZE))
1136e0c4386eSCy Schubert             goto end;
1137e0c4386eSCy Schubert     }
1138e0c4386eSCy Schubert 
1139e0c4386eSCy Schubert     if (!BIO_get_ktls_recv(clientssl->wbio)) {
1140e0c4386eSCy Schubert         if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
1141e0c4386eSCy Schubert                          crec_rseq_after, SEQ_NUM_SIZE))
1142e0c4386eSCy Schubert             goto end;
1143e0c4386eSCy Schubert     } else {
1144e0c4386eSCy Schubert         if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
1145e0c4386eSCy Schubert                          crec_rseq_after, SEQ_NUM_SIZE))
1146e0c4386eSCy Schubert             goto end;
1147e0c4386eSCy Schubert     }
1148e0c4386eSCy Schubert 
1149e0c4386eSCy Schubert     if (!BIO_get_ktls_recv(serverssl->wbio)) {
1150e0c4386eSCy Schubert         if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
1151e0c4386eSCy Schubert                          srec_rseq_after, SEQ_NUM_SIZE))
1152e0c4386eSCy Schubert             goto end;
1153e0c4386eSCy Schubert     } else {
1154e0c4386eSCy Schubert         if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
1155e0c4386eSCy Schubert                          srec_rseq_after, SEQ_NUM_SIZE))
1156e0c4386eSCy Schubert             goto end;
1157e0c4386eSCy Schubert     }
1158e0c4386eSCy Schubert 
1159e0c4386eSCy Schubert     return 1;
1160e0c4386eSCy Schubert end:
1161e0c4386eSCy Schubert     return 0;
1162e0c4386eSCy Schubert }
1163e0c4386eSCy Schubert 
execute_test_ktls(int cis_ktls,int sis_ktls,int tls_version,const char * cipher)1164e0c4386eSCy Schubert static int execute_test_ktls(int cis_ktls, int sis_ktls,
1165e0c4386eSCy Schubert                              int tls_version, const char *cipher)
1166e0c4386eSCy Schubert {
1167e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1168e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1169e0c4386eSCy Schubert     int ktls_used = 0, testresult = 0;
1170e0c4386eSCy Schubert     int cfd = -1, sfd = -1;
1171e0c4386eSCy Schubert     int rx_supported;
1172e0c4386eSCy Schubert 
1173e0c4386eSCy Schubert     if (!TEST_true(create_test_sockets(&cfd, &sfd)))
1174e0c4386eSCy Schubert         goto end;
1175e0c4386eSCy Schubert 
1176e0c4386eSCy Schubert     /* Skip this test if the platform does not support ktls */
1177e0c4386eSCy Schubert     if (!ktls_chk_platform(cfd)) {
1178e0c4386eSCy Schubert         testresult = TEST_skip("Kernel does not support KTLS");
1179e0c4386eSCy Schubert         goto end;
1180e0c4386eSCy Schubert     }
1181e0c4386eSCy Schubert 
1182e0c4386eSCy Schubert     if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1183e0c4386eSCy Schubert         testresult = TEST_skip("CHACHA is not supported in FIPS");
1184e0c4386eSCy Schubert         goto end;
1185e0c4386eSCy Schubert     }
1186e0c4386eSCy Schubert 
1187e0c4386eSCy Schubert     /* Create a session based on SHA-256 */
1188e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1189e0c4386eSCy Schubert                                        TLS_client_method(),
1190e0c4386eSCy Schubert                                        tls_version, tls_version,
1191e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
1192e0c4386eSCy Schubert         goto end;
1193e0c4386eSCy Schubert 
1194e0c4386eSCy Schubert     if (tls_version == TLS1_3_VERSION) {
1195e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1196e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1197e0c4386eSCy Schubert             goto end;
1198e0c4386eSCy Schubert     } else {
1199e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1200e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1201e0c4386eSCy Schubert             goto end;
1202e0c4386eSCy Schubert     }
1203e0c4386eSCy Schubert 
1204e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1205e0c4386eSCy Schubert                                        &clientssl, sfd, cfd)))
1206e0c4386eSCy Schubert         goto end;
1207e0c4386eSCy Schubert 
1208e0c4386eSCy Schubert     if (cis_ktls) {
1209e0c4386eSCy Schubert         if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
1210e0c4386eSCy Schubert             goto end;
1211e0c4386eSCy Schubert     }
1212e0c4386eSCy Schubert 
1213e0c4386eSCy Schubert     if (sis_ktls) {
1214e0c4386eSCy Schubert         if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1215e0c4386eSCy Schubert             goto end;
1216e0c4386eSCy Schubert     }
1217e0c4386eSCy Schubert 
1218e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1219e0c4386eSCy Schubert         goto end;
1220e0c4386eSCy Schubert 
1221e0c4386eSCy Schubert     /*
1222e0c4386eSCy Schubert      * The running kernel may not support a given cipher suite
1223e0c4386eSCy Schubert      * or direction, so just check that KTLS isn't used when it
1224e0c4386eSCy Schubert      * isn't enabled.
1225e0c4386eSCy Schubert      */
1226e0c4386eSCy Schubert     if (!cis_ktls) {
1227e0c4386eSCy Schubert         if (!TEST_false(BIO_get_ktls_send(clientssl->wbio)))
1228e0c4386eSCy Schubert             goto end;
1229e0c4386eSCy Schubert     } else {
1230e0c4386eSCy Schubert         if (BIO_get_ktls_send(clientssl->wbio))
1231e0c4386eSCy Schubert             ktls_used = 1;
1232e0c4386eSCy Schubert     }
1233e0c4386eSCy Schubert 
1234e0c4386eSCy Schubert     if (!sis_ktls) {
1235e0c4386eSCy Schubert         if (!TEST_false(BIO_get_ktls_send(serverssl->wbio)))
1236e0c4386eSCy Schubert             goto end;
1237e0c4386eSCy Schubert     } else {
1238e0c4386eSCy Schubert         if (BIO_get_ktls_send(serverssl->wbio))
1239e0c4386eSCy Schubert             ktls_used = 1;
1240e0c4386eSCy Schubert     }
1241e0c4386eSCy Schubert 
1242e0c4386eSCy Schubert #if defined(OPENSSL_NO_KTLS_RX)
1243e0c4386eSCy Schubert     rx_supported = 0;
1244e0c4386eSCy Schubert #else
1245e0c4386eSCy Schubert     rx_supported = (tls_version != TLS1_3_VERSION);
1246e0c4386eSCy Schubert #endif
1247e0c4386eSCy Schubert     if (!cis_ktls || !rx_supported) {
1248e0c4386eSCy Schubert         if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio)))
1249e0c4386eSCy Schubert             goto end;
1250e0c4386eSCy Schubert     } else {
1251e0c4386eSCy Schubert         if (BIO_get_ktls_send(clientssl->rbio))
1252e0c4386eSCy Schubert             ktls_used = 1;
1253e0c4386eSCy Schubert     }
1254e0c4386eSCy Schubert 
1255e0c4386eSCy Schubert     if (!sis_ktls || !rx_supported) {
1256e0c4386eSCy Schubert         if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio)))
1257e0c4386eSCy Schubert             goto end;
1258e0c4386eSCy Schubert     } else {
1259e0c4386eSCy Schubert         if (BIO_get_ktls_send(serverssl->rbio))
1260e0c4386eSCy Schubert             ktls_used = 1;
1261e0c4386eSCy Schubert     }
1262e0c4386eSCy Schubert 
1263e0c4386eSCy Schubert     if ((cis_ktls || sis_ktls) && !ktls_used) {
1264e0c4386eSCy Schubert         testresult = TEST_skip("KTLS not supported for %s cipher %s",
1265e0c4386eSCy Schubert                                tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1266e0c4386eSCy Schubert                                "TLS 1.2", cipher);
1267e0c4386eSCy Schubert         goto end;
1268e0c4386eSCy Schubert     }
1269e0c4386eSCy Schubert 
1270e0c4386eSCy Schubert     if (!TEST_true(ping_pong_query(clientssl, serverssl)))
1271e0c4386eSCy Schubert         goto end;
1272e0c4386eSCy Schubert 
1273e0c4386eSCy Schubert     testresult = 1;
1274e0c4386eSCy Schubert end:
1275e0c4386eSCy Schubert     if (clientssl) {
1276e0c4386eSCy Schubert         SSL_shutdown(clientssl);
1277e0c4386eSCy Schubert         SSL_free(clientssl);
1278e0c4386eSCy Schubert     }
1279e0c4386eSCy Schubert     if (serverssl) {
1280e0c4386eSCy Schubert         SSL_shutdown(serverssl);
1281e0c4386eSCy Schubert         SSL_free(serverssl);
1282e0c4386eSCy Schubert     }
1283e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1284e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1285e0c4386eSCy Schubert     serverssl = clientssl = NULL;
1286e0c4386eSCy Schubert     if (cfd != -1)
1287e0c4386eSCy Schubert         close(cfd);
1288e0c4386eSCy Schubert     if (sfd != -1)
1289e0c4386eSCy Schubert         close(sfd);
1290e0c4386eSCy Schubert     return testresult;
1291e0c4386eSCy Schubert }
1292e0c4386eSCy Schubert 
1293e0c4386eSCy Schubert #define SENDFILE_SZ                     (16 * 4096)
1294e0c4386eSCy Schubert #define SENDFILE_CHUNK                  (4 * 4096)
1295e0c4386eSCy Schubert #define min(a,b)                        ((a) > (b) ? (b) : (a))
1296e0c4386eSCy Schubert 
execute_test_ktls_sendfile(int tls_version,const char * cipher)1297e0c4386eSCy Schubert static int execute_test_ktls_sendfile(int tls_version, const char *cipher)
1298e0c4386eSCy Schubert {
1299e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1300e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1301e0c4386eSCy Schubert     unsigned char *buf, *buf_dst;
1302e0c4386eSCy Schubert     BIO *out = NULL, *in = NULL;
1303e0c4386eSCy Schubert     int cfd = -1, sfd = -1, ffd, err;
1304e0c4386eSCy Schubert     ssize_t chunk_size = 0;
1305e0c4386eSCy Schubert     off_t chunk_off = 0;
1306e0c4386eSCy Schubert     int testresult = 0;
1307e0c4386eSCy Schubert     FILE *ffdp;
1308e0c4386eSCy Schubert 
1309e0c4386eSCy Schubert     buf = OPENSSL_zalloc(SENDFILE_SZ);
1310e0c4386eSCy Schubert     buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
1311e0c4386eSCy Schubert     if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
1312e0c4386eSCy Schubert         || !TEST_true(create_test_sockets(&cfd, &sfd)))
1313e0c4386eSCy Schubert         goto end;
1314e0c4386eSCy Schubert 
1315e0c4386eSCy Schubert     /* Skip this test if the platform does not support ktls */
1316e0c4386eSCy Schubert     if (!ktls_chk_platform(sfd)) {
1317e0c4386eSCy Schubert         testresult = TEST_skip("Kernel does not support KTLS");
1318e0c4386eSCy Schubert         goto end;
1319e0c4386eSCy Schubert     }
1320e0c4386eSCy Schubert 
1321e0c4386eSCy Schubert     if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1322e0c4386eSCy Schubert         testresult = TEST_skip("CHACHA is not supported in FIPS");
1323e0c4386eSCy Schubert         goto end;
1324e0c4386eSCy Schubert     }
1325e0c4386eSCy Schubert 
1326e0c4386eSCy Schubert     /* Create a session based on SHA-256 */
1327e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1328e0c4386eSCy Schubert                                        TLS_client_method(),
1329e0c4386eSCy Schubert                                        tls_version, tls_version,
1330e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
1331e0c4386eSCy Schubert         goto end;
1332e0c4386eSCy Schubert 
1333e0c4386eSCy Schubert     if (tls_version == TLS1_3_VERSION) {
1334e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1335e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1336e0c4386eSCy Schubert             goto end;
1337e0c4386eSCy Schubert     } else {
1338e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1339e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1340e0c4386eSCy Schubert             goto end;
1341e0c4386eSCy Schubert     }
1342e0c4386eSCy Schubert 
1343e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1344e0c4386eSCy Schubert                                        &clientssl, sfd, cfd)))
1345e0c4386eSCy Schubert         goto end;
1346e0c4386eSCy Schubert 
1347e0c4386eSCy Schubert     if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1348e0c4386eSCy Schubert         goto end;
1349e0c4386eSCy Schubert 
1350e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1351e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
1352e0c4386eSCy Schubert         goto end;
1353e0c4386eSCy Schubert 
1354e0c4386eSCy Schubert     if (!BIO_get_ktls_send(serverssl->wbio)) {
1355e0c4386eSCy Schubert         testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
1356e0c4386eSCy Schubert                                tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1357e0c4386eSCy Schubert                                "TLS 1.2", cipher);
1358e0c4386eSCy Schubert         goto end;
1359e0c4386eSCy Schubert     }
1360e0c4386eSCy Schubert 
1361e0c4386eSCy Schubert     if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
1362e0c4386eSCy Schubert         goto end;
1363e0c4386eSCy Schubert 
1364e0c4386eSCy Schubert     out = BIO_new_file(tmpfilename, "wb");
1365e0c4386eSCy Schubert     if (!TEST_ptr(out))
1366e0c4386eSCy Schubert         goto end;
1367e0c4386eSCy Schubert 
1368e0c4386eSCy Schubert     if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
1369e0c4386eSCy Schubert         goto end;
1370e0c4386eSCy Schubert 
1371e0c4386eSCy Schubert     BIO_free(out);
1372e0c4386eSCy Schubert     out = NULL;
1373e0c4386eSCy Schubert     in = BIO_new_file(tmpfilename, "rb");
1374e0c4386eSCy Schubert     BIO_get_fp(in, &ffdp);
1375e0c4386eSCy Schubert     ffd = fileno(ffdp);
1376e0c4386eSCy Schubert 
1377e0c4386eSCy Schubert     while (chunk_off < SENDFILE_SZ) {
1378e0c4386eSCy Schubert         chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
1379e0c4386eSCy Schubert         while ((err = SSL_sendfile(serverssl,
1380e0c4386eSCy Schubert                                    ffd,
1381e0c4386eSCy Schubert                                    chunk_off,
1382e0c4386eSCy Schubert                                    chunk_size,
1383e0c4386eSCy Schubert                                    0)) != chunk_size) {
1384e0c4386eSCy Schubert             if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
1385e0c4386eSCy Schubert                 goto end;
1386e0c4386eSCy Schubert         }
1387e0c4386eSCy Schubert         while ((err = SSL_read(clientssl,
1388e0c4386eSCy Schubert                                buf_dst + chunk_off,
1389e0c4386eSCy Schubert                                chunk_size)) != chunk_size) {
1390e0c4386eSCy Schubert             if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
1391e0c4386eSCy Schubert                 goto end;
1392e0c4386eSCy Schubert         }
1393e0c4386eSCy Schubert 
1394e0c4386eSCy Schubert         /* verify the payload */
1395e0c4386eSCy Schubert         if (!TEST_mem_eq(buf_dst + chunk_off,
1396e0c4386eSCy Schubert                          chunk_size,
1397e0c4386eSCy Schubert                          buf + chunk_off,
1398e0c4386eSCy Schubert                          chunk_size))
1399e0c4386eSCy Schubert             goto end;
1400e0c4386eSCy Schubert 
1401e0c4386eSCy Schubert         chunk_off += chunk_size;
1402e0c4386eSCy Schubert     }
1403e0c4386eSCy Schubert 
1404e0c4386eSCy Schubert     testresult = 1;
1405e0c4386eSCy Schubert end:
1406e0c4386eSCy Schubert     if (clientssl) {
1407e0c4386eSCy Schubert         SSL_shutdown(clientssl);
1408e0c4386eSCy Schubert         SSL_free(clientssl);
1409e0c4386eSCy Schubert     }
1410e0c4386eSCy Schubert     if (serverssl) {
1411e0c4386eSCy Schubert         SSL_shutdown(serverssl);
1412e0c4386eSCy Schubert         SSL_free(serverssl);
1413e0c4386eSCy Schubert     }
1414e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1415e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1416e0c4386eSCy Schubert     serverssl = clientssl = NULL;
1417e0c4386eSCy Schubert     BIO_free(out);
1418e0c4386eSCy Schubert     BIO_free(in);
1419e0c4386eSCy Schubert     if (cfd != -1)
1420e0c4386eSCy Schubert         close(cfd);
1421e0c4386eSCy Schubert     if (sfd != -1)
1422e0c4386eSCy Schubert         close(sfd);
1423e0c4386eSCy Schubert     OPENSSL_free(buf);
1424e0c4386eSCy Schubert     OPENSSL_free(buf_dst);
1425e0c4386eSCy Schubert     return testresult;
1426e0c4386eSCy Schubert }
1427e0c4386eSCy Schubert 
1428e0c4386eSCy Schubert static struct ktls_test_cipher {
1429e0c4386eSCy Schubert     int tls_version;
1430e0c4386eSCy Schubert     const char *cipher;
1431e0c4386eSCy Schubert } ktls_test_ciphers[] = {
1432e0c4386eSCy Schubert # if !defined(OPENSSL_NO_TLS1_2)
1433e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_GCM_128
1434e0c4386eSCy Schubert     { TLS1_2_VERSION, "AES128-GCM-SHA256" },
1435e0c4386eSCy Schubert #  endif
1436e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_CCM_128
1437e0c4386eSCy Schubert     { TLS1_2_VERSION, "AES128-CCM"},
1438e0c4386eSCy Schubert #  endif
1439e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_GCM_256
1440e0c4386eSCy Schubert     { TLS1_2_VERSION, "AES256-GCM-SHA384"},
1441e0c4386eSCy Schubert #  endif
1442e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1443e0c4386eSCy Schubert #    ifndef OPENSSL_NO_EC
1444e0c4386eSCy Schubert     { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
1445e0c4386eSCy Schubert #    endif
1446e0c4386eSCy Schubert #  endif
1447e0c4386eSCy Schubert # endif
1448e0c4386eSCy Schubert # if !defined(OSSL_NO_USABLE_TLS1_3)
1449e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_GCM_128
1450e0c4386eSCy Schubert     { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
1451e0c4386eSCy Schubert #  endif
1452e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_CCM_128
1453e0c4386eSCy Schubert     { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
1454e0c4386eSCy Schubert #  endif
1455e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_AES_GCM_256
1456e0c4386eSCy Schubert     { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
1457e0c4386eSCy Schubert #  endif
1458e0c4386eSCy Schubert #  ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1459e0c4386eSCy Schubert     { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
1460e0c4386eSCy Schubert #  endif
1461e0c4386eSCy Schubert # endif
1462e0c4386eSCy Schubert };
1463e0c4386eSCy Schubert 
1464e0c4386eSCy Schubert #define NUM_KTLS_TEST_CIPHERS \
1465e0c4386eSCy Schubert     (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
1466e0c4386eSCy Schubert 
test_ktls(int test)1467e0c4386eSCy Schubert static int test_ktls(int test)
1468e0c4386eSCy Schubert {
1469e0c4386eSCy Schubert     struct ktls_test_cipher *cipher;
1470e0c4386eSCy Schubert     int cis_ktls, sis_ktls;
1471e0c4386eSCy Schubert 
1472e0c4386eSCy Schubert     OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
1473e0c4386eSCy Schubert     cipher = &ktls_test_ciphers[test / 4];
1474e0c4386eSCy Schubert 
1475e0c4386eSCy Schubert     cis_ktls = (test & 1) != 0;
1476e0c4386eSCy Schubert     sis_ktls = (test & 2) != 0;
1477e0c4386eSCy Schubert 
1478e0c4386eSCy Schubert     return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
1479e0c4386eSCy Schubert                              cipher->cipher);
1480e0c4386eSCy Schubert }
1481e0c4386eSCy Schubert 
test_ktls_sendfile(int tst)1482e0c4386eSCy Schubert static int test_ktls_sendfile(int tst)
1483e0c4386eSCy Schubert {
1484e0c4386eSCy Schubert     struct ktls_test_cipher *cipher;
1485e0c4386eSCy Schubert 
1486e0c4386eSCy Schubert     OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
1487e0c4386eSCy Schubert     cipher = &ktls_test_ciphers[tst];
1488e0c4386eSCy Schubert 
1489e0c4386eSCy Schubert     return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher);
1490e0c4386eSCy Schubert }
1491e0c4386eSCy Schubert #endif
1492e0c4386eSCy Schubert 
test_large_message_tls(void)1493e0c4386eSCy Schubert static int test_large_message_tls(void)
1494e0c4386eSCy Schubert {
1495e0c4386eSCy Schubert     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1496e0c4386eSCy Schubert                                       TLS1_VERSION, 0, 0);
1497e0c4386eSCy Schubert }
1498e0c4386eSCy Schubert 
test_large_message_tls_read_ahead(void)1499e0c4386eSCy Schubert static int test_large_message_tls_read_ahead(void)
1500e0c4386eSCy Schubert {
1501e0c4386eSCy Schubert     return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1502e0c4386eSCy Schubert                                       TLS1_VERSION, 0, 1);
1503e0c4386eSCy Schubert }
1504e0c4386eSCy Schubert 
1505e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
test_large_message_dtls(void)1506e0c4386eSCy Schubert static int test_large_message_dtls(void)
1507e0c4386eSCy Schubert {
1508e0c4386eSCy Schubert # ifdef OPENSSL_NO_DTLS1_2
1509e0c4386eSCy Schubert     /* Not supported in the FIPS provider */
1510e0c4386eSCy Schubert     if (is_fips)
1511e0c4386eSCy Schubert         return 1;
1512e0c4386eSCy Schubert # endif
1513e0c4386eSCy Schubert     /*
1514e0c4386eSCy Schubert      * read_ahead is not relevant to DTLS because DTLS always acts as if
1515e0c4386eSCy Schubert      * read_ahead is set.
1516e0c4386eSCy Schubert      */
1517e0c4386eSCy Schubert     return execute_test_large_message(DTLS_server_method(),
1518e0c4386eSCy Schubert                                       DTLS_client_method(),
1519e0c4386eSCy Schubert                                       DTLS1_VERSION, 0, 0);
1520e0c4386eSCy Schubert }
1521e0c4386eSCy Schubert #endif
1522e0c4386eSCy Schubert 
1523e0c4386eSCy Schubert /*
1524e0c4386eSCy Schubert  * Test we can successfully send the maximum amount of application data. We
1525e0c4386eSCy Schubert  * test each protocol version individually, each with and without EtM enabled.
1526e0c4386eSCy Schubert  * TLSv1.3 doesn't use EtM so technically it is redundant to test both but it is
1527e0c4386eSCy Schubert  * simpler this way. We also test all combinations with and without the
1528e0c4386eSCy Schubert  * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS option which affects the size of the
1529e0c4386eSCy Schubert  * underlying buffer.
1530e0c4386eSCy Schubert  */
test_large_app_data(int tst)1531e0c4386eSCy Schubert static int test_large_app_data(int tst)
1532e0c4386eSCy Schubert {
1533e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1534e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1535e0c4386eSCy Schubert     int testresult = 0, prot;
1536e0c4386eSCy Schubert     unsigned char *msg, *buf = NULL;
1537e0c4386eSCy Schubert     size_t written, readbytes;
1538e0c4386eSCy Schubert     const SSL_METHOD *smeth = TLS_server_method();
1539e0c4386eSCy Schubert     const SSL_METHOD *cmeth = TLS_client_method();
1540e0c4386eSCy Schubert 
1541e0c4386eSCy Schubert     switch (tst >> 2) {
1542e0c4386eSCy Schubert     case 0:
1543e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
1544e0c4386eSCy Schubert         prot = TLS1_3_VERSION;
1545e0c4386eSCy Schubert         break;
1546e0c4386eSCy Schubert #else
1547e0c4386eSCy Schubert         return 1;
1548e0c4386eSCy Schubert #endif
1549e0c4386eSCy Schubert 
1550e0c4386eSCy Schubert     case 1:
1551e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
1552e0c4386eSCy Schubert         prot = TLS1_2_VERSION;
1553e0c4386eSCy Schubert         break;
1554e0c4386eSCy Schubert #else
1555e0c4386eSCy Schubert         return 1;
1556e0c4386eSCy Schubert #endif
1557e0c4386eSCy Schubert 
1558e0c4386eSCy Schubert     case 2:
1559e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_1
1560e0c4386eSCy Schubert         prot = TLS1_1_VERSION;
1561e0c4386eSCy Schubert         break;
1562e0c4386eSCy Schubert #else
1563e0c4386eSCy Schubert         return 1;
1564e0c4386eSCy Schubert #endif
1565e0c4386eSCy Schubert 
1566e0c4386eSCy Schubert     case 3:
1567e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1
1568e0c4386eSCy Schubert         prot = TLS1_VERSION;
1569e0c4386eSCy Schubert         break;
1570e0c4386eSCy Schubert #else
1571e0c4386eSCy Schubert         return 1;
1572e0c4386eSCy Schubert #endif
1573e0c4386eSCy Schubert 
1574e0c4386eSCy Schubert     case 4:
1575e0c4386eSCy Schubert #ifndef OPENSSL_NO_SSL3
1576e0c4386eSCy Schubert         prot = SSL3_VERSION;
1577e0c4386eSCy Schubert         break;
1578e0c4386eSCy Schubert #else
1579e0c4386eSCy Schubert         return 1;
1580e0c4386eSCy Schubert #endif
1581e0c4386eSCy Schubert 
1582e0c4386eSCy Schubert     case 5:
1583e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1_2
1584e0c4386eSCy Schubert         prot = DTLS1_2_VERSION;
1585e0c4386eSCy Schubert         smeth = DTLS_server_method();
1586e0c4386eSCy Schubert         cmeth = DTLS_client_method();
1587e0c4386eSCy Schubert         break;
1588e0c4386eSCy Schubert #else
1589e0c4386eSCy Schubert         return 1;
1590e0c4386eSCy Schubert #endif
1591e0c4386eSCy Schubert 
1592e0c4386eSCy Schubert     case 6:
1593e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1
1594e0c4386eSCy Schubert         prot = DTLS1_VERSION;
1595e0c4386eSCy Schubert         smeth = DTLS_server_method();
1596e0c4386eSCy Schubert         cmeth = DTLS_client_method();
1597e0c4386eSCy Schubert         break;
1598e0c4386eSCy Schubert #else
1599e0c4386eSCy Schubert         return 1;
1600e0c4386eSCy Schubert #endif
1601e0c4386eSCy Schubert 
1602e0c4386eSCy Schubert     default:
1603e0c4386eSCy Schubert         /* Shouldn't happen */
1604e0c4386eSCy Schubert         return 0;
1605e0c4386eSCy Schubert     }
1606e0c4386eSCy Schubert 
1607e0c4386eSCy Schubert     if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips)
1608e0c4386eSCy Schubert         return 1;
1609e0c4386eSCy Schubert 
1610e0c4386eSCy Schubert     /* Maximal sized message of zeros */
1611e0c4386eSCy Schubert     msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH);
1612e0c4386eSCy Schubert     if (!TEST_ptr(msg))
1613e0c4386eSCy Schubert         goto end;
1614e0c4386eSCy Schubert 
1615e0c4386eSCy Schubert     buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1);
1616e0c4386eSCy Schubert     if (!TEST_ptr(buf))
1617e0c4386eSCy Schubert         goto end;
1618e0c4386eSCy Schubert     /* Set whole buffer to all bits set */
1619e0c4386eSCy Schubert     memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1);
1620e0c4386eSCy Schubert 
1621e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot,
1622e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
1623e0c4386eSCy Schubert         goto end;
1624e0c4386eSCy Schubert 
1625e0c4386eSCy Schubert     if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) {
1626e0c4386eSCy Schubert         /* Older protocol versions need SECLEVEL=0 due to SHA1 usage */
1627e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))
1628e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_cipher_list(sctx,
1629e0c4386eSCy Schubert                                                       "DEFAULT:@SECLEVEL=0")))
1630e0c4386eSCy Schubert         goto end;
1631e0c4386eSCy Schubert     }
1632e0c4386eSCy Schubert 
1633e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1634e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
1635e0c4386eSCy Schubert         goto end;
1636e0c4386eSCy Schubert 
1637e0c4386eSCy Schubert     if ((tst & 1) != 0) {
1638e0c4386eSCy Schubert         /* Setting this option gives us a minimally sized underlying buffer */
1639e0c4386eSCy Schubert         if (!TEST_true(SSL_set_options(serverssl,
1640e0c4386eSCy Schubert                                        SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
1641e0c4386eSCy Schubert                 || !TEST_true(SSL_set_options(clientssl,
1642e0c4386eSCy Schubert                                               SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)))
1643e0c4386eSCy Schubert             goto end;
1644e0c4386eSCy Schubert     }
1645e0c4386eSCy Schubert 
1646e0c4386eSCy Schubert     if ((tst & 2) != 0) {
1647e0c4386eSCy Schubert         /*
1648e0c4386eSCy Schubert          * Setting this option means the MAC is added before encryption
1649e0c4386eSCy Schubert          * giving us a larger record for the encryption process
1650e0c4386eSCy Schubert          */
1651e0c4386eSCy Schubert         if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC))
1652e0c4386eSCy Schubert                 || !TEST_true(SSL_set_options(clientssl,
1653e0c4386eSCy Schubert                                               SSL_OP_NO_ENCRYPT_THEN_MAC)))
1654e0c4386eSCy Schubert             goto end;
1655e0c4386eSCy Schubert     }
1656e0c4386eSCy Schubert 
1657e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1658e0c4386eSCy Schubert         goto end;
1659e0c4386eSCy Schubert 
1660e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH,
1661e0c4386eSCy Schubert                                 &written))
1662e0c4386eSCy Schubert             || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH))
1663e0c4386eSCy Schubert         goto end;
1664e0c4386eSCy Schubert 
1665e0c4386eSCy Schubert     /* We provide a buffer slightly larger than what we are actually expecting */
1666e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1,
1667e0c4386eSCy Schubert                                &readbytes)))
1668e0c4386eSCy Schubert         goto end;
1669e0c4386eSCy Schubert 
1670e0c4386eSCy Schubert     if (!TEST_mem_eq(msg, written, buf, readbytes))
1671e0c4386eSCy Schubert         goto end;
1672e0c4386eSCy Schubert 
1673e0c4386eSCy Schubert     testresult = 1;
1674e0c4386eSCy Schubert end:
1675e0c4386eSCy Schubert     OPENSSL_free(msg);
1676e0c4386eSCy Schubert     OPENSSL_free(buf);
1677e0c4386eSCy Schubert     SSL_free(serverssl);
1678e0c4386eSCy Schubert     SSL_free(clientssl);
1679e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1680e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1681e0c4386eSCy Schubert     return testresult;
1682e0c4386eSCy Schubert }
1683e0c4386eSCy Schubert 
1684e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) \
1685e0c4386eSCy Schubert     || !defined(OPENSSL_NO_DTLS)
execute_cleanse_plaintext(const SSL_METHOD * smeth,const SSL_METHOD * cmeth,int min_version,int max_version)1686e0c4386eSCy Schubert static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
1687e0c4386eSCy Schubert                                      const SSL_METHOD *cmeth,
1688e0c4386eSCy Schubert                                      int min_version, int max_version)
1689e0c4386eSCy Schubert {
1690e0c4386eSCy Schubert     size_t i;
1691e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1692e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1693e0c4386eSCy Schubert     int testresult = 0;
1694e0c4386eSCy Schubert     SSL3_RECORD *rr;
1695e0c4386eSCy Schubert     void *zbuf;
1696e0c4386eSCy Schubert 
1697e0c4386eSCy Schubert     static unsigned char cbuf[16000];
1698e0c4386eSCy Schubert     static unsigned char sbuf[16000];
1699e0c4386eSCy Schubert 
1700e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx,
1701e0c4386eSCy Schubert                                        smeth, cmeth,
1702e0c4386eSCy Schubert                                        min_version, max_version,
1703e0c4386eSCy Schubert                                        &sctx, &cctx, cert,
1704e0c4386eSCy Schubert                                        privkey)))
1705e0c4386eSCy Schubert         goto end;
1706e0c4386eSCy Schubert 
1707e0c4386eSCy Schubert # ifdef OPENSSL_NO_DTLS1_2
1708e0c4386eSCy Schubert     if (smeth == DTLS_server_method()) {
1709e0c4386eSCy Schubert         /* Not supported in the FIPS provider */
1710e0c4386eSCy Schubert         if (is_fips) {
1711e0c4386eSCy Schubert             testresult = 1;
1712e0c4386eSCy Schubert             goto end;
1713e0c4386eSCy Schubert         };
1714e0c4386eSCy Schubert         /*
1715e0c4386eSCy Schubert          * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1716e0c4386eSCy Schubert          * level 0
1717e0c4386eSCy Schubert          */
1718e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1719e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1720e0c4386eSCy Schubert                                                     "DEFAULT:@SECLEVEL=0")))
1721e0c4386eSCy Schubert             goto end;
1722e0c4386eSCy Schubert     }
1723e0c4386eSCy Schubert # endif
1724e0c4386eSCy Schubert 
1725e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1726e0c4386eSCy Schubert                                       NULL, NULL)))
1727e0c4386eSCy Schubert         goto end;
1728e0c4386eSCy Schubert 
1729e0c4386eSCy Schubert     if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
1730e0c4386eSCy Schubert         goto end;
1731e0c4386eSCy Schubert 
1732e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1733e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
1734e0c4386eSCy Schubert         goto end;
1735e0c4386eSCy Schubert 
1736e0c4386eSCy Schubert     for (i = 0; i < sizeof(cbuf); i++) {
1737e0c4386eSCy Schubert         cbuf[i] = i & 0xff;
1738e0c4386eSCy Schubert     }
1739e0c4386eSCy Schubert 
1740e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
1741e0c4386eSCy Schubert         goto end;
1742e0c4386eSCy Schubert 
1743e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1744e0c4386eSCy Schubert         goto end;
1745e0c4386eSCy Schubert 
1746e0c4386eSCy Schubert     if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1747e0c4386eSCy Schubert         goto end;
1748e0c4386eSCy Schubert 
1749e0c4386eSCy Schubert     /*
1750e0c4386eSCy Schubert      * Since we called SSL_peek(), we know the data in the record
1751e0c4386eSCy Schubert      * layer is a plaintext record. We can gather the pointer to check
1752e0c4386eSCy Schubert      * for zeroization after SSL_read().
1753e0c4386eSCy Schubert      */
1754e0c4386eSCy Schubert     rr = serverssl->rlayer.rrec;
1755e0c4386eSCy Schubert     zbuf = &rr->data[rr->off];
1756e0c4386eSCy Schubert     if (!TEST_int_eq(rr->length, sizeof(cbuf)))
1757e0c4386eSCy Schubert         goto end;
1758e0c4386eSCy Schubert 
1759e0c4386eSCy Schubert     /*
1760e0c4386eSCy Schubert      * After SSL_peek() the plaintext must still be stored in the
1761e0c4386eSCy Schubert      * record.
1762e0c4386eSCy Schubert      */
1763e0c4386eSCy Schubert     if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1764e0c4386eSCy Schubert         goto end;
1765e0c4386eSCy Schubert 
1766e0c4386eSCy Schubert     memset(sbuf, 0, sizeof(sbuf));
1767e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1768e0c4386eSCy Schubert         goto end;
1769e0c4386eSCy Schubert 
1770e0c4386eSCy Schubert     if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
1771e0c4386eSCy Schubert         goto end;
1772e0c4386eSCy Schubert 
1773e0c4386eSCy Schubert     /* Check if rbuf is cleansed */
1774e0c4386eSCy Schubert     memset(cbuf, 0, sizeof(cbuf));
1775e0c4386eSCy Schubert     if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1776e0c4386eSCy Schubert         goto end;
1777e0c4386eSCy Schubert 
1778e0c4386eSCy Schubert     testresult = 1;
1779e0c4386eSCy Schubert  end:
1780e0c4386eSCy Schubert     SSL_free(serverssl);
1781e0c4386eSCy Schubert     SSL_free(clientssl);
1782e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1783e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1784e0c4386eSCy Schubert 
1785e0c4386eSCy Schubert     return testresult;
1786e0c4386eSCy Schubert }
1787e0c4386eSCy Schubert #endif /*
1788e0c4386eSCy Schubert         * !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
1789e0c4386eSCy Schubert         * || !defined(OPENSSL_NO_DTLS)
1790e0c4386eSCy Schubert         */
1791e0c4386eSCy Schubert 
test_cleanse_plaintext(void)1792e0c4386eSCy Schubert static int test_cleanse_plaintext(void)
1793e0c4386eSCy Schubert {
1794e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2)
1795e0c4386eSCy Schubert     if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1796e0c4386eSCy Schubert                                              TLS_client_method(),
1797e0c4386eSCy Schubert                                              TLS1_2_VERSION,
1798e0c4386eSCy Schubert                                              TLS1_2_VERSION)))
1799e0c4386eSCy Schubert         return 0;
1800e0c4386eSCy Schubert 
1801e0c4386eSCy Schubert #endif
1802e0c4386eSCy Schubert 
1803e0c4386eSCy Schubert #if !defined(OSSL_NO_USABLE_TLS1_3)
1804e0c4386eSCy Schubert     if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1805e0c4386eSCy Schubert                                              TLS_client_method(),
1806e0c4386eSCy Schubert                                              TLS1_3_VERSION,
1807e0c4386eSCy Schubert                                              TLS1_3_VERSION)))
1808e0c4386eSCy Schubert         return 0;
1809e0c4386eSCy Schubert #endif
1810e0c4386eSCy Schubert 
1811e0c4386eSCy Schubert #if !defined(OPENSSL_NO_DTLS)
1812e0c4386eSCy Schubert 
1813e0c4386eSCy Schubert     if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
1814e0c4386eSCy Schubert                                              DTLS_client_method(),
1815e0c4386eSCy Schubert                                              DTLS1_VERSION,
1816e0c4386eSCy Schubert                                              0)))
1817e0c4386eSCy Schubert         return 0;
1818e0c4386eSCy Schubert #endif
1819e0c4386eSCy Schubert     return 1;
1820e0c4386eSCy Schubert }
1821e0c4386eSCy Schubert 
1822e0c4386eSCy Schubert #ifndef OPENSSL_NO_OCSP
ocsp_server_cb(SSL * s,void * arg)1823e0c4386eSCy Schubert static int ocsp_server_cb(SSL *s, void *arg)
1824e0c4386eSCy Schubert {
1825e0c4386eSCy Schubert     int *argi = (int *)arg;
1826e0c4386eSCy Schubert     unsigned char *copy = NULL;
1827e0c4386eSCy Schubert     STACK_OF(OCSP_RESPID) *ids = NULL;
1828e0c4386eSCy Schubert     OCSP_RESPID *id = NULL;
1829e0c4386eSCy Schubert 
1830e0c4386eSCy Schubert     if (*argi == 2) {
1831e0c4386eSCy Schubert         /* In this test we are expecting exactly 1 OCSP_RESPID */
1832e0c4386eSCy Schubert         SSL_get_tlsext_status_ids(s, &ids);
1833e0c4386eSCy Schubert         if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
1834e0c4386eSCy Schubert             return SSL_TLSEXT_ERR_ALERT_FATAL;
1835e0c4386eSCy Schubert 
1836e0c4386eSCy Schubert         id = sk_OCSP_RESPID_value(ids, 0);
1837e0c4386eSCy Schubert         if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
1838e0c4386eSCy Schubert             return SSL_TLSEXT_ERR_ALERT_FATAL;
1839e0c4386eSCy Schubert     } else if (*argi != 1) {
1840e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_ALERT_FATAL;
1841e0c4386eSCy Schubert     }
1842e0c4386eSCy Schubert 
1843e0c4386eSCy Schubert     if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
1844e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_ALERT_FATAL;
1845e0c4386eSCy Schubert 
1846e0c4386eSCy Schubert     if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
1847e0c4386eSCy Schubert                                                    sizeof(orespder)))) {
1848e0c4386eSCy Schubert         OPENSSL_free(copy);
1849e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_ALERT_FATAL;
1850e0c4386eSCy Schubert     }
1851e0c4386eSCy Schubert     ocsp_server_called = 1;
1852e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
1853e0c4386eSCy Schubert }
1854e0c4386eSCy Schubert 
ocsp_client_cb(SSL * s,void * arg)1855e0c4386eSCy Schubert static int ocsp_client_cb(SSL *s, void *arg)
1856e0c4386eSCy Schubert {
1857e0c4386eSCy Schubert     int *argi = (int *)arg;
1858e0c4386eSCy Schubert     const unsigned char *respderin;
1859e0c4386eSCy Schubert     size_t len;
1860e0c4386eSCy Schubert 
1861e0c4386eSCy Schubert     if (*argi != 1 && *argi != 2)
1862e0c4386eSCy Schubert         return 0;
1863e0c4386eSCy Schubert 
1864e0c4386eSCy Schubert     len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
1865e0c4386eSCy Schubert     if (!TEST_mem_eq(orespder, len, respderin, len))
1866e0c4386eSCy Schubert         return 0;
1867e0c4386eSCy Schubert 
1868e0c4386eSCy Schubert     ocsp_client_called = 1;
1869e0c4386eSCy Schubert     return 1;
1870e0c4386eSCy Schubert }
1871e0c4386eSCy Schubert 
test_tlsext_status_type(void)1872e0c4386eSCy Schubert static int test_tlsext_status_type(void)
1873e0c4386eSCy Schubert {
1874e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
1875e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
1876e0c4386eSCy Schubert     int testresult = 0;
1877e0c4386eSCy Schubert     STACK_OF(OCSP_RESPID) *ids = NULL;
1878e0c4386eSCy Schubert     OCSP_RESPID *id = NULL;
1879e0c4386eSCy Schubert     BIO *certbio = NULL;
1880e0c4386eSCy Schubert 
1881e0c4386eSCy Schubert     if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
1882e0c4386eSCy Schubert                              TLS1_VERSION, 0,
1883e0c4386eSCy Schubert                              &sctx, &cctx, cert, privkey))
1884e0c4386eSCy Schubert         return 0;
1885e0c4386eSCy Schubert 
1886e0c4386eSCy Schubert     if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
1887e0c4386eSCy Schubert         goto end;
1888e0c4386eSCy Schubert 
1889e0c4386eSCy Schubert     /* First just do various checks getting and setting tlsext_status_type */
1890e0c4386eSCy Schubert 
1891e0c4386eSCy Schubert     clientssl = SSL_new(cctx);
1892e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
1893e0c4386eSCy Schubert             || !TEST_true(SSL_set_tlsext_status_type(clientssl,
1894e0c4386eSCy Schubert                                                       TLSEXT_STATUSTYPE_ocsp))
1895e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
1896e0c4386eSCy Schubert                             TLSEXT_STATUSTYPE_ocsp))
1897e0c4386eSCy Schubert         goto end;
1898e0c4386eSCy Schubert 
1899e0c4386eSCy Schubert     SSL_free(clientssl);
1900e0c4386eSCy Schubert     clientssl = NULL;
1901e0c4386eSCy Schubert 
1902e0c4386eSCy Schubert     if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
1903e0c4386eSCy Schubert      || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
1904e0c4386eSCy Schubert         goto end;
1905e0c4386eSCy Schubert 
1906e0c4386eSCy Schubert     clientssl = SSL_new(cctx);
1907e0c4386eSCy Schubert     if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
1908e0c4386eSCy Schubert         goto end;
1909e0c4386eSCy Schubert     SSL_free(clientssl);
1910e0c4386eSCy Schubert     clientssl = NULL;
1911e0c4386eSCy Schubert 
1912e0c4386eSCy Schubert     /*
1913e0c4386eSCy Schubert      * Now actually do a handshake and check OCSP information is exchanged and
1914e0c4386eSCy Schubert      * the callbacks get called
1915e0c4386eSCy Schubert      */
1916e0c4386eSCy Schubert     SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
1917e0c4386eSCy Schubert     SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
1918e0c4386eSCy Schubert     SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
1919e0c4386eSCy Schubert     SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
1920e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1921e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
1922e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
1923e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
1924e0c4386eSCy Schubert             || !TEST_true(ocsp_client_called)
1925e0c4386eSCy Schubert             || !TEST_true(ocsp_server_called))
1926e0c4386eSCy Schubert         goto end;
1927e0c4386eSCy Schubert     SSL_free(serverssl);
1928e0c4386eSCy Schubert     SSL_free(clientssl);
1929e0c4386eSCy Schubert     serverssl = NULL;
1930e0c4386eSCy Schubert     clientssl = NULL;
1931e0c4386eSCy Schubert 
1932e0c4386eSCy Schubert     /* Try again but this time force the server side callback to fail */
1933e0c4386eSCy Schubert     ocsp_client_called = 0;
1934e0c4386eSCy Schubert     ocsp_server_called = 0;
1935e0c4386eSCy Schubert     cdummyarg = 0;
1936e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1937e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
1938e0c4386eSCy Schubert                 /* This should fail because the callback will fail */
1939e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
1940e0c4386eSCy Schubert                                                  SSL_ERROR_NONE))
1941e0c4386eSCy Schubert             || !TEST_false(ocsp_client_called)
1942e0c4386eSCy Schubert             || !TEST_false(ocsp_server_called))
1943e0c4386eSCy Schubert         goto end;
1944e0c4386eSCy Schubert     SSL_free(serverssl);
1945e0c4386eSCy Schubert     SSL_free(clientssl);
1946e0c4386eSCy Schubert     serverssl = NULL;
1947e0c4386eSCy Schubert     clientssl = NULL;
1948e0c4386eSCy Schubert 
1949e0c4386eSCy Schubert     /*
1950e0c4386eSCy Schubert      * This time we'll get the client to send an OCSP_RESPID that it will
1951e0c4386eSCy Schubert      * accept.
1952e0c4386eSCy Schubert      */
1953e0c4386eSCy Schubert     ocsp_client_called = 0;
1954e0c4386eSCy Schubert     ocsp_server_called = 0;
1955e0c4386eSCy Schubert     cdummyarg = 2;
1956e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1957e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
1958e0c4386eSCy Schubert         goto end;
1959e0c4386eSCy Schubert 
1960e0c4386eSCy Schubert     /*
1961e0c4386eSCy Schubert      * We'll just use any old cert for this test - it doesn't have to be an OCSP
1962e0c4386eSCy Schubert      * specific one. We'll use the server cert.
1963e0c4386eSCy Schubert      */
1964e0c4386eSCy Schubert     if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
1965e0c4386eSCy Schubert             || !TEST_ptr(id = OCSP_RESPID_new())
1966e0c4386eSCy Schubert             || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
1967e0c4386eSCy Schubert             || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
1968e0c4386eSCy Schubert             || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
1969e0c4386eSCy Schubert             || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
1970e0c4386eSCy Schubert             || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
1971e0c4386eSCy Schubert         goto end;
1972e0c4386eSCy Schubert     id = NULL;
1973e0c4386eSCy Schubert     SSL_set_tlsext_status_ids(clientssl, ids);
1974e0c4386eSCy Schubert     /* Control has been transferred */
1975e0c4386eSCy Schubert     ids = NULL;
1976e0c4386eSCy Schubert 
1977e0c4386eSCy Schubert     BIO_free(certbio);
1978e0c4386eSCy Schubert     certbio = NULL;
1979e0c4386eSCy Schubert 
1980e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1981e0c4386eSCy Schubert                                          SSL_ERROR_NONE))
1982e0c4386eSCy Schubert             || !TEST_true(ocsp_client_called)
1983e0c4386eSCy Schubert             || !TEST_true(ocsp_server_called))
1984e0c4386eSCy Schubert         goto end;
1985e0c4386eSCy Schubert 
1986e0c4386eSCy Schubert     testresult = 1;
1987e0c4386eSCy Schubert 
1988e0c4386eSCy Schubert  end:
1989e0c4386eSCy Schubert     SSL_free(serverssl);
1990e0c4386eSCy Schubert     SSL_free(clientssl);
1991e0c4386eSCy Schubert     SSL_CTX_free(sctx);
1992e0c4386eSCy Schubert     SSL_CTX_free(cctx);
1993e0c4386eSCy Schubert     sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
1994e0c4386eSCy Schubert     OCSP_RESPID_free(id);
1995e0c4386eSCy Schubert     BIO_free(certbio);
1996e0c4386eSCy Schubert     X509_free(ocspcert);
1997e0c4386eSCy Schubert     ocspcert = NULL;
1998e0c4386eSCy Schubert 
1999e0c4386eSCy Schubert     return testresult;
2000e0c4386eSCy Schubert }
2001e0c4386eSCy Schubert #endif
2002e0c4386eSCy Schubert 
2003e0c4386eSCy Schubert #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
2004e0c4386eSCy Schubert static int new_called, remove_called, get_called;
2005e0c4386eSCy Schubert 
new_session_cb(SSL * ssl,SSL_SESSION * sess)2006e0c4386eSCy Schubert static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
2007e0c4386eSCy Schubert {
2008e0c4386eSCy Schubert     new_called++;
2009e0c4386eSCy Schubert     /*
2010e0c4386eSCy Schubert      * sess has been up-refed for us, but we don't actually need it so free it
2011e0c4386eSCy Schubert      * immediately.
2012e0c4386eSCy Schubert      */
2013e0c4386eSCy Schubert     SSL_SESSION_free(sess);
2014e0c4386eSCy Schubert     return 1;
2015e0c4386eSCy Schubert }
2016e0c4386eSCy Schubert 
remove_session_cb(SSL_CTX * ctx,SSL_SESSION * sess)2017e0c4386eSCy Schubert static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
2018e0c4386eSCy Schubert {
2019e0c4386eSCy Schubert     remove_called++;
2020e0c4386eSCy Schubert }
2021e0c4386eSCy Schubert 
2022e0c4386eSCy Schubert static SSL_SESSION *get_sess_val = NULL;
2023e0c4386eSCy Schubert 
get_session_cb(SSL * ssl,const unsigned char * id,int len,int * copy)2024e0c4386eSCy Schubert static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
2025e0c4386eSCy Schubert                                    int *copy)
2026e0c4386eSCy Schubert {
2027e0c4386eSCy Schubert     get_called++;
2028e0c4386eSCy Schubert     *copy = 1;
2029e0c4386eSCy Schubert     return get_sess_val;
2030e0c4386eSCy Schubert }
2031e0c4386eSCy Schubert 
execute_test_session(int maxprot,int use_int_cache,int use_ext_cache,long s_options)2032e0c4386eSCy Schubert static int execute_test_session(int maxprot, int use_int_cache,
2033e0c4386eSCy Schubert                                 int use_ext_cache, long s_options)
2034e0c4386eSCy Schubert {
2035e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
2036e0c4386eSCy Schubert     SSL *serverssl1 = NULL, *clientssl1 = NULL;
2037e0c4386eSCy Schubert     SSL *serverssl2 = NULL, *clientssl2 = NULL;
2038e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_1
2039e0c4386eSCy Schubert     SSL *serverssl3 = NULL, *clientssl3 = NULL;
2040e0c4386eSCy Schubert # endif
2041e0c4386eSCy Schubert     SSL_SESSION *sess1 = NULL, *sess2 = NULL;
2042e0c4386eSCy Schubert     int testresult = 0, numnewsesstick = 1;
2043e0c4386eSCy Schubert 
2044e0c4386eSCy Schubert     new_called = remove_called = 0;
2045e0c4386eSCy Schubert 
2046e0c4386eSCy Schubert     /* TLSv1.3 sends 2 NewSessionTickets */
2047e0c4386eSCy Schubert     if (maxprot == TLS1_3_VERSION)
2048e0c4386eSCy Schubert         numnewsesstick = 2;
2049e0c4386eSCy Schubert 
2050e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2051e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
2052e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
2053e0c4386eSCy Schubert         return 0;
2054e0c4386eSCy Schubert 
2055e0c4386eSCy Schubert     /*
2056e0c4386eSCy Schubert      * Only allow the max protocol version so we can force a connection failure
2057e0c4386eSCy Schubert      * later
2058e0c4386eSCy Schubert      */
2059e0c4386eSCy Schubert     SSL_CTX_set_min_proto_version(cctx, maxprot);
2060e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(cctx, maxprot);
2061e0c4386eSCy Schubert 
2062e0c4386eSCy Schubert     /* Set up session cache */
2063e0c4386eSCy Schubert     if (use_ext_cache) {
2064e0c4386eSCy Schubert         SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2065e0c4386eSCy Schubert         SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
2066e0c4386eSCy Schubert     }
2067e0c4386eSCy Schubert     if (use_int_cache) {
2068e0c4386eSCy Schubert         /* Also covers instance where both are set */
2069e0c4386eSCy Schubert         SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
2070e0c4386eSCy Schubert     } else {
2071e0c4386eSCy Schubert         SSL_CTX_set_session_cache_mode(cctx,
2072e0c4386eSCy Schubert                                        SSL_SESS_CACHE_CLIENT
2073e0c4386eSCy Schubert                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2074e0c4386eSCy Schubert     }
2075e0c4386eSCy Schubert 
2076e0c4386eSCy Schubert     if (s_options) {
2077e0c4386eSCy Schubert         SSL_CTX_set_options(sctx, s_options);
2078e0c4386eSCy Schubert     }
2079e0c4386eSCy Schubert 
2080e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2081e0c4386eSCy Schubert                                       NULL, NULL))
2082e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2083e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2084e0c4386eSCy Schubert             || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
2085e0c4386eSCy Schubert         goto end;
2086e0c4386eSCy Schubert 
2087e0c4386eSCy Schubert     /* Should fail because it should already be in the cache */
2088e0c4386eSCy Schubert     if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
2089e0c4386eSCy Schubert         goto end;
2090e0c4386eSCy Schubert     if (use_ext_cache
2091e0c4386eSCy Schubert             && (!TEST_int_eq(new_called, numnewsesstick)
2092e0c4386eSCy Schubert 
2093e0c4386eSCy Schubert                 || !TEST_int_eq(remove_called, 0)))
2094e0c4386eSCy Schubert         goto end;
2095e0c4386eSCy Schubert 
2096e0c4386eSCy Schubert     new_called = remove_called = 0;
2097e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2098e0c4386eSCy Schubert                                       &clientssl2, NULL, NULL))
2099e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl2, sess1))
2100e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2101e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2102e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl2)))
2103e0c4386eSCy Schubert         goto end;
2104e0c4386eSCy Schubert 
2105e0c4386eSCy Schubert     if (maxprot == TLS1_3_VERSION) {
2106e0c4386eSCy Schubert         /*
2107e0c4386eSCy Schubert          * In TLSv1.3 we should have created a new session even though we have
2108e0c4386eSCy Schubert          * resumed. Since we attempted a resume we should also have removed the
2109e0c4386eSCy Schubert          * old ticket from the cache so that we try to only use tickets once.
2110e0c4386eSCy Schubert          */
2111e0c4386eSCy Schubert         if (use_ext_cache
2112e0c4386eSCy Schubert                 && (!TEST_int_eq(new_called, 1)
2113e0c4386eSCy Schubert                     || !TEST_int_eq(remove_called, 1)))
2114e0c4386eSCy Schubert             goto end;
2115e0c4386eSCy Schubert     } else {
2116e0c4386eSCy Schubert         /*
2117e0c4386eSCy Schubert          * In TLSv1.2 we expect to have resumed so no sessions added or
2118e0c4386eSCy Schubert          * removed.
2119e0c4386eSCy Schubert          */
2120e0c4386eSCy Schubert         if (use_ext_cache
2121e0c4386eSCy Schubert                 && (!TEST_int_eq(new_called, 0)
2122e0c4386eSCy Schubert                     || !TEST_int_eq(remove_called, 0)))
2123e0c4386eSCy Schubert             goto end;
2124e0c4386eSCy Schubert     }
2125e0c4386eSCy Schubert 
2126e0c4386eSCy Schubert     SSL_SESSION_free(sess1);
2127e0c4386eSCy Schubert     if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
2128e0c4386eSCy Schubert         goto end;
2129e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl2, clientssl2);
2130e0c4386eSCy Schubert     serverssl2 = clientssl2 = NULL;
2131e0c4386eSCy Schubert 
2132e0c4386eSCy Schubert     new_called = remove_called = 0;
2133e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2134e0c4386eSCy Schubert                                       &clientssl2, NULL, NULL))
2135e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2136e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
2137e0c4386eSCy Schubert         goto end;
2138e0c4386eSCy Schubert 
2139e0c4386eSCy Schubert     if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
2140e0c4386eSCy Schubert         goto end;
2141e0c4386eSCy Schubert 
2142e0c4386eSCy Schubert     if (use_ext_cache
2143e0c4386eSCy Schubert             && (!TEST_int_eq(new_called, numnewsesstick)
2144e0c4386eSCy Schubert                 || !TEST_int_eq(remove_called, 0)))
2145e0c4386eSCy Schubert         goto end;
2146e0c4386eSCy Schubert 
2147e0c4386eSCy Schubert     new_called = remove_called = 0;
2148e0c4386eSCy Schubert     /*
2149e0c4386eSCy Schubert      * This should clear sess2 from the cache because it is a "bad" session.
2150e0c4386eSCy Schubert      * See SSL_set_session() documentation.
2151e0c4386eSCy Schubert      */
2152e0c4386eSCy Schubert     if (!TEST_true(SSL_set_session(clientssl2, sess1)))
2153e0c4386eSCy Schubert         goto end;
2154e0c4386eSCy Schubert     if (use_ext_cache
2155e0c4386eSCy Schubert             && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2156e0c4386eSCy Schubert         goto end;
2157e0c4386eSCy Schubert     if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
2158e0c4386eSCy Schubert         goto end;
2159e0c4386eSCy Schubert 
2160e0c4386eSCy Schubert     if (use_int_cache) {
2161e0c4386eSCy Schubert         /* Should succeeded because it should not already be in the cache */
2162e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
2163e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
2164e0c4386eSCy Schubert             goto end;
2165e0c4386eSCy Schubert     }
2166e0c4386eSCy Schubert 
2167e0c4386eSCy Schubert     new_called = remove_called = 0;
2168e0c4386eSCy Schubert     /* This shouldn't be in the cache so should fail */
2169e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
2170e0c4386eSCy Schubert         goto end;
2171e0c4386eSCy Schubert 
2172e0c4386eSCy Schubert     if (use_ext_cache
2173e0c4386eSCy Schubert             && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2174e0c4386eSCy Schubert         goto end;
2175e0c4386eSCy Schubert 
2176e0c4386eSCy Schubert # if !defined(OPENSSL_NO_TLS1_1)
2177e0c4386eSCy Schubert     new_called = remove_called = 0;
2178e0c4386eSCy Schubert     /* Force a connection failure */
2179e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
2180e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
2181e0c4386eSCy Schubert                                       &clientssl3, NULL, NULL))
2182e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl3, sess1))
2183e0c4386eSCy Schubert             /* This should fail because of the mismatched protocol versions */
2184e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
2185e0c4386eSCy Schubert                                                  SSL_ERROR_NONE)))
2186e0c4386eSCy Schubert         goto end;
2187e0c4386eSCy Schubert 
2188e0c4386eSCy Schubert     /* We should have automatically removed the session from the cache */
2189e0c4386eSCy Schubert     if (use_ext_cache
2190e0c4386eSCy Schubert             && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2191e0c4386eSCy Schubert         goto end;
2192e0c4386eSCy Schubert 
2193e0c4386eSCy Schubert     /* Should succeed because it should not already be in the cache */
2194e0c4386eSCy Schubert     if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
2195e0c4386eSCy Schubert         goto end;
2196e0c4386eSCy Schubert # endif
2197e0c4386eSCy Schubert 
2198e0c4386eSCy Schubert     /* Now do some tests for server side caching */
2199e0c4386eSCy Schubert     if (use_ext_cache) {
2200e0c4386eSCy Schubert         SSL_CTX_sess_set_new_cb(cctx, NULL);
2201e0c4386eSCy Schubert         SSL_CTX_sess_set_remove_cb(cctx, NULL);
2202e0c4386eSCy Schubert         SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2203e0c4386eSCy Schubert         SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
2204e0c4386eSCy Schubert         SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
2205e0c4386eSCy Schubert         get_sess_val = NULL;
2206e0c4386eSCy Schubert     }
2207e0c4386eSCy Schubert 
2208e0c4386eSCy Schubert     SSL_CTX_set_session_cache_mode(cctx, 0);
2209e0c4386eSCy Schubert     /* Internal caching is the default on the server side */
2210e0c4386eSCy Schubert     if (!use_int_cache)
2211e0c4386eSCy Schubert         SSL_CTX_set_session_cache_mode(sctx,
2212e0c4386eSCy Schubert                                        SSL_SESS_CACHE_SERVER
2213e0c4386eSCy Schubert                                        | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2214e0c4386eSCy Schubert 
2215e0c4386eSCy Schubert     SSL_free(serverssl1);
2216e0c4386eSCy Schubert     SSL_free(clientssl1);
2217e0c4386eSCy Schubert     serverssl1 = clientssl1 = NULL;
2218e0c4386eSCy Schubert     SSL_free(serverssl2);
2219e0c4386eSCy Schubert     SSL_free(clientssl2);
2220e0c4386eSCy Schubert     serverssl2 = clientssl2 = NULL;
2221e0c4386eSCy Schubert     SSL_SESSION_free(sess1);
2222e0c4386eSCy Schubert     sess1 = NULL;
2223e0c4386eSCy Schubert     SSL_SESSION_free(sess2);
2224e0c4386eSCy Schubert     sess2 = NULL;
2225e0c4386eSCy Schubert 
2226e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(sctx, maxprot);
2227e0c4386eSCy Schubert     if (maxprot == TLS1_2_VERSION)
2228e0c4386eSCy Schubert         SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
2229e0c4386eSCy Schubert     new_called = remove_called = get_called = 0;
2230e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2231e0c4386eSCy Schubert                                       NULL, NULL))
2232e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2233e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2234e0c4386eSCy Schubert             || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
2235e0c4386eSCy Schubert             || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
2236e0c4386eSCy Schubert         goto end;
2237e0c4386eSCy Schubert 
2238e0c4386eSCy Schubert     if (use_int_cache) {
2239e0c4386eSCy Schubert         if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
2240e0c4386eSCy Schubert             /*
2241e0c4386eSCy Schubert              * In TLSv1.3 it should not have been added to the internal cache,
2242e0c4386eSCy Schubert              * except in the case where we also have an external cache (in that
2243e0c4386eSCy Schubert              * case it gets added to the cache in order to generate remove
2244e0c4386eSCy Schubert              * events after timeout).
2245e0c4386eSCy Schubert              */
2246e0c4386eSCy Schubert             if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
2247e0c4386eSCy Schubert                 goto end;
2248e0c4386eSCy Schubert         } else {
2249e0c4386eSCy Schubert             /* Should fail because it should already be in the cache */
2250e0c4386eSCy Schubert             if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
2251e0c4386eSCy Schubert                 goto end;
2252e0c4386eSCy Schubert         }
2253e0c4386eSCy Schubert     }
2254e0c4386eSCy Schubert 
2255e0c4386eSCy Schubert     if (use_ext_cache) {
2256e0c4386eSCy Schubert         SSL_SESSION *tmp = sess2;
2257e0c4386eSCy Schubert 
2258e0c4386eSCy Schubert         if (!TEST_int_eq(new_called, numnewsesstick)
2259e0c4386eSCy Schubert                 || !TEST_int_eq(remove_called, 0)
2260e0c4386eSCy Schubert                 || !TEST_int_eq(get_called, 0))
2261e0c4386eSCy Schubert             goto end;
2262e0c4386eSCy Schubert         /*
2263e0c4386eSCy Schubert          * Delete the session from the internal cache to force a lookup from
2264e0c4386eSCy Schubert          * the external cache. We take a copy first because
2265e0c4386eSCy Schubert          * SSL_CTX_remove_session() also marks the session as non-resumable.
2266e0c4386eSCy Schubert          */
2267e0c4386eSCy Schubert         if (use_int_cache && maxprot != TLS1_3_VERSION) {
2268e0c4386eSCy Schubert             if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
2269e0c4386eSCy Schubert                 || !TEST_true(sess2->owner != NULL)
2270e0c4386eSCy Schubert                 || !TEST_true(tmp->owner == NULL)
2271e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
2272e0c4386eSCy Schubert                 goto end;
2273e0c4386eSCy Schubert             SSL_SESSION_free(sess2);
2274e0c4386eSCy Schubert         }
2275e0c4386eSCy Schubert         sess2 = tmp;
2276e0c4386eSCy Schubert     }
2277e0c4386eSCy Schubert 
2278e0c4386eSCy Schubert     new_called = remove_called = get_called = 0;
2279e0c4386eSCy Schubert     get_sess_val = sess2;
2280e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2281e0c4386eSCy Schubert                                       &clientssl2, NULL, NULL))
2282e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl2, sess1))
2283e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2284e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2285e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl2)))
2286e0c4386eSCy Schubert         goto end;
2287e0c4386eSCy Schubert 
2288e0c4386eSCy Schubert     if (use_ext_cache) {
2289e0c4386eSCy Schubert         if (!TEST_int_eq(remove_called, 0))
2290e0c4386eSCy Schubert             goto end;
2291e0c4386eSCy Schubert 
2292e0c4386eSCy Schubert         if (maxprot == TLS1_3_VERSION) {
2293e0c4386eSCy Schubert             if (!TEST_int_eq(new_called, 1)
2294e0c4386eSCy Schubert                     || !TEST_int_eq(get_called, 0))
2295e0c4386eSCy Schubert                 goto end;
2296e0c4386eSCy Schubert         } else {
2297e0c4386eSCy Schubert             if (!TEST_int_eq(new_called, 0)
2298e0c4386eSCy Schubert                     || !TEST_int_eq(get_called, 1))
2299e0c4386eSCy Schubert                 goto end;
2300e0c4386eSCy Schubert         }
2301e0c4386eSCy Schubert     }
2302e0c4386eSCy Schubert     /*
2303e0c4386eSCy Schubert      * Make a small cache, force out all other sessions but
2304e0c4386eSCy Schubert      * sess2, try to add sess1, which should succeed. Then
2305e0c4386eSCy Schubert      * make sure it's there by checking the owners. Despite
2306e0c4386eSCy Schubert      * the timeouts, sess1 should have kicked out sess2
2307e0c4386eSCy Schubert      */
2308e0c4386eSCy Schubert 
2309e0c4386eSCy Schubert     /* Make sess1 expire before sess2 */
2310e0c4386eSCy Schubert     if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
2311e0c4386eSCy Schubert             || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
2312e0c4386eSCy Schubert             || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
2313e0c4386eSCy Schubert             || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
2314e0c4386eSCy Schubert         goto end;
2315e0c4386eSCy Schubert 
2316e0c4386eSCy Schubert     if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
2317e0c4386eSCy Schubert         goto end;
2318e0c4386eSCy Schubert 
2319e0c4386eSCy Schubert     /* Don't care about results - cache should only be sess2 at end */
2320e0c4386eSCy Schubert     SSL_CTX_add_session(sctx, sess1);
2321e0c4386eSCy Schubert     SSL_CTX_add_session(sctx, sess2);
2322e0c4386eSCy Schubert 
2323e0c4386eSCy Schubert     /* Now add sess1, and make sure it remains, despite timeout */
2324e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
2325e0c4386eSCy Schubert             || !TEST_ptr(sess1->owner)
2326e0c4386eSCy Schubert             || !TEST_ptr_null(sess2->owner))
2327e0c4386eSCy Schubert         goto end;
2328e0c4386eSCy Schubert 
2329e0c4386eSCy Schubert     testresult = 1;
2330e0c4386eSCy Schubert 
2331e0c4386eSCy Schubert  end:
2332e0c4386eSCy Schubert     SSL_free(serverssl1);
2333e0c4386eSCy Schubert     SSL_free(clientssl1);
2334e0c4386eSCy Schubert     SSL_free(serverssl2);
2335e0c4386eSCy Schubert     SSL_free(clientssl2);
2336e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_1
2337e0c4386eSCy Schubert     SSL_free(serverssl3);
2338e0c4386eSCy Schubert     SSL_free(clientssl3);
2339e0c4386eSCy Schubert # endif
2340e0c4386eSCy Schubert     SSL_SESSION_free(sess1);
2341e0c4386eSCy Schubert     SSL_SESSION_free(sess2);
2342e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2343e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2344e0c4386eSCy Schubert 
2345e0c4386eSCy Schubert     return testresult;
2346e0c4386eSCy Schubert }
2347e0c4386eSCy Schubert #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
2348e0c4386eSCy Schubert 
test_session_with_only_int_cache(void)2349e0c4386eSCy Schubert static int test_session_with_only_int_cache(void)
2350e0c4386eSCy Schubert {
2351e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
2352e0c4386eSCy Schubert     if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
2353e0c4386eSCy Schubert         return 0;
2354e0c4386eSCy Schubert #endif
2355e0c4386eSCy Schubert 
2356e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
2357e0c4386eSCy Schubert     return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
2358e0c4386eSCy Schubert #else
2359e0c4386eSCy Schubert     return 1;
2360e0c4386eSCy Schubert #endif
2361e0c4386eSCy Schubert }
2362e0c4386eSCy Schubert 
test_session_with_only_ext_cache(void)2363e0c4386eSCy Schubert static int test_session_with_only_ext_cache(void)
2364e0c4386eSCy Schubert {
2365e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
2366e0c4386eSCy Schubert     if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
2367e0c4386eSCy Schubert         return 0;
2368e0c4386eSCy Schubert #endif
2369e0c4386eSCy Schubert 
2370e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
2371e0c4386eSCy Schubert     return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
2372e0c4386eSCy Schubert #else
2373e0c4386eSCy Schubert     return 1;
2374e0c4386eSCy Schubert #endif
2375e0c4386eSCy Schubert }
2376e0c4386eSCy Schubert 
test_session_with_both_cache(void)2377e0c4386eSCy Schubert static int test_session_with_both_cache(void)
2378e0c4386eSCy Schubert {
2379e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
2380e0c4386eSCy Schubert     if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
2381e0c4386eSCy Schubert         return 0;
2382e0c4386eSCy Schubert #endif
2383e0c4386eSCy Schubert 
2384e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
2385e0c4386eSCy Schubert     return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
2386e0c4386eSCy Schubert #else
2387e0c4386eSCy Schubert     return 1;
2388e0c4386eSCy Schubert #endif
2389e0c4386eSCy Schubert }
2390e0c4386eSCy Schubert 
test_session_wo_ca_names(void)2391e0c4386eSCy Schubert static int test_session_wo_ca_names(void)
2392e0c4386eSCy Schubert {
2393e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
2394e0c4386eSCy Schubert     if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
2395e0c4386eSCy Schubert         return 0;
2396e0c4386eSCy Schubert #endif
2397e0c4386eSCy Schubert 
2398e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
2399e0c4386eSCy Schubert     return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
2400e0c4386eSCy Schubert #else
2401e0c4386eSCy Schubert     return 1;
2402e0c4386eSCy Schubert #endif
2403e0c4386eSCy Schubert }
2404e0c4386eSCy Schubert 
2405e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
2406e0c4386eSCy Schubert static SSL_SESSION *sesscache[6];
2407e0c4386eSCy Schubert static int do_cache;
2408e0c4386eSCy Schubert 
new_cachesession_cb(SSL * ssl,SSL_SESSION * sess)2409e0c4386eSCy Schubert static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
2410e0c4386eSCy Schubert {
2411e0c4386eSCy Schubert     if (do_cache) {
2412e0c4386eSCy Schubert         sesscache[new_called] = sess;
2413e0c4386eSCy Schubert     } else {
2414e0c4386eSCy Schubert         /* We don't need the reference to the session, so free it */
2415e0c4386eSCy Schubert         SSL_SESSION_free(sess);
2416e0c4386eSCy Schubert     }
2417e0c4386eSCy Schubert     new_called++;
2418e0c4386eSCy Schubert 
2419e0c4386eSCy Schubert     return 1;
2420e0c4386eSCy Schubert }
2421e0c4386eSCy Schubert 
post_handshake_verify(SSL * sssl,SSL * cssl)2422e0c4386eSCy Schubert static int post_handshake_verify(SSL *sssl, SSL *cssl)
2423e0c4386eSCy Schubert {
2424e0c4386eSCy Schubert     SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
2425e0c4386eSCy Schubert     if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
2426e0c4386eSCy Schubert         return 0;
2427e0c4386eSCy Schubert 
2428e0c4386eSCy Schubert     /* Start handshake on the server and client */
2429e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
2430e0c4386eSCy Schubert             || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
2431e0c4386eSCy Schubert             || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
2432e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(sssl, cssl,
2433e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
2434e0c4386eSCy Schubert         return 0;
2435e0c4386eSCy Schubert 
2436e0c4386eSCy Schubert     return 1;
2437e0c4386eSCy Schubert }
2438e0c4386eSCy Schubert 
setup_ticket_test(int stateful,int idx,SSL_CTX ** sctx,SSL_CTX ** cctx)2439e0c4386eSCy Schubert static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
2440e0c4386eSCy Schubert                              SSL_CTX **cctx)
2441e0c4386eSCy Schubert {
2442e0c4386eSCy Schubert     int sess_id_ctx = 1;
2443e0c4386eSCy Schubert 
2444e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2445e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
2446e0c4386eSCy Schubert                                        sctx, cctx, cert, privkey))
2447e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
2448e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
2449e0c4386eSCy Schubert                                                          (void *)&sess_id_ctx,
2450e0c4386eSCy Schubert                                                          sizeof(sess_id_ctx))))
2451e0c4386eSCy Schubert         return 0;
2452e0c4386eSCy Schubert 
2453e0c4386eSCy Schubert     if (stateful)
2454e0c4386eSCy Schubert         SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
2455e0c4386eSCy Schubert 
2456e0c4386eSCy Schubert     SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
2457e0c4386eSCy Schubert                                           | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2458e0c4386eSCy Schubert     SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
2459e0c4386eSCy Schubert 
2460e0c4386eSCy Schubert     return 1;
2461e0c4386eSCy Schubert }
2462e0c4386eSCy Schubert 
check_resumption(int idx,SSL_CTX * sctx,SSL_CTX * cctx,int succ)2463e0c4386eSCy Schubert static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
2464e0c4386eSCy Schubert {
2465e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
2466e0c4386eSCy Schubert     int i;
2467e0c4386eSCy Schubert 
2468e0c4386eSCy Schubert     /* Test that we can resume with all the tickets we got given */
2469e0c4386eSCy Schubert     for (i = 0; i < idx * 2; i++) {
2470e0c4386eSCy Schubert         new_called = 0;
2471e0c4386eSCy Schubert         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2472e0c4386eSCy Schubert                                               &clientssl, NULL, NULL))
2473e0c4386eSCy Schubert                 || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
2474e0c4386eSCy Schubert             goto end;
2475e0c4386eSCy Schubert 
2476e0c4386eSCy Schubert         SSL_set_post_handshake_auth(clientssl, 1);
2477e0c4386eSCy Schubert 
2478e0c4386eSCy Schubert         if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2479e0c4386eSCy Schubert                                                     SSL_ERROR_NONE)))
2480e0c4386eSCy Schubert             goto end;
2481e0c4386eSCy Schubert 
2482e0c4386eSCy Schubert         /*
2483e0c4386eSCy Schubert          * Following a successful resumption we only get 1 ticket. After a
2484e0c4386eSCy Schubert          * failed one we should get idx tickets.
2485e0c4386eSCy Schubert          */
2486e0c4386eSCy Schubert         if (succ) {
2487e0c4386eSCy Schubert             if (!TEST_true(SSL_session_reused(clientssl))
2488e0c4386eSCy Schubert                     || !TEST_int_eq(new_called, 1))
2489e0c4386eSCy Schubert                 goto end;
2490e0c4386eSCy Schubert         } else {
2491e0c4386eSCy Schubert             if (!TEST_false(SSL_session_reused(clientssl))
2492e0c4386eSCy Schubert                     || !TEST_int_eq(new_called, idx))
2493e0c4386eSCy Schubert                 goto end;
2494e0c4386eSCy Schubert         }
2495e0c4386eSCy Schubert 
2496e0c4386eSCy Schubert         new_called = 0;
2497e0c4386eSCy Schubert         /* After a post-handshake authentication we should get 1 new ticket */
2498e0c4386eSCy Schubert         if (succ
2499e0c4386eSCy Schubert                 && (!post_handshake_verify(serverssl, clientssl)
2500e0c4386eSCy Schubert                     || !TEST_int_eq(new_called, 1)))
2501e0c4386eSCy Schubert             goto end;
2502e0c4386eSCy Schubert 
2503e0c4386eSCy Schubert         SSL_shutdown(clientssl);
2504e0c4386eSCy Schubert         SSL_shutdown(serverssl);
2505e0c4386eSCy Schubert         SSL_free(serverssl);
2506e0c4386eSCy Schubert         SSL_free(clientssl);
2507e0c4386eSCy Schubert         serverssl = clientssl = NULL;
2508e0c4386eSCy Schubert         SSL_SESSION_free(sesscache[i]);
2509e0c4386eSCy Schubert         sesscache[i] = NULL;
2510e0c4386eSCy Schubert     }
2511e0c4386eSCy Schubert 
2512e0c4386eSCy Schubert     return 1;
2513e0c4386eSCy Schubert 
2514e0c4386eSCy Schubert  end:
2515e0c4386eSCy Schubert     SSL_free(clientssl);
2516e0c4386eSCy Schubert     SSL_free(serverssl);
2517e0c4386eSCy Schubert     return 0;
2518e0c4386eSCy Schubert }
2519e0c4386eSCy Schubert 
test_tickets(int stateful,int idx)2520e0c4386eSCy Schubert static int test_tickets(int stateful, int idx)
2521e0c4386eSCy Schubert {
2522e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
2523e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
2524e0c4386eSCy Schubert     int testresult = 0;
2525e0c4386eSCy Schubert     size_t j;
2526e0c4386eSCy Schubert 
2527e0c4386eSCy Schubert     /* idx is the test number, but also the number of tickets we want */
2528e0c4386eSCy Schubert 
2529e0c4386eSCy Schubert     new_called = 0;
2530e0c4386eSCy Schubert     do_cache = 1;
2531e0c4386eSCy Schubert 
2532e0c4386eSCy Schubert     if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2533e0c4386eSCy Schubert         goto end;
2534e0c4386eSCy Schubert 
2535e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2536e0c4386eSCy Schubert                                           &clientssl, NULL, NULL)))
2537e0c4386eSCy Schubert         goto end;
2538e0c4386eSCy Schubert 
2539e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2540e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2541e0c4386eSCy Schubert                /* Check we got the number of tickets we were expecting */
2542e0c4386eSCy Schubert             || !TEST_int_eq(idx, new_called))
2543e0c4386eSCy Schubert         goto end;
2544e0c4386eSCy Schubert 
2545e0c4386eSCy Schubert     SSL_shutdown(clientssl);
2546e0c4386eSCy Schubert     SSL_shutdown(serverssl);
2547e0c4386eSCy Schubert     SSL_free(serverssl);
2548e0c4386eSCy Schubert     SSL_free(clientssl);
2549e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2550e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2551e0c4386eSCy Schubert     clientssl = serverssl = NULL;
2552e0c4386eSCy Schubert     sctx = cctx = NULL;
2553e0c4386eSCy Schubert 
2554e0c4386eSCy Schubert     /*
2555e0c4386eSCy Schubert      * Now we try to resume with the tickets we previously created. The
2556e0c4386eSCy Schubert      * resumption attempt is expected to fail (because we're now using a new
2557e0c4386eSCy Schubert      * SSL_CTX). We should see idx number of tickets issued again.
2558e0c4386eSCy Schubert      */
2559e0c4386eSCy Schubert 
2560e0c4386eSCy Schubert     /* Stop caching sessions - just count them */
2561e0c4386eSCy Schubert     do_cache = 0;
2562e0c4386eSCy Schubert 
2563e0c4386eSCy Schubert     if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2564e0c4386eSCy Schubert         goto end;
2565e0c4386eSCy Schubert 
2566e0c4386eSCy Schubert     if (!check_resumption(idx, sctx, cctx, 0))
2567e0c4386eSCy Schubert         goto end;
2568e0c4386eSCy Schubert 
2569e0c4386eSCy Schubert     /* Start again with caching sessions */
2570e0c4386eSCy Schubert     new_called = 0;
2571e0c4386eSCy Schubert     do_cache = 1;
2572e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2573e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2574e0c4386eSCy Schubert     sctx = cctx = NULL;
2575e0c4386eSCy Schubert 
2576e0c4386eSCy Schubert     if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2577e0c4386eSCy Schubert         goto end;
2578e0c4386eSCy Schubert 
2579e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2580e0c4386eSCy Schubert                                           &clientssl, NULL, NULL)))
2581e0c4386eSCy Schubert         goto end;
2582e0c4386eSCy Schubert 
2583e0c4386eSCy Schubert     SSL_set_post_handshake_auth(clientssl, 1);
2584e0c4386eSCy Schubert 
2585e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2586e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2587e0c4386eSCy Schubert                /* Check we got the number of tickets we were expecting */
2588e0c4386eSCy Schubert             || !TEST_int_eq(idx, new_called))
2589e0c4386eSCy Schubert         goto end;
2590e0c4386eSCy Schubert 
2591e0c4386eSCy Schubert     /* After a post-handshake authentication we should get new tickets issued */
2592e0c4386eSCy Schubert     if (!post_handshake_verify(serverssl, clientssl)
2593e0c4386eSCy Schubert             || !TEST_int_eq(idx * 2, new_called))
2594e0c4386eSCy Schubert         goto end;
2595e0c4386eSCy Schubert 
2596e0c4386eSCy Schubert     SSL_shutdown(clientssl);
2597e0c4386eSCy Schubert     SSL_shutdown(serverssl);
2598e0c4386eSCy Schubert     SSL_free(serverssl);
2599e0c4386eSCy Schubert     SSL_free(clientssl);
2600e0c4386eSCy Schubert     serverssl = clientssl = NULL;
2601e0c4386eSCy Schubert 
2602e0c4386eSCy Schubert     /* Stop caching sessions - just count them */
2603e0c4386eSCy Schubert     do_cache = 0;
2604e0c4386eSCy Schubert 
2605e0c4386eSCy Schubert     /*
2606e0c4386eSCy Schubert      * Check we can resume with all the tickets we created. This time around the
2607e0c4386eSCy Schubert      * resumptions should all be successful.
2608e0c4386eSCy Schubert      */
2609e0c4386eSCy Schubert     if (!check_resumption(idx, sctx, cctx, 1))
2610e0c4386eSCy Schubert         goto end;
2611e0c4386eSCy Schubert 
2612e0c4386eSCy Schubert     testresult = 1;
2613e0c4386eSCy Schubert 
2614e0c4386eSCy Schubert  end:
2615e0c4386eSCy Schubert     SSL_free(serverssl);
2616e0c4386eSCy Schubert     SSL_free(clientssl);
2617e0c4386eSCy Schubert     for (j = 0; j < OSSL_NELEM(sesscache); j++) {
2618e0c4386eSCy Schubert         SSL_SESSION_free(sesscache[j]);
2619e0c4386eSCy Schubert         sesscache[j] = NULL;
2620e0c4386eSCy Schubert     }
2621e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2622e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2623e0c4386eSCy Schubert 
2624e0c4386eSCy Schubert     return testresult;
2625e0c4386eSCy Schubert }
2626e0c4386eSCy Schubert 
test_stateless_tickets(int idx)2627e0c4386eSCy Schubert static int test_stateless_tickets(int idx)
2628e0c4386eSCy Schubert {
2629e0c4386eSCy Schubert     return test_tickets(0, idx);
2630e0c4386eSCy Schubert }
2631e0c4386eSCy Schubert 
test_stateful_tickets(int idx)2632e0c4386eSCy Schubert static int test_stateful_tickets(int idx)
2633e0c4386eSCy Schubert {
2634e0c4386eSCy Schubert     return test_tickets(1, idx);
2635e0c4386eSCy Schubert }
2636e0c4386eSCy Schubert 
test_psk_tickets(void)2637e0c4386eSCy Schubert static int test_psk_tickets(void)
2638e0c4386eSCy Schubert {
2639e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
2640e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
2641e0c4386eSCy Schubert     int testresult = 0;
2642e0c4386eSCy Schubert     int sess_id_ctx = 1;
2643e0c4386eSCy Schubert 
2644e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2645e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
2646e0c4386eSCy Schubert                                        &sctx, &cctx, NULL, NULL))
2647e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_session_id_context(sctx,
2648e0c4386eSCy Schubert                                                          (void *)&sess_id_ctx,
2649e0c4386eSCy Schubert                                                          sizeof(sess_id_ctx))))
2650e0c4386eSCy Schubert         goto end;
2651e0c4386eSCy Schubert 
2652e0c4386eSCy Schubert     SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
2653e0c4386eSCy Schubert                                          | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2654e0c4386eSCy Schubert     SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
2655e0c4386eSCy Schubert     SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
2656e0c4386eSCy Schubert     SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2657e0c4386eSCy Schubert     use_session_cb_cnt = 0;
2658e0c4386eSCy Schubert     find_session_cb_cnt = 0;
2659e0c4386eSCy Schubert     srvid = pskid;
2660e0c4386eSCy Schubert     new_called = 0;
2661e0c4386eSCy Schubert 
2662e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2663e0c4386eSCy Schubert                                       NULL, NULL)))
2664e0c4386eSCy Schubert         goto end;
2665e0c4386eSCy Schubert     clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH);
2666e0c4386eSCy Schubert     if (!TEST_ptr(clientpsk))
2667e0c4386eSCy Schubert         goto end;
2668e0c4386eSCy Schubert     SSL_SESSION_up_ref(clientpsk);
2669e0c4386eSCy Schubert 
2670e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2671e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2672e0c4386eSCy Schubert             || !TEST_int_eq(1, find_session_cb_cnt)
2673e0c4386eSCy Schubert             || !TEST_int_eq(1, use_session_cb_cnt)
2674e0c4386eSCy Schubert                /* We should always get 1 ticket when using external PSK */
2675e0c4386eSCy Schubert             || !TEST_int_eq(1, new_called))
2676e0c4386eSCy Schubert         goto end;
2677e0c4386eSCy Schubert 
2678e0c4386eSCy Schubert     testresult = 1;
2679e0c4386eSCy Schubert 
2680e0c4386eSCy Schubert  end:
2681e0c4386eSCy Schubert     SSL_free(serverssl);
2682e0c4386eSCy Schubert     SSL_free(clientssl);
2683e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2684e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2685e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
2686e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
2687e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
2688e0c4386eSCy Schubert 
2689e0c4386eSCy Schubert     return testresult;
2690e0c4386eSCy Schubert }
2691e0c4386eSCy Schubert 
test_extra_tickets(int idx)2692e0c4386eSCy Schubert static int test_extra_tickets(int idx)
2693e0c4386eSCy Schubert {
2694e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
2695e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
2696e0c4386eSCy Schubert     BIO *bretry = BIO_new(bio_s_always_retry());
2697e0c4386eSCy Schubert     BIO *tmp = NULL;
2698e0c4386eSCy Schubert     int testresult = 0;
2699e0c4386eSCy Schubert     int stateful = 0;
2700e0c4386eSCy Schubert     size_t nbytes;
2701e0c4386eSCy Schubert     unsigned char c, buf[1];
2702e0c4386eSCy Schubert 
2703e0c4386eSCy Schubert     new_called = 0;
2704e0c4386eSCy Schubert     do_cache = 1;
2705e0c4386eSCy Schubert 
2706e0c4386eSCy Schubert     if (idx >= 3) {
2707e0c4386eSCy Schubert         idx -= 3;
2708e0c4386eSCy Schubert         stateful = 1;
2709e0c4386eSCy Schubert     }
2710e0c4386eSCy Schubert 
2711e0c4386eSCy Schubert     if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
2712e0c4386eSCy Schubert         goto end;
2713e0c4386eSCy Schubert     SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2714e0c4386eSCy Schubert     /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
2715e0c4386eSCy Schubert     SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2716e0c4386eSCy Schubert 
2717e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2718e0c4386eSCy Schubert                                           &clientssl, NULL, NULL)))
2719e0c4386eSCy Schubert         goto end;
2720e0c4386eSCy Schubert 
2721e0c4386eSCy Schubert     /*
2722e0c4386eSCy Schubert      * Note that we have new_session_cb on both sctx and cctx, so new_called is
2723e0c4386eSCy Schubert      * incremented by both client and server.
2724e0c4386eSCy Schubert      */
2725e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2726e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
2727e0c4386eSCy Schubert                /* Check we got the number of tickets we were expecting */
2728e0c4386eSCy Schubert             || !TEST_int_eq(idx * 2, new_called)
2729e0c4386eSCy Schubert             || !TEST_true(SSL_new_session_ticket(serverssl))
2730e0c4386eSCy Schubert             || !TEST_true(SSL_new_session_ticket(serverssl))
2731e0c4386eSCy Schubert             || !TEST_int_eq(idx * 2, new_called))
2732e0c4386eSCy Schubert         goto end;
2733e0c4386eSCy Schubert 
2734e0c4386eSCy Schubert     /* Now try a (real) write to actually send the tickets */
2735e0c4386eSCy Schubert     c = '1';
2736e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2737e0c4386eSCy Schubert             || !TEST_size_t_eq(1, nbytes)
2738e0c4386eSCy Schubert             || !TEST_int_eq(idx * 2 + 2, new_called)
2739e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2740e0c4386eSCy Schubert             || !TEST_int_eq(idx * 2 + 4, new_called)
2741e0c4386eSCy Schubert             || !TEST_int_eq(sizeof(buf), nbytes)
2742e0c4386eSCy Schubert             || !TEST_int_eq(c, buf[0])
2743e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2744e0c4386eSCy Schubert         goto end;
2745e0c4386eSCy Schubert 
2746e0c4386eSCy Schubert     /* Try with only requesting one new ticket, too */
2747e0c4386eSCy Schubert     c = '2';
2748e0c4386eSCy Schubert     new_called = 0;
2749e0c4386eSCy Schubert     if (!TEST_true(SSL_new_session_ticket(serverssl))
2750e0c4386eSCy Schubert             || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
2751e0c4386eSCy Schubert             || !TEST_size_t_eq(sizeof(c), nbytes)
2752e0c4386eSCy Schubert             || !TEST_int_eq(1, new_called)
2753e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2754e0c4386eSCy Schubert             || !TEST_int_eq(2, new_called)
2755e0c4386eSCy Schubert             || !TEST_size_t_eq(sizeof(buf), nbytes)
2756e0c4386eSCy Schubert             || !TEST_int_eq(c, buf[0]))
2757e0c4386eSCy Schubert         goto end;
2758e0c4386eSCy Schubert 
2759e0c4386eSCy Schubert     /* Do it again but use dummy writes to drive the ticket generation */
2760e0c4386eSCy Schubert     c = '3';
2761e0c4386eSCy Schubert     new_called = 0;
2762e0c4386eSCy Schubert     if (!TEST_true(SSL_new_session_ticket(serverssl))
2763e0c4386eSCy Schubert             || !TEST_true(SSL_new_session_ticket(serverssl))
2764e0c4386eSCy Schubert             || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
2765e0c4386eSCy Schubert             || !TEST_size_t_eq(0, nbytes)
2766e0c4386eSCy Schubert             || !TEST_int_eq(2, new_called)
2767e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2768e0c4386eSCy Schubert             || !TEST_int_eq(4, new_called))
2769e0c4386eSCy Schubert         goto end;
2770e0c4386eSCy Schubert 
2771e0c4386eSCy Schubert     /* Once more, but with SSL_do_handshake() to drive the ticket generation */
2772e0c4386eSCy Schubert     c = '4';
2773e0c4386eSCy Schubert     new_called = 0;
2774e0c4386eSCy Schubert     if (!TEST_true(SSL_new_session_ticket(serverssl))
2775e0c4386eSCy Schubert             || !TEST_true(SSL_new_session_ticket(serverssl))
2776e0c4386eSCy Schubert             || !TEST_true(SSL_do_handshake(serverssl))
2777e0c4386eSCy Schubert             || !TEST_int_eq(2, new_called)
2778e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2779e0c4386eSCy Schubert             || !TEST_int_eq(4, new_called))
2780e0c4386eSCy Schubert         goto end;
2781e0c4386eSCy Schubert 
2782e0c4386eSCy Schubert     /*
2783e0c4386eSCy Schubert      * Use the always-retry BIO to exercise the logic that forces ticket
2784e0c4386eSCy Schubert      * generation to wait until a record boundary.
2785e0c4386eSCy Schubert      */
2786e0c4386eSCy Schubert     c = '5';
2787e0c4386eSCy Schubert     new_called = 0;
2788e0c4386eSCy Schubert     tmp = SSL_get_wbio(serverssl);
2789e0c4386eSCy Schubert     if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
2790e0c4386eSCy Schubert         tmp = NULL;
2791e0c4386eSCy Schubert         goto end;
2792e0c4386eSCy Schubert     }
2793e0c4386eSCy Schubert     SSL_set0_wbio(serverssl, bretry);
2794e0c4386eSCy Schubert     bretry = NULL;
2795e0c4386eSCy Schubert     if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
2796e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
2797e0c4386eSCy Schubert             || !TEST_size_t_eq(nbytes, 0))
2798e0c4386eSCy Schubert         goto end;
2799e0c4386eSCy Schubert     /* Restore a BIO that will let the write succeed */
2800e0c4386eSCy Schubert     SSL_set0_wbio(serverssl, tmp);
2801e0c4386eSCy Schubert     tmp = NULL;
2802e0c4386eSCy Schubert     /*
2803e0c4386eSCy Schubert      * These calls should just queue the request and not send anything
2804e0c4386eSCy Schubert      * even if we explicitly try to hit the state machine.
2805e0c4386eSCy Schubert      */
2806e0c4386eSCy Schubert     if (!TEST_true(SSL_new_session_ticket(serverssl))
2807e0c4386eSCy Schubert             || !TEST_true(SSL_new_session_ticket(serverssl))
2808e0c4386eSCy Schubert             || !TEST_int_eq(0, new_called)
2809e0c4386eSCy Schubert             || !TEST_true(SSL_do_handshake(serverssl))
2810e0c4386eSCy Schubert             || !TEST_int_eq(0, new_called))
2811e0c4386eSCy Schubert         goto end;
2812e0c4386eSCy Schubert     /* Re-do the write; still no tickets sent */
2813e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2814e0c4386eSCy Schubert             || !TEST_size_t_eq(1, nbytes)
2815e0c4386eSCy Schubert             || !TEST_int_eq(0, new_called)
2816e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2817e0c4386eSCy Schubert             || !TEST_int_eq(0, new_called)
2818e0c4386eSCy Schubert             || !TEST_int_eq(sizeof(buf), nbytes)
2819e0c4386eSCy Schubert             || !TEST_int_eq(c, buf[0])
2820e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2821e0c4386eSCy Schubert         goto end;
2822e0c4386eSCy Schubert     /* Even trying to hit the state machine now will still not send tickets */
2823e0c4386eSCy Schubert     if (!TEST_true(SSL_do_handshake(serverssl))
2824e0c4386eSCy Schubert             || !TEST_int_eq(0, new_called))
2825e0c4386eSCy Schubert         goto end;
2826e0c4386eSCy Schubert     /* Now the *next* write should send the tickets */
2827e0c4386eSCy Schubert     c = '6';
2828e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2829e0c4386eSCy Schubert             || !TEST_size_t_eq(1, nbytes)
2830e0c4386eSCy Schubert             || !TEST_int_eq(2, new_called)
2831e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2832e0c4386eSCy Schubert             || !TEST_int_eq(4, new_called)
2833e0c4386eSCy Schubert             || !TEST_int_eq(sizeof(buf), nbytes)
2834e0c4386eSCy Schubert             || !TEST_int_eq(c, buf[0])
2835e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2836e0c4386eSCy Schubert         goto end;
2837e0c4386eSCy Schubert 
2838e0c4386eSCy Schubert     SSL_shutdown(clientssl);
2839e0c4386eSCy Schubert     SSL_shutdown(serverssl);
2840e0c4386eSCy Schubert     testresult = 1;
2841e0c4386eSCy Schubert 
2842e0c4386eSCy Schubert  end:
2843e0c4386eSCy Schubert     BIO_free(bretry);
2844e0c4386eSCy Schubert     BIO_free(tmp);
2845e0c4386eSCy Schubert     SSL_free(serverssl);
2846e0c4386eSCy Schubert     SSL_free(clientssl);
2847e0c4386eSCy Schubert     SSL_CTX_free(sctx);
2848e0c4386eSCy Schubert     SSL_CTX_free(cctx);
2849e0c4386eSCy Schubert     clientssl = serverssl = NULL;
2850e0c4386eSCy Schubert     sctx = cctx = NULL;
2851e0c4386eSCy Schubert     return testresult;
2852e0c4386eSCy Schubert }
2853e0c4386eSCy Schubert #endif
2854e0c4386eSCy Schubert 
2855e0c4386eSCy Schubert #define USE_NULL            0
2856e0c4386eSCy Schubert #define USE_BIO_1           1
2857e0c4386eSCy Schubert #define USE_BIO_2           2
2858e0c4386eSCy Schubert #define USE_DEFAULT         3
2859e0c4386eSCy Schubert 
2860e0c4386eSCy Schubert #define CONNTYPE_CONNECTION_SUCCESS  0
2861e0c4386eSCy Schubert #define CONNTYPE_CONNECTION_FAIL     1
2862e0c4386eSCy Schubert #define CONNTYPE_NO_CONNECTION       2
2863e0c4386eSCy Schubert 
2864e0c4386eSCy Schubert #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS         (3 * 3 * 3 * 3)
2865e0c4386eSCy Schubert #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS    (2 * 2)
2866e0c4386eSCy Schubert #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
2867e0c4386eSCy Schubert # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS       (2 * 2)
2868e0c4386eSCy Schubert #else
2869e0c4386eSCy Schubert # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS       0
2870e0c4386eSCy Schubert #endif
2871e0c4386eSCy Schubert 
2872e0c4386eSCy Schubert #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
2873e0c4386eSCy Schubert                                 + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
2874e0c4386eSCy Schubert                                 + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
2875e0c4386eSCy Schubert 
setupbio(BIO ** res,BIO * bio1,BIO * bio2,int type)2876e0c4386eSCy Schubert static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
2877e0c4386eSCy Schubert {
2878e0c4386eSCy Schubert     switch (type) {
2879e0c4386eSCy Schubert     case USE_NULL:
2880e0c4386eSCy Schubert         *res = NULL;
2881e0c4386eSCy Schubert         break;
2882e0c4386eSCy Schubert     case USE_BIO_1:
2883e0c4386eSCy Schubert         *res = bio1;
2884e0c4386eSCy Schubert         break;
2885e0c4386eSCy Schubert     case USE_BIO_2:
2886e0c4386eSCy Schubert         *res = bio2;
2887e0c4386eSCy Schubert         break;
2888e0c4386eSCy Schubert     }
2889e0c4386eSCy Schubert }
2890e0c4386eSCy Schubert 
2891e0c4386eSCy Schubert 
2892e0c4386eSCy Schubert /*
2893e0c4386eSCy Schubert  * Tests calls to SSL_set_bio() under various conditions.
2894e0c4386eSCy Schubert  *
2895e0c4386eSCy Schubert  * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
2896e0c4386eSCy Schubert  * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
2897e0c4386eSCy Schubert  * then do more tests where we create a successful connection first using our
2898e0c4386eSCy Schubert  * standard connection setup functions, and then call SSL_set_bio() with
2899e0c4386eSCy Schubert  * various combinations of valid BIOs or NULL. We then repeat these tests
2900e0c4386eSCy Schubert  * following a failed connection. In this last case we are looking to check that
2901e0c4386eSCy Schubert  * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
2902e0c4386eSCy Schubert  */
test_ssl_set_bio(int idx)2903e0c4386eSCy Schubert static int test_ssl_set_bio(int idx)
2904e0c4386eSCy Schubert {
2905e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
2906e0c4386eSCy Schubert     BIO *bio1 = NULL;
2907e0c4386eSCy Schubert     BIO *bio2 = NULL;
2908e0c4386eSCy Schubert     BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
2909e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
2910e0c4386eSCy Schubert     int initrbio, initwbio, newrbio, newwbio, conntype;
2911e0c4386eSCy Schubert     int testresult = 0;
2912e0c4386eSCy Schubert 
2913e0c4386eSCy Schubert     if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
2914e0c4386eSCy Schubert         initrbio = idx % 3;
2915e0c4386eSCy Schubert         idx /= 3;
2916e0c4386eSCy Schubert         initwbio = idx % 3;
2917e0c4386eSCy Schubert         idx /= 3;
2918e0c4386eSCy Schubert         newrbio = idx % 3;
2919e0c4386eSCy Schubert         idx /= 3;
2920e0c4386eSCy Schubert         newwbio = idx % 3;
2921e0c4386eSCy Schubert         conntype = CONNTYPE_NO_CONNECTION;
2922e0c4386eSCy Schubert     } else {
2923e0c4386eSCy Schubert         idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
2924e0c4386eSCy Schubert         initrbio = initwbio = USE_DEFAULT;
2925e0c4386eSCy Schubert         newrbio = idx % 2;
2926e0c4386eSCy Schubert         idx /= 2;
2927e0c4386eSCy Schubert         newwbio = idx % 2;
2928e0c4386eSCy Schubert         idx /= 2;
2929e0c4386eSCy Schubert         conntype = idx % 2;
2930e0c4386eSCy Schubert     }
2931e0c4386eSCy Schubert 
2932e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2933e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
2934e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
2935e0c4386eSCy Schubert         goto end;
2936e0c4386eSCy Schubert 
2937e0c4386eSCy Schubert     if (conntype == CONNTYPE_CONNECTION_FAIL) {
2938e0c4386eSCy Schubert         /*
2939e0c4386eSCy Schubert          * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
2940e0c4386eSCy Schubert          * because we reduced the number of tests in the definition of
2941e0c4386eSCy Schubert          * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
2942e0c4386eSCy Schubert          * mismatched protocol versions we will force a connection failure.
2943e0c4386eSCy Schubert          */
2944e0c4386eSCy Schubert         SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
2945e0c4386eSCy Schubert         SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2946e0c4386eSCy Schubert     }
2947e0c4386eSCy Schubert 
2948e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2949e0c4386eSCy Schubert                                       NULL, NULL)))
2950e0c4386eSCy Schubert         goto end;
2951e0c4386eSCy Schubert 
2952e0c4386eSCy Schubert     if (initrbio == USE_BIO_1
2953e0c4386eSCy Schubert             || initwbio == USE_BIO_1
2954e0c4386eSCy Schubert             || newrbio == USE_BIO_1
2955e0c4386eSCy Schubert             || newwbio == USE_BIO_1) {
2956e0c4386eSCy Schubert         if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
2957e0c4386eSCy Schubert             goto end;
2958e0c4386eSCy Schubert     }
2959e0c4386eSCy Schubert 
2960e0c4386eSCy Schubert     if (initrbio == USE_BIO_2
2961e0c4386eSCy Schubert             || initwbio == USE_BIO_2
2962e0c4386eSCy Schubert             || newrbio == USE_BIO_2
2963e0c4386eSCy Schubert             || newwbio == USE_BIO_2) {
2964e0c4386eSCy Schubert         if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
2965e0c4386eSCy Schubert             goto end;
2966e0c4386eSCy Schubert     }
2967e0c4386eSCy Schubert 
2968e0c4386eSCy Schubert     if (initrbio != USE_DEFAULT) {
2969e0c4386eSCy Schubert         setupbio(&irbio, bio1, bio2, initrbio);
2970e0c4386eSCy Schubert         setupbio(&iwbio, bio1, bio2, initwbio);
2971e0c4386eSCy Schubert         SSL_set_bio(clientssl, irbio, iwbio);
2972e0c4386eSCy Schubert 
2973e0c4386eSCy Schubert         /*
2974e0c4386eSCy Schubert          * We want to maintain our own refs to these BIO, so do an up ref for
2975e0c4386eSCy Schubert          * each BIO that will have ownership transferred in the SSL_set_bio()
2976e0c4386eSCy Schubert          * call
2977e0c4386eSCy Schubert          */
2978e0c4386eSCy Schubert         if (irbio != NULL)
2979e0c4386eSCy Schubert             BIO_up_ref(irbio);
2980e0c4386eSCy Schubert         if (iwbio != NULL && iwbio != irbio)
2981e0c4386eSCy Schubert             BIO_up_ref(iwbio);
2982e0c4386eSCy Schubert     }
2983e0c4386eSCy Schubert 
2984e0c4386eSCy Schubert     if (conntype != CONNTYPE_NO_CONNECTION
2985e0c4386eSCy Schubert             && !TEST_true(create_ssl_connection(serverssl, clientssl,
2986e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)
2987e0c4386eSCy Schubert                           == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
2988e0c4386eSCy Schubert         goto end;
2989e0c4386eSCy Schubert 
2990e0c4386eSCy Schubert     setupbio(&nrbio, bio1, bio2, newrbio);
2991e0c4386eSCy Schubert     setupbio(&nwbio, bio1, bio2, newwbio);
2992e0c4386eSCy Schubert 
2993e0c4386eSCy Schubert     /*
2994e0c4386eSCy Schubert      * We will (maybe) transfer ownership again so do more up refs.
2995e0c4386eSCy Schubert      * SSL_set_bio() has some really complicated ownership rules where BIOs have
2996e0c4386eSCy Schubert      * already been set!
2997e0c4386eSCy Schubert      */
2998e0c4386eSCy Schubert     if (nrbio != NULL
2999e0c4386eSCy Schubert             && nrbio != irbio
3000e0c4386eSCy Schubert             && (nwbio != iwbio || nrbio != nwbio))
3001e0c4386eSCy Schubert         BIO_up_ref(nrbio);
3002e0c4386eSCy Schubert     if (nwbio != NULL
3003e0c4386eSCy Schubert             && nwbio != nrbio
3004e0c4386eSCy Schubert             && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
3005e0c4386eSCy Schubert         BIO_up_ref(nwbio);
3006e0c4386eSCy Schubert 
3007e0c4386eSCy Schubert     SSL_set_bio(clientssl, nrbio, nwbio);
3008e0c4386eSCy Schubert 
3009e0c4386eSCy Schubert     testresult = 1;
3010e0c4386eSCy Schubert 
3011e0c4386eSCy Schubert  end:
3012e0c4386eSCy Schubert     BIO_free(bio1);
3013e0c4386eSCy Schubert     BIO_free(bio2);
3014e0c4386eSCy Schubert 
3015e0c4386eSCy Schubert     /*
3016e0c4386eSCy Schubert      * This test is checking that the ref counting for SSL_set_bio is correct.
3017e0c4386eSCy Schubert      * If we get here and we did too many frees then we will fail in the above
3018e0c4386eSCy Schubert      * functions.
3019e0c4386eSCy Schubert      */
3020e0c4386eSCy Schubert     SSL_free(serverssl);
3021e0c4386eSCy Schubert     SSL_free(clientssl);
3022e0c4386eSCy Schubert     SSL_CTX_free(sctx);
3023e0c4386eSCy Schubert     SSL_CTX_free(cctx);
3024e0c4386eSCy Schubert     return testresult;
3025e0c4386eSCy Schubert }
3026e0c4386eSCy Schubert 
3027e0c4386eSCy Schubert typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
3028e0c4386eSCy Schubert 
execute_test_ssl_bio(int pop_ssl,bio_change_t change_bio)3029e0c4386eSCy Schubert static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
3030e0c4386eSCy Schubert {
3031e0c4386eSCy Schubert     BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
3032e0c4386eSCy Schubert     SSL_CTX *ctx;
3033e0c4386eSCy Schubert     SSL *ssl = NULL;
3034e0c4386eSCy Schubert     int testresult = 0;
3035e0c4386eSCy Schubert 
3036e0c4386eSCy Schubert     if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
3037e0c4386eSCy Schubert             || !TEST_ptr(ssl = SSL_new(ctx))
3038e0c4386eSCy Schubert             || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
3039e0c4386eSCy Schubert             || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
3040e0c4386eSCy Schubert         goto end;
3041e0c4386eSCy Schubert 
3042e0c4386eSCy Schubert     BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
3043e0c4386eSCy Schubert 
3044e0c4386eSCy Schubert     /*
3045e0c4386eSCy Schubert      * If anything goes wrong here then we could leak memory.
3046e0c4386eSCy Schubert      */
3047e0c4386eSCy Schubert     BIO_push(sslbio, membio1);
3048e0c4386eSCy Schubert 
3049e0c4386eSCy Schubert     /* Verify changing the rbio/wbio directly does not cause leaks */
3050e0c4386eSCy Schubert     if (change_bio != NO_BIO_CHANGE) {
3051e0c4386eSCy Schubert         if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
3052e0c4386eSCy Schubert             ssl = NULL;
3053e0c4386eSCy Schubert             goto end;
3054e0c4386eSCy Schubert         }
3055e0c4386eSCy Schubert         if (change_bio == CHANGE_RBIO)
3056e0c4386eSCy Schubert             SSL_set0_rbio(ssl, membio2);
3057e0c4386eSCy Schubert         else
3058e0c4386eSCy Schubert             SSL_set0_wbio(ssl, membio2);
3059e0c4386eSCy Schubert     }
3060e0c4386eSCy Schubert     ssl = NULL;
3061e0c4386eSCy Schubert 
3062e0c4386eSCy Schubert     if (pop_ssl)
3063e0c4386eSCy Schubert         BIO_pop(sslbio);
3064e0c4386eSCy Schubert     else
3065e0c4386eSCy Schubert         BIO_pop(membio1);
3066e0c4386eSCy Schubert 
3067e0c4386eSCy Schubert     testresult = 1;
3068e0c4386eSCy Schubert  end:
3069e0c4386eSCy Schubert     BIO_free(membio1);
3070e0c4386eSCy Schubert     BIO_free(sslbio);
3071e0c4386eSCy Schubert     SSL_free(ssl);
3072e0c4386eSCy Schubert     SSL_CTX_free(ctx);
3073e0c4386eSCy Schubert 
3074e0c4386eSCy Schubert     return testresult;
3075e0c4386eSCy Schubert }
3076e0c4386eSCy Schubert 
test_ssl_bio_pop_next_bio(void)3077e0c4386eSCy Schubert static int test_ssl_bio_pop_next_bio(void)
3078e0c4386eSCy Schubert {
3079e0c4386eSCy Schubert     return execute_test_ssl_bio(0, NO_BIO_CHANGE);
3080e0c4386eSCy Schubert }
3081e0c4386eSCy Schubert 
test_ssl_bio_pop_ssl_bio(void)3082e0c4386eSCy Schubert static int test_ssl_bio_pop_ssl_bio(void)
3083e0c4386eSCy Schubert {
3084e0c4386eSCy Schubert     return execute_test_ssl_bio(1, NO_BIO_CHANGE);
3085e0c4386eSCy Schubert }
3086e0c4386eSCy Schubert 
test_ssl_bio_change_rbio(void)3087e0c4386eSCy Schubert static int test_ssl_bio_change_rbio(void)
3088e0c4386eSCy Schubert {
3089e0c4386eSCy Schubert     return execute_test_ssl_bio(0, CHANGE_RBIO);
3090e0c4386eSCy Schubert }
3091e0c4386eSCy Schubert 
test_ssl_bio_change_wbio(void)3092e0c4386eSCy Schubert static int test_ssl_bio_change_wbio(void)
3093e0c4386eSCy Schubert {
3094e0c4386eSCy Schubert     return execute_test_ssl_bio(0, CHANGE_WBIO);
3095e0c4386eSCy Schubert }
3096e0c4386eSCy Schubert 
3097e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
3098e0c4386eSCy Schubert typedef struct {
3099e0c4386eSCy Schubert     /* The list of sig algs */
3100e0c4386eSCy Schubert     const int *list;
3101e0c4386eSCy Schubert     /* The length of the list */
3102e0c4386eSCy Schubert     size_t listlen;
3103e0c4386eSCy Schubert     /* A sigalgs list in string format */
3104e0c4386eSCy Schubert     const char *liststr;
3105e0c4386eSCy Schubert     /* Whether setting the list should succeed */
3106e0c4386eSCy Schubert     int valid;
3107e0c4386eSCy Schubert     /* Whether creating a connection with the list should succeed */
3108e0c4386eSCy Schubert     int connsuccess;
3109e0c4386eSCy Schubert } sigalgs_list;
3110e0c4386eSCy Schubert 
3111e0c4386eSCy Schubert static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
3112e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
3113e0c4386eSCy Schubert static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
3114e0c4386eSCy Schubert static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
3115e0c4386eSCy Schubert # endif
3116e0c4386eSCy Schubert static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
3117e0c4386eSCy Schubert static const int invalidlist2[] = {NID_sha256, NID_undef};
3118e0c4386eSCy Schubert static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
3119e0c4386eSCy Schubert static const int invalidlist4[] = {NID_sha256};
3120e0c4386eSCy Schubert static const sigalgs_list testsigalgs[] = {
3121e0c4386eSCy Schubert     {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
3122e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
3123e0c4386eSCy Schubert     {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
3124e0c4386eSCy Schubert     {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
3125e0c4386eSCy Schubert # endif
3126e0c4386eSCy Schubert     {NULL, 0, "RSA+SHA256", 1, 1},
3127e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
3128e0c4386eSCy Schubert     {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
3129e0c4386eSCy Schubert     {NULL, 0, "ECDSA+SHA512", 1, 0},
3130e0c4386eSCy Schubert # endif
3131e0c4386eSCy Schubert     {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
3132e0c4386eSCy Schubert     {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
3133e0c4386eSCy Schubert     {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
3134e0c4386eSCy Schubert     {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
3135e0c4386eSCy Schubert     {NULL, 0, "RSA", 0, 0},
3136e0c4386eSCy Schubert     {NULL, 0, "SHA256", 0, 0},
3137e0c4386eSCy Schubert     {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
3138e0c4386eSCy Schubert     {NULL, 0, "Invalid", 0, 0}
3139e0c4386eSCy Schubert };
3140e0c4386eSCy Schubert 
test_set_sigalgs(int idx)3141e0c4386eSCy Schubert static int test_set_sigalgs(int idx)
3142e0c4386eSCy Schubert {
3143e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
3144e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
3145e0c4386eSCy Schubert     int testresult = 0;
3146e0c4386eSCy Schubert     const sigalgs_list *curr;
3147e0c4386eSCy Schubert     int testctx;
3148e0c4386eSCy Schubert 
3149e0c4386eSCy Schubert     /* Should never happen */
3150e0c4386eSCy Schubert     if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
3151e0c4386eSCy Schubert         return 0;
3152e0c4386eSCy Schubert 
3153e0c4386eSCy Schubert     testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
3154e0c4386eSCy Schubert     curr = testctx ? &testsigalgs[idx]
3155e0c4386eSCy Schubert                    : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
3156e0c4386eSCy Schubert 
3157e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3158e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
3159e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
3160e0c4386eSCy Schubert         return 0;
3161e0c4386eSCy Schubert 
3162e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
3163e0c4386eSCy Schubert 
3164e0c4386eSCy Schubert     if (testctx) {
3165e0c4386eSCy Schubert         int ret;
3166e0c4386eSCy Schubert 
3167e0c4386eSCy Schubert         if (curr->list != NULL)
3168e0c4386eSCy Schubert             ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
3169e0c4386eSCy Schubert         else
3170e0c4386eSCy Schubert             ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
3171e0c4386eSCy Schubert 
3172e0c4386eSCy Schubert         if (!ret) {
3173e0c4386eSCy Schubert             if (curr->valid)
3174e0c4386eSCy Schubert                 TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
3175e0c4386eSCy Schubert             else
3176e0c4386eSCy Schubert                 testresult = 1;
3177e0c4386eSCy Schubert             goto end;
3178e0c4386eSCy Schubert         }
3179e0c4386eSCy Schubert         if (!curr->valid) {
3180e0c4386eSCy Schubert             TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
3181e0c4386eSCy Schubert             goto end;
3182e0c4386eSCy Schubert         }
3183e0c4386eSCy Schubert     }
3184e0c4386eSCy Schubert 
3185e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3186e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
3187e0c4386eSCy Schubert         goto end;
3188e0c4386eSCy Schubert 
3189e0c4386eSCy Schubert     if (!testctx) {
3190e0c4386eSCy Schubert         int ret;
3191e0c4386eSCy Schubert 
3192e0c4386eSCy Schubert         if (curr->list != NULL)
3193e0c4386eSCy Schubert             ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
3194e0c4386eSCy Schubert         else
3195e0c4386eSCy Schubert             ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
3196e0c4386eSCy Schubert         if (!ret) {
3197e0c4386eSCy Schubert             if (curr->valid)
3198e0c4386eSCy Schubert                 TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
3199e0c4386eSCy Schubert             else
3200e0c4386eSCy Schubert                 testresult = 1;
3201e0c4386eSCy Schubert             goto end;
3202e0c4386eSCy Schubert         }
3203e0c4386eSCy Schubert         if (!curr->valid)
3204e0c4386eSCy Schubert             goto end;
3205e0c4386eSCy Schubert     }
3206e0c4386eSCy Schubert 
3207e0c4386eSCy Schubert     if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
3208e0c4386eSCy Schubert                                            SSL_ERROR_NONE),
3209e0c4386eSCy Schubert                 curr->connsuccess))
3210e0c4386eSCy Schubert         goto end;
3211e0c4386eSCy Schubert 
3212e0c4386eSCy Schubert     testresult = 1;
3213e0c4386eSCy Schubert 
3214e0c4386eSCy Schubert  end:
3215e0c4386eSCy Schubert     SSL_free(serverssl);
3216e0c4386eSCy Schubert     SSL_free(clientssl);
3217e0c4386eSCy Schubert     SSL_CTX_free(sctx);
3218e0c4386eSCy Schubert     SSL_CTX_free(cctx);
3219e0c4386eSCy Schubert 
3220e0c4386eSCy Schubert     return testresult;
3221e0c4386eSCy Schubert }
3222e0c4386eSCy Schubert #endif
3223e0c4386eSCy Schubert 
3224e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
3225e0c4386eSCy Schubert static int psk_client_cb_cnt = 0;
3226e0c4386eSCy Schubert static int psk_server_cb_cnt = 0;
3227e0c4386eSCy Schubert 
use_session_cb(SSL * ssl,const EVP_MD * md,const unsigned char ** id,size_t * idlen,SSL_SESSION ** sess)3228e0c4386eSCy Schubert static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
3229e0c4386eSCy Schubert                           size_t *idlen, SSL_SESSION **sess)
3230e0c4386eSCy Schubert {
3231e0c4386eSCy Schubert     switch (++use_session_cb_cnt) {
3232e0c4386eSCy Schubert     case 1:
3233e0c4386eSCy Schubert         /* The first call should always have a NULL md */
3234e0c4386eSCy Schubert         if (md != NULL)
3235e0c4386eSCy Schubert             return 0;
3236e0c4386eSCy Schubert         break;
3237e0c4386eSCy Schubert 
3238e0c4386eSCy Schubert     case 2:
3239e0c4386eSCy Schubert         /* The second call should always have an md */
3240e0c4386eSCy Schubert         if (md == NULL)
3241e0c4386eSCy Schubert             return 0;
3242e0c4386eSCy Schubert         break;
3243e0c4386eSCy Schubert 
3244e0c4386eSCy Schubert     default:
3245e0c4386eSCy Schubert         /* We should only be called a maximum of twice */
3246e0c4386eSCy Schubert         return 0;
3247e0c4386eSCy Schubert     }
3248e0c4386eSCy Schubert 
3249e0c4386eSCy Schubert     if (clientpsk != NULL)
3250e0c4386eSCy Schubert         SSL_SESSION_up_ref(clientpsk);
3251e0c4386eSCy Schubert 
3252e0c4386eSCy Schubert     *sess = clientpsk;
3253e0c4386eSCy Schubert     *id = (const unsigned char *)pskid;
3254e0c4386eSCy Schubert     *idlen = strlen(pskid);
3255e0c4386eSCy Schubert 
3256e0c4386eSCy Schubert     return 1;
3257e0c4386eSCy Schubert }
3258e0c4386eSCy Schubert 
3259e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
psk_client_cb(SSL * ssl,const char * hint,char * id,unsigned int max_id_len,unsigned char * psk,unsigned int max_psk_len)3260e0c4386eSCy Schubert static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
3261e0c4386eSCy Schubert                                   unsigned int max_id_len,
3262e0c4386eSCy Schubert                                   unsigned char *psk,
3263e0c4386eSCy Schubert                                   unsigned int max_psk_len)
3264e0c4386eSCy Schubert {
3265e0c4386eSCy Schubert     unsigned int psklen = 0;
3266e0c4386eSCy Schubert 
3267e0c4386eSCy Schubert     psk_client_cb_cnt++;
3268e0c4386eSCy Schubert 
3269e0c4386eSCy Schubert     if (strlen(pskid) + 1 > max_id_len)
3270e0c4386eSCy Schubert         return 0;
3271e0c4386eSCy Schubert 
3272e0c4386eSCy Schubert     /* We should only ever be called a maximum of twice per connection */
3273e0c4386eSCy Schubert     if (psk_client_cb_cnt > 2)
3274e0c4386eSCy Schubert         return 0;
3275e0c4386eSCy Schubert 
3276e0c4386eSCy Schubert     if (clientpsk == NULL)
3277e0c4386eSCy Schubert         return 0;
3278e0c4386eSCy Schubert 
3279e0c4386eSCy Schubert     /* We'll reuse the PSK we set up for TLSv1.3 */
3280e0c4386eSCy Schubert     if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
3281e0c4386eSCy Schubert         return 0;
3282e0c4386eSCy Schubert     psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
3283e0c4386eSCy Schubert     strncpy(id, pskid, max_id_len);
3284e0c4386eSCy Schubert 
3285e0c4386eSCy Schubert     return psklen;
3286e0c4386eSCy Schubert }
3287e0c4386eSCy Schubert #endif /* OPENSSL_NO_PSK */
3288e0c4386eSCy Schubert 
find_session_cb(SSL * ssl,const unsigned char * identity,size_t identity_len,SSL_SESSION ** sess)3289e0c4386eSCy Schubert static int find_session_cb(SSL *ssl, const unsigned char *identity,
3290e0c4386eSCy Schubert                            size_t identity_len, SSL_SESSION **sess)
3291e0c4386eSCy Schubert {
3292e0c4386eSCy Schubert     find_session_cb_cnt++;
3293e0c4386eSCy Schubert 
3294e0c4386eSCy Schubert     /* We should only ever be called a maximum of twice per connection */
3295e0c4386eSCy Schubert     if (find_session_cb_cnt > 2)
3296e0c4386eSCy Schubert         return 0;
3297e0c4386eSCy Schubert 
3298e0c4386eSCy Schubert     if (serverpsk == NULL)
3299e0c4386eSCy Schubert         return 0;
3300e0c4386eSCy Schubert 
3301e0c4386eSCy Schubert     /* Identity should match that set by the client */
3302e0c4386eSCy Schubert     if (strlen(srvid) != identity_len
3303e0c4386eSCy Schubert             || strncmp(srvid, (const char *)identity, identity_len) != 0) {
3304e0c4386eSCy Schubert         /* No PSK found, continue but without a PSK */
3305e0c4386eSCy Schubert         *sess = NULL;
3306e0c4386eSCy Schubert         return 1;
3307e0c4386eSCy Schubert     }
3308e0c4386eSCy Schubert 
3309e0c4386eSCy Schubert     SSL_SESSION_up_ref(serverpsk);
3310e0c4386eSCy Schubert     *sess = serverpsk;
3311e0c4386eSCy Schubert 
3312e0c4386eSCy Schubert     return 1;
3313e0c4386eSCy Schubert }
3314e0c4386eSCy Schubert 
3315e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
psk_server_cb(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len)3316e0c4386eSCy Schubert static unsigned int psk_server_cb(SSL *ssl, const char *identity,
3317e0c4386eSCy Schubert                                   unsigned char *psk, unsigned int max_psk_len)
3318e0c4386eSCy Schubert {
3319e0c4386eSCy Schubert     unsigned int psklen = 0;
3320e0c4386eSCy Schubert 
3321e0c4386eSCy Schubert     psk_server_cb_cnt++;
3322e0c4386eSCy Schubert 
3323e0c4386eSCy Schubert     /* We should only ever be called a maximum of twice per connection */
3324e0c4386eSCy Schubert     if (find_session_cb_cnt > 2)
3325e0c4386eSCy Schubert         return 0;
3326e0c4386eSCy Schubert 
3327e0c4386eSCy Schubert     if (serverpsk == NULL)
3328e0c4386eSCy Schubert         return 0;
3329e0c4386eSCy Schubert 
3330e0c4386eSCy Schubert     /* Identity should match that set by the client */
3331e0c4386eSCy Schubert     if (strcmp(srvid, identity) != 0) {
3332e0c4386eSCy Schubert         return 0;
3333e0c4386eSCy Schubert     }
3334e0c4386eSCy Schubert 
3335e0c4386eSCy Schubert     /* We'll reuse the PSK we set up for TLSv1.3 */
3336e0c4386eSCy Schubert     if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
3337e0c4386eSCy Schubert         return 0;
3338e0c4386eSCy Schubert     psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
3339e0c4386eSCy Schubert 
3340e0c4386eSCy Schubert     return psklen;
3341e0c4386eSCy Schubert }
3342e0c4386eSCy Schubert #endif /* OPENSSL_NO_PSK */
3343e0c4386eSCy Schubert 
3344e0c4386eSCy Schubert #define MSG1    "Hello"
3345e0c4386eSCy Schubert #define MSG2    "World."
3346e0c4386eSCy Schubert #define MSG3    "This"
3347e0c4386eSCy Schubert #define MSG4    "is"
3348e0c4386eSCy Schubert #define MSG5    "a"
3349e0c4386eSCy Schubert #define MSG6    "test"
3350e0c4386eSCy Schubert #define MSG7    "message."
3351e0c4386eSCy Schubert 
3352e0c4386eSCy Schubert #define TLS13_AES_128_GCM_SHA256_BYTES  ((const unsigned char *)"\x13\x01")
3353e0c4386eSCy Schubert #define TLS13_AES_256_GCM_SHA384_BYTES  ((const unsigned char *)"\x13\x02")
3354e0c4386eSCy Schubert #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
3355e0c4386eSCy Schubert #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
3356e0c4386eSCy Schubert #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
3357e0c4386eSCy Schubert 
3358e0c4386eSCy Schubert 
create_a_psk(SSL * ssl,size_t mdsize)3359e0c4386eSCy Schubert static SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize)
3360e0c4386eSCy Schubert {
3361e0c4386eSCy Schubert     const SSL_CIPHER *cipher = NULL;
3362e0c4386eSCy Schubert     const unsigned char key[] = {
3363e0c4386eSCy Schubert         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
3364e0c4386eSCy Schubert         0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
3365e0c4386eSCy Schubert         0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
3366e0c4386eSCy Schubert         0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
3367e0c4386eSCy Schubert         0x2c, 0x2d, 0x2e, 0x2f /* SHA384_DIGEST_LENGTH bytes */
3368e0c4386eSCy Schubert     };
3369e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
3370e0c4386eSCy Schubert 
3371e0c4386eSCy Schubert     if (mdsize == SHA384_DIGEST_LENGTH) {
3372e0c4386eSCy Schubert         cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
3373e0c4386eSCy Schubert     } else if (mdsize == SHA256_DIGEST_LENGTH) {
3374e0c4386eSCy Schubert         /*
3375e0c4386eSCy Schubert          * Any ciphersuite using SHA256 will do - it will be compatible with
3376e0c4386eSCy Schubert          * the actual ciphersuite selected as long as it too is based on SHA256
3377e0c4386eSCy Schubert          */
3378e0c4386eSCy Schubert         cipher = SSL_CIPHER_find(ssl, TLS13_AES_128_GCM_SHA256_BYTES);
3379e0c4386eSCy Schubert     } else {
3380e0c4386eSCy Schubert         /* Should not happen */
3381e0c4386eSCy Schubert         return NULL;
3382e0c4386eSCy Schubert     }
3383e0c4386eSCy Schubert     sess = SSL_SESSION_new();
3384e0c4386eSCy Schubert     if (!TEST_ptr(sess)
3385e0c4386eSCy Schubert             || !TEST_ptr(cipher)
3386e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_set1_master_key(sess, key, mdsize))
3387e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
3388e0c4386eSCy Schubert             || !TEST_true(
3389e0c4386eSCy Schubert                     SSL_SESSION_set_protocol_version(sess,
3390e0c4386eSCy Schubert                                                      TLS1_3_VERSION))) {
3391e0c4386eSCy Schubert         SSL_SESSION_free(sess);
3392e0c4386eSCy Schubert         return NULL;
3393e0c4386eSCy Schubert     }
3394e0c4386eSCy Schubert     return sess;
3395e0c4386eSCy Schubert }
3396e0c4386eSCy Schubert 
3397e0c4386eSCy Schubert /*
3398e0c4386eSCy Schubert  * Helper method to setup objects for early data test. Caller frees objects on
3399e0c4386eSCy Schubert  * error.
3400e0c4386eSCy Schubert  */
setupearly_data_test(SSL_CTX ** cctx,SSL_CTX ** sctx,SSL ** clientssl,SSL ** serverssl,SSL_SESSION ** sess,int idx,size_t mdsize)3401e0c4386eSCy Schubert static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
3402e0c4386eSCy Schubert                                 SSL **serverssl, SSL_SESSION **sess, int idx,
3403e0c4386eSCy Schubert                                 size_t mdsize)
3404e0c4386eSCy Schubert {
3405e0c4386eSCy Schubert     if (*sctx == NULL
3406e0c4386eSCy Schubert             && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3407e0c4386eSCy Schubert                                               TLS_client_method(),
3408e0c4386eSCy Schubert                                               TLS1_VERSION, 0,
3409e0c4386eSCy Schubert                                               sctx, cctx, cert, privkey)))
3410e0c4386eSCy Schubert         return 0;
3411e0c4386eSCy Schubert 
3412e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
3413e0c4386eSCy Schubert         return 0;
3414e0c4386eSCy Schubert 
3415e0c4386eSCy Schubert     if (idx == 1) {
3416e0c4386eSCy Schubert         /* When idx == 1 we repeat the tests with read_ahead set */
3417e0c4386eSCy Schubert         SSL_CTX_set_read_ahead(*cctx, 1);
3418e0c4386eSCy Schubert         SSL_CTX_set_read_ahead(*sctx, 1);
3419e0c4386eSCy Schubert     } else if (idx == 2) {
3420e0c4386eSCy Schubert         /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
3421e0c4386eSCy Schubert         SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
3422e0c4386eSCy Schubert         SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
3423e0c4386eSCy Schubert         use_session_cb_cnt = 0;
3424e0c4386eSCy Schubert         find_session_cb_cnt = 0;
3425e0c4386eSCy Schubert         srvid = pskid;
3426e0c4386eSCy Schubert     }
3427e0c4386eSCy Schubert 
3428e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
3429e0c4386eSCy Schubert                                       NULL, NULL)))
3430e0c4386eSCy Schubert         return 0;
3431e0c4386eSCy Schubert 
3432e0c4386eSCy Schubert     /*
3433e0c4386eSCy Schubert      * For one of the run throughs (doesn't matter which one), we'll try sending
3434e0c4386eSCy Schubert      * some SNI data in the initial ClientHello. This will be ignored (because
3435e0c4386eSCy Schubert      * there is no SNI cb set up by the server), so it should not impact
3436e0c4386eSCy Schubert      * early_data.
3437e0c4386eSCy Schubert      */
3438e0c4386eSCy Schubert     if (idx == 1
3439e0c4386eSCy Schubert             && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
3440e0c4386eSCy Schubert         return 0;
3441e0c4386eSCy Schubert 
3442e0c4386eSCy Schubert     if (idx == 2) {
3443e0c4386eSCy Schubert         clientpsk = create_a_psk(*clientssl, mdsize);
3444e0c4386eSCy Schubert         if (!TEST_ptr(clientpsk)
3445e0c4386eSCy Schubert                    /*
3446e0c4386eSCy Schubert                     * We just choose an arbitrary value for max_early_data which
3447e0c4386eSCy Schubert                     * should be big enough for testing purposes.
3448e0c4386eSCy Schubert                     */
3449e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
3450e0c4386eSCy Schubert                                                              0x100))
3451e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3452e0c4386eSCy Schubert             SSL_SESSION_free(clientpsk);
3453e0c4386eSCy Schubert             clientpsk = NULL;
3454e0c4386eSCy Schubert             return 0;
3455e0c4386eSCy Schubert         }
3456e0c4386eSCy Schubert         serverpsk = clientpsk;
3457e0c4386eSCy Schubert 
3458e0c4386eSCy Schubert         if (sess != NULL) {
3459e0c4386eSCy Schubert             if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3460e0c4386eSCy Schubert                 SSL_SESSION_free(clientpsk);
3461e0c4386eSCy Schubert                 SSL_SESSION_free(serverpsk);
3462e0c4386eSCy Schubert                 clientpsk = serverpsk = NULL;
3463e0c4386eSCy Schubert                 return 0;
3464e0c4386eSCy Schubert             }
3465e0c4386eSCy Schubert             *sess = clientpsk;
3466e0c4386eSCy Schubert         }
3467e0c4386eSCy Schubert         return 1;
3468e0c4386eSCy Schubert     }
3469e0c4386eSCy Schubert 
3470e0c4386eSCy Schubert     if (sess == NULL)
3471e0c4386eSCy Schubert         return 1;
3472e0c4386eSCy Schubert 
3473e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
3474e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
3475e0c4386eSCy Schubert         return 0;
3476e0c4386eSCy Schubert 
3477e0c4386eSCy Schubert     *sess = SSL_get1_session(*clientssl);
3478e0c4386eSCy Schubert     SSL_shutdown(*clientssl);
3479e0c4386eSCy Schubert     SSL_shutdown(*serverssl);
3480e0c4386eSCy Schubert     SSL_free(*serverssl);
3481e0c4386eSCy Schubert     SSL_free(*clientssl);
3482e0c4386eSCy Schubert     *serverssl = *clientssl = NULL;
3483e0c4386eSCy Schubert 
3484e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
3485e0c4386eSCy Schubert                                       clientssl, NULL, NULL))
3486e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(*clientssl, *sess)))
3487e0c4386eSCy Schubert         return 0;
3488e0c4386eSCy Schubert 
3489e0c4386eSCy Schubert     return 1;
3490e0c4386eSCy Schubert }
3491e0c4386eSCy Schubert 
check_early_data_timeout(time_t timer)349244096ebdSEnji Cooper static int check_early_data_timeout(time_t timer)
349344096ebdSEnji Cooper {
349444096ebdSEnji Cooper     int res = 0;
349544096ebdSEnji Cooper 
349644096ebdSEnji Cooper     /*
349744096ebdSEnji Cooper      * Early data is time sensitive. We have an approx 8 second allowance
349844096ebdSEnji Cooper      * between writing the early data and reading it. If we exceed that time
349944096ebdSEnji Cooper      * then this test will fail. This can sometimes (rarely) occur in normal CI
350044096ebdSEnji Cooper      * operation. We can try and detect this and just ignore the result of this
350144096ebdSEnji Cooper      * test if it has taken too long. We assume anything over 7 seconds is too
350244096ebdSEnji Cooper      * long
350344096ebdSEnji Cooper      */
350444096ebdSEnji Cooper     timer = time(NULL) - timer;
350544096ebdSEnji Cooper     if (timer >= 7)
350644096ebdSEnji Cooper         res = TEST_skip("Test took too long, ignoring result");
350744096ebdSEnji Cooper 
350844096ebdSEnji Cooper     return res;
350944096ebdSEnji Cooper }
351044096ebdSEnji Cooper 
test_early_data_read_write(int idx)3511e0c4386eSCy Schubert static int test_early_data_read_write(int idx)
3512e0c4386eSCy Schubert {
3513e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
3514e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
3515e0c4386eSCy Schubert     int testresult = 0;
3516e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
3517e0c4386eSCy Schubert     unsigned char buf[20], data[1024];
3518e0c4386eSCy Schubert     size_t readbytes, written, eoedlen, rawread, rawwritten;
3519e0c4386eSCy Schubert     BIO *rbio;
352044096ebdSEnji Cooper     time_t timer;
3521e0c4386eSCy Schubert 
3522e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3523e0c4386eSCy Schubert                                         &serverssl, &sess, idx,
3524e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
3525e0c4386eSCy Schubert         goto end;
3526e0c4386eSCy Schubert 
3527e0c4386eSCy Schubert     /* Write and read some early data */
352844096ebdSEnji Cooper     timer = time(NULL);
3529e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3530e0c4386eSCy Schubert                                         &written))
353144096ebdSEnji Cooper             || !TEST_size_t_eq(written, strlen(MSG1)))
353244096ebdSEnji Cooper         goto end;
353344096ebdSEnji Cooper 
353444096ebdSEnji Cooper     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
353544096ebdSEnji Cooper                                          &readbytes),
353644096ebdSEnji Cooper                      SSL_READ_EARLY_DATA_SUCCESS)) {
353744096ebdSEnji Cooper         testresult = check_early_data_timeout(timer);
353844096ebdSEnji Cooper         goto end;
353944096ebdSEnji Cooper     }
354044096ebdSEnji Cooper 
354144096ebdSEnji Cooper     if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
3542e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3543e0c4386eSCy Schubert                             SSL_EARLY_DATA_ACCEPTED))
3544e0c4386eSCy Schubert         goto end;
3545e0c4386eSCy Schubert 
3546e0c4386eSCy Schubert     /*
3547e0c4386eSCy Schubert      * Server should be able to write data, and client should be able to
3548e0c4386eSCy Schubert      * read it.
3549e0c4386eSCy Schubert      */
3550e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
3551e0c4386eSCy Schubert                                         &written))
3552e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2))
3553e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3554e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3555e0c4386eSCy Schubert         goto end;
3556e0c4386eSCy Schubert 
3557e0c4386eSCy Schubert     /* Even after reading normal data, client should be able write early data */
3558e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
3559e0c4386eSCy Schubert                                         &written))
3560e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG3)))
3561e0c4386eSCy Schubert         goto end;
3562e0c4386eSCy Schubert 
3563e0c4386eSCy Schubert     /* Server should still be able read early data after writing data */
3564e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3565e0c4386eSCy Schubert                                          &readbytes),
3566e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_SUCCESS)
3567e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
3568e0c4386eSCy Schubert         goto end;
3569e0c4386eSCy Schubert 
3570e0c4386eSCy Schubert     /* Write more data from server and read it from client */
3571e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
3572e0c4386eSCy Schubert                                         &written))
3573e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG4))
3574e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3575e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
3576e0c4386eSCy Schubert         goto end;
3577e0c4386eSCy Schubert 
3578e0c4386eSCy Schubert     /*
3579e0c4386eSCy Schubert      * If client writes normal data it should mean writing early data is no
3580e0c4386eSCy Schubert      * longer possible.
3581e0c4386eSCy Schubert      */
3582e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3583e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG5))
3584e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3585e0c4386eSCy Schubert                             SSL_EARLY_DATA_ACCEPTED))
3586e0c4386eSCy Schubert         goto end;
3587e0c4386eSCy Schubert 
3588e0c4386eSCy Schubert     /*
3589e0c4386eSCy Schubert      * At this point the client has written EndOfEarlyData, ClientFinished and
3590e0c4386eSCy Schubert      * normal (fully protected) data. We are going to cause a delay between the
3591e0c4386eSCy Schubert      * arrival of EndOfEarlyData and ClientFinished. We read out all the data
3592e0c4386eSCy Schubert      * in the read BIO, and then just put back the EndOfEarlyData message.
3593e0c4386eSCy Schubert      */
3594e0c4386eSCy Schubert     rbio = SSL_get_rbio(serverssl);
3595e0c4386eSCy Schubert     if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
3596e0c4386eSCy Schubert             || !TEST_size_t_lt(rawread, sizeof(data))
3597e0c4386eSCy Schubert             || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
3598e0c4386eSCy Schubert         goto end;
3599e0c4386eSCy Schubert 
3600e0c4386eSCy Schubert     /* Record length is in the 4th and 5th bytes of the record header */
3601e0c4386eSCy Schubert     eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
3602e0c4386eSCy Schubert     if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
3603e0c4386eSCy Schubert             || !TEST_size_t_eq(rawwritten, eoedlen))
3604e0c4386eSCy Schubert         goto end;
3605e0c4386eSCy Schubert 
3606e0c4386eSCy Schubert     /* Server should be told that there is no more early data */
3607e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3608e0c4386eSCy Schubert                                          &readbytes),
3609e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_FINISH)
3610e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, 0))
3611e0c4386eSCy Schubert         goto end;
3612e0c4386eSCy Schubert 
3613e0c4386eSCy Schubert     /*
3614e0c4386eSCy Schubert      * Server has not finished init yet, so should still be able to write early
3615e0c4386eSCy Schubert      * data.
3616e0c4386eSCy Schubert      */
3617e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
3618e0c4386eSCy Schubert                                         &written))
3619e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG6)))
3620e0c4386eSCy Schubert         goto end;
3621e0c4386eSCy Schubert 
3622e0c4386eSCy Schubert     /* Push the ClientFinished and the normal data back into the server rbio */
3623e0c4386eSCy Schubert     if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
3624e0c4386eSCy Schubert                                 &rawwritten))
3625e0c4386eSCy Schubert             || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
3626e0c4386eSCy Schubert         goto end;
3627e0c4386eSCy Schubert 
3628e0c4386eSCy Schubert     /* Server should be able to read normal data */
3629e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3630e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3631e0c4386eSCy Schubert         goto end;
3632e0c4386eSCy Schubert 
3633e0c4386eSCy Schubert     /* Client and server should not be able to write/read early data now */
3634e0c4386eSCy Schubert     if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3635e0c4386eSCy Schubert                                          &written)))
3636e0c4386eSCy Schubert         goto end;
3637e0c4386eSCy Schubert     ERR_clear_error();
3638e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3639e0c4386eSCy Schubert                                          &readbytes),
3640e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_ERROR))
3641e0c4386eSCy Schubert         goto end;
3642e0c4386eSCy Schubert     ERR_clear_error();
3643e0c4386eSCy Schubert 
3644e0c4386eSCy Schubert     /* Client should be able to read the data sent by the server */
3645e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3646e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
3647e0c4386eSCy Schubert         goto end;
3648e0c4386eSCy Schubert 
3649e0c4386eSCy Schubert     /*
3650e0c4386eSCy Schubert      * Make sure we process the two NewSessionTickets. These arrive
3651e0c4386eSCy Schubert      * post-handshake. We attempt reads which we do not expect to return any
3652e0c4386eSCy Schubert      * data.
3653e0c4386eSCy Schubert      */
3654e0c4386eSCy Schubert     if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3655e0c4386eSCy Schubert             || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
3656e0c4386eSCy Schubert                            &readbytes)))
3657e0c4386eSCy Schubert         goto end;
3658e0c4386eSCy Schubert 
3659e0c4386eSCy Schubert     /* Server should be able to write normal data */
3660e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
3661e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG7))
3662e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3663e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
3664e0c4386eSCy Schubert         goto end;
3665e0c4386eSCy Schubert 
3666e0c4386eSCy Schubert     SSL_SESSION_free(sess);
3667e0c4386eSCy Schubert     sess = SSL_get1_session(clientssl);
3668e0c4386eSCy Schubert     use_session_cb_cnt = 0;
3669e0c4386eSCy Schubert     find_session_cb_cnt = 0;
3670e0c4386eSCy Schubert 
3671e0c4386eSCy Schubert     SSL_shutdown(clientssl);
3672e0c4386eSCy Schubert     SSL_shutdown(serverssl);
3673e0c4386eSCy Schubert     SSL_free(serverssl);
3674e0c4386eSCy Schubert     SSL_free(clientssl);
3675e0c4386eSCy Schubert     serverssl = clientssl = NULL;
3676e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3677e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
3678e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, sess)))
3679e0c4386eSCy Schubert         goto end;
3680e0c4386eSCy Schubert 
3681e0c4386eSCy Schubert     /* Write and read some early data */
3682e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3683e0c4386eSCy Schubert                                         &written))
3684e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG1))
3685e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3686e0c4386eSCy Schubert                                                 &readbytes),
3687e0c4386eSCy Schubert                             SSL_READ_EARLY_DATA_SUCCESS)
3688e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
3689e0c4386eSCy Schubert         goto end;
3690e0c4386eSCy Schubert 
3691e0c4386eSCy Schubert     if (!TEST_int_gt(SSL_connect(clientssl), 0)
3692e0c4386eSCy Schubert             || !TEST_int_gt(SSL_accept(serverssl), 0))
3693e0c4386eSCy Schubert         goto end;
3694e0c4386eSCy Schubert 
3695e0c4386eSCy Schubert     /* Client and server should not be able to write/read early data now */
3696e0c4386eSCy Schubert     if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3697e0c4386eSCy Schubert                                          &written)))
3698e0c4386eSCy Schubert         goto end;
3699e0c4386eSCy Schubert     ERR_clear_error();
3700e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3701e0c4386eSCy Schubert                                          &readbytes),
3702e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_ERROR))
3703e0c4386eSCy Schubert         goto end;
3704e0c4386eSCy Schubert     ERR_clear_error();
3705e0c4386eSCy Schubert 
3706e0c4386eSCy Schubert     /* Client and server should be able to write/read normal data */
3707e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3708e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG5))
3709e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3710e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3711e0c4386eSCy Schubert         goto end;
3712e0c4386eSCy Schubert 
3713e0c4386eSCy Schubert     testresult = 1;
3714e0c4386eSCy Schubert 
3715e0c4386eSCy Schubert  end:
3716e0c4386eSCy Schubert     SSL_SESSION_free(sess);
3717e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
3718e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
3719e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
3720e0c4386eSCy Schubert     SSL_free(serverssl);
3721e0c4386eSCy Schubert     SSL_free(clientssl);
3722e0c4386eSCy Schubert     SSL_CTX_free(sctx);
3723e0c4386eSCy Schubert     SSL_CTX_free(cctx);
3724e0c4386eSCy Schubert     return testresult;
3725e0c4386eSCy Schubert }
3726e0c4386eSCy Schubert 
3727e0c4386eSCy Schubert static int allow_ed_cb_called = 0;
3728e0c4386eSCy Schubert 
allow_early_data_cb(SSL * s,void * arg)3729e0c4386eSCy Schubert static int allow_early_data_cb(SSL *s, void *arg)
3730e0c4386eSCy Schubert {
3731e0c4386eSCy Schubert     int *usecb = (int *)arg;
3732e0c4386eSCy Schubert 
3733e0c4386eSCy Schubert     allow_ed_cb_called++;
3734e0c4386eSCy Schubert 
3735e0c4386eSCy Schubert     if (*usecb == 1)
3736e0c4386eSCy Schubert         return 0;
3737e0c4386eSCy Schubert 
3738e0c4386eSCy Schubert     return 1;
3739e0c4386eSCy Schubert }
3740e0c4386eSCy Schubert 
3741e0c4386eSCy Schubert /*
3742e0c4386eSCy Schubert  * idx == 0: Standard early_data setup
3743e0c4386eSCy Schubert  * idx == 1: early_data setup using read_ahead
3744e0c4386eSCy Schubert  * usecb == 0: Don't use a custom early data callback
3745e0c4386eSCy Schubert  * usecb == 1: Use a custom early data callback and reject the early data
3746e0c4386eSCy Schubert  * usecb == 2: Use a custom early data callback and accept the early data
3747e0c4386eSCy Schubert  * confopt == 0: Configure anti-replay directly
3748e0c4386eSCy Schubert  * confopt == 1: Configure anti-replay using SSL_CONF
3749e0c4386eSCy Schubert  */
test_early_data_replay_int(int idx,int usecb,int confopt)3750e0c4386eSCy Schubert static int test_early_data_replay_int(int idx, int usecb, int confopt)
3751e0c4386eSCy Schubert {
3752e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
3753e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
3754e0c4386eSCy Schubert     int testresult = 0;
3755e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
3756e0c4386eSCy Schubert     size_t readbytes, written;
3757e0c4386eSCy Schubert     unsigned char buf[20];
375844096ebdSEnji Cooper     time_t timer;
3759e0c4386eSCy Schubert 
3760e0c4386eSCy Schubert     allow_ed_cb_called = 0;
3761e0c4386eSCy Schubert 
3762e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3763e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
3764e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
3765e0c4386eSCy Schubert         return 0;
3766e0c4386eSCy Schubert 
3767e0c4386eSCy Schubert     if (usecb > 0) {
3768e0c4386eSCy Schubert         if (confopt == 0) {
3769e0c4386eSCy Schubert             SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
3770e0c4386eSCy Schubert         } else {
3771e0c4386eSCy Schubert             SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
3772e0c4386eSCy Schubert 
3773e0c4386eSCy Schubert             if (!TEST_ptr(confctx))
3774e0c4386eSCy Schubert                 goto end;
3775e0c4386eSCy Schubert             SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
3776e0c4386eSCy Schubert                                             | SSL_CONF_FLAG_SERVER);
3777e0c4386eSCy Schubert             SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
3778e0c4386eSCy Schubert             if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
3779e0c4386eSCy Schubert                              2)) {
3780e0c4386eSCy Schubert                 SSL_CONF_CTX_free(confctx);
3781e0c4386eSCy Schubert                 goto end;
3782e0c4386eSCy Schubert             }
3783e0c4386eSCy Schubert             SSL_CONF_CTX_free(confctx);
3784e0c4386eSCy Schubert         }
3785e0c4386eSCy Schubert         SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
3786e0c4386eSCy Schubert     }
3787e0c4386eSCy Schubert 
3788e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3789e0c4386eSCy Schubert                                         &serverssl, &sess, idx,
3790e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
3791e0c4386eSCy Schubert         goto end;
3792e0c4386eSCy Schubert 
3793e0c4386eSCy Schubert     /*
3794e0c4386eSCy Schubert      * The server is configured to accept early data. Create a connection to
3795e0c4386eSCy Schubert      * "use up" the ticket
3796e0c4386eSCy Schubert      */
3797e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3798e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
3799e0c4386eSCy Schubert         goto end;
3800e0c4386eSCy Schubert 
3801e0c4386eSCy Schubert     SSL_shutdown(clientssl);
3802e0c4386eSCy Schubert     SSL_shutdown(serverssl);
3803e0c4386eSCy Schubert     SSL_free(serverssl);
3804e0c4386eSCy Schubert     SSL_free(clientssl);
3805e0c4386eSCy Schubert     serverssl = clientssl = NULL;
3806e0c4386eSCy Schubert 
3807e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3808e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
3809e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, sess)))
3810e0c4386eSCy Schubert         goto end;
3811e0c4386eSCy Schubert 
3812e0c4386eSCy Schubert     /* Write and read some early data */
381344096ebdSEnji Cooper     timer = time(NULL);
3814e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3815e0c4386eSCy Schubert                                         &written))
3816e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG1)))
3817e0c4386eSCy Schubert         goto end;
3818e0c4386eSCy Schubert 
3819e0c4386eSCy Schubert     if (usecb <= 1) {
3820e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3821e0c4386eSCy Schubert                                              &readbytes),
3822e0c4386eSCy Schubert                          SSL_READ_EARLY_DATA_FINISH)
3823e0c4386eSCy Schubert                    /*
3824e0c4386eSCy Schubert                     * The ticket was reused, so the we should have rejected the
3825e0c4386eSCy Schubert                     * early data
3826e0c4386eSCy Schubert                     */
3827e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3828e0c4386eSCy Schubert                                 SSL_EARLY_DATA_REJECTED))
3829e0c4386eSCy Schubert             goto end;
3830e0c4386eSCy Schubert     } else {
3831e0c4386eSCy Schubert         /* In this case the callback decides to accept the early data */
3832e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3833e0c4386eSCy Schubert                                              &readbytes),
383444096ebdSEnji Cooper                          SSL_READ_EARLY_DATA_SUCCESS)) {
383544096ebdSEnji Cooper             testresult = check_early_data_timeout(timer);
383644096ebdSEnji Cooper             goto end;
383744096ebdSEnji Cooper         }
383844096ebdSEnji Cooper         if (!TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
3839e0c4386eSCy Schubert                    /*
3840e0c4386eSCy Schubert                     * Server will have sent its flight so client can now send
3841e0c4386eSCy Schubert                     * end of early data and complete its half of the handshake
3842e0c4386eSCy Schubert                     */
3843e0c4386eSCy Schubert                 || !TEST_int_gt(SSL_connect(clientssl), 0)
3844e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3845e0c4386eSCy Schubert                                              &readbytes),
3846e0c4386eSCy Schubert                                 SSL_READ_EARLY_DATA_FINISH)
3847e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3848e0c4386eSCy Schubert                                 SSL_EARLY_DATA_ACCEPTED))
3849e0c4386eSCy Schubert             goto end;
3850e0c4386eSCy Schubert     }
3851e0c4386eSCy Schubert 
3852e0c4386eSCy Schubert     /* Complete the connection */
3853e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3854e0c4386eSCy Schubert             || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
3855e0c4386eSCy Schubert             || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
3856e0c4386eSCy Schubert         goto end;
3857e0c4386eSCy Schubert 
3858e0c4386eSCy Schubert     testresult = 1;
3859e0c4386eSCy Schubert 
3860e0c4386eSCy Schubert  end:
3861e0c4386eSCy Schubert     SSL_SESSION_free(sess);
3862e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
3863e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
3864e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
3865e0c4386eSCy Schubert     SSL_free(serverssl);
3866e0c4386eSCy Schubert     SSL_free(clientssl);
3867e0c4386eSCy Schubert     SSL_CTX_free(sctx);
3868e0c4386eSCy Schubert     SSL_CTX_free(cctx);
3869e0c4386eSCy Schubert     return testresult;
3870e0c4386eSCy Schubert }
3871e0c4386eSCy Schubert 
test_early_data_replay(int idx)3872e0c4386eSCy Schubert static int test_early_data_replay(int idx)
3873e0c4386eSCy Schubert {
3874e0c4386eSCy Schubert     int ret = 1, usecb, confopt;
3875e0c4386eSCy Schubert 
3876e0c4386eSCy Schubert     for (usecb = 0; usecb < 3; usecb++) {
3877e0c4386eSCy Schubert         for (confopt = 0; confopt < 2; confopt++)
3878e0c4386eSCy Schubert             ret &= test_early_data_replay_int(idx, usecb, confopt);
3879e0c4386eSCy Schubert     }
3880e0c4386eSCy Schubert 
3881e0c4386eSCy Schubert     return ret;
3882e0c4386eSCy Schubert }
3883e0c4386eSCy Schubert 
3884e0c4386eSCy Schubert static const char *ciphersuites[] = {
3885e0c4386eSCy Schubert     "TLS_AES_128_CCM_8_SHA256",
3886e0c4386eSCy Schubert     "TLS_AES_128_GCM_SHA256",
3887e0c4386eSCy Schubert     "TLS_AES_256_GCM_SHA384",
3888e0c4386eSCy Schubert     "TLS_AES_128_CCM_SHA256",
3889e0c4386eSCy Schubert #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
3890e0c4386eSCy Schubert     "TLS_CHACHA20_POLY1305_SHA256"
3891e0c4386eSCy Schubert #endif
3892e0c4386eSCy Schubert };
3893e0c4386eSCy Schubert 
3894e0c4386eSCy Schubert /*
3895e0c4386eSCy Schubert  * Helper function to test that a server attempting to read early data can
3896e0c4386eSCy Schubert  * handle a connection from a client where the early data should be skipped.
3897e0c4386eSCy Schubert  * testtype: 0 == No HRR
3898e0c4386eSCy Schubert  * testtype: 1 == HRR
3899e0c4386eSCy Schubert  * testtype: 2 == HRR, invalid early_data sent after HRR
3900e0c4386eSCy Schubert  * testtype: 3 == recv_max_early_data set to 0
3901e0c4386eSCy Schubert  */
early_data_skip_helper(int testtype,int cipher,int idx)3902e0c4386eSCy Schubert static int early_data_skip_helper(int testtype, int cipher, int idx)
3903e0c4386eSCy Schubert {
3904e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
3905e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
3906e0c4386eSCy Schubert     int testresult = 0;
3907e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
3908e0c4386eSCy Schubert     unsigned char buf[20];
3909e0c4386eSCy Schubert     size_t readbytes, written;
3910e0c4386eSCy Schubert 
3911e0c4386eSCy Schubert     if (is_fips && cipher == 4)
3912e0c4386eSCy Schubert         return 1;
3913e0c4386eSCy Schubert 
3914e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3915e0c4386eSCy Schubert                                               TLS_client_method(),
3916e0c4386eSCy Schubert                                               TLS1_VERSION, 0,
3917e0c4386eSCy Schubert                                               &sctx, &cctx, cert, privkey)))
3918e0c4386eSCy Schubert         goto end;
3919e0c4386eSCy Schubert 
3920e0c4386eSCy Schubert     if (cipher == 0) {
3921e0c4386eSCy Schubert         SSL_CTX_set_security_level(sctx, 0);
3922e0c4386eSCy Schubert         SSL_CTX_set_security_level(cctx, 0);
3923e0c4386eSCy Schubert     }
3924e0c4386eSCy Schubert 
3925e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher]))
3926e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher])))
3927e0c4386eSCy Schubert         goto end;
3928e0c4386eSCy Schubert 
3929e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3930e0c4386eSCy Schubert                                         &serverssl, &sess, idx,
3931e0c4386eSCy Schubert                                         cipher == 2 ? SHA384_DIGEST_LENGTH
3932e0c4386eSCy Schubert                                                     : SHA256_DIGEST_LENGTH)))
3933e0c4386eSCy Schubert         goto end;
3934e0c4386eSCy Schubert 
3935e0c4386eSCy Schubert     if (testtype == 1 || testtype == 2) {
3936e0c4386eSCy Schubert         /* Force an HRR to occur */
3937e0c4386eSCy Schubert #if defined(OPENSSL_NO_EC)
3938e0c4386eSCy Schubert         if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
3939e0c4386eSCy Schubert             goto end;
3940e0c4386eSCy Schubert #else
3941a7148ab3SEnji Cooper         if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
3942e0c4386eSCy Schubert             goto end;
3943e0c4386eSCy Schubert #endif
3944e0c4386eSCy Schubert     } else if (idx == 2) {
3945e0c4386eSCy Schubert         /*
3946e0c4386eSCy Schubert          * We force early_data rejection by ensuring the PSK identity is
3947e0c4386eSCy Schubert          * unrecognised
3948e0c4386eSCy Schubert          */
3949e0c4386eSCy Schubert         srvid = "Dummy Identity";
3950e0c4386eSCy Schubert     } else {
3951e0c4386eSCy Schubert         /*
3952e0c4386eSCy Schubert          * Deliberately corrupt the creation time. We take 20 seconds off the
3953e0c4386eSCy Schubert          * time. It could be any value as long as it is not within tolerance.
3954e0c4386eSCy Schubert          * This should mean the ticket is rejected.
3955e0c4386eSCy Schubert          */
3956e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
3957e0c4386eSCy Schubert             goto end;
3958e0c4386eSCy Schubert     }
3959e0c4386eSCy Schubert 
3960e0c4386eSCy Schubert     if (testtype == 3
3961e0c4386eSCy Schubert             && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
3962e0c4386eSCy Schubert         goto end;
3963e0c4386eSCy Schubert 
3964e0c4386eSCy Schubert     /* Write some early data */
3965e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3966e0c4386eSCy Schubert                                         &written))
3967e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG1)))
3968e0c4386eSCy Schubert         goto end;
3969e0c4386eSCy Schubert 
3970e0c4386eSCy Schubert     /* Server should reject the early data */
3971e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3972e0c4386eSCy Schubert                                          &readbytes),
3973e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_FINISH)
3974e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, 0)
3975e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3976e0c4386eSCy Schubert                             SSL_EARLY_DATA_REJECTED))
3977e0c4386eSCy Schubert         goto end;
3978e0c4386eSCy Schubert 
3979e0c4386eSCy Schubert     switch (testtype) {
3980e0c4386eSCy Schubert     case 0:
3981e0c4386eSCy Schubert         /* Nothing to do */
3982e0c4386eSCy Schubert         break;
3983e0c4386eSCy Schubert 
3984e0c4386eSCy Schubert     case 1:
3985e0c4386eSCy Schubert         /*
3986e0c4386eSCy Schubert          * Finish off the handshake. We perform the same writes and reads as
3987e0c4386eSCy Schubert          * further down but we expect them to fail due to the incomplete
3988e0c4386eSCy Schubert          * handshake.
3989e0c4386eSCy Schubert          */
3990e0c4386eSCy Schubert         if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3991e0c4386eSCy Schubert                 || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
3992e0c4386eSCy Schubert                                &readbytes)))
3993e0c4386eSCy Schubert             goto end;
3994e0c4386eSCy Schubert         break;
3995e0c4386eSCy Schubert 
3996e0c4386eSCy Schubert     case 2:
3997e0c4386eSCy Schubert         {
3998e0c4386eSCy Schubert             BIO *wbio = SSL_get_wbio(clientssl);
3999e0c4386eSCy Schubert             /* A record that will appear as bad early_data */
4000e0c4386eSCy Schubert             const unsigned char bad_early_data[] = {
4001e0c4386eSCy Schubert                 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
4002e0c4386eSCy Schubert             };
4003e0c4386eSCy Schubert 
4004e0c4386eSCy Schubert             /*
4005e0c4386eSCy Schubert              * We force the client to attempt a write. This will fail because
4006e0c4386eSCy Schubert              * we're still in the handshake. It will cause the second
4007e0c4386eSCy Schubert              * ClientHello to be sent.
4008e0c4386eSCy Schubert              */
4009e0c4386eSCy Schubert             if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
4010e0c4386eSCy Schubert                                          &written)))
4011e0c4386eSCy Schubert                 goto end;
4012e0c4386eSCy Schubert 
4013e0c4386eSCy Schubert             /*
4014e0c4386eSCy Schubert              * Inject some early_data after the second ClientHello. This should
4015e0c4386eSCy Schubert              * cause the server to fail
4016e0c4386eSCy Schubert              */
4017e0c4386eSCy Schubert             if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
4018e0c4386eSCy Schubert                                         sizeof(bad_early_data), &written)))
4019e0c4386eSCy Schubert                 goto end;
4020e0c4386eSCy Schubert         }
4021e0c4386eSCy Schubert         /* fallthrough */
4022e0c4386eSCy Schubert 
4023e0c4386eSCy Schubert     case 3:
4024e0c4386eSCy Schubert         /*
4025e0c4386eSCy Schubert          * This client has sent more early_data than we are willing to skip
4026e0c4386eSCy Schubert          * (case 3) or sent invalid early_data (case 2) so the connection should
4027e0c4386eSCy Schubert          * abort.
4028e0c4386eSCy Schubert          */
4029e0c4386eSCy Schubert         if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4030e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
4031e0c4386eSCy Schubert             goto end;
4032e0c4386eSCy Schubert 
4033e0c4386eSCy Schubert         /* Connection has failed - nothing more to do */
4034e0c4386eSCy Schubert         testresult = 1;
4035e0c4386eSCy Schubert         goto end;
4036e0c4386eSCy Schubert 
4037e0c4386eSCy Schubert     default:
4038e0c4386eSCy Schubert         TEST_error("Invalid test type");
4039e0c4386eSCy Schubert         goto end;
4040e0c4386eSCy Schubert     }
4041e0c4386eSCy Schubert 
4042e0c4386eSCy Schubert     ERR_clear_error();
4043e0c4386eSCy Schubert     /*
4044e0c4386eSCy Schubert      * Should be able to send normal data despite rejection of early data. The
4045e0c4386eSCy Schubert      * early_data should be skipped.
4046e0c4386eSCy Schubert      */
4047e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4048e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2))
4049e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4050e0c4386eSCy Schubert                             SSL_EARLY_DATA_REJECTED)
4051e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4052e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4053e0c4386eSCy Schubert         goto end;
4054e0c4386eSCy Schubert 
4055e0c4386eSCy Schubert     /*
4056e0c4386eSCy Schubert      * Failure to decrypt early data records should not leave spurious errors
4057e0c4386eSCy Schubert      * on the error stack
4058e0c4386eSCy Schubert      */
4059e0c4386eSCy Schubert     if (!TEST_long_eq(ERR_peek_error(), 0))
4060e0c4386eSCy Schubert         goto end;
4061e0c4386eSCy Schubert 
4062e0c4386eSCy Schubert     testresult = 1;
4063e0c4386eSCy Schubert 
4064e0c4386eSCy Schubert  end:
4065e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4066e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4067e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4068e0c4386eSCy Schubert     SSL_SESSION_free(sess);
4069e0c4386eSCy Schubert     SSL_free(serverssl);
4070e0c4386eSCy Schubert     SSL_free(clientssl);
4071e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4072e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4073e0c4386eSCy Schubert     return testresult;
4074e0c4386eSCy Schubert }
4075e0c4386eSCy Schubert 
4076e0c4386eSCy Schubert /*
4077e0c4386eSCy Schubert  * Test that a server attempting to read early data can handle a connection
4078e0c4386eSCy Schubert  * from a client where the early data is not acceptable.
4079e0c4386eSCy Schubert  */
test_early_data_skip(int idx)4080e0c4386eSCy Schubert static int test_early_data_skip(int idx)
4081e0c4386eSCy Schubert {
4082e0c4386eSCy Schubert     return early_data_skip_helper(0,
4083e0c4386eSCy Schubert                                   idx % OSSL_NELEM(ciphersuites),
4084e0c4386eSCy Schubert                                   idx / OSSL_NELEM(ciphersuites));
4085e0c4386eSCy Schubert }
4086e0c4386eSCy Schubert 
4087e0c4386eSCy Schubert /*
4088e0c4386eSCy Schubert  * Test that a server attempting to read early data can handle a connection
4089e0c4386eSCy Schubert  * from a client where an HRR occurs.
4090e0c4386eSCy Schubert  */
test_early_data_skip_hrr(int idx)4091e0c4386eSCy Schubert static int test_early_data_skip_hrr(int idx)
4092e0c4386eSCy Schubert {
4093e0c4386eSCy Schubert     return early_data_skip_helper(1,
4094e0c4386eSCy Schubert                                   idx % OSSL_NELEM(ciphersuites),
4095e0c4386eSCy Schubert                                   idx / OSSL_NELEM(ciphersuites));
4096e0c4386eSCy Schubert }
4097e0c4386eSCy Schubert 
4098e0c4386eSCy Schubert /*
4099e0c4386eSCy Schubert  * Test that a server attempting to read early data can handle a connection
4100e0c4386eSCy Schubert  * from a client where an HRR occurs and correctly fails if early_data is sent
4101e0c4386eSCy Schubert  * after the HRR
4102e0c4386eSCy Schubert  */
test_early_data_skip_hrr_fail(int idx)4103e0c4386eSCy Schubert static int test_early_data_skip_hrr_fail(int idx)
4104e0c4386eSCy Schubert {
4105e0c4386eSCy Schubert     return early_data_skip_helper(2,
4106e0c4386eSCy Schubert                                   idx % OSSL_NELEM(ciphersuites),
4107e0c4386eSCy Schubert                                   idx / OSSL_NELEM(ciphersuites));
4108e0c4386eSCy Schubert }
4109e0c4386eSCy Schubert 
4110e0c4386eSCy Schubert /*
4111e0c4386eSCy Schubert  * Test that a server attempting to read early data will abort if it tries to
4112e0c4386eSCy Schubert  * skip over too much.
4113e0c4386eSCy Schubert  */
test_early_data_skip_abort(int idx)4114e0c4386eSCy Schubert static int test_early_data_skip_abort(int idx)
4115e0c4386eSCy Schubert {
4116e0c4386eSCy Schubert     return early_data_skip_helper(3,
4117e0c4386eSCy Schubert                                   idx % OSSL_NELEM(ciphersuites),
4118e0c4386eSCy Schubert                                   idx / OSSL_NELEM(ciphersuites));
4119e0c4386eSCy Schubert }
4120e0c4386eSCy Schubert 
4121e0c4386eSCy Schubert /*
4122e0c4386eSCy Schubert  * Test that a server attempting to read early data can handle a connection
4123e0c4386eSCy Schubert  * from a client that doesn't send any.
4124e0c4386eSCy Schubert  */
test_early_data_not_sent(int idx)4125e0c4386eSCy Schubert static int test_early_data_not_sent(int idx)
4126e0c4386eSCy Schubert {
4127e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4128e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4129e0c4386eSCy Schubert     int testresult = 0;
4130e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
4131e0c4386eSCy Schubert     unsigned char buf[20];
4132e0c4386eSCy Schubert     size_t readbytes, written;
4133e0c4386eSCy Schubert 
4134e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4135e0c4386eSCy Schubert                                         &serverssl, &sess, idx,
4136e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
4137e0c4386eSCy Schubert         goto end;
4138e0c4386eSCy Schubert 
4139e0c4386eSCy Schubert     /* Write some data - should block due to handshake with server */
4140e0c4386eSCy Schubert     SSL_set_connect_state(clientssl);
4141e0c4386eSCy Schubert     if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4142e0c4386eSCy Schubert         goto end;
4143e0c4386eSCy Schubert 
4144e0c4386eSCy Schubert     /* Server should detect that early data has not been sent */
4145e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4146e0c4386eSCy Schubert                                          &readbytes),
4147e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_FINISH)
4148e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, 0)
4149e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4150e0c4386eSCy Schubert                             SSL_EARLY_DATA_NOT_SENT)
4151e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4152e0c4386eSCy Schubert                             SSL_EARLY_DATA_NOT_SENT))
4153e0c4386eSCy Schubert         goto end;
4154e0c4386eSCy Schubert 
4155e0c4386eSCy Schubert     /* Continue writing the message we started earlier */
4156e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4157e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG1))
4158e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4159e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4160e0c4386eSCy Schubert             || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
4161e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2)))
4162e0c4386eSCy Schubert         goto end;
4163e0c4386eSCy Schubert 
4164e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
4165e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4166e0c4386eSCy Schubert         goto end;
4167e0c4386eSCy Schubert 
4168e0c4386eSCy Schubert     testresult = 1;
4169e0c4386eSCy Schubert 
4170e0c4386eSCy Schubert  end:
4171e0c4386eSCy Schubert     SSL_SESSION_free(sess);
4172e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4173e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4174e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4175e0c4386eSCy Schubert     SSL_free(serverssl);
4176e0c4386eSCy Schubert     SSL_free(clientssl);
4177e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4178e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4179e0c4386eSCy Schubert     return testresult;
4180e0c4386eSCy Schubert }
4181e0c4386eSCy Schubert 
4182e0c4386eSCy Schubert static const char *servalpn;
4183e0c4386eSCy Schubert 
alpn_select_cb(SSL * ssl,const unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)4184e0c4386eSCy Schubert static int alpn_select_cb(SSL *ssl, const unsigned char **out,
4185e0c4386eSCy Schubert                           unsigned char *outlen, const unsigned char *in,
4186e0c4386eSCy Schubert                           unsigned int inlen, void *arg)
4187e0c4386eSCy Schubert {
4188e0c4386eSCy Schubert     unsigned int protlen = 0;
4189e0c4386eSCy Schubert     const unsigned char *prot;
4190e0c4386eSCy Schubert 
4191e0c4386eSCy Schubert     for (prot = in; prot < in + inlen; prot += protlen) {
4192e0c4386eSCy Schubert         protlen = *prot++;
4193e0c4386eSCy Schubert         if (in + inlen < prot + protlen)
4194e0c4386eSCy Schubert             return SSL_TLSEXT_ERR_NOACK;
4195e0c4386eSCy Schubert 
4196e0c4386eSCy Schubert         if (protlen == strlen(servalpn)
4197e0c4386eSCy Schubert                 && memcmp(prot, servalpn, protlen) == 0) {
4198e0c4386eSCy Schubert             *out = prot;
4199e0c4386eSCy Schubert             *outlen = protlen;
4200e0c4386eSCy Schubert             return SSL_TLSEXT_ERR_OK;
4201e0c4386eSCy Schubert         }
4202e0c4386eSCy Schubert     }
4203e0c4386eSCy Schubert 
4204e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_NOACK;
4205e0c4386eSCy Schubert }
4206e0c4386eSCy Schubert 
4207e0c4386eSCy Schubert /* Test that a PSK can be used to send early_data */
test_early_data_psk(int idx)4208e0c4386eSCy Schubert static int test_early_data_psk(int idx)
4209e0c4386eSCy Schubert {
4210e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4211e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4212e0c4386eSCy Schubert     int testresult = 0;
4213e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
4214e0c4386eSCy Schubert     unsigned char alpnlist[] = {
4215e0c4386eSCy Schubert         0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
4216e0c4386eSCy Schubert         'l', 'p', 'n'
4217e0c4386eSCy Schubert     };
4218e0c4386eSCy Schubert #define GOODALPNLEN     9
4219e0c4386eSCy Schubert #define BADALPNLEN      8
4220e0c4386eSCy Schubert #define GOODALPN        (alpnlist)
4221e0c4386eSCy Schubert #define BADALPN         (alpnlist + GOODALPNLEN)
4222e0c4386eSCy Schubert     int err = 0;
4223e0c4386eSCy Schubert     unsigned char buf[20];
4224e0c4386eSCy Schubert     size_t readbytes, written;
4225e0c4386eSCy Schubert     int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
4226e0c4386eSCy Schubert     int edstatus = SSL_EARLY_DATA_ACCEPTED;
4227e0c4386eSCy Schubert 
4228e0c4386eSCy Schubert     /* We always set this up with a final parameter of "2" for PSK */
4229e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4230e0c4386eSCy Schubert                                         &serverssl, &sess, 2,
4231e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
4232e0c4386eSCy Schubert         goto end;
4233e0c4386eSCy Schubert 
4234e0c4386eSCy Schubert     servalpn = "goodalpn";
4235e0c4386eSCy Schubert 
4236e0c4386eSCy Schubert     /*
4237e0c4386eSCy Schubert      * Note: There is no test for inconsistent SNI with late client detection.
4238e0c4386eSCy Schubert      * This is because servers do not acknowledge SNI even if they are using
4239e0c4386eSCy Schubert      * it in a resumption handshake - so it is not actually possible for a
4240e0c4386eSCy Schubert      * client to detect a problem.
4241e0c4386eSCy Schubert      */
4242e0c4386eSCy Schubert     switch (idx) {
4243e0c4386eSCy Schubert     case 0:
4244e0c4386eSCy Schubert         /* Set inconsistent SNI (early client detection) */
4245e0c4386eSCy Schubert         err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
4246e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4247e0c4386eSCy Schubert                 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
4248e0c4386eSCy Schubert             goto end;
4249e0c4386eSCy Schubert         break;
4250e0c4386eSCy Schubert 
4251e0c4386eSCy Schubert     case 1:
4252e0c4386eSCy Schubert         /* Set inconsistent ALPN (early client detection) */
4253e0c4386eSCy Schubert         err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
4254e0c4386eSCy Schubert         /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
4255e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
4256e0c4386eSCy Schubert                                                       GOODALPNLEN))
4257e0c4386eSCy Schubert                 || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
4258e0c4386eSCy Schubert                                                    BADALPNLEN)))
4259e0c4386eSCy Schubert             goto end;
4260e0c4386eSCy Schubert         break;
4261e0c4386eSCy Schubert 
4262e0c4386eSCy Schubert     case 2:
4263e0c4386eSCy Schubert         /*
4264e0c4386eSCy Schubert          * Set invalid protocol version. Technically this affects PSKs without
4265e0c4386eSCy Schubert          * early_data too, but we test it here because it is similar to the
4266e0c4386eSCy Schubert          * SNI/ALPN consistency tests.
4267e0c4386eSCy Schubert          */
4268e0c4386eSCy Schubert         err = SSL_R_BAD_PSK;
4269e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
4270e0c4386eSCy Schubert             goto end;
4271e0c4386eSCy Schubert         break;
4272e0c4386eSCy Schubert 
4273e0c4386eSCy Schubert     case 3:
4274e0c4386eSCy Schubert         /*
4275e0c4386eSCy Schubert          * Set inconsistent SNI (server side). In this case the connection
4276e0c4386eSCy Schubert          * will succeed and accept early_data. In TLSv1.3 on the server side SNI
4277e0c4386eSCy Schubert          * is associated with each handshake - not the session. Therefore it
4278e0c4386eSCy Schubert          * should not matter that we used a different server name last time.
4279e0c4386eSCy Schubert          */
4280e0c4386eSCy Schubert         SSL_SESSION_free(serverpsk);
4281e0c4386eSCy Schubert         serverpsk = SSL_SESSION_dup(clientpsk);
4282e0c4386eSCy Schubert         if (!TEST_ptr(serverpsk)
4283e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
4284e0c4386eSCy Schubert             goto end;
4285e0c4386eSCy Schubert         /* Fall through */
4286e0c4386eSCy Schubert     case 4:
4287e0c4386eSCy Schubert         /* Set consistent SNI */
4288e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4289e0c4386eSCy Schubert                 || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
4290e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
4291e0c4386eSCy Schubert                                 hostname_cb)))
4292e0c4386eSCy Schubert             goto end;
4293e0c4386eSCy Schubert         break;
4294e0c4386eSCy Schubert 
4295e0c4386eSCy Schubert     case 5:
4296e0c4386eSCy Schubert         /*
4297e0c4386eSCy Schubert          * Set inconsistent ALPN (server detected). In this case the connection
4298e0c4386eSCy Schubert          * will succeed but reject early_data.
4299e0c4386eSCy Schubert          */
4300e0c4386eSCy Schubert         servalpn = "badalpn";
4301e0c4386eSCy Schubert         edstatus = SSL_EARLY_DATA_REJECTED;
4302e0c4386eSCy Schubert         readearlyres = SSL_READ_EARLY_DATA_FINISH;
4303e0c4386eSCy Schubert         /* Fall through */
4304e0c4386eSCy Schubert     case 6:
4305e0c4386eSCy Schubert         /*
4306e0c4386eSCy Schubert          * Set consistent ALPN.
4307e0c4386eSCy Schubert          * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
4308e0c4386eSCy Schubert          * accepts a list of protos (each one length prefixed).
4309e0c4386eSCy Schubert          * SSL_set1_alpn_selected accepts a single protocol (not length
4310e0c4386eSCy Schubert          * prefixed)
4311e0c4386eSCy Schubert          */
4312e0c4386eSCy Schubert         if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
4313e0c4386eSCy Schubert                                                       GOODALPNLEN - 1))
4314e0c4386eSCy Schubert                 || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
4315e0c4386eSCy Schubert                                                    GOODALPNLEN)))
4316e0c4386eSCy Schubert             goto end;
4317e0c4386eSCy Schubert 
4318e0c4386eSCy Schubert         SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4319e0c4386eSCy Schubert         break;
4320e0c4386eSCy Schubert 
4321e0c4386eSCy Schubert     case 7:
4322e0c4386eSCy Schubert         /* Set inconsistent ALPN (late client detection) */
4323e0c4386eSCy Schubert         SSL_SESSION_free(serverpsk);
4324e0c4386eSCy Schubert         serverpsk = SSL_SESSION_dup(clientpsk);
4325e0c4386eSCy Schubert         if (!TEST_ptr(serverpsk)
4326e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
4327e0c4386eSCy Schubert                                                              BADALPN + 1,
4328e0c4386eSCy Schubert                                                              BADALPNLEN - 1))
4329e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
4330e0c4386eSCy Schubert                                                              GOODALPN + 1,
4331e0c4386eSCy Schubert                                                              GOODALPNLEN - 1))
4332e0c4386eSCy Schubert                 || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
4333e0c4386eSCy Schubert                                                    sizeof(alpnlist))))
4334e0c4386eSCy Schubert             goto end;
4335e0c4386eSCy Schubert         SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4336e0c4386eSCy Schubert         edstatus = SSL_EARLY_DATA_ACCEPTED;
4337e0c4386eSCy Schubert         readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
4338e0c4386eSCy Schubert         /* SSL_connect() call should fail */
4339e0c4386eSCy Schubert         connectres = -1;
4340e0c4386eSCy Schubert         break;
4341e0c4386eSCy Schubert 
4342e0c4386eSCy Schubert     default:
4343e0c4386eSCy Schubert         TEST_error("Bad test index");
4344e0c4386eSCy Schubert         goto end;
4345e0c4386eSCy Schubert     }
4346e0c4386eSCy Schubert 
4347e0c4386eSCy Schubert     SSL_set_connect_state(clientssl);
4348e0c4386eSCy Schubert     if (err != 0) {
4349e0c4386eSCy Schubert         if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4350e0c4386eSCy Schubert                                             &written))
4351e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
4352e0c4386eSCy Schubert                 || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
4353e0c4386eSCy Schubert             goto end;
4354e0c4386eSCy Schubert     } else {
435544096ebdSEnji Cooper         time_t timer = time(NULL);
435644096ebdSEnji Cooper 
4357e0c4386eSCy Schubert         if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4358e0c4386eSCy Schubert                                             &written)))
4359e0c4386eSCy Schubert             goto end;
4360e0c4386eSCy Schubert 
4361e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
436244096ebdSEnji Cooper                                              &readbytes), readearlyres)) {
436344096ebdSEnji Cooper             testresult = check_early_data_timeout(timer);
436444096ebdSEnji Cooper             goto end;
436544096ebdSEnji Cooper         }
436644096ebdSEnji Cooper 
436744096ebdSEnji Cooper         if ((readearlyres == SSL_READ_EARLY_DATA_SUCCESS
4368e0c4386eSCy Schubert                     && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
4369e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
4370e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_connect(clientssl), connectres))
4371e0c4386eSCy Schubert             goto end;
4372e0c4386eSCy Schubert     }
4373e0c4386eSCy Schubert 
4374e0c4386eSCy Schubert     testresult = 1;
4375e0c4386eSCy Schubert 
4376e0c4386eSCy Schubert  end:
4377e0c4386eSCy Schubert     SSL_SESSION_free(sess);
4378e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4379e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4380e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4381e0c4386eSCy Schubert     SSL_free(serverssl);
4382e0c4386eSCy Schubert     SSL_free(clientssl);
4383e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4384e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4385e0c4386eSCy Schubert     return testresult;
4386e0c4386eSCy Schubert }
4387e0c4386eSCy Schubert 
4388e0c4386eSCy Schubert /*
4389e0c4386eSCy Schubert  * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
4390e0c4386eSCy Schubert  * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
4391e0c4386eSCy Schubert  * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
4392e0c4386eSCy Schubert  * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4393e0c4386eSCy Schubert  * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
4394e0c4386eSCy Schubert  * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
4395e0c4386eSCy Schubert  */
test_early_data_psk_with_all_ciphers(int idx)4396e0c4386eSCy Schubert static int test_early_data_psk_with_all_ciphers(int idx)
4397e0c4386eSCy Schubert {
4398e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4399e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4400e0c4386eSCy Schubert     int testresult = 0;
4401e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
4402e0c4386eSCy Schubert     unsigned char buf[20];
4403e0c4386eSCy Schubert     size_t readbytes, written;
4404e0c4386eSCy Schubert     const SSL_CIPHER *cipher;
440544096ebdSEnji Cooper     time_t timer;
4406e0c4386eSCy Schubert     const char *cipher_str[] = {
4407e0c4386eSCy Schubert         TLS1_3_RFC_AES_128_GCM_SHA256,
4408e0c4386eSCy Schubert         TLS1_3_RFC_AES_256_GCM_SHA384,
4409e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4410e0c4386eSCy Schubert         TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4411e0c4386eSCy Schubert # else
4412e0c4386eSCy Schubert         NULL,
4413e0c4386eSCy Schubert # endif
4414e0c4386eSCy Schubert         TLS1_3_RFC_AES_128_CCM_SHA256,
4415e0c4386eSCy Schubert         TLS1_3_RFC_AES_128_CCM_8_SHA256
4416e0c4386eSCy Schubert     };
4417e0c4386eSCy Schubert     const unsigned char *cipher_bytes[] = {
4418e0c4386eSCy Schubert         TLS13_AES_128_GCM_SHA256_BYTES,
4419e0c4386eSCy Schubert         TLS13_AES_256_GCM_SHA384_BYTES,
4420e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4421e0c4386eSCy Schubert         TLS13_CHACHA20_POLY1305_SHA256_BYTES,
4422e0c4386eSCy Schubert # else
4423e0c4386eSCy Schubert         NULL,
4424e0c4386eSCy Schubert # endif
4425e0c4386eSCy Schubert         TLS13_AES_128_CCM_SHA256_BYTES,
4426e0c4386eSCy Schubert         TLS13_AES_128_CCM_8_SHA256_BYTES
4427e0c4386eSCy Schubert     };
4428e0c4386eSCy Schubert 
4429e0c4386eSCy Schubert     if (cipher_str[idx] == NULL)
4430e0c4386eSCy Schubert         return 1;
4431e0c4386eSCy Schubert     /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
4432e0c4386eSCy Schubert     if (idx == 2 && is_fips == 1)
4433e0c4386eSCy Schubert         return 1;
4434e0c4386eSCy Schubert 
4435e0c4386eSCy Schubert     /* We always set this up with a final parameter of "2" for PSK */
4436e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4437e0c4386eSCy Schubert                                         &serverssl, &sess, 2,
4438e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
4439e0c4386eSCy Schubert         goto end;
4440e0c4386eSCy Schubert 
4441e0c4386eSCy Schubert     if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
4442e0c4386eSCy Schubert             || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
4443e0c4386eSCy Schubert         goto end;
4444e0c4386eSCy Schubert 
4445e0c4386eSCy Schubert     /*
4446e0c4386eSCy Schubert      * 'setupearly_data_test' creates only one instance of SSL_SESSION
4447e0c4386eSCy Schubert      * and assigns to both client and server with incremented reference
4448e0c4386eSCy Schubert      * and the same instance is updated in 'sess'.
4449e0c4386eSCy Schubert      * So updating ciphersuite in 'sess' which will get reflected in
4450e0c4386eSCy Schubert      * PSK handshake using psk use sess and find sess cb.
4451e0c4386eSCy Schubert      */
4452e0c4386eSCy Schubert     cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
4453e0c4386eSCy Schubert     if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
4454e0c4386eSCy Schubert         goto end;
4455e0c4386eSCy Schubert 
4456e0c4386eSCy Schubert     SSL_set_connect_state(clientssl);
445744096ebdSEnji Cooper     timer = time(NULL);
4458e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4459e0c4386eSCy Schubert                                         &written)))
4460e0c4386eSCy Schubert         goto end;
4461e0c4386eSCy Schubert 
4462e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4463e0c4386eSCy Schubert                                          &readbytes),
446444096ebdSEnji Cooper                                          SSL_READ_EARLY_DATA_SUCCESS)) {
446544096ebdSEnji Cooper         testresult = check_early_data_timeout(timer);
446644096ebdSEnji Cooper         goto end;
446744096ebdSEnji Cooper     }
446844096ebdSEnji Cooper 
446944096ebdSEnji Cooper     if (!TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4470e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4471e0c4386eSCy Schubert                                                       SSL_EARLY_DATA_ACCEPTED)
4472e0c4386eSCy Schubert             || !TEST_int_eq(SSL_connect(clientssl), 1)
4473e0c4386eSCy Schubert             || !TEST_int_eq(SSL_accept(serverssl), 1))
4474e0c4386eSCy Schubert         goto end;
4475e0c4386eSCy Schubert 
4476e0c4386eSCy Schubert     /* Send some normal data from client to server */
4477e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4478e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2)))
4479e0c4386eSCy Schubert         goto end;
4480e0c4386eSCy Schubert 
4481e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4482e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4483e0c4386eSCy Schubert         goto end;
4484e0c4386eSCy Schubert 
4485e0c4386eSCy Schubert     testresult = 1;
4486e0c4386eSCy Schubert  end:
4487e0c4386eSCy Schubert     SSL_SESSION_free(sess);
4488e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4489e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4490e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4491e0c4386eSCy Schubert     if (clientssl != NULL)
4492e0c4386eSCy Schubert         SSL_shutdown(clientssl);
4493e0c4386eSCy Schubert     if (serverssl != NULL)
4494e0c4386eSCy Schubert         SSL_shutdown(serverssl);
4495e0c4386eSCy Schubert     SSL_free(serverssl);
4496e0c4386eSCy Schubert     SSL_free(clientssl);
4497e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4498e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4499e0c4386eSCy Schubert     return testresult;
4500e0c4386eSCy Schubert }
4501e0c4386eSCy Schubert 
4502e0c4386eSCy Schubert /*
4503e0c4386eSCy Schubert  * Test that a server that doesn't try to read early data can handle a
4504e0c4386eSCy Schubert  * client sending some.
4505e0c4386eSCy Schubert  */
test_early_data_not_expected(int idx)4506e0c4386eSCy Schubert static int test_early_data_not_expected(int idx)
4507e0c4386eSCy Schubert {
4508e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4509e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4510e0c4386eSCy Schubert     int testresult = 0;
4511e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
4512e0c4386eSCy Schubert     unsigned char buf[20];
4513e0c4386eSCy Schubert     size_t readbytes, written;
4514e0c4386eSCy Schubert 
4515e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4516e0c4386eSCy Schubert                                         &serverssl, &sess, idx,
4517e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
4518e0c4386eSCy Schubert         goto end;
4519e0c4386eSCy Schubert 
4520e0c4386eSCy Schubert     /* Write some early data */
4521e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4522e0c4386eSCy Schubert                                         &written)))
4523e0c4386eSCy Schubert         goto end;
4524e0c4386eSCy Schubert 
4525e0c4386eSCy Schubert     /*
4526e0c4386eSCy Schubert      * Server should skip over early data and then block waiting for client to
4527e0c4386eSCy Schubert      * continue handshake
4528e0c4386eSCy Schubert      */
4529e0c4386eSCy Schubert     if (!TEST_int_le(SSL_accept(serverssl), 0)
4530e0c4386eSCy Schubert      || !TEST_int_gt(SSL_connect(clientssl), 0)
4531e0c4386eSCy Schubert      || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4532e0c4386eSCy Schubert                      SSL_EARLY_DATA_REJECTED)
4533e0c4386eSCy Schubert      || !TEST_int_gt(SSL_accept(serverssl), 0)
4534e0c4386eSCy Schubert      || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4535e0c4386eSCy Schubert                      SSL_EARLY_DATA_REJECTED))
4536e0c4386eSCy Schubert         goto end;
4537e0c4386eSCy Schubert 
4538e0c4386eSCy Schubert     /* Send some normal data from client to server */
4539e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4540e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2)))
4541e0c4386eSCy Schubert         goto end;
4542e0c4386eSCy Schubert 
4543e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4544e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4545e0c4386eSCy Schubert         goto end;
4546e0c4386eSCy Schubert 
4547e0c4386eSCy Schubert     testresult = 1;
4548e0c4386eSCy Schubert 
4549e0c4386eSCy Schubert  end:
4550e0c4386eSCy Schubert     SSL_SESSION_free(sess);
4551e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4552e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4553e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4554e0c4386eSCy Schubert     SSL_free(serverssl);
4555e0c4386eSCy Schubert     SSL_free(clientssl);
4556e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4557e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4558e0c4386eSCy Schubert     return testresult;
4559e0c4386eSCy Schubert }
4560e0c4386eSCy Schubert 
4561e0c4386eSCy Schubert 
4562e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
4563e0c4386eSCy Schubert /*
4564e0c4386eSCy Schubert  * Test that a server attempting to read early data can handle a connection
4565e0c4386eSCy Schubert  * from a TLSv1.2 client.
4566e0c4386eSCy Schubert  */
test_early_data_tls1_2(int idx)4567e0c4386eSCy Schubert static int test_early_data_tls1_2(int idx)
4568e0c4386eSCy Schubert {
4569e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4570e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4571e0c4386eSCy Schubert     int testresult = 0;
4572e0c4386eSCy Schubert     unsigned char buf[20];
4573e0c4386eSCy Schubert     size_t readbytes, written;
4574e0c4386eSCy Schubert 
4575e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4576e0c4386eSCy Schubert                                         &serverssl, NULL, idx,
4577e0c4386eSCy Schubert                                         SHA384_DIGEST_LENGTH)))
4578e0c4386eSCy Schubert         goto end;
4579e0c4386eSCy Schubert 
4580e0c4386eSCy Schubert     /* Write some data - should block due to handshake with server */
4581e0c4386eSCy Schubert     SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
4582e0c4386eSCy Schubert     SSL_set_connect_state(clientssl);
4583e0c4386eSCy Schubert     if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4584e0c4386eSCy Schubert         goto end;
4585e0c4386eSCy Schubert 
4586e0c4386eSCy Schubert     /*
4587e0c4386eSCy Schubert      * Server should do TLSv1.2 handshake. First it will block waiting for more
4588e0c4386eSCy Schubert      * messages from client after ServerDone. Then SSL_read_early_data should
4589e0c4386eSCy Schubert      * finish and detect that early data has not been sent
4590e0c4386eSCy Schubert      */
4591e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4592e0c4386eSCy Schubert                                          &readbytes),
4593e0c4386eSCy Schubert                      SSL_READ_EARLY_DATA_ERROR))
4594e0c4386eSCy Schubert         goto end;
4595e0c4386eSCy Schubert 
4596e0c4386eSCy Schubert     /*
4597e0c4386eSCy Schubert      * Continue writing the message we started earlier. Will still block waiting
4598e0c4386eSCy Schubert      * for the CCS/Finished from server
4599e0c4386eSCy Schubert      */
4600e0c4386eSCy Schubert     if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4601e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4602e0c4386eSCy Schubert                                                 &readbytes),
4603e0c4386eSCy Schubert                             SSL_READ_EARLY_DATA_FINISH)
4604e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, 0)
4605e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4606e0c4386eSCy Schubert                             SSL_EARLY_DATA_NOT_SENT))
4607e0c4386eSCy Schubert         goto end;
4608e0c4386eSCy Schubert 
4609e0c4386eSCy Schubert     /* Continue writing the message we started earlier */
4610e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4611e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG1))
4612e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4613e0c4386eSCy Schubert                             SSL_EARLY_DATA_NOT_SENT)
4614e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4615e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4616e0c4386eSCy Schubert             || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
4617e0c4386eSCy Schubert             || !TEST_size_t_eq(written, strlen(MSG2))
4618e0c4386eSCy Schubert             || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
4619e0c4386eSCy Schubert             || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4620e0c4386eSCy Schubert         goto end;
4621e0c4386eSCy Schubert 
4622e0c4386eSCy Schubert     testresult = 1;
4623e0c4386eSCy Schubert 
4624e0c4386eSCy Schubert  end:
4625e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
4626e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
4627e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
4628e0c4386eSCy Schubert     SSL_free(serverssl);
4629e0c4386eSCy Schubert     SSL_free(clientssl);
4630e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4631e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4632e0c4386eSCy Schubert 
4633e0c4386eSCy Schubert     return testresult;
4634e0c4386eSCy Schubert }
4635e0c4386eSCy Schubert # endif /* OPENSSL_NO_TLS1_2 */
4636e0c4386eSCy Schubert 
4637e0c4386eSCy Schubert /*
4638e0c4386eSCy Schubert  * Test configuring the TLSv1.3 ciphersuites
4639e0c4386eSCy Schubert  *
4640e0c4386eSCy Schubert  * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
4641e0c4386eSCy Schubert  * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
4642e0c4386eSCy Schubert  * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
4643e0c4386eSCy Schubert  * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
4644e0c4386eSCy Schubert  * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4645e0c4386eSCy Schubert  * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4646e0c4386eSCy Schubert  * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
4647e0c4386eSCy Schubert  * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
4648e0c4386eSCy Schubert  * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
4649e0c4386eSCy Schubert  * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
4650e0c4386eSCy Schubert  */
test_set_ciphersuite(int idx)4651e0c4386eSCy Schubert static int test_set_ciphersuite(int idx)
4652e0c4386eSCy Schubert {
4653e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4654e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4655e0c4386eSCy Schubert     int testresult = 0;
4656e0c4386eSCy Schubert 
4657e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4658e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
4659e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
4660e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4661e0c4386eSCy Schubert                            "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
4662e0c4386eSCy Schubert         goto end;
4663e0c4386eSCy Schubert 
4664e0c4386eSCy Schubert     if (idx >=4 && idx <= 7) {
4665e0c4386eSCy Schubert         /* SSL_CTX explicit cipher list */
4666e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
4667e0c4386eSCy Schubert             goto end;
4668e0c4386eSCy Schubert     }
4669e0c4386eSCy Schubert 
4670e0c4386eSCy Schubert     if (idx == 0 || idx == 4) {
4671e0c4386eSCy Schubert         /* Default ciphersuite */
4672e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4673e0c4386eSCy Schubert                                                 "TLS_AES_128_GCM_SHA256")))
4674e0c4386eSCy Schubert             goto end;
4675e0c4386eSCy Schubert     } else if (idx == 1 || idx == 5) {
4676e0c4386eSCy Schubert         /* Non default ciphersuite */
4677e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4678e0c4386eSCy Schubert                                                 "TLS_AES_128_CCM_SHA256")))
4679e0c4386eSCy Schubert             goto end;
4680e0c4386eSCy Schubert     }
4681e0c4386eSCy Schubert 
4682e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4683e0c4386eSCy Schubert                                           &clientssl, NULL, NULL)))
4684e0c4386eSCy Schubert         goto end;
4685e0c4386eSCy Schubert 
4686e0c4386eSCy Schubert     if (idx == 8 || idx == 9) {
4687e0c4386eSCy Schubert         /* SSL explicit cipher list */
4688e0c4386eSCy Schubert         if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
4689e0c4386eSCy Schubert             goto end;
4690e0c4386eSCy Schubert     }
4691e0c4386eSCy Schubert 
4692e0c4386eSCy Schubert     if (idx == 2 || idx == 6 || idx == 8) {
4693e0c4386eSCy Schubert         /* Default ciphersuite */
4694e0c4386eSCy Schubert         if (!TEST_true(SSL_set_ciphersuites(clientssl,
4695e0c4386eSCy Schubert                                             "TLS_AES_128_GCM_SHA256")))
4696e0c4386eSCy Schubert             goto end;
4697e0c4386eSCy Schubert     } else if (idx == 3 || idx == 7 || idx == 9) {
4698e0c4386eSCy Schubert         /* Non default ciphersuite */
4699e0c4386eSCy Schubert         if (!TEST_true(SSL_set_ciphersuites(clientssl,
4700e0c4386eSCy Schubert                                             "TLS_AES_128_CCM_SHA256")))
4701e0c4386eSCy Schubert             goto end;
4702e0c4386eSCy Schubert     }
4703e0c4386eSCy Schubert 
4704e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4705e0c4386eSCy Schubert         goto end;
4706e0c4386eSCy Schubert 
4707e0c4386eSCy Schubert     testresult = 1;
4708e0c4386eSCy Schubert 
4709e0c4386eSCy Schubert  end:
4710e0c4386eSCy Schubert     SSL_free(serverssl);
4711e0c4386eSCy Schubert     SSL_free(clientssl);
4712e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4713e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4714e0c4386eSCy Schubert 
4715e0c4386eSCy Schubert     return testresult;
4716e0c4386eSCy Schubert }
4717e0c4386eSCy Schubert 
test_ciphersuite_change(void)4718e0c4386eSCy Schubert static int test_ciphersuite_change(void)
4719e0c4386eSCy Schubert {
4720e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
4721e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
4722e0c4386eSCy Schubert     SSL_SESSION *clntsess = NULL;
4723e0c4386eSCy Schubert     int testresult = 0;
4724e0c4386eSCy Schubert     const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
4725e0c4386eSCy Schubert 
4726e0c4386eSCy Schubert     /* Create a session based on SHA-256 */
4727e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4728e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
4729e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
4730e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4731e0c4386eSCy Schubert                                                    "TLS_AES_128_GCM_SHA256:"
4732e0c4386eSCy Schubert                                                    "TLS_AES_256_GCM_SHA384:"
4733e0c4386eSCy Schubert                                                    "TLS_AES_128_CCM_SHA256"))
4734e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4735e0c4386eSCy Schubert                                                    "TLS_AES_128_GCM_SHA256"))
4736e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4737e0c4386eSCy Schubert                                           &clientssl, NULL, NULL))
4738e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
4739e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
4740e0c4386eSCy Schubert         goto end;
4741e0c4386eSCy Schubert 
4742e0c4386eSCy Schubert     clntsess = SSL_get1_session(clientssl);
4743e0c4386eSCy Schubert     /* Save for later */
4744e0c4386eSCy Schubert     aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
4745e0c4386eSCy Schubert     SSL_shutdown(clientssl);
4746e0c4386eSCy Schubert     SSL_shutdown(serverssl);
4747e0c4386eSCy Schubert     SSL_free(serverssl);
4748e0c4386eSCy Schubert     SSL_free(clientssl);
4749e0c4386eSCy Schubert     serverssl = clientssl = NULL;
4750e0c4386eSCy Schubert 
4751e0c4386eSCy Schubert     /* Check we can resume a session with a different SHA-256 ciphersuite */
4752e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4753e0c4386eSCy Schubert                                             "TLS_AES_128_CCM_SHA256"))
4754e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4755e0c4386eSCy Schubert                                              &clientssl, NULL, NULL))
4756e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, clntsess))
4757e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
4758e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
4759e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
4760e0c4386eSCy Schubert         goto end;
4761e0c4386eSCy Schubert 
4762e0c4386eSCy Schubert     SSL_SESSION_free(clntsess);
4763e0c4386eSCy Schubert     clntsess = SSL_get1_session(clientssl);
4764e0c4386eSCy Schubert     SSL_shutdown(clientssl);
4765e0c4386eSCy Schubert     SSL_shutdown(serverssl);
4766e0c4386eSCy Schubert     SSL_free(serverssl);
4767e0c4386eSCy Schubert     SSL_free(clientssl);
4768e0c4386eSCy Schubert     serverssl = clientssl = NULL;
4769e0c4386eSCy Schubert 
4770e0c4386eSCy Schubert     /*
4771e0c4386eSCy Schubert      * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
4772e0c4386eSCy Schubert      * succeeds but does not resume.
4773e0c4386eSCy Schubert      */
4774e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4775e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4776e0c4386eSCy Schubert                                              NULL, NULL))
4777e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, clntsess))
4778e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
4779e0c4386eSCy Schubert                                                 SSL_ERROR_SSL))
4780e0c4386eSCy Schubert             || !TEST_false(SSL_session_reused(clientssl)))
4781e0c4386eSCy Schubert         goto end;
4782e0c4386eSCy Schubert 
4783e0c4386eSCy Schubert     SSL_SESSION_free(clntsess);
4784e0c4386eSCy Schubert     clntsess = NULL;
4785e0c4386eSCy Schubert     SSL_shutdown(clientssl);
4786e0c4386eSCy Schubert     SSL_shutdown(serverssl);
4787e0c4386eSCy Schubert     SSL_free(serverssl);
4788e0c4386eSCy Schubert     SSL_free(clientssl);
4789e0c4386eSCy Schubert     serverssl = clientssl = NULL;
4790e0c4386eSCy Schubert 
4791e0c4386eSCy Schubert     /* Create a session based on SHA384 */
4792e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4793e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4794e0c4386eSCy Schubert                                           &clientssl, NULL, NULL))
4795e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
4796e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
4797e0c4386eSCy Schubert         goto end;
4798e0c4386eSCy Schubert 
4799e0c4386eSCy Schubert     clntsess = SSL_get1_session(clientssl);
4800e0c4386eSCy Schubert     SSL_shutdown(clientssl);
4801e0c4386eSCy Schubert     SSL_shutdown(serverssl);
4802e0c4386eSCy Schubert     SSL_free(serverssl);
4803e0c4386eSCy Schubert     SSL_free(clientssl);
4804e0c4386eSCy Schubert     serverssl = clientssl = NULL;
4805e0c4386eSCy Schubert 
4806e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4807e0c4386eSCy Schubert                    "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
4808e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4809e0c4386eSCy Schubert                                                    "TLS_AES_256_GCM_SHA384"))
4810e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4811e0c4386eSCy Schubert                                              NULL, NULL))
4812e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, clntsess))
4813e0c4386eSCy Schubert                /*
4814e0c4386eSCy Schubert                 * We use SSL_ERROR_WANT_READ below so that we can pause the
4815e0c4386eSCy Schubert                 * connection after the initial ClientHello has been sent to
4816e0c4386eSCy Schubert                 * enable us to make some session changes.
4817e0c4386eSCy Schubert                 */
4818e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
4819e0c4386eSCy Schubert                                                 SSL_ERROR_WANT_READ)))
4820e0c4386eSCy Schubert         goto end;
4821e0c4386eSCy Schubert 
4822e0c4386eSCy Schubert     /* Trick the client into thinking this session is for a different digest */
4823e0c4386eSCy Schubert     clntsess->cipher = aes_128_gcm_sha256;
4824e0c4386eSCy Schubert     clntsess->cipher_id = clntsess->cipher->id;
4825e0c4386eSCy Schubert 
4826e0c4386eSCy Schubert     /*
4827e0c4386eSCy Schubert      * Continue the previously started connection. Server has selected a SHA-384
4828e0c4386eSCy Schubert      * ciphersuite, but client thinks the session is for SHA-256, so it should
4829e0c4386eSCy Schubert      * bail out.
4830e0c4386eSCy Schubert      */
4831e0c4386eSCy Schubert     if (!TEST_false(create_ssl_connection(serverssl, clientssl,
4832e0c4386eSCy Schubert                                                 SSL_ERROR_SSL))
4833e0c4386eSCy Schubert             || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
4834e0c4386eSCy Schubert                             SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
4835e0c4386eSCy Schubert         goto end;
4836e0c4386eSCy Schubert 
4837e0c4386eSCy Schubert     testresult = 1;
4838e0c4386eSCy Schubert 
4839e0c4386eSCy Schubert  end:
4840e0c4386eSCy Schubert     SSL_SESSION_free(clntsess);
4841e0c4386eSCy Schubert     SSL_free(serverssl);
4842e0c4386eSCy Schubert     SSL_free(clientssl);
4843e0c4386eSCy Schubert     SSL_CTX_free(sctx);
4844e0c4386eSCy Schubert     SSL_CTX_free(cctx);
4845e0c4386eSCy Schubert 
4846e0c4386eSCy Schubert     return testresult;
4847e0c4386eSCy Schubert }
4848e0c4386eSCy Schubert 
4849e0c4386eSCy Schubert /*
4850e0c4386eSCy Schubert  * Test TLSv1.3 Key exchange
4851e0c4386eSCy Schubert  * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
4852e0c4386eSCy Schubert  * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
4853e0c4386eSCy Schubert  * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
4854e0c4386eSCy Schubert  * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
4855e0c4386eSCy Schubert  * Test 4 = Test NID_X25519 with TLSv1.3 client and server
4856e0c4386eSCy Schubert  * Test 5 = Test NID_X448 with TLSv1.3 client and server
4857e0c4386eSCy Schubert  * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
4858e0c4386eSCy Schubert  * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
4859e0c4386eSCy Schubert  * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
4860e0c4386eSCy Schubert  * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
4861e0c4386eSCy Schubert  * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
4862e0c4386eSCy Schubert  * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
4863e0c4386eSCy Schubert  * Test 12 = Test all ECDHE with TLSv1.2 client and server
4864e0c4386eSCy Schubert  * Test 13 = Test all FFDHE with TLSv1.2 client and server
4865e0c4386eSCy Schubert  */
4866e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
4867e0c4386eSCy Schubert static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
4868e0c4386eSCy Schubert                                    NID_secp521r1, NID_X25519, NID_X448};
4869e0c4386eSCy Schubert # endif
4870e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
4871e0c4386eSCy Schubert static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
4872e0c4386eSCy Schubert                                    NID_ffdhe6144, NID_ffdhe8192};
4873e0c4386eSCy Schubert # endif
test_key_exchange(int idx)4874e0c4386eSCy Schubert static int test_key_exchange(int idx)
4875e0c4386eSCy Schubert {
4876e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
4877e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
4878e0c4386eSCy Schubert     int testresult = 0;
4879e0c4386eSCy Schubert     int kexch_alg;
4880e0c4386eSCy Schubert     int *kexch_groups = &kexch_alg;
4881e0c4386eSCy Schubert     int kexch_groups_size = 1;
4882e0c4386eSCy Schubert     int max_version = TLS1_3_VERSION;
4883e0c4386eSCy Schubert     char *kexch_name0 = NULL;
4884e0c4386eSCy Schubert 
4885e0c4386eSCy Schubert     switch (idx) {
4886e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
4887e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
4888e0c4386eSCy Schubert         case 12:
4889e0c4386eSCy Schubert             max_version = TLS1_2_VERSION;
4890e0c4386eSCy Schubert # endif
4891e0c4386eSCy Schubert             /* Fall through */
4892e0c4386eSCy Schubert         case 0:
4893e0c4386eSCy Schubert             kexch_groups = ecdhe_kexch_groups;
4894e0c4386eSCy Schubert             kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
4895e0c4386eSCy Schubert             kexch_name0 = "secp256r1";
4896e0c4386eSCy Schubert             break;
4897e0c4386eSCy Schubert         case 1:
4898e0c4386eSCy Schubert             kexch_alg = NID_X9_62_prime256v1;
4899e0c4386eSCy Schubert             kexch_name0 = "secp256r1";
4900e0c4386eSCy Schubert             break;
4901e0c4386eSCy Schubert         case 2:
4902e0c4386eSCy Schubert             kexch_alg = NID_secp384r1;
4903e0c4386eSCy Schubert             kexch_name0 = "secp384r1";
4904e0c4386eSCy Schubert             break;
4905e0c4386eSCy Schubert         case 3:
4906e0c4386eSCy Schubert             kexch_alg = NID_secp521r1;
4907e0c4386eSCy Schubert             kexch_name0 = "secp521r1";
4908e0c4386eSCy Schubert             break;
4909e0c4386eSCy Schubert         case 4:
491044096ebdSEnji Cooper             if (is_fips)
491144096ebdSEnji Cooper                 return TEST_skip("X25519 might not be supported by fips provider.");
4912e0c4386eSCy Schubert             kexch_alg = NID_X25519;
4913e0c4386eSCy Schubert             kexch_name0 = "x25519";
4914e0c4386eSCy Schubert             break;
4915e0c4386eSCy Schubert         case 5:
491644096ebdSEnji Cooper             if (is_fips)
491744096ebdSEnji Cooper                 return TEST_skip("X448 might not be supported by fips provider.");
4918e0c4386eSCy Schubert             kexch_alg = NID_X448;
4919e0c4386eSCy Schubert             kexch_name0 = "x448";
4920e0c4386eSCy Schubert             break;
4921e0c4386eSCy Schubert # endif
4922e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
4923e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
4924e0c4386eSCy Schubert         case 13:
4925e0c4386eSCy Schubert             max_version = TLS1_2_VERSION;
4926e0c4386eSCy Schubert             kexch_name0 = "ffdhe2048";
4927e0c4386eSCy Schubert # endif
4928e0c4386eSCy Schubert             /* Fall through */
4929e0c4386eSCy Schubert         case 6:
4930e0c4386eSCy Schubert             kexch_groups = ffdhe_kexch_groups;
4931e0c4386eSCy Schubert             kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
4932e0c4386eSCy Schubert             kexch_name0 = "ffdhe2048";
4933e0c4386eSCy Schubert             break;
4934e0c4386eSCy Schubert         case 7:
4935e0c4386eSCy Schubert             kexch_alg = NID_ffdhe2048;
4936e0c4386eSCy Schubert             kexch_name0 = "ffdhe2048";
4937e0c4386eSCy Schubert             break;
4938e0c4386eSCy Schubert         case 8:
4939e0c4386eSCy Schubert             kexch_alg = NID_ffdhe3072;
4940e0c4386eSCy Schubert             kexch_name0 = "ffdhe3072";
4941e0c4386eSCy Schubert             break;
4942e0c4386eSCy Schubert         case 9:
4943e0c4386eSCy Schubert             kexch_alg = NID_ffdhe4096;
4944e0c4386eSCy Schubert             kexch_name0 = "ffdhe4096";
4945e0c4386eSCy Schubert             break;
4946e0c4386eSCy Schubert         case 10:
4947e0c4386eSCy Schubert             kexch_alg = NID_ffdhe6144;
4948e0c4386eSCy Schubert             kexch_name0 = "ffdhe6144";
4949e0c4386eSCy Schubert             break;
4950e0c4386eSCy Schubert         case 11:
4951e0c4386eSCy Schubert             kexch_alg = NID_ffdhe8192;
4952e0c4386eSCy Schubert             kexch_name0 = "ffdhe8192";
4953e0c4386eSCy Schubert             break;
4954e0c4386eSCy Schubert # endif
4955e0c4386eSCy Schubert         default:
4956e0c4386eSCy Schubert             /* We're skipping this test */
4957e0c4386eSCy Schubert             return 1;
4958e0c4386eSCy Schubert     }
4959e0c4386eSCy Schubert 
4960e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4961e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION,
4962e0c4386eSCy Schubert                                        max_version, &sctx, &cctx, cert,
4963e0c4386eSCy Schubert                                        privkey)))
4964e0c4386eSCy Schubert         goto end;
4965e0c4386eSCy Schubert 
4966e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
4967e0c4386eSCy Schubert                    TLS1_3_RFC_AES_128_GCM_SHA256)))
4968e0c4386eSCy Schubert         goto end;
4969e0c4386eSCy Schubert 
4970e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4971e0c4386eSCy Schubert                    TLS1_3_RFC_AES_128_GCM_SHA256)))
4972e0c4386eSCy Schubert         goto end;
4973e0c4386eSCy Schubert 
4974e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
4975e0c4386eSCy Schubert                    TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4976e0c4386eSCy Schubert                    TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
4977e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
4978e0c4386eSCy Schubert         goto end;
4979e0c4386eSCy Schubert 
4980e0c4386eSCy Schubert     /*
4981e0c4386eSCy Schubert      * Must include an EC ciphersuite so that we send supported groups in
4982e0c4386eSCy Schubert      * TLSv1.2
4983e0c4386eSCy Schubert      */
4984e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
4985e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4986e0c4386eSCy Schubert                    TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4987e0c4386eSCy Schubert                    TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
4988e0c4386eSCy Schubert         goto end;
4989e0c4386eSCy Schubert # endif
4990e0c4386eSCy Schubert 
4991e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4992e0c4386eSCy Schubert                                              NULL, NULL)))
4993e0c4386eSCy Schubert         goto end;
4994e0c4386eSCy Schubert 
4995e0c4386eSCy Schubert     if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
4996e0c4386eSCy Schubert         || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
4997e0c4386eSCy Schubert         goto end;
4998e0c4386eSCy Schubert 
4999e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
5000e0c4386eSCy Schubert         goto end;
5001e0c4386eSCy Schubert 
5002e0c4386eSCy Schubert     /*
5003e0c4386eSCy Schubert      * If Handshake succeeds the negotiated kexch alg should be the first one in
5004e0c4386eSCy Schubert      * configured, except in the case of FFDHE groups (idx 13), which are
5005e0c4386eSCy Schubert      * TLSv1.3 only so we expect no shared group to exist.
5006e0c4386eSCy Schubert      */
5007e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
5008e0c4386eSCy Schubert                      idx == 13 ? 0 : kexch_groups[0]))
5009e0c4386eSCy Schubert         goto end;
5010e0c4386eSCy Schubert 
5011e0c4386eSCy Schubert     if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
5012e0c4386eSCy Schubert                      kexch_name0))
5013e0c4386eSCy Schubert         goto end;
5014e0c4386eSCy Schubert 
5015e0c4386eSCy Schubert     /* We don't implement RFC 7919 named groups for TLS 1.2. */
5016e0c4386eSCy Schubert     if (idx != 13) {
5017e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
5018e0c4386eSCy Schubert             goto end;
5019e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
5020e0c4386eSCy Schubert             goto end;
5021e0c4386eSCy Schubert     }
5022e0c4386eSCy Schubert 
5023e0c4386eSCy Schubert     testresult = 1;
5024e0c4386eSCy Schubert  end:
5025e0c4386eSCy Schubert     SSL_free(serverssl);
5026e0c4386eSCy Schubert     SSL_free(clientssl);
5027e0c4386eSCy Schubert     SSL_CTX_free(sctx);
5028e0c4386eSCy Schubert     SSL_CTX_free(cctx);
5029e0c4386eSCy Schubert     return testresult;
5030e0c4386eSCy Schubert }
5031e0c4386eSCy Schubert 
5032e0c4386eSCy Schubert # if !defined(OPENSSL_NO_TLS1_2) \
5033e0c4386eSCy Schubert      && !defined(OPENSSL_NO_EC)  \
5034e0c4386eSCy Schubert      && !defined(OPENSSL_NO_DH)
set_ssl_groups(SSL * serverssl,SSL * clientssl,int clientmulti,int isecdhe,int idx)5035e0c4386eSCy Schubert static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
5036e0c4386eSCy Schubert                           int isecdhe, int idx)
5037e0c4386eSCy Schubert {
5038e0c4386eSCy Schubert     int kexch_alg;
5039e0c4386eSCy Schubert     int *kexch_groups = &kexch_alg;
5040e0c4386eSCy Schubert     int numec, numff;
5041e0c4386eSCy Schubert 
5042e0c4386eSCy Schubert     numec = OSSL_NELEM(ecdhe_kexch_groups);
5043e0c4386eSCy Schubert     numff = OSSL_NELEM(ffdhe_kexch_groups);
5044e0c4386eSCy Schubert     if (isecdhe)
5045e0c4386eSCy Schubert         kexch_alg = ecdhe_kexch_groups[idx];
5046e0c4386eSCy Schubert     else
5047e0c4386eSCy Schubert         kexch_alg = ffdhe_kexch_groups[idx];
5048e0c4386eSCy Schubert 
5049e0c4386eSCy Schubert     if (clientmulti) {
5050e0c4386eSCy Schubert         if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
5051e0c4386eSCy Schubert             return 0;
5052e0c4386eSCy Schubert         if (isecdhe) {
5053e0c4386eSCy Schubert             if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
5054e0c4386eSCy Schubert                                            numec)))
5055e0c4386eSCy Schubert                 return 0;
5056e0c4386eSCy Schubert         } else {
5057e0c4386eSCy Schubert             if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
5058e0c4386eSCy Schubert                                            numff)))
5059e0c4386eSCy Schubert                 return 0;
5060e0c4386eSCy Schubert         }
5061e0c4386eSCy Schubert     } else {
5062e0c4386eSCy Schubert         if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
5063e0c4386eSCy Schubert             return 0;
5064e0c4386eSCy Schubert         if (isecdhe) {
5065e0c4386eSCy Schubert             if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
5066e0c4386eSCy Schubert                                            numec)))
5067e0c4386eSCy Schubert                 return 0;
5068e0c4386eSCy Schubert         } else {
5069e0c4386eSCy Schubert             if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
5070e0c4386eSCy Schubert                                            numff)))
5071e0c4386eSCy Schubert                 return 0;
5072e0c4386eSCy Schubert         }
5073e0c4386eSCy Schubert     }
5074e0c4386eSCy Schubert     return 1;
5075e0c4386eSCy Schubert }
5076e0c4386eSCy Schubert 
5077e0c4386eSCy Schubert /*-
5078e0c4386eSCy Schubert  * Test the SSL_get_negotiated_group() API across a battery of scenarios.
5079e0c4386eSCy Schubert  * Run through both the ECDHE and FFDHE group lists used in the previous
5080e0c4386eSCy Schubert  * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
5081e0c4386eSCy Schubert  * confirming the expected result; then perform a resumption handshake
5082e0c4386eSCy Schubert  * while offering the same group list, and another resumption handshake
5083e0c4386eSCy Schubert  * offering a different group list.  The returned value should be the
5084e0c4386eSCy Schubert  * negotiated group for the initial handshake; for TLS 1.3 resumption
5085e0c4386eSCy Schubert  * handshakes the returned value will be negotiated on the resumption
5086e0c4386eSCy Schubert  * handshake itself, but for TLS 1.2 resumption handshakes the value will
5087e0c4386eSCy Schubert  * be cached in the session from the original handshake, regardless of what
5088e0c4386eSCy Schubert  * was offered in the resumption ClientHello.
5089e0c4386eSCy Schubert  *
5090e0c4386eSCy Schubert  * Using E for the number of EC groups and F for the number of FF groups:
5091e0c4386eSCy Schubert  * E tests of ECDHE with TLS 1.3, server only has one group
5092e0c4386eSCy Schubert  * F tests of FFDHE with TLS 1.3, server only has one group
5093e0c4386eSCy Schubert  * E tests of ECDHE with TLS 1.2, server only has one group
5094e0c4386eSCy Schubert  * F tests of FFDHE with TLS 1.2, server only has one group
5095e0c4386eSCy Schubert  * E tests of ECDHE with TLS 1.3, client sends only one group
5096e0c4386eSCy Schubert  * F tests of FFDHE with TLS 1.3, client sends only one group
5097e0c4386eSCy Schubert  * E tests of ECDHE with TLS 1.2, client sends only one group
5098e0c4386eSCy Schubert  * F tests of FFDHE with TLS 1.2, client sends only one group
5099e0c4386eSCy Schubert  */
test_negotiated_group(int idx)5100e0c4386eSCy Schubert static int test_negotiated_group(int idx)
5101e0c4386eSCy Schubert {
5102e0c4386eSCy Schubert     int clientmulti, istls13, isecdhe, numec, numff, numgroups;
5103e0c4386eSCy Schubert     int expectednid;
5104e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
5105e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
5106e0c4386eSCy Schubert     SSL_SESSION *origsess = NULL;
5107e0c4386eSCy Schubert     int testresult = 0;
5108e0c4386eSCy Schubert     int kexch_alg;
5109e0c4386eSCy Schubert     int max_version = TLS1_3_VERSION;
5110e0c4386eSCy Schubert 
5111e0c4386eSCy Schubert     numec = OSSL_NELEM(ecdhe_kexch_groups);
5112e0c4386eSCy Schubert     numff = OSSL_NELEM(ffdhe_kexch_groups);
5113e0c4386eSCy Schubert     numgroups = numec + numff;
5114e0c4386eSCy Schubert     clientmulti = (idx < 2 * numgroups);
5115e0c4386eSCy Schubert     idx = idx % (2 * numgroups);
5116e0c4386eSCy Schubert     istls13 = (idx < numgroups);
5117e0c4386eSCy Schubert     idx = idx % numgroups;
5118e0c4386eSCy Schubert     isecdhe = (idx < numec);
5119e0c4386eSCy Schubert     if (!isecdhe)
5120e0c4386eSCy Schubert         idx -= numec;
5121e0c4386eSCy Schubert     /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
5122e0c4386eSCy Schubert     if (isecdhe)
5123e0c4386eSCy Schubert         kexch_alg = ecdhe_kexch_groups[idx];
5124e0c4386eSCy Schubert     else
5125e0c4386eSCy Schubert         kexch_alg = ffdhe_kexch_groups[idx];
5126e0c4386eSCy Schubert     /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
5127e0c4386eSCy Schubert     if (!istls13 && !isecdhe)
5128e0c4386eSCy Schubert         expectednid = NID_undef;
5129e0c4386eSCy Schubert     else
5130e0c4386eSCy Schubert         expectednid = kexch_alg;
5131e0c4386eSCy Schubert 
513244096ebdSEnji Cooper     if (is_fips && (kexch_alg == NID_X25519 || kexch_alg == NID_X448))
513344096ebdSEnji Cooper         return TEST_skip("X25519 and X448 might not be available in fips provider.");
513444096ebdSEnji Cooper 
5135e0c4386eSCy Schubert     if (!istls13)
5136e0c4386eSCy Schubert         max_version = TLS1_2_VERSION;
5137e0c4386eSCy Schubert 
5138e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5139e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION,
5140e0c4386eSCy Schubert                                        max_version, &sctx, &cctx, cert,
5141e0c4386eSCy Schubert                                        privkey)))
5142e0c4386eSCy Schubert         goto end;
5143e0c4386eSCy Schubert 
5144e0c4386eSCy Schubert     /*
5145e0c4386eSCy Schubert      * Force (EC)DHE ciphers for TLS 1.2.
5146e0c4386eSCy Schubert      * Be sure to enable auto tmp DH so that FFDHE can succeed.
5147e0c4386eSCy Schubert      */
5148e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
5149e0c4386eSCy Schubert                    TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5150e0c4386eSCy Schubert                    TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
5151e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
5152e0c4386eSCy Schubert         goto end;
5153e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
5154e0c4386eSCy Schubert                    TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5155e0c4386eSCy Schubert                    TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
5156e0c4386eSCy Schubert         goto end;
5157e0c4386eSCy Schubert 
5158e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5159e0c4386eSCy Schubert                                              NULL, NULL)))
5160e0c4386eSCy Schubert         goto end;
5161e0c4386eSCy Schubert 
5162e0c4386eSCy Schubert     if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
5163e0c4386eSCy Schubert                                   idx)))
5164e0c4386eSCy Schubert         goto end;
5165e0c4386eSCy Schubert 
5166e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
5167e0c4386eSCy Schubert         goto end;
5168e0c4386eSCy Schubert 
5169e0c4386eSCy Schubert     /* Initial handshake; always the configured one */
5170e0c4386eSCy Schubert     if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5171e0c4386eSCy Schubert             || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5172e0c4386eSCy Schubert         goto end;
5173e0c4386eSCy Schubert 
5174e0c4386eSCy Schubert     if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
5175e0c4386eSCy Schubert         goto end;
5176e0c4386eSCy Schubert 
5177e0c4386eSCy Schubert     SSL_shutdown(clientssl);
5178e0c4386eSCy Schubert     SSL_shutdown(serverssl);
5179e0c4386eSCy Schubert     SSL_free(serverssl);
5180e0c4386eSCy Schubert     SSL_free(clientssl);
5181e0c4386eSCy Schubert     serverssl = clientssl = NULL;
5182e0c4386eSCy Schubert 
5183e0c4386eSCy Schubert     /* First resumption attempt; use the same config as initial handshake */
5184e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5185e0c4386eSCy Schubert                                              NULL, NULL))
5186e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, origsess))
5187e0c4386eSCy Schubert             || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5188e0c4386eSCy Schubert                                          isecdhe, idx)))
5189e0c4386eSCy Schubert         goto end;
5190e0c4386eSCy Schubert 
5191e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5192e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
5193e0c4386eSCy Schubert         goto end;
5194e0c4386eSCy Schubert 
5195e0c4386eSCy Schubert     /* Still had better agree, since nothing changed... */
5196e0c4386eSCy Schubert     if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5197e0c4386eSCy Schubert             || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5198e0c4386eSCy Schubert         goto end;
5199e0c4386eSCy Schubert 
5200e0c4386eSCy Schubert     SSL_shutdown(clientssl);
5201e0c4386eSCy Schubert     SSL_shutdown(serverssl);
5202e0c4386eSCy Schubert     SSL_free(serverssl);
5203e0c4386eSCy Schubert     SSL_free(clientssl);
5204e0c4386eSCy Schubert     serverssl = clientssl = NULL;
5205e0c4386eSCy Schubert 
5206e0c4386eSCy Schubert     /*-
5207e0c4386eSCy Schubert      * Second resumption attempt
5208e0c4386eSCy Schubert      * The party that picks one group changes it, which we effectuate by
5209e0c4386eSCy Schubert      * changing 'idx' and updating what we expect.
5210e0c4386eSCy Schubert      */
5211e0c4386eSCy Schubert     if (idx == 0)
5212e0c4386eSCy Schubert         idx = 1;
5213e0c4386eSCy Schubert     else
5214e0c4386eSCy Schubert         idx--;
5215e0c4386eSCy Schubert     if (istls13) {
5216e0c4386eSCy Schubert         if (isecdhe)
5217e0c4386eSCy Schubert             expectednid = ecdhe_kexch_groups[idx];
5218e0c4386eSCy Schubert         else
5219e0c4386eSCy Schubert             expectednid = ffdhe_kexch_groups[idx];
5220e0c4386eSCy Schubert         /* Verify that we are changing what we expect. */
5221e0c4386eSCy Schubert         if (!TEST_int_ne(expectednid, kexch_alg))
5222e0c4386eSCy Schubert             goto end;
5223e0c4386eSCy Schubert     } else {
5224e0c4386eSCy Schubert         /* TLS 1.2 only supports named groups for ECDHE. */
5225e0c4386eSCy Schubert         if (isecdhe)
5226e0c4386eSCy Schubert             expectednid = kexch_alg;
5227e0c4386eSCy Schubert         else
5228e0c4386eSCy Schubert             expectednid = 0;
5229e0c4386eSCy Schubert     }
5230e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5231e0c4386eSCy Schubert                                              NULL, NULL))
5232e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, origsess))
5233e0c4386eSCy Schubert             || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5234e0c4386eSCy Schubert                                          isecdhe, idx)))
5235e0c4386eSCy Schubert         goto end;
5236e0c4386eSCy Schubert 
5237e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5238e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
5239e0c4386eSCy Schubert         goto end;
5240e0c4386eSCy Schubert 
5241e0c4386eSCy Schubert     /* Check that we get what we expected */
5242e0c4386eSCy Schubert     if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5243e0c4386eSCy Schubert             || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5244e0c4386eSCy Schubert         goto end;
5245e0c4386eSCy Schubert 
5246e0c4386eSCy Schubert     testresult = 1;
5247e0c4386eSCy Schubert  end:
5248e0c4386eSCy Schubert     SSL_free(serverssl);
5249e0c4386eSCy Schubert     SSL_free(clientssl);
5250e0c4386eSCy Schubert     SSL_CTX_free(sctx);
5251e0c4386eSCy Schubert     SSL_CTX_free(cctx);
5252e0c4386eSCy Schubert     SSL_SESSION_free(origsess);
5253e0c4386eSCy Schubert     return testresult;
5254e0c4386eSCy Schubert }
5255e0c4386eSCy Schubert # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
5256e0c4386eSCy Schubert 
5257e0c4386eSCy Schubert /*
5258e0c4386eSCy Schubert  * Test TLSv1.3 Cipher Suite
5259e0c4386eSCy Schubert  * Test 0 = Set TLS1.3 cipher on context
5260e0c4386eSCy Schubert  * Test 1 = Set TLS1.3 cipher on SSL
5261e0c4386eSCy Schubert  * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
5262e0c4386eSCy Schubert  * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
5263e0c4386eSCy Schubert  */
test_tls13_ciphersuite(int idx)5264e0c4386eSCy Schubert static int test_tls13_ciphersuite(int idx)
5265e0c4386eSCy Schubert {
5266e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
5267e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
5268e0c4386eSCy Schubert     static const struct {
5269e0c4386eSCy Schubert         const char *ciphername;
5270e0c4386eSCy Schubert         int fipscapable;
5271e0c4386eSCy Schubert     } t13_ciphers[] = {
5272e0c4386eSCy Schubert         { TLS1_3_RFC_AES_128_GCM_SHA256, 1 },
5273e0c4386eSCy Schubert         { TLS1_3_RFC_AES_256_GCM_SHA384, 1 },
5274e0c4386eSCy Schubert         { TLS1_3_RFC_AES_128_CCM_SHA256, 1 },
5275e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
5276e0c4386eSCy Schubert         { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5277e0c4386eSCy Schubert         { TLS1_3_RFC_AES_256_GCM_SHA384
5278e0c4386eSCy Schubert           ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5279e0c4386eSCy Schubert # endif
5280e0c4386eSCy Schubert         { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1 }
5281e0c4386eSCy Schubert     };
5282e0c4386eSCy Schubert     const char *t13_cipher = NULL;
5283e0c4386eSCy Schubert     const char *t12_cipher = NULL;
5284e0c4386eSCy Schubert     const char *negotiated_scipher;
5285e0c4386eSCy Schubert     const char *negotiated_ccipher;
5286e0c4386eSCy Schubert     int set_at_ctx = 0;
5287e0c4386eSCy Schubert     int set_at_ssl = 0;
5288e0c4386eSCy Schubert     int testresult = 0;
5289e0c4386eSCy Schubert     int max_ver;
5290e0c4386eSCy Schubert     size_t i;
5291e0c4386eSCy Schubert 
5292e0c4386eSCy Schubert     switch (idx) {
5293e0c4386eSCy Schubert         case 0:
5294e0c4386eSCy Schubert             set_at_ctx = 1;
5295e0c4386eSCy Schubert             break;
5296e0c4386eSCy Schubert         case 1:
5297e0c4386eSCy Schubert             set_at_ssl = 1;
5298e0c4386eSCy Schubert             break;
5299e0c4386eSCy Schubert         case 2:
5300e0c4386eSCy Schubert             set_at_ctx = 1;
5301e0c4386eSCy Schubert             t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5302e0c4386eSCy Schubert             break;
5303e0c4386eSCy Schubert         case 3:
5304e0c4386eSCy Schubert             set_at_ssl = 1;
5305e0c4386eSCy Schubert             t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5306e0c4386eSCy Schubert             break;
5307e0c4386eSCy Schubert     }
5308e0c4386eSCy Schubert 
5309e0c4386eSCy Schubert     for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
5310e0c4386eSCy Schubert # ifdef OPENSSL_NO_TLS1_2
5311e0c4386eSCy Schubert         if (max_ver == TLS1_2_VERSION)
5312e0c4386eSCy Schubert             continue;
5313e0c4386eSCy Schubert # endif
5314e0c4386eSCy Schubert         for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
5315e0c4386eSCy Schubert             if (is_fips && !t13_ciphers[i].fipscapable)
5316e0c4386eSCy Schubert                 continue;
5317e0c4386eSCy Schubert             t13_cipher = t13_ciphers[i].ciphername;
5318e0c4386eSCy Schubert             if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5319e0c4386eSCy Schubert                                                TLS_client_method(),
5320e0c4386eSCy Schubert                                                TLS1_VERSION, max_ver,
5321e0c4386eSCy Schubert                                                &sctx, &cctx, cert, privkey)))
5322e0c4386eSCy Schubert                 goto end;
5323e0c4386eSCy Schubert 
5324e0c4386eSCy Schubert             if (set_at_ctx) {
5325e0c4386eSCy Schubert                 if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
5326e0c4386eSCy Schubert                     || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
5327e0c4386eSCy Schubert                     goto end;
5328e0c4386eSCy Schubert                 if (t12_cipher != NULL) {
5329e0c4386eSCy Schubert                     if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
5330e0c4386eSCy Schubert                         || !TEST_true(SSL_CTX_set_cipher_list(cctx,
5331e0c4386eSCy Schubert                                                               t12_cipher)))
5332e0c4386eSCy Schubert                         goto end;
5333e0c4386eSCy Schubert                 }
5334e0c4386eSCy Schubert             }
5335e0c4386eSCy Schubert 
5336e0c4386eSCy Schubert             if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5337e0c4386eSCy Schubert                                               &clientssl, NULL, NULL)))
5338e0c4386eSCy Schubert                 goto end;
5339e0c4386eSCy Schubert 
5340e0c4386eSCy Schubert             if (set_at_ssl) {
5341e0c4386eSCy Schubert                 if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
5342e0c4386eSCy Schubert                     || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
5343e0c4386eSCy Schubert                     goto end;
5344e0c4386eSCy Schubert                 if (t12_cipher != NULL) {
5345e0c4386eSCy Schubert                     if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
5346e0c4386eSCy Schubert                         || !TEST_true(SSL_set_cipher_list(clientssl,
5347e0c4386eSCy Schubert                                                           t12_cipher)))
5348e0c4386eSCy Schubert                         goto end;
5349e0c4386eSCy Schubert                 }
5350e0c4386eSCy Schubert             }
5351e0c4386eSCy Schubert 
5352e0c4386eSCy Schubert             if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5353e0c4386eSCy Schubert                                                  SSL_ERROR_NONE)))
5354e0c4386eSCy Schubert                 goto end;
5355e0c4386eSCy Schubert 
5356e0c4386eSCy Schubert             negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5357e0c4386eSCy Schubert                                                                  serverssl));
5358e0c4386eSCy Schubert             negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5359e0c4386eSCy Schubert                                                                  clientssl));
5360e0c4386eSCy Schubert             if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
5361e0c4386eSCy Schubert                 goto end;
5362e0c4386eSCy Schubert 
5363e0c4386eSCy Schubert             /*
5364e0c4386eSCy Schubert              * TEST_strn_eq is used below because t13_cipher can contain
5365e0c4386eSCy Schubert              * multiple ciphersuites
5366e0c4386eSCy Schubert              */
5367e0c4386eSCy Schubert             if (max_ver == TLS1_3_VERSION
5368e0c4386eSCy Schubert                 && !TEST_strn_eq(t13_cipher, negotiated_scipher,
5369e0c4386eSCy Schubert                                  strlen(negotiated_scipher)))
5370e0c4386eSCy Schubert                 goto end;
5371e0c4386eSCy Schubert 
5372e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
5373e0c4386eSCy Schubert             /* Below validation is not done when t12_cipher is NULL */
5374e0c4386eSCy Schubert             if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
5375e0c4386eSCy Schubert                 && !TEST_str_eq(t12_cipher, negotiated_scipher))
5376e0c4386eSCy Schubert                 goto end;
5377e0c4386eSCy Schubert # endif
5378e0c4386eSCy Schubert 
5379e0c4386eSCy Schubert             SSL_free(serverssl);
5380e0c4386eSCy Schubert             serverssl = NULL;
5381e0c4386eSCy Schubert             SSL_free(clientssl);
5382e0c4386eSCy Schubert             clientssl = NULL;
5383e0c4386eSCy Schubert             SSL_CTX_free(sctx);
5384e0c4386eSCy Schubert             sctx = NULL;
5385e0c4386eSCy Schubert             SSL_CTX_free(cctx);
5386e0c4386eSCy Schubert             cctx = NULL;
5387e0c4386eSCy Schubert         }
5388e0c4386eSCy Schubert     }
5389e0c4386eSCy Schubert 
5390e0c4386eSCy Schubert     testresult = 1;
5391e0c4386eSCy Schubert  end:
5392e0c4386eSCy Schubert     SSL_free(serverssl);
5393e0c4386eSCy Schubert     SSL_free(clientssl);
5394e0c4386eSCy Schubert     SSL_CTX_free(sctx);
5395e0c4386eSCy Schubert     SSL_CTX_free(cctx);
5396e0c4386eSCy Schubert     return testresult;
5397e0c4386eSCy Schubert }
5398e0c4386eSCy Schubert 
5399e0c4386eSCy Schubert /*
5400e0c4386eSCy Schubert  * Test TLSv1.3 PSKs
5401e0c4386eSCy Schubert  * Test 0 = Test new style callbacks
5402e0c4386eSCy Schubert  * Test 1 = Test both new and old style callbacks
5403e0c4386eSCy Schubert  * Test 2 = Test old style callbacks
5404e0c4386eSCy Schubert  * Test 3 = Test old style callbacks with no certificate
5405e0c4386eSCy Schubert  */
test_tls13_psk(int idx)5406e0c4386eSCy Schubert static int test_tls13_psk(int idx)
5407e0c4386eSCy Schubert {
5408e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
5409e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
5410e0c4386eSCy Schubert     const SSL_CIPHER *cipher = NULL;
5411e0c4386eSCy Schubert     const unsigned char key[] = {
5412e0c4386eSCy Schubert         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
5413e0c4386eSCy Schubert         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
5414e0c4386eSCy Schubert         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
5415e0c4386eSCy Schubert         0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
5416e0c4386eSCy Schubert     };
5417e0c4386eSCy Schubert     int testresult = 0;
5418e0c4386eSCy Schubert 
5419e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5420e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
5421e0c4386eSCy Schubert                                        &sctx, &cctx, idx == 3 ? NULL : cert,
5422e0c4386eSCy Schubert                                        idx == 3 ? NULL : privkey)))
5423e0c4386eSCy Schubert         goto end;
5424e0c4386eSCy Schubert 
5425e0c4386eSCy Schubert     if (idx != 3) {
5426e0c4386eSCy Schubert         /*
5427e0c4386eSCy Schubert          * We use a ciphersuite with SHA256 to ease testing old style PSK
5428e0c4386eSCy Schubert          * callbacks which will always default to SHA256. This should not be
5429e0c4386eSCy Schubert          * necessary if we have no cert/priv key. In that case the server should
5430e0c4386eSCy Schubert          * prefer SHA256 automatically.
5431e0c4386eSCy Schubert          */
5432e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5433e0c4386eSCy Schubert                                                 "TLS_AES_128_GCM_SHA256")))
5434e0c4386eSCy Schubert             goto end;
5435e0c4386eSCy Schubert     } else {
5436e0c4386eSCy Schubert         /*
5437e0c4386eSCy Schubert          * As noted above the server should prefer SHA256 automatically. However
5438e0c4386eSCy Schubert          * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
5439e0c4386eSCy Schubert          * code works even if we are testing with only the FIPS provider loaded.
5440e0c4386eSCy Schubert          */
5441e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5442e0c4386eSCy Schubert                                                 "TLS_AES_256_GCM_SHA384:"
5443e0c4386eSCy Schubert                                                 "TLS_AES_128_GCM_SHA256")))
5444e0c4386eSCy Schubert             goto end;
5445e0c4386eSCy Schubert     }
5446e0c4386eSCy Schubert 
5447e0c4386eSCy Schubert     /*
5448e0c4386eSCy Schubert      * Test 0: New style callbacks only
5449e0c4386eSCy Schubert      * Test 1: New and old style callbacks (only the new ones should be used)
5450e0c4386eSCy Schubert      * Test 2: Old style callbacks only
5451e0c4386eSCy Schubert      */
5452e0c4386eSCy Schubert     if (idx == 0 || idx == 1) {
5453e0c4386eSCy Schubert         SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
5454e0c4386eSCy Schubert         SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
5455e0c4386eSCy Schubert     }
5456e0c4386eSCy Schubert #ifndef OPENSSL_NO_PSK
5457e0c4386eSCy Schubert     if (idx >= 1) {
5458e0c4386eSCy Schubert         SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
5459e0c4386eSCy Schubert         SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
5460e0c4386eSCy Schubert     }
5461e0c4386eSCy Schubert #endif
5462e0c4386eSCy Schubert     srvid = pskid;
5463e0c4386eSCy Schubert     use_session_cb_cnt = 0;
5464e0c4386eSCy Schubert     find_session_cb_cnt = 0;
5465e0c4386eSCy Schubert     psk_client_cb_cnt = 0;
5466e0c4386eSCy Schubert     psk_server_cb_cnt = 0;
5467e0c4386eSCy Schubert 
5468e0c4386eSCy Schubert     if (idx != 3) {
5469e0c4386eSCy Schubert         /*
5470e0c4386eSCy Schubert          * Check we can create a connection if callback decides not to send a
5471e0c4386eSCy Schubert          * PSK
5472e0c4386eSCy Schubert          */
5473e0c4386eSCy Schubert         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5474e0c4386eSCy Schubert                                                  NULL, NULL))
5475e0c4386eSCy Schubert                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5476e0c4386eSCy Schubert                                                     SSL_ERROR_NONE))
5477e0c4386eSCy Schubert                 || !TEST_false(SSL_session_reused(clientssl))
5478e0c4386eSCy Schubert                 || !TEST_false(SSL_session_reused(serverssl)))
5479e0c4386eSCy Schubert             goto end;
5480e0c4386eSCy Schubert 
5481e0c4386eSCy Schubert         if (idx == 0 || idx == 1) {
5482e0c4386eSCy Schubert             if (!TEST_true(use_session_cb_cnt == 1)
5483e0c4386eSCy Schubert                     || !TEST_true(find_session_cb_cnt == 0)
5484e0c4386eSCy Schubert                        /*
5485e0c4386eSCy Schubert                         * If no old style callback then below should be 0
5486e0c4386eSCy Schubert                         * otherwise 1
5487e0c4386eSCy Schubert                         */
5488e0c4386eSCy Schubert                     || !TEST_true(psk_client_cb_cnt == idx)
5489e0c4386eSCy Schubert                     || !TEST_true(psk_server_cb_cnt == 0))
5490e0c4386eSCy Schubert                 goto end;
5491e0c4386eSCy Schubert         } else {
5492e0c4386eSCy Schubert             if (!TEST_true(use_session_cb_cnt == 0)
5493e0c4386eSCy Schubert                     || !TEST_true(find_session_cb_cnt == 0)
5494e0c4386eSCy Schubert                     || !TEST_true(psk_client_cb_cnt == 1)
5495e0c4386eSCy Schubert                     || !TEST_true(psk_server_cb_cnt == 0))
5496e0c4386eSCy Schubert                 goto end;
5497e0c4386eSCy Schubert         }
5498e0c4386eSCy Schubert 
5499e0c4386eSCy Schubert         shutdown_ssl_connection(serverssl, clientssl);
5500e0c4386eSCy Schubert         serverssl = clientssl = NULL;
5501e0c4386eSCy Schubert         use_session_cb_cnt = psk_client_cb_cnt = 0;
5502e0c4386eSCy Schubert     }
5503e0c4386eSCy Schubert 
5504e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5505e0c4386eSCy Schubert                                              NULL, NULL)))
5506e0c4386eSCy Schubert         goto end;
5507e0c4386eSCy Schubert 
5508e0c4386eSCy Schubert     /* Create the PSK */
5509e0c4386eSCy Schubert     cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
5510e0c4386eSCy Schubert     clientpsk = SSL_SESSION_new();
5511e0c4386eSCy Schubert     if (!TEST_ptr(clientpsk)
5512e0c4386eSCy Schubert             || !TEST_ptr(cipher)
5513e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
5514e0c4386eSCy Schubert                                                       sizeof(key)))
5515e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
5516e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
5517e0c4386eSCy Schubert                                                            TLS1_3_VERSION))
5518e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
5519e0c4386eSCy Schubert         goto end;
5520e0c4386eSCy Schubert     serverpsk = clientpsk;
5521e0c4386eSCy Schubert 
5522e0c4386eSCy Schubert     /* Check we can create a connection and the PSK is used */
5523e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5524e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl))
5525e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(serverssl)))
5526e0c4386eSCy Schubert         goto end;
5527e0c4386eSCy Schubert 
5528e0c4386eSCy Schubert     if (idx == 0 || idx == 1) {
5529e0c4386eSCy Schubert         if (!TEST_true(use_session_cb_cnt == 1)
5530e0c4386eSCy Schubert                 || !TEST_true(find_session_cb_cnt == 1)
5531e0c4386eSCy Schubert                 || !TEST_true(psk_client_cb_cnt == 0)
5532e0c4386eSCy Schubert                 || !TEST_true(psk_server_cb_cnt == 0))
5533e0c4386eSCy Schubert             goto end;
5534e0c4386eSCy Schubert     } else {
5535e0c4386eSCy Schubert         if (!TEST_true(use_session_cb_cnt == 0)
5536e0c4386eSCy Schubert                 || !TEST_true(find_session_cb_cnt == 0)
5537e0c4386eSCy Schubert                 || !TEST_true(psk_client_cb_cnt == 1)
5538e0c4386eSCy Schubert                 || !TEST_true(psk_server_cb_cnt == 1))
5539e0c4386eSCy Schubert             goto end;
5540e0c4386eSCy Schubert     }
5541e0c4386eSCy Schubert 
5542e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
5543e0c4386eSCy Schubert     serverssl = clientssl = NULL;
5544e0c4386eSCy Schubert     use_session_cb_cnt = find_session_cb_cnt = 0;
5545e0c4386eSCy Schubert     psk_client_cb_cnt = psk_server_cb_cnt = 0;
5546e0c4386eSCy Schubert 
5547e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5548e0c4386eSCy Schubert                                              NULL, NULL)))
5549e0c4386eSCy Schubert         goto end;
5550e0c4386eSCy Schubert 
5551e0c4386eSCy Schubert     /* Force an HRR */
5552e0c4386eSCy Schubert #if defined(OPENSSL_NO_EC)
5553e0c4386eSCy Schubert     if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
5554e0c4386eSCy Schubert         goto end;
5555e0c4386eSCy Schubert #else
5556a7148ab3SEnji Cooper     if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
5557e0c4386eSCy Schubert         goto end;
5558e0c4386eSCy Schubert #endif
5559e0c4386eSCy Schubert 
5560e0c4386eSCy Schubert     /*
5561e0c4386eSCy Schubert      * Check we can create a connection, the PSK is used and the callbacks are
5562e0c4386eSCy Schubert      * called twice.
5563e0c4386eSCy Schubert      */
5564e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5565e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl))
5566e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(serverssl)))
5567e0c4386eSCy Schubert         goto end;
5568e0c4386eSCy Schubert 
5569e0c4386eSCy Schubert     if (idx == 0 || idx == 1) {
5570e0c4386eSCy Schubert         if (!TEST_true(use_session_cb_cnt == 2)
5571e0c4386eSCy Schubert                 || !TEST_true(find_session_cb_cnt == 2)
5572e0c4386eSCy Schubert                 || !TEST_true(psk_client_cb_cnt == 0)
5573e0c4386eSCy Schubert                 || !TEST_true(psk_server_cb_cnt == 0))
5574e0c4386eSCy Schubert             goto end;
5575e0c4386eSCy Schubert     } else {
5576e0c4386eSCy Schubert         if (!TEST_true(use_session_cb_cnt == 0)
5577e0c4386eSCy Schubert                 || !TEST_true(find_session_cb_cnt == 0)
5578e0c4386eSCy Schubert                 || !TEST_true(psk_client_cb_cnt == 2)
5579e0c4386eSCy Schubert                 || !TEST_true(psk_server_cb_cnt == 2))
5580e0c4386eSCy Schubert             goto end;
5581e0c4386eSCy Schubert     }
5582e0c4386eSCy Schubert 
5583e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
5584e0c4386eSCy Schubert     serverssl = clientssl = NULL;
5585e0c4386eSCy Schubert     use_session_cb_cnt = find_session_cb_cnt = 0;
5586e0c4386eSCy Schubert     psk_client_cb_cnt = psk_server_cb_cnt = 0;
5587e0c4386eSCy Schubert 
5588e0c4386eSCy Schubert     if (idx != 3) {
5589e0c4386eSCy Schubert         /*
5590e0c4386eSCy Schubert          * Check that if the server rejects the PSK we can still connect, but with
5591e0c4386eSCy Schubert          * a full handshake
5592e0c4386eSCy Schubert          */
5593e0c4386eSCy Schubert         srvid = "Dummy Identity";
5594e0c4386eSCy Schubert         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5595e0c4386eSCy Schubert                                                  NULL, NULL))
5596e0c4386eSCy Schubert                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
5597e0c4386eSCy Schubert                                                     SSL_ERROR_NONE))
5598e0c4386eSCy Schubert                 || !TEST_false(SSL_session_reused(clientssl))
5599e0c4386eSCy Schubert                 || !TEST_false(SSL_session_reused(serverssl)))
5600e0c4386eSCy Schubert             goto end;
5601e0c4386eSCy Schubert 
5602e0c4386eSCy Schubert         if (idx == 0 || idx == 1) {
5603e0c4386eSCy Schubert             if (!TEST_true(use_session_cb_cnt == 1)
5604e0c4386eSCy Schubert                     || !TEST_true(find_session_cb_cnt == 1)
5605e0c4386eSCy Schubert                     || !TEST_true(psk_client_cb_cnt == 0)
5606e0c4386eSCy Schubert                        /*
5607e0c4386eSCy Schubert                         * If no old style callback then below should be 0
5608e0c4386eSCy Schubert                         * otherwise 1
5609e0c4386eSCy Schubert                         */
5610e0c4386eSCy Schubert                     || !TEST_true(psk_server_cb_cnt == idx))
5611e0c4386eSCy Schubert                 goto end;
5612e0c4386eSCy Schubert         } else {
5613e0c4386eSCy Schubert             if (!TEST_true(use_session_cb_cnt == 0)
5614e0c4386eSCy Schubert                     || !TEST_true(find_session_cb_cnt == 0)
5615e0c4386eSCy Schubert                     || !TEST_true(psk_client_cb_cnt == 1)
5616e0c4386eSCy Schubert                     || !TEST_true(psk_server_cb_cnt == 1))
5617e0c4386eSCy Schubert                 goto end;
5618e0c4386eSCy Schubert         }
5619e0c4386eSCy Schubert 
5620e0c4386eSCy Schubert         shutdown_ssl_connection(serverssl, clientssl);
5621e0c4386eSCy Schubert         serverssl = clientssl = NULL;
5622e0c4386eSCy Schubert     }
5623e0c4386eSCy Schubert     testresult = 1;
5624e0c4386eSCy Schubert 
5625e0c4386eSCy Schubert  end:
5626e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
5627e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
5628e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
5629e0c4386eSCy Schubert     SSL_free(serverssl);
5630e0c4386eSCy Schubert     SSL_free(clientssl);
5631e0c4386eSCy Schubert     SSL_CTX_free(sctx);
5632e0c4386eSCy Schubert     SSL_CTX_free(cctx);
5633e0c4386eSCy Schubert     return testresult;
5634e0c4386eSCy Schubert }
5635e0c4386eSCy Schubert 
5636e0c4386eSCy Schubert static unsigned char cookie_magic_value[] = "cookie magic";
5637e0c4386eSCy Schubert 
generate_cookie_callback(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len)5638e0c4386eSCy Schubert static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
5639e0c4386eSCy Schubert                                     unsigned int *cookie_len)
5640e0c4386eSCy Schubert {
5641e0c4386eSCy Schubert     /*
5642e0c4386eSCy Schubert      * Not suitable as a real cookie generation function but good enough for
5643e0c4386eSCy Schubert      * testing!
5644e0c4386eSCy Schubert      */
5645e0c4386eSCy Schubert     memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
5646e0c4386eSCy Schubert     *cookie_len = sizeof(cookie_magic_value) - 1;
5647e0c4386eSCy Schubert 
5648e0c4386eSCy Schubert     return 1;
5649e0c4386eSCy Schubert }
5650e0c4386eSCy Schubert 
verify_cookie_callback(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len)5651e0c4386eSCy Schubert static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
5652e0c4386eSCy Schubert                                   unsigned int cookie_len)
5653e0c4386eSCy Schubert {
5654e0c4386eSCy Schubert     if (cookie_len == sizeof(cookie_magic_value) - 1
5655e0c4386eSCy Schubert         && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
5656e0c4386eSCy Schubert         return 1;
5657e0c4386eSCy Schubert 
5658e0c4386eSCy Schubert     return 0;
5659e0c4386eSCy Schubert }
5660e0c4386eSCy Schubert 
generate_stateless_cookie_callback(SSL * ssl,unsigned char * cookie,size_t * cookie_len)5661e0c4386eSCy Schubert static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
5662e0c4386eSCy Schubert                                         size_t *cookie_len)
5663e0c4386eSCy Schubert {
5664e0c4386eSCy Schubert     unsigned int temp;
5665e0c4386eSCy Schubert     int res = generate_cookie_callback(ssl, cookie, &temp);
5666e0c4386eSCy Schubert     *cookie_len = temp;
5667e0c4386eSCy Schubert     return res;
5668e0c4386eSCy Schubert }
5669e0c4386eSCy Schubert 
verify_stateless_cookie_callback(SSL * ssl,const unsigned char * cookie,size_t cookie_len)5670e0c4386eSCy Schubert static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
5671e0c4386eSCy Schubert                                       size_t cookie_len)
5672e0c4386eSCy Schubert {
5673e0c4386eSCy Schubert     return verify_cookie_callback(ssl, cookie, cookie_len);
5674e0c4386eSCy Schubert }
5675e0c4386eSCy Schubert 
test_stateless(void)5676e0c4386eSCy Schubert static int test_stateless(void)
5677e0c4386eSCy Schubert {
5678e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
5679e0c4386eSCy Schubert     SSL *serverssl = NULL, *clientssl = NULL;
5680e0c4386eSCy Schubert     int testresult = 0;
5681e0c4386eSCy Schubert 
5682e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5683e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
5684e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
5685e0c4386eSCy Schubert         goto end;
5686e0c4386eSCy Schubert 
5687e0c4386eSCy Schubert     /* The arrival of CCS messages can confuse the test */
5688e0c4386eSCy Schubert     SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
5689e0c4386eSCy Schubert 
5690e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5691e0c4386eSCy Schubert                                       NULL, NULL))
5692e0c4386eSCy Schubert                /* Send the first ClientHello */
5693e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
5694e0c4386eSCy Schubert                                                  SSL_ERROR_WANT_READ))
5695e0c4386eSCy Schubert                /*
5696e0c4386eSCy Schubert                 * This should fail with a -1 return because we have no callbacks
5697e0c4386eSCy Schubert                 * set up
5698e0c4386eSCy Schubert                 */
5699e0c4386eSCy Schubert             || !TEST_int_eq(SSL_stateless(serverssl), -1))
5700e0c4386eSCy Schubert         goto end;
5701e0c4386eSCy Schubert 
5702e0c4386eSCy Schubert     /* Fatal error so abandon the connection from this client */
5703e0c4386eSCy Schubert     SSL_free(clientssl);
5704e0c4386eSCy Schubert     clientssl = NULL;
5705e0c4386eSCy Schubert 
5706e0c4386eSCy Schubert     /* Set up the cookie generation and verification callbacks */
5707e0c4386eSCy Schubert     SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
5708e0c4386eSCy Schubert     SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
5709e0c4386eSCy Schubert 
5710e0c4386eSCy Schubert     /*
5711e0c4386eSCy Schubert      * Create a new connection from the client (we can reuse the server SSL
5712e0c4386eSCy Schubert      * object).
5713e0c4386eSCy Schubert      */
5714e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5715e0c4386eSCy Schubert                                              NULL, NULL))
5716e0c4386eSCy Schubert                /* Send the first ClientHello */
5717e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
5718e0c4386eSCy Schubert                                                 SSL_ERROR_WANT_READ))
5719e0c4386eSCy Schubert                /* This should fail because there is no cookie */
5720e0c4386eSCy Schubert             || !TEST_int_eq(SSL_stateless(serverssl), 0))
5721e0c4386eSCy Schubert         goto end;
5722e0c4386eSCy Schubert 
5723e0c4386eSCy Schubert     /* Abandon the connection from this client */
5724e0c4386eSCy Schubert     SSL_free(clientssl);
5725e0c4386eSCy Schubert     clientssl = NULL;
5726e0c4386eSCy Schubert 
5727e0c4386eSCy Schubert     /*
5728e0c4386eSCy Schubert      * Now create a connection from a new client but with the same server SSL
5729e0c4386eSCy Schubert      * object
5730e0c4386eSCy Schubert      */
5731e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5732e0c4386eSCy Schubert                                              NULL, NULL))
5733e0c4386eSCy Schubert                /* Send the first ClientHello */
5734e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
5735e0c4386eSCy Schubert                                                 SSL_ERROR_WANT_READ))
5736e0c4386eSCy Schubert                /* This should fail because there is no cookie */
5737e0c4386eSCy Schubert             || !TEST_int_eq(SSL_stateless(serverssl), 0)
5738e0c4386eSCy Schubert                /* Send the second ClientHello */
5739e0c4386eSCy Schubert             || !TEST_false(create_ssl_connection(serverssl, clientssl,
5740e0c4386eSCy Schubert                                                 SSL_ERROR_WANT_READ))
5741e0c4386eSCy Schubert                /* This should succeed because a cookie is now present */
5742e0c4386eSCy Schubert             || !TEST_int_eq(SSL_stateless(serverssl), 1)
5743e0c4386eSCy Schubert                /* Complete the connection */
5744e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
5745e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
5746e0c4386eSCy Schubert         goto end;
5747e0c4386eSCy Schubert 
5748e0c4386eSCy Schubert     shutdown_ssl_connection(serverssl, clientssl);
5749e0c4386eSCy Schubert     serverssl = clientssl = NULL;
5750e0c4386eSCy Schubert     testresult = 1;
5751e0c4386eSCy Schubert 
5752e0c4386eSCy Schubert  end:
5753e0c4386eSCy Schubert     SSL_free(serverssl);
5754e0c4386eSCy Schubert     SSL_free(clientssl);
5755e0c4386eSCy Schubert     SSL_CTX_free(sctx);
5756e0c4386eSCy Schubert     SSL_CTX_free(cctx);
5757e0c4386eSCy Schubert     return testresult;
5758e0c4386eSCy Schubert 
5759e0c4386eSCy Schubert }
5760e0c4386eSCy Schubert #endif /* OSSL_NO_USABLE_TLS1_3 */
5761e0c4386eSCy Schubert 
5762e0c4386eSCy Schubert static int clntaddoldcb = 0;
5763e0c4386eSCy Schubert static int clntparseoldcb = 0;
5764e0c4386eSCy Schubert static int srvaddoldcb = 0;
5765e0c4386eSCy Schubert static int srvparseoldcb = 0;
5766e0c4386eSCy Schubert static int clntaddnewcb = 0;
5767e0c4386eSCy Schubert static int clntparsenewcb = 0;
5768e0c4386eSCy Schubert static int srvaddnewcb = 0;
5769e0c4386eSCy Schubert static int srvparsenewcb = 0;
5770e0c4386eSCy Schubert static int snicb = 0;
5771e0c4386eSCy Schubert 
5772e0c4386eSCy Schubert #define TEST_EXT_TYPE1  0xff00
5773e0c4386eSCy Schubert 
old_add_cb(SSL * s,unsigned int ext_type,const unsigned char ** out,size_t * outlen,int * al,void * add_arg)5774e0c4386eSCy Schubert static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
5775e0c4386eSCy Schubert                       size_t *outlen, int *al, void *add_arg)
5776e0c4386eSCy Schubert {
5777e0c4386eSCy Schubert     int *server = (int *)add_arg;
5778e0c4386eSCy Schubert     unsigned char *data;
5779e0c4386eSCy Schubert 
5780e0c4386eSCy Schubert     if (SSL_is_server(s))
5781e0c4386eSCy Schubert         srvaddoldcb++;
5782e0c4386eSCy Schubert     else
5783e0c4386eSCy Schubert         clntaddoldcb++;
5784e0c4386eSCy Schubert 
5785e0c4386eSCy Schubert     if (*server != SSL_is_server(s)
5786e0c4386eSCy Schubert             || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5787e0c4386eSCy Schubert         return -1;
5788e0c4386eSCy Schubert 
5789e0c4386eSCy Schubert     *data = 1;
5790e0c4386eSCy Schubert     *out = data;
5791e0c4386eSCy Schubert     *outlen = sizeof(char);
5792e0c4386eSCy Schubert     return 1;
5793e0c4386eSCy Schubert }
5794e0c4386eSCy Schubert 
old_free_cb(SSL * s,unsigned int ext_type,const unsigned char * out,void * add_arg)5795e0c4386eSCy Schubert static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
5796e0c4386eSCy Schubert                         void *add_arg)
5797e0c4386eSCy Schubert {
5798e0c4386eSCy Schubert     OPENSSL_free((unsigned char *)out);
5799e0c4386eSCy Schubert }
5800e0c4386eSCy Schubert 
old_parse_cb(SSL * s,unsigned int ext_type,const unsigned char * in,size_t inlen,int * al,void * parse_arg)5801e0c4386eSCy Schubert static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
5802e0c4386eSCy Schubert                         size_t inlen, int *al, void *parse_arg)
5803e0c4386eSCy Schubert {
5804e0c4386eSCy Schubert     int *server = (int *)parse_arg;
5805e0c4386eSCy Schubert 
5806e0c4386eSCy Schubert     if (SSL_is_server(s))
5807e0c4386eSCy Schubert         srvparseoldcb++;
5808e0c4386eSCy Schubert     else
5809e0c4386eSCy Schubert         clntparseoldcb++;
5810e0c4386eSCy Schubert 
5811e0c4386eSCy Schubert     if (*server != SSL_is_server(s)
5812e0c4386eSCy Schubert             || inlen != sizeof(char)
5813e0c4386eSCy Schubert             || *in != 1)
5814e0c4386eSCy Schubert         return -1;
5815e0c4386eSCy Schubert 
5816e0c4386eSCy Schubert     return 1;
5817e0c4386eSCy Schubert }
5818e0c4386eSCy Schubert 
new_add_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char ** out,size_t * outlen,X509 * x,size_t chainidx,int * al,void * add_arg)5819e0c4386eSCy Schubert static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
5820e0c4386eSCy Schubert                       const unsigned char **out, size_t *outlen, X509 *x,
5821e0c4386eSCy Schubert                       size_t chainidx, int *al, void *add_arg)
5822e0c4386eSCy Schubert {
5823e0c4386eSCy Schubert     int *server = (int *)add_arg;
5824e0c4386eSCy Schubert     unsigned char *data;
5825e0c4386eSCy Schubert 
5826e0c4386eSCy Schubert     if (SSL_is_server(s))
5827e0c4386eSCy Schubert         srvaddnewcb++;
5828e0c4386eSCy Schubert     else
5829e0c4386eSCy Schubert         clntaddnewcb++;
5830e0c4386eSCy Schubert 
5831e0c4386eSCy Schubert     if (*server != SSL_is_server(s)
5832e0c4386eSCy Schubert             || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5833e0c4386eSCy Schubert         return -1;
5834e0c4386eSCy Schubert 
5835e0c4386eSCy Schubert     *data = 1;
5836e0c4386eSCy Schubert     *out = data;
5837e0c4386eSCy Schubert     *outlen = sizeof(*data);
5838e0c4386eSCy Schubert     return 1;
5839e0c4386eSCy Schubert }
5840e0c4386eSCy Schubert 
new_free_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * out,void * add_arg)5841e0c4386eSCy Schubert static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
5842e0c4386eSCy Schubert                         const unsigned char *out, void *add_arg)
5843e0c4386eSCy Schubert {
5844e0c4386eSCy Schubert     OPENSSL_free((unsigned char *)out);
5845e0c4386eSCy Schubert }
5846e0c4386eSCy Schubert 
new_parse_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * in,size_t inlen,X509 * x,size_t chainidx,int * al,void * parse_arg)5847e0c4386eSCy Schubert static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
5848e0c4386eSCy Schubert                         const unsigned char *in, size_t inlen, X509 *x,
5849e0c4386eSCy Schubert                         size_t chainidx, int *al, void *parse_arg)
5850e0c4386eSCy Schubert {
5851e0c4386eSCy Schubert     int *server = (int *)parse_arg;
5852e0c4386eSCy Schubert 
5853e0c4386eSCy Schubert     if (SSL_is_server(s))
5854e0c4386eSCy Schubert         srvparsenewcb++;
5855e0c4386eSCy Schubert     else
5856e0c4386eSCy Schubert         clntparsenewcb++;
5857e0c4386eSCy Schubert 
5858e0c4386eSCy Schubert     if (*server != SSL_is_server(s)
5859e0c4386eSCy Schubert             || inlen != sizeof(char) || *in != 1)
5860e0c4386eSCy Schubert         return -1;
5861e0c4386eSCy Schubert 
5862e0c4386eSCy Schubert     return 1;
5863e0c4386eSCy Schubert }
5864e0c4386eSCy Schubert 
sni_cb(SSL * s,int * al,void * arg)5865e0c4386eSCy Schubert static int sni_cb(SSL *s, int *al, void *arg)
5866e0c4386eSCy Schubert {
5867e0c4386eSCy Schubert     SSL_CTX *ctx = (SSL_CTX *)arg;
5868e0c4386eSCy Schubert 
5869e0c4386eSCy Schubert     if (SSL_set_SSL_CTX(s, ctx) == NULL) {
5870e0c4386eSCy Schubert         *al = SSL_AD_INTERNAL_ERROR;
5871e0c4386eSCy Schubert         return SSL_TLSEXT_ERR_ALERT_FATAL;
5872e0c4386eSCy Schubert     }
5873e0c4386eSCy Schubert     snicb++;
5874e0c4386eSCy Schubert     return SSL_TLSEXT_ERR_OK;
5875e0c4386eSCy Schubert }
5876e0c4386eSCy Schubert 
verify_cb(int preverify_ok,X509_STORE_CTX * x509_ctx)5877e0c4386eSCy Schubert static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
5878e0c4386eSCy Schubert {
5879e0c4386eSCy Schubert     return 1;
5880e0c4386eSCy Schubert }
5881e0c4386eSCy Schubert 
5882e0c4386eSCy Schubert /*
5883e0c4386eSCy Schubert  * Custom call back tests.
5884e0c4386eSCy Schubert  * Test 0: Old style callbacks in TLSv1.2
5885e0c4386eSCy Schubert  * Test 1: New style callbacks in TLSv1.2
5886e0c4386eSCy Schubert  * Test 2: New style callbacks in TLSv1.2 with SNI
5887e0c4386eSCy Schubert  * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
5888e0c4386eSCy Schubert  * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
5889e0c4386eSCy Schubert  * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
5890e0c4386eSCy Schubert  */
test_custom_exts(int tst)5891e0c4386eSCy Schubert static int test_custom_exts(int tst)
5892e0c4386eSCy Schubert {
5893e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
5894e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
5895e0c4386eSCy Schubert     int testresult = 0;
5896e0c4386eSCy Schubert     static int server = 1;
5897e0c4386eSCy Schubert     static int client = 0;
5898e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
5899e0c4386eSCy Schubert     unsigned int context;
5900e0c4386eSCy Schubert 
5901e0c4386eSCy Schubert #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
5902e0c4386eSCy Schubert     /* Skip tests for TLSv1.2 and below in this case */
5903e0c4386eSCy Schubert     if (tst < 3)
5904e0c4386eSCy Schubert         return 1;
5905e0c4386eSCy Schubert #endif
5906e0c4386eSCy Schubert 
5907e0c4386eSCy Schubert     /* Reset callback counters */
5908e0c4386eSCy Schubert     clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
5909e0c4386eSCy Schubert     clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
5910e0c4386eSCy Schubert     snicb = 0;
5911e0c4386eSCy Schubert 
5912e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5913e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
5914e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
5915e0c4386eSCy Schubert         goto end;
5916e0c4386eSCy Schubert 
5917e0c4386eSCy Schubert     if (tst == 2
5918e0c4386eSCy Schubert             && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
5919e0c4386eSCy Schubert                                               TLS1_VERSION, 0,
5920e0c4386eSCy Schubert                                               &sctx2, NULL, cert, privkey)))
5921e0c4386eSCy Schubert         goto end;
5922e0c4386eSCy Schubert 
5923e0c4386eSCy Schubert 
5924e0c4386eSCy Schubert     if (tst < 3) {
5925e0c4386eSCy Schubert         SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
5926e0c4386eSCy Schubert         SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
5927e0c4386eSCy Schubert         if (sctx2 != NULL)
5928e0c4386eSCy Schubert             SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
5929e0c4386eSCy Schubert     }
5930e0c4386eSCy Schubert 
5931e0c4386eSCy Schubert     if (tst == 5) {
5932e0c4386eSCy Schubert         context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
5933e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_CERTIFICATE;
5934e0c4386eSCy Schubert         SSL_CTX_set_verify(sctx,
5935e0c4386eSCy Schubert                            SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
5936e0c4386eSCy Schubert                            verify_cb);
5937e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
5938e0c4386eSCy Schubert                                                       SSL_FILETYPE_PEM), 1)
5939e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
5940e0c4386eSCy Schubert                                                             SSL_FILETYPE_PEM), 1)
5941e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
5942e0c4386eSCy Schubert             goto end;
5943e0c4386eSCy Schubert     } else if (tst == 4) {
5944e0c4386eSCy Schubert         context = SSL_EXT_CLIENT_HELLO
5945e0c4386eSCy Schubert                   | SSL_EXT_TLS1_2_SERVER_HELLO
5946e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_SERVER_HELLO
5947e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
5948e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_CERTIFICATE
5949e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
5950e0c4386eSCy Schubert     } else {
5951e0c4386eSCy Schubert         context = SSL_EXT_CLIENT_HELLO
5952e0c4386eSCy Schubert                   | SSL_EXT_TLS1_2_SERVER_HELLO
5953e0c4386eSCy Schubert                   | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
5954e0c4386eSCy Schubert     }
5955e0c4386eSCy Schubert 
5956e0c4386eSCy Schubert     /* Create a client side custom extension */
5957e0c4386eSCy Schubert     if (tst == 0) {
5958e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5959e0c4386eSCy Schubert                                                      old_add_cb, old_free_cb,
5960e0c4386eSCy Schubert                                                      &client, old_parse_cb,
5961e0c4386eSCy Schubert                                                      &client)))
5962e0c4386eSCy Schubert             goto end;
5963e0c4386eSCy Schubert     } else {
5964e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
5965e0c4386eSCy Schubert                                               new_add_cb, new_free_cb,
5966e0c4386eSCy Schubert                                               &client, new_parse_cb, &client)))
5967e0c4386eSCy Schubert             goto end;
5968e0c4386eSCy Schubert     }
5969e0c4386eSCy Schubert 
5970e0c4386eSCy Schubert     /* Should not be able to add duplicates */
5971e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5972e0c4386eSCy Schubert                                                   old_add_cb, old_free_cb,
5973e0c4386eSCy Schubert                                                   &client, old_parse_cb,
5974e0c4386eSCy Schubert                                                   &client))
5975e0c4386eSCy Schubert             || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
5976e0c4386eSCy Schubert                                                   context, new_add_cb,
5977e0c4386eSCy Schubert                                                   new_free_cb, &client,
5978e0c4386eSCy Schubert                                                   new_parse_cb, &client)))
5979e0c4386eSCy Schubert         goto end;
5980e0c4386eSCy Schubert 
5981e0c4386eSCy Schubert     /* Create a server side custom extension */
5982e0c4386eSCy Schubert     if (tst == 0) {
5983e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5984e0c4386eSCy Schubert                                                      old_add_cb, old_free_cb,
5985e0c4386eSCy Schubert                                                      &server, old_parse_cb,
5986e0c4386eSCy Schubert                                                      &server)))
5987e0c4386eSCy Schubert             goto end;
5988e0c4386eSCy Schubert     } else {
5989e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
5990e0c4386eSCy Schubert                                               new_add_cb, new_free_cb,
5991e0c4386eSCy Schubert                                               &server, new_parse_cb, &server)))
5992e0c4386eSCy Schubert             goto end;
5993e0c4386eSCy Schubert         if (sctx2 != NULL
5994e0c4386eSCy Schubert                 && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
5995e0c4386eSCy Schubert                                                      context, new_add_cb,
5996e0c4386eSCy Schubert                                                      new_free_cb, &server,
5997e0c4386eSCy Schubert                                                      new_parse_cb, &server)))
5998e0c4386eSCy Schubert             goto end;
5999e0c4386eSCy Schubert     }
6000e0c4386eSCy Schubert 
6001e0c4386eSCy Schubert     /* Should not be able to add duplicates */
6002e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
6003e0c4386eSCy Schubert                                                   old_add_cb, old_free_cb,
6004e0c4386eSCy Schubert                                                   &server, old_parse_cb,
6005e0c4386eSCy Schubert                                                   &server))
6006e0c4386eSCy Schubert             || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
6007e0c4386eSCy Schubert                                                   context, new_add_cb,
6008e0c4386eSCy Schubert                                                   new_free_cb, &server,
6009e0c4386eSCy Schubert                                                   new_parse_cb, &server)))
6010e0c4386eSCy Schubert         goto end;
6011e0c4386eSCy Schubert 
6012e0c4386eSCy Schubert     if (tst == 2) {
6013e0c4386eSCy Schubert         /* Set up SNI */
6014e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
6015e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
6016e0c4386eSCy Schubert             goto end;
6017e0c4386eSCy Schubert     }
6018e0c4386eSCy Schubert 
6019e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6020e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
6021e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6022e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6023e0c4386eSCy Schubert         goto end;
6024e0c4386eSCy Schubert 
6025e0c4386eSCy Schubert     if (tst == 0) {
6026e0c4386eSCy Schubert         if (clntaddoldcb != 1
6027e0c4386eSCy Schubert                 || clntparseoldcb != 1
6028e0c4386eSCy Schubert                 || srvaddoldcb != 1
6029e0c4386eSCy Schubert                 || srvparseoldcb != 1)
6030e0c4386eSCy Schubert             goto end;
6031e0c4386eSCy Schubert     } else if (tst == 1 || tst == 2 || tst == 3) {
6032e0c4386eSCy Schubert         if (clntaddnewcb != 1
6033e0c4386eSCy Schubert                 || clntparsenewcb != 1
6034e0c4386eSCy Schubert                 || srvaddnewcb != 1
6035e0c4386eSCy Schubert                 || srvparsenewcb != 1
6036e0c4386eSCy Schubert                 || (tst != 2 && snicb != 0)
6037e0c4386eSCy Schubert                 || (tst == 2 && snicb != 1))
6038e0c4386eSCy Schubert             goto end;
6039e0c4386eSCy Schubert     } else if (tst == 5) {
6040e0c4386eSCy Schubert         if (clntaddnewcb != 1
6041e0c4386eSCy Schubert                 || clntparsenewcb != 1
6042e0c4386eSCy Schubert                 || srvaddnewcb != 1
6043e0c4386eSCy Schubert                 || srvparsenewcb != 1)
6044e0c4386eSCy Schubert             goto end;
6045e0c4386eSCy Schubert     } else {
6046e0c4386eSCy Schubert         /* In this case there 2 NewSessionTicket messages created */
6047e0c4386eSCy Schubert         if (clntaddnewcb != 1
6048e0c4386eSCy Schubert                 || clntparsenewcb != 5
6049e0c4386eSCy Schubert                 || srvaddnewcb != 5
6050e0c4386eSCy Schubert                 || srvparsenewcb != 1)
6051e0c4386eSCy Schubert             goto end;
6052e0c4386eSCy Schubert     }
6053e0c4386eSCy Schubert 
6054e0c4386eSCy Schubert     sess = SSL_get1_session(clientssl);
6055e0c4386eSCy Schubert     SSL_shutdown(clientssl);
6056e0c4386eSCy Schubert     SSL_shutdown(serverssl);
6057e0c4386eSCy Schubert     SSL_free(serverssl);
6058e0c4386eSCy Schubert     SSL_free(clientssl);
6059e0c4386eSCy Schubert     serverssl = clientssl = NULL;
6060e0c4386eSCy Schubert 
6061e0c4386eSCy Schubert     if (tst == 3 || tst == 5) {
6062e0c4386eSCy Schubert         /* We don't bother with the resumption aspects for these tests */
6063e0c4386eSCy Schubert         testresult = 1;
6064e0c4386eSCy Schubert         goto end;
6065e0c4386eSCy Schubert     }
6066e0c4386eSCy Schubert 
6067e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6068e0c4386eSCy Schubert                                       NULL, NULL))
6069e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, sess))
6070e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6071e0c4386eSCy Schubert                                                SSL_ERROR_NONE)))
6072e0c4386eSCy Schubert         goto end;
6073e0c4386eSCy Schubert 
6074e0c4386eSCy Schubert     /*
6075e0c4386eSCy Schubert      * For a resumed session we expect to add the ClientHello extension. For the
6076e0c4386eSCy Schubert      * old style callbacks we ignore it on the server side because they set
6077e0c4386eSCy Schubert      * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
6078e0c4386eSCy Schubert      * them.
6079e0c4386eSCy Schubert      */
6080e0c4386eSCy Schubert     if (tst == 0) {
6081e0c4386eSCy Schubert         if (clntaddoldcb != 2
6082e0c4386eSCy Schubert                 || clntparseoldcb != 1
6083e0c4386eSCy Schubert                 || srvaddoldcb != 1
6084e0c4386eSCy Schubert                 || srvparseoldcb != 1)
6085e0c4386eSCy Schubert             goto end;
6086e0c4386eSCy Schubert     } else if (tst == 1 || tst == 2 || tst == 3) {
6087e0c4386eSCy Schubert         if (clntaddnewcb != 2
6088e0c4386eSCy Schubert                 || clntparsenewcb != 2
6089e0c4386eSCy Schubert                 || srvaddnewcb != 2
6090e0c4386eSCy Schubert                 || srvparsenewcb != 2)
6091e0c4386eSCy Schubert             goto end;
6092e0c4386eSCy Schubert     } else {
6093e0c4386eSCy Schubert         /*
6094e0c4386eSCy Schubert          * No Certificate message extensions in the resumption handshake,
6095e0c4386eSCy Schubert          * 2 NewSessionTickets in the initial handshake, 1 in the resumption
6096e0c4386eSCy Schubert          */
6097e0c4386eSCy Schubert         if (clntaddnewcb != 2
6098e0c4386eSCy Schubert                 || clntparsenewcb != 8
6099e0c4386eSCy Schubert                 || srvaddnewcb != 8
6100e0c4386eSCy Schubert                 || srvparsenewcb != 2)
6101e0c4386eSCy Schubert             goto end;
6102e0c4386eSCy Schubert     }
6103e0c4386eSCy Schubert 
6104e0c4386eSCy Schubert     testresult = 1;
6105e0c4386eSCy Schubert 
6106e0c4386eSCy Schubert end:
6107e0c4386eSCy Schubert     SSL_SESSION_free(sess);
6108e0c4386eSCy Schubert     SSL_free(serverssl);
6109e0c4386eSCy Schubert     SSL_free(clientssl);
6110e0c4386eSCy Schubert     SSL_CTX_free(sctx2);
6111e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6112e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6113e0c4386eSCy Schubert     return testresult;
6114e0c4386eSCy Schubert }
6115e0c4386eSCy Schubert 
6116e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
6117e0c4386eSCy Schubert 
6118e0c4386eSCy Schubert #define  SYNTHV1CONTEXT     (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
6119e0c4386eSCy Schubert                              | SSL_EXT_CLIENT_HELLO \
6120e0c4386eSCy Schubert                              | SSL_EXT_TLS1_2_SERVER_HELLO \
6121e0c4386eSCy Schubert                              | SSL_EXT_IGNORE_ON_RESUMPTION)
6122e0c4386eSCy Schubert 
6123e0c4386eSCy Schubert #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
6124e0c4386eSCy Schubert                       | SSL_EXT_TLS1_2_SERVER_HELLO \
6125e0c4386eSCy Schubert                       | SSL_EXT_CLIENT_HELLO)
6126e0c4386eSCy Schubert 
6127e0c4386eSCy Schubert #define SERVERINFO_CUSTOM                                 \
6128e0c4386eSCy Schubert     0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
6129e0c4386eSCy Schubert     0x00, 0x03,                                           \
6130e0c4386eSCy Schubert     0x04, 0x05, 0x06                                      \
6131e0c4386eSCy Schubert 
6132e0c4386eSCy Schubert static const unsigned char serverinfo_custom_tls13[] = {
6133e0c4386eSCy Schubert     0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
6134e0c4386eSCy Schubert     SERVERINFO_CUSTOM
6135e0c4386eSCy Schubert };
6136e0c4386eSCy Schubert static const unsigned char serverinfo_custom_v2[] = {
6137e0c4386eSCy Schubert     0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff,  SYNTHV1CONTEXT & 0xff,
6138e0c4386eSCy Schubert     SERVERINFO_CUSTOM
6139e0c4386eSCy Schubert };
6140e0c4386eSCy Schubert static const unsigned char serverinfo_custom_v1[] = {
6141e0c4386eSCy Schubert     SERVERINFO_CUSTOM
6142e0c4386eSCy Schubert };
6143e0c4386eSCy Schubert static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
6144e0c4386eSCy Schubert static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
6145e0c4386eSCy Schubert static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
6146e0c4386eSCy Schubert 
serverinfo_custom_parse_cb(SSL * s,unsigned int ext_type,unsigned int context,const unsigned char * in,size_t inlen,X509 * x,size_t chainidx,int * al,void * parse_arg)6147e0c4386eSCy Schubert static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
6148e0c4386eSCy Schubert                                       unsigned int context,
6149e0c4386eSCy Schubert                                       const unsigned char *in,
6150e0c4386eSCy Schubert                                       size_t inlen, X509 *x,
6151e0c4386eSCy Schubert                                       size_t chainidx, int *al,
6152e0c4386eSCy Schubert                                       void *parse_arg)
6153e0c4386eSCy Schubert {
6154e0c4386eSCy Schubert     const size_t len = serverinfo_custom_v1_len;
6155e0c4386eSCy Schubert     const unsigned char *si = &serverinfo_custom_v1[len - 3];
6156e0c4386eSCy Schubert     int *p_cb_result = (int*)parse_arg;
6157e0c4386eSCy Schubert     *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
6158e0c4386eSCy Schubert     return 1;
6159e0c4386eSCy Schubert }
6160e0c4386eSCy Schubert 
test_serverinfo_custom(const int idx)6161e0c4386eSCy Schubert static int test_serverinfo_custom(const int idx)
6162e0c4386eSCy Schubert {
6163e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
6164e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6165e0c4386eSCy Schubert     int testresult = 0;
6166e0c4386eSCy Schubert     int cb_result = 0;
6167e0c4386eSCy Schubert 
6168e0c4386eSCy Schubert     /*
6169e0c4386eSCy Schubert      * Following variables are set in the switch statement
6170e0c4386eSCy Schubert      *  according to the test iteration.
6171e0c4386eSCy Schubert      * Default values do not make much sense: test would fail with them.
6172e0c4386eSCy Schubert      */
6173e0c4386eSCy Schubert     int serverinfo_version = 0;
6174e0c4386eSCy Schubert     int protocol_version = 0;
6175e0c4386eSCy Schubert     unsigned int extension_context = 0;
6176e0c4386eSCy Schubert     const unsigned char *si = NULL;
6177e0c4386eSCy Schubert     size_t si_len = 0;
6178e0c4386eSCy Schubert 
6179e0c4386eSCy Schubert     const int call_use_serverinfo_ex = idx > 0;
6180e0c4386eSCy Schubert     switch (idx) {
6181e0c4386eSCy Schubert     case 0: /* FALLTHROUGH */
6182e0c4386eSCy Schubert     case 1:
6183e0c4386eSCy Schubert         serverinfo_version = SSL_SERVERINFOV1;
6184e0c4386eSCy Schubert         protocol_version = TLS1_2_VERSION;
6185e0c4386eSCy Schubert         extension_context = SYNTHV1CONTEXT;
6186e0c4386eSCy Schubert         si = serverinfo_custom_v1;
6187e0c4386eSCy Schubert         si_len = serverinfo_custom_v1_len;
6188e0c4386eSCy Schubert         break;
6189e0c4386eSCy Schubert     case 2:
6190e0c4386eSCy Schubert         serverinfo_version = SSL_SERVERINFOV2;
6191e0c4386eSCy Schubert         protocol_version = TLS1_2_VERSION;
6192e0c4386eSCy Schubert         extension_context = SYNTHV1CONTEXT;
6193e0c4386eSCy Schubert         si = serverinfo_custom_v2;
6194e0c4386eSCy Schubert         si_len = serverinfo_custom_v2_len;
6195e0c4386eSCy Schubert         break;
6196e0c4386eSCy Schubert     case 3:
6197e0c4386eSCy Schubert         serverinfo_version = SSL_SERVERINFOV2;
6198e0c4386eSCy Schubert         protocol_version = TLS1_3_VERSION;
6199e0c4386eSCy Schubert         extension_context = TLS13CONTEXT;
6200e0c4386eSCy Schubert         si = serverinfo_custom_tls13;
6201e0c4386eSCy Schubert         si_len = serverinfo_custom_tls13_len;
6202e0c4386eSCy Schubert         break;
6203e0c4386eSCy Schubert     }
6204e0c4386eSCy Schubert 
6205e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx,
6206e0c4386eSCy Schubert                                        TLS_method(),
6207e0c4386eSCy Schubert                                        TLS_method(),
6208e0c4386eSCy Schubert                                        protocol_version,
6209e0c4386eSCy Schubert                                        protocol_version,
6210e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
6211e0c4386eSCy Schubert         goto end;
6212e0c4386eSCy Schubert 
6213e0c4386eSCy Schubert     if (call_use_serverinfo_ex) {
6214e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
6215e0c4386eSCy Schubert                                                  si, si_len)))
6216e0c4386eSCy Schubert             goto end;
6217e0c4386eSCy Schubert     } else {
6218e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
6219e0c4386eSCy Schubert             goto end;
6220e0c4386eSCy Schubert     }
6221e0c4386eSCy Schubert 
6222e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
6223e0c4386eSCy Schubert                                           extension_context,
6224e0c4386eSCy Schubert                                           NULL, NULL, NULL,
6225e0c4386eSCy Schubert                                           serverinfo_custom_parse_cb,
6226e0c4386eSCy Schubert                                           &cb_result))
6227e0c4386eSCy Schubert         || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6228e0c4386eSCy Schubert                                          NULL, NULL))
6229e0c4386eSCy Schubert         || !TEST_true(create_ssl_connection(serverssl, clientssl,
6230e0c4386eSCy Schubert                                             SSL_ERROR_NONE))
6231e0c4386eSCy Schubert         || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
6232e0c4386eSCy Schubert         goto end;
6233e0c4386eSCy Schubert 
6234e0c4386eSCy Schubert     if (!TEST_true(cb_result))
6235e0c4386eSCy Schubert         goto end;
6236e0c4386eSCy Schubert 
6237e0c4386eSCy Schubert     testresult = 1;
6238e0c4386eSCy Schubert 
6239e0c4386eSCy Schubert  end:
6240e0c4386eSCy Schubert     SSL_free(serverssl);
6241e0c4386eSCy Schubert     SSL_free(clientssl);
6242e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6243e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6244e0c4386eSCy Schubert 
6245e0c4386eSCy Schubert     return testresult;
6246e0c4386eSCy Schubert }
6247e0c4386eSCy Schubert #endif
6248e0c4386eSCy Schubert 
6249e0c4386eSCy Schubert /*
6250e0c4386eSCy Schubert  * Test that SSL_export_keying_material() produces expected results. There are
6251e0c4386eSCy Schubert  * no test vectors so all we do is test that both sides of the communication
6252e0c4386eSCy Schubert  * produce the same results for different protocol versions.
6253e0c4386eSCy Schubert  */
6254e0c4386eSCy Schubert #define SMALL_LABEL_LEN 10
6255e0c4386eSCy Schubert #define LONG_LABEL_LEN  249
test_export_key_mat(int tst)6256e0c4386eSCy Schubert static int test_export_key_mat(int tst)
6257e0c4386eSCy Schubert {
6258e0c4386eSCy Schubert     int testresult = 0;
6259e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
6260e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6261e0c4386eSCy Schubert     const char label[LONG_LABEL_LEN + 1] = "test label";
6262e0c4386eSCy Schubert     const unsigned char context[] = "context";
6263e0c4386eSCy Schubert     const unsigned char *emptycontext = NULL;
6264e0c4386eSCy Schubert     unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
6265e0c4386eSCy Schubert     unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
6266e0c4386eSCy Schubert     size_t labellen;
6267e0c4386eSCy Schubert     const int protocols[] = {
6268e0c4386eSCy Schubert         TLS1_VERSION,
6269e0c4386eSCy Schubert         TLS1_1_VERSION,
6270e0c4386eSCy Schubert         TLS1_2_VERSION,
6271e0c4386eSCy Schubert         TLS1_3_VERSION,
6272e0c4386eSCy Schubert         TLS1_3_VERSION,
6273e0c4386eSCy Schubert         TLS1_3_VERSION
6274e0c4386eSCy Schubert     };
6275e0c4386eSCy Schubert 
6276e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1
6277e0c4386eSCy Schubert     if (tst == 0)
6278e0c4386eSCy Schubert         return 1;
6279e0c4386eSCy Schubert #endif
6280e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_1
6281e0c4386eSCy Schubert     if (tst == 1)
6282e0c4386eSCy Schubert         return 1;
6283e0c4386eSCy Schubert #endif
6284e0c4386eSCy Schubert     if (is_fips && (tst == 0 || tst == 1))
6285e0c4386eSCy Schubert         return 1;
6286e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
6287e0c4386eSCy Schubert     if (tst == 2)
6288e0c4386eSCy Schubert         return 1;
6289e0c4386eSCy Schubert #endif
6290e0c4386eSCy Schubert #ifdef OSSL_NO_USABLE_TLS1_3
6291e0c4386eSCy Schubert     if (tst >= 3)
6292e0c4386eSCy Schubert         return 1;
6293e0c4386eSCy Schubert #endif
6294e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6295e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
6296e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
6297e0c4386eSCy Schubert         goto end;
6298e0c4386eSCy Schubert 
6299e0c4386eSCy Schubert     OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
6300e0c4386eSCy Schubert     SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
6301e0c4386eSCy Schubert     SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
6302e0c4386eSCy Schubert     if ((protocols[tst] < TLS1_2_VERSION) &&
6303e0c4386eSCy Schubert         (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
6304e0c4386eSCy Schubert         || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
6305e0c4386eSCy Schubert         goto end;
6306e0c4386eSCy Schubert 
6307e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
6308e0c4386eSCy Schubert                                       NULL)))
6309e0c4386eSCy Schubert         goto end;
6310e0c4386eSCy Schubert 
6311e0c4386eSCy Schubert     /*
6312e0c4386eSCy Schubert      * Premature call of SSL_export_keying_material should just fail.
6313e0c4386eSCy Schubert      */
6314e0c4386eSCy Schubert     if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6315e0c4386eSCy Schubert                                                 sizeof(ckeymat1), label,
6316e0c4386eSCy Schubert                                                 SMALL_LABEL_LEN + 1, context,
6317e0c4386eSCy Schubert                                                 sizeof(context) - 1, 1), 0))
6318e0c4386eSCy Schubert         goto end;
6319e0c4386eSCy Schubert 
6320e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6321e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
6322e0c4386eSCy Schubert         goto end;
6323e0c4386eSCy Schubert 
6324e0c4386eSCy Schubert     if (tst == 5) {
6325e0c4386eSCy Schubert         /*
6326e0c4386eSCy Schubert          * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
6327e0c4386eSCy Schubert          * go over that.
6328e0c4386eSCy Schubert          */
6329e0c4386eSCy Schubert         if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6330e0c4386eSCy Schubert                                                     sizeof(ckeymat1), label,
6331e0c4386eSCy Schubert                                                     LONG_LABEL_LEN + 1, context,
6332e0c4386eSCy Schubert                                                     sizeof(context) - 1, 1), 0))
6333e0c4386eSCy Schubert             goto end;
6334e0c4386eSCy Schubert 
6335e0c4386eSCy Schubert         testresult = 1;
6336e0c4386eSCy Schubert         goto end;
6337e0c4386eSCy Schubert     } else if (tst == 4) {
6338e0c4386eSCy Schubert         labellen = LONG_LABEL_LEN;
6339e0c4386eSCy Schubert     } else {
6340e0c4386eSCy Schubert         labellen = SMALL_LABEL_LEN;
6341e0c4386eSCy Schubert     }
6342e0c4386eSCy Schubert 
6343e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
6344e0c4386eSCy Schubert                                                 sizeof(ckeymat1), label,
6345e0c4386eSCy Schubert                                                 labellen, context,
6346e0c4386eSCy Schubert                                                 sizeof(context) - 1, 1), 1)
6347e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
6348e0c4386eSCy Schubert                                                        sizeof(ckeymat2), label,
6349e0c4386eSCy Schubert                                                        labellen,
6350e0c4386eSCy Schubert                                                        emptycontext,
6351e0c4386eSCy Schubert                                                        0, 1), 1)
6352e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
6353e0c4386eSCy Schubert                                                        sizeof(ckeymat3), label,
6354e0c4386eSCy Schubert                                                        labellen,
6355e0c4386eSCy Schubert                                                        NULL, 0, 0), 1)
6356e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
6357e0c4386eSCy Schubert                                                        sizeof(skeymat1), label,
6358e0c4386eSCy Schubert                                                        labellen,
6359e0c4386eSCy Schubert                                                        context,
6360e0c4386eSCy Schubert                                                        sizeof(context) -1, 1),
6361e0c4386eSCy Schubert                             1)
6362e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
6363e0c4386eSCy Schubert                                                        sizeof(skeymat2), label,
6364e0c4386eSCy Schubert                                                        labellen,
6365e0c4386eSCy Schubert                                                        emptycontext,
6366e0c4386eSCy Schubert                                                        0, 1), 1)
6367e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
6368e0c4386eSCy Schubert                                                        sizeof(skeymat3), label,
6369e0c4386eSCy Schubert                                                        labellen,
6370e0c4386eSCy Schubert                                                        NULL, 0, 0), 1)
6371e0c4386eSCy Schubert                /*
6372e0c4386eSCy Schubert                 * Check that both sides created the same key material with the
6373e0c4386eSCy Schubert                 * same context.
6374e0c4386eSCy Schubert                 */
6375e0c4386eSCy Schubert             || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6376e0c4386eSCy Schubert                             sizeof(skeymat1))
6377e0c4386eSCy Schubert                /*
6378e0c4386eSCy Schubert                 * Check that both sides created the same key material with an
6379e0c4386eSCy Schubert                 * empty context.
6380e0c4386eSCy Schubert                 */
6381e0c4386eSCy Schubert             || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6382e0c4386eSCy Schubert                             sizeof(skeymat2))
6383e0c4386eSCy Schubert                /*
6384e0c4386eSCy Schubert                 * Check that both sides created the same key material without a
6385e0c4386eSCy Schubert                 * context.
6386e0c4386eSCy Schubert                 */
6387e0c4386eSCy Schubert             || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
6388e0c4386eSCy Schubert                             sizeof(skeymat3))
6389e0c4386eSCy Schubert                /* Different contexts should produce different results */
6390e0c4386eSCy Schubert             || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6391e0c4386eSCy Schubert                             sizeof(ckeymat2)))
6392e0c4386eSCy Schubert         goto end;
6393e0c4386eSCy Schubert 
6394e0c4386eSCy Schubert     /*
6395e0c4386eSCy Schubert      * Check that an empty context and no context produce different results in
6396e0c4386eSCy Schubert      * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
6397e0c4386eSCy Schubert      */
6398e0c4386eSCy Schubert     if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
6399e0c4386eSCy Schubert                                   sizeof(ckeymat3)))
6400e0c4386eSCy Schubert             || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
6401e0c4386eSCy Schubert                                          sizeof(ckeymat3))))
6402e0c4386eSCy Schubert         goto end;
6403e0c4386eSCy Schubert 
6404e0c4386eSCy Schubert     testresult = 1;
6405e0c4386eSCy Schubert 
6406e0c4386eSCy Schubert  end:
6407e0c4386eSCy Schubert     SSL_free(serverssl);
6408e0c4386eSCy Schubert     SSL_free(clientssl);
6409e0c4386eSCy Schubert     SSL_CTX_free(sctx2);
6410e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6411e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6412e0c4386eSCy Schubert 
6413e0c4386eSCy Schubert     return testresult;
6414e0c4386eSCy Schubert }
6415e0c4386eSCy Schubert 
6416e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
6417e0c4386eSCy Schubert /*
6418e0c4386eSCy Schubert  * Test that SSL_export_keying_material_early() produces expected
6419e0c4386eSCy Schubert  * results. There are no test vectors so all we do is test that both
6420e0c4386eSCy Schubert  * sides of the communication produce the same results for different
6421e0c4386eSCy Schubert  * protocol versions.
6422e0c4386eSCy Schubert  */
test_export_key_mat_early(int idx)6423e0c4386eSCy Schubert static int test_export_key_mat_early(int idx)
6424e0c4386eSCy Schubert {
6425e0c4386eSCy Schubert     static const char label[] = "test label";
6426e0c4386eSCy Schubert     static const unsigned char context[] = "context";
6427e0c4386eSCy Schubert     int testresult = 0;
6428e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6429e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6430e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
6431e0c4386eSCy Schubert     const unsigned char *emptycontext = NULL;
6432e0c4386eSCy Schubert     unsigned char ckeymat1[80], ckeymat2[80];
6433e0c4386eSCy Schubert     unsigned char skeymat1[80], skeymat2[80];
6434e0c4386eSCy Schubert     unsigned char buf[1];
6435e0c4386eSCy Schubert     size_t readbytes, written;
6436e0c4386eSCy Schubert 
6437e0c4386eSCy Schubert     if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
6438e0c4386eSCy Schubert                                         &sess, idx, SHA384_DIGEST_LENGTH)))
6439e0c4386eSCy Schubert         goto end;
6440e0c4386eSCy Schubert 
6441e0c4386eSCy Schubert     /* Here writing 0 length early data is enough. */
6442e0c4386eSCy Schubert     if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
6443e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
6444e0c4386eSCy Schubert                                                 &readbytes),
6445e0c4386eSCy Schubert                             SSL_READ_EARLY_DATA_ERROR)
6446e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_early_data_status(serverssl),
6447e0c4386eSCy Schubert                             SSL_EARLY_DATA_ACCEPTED))
6448e0c4386eSCy Schubert         goto end;
6449e0c4386eSCy Schubert 
6450e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_export_keying_material_early(
6451e0c4386eSCy Schubert                      clientssl, ckeymat1, sizeof(ckeymat1), label,
6452e0c4386eSCy Schubert                      sizeof(label) - 1, context, sizeof(context) - 1), 1)
6453e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material_early(
6454e0c4386eSCy Schubert                             clientssl, ckeymat2, sizeof(ckeymat2), label,
6455e0c4386eSCy Schubert                             sizeof(label) - 1, emptycontext, 0), 1)
6456e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material_early(
6457e0c4386eSCy Schubert                             serverssl, skeymat1, sizeof(skeymat1), label,
6458e0c4386eSCy Schubert                             sizeof(label) - 1, context, sizeof(context) - 1), 1)
6459e0c4386eSCy Schubert             || !TEST_int_eq(SSL_export_keying_material_early(
6460e0c4386eSCy Schubert                             serverssl, skeymat2, sizeof(skeymat2), label,
6461e0c4386eSCy Schubert                             sizeof(label) - 1, emptycontext, 0), 1)
6462e0c4386eSCy Schubert                /*
6463e0c4386eSCy Schubert                 * Check that both sides created the same key material with the
6464e0c4386eSCy Schubert                 * same context.
6465e0c4386eSCy Schubert                 */
6466e0c4386eSCy Schubert             || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6467e0c4386eSCy Schubert                             sizeof(skeymat1))
6468e0c4386eSCy Schubert                /*
6469e0c4386eSCy Schubert                 * Check that both sides created the same key material with an
6470e0c4386eSCy Schubert                 * empty context.
6471e0c4386eSCy Schubert                 */
6472e0c4386eSCy Schubert             || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6473e0c4386eSCy Schubert                             sizeof(skeymat2))
6474e0c4386eSCy Schubert                /* Different contexts should produce different results */
6475e0c4386eSCy Schubert             || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6476e0c4386eSCy Schubert                             sizeof(ckeymat2)))
6477e0c4386eSCy Schubert         goto end;
6478e0c4386eSCy Schubert 
6479e0c4386eSCy Schubert     testresult = 1;
6480e0c4386eSCy Schubert 
6481e0c4386eSCy Schubert  end:
6482e0c4386eSCy Schubert     SSL_SESSION_free(sess);
6483e0c4386eSCy Schubert     SSL_SESSION_free(clientpsk);
6484e0c4386eSCy Schubert     SSL_SESSION_free(serverpsk);
6485e0c4386eSCy Schubert     clientpsk = serverpsk = NULL;
6486e0c4386eSCy Schubert     SSL_free(serverssl);
6487e0c4386eSCy Schubert     SSL_free(clientssl);
6488e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6489e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6490e0c4386eSCy Schubert 
6491e0c4386eSCy Schubert     return testresult;
6492e0c4386eSCy Schubert }
6493e0c4386eSCy Schubert 
6494e0c4386eSCy Schubert #define NUM_KEY_UPDATE_MESSAGES 40
6495e0c4386eSCy Schubert /*
6496e0c4386eSCy Schubert  * Test KeyUpdate.
6497e0c4386eSCy Schubert  */
test_key_update(void)6498e0c4386eSCy Schubert static int test_key_update(void)
6499e0c4386eSCy Schubert {
6500e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6501e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6502e0c4386eSCy Schubert     int testresult = 0, i, j;
6503e0c4386eSCy Schubert     char buf[20];
6504e0c4386eSCy Schubert     static char *mess = "A test message";
6505e0c4386eSCy Schubert 
6506e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6507e0c4386eSCy Schubert                                        TLS_client_method(),
6508e0c4386eSCy Schubert                                        TLS1_3_VERSION,
6509e0c4386eSCy Schubert                                        0,
6510e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
6511e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6512e0c4386eSCy Schubert                                              NULL, NULL))
6513e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6514e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6515e0c4386eSCy Schubert         goto end;
6516e0c4386eSCy Schubert 
6517e0c4386eSCy Schubert     for (j = 0; j < 2; j++) {
6518e0c4386eSCy Schubert         /* Send lots of KeyUpdate messages */
6519e0c4386eSCy Schubert         for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
6520e0c4386eSCy Schubert             if (!TEST_true(SSL_key_update(clientssl,
6521e0c4386eSCy Schubert                                           (j == 0)
6522e0c4386eSCy Schubert                                           ? SSL_KEY_UPDATE_NOT_REQUESTED
6523e0c4386eSCy Schubert                                           : SSL_KEY_UPDATE_REQUESTED))
6524e0c4386eSCy Schubert                     || !TEST_true(SSL_do_handshake(clientssl)))
6525e0c4386eSCy Schubert                 goto end;
6526e0c4386eSCy Schubert         }
6527e0c4386eSCy Schubert 
6528e0c4386eSCy Schubert         /* Check that sending and receiving app data is ok */
6529e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
6530e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
6531e0c4386eSCy Schubert                                          strlen(mess)))
6532e0c4386eSCy Schubert             goto end;
6533e0c4386eSCy Schubert 
6534e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
6535e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
6536e0c4386eSCy Schubert                                          strlen(mess)))
6537e0c4386eSCy Schubert             goto end;
6538e0c4386eSCy Schubert     }
6539e0c4386eSCy Schubert 
6540e0c4386eSCy Schubert     testresult = 1;
6541e0c4386eSCy Schubert 
6542e0c4386eSCy Schubert  end:
6543e0c4386eSCy Schubert     SSL_free(serverssl);
6544e0c4386eSCy Schubert     SSL_free(clientssl);
6545e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6546e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6547e0c4386eSCy Schubert 
6548e0c4386eSCy Schubert     return testresult;
6549e0c4386eSCy Schubert }
6550e0c4386eSCy Schubert 
6551e0c4386eSCy Schubert /*
6552e0c4386eSCy Schubert  * Test we can handle a KeyUpdate (update requested) message while
6553e0c4386eSCy Schubert  * write data is pending in peer.
6554e0c4386eSCy Schubert  * Test 0: Client sends KeyUpdate while Server is writing
6555e0c4386eSCy Schubert  * Test 1: Server sends KeyUpdate while Client is writing
6556e0c4386eSCy Schubert  */
test_key_update_peer_in_write(int tst)6557e0c4386eSCy Schubert static int test_key_update_peer_in_write(int tst)
6558e0c4386eSCy Schubert {
6559e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6560e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6561e0c4386eSCy Schubert     int testresult = 0;
6562e0c4386eSCy Schubert     char buf[20];
6563e0c4386eSCy Schubert     static char *mess = "A test message";
6564e0c4386eSCy Schubert     BIO *bretry = BIO_new(bio_s_always_retry());
6565e0c4386eSCy Schubert     BIO *tmp = NULL;
6566e0c4386eSCy Schubert     SSL *peerupdate = NULL, *peerwrite = NULL;
6567e0c4386eSCy Schubert 
6568e0c4386eSCy Schubert     if (!TEST_ptr(bretry)
6569e0c4386eSCy Schubert             || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6570e0c4386eSCy Schubert                                               TLS_client_method(),
6571e0c4386eSCy Schubert                                               TLS1_3_VERSION,
6572e0c4386eSCy Schubert                                               0,
6573e0c4386eSCy Schubert                                               &sctx, &cctx, cert, privkey))
6574e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6575e0c4386eSCy Schubert                                              NULL, NULL))
6576e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6577e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6578e0c4386eSCy Schubert         goto end;
6579e0c4386eSCy Schubert 
6580e0c4386eSCy Schubert     peerupdate = tst == 0 ? clientssl : serverssl;
6581e0c4386eSCy Schubert     peerwrite = tst == 0 ? serverssl : clientssl;
6582e0c4386eSCy Schubert 
6583e0c4386eSCy Schubert     if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
6584e0c4386eSCy Schubert             || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
6585e0c4386eSCy Schubert         goto end;
6586e0c4386eSCy Schubert 
6587e0c4386eSCy Schubert     /* Swap the writing endpoint's write BIO to force a retry */
6588e0c4386eSCy Schubert     tmp = SSL_get_wbio(peerwrite);
6589e0c4386eSCy Schubert     if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6590e0c4386eSCy Schubert         tmp = NULL;
6591e0c4386eSCy Schubert         goto end;
6592e0c4386eSCy Schubert     }
6593e0c4386eSCy Schubert     SSL_set0_wbio(peerwrite, bretry);
6594e0c4386eSCy Schubert     bretry = NULL;
6595e0c4386eSCy Schubert 
6596e0c4386eSCy Schubert     /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
6597e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
6598e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
6599e0c4386eSCy Schubert         goto end;
6600e0c4386eSCy Schubert 
6601e0c4386eSCy Schubert     /* Reinstate the original writing endpoint's write BIO */
6602e0c4386eSCy Schubert     SSL_set0_wbio(peerwrite, tmp);
6603e0c4386eSCy Schubert     tmp = NULL;
6604e0c4386eSCy Schubert 
6605e0c4386eSCy Schubert     /* Now read some data - we will read the key update */
6606e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
6607e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
6608e0c4386eSCy Schubert         goto end;
6609e0c4386eSCy Schubert 
6610e0c4386eSCy Schubert     /*
6611e0c4386eSCy Schubert      * Complete the write we started previously and read it from the other
6612e0c4386eSCy Schubert      * endpoint
6613e0c4386eSCy Schubert      */
6614e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6615e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6616e0c4386eSCy Schubert         goto end;
6617e0c4386eSCy Schubert 
6618e0c4386eSCy Schubert     /* Write more data to ensure we send the KeyUpdate message back */
6619e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6620e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6621e0c4386eSCy Schubert         goto end;
6622e0c4386eSCy Schubert 
6623e0c4386eSCy Schubert     testresult = 1;
6624e0c4386eSCy Schubert 
6625e0c4386eSCy Schubert  end:
6626e0c4386eSCy Schubert     SSL_free(serverssl);
6627e0c4386eSCy Schubert     SSL_free(clientssl);
6628e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6629e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6630e0c4386eSCy Schubert     BIO_free(bretry);
6631e0c4386eSCy Schubert     BIO_free(tmp);
6632e0c4386eSCy Schubert 
6633e0c4386eSCy Schubert     return testresult;
6634e0c4386eSCy Schubert }
6635e0c4386eSCy Schubert 
6636e0c4386eSCy Schubert /*
6637e0c4386eSCy Schubert  * Test we can handle a KeyUpdate (update requested) message while
6638e0c4386eSCy Schubert  * peer read data is pending after peer accepted keyupdate(the msg header
6639e0c4386eSCy Schubert  * had been read 5 bytes).
6640e0c4386eSCy Schubert  * Test 0: Client sends KeyUpdate while Server is reading
6641e0c4386eSCy Schubert  * Test 1: Server sends KeyUpdate while Client is reading
6642e0c4386eSCy Schubert  */
test_key_update_peer_in_read(int tst)6643e0c4386eSCy Schubert static int test_key_update_peer_in_read(int tst)
6644e0c4386eSCy Schubert {
6645e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6646e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6647e0c4386eSCy Schubert     int testresult = 0;
6648e0c4386eSCy Schubert     char prbuf[515], lwbuf[515] = {0};
6649e0c4386eSCy Schubert     static char *mess = "A test message";
6650e0c4386eSCy Schubert     BIO *lbio = NULL, *pbio = NULL;
6651e0c4386eSCy Schubert     SSL *local = NULL, *peer = NULL;
6652e0c4386eSCy Schubert 
6653e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6654e0c4386eSCy Schubert                                               TLS_client_method(),
6655e0c4386eSCy Schubert                                               TLS1_3_VERSION,
6656e0c4386eSCy Schubert                                               0,
6657e0c4386eSCy Schubert                                               &sctx, &cctx, cert, privkey))
6658e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6659e0c4386eSCy Schubert                                              NULL, NULL))
6660e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6661e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6662e0c4386eSCy Schubert         goto end;
6663e0c4386eSCy Schubert 
6664e0c4386eSCy Schubert     local = tst == 0 ? clientssl : serverssl;
6665e0c4386eSCy Schubert     peer = tst == 0 ? serverssl : clientssl;
6666e0c4386eSCy Schubert 
6667e0c4386eSCy Schubert     if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6668e0c4386eSCy Schubert         goto end;
6669e0c4386eSCy Schubert 
6670e0c4386eSCy Schubert     SSL_set_bio(local, lbio, lbio);
6671e0c4386eSCy Schubert     SSL_set_bio(peer, pbio, pbio);
6672e0c4386eSCy Schubert 
6673e0c4386eSCy Schubert     /*
6674e0c4386eSCy Schubert      * we first write keyupdate msg then appdata in local
6675e0c4386eSCy Schubert      * write data in local will fail with SSL_ERROR_WANT_WRITE,because
6676e0c4386eSCy Schubert      * lwbuf app data msg size + key updata msg size > 512(the size of
6677e0c4386eSCy Schubert      * the bio pair buffer)
6678e0c4386eSCy Schubert      */
6679e0c4386eSCy Schubert     if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6680e0c4386eSCy Schubert             || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
6681e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6682e0c4386eSCy Schubert         goto end;
6683e0c4386eSCy Schubert 
6684e0c4386eSCy Schubert     /*
6685e0c4386eSCy Schubert      * first read keyupdate msg in peer in peer
6686e0c4386eSCy Schubert      * then read appdata that we know will fail with SSL_ERROR_WANT_READ
6687e0c4386eSCy Schubert      */
6688e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
6689e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
6690e0c4386eSCy Schubert         goto end;
6691e0c4386eSCy Schubert 
6692e0c4386eSCy Schubert     /* Now write some data in peer - we will write the key update */
6693e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
6694e0c4386eSCy Schubert         goto end;
6695e0c4386eSCy Schubert 
6696e0c4386eSCy Schubert     /*
6697e0c4386eSCy Schubert      * write data in local previously that we will complete
6698e0c4386eSCy Schubert      * read data in peer previously that we will complete
6699e0c4386eSCy Schubert      */
6700e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
6701e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
6702e0c4386eSCy Schubert         goto end;
6703e0c4386eSCy Schubert 
6704e0c4386eSCy Schubert     /* check that sending and receiving appdata ok */
6705e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6706e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6707e0c4386eSCy Schubert         goto end;
6708e0c4386eSCy Schubert 
6709e0c4386eSCy Schubert     testresult = 1;
6710e0c4386eSCy Schubert 
6711e0c4386eSCy Schubert  end:
6712e0c4386eSCy Schubert     SSL_free(serverssl);
6713e0c4386eSCy Schubert     SSL_free(clientssl);
6714e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6715e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6716e0c4386eSCy Schubert 
6717e0c4386eSCy Schubert     return testresult;
6718e0c4386eSCy Schubert }
6719e0c4386eSCy Schubert 
6720e0c4386eSCy Schubert /*
6721e0c4386eSCy Schubert  * Test we can't send a KeyUpdate (update requested) message while
6722e0c4386eSCy Schubert  * local write data is pending.
6723e0c4386eSCy Schubert  * Test 0: Client sends KeyUpdate while Client is writing
6724e0c4386eSCy Schubert  * Test 1: Server sends KeyUpdate while Server is writing
6725e0c4386eSCy Schubert  */
test_key_update_local_in_write(int tst)6726e0c4386eSCy Schubert static int test_key_update_local_in_write(int tst)
6727e0c4386eSCy Schubert {
6728e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6729e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6730e0c4386eSCy Schubert     int testresult = 0;
6731e0c4386eSCy Schubert     char buf[20];
6732e0c4386eSCy Schubert     static char *mess = "A test message";
6733e0c4386eSCy Schubert     BIO *bretry = BIO_new(bio_s_always_retry());
6734e0c4386eSCy Schubert     BIO *tmp = NULL;
6735e0c4386eSCy Schubert     SSL *local = NULL, *peer = NULL;
6736e0c4386eSCy Schubert 
6737e0c4386eSCy Schubert     if (!TEST_ptr(bretry)
6738e0c4386eSCy Schubert             || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6739e0c4386eSCy Schubert                                               TLS_client_method(),
6740e0c4386eSCy Schubert                                               TLS1_3_VERSION,
6741e0c4386eSCy Schubert                                               0,
6742e0c4386eSCy Schubert                                               &sctx, &cctx, cert, privkey))
6743e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6744e0c4386eSCy Schubert                                              NULL, NULL))
6745e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6746e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6747e0c4386eSCy Schubert         goto end;
6748e0c4386eSCy Schubert 
6749e0c4386eSCy Schubert     local = tst == 0 ? clientssl : serverssl;
6750e0c4386eSCy Schubert     peer = tst == 0 ? serverssl : clientssl;
6751e0c4386eSCy Schubert 
6752e0c4386eSCy Schubert     /* Swap the writing endpoint's write BIO to force a retry */
6753e0c4386eSCy Schubert     tmp = SSL_get_wbio(local);
6754e0c4386eSCy Schubert     if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6755e0c4386eSCy Schubert         tmp = NULL;
6756e0c4386eSCy Schubert         goto end;
6757e0c4386eSCy Schubert     }
6758e0c4386eSCy Schubert     SSL_set0_wbio(local, bretry);
6759e0c4386eSCy Schubert     bretry = NULL;
6760e0c4386eSCy Schubert 
6761e0c4386eSCy Schubert     /* write data in local will fail with SSL_ERROR_WANT_WRITE */
6762e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
6763e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6764e0c4386eSCy Schubert         goto end;
6765e0c4386eSCy Schubert 
6766e0c4386eSCy Schubert     /* Reinstate the original writing endpoint's write BIO */
6767e0c4386eSCy Schubert     SSL_set0_wbio(local, tmp);
6768e0c4386eSCy Schubert     tmp = NULL;
6769e0c4386eSCy Schubert 
6770e0c4386eSCy Schubert     /* SSL_key_update will fail, because writing in local*/
6771e0c4386eSCy Schubert     if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6772e0c4386eSCy Schubert         || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
6773e0c4386eSCy Schubert     goto end;
6774e0c4386eSCy Schubert 
6775e0c4386eSCy Schubert     ERR_clear_error();
6776e0c4386eSCy Schubert     /* write data in local previously that we will complete */
6777e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
6778e0c4386eSCy Schubert         goto end;
6779e0c4386eSCy Schubert 
6780e0c4386eSCy Schubert     /* SSL_key_update will succeed because there is no pending write data */
6781e0c4386eSCy Schubert     if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6782e0c4386eSCy Schubert         || !TEST_int_eq(SSL_do_handshake(local), 1))
6783e0c4386eSCy Schubert         goto end;
6784e0c4386eSCy Schubert 
6785e0c4386eSCy Schubert     /*
6786e0c4386eSCy Schubert      * we write some appdata in local
6787e0c4386eSCy Schubert      * read data in peer - we will read the keyupdate msg
6788e0c4386eSCy Schubert      */
6789e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6790e0c4386eSCy Schubert         || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
6791e0c4386eSCy Schubert         goto end;
6792e0c4386eSCy Schubert 
6793e0c4386eSCy Schubert     /* Write more peer more data to ensure we send the keyupdate message back */
6794e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6795e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
6796e0c4386eSCy Schubert         goto end;
6797e0c4386eSCy Schubert 
6798e0c4386eSCy Schubert     testresult = 1;
6799e0c4386eSCy Schubert 
6800e0c4386eSCy Schubert  end:
6801e0c4386eSCy Schubert     SSL_free(serverssl);
6802e0c4386eSCy Schubert     SSL_free(clientssl);
6803e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6804e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6805e0c4386eSCy Schubert     BIO_free(bretry);
6806e0c4386eSCy Schubert     BIO_free(tmp);
6807e0c4386eSCy Schubert 
6808e0c4386eSCy Schubert     return testresult;
6809e0c4386eSCy Schubert }
6810e0c4386eSCy Schubert 
6811e0c4386eSCy Schubert /*
6812e0c4386eSCy Schubert  * Test we can handle a KeyUpdate (update requested) message while
6813e0c4386eSCy Schubert  * local read data is pending(the msg header had been read 5 bytes).
6814e0c4386eSCy Schubert  * Test 0: Client sends KeyUpdate while Client is reading
6815e0c4386eSCy Schubert  * Test 1: Server sends KeyUpdate while Server is reading
6816e0c4386eSCy Schubert  */
test_key_update_local_in_read(int tst)6817e0c4386eSCy Schubert static int test_key_update_local_in_read(int tst)
6818e0c4386eSCy Schubert {
6819e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6820e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6821e0c4386eSCy Schubert     int testresult = 0;
6822e0c4386eSCy Schubert     char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
6823e0c4386eSCy Schubert     static char *mess = "A test message";
6824e0c4386eSCy Schubert     BIO *lbio = NULL, *pbio = NULL;
6825e0c4386eSCy Schubert     SSL *local = NULL, *peer = NULL;
6826e0c4386eSCy Schubert 
6827e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6828e0c4386eSCy Schubert                                               TLS_client_method(),
6829e0c4386eSCy Schubert                                               TLS1_3_VERSION,
6830e0c4386eSCy Schubert                                               0,
6831e0c4386eSCy Schubert                                               &sctx, &cctx, cert, privkey))
6832e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6833e0c4386eSCy Schubert                                              NULL, NULL))
6834e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6835e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6836e0c4386eSCy Schubert         goto end;
6837e0c4386eSCy Schubert 
6838e0c4386eSCy Schubert     local = tst == 0 ? clientssl : serverssl;
6839e0c4386eSCy Schubert     peer = tst == 0 ? serverssl : clientssl;
6840e0c4386eSCy Schubert 
6841e0c4386eSCy Schubert     if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6842e0c4386eSCy Schubert         goto end;
6843e0c4386eSCy Schubert 
6844e0c4386eSCy Schubert     SSL_set_bio(local, lbio, lbio);
6845e0c4386eSCy Schubert     SSL_set_bio(peer, pbio, pbio);
6846e0c4386eSCy Schubert 
6847e0c4386eSCy Schubert     /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
6848e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
6849e0c4386eSCy Schubert         || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
6850e0c4386eSCy Schubert         goto end;
6851e0c4386eSCy Schubert 
6852e0c4386eSCy Schubert     /* read appdata in local will fail with SSL_ERROR_WANT_READ */
6853e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
6854e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
6855e0c4386eSCy Schubert         goto end;
6856e0c4386eSCy Schubert 
6857e0c4386eSCy Schubert     /* SSL_do_handshake will send keyupdate msg */
6858e0c4386eSCy Schubert     if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6859e0c4386eSCy Schubert             || !TEST_int_eq(SSL_do_handshake(local), 1))
6860e0c4386eSCy Schubert         goto end;
6861e0c4386eSCy Schubert 
6862e0c4386eSCy Schubert     /*
6863e0c4386eSCy Schubert      * write data in peer previously that we will complete
6864e0c4386eSCy Schubert      * read data in local previously that we will complete
6865e0c4386eSCy Schubert      */
6866e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
6867e0c4386eSCy Schubert         || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
6868e0c4386eSCy Schubert         goto end;
6869e0c4386eSCy Schubert 
6870e0c4386eSCy Schubert     /*
6871e0c4386eSCy Schubert      * write data in local
6872e0c4386eSCy Schubert      * read data in peer - we will read the key update
6873e0c4386eSCy Schubert      */
6874e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6875e0c4386eSCy Schubert         || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6876e0c4386eSCy Schubert         goto end;
6877e0c4386eSCy Schubert 
6878e0c4386eSCy Schubert   /* Write more peer data to ensure we send the keyupdate message back */
6879e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6880e0c4386eSCy Schubert             || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
6881e0c4386eSCy Schubert         goto end;
6882e0c4386eSCy Schubert 
6883e0c4386eSCy Schubert     testresult = 1;
6884e0c4386eSCy Schubert 
6885e0c4386eSCy Schubert  end:
6886e0c4386eSCy Schubert     SSL_free(serverssl);
6887e0c4386eSCy Schubert     SSL_free(clientssl);
6888e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6889e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6890e0c4386eSCy Schubert 
6891e0c4386eSCy Schubert     return testresult;
6892e0c4386eSCy Schubert }
6893e0c4386eSCy Schubert #endif /* OSSL_NO_USABLE_TLS1_3 */
6894e0c4386eSCy Schubert 
test_ssl_clear(int idx)6895e0c4386eSCy Schubert static int test_ssl_clear(int idx)
6896e0c4386eSCy Schubert {
6897e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
6898e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
6899e0c4386eSCy Schubert     int testresult = 0;
6900e0c4386eSCy Schubert 
6901e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
6902e0c4386eSCy Schubert     if (idx == 1)
6903e0c4386eSCy Schubert         return 1;
6904e0c4386eSCy Schubert #endif
6905e0c4386eSCy Schubert 
6906e0c4386eSCy Schubert     /* Create an initial connection */
6907e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6908e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
6909e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
6910e0c4386eSCy Schubert             || (idx == 1
6911e0c4386eSCy Schubert                 && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
6912e0c4386eSCy Schubert                                                             TLS1_2_VERSION)))
6913e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6914e0c4386eSCy Schubert                                           &clientssl, NULL, NULL))
6915e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6916e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
6917e0c4386eSCy Schubert         goto end;
6918e0c4386eSCy Schubert 
6919e0c4386eSCy Schubert     SSL_shutdown(clientssl);
6920e0c4386eSCy Schubert     SSL_shutdown(serverssl);
6921e0c4386eSCy Schubert     SSL_free(serverssl);
6922e0c4386eSCy Schubert     serverssl = NULL;
6923e0c4386eSCy Schubert 
6924e0c4386eSCy Schubert     /* Clear clientssl - we're going to reuse the object */
6925e0c4386eSCy Schubert     if (!TEST_true(SSL_clear(clientssl)))
6926e0c4386eSCy Schubert         goto end;
6927e0c4386eSCy Schubert 
6928e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6929e0c4386eSCy Schubert                                              NULL, NULL))
6930e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
6931e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
6932e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl)))
6933e0c4386eSCy Schubert         goto end;
6934e0c4386eSCy Schubert 
6935e0c4386eSCy Schubert     SSL_shutdown(clientssl);
6936e0c4386eSCy Schubert     SSL_shutdown(serverssl);
6937e0c4386eSCy Schubert 
6938e0c4386eSCy Schubert     testresult = 1;
6939e0c4386eSCy Schubert 
6940e0c4386eSCy Schubert  end:
6941e0c4386eSCy Schubert     SSL_free(serverssl);
6942e0c4386eSCy Schubert     SSL_free(clientssl);
6943e0c4386eSCy Schubert     SSL_CTX_free(sctx);
6944e0c4386eSCy Schubert     SSL_CTX_free(cctx);
6945e0c4386eSCy Schubert 
6946e0c4386eSCy Schubert     return testresult;
6947e0c4386eSCy Schubert }
6948e0c4386eSCy Schubert 
6949e0c4386eSCy Schubert /* Parse CH and retrieve any MFL extension value if present */
get_MFL_from_client_hello(BIO * bio,int * mfl_codemfl_code)6950e0c4386eSCy Schubert static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
6951e0c4386eSCy Schubert {
6952e0c4386eSCy Schubert     long len;
6953e0c4386eSCy Schubert     unsigned char *data;
6954e0c4386eSCy Schubert     PACKET pkt, pkt2, pkt3;
6955e0c4386eSCy Schubert     unsigned int MFL_code = 0, type = 0;
6956e0c4386eSCy Schubert 
6957e0c4386eSCy Schubert     if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
6958e0c4386eSCy Schubert         goto end;
6959e0c4386eSCy Schubert 
6960e0c4386eSCy Schubert     memset(&pkt, 0, sizeof(pkt));
6961e0c4386eSCy Schubert     memset(&pkt2, 0, sizeof(pkt2));
6962e0c4386eSCy Schubert     memset(&pkt3, 0, sizeof(pkt3));
6963e0c4386eSCy Schubert 
6964e0c4386eSCy Schubert     if (!TEST_long_gt(len, 0)
6965e0c4386eSCy Schubert             || !TEST_true( PACKET_buf_init( &pkt, data, len ) )
6966e0c4386eSCy Schubert                /* Skip the record header */
6967e0c4386eSCy Schubert             || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
6968e0c4386eSCy Schubert                /* Skip the handshake message header */
6969e0c4386eSCy Schubert             || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
6970e0c4386eSCy Schubert                /* Skip client version and random */
6971e0c4386eSCy Schubert             || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
6972e0c4386eSCy Schubert                                                + SSL3_RANDOM_SIZE))
6973e0c4386eSCy Schubert                /* Skip session id */
6974e0c4386eSCy Schubert             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6975e0c4386eSCy Schubert                /* Skip ciphers */
6976e0c4386eSCy Schubert             || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
6977e0c4386eSCy Schubert                /* Skip compression */
6978e0c4386eSCy Schubert             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6979e0c4386eSCy Schubert                /* Extensions len */
6980e0c4386eSCy Schubert             || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6981e0c4386eSCy Schubert         goto end;
6982e0c4386eSCy Schubert 
6983e0c4386eSCy Schubert     /* Loop through all extensions */
6984e0c4386eSCy Schubert     while (PACKET_remaining(&pkt2)) {
6985e0c4386eSCy Schubert         if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
6986e0c4386eSCy Schubert                 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
6987e0c4386eSCy Schubert             goto end;
6988e0c4386eSCy Schubert 
6989e0c4386eSCy Schubert         if (type == TLSEXT_TYPE_max_fragment_length) {
6990e0c4386eSCy Schubert             if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
6991e0c4386eSCy Schubert                     || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
6992e0c4386eSCy Schubert                 goto end;
6993e0c4386eSCy Schubert 
6994e0c4386eSCy Schubert             *mfl_codemfl_code = MFL_code;
6995e0c4386eSCy Schubert             return 1;
6996e0c4386eSCy Schubert         }
6997e0c4386eSCy Schubert     }
6998e0c4386eSCy Schubert 
6999e0c4386eSCy Schubert  end:
7000e0c4386eSCy Schubert     return 0;
7001e0c4386eSCy Schubert }
7002e0c4386eSCy Schubert 
7003e0c4386eSCy Schubert /* Maximum-Fragment-Length TLS extension mode to test */
7004e0c4386eSCy Schubert static const unsigned char max_fragment_len_test[] = {
7005e0c4386eSCy Schubert     TLSEXT_max_fragment_length_512,
7006e0c4386eSCy Schubert     TLSEXT_max_fragment_length_1024,
7007e0c4386eSCy Schubert     TLSEXT_max_fragment_length_2048,
7008e0c4386eSCy Schubert     TLSEXT_max_fragment_length_4096
7009e0c4386eSCy Schubert };
7010e0c4386eSCy Schubert 
test_max_fragment_len_ext(int idx_tst)7011e0c4386eSCy Schubert static int test_max_fragment_len_ext(int idx_tst)
7012e0c4386eSCy Schubert {
7013e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
7014e0c4386eSCy Schubert     SSL *con = NULL;
7015e0c4386eSCy Schubert     int testresult = 0, MFL_mode = 0;
7016e0c4386eSCy Schubert     BIO *rbio, *wbio;
7017e0c4386eSCy Schubert 
7018e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
7019e0c4386eSCy Schubert                                        TLS1_VERSION, 0, NULL, &ctx, NULL,
7020e0c4386eSCy Schubert                                        NULL)))
7021e0c4386eSCy Schubert         return 0;
7022e0c4386eSCy Schubert 
7023e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
7024e0c4386eSCy Schubert                    ctx, max_fragment_len_test[idx_tst])))
7025e0c4386eSCy Schubert         goto end;
7026e0c4386eSCy Schubert 
7027e0c4386eSCy Schubert     con = SSL_new(ctx);
7028e0c4386eSCy Schubert     if (!TEST_ptr(con))
7029e0c4386eSCy Schubert         goto end;
7030e0c4386eSCy Schubert 
7031e0c4386eSCy Schubert     rbio = BIO_new(BIO_s_mem());
7032e0c4386eSCy Schubert     wbio = BIO_new(BIO_s_mem());
7033e0c4386eSCy Schubert     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
7034e0c4386eSCy Schubert         BIO_free(rbio);
7035e0c4386eSCy Schubert         BIO_free(wbio);
7036e0c4386eSCy Schubert         goto end;
7037e0c4386eSCy Schubert     }
7038e0c4386eSCy Schubert 
7039e0c4386eSCy Schubert     SSL_set_bio(con, rbio, wbio);
7040e0c4386eSCy Schubert 
7041e0c4386eSCy Schubert     if (!TEST_int_le(SSL_connect(con), 0)) {
7042e0c4386eSCy Schubert         /* This shouldn't succeed because we don't have a server! */
7043e0c4386eSCy Schubert         goto end;
7044e0c4386eSCy Schubert     }
7045e0c4386eSCy Schubert 
7046e0c4386eSCy Schubert     if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
7047e0c4386eSCy Schubert         /* no MFL in client hello */
7048e0c4386eSCy Schubert         goto end;
7049e0c4386eSCy Schubert     if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
7050e0c4386eSCy Schubert         goto end;
7051e0c4386eSCy Schubert 
7052e0c4386eSCy Schubert     testresult = 1;
7053e0c4386eSCy Schubert 
7054e0c4386eSCy Schubert end:
7055e0c4386eSCy Schubert     SSL_free(con);
7056e0c4386eSCy Schubert     SSL_CTX_free(ctx);
7057e0c4386eSCy Schubert 
7058e0c4386eSCy Schubert     return testresult;
7059e0c4386eSCy Schubert }
7060e0c4386eSCy Schubert 
7061e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
test_pha_key_update(void)7062e0c4386eSCy Schubert static int test_pha_key_update(void)
7063e0c4386eSCy Schubert {
7064e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
7065e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
7066e0c4386eSCy Schubert     int testresult = 0;
7067e0c4386eSCy Schubert 
7068e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7069e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
7070e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
7071e0c4386eSCy Schubert         return 0;
7072e0c4386eSCy Schubert 
7073e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
7074e0c4386eSCy Schubert         || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
7075e0c4386eSCy Schubert         || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
7076e0c4386eSCy Schubert         || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
7077e0c4386eSCy Schubert         goto end;
7078e0c4386eSCy Schubert 
7079e0c4386eSCy Schubert     SSL_CTX_set_post_handshake_auth(cctx, 1);
7080e0c4386eSCy Schubert 
7081e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7082e0c4386eSCy Schubert                                       NULL, NULL)))
7083e0c4386eSCy Schubert         goto end;
7084e0c4386eSCy Schubert 
7085e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7086e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
7087e0c4386eSCy Schubert         goto end;
7088e0c4386eSCy Schubert 
7089e0c4386eSCy Schubert     SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
7090e0c4386eSCy Schubert     if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
7091e0c4386eSCy Schubert         goto end;
7092e0c4386eSCy Schubert 
7093e0c4386eSCy Schubert     if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
7094e0c4386eSCy Schubert         goto end;
7095e0c4386eSCy Schubert 
7096e0c4386eSCy Schubert     /* Start handshake on the server */
7097e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
7098e0c4386eSCy Schubert         goto end;
7099e0c4386eSCy Schubert 
7100e0c4386eSCy Schubert     /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
7101e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7102e0c4386eSCy Schubert                                          SSL_ERROR_NONE)))
7103e0c4386eSCy Schubert         goto end;
7104e0c4386eSCy Schubert 
7105e0c4386eSCy Schubert     SSL_shutdown(clientssl);
7106e0c4386eSCy Schubert     SSL_shutdown(serverssl);
7107e0c4386eSCy Schubert 
7108e0c4386eSCy Schubert     testresult = 1;
7109e0c4386eSCy Schubert 
7110e0c4386eSCy Schubert  end:
7111e0c4386eSCy Schubert     SSL_free(serverssl);
7112e0c4386eSCy Schubert     SSL_free(clientssl);
7113e0c4386eSCy Schubert     SSL_CTX_free(sctx);
7114e0c4386eSCy Schubert     SSL_CTX_free(cctx);
7115e0c4386eSCy Schubert     return testresult;
7116e0c4386eSCy Schubert }
7117e0c4386eSCy Schubert #endif
7118e0c4386eSCy Schubert 
7119e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
7120e0c4386eSCy Schubert 
7121e0c4386eSCy Schubert static SRP_VBASE *vbase = NULL;
7122e0c4386eSCy Schubert 
ssl_srp_cb(SSL * s,int * ad,void * arg)7123e0c4386eSCy Schubert static int ssl_srp_cb(SSL *s, int *ad, void *arg)
7124e0c4386eSCy Schubert {
7125e0c4386eSCy Schubert     int ret = SSL3_AL_FATAL;
7126e0c4386eSCy Schubert     char *username;
7127e0c4386eSCy Schubert     SRP_user_pwd *user = NULL;
7128e0c4386eSCy Schubert 
7129e0c4386eSCy Schubert     username = SSL_get_srp_username(s);
7130e0c4386eSCy Schubert     if (username == NULL) {
7131e0c4386eSCy Schubert         *ad = SSL_AD_INTERNAL_ERROR;
7132e0c4386eSCy Schubert         goto err;
7133e0c4386eSCy Schubert     }
7134e0c4386eSCy Schubert 
7135e0c4386eSCy Schubert     user = SRP_VBASE_get1_by_user(vbase, username);
7136e0c4386eSCy Schubert     if (user == NULL) {
7137e0c4386eSCy Schubert         *ad = SSL_AD_INTERNAL_ERROR;
7138e0c4386eSCy Schubert         goto err;
7139e0c4386eSCy Schubert     }
7140e0c4386eSCy Schubert 
7141e0c4386eSCy Schubert     if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
7142e0c4386eSCy Schubert                                  user->info) <= 0) {
7143e0c4386eSCy Schubert         *ad = SSL_AD_INTERNAL_ERROR;
7144e0c4386eSCy Schubert         goto err;
7145e0c4386eSCy Schubert     }
7146e0c4386eSCy Schubert 
7147e0c4386eSCy Schubert     ret = 0;
7148e0c4386eSCy Schubert 
7149e0c4386eSCy Schubert  err:
7150e0c4386eSCy Schubert     SRP_user_pwd_free(user);
7151e0c4386eSCy Schubert     return ret;
7152e0c4386eSCy Schubert }
7153e0c4386eSCy Schubert 
create_new_vfile(char * userid,char * password,const char * filename)7154e0c4386eSCy Schubert static int create_new_vfile(char *userid, char *password, const char *filename)
7155e0c4386eSCy Schubert {
7156e0c4386eSCy Schubert     char *gNid = NULL;
7157e0c4386eSCy Schubert     OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
7158e0c4386eSCy Schubert     TXT_DB *db = NULL;
7159e0c4386eSCy Schubert     int ret = 0;
7160e0c4386eSCy Schubert     BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
7161e0c4386eSCy Schubert     size_t i;
7162e0c4386eSCy Schubert 
7163e0c4386eSCy Schubert     if (!TEST_ptr(dummy) || !TEST_ptr(row))
7164e0c4386eSCy Schubert         goto end;
7165e0c4386eSCy Schubert 
7166e0c4386eSCy Schubert     gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
7167e0c4386eSCy Schubert                                   &row[DB_srpverifier], NULL, NULL, libctx, NULL);
7168e0c4386eSCy Schubert     if (!TEST_ptr(gNid))
7169e0c4386eSCy Schubert         goto end;
7170e0c4386eSCy Schubert 
7171e0c4386eSCy Schubert     /*
7172e0c4386eSCy Schubert      * The only way to create an empty TXT_DB is to provide a BIO with no data
7173e0c4386eSCy Schubert      * in it!
7174e0c4386eSCy Schubert      */
7175e0c4386eSCy Schubert     db = TXT_DB_read(dummy, DB_NUMBER);
7176e0c4386eSCy Schubert     if (!TEST_ptr(db))
7177e0c4386eSCy Schubert         goto end;
7178e0c4386eSCy Schubert 
7179e0c4386eSCy Schubert     out = BIO_new_file(filename, "w");
7180e0c4386eSCy Schubert     if (!TEST_ptr(out))
7181e0c4386eSCy Schubert         goto end;
7182e0c4386eSCy Schubert 
7183e0c4386eSCy Schubert     row[DB_srpid] = OPENSSL_strdup(userid);
7184e0c4386eSCy Schubert     row[DB_srptype] = OPENSSL_strdup("V");
7185e0c4386eSCy Schubert     row[DB_srpgN] = OPENSSL_strdup(gNid);
7186e0c4386eSCy Schubert 
7187e0c4386eSCy Schubert     if (!TEST_ptr(row[DB_srpid])
7188e0c4386eSCy Schubert             || !TEST_ptr(row[DB_srptype])
7189e0c4386eSCy Schubert             || !TEST_ptr(row[DB_srpgN])
7190e0c4386eSCy Schubert             || !TEST_true(TXT_DB_insert(db, row)))
7191e0c4386eSCy Schubert         goto end;
7192e0c4386eSCy Schubert 
7193e0c4386eSCy Schubert     row = NULL;
7194e0c4386eSCy Schubert 
7195e0c4386eSCy Schubert     if (TXT_DB_write(out, db) <= 0)
7196e0c4386eSCy Schubert         goto end;
7197e0c4386eSCy Schubert 
7198e0c4386eSCy Schubert     ret = 1;
7199e0c4386eSCy Schubert  end:
7200e0c4386eSCy Schubert     if (row != NULL) {
7201e0c4386eSCy Schubert         for (i = 0; i < DB_NUMBER; i++)
7202e0c4386eSCy Schubert             OPENSSL_free(row[i]);
7203e0c4386eSCy Schubert     }
7204e0c4386eSCy Schubert     OPENSSL_free(row);
7205e0c4386eSCy Schubert     BIO_free(dummy);
7206e0c4386eSCy Schubert     BIO_free(out);
7207e0c4386eSCy Schubert     TXT_DB_free(db);
7208e0c4386eSCy Schubert 
7209e0c4386eSCy Schubert     return ret;
7210e0c4386eSCy Schubert }
7211e0c4386eSCy Schubert 
create_new_vbase(char * userid,char * password)7212e0c4386eSCy Schubert static int create_new_vbase(char *userid, char *password)
7213e0c4386eSCy Schubert {
7214e0c4386eSCy Schubert     BIGNUM *verifier = NULL, *salt = NULL;
7215e0c4386eSCy Schubert     const SRP_gN *lgN = NULL;
7216e0c4386eSCy Schubert     SRP_user_pwd *user_pwd = NULL;
7217e0c4386eSCy Schubert     int ret = 0;
7218e0c4386eSCy Schubert 
7219e0c4386eSCy Schubert     lgN = SRP_get_default_gN(NULL);
7220e0c4386eSCy Schubert     if (!TEST_ptr(lgN))
7221e0c4386eSCy Schubert         goto end;
7222e0c4386eSCy Schubert 
7223e0c4386eSCy Schubert     if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
7224e0c4386eSCy Schubert                                              lgN->N, lgN->g, libctx, NULL)))
7225e0c4386eSCy Schubert         goto end;
7226e0c4386eSCy Schubert 
7227e0c4386eSCy Schubert     user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
7228e0c4386eSCy Schubert     if (!TEST_ptr(user_pwd))
7229e0c4386eSCy Schubert         goto end;
7230e0c4386eSCy Schubert 
7231e0c4386eSCy Schubert     user_pwd->N = lgN->N;
7232e0c4386eSCy Schubert     user_pwd->g = lgN->g;
7233e0c4386eSCy Schubert     user_pwd->id = OPENSSL_strdup(userid);
7234e0c4386eSCy Schubert     if (!TEST_ptr(user_pwd->id))
7235e0c4386eSCy Schubert         goto end;
7236e0c4386eSCy Schubert 
7237e0c4386eSCy Schubert     user_pwd->v = verifier;
7238e0c4386eSCy Schubert     user_pwd->s = salt;
7239e0c4386eSCy Schubert     verifier = salt = NULL;
7240e0c4386eSCy Schubert 
7241e0c4386eSCy Schubert     if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
7242e0c4386eSCy Schubert         goto end;
7243e0c4386eSCy Schubert     user_pwd = NULL;
7244e0c4386eSCy Schubert 
7245e0c4386eSCy Schubert     ret = 1;
7246e0c4386eSCy Schubert end:
7247e0c4386eSCy Schubert     SRP_user_pwd_free(user_pwd);
7248e0c4386eSCy Schubert     BN_free(salt);
7249e0c4386eSCy Schubert     BN_free(verifier);
7250e0c4386eSCy Schubert 
7251e0c4386eSCy Schubert     return ret;
7252e0c4386eSCy Schubert }
7253e0c4386eSCy Schubert 
7254e0c4386eSCy Schubert /*
7255e0c4386eSCy Schubert  * SRP tests
7256e0c4386eSCy Schubert  *
7257e0c4386eSCy Schubert  * Test 0: Simple successful SRP connection, new vbase
7258e0c4386eSCy Schubert  * Test 1: Connection failure due to bad password, new vbase
7259e0c4386eSCy Schubert  * Test 2: Simple successful SRP connection, vbase loaded from existing file
7260e0c4386eSCy Schubert  * Test 3: Connection failure due to bad password, vbase loaded from existing
7261e0c4386eSCy Schubert  *         file
7262e0c4386eSCy Schubert  * Test 4: Simple successful SRP connection, vbase loaded from new file
7263e0c4386eSCy Schubert  * Test 5: Connection failure due to bad password, vbase loaded from new file
7264e0c4386eSCy Schubert  */
test_srp(int tst)7265e0c4386eSCy Schubert static int test_srp(int tst)
7266e0c4386eSCy Schubert {
7267e0c4386eSCy Schubert     char *userid = "test", *password = "password", *tstsrpfile;
7268e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
7269e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
7270e0c4386eSCy Schubert     int ret, testresult = 0;
7271e0c4386eSCy Schubert 
7272e0c4386eSCy Schubert     vbase = SRP_VBASE_new(NULL);
7273e0c4386eSCy Schubert     if (!TEST_ptr(vbase))
7274e0c4386eSCy Schubert         goto end;
7275e0c4386eSCy Schubert 
7276e0c4386eSCy Schubert     if (tst == 0 || tst == 1) {
7277e0c4386eSCy Schubert         if (!TEST_true(create_new_vbase(userid, password)))
7278e0c4386eSCy Schubert             goto end;
7279e0c4386eSCy Schubert     } else {
7280e0c4386eSCy Schubert         if (tst == 4 || tst == 5) {
7281e0c4386eSCy Schubert             if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
7282e0c4386eSCy Schubert                 goto end;
7283e0c4386eSCy Schubert             tstsrpfile = tmpfilename;
7284e0c4386eSCy Schubert         } else {
7285e0c4386eSCy Schubert             tstsrpfile = srpvfile;
7286e0c4386eSCy Schubert         }
7287e0c4386eSCy Schubert         if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
7288e0c4386eSCy Schubert             goto end;
7289e0c4386eSCy Schubert     }
7290e0c4386eSCy Schubert 
7291e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7292e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_VERSION, 0,
7293e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
7294e0c4386eSCy Schubert         goto end;
7295e0c4386eSCy Schubert 
7296e0c4386eSCy Schubert     if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
7297e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
7298e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
7299e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
7300e0c4386eSCy Schubert             || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
7301e0c4386eSCy Schubert         goto end;
7302e0c4386eSCy Schubert 
7303e0c4386eSCy Schubert     if (tst % 2 == 1) {
7304e0c4386eSCy Schubert         if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
7305e0c4386eSCy Schubert             goto end;
7306e0c4386eSCy Schubert     } else {
7307e0c4386eSCy Schubert         if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
7308e0c4386eSCy Schubert             goto end;
7309e0c4386eSCy Schubert     }
7310e0c4386eSCy Schubert 
7311e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7312e0c4386eSCy Schubert                                       NULL, NULL)))
7313e0c4386eSCy Schubert         goto end;
7314e0c4386eSCy Schubert 
7315e0c4386eSCy Schubert     ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
7316e0c4386eSCy Schubert     if (ret) {
7317e0c4386eSCy Schubert         if (!TEST_true(tst % 2 == 0))
7318e0c4386eSCy Schubert             goto end;
7319e0c4386eSCy Schubert     } else {
7320e0c4386eSCy Schubert         if (!TEST_true(tst % 2 == 1))
7321e0c4386eSCy Schubert             goto end;
7322e0c4386eSCy Schubert     }
7323e0c4386eSCy Schubert 
7324e0c4386eSCy Schubert     testresult = 1;
7325e0c4386eSCy Schubert 
7326e0c4386eSCy Schubert  end:
7327e0c4386eSCy Schubert     SRP_VBASE_free(vbase);
7328e0c4386eSCy Schubert     vbase = NULL;
7329e0c4386eSCy Schubert     SSL_free(serverssl);
7330e0c4386eSCy Schubert     SSL_free(clientssl);
7331e0c4386eSCy Schubert     SSL_CTX_free(sctx);
7332e0c4386eSCy Schubert     SSL_CTX_free(cctx);
7333e0c4386eSCy Schubert 
7334e0c4386eSCy Schubert     return testresult;
7335e0c4386eSCy Schubert }
7336e0c4386eSCy Schubert #endif
7337e0c4386eSCy Schubert 
7338e0c4386eSCy Schubert static int info_cb_failed = 0;
7339e0c4386eSCy Schubert static int info_cb_offset = 0;
7340e0c4386eSCy Schubert static int info_cb_this_state = -1;
7341e0c4386eSCy Schubert 
7342e0c4386eSCy Schubert static struct info_cb_states_st {
7343e0c4386eSCy Schubert     int where;
7344e0c4386eSCy Schubert     const char *statestr;
7345e0c4386eSCy Schubert } info_cb_states[][60] = {
7346e0c4386eSCy Schubert     {
7347e0c4386eSCy Schubert         /* TLSv1.2 server followed by resumption */
7348e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7349e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7350e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
7351e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
7352e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
7353e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7354e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7355e0c4386eSCy Schubert         {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7356e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
7357e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7358e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
7359e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7360e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {0, NULL},
7361e0c4386eSCy Schubert     }, {
7362e0c4386eSCy Schubert         /* TLSv1.2 client followed by resumption */
7363e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7364e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7365e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
7366e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
7367e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
7368e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7369e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7370e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7371e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7372e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7373e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCCS"},  {SSL_CB_LOOP, "TWFIN"},
7374e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
7375e0c4386eSCy Schubert     }, {
7376e0c4386eSCy Schubert         /* TLSv1.3 server followed by resumption */
7377e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7378e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7379e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
7380e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
7381e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
7382e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7383e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7384e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7385e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7386e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7387e0c4386eSCy Schubert         {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
7388e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7389e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7390e0c4386eSCy Schubert     }, {
7391e0c4386eSCy Schubert         /* TLSv1.3 client followed by resumption */
7392e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7393e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7394e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
7395e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
7396e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWFIN"},  {SSL_CB_HANDSHAKE_DONE, NULL},
7397e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7398e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
7399e0c4386eSCy Schubert         {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
7400e0c4386eSCy Schubert         {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7401e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
7402e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"},  {SSL_CB_LOOP, "TREE"},
7403e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7404e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7405e0c4386eSCy Schubert         {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
7406e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {0, NULL},
7407e0c4386eSCy Schubert     }, {
7408e0c4386eSCy Schubert         /* TLSv1.3 server, early_data */
7409e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7410e0c4386eSCy Schubert         {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7411e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7412e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7413e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7414e0c4386eSCy Schubert         {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
7415e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7416e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {0, NULL},
7417e0c4386eSCy Schubert     }, {
7418e0c4386eSCy Schubert         /* TLSv1.3 client, early_data */
7419e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7420e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
7421e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7422e0c4386eSCy Schubert         {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7423e0c4386eSCy Schubert         {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7424e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
7425e0c4386eSCy Schubert         {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7426e0c4386eSCy Schubert         {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7427e0c4386eSCy Schubert         {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7428e0c4386eSCy Schubert     }, {
7429e0c4386eSCy Schubert         {0, NULL},
7430e0c4386eSCy Schubert     }
7431e0c4386eSCy Schubert };
7432e0c4386eSCy Schubert 
sslapi_info_callback(const SSL * s,int where,int ret)7433e0c4386eSCy Schubert static void sslapi_info_callback(const SSL *s, int where, int ret)
7434e0c4386eSCy Schubert {
7435e0c4386eSCy Schubert     struct info_cb_states_st *state = info_cb_states[info_cb_offset];
7436e0c4386eSCy Schubert 
7437e0c4386eSCy Schubert     /* We do not ever expect a connection to fail in this test */
7438e0c4386eSCy Schubert     if (!TEST_false(ret == 0)) {
7439e0c4386eSCy Schubert         info_cb_failed = 1;
7440e0c4386eSCy Schubert         return;
7441e0c4386eSCy Schubert     }
7442e0c4386eSCy Schubert 
7443e0c4386eSCy Schubert     /*
7444e0c4386eSCy Schubert      * Do some sanity checks. We never expect these things to happen in this
7445e0c4386eSCy Schubert      * test
7446e0c4386eSCy Schubert      */
7447e0c4386eSCy Schubert     if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
7448e0c4386eSCy Schubert             || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
7449e0c4386eSCy Schubert             || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
7450e0c4386eSCy Schubert         info_cb_failed = 1;
7451e0c4386eSCy Schubert         return;
7452e0c4386eSCy Schubert     }
7453e0c4386eSCy Schubert 
7454e0c4386eSCy Schubert     /* Now check we're in the right state */
7455e0c4386eSCy Schubert     if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
7456e0c4386eSCy Schubert         info_cb_failed = 1;
7457e0c4386eSCy Schubert         return;
7458e0c4386eSCy Schubert     }
7459e0c4386eSCy Schubert     if ((where & SSL_CB_LOOP) != 0
7460e0c4386eSCy Schubert             && !TEST_int_eq(strcmp(SSL_state_string(s),
7461e0c4386eSCy Schubert                             state[info_cb_this_state].statestr), 0)) {
7462e0c4386eSCy Schubert         info_cb_failed = 1;
7463e0c4386eSCy Schubert         return;
7464e0c4386eSCy Schubert     }
7465e0c4386eSCy Schubert 
7466e0c4386eSCy Schubert     /*
7467e0c4386eSCy Schubert      * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
7468e0c4386eSCy Schubert      */
7469e0c4386eSCy Schubert     if ((where & SSL_CB_HANDSHAKE_DONE)
7470e0c4386eSCy Schubert             && SSL_in_init((SSL *)s) != 0) {
7471e0c4386eSCy Schubert         info_cb_failed = 1;
7472e0c4386eSCy Schubert         return;
7473e0c4386eSCy Schubert     }
7474e0c4386eSCy Schubert }
7475e0c4386eSCy Schubert 
7476e0c4386eSCy Schubert /*
7477e0c4386eSCy Schubert  * Test the info callback gets called when we expect it to.
7478e0c4386eSCy Schubert  *
7479e0c4386eSCy Schubert  * Test 0: TLSv1.2, server
7480e0c4386eSCy Schubert  * Test 1: TLSv1.2, client
7481e0c4386eSCy Schubert  * Test 2: TLSv1.3, server
7482e0c4386eSCy Schubert  * Test 3: TLSv1.3, client
7483e0c4386eSCy Schubert  * Test 4: TLSv1.3, server, early_data
7484e0c4386eSCy Schubert  * Test 5: TLSv1.3, client, early_data
7485e0c4386eSCy Schubert  */
test_info_callback(int tst)7486e0c4386eSCy Schubert static int test_info_callback(int tst)
7487e0c4386eSCy Schubert {
7488e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
7489e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
7490e0c4386eSCy Schubert     SSL_SESSION *clntsess = NULL;
7491e0c4386eSCy Schubert     int testresult = 0;
7492e0c4386eSCy Schubert     int tlsvers;
7493e0c4386eSCy Schubert 
7494e0c4386eSCy Schubert     if (tst < 2) {
7495e0c4386eSCy Schubert /* We need either ECDHE or DHE for the TLSv1.2 test to work */
7496e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
7497e0c4386eSCy Schubert                                     || !defined(OPENSSL_NO_DH))
7498e0c4386eSCy Schubert         tlsvers = TLS1_2_VERSION;
7499e0c4386eSCy Schubert #else
7500e0c4386eSCy Schubert         return 1;
7501e0c4386eSCy Schubert #endif
7502e0c4386eSCy Schubert     } else {
7503e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
7504e0c4386eSCy Schubert         tlsvers = TLS1_3_VERSION;
7505e0c4386eSCy Schubert #else
7506e0c4386eSCy Schubert         return 1;
7507e0c4386eSCy Schubert #endif
7508e0c4386eSCy Schubert     }
7509e0c4386eSCy Schubert 
7510e0c4386eSCy Schubert     /* Reset globals */
7511e0c4386eSCy Schubert     info_cb_failed = 0;
7512e0c4386eSCy Schubert     info_cb_this_state = -1;
7513e0c4386eSCy Schubert     info_cb_offset = tst;
7514e0c4386eSCy Schubert 
7515e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
7516e0c4386eSCy Schubert     if (tst >= 4) {
7517e0c4386eSCy Schubert         SSL_SESSION *sess = NULL;
7518e0c4386eSCy Schubert         size_t written, readbytes;
7519e0c4386eSCy Schubert         unsigned char buf[80];
752044096ebdSEnji Cooper         time_t timer;
7521e0c4386eSCy Schubert 
7522e0c4386eSCy Schubert         /* early_data tests */
7523e0c4386eSCy Schubert         if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
7524e0c4386eSCy Schubert                                             &serverssl, &sess, 0,
7525e0c4386eSCy Schubert                                             SHA384_DIGEST_LENGTH)))
7526e0c4386eSCy Schubert             goto end;
7527e0c4386eSCy Schubert 
7528e0c4386eSCy Schubert         /* We don't actually need this reference */
7529e0c4386eSCy Schubert         SSL_SESSION_free(sess);
7530e0c4386eSCy Schubert 
7531e0c4386eSCy Schubert         SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
7532e0c4386eSCy Schubert                               sslapi_info_callback);
7533e0c4386eSCy Schubert 
7534e0c4386eSCy Schubert         /* Write and read some early data and then complete the connection */
753544096ebdSEnji Cooper         timer = time(NULL);
7536e0c4386eSCy Schubert         if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
7537e0c4386eSCy Schubert                                             &written))
753844096ebdSEnji Cooper                 || !TEST_size_t_eq(written, strlen(MSG1)))
753944096ebdSEnji Cooper             goto end;
754044096ebdSEnji Cooper 
754144096ebdSEnji Cooper         if (!TEST_int_eq(SSL_read_early_data(serverssl, buf,
7542e0c4386eSCy Schubert                                              sizeof(buf), &readbytes),
754344096ebdSEnji Cooper                          SSL_READ_EARLY_DATA_SUCCESS)) {
754444096ebdSEnji Cooper             testresult = check_early_data_timeout(timer);
754544096ebdSEnji Cooper             goto end;
754644096ebdSEnji Cooper         }
754744096ebdSEnji Cooper 
754844096ebdSEnji Cooper         if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
7549e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_early_data_status(serverssl),
7550e0c4386eSCy Schubert                                 SSL_EARLY_DATA_ACCEPTED)
7551e0c4386eSCy Schubert                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
7552e0c4386eSCy Schubert                                                     SSL_ERROR_NONE))
7553e0c4386eSCy Schubert                 || !TEST_false(info_cb_failed))
7554e0c4386eSCy Schubert             goto end;
7555e0c4386eSCy Schubert 
7556e0c4386eSCy Schubert         testresult = 1;
7557e0c4386eSCy Schubert         goto end;
7558e0c4386eSCy Schubert     }
7559e0c4386eSCy Schubert #endif
7560e0c4386eSCy Schubert 
7561e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7562e0c4386eSCy Schubert                                        TLS_client_method(),
7563e0c4386eSCy Schubert                                        tlsvers, tlsvers, &sctx, &cctx, cert,
7564e0c4386eSCy Schubert                                        privkey)))
7565e0c4386eSCy Schubert         goto end;
7566e0c4386eSCy Schubert 
7567e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
7568e0c4386eSCy Schubert         goto end;
7569e0c4386eSCy Schubert 
7570e0c4386eSCy Schubert     /*
7571e0c4386eSCy Schubert      * For even numbered tests we check the server callbacks. For odd numbers we
7572e0c4386eSCy Schubert      * check the client.
7573e0c4386eSCy Schubert      */
7574e0c4386eSCy Schubert     SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
7575e0c4386eSCy Schubert                               sslapi_info_callback);
7576e0c4386eSCy Schubert 
7577e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
7578e0c4386eSCy Schubert                                           &clientssl, NULL, NULL))
7579e0c4386eSCy Schubert         || !TEST_true(create_ssl_connection(serverssl, clientssl,
7580e0c4386eSCy Schubert                                             SSL_ERROR_NONE))
7581e0c4386eSCy Schubert         || !TEST_false(info_cb_failed))
7582e0c4386eSCy Schubert     goto end;
7583e0c4386eSCy Schubert 
7584e0c4386eSCy Schubert 
7585e0c4386eSCy Schubert 
7586e0c4386eSCy Schubert     clntsess = SSL_get1_session(clientssl);
7587e0c4386eSCy Schubert     SSL_shutdown(clientssl);
7588e0c4386eSCy Schubert     SSL_shutdown(serverssl);
7589e0c4386eSCy Schubert     SSL_free(serverssl);
7590e0c4386eSCy Schubert     SSL_free(clientssl);
7591e0c4386eSCy Schubert     serverssl = clientssl = NULL;
7592e0c4386eSCy Schubert 
7593e0c4386eSCy Schubert     /* Now do a resumption */
7594e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7595e0c4386eSCy Schubert                                       NULL))
7596e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, clntsess))
7597e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
7598e0c4386eSCy Schubert                                                 SSL_ERROR_NONE))
7599e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(clientssl))
7600e0c4386eSCy Schubert             || !TEST_false(info_cb_failed))
7601e0c4386eSCy Schubert         goto end;
7602e0c4386eSCy Schubert 
7603e0c4386eSCy Schubert     testresult = 1;
7604e0c4386eSCy Schubert 
7605e0c4386eSCy Schubert  end:
7606e0c4386eSCy Schubert     SSL_free(serverssl);
7607e0c4386eSCy Schubert     SSL_free(clientssl);
7608e0c4386eSCy Schubert     SSL_SESSION_free(clntsess);
7609e0c4386eSCy Schubert     SSL_CTX_free(sctx);
7610e0c4386eSCy Schubert     SSL_CTX_free(cctx);
7611e0c4386eSCy Schubert     return testresult;
7612e0c4386eSCy Schubert }
7613e0c4386eSCy Schubert 
test_ssl_pending(int tst)7614e0c4386eSCy Schubert static int test_ssl_pending(int tst)
7615e0c4386eSCy Schubert {
7616e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
7617e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
7618e0c4386eSCy Schubert     int testresult = 0;
7619e0c4386eSCy Schubert     char msg[] = "A test message";
7620e0c4386eSCy Schubert     char buf[5];
7621e0c4386eSCy Schubert     size_t written, readbytes;
7622e0c4386eSCy Schubert 
7623e0c4386eSCy Schubert     if (tst == 0) {
7624e0c4386eSCy Schubert         if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7625e0c4386eSCy Schubert                                            TLS_client_method(),
7626e0c4386eSCy Schubert                                            TLS1_VERSION, 0,
7627e0c4386eSCy Schubert                                            &sctx, &cctx, cert, privkey)))
7628e0c4386eSCy Schubert             goto end;
7629e0c4386eSCy Schubert     } else {
7630e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
7631e0c4386eSCy Schubert         if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
7632e0c4386eSCy Schubert                                            DTLS_client_method(),
7633e0c4386eSCy Schubert                                            DTLS1_VERSION, 0,
7634e0c4386eSCy Schubert                                            &sctx, &cctx, cert, privkey)))
7635e0c4386eSCy Schubert             goto end;
7636e0c4386eSCy Schubert 
7637e0c4386eSCy Schubert # ifdef OPENSSL_NO_DTLS1_2
7638e0c4386eSCy Schubert         /* Not supported in the FIPS provider */
7639e0c4386eSCy Schubert         if (is_fips) {
7640e0c4386eSCy Schubert             testresult = 1;
7641e0c4386eSCy Schubert             goto end;
7642e0c4386eSCy Schubert         };
7643e0c4386eSCy Schubert         /*
7644e0c4386eSCy Schubert          * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
7645e0c4386eSCy Schubert          * level 0
7646e0c4386eSCy Schubert          */
7647e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
7648e0c4386eSCy Schubert                 || !TEST_true(SSL_CTX_set_cipher_list(cctx,
7649e0c4386eSCy Schubert                                                     "DEFAULT:@SECLEVEL=0")))
7650e0c4386eSCy Schubert             goto end;
7651e0c4386eSCy Schubert # endif
7652e0c4386eSCy Schubert #else
7653e0c4386eSCy Schubert         return 1;
7654e0c4386eSCy Schubert #endif
7655e0c4386eSCy Schubert     }
7656e0c4386eSCy Schubert 
7657e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7658e0c4386eSCy Schubert                                              NULL, NULL))
7659e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
7660e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
7661e0c4386eSCy Schubert         goto end;
7662e0c4386eSCy Schubert 
7663e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_pending(clientssl), 0)
7664e0c4386eSCy Schubert             || !TEST_false(SSL_has_pending(clientssl))
7665e0c4386eSCy Schubert             || !TEST_int_eq(SSL_pending(serverssl), 0)
7666e0c4386eSCy Schubert             || !TEST_false(SSL_has_pending(serverssl))
7667e0c4386eSCy Schubert             || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
7668e0c4386eSCy Schubert             || !TEST_size_t_eq(written, sizeof(msg))
7669e0c4386eSCy Schubert             || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
7670e0c4386eSCy Schubert             || !TEST_size_t_eq(readbytes, sizeof(buf))
7671e0c4386eSCy Schubert             || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
7672e0c4386eSCy Schubert             || !TEST_true(SSL_has_pending(clientssl)))
7673e0c4386eSCy Schubert         goto end;
7674e0c4386eSCy Schubert 
7675e0c4386eSCy Schubert     testresult = 1;
7676e0c4386eSCy Schubert 
7677e0c4386eSCy Schubert  end:
7678e0c4386eSCy Schubert     SSL_free(serverssl);
7679e0c4386eSCy Schubert     SSL_free(clientssl);
7680e0c4386eSCy Schubert     SSL_CTX_free(sctx);
7681e0c4386eSCy Schubert     SSL_CTX_free(cctx);
7682e0c4386eSCy Schubert 
7683e0c4386eSCy Schubert     return testresult;
7684e0c4386eSCy Schubert }
7685e0c4386eSCy Schubert 
7686e0c4386eSCy Schubert static struct {
7687e0c4386eSCy Schubert     unsigned int maxprot;
7688e0c4386eSCy Schubert     const char *clntciphers;
7689e0c4386eSCy Schubert     const char *clnttls13ciphers;
7690e0c4386eSCy Schubert     const char *srvrciphers;
7691e0c4386eSCy Schubert     const char *srvrtls13ciphers;
7692e0c4386eSCy Schubert     const char *shared;
7693e0c4386eSCy Schubert     const char *fipsshared;
7694e0c4386eSCy Schubert } shared_ciphers_data[] = {
7695e0c4386eSCy Schubert /*
7696e0c4386eSCy Schubert  * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
7697e0c4386eSCy Schubert  * TLSv1.3 is enabled but TLSv1.2 is disabled.
7698e0c4386eSCy Schubert  */
7699e0c4386eSCy Schubert #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
7700e0c4386eSCy Schubert     {
7701e0c4386eSCy Schubert         TLS1_2_VERSION,
7702e0c4386eSCy Schubert         "AES128-SHA:AES256-SHA",
7703e0c4386eSCy Schubert         NULL,
7704e0c4386eSCy Schubert         "AES256-SHA:DHE-RSA-AES128-SHA",
7705e0c4386eSCy Schubert         NULL,
7706e0c4386eSCy Schubert         "AES256-SHA",
7707e0c4386eSCy Schubert         "AES256-SHA"
7708e0c4386eSCy Schubert     },
7709e0c4386eSCy Schubert # if !defined(OPENSSL_NO_CHACHA) \
7710e0c4386eSCy Schubert      && !defined(OPENSSL_NO_POLY1305) \
7711e0c4386eSCy Schubert      && !defined(OPENSSL_NO_EC)
7712e0c4386eSCy Schubert     {
7713e0c4386eSCy Schubert         TLS1_2_VERSION,
7714e0c4386eSCy Schubert         "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7715e0c4386eSCy Schubert         NULL,
7716e0c4386eSCy Schubert         "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7717e0c4386eSCy Schubert         NULL,
7718e0c4386eSCy Schubert         "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7719e0c4386eSCy Schubert         "AES128-SHA"
7720e0c4386eSCy Schubert     },
7721e0c4386eSCy Schubert # endif
7722e0c4386eSCy Schubert     {
7723e0c4386eSCy Schubert         TLS1_2_VERSION,
7724e0c4386eSCy Schubert         "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
7725e0c4386eSCy Schubert         NULL,
7726e0c4386eSCy Schubert         "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
7727e0c4386eSCy Schubert         NULL,
7728e0c4386eSCy Schubert         "AES128-SHA:AES256-SHA",
7729e0c4386eSCy Schubert         "AES128-SHA:AES256-SHA"
7730e0c4386eSCy Schubert     },
7731e0c4386eSCy Schubert     {
7732e0c4386eSCy Schubert         TLS1_2_VERSION,
7733e0c4386eSCy Schubert         "AES128-SHA:AES256-SHA",
7734e0c4386eSCy Schubert         NULL,
7735e0c4386eSCy Schubert         "AES128-SHA:DHE-RSA-AES128-SHA",
7736e0c4386eSCy Schubert         NULL,
7737e0c4386eSCy Schubert         "AES128-SHA",
7738e0c4386eSCy Schubert         "AES128-SHA"
7739e0c4386eSCy Schubert     },
7740e0c4386eSCy Schubert #endif
7741e0c4386eSCy Schubert /*
7742e0c4386eSCy Schubert  * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
7743e0c4386eSCy Schubert  * enabled.
7744e0c4386eSCy Schubert  */
7745e0c4386eSCy Schubert #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
7746e0c4386eSCy Schubert     && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
7747e0c4386eSCy Schubert     {
7748e0c4386eSCy Schubert         TLS1_3_VERSION,
7749e0c4386eSCy Schubert         "AES128-SHA:AES256-SHA",
7750e0c4386eSCy Schubert         NULL,
7751e0c4386eSCy Schubert         "AES256-SHA:AES128-SHA256",
7752e0c4386eSCy Schubert         NULL,
7753e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
7754e0c4386eSCy Schubert         "TLS_AES_128_GCM_SHA256:AES256-SHA",
7755e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
7756e0c4386eSCy Schubert     },
7757e0c4386eSCy Schubert #endif
7758e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
7759e0c4386eSCy Schubert     {
7760e0c4386eSCy Schubert         TLS1_3_VERSION,
7761e0c4386eSCy Schubert         "AES128-SHA",
7762e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384",
7763e0c4386eSCy Schubert         "AES256-SHA",
7764e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384",
7765e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384",
7766e0c4386eSCy Schubert         "TLS_AES_256_GCM_SHA384"
7767e0c4386eSCy Schubert     },
7768e0c4386eSCy Schubert #endif
7769e0c4386eSCy Schubert };
7770e0c4386eSCy Schubert 
int_test_ssl_get_shared_ciphers(int tst,int clnt)7771e0c4386eSCy Schubert static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
7772e0c4386eSCy Schubert {
7773e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
7774e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
7775e0c4386eSCy Schubert     int testresult = 0;
7776e0c4386eSCy Schubert     char buf[1024];
7777e0c4386eSCy Schubert     OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
7778e0c4386eSCy Schubert 
7779e0c4386eSCy Schubert     if (!TEST_ptr(tmplibctx))
7780e0c4386eSCy Schubert         goto end;
7781e0c4386eSCy Schubert 
7782e0c4386eSCy Schubert     /*
7783e0c4386eSCy Schubert      * Regardless of whether we're testing with the FIPS provider loaded into
7784e0c4386eSCy Schubert      * libctx, we want one peer to always use the full set of ciphersuites
7785e0c4386eSCy Schubert      * available. Therefore we use a separate libctx with the default provider
7786e0c4386eSCy Schubert      * loaded into it. We run the same tests twice - once with the client side
7787e0c4386eSCy Schubert      * having the full set of ciphersuites and once with the server side.
7788e0c4386eSCy Schubert      */
7789e0c4386eSCy Schubert     if (clnt) {
7790e0c4386eSCy Schubert         cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
7791e0c4386eSCy Schubert         if (!TEST_ptr(cctx))
7792e0c4386eSCy Schubert             goto end;
7793e0c4386eSCy Schubert     } else {
7794e0c4386eSCy Schubert         sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
7795e0c4386eSCy Schubert         if (!TEST_ptr(sctx))
7796e0c4386eSCy Schubert             goto end;
7797e0c4386eSCy Schubert     }
7798e0c4386eSCy Schubert 
7799e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7800e0c4386eSCy Schubert                                        TLS_client_method(),
7801e0c4386eSCy Schubert                                        TLS1_VERSION,
7802e0c4386eSCy Schubert                                        shared_ciphers_data[tst].maxprot,
7803e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
7804e0c4386eSCy Schubert         goto end;
7805e0c4386eSCy Schubert 
7806e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
7807e0c4386eSCy Schubert                                         shared_ciphers_data[tst].clntciphers))
7808e0c4386eSCy Schubert             || (shared_ciphers_data[tst].clnttls13ciphers != NULL
7809e0c4386eSCy Schubert                 && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
7810e0c4386eSCy Schubert                                     shared_ciphers_data[tst].clnttls13ciphers)))
7811e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_cipher_list(sctx,
7812e0c4386eSCy Schubert                                         shared_ciphers_data[tst].srvrciphers))
7813e0c4386eSCy Schubert             || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
7814e0c4386eSCy Schubert                 && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
7815e0c4386eSCy Schubert                                     shared_ciphers_data[tst].srvrtls13ciphers))))
7816e0c4386eSCy Schubert         goto end;
7817e0c4386eSCy Schubert 
7818e0c4386eSCy Schubert 
7819e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7820e0c4386eSCy Schubert                                              NULL, NULL))
7821e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
7822e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
7823e0c4386eSCy Schubert         goto end;
7824e0c4386eSCy Schubert 
7825e0c4386eSCy Schubert     if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
7826e0c4386eSCy Schubert             || !TEST_int_eq(strcmp(buf,
7827e0c4386eSCy Schubert                                    is_fips
7828e0c4386eSCy Schubert                                    ? shared_ciphers_data[tst].fipsshared
7829e0c4386eSCy Schubert                                    : shared_ciphers_data[tst].shared),
7830e0c4386eSCy Schubert                                    0)) {
7831e0c4386eSCy Schubert         TEST_info("Shared ciphers are: %s\n", buf);
7832e0c4386eSCy Schubert         goto end;
7833e0c4386eSCy Schubert     }
7834e0c4386eSCy Schubert 
7835e0c4386eSCy Schubert     testresult = 1;
7836e0c4386eSCy Schubert 
7837e0c4386eSCy Schubert  end:
7838e0c4386eSCy Schubert     SSL_free(serverssl);
7839e0c4386eSCy Schubert     SSL_free(clientssl);
7840e0c4386eSCy Schubert     SSL_CTX_free(sctx);
7841e0c4386eSCy Schubert     SSL_CTX_free(cctx);
7842e0c4386eSCy Schubert     OSSL_LIB_CTX_free(tmplibctx);
7843e0c4386eSCy Schubert 
7844e0c4386eSCy Schubert     return testresult;
7845e0c4386eSCy Schubert }
7846e0c4386eSCy Schubert 
test_ssl_get_shared_ciphers(int tst)7847e0c4386eSCy Schubert static int test_ssl_get_shared_ciphers(int tst)
7848e0c4386eSCy Schubert {
7849e0c4386eSCy Schubert     return int_test_ssl_get_shared_ciphers(tst, 0)
7850e0c4386eSCy Schubert            && int_test_ssl_get_shared_ciphers(tst, 1);
7851e0c4386eSCy Schubert }
7852e0c4386eSCy Schubert 
7853e0c4386eSCy Schubert 
7854e0c4386eSCy Schubert static const char *appdata = "Hello World";
7855e0c4386eSCy Schubert static int gen_tick_called, dec_tick_called, tick_key_cb_called;
7856e0c4386eSCy Schubert static int tick_key_renew = 0;
7857e0c4386eSCy Schubert static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7858e0c4386eSCy Schubert 
gen_tick_cb(SSL * s,void * arg)7859e0c4386eSCy Schubert static int gen_tick_cb(SSL *s, void *arg)
7860e0c4386eSCy Schubert {
7861e0c4386eSCy Schubert     gen_tick_called = 1;
7862e0c4386eSCy Schubert 
7863e0c4386eSCy Schubert     return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
7864e0c4386eSCy Schubert                                            strlen(appdata));
7865e0c4386eSCy Schubert }
7866e0c4386eSCy Schubert 
dec_tick_cb(SSL * s,SSL_SESSION * ss,const unsigned char * keyname,size_t keyname_length,SSL_TICKET_STATUS status,void * arg)7867e0c4386eSCy Schubert static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
7868e0c4386eSCy Schubert                                      const unsigned char *keyname,
7869e0c4386eSCy Schubert                                      size_t keyname_length,
7870e0c4386eSCy Schubert                                      SSL_TICKET_STATUS status,
7871e0c4386eSCy Schubert                                      void *arg)
7872e0c4386eSCy Schubert {
7873e0c4386eSCy Schubert     void *tickdata;
7874e0c4386eSCy Schubert     size_t tickdlen;
7875e0c4386eSCy Schubert 
7876e0c4386eSCy Schubert     dec_tick_called = 1;
7877e0c4386eSCy Schubert 
7878e0c4386eSCy Schubert     if (status == SSL_TICKET_EMPTY)
7879e0c4386eSCy Schubert         return SSL_TICKET_RETURN_IGNORE_RENEW;
7880e0c4386eSCy Schubert 
7881e0c4386eSCy Schubert     if (!TEST_true(status == SSL_TICKET_SUCCESS
7882e0c4386eSCy Schubert                    || status == SSL_TICKET_SUCCESS_RENEW))
7883e0c4386eSCy Schubert         return SSL_TICKET_RETURN_ABORT;
7884e0c4386eSCy Schubert 
7885e0c4386eSCy Schubert     if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
7886e0c4386eSCy Schubert                                                    &tickdlen))
7887e0c4386eSCy Schubert             || !TEST_size_t_eq(tickdlen, strlen(appdata))
7888e0c4386eSCy Schubert             || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
7889e0c4386eSCy Schubert         return SSL_TICKET_RETURN_ABORT;
7890e0c4386eSCy Schubert 
7891e0c4386eSCy Schubert     if (tick_key_cb_called)  {
7892e0c4386eSCy Schubert         /* Don't change what the ticket key callback wanted to do */
7893e0c4386eSCy Schubert         switch (status) {
7894e0c4386eSCy Schubert         case SSL_TICKET_NO_DECRYPT:
7895e0c4386eSCy Schubert             return SSL_TICKET_RETURN_IGNORE_RENEW;
7896e0c4386eSCy Schubert 
7897e0c4386eSCy Schubert         case SSL_TICKET_SUCCESS:
7898e0c4386eSCy Schubert             return SSL_TICKET_RETURN_USE;
7899e0c4386eSCy Schubert 
7900e0c4386eSCy Schubert         case SSL_TICKET_SUCCESS_RENEW:
7901e0c4386eSCy Schubert             return SSL_TICKET_RETURN_USE_RENEW;
7902e0c4386eSCy Schubert 
7903e0c4386eSCy Schubert         default:
7904e0c4386eSCy Schubert             return SSL_TICKET_RETURN_ABORT;
7905e0c4386eSCy Schubert         }
7906e0c4386eSCy Schubert     }
7907e0c4386eSCy Schubert     return tick_dec_ret;
7908e0c4386eSCy Schubert 
7909e0c4386eSCy Schubert }
7910e0c4386eSCy Schubert 
7911e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
tick_key_cb(SSL * s,unsigned char key_name[16],unsigned char iv[EVP_MAX_IV_LENGTH],EVP_CIPHER_CTX * ctx,HMAC_CTX * hctx,int enc)7912e0c4386eSCy Schubert static int tick_key_cb(SSL *s, unsigned char key_name[16],
7913e0c4386eSCy Schubert                        unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
7914e0c4386eSCy Schubert                        HMAC_CTX *hctx, int enc)
7915e0c4386eSCy Schubert {
7916e0c4386eSCy Schubert     const unsigned char tick_aes_key[16] = "0123456789abcdef";
7917e0c4386eSCy Schubert     const unsigned char tick_hmac_key[16] = "0123456789abcdef";
7918e0c4386eSCy Schubert     EVP_CIPHER *aes128cbc;
7919e0c4386eSCy Schubert     EVP_MD *sha256;
7920e0c4386eSCy Schubert     int ret;
7921e0c4386eSCy Schubert 
7922e0c4386eSCy Schubert     tick_key_cb_called = 1;
7923e0c4386eSCy Schubert 
7924e0c4386eSCy Schubert     if (tick_key_renew == -1)
7925e0c4386eSCy Schubert         return 0;
7926e0c4386eSCy Schubert 
7927e0c4386eSCy Schubert     aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7928e0c4386eSCy Schubert     if (!TEST_ptr(aes128cbc))
7929e0c4386eSCy Schubert         return 0;
7930e0c4386eSCy Schubert     sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
7931e0c4386eSCy Schubert     if (!TEST_ptr(sha256)) {
7932e0c4386eSCy Schubert         EVP_CIPHER_free(aes128cbc);
7933e0c4386eSCy Schubert         return 0;
7934e0c4386eSCy Schubert     }
7935e0c4386eSCy Schubert 
7936e0c4386eSCy Schubert     memset(iv, 0, AES_BLOCK_SIZE);
7937e0c4386eSCy Schubert     memset(key_name, 0, 16);
7938e0c4386eSCy Schubert     if (aes128cbc == NULL
7939e0c4386eSCy Schubert             || sha256 == NULL
7940e0c4386eSCy Schubert             || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7941e0c4386eSCy Schubert             || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
7942e0c4386eSCy Schubert                              NULL))
7943e0c4386eSCy Schubert         ret = -1;
7944e0c4386eSCy Schubert     else
7945e0c4386eSCy Schubert         ret = tick_key_renew ? 2 : 1;
7946e0c4386eSCy Schubert 
7947e0c4386eSCy Schubert     EVP_CIPHER_free(aes128cbc);
7948e0c4386eSCy Schubert     EVP_MD_free(sha256);
7949e0c4386eSCy Schubert 
7950e0c4386eSCy Schubert     return ret;
7951e0c4386eSCy Schubert }
7952e0c4386eSCy Schubert #endif
7953e0c4386eSCy Schubert 
tick_key_evp_cb(SSL * s,unsigned char key_name[16],unsigned char iv[EVP_MAX_IV_LENGTH],EVP_CIPHER_CTX * ctx,EVP_MAC_CTX * hctx,int enc)7954e0c4386eSCy Schubert static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
7955e0c4386eSCy Schubert                            unsigned char iv[EVP_MAX_IV_LENGTH],
7956e0c4386eSCy Schubert                            EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
7957e0c4386eSCy Schubert {
7958e0c4386eSCy Schubert     const unsigned char tick_aes_key[16] = "0123456789abcdef";
7959e0c4386eSCy Schubert     unsigned char tick_hmac_key[16] = "0123456789abcdef";
7960e0c4386eSCy Schubert     OSSL_PARAM params[2];
7961e0c4386eSCy Schubert     EVP_CIPHER *aes128cbc;
7962e0c4386eSCy Schubert     int ret;
7963e0c4386eSCy Schubert 
7964e0c4386eSCy Schubert     tick_key_cb_called = 1;
7965e0c4386eSCy Schubert 
7966e0c4386eSCy Schubert     if (tick_key_renew == -1)
7967e0c4386eSCy Schubert         return 0;
7968e0c4386eSCy Schubert 
7969e0c4386eSCy Schubert     aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7970e0c4386eSCy Schubert     if (!TEST_ptr(aes128cbc))
7971e0c4386eSCy Schubert         return 0;
7972e0c4386eSCy Schubert 
7973e0c4386eSCy Schubert     memset(iv, 0, AES_BLOCK_SIZE);
7974e0c4386eSCy Schubert     memset(key_name, 0, 16);
7975e0c4386eSCy Schubert     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
7976e0c4386eSCy Schubert                                                  "SHA256", 0);
7977e0c4386eSCy Schubert     params[1] = OSSL_PARAM_construct_end();
7978e0c4386eSCy Schubert     if (aes128cbc == NULL
7979e0c4386eSCy Schubert             || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7980e0c4386eSCy Schubert             || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
7981e0c4386eSCy Schubert                              params))
7982e0c4386eSCy Schubert         ret = -1;
7983e0c4386eSCy Schubert     else
7984e0c4386eSCy Schubert         ret = tick_key_renew ? 2 : 1;
7985e0c4386eSCy Schubert 
7986e0c4386eSCy Schubert     EVP_CIPHER_free(aes128cbc);
7987e0c4386eSCy Schubert 
7988e0c4386eSCy Schubert     return ret;
7989e0c4386eSCy Schubert }
7990e0c4386eSCy Schubert 
7991e0c4386eSCy Schubert /*
7992e0c4386eSCy Schubert  * Test the various ticket callbacks
7993e0c4386eSCy Schubert  * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
7994e0c4386eSCy Schubert  * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
7995e0c4386eSCy Schubert  * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
7996e0c4386eSCy Schubert  * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
7997e0c4386eSCy Schubert  * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
7998e0c4386eSCy Schubert  * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
7999e0c4386eSCy Schubert  * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
8000e0c4386eSCy Schubert  * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
8001e0c4386eSCy Schubert  * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
8002e0c4386eSCy Schubert  * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
8003e0c4386eSCy Schubert  * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
8004e0c4386eSCy Schubert  * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
8005e0c4386eSCy Schubert  * Test 12: TLSv1.2, old ticket key callback, no ticket
8006e0c4386eSCy Schubert  * Test 13: TLSv1.3, old ticket key callback, no ticket
8007e0c4386eSCy Schubert  * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
8008e0c4386eSCy Schubert  * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
8009e0c4386eSCy Schubert  * Test 16: TLSv1.2, ticket key callback, ticket, renewal
8010e0c4386eSCy Schubert  * Test 17: TLSv1.3, ticket key callback, ticket, renewal
8011e0c4386eSCy Schubert  * Test 18: TLSv1.2, ticket key callback, no ticket
8012e0c4386eSCy Schubert  * Test 19: TLSv1.3, ticket key callback, no ticket
8013e0c4386eSCy Schubert  */
test_ticket_callbacks(int tst)8014e0c4386eSCy Schubert static int test_ticket_callbacks(int tst)
8015e0c4386eSCy Schubert {
8016e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8017e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8018e0c4386eSCy Schubert     SSL_SESSION *clntsess = NULL;
8019e0c4386eSCy Schubert     int testresult = 0;
8020e0c4386eSCy Schubert 
8021e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
8022e0c4386eSCy Schubert     if (tst % 2 == 0)
8023e0c4386eSCy Schubert         return 1;
8024e0c4386eSCy Schubert #endif
8025e0c4386eSCy Schubert #ifdef OSSL_NO_USABLE_TLS1_3
8026e0c4386eSCy Schubert     if (tst % 2 == 1)
8027e0c4386eSCy Schubert         return 1;
8028e0c4386eSCy Schubert #endif
8029e0c4386eSCy Schubert #ifdef OPENSSL_NO_DEPRECATED_3_0
8030e0c4386eSCy Schubert     if (tst >= 8 && tst <= 13)
8031e0c4386eSCy Schubert         return 1;
8032e0c4386eSCy Schubert #endif
8033e0c4386eSCy Schubert 
8034e0c4386eSCy Schubert     gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
8035e0c4386eSCy Schubert 
8036e0c4386eSCy Schubert     /* Which tests the ticket key callback should request renewal for */
8037e0c4386eSCy Schubert 
8038e0c4386eSCy Schubert     if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
8039e0c4386eSCy Schubert         tick_key_renew = 1;
8040e0c4386eSCy Schubert     else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
8041e0c4386eSCy Schubert         tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
8042e0c4386eSCy Schubert     else
8043e0c4386eSCy Schubert         tick_key_renew = 0;
8044e0c4386eSCy Schubert 
8045e0c4386eSCy Schubert     /* Which tests the decrypt ticket callback should request renewal for */
8046e0c4386eSCy Schubert     switch (tst) {
8047e0c4386eSCy Schubert     case 0:
8048e0c4386eSCy Schubert     case 1:
8049e0c4386eSCy Schubert         tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
8050e0c4386eSCy Schubert         break;
8051e0c4386eSCy Schubert 
8052e0c4386eSCy Schubert     case 2:
8053e0c4386eSCy Schubert     case 3:
8054e0c4386eSCy Schubert         tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
8055e0c4386eSCy Schubert         break;
8056e0c4386eSCy Schubert 
8057e0c4386eSCy Schubert     case 4:
8058e0c4386eSCy Schubert     case 5:
8059e0c4386eSCy Schubert         tick_dec_ret = SSL_TICKET_RETURN_USE;
8060e0c4386eSCy Schubert         break;
8061e0c4386eSCy Schubert 
8062e0c4386eSCy Schubert     case 6:
8063e0c4386eSCy Schubert     case 7:
8064e0c4386eSCy Schubert         tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
8065e0c4386eSCy Schubert         break;
8066e0c4386eSCy Schubert 
8067e0c4386eSCy Schubert     default:
8068e0c4386eSCy Schubert         tick_dec_ret = SSL_TICKET_RETURN_ABORT;
8069e0c4386eSCy Schubert     }
8070e0c4386eSCy Schubert 
8071e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8072e0c4386eSCy Schubert                                        TLS_client_method(),
8073e0c4386eSCy Schubert                                        TLS1_VERSION,
8074e0c4386eSCy Schubert                                        ((tst % 2) == 0) ? TLS1_2_VERSION
8075e0c4386eSCy Schubert                                                         : TLS1_3_VERSION,
8076e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
8077e0c4386eSCy Schubert         goto end;
8078e0c4386eSCy Schubert 
8079e0c4386eSCy Schubert     /*
8080e0c4386eSCy Schubert      * We only want sessions to resume from tickets - not the session cache. So
8081e0c4386eSCy Schubert      * switch the cache off.
8082e0c4386eSCy Schubert      */
8083e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
8084e0c4386eSCy Schubert         goto end;
8085e0c4386eSCy Schubert 
8086e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
8087e0c4386eSCy Schubert                                                  NULL)))
8088e0c4386eSCy Schubert         goto end;
8089e0c4386eSCy Schubert 
8090e0c4386eSCy Schubert     if (tst >= 14) {
8091e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
8092e0c4386eSCy Schubert             goto end;
8093e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
8094e0c4386eSCy Schubert     } else if (tst >= 8) {
8095e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
8096e0c4386eSCy Schubert             goto end;
8097e0c4386eSCy Schubert #endif
8098e0c4386eSCy Schubert     }
8099e0c4386eSCy Schubert 
8100e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8101e0c4386eSCy Schubert                                              NULL, NULL))
8102e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
8103e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
8104e0c4386eSCy Schubert         goto end;
8105e0c4386eSCy Schubert 
8106e0c4386eSCy Schubert     /*
8107e0c4386eSCy Schubert      * The decrypt ticket key callback in TLSv1.2 should be called even though
8108e0c4386eSCy Schubert      * we have no ticket yet, because it gets called with a status of
8109e0c4386eSCy Schubert      * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
8110e0c4386eSCy Schubert      * actually send any ticket data). This does not happen in TLSv1.3 because
8111e0c4386eSCy Schubert      * it is not valid to send empty ticket data in TLSv1.3.
8112e0c4386eSCy Schubert      */
8113e0c4386eSCy Schubert     if (!TEST_int_eq(gen_tick_called, 1)
8114e0c4386eSCy Schubert             || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
8115e0c4386eSCy Schubert         goto end;
8116e0c4386eSCy Schubert 
8117e0c4386eSCy Schubert     gen_tick_called = dec_tick_called = 0;
8118e0c4386eSCy Schubert 
8119e0c4386eSCy Schubert     clntsess = SSL_get1_session(clientssl);
8120e0c4386eSCy Schubert     SSL_shutdown(clientssl);
8121e0c4386eSCy Schubert     SSL_shutdown(serverssl);
8122e0c4386eSCy Schubert     SSL_free(serverssl);
8123e0c4386eSCy Schubert     SSL_free(clientssl);
8124e0c4386eSCy Schubert     serverssl = clientssl = NULL;
8125e0c4386eSCy Schubert 
8126e0c4386eSCy Schubert     /* Now do a resumption */
8127e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
8128e0c4386eSCy Schubert                                       NULL))
8129e0c4386eSCy Schubert             || !TEST_true(SSL_set_session(clientssl, clntsess))
8130e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
8131e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
8132e0c4386eSCy Schubert         goto end;
8133e0c4386eSCy Schubert 
8134e0c4386eSCy Schubert     if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
8135e0c4386eSCy Schubert             || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8136e0c4386eSCy Schubert             || tick_key_renew == -1) {
8137e0c4386eSCy Schubert         if (!TEST_false(SSL_session_reused(clientssl)))
8138e0c4386eSCy Schubert             goto end;
8139e0c4386eSCy Schubert     } else {
8140e0c4386eSCy Schubert         if (!TEST_true(SSL_session_reused(clientssl)))
8141e0c4386eSCy Schubert             goto end;
8142e0c4386eSCy Schubert     }
8143e0c4386eSCy Schubert 
8144e0c4386eSCy Schubert     if (!TEST_int_eq(gen_tick_called,
8145e0c4386eSCy Schubert                      (tick_key_renew
8146e0c4386eSCy Schubert                       || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8147e0c4386eSCy Schubert                       || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
8148e0c4386eSCy Schubert                      ? 1 : 0)
8149e0c4386eSCy Schubert                /* There is no ticket to decrypt in tests 13 and 19 */
8150e0c4386eSCy Schubert             || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
8151e0c4386eSCy Schubert         goto end;
8152e0c4386eSCy Schubert 
8153e0c4386eSCy Schubert     testresult = 1;
8154e0c4386eSCy Schubert 
8155e0c4386eSCy Schubert  end:
8156e0c4386eSCy Schubert     SSL_SESSION_free(clntsess);
8157e0c4386eSCy Schubert     SSL_free(serverssl);
8158e0c4386eSCy Schubert     SSL_free(clientssl);
8159e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8160e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8161e0c4386eSCy Schubert 
8162e0c4386eSCy Schubert     return testresult;
8163e0c4386eSCy Schubert }
8164e0c4386eSCy Schubert 
8165e0c4386eSCy Schubert /*
8166e0c4386eSCy Schubert  * Test incorrect shutdown.
8167e0c4386eSCy Schubert  * Test 0: client does not shutdown properly,
8168e0c4386eSCy Schubert  *         server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
8169e0c4386eSCy Schubert  *         server should get SSL_ERROR_SSL
8170e0c4386eSCy Schubert  * Test 1: client does not shutdown properly,
8171e0c4386eSCy Schubert  *         server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
8172e0c4386eSCy Schubert  *         server should get SSL_ERROR_ZERO_RETURN
8173e0c4386eSCy Schubert  */
test_incorrect_shutdown(int tst)8174e0c4386eSCy Schubert static int test_incorrect_shutdown(int tst)
8175e0c4386eSCy Schubert {
8176e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8177e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8178e0c4386eSCy Schubert     int testresult = 0;
8179e0c4386eSCy Schubert     char buf[80];
8180e0c4386eSCy Schubert     BIO *c2s;
8181e0c4386eSCy Schubert 
8182e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8183e0c4386eSCy Schubert                                        TLS_client_method(), 0, 0,
8184e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
8185e0c4386eSCy Schubert         goto end;
8186e0c4386eSCy Schubert 
8187e0c4386eSCy Schubert     if (tst == 1)
8188e0c4386eSCy Schubert         SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
8189e0c4386eSCy Schubert 
8190e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8191e0c4386eSCy Schubert                                             NULL, NULL)))
8192e0c4386eSCy Schubert         goto end;
8193e0c4386eSCy Schubert 
8194e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8195e0c4386eSCy Schubert                                               SSL_ERROR_NONE)))
8196e0c4386eSCy Schubert         goto end;
8197e0c4386eSCy Schubert 
8198e0c4386eSCy Schubert     c2s = SSL_get_rbio(serverssl);
8199e0c4386eSCy Schubert     BIO_set_mem_eof_return(c2s, 0);
8200e0c4386eSCy Schubert 
8201e0c4386eSCy Schubert     if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
8202e0c4386eSCy Schubert         goto end;
8203e0c4386eSCy Schubert 
8204e0c4386eSCy Schubert     if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
8205e0c4386eSCy Schubert         goto end;
8206e0c4386eSCy Schubert     if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
8207e0c4386eSCy Schubert         goto end;
8208e0c4386eSCy Schubert 
8209e0c4386eSCy Schubert     testresult = 1;
8210e0c4386eSCy Schubert 
8211e0c4386eSCy Schubert  end:
8212e0c4386eSCy Schubert     SSL_free(serverssl);
8213e0c4386eSCy Schubert     SSL_free(clientssl);
8214e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8215e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8216e0c4386eSCy Schubert 
8217e0c4386eSCy Schubert     return testresult;
8218e0c4386eSCy Schubert }
8219e0c4386eSCy Schubert 
8220e0c4386eSCy Schubert /*
8221e0c4386eSCy Schubert  * Test bi-directional shutdown.
8222e0c4386eSCy Schubert  * Test 0: TLSv1.2
8223e0c4386eSCy Schubert  * Test 1: TLSv1.2, server continues to read/write after client shutdown
8224e0c4386eSCy Schubert  * Test 2: TLSv1.3, no pending NewSessionTicket messages
8225e0c4386eSCy Schubert  * Test 3: TLSv1.3, pending NewSessionTicket messages
8226e0c4386eSCy Schubert  * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
8227e0c4386eSCy Schubert  *                  sends key update, client reads it
8228e0c4386eSCy Schubert  * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
8229e0c4386eSCy Schubert  *                  sends CertificateRequest, client reads and ignores it
8230e0c4386eSCy Schubert  * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
8231e0c4386eSCy Schubert  *                  doesn't read it
8232e0c4386eSCy Schubert  */
test_shutdown(int tst)8233e0c4386eSCy Schubert static int test_shutdown(int tst)
8234e0c4386eSCy Schubert {
8235e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8236e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8237e0c4386eSCy Schubert     int testresult = 0;
8238e0c4386eSCy Schubert     char msg[] = "A test message";
8239e0c4386eSCy Schubert     char buf[80];
8240e0c4386eSCy Schubert     size_t written, readbytes;
8241e0c4386eSCy Schubert     SSL_SESSION *sess;
8242e0c4386eSCy Schubert 
8243e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
8244e0c4386eSCy Schubert     if (tst <= 1)
8245e0c4386eSCy Schubert         return 1;
8246e0c4386eSCy Schubert #endif
8247e0c4386eSCy Schubert #ifdef OSSL_NO_USABLE_TLS1_3
8248e0c4386eSCy Schubert     if (tst >= 2)
8249e0c4386eSCy Schubert         return 1;
8250e0c4386eSCy Schubert #endif
8251e0c4386eSCy Schubert 
8252e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8253e0c4386eSCy Schubert                                        TLS_client_method(),
8254e0c4386eSCy Schubert                                        TLS1_VERSION,
8255e0c4386eSCy Schubert                                        (tst <= 1) ? TLS1_2_VERSION
8256e0c4386eSCy Schubert                                                   : TLS1_3_VERSION,
8257e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
8258e0c4386eSCy Schubert         goto end;
8259e0c4386eSCy Schubert 
8260e0c4386eSCy Schubert     if (tst == 5)
8261e0c4386eSCy Schubert         SSL_CTX_set_post_handshake_auth(cctx, 1);
8262e0c4386eSCy Schubert 
8263e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8264e0c4386eSCy Schubert                                              NULL, NULL)))
8265e0c4386eSCy Schubert         goto end;
8266e0c4386eSCy Schubert 
8267e0c4386eSCy Schubert     if (tst == 3) {
8268e0c4386eSCy Schubert         if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
8269e0c4386eSCy Schubert                                                   SSL_ERROR_NONE, 1))
8270e0c4386eSCy Schubert                 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8271e0c4386eSCy Schubert                 || !TEST_false(SSL_SESSION_is_resumable(sess)))
8272e0c4386eSCy Schubert             goto end;
8273e0c4386eSCy Schubert     } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8274e0c4386eSCy Schubert                                               SSL_ERROR_NONE))
8275e0c4386eSCy Schubert             || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8276e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_is_resumable(sess))) {
8277e0c4386eSCy Schubert         goto end;
8278e0c4386eSCy Schubert     }
8279e0c4386eSCy Schubert 
8280e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
8281e0c4386eSCy Schubert         goto end;
8282e0c4386eSCy Schubert 
8283e0c4386eSCy Schubert     if (tst >= 4) {
8284e0c4386eSCy Schubert         /*
8285e0c4386eSCy Schubert          * Reading on the server after the client has sent close_notify should
8286e0c4386eSCy Schubert          * fail and provide SSL_ERROR_ZERO_RETURN
8287e0c4386eSCy Schubert          */
8288e0c4386eSCy Schubert         if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
8289e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_error(serverssl, 0),
8290e0c4386eSCy Schubert                                 SSL_ERROR_ZERO_RETURN)
8291e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_shutdown(serverssl),
8292e0c4386eSCy Schubert                                 SSL_RECEIVED_SHUTDOWN)
8293e0c4386eSCy Schubert                    /*
8294e0c4386eSCy Schubert                     * Even though we're shutdown on receive we should still be
8295e0c4386eSCy Schubert                     * able to write.
8296e0c4386eSCy Schubert                     */
8297e0c4386eSCy Schubert                 || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8298e0c4386eSCy Schubert             goto end;
8299e0c4386eSCy Schubert         if (tst == 4
8300e0c4386eSCy Schubert                 && !TEST_true(SSL_key_update(serverssl,
8301e0c4386eSCy Schubert                                              SSL_KEY_UPDATE_REQUESTED)))
8302e0c4386eSCy Schubert             goto end;
8303e0c4386eSCy Schubert         if (tst == 5) {
8304e0c4386eSCy Schubert             SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
8305e0c4386eSCy Schubert             if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
8306e0c4386eSCy Schubert                 goto end;
8307e0c4386eSCy Schubert         }
8308e0c4386eSCy Schubert         if ((tst == 4 || tst == 5)
8309e0c4386eSCy Schubert                 && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8310e0c4386eSCy Schubert             goto end;
8311e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
8312e0c4386eSCy Schubert             goto end;
8313e0c4386eSCy Schubert         if (tst == 4 || tst == 5) {
8314e0c4386eSCy Schubert             /* Should still be able to read data from server */
8315e0c4386eSCy Schubert             if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8316e0c4386eSCy Schubert                                        &readbytes))
8317e0c4386eSCy Schubert                     || !TEST_size_t_eq(readbytes, sizeof(msg))
8318e0c4386eSCy Schubert                     || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
8319e0c4386eSCy Schubert                     || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8320e0c4386eSCy Schubert                                               &readbytes))
8321e0c4386eSCy Schubert                     || !TEST_size_t_eq(readbytes, sizeof(msg))
8322e0c4386eSCy Schubert                     || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
8323e0c4386eSCy Schubert                 goto end;
8324e0c4386eSCy Schubert         }
8325e0c4386eSCy Schubert     }
8326e0c4386eSCy Schubert 
8327e0c4386eSCy Schubert     /* Writing on the client after sending close_notify shouldn't be possible */
8328e0c4386eSCy Schubert     if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
8329e0c4386eSCy Schubert         goto end;
8330e0c4386eSCy Schubert 
8331e0c4386eSCy Schubert     if (tst < 4) {
8332e0c4386eSCy Schubert         /*
8333e0c4386eSCy Schubert          * For these tests the client has sent close_notify but it has not yet
8334e0c4386eSCy Schubert          * been received by the server. The server has not sent close_notify
8335e0c4386eSCy Schubert          * yet.
8336e0c4386eSCy Schubert          */
8337e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
8338e0c4386eSCy Schubert                    /*
8339e0c4386eSCy Schubert                     * Writing on the server after sending close_notify shouldn't
8340e0c4386eSCy Schubert                     * be possible.
8341e0c4386eSCy Schubert                     */
8342e0c4386eSCy Schubert                 || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8343e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_shutdown(clientssl), 1)
8344e0c4386eSCy Schubert                 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8345e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_is_resumable(sess))
8346e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_shutdown(serverssl), 1))
8347e0c4386eSCy Schubert             goto end;
8348e0c4386eSCy Schubert     } else if (tst == 4 || tst == 5) {
8349e0c4386eSCy Schubert         /*
8350e0c4386eSCy Schubert          * In this test the client has sent close_notify and it has been
8351e0c4386eSCy Schubert          * received by the server which has responded with a close_notify. The
8352e0c4386eSCy Schubert          * client needs to read the close_notify sent by the server.
8353e0c4386eSCy Schubert          */
8354e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
8355e0c4386eSCy Schubert                 || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8356e0c4386eSCy Schubert                 || !TEST_true(SSL_SESSION_is_resumable(sess)))
8357e0c4386eSCy Schubert             goto end;
8358e0c4386eSCy Schubert     } else {
8359e0c4386eSCy Schubert         /*
8360e0c4386eSCy Schubert          * tst == 6
8361e0c4386eSCy Schubert          *
8362e0c4386eSCy Schubert          * The client has sent close_notify and is expecting a close_notify
8363e0c4386eSCy Schubert          * back, but instead there is application data first. The shutdown
8364e0c4386eSCy Schubert          * should fail with a fatal error.
8365e0c4386eSCy Schubert          */
8366e0c4386eSCy Schubert         if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
8367e0c4386eSCy Schubert                 || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
8368e0c4386eSCy Schubert             goto end;
8369e0c4386eSCy Schubert     }
8370e0c4386eSCy Schubert 
8371e0c4386eSCy Schubert     testresult = 1;
8372e0c4386eSCy Schubert 
8373e0c4386eSCy Schubert  end:
8374e0c4386eSCy Schubert     SSL_free(serverssl);
8375e0c4386eSCy Schubert     SSL_free(clientssl);
8376e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8377e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8378e0c4386eSCy Schubert 
8379e0c4386eSCy Schubert     return testresult;
8380e0c4386eSCy Schubert }
8381e0c4386eSCy Schubert 
8382e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8383e0c4386eSCy Schubert static int cert_cb_cnt;
8384e0c4386eSCy Schubert 
cert_cb(SSL * s,void * arg)8385e0c4386eSCy Schubert static int cert_cb(SSL *s, void *arg)
8386e0c4386eSCy Schubert {
8387e0c4386eSCy Schubert     SSL_CTX *ctx = (SSL_CTX *)arg;
8388e0c4386eSCy Schubert     BIO *in = NULL;
8389e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
8390e0c4386eSCy Schubert     X509 *x509 = NULL, *rootx = NULL;
8391e0c4386eSCy Schubert     STACK_OF(X509) *chain = NULL;
8392e0c4386eSCy Schubert     char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
8393e0c4386eSCy Schubert     int ret = 0;
8394e0c4386eSCy Schubert 
8395e0c4386eSCy Schubert     if (cert_cb_cnt == 0) {
8396e0c4386eSCy Schubert         /* Suspend the handshake */
8397e0c4386eSCy Schubert         cert_cb_cnt++;
8398e0c4386eSCy Schubert         return -1;
8399e0c4386eSCy Schubert     } else if (cert_cb_cnt == 1) {
8400e0c4386eSCy Schubert         /*
8401e0c4386eSCy Schubert          * Update the SSL_CTX, set the certificate and private key and then
8402e0c4386eSCy Schubert          * continue the handshake normally.
8403e0c4386eSCy Schubert          */
8404e0c4386eSCy Schubert         if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
8405e0c4386eSCy Schubert             return 0;
8406e0c4386eSCy Schubert 
8407e0c4386eSCy Schubert         if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
8408e0c4386eSCy Schubert                 || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
8409e0c4386eSCy Schubert                                                       SSL_FILETYPE_PEM))
8410e0c4386eSCy Schubert                 || !TEST_true(SSL_check_private_key(s)))
8411e0c4386eSCy Schubert             return 0;
8412e0c4386eSCy Schubert         cert_cb_cnt++;
8413e0c4386eSCy Schubert         return 1;
8414e0c4386eSCy Schubert     } else if (cert_cb_cnt == 3) {
8415e0c4386eSCy Schubert         int rv;
8416e0c4386eSCy Schubert 
8417e0c4386eSCy Schubert         rootfile = test_mk_file_path(certsdir, "rootcert.pem");
8418e0c4386eSCy Schubert         ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
8419e0c4386eSCy Schubert         ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
8420e0c4386eSCy Schubert         if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
8421e0c4386eSCy Schubert             goto out;
8422e0c4386eSCy Schubert         chain = sk_X509_new_null();
8423e0c4386eSCy Schubert         if (!TEST_ptr(chain))
8424e0c4386eSCy Schubert             goto out;
8425e0c4386eSCy Schubert         if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8426e0c4386eSCy Schubert                 || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
8427e0c4386eSCy Schubert                 || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
8428e0c4386eSCy Schubert                 || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
8429e0c4386eSCy Schubert                 || !TEST_true(sk_X509_push(chain, rootx)))
8430e0c4386eSCy Schubert             goto out;
8431e0c4386eSCy Schubert         rootx = NULL;
8432e0c4386eSCy Schubert         BIO_free(in);
8433e0c4386eSCy Schubert         if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8434e0c4386eSCy Schubert                 || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
8435e0c4386eSCy Schubert                 || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
8436e0c4386eSCy Schubert                 || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
8437e0c4386eSCy Schubert             goto out;
8438e0c4386eSCy Schubert         BIO_free(in);
8439e0c4386eSCy Schubert         if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8440e0c4386eSCy Schubert                 || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
8441e0c4386eSCy Schubert                 || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
8442e0c4386eSCy Schubert                                                                NULL, NULL,
8443e0c4386eSCy Schubert                                                                libctx, NULL)))
8444e0c4386eSCy Schubert             goto out;
8445e0c4386eSCy Schubert         rv = SSL_check_chain(s, x509, pkey, chain);
8446e0c4386eSCy Schubert         /*
8447e0c4386eSCy Schubert          * If the cert doesn't show as valid here (e.g., because we don't
8448e0c4386eSCy Schubert          * have any shared sigalgs), then we will not set it, and there will
8449e0c4386eSCy Schubert          * be no certificate at all on the SSL or SSL_CTX.  This, in turn,
8450e0c4386eSCy Schubert          * will cause tls_choose_sigalgs() to fail the connection.
8451e0c4386eSCy Schubert          */
8452e0c4386eSCy Schubert         if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
8453e0c4386eSCy Schubert                 == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
8454e0c4386eSCy Schubert             if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
8455e0c4386eSCy Schubert                 goto out;
8456e0c4386eSCy Schubert         }
8457e0c4386eSCy Schubert 
8458e0c4386eSCy Schubert         ret = 1;
8459e0c4386eSCy Schubert     }
8460e0c4386eSCy Schubert 
8461e0c4386eSCy Schubert     /* Abort the handshake */
8462e0c4386eSCy Schubert  out:
8463e0c4386eSCy Schubert     OPENSSL_free(ecdsacert);
8464e0c4386eSCy Schubert     OPENSSL_free(ecdsakey);
8465e0c4386eSCy Schubert     OPENSSL_free(rootfile);
8466e0c4386eSCy Schubert     BIO_free(in);
8467e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
8468e0c4386eSCy Schubert     X509_free(x509);
8469e0c4386eSCy Schubert     X509_free(rootx);
8470e0c4386eSCy Schubert     sk_X509_pop_free(chain, X509_free);
8471e0c4386eSCy Schubert     return ret;
8472e0c4386eSCy Schubert }
8473e0c4386eSCy Schubert 
8474e0c4386eSCy Schubert /*
8475e0c4386eSCy Schubert  * Test the certificate callback.
8476e0c4386eSCy Schubert  * Test 0: Callback fails
8477e0c4386eSCy Schubert  * Test 1: Success - no SSL_set_SSL_CTX() in the callback
8478e0c4386eSCy Schubert  * Test 2: Success - SSL_set_SSL_CTX() in the callback
8479e0c4386eSCy Schubert  * Test 3: Success - Call SSL_check_chain from the callback
8480e0c4386eSCy Schubert  * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
8481e0c4386eSCy Schubert  *                   chain
8482e0c4386eSCy Schubert  * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
8483e0c4386eSCy Schubert  */
test_cert_cb_int(int prot,int tst)8484e0c4386eSCy Schubert static int test_cert_cb_int(int prot, int tst)
8485e0c4386eSCy Schubert {
8486e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
8487e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8488e0c4386eSCy Schubert     int testresult = 0, ret;
8489e0c4386eSCy Schubert 
8490e0c4386eSCy Schubert #ifdef OPENSSL_NO_EC
8491e0c4386eSCy Schubert     /* We use an EC cert in these tests, so we skip in a no-ec build */
8492e0c4386eSCy Schubert     if (tst >= 3)
8493e0c4386eSCy Schubert         return 1;
8494e0c4386eSCy Schubert #endif
8495e0c4386eSCy Schubert 
8496e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8497e0c4386eSCy Schubert                                        TLS_client_method(),
8498e0c4386eSCy Schubert                                        TLS1_VERSION,
8499e0c4386eSCy Schubert                                        prot,
8500e0c4386eSCy Schubert                                        &sctx, &cctx, NULL, NULL)))
8501e0c4386eSCy Schubert         goto end;
8502e0c4386eSCy Schubert 
8503e0c4386eSCy Schubert     if (tst == 0)
8504e0c4386eSCy Schubert         cert_cb_cnt = -1;
8505e0c4386eSCy Schubert     else if (tst >= 3)
8506e0c4386eSCy Schubert         cert_cb_cnt = 3;
8507e0c4386eSCy Schubert     else
8508e0c4386eSCy Schubert         cert_cb_cnt = 0;
8509e0c4386eSCy Schubert 
8510e0c4386eSCy Schubert     if (tst == 2) {
8511e0c4386eSCy Schubert         snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
8512e0c4386eSCy Schubert         if (!TEST_ptr(snictx))
8513e0c4386eSCy Schubert             goto end;
8514e0c4386eSCy Schubert     }
8515e0c4386eSCy Schubert 
8516e0c4386eSCy Schubert     SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
8517e0c4386eSCy Schubert 
8518e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8519e0c4386eSCy Schubert                                       NULL, NULL)))
8520e0c4386eSCy Schubert         goto end;
8521e0c4386eSCy Schubert 
8522e0c4386eSCy Schubert     if (tst == 4) {
8523e0c4386eSCy Schubert         /*
8524e0c4386eSCy Schubert          * We cause SSL_check_chain() to fail by specifying sig_algs that
8525e0c4386eSCy Schubert          * the chain doesn't meet (the root uses an RSA cert)
8526e0c4386eSCy Schubert          */
8527e0c4386eSCy Schubert         if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8528e0c4386eSCy Schubert                                              "ecdsa_secp256r1_sha256")))
8529e0c4386eSCy Schubert             goto end;
8530e0c4386eSCy Schubert     } else if (tst == 5) {
8531e0c4386eSCy Schubert         /*
8532e0c4386eSCy Schubert          * We cause SSL_check_chain() to fail by specifying sig_algs that
8533e0c4386eSCy Schubert          * the ee cert doesn't meet (the ee uses an ECDSA cert)
8534e0c4386eSCy Schubert          */
8535e0c4386eSCy Schubert         if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8536e0c4386eSCy Schubert                            "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
8537e0c4386eSCy Schubert             goto end;
8538e0c4386eSCy Schubert     }
8539e0c4386eSCy Schubert 
8540e0c4386eSCy Schubert     ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
8541e0c4386eSCy Schubert     if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
8542e0c4386eSCy Schubert             || (tst > 0
8543e0c4386eSCy Schubert                 && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
8544e0c4386eSCy Schubert         goto end;
8545e0c4386eSCy Schubert     }
8546e0c4386eSCy Schubert 
8547e0c4386eSCy Schubert     testresult = 1;
8548e0c4386eSCy Schubert 
8549e0c4386eSCy Schubert  end:
8550e0c4386eSCy Schubert     SSL_free(serverssl);
8551e0c4386eSCy Schubert     SSL_free(clientssl);
8552e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8553e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8554e0c4386eSCy Schubert     SSL_CTX_free(snictx);
8555e0c4386eSCy Schubert 
8556e0c4386eSCy Schubert     return testresult;
8557e0c4386eSCy Schubert }
8558e0c4386eSCy Schubert #endif
8559e0c4386eSCy Schubert 
test_cert_cb(int tst)8560e0c4386eSCy Schubert static int test_cert_cb(int tst)
8561e0c4386eSCy Schubert {
8562e0c4386eSCy Schubert     int testresult = 1;
8563e0c4386eSCy Schubert 
8564e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
8565e0c4386eSCy Schubert     testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
8566e0c4386eSCy Schubert #endif
8567e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
8568e0c4386eSCy Schubert     testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
8569e0c4386eSCy Schubert #endif
8570e0c4386eSCy Schubert 
8571e0c4386eSCy Schubert     return testresult;
8572e0c4386eSCy Schubert }
8573e0c4386eSCy Schubert 
client_cert_cb(SSL * ssl,X509 ** x509,EVP_PKEY ** pkey)8574e0c4386eSCy Schubert static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
8575e0c4386eSCy Schubert {
8576e0c4386eSCy Schubert     X509 *xcert;
8577e0c4386eSCy Schubert     EVP_PKEY *privpkey;
8578e0c4386eSCy Schubert     BIO *in = NULL;
8579e0c4386eSCy Schubert     BIO *priv_in = NULL;
8580e0c4386eSCy Schubert 
8581e0c4386eSCy Schubert     /* Check that SSL_get0_peer_certificate() returns something sensible */
8582e0c4386eSCy Schubert     if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
8583e0c4386eSCy Schubert         return 0;
8584e0c4386eSCy Schubert 
8585e0c4386eSCy Schubert     in = BIO_new_file(cert, "r");
8586e0c4386eSCy Schubert     if (!TEST_ptr(in))
8587e0c4386eSCy Schubert         return 0;
8588e0c4386eSCy Schubert 
8589e0c4386eSCy Schubert     if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
8590e0c4386eSCy Schubert             || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
8591e0c4386eSCy Schubert             || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
8592e0c4386eSCy Schubert             || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
8593e0c4386eSCy Schubert                                                                NULL, NULL,
8594e0c4386eSCy Schubert                                                                libctx, NULL)))
8595e0c4386eSCy Schubert         goto err;
8596e0c4386eSCy Schubert 
8597e0c4386eSCy Schubert     *x509 = xcert;
8598e0c4386eSCy Schubert     *pkey = privpkey;
8599e0c4386eSCy Schubert 
8600e0c4386eSCy Schubert     BIO_free(in);
8601e0c4386eSCy Schubert     BIO_free(priv_in);
8602e0c4386eSCy Schubert     return 1;
8603e0c4386eSCy Schubert err:
8604e0c4386eSCy Schubert     X509_free(xcert);
8605e0c4386eSCy Schubert     BIO_free(in);
8606e0c4386eSCy Schubert     BIO_free(priv_in);
8607e0c4386eSCy Schubert     return 0;
8608e0c4386eSCy Schubert }
8609e0c4386eSCy Schubert 
test_client_cert_cb(int tst)8610e0c4386eSCy Schubert static int test_client_cert_cb(int tst)
8611e0c4386eSCy Schubert {
8612e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8613e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8614e0c4386eSCy Schubert     int testresult = 0;
8615e0c4386eSCy Schubert 
8616e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
8617e0c4386eSCy Schubert     if (tst == 0)
8618e0c4386eSCy Schubert         return 1;
8619e0c4386eSCy Schubert #endif
8620e0c4386eSCy Schubert #ifdef OSSL_NO_USABLE_TLS1_3
8621e0c4386eSCy Schubert     if (tst == 1)
8622e0c4386eSCy Schubert         return 1;
8623e0c4386eSCy Schubert #endif
8624e0c4386eSCy Schubert 
8625e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8626e0c4386eSCy Schubert                                        TLS_client_method(),
8627e0c4386eSCy Schubert                                        TLS1_VERSION,
8628e0c4386eSCy Schubert                                        tst == 0 ? TLS1_2_VERSION
8629e0c4386eSCy Schubert                                                 : TLS1_3_VERSION,
8630e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
8631e0c4386eSCy Schubert         goto end;
8632e0c4386eSCy Schubert 
8633e0c4386eSCy Schubert     /*
8634e0c4386eSCy Schubert      * Test that setting a client_cert_cb results in a client certificate being
8635e0c4386eSCy Schubert      * sent.
8636e0c4386eSCy Schubert      */
8637e0c4386eSCy Schubert     SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
8638e0c4386eSCy Schubert     SSL_CTX_set_verify(sctx,
8639e0c4386eSCy Schubert                        SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
8640e0c4386eSCy Schubert                        verify_cb);
8641e0c4386eSCy Schubert 
8642e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8643e0c4386eSCy Schubert                                       NULL, NULL))
8644e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
8645e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
8646e0c4386eSCy Schubert         goto end;
8647e0c4386eSCy Schubert 
8648e0c4386eSCy Schubert     testresult = 1;
8649e0c4386eSCy Schubert 
8650e0c4386eSCy Schubert  end:
8651e0c4386eSCy Schubert     SSL_free(serverssl);
8652e0c4386eSCy Schubert     SSL_free(clientssl);
8653e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8654e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8655e0c4386eSCy Schubert 
8656e0c4386eSCy Schubert     return testresult;
8657e0c4386eSCy Schubert }
8658e0c4386eSCy Schubert 
8659e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8660e0c4386eSCy Schubert /*
8661e0c4386eSCy Schubert  * Test setting certificate authorities on both client and server.
8662e0c4386eSCy Schubert  *
8663e0c4386eSCy Schubert  * Test 0: SSL_CTX_set0_CA_list() only
8664e0c4386eSCy Schubert  * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
8665e0c4386eSCy Schubert  * Test 2: Only SSL_CTX_set_client_CA_list()
8666e0c4386eSCy Schubert  */
test_ca_names_int(int prot,int tst)8667e0c4386eSCy Schubert static int test_ca_names_int(int prot, int tst)
8668e0c4386eSCy Schubert {
8669e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8670e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8671e0c4386eSCy Schubert     int testresult = 0;
8672e0c4386eSCy Schubert     size_t i;
8673e0c4386eSCy Schubert     X509_NAME *name[] = { NULL, NULL, NULL, NULL };
8674e0c4386eSCy Schubert     char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
8675e0c4386eSCy Schubert     STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
8676e0c4386eSCy Schubert     const STACK_OF(X509_NAME) *sktmp = NULL;
8677e0c4386eSCy Schubert 
8678e0c4386eSCy Schubert     for (i = 0; i < OSSL_NELEM(name); i++) {
8679e0c4386eSCy Schubert         name[i] = X509_NAME_new();
8680e0c4386eSCy Schubert         if (!TEST_ptr(name[i])
8681e0c4386eSCy Schubert                 || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
8682e0c4386eSCy Schubert                                                          MBSTRING_ASC,
8683e0c4386eSCy Schubert                                                          (unsigned char *)
8684e0c4386eSCy Schubert                                                          strnames[i],
8685e0c4386eSCy Schubert                                                          -1, -1, 0)))
8686e0c4386eSCy Schubert             goto end;
8687e0c4386eSCy Schubert     }
8688e0c4386eSCy Schubert 
8689e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8690e0c4386eSCy Schubert                                        TLS_client_method(),
8691e0c4386eSCy Schubert                                        TLS1_VERSION,
8692e0c4386eSCy Schubert                                        prot,
8693e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
8694e0c4386eSCy Schubert         goto end;
8695e0c4386eSCy Schubert 
8696e0c4386eSCy Schubert     SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
8697e0c4386eSCy Schubert 
8698e0c4386eSCy Schubert     if (tst == 0 || tst == 1) {
8699e0c4386eSCy Schubert         if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8700e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
8701e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
8702e0c4386eSCy Schubert                 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8703e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
8704e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
8705e0c4386eSCy Schubert             goto end;
8706e0c4386eSCy Schubert 
8707e0c4386eSCy Schubert         SSL_CTX_set0_CA_list(sctx, sk1);
8708e0c4386eSCy Schubert         SSL_CTX_set0_CA_list(cctx, sk2);
8709e0c4386eSCy Schubert         sk1 = sk2 = NULL;
8710e0c4386eSCy Schubert     }
8711e0c4386eSCy Schubert     if (tst == 1 || tst == 2) {
8712e0c4386eSCy Schubert         if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8713e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
8714e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
8715e0c4386eSCy Schubert                 || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8716e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
8717e0c4386eSCy Schubert                 || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
8718e0c4386eSCy Schubert             goto end;
8719e0c4386eSCy Schubert 
8720e0c4386eSCy Schubert         SSL_CTX_set_client_CA_list(sctx, sk1);
8721e0c4386eSCy Schubert         SSL_CTX_set_client_CA_list(cctx, sk2);
8722e0c4386eSCy Schubert         sk1 = sk2 = NULL;
8723e0c4386eSCy Schubert     }
8724e0c4386eSCy Schubert 
8725e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8726e0c4386eSCy Schubert                                       NULL, NULL))
8727e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
8728e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
8729e0c4386eSCy Schubert         goto end;
8730e0c4386eSCy Schubert 
8731e0c4386eSCy Schubert     /*
8732e0c4386eSCy Schubert      * We only expect certificate authorities to have been sent to the server
8733e0c4386eSCy Schubert      * if we are using TLSv1.3 and SSL_set0_CA_list() was used
8734e0c4386eSCy Schubert      */
8735e0c4386eSCy Schubert     sktmp = SSL_get0_peer_CA_list(serverssl);
8736e0c4386eSCy Schubert     if (prot == TLS1_3_VERSION
8737e0c4386eSCy Schubert             && (tst == 0 || tst == 1)) {
8738e0c4386eSCy Schubert         if (!TEST_ptr(sktmp)
8739e0c4386eSCy Schubert                 || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8740e0c4386eSCy Schubert                 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8741e0c4386eSCy Schubert                                               name[0]), 0)
8742e0c4386eSCy Schubert                 || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8743e0c4386eSCy Schubert                                               name[1]), 0))
8744e0c4386eSCy Schubert             goto end;
8745e0c4386eSCy Schubert     } else if (!TEST_ptr_null(sktmp)) {
8746e0c4386eSCy Schubert         goto end;
8747e0c4386eSCy Schubert     }
8748e0c4386eSCy Schubert 
8749e0c4386eSCy Schubert     /*
8750e0c4386eSCy Schubert      * In all tests we expect certificate authorities to have been sent to the
8751e0c4386eSCy Schubert      * client. However, SSL_set_client_CA_list() should override
8752e0c4386eSCy Schubert      * SSL_set0_CA_list()
8753e0c4386eSCy Schubert      */
8754e0c4386eSCy Schubert     sktmp = SSL_get0_peer_CA_list(clientssl);
8755e0c4386eSCy Schubert     if (!TEST_ptr(sktmp)
8756e0c4386eSCy Schubert             || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8757e0c4386eSCy Schubert             || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8758e0c4386eSCy Schubert                                           name[tst == 0 ? 0 : 2]), 0)
8759e0c4386eSCy Schubert             || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8760e0c4386eSCy Schubert                                           name[tst == 0 ? 1 : 3]), 0))
8761e0c4386eSCy Schubert         goto end;
8762e0c4386eSCy Schubert 
8763e0c4386eSCy Schubert     testresult = 1;
8764e0c4386eSCy Schubert 
8765e0c4386eSCy Schubert  end:
8766e0c4386eSCy Schubert     SSL_free(serverssl);
8767e0c4386eSCy Schubert     SSL_free(clientssl);
8768e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8769e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8770e0c4386eSCy Schubert     for (i = 0; i < OSSL_NELEM(name); i++)
8771e0c4386eSCy Schubert         X509_NAME_free(name[i]);
8772e0c4386eSCy Schubert     sk_X509_NAME_pop_free(sk1, X509_NAME_free);
8773e0c4386eSCy Schubert     sk_X509_NAME_pop_free(sk2, X509_NAME_free);
8774e0c4386eSCy Schubert 
8775e0c4386eSCy Schubert     return testresult;
8776e0c4386eSCy Schubert }
8777e0c4386eSCy Schubert #endif
8778e0c4386eSCy Schubert 
test_ca_names(int tst)8779e0c4386eSCy Schubert static int test_ca_names(int tst)
8780e0c4386eSCy Schubert {
8781e0c4386eSCy Schubert     int testresult = 1;
8782e0c4386eSCy Schubert 
8783e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
8784e0c4386eSCy Schubert     testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
8785e0c4386eSCy Schubert #endif
8786e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
8787e0c4386eSCy Schubert     testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
8788e0c4386eSCy Schubert #endif
8789e0c4386eSCy Schubert 
8790e0c4386eSCy Schubert     return testresult;
8791e0c4386eSCy Schubert }
8792e0c4386eSCy Schubert 
8793e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
8794e0c4386eSCy Schubert static const char *multiblock_cipherlist_data[]=
8795e0c4386eSCy Schubert {
8796e0c4386eSCy Schubert     "AES128-SHA",
8797e0c4386eSCy Schubert     "AES128-SHA256",
8798e0c4386eSCy Schubert     "AES256-SHA",
8799e0c4386eSCy Schubert     "AES256-SHA256",
8800e0c4386eSCy Schubert };
8801e0c4386eSCy Schubert 
8802e0c4386eSCy Schubert /* Reduce the fragment size - so the multiblock test buffer can be small */
8803e0c4386eSCy Schubert # define MULTIBLOCK_FRAGSIZE 512
8804e0c4386eSCy Schubert 
test_multiblock_write(int test_index)8805e0c4386eSCy Schubert static int test_multiblock_write(int test_index)
8806e0c4386eSCy Schubert {
8807e0c4386eSCy Schubert     static const char *fetchable_ciphers[]=
8808e0c4386eSCy Schubert     {
8809e0c4386eSCy Schubert         "AES-128-CBC-HMAC-SHA1",
8810e0c4386eSCy Schubert         "AES-128-CBC-HMAC-SHA256",
8811e0c4386eSCy Schubert         "AES-256-CBC-HMAC-SHA1",
8812e0c4386eSCy Schubert         "AES-256-CBC-HMAC-SHA256"
8813e0c4386eSCy Schubert     };
8814e0c4386eSCy Schubert     const char *cipherlist = multiblock_cipherlist_data[test_index];
8815e0c4386eSCy Schubert     const SSL_METHOD *smeth = TLS_server_method();
8816e0c4386eSCy Schubert     const SSL_METHOD *cmeth = TLS_client_method();
8817e0c4386eSCy Schubert     int min_version = TLS1_VERSION;
8818e0c4386eSCy Schubert     int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
8819e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
8820e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
8821e0c4386eSCy Schubert     int testresult = 0;
8822e0c4386eSCy Schubert 
8823e0c4386eSCy Schubert     /*
8824e0c4386eSCy Schubert      * Choose a buffer large enough to perform a multi-block operation
8825e0c4386eSCy Schubert      * i.e: write_len >= 4 * frag_size
8826e0c4386eSCy Schubert      * 9 * is chosen so that multiple multiblocks are used + some leftover.
8827e0c4386eSCy Schubert      */
8828e0c4386eSCy Schubert     unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
8829e0c4386eSCy Schubert     unsigned char buf[sizeof(msg)], *p = buf;
8830e0c4386eSCy Schubert     size_t readbytes, written, len;
8831e0c4386eSCy Schubert     EVP_CIPHER *ciph = NULL;
8832e0c4386eSCy Schubert 
8833e0c4386eSCy Schubert     /*
8834e0c4386eSCy Schubert      * Check if the cipher exists before attempting to use it since it only has
8835e0c4386eSCy Schubert      * a hardware specific implementation.
8836e0c4386eSCy Schubert      */
8837e0c4386eSCy Schubert     ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
8838e0c4386eSCy Schubert     if (ciph == NULL) {
8839e0c4386eSCy Schubert         TEST_skip("Multiblock cipher is not available for %s", cipherlist);
8840e0c4386eSCy Schubert         return 1;
8841e0c4386eSCy Schubert     }
8842e0c4386eSCy Schubert     EVP_CIPHER_free(ciph);
8843e0c4386eSCy Schubert 
8844e0c4386eSCy Schubert     /* Set up a buffer with some data that will be sent to the client */
8845e0c4386eSCy Schubert     RAND_bytes(msg, sizeof(msg));
8846e0c4386eSCy Schubert 
8847e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
8848e0c4386eSCy Schubert                                        max_version, &sctx, &cctx, cert,
8849e0c4386eSCy Schubert                                        privkey)))
8850e0c4386eSCy Schubert         goto end;
8851e0c4386eSCy Schubert 
8852e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
8853e0c4386eSCy Schubert         goto end;
8854e0c4386eSCy Schubert 
8855e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8856e0c4386eSCy Schubert                                       NULL, NULL)))
8857e0c4386eSCy Schubert             goto end;
8858e0c4386eSCy Schubert 
8859e0c4386eSCy Schubert     /* settings to force it to use AES-CBC-HMAC_SHA */
8860e0c4386eSCy Schubert     SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
8861e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
8862e0c4386eSCy Schubert        goto end;
8863e0c4386eSCy Schubert 
8864e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8865e0c4386eSCy Schubert         goto end;
8866e0c4386eSCy Schubert 
8867e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8868e0c4386eSCy Schubert         || !TEST_size_t_eq(written, sizeof(msg)))
8869e0c4386eSCy Schubert         goto end;
8870e0c4386eSCy Schubert 
8871e0c4386eSCy Schubert     len = written;
8872e0c4386eSCy Schubert     while (len > 0) {
8873e0c4386eSCy Schubert         if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
8874e0c4386eSCy Schubert             goto end;
8875e0c4386eSCy Schubert         p += readbytes;
8876e0c4386eSCy Schubert         len -= readbytes;
8877e0c4386eSCy Schubert     }
8878e0c4386eSCy Schubert     if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
8879e0c4386eSCy Schubert         goto end;
8880e0c4386eSCy Schubert 
8881e0c4386eSCy Schubert     testresult = 1;
8882e0c4386eSCy Schubert end:
8883e0c4386eSCy Schubert     SSL_free(serverssl);
8884e0c4386eSCy Schubert     SSL_free(clientssl);
8885e0c4386eSCy Schubert     SSL_CTX_free(sctx);
8886e0c4386eSCy Schubert     SSL_CTX_free(cctx);
8887e0c4386eSCy Schubert 
8888e0c4386eSCy Schubert     return testresult;
8889e0c4386eSCy Schubert }
8890e0c4386eSCy Schubert #endif /* OPENSSL_NO_TLS1_2 */
8891e0c4386eSCy Schubert 
test_session_timeout(int test)8892e0c4386eSCy Schubert static int test_session_timeout(int test)
8893e0c4386eSCy Schubert {
8894e0c4386eSCy Schubert     /*
8895e0c4386eSCy Schubert      * Test session ordering and timeout
8896e0c4386eSCy Schubert      * Can't explicitly test performance of the new code,
8897e0c4386eSCy Schubert      * but can test to see if the ordering of the sessions
8898e0c4386eSCy Schubert      * are correct, and they they are removed as expected
8899e0c4386eSCy Schubert      */
8900e0c4386eSCy Schubert     SSL_SESSION *early = NULL;
8901e0c4386eSCy Schubert     SSL_SESSION *middle = NULL;
8902e0c4386eSCy Schubert     SSL_SESSION *late = NULL;
8903e0c4386eSCy Schubert     SSL_CTX *ctx;
8904e0c4386eSCy Schubert     int testresult = 0;
8905e0c4386eSCy Schubert     long now = (long)time(NULL);
8906e0c4386eSCy Schubert #define TIMEOUT 10
8907e0c4386eSCy Schubert 
8908e0c4386eSCy Schubert     if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
8909e0c4386eSCy Schubert         || !TEST_ptr(early = SSL_SESSION_new())
8910e0c4386eSCy Schubert         || !TEST_ptr(middle = SSL_SESSION_new())
8911e0c4386eSCy Schubert         || !TEST_ptr(late = SSL_SESSION_new()))
8912e0c4386eSCy Schubert         goto end;
8913e0c4386eSCy Schubert 
8914e0c4386eSCy Schubert     /* assign unique session ids */
8915e0c4386eSCy Schubert     early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8916e0c4386eSCy Schubert     memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
8917e0c4386eSCy Schubert     middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8918e0c4386eSCy Schubert     memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
8919e0c4386eSCy Schubert     late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8920e0c4386eSCy Schubert     memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
8921e0c4386eSCy Schubert 
8922e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8923e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8924e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8925e0c4386eSCy Schubert         goto end;
8926e0c4386eSCy Schubert 
8927e0c4386eSCy Schubert     /* Make sure they are all added */
8928e0c4386eSCy Schubert     if (!TEST_ptr(early->prev)
8929e0c4386eSCy Schubert         || !TEST_ptr(middle->prev)
8930e0c4386eSCy Schubert         || !TEST_ptr(late->prev))
8931e0c4386eSCy Schubert         goto end;
8932e0c4386eSCy Schubert 
8933e0c4386eSCy Schubert     if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
8934e0c4386eSCy Schubert         || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
8935e0c4386eSCy Schubert         || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
8936e0c4386eSCy Schubert         goto end;
8937e0c4386eSCy Schubert 
8938e0c4386eSCy Schubert     if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
8939e0c4386eSCy Schubert         || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
8940e0c4386eSCy Schubert         || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
8941e0c4386eSCy Schubert         goto end;
8942e0c4386eSCy Schubert 
8943e0c4386eSCy Schubert     /* Make sure they are all still there */
8944e0c4386eSCy Schubert     if (!TEST_ptr(early->prev)
8945e0c4386eSCy Schubert         || !TEST_ptr(middle->prev)
8946e0c4386eSCy Schubert         || !TEST_ptr(late->prev))
8947e0c4386eSCy Schubert         goto end;
8948e0c4386eSCy Schubert 
8949e0c4386eSCy Schubert     /* Make sure they are in the expected order */
8950e0c4386eSCy Schubert     if (!TEST_ptr_eq(late->next, middle)
8951e0c4386eSCy Schubert         || !TEST_ptr_eq(middle->next, early)
8952e0c4386eSCy Schubert         || !TEST_ptr_eq(early->prev, middle)
8953e0c4386eSCy Schubert         || !TEST_ptr_eq(middle->prev, late))
8954e0c4386eSCy Schubert         goto end;
8955e0c4386eSCy Schubert 
8956e0c4386eSCy Schubert     /* This should remove "early" */
8957e0c4386eSCy Schubert     SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
8958e0c4386eSCy Schubert     if (!TEST_ptr_null(early->prev)
8959e0c4386eSCy Schubert         || !TEST_ptr(middle->prev)
8960e0c4386eSCy Schubert         || !TEST_ptr(late->prev))
8961e0c4386eSCy Schubert         goto end;
8962e0c4386eSCy Schubert 
8963e0c4386eSCy Schubert     /* This should remove "middle" */
8964e0c4386eSCy Schubert     SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
8965e0c4386eSCy Schubert     if (!TEST_ptr_null(early->prev)
8966e0c4386eSCy Schubert         || !TEST_ptr_null(middle->prev)
8967e0c4386eSCy Schubert         || !TEST_ptr(late->prev))
8968e0c4386eSCy Schubert         goto end;
8969e0c4386eSCy Schubert 
8970e0c4386eSCy Schubert     /* This should remove "late" */
8971e0c4386eSCy Schubert     SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
8972e0c4386eSCy Schubert     if (!TEST_ptr_null(early->prev)
8973e0c4386eSCy Schubert         || !TEST_ptr_null(middle->prev)
8974e0c4386eSCy Schubert         || !TEST_ptr_null(late->prev))
8975e0c4386eSCy Schubert         goto end;
8976e0c4386eSCy Schubert 
8977e0c4386eSCy Schubert     /* Add them back in again */
8978e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8979e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8980e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8981e0c4386eSCy Schubert         goto end;
8982e0c4386eSCy Schubert 
8983e0c4386eSCy Schubert     /* Make sure they are all added */
8984e0c4386eSCy Schubert     if (!TEST_ptr(early->prev)
8985e0c4386eSCy Schubert         || !TEST_ptr(middle->prev)
8986e0c4386eSCy Schubert         || !TEST_ptr(late->prev))
8987e0c4386eSCy Schubert         goto end;
8988e0c4386eSCy Schubert 
8989e0c4386eSCy Schubert     /* This should remove all of them */
8990e0c4386eSCy Schubert     SSL_CTX_flush_sessions(ctx, 0);
8991e0c4386eSCy Schubert     if (!TEST_ptr_null(early->prev)
8992e0c4386eSCy Schubert         || !TEST_ptr_null(middle->prev)
8993e0c4386eSCy Schubert         || !TEST_ptr_null(late->prev))
8994e0c4386eSCy Schubert         goto end;
8995e0c4386eSCy Schubert 
8996e0c4386eSCy Schubert     (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
8997e0c4386eSCy Schubert                                          | SSL_CTX_get_session_cache_mode(ctx));
8998e0c4386eSCy Schubert 
8999e0c4386eSCy Schubert     /* make sure |now| is NOT  equal to the current time */
9000e0c4386eSCy Schubert     now -= 10;
9001e0c4386eSCy Schubert     if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
9002e0c4386eSCy Schubert         || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
9003e0c4386eSCy Schubert         || !TEST_long_ne(SSL_SESSION_get_time(early), now))
9004e0c4386eSCy Schubert         goto end;
9005e0c4386eSCy Schubert 
9006e0c4386eSCy Schubert     testresult = 1;
9007e0c4386eSCy Schubert  end:
9008e0c4386eSCy Schubert     SSL_CTX_free(ctx);
9009e0c4386eSCy Schubert     SSL_SESSION_free(early);
9010e0c4386eSCy Schubert     SSL_SESSION_free(middle);
9011e0c4386eSCy Schubert     SSL_SESSION_free(late);
9012e0c4386eSCy Schubert     return testresult;
9013e0c4386eSCy Schubert }
9014e0c4386eSCy Schubert 
9015e0c4386eSCy Schubert /*
901644096ebdSEnji Cooper  * Test that a session cache overflow works as expected
901744096ebdSEnji Cooper  * Test 0: TLSv1.3, timeout on new session later than old session
901844096ebdSEnji Cooper  * Test 1: TLSv1.2, timeout on new session later than old session
901944096ebdSEnji Cooper  * Test 2: TLSv1.3, timeout on new session earlier than old session
902044096ebdSEnji Cooper  * Test 3: TLSv1.2, timeout on new session earlier than old session
902144096ebdSEnji Cooper  */
902244096ebdSEnji Cooper #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
test_session_cache_overflow(int idx)902344096ebdSEnji Cooper static int test_session_cache_overflow(int idx)
902444096ebdSEnji Cooper {
902544096ebdSEnji Cooper     SSL_CTX *sctx = NULL, *cctx = NULL;
902644096ebdSEnji Cooper     SSL *serverssl = NULL, *clientssl = NULL;
902744096ebdSEnji Cooper     int testresult = 0;
902844096ebdSEnji Cooper     SSL_SESSION *sess = NULL;
902944096ebdSEnji Cooper 
903044096ebdSEnji Cooper #ifdef OSSL_NO_USABLE_TLS1_3
903144096ebdSEnji Cooper     /* If no TLSv1.3 available then do nothing in this case */
903244096ebdSEnji Cooper     if (idx % 2 == 0)
903344096ebdSEnji Cooper         return TEST_skip("No TLSv1.3 available");
903444096ebdSEnji Cooper #endif
903544096ebdSEnji Cooper #ifdef OPENSSL_NO_TLS1_2
903644096ebdSEnji Cooper     /* If no TLSv1.2 available then do nothing in this case */
903744096ebdSEnji Cooper     if (idx % 2 == 1)
903844096ebdSEnji Cooper         return TEST_skip("No TLSv1.2 available");
903944096ebdSEnji Cooper #endif
904044096ebdSEnji Cooper 
904144096ebdSEnji Cooper     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
904244096ebdSEnji Cooper                                        TLS_client_method(), TLS1_VERSION,
904344096ebdSEnji Cooper                                        (idx % 2 == 0) ? TLS1_3_VERSION
904444096ebdSEnji Cooper                                                       : TLS1_2_VERSION,
904544096ebdSEnji Cooper                                        &sctx, &cctx, cert, privkey))
904644096ebdSEnji Cooper             || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)))
904744096ebdSEnji Cooper         goto end;
904844096ebdSEnji Cooper 
904944096ebdSEnji Cooper     SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
905044096ebdSEnji Cooper     get_sess_val = NULL;
905144096ebdSEnji Cooper 
905244096ebdSEnji Cooper     SSL_CTX_sess_set_cache_size(sctx, 1);
905344096ebdSEnji Cooper 
905444096ebdSEnji Cooper     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
905544096ebdSEnji Cooper                                       NULL, NULL)))
905644096ebdSEnji Cooper         goto end;
905744096ebdSEnji Cooper 
905844096ebdSEnji Cooper     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
905944096ebdSEnji Cooper         goto end;
906044096ebdSEnji Cooper 
906144096ebdSEnji Cooper     if (idx > 1) {
906244096ebdSEnji Cooper         sess = SSL_get_session(serverssl);
906344096ebdSEnji Cooper         if (!TEST_ptr(sess))
906444096ebdSEnji Cooper             goto end;
906544096ebdSEnji Cooper 
906644096ebdSEnji Cooper         /*
906744096ebdSEnji Cooper          * Cause this session to have a longer timeout than the next session to
906844096ebdSEnji Cooper          * be added.
906944096ebdSEnji Cooper          */
907044096ebdSEnji Cooper         if (!TEST_true(SSL_SESSION_set_timeout(sess, LONG_MAX / 2))) {
907144096ebdSEnji Cooper             sess = NULL;
907244096ebdSEnji Cooper             goto end;
907344096ebdSEnji Cooper         }
907444096ebdSEnji Cooper         sess = NULL;
907544096ebdSEnji Cooper     }
907644096ebdSEnji Cooper 
907744096ebdSEnji Cooper     SSL_shutdown(serverssl);
907844096ebdSEnji Cooper     SSL_shutdown(clientssl);
907944096ebdSEnji Cooper     SSL_free(serverssl);
908044096ebdSEnji Cooper     SSL_free(clientssl);
908144096ebdSEnji Cooper     serverssl = clientssl = NULL;
908244096ebdSEnji Cooper 
908344096ebdSEnji Cooper     /*
908444096ebdSEnji Cooper      * Session cache size is 1 and we already populated the cache with a session
908544096ebdSEnji Cooper      * so the next connection should cause an overflow.
908644096ebdSEnji Cooper      */
908744096ebdSEnji Cooper 
908844096ebdSEnji Cooper     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
908944096ebdSEnji Cooper                                       NULL, NULL)))
909044096ebdSEnji Cooper         goto end;
909144096ebdSEnji Cooper 
909244096ebdSEnji Cooper     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
909344096ebdSEnji Cooper         goto end;
909444096ebdSEnji Cooper 
909544096ebdSEnji Cooper     /*
909644096ebdSEnji Cooper      * The session we just negotiated may have been already removed from the
909744096ebdSEnji Cooper      * internal cache - but we will return it anyway from our external cache.
909844096ebdSEnji Cooper      */
909944096ebdSEnji Cooper     get_sess_val = SSL_get_session(serverssl);
910044096ebdSEnji Cooper     if (!TEST_ptr(get_sess_val))
910144096ebdSEnji Cooper         goto end;
910244096ebdSEnji Cooper     sess = SSL_get1_session(clientssl);
910344096ebdSEnji Cooper     if (!TEST_ptr(sess))
910444096ebdSEnji Cooper         goto end;
910544096ebdSEnji Cooper 
910644096ebdSEnji Cooper     SSL_shutdown(serverssl);
910744096ebdSEnji Cooper     SSL_shutdown(clientssl);
910844096ebdSEnji Cooper     SSL_free(serverssl);
910944096ebdSEnji Cooper     SSL_free(clientssl);
911044096ebdSEnji Cooper     serverssl = clientssl = NULL;
911144096ebdSEnji Cooper 
911244096ebdSEnji Cooper     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
911344096ebdSEnji Cooper                                       NULL, NULL)))
911444096ebdSEnji Cooper         goto end;
911544096ebdSEnji Cooper 
911644096ebdSEnji Cooper     if (!TEST_true(SSL_set_session(clientssl, sess)))
911744096ebdSEnji Cooper         goto end;
911844096ebdSEnji Cooper 
911944096ebdSEnji Cooper     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
912044096ebdSEnji Cooper         goto end;
912144096ebdSEnji Cooper 
912244096ebdSEnji Cooper     testresult = 1;
912344096ebdSEnji Cooper 
912444096ebdSEnji Cooper  end:
912544096ebdSEnji Cooper     SSL_free(serverssl);
912644096ebdSEnji Cooper     SSL_free(clientssl);
912744096ebdSEnji Cooper     SSL_CTX_free(sctx);
912844096ebdSEnji Cooper     SSL_CTX_free(cctx);
912944096ebdSEnji Cooper     SSL_SESSION_free(sess);
913044096ebdSEnji Cooper 
913144096ebdSEnji Cooper     return testresult;
913244096ebdSEnji Cooper }
913344096ebdSEnji Cooper #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
913444096ebdSEnji Cooper 
913544096ebdSEnji Cooper /*
9136e0c4386eSCy Schubert  * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
9137e0c4386eSCy Schubert  * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
9138e0c4386eSCy Schubert  * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
9139e0c4386eSCy Schubert  * Test 3: Client does not set servername on initial handshake (TLSv1.2)
9140e0c4386eSCy Schubert  * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
9141e0c4386eSCy Schubert  * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
9142e0c4386eSCy Schubert  * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
9143e0c4386eSCy Schubert  * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
9144e0c4386eSCy Schubert  * Test 8: Client does not set servername on initial handshake(TLSv1.3)
9145e0c4386eSCy Schubert  * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
9146e0c4386eSCy Schubert  */
test_servername(int tst)9147e0c4386eSCy Schubert static int test_servername(int tst)
9148e0c4386eSCy Schubert {
9149e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9150e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9151e0c4386eSCy Schubert     int testresult = 0;
9152e0c4386eSCy Schubert     SSL_SESSION *sess = NULL;
9153e0c4386eSCy Schubert     const char *sexpectedhost = NULL, *cexpectedhost = NULL;
9154e0c4386eSCy Schubert 
9155e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
9156e0c4386eSCy Schubert     if (tst <= 4)
9157e0c4386eSCy Schubert         return 1;
9158e0c4386eSCy Schubert #endif
9159e0c4386eSCy Schubert #ifdef OSSL_NO_USABLE_TLS1_3
9160e0c4386eSCy Schubert     if (tst >= 5)
9161e0c4386eSCy Schubert         return 1;
9162e0c4386eSCy Schubert #endif
9163e0c4386eSCy Schubert 
9164e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9165e0c4386eSCy Schubert                                        TLS_client_method(),
9166e0c4386eSCy Schubert                                        TLS1_VERSION,
9167e0c4386eSCy Schubert                                        (tst <= 4) ? TLS1_2_VERSION
9168e0c4386eSCy Schubert                                                   : TLS1_3_VERSION,
9169e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
9170e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9171e0c4386eSCy Schubert                                              NULL, NULL)))
9172e0c4386eSCy Schubert         goto end;
9173e0c4386eSCy Schubert 
9174e0c4386eSCy Schubert     if (tst != 1 && tst != 6) {
9175e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
9176e0c4386eSCy Schubert                                                               hostname_cb)))
9177e0c4386eSCy Schubert             goto end;
9178e0c4386eSCy Schubert     }
9179e0c4386eSCy Schubert 
9180e0c4386eSCy Schubert     if (tst != 3 && tst != 8) {
9181e0c4386eSCy Schubert         if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9182e0c4386eSCy Schubert             goto end;
9183e0c4386eSCy Schubert         sexpectedhost = cexpectedhost = "goodhost";
9184e0c4386eSCy Schubert     }
9185e0c4386eSCy Schubert 
9186e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9187e0c4386eSCy Schubert         goto end;
9188e0c4386eSCy Schubert 
9189e0c4386eSCy Schubert     if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
9190e0c4386eSCy Schubert                      cexpectedhost)
9191e0c4386eSCy Schubert             || !TEST_str_eq(SSL_get_servername(serverssl,
9192e0c4386eSCy Schubert                                                TLSEXT_NAMETYPE_host_name),
9193e0c4386eSCy Schubert                             sexpectedhost))
9194e0c4386eSCy Schubert         goto end;
9195e0c4386eSCy Schubert 
9196e0c4386eSCy Schubert     /* Now repeat with a resumption handshake */
9197e0c4386eSCy Schubert 
9198e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
9199e0c4386eSCy Schubert             || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
9200e0c4386eSCy Schubert             || !TEST_true(SSL_SESSION_is_resumable(sess))
9201e0c4386eSCy Schubert             || !TEST_int_eq(SSL_shutdown(serverssl), 0))
9202e0c4386eSCy Schubert         goto end;
9203e0c4386eSCy Schubert 
9204e0c4386eSCy Schubert     SSL_free(clientssl);
9205e0c4386eSCy Schubert     SSL_free(serverssl);
9206e0c4386eSCy Schubert     clientssl = serverssl = NULL;
9207e0c4386eSCy Schubert 
9208e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
9209e0c4386eSCy Schubert                                       NULL)))
9210e0c4386eSCy Schubert         goto end;
9211e0c4386eSCy Schubert 
9212e0c4386eSCy Schubert     if (!TEST_true(SSL_set_session(clientssl, sess)))
9213e0c4386eSCy Schubert         goto end;
9214e0c4386eSCy Schubert 
9215e0c4386eSCy Schubert     sexpectedhost = cexpectedhost = "goodhost";
9216e0c4386eSCy Schubert     if (tst == 2 || tst == 7) {
9217e0c4386eSCy Schubert         /* Set an inconsistent hostname */
9218e0c4386eSCy Schubert         if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
9219e0c4386eSCy Schubert             goto end;
9220e0c4386eSCy Schubert         /*
9221e0c4386eSCy Schubert          * In TLSv1.2 we expect the hostname from the original handshake, in
9222e0c4386eSCy Schubert          * TLSv1.3 we expect the hostname from this handshake
9223e0c4386eSCy Schubert          */
9224e0c4386eSCy Schubert         if (tst == 7)
9225e0c4386eSCy Schubert             sexpectedhost = cexpectedhost = "altgoodhost";
9226e0c4386eSCy Schubert 
9227e0c4386eSCy Schubert         if (!TEST_str_eq(SSL_get_servername(clientssl,
9228e0c4386eSCy Schubert                                             TLSEXT_NAMETYPE_host_name),
9229e0c4386eSCy Schubert                          "altgoodhost"))
9230e0c4386eSCy Schubert             goto end;
9231e0c4386eSCy Schubert     } else if (tst == 4 || tst == 9) {
9232e0c4386eSCy Schubert         /*
9233e0c4386eSCy Schubert          * A TLSv1.3 session does not associate a session with a servername,
9234e0c4386eSCy Schubert          * but a TLSv1.2 session does.
9235e0c4386eSCy Schubert          */
9236e0c4386eSCy Schubert         if (tst == 9)
9237e0c4386eSCy Schubert             sexpectedhost = cexpectedhost = NULL;
9238e0c4386eSCy Schubert 
9239e0c4386eSCy Schubert         if (!TEST_str_eq(SSL_get_servername(clientssl,
9240e0c4386eSCy Schubert                                             TLSEXT_NAMETYPE_host_name),
9241e0c4386eSCy Schubert                          cexpectedhost))
9242e0c4386eSCy Schubert             goto end;
9243e0c4386eSCy Schubert     } else {
9244e0c4386eSCy Schubert         if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9245e0c4386eSCy Schubert             goto end;
9246e0c4386eSCy Schubert         /*
9247e0c4386eSCy Schubert          * In a TLSv1.2 resumption where the hostname was not acknowledged
9248e0c4386eSCy Schubert          * we expect the hostname on the server to be empty. On the client we
9249e0c4386eSCy Schubert          * return what was requested in this case.
9250e0c4386eSCy Schubert          *
9251e0c4386eSCy Schubert          * Similarly if the client didn't set a hostname on an original TLSv1.2
9252e0c4386eSCy Schubert          * session but is now, the server hostname will be empty, but the client
9253e0c4386eSCy Schubert          * is as we set it.
9254e0c4386eSCy Schubert          */
9255e0c4386eSCy Schubert         if (tst == 1 || tst == 3)
9256e0c4386eSCy Schubert             sexpectedhost = NULL;
9257e0c4386eSCy Schubert 
9258e0c4386eSCy Schubert         if (!TEST_str_eq(SSL_get_servername(clientssl,
9259e0c4386eSCy Schubert                                             TLSEXT_NAMETYPE_host_name),
9260e0c4386eSCy Schubert                          "goodhost"))
9261e0c4386eSCy Schubert             goto end;
9262e0c4386eSCy Schubert     }
9263e0c4386eSCy Schubert 
9264e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9265e0c4386eSCy Schubert         goto end;
9266e0c4386eSCy Schubert 
9267e0c4386eSCy Schubert     if (!TEST_true(SSL_session_reused(clientssl))
9268e0c4386eSCy Schubert             || !TEST_true(SSL_session_reused(serverssl))
9269e0c4386eSCy Schubert             || !TEST_str_eq(SSL_get_servername(clientssl,
9270e0c4386eSCy Schubert                                                TLSEXT_NAMETYPE_host_name),
9271e0c4386eSCy Schubert                             cexpectedhost)
9272e0c4386eSCy Schubert             || !TEST_str_eq(SSL_get_servername(serverssl,
9273e0c4386eSCy Schubert                                                TLSEXT_NAMETYPE_host_name),
9274e0c4386eSCy Schubert                             sexpectedhost))
9275e0c4386eSCy Schubert         goto end;
9276e0c4386eSCy Schubert 
9277e0c4386eSCy Schubert     testresult = 1;
9278e0c4386eSCy Schubert 
9279e0c4386eSCy Schubert  end:
9280e0c4386eSCy Schubert     SSL_SESSION_free(sess);
9281e0c4386eSCy Schubert     SSL_free(serverssl);
9282e0c4386eSCy Schubert     SSL_free(clientssl);
9283e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9284e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9285e0c4386eSCy Schubert 
9286e0c4386eSCy Schubert     return testresult;
9287e0c4386eSCy Schubert }
9288e0c4386eSCy Schubert 
9289e0c4386eSCy Schubert #if !defined(OPENSSL_NO_EC) \
9290e0c4386eSCy Schubert     && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9291e0c4386eSCy Schubert /*
9292e0c4386eSCy Schubert  * Test that if signature algorithms are not available, then we do not offer or
9293e0c4386eSCy Schubert  * accept them.
9294e0c4386eSCy Schubert  * Test 0: Two RSA sig algs available: both RSA sig algs shared
9295e0c4386eSCy Schubert  * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
9296e0c4386eSCy Schubert  * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
9297e0c4386eSCy Schubert  * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
9298e0c4386eSCy Schubert  * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
9299e0c4386eSCy Schubert  * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
9300e0c4386eSCy Schubert  */
test_sigalgs_available(int idx)9301e0c4386eSCy Schubert static int test_sigalgs_available(int idx)
9302e0c4386eSCy Schubert {
9303e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9304e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9305e0c4386eSCy Schubert     int testresult = 0;
9306e0c4386eSCy Schubert     OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
9307e0c4386eSCy Schubert     OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
9308e0c4386eSCy Schubert     OSSL_PROVIDER *filterprov = NULL;
9309e0c4386eSCy Schubert     int sig, hash;
9310e0c4386eSCy Schubert 
9311e0c4386eSCy Schubert     if (!TEST_ptr(tmpctx))
9312e0c4386eSCy Schubert         goto end;
9313e0c4386eSCy Schubert 
9314e0c4386eSCy Schubert     if (idx != 0 && idx != 3) {
9315e0c4386eSCy Schubert         if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
9316e0c4386eSCy Schubert                                                  filter_provider_init)))
9317e0c4386eSCy Schubert             goto end;
9318e0c4386eSCy Schubert 
9319e0c4386eSCy Schubert         filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
9320e0c4386eSCy Schubert         if (!TEST_ptr(filterprov))
9321e0c4386eSCy Schubert             goto end;
9322e0c4386eSCy Schubert 
9323e0c4386eSCy Schubert         if (idx < 3) {
9324e0c4386eSCy Schubert             /*
9325e0c4386eSCy Schubert              * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
9326e0c4386eSCy Schubert              * or accepted for the peer that uses this libctx. Note that libssl
9327e0c4386eSCy Schubert              * *requires* SHA2-256 to be available so we cannot disable that. We
9328e0c4386eSCy Schubert              * also need SHA1 for our certificate.
9329e0c4386eSCy Schubert              */
9330e0c4386eSCy Schubert             if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
9331e0c4386eSCy Schubert                                                       "SHA2-256:SHA1")))
9332e0c4386eSCy Schubert                 goto end;
9333e0c4386eSCy Schubert         } else {
9334e0c4386eSCy Schubert             if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
9335e0c4386eSCy Schubert                                                       "ECDSA"))
9336e0c4386eSCy Schubert                     || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
9337e0c4386eSCy Schubert                                                              "EC:X25519:X448")))
9338e0c4386eSCy Schubert                 goto end;
9339e0c4386eSCy Schubert         }
9340e0c4386eSCy Schubert 
9341e0c4386eSCy Schubert         if (idx == 1 || idx == 4)
9342e0c4386eSCy Schubert             clientctx = tmpctx;
9343e0c4386eSCy Schubert         else
9344e0c4386eSCy Schubert             serverctx = tmpctx;
9345e0c4386eSCy Schubert     }
9346e0c4386eSCy Schubert 
9347e0c4386eSCy Schubert     cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
9348e0c4386eSCy Schubert     sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
9349e0c4386eSCy Schubert     if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
9350e0c4386eSCy Schubert         goto end;
9351e0c4386eSCy Schubert 
9352e0c4386eSCy Schubert     if (idx != 5) {
9353e0c4386eSCy Schubert         if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9354e0c4386eSCy Schubert                                            TLS_client_method(),
9355e0c4386eSCy Schubert                                            TLS1_VERSION,
9356e0c4386eSCy Schubert                                            0,
9357e0c4386eSCy Schubert                                            &sctx, &cctx, cert, privkey)))
9358e0c4386eSCy Schubert             goto end;
9359e0c4386eSCy Schubert     } else {
9360e0c4386eSCy Schubert         if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9361e0c4386eSCy Schubert                                            TLS_client_method(),
9362e0c4386eSCy Schubert                                            TLS1_VERSION,
9363e0c4386eSCy Schubert                                            0,
9364e0c4386eSCy Schubert                                            &sctx, &cctx, cert2, privkey2)))
9365e0c4386eSCy Schubert             goto end;
9366e0c4386eSCy Schubert     }
9367e0c4386eSCy Schubert 
9368e0c4386eSCy Schubert     /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
9369e0c4386eSCy Schubert     if (idx < 4) {
9370e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9371e0c4386eSCy Schubert                                                "ECDHE-RSA-AES128-GCM-SHA256")))
9372e0c4386eSCy Schubert             goto end;
9373e0c4386eSCy Schubert     } else {
9374e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9375e0c4386eSCy Schubert                                                "ECDHE-ECDSA-AES128-GCM-SHA256")))
9376e0c4386eSCy Schubert             goto end;
9377e0c4386eSCy Schubert     }
9378e0c4386eSCy Schubert 
9379e0c4386eSCy Schubert     if (idx < 3) {
9380e0c4386eSCy Schubert         if (!SSL_CTX_set1_sigalgs_list(cctx,
9381e0c4386eSCy Schubert                                        "rsa_pss_rsae_sha384"
9382e0c4386eSCy Schubert                                        ":rsa_pss_rsae_sha256")
9383e0c4386eSCy Schubert                 || !SSL_CTX_set1_sigalgs_list(sctx,
9384e0c4386eSCy Schubert                                               "rsa_pss_rsae_sha384"
9385e0c4386eSCy Schubert                                               ":rsa_pss_rsae_sha256"))
9386e0c4386eSCy Schubert             goto end;
9387e0c4386eSCy Schubert     } else {
9388e0c4386eSCy Schubert         if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
9389e0c4386eSCy Schubert                 || !SSL_CTX_set1_sigalgs_list(sctx,
9390e0c4386eSCy Schubert                                               "rsa_pss_rsae_sha256:ECDSA+SHA256"))
9391e0c4386eSCy Schubert             goto end;
9392e0c4386eSCy Schubert     }
9393e0c4386eSCy Schubert 
9394e0c4386eSCy Schubert     if (idx != 5
9395e0c4386eSCy Schubert         && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
9396e0c4386eSCy Schubert                                                       SSL_FILETYPE_PEM), 1)
9397e0c4386eSCy Schubert             || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
9398e0c4386eSCy Schubert                                                         privkey2,
9399e0c4386eSCy Schubert                                                         SSL_FILETYPE_PEM), 1)
9400e0c4386eSCy Schubert             || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
9401e0c4386eSCy Schubert         goto end;
9402e0c4386eSCy Schubert 
9403e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9404e0c4386eSCy Schubert                                       NULL, NULL)))
9405e0c4386eSCy Schubert         goto end;
9406e0c4386eSCy Schubert 
9407e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9408e0c4386eSCy Schubert         goto end;
9409e0c4386eSCy Schubert 
9410e0c4386eSCy Schubert     /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
9411e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
9412e0c4386eSCy Schubert                                             NULL, NULL),
9413e0c4386eSCy Schubert                      (idx == 0 || idx == 3) ? 2 : 1))
9414e0c4386eSCy Schubert         goto end;
9415e0c4386eSCy Schubert 
9416e0c4386eSCy Schubert     if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
9417e0c4386eSCy Schubert         goto end;
9418e0c4386eSCy Schubert 
9419e0c4386eSCy Schubert     if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
9420e0c4386eSCy Schubert                                                  : NID_rsassaPss))
9421e0c4386eSCy Schubert         goto end;
9422e0c4386eSCy Schubert 
9423e0c4386eSCy Schubert     testresult = filter_provider_check_clean_finish();
9424e0c4386eSCy Schubert 
9425e0c4386eSCy Schubert  end:
9426e0c4386eSCy Schubert     SSL_free(serverssl);
9427e0c4386eSCy Schubert     SSL_free(clientssl);
9428e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9429e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9430e0c4386eSCy Schubert     OSSL_PROVIDER_unload(filterprov);
9431e0c4386eSCy Schubert     OSSL_LIB_CTX_free(tmpctx);
9432e0c4386eSCy Schubert 
9433e0c4386eSCy Schubert     return testresult;
9434e0c4386eSCy Schubert }
9435e0c4386eSCy Schubert #endif /*
9436e0c4386eSCy Schubert         * !defined(OPENSSL_NO_EC) \
9437e0c4386eSCy Schubert         * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9438e0c4386eSCy Schubert         */
9439e0c4386eSCy Schubert 
9440e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_3
9441e0c4386eSCy Schubert /* This test can run in TLSv1.3 even if ec and dh are disabled */
test_pluggable_group(int idx)9442e0c4386eSCy Schubert static int test_pluggable_group(int idx)
9443e0c4386eSCy Schubert {
9444e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9445e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9446e0c4386eSCy Schubert     int testresult = 0;
9447e0c4386eSCy Schubert     OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
9448e0c4386eSCy Schubert     /* Check that we are not impacted by a provider without any groups */
9449e0c4386eSCy Schubert     OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
945044096ebdSEnji Cooper     const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup";
9451e0c4386eSCy Schubert 
9452e0c4386eSCy Schubert     if (!TEST_ptr(tlsprov))
9453e0c4386eSCy Schubert         goto end;
9454e0c4386eSCy Schubert 
9455e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9456e0c4386eSCy Schubert                                        TLS_client_method(),
9457e0c4386eSCy Schubert                                        TLS1_3_VERSION,
9458e0c4386eSCy Schubert                                        TLS1_3_VERSION,
9459e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey))
9460e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9461e0c4386eSCy Schubert                                              NULL, NULL)))
9462e0c4386eSCy Schubert         goto end;
9463e0c4386eSCy Schubert 
946444096ebdSEnji Cooper     /* ensure GROUPLIST_INCREMENT (=40) logic triggers: */
946544096ebdSEnji Cooper     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"))
946644096ebdSEnji Cooper     /* removing a single algorithm from the list makes the test pass */
9467e0c4386eSCy Schubert             || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
9468e0c4386eSCy Schubert         goto end;
9469e0c4386eSCy Schubert 
9470e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9471e0c4386eSCy Schubert         goto end;
9472e0c4386eSCy Schubert 
9473e0c4386eSCy Schubert     if (!TEST_str_eq(group_name,
9474e0c4386eSCy Schubert                      SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
9475e0c4386eSCy Schubert         goto end;
9476e0c4386eSCy Schubert 
9477e0c4386eSCy Schubert     testresult = 1;
9478e0c4386eSCy Schubert 
9479e0c4386eSCy Schubert  end:
9480e0c4386eSCy Schubert     SSL_free(serverssl);
9481e0c4386eSCy Schubert     SSL_free(clientssl);
9482e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9483e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9484e0c4386eSCy Schubert     OSSL_PROVIDER_unload(tlsprov);
9485e0c4386eSCy Schubert     OSSL_PROVIDER_unload(legacyprov);
9486e0c4386eSCy Schubert 
9487e0c4386eSCy Schubert     return testresult;
9488e0c4386eSCy Schubert }
9489e0c4386eSCy Schubert #endif
9490e0c4386eSCy Schubert 
9491e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
test_ssl_dup(void)9492e0c4386eSCy Schubert static int test_ssl_dup(void)
9493e0c4386eSCy Schubert {
9494e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9495e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
9496e0c4386eSCy Schubert     int testresult = 0;
9497e0c4386eSCy Schubert     BIO *rbio = NULL, *wbio = NULL;
9498e0c4386eSCy Schubert 
9499e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9500e0c4386eSCy Schubert                                        TLS_client_method(),
9501e0c4386eSCy Schubert                                        0,
9502e0c4386eSCy Schubert                                        0,
9503e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
9504e0c4386eSCy Schubert         goto end;
9505e0c4386eSCy Schubert 
9506e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9507e0c4386eSCy Schubert                                              NULL, NULL)))
9508e0c4386eSCy Schubert         goto end;
9509e0c4386eSCy Schubert 
9510e0c4386eSCy Schubert     if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
9511e0c4386eSCy Schubert             || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
9512e0c4386eSCy Schubert         goto end;
9513e0c4386eSCy Schubert 
9514e0c4386eSCy Schubert     client2ssl = SSL_dup(clientssl);
9515e0c4386eSCy Schubert     rbio = SSL_get_rbio(clientssl);
9516e0c4386eSCy Schubert     if (!TEST_ptr(rbio)
9517e0c4386eSCy Schubert             || !TEST_true(BIO_up_ref(rbio)))
9518e0c4386eSCy Schubert         goto end;
9519e0c4386eSCy Schubert     SSL_set0_rbio(client2ssl, rbio);
9520e0c4386eSCy Schubert     rbio = NULL;
9521e0c4386eSCy Schubert 
9522e0c4386eSCy Schubert     wbio = SSL_get_wbio(clientssl);
9523e0c4386eSCy Schubert     if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
9524e0c4386eSCy Schubert         goto end;
9525e0c4386eSCy Schubert     SSL_set0_wbio(client2ssl, wbio);
9526e0c4386eSCy Schubert     rbio = NULL;
9527e0c4386eSCy Schubert 
9528e0c4386eSCy Schubert     if (!TEST_ptr(client2ssl)
9529e0c4386eSCy Schubert                /* Handshake not started so pointers should be different */
9530e0c4386eSCy Schubert             || !TEST_ptr_ne(clientssl, client2ssl))
9531e0c4386eSCy Schubert         goto end;
9532e0c4386eSCy Schubert 
9533e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
9534e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
9535e0c4386eSCy Schubert         goto end;
9536e0c4386eSCy Schubert 
9537e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
9538e0c4386eSCy Schubert         goto end;
9539e0c4386eSCy Schubert 
9540e0c4386eSCy Schubert     SSL_free(clientssl);
9541e0c4386eSCy Schubert     clientssl = SSL_dup(client2ssl);
9542e0c4386eSCy Schubert     if (!TEST_ptr(clientssl)
9543e0c4386eSCy Schubert                /* Handshake has finished so pointers should be the same */
9544e0c4386eSCy Schubert             || !TEST_ptr_eq(clientssl, client2ssl))
9545e0c4386eSCy Schubert         goto end;
9546e0c4386eSCy Schubert 
9547e0c4386eSCy Schubert     testresult = 1;
9548e0c4386eSCy Schubert 
9549e0c4386eSCy Schubert  end:
9550e0c4386eSCy Schubert     SSL_free(serverssl);
9551e0c4386eSCy Schubert     SSL_free(clientssl);
9552e0c4386eSCy Schubert     SSL_free(client2ssl);
9553e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9554e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9555e0c4386eSCy Schubert 
9556e0c4386eSCy Schubert     return testresult;
9557e0c4386eSCy Schubert }
9558e0c4386eSCy Schubert 
9559e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
9560e0c4386eSCy Schubert 
9561e0c4386eSCy Schubert static EVP_PKEY *tmp_dh_params = NULL;
9562e0c4386eSCy Schubert 
9563e0c4386eSCy Schubert /* Helper function for the test_set_tmp_dh() tests */
get_tmp_dh_params(void)9564e0c4386eSCy Schubert static EVP_PKEY *get_tmp_dh_params(void)
9565e0c4386eSCy Schubert {
9566e0c4386eSCy Schubert     if (tmp_dh_params == NULL) {
9567e0c4386eSCy Schubert         BIGNUM *p = NULL;
9568e0c4386eSCy Schubert         OSSL_PARAM_BLD *tmpl = NULL;
9569e0c4386eSCy Schubert         EVP_PKEY_CTX *pctx = NULL;
9570e0c4386eSCy Schubert         OSSL_PARAM *params = NULL;
9571e0c4386eSCy Schubert         EVP_PKEY *dhpkey = NULL;
9572e0c4386eSCy Schubert 
9573e0c4386eSCy Schubert         p = BN_get_rfc3526_prime_2048(NULL);
9574e0c4386eSCy Schubert         if (!TEST_ptr(p))
9575e0c4386eSCy Schubert             goto end;
9576e0c4386eSCy Schubert 
9577e0c4386eSCy Schubert         pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
9578e0c4386eSCy Schubert         if (!TEST_ptr(pctx)
9579e0c4386eSCy Schubert                 || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
9580e0c4386eSCy Schubert             goto end;
9581e0c4386eSCy Schubert 
9582e0c4386eSCy Schubert         tmpl = OSSL_PARAM_BLD_new();
9583e0c4386eSCy Schubert         if (!TEST_ptr(tmpl)
9584e0c4386eSCy Schubert                 || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
9585e0c4386eSCy Schubert                                                         OSSL_PKEY_PARAM_FFC_P,
9586e0c4386eSCy Schubert                                                         p))
9587e0c4386eSCy Schubert                 || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
9588e0c4386eSCy Schubert                                                         OSSL_PKEY_PARAM_FFC_G,
9589e0c4386eSCy Schubert                                                         2)))
9590e0c4386eSCy Schubert             goto end;
9591e0c4386eSCy Schubert 
9592e0c4386eSCy Schubert         params = OSSL_PARAM_BLD_to_param(tmpl);
9593e0c4386eSCy Schubert         if (!TEST_ptr(params)
9594e0c4386eSCy Schubert                 || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
9595e0c4386eSCy Schubert                                                   EVP_PKEY_KEY_PARAMETERS,
9596e0c4386eSCy Schubert                                                   params), 1))
9597e0c4386eSCy Schubert             goto end;
9598e0c4386eSCy Schubert 
9599e0c4386eSCy Schubert         tmp_dh_params = dhpkey;
9600e0c4386eSCy Schubert     end:
9601e0c4386eSCy Schubert         BN_free(p);
9602e0c4386eSCy Schubert         EVP_PKEY_CTX_free(pctx);
9603e0c4386eSCy Schubert         OSSL_PARAM_BLD_free(tmpl);
9604e0c4386eSCy Schubert         OSSL_PARAM_free(params);
9605e0c4386eSCy Schubert     }
9606e0c4386eSCy Schubert 
9607e0c4386eSCy Schubert     if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
9608e0c4386eSCy Schubert         return NULL;
9609e0c4386eSCy Schubert 
9610e0c4386eSCy Schubert     return tmp_dh_params;
9611e0c4386eSCy Schubert }
9612e0c4386eSCy Schubert 
9613e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9614e0c4386eSCy Schubert /* Callback used by test_set_tmp_dh() */
tmp_dh_callback(SSL * s,int is_export,int keylen)9615e0c4386eSCy Schubert static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
9616e0c4386eSCy Schubert {
9617e0c4386eSCy Schubert     EVP_PKEY *dhpkey = get_tmp_dh_params();
9618e0c4386eSCy Schubert     DH *ret = NULL;
9619e0c4386eSCy Schubert 
9620e0c4386eSCy Schubert     if (!TEST_ptr(dhpkey))
9621e0c4386eSCy Schubert         return NULL;
9622e0c4386eSCy Schubert 
9623e0c4386eSCy Schubert     /*
9624e0c4386eSCy Schubert      * libssl does not free the returned DH, so we free it now knowing that even
9625e0c4386eSCy Schubert      * after we free dhpkey, there will still be a reference to the owning
9626e0c4386eSCy Schubert      * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
9627e0c4386eSCy Schubert      * of time we need it for.
9628e0c4386eSCy Schubert      */
9629e0c4386eSCy Schubert     ret = EVP_PKEY_get1_DH(dhpkey);
9630e0c4386eSCy Schubert     DH_free(ret);
9631e0c4386eSCy Schubert 
9632e0c4386eSCy Schubert     EVP_PKEY_free(dhpkey);
9633e0c4386eSCy Schubert 
9634e0c4386eSCy Schubert     return ret;
9635e0c4386eSCy Schubert }
9636e0c4386eSCy Schubert #  endif
9637e0c4386eSCy Schubert 
9638e0c4386eSCy Schubert /*
9639e0c4386eSCy Schubert  * Test the various methods for setting temporary DH parameters
9640e0c4386eSCy Schubert  *
9641e0c4386eSCy Schubert  * Test  0: Default (no auto) setting
9642e0c4386eSCy Schubert  * Test  1: Explicit SSL_CTX auto off
9643e0c4386eSCy Schubert  * Test  2: Explicit SSL auto off
9644e0c4386eSCy Schubert  * Test  3: Explicit SSL_CTX auto on
9645e0c4386eSCy Schubert  * Test  4: Explicit SSL auto on
9646e0c4386eSCy Schubert  * Test  5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
9647e0c4386eSCy Schubert  * Test  6: Explicit SSL auto off, custom DH params via EVP_PKEY
9648e0c4386eSCy Schubert  *
9649e0c4386eSCy Schubert  * The following are testing deprecated APIs, so we only run them if available
9650e0c4386eSCy Schubert  * Test  7: Explicit SSL_CTX auto off, custom DH params via DH
9651e0c4386eSCy Schubert  * Test  8: Explicit SSL auto off, custom DH params via DH
9652e0c4386eSCy Schubert  * Test  9: Explicit SSL_CTX auto off, custom DH params via callback
9653e0c4386eSCy Schubert  * Test 10: Explicit SSL auto off, custom DH params via callback
9654e0c4386eSCy Schubert  */
test_set_tmp_dh(int idx)9655e0c4386eSCy Schubert static int test_set_tmp_dh(int idx)
9656e0c4386eSCy Schubert {
9657e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9658e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9659e0c4386eSCy Schubert     int testresult = 0;
9660e0c4386eSCy Schubert     int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
9661e0c4386eSCy Schubert     int expected = (idx <= 2) ? 0 : 1;
9662e0c4386eSCy Schubert     EVP_PKEY *dhpkey = NULL;
9663e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9664e0c4386eSCy Schubert     DH *dh = NULL;
9665e0c4386eSCy Schubert #  else
9666e0c4386eSCy Schubert 
9667e0c4386eSCy Schubert     if (idx >= 7)
9668e0c4386eSCy Schubert         return 1;
9669e0c4386eSCy Schubert #  endif
9670e0c4386eSCy Schubert 
9671e0c4386eSCy Schubert     if (idx >= 5 && idx <= 8) {
9672e0c4386eSCy Schubert         dhpkey = get_tmp_dh_params();
9673e0c4386eSCy Schubert         if (!TEST_ptr(dhpkey))
9674e0c4386eSCy Schubert             goto end;
9675e0c4386eSCy Schubert     }
9676e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9677e0c4386eSCy Schubert     if (idx == 7 || idx == 8) {
9678e0c4386eSCy Schubert         dh = EVP_PKEY_get1_DH(dhpkey);
9679e0c4386eSCy Schubert         if (!TEST_ptr(dh))
9680e0c4386eSCy Schubert             goto end;
9681e0c4386eSCy Schubert     }
9682e0c4386eSCy Schubert #  endif
9683e0c4386eSCy Schubert 
9684e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9685e0c4386eSCy Schubert                                        TLS_client_method(),
9686e0c4386eSCy Schubert                                        0,
9687e0c4386eSCy Schubert                                        0,
9688e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
9689e0c4386eSCy Schubert         goto end;
9690e0c4386eSCy Schubert 
9691e0c4386eSCy Schubert     if ((idx & 1) == 1) {
9692e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
9693e0c4386eSCy Schubert             goto end;
9694e0c4386eSCy Schubert     }
9695e0c4386eSCy Schubert 
9696e0c4386eSCy Schubert     if (idx == 5) {
9697e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
9698e0c4386eSCy Schubert             goto end;
9699e0c4386eSCy Schubert         dhpkey = NULL;
9700e0c4386eSCy Schubert     }
9701e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9702e0c4386eSCy Schubert     else if (idx == 7) {
9703e0c4386eSCy Schubert         if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
9704e0c4386eSCy Schubert             goto end;
9705e0c4386eSCy Schubert     } else if (idx == 9) {
9706e0c4386eSCy Schubert         SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
9707e0c4386eSCy Schubert     }
9708e0c4386eSCy Schubert #  endif
9709e0c4386eSCy Schubert 
9710e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9711e0c4386eSCy Schubert                                       NULL, NULL)))
9712e0c4386eSCy Schubert         goto end;
9713e0c4386eSCy Schubert 
9714e0c4386eSCy Schubert     if ((idx & 1) == 0 && idx != 0) {
9715e0c4386eSCy Schubert         if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
9716e0c4386eSCy Schubert             goto end;
9717e0c4386eSCy Schubert     }
9718e0c4386eSCy Schubert     if (idx == 6) {
9719e0c4386eSCy Schubert         if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
9720e0c4386eSCy Schubert             goto end;
9721e0c4386eSCy Schubert         dhpkey = NULL;
9722e0c4386eSCy Schubert     }
9723e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9724e0c4386eSCy Schubert     else if (idx == 8) {
9725e0c4386eSCy Schubert         if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
9726e0c4386eSCy Schubert             goto end;
9727e0c4386eSCy Schubert     } else if (idx == 10) {
9728e0c4386eSCy Schubert         SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
9729e0c4386eSCy Schubert     }
9730e0c4386eSCy Schubert #  endif
9731e0c4386eSCy Schubert 
9732e0c4386eSCy Schubert     if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9733e0c4386eSCy Schubert             || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9734e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
9735e0c4386eSCy Schubert         goto end;
9736e0c4386eSCy Schubert 
9737e0c4386eSCy Schubert     /*
9738e0c4386eSCy Schubert      * If autoon then we should succeed. Otherwise we expect failure because
9739e0c4386eSCy Schubert      * there are no parameters
9740e0c4386eSCy Schubert      */
9741e0c4386eSCy Schubert     if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
9742e0c4386eSCy Schubert                                            SSL_ERROR_NONE), expected))
9743e0c4386eSCy Schubert         goto end;
9744e0c4386eSCy Schubert 
9745e0c4386eSCy Schubert     testresult = 1;
9746e0c4386eSCy Schubert 
9747e0c4386eSCy Schubert  end:
9748e0c4386eSCy Schubert #  ifndef OPENSSL_NO_DEPRECATED_3_0
9749e0c4386eSCy Schubert     DH_free(dh);
9750e0c4386eSCy Schubert #  endif
9751e0c4386eSCy Schubert     SSL_free(serverssl);
9752e0c4386eSCy Schubert     SSL_free(clientssl);
9753e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9754e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9755e0c4386eSCy Schubert     EVP_PKEY_free(dhpkey);
9756e0c4386eSCy Schubert 
9757e0c4386eSCy Schubert     return testresult;
9758e0c4386eSCy Schubert }
9759e0c4386eSCy Schubert 
9760e0c4386eSCy Schubert /*
9761e0c4386eSCy Schubert  * Test the auto DH keys are appropriately sized
9762e0c4386eSCy Schubert  */
test_dh_auto(int idx)9763e0c4386eSCy Schubert static int test_dh_auto(int idx)
9764e0c4386eSCy Schubert {
9765e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9766e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9767e0c4386eSCy Schubert     int testresult = 0;
9768e0c4386eSCy Schubert     EVP_PKEY *tmpkey = NULL;
9769e0c4386eSCy Schubert     char *thiscert = NULL, *thiskey = NULL;
9770e0c4386eSCy Schubert     size_t expdhsize = 0;
9771e0c4386eSCy Schubert     const char *ciphersuite = "DHE-RSA-AES128-SHA";
9772e0c4386eSCy Schubert 
9773e0c4386eSCy Schubert     switch (idx) {
9774e0c4386eSCy Schubert     case 0:
9775e0c4386eSCy Schubert         /* The FIPS provider doesn't support this DH size - so we ignore it */
9776e0c4386eSCy Schubert         if (is_fips)
9777e0c4386eSCy Schubert             return 1;
9778e0c4386eSCy Schubert         thiscert = cert1024;
9779e0c4386eSCy Schubert         thiskey = privkey1024;
9780e0c4386eSCy Schubert         expdhsize = 1024;
9781e0c4386eSCy Schubert         break;
9782e0c4386eSCy Schubert     case 1:
9783e0c4386eSCy Schubert         /* 2048 bit prime */
9784e0c4386eSCy Schubert         thiscert = cert;
9785e0c4386eSCy Schubert         thiskey = privkey;
9786e0c4386eSCy Schubert         expdhsize = 2048;
9787e0c4386eSCy Schubert         break;
9788e0c4386eSCy Schubert     case 2:
9789e0c4386eSCy Schubert         thiscert = cert3072;
9790e0c4386eSCy Schubert         thiskey = privkey3072;
9791e0c4386eSCy Schubert         expdhsize = 3072;
9792e0c4386eSCy Schubert         break;
9793e0c4386eSCy Schubert     case 3:
9794e0c4386eSCy Schubert         thiscert = cert4096;
9795e0c4386eSCy Schubert         thiskey = privkey4096;
9796e0c4386eSCy Schubert         expdhsize = 4096;
9797e0c4386eSCy Schubert         break;
9798e0c4386eSCy Schubert     case 4:
9799e0c4386eSCy Schubert         thiscert = cert8192;
9800e0c4386eSCy Schubert         thiskey = privkey8192;
9801e0c4386eSCy Schubert         expdhsize = 8192;
9802e0c4386eSCy Schubert         break;
9803e0c4386eSCy Schubert     /* No certificate cases */
9804e0c4386eSCy Schubert     case 5:
9805e0c4386eSCy Schubert         /* The FIPS provider doesn't support this DH size - so we ignore it */
9806e0c4386eSCy Schubert         if (is_fips)
9807e0c4386eSCy Schubert             return 1;
9808e0c4386eSCy Schubert         ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
9809e0c4386eSCy Schubert         expdhsize = 1024;
9810e0c4386eSCy Schubert         break;
9811e0c4386eSCy Schubert     case 6:
9812e0c4386eSCy Schubert         ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
9813e0c4386eSCy Schubert         expdhsize = 3072;
9814e0c4386eSCy Schubert         break;
9815e0c4386eSCy Schubert     default:
9816e0c4386eSCy Schubert         TEST_error("Invalid text index");
9817e0c4386eSCy Schubert         goto end;
9818e0c4386eSCy Schubert     }
9819e0c4386eSCy Schubert 
9820e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9821e0c4386eSCy Schubert                                        TLS_client_method(),
9822e0c4386eSCy Schubert                                        0,
9823e0c4386eSCy Schubert                                        0,
9824e0c4386eSCy Schubert                                        &sctx, &cctx, thiscert, thiskey)))
9825e0c4386eSCy Schubert         goto end;
9826e0c4386eSCy Schubert 
9827e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9828e0c4386eSCy Schubert                                       NULL, NULL)))
9829e0c4386eSCy Schubert         goto end;
9830e0c4386eSCy Schubert 
9831e0c4386eSCy Schubert     if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
9832e0c4386eSCy Schubert             || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9833e0c4386eSCy Schubert             || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9834e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
9835e0c4386eSCy Schubert             || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
9836e0c4386eSCy Schubert         goto end;
9837e0c4386eSCy Schubert 
9838e0c4386eSCy Schubert     /*
9839e0c4386eSCy Schubert      * Send the server's first flight. At this point the server has created the
9840e0c4386eSCy Schubert      * temporary DH key but hasn't finished using it yet. Once used it is
9841e0c4386eSCy Schubert      * removed, so we cannot test it.
9842e0c4386eSCy Schubert      */
9843e0c4386eSCy Schubert     if (!TEST_int_le(SSL_connect(clientssl), 0)
9844e0c4386eSCy Schubert             || !TEST_int_le(SSL_accept(serverssl), 0))
9845e0c4386eSCy Schubert         goto end;
9846e0c4386eSCy Schubert 
9847e0c4386eSCy Schubert     if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
9848e0c4386eSCy Schubert         goto end;
9849e0c4386eSCy Schubert     if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
9850e0c4386eSCy Schubert         goto end;
9851e0c4386eSCy Schubert 
9852e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9853e0c4386eSCy Schubert         goto end;
9854e0c4386eSCy Schubert 
9855e0c4386eSCy Schubert     testresult = 1;
9856e0c4386eSCy Schubert 
9857e0c4386eSCy Schubert  end:
9858e0c4386eSCy Schubert     SSL_free(serverssl);
9859e0c4386eSCy Schubert     SSL_free(clientssl);
9860e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9861e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9862e0c4386eSCy Schubert     EVP_PKEY_free(tmpkey);
9863e0c4386eSCy Schubert 
9864e0c4386eSCy Schubert     return testresult;
9865e0c4386eSCy Schubert 
9866e0c4386eSCy Schubert }
9867e0c4386eSCy Schubert # endif /* OPENSSL_NO_DH */
9868e0c4386eSCy Schubert #endif /* OPENSSL_NO_TLS1_2 */
9869e0c4386eSCy Schubert 
9870e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
9871e0c4386eSCy Schubert /*
9872e0c4386eSCy Schubert  * Test that setting an SNI callback works with TLSv1.3. Specifically we check
9873e0c4386eSCy Schubert  * that it works even without a certificate configured for the original
9874e0c4386eSCy Schubert  * SSL_CTX
9875e0c4386eSCy Schubert  */
test_sni_tls13(void)9876e0c4386eSCy Schubert static int test_sni_tls13(void)
9877e0c4386eSCy Schubert {
9878e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
9879e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9880e0c4386eSCy Schubert     int testresult = 0;
9881e0c4386eSCy Schubert 
9882e0c4386eSCy Schubert     /* Reset callback counter */
9883e0c4386eSCy Schubert     snicb = 0;
9884e0c4386eSCy Schubert 
9885e0c4386eSCy Schubert     /* Create an initial SSL_CTX with no certificate configured */
9886e0c4386eSCy Schubert     sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9887e0c4386eSCy Schubert     if (!TEST_ptr(sctx))
9888e0c4386eSCy Schubert         goto end;
9889e0c4386eSCy Schubert     /* Require TLSv1.3 as a minimum */
9890e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9891e0c4386eSCy Schubert                                        TLS_client_method(), TLS1_3_VERSION, 0,
9892e0c4386eSCy Schubert                                        &sctx2, &cctx, cert, privkey)))
9893e0c4386eSCy Schubert         goto end;
9894e0c4386eSCy Schubert 
9895e0c4386eSCy Schubert     /* Set up SNI */
9896e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
9897e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
9898e0c4386eSCy Schubert         goto end;
9899e0c4386eSCy Schubert 
9900e0c4386eSCy Schubert     /*
9901e0c4386eSCy Schubert      * Connection should still succeed because the final SSL_CTX has the right
9902e0c4386eSCy Schubert      * certificates configured.
9903e0c4386eSCy Schubert      */
9904e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9905e0c4386eSCy Schubert                                       &clientssl, NULL, NULL))
9906e0c4386eSCy Schubert             || !TEST_true(create_ssl_connection(serverssl, clientssl,
9907e0c4386eSCy Schubert                                                 SSL_ERROR_NONE)))
9908e0c4386eSCy Schubert         goto end;
9909e0c4386eSCy Schubert 
9910e0c4386eSCy Schubert     /* We should have had the SNI callback called exactly once */
9911e0c4386eSCy Schubert     if (!TEST_int_eq(snicb, 1))
9912e0c4386eSCy Schubert         goto end;
9913e0c4386eSCy Schubert 
9914e0c4386eSCy Schubert     testresult = 1;
9915e0c4386eSCy Schubert 
9916e0c4386eSCy Schubert end:
9917e0c4386eSCy Schubert     SSL_free(serverssl);
9918e0c4386eSCy Schubert     SSL_free(clientssl);
9919e0c4386eSCy Schubert     SSL_CTX_free(sctx2);
9920e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9921e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9922e0c4386eSCy Schubert     return testresult;
9923e0c4386eSCy Schubert }
9924e0c4386eSCy Schubert 
9925e0c4386eSCy Schubert /*
9926e0c4386eSCy Schubert  * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
9927e0c4386eSCy Schubert  * 0 = TLSv1.2
9928e0c4386eSCy Schubert  * 1 = TLSv1.3
9929e0c4386eSCy Schubert  */
test_ticket_lifetime(int idx)9930e0c4386eSCy Schubert static int test_ticket_lifetime(int idx)
9931e0c4386eSCy Schubert {
9932e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
9933e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
9934e0c4386eSCy Schubert     int testresult = 0;
9935e0c4386eSCy Schubert     int version = TLS1_3_VERSION;
9936e0c4386eSCy Schubert 
9937e0c4386eSCy Schubert #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
9938e0c4386eSCy Schubert #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
9939e0c4386eSCy Schubert 
9940e0c4386eSCy Schubert     if (idx == 0) {
9941e0c4386eSCy Schubert #ifdef OPENSSL_NO_TLS1_2
9942e0c4386eSCy Schubert         return TEST_skip("TLS 1.2 is disabled.");
9943e0c4386eSCy Schubert #else
9944e0c4386eSCy Schubert         version = TLS1_2_VERSION;
9945e0c4386eSCy Schubert #endif
9946e0c4386eSCy Schubert     }
9947e0c4386eSCy Schubert 
9948e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9949e0c4386eSCy Schubert                                        TLS_client_method(), version, version,
9950e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
9951e0c4386eSCy Schubert         goto end;
9952e0c4386eSCy Schubert 
9953e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9954e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
9955e0c4386eSCy Schubert         goto end;
9956e0c4386eSCy Schubert 
9957e0c4386eSCy Schubert     /*
9958e0c4386eSCy Schubert      * Set the timeout to be more than 1 week
9959e0c4386eSCy Schubert      * make sure the returned value is the default
9960e0c4386eSCy Schubert      */
9961e0c4386eSCy Schubert     if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
9962e0c4386eSCy Schubert                       SSL_get_default_timeout(serverssl)))
9963e0c4386eSCy Schubert         goto end;
9964e0c4386eSCy Schubert 
9965e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9966e0c4386eSCy Schubert         goto end;
9967e0c4386eSCy Schubert 
9968e0c4386eSCy Schubert     if (idx == 0) {
9969e0c4386eSCy Schubert         /* TLSv1.2 uses the set value */
9970e0c4386eSCy Schubert         if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
9971e0c4386eSCy Schubert             goto end;
9972e0c4386eSCy Schubert     } else {
9973e0c4386eSCy Schubert         /* TLSv1.3 uses the limited value */
9974e0c4386eSCy Schubert         if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
9975e0c4386eSCy Schubert             goto end;
9976e0c4386eSCy Schubert     }
9977e0c4386eSCy Schubert     testresult = 1;
9978e0c4386eSCy Schubert 
9979e0c4386eSCy Schubert end:
9980e0c4386eSCy Schubert     SSL_free(serverssl);
9981e0c4386eSCy Schubert     SSL_free(clientssl);
9982e0c4386eSCy Schubert     SSL_CTX_free(sctx);
9983e0c4386eSCy Schubert     SSL_CTX_free(cctx);
9984e0c4386eSCy Schubert     return testresult;
9985e0c4386eSCy Schubert }
9986e0c4386eSCy Schubert #endif
9987e0c4386eSCy Schubert /*
9988e0c4386eSCy Schubert  * Test that setting an ALPN does not violate RFC
9989e0c4386eSCy Schubert  */
test_set_alpn(void)9990e0c4386eSCy Schubert static int test_set_alpn(void)
9991e0c4386eSCy Schubert {
9992e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
9993e0c4386eSCy Schubert     SSL *ssl = NULL;
9994e0c4386eSCy Schubert     int testresult = 0;
9995e0c4386eSCy Schubert 
9996e0c4386eSCy Schubert     unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
9997e0c4386eSCy Schubert     unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
9998e0c4386eSCy Schubert     unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
9999e0c4386eSCy Schubert     unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
10000e0c4386eSCy Schubert     unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
10001e0c4386eSCy Schubert     unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
10002e0c4386eSCy Schubert 
10003e0c4386eSCy Schubert     /* Create an initial SSL_CTX with no certificate configured */
10004e0c4386eSCy Schubert     ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10005e0c4386eSCy Schubert     if (!TEST_ptr(ctx))
10006e0c4386eSCy Schubert         goto end;
10007e0c4386eSCy Schubert 
10008e0c4386eSCy Schubert     /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
10009e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
10010e0c4386eSCy Schubert         goto end;
10011e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
10012e0c4386eSCy Schubert         goto end;
10013e0c4386eSCy Schubert     if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
10014e0c4386eSCy Schubert         goto end;
10015e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
10016e0c4386eSCy Schubert         goto end;
10017e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
10018e0c4386eSCy Schubert         goto end;
10019e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
10020e0c4386eSCy Schubert         goto end;
10021e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
10022e0c4386eSCy Schubert         goto end;
10023e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
10024e0c4386eSCy Schubert         goto end;
10025e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
10026e0c4386eSCy Schubert         goto end;
10027e0c4386eSCy Schubert 
10028e0c4386eSCy Schubert     ssl = SSL_new(ctx);
10029e0c4386eSCy Schubert     if (!TEST_ptr(ssl))
10030e0c4386eSCy Schubert         goto end;
10031e0c4386eSCy Schubert 
10032e0c4386eSCy Schubert     if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
10033e0c4386eSCy Schubert         goto end;
10034e0c4386eSCy Schubert     if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
10035e0c4386eSCy Schubert         goto end;
10036e0c4386eSCy Schubert     if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
10037e0c4386eSCy Schubert         goto end;
10038e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
10039e0c4386eSCy Schubert         goto end;
10040e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
10041e0c4386eSCy Schubert         goto end;
10042e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
10043e0c4386eSCy Schubert         goto end;
10044e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
10045e0c4386eSCy Schubert         goto end;
10046e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
10047e0c4386eSCy Schubert         goto end;
10048e0c4386eSCy Schubert     if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
10049e0c4386eSCy Schubert         goto end;
10050e0c4386eSCy Schubert 
10051e0c4386eSCy Schubert     testresult = 1;
10052e0c4386eSCy Schubert 
10053e0c4386eSCy Schubert end:
10054e0c4386eSCy Schubert     SSL_free(ssl);
10055e0c4386eSCy Schubert     SSL_CTX_free(ctx);
10056e0c4386eSCy Schubert     return testresult;
10057e0c4386eSCy Schubert }
10058e0c4386eSCy Schubert 
10059e0c4386eSCy Schubert /*
10060e0c4386eSCy Schubert  * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
10061e0c4386eSCy Schubert  */
test_set_verify_cert_store_ssl_ctx(void)10062e0c4386eSCy Schubert static int test_set_verify_cert_store_ssl_ctx(void)
10063e0c4386eSCy Schubert {
10064e0c4386eSCy Schubert    SSL_CTX *ctx = NULL;
10065e0c4386eSCy Schubert    int testresult = 0;
10066e0c4386eSCy Schubert    X509_STORE *store = NULL, *new_store = NULL,
10067e0c4386eSCy Schubert               *cstore = NULL, *new_cstore = NULL;
10068e0c4386eSCy Schubert 
10069e0c4386eSCy Schubert    /* Create an initial SSL_CTX. */
10070e0c4386eSCy Schubert    ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10071e0c4386eSCy Schubert    if (!TEST_ptr(ctx))
10072e0c4386eSCy Schubert        goto end;
10073e0c4386eSCy Schubert 
10074e0c4386eSCy Schubert    /* Retrieve verify store pointer. */
10075e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10076e0c4386eSCy Schubert        goto end;
10077e0c4386eSCy Schubert 
10078e0c4386eSCy Schubert    /* Retrieve chain store pointer. */
10079e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10080e0c4386eSCy Schubert        goto end;
10081e0c4386eSCy Schubert 
10082e0c4386eSCy Schubert    /* We haven't set any yet, so this should be NULL. */
10083e0c4386eSCy Schubert    if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10084e0c4386eSCy Schubert        goto end;
10085e0c4386eSCy Schubert 
10086e0c4386eSCy Schubert    /* Create stores. We use separate stores so pointers are different. */
10087e0c4386eSCy Schubert    new_store = X509_STORE_new();
10088e0c4386eSCy Schubert    if (!TEST_ptr(new_store))
10089e0c4386eSCy Schubert        goto end;
10090e0c4386eSCy Schubert 
10091e0c4386eSCy Schubert    new_cstore = X509_STORE_new();
10092e0c4386eSCy Schubert    if (!TEST_ptr(new_cstore))
10093e0c4386eSCy Schubert        goto end;
10094e0c4386eSCy Schubert 
10095e0c4386eSCy Schubert    /* Set stores. */
10096e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
10097e0c4386eSCy Schubert        goto end;
10098e0c4386eSCy Schubert 
10099e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
10100e0c4386eSCy Schubert        goto end;
10101e0c4386eSCy Schubert 
10102e0c4386eSCy Schubert    /* Should be able to retrieve the same pointer. */
10103e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10104e0c4386eSCy Schubert        goto end;
10105e0c4386eSCy Schubert 
10106e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10107e0c4386eSCy Schubert        goto end;
10108e0c4386eSCy Schubert 
10109e0c4386eSCy Schubert    if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10110e0c4386eSCy Schubert        goto end;
10111e0c4386eSCy Schubert 
10112e0c4386eSCy Schubert    /* Should be able to unset again. */
10113e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
10114e0c4386eSCy Schubert        goto end;
10115e0c4386eSCy Schubert 
10116e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
10117e0c4386eSCy Schubert        goto end;
10118e0c4386eSCy Schubert 
10119e0c4386eSCy Schubert    /* Should now be NULL. */
10120e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10121e0c4386eSCy Schubert        goto end;
10122e0c4386eSCy Schubert 
10123e0c4386eSCy Schubert    if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10124e0c4386eSCy Schubert        goto end;
10125e0c4386eSCy Schubert 
10126e0c4386eSCy Schubert    if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10127e0c4386eSCy Schubert        goto end;
10128e0c4386eSCy Schubert 
10129e0c4386eSCy Schubert    testresult = 1;
10130e0c4386eSCy Schubert 
10131e0c4386eSCy Schubert end:
10132e0c4386eSCy Schubert    X509_STORE_free(new_store);
10133e0c4386eSCy Schubert    X509_STORE_free(new_cstore);
10134e0c4386eSCy Schubert    SSL_CTX_free(ctx);
10135e0c4386eSCy Schubert    return testresult;
10136e0c4386eSCy Schubert }
10137e0c4386eSCy Schubert 
10138e0c4386eSCy Schubert /*
10139e0c4386eSCy Schubert  * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
10140e0c4386eSCy Schubert  */
test_set_verify_cert_store_ssl(void)10141e0c4386eSCy Schubert static int test_set_verify_cert_store_ssl(void)
10142e0c4386eSCy Schubert {
10143e0c4386eSCy Schubert    SSL_CTX *ctx = NULL;
10144e0c4386eSCy Schubert    SSL *ssl = NULL;
10145e0c4386eSCy Schubert    int testresult = 0;
10146e0c4386eSCy Schubert    X509_STORE *store = NULL, *new_store = NULL,
10147e0c4386eSCy Schubert               *cstore = NULL, *new_cstore = NULL;
10148e0c4386eSCy Schubert 
10149e0c4386eSCy Schubert    /* Create an initial SSL_CTX. */
10150e0c4386eSCy Schubert    ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10151e0c4386eSCy Schubert    if (!TEST_ptr(ctx))
10152e0c4386eSCy Schubert        goto end;
10153e0c4386eSCy Schubert 
10154e0c4386eSCy Schubert    /* Create an SSL object. */
10155e0c4386eSCy Schubert    ssl = SSL_new(ctx);
10156e0c4386eSCy Schubert    if (!TEST_ptr(ssl))
10157e0c4386eSCy Schubert        goto end;
10158e0c4386eSCy Schubert 
10159e0c4386eSCy Schubert    /* Retrieve verify store pointer. */
10160e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10161e0c4386eSCy Schubert        goto end;
10162e0c4386eSCy Schubert 
10163e0c4386eSCy Schubert    /* Retrieve chain store pointer. */
10164e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10165e0c4386eSCy Schubert        goto end;
10166e0c4386eSCy Schubert 
10167e0c4386eSCy Schubert    /* We haven't set any yet, so this should be NULL. */
10168e0c4386eSCy Schubert    if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10169e0c4386eSCy Schubert        goto end;
10170e0c4386eSCy Schubert 
10171e0c4386eSCy Schubert    /* Create stores. We use separate stores so pointers are different. */
10172e0c4386eSCy Schubert    new_store = X509_STORE_new();
10173e0c4386eSCy Schubert    if (!TEST_ptr(new_store))
10174e0c4386eSCy Schubert        goto end;
10175e0c4386eSCy Schubert 
10176e0c4386eSCy Schubert    new_cstore = X509_STORE_new();
10177e0c4386eSCy Schubert    if (!TEST_ptr(new_cstore))
10178e0c4386eSCy Schubert        goto end;
10179e0c4386eSCy Schubert 
10180e0c4386eSCy Schubert    /* Set stores. */
10181e0c4386eSCy Schubert    if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
10182e0c4386eSCy Schubert        goto end;
10183e0c4386eSCy Schubert 
10184e0c4386eSCy Schubert    if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
10185e0c4386eSCy Schubert        goto end;
10186e0c4386eSCy Schubert 
10187e0c4386eSCy Schubert    /* Should be able to retrieve the same pointer. */
10188e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10189e0c4386eSCy Schubert        goto end;
10190e0c4386eSCy Schubert 
10191e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10192e0c4386eSCy Schubert        goto end;
10193e0c4386eSCy Schubert 
10194e0c4386eSCy Schubert    if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10195e0c4386eSCy Schubert        goto end;
10196e0c4386eSCy Schubert 
10197e0c4386eSCy Schubert    /* Should be able to unset again. */
10198e0c4386eSCy Schubert    if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
10199e0c4386eSCy Schubert        goto end;
10200e0c4386eSCy Schubert 
10201e0c4386eSCy Schubert    if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
10202e0c4386eSCy Schubert        goto end;
10203e0c4386eSCy Schubert 
10204e0c4386eSCy Schubert    /* Should now be NULL. */
10205e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10206e0c4386eSCy Schubert        goto end;
10207e0c4386eSCy Schubert 
10208e0c4386eSCy Schubert    if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10209e0c4386eSCy Schubert        goto end;
10210e0c4386eSCy Schubert 
10211e0c4386eSCy Schubert    if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10212e0c4386eSCy Schubert        goto end;
10213e0c4386eSCy Schubert 
10214e0c4386eSCy Schubert    testresult = 1;
10215e0c4386eSCy Schubert 
10216e0c4386eSCy Schubert end:
10217e0c4386eSCy Schubert    X509_STORE_free(new_store);
10218e0c4386eSCy Schubert    X509_STORE_free(new_cstore);
10219e0c4386eSCy Schubert    SSL_free(ssl);
10220e0c4386eSCy Schubert    SSL_CTX_free(ctx);
10221e0c4386eSCy Schubert    return testresult;
10222e0c4386eSCy Schubert }
10223e0c4386eSCy Schubert 
10224e0c4386eSCy Schubert 
test_inherit_verify_param(void)10225e0c4386eSCy Schubert static int test_inherit_verify_param(void)
10226e0c4386eSCy Schubert {
10227e0c4386eSCy Schubert     int testresult = 0;
10228e0c4386eSCy Schubert 
10229e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
10230e0c4386eSCy Schubert     X509_VERIFY_PARAM *cp = NULL;
10231e0c4386eSCy Schubert     SSL *ssl = NULL;
10232e0c4386eSCy Schubert     X509_VERIFY_PARAM *sp = NULL;
10233e0c4386eSCy Schubert     int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
10234e0c4386eSCy Schubert 
10235e0c4386eSCy Schubert     ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10236e0c4386eSCy Schubert     if (!TEST_ptr(ctx))
10237e0c4386eSCy Schubert         goto end;
10238e0c4386eSCy Schubert 
10239e0c4386eSCy Schubert     cp = SSL_CTX_get0_param(ctx);
10240e0c4386eSCy Schubert     if (!TEST_ptr(cp))
10241e0c4386eSCy Schubert         goto end;
10242e0c4386eSCy Schubert     if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
10243e0c4386eSCy Schubert         goto end;
10244e0c4386eSCy Schubert 
10245e0c4386eSCy Schubert     X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
10246e0c4386eSCy Schubert 
10247e0c4386eSCy Schubert     ssl = SSL_new(ctx);
10248e0c4386eSCy Schubert     if (!TEST_ptr(ssl))
10249e0c4386eSCy Schubert         goto end;
10250e0c4386eSCy Schubert 
10251e0c4386eSCy Schubert     sp = SSL_get0_param(ssl);
10252e0c4386eSCy Schubert     if (!TEST_ptr(sp))
10253e0c4386eSCy Schubert         goto end;
10254e0c4386eSCy Schubert     if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
10255e0c4386eSCy Schubert         goto end;
10256e0c4386eSCy Schubert 
10257e0c4386eSCy Schubert     testresult = 1;
10258e0c4386eSCy Schubert 
10259e0c4386eSCy Schubert  end:
10260e0c4386eSCy Schubert     SSL_free(ssl);
10261e0c4386eSCy Schubert     SSL_CTX_free(ctx);
10262e0c4386eSCy Schubert 
10263e0c4386eSCy Schubert     return testresult;
10264e0c4386eSCy Schubert }
10265e0c4386eSCy Schubert 
test_load_dhfile(void)10266e0c4386eSCy Schubert static int test_load_dhfile(void)
10267e0c4386eSCy Schubert {
10268e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
10269e0c4386eSCy Schubert     int testresult = 0;
10270e0c4386eSCy Schubert 
10271e0c4386eSCy Schubert     SSL_CTX *ctx = NULL;
10272e0c4386eSCy Schubert     SSL_CONF_CTX *cctx = NULL;
10273e0c4386eSCy Schubert 
10274e0c4386eSCy Schubert     if (dhfile == NULL)
10275e0c4386eSCy Schubert         return 1;
10276e0c4386eSCy Schubert 
10277e0c4386eSCy Schubert     if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
10278e0c4386eSCy Schubert         || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
10279e0c4386eSCy Schubert         goto end;
10280e0c4386eSCy Schubert 
10281e0c4386eSCy Schubert     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
10282e0c4386eSCy Schubert     SSL_CONF_CTX_set_flags(cctx,
10283e0c4386eSCy Schubert                            SSL_CONF_FLAG_CERTIFICATE
10284e0c4386eSCy Schubert                            | SSL_CONF_FLAG_SERVER
10285e0c4386eSCy Schubert                            | SSL_CONF_FLAG_FILE);
10286e0c4386eSCy Schubert 
10287e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
10288e0c4386eSCy Schubert         goto end;
10289e0c4386eSCy Schubert 
10290e0c4386eSCy Schubert     testresult = 1;
10291e0c4386eSCy Schubert end:
10292e0c4386eSCy Schubert     SSL_CONF_CTX_free(cctx);
10293e0c4386eSCy Schubert     SSL_CTX_free(ctx);
10294e0c4386eSCy Schubert 
10295e0c4386eSCy Schubert     return testresult;
10296e0c4386eSCy Schubert #else
10297e0c4386eSCy Schubert     return TEST_skip("DH not supported by this build");
10298e0c4386eSCy Schubert #endif
10299e0c4386eSCy Schubert }
10300e0c4386eSCy Schubert 
10301e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
10302e0c4386eSCy Schubert /*
10303e0c4386eSCy Schubert  * Test TLSv1.2 with a pipeline capable cipher. TLSv1.3 and DTLS do not
10304e0c4386eSCy Schubert  * support this yet. The only pipeline capable cipher that we have is in the
10305e0c4386eSCy Schubert  * dasync engine (providers don't support this yet), so we have to use
10306e0c4386eSCy Schubert  * deprecated APIs for this test.
10307e0c4386eSCy Schubert  *
10308e0c4386eSCy Schubert  * Test 0: Client has pipelining enabled, server does not
10309e0c4386eSCy Schubert  * Test 1: Server has pipelining enabled, client does not
10310e0c4386eSCy Schubert  * Test 2: Client has pipelining enabled, server does not: not enough data to
10311e0c4386eSCy Schubert  *         fill all the pipelines
10312e0c4386eSCy Schubert  * Test 3: Client has pipelining enabled, server does not: not enough data to
10313e0c4386eSCy Schubert  *         fill all the pipelines by more than a full pipeline's worth
10314e0c4386eSCy Schubert  * Test 4: Client has pipelining enabled, server does not: more data than all
10315e0c4386eSCy Schubert  *         the available pipelines can take
10316e0c4386eSCy Schubert  * Test 5: Client has pipelining enabled, server does not: Maximum size pipeline
10317e0c4386eSCy Schubert  * Test 6: Repeat of test 0, but the engine is loaded late (after the SSL_CTX
10318e0c4386eSCy Schubert  *         is created)
10319e0c4386eSCy Schubert  */
test_pipelining(int idx)10320e0c4386eSCy Schubert static int test_pipelining(int idx)
10321e0c4386eSCy Schubert {
10322e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
10323e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb;
10324e0c4386eSCy Schubert     int testresult = 0, numreads;
10325e0c4386eSCy Schubert     /* A 55 byte message */
10326e0c4386eSCy Schubert     unsigned char *msg = (unsigned char *)
10327e0c4386eSCy Schubert         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123";
10328e0c4386eSCy Schubert     size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5;
10329e0c4386eSCy Schubert     size_t expectedreads;
10330e0c4386eSCy Schubert     unsigned char *buf = NULL;
10331e0c4386eSCy Schubert     ENGINE *e = NULL;
10332e0c4386eSCy Schubert 
10333e0c4386eSCy Schubert     if (idx != 6) {
10334e0c4386eSCy Schubert         e = load_dasync();
10335e0c4386eSCy Schubert         if (e == NULL)
10336e0c4386eSCy Schubert             return 0;
10337e0c4386eSCy Schubert     }
10338e0c4386eSCy Schubert 
10339e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10340e0c4386eSCy Schubert                                        TLS_client_method(), 0,
10341e0c4386eSCy Schubert                                        TLS1_2_VERSION, &sctx, &cctx, cert,
10342e0c4386eSCy Schubert                                        privkey)))
10343e0c4386eSCy Schubert         goto end;
10344e0c4386eSCy Schubert 
10345e0c4386eSCy Schubert     if (idx == 6) {
10346e0c4386eSCy Schubert         e = load_dasync();
10347e0c4386eSCy Schubert         if (e == NULL)
10348e0c4386eSCy Schubert             goto end;
10349e0c4386eSCy Schubert         /* Now act like test 0 */
10350e0c4386eSCy Schubert         idx = 0;
10351e0c4386eSCy Schubert     }
10352e0c4386eSCy Schubert 
10353e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10354e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
10355e0c4386eSCy Schubert         goto end;
10356e0c4386eSCy Schubert 
10357e0c4386eSCy Schubert     if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA")))
10358e0c4386eSCy Schubert         goto end;
10359e0c4386eSCy Schubert 
10360e0c4386eSCy Schubert     /* peera is always configured for pipelining, while peerb is not. */
10361e0c4386eSCy Schubert     if (idx == 1) {
10362e0c4386eSCy Schubert         peera = serverssl;
10363e0c4386eSCy Schubert         peerb = clientssl;
10364e0c4386eSCy Schubert 
10365e0c4386eSCy Schubert     } else {
10366e0c4386eSCy Schubert         peera = clientssl;
10367e0c4386eSCy Schubert         peerb = serverssl;
10368e0c4386eSCy Schubert     }
10369e0c4386eSCy Schubert 
10370e0c4386eSCy Schubert     if (idx == 5) {
10371e0c4386eSCy Schubert         numpipes = 2;
10372e0c4386eSCy Schubert         /* Maximum allowed fragment size */
10373e0c4386eSCy Schubert         fragsize = SSL3_RT_MAX_PLAIN_LENGTH;
10374e0c4386eSCy Schubert         msglen = fragsize * numpipes;
10375e0c4386eSCy Schubert         msg = OPENSSL_malloc(msglen);
10376e0c4386eSCy Schubert         if (!TEST_ptr(msg))
10377e0c4386eSCy Schubert             goto end;
10378e0c4386eSCy Schubert         if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0))
10379e0c4386eSCy Schubert             goto end;
10380e0c4386eSCy Schubert     } else if (idx == 4) {
10381e0c4386eSCy Schubert         msglen = 55;
10382e0c4386eSCy Schubert     } else {
10383e0c4386eSCy Schubert         msglen = 50;
10384e0c4386eSCy Schubert     }
10385e0c4386eSCy Schubert     if (idx == 2)
10386e0c4386eSCy Schubert         msglen -= 2; /* Send 2 less bytes */
10387e0c4386eSCy Schubert     else if (idx == 3)
10388e0c4386eSCy Schubert         msglen -= 12; /* Send 12 less bytes */
10389e0c4386eSCy Schubert 
10390e0c4386eSCy Schubert     buf = OPENSSL_malloc(msglen);
10391e0c4386eSCy Schubert     if (!TEST_ptr(buf))
10392e0c4386eSCy Schubert         goto end;
10393e0c4386eSCy Schubert 
10394e0c4386eSCy Schubert     if (idx == 5) {
10395e0c4386eSCy Schubert         /*
10396e0c4386eSCy Schubert          * Test that setting a split send fragment longer than the maximum
10397e0c4386eSCy Schubert          * allowed fails
10398e0c4386eSCy Schubert          */
10399e0c4386eSCy Schubert         if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1)))
10400e0c4386eSCy Schubert             goto end;
10401e0c4386eSCy Schubert     }
10402e0c4386eSCy Schubert 
10403e0c4386eSCy Schubert     /*
10404e0c4386eSCy Schubert      * In the normal case. We have 5 pipelines with 10 bytes per pipeline
10405e0c4386eSCy Schubert      * (50 bytes in total). This is a ridiculously small number of bytes -
10406e0c4386eSCy Schubert      * but sufficient for our purposes
10407e0c4386eSCy Schubert      */
10408e0c4386eSCy Schubert     if (!TEST_true(SSL_set_max_pipelines(peera, numpipes))
10409e0c4386eSCy Schubert             || !TEST_true(SSL_set_split_send_fragment(peera, fragsize)))
10410e0c4386eSCy Schubert         goto end;
10411e0c4386eSCy Schubert 
10412e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10413e0c4386eSCy Schubert         goto end;
10414e0c4386eSCy Schubert 
10415e0c4386eSCy Schubert     /* Write some data from peera to peerb */
10416e0c4386eSCy Schubert     if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written))
10417e0c4386eSCy Schubert         || !TEST_size_t_eq(written, msglen))
10418e0c4386eSCy Schubert         goto end;
10419e0c4386eSCy Schubert 
10420e0c4386eSCy Schubert     /*
10421e0c4386eSCy Schubert      * If the pipelining code worked, then we expect all |numpipes| pipelines to
10422e0c4386eSCy Schubert      * have been used - except in test 3 where only |numpipes - 1| pipelines
10423e0c4386eSCy Schubert      * will be used. This will result in |numpipes| records (|numpipes - 1| for
10424e0c4386eSCy Schubert      * test 3) having been sent to peerb. Since peerb is not using read_ahead we
10425e0c4386eSCy Schubert      * expect this to be read in |numpipes| or |numpipes - 1| separate
10426e0c4386eSCy Schubert      * SSL_read_ex calls. In the case of test 4, there is then one additional
10427e0c4386eSCy Schubert      * read for left over data that couldn't fit in the previous pipelines
10428e0c4386eSCy Schubert      */
10429e0c4386eSCy Schubert     for (offset = 0, numreads = 0;
10430e0c4386eSCy Schubert          offset < msglen;
10431e0c4386eSCy Schubert          offset += readbytes, numreads++) {
10432e0c4386eSCy Schubert         if (!TEST_true(SSL_read_ex(peerb, buf + offset,
10433e0c4386eSCy Schubert                                    msglen - offset, &readbytes)))
10434e0c4386eSCy Schubert             goto end;
10435e0c4386eSCy Schubert     }
10436e0c4386eSCy Schubert 
10437e0c4386eSCy Schubert     expectedreads = idx == 4 ? numpipes + 1
10438e0c4386eSCy Schubert                              : (idx == 3 ? numpipes - 1 : numpipes);
10439e0c4386eSCy Schubert     if (!TEST_mem_eq(msg, msglen, buf, offset)
10440e0c4386eSCy Schubert             || !TEST_int_eq(numreads, expectedreads))
10441e0c4386eSCy Schubert         goto end;
10442e0c4386eSCy Schubert 
10443e0c4386eSCy Schubert     /*
10444e0c4386eSCy Schubert      * Write some data from peerb to peera. We do this in up to |numpipes + 1|
10445e0c4386eSCy Schubert      * chunks to exercise the read pipelining code on peera.
10446e0c4386eSCy Schubert      */
10447e0c4386eSCy Schubert     for (offset = 0; offset < msglen; offset += fragsize) {
10448e0c4386eSCy Schubert         size_t sendlen = msglen - offset;
10449e0c4386eSCy Schubert 
10450e0c4386eSCy Schubert         if (sendlen > fragsize)
10451e0c4386eSCy Schubert             sendlen = fragsize;
10452e0c4386eSCy Schubert         if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written))
10453e0c4386eSCy Schubert                 || !TEST_size_t_eq(written, sendlen))
10454e0c4386eSCy Schubert             goto end;
10455e0c4386eSCy Schubert     }
10456e0c4386eSCy Schubert 
10457e0c4386eSCy Schubert     /*
10458e0c4386eSCy Schubert      * The data was written in |numpipes|, |numpipes - 1| or |numpipes + 1|
10459e0c4386eSCy Schubert      * separate chunks (depending on which test we are running). If the
10460e0c4386eSCy Schubert      * pipelining is working then we expect peera to read up to numpipes chunks
10461e0c4386eSCy Schubert      * and process them in parallel, giving back the complete result in a single
10462e0c4386eSCy Schubert      * call to SSL_read_ex
10463e0c4386eSCy Schubert      */
10464e0c4386eSCy Schubert     if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes))
10465e0c4386eSCy Schubert             || !TEST_size_t_le(readbytes, msglen))
10466e0c4386eSCy Schubert         goto end;
10467e0c4386eSCy Schubert 
10468e0c4386eSCy Schubert     if (idx == 4) {
10469e0c4386eSCy Schubert         size_t readbytes2;
10470e0c4386eSCy Schubert 
10471e0c4386eSCy Schubert         if (!TEST_true(SSL_read_ex(peera, buf + readbytes,
10472e0c4386eSCy Schubert                                    msglen - readbytes, &readbytes2)))
10473e0c4386eSCy Schubert             goto end;
10474e0c4386eSCy Schubert         readbytes += readbytes2;
10475e0c4386eSCy Schubert         if (!TEST_size_t_le(readbytes, msglen))
10476e0c4386eSCy Schubert             goto end;
10477e0c4386eSCy Schubert     }
10478e0c4386eSCy Schubert 
10479e0c4386eSCy Schubert     if (!TEST_mem_eq(msg, msglen, buf, readbytes))
10480e0c4386eSCy Schubert         goto end;
10481e0c4386eSCy Schubert 
10482e0c4386eSCy Schubert     testresult = 1;
10483e0c4386eSCy Schubert end:
10484e0c4386eSCy Schubert     SSL_free(serverssl);
10485e0c4386eSCy Schubert     SSL_free(clientssl);
10486e0c4386eSCy Schubert     SSL_CTX_free(sctx);
10487e0c4386eSCy Schubert     SSL_CTX_free(cctx);
10488e0c4386eSCy Schubert     if (e != NULL) {
10489e0c4386eSCy Schubert         ENGINE_unregister_ciphers(e);
10490e0c4386eSCy Schubert         ENGINE_finish(e);
10491e0c4386eSCy Schubert         ENGINE_free(e);
10492e0c4386eSCy Schubert     }
10493e0c4386eSCy Schubert     OPENSSL_free(buf);
10494e0c4386eSCy Schubert     if (fragsize == SSL3_RT_MAX_PLAIN_LENGTH)
10495e0c4386eSCy Schubert         OPENSSL_free(msg);
10496e0c4386eSCy Schubert     return testresult;
10497e0c4386eSCy Schubert }
10498e0c4386eSCy Schubert #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
10499e0c4386eSCy Schubert 
10500e0c4386eSCy Schubert /*
10501e0c4386eSCy Schubert  * Force a write retry during handshaking. We test various combinations of
10502e0c4386eSCy Schubert  * scenarios. We test a large certificate message which will fill the buffering
10503e0c4386eSCy Schubert  * BIO used in the handshake. We try with client auth on and off. Finally we
10504e0c4386eSCy Schubert  * also try a BIO that indicates retry via a 0 return. BIO_write() is documented
10505e0c4386eSCy Schubert  * to indicate retry via -1 - but sometimes BIOs don't do that.
10506e0c4386eSCy Schubert  *
10507e0c4386eSCy Schubert  * Test 0: Standard certificate message
10508e0c4386eSCy Schubert  * Test 1: Large certificate message
10509e0c4386eSCy Schubert  * Test 2: Standard cert, verify peer
10510e0c4386eSCy Schubert  * Test 3: Large cert, verify peer
10511e0c4386eSCy Schubert  * Test 4: Standard cert, BIO returns 0 on retry
10512e0c4386eSCy Schubert  * Test 5: Large cert, BIO returns 0 on retry
10513e0c4386eSCy Schubert  * Test 6: Standard cert, verify peer, BIO returns 0 on retry
10514e0c4386eSCy Schubert  * Test 7: Large cert, verify peer, BIO returns 0 on retry
10515e0c4386eSCy Schubert  * Test 8-15: Repeat of above with TLSv1.2
10516e0c4386eSCy Schubert  */
test_handshake_retry(int idx)10517e0c4386eSCy Schubert static int test_handshake_retry(int idx)
10518e0c4386eSCy Schubert {
10519e0c4386eSCy Schubert     SSL_CTX *cctx = NULL, *sctx = NULL;
10520e0c4386eSCy Schubert     SSL *clientssl = NULL, *serverssl = NULL;
10521e0c4386eSCy Schubert     int testresult = 0;
10522e0c4386eSCy Schubert     BIO *tmp = NULL, *bretry = BIO_new(bio_s_always_retry());
10523e0c4386eSCy Schubert     int maxversion = 0;
10524e0c4386eSCy Schubert 
10525e0c4386eSCy Schubert     if (!TEST_ptr(bretry))
10526e0c4386eSCy Schubert         goto end;
10527e0c4386eSCy Schubert 
10528e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
10529e0c4386eSCy Schubert     if ((idx & 8) == 8)
10530e0c4386eSCy Schubert         maxversion = TLS1_2_VERSION;
10531e0c4386eSCy Schubert #else
10532e0c4386eSCy Schubert     if ((idx & 8) == 8)
10533e0c4386eSCy Schubert         return TEST_skip("No TLSv1.2");
10534e0c4386eSCy Schubert #endif
10535e0c4386eSCy Schubert 
10536e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10537e0c4386eSCy Schubert                                        TLS_client_method(), 0, maxversion,
10538e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
10539e0c4386eSCy Schubert         goto end;
10540e0c4386eSCy Schubert 
10541e0c4386eSCy Schubert     /*
10542e0c4386eSCy Schubert      * Add a large amount of data to fill the buffering BIO used by the SSL
10543e0c4386eSCy Schubert      * object
10544e0c4386eSCy Schubert      */
10545e0c4386eSCy Schubert     if ((idx & 1) == 1 && !add_large_cert_chain(sctx))
10546e0c4386eSCy Schubert         goto end;
10547e0c4386eSCy Schubert 
10548e0c4386eSCy Schubert     /*
10549e0c4386eSCy Schubert      * We don't actually configure a client cert, but neither do we fail if one
10550e0c4386eSCy Schubert      * isn't present.
10551e0c4386eSCy Schubert      */
10552e0c4386eSCy Schubert     if ((idx & 2) == 2)
10553e0c4386eSCy Schubert         SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
10554e0c4386eSCy Schubert 
10555e0c4386eSCy Schubert     if ((idx & 4) == 4)
10556e0c4386eSCy Schubert         set_always_retry_err_val(0);
10557e0c4386eSCy Schubert 
10558e0c4386eSCy Schubert     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10559e0c4386eSCy Schubert                                       &clientssl, NULL, NULL)))
10560e0c4386eSCy Schubert         goto end;
10561e0c4386eSCy Schubert 
10562e0c4386eSCy Schubert     tmp = SSL_get_wbio(serverssl);
10563e0c4386eSCy Schubert     if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
10564e0c4386eSCy Schubert         tmp = NULL;
10565e0c4386eSCy Schubert         goto end;
10566e0c4386eSCy Schubert     }
10567e0c4386eSCy Schubert     SSL_set0_wbio(serverssl, bretry);
10568e0c4386eSCy Schubert     bretry = NULL;
10569e0c4386eSCy Schubert 
10570e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_connect(clientssl), -1))
10571e0c4386eSCy Schubert         goto end;
10572e0c4386eSCy Schubert 
10573e0c4386eSCy Schubert     if (!TEST_int_eq(SSL_accept(serverssl), -1)
10574e0c4386eSCy Schubert             || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
10575e0c4386eSCy Schubert         goto end;
10576e0c4386eSCy Schubert 
10577e0c4386eSCy Schubert     /* Restore a BIO that will let the write succeed */
10578e0c4386eSCy Schubert     SSL_set0_wbio(serverssl, tmp);
10579e0c4386eSCy Schubert     tmp = NULL;
10580e0c4386eSCy Schubert 
10581e0c4386eSCy Schubert     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10582e0c4386eSCy Schubert         goto end;
10583e0c4386eSCy Schubert 
10584e0c4386eSCy Schubert     testresult = 1;
10585e0c4386eSCy Schubert end:
10586e0c4386eSCy Schubert     SSL_free(serverssl);
10587e0c4386eSCy Schubert     SSL_free(clientssl);
10588e0c4386eSCy Schubert     SSL_CTX_free(sctx);
10589e0c4386eSCy Schubert     SSL_CTX_free(cctx);
10590e0c4386eSCy Schubert     BIO_free(bretry);
10591e0c4386eSCy Schubert     BIO_free(tmp);
10592e0c4386eSCy Schubert     set_always_retry_err_val(-1);
10593e0c4386eSCy Schubert     return testresult;
10594e0c4386eSCy Schubert }
10595e0c4386eSCy Schubert 
1059644096ebdSEnji Cooper struct resume_servername_cb_data {
1059744096ebdSEnji Cooper     int i;
1059844096ebdSEnji Cooper     SSL_CTX *cctx;
1059944096ebdSEnji Cooper     SSL_CTX *sctx;
1060044096ebdSEnji Cooper     SSL_SESSION *sess;
1060144096ebdSEnji Cooper     int recurse;
1060244096ebdSEnji Cooper };
1060344096ebdSEnji Cooper 
1060444096ebdSEnji Cooper /*
1060544096ebdSEnji Cooper  * Servername callback. We use it here to run another complete handshake using
1060644096ebdSEnji Cooper  * the same session - and mark the session as not_resuamble at the end
1060744096ebdSEnji Cooper  */
resume_servername_cb(SSL * s,int * ad,void * arg)1060844096ebdSEnji Cooper static int resume_servername_cb(SSL *s, int *ad, void *arg)
1060944096ebdSEnji Cooper {
1061044096ebdSEnji Cooper     struct resume_servername_cb_data *cbdata = arg;
1061144096ebdSEnji Cooper     SSL *serverssl = NULL, *clientssl = NULL;
1061244096ebdSEnji Cooper     int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1061344096ebdSEnji Cooper 
1061444096ebdSEnji Cooper     if (cbdata->recurse)
1061544096ebdSEnji Cooper         return SSL_TLSEXT_ERR_ALERT_FATAL;
1061644096ebdSEnji Cooper 
1061744096ebdSEnji Cooper     if ((cbdata->i % 3) != 1)
1061844096ebdSEnji Cooper         return SSL_TLSEXT_ERR_OK;
1061944096ebdSEnji Cooper 
1062044096ebdSEnji Cooper     cbdata->recurse = 1;
1062144096ebdSEnji Cooper 
1062244096ebdSEnji Cooper     if (!TEST_true(create_ssl_objects(cbdata->sctx, cbdata->cctx, &serverssl,
1062344096ebdSEnji Cooper                                       &clientssl, NULL, NULL))
1062444096ebdSEnji Cooper             || !TEST_true(SSL_set_session(clientssl, cbdata->sess)))
1062544096ebdSEnji Cooper         goto end;
1062644096ebdSEnji Cooper 
1062744096ebdSEnji Cooper     ERR_set_mark();
1062844096ebdSEnji Cooper     /*
1062944096ebdSEnji Cooper      * We expect this to fail - because the servername cb will fail. This will
1063044096ebdSEnji Cooper      * mark the session as not_resumable.
1063144096ebdSEnji Cooper      */
1063244096ebdSEnji Cooper     if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
1063344096ebdSEnji Cooper         ERR_clear_last_mark();
1063444096ebdSEnji Cooper         goto end;
1063544096ebdSEnji Cooper     }
1063644096ebdSEnji Cooper     ERR_pop_to_mark();
1063744096ebdSEnji Cooper 
1063844096ebdSEnji Cooper     ret = SSL_TLSEXT_ERR_OK;
1063944096ebdSEnji Cooper  end:
1064044096ebdSEnji Cooper     SSL_free(serverssl);
1064144096ebdSEnji Cooper     SSL_free(clientssl);
1064244096ebdSEnji Cooper     cbdata->recurse = 0;
1064344096ebdSEnji Cooper     return ret;
1064444096ebdSEnji Cooper }
1064544096ebdSEnji Cooper 
1064644096ebdSEnji Cooper /*
1064744096ebdSEnji Cooper  * Test multiple resumptions and cache size handling
1064844096ebdSEnji Cooper  * Test 0: TLSv1.3 (max_early_data set)
1064944096ebdSEnji Cooper  * Test 1: TLSv1.3 (SSL_OP_NO_TICKET set)
1065044096ebdSEnji Cooper  * Test 2: TLSv1.3 (max_early_data and SSL_OP_NO_TICKET set)
1065144096ebdSEnji Cooper  * Test 3: TLSv1.3 (SSL_OP_NO_TICKET, simultaneous resumes)
1065244096ebdSEnji Cooper  * Test 4: TLSv1.2
1065344096ebdSEnji Cooper  */
test_multi_resume(int idx)1065444096ebdSEnji Cooper static int test_multi_resume(int idx)
1065544096ebdSEnji Cooper {
1065644096ebdSEnji Cooper     SSL_CTX *sctx = NULL, *cctx = NULL;
1065744096ebdSEnji Cooper     SSL *serverssl = NULL, *clientssl = NULL;
1065844096ebdSEnji Cooper     SSL_SESSION *sess = NULL;
1065944096ebdSEnji Cooper     int max_version = TLS1_3_VERSION;
1066044096ebdSEnji Cooper     int i, testresult = 0;
1066144096ebdSEnji Cooper     struct resume_servername_cb_data cbdata;
1066244096ebdSEnji Cooper 
1066344096ebdSEnji Cooper #if defined(OPENSSL_NO_TLS1_2)
1066444096ebdSEnji Cooper     if (idx == 4)
1066544096ebdSEnji Cooper         return TEST_skip("TLSv1.2 is disabled in this build");
1066644096ebdSEnji Cooper #else
1066744096ebdSEnji Cooper     if (idx == 4)
1066844096ebdSEnji Cooper         max_version = TLS1_2_VERSION;
1066944096ebdSEnji Cooper #endif
1067044096ebdSEnji Cooper #if defined(OSSL_NO_USABLE_TLS1_3)
1067144096ebdSEnji Cooper     if (idx != 4)
1067244096ebdSEnji Cooper         return TEST_skip("No usable TLSv1.3 in this build");
1067344096ebdSEnji Cooper #endif
1067444096ebdSEnji Cooper 
1067544096ebdSEnji Cooper     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1067644096ebdSEnji Cooper                                        TLS_client_method(), TLS1_VERSION,
1067744096ebdSEnji Cooper                                        max_version, &sctx, &cctx, cert,
1067844096ebdSEnji Cooper                                        privkey)))
1067944096ebdSEnji Cooper         goto end;
1068044096ebdSEnji Cooper 
1068144096ebdSEnji Cooper     /*
1068244096ebdSEnji Cooper      * TLSv1.3 only uses a session cache if either max_early_data > 0 (used for
1068344096ebdSEnji Cooper      * replay protection), or if SSL_OP_NO_TICKET is in use
1068444096ebdSEnji Cooper      */
1068544096ebdSEnji Cooper     if (idx == 0 || idx == 2)  {
1068644096ebdSEnji Cooper         if (!TEST_true(SSL_CTX_set_max_early_data(sctx, 1024)))
1068744096ebdSEnji Cooper             goto end;
1068844096ebdSEnji Cooper     }
1068944096ebdSEnji Cooper     if (idx == 1 || idx == 2 || idx == 3)
1069044096ebdSEnji Cooper         SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
1069144096ebdSEnji Cooper 
1069244096ebdSEnji Cooper     SSL_CTX_sess_set_cache_size(sctx, 5);
1069344096ebdSEnji Cooper 
1069444096ebdSEnji Cooper     if (idx == 3) {
1069544096ebdSEnji Cooper         SSL_CTX_set_tlsext_servername_callback(sctx, resume_servername_cb);
1069644096ebdSEnji Cooper         SSL_CTX_set_tlsext_servername_arg(sctx, &cbdata);
1069744096ebdSEnji Cooper         cbdata.cctx = cctx;
1069844096ebdSEnji Cooper         cbdata.sctx = sctx;
1069944096ebdSEnji Cooper         cbdata.recurse = 0;
1070044096ebdSEnji Cooper     }
1070144096ebdSEnji Cooper 
1070244096ebdSEnji Cooper     for (i = 0; i < 30; i++) {
1070344096ebdSEnji Cooper         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1070444096ebdSEnji Cooper                                                 NULL, NULL))
1070544096ebdSEnji Cooper                 || !TEST_true(SSL_set_session(clientssl, sess)))
1070644096ebdSEnji Cooper             goto end;
1070744096ebdSEnji Cooper 
1070844096ebdSEnji Cooper         /*
1070944096ebdSEnji Cooper          * Check simultaneous resumes. We pause the connection part way through
1071044096ebdSEnji Cooper          * the handshake by (mis)using the servername_cb. The pause occurs after
1071144096ebdSEnji Cooper          * session resumption has already occurred, but before any session
1071244096ebdSEnji Cooper          * tickets have been issued. While paused we run another complete
1071344096ebdSEnji Cooper          * handshake resuming the same session.
1071444096ebdSEnji Cooper          */
1071544096ebdSEnji Cooper         if (idx == 3) {
1071644096ebdSEnji Cooper             cbdata.i = i;
1071744096ebdSEnji Cooper             cbdata.sess = sess;
1071844096ebdSEnji Cooper         }
1071944096ebdSEnji Cooper 
1072044096ebdSEnji Cooper         /*
1072144096ebdSEnji Cooper          * Recreate a bug where dynamically changing the max_early_data value
1072244096ebdSEnji Cooper          * can cause sessions in the session cache which cannot be deleted.
1072344096ebdSEnji Cooper          */
1072444096ebdSEnji Cooper         if ((idx == 0 || idx == 2) && (i % 3) == 2)
1072544096ebdSEnji Cooper             SSL_set_max_early_data(serverssl, 0);
1072644096ebdSEnji Cooper 
1072744096ebdSEnji Cooper         if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1072844096ebdSEnji Cooper             goto end;
1072944096ebdSEnji Cooper 
1073044096ebdSEnji Cooper         if (sess == NULL || (idx == 0 && (i % 3) == 2)) {
1073144096ebdSEnji Cooper             if (!TEST_false(SSL_session_reused(clientssl)))
1073244096ebdSEnji Cooper                 goto end;
1073344096ebdSEnji Cooper         } else {
1073444096ebdSEnji Cooper             if (!TEST_true(SSL_session_reused(clientssl)))
1073544096ebdSEnji Cooper                 goto end;
1073644096ebdSEnji Cooper         }
1073744096ebdSEnji Cooper         SSL_SESSION_free(sess);
1073844096ebdSEnji Cooper 
1073944096ebdSEnji Cooper         /* Do a full handshake, followed by two resumptions */
1074044096ebdSEnji Cooper         if ((i % 3) == 2) {
1074144096ebdSEnji Cooper             sess = NULL;
1074244096ebdSEnji Cooper         } else {
1074344096ebdSEnji Cooper             if (!TEST_ptr((sess = SSL_get1_session(clientssl))))
1074444096ebdSEnji Cooper                 goto end;
1074544096ebdSEnji Cooper         }
1074644096ebdSEnji Cooper 
1074744096ebdSEnji Cooper         SSL_shutdown(clientssl);
1074844096ebdSEnji Cooper         SSL_shutdown(serverssl);
1074944096ebdSEnji Cooper         SSL_free(serverssl);
1075044096ebdSEnji Cooper         SSL_free(clientssl);
1075144096ebdSEnji Cooper         serverssl = clientssl = NULL;
1075244096ebdSEnji Cooper     }
1075344096ebdSEnji Cooper 
1075444096ebdSEnji Cooper     /* We should never exceed the session cache size limit */
1075544096ebdSEnji Cooper     if (!TEST_long_le(SSL_CTX_sess_number(sctx), 5))
1075644096ebdSEnji Cooper         goto end;
1075744096ebdSEnji Cooper 
1075844096ebdSEnji Cooper     testresult = 1;
1075944096ebdSEnji Cooper  end:
1076044096ebdSEnji Cooper     SSL_free(serverssl);
1076144096ebdSEnji Cooper     SSL_free(clientssl);
1076244096ebdSEnji Cooper     SSL_CTX_free(sctx);
1076344096ebdSEnji Cooper     SSL_CTX_free(cctx);
1076444096ebdSEnji Cooper     SSL_SESSION_free(sess);
1076544096ebdSEnji Cooper     return testresult;
1076644096ebdSEnji Cooper }
1076744096ebdSEnji Cooper 
10768a7148ab3SEnji Cooper static struct next_proto_st {
10769a7148ab3SEnji Cooper     int serverlen;
10770a7148ab3SEnji Cooper     unsigned char server[40];
10771a7148ab3SEnji Cooper     int clientlen;
10772a7148ab3SEnji Cooper     unsigned char client[40];
10773a7148ab3SEnji Cooper     int expected_ret;
10774a7148ab3SEnji Cooper     size_t selectedlen;
10775a7148ab3SEnji Cooper     unsigned char selected[40];
10776a7148ab3SEnji Cooper } next_proto_tests[] = {
10777a7148ab3SEnji Cooper     {
10778a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10779a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10780a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10781a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10782a7148ab3SEnji Cooper     },
10783a7148ab3SEnji Cooper     {
10784a7148ab3SEnji Cooper         7, { 3, 'a', 'b', 'c', 2, 'a', 'b' },
10785a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10786a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10787a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10788a7148ab3SEnji Cooper     },
10789a7148ab3SEnji Cooper     {
10790a7148ab3SEnji Cooper         7, { 2, 'a', 'b', 3, 'a', 'b', 'c', },
10791a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10792a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10793a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10794a7148ab3SEnji Cooper     },
10795a7148ab3SEnji Cooper     {
10796a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10797a7148ab3SEnji Cooper         7, { 3, 'a', 'b', 'c', 2, 'a', 'b', },
10798a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10799a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10800a7148ab3SEnji Cooper     },
10801a7148ab3SEnji Cooper     {
10802a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10803a7148ab3SEnji Cooper         7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10804a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10805a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10806a7148ab3SEnji Cooper     },
10807a7148ab3SEnji Cooper     {
10808a7148ab3SEnji Cooper         7, { 2, 'b', 'c', 3, 'a', 'b', 'c' },
10809a7148ab3SEnji Cooper         7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10810a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10811a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10812a7148ab3SEnji Cooper     },
10813a7148ab3SEnji Cooper     {
10814a7148ab3SEnji Cooper         10, { 2, 'b', 'c', 3, 'a', 'b', 'c', 2, 'a', 'b' },
10815a7148ab3SEnji Cooper         7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
10816a7148ab3SEnji Cooper         OPENSSL_NPN_NEGOTIATED,
10817a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10818a7148ab3SEnji Cooper     },
10819a7148ab3SEnji Cooper     {
10820a7148ab3SEnji Cooper         4, { 3, 'b', 'c', 'd' },
10821a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10822a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10823a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10824a7148ab3SEnji Cooper     },
10825a7148ab3SEnji Cooper     {
10826a7148ab3SEnji Cooper         0, { 0 },
10827a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10828a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10829a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10830a7148ab3SEnji Cooper     },
10831a7148ab3SEnji Cooper     {
10832a7148ab3SEnji Cooper         -1, { 0 },
10833a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10834a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10835a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10836a7148ab3SEnji Cooper     },
10837a7148ab3SEnji Cooper     {
10838a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10839a7148ab3SEnji Cooper         0, { 0 },
10840a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10841a7148ab3SEnji Cooper         0, { 0 }
10842a7148ab3SEnji Cooper     },
10843a7148ab3SEnji Cooper     {
10844a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10845a7148ab3SEnji Cooper         -1, { 0 },
10846a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10847a7148ab3SEnji Cooper         0, { 0 }
10848a7148ab3SEnji Cooper     },
10849a7148ab3SEnji Cooper     {
10850a7148ab3SEnji Cooper         3, { 3, 'a', 'b', 'c' },
10851a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10852a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10853a7148ab3SEnji Cooper         3, { 'a', 'b', 'c' }
10854a7148ab3SEnji Cooper     },
10855a7148ab3SEnji Cooper     {
10856a7148ab3SEnji Cooper         4, { 3, 'a', 'b', 'c' },
10857a7148ab3SEnji Cooper         3, { 3, 'a', 'b', 'c' },
10858a7148ab3SEnji Cooper         OPENSSL_NPN_NO_OVERLAP,
10859a7148ab3SEnji Cooper         0, { 0 }
10860a7148ab3SEnji Cooper     }
10861a7148ab3SEnji Cooper };
10862a7148ab3SEnji Cooper 
test_select_next_proto(int idx)10863a7148ab3SEnji Cooper static int test_select_next_proto(int idx)
10864a7148ab3SEnji Cooper {
10865a7148ab3SEnji Cooper     struct next_proto_st *np = &next_proto_tests[idx];
10866a7148ab3SEnji Cooper     int ret = 0;
10867a7148ab3SEnji Cooper     unsigned char *out, *client, *server;
10868a7148ab3SEnji Cooper     unsigned char outlen;
10869a7148ab3SEnji Cooper     unsigned int clientlen, serverlen;
10870a7148ab3SEnji Cooper 
10871a7148ab3SEnji Cooper     if (np->clientlen == -1) {
10872a7148ab3SEnji Cooper         client = NULL;
10873a7148ab3SEnji Cooper         clientlen = 0;
10874a7148ab3SEnji Cooper     } else {
10875a7148ab3SEnji Cooper         client = np->client;
10876a7148ab3SEnji Cooper         clientlen = (unsigned int)np->clientlen;
10877a7148ab3SEnji Cooper     }
10878a7148ab3SEnji Cooper     if (np->serverlen == -1) {
10879a7148ab3SEnji Cooper         server = NULL;
10880a7148ab3SEnji Cooper         serverlen = 0;
10881a7148ab3SEnji Cooper     } else {
10882a7148ab3SEnji Cooper         server = np->server;
10883a7148ab3SEnji Cooper         serverlen = (unsigned int)np->serverlen;
10884a7148ab3SEnji Cooper     }
10885a7148ab3SEnji Cooper 
10886a7148ab3SEnji Cooper     if (!TEST_int_eq(SSL_select_next_proto(&out, &outlen, server, serverlen,
10887a7148ab3SEnji Cooper                                            client, clientlen),
10888a7148ab3SEnji Cooper                      np->expected_ret))
10889a7148ab3SEnji Cooper         goto err;
10890a7148ab3SEnji Cooper 
10891a7148ab3SEnji Cooper     if (np->selectedlen == 0) {
10892a7148ab3SEnji Cooper         if (!TEST_ptr_null(out) || !TEST_uchar_eq(outlen, 0))
10893a7148ab3SEnji Cooper             goto err;
10894a7148ab3SEnji Cooper     } else {
10895a7148ab3SEnji Cooper         if (!TEST_mem_eq(out, outlen, np->selected, np->selectedlen))
10896a7148ab3SEnji Cooper             goto err;
10897a7148ab3SEnji Cooper     }
10898a7148ab3SEnji Cooper 
10899a7148ab3SEnji Cooper     ret = 1;
10900a7148ab3SEnji Cooper  err:
10901a7148ab3SEnji Cooper     return ret;
10902a7148ab3SEnji Cooper }
10903a7148ab3SEnji Cooper 
10904a7148ab3SEnji Cooper static const unsigned char fooprot[] = {3, 'f', 'o', 'o' };
10905a7148ab3SEnji Cooper static const unsigned char barprot[] = {3, 'b', 'a', 'r' };
10906a7148ab3SEnji Cooper 
10907a7148ab3SEnji Cooper #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
npn_advert_cb(SSL * ssl,const unsigned char ** out,unsigned int * outlen,void * arg)10908a7148ab3SEnji Cooper static int npn_advert_cb(SSL *ssl, const unsigned char **out,
10909a7148ab3SEnji Cooper                          unsigned int *outlen, void *arg)
10910a7148ab3SEnji Cooper {
10911a7148ab3SEnji Cooper     int *idx = (int *)arg;
10912a7148ab3SEnji Cooper 
10913a7148ab3SEnji Cooper     switch (*idx) {
10914a7148ab3SEnji Cooper     default:
10915a7148ab3SEnji Cooper     case 0:
10916a7148ab3SEnji Cooper         *out = fooprot;
10917a7148ab3SEnji Cooper         *outlen = sizeof(fooprot);
10918a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
10919a7148ab3SEnji Cooper 
10920a7148ab3SEnji Cooper     case 1:
10921*0d0c8621SEnji Cooper         *out = NULL;
10922a7148ab3SEnji Cooper         *outlen = 0;
10923a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
10924a7148ab3SEnji Cooper 
10925a7148ab3SEnji Cooper     case 2:
10926a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_NOACK;
10927a7148ab3SEnji Cooper     }
10928a7148ab3SEnji Cooper }
10929a7148ab3SEnji Cooper 
npn_select_cb(SSL * s,unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)10930a7148ab3SEnji Cooper static int npn_select_cb(SSL *s, unsigned char **out, unsigned char *outlen,
10931a7148ab3SEnji Cooper                          const unsigned char *in, unsigned int inlen, void *arg)
10932a7148ab3SEnji Cooper {
10933a7148ab3SEnji Cooper     int *idx = (int *)arg;
10934a7148ab3SEnji Cooper 
10935a7148ab3SEnji Cooper     switch (*idx) {
10936a7148ab3SEnji Cooper     case 0:
10937a7148ab3SEnji Cooper     case 1:
10938a7148ab3SEnji Cooper         *out = (unsigned char *)(fooprot + 1);
10939a7148ab3SEnji Cooper         *outlen = *fooprot;
10940a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
10941a7148ab3SEnji Cooper 
10942a7148ab3SEnji Cooper     case 3:
10943a7148ab3SEnji Cooper         *out = (unsigned char *)(barprot + 1);
10944a7148ab3SEnji Cooper         *outlen = *barprot;
10945a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
10946a7148ab3SEnji Cooper 
10947a7148ab3SEnji Cooper     case 4:
10948a7148ab3SEnji Cooper         *outlen = 0;
10949a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
10950a7148ab3SEnji Cooper 
10951a7148ab3SEnji Cooper     default:
10952a7148ab3SEnji Cooper     case 2:
10953a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_ALERT_FATAL;
10954a7148ab3SEnji Cooper     }
10955a7148ab3SEnji Cooper }
10956a7148ab3SEnji Cooper 
10957a7148ab3SEnji Cooper /*
10958a7148ab3SEnji Cooper  * Test the NPN callbacks
10959a7148ab3SEnji Cooper  * Test 0: advert = foo, select = foo
10960a7148ab3SEnji Cooper  * Test 1: advert = <empty>, select = foo
10961a7148ab3SEnji Cooper  * Test 2: no advert
10962a7148ab3SEnji Cooper  * Test 3: advert = foo, select = bar
10963a7148ab3SEnji Cooper  * Test 4: advert = foo, select = <empty> (should fail)
10964a7148ab3SEnji Cooper  */
test_npn(int idx)10965a7148ab3SEnji Cooper static int test_npn(int idx)
10966a7148ab3SEnji Cooper {
10967a7148ab3SEnji Cooper     SSL_CTX *sctx = NULL, *cctx = NULL;
10968a7148ab3SEnji Cooper     SSL *serverssl = NULL, *clientssl = NULL;
10969a7148ab3SEnji Cooper     int testresult = 0;
10970a7148ab3SEnji Cooper 
10971a7148ab3SEnji Cooper     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10972a7148ab3SEnji Cooper                                        TLS_client_method(), 0, TLS1_2_VERSION,
10973a7148ab3SEnji Cooper                                        &sctx, &cctx, cert, privkey)))
10974a7148ab3SEnji Cooper         goto end;
10975a7148ab3SEnji Cooper 
10976a7148ab3SEnji Cooper     SSL_CTX_set_next_protos_advertised_cb(sctx, npn_advert_cb, &idx);
10977a7148ab3SEnji Cooper     SSL_CTX_set_next_proto_select_cb(cctx, npn_select_cb, &idx);
10978a7148ab3SEnji Cooper 
10979a7148ab3SEnji Cooper     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
10980a7148ab3SEnji Cooper                                       NULL)))
10981a7148ab3SEnji Cooper         goto end;
10982a7148ab3SEnji Cooper 
10983a7148ab3SEnji Cooper     if (idx == 4) {
10984a7148ab3SEnji Cooper         /* We don't allow empty selection of NPN, so this should fail */
10985a7148ab3SEnji Cooper         if (!TEST_false(create_ssl_connection(serverssl, clientssl,
10986a7148ab3SEnji Cooper                                               SSL_ERROR_NONE)))
10987a7148ab3SEnji Cooper             goto end;
10988a7148ab3SEnji Cooper     } else {
10989a7148ab3SEnji Cooper         const unsigned char *prot;
10990a7148ab3SEnji Cooper         unsigned int protlen;
10991a7148ab3SEnji Cooper 
10992a7148ab3SEnji Cooper         if (!TEST_true(create_ssl_connection(serverssl, clientssl,
10993a7148ab3SEnji Cooper                                              SSL_ERROR_NONE)))
10994a7148ab3SEnji Cooper             goto end;
10995a7148ab3SEnji Cooper 
10996a7148ab3SEnji Cooper         SSL_get0_next_proto_negotiated(serverssl, &prot, &protlen);
10997a7148ab3SEnji Cooper         switch (idx) {
10998a7148ab3SEnji Cooper         case 0:
10999a7148ab3SEnji Cooper         case 1:
11000a7148ab3SEnji Cooper             if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
11001a7148ab3SEnji Cooper                 goto end;
11002a7148ab3SEnji Cooper             break;
11003a7148ab3SEnji Cooper         case 2:
11004a7148ab3SEnji Cooper             if (!TEST_uint_eq(protlen, 0))
11005a7148ab3SEnji Cooper                 goto end;
11006a7148ab3SEnji Cooper             break;
11007a7148ab3SEnji Cooper         case 3:
11008a7148ab3SEnji Cooper             if (!TEST_mem_eq(prot, protlen, barprot + 1, *barprot))
11009a7148ab3SEnji Cooper                 goto end;
11010a7148ab3SEnji Cooper             break;
11011a7148ab3SEnji Cooper         default:
11012a7148ab3SEnji Cooper             TEST_error("Should not get here");
11013a7148ab3SEnji Cooper             goto end;
11014a7148ab3SEnji Cooper         }
11015a7148ab3SEnji Cooper     }
11016a7148ab3SEnji Cooper 
11017a7148ab3SEnji Cooper     testresult = 1;
11018a7148ab3SEnji Cooper  end:
11019a7148ab3SEnji Cooper     SSL_free(serverssl);
11020a7148ab3SEnji Cooper     SSL_free(clientssl);
11021a7148ab3SEnji Cooper     SSL_CTX_free(sctx);
11022a7148ab3SEnji Cooper     SSL_CTX_free(cctx);
11023a7148ab3SEnji Cooper 
11024a7148ab3SEnji Cooper     return testresult;
11025a7148ab3SEnji Cooper }
11026a7148ab3SEnji Cooper #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG) */
11027a7148ab3SEnji Cooper 
alpn_select_cb2(SSL * ssl,const unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg)11028a7148ab3SEnji Cooper static int alpn_select_cb2(SSL *ssl, const unsigned char **out,
11029a7148ab3SEnji Cooper                            unsigned char *outlen, const unsigned char *in,
11030a7148ab3SEnji Cooper                            unsigned int inlen, void *arg)
11031a7148ab3SEnji Cooper {
11032a7148ab3SEnji Cooper     int *idx = (int *)arg;
11033a7148ab3SEnji Cooper 
11034a7148ab3SEnji Cooper     switch (*idx) {
11035a7148ab3SEnji Cooper     case 0:
11036a7148ab3SEnji Cooper         *out = (unsigned char *)(fooprot + 1);
11037a7148ab3SEnji Cooper         *outlen = *fooprot;
11038a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
11039a7148ab3SEnji Cooper 
11040a7148ab3SEnji Cooper     case 2:
11041a7148ab3SEnji Cooper         *out = (unsigned char *)(barprot + 1);
11042a7148ab3SEnji Cooper         *outlen = *barprot;
11043a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
11044a7148ab3SEnji Cooper 
11045a7148ab3SEnji Cooper     case 3:
11046a7148ab3SEnji Cooper         *outlen = 0;
11047a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_OK;
11048a7148ab3SEnji Cooper 
11049a7148ab3SEnji Cooper     default:
11050a7148ab3SEnji Cooper     case 1:
11051a7148ab3SEnji Cooper         return SSL_TLSEXT_ERR_ALERT_FATAL;
11052a7148ab3SEnji Cooper     }
11053a7148ab3SEnji Cooper     return 0;
11054a7148ab3SEnji Cooper }
11055a7148ab3SEnji Cooper 
11056a7148ab3SEnji Cooper /*
11057a7148ab3SEnji Cooper  * Test the ALPN callbacks
11058a7148ab3SEnji Cooper  * Test 0: client = foo, select = foo
11059a7148ab3SEnji Cooper  * Test 1: client = <empty>, select = none
11060a7148ab3SEnji Cooper  * Test 2: client = foo, select = bar (should fail)
11061a7148ab3SEnji Cooper  * Test 3: client = foo, select = <empty> (should fail)
11062a7148ab3SEnji Cooper  */
test_alpn(int idx)11063a7148ab3SEnji Cooper static int test_alpn(int idx)
11064a7148ab3SEnji Cooper {
11065a7148ab3SEnji Cooper     SSL_CTX *sctx = NULL, *cctx = NULL;
11066a7148ab3SEnji Cooper     SSL *serverssl = NULL, *clientssl = NULL;
11067a7148ab3SEnji Cooper     int testresult = 0;
11068a7148ab3SEnji Cooper     const unsigned char *prots = fooprot;
11069a7148ab3SEnji Cooper     unsigned int protslen = sizeof(fooprot);
11070a7148ab3SEnji Cooper 
11071a7148ab3SEnji Cooper     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
11072a7148ab3SEnji Cooper                                        TLS_client_method(), 0, 0,
11073a7148ab3SEnji Cooper                                        &sctx, &cctx, cert, privkey)))
11074a7148ab3SEnji Cooper         goto end;
11075a7148ab3SEnji Cooper 
11076a7148ab3SEnji Cooper     SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb2, &idx);
11077a7148ab3SEnji Cooper 
11078a7148ab3SEnji Cooper     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
11079a7148ab3SEnji Cooper                                       NULL)))
11080a7148ab3SEnji Cooper         goto end;
11081a7148ab3SEnji Cooper 
11082a7148ab3SEnji Cooper     if (idx == 1) {
11083a7148ab3SEnji Cooper         prots = NULL;
11084a7148ab3SEnji Cooper         protslen = 0;
11085a7148ab3SEnji Cooper     }
11086a7148ab3SEnji Cooper 
11087a7148ab3SEnji Cooper     /* SSL_set_alpn_protos returns 0 for success! */
11088a7148ab3SEnji Cooper     if (!TEST_false(SSL_set_alpn_protos(clientssl, prots, protslen)))
11089a7148ab3SEnji Cooper         goto end;
11090a7148ab3SEnji Cooper 
11091a7148ab3SEnji Cooper     if (idx == 2 || idx == 3) {
11092a7148ab3SEnji Cooper         /* We don't allow empty selection of NPN, so this should fail */
11093a7148ab3SEnji Cooper         if (!TEST_false(create_ssl_connection(serverssl, clientssl,
11094a7148ab3SEnji Cooper                                               SSL_ERROR_NONE)))
11095a7148ab3SEnji Cooper             goto end;
11096a7148ab3SEnji Cooper     } else {
11097a7148ab3SEnji Cooper         const unsigned char *prot;
11098a7148ab3SEnji Cooper         unsigned int protlen;
11099a7148ab3SEnji Cooper 
11100a7148ab3SEnji Cooper         if (!TEST_true(create_ssl_connection(serverssl, clientssl,
11101a7148ab3SEnji Cooper                                              SSL_ERROR_NONE)))
11102a7148ab3SEnji Cooper             goto end;
11103a7148ab3SEnji Cooper 
11104a7148ab3SEnji Cooper         SSL_get0_alpn_selected(clientssl, &prot, &protlen);
11105a7148ab3SEnji Cooper         switch (idx) {
11106a7148ab3SEnji Cooper         case 0:
11107a7148ab3SEnji Cooper             if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
11108a7148ab3SEnji Cooper                 goto end;
11109a7148ab3SEnji Cooper             break;
11110a7148ab3SEnji Cooper         case 1:
11111a7148ab3SEnji Cooper             if (!TEST_uint_eq(protlen, 0))
11112a7148ab3SEnji Cooper                 goto end;
11113a7148ab3SEnji Cooper             break;
11114a7148ab3SEnji Cooper         default:
11115a7148ab3SEnji Cooper             TEST_error("Should not get here");
11116a7148ab3SEnji Cooper             goto end;
11117a7148ab3SEnji Cooper         }
11118a7148ab3SEnji Cooper     }
11119a7148ab3SEnji Cooper 
11120a7148ab3SEnji Cooper     testresult = 1;
11121a7148ab3SEnji Cooper  end:
11122a7148ab3SEnji Cooper     SSL_free(serverssl);
11123a7148ab3SEnji Cooper     SSL_free(clientssl);
11124a7148ab3SEnji Cooper     SSL_CTX_free(sctx);
11125a7148ab3SEnji Cooper     SSL_CTX_free(cctx);
11126a7148ab3SEnji Cooper 
11127a7148ab3SEnji Cooper     return testresult;
11128a7148ab3SEnji Cooper }
11129a7148ab3SEnji Cooper 
11130e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
11131e0c4386eSCy Schubert 
setup_tests(void)11132e0c4386eSCy Schubert int setup_tests(void)
11133e0c4386eSCy Schubert {
11134e0c4386eSCy Schubert     char *modulename;
11135e0c4386eSCy Schubert     char *configfile;
11136e0c4386eSCy Schubert 
11137e0c4386eSCy Schubert     libctx = OSSL_LIB_CTX_new();
11138e0c4386eSCy Schubert     if (!TEST_ptr(libctx))
11139e0c4386eSCy Schubert         return 0;
11140e0c4386eSCy Schubert 
11141e0c4386eSCy Schubert     defctxnull = OSSL_PROVIDER_load(NULL, "null");
11142e0c4386eSCy Schubert 
11143e0c4386eSCy Schubert     /*
11144e0c4386eSCy Schubert      * Verify that the default and fips providers in the default libctx are not
11145e0c4386eSCy Schubert      * available
11146e0c4386eSCy Schubert      */
11147e0c4386eSCy Schubert     if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
11148e0c4386eSCy Schubert             || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
11149e0c4386eSCy Schubert         return 0;
11150e0c4386eSCy Schubert 
11151e0c4386eSCy Schubert     if (!test_skip_common_options()) {
11152e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
11153e0c4386eSCy Schubert         return 0;
11154e0c4386eSCy Schubert     }
11155e0c4386eSCy Schubert 
11156e0c4386eSCy Schubert     if (!TEST_ptr(certsdir = test_get_argument(0))
11157e0c4386eSCy Schubert             || !TEST_ptr(srpvfile = test_get_argument(1))
11158e0c4386eSCy Schubert             || !TEST_ptr(tmpfilename = test_get_argument(2))
11159e0c4386eSCy Schubert             || !TEST_ptr(modulename = test_get_argument(3))
11160e0c4386eSCy Schubert             || !TEST_ptr(configfile = test_get_argument(4))
11161e0c4386eSCy Schubert             || !TEST_ptr(dhfile = test_get_argument(5)))
11162e0c4386eSCy Schubert         return 0;
11163e0c4386eSCy Schubert 
11164e0c4386eSCy Schubert     if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
11165e0c4386eSCy Schubert         return 0;
11166e0c4386eSCy Schubert 
11167e0c4386eSCy Schubert     /* Check we have the expected provider available */
11168e0c4386eSCy Schubert     if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
11169e0c4386eSCy Schubert         return 0;
11170e0c4386eSCy Schubert 
11171e0c4386eSCy Schubert     /* Check the default provider is not available */
11172e0c4386eSCy Schubert     if (strcmp(modulename, "default") != 0
11173e0c4386eSCy Schubert             && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
11174e0c4386eSCy Schubert         return 0;
11175e0c4386eSCy Schubert 
11176e0c4386eSCy Schubert     if (strcmp(modulename, "fips") == 0)
11177e0c4386eSCy Schubert         is_fips = 1;
11178e0c4386eSCy Schubert 
11179e0c4386eSCy Schubert     /*
11180e0c4386eSCy Schubert      * We add, but don't load the test "tls-provider". We'll load it when we
11181e0c4386eSCy Schubert      * need it.
11182e0c4386eSCy Schubert      */
11183e0c4386eSCy Schubert     if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
11184e0c4386eSCy Schubert                                              tls_provider_init)))
11185e0c4386eSCy Schubert         return 0;
11186e0c4386eSCy Schubert 
11187e0c4386eSCy Schubert 
11188e0c4386eSCy Schubert     if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
11189e0c4386eSCy Schubert #ifdef OPENSSL_NO_CRYPTO_MDEBUG
11190e0c4386eSCy Schubert         TEST_error("not supported in this build");
11191e0c4386eSCy Schubert         return 0;
11192e0c4386eSCy Schubert #else
11193e0c4386eSCy Schubert         int i, mcount, rcount, fcount;
11194e0c4386eSCy Schubert 
11195e0c4386eSCy Schubert         for (i = 0; i < 4; i++)
11196e0c4386eSCy Schubert             test_export_key_mat(i);
11197e0c4386eSCy Schubert         CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
11198e0c4386eSCy Schubert         test_printf_stdout("malloc %d realloc %d free %d\n",
11199e0c4386eSCy Schubert                 mcount, rcount, fcount);
11200e0c4386eSCy Schubert         return 1;
11201e0c4386eSCy Schubert #endif
11202e0c4386eSCy Schubert     }
11203e0c4386eSCy Schubert 
11204e0c4386eSCy Schubert     cert = test_mk_file_path(certsdir, "servercert.pem");
11205e0c4386eSCy Schubert     if (cert == NULL)
11206e0c4386eSCy Schubert         goto err;
11207e0c4386eSCy Schubert 
11208e0c4386eSCy Schubert     privkey = test_mk_file_path(certsdir, "serverkey.pem");
11209e0c4386eSCy Schubert     if (privkey == NULL)
11210e0c4386eSCy Schubert         goto err;
11211e0c4386eSCy Schubert 
11212e0c4386eSCy Schubert     cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
11213e0c4386eSCy Schubert     if (cert2 == NULL)
11214e0c4386eSCy Schubert         goto err;
11215e0c4386eSCy Schubert 
11216e0c4386eSCy Schubert     privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
11217e0c4386eSCy Schubert     if (privkey2 == NULL)
11218e0c4386eSCy Schubert         goto err;
11219e0c4386eSCy Schubert 
11220e0c4386eSCy Schubert     cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
11221e0c4386eSCy Schubert     if (cert1024 == NULL)
11222e0c4386eSCy Schubert         goto err;
11223e0c4386eSCy Schubert 
11224e0c4386eSCy Schubert     privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
11225e0c4386eSCy Schubert     if (privkey1024 == NULL)
11226e0c4386eSCy Schubert         goto err;
11227e0c4386eSCy Schubert 
11228e0c4386eSCy Schubert     cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
11229e0c4386eSCy Schubert     if (cert3072 == NULL)
11230e0c4386eSCy Schubert         goto err;
11231e0c4386eSCy Schubert 
11232e0c4386eSCy Schubert     privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
11233e0c4386eSCy Schubert     if (privkey3072 == NULL)
11234e0c4386eSCy Schubert         goto err;
11235e0c4386eSCy Schubert 
11236e0c4386eSCy Schubert     cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
11237e0c4386eSCy Schubert     if (cert4096 == NULL)
11238e0c4386eSCy Schubert         goto err;
11239e0c4386eSCy Schubert 
11240e0c4386eSCy Schubert     privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
11241e0c4386eSCy Schubert     if (privkey4096 == NULL)
11242e0c4386eSCy Schubert         goto err;
11243e0c4386eSCy Schubert 
11244e0c4386eSCy Schubert     cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
11245e0c4386eSCy Schubert     if (cert8192 == NULL)
11246e0c4386eSCy Schubert         goto err;
11247e0c4386eSCy Schubert 
11248e0c4386eSCy Schubert     privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
11249e0c4386eSCy Schubert     if (privkey8192 == NULL)
11250e0c4386eSCy Schubert         goto err;
11251e0c4386eSCy Schubert 
11252e0c4386eSCy Schubert #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
11253e0c4386eSCy Schubert # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
11254e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
11255e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
11256e0c4386eSCy Schubert # endif
11257e0c4386eSCy Schubert #endif
11258e0c4386eSCy Schubert     ADD_TEST(test_large_message_tls);
11259e0c4386eSCy Schubert     ADD_TEST(test_large_message_tls_read_ahead);
11260e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS
11261e0c4386eSCy Schubert     ADD_TEST(test_large_message_dtls);
11262e0c4386eSCy Schubert #endif
11263e0c4386eSCy Schubert     ADD_ALL_TESTS(test_large_app_data, 28);
11264e0c4386eSCy Schubert     ADD_TEST(test_cleanse_plaintext);
11265e0c4386eSCy Schubert #ifndef OPENSSL_NO_OCSP
11266e0c4386eSCy Schubert     ADD_TEST(test_tlsext_status_type);
11267e0c4386eSCy Schubert #endif
11268e0c4386eSCy Schubert     ADD_TEST(test_session_with_only_int_cache);
11269e0c4386eSCy Schubert     ADD_TEST(test_session_with_only_ext_cache);
11270e0c4386eSCy Schubert     ADD_TEST(test_session_with_both_cache);
11271e0c4386eSCy Schubert     ADD_TEST(test_session_wo_ca_names);
11272e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11273e0c4386eSCy Schubert     ADD_ALL_TESTS(test_stateful_tickets, 3);
11274e0c4386eSCy Schubert     ADD_ALL_TESTS(test_stateless_tickets, 3);
11275e0c4386eSCy Schubert     ADD_TEST(test_psk_tickets);
11276e0c4386eSCy Schubert     ADD_ALL_TESTS(test_extra_tickets, 6);
11277e0c4386eSCy Schubert #endif
11278e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
11279e0c4386eSCy Schubert     ADD_TEST(test_ssl_bio_pop_next_bio);
11280e0c4386eSCy Schubert     ADD_TEST(test_ssl_bio_pop_ssl_bio);
11281e0c4386eSCy Schubert     ADD_TEST(test_ssl_bio_change_rbio);
11282e0c4386eSCy Schubert     ADD_TEST(test_ssl_bio_change_wbio);
11283e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
11284e0c4386eSCy Schubert     ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
11285e0c4386eSCy Schubert     ADD_TEST(test_keylog);
11286e0c4386eSCy Schubert #endif
11287e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11288e0c4386eSCy Schubert     ADD_TEST(test_keylog_no_master_key);
11289e0c4386eSCy Schubert #endif
11290e0c4386eSCy Schubert     ADD_TEST(test_client_cert_verify_cb);
11291e0c4386eSCy Schubert     ADD_TEST(test_ssl_build_cert_chain);
11292e0c4386eSCy Schubert     ADD_TEST(test_ssl_ctx_build_cert_chain);
11293e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
11294e0c4386eSCy Schubert     ADD_TEST(test_client_hello_cb);
11295e0c4386eSCy Schubert     ADD_TEST(test_no_ems);
11296e0c4386eSCy Schubert     ADD_TEST(test_ccs_change_cipher);
11297e0c4386eSCy Schubert #endif
11298e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11299e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_read_write, 6);
11300e0c4386eSCy Schubert     /*
11301e0c4386eSCy Schubert      * We don't do replay tests for external PSK. Replay protection isn't used
11302e0c4386eSCy Schubert      * in that scenario.
11303e0c4386eSCy Schubert      */
11304e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_replay, 2);
11305e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3);
11306e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3);
11307e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3);
11308e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3);
11309e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_not_sent, 3);
11310e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_psk, 8);
11311e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
11312e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_not_expected, 3);
11313e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
11314e0c4386eSCy Schubert     ADD_ALL_TESTS(test_early_data_tls1_2, 3);
11315e0c4386eSCy Schubert # endif
11316e0c4386eSCy Schubert #endif
11317e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11318e0c4386eSCy Schubert     ADD_ALL_TESTS(test_set_ciphersuite, 10);
11319e0c4386eSCy Schubert     ADD_TEST(test_ciphersuite_change);
11320e0c4386eSCy Schubert     ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
11321e0c4386eSCy Schubert # ifdef OPENSSL_NO_PSK
11322e0c4386eSCy Schubert     ADD_ALL_TESTS(test_tls13_psk, 1);
11323e0c4386eSCy Schubert # else
11324e0c4386eSCy Schubert     ADD_ALL_TESTS(test_tls13_psk, 4);
11325e0c4386eSCy Schubert # endif  /* OPENSSL_NO_PSK */
11326e0c4386eSCy Schubert # ifndef OPENSSL_NO_TLS1_2
11327e0c4386eSCy Schubert     /* Test with both TLSv1.3 and 1.2 versions */
11328e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_exchange, 14);
11329e0c4386eSCy Schubert #  if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
11330e0c4386eSCy Schubert     ADD_ALL_TESTS(test_negotiated_group,
11331e0c4386eSCy Schubert                   4 * (OSSL_NELEM(ecdhe_kexch_groups)
11332e0c4386eSCy Schubert                        + OSSL_NELEM(ffdhe_kexch_groups)));
11333e0c4386eSCy Schubert #  endif
11334e0c4386eSCy Schubert # else
11335e0c4386eSCy Schubert     /* Test with only TLSv1.3 versions */
11336e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_exchange, 12);
11337e0c4386eSCy Schubert # endif
11338e0c4386eSCy Schubert     ADD_ALL_TESTS(test_custom_exts, 6);
11339e0c4386eSCy Schubert     ADD_TEST(test_stateless);
11340e0c4386eSCy Schubert     ADD_TEST(test_pha_key_update);
11341e0c4386eSCy Schubert #else
11342e0c4386eSCy Schubert     ADD_ALL_TESTS(test_custom_exts, 3);
11343e0c4386eSCy Schubert #endif
11344e0c4386eSCy Schubert     ADD_ALL_TESTS(test_export_key_mat, 6);
11345e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11346e0c4386eSCy Schubert     ADD_ALL_TESTS(test_export_key_mat_early, 3);
11347e0c4386eSCy Schubert     ADD_TEST(test_key_update);
11348e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
11349e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
11350e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_update_local_in_write, 2);
11351e0c4386eSCy Schubert     ADD_ALL_TESTS(test_key_update_local_in_read, 2);
11352e0c4386eSCy Schubert #endif
11353e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ssl_clear, 2);
11354e0c4386eSCy Schubert     ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
11355e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
11356e0c4386eSCy Schubert     ADD_ALL_TESTS(test_srp, 6);
11357e0c4386eSCy Schubert #endif
11358e0c4386eSCy Schubert     ADD_ALL_TESTS(test_info_callback, 6);
11359e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ssl_pending, 2);
11360e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
11361e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ticket_callbacks, 20);
11362e0c4386eSCy Schubert     ADD_ALL_TESTS(test_shutdown, 7);
11363e0c4386eSCy Schubert     ADD_ALL_TESTS(test_incorrect_shutdown, 2);
11364e0c4386eSCy Schubert     ADD_ALL_TESTS(test_cert_cb, 6);
11365e0c4386eSCy Schubert     ADD_ALL_TESTS(test_client_cert_cb, 2);
11366e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ca_names, 3);
11367e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
11368e0c4386eSCy Schubert     ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
11369e0c4386eSCy Schubert #endif
11370e0c4386eSCy Schubert     ADD_ALL_TESTS(test_servername, 10);
11371e0c4386eSCy Schubert #if !defined(OPENSSL_NO_EC) \
11372e0c4386eSCy Schubert     && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
11373e0c4386eSCy Schubert     ADD_ALL_TESTS(test_sigalgs_available, 6);
11374e0c4386eSCy Schubert #endif
11375e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_3
11376e0c4386eSCy Schubert     ADD_ALL_TESTS(test_pluggable_group, 2);
11377e0c4386eSCy Schubert #endif
11378e0c4386eSCy Schubert #ifndef OPENSSL_NO_TLS1_2
11379e0c4386eSCy Schubert     ADD_TEST(test_ssl_dup);
11380e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
11381e0c4386eSCy Schubert     ADD_ALL_TESTS(test_set_tmp_dh, 11);
11382e0c4386eSCy Schubert     ADD_ALL_TESTS(test_dh_auto, 7);
11383e0c4386eSCy Schubert # endif
11384e0c4386eSCy Schubert #endif
11385e0c4386eSCy Schubert #ifndef OSSL_NO_USABLE_TLS1_3
11386e0c4386eSCy Schubert     ADD_TEST(test_sni_tls13);
11387e0c4386eSCy Schubert     ADD_ALL_TESTS(test_ticket_lifetime, 2);
11388e0c4386eSCy Schubert #endif
11389e0c4386eSCy Schubert     ADD_TEST(test_inherit_verify_param);
11390e0c4386eSCy Schubert     ADD_TEST(test_set_alpn);
11391e0c4386eSCy Schubert     ADD_TEST(test_set_verify_cert_store_ssl_ctx);
11392e0c4386eSCy Schubert     ADD_TEST(test_set_verify_cert_store_ssl);
11393e0c4386eSCy Schubert     ADD_ALL_TESTS(test_session_timeout, 1);
1139444096ebdSEnji Cooper #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
1139544096ebdSEnji Cooper     ADD_ALL_TESTS(test_session_cache_overflow, 4);
1139644096ebdSEnji Cooper #endif
11397e0c4386eSCy Schubert     ADD_TEST(test_load_dhfile);
11398e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
11399e0c4386eSCy Schubert     ADD_ALL_TESTS(test_serverinfo_custom, 4);
11400e0c4386eSCy Schubert #endif
11401e0c4386eSCy Schubert #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
11402e0c4386eSCy Schubert     ADD_ALL_TESTS(test_pipelining, 7);
11403e0c4386eSCy Schubert #endif
11404e0c4386eSCy Schubert     ADD_ALL_TESTS(test_handshake_retry, 16);
1140544096ebdSEnji Cooper     ADD_ALL_TESTS(test_multi_resume, 5);
11406a7148ab3SEnji Cooper     ADD_ALL_TESTS(test_select_next_proto, OSSL_NELEM(next_proto_tests));
11407a7148ab3SEnji Cooper #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
11408a7148ab3SEnji Cooper     ADD_ALL_TESTS(test_npn, 5);
11409a7148ab3SEnji Cooper #endif
11410a7148ab3SEnji Cooper     ADD_ALL_TESTS(test_alpn, 4);
11411e0c4386eSCy Schubert     return 1;
11412e0c4386eSCy Schubert 
11413e0c4386eSCy Schubert  err:
11414e0c4386eSCy Schubert     OPENSSL_free(cert);
11415e0c4386eSCy Schubert     OPENSSL_free(privkey);
11416e0c4386eSCy Schubert     OPENSSL_free(cert2);
11417e0c4386eSCy Schubert     OPENSSL_free(privkey2);
11418e0c4386eSCy Schubert     return 0;
11419e0c4386eSCy Schubert }
11420e0c4386eSCy Schubert 
cleanup_tests(void)11421e0c4386eSCy Schubert void cleanup_tests(void)
11422e0c4386eSCy Schubert {
11423e0c4386eSCy Schubert # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
11424e0c4386eSCy Schubert     EVP_PKEY_free(tmp_dh_params);
11425e0c4386eSCy Schubert #endif
11426e0c4386eSCy Schubert     OPENSSL_free(cert);
11427e0c4386eSCy Schubert     OPENSSL_free(privkey);
11428e0c4386eSCy Schubert     OPENSSL_free(cert2);
11429e0c4386eSCy Schubert     OPENSSL_free(privkey2);
11430e0c4386eSCy Schubert     OPENSSL_free(cert1024);
11431e0c4386eSCy Schubert     OPENSSL_free(privkey1024);
11432e0c4386eSCy Schubert     OPENSSL_free(cert3072);
11433e0c4386eSCy Schubert     OPENSSL_free(privkey3072);
11434e0c4386eSCy Schubert     OPENSSL_free(cert4096);
11435e0c4386eSCy Schubert     OPENSSL_free(privkey4096);
11436e0c4386eSCy Schubert     OPENSSL_free(cert8192);
11437e0c4386eSCy Schubert     OPENSSL_free(privkey8192);
11438e0c4386eSCy Schubert     bio_s_mempacket_test_free();
11439e0c4386eSCy Schubert     bio_s_always_retry_free();
11440e0c4386eSCy Schubert     OSSL_PROVIDER_unload(defctxnull);
11441e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
11442e0c4386eSCy Schubert }
11443