1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * We need access to the deprecated low level HMAC APIs for legacy purposes
12e1051a39Sopenharmony_ci * when the deprecated calls are not hidden
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
15e1051a39Sopenharmony_ci# define OPENSSL_SUPPRESS_DEPRECATED
16e1051a39Sopenharmony_ci#endif
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci#include <stdio.h>
19e1051a39Sopenharmony_ci#include <string.h>
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci#include <openssl/opensslconf.h>
22e1051a39Sopenharmony_ci#include <openssl/bio.h>
23e1051a39Sopenharmony_ci#include <openssl/crypto.h>
24e1051a39Sopenharmony_ci#include <openssl/ssl.h>
25e1051a39Sopenharmony_ci#include <openssl/ocsp.h>
26e1051a39Sopenharmony_ci#include <openssl/srp.h>
27e1051a39Sopenharmony_ci#include <openssl/txt_db.h>
28e1051a39Sopenharmony_ci#include <openssl/aes.h>
29e1051a39Sopenharmony_ci#include <openssl/rand.h>
30e1051a39Sopenharmony_ci#include <openssl/core_names.h>
31e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h>
32e1051a39Sopenharmony_ci#include <openssl/provider.h>
33e1051a39Sopenharmony_ci#include <openssl/param_build.h>
34e1051a39Sopenharmony_ci#include <openssl/x509v3.h>
35e1051a39Sopenharmony_ci#include <openssl/dh.h>
36e1051a39Sopenharmony_ci#include <openssl/engine.h>
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci#include "helpers/ssltestlib.h"
39e1051a39Sopenharmony_ci#include "testutil.h"
40e1051a39Sopenharmony_ci#include "testutil/output.h"
41e1051a39Sopenharmony_ci#include "internal/nelem.h"
42e1051a39Sopenharmony_ci#include "internal/ktls.h"
43e1051a39Sopenharmony_ci#include "../ssl/ssl_local.h"
44e1051a39Sopenharmony_ci#include "filterprov.h"
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci#undef OSSL_NO_USABLE_TLS1_3
47e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_TLS1_3) \
48e1051a39Sopenharmony_ci    || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
49e1051a39Sopenharmony_ci/*
50e1051a39Sopenharmony_ci * If we don't have ec or dh then there are no built-in groups that are usable
51e1051a39Sopenharmony_ci * with TLSv1.3
52e1051a39Sopenharmony_ci */
53e1051a39Sopenharmony_ci# define OSSL_NO_USABLE_TLS1_3
54e1051a39Sopenharmony_ci#endif
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci/* Defined in tls-provider.c */
57e1051a39Sopenharmony_ciint tls_provider_init(const OSSL_CORE_HANDLE *handle,
58e1051a39Sopenharmony_ci                      const OSSL_DISPATCH *in,
59e1051a39Sopenharmony_ci                      const OSSL_DISPATCH **out,
60e1051a39Sopenharmony_ci                      void **provctx);
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_cistatic OSSL_LIB_CTX *libctx = NULL;
63e1051a39Sopenharmony_cistatic OSSL_PROVIDER *defctxnull = NULL;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_cistatic SSL_SESSION *clientpsk = NULL;
68e1051a39Sopenharmony_cistatic SSL_SESSION *serverpsk = NULL;
69e1051a39Sopenharmony_cistatic const char *pskid = "Identity";
70e1051a39Sopenharmony_cistatic const char *srvid;
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_cistatic int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
73e1051a39Sopenharmony_ci                          size_t *idlen, SSL_SESSION **sess);
74e1051a39Sopenharmony_cistatic int find_session_cb(SSL *ssl, const unsigned char *identity,
75e1051a39Sopenharmony_ci                           size_t identity_len, SSL_SESSION **sess);
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_cistatic int use_session_cb_cnt = 0;
78e1051a39Sopenharmony_cistatic int find_session_cb_cnt = 0;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_cistatic SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize);
81e1051a39Sopenharmony_ci#endif
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_cistatic char *certsdir = NULL;
84e1051a39Sopenharmony_cistatic char *cert = NULL;
85e1051a39Sopenharmony_cistatic char *privkey = NULL;
86e1051a39Sopenharmony_cistatic char *cert2 = NULL;
87e1051a39Sopenharmony_cistatic char *privkey2 = NULL;
88e1051a39Sopenharmony_cistatic char *cert1024 = NULL;
89e1051a39Sopenharmony_cistatic char *privkey1024 = NULL;
90e1051a39Sopenharmony_cistatic char *cert3072 = NULL;
91e1051a39Sopenharmony_cistatic char *privkey3072 = NULL;
92e1051a39Sopenharmony_cistatic char *cert4096 = NULL;
93e1051a39Sopenharmony_cistatic char *privkey4096 = NULL;
94e1051a39Sopenharmony_cistatic char *cert8192 = NULL;
95e1051a39Sopenharmony_cistatic char *privkey8192 = NULL;
96e1051a39Sopenharmony_cistatic char *srpvfile = NULL;
97e1051a39Sopenharmony_cistatic char *tmpfilename = NULL;
98e1051a39Sopenharmony_cistatic char *dhfile = NULL;
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_cistatic int is_fips = 0;
101e1051a39Sopenharmony_ci
102e1051a39Sopenharmony_ci#define LOG_BUFFER_SIZE 2048
103e1051a39Sopenharmony_cistatic char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
104e1051a39Sopenharmony_cistatic size_t server_log_buffer_index = 0;
105e1051a39Sopenharmony_cistatic char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
106e1051a39Sopenharmony_cistatic size_t client_log_buffer_index = 0;
107e1051a39Sopenharmony_cistatic int error_writing_log = 0;
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCSP
110e1051a39Sopenharmony_cistatic const unsigned char orespder[] = "Dummy OCSP Response";
111e1051a39Sopenharmony_cistatic int ocsp_server_called = 0;
112e1051a39Sopenharmony_cistatic int ocsp_client_called = 0;
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_cistatic int cdummyarg = 1;
115e1051a39Sopenharmony_cistatic X509 *ocspcert = NULL;
116e1051a39Sopenharmony_ci#endif
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ci#define NUM_EXTRA_CERTS 40
119e1051a39Sopenharmony_ci#define CLIENT_VERSION_LEN      2
120e1051a39Sopenharmony_ci
121e1051a39Sopenharmony_ci/*
122e1051a39Sopenharmony_ci * This structure is used to validate that the correct number of log messages
123e1051a39Sopenharmony_ci * of various types are emitted when emitting secret logs.
124e1051a39Sopenharmony_ci */
125e1051a39Sopenharmony_cistruct sslapitest_log_counts {
126e1051a39Sopenharmony_ci    unsigned int rsa_key_exchange_count;
127e1051a39Sopenharmony_ci    unsigned int master_secret_count;
128e1051a39Sopenharmony_ci    unsigned int client_early_secret_count;
129e1051a39Sopenharmony_ci    unsigned int client_handshake_secret_count;
130e1051a39Sopenharmony_ci    unsigned int server_handshake_secret_count;
131e1051a39Sopenharmony_ci    unsigned int client_application_secret_count;
132e1051a39Sopenharmony_ci    unsigned int server_application_secret_count;
133e1051a39Sopenharmony_ci    unsigned int early_exporter_secret_count;
134e1051a39Sopenharmony_ci    unsigned int exporter_secret_count;
135e1051a39Sopenharmony_ci};
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_cistatic int hostname_cb(SSL *s, int *al, void *arg)
139e1051a39Sopenharmony_ci{
140e1051a39Sopenharmony_ci    const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
141e1051a39Sopenharmony_ci
142e1051a39Sopenharmony_ci    if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
143e1051a39Sopenharmony_ci                             || strcmp(hostname, "altgoodhost") == 0))
144e1051a39Sopenharmony_ci        return  SSL_TLSEXT_ERR_OK;
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    return SSL_TLSEXT_ERR_NOACK;
147e1051a39Sopenharmony_ci}
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_cistatic void client_keylog_callback(const SSL *ssl, const char *line)
150e1051a39Sopenharmony_ci{
151e1051a39Sopenharmony_ci    int line_length = strlen(line);
152e1051a39Sopenharmony_ci
153e1051a39Sopenharmony_ci    /* If the log doesn't fit, error out. */
154e1051a39Sopenharmony_ci    if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
155e1051a39Sopenharmony_ci        TEST_info("Client log too full");
156e1051a39Sopenharmony_ci        error_writing_log = 1;
157e1051a39Sopenharmony_ci        return;
158e1051a39Sopenharmony_ci    }
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ci    strcat(client_log_buffer, line);
161e1051a39Sopenharmony_ci    client_log_buffer_index += line_length;
162e1051a39Sopenharmony_ci    client_log_buffer[client_log_buffer_index++] = '\n';
163e1051a39Sopenharmony_ci}
164e1051a39Sopenharmony_ci
165e1051a39Sopenharmony_cistatic void server_keylog_callback(const SSL *ssl, const char *line)
166e1051a39Sopenharmony_ci{
167e1051a39Sopenharmony_ci    int line_length = strlen(line);
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ci    /* If the log doesn't fit, error out. */
170e1051a39Sopenharmony_ci    if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
171e1051a39Sopenharmony_ci        TEST_info("Server log too full");
172e1051a39Sopenharmony_ci        error_writing_log = 1;
173e1051a39Sopenharmony_ci        return;
174e1051a39Sopenharmony_ci    }
175e1051a39Sopenharmony_ci
176e1051a39Sopenharmony_ci    strcat(server_log_buffer, line);
177e1051a39Sopenharmony_ci    server_log_buffer_index += line_length;
178e1051a39Sopenharmony_ci    server_log_buffer[server_log_buffer_index++] = '\n';
179e1051a39Sopenharmony_ci}
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_cistatic int compare_hex_encoded_buffer(const char *hex_encoded,
182e1051a39Sopenharmony_ci                                      size_t hex_length,
183e1051a39Sopenharmony_ci                                      const uint8_t *raw,
184e1051a39Sopenharmony_ci                                      size_t raw_length)
185e1051a39Sopenharmony_ci{
186e1051a39Sopenharmony_ci    size_t i, j;
187e1051a39Sopenharmony_ci    char hexed[3];
188e1051a39Sopenharmony_ci
189e1051a39Sopenharmony_ci    if (!TEST_size_t_eq(raw_length * 2, hex_length))
190e1051a39Sopenharmony_ci        return 1;
191e1051a39Sopenharmony_ci
192e1051a39Sopenharmony_ci    for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
193e1051a39Sopenharmony_ci        sprintf(hexed, "%02x", raw[i]);
194e1051a39Sopenharmony_ci        if (!TEST_int_eq(hexed[0], hex_encoded[j])
195e1051a39Sopenharmony_ci                || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
196e1051a39Sopenharmony_ci            return 1;
197e1051a39Sopenharmony_ci    }
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci    return 0;
200e1051a39Sopenharmony_ci}
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_cistatic int test_keylog_output(char *buffer, const SSL *ssl,
203e1051a39Sopenharmony_ci                              const SSL_SESSION *session,
204e1051a39Sopenharmony_ci                              struct sslapitest_log_counts *expected)
205e1051a39Sopenharmony_ci{
206e1051a39Sopenharmony_ci    char *token = NULL;
207e1051a39Sopenharmony_ci    unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
208e1051a39Sopenharmony_ci    size_t client_random_size = SSL3_RANDOM_SIZE;
209e1051a39Sopenharmony_ci    unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
210e1051a39Sopenharmony_ci    size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
211e1051a39Sopenharmony_ci    unsigned int rsa_key_exchange_count = 0;
212e1051a39Sopenharmony_ci    unsigned int master_secret_count = 0;
213e1051a39Sopenharmony_ci    unsigned int client_early_secret_count = 0;
214e1051a39Sopenharmony_ci    unsigned int client_handshake_secret_count = 0;
215e1051a39Sopenharmony_ci    unsigned int server_handshake_secret_count = 0;
216e1051a39Sopenharmony_ci    unsigned int client_application_secret_count = 0;
217e1051a39Sopenharmony_ci    unsigned int server_application_secret_count = 0;
218e1051a39Sopenharmony_ci    unsigned int early_exporter_secret_count = 0;
219e1051a39Sopenharmony_ci    unsigned int exporter_secret_count = 0;
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_ci    for (token = strtok(buffer, " \n"); token != NULL;
222e1051a39Sopenharmony_ci         token = strtok(NULL, " \n")) {
223e1051a39Sopenharmony_ci        if (strcmp(token, "RSA") == 0) {
224e1051a39Sopenharmony_ci            /*
225e1051a39Sopenharmony_ci             * Premaster secret. Tokens should be: 16 ASCII bytes of
226e1051a39Sopenharmony_ci             * hex-encoded encrypted secret, then the hex-encoded pre-master
227e1051a39Sopenharmony_ci             * secret.
228e1051a39Sopenharmony_ci             */
229e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
230e1051a39Sopenharmony_ci                return 0;
231e1051a39Sopenharmony_ci            if (!TEST_size_t_eq(strlen(token), 16))
232e1051a39Sopenharmony_ci                return 0;
233e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
234e1051a39Sopenharmony_ci                return 0;
235e1051a39Sopenharmony_ci            /*
236e1051a39Sopenharmony_ci             * We can't sensibly check the log because the premaster secret is
237e1051a39Sopenharmony_ci             * transient, and OpenSSL doesn't keep hold of it once the master
238e1051a39Sopenharmony_ci             * secret is generated.
239e1051a39Sopenharmony_ci             */
240e1051a39Sopenharmony_ci            rsa_key_exchange_count++;
241e1051a39Sopenharmony_ci        } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
242e1051a39Sopenharmony_ci            /*
243e1051a39Sopenharmony_ci             * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
244e1051a39Sopenharmony_ci             * client random, then the hex-encoded master secret.
245e1051a39Sopenharmony_ci             */
246e1051a39Sopenharmony_ci            client_random_size = SSL_get_client_random(ssl,
247e1051a39Sopenharmony_ci                                                       actual_client_random,
248e1051a39Sopenharmony_ci                                                       SSL3_RANDOM_SIZE);
249e1051a39Sopenharmony_ci            if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
250e1051a39Sopenharmony_ci                return 0;
251e1051a39Sopenharmony_ci
252e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
253e1051a39Sopenharmony_ci                return 0;
254e1051a39Sopenharmony_ci            if (!TEST_size_t_eq(strlen(token), 64))
255e1051a39Sopenharmony_ci                return 0;
256e1051a39Sopenharmony_ci            if (!TEST_false(compare_hex_encoded_buffer(token, 64,
257e1051a39Sopenharmony_ci                                                       actual_client_random,
258e1051a39Sopenharmony_ci                                                       client_random_size)))
259e1051a39Sopenharmony_ci                return 0;
260e1051a39Sopenharmony_ci
261e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
262e1051a39Sopenharmony_ci                return 0;
263e1051a39Sopenharmony_ci            master_key_size = SSL_SESSION_get_master_key(session,
264e1051a39Sopenharmony_ci                                                         actual_master_key,
265e1051a39Sopenharmony_ci                                                         master_key_size);
266e1051a39Sopenharmony_ci            if (!TEST_size_t_ne(master_key_size, 0))
267e1051a39Sopenharmony_ci                return 0;
268e1051a39Sopenharmony_ci            if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
269e1051a39Sopenharmony_ci                                                       actual_master_key,
270e1051a39Sopenharmony_ci                                                       master_key_size)))
271e1051a39Sopenharmony_ci                return 0;
272e1051a39Sopenharmony_ci            master_secret_count++;
273e1051a39Sopenharmony_ci        } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
274e1051a39Sopenharmony_ci                    || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
275e1051a39Sopenharmony_ci                    || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
276e1051a39Sopenharmony_ci                    || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
277e1051a39Sopenharmony_ci                    || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
278e1051a39Sopenharmony_ci                    || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
279e1051a39Sopenharmony_ci                    || strcmp(token, "EXPORTER_SECRET") == 0) {
280e1051a39Sopenharmony_ci            /*
281e1051a39Sopenharmony_ci             * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
282e1051a39Sopenharmony_ci             * client random, and then the hex-encoded secret. In this case,
283e1051a39Sopenharmony_ci             * we treat all of these secrets identically and then just
284e1051a39Sopenharmony_ci             * distinguish between them when counting what we saw.
285e1051a39Sopenharmony_ci             */
286e1051a39Sopenharmony_ci            if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
287e1051a39Sopenharmony_ci                client_early_secret_count++;
288e1051a39Sopenharmony_ci            else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
289e1051a39Sopenharmony_ci                client_handshake_secret_count++;
290e1051a39Sopenharmony_ci            else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
291e1051a39Sopenharmony_ci                server_handshake_secret_count++;
292e1051a39Sopenharmony_ci            else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
293e1051a39Sopenharmony_ci                client_application_secret_count++;
294e1051a39Sopenharmony_ci            else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
295e1051a39Sopenharmony_ci                server_application_secret_count++;
296e1051a39Sopenharmony_ci            else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
297e1051a39Sopenharmony_ci                early_exporter_secret_count++;
298e1051a39Sopenharmony_ci            else if (strcmp(token, "EXPORTER_SECRET") == 0)
299e1051a39Sopenharmony_ci                exporter_secret_count++;
300e1051a39Sopenharmony_ci
301e1051a39Sopenharmony_ci            client_random_size = SSL_get_client_random(ssl,
302e1051a39Sopenharmony_ci                                                       actual_client_random,
303e1051a39Sopenharmony_ci                                                       SSL3_RANDOM_SIZE);
304e1051a39Sopenharmony_ci            if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
305e1051a39Sopenharmony_ci                return 0;
306e1051a39Sopenharmony_ci
307e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
308e1051a39Sopenharmony_ci                return 0;
309e1051a39Sopenharmony_ci            if (!TEST_size_t_eq(strlen(token), 64))
310e1051a39Sopenharmony_ci                return 0;
311e1051a39Sopenharmony_ci            if (!TEST_false(compare_hex_encoded_buffer(token, 64,
312e1051a39Sopenharmony_ci                                                       actual_client_random,
313e1051a39Sopenharmony_ci                                                       client_random_size)))
314e1051a39Sopenharmony_ci                return 0;
315e1051a39Sopenharmony_ci
316e1051a39Sopenharmony_ci            if (!TEST_ptr(token = strtok(NULL, " \n")))
317e1051a39Sopenharmony_ci                return 0;
318e1051a39Sopenharmony_ci        } else {
319e1051a39Sopenharmony_ci            TEST_info("Unexpected token %s\n", token);
320e1051a39Sopenharmony_ci            return 0;
321e1051a39Sopenharmony_ci        }
322e1051a39Sopenharmony_ci    }
323e1051a39Sopenharmony_ci
324e1051a39Sopenharmony_ci    /* Got what we expected? */
325e1051a39Sopenharmony_ci    if (!TEST_size_t_eq(rsa_key_exchange_count,
326e1051a39Sopenharmony_ci                        expected->rsa_key_exchange_count)
327e1051a39Sopenharmony_ci            || !TEST_size_t_eq(master_secret_count,
328e1051a39Sopenharmony_ci                               expected->master_secret_count)
329e1051a39Sopenharmony_ci            || !TEST_size_t_eq(client_early_secret_count,
330e1051a39Sopenharmony_ci                               expected->client_early_secret_count)
331e1051a39Sopenharmony_ci            || !TEST_size_t_eq(client_handshake_secret_count,
332e1051a39Sopenharmony_ci                               expected->client_handshake_secret_count)
333e1051a39Sopenharmony_ci            || !TEST_size_t_eq(server_handshake_secret_count,
334e1051a39Sopenharmony_ci                               expected->server_handshake_secret_count)
335e1051a39Sopenharmony_ci            || !TEST_size_t_eq(client_application_secret_count,
336e1051a39Sopenharmony_ci                               expected->client_application_secret_count)
337e1051a39Sopenharmony_ci            || !TEST_size_t_eq(server_application_secret_count,
338e1051a39Sopenharmony_ci                               expected->server_application_secret_count)
339e1051a39Sopenharmony_ci            || !TEST_size_t_eq(early_exporter_secret_count,
340e1051a39Sopenharmony_ci                               expected->early_exporter_secret_count)
341e1051a39Sopenharmony_ci            || !TEST_size_t_eq(exporter_secret_count,
342e1051a39Sopenharmony_ci                               expected->exporter_secret_count))
343e1051a39Sopenharmony_ci        return 0;
344e1051a39Sopenharmony_ci    return 1;
345e1051a39Sopenharmony_ci}
346e1051a39Sopenharmony_ci
347e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
348e1051a39Sopenharmony_cistatic int test_keylog(void)
349e1051a39Sopenharmony_ci{
350e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
351e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
352e1051a39Sopenharmony_ci    int testresult = 0;
353e1051a39Sopenharmony_ci    struct sslapitest_log_counts expected;
354e1051a39Sopenharmony_ci
355e1051a39Sopenharmony_ci    /* Clean up logging space */
356e1051a39Sopenharmony_ci    memset(&expected, 0, sizeof(expected));
357e1051a39Sopenharmony_ci    memset(client_log_buffer, 0, sizeof(client_log_buffer));
358e1051a39Sopenharmony_ci    memset(server_log_buffer, 0, sizeof(server_log_buffer));
359e1051a39Sopenharmony_ci    client_log_buffer_index = 0;
360e1051a39Sopenharmony_ci    server_log_buffer_index = 0;
361e1051a39Sopenharmony_ci    error_writing_log = 0;
362e1051a39Sopenharmony_ci
363e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
364e1051a39Sopenharmony_ci                                       TLS_client_method(),
365e1051a39Sopenharmony_ci                                       TLS1_VERSION, 0,
366e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
367e1051a39Sopenharmony_ci        return 0;
368e1051a39Sopenharmony_ci
369e1051a39Sopenharmony_ci    /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
370e1051a39Sopenharmony_ci    SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
371e1051a39Sopenharmony_ci    SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
372e1051a39Sopenharmony_ci
373e1051a39Sopenharmony_ci    /* We also want to ensure that we use RSA-based key exchange. */
374e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
375e1051a39Sopenharmony_ci        goto end;
376e1051a39Sopenharmony_ci
377e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
378e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
379e1051a39Sopenharmony_ci        goto end;
380e1051a39Sopenharmony_ci    SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
381e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
382e1051a39Sopenharmony_ci                   == client_keylog_callback))
383e1051a39Sopenharmony_ci        goto end;
384e1051a39Sopenharmony_ci    SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
385e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
386e1051a39Sopenharmony_ci                   == server_keylog_callback))
387e1051a39Sopenharmony_ci        goto end;
388e1051a39Sopenharmony_ci
389e1051a39Sopenharmony_ci    /* Now do a handshake and check that the logs have been written to. */
390e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
391e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
392e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
393e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
394e1051a39Sopenharmony_ci            || !TEST_false(error_writing_log)
395e1051a39Sopenharmony_ci            || !TEST_int_gt(client_log_buffer_index, 0)
396e1051a39Sopenharmony_ci            || !TEST_int_gt(server_log_buffer_index, 0))
397e1051a39Sopenharmony_ci        goto end;
398e1051a39Sopenharmony_ci
399e1051a39Sopenharmony_ci    /*
400e1051a39Sopenharmony_ci     * Now we want to test that our output data was vaguely sensible. We
401e1051a39Sopenharmony_ci     * do that by using strtok and confirming that we have more or less the
402e1051a39Sopenharmony_ci     * data we expect. For both client and server, we expect to see one master
403e1051a39Sopenharmony_ci     * secret. The client should also see an RSA key exchange.
404e1051a39Sopenharmony_ci     */
405e1051a39Sopenharmony_ci    expected.rsa_key_exchange_count = 1;
406e1051a39Sopenharmony_ci    expected.master_secret_count = 1;
407e1051a39Sopenharmony_ci    if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
408e1051a39Sopenharmony_ci                                      SSL_get_session(clientssl), &expected)))
409e1051a39Sopenharmony_ci        goto end;
410e1051a39Sopenharmony_ci
411e1051a39Sopenharmony_ci    expected.rsa_key_exchange_count = 0;
412e1051a39Sopenharmony_ci    if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
413e1051a39Sopenharmony_ci                                      SSL_get_session(serverssl), &expected)))
414e1051a39Sopenharmony_ci        goto end;
415e1051a39Sopenharmony_ci
416e1051a39Sopenharmony_ci    testresult = 1;
417e1051a39Sopenharmony_ci
418e1051a39Sopenharmony_ciend:
419e1051a39Sopenharmony_ci    SSL_free(serverssl);
420e1051a39Sopenharmony_ci    SSL_free(clientssl);
421e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
422e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
423e1051a39Sopenharmony_ci
424e1051a39Sopenharmony_ci    return testresult;
425e1051a39Sopenharmony_ci}
426e1051a39Sopenharmony_ci#endif
427e1051a39Sopenharmony_ci
428e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
429e1051a39Sopenharmony_cistatic int test_keylog_no_master_key(void)
430e1051a39Sopenharmony_ci{
431e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
432e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
433e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
434e1051a39Sopenharmony_ci    int testresult = 0;
435e1051a39Sopenharmony_ci    struct sslapitest_log_counts expected;
436e1051a39Sopenharmony_ci    unsigned char buf[1];
437e1051a39Sopenharmony_ci    size_t readbytes, written;
438e1051a39Sopenharmony_ci
439e1051a39Sopenharmony_ci    /* Clean up logging space */
440e1051a39Sopenharmony_ci    memset(&expected, 0, sizeof(expected));
441e1051a39Sopenharmony_ci    memset(client_log_buffer, 0, sizeof(client_log_buffer));
442e1051a39Sopenharmony_ci    memset(server_log_buffer, 0, sizeof(server_log_buffer));
443e1051a39Sopenharmony_ci    client_log_buffer_index = 0;
444e1051a39Sopenharmony_ci    server_log_buffer_index = 0;
445e1051a39Sopenharmony_ci    error_writing_log = 0;
446e1051a39Sopenharmony_ci
447e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
448e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
449e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
450e1051a39Sopenharmony_ci        || !TEST_true(SSL_CTX_set_max_early_data(sctx,
451e1051a39Sopenharmony_ci                                                 SSL3_RT_MAX_PLAIN_LENGTH)))
452e1051a39Sopenharmony_ci        return 0;
453e1051a39Sopenharmony_ci
454e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
455e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
456e1051a39Sopenharmony_ci        goto end;
457e1051a39Sopenharmony_ci
458e1051a39Sopenharmony_ci    SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
459e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
460e1051a39Sopenharmony_ci                   == client_keylog_callback))
461e1051a39Sopenharmony_ci        goto end;
462e1051a39Sopenharmony_ci
463e1051a39Sopenharmony_ci    SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
464e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
465e1051a39Sopenharmony_ci                   == server_keylog_callback))
466e1051a39Sopenharmony_ci        goto end;
467e1051a39Sopenharmony_ci
468e1051a39Sopenharmony_ci    /* Now do a handshake and check that the logs have been written to. */
469e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
470e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
471e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
472e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
473e1051a39Sopenharmony_ci            || !TEST_false(error_writing_log))
474e1051a39Sopenharmony_ci        goto end;
475e1051a39Sopenharmony_ci
476e1051a39Sopenharmony_ci    /*
477e1051a39Sopenharmony_ci     * Now we want to test that our output data was vaguely sensible. For this
478e1051a39Sopenharmony_ci     * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
479e1051a39Sopenharmony_ci     * TLSv1.3, but we do expect both client and server to emit keys.
480e1051a39Sopenharmony_ci     */
481e1051a39Sopenharmony_ci    expected.client_handshake_secret_count = 1;
482e1051a39Sopenharmony_ci    expected.server_handshake_secret_count = 1;
483e1051a39Sopenharmony_ci    expected.client_application_secret_count = 1;
484e1051a39Sopenharmony_ci    expected.server_application_secret_count = 1;
485e1051a39Sopenharmony_ci    expected.exporter_secret_count = 1;
486e1051a39Sopenharmony_ci    if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
487e1051a39Sopenharmony_ci                                      SSL_get_session(clientssl), &expected))
488e1051a39Sopenharmony_ci            || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
489e1051a39Sopenharmony_ci                                             SSL_get_session(serverssl),
490e1051a39Sopenharmony_ci                                             &expected)))
491e1051a39Sopenharmony_ci        goto end;
492e1051a39Sopenharmony_ci
493e1051a39Sopenharmony_ci    /* Terminate old session and resume with early data. */
494e1051a39Sopenharmony_ci    sess = SSL_get1_session(clientssl);
495e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
496e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
497e1051a39Sopenharmony_ci    SSL_free(serverssl);
498e1051a39Sopenharmony_ci    SSL_free(clientssl);
499e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
500e1051a39Sopenharmony_ci
501e1051a39Sopenharmony_ci    /* Reset key log */
502e1051a39Sopenharmony_ci    memset(client_log_buffer, 0, sizeof(client_log_buffer));
503e1051a39Sopenharmony_ci    memset(server_log_buffer, 0, sizeof(server_log_buffer));
504e1051a39Sopenharmony_ci    client_log_buffer_index = 0;
505e1051a39Sopenharmony_ci    server_log_buffer_index = 0;
506e1051a39Sopenharmony_ci
507e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
508e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
509e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, sess))
510e1051a39Sopenharmony_ci            /* Here writing 0 length early data is enough. */
511e1051a39Sopenharmony_ci            || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
512e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
513e1051a39Sopenharmony_ci                                                &readbytes),
514e1051a39Sopenharmony_ci                            SSL_READ_EARLY_DATA_ERROR)
515e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
516e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_ACCEPTED)
517e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
518e1051a39Sopenharmony_ci                          SSL_ERROR_NONE))
519e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
520e1051a39Sopenharmony_ci        goto end;
521e1051a39Sopenharmony_ci
522e1051a39Sopenharmony_ci    /* In addition to the previous entries, expect early secrets. */
523e1051a39Sopenharmony_ci    expected.client_early_secret_count = 1;
524e1051a39Sopenharmony_ci    expected.early_exporter_secret_count = 1;
525e1051a39Sopenharmony_ci    if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
526e1051a39Sopenharmony_ci                                      SSL_get_session(clientssl), &expected))
527e1051a39Sopenharmony_ci            || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
528e1051a39Sopenharmony_ci                                             SSL_get_session(serverssl),
529e1051a39Sopenharmony_ci                                             &expected)))
530e1051a39Sopenharmony_ci        goto end;
531e1051a39Sopenharmony_ci
532e1051a39Sopenharmony_ci    testresult = 1;
533e1051a39Sopenharmony_ci
534e1051a39Sopenharmony_ciend:
535e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
536e1051a39Sopenharmony_ci    SSL_free(serverssl);
537e1051a39Sopenharmony_ci    SSL_free(clientssl);
538e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
539e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
540e1051a39Sopenharmony_ci
541e1051a39Sopenharmony_ci    return testresult;
542e1051a39Sopenharmony_ci}
543e1051a39Sopenharmony_ci#endif
544e1051a39Sopenharmony_ci
545e1051a39Sopenharmony_cistatic int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
546e1051a39Sopenharmony_ci{
547e1051a39Sopenharmony_ci    int res = X509_verify_cert(ctx);
548e1051a39Sopenharmony_ci    int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
549e1051a39Sopenharmony_ci    SSL *ssl;
550e1051a39Sopenharmony_ci
551e1051a39Sopenharmony_ci    /* this should not happen but check anyway */
552e1051a39Sopenharmony_ci    if (idx < 0
553e1051a39Sopenharmony_ci        || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
554e1051a39Sopenharmony_ci        return 0;
555e1051a39Sopenharmony_ci
556e1051a39Sopenharmony_ci    if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
557e1051a39Sopenharmony_ci        X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
558e1051a39Sopenharmony_ci        /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
559e1051a39Sopenharmony_ci        return SSL_set_retry_verify(ssl);
560e1051a39Sopenharmony_ci
561e1051a39Sopenharmony_ci    return res;
562e1051a39Sopenharmony_ci}
563e1051a39Sopenharmony_ci
564e1051a39Sopenharmony_cistatic int test_client_cert_verify_cb(void)
565e1051a39Sopenharmony_ci{
566e1051a39Sopenharmony_ci    /* server key, cert, chain, and root */
567e1051a39Sopenharmony_ci    char *skey = test_mk_file_path(certsdir, "leaf.key");
568e1051a39Sopenharmony_ci    char *leaf = test_mk_file_path(certsdir, "leaf.pem");
569e1051a39Sopenharmony_ci    char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
570e1051a39Sopenharmony_ci    char *int1 = test_mk_file_path(certsdir, "interCA.pem");
571e1051a39Sopenharmony_ci    char *root = test_mk_file_path(certsdir, "rootCA.pem");
572e1051a39Sopenharmony_ci    X509 *crt1 = NULL, *crt2 = NULL;
573e1051a39Sopenharmony_ci    STACK_OF(X509) *server_chain;
574e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
575e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
576e1051a39Sopenharmony_ci    int testresult = 0;
577e1051a39Sopenharmony_ci
578e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
579e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
580e1051a39Sopenharmony_ci                                       &sctx, &cctx, NULL, NULL)))
581e1051a39Sopenharmony_ci        goto end;
582e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
583e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
584e1051a39Sopenharmony_ci                                                        SSL_FILETYPE_PEM), 1)
585e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
586e1051a39Sopenharmony_ci        goto end;
587e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
588e1051a39Sopenharmony_ci        goto end;
589e1051a39Sopenharmony_ci    SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
590e1051a39Sopenharmony_ci    SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
591e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
592e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
593e1051a39Sopenharmony_ci        goto end;
594e1051a39Sopenharmony_ci
595e1051a39Sopenharmony_ci    /* attempt SSL_connect() with incomplete server chain */
596e1051a39Sopenharmony_ci    if (!TEST_false(create_ssl_connection(serverssl, clientssl,
597e1051a39Sopenharmony_ci                                          SSL_ERROR_WANT_RETRY_VERIFY)))
598e1051a39Sopenharmony_ci        goto end;
599e1051a39Sopenharmony_ci
600e1051a39Sopenharmony_ci    /* application provides intermediate certs needed to verify server cert */
601e1051a39Sopenharmony_ci    if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
602e1051a39Sopenharmony_ci        || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
603e1051a39Sopenharmony_ci        || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
604e1051a39Sopenharmony_ci        goto end;
605e1051a39Sopenharmony_ci    /* add certs in reverse order to demonstrate real chain building */
606e1051a39Sopenharmony_ci    if (!TEST_true(sk_X509_push(server_chain, crt1)))
607e1051a39Sopenharmony_ci        goto end;
608e1051a39Sopenharmony_ci    crt1 = NULL;
609e1051a39Sopenharmony_ci    if (!TEST_true(sk_X509_push(server_chain, crt2)))
610e1051a39Sopenharmony_ci        goto end;
611e1051a39Sopenharmony_ci    crt2 = NULL;
612e1051a39Sopenharmony_ci
613e1051a39Sopenharmony_ci    /* continue SSL_connect(), must now succeed with completed server chain */
614e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
615e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
616e1051a39Sopenharmony_ci        goto end;
617e1051a39Sopenharmony_ci
618e1051a39Sopenharmony_ci    testresult = 1;
619e1051a39Sopenharmony_ci
620e1051a39Sopenharmony_ciend:
621e1051a39Sopenharmony_ci    X509_free(crt1);
622e1051a39Sopenharmony_ci    X509_free(crt2);
623e1051a39Sopenharmony_ci    if (clientssl != NULL) {
624e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
625e1051a39Sopenharmony_ci        SSL_free(clientssl);
626e1051a39Sopenharmony_ci    }
627e1051a39Sopenharmony_ci    if (serverssl != NULL) {
628e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
629e1051a39Sopenharmony_ci        SSL_free(serverssl);
630e1051a39Sopenharmony_ci    }
631e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
632e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
633e1051a39Sopenharmony_ci
634e1051a39Sopenharmony_ci    OPENSSL_free(skey);
635e1051a39Sopenharmony_ci    OPENSSL_free(leaf);
636e1051a39Sopenharmony_ci    OPENSSL_free(int2);
637e1051a39Sopenharmony_ci    OPENSSL_free(int1);
638e1051a39Sopenharmony_ci    OPENSSL_free(root);
639e1051a39Sopenharmony_ci
640e1051a39Sopenharmony_ci    return testresult;
641e1051a39Sopenharmony_ci}
642e1051a39Sopenharmony_ci
643e1051a39Sopenharmony_cistatic int test_ssl_build_cert_chain(void)
644e1051a39Sopenharmony_ci{
645e1051a39Sopenharmony_ci    int ret = 0;
646e1051a39Sopenharmony_ci    SSL_CTX *ssl_ctx = NULL;
647e1051a39Sopenharmony_ci    SSL *ssl = NULL;
648e1051a39Sopenharmony_ci    char *skey = test_mk_file_path(certsdir, "leaf.key");
649e1051a39Sopenharmony_ci    char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
650e1051a39Sopenharmony_ci
651e1051a39Sopenharmony_ci    if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
652e1051a39Sopenharmony_ci        goto end;
653e1051a39Sopenharmony_ci    if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
654e1051a39Sopenharmony_ci        goto end;
655e1051a39Sopenharmony_ci    /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
656e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
657e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
658e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_check_private_key(ssl), 1))
659e1051a39Sopenharmony_ci        goto end;
660e1051a39Sopenharmony_ci    if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
661e1051a39Sopenharmony_ci                                             | SSL_BUILD_CHAIN_FLAG_CHECK)))
662e1051a39Sopenharmony_ci        goto end;
663e1051a39Sopenharmony_ci    ret = 1;
664e1051a39Sopenharmony_ciend:
665e1051a39Sopenharmony_ci    SSL_free(ssl);
666e1051a39Sopenharmony_ci    SSL_CTX_free(ssl_ctx);
667e1051a39Sopenharmony_ci    OPENSSL_free(leaf_chain);
668e1051a39Sopenharmony_ci    OPENSSL_free(skey);
669e1051a39Sopenharmony_ci    return ret;
670e1051a39Sopenharmony_ci}
671e1051a39Sopenharmony_ci
672e1051a39Sopenharmony_cistatic int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
673e1051a39Sopenharmony_ci{
674e1051a39Sopenharmony_ci    static const char pass[] = "testpass";
675e1051a39Sopenharmony_ci
676e1051a39Sopenharmony_ci    if (!TEST_int_eq(size, PEM_BUFSIZE))
677e1051a39Sopenharmony_ci        return -1;
678e1051a39Sopenharmony_ci
679e1051a39Sopenharmony_ci    memcpy(buf, pass, sizeof(pass) - 1);
680e1051a39Sopenharmony_ci    return sizeof(pass) - 1;
681e1051a39Sopenharmony_ci}
682e1051a39Sopenharmony_ci
683e1051a39Sopenharmony_cistatic int test_ssl_ctx_build_cert_chain(void)
684e1051a39Sopenharmony_ci{
685e1051a39Sopenharmony_ci    int ret = 0;
686e1051a39Sopenharmony_ci    SSL_CTX *ctx = NULL;
687e1051a39Sopenharmony_ci    char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
688e1051a39Sopenharmony_ci    char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
689e1051a39Sopenharmony_ci
690e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
691e1051a39Sopenharmony_ci        goto end;
692e1051a39Sopenharmony_ci    SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
693e1051a39Sopenharmony_ci    /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
694e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
695e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
696e1051a39Sopenharmony_ci                                                    SSL_FILETYPE_PEM), 1)
697e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
698e1051a39Sopenharmony_ci        goto end;
699e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
700e1051a39Sopenharmony_ci                                                | SSL_BUILD_CHAIN_FLAG_CHECK)))
701e1051a39Sopenharmony_ci        goto end;
702e1051a39Sopenharmony_ci    ret = 1;
703e1051a39Sopenharmony_ciend:
704e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
705e1051a39Sopenharmony_ci    OPENSSL_free(leaf_chain);
706e1051a39Sopenharmony_ci    OPENSSL_free(skey);
707e1051a39Sopenharmony_ci    return ret;
708e1051a39Sopenharmony_ci}
709e1051a39Sopenharmony_ci
710e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
711e1051a39Sopenharmony_cistatic int full_client_hello_callback(SSL *s, int *al, void *arg)
712e1051a39Sopenharmony_ci{
713e1051a39Sopenharmony_ci    int *ctr = arg;
714e1051a39Sopenharmony_ci    const unsigned char *p;
715e1051a39Sopenharmony_ci    int *exts;
716e1051a39Sopenharmony_ci    /* We only configure two ciphers, but the SCSV is added automatically. */
717e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_EC
718e1051a39Sopenharmony_ci    const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
719e1051a39Sopenharmony_ci#else
720e1051a39Sopenharmony_ci    const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
721e1051a39Sopenharmony_ci                                              0x2c, 0x00, 0xff};
722e1051a39Sopenharmony_ci#endif
723e1051a39Sopenharmony_ci    const int expected_extensions[] = {
724e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
725e1051a39Sopenharmony_ci                                       11, 10,
726e1051a39Sopenharmony_ci#endif
727e1051a39Sopenharmony_ci                                       35, 22, 23, 13};
728e1051a39Sopenharmony_ci    size_t len;
729e1051a39Sopenharmony_ci
730e1051a39Sopenharmony_ci    /* Make sure we can defer processing and get called back. */
731e1051a39Sopenharmony_ci    if ((*ctr)++ == 0)
732e1051a39Sopenharmony_ci        return SSL_CLIENT_HELLO_RETRY;
733e1051a39Sopenharmony_ci
734e1051a39Sopenharmony_ci    len = SSL_client_hello_get0_ciphers(s, &p);
735e1051a39Sopenharmony_ci    if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
736e1051a39Sopenharmony_ci            || !TEST_size_t_eq(
737e1051a39Sopenharmony_ci                       SSL_client_hello_get0_compression_methods(s, &p), 1)
738e1051a39Sopenharmony_ci            || !TEST_int_eq(*p, 0))
739e1051a39Sopenharmony_ci        return SSL_CLIENT_HELLO_ERROR;
740e1051a39Sopenharmony_ci    if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
741e1051a39Sopenharmony_ci        return SSL_CLIENT_HELLO_ERROR;
742e1051a39Sopenharmony_ci    if (len != OSSL_NELEM(expected_extensions) ||
743e1051a39Sopenharmony_ci        memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
744e1051a39Sopenharmony_ci        printf("ClientHello callback expected extensions mismatch\n");
745e1051a39Sopenharmony_ci        OPENSSL_free(exts);
746e1051a39Sopenharmony_ci        return SSL_CLIENT_HELLO_ERROR;
747e1051a39Sopenharmony_ci    }
748e1051a39Sopenharmony_ci    OPENSSL_free(exts);
749e1051a39Sopenharmony_ci    return SSL_CLIENT_HELLO_SUCCESS;
750e1051a39Sopenharmony_ci}
751e1051a39Sopenharmony_ci
752e1051a39Sopenharmony_cistatic int test_client_hello_cb(void)
753e1051a39Sopenharmony_ci{
754e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
755e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
756e1051a39Sopenharmony_ci    int testctr = 0, testresult = 0;
757e1051a39Sopenharmony_ci
758e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
759e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
760e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
761e1051a39Sopenharmony_ci        goto end;
762e1051a39Sopenharmony_ci    SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
763e1051a39Sopenharmony_ci
764e1051a39Sopenharmony_ci    /* The gimpy cipher list we configure can't do TLS 1.3. */
765e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
766e1051a39Sopenharmony_ci
767e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
768e1051a39Sopenharmony_ci                        "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
769e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
770e1051a39Sopenharmony_ci                                             &clientssl, NULL, NULL))
771e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
772e1051a39Sopenharmony_ci                        SSL_ERROR_WANT_CLIENT_HELLO_CB))
773e1051a39Sopenharmony_ci                /*
774e1051a39Sopenharmony_ci                 * Passing a -1 literal is a hack since
775e1051a39Sopenharmony_ci                 * the real value was lost.
776e1051a39Sopenharmony_ci                 * */
777e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(serverssl, -1),
778e1051a39Sopenharmony_ci                            SSL_ERROR_WANT_CLIENT_HELLO_CB)
779e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
780e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
781e1051a39Sopenharmony_ci        goto end;
782e1051a39Sopenharmony_ci
783e1051a39Sopenharmony_ci    testresult = 1;
784e1051a39Sopenharmony_ci
785e1051a39Sopenharmony_ciend:
786e1051a39Sopenharmony_ci    SSL_free(serverssl);
787e1051a39Sopenharmony_ci    SSL_free(clientssl);
788e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
789e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
790e1051a39Sopenharmony_ci
791e1051a39Sopenharmony_ci    return testresult;
792e1051a39Sopenharmony_ci}
793e1051a39Sopenharmony_ci
794e1051a39Sopenharmony_cistatic int test_no_ems(void)
795e1051a39Sopenharmony_ci{
796e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
797e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
798e1051a39Sopenharmony_ci    int testresult = 0;
799e1051a39Sopenharmony_ci
800e1051a39Sopenharmony_ci    if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
801e1051a39Sopenharmony_ci                             TLS1_VERSION, TLS1_2_VERSION,
802e1051a39Sopenharmony_ci                             &sctx, &cctx, cert, privkey)) {
803e1051a39Sopenharmony_ci        printf("Unable to create SSL_CTX pair\n");
804e1051a39Sopenharmony_ci        goto end;
805e1051a39Sopenharmony_ci    }
806e1051a39Sopenharmony_ci
807e1051a39Sopenharmony_ci    SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
808e1051a39Sopenharmony_ci
809e1051a39Sopenharmony_ci    if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
810e1051a39Sopenharmony_ci        printf("Unable to create SSL objects\n");
811e1051a39Sopenharmony_ci        goto end;
812e1051a39Sopenharmony_ci    }
813e1051a39Sopenharmony_ci
814e1051a39Sopenharmony_ci    if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
815e1051a39Sopenharmony_ci        printf("Creating SSL connection failed\n");
816e1051a39Sopenharmony_ci        goto end;
817e1051a39Sopenharmony_ci    }
818e1051a39Sopenharmony_ci
819e1051a39Sopenharmony_ci    if (SSL_get_extms_support(serverssl)) {
820e1051a39Sopenharmony_ci        printf("Server reports Extended Master Secret support\n");
821e1051a39Sopenharmony_ci        goto end;
822e1051a39Sopenharmony_ci    }
823e1051a39Sopenharmony_ci
824e1051a39Sopenharmony_ci    if (SSL_get_extms_support(clientssl)) {
825e1051a39Sopenharmony_ci        printf("Client reports Extended Master Secret support\n");
826e1051a39Sopenharmony_ci        goto end;
827e1051a39Sopenharmony_ci    }
828e1051a39Sopenharmony_ci    testresult = 1;
829e1051a39Sopenharmony_ci
830e1051a39Sopenharmony_ciend:
831e1051a39Sopenharmony_ci    SSL_free(serverssl);
832e1051a39Sopenharmony_ci    SSL_free(clientssl);
833e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
834e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
835e1051a39Sopenharmony_ci
836e1051a39Sopenharmony_ci    return testresult;
837e1051a39Sopenharmony_ci}
838e1051a39Sopenharmony_ci
839e1051a39Sopenharmony_ci/*
840e1051a39Sopenharmony_ci * Very focused test to exercise a single case in the server-side state
841e1051a39Sopenharmony_ci * machine, when the ChangeCipherState message needs to actually change
842e1051a39Sopenharmony_ci * from one cipher to a different cipher (i.e., not changing from null
843e1051a39Sopenharmony_ci * encryption to real encryption).
844e1051a39Sopenharmony_ci */
845e1051a39Sopenharmony_cistatic int test_ccs_change_cipher(void)
846e1051a39Sopenharmony_ci{
847e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
848e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
849e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL, *sesspre, *sesspost;
850e1051a39Sopenharmony_ci    int testresult = 0;
851e1051a39Sopenharmony_ci    int i;
852e1051a39Sopenharmony_ci    unsigned char buf;
853e1051a39Sopenharmony_ci    size_t readbytes;
854e1051a39Sopenharmony_ci
855e1051a39Sopenharmony_ci    /*
856e1051a39Sopenharmony_ci     * Create a conection so we can resume and potentially (but not) use
857e1051a39Sopenharmony_ci     * a different cipher in the second connection.
858e1051a39Sopenharmony_ci     */
859e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
860e1051a39Sopenharmony_ci                                       TLS_client_method(),
861e1051a39Sopenharmony_ci                                       TLS1_VERSION, TLS1_2_VERSION,
862e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
863e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
864e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
865e1051a39Sopenharmony_ci                          NULL, NULL))
866e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
867e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
868e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
869e1051a39Sopenharmony_ci            || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
870e1051a39Sopenharmony_ci            || !TEST_ptr(sess = SSL_get1_session(clientssl)))
871e1051a39Sopenharmony_ci        goto end;
872e1051a39Sopenharmony_ci
873e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
874e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
875e1051a39Sopenharmony_ci
876e1051a39Sopenharmony_ci    /* Resume, preferring a different cipher. Our server will force the
877e1051a39Sopenharmony_ci     * same cipher to be used as the initial handshake. */
878e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
879e1051a39Sopenharmony_ci                          NULL, NULL))
880e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, sess))
881e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
882e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
883e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
884e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl))
885e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(serverssl))
886e1051a39Sopenharmony_ci            || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
887e1051a39Sopenharmony_ci            || !TEST_ptr_eq(sesspre, sesspost)
888e1051a39Sopenharmony_ci            || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
889e1051a39Sopenharmony_ci                            SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
890e1051a39Sopenharmony_ci        goto end;
891e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
892e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
893e1051a39Sopenharmony_ci
894e1051a39Sopenharmony_ci    /*
895e1051a39Sopenharmony_ci     * Now create a fresh connection and try to renegotiate a different
896e1051a39Sopenharmony_ci     * cipher on it.
897e1051a39Sopenharmony_ci     */
898e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
899e1051a39Sopenharmony_ci                                      NULL, NULL))
900e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
901e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
902e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
903e1051a39Sopenharmony_ci            || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
904e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
905e1051a39Sopenharmony_ci            || !TEST_true(SSL_renegotiate(clientssl))
906e1051a39Sopenharmony_ci            || !TEST_true(SSL_renegotiate_pending(clientssl)))
907e1051a39Sopenharmony_ci        goto end;
908e1051a39Sopenharmony_ci    /* Actually drive the renegotiation. */
909e1051a39Sopenharmony_ci    for (i = 0; i < 3; i++) {
910e1051a39Sopenharmony_ci        if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
911e1051a39Sopenharmony_ci            if (!TEST_ulong_eq(readbytes, 0))
912e1051a39Sopenharmony_ci                goto end;
913e1051a39Sopenharmony_ci        } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
914e1051a39Sopenharmony_ci                                SSL_ERROR_WANT_READ)) {
915e1051a39Sopenharmony_ci            goto end;
916e1051a39Sopenharmony_ci        }
917e1051a39Sopenharmony_ci        if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
918e1051a39Sopenharmony_ci            if (!TEST_ulong_eq(readbytes, 0))
919e1051a39Sopenharmony_ci                goto end;
920e1051a39Sopenharmony_ci        } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
921e1051a39Sopenharmony_ci                                SSL_ERROR_WANT_READ)) {
922e1051a39Sopenharmony_ci            goto end;
923e1051a39Sopenharmony_ci        }
924e1051a39Sopenharmony_ci    }
925e1051a39Sopenharmony_ci    /* sesspre and sesspost should be different since the cipher changed. */
926e1051a39Sopenharmony_ci    if (!TEST_false(SSL_renegotiate_pending(clientssl))
927e1051a39Sopenharmony_ci            || !TEST_false(SSL_session_reused(clientssl))
928e1051a39Sopenharmony_ci            || !TEST_false(SSL_session_reused(serverssl))
929e1051a39Sopenharmony_ci            || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
930e1051a39Sopenharmony_ci            || !TEST_ptr_ne(sesspre, sesspost)
931e1051a39Sopenharmony_ci            || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
932e1051a39Sopenharmony_ci                            SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
933e1051a39Sopenharmony_ci        goto end;
934e1051a39Sopenharmony_ci
935e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
936e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
937e1051a39Sopenharmony_ci
938e1051a39Sopenharmony_ci    testresult = 1;
939e1051a39Sopenharmony_ci
940e1051a39Sopenharmony_ciend:
941e1051a39Sopenharmony_ci    SSL_free(serverssl);
942e1051a39Sopenharmony_ci    SSL_free(clientssl);
943e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
944e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
945e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
946e1051a39Sopenharmony_ci
947e1051a39Sopenharmony_ci    return testresult;
948e1051a39Sopenharmony_ci}
949e1051a39Sopenharmony_ci#endif
950e1051a39Sopenharmony_ci
951e1051a39Sopenharmony_cistatic int execute_test_large_message(const SSL_METHOD *smeth,
952e1051a39Sopenharmony_ci                                      const SSL_METHOD *cmeth,
953e1051a39Sopenharmony_ci                                      int min_version, int max_version,
954e1051a39Sopenharmony_ci                                      int read_ahead)
955e1051a39Sopenharmony_ci{
956e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
957e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
958e1051a39Sopenharmony_ci    int testresult = 0;
959e1051a39Sopenharmony_ci    int i;
960e1051a39Sopenharmony_ci    BIO *certbio = NULL;
961e1051a39Sopenharmony_ci    X509 *chaincert = NULL;
962e1051a39Sopenharmony_ci    int certlen;
963e1051a39Sopenharmony_ci
964e1051a39Sopenharmony_ci    if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
965e1051a39Sopenharmony_ci        goto end;
966e1051a39Sopenharmony_ci
967e1051a39Sopenharmony_ci    if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
968e1051a39Sopenharmony_ci        goto end;
969e1051a39Sopenharmony_ci
970e1051a39Sopenharmony_ci    if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
971e1051a39Sopenharmony_ci        goto end;
972e1051a39Sopenharmony_ci    BIO_free(certbio);
973e1051a39Sopenharmony_ci    certbio = NULL;
974e1051a39Sopenharmony_ci
975e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
976e1051a39Sopenharmony_ci                                       max_version, &sctx, &cctx, cert,
977e1051a39Sopenharmony_ci                                       privkey)))
978e1051a39Sopenharmony_ci        goto end;
979e1051a39Sopenharmony_ci
980e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_DTLS1_2
981e1051a39Sopenharmony_ci    if (smeth == DTLS_server_method()) {
982e1051a39Sopenharmony_ci        /*
983e1051a39Sopenharmony_ci         * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
984e1051a39Sopenharmony_ci         * level 0
985e1051a39Sopenharmony_ci         */
986e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
987e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_cipher_list(cctx,
988e1051a39Sopenharmony_ci                                                    "DEFAULT:@SECLEVEL=0")))
989e1051a39Sopenharmony_ci            goto end;
990e1051a39Sopenharmony_ci    }
991e1051a39Sopenharmony_ci#endif
992e1051a39Sopenharmony_ci
993e1051a39Sopenharmony_ci    if (read_ahead) {
994e1051a39Sopenharmony_ci        /*
995e1051a39Sopenharmony_ci         * Test that read_ahead works correctly when dealing with large
996e1051a39Sopenharmony_ci         * records
997e1051a39Sopenharmony_ci         */
998e1051a39Sopenharmony_ci        SSL_CTX_set_read_ahead(cctx, 1);
999e1051a39Sopenharmony_ci    }
1000e1051a39Sopenharmony_ci
1001e1051a39Sopenharmony_ci    /*
1002e1051a39Sopenharmony_ci     * We assume the supplied certificate is big enough so that if we add
1003e1051a39Sopenharmony_ci     * NUM_EXTRA_CERTS it will make the overall message large enough. The
1004e1051a39Sopenharmony_ci     * default buffer size is requested to be 16k, but due to the way BUF_MEM
1005e1051a39Sopenharmony_ci     * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
1006e1051a39Sopenharmony_ci     * test we need to have a message larger than that.
1007e1051a39Sopenharmony_ci     */
1008e1051a39Sopenharmony_ci    certlen = i2d_X509(chaincert, NULL);
1009e1051a39Sopenharmony_ci    OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
1010e1051a39Sopenharmony_ci                   (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
1011e1051a39Sopenharmony_ci    for (i = 0; i < NUM_EXTRA_CERTS; i++) {
1012e1051a39Sopenharmony_ci        if (!X509_up_ref(chaincert))
1013e1051a39Sopenharmony_ci            goto end;
1014e1051a39Sopenharmony_ci        if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
1015e1051a39Sopenharmony_ci            X509_free(chaincert);
1016e1051a39Sopenharmony_ci            goto end;
1017e1051a39Sopenharmony_ci        }
1018e1051a39Sopenharmony_ci    }
1019e1051a39Sopenharmony_ci
1020e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1021e1051a39Sopenharmony_ci                                      NULL, NULL))
1022e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
1023e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
1024e1051a39Sopenharmony_ci        goto end;
1025e1051a39Sopenharmony_ci
1026e1051a39Sopenharmony_ci    /*
1027e1051a39Sopenharmony_ci     * Calling SSL_clear() first is not required but this tests that SSL_clear()
1028e1051a39Sopenharmony_ci     * doesn't leak.
1029e1051a39Sopenharmony_ci     */
1030e1051a39Sopenharmony_ci    if (!TEST_true(SSL_clear(serverssl)))
1031e1051a39Sopenharmony_ci        goto end;
1032e1051a39Sopenharmony_ci
1033e1051a39Sopenharmony_ci    testresult = 1;
1034e1051a39Sopenharmony_ci end:
1035e1051a39Sopenharmony_ci    BIO_free(certbio);
1036e1051a39Sopenharmony_ci    X509_free(chaincert);
1037e1051a39Sopenharmony_ci    SSL_free(serverssl);
1038e1051a39Sopenharmony_ci    SSL_free(clientssl);
1039e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1040e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1041e1051a39Sopenharmony_ci
1042e1051a39Sopenharmony_ci    return testresult;
1043e1051a39Sopenharmony_ci}
1044e1051a39Sopenharmony_ci
1045e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
1046e1051a39Sopenharmony_ci    !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
1047e1051a39Sopenharmony_ci/* sock must be connected */
1048e1051a39Sopenharmony_cistatic int ktls_chk_platform(int sock)
1049e1051a39Sopenharmony_ci{
1050e1051a39Sopenharmony_ci    if (!ktls_enable(sock))
1051e1051a39Sopenharmony_ci        return 0;
1052e1051a39Sopenharmony_ci    return 1;
1053e1051a39Sopenharmony_ci}
1054e1051a39Sopenharmony_ci
1055e1051a39Sopenharmony_cistatic int ping_pong_query(SSL *clientssl, SSL *serverssl)
1056e1051a39Sopenharmony_ci{
1057e1051a39Sopenharmony_ci    static char count = 1;
1058e1051a39Sopenharmony_ci    unsigned char cbuf[16000] = {0};
1059e1051a39Sopenharmony_ci    unsigned char sbuf[16000];
1060e1051a39Sopenharmony_ci    size_t err = 0;
1061e1051a39Sopenharmony_ci    char crec_wseq_before[SEQ_NUM_SIZE];
1062e1051a39Sopenharmony_ci    char crec_wseq_after[SEQ_NUM_SIZE];
1063e1051a39Sopenharmony_ci    char crec_rseq_before[SEQ_NUM_SIZE];
1064e1051a39Sopenharmony_ci    char crec_rseq_after[SEQ_NUM_SIZE];
1065e1051a39Sopenharmony_ci    char srec_wseq_before[SEQ_NUM_SIZE];
1066e1051a39Sopenharmony_ci    char srec_wseq_after[SEQ_NUM_SIZE];
1067e1051a39Sopenharmony_ci    char srec_rseq_before[SEQ_NUM_SIZE];
1068e1051a39Sopenharmony_ci    char srec_rseq_after[SEQ_NUM_SIZE];
1069e1051a39Sopenharmony_ci
1070e1051a39Sopenharmony_ci    cbuf[0] = count++;
1071e1051a39Sopenharmony_ci    memcpy(crec_wseq_before, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1072e1051a39Sopenharmony_ci    memcpy(crec_rseq_before, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1073e1051a39Sopenharmony_ci    memcpy(srec_wseq_before, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1074e1051a39Sopenharmony_ci    memcpy(srec_rseq_before, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1075e1051a39Sopenharmony_ci
1076e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
1077e1051a39Sopenharmony_ci        goto end;
1078e1051a39Sopenharmony_ci
1079e1051a39Sopenharmony_ci    while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
1080e1051a39Sopenharmony_ci        if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
1081e1051a39Sopenharmony_ci            goto end;
1082e1051a39Sopenharmony_ci        }
1083e1051a39Sopenharmony_ci    }
1084e1051a39Sopenharmony_ci
1085e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
1086e1051a39Sopenharmony_ci        goto end;
1087e1051a39Sopenharmony_ci
1088e1051a39Sopenharmony_ci    while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
1089e1051a39Sopenharmony_ci        if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
1090e1051a39Sopenharmony_ci            goto end;
1091e1051a39Sopenharmony_ci        }
1092e1051a39Sopenharmony_ci    }
1093e1051a39Sopenharmony_ci
1094e1051a39Sopenharmony_ci    memcpy(crec_wseq_after, &clientssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1095e1051a39Sopenharmony_ci    memcpy(crec_rseq_after, &clientssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1096e1051a39Sopenharmony_ci    memcpy(srec_wseq_after, &serverssl->rlayer.write_sequence, SEQ_NUM_SIZE);
1097e1051a39Sopenharmony_ci    memcpy(srec_rseq_after, &serverssl->rlayer.read_sequence, SEQ_NUM_SIZE);
1098e1051a39Sopenharmony_ci
1099e1051a39Sopenharmony_ci    /* verify the payload */
1100e1051a39Sopenharmony_ci    if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1101e1051a39Sopenharmony_ci        goto end;
1102e1051a39Sopenharmony_ci
1103e1051a39Sopenharmony_ci    /*
1104e1051a39Sopenharmony_ci     * If ktls is used then kernel sequences are used instead of
1105e1051a39Sopenharmony_ci     * OpenSSL sequences
1106e1051a39Sopenharmony_ci     */
1107e1051a39Sopenharmony_ci    if (!BIO_get_ktls_send(clientssl->wbio)) {
1108e1051a39Sopenharmony_ci        if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
1109e1051a39Sopenharmony_ci                         crec_wseq_after, SEQ_NUM_SIZE))
1110e1051a39Sopenharmony_ci            goto end;
1111e1051a39Sopenharmony_ci    } else {
1112e1051a39Sopenharmony_ci        if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
1113e1051a39Sopenharmony_ci                         crec_wseq_after, SEQ_NUM_SIZE))
1114e1051a39Sopenharmony_ci            goto end;
1115e1051a39Sopenharmony_ci    }
1116e1051a39Sopenharmony_ci
1117e1051a39Sopenharmony_ci    if (!BIO_get_ktls_send(serverssl->wbio)) {
1118e1051a39Sopenharmony_ci        if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
1119e1051a39Sopenharmony_ci                         srec_wseq_after, SEQ_NUM_SIZE))
1120e1051a39Sopenharmony_ci            goto end;
1121e1051a39Sopenharmony_ci    } else {
1122e1051a39Sopenharmony_ci        if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
1123e1051a39Sopenharmony_ci                         srec_wseq_after, SEQ_NUM_SIZE))
1124e1051a39Sopenharmony_ci            goto end;
1125e1051a39Sopenharmony_ci    }
1126e1051a39Sopenharmony_ci
1127e1051a39Sopenharmony_ci    if (!BIO_get_ktls_recv(clientssl->wbio)) {
1128e1051a39Sopenharmony_ci        if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
1129e1051a39Sopenharmony_ci                         crec_rseq_after, SEQ_NUM_SIZE))
1130e1051a39Sopenharmony_ci            goto end;
1131e1051a39Sopenharmony_ci    } else {
1132e1051a39Sopenharmony_ci        if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
1133e1051a39Sopenharmony_ci                         crec_rseq_after, SEQ_NUM_SIZE))
1134e1051a39Sopenharmony_ci            goto end;
1135e1051a39Sopenharmony_ci    }
1136e1051a39Sopenharmony_ci
1137e1051a39Sopenharmony_ci    if (!BIO_get_ktls_recv(serverssl->wbio)) {
1138e1051a39Sopenharmony_ci        if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
1139e1051a39Sopenharmony_ci                         srec_rseq_after, SEQ_NUM_SIZE))
1140e1051a39Sopenharmony_ci            goto end;
1141e1051a39Sopenharmony_ci    } else {
1142e1051a39Sopenharmony_ci        if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
1143e1051a39Sopenharmony_ci                         srec_rseq_after, SEQ_NUM_SIZE))
1144e1051a39Sopenharmony_ci            goto end;
1145e1051a39Sopenharmony_ci    }
1146e1051a39Sopenharmony_ci
1147e1051a39Sopenharmony_ci    return 1;
1148e1051a39Sopenharmony_ciend:
1149e1051a39Sopenharmony_ci    return 0;
1150e1051a39Sopenharmony_ci}
1151e1051a39Sopenharmony_ci
1152e1051a39Sopenharmony_cistatic int execute_test_ktls(int cis_ktls, int sis_ktls,
1153e1051a39Sopenharmony_ci                             int tls_version, const char *cipher)
1154e1051a39Sopenharmony_ci{
1155e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
1156e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
1157e1051a39Sopenharmony_ci    int ktls_used = 0, testresult = 0;
1158e1051a39Sopenharmony_ci    int cfd = -1, sfd = -1;
1159e1051a39Sopenharmony_ci    int rx_supported;
1160e1051a39Sopenharmony_ci
1161e1051a39Sopenharmony_ci    if (!TEST_true(create_test_sockets(&cfd, &sfd)))
1162e1051a39Sopenharmony_ci        goto end;
1163e1051a39Sopenharmony_ci
1164e1051a39Sopenharmony_ci    /* Skip this test if the platform does not support ktls */
1165e1051a39Sopenharmony_ci    if (!ktls_chk_platform(cfd)) {
1166e1051a39Sopenharmony_ci        testresult = TEST_skip("Kernel does not support KTLS");
1167e1051a39Sopenharmony_ci        goto end;
1168e1051a39Sopenharmony_ci    }
1169e1051a39Sopenharmony_ci
1170e1051a39Sopenharmony_ci    if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1171e1051a39Sopenharmony_ci        testresult = TEST_skip("CHACHA is not supported in FIPS");
1172e1051a39Sopenharmony_ci        goto end;
1173e1051a39Sopenharmony_ci    }
1174e1051a39Sopenharmony_ci
1175e1051a39Sopenharmony_ci    /* Create a session based on SHA-256 */
1176e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1177e1051a39Sopenharmony_ci                                       TLS_client_method(),
1178e1051a39Sopenharmony_ci                                       tls_version, tls_version,
1179e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
1180e1051a39Sopenharmony_ci        goto end;
1181e1051a39Sopenharmony_ci
1182e1051a39Sopenharmony_ci    if (tls_version == TLS1_3_VERSION) {
1183e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1184e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1185e1051a39Sopenharmony_ci            goto end;
1186e1051a39Sopenharmony_ci    } else {
1187e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1188e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1189e1051a39Sopenharmony_ci            goto end;
1190e1051a39Sopenharmony_ci    }
1191e1051a39Sopenharmony_ci
1192e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1193e1051a39Sopenharmony_ci                                       &clientssl, sfd, cfd)))
1194e1051a39Sopenharmony_ci        goto end;
1195e1051a39Sopenharmony_ci
1196e1051a39Sopenharmony_ci    if (cis_ktls) {
1197e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
1198e1051a39Sopenharmony_ci            goto end;
1199e1051a39Sopenharmony_ci    }
1200e1051a39Sopenharmony_ci
1201e1051a39Sopenharmony_ci    if (sis_ktls) {
1202e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1203e1051a39Sopenharmony_ci            goto end;
1204e1051a39Sopenharmony_ci    }
1205e1051a39Sopenharmony_ci
1206e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1207e1051a39Sopenharmony_ci        goto end;
1208e1051a39Sopenharmony_ci
1209e1051a39Sopenharmony_ci    /*
1210e1051a39Sopenharmony_ci     * The running kernel may not support a given cipher suite
1211e1051a39Sopenharmony_ci     * or direction, so just check that KTLS isn't used when it
1212e1051a39Sopenharmony_ci     * isn't enabled.
1213e1051a39Sopenharmony_ci     */
1214e1051a39Sopenharmony_ci    if (!cis_ktls) {
1215e1051a39Sopenharmony_ci        if (!TEST_false(BIO_get_ktls_send(clientssl->wbio)))
1216e1051a39Sopenharmony_ci            goto end;
1217e1051a39Sopenharmony_ci    } else {
1218e1051a39Sopenharmony_ci        if (BIO_get_ktls_send(clientssl->wbio))
1219e1051a39Sopenharmony_ci            ktls_used = 1;
1220e1051a39Sopenharmony_ci    }
1221e1051a39Sopenharmony_ci
1222e1051a39Sopenharmony_ci    if (!sis_ktls) {
1223e1051a39Sopenharmony_ci        if (!TEST_false(BIO_get_ktls_send(serverssl->wbio)))
1224e1051a39Sopenharmony_ci            goto end;
1225e1051a39Sopenharmony_ci    } else {
1226e1051a39Sopenharmony_ci        if (BIO_get_ktls_send(serverssl->wbio))
1227e1051a39Sopenharmony_ci            ktls_used = 1;
1228e1051a39Sopenharmony_ci    }
1229e1051a39Sopenharmony_ci
1230e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_KTLS_RX)
1231e1051a39Sopenharmony_ci    rx_supported = 0;
1232e1051a39Sopenharmony_ci#else
1233e1051a39Sopenharmony_ci    rx_supported = (tls_version != TLS1_3_VERSION);
1234e1051a39Sopenharmony_ci#endif
1235e1051a39Sopenharmony_ci    if (!cis_ktls || !rx_supported) {
1236e1051a39Sopenharmony_ci        if (!TEST_false(BIO_get_ktls_recv(clientssl->rbio)))
1237e1051a39Sopenharmony_ci            goto end;
1238e1051a39Sopenharmony_ci    } else {
1239e1051a39Sopenharmony_ci        if (BIO_get_ktls_send(clientssl->rbio))
1240e1051a39Sopenharmony_ci            ktls_used = 1;
1241e1051a39Sopenharmony_ci    }
1242e1051a39Sopenharmony_ci
1243e1051a39Sopenharmony_ci    if (!sis_ktls || !rx_supported) {
1244e1051a39Sopenharmony_ci        if (!TEST_false(BIO_get_ktls_recv(serverssl->rbio)))
1245e1051a39Sopenharmony_ci            goto end;
1246e1051a39Sopenharmony_ci    } else {
1247e1051a39Sopenharmony_ci        if (BIO_get_ktls_send(serverssl->rbio))
1248e1051a39Sopenharmony_ci            ktls_used = 1;
1249e1051a39Sopenharmony_ci    }
1250e1051a39Sopenharmony_ci
1251e1051a39Sopenharmony_ci    if ((cis_ktls || sis_ktls) && !ktls_used) {
1252e1051a39Sopenharmony_ci        testresult = TEST_skip("KTLS not supported for %s cipher %s",
1253e1051a39Sopenharmony_ci                               tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1254e1051a39Sopenharmony_ci                               "TLS 1.2", cipher);
1255e1051a39Sopenharmony_ci        goto end;
1256e1051a39Sopenharmony_ci    }
1257e1051a39Sopenharmony_ci
1258e1051a39Sopenharmony_ci    if (!TEST_true(ping_pong_query(clientssl, serverssl)))
1259e1051a39Sopenharmony_ci        goto end;
1260e1051a39Sopenharmony_ci
1261e1051a39Sopenharmony_ci    testresult = 1;
1262e1051a39Sopenharmony_ciend:
1263e1051a39Sopenharmony_ci    if (clientssl) {
1264e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
1265e1051a39Sopenharmony_ci        SSL_free(clientssl);
1266e1051a39Sopenharmony_ci    }
1267e1051a39Sopenharmony_ci    if (serverssl) {
1268e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
1269e1051a39Sopenharmony_ci        SSL_free(serverssl);
1270e1051a39Sopenharmony_ci    }
1271e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1272e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1273e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
1274e1051a39Sopenharmony_ci    if (cfd != -1)
1275e1051a39Sopenharmony_ci        close(cfd);
1276e1051a39Sopenharmony_ci    if (sfd != -1)
1277e1051a39Sopenharmony_ci        close(sfd);
1278e1051a39Sopenharmony_ci    return testresult;
1279e1051a39Sopenharmony_ci}
1280e1051a39Sopenharmony_ci
1281e1051a39Sopenharmony_ci#define SENDFILE_SZ                     (16 * 4096)
1282e1051a39Sopenharmony_ci#define SENDFILE_CHUNK                  (4 * 4096)
1283e1051a39Sopenharmony_ci#define min(a,b)                        ((a) > (b) ? (b) : (a))
1284e1051a39Sopenharmony_ci
1285e1051a39Sopenharmony_cistatic int execute_test_ktls_sendfile(int tls_version, const char *cipher)
1286e1051a39Sopenharmony_ci{
1287e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
1288e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
1289e1051a39Sopenharmony_ci    unsigned char *buf, *buf_dst;
1290e1051a39Sopenharmony_ci    BIO *out = NULL, *in = NULL;
1291e1051a39Sopenharmony_ci    int cfd = -1, sfd = -1, ffd, err;
1292e1051a39Sopenharmony_ci    ssize_t chunk_size = 0;
1293e1051a39Sopenharmony_ci    off_t chunk_off = 0;
1294e1051a39Sopenharmony_ci    int testresult = 0;
1295e1051a39Sopenharmony_ci    FILE *ffdp;
1296e1051a39Sopenharmony_ci
1297e1051a39Sopenharmony_ci    buf = OPENSSL_zalloc(SENDFILE_SZ);
1298e1051a39Sopenharmony_ci    buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
1299e1051a39Sopenharmony_ci    if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
1300e1051a39Sopenharmony_ci        || !TEST_true(create_test_sockets(&cfd, &sfd)))
1301e1051a39Sopenharmony_ci        goto end;
1302e1051a39Sopenharmony_ci
1303e1051a39Sopenharmony_ci    /* Skip this test if the platform does not support ktls */
1304e1051a39Sopenharmony_ci    if (!ktls_chk_platform(sfd)) {
1305e1051a39Sopenharmony_ci        testresult = TEST_skip("Kernel does not support KTLS");
1306e1051a39Sopenharmony_ci        goto end;
1307e1051a39Sopenharmony_ci    }
1308e1051a39Sopenharmony_ci
1309e1051a39Sopenharmony_ci    if (is_fips && strstr(cipher, "CHACHA") != NULL) {
1310e1051a39Sopenharmony_ci        testresult = TEST_skip("CHACHA is not supported in FIPS");
1311e1051a39Sopenharmony_ci        goto end;
1312e1051a39Sopenharmony_ci    }
1313e1051a39Sopenharmony_ci
1314e1051a39Sopenharmony_ci    /* Create a session based on SHA-256 */
1315e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
1316e1051a39Sopenharmony_ci                                       TLS_client_method(),
1317e1051a39Sopenharmony_ci                                       tls_version, tls_version,
1318e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
1319e1051a39Sopenharmony_ci        goto end;
1320e1051a39Sopenharmony_ci
1321e1051a39Sopenharmony_ci    if (tls_version == TLS1_3_VERSION) {
1322e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
1323e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
1324e1051a39Sopenharmony_ci            goto end;
1325e1051a39Sopenharmony_ci    } else {
1326e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
1327e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
1328e1051a39Sopenharmony_ci            goto end;
1329e1051a39Sopenharmony_ci    }
1330e1051a39Sopenharmony_ci
1331e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
1332e1051a39Sopenharmony_ci                                       &clientssl, sfd, cfd)))
1333e1051a39Sopenharmony_ci        goto end;
1334e1051a39Sopenharmony_ci
1335e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
1336e1051a39Sopenharmony_ci        goto end;
1337e1051a39Sopenharmony_ci
1338e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1339e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
1340e1051a39Sopenharmony_ci        goto end;
1341e1051a39Sopenharmony_ci
1342e1051a39Sopenharmony_ci    if (!BIO_get_ktls_send(serverssl->wbio)) {
1343e1051a39Sopenharmony_ci        testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
1344e1051a39Sopenharmony_ci                               tls_version == TLS1_3_VERSION ? "TLS 1.3" :
1345e1051a39Sopenharmony_ci                               "TLS 1.2", cipher);
1346e1051a39Sopenharmony_ci        goto end;
1347e1051a39Sopenharmony_ci    }
1348e1051a39Sopenharmony_ci
1349e1051a39Sopenharmony_ci    if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
1350e1051a39Sopenharmony_ci        goto end;
1351e1051a39Sopenharmony_ci
1352e1051a39Sopenharmony_ci    out = BIO_new_file(tmpfilename, "wb");
1353e1051a39Sopenharmony_ci    if (!TEST_ptr(out))
1354e1051a39Sopenharmony_ci        goto end;
1355e1051a39Sopenharmony_ci
1356e1051a39Sopenharmony_ci    if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
1357e1051a39Sopenharmony_ci        goto end;
1358e1051a39Sopenharmony_ci
1359e1051a39Sopenharmony_ci    BIO_free(out);
1360e1051a39Sopenharmony_ci    out = NULL;
1361e1051a39Sopenharmony_ci    in = BIO_new_file(tmpfilename, "rb");
1362e1051a39Sopenharmony_ci    BIO_get_fp(in, &ffdp);
1363e1051a39Sopenharmony_ci    ffd = fileno(ffdp);
1364e1051a39Sopenharmony_ci
1365e1051a39Sopenharmony_ci    while (chunk_off < SENDFILE_SZ) {
1366e1051a39Sopenharmony_ci        chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
1367e1051a39Sopenharmony_ci        while ((err = SSL_sendfile(serverssl,
1368e1051a39Sopenharmony_ci                                   ffd,
1369e1051a39Sopenharmony_ci                                   chunk_off,
1370e1051a39Sopenharmony_ci                                   chunk_size,
1371e1051a39Sopenharmony_ci                                   0)) != chunk_size) {
1372e1051a39Sopenharmony_ci            if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
1373e1051a39Sopenharmony_ci                goto end;
1374e1051a39Sopenharmony_ci        }
1375e1051a39Sopenharmony_ci        while ((err = SSL_read(clientssl,
1376e1051a39Sopenharmony_ci                               buf_dst + chunk_off,
1377e1051a39Sopenharmony_ci                               chunk_size)) != chunk_size) {
1378e1051a39Sopenharmony_ci            if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
1379e1051a39Sopenharmony_ci                goto end;
1380e1051a39Sopenharmony_ci        }
1381e1051a39Sopenharmony_ci
1382e1051a39Sopenharmony_ci        /* verify the payload */
1383e1051a39Sopenharmony_ci        if (!TEST_mem_eq(buf_dst + chunk_off,
1384e1051a39Sopenharmony_ci                         chunk_size,
1385e1051a39Sopenharmony_ci                         buf + chunk_off,
1386e1051a39Sopenharmony_ci                         chunk_size))
1387e1051a39Sopenharmony_ci            goto end;
1388e1051a39Sopenharmony_ci
1389e1051a39Sopenharmony_ci        chunk_off += chunk_size;
1390e1051a39Sopenharmony_ci    }
1391e1051a39Sopenharmony_ci
1392e1051a39Sopenharmony_ci    testresult = 1;
1393e1051a39Sopenharmony_ciend:
1394e1051a39Sopenharmony_ci    if (clientssl) {
1395e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
1396e1051a39Sopenharmony_ci        SSL_free(clientssl);
1397e1051a39Sopenharmony_ci    }
1398e1051a39Sopenharmony_ci    if (serverssl) {
1399e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
1400e1051a39Sopenharmony_ci        SSL_free(serverssl);
1401e1051a39Sopenharmony_ci    }
1402e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1403e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1404e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
1405e1051a39Sopenharmony_ci    BIO_free(out);
1406e1051a39Sopenharmony_ci    BIO_free(in);
1407e1051a39Sopenharmony_ci    if (cfd != -1)
1408e1051a39Sopenharmony_ci        close(cfd);
1409e1051a39Sopenharmony_ci    if (sfd != -1)
1410e1051a39Sopenharmony_ci        close(sfd);
1411e1051a39Sopenharmony_ci    OPENSSL_free(buf);
1412e1051a39Sopenharmony_ci    OPENSSL_free(buf_dst);
1413e1051a39Sopenharmony_ci    return testresult;
1414e1051a39Sopenharmony_ci}
1415e1051a39Sopenharmony_ci
1416e1051a39Sopenharmony_cistatic struct ktls_test_cipher {
1417e1051a39Sopenharmony_ci    int tls_version;
1418e1051a39Sopenharmony_ci    const char *cipher;
1419e1051a39Sopenharmony_ci} ktls_test_ciphers[] = {
1420e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_TLS1_2)
1421e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_GCM_128
1422e1051a39Sopenharmony_ci    { TLS1_2_VERSION, "AES128-GCM-SHA256" },
1423e1051a39Sopenharmony_ci#  endif
1424e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_CCM_128
1425e1051a39Sopenharmony_ci    { TLS1_2_VERSION, "AES128-CCM"},
1426e1051a39Sopenharmony_ci#  endif
1427e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_GCM_256
1428e1051a39Sopenharmony_ci    { TLS1_2_VERSION, "AES256-GCM-SHA384"},
1429e1051a39Sopenharmony_ci#  endif
1430e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1431e1051a39Sopenharmony_ci#    ifndef OPENSSL_NO_EC
1432e1051a39Sopenharmony_ci    { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
1433e1051a39Sopenharmony_ci#    endif
1434e1051a39Sopenharmony_ci#  endif
1435e1051a39Sopenharmony_ci# endif
1436e1051a39Sopenharmony_ci# if !defined(OSSL_NO_USABLE_TLS1_3)
1437e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_GCM_128
1438e1051a39Sopenharmony_ci    { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
1439e1051a39Sopenharmony_ci#  endif
1440e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_CCM_128
1441e1051a39Sopenharmony_ci    { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
1442e1051a39Sopenharmony_ci#  endif
1443e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_AES_GCM_256
1444e1051a39Sopenharmony_ci    { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
1445e1051a39Sopenharmony_ci#  endif
1446e1051a39Sopenharmony_ci#  ifdef OPENSSL_KTLS_CHACHA20_POLY1305
1447e1051a39Sopenharmony_ci    { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
1448e1051a39Sopenharmony_ci#  endif
1449e1051a39Sopenharmony_ci# endif
1450e1051a39Sopenharmony_ci};
1451e1051a39Sopenharmony_ci
1452e1051a39Sopenharmony_ci#define NUM_KTLS_TEST_CIPHERS \
1453e1051a39Sopenharmony_ci    (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
1454e1051a39Sopenharmony_ci
1455e1051a39Sopenharmony_cistatic int test_ktls(int test)
1456e1051a39Sopenharmony_ci{
1457e1051a39Sopenharmony_ci    struct ktls_test_cipher *cipher;
1458e1051a39Sopenharmony_ci    int cis_ktls, sis_ktls;
1459e1051a39Sopenharmony_ci
1460e1051a39Sopenharmony_ci    OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
1461e1051a39Sopenharmony_ci    cipher = &ktls_test_ciphers[test / 4];
1462e1051a39Sopenharmony_ci
1463e1051a39Sopenharmony_ci    cis_ktls = (test & 1) != 0;
1464e1051a39Sopenharmony_ci    sis_ktls = (test & 2) != 0;
1465e1051a39Sopenharmony_ci
1466e1051a39Sopenharmony_ci    return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
1467e1051a39Sopenharmony_ci                             cipher->cipher);
1468e1051a39Sopenharmony_ci}
1469e1051a39Sopenharmony_ci
1470e1051a39Sopenharmony_cistatic int test_ktls_sendfile(int tst)
1471e1051a39Sopenharmony_ci{
1472e1051a39Sopenharmony_ci    struct ktls_test_cipher *cipher;
1473e1051a39Sopenharmony_ci
1474e1051a39Sopenharmony_ci    OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
1475e1051a39Sopenharmony_ci    cipher = &ktls_test_ciphers[tst];
1476e1051a39Sopenharmony_ci
1477e1051a39Sopenharmony_ci    return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher);
1478e1051a39Sopenharmony_ci}
1479e1051a39Sopenharmony_ci#endif
1480e1051a39Sopenharmony_ci
1481e1051a39Sopenharmony_cistatic int test_large_message_tls(void)
1482e1051a39Sopenharmony_ci{
1483e1051a39Sopenharmony_ci    return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1484e1051a39Sopenharmony_ci                                      TLS1_VERSION, 0, 0);
1485e1051a39Sopenharmony_ci}
1486e1051a39Sopenharmony_ci
1487e1051a39Sopenharmony_cistatic int test_large_message_tls_read_ahead(void)
1488e1051a39Sopenharmony_ci{
1489e1051a39Sopenharmony_ci    return execute_test_large_message(TLS_server_method(), TLS_client_method(),
1490e1051a39Sopenharmony_ci                                      TLS1_VERSION, 0, 1);
1491e1051a39Sopenharmony_ci}
1492e1051a39Sopenharmony_ci
1493e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS
1494e1051a39Sopenharmony_cistatic int test_large_message_dtls(void)
1495e1051a39Sopenharmony_ci{
1496e1051a39Sopenharmony_ci# ifdef OPENSSL_NO_DTLS1_2
1497e1051a39Sopenharmony_ci    /* Not supported in the FIPS provider */
1498e1051a39Sopenharmony_ci    if (is_fips)
1499e1051a39Sopenharmony_ci        return 1;
1500e1051a39Sopenharmony_ci# endif
1501e1051a39Sopenharmony_ci    /*
1502e1051a39Sopenharmony_ci     * read_ahead is not relevant to DTLS because DTLS always acts as if
1503e1051a39Sopenharmony_ci     * read_ahead is set.
1504e1051a39Sopenharmony_ci     */
1505e1051a39Sopenharmony_ci    return execute_test_large_message(DTLS_server_method(),
1506e1051a39Sopenharmony_ci                                      DTLS_client_method(),
1507e1051a39Sopenharmony_ci                                      DTLS1_VERSION, 0, 0);
1508e1051a39Sopenharmony_ci}
1509e1051a39Sopenharmony_ci#endif
1510e1051a39Sopenharmony_ci
1511e1051a39Sopenharmony_ci/*
1512e1051a39Sopenharmony_ci * Test we can successfully send the maximum amount of application data. We
1513e1051a39Sopenharmony_ci * test each protocol version individually, each with and without EtM enabled.
1514e1051a39Sopenharmony_ci * TLSv1.3 doesn't use EtM so technically it is redundant to test both but it is
1515e1051a39Sopenharmony_ci * simpler this way. We also test all combinations with and without the
1516e1051a39Sopenharmony_ci * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS option which affects the size of the
1517e1051a39Sopenharmony_ci * underlying buffer.
1518e1051a39Sopenharmony_ci */
1519e1051a39Sopenharmony_cistatic int test_large_app_data(int tst)
1520e1051a39Sopenharmony_ci{
1521e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
1522e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
1523e1051a39Sopenharmony_ci    int testresult = 0, prot;
1524e1051a39Sopenharmony_ci    unsigned char *msg, *buf = NULL;
1525e1051a39Sopenharmony_ci    size_t written, readbytes;
1526e1051a39Sopenharmony_ci    const SSL_METHOD *smeth = TLS_server_method();
1527e1051a39Sopenharmony_ci    const SSL_METHOD *cmeth = TLS_client_method();
1528e1051a39Sopenharmony_ci
1529e1051a39Sopenharmony_ci    switch (tst >> 2) {
1530e1051a39Sopenharmony_ci    case 0:
1531e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
1532e1051a39Sopenharmony_ci        prot = TLS1_3_VERSION;
1533e1051a39Sopenharmony_ci        break;
1534e1051a39Sopenharmony_ci#else
1535e1051a39Sopenharmony_ci        return 1;
1536e1051a39Sopenharmony_ci#endif
1537e1051a39Sopenharmony_ci
1538e1051a39Sopenharmony_ci    case 1:
1539e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
1540e1051a39Sopenharmony_ci        prot = TLS1_2_VERSION;
1541e1051a39Sopenharmony_ci        break;
1542e1051a39Sopenharmony_ci#else
1543e1051a39Sopenharmony_ci        return 1;
1544e1051a39Sopenharmony_ci#endif
1545e1051a39Sopenharmony_ci
1546e1051a39Sopenharmony_ci    case 2:
1547e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_1
1548e1051a39Sopenharmony_ci        prot = TLS1_1_VERSION;
1549e1051a39Sopenharmony_ci        break;
1550e1051a39Sopenharmony_ci#else
1551e1051a39Sopenharmony_ci        return 1;
1552e1051a39Sopenharmony_ci#endif
1553e1051a39Sopenharmony_ci
1554e1051a39Sopenharmony_ci    case 3:
1555e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1
1556e1051a39Sopenharmony_ci        prot = TLS1_VERSION;
1557e1051a39Sopenharmony_ci        break;
1558e1051a39Sopenharmony_ci#else
1559e1051a39Sopenharmony_ci        return 1;
1560e1051a39Sopenharmony_ci#endif
1561e1051a39Sopenharmony_ci
1562e1051a39Sopenharmony_ci    case 4:
1563e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SSL3
1564e1051a39Sopenharmony_ci        prot = SSL3_VERSION;
1565e1051a39Sopenharmony_ci        break;
1566e1051a39Sopenharmony_ci#else
1567e1051a39Sopenharmony_ci        return 1;
1568e1051a39Sopenharmony_ci#endif
1569e1051a39Sopenharmony_ci
1570e1051a39Sopenharmony_ci    case 5:
1571e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS1_2
1572e1051a39Sopenharmony_ci        prot = DTLS1_2_VERSION;
1573e1051a39Sopenharmony_ci        smeth = DTLS_server_method();
1574e1051a39Sopenharmony_ci        cmeth = DTLS_client_method();
1575e1051a39Sopenharmony_ci        break;
1576e1051a39Sopenharmony_ci#else
1577e1051a39Sopenharmony_ci        return 1;
1578e1051a39Sopenharmony_ci#endif
1579e1051a39Sopenharmony_ci
1580e1051a39Sopenharmony_ci    case 6:
1581e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS1
1582e1051a39Sopenharmony_ci        prot = DTLS1_VERSION;
1583e1051a39Sopenharmony_ci        smeth = DTLS_server_method();
1584e1051a39Sopenharmony_ci        cmeth = DTLS_client_method();
1585e1051a39Sopenharmony_ci        break;
1586e1051a39Sopenharmony_ci#else
1587e1051a39Sopenharmony_ci        return 1;
1588e1051a39Sopenharmony_ci#endif
1589e1051a39Sopenharmony_ci
1590e1051a39Sopenharmony_ci    default:
1591e1051a39Sopenharmony_ci        /* Shouldn't happen */
1592e1051a39Sopenharmony_ci        return 0;
1593e1051a39Sopenharmony_ci    }
1594e1051a39Sopenharmony_ci
1595e1051a39Sopenharmony_ci    if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips)
1596e1051a39Sopenharmony_ci        return 1;
1597e1051a39Sopenharmony_ci
1598e1051a39Sopenharmony_ci    /* Maximal sized message of zeros */
1599e1051a39Sopenharmony_ci    msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH);
1600e1051a39Sopenharmony_ci    if (!TEST_ptr(msg))
1601e1051a39Sopenharmony_ci        goto end;
1602e1051a39Sopenharmony_ci
1603e1051a39Sopenharmony_ci    buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1);
1604e1051a39Sopenharmony_ci    if (!TEST_ptr(buf))
1605e1051a39Sopenharmony_ci        goto end;
1606e1051a39Sopenharmony_ci    /* Set whole buffer to all bits set */
1607e1051a39Sopenharmony_ci    memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1);
1608e1051a39Sopenharmony_ci
1609e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot,
1610e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
1611e1051a39Sopenharmony_ci        goto end;
1612e1051a39Sopenharmony_ci
1613e1051a39Sopenharmony_ci    if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) {
1614e1051a39Sopenharmony_ci        /* Older protocol versions need SECLEVEL=0 due to SHA1 usage */
1615e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))
1616e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_cipher_list(sctx,
1617e1051a39Sopenharmony_ci                                                      "DEFAULT:@SECLEVEL=0")))
1618e1051a39Sopenharmony_ci        goto end;
1619e1051a39Sopenharmony_ci    }
1620e1051a39Sopenharmony_ci
1621e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1622e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
1623e1051a39Sopenharmony_ci        goto end;
1624e1051a39Sopenharmony_ci
1625e1051a39Sopenharmony_ci    if ((tst & 1) != 0) {
1626e1051a39Sopenharmony_ci        /* Setting this option gives us a minimally sized underlying buffer */
1627e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_options(serverssl,
1628e1051a39Sopenharmony_ci                                       SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
1629e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_options(clientssl,
1630e1051a39Sopenharmony_ci                                              SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)))
1631e1051a39Sopenharmony_ci            goto end;
1632e1051a39Sopenharmony_ci    }
1633e1051a39Sopenharmony_ci
1634e1051a39Sopenharmony_ci    if ((tst & 2) != 0) {
1635e1051a39Sopenharmony_ci        /*
1636e1051a39Sopenharmony_ci         * Setting this option means the MAC is added before encryption
1637e1051a39Sopenharmony_ci         * giving us a larger record for the encryption process
1638e1051a39Sopenharmony_ci         */
1639e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC))
1640e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_options(clientssl,
1641e1051a39Sopenharmony_ci                                              SSL_OP_NO_ENCRYPT_THEN_MAC)))
1642e1051a39Sopenharmony_ci            goto end;
1643e1051a39Sopenharmony_ci    }
1644e1051a39Sopenharmony_ci
1645e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
1646e1051a39Sopenharmony_ci        goto end;
1647e1051a39Sopenharmony_ci
1648e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH,
1649e1051a39Sopenharmony_ci                                &written))
1650e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH))
1651e1051a39Sopenharmony_ci        goto end;
1652e1051a39Sopenharmony_ci
1653e1051a39Sopenharmony_ci    /* We provide a buffer slightly larger than what we are actually expecting */
1654e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1,
1655e1051a39Sopenharmony_ci                               &readbytes)))
1656e1051a39Sopenharmony_ci        goto end;
1657e1051a39Sopenharmony_ci
1658e1051a39Sopenharmony_ci    if (!TEST_mem_eq(msg, written, buf, readbytes))
1659e1051a39Sopenharmony_ci        goto end;
1660e1051a39Sopenharmony_ci
1661e1051a39Sopenharmony_ci    testresult = 1;
1662e1051a39Sopenharmony_ciend:
1663e1051a39Sopenharmony_ci    OPENSSL_free(msg);
1664e1051a39Sopenharmony_ci    OPENSSL_free(buf);
1665e1051a39Sopenharmony_ci    SSL_free(serverssl);
1666e1051a39Sopenharmony_ci    SSL_free(clientssl);
1667e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1668e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1669e1051a39Sopenharmony_ci    return testresult;
1670e1051a39Sopenharmony_ci}
1671e1051a39Sopenharmony_ci
1672e1051a39Sopenharmony_cistatic int execute_cleanse_plaintext(const SSL_METHOD *smeth,
1673e1051a39Sopenharmony_ci                                     const SSL_METHOD *cmeth,
1674e1051a39Sopenharmony_ci                                     int min_version, int max_version)
1675e1051a39Sopenharmony_ci{
1676e1051a39Sopenharmony_ci    size_t i;
1677e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
1678e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
1679e1051a39Sopenharmony_ci    int testresult = 0;
1680e1051a39Sopenharmony_ci    SSL3_RECORD *rr;
1681e1051a39Sopenharmony_ci    void *zbuf;
1682e1051a39Sopenharmony_ci
1683e1051a39Sopenharmony_ci    static unsigned char cbuf[16000];
1684e1051a39Sopenharmony_ci    static unsigned char sbuf[16000];
1685e1051a39Sopenharmony_ci
1686e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx,
1687e1051a39Sopenharmony_ci                                       smeth, cmeth,
1688e1051a39Sopenharmony_ci                                       min_version, max_version,
1689e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert,
1690e1051a39Sopenharmony_ci                                       privkey)))
1691e1051a39Sopenharmony_ci        goto end;
1692e1051a39Sopenharmony_ci
1693e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_DTLS1_2
1694e1051a39Sopenharmony_ci    if (smeth == DTLS_server_method()) {
1695e1051a39Sopenharmony_ci# ifdef OPENSSL_NO_DTLS1_2
1696e1051a39Sopenharmony_ci        /* Not supported in the FIPS provider */
1697e1051a39Sopenharmony_ci        if (is_fips) {
1698e1051a39Sopenharmony_ci            testresult = 1;
1699e1051a39Sopenharmony_ci            goto end;
1700e1051a39Sopenharmony_ci        };
1701e1051a39Sopenharmony_ci# endif
1702e1051a39Sopenharmony_ci        /*
1703e1051a39Sopenharmony_ci         * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
1704e1051a39Sopenharmony_ci         * level 0
1705e1051a39Sopenharmony_ci         */
1706e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
1707e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_cipher_list(cctx,
1708e1051a39Sopenharmony_ci                                                    "DEFAULT:@SECLEVEL=0")))
1709e1051a39Sopenharmony_ci            goto end;
1710e1051a39Sopenharmony_ci    }
1711e1051a39Sopenharmony_ci#endif
1712e1051a39Sopenharmony_ci
1713e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
1714e1051a39Sopenharmony_ci                                      NULL, NULL)))
1715e1051a39Sopenharmony_ci        goto end;
1716e1051a39Sopenharmony_ci
1717e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
1718e1051a39Sopenharmony_ci        goto end;
1719e1051a39Sopenharmony_ci
1720e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1721e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
1722e1051a39Sopenharmony_ci        goto end;
1723e1051a39Sopenharmony_ci
1724e1051a39Sopenharmony_ci    for (i = 0; i < sizeof(cbuf); i++) {
1725e1051a39Sopenharmony_ci        cbuf[i] = i & 0xff;
1726e1051a39Sopenharmony_ci    }
1727e1051a39Sopenharmony_ci
1728e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
1729e1051a39Sopenharmony_ci        goto end;
1730e1051a39Sopenharmony_ci
1731e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1732e1051a39Sopenharmony_ci        goto end;
1733e1051a39Sopenharmony_ci
1734e1051a39Sopenharmony_ci    if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
1735e1051a39Sopenharmony_ci        goto end;
1736e1051a39Sopenharmony_ci
1737e1051a39Sopenharmony_ci    /*
1738e1051a39Sopenharmony_ci     * Since we called SSL_peek(), we know the data in the record
1739e1051a39Sopenharmony_ci     * layer is a plaintext record. We can gather the pointer to check
1740e1051a39Sopenharmony_ci     * for zeroization after SSL_read().
1741e1051a39Sopenharmony_ci     */
1742e1051a39Sopenharmony_ci    rr = serverssl->rlayer.rrec;
1743e1051a39Sopenharmony_ci    zbuf = &rr->data[rr->off];
1744e1051a39Sopenharmony_ci    if (!TEST_int_eq(rr->length, sizeof(cbuf)))
1745e1051a39Sopenharmony_ci        goto end;
1746e1051a39Sopenharmony_ci
1747e1051a39Sopenharmony_ci    /*
1748e1051a39Sopenharmony_ci     * After SSL_peek() the plaintext must still be stored in the
1749e1051a39Sopenharmony_ci     * record.
1750e1051a39Sopenharmony_ci     */
1751e1051a39Sopenharmony_ci    if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1752e1051a39Sopenharmony_ci        goto end;
1753e1051a39Sopenharmony_ci
1754e1051a39Sopenharmony_ci    memset(sbuf, 0, sizeof(sbuf));
1755e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
1756e1051a39Sopenharmony_ci        goto end;
1757e1051a39Sopenharmony_ci
1758e1051a39Sopenharmony_ci    if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
1759e1051a39Sopenharmony_ci        goto end;
1760e1051a39Sopenharmony_ci
1761e1051a39Sopenharmony_ci    /* Check if rbuf is cleansed */
1762e1051a39Sopenharmony_ci    memset(cbuf, 0, sizeof(cbuf));
1763e1051a39Sopenharmony_ci    if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
1764e1051a39Sopenharmony_ci        goto end;
1765e1051a39Sopenharmony_ci
1766e1051a39Sopenharmony_ci    testresult = 1;
1767e1051a39Sopenharmony_ci end:
1768e1051a39Sopenharmony_ci    SSL_free(serverssl);
1769e1051a39Sopenharmony_ci    SSL_free(clientssl);
1770e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1771e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1772e1051a39Sopenharmony_ci
1773e1051a39Sopenharmony_ci    return testresult;
1774e1051a39Sopenharmony_ci}
1775e1051a39Sopenharmony_ci
1776e1051a39Sopenharmony_cistatic int test_cleanse_plaintext(void)
1777e1051a39Sopenharmony_ci{
1778e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2)
1779e1051a39Sopenharmony_ci    if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1780e1051a39Sopenharmony_ci                                             TLS_client_method(),
1781e1051a39Sopenharmony_ci                                             TLS1_2_VERSION,
1782e1051a39Sopenharmony_ci                                             TLS1_2_VERSION)))
1783e1051a39Sopenharmony_ci        return 0;
1784e1051a39Sopenharmony_ci
1785e1051a39Sopenharmony_ci#endif
1786e1051a39Sopenharmony_ci
1787e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3)
1788e1051a39Sopenharmony_ci    if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
1789e1051a39Sopenharmony_ci                                             TLS_client_method(),
1790e1051a39Sopenharmony_ci                                             TLS1_3_VERSION,
1791e1051a39Sopenharmony_ci                                             TLS1_3_VERSION)))
1792e1051a39Sopenharmony_ci        return 0;
1793e1051a39Sopenharmony_ci#endif
1794e1051a39Sopenharmony_ci
1795e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_DTLS)
1796e1051a39Sopenharmony_ci
1797e1051a39Sopenharmony_ci    if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
1798e1051a39Sopenharmony_ci                                             DTLS_client_method(),
1799e1051a39Sopenharmony_ci                                             DTLS1_VERSION,
1800e1051a39Sopenharmony_ci                                             0)))
1801e1051a39Sopenharmony_ci        return 0;
1802e1051a39Sopenharmony_ci#endif
1803e1051a39Sopenharmony_ci    return 1;
1804e1051a39Sopenharmony_ci}
1805e1051a39Sopenharmony_ci
1806e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCSP
1807e1051a39Sopenharmony_cistatic int ocsp_server_cb(SSL *s, void *arg)
1808e1051a39Sopenharmony_ci{
1809e1051a39Sopenharmony_ci    int *argi = (int *)arg;
1810e1051a39Sopenharmony_ci    unsigned char *copy = NULL;
1811e1051a39Sopenharmony_ci    STACK_OF(OCSP_RESPID) *ids = NULL;
1812e1051a39Sopenharmony_ci    OCSP_RESPID *id = NULL;
1813e1051a39Sopenharmony_ci
1814e1051a39Sopenharmony_ci    if (*argi == 2) {
1815e1051a39Sopenharmony_ci        /* In this test we are expecting exactly 1 OCSP_RESPID */
1816e1051a39Sopenharmony_ci        SSL_get_tlsext_status_ids(s, &ids);
1817e1051a39Sopenharmony_ci        if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
1818e1051a39Sopenharmony_ci            return SSL_TLSEXT_ERR_ALERT_FATAL;
1819e1051a39Sopenharmony_ci
1820e1051a39Sopenharmony_ci        id = sk_OCSP_RESPID_value(ids, 0);
1821e1051a39Sopenharmony_ci        if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
1822e1051a39Sopenharmony_ci            return SSL_TLSEXT_ERR_ALERT_FATAL;
1823e1051a39Sopenharmony_ci    } else if (*argi != 1) {
1824e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_ALERT_FATAL;
1825e1051a39Sopenharmony_ci    }
1826e1051a39Sopenharmony_ci
1827e1051a39Sopenharmony_ci    if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
1828e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_ALERT_FATAL;
1829e1051a39Sopenharmony_ci
1830e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
1831e1051a39Sopenharmony_ci                                                   sizeof(orespder)))) {
1832e1051a39Sopenharmony_ci        OPENSSL_free(copy);
1833e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_ALERT_FATAL;
1834e1051a39Sopenharmony_ci    }
1835e1051a39Sopenharmony_ci    ocsp_server_called = 1;
1836e1051a39Sopenharmony_ci    return SSL_TLSEXT_ERR_OK;
1837e1051a39Sopenharmony_ci}
1838e1051a39Sopenharmony_ci
1839e1051a39Sopenharmony_cistatic int ocsp_client_cb(SSL *s, void *arg)
1840e1051a39Sopenharmony_ci{
1841e1051a39Sopenharmony_ci    int *argi = (int *)arg;
1842e1051a39Sopenharmony_ci    const unsigned char *respderin;
1843e1051a39Sopenharmony_ci    size_t len;
1844e1051a39Sopenharmony_ci
1845e1051a39Sopenharmony_ci    if (*argi != 1 && *argi != 2)
1846e1051a39Sopenharmony_ci        return 0;
1847e1051a39Sopenharmony_ci
1848e1051a39Sopenharmony_ci    len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
1849e1051a39Sopenharmony_ci    if (!TEST_mem_eq(orespder, len, respderin, len))
1850e1051a39Sopenharmony_ci        return 0;
1851e1051a39Sopenharmony_ci
1852e1051a39Sopenharmony_ci    ocsp_client_called = 1;
1853e1051a39Sopenharmony_ci    return 1;
1854e1051a39Sopenharmony_ci}
1855e1051a39Sopenharmony_ci
1856e1051a39Sopenharmony_cistatic int test_tlsext_status_type(void)
1857e1051a39Sopenharmony_ci{
1858e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
1859e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
1860e1051a39Sopenharmony_ci    int testresult = 0;
1861e1051a39Sopenharmony_ci    STACK_OF(OCSP_RESPID) *ids = NULL;
1862e1051a39Sopenharmony_ci    OCSP_RESPID *id = NULL;
1863e1051a39Sopenharmony_ci    BIO *certbio = NULL;
1864e1051a39Sopenharmony_ci
1865e1051a39Sopenharmony_ci    if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
1866e1051a39Sopenharmony_ci                             TLS1_VERSION, 0,
1867e1051a39Sopenharmony_ci                             &sctx, &cctx, cert, privkey))
1868e1051a39Sopenharmony_ci        return 0;
1869e1051a39Sopenharmony_ci
1870e1051a39Sopenharmony_ci    if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
1871e1051a39Sopenharmony_ci        goto end;
1872e1051a39Sopenharmony_ci
1873e1051a39Sopenharmony_ci    /* First just do various checks getting and setting tlsext_status_type */
1874e1051a39Sopenharmony_ci
1875e1051a39Sopenharmony_ci    clientssl = SSL_new(cctx);
1876e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
1877e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_tlsext_status_type(clientssl,
1878e1051a39Sopenharmony_ci                                                      TLSEXT_STATUSTYPE_ocsp))
1879e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
1880e1051a39Sopenharmony_ci                            TLSEXT_STATUSTYPE_ocsp))
1881e1051a39Sopenharmony_ci        goto end;
1882e1051a39Sopenharmony_ci
1883e1051a39Sopenharmony_ci    SSL_free(clientssl);
1884e1051a39Sopenharmony_ci    clientssl = NULL;
1885e1051a39Sopenharmony_ci
1886e1051a39Sopenharmony_ci    if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
1887e1051a39Sopenharmony_ci     || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
1888e1051a39Sopenharmony_ci        goto end;
1889e1051a39Sopenharmony_ci
1890e1051a39Sopenharmony_ci    clientssl = SSL_new(cctx);
1891e1051a39Sopenharmony_ci    if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
1892e1051a39Sopenharmony_ci        goto end;
1893e1051a39Sopenharmony_ci    SSL_free(clientssl);
1894e1051a39Sopenharmony_ci    clientssl = NULL;
1895e1051a39Sopenharmony_ci
1896e1051a39Sopenharmony_ci    /*
1897e1051a39Sopenharmony_ci     * Now actually do a handshake and check OCSP information is exchanged and
1898e1051a39Sopenharmony_ci     * the callbacks get called
1899e1051a39Sopenharmony_ci     */
1900e1051a39Sopenharmony_ci    SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
1901e1051a39Sopenharmony_ci    SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
1902e1051a39Sopenharmony_ci    SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
1903e1051a39Sopenharmony_ci    SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
1904e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1905e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
1906e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
1907e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
1908e1051a39Sopenharmony_ci            || !TEST_true(ocsp_client_called)
1909e1051a39Sopenharmony_ci            || !TEST_true(ocsp_server_called))
1910e1051a39Sopenharmony_ci        goto end;
1911e1051a39Sopenharmony_ci    SSL_free(serverssl);
1912e1051a39Sopenharmony_ci    SSL_free(clientssl);
1913e1051a39Sopenharmony_ci    serverssl = NULL;
1914e1051a39Sopenharmony_ci    clientssl = NULL;
1915e1051a39Sopenharmony_ci
1916e1051a39Sopenharmony_ci    /* Try again but this time force the server side callback to fail */
1917e1051a39Sopenharmony_ci    ocsp_client_called = 0;
1918e1051a39Sopenharmony_ci    ocsp_server_called = 0;
1919e1051a39Sopenharmony_ci    cdummyarg = 0;
1920e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1921e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
1922e1051a39Sopenharmony_ci                /* This should fail because the callback will fail */
1923e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
1924e1051a39Sopenharmony_ci                                                 SSL_ERROR_NONE))
1925e1051a39Sopenharmony_ci            || !TEST_false(ocsp_client_called)
1926e1051a39Sopenharmony_ci            || !TEST_false(ocsp_server_called))
1927e1051a39Sopenharmony_ci        goto end;
1928e1051a39Sopenharmony_ci    SSL_free(serverssl);
1929e1051a39Sopenharmony_ci    SSL_free(clientssl);
1930e1051a39Sopenharmony_ci    serverssl = NULL;
1931e1051a39Sopenharmony_ci    clientssl = NULL;
1932e1051a39Sopenharmony_ci
1933e1051a39Sopenharmony_ci    /*
1934e1051a39Sopenharmony_ci     * This time we'll get the client to send an OCSP_RESPID that it will
1935e1051a39Sopenharmony_ci     * accept.
1936e1051a39Sopenharmony_ci     */
1937e1051a39Sopenharmony_ci    ocsp_client_called = 0;
1938e1051a39Sopenharmony_ci    ocsp_server_called = 0;
1939e1051a39Sopenharmony_ci    cdummyarg = 2;
1940e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
1941e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
1942e1051a39Sopenharmony_ci        goto end;
1943e1051a39Sopenharmony_ci
1944e1051a39Sopenharmony_ci    /*
1945e1051a39Sopenharmony_ci     * We'll just use any old cert for this test - it doesn't have to be an OCSP
1946e1051a39Sopenharmony_ci     * specific one. We'll use the server cert.
1947e1051a39Sopenharmony_ci     */
1948e1051a39Sopenharmony_ci    if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
1949e1051a39Sopenharmony_ci            || !TEST_ptr(id = OCSP_RESPID_new())
1950e1051a39Sopenharmony_ci            || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
1951e1051a39Sopenharmony_ci            || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
1952e1051a39Sopenharmony_ci            || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
1953e1051a39Sopenharmony_ci            || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
1954e1051a39Sopenharmony_ci            || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
1955e1051a39Sopenharmony_ci        goto end;
1956e1051a39Sopenharmony_ci    id = NULL;
1957e1051a39Sopenharmony_ci    SSL_set_tlsext_status_ids(clientssl, ids);
1958e1051a39Sopenharmony_ci    /* Control has been transferred */
1959e1051a39Sopenharmony_ci    ids = NULL;
1960e1051a39Sopenharmony_ci
1961e1051a39Sopenharmony_ci    BIO_free(certbio);
1962e1051a39Sopenharmony_ci    certbio = NULL;
1963e1051a39Sopenharmony_ci
1964e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
1965e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE))
1966e1051a39Sopenharmony_ci            || !TEST_true(ocsp_client_called)
1967e1051a39Sopenharmony_ci            || !TEST_true(ocsp_server_called))
1968e1051a39Sopenharmony_ci        goto end;
1969e1051a39Sopenharmony_ci
1970e1051a39Sopenharmony_ci    testresult = 1;
1971e1051a39Sopenharmony_ci
1972e1051a39Sopenharmony_ci end:
1973e1051a39Sopenharmony_ci    SSL_free(serverssl);
1974e1051a39Sopenharmony_ci    SSL_free(clientssl);
1975e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
1976e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
1977e1051a39Sopenharmony_ci    sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
1978e1051a39Sopenharmony_ci    OCSP_RESPID_free(id);
1979e1051a39Sopenharmony_ci    BIO_free(certbio);
1980e1051a39Sopenharmony_ci    X509_free(ocspcert);
1981e1051a39Sopenharmony_ci    ocspcert = NULL;
1982e1051a39Sopenharmony_ci
1983e1051a39Sopenharmony_ci    return testresult;
1984e1051a39Sopenharmony_ci}
1985e1051a39Sopenharmony_ci#endif
1986e1051a39Sopenharmony_ci
1987e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
1988e1051a39Sopenharmony_cistatic int new_called, remove_called, get_called;
1989e1051a39Sopenharmony_ci
1990e1051a39Sopenharmony_cistatic int new_session_cb(SSL *ssl, SSL_SESSION *sess)
1991e1051a39Sopenharmony_ci{
1992e1051a39Sopenharmony_ci    new_called++;
1993e1051a39Sopenharmony_ci    /*
1994e1051a39Sopenharmony_ci     * sess has been up-refed for us, but we don't actually need it so free it
1995e1051a39Sopenharmony_ci     * immediately.
1996e1051a39Sopenharmony_ci     */
1997e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
1998e1051a39Sopenharmony_ci    return 1;
1999e1051a39Sopenharmony_ci}
2000e1051a39Sopenharmony_ci
2001e1051a39Sopenharmony_cistatic void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
2002e1051a39Sopenharmony_ci{
2003e1051a39Sopenharmony_ci    remove_called++;
2004e1051a39Sopenharmony_ci}
2005e1051a39Sopenharmony_ci
2006e1051a39Sopenharmony_cistatic SSL_SESSION *get_sess_val = NULL;
2007e1051a39Sopenharmony_ci
2008e1051a39Sopenharmony_cistatic SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
2009e1051a39Sopenharmony_ci                                   int *copy)
2010e1051a39Sopenharmony_ci{
2011e1051a39Sopenharmony_ci    get_called++;
2012e1051a39Sopenharmony_ci    *copy = 1;
2013e1051a39Sopenharmony_ci    return get_sess_val;
2014e1051a39Sopenharmony_ci}
2015e1051a39Sopenharmony_ci
2016e1051a39Sopenharmony_cistatic int execute_test_session(int maxprot, int use_int_cache,
2017e1051a39Sopenharmony_ci                                int use_ext_cache, long s_options)
2018e1051a39Sopenharmony_ci{
2019e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
2020e1051a39Sopenharmony_ci    SSL *serverssl1 = NULL, *clientssl1 = NULL;
2021e1051a39Sopenharmony_ci    SSL *serverssl2 = NULL, *clientssl2 = NULL;
2022e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_1
2023e1051a39Sopenharmony_ci    SSL *serverssl3 = NULL, *clientssl3 = NULL;
2024e1051a39Sopenharmony_ci# endif
2025e1051a39Sopenharmony_ci    SSL_SESSION *sess1 = NULL, *sess2 = NULL;
2026e1051a39Sopenharmony_ci    int testresult = 0, numnewsesstick = 1;
2027e1051a39Sopenharmony_ci
2028e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2029e1051a39Sopenharmony_ci
2030e1051a39Sopenharmony_ci    /* TLSv1.3 sends 2 NewSessionTickets */
2031e1051a39Sopenharmony_ci    if (maxprot == TLS1_3_VERSION)
2032e1051a39Sopenharmony_ci        numnewsesstick = 2;
2033e1051a39Sopenharmony_ci
2034e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2035e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
2036e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
2037e1051a39Sopenharmony_ci        return 0;
2038e1051a39Sopenharmony_ci
2039e1051a39Sopenharmony_ci    /*
2040e1051a39Sopenharmony_ci     * Only allow the max protocol version so we can force a connection failure
2041e1051a39Sopenharmony_ci     * later
2042e1051a39Sopenharmony_ci     */
2043e1051a39Sopenharmony_ci    SSL_CTX_set_min_proto_version(cctx, maxprot);
2044e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(cctx, maxprot);
2045e1051a39Sopenharmony_ci
2046e1051a39Sopenharmony_ci    /* Set up session cache */
2047e1051a39Sopenharmony_ci    if (use_ext_cache) {
2048e1051a39Sopenharmony_ci        SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2049e1051a39Sopenharmony_ci        SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
2050e1051a39Sopenharmony_ci    }
2051e1051a39Sopenharmony_ci    if (use_int_cache) {
2052e1051a39Sopenharmony_ci        /* Also covers instance where both are set */
2053e1051a39Sopenharmony_ci        SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
2054e1051a39Sopenharmony_ci    } else {
2055e1051a39Sopenharmony_ci        SSL_CTX_set_session_cache_mode(cctx,
2056e1051a39Sopenharmony_ci                                       SSL_SESS_CACHE_CLIENT
2057e1051a39Sopenharmony_ci                                       | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2058e1051a39Sopenharmony_ci    }
2059e1051a39Sopenharmony_ci
2060e1051a39Sopenharmony_ci    if (s_options) {
2061e1051a39Sopenharmony_ci        SSL_CTX_set_options(sctx, s_options);
2062e1051a39Sopenharmony_ci    }
2063e1051a39Sopenharmony_ci
2064e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2065e1051a39Sopenharmony_ci                                      NULL, NULL))
2066e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2067e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2068e1051a39Sopenharmony_ci            || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
2069e1051a39Sopenharmony_ci        goto end;
2070e1051a39Sopenharmony_ci
2071e1051a39Sopenharmony_ci    /* Should fail because it should already be in the cache */
2072e1051a39Sopenharmony_ci    if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
2073e1051a39Sopenharmony_ci        goto end;
2074e1051a39Sopenharmony_ci    if (use_ext_cache
2075e1051a39Sopenharmony_ci            && (!TEST_int_eq(new_called, numnewsesstick)
2076e1051a39Sopenharmony_ci
2077e1051a39Sopenharmony_ci                || !TEST_int_eq(remove_called, 0)))
2078e1051a39Sopenharmony_ci        goto end;
2079e1051a39Sopenharmony_ci
2080e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2081e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2082e1051a39Sopenharmony_ci                                      &clientssl2, NULL, NULL))
2083e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl2, sess1))
2084e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2085e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2086e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl2)))
2087e1051a39Sopenharmony_ci        goto end;
2088e1051a39Sopenharmony_ci
2089e1051a39Sopenharmony_ci    if (maxprot == TLS1_3_VERSION) {
2090e1051a39Sopenharmony_ci        /*
2091e1051a39Sopenharmony_ci         * In TLSv1.3 we should have created a new session even though we have
2092e1051a39Sopenharmony_ci         * resumed. Since we attempted a resume we should also have removed the
2093e1051a39Sopenharmony_ci         * old ticket from the cache so that we try to only use tickets once.
2094e1051a39Sopenharmony_ci         */
2095e1051a39Sopenharmony_ci        if (use_ext_cache
2096e1051a39Sopenharmony_ci                && (!TEST_int_eq(new_called, 1)
2097e1051a39Sopenharmony_ci                    || !TEST_int_eq(remove_called, 1)))
2098e1051a39Sopenharmony_ci            goto end;
2099e1051a39Sopenharmony_ci    } else {
2100e1051a39Sopenharmony_ci        /*
2101e1051a39Sopenharmony_ci         * In TLSv1.2 we expect to have resumed so no sessions added or
2102e1051a39Sopenharmony_ci         * removed.
2103e1051a39Sopenharmony_ci         */
2104e1051a39Sopenharmony_ci        if (use_ext_cache
2105e1051a39Sopenharmony_ci                && (!TEST_int_eq(new_called, 0)
2106e1051a39Sopenharmony_ci                    || !TEST_int_eq(remove_called, 0)))
2107e1051a39Sopenharmony_ci            goto end;
2108e1051a39Sopenharmony_ci    }
2109e1051a39Sopenharmony_ci
2110e1051a39Sopenharmony_ci    SSL_SESSION_free(sess1);
2111e1051a39Sopenharmony_ci    if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
2112e1051a39Sopenharmony_ci        goto end;
2113e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl2, clientssl2);
2114e1051a39Sopenharmony_ci    serverssl2 = clientssl2 = NULL;
2115e1051a39Sopenharmony_ci
2116e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2117e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2118e1051a39Sopenharmony_ci                                      &clientssl2, NULL, NULL))
2119e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2120e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
2121e1051a39Sopenharmony_ci        goto end;
2122e1051a39Sopenharmony_ci
2123e1051a39Sopenharmony_ci    if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
2124e1051a39Sopenharmony_ci        goto end;
2125e1051a39Sopenharmony_ci
2126e1051a39Sopenharmony_ci    if (use_ext_cache
2127e1051a39Sopenharmony_ci            && (!TEST_int_eq(new_called, numnewsesstick)
2128e1051a39Sopenharmony_ci                || !TEST_int_eq(remove_called, 0)))
2129e1051a39Sopenharmony_ci        goto end;
2130e1051a39Sopenharmony_ci
2131e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2132e1051a39Sopenharmony_ci    /*
2133e1051a39Sopenharmony_ci     * This should clear sess2 from the cache because it is a "bad" session.
2134e1051a39Sopenharmony_ci     * See SSL_set_session() documentation.
2135e1051a39Sopenharmony_ci     */
2136e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_session(clientssl2, sess1)))
2137e1051a39Sopenharmony_ci        goto end;
2138e1051a39Sopenharmony_ci    if (use_ext_cache
2139e1051a39Sopenharmony_ci            && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2140e1051a39Sopenharmony_ci        goto end;
2141e1051a39Sopenharmony_ci    if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
2142e1051a39Sopenharmony_ci        goto end;
2143e1051a39Sopenharmony_ci
2144e1051a39Sopenharmony_ci    if (use_int_cache) {
2145e1051a39Sopenharmony_ci        /* Should succeeded because it should not already be in the cache */
2146e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
2147e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
2148e1051a39Sopenharmony_ci            goto end;
2149e1051a39Sopenharmony_ci    }
2150e1051a39Sopenharmony_ci
2151e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2152e1051a39Sopenharmony_ci    /* This shouldn't be in the cache so should fail */
2153e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
2154e1051a39Sopenharmony_ci        goto end;
2155e1051a39Sopenharmony_ci
2156e1051a39Sopenharmony_ci    if (use_ext_cache
2157e1051a39Sopenharmony_ci            && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2158e1051a39Sopenharmony_ci        goto end;
2159e1051a39Sopenharmony_ci
2160e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_TLS1_1)
2161e1051a39Sopenharmony_ci    new_called = remove_called = 0;
2162e1051a39Sopenharmony_ci    /* Force a connection failure */
2163e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
2164e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
2165e1051a39Sopenharmony_ci                                      &clientssl3, NULL, NULL))
2166e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl3, sess1))
2167e1051a39Sopenharmony_ci            /* This should fail because of the mismatched protocol versions */
2168e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
2169e1051a39Sopenharmony_ci                                                 SSL_ERROR_NONE)))
2170e1051a39Sopenharmony_ci        goto end;
2171e1051a39Sopenharmony_ci
2172e1051a39Sopenharmony_ci    /* We should have automatically removed the session from the cache */
2173e1051a39Sopenharmony_ci    if (use_ext_cache
2174e1051a39Sopenharmony_ci            && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
2175e1051a39Sopenharmony_ci        goto end;
2176e1051a39Sopenharmony_ci
2177e1051a39Sopenharmony_ci    /* Should succeed because it should not already be in the cache */
2178e1051a39Sopenharmony_ci    if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
2179e1051a39Sopenharmony_ci        goto end;
2180e1051a39Sopenharmony_ci# endif
2181e1051a39Sopenharmony_ci
2182e1051a39Sopenharmony_ci    /* Now do some tests for server side caching */
2183e1051a39Sopenharmony_ci    if (use_ext_cache) {
2184e1051a39Sopenharmony_ci        SSL_CTX_sess_set_new_cb(cctx, NULL);
2185e1051a39Sopenharmony_ci        SSL_CTX_sess_set_remove_cb(cctx, NULL);
2186e1051a39Sopenharmony_ci        SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2187e1051a39Sopenharmony_ci        SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
2188e1051a39Sopenharmony_ci        SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
2189e1051a39Sopenharmony_ci        get_sess_val = NULL;
2190e1051a39Sopenharmony_ci    }
2191e1051a39Sopenharmony_ci
2192e1051a39Sopenharmony_ci    SSL_CTX_set_session_cache_mode(cctx, 0);
2193e1051a39Sopenharmony_ci    /* Internal caching is the default on the server side */
2194e1051a39Sopenharmony_ci    if (!use_int_cache)
2195e1051a39Sopenharmony_ci        SSL_CTX_set_session_cache_mode(sctx,
2196e1051a39Sopenharmony_ci                                       SSL_SESS_CACHE_SERVER
2197e1051a39Sopenharmony_ci                                       | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2198e1051a39Sopenharmony_ci
2199e1051a39Sopenharmony_ci    SSL_free(serverssl1);
2200e1051a39Sopenharmony_ci    SSL_free(clientssl1);
2201e1051a39Sopenharmony_ci    serverssl1 = clientssl1 = NULL;
2202e1051a39Sopenharmony_ci    SSL_free(serverssl2);
2203e1051a39Sopenharmony_ci    SSL_free(clientssl2);
2204e1051a39Sopenharmony_ci    serverssl2 = clientssl2 = NULL;
2205e1051a39Sopenharmony_ci    SSL_SESSION_free(sess1);
2206e1051a39Sopenharmony_ci    sess1 = NULL;
2207e1051a39Sopenharmony_ci    SSL_SESSION_free(sess2);
2208e1051a39Sopenharmony_ci    sess2 = NULL;
2209e1051a39Sopenharmony_ci
2210e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(sctx, maxprot);
2211e1051a39Sopenharmony_ci    if (maxprot == TLS1_2_VERSION)
2212e1051a39Sopenharmony_ci        SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
2213e1051a39Sopenharmony_ci    new_called = remove_called = get_called = 0;
2214e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
2215e1051a39Sopenharmony_ci                                      NULL, NULL))
2216e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
2217e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2218e1051a39Sopenharmony_ci            || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
2219e1051a39Sopenharmony_ci            || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
2220e1051a39Sopenharmony_ci        goto end;
2221e1051a39Sopenharmony_ci
2222e1051a39Sopenharmony_ci    if (use_int_cache) {
2223e1051a39Sopenharmony_ci        if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
2224e1051a39Sopenharmony_ci            /*
2225e1051a39Sopenharmony_ci             * In TLSv1.3 it should not have been added to the internal cache,
2226e1051a39Sopenharmony_ci             * except in the case where we also have an external cache (in that
2227e1051a39Sopenharmony_ci             * case it gets added to the cache in order to generate remove
2228e1051a39Sopenharmony_ci             * events after timeout).
2229e1051a39Sopenharmony_ci             */
2230e1051a39Sopenharmony_ci            if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
2231e1051a39Sopenharmony_ci                goto end;
2232e1051a39Sopenharmony_ci        } else {
2233e1051a39Sopenharmony_ci            /* Should fail because it should already be in the cache */
2234e1051a39Sopenharmony_ci            if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
2235e1051a39Sopenharmony_ci                goto end;
2236e1051a39Sopenharmony_ci        }
2237e1051a39Sopenharmony_ci    }
2238e1051a39Sopenharmony_ci
2239e1051a39Sopenharmony_ci    if (use_ext_cache) {
2240e1051a39Sopenharmony_ci        SSL_SESSION *tmp = sess2;
2241e1051a39Sopenharmony_ci
2242e1051a39Sopenharmony_ci        if (!TEST_int_eq(new_called, numnewsesstick)
2243e1051a39Sopenharmony_ci                || !TEST_int_eq(remove_called, 0)
2244e1051a39Sopenharmony_ci                || !TEST_int_eq(get_called, 0))
2245e1051a39Sopenharmony_ci            goto end;
2246e1051a39Sopenharmony_ci        /*
2247e1051a39Sopenharmony_ci         * Delete the session from the internal cache to force a lookup from
2248e1051a39Sopenharmony_ci         * the external cache. We take a copy first because
2249e1051a39Sopenharmony_ci         * SSL_CTX_remove_session() also marks the session as non-resumable.
2250e1051a39Sopenharmony_ci         */
2251e1051a39Sopenharmony_ci        if (use_int_cache && maxprot != TLS1_3_VERSION) {
2252e1051a39Sopenharmony_ci            if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
2253e1051a39Sopenharmony_ci                || !TEST_true(sess2->owner != NULL)
2254e1051a39Sopenharmony_ci                || !TEST_true(tmp->owner == NULL)
2255e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
2256e1051a39Sopenharmony_ci                goto end;
2257e1051a39Sopenharmony_ci            SSL_SESSION_free(sess2);
2258e1051a39Sopenharmony_ci        }
2259e1051a39Sopenharmony_ci        sess2 = tmp;
2260e1051a39Sopenharmony_ci    }
2261e1051a39Sopenharmony_ci
2262e1051a39Sopenharmony_ci    new_called = remove_called = get_called = 0;
2263e1051a39Sopenharmony_ci    get_sess_val = sess2;
2264e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
2265e1051a39Sopenharmony_ci                                      &clientssl2, NULL, NULL))
2266e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl2, sess1))
2267e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
2268e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2269e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl2)))
2270e1051a39Sopenharmony_ci        goto end;
2271e1051a39Sopenharmony_ci
2272e1051a39Sopenharmony_ci    if (use_ext_cache) {
2273e1051a39Sopenharmony_ci        if (!TEST_int_eq(remove_called, 0))
2274e1051a39Sopenharmony_ci            goto end;
2275e1051a39Sopenharmony_ci
2276e1051a39Sopenharmony_ci        if (maxprot == TLS1_3_VERSION) {
2277e1051a39Sopenharmony_ci            if (!TEST_int_eq(new_called, 1)
2278e1051a39Sopenharmony_ci                    || !TEST_int_eq(get_called, 0))
2279e1051a39Sopenharmony_ci                goto end;
2280e1051a39Sopenharmony_ci        } else {
2281e1051a39Sopenharmony_ci            if (!TEST_int_eq(new_called, 0)
2282e1051a39Sopenharmony_ci                    || !TEST_int_eq(get_called, 1))
2283e1051a39Sopenharmony_ci                goto end;
2284e1051a39Sopenharmony_ci        }
2285e1051a39Sopenharmony_ci    }
2286e1051a39Sopenharmony_ci    /*
2287e1051a39Sopenharmony_ci     * Make a small cache, force out all other sessions but
2288e1051a39Sopenharmony_ci     * sess2, try to add sess1, which should succeed. Then
2289e1051a39Sopenharmony_ci     * make sure it's there by checking the owners. Despite
2290e1051a39Sopenharmony_ci     * the timeouts, sess1 should have kicked out sess2
2291e1051a39Sopenharmony_ci     */
2292e1051a39Sopenharmony_ci
2293e1051a39Sopenharmony_ci    /* Make sess1 expire before sess2 */
2294e1051a39Sopenharmony_ci    if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
2295e1051a39Sopenharmony_ci            || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
2296e1051a39Sopenharmony_ci            || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
2297e1051a39Sopenharmony_ci            || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
2298e1051a39Sopenharmony_ci        goto end;
2299e1051a39Sopenharmony_ci
2300e1051a39Sopenharmony_ci    if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
2301e1051a39Sopenharmony_ci        goto end;
2302e1051a39Sopenharmony_ci
2303e1051a39Sopenharmony_ci    /* Don't care about results - cache should only be sess2 at end */
2304e1051a39Sopenharmony_ci    SSL_CTX_add_session(sctx, sess1);
2305e1051a39Sopenharmony_ci    SSL_CTX_add_session(sctx, sess2);
2306e1051a39Sopenharmony_ci
2307e1051a39Sopenharmony_ci    /* Now add sess1, and make sure it remains, despite timeout */
2308e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
2309e1051a39Sopenharmony_ci            || !TEST_ptr(sess1->owner)
2310e1051a39Sopenharmony_ci            || !TEST_ptr_null(sess2->owner))
2311e1051a39Sopenharmony_ci        goto end;
2312e1051a39Sopenharmony_ci
2313e1051a39Sopenharmony_ci    testresult = 1;
2314e1051a39Sopenharmony_ci
2315e1051a39Sopenharmony_ci end:
2316e1051a39Sopenharmony_ci    SSL_free(serverssl1);
2317e1051a39Sopenharmony_ci    SSL_free(clientssl1);
2318e1051a39Sopenharmony_ci    SSL_free(serverssl2);
2319e1051a39Sopenharmony_ci    SSL_free(clientssl2);
2320e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_1
2321e1051a39Sopenharmony_ci    SSL_free(serverssl3);
2322e1051a39Sopenharmony_ci    SSL_free(clientssl3);
2323e1051a39Sopenharmony_ci# endif
2324e1051a39Sopenharmony_ci    SSL_SESSION_free(sess1);
2325e1051a39Sopenharmony_ci    SSL_SESSION_free(sess2);
2326e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2327e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2328e1051a39Sopenharmony_ci
2329e1051a39Sopenharmony_ci    return testresult;
2330e1051a39Sopenharmony_ci}
2331e1051a39Sopenharmony_ci#endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
2332e1051a39Sopenharmony_ci
2333e1051a39Sopenharmony_cistatic int test_session_with_only_int_cache(void)
2334e1051a39Sopenharmony_ci{
2335e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
2336e1051a39Sopenharmony_ci    if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
2337e1051a39Sopenharmony_ci        return 0;
2338e1051a39Sopenharmony_ci#endif
2339e1051a39Sopenharmony_ci
2340e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
2341e1051a39Sopenharmony_ci    return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
2342e1051a39Sopenharmony_ci#else
2343e1051a39Sopenharmony_ci    return 1;
2344e1051a39Sopenharmony_ci#endif
2345e1051a39Sopenharmony_ci}
2346e1051a39Sopenharmony_ci
2347e1051a39Sopenharmony_cistatic int test_session_with_only_ext_cache(void)
2348e1051a39Sopenharmony_ci{
2349e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
2350e1051a39Sopenharmony_ci    if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
2351e1051a39Sopenharmony_ci        return 0;
2352e1051a39Sopenharmony_ci#endif
2353e1051a39Sopenharmony_ci
2354e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
2355e1051a39Sopenharmony_ci    return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
2356e1051a39Sopenharmony_ci#else
2357e1051a39Sopenharmony_ci    return 1;
2358e1051a39Sopenharmony_ci#endif
2359e1051a39Sopenharmony_ci}
2360e1051a39Sopenharmony_ci
2361e1051a39Sopenharmony_cistatic int test_session_with_both_cache(void)
2362e1051a39Sopenharmony_ci{
2363e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
2364e1051a39Sopenharmony_ci    if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
2365e1051a39Sopenharmony_ci        return 0;
2366e1051a39Sopenharmony_ci#endif
2367e1051a39Sopenharmony_ci
2368e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
2369e1051a39Sopenharmony_ci    return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
2370e1051a39Sopenharmony_ci#else
2371e1051a39Sopenharmony_ci    return 1;
2372e1051a39Sopenharmony_ci#endif
2373e1051a39Sopenharmony_ci}
2374e1051a39Sopenharmony_ci
2375e1051a39Sopenharmony_cistatic int test_session_wo_ca_names(void)
2376e1051a39Sopenharmony_ci{
2377e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
2378e1051a39Sopenharmony_ci    if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
2379e1051a39Sopenharmony_ci        return 0;
2380e1051a39Sopenharmony_ci#endif
2381e1051a39Sopenharmony_ci
2382e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
2383e1051a39Sopenharmony_ci    return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
2384e1051a39Sopenharmony_ci#else
2385e1051a39Sopenharmony_ci    return 1;
2386e1051a39Sopenharmony_ci#endif
2387e1051a39Sopenharmony_ci}
2388e1051a39Sopenharmony_ci
2389e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
2390e1051a39Sopenharmony_cistatic SSL_SESSION *sesscache[6];
2391e1051a39Sopenharmony_cistatic int do_cache;
2392e1051a39Sopenharmony_ci
2393e1051a39Sopenharmony_cistatic int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
2394e1051a39Sopenharmony_ci{
2395e1051a39Sopenharmony_ci    if (do_cache) {
2396e1051a39Sopenharmony_ci        sesscache[new_called] = sess;
2397e1051a39Sopenharmony_ci    } else {
2398e1051a39Sopenharmony_ci        /* We don't need the reference to the session, so free it */
2399e1051a39Sopenharmony_ci        SSL_SESSION_free(sess);
2400e1051a39Sopenharmony_ci    }
2401e1051a39Sopenharmony_ci    new_called++;
2402e1051a39Sopenharmony_ci
2403e1051a39Sopenharmony_ci    return 1;
2404e1051a39Sopenharmony_ci}
2405e1051a39Sopenharmony_ci
2406e1051a39Sopenharmony_cistatic int post_handshake_verify(SSL *sssl, SSL *cssl)
2407e1051a39Sopenharmony_ci{
2408e1051a39Sopenharmony_ci    SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
2409e1051a39Sopenharmony_ci    if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
2410e1051a39Sopenharmony_ci        return 0;
2411e1051a39Sopenharmony_ci
2412e1051a39Sopenharmony_ci    /* Start handshake on the server and client */
2413e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
2414e1051a39Sopenharmony_ci            || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
2415e1051a39Sopenharmony_ci            || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
2416e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(sssl, cssl,
2417e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
2418e1051a39Sopenharmony_ci        return 0;
2419e1051a39Sopenharmony_ci
2420e1051a39Sopenharmony_ci    return 1;
2421e1051a39Sopenharmony_ci}
2422e1051a39Sopenharmony_ci
2423e1051a39Sopenharmony_cistatic int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
2424e1051a39Sopenharmony_ci                             SSL_CTX **cctx)
2425e1051a39Sopenharmony_ci{
2426e1051a39Sopenharmony_ci    int sess_id_ctx = 1;
2427e1051a39Sopenharmony_ci
2428e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2429e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
2430e1051a39Sopenharmony_ci                                       sctx, cctx, cert, privkey))
2431e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
2432e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
2433e1051a39Sopenharmony_ci                                                         (void *)&sess_id_ctx,
2434e1051a39Sopenharmony_ci                                                         sizeof(sess_id_ctx))))
2435e1051a39Sopenharmony_ci        return 0;
2436e1051a39Sopenharmony_ci
2437e1051a39Sopenharmony_ci    if (stateful)
2438e1051a39Sopenharmony_ci        SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
2439e1051a39Sopenharmony_ci
2440e1051a39Sopenharmony_ci    SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
2441e1051a39Sopenharmony_ci                                          | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2442e1051a39Sopenharmony_ci    SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
2443e1051a39Sopenharmony_ci
2444e1051a39Sopenharmony_ci    return 1;
2445e1051a39Sopenharmony_ci}
2446e1051a39Sopenharmony_ci
2447e1051a39Sopenharmony_cistatic int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
2448e1051a39Sopenharmony_ci{
2449e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
2450e1051a39Sopenharmony_ci    int i;
2451e1051a39Sopenharmony_ci
2452e1051a39Sopenharmony_ci    /* Test that we can resume with all the tickets we got given */
2453e1051a39Sopenharmony_ci    for (i = 0; i < idx * 2; i++) {
2454e1051a39Sopenharmony_ci        new_called = 0;
2455e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2456e1051a39Sopenharmony_ci                                              &clientssl, NULL, NULL))
2457e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
2458e1051a39Sopenharmony_ci            goto end;
2459e1051a39Sopenharmony_ci
2460e1051a39Sopenharmony_ci        SSL_set_post_handshake_auth(clientssl, 1);
2461e1051a39Sopenharmony_ci
2462e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2463e1051a39Sopenharmony_ci                                                    SSL_ERROR_NONE)))
2464e1051a39Sopenharmony_ci            goto end;
2465e1051a39Sopenharmony_ci
2466e1051a39Sopenharmony_ci        /*
2467e1051a39Sopenharmony_ci         * Following a successful resumption we only get 1 ticket. After a
2468e1051a39Sopenharmony_ci         * failed one we should get idx tickets.
2469e1051a39Sopenharmony_ci         */
2470e1051a39Sopenharmony_ci        if (succ) {
2471e1051a39Sopenharmony_ci            if (!TEST_true(SSL_session_reused(clientssl))
2472e1051a39Sopenharmony_ci                    || !TEST_int_eq(new_called, 1))
2473e1051a39Sopenharmony_ci                goto end;
2474e1051a39Sopenharmony_ci        } else {
2475e1051a39Sopenharmony_ci            if (!TEST_false(SSL_session_reused(clientssl))
2476e1051a39Sopenharmony_ci                    || !TEST_int_eq(new_called, idx))
2477e1051a39Sopenharmony_ci                goto end;
2478e1051a39Sopenharmony_ci        }
2479e1051a39Sopenharmony_ci
2480e1051a39Sopenharmony_ci        new_called = 0;
2481e1051a39Sopenharmony_ci        /* After a post-handshake authentication we should get 1 new ticket */
2482e1051a39Sopenharmony_ci        if (succ
2483e1051a39Sopenharmony_ci                && (!post_handshake_verify(serverssl, clientssl)
2484e1051a39Sopenharmony_ci                    || !TEST_int_eq(new_called, 1)))
2485e1051a39Sopenharmony_ci            goto end;
2486e1051a39Sopenharmony_ci
2487e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
2488e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
2489e1051a39Sopenharmony_ci        SSL_free(serverssl);
2490e1051a39Sopenharmony_ci        SSL_free(clientssl);
2491e1051a39Sopenharmony_ci        serverssl = clientssl = NULL;
2492e1051a39Sopenharmony_ci        SSL_SESSION_free(sesscache[i]);
2493e1051a39Sopenharmony_ci        sesscache[i] = NULL;
2494e1051a39Sopenharmony_ci    }
2495e1051a39Sopenharmony_ci
2496e1051a39Sopenharmony_ci    return 1;
2497e1051a39Sopenharmony_ci
2498e1051a39Sopenharmony_ci end:
2499e1051a39Sopenharmony_ci    SSL_free(clientssl);
2500e1051a39Sopenharmony_ci    SSL_free(serverssl);
2501e1051a39Sopenharmony_ci    return 0;
2502e1051a39Sopenharmony_ci}
2503e1051a39Sopenharmony_ci
2504e1051a39Sopenharmony_cistatic int test_tickets(int stateful, int idx)
2505e1051a39Sopenharmony_ci{
2506e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
2507e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
2508e1051a39Sopenharmony_ci    int testresult = 0;
2509e1051a39Sopenharmony_ci    size_t j;
2510e1051a39Sopenharmony_ci
2511e1051a39Sopenharmony_ci    /* idx is the test number, but also the number of tickets we want */
2512e1051a39Sopenharmony_ci
2513e1051a39Sopenharmony_ci    new_called = 0;
2514e1051a39Sopenharmony_ci    do_cache = 1;
2515e1051a39Sopenharmony_ci
2516e1051a39Sopenharmony_ci    if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2517e1051a39Sopenharmony_ci        goto end;
2518e1051a39Sopenharmony_ci
2519e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2520e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL)))
2521e1051a39Sopenharmony_ci        goto end;
2522e1051a39Sopenharmony_ci
2523e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2524e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2525e1051a39Sopenharmony_ci               /* Check we got the number of tickets we were expecting */
2526e1051a39Sopenharmony_ci            || !TEST_int_eq(idx, new_called))
2527e1051a39Sopenharmony_ci        goto end;
2528e1051a39Sopenharmony_ci
2529e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
2530e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
2531e1051a39Sopenharmony_ci    SSL_free(serverssl);
2532e1051a39Sopenharmony_ci    SSL_free(clientssl);
2533e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2534e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2535e1051a39Sopenharmony_ci    clientssl = serverssl = NULL;
2536e1051a39Sopenharmony_ci    sctx = cctx = NULL;
2537e1051a39Sopenharmony_ci
2538e1051a39Sopenharmony_ci    /*
2539e1051a39Sopenharmony_ci     * Now we try to resume with the tickets we previously created. The
2540e1051a39Sopenharmony_ci     * resumption attempt is expected to fail (because we're now using a new
2541e1051a39Sopenharmony_ci     * SSL_CTX). We should see idx number of tickets issued again.
2542e1051a39Sopenharmony_ci     */
2543e1051a39Sopenharmony_ci
2544e1051a39Sopenharmony_ci    /* Stop caching sessions - just count them */
2545e1051a39Sopenharmony_ci    do_cache = 0;
2546e1051a39Sopenharmony_ci
2547e1051a39Sopenharmony_ci    if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2548e1051a39Sopenharmony_ci        goto end;
2549e1051a39Sopenharmony_ci
2550e1051a39Sopenharmony_ci    if (!check_resumption(idx, sctx, cctx, 0))
2551e1051a39Sopenharmony_ci        goto end;
2552e1051a39Sopenharmony_ci
2553e1051a39Sopenharmony_ci    /* Start again with caching sessions */
2554e1051a39Sopenharmony_ci    new_called = 0;
2555e1051a39Sopenharmony_ci    do_cache = 1;
2556e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2557e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2558e1051a39Sopenharmony_ci    sctx = cctx = NULL;
2559e1051a39Sopenharmony_ci
2560e1051a39Sopenharmony_ci    if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
2561e1051a39Sopenharmony_ci        goto end;
2562e1051a39Sopenharmony_ci
2563e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2564e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL)))
2565e1051a39Sopenharmony_ci        goto end;
2566e1051a39Sopenharmony_ci
2567e1051a39Sopenharmony_ci    SSL_set_post_handshake_auth(clientssl, 1);
2568e1051a39Sopenharmony_ci
2569e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2570e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2571e1051a39Sopenharmony_ci               /* Check we got the number of tickets we were expecting */
2572e1051a39Sopenharmony_ci            || !TEST_int_eq(idx, new_called))
2573e1051a39Sopenharmony_ci        goto end;
2574e1051a39Sopenharmony_ci
2575e1051a39Sopenharmony_ci    /* After a post-handshake authentication we should get new tickets issued */
2576e1051a39Sopenharmony_ci    if (!post_handshake_verify(serverssl, clientssl)
2577e1051a39Sopenharmony_ci            || !TEST_int_eq(idx * 2, new_called))
2578e1051a39Sopenharmony_ci        goto end;
2579e1051a39Sopenharmony_ci
2580e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
2581e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
2582e1051a39Sopenharmony_ci    SSL_free(serverssl);
2583e1051a39Sopenharmony_ci    SSL_free(clientssl);
2584e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
2585e1051a39Sopenharmony_ci
2586e1051a39Sopenharmony_ci    /* Stop caching sessions - just count them */
2587e1051a39Sopenharmony_ci    do_cache = 0;
2588e1051a39Sopenharmony_ci
2589e1051a39Sopenharmony_ci    /*
2590e1051a39Sopenharmony_ci     * Check we can resume with all the tickets we created. This time around the
2591e1051a39Sopenharmony_ci     * resumptions should all be successful.
2592e1051a39Sopenharmony_ci     */
2593e1051a39Sopenharmony_ci    if (!check_resumption(idx, sctx, cctx, 1))
2594e1051a39Sopenharmony_ci        goto end;
2595e1051a39Sopenharmony_ci
2596e1051a39Sopenharmony_ci    testresult = 1;
2597e1051a39Sopenharmony_ci
2598e1051a39Sopenharmony_ci end:
2599e1051a39Sopenharmony_ci    SSL_free(serverssl);
2600e1051a39Sopenharmony_ci    SSL_free(clientssl);
2601e1051a39Sopenharmony_ci    for (j = 0; j < OSSL_NELEM(sesscache); j++) {
2602e1051a39Sopenharmony_ci        SSL_SESSION_free(sesscache[j]);
2603e1051a39Sopenharmony_ci        sesscache[j] = NULL;
2604e1051a39Sopenharmony_ci    }
2605e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2606e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2607e1051a39Sopenharmony_ci
2608e1051a39Sopenharmony_ci    return testresult;
2609e1051a39Sopenharmony_ci}
2610e1051a39Sopenharmony_ci
2611e1051a39Sopenharmony_cistatic int test_stateless_tickets(int idx)
2612e1051a39Sopenharmony_ci{
2613e1051a39Sopenharmony_ci    return test_tickets(0, idx);
2614e1051a39Sopenharmony_ci}
2615e1051a39Sopenharmony_ci
2616e1051a39Sopenharmony_cistatic int test_stateful_tickets(int idx)
2617e1051a39Sopenharmony_ci{
2618e1051a39Sopenharmony_ci    return test_tickets(1, idx);
2619e1051a39Sopenharmony_ci}
2620e1051a39Sopenharmony_ci
2621e1051a39Sopenharmony_cistatic int test_psk_tickets(void)
2622e1051a39Sopenharmony_ci{
2623e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
2624e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
2625e1051a39Sopenharmony_ci    int testresult = 0;
2626e1051a39Sopenharmony_ci    int sess_id_ctx = 1;
2627e1051a39Sopenharmony_ci
2628e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2629e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
2630e1051a39Sopenharmony_ci                                       &sctx, &cctx, NULL, NULL))
2631e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_session_id_context(sctx,
2632e1051a39Sopenharmony_ci                                                         (void *)&sess_id_ctx,
2633e1051a39Sopenharmony_ci                                                         sizeof(sess_id_ctx))))
2634e1051a39Sopenharmony_ci        goto end;
2635e1051a39Sopenharmony_ci
2636e1051a39Sopenharmony_ci    SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
2637e1051a39Sopenharmony_ci                                         | SSL_SESS_CACHE_NO_INTERNAL_STORE);
2638e1051a39Sopenharmony_ci    SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
2639e1051a39Sopenharmony_ci    SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
2640e1051a39Sopenharmony_ci    SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2641e1051a39Sopenharmony_ci    use_session_cb_cnt = 0;
2642e1051a39Sopenharmony_ci    find_session_cb_cnt = 0;
2643e1051a39Sopenharmony_ci    srvid = pskid;
2644e1051a39Sopenharmony_ci    new_called = 0;
2645e1051a39Sopenharmony_ci
2646e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2647e1051a39Sopenharmony_ci                                      NULL, NULL)))
2648e1051a39Sopenharmony_ci        goto end;
2649e1051a39Sopenharmony_ci    clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH);
2650e1051a39Sopenharmony_ci    if (!TEST_ptr(clientpsk))
2651e1051a39Sopenharmony_ci        goto end;
2652e1051a39Sopenharmony_ci    SSL_SESSION_up_ref(clientpsk);
2653e1051a39Sopenharmony_ci
2654e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2655e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2656e1051a39Sopenharmony_ci            || !TEST_int_eq(1, find_session_cb_cnt)
2657e1051a39Sopenharmony_ci            || !TEST_int_eq(1, use_session_cb_cnt)
2658e1051a39Sopenharmony_ci               /* We should always get 1 ticket when using external PSK */
2659e1051a39Sopenharmony_ci            || !TEST_int_eq(1, new_called))
2660e1051a39Sopenharmony_ci        goto end;
2661e1051a39Sopenharmony_ci
2662e1051a39Sopenharmony_ci    testresult = 1;
2663e1051a39Sopenharmony_ci
2664e1051a39Sopenharmony_ci end:
2665e1051a39Sopenharmony_ci    SSL_free(serverssl);
2666e1051a39Sopenharmony_ci    SSL_free(clientssl);
2667e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2668e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2669e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
2670e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
2671e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
2672e1051a39Sopenharmony_ci
2673e1051a39Sopenharmony_ci    return testresult;
2674e1051a39Sopenharmony_ci}
2675e1051a39Sopenharmony_ci
2676e1051a39Sopenharmony_cistatic int test_extra_tickets(int idx)
2677e1051a39Sopenharmony_ci{
2678e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
2679e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
2680e1051a39Sopenharmony_ci    BIO *bretry = BIO_new(bio_s_always_retry());
2681e1051a39Sopenharmony_ci    BIO *tmp = NULL;
2682e1051a39Sopenharmony_ci    int testresult = 0;
2683e1051a39Sopenharmony_ci    int stateful = 0;
2684e1051a39Sopenharmony_ci    size_t nbytes;
2685e1051a39Sopenharmony_ci    unsigned char c, buf[1];
2686e1051a39Sopenharmony_ci
2687e1051a39Sopenharmony_ci    new_called = 0;
2688e1051a39Sopenharmony_ci    do_cache = 1;
2689e1051a39Sopenharmony_ci
2690e1051a39Sopenharmony_ci    if (idx >= 3) {
2691e1051a39Sopenharmony_ci        idx -= 3;
2692e1051a39Sopenharmony_ci        stateful = 1;
2693e1051a39Sopenharmony_ci    }
2694e1051a39Sopenharmony_ci
2695e1051a39Sopenharmony_ci    if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
2696e1051a39Sopenharmony_ci        goto end;
2697e1051a39Sopenharmony_ci    SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
2698e1051a39Sopenharmony_ci    /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
2699e1051a39Sopenharmony_ci    SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
2700e1051a39Sopenharmony_ci
2701e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
2702e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL)))
2703e1051a39Sopenharmony_ci        goto end;
2704e1051a39Sopenharmony_ci
2705e1051a39Sopenharmony_ci    /*
2706e1051a39Sopenharmony_ci     * Note that we have new_session_cb on both sctx and cctx, so new_called is
2707e1051a39Sopenharmony_ci     * incremented by both client and server.
2708e1051a39Sopenharmony_ci     */
2709e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
2710e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
2711e1051a39Sopenharmony_ci               /* Check we got the number of tickets we were expecting */
2712e1051a39Sopenharmony_ci            || !TEST_int_eq(idx * 2, new_called)
2713e1051a39Sopenharmony_ci            || !TEST_true(SSL_new_session_ticket(serverssl))
2714e1051a39Sopenharmony_ci            || !TEST_true(SSL_new_session_ticket(serverssl))
2715e1051a39Sopenharmony_ci            || !TEST_int_eq(idx * 2, new_called))
2716e1051a39Sopenharmony_ci        goto end;
2717e1051a39Sopenharmony_ci
2718e1051a39Sopenharmony_ci    /* Now try a (real) write to actually send the tickets */
2719e1051a39Sopenharmony_ci    c = '1';
2720e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2721e1051a39Sopenharmony_ci            || !TEST_size_t_eq(1, nbytes)
2722e1051a39Sopenharmony_ci            || !TEST_int_eq(idx * 2 + 2, new_called)
2723e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2724e1051a39Sopenharmony_ci            || !TEST_int_eq(idx * 2 + 4, new_called)
2725e1051a39Sopenharmony_ci            || !TEST_int_eq(sizeof(buf), nbytes)
2726e1051a39Sopenharmony_ci            || !TEST_int_eq(c, buf[0])
2727e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2728e1051a39Sopenharmony_ci        goto end;
2729e1051a39Sopenharmony_ci
2730e1051a39Sopenharmony_ci    /* Try with only requesting one new ticket, too */
2731e1051a39Sopenharmony_ci    c = '2';
2732e1051a39Sopenharmony_ci    new_called = 0;
2733e1051a39Sopenharmony_ci    if (!TEST_true(SSL_new_session_ticket(serverssl))
2734e1051a39Sopenharmony_ci            || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
2735e1051a39Sopenharmony_ci            || !TEST_size_t_eq(sizeof(c), nbytes)
2736e1051a39Sopenharmony_ci            || !TEST_int_eq(1, new_called)
2737e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2738e1051a39Sopenharmony_ci            || !TEST_int_eq(2, new_called)
2739e1051a39Sopenharmony_ci            || !TEST_size_t_eq(sizeof(buf), nbytes)
2740e1051a39Sopenharmony_ci            || !TEST_int_eq(c, buf[0]))
2741e1051a39Sopenharmony_ci        goto end;
2742e1051a39Sopenharmony_ci
2743e1051a39Sopenharmony_ci    /* Do it again but use dummy writes to drive the ticket generation */
2744e1051a39Sopenharmony_ci    c = '3';
2745e1051a39Sopenharmony_ci    new_called = 0;
2746e1051a39Sopenharmony_ci    if (!TEST_true(SSL_new_session_ticket(serverssl))
2747e1051a39Sopenharmony_ci            || !TEST_true(SSL_new_session_ticket(serverssl))
2748e1051a39Sopenharmony_ci            || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
2749e1051a39Sopenharmony_ci            || !TEST_size_t_eq(0, nbytes)
2750e1051a39Sopenharmony_ci            || !TEST_int_eq(2, new_called)
2751e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2752e1051a39Sopenharmony_ci            || !TEST_int_eq(4, new_called))
2753e1051a39Sopenharmony_ci        goto end;
2754e1051a39Sopenharmony_ci
2755e1051a39Sopenharmony_ci    /* Once more, but with SSL_do_handshake() to drive the ticket generation */
2756e1051a39Sopenharmony_ci    c = '4';
2757e1051a39Sopenharmony_ci    new_called = 0;
2758e1051a39Sopenharmony_ci    if (!TEST_true(SSL_new_session_ticket(serverssl))
2759e1051a39Sopenharmony_ci            || !TEST_true(SSL_new_session_ticket(serverssl))
2760e1051a39Sopenharmony_ci            || !TEST_true(SSL_do_handshake(serverssl))
2761e1051a39Sopenharmony_ci            || !TEST_int_eq(2, new_called)
2762e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2763e1051a39Sopenharmony_ci            || !TEST_int_eq(4, new_called))
2764e1051a39Sopenharmony_ci        goto end;
2765e1051a39Sopenharmony_ci
2766e1051a39Sopenharmony_ci    /*
2767e1051a39Sopenharmony_ci     * Use the always-retry BIO to exercise the logic that forces ticket
2768e1051a39Sopenharmony_ci     * generation to wait until a record boundary.
2769e1051a39Sopenharmony_ci     */
2770e1051a39Sopenharmony_ci    c = '5';
2771e1051a39Sopenharmony_ci    new_called = 0;
2772e1051a39Sopenharmony_ci    tmp = SSL_get_wbio(serverssl);
2773e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
2774e1051a39Sopenharmony_ci        tmp = NULL;
2775e1051a39Sopenharmony_ci        goto end;
2776e1051a39Sopenharmony_ci    }
2777e1051a39Sopenharmony_ci    SSL_set0_wbio(serverssl, bretry);
2778e1051a39Sopenharmony_ci    bretry = NULL;
2779e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
2780e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
2781e1051a39Sopenharmony_ci            || !TEST_size_t_eq(nbytes, 0))
2782e1051a39Sopenharmony_ci        goto end;
2783e1051a39Sopenharmony_ci    /* Restore a BIO that will let the write succeed */
2784e1051a39Sopenharmony_ci    SSL_set0_wbio(serverssl, tmp);
2785e1051a39Sopenharmony_ci    tmp = NULL;
2786e1051a39Sopenharmony_ci    /*
2787e1051a39Sopenharmony_ci     * These calls should just queue the request and not send anything
2788e1051a39Sopenharmony_ci     * even if we explicitly try to hit the state machine.
2789e1051a39Sopenharmony_ci     */
2790e1051a39Sopenharmony_ci    if (!TEST_true(SSL_new_session_ticket(serverssl))
2791e1051a39Sopenharmony_ci            || !TEST_true(SSL_new_session_ticket(serverssl))
2792e1051a39Sopenharmony_ci            || !TEST_int_eq(0, new_called)
2793e1051a39Sopenharmony_ci            || !TEST_true(SSL_do_handshake(serverssl))
2794e1051a39Sopenharmony_ci            || !TEST_int_eq(0, new_called))
2795e1051a39Sopenharmony_ci        goto end;
2796e1051a39Sopenharmony_ci    /* Re-do the write; still no tickets sent */
2797e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2798e1051a39Sopenharmony_ci            || !TEST_size_t_eq(1, nbytes)
2799e1051a39Sopenharmony_ci            || !TEST_int_eq(0, new_called)
2800e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2801e1051a39Sopenharmony_ci            || !TEST_int_eq(0, new_called)
2802e1051a39Sopenharmony_ci            || !TEST_int_eq(sizeof(buf), nbytes)
2803e1051a39Sopenharmony_ci            || !TEST_int_eq(c, buf[0])
2804e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2805e1051a39Sopenharmony_ci        goto end;
2806e1051a39Sopenharmony_ci    /* Even trying to hit the state machine now will still not send tickets */
2807e1051a39Sopenharmony_ci    if (!TEST_true(SSL_do_handshake(serverssl))
2808e1051a39Sopenharmony_ci            || !TEST_int_eq(0, new_called))
2809e1051a39Sopenharmony_ci        goto end;
2810e1051a39Sopenharmony_ci    /* Now the *next* write should send the tickets */
2811e1051a39Sopenharmony_ci    c = '6';
2812e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
2813e1051a39Sopenharmony_ci            || !TEST_size_t_eq(1, nbytes)
2814e1051a39Sopenharmony_ci            || !TEST_int_eq(2, new_called)
2815e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
2816e1051a39Sopenharmony_ci            || !TEST_int_eq(4, new_called)
2817e1051a39Sopenharmony_ci            || !TEST_int_eq(sizeof(buf), nbytes)
2818e1051a39Sopenharmony_ci            || !TEST_int_eq(c, buf[0])
2819e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
2820e1051a39Sopenharmony_ci        goto end;
2821e1051a39Sopenharmony_ci
2822e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
2823e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
2824e1051a39Sopenharmony_ci    testresult = 1;
2825e1051a39Sopenharmony_ci
2826e1051a39Sopenharmony_ci end:
2827e1051a39Sopenharmony_ci    BIO_free(bretry);
2828e1051a39Sopenharmony_ci    BIO_free(tmp);
2829e1051a39Sopenharmony_ci    SSL_free(serverssl);
2830e1051a39Sopenharmony_ci    SSL_free(clientssl);
2831e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
2832e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
2833e1051a39Sopenharmony_ci    clientssl = serverssl = NULL;
2834e1051a39Sopenharmony_ci    sctx = cctx = NULL;
2835e1051a39Sopenharmony_ci    return testresult;
2836e1051a39Sopenharmony_ci}
2837e1051a39Sopenharmony_ci#endif
2838e1051a39Sopenharmony_ci
2839e1051a39Sopenharmony_ci#define USE_NULL            0
2840e1051a39Sopenharmony_ci#define USE_BIO_1           1
2841e1051a39Sopenharmony_ci#define USE_BIO_2           2
2842e1051a39Sopenharmony_ci#define USE_DEFAULT         3
2843e1051a39Sopenharmony_ci
2844e1051a39Sopenharmony_ci#define CONNTYPE_CONNECTION_SUCCESS  0
2845e1051a39Sopenharmony_ci#define CONNTYPE_CONNECTION_FAIL     1
2846e1051a39Sopenharmony_ci#define CONNTYPE_NO_CONNECTION       2
2847e1051a39Sopenharmony_ci
2848e1051a39Sopenharmony_ci#define TOTAL_NO_CONN_SSL_SET_BIO_TESTS         (3 * 3 * 3 * 3)
2849e1051a39Sopenharmony_ci#define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS    (2 * 2)
2850e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
2851e1051a39Sopenharmony_ci# define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS       (2 * 2)
2852e1051a39Sopenharmony_ci#else
2853e1051a39Sopenharmony_ci# define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS       0
2854e1051a39Sopenharmony_ci#endif
2855e1051a39Sopenharmony_ci
2856e1051a39Sopenharmony_ci#define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
2857e1051a39Sopenharmony_ci                                + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
2858e1051a39Sopenharmony_ci                                + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
2859e1051a39Sopenharmony_ci
2860e1051a39Sopenharmony_cistatic void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
2861e1051a39Sopenharmony_ci{
2862e1051a39Sopenharmony_ci    switch (type) {
2863e1051a39Sopenharmony_ci    case USE_NULL:
2864e1051a39Sopenharmony_ci        *res = NULL;
2865e1051a39Sopenharmony_ci        break;
2866e1051a39Sopenharmony_ci    case USE_BIO_1:
2867e1051a39Sopenharmony_ci        *res = bio1;
2868e1051a39Sopenharmony_ci        break;
2869e1051a39Sopenharmony_ci    case USE_BIO_2:
2870e1051a39Sopenharmony_ci        *res = bio2;
2871e1051a39Sopenharmony_ci        break;
2872e1051a39Sopenharmony_ci    }
2873e1051a39Sopenharmony_ci}
2874e1051a39Sopenharmony_ci
2875e1051a39Sopenharmony_ci
2876e1051a39Sopenharmony_ci/*
2877e1051a39Sopenharmony_ci * Tests calls to SSL_set_bio() under various conditions.
2878e1051a39Sopenharmony_ci *
2879e1051a39Sopenharmony_ci * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
2880e1051a39Sopenharmony_ci * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
2881e1051a39Sopenharmony_ci * then do more tests where we create a successful connection first using our
2882e1051a39Sopenharmony_ci * standard connection setup functions, and then call SSL_set_bio() with
2883e1051a39Sopenharmony_ci * various combinations of valid BIOs or NULL. We then repeat these tests
2884e1051a39Sopenharmony_ci * following a failed connection. In this last case we are looking to check that
2885e1051a39Sopenharmony_ci * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
2886e1051a39Sopenharmony_ci */
2887e1051a39Sopenharmony_cistatic int test_ssl_set_bio(int idx)
2888e1051a39Sopenharmony_ci{
2889e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
2890e1051a39Sopenharmony_ci    BIO *bio1 = NULL;
2891e1051a39Sopenharmony_ci    BIO *bio2 = NULL;
2892e1051a39Sopenharmony_ci    BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
2893e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
2894e1051a39Sopenharmony_ci    int initrbio, initwbio, newrbio, newwbio, conntype;
2895e1051a39Sopenharmony_ci    int testresult = 0;
2896e1051a39Sopenharmony_ci
2897e1051a39Sopenharmony_ci    if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
2898e1051a39Sopenharmony_ci        initrbio = idx % 3;
2899e1051a39Sopenharmony_ci        idx /= 3;
2900e1051a39Sopenharmony_ci        initwbio = idx % 3;
2901e1051a39Sopenharmony_ci        idx /= 3;
2902e1051a39Sopenharmony_ci        newrbio = idx % 3;
2903e1051a39Sopenharmony_ci        idx /= 3;
2904e1051a39Sopenharmony_ci        newwbio = idx % 3;
2905e1051a39Sopenharmony_ci        conntype = CONNTYPE_NO_CONNECTION;
2906e1051a39Sopenharmony_ci    } else {
2907e1051a39Sopenharmony_ci        idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
2908e1051a39Sopenharmony_ci        initrbio = initwbio = USE_DEFAULT;
2909e1051a39Sopenharmony_ci        newrbio = idx % 2;
2910e1051a39Sopenharmony_ci        idx /= 2;
2911e1051a39Sopenharmony_ci        newwbio = idx % 2;
2912e1051a39Sopenharmony_ci        idx /= 2;
2913e1051a39Sopenharmony_ci        conntype = idx % 2;
2914e1051a39Sopenharmony_ci    }
2915e1051a39Sopenharmony_ci
2916e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
2917e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
2918e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
2919e1051a39Sopenharmony_ci        goto end;
2920e1051a39Sopenharmony_ci
2921e1051a39Sopenharmony_ci    if (conntype == CONNTYPE_CONNECTION_FAIL) {
2922e1051a39Sopenharmony_ci        /*
2923e1051a39Sopenharmony_ci         * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
2924e1051a39Sopenharmony_ci         * because we reduced the number of tests in the definition of
2925e1051a39Sopenharmony_ci         * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
2926e1051a39Sopenharmony_ci         * mismatched protocol versions we will force a connection failure.
2927e1051a39Sopenharmony_ci         */
2928e1051a39Sopenharmony_ci        SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
2929e1051a39Sopenharmony_ci        SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
2930e1051a39Sopenharmony_ci    }
2931e1051a39Sopenharmony_ci
2932e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
2933e1051a39Sopenharmony_ci                                      NULL, NULL)))
2934e1051a39Sopenharmony_ci        goto end;
2935e1051a39Sopenharmony_ci
2936e1051a39Sopenharmony_ci    if (initrbio == USE_BIO_1
2937e1051a39Sopenharmony_ci            || initwbio == USE_BIO_1
2938e1051a39Sopenharmony_ci            || newrbio == USE_BIO_1
2939e1051a39Sopenharmony_ci            || newwbio == USE_BIO_1) {
2940e1051a39Sopenharmony_ci        if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
2941e1051a39Sopenharmony_ci            goto end;
2942e1051a39Sopenharmony_ci    }
2943e1051a39Sopenharmony_ci
2944e1051a39Sopenharmony_ci    if (initrbio == USE_BIO_2
2945e1051a39Sopenharmony_ci            || initwbio == USE_BIO_2
2946e1051a39Sopenharmony_ci            || newrbio == USE_BIO_2
2947e1051a39Sopenharmony_ci            || newwbio == USE_BIO_2) {
2948e1051a39Sopenharmony_ci        if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
2949e1051a39Sopenharmony_ci            goto end;
2950e1051a39Sopenharmony_ci    }
2951e1051a39Sopenharmony_ci
2952e1051a39Sopenharmony_ci    if (initrbio != USE_DEFAULT) {
2953e1051a39Sopenharmony_ci        setupbio(&irbio, bio1, bio2, initrbio);
2954e1051a39Sopenharmony_ci        setupbio(&iwbio, bio1, bio2, initwbio);
2955e1051a39Sopenharmony_ci        SSL_set_bio(clientssl, irbio, iwbio);
2956e1051a39Sopenharmony_ci
2957e1051a39Sopenharmony_ci        /*
2958e1051a39Sopenharmony_ci         * We want to maintain our own refs to these BIO, so do an up ref for
2959e1051a39Sopenharmony_ci         * each BIO that will have ownership transferred in the SSL_set_bio()
2960e1051a39Sopenharmony_ci         * call
2961e1051a39Sopenharmony_ci         */
2962e1051a39Sopenharmony_ci        if (irbio != NULL)
2963e1051a39Sopenharmony_ci            BIO_up_ref(irbio);
2964e1051a39Sopenharmony_ci        if (iwbio != NULL && iwbio != irbio)
2965e1051a39Sopenharmony_ci            BIO_up_ref(iwbio);
2966e1051a39Sopenharmony_ci    }
2967e1051a39Sopenharmony_ci
2968e1051a39Sopenharmony_ci    if (conntype != CONNTYPE_NO_CONNECTION
2969e1051a39Sopenharmony_ci            && !TEST_true(create_ssl_connection(serverssl, clientssl,
2970e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)
2971e1051a39Sopenharmony_ci                          == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
2972e1051a39Sopenharmony_ci        goto end;
2973e1051a39Sopenharmony_ci
2974e1051a39Sopenharmony_ci    setupbio(&nrbio, bio1, bio2, newrbio);
2975e1051a39Sopenharmony_ci    setupbio(&nwbio, bio1, bio2, newwbio);
2976e1051a39Sopenharmony_ci
2977e1051a39Sopenharmony_ci    /*
2978e1051a39Sopenharmony_ci     * We will (maybe) transfer ownership again so do more up refs.
2979e1051a39Sopenharmony_ci     * SSL_set_bio() has some really complicated ownership rules where BIOs have
2980e1051a39Sopenharmony_ci     * already been set!
2981e1051a39Sopenharmony_ci     */
2982e1051a39Sopenharmony_ci    if (nrbio != NULL
2983e1051a39Sopenharmony_ci            && nrbio != irbio
2984e1051a39Sopenharmony_ci            && (nwbio != iwbio || nrbio != nwbio))
2985e1051a39Sopenharmony_ci        BIO_up_ref(nrbio);
2986e1051a39Sopenharmony_ci    if (nwbio != NULL
2987e1051a39Sopenharmony_ci            && nwbio != nrbio
2988e1051a39Sopenharmony_ci            && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
2989e1051a39Sopenharmony_ci        BIO_up_ref(nwbio);
2990e1051a39Sopenharmony_ci
2991e1051a39Sopenharmony_ci    SSL_set_bio(clientssl, nrbio, nwbio);
2992e1051a39Sopenharmony_ci
2993e1051a39Sopenharmony_ci    testresult = 1;
2994e1051a39Sopenharmony_ci
2995e1051a39Sopenharmony_ci end:
2996e1051a39Sopenharmony_ci    BIO_free(bio1);
2997e1051a39Sopenharmony_ci    BIO_free(bio2);
2998e1051a39Sopenharmony_ci
2999e1051a39Sopenharmony_ci    /*
3000e1051a39Sopenharmony_ci     * This test is checking that the ref counting for SSL_set_bio is correct.
3001e1051a39Sopenharmony_ci     * If we get here and we did too many frees then we will fail in the above
3002e1051a39Sopenharmony_ci     * functions.
3003e1051a39Sopenharmony_ci     */
3004e1051a39Sopenharmony_ci    SSL_free(serverssl);
3005e1051a39Sopenharmony_ci    SSL_free(clientssl);
3006e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
3007e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
3008e1051a39Sopenharmony_ci    return testresult;
3009e1051a39Sopenharmony_ci}
3010e1051a39Sopenharmony_ci
3011e1051a39Sopenharmony_citypedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
3012e1051a39Sopenharmony_ci
3013e1051a39Sopenharmony_cistatic int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
3014e1051a39Sopenharmony_ci{
3015e1051a39Sopenharmony_ci    BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
3016e1051a39Sopenharmony_ci    SSL_CTX *ctx;
3017e1051a39Sopenharmony_ci    SSL *ssl = NULL;
3018e1051a39Sopenharmony_ci    int testresult = 0;
3019e1051a39Sopenharmony_ci
3020e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
3021e1051a39Sopenharmony_ci            || !TEST_ptr(ssl = SSL_new(ctx))
3022e1051a39Sopenharmony_ci            || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
3023e1051a39Sopenharmony_ci            || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
3024e1051a39Sopenharmony_ci        goto end;
3025e1051a39Sopenharmony_ci
3026e1051a39Sopenharmony_ci    BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
3027e1051a39Sopenharmony_ci
3028e1051a39Sopenharmony_ci    /*
3029e1051a39Sopenharmony_ci     * If anything goes wrong here then we could leak memory.
3030e1051a39Sopenharmony_ci     */
3031e1051a39Sopenharmony_ci    BIO_push(sslbio, membio1);
3032e1051a39Sopenharmony_ci
3033e1051a39Sopenharmony_ci    /* Verify changing the rbio/wbio directly does not cause leaks */
3034e1051a39Sopenharmony_ci    if (change_bio != NO_BIO_CHANGE) {
3035e1051a39Sopenharmony_ci        if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
3036e1051a39Sopenharmony_ci            ssl = NULL;
3037e1051a39Sopenharmony_ci            goto end;
3038e1051a39Sopenharmony_ci        }
3039e1051a39Sopenharmony_ci        if (change_bio == CHANGE_RBIO)
3040e1051a39Sopenharmony_ci            SSL_set0_rbio(ssl, membio2);
3041e1051a39Sopenharmony_ci        else
3042e1051a39Sopenharmony_ci            SSL_set0_wbio(ssl, membio2);
3043e1051a39Sopenharmony_ci    }
3044e1051a39Sopenharmony_ci    ssl = NULL;
3045e1051a39Sopenharmony_ci
3046e1051a39Sopenharmony_ci    if (pop_ssl)
3047e1051a39Sopenharmony_ci        BIO_pop(sslbio);
3048e1051a39Sopenharmony_ci    else
3049e1051a39Sopenharmony_ci        BIO_pop(membio1);
3050e1051a39Sopenharmony_ci
3051e1051a39Sopenharmony_ci    testresult = 1;
3052e1051a39Sopenharmony_ci end:
3053e1051a39Sopenharmony_ci    BIO_free(membio1);
3054e1051a39Sopenharmony_ci    BIO_free(sslbio);
3055e1051a39Sopenharmony_ci    SSL_free(ssl);
3056e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
3057e1051a39Sopenharmony_ci
3058e1051a39Sopenharmony_ci    return testresult;
3059e1051a39Sopenharmony_ci}
3060e1051a39Sopenharmony_ci
3061e1051a39Sopenharmony_cistatic int test_ssl_bio_pop_next_bio(void)
3062e1051a39Sopenharmony_ci{
3063e1051a39Sopenharmony_ci    return execute_test_ssl_bio(0, NO_BIO_CHANGE);
3064e1051a39Sopenharmony_ci}
3065e1051a39Sopenharmony_ci
3066e1051a39Sopenharmony_cistatic int test_ssl_bio_pop_ssl_bio(void)
3067e1051a39Sopenharmony_ci{
3068e1051a39Sopenharmony_ci    return execute_test_ssl_bio(1, NO_BIO_CHANGE);
3069e1051a39Sopenharmony_ci}
3070e1051a39Sopenharmony_ci
3071e1051a39Sopenharmony_cistatic int test_ssl_bio_change_rbio(void)
3072e1051a39Sopenharmony_ci{
3073e1051a39Sopenharmony_ci    return execute_test_ssl_bio(0, CHANGE_RBIO);
3074e1051a39Sopenharmony_ci}
3075e1051a39Sopenharmony_ci
3076e1051a39Sopenharmony_cistatic int test_ssl_bio_change_wbio(void)
3077e1051a39Sopenharmony_ci{
3078e1051a39Sopenharmony_ci    return execute_test_ssl_bio(0, CHANGE_WBIO);
3079e1051a39Sopenharmony_ci}
3080e1051a39Sopenharmony_ci
3081e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
3082e1051a39Sopenharmony_citypedef struct {
3083e1051a39Sopenharmony_ci    /* The list of sig algs */
3084e1051a39Sopenharmony_ci    const int *list;
3085e1051a39Sopenharmony_ci    /* The length of the list */
3086e1051a39Sopenharmony_ci    size_t listlen;
3087e1051a39Sopenharmony_ci    /* A sigalgs list in string format */
3088e1051a39Sopenharmony_ci    const char *liststr;
3089e1051a39Sopenharmony_ci    /* Whether setting the list should succeed */
3090e1051a39Sopenharmony_ci    int valid;
3091e1051a39Sopenharmony_ci    /* Whether creating a connection with the list should succeed */
3092e1051a39Sopenharmony_ci    int connsuccess;
3093e1051a39Sopenharmony_ci} sigalgs_list;
3094e1051a39Sopenharmony_ci
3095e1051a39Sopenharmony_cistatic const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
3096e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
3097e1051a39Sopenharmony_cistatic const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
3098e1051a39Sopenharmony_cistatic const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
3099e1051a39Sopenharmony_ci# endif
3100e1051a39Sopenharmony_cistatic const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
3101e1051a39Sopenharmony_cistatic const int invalidlist2[] = {NID_sha256, NID_undef};
3102e1051a39Sopenharmony_cistatic const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
3103e1051a39Sopenharmony_cistatic const int invalidlist4[] = {NID_sha256};
3104e1051a39Sopenharmony_cistatic const sigalgs_list testsigalgs[] = {
3105e1051a39Sopenharmony_ci    {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
3106e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
3107e1051a39Sopenharmony_ci    {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
3108e1051a39Sopenharmony_ci    {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
3109e1051a39Sopenharmony_ci# endif
3110e1051a39Sopenharmony_ci    {NULL, 0, "RSA+SHA256", 1, 1},
3111e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
3112e1051a39Sopenharmony_ci    {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
3113e1051a39Sopenharmony_ci    {NULL, 0, "ECDSA+SHA512", 1, 0},
3114e1051a39Sopenharmony_ci# endif
3115e1051a39Sopenharmony_ci    {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
3116e1051a39Sopenharmony_ci    {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
3117e1051a39Sopenharmony_ci    {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
3118e1051a39Sopenharmony_ci    {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
3119e1051a39Sopenharmony_ci    {NULL, 0, "RSA", 0, 0},
3120e1051a39Sopenharmony_ci    {NULL, 0, "SHA256", 0, 0},
3121e1051a39Sopenharmony_ci    {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
3122e1051a39Sopenharmony_ci    {NULL, 0, "Invalid", 0, 0}
3123e1051a39Sopenharmony_ci};
3124e1051a39Sopenharmony_ci
3125e1051a39Sopenharmony_cistatic int test_set_sigalgs(int idx)
3126e1051a39Sopenharmony_ci{
3127e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
3128e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
3129e1051a39Sopenharmony_ci    int testresult = 0;
3130e1051a39Sopenharmony_ci    const sigalgs_list *curr;
3131e1051a39Sopenharmony_ci    int testctx;
3132e1051a39Sopenharmony_ci
3133e1051a39Sopenharmony_ci    /* Should never happen */
3134e1051a39Sopenharmony_ci    if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
3135e1051a39Sopenharmony_ci        return 0;
3136e1051a39Sopenharmony_ci
3137e1051a39Sopenharmony_ci    testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
3138e1051a39Sopenharmony_ci    curr = testctx ? &testsigalgs[idx]
3139e1051a39Sopenharmony_ci                   : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
3140e1051a39Sopenharmony_ci
3141e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3142e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
3143e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
3144e1051a39Sopenharmony_ci        return 0;
3145e1051a39Sopenharmony_ci
3146e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
3147e1051a39Sopenharmony_ci
3148e1051a39Sopenharmony_ci    if (testctx) {
3149e1051a39Sopenharmony_ci        int ret;
3150e1051a39Sopenharmony_ci
3151e1051a39Sopenharmony_ci        if (curr->list != NULL)
3152e1051a39Sopenharmony_ci            ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
3153e1051a39Sopenharmony_ci        else
3154e1051a39Sopenharmony_ci            ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
3155e1051a39Sopenharmony_ci
3156e1051a39Sopenharmony_ci        if (!ret) {
3157e1051a39Sopenharmony_ci            if (curr->valid)
3158e1051a39Sopenharmony_ci                TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
3159e1051a39Sopenharmony_ci            else
3160e1051a39Sopenharmony_ci                testresult = 1;
3161e1051a39Sopenharmony_ci            goto end;
3162e1051a39Sopenharmony_ci        }
3163e1051a39Sopenharmony_ci        if (!curr->valid) {
3164e1051a39Sopenharmony_ci            TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
3165e1051a39Sopenharmony_ci            goto end;
3166e1051a39Sopenharmony_ci        }
3167e1051a39Sopenharmony_ci    }
3168e1051a39Sopenharmony_ci
3169e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3170e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
3171e1051a39Sopenharmony_ci        goto end;
3172e1051a39Sopenharmony_ci
3173e1051a39Sopenharmony_ci    if (!testctx) {
3174e1051a39Sopenharmony_ci        int ret;
3175e1051a39Sopenharmony_ci
3176e1051a39Sopenharmony_ci        if (curr->list != NULL)
3177e1051a39Sopenharmony_ci            ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
3178e1051a39Sopenharmony_ci        else
3179e1051a39Sopenharmony_ci            ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
3180e1051a39Sopenharmony_ci        if (!ret) {
3181e1051a39Sopenharmony_ci            if (curr->valid)
3182e1051a39Sopenharmony_ci                TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
3183e1051a39Sopenharmony_ci            else
3184e1051a39Sopenharmony_ci                testresult = 1;
3185e1051a39Sopenharmony_ci            goto end;
3186e1051a39Sopenharmony_ci        }
3187e1051a39Sopenharmony_ci        if (!curr->valid)
3188e1051a39Sopenharmony_ci            goto end;
3189e1051a39Sopenharmony_ci    }
3190e1051a39Sopenharmony_ci
3191e1051a39Sopenharmony_ci    if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
3192e1051a39Sopenharmony_ci                                           SSL_ERROR_NONE),
3193e1051a39Sopenharmony_ci                curr->connsuccess))
3194e1051a39Sopenharmony_ci        goto end;
3195e1051a39Sopenharmony_ci
3196e1051a39Sopenharmony_ci    testresult = 1;
3197e1051a39Sopenharmony_ci
3198e1051a39Sopenharmony_ci end:
3199e1051a39Sopenharmony_ci    SSL_free(serverssl);
3200e1051a39Sopenharmony_ci    SSL_free(clientssl);
3201e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
3202e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
3203e1051a39Sopenharmony_ci
3204e1051a39Sopenharmony_ci    return testresult;
3205e1051a39Sopenharmony_ci}
3206e1051a39Sopenharmony_ci#endif
3207e1051a39Sopenharmony_ci
3208e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
3209e1051a39Sopenharmony_cistatic int psk_client_cb_cnt = 0;
3210e1051a39Sopenharmony_cistatic int psk_server_cb_cnt = 0;
3211e1051a39Sopenharmony_ci
3212e1051a39Sopenharmony_cistatic int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
3213e1051a39Sopenharmony_ci                          size_t *idlen, SSL_SESSION **sess)
3214e1051a39Sopenharmony_ci{
3215e1051a39Sopenharmony_ci    switch (++use_session_cb_cnt) {
3216e1051a39Sopenharmony_ci    case 1:
3217e1051a39Sopenharmony_ci        /* The first call should always have a NULL md */
3218e1051a39Sopenharmony_ci        if (md != NULL)
3219e1051a39Sopenharmony_ci            return 0;
3220e1051a39Sopenharmony_ci        break;
3221e1051a39Sopenharmony_ci
3222e1051a39Sopenharmony_ci    case 2:
3223e1051a39Sopenharmony_ci        /* The second call should always have an md */
3224e1051a39Sopenharmony_ci        if (md == NULL)
3225e1051a39Sopenharmony_ci            return 0;
3226e1051a39Sopenharmony_ci        break;
3227e1051a39Sopenharmony_ci
3228e1051a39Sopenharmony_ci    default:
3229e1051a39Sopenharmony_ci        /* We should only be called a maximum of twice */
3230e1051a39Sopenharmony_ci        return 0;
3231e1051a39Sopenharmony_ci    }
3232e1051a39Sopenharmony_ci
3233e1051a39Sopenharmony_ci    if (clientpsk != NULL)
3234e1051a39Sopenharmony_ci        SSL_SESSION_up_ref(clientpsk);
3235e1051a39Sopenharmony_ci
3236e1051a39Sopenharmony_ci    *sess = clientpsk;
3237e1051a39Sopenharmony_ci    *id = (const unsigned char *)pskid;
3238e1051a39Sopenharmony_ci    *idlen = strlen(pskid);
3239e1051a39Sopenharmony_ci
3240e1051a39Sopenharmony_ci    return 1;
3241e1051a39Sopenharmony_ci}
3242e1051a39Sopenharmony_ci
3243e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_PSK
3244e1051a39Sopenharmony_cistatic unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
3245e1051a39Sopenharmony_ci                                  unsigned int max_id_len,
3246e1051a39Sopenharmony_ci                                  unsigned char *psk,
3247e1051a39Sopenharmony_ci                                  unsigned int max_psk_len)
3248e1051a39Sopenharmony_ci{
3249e1051a39Sopenharmony_ci    unsigned int psklen = 0;
3250e1051a39Sopenharmony_ci
3251e1051a39Sopenharmony_ci    psk_client_cb_cnt++;
3252e1051a39Sopenharmony_ci
3253e1051a39Sopenharmony_ci    if (strlen(pskid) + 1 > max_id_len)
3254e1051a39Sopenharmony_ci        return 0;
3255e1051a39Sopenharmony_ci
3256e1051a39Sopenharmony_ci    /* We should only ever be called a maximum of twice per connection */
3257e1051a39Sopenharmony_ci    if (psk_client_cb_cnt > 2)
3258e1051a39Sopenharmony_ci        return 0;
3259e1051a39Sopenharmony_ci
3260e1051a39Sopenharmony_ci    if (clientpsk == NULL)
3261e1051a39Sopenharmony_ci        return 0;
3262e1051a39Sopenharmony_ci
3263e1051a39Sopenharmony_ci    /* We'll reuse the PSK we set up for TLSv1.3 */
3264e1051a39Sopenharmony_ci    if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
3265e1051a39Sopenharmony_ci        return 0;
3266e1051a39Sopenharmony_ci    psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
3267e1051a39Sopenharmony_ci    strncpy(id, pskid, max_id_len);
3268e1051a39Sopenharmony_ci
3269e1051a39Sopenharmony_ci    return psklen;
3270e1051a39Sopenharmony_ci}
3271e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_PSK */
3272e1051a39Sopenharmony_ci
3273e1051a39Sopenharmony_cistatic int find_session_cb(SSL *ssl, const unsigned char *identity,
3274e1051a39Sopenharmony_ci                           size_t identity_len, SSL_SESSION **sess)
3275e1051a39Sopenharmony_ci{
3276e1051a39Sopenharmony_ci    find_session_cb_cnt++;
3277e1051a39Sopenharmony_ci
3278e1051a39Sopenharmony_ci    /* We should only ever be called a maximum of twice per connection */
3279e1051a39Sopenharmony_ci    if (find_session_cb_cnt > 2)
3280e1051a39Sopenharmony_ci        return 0;
3281e1051a39Sopenharmony_ci
3282e1051a39Sopenharmony_ci    if (serverpsk == NULL)
3283e1051a39Sopenharmony_ci        return 0;
3284e1051a39Sopenharmony_ci
3285e1051a39Sopenharmony_ci    /* Identity should match that set by the client */
3286e1051a39Sopenharmony_ci    if (strlen(srvid) != identity_len
3287e1051a39Sopenharmony_ci            || strncmp(srvid, (const char *)identity, identity_len) != 0) {
3288e1051a39Sopenharmony_ci        /* No PSK found, continue but without a PSK */
3289e1051a39Sopenharmony_ci        *sess = NULL;
3290e1051a39Sopenharmony_ci        return 1;
3291e1051a39Sopenharmony_ci    }
3292e1051a39Sopenharmony_ci
3293e1051a39Sopenharmony_ci    SSL_SESSION_up_ref(serverpsk);
3294e1051a39Sopenharmony_ci    *sess = serverpsk;
3295e1051a39Sopenharmony_ci
3296e1051a39Sopenharmony_ci    return 1;
3297e1051a39Sopenharmony_ci}
3298e1051a39Sopenharmony_ci
3299e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_PSK
3300e1051a39Sopenharmony_cistatic unsigned int psk_server_cb(SSL *ssl, const char *identity,
3301e1051a39Sopenharmony_ci                                  unsigned char *psk, unsigned int max_psk_len)
3302e1051a39Sopenharmony_ci{
3303e1051a39Sopenharmony_ci    unsigned int psklen = 0;
3304e1051a39Sopenharmony_ci
3305e1051a39Sopenharmony_ci    psk_server_cb_cnt++;
3306e1051a39Sopenharmony_ci
3307e1051a39Sopenharmony_ci    /* We should only ever be called a maximum of twice per connection */
3308e1051a39Sopenharmony_ci    if (find_session_cb_cnt > 2)
3309e1051a39Sopenharmony_ci        return 0;
3310e1051a39Sopenharmony_ci
3311e1051a39Sopenharmony_ci    if (serverpsk == NULL)
3312e1051a39Sopenharmony_ci        return 0;
3313e1051a39Sopenharmony_ci
3314e1051a39Sopenharmony_ci    /* Identity should match that set by the client */
3315e1051a39Sopenharmony_ci    if (strcmp(srvid, identity) != 0) {
3316e1051a39Sopenharmony_ci        return 0;
3317e1051a39Sopenharmony_ci    }
3318e1051a39Sopenharmony_ci
3319e1051a39Sopenharmony_ci    /* We'll reuse the PSK we set up for TLSv1.3 */
3320e1051a39Sopenharmony_ci    if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
3321e1051a39Sopenharmony_ci        return 0;
3322e1051a39Sopenharmony_ci    psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
3323e1051a39Sopenharmony_ci
3324e1051a39Sopenharmony_ci    return psklen;
3325e1051a39Sopenharmony_ci}
3326e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_PSK */
3327e1051a39Sopenharmony_ci
3328e1051a39Sopenharmony_ci#define MSG1    "Hello"
3329e1051a39Sopenharmony_ci#define MSG2    "World."
3330e1051a39Sopenharmony_ci#define MSG3    "This"
3331e1051a39Sopenharmony_ci#define MSG4    "is"
3332e1051a39Sopenharmony_ci#define MSG5    "a"
3333e1051a39Sopenharmony_ci#define MSG6    "test"
3334e1051a39Sopenharmony_ci#define MSG7    "message."
3335e1051a39Sopenharmony_ci
3336e1051a39Sopenharmony_ci#define TLS13_AES_128_GCM_SHA256_BYTES  ((const unsigned char *)"\x13\x01")
3337e1051a39Sopenharmony_ci#define TLS13_AES_256_GCM_SHA384_BYTES  ((const unsigned char *)"\x13\x02")
3338e1051a39Sopenharmony_ci#define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
3339e1051a39Sopenharmony_ci#define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
3340e1051a39Sopenharmony_ci#define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
3341e1051a39Sopenharmony_ci
3342e1051a39Sopenharmony_ci
3343e1051a39Sopenharmony_cistatic SSL_SESSION *create_a_psk(SSL *ssl, size_t mdsize)
3344e1051a39Sopenharmony_ci{
3345e1051a39Sopenharmony_ci    const SSL_CIPHER *cipher = NULL;
3346e1051a39Sopenharmony_ci    const unsigned char key[] = {
3347e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
3348e1051a39Sopenharmony_ci        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
3349e1051a39Sopenharmony_ci        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
3350e1051a39Sopenharmony_ci        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
3351e1051a39Sopenharmony_ci        0x2c, 0x2d, 0x2e, 0x2f /* SHA384_DIGEST_LENGTH bytes */
3352e1051a39Sopenharmony_ci    };
3353e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
3354e1051a39Sopenharmony_ci
3355e1051a39Sopenharmony_ci    if (mdsize == SHA384_DIGEST_LENGTH) {
3356e1051a39Sopenharmony_ci        cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
3357e1051a39Sopenharmony_ci    } else if (mdsize == SHA256_DIGEST_LENGTH) {
3358e1051a39Sopenharmony_ci        /*
3359e1051a39Sopenharmony_ci         * Any ciphersuite using SHA256 will do - it will be compatible with
3360e1051a39Sopenharmony_ci         * the actual ciphersuite selected as long as it too is based on SHA256
3361e1051a39Sopenharmony_ci         */
3362e1051a39Sopenharmony_ci        cipher = SSL_CIPHER_find(ssl, TLS13_AES_128_GCM_SHA256_BYTES);
3363e1051a39Sopenharmony_ci    } else {
3364e1051a39Sopenharmony_ci        /* Should not happen */
3365e1051a39Sopenharmony_ci        return NULL;
3366e1051a39Sopenharmony_ci    }
3367e1051a39Sopenharmony_ci    sess = SSL_SESSION_new();
3368e1051a39Sopenharmony_ci    if (!TEST_ptr(sess)
3369e1051a39Sopenharmony_ci            || !TEST_ptr(cipher)
3370e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_set1_master_key(sess, key, mdsize))
3371e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
3372e1051a39Sopenharmony_ci            || !TEST_true(
3373e1051a39Sopenharmony_ci                    SSL_SESSION_set_protocol_version(sess,
3374e1051a39Sopenharmony_ci                                                     TLS1_3_VERSION))) {
3375e1051a39Sopenharmony_ci        SSL_SESSION_free(sess);
3376e1051a39Sopenharmony_ci        return NULL;
3377e1051a39Sopenharmony_ci    }
3378e1051a39Sopenharmony_ci    return sess;
3379e1051a39Sopenharmony_ci}
3380e1051a39Sopenharmony_ci
3381e1051a39Sopenharmony_ci/*
3382e1051a39Sopenharmony_ci * Helper method to setup objects for early data test. Caller frees objects on
3383e1051a39Sopenharmony_ci * error.
3384e1051a39Sopenharmony_ci */
3385e1051a39Sopenharmony_cistatic int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
3386e1051a39Sopenharmony_ci                                SSL **serverssl, SSL_SESSION **sess, int idx,
3387e1051a39Sopenharmony_ci                                size_t mdsize)
3388e1051a39Sopenharmony_ci{
3389e1051a39Sopenharmony_ci    if (*sctx == NULL
3390e1051a39Sopenharmony_ci            && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3391e1051a39Sopenharmony_ci                                              TLS_client_method(),
3392e1051a39Sopenharmony_ci                                              TLS1_VERSION, 0,
3393e1051a39Sopenharmony_ci                                              sctx, cctx, cert, privkey)))
3394e1051a39Sopenharmony_ci        return 0;
3395e1051a39Sopenharmony_ci
3396e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
3397e1051a39Sopenharmony_ci        return 0;
3398e1051a39Sopenharmony_ci
3399e1051a39Sopenharmony_ci    if (idx == 1) {
3400e1051a39Sopenharmony_ci        /* When idx == 1 we repeat the tests with read_ahead set */
3401e1051a39Sopenharmony_ci        SSL_CTX_set_read_ahead(*cctx, 1);
3402e1051a39Sopenharmony_ci        SSL_CTX_set_read_ahead(*sctx, 1);
3403e1051a39Sopenharmony_ci    } else if (idx == 2) {
3404e1051a39Sopenharmony_ci        /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
3405e1051a39Sopenharmony_ci        SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
3406e1051a39Sopenharmony_ci        SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
3407e1051a39Sopenharmony_ci        use_session_cb_cnt = 0;
3408e1051a39Sopenharmony_ci        find_session_cb_cnt = 0;
3409e1051a39Sopenharmony_ci        srvid = pskid;
3410e1051a39Sopenharmony_ci    }
3411e1051a39Sopenharmony_ci
3412e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
3413e1051a39Sopenharmony_ci                                      NULL, NULL)))
3414e1051a39Sopenharmony_ci        return 0;
3415e1051a39Sopenharmony_ci
3416e1051a39Sopenharmony_ci    /*
3417e1051a39Sopenharmony_ci     * For one of the run throughs (doesn't matter which one), we'll try sending
3418e1051a39Sopenharmony_ci     * some SNI data in the initial ClientHello. This will be ignored (because
3419e1051a39Sopenharmony_ci     * there is no SNI cb set up by the server), so it should not impact
3420e1051a39Sopenharmony_ci     * early_data.
3421e1051a39Sopenharmony_ci     */
3422e1051a39Sopenharmony_ci    if (idx == 1
3423e1051a39Sopenharmony_ci            && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
3424e1051a39Sopenharmony_ci        return 0;
3425e1051a39Sopenharmony_ci
3426e1051a39Sopenharmony_ci    if (idx == 2) {
3427e1051a39Sopenharmony_ci        clientpsk = create_a_psk(*clientssl, mdsize);
3428e1051a39Sopenharmony_ci        if (!TEST_ptr(clientpsk)
3429e1051a39Sopenharmony_ci                   /*
3430e1051a39Sopenharmony_ci                    * We just choose an arbitrary value for max_early_data which
3431e1051a39Sopenharmony_ci                    * should be big enough for testing purposes.
3432e1051a39Sopenharmony_ci                    */
3433e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
3434e1051a39Sopenharmony_ci                                                             0x100))
3435e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3436e1051a39Sopenharmony_ci            SSL_SESSION_free(clientpsk);
3437e1051a39Sopenharmony_ci            clientpsk = NULL;
3438e1051a39Sopenharmony_ci            return 0;
3439e1051a39Sopenharmony_ci        }
3440e1051a39Sopenharmony_ci        serverpsk = clientpsk;
3441e1051a39Sopenharmony_ci
3442e1051a39Sopenharmony_ci        if (sess != NULL) {
3443e1051a39Sopenharmony_ci            if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
3444e1051a39Sopenharmony_ci                SSL_SESSION_free(clientpsk);
3445e1051a39Sopenharmony_ci                SSL_SESSION_free(serverpsk);
3446e1051a39Sopenharmony_ci                clientpsk = serverpsk = NULL;
3447e1051a39Sopenharmony_ci                return 0;
3448e1051a39Sopenharmony_ci            }
3449e1051a39Sopenharmony_ci            *sess = clientpsk;
3450e1051a39Sopenharmony_ci        }
3451e1051a39Sopenharmony_ci        return 1;
3452e1051a39Sopenharmony_ci    }
3453e1051a39Sopenharmony_ci
3454e1051a39Sopenharmony_ci    if (sess == NULL)
3455e1051a39Sopenharmony_ci        return 1;
3456e1051a39Sopenharmony_ci
3457e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
3458e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
3459e1051a39Sopenharmony_ci        return 0;
3460e1051a39Sopenharmony_ci
3461e1051a39Sopenharmony_ci    *sess = SSL_get1_session(*clientssl);
3462e1051a39Sopenharmony_ci    SSL_shutdown(*clientssl);
3463e1051a39Sopenharmony_ci    SSL_shutdown(*serverssl);
3464e1051a39Sopenharmony_ci    SSL_free(*serverssl);
3465e1051a39Sopenharmony_ci    SSL_free(*clientssl);
3466e1051a39Sopenharmony_ci    *serverssl = *clientssl = NULL;
3467e1051a39Sopenharmony_ci
3468e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
3469e1051a39Sopenharmony_ci                                      clientssl, NULL, NULL))
3470e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(*clientssl, *sess)))
3471e1051a39Sopenharmony_ci        return 0;
3472e1051a39Sopenharmony_ci
3473e1051a39Sopenharmony_ci    return 1;
3474e1051a39Sopenharmony_ci}
3475e1051a39Sopenharmony_ci
3476e1051a39Sopenharmony_cistatic int test_early_data_read_write(int idx)
3477e1051a39Sopenharmony_ci{
3478e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
3479e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
3480e1051a39Sopenharmony_ci    int testresult = 0;
3481e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
3482e1051a39Sopenharmony_ci    unsigned char buf[20], data[1024];
3483e1051a39Sopenharmony_ci    size_t readbytes, written, eoedlen, rawread, rawwritten;
3484e1051a39Sopenharmony_ci    BIO *rbio;
3485e1051a39Sopenharmony_ci
3486e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3487e1051a39Sopenharmony_ci                                        &serverssl, &sess, idx,
3488e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
3489e1051a39Sopenharmony_ci        goto end;
3490e1051a39Sopenharmony_ci
3491e1051a39Sopenharmony_ci    /* Write and read some early data */
3492e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3493e1051a39Sopenharmony_ci                                        &written))
3494e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1))
3495e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
3496e1051a39Sopenharmony_ci                                                sizeof(buf), &readbytes),
3497e1051a39Sopenharmony_ci                            SSL_READ_EARLY_DATA_SUCCESS)
3498e1051a39Sopenharmony_ci            || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
3499e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3500e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_ACCEPTED))
3501e1051a39Sopenharmony_ci        goto end;
3502e1051a39Sopenharmony_ci
3503e1051a39Sopenharmony_ci    /*
3504e1051a39Sopenharmony_ci     * Server should be able to write data, and client should be able to
3505e1051a39Sopenharmony_ci     * read it.
3506e1051a39Sopenharmony_ci     */
3507e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
3508e1051a39Sopenharmony_ci                                        &written))
3509e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2))
3510e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3511e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
3512e1051a39Sopenharmony_ci        goto end;
3513e1051a39Sopenharmony_ci
3514e1051a39Sopenharmony_ci    /* Even after reading normal data, client should be able write early data */
3515e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
3516e1051a39Sopenharmony_ci                                        &written))
3517e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG3)))
3518e1051a39Sopenharmony_ci        goto end;
3519e1051a39Sopenharmony_ci
3520e1051a39Sopenharmony_ci    /* Server should still be able read early data after writing data */
3521e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3522e1051a39Sopenharmony_ci                                         &readbytes),
3523e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_SUCCESS)
3524e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
3525e1051a39Sopenharmony_ci        goto end;
3526e1051a39Sopenharmony_ci
3527e1051a39Sopenharmony_ci    /* Write more data from server and read it from client */
3528e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
3529e1051a39Sopenharmony_ci                                        &written))
3530e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG4))
3531e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3532e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
3533e1051a39Sopenharmony_ci        goto end;
3534e1051a39Sopenharmony_ci
3535e1051a39Sopenharmony_ci    /*
3536e1051a39Sopenharmony_ci     * If client writes normal data it should mean writing early data is no
3537e1051a39Sopenharmony_ci     * longer possible.
3538e1051a39Sopenharmony_ci     */
3539e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3540e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG5))
3541e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(clientssl),
3542e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_ACCEPTED))
3543e1051a39Sopenharmony_ci        goto end;
3544e1051a39Sopenharmony_ci
3545e1051a39Sopenharmony_ci    /*
3546e1051a39Sopenharmony_ci     * At this point the client has written EndOfEarlyData, ClientFinished and
3547e1051a39Sopenharmony_ci     * normal (fully protected) data. We are going to cause a delay between the
3548e1051a39Sopenharmony_ci     * arrival of EndOfEarlyData and ClientFinished. We read out all the data
3549e1051a39Sopenharmony_ci     * in the read BIO, and then just put back the EndOfEarlyData message.
3550e1051a39Sopenharmony_ci     */
3551e1051a39Sopenharmony_ci    rbio = SSL_get_rbio(serverssl);
3552e1051a39Sopenharmony_ci    if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
3553e1051a39Sopenharmony_ci            || !TEST_size_t_lt(rawread, sizeof(data))
3554e1051a39Sopenharmony_ci            || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
3555e1051a39Sopenharmony_ci        goto end;
3556e1051a39Sopenharmony_ci
3557e1051a39Sopenharmony_ci    /* Record length is in the 4th and 5th bytes of the record header */
3558e1051a39Sopenharmony_ci    eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
3559e1051a39Sopenharmony_ci    if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
3560e1051a39Sopenharmony_ci            || !TEST_size_t_eq(rawwritten, eoedlen))
3561e1051a39Sopenharmony_ci        goto end;
3562e1051a39Sopenharmony_ci
3563e1051a39Sopenharmony_ci    /* Server should be told that there is no more early data */
3564e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3565e1051a39Sopenharmony_ci                                         &readbytes),
3566e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_FINISH)
3567e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, 0))
3568e1051a39Sopenharmony_ci        goto end;
3569e1051a39Sopenharmony_ci
3570e1051a39Sopenharmony_ci    /*
3571e1051a39Sopenharmony_ci     * Server has not finished init yet, so should still be able to write early
3572e1051a39Sopenharmony_ci     * data.
3573e1051a39Sopenharmony_ci     */
3574e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
3575e1051a39Sopenharmony_ci                                        &written))
3576e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG6)))
3577e1051a39Sopenharmony_ci        goto end;
3578e1051a39Sopenharmony_ci
3579e1051a39Sopenharmony_ci    /* Push the ClientFinished and the normal data back into the server rbio */
3580e1051a39Sopenharmony_ci    if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
3581e1051a39Sopenharmony_ci                                &rawwritten))
3582e1051a39Sopenharmony_ci            || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
3583e1051a39Sopenharmony_ci        goto end;
3584e1051a39Sopenharmony_ci
3585e1051a39Sopenharmony_ci    /* Server should be able to read normal data */
3586e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3587e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3588e1051a39Sopenharmony_ci        goto end;
3589e1051a39Sopenharmony_ci
3590e1051a39Sopenharmony_ci    /* Client and server should not be able to write/read early data now */
3591e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3592e1051a39Sopenharmony_ci                                         &written)))
3593e1051a39Sopenharmony_ci        goto end;
3594e1051a39Sopenharmony_ci    ERR_clear_error();
3595e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3596e1051a39Sopenharmony_ci                                         &readbytes),
3597e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_ERROR))
3598e1051a39Sopenharmony_ci        goto end;
3599e1051a39Sopenharmony_ci    ERR_clear_error();
3600e1051a39Sopenharmony_ci
3601e1051a39Sopenharmony_ci    /* Client should be able to read the data sent by the server */
3602e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3603e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
3604e1051a39Sopenharmony_ci        goto end;
3605e1051a39Sopenharmony_ci
3606e1051a39Sopenharmony_ci    /*
3607e1051a39Sopenharmony_ci     * Make sure we process the two NewSessionTickets. These arrive
3608e1051a39Sopenharmony_ci     * post-handshake. We attempt reads which we do not expect to return any
3609e1051a39Sopenharmony_ci     * data.
3610e1051a39Sopenharmony_ci     */
3611e1051a39Sopenharmony_ci    if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3612e1051a39Sopenharmony_ci            || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
3613e1051a39Sopenharmony_ci                           &readbytes)))
3614e1051a39Sopenharmony_ci        goto end;
3615e1051a39Sopenharmony_ci
3616e1051a39Sopenharmony_ci    /* Server should be able to write normal data */
3617e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
3618e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG7))
3619e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
3620e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
3621e1051a39Sopenharmony_ci        goto end;
3622e1051a39Sopenharmony_ci
3623e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
3624e1051a39Sopenharmony_ci    sess = SSL_get1_session(clientssl);
3625e1051a39Sopenharmony_ci    use_session_cb_cnt = 0;
3626e1051a39Sopenharmony_ci    find_session_cb_cnt = 0;
3627e1051a39Sopenharmony_ci
3628e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
3629e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
3630e1051a39Sopenharmony_ci    SSL_free(serverssl);
3631e1051a39Sopenharmony_ci    SSL_free(clientssl);
3632e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
3633e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3634e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
3635e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, sess)))
3636e1051a39Sopenharmony_ci        goto end;
3637e1051a39Sopenharmony_ci
3638e1051a39Sopenharmony_ci    /* Write and read some early data */
3639e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3640e1051a39Sopenharmony_ci                                        &written))
3641e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1))
3642e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3643e1051a39Sopenharmony_ci                                                &readbytes),
3644e1051a39Sopenharmony_ci                            SSL_READ_EARLY_DATA_SUCCESS)
3645e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
3646e1051a39Sopenharmony_ci        goto end;
3647e1051a39Sopenharmony_ci
3648e1051a39Sopenharmony_ci    if (!TEST_int_gt(SSL_connect(clientssl), 0)
3649e1051a39Sopenharmony_ci            || !TEST_int_gt(SSL_accept(serverssl), 0))
3650e1051a39Sopenharmony_ci        goto end;
3651e1051a39Sopenharmony_ci
3652e1051a39Sopenharmony_ci    /* Client and server should not be able to write/read early data now */
3653e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
3654e1051a39Sopenharmony_ci                                         &written)))
3655e1051a39Sopenharmony_ci        goto end;
3656e1051a39Sopenharmony_ci    ERR_clear_error();
3657e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3658e1051a39Sopenharmony_ci                                         &readbytes),
3659e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_ERROR))
3660e1051a39Sopenharmony_ci        goto end;
3661e1051a39Sopenharmony_ci    ERR_clear_error();
3662e1051a39Sopenharmony_ci
3663e1051a39Sopenharmony_ci    /* Client and server should be able to write/read normal data */
3664e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
3665e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG5))
3666e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3667e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, strlen(MSG5)))
3668e1051a39Sopenharmony_ci        goto end;
3669e1051a39Sopenharmony_ci
3670e1051a39Sopenharmony_ci    testresult = 1;
3671e1051a39Sopenharmony_ci
3672e1051a39Sopenharmony_ci end:
3673e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
3674e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
3675e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
3676e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
3677e1051a39Sopenharmony_ci    SSL_free(serverssl);
3678e1051a39Sopenharmony_ci    SSL_free(clientssl);
3679e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
3680e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
3681e1051a39Sopenharmony_ci    return testresult;
3682e1051a39Sopenharmony_ci}
3683e1051a39Sopenharmony_ci
3684e1051a39Sopenharmony_cistatic int allow_ed_cb_called = 0;
3685e1051a39Sopenharmony_ci
3686e1051a39Sopenharmony_cistatic int allow_early_data_cb(SSL *s, void *arg)
3687e1051a39Sopenharmony_ci{
3688e1051a39Sopenharmony_ci    int *usecb = (int *)arg;
3689e1051a39Sopenharmony_ci
3690e1051a39Sopenharmony_ci    allow_ed_cb_called++;
3691e1051a39Sopenharmony_ci
3692e1051a39Sopenharmony_ci    if (*usecb == 1)
3693e1051a39Sopenharmony_ci        return 0;
3694e1051a39Sopenharmony_ci
3695e1051a39Sopenharmony_ci    return 1;
3696e1051a39Sopenharmony_ci}
3697e1051a39Sopenharmony_ci
3698e1051a39Sopenharmony_ci/*
3699e1051a39Sopenharmony_ci * idx == 0: Standard early_data setup
3700e1051a39Sopenharmony_ci * idx == 1: early_data setup using read_ahead
3701e1051a39Sopenharmony_ci * usecb == 0: Don't use a custom early data callback
3702e1051a39Sopenharmony_ci * usecb == 1: Use a custom early data callback and reject the early data
3703e1051a39Sopenharmony_ci * usecb == 2: Use a custom early data callback and accept the early data
3704e1051a39Sopenharmony_ci * confopt == 0: Configure anti-replay directly
3705e1051a39Sopenharmony_ci * confopt == 1: Configure anti-replay using SSL_CONF
3706e1051a39Sopenharmony_ci */
3707e1051a39Sopenharmony_cistatic int test_early_data_replay_int(int idx, int usecb, int confopt)
3708e1051a39Sopenharmony_ci{
3709e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
3710e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
3711e1051a39Sopenharmony_ci    int testresult = 0;
3712e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
3713e1051a39Sopenharmony_ci    size_t readbytes, written;
3714e1051a39Sopenharmony_ci    unsigned char buf[20];
3715e1051a39Sopenharmony_ci
3716e1051a39Sopenharmony_ci    allow_ed_cb_called = 0;
3717e1051a39Sopenharmony_ci
3718e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3719e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
3720e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
3721e1051a39Sopenharmony_ci        return 0;
3722e1051a39Sopenharmony_ci
3723e1051a39Sopenharmony_ci    if (usecb > 0) {
3724e1051a39Sopenharmony_ci        if (confopt == 0) {
3725e1051a39Sopenharmony_ci            SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
3726e1051a39Sopenharmony_ci        } else {
3727e1051a39Sopenharmony_ci            SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
3728e1051a39Sopenharmony_ci
3729e1051a39Sopenharmony_ci            if (!TEST_ptr(confctx))
3730e1051a39Sopenharmony_ci                goto end;
3731e1051a39Sopenharmony_ci            SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
3732e1051a39Sopenharmony_ci                                            | SSL_CONF_FLAG_SERVER);
3733e1051a39Sopenharmony_ci            SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
3734e1051a39Sopenharmony_ci            if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
3735e1051a39Sopenharmony_ci                             2)) {
3736e1051a39Sopenharmony_ci                SSL_CONF_CTX_free(confctx);
3737e1051a39Sopenharmony_ci                goto end;
3738e1051a39Sopenharmony_ci            }
3739e1051a39Sopenharmony_ci            SSL_CONF_CTX_free(confctx);
3740e1051a39Sopenharmony_ci        }
3741e1051a39Sopenharmony_ci        SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
3742e1051a39Sopenharmony_ci    }
3743e1051a39Sopenharmony_ci
3744e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3745e1051a39Sopenharmony_ci                                        &serverssl, &sess, idx,
3746e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
3747e1051a39Sopenharmony_ci        goto end;
3748e1051a39Sopenharmony_ci
3749e1051a39Sopenharmony_ci    /*
3750e1051a39Sopenharmony_ci     * The server is configured to accept early data. Create a connection to
3751e1051a39Sopenharmony_ci     * "use up" the ticket
3752e1051a39Sopenharmony_ci     */
3753e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3754e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
3755e1051a39Sopenharmony_ci        goto end;
3756e1051a39Sopenharmony_ci
3757e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
3758e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
3759e1051a39Sopenharmony_ci    SSL_free(serverssl);
3760e1051a39Sopenharmony_ci    SSL_free(clientssl);
3761e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
3762e1051a39Sopenharmony_ci
3763e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
3764e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
3765e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, sess)))
3766e1051a39Sopenharmony_ci        goto end;
3767e1051a39Sopenharmony_ci
3768e1051a39Sopenharmony_ci    /* Write and read some early data */
3769e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3770e1051a39Sopenharmony_ci                                        &written))
3771e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1)))
3772e1051a39Sopenharmony_ci        goto end;
3773e1051a39Sopenharmony_ci
3774e1051a39Sopenharmony_ci    if (usecb <= 1) {
3775e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3776e1051a39Sopenharmony_ci                                             &readbytes),
3777e1051a39Sopenharmony_ci                         SSL_READ_EARLY_DATA_FINISH)
3778e1051a39Sopenharmony_ci                   /*
3779e1051a39Sopenharmony_ci                    * The ticket was reused, so the we should have rejected the
3780e1051a39Sopenharmony_ci                    * early data
3781e1051a39Sopenharmony_ci                    */
3782e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3783e1051a39Sopenharmony_ci                                SSL_EARLY_DATA_REJECTED))
3784e1051a39Sopenharmony_ci            goto end;
3785e1051a39Sopenharmony_ci    } else {
3786e1051a39Sopenharmony_ci        /* In this case the callback decides to accept the early data */
3787e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3788e1051a39Sopenharmony_ci                                             &readbytes),
3789e1051a39Sopenharmony_ci                         SSL_READ_EARLY_DATA_SUCCESS)
3790e1051a39Sopenharmony_ci                || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
3791e1051a39Sopenharmony_ci                   /*
3792e1051a39Sopenharmony_ci                    * Server will have sent its flight so client can now send
3793e1051a39Sopenharmony_ci                    * end of early data and complete its half of the handshake
3794e1051a39Sopenharmony_ci                    */
3795e1051a39Sopenharmony_ci                || !TEST_int_gt(SSL_connect(clientssl), 0)
3796e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3797e1051a39Sopenharmony_ci                                             &readbytes),
3798e1051a39Sopenharmony_ci                                SSL_READ_EARLY_DATA_FINISH)
3799e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3800e1051a39Sopenharmony_ci                                SSL_EARLY_DATA_ACCEPTED))
3801e1051a39Sopenharmony_ci            goto end;
3802e1051a39Sopenharmony_ci    }
3803e1051a39Sopenharmony_ci
3804e1051a39Sopenharmony_ci    /* Complete the connection */
3805e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
3806e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
3807e1051a39Sopenharmony_ci            || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
3808e1051a39Sopenharmony_ci        goto end;
3809e1051a39Sopenharmony_ci
3810e1051a39Sopenharmony_ci    testresult = 1;
3811e1051a39Sopenharmony_ci
3812e1051a39Sopenharmony_ci end:
3813e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
3814e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
3815e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
3816e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
3817e1051a39Sopenharmony_ci    SSL_free(serverssl);
3818e1051a39Sopenharmony_ci    SSL_free(clientssl);
3819e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
3820e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
3821e1051a39Sopenharmony_ci    return testresult;
3822e1051a39Sopenharmony_ci}
3823e1051a39Sopenharmony_ci
3824e1051a39Sopenharmony_cistatic int test_early_data_replay(int idx)
3825e1051a39Sopenharmony_ci{
3826e1051a39Sopenharmony_ci    int ret = 1, usecb, confopt;
3827e1051a39Sopenharmony_ci
3828e1051a39Sopenharmony_ci    for (usecb = 0; usecb < 3; usecb++) {
3829e1051a39Sopenharmony_ci        for (confopt = 0; confopt < 2; confopt++)
3830e1051a39Sopenharmony_ci            ret &= test_early_data_replay_int(idx, usecb, confopt);
3831e1051a39Sopenharmony_ci    }
3832e1051a39Sopenharmony_ci
3833e1051a39Sopenharmony_ci    return ret;
3834e1051a39Sopenharmony_ci}
3835e1051a39Sopenharmony_ci
3836e1051a39Sopenharmony_cistatic const char *ciphersuites[] = {
3837e1051a39Sopenharmony_ci    "TLS_AES_128_CCM_8_SHA256",
3838e1051a39Sopenharmony_ci    "TLS_AES_128_GCM_SHA256",
3839e1051a39Sopenharmony_ci    "TLS_AES_256_GCM_SHA384",
3840e1051a39Sopenharmony_ci    "TLS_AES_128_CCM_SHA256",
3841e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
3842e1051a39Sopenharmony_ci    "TLS_CHACHA20_POLY1305_SHA256"
3843e1051a39Sopenharmony_ci#endif
3844e1051a39Sopenharmony_ci};
3845e1051a39Sopenharmony_ci
3846e1051a39Sopenharmony_ci/*
3847e1051a39Sopenharmony_ci * Helper function to test that a server attempting to read early data can
3848e1051a39Sopenharmony_ci * handle a connection from a client where the early data should be skipped.
3849e1051a39Sopenharmony_ci * testtype: 0 == No HRR
3850e1051a39Sopenharmony_ci * testtype: 1 == HRR
3851e1051a39Sopenharmony_ci * testtype: 2 == HRR, invalid early_data sent after HRR
3852e1051a39Sopenharmony_ci * testtype: 3 == recv_max_early_data set to 0
3853e1051a39Sopenharmony_ci */
3854e1051a39Sopenharmony_cistatic int early_data_skip_helper(int testtype, int cipher, int idx)
3855e1051a39Sopenharmony_ci{
3856e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
3857e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
3858e1051a39Sopenharmony_ci    int testresult = 0;
3859e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
3860e1051a39Sopenharmony_ci    unsigned char buf[20];
3861e1051a39Sopenharmony_ci    size_t readbytes, written;
3862e1051a39Sopenharmony_ci
3863e1051a39Sopenharmony_ci    if (is_fips && cipher == 4)
3864e1051a39Sopenharmony_ci        return 1;
3865e1051a39Sopenharmony_ci
3866e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
3867e1051a39Sopenharmony_ci                                              TLS_client_method(),
3868e1051a39Sopenharmony_ci                                              TLS1_VERSION, 0,
3869e1051a39Sopenharmony_ci                                              &sctx, &cctx, cert, privkey)))
3870e1051a39Sopenharmony_ci        goto end;
3871e1051a39Sopenharmony_ci
3872e1051a39Sopenharmony_ci    if (cipher == 0) {
3873e1051a39Sopenharmony_ci        SSL_CTX_set_security_level(sctx, 0);
3874e1051a39Sopenharmony_ci        SSL_CTX_set_security_level(cctx, 0);
3875e1051a39Sopenharmony_ci    }
3876e1051a39Sopenharmony_ci
3877e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher]))
3878e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher])))
3879e1051a39Sopenharmony_ci        goto end;
3880e1051a39Sopenharmony_ci
3881e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
3882e1051a39Sopenharmony_ci                                        &serverssl, &sess, idx,
3883e1051a39Sopenharmony_ci                                        cipher == 2 ? SHA384_DIGEST_LENGTH
3884e1051a39Sopenharmony_ci                                                    : SHA256_DIGEST_LENGTH)))
3885e1051a39Sopenharmony_ci        goto end;
3886e1051a39Sopenharmony_ci
3887e1051a39Sopenharmony_ci    if (testtype == 1 || testtype == 2) {
3888e1051a39Sopenharmony_ci        /* Force an HRR to occur */
3889e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_EC)
3890e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
3891e1051a39Sopenharmony_ci            goto end;
3892e1051a39Sopenharmony_ci#else
3893e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
3894e1051a39Sopenharmony_ci            goto end;
3895e1051a39Sopenharmony_ci#endif
3896e1051a39Sopenharmony_ci    } else if (idx == 2) {
3897e1051a39Sopenharmony_ci        /*
3898e1051a39Sopenharmony_ci         * We force early_data rejection by ensuring the PSK identity is
3899e1051a39Sopenharmony_ci         * unrecognised
3900e1051a39Sopenharmony_ci         */
3901e1051a39Sopenharmony_ci        srvid = "Dummy Identity";
3902e1051a39Sopenharmony_ci    } else {
3903e1051a39Sopenharmony_ci        /*
3904e1051a39Sopenharmony_ci         * Deliberately corrupt the creation time. We take 20 seconds off the
3905e1051a39Sopenharmony_ci         * time. It could be any value as long as it is not within tolerance.
3906e1051a39Sopenharmony_ci         * This should mean the ticket is rejected.
3907e1051a39Sopenharmony_ci         */
3908e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
3909e1051a39Sopenharmony_ci            goto end;
3910e1051a39Sopenharmony_ci    }
3911e1051a39Sopenharmony_ci
3912e1051a39Sopenharmony_ci    if (testtype == 3
3913e1051a39Sopenharmony_ci            && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
3914e1051a39Sopenharmony_ci        goto end;
3915e1051a39Sopenharmony_ci
3916e1051a39Sopenharmony_ci    /* Write some early data */
3917e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
3918e1051a39Sopenharmony_ci                                        &written))
3919e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1)))
3920e1051a39Sopenharmony_ci        goto end;
3921e1051a39Sopenharmony_ci
3922e1051a39Sopenharmony_ci    /* Server should reject the early data */
3923e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
3924e1051a39Sopenharmony_ci                                         &readbytes),
3925e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_FINISH)
3926e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, 0)
3927e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
3928e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_REJECTED))
3929e1051a39Sopenharmony_ci        goto end;
3930e1051a39Sopenharmony_ci
3931e1051a39Sopenharmony_ci    switch (testtype) {
3932e1051a39Sopenharmony_ci    case 0:
3933e1051a39Sopenharmony_ci        /* Nothing to do */
3934e1051a39Sopenharmony_ci        break;
3935e1051a39Sopenharmony_ci
3936e1051a39Sopenharmony_ci    case 1:
3937e1051a39Sopenharmony_ci        /*
3938e1051a39Sopenharmony_ci         * Finish off the handshake. We perform the same writes and reads as
3939e1051a39Sopenharmony_ci         * further down but we expect them to fail due to the incomplete
3940e1051a39Sopenharmony_ci         * handshake.
3941e1051a39Sopenharmony_ci         */
3942e1051a39Sopenharmony_ci        if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
3943e1051a39Sopenharmony_ci                || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
3944e1051a39Sopenharmony_ci                               &readbytes)))
3945e1051a39Sopenharmony_ci            goto end;
3946e1051a39Sopenharmony_ci        break;
3947e1051a39Sopenharmony_ci
3948e1051a39Sopenharmony_ci    case 2:
3949e1051a39Sopenharmony_ci        {
3950e1051a39Sopenharmony_ci            BIO *wbio = SSL_get_wbio(clientssl);
3951e1051a39Sopenharmony_ci            /* A record that will appear as bad early_data */
3952e1051a39Sopenharmony_ci            const unsigned char bad_early_data[] = {
3953e1051a39Sopenharmony_ci                0x17, 0x03, 0x03, 0x00, 0x01, 0x00
3954e1051a39Sopenharmony_ci            };
3955e1051a39Sopenharmony_ci
3956e1051a39Sopenharmony_ci            /*
3957e1051a39Sopenharmony_ci             * We force the client to attempt a write. This will fail because
3958e1051a39Sopenharmony_ci             * we're still in the handshake. It will cause the second
3959e1051a39Sopenharmony_ci             * ClientHello to be sent.
3960e1051a39Sopenharmony_ci             */
3961e1051a39Sopenharmony_ci            if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
3962e1051a39Sopenharmony_ci                                         &written)))
3963e1051a39Sopenharmony_ci                goto end;
3964e1051a39Sopenharmony_ci
3965e1051a39Sopenharmony_ci            /*
3966e1051a39Sopenharmony_ci             * Inject some early_data after the second ClientHello. This should
3967e1051a39Sopenharmony_ci             * cause the server to fail
3968e1051a39Sopenharmony_ci             */
3969e1051a39Sopenharmony_ci            if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
3970e1051a39Sopenharmony_ci                                        sizeof(bad_early_data), &written)))
3971e1051a39Sopenharmony_ci                goto end;
3972e1051a39Sopenharmony_ci        }
3973e1051a39Sopenharmony_ci        /* fallthrough */
3974e1051a39Sopenharmony_ci
3975e1051a39Sopenharmony_ci    case 3:
3976e1051a39Sopenharmony_ci        /*
3977e1051a39Sopenharmony_ci         * This client has sent more early_data than we are willing to skip
3978e1051a39Sopenharmony_ci         * (case 3) or sent invalid early_data (case 2) so the connection should
3979e1051a39Sopenharmony_ci         * abort.
3980e1051a39Sopenharmony_ci         */
3981e1051a39Sopenharmony_ci        if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
3982e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
3983e1051a39Sopenharmony_ci            goto end;
3984e1051a39Sopenharmony_ci
3985e1051a39Sopenharmony_ci        /* Connection has failed - nothing more to do */
3986e1051a39Sopenharmony_ci        testresult = 1;
3987e1051a39Sopenharmony_ci        goto end;
3988e1051a39Sopenharmony_ci
3989e1051a39Sopenharmony_ci    default:
3990e1051a39Sopenharmony_ci        TEST_error("Invalid test type");
3991e1051a39Sopenharmony_ci        goto end;
3992e1051a39Sopenharmony_ci    }
3993e1051a39Sopenharmony_ci
3994e1051a39Sopenharmony_ci    ERR_clear_error();
3995e1051a39Sopenharmony_ci    /*
3996e1051a39Sopenharmony_ci     * Should be able to send normal data despite rejection of early data. The
3997e1051a39Sopenharmony_ci     * early_data should be skipped.
3998e1051a39Sopenharmony_ci     */
3999e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4000e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2))
4001e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4002e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_REJECTED)
4003e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4004e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4005e1051a39Sopenharmony_ci        goto end;
4006e1051a39Sopenharmony_ci
4007e1051a39Sopenharmony_ci    /*
4008e1051a39Sopenharmony_ci     * Failure to decrypt early data records should not leave spurious errors
4009e1051a39Sopenharmony_ci     * on the error stack
4010e1051a39Sopenharmony_ci     */
4011e1051a39Sopenharmony_ci    if (!TEST_long_eq(ERR_peek_error(), 0))
4012e1051a39Sopenharmony_ci        goto end;
4013e1051a39Sopenharmony_ci
4014e1051a39Sopenharmony_ci    testresult = 1;
4015e1051a39Sopenharmony_ci
4016e1051a39Sopenharmony_ci end:
4017e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4018e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4019e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4020e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
4021e1051a39Sopenharmony_ci    SSL_free(serverssl);
4022e1051a39Sopenharmony_ci    SSL_free(clientssl);
4023e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4024e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4025e1051a39Sopenharmony_ci    return testresult;
4026e1051a39Sopenharmony_ci}
4027e1051a39Sopenharmony_ci
4028e1051a39Sopenharmony_ci/*
4029e1051a39Sopenharmony_ci * Test that a server attempting to read early data can handle a connection
4030e1051a39Sopenharmony_ci * from a client where the early data is not acceptable.
4031e1051a39Sopenharmony_ci */
4032e1051a39Sopenharmony_cistatic int test_early_data_skip(int idx)
4033e1051a39Sopenharmony_ci{
4034e1051a39Sopenharmony_ci    return early_data_skip_helper(0,
4035e1051a39Sopenharmony_ci                                  idx % OSSL_NELEM(ciphersuites),
4036e1051a39Sopenharmony_ci                                  idx / OSSL_NELEM(ciphersuites));
4037e1051a39Sopenharmony_ci}
4038e1051a39Sopenharmony_ci
4039e1051a39Sopenharmony_ci/*
4040e1051a39Sopenharmony_ci * Test that a server attempting to read early data can handle a connection
4041e1051a39Sopenharmony_ci * from a client where an HRR occurs.
4042e1051a39Sopenharmony_ci */
4043e1051a39Sopenharmony_cistatic int test_early_data_skip_hrr(int idx)
4044e1051a39Sopenharmony_ci{
4045e1051a39Sopenharmony_ci    return early_data_skip_helper(1,
4046e1051a39Sopenharmony_ci                                  idx % OSSL_NELEM(ciphersuites),
4047e1051a39Sopenharmony_ci                                  idx / OSSL_NELEM(ciphersuites));
4048e1051a39Sopenharmony_ci}
4049e1051a39Sopenharmony_ci
4050e1051a39Sopenharmony_ci/*
4051e1051a39Sopenharmony_ci * Test that a server attempting to read early data can handle a connection
4052e1051a39Sopenharmony_ci * from a client where an HRR occurs and correctly fails if early_data is sent
4053e1051a39Sopenharmony_ci * after the HRR
4054e1051a39Sopenharmony_ci */
4055e1051a39Sopenharmony_cistatic int test_early_data_skip_hrr_fail(int idx)
4056e1051a39Sopenharmony_ci{
4057e1051a39Sopenharmony_ci    return early_data_skip_helper(2,
4058e1051a39Sopenharmony_ci                                  idx % OSSL_NELEM(ciphersuites),
4059e1051a39Sopenharmony_ci                                  idx / OSSL_NELEM(ciphersuites));
4060e1051a39Sopenharmony_ci}
4061e1051a39Sopenharmony_ci
4062e1051a39Sopenharmony_ci/*
4063e1051a39Sopenharmony_ci * Test that a server attempting to read early data will abort if it tries to
4064e1051a39Sopenharmony_ci * skip over too much.
4065e1051a39Sopenharmony_ci */
4066e1051a39Sopenharmony_cistatic int test_early_data_skip_abort(int idx)
4067e1051a39Sopenharmony_ci{
4068e1051a39Sopenharmony_ci    return early_data_skip_helper(3,
4069e1051a39Sopenharmony_ci                                  idx % OSSL_NELEM(ciphersuites),
4070e1051a39Sopenharmony_ci                                  idx / OSSL_NELEM(ciphersuites));
4071e1051a39Sopenharmony_ci}
4072e1051a39Sopenharmony_ci
4073e1051a39Sopenharmony_ci/*
4074e1051a39Sopenharmony_ci * Test that a server attempting to read early data can handle a connection
4075e1051a39Sopenharmony_ci * from a client that doesn't send any.
4076e1051a39Sopenharmony_ci */
4077e1051a39Sopenharmony_cistatic int test_early_data_not_sent(int idx)
4078e1051a39Sopenharmony_ci{
4079e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4080e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4081e1051a39Sopenharmony_ci    int testresult = 0;
4082e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
4083e1051a39Sopenharmony_ci    unsigned char buf[20];
4084e1051a39Sopenharmony_ci    size_t readbytes, written;
4085e1051a39Sopenharmony_ci
4086e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4087e1051a39Sopenharmony_ci                                        &serverssl, &sess, idx,
4088e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
4089e1051a39Sopenharmony_ci        goto end;
4090e1051a39Sopenharmony_ci
4091e1051a39Sopenharmony_ci    /* Write some data - should block due to handshake with server */
4092e1051a39Sopenharmony_ci    SSL_set_connect_state(clientssl);
4093e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4094e1051a39Sopenharmony_ci        goto end;
4095e1051a39Sopenharmony_ci
4096e1051a39Sopenharmony_ci    /* Server should detect that early data has not been sent */
4097e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4098e1051a39Sopenharmony_ci                                         &readbytes),
4099e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_FINISH)
4100e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, 0)
4101e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4102e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_NOT_SENT)
4103e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4104e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_NOT_SENT))
4105e1051a39Sopenharmony_ci        goto end;
4106e1051a39Sopenharmony_ci
4107e1051a39Sopenharmony_ci    /* Continue writing the message we started earlier */
4108e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4109e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1))
4110e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4111e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4112e1051a39Sopenharmony_ci            || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
4113e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2)))
4114e1051a39Sopenharmony_ci        goto end;
4115e1051a39Sopenharmony_ci
4116e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
4117e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4118e1051a39Sopenharmony_ci        goto end;
4119e1051a39Sopenharmony_ci
4120e1051a39Sopenharmony_ci    testresult = 1;
4121e1051a39Sopenharmony_ci
4122e1051a39Sopenharmony_ci end:
4123e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
4124e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4125e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4126e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4127e1051a39Sopenharmony_ci    SSL_free(serverssl);
4128e1051a39Sopenharmony_ci    SSL_free(clientssl);
4129e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4130e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4131e1051a39Sopenharmony_ci    return testresult;
4132e1051a39Sopenharmony_ci}
4133e1051a39Sopenharmony_ci
4134e1051a39Sopenharmony_cistatic const char *servalpn;
4135e1051a39Sopenharmony_ci
4136e1051a39Sopenharmony_cistatic int alpn_select_cb(SSL *ssl, const unsigned char **out,
4137e1051a39Sopenharmony_ci                          unsigned char *outlen, const unsigned char *in,
4138e1051a39Sopenharmony_ci                          unsigned int inlen, void *arg)
4139e1051a39Sopenharmony_ci{
4140e1051a39Sopenharmony_ci    unsigned int protlen = 0;
4141e1051a39Sopenharmony_ci    const unsigned char *prot;
4142e1051a39Sopenharmony_ci
4143e1051a39Sopenharmony_ci    for (prot = in; prot < in + inlen; prot += protlen) {
4144e1051a39Sopenharmony_ci        protlen = *prot++;
4145e1051a39Sopenharmony_ci        if (in + inlen < prot + protlen)
4146e1051a39Sopenharmony_ci            return SSL_TLSEXT_ERR_NOACK;
4147e1051a39Sopenharmony_ci
4148e1051a39Sopenharmony_ci        if (protlen == strlen(servalpn)
4149e1051a39Sopenharmony_ci                && memcmp(prot, servalpn, protlen) == 0) {
4150e1051a39Sopenharmony_ci            *out = prot;
4151e1051a39Sopenharmony_ci            *outlen = protlen;
4152e1051a39Sopenharmony_ci            return SSL_TLSEXT_ERR_OK;
4153e1051a39Sopenharmony_ci        }
4154e1051a39Sopenharmony_ci    }
4155e1051a39Sopenharmony_ci
4156e1051a39Sopenharmony_ci    return SSL_TLSEXT_ERR_NOACK;
4157e1051a39Sopenharmony_ci}
4158e1051a39Sopenharmony_ci
4159e1051a39Sopenharmony_ci/* Test that a PSK can be used to send early_data */
4160e1051a39Sopenharmony_cistatic int test_early_data_psk(int idx)
4161e1051a39Sopenharmony_ci{
4162e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4163e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4164e1051a39Sopenharmony_ci    int testresult = 0;
4165e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
4166e1051a39Sopenharmony_ci    unsigned char alpnlist[] = {
4167e1051a39Sopenharmony_ci        0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
4168e1051a39Sopenharmony_ci        'l', 'p', 'n'
4169e1051a39Sopenharmony_ci    };
4170e1051a39Sopenharmony_ci#define GOODALPNLEN     9
4171e1051a39Sopenharmony_ci#define BADALPNLEN      8
4172e1051a39Sopenharmony_ci#define GOODALPN        (alpnlist)
4173e1051a39Sopenharmony_ci#define BADALPN         (alpnlist + GOODALPNLEN)
4174e1051a39Sopenharmony_ci    int err = 0;
4175e1051a39Sopenharmony_ci    unsigned char buf[20];
4176e1051a39Sopenharmony_ci    size_t readbytes, written;
4177e1051a39Sopenharmony_ci    int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
4178e1051a39Sopenharmony_ci    int edstatus = SSL_EARLY_DATA_ACCEPTED;
4179e1051a39Sopenharmony_ci
4180e1051a39Sopenharmony_ci    /* We always set this up with a final parameter of "2" for PSK */
4181e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4182e1051a39Sopenharmony_ci                                        &serverssl, &sess, 2,
4183e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
4184e1051a39Sopenharmony_ci        goto end;
4185e1051a39Sopenharmony_ci
4186e1051a39Sopenharmony_ci    servalpn = "goodalpn";
4187e1051a39Sopenharmony_ci
4188e1051a39Sopenharmony_ci    /*
4189e1051a39Sopenharmony_ci     * Note: There is no test for inconsistent SNI with late client detection.
4190e1051a39Sopenharmony_ci     * This is because servers do not acknowledge SNI even if they are using
4191e1051a39Sopenharmony_ci     * it in a resumption handshake - so it is not actually possible for a
4192e1051a39Sopenharmony_ci     * client to detect a problem.
4193e1051a39Sopenharmony_ci     */
4194e1051a39Sopenharmony_ci    switch (idx) {
4195e1051a39Sopenharmony_ci    case 0:
4196e1051a39Sopenharmony_ci        /* Set inconsistent SNI (early client detection) */
4197e1051a39Sopenharmony_ci        err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
4198e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4199e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
4200e1051a39Sopenharmony_ci            goto end;
4201e1051a39Sopenharmony_ci        break;
4202e1051a39Sopenharmony_ci
4203e1051a39Sopenharmony_ci    case 1:
4204e1051a39Sopenharmony_ci        /* Set inconsistent ALPN (early client detection) */
4205e1051a39Sopenharmony_ci        err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
4206e1051a39Sopenharmony_ci        /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
4207e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
4208e1051a39Sopenharmony_ci                                                      GOODALPNLEN))
4209e1051a39Sopenharmony_ci                || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
4210e1051a39Sopenharmony_ci                                                   BADALPNLEN)))
4211e1051a39Sopenharmony_ci            goto end;
4212e1051a39Sopenharmony_ci        break;
4213e1051a39Sopenharmony_ci
4214e1051a39Sopenharmony_ci    case 2:
4215e1051a39Sopenharmony_ci        /*
4216e1051a39Sopenharmony_ci         * Set invalid protocol version. Technically this affects PSKs without
4217e1051a39Sopenharmony_ci         * early_data too, but we test it here because it is similar to the
4218e1051a39Sopenharmony_ci         * SNI/ALPN consistency tests.
4219e1051a39Sopenharmony_ci         */
4220e1051a39Sopenharmony_ci        err = SSL_R_BAD_PSK;
4221e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
4222e1051a39Sopenharmony_ci            goto end;
4223e1051a39Sopenharmony_ci        break;
4224e1051a39Sopenharmony_ci
4225e1051a39Sopenharmony_ci    case 3:
4226e1051a39Sopenharmony_ci        /*
4227e1051a39Sopenharmony_ci         * Set inconsistent SNI (server side). In this case the connection
4228e1051a39Sopenharmony_ci         * will succeed and accept early_data. In TLSv1.3 on the server side SNI
4229e1051a39Sopenharmony_ci         * is associated with each handshake - not the session. Therefore it
4230e1051a39Sopenharmony_ci         * should not matter that we used a different server name last time.
4231e1051a39Sopenharmony_ci         */
4232e1051a39Sopenharmony_ci        SSL_SESSION_free(serverpsk);
4233e1051a39Sopenharmony_ci        serverpsk = SSL_SESSION_dup(clientpsk);
4234e1051a39Sopenharmony_ci        if (!TEST_ptr(serverpsk)
4235e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
4236e1051a39Sopenharmony_ci            goto end;
4237e1051a39Sopenharmony_ci        /* Fall through */
4238e1051a39Sopenharmony_ci    case 4:
4239e1051a39Sopenharmony_ci        /* Set consistent SNI */
4240e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
4241e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
4242e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
4243e1051a39Sopenharmony_ci                                hostname_cb)))
4244e1051a39Sopenharmony_ci            goto end;
4245e1051a39Sopenharmony_ci        break;
4246e1051a39Sopenharmony_ci
4247e1051a39Sopenharmony_ci    case 5:
4248e1051a39Sopenharmony_ci        /*
4249e1051a39Sopenharmony_ci         * Set inconsistent ALPN (server detected). In this case the connection
4250e1051a39Sopenharmony_ci         * will succeed but reject early_data.
4251e1051a39Sopenharmony_ci         */
4252e1051a39Sopenharmony_ci        servalpn = "badalpn";
4253e1051a39Sopenharmony_ci        edstatus = SSL_EARLY_DATA_REJECTED;
4254e1051a39Sopenharmony_ci        readearlyres = SSL_READ_EARLY_DATA_FINISH;
4255e1051a39Sopenharmony_ci        /* Fall through */
4256e1051a39Sopenharmony_ci    case 6:
4257e1051a39Sopenharmony_ci        /*
4258e1051a39Sopenharmony_ci         * Set consistent ALPN.
4259e1051a39Sopenharmony_ci         * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
4260e1051a39Sopenharmony_ci         * accepts a list of protos (each one length prefixed).
4261e1051a39Sopenharmony_ci         * SSL_set1_alpn_selected accepts a single protocol (not length
4262e1051a39Sopenharmony_ci         * prefixed)
4263e1051a39Sopenharmony_ci         */
4264e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
4265e1051a39Sopenharmony_ci                                                      GOODALPNLEN - 1))
4266e1051a39Sopenharmony_ci                || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
4267e1051a39Sopenharmony_ci                                                   GOODALPNLEN)))
4268e1051a39Sopenharmony_ci            goto end;
4269e1051a39Sopenharmony_ci
4270e1051a39Sopenharmony_ci        SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4271e1051a39Sopenharmony_ci        break;
4272e1051a39Sopenharmony_ci
4273e1051a39Sopenharmony_ci    case 7:
4274e1051a39Sopenharmony_ci        /* Set inconsistent ALPN (late client detection) */
4275e1051a39Sopenharmony_ci        SSL_SESSION_free(serverpsk);
4276e1051a39Sopenharmony_ci        serverpsk = SSL_SESSION_dup(clientpsk);
4277e1051a39Sopenharmony_ci        if (!TEST_ptr(serverpsk)
4278e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
4279e1051a39Sopenharmony_ci                                                             BADALPN + 1,
4280e1051a39Sopenharmony_ci                                                             BADALPNLEN - 1))
4281e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
4282e1051a39Sopenharmony_ci                                                             GOODALPN + 1,
4283e1051a39Sopenharmony_ci                                                             GOODALPNLEN - 1))
4284e1051a39Sopenharmony_ci                || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
4285e1051a39Sopenharmony_ci                                                   sizeof(alpnlist))))
4286e1051a39Sopenharmony_ci            goto end;
4287e1051a39Sopenharmony_ci        SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
4288e1051a39Sopenharmony_ci        edstatus = SSL_EARLY_DATA_ACCEPTED;
4289e1051a39Sopenharmony_ci        readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
4290e1051a39Sopenharmony_ci        /* SSL_connect() call should fail */
4291e1051a39Sopenharmony_ci        connectres = -1;
4292e1051a39Sopenharmony_ci        break;
4293e1051a39Sopenharmony_ci
4294e1051a39Sopenharmony_ci    default:
4295e1051a39Sopenharmony_ci        TEST_error("Bad test index");
4296e1051a39Sopenharmony_ci        goto end;
4297e1051a39Sopenharmony_ci    }
4298e1051a39Sopenharmony_ci
4299e1051a39Sopenharmony_ci    SSL_set_connect_state(clientssl);
4300e1051a39Sopenharmony_ci    if (err != 0) {
4301e1051a39Sopenharmony_ci        if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4302e1051a39Sopenharmony_ci                                            &written))
4303e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
4304e1051a39Sopenharmony_ci                || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
4305e1051a39Sopenharmony_ci            goto end;
4306e1051a39Sopenharmony_ci    } else {
4307e1051a39Sopenharmony_ci        if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4308e1051a39Sopenharmony_ci                                            &written)))
4309e1051a39Sopenharmony_ci            goto end;
4310e1051a39Sopenharmony_ci
4311e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4312e1051a39Sopenharmony_ci                                             &readbytes), readearlyres)
4313e1051a39Sopenharmony_ci                || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
4314e1051a39Sopenharmony_ci                    && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
4315e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
4316e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_connect(clientssl), connectres))
4317e1051a39Sopenharmony_ci            goto end;
4318e1051a39Sopenharmony_ci    }
4319e1051a39Sopenharmony_ci
4320e1051a39Sopenharmony_ci    testresult = 1;
4321e1051a39Sopenharmony_ci
4322e1051a39Sopenharmony_ci end:
4323e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
4324e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4325e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4326e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4327e1051a39Sopenharmony_ci    SSL_free(serverssl);
4328e1051a39Sopenharmony_ci    SSL_free(clientssl);
4329e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4330e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4331e1051a39Sopenharmony_ci    return testresult;
4332e1051a39Sopenharmony_ci}
4333e1051a39Sopenharmony_ci
4334e1051a39Sopenharmony_ci/*
4335e1051a39Sopenharmony_ci * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
4336e1051a39Sopenharmony_ci * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
4337e1051a39Sopenharmony_ci * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
4338e1051a39Sopenharmony_ci * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4339e1051a39Sopenharmony_ci * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
4340e1051a39Sopenharmony_ci * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
4341e1051a39Sopenharmony_ci */
4342e1051a39Sopenharmony_cistatic int test_early_data_psk_with_all_ciphers(int idx)
4343e1051a39Sopenharmony_ci{
4344e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4345e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4346e1051a39Sopenharmony_ci    int testresult = 0;
4347e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
4348e1051a39Sopenharmony_ci    unsigned char buf[20];
4349e1051a39Sopenharmony_ci    size_t readbytes, written;
4350e1051a39Sopenharmony_ci    const SSL_CIPHER *cipher;
4351e1051a39Sopenharmony_ci    const char *cipher_str[] = {
4352e1051a39Sopenharmony_ci        TLS1_3_RFC_AES_128_GCM_SHA256,
4353e1051a39Sopenharmony_ci        TLS1_3_RFC_AES_256_GCM_SHA384,
4354e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4355e1051a39Sopenharmony_ci        TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
4356e1051a39Sopenharmony_ci# else
4357e1051a39Sopenharmony_ci        NULL,
4358e1051a39Sopenharmony_ci# endif
4359e1051a39Sopenharmony_ci        TLS1_3_RFC_AES_128_CCM_SHA256,
4360e1051a39Sopenharmony_ci        TLS1_3_RFC_AES_128_CCM_8_SHA256
4361e1051a39Sopenharmony_ci    };
4362e1051a39Sopenharmony_ci    const unsigned char *cipher_bytes[] = {
4363e1051a39Sopenharmony_ci        TLS13_AES_128_GCM_SHA256_BYTES,
4364e1051a39Sopenharmony_ci        TLS13_AES_256_GCM_SHA384_BYTES,
4365e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4366e1051a39Sopenharmony_ci        TLS13_CHACHA20_POLY1305_SHA256_BYTES,
4367e1051a39Sopenharmony_ci# else
4368e1051a39Sopenharmony_ci        NULL,
4369e1051a39Sopenharmony_ci# endif
4370e1051a39Sopenharmony_ci        TLS13_AES_128_CCM_SHA256_BYTES,
4371e1051a39Sopenharmony_ci        TLS13_AES_128_CCM_8_SHA256_BYTES
4372e1051a39Sopenharmony_ci    };
4373e1051a39Sopenharmony_ci
4374e1051a39Sopenharmony_ci    if (cipher_str[idx] == NULL)
4375e1051a39Sopenharmony_ci        return 1;
4376e1051a39Sopenharmony_ci    /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
4377e1051a39Sopenharmony_ci    if (idx == 2 && is_fips == 1)
4378e1051a39Sopenharmony_ci        return 1;
4379e1051a39Sopenharmony_ci
4380e1051a39Sopenharmony_ci    /* We always set this up with a final parameter of "2" for PSK */
4381e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4382e1051a39Sopenharmony_ci                                        &serverssl, &sess, 2,
4383e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
4384e1051a39Sopenharmony_ci        goto end;
4385e1051a39Sopenharmony_ci
4386e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
4387e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
4388e1051a39Sopenharmony_ci        goto end;
4389e1051a39Sopenharmony_ci
4390e1051a39Sopenharmony_ci    /*
4391e1051a39Sopenharmony_ci     * 'setupearly_data_test' creates only one instance of SSL_SESSION
4392e1051a39Sopenharmony_ci     * and assigns to both client and server with incremented reference
4393e1051a39Sopenharmony_ci     * and the same instance is updated in 'sess'.
4394e1051a39Sopenharmony_ci     * So updating ciphersuite in 'sess' which will get reflected in
4395e1051a39Sopenharmony_ci     * PSK handshake using psk use sess and find sess cb.
4396e1051a39Sopenharmony_ci     */
4397e1051a39Sopenharmony_ci    cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
4398e1051a39Sopenharmony_ci    if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
4399e1051a39Sopenharmony_ci        goto end;
4400e1051a39Sopenharmony_ci
4401e1051a39Sopenharmony_ci    SSL_set_connect_state(clientssl);
4402e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4403e1051a39Sopenharmony_ci                                        &written)))
4404e1051a39Sopenharmony_ci        goto end;
4405e1051a39Sopenharmony_ci
4406e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4407e1051a39Sopenharmony_ci                                         &readbytes),
4408e1051a39Sopenharmony_ci                                         SSL_READ_EARLY_DATA_SUCCESS)
4409e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4410e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4411e1051a39Sopenharmony_ci                                                      SSL_EARLY_DATA_ACCEPTED)
4412e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_connect(clientssl), 1)
4413e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_accept(serverssl), 1))
4414e1051a39Sopenharmony_ci        goto end;
4415e1051a39Sopenharmony_ci
4416e1051a39Sopenharmony_ci    /* Send some normal data from client to server */
4417e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4418e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2)))
4419e1051a39Sopenharmony_ci        goto end;
4420e1051a39Sopenharmony_ci
4421e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4422e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4423e1051a39Sopenharmony_ci        goto end;
4424e1051a39Sopenharmony_ci
4425e1051a39Sopenharmony_ci    testresult = 1;
4426e1051a39Sopenharmony_ci end:
4427e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
4428e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4429e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4430e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4431e1051a39Sopenharmony_ci    if (clientssl != NULL)
4432e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
4433e1051a39Sopenharmony_ci    if (serverssl != NULL)
4434e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
4435e1051a39Sopenharmony_ci    SSL_free(serverssl);
4436e1051a39Sopenharmony_ci    SSL_free(clientssl);
4437e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4438e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4439e1051a39Sopenharmony_ci    return testresult;
4440e1051a39Sopenharmony_ci}
4441e1051a39Sopenharmony_ci
4442e1051a39Sopenharmony_ci/*
4443e1051a39Sopenharmony_ci * Test that a server that doesn't try to read early data can handle a
4444e1051a39Sopenharmony_ci * client sending some.
4445e1051a39Sopenharmony_ci */
4446e1051a39Sopenharmony_cistatic int test_early_data_not_expected(int idx)
4447e1051a39Sopenharmony_ci{
4448e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4449e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4450e1051a39Sopenharmony_ci    int testresult = 0;
4451e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
4452e1051a39Sopenharmony_ci    unsigned char buf[20];
4453e1051a39Sopenharmony_ci    size_t readbytes, written;
4454e1051a39Sopenharmony_ci
4455e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4456e1051a39Sopenharmony_ci                                        &serverssl, &sess, idx,
4457e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
4458e1051a39Sopenharmony_ci        goto end;
4459e1051a39Sopenharmony_ci
4460e1051a39Sopenharmony_ci    /* Write some early data */
4461e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
4462e1051a39Sopenharmony_ci                                        &written)))
4463e1051a39Sopenharmony_ci        goto end;
4464e1051a39Sopenharmony_ci
4465e1051a39Sopenharmony_ci    /*
4466e1051a39Sopenharmony_ci     * Server should skip over early data and then block waiting for client to
4467e1051a39Sopenharmony_ci     * continue handshake
4468e1051a39Sopenharmony_ci     */
4469e1051a39Sopenharmony_ci    if (!TEST_int_le(SSL_accept(serverssl), 0)
4470e1051a39Sopenharmony_ci     || !TEST_int_gt(SSL_connect(clientssl), 0)
4471e1051a39Sopenharmony_ci     || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4472e1051a39Sopenharmony_ci                     SSL_EARLY_DATA_REJECTED)
4473e1051a39Sopenharmony_ci     || !TEST_int_gt(SSL_accept(serverssl), 0)
4474e1051a39Sopenharmony_ci     || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4475e1051a39Sopenharmony_ci                     SSL_EARLY_DATA_REJECTED))
4476e1051a39Sopenharmony_ci        goto end;
4477e1051a39Sopenharmony_ci
4478e1051a39Sopenharmony_ci    /* Send some normal data from client to server */
4479e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
4480e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2)))
4481e1051a39Sopenharmony_ci        goto end;
4482e1051a39Sopenharmony_ci
4483e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4484e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4485e1051a39Sopenharmony_ci        goto end;
4486e1051a39Sopenharmony_ci
4487e1051a39Sopenharmony_ci    testresult = 1;
4488e1051a39Sopenharmony_ci
4489e1051a39Sopenharmony_ci end:
4490e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
4491e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4492e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4493e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4494e1051a39Sopenharmony_ci    SSL_free(serverssl);
4495e1051a39Sopenharmony_ci    SSL_free(clientssl);
4496e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4497e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4498e1051a39Sopenharmony_ci    return testresult;
4499e1051a39Sopenharmony_ci}
4500e1051a39Sopenharmony_ci
4501e1051a39Sopenharmony_ci
4502e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
4503e1051a39Sopenharmony_ci/*
4504e1051a39Sopenharmony_ci * Test that a server attempting to read early data can handle a connection
4505e1051a39Sopenharmony_ci * from a TLSv1.2 client.
4506e1051a39Sopenharmony_ci */
4507e1051a39Sopenharmony_cistatic int test_early_data_tls1_2(int idx)
4508e1051a39Sopenharmony_ci{
4509e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4510e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4511e1051a39Sopenharmony_ci    int testresult = 0;
4512e1051a39Sopenharmony_ci    unsigned char buf[20];
4513e1051a39Sopenharmony_ci    size_t readbytes, written;
4514e1051a39Sopenharmony_ci
4515e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
4516e1051a39Sopenharmony_ci                                        &serverssl, NULL, idx,
4517e1051a39Sopenharmony_ci                                        SHA384_DIGEST_LENGTH)))
4518e1051a39Sopenharmony_ci        goto end;
4519e1051a39Sopenharmony_ci
4520e1051a39Sopenharmony_ci    /* Write some data - should block due to handshake with server */
4521e1051a39Sopenharmony_ci    SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
4522e1051a39Sopenharmony_ci    SSL_set_connect_state(clientssl);
4523e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
4524e1051a39Sopenharmony_ci        goto end;
4525e1051a39Sopenharmony_ci
4526e1051a39Sopenharmony_ci    /*
4527e1051a39Sopenharmony_ci     * Server should do TLSv1.2 handshake. First it will block waiting for more
4528e1051a39Sopenharmony_ci     * messages from client after ServerDone. Then SSL_read_early_data should
4529e1051a39Sopenharmony_ci     * finish and detect that early data has not been sent
4530e1051a39Sopenharmony_ci     */
4531e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4532e1051a39Sopenharmony_ci                                         &readbytes),
4533e1051a39Sopenharmony_ci                     SSL_READ_EARLY_DATA_ERROR))
4534e1051a39Sopenharmony_ci        goto end;
4535e1051a39Sopenharmony_ci
4536e1051a39Sopenharmony_ci    /*
4537e1051a39Sopenharmony_ci     * Continue writing the message we started earlier. Will still block waiting
4538e1051a39Sopenharmony_ci     * for the CCS/Finished from server
4539e1051a39Sopenharmony_ci     */
4540e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4541e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
4542e1051a39Sopenharmony_ci                                                &readbytes),
4543e1051a39Sopenharmony_ci                            SSL_READ_EARLY_DATA_FINISH)
4544e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, 0)
4545e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
4546e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_NOT_SENT))
4547e1051a39Sopenharmony_ci        goto end;
4548e1051a39Sopenharmony_ci
4549e1051a39Sopenharmony_ci    /* Continue writing the message we started earlier */
4550e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
4551e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG1))
4552e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(clientssl),
4553e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_NOT_SENT)
4554e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
4555e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
4556e1051a39Sopenharmony_ci            || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
4557e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, strlen(MSG2))
4558e1051a39Sopenharmony_ci            || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
4559e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
4560e1051a39Sopenharmony_ci        goto end;
4561e1051a39Sopenharmony_ci
4562e1051a39Sopenharmony_ci    testresult = 1;
4563e1051a39Sopenharmony_ci
4564e1051a39Sopenharmony_ci end:
4565e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
4566e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
4567e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
4568e1051a39Sopenharmony_ci    SSL_free(serverssl);
4569e1051a39Sopenharmony_ci    SSL_free(clientssl);
4570e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4571e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4572e1051a39Sopenharmony_ci
4573e1051a39Sopenharmony_ci    return testresult;
4574e1051a39Sopenharmony_ci}
4575e1051a39Sopenharmony_ci# endif /* OPENSSL_NO_TLS1_2 */
4576e1051a39Sopenharmony_ci
4577e1051a39Sopenharmony_ci/*
4578e1051a39Sopenharmony_ci * Test configuring the TLSv1.3 ciphersuites
4579e1051a39Sopenharmony_ci *
4580e1051a39Sopenharmony_ci * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
4581e1051a39Sopenharmony_ci * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
4582e1051a39Sopenharmony_ci * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
4583e1051a39Sopenharmony_ci * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
4584e1051a39Sopenharmony_ci * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4585e1051a39Sopenharmony_ci * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
4586e1051a39Sopenharmony_ci * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
4587e1051a39Sopenharmony_ci * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
4588e1051a39Sopenharmony_ci * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
4589e1051a39Sopenharmony_ci * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
4590e1051a39Sopenharmony_ci */
4591e1051a39Sopenharmony_cistatic int test_set_ciphersuite(int idx)
4592e1051a39Sopenharmony_ci{
4593e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4594e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4595e1051a39Sopenharmony_ci    int testresult = 0;
4596e1051a39Sopenharmony_ci
4597e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4598e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
4599e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
4600e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4601e1051a39Sopenharmony_ci                           "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
4602e1051a39Sopenharmony_ci        goto end;
4603e1051a39Sopenharmony_ci
4604e1051a39Sopenharmony_ci    if (idx >=4 && idx <= 7) {
4605e1051a39Sopenharmony_ci        /* SSL_CTX explicit cipher list */
4606e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
4607e1051a39Sopenharmony_ci            goto end;
4608e1051a39Sopenharmony_ci    }
4609e1051a39Sopenharmony_ci
4610e1051a39Sopenharmony_ci    if (idx == 0 || idx == 4) {
4611e1051a39Sopenharmony_ci        /* Default ciphersuite */
4612e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4613e1051a39Sopenharmony_ci                                                "TLS_AES_128_GCM_SHA256")))
4614e1051a39Sopenharmony_ci            goto end;
4615e1051a39Sopenharmony_ci    } else if (idx == 1 || idx == 5) {
4616e1051a39Sopenharmony_ci        /* Non default ciphersuite */
4617e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4618e1051a39Sopenharmony_ci                                                "TLS_AES_128_CCM_SHA256")))
4619e1051a39Sopenharmony_ci            goto end;
4620e1051a39Sopenharmony_ci    }
4621e1051a39Sopenharmony_ci
4622e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4623e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL)))
4624e1051a39Sopenharmony_ci        goto end;
4625e1051a39Sopenharmony_ci
4626e1051a39Sopenharmony_ci    if (idx == 8 || idx == 9) {
4627e1051a39Sopenharmony_ci        /* SSL explicit cipher list */
4628e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
4629e1051a39Sopenharmony_ci            goto end;
4630e1051a39Sopenharmony_ci    }
4631e1051a39Sopenharmony_ci
4632e1051a39Sopenharmony_ci    if (idx == 2 || idx == 6 || idx == 8) {
4633e1051a39Sopenharmony_ci        /* Default ciphersuite */
4634e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_ciphersuites(clientssl,
4635e1051a39Sopenharmony_ci                                            "TLS_AES_128_GCM_SHA256")))
4636e1051a39Sopenharmony_ci            goto end;
4637e1051a39Sopenharmony_ci    } else if (idx == 3 || idx == 7 || idx == 9) {
4638e1051a39Sopenharmony_ci        /* Non default ciphersuite */
4639e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_ciphersuites(clientssl,
4640e1051a39Sopenharmony_ci                                            "TLS_AES_128_CCM_SHA256")))
4641e1051a39Sopenharmony_ci            goto end;
4642e1051a39Sopenharmony_ci    }
4643e1051a39Sopenharmony_ci
4644e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4645e1051a39Sopenharmony_ci        goto end;
4646e1051a39Sopenharmony_ci
4647e1051a39Sopenharmony_ci    testresult = 1;
4648e1051a39Sopenharmony_ci
4649e1051a39Sopenharmony_ci end:
4650e1051a39Sopenharmony_ci    SSL_free(serverssl);
4651e1051a39Sopenharmony_ci    SSL_free(clientssl);
4652e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4653e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4654e1051a39Sopenharmony_ci
4655e1051a39Sopenharmony_ci    return testresult;
4656e1051a39Sopenharmony_ci}
4657e1051a39Sopenharmony_ci
4658e1051a39Sopenharmony_cistatic int test_ciphersuite_change(void)
4659e1051a39Sopenharmony_ci{
4660e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
4661e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
4662e1051a39Sopenharmony_ci    SSL_SESSION *clntsess = NULL;
4663e1051a39Sopenharmony_ci    int testresult = 0;
4664e1051a39Sopenharmony_ci    const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
4665e1051a39Sopenharmony_ci
4666e1051a39Sopenharmony_ci    /* Create a session based on SHA-256 */
4667e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4668e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
4669e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
4670e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4671e1051a39Sopenharmony_ci                                                   "TLS_AES_128_GCM_SHA256:"
4672e1051a39Sopenharmony_ci                                                   "TLS_AES_256_GCM_SHA384:"
4673e1051a39Sopenharmony_ci                                                   "TLS_AES_128_CCM_SHA256"))
4674e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4675e1051a39Sopenharmony_ci                                                   "TLS_AES_128_GCM_SHA256"))
4676e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4677e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL))
4678e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
4679e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
4680e1051a39Sopenharmony_ci        goto end;
4681e1051a39Sopenharmony_ci
4682e1051a39Sopenharmony_ci    clntsess = SSL_get1_session(clientssl);
4683e1051a39Sopenharmony_ci    /* Save for later */
4684e1051a39Sopenharmony_ci    aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
4685e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
4686e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
4687e1051a39Sopenharmony_ci    SSL_free(serverssl);
4688e1051a39Sopenharmony_ci    SSL_free(clientssl);
4689e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
4690e1051a39Sopenharmony_ci
4691e1051a39Sopenharmony_ci    /* Check we can resume a session with a different SHA-256 ciphersuite */
4692e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4693e1051a39Sopenharmony_ci                                            "TLS_AES_128_CCM_SHA256"))
4694e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4695e1051a39Sopenharmony_ci                                             &clientssl, NULL, NULL))
4696e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, clntsess))
4697e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
4698e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
4699e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
4700e1051a39Sopenharmony_ci        goto end;
4701e1051a39Sopenharmony_ci
4702e1051a39Sopenharmony_ci    SSL_SESSION_free(clntsess);
4703e1051a39Sopenharmony_ci    clntsess = SSL_get1_session(clientssl);
4704e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
4705e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
4706e1051a39Sopenharmony_ci    SSL_free(serverssl);
4707e1051a39Sopenharmony_ci    SSL_free(clientssl);
4708e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
4709e1051a39Sopenharmony_ci
4710e1051a39Sopenharmony_ci    /*
4711e1051a39Sopenharmony_ci     * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
4712e1051a39Sopenharmony_ci     * succeeds but does not resume.
4713e1051a39Sopenharmony_ci     */
4714e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4715e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4716e1051a39Sopenharmony_ci                                             NULL, NULL))
4717e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, clntsess))
4718e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
4719e1051a39Sopenharmony_ci                                                SSL_ERROR_SSL))
4720e1051a39Sopenharmony_ci            || !TEST_false(SSL_session_reused(clientssl)))
4721e1051a39Sopenharmony_ci        goto end;
4722e1051a39Sopenharmony_ci
4723e1051a39Sopenharmony_ci    SSL_SESSION_free(clntsess);
4724e1051a39Sopenharmony_ci    clntsess = NULL;
4725e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
4726e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
4727e1051a39Sopenharmony_ci    SSL_free(serverssl);
4728e1051a39Sopenharmony_ci    SSL_free(clientssl);
4729e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
4730e1051a39Sopenharmony_ci
4731e1051a39Sopenharmony_ci    /* Create a session based on SHA384 */
4732e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
4733e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
4734e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL))
4735e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
4736e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
4737e1051a39Sopenharmony_ci        goto end;
4738e1051a39Sopenharmony_ci
4739e1051a39Sopenharmony_ci    clntsess = SSL_get1_session(clientssl);
4740e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
4741e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
4742e1051a39Sopenharmony_ci    SSL_free(serverssl);
4743e1051a39Sopenharmony_ci    SSL_free(clientssl);
4744e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
4745e1051a39Sopenharmony_ci
4746e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4747e1051a39Sopenharmony_ci                   "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
4748e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4749e1051a39Sopenharmony_ci                                                   "TLS_AES_256_GCM_SHA384"))
4750e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4751e1051a39Sopenharmony_ci                                             NULL, NULL))
4752e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, clntsess))
4753e1051a39Sopenharmony_ci               /*
4754e1051a39Sopenharmony_ci                * We use SSL_ERROR_WANT_READ below so that we can pause the
4755e1051a39Sopenharmony_ci                * connection after the initial ClientHello has been sent to
4756e1051a39Sopenharmony_ci                * enable us to make some session changes.
4757e1051a39Sopenharmony_ci                */
4758e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
4759e1051a39Sopenharmony_ci                                                SSL_ERROR_WANT_READ)))
4760e1051a39Sopenharmony_ci        goto end;
4761e1051a39Sopenharmony_ci
4762e1051a39Sopenharmony_ci    /* Trick the client into thinking this session is for a different digest */
4763e1051a39Sopenharmony_ci    clntsess->cipher = aes_128_gcm_sha256;
4764e1051a39Sopenharmony_ci    clntsess->cipher_id = clntsess->cipher->id;
4765e1051a39Sopenharmony_ci
4766e1051a39Sopenharmony_ci    /*
4767e1051a39Sopenharmony_ci     * Continue the previously started connection. Server has selected a SHA-384
4768e1051a39Sopenharmony_ci     * ciphersuite, but client thinks the session is for SHA-256, so it should
4769e1051a39Sopenharmony_ci     * bail out.
4770e1051a39Sopenharmony_ci     */
4771e1051a39Sopenharmony_ci    if (!TEST_false(create_ssl_connection(serverssl, clientssl,
4772e1051a39Sopenharmony_ci                                                SSL_ERROR_SSL))
4773e1051a39Sopenharmony_ci            || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
4774e1051a39Sopenharmony_ci                            SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
4775e1051a39Sopenharmony_ci        goto end;
4776e1051a39Sopenharmony_ci
4777e1051a39Sopenharmony_ci    testresult = 1;
4778e1051a39Sopenharmony_ci
4779e1051a39Sopenharmony_ci end:
4780e1051a39Sopenharmony_ci    SSL_SESSION_free(clntsess);
4781e1051a39Sopenharmony_ci    SSL_free(serverssl);
4782e1051a39Sopenharmony_ci    SSL_free(clientssl);
4783e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4784e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4785e1051a39Sopenharmony_ci
4786e1051a39Sopenharmony_ci    return testresult;
4787e1051a39Sopenharmony_ci}
4788e1051a39Sopenharmony_ci
4789e1051a39Sopenharmony_ci/*
4790e1051a39Sopenharmony_ci * Test TLSv1.3 Key exchange
4791e1051a39Sopenharmony_ci * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
4792e1051a39Sopenharmony_ci * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
4793e1051a39Sopenharmony_ci * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
4794e1051a39Sopenharmony_ci * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
4795e1051a39Sopenharmony_ci * Test 4 = Test NID_X25519 with TLSv1.3 client and server
4796e1051a39Sopenharmony_ci * Test 5 = Test NID_X448 with TLSv1.3 client and server
4797e1051a39Sopenharmony_ci * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
4798e1051a39Sopenharmony_ci * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
4799e1051a39Sopenharmony_ci * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
4800e1051a39Sopenharmony_ci * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
4801e1051a39Sopenharmony_ci * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
4802e1051a39Sopenharmony_ci * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
4803e1051a39Sopenharmony_ci * Test 12 = Test all ECDHE with TLSv1.2 client and server
4804e1051a39Sopenharmony_ci * Test 13 = Test all FFDHE with TLSv1.2 client and server
4805e1051a39Sopenharmony_ci */
4806e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
4807e1051a39Sopenharmony_cistatic int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
4808e1051a39Sopenharmony_ci                                   NID_secp521r1, NID_X25519, NID_X448};
4809e1051a39Sopenharmony_ci# endif
4810e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
4811e1051a39Sopenharmony_cistatic int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
4812e1051a39Sopenharmony_ci                                   NID_ffdhe6144, NID_ffdhe8192};
4813e1051a39Sopenharmony_ci# endif
4814e1051a39Sopenharmony_cistatic int test_key_exchange(int idx)
4815e1051a39Sopenharmony_ci{
4816e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
4817e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
4818e1051a39Sopenharmony_ci    int testresult = 0;
4819e1051a39Sopenharmony_ci    int kexch_alg;
4820e1051a39Sopenharmony_ci    int *kexch_groups = &kexch_alg;
4821e1051a39Sopenharmony_ci    int kexch_groups_size = 1;
4822e1051a39Sopenharmony_ci    int max_version = TLS1_3_VERSION;
4823e1051a39Sopenharmony_ci    char *kexch_name0 = NULL;
4824e1051a39Sopenharmony_ci
4825e1051a39Sopenharmony_ci    switch (idx) {
4826e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
4827e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
4828e1051a39Sopenharmony_ci        case 12:
4829e1051a39Sopenharmony_ci            max_version = TLS1_2_VERSION;
4830e1051a39Sopenharmony_ci# endif
4831e1051a39Sopenharmony_ci            /* Fall through */
4832e1051a39Sopenharmony_ci        case 0:
4833e1051a39Sopenharmony_ci            kexch_groups = ecdhe_kexch_groups;
4834e1051a39Sopenharmony_ci            kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
4835e1051a39Sopenharmony_ci            kexch_name0 = "secp256r1";
4836e1051a39Sopenharmony_ci            break;
4837e1051a39Sopenharmony_ci        case 1:
4838e1051a39Sopenharmony_ci            kexch_alg = NID_X9_62_prime256v1;
4839e1051a39Sopenharmony_ci            kexch_name0 = "secp256r1";
4840e1051a39Sopenharmony_ci            break;
4841e1051a39Sopenharmony_ci        case 2:
4842e1051a39Sopenharmony_ci            kexch_alg = NID_secp384r1;
4843e1051a39Sopenharmony_ci            kexch_name0 = "secp384r1";
4844e1051a39Sopenharmony_ci            break;
4845e1051a39Sopenharmony_ci        case 3:
4846e1051a39Sopenharmony_ci            kexch_alg = NID_secp521r1;
4847e1051a39Sopenharmony_ci            kexch_name0 = "secp521r1";
4848e1051a39Sopenharmony_ci            break;
4849e1051a39Sopenharmony_ci        case 4:
4850e1051a39Sopenharmony_ci            kexch_alg = NID_X25519;
4851e1051a39Sopenharmony_ci            kexch_name0 = "x25519";
4852e1051a39Sopenharmony_ci            break;
4853e1051a39Sopenharmony_ci        case 5:
4854e1051a39Sopenharmony_ci            kexch_alg = NID_X448;
4855e1051a39Sopenharmony_ci            kexch_name0 = "x448";
4856e1051a39Sopenharmony_ci            break;
4857e1051a39Sopenharmony_ci# endif
4858e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
4859e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
4860e1051a39Sopenharmony_ci        case 13:
4861e1051a39Sopenharmony_ci            max_version = TLS1_2_VERSION;
4862e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe2048";
4863e1051a39Sopenharmony_ci# endif
4864e1051a39Sopenharmony_ci            /* Fall through */
4865e1051a39Sopenharmony_ci        case 6:
4866e1051a39Sopenharmony_ci            kexch_groups = ffdhe_kexch_groups;
4867e1051a39Sopenharmony_ci            kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
4868e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe2048";
4869e1051a39Sopenharmony_ci            break;
4870e1051a39Sopenharmony_ci        case 7:
4871e1051a39Sopenharmony_ci            kexch_alg = NID_ffdhe2048;
4872e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe2048";
4873e1051a39Sopenharmony_ci            break;
4874e1051a39Sopenharmony_ci        case 8:
4875e1051a39Sopenharmony_ci            kexch_alg = NID_ffdhe3072;
4876e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe3072";
4877e1051a39Sopenharmony_ci            break;
4878e1051a39Sopenharmony_ci        case 9:
4879e1051a39Sopenharmony_ci            kexch_alg = NID_ffdhe4096;
4880e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe4096";
4881e1051a39Sopenharmony_ci            break;
4882e1051a39Sopenharmony_ci        case 10:
4883e1051a39Sopenharmony_ci            kexch_alg = NID_ffdhe6144;
4884e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe6144";
4885e1051a39Sopenharmony_ci            break;
4886e1051a39Sopenharmony_ci        case 11:
4887e1051a39Sopenharmony_ci            kexch_alg = NID_ffdhe8192;
4888e1051a39Sopenharmony_ci            kexch_name0 = "ffdhe8192";
4889e1051a39Sopenharmony_ci            break;
4890e1051a39Sopenharmony_ci# endif
4891e1051a39Sopenharmony_ci        default:
4892e1051a39Sopenharmony_ci            /* We're skipping this test */
4893e1051a39Sopenharmony_ci            return 1;
4894e1051a39Sopenharmony_ci    }
4895e1051a39Sopenharmony_ci
4896e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
4897e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION,
4898e1051a39Sopenharmony_ci                                       max_version, &sctx, &cctx, cert,
4899e1051a39Sopenharmony_ci                                       privkey)))
4900e1051a39Sopenharmony_ci        goto end;
4901e1051a39Sopenharmony_ci
4902e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
4903e1051a39Sopenharmony_ci                   TLS1_3_RFC_AES_128_GCM_SHA256)))
4904e1051a39Sopenharmony_ci        goto end;
4905e1051a39Sopenharmony_ci
4906e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
4907e1051a39Sopenharmony_ci                   TLS1_3_RFC_AES_128_GCM_SHA256)))
4908e1051a39Sopenharmony_ci        goto end;
4909e1051a39Sopenharmony_ci
4910e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
4911e1051a39Sopenharmony_ci                   TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4912e1051a39Sopenharmony_ci                   TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
4913e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
4914e1051a39Sopenharmony_ci        goto end;
4915e1051a39Sopenharmony_ci
4916e1051a39Sopenharmony_ci    /*
4917e1051a39Sopenharmony_ci     * Must include an EC ciphersuite so that we send supported groups in
4918e1051a39Sopenharmony_ci     * TLSv1.2
4919e1051a39Sopenharmony_ci     */
4920e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
4921e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
4922e1051a39Sopenharmony_ci                   TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
4923e1051a39Sopenharmony_ci                   TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
4924e1051a39Sopenharmony_ci        goto end;
4925e1051a39Sopenharmony_ci# endif
4926e1051a39Sopenharmony_ci
4927e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
4928e1051a39Sopenharmony_ci                                             NULL, NULL)))
4929e1051a39Sopenharmony_ci        goto end;
4930e1051a39Sopenharmony_ci
4931e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
4932e1051a39Sopenharmony_ci        || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
4933e1051a39Sopenharmony_ci        goto end;
4934e1051a39Sopenharmony_ci
4935e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
4936e1051a39Sopenharmony_ci        goto end;
4937e1051a39Sopenharmony_ci
4938e1051a39Sopenharmony_ci    /*
4939e1051a39Sopenharmony_ci     * If Handshake succeeds the negotiated kexch alg should be the first one in
4940e1051a39Sopenharmony_ci     * configured, except in the case of FFDHE groups (idx 13), which are
4941e1051a39Sopenharmony_ci     * TLSv1.3 only so we expect no shared group to exist.
4942e1051a39Sopenharmony_ci     */
4943e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
4944e1051a39Sopenharmony_ci                     idx == 13 ? 0 : kexch_groups[0]))
4945e1051a39Sopenharmony_ci        goto end;
4946e1051a39Sopenharmony_ci
4947e1051a39Sopenharmony_ci    if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
4948e1051a39Sopenharmony_ci                     kexch_name0))
4949e1051a39Sopenharmony_ci        goto end;
4950e1051a39Sopenharmony_ci
4951e1051a39Sopenharmony_ci    /* We don't implement RFC 7919 named groups for TLS 1.2. */
4952e1051a39Sopenharmony_ci    if (idx != 13) {
4953e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
4954e1051a39Sopenharmony_ci            goto end;
4955e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
4956e1051a39Sopenharmony_ci            goto end;
4957e1051a39Sopenharmony_ci    }
4958e1051a39Sopenharmony_ci
4959e1051a39Sopenharmony_ci    testresult = 1;
4960e1051a39Sopenharmony_ci end:
4961e1051a39Sopenharmony_ci    SSL_free(serverssl);
4962e1051a39Sopenharmony_ci    SSL_free(clientssl);
4963e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
4964e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
4965e1051a39Sopenharmony_ci    return testresult;
4966e1051a39Sopenharmony_ci}
4967e1051a39Sopenharmony_ci
4968e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_TLS1_2) \
4969e1051a39Sopenharmony_ci     && !defined(OPENSSL_NO_EC)  \
4970e1051a39Sopenharmony_ci     && !defined(OPENSSL_NO_DH)
4971e1051a39Sopenharmony_cistatic int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
4972e1051a39Sopenharmony_ci                          int isecdhe, int idx)
4973e1051a39Sopenharmony_ci{
4974e1051a39Sopenharmony_ci    int kexch_alg;
4975e1051a39Sopenharmony_ci    int *kexch_groups = &kexch_alg;
4976e1051a39Sopenharmony_ci    int numec, numff;
4977e1051a39Sopenharmony_ci
4978e1051a39Sopenharmony_ci    numec = OSSL_NELEM(ecdhe_kexch_groups);
4979e1051a39Sopenharmony_ci    numff = OSSL_NELEM(ffdhe_kexch_groups);
4980e1051a39Sopenharmony_ci    if (isecdhe)
4981e1051a39Sopenharmony_ci        kexch_alg = ecdhe_kexch_groups[idx];
4982e1051a39Sopenharmony_ci    else
4983e1051a39Sopenharmony_ci        kexch_alg = ffdhe_kexch_groups[idx];
4984e1051a39Sopenharmony_ci
4985e1051a39Sopenharmony_ci    if (clientmulti) {
4986e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
4987e1051a39Sopenharmony_ci            return 0;
4988e1051a39Sopenharmony_ci        if (isecdhe) {
4989e1051a39Sopenharmony_ci            if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
4990e1051a39Sopenharmony_ci                                           numec)))
4991e1051a39Sopenharmony_ci                return 0;
4992e1051a39Sopenharmony_ci        } else {
4993e1051a39Sopenharmony_ci            if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
4994e1051a39Sopenharmony_ci                                           numff)))
4995e1051a39Sopenharmony_ci                return 0;
4996e1051a39Sopenharmony_ci        }
4997e1051a39Sopenharmony_ci    } else {
4998e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
4999e1051a39Sopenharmony_ci            return 0;
5000e1051a39Sopenharmony_ci        if (isecdhe) {
5001e1051a39Sopenharmony_ci            if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
5002e1051a39Sopenharmony_ci                                           numec)))
5003e1051a39Sopenharmony_ci                return 0;
5004e1051a39Sopenharmony_ci        } else {
5005e1051a39Sopenharmony_ci            if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
5006e1051a39Sopenharmony_ci                                           numff)))
5007e1051a39Sopenharmony_ci                return 0;
5008e1051a39Sopenharmony_ci        }
5009e1051a39Sopenharmony_ci    }
5010e1051a39Sopenharmony_ci    return 1;
5011e1051a39Sopenharmony_ci}
5012e1051a39Sopenharmony_ci
5013e1051a39Sopenharmony_ci/*-
5014e1051a39Sopenharmony_ci * Test the SSL_get_negotiated_group() API across a battery of scenarios.
5015e1051a39Sopenharmony_ci * Run through both the ECDHE and FFDHE group lists used in the previous
5016e1051a39Sopenharmony_ci * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
5017e1051a39Sopenharmony_ci * confirming the expected result; then perform a resumption handshake
5018e1051a39Sopenharmony_ci * while offering the same group list, and another resumption handshake
5019e1051a39Sopenharmony_ci * offering a different group list.  The returned value should be the
5020e1051a39Sopenharmony_ci * negotiated group for the initial handshake; for TLS 1.3 resumption
5021e1051a39Sopenharmony_ci * handshakes the returned value will be negotiated on the resumption
5022e1051a39Sopenharmony_ci * handshake itself, but for TLS 1.2 resumption handshakes the value will
5023e1051a39Sopenharmony_ci * be cached in the session from the original handshake, regardless of what
5024e1051a39Sopenharmony_ci * was offered in the resumption ClientHello.
5025e1051a39Sopenharmony_ci *
5026e1051a39Sopenharmony_ci * Using E for the number of EC groups and F for the number of FF groups:
5027e1051a39Sopenharmony_ci * E tests of ECDHE with TLS 1.3, server only has one group
5028e1051a39Sopenharmony_ci * F tests of FFDHE with TLS 1.3, server only has one group
5029e1051a39Sopenharmony_ci * E tests of ECDHE with TLS 1.2, server only has one group
5030e1051a39Sopenharmony_ci * F tests of FFDHE with TLS 1.2, server only has one group
5031e1051a39Sopenharmony_ci * E tests of ECDHE with TLS 1.3, client sends only one group
5032e1051a39Sopenharmony_ci * F tests of FFDHE with TLS 1.3, client sends only one group
5033e1051a39Sopenharmony_ci * E tests of ECDHE with TLS 1.2, client sends only one group
5034e1051a39Sopenharmony_ci * F tests of FFDHE with TLS 1.2, client sends only one group
5035e1051a39Sopenharmony_ci */
5036e1051a39Sopenharmony_cistatic int test_negotiated_group(int idx)
5037e1051a39Sopenharmony_ci{
5038e1051a39Sopenharmony_ci    int clientmulti, istls13, isecdhe, numec, numff, numgroups;
5039e1051a39Sopenharmony_ci    int expectednid;
5040e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
5041e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
5042e1051a39Sopenharmony_ci    SSL_SESSION *origsess = NULL;
5043e1051a39Sopenharmony_ci    int testresult = 0;
5044e1051a39Sopenharmony_ci    int kexch_alg;
5045e1051a39Sopenharmony_ci    int max_version = TLS1_3_VERSION;
5046e1051a39Sopenharmony_ci
5047e1051a39Sopenharmony_ci    numec = OSSL_NELEM(ecdhe_kexch_groups);
5048e1051a39Sopenharmony_ci    numff = OSSL_NELEM(ffdhe_kexch_groups);
5049e1051a39Sopenharmony_ci    numgroups = numec + numff;
5050e1051a39Sopenharmony_ci    clientmulti = (idx < 2 * numgroups);
5051e1051a39Sopenharmony_ci    idx = idx % (2 * numgroups);
5052e1051a39Sopenharmony_ci    istls13 = (idx < numgroups);
5053e1051a39Sopenharmony_ci    idx = idx % numgroups;
5054e1051a39Sopenharmony_ci    isecdhe = (idx < numec);
5055e1051a39Sopenharmony_ci    if (!isecdhe)
5056e1051a39Sopenharmony_ci        idx -= numec;
5057e1051a39Sopenharmony_ci    /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
5058e1051a39Sopenharmony_ci    if (isecdhe)
5059e1051a39Sopenharmony_ci        kexch_alg = ecdhe_kexch_groups[idx];
5060e1051a39Sopenharmony_ci    else
5061e1051a39Sopenharmony_ci        kexch_alg = ffdhe_kexch_groups[idx];
5062e1051a39Sopenharmony_ci    /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
5063e1051a39Sopenharmony_ci    if (!istls13 && !isecdhe)
5064e1051a39Sopenharmony_ci        expectednid = NID_undef;
5065e1051a39Sopenharmony_ci    else
5066e1051a39Sopenharmony_ci        expectednid = kexch_alg;
5067e1051a39Sopenharmony_ci
5068e1051a39Sopenharmony_ci    if (!istls13)
5069e1051a39Sopenharmony_ci        max_version = TLS1_2_VERSION;
5070e1051a39Sopenharmony_ci
5071e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5072e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION,
5073e1051a39Sopenharmony_ci                                       max_version, &sctx, &cctx, cert,
5074e1051a39Sopenharmony_ci                                       privkey)))
5075e1051a39Sopenharmony_ci        goto end;
5076e1051a39Sopenharmony_ci
5077e1051a39Sopenharmony_ci    /*
5078e1051a39Sopenharmony_ci     * Force (EC)DHE ciphers for TLS 1.2.
5079e1051a39Sopenharmony_ci     * Be sure to enable auto tmp DH so that FFDHE can succeed.
5080e1051a39Sopenharmony_ci     */
5081e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
5082e1051a39Sopenharmony_ci                   TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5083e1051a39Sopenharmony_ci                   TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
5084e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
5085e1051a39Sopenharmony_ci        goto end;
5086e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
5087e1051a39Sopenharmony_ci                   TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
5088e1051a39Sopenharmony_ci                   TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
5089e1051a39Sopenharmony_ci        goto end;
5090e1051a39Sopenharmony_ci
5091e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5092e1051a39Sopenharmony_ci                                             NULL, NULL)))
5093e1051a39Sopenharmony_ci        goto end;
5094e1051a39Sopenharmony_ci
5095e1051a39Sopenharmony_ci    if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
5096e1051a39Sopenharmony_ci                                  idx)))
5097e1051a39Sopenharmony_ci        goto end;
5098e1051a39Sopenharmony_ci
5099e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
5100e1051a39Sopenharmony_ci        goto end;
5101e1051a39Sopenharmony_ci
5102e1051a39Sopenharmony_ci    /* Initial handshake; always the configured one */
5103e1051a39Sopenharmony_ci    if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5104e1051a39Sopenharmony_ci            || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5105e1051a39Sopenharmony_ci        goto end;
5106e1051a39Sopenharmony_ci
5107e1051a39Sopenharmony_ci    if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
5108e1051a39Sopenharmony_ci        goto end;
5109e1051a39Sopenharmony_ci
5110e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
5111e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
5112e1051a39Sopenharmony_ci    SSL_free(serverssl);
5113e1051a39Sopenharmony_ci    SSL_free(clientssl);
5114e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5115e1051a39Sopenharmony_ci
5116e1051a39Sopenharmony_ci    /* First resumption attempt; use the same config as initial handshake */
5117e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5118e1051a39Sopenharmony_ci                                             NULL, NULL))
5119e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, origsess))
5120e1051a39Sopenharmony_ci            || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5121e1051a39Sopenharmony_ci                                         isecdhe, idx)))
5122e1051a39Sopenharmony_ci        goto end;
5123e1051a39Sopenharmony_ci
5124e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5125e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
5126e1051a39Sopenharmony_ci        goto end;
5127e1051a39Sopenharmony_ci
5128e1051a39Sopenharmony_ci    /* Still had better agree, since nothing changed... */
5129e1051a39Sopenharmony_ci    if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5130e1051a39Sopenharmony_ci            || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5131e1051a39Sopenharmony_ci        goto end;
5132e1051a39Sopenharmony_ci
5133e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
5134e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
5135e1051a39Sopenharmony_ci    SSL_free(serverssl);
5136e1051a39Sopenharmony_ci    SSL_free(clientssl);
5137e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5138e1051a39Sopenharmony_ci
5139e1051a39Sopenharmony_ci    /*-
5140e1051a39Sopenharmony_ci     * Second resumption attempt
5141e1051a39Sopenharmony_ci     * The party that picks one group changes it, which we effectuate by
5142e1051a39Sopenharmony_ci     * changing 'idx' and updating what we expect.
5143e1051a39Sopenharmony_ci     */
5144e1051a39Sopenharmony_ci    if (idx == 0)
5145e1051a39Sopenharmony_ci        idx = 1;
5146e1051a39Sopenharmony_ci    else
5147e1051a39Sopenharmony_ci        idx--;
5148e1051a39Sopenharmony_ci    if (istls13) {
5149e1051a39Sopenharmony_ci        if (isecdhe)
5150e1051a39Sopenharmony_ci            expectednid = ecdhe_kexch_groups[idx];
5151e1051a39Sopenharmony_ci        else
5152e1051a39Sopenharmony_ci            expectednid = ffdhe_kexch_groups[idx];
5153e1051a39Sopenharmony_ci        /* Verify that we are changing what we expect. */
5154e1051a39Sopenharmony_ci        if (!TEST_int_ne(expectednid, kexch_alg))
5155e1051a39Sopenharmony_ci            goto end;
5156e1051a39Sopenharmony_ci    } else {
5157e1051a39Sopenharmony_ci        /* TLS 1.2 only supports named groups for ECDHE. */
5158e1051a39Sopenharmony_ci        if (isecdhe)
5159e1051a39Sopenharmony_ci            expectednid = kexch_alg;
5160e1051a39Sopenharmony_ci        else
5161e1051a39Sopenharmony_ci            expectednid = 0;
5162e1051a39Sopenharmony_ci    }
5163e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5164e1051a39Sopenharmony_ci                                             NULL, NULL))
5165e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, origsess))
5166e1051a39Sopenharmony_ci            || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
5167e1051a39Sopenharmony_ci                                         isecdhe, idx)))
5168e1051a39Sopenharmony_ci        goto end;
5169e1051a39Sopenharmony_ci
5170e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5171e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
5172e1051a39Sopenharmony_ci        goto end;
5173e1051a39Sopenharmony_ci
5174e1051a39Sopenharmony_ci    /* Check that we get what we expected */
5175e1051a39Sopenharmony_ci    if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
5176e1051a39Sopenharmony_ci            || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
5177e1051a39Sopenharmony_ci        goto end;
5178e1051a39Sopenharmony_ci
5179e1051a39Sopenharmony_ci    testresult = 1;
5180e1051a39Sopenharmony_ci end:
5181e1051a39Sopenharmony_ci    SSL_free(serverssl);
5182e1051a39Sopenharmony_ci    SSL_free(clientssl);
5183e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
5184e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
5185e1051a39Sopenharmony_ci    SSL_SESSION_free(origsess);
5186e1051a39Sopenharmony_ci    return testresult;
5187e1051a39Sopenharmony_ci}
5188e1051a39Sopenharmony_ci# endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
5189e1051a39Sopenharmony_ci
5190e1051a39Sopenharmony_ci/*
5191e1051a39Sopenharmony_ci * Test TLSv1.3 Cipher Suite
5192e1051a39Sopenharmony_ci * Test 0 = Set TLS1.3 cipher on context
5193e1051a39Sopenharmony_ci * Test 1 = Set TLS1.3 cipher on SSL
5194e1051a39Sopenharmony_ci * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
5195e1051a39Sopenharmony_ci * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
5196e1051a39Sopenharmony_ci */
5197e1051a39Sopenharmony_cistatic int test_tls13_ciphersuite(int idx)
5198e1051a39Sopenharmony_ci{
5199e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
5200e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
5201e1051a39Sopenharmony_ci    static const struct {
5202e1051a39Sopenharmony_ci        const char *ciphername;
5203e1051a39Sopenharmony_ci        int fipscapable;
5204e1051a39Sopenharmony_ci    } t13_ciphers[] = {
5205e1051a39Sopenharmony_ci        { TLS1_3_RFC_AES_128_GCM_SHA256, 1 },
5206e1051a39Sopenharmony_ci        { TLS1_3_RFC_AES_256_GCM_SHA384, 1 },
5207e1051a39Sopenharmony_ci        { TLS1_3_RFC_AES_128_CCM_SHA256, 1 },
5208e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
5209e1051a39Sopenharmony_ci        { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5210e1051a39Sopenharmony_ci        { TLS1_3_RFC_AES_256_GCM_SHA384
5211e1051a39Sopenharmony_ci          ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0 },
5212e1051a39Sopenharmony_ci# endif
5213e1051a39Sopenharmony_ci        { TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1 }
5214e1051a39Sopenharmony_ci    };
5215e1051a39Sopenharmony_ci    const char *t13_cipher = NULL;
5216e1051a39Sopenharmony_ci    const char *t12_cipher = NULL;
5217e1051a39Sopenharmony_ci    const char *negotiated_scipher;
5218e1051a39Sopenharmony_ci    const char *negotiated_ccipher;
5219e1051a39Sopenharmony_ci    int set_at_ctx = 0;
5220e1051a39Sopenharmony_ci    int set_at_ssl = 0;
5221e1051a39Sopenharmony_ci    int testresult = 0;
5222e1051a39Sopenharmony_ci    int max_ver;
5223e1051a39Sopenharmony_ci    size_t i;
5224e1051a39Sopenharmony_ci
5225e1051a39Sopenharmony_ci    switch (idx) {
5226e1051a39Sopenharmony_ci        case 0:
5227e1051a39Sopenharmony_ci            set_at_ctx = 1;
5228e1051a39Sopenharmony_ci            break;
5229e1051a39Sopenharmony_ci        case 1:
5230e1051a39Sopenharmony_ci            set_at_ssl = 1;
5231e1051a39Sopenharmony_ci            break;
5232e1051a39Sopenharmony_ci        case 2:
5233e1051a39Sopenharmony_ci            set_at_ctx = 1;
5234e1051a39Sopenharmony_ci            t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5235e1051a39Sopenharmony_ci            break;
5236e1051a39Sopenharmony_ci        case 3:
5237e1051a39Sopenharmony_ci            set_at_ssl = 1;
5238e1051a39Sopenharmony_ci            t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
5239e1051a39Sopenharmony_ci            break;
5240e1051a39Sopenharmony_ci    }
5241e1051a39Sopenharmony_ci
5242e1051a39Sopenharmony_ci    for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
5243e1051a39Sopenharmony_ci# ifdef OPENSSL_NO_TLS1_2
5244e1051a39Sopenharmony_ci        if (max_ver == TLS1_2_VERSION)
5245e1051a39Sopenharmony_ci            continue;
5246e1051a39Sopenharmony_ci# endif
5247e1051a39Sopenharmony_ci        for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
5248e1051a39Sopenharmony_ci            if (is_fips && !t13_ciphers[i].fipscapable)
5249e1051a39Sopenharmony_ci                continue;
5250e1051a39Sopenharmony_ci            t13_cipher = t13_ciphers[i].ciphername;
5251e1051a39Sopenharmony_ci            if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5252e1051a39Sopenharmony_ci                                               TLS_client_method(),
5253e1051a39Sopenharmony_ci                                               TLS1_VERSION, max_ver,
5254e1051a39Sopenharmony_ci                                               &sctx, &cctx, cert, privkey)))
5255e1051a39Sopenharmony_ci                goto end;
5256e1051a39Sopenharmony_ci
5257e1051a39Sopenharmony_ci            if (set_at_ctx) {
5258e1051a39Sopenharmony_ci                if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
5259e1051a39Sopenharmony_ci                    || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
5260e1051a39Sopenharmony_ci                    goto end;
5261e1051a39Sopenharmony_ci                if (t12_cipher != NULL) {
5262e1051a39Sopenharmony_ci                    if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
5263e1051a39Sopenharmony_ci                        || !TEST_true(SSL_CTX_set_cipher_list(cctx,
5264e1051a39Sopenharmony_ci                                                              t12_cipher)))
5265e1051a39Sopenharmony_ci                        goto end;
5266e1051a39Sopenharmony_ci                }
5267e1051a39Sopenharmony_ci            }
5268e1051a39Sopenharmony_ci
5269e1051a39Sopenharmony_ci            if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5270e1051a39Sopenharmony_ci                                              &clientssl, NULL, NULL)))
5271e1051a39Sopenharmony_ci                goto end;
5272e1051a39Sopenharmony_ci
5273e1051a39Sopenharmony_ci            if (set_at_ssl) {
5274e1051a39Sopenharmony_ci                if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
5275e1051a39Sopenharmony_ci                    || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
5276e1051a39Sopenharmony_ci                    goto end;
5277e1051a39Sopenharmony_ci                if (t12_cipher != NULL) {
5278e1051a39Sopenharmony_ci                    if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
5279e1051a39Sopenharmony_ci                        || !TEST_true(SSL_set_cipher_list(clientssl,
5280e1051a39Sopenharmony_ci                                                          t12_cipher)))
5281e1051a39Sopenharmony_ci                        goto end;
5282e1051a39Sopenharmony_ci                }
5283e1051a39Sopenharmony_ci            }
5284e1051a39Sopenharmony_ci
5285e1051a39Sopenharmony_ci            if (!TEST_true(create_ssl_connection(serverssl, clientssl,
5286e1051a39Sopenharmony_ci                                                 SSL_ERROR_NONE)))
5287e1051a39Sopenharmony_ci                goto end;
5288e1051a39Sopenharmony_ci
5289e1051a39Sopenharmony_ci            negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5290e1051a39Sopenharmony_ci                                                                 serverssl));
5291e1051a39Sopenharmony_ci            negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
5292e1051a39Sopenharmony_ci                                                                 clientssl));
5293e1051a39Sopenharmony_ci            if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
5294e1051a39Sopenharmony_ci                goto end;
5295e1051a39Sopenharmony_ci
5296e1051a39Sopenharmony_ci            /*
5297e1051a39Sopenharmony_ci             * TEST_strn_eq is used below because t13_cipher can contain
5298e1051a39Sopenharmony_ci             * multiple ciphersuites
5299e1051a39Sopenharmony_ci             */
5300e1051a39Sopenharmony_ci            if (max_ver == TLS1_3_VERSION
5301e1051a39Sopenharmony_ci                && !TEST_strn_eq(t13_cipher, negotiated_scipher,
5302e1051a39Sopenharmony_ci                                 strlen(negotiated_scipher)))
5303e1051a39Sopenharmony_ci                goto end;
5304e1051a39Sopenharmony_ci
5305e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
5306e1051a39Sopenharmony_ci            /* Below validation is not done when t12_cipher is NULL */
5307e1051a39Sopenharmony_ci            if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
5308e1051a39Sopenharmony_ci                && !TEST_str_eq(t12_cipher, negotiated_scipher))
5309e1051a39Sopenharmony_ci                goto end;
5310e1051a39Sopenharmony_ci# endif
5311e1051a39Sopenharmony_ci
5312e1051a39Sopenharmony_ci            SSL_free(serverssl);
5313e1051a39Sopenharmony_ci            serverssl = NULL;
5314e1051a39Sopenharmony_ci            SSL_free(clientssl);
5315e1051a39Sopenharmony_ci            clientssl = NULL;
5316e1051a39Sopenharmony_ci            SSL_CTX_free(sctx);
5317e1051a39Sopenharmony_ci            sctx = NULL;
5318e1051a39Sopenharmony_ci            SSL_CTX_free(cctx);
5319e1051a39Sopenharmony_ci            cctx = NULL;
5320e1051a39Sopenharmony_ci        }
5321e1051a39Sopenharmony_ci    }
5322e1051a39Sopenharmony_ci
5323e1051a39Sopenharmony_ci    testresult = 1;
5324e1051a39Sopenharmony_ci end:
5325e1051a39Sopenharmony_ci    SSL_free(serverssl);
5326e1051a39Sopenharmony_ci    SSL_free(clientssl);
5327e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
5328e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
5329e1051a39Sopenharmony_ci    return testresult;
5330e1051a39Sopenharmony_ci}
5331e1051a39Sopenharmony_ci
5332e1051a39Sopenharmony_ci/*
5333e1051a39Sopenharmony_ci * Test TLSv1.3 PSKs
5334e1051a39Sopenharmony_ci * Test 0 = Test new style callbacks
5335e1051a39Sopenharmony_ci * Test 1 = Test both new and old style callbacks
5336e1051a39Sopenharmony_ci * Test 2 = Test old style callbacks
5337e1051a39Sopenharmony_ci * Test 3 = Test old style callbacks with no certificate
5338e1051a39Sopenharmony_ci */
5339e1051a39Sopenharmony_cistatic int test_tls13_psk(int idx)
5340e1051a39Sopenharmony_ci{
5341e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
5342e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
5343e1051a39Sopenharmony_ci    const SSL_CIPHER *cipher = NULL;
5344e1051a39Sopenharmony_ci    const unsigned char key[] = {
5345e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
5346e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
5347e1051a39Sopenharmony_ci        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
5348e1051a39Sopenharmony_ci        0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
5349e1051a39Sopenharmony_ci    };
5350e1051a39Sopenharmony_ci    int testresult = 0;
5351e1051a39Sopenharmony_ci
5352e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5353e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
5354e1051a39Sopenharmony_ci                                       &sctx, &cctx, idx == 3 ? NULL : cert,
5355e1051a39Sopenharmony_ci                                       idx == 3 ? NULL : privkey)))
5356e1051a39Sopenharmony_ci        goto end;
5357e1051a39Sopenharmony_ci
5358e1051a39Sopenharmony_ci    if (idx != 3) {
5359e1051a39Sopenharmony_ci        /*
5360e1051a39Sopenharmony_ci         * We use a ciphersuite with SHA256 to ease testing old style PSK
5361e1051a39Sopenharmony_ci         * callbacks which will always default to SHA256. This should not be
5362e1051a39Sopenharmony_ci         * necessary if we have no cert/priv key. In that case the server should
5363e1051a39Sopenharmony_ci         * prefer SHA256 automatically.
5364e1051a39Sopenharmony_ci         */
5365e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5366e1051a39Sopenharmony_ci                                                "TLS_AES_128_GCM_SHA256")))
5367e1051a39Sopenharmony_ci            goto end;
5368e1051a39Sopenharmony_ci    } else {
5369e1051a39Sopenharmony_ci        /*
5370e1051a39Sopenharmony_ci         * As noted above the server should prefer SHA256 automatically. However
5371e1051a39Sopenharmony_ci         * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
5372e1051a39Sopenharmony_ci         * code works even if we are testing with only the FIPS provider loaded.
5373e1051a39Sopenharmony_ci         */
5374e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
5375e1051a39Sopenharmony_ci                                                "TLS_AES_256_GCM_SHA384:"
5376e1051a39Sopenharmony_ci                                                "TLS_AES_128_GCM_SHA256")))
5377e1051a39Sopenharmony_ci            goto end;
5378e1051a39Sopenharmony_ci    }
5379e1051a39Sopenharmony_ci
5380e1051a39Sopenharmony_ci    /*
5381e1051a39Sopenharmony_ci     * Test 0: New style callbacks only
5382e1051a39Sopenharmony_ci     * Test 1: New and old style callbacks (only the new ones should be used)
5383e1051a39Sopenharmony_ci     * Test 2: Old style callbacks only
5384e1051a39Sopenharmony_ci     */
5385e1051a39Sopenharmony_ci    if (idx == 0 || idx == 1) {
5386e1051a39Sopenharmony_ci        SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
5387e1051a39Sopenharmony_ci        SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
5388e1051a39Sopenharmony_ci    }
5389e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_PSK
5390e1051a39Sopenharmony_ci    if (idx >= 1) {
5391e1051a39Sopenharmony_ci        SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
5392e1051a39Sopenharmony_ci        SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
5393e1051a39Sopenharmony_ci    }
5394e1051a39Sopenharmony_ci#endif
5395e1051a39Sopenharmony_ci    srvid = pskid;
5396e1051a39Sopenharmony_ci    use_session_cb_cnt = 0;
5397e1051a39Sopenharmony_ci    find_session_cb_cnt = 0;
5398e1051a39Sopenharmony_ci    psk_client_cb_cnt = 0;
5399e1051a39Sopenharmony_ci    psk_server_cb_cnt = 0;
5400e1051a39Sopenharmony_ci
5401e1051a39Sopenharmony_ci    if (idx != 3) {
5402e1051a39Sopenharmony_ci        /*
5403e1051a39Sopenharmony_ci         * Check we can create a connection if callback decides not to send a
5404e1051a39Sopenharmony_ci         * PSK
5405e1051a39Sopenharmony_ci         */
5406e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5407e1051a39Sopenharmony_ci                                                 NULL, NULL))
5408e1051a39Sopenharmony_ci                || !TEST_true(create_ssl_connection(serverssl, clientssl,
5409e1051a39Sopenharmony_ci                                                    SSL_ERROR_NONE))
5410e1051a39Sopenharmony_ci                || !TEST_false(SSL_session_reused(clientssl))
5411e1051a39Sopenharmony_ci                || !TEST_false(SSL_session_reused(serverssl)))
5412e1051a39Sopenharmony_ci            goto end;
5413e1051a39Sopenharmony_ci
5414e1051a39Sopenharmony_ci        if (idx == 0 || idx == 1) {
5415e1051a39Sopenharmony_ci            if (!TEST_true(use_session_cb_cnt == 1)
5416e1051a39Sopenharmony_ci                    || !TEST_true(find_session_cb_cnt == 0)
5417e1051a39Sopenharmony_ci                       /*
5418e1051a39Sopenharmony_ci                        * If no old style callback then below should be 0
5419e1051a39Sopenharmony_ci                        * otherwise 1
5420e1051a39Sopenharmony_ci                        */
5421e1051a39Sopenharmony_ci                    || !TEST_true(psk_client_cb_cnt == idx)
5422e1051a39Sopenharmony_ci                    || !TEST_true(psk_server_cb_cnt == 0))
5423e1051a39Sopenharmony_ci                goto end;
5424e1051a39Sopenharmony_ci        } else {
5425e1051a39Sopenharmony_ci            if (!TEST_true(use_session_cb_cnt == 0)
5426e1051a39Sopenharmony_ci                    || !TEST_true(find_session_cb_cnt == 0)
5427e1051a39Sopenharmony_ci                    || !TEST_true(psk_client_cb_cnt == 1)
5428e1051a39Sopenharmony_ci                    || !TEST_true(psk_server_cb_cnt == 0))
5429e1051a39Sopenharmony_ci                goto end;
5430e1051a39Sopenharmony_ci        }
5431e1051a39Sopenharmony_ci
5432e1051a39Sopenharmony_ci        shutdown_ssl_connection(serverssl, clientssl);
5433e1051a39Sopenharmony_ci        serverssl = clientssl = NULL;
5434e1051a39Sopenharmony_ci        use_session_cb_cnt = psk_client_cb_cnt = 0;
5435e1051a39Sopenharmony_ci    }
5436e1051a39Sopenharmony_ci
5437e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5438e1051a39Sopenharmony_ci                                             NULL, NULL)))
5439e1051a39Sopenharmony_ci        goto end;
5440e1051a39Sopenharmony_ci
5441e1051a39Sopenharmony_ci    /* Create the PSK */
5442e1051a39Sopenharmony_ci    cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
5443e1051a39Sopenharmony_ci    clientpsk = SSL_SESSION_new();
5444e1051a39Sopenharmony_ci    if (!TEST_ptr(clientpsk)
5445e1051a39Sopenharmony_ci            || !TEST_ptr(cipher)
5446e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
5447e1051a39Sopenharmony_ci                                                      sizeof(key)))
5448e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
5449e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
5450e1051a39Sopenharmony_ci                                                           TLS1_3_VERSION))
5451e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
5452e1051a39Sopenharmony_ci        goto end;
5453e1051a39Sopenharmony_ci    serverpsk = clientpsk;
5454e1051a39Sopenharmony_ci
5455e1051a39Sopenharmony_ci    /* Check we can create a connection and the PSK is used */
5456e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5457e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl))
5458e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(serverssl)))
5459e1051a39Sopenharmony_ci        goto end;
5460e1051a39Sopenharmony_ci
5461e1051a39Sopenharmony_ci    if (idx == 0 || idx == 1) {
5462e1051a39Sopenharmony_ci        if (!TEST_true(use_session_cb_cnt == 1)
5463e1051a39Sopenharmony_ci                || !TEST_true(find_session_cb_cnt == 1)
5464e1051a39Sopenharmony_ci                || !TEST_true(psk_client_cb_cnt == 0)
5465e1051a39Sopenharmony_ci                || !TEST_true(psk_server_cb_cnt == 0))
5466e1051a39Sopenharmony_ci            goto end;
5467e1051a39Sopenharmony_ci    } else {
5468e1051a39Sopenharmony_ci        if (!TEST_true(use_session_cb_cnt == 0)
5469e1051a39Sopenharmony_ci                || !TEST_true(find_session_cb_cnt == 0)
5470e1051a39Sopenharmony_ci                || !TEST_true(psk_client_cb_cnt == 1)
5471e1051a39Sopenharmony_ci                || !TEST_true(psk_server_cb_cnt == 1))
5472e1051a39Sopenharmony_ci            goto end;
5473e1051a39Sopenharmony_ci    }
5474e1051a39Sopenharmony_ci
5475e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
5476e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5477e1051a39Sopenharmony_ci    use_session_cb_cnt = find_session_cb_cnt = 0;
5478e1051a39Sopenharmony_ci    psk_client_cb_cnt = psk_server_cb_cnt = 0;
5479e1051a39Sopenharmony_ci
5480e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5481e1051a39Sopenharmony_ci                                             NULL, NULL)))
5482e1051a39Sopenharmony_ci        goto end;
5483e1051a39Sopenharmony_ci
5484e1051a39Sopenharmony_ci    /* Force an HRR */
5485e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_EC)
5486e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
5487e1051a39Sopenharmony_ci        goto end;
5488e1051a39Sopenharmony_ci#else
5489e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
5490e1051a39Sopenharmony_ci        goto end;
5491e1051a39Sopenharmony_ci#endif
5492e1051a39Sopenharmony_ci
5493e1051a39Sopenharmony_ci    /*
5494e1051a39Sopenharmony_ci     * Check we can create a connection, the PSK is used and the callbacks are
5495e1051a39Sopenharmony_ci     * called twice.
5496e1051a39Sopenharmony_ci     */
5497e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
5498e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl))
5499e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(serverssl)))
5500e1051a39Sopenharmony_ci        goto end;
5501e1051a39Sopenharmony_ci
5502e1051a39Sopenharmony_ci    if (idx == 0 || idx == 1) {
5503e1051a39Sopenharmony_ci        if (!TEST_true(use_session_cb_cnt == 2)
5504e1051a39Sopenharmony_ci                || !TEST_true(find_session_cb_cnt == 2)
5505e1051a39Sopenharmony_ci                || !TEST_true(psk_client_cb_cnt == 0)
5506e1051a39Sopenharmony_ci                || !TEST_true(psk_server_cb_cnt == 0))
5507e1051a39Sopenharmony_ci            goto end;
5508e1051a39Sopenharmony_ci    } else {
5509e1051a39Sopenharmony_ci        if (!TEST_true(use_session_cb_cnt == 0)
5510e1051a39Sopenharmony_ci                || !TEST_true(find_session_cb_cnt == 0)
5511e1051a39Sopenharmony_ci                || !TEST_true(psk_client_cb_cnt == 2)
5512e1051a39Sopenharmony_ci                || !TEST_true(psk_server_cb_cnt == 2))
5513e1051a39Sopenharmony_ci            goto end;
5514e1051a39Sopenharmony_ci    }
5515e1051a39Sopenharmony_ci
5516e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
5517e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5518e1051a39Sopenharmony_ci    use_session_cb_cnt = find_session_cb_cnt = 0;
5519e1051a39Sopenharmony_ci    psk_client_cb_cnt = psk_server_cb_cnt = 0;
5520e1051a39Sopenharmony_ci
5521e1051a39Sopenharmony_ci    if (idx != 3) {
5522e1051a39Sopenharmony_ci        /*
5523e1051a39Sopenharmony_ci         * Check that if the server rejects the PSK we can still connect, but with
5524e1051a39Sopenharmony_ci         * a full handshake
5525e1051a39Sopenharmony_ci         */
5526e1051a39Sopenharmony_ci        srvid = "Dummy Identity";
5527e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5528e1051a39Sopenharmony_ci                                                 NULL, NULL))
5529e1051a39Sopenharmony_ci                || !TEST_true(create_ssl_connection(serverssl, clientssl,
5530e1051a39Sopenharmony_ci                                                    SSL_ERROR_NONE))
5531e1051a39Sopenharmony_ci                || !TEST_false(SSL_session_reused(clientssl))
5532e1051a39Sopenharmony_ci                || !TEST_false(SSL_session_reused(serverssl)))
5533e1051a39Sopenharmony_ci            goto end;
5534e1051a39Sopenharmony_ci
5535e1051a39Sopenharmony_ci        if (idx == 0 || idx == 1) {
5536e1051a39Sopenharmony_ci            if (!TEST_true(use_session_cb_cnt == 1)
5537e1051a39Sopenharmony_ci                    || !TEST_true(find_session_cb_cnt == 1)
5538e1051a39Sopenharmony_ci                    || !TEST_true(psk_client_cb_cnt == 0)
5539e1051a39Sopenharmony_ci                       /*
5540e1051a39Sopenharmony_ci                        * If no old style callback then below should be 0
5541e1051a39Sopenharmony_ci                        * otherwise 1
5542e1051a39Sopenharmony_ci                        */
5543e1051a39Sopenharmony_ci                    || !TEST_true(psk_server_cb_cnt == idx))
5544e1051a39Sopenharmony_ci                goto end;
5545e1051a39Sopenharmony_ci        } else {
5546e1051a39Sopenharmony_ci            if (!TEST_true(use_session_cb_cnt == 0)
5547e1051a39Sopenharmony_ci                    || !TEST_true(find_session_cb_cnt == 0)
5548e1051a39Sopenharmony_ci                    || !TEST_true(psk_client_cb_cnt == 1)
5549e1051a39Sopenharmony_ci                    || !TEST_true(psk_server_cb_cnt == 1))
5550e1051a39Sopenharmony_ci                goto end;
5551e1051a39Sopenharmony_ci        }
5552e1051a39Sopenharmony_ci
5553e1051a39Sopenharmony_ci        shutdown_ssl_connection(serverssl, clientssl);
5554e1051a39Sopenharmony_ci        serverssl = clientssl = NULL;
5555e1051a39Sopenharmony_ci    }
5556e1051a39Sopenharmony_ci    testresult = 1;
5557e1051a39Sopenharmony_ci
5558e1051a39Sopenharmony_ci end:
5559e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
5560e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
5561e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
5562e1051a39Sopenharmony_ci    SSL_free(serverssl);
5563e1051a39Sopenharmony_ci    SSL_free(clientssl);
5564e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
5565e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
5566e1051a39Sopenharmony_ci    return testresult;
5567e1051a39Sopenharmony_ci}
5568e1051a39Sopenharmony_ci
5569e1051a39Sopenharmony_cistatic unsigned char cookie_magic_value[] = "cookie magic";
5570e1051a39Sopenharmony_ci
5571e1051a39Sopenharmony_cistatic int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
5572e1051a39Sopenharmony_ci                                    unsigned int *cookie_len)
5573e1051a39Sopenharmony_ci{
5574e1051a39Sopenharmony_ci    /*
5575e1051a39Sopenharmony_ci     * Not suitable as a real cookie generation function but good enough for
5576e1051a39Sopenharmony_ci     * testing!
5577e1051a39Sopenharmony_ci     */
5578e1051a39Sopenharmony_ci    memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
5579e1051a39Sopenharmony_ci    *cookie_len = sizeof(cookie_magic_value) - 1;
5580e1051a39Sopenharmony_ci
5581e1051a39Sopenharmony_ci    return 1;
5582e1051a39Sopenharmony_ci}
5583e1051a39Sopenharmony_ci
5584e1051a39Sopenharmony_cistatic int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
5585e1051a39Sopenharmony_ci                                  unsigned int cookie_len)
5586e1051a39Sopenharmony_ci{
5587e1051a39Sopenharmony_ci    if (cookie_len == sizeof(cookie_magic_value) - 1
5588e1051a39Sopenharmony_ci        && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
5589e1051a39Sopenharmony_ci        return 1;
5590e1051a39Sopenharmony_ci
5591e1051a39Sopenharmony_ci    return 0;
5592e1051a39Sopenharmony_ci}
5593e1051a39Sopenharmony_ci
5594e1051a39Sopenharmony_cistatic int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
5595e1051a39Sopenharmony_ci                                        size_t *cookie_len)
5596e1051a39Sopenharmony_ci{
5597e1051a39Sopenharmony_ci    unsigned int temp;
5598e1051a39Sopenharmony_ci    int res = generate_cookie_callback(ssl, cookie, &temp);
5599e1051a39Sopenharmony_ci    *cookie_len = temp;
5600e1051a39Sopenharmony_ci    return res;
5601e1051a39Sopenharmony_ci}
5602e1051a39Sopenharmony_ci
5603e1051a39Sopenharmony_cistatic int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
5604e1051a39Sopenharmony_ci                                      size_t cookie_len)
5605e1051a39Sopenharmony_ci{
5606e1051a39Sopenharmony_ci    return verify_cookie_callback(ssl, cookie, cookie_len);
5607e1051a39Sopenharmony_ci}
5608e1051a39Sopenharmony_ci
5609e1051a39Sopenharmony_cistatic int test_stateless(void)
5610e1051a39Sopenharmony_ci{
5611e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
5612e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
5613e1051a39Sopenharmony_ci    int testresult = 0;
5614e1051a39Sopenharmony_ci
5615e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5616e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
5617e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
5618e1051a39Sopenharmony_ci        goto end;
5619e1051a39Sopenharmony_ci
5620e1051a39Sopenharmony_ci    /* The arrival of CCS messages can confuse the test */
5621e1051a39Sopenharmony_ci    SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
5622e1051a39Sopenharmony_ci
5623e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5624e1051a39Sopenharmony_ci                                      NULL, NULL))
5625e1051a39Sopenharmony_ci               /* Send the first ClientHello */
5626e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
5627e1051a39Sopenharmony_ci                                                 SSL_ERROR_WANT_READ))
5628e1051a39Sopenharmony_ci               /*
5629e1051a39Sopenharmony_ci                * This should fail with a -1 return because we have no callbacks
5630e1051a39Sopenharmony_ci                * set up
5631e1051a39Sopenharmony_ci                */
5632e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_stateless(serverssl), -1))
5633e1051a39Sopenharmony_ci        goto end;
5634e1051a39Sopenharmony_ci
5635e1051a39Sopenharmony_ci    /* Fatal error so abandon the connection from this client */
5636e1051a39Sopenharmony_ci    SSL_free(clientssl);
5637e1051a39Sopenharmony_ci    clientssl = NULL;
5638e1051a39Sopenharmony_ci
5639e1051a39Sopenharmony_ci    /* Set up the cookie generation and verification callbacks */
5640e1051a39Sopenharmony_ci    SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
5641e1051a39Sopenharmony_ci    SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
5642e1051a39Sopenharmony_ci
5643e1051a39Sopenharmony_ci    /*
5644e1051a39Sopenharmony_ci     * Create a new connection from the client (we can reuse the server SSL
5645e1051a39Sopenharmony_ci     * object).
5646e1051a39Sopenharmony_ci     */
5647e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5648e1051a39Sopenharmony_ci                                             NULL, NULL))
5649e1051a39Sopenharmony_ci               /* Send the first ClientHello */
5650e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
5651e1051a39Sopenharmony_ci                                                SSL_ERROR_WANT_READ))
5652e1051a39Sopenharmony_ci               /* This should fail because there is no cookie */
5653e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_stateless(serverssl), 0))
5654e1051a39Sopenharmony_ci        goto end;
5655e1051a39Sopenharmony_ci
5656e1051a39Sopenharmony_ci    /* Abandon the connection from this client */
5657e1051a39Sopenharmony_ci    SSL_free(clientssl);
5658e1051a39Sopenharmony_ci    clientssl = NULL;
5659e1051a39Sopenharmony_ci
5660e1051a39Sopenharmony_ci    /*
5661e1051a39Sopenharmony_ci     * Now create a connection from a new client but with the same server SSL
5662e1051a39Sopenharmony_ci     * object
5663e1051a39Sopenharmony_ci     */
5664e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
5665e1051a39Sopenharmony_ci                                             NULL, NULL))
5666e1051a39Sopenharmony_ci               /* Send the first ClientHello */
5667e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
5668e1051a39Sopenharmony_ci                                                SSL_ERROR_WANT_READ))
5669e1051a39Sopenharmony_ci               /* This should fail because there is no cookie */
5670e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_stateless(serverssl), 0)
5671e1051a39Sopenharmony_ci               /* Send the second ClientHello */
5672e1051a39Sopenharmony_ci            || !TEST_false(create_ssl_connection(serverssl, clientssl,
5673e1051a39Sopenharmony_ci                                                SSL_ERROR_WANT_READ))
5674e1051a39Sopenharmony_ci               /* This should succeed because a cookie is now present */
5675e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_stateless(serverssl), 1)
5676e1051a39Sopenharmony_ci               /* Complete the connection */
5677e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
5678e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
5679e1051a39Sopenharmony_ci        goto end;
5680e1051a39Sopenharmony_ci
5681e1051a39Sopenharmony_ci    shutdown_ssl_connection(serverssl, clientssl);
5682e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5683e1051a39Sopenharmony_ci    testresult = 1;
5684e1051a39Sopenharmony_ci
5685e1051a39Sopenharmony_ci end:
5686e1051a39Sopenharmony_ci    SSL_free(serverssl);
5687e1051a39Sopenharmony_ci    SSL_free(clientssl);
5688e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
5689e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
5690e1051a39Sopenharmony_ci    return testresult;
5691e1051a39Sopenharmony_ci
5692e1051a39Sopenharmony_ci}
5693e1051a39Sopenharmony_ci#endif /* OSSL_NO_USABLE_TLS1_3 */
5694e1051a39Sopenharmony_ci
5695e1051a39Sopenharmony_cistatic int clntaddoldcb = 0;
5696e1051a39Sopenharmony_cistatic int clntparseoldcb = 0;
5697e1051a39Sopenharmony_cistatic int srvaddoldcb = 0;
5698e1051a39Sopenharmony_cistatic int srvparseoldcb = 0;
5699e1051a39Sopenharmony_cistatic int clntaddnewcb = 0;
5700e1051a39Sopenharmony_cistatic int clntparsenewcb = 0;
5701e1051a39Sopenharmony_cistatic int srvaddnewcb = 0;
5702e1051a39Sopenharmony_cistatic int srvparsenewcb = 0;
5703e1051a39Sopenharmony_cistatic int snicb = 0;
5704e1051a39Sopenharmony_ci
5705e1051a39Sopenharmony_ci#define TEST_EXT_TYPE1  0xff00
5706e1051a39Sopenharmony_ci
5707e1051a39Sopenharmony_cistatic int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
5708e1051a39Sopenharmony_ci                      size_t *outlen, int *al, void *add_arg)
5709e1051a39Sopenharmony_ci{
5710e1051a39Sopenharmony_ci    int *server = (int *)add_arg;
5711e1051a39Sopenharmony_ci    unsigned char *data;
5712e1051a39Sopenharmony_ci
5713e1051a39Sopenharmony_ci    if (SSL_is_server(s))
5714e1051a39Sopenharmony_ci        srvaddoldcb++;
5715e1051a39Sopenharmony_ci    else
5716e1051a39Sopenharmony_ci        clntaddoldcb++;
5717e1051a39Sopenharmony_ci
5718e1051a39Sopenharmony_ci    if (*server != SSL_is_server(s)
5719e1051a39Sopenharmony_ci            || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5720e1051a39Sopenharmony_ci        return -1;
5721e1051a39Sopenharmony_ci
5722e1051a39Sopenharmony_ci    *data = 1;
5723e1051a39Sopenharmony_ci    *out = data;
5724e1051a39Sopenharmony_ci    *outlen = sizeof(char);
5725e1051a39Sopenharmony_ci    return 1;
5726e1051a39Sopenharmony_ci}
5727e1051a39Sopenharmony_ci
5728e1051a39Sopenharmony_cistatic void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
5729e1051a39Sopenharmony_ci                        void *add_arg)
5730e1051a39Sopenharmony_ci{
5731e1051a39Sopenharmony_ci    OPENSSL_free((unsigned char *)out);
5732e1051a39Sopenharmony_ci}
5733e1051a39Sopenharmony_ci
5734e1051a39Sopenharmony_cistatic int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
5735e1051a39Sopenharmony_ci                        size_t inlen, int *al, void *parse_arg)
5736e1051a39Sopenharmony_ci{
5737e1051a39Sopenharmony_ci    int *server = (int *)parse_arg;
5738e1051a39Sopenharmony_ci
5739e1051a39Sopenharmony_ci    if (SSL_is_server(s))
5740e1051a39Sopenharmony_ci        srvparseoldcb++;
5741e1051a39Sopenharmony_ci    else
5742e1051a39Sopenharmony_ci        clntparseoldcb++;
5743e1051a39Sopenharmony_ci
5744e1051a39Sopenharmony_ci    if (*server != SSL_is_server(s)
5745e1051a39Sopenharmony_ci            || inlen != sizeof(char)
5746e1051a39Sopenharmony_ci            || *in != 1)
5747e1051a39Sopenharmony_ci        return -1;
5748e1051a39Sopenharmony_ci
5749e1051a39Sopenharmony_ci    return 1;
5750e1051a39Sopenharmony_ci}
5751e1051a39Sopenharmony_ci
5752e1051a39Sopenharmony_cistatic int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
5753e1051a39Sopenharmony_ci                      const unsigned char **out, size_t *outlen, X509 *x,
5754e1051a39Sopenharmony_ci                      size_t chainidx, int *al, void *add_arg)
5755e1051a39Sopenharmony_ci{
5756e1051a39Sopenharmony_ci    int *server = (int *)add_arg;
5757e1051a39Sopenharmony_ci    unsigned char *data;
5758e1051a39Sopenharmony_ci
5759e1051a39Sopenharmony_ci    if (SSL_is_server(s))
5760e1051a39Sopenharmony_ci        srvaddnewcb++;
5761e1051a39Sopenharmony_ci    else
5762e1051a39Sopenharmony_ci        clntaddnewcb++;
5763e1051a39Sopenharmony_ci
5764e1051a39Sopenharmony_ci    if (*server != SSL_is_server(s)
5765e1051a39Sopenharmony_ci            || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
5766e1051a39Sopenharmony_ci        return -1;
5767e1051a39Sopenharmony_ci
5768e1051a39Sopenharmony_ci    *data = 1;
5769e1051a39Sopenharmony_ci    *out = data;
5770e1051a39Sopenharmony_ci    *outlen = sizeof(*data);
5771e1051a39Sopenharmony_ci    return 1;
5772e1051a39Sopenharmony_ci}
5773e1051a39Sopenharmony_ci
5774e1051a39Sopenharmony_cistatic void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
5775e1051a39Sopenharmony_ci                        const unsigned char *out, void *add_arg)
5776e1051a39Sopenharmony_ci{
5777e1051a39Sopenharmony_ci    OPENSSL_free((unsigned char *)out);
5778e1051a39Sopenharmony_ci}
5779e1051a39Sopenharmony_ci
5780e1051a39Sopenharmony_cistatic int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
5781e1051a39Sopenharmony_ci                        const unsigned char *in, size_t inlen, X509 *x,
5782e1051a39Sopenharmony_ci                        size_t chainidx, int *al, void *parse_arg)
5783e1051a39Sopenharmony_ci{
5784e1051a39Sopenharmony_ci    int *server = (int *)parse_arg;
5785e1051a39Sopenharmony_ci
5786e1051a39Sopenharmony_ci    if (SSL_is_server(s))
5787e1051a39Sopenharmony_ci        srvparsenewcb++;
5788e1051a39Sopenharmony_ci    else
5789e1051a39Sopenharmony_ci        clntparsenewcb++;
5790e1051a39Sopenharmony_ci
5791e1051a39Sopenharmony_ci    if (*server != SSL_is_server(s)
5792e1051a39Sopenharmony_ci            || inlen != sizeof(char) || *in != 1)
5793e1051a39Sopenharmony_ci        return -1;
5794e1051a39Sopenharmony_ci
5795e1051a39Sopenharmony_ci    return 1;
5796e1051a39Sopenharmony_ci}
5797e1051a39Sopenharmony_ci
5798e1051a39Sopenharmony_cistatic int sni_cb(SSL *s, int *al, void *arg)
5799e1051a39Sopenharmony_ci{
5800e1051a39Sopenharmony_ci    SSL_CTX *ctx = (SSL_CTX *)arg;
5801e1051a39Sopenharmony_ci
5802e1051a39Sopenharmony_ci    if (SSL_set_SSL_CTX(s, ctx) == NULL) {
5803e1051a39Sopenharmony_ci        *al = SSL_AD_INTERNAL_ERROR;
5804e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_ALERT_FATAL;
5805e1051a39Sopenharmony_ci    }
5806e1051a39Sopenharmony_ci    snicb++;
5807e1051a39Sopenharmony_ci    return SSL_TLSEXT_ERR_OK;
5808e1051a39Sopenharmony_ci}
5809e1051a39Sopenharmony_ci
5810e1051a39Sopenharmony_cistatic int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
5811e1051a39Sopenharmony_ci{
5812e1051a39Sopenharmony_ci    return 1;
5813e1051a39Sopenharmony_ci}
5814e1051a39Sopenharmony_ci
5815e1051a39Sopenharmony_ci/*
5816e1051a39Sopenharmony_ci * Custom call back tests.
5817e1051a39Sopenharmony_ci * Test 0: Old style callbacks in TLSv1.2
5818e1051a39Sopenharmony_ci * Test 1: New style callbacks in TLSv1.2
5819e1051a39Sopenharmony_ci * Test 2: New style callbacks in TLSv1.2 with SNI
5820e1051a39Sopenharmony_ci * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
5821e1051a39Sopenharmony_ci * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
5822e1051a39Sopenharmony_ci * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
5823e1051a39Sopenharmony_ci */
5824e1051a39Sopenharmony_cistatic int test_custom_exts(int tst)
5825e1051a39Sopenharmony_ci{
5826e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
5827e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
5828e1051a39Sopenharmony_ci    int testresult = 0;
5829e1051a39Sopenharmony_ci    static int server = 1;
5830e1051a39Sopenharmony_ci    static int client = 0;
5831e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
5832e1051a39Sopenharmony_ci    unsigned int context;
5833e1051a39Sopenharmony_ci
5834e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
5835e1051a39Sopenharmony_ci    /* Skip tests for TLSv1.2 and below in this case */
5836e1051a39Sopenharmony_ci    if (tst < 3)
5837e1051a39Sopenharmony_ci        return 1;
5838e1051a39Sopenharmony_ci#endif
5839e1051a39Sopenharmony_ci
5840e1051a39Sopenharmony_ci    /* Reset callback counters */
5841e1051a39Sopenharmony_ci    clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
5842e1051a39Sopenharmony_ci    clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
5843e1051a39Sopenharmony_ci    snicb = 0;
5844e1051a39Sopenharmony_ci
5845e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
5846e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
5847e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
5848e1051a39Sopenharmony_ci        goto end;
5849e1051a39Sopenharmony_ci
5850e1051a39Sopenharmony_ci    if (tst == 2
5851e1051a39Sopenharmony_ci            && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
5852e1051a39Sopenharmony_ci                                              TLS1_VERSION, 0,
5853e1051a39Sopenharmony_ci                                              &sctx2, NULL, cert, privkey)))
5854e1051a39Sopenharmony_ci        goto end;
5855e1051a39Sopenharmony_ci
5856e1051a39Sopenharmony_ci
5857e1051a39Sopenharmony_ci    if (tst < 3) {
5858e1051a39Sopenharmony_ci        SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
5859e1051a39Sopenharmony_ci        SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
5860e1051a39Sopenharmony_ci        if (sctx2 != NULL)
5861e1051a39Sopenharmony_ci            SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
5862e1051a39Sopenharmony_ci    }
5863e1051a39Sopenharmony_ci
5864e1051a39Sopenharmony_ci    if (tst == 5) {
5865e1051a39Sopenharmony_ci        context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
5866e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_CERTIFICATE;
5867e1051a39Sopenharmony_ci        SSL_CTX_set_verify(sctx,
5868e1051a39Sopenharmony_ci                           SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
5869e1051a39Sopenharmony_ci                           verify_cb);
5870e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
5871e1051a39Sopenharmony_ci                                                      SSL_FILETYPE_PEM), 1)
5872e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
5873e1051a39Sopenharmony_ci                                                            SSL_FILETYPE_PEM), 1)
5874e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
5875e1051a39Sopenharmony_ci            goto end;
5876e1051a39Sopenharmony_ci    } else if (tst == 4) {
5877e1051a39Sopenharmony_ci        context = SSL_EXT_CLIENT_HELLO
5878e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_2_SERVER_HELLO
5879e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_SERVER_HELLO
5880e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
5881e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_CERTIFICATE
5882e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
5883e1051a39Sopenharmony_ci    } else {
5884e1051a39Sopenharmony_ci        context = SSL_EXT_CLIENT_HELLO
5885e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_2_SERVER_HELLO
5886e1051a39Sopenharmony_ci                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
5887e1051a39Sopenharmony_ci    }
5888e1051a39Sopenharmony_ci
5889e1051a39Sopenharmony_ci    /* Create a client side custom extension */
5890e1051a39Sopenharmony_ci    if (tst == 0) {
5891e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5892e1051a39Sopenharmony_ci                                                     old_add_cb, old_free_cb,
5893e1051a39Sopenharmony_ci                                                     &client, old_parse_cb,
5894e1051a39Sopenharmony_ci                                                     &client)))
5895e1051a39Sopenharmony_ci            goto end;
5896e1051a39Sopenharmony_ci    } else {
5897e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
5898e1051a39Sopenharmony_ci                                              new_add_cb, new_free_cb,
5899e1051a39Sopenharmony_ci                                              &client, new_parse_cb, &client)))
5900e1051a39Sopenharmony_ci            goto end;
5901e1051a39Sopenharmony_ci    }
5902e1051a39Sopenharmony_ci
5903e1051a39Sopenharmony_ci    /* Should not be able to add duplicates */
5904e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
5905e1051a39Sopenharmony_ci                                                  old_add_cb, old_free_cb,
5906e1051a39Sopenharmony_ci                                                  &client, old_parse_cb,
5907e1051a39Sopenharmony_ci                                                  &client))
5908e1051a39Sopenharmony_ci            || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
5909e1051a39Sopenharmony_ci                                                  context, new_add_cb,
5910e1051a39Sopenharmony_ci                                                  new_free_cb, &client,
5911e1051a39Sopenharmony_ci                                                  new_parse_cb, &client)))
5912e1051a39Sopenharmony_ci        goto end;
5913e1051a39Sopenharmony_ci
5914e1051a39Sopenharmony_ci    /* Create a server side custom extension */
5915e1051a39Sopenharmony_ci    if (tst == 0) {
5916e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5917e1051a39Sopenharmony_ci                                                     old_add_cb, old_free_cb,
5918e1051a39Sopenharmony_ci                                                     &server, old_parse_cb,
5919e1051a39Sopenharmony_ci                                                     &server)))
5920e1051a39Sopenharmony_ci            goto end;
5921e1051a39Sopenharmony_ci    } else {
5922e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
5923e1051a39Sopenharmony_ci                                              new_add_cb, new_free_cb,
5924e1051a39Sopenharmony_ci                                              &server, new_parse_cb, &server)))
5925e1051a39Sopenharmony_ci            goto end;
5926e1051a39Sopenharmony_ci        if (sctx2 != NULL
5927e1051a39Sopenharmony_ci                && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
5928e1051a39Sopenharmony_ci                                                     context, new_add_cb,
5929e1051a39Sopenharmony_ci                                                     new_free_cb, &server,
5930e1051a39Sopenharmony_ci                                                     new_parse_cb, &server)))
5931e1051a39Sopenharmony_ci            goto end;
5932e1051a39Sopenharmony_ci    }
5933e1051a39Sopenharmony_ci
5934e1051a39Sopenharmony_ci    /* Should not be able to add duplicates */
5935e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
5936e1051a39Sopenharmony_ci                                                  old_add_cb, old_free_cb,
5937e1051a39Sopenharmony_ci                                                  &server, old_parse_cb,
5938e1051a39Sopenharmony_ci                                                  &server))
5939e1051a39Sopenharmony_ci            || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
5940e1051a39Sopenharmony_ci                                                  context, new_add_cb,
5941e1051a39Sopenharmony_ci                                                  new_free_cb, &server,
5942e1051a39Sopenharmony_ci                                                  new_parse_cb, &server)))
5943e1051a39Sopenharmony_ci        goto end;
5944e1051a39Sopenharmony_ci
5945e1051a39Sopenharmony_ci    if (tst == 2) {
5946e1051a39Sopenharmony_ci        /* Set up SNI */
5947e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
5948e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
5949e1051a39Sopenharmony_ci            goto end;
5950e1051a39Sopenharmony_ci    }
5951e1051a39Sopenharmony_ci
5952e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
5953e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
5954e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
5955e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
5956e1051a39Sopenharmony_ci        goto end;
5957e1051a39Sopenharmony_ci
5958e1051a39Sopenharmony_ci    if (tst == 0) {
5959e1051a39Sopenharmony_ci        if (clntaddoldcb != 1
5960e1051a39Sopenharmony_ci                || clntparseoldcb != 1
5961e1051a39Sopenharmony_ci                || srvaddoldcb != 1
5962e1051a39Sopenharmony_ci                || srvparseoldcb != 1)
5963e1051a39Sopenharmony_ci            goto end;
5964e1051a39Sopenharmony_ci    } else if (tst == 1 || tst == 2 || tst == 3) {
5965e1051a39Sopenharmony_ci        if (clntaddnewcb != 1
5966e1051a39Sopenharmony_ci                || clntparsenewcb != 1
5967e1051a39Sopenharmony_ci                || srvaddnewcb != 1
5968e1051a39Sopenharmony_ci                || srvparsenewcb != 1
5969e1051a39Sopenharmony_ci                || (tst != 2 && snicb != 0)
5970e1051a39Sopenharmony_ci                || (tst == 2 && snicb != 1))
5971e1051a39Sopenharmony_ci            goto end;
5972e1051a39Sopenharmony_ci    } else if (tst == 5) {
5973e1051a39Sopenharmony_ci        if (clntaddnewcb != 1
5974e1051a39Sopenharmony_ci                || clntparsenewcb != 1
5975e1051a39Sopenharmony_ci                || srvaddnewcb != 1
5976e1051a39Sopenharmony_ci                || srvparsenewcb != 1)
5977e1051a39Sopenharmony_ci            goto end;
5978e1051a39Sopenharmony_ci    } else {
5979e1051a39Sopenharmony_ci        /* In this case there 2 NewSessionTicket messages created */
5980e1051a39Sopenharmony_ci        if (clntaddnewcb != 1
5981e1051a39Sopenharmony_ci                || clntparsenewcb != 5
5982e1051a39Sopenharmony_ci                || srvaddnewcb != 5
5983e1051a39Sopenharmony_ci                || srvparsenewcb != 1)
5984e1051a39Sopenharmony_ci            goto end;
5985e1051a39Sopenharmony_ci    }
5986e1051a39Sopenharmony_ci
5987e1051a39Sopenharmony_ci    sess = SSL_get1_session(clientssl);
5988e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
5989e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
5990e1051a39Sopenharmony_ci    SSL_free(serverssl);
5991e1051a39Sopenharmony_ci    SSL_free(clientssl);
5992e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
5993e1051a39Sopenharmony_ci
5994e1051a39Sopenharmony_ci    if (tst == 3 || tst == 5) {
5995e1051a39Sopenharmony_ci        /* We don't bother with the resumption aspects for these tests */
5996e1051a39Sopenharmony_ci        testresult = 1;
5997e1051a39Sopenharmony_ci        goto end;
5998e1051a39Sopenharmony_ci    }
5999e1051a39Sopenharmony_ci
6000e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6001e1051a39Sopenharmony_ci                                      NULL, NULL))
6002e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, sess))
6003e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6004e1051a39Sopenharmony_ci                                               SSL_ERROR_NONE)))
6005e1051a39Sopenharmony_ci        goto end;
6006e1051a39Sopenharmony_ci
6007e1051a39Sopenharmony_ci    /*
6008e1051a39Sopenharmony_ci     * For a resumed session we expect to add the ClientHello extension. For the
6009e1051a39Sopenharmony_ci     * old style callbacks we ignore it on the server side because they set
6010e1051a39Sopenharmony_ci     * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
6011e1051a39Sopenharmony_ci     * them.
6012e1051a39Sopenharmony_ci     */
6013e1051a39Sopenharmony_ci    if (tst == 0) {
6014e1051a39Sopenharmony_ci        if (clntaddoldcb != 2
6015e1051a39Sopenharmony_ci                || clntparseoldcb != 1
6016e1051a39Sopenharmony_ci                || srvaddoldcb != 1
6017e1051a39Sopenharmony_ci                || srvparseoldcb != 1)
6018e1051a39Sopenharmony_ci            goto end;
6019e1051a39Sopenharmony_ci    } else if (tst == 1 || tst == 2 || tst == 3) {
6020e1051a39Sopenharmony_ci        if (clntaddnewcb != 2
6021e1051a39Sopenharmony_ci                || clntparsenewcb != 2
6022e1051a39Sopenharmony_ci                || srvaddnewcb != 2
6023e1051a39Sopenharmony_ci                || srvparsenewcb != 2)
6024e1051a39Sopenharmony_ci            goto end;
6025e1051a39Sopenharmony_ci    } else {
6026e1051a39Sopenharmony_ci        /*
6027e1051a39Sopenharmony_ci         * No Certificate message extensions in the resumption handshake,
6028e1051a39Sopenharmony_ci         * 2 NewSessionTickets in the initial handshake, 1 in the resumption
6029e1051a39Sopenharmony_ci         */
6030e1051a39Sopenharmony_ci        if (clntaddnewcb != 2
6031e1051a39Sopenharmony_ci                || clntparsenewcb != 8
6032e1051a39Sopenharmony_ci                || srvaddnewcb != 8
6033e1051a39Sopenharmony_ci                || srvparsenewcb != 2)
6034e1051a39Sopenharmony_ci            goto end;
6035e1051a39Sopenharmony_ci    }
6036e1051a39Sopenharmony_ci
6037e1051a39Sopenharmony_ci    testresult = 1;
6038e1051a39Sopenharmony_ci
6039e1051a39Sopenharmony_ciend:
6040e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
6041e1051a39Sopenharmony_ci    SSL_free(serverssl);
6042e1051a39Sopenharmony_ci    SSL_free(clientssl);
6043e1051a39Sopenharmony_ci    SSL_CTX_free(sctx2);
6044e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6045e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6046e1051a39Sopenharmony_ci    return testresult;
6047e1051a39Sopenharmony_ci}
6048e1051a39Sopenharmony_ci
6049e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
6050e1051a39Sopenharmony_ci
6051e1051a39Sopenharmony_ci#define  SYNTHV1CONTEXT     (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
6052e1051a39Sopenharmony_ci                             | SSL_EXT_CLIENT_HELLO \
6053e1051a39Sopenharmony_ci                             | SSL_EXT_TLS1_2_SERVER_HELLO \
6054e1051a39Sopenharmony_ci                             | SSL_EXT_IGNORE_ON_RESUMPTION)
6055e1051a39Sopenharmony_ci
6056e1051a39Sopenharmony_ci#define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
6057e1051a39Sopenharmony_ci                      | SSL_EXT_TLS1_2_SERVER_HELLO \
6058e1051a39Sopenharmony_ci                      | SSL_EXT_CLIENT_HELLO)
6059e1051a39Sopenharmony_ci
6060e1051a39Sopenharmony_ci#define SERVERINFO_CUSTOM                                 \
6061e1051a39Sopenharmony_ci    0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
6062e1051a39Sopenharmony_ci    0x00, 0x03,                                           \
6063e1051a39Sopenharmony_ci    0x04, 0x05, 0x06                                      \
6064e1051a39Sopenharmony_ci
6065e1051a39Sopenharmony_cistatic const unsigned char serverinfo_custom_tls13[] = {
6066e1051a39Sopenharmony_ci    0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
6067e1051a39Sopenharmony_ci    SERVERINFO_CUSTOM
6068e1051a39Sopenharmony_ci};
6069e1051a39Sopenharmony_cistatic const unsigned char serverinfo_custom_v2[] = {
6070e1051a39Sopenharmony_ci    0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff,  SYNTHV1CONTEXT & 0xff,
6071e1051a39Sopenharmony_ci    SERVERINFO_CUSTOM
6072e1051a39Sopenharmony_ci};
6073e1051a39Sopenharmony_cistatic const unsigned char serverinfo_custom_v1[] = {
6074e1051a39Sopenharmony_ci    SERVERINFO_CUSTOM
6075e1051a39Sopenharmony_ci};
6076e1051a39Sopenharmony_cistatic const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
6077e1051a39Sopenharmony_cistatic const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
6078e1051a39Sopenharmony_cistatic const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
6079e1051a39Sopenharmony_ci
6080e1051a39Sopenharmony_cistatic int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
6081e1051a39Sopenharmony_ci                                      unsigned int context,
6082e1051a39Sopenharmony_ci                                      const unsigned char *in,
6083e1051a39Sopenharmony_ci                                      size_t inlen, X509 *x,
6084e1051a39Sopenharmony_ci                                      size_t chainidx, int *al,
6085e1051a39Sopenharmony_ci                                      void *parse_arg)
6086e1051a39Sopenharmony_ci{
6087e1051a39Sopenharmony_ci    const size_t len = serverinfo_custom_v1_len;
6088e1051a39Sopenharmony_ci    const unsigned char *si = &serverinfo_custom_v1[len - 3];
6089e1051a39Sopenharmony_ci    int *p_cb_result = (int*)parse_arg;
6090e1051a39Sopenharmony_ci    *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
6091e1051a39Sopenharmony_ci    return 1;
6092e1051a39Sopenharmony_ci}
6093e1051a39Sopenharmony_ci
6094e1051a39Sopenharmony_cistatic int test_serverinfo_custom(const int idx)
6095e1051a39Sopenharmony_ci{
6096e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
6097e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6098e1051a39Sopenharmony_ci    int testresult = 0;
6099e1051a39Sopenharmony_ci    int cb_result = 0;
6100e1051a39Sopenharmony_ci
6101e1051a39Sopenharmony_ci    /*
6102e1051a39Sopenharmony_ci     * Following variables are set in the switch statement
6103e1051a39Sopenharmony_ci     *  according to the test iteration.
6104e1051a39Sopenharmony_ci     * Default values do not make much sense: test would fail with them.
6105e1051a39Sopenharmony_ci     */
6106e1051a39Sopenharmony_ci    int serverinfo_version = 0;
6107e1051a39Sopenharmony_ci    int protocol_version = 0;
6108e1051a39Sopenharmony_ci    unsigned int extension_context = 0;
6109e1051a39Sopenharmony_ci    const unsigned char *si = NULL;
6110e1051a39Sopenharmony_ci    size_t si_len = 0;
6111e1051a39Sopenharmony_ci
6112e1051a39Sopenharmony_ci    const int call_use_serverinfo_ex = idx > 0;
6113e1051a39Sopenharmony_ci    switch (idx) {
6114e1051a39Sopenharmony_ci    case 0: /* FALLTHROUGH */
6115e1051a39Sopenharmony_ci    case 1:
6116e1051a39Sopenharmony_ci        serverinfo_version = SSL_SERVERINFOV1;
6117e1051a39Sopenharmony_ci        protocol_version = TLS1_2_VERSION;
6118e1051a39Sopenharmony_ci        extension_context = SYNTHV1CONTEXT;
6119e1051a39Sopenharmony_ci        si = serverinfo_custom_v1;
6120e1051a39Sopenharmony_ci        si_len = serverinfo_custom_v1_len;
6121e1051a39Sopenharmony_ci        break;
6122e1051a39Sopenharmony_ci    case 2:
6123e1051a39Sopenharmony_ci        serverinfo_version = SSL_SERVERINFOV2;
6124e1051a39Sopenharmony_ci        protocol_version = TLS1_2_VERSION;
6125e1051a39Sopenharmony_ci        extension_context = SYNTHV1CONTEXT;
6126e1051a39Sopenharmony_ci        si = serverinfo_custom_v2;
6127e1051a39Sopenharmony_ci        si_len = serverinfo_custom_v2_len;
6128e1051a39Sopenharmony_ci        break;
6129e1051a39Sopenharmony_ci    case 3:
6130e1051a39Sopenharmony_ci        serverinfo_version = SSL_SERVERINFOV2;
6131e1051a39Sopenharmony_ci        protocol_version = TLS1_3_VERSION;
6132e1051a39Sopenharmony_ci        extension_context = TLS13CONTEXT;
6133e1051a39Sopenharmony_ci        si = serverinfo_custom_tls13;
6134e1051a39Sopenharmony_ci        si_len = serverinfo_custom_tls13_len;
6135e1051a39Sopenharmony_ci        break;
6136e1051a39Sopenharmony_ci    }
6137e1051a39Sopenharmony_ci
6138e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx,
6139e1051a39Sopenharmony_ci                                       TLS_method(),
6140e1051a39Sopenharmony_ci                                       TLS_method(),
6141e1051a39Sopenharmony_ci                                       protocol_version,
6142e1051a39Sopenharmony_ci                                       protocol_version,
6143e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
6144e1051a39Sopenharmony_ci        goto end;
6145e1051a39Sopenharmony_ci
6146e1051a39Sopenharmony_ci    if (call_use_serverinfo_ex) {
6147e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
6148e1051a39Sopenharmony_ci                                                 si, si_len)))
6149e1051a39Sopenharmony_ci            goto end;
6150e1051a39Sopenharmony_ci    } else {
6151e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
6152e1051a39Sopenharmony_ci            goto end;
6153e1051a39Sopenharmony_ci    }
6154e1051a39Sopenharmony_ci
6155e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
6156e1051a39Sopenharmony_ci                                          extension_context,
6157e1051a39Sopenharmony_ci                                          NULL, NULL, NULL,
6158e1051a39Sopenharmony_ci                                          serverinfo_custom_parse_cb,
6159e1051a39Sopenharmony_ci                                          &cb_result))
6160e1051a39Sopenharmony_ci        || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6161e1051a39Sopenharmony_ci                                         NULL, NULL))
6162e1051a39Sopenharmony_ci        || !TEST_true(create_ssl_connection(serverssl, clientssl,
6163e1051a39Sopenharmony_ci                                            SSL_ERROR_NONE))
6164e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
6165e1051a39Sopenharmony_ci        goto end;
6166e1051a39Sopenharmony_ci
6167e1051a39Sopenharmony_ci    if (!TEST_true(cb_result))
6168e1051a39Sopenharmony_ci        goto end;
6169e1051a39Sopenharmony_ci
6170e1051a39Sopenharmony_ci    testresult = 1;
6171e1051a39Sopenharmony_ci
6172e1051a39Sopenharmony_ci end:
6173e1051a39Sopenharmony_ci    SSL_free(serverssl);
6174e1051a39Sopenharmony_ci    SSL_free(clientssl);
6175e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6176e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6177e1051a39Sopenharmony_ci
6178e1051a39Sopenharmony_ci    return testresult;
6179e1051a39Sopenharmony_ci}
6180e1051a39Sopenharmony_ci#endif
6181e1051a39Sopenharmony_ci
6182e1051a39Sopenharmony_ci/*
6183e1051a39Sopenharmony_ci * Test that SSL_export_keying_material() produces expected results. There are
6184e1051a39Sopenharmony_ci * no test vectors so all we do is test that both sides of the communication
6185e1051a39Sopenharmony_ci * produce the same results for different protocol versions.
6186e1051a39Sopenharmony_ci */
6187e1051a39Sopenharmony_ci#define SMALL_LABEL_LEN 10
6188e1051a39Sopenharmony_ci#define LONG_LABEL_LEN  249
6189e1051a39Sopenharmony_cistatic int test_export_key_mat(int tst)
6190e1051a39Sopenharmony_ci{
6191e1051a39Sopenharmony_ci    int testresult = 0;
6192e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
6193e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6194e1051a39Sopenharmony_ci    const char label[LONG_LABEL_LEN + 1] = "test label";
6195e1051a39Sopenharmony_ci    const unsigned char context[] = "context";
6196e1051a39Sopenharmony_ci    const unsigned char *emptycontext = NULL;
6197e1051a39Sopenharmony_ci    unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
6198e1051a39Sopenharmony_ci    unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
6199e1051a39Sopenharmony_ci    size_t labellen;
6200e1051a39Sopenharmony_ci    const int protocols[] = {
6201e1051a39Sopenharmony_ci        TLS1_VERSION,
6202e1051a39Sopenharmony_ci        TLS1_1_VERSION,
6203e1051a39Sopenharmony_ci        TLS1_2_VERSION,
6204e1051a39Sopenharmony_ci        TLS1_3_VERSION,
6205e1051a39Sopenharmony_ci        TLS1_3_VERSION,
6206e1051a39Sopenharmony_ci        TLS1_3_VERSION
6207e1051a39Sopenharmony_ci    };
6208e1051a39Sopenharmony_ci
6209e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1
6210e1051a39Sopenharmony_ci    if (tst == 0)
6211e1051a39Sopenharmony_ci        return 1;
6212e1051a39Sopenharmony_ci#endif
6213e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_1
6214e1051a39Sopenharmony_ci    if (tst == 1)
6215e1051a39Sopenharmony_ci        return 1;
6216e1051a39Sopenharmony_ci#endif
6217e1051a39Sopenharmony_ci    if (is_fips && (tst == 0 || tst == 1))
6218e1051a39Sopenharmony_ci        return 1;
6219e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
6220e1051a39Sopenharmony_ci    if (tst == 2)
6221e1051a39Sopenharmony_ci        return 1;
6222e1051a39Sopenharmony_ci#endif
6223e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
6224e1051a39Sopenharmony_ci    if (tst >= 3)
6225e1051a39Sopenharmony_ci        return 1;
6226e1051a39Sopenharmony_ci#endif
6227e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6228e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
6229e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
6230e1051a39Sopenharmony_ci        goto end;
6231e1051a39Sopenharmony_ci
6232e1051a39Sopenharmony_ci    OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
6233e1051a39Sopenharmony_ci    SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
6234e1051a39Sopenharmony_ci    SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
6235e1051a39Sopenharmony_ci    if ((protocols[tst] < TLS1_2_VERSION) &&
6236e1051a39Sopenharmony_ci        (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
6237e1051a39Sopenharmony_ci        || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
6238e1051a39Sopenharmony_ci        goto end;
6239e1051a39Sopenharmony_ci
6240e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
6241e1051a39Sopenharmony_ci                                      NULL)))
6242e1051a39Sopenharmony_ci        goto end;
6243e1051a39Sopenharmony_ci
6244e1051a39Sopenharmony_ci    /*
6245e1051a39Sopenharmony_ci     * Premature call of SSL_export_keying_material should just fail.
6246e1051a39Sopenharmony_ci     */
6247e1051a39Sopenharmony_ci    if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6248e1051a39Sopenharmony_ci                                                sizeof(ckeymat1), label,
6249e1051a39Sopenharmony_ci                                                SMALL_LABEL_LEN + 1, context,
6250e1051a39Sopenharmony_ci                                                sizeof(context) - 1, 1), 0))
6251e1051a39Sopenharmony_ci        goto end;
6252e1051a39Sopenharmony_ci
6253e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
6254e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
6255e1051a39Sopenharmony_ci        goto end;
6256e1051a39Sopenharmony_ci
6257e1051a39Sopenharmony_ci    if (tst == 5) {
6258e1051a39Sopenharmony_ci        /*
6259e1051a39Sopenharmony_ci         * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
6260e1051a39Sopenharmony_ci         * go over that.
6261e1051a39Sopenharmony_ci         */
6262e1051a39Sopenharmony_ci        if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
6263e1051a39Sopenharmony_ci                                                    sizeof(ckeymat1), label,
6264e1051a39Sopenharmony_ci                                                    LONG_LABEL_LEN + 1, context,
6265e1051a39Sopenharmony_ci                                                    sizeof(context) - 1, 1), 0))
6266e1051a39Sopenharmony_ci            goto end;
6267e1051a39Sopenharmony_ci
6268e1051a39Sopenharmony_ci        testresult = 1;
6269e1051a39Sopenharmony_ci        goto end;
6270e1051a39Sopenharmony_ci    } else if (tst == 4) {
6271e1051a39Sopenharmony_ci        labellen = LONG_LABEL_LEN;
6272e1051a39Sopenharmony_ci    } else {
6273e1051a39Sopenharmony_ci        labellen = SMALL_LABEL_LEN;
6274e1051a39Sopenharmony_ci    }
6275e1051a39Sopenharmony_ci
6276e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
6277e1051a39Sopenharmony_ci                                                sizeof(ckeymat1), label,
6278e1051a39Sopenharmony_ci                                                labellen, context,
6279e1051a39Sopenharmony_ci                                                sizeof(context) - 1, 1), 1)
6280e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
6281e1051a39Sopenharmony_ci                                                       sizeof(ckeymat2), label,
6282e1051a39Sopenharmony_ci                                                       labellen,
6283e1051a39Sopenharmony_ci                                                       emptycontext,
6284e1051a39Sopenharmony_ci                                                       0, 1), 1)
6285e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
6286e1051a39Sopenharmony_ci                                                       sizeof(ckeymat3), label,
6287e1051a39Sopenharmony_ci                                                       labellen,
6288e1051a39Sopenharmony_ci                                                       NULL, 0, 0), 1)
6289e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
6290e1051a39Sopenharmony_ci                                                       sizeof(skeymat1), label,
6291e1051a39Sopenharmony_ci                                                       labellen,
6292e1051a39Sopenharmony_ci                                                       context,
6293e1051a39Sopenharmony_ci                                                       sizeof(context) -1, 1),
6294e1051a39Sopenharmony_ci                            1)
6295e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
6296e1051a39Sopenharmony_ci                                                       sizeof(skeymat2), label,
6297e1051a39Sopenharmony_ci                                                       labellen,
6298e1051a39Sopenharmony_ci                                                       emptycontext,
6299e1051a39Sopenharmony_ci                                                       0, 1), 1)
6300e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
6301e1051a39Sopenharmony_ci                                                       sizeof(skeymat3), label,
6302e1051a39Sopenharmony_ci                                                       labellen,
6303e1051a39Sopenharmony_ci                                                       NULL, 0, 0), 1)
6304e1051a39Sopenharmony_ci               /*
6305e1051a39Sopenharmony_ci                * Check that both sides created the same key material with the
6306e1051a39Sopenharmony_ci                * same context.
6307e1051a39Sopenharmony_ci                */
6308e1051a39Sopenharmony_ci            || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6309e1051a39Sopenharmony_ci                            sizeof(skeymat1))
6310e1051a39Sopenharmony_ci               /*
6311e1051a39Sopenharmony_ci                * Check that both sides created the same key material with an
6312e1051a39Sopenharmony_ci                * empty context.
6313e1051a39Sopenharmony_ci                */
6314e1051a39Sopenharmony_ci            || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6315e1051a39Sopenharmony_ci                            sizeof(skeymat2))
6316e1051a39Sopenharmony_ci               /*
6317e1051a39Sopenharmony_ci                * Check that both sides created the same key material without a
6318e1051a39Sopenharmony_ci                * context.
6319e1051a39Sopenharmony_ci                */
6320e1051a39Sopenharmony_ci            || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
6321e1051a39Sopenharmony_ci                            sizeof(skeymat3))
6322e1051a39Sopenharmony_ci               /* Different contexts should produce different results */
6323e1051a39Sopenharmony_ci            || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6324e1051a39Sopenharmony_ci                            sizeof(ckeymat2)))
6325e1051a39Sopenharmony_ci        goto end;
6326e1051a39Sopenharmony_ci
6327e1051a39Sopenharmony_ci    /*
6328e1051a39Sopenharmony_ci     * Check that an empty context and no context produce different results in
6329e1051a39Sopenharmony_ci     * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
6330e1051a39Sopenharmony_ci     */
6331e1051a39Sopenharmony_ci    if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
6332e1051a39Sopenharmony_ci                                  sizeof(ckeymat3)))
6333e1051a39Sopenharmony_ci            || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
6334e1051a39Sopenharmony_ci                                         sizeof(ckeymat3))))
6335e1051a39Sopenharmony_ci        goto end;
6336e1051a39Sopenharmony_ci
6337e1051a39Sopenharmony_ci    testresult = 1;
6338e1051a39Sopenharmony_ci
6339e1051a39Sopenharmony_ci end:
6340e1051a39Sopenharmony_ci    SSL_free(serverssl);
6341e1051a39Sopenharmony_ci    SSL_free(clientssl);
6342e1051a39Sopenharmony_ci    SSL_CTX_free(sctx2);
6343e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6344e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6345e1051a39Sopenharmony_ci
6346e1051a39Sopenharmony_ci    return testresult;
6347e1051a39Sopenharmony_ci}
6348e1051a39Sopenharmony_ci
6349e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
6350e1051a39Sopenharmony_ci/*
6351e1051a39Sopenharmony_ci * Test that SSL_export_keying_material_early() produces expected
6352e1051a39Sopenharmony_ci * results. There are no test vectors so all we do is test that both
6353e1051a39Sopenharmony_ci * sides of the communication produce the same results for different
6354e1051a39Sopenharmony_ci * protocol versions.
6355e1051a39Sopenharmony_ci */
6356e1051a39Sopenharmony_cistatic int test_export_key_mat_early(int idx)
6357e1051a39Sopenharmony_ci{
6358e1051a39Sopenharmony_ci    static const char label[] = "test label";
6359e1051a39Sopenharmony_ci    static const unsigned char context[] = "context";
6360e1051a39Sopenharmony_ci    int testresult = 0;
6361e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6362e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6363e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
6364e1051a39Sopenharmony_ci    const unsigned char *emptycontext = NULL;
6365e1051a39Sopenharmony_ci    unsigned char ckeymat1[80], ckeymat2[80];
6366e1051a39Sopenharmony_ci    unsigned char skeymat1[80], skeymat2[80];
6367e1051a39Sopenharmony_ci    unsigned char buf[1];
6368e1051a39Sopenharmony_ci    size_t readbytes, written;
6369e1051a39Sopenharmony_ci
6370e1051a39Sopenharmony_ci    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
6371e1051a39Sopenharmony_ci                                        &sess, idx, SHA384_DIGEST_LENGTH)))
6372e1051a39Sopenharmony_ci        goto end;
6373e1051a39Sopenharmony_ci
6374e1051a39Sopenharmony_ci    /* Here writing 0 length early data is enough. */
6375e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
6376e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
6377e1051a39Sopenharmony_ci                                                &readbytes),
6378e1051a39Sopenharmony_ci                            SSL_READ_EARLY_DATA_ERROR)
6379e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_early_data_status(serverssl),
6380e1051a39Sopenharmony_ci                            SSL_EARLY_DATA_ACCEPTED))
6381e1051a39Sopenharmony_ci        goto end;
6382e1051a39Sopenharmony_ci
6383e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_export_keying_material_early(
6384e1051a39Sopenharmony_ci                     clientssl, ckeymat1, sizeof(ckeymat1), label,
6385e1051a39Sopenharmony_ci                     sizeof(label) - 1, context, sizeof(context) - 1), 1)
6386e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material_early(
6387e1051a39Sopenharmony_ci                            clientssl, ckeymat2, sizeof(ckeymat2), label,
6388e1051a39Sopenharmony_ci                            sizeof(label) - 1, emptycontext, 0), 1)
6389e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material_early(
6390e1051a39Sopenharmony_ci                            serverssl, skeymat1, sizeof(skeymat1), label,
6391e1051a39Sopenharmony_ci                            sizeof(label) - 1, context, sizeof(context) - 1), 1)
6392e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_export_keying_material_early(
6393e1051a39Sopenharmony_ci                            serverssl, skeymat2, sizeof(skeymat2), label,
6394e1051a39Sopenharmony_ci                            sizeof(label) - 1, emptycontext, 0), 1)
6395e1051a39Sopenharmony_ci               /*
6396e1051a39Sopenharmony_ci                * Check that both sides created the same key material with the
6397e1051a39Sopenharmony_ci                * same context.
6398e1051a39Sopenharmony_ci                */
6399e1051a39Sopenharmony_ci            || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
6400e1051a39Sopenharmony_ci                            sizeof(skeymat1))
6401e1051a39Sopenharmony_ci               /*
6402e1051a39Sopenharmony_ci                * Check that both sides created the same key material with an
6403e1051a39Sopenharmony_ci                * empty context.
6404e1051a39Sopenharmony_ci                */
6405e1051a39Sopenharmony_ci            || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
6406e1051a39Sopenharmony_ci                            sizeof(skeymat2))
6407e1051a39Sopenharmony_ci               /* Different contexts should produce different results */
6408e1051a39Sopenharmony_ci            || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
6409e1051a39Sopenharmony_ci                            sizeof(ckeymat2)))
6410e1051a39Sopenharmony_ci        goto end;
6411e1051a39Sopenharmony_ci
6412e1051a39Sopenharmony_ci    testresult = 1;
6413e1051a39Sopenharmony_ci
6414e1051a39Sopenharmony_ci end:
6415e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
6416e1051a39Sopenharmony_ci    SSL_SESSION_free(clientpsk);
6417e1051a39Sopenharmony_ci    SSL_SESSION_free(serverpsk);
6418e1051a39Sopenharmony_ci    clientpsk = serverpsk = NULL;
6419e1051a39Sopenharmony_ci    SSL_free(serverssl);
6420e1051a39Sopenharmony_ci    SSL_free(clientssl);
6421e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6422e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6423e1051a39Sopenharmony_ci
6424e1051a39Sopenharmony_ci    return testresult;
6425e1051a39Sopenharmony_ci}
6426e1051a39Sopenharmony_ci
6427e1051a39Sopenharmony_ci#define NUM_KEY_UPDATE_MESSAGES 40
6428e1051a39Sopenharmony_ci/*
6429e1051a39Sopenharmony_ci * Test KeyUpdate.
6430e1051a39Sopenharmony_ci */
6431e1051a39Sopenharmony_cistatic int test_key_update(void)
6432e1051a39Sopenharmony_ci{
6433e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6434e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6435e1051a39Sopenharmony_ci    int testresult = 0, i, j;
6436e1051a39Sopenharmony_ci    char buf[20];
6437e1051a39Sopenharmony_ci    static char *mess = "A test message";
6438e1051a39Sopenharmony_ci
6439e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6440e1051a39Sopenharmony_ci                                       TLS_client_method(),
6441e1051a39Sopenharmony_ci                                       TLS1_3_VERSION,
6442e1051a39Sopenharmony_ci                                       0,
6443e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
6444e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6445e1051a39Sopenharmony_ci                                             NULL, NULL))
6446e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6447e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6448e1051a39Sopenharmony_ci        goto end;
6449e1051a39Sopenharmony_ci
6450e1051a39Sopenharmony_ci    for (j = 0; j < 2; j++) {
6451e1051a39Sopenharmony_ci        /* Send lots of KeyUpdate messages */
6452e1051a39Sopenharmony_ci        for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
6453e1051a39Sopenharmony_ci            if (!TEST_true(SSL_key_update(clientssl,
6454e1051a39Sopenharmony_ci                                          (j == 0)
6455e1051a39Sopenharmony_ci                                          ? SSL_KEY_UPDATE_NOT_REQUESTED
6456e1051a39Sopenharmony_ci                                          : SSL_KEY_UPDATE_REQUESTED))
6457e1051a39Sopenharmony_ci                    || !TEST_true(SSL_do_handshake(clientssl)))
6458e1051a39Sopenharmony_ci                goto end;
6459e1051a39Sopenharmony_ci        }
6460e1051a39Sopenharmony_ci
6461e1051a39Sopenharmony_ci        /* Check that sending and receiving app data is ok */
6462e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
6463e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
6464e1051a39Sopenharmony_ci                                         strlen(mess)))
6465e1051a39Sopenharmony_ci            goto end;
6466e1051a39Sopenharmony_ci
6467e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
6468e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
6469e1051a39Sopenharmony_ci                                         strlen(mess)))
6470e1051a39Sopenharmony_ci            goto end;
6471e1051a39Sopenharmony_ci    }
6472e1051a39Sopenharmony_ci
6473e1051a39Sopenharmony_ci    testresult = 1;
6474e1051a39Sopenharmony_ci
6475e1051a39Sopenharmony_ci end:
6476e1051a39Sopenharmony_ci    SSL_free(serverssl);
6477e1051a39Sopenharmony_ci    SSL_free(clientssl);
6478e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6479e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6480e1051a39Sopenharmony_ci
6481e1051a39Sopenharmony_ci    return testresult;
6482e1051a39Sopenharmony_ci}
6483e1051a39Sopenharmony_ci
6484e1051a39Sopenharmony_ci/*
6485e1051a39Sopenharmony_ci * Test we can handle a KeyUpdate (update requested) message while
6486e1051a39Sopenharmony_ci * write data is pending in peer.
6487e1051a39Sopenharmony_ci * Test 0: Client sends KeyUpdate while Server is writing
6488e1051a39Sopenharmony_ci * Test 1: Server sends KeyUpdate while Client is writing
6489e1051a39Sopenharmony_ci */
6490e1051a39Sopenharmony_cistatic int test_key_update_peer_in_write(int tst)
6491e1051a39Sopenharmony_ci{
6492e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6493e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6494e1051a39Sopenharmony_ci    int testresult = 0;
6495e1051a39Sopenharmony_ci    char buf[20];
6496e1051a39Sopenharmony_ci    static char *mess = "A test message";
6497e1051a39Sopenharmony_ci    BIO *bretry = BIO_new(bio_s_always_retry());
6498e1051a39Sopenharmony_ci    BIO *tmp = NULL;
6499e1051a39Sopenharmony_ci    SSL *peerupdate = NULL, *peerwrite = NULL;
6500e1051a39Sopenharmony_ci
6501e1051a39Sopenharmony_ci    if (!TEST_ptr(bretry)
6502e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6503e1051a39Sopenharmony_ci                                              TLS_client_method(),
6504e1051a39Sopenharmony_ci                                              TLS1_3_VERSION,
6505e1051a39Sopenharmony_ci                                              0,
6506e1051a39Sopenharmony_ci                                              &sctx, &cctx, cert, privkey))
6507e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6508e1051a39Sopenharmony_ci                                             NULL, NULL))
6509e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6510e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6511e1051a39Sopenharmony_ci        goto end;
6512e1051a39Sopenharmony_ci
6513e1051a39Sopenharmony_ci    peerupdate = tst == 0 ? clientssl : serverssl;
6514e1051a39Sopenharmony_ci    peerwrite = tst == 0 ? serverssl : clientssl;
6515e1051a39Sopenharmony_ci
6516e1051a39Sopenharmony_ci    if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
6517e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
6518e1051a39Sopenharmony_ci        goto end;
6519e1051a39Sopenharmony_ci
6520e1051a39Sopenharmony_ci    /* Swap the writing endpoint's write BIO to force a retry */
6521e1051a39Sopenharmony_ci    tmp = SSL_get_wbio(peerwrite);
6522e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6523e1051a39Sopenharmony_ci        tmp = NULL;
6524e1051a39Sopenharmony_ci        goto end;
6525e1051a39Sopenharmony_ci    }
6526e1051a39Sopenharmony_ci    SSL_set0_wbio(peerwrite, bretry);
6527e1051a39Sopenharmony_ci    bretry = NULL;
6528e1051a39Sopenharmony_ci
6529e1051a39Sopenharmony_ci    /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
6530e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
6531e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
6532e1051a39Sopenharmony_ci        goto end;
6533e1051a39Sopenharmony_ci
6534e1051a39Sopenharmony_ci    /* Reinstate the original writing endpoint's write BIO */
6535e1051a39Sopenharmony_ci    SSL_set0_wbio(peerwrite, tmp);
6536e1051a39Sopenharmony_ci    tmp = NULL;
6537e1051a39Sopenharmony_ci
6538e1051a39Sopenharmony_ci    /* Now read some data - we will read the key update */
6539e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
6540e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
6541e1051a39Sopenharmony_ci        goto end;
6542e1051a39Sopenharmony_ci
6543e1051a39Sopenharmony_ci    /*
6544e1051a39Sopenharmony_ci     * Complete the write we started previously and read it from the other
6545e1051a39Sopenharmony_ci     * endpoint
6546e1051a39Sopenharmony_ci     */
6547e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6548e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6549e1051a39Sopenharmony_ci        goto end;
6550e1051a39Sopenharmony_ci
6551e1051a39Sopenharmony_ci    /* Write more data to ensure we send the KeyUpdate message back */
6552e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
6553e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
6554e1051a39Sopenharmony_ci        goto end;
6555e1051a39Sopenharmony_ci
6556e1051a39Sopenharmony_ci    testresult = 1;
6557e1051a39Sopenharmony_ci
6558e1051a39Sopenharmony_ci end:
6559e1051a39Sopenharmony_ci    SSL_free(serverssl);
6560e1051a39Sopenharmony_ci    SSL_free(clientssl);
6561e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6562e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6563e1051a39Sopenharmony_ci    BIO_free(bretry);
6564e1051a39Sopenharmony_ci    BIO_free(tmp);
6565e1051a39Sopenharmony_ci
6566e1051a39Sopenharmony_ci    return testresult;
6567e1051a39Sopenharmony_ci}
6568e1051a39Sopenharmony_ci
6569e1051a39Sopenharmony_ci/*
6570e1051a39Sopenharmony_ci * Test we can handle a KeyUpdate (update requested) message while
6571e1051a39Sopenharmony_ci * peer read data is pending after peer accepted keyupdate(the msg header
6572e1051a39Sopenharmony_ci * had been read 5 bytes).
6573e1051a39Sopenharmony_ci * Test 0: Client sends KeyUpdate while Server is reading
6574e1051a39Sopenharmony_ci * Test 1: Server sends KeyUpdate while Client is reading
6575e1051a39Sopenharmony_ci */
6576e1051a39Sopenharmony_cistatic int test_key_update_peer_in_read(int tst)
6577e1051a39Sopenharmony_ci{
6578e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6579e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6580e1051a39Sopenharmony_ci    int testresult = 0;
6581e1051a39Sopenharmony_ci    char prbuf[515], lwbuf[515] = {0};
6582e1051a39Sopenharmony_ci    static char *mess = "A test message";
6583e1051a39Sopenharmony_ci    BIO *lbio = NULL, *pbio = NULL;
6584e1051a39Sopenharmony_ci    SSL *local = NULL, *peer = NULL;
6585e1051a39Sopenharmony_ci
6586e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6587e1051a39Sopenharmony_ci                                              TLS_client_method(),
6588e1051a39Sopenharmony_ci                                              TLS1_3_VERSION,
6589e1051a39Sopenharmony_ci                                              0,
6590e1051a39Sopenharmony_ci                                              &sctx, &cctx, cert, privkey))
6591e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6592e1051a39Sopenharmony_ci                                             NULL, NULL))
6593e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6594e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6595e1051a39Sopenharmony_ci        goto end;
6596e1051a39Sopenharmony_ci
6597e1051a39Sopenharmony_ci    local = tst == 0 ? clientssl : serverssl;
6598e1051a39Sopenharmony_ci    peer = tst == 0 ? serverssl : clientssl;
6599e1051a39Sopenharmony_ci
6600e1051a39Sopenharmony_ci    if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6601e1051a39Sopenharmony_ci        goto end;
6602e1051a39Sopenharmony_ci
6603e1051a39Sopenharmony_ci    SSL_set_bio(local, lbio, lbio);
6604e1051a39Sopenharmony_ci    SSL_set_bio(peer, pbio, pbio);
6605e1051a39Sopenharmony_ci
6606e1051a39Sopenharmony_ci    /*
6607e1051a39Sopenharmony_ci     * we first write keyupdate msg then appdata in local
6608e1051a39Sopenharmony_ci     * write data in local will fail with SSL_ERROR_WANT_WRITE,because
6609e1051a39Sopenharmony_ci     * lwbuf app data msg size + key updata msg size > 512(the size of
6610e1051a39Sopenharmony_ci     * the bio pair buffer)
6611e1051a39Sopenharmony_ci     */
6612e1051a39Sopenharmony_ci    if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6613e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
6614e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6615e1051a39Sopenharmony_ci        goto end;
6616e1051a39Sopenharmony_ci
6617e1051a39Sopenharmony_ci    /*
6618e1051a39Sopenharmony_ci     * first read keyupdate msg in peer in peer
6619e1051a39Sopenharmony_ci     * then read appdata that we know will fail with SSL_ERROR_WANT_READ
6620e1051a39Sopenharmony_ci     */
6621e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
6622e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
6623e1051a39Sopenharmony_ci        goto end;
6624e1051a39Sopenharmony_ci
6625e1051a39Sopenharmony_ci    /* Now write some data in peer - we will write the key update */
6626e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
6627e1051a39Sopenharmony_ci        goto end;
6628e1051a39Sopenharmony_ci
6629e1051a39Sopenharmony_ci    /*
6630e1051a39Sopenharmony_ci     * write data in local previously that we will complete
6631e1051a39Sopenharmony_ci     * read data in peer previously that we will complete
6632e1051a39Sopenharmony_ci     */
6633e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
6634e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
6635e1051a39Sopenharmony_ci        goto end;
6636e1051a39Sopenharmony_ci
6637e1051a39Sopenharmony_ci    /* check that sending and receiving appdata ok */
6638e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6639e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6640e1051a39Sopenharmony_ci        goto end;
6641e1051a39Sopenharmony_ci
6642e1051a39Sopenharmony_ci    testresult = 1;
6643e1051a39Sopenharmony_ci
6644e1051a39Sopenharmony_ci end:
6645e1051a39Sopenharmony_ci    SSL_free(serverssl);
6646e1051a39Sopenharmony_ci    SSL_free(clientssl);
6647e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6648e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6649e1051a39Sopenharmony_ci
6650e1051a39Sopenharmony_ci    return testresult;
6651e1051a39Sopenharmony_ci}
6652e1051a39Sopenharmony_ci
6653e1051a39Sopenharmony_ci/*
6654e1051a39Sopenharmony_ci * Test we can't send a KeyUpdate (update requested) message while
6655e1051a39Sopenharmony_ci * local write data is pending.
6656e1051a39Sopenharmony_ci * Test 0: Client sends KeyUpdate while Client is writing
6657e1051a39Sopenharmony_ci * Test 1: Server sends KeyUpdate while Server is writing
6658e1051a39Sopenharmony_ci */
6659e1051a39Sopenharmony_cistatic int test_key_update_local_in_write(int tst)
6660e1051a39Sopenharmony_ci{
6661e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6662e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6663e1051a39Sopenharmony_ci    int testresult = 0;
6664e1051a39Sopenharmony_ci    char buf[20];
6665e1051a39Sopenharmony_ci    static char *mess = "A test message";
6666e1051a39Sopenharmony_ci    BIO *bretry = BIO_new(bio_s_always_retry());
6667e1051a39Sopenharmony_ci    BIO *tmp = NULL;
6668e1051a39Sopenharmony_ci    SSL *local = NULL, *peer = NULL;
6669e1051a39Sopenharmony_ci
6670e1051a39Sopenharmony_ci    if (!TEST_ptr(bretry)
6671e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6672e1051a39Sopenharmony_ci                                              TLS_client_method(),
6673e1051a39Sopenharmony_ci                                              TLS1_3_VERSION,
6674e1051a39Sopenharmony_ci                                              0,
6675e1051a39Sopenharmony_ci                                              &sctx, &cctx, cert, privkey))
6676e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6677e1051a39Sopenharmony_ci                                             NULL, NULL))
6678e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6679e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6680e1051a39Sopenharmony_ci        goto end;
6681e1051a39Sopenharmony_ci
6682e1051a39Sopenharmony_ci    local = tst == 0 ? clientssl : serverssl;
6683e1051a39Sopenharmony_ci    peer = tst == 0 ? serverssl : clientssl;
6684e1051a39Sopenharmony_ci
6685e1051a39Sopenharmony_ci    /* Swap the writing endpoint's write BIO to force a retry */
6686e1051a39Sopenharmony_ci    tmp = SSL_get_wbio(local);
6687e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
6688e1051a39Sopenharmony_ci        tmp = NULL;
6689e1051a39Sopenharmony_ci        goto end;
6690e1051a39Sopenharmony_ci    }
6691e1051a39Sopenharmony_ci    SSL_set0_wbio(local, bretry);
6692e1051a39Sopenharmony_ci    bretry = NULL;
6693e1051a39Sopenharmony_ci
6694e1051a39Sopenharmony_ci    /* write data in local will fail with SSL_ERROR_WANT_WRITE */
6695e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
6696e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
6697e1051a39Sopenharmony_ci        goto end;
6698e1051a39Sopenharmony_ci
6699e1051a39Sopenharmony_ci    /* Reinstate the original writing endpoint's write BIO */
6700e1051a39Sopenharmony_ci    SSL_set0_wbio(local, tmp);
6701e1051a39Sopenharmony_ci    tmp = NULL;
6702e1051a39Sopenharmony_ci
6703e1051a39Sopenharmony_ci    /* SSL_key_update will fail, because writing in local*/
6704e1051a39Sopenharmony_ci    if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6705e1051a39Sopenharmony_ci        || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
6706e1051a39Sopenharmony_ci    goto end;
6707e1051a39Sopenharmony_ci
6708e1051a39Sopenharmony_ci    ERR_clear_error();
6709e1051a39Sopenharmony_ci    /* write data in local previously that we will complete */
6710e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
6711e1051a39Sopenharmony_ci        goto end;
6712e1051a39Sopenharmony_ci
6713e1051a39Sopenharmony_ci    /* SSL_key_update will succeed because there is no pending write data */
6714e1051a39Sopenharmony_ci    if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6715e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_do_handshake(local), 1))
6716e1051a39Sopenharmony_ci        goto end;
6717e1051a39Sopenharmony_ci
6718e1051a39Sopenharmony_ci    /*
6719e1051a39Sopenharmony_ci     * we write some appdata in local
6720e1051a39Sopenharmony_ci     * read data in peer - we will read the keyupdate msg
6721e1051a39Sopenharmony_ci     */
6722e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6723e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
6724e1051a39Sopenharmony_ci        goto end;
6725e1051a39Sopenharmony_ci
6726e1051a39Sopenharmony_ci    /* Write more peer more data to ensure we send the keyupdate message back */
6727e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6728e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
6729e1051a39Sopenharmony_ci        goto end;
6730e1051a39Sopenharmony_ci
6731e1051a39Sopenharmony_ci    testresult = 1;
6732e1051a39Sopenharmony_ci
6733e1051a39Sopenharmony_ci end:
6734e1051a39Sopenharmony_ci    SSL_free(serverssl);
6735e1051a39Sopenharmony_ci    SSL_free(clientssl);
6736e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6737e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6738e1051a39Sopenharmony_ci    BIO_free(bretry);
6739e1051a39Sopenharmony_ci    BIO_free(tmp);
6740e1051a39Sopenharmony_ci
6741e1051a39Sopenharmony_ci    return testresult;
6742e1051a39Sopenharmony_ci}
6743e1051a39Sopenharmony_ci
6744e1051a39Sopenharmony_ci/*
6745e1051a39Sopenharmony_ci * Test we can handle a KeyUpdate (update requested) message while
6746e1051a39Sopenharmony_ci * local read data is pending(the msg header had been read 5 bytes).
6747e1051a39Sopenharmony_ci * Test 0: Client sends KeyUpdate while Client is reading
6748e1051a39Sopenharmony_ci * Test 1: Server sends KeyUpdate while Server is reading
6749e1051a39Sopenharmony_ci */
6750e1051a39Sopenharmony_cistatic int test_key_update_local_in_read(int tst)
6751e1051a39Sopenharmony_ci{
6752e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6753e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6754e1051a39Sopenharmony_ci    int testresult = 0;
6755e1051a39Sopenharmony_ci    char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
6756e1051a39Sopenharmony_ci    static char *mess = "A test message";
6757e1051a39Sopenharmony_ci    BIO *lbio = NULL, *pbio = NULL;
6758e1051a39Sopenharmony_ci    SSL *local = NULL, *peer = NULL;
6759e1051a39Sopenharmony_ci
6760e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6761e1051a39Sopenharmony_ci                                              TLS_client_method(),
6762e1051a39Sopenharmony_ci                                              TLS1_3_VERSION,
6763e1051a39Sopenharmony_ci                                              0,
6764e1051a39Sopenharmony_ci                                              &sctx, &cctx, cert, privkey))
6765e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6766e1051a39Sopenharmony_ci                                             NULL, NULL))
6767e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6768e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6769e1051a39Sopenharmony_ci        goto end;
6770e1051a39Sopenharmony_ci
6771e1051a39Sopenharmony_ci    local = tst == 0 ? clientssl : serverssl;
6772e1051a39Sopenharmony_ci    peer = tst == 0 ? serverssl : clientssl;
6773e1051a39Sopenharmony_ci
6774e1051a39Sopenharmony_ci    if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
6775e1051a39Sopenharmony_ci        goto end;
6776e1051a39Sopenharmony_ci
6777e1051a39Sopenharmony_ci    SSL_set_bio(local, lbio, lbio);
6778e1051a39Sopenharmony_ci    SSL_set_bio(peer, pbio, pbio);
6779e1051a39Sopenharmony_ci
6780e1051a39Sopenharmony_ci    /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
6781e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
6782e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
6783e1051a39Sopenharmony_ci        goto end;
6784e1051a39Sopenharmony_ci
6785e1051a39Sopenharmony_ci    /* read appdata in local will fail with SSL_ERROR_WANT_READ */
6786e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
6787e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
6788e1051a39Sopenharmony_ci        goto end;
6789e1051a39Sopenharmony_ci
6790e1051a39Sopenharmony_ci    /* SSL_do_handshake will send keyupdate msg */
6791e1051a39Sopenharmony_ci    if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
6792e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_do_handshake(local), 1))
6793e1051a39Sopenharmony_ci        goto end;
6794e1051a39Sopenharmony_ci
6795e1051a39Sopenharmony_ci    /*
6796e1051a39Sopenharmony_ci     * write data in peer previously that we will complete
6797e1051a39Sopenharmony_ci     * read data in local previously that we will complete
6798e1051a39Sopenharmony_ci     */
6799e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
6800e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
6801e1051a39Sopenharmony_ci        goto end;
6802e1051a39Sopenharmony_ci
6803e1051a39Sopenharmony_ci    /*
6804e1051a39Sopenharmony_ci     * write data in local
6805e1051a39Sopenharmony_ci     * read data in peer - we will read the key update
6806e1051a39Sopenharmony_ci     */
6807e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
6808e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
6809e1051a39Sopenharmony_ci        goto end;
6810e1051a39Sopenharmony_ci
6811e1051a39Sopenharmony_ci  /* Write more peer data to ensure we send the keyupdate message back */
6812e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
6813e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
6814e1051a39Sopenharmony_ci        goto end;
6815e1051a39Sopenharmony_ci
6816e1051a39Sopenharmony_ci    testresult = 1;
6817e1051a39Sopenharmony_ci
6818e1051a39Sopenharmony_ci end:
6819e1051a39Sopenharmony_ci    SSL_free(serverssl);
6820e1051a39Sopenharmony_ci    SSL_free(clientssl);
6821e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6822e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6823e1051a39Sopenharmony_ci
6824e1051a39Sopenharmony_ci    return testresult;
6825e1051a39Sopenharmony_ci}
6826e1051a39Sopenharmony_ci#endif /* OSSL_NO_USABLE_TLS1_3 */
6827e1051a39Sopenharmony_ci
6828e1051a39Sopenharmony_cistatic int test_ssl_clear(int idx)
6829e1051a39Sopenharmony_ci{
6830e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6831e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6832e1051a39Sopenharmony_ci    int testresult = 0;
6833e1051a39Sopenharmony_ci
6834e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
6835e1051a39Sopenharmony_ci    if (idx == 1)
6836e1051a39Sopenharmony_ci        return 1;
6837e1051a39Sopenharmony_ci#endif
6838e1051a39Sopenharmony_ci
6839e1051a39Sopenharmony_ci    /* Create an initial connection */
6840e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
6841e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
6842e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
6843e1051a39Sopenharmony_ci            || (idx == 1
6844e1051a39Sopenharmony_ci                && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
6845e1051a39Sopenharmony_ci                                                            TLS1_2_VERSION)))
6846e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
6847e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL))
6848e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6849e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
6850e1051a39Sopenharmony_ci        goto end;
6851e1051a39Sopenharmony_ci
6852e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
6853e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
6854e1051a39Sopenharmony_ci    SSL_free(serverssl);
6855e1051a39Sopenharmony_ci    serverssl = NULL;
6856e1051a39Sopenharmony_ci
6857e1051a39Sopenharmony_ci    /* Clear clientssl - we're going to reuse the object */
6858e1051a39Sopenharmony_ci    if (!TEST_true(SSL_clear(clientssl)))
6859e1051a39Sopenharmony_ci        goto end;
6860e1051a39Sopenharmony_ci
6861e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
6862e1051a39Sopenharmony_ci                                             NULL, NULL))
6863e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
6864e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
6865e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl)))
6866e1051a39Sopenharmony_ci        goto end;
6867e1051a39Sopenharmony_ci
6868e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
6869e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
6870e1051a39Sopenharmony_ci
6871e1051a39Sopenharmony_ci    testresult = 1;
6872e1051a39Sopenharmony_ci
6873e1051a39Sopenharmony_ci end:
6874e1051a39Sopenharmony_ci    SSL_free(serverssl);
6875e1051a39Sopenharmony_ci    SSL_free(clientssl);
6876e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
6877e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
6878e1051a39Sopenharmony_ci
6879e1051a39Sopenharmony_ci    return testresult;
6880e1051a39Sopenharmony_ci}
6881e1051a39Sopenharmony_ci
6882e1051a39Sopenharmony_ci/* Parse CH and retrieve any MFL extension value if present */
6883e1051a39Sopenharmony_cistatic int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
6884e1051a39Sopenharmony_ci{
6885e1051a39Sopenharmony_ci    long len;
6886e1051a39Sopenharmony_ci    unsigned char *data;
6887e1051a39Sopenharmony_ci    PACKET pkt, pkt2, pkt3;
6888e1051a39Sopenharmony_ci    unsigned int MFL_code = 0, type = 0;
6889e1051a39Sopenharmony_ci
6890e1051a39Sopenharmony_ci    if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
6891e1051a39Sopenharmony_ci        goto end;
6892e1051a39Sopenharmony_ci
6893e1051a39Sopenharmony_ci    memset(&pkt, 0, sizeof(pkt));
6894e1051a39Sopenharmony_ci    memset(&pkt2, 0, sizeof(pkt2));
6895e1051a39Sopenharmony_ci    memset(&pkt3, 0, sizeof(pkt3));
6896e1051a39Sopenharmony_ci
6897e1051a39Sopenharmony_ci    if (!TEST_long_gt(len, 0)
6898e1051a39Sopenharmony_ci            || !TEST_true( PACKET_buf_init( &pkt, data, len ) )
6899e1051a39Sopenharmony_ci               /* Skip the record header */
6900e1051a39Sopenharmony_ci            || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
6901e1051a39Sopenharmony_ci               /* Skip the handshake message header */
6902e1051a39Sopenharmony_ci            || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
6903e1051a39Sopenharmony_ci               /* Skip client version and random */
6904e1051a39Sopenharmony_ci            || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
6905e1051a39Sopenharmony_ci                                               + SSL3_RANDOM_SIZE))
6906e1051a39Sopenharmony_ci               /* Skip session id */
6907e1051a39Sopenharmony_ci            || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6908e1051a39Sopenharmony_ci               /* Skip ciphers */
6909e1051a39Sopenharmony_ci            || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
6910e1051a39Sopenharmony_ci               /* Skip compression */
6911e1051a39Sopenharmony_ci            || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
6912e1051a39Sopenharmony_ci               /* Extensions len */
6913e1051a39Sopenharmony_ci            || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
6914e1051a39Sopenharmony_ci        goto end;
6915e1051a39Sopenharmony_ci
6916e1051a39Sopenharmony_ci    /* Loop through all extensions */
6917e1051a39Sopenharmony_ci    while (PACKET_remaining(&pkt2)) {
6918e1051a39Sopenharmony_ci        if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
6919e1051a39Sopenharmony_ci                || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
6920e1051a39Sopenharmony_ci            goto end;
6921e1051a39Sopenharmony_ci
6922e1051a39Sopenharmony_ci        if (type == TLSEXT_TYPE_max_fragment_length) {
6923e1051a39Sopenharmony_ci            if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
6924e1051a39Sopenharmony_ci                    || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
6925e1051a39Sopenharmony_ci                goto end;
6926e1051a39Sopenharmony_ci
6927e1051a39Sopenharmony_ci            *mfl_codemfl_code = MFL_code;
6928e1051a39Sopenharmony_ci            return 1;
6929e1051a39Sopenharmony_ci        }
6930e1051a39Sopenharmony_ci    }
6931e1051a39Sopenharmony_ci
6932e1051a39Sopenharmony_ci end:
6933e1051a39Sopenharmony_ci    return 0;
6934e1051a39Sopenharmony_ci}
6935e1051a39Sopenharmony_ci
6936e1051a39Sopenharmony_ci/* Maximum-Fragment-Length TLS extension mode to test */
6937e1051a39Sopenharmony_cistatic const unsigned char max_fragment_len_test[] = {
6938e1051a39Sopenharmony_ci    TLSEXT_max_fragment_length_512,
6939e1051a39Sopenharmony_ci    TLSEXT_max_fragment_length_1024,
6940e1051a39Sopenharmony_ci    TLSEXT_max_fragment_length_2048,
6941e1051a39Sopenharmony_ci    TLSEXT_max_fragment_length_4096
6942e1051a39Sopenharmony_ci};
6943e1051a39Sopenharmony_ci
6944e1051a39Sopenharmony_cistatic int test_max_fragment_len_ext(int idx_tst)
6945e1051a39Sopenharmony_ci{
6946e1051a39Sopenharmony_ci    SSL_CTX *ctx = NULL;
6947e1051a39Sopenharmony_ci    SSL *con = NULL;
6948e1051a39Sopenharmony_ci    int testresult = 0, MFL_mode = 0;
6949e1051a39Sopenharmony_ci    BIO *rbio, *wbio;
6950e1051a39Sopenharmony_ci
6951e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
6952e1051a39Sopenharmony_ci                                       TLS1_VERSION, 0, NULL, &ctx, NULL,
6953e1051a39Sopenharmony_ci                                       NULL)))
6954e1051a39Sopenharmony_ci        return 0;
6955e1051a39Sopenharmony_ci
6956e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
6957e1051a39Sopenharmony_ci                   ctx, max_fragment_len_test[idx_tst])))
6958e1051a39Sopenharmony_ci        goto end;
6959e1051a39Sopenharmony_ci
6960e1051a39Sopenharmony_ci    con = SSL_new(ctx);
6961e1051a39Sopenharmony_ci    if (!TEST_ptr(con))
6962e1051a39Sopenharmony_ci        goto end;
6963e1051a39Sopenharmony_ci
6964e1051a39Sopenharmony_ci    rbio = BIO_new(BIO_s_mem());
6965e1051a39Sopenharmony_ci    wbio = BIO_new(BIO_s_mem());
6966e1051a39Sopenharmony_ci    if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
6967e1051a39Sopenharmony_ci        BIO_free(rbio);
6968e1051a39Sopenharmony_ci        BIO_free(wbio);
6969e1051a39Sopenharmony_ci        goto end;
6970e1051a39Sopenharmony_ci    }
6971e1051a39Sopenharmony_ci
6972e1051a39Sopenharmony_ci    SSL_set_bio(con, rbio, wbio);
6973e1051a39Sopenharmony_ci
6974e1051a39Sopenharmony_ci    if (!TEST_int_le(SSL_connect(con), 0)) {
6975e1051a39Sopenharmony_ci        /* This shouldn't succeed because we don't have a server! */
6976e1051a39Sopenharmony_ci        goto end;
6977e1051a39Sopenharmony_ci    }
6978e1051a39Sopenharmony_ci
6979e1051a39Sopenharmony_ci    if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
6980e1051a39Sopenharmony_ci        /* no MFL in client hello */
6981e1051a39Sopenharmony_ci        goto end;
6982e1051a39Sopenharmony_ci    if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
6983e1051a39Sopenharmony_ci        goto end;
6984e1051a39Sopenharmony_ci
6985e1051a39Sopenharmony_ci    testresult = 1;
6986e1051a39Sopenharmony_ci
6987e1051a39Sopenharmony_ciend:
6988e1051a39Sopenharmony_ci    SSL_free(con);
6989e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
6990e1051a39Sopenharmony_ci
6991e1051a39Sopenharmony_ci    return testresult;
6992e1051a39Sopenharmony_ci}
6993e1051a39Sopenharmony_ci
6994e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
6995e1051a39Sopenharmony_cistatic int test_pha_key_update(void)
6996e1051a39Sopenharmony_ci{
6997e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
6998e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
6999e1051a39Sopenharmony_ci    int testresult = 0;
7000e1051a39Sopenharmony_ci
7001e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7002e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
7003e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
7004e1051a39Sopenharmony_ci        return 0;
7005e1051a39Sopenharmony_ci
7006e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
7007e1051a39Sopenharmony_ci        || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
7008e1051a39Sopenharmony_ci        || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
7009e1051a39Sopenharmony_ci        || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
7010e1051a39Sopenharmony_ci        goto end;
7011e1051a39Sopenharmony_ci
7012e1051a39Sopenharmony_ci    SSL_CTX_set_post_handshake_auth(cctx, 1);
7013e1051a39Sopenharmony_ci
7014e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7015e1051a39Sopenharmony_ci                                      NULL, NULL)))
7016e1051a39Sopenharmony_ci        goto end;
7017e1051a39Sopenharmony_ci
7018e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7019e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
7020e1051a39Sopenharmony_ci        goto end;
7021e1051a39Sopenharmony_ci
7022e1051a39Sopenharmony_ci    SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
7023e1051a39Sopenharmony_ci    if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
7024e1051a39Sopenharmony_ci        goto end;
7025e1051a39Sopenharmony_ci
7026e1051a39Sopenharmony_ci    if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
7027e1051a39Sopenharmony_ci        goto end;
7028e1051a39Sopenharmony_ci
7029e1051a39Sopenharmony_ci    /* Start handshake on the server */
7030e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
7031e1051a39Sopenharmony_ci        goto end;
7032e1051a39Sopenharmony_ci
7033e1051a39Sopenharmony_ci    /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
7034e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
7035e1051a39Sopenharmony_ci                                         SSL_ERROR_NONE)))
7036e1051a39Sopenharmony_ci        goto end;
7037e1051a39Sopenharmony_ci
7038e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
7039e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
7040e1051a39Sopenharmony_ci
7041e1051a39Sopenharmony_ci    testresult = 1;
7042e1051a39Sopenharmony_ci
7043e1051a39Sopenharmony_ci end:
7044e1051a39Sopenharmony_ci    SSL_free(serverssl);
7045e1051a39Sopenharmony_ci    SSL_free(clientssl);
7046e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
7047e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
7048e1051a39Sopenharmony_ci    return testresult;
7049e1051a39Sopenharmony_ci}
7050e1051a39Sopenharmony_ci#endif
7051e1051a39Sopenharmony_ci
7052e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
7053e1051a39Sopenharmony_ci
7054e1051a39Sopenharmony_cistatic SRP_VBASE *vbase = NULL;
7055e1051a39Sopenharmony_ci
7056e1051a39Sopenharmony_cistatic int ssl_srp_cb(SSL *s, int *ad, void *arg)
7057e1051a39Sopenharmony_ci{
7058e1051a39Sopenharmony_ci    int ret = SSL3_AL_FATAL;
7059e1051a39Sopenharmony_ci    char *username;
7060e1051a39Sopenharmony_ci    SRP_user_pwd *user = NULL;
7061e1051a39Sopenharmony_ci
7062e1051a39Sopenharmony_ci    username = SSL_get_srp_username(s);
7063e1051a39Sopenharmony_ci    if (username == NULL) {
7064e1051a39Sopenharmony_ci        *ad = SSL_AD_INTERNAL_ERROR;
7065e1051a39Sopenharmony_ci        goto err;
7066e1051a39Sopenharmony_ci    }
7067e1051a39Sopenharmony_ci
7068e1051a39Sopenharmony_ci    user = SRP_VBASE_get1_by_user(vbase, username);
7069e1051a39Sopenharmony_ci    if (user == NULL) {
7070e1051a39Sopenharmony_ci        *ad = SSL_AD_INTERNAL_ERROR;
7071e1051a39Sopenharmony_ci        goto err;
7072e1051a39Sopenharmony_ci    }
7073e1051a39Sopenharmony_ci
7074e1051a39Sopenharmony_ci    if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
7075e1051a39Sopenharmony_ci                                 user->info) <= 0) {
7076e1051a39Sopenharmony_ci        *ad = SSL_AD_INTERNAL_ERROR;
7077e1051a39Sopenharmony_ci        goto err;
7078e1051a39Sopenharmony_ci    }
7079e1051a39Sopenharmony_ci
7080e1051a39Sopenharmony_ci    ret = 0;
7081e1051a39Sopenharmony_ci
7082e1051a39Sopenharmony_ci err:
7083e1051a39Sopenharmony_ci    SRP_user_pwd_free(user);
7084e1051a39Sopenharmony_ci    return ret;
7085e1051a39Sopenharmony_ci}
7086e1051a39Sopenharmony_ci
7087e1051a39Sopenharmony_cistatic int create_new_vfile(char *userid, char *password, const char *filename)
7088e1051a39Sopenharmony_ci{
7089e1051a39Sopenharmony_ci    char *gNid = NULL;
7090e1051a39Sopenharmony_ci    OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
7091e1051a39Sopenharmony_ci    TXT_DB *db = NULL;
7092e1051a39Sopenharmony_ci    int ret = 0;
7093e1051a39Sopenharmony_ci    BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
7094e1051a39Sopenharmony_ci    size_t i;
7095e1051a39Sopenharmony_ci
7096e1051a39Sopenharmony_ci    if (!TEST_ptr(dummy) || !TEST_ptr(row))
7097e1051a39Sopenharmony_ci        goto end;
7098e1051a39Sopenharmony_ci
7099e1051a39Sopenharmony_ci    gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
7100e1051a39Sopenharmony_ci                                  &row[DB_srpverifier], NULL, NULL, libctx, NULL);
7101e1051a39Sopenharmony_ci    if (!TEST_ptr(gNid))
7102e1051a39Sopenharmony_ci        goto end;
7103e1051a39Sopenharmony_ci
7104e1051a39Sopenharmony_ci    /*
7105e1051a39Sopenharmony_ci     * The only way to create an empty TXT_DB is to provide a BIO with no data
7106e1051a39Sopenharmony_ci     * in it!
7107e1051a39Sopenharmony_ci     */
7108e1051a39Sopenharmony_ci    db = TXT_DB_read(dummy, DB_NUMBER);
7109e1051a39Sopenharmony_ci    if (!TEST_ptr(db))
7110e1051a39Sopenharmony_ci        goto end;
7111e1051a39Sopenharmony_ci
7112e1051a39Sopenharmony_ci    out = BIO_new_file(filename, "w");
7113e1051a39Sopenharmony_ci    if (!TEST_ptr(out))
7114e1051a39Sopenharmony_ci        goto end;
7115e1051a39Sopenharmony_ci
7116e1051a39Sopenharmony_ci    row[DB_srpid] = OPENSSL_strdup(userid);
7117e1051a39Sopenharmony_ci    row[DB_srptype] = OPENSSL_strdup("V");
7118e1051a39Sopenharmony_ci    row[DB_srpgN] = OPENSSL_strdup(gNid);
7119e1051a39Sopenharmony_ci
7120e1051a39Sopenharmony_ci    if (!TEST_ptr(row[DB_srpid])
7121e1051a39Sopenharmony_ci            || !TEST_ptr(row[DB_srptype])
7122e1051a39Sopenharmony_ci            || !TEST_ptr(row[DB_srpgN])
7123e1051a39Sopenharmony_ci            || !TEST_true(TXT_DB_insert(db, row)))
7124e1051a39Sopenharmony_ci        goto end;
7125e1051a39Sopenharmony_ci
7126e1051a39Sopenharmony_ci    row = NULL;
7127e1051a39Sopenharmony_ci
7128e1051a39Sopenharmony_ci    if (TXT_DB_write(out, db) <= 0)
7129e1051a39Sopenharmony_ci        goto end;
7130e1051a39Sopenharmony_ci
7131e1051a39Sopenharmony_ci    ret = 1;
7132e1051a39Sopenharmony_ci end:
7133e1051a39Sopenharmony_ci    if (row != NULL) {
7134e1051a39Sopenharmony_ci        for (i = 0; i < DB_NUMBER; i++)
7135e1051a39Sopenharmony_ci            OPENSSL_free(row[i]);
7136e1051a39Sopenharmony_ci    }
7137e1051a39Sopenharmony_ci    OPENSSL_free(row);
7138e1051a39Sopenharmony_ci    BIO_free(dummy);
7139e1051a39Sopenharmony_ci    BIO_free(out);
7140e1051a39Sopenharmony_ci    TXT_DB_free(db);
7141e1051a39Sopenharmony_ci
7142e1051a39Sopenharmony_ci    return ret;
7143e1051a39Sopenharmony_ci}
7144e1051a39Sopenharmony_ci
7145e1051a39Sopenharmony_cistatic int create_new_vbase(char *userid, char *password)
7146e1051a39Sopenharmony_ci{
7147e1051a39Sopenharmony_ci    BIGNUM *verifier = NULL, *salt = NULL;
7148e1051a39Sopenharmony_ci    const SRP_gN *lgN = NULL;
7149e1051a39Sopenharmony_ci    SRP_user_pwd *user_pwd = NULL;
7150e1051a39Sopenharmony_ci    int ret = 0;
7151e1051a39Sopenharmony_ci
7152e1051a39Sopenharmony_ci    lgN = SRP_get_default_gN(NULL);
7153e1051a39Sopenharmony_ci    if (!TEST_ptr(lgN))
7154e1051a39Sopenharmony_ci        goto end;
7155e1051a39Sopenharmony_ci
7156e1051a39Sopenharmony_ci    if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
7157e1051a39Sopenharmony_ci                                             lgN->N, lgN->g, libctx, NULL)))
7158e1051a39Sopenharmony_ci        goto end;
7159e1051a39Sopenharmony_ci
7160e1051a39Sopenharmony_ci    user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
7161e1051a39Sopenharmony_ci    if (!TEST_ptr(user_pwd))
7162e1051a39Sopenharmony_ci        goto end;
7163e1051a39Sopenharmony_ci
7164e1051a39Sopenharmony_ci    user_pwd->N = lgN->N;
7165e1051a39Sopenharmony_ci    user_pwd->g = lgN->g;
7166e1051a39Sopenharmony_ci    user_pwd->id = OPENSSL_strdup(userid);
7167e1051a39Sopenharmony_ci    if (!TEST_ptr(user_pwd->id))
7168e1051a39Sopenharmony_ci        goto end;
7169e1051a39Sopenharmony_ci
7170e1051a39Sopenharmony_ci    user_pwd->v = verifier;
7171e1051a39Sopenharmony_ci    user_pwd->s = salt;
7172e1051a39Sopenharmony_ci    verifier = salt = NULL;
7173e1051a39Sopenharmony_ci
7174e1051a39Sopenharmony_ci    if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
7175e1051a39Sopenharmony_ci        goto end;
7176e1051a39Sopenharmony_ci    user_pwd = NULL;
7177e1051a39Sopenharmony_ci
7178e1051a39Sopenharmony_ci    ret = 1;
7179e1051a39Sopenharmony_ciend:
7180e1051a39Sopenharmony_ci    SRP_user_pwd_free(user_pwd);
7181e1051a39Sopenharmony_ci    BN_free(salt);
7182e1051a39Sopenharmony_ci    BN_free(verifier);
7183e1051a39Sopenharmony_ci
7184e1051a39Sopenharmony_ci    return ret;
7185e1051a39Sopenharmony_ci}
7186e1051a39Sopenharmony_ci
7187e1051a39Sopenharmony_ci/*
7188e1051a39Sopenharmony_ci * SRP tests
7189e1051a39Sopenharmony_ci *
7190e1051a39Sopenharmony_ci * Test 0: Simple successful SRP connection, new vbase
7191e1051a39Sopenharmony_ci * Test 1: Connection failure due to bad password, new vbase
7192e1051a39Sopenharmony_ci * Test 2: Simple successful SRP connection, vbase loaded from existing file
7193e1051a39Sopenharmony_ci * Test 3: Connection failure due to bad password, vbase loaded from existing
7194e1051a39Sopenharmony_ci *         file
7195e1051a39Sopenharmony_ci * Test 4: Simple successful SRP connection, vbase loaded from new file
7196e1051a39Sopenharmony_ci * Test 5: Connection failure due to bad password, vbase loaded from new file
7197e1051a39Sopenharmony_ci */
7198e1051a39Sopenharmony_cistatic int test_srp(int tst)
7199e1051a39Sopenharmony_ci{
7200e1051a39Sopenharmony_ci    char *userid = "test", *password = "password", *tstsrpfile;
7201e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
7202e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
7203e1051a39Sopenharmony_ci    int ret, testresult = 0;
7204e1051a39Sopenharmony_ci
7205e1051a39Sopenharmony_ci    vbase = SRP_VBASE_new(NULL);
7206e1051a39Sopenharmony_ci    if (!TEST_ptr(vbase))
7207e1051a39Sopenharmony_ci        goto end;
7208e1051a39Sopenharmony_ci
7209e1051a39Sopenharmony_ci    if (tst == 0 || tst == 1) {
7210e1051a39Sopenharmony_ci        if (!TEST_true(create_new_vbase(userid, password)))
7211e1051a39Sopenharmony_ci            goto end;
7212e1051a39Sopenharmony_ci    } else {
7213e1051a39Sopenharmony_ci        if (tst == 4 || tst == 5) {
7214e1051a39Sopenharmony_ci            if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
7215e1051a39Sopenharmony_ci                goto end;
7216e1051a39Sopenharmony_ci            tstsrpfile = tmpfilename;
7217e1051a39Sopenharmony_ci        } else {
7218e1051a39Sopenharmony_ci            tstsrpfile = srpvfile;
7219e1051a39Sopenharmony_ci        }
7220e1051a39Sopenharmony_ci        if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
7221e1051a39Sopenharmony_ci            goto end;
7222e1051a39Sopenharmony_ci    }
7223e1051a39Sopenharmony_ci
7224e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7225e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION, 0,
7226e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
7227e1051a39Sopenharmony_ci        goto end;
7228e1051a39Sopenharmony_ci
7229e1051a39Sopenharmony_ci    if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
7230e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
7231e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
7232e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
7233e1051a39Sopenharmony_ci            || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
7234e1051a39Sopenharmony_ci        goto end;
7235e1051a39Sopenharmony_ci
7236e1051a39Sopenharmony_ci    if (tst % 2 == 1) {
7237e1051a39Sopenharmony_ci        if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
7238e1051a39Sopenharmony_ci            goto end;
7239e1051a39Sopenharmony_ci    } else {
7240e1051a39Sopenharmony_ci        if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
7241e1051a39Sopenharmony_ci            goto end;
7242e1051a39Sopenharmony_ci    }
7243e1051a39Sopenharmony_ci
7244e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7245e1051a39Sopenharmony_ci                                      NULL, NULL)))
7246e1051a39Sopenharmony_ci        goto end;
7247e1051a39Sopenharmony_ci
7248e1051a39Sopenharmony_ci    ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
7249e1051a39Sopenharmony_ci    if (ret) {
7250e1051a39Sopenharmony_ci        if (!TEST_true(tst % 2 == 0))
7251e1051a39Sopenharmony_ci            goto end;
7252e1051a39Sopenharmony_ci    } else {
7253e1051a39Sopenharmony_ci        if (!TEST_true(tst % 2 == 1))
7254e1051a39Sopenharmony_ci            goto end;
7255e1051a39Sopenharmony_ci    }
7256e1051a39Sopenharmony_ci
7257e1051a39Sopenharmony_ci    testresult = 1;
7258e1051a39Sopenharmony_ci
7259e1051a39Sopenharmony_ci end:
7260e1051a39Sopenharmony_ci    SRP_VBASE_free(vbase);
7261e1051a39Sopenharmony_ci    vbase = NULL;
7262e1051a39Sopenharmony_ci    SSL_free(serverssl);
7263e1051a39Sopenharmony_ci    SSL_free(clientssl);
7264e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
7265e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
7266e1051a39Sopenharmony_ci
7267e1051a39Sopenharmony_ci    return testresult;
7268e1051a39Sopenharmony_ci}
7269e1051a39Sopenharmony_ci#endif
7270e1051a39Sopenharmony_ci
7271e1051a39Sopenharmony_cistatic int info_cb_failed = 0;
7272e1051a39Sopenharmony_cistatic int info_cb_offset = 0;
7273e1051a39Sopenharmony_cistatic int info_cb_this_state = -1;
7274e1051a39Sopenharmony_ci
7275e1051a39Sopenharmony_cistatic struct info_cb_states_st {
7276e1051a39Sopenharmony_ci    int where;
7277e1051a39Sopenharmony_ci    const char *statestr;
7278e1051a39Sopenharmony_ci} info_cb_states[][60] = {
7279e1051a39Sopenharmony_ci    {
7280e1051a39Sopenharmony_ci        /* TLSv1.2 server followed by resumption */
7281e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7282e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7283e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
7284e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
7285e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
7286e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7287e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7288e1051a39Sopenharmony_ci        {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7289e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
7290e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7291e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
7292e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7293e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {0, NULL},
7294e1051a39Sopenharmony_ci    }, {
7295e1051a39Sopenharmony_ci        /* TLSv1.2 client followed by resumption */
7296e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7297e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7298e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
7299e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
7300e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
7301e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7302e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7303e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7304e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7305e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
7306e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCCS"},  {SSL_CB_LOOP, "TWFIN"},
7307e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
7308e1051a39Sopenharmony_ci    }, {
7309e1051a39Sopenharmony_ci        /* TLSv1.3 server followed by resumption */
7310e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7311e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7312e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
7313e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
7314e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
7315e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7316e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
7317e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7318e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7319e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7320e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
7321e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7322e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7323e1051a39Sopenharmony_ci    }, {
7324e1051a39Sopenharmony_ci        /* TLSv1.3 client followed by resumption */
7325e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7326e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
7327e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
7328e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
7329e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWFIN"},  {SSL_CB_HANDSHAKE_DONE, NULL},
7330e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7331e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
7332e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
7333e1051a39Sopenharmony_ci        {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
7334e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
7335e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"},  {SSL_CB_LOOP, "TREE"},
7336e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
7337e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7338e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
7339e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {0, NULL},
7340e1051a39Sopenharmony_ci    }, {
7341e1051a39Sopenharmony_ci        /* TLSv1.3 server, early_data */
7342e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7343e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
7344e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
7345e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7346e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7347e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
7348e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
7349e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {0, NULL},
7350e1051a39Sopenharmony_ci    }, {
7351e1051a39Sopenharmony_ci        /* TLSv1.3 client, early_data */
7352e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
7353e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
7354e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
7355e1051a39Sopenharmony_ci        {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
7356e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
7357e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
7358e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
7359e1051a39Sopenharmony_ci        {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
7360e1051a39Sopenharmony_ci        {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
7361e1051a39Sopenharmony_ci    }, {
7362e1051a39Sopenharmony_ci        {0, NULL},
7363e1051a39Sopenharmony_ci    }
7364e1051a39Sopenharmony_ci};
7365e1051a39Sopenharmony_ci
7366e1051a39Sopenharmony_cistatic void sslapi_info_callback(const SSL *s, int where, int ret)
7367e1051a39Sopenharmony_ci{
7368e1051a39Sopenharmony_ci    struct info_cb_states_st *state = info_cb_states[info_cb_offset];
7369e1051a39Sopenharmony_ci
7370e1051a39Sopenharmony_ci    /* We do not ever expect a connection to fail in this test */
7371e1051a39Sopenharmony_ci    if (!TEST_false(ret == 0)) {
7372e1051a39Sopenharmony_ci        info_cb_failed = 1;
7373e1051a39Sopenharmony_ci        return;
7374e1051a39Sopenharmony_ci    }
7375e1051a39Sopenharmony_ci
7376e1051a39Sopenharmony_ci    /*
7377e1051a39Sopenharmony_ci     * Do some sanity checks. We never expect these things to happen in this
7378e1051a39Sopenharmony_ci     * test
7379e1051a39Sopenharmony_ci     */
7380e1051a39Sopenharmony_ci    if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
7381e1051a39Sopenharmony_ci            || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
7382e1051a39Sopenharmony_ci            || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
7383e1051a39Sopenharmony_ci        info_cb_failed = 1;
7384e1051a39Sopenharmony_ci        return;
7385e1051a39Sopenharmony_ci    }
7386e1051a39Sopenharmony_ci
7387e1051a39Sopenharmony_ci    /* Now check we're in the right state */
7388e1051a39Sopenharmony_ci    if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
7389e1051a39Sopenharmony_ci        info_cb_failed = 1;
7390e1051a39Sopenharmony_ci        return;
7391e1051a39Sopenharmony_ci    }
7392e1051a39Sopenharmony_ci    if ((where & SSL_CB_LOOP) != 0
7393e1051a39Sopenharmony_ci            && !TEST_int_eq(strcmp(SSL_state_string(s),
7394e1051a39Sopenharmony_ci                            state[info_cb_this_state].statestr), 0)) {
7395e1051a39Sopenharmony_ci        info_cb_failed = 1;
7396e1051a39Sopenharmony_ci        return;
7397e1051a39Sopenharmony_ci    }
7398e1051a39Sopenharmony_ci
7399e1051a39Sopenharmony_ci    /*
7400e1051a39Sopenharmony_ci     * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
7401e1051a39Sopenharmony_ci     */
7402e1051a39Sopenharmony_ci    if ((where & SSL_CB_HANDSHAKE_DONE)
7403e1051a39Sopenharmony_ci            && SSL_in_init((SSL *)s) != 0) {
7404e1051a39Sopenharmony_ci        info_cb_failed = 1;
7405e1051a39Sopenharmony_ci        return;
7406e1051a39Sopenharmony_ci    }
7407e1051a39Sopenharmony_ci}
7408e1051a39Sopenharmony_ci
7409e1051a39Sopenharmony_ci/*
7410e1051a39Sopenharmony_ci * Test the info callback gets called when we expect it to.
7411e1051a39Sopenharmony_ci *
7412e1051a39Sopenharmony_ci * Test 0: TLSv1.2, server
7413e1051a39Sopenharmony_ci * Test 1: TLSv1.2, client
7414e1051a39Sopenharmony_ci * Test 2: TLSv1.3, server
7415e1051a39Sopenharmony_ci * Test 3: TLSv1.3, client
7416e1051a39Sopenharmony_ci * Test 4: TLSv1.3, server, early_data
7417e1051a39Sopenharmony_ci * Test 5: TLSv1.3, client, early_data
7418e1051a39Sopenharmony_ci */
7419e1051a39Sopenharmony_cistatic int test_info_callback(int tst)
7420e1051a39Sopenharmony_ci{
7421e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
7422e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
7423e1051a39Sopenharmony_ci    SSL_SESSION *clntsess = NULL;
7424e1051a39Sopenharmony_ci    int testresult = 0;
7425e1051a39Sopenharmony_ci    int tlsvers;
7426e1051a39Sopenharmony_ci
7427e1051a39Sopenharmony_ci    if (tst < 2) {
7428e1051a39Sopenharmony_ci/* We need either ECDHE or DHE for the TLSv1.2 test to work */
7429e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
7430e1051a39Sopenharmony_ci                                    || !defined(OPENSSL_NO_DH))
7431e1051a39Sopenharmony_ci        tlsvers = TLS1_2_VERSION;
7432e1051a39Sopenharmony_ci#else
7433e1051a39Sopenharmony_ci        return 1;
7434e1051a39Sopenharmony_ci#endif
7435e1051a39Sopenharmony_ci    } else {
7436e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
7437e1051a39Sopenharmony_ci        tlsvers = TLS1_3_VERSION;
7438e1051a39Sopenharmony_ci#else
7439e1051a39Sopenharmony_ci        return 1;
7440e1051a39Sopenharmony_ci#endif
7441e1051a39Sopenharmony_ci    }
7442e1051a39Sopenharmony_ci
7443e1051a39Sopenharmony_ci    /* Reset globals */
7444e1051a39Sopenharmony_ci    info_cb_failed = 0;
7445e1051a39Sopenharmony_ci    info_cb_this_state = -1;
7446e1051a39Sopenharmony_ci    info_cb_offset = tst;
7447e1051a39Sopenharmony_ci
7448e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
7449e1051a39Sopenharmony_ci    if (tst >= 4) {
7450e1051a39Sopenharmony_ci        SSL_SESSION *sess = NULL;
7451e1051a39Sopenharmony_ci        size_t written, readbytes;
7452e1051a39Sopenharmony_ci        unsigned char buf[80];
7453e1051a39Sopenharmony_ci
7454e1051a39Sopenharmony_ci        /* early_data tests */
7455e1051a39Sopenharmony_ci        if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
7456e1051a39Sopenharmony_ci                                            &serverssl, &sess, 0,
7457e1051a39Sopenharmony_ci                                            SHA384_DIGEST_LENGTH)))
7458e1051a39Sopenharmony_ci            goto end;
7459e1051a39Sopenharmony_ci
7460e1051a39Sopenharmony_ci        /* We don't actually need this reference */
7461e1051a39Sopenharmony_ci        SSL_SESSION_free(sess);
7462e1051a39Sopenharmony_ci
7463e1051a39Sopenharmony_ci        SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
7464e1051a39Sopenharmony_ci                              sslapi_info_callback);
7465e1051a39Sopenharmony_ci
7466e1051a39Sopenharmony_ci        /* Write and read some early data and then complete the connection */
7467e1051a39Sopenharmony_ci        if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
7468e1051a39Sopenharmony_ci                                            &written))
7469e1051a39Sopenharmony_ci                || !TEST_size_t_eq(written, strlen(MSG1))
7470e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
7471e1051a39Sopenharmony_ci                                                    sizeof(buf), &readbytes),
7472e1051a39Sopenharmony_ci                                SSL_READ_EARLY_DATA_SUCCESS)
7473e1051a39Sopenharmony_ci                || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
7474e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_early_data_status(serverssl),
7475e1051a39Sopenharmony_ci                                SSL_EARLY_DATA_ACCEPTED)
7476e1051a39Sopenharmony_ci                || !TEST_true(create_ssl_connection(serverssl, clientssl,
7477e1051a39Sopenharmony_ci                                                    SSL_ERROR_NONE))
7478e1051a39Sopenharmony_ci                || !TEST_false(info_cb_failed))
7479e1051a39Sopenharmony_ci            goto end;
7480e1051a39Sopenharmony_ci
7481e1051a39Sopenharmony_ci        testresult = 1;
7482e1051a39Sopenharmony_ci        goto end;
7483e1051a39Sopenharmony_ci    }
7484e1051a39Sopenharmony_ci#endif
7485e1051a39Sopenharmony_ci
7486e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7487e1051a39Sopenharmony_ci                                       TLS_client_method(),
7488e1051a39Sopenharmony_ci                                       tlsvers, tlsvers, &sctx, &cctx, cert,
7489e1051a39Sopenharmony_ci                                       privkey)))
7490e1051a39Sopenharmony_ci        goto end;
7491e1051a39Sopenharmony_ci
7492e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
7493e1051a39Sopenharmony_ci        goto end;
7494e1051a39Sopenharmony_ci
7495e1051a39Sopenharmony_ci    /*
7496e1051a39Sopenharmony_ci     * For even numbered tests we check the server callbacks. For odd numbers we
7497e1051a39Sopenharmony_ci     * check the client.
7498e1051a39Sopenharmony_ci     */
7499e1051a39Sopenharmony_ci    SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
7500e1051a39Sopenharmony_ci                              sslapi_info_callback);
7501e1051a39Sopenharmony_ci
7502e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
7503e1051a39Sopenharmony_ci                                          &clientssl, NULL, NULL))
7504e1051a39Sopenharmony_ci        || !TEST_true(create_ssl_connection(serverssl, clientssl,
7505e1051a39Sopenharmony_ci                                            SSL_ERROR_NONE))
7506e1051a39Sopenharmony_ci        || !TEST_false(info_cb_failed))
7507e1051a39Sopenharmony_ci    goto end;
7508e1051a39Sopenharmony_ci
7509e1051a39Sopenharmony_ci
7510e1051a39Sopenharmony_ci
7511e1051a39Sopenharmony_ci    clntsess = SSL_get1_session(clientssl);
7512e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
7513e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
7514e1051a39Sopenharmony_ci    SSL_free(serverssl);
7515e1051a39Sopenharmony_ci    SSL_free(clientssl);
7516e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
7517e1051a39Sopenharmony_ci
7518e1051a39Sopenharmony_ci    /* Now do a resumption */
7519e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
7520e1051a39Sopenharmony_ci                                      NULL))
7521e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, clntsess))
7522e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
7523e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE))
7524e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(clientssl))
7525e1051a39Sopenharmony_ci            || !TEST_false(info_cb_failed))
7526e1051a39Sopenharmony_ci        goto end;
7527e1051a39Sopenharmony_ci
7528e1051a39Sopenharmony_ci    testresult = 1;
7529e1051a39Sopenharmony_ci
7530e1051a39Sopenharmony_ci end:
7531e1051a39Sopenharmony_ci    SSL_free(serverssl);
7532e1051a39Sopenharmony_ci    SSL_free(clientssl);
7533e1051a39Sopenharmony_ci    SSL_SESSION_free(clntsess);
7534e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
7535e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
7536e1051a39Sopenharmony_ci    return testresult;
7537e1051a39Sopenharmony_ci}
7538e1051a39Sopenharmony_ci
7539e1051a39Sopenharmony_cistatic int test_ssl_pending(int tst)
7540e1051a39Sopenharmony_ci{
7541e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
7542e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
7543e1051a39Sopenharmony_ci    int testresult = 0;
7544e1051a39Sopenharmony_ci    char msg[] = "A test message";
7545e1051a39Sopenharmony_ci    char buf[5];
7546e1051a39Sopenharmony_ci    size_t written, readbytes;
7547e1051a39Sopenharmony_ci
7548e1051a39Sopenharmony_ci    if (tst == 0) {
7549e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7550e1051a39Sopenharmony_ci                                           TLS_client_method(),
7551e1051a39Sopenharmony_ci                                           TLS1_VERSION, 0,
7552e1051a39Sopenharmony_ci                                           &sctx, &cctx, cert, privkey)))
7553e1051a39Sopenharmony_ci            goto end;
7554e1051a39Sopenharmony_ci    } else {
7555e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS
7556e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
7557e1051a39Sopenharmony_ci                                           DTLS_client_method(),
7558e1051a39Sopenharmony_ci                                           DTLS1_VERSION, 0,
7559e1051a39Sopenharmony_ci                                           &sctx, &cctx, cert, privkey)))
7560e1051a39Sopenharmony_ci            goto end;
7561e1051a39Sopenharmony_ci
7562e1051a39Sopenharmony_ci# ifdef OPENSSL_NO_DTLS1_2
7563e1051a39Sopenharmony_ci        /* Not supported in the FIPS provider */
7564e1051a39Sopenharmony_ci        if (is_fips) {
7565e1051a39Sopenharmony_ci            testresult = 1;
7566e1051a39Sopenharmony_ci            goto end;
7567e1051a39Sopenharmony_ci        };
7568e1051a39Sopenharmony_ci        /*
7569e1051a39Sopenharmony_ci         * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
7570e1051a39Sopenharmony_ci         * level 0
7571e1051a39Sopenharmony_ci         */
7572e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
7573e1051a39Sopenharmony_ci                || !TEST_true(SSL_CTX_set_cipher_list(cctx,
7574e1051a39Sopenharmony_ci                                                    "DEFAULT:@SECLEVEL=0")))
7575e1051a39Sopenharmony_ci            goto end;
7576e1051a39Sopenharmony_ci# endif
7577e1051a39Sopenharmony_ci#else
7578e1051a39Sopenharmony_ci        return 1;
7579e1051a39Sopenharmony_ci#endif
7580e1051a39Sopenharmony_ci    }
7581e1051a39Sopenharmony_ci
7582e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7583e1051a39Sopenharmony_ci                                             NULL, NULL))
7584e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
7585e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
7586e1051a39Sopenharmony_ci        goto end;
7587e1051a39Sopenharmony_ci
7588e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_pending(clientssl), 0)
7589e1051a39Sopenharmony_ci            || !TEST_false(SSL_has_pending(clientssl))
7590e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_pending(serverssl), 0)
7591e1051a39Sopenharmony_ci            || !TEST_false(SSL_has_pending(serverssl))
7592e1051a39Sopenharmony_ci            || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
7593e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, sizeof(msg))
7594e1051a39Sopenharmony_ci            || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
7595e1051a39Sopenharmony_ci            || !TEST_size_t_eq(readbytes, sizeof(buf))
7596e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
7597e1051a39Sopenharmony_ci            || !TEST_true(SSL_has_pending(clientssl)))
7598e1051a39Sopenharmony_ci        goto end;
7599e1051a39Sopenharmony_ci
7600e1051a39Sopenharmony_ci    testresult = 1;
7601e1051a39Sopenharmony_ci
7602e1051a39Sopenharmony_ci end:
7603e1051a39Sopenharmony_ci    SSL_free(serverssl);
7604e1051a39Sopenharmony_ci    SSL_free(clientssl);
7605e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
7606e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
7607e1051a39Sopenharmony_ci
7608e1051a39Sopenharmony_ci    return testresult;
7609e1051a39Sopenharmony_ci}
7610e1051a39Sopenharmony_ci
7611e1051a39Sopenharmony_cistatic struct {
7612e1051a39Sopenharmony_ci    unsigned int maxprot;
7613e1051a39Sopenharmony_ci    const char *clntciphers;
7614e1051a39Sopenharmony_ci    const char *clnttls13ciphers;
7615e1051a39Sopenharmony_ci    const char *srvrciphers;
7616e1051a39Sopenharmony_ci    const char *srvrtls13ciphers;
7617e1051a39Sopenharmony_ci    const char *shared;
7618e1051a39Sopenharmony_ci    const char *fipsshared;
7619e1051a39Sopenharmony_ci} shared_ciphers_data[] = {
7620e1051a39Sopenharmony_ci/*
7621e1051a39Sopenharmony_ci * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
7622e1051a39Sopenharmony_ci * TLSv1.3 is enabled but TLSv1.2 is disabled.
7623e1051a39Sopenharmony_ci */
7624e1051a39Sopenharmony_ci#if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
7625e1051a39Sopenharmony_ci    {
7626e1051a39Sopenharmony_ci        TLS1_2_VERSION,
7627e1051a39Sopenharmony_ci        "AES128-SHA:AES256-SHA",
7628e1051a39Sopenharmony_ci        NULL,
7629e1051a39Sopenharmony_ci        "AES256-SHA:DHE-RSA-AES128-SHA",
7630e1051a39Sopenharmony_ci        NULL,
7631e1051a39Sopenharmony_ci        "AES256-SHA",
7632e1051a39Sopenharmony_ci        "AES256-SHA"
7633e1051a39Sopenharmony_ci    },
7634e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_CHACHA) \
7635e1051a39Sopenharmony_ci     && !defined(OPENSSL_NO_POLY1305) \
7636e1051a39Sopenharmony_ci     && !defined(OPENSSL_NO_EC)
7637e1051a39Sopenharmony_ci    {
7638e1051a39Sopenharmony_ci        TLS1_2_VERSION,
7639e1051a39Sopenharmony_ci        "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7640e1051a39Sopenharmony_ci        NULL,
7641e1051a39Sopenharmony_ci        "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7642e1051a39Sopenharmony_ci        NULL,
7643e1051a39Sopenharmony_ci        "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
7644e1051a39Sopenharmony_ci        "AES128-SHA"
7645e1051a39Sopenharmony_ci    },
7646e1051a39Sopenharmony_ci# endif
7647e1051a39Sopenharmony_ci    {
7648e1051a39Sopenharmony_ci        TLS1_2_VERSION,
7649e1051a39Sopenharmony_ci        "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
7650e1051a39Sopenharmony_ci        NULL,
7651e1051a39Sopenharmony_ci        "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
7652e1051a39Sopenharmony_ci        NULL,
7653e1051a39Sopenharmony_ci        "AES128-SHA:AES256-SHA",
7654e1051a39Sopenharmony_ci        "AES128-SHA:AES256-SHA"
7655e1051a39Sopenharmony_ci    },
7656e1051a39Sopenharmony_ci    {
7657e1051a39Sopenharmony_ci        TLS1_2_VERSION,
7658e1051a39Sopenharmony_ci        "AES128-SHA:AES256-SHA",
7659e1051a39Sopenharmony_ci        NULL,
7660e1051a39Sopenharmony_ci        "AES128-SHA:DHE-RSA-AES128-SHA",
7661e1051a39Sopenharmony_ci        NULL,
7662e1051a39Sopenharmony_ci        "AES128-SHA",
7663e1051a39Sopenharmony_ci        "AES128-SHA"
7664e1051a39Sopenharmony_ci    },
7665e1051a39Sopenharmony_ci#endif
7666e1051a39Sopenharmony_ci/*
7667e1051a39Sopenharmony_ci * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
7668e1051a39Sopenharmony_ci * enabled.
7669e1051a39Sopenharmony_ci */
7670e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
7671e1051a39Sopenharmony_ci    && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
7672e1051a39Sopenharmony_ci    {
7673e1051a39Sopenharmony_ci        TLS1_3_VERSION,
7674e1051a39Sopenharmony_ci        "AES128-SHA:AES256-SHA",
7675e1051a39Sopenharmony_ci        NULL,
7676e1051a39Sopenharmony_ci        "AES256-SHA:AES128-SHA256",
7677e1051a39Sopenharmony_ci        NULL,
7678e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
7679e1051a39Sopenharmony_ci        "TLS_AES_128_GCM_SHA256:AES256-SHA",
7680e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
7681e1051a39Sopenharmony_ci    },
7682e1051a39Sopenharmony_ci#endif
7683e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
7684e1051a39Sopenharmony_ci    {
7685e1051a39Sopenharmony_ci        TLS1_3_VERSION,
7686e1051a39Sopenharmony_ci        "AES128-SHA",
7687e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384",
7688e1051a39Sopenharmony_ci        "AES256-SHA",
7689e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384",
7690e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384",
7691e1051a39Sopenharmony_ci        "TLS_AES_256_GCM_SHA384"
7692e1051a39Sopenharmony_ci    },
7693e1051a39Sopenharmony_ci#endif
7694e1051a39Sopenharmony_ci};
7695e1051a39Sopenharmony_ci
7696e1051a39Sopenharmony_cistatic int int_test_ssl_get_shared_ciphers(int tst, int clnt)
7697e1051a39Sopenharmony_ci{
7698e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
7699e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
7700e1051a39Sopenharmony_ci    int testresult = 0;
7701e1051a39Sopenharmony_ci    char buf[1024];
7702e1051a39Sopenharmony_ci    OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
7703e1051a39Sopenharmony_ci
7704e1051a39Sopenharmony_ci    if (!TEST_ptr(tmplibctx))
7705e1051a39Sopenharmony_ci        goto end;
7706e1051a39Sopenharmony_ci
7707e1051a39Sopenharmony_ci    /*
7708e1051a39Sopenharmony_ci     * Regardless of whether we're testing with the FIPS provider loaded into
7709e1051a39Sopenharmony_ci     * libctx, we want one peer to always use the full set of ciphersuites
7710e1051a39Sopenharmony_ci     * available. Therefore we use a separate libctx with the default provider
7711e1051a39Sopenharmony_ci     * loaded into it. We run the same tests twice - once with the client side
7712e1051a39Sopenharmony_ci     * having the full set of ciphersuites and once with the server side.
7713e1051a39Sopenharmony_ci     */
7714e1051a39Sopenharmony_ci    if (clnt) {
7715e1051a39Sopenharmony_ci        cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
7716e1051a39Sopenharmony_ci        if (!TEST_ptr(cctx))
7717e1051a39Sopenharmony_ci            goto end;
7718e1051a39Sopenharmony_ci    } else {
7719e1051a39Sopenharmony_ci        sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
7720e1051a39Sopenharmony_ci        if (!TEST_ptr(sctx))
7721e1051a39Sopenharmony_ci            goto end;
7722e1051a39Sopenharmony_ci    }
7723e1051a39Sopenharmony_ci
7724e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7725e1051a39Sopenharmony_ci                                       TLS_client_method(),
7726e1051a39Sopenharmony_ci                                       TLS1_VERSION,
7727e1051a39Sopenharmony_ci                                       shared_ciphers_data[tst].maxprot,
7728e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
7729e1051a39Sopenharmony_ci        goto end;
7730e1051a39Sopenharmony_ci
7731e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
7732e1051a39Sopenharmony_ci                                        shared_ciphers_data[tst].clntciphers))
7733e1051a39Sopenharmony_ci            || (shared_ciphers_data[tst].clnttls13ciphers != NULL
7734e1051a39Sopenharmony_ci                && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
7735e1051a39Sopenharmony_ci                                    shared_ciphers_data[tst].clnttls13ciphers)))
7736e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_cipher_list(sctx,
7737e1051a39Sopenharmony_ci                                        shared_ciphers_data[tst].srvrciphers))
7738e1051a39Sopenharmony_ci            || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
7739e1051a39Sopenharmony_ci                && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
7740e1051a39Sopenharmony_ci                                    shared_ciphers_data[tst].srvrtls13ciphers))))
7741e1051a39Sopenharmony_ci        goto end;
7742e1051a39Sopenharmony_ci
7743e1051a39Sopenharmony_ci
7744e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
7745e1051a39Sopenharmony_ci                                             NULL, NULL))
7746e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
7747e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
7748e1051a39Sopenharmony_ci        goto end;
7749e1051a39Sopenharmony_ci
7750e1051a39Sopenharmony_ci    if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
7751e1051a39Sopenharmony_ci            || !TEST_int_eq(strcmp(buf,
7752e1051a39Sopenharmony_ci                                   is_fips
7753e1051a39Sopenharmony_ci                                   ? shared_ciphers_data[tst].fipsshared
7754e1051a39Sopenharmony_ci                                   : shared_ciphers_data[tst].shared),
7755e1051a39Sopenharmony_ci                                   0)) {
7756e1051a39Sopenharmony_ci        TEST_info("Shared ciphers are: %s\n", buf);
7757e1051a39Sopenharmony_ci        goto end;
7758e1051a39Sopenharmony_ci    }
7759e1051a39Sopenharmony_ci
7760e1051a39Sopenharmony_ci    testresult = 1;
7761e1051a39Sopenharmony_ci
7762e1051a39Sopenharmony_ci end:
7763e1051a39Sopenharmony_ci    SSL_free(serverssl);
7764e1051a39Sopenharmony_ci    SSL_free(clientssl);
7765e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
7766e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
7767e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(tmplibctx);
7768e1051a39Sopenharmony_ci
7769e1051a39Sopenharmony_ci    return testresult;
7770e1051a39Sopenharmony_ci}
7771e1051a39Sopenharmony_ci
7772e1051a39Sopenharmony_cistatic int test_ssl_get_shared_ciphers(int tst)
7773e1051a39Sopenharmony_ci{
7774e1051a39Sopenharmony_ci    return int_test_ssl_get_shared_ciphers(tst, 0)
7775e1051a39Sopenharmony_ci           && int_test_ssl_get_shared_ciphers(tst, 1);
7776e1051a39Sopenharmony_ci}
7777e1051a39Sopenharmony_ci
7778e1051a39Sopenharmony_ci
7779e1051a39Sopenharmony_cistatic const char *appdata = "Hello World";
7780e1051a39Sopenharmony_cistatic int gen_tick_called, dec_tick_called, tick_key_cb_called;
7781e1051a39Sopenharmony_cistatic int tick_key_renew = 0;
7782e1051a39Sopenharmony_cistatic SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7783e1051a39Sopenharmony_ci
7784e1051a39Sopenharmony_cistatic int gen_tick_cb(SSL *s, void *arg)
7785e1051a39Sopenharmony_ci{
7786e1051a39Sopenharmony_ci    gen_tick_called = 1;
7787e1051a39Sopenharmony_ci
7788e1051a39Sopenharmony_ci    return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
7789e1051a39Sopenharmony_ci                                           strlen(appdata));
7790e1051a39Sopenharmony_ci}
7791e1051a39Sopenharmony_ci
7792e1051a39Sopenharmony_cistatic SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
7793e1051a39Sopenharmony_ci                                     const unsigned char *keyname,
7794e1051a39Sopenharmony_ci                                     size_t keyname_length,
7795e1051a39Sopenharmony_ci                                     SSL_TICKET_STATUS status,
7796e1051a39Sopenharmony_ci                                     void *arg)
7797e1051a39Sopenharmony_ci{
7798e1051a39Sopenharmony_ci    void *tickdata;
7799e1051a39Sopenharmony_ci    size_t tickdlen;
7800e1051a39Sopenharmony_ci
7801e1051a39Sopenharmony_ci    dec_tick_called = 1;
7802e1051a39Sopenharmony_ci
7803e1051a39Sopenharmony_ci    if (status == SSL_TICKET_EMPTY)
7804e1051a39Sopenharmony_ci        return SSL_TICKET_RETURN_IGNORE_RENEW;
7805e1051a39Sopenharmony_ci
7806e1051a39Sopenharmony_ci    if (!TEST_true(status == SSL_TICKET_SUCCESS
7807e1051a39Sopenharmony_ci                   || status == SSL_TICKET_SUCCESS_RENEW))
7808e1051a39Sopenharmony_ci        return SSL_TICKET_RETURN_ABORT;
7809e1051a39Sopenharmony_ci
7810e1051a39Sopenharmony_ci    if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
7811e1051a39Sopenharmony_ci                                                   &tickdlen))
7812e1051a39Sopenharmony_ci            || !TEST_size_t_eq(tickdlen, strlen(appdata))
7813e1051a39Sopenharmony_ci            || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
7814e1051a39Sopenharmony_ci        return SSL_TICKET_RETURN_ABORT;
7815e1051a39Sopenharmony_ci
7816e1051a39Sopenharmony_ci    if (tick_key_cb_called)  {
7817e1051a39Sopenharmony_ci        /* Don't change what the ticket key callback wanted to do */
7818e1051a39Sopenharmony_ci        switch (status) {
7819e1051a39Sopenharmony_ci        case SSL_TICKET_NO_DECRYPT:
7820e1051a39Sopenharmony_ci            return SSL_TICKET_RETURN_IGNORE_RENEW;
7821e1051a39Sopenharmony_ci
7822e1051a39Sopenharmony_ci        case SSL_TICKET_SUCCESS:
7823e1051a39Sopenharmony_ci            return SSL_TICKET_RETURN_USE;
7824e1051a39Sopenharmony_ci
7825e1051a39Sopenharmony_ci        case SSL_TICKET_SUCCESS_RENEW:
7826e1051a39Sopenharmony_ci            return SSL_TICKET_RETURN_USE_RENEW;
7827e1051a39Sopenharmony_ci
7828e1051a39Sopenharmony_ci        default:
7829e1051a39Sopenharmony_ci            return SSL_TICKET_RETURN_ABORT;
7830e1051a39Sopenharmony_ci        }
7831e1051a39Sopenharmony_ci    }
7832e1051a39Sopenharmony_ci    return tick_dec_ret;
7833e1051a39Sopenharmony_ci
7834e1051a39Sopenharmony_ci}
7835e1051a39Sopenharmony_ci
7836e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
7837e1051a39Sopenharmony_cistatic int tick_key_cb(SSL *s, unsigned char key_name[16],
7838e1051a39Sopenharmony_ci                       unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
7839e1051a39Sopenharmony_ci                       HMAC_CTX *hctx, int enc)
7840e1051a39Sopenharmony_ci{
7841e1051a39Sopenharmony_ci    const unsigned char tick_aes_key[16] = "0123456789abcdef";
7842e1051a39Sopenharmony_ci    const unsigned char tick_hmac_key[16] = "0123456789abcdef";
7843e1051a39Sopenharmony_ci    EVP_CIPHER *aes128cbc;
7844e1051a39Sopenharmony_ci    EVP_MD *sha256;
7845e1051a39Sopenharmony_ci    int ret;
7846e1051a39Sopenharmony_ci
7847e1051a39Sopenharmony_ci    tick_key_cb_called = 1;
7848e1051a39Sopenharmony_ci
7849e1051a39Sopenharmony_ci    if (tick_key_renew == -1)
7850e1051a39Sopenharmony_ci        return 0;
7851e1051a39Sopenharmony_ci
7852e1051a39Sopenharmony_ci    aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7853e1051a39Sopenharmony_ci    if (!TEST_ptr(aes128cbc))
7854e1051a39Sopenharmony_ci        return 0;
7855e1051a39Sopenharmony_ci    sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
7856e1051a39Sopenharmony_ci    if (!TEST_ptr(sha256)) {
7857e1051a39Sopenharmony_ci        EVP_CIPHER_free(aes128cbc);
7858e1051a39Sopenharmony_ci        return 0;
7859e1051a39Sopenharmony_ci    }
7860e1051a39Sopenharmony_ci
7861e1051a39Sopenharmony_ci    memset(iv, 0, AES_BLOCK_SIZE);
7862e1051a39Sopenharmony_ci    memset(key_name, 0, 16);
7863e1051a39Sopenharmony_ci    if (aes128cbc == NULL
7864e1051a39Sopenharmony_ci            || sha256 == NULL
7865e1051a39Sopenharmony_ci            || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7866e1051a39Sopenharmony_ci            || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
7867e1051a39Sopenharmony_ci                             NULL))
7868e1051a39Sopenharmony_ci        ret = -1;
7869e1051a39Sopenharmony_ci    else
7870e1051a39Sopenharmony_ci        ret = tick_key_renew ? 2 : 1;
7871e1051a39Sopenharmony_ci
7872e1051a39Sopenharmony_ci    EVP_CIPHER_free(aes128cbc);
7873e1051a39Sopenharmony_ci    EVP_MD_free(sha256);
7874e1051a39Sopenharmony_ci
7875e1051a39Sopenharmony_ci    return ret;
7876e1051a39Sopenharmony_ci}
7877e1051a39Sopenharmony_ci#endif
7878e1051a39Sopenharmony_ci
7879e1051a39Sopenharmony_cistatic int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
7880e1051a39Sopenharmony_ci                           unsigned char iv[EVP_MAX_IV_LENGTH],
7881e1051a39Sopenharmony_ci                           EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
7882e1051a39Sopenharmony_ci{
7883e1051a39Sopenharmony_ci    const unsigned char tick_aes_key[16] = "0123456789abcdef";
7884e1051a39Sopenharmony_ci    unsigned char tick_hmac_key[16] = "0123456789abcdef";
7885e1051a39Sopenharmony_ci    OSSL_PARAM params[2];
7886e1051a39Sopenharmony_ci    EVP_CIPHER *aes128cbc;
7887e1051a39Sopenharmony_ci    int ret;
7888e1051a39Sopenharmony_ci
7889e1051a39Sopenharmony_ci    tick_key_cb_called = 1;
7890e1051a39Sopenharmony_ci
7891e1051a39Sopenharmony_ci    if (tick_key_renew == -1)
7892e1051a39Sopenharmony_ci        return 0;
7893e1051a39Sopenharmony_ci
7894e1051a39Sopenharmony_ci    aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
7895e1051a39Sopenharmony_ci    if (!TEST_ptr(aes128cbc))
7896e1051a39Sopenharmony_ci        return 0;
7897e1051a39Sopenharmony_ci
7898e1051a39Sopenharmony_ci    memset(iv, 0, AES_BLOCK_SIZE);
7899e1051a39Sopenharmony_ci    memset(key_name, 0, 16);
7900e1051a39Sopenharmony_ci    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
7901e1051a39Sopenharmony_ci                                                 "SHA256", 0);
7902e1051a39Sopenharmony_ci    params[1] = OSSL_PARAM_construct_end();
7903e1051a39Sopenharmony_ci    if (aes128cbc == NULL
7904e1051a39Sopenharmony_ci            || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
7905e1051a39Sopenharmony_ci            || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
7906e1051a39Sopenharmony_ci                             params))
7907e1051a39Sopenharmony_ci        ret = -1;
7908e1051a39Sopenharmony_ci    else
7909e1051a39Sopenharmony_ci        ret = tick_key_renew ? 2 : 1;
7910e1051a39Sopenharmony_ci
7911e1051a39Sopenharmony_ci    EVP_CIPHER_free(aes128cbc);
7912e1051a39Sopenharmony_ci
7913e1051a39Sopenharmony_ci    return ret;
7914e1051a39Sopenharmony_ci}
7915e1051a39Sopenharmony_ci
7916e1051a39Sopenharmony_ci/*
7917e1051a39Sopenharmony_ci * Test the various ticket callbacks
7918e1051a39Sopenharmony_ci * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
7919e1051a39Sopenharmony_ci * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
7920e1051a39Sopenharmony_ci * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
7921e1051a39Sopenharmony_ci * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
7922e1051a39Sopenharmony_ci * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
7923e1051a39Sopenharmony_ci * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
7924e1051a39Sopenharmony_ci * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
7925e1051a39Sopenharmony_ci * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
7926e1051a39Sopenharmony_ci * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
7927e1051a39Sopenharmony_ci * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
7928e1051a39Sopenharmony_ci * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
7929e1051a39Sopenharmony_ci * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
7930e1051a39Sopenharmony_ci * Test 12: TLSv1.2, old ticket key callback, no ticket
7931e1051a39Sopenharmony_ci * Test 13: TLSv1.3, old ticket key callback, no ticket
7932e1051a39Sopenharmony_ci * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
7933e1051a39Sopenharmony_ci * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
7934e1051a39Sopenharmony_ci * Test 16: TLSv1.2, ticket key callback, ticket, renewal
7935e1051a39Sopenharmony_ci * Test 17: TLSv1.3, ticket key callback, ticket, renewal
7936e1051a39Sopenharmony_ci * Test 18: TLSv1.2, ticket key callback, no ticket
7937e1051a39Sopenharmony_ci * Test 19: TLSv1.3, ticket key callback, no ticket
7938e1051a39Sopenharmony_ci */
7939e1051a39Sopenharmony_cistatic int test_ticket_callbacks(int tst)
7940e1051a39Sopenharmony_ci{
7941e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
7942e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
7943e1051a39Sopenharmony_ci    SSL_SESSION *clntsess = NULL;
7944e1051a39Sopenharmony_ci    int testresult = 0;
7945e1051a39Sopenharmony_ci
7946e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
7947e1051a39Sopenharmony_ci    if (tst % 2 == 0)
7948e1051a39Sopenharmony_ci        return 1;
7949e1051a39Sopenharmony_ci#endif
7950e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
7951e1051a39Sopenharmony_ci    if (tst % 2 == 1)
7952e1051a39Sopenharmony_ci        return 1;
7953e1051a39Sopenharmony_ci#endif
7954e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_DEPRECATED_3_0
7955e1051a39Sopenharmony_ci    if (tst >= 8 && tst <= 13)
7956e1051a39Sopenharmony_ci        return 1;
7957e1051a39Sopenharmony_ci#endif
7958e1051a39Sopenharmony_ci
7959e1051a39Sopenharmony_ci    gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
7960e1051a39Sopenharmony_ci
7961e1051a39Sopenharmony_ci    /* Which tests the ticket key callback should request renewal for */
7962e1051a39Sopenharmony_ci
7963e1051a39Sopenharmony_ci    if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
7964e1051a39Sopenharmony_ci        tick_key_renew = 1;
7965e1051a39Sopenharmony_ci    else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
7966e1051a39Sopenharmony_ci        tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
7967e1051a39Sopenharmony_ci    else
7968e1051a39Sopenharmony_ci        tick_key_renew = 0;
7969e1051a39Sopenharmony_ci
7970e1051a39Sopenharmony_ci    /* Which tests the decrypt ticket callback should request renewal for */
7971e1051a39Sopenharmony_ci    switch (tst) {
7972e1051a39Sopenharmony_ci    case 0:
7973e1051a39Sopenharmony_ci    case 1:
7974e1051a39Sopenharmony_ci        tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
7975e1051a39Sopenharmony_ci        break;
7976e1051a39Sopenharmony_ci
7977e1051a39Sopenharmony_ci    case 2:
7978e1051a39Sopenharmony_ci    case 3:
7979e1051a39Sopenharmony_ci        tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
7980e1051a39Sopenharmony_ci        break;
7981e1051a39Sopenharmony_ci
7982e1051a39Sopenharmony_ci    case 4:
7983e1051a39Sopenharmony_ci    case 5:
7984e1051a39Sopenharmony_ci        tick_dec_ret = SSL_TICKET_RETURN_USE;
7985e1051a39Sopenharmony_ci        break;
7986e1051a39Sopenharmony_ci
7987e1051a39Sopenharmony_ci    case 6:
7988e1051a39Sopenharmony_ci    case 7:
7989e1051a39Sopenharmony_ci        tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
7990e1051a39Sopenharmony_ci        break;
7991e1051a39Sopenharmony_ci
7992e1051a39Sopenharmony_ci    default:
7993e1051a39Sopenharmony_ci        tick_dec_ret = SSL_TICKET_RETURN_ABORT;
7994e1051a39Sopenharmony_ci    }
7995e1051a39Sopenharmony_ci
7996e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
7997e1051a39Sopenharmony_ci                                       TLS_client_method(),
7998e1051a39Sopenharmony_ci                                       TLS1_VERSION,
7999e1051a39Sopenharmony_ci                                       ((tst % 2) == 0) ? TLS1_2_VERSION
8000e1051a39Sopenharmony_ci                                                        : TLS1_3_VERSION,
8001e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
8002e1051a39Sopenharmony_ci        goto end;
8003e1051a39Sopenharmony_ci
8004e1051a39Sopenharmony_ci    /*
8005e1051a39Sopenharmony_ci     * We only want sessions to resume from tickets - not the session cache. So
8006e1051a39Sopenharmony_ci     * switch the cache off.
8007e1051a39Sopenharmony_ci     */
8008e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
8009e1051a39Sopenharmony_ci        goto end;
8010e1051a39Sopenharmony_ci
8011e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
8012e1051a39Sopenharmony_ci                                                 NULL)))
8013e1051a39Sopenharmony_ci        goto end;
8014e1051a39Sopenharmony_ci
8015e1051a39Sopenharmony_ci    if (tst >= 14) {
8016e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
8017e1051a39Sopenharmony_ci            goto end;
8018e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
8019e1051a39Sopenharmony_ci    } else if (tst >= 8) {
8020e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
8021e1051a39Sopenharmony_ci            goto end;
8022e1051a39Sopenharmony_ci#endif
8023e1051a39Sopenharmony_ci    }
8024e1051a39Sopenharmony_ci
8025e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8026e1051a39Sopenharmony_ci                                             NULL, NULL))
8027e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
8028e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
8029e1051a39Sopenharmony_ci        goto end;
8030e1051a39Sopenharmony_ci
8031e1051a39Sopenharmony_ci    /*
8032e1051a39Sopenharmony_ci     * The decrypt ticket key callback in TLSv1.2 should be called even though
8033e1051a39Sopenharmony_ci     * we have no ticket yet, because it gets called with a status of
8034e1051a39Sopenharmony_ci     * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
8035e1051a39Sopenharmony_ci     * actually send any ticket data). This does not happen in TLSv1.3 because
8036e1051a39Sopenharmony_ci     * it is not valid to send empty ticket data in TLSv1.3.
8037e1051a39Sopenharmony_ci     */
8038e1051a39Sopenharmony_ci    if (!TEST_int_eq(gen_tick_called, 1)
8039e1051a39Sopenharmony_ci            || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
8040e1051a39Sopenharmony_ci        goto end;
8041e1051a39Sopenharmony_ci
8042e1051a39Sopenharmony_ci    gen_tick_called = dec_tick_called = 0;
8043e1051a39Sopenharmony_ci
8044e1051a39Sopenharmony_ci    clntsess = SSL_get1_session(clientssl);
8045e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
8046e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
8047e1051a39Sopenharmony_ci    SSL_free(serverssl);
8048e1051a39Sopenharmony_ci    SSL_free(clientssl);
8049e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
8050e1051a39Sopenharmony_ci
8051e1051a39Sopenharmony_ci    /* Now do a resumption */
8052e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
8053e1051a39Sopenharmony_ci                                      NULL))
8054e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, clntsess))
8055e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
8056e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
8057e1051a39Sopenharmony_ci        goto end;
8058e1051a39Sopenharmony_ci
8059e1051a39Sopenharmony_ci    if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
8060e1051a39Sopenharmony_ci            || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8061e1051a39Sopenharmony_ci            || tick_key_renew == -1) {
8062e1051a39Sopenharmony_ci        if (!TEST_false(SSL_session_reused(clientssl)))
8063e1051a39Sopenharmony_ci            goto end;
8064e1051a39Sopenharmony_ci    } else {
8065e1051a39Sopenharmony_ci        if (!TEST_true(SSL_session_reused(clientssl)))
8066e1051a39Sopenharmony_ci            goto end;
8067e1051a39Sopenharmony_ci    }
8068e1051a39Sopenharmony_ci
8069e1051a39Sopenharmony_ci    if (!TEST_int_eq(gen_tick_called,
8070e1051a39Sopenharmony_ci                     (tick_key_renew
8071e1051a39Sopenharmony_ci                      || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
8072e1051a39Sopenharmony_ci                      || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
8073e1051a39Sopenharmony_ci                     ? 1 : 0)
8074e1051a39Sopenharmony_ci               /* There is no ticket to decrypt in tests 13 and 19 */
8075e1051a39Sopenharmony_ci            || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
8076e1051a39Sopenharmony_ci        goto end;
8077e1051a39Sopenharmony_ci
8078e1051a39Sopenharmony_ci    testresult = 1;
8079e1051a39Sopenharmony_ci
8080e1051a39Sopenharmony_ci end:
8081e1051a39Sopenharmony_ci    SSL_SESSION_free(clntsess);
8082e1051a39Sopenharmony_ci    SSL_free(serverssl);
8083e1051a39Sopenharmony_ci    SSL_free(clientssl);
8084e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8085e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8086e1051a39Sopenharmony_ci
8087e1051a39Sopenharmony_ci    return testresult;
8088e1051a39Sopenharmony_ci}
8089e1051a39Sopenharmony_ci
8090e1051a39Sopenharmony_ci/*
8091e1051a39Sopenharmony_ci * Test incorrect shutdown.
8092e1051a39Sopenharmony_ci * Test 0: client does not shutdown properly,
8093e1051a39Sopenharmony_ci *         server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
8094e1051a39Sopenharmony_ci *         server should get SSL_ERROR_SSL
8095e1051a39Sopenharmony_ci * Test 1: client does not shutdown properly,
8096e1051a39Sopenharmony_ci *         server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
8097e1051a39Sopenharmony_ci *         server should get SSL_ERROR_ZERO_RETURN
8098e1051a39Sopenharmony_ci */
8099e1051a39Sopenharmony_cistatic int test_incorrect_shutdown(int tst)
8100e1051a39Sopenharmony_ci{
8101e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
8102e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8103e1051a39Sopenharmony_ci    int testresult = 0;
8104e1051a39Sopenharmony_ci    char buf[80];
8105e1051a39Sopenharmony_ci    BIO *c2s;
8106e1051a39Sopenharmony_ci
8107e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8108e1051a39Sopenharmony_ci                                       TLS_client_method(), 0, 0,
8109e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
8110e1051a39Sopenharmony_ci        goto end;
8111e1051a39Sopenharmony_ci
8112e1051a39Sopenharmony_ci    if (tst == 1)
8113e1051a39Sopenharmony_ci        SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
8114e1051a39Sopenharmony_ci
8115e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8116e1051a39Sopenharmony_ci                                            NULL, NULL)))
8117e1051a39Sopenharmony_ci        goto end;
8118e1051a39Sopenharmony_ci
8119e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8120e1051a39Sopenharmony_ci                                              SSL_ERROR_NONE)))
8121e1051a39Sopenharmony_ci        goto end;
8122e1051a39Sopenharmony_ci
8123e1051a39Sopenharmony_ci    c2s = SSL_get_rbio(serverssl);
8124e1051a39Sopenharmony_ci    BIO_set_mem_eof_return(c2s, 0);
8125e1051a39Sopenharmony_ci
8126e1051a39Sopenharmony_ci    if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
8127e1051a39Sopenharmony_ci        goto end;
8128e1051a39Sopenharmony_ci
8129e1051a39Sopenharmony_ci    if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
8130e1051a39Sopenharmony_ci        goto end;
8131e1051a39Sopenharmony_ci    if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
8132e1051a39Sopenharmony_ci        goto end;
8133e1051a39Sopenharmony_ci
8134e1051a39Sopenharmony_ci    testresult = 1;
8135e1051a39Sopenharmony_ci
8136e1051a39Sopenharmony_ci end:
8137e1051a39Sopenharmony_ci    SSL_free(serverssl);
8138e1051a39Sopenharmony_ci    SSL_free(clientssl);
8139e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8140e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8141e1051a39Sopenharmony_ci
8142e1051a39Sopenharmony_ci    return testresult;
8143e1051a39Sopenharmony_ci}
8144e1051a39Sopenharmony_ci
8145e1051a39Sopenharmony_ci/*
8146e1051a39Sopenharmony_ci * Test bi-directional shutdown.
8147e1051a39Sopenharmony_ci * Test 0: TLSv1.2
8148e1051a39Sopenharmony_ci * Test 1: TLSv1.2, server continues to read/write after client shutdown
8149e1051a39Sopenharmony_ci * Test 2: TLSv1.3, no pending NewSessionTicket messages
8150e1051a39Sopenharmony_ci * Test 3: TLSv1.3, pending NewSessionTicket messages
8151e1051a39Sopenharmony_ci * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
8152e1051a39Sopenharmony_ci *                  sends key update, client reads it
8153e1051a39Sopenharmony_ci * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
8154e1051a39Sopenharmony_ci *                  sends CertificateRequest, client reads and ignores it
8155e1051a39Sopenharmony_ci * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
8156e1051a39Sopenharmony_ci *                  doesn't read it
8157e1051a39Sopenharmony_ci */
8158e1051a39Sopenharmony_cistatic int test_shutdown(int tst)
8159e1051a39Sopenharmony_ci{
8160e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
8161e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8162e1051a39Sopenharmony_ci    int testresult = 0;
8163e1051a39Sopenharmony_ci    char msg[] = "A test message";
8164e1051a39Sopenharmony_ci    char buf[80];
8165e1051a39Sopenharmony_ci    size_t written, readbytes;
8166e1051a39Sopenharmony_ci    SSL_SESSION *sess;
8167e1051a39Sopenharmony_ci
8168e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
8169e1051a39Sopenharmony_ci    if (tst <= 1)
8170e1051a39Sopenharmony_ci        return 1;
8171e1051a39Sopenharmony_ci#endif
8172e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
8173e1051a39Sopenharmony_ci    if (tst >= 2)
8174e1051a39Sopenharmony_ci        return 1;
8175e1051a39Sopenharmony_ci#endif
8176e1051a39Sopenharmony_ci
8177e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8178e1051a39Sopenharmony_ci                                       TLS_client_method(),
8179e1051a39Sopenharmony_ci                                       TLS1_VERSION,
8180e1051a39Sopenharmony_ci                                       (tst <= 1) ? TLS1_2_VERSION
8181e1051a39Sopenharmony_ci                                                  : TLS1_3_VERSION,
8182e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
8183e1051a39Sopenharmony_ci        goto end;
8184e1051a39Sopenharmony_ci
8185e1051a39Sopenharmony_ci    if (tst == 5)
8186e1051a39Sopenharmony_ci        SSL_CTX_set_post_handshake_auth(cctx, 1);
8187e1051a39Sopenharmony_ci
8188e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8189e1051a39Sopenharmony_ci                                             NULL, NULL)))
8190e1051a39Sopenharmony_ci        goto end;
8191e1051a39Sopenharmony_ci
8192e1051a39Sopenharmony_ci    if (tst == 3) {
8193e1051a39Sopenharmony_ci        if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
8194e1051a39Sopenharmony_ci                                                  SSL_ERROR_NONE, 1))
8195e1051a39Sopenharmony_ci                || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8196e1051a39Sopenharmony_ci                || !TEST_false(SSL_SESSION_is_resumable(sess)))
8197e1051a39Sopenharmony_ci            goto end;
8198e1051a39Sopenharmony_ci    } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
8199e1051a39Sopenharmony_ci                                              SSL_ERROR_NONE))
8200e1051a39Sopenharmony_ci            || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8201e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_is_resumable(sess))) {
8202e1051a39Sopenharmony_ci        goto end;
8203e1051a39Sopenharmony_ci    }
8204e1051a39Sopenharmony_ci
8205e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
8206e1051a39Sopenharmony_ci        goto end;
8207e1051a39Sopenharmony_ci
8208e1051a39Sopenharmony_ci    if (tst >= 4) {
8209e1051a39Sopenharmony_ci        /*
8210e1051a39Sopenharmony_ci         * Reading on the server after the client has sent close_notify should
8211e1051a39Sopenharmony_ci         * fail and provide SSL_ERROR_ZERO_RETURN
8212e1051a39Sopenharmony_ci         */
8213e1051a39Sopenharmony_ci        if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
8214e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_error(serverssl, 0),
8215e1051a39Sopenharmony_ci                                SSL_ERROR_ZERO_RETURN)
8216e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_shutdown(serverssl),
8217e1051a39Sopenharmony_ci                                SSL_RECEIVED_SHUTDOWN)
8218e1051a39Sopenharmony_ci                   /*
8219e1051a39Sopenharmony_ci                    * Even though we're shutdown on receive we should still be
8220e1051a39Sopenharmony_ci                    * able to write.
8221e1051a39Sopenharmony_ci                    */
8222e1051a39Sopenharmony_ci                || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8223e1051a39Sopenharmony_ci            goto end;
8224e1051a39Sopenharmony_ci        if (tst == 4
8225e1051a39Sopenharmony_ci                && !TEST_true(SSL_key_update(serverssl,
8226e1051a39Sopenharmony_ci                                             SSL_KEY_UPDATE_REQUESTED)))
8227e1051a39Sopenharmony_ci            goto end;
8228e1051a39Sopenharmony_ci        if (tst == 5) {
8229e1051a39Sopenharmony_ci            SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
8230e1051a39Sopenharmony_ci            if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
8231e1051a39Sopenharmony_ci                goto end;
8232e1051a39Sopenharmony_ci        }
8233e1051a39Sopenharmony_ci        if ((tst == 4 || tst == 5)
8234e1051a39Sopenharmony_ci                && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
8235e1051a39Sopenharmony_ci            goto end;
8236e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
8237e1051a39Sopenharmony_ci            goto end;
8238e1051a39Sopenharmony_ci        if (tst == 4 || tst == 5) {
8239e1051a39Sopenharmony_ci            /* Should still be able to read data from server */
8240e1051a39Sopenharmony_ci            if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8241e1051a39Sopenharmony_ci                                       &readbytes))
8242e1051a39Sopenharmony_ci                    || !TEST_size_t_eq(readbytes, sizeof(msg))
8243e1051a39Sopenharmony_ci                    || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
8244e1051a39Sopenharmony_ci                    || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
8245e1051a39Sopenharmony_ci                                              &readbytes))
8246e1051a39Sopenharmony_ci                    || !TEST_size_t_eq(readbytes, sizeof(msg))
8247e1051a39Sopenharmony_ci                    || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
8248e1051a39Sopenharmony_ci                goto end;
8249e1051a39Sopenharmony_ci        }
8250e1051a39Sopenharmony_ci    }
8251e1051a39Sopenharmony_ci
8252e1051a39Sopenharmony_ci    /* Writing on the client after sending close_notify shouldn't be possible */
8253e1051a39Sopenharmony_ci    if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
8254e1051a39Sopenharmony_ci        goto end;
8255e1051a39Sopenharmony_ci
8256e1051a39Sopenharmony_ci    if (tst < 4) {
8257e1051a39Sopenharmony_ci        /*
8258e1051a39Sopenharmony_ci         * For these tests the client has sent close_notify but it has not yet
8259e1051a39Sopenharmony_ci         * been received by the server. The server has not sent close_notify
8260e1051a39Sopenharmony_ci         * yet.
8261e1051a39Sopenharmony_ci         */
8262e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
8263e1051a39Sopenharmony_ci                   /*
8264e1051a39Sopenharmony_ci                    * Writing on the server after sending close_notify shouldn't
8265e1051a39Sopenharmony_ci                    * be possible.
8266e1051a39Sopenharmony_ci                    */
8267e1051a39Sopenharmony_ci                || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8268e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_shutdown(clientssl), 1)
8269e1051a39Sopenharmony_ci                || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8270e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_is_resumable(sess))
8271e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_shutdown(serverssl), 1))
8272e1051a39Sopenharmony_ci            goto end;
8273e1051a39Sopenharmony_ci    } else if (tst == 4 || tst == 5) {
8274e1051a39Sopenharmony_ci        /*
8275e1051a39Sopenharmony_ci         * In this test the client has sent close_notify and it has been
8276e1051a39Sopenharmony_ci         * received by the server which has responded with a close_notify. The
8277e1051a39Sopenharmony_ci         * client needs to read the close_notify sent by the server.
8278e1051a39Sopenharmony_ci         */
8279e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
8280e1051a39Sopenharmony_ci                || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
8281e1051a39Sopenharmony_ci                || !TEST_true(SSL_SESSION_is_resumable(sess)))
8282e1051a39Sopenharmony_ci            goto end;
8283e1051a39Sopenharmony_ci    } else {
8284e1051a39Sopenharmony_ci        /*
8285e1051a39Sopenharmony_ci         * tst == 6
8286e1051a39Sopenharmony_ci         *
8287e1051a39Sopenharmony_ci         * The client has sent close_notify and is expecting a close_notify
8288e1051a39Sopenharmony_ci         * back, but instead there is application data first. The shutdown
8289e1051a39Sopenharmony_ci         * should fail with a fatal error.
8290e1051a39Sopenharmony_ci         */
8291e1051a39Sopenharmony_ci        if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
8292e1051a39Sopenharmony_ci                || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
8293e1051a39Sopenharmony_ci            goto end;
8294e1051a39Sopenharmony_ci    }
8295e1051a39Sopenharmony_ci
8296e1051a39Sopenharmony_ci    testresult = 1;
8297e1051a39Sopenharmony_ci
8298e1051a39Sopenharmony_ci end:
8299e1051a39Sopenharmony_ci    SSL_free(serverssl);
8300e1051a39Sopenharmony_ci    SSL_free(clientssl);
8301e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8302e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8303e1051a39Sopenharmony_ci
8304e1051a39Sopenharmony_ci    return testresult;
8305e1051a39Sopenharmony_ci}
8306e1051a39Sopenharmony_ci
8307e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8308e1051a39Sopenharmony_cistatic int cert_cb_cnt;
8309e1051a39Sopenharmony_ci
8310e1051a39Sopenharmony_cistatic int cert_cb(SSL *s, void *arg)
8311e1051a39Sopenharmony_ci{
8312e1051a39Sopenharmony_ci    SSL_CTX *ctx = (SSL_CTX *)arg;
8313e1051a39Sopenharmony_ci    BIO *in = NULL;
8314e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
8315e1051a39Sopenharmony_ci    X509 *x509 = NULL, *rootx = NULL;
8316e1051a39Sopenharmony_ci    STACK_OF(X509) *chain = NULL;
8317e1051a39Sopenharmony_ci    char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
8318e1051a39Sopenharmony_ci    int ret = 0;
8319e1051a39Sopenharmony_ci
8320e1051a39Sopenharmony_ci    if (cert_cb_cnt == 0) {
8321e1051a39Sopenharmony_ci        /* Suspend the handshake */
8322e1051a39Sopenharmony_ci        cert_cb_cnt++;
8323e1051a39Sopenharmony_ci        return -1;
8324e1051a39Sopenharmony_ci    } else if (cert_cb_cnt == 1) {
8325e1051a39Sopenharmony_ci        /*
8326e1051a39Sopenharmony_ci         * Update the SSL_CTX, set the certificate and private key and then
8327e1051a39Sopenharmony_ci         * continue the handshake normally.
8328e1051a39Sopenharmony_ci         */
8329e1051a39Sopenharmony_ci        if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
8330e1051a39Sopenharmony_ci            return 0;
8331e1051a39Sopenharmony_ci
8332e1051a39Sopenharmony_ci        if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
8333e1051a39Sopenharmony_ci                || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
8334e1051a39Sopenharmony_ci                                                      SSL_FILETYPE_PEM))
8335e1051a39Sopenharmony_ci                || !TEST_true(SSL_check_private_key(s)))
8336e1051a39Sopenharmony_ci            return 0;
8337e1051a39Sopenharmony_ci        cert_cb_cnt++;
8338e1051a39Sopenharmony_ci        return 1;
8339e1051a39Sopenharmony_ci    } else if (cert_cb_cnt == 3) {
8340e1051a39Sopenharmony_ci        int rv;
8341e1051a39Sopenharmony_ci
8342e1051a39Sopenharmony_ci        rootfile = test_mk_file_path(certsdir, "rootcert.pem");
8343e1051a39Sopenharmony_ci        ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
8344e1051a39Sopenharmony_ci        ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
8345e1051a39Sopenharmony_ci        if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
8346e1051a39Sopenharmony_ci            goto out;
8347e1051a39Sopenharmony_ci        chain = sk_X509_new_null();
8348e1051a39Sopenharmony_ci        if (!TEST_ptr(chain))
8349e1051a39Sopenharmony_ci            goto out;
8350e1051a39Sopenharmony_ci        if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8351e1051a39Sopenharmony_ci                || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
8352e1051a39Sopenharmony_ci                || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
8353e1051a39Sopenharmony_ci                || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
8354e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_push(chain, rootx)))
8355e1051a39Sopenharmony_ci            goto out;
8356e1051a39Sopenharmony_ci        rootx = NULL;
8357e1051a39Sopenharmony_ci        BIO_free(in);
8358e1051a39Sopenharmony_ci        if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8359e1051a39Sopenharmony_ci                || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
8360e1051a39Sopenharmony_ci                || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
8361e1051a39Sopenharmony_ci                || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
8362e1051a39Sopenharmony_ci            goto out;
8363e1051a39Sopenharmony_ci        BIO_free(in);
8364e1051a39Sopenharmony_ci        if (!TEST_ptr(in = BIO_new(BIO_s_file()))
8365e1051a39Sopenharmony_ci                || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
8366e1051a39Sopenharmony_ci                || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
8367e1051a39Sopenharmony_ci                                                               NULL, NULL,
8368e1051a39Sopenharmony_ci                                                               libctx, NULL)))
8369e1051a39Sopenharmony_ci            goto out;
8370e1051a39Sopenharmony_ci        rv = SSL_check_chain(s, x509, pkey, chain);
8371e1051a39Sopenharmony_ci        /*
8372e1051a39Sopenharmony_ci         * If the cert doesn't show as valid here (e.g., because we don't
8373e1051a39Sopenharmony_ci         * have any shared sigalgs), then we will not set it, and there will
8374e1051a39Sopenharmony_ci         * be no certificate at all on the SSL or SSL_CTX.  This, in turn,
8375e1051a39Sopenharmony_ci         * will cause tls_choose_sigalgs() to fail the connection.
8376e1051a39Sopenharmony_ci         */
8377e1051a39Sopenharmony_ci        if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
8378e1051a39Sopenharmony_ci                == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
8379e1051a39Sopenharmony_ci            if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
8380e1051a39Sopenharmony_ci                goto out;
8381e1051a39Sopenharmony_ci        }
8382e1051a39Sopenharmony_ci
8383e1051a39Sopenharmony_ci        ret = 1;
8384e1051a39Sopenharmony_ci    }
8385e1051a39Sopenharmony_ci
8386e1051a39Sopenharmony_ci    /* Abort the handshake */
8387e1051a39Sopenharmony_ci out:
8388e1051a39Sopenharmony_ci    OPENSSL_free(ecdsacert);
8389e1051a39Sopenharmony_ci    OPENSSL_free(ecdsakey);
8390e1051a39Sopenharmony_ci    OPENSSL_free(rootfile);
8391e1051a39Sopenharmony_ci    BIO_free(in);
8392e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
8393e1051a39Sopenharmony_ci    X509_free(x509);
8394e1051a39Sopenharmony_ci    X509_free(rootx);
8395e1051a39Sopenharmony_ci    sk_X509_pop_free(chain, X509_free);
8396e1051a39Sopenharmony_ci    return ret;
8397e1051a39Sopenharmony_ci}
8398e1051a39Sopenharmony_ci
8399e1051a39Sopenharmony_ci/*
8400e1051a39Sopenharmony_ci * Test the certificate callback.
8401e1051a39Sopenharmony_ci * Test 0: Callback fails
8402e1051a39Sopenharmony_ci * Test 1: Success - no SSL_set_SSL_CTX() in the callback
8403e1051a39Sopenharmony_ci * Test 2: Success - SSL_set_SSL_CTX() in the callback
8404e1051a39Sopenharmony_ci * Test 3: Success - Call SSL_check_chain from the callback
8405e1051a39Sopenharmony_ci * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
8406e1051a39Sopenharmony_ci *                   chain
8407e1051a39Sopenharmony_ci * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
8408e1051a39Sopenharmony_ci */
8409e1051a39Sopenharmony_cistatic int test_cert_cb_int(int prot, int tst)
8410e1051a39Sopenharmony_ci{
8411e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
8412e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8413e1051a39Sopenharmony_ci    int testresult = 0, ret;
8414e1051a39Sopenharmony_ci
8415e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_EC
8416e1051a39Sopenharmony_ci    /* We use an EC cert in these tests, so we skip in a no-ec build */
8417e1051a39Sopenharmony_ci    if (tst >= 3)
8418e1051a39Sopenharmony_ci        return 1;
8419e1051a39Sopenharmony_ci#endif
8420e1051a39Sopenharmony_ci
8421e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8422e1051a39Sopenharmony_ci                                       TLS_client_method(),
8423e1051a39Sopenharmony_ci                                       TLS1_VERSION,
8424e1051a39Sopenharmony_ci                                       prot,
8425e1051a39Sopenharmony_ci                                       &sctx, &cctx, NULL, NULL)))
8426e1051a39Sopenharmony_ci        goto end;
8427e1051a39Sopenharmony_ci
8428e1051a39Sopenharmony_ci    if (tst == 0)
8429e1051a39Sopenharmony_ci        cert_cb_cnt = -1;
8430e1051a39Sopenharmony_ci    else if (tst >= 3)
8431e1051a39Sopenharmony_ci        cert_cb_cnt = 3;
8432e1051a39Sopenharmony_ci    else
8433e1051a39Sopenharmony_ci        cert_cb_cnt = 0;
8434e1051a39Sopenharmony_ci
8435e1051a39Sopenharmony_ci    if (tst == 2) {
8436e1051a39Sopenharmony_ci        snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
8437e1051a39Sopenharmony_ci        if (!TEST_ptr(snictx))
8438e1051a39Sopenharmony_ci            goto end;
8439e1051a39Sopenharmony_ci    }
8440e1051a39Sopenharmony_ci
8441e1051a39Sopenharmony_ci    SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
8442e1051a39Sopenharmony_ci
8443e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8444e1051a39Sopenharmony_ci                                      NULL, NULL)))
8445e1051a39Sopenharmony_ci        goto end;
8446e1051a39Sopenharmony_ci
8447e1051a39Sopenharmony_ci    if (tst == 4) {
8448e1051a39Sopenharmony_ci        /*
8449e1051a39Sopenharmony_ci         * We cause SSL_check_chain() to fail by specifying sig_algs that
8450e1051a39Sopenharmony_ci         * the chain doesn't meet (the root uses an RSA cert)
8451e1051a39Sopenharmony_ci         */
8452e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8453e1051a39Sopenharmony_ci                                             "ecdsa_secp256r1_sha256")))
8454e1051a39Sopenharmony_ci            goto end;
8455e1051a39Sopenharmony_ci    } else if (tst == 5) {
8456e1051a39Sopenharmony_ci        /*
8457e1051a39Sopenharmony_ci         * We cause SSL_check_chain() to fail by specifying sig_algs that
8458e1051a39Sopenharmony_ci         * the ee cert doesn't meet (the ee uses an ECDSA cert)
8459e1051a39Sopenharmony_ci         */
8460e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
8461e1051a39Sopenharmony_ci                           "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
8462e1051a39Sopenharmony_ci            goto end;
8463e1051a39Sopenharmony_ci    }
8464e1051a39Sopenharmony_ci
8465e1051a39Sopenharmony_ci    ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
8466e1051a39Sopenharmony_ci    if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
8467e1051a39Sopenharmony_ci            || (tst > 0
8468e1051a39Sopenharmony_ci                && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
8469e1051a39Sopenharmony_ci        goto end;
8470e1051a39Sopenharmony_ci    }
8471e1051a39Sopenharmony_ci
8472e1051a39Sopenharmony_ci    testresult = 1;
8473e1051a39Sopenharmony_ci
8474e1051a39Sopenharmony_ci end:
8475e1051a39Sopenharmony_ci    SSL_free(serverssl);
8476e1051a39Sopenharmony_ci    SSL_free(clientssl);
8477e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8478e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8479e1051a39Sopenharmony_ci    SSL_CTX_free(snictx);
8480e1051a39Sopenharmony_ci
8481e1051a39Sopenharmony_ci    return testresult;
8482e1051a39Sopenharmony_ci}
8483e1051a39Sopenharmony_ci#endif
8484e1051a39Sopenharmony_ci
8485e1051a39Sopenharmony_cistatic int test_cert_cb(int tst)
8486e1051a39Sopenharmony_ci{
8487e1051a39Sopenharmony_ci    int testresult = 1;
8488e1051a39Sopenharmony_ci
8489e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
8490e1051a39Sopenharmony_ci    testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
8491e1051a39Sopenharmony_ci#endif
8492e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
8493e1051a39Sopenharmony_ci    testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
8494e1051a39Sopenharmony_ci#endif
8495e1051a39Sopenharmony_ci
8496e1051a39Sopenharmony_ci    return testresult;
8497e1051a39Sopenharmony_ci}
8498e1051a39Sopenharmony_ci
8499e1051a39Sopenharmony_cistatic int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
8500e1051a39Sopenharmony_ci{
8501e1051a39Sopenharmony_ci    X509 *xcert;
8502e1051a39Sopenharmony_ci    EVP_PKEY *privpkey;
8503e1051a39Sopenharmony_ci    BIO *in = NULL;
8504e1051a39Sopenharmony_ci    BIO *priv_in = NULL;
8505e1051a39Sopenharmony_ci
8506e1051a39Sopenharmony_ci    /* Check that SSL_get0_peer_certificate() returns something sensible */
8507e1051a39Sopenharmony_ci    if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
8508e1051a39Sopenharmony_ci        return 0;
8509e1051a39Sopenharmony_ci
8510e1051a39Sopenharmony_ci    in = BIO_new_file(cert, "r");
8511e1051a39Sopenharmony_ci    if (!TEST_ptr(in))
8512e1051a39Sopenharmony_ci        return 0;
8513e1051a39Sopenharmony_ci
8514e1051a39Sopenharmony_ci    if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
8515e1051a39Sopenharmony_ci            || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
8516e1051a39Sopenharmony_ci            || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
8517e1051a39Sopenharmony_ci            || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
8518e1051a39Sopenharmony_ci                                                               NULL, NULL,
8519e1051a39Sopenharmony_ci                                                               libctx, NULL)))
8520e1051a39Sopenharmony_ci        goto err;
8521e1051a39Sopenharmony_ci
8522e1051a39Sopenharmony_ci    *x509 = xcert;
8523e1051a39Sopenharmony_ci    *pkey = privpkey;
8524e1051a39Sopenharmony_ci
8525e1051a39Sopenharmony_ci    BIO_free(in);
8526e1051a39Sopenharmony_ci    BIO_free(priv_in);
8527e1051a39Sopenharmony_ci    return 1;
8528e1051a39Sopenharmony_cierr:
8529e1051a39Sopenharmony_ci    X509_free(xcert);
8530e1051a39Sopenharmony_ci    BIO_free(in);
8531e1051a39Sopenharmony_ci    BIO_free(priv_in);
8532e1051a39Sopenharmony_ci    return 0;
8533e1051a39Sopenharmony_ci}
8534e1051a39Sopenharmony_ci
8535e1051a39Sopenharmony_cistatic int test_client_cert_cb(int tst)
8536e1051a39Sopenharmony_ci{
8537e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
8538e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8539e1051a39Sopenharmony_ci    int testresult = 0;
8540e1051a39Sopenharmony_ci
8541e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
8542e1051a39Sopenharmony_ci    if (tst == 0)
8543e1051a39Sopenharmony_ci        return 1;
8544e1051a39Sopenharmony_ci#endif
8545e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
8546e1051a39Sopenharmony_ci    if (tst == 1)
8547e1051a39Sopenharmony_ci        return 1;
8548e1051a39Sopenharmony_ci#endif
8549e1051a39Sopenharmony_ci
8550e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8551e1051a39Sopenharmony_ci                                       TLS_client_method(),
8552e1051a39Sopenharmony_ci                                       TLS1_VERSION,
8553e1051a39Sopenharmony_ci                                       tst == 0 ? TLS1_2_VERSION
8554e1051a39Sopenharmony_ci                                                : TLS1_3_VERSION,
8555e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
8556e1051a39Sopenharmony_ci        goto end;
8557e1051a39Sopenharmony_ci
8558e1051a39Sopenharmony_ci    /*
8559e1051a39Sopenharmony_ci     * Test that setting a client_cert_cb results in a client certificate being
8560e1051a39Sopenharmony_ci     * sent.
8561e1051a39Sopenharmony_ci     */
8562e1051a39Sopenharmony_ci    SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
8563e1051a39Sopenharmony_ci    SSL_CTX_set_verify(sctx,
8564e1051a39Sopenharmony_ci                       SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
8565e1051a39Sopenharmony_ci                       verify_cb);
8566e1051a39Sopenharmony_ci
8567e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8568e1051a39Sopenharmony_ci                                      NULL, NULL))
8569e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
8570e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
8571e1051a39Sopenharmony_ci        goto end;
8572e1051a39Sopenharmony_ci
8573e1051a39Sopenharmony_ci    testresult = 1;
8574e1051a39Sopenharmony_ci
8575e1051a39Sopenharmony_ci end:
8576e1051a39Sopenharmony_ci    SSL_free(serverssl);
8577e1051a39Sopenharmony_ci    SSL_free(clientssl);
8578e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8579e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8580e1051a39Sopenharmony_ci
8581e1051a39Sopenharmony_ci    return testresult;
8582e1051a39Sopenharmony_ci}
8583e1051a39Sopenharmony_ci
8584e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
8585e1051a39Sopenharmony_ci/*
8586e1051a39Sopenharmony_ci * Test setting certificate authorities on both client and server.
8587e1051a39Sopenharmony_ci *
8588e1051a39Sopenharmony_ci * Test 0: SSL_CTX_set0_CA_list() only
8589e1051a39Sopenharmony_ci * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
8590e1051a39Sopenharmony_ci * Test 2: Only SSL_CTX_set_client_CA_list()
8591e1051a39Sopenharmony_ci */
8592e1051a39Sopenharmony_cistatic int test_ca_names_int(int prot, int tst)
8593e1051a39Sopenharmony_ci{
8594e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
8595e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8596e1051a39Sopenharmony_ci    int testresult = 0;
8597e1051a39Sopenharmony_ci    size_t i;
8598e1051a39Sopenharmony_ci    X509_NAME *name[] = { NULL, NULL, NULL, NULL };
8599e1051a39Sopenharmony_ci    char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
8600e1051a39Sopenharmony_ci    STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
8601e1051a39Sopenharmony_ci    const STACK_OF(X509_NAME) *sktmp = NULL;
8602e1051a39Sopenharmony_ci
8603e1051a39Sopenharmony_ci    for (i = 0; i < OSSL_NELEM(name); i++) {
8604e1051a39Sopenharmony_ci        name[i] = X509_NAME_new();
8605e1051a39Sopenharmony_ci        if (!TEST_ptr(name[i])
8606e1051a39Sopenharmony_ci                || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
8607e1051a39Sopenharmony_ci                                                         MBSTRING_ASC,
8608e1051a39Sopenharmony_ci                                                         (unsigned char *)
8609e1051a39Sopenharmony_ci                                                         strnames[i],
8610e1051a39Sopenharmony_ci                                                         -1, -1, 0)))
8611e1051a39Sopenharmony_ci            goto end;
8612e1051a39Sopenharmony_ci    }
8613e1051a39Sopenharmony_ci
8614e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8615e1051a39Sopenharmony_ci                                       TLS_client_method(),
8616e1051a39Sopenharmony_ci                                       TLS1_VERSION,
8617e1051a39Sopenharmony_ci                                       prot,
8618e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
8619e1051a39Sopenharmony_ci        goto end;
8620e1051a39Sopenharmony_ci
8621e1051a39Sopenharmony_ci    SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
8622e1051a39Sopenharmony_ci
8623e1051a39Sopenharmony_ci    if (tst == 0 || tst == 1) {
8624e1051a39Sopenharmony_ci        if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8625e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
8626e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
8627e1051a39Sopenharmony_ci                || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8628e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
8629e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
8630e1051a39Sopenharmony_ci            goto end;
8631e1051a39Sopenharmony_ci
8632e1051a39Sopenharmony_ci        SSL_CTX_set0_CA_list(sctx, sk1);
8633e1051a39Sopenharmony_ci        SSL_CTX_set0_CA_list(cctx, sk2);
8634e1051a39Sopenharmony_ci        sk1 = sk2 = NULL;
8635e1051a39Sopenharmony_ci    }
8636e1051a39Sopenharmony_ci    if (tst == 1 || tst == 2) {
8637e1051a39Sopenharmony_ci        if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
8638e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
8639e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
8640e1051a39Sopenharmony_ci                || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
8641e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
8642e1051a39Sopenharmony_ci                || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
8643e1051a39Sopenharmony_ci            goto end;
8644e1051a39Sopenharmony_ci
8645e1051a39Sopenharmony_ci        SSL_CTX_set_client_CA_list(sctx, sk1);
8646e1051a39Sopenharmony_ci        SSL_CTX_set_client_CA_list(cctx, sk2);
8647e1051a39Sopenharmony_ci        sk1 = sk2 = NULL;
8648e1051a39Sopenharmony_ci    }
8649e1051a39Sopenharmony_ci
8650e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8651e1051a39Sopenharmony_ci                                      NULL, NULL))
8652e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
8653e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
8654e1051a39Sopenharmony_ci        goto end;
8655e1051a39Sopenharmony_ci
8656e1051a39Sopenharmony_ci    /*
8657e1051a39Sopenharmony_ci     * We only expect certificate authorities to have been sent to the server
8658e1051a39Sopenharmony_ci     * if we are using TLSv1.3 and SSL_set0_CA_list() was used
8659e1051a39Sopenharmony_ci     */
8660e1051a39Sopenharmony_ci    sktmp = SSL_get0_peer_CA_list(serverssl);
8661e1051a39Sopenharmony_ci    if (prot == TLS1_3_VERSION
8662e1051a39Sopenharmony_ci            && (tst == 0 || tst == 1)) {
8663e1051a39Sopenharmony_ci        if (!TEST_ptr(sktmp)
8664e1051a39Sopenharmony_ci                || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8665e1051a39Sopenharmony_ci                || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8666e1051a39Sopenharmony_ci                                              name[0]), 0)
8667e1051a39Sopenharmony_ci                || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8668e1051a39Sopenharmony_ci                                              name[1]), 0))
8669e1051a39Sopenharmony_ci            goto end;
8670e1051a39Sopenharmony_ci    } else if (!TEST_ptr_null(sktmp)) {
8671e1051a39Sopenharmony_ci        goto end;
8672e1051a39Sopenharmony_ci    }
8673e1051a39Sopenharmony_ci
8674e1051a39Sopenharmony_ci    /*
8675e1051a39Sopenharmony_ci     * In all tests we expect certificate authorities to have been sent to the
8676e1051a39Sopenharmony_ci     * client. However, SSL_set_client_CA_list() should override
8677e1051a39Sopenharmony_ci     * SSL_set0_CA_list()
8678e1051a39Sopenharmony_ci     */
8679e1051a39Sopenharmony_ci    sktmp = SSL_get0_peer_CA_list(clientssl);
8680e1051a39Sopenharmony_ci    if (!TEST_ptr(sktmp)
8681e1051a39Sopenharmony_ci            || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
8682e1051a39Sopenharmony_ci            || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
8683e1051a39Sopenharmony_ci                                          name[tst == 0 ? 0 : 2]), 0)
8684e1051a39Sopenharmony_ci            || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
8685e1051a39Sopenharmony_ci                                          name[tst == 0 ? 1 : 3]), 0))
8686e1051a39Sopenharmony_ci        goto end;
8687e1051a39Sopenharmony_ci
8688e1051a39Sopenharmony_ci    testresult = 1;
8689e1051a39Sopenharmony_ci
8690e1051a39Sopenharmony_ci end:
8691e1051a39Sopenharmony_ci    SSL_free(serverssl);
8692e1051a39Sopenharmony_ci    SSL_free(clientssl);
8693e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8694e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8695e1051a39Sopenharmony_ci    for (i = 0; i < OSSL_NELEM(name); i++)
8696e1051a39Sopenharmony_ci        X509_NAME_free(name[i]);
8697e1051a39Sopenharmony_ci    sk_X509_NAME_pop_free(sk1, X509_NAME_free);
8698e1051a39Sopenharmony_ci    sk_X509_NAME_pop_free(sk2, X509_NAME_free);
8699e1051a39Sopenharmony_ci
8700e1051a39Sopenharmony_ci    return testresult;
8701e1051a39Sopenharmony_ci}
8702e1051a39Sopenharmony_ci#endif
8703e1051a39Sopenharmony_ci
8704e1051a39Sopenharmony_cistatic int test_ca_names(int tst)
8705e1051a39Sopenharmony_ci{
8706e1051a39Sopenharmony_ci    int testresult = 1;
8707e1051a39Sopenharmony_ci
8708e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
8709e1051a39Sopenharmony_ci    testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
8710e1051a39Sopenharmony_ci#endif
8711e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
8712e1051a39Sopenharmony_ci    testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
8713e1051a39Sopenharmony_ci#endif
8714e1051a39Sopenharmony_ci
8715e1051a39Sopenharmony_ci    return testresult;
8716e1051a39Sopenharmony_ci}
8717e1051a39Sopenharmony_ci
8718e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
8719e1051a39Sopenharmony_cistatic const char *multiblock_cipherlist_data[]=
8720e1051a39Sopenharmony_ci{
8721e1051a39Sopenharmony_ci    "AES128-SHA",
8722e1051a39Sopenharmony_ci    "AES128-SHA256",
8723e1051a39Sopenharmony_ci    "AES256-SHA",
8724e1051a39Sopenharmony_ci    "AES256-SHA256",
8725e1051a39Sopenharmony_ci};
8726e1051a39Sopenharmony_ci
8727e1051a39Sopenharmony_ci/* Reduce the fragment size - so the multiblock test buffer can be small */
8728e1051a39Sopenharmony_ci# define MULTIBLOCK_FRAGSIZE 512
8729e1051a39Sopenharmony_ci
8730e1051a39Sopenharmony_cistatic int test_multiblock_write(int test_index)
8731e1051a39Sopenharmony_ci{
8732e1051a39Sopenharmony_ci    static const char *fetchable_ciphers[]=
8733e1051a39Sopenharmony_ci    {
8734e1051a39Sopenharmony_ci        "AES-128-CBC-HMAC-SHA1",
8735e1051a39Sopenharmony_ci        "AES-128-CBC-HMAC-SHA256",
8736e1051a39Sopenharmony_ci        "AES-256-CBC-HMAC-SHA1",
8737e1051a39Sopenharmony_ci        "AES-256-CBC-HMAC-SHA256"
8738e1051a39Sopenharmony_ci    };
8739e1051a39Sopenharmony_ci    const char *cipherlist = multiblock_cipherlist_data[test_index];
8740e1051a39Sopenharmony_ci    const SSL_METHOD *smeth = TLS_server_method();
8741e1051a39Sopenharmony_ci    const SSL_METHOD *cmeth = TLS_client_method();
8742e1051a39Sopenharmony_ci    int min_version = TLS1_VERSION;
8743e1051a39Sopenharmony_ci    int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
8744e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
8745e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
8746e1051a39Sopenharmony_ci    int testresult = 0;
8747e1051a39Sopenharmony_ci
8748e1051a39Sopenharmony_ci    /*
8749e1051a39Sopenharmony_ci     * Choose a buffer large enough to perform a multi-block operation
8750e1051a39Sopenharmony_ci     * i.e: write_len >= 4 * frag_size
8751e1051a39Sopenharmony_ci     * 9 * is chosen so that multiple multiblocks are used + some leftover.
8752e1051a39Sopenharmony_ci     */
8753e1051a39Sopenharmony_ci    unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
8754e1051a39Sopenharmony_ci    unsigned char buf[sizeof(msg)], *p = buf;
8755e1051a39Sopenharmony_ci    size_t readbytes, written, len;
8756e1051a39Sopenharmony_ci    EVP_CIPHER *ciph = NULL;
8757e1051a39Sopenharmony_ci
8758e1051a39Sopenharmony_ci    /*
8759e1051a39Sopenharmony_ci     * Check if the cipher exists before attempting to use it since it only has
8760e1051a39Sopenharmony_ci     * a hardware specific implementation.
8761e1051a39Sopenharmony_ci     */
8762e1051a39Sopenharmony_ci    ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
8763e1051a39Sopenharmony_ci    if (ciph == NULL) {
8764e1051a39Sopenharmony_ci        TEST_skip("Multiblock cipher is not available for %s", cipherlist);
8765e1051a39Sopenharmony_ci        return 1;
8766e1051a39Sopenharmony_ci    }
8767e1051a39Sopenharmony_ci    EVP_CIPHER_free(ciph);
8768e1051a39Sopenharmony_ci
8769e1051a39Sopenharmony_ci    /* Set up a buffer with some data that will be sent to the client */
8770e1051a39Sopenharmony_ci    RAND_bytes(msg, sizeof(msg));
8771e1051a39Sopenharmony_ci
8772e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
8773e1051a39Sopenharmony_ci                                       max_version, &sctx, &cctx, cert,
8774e1051a39Sopenharmony_ci                                       privkey)))
8775e1051a39Sopenharmony_ci        goto end;
8776e1051a39Sopenharmony_ci
8777e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
8778e1051a39Sopenharmony_ci        goto end;
8779e1051a39Sopenharmony_ci
8780e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8781e1051a39Sopenharmony_ci                                      NULL, NULL)))
8782e1051a39Sopenharmony_ci            goto end;
8783e1051a39Sopenharmony_ci
8784e1051a39Sopenharmony_ci    /* settings to force it to use AES-CBC-HMAC_SHA */
8785e1051a39Sopenharmony_ci    SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
8786e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
8787e1051a39Sopenharmony_ci       goto end;
8788e1051a39Sopenharmony_ci
8789e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8790e1051a39Sopenharmony_ci        goto end;
8791e1051a39Sopenharmony_ci
8792e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
8793e1051a39Sopenharmony_ci        || !TEST_size_t_eq(written, sizeof(msg)))
8794e1051a39Sopenharmony_ci        goto end;
8795e1051a39Sopenharmony_ci
8796e1051a39Sopenharmony_ci    len = written;
8797e1051a39Sopenharmony_ci    while (len > 0) {
8798e1051a39Sopenharmony_ci        if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
8799e1051a39Sopenharmony_ci            goto end;
8800e1051a39Sopenharmony_ci        p += readbytes;
8801e1051a39Sopenharmony_ci        len -= readbytes;
8802e1051a39Sopenharmony_ci    }
8803e1051a39Sopenharmony_ci    if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
8804e1051a39Sopenharmony_ci        goto end;
8805e1051a39Sopenharmony_ci
8806e1051a39Sopenharmony_ci    testresult = 1;
8807e1051a39Sopenharmony_ciend:
8808e1051a39Sopenharmony_ci    SSL_free(serverssl);
8809e1051a39Sopenharmony_ci    SSL_free(clientssl);
8810e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
8811e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
8812e1051a39Sopenharmony_ci
8813e1051a39Sopenharmony_ci    return testresult;
8814e1051a39Sopenharmony_ci}
8815e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_TLS1_2 */
8816e1051a39Sopenharmony_ci
8817e1051a39Sopenharmony_cistatic int test_session_timeout(int test)
8818e1051a39Sopenharmony_ci{
8819e1051a39Sopenharmony_ci    /*
8820e1051a39Sopenharmony_ci     * Test session ordering and timeout
8821e1051a39Sopenharmony_ci     * Can't explicitly test performance of the new code,
8822e1051a39Sopenharmony_ci     * but can test to see if the ordering of the sessions
8823e1051a39Sopenharmony_ci     * are correct, and they they are removed as expected
8824e1051a39Sopenharmony_ci     */
8825e1051a39Sopenharmony_ci    SSL_SESSION *early = NULL;
8826e1051a39Sopenharmony_ci    SSL_SESSION *middle = NULL;
8827e1051a39Sopenharmony_ci    SSL_SESSION *late = NULL;
8828e1051a39Sopenharmony_ci    SSL_CTX *ctx;
8829e1051a39Sopenharmony_ci    int testresult = 0;
8830e1051a39Sopenharmony_ci    long now = (long)time(NULL);
8831e1051a39Sopenharmony_ci#define TIMEOUT 10
8832e1051a39Sopenharmony_ci
8833e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
8834e1051a39Sopenharmony_ci        || !TEST_ptr(early = SSL_SESSION_new())
8835e1051a39Sopenharmony_ci        || !TEST_ptr(middle = SSL_SESSION_new())
8836e1051a39Sopenharmony_ci        || !TEST_ptr(late = SSL_SESSION_new()))
8837e1051a39Sopenharmony_ci        goto end;
8838e1051a39Sopenharmony_ci
8839e1051a39Sopenharmony_ci    /* assign unique session ids */
8840e1051a39Sopenharmony_ci    early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8841e1051a39Sopenharmony_ci    memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
8842e1051a39Sopenharmony_ci    middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8843e1051a39Sopenharmony_ci    memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
8844e1051a39Sopenharmony_ci    late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
8845e1051a39Sopenharmony_ci    memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
8846e1051a39Sopenharmony_ci
8847e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8848e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8849e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8850e1051a39Sopenharmony_ci        goto end;
8851e1051a39Sopenharmony_ci
8852e1051a39Sopenharmony_ci    /* Make sure they are all added */
8853e1051a39Sopenharmony_ci    if (!TEST_ptr(early->prev)
8854e1051a39Sopenharmony_ci        || !TEST_ptr(middle->prev)
8855e1051a39Sopenharmony_ci        || !TEST_ptr(late->prev))
8856e1051a39Sopenharmony_ci        goto end;
8857e1051a39Sopenharmony_ci
8858e1051a39Sopenharmony_ci    if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
8859e1051a39Sopenharmony_ci        || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
8860e1051a39Sopenharmony_ci        || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
8861e1051a39Sopenharmony_ci        goto end;
8862e1051a39Sopenharmony_ci
8863e1051a39Sopenharmony_ci    if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
8864e1051a39Sopenharmony_ci        || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
8865e1051a39Sopenharmony_ci        || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
8866e1051a39Sopenharmony_ci        goto end;
8867e1051a39Sopenharmony_ci
8868e1051a39Sopenharmony_ci    /* Make sure they are all still there */
8869e1051a39Sopenharmony_ci    if (!TEST_ptr(early->prev)
8870e1051a39Sopenharmony_ci        || !TEST_ptr(middle->prev)
8871e1051a39Sopenharmony_ci        || !TEST_ptr(late->prev))
8872e1051a39Sopenharmony_ci        goto end;
8873e1051a39Sopenharmony_ci
8874e1051a39Sopenharmony_ci    /* Make sure they are in the expected order */
8875e1051a39Sopenharmony_ci    if (!TEST_ptr_eq(late->next, middle)
8876e1051a39Sopenharmony_ci        || !TEST_ptr_eq(middle->next, early)
8877e1051a39Sopenharmony_ci        || !TEST_ptr_eq(early->prev, middle)
8878e1051a39Sopenharmony_ci        || !TEST_ptr_eq(middle->prev, late))
8879e1051a39Sopenharmony_ci        goto end;
8880e1051a39Sopenharmony_ci
8881e1051a39Sopenharmony_ci    /* This should remove "early" */
8882e1051a39Sopenharmony_ci    SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
8883e1051a39Sopenharmony_ci    if (!TEST_ptr_null(early->prev)
8884e1051a39Sopenharmony_ci        || !TEST_ptr(middle->prev)
8885e1051a39Sopenharmony_ci        || !TEST_ptr(late->prev))
8886e1051a39Sopenharmony_ci        goto end;
8887e1051a39Sopenharmony_ci
8888e1051a39Sopenharmony_ci    /* This should remove "middle" */
8889e1051a39Sopenharmony_ci    SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
8890e1051a39Sopenharmony_ci    if (!TEST_ptr_null(early->prev)
8891e1051a39Sopenharmony_ci        || !TEST_ptr_null(middle->prev)
8892e1051a39Sopenharmony_ci        || !TEST_ptr(late->prev))
8893e1051a39Sopenharmony_ci        goto end;
8894e1051a39Sopenharmony_ci
8895e1051a39Sopenharmony_ci    /* This should remove "late" */
8896e1051a39Sopenharmony_ci    SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
8897e1051a39Sopenharmony_ci    if (!TEST_ptr_null(early->prev)
8898e1051a39Sopenharmony_ci        || !TEST_ptr_null(middle->prev)
8899e1051a39Sopenharmony_ci        || !TEST_ptr_null(late->prev))
8900e1051a39Sopenharmony_ci        goto end;
8901e1051a39Sopenharmony_ci
8902e1051a39Sopenharmony_ci    /* Add them back in again */
8903e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8904e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
8905e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
8906e1051a39Sopenharmony_ci        goto end;
8907e1051a39Sopenharmony_ci
8908e1051a39Sopenharmony_ci    /* Make sure they are all added */
8909e1051a39Sopenharmony_ci    if (!TEST_ptr(early->prev)
8910e1051a39Sopenharmony_ci        || !TEST_ptr(middle->prev)
8911e1051a39Sopenharmony_ci        || !TEST_ptr(late->prev))
8912e1051a39Sopenharmony_ci        goto end;
8913e1051a39Sopenharmony_ci
8914e1051a39Sopenharmony_ci    /* This should remove all of them */
8915e1051a39Sopenharmony_ci    SSL_CTX_flush_sessions(ctx, 0);
8916e1051a39Sopenharmony_ci    if (!TEST_ptr_null(early->prev)
8917e1051a39Sopenharmony_ci        || !TEST_ptr_null(middle->prev)
8918e1051a39Sopenharmony_ci        || !TEST_ptr_null(late->prev))
8919e1051a39Sopenharmony_ci        goto end;
8920e1051a39Sopenharmony_ci
8921e1051a39Sopenharmony_ci    (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
8922e1051a39Sopenharmony_ci                                         | SSL_CTX_get_session_cache_mode(ctx));
8923e1051a39Sopenharmony_ci
8924e1051a39Sopenharmony_ci    /* make sure |now| is NOT  equal to the current time */
8925e1051a39Sopenharmony_ci    now -= 10;
8926e1051a39Sopenharmony_ci    if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
8927e1051a39Sopenharmony_ci        || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
8928e1051a39Sopenharmony_ci        || !TEST_long_ne(SSL_SESSION_get_time(early), now))
8929e1051a39Sopenharmony_ci        goto end;
8930e1051a39Sopenharmony_ci
8931e1051a39Sopenharmony_ci    testresult = 1;
8932e1051a39Sopenharmony_ci end:
8933e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
8934e1051a39Sopenharmony_ci    SSL_SESSION_free(early);
8935e1051a39Sopenharmony_ci    SSL_SESSION_free(middle);
8936e1051a39Sopenharmony_ci    SSL_SESSION_free(late);
8937e1051a39Sopenharmony_ci    return testresult;
8938e1051a39Sopenharmony_ci}
8939e1051a39Sopenharmony_ci
8940e1051a39Sopenharmony_ci/*
8941e1051a39Sopenharmony_ci * Test that a session cache overflow works as expected
8942e1051a39Sopenharmony_ci * Test 0: TLSv1.3, timeout on new session later than old session
8943e1051a39Sopenharmony_ci * Test 1: TLSv1.2, timeout on new session later than old session
8944e1051a39Sopenharmony_ci * Test 2: TLSv1.3, timeout on new session earlier than old session
8945e1051a39Sopenharmony_ci * Test 3: TLSv1.2, timeout on new session earlier than old session
8946e1051a39Sopenharmony_ci */
8947e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
8948e1051a39Sopenharmony_cistatic int test_session_cache_overflow(int idx)
8949e1051a39Sopenharmony_ci{
8950e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
8951e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
8952e1051a39Sopenharmony_ci    int testresult = 0;
8953e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
8954e1051a39Sopenharmony_ci
8955e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
8956e1051a39Sopenharmony_ci    /* If no TLSv1.3 available then do nothing in this case */
8957e1051a39Sopenharmony_ci    if (idx % 2 == 0)
8958e1051a39Sopenharmony_ci        return TEST_skip("No TLSv1.3 available");
8959e1051a39Sopenharmony_ci#endif
8960e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
8961e1051a39Sopenharmony_ci    /* If no TLSv1.2 available then do nothing in this case */
8962e1051a39Sopenharmony_ci    if (idx % 2 == 1)
8963e1051a39Sopenharmony_ci        return TEST_skip("No TLSv1.2 available");
8964e1051a39Sopenharmony_ci#endif
8965e1051a39Sopenharmony_ci
8966e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
8967e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION,
8968e1051a39Sopenharmony_ci                                       (idx % 2 == 0) ? TLS1_3_VERSION
8969e1051a39Sopenharmony_ci                                                      : TLS1_2_VERSION,
8970e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
8971e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)))
8972e1051a39Sopenharmony_ci        goto end;
8973e1051a39Sopenharmony_ci
8974e1051a39Sopenharmony_ci    SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
8975e1051a39Sopenharmony_ci    get_sess_val = NULL;
8976e1051a39Sopenharmony_ci
8977e1051a39Sopenharmony_ci    SSL_CTX_sess_set_cache_size(sctx, 1);
8978e1051a39Sopenharmony_ci
8979e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
8980e1051a39Sopenharmony_ci                                      NULL, NULL)))
8981e1051a39Sopenharmony_ci        goto end;
8982e1051a39Sopenharmony_ci
8983e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
8984e1051a39Sopenharmony_ci        goto end;
8985e1051a39Sopenharmony_ci
8986e1051a39Sopenharmony_ci    if (idx > 1) {
8987e1051a39Sopenharmony_ci        sess = SSL_get_session(serverssl);
8988e1051a39Sopenharmony_ci        if (!TEST_ptr(sess))
8989e1051a39Sopenharmony_ci            goto end;
8990e1051a39Sopenharmony_ci
8991e1051a39Sopenharmony_ci        /*
8992e1051a39Sopenharmony_ci         * Cause this session to have a longer timeout than the next session to
8993e1051a39Sopenharmony_ci         * be added.
8994e1051a39Sopenharmony_ci         */
8995e1051a39Sopenharmony_ci        if (!TEST_true(SSL_SESSION_set_timeout(sess, LONG_MAX / 2))) {
8996e1051a39Sopenharmony_ci            sess = NULL;
8997e1051a39Sopenharmony_ci            goto end;
8998e1051a39Sopenharmony_ci        }
8999e1051a39Sopenharmony_ci        sess = NULL;
9000e1051a39Sopenharmony_ci    }
9001e1051a39Sopenharmony_ci
9002e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
9003e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
9004e1051a39Sopenharmony_ci    SSL_free(serverssl);
9005e1051a39Sopenharmony_ci    SSL_free(clientssl);
9006e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
9007e1051a39Sopenharmony_ci
9008e1051a39Sopenharmony_ci    /*
9009e1051a39Sopenharmony_ci     * Session cache size is 1 and we already populated the cache with a session
9010e1051a39Sopenharmony_ci     * so the next connection should cause an overflow.
9011e1051a39Sopenharmony_ci     */
9012e1051a39Sopenharmony_ci
9013e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9014e1051a39Sopenharmony_ci                                      NULL, NULL)))
9015e1051a39Sopenharmony_ci        goto end;
9016e1051a39Sopenharmony_ci
9017e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9018e1051a39Sopenharmony_ci        goto end;
9019e1051a39Sopenharmony_ci
9020e1051a39Sopenharmony_ci    /*
9021e1051a39Sopenharmony_ci     * The session we just negotiated may have been already removed from the
9022e1051a39Sopenharmony_ci     * internal cache - but we will return it anyway from our external cache.
9023e1051a39Sopenharmony_ci     */
9024e1051a39Sopenharmony_ci    get_sess_val = SSL_get_session(serverssl);
9025e1051a39Sopenharmony_ci    if (!TEST_ptr(get_sess_val))
9026e1051a39Sopenharmony_ci        goto end;
9027e1051a39Sopenharmony_ci    sess = SSL_get1_session(clientssl);
9028e1051a39Sopenharmony_ci    if (!TEST_ptr(sess))
9029e1051a39Sopenharmony_ci        goto end;
9030e1051a39Sopenharmony_ci
9031e1051a39Sopenharmony_ci    SSL_shutdown(serverssl);
9032e1051a39Sopenharmony_ci    SSL_shutdown(clientssl);
9033e1051a39Sopenharmony_ci    SSL_free(serverssl);
9034e1051a39Sopenharmony_ci    SSL_free(clientssl);
9035e1051a39Sopenharmony_ci    serverssl = clientssl = NULL;
9036e1051a39Sopenharmony_ci
9037e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9038e1051a39Sopenharmony_ci                                      NULL, NULL)))
9039e1051a39Sopenharmony_ci        goto end;
9040e1051a39Sopenharmony_ci
9041e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_session(clientssl, sess)))
9042e1051a39Sopenharmony_ci        goto end;
9043e1051a39Sopenharmony_ci
9044e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9045e1051a39Sopenharmony_ci        goto end;
9046e1051a39Sopenharmony_ci
9047e1051a39Sopenharmony_ci    testresult = 1;
9048e1051a39Sopenharmony_ci
9049e1051a39Sopenharmony_ci end:
9050e1051a39Sopenharmony_ci    SSL_free(serverssl);
9051e1051a39Sopenharmony_ci    SSL_free(clientssl);
9052e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9053e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9054e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
9055e1051a39Sopenharmony_ci
9056e1051a39Sopenharmony_ci    return testresult;
9057e1051a39Sopenharmony_ci}
9058e1051a39Sopenharmony_ci#endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
9059e1051a39Sopenharmony_ci
9060e1051a39Sopenharmony_ci/*
9061e1051a39Sopenharmony_ci * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
9062e1051a39Sopenharmony_ci * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
9063e1051a39Sopenharmony_ci * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
9064e1051a39Sopenharmony_ci * Test 3: Client does not set servername on initial handshake (TLSv1.2)
9065e1051a39Sopenharmony_ci * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
9066e1051a39Sopenharmony_ci * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
9067e1051a39Sopenharmony_ci * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
9068e1051a39Sopenharmony_ci * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
9069e1051a39Sopenharmony_ci * Test 8: Client does not set servername on initial handshake(TLSv1.3)
9070e1051a39Sopenharmony_ci * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
9071e1051a39Sopenharmony_ci */
9072e1051a39Sopenharmony_cistatic int test_servername(int tst)
9073e1051a39Sopenharmony_ci{
9074e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9075e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9076e1051a39Sopenharmony_ci    int testresult = 0;
9077e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
9078e1051a39Sopenharmony_ci    const char *sexpectedhost = NULL, *cexpectedhost = NULL;
9079e1051a39Sopenharmony_ci
9080e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
9081e1051a39Sopenharmony_ci    if (tst <= 4)
9082e1051a39Sopenharmony_ci        return 1;
9083e1051a39Sopenharmony_ci#endif
9084e1051a39Sopenharmony_ci#ifdef OSSL_NO_USABLE_TLS1_3
9085e1051a39Sopenharmony_ci    if (tst >= 5)
9086e1051a39Sopenharmony_ci        return 1;
9087e1051a39Sopenharmony_ci#endif
9088e1051a39Sopenharmony_ci
9089e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9090e1051a39Sopenharmony_ci                                       TLS_client_method(),
9091e1051a39Sopenharmony_ci                                       TLS1_VERSION,
9092e1051a39Sopenharmony_ci                                       (tst <= 4) ? TLS1_2_VERSION
9093e1051a39Sopenharmony_ci                                                  : TLS1_3_VERSION,
9094e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
9095e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9096e1051a39Sopenharmony_ci                                             NULL, NULL)))
9097e1051a39Sopenharmony_ci        goto end;
9098e1051a39Sopenharmony_ci
9099e1051a39Sopenharmony_ci    if (tst != 1 && tst != 6) {
9100e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
9101e1051a39Sopenharmony_ci                                                              hostname_cb)))
9102e1051a39Sopenharmony_ci            goto end;
9103e1051a39Sopenharmony_ci    }
9104e1051a39Sopenharmony_ci
9105e1051a39Sopenharmony_ci    if (tst != 3 && tst != 8) {
9106e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9107e1051a39Sopenharmony_ci            goto end;
9108e1051a39Sopenharmony_ci        sexpectedhost = cexpectedhost = "goodhost";
9109e1051a39Sopenharmony_ci    }
9110e1051a39Sopenharmony_ci
9111e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9112e1051a39Sopenharmony_ci        goto end;
9113e1051a39Sopenharmony_ci
9114e1051a39Sopenharmony_ci    if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
9115e1051a39Sopenharmony_ci                     cexpectedhost)
9116e1051a39Sopenharmony_ci            || !TEST_str_eq(SSL_get_servername(serverssl,
9117e1051a39Sopenharmony_ci                                               TLSEXT_NAMETYPE_host_name),
9118e1051a39Sopenharmony_ci                            sexpectedhost))
9119e1051a39Sopenharmony_ci        goto end;
9120e1051a39Sopenharmony_ci
9121e1051a39Sopenharmony_ci    /* Now repeat with a resumption handshake */
9122e1051a39Sopenharmony_ci
9123e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
9124e1051a39Sopenharmony_ci            || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
9125e1051a39Sopenharmony_ci            || !TEST_true(SSL_SESSION_is_resumable(sess))
9126e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_shutdown(serverssl), 0))
9127e1051a39Sopenharmony_ci        goto end;
9128e1051a39Sopenharmony_ci
9129e1051a39Sopenharmony_ci    SSL_free(clientssl);
9130e1051a39Sopenharmony_ci    SSL_free(serverssl);
9131e1051a39Sopenharmony_ci    clientssl = serverssl = NULL;
9132e1051a39Sopenharmony_ci
9133e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
9134e1051a39Sopenharmony_ci                                      NULL)))
9135e1051a39Sopenharmony_ci        goto end;
9136e1051a39Sopenharmony_ci
9137e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_session(clientssl, sess)))
9138e1051a39Sopenharmony_ci        goto end;
9139e1051a39Sopenharmony_ci
9140e1051a39Sopenharmony_ci    sexpectedhost = cexpectedhost = "goodhost";
9141e1051a39Sopenharmony_ci    if (tst == 2 || tst == 7) {
9142e1051a39Sopenharmony_ci        /* Set an inconsistent hostname */
9143e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
9144e1051a39Sopenharmony_ci            goto end;
9145e1051a39Sopenharmony_ci        /*
9146e1051a39Sopenharmony_ci         * In TLSv1.2 we expect the hostname from the original handshake, in
9147e1051a39Sopenharmony_ci         * TLSv1.3 we expect the hostname from this handshake
9148e1051a39Sopenharmony_ci         */
9149e1051a39Sopenharmony_ci        if (tst == 7)
9150e1051a39Sopenharmony_ci            sexpectedhost = cexpectedhost = "altgoodhost";
9151e1051a39Sopenharmony_ci
9152e1051a39Sopenharmony_ci        if (!TEST_str_eq(SSL_get_servername(clientssl,
9153e1051a39Sopenharmony_ci                                            TLSEXT_NAMETYPE_host_name),
9154e1051a39Sopenharmony_ci                         "altgoodhost"))
9155e1051a39Sopenharmony_ci            goto end;
9156e1051a39Sopenharmony_ci    } else if (tst == 4 || tst == 9) {
9157e1051a39Sopenharmony_ci        /*
9158e1051a39Sopenharmony_ci         * A TLSv1.3 session does not associate a session with a servername,
9159e1051a39Sopenharmony_ci         * but a TLSv1.2 session does.
9160e1051a39Sopenharmony_ci         */
9161e1051a39Sopenharmony_ci        if (tst == 9)
9162e1051a39Sopenharmony_ci            sexpectedhost = cexpectedhost = NULL;
9163e1051a39Sopenharmony_ci
9164e1051a39Sopenharmony_ci        if (!TEST_str_eq(SSL_get_servername(clientssl,
9165e1051a39Sopenharmony_ci                                            TLSEXT_NAMETYPE_host_name),
9166e1051a39Sopenharmony_ci                         cexpectedhost))
9167e1051a39Sopenharmony_ci            goto end;
9168e1051a39Sopenharmony_ci    } else {
9169e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
9170e1051a39Sopenharmony_ci            goto end;
9171e1051a39Sopenharmony_ci        /*
9172e1051a39Sopenharmony_ci         * In a TLSv1.2 resumption where the hostname was not acknowledged
9173e1051a39Sopenharmony_ci         * we expect the hostname on the server to be empty. On the client we
9174e1051a39Sopenharmony_ci         * return what was requested in this case.
9175e1051a39Sopenharmony_ci         *
9176e1051a39Sopenharmony_ci         * Similarly if the client didn't set a hostname on an original TLSv1.2
9177e1051a39Sopenharmony_ci         * session but is now, the server hostname will be empty, but the client
9178e1051a39Sopenharmony_ci         * is as we set it.
9179e1051a39Sopenharmony_ci         */
9180e1051a39Sopenharmony_ci        if (tst == 1 || tst == 3)
9181e1051a39Sopenharmony_ci            sexpectedhost = NULL;
9182e1051a39Sopenharmony_ci
9183e1051a39Sopenharmony_ci        if (!TEST_str_eq(SSL_get_servername(clientssl,
9184e1051a39Sopenharmony_ci                                            TLSEXT_NAMETYPE_host_name),
9185e1051a39Sopenharmony_ci                         "goodhost"))
9186e1051a39Sopenharmony_ci            goto end;
9187e1051a39Sopenharmony_ci    }
9188e1051a39Sopenharmony_ci
9189e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9190e1051a39Sopenharmony_ci        goto end;
9191e1051a39Sopenharmony_ci
9192e1051a39Sopenharmony_ci    if (!TEST_true(SSL_session_reused(clientssl))
9193e1051a39Sopenharmony_ci            || !TEST_true(SSL_session_reused(serverssl))
9194e1051a39Sopenharmony_ci            || !TEST_str_eq(SSL_get_servername(clientssl,
9195e1051a39Sopenharmony_ci                                               TLSEXT_NAMETYPE_host_name),
9196e1051a39Sopenharmony_ci                            cexpectedhost)
9197e1051a39Sopenharmony_ci            || !TEST_str_eq(SSL_get_servername(serverssl,
9198e1051a39Sopenharmony_ci                                               TLSEXT_NAMETYPE_host_name),
9199e1051a39Sopenharmony_ci                            sexpectedhost))
9200e1051a39Sopenharmony_ci        goto end;
9201e1051a39Sopenharmony_ci
9202e1051a39Sopenharmony_ci    testresult = 1;
9203e1051a39Sopenharmony_ci
9204e1051a39Sopenharmony_ci end:
9205e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
9206e1051a39Sopenharmony_ci    SSL_free(serverssl);
9207e1051a39Sopenharmony_ci    SSL_free(clientssl);
9208e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9209e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9210e1051a39Sopenharmony_ci
9211e1051a39Sopenharmony_ci    return testresult;
9212e1051a39Sopenharmony_ci}
9213e1051a39Sopenharmony_ci
9214e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_EC) \
9215e1051a39Sopenharmony_ci    && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9216e1051a39Sopenharmony_ci/*
9217e1051a39Sopenharmony_ci * Test that if signature algorithms are not available, then we do not offer or
9218e1051a39Sopenharmony_ci * accept them.
9219e1051a39Sopenharmony_ci * Test 0: Two RSA sig algs available: both RSA sig algs shared
9220e1051a39Sopenharmony_ci * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
9221e1051a39Sopenharmony_ci * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
9222e1051a39Sopenharmony_ci * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
9223e1051a39Sopenharmony_ci * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
9224e1051a39Sopenharmony_ci * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
9225e1051a39Sopenharmony_ci */
9226e1051a39Sopenharmony_cistatic int test_sigalgs_available(int idx)
9227e1051a39Sopenharmony_ci{
9228e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9229e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9230e1051a39Sopenharmony_ci    int testresult = 0;
9231e1051a39Sopenharmony_ci    OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
9232e1051a39Sopenharmony_ci    OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
9233e1051a39Sopenharmony_ci    OSSL_PROVIDER *filterprov = NULL;
9234e1051a39Sopenharmony_ci    int sig, hash;
9235e1051a39Sopenharmony_ci
9236e1051a39Sopenharmony_ci    if (!TEST_ptr(tmpctx))
9237e1051a39Sopenharmony_ci        goto end;
9238e1051a39Sopenharmony_ci
9239e1051a39Sopenharmony_ci    if (idx != 0 && idx != 3) {
9240e1051a39Sopenharmony_ci        if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
9241e1051a39Sopenharmony_ci                                                 filter_provider_init)))
9242e1051a39Sopenharmony_ci            goto end;
9243e1051a39Sopenharmony_ci
9244e1051a39Sopenharmony_ci        filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
9245e1051a39Sopenharmony_ci        if (!TEST_ptr(filterprov))
9246e1051a39Sopenharmony_ci            goto end;
9247e1051a39Sopenharmony_ci
9248e1051a39Sopenharmony_ci        if (idx < 3) {
9249e1051a39Sopenharmony_ci            /*
9250e1051a39Sopenharmony_ci             * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
9251e1051a39Sopenharmony_ci             * or accepted for the peer that uses this libctx. Note that libssl
9252e1051a39Sopenharmony_ci             * *requires* SHA2-256 to be available so we cannot disable that. We
9253e1051a39Sopenharmony_ci             * also need SHA1 for our certificate.
9254e1051a39Sopenharmony_ci             */
9255e1051a39Sopenharmony_ci            if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
9256e1051a39Sopenharmony_ci                                                      "SHA2-256:SHA1")))
9257e1051a39Sopenharmony_ci                goto end;
9258e1051a39Sopenharmony_ci        } else {
9259e1051a39Sopenharmony_ci            if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
9260e1051a39Sopenharmony_ci                                                      "ECDSA"))
9261e1051a39Sopenharmony_ci                    || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
9262e1051a39Sopenharmony_ci                                                             "EC:X25519:X448")))
9263e1051a39Sopenharmony_ci                goto end;
9264e1051a39Sopenharmony_ci        }
9265e1051a39Sopenharmony_ci
9266e1051a39Sopenharmony_ci        if (idx == 1 || idx == 4)
9267e1051a39Sopenharmony_ci            clientctx = tmpctx;
9268e1051a39Sopenharmony_ci        else
9269e1051a39Sopenharmony_ci            serverctx = tmpctx;
9270e1051a39Sopenharmony_ci    }
9271e1051a39Sopenharmony_ci
9272e1051a39Sopenharmony_ci    cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
9273e1051a39Sopenharmony_ci    sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
9274e1051a39Sopenharmony_ci    if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
9275e1051a39Sopenharmony_ci        goto end;
9276e1051a39Sopenharmony_ci
9277e1051a39Sopenharmony_ci    if (idx != 5) {
9278e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9279e1051a39Sopenharmony_ci                                           TLS_client_method(),
9280e1051a39Sopenharmony_ci                                           TLS1_VERSION,
9281e1051a39Sopenharmony_ci                                           0,
9282e1051a39Sopenharmony_ci                                           &sctx, &cctx, cert, privkey)))
9283e1051a39Sopenharmony_ci            goto end;
9284e1051a39Sopenharmony_ci    } else {
9285e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9286e1051a39Sopenharmony_ci                                           TLS_client_method(),
9287e1051a39Sopenharmony_ci                                           TLS1_VERSION,
9288e1051a39Sopenharmony_ci                                           0,
9289e1051a39Sopenharmony_ci                                           &sctx, &cctx, cert2, privkey2)))
9290e1051a39Sopenharmony_ci            goto end;
9291e1051a39Sopenharmony_ci    }
9292e1051a39Sopenharmony_ci
9293e1051a39Sopenharmony_ci    /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
9294e1051a39Sopenharmony_ci    if (idx < 4) {
9295e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9296e1051a39Sopenharmony_ci                                               "ECDHE-RSA-AES128-GCM-SHA256")))
9297e1051a39Sopenharmony_ci            goto end;
9298e1051a39Sopenharmony_ci    } else {
9299e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
9300e1051a39Sopenharmony_ci                                               "ECDHE-ECDSA-AES128-GCM-SHA256")))
9301e1051a39Sopenharmony_ci            goto end;
9302e1051a39Sopenharmony_ci    }
9303e1051a39Sopenharmony_ci
9304e1051a39Sopenharmony_ci    if (idx < 3) {
9305e1051a39Sopenharmony_ci        if (!SSL_CTX_set1_sigalgs_list(cctx,
9306e1051a39Sopenharmony_ci                                       "rsa_pss_rsae_sha384"
9307e1051a39Sopenharmony_ci                                       ":rsa_pss_rsae_sha256")
9308e1051a39Sopenharmony_ci                || !SSL_CTX_set1_sigalgs_list(sctx,
9309e1051a39Sopenharmony_ci                                              "rsa_pss_rsae_sha384"
9310e1051a39Sopenharmony_ci                                              ":rsa_pss_rsae_sha256"))
9311e1051a39Sopenharmony_ci            goto end;
9312e1051a39Sopenharmony_ci    } else {
9313e1051a39Sopenharmony_ci        if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
9314e1051a39Sopenharmony_ci                || !SSL_CTX_set1_sigalgs_list(sctx,
9315e1051a39Sopenharmony_ci                                              "rsa_pss_rsae_sha256:ECDSA+SHA256"))
9316e1051a39Sopenharmony_ci            goto end;
9317e1051a39Sopenharmony_ci    }
9318e1051a39Sopenharmony_ci
9319e1051a39Sopenharmony_ci    if (idx != 5
9320e1051a39Sopenharmony_ci        && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
9321e1051a39Sopenharmony_ci                                                      SSL_FILETYPE_PEM), 1)
9322e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
9323e1051a39Sopenharmony_ci                                                        privkey2,
9324e1051a39Sopenharmony_ci                                                        SSL_FILETYPE_PEM), 1)
9325e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
9326e1051a39Sopenharmony_ci        goto end;
9327e1051a39Sopenharmony_ci
9328e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9329e1051a39Sopenharmony_ci                                      NULL, NULL)))
9330e1051a39Sopenharmony_ci        goto end;
9331e1051a39Sopenharmony_ci
9332e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9333e1051a39Sopenharmony_ci        goto end;
9334e1051a39Sopenharmony_ci
9335e1051a39Sopenharmony_ci    /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
9336e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
9337e1051a39Sopenharmony_ci                                            NULL, NULL),
9338e1051a39Sopenharmony_ci                     (idx == 0 || idx == 3) ? 2 : 1))
9339e1051a39Sopenharmony_ci        goto end;
9340e1051a39Sopenharmony_ci
9341e1051a39Sopenharmony_ci    if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
9342e1051a39Sopenharmony_ci        goto end;
9343e1051a39Sopenharmony_ci
9344e1051a39Sopenharmony_ci    if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
9345e1051a39Sopenharmony_ci                                                 : NID_rsassaPss))
9346e1051a39Sopenharmony_ci        goto end;
9347e1051a39Sopenharmony_ci
9348e1051a39Sopenharmony_ci    testresult = filter_provider_check_clean_finish();
9349e1051a39Sopenharmony_ci
9350e1051a39Sopenharmony_ci end:
9351e1051a39Sopenharmony_ci    SSL_free(serverssl);
9352e1051a39Sopenharmony_ci    SSL_free(clientssl);
9353e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9354e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9355e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(filterprov);
9356e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(tmpctx);
9357e1051a39Sopenharmony_ci
9358e1051a39Sopenharmony_ci    return testresult;
9359e1051a39Sopenharmony_ci}
9360e1051a39Sopenharmony_ci#endif /*
9361e1051a39Sopenharmony_ci        * !defined(OPENSSL_NO_EC) \
9362e1051a39Sopenharmony_ci        * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
9363e1051a39Sopenharmony_ci        */
9364e1051a39Sopenharmony_ci
9365e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_3
9366e1051a39Sopenharmony_ci/* This test can run in TLSv1.3 even if ec and dh are disabled */
9367e1051a39Sopenharmony_cistatic int test_pluggable_group(int idx)
9368e1051a39Sopenharmony_ci{
9369e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9370e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9371e1051a39Sopenharmony_ci    int testresult = 0;
9372e1051a39Sopenharmony_ci    OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
9373e1051a39Sopenharmony_ci    /* Check that we are not impacted by a provider without any groups */
9374e1051a39Sopenharmony_ci    OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
9375e1051a39Sopenharmony_ci    const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup";
9376e1051a39Sopenharmony_ci
9377e1051a39Sopenharmony_ci    if (!TEST_ptr(tlsprov))
9378e1051a39Sopenharmony_ci        goto end;
9379e1051a39Sopenharmony_ci
9380e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9381e1051a39Sopenharmony_ci                                       TLS_client_method(),
9382e1051a39Sopenharmony_ci                                       TLS1_3_VERSION,
9383e1051a39Sopenharmony_ci                                       TLS1_3_VERSION,
9384e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey))
9385e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9386e1051a39Sopenharmony_ci                                             NULL, NULL)))
9387e1051a39Sopenharmony_ci        goto end;
9388e1051a39Sopenharmony_ci
9389e1051a39Sopenharmony_ci    /* ensure GROUPLIST_INCREMENT (=40) logic triggers: */
9390e1051a39Sopenharmony_ci    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"))
9391e1051a39Sopenharmony_ci    /* removing a single algorithm from the list makes the test pass */
9392e1051a39Sopenharmony_ci            || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
9393e1051a39Sopenharmony_ci        goto end;
9394e1051a39Sopenharmony_ci
9395e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9396e1051a39Sopenharmony_ci        goto end;
9397e1051a39Sopenharmony_ci
9398e1051a39Sopenharmony_ci    if (!TEST_str_eq(group_name,
9399e1051a39Sopenharmony_ci                     SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
9400e1051a39Sopenharmony_ci        goto end;
9401e1051a39Sopenharmony_ci
9402e1051a39Sopenharmony_ci    testresult = 1;
9403e1051a39Sopenharmony_ci
9404e1051a39Sopenharmony_ci end:
9405e1051a39Sopenharmony_ci    SSL_free(serverssl);
9406e1051a39Sopenharmony_ci    SSL_free(clientssl);
9407e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9408e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9409e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(tlsprov);
9410e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(legacyprov);
9411e1051a39Sopenharmony_ci
9412e1051a39Sopenharmony_ci    return testresult;
9413e1051a39Sopenharmony_ci}
9414e1051a39Sopenharmony_ci#endif
9415e1051a39Sopenharmony_ci
9416e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
9417e1051a39Sopenharmony_cistatic int test_ssl_dup(void)
9418e1051a39Sopenharmony_ci{
9419e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9420e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
9421e1051a39Sopenharmony_ci    int testresult = 0;
9422e1051a39Sopenharmony_ci    BIO *rbio = NULL, *wbio = NULL;
9423e1051a39Sopenharmony_ci
9424e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9425e1051a39Sopenharmony_ci                                       TLS_client_method(),
9426e1051a39Sopenharmony_ci                                       0,
9427e1051a39Sopenharmony_ci                                       0,
9428e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
9429e1051a39Sopenharmony_ci        goto end;
9430e1051a39Sopenharmony_ci
9431e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9432e1051a39Sopenharmony_ci                                             NULL, NULL)))
9433e1051a39Sopenharmony_ci        goto end;
9434e1051a39Sopenharmony_ci
9435e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
9436e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
9437e1051a39Sopenharmony_ci        goto end;
9438e1051a39Sopenharmony_ci
9439e1051a39Sopenharmony_ci    client2ssl = SSL_dup(clientssl);
9440e1051a39Sopenharmony_ci    rbio = SSL_get_rbio(clientssl);
9441e1051a39Sopenharmony_ci    if (!TEST_ptr(rbio)
9442e1051a39Sopenharmony_ci            || !TEST_true(BIO_up_ref(rbio)))
9443e1051a39Sopenharmony_ci        goto end;
9444e1051a39Sopenharmony_ci    SSL_set0_rbio(client2ssl, rbio);
9445e1051a39Sopenharmony_ci    rbio = NULL;
9446e1051a39Sopenharmony_ci
9447e1051a39Sopenharmony_ci    wbio = SSL_get_wbio(clientssl);
9448e1051a39Sopenharmony_ci    if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
9449e1051a39Sopenharmony_ci        goto end;
9450e1051a39Sopenharmony_ci    SSL_set0_wbio(client2ssl, wbio);
9451e1051a39Sopenharmony_ci    rbio = NULL;
9452e1051a39Sopenharmony_ci
9453e1051a39Sopenharmony_ci    if (!TEST_ptr(client2ssl)
9454e1051a39Sopenharmony_ci               /* Handshake not started so pointers should be different */
9455e1051a39Sopenharmony_ci            || !TEST_ptr_ne(clientssl, client2ssl))
9456e1051a39Sopenharmony_ci        goto end;
9457e1051a39Sopenharmony_ci
9458e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
9459e1051a39Sopenharmony_ci            || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
9460e1051a39Sopenharmony_ci        goto end;
9461e1051a39Sopenharmony_ci
9462e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
9463e1051a39Sopenharmony_ci        goto end;
9464e1051a39Sopenharmony_ci
9465e1051a39Sopenharmony_ci    SSL_free(clientssl);
9466e1051a39Sopenharmony_ci    clientssl = SSL_dup(client2ssl);
9467e1051a39Sopenharmony_ci    if (!TEST_ptr(clientssl)
9468e1051a39Sopenharmony_ci               /* Handshake has finished so pointers should be the same */
9469e1051a39Sopenharmony_ci            || !TEST_ptr_eq(clientssl, client2ssl))
9470e1051a39Sopenharmony_ci        goto end;
9471e1051a39Sopenharmony_ci
9472e1051a39Sopenharmony_ci    testresult = 1;
9473e1051a39Sopenharmony_ci
9474e1051a39Sopenharmony_ci end:
9475e1051a39Sopenharmony_ci    SSL_free(serverssl);
9476e1051a39Sopenharmony_ci    SSL_free(clientssl);
9477e1051a39Sopenharmony_ci    SSL_free(client2ssl);
9478e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9479e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9480e1051a39Sopenharmony_ci
9481e1051a39Sopenharmony_ci    return testresult;
9482e1051a39Sopenharmony_ci}
9483e1051a39Sopenharmony_ci
9484e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
9485e1051a39Sopenharmony_ci
9486e1051a39Sopenharmony_cistatic EVP_PKEY *tmp_dh_params = NULL;
9487e1051a39Sopenharmony_ci
9488e1051a39Sopenharmony_ci/* Helper function for the test_set_tmp_dh() tests */
9489e1051a39Sopenharmony_cistatic EVP_PKEY *get_tmp_dh_params(void)
9490e1051a39Sopenharmony_ci{
9491e1051a39Sopenharmony_ci    if (tmp_dh_params == NULL) {
9492e1051a39Sopenharmony_ci        BIGNUM *p = NULL;
9493e1051a39Sopenharmony_ci        OSSL_PARAM_BLD *tmpl = NULL;
9494e1051a39Sopenharmony_ci        EVP_PKEY_CTX *pctx = NULL;
9495e1051a39Sopenharmony_ci        OSSL_PARAM *params = NULL;
9496e1051a39Sopenharmony_ci        EVP_PKEY *dhpkey = NULL;
9497e1051a39Sopenharmony_ci
9498e1051a39Sopenharmony_ci        p = BN_get_rfc3526_prime_2048(NULL);
9499e1051a39Sopenharmony_ci        if (!TEST_ptr(p))
9500e1051a39Sopenharmony_ci            goto end;
9501e1051a39Sopenharmony_ci
9502e1051a39Sopenharmony_ci        pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
9503e1051a39Sopenharmony_ci        if (!TEST_ptr(pctx)
9504e1051a39Sopenharmony_ci                || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
9505e1051a39Sopenharmony_ci            goto end;
9506e1051a39Sopenharmony_ci
9507e1051a39Sopenharmony_ci        tmpl = OSSL_PARAM_BLD_new();
9508e1051a39Sopenharmony_ci        if (!TEST_ptr(tmpl)
9509e1051a39Sopenharmony_ci                || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
9510e1051a39Sopenharmony_ci                                                        OSSL_PKEY_PARAM_FFC_P,
9511e1051a39Sopenharmony_ci                                                        p))
9512e1051a39Sopenharmony_ci                || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
9513e1051a39Sopenharmony_ci                                                        OSSL_PKEY_PARAM_FFC_G,
9514e1051a39Sopenharmony_ci                                                        2)))
9515e1051a39Sopenharmony_ci            goto end;
9516e1051a39Sopenharmony_ci
9517e1051a39Sopenharmony_ci        params = OSSL_PARAM_BLD_to_param(tmpl);
9518e1051a39Sopenharmony_ci        if (!TEST_ptr(params)
9519e1051a39Sopenharmony_ci                || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
9520e1051a39Sopenharmony_ci                                                  EVP_PKEY_KEY_PARAMETERS,
9521e1051a39Sopenharmony_ci                                                  params), 1))
9522e1051a39Sopenharmony_ci            goto end;
9523e1051a39Sopenharmony_ci
9524e1051a39Sopenharmony_ci        tmp_dh_params = dhpkey;
9525e1051a39Sopenharmony_ci    end:
9526e1051a39Sopenharmony_ci        BN_free(p);
9527e1051a39Sopenharmony_ci        EVP_PKEY_CTX_free(pctx);
9528e1051a39Sopenharmony_ci        OSSL_PARAM_BLD_free(tmpl);
9529e1051a39Sopenharmony_ci        OSSL_PARAM_free(params);
9530e1051a39Sopenharmony_ci    }
9531e1051a39Sopenharmony_ci
9532e1051a39Sopenharmony_ci    if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
9533e1051a39Sopenharmony_ci        return NULL;
9534e1051a39Sopenharmony_ci
9535e1051a39Sopenharmony_ci    return tmp_dh_params;
9536e1051a39Sopenharmony_ci}
9537e1051a39Sopenharmony_ci
9538e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9539e1051a39Sopenharmony_ci/* Callback used by test_set_tmp_dh() */
9540e1051a39Sopenharmony_cistatic DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
9541e1051a39Sopenharmony_ci{
9542e1051a39Sopenharmony_ci    EVP_PKEY *dhpkey = get_tmp_dh_params();
9543e1051a39Sopenharmony_ci    DH *ret = NULL;
9544e1051a39Sopenharmony_ci
9545e1051a39Sopenharmony_ci    if (!TEST_ptr(dhpkey))
9546e1051a39Sopenharmony_ci        return NULL;
9547e1051a39Sopenharmony_ci
9548e1051a39Sopenharmony_ci    /*
9549e1051a39Sopenharmony_ci     * libssl does not free the returned DH, so we free it now knowing that even
9550e1051a39Sopenharmony_ci     * after we free dhpkey, there will still be a reference to the owning
9551e1051a39Sopenharmony_ci     * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
9552e1051a39Sopenharmony_ci     * of time we need it for.
9553e1051a39Sopenharmony_ci     */
9554e1051a39Sopenharmony_ci    ret = EVP_PKEY_get1_DH(dhpkey);
9555e1051a39Sopenharmony_ci    DH_free(ret);
9556e1051a39Sopenharmony_ci
9557e1051a39Sopenharmony_ci    EVP_PKEY_free(dhpkey);
9558e1051a39Sopenharmony_ci
9559e1051a39Sopenharmony_ci    return ret;
9560e1051a39Sopenharmony_ci}
9561e1051a39Sopenharmony_ci#  endif
9562e1051a39Sopenharmony_ci
9563e1051a39Sopenharmony_ci/*
9564e1051a39Sopenharmony_ci * Test the various methods for setting temporary DH parameters
9565e1051a39Sopenharmony_ci *
9566e1051a39Sopenharmony_ci * Test  0: Default (no auto) setting
9567e1051a39Sopenharmony_ci * Test  1: Explicit SSL_CTX auto off
9568e1051a39Sopenharmony_ci * Test  2: Explicit SSL auto off
9569e1051a39Sopenharmony_ci * Test  3: Explicit SSL_CTX auto on
9570e1051a39Sopenharmony_ci * Test  4: Explicit SSL auto on
9571e1051a39Sopenharmony_ci * Test  5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
9572e1051a39Sopenharmony_ci * Test  6: Explicit SSL auto off, custom DH params via EVP_PKEY
9573e1051a39Sopenharmony_ci *
9574e1051a39Sopenharmony_ci * The following are testing deprecated APIs, so we only run them if available
9575e1051a39Sopenharmony_ci * Test  7: Explicit SSL_CTX auto off, custom DH params via DH
9576e1051a39Sopenharmony_ci * Test  8: Explicit SSL auto off, custom DH params via DH
9577e1051a39Sopenharmony_ci * Test  9: Explicit SSL_CTX auto off, custom DH params via callback
9578e1051a39Sopenharmony_ci * Test 10: Explicit SSL auto off, custom DH params via callback
9579e1051a39Sopenharmony_ci */
9580e1051a39Sopenharmony_cistatic int test_set_tmp_dh(int idx)
9581e1051a39Sopenharmony_ci{
9582e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9583e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9584e1051a39Sopenharmony_ci    int testresult = 0;
9585e1051a39Sopenharmony_ci    int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
9586e1051a39Sopenharmony_ci    int expected = (idx <= 2) ? 0 : 1;
9587e1051a39Sopenharmony_ci    EVP_PKEY *dhpkey = NULL;
9588e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9589e1051a39Sopenharmony_ci    DH *dh = NULL;
9590e1051a39Sopenharmony_ci#  else
9591e1051a39Sopenharmony_ci
9592e1051a39Sopenharmony_ci    if (idx >= 7)
9593e1051a39Sopenharmony_ci        return 1;
9594e1051a39Sopenharmony_ci#  endif
9595e1051a39Sopenharmony_ci
9596e1051a39Sopenharmony_ci    if (idx >= 5 && idx <= 8) {
9597e1051a39Sopenharmony_ci        dhpkey = get_tmp_dh_params();
9598e1051a39Sopenharmony_ci        if (!TEST_ptr(dhpkey))
9599e1051a39Sopenharmony_ci            goto end;
9600e1051a39Sopenharmony_ci    }
9601e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9602e1051a39Sopenharmony_ci    if (idx == 7 || idx == 8) {
9603e1051a39Sopenharmony_ci        dh = EVP_PKEY_get1_DH(dhpkey);
9604e1051a39Sopenharmony_ci        if (!TEST_ptr(dh))
9605e1051a39Sopenharmony_ci            goto end;
9606e1051a39Sopenharmony_ci    }
9607e1051a39Sopenharmony_ci#  endif
9608e1051a39Sopenharmony_ci
9609e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9610e1051a39Sopenharmony_ci                                       TLS_client_method(),
9611e1051a39Sopenharmony_ci                                       0,
9612e1051a39Sopenharmony_ci                                       0,
9613e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
9614e1051a39Sopenharmony_ci        goto end;
9615e1051a39Sopenharmony_ci
9616e1051a39Sopenharmony_ci    if ((idx & 1) == 1) {
9617e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
9618e1051a39Sopenharmony_ci            goto end;
9619e1051a39Sopenharmony_ci    }
9620e1051a39Sopenharmony_ci
9621e1051a39Sopenharmony_ci    if (idx == 5) {
9622e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
9623e1051a39Sopenharmony_ci            goto end;
9624e1051a39Sopenharmony_ci        dhpkey = NULL;
9625e1051a39Sopenharmony_ci    }
9626e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9627e1051a39Sopenharmony_ci    else if (idx == 7) {
9628e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
9629e1051a39Sopenharmony_ci            goto end;
9630e1051a39Sopenharmony_ci    } else if (idx == 9) {
9631e1051a39Sopenharmony_ci        SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
9632e1051a39Sopenharmony_ci    }
9633e1051a39Sopenharmony_ci#  endif
9634e1051a39Sopenharmony_ci
9635e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9636e1051a39Sopenharmony_ci                                      NULL, NULL)))
9637e1051a39Sopenharmony_ci        goto end;
9638e1051a39Sopenharmony_ci
9639e1051a39Sopenharmony_ci    if ((idx & 1) == 0 && idx != 0) {
9640e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
9641e1051a39Sopenharmony_ci            goto end;
9642e1051a39Sopenharmony_ci    }
9643e1051a39Sopenharmony_ci    if (idx == 6) {
9644e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
9645e1051a39Sopenharmony_ci            goto end;
9646e1051a39Sopenharmony_ci        dhpkey = NULL;
9647e1051a39Sopenharmony_ci    }
9648e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9649e1051a39Sopenharmony_ci    else if (idx == 8) {
9650e1051a39Sopenharmony_ci        if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
9651e1051a39Sopenharmony_ci            goto end;
9652e1051a39Sopenharmony_ci    } else if (idx == 10) {
9653e1051a39Sopenharmony_ci        SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
9654e1051a39Sopenharmony_ci    }
9655e1051a39Sopenharmony_ci#  endif
9656e1051a39Sopenharmony_ci
9657e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9658e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9659e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
9660e1051a39Sopenharmony_ci        goto end;
9661e1051a39Sopenharmony_ci
9662e1051a39Sopenharmony_ci    /*
9663e1051a39Sopenharmony_ci     * If autoon then we should succeed. Otherwise we expect failure because
9664e1051a39Sopenharmony_ci     * there are no parameters
9665e1051a39Sopenharmony_ci     */
9666e1051a39Sopenharmony_ci    if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
9667e1051a39Sopenharmony_ci                                           SSL_ERROR_NONE), expected))
9668e1051a39Sopenharmony_ci        goto end;
9669e1051a39Sopenharmony_ci
9670e1051a39Sopenharmony_ci    testresult = 1;
9671e1051a39Sopenharmony_ci
9672e1051a39Sopenharmony_ci end:
9673e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_DEPRECATED_3_0
9674e1051a39Sopenharmony_ci    DH_free(dh);
9675e1051a39Sopenharmony_ci#  endif
9676e1051a39Sopenharmony_ci    SSL_free(serverssl);
9677e1051a39Sopenharmony_ci    SSL_free(clientssl);
9678e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9679e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9680e1051a39Sopenharmony_ci    EVP_PKEY_free(dhpkey);
9681e1051a39Sopenharmony_ci
9682e1051a39Sopenharmony_ci    return testresult;
9683e1051a39Sopenharmony_ci}
9684e1051a39Sopenharmony_ci
9685e1051a39Sopenharmony_ci/*
9686e1051a39Sopenharmony_ci * Test the auto DH keys are appropriately sized
9687e1051a39Sopenharmony_ci */
9688e1051a39Sopenharmony_cistatic int test_dh_auto(int idx)
9689e1051a39Sopenharmony_ci{
9690e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9691e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9692e1051a39Sopenharmony_ci    int testresult = 0;
9693e1051a39Sopenharmony_ci    EVP_PKEY *tmpkey = NULL;
9694e1051a39Sopenharmony_ci    char *thiscert = NULL, *thiskey = NULL;
9695e1051a39Sopenharmony_ci    size_t expdhsize = 0;
9696e1051a39Sopenharmony_ci    const char *ciphersuite = "DHE-RSA-AES128-SHA";
9697e1051a39Sopenharmony_ci
9698e1051a39Sopenharmony_ci    switch (idx) {
9699e1051a39Sopenharmony_ci    case 0:
9700e1051a39Sopenharmony_ci        /* The FIPS provider doesn't support this DH size - so we ignore it */
9701e1051a39Sopenharmony_ci        if (is_fips)
9702e1051a39Sopenharmony_ci            return 1;
9703e1051a39Sopenharmony_ci        thiscert = cert1024;
9704e1051a39Sopenharmony_ci        thiskey = privkey1024;
9705e1051a39Sopenharmony_ci        expdhsize = 1024;
9706e1051a39Sopenharmony_ci        break;
9707e1051a39Sopenharmony_ci    case 1:
9708e1051a39Sopenharmony_ci        /* 2048 bit prime */
9709e1051a39Sopenharmony_ci        thiscert = cert;
9710e1051a39Sopenharmony_ci        thiskey = privkey;
9711e1051a39Sopenharmony_ci        expdhsize = 2048;
9712e1051a39Sopenharmony_ci        break;
9713e1051a39Sopenharmony_ci    case 2:
9714e1051a39Sopenharmony_ci        thiscert = cert3072;
9715e1051a39Sopenharmony_ci        thiskey = privkey3072;
9716e1051a39Sopenharmony_ci        expdhsize = 3072;
9717e1051a39Sopenharmony_ci        break;
9718e1051a39Sopenharmony_ci    case 3:
9719e1051a39Sopenharmony_ci        thiscert = cert4096;
9720e1051a39Sopenharmony_ci        thiskey = privkey4096;
9721e1051a39Sopenharmony_ci        expdhsize = 4096;
9722e1051a39Sopenharmony_ci        break;
9723e1051a39Sopenharmony_ci    case 4:
9724e1051a39Sopenharmony_ci        thiscert = cert8192;
9725e1051a39Sopenharmony_ci        thiskey = privkey8192;
9726e1051a39Sopenharmony_ci        expdhsize = 8192;
9727e1051a39Sopenharmony_ci        break;
9728e1051a39Sopenharmony_ci    /* No certificate cases */
9729e1051a39Sopenharmony_ci    case 5:
9730e1051a39Sopenharmony_ci        /* The FIPS provider doesn't support this DH size - so we ignore it */
9731e1051a39Sopenharmony_ci        if (is_fips)
9732e1051a39Sopenharmony_ci            return 1;
9733e1051a39Sopenharmony_ci        ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
9734e1051a39Sopenharmony_ci        expdhsize = 1024;
9735e1051a39Sopenharmony_ci        break;
9736e1051a39Sopenharmony_ci    case 6:
9737e1051a39Sopenharmony_ci        ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
9738e1051a39Sopenharmony_ci        expdhsize = 3072;
9739e1051a39Sopenharmony_ci        break;
9740e1051a39Sopenharmony_ci    default:
9741e1051a39Sopenharmony_ci        TEST_error("Invalid text index");
9742e1051a39Sopenharmony_ci        goto end;
9743e1051a39Sopenharmony_ci    }
9744e1051a39Sopenharmony_ci
9745e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9746e1051a39Sopenharmony_ci                                       TLS_client_method(),
9747e1051a39Sopenharmony_ci                                       0,
9748e1051a39Sopenharmony_ci                                       0,
9749e1051a39Sopenharmony_ci                                       &sctx, &cctx, thiscert, thiskey)))
9750e1051a39Sopenharmony_ci        goto end;
9751e1051a39Sopenharmony_ci
9752e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
9753e1051a39Sopenharmony_ci                                      NULL, NULL)))
9754e1051a39Sopenharmony_ci        goto end;
9755e1051a39Sopenharmony_ci
9756e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
9757e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
9758e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
9759e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
9760e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
9761e1051a39Sopenharmony_ci        goto end;
9762e1051a39Sopenharmony_ci
9763e1051a39Sopenharmony_ci    /*
9764e1051a39Sopenharmony_ci     * Send the server's first flight. At this point the server has created the
9765e1051a39Sopenharmony_ci     * temporary DH key but hasn't finished using it yet. Once used it is
9766e1051a39Sopenharmony_ci     * removed, so we cannot test it.
9767e1051a39Sopenharmony_ci     */
9768e1051a39Sopenharmony_ci    if (!TEST_int_le(SSL_connect(clientssl), 0)
9769e1051a39Sopenharmony_ci            || !TEST_int_le(SSL_accept(serverssl), 0))
9770e1051a39Sopenharmony_ci        goto end;
9771e1051a39Sopenharmony_ci
9772e1051a39Sopenharmony_ci    if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
9773e1051a39Sopenharmony_ci        goto end;
9774e1051a39Sopenharmony_ci    if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
9775e1051a39Sopenharmony_ci        goto end;
9776e1051a39Sopenharmony_ci
9777e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9778e1051a39Sopenharmony_ci        goto end;
9779e1051a39Sopenharmony_ci
9780e1051a39Sopenharmony_ci    testresult = 1;
9781e1051a39Sopenharmony_ci
9782e1051a39Sopenharmony_ci end:
9783e1051a39Sopenharmony_ci    SSL_free(serverssl);
9784e1051a39Sopenharmony_ci    SSL_free(clientssl);
9785e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9786e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9787e1051a39Sopenharmony_ci    EVP_PKEY_free(tmpkey);
9788e1051a39Sopenharmony_ci
9789e1051a39Sopenharmony_ci    return testresult;
9790e1051a39Sopenharmony_ci
9791e1051a39Sopenharmony_ci}
9792e1051a39Sopenharmony_ci# endif /* OPENSSL_NO_DH */
9793e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_TLS1_2 */
9794e1051a39Sopenharmony_ci
9795e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
9796e1051a39Sopenharmony_ci/*
9797e1051a39Sopenharmony_ci * Test that setting an SNI callback works with TLSv1.3. Specifically we check
9798e1051a39Sopenharmony_ci * that it works even without a certificate configured for the original
9799e1051a39Sopenharmony_ci * SSL_CTX
9800e1051a39Sopenharmony_ci */
9801e1051a39Sopenharmony_cistatic int test_sni_tls13(void)
9802e1051a39Sopenharmony_ci{
9803e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
9804e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9805e1051a39Sopenharmony_ci    int testresult = 0;
9806e1051a39Sopenharmony_ci
9807e1051a39Sopenharmony_ci    /* Reset callback counter */
9808e1051a39Sopenharmony_ci    snicb = 0;
9809e1051a39Sopenharmony_ci
9810e1051a39Sopenharmony_ci    /* Create an initial SSL_CTX with no certificate configured */
9811e1051a39Sopenharmony_ci    sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9812e1051a39Sopenharmony_ci    if (!TEST_ptr(sctx))
9813e1051a39Sopenharmony_ci        goto end;
9814e1051a39Sopenharmony_ci    /* Require TLSv1.3 as a minimum */
9815e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9816e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_3_VERSION, 0,
9817e1051a39Sopenharmony_ci                                       &sctx2, &cctx, cert, privkey)))
9818e1051a39Sopenharmony_ci        goto end;
9819e1051a39Sopenharmony_ci
9820e1051a39Sopenharmony_ci    /* Set up SNI */
9821e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
9822e1051a39Sopenharmony_ci            || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
9823e1051a39Sopenharmony_ci        goto end;
9824e1051a39Sopenharmony_ci
9825e1051a39Sopenharmony_ci    /*
9826e1051a39Sopenharmony_ci     * Connection should still succeed because the final SSL_CTX has the right
9827e1051a39Sopenharmony_ci     * certificates configured.
9828e1051a39Sopenharmony_ci     */
9829e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9830e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
9831e1051a39Sopenharmony_ci            || !TEST_true(create_ssl_connection(serverssl, clientssl,
9832e1051a39Sopenharmony_ci                                                SSL_ERROR_NONE)))
9833e1051a39Sopenharmony_ci        goto end;
9834e1051a39Sopenharmony_ci
9835e1051a39Sopenharmony_ci    /* We should have had the SNI callback called exactly once */
9836e1051a39Sopenharmony_ci    if (!TEST_int_eq(snicb, 1))
9837e1051a39Sopenharmony_ci        goto end;
9838e1051a39Sopenharmony_ci
9839e1051a39Sopenharmony_ci    testresult = 1;
9840e1051a39Sopenharmony_ci
9841e1051a39Sopenharmony_ciend:
9842e1051a39Sopenharmony_ci    SSL_free(serverssl);
9843e1051a39Sopenharmony_ci    SSL_free(clientssl);
9844e1051a39Sopenharmony_ci    SSL_CTX_free(sctx2);
9845e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9846e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9847e1051a39Sopenharmony_ci    return testresult;
9848e1051a39Sopenharmony_ci}
9849e1051a39Sopenharmony_ci
9850e1051a39Sopenharmony_ci/*
9851e1051a39Sopenharmony_ci * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
9852e1051a39Sopenharmony_ci * 0 = TLSv1.2
9853e1051a39Sopenharmony_ci * 1 = TLSv1.3
9854e1051a39Sopenharmony_ci */
9855e1051a39Sopenharmony_cistatic int test_ticket_lifetime(int idx)
9856e1051a39Sopenharmony_ci{
9857e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
9858e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL;
9859e1051a39Sopenharmony_ci    int testresult = 0;
9860e1051a39Sopenharmony_ci    int version = TLS1_3_VERSION;
9861e1051a39Sopenharmony_ci
9862e1051a39Sopenharmony_ci#define ONE_WEEK_SEC (7 * 24 * 60 * 60)
9863e1051a39Sopenharmony_ci#define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
9864e1051a39Sopenharmony_ci
9865e1051a39Sopenharmony_ci    if (idx == 0) {
9866e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_TLS1_2
9867e1051a39Sopenharmony_ci        return TEST_skip("TLS 1.2 is disabled.");
9868e1051a39Sopenharmony_ci#else
9869e1051a39Sopenharmony_ci        version = TLS1_2_VERSION;
9870e1051a39Sopenharmony_ci#endif
9871e1051a39Sopenharmony_ci    }
9872e1051a39Sopenharmony_ci
9873e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
9874e1051a39Sopenharmony_ci                                       TLS_client_method(), version, version,
9875e1051a39Sopenharmony_ci                                       &sctx, &cctx, cert, privkey)))
9876e1051a39Sopenharmony_ci        goto end;
9877e1051a39Sopenharmony_ci
9878e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
9879e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
9880e1051a39Sopenharmony_ci        goto end;
9881e1051a39Sopenharmony_ci
9882e1051a39Sopenharmony_ci    /*
9883e1051a39Sopenharmony_ci     * Set the timeout to be more than 1 week
9884e1051a39Sopenharmony_ci     * make sure the returned value is the default
9885e1051a39Sopenharmony_ci     */
9886e1051a39Sopenharmony_ci    if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
9887e1051a39Sopenharmony_ci                      SSL_get_default_timeout(serverssl)))
9888e1051a39Sopenharmony_ci        goto end;
9889e1051a39Sopenharmony_ci
9890e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
9891e1051a39Sopenharmony_ci        goto end;
9892e1051a39Sopenharmony_ci
9893e1051a39Sopenharmony_ci    if (idx == 0) {
9894e1051a39Sopenharmony_ci        /* TLSv1.2 uses the set value */
9895e1051a39Sopenharmony_ci        if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
9896e1051a39Sopenharmony_ci            goto end;
9897e1051a39Sopenharmony_ci    } else {
9898e1051a39Sopenharmony_ci        /* TLSv1.3 uses the limited value */
9899e1051a39Sopenharmony_ci        if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
9900e1051a39Sopenharmony_ci            goto end;
9901e1051a39Sopenharmony_ci    }
9902e1051a39Sopenharmony_ci    testresult = 1;
9903e1051a39Sopenharmony_ci
9904e1051a39Sopenharmony_ciend:
9905e1051a39Sopenharmony_ci    SSL_free(serverssl);
9906e1051a39Sopenharmony_ci    SSL_free(clientssl);
9907e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
9908e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
9909e1051a39Sopenharmony_ci    return testresult;
9910e1051a39Sopenharmony_ci}
9911e1051a39Sopenharmony_ci#endif
9912e1051a39Sopenharmony_ci/*
9913e1051a39Sopenharmony_ci * Test that setting an ALPN does not violate RFC
9914e1051a39Sopenharmony_ci */
9915e1051a39Sopenharmony_cistatic int test_set_alpn(void)
9916e1051a39Sopenharmony_ci{
9917e1051a39Sopenharmony_ci    SSL_CTX *ctx = NULL;
9918e1051a39Sopenharmony_ci    SSL *ssl = NULL;
9919e1051a39Sopenharmony_ci    int testresult = 0;
9920e1051a39Sopenharmony_ci
9921e1051a39Sopenharmony_ci    unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
9922e1051a39Sopenharmony_ci    unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
9923e1051a39Sopenharmony_ci    unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
9924e1051a39Sopenharmony_ci    unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
9925e1051a39Sopenharmony_ci    unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
9926e1051a39Sopenharmony_ci    unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
9927e1051a39Sopenharmony_ci
9928e1051a39Sopenharmony_ci    /* Create an initial SSL_CTX with no certificate configured */
9929e1051a39Sopenharmony_ci    ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9930e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx))
9931e1051a39Sopenharmony_ci        goto end;
9932e1051a39Sopenharmony_ci
9933e1051a39Sopenharmony_ci    /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
9934e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
9935e1051a39Sopenharmony_ci        goto end;
9936e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
9937e1051a39Sopenharmony_ci        goto end;
9938e1051a39Sopenharmony_ci    if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
9939e1051a39Sopenharmony_ci        goto end;
9940e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
9941e1051a39Sopenharmony_ci        goto end;
9942e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
9943e1051a39Sopenharmony_ci        goto end;
9944e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
9945e1051a39Sopenharmony_ci        goto end;
9946e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
9947e1051a39Sopenharmony_ci        goto end;
9948e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
9949e1051a39Sopenharmony_ci        goto end;
9950e1051a39Sopenharmony_ci    if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
9951e1051a39Sopenharmony_ci        goto end;
9952e1051a39Sopenharmony_ci
9953e1051a39Sopenharmony_ci    ssl = SSL_new(ctx);
9954e1051a39Sopenharmony_ci    if (!TEST_ptr(ssl))
9955e1051a39Sopenharmony_ci        goto end;
9956e1051a39Sopenharmony_ci
9957e1051a39Sopenharmony_ci    if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
9958e1051a39Sopenharmony_ci        goto end;
9959e1051a39Sopenharmony_ci    if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
9960e1051a39Sopenharmony_ci        goto end;
9961e1051a39Sopenharmony_ci    if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
9962e1051a39Sopenharmony_ci        goto end;
9963e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
9964e1051a39Sopenharmony_ci        goto end;
9965e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
9966e1051a39Sopenharmony_ci        goto end;
9967e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
9968e1051a39Sopenharmony_ci        goto end;
9969e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
9970e1051a39Sopenharmony_ci        goto end;
9971e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
9972e1051a39Sopenharmony_ci        goto end;
9973e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
9974e1051a39Sopenharmony_ci        goto end;
9975e1051a39Sopenharmony_ci
9976e1051a39Sopenharmony_ci    testresult = 1;
9977e1051a39Sopenharmony_ci
9978e1051a39Sopenharmony_ciend:
9979e1051a39Sopenharmony_ci    SSL_free(ssl);
9980e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
9981e1051a39Sopenharmony_ci    return testresult;
9982e1051a39Sopenharmony_ci}
9983e1051a39Sopenharmony_ci
9984e1051a39Sopenharmony_ci/*
9985e1051a39Sopenharmony_ci * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
9986e1051a39Sopenharmony_ci */
9987e1051a39Sopenharmony_cistatic int test_set_verify_cert_store_ssl_ctx(void)
9988e1051a39Sopenharmony_ci{
9989e1051a39Sopenharmony_ci   SSL_CTX *ctx = NULL;
9990e1051a39Sopenharmony_ci   int testresult = 0;
9991e1051a39Sopenharmony_ci   X509_STORE *store = NULL, *new_store = NULL,
9992e1051a39Sopenharmony_ci              *cstore = NULL, *new_cstore = NULL;
9993e1051a39Sopenharmony_ci
9994e1051a39Sopenharmony_ci   /* Create an initial SSL_CTX. */
9995e1051a39Sopenharmony_ci   ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
9996e1051a39Sopenharmony_ci   if (!TEST_ptr(ctx))
9997e1051a39Sopenharmony_ci       goto end;
9998e1051a39Sopenharmony_ci
9999e1051a39Sopenharmony_ci   /* Retrieve verify store pointer. */
10000e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10001e1051a39Sopenharmony_ci       goto end;
10002e1051a39Sopenharmony_ci
10003e1051a39Sopenharmony_ci   /* Retrieve chain store pointer. */
10004e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10005e1051a39Sopenharmony_ci       goto end;
10006e1051a39Sopenharmony_ci
10007e1051a39Sopenharmony_ci   /* We haven't set any yet, so this should be NULL. */
10008e1051a39Sopenharmony_ci   if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10009e1051a39Sopenharmony_ci       goto end;
10010e1051a39Sopenharmony_ci
10011e1051a39Sopenharmony_ci   /* Create stores. We use separate stores so pointers are different. */
10012e1051a39Sopenharmony_ci   new_store = X509_STORE_new();
10013e1051a39Sopenharmony_ci   if (!TEST_ptr(new_store))
10014e1051a39Sopenharmony_ci       goto end;
10015e1051a39Sopenharmony_ci
10016e1051a39Sopenharmony_ci   new_cstore = X509_STORE_new();
10017e1051a39Sopenharmony_ci   if (!TEST_ptr(new_cstore))
10018e1051a39Sopenharmony_ci       goto end;
10019e1051a39Sopenharmony_ci
10020e1051a39Sopenharmony_ci   /* Set stores. */
10021e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
10022e1051a39Sopenharmony_ci       goto end;
10023e1051a39Sopenharmony_ci
10024e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
10025e1051a39Sopenharmony_ci       goto end;
10026e1051a39Sopenharmony_ci
10027e1051a39Sopenharmony_ci   /* Should be able to retrieve the same pointer. */
10028e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10029e1051a39Sopenharmony_ci       goto end;
10030e1051a39Sopenharmony_ci
10031e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10032e1051a39Sopenharmony_ci       goto end;
10033e1051a39Sopenharmony_ci
10034e1051a39Sopenharmony_ci   if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10035e1051a39Sopenharmony_ci       goto end;
10036e1051a39Sopenharmony_ci
10037e1051a39Sopenharmony_ci   /* Should be able to unset again. */
10038e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
10039e1051a39Sopenharmony_ci       goto end;
10040e1051a39Sopenharmony_ci
10041e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
10042e1051a39Sopenharmony_ci       goto end;
10043e1051a39Sopenharmony_ci
10044e1051a39Sopenharmony_ci   /* Should now be NULL. */
10045e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
10046e1051a39Sopenharmony_ci       goto end;
10047e1051a39Sopenharmony_ci
10048e1051a39Sopenharmony_ci   if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
10049e1051a39Sopenharmony_ci       goto end;
10050e1051a39Sopenharmony_ci
10051e1051a39Sopenharmony_ci   if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10052e1051a39Sopenharmony_ci       goto end;
10053e1051a39Sopenharmony_ci
10054e1051a39Sopenharmony_ci   testresult = 1;
10055e1051a39Sopenharmony_ci
10056e1051a39Sopenharmony_ciend:
10057e1051a39Sopenharmony_ci   X509_STORE_free(new_store);
10058e1051a39Sopenharmony_ci   X509_STORE_free(new_cstore);
10059e1051a39Sopenharmony_ci   SSL_CTX_free(ctx);
10060e1051a39Sopenharmony_ci   return testresult;
10061e1051a39Sopenharmony_ci}
10062e1051a39Sopenharmony_ci
10063e1051a39Sopenharmony_ci/*
10064e1051a39Sopenharmony_ci * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
10065e1051a39Sopenharmony_ci */
10066e1051a39Sopenharmony_cistatic int test_set_verify_cert_store_ssl(void)
10067e1051a39Sopenharmony_ci{
10068e1051a39Sopenharmony_ci   SSL_CTX *ctx = NULL;
10069e1051a39Sopenharmony_ci   SSL *ssl = NULL;
10070e1051a39Sopenharmony_ci   int testresult = 0;
10071e1051a39Sopenharmony_ci   X509_STORE *store = NULL, *new_store = NULL,
10072e1051a39Sopenharmony_ci              *cstore = NULL, *new_cstore = NULL;
10073e1051a39Sopenharmony_ci
10074e1051a39Sopenharmony_ci   /* Create an initial SSL_CTX. */
10075e1051a39Sopenharmony_ci   ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10076e1051a39Sopenharmony_ci   if (!TEST_ptr(ctx))
10077e1051a39Sopenharmony_ci       goto end;
10078e1051a39Sopenharmony_ci
10079e1051a39Sopenharmony_ci   /* Create an SSL object. */
10080e1051a39Sopenharmony_ci   ssl = SSL_new(ctx);
10081e1051a39Sopenharmony_ci   if (!TEST_ptr(ssl))
10082e1051a39Sopenharmony_ci       goto end;
10083e1051a39Sopenharmony_ci
10084e1051a39Sopenharmony_ci   /* Retrieve verify store pointer. */
10085e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10086e1051a39Sopenharmony_ci       goto end;
10087e1051a39Sopenharmony_ci
10088e1051a39Sopenharmony_ci   /* Retrieve chain store pointer. */
10089e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10090e1051a39Sopenharmony_ci       goto end;
10091e1051a39Sopenharmony_ci
10092e1051a39Sopenharmony_ci   /* We haven't set any yet, so this should be NULL. */
10093e1051a39Sopenharmony_ci   if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10094e1051a39Sopenharmony_ci       goto end;
10095e1051a39Sopenharmony_ci
10096e1051a39Sopenharmony_ci   /* Create stores. We use separate stores so pointers are different. */
10097e1051a39Sopenharmony_ci   new_store = X509_STORE_new();
10098e1051a39Sopenharmony_ci   if (!TEST_ptr(new_store))
10099e1051a39Sopenharmony_ci       goto end;
10100e1051a39Sopenharmony_ci
10101e1051a39Sopenharmony_ci   new_cstore = X509_STORE_new();
10102e1051a39Sopenharmony_ci   if (!TEST_ptr(new_cstore))
10103e1051a39Sopenharmony_ci       goto end;
10104e1051a39Sopenharmony_ci
10105e1051a39Sopenharmony_ci   /* Set stores. */
10106e1051a39Sopenharmony_ci   if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
10107e1051a39Sopenharmony_ci       goto end;
10108e1051a39Sopenharmony_ci
10109e1051a39Sopenharmony_ci   if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
10110e1051a39Sopenharmony_ci       goto end;
10111e1051a39Sopenharmony_ci
10112e1051a39Sopenharmony_ci   /* Should be able to retrieve the same pointer. */
10113e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10114e1051a39Sopenharmony_ci       goto end;
10115e1051a39Sopenharmony_ci
10116e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10117e1051a39Sopenharmony_ci       goto end;
10118e1051a39Sopenharmony_ci
10119e1051a39Sopenharmony_ci   if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
10120e1051a39Sopenharmony_ci       goto end;
10121e1051a39Sopenharmony_ci
10122e1051a39Sopenharmony_ci   /* Should be able to unset again. */
10123e1051a39Sopenharmony_ci   if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
10124e1051a39Sopenharmony_ci       goto end;
10125e1051a39Sopenharmony_ci
10126e1051a39Sopenharmony_ci   if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
10127e1051a39Sopenharmony_ci       goto end;
10128e1051a39Sopenharmony_ci
10129e1051a39Sopenharmony_ci   /* Should now be NULL. */
10130e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
10131e1051a39Sopenharmony_ci       goto end;
10132e1051a39Sopenharmony_ci
10133e1051a39Sopenharmony_ci   if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
10134e1051a39Sopenharmony_ci       goto end;
10135e1051a39Sopenharmony_ci
10136e1051a39Sopenharmony_ci   if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
10137e1051a39Sopenharmony_ci       goto end;
10138e1051a39Sopenharmony_ci
10139e1051a39Sopenharmony_ci   testresult = 1;
10140e1051a39Sopenharmony_ci
10141e1051a39Sopenharmony_ciend:
10142e1051a39Sopenharmony_ci   X509_STORE_free(new_store);
10143e1051a39Sopenharmony_ci   X509_STORE_free(new_cstore);
10144e1051a39Sopenharmony_ci   SSL_free(ssl);
10145e1051a39Sopenharmony_ci   SSL_CTX_free(ctx);
10146e1051a39Sopenharmony_ci   return testresult;
10147e1051a39Sopenharmony_ci}
10148e1051a39Sopenharmony_ci
10149e1051a39Sopenharmony_ci
10150e1051a39Sopenharmony_cistatic int test_inherit_verify_param(void)
10151e1051a39Sopenharmony_ci{
10152e1051a39Sopenharmony_ci    int testresult = 0;
10153e1051a39Sopenharmony_ci
10154e1051a39Sopenharmony_ci    SSL_CTX *ctx = NULL;
10155e1051a39Sopenharmony_ci    X509_VERIFY_PARAM *cp = NULL;
10156e1051a39Sopenharmony_ci    SSL *ssl = NULL;
10157e1051a39Sopenharmony_ci    X509_VERIFY_PARAM *sp = NULL;
10158e1051a39Sopenharmony_ci    int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
10159e1051a39Sopenharmony_ci
10160e1051a39Sopenharmony_ci    ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
10161e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx))
10162e1051a39Sopenharmony_ci        goto end;
10163e1051a39Sopenharmony_ci
10164e1051a39Sopenharmony_ci    cp = SSL_CTX_get0_param(ctx);
10165e1051a39Sopenharmony_ci    if (!TEST_ptr(cp))
10166e1051a39Sopenharmony_ci        goto end;
10167e1051a39Sopenharmony_ci    if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
10168e1051a39Sopenharmony_ci        goto end;
10169e1051a39Sopenharmony_ci
10170e1051a39Sopenharmony_ci    X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
10171e1051a39Sopenharmony_ci
10172e1051a39Sopenharmony_ci    ssl = SSL_new(ctx);
10173e1051a39Sopenharmony_ci    if (!TEST_ptr(ssl))
10174e1051a39Sopenharmony_ci        goto end;
10175e1051a39Sopenharmony_ci
10176e1051a39Sopenharmony_ci    sp = SSL_get0_param(ssl);
10177e1051a39Sopenharmony_ci    if (!TEST_ptr(sp))
10178e1051a39Sopenharmony_ci        goto end;
10179e1051a39Sopenharmony_ci    if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
10180e1051a39Sopenharmony_ci        goto end;
10181e1051a39Sopenharmony_ci
10182e1051a39Sopenharmony_ci    testresult = 1;
10183e1051a39Sopenharmony_ci
10184e1051a39Sopenharmony_ci end:
10185e1051a39Sopenharmony_ci    SSL_free(ssl);
10186e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
10187e1051a39Sopenharmony_ci
10188e1051a39Sopenharmony_ci    return testresult;
10189e1051a39Sopenharmony_ci}
10190e1051a39Sopenharmony_ci
10191e1051a39Sopenharmony_cistatic int test_load_dhfile(void)
10192e1051a39Sopenharmony_ci{
10193e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH
10194e1051a39Sopenharmony_ci    int testresult = 0;
10195e1051a39Sopenharmony_ci
10196e1051a39Sopenharmony_ci    SSL_CTX *ctx = NULL;
10197e1051a39Sopenharmony_ci    SSL_CONF_CTX *cctx = NULL;
10198e1051a39Sopenharmony_ci
10199e1051a39Sopenharmony_ci    if (dhfile == NULL)
10200e1051a39Sopenharmony_ci        return 1;
10201e1051a39Sopenharmony_ci
10202e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
10203e1051a39Sopenharmony_ci        || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
10204e1051a39Sopenharmony_ci        goto end;
10205e1051a39Sopenharmony_ci
10206e1051a39Sopenharmony_ci    SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
10207e1051a39Sopenharmony_ci    SSL_CONF_CTX_set_flags(cctx,
10208e1051a39Sopenharmony_ci                           SSL_CONF_FLAG_CERTIFICATE
10209e1051a39Sopenharmony_ci                           | SSL_CONF_FLAG_SERVER
10210e1051a39Sopenharmony_ci                           | SSL_CONF_FLAG_FILE);
10211e1051a39Sopenharmony_ci
10212e1051a39Sopenharmony_ci    if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
10213e1051a39Sopenharmony_ci        goto end;
10214e1051a39Sopenharmony_ci
10215e1051a39Sopenharmony_ci    testresult = 1;
10216e1051a39Sopenharmony_ciend:
10217e1051a39Sopenharmony_ci    SSL_CONF_CTX_free(cctx);
10218e1051a39Sopenharmony_ci    SSL_CTX_free(ctx);
10219e1051a39Sopenharmony_ci
10220e1051a39Sopenharmony_ci    return testresult;
10221e1051a39Sopenharmony_ci#else
10222e1051a39Sopenharmony_ci    return TEST_skip("DH not supported by this build");
10223e1051a39Sopenharmony_ci#endif
10224e1051a39Sopenharmony_ci}
10225e1051a39Sopenharmony_ci
10226e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
10227e1051a39Sopenharmony_ci/*
10228e1051a39Sopenharmony_ci * Test TLSv1.2 with a pipeline capable cipher. TLSv1.3 and DTLS do not
10229e1051a39Sopenharmony_ci * support this yet. The only pipeline capable cipher that we have is in the
10230e1051a39Sopenharmony_ci * dasync engine (providers don't support this yet), so we have to use
10231e1051a39Sopenharmony_ci * deprecated APIs for this test.
10232e1051a39Sopenharmony_ci *
10233e1051a39Sopenharmony_ci * Test 0: Client has pipelining enabled, server does not
10234e1051a39Sopenharmony_ci * Test 1: Server has pipelining enabled, client does not
10235e1051a39Sopenharmony_ci * Test 2: Client has pipelining enabled, server does not: not enough data to
10236e1051a39Sopenharmony_ci *         fill all the pipelines
10237e1051a39Sopenharmony_ci * Test 3: Client has pipelining enabled, server does not: not enough data to
10238e1051a39Sopenharmony_ci *         fill all the pipelines by more than a full pipeline's worth
10239e1051a39Sopenharmony_ci * Test 4: Client has pipelining enabled, server does not: more data than all
10240e1051a39Sopenharmony_ci *         the available pipelines can take
10241e1051a39Sopenharmony_ci * Test 5: Client has pipelining enabled, server does not: Maximum size pipeline
10242e1051a39Sopenharmony_ci */
10243e1051a39Sopenharmony_cistatic int test_pipelining(int idx)
10244e1051a39Sopenharmony_ci{
10245e1051a39Sopenharmony_ci    SSL_CTX *cctx = NULL, *sctx = NULL;
10246e1051a39Sopenharmony_ci    SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb;
10247e1051a39Sopenharmony_ci    int testresult = 0, numreads;
10248e1051a39Sopenharmony_ci    /* A 55 byte message */
10249e1051a39Sopenharmony_ci    unsigned char *msg = (unsigned char *)
10250e1051a39Sopenharmony_ci        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123";
10251e1051a39Sopenharmony_ci    size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5;
10252e1051a39Sopenharmony_ci    size_t expectedreads;
10253e1051a39Sopenharmony_ci    unsigned char *buf = NULL;
10254e1051a39Sopenharmony_ci    ENGINE *e;
10255e1051a39Sopenharmony_ci
10256e1051a39Sopenharmony_ci    if (!TEST_ptr(e = ENGINE_by_id("dasync")))
10257e1051a39Sopenharmony_ci        return 0;
10258e1051a39Sopenharmony_ci
10259e1051a39Sopenharmony_ci    if (!TEST_true(ENGINE_init(e))) {
10260e1051a39Sopenharmony_ci        ENGINE_free(e);
10261e1051a39Sopenharmony_ci        return 0;
10262e1051a39Sopenharmony_ci    }
10263e1051a39Sopenharmony_ci
10264e1051a39Sopenharmony_ci    if (!TEST_true(ENGINE_register_ciphers(e)))
10265e1051a39Sopenharmony_ci        goto end;
10266e1051a39Sopenharmony_ci
10267e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10268e1051a39Sopenharmony_ci                                       TLS_client_method(), 0,
10269e1051a39Sopenharmony_ci                                       TLS1_2_VERSION, &sctx, &cctx, cert,
10270e1051a39Sopenharmony_ci                                       privkey)))
10271e1051a39Sopenharmony_ci        goto end;
10272e1051a39Sopenharmony_ci
10273e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
10274e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL)))
10275e1051a39Sopenharmony_ci        goto end;
10276e1051a39Sopenharmony_ci
10277e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA")))
10278e1051a39Sopenharmony_ci        goto end;
10279e1051a39Sopenharmony_ci
10280e1051a39Sopenharmony_ci    /* peera is always configured for pipelining, while peerb is not. */
10281e1051a39Sopenharmony_ci    if (idx == 1) {
10282e1051a39Sopenharmony_ci        peera = serverssl;
10283e1051a39Sopenharmony_ci        peerb = clientssl;
10284e1051a39Sopenharmony_ci
10285e1051a39Sopenharmony_ci    } else {
10286e1051a39Sopenharmony_ci        peera = clientssl;
10287e1051a39Sopenharmony_ci        peerb = serverssl;
10288e1051a39Sopenharmony_ci    }
10289e1051a39Sopenharmony_ci
10290e1051a39Sopenharmony_ci    if (idx == 5) {
10291e1051a39Sopenharmony_ci        numpipes = 2;
10292e1051a39Sopenharmony_ci        /* Maximum allowed fragment size */
10293e1051a39Sopenharmony_ci        fragsize = SSL3_RT_MAX_PLAIN_LENGTH;
10294e1051a39Sopenharmony_ci        msglen = fragsize * numpipes;
10295e1051a39Sopenharmony_ci        msg = OPENSSL_malloc(msglen);
10296e1051a39Sopenharmony_ci        if (!TEST_ptr(msg))
10297e1051a39Sopenharmony_ci            goto end;
10298e1051a39Sopenharmony_ci        if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0))
10299e1051a39Sopenharmony_ci            goto end;
10300e1051a39Sopenharmony_ci    } else if (idx == 4) {
10301e1051a39Sopenharmony_ci        msglen = 55;
10302e1051a39Sopenharmony_ci    } else {
10303e1051a39Sopenharmony_ci        msglen = 50;
10304e1051a39Sopenharmony_ci    }
10305e1051a39Sopenharmony_ci    if (idx == 2)
10306e1051a39Sopenharmony_ci        msglen -= 2; /* Send 2 less bytes */
10307e1051a39Sopenharmony_ci    else if (idx == 3)
10308e1051a39Sopenharmony_ci        msglen -= 12; /* Send 12 less bytes */
10309e1051a39Sopenharmony_ci
10310e1051a39Sopenharmony_ci    buf = OPENSSL_malloc(msglen);
10311e1051a39Sopenharmony_ci    if (!TEST_ptr(buf))
10312e1051a39Sopenharmony_ci        goto end;
10313e1051a39Sopenharmony_ci
10314e1051a39Sopenharmony_ci    if (idx == 5) {
10315e1051a39Sopenharmony_ci        /*
10316e1051a39Sopenharmony_ci         * Test that setting a split send fragment longer than the maximum
10317e1051a39Sopenharmony_ci         * allowed fails
10318e1051a39Sopenharmony_ci         */
10319e1051a39Sopenharmony_ci        if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1)))
10320e1051a39Sopenharmony_ci            goto end;
10321e1051a39Sopenharmony_ci    }
10322e1051a39Sopenharmony_ci
10323e1051a39Sopenharmony_ci    /*
10324e1051a39Sopenharmony_ci     * In the normal case. We have 5 pipelines with 10 bytes per pipeline
10325e1051a39Sopenharmony_ci     * (50 bytes in total). This is a ridiculously small number of bytes -
10326e1051a39Sopenharmony_ci     * but sufficient for our purposes
10327e1051a39Sopenharmony_ci     */
10328e1051a39Sopenharmony_ci    if (!TEST_true(SSL_set_max_pipelines(peera, numpipes))
10329e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_split_send_fragment(peera, fragsize)))
10330e1051a39Sopenharmony_ci        goto end;
10331e1051a39Sopenharmony_ci
10332e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10333e1051a39Sopenharmony_ci        goto end;
10334e1051a39Sopenharmony_ci
10335e1051a39Sopenharmony_ci    /* Write some data from peera to peerb */
10336e1051a39Sopenharmony_ci    if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written))
10337e1051a39Sopenharmony_ci        || !TEST_size_t_eq(written, msglen))
10338e1051a39Sopenharmony_ci        goto end;
10339e1051a39Sopenharmony_ci
10340e1051a39Sopenharmony_ci    /*
10341e1051a39Sopenharmony_ci     * If the pipelining code worked, then we expect all |numpipes| pipelines to
10342e1051a39Sopenharmony_ci     * have been used - except in test 3 where only |numpipes - 1| pipelines
10343e1051a39Sopenharmony_ci     * will be used. This will result in |numpipes| records (|numpipes - 1| for
10344e1051a39Sopenharmony_ci     * test 3) having been sent to peerb. Since peerb is not using read_ahead we
10345e1051a39Sopenharmony_ci     * expect this to be read in |numpipes| or |numpipes - 1| separate
10346e1051a39Sopenharmony_ci     * SSL_read_ex calls. In the case of test 4, there is then one additional
10347e1051a39Sopenharmony_ci     * read for left over data that couldn't fit in the previous pipelines
10348e1051a39Sopenharmony_ci     */
10349e1051a39Sopenharmony_ci    for (offset = 0, numreads = 0;
10350e1051a39Sopenharmony_ci         offset < msglen;
10351e1051a39Sopenharmony_ci         offset += readbytes, numreads++) {
10352e1051a39Sopenharmony_ci        if (!TEST_true(SSL_read_ex(peerb, buf + offset,
10353e1051a39Sopenharmony_ci                                   msglen - offset, &readbytes)))
10354e1051a39Sopenharmony_ci            goto end;
10355e1051a39Sopenharmony_ci    }
10356e1051a39Sopenharmony_ci
10357e1051a39Sopenharmony_ci    expectedreads = idx == 4 ? numpipes + 1
10358e1051a39Sopenharmony_ci                             : (idx == 3 ? numpipes - 1 : numpipes);
10359e1051a39Sopenharmony_ci    if (!TEST_mem_eq(msg, msglen, buf, offset)
10360e1051a39Sopenharmony_ci            || !TEST_int_eq(numreads, expectedreads))
10361e1051a39Sopenharmony_ci        goto end;
10362e1051a39Sopenharmony_ci
10363e1051a39Sopenharmony_ci    /*
10364e1051a39Sopenharmony_ci     * Write some data from peerb to peera. We do this in up to |numpipes + 1|
10365e1051a39Sopenharmony_ci     * chunks to exercise the read pipelining code on peera.
10366e1051a39Sopenharmony_ci     */
10367e1051a39Sopenharmony_ci    for (offset = 0; offset < msglen; offset += fragsize) {
10368e1051a39Sopenharmony_ci        size_t sendlen = msglen - offset;
10369e1051a39Sopenharmony_ci
10370e1051a39Sopenharmony_ci        if (sendlen > fragsize)
10371e1051a39Sopenharmony_ci            sendlen = fragsize;
10372e1051a39Sopenharmony_ci        if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written))
10373e1051a39Sopenharmony_ci                || !TEST_size_t_eq(written, sendlen))
10374e1051a39Sopenharmony_ci            goto end;
10375e1051a39Sopenharmony_ci    }
10376e1051a39Sopenharmony_ci
10377e1051a39Sopenharmony_ci    /*
10378e1051a39Sopenharmony_ci     * The data was written in |numpipes|, |numpipes - 1| or |numpipes + 1|
10379e1051a39Sopenharmony_ci     * separate chunks (depending on which test we are running). If the
10380e1051a39Sopenharmony_ci     * pipelining is working then we expect peera to read up to numpipes chunks
10381e1051a39Sopenharmony_ci     * and process them in parallel, giving back the complete result in a single
10382e1051a39Sopenharmony_ci     * call to SSL_read_ex
10383e1051a39Sopenharmony_ci     */
10384e1051a39Sopenharmony_ci    if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes))
10385e1051a39Sopenharmony_ci            || !TEST_size_t_le(readbytes, msglen))
10386e1051a39Sopenharmony_ci        goto end;
10387e1051a39Sopenharmony_ci
10388e1051a39Sopenharmony_ci    if (idx == 4) {
10389e1051a39Sopenharmony_ci        size_t readbytes2;
10390e1051a39Sopenharmony_ci
10391e1051a39Sopenharmony_ci        if (!TEST_true(SSL_read_ex(peera, buf + readbytes,
10392e1051a39Sopenharmony_ci                                   msglen - readbytes, &readbytes2)))
10393e1051a39Sopenharmony_ci            goto end;
10394e1051a39Sopenharmony_ci        readbytes += readbytes2;
10395e1051a39Sopenharmony_ci        if (!TEST_size_t_le(readbytes, msglen))
10396e1051a39Sopenharmony_ci            goto end;
10397e1051a39Sopenharmony_ci    }
10398e1051a39Sopenharmony_ci
10399e1051a39Sopenharmony_ci    if (!TEST_mem_eq(msg, msglen, buf, readbytes))
10400e1051a39Sopenharmony_ci        goto end;
10401e1051a39Sopenharmony_ci
10402e1051a39Sopenharmony_ci    testresult = 1;
10403e1051a39Sopenharmony_ciend:
10404e1051a39Sopenharmony_ci    SSL_free(serverssl);
10405e1051a39Sopenharmony_ci    SSL_free(clientssl);
10406e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
10407e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
10408e1051a39Sopenharmony_ci    ENGINE_unregister_ciphers(e);
10409e1051a39Sopenharmony_ci    ENGINE_finish(e);
10410e1051a39Sopenharmony_ci    ENGINE_free(e);
10411e1051a39Sopenharmony_ci    OPENSSL_free(buf);
10412e1051a39Sopenharmony_ci    if (idx == 5)
10413e1051a39Sopenharmony_ci        OPENSSL_free(msg);
10414e1051a39Sopenharmony_ci    return testresult;
10415e1051a39Sopenharmony_ci}
10416e1051a39Sopenharmony_ci#endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
10417e1051a39Sopenharmony_ci
10418e1051a39Sopenharmony_cistruct resume_servername_cb_data {
10419e1051a39Sopenharmony_ci    int i;
10420e1051a39Sopenharmony_ci    SSL_CTX *cctx;
10421e1051a39Sopenharmony_ci    SSL_CTX *sctx;
10422e1051a39Sopenharmony_ci    SSL_SESSION *sess;
10423e1051a39Sopenharmony_ci    int recurse;
10424e1051a39Sopenharmony_ci};
10425e1051a39Sopenharmony_ci
10426e1051a39Sopenharmony_ci/*
10427e1051a39Sopenharmony_ci * Servername callback. We use it here to run another complete handshake using
10428e1051a39Sopenharmony_ci * the same session - and mark the session as not_resuamble at the end
10429e1051a39Sopenharmony_ci */
10430e1051a39Sopenharmony_cistatic int resume_servername_cb(SSL *s, int *ad, void *arg)
10431e1051a39Sopenharmony_ci{
10432e1051a39Sopenharmony_ci    struct resume_servername_cb_data *cbdata = arg;
10433e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
10434e1051a39Sopenharmony_ci    int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
10435e1051a39Sopenharmony_ci
10436e1051a39Sopenharmony_ci    if (cbdata->recurse)
10437e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_ALERT_FATAL;
10438e1051a39Sopenharmony_ci
10439e1051a39Sopenharmony_ci    if ((cbdata->i % 3) != 1)
10440e1051a39Sopenharmony_ci        return SSL_TLSEXT_ERR_OK;
10441e1051a39Sopenharmony_ci
10442e1051a39Sopenharmony_ci    cbdata->recurse = 1;
10443e1051a39Sopenharmony_ci
10444e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_objects(cbdata->sctx, cbdata->cctx, &serverssl,
10445e1051a39Sopenharmony_ci                                      &clientssl, NULL, NULL))
10446e1051a39Sopenharmony_ci            || !TEST_true(SSL_set_session(clientssl, cbdata->sess)))
10447e1051a39Sopenharmony_ci        goto end;
10448e1051a39Sopenharmony_ci
10449e1051a39Sopenharmony_ci    ERR_set_mark();
10450e1051a39Sopenharmony_ci    /*
10451e1051a39Sopenharmony_ci     * We expect this to fail - because the servername cb will fail. This will
10452e1051a39Sopenharmony_ci     * mark the session as not_resumable.
10453e1051a39Sopenharmony_ci     */
10454e1051a39Sopenharmony_ci    if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
10455e1051a39Sopenharmony_ci        ERR_clear_last_mark();
10456e1051a39Sopenharmony_ci        goto end;
10457e1051a39Sopenharmony_ci    }
10458e1051a39Sopenharmony_ci    ERR_pop_to_mark();
10459e1051a39Sopenharmony_ci
10460e1051a39Sopenharmony_ci    ret = SSL_TLSEXT_ERR_OK;
10461e1051a39Sopenharmony_ci end:
10462e1051a39Sopenharmony_ci    SSL_free(serverssl);
10463e1051a39Sopenharmony_ci    SSL_free(clientssl);
10464e1051a39Sopenharmony_ci    cbdata->recurse = 0;
10465e1051a39Sopenharmony_ci    return ret;
10466e1051a39Sopenharmony_ci}
10467e1051a39Sopenharmony_ci
10468e1051a39Sopenharmony_ci/*
10469e1051a39Sopenharmony_ci * Test multiple resumptions and cache size handling
10470e1051a39Sopenharmony_ci * Test 0: TLSv1.3 (max_early_data set)
10471e1051a39Sopenharmony_ci * Test 1: TLSv1.3 (SSL_OP_NO_TICKET set)
10472e1051a39Sopenharmony_ci * Test 2: TLSv1.3 (max_early_data and SSL_OP_NO_TICKET set)
10473e1051a39Sopenharmony_ci * Test 3: TLSv1.3 (SSL_OP_NO_TICKET, simultaneous resumes)
10474e1051a39Sopenharmony_ci * Test 4: TLSv1.2
10475e1051a39Sopenharmony_ci */
10476e1051a39Sopenharmony_cistatic int test_multi_resume(int idx)
10477e1051a39Sopenharmony_ci{
10478e1051a39Sopenharmony_ci    SSL_CTX *sctx = NULL, *cctx = NULL;
10479e1051a39Sopenharmony_ci    SSL *serverssl = NULL, *clientssl = NULL;
10480e1051a39Sopenharmony_ci    SSL_SESSION *sess = NULL;
10481e1051a39Sopenharmony_ci    int max_version = TLS1_3_VERSION;
10482e1051a39Sopenharmony_ci    int i, testresult = 0;
10483e1051a39Sopenharmony_ci    struct resume_servername_cb_data cbdata;
10484e1051a39Sopenharmony_ci
10485e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_TLS1_2)
10486e1051a39Sopenharmony_ci    if (idx == 4)
10487e1051a39Sopenharmony_ci        return TEST_skip("TLSv1.2 is disabled in this build");
10488e1051a39Sopenharmony_ci#else
10489e1051a39Sopenharmony_ci    if (idx == 4)
10490e1051a39Sopenharmony_ci        max_version = TLS1_2_VERSION;
10491e1051a39Sopenharmony_ci#endif
10492e1051a39Sopenharmony_ci#if defined(OSSL_NO_USABLE_TLS1_3)
10493e1051a39Sopenharmony_ci    if (idx != 4)
10494e1051a39Sopenharmony_ci        return TEST_skip("No usable TLSv1.3 in this build");
10495e1051a39Sopenharmony_ci#endif
10496e1051a39Sopenharmony_ci
10497e1051a39Sopenharmony_ci    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
10498e1051a39Sopenharmony_ci                                       TLS_client_method(), TLS1_VERSION,
10499e1051a39Sopenharmony_ci                                       max_version, &sctx, &cctx, cert,
10500e1051a39Sopenharmony_ci                                       privkey)))
10501e1051a39Sopenharmony_ci        goto end;
10502e1051a39Sopenharmony_ci
10503e1051a39Sopenharmony_ci    /*
10504e1051a39Sopenharmony_ci     * TLSv1.3 only uses a session cache if either max_early_data > 0 (used for
10505e1051a39Sopenharmony_ci     * replay protection), or if SSL_OP_NO_TICKET is in use
10506e1051a39Sopenharmony_ci     */
10507e1051a39Sopenharmony_ci    if (idx == 0 || idx == 2)  {
10508e1051a39Sopenharmony_ci        if (!TEST_true(SSL_CTX_set_max_early_data(sctx, 1024)))
10509e1051a39Sopenharmony_ci            goto end;
10510e1051a39Sopenharmony_ci    }
10511e1051a39Sopenharmony_ci    if (idx == 1 || idx == 2 || idx == 3)
10512e1051a39Sopenharmony_ci        SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
10513e1051a39Sopenharmony_ci
10514e1051a39Sopenharmony_ci    SSL_CTX_sess_set_cache_size(sctx, 5);
10515e1051a39Sopenharmony_ci
10516e1051a39Sopenharmony_ci    if (idx == 3) {
10517e1051a39Sopenharmony_ci        SSL_CTX_set_tlsext_servername_callback(sctx, resume_servername_cb);
10518e1051a39Sopenharmony_ci        SSL_CTX_set_tlsext_servername_arg(sctx, &cbdata);
10519e1051a39Sopenharmony_ci        cbdata.cctx = cctx;
10520e1051a39Sopenharmony_ci        cbdata.sctx = sctx;
10521e1051a39Sopenharmony_ci        cbdata.recurse = 0;
10522e1051a39Sopenharmony_ci    }
10523e1051a39Sopenharmony_ci
10524e1051a39Sopenharmony_ci    for (i = 0; i < 30; i++) {
10525e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
10526e1051a39Sopenharmony_ci                                                NULL, NULL))
10527e1051a39Sopenharmony_ci                || !TEST_true(SSL_set_session(clientssl, sess)))
10528e1051a39Sopenharmony_ci            goto end;
10529e1051a39Sopenharmony_ci
10530e1051a39Sopenharmony_ci        /*
10531e1051a39Sopenharmony_ci         * Check simultaneous resumes. We pause the connection part way through
10532e1051a39Sopenharmony_ci         * the handshake by (mis)using the servername_cb. The pause occurs after
10533e1051a39Sopenharmony_ci         * session resumption has already occurred, but before any session
10534e1051a39Sopenharmony_ci         * tickets have been issued. While paused we run another complete
10535e1051a39Sopenharmony_ci         * handshake resuming the same session.
10536e1051a39Sopenharmony_ci         */
10537e1051a39Sopenharmony_ci        if (idx == 3) {
10538e1051a39Sopenharmony_ci            cbdata.i = i;
10539e1051a39Sopenharmony_ci            cbdata.sess = sess;
10540e1051a39Sopenharmony_ci        }
10541e1051a39Sopenharmony_ci
10542e1051a39Sopenharmony_ci        /*
10543e1051a39Sopenharmony_ci         * Recreate a bug where dynamically changing the max_early_data value
10544e1051a39Sopenharmony_ci         * can cause sessions in the session cache which cannot be deleted.
10545e1051a39Sopenharmony_ci         */
10546e1051a39Sopenharmony_ci        if ((idx == 0 || idx == 2) && (i % 3) == 2)
10547e1051a39Sopenharmony_ci            SSL_set_max_early_data(serverssl, 0);
10548e1051a39Sopenharmony_ci
10549e1051a39Sopenharmony_ci        if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
10550e1051a39Sopenharmony_ci            goto end;
10551e1051a39Sopenharmony_ci
10552e1051a39Sopenharmony_ci        if (sess == NULL || (idx == 0 && (i % 3) == 2)) {
10553e1051a39Sopenharmony_ci            if (!TEST_false(SSL_session_reused(clientssl)))
10554e1051a39Sopenharmony_ci                goto end;
10555e1051a39Sopenharmony_ci        } else {
10556e1051a39Sopenharmony_ci            if (!TEST_true(SSL_session_reused(clientssl)))
10557e1051a39Sopenharmony_ci                goto end;
10558e1051a39Sopenharmony_ci        }
10559e1051a39Sopenharmony_ci        SSL_SESSION_free(sess);
10560e1051a39Sopenharmony_ci
10561e1051a39Sopenharmony_ci        /* Do a full handshake, followed by two resumptions */
10562e1051a39Sopenharmony_ci        if ((i % 3) == 2) {
10563e1051a39Sopenharmony_ci            sess = NULL;
10564e1051a39Sopenharmony_ci        } else {
10565e1051a39Sopenharmony_ci            if (!TEST_ptr((sess = SSL_get1_session(clientssl))))
10566e1051a39Sopenharmony_ci                goto end;
10567e1051a39Sopenharmony_ci        }
10568e1051a39Sopenharmony_ci
10569e1051a39Sopenharmony_ci        SSL_shutdown(clientssl);
10570e1051a39Sopenharmony_ci        SSL_shutdown(serverssl);
10571e1051a39Sopenharmony_ci        SSL_free(serverssl);
10572e1051a39Sopenharmony_ci        SSL_free(clientssl);
10573e1051a39Sopenharmony_ci        serverssl = clientssl = NULL;
10574e1051a39Sopenharmony_ci    }
10575e1051a39Sopenharmony_ci
10576e1051a39Sopenharmony_ci    /* We should never exceed the session cache size limit */
10577e1051a39Sopenharmony_ci    if (!TEST_long_le(SSL_CTX_sess_number(sctx), 5))
10578e1051a39Sopenharmony_ci        goto end;
10579e1051a39Sopenharmony_ci
10580e1051a39Sopenharmony_ci    testresult = 1;
10581e1051a39Sopenharmony_ci end:
10582e1051a39Sopenharmony_ci    SSL_free(serverssl);
10583e1051a39Sopenharmony_ci    SSL_free(clientssl);
10584e1051a39Sopenharmony_ci    SSL_CTX_free(sctx);
10585e1051a39Sopenharmony_ci    SSL_CTX_free(cctx);
10586e1051a39Sopenharmony_ci    SSL_SESSION_free(sess);
10587e1051a39Sopenharmony_ci    return testresult;
10588e1051a39Sopenharmony_ci}
10589e1051a39Sopenharmony_ci
10590e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
10591e1051a39Sopenharmony_ci
10592e1051a39Sopenharmony_ciint setup_tests(void)
10593e1051a39Sopenharmony_ci{
10594e1051a39Sopenharmony_ci    char *modulename;
10595e1051a39Sopenharmony_ci    char *configfile;
10596e1051a39Sopenharmony_ci
10597e1051a39Sopenharmony_ci    libctx = OSSL_LIB_CTX_new();
10598e1051a39Sopenharmony_ci    if (!TEST_ptr(libctx))
10599e1051a39Sopenharmony_ci        return 0;
10600e1051a39Sopenharmony_ci
10601e1051a39Sopenharmony_ci    defctxnull = OSSL_PROVIDER_load(NULL, "null");
10602e1051a39Sopenharmony_ci
10603e1051a39Sopenharmony_ci    /*
10604e1051a39Sopenharmony_ci     * Verify that the default and fips providers in the default libctx are not
10605e1051a39Sopenharmony_ci     * available
10606e1051a39Sopenharmony_ci     */
10607e1051a39Sopenharmony_ci    if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
10608e1051a39Sopenharmony_ci            || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
10609e1051a39Sopenharmony_ci        return 0;
10610e1051a39Sopenharmony_ci
10611e1051a39Sopenharmony_ci    if (!test_skip_common_options()) {
10612e1051a39Sopenharmony_ci        TEST_error("Error parsing test options\n");
10613e1051a39Sopenharmony_ci        return 0;
10614e1051a39Sopenharmony_ci    }
10615e1051a39Sopenharmony_ci
10616e1051a39Sopenharmony_ci    if (!TEST_ptr(certsdir = test_get_argument(0))
10617e1051a39Sopenharmony_ci            || !TEST_ptr(srpvfile = test_get_argument(1))
10618e1051a39Sopenharmony_ci            || !TEST_ptr(tmpfilename = test_get_argument(2))
10619e1051a39Sopenharmony_ci            || !TEST_ptr(modulename = test_get_argument(3))
10620e1051a39Sopenharmony_ci            || !TEST_ptr(configfile = test_get_argument(4))
10621e1051a39Sopenharmony_ci            || !TEST_ptr(dhfile = test_get_argument(5)))
10622e1051a39Sopenharmony_ci        return 0;
10623e1051a39Sopenharmony_ci
10624e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
10625e1051a39Sopenharmony_ci        return 0;
10626e1051a39Sopenharmony_ci
10627e1051a39Sopenharmony_ci    /* Check we have the expected provider available */
10628e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
10629e1051a39Sopenharmony_ci        return 0;
10630e1051a39Sopenharmony_ci
10631e1051a39Sopenharmony_ci    /* Check the default provider is not available */
10632e1051a39Sopenharmony_ci    if (strcmp(modulename, "default") != 0
10633e1051a39Sopenharmony_ci            && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
10634e1051a39Sopenharmony_ci        return 0;
10635e1051a39Sopenharmony_ci
10636e1051a39Sopenharmony_ci    if (strcmp(modulename, "fips") == 0)
10637e1051a39Sopenharmony_ci        is_fips = 1;
10638e1051a39Sopenharmony_ci
10639e1051a39Sopenharmony_ci    /*
10640e1051a39Sopenharmony_ci     * We add, but don't load the test "tls-provider". We'll load it when we
10641e1051a39Sopenharmony_ci     * need it.
10642e1051a39Sopenharmony_ci     */
10643e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
10644e1051a39Sopenharmony_ci                                             tls_provider_init)))
10645e1051a39Sopenharmony_ci        return 0;
10646e1051a39Sopenharmony_ci
10647e1051a39Sopenharmony_ci
10648e1051a39Sopenharmony_ci    if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
10649e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_CRYPTO_MDEBUG
10650e1051a39Sopenharmony_ci        TEST_error("not supported in this build");
10651e1051a39Sopenharmony_ci        return 0;
10652e1051a39Sopenharmony_ci#else
10653e1051a39Sopenharmony_ci        int i, mcount, rcount, fcount;
10654e1051a39Sopenharmony_ci
10655e1051a39Sopenharmony_ci        for (i = 0; i < 4; i++)
10656e1051a39Sopenharmony_ci            test_export_key_mat(i);
10657e1051a39Sopenharmony_ci        CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
10658e1051a39Sopenharmony_ci        test_printf_stdout("malloc %d realloc %d free %d\n",
10659e1051a39Sopenharmony_ci                mcount, rcount, fcount);
10660e1051a39Sopenharmony_ci        return 1;
10661e1051a39Sopenharmony_ci#endif
10662e1051a39Sopenharmony_ci    }
10663e1051a39Sopenharmony_ci
10664e1051a39Sopenharmony_ci    cert = test_mk_file_path(certsdir, "servercert.pem");
10665e1051a39Sopenharmony_ci    if (cert == NULL)
10666e1051a39Sopenharmony_ci        goto err;
10667e1051a39Sopenharmony_ci
10668e1051a39Sopenharmony_ci    privkey = test_mk_file_path(certsdir, "serverkey.pem");
10669e1051a39Sopenharmony_ci    if (privkey == NULL)
10670e1051a39Sopenharmony_ci        goto err;
10671e1051a39Sopenharmony_ci
10672e1051a39Sopenharmony_ci    cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
10673e1051a39Sopenharmony_ci    if (cert2 == NULL)
10674e1051a39Sopenharmony_ci        goto err;
10675e1051a39Sopenharmony_ci
10676e1051a39Sopenharmony_ci    privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
10677e1051a39Sopenharmony_ci    if (privkey2 == NULL)
10678e1051a39Sopenharmony_ci        goto err;
10679e1051a39Sopenharmony_ci
10680e1051a39Sopenharmony_ci    cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
10681e1051a39Sopenharmony_ci    if (cert1024 == NULL)
10682e1051a39Sopenharmony_ci        goto err;
10683e1051a39Sopenharmony_ci
10684e1051a39Sopenharmony_ci    privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
10685e1051a39Sopenharmony_ci    if (privkey1024 == NULL)
10686e1051a39Sopenharmony_ci        goto err;
10687e1051a39Sopenharmony_ci
10688e1051a39Sopenharmony_ci    cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
10689e1051a39Sopenharmony_ci    if (cert3072 == NULL)
10690e1051a39Sopenharmony_ci        goto err;
10691e1051a39Sopenharmony_ci
10692e1051a39Sopenharmony_ci    privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
10693e1051a39Sopenharmony_ci    if (privkey3072 == NULL)
10694e1051a39Sopenharmony_ci        goto err;
10695e1051a39Sopenharmony_ci
10696e1051a39Sopenharmony_ci    cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
10697e1051a39Sopenharmony_ci    if (cert4096 == NULL)
10698e1051a39Sopenharmony_ci        goto err;
10699e1051a39Sopenharmony_ci
10700e1051a39Sopenharmony_ci    privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
10701e1051a39Sopenharmony_ci    if (privkey4096 == NULL)
10702e1051a39Sopenharmony_ci        goto err;
10703e1051a39Sopenharmony_ci
10704e1051a39Sopenharmony_ci    cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
10705e1051a39Sopenharmony_ci    if (cert8192 == NULL)
10706e1051a39Sopenharmony_ci        goto err;
10707e1051a39Sopenharmony_ci
10708e1051a39Sopenharmony_ci    privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
10709e1051a39Sopenharmony_ci    if (privkey8192 == NULL)
10710e1051a39Sopenharmony_ci        goto err;
10711e1051a39Sopenharmony_ci
10712e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
10713e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
10714e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
10715e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
10716e1051a39Sopenharmony_ci# endif
10717e1051a39Sopenharmony_ci#endif
10718e1051a39Sopenharmony_ci    ADD_TEST(test_large_message_tls);
10719e1051a39Sopenharmony_ci    ADD_TEST(test_large_message_tls_read_ahead);
10720e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DTLS
10721e1051a39Sopenharmony_ci    ADD_TEST(test_large_message_dtls);
10722e1051a39Sopenharmony_ci#endif
10723e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_large_app_data, 28);
10724e1051a39Sopenharmony_ci    ADD_TEST(test_cleanse_plaintext);
10725e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCSP
10726e1051a39Sopenharmony_ci    ADD_TEST(test_tlsext_status_type);
10727e1051a39Sopenharmony_ci#endif
10728e1051a39Sopenharmony_ci    ADD_TEST(test_session_with_only_int_cache);
10729e1051a39Sopenharmony_ci    ADD_TEST(test_session_with_only_ext_cache);
10730e1051a39Sopenharmony_ci    ADD_TEST(test_session_with_both_cache);
10731e1051a39Sopenharmony_ci    ADD_TEST(test_session_wo_ca_names);
10732e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10733e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_stateful_tickets, 3);
10734e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_stateless_tickets, 3);
10735e1051a39Sopenharmony_ci    ADD_TEST(test_psk_tickets);
10736e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_extra_tickets, 6);
10737e1051a39Sopenharmony_ci#endif
10738e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
10739e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_bio_pop_next_bio);
10740e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_bio_pop_ssl_bio);
10741e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_bio_change_rbio);
10742e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_bio_change_wbio);
10743e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
10744e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
10745e1051a39Sopenharmony_ci    ADD_TEST(test_keylog);
10746e1051a39Sopenharmony_ci#endif
10747e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10748e1051a39Sopenharmony_ci    ADD_TEST(test_keylog_no_master_key);
10749e1051a39Sopenharmony_ci#endif
10750e1051a39Sopenharmony_ci    ADD_TEST(test_client_cert_verify_cb);
10751e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_build_cert_chain);
10752e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_ctx_build_cert_chain);
10753e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
10754e1051a39Sopenharmony_ci    ADD_TEST(test_client_hello_cb);
10755e1051a39Sopenharmony_ci    ADD_TEST(test_no_ems);
10756e1051a39Sopenharmony_ci    ADD_TEST(test_ccs_change_cipher);
10757e1051a39Sopenharmony_ci#endif
10758e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10759e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_read_write, 6);
10760e1051a39Sopenharmony_ci    /*
10761e1051a39Sopenharmony_ci     * We don't do replay tests for external PSK. Replay protection isn't used
10762e1051a39Sopenharmony_ci     * in that scenario.
10763e1051a39Sopenharmony_ci     */
10764e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_replay, 2);
10765e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3);
10766e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3);
10767e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3);
10768e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3);
10769e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_not_sent, 3);
10770e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_psk, 8);
10771e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
10772e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_not_expected, 3);
10773e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
10774e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_early_data_tls1_2, 3);
10775e1051a39Sopenharmony_ci# endif
10776e1051a39Sopenharmony_ci#endif
10777e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10778e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_set_ciphersuite, 10);
10779e1051a39Sopenharmony_ci    ADD_TEST(test_ciphersuite_change);
10780e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
10781e1051a39Sopenharmony_ci# ifdef OPENSSL_NO_PSK
10782e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_tls13_psk, 1);
10783e1051a39Sopenharmony_ci# else
10784e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_tls13_psk, 4);
10785e1051a39Sopenharmony_ci# endif  /* OPENSSL_NO_PSK */
10786e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_TLS1_2
10787e1051a39Sopenharmony_ci    /* Test with both TLSv1.3 and 1.2 versions */
10788e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_exchange, 14);
10789e1051a39Sopenharmony_ci#  if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
10790e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_negotiated_group,
10791e1051a39Sopenharmony_ci                  4 * (OSSL_NELEM(ecdhe_kexch_groups)
10792e1051a39Sopenharmony_ci                       + OSSL_NELEM(ffdhe_kexch_groups)));
10793e1051a39Sopenharmony_ci#  endif
10794e1051a39Sopenharmony_ci# else
10795e1051a39Sopenharmony_ci    /* Test with only TLSv1.3 versions */
10796e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_exchange, 12);
10797e1051a39Sopenharmony_ci# endif
10798e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_custom_exts, 6);
10799e1051a39Sopenharmony_ci    ADD_TEST(test_stateless);
10800e1051a39Sopenharmony_ci    ADD_TEST(test_pha_key_update);
10801e1051a39Sopenharmony_ci#else
10802e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_custom_exts, 3);
10803e1051a39Sopenharmony_ci#endif
10804e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_export_key_mat, 6);
10805e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10806e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_export_key_mat_early, 3);
10807e1051a39Sopenharmony_ci    ADD_TEST(test_key_update);
10808e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
10809e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
10810e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_update_local_in_write, 2);
10811e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_key_update_local_in_read, 2);
10812e1051a39Sopenharmony_ci#endif
10813e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ssl_clear, 2);
10814e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
10815e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
10816e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_srp, 6);
10817e1051a39Sopenharmony_ci#endif
10818e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_info_callback, 6);
10819e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ssl_pending, 2);
10820e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
10821e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ticket_callbacks, 20);
10822e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_shutdown, 7);
10823e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_incorrect_shutdown, 2);
10824e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_cert_cb, 6);
10825e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_client_cert_cb, 2);
10826e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ca_names, 3);
10827e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
10828e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
10829e1051a39Sopenharmony_ci#endif
10830e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_servername, 10);
10831e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_EC) \
10832e1051a39Sopenharmony_ci    && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
10833e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_sigalgs_available, 6);
10834e1051a39Sopenharmony_ci#endif
10835e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_3
10836e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_pluggable_group, 2);
10837e1051a39Sopenharmony_ci#endif
10838e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_TLS1_2
10839e1051a39Sopenharmony_ci    ADD_TEST(test_ssl_dup);
10840e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
10841e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_set_tmp_dh, 11);
10842e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_dh_auto, 7);
10843e1051a39Sopenharmony_ci# endif
10844e1051a39Sopenharmony_ci#endif
10845e1051a39Sopenharmony_ci#ifndef OSSL_NO_USABLE_TLS1_3
10846e1051a39Sopenharmony_ci    ADD_TEST(test_sni_tls13);
10847e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ticket_lifetime, 2);
10848e1051a39Sopenharmony_ci#endif
10849e1051a39Sopenharmony_ci    ADD_TEST(test_inherit_verify_param);
10850e1051a39Sopenharmony_ci    ADD_TEST(test_set_alpn);
10851e1051a39Sopenharmony_ci    ADD_TEST(test_set_verify_cert_store_ssl_ctx);
10852e1051a39Sopenharmony_ci    ADD_TEST(test_set_verify_cert_store_ssl);
10853e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_session_timeout, 1);
10854e1051a39Sopenharmony_ci#if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
10855e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_session_cache_overflow, 4);
10856e1051a39Sopenharmony_ci#endif
10857e1051a39Sopenharmony_ci    ADD_TEST(test_load_dhfile);
10858e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
10859e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_serverinfo_custom, 4);
10860e1051a39Sopenharmony_ci#endif
10861e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
10862e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_pipelining, 6);
10863e1051a39Sopenharmony_ci#endif
10864e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_multi_resume, 5);
10865e1051a39Sopenharmony_ci    return 1;
10866e1051a39Sopenharmony_ci
10867e1051a39Sopenharmony_ci err:
10868e1051a39Sopenharmony_ci    OPENSSL_free(cert);
10869e1051a39Sopenharmony_ci    OPENSSL_free(privkey);
10870e1051a39Sopenharmony_ci    OPENSSL_free(cert2);
10871e1051a39Sopenharmony_ci    OPENSSL_free(privkey2);
10872e1051a39Sopenharmony_ci    return 0;
10873e1051a39Sopenharmony_ci}
10874e1051a39Sopenharmony_ci
10875e1051a39Sopenharmony_civoid cleanup_tests(void)
10876e1051a39Sopenharmony_ci{
10877e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
10878e1051a39Sopenharmony_ci    EVP_PKEY_free(tmp_dh_params);
10879e1051a39Sopenharmony_ci#endif
10880e1051a39Sopenharmony_ci    OPENSSL_free(cert);
10881e1051a39Sopenharmony_ci    OPENSSL_free(privkey);
10882e1051a39Sopenharmony_ci    OPENSSL_free(cert2);
10883e1051a39Sopenharmony_ci    OPENSSL_free(privkey2);
10884e1051a39Sopenharmony_ci    OPENSSL_free(cert1024);
10885e1051a39Sopenharmony_ci    OPENSSL_free(privkey1024);
10886e1051a39Sopenharmony_ci    OPENSSL_free(cert3072);
10887e1051a39Sopenharmony_ci    OPENSSL_free(privkey3072);
10888e1051a39Sopenharmony_ci    OPENSSL_free(cert4096);
10889e1051a39Sopenharmony_ci    OPENSSL_free(privkey4096);
10890e1051a39Sopenharmony_ci    OPENSSL_free(cert8192);
10891e1051a39Sopenharmony_ci    OPENSSL_free(privkey8192);
10892e1051a39Sopenharmony_ci    bio_s_mempacket_test_free();
10893e1051a39Sopenharmony_ci    bio_s_always_retry_free();
10894e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(defctxnull);
10895e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(libctx);
10896e1051a39Sopenharmony_ci}
10897