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