1e0c4386eSCy Schubert /* 2*a7148ab3SEnji Cooper * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved. 3e0c4386eSCy Schubert * 4e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use 5e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy 6e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at 7e0c4386eSCy Schubert * https://www.openssl.org/source/license.html 8e0c4386eSCy Schubert */ 9e0c4386eSCy Schubert 10e0c4386eSCy Schubert #include <string.h> 11e0c4386eSCy Schubert 12e0c4386eSCy Schubert #include <openssl/bio.h> 13e0c4386eSCy Schubert #include <openssl/x509_vfy.h> 14e0c4386eSCy Schubert #include <openssl/ssl.h> 15e0c4386eSCy Schubert #include <openssl/core_names.h> 16e0c4386eSCy Schubert 17e0c4386eSCy Schubert #include "../../ssl/ssl_local.h" 18e0c4386eSCy Schubert #include "internal/sockets.h" 19e0c4386eSCy Schubert #include "internal/nelem.h" 20e0c4386eSCy Schubert #include "handshake.h" 21e0c4386eSCy Schubert #include "../testutil.h" 22e0c4386eSCy Schubert 23e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) 24e0c4386eSCy Schubert #include <netinet/sctp.h> 25e0c4386eSCy Schubert #endif 26e0c4386eSCy Schubert 27e0c4386eSCy Schubert HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void) 28e0c4386eSCy Schubert { 29e0c4386eSCy Schubert HANDSHAKE_RESULT *ret; 30e0c4386eSCy Schubert 31e0c4386eSCy Schubert TEST_ptr(ret = OPENSSL_zalloc(sizeof(*ret))); 32e0c4386eSCy Schubert return ret; 33e0c4386eSCy Schubert } 34e0c4386eSCy Schubert 35e0c4386eSCy Schubert void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result) 36e0c4386eSCy Schubert { 37e0c4386eSCy Schubert if (result == NULL) 38e0c4386eSCy Schubert return; 39e0c4386eSCy Schubert OPENSSL_free(result->client_npn_negotiated); 40e0c4386eSCy Schubert OPENSSL_free(result->server_npn_negotiated); 41e0c4386eSCy Schubert OPENSSL_free(result->client_alpn_negotiated); 42e0c4386eSCy Schubert OPENSSL_free(result->server_alpn_negotiated); 43e0c4386eSCy Schubert OPENSSL_free(result->result_session_ticket_app_data); 44e0c4386eSCy Schubert sk_X509_NAME_pop_free(result->server_ca_names, X509_NAME_free); 45e0c4386eSCy Schubert sk_X509_NAME_pop_free(result->client_ca_names, X509_NAME_free); 46e0c4386eSCy Schubert OPENSSL_free(result->cipher); 47e0c4386eSCy Schubert OPENSSL_free(result); 48e0c4386eSCy Schubert } 49e0c4386eSCy Schubert 50e0c4386eSCy Schubert /* 51e0c4386eSCy Schubert * Since there appears to be no way to extract the sent/received alert 52e0c4386eSCy Schubert * from the SSL object directly, we use the info callback and stash 53e0c4386eSCy Schubert * the result in ex_data. 54e0c4386eSCy Schubert */ 55e0c4386eSCy Schubert typedef struct handshake_ex_data_st { 56e0c4386eSCy Schubert int alert_sent; 57e0c4386eSCy Schubert int num_fatal_alerts_sent; 58e0c4386eSCy Schubert int alert_received; 59e0c4386eSCy Schubert int session_ticket_do_not_call; 60e0c4386eSCy Schubert ssl_servername_t servername; 61e0c4386eSCy Schubert } HANDSHAKE_EX_DATA; 62e0c4386eSCy Schubert 63e0c4386eSCy Schubert /* |ctx_data| itself is stack-allocated. */ 64e0c4386eSCy Schubert static void ctx_data_free_data(CTX_DATA *ctx_data) 65e0c4386eSCy Schubert { 66e0c4386eSCy Schubert OPENSSL_free(ctx_data->npn_protocols); 67e0c4386eSCy Schubert ctx_data->npn_protocols = NULL; 68e0c4386eSCy Schubert OPENSSL_free(ctx_data->alpn_protocols); 69e0c4386eSCy Schubert ctx_data->alpn_protocols = NULL; 70e0c4386eSCy Schubert OPENSSL_free(ctx_data->srp_user); 71e0c4386eSCy Schubert ctx_data->srp_user = NULL; 72e0c4386eSCy Schubert OPENSSL_free(ctx_data->srp_password); 73e0c4386eSCy Schubert ctx_data->srp_password = NULL; 74e0c4386eSCy Schubert OPENSSL_free(ctx_data->session_ticket_app_data); 75e0c4386eSCy Schubert ctx_data->session_ticket_app_data = NULL; 76e0c4386eSCy Schubert } 77e0c4386eSCy Schubert 78e0c4386eSCy Schubert static int ex_data_idx; 79e0c4386eSCy Schubert 80e0c4386eSCy Schubert static void info_cb(const SSL *s, int where, int ret) 81e0c4386eSCy Schubert { 82e0c4386eSCy Schubert if (where & SSL_CB_ALERT) { 83e0c4386eSCy Schubert HANDSHAKE_EX_DATA *ex_data = 84e0c4386eSCy Schubert (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); 85e0c4386eSCy Schubert if (where & SSL_CB_WRITE) { 86e0c4386eSCy Schubert ex_data->alert_sent = ret; 87e0c4386eSCy Schubert if (strcmp(SSL_alert_type_string(ret), "F") == 0 88e0c4386eSCy Schubert || strcmp(SSL_alert_desc_string(ret), "CN") == 0) 89e0c4386eSCy Schubert ex_data->num_fatal_alerts_sent++; 90e0c4386eSCy Schubert } else { 91e0c4386eSCy Schubert ex_data->alert_received = ret; 92e0c4386eSCy Schubert } 93e0c4386eSCy Schubert } 94e0c4386eSCy Schubert } 95e0c4386eSCy Schubert 96e0c4386eSCy Schubert /* Select the appropriate server CTX. 97e0c4386eSCy Schubert * Returns SSL_TLSEXT_ERR_OK if a match was found. 98e0c4386eSCy Schubert * If |ignore| is 1, returns SSL_TLSEXT_ERR_NOACK on mismatch. 99e0c4386eSCy Schubert * Otherwise, returns SSL_TLSEXT_ERR_ALERT_FATAL on mismatch. 100e0c4386eSCy Schubert * An empty SNI extension also returns SSL_TSLEXT_ERR_NOACK. 101e0c4386eSCy Schubert */ 102e0c4386eSCy Schubert static int select_server_ctx(SSL *s, void *arg, int ignore) 103e0c4386eSCy Schubert { 104e0c4386eSCy Schubert const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); 105e0c4386eSCy Schubert HANDSHAKE_EX_DATA *ex_data = 106e0c4386eSCy Schubert (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); 107e0c4386eSCy Schubert 108e0c4386eSCy Schubert if (servername == NULL) { 109e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; 110e0c4386eSCy Schubert return SSL_TLSEXT_ERR_NOACK; 111e0c4386eSCy Schubert } 112e0c4386eSCy Schubert 113e0c4386eSCy Schubert if (strcmp(servername, "server2") == 0) { 114e0c4386eSCy Schubert SSL_CTX *new_ctx = (SSL_CTX*)arg; 115e0c4386eSCy Schubert SSL_set_SSL_CTX(s, new_ctx); 116e0c4386eSCy Schubert /* 117e0c4386eSCy Schubert * Copy over all the SSL_CTX options - reasonable behavior 118e0c4386eSCy Schubert * allows testing of cases where the options between two 119e0c4386eSCy Schubert * contexts differ/conflict 120e0c4386eSCy Schubert */ 121e0c4386eSCy Schubert SSL_clear_options(s, 0xFFFFFFFFL); 122e0c4386eSCy Schubert SSL_set_options(s, SSL_CTX_get_options(new_ctx)); 123e0c4386eSCy Schubert 124e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; 125e0c4386eSCy Schubert return SSL_TLSEXT_ERR_OK; 126e0c4386eSCy Schubert } else if (strcmp(servername, "server1") == 0) { 127e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; 128e0c4386eSCy Schubert return SSL_TLSEXT_ERR_OK; 129e0c4386eSCy Schubert } else if (ignore) { 130e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; 131e0c4386eSCy Schubert return SSL_TLSEXT_ERR_NOACK; 132e0c4386eSCy Schubert } else { 133e0c4386eSCy Schubert /* Don't set an explicit alert, to test library defaults. */ 134e0c4386eSCy Schubert return SSL_TLSEXT_ERR_ALERT_FATAL; 135e0c4386eSCy Schubert } 136e0c4386eSCy Schubert } 137e0c4386eSCy Schubert 138e0c4386eSCy Schubert static int client_hello_select_server_ctx(SSL *s, void *arg, int ignore) 139e0c4386eSCy Schubert { 140e0c4386eSCy Schubert const char *servername; 141e0c4386eSCy Schubert const unsigned char *p; 142e0c4386eSCy Schubert size_t len, remaining; 143e0c4386eSCy Schubert HANDSHAKE_EX_DATA *ex_data = 144e0c4386eSCy Schubert (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); 145e0c4386eSCy Schubert 146e0c4386eSCy Schubert /* 147e0c4386eSCy Schubert * The server_name extension was given too much extensibility when it 148e0c4386eSCy Schubert * was written, so parsing the normal case is a bit complex. 149e0c4386eSCy Schubert */ 150e0c4386eSCy Schubert if (!SSL_client_hello_get0_ext(s, TLSEXT_TYPE_server_name, &p, 151e0c4386eSCy Schubert &remaining) || 152e0c4386eSCy Schubert remaining <= 2) 153e0c4386eSCy Schubert return 0; 154e0c4386eSCy Schubert /* Extract the length of the supplied list of names. */ 155e0c4386eSCy Schubert len = (*(p++) << 8); 156e0c4386eSCy Schubert len += *(p++); 157e0c4386eSCy Schubert if (len + 2 != remaining) 158e0c4386eSCy Schubert return 0; 159e0c4386eSCy Schubert remaining = len; 160e0c4386eSCy Schubert /* 161e0c4386eSCy Schubert * The list in practice only has a single element, so we only consider 162e0c4386eSCy Schubert * the first one. 163e0c4386eSCy Schubert */ 164e0c4386eSCy Schubert if (remaining == 0 || *p++ != TLSEXT_NAMETYPE_host_name) 165e0c4386eSCy Schubert return 0; 166e0c4386eSCy Schubert remaining--; 167e0c4386eSCy Schubert /* Now we can finally pull out the byte array with the actual hostname. */ 168e0c4386eSCy Schubert if (remaining <= 2) 169e0c4386eSCy Schubert return 0; 170e0c4386eSCy Schubert len = (*(p++) << 8); 171e0c4386eSCy Schubert len += *(p++); 172e0c4386eSCy Schubert if (len + 2 > remaining) 173e0c4386eSCy Schubert return 0; 174e0c4386eSCy Schubert remaining = len; 175e0c4386eSCy Schubert servername = (const char *)p; 176e0c4386eSCy Schubert 177e0c4386eSCy Schubert if (len == strlen("server2") && strncmp(servername, "server2", len) == 0) { 178e0c4386eSCy Schubert SSL_CTX *new_ctx = arg; 179e0c4386eSCy Schubert SSL_set_SSL_CTX(s, new_ctx); 180e0c4386eSCy Schubert /* 181e0c4386eSCy Schubert * Copy over all the SSL_CTX options - reasonable behavior 182e0c4386eSCy Schubert * allows testing of cases where the options between two 183e0c4386eSCy Schubert * contexts differ/conflict 184e0c4386eSCy Schubert */ 185e0c4386eSCy Schubert SSL_clear_options(s, 0xFFFFFFFFL); 186e0c4386eSCy Schubert SSL_set_options(s, SSL_CTX_get_options(new_ctx)); 187e0c4386eSCy Schubert 188e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; 189e0c4386eSCy Schubert return 1; 190e0c4386eSCy Schubert } else if (len == strlen("server1") && 191e0c4386eSCy Schubert strncmp(servername, "server1", len) == 0) { 192e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; 193e0c4386eSCy Schubert return 1; 194e0c4386eSCy Schubert } else if (ignore) { 195e0c4386eSCy Schubert ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; 196e0c4386eSCy Schubert return 1; 197e0c4386eSCy Schubert } 198e0c4386eSCy Schubert return 0; 199e0c4386eSCy Schubert } 200e0c4386eSCy Schubert /* 201e0c4386eSCy Schubert * (RFC 6066): 202e0c4386eSCy Schubert * If the server understood the ClientHello extension but 203e0c4386eSCy Schubert * does not recognize the server name, the server SHOULD take one of two 204e0c4386eSCy Schubert * actions: either abort the handshake by sending a fatal-level 205e0c4386eSCy Schubert * unrecognized_name(112) alert or continue the handshake. 206e0c4386eSCy Schubert * 207e0c4386eSCy Schubert * This behaviour is up to the application to configure; we test both 208e0c4386eSCy Schubert * configurations to ensure the state machine propagates the result 209e0c4386eSCy Schubert * correctly. 210e0c4386eSCy Schubert */ 211e0c4386eSCy Schubert static int servername_ignore_cb(SSL *s, int *ad, void *arg) 212e0c4386eSCy Schubert { 213e0c4386eSCy Schubert return select_server_ctx(s, arg, 1); 214e0c4386eSCy Schubert } 215e0c4386eSCy Schubert 216e0c4386eSCy Schubert static int servername_reject_cb(SSL *s, int *ad, void *arg) 217e0c4386eSCy Schubert { 218e0c4386eSCy Schubert return select_server_ctx(s, arg, 0); 219e0c4386eSCy Schubert } 220e0c4386eSCy Schubert 221e0c4386eSCy Schubert static int client_hello_ignore_cb(SSL *s, int *al, void *arg) 222e0c4386eSCy Schubert { 223e0c4386eSCy Schubert if (!client_hello_select_server_ctx(s, arg, 1)) { 224e0c4386eSCy Schubert *al = SSL_AD_UNRECOGNIZED_NAME; 225e0c4386eSCy Schubert return SSL_CLIENT_HELLO_ERROR; 226e0c4386eSCy Schubert } 227e0c4386eSCy Schubert return SSL_CLIENT_HELLO_SUCCESS; 228e0c4386eSCy Schubert } 229e0c4386eSCy Schubert 230e0c4386eSCy Schubert static int client_hello_reject_cb(SSL *s, int *al, void *arg) 231e0c4386eSCy Schubert { 232e0c4386eSCy Schubert if (!client_hello_select_server_ctx(s, arg, 0)) { 233e0c4386eSCy Schubert *al = SSL_AD_UNRECOGNIZED_NAME; 234e0c4386eSCy Schubert return SSL_CLIENT_HELLO_ERROR; 235e0c4386eSCy Schubert } 236e0c4386eSCy Schubert return SSL_CLIENT_HELLO_SUCCESS; 237e0c4386eSCy Schubert } 238e0c4386eSCy Schubert 239e0c4386eSCy Schubert static int client_hello_nov12_cb(SSL *s, int *al, void *arg) 240e0c4386eSCy Schubert { 241e0c4386eSCy Schubert int ret; 242e0c4386eSCy Schubert unsigned int v; 243e0c4386eSCy Schubert const unsigned char *p; 244e0c4386eSCy Schubert 245e0c4386eSCy Schubert v = SSL_client_hello_get0_legacy_version(s); 246e0c4386eSCy Schubert if (v > TLS1_2_VERSION || v < SSL3_VERSION) { 247e0c4386eSCy Schubert *al = SSL_AD_PROTOCOL_VERSION; 248e0c4386eSCy Schubert return SSL_CLIENT_HELLO_ERROR; 249e0c4386eSCy Schubert } 250e0c4386eSCy Schubert (void)SSL_client_hello_get0_session_id(s, &p); 251e0c4386eSCy Schubert if (p == NULL || 252e0c4386eSCy Schubert SSL_client_hello_get0_random(s, &p) == 0 || 253e0c4386eSCy Schubert SSL_client_hello_get0_ciphers(s, &p) == 0 || 254e0c4386eSCy Schubert SSL_client_hello_get0_compression_methods(s, &p) == 0) { 255e0c4386eSCy Schubert *al = SSL_AD_INTERNAL_ERROR; 256e0c4386eSCy Schubert return SSL_CLIENT_HELLO_ERROR; 257e0c4386eSCy Schubert } 258e0c4386eSCy Schubert ret = client_hello_select_server_ctx(s, arg, 0); 259e0c4386eSCy Schubert SSL_set_max_proto_version(s, TLS1_1_VERSION); 260e0c4386eSCy Schubert if (!ret) { 261e0c4386eSCy Schubert *al = SSL_AD_UNRECOGNIZED_NAME; 262e0c4386eSCy Schubert return SSL_CLIENT_HELLO_ERROR; 263e0c4386eSCy Schubert } 264e0c4386eSCy Schubert return SSL_CLIENT_HELLO_SUCCESS; 265e0c4386eSCy Schubert } 266e0c4386eSCy Schubert 267e0c4386eSCy Schubert static unsigned char dummy_ocsp_resp_good_val = 0xff; 268e0c4386eSCy Schubert static unsigned char dummy_ocsp_resp_bad_val = 0xfe; 269e0c4386eSCy Schubert 270e0c4386eSCy Schubert static int server_ocsp_cb(SSL *s, void *arg) 271e0c4386eSCy Schubert { 272e0c4386eSCy Schubert unsigned char *resp; 273e0c4386eSCy Schubert 274e0c4386eSCy Schubert resp = OPENSSL_malloc(1); 275e0c4386eSCy Schubert if (resp == NULL) 276e0c4386eSCy Schubert return SSL_TLSEXT_ERR_ALERT_FATAL; 277e0c4386eSCy Schubert /* 278e0c4386eSCy Schubert * For the purposes of testing we just send back a dummy OCSP response 279e0c4386eSCy Schubert */ 280e0c4386eSCy Schubert *resp = *(unsigned char *)arg; 281e0c4386eSCy Schubert if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1)) { 282e0c4386eSCy Schubert OPENSSL_free(resp); 283e0c4386eSCy Schubert return SSL_TLSEXT_ERR_ALERT_FATAL; 284e0c4386eSCy Schubert } 285e0c4386eSCy Schubert 286e0c4386eSCy Schubert return SSL_TLSEXT_ERR_OK; 287e0c4386eSCy Schubert } 288e0c4386eSCy Schubert 289e0c4386eSCy Schubert static int client_ocsp_cb(SSL *s, void *arg) 290e0c4386eSCy Schubert { 291e0c4386eSCy Schubert const unsigned char *resp; 292e0c4386eSCy Schubert int len; 293e0c4386eSCy Schubert 294e0c4386eSCy Schubert len = SSL_get_tlsext_status_ocsp_resp(s, &resp); 295e0c4386eSCy Schubert if (len != 1 || *resp != dummy_ocsp_resp_good_val) 296e0c4386eSCy Schubert return 0; 297e0c4386eSCy Schubert 298e0c4386eSCy Schubert return 1; 299e0c4386eSCy Schubert } 300e0c4386eSCy Schubert 301e0c4386eSCy Schubert static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) { 302e0c4386eSCy Schubert X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION); 303e0c4386eSCy Schubert return 0; 304e0c4386eSCy Schubert } 305e0c4386eSCy Schubert 306e0c4386eSCy Schubert static int n_retries = 0; 307e0c4386eSCy Schubert static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg) { 308e0c4386eSCy Schubert int idx = SSL_get_ex_data_X509_STORE_CTX_idx(); 309e0c4386eSCy Schubert SSL *ssl; 310e0c4386eSCy Schubert 311e0c4386eSCy Schubert /* this should not happen but check anyway */ 312e0c4386eSCy Schubert if (idx < 0 313e0c4386eSCy Schubert || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL) 314e0c4386eSCy Schubert return 0; 315e0c4386eSCy Schubert 316e0c4386eSCy Schubert if (--n_retries < 0) 317e0c4386eSCy Schubert return 1; 318e0c4386eSCy Schubert 319e0c4386eSCy Schubert return SSL_set_retry_verify(ssl); 320e0c4386eSCy Schubert } 321e0c4386eSCy Schubert 322e0c4386eSCy Schubert static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) { 323e0c4386eSCy Schubert return 1; 324e0c4386eSCy Schubert } 325e0c4386eSCy Schubert 326e0c4386eSCy Schubert static int broken_session_ticket_cb(SSL *s, unsigned char *key_name, 327e0c4386eSCy Schubert unsigned char *iv, EVP_CIPHER_CTX *ctx, 328e0c4386eSCy Schubert EVP_MAC_CTX *hctx, int enc) 329e0c4386eSCy Schubert { 330e0c4386eSCy Schubert return 0; 331e0c4386eSCy Schubert } 332e0c4386eSCy Schubert 333e0c4386eSCy Schubert static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name, 334e0c4386eSCy Schubert unsigned char *iv, 335e0c4386eSCy Schubert EVP_CIPHER_CTX *ctx, 336e0c4386eSCy Schubert EVP_MAC_CTX *hctx, int enc) 337e0c4386eSCy Schubert { 338e0c4386eSCy Schubert HANDSHAKE_EX_DATA *ex_data = 339e0c4386eSCy Schubert (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); 340e0c4386eSCy Schubert ex_data->session_ticket_do_not_call = 1; 341e0c4386eSCy Schubert return 0; 342e0c4386eSCy Schubert } 343e0c4386eSCy Schubert 344e0c4386eSCy Schubert /* Parse the comma-separated list into TLS format. */ 345e0c4386eSCy Schubert static int parse_protos(const char *protos, unsigned char **out, size_t *outlen) 346e0c4386eSCy Schubert { 347e0c4386eSCy Schubert size_t len, i, prefix; 348e0c4386eSCy Schubert 349e0c4386eSCy Schubert len = strlen(protos); 350e0c4386eSCy Schubert 351*a7148ab3SEnji Cooper if (len == 0) { 352*a7148ab3SEnji Cooper *out = NULL; 353*a7148ab3SEnji Cooper *outlen = 0; 354*a7148ab3SEnji Cooper return 1; 355*a7148ab3SEnji Cooper } 356*a7148ab3SEnji Cooper 357e0c4386eSCy Schubert /* Should never have reuse. */ 358e0c4386eSCy Schubert if (!TEST_ptr_null(*out) 359e0c4386eSCy Schubert /* Test values are small, so we omit length limit checks. */ 360e0c4386eSCy Schubert || !TEST_ptr(*out = OPENSSL_malloc(len + 1))) 361e0c4386eSCy Schubert return 0; 362e0c4386eSCy Schubert *outlen = len + 1; 363e0c4386eSCy Schubert 364e0c4386eSCy Schubert /* 365e0c4386eSCy Schubert * foo => '3', 'f', 'o', 'o' 366e0c4386eSCy Schubert * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r' 367e0c4386eSCy Schubert */ 368e0c4386eSCy Schubert memcpy(*out + 1, protos, len); 369e0c4386eSCy Schubert 370e0c4386eSCy Schubert prefix = 0; 371e0c4386eSCy Schubert i = prefix + 1; 372e0c4386eSCy Schubert while (i <= len) { 373e0c4386eSCy Schubert if ((*out)[i] == ',') { 374e0c4386eSCy Schubert if (!TEST_int_gt(i - 1, prefix)) 375e0c4386eSCy Schubert goto err; 376e0c4386eSCy Schubert (*out)[prefix] = (unsigned char)(i - 1 - prefix); 377e0c4386eSCy Schubert prefix = i; 378e0c4386eSCy Schubert } 379e0c4386eSCy Schubert i++; 380e0c4386eSCy Schubert } 381e0c4386eSCy Schubert if (!TEST_int_gt(len, prefix)) 382e0c4386eSCy Schubert goto err; 383e0c4386eSCy Schubert (*out)[prefix] = (unsigned char)(len - prefix); 384e0c4386eSCy Schubert return 1; 385e0c4386eSCy Schubert 386e0c4386eSCy Schubert err: 387e0c4386eSCy Schubert OPENSSL_free(*out); 388e0c4386eSCy Schubert *out = NULL; 389e0c4386eSCy Schubert return 0; 390e0c4386eSCy Schubert } 391e0c4386eSCy Schubert 392e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG 393e0c4386eSCy Schubert /* 394e0c4386eSCy Schubert * The client SHOULD select the first protocol advertised by the server that it 395e0c4386eSCy Schubert * also supports. In the event that the client doesn't support any of server's 396e0c4386eSCy Schubert * protocols, or the server doesn't advertise any, it SHOULD select the first 397e0c4386eSCy Schubert * protocol that it supports. 398e0c4386eSCy Schubert */ 399e0c4386eSCy Schubert static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen, 400e0c4386eSCy Schubert const unsigned char *in, unsigned int inlen, 401e0c4386eSCy Schubert void *arg) 402e0c4386eSCy Schubert { 403e0c4386eSCy Schubert CTX_DATA *ctx_data = (CTX_DATA*)(arg); 404e0c4386eSCy Schubert int ret; 405e0c4386eSCy Schubert 406e0c4386eSCy Schubert ret = SSL_select_next_proto(out, outlen, in, inlen, 407e0c4386eSCy Schubert ctx_data->npn_protocols, 408e0c4386eSCy Schubert ctx_data->npn_protocols_len); 409e0c4386eSCy Schubert /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */ 410e0c4386eSCy Schubert return TEST_true(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP) 411e0c4386eSCy Schubert ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL; 412e0c4386eSCy Schubert } 413e0c4386eSCy Schubert 414e0c4386eSCy Schubert static int server_npn_cb(SSL *s, const unsigned char **data, 415e0c4386eSCy Schubert unsigned int *len, void *arg) 416e0c4386eSCy Schubert { 417e0c4386eSCy Schubert CTX_DATA *ctx_data = (CTX_DATA*)(arg); 418e0c4386eSCy Schubert *data = ctx_data->npn_protocols; 419e0c4386eSCy Schubert *len = ctx_data->npn_protocols_len; 420e0c4386eSCy Schubert return SSL_TLSEXT_ERR_OK; 421e0c4386eSCy Schubert } 422e0c4386eSCy Schubert #endif 423e0c4386eSCy Schubert 424e0c4386eSCy Schubert /* 425e0c4386eSCy Schubert * The server SHOULD select the most highly preferred protocol that it supports 426e0c4386eSCy Schubert * and that is also advertised by the client. In the event that the server 427e0c4386eSCy Schubert * supports no protocols that the client advertises, then the server SHALL 428e0c4386eSCy Schubert * respond with a fatal "no_application_protocol" alert. 429e0c4386eSCy Schubert */ 430e0c4386eSCy Schubert static int server_alpn_cb(SSL *s, const unsigned char **out, 431e0c4386eSCy Schubert unsigned char *outlen, const unsigned char *in, 432e0c4386eSCy Schubert unsigned int inlen, void *arg) 433e0c4386eSCy Schubert { 434e0c4386eSCy Schubert CTX_DATA *ctx_data = (CTX_DATA*)(arg); 435e0c4386eSCy Schubert int ret; 436e0c4386eSCy Schubert 437e0c4386eSCy Schubert /* SSL_select_next_proto isn't const-correct... */ 438e0c4386eSCy Schubert unsigned char *tmp_out; 439e0c4386eSCy Schubert 440e0c4386eSCy Schubert /* 441e0c4386eSCy Schubert * The result points either to |in| or to |ctx_data->alpn_protocols|. 442e0c4386eSCy Schubert * The callback is allowed to point to |in| or to a long-lived buffer, 443e0c4386eSCy Schubert * so we can return directly without storing a copy. 444e0c4386eSCy Schubert */ 445e0c4386eSCy Schubert ret = SSL_select_next_proto(&tmp_out, outlen, 446e0c4386eSCy Schubert ctx_data->alpn_protocols, 447e0c4386eSCy Schubert ctx_data->alpn_protocols_len, in, inlen); 448e0c4386eSCy Schubert 449e0c4386eSCy Schubert *out = tmp_out; 450e0c4386eSCy Schubert /* Unlike NPN, we don't tolerate a mismatch. */ 451e0c4386eSCy Schubert return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK 452e0c4386eSCy Schubert : SSL_TLSEXT_ERR_ALERT_FATAL; 453e0c4386eSCy Schubert } 454e0c4386eSCy Schubert 455e0c4386eSCy Schubert static int generate_session_ticket_cb(SSL *s, void *arg) 456e0c4386eSCy Schubert { 457e0c4386eSCy Schubert CTX_DATA *server_ctx_data = arg; 458e0c4386eSCy Schubert SSL_SESSION *ss = SSL_get_session(s); 459e0c4386eSCy Schubert char *app_data = server_ctx_data->session_ticket_app_data; 460e0c4386eSCy Schubert 461e0c4386eSCy Schubert if (ss == NULL || app_data == NULL) 462e0c4386eSCy Schubert return 0; 463e0c4386eSCy Schubert 464e0c4386eSCy Schubert return SSL_SESSION_set1_ticket_appdata(ss, app_data, strlen(app_data)); 465e0c4386eSCy Schubert } 466e0c4386eSCy Schubert 467e0c4386eSCy Schubert static int decrypt_session_ticket_cb(SSL *s, SSL_SESSION *ss, 468e0c4386eSCy Schubert const unsigned char *keyname, 469e0c4386eSCy Schubert size_t keyname_len, 470e0c4386eSCy Schubert SSL_TICKET_STATUS status, 471e0c4386eSCy Schubert void *arg) 472e0c4386eSCy Schubert { 473e0c4386eSCy Schubert switch (status) { 474e0c4386eSCy Schubert case SSL_TICKET_EMPTY: 475e0c4386eSCy Schubert case SSL_TICKET_NO_DECRYPT: 476e0c4386eSCy Schubert return SSL_TICKET_RETURN_IGNORE_RENEW; 477e0c4386eSCy Schubert case SSL_TICKET_SUCCESS: 478e0c4386eSCy Schubert return SSL_TICKET_RETURN_USE; 479e0c4386eSCy Schubert case SSL_TICKET_SUCCESS_RENEW: 480e0c4386eSCy Schubert return SSL_TICKET_RETURN_USE_RENEW; 481e0c4386eSCy Schubert default: 482e0c4386eSCy Schubert break; 483e0c4386eSCy Schubert } 484e0c4386eSCy Schubert return SSL_TICKET_RETURN_ABORT; 485e0c4386eSCy Schubert } 486e0c4386eSCy Schubert 487e0c4386eSCy Schubert /* 488e0c4386eSCy Schubert * Configure callbacks and other properties that can't be set directly 489e0c4386eSCy Schubert * in the server/client CONF. 490e0c4386eSCy Schubert */ 491e0c4386eSCy Schubert static int configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, 492e0c4386eSCy Schubert SSL_CTX *client_ctx, 493e0c4386eSCy Schubert const SSL_TEST_CTX *test, 494e0c4386eSCy Schubert const SSL_TEST_EXTRA_CONF *extra, 495e0c4386eSCy Schubert CTX_DATA *server_ctx_data, 496e0c4386eSCy Schubert CTX_DATA *server2_ctx_data, 497e0c4386eSCy Schubert CTX_DATA *client_ctx_data) 498e0c4386eSCy Schubert { 499e0c4386eSCy Schubert unsigned char *ticket_keys; 500e0c4386eSCy Schubert size_t ticket_key_len; 501e0c4386eSCy Schubert 502e0c4386eSCy Schubert if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server_ctx, 503e0c4386eSCy Schubert test->max_fragment_size), 1)) 504e0c4386eSCy Schubert goto err; 505e0c4386eSCy Schubert if (server2_ctx != NULL) { 506e0c4386eSCy Schubert if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server2_ctx, 507e0c4386eSCy Schubert test->max_fragment_size), 508e0c4386eSCy Schubert 1)) 509e0c4386eSCy Schubert goto err; 510e0c4386eSCy Schubert } 511e0c4386eSCy Schubert if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(client_ctx, 512e0c4386eSCy Schubert test->max_fragment_size), 1)) 513e0c4386eSCy Schubert goto err; 514e0c4386eSCy Schubert 515e0c4386eSCy Schubert switch (extra->client.verify_callback) { 516e0c4386eSCy Schubert case SSL_TEST_VERIFY_ACCEPT_ALL: 517e0c4386eSCy Schubert SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb, NULL); 518e0c4386eSCy Schubert break; 519e0c4386eSCy Schubert case SSL_TEST_VERIFY_RETRY_ONCE: 520e0c4386eSCy Schubert n_retries = 1; 521e0c4386eSCy Schubert SSL_CTX_set_cert_verify_callback(client_ctx, &verify_retry_cb, NULL); 522e0c4386eSCy Schubert break; 523e0c4386eSCy Schubert case SSL_TEST_VERIFY_REJECT_ALL: 524e0c4386eSCy Schubert SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, NULL); 525e0c4386eSCy Schubert break; 526e0c4386eSCy Schubert case SSL_TEST_VERIFY_NONE: 527e0c4386eSCy Schubert break; 528e0c4386eSCy Schubert } 529e0c4386eSCy Schubert 530e0c4386eSCy Schubert switch (extra->client.max_fragment_len_mode) { 531e0c4386eSCy Schubert case TLSEXT_max_fragment_length_512: 532e0c4386eSCy Schubert case TLSEXT_max_fragment_length_1024: 533e0c4386eSCy Schubert case TLSEXT_max_fragment_length_2048: 534e0c4386eSCy Schubert case TLSEXT_max_fragment_length_4096: 535e0c4386eSCy Schubert case TLSEXT_max_fragment_length_DISABLED: 536e0c4386eSCy Schubert SSL_CTX_set_tlsext_max_fragment_length( 537e0c4386eSCy Schubert client_ctx, extra->client.max_fragment_len_mode); 538e0c4386eSCy Schubert break; 539e0c4386eSCy Schubert } 540e0c4386eSCy Schubert 541e0c4386eSCy Schubert /* 542e0c4386eSCy Schubert * Link the two contexts for SNI purposes. 543e0c4386eSCy Schubert * Also do ClientHello callbacks here, as setting both ClientHello and SNI 544e0c4386eSCy Schubert * is bad. 545e0c4386eSCy Schubert */ 546e0c4386eSCy Schubert switch (extra->server.servername_callback) { 547e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_IGNORE_MISMATCH: 548e0c4386eSCy Schubert SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb); 549e0c4386eSCy Schubert SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); 550e0c4386eSCy Schubert break; 551e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_REJECT_MISMATCH: 552e0c4386eSCy Schubert SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb); 553e0c4386eSCy Schubert SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); 554e0c4386eSCy Schubert break; 555e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_CB_NONE: 556e0c4386eSCy Schubert break; 557e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH: 558e0c4386eSCy Schubert SSL_CTX_set_client_hello_cb(server_ctx, client_hello_ignore_cb, server2_ctx); 559e0c4386eSCy Schubert break; 560e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH: 561e0c4386eSCy Schubert SSL_CTX_set_client_hello_cb(server_ctx, client_hello_reject_cb, server2_ctx); 562e0c4386eSCy Schubert break; 563e0c4386eSCy Schubert case SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12: 564e0c4386eSCy Schubert SSL_CTX_set_client_hello_cb(server_ctx, client_hello_nov12_cb, server2_ctx); 565e0c4386eSCy Schubert } 566e0c4386eSCy Schubert 567e0c4386eSCy Schubert if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) { 568e0c4386eSCy Schubert SSL_CTX_set_tlsext_status_type(client_ctx, TLSEXT_STATUSTYPE_ocsp); 569e0c4386eSCy Schubert SSL_CTX_set_tlsext_status_cb(client_ctx, client_ocsp_cb); 570e0c4386eSCy Schubert SSL_CTX_set_tlsext_status_arg(client_ctx, NULL); 571e0c4386eSCy Schubert SSL_CTX_set_tlsext_status_cb(server_ctx, server_ocsp_cb); 572e0c4386eSCy Schubert SSL_CTX_set_tlsext_status_arg(server_ctx, 573e0c4386eSCy Schubert ((extra->server.cert_status == SSL_TEST_CERT_STATUS_GOOD_RESPONSE) 574e0c4386eSCy Schubert ? &dummy_ocsp_resp_good_val : &dummy_ocsp_resp_bad_val)); 575e0c4386eSCy Schubert } 576e0c4386eSCy Schubert 577e0c4386eSCy Schubert /* 578e0c4386eSCy Schubert * The initial_ctx/session_ctx always handles the encrypt/decrypt of the 579e0c4386eSCy Schubert * session ticket. This ticket_key callback is assigned to the second 580e0c4386eSCy Schubert * session (assigned via SNI), and should never be invoked 581e0c4386eSCy Schubert */ 582e0c4386eSCy Schubert if (server2_ctx != NULL) 583e0c4386eSCy Schubert SSL_CTX_set_tlsext_ticket_key_evp_cb(server2_ctx, 584e0c4386eSCy Schubert do_not_call_session_ticket_cb); 585e0c4386eSCy Schubert 586e0c4386eSCy Schubert if (extra->server.broken_session_ticket) { 587e0c4386eSCy Schubert SSL_CTX_set_tlsext_ticket_key_evp_cb(server_ctx, 588e0c4386eSCy Schubert broken_session_ticket_cb); 589e0c4386eSCy Schubert } 590e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG 591e0c4386eSCy Schubert if (extra->server.npn_protocols != NULL) { 592e0c4386eSCy Schubert if (!TEST_true(parse_protos(extra->server.npn_protocols, 593e0c4386eSCy Schubert &server_ctx_data->npn_protocols, 594e0c4386eSCy Schubert &server_ctx_data->npn_protocols_len))) 595e0c4386eSCy Schubert goto err; 596e0c4386eSCy Schubert SSL_CTX_set_npn_advertised_cb(server_ctx, server_npn_cb, 597e0c4386eSCy Schubert server_ctx_data); 598e0c4386eSCy Schubert } 599e0c4386eSCy Schubert if (extra->server2.npn_protocols != NULL) { 600e0c4386eSCy Schubert if (!TEST_true(parse_protos(extra->server2.npn_protocols, 601e0c4386eSCy Schubert &server2_ctx_data->npn_protocols, 602e0c4386eSCy Schubert &server2_ctx_data->npn_protocols_len)) 603e0c4386eSCy Schubert || !TEST_ptr(server2_ctx)) 604e0c4386eSCy Schubert goto err; 605e0c4386eSCy Schubert SSL_CTX_set_npn_advertised_cb(server2_ctx, server_npn_cb, 606e0c4386eSCy Schubert server2_ctx_data); 607e0c4386eSCy Schubert } 608e0c4386eSCy Schubert if (extra->client.npn_protocols != NULL) { 609e0c4386eSCy Schubert if (!TEST_true(parse_protos(extra->client.npn_protocols, 610e0c4386eSCy Schubert &client_ctx_data->npn_protocols, 611e0c4386eSCy Schubert &client_ctx_data->npn_protocols_len))) 612e0c4386eSCy Schubert goto err; 613e0c4386eSCy Schubert SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb, 614e0c4386eSCy Schubert client_ctx_data); 615e0c4386eSCy Schubert } 616e0c4386eSCy Schubert #endif 617e0c4386eSCy Schubert if (extra->server.alpn_protocols != NULL) { 618e0c4386eSCy Schubert if (!TEST_true(parse_protos(extra->server.alpn_protocols, 619e0c4386eSCy Schubert &server_ctx_data->alpn_protocols, 620e0c4386eSCy Schubert &server_ctx_data->alpn_protocols_len))) 621e0c4386eSCy Schubert goto err; 622e0c4386eSCy Schubert SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data); 623e0c4386eSCy Schubert } 624e0c4386eSCy Schubert if (extra->server2.alpn_protocols != NULL) { 625e0c4386eSCy Schubert if (!TEST_ptr(server2_ctx) 626e0c4386eSCy Schubert || !TEST_true(parse_protos(extra->server2.alpn_protocols, 627e0c4386eSCy Schubert &server2_ctx_data->alpn_protocols, 628e0c4386eSCy Schubert &server2_ctx_data->alpn_protocols_len 629e0c4386eSCy Schubert ))) 630e0c4386eSCy Schubert goto err; 631e0c4386eSCy Schubert SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb, 632e0c4386eSCy Schubert server2_ctx_data); 633e0c4386eSCy Schubert } 634e0c4386eSCy Schubert if (extra->client.alpn_protocols != NULL) { 635e0c4386eSCy Schubert unsigned char *alpn_protos = NULL; 636e0c4386eSCy Schubert size_t alpn_protos_len = 0; 637e0c4386eSCy Schubert 638e0c4386eSCy Schubert if (!TEST_true(parse_protos(extra->client.alpn_protocols, 639e0c4386eSCy Schubert &alpn_protos, &alpn_protos_len)) 640e0c4386eSCy Schubert /* Reversed return value convention... */ 641e0c4386eSCy Schubert || !TEST_int_eq(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos, 642e0c4386eSCy Schubert alpn_protos_len), 0)) 643e0c4386eSCy Schubert goto err; 644e0c4386eSCy Schubert OPENSSL_free(alpn_protos); 645e0c4386eSCy Schubert } 646e0c4386eSCy Schubert 647e0c4386eSCy Schubert if (extra->server.session_ticket_app_data != NULL) { 648e0c4386eSCy Schubert server_ctx_data->session_ticket_app_data = 649e0c4386eSCy Schubert OPENSSL_strdup(extra->server.session_ticket_app_data); 650e0c4386eSCy Schubert SSL_CTX_set_session_ticket_cb(server_ctx, generate_session_ticket_cb, 651e0c4386eSCy Schubert decrypt_session_ticket_cb, server_ctx_data); 652e0c4386eSCy Schubert } 653e0c4386eSCy Schubert if (extra->server2.session_ticket_app_data != NULL) { 654e0c4386eSCy Schubert if (!TEST_ptr(server2_ctx)) 655e0c4386eSCy Schubert goto err; 656e0c4386eSCy Schubert server2_ctx_data->session_ticket_app_data = 657e0c4386eSCy Schubert OPENSSL_strdup(extra->server2.session_ticket_app_data); 658e0c4386eSCy Schubert SSL_CTX_set_session_ticket_cb(server2_ctx, NULL, 659e0c4386eSCy Schubert decrypt_session_ticket_cb, server2_ctx_data); 660e0c4386eSCy Schubert } 661e0c4386eSCy Schubert 662e0c4386eSCy Schubert /* 663e0c4386eSCy Schubert * Use fixed session ticket keys so that we can decrypt a ticket created with 664e0c4386eSCy Schubert * one CTX in another CTX. Don't address server2 for the moment. 665e0c4386eSCy Schubert */ 666e0c4386eSCy Schubert ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0); 667e0c4386eSCy Schubert if (!TEST_ptr(ticket_keys = OPENSSL_zalloc(ticket_key_len)) 668e0c4386eSCy Schubert || !TEST_int_eq(SSL_CTX_set_tlsext_ticket_keys(server_ctx, 669e0c4386eSCy Schubert ticket_keys, 670e0c4386eSCy Schubert ticket_key_len), 1)) { 671e0c4386eSCy Schubert OPENSSL_free(ticket_keys); 672e0c4386eSCy Schubert goto err; 673e0c4386eSCy Schubert } 674e0c4386eSCy Schubert OPENSSL_free(ticket_keys); 675e0c4386eSCy Schubert 676e0c4386eSCy Schubert /* The default log list includes EC keys, so CT can't work without EC. */ 677e0c4386eSCy Schubert #if !defined(OPENSSL_NO_CT) && !defined(OPENSSL_NO_EC) 678e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_default_ctlog_list_file(client_ctx))) 679e0c4386eSCy Schubert goto err; 680e0c4386eSCy Schubert switch (extra->client.ct_validation) { 681e0c4386eSCy Schubert case SSL_TEST_CT_VALIDATION_PERMISSIVE: 682e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_enable_ct(client_ctx, 683e0c4386eSCy Schubert SSL_CT_VALIDATION_PERMISSIVE))) 684e0c4386eSCy Schubert goto err; 685e0c4386eSCy Schubert break; 686e0c4386eSCy Schubert case SSL_TEST_CT_VALIDATION_STRICT: 687e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT))) 688e0c4386eSCy Schubert goto err; 689e0c4386eSCy Schubert break; 690e0c4386eSCy Schubert case SSL_TEST_CT_VALIDATION_NONE: 691e0c4386eSCy Schubert break; 692e0c4386eSCy Schubert } 693e0c4386eSCy Schubert #endif 694e0c4386eSCy Schubert #ifndef OPENSSL_NO_SRP 695e0c4386eSCy Schubert if (!configure_handshake_ctx_for_srp(server_ctx, server2_ctx, client_ctx, 696e0c4386eSCy Schubert extra, server_ctx_data, 697e0c4386eSCy Schubert server2_ctx_data, client_ctx_data)) 698e0c4386eSCy Schubert goto err; 699e0c4386eSCy Schubert #endif /* !OPENSSL_NO_SRP */ 700e0c4386eSCy Schubert return 1; 701e0c4386eSCy Schubert err: 702e0c4386eSCy Schubert return 0; 703e0c4386eSCy Schubert } 704e0c4386eSCy Schubert 705e0c4386eSCy Schubert /* Configure per-SSL callbacks and other properties. */ 706e0c4386eSCy Schubert static void configure_handshake_ssl(SSL *server, SSL *client, 707e0c4386eSCy Schubert const SSL_TEST_EXTRA_CONF *extra) 708e0c4386eSCy Schubert { 709e0c4386eSCy Schubert if (extra->client.servername != SSL_TEST_SERVERNAME_NONE) 710e0c4386eSCy Schubert SSL_set_tlsext_host_name(client, 711e0c4386eSCy Schubert ssl_servername_name(extra->client.servername)); 712e0c4386eSCy Schubert if (extra->client.enable_pha) 713e0c4386eSCy Schubert SSL_set_post_handshake_auth(client, 1); 714e0c4386eSCy Schubert } 715e0c4386eSCy Schubert 716e0c4386eSCy Schubert /* The status for each connection phase. */ 717e0c4386eSCy Schubert typedef enum { 718e0c4386eSCy Schubert PEER_SUCCESS, 719e0c4386eSCy Schubert PEER_RETRY, 720e0c4386eSCy Schubert PEER_ERROR, 721e0c4386eSCy Schubert PEER_WAITING, 722e0c4386eSCy Schubert PEER_TEST_FAILURE 723e0c4386eSCy Schubert } peer_status_t; 724e0c4386eSCy Schubert 725e0c4386eSCy Schubert /* An SSL object and associated read-write buffers. */ 726e0c4386eSCy Schubert typedef struct peer_st { 727e0c4386eSCy Schubert SSL *ssl; 728e0c4386eSCy Schubert /* Buffer lengths are int to match the SSL read/write API. */ 729e0c4386eSCy Schubert unsigned char *write_buf; 730e0c4386eSCy Schubert int write_buf_len; 731e0c4386eSCy Schubert unsigned char *read_buf; 732e0c4386eSCy Schubert int read_buf_len; 733e0c4386eSCy Schubert int bytes_to_write; 734e0c4386eSCy Schubert int bytes_to_read; 735e0c4386eSCy Schubert peer_status_t status; 736e0c4386eSCy Schubert } PEER; 737e0c4386eSCy Schubert 738e0c4386eSCy Schubert static int create_peer(PEER *peer, SSL_CTX *ctx) 739e0c4386eSCy Schubert { 740e0c4386eSCy Schubert static const int peer_buffer_size = 64 * 1024; 741e0c4386eSCy Schubert SSL *ssl = NULL; 742e0c4386eSCy Schubert unsigned char *read_buf = NULL, *write_buf = NULL; 743e0c4386eSCy Schubert 744e0c4386eSCy Schubert if (!TEST_ptr(ssl = SSL_new(ctx)) 745e0c4386eSCy Schubert || !TEST_ptr(write_buf = OPENSSL_zalloc(peer_buffer_size)) 746e0c4386eSCy Schubert || !TEST_ptr(read_buf = OPENSSL_zalloc(peer_buffer_size))) 747e0c4386eSCy Schubert goto err; 748e0c4386eSCy Schubert 749e0c4386eSCy Schubert peer->ssl = ssl; 750e0c4386eSCy Schubert peer->write_buf = write_buf; 751e0c4386eSCy Schubert peer->read_buf = read_buf; 752e0c4386eSCy Schubert peer->write_buf_len = peer->read_buf_len = peer_buffer_size; 753e0c4386eSCy Schubert return 1; 754e0c4386eSCy Schubert err: 755e0c4386eSCy Schubert SSL_free(ssl); 756e0c4386eSCy Schubert OPENSSL_free(write_buf); 757e0c4386eSCy Schubert OPENSSL_free(read_buf); 758e0c4386eSCy Schubert return 0; 759e0c4386eSCy Schubert } 760e0c4386eSCy Schubert 761e0c4386eSCy Schubert static void peer_free_data(PEER *peer) 762e0c4386eSCy Schubert { 763e0c4386eSCy Schubert SSL_free(peer->ssl); 764e0c4386eSCy Schubert OPENSSL_free(peer->write_buf); 765e0c4386eSCy Schubert OPENSSL_free(peer->read_buf); 766e0c4386eSCy Schubert } 767e0c4386eSCy Schubert 768e0c4386eSCy Schubert /* 769e0c4386eSCy Schubert * Note that we could do the handshake transparently under an SSL_write, 770e0c4386eSCy Schubert * but separating the steps is more helpful for debugging test failures. 771e0c4386eSCy Schubert */ 772e0c4386eSCy Schubert static void do_handshake_step(PEER *peer) 773e0c4386eSCy Schubert { 774e0c4386eSCy Schubert if (!TEST_int_eq(peer->status, PEER_RETRY)) { 775e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 776e0c4386eSCy Schubert } else { 777e0c4386eSCy Schubert int ret = SSL_do_handshake(peer->ssl); 778e0c4386eSCy Schubert 779e0c4386eSCy Schubert if (ret == 1) { 780e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 781e0c4386eSCy Schubert } else if (ret == 0) { 782e0c4386eSCy Schubert peer->status = PEER_ERROR; 783e0c4386eSCy Schubert } else { 784e0c4386eSCy Schubert int error = SSL_get_error(peer->ssl, ret); 785e0c4386eSCy Schubert 786e0c4386eSCy Schubert /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */ 787e0c4386eSCy Schubert if (error != SSL_ERROR_WANT_READ 788e0c4386eSCy Schubert && error != SSL_ERROR_WANT_RETRY_VERIFY) 789e0c4386eSCy Schubert peer->status = PEER_ERROR; 790e0c4386eSCy Schubert } 791e0c4386eSCy Schubert } 792e0c4386eSCy Schubert } 793e0c4386eSCy Schubert 794e0c4386eSCy Schubert /*- 795e0c4386eSCy Schubert * Send/receive some application data. The read-write sequence is 796e0c4386eSCy Schubert * Peer A: (R) W - first read will yield no data 797e0c4386eSCy Schubert * Peer B: R W 798e0c4386eSCy Schubert * ... 799e0c4386eSCy Schubert * Peer A: R W 800e0c4386eSCy Schubert * Peer B: R W 801e0c4386eSCy Schubert * Peer A: R 802e0c4386eSCy Schubert */ 803e0c4386eSCy Schubert static void do_app_data_step(PEER *peer) 804e0c4386eSCy Schubert { 805e0c4386eSCy Schubert int ret = 1, write_bytes; 806e0c4386eSCy Schubert 807e0c4386eSCy Schubert if (!TEST_int_eq(peer->status, PEER_RETRY)) { 808e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 809e0c4386eSCy Schubert return; 810e0c4386eSCy Schubert } 811e0c4386eSCy Schubert 812e0c4386eSCy Schubert /* We read everything available... */ 813e0c4386eSCy Schubert while (ret > 0 && peer->bytes_to_read) { 814e0c4386eSCy Schubert ret = SSL_read(peer->ssl, peer->read_buf, peer->read_buf_len); 815e0c4386eSCy Schubert if (ret > 0) { 816e0c4386eSCy Schubert if (!TEST_int_le(ret, peer->bytes_to_read)) { 817e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 818e0c4386eSCy Schubert return; 819e0c4386eSCy Schubert } 820e0c4386eSCy Schubert peer->bytes_to_read -= ret; 821e0c4386eSCy Schubert } else if (ret == 0) { 822e0c4386eSCy Schubert peer->status = PEER_ERROR; 823e0c4386eSCy Schubert return; 824e0c4386eSCy Schubert } else { 825e0c4386eSCy Schubert int error = SSL_get_error(peer->ssl, ret); 826e0c4386eSCy Schubert if (error != SSL_ERROR_WANT_READ) { 827e0c4386eSCy Schubert peer->status = PEER_ERROR; 828e0c4386eSCy Schubert return; 829e0c4386eSCy Schubert } /* Else continue with write. */ 830e0c4386eSCy Schubert } 831e0c4386eSCy Schubert } 832e0c4386eSCy Schubert 833e0c4386eSCy Schubert /* ... but we only write one write-buffer-full of data. */ 834e0c4386eSCy Schubert write_bytes = peer->bytes_to_write < peer->write_buf_len ? peer->bytes_to_write : 835e0c4386eSCy Schubert peer->write_buf_len; 836e0c4386eSCy Schubert if (write_bytes) { 837e0c4386eSCy Schubert ret = SSL_write(peer->ssl, peer->write_buf, write_bytes); 838e0c4386eSCy Schubert if (ret > 0) { 839e0c4386eSCy Schubert /* SSL_write will only succeed with a complete write. */ 840e0c4386eSCy Schubert if (!TEST_int_eq(ret, write_bytes)) { 841e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 842e0c4386eSCy Schubert return; 843e0c4386eSCy Schubert } 844e0c4386eSCy Schubert peer->bytes_to_write -= ret; 845e0c4386eSCy Schubert } else { 846e0c4386eSCy Schubert /* 847e0c4386eSCy Schubert * We should perhaps check for SSL_ERROR_WANT_READ/WRITE here 848e0c4386eSCy Schubert * but this doesn't yet occur with current app data sizes. 849e0c4386eSCy Schubert */ 850e0c4386eSCy Schubert peer->status = PEER_ERROR; 851e0c4386eSCy Schubert return; 852e0c4386eSCy Schubert } 853e0c4386eSCy Schubert } 854e0c4386eSCy Schubert 855e0c4386eSCy Schubert /* 856e0c4386eSCy Schubert * We could simply finish when there was nothing to read, and we have 857e0c4386eSCy Schubert * nothing left to write. But keeping track of the expected number of bytes 858e0c4386eSCy Schubert * to read gives us somewhat better guarantees that all data sent is in fact 859e0c4386eSCy Schubert * received. 860e0c4386eSCy Schubert */ 861e0c4386eSCy Schubert if (peer->bytes_to_write == 0 && peer->bytes_to_read == 0) { 862e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 863e0c4386eSCy Schubert } 864e0c4386eSCy Schubert } 865e0c4386eSCy Schubert 866e0c4386eSCy Schubert static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer) 867e0c4386eSCy Schubert { 868e0c4386eSCy Schubert int ret; 869e0c4386eSCy Schubert char buf; 870e0c4386eSCy Schubert 871e0c4386eSCy Schubert if (peer->status == PEER_SUCCESS) { 872e0c4386eSCy Schubert /* 873e0c4386eSCy Schubert * We are a client that succeeded this step previously, but the server 874e0c4386eSCy Schubert * wanted to retry. Probably there is a no_renegotiation warning alert 875e0c4386eSCy Schubert * waiting for us. Attempt to continue the handshake. 876e0c4386eSCy Schubert */ 877e0c4386eSCy Schubert peer->status = PEER_RETRY; 878e0c4386eSCy Schubert do_handshake_step(peer); 879e0c4386eSCy Schubert return; 880e0c4386eSCy Schubert } 881e0c4386eSCy Schubert 882e0c4386eSCy Schubert if (!TEST_int_eq(peer->status, PEER_RETRY) 883e0c4386eSCy Schubert || !TEST_true(test_ctx->handshake_mode 884e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_RENEG_SERVER 885e0c4386eSCy Schubert || test_ctx->handshake_mode 886e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_RENEG_CLIENT 887e0c4386eSCy Schubert || test_ctx->handshake_mode 888e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER 889e0c4386eSCy Schubert || test_ctx->handshake_mode 890e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT 891e0c4386eSCy Schubert || test_ctx->handshake_mode 892e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH)) { 893e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 894e0c4386eSCy Schubert return; 895e0c4386eSCy Schubert } 896e0c4386eSCy Schubert 897e0c4386eSCy Schubert /* Reset the count of the amount of app data we need to read/write */ 898e0c4386eSCy Schubert peer->bytes_to_write = peer->bytes_to_read = test_ctx->app_data_size; 899e0c4386eSCy Schubert 900e0c4386eSCy Schubert /* Check if we are the peer that is going to initiate */ 901e0c4386eSCy Schubert if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER 902e0c4386eSCy Schubert && SSL_is_server(peer->ssl)) 903e0c4386eSCy Schubert || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT 904e0c4386eSCy Schubert && !SSL_is_server(peer->ssl))) { 905e0c4386eSCy Schubert /* 906e0c4386eSCy Schubert * If we already asked for a renegotiation then fall through to the 907e0c4386eSCy Schubert * SSL_read() below. 908e0c4386eSCy Schubert */ 909e0c4386eSCy Schubert if (!SSL_renegotiate_pending(peer->ssl)) { 910e0c4386eSCy Schubert /* 911e0c4386eSCy Schubert * If we are the client we will always attempt to resume the 912e0c4386eSCy Schubert * session. The server may or may not resume dependent on the 913e0c4386eSCy Schubert * setting of SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 914e0c4386eSCy Schubert */ 915e0c4386eSCy Schubert if (SSL_is_server(peer->ssl)) { 916e0c4386eSCy Schubert ret = SSL_renegotiate(peer->ssl); 917e0c4386eSCy Schubert } else { 918e0c4386eSCy Schubert int full_reneg = 0; 919e0c4386eSCy Schubert 920e0c4386eSCy Schubert if (test_ctx->extra.client.no_extms_on_reneg) { 921e0c4386eSCy Schubert SSL_set_options(peer->ssl, SSL_OP_NO_EXTENDED_MASTER_SECRET); 922e0c4386eSCy Schubert full_reneg = 1; 923e0c4386eSCy Schubert } 924e0c4386eSCy Schubert if (test_ctx->extra.client.reneg_ciphers != NULL) { 925e0c4386eSCy Schubert if (!SSL_set_cipher_list(peer->ssl, 926e0c4386eSCy Schubert test_ctx->extra.client.reneg_ciphers)) { 927e0c4386eSCy Schubert peer->status = PEER_ERROR; 928e0c4386eSCy Schubert return; 929e0c4386eSCy Schubert } 930e0c4386eSCy Schubert full_reneg = 1; 931e0c4386eSCy Schubert } 932e0c4386eSCy Schubert if (full_reneg) 933e0c4386eSCy Schubert ret = SSL_renegotiate(peer->ssl); 934e0c4386eSCy Schubert else 935e0c4386eSCy Schubert ret = SSL_renegotiate_abbreviated(peer->ssl); 936e0c4386eSCy Schubert } 937e0c4386eSCy Schubert if (!ret) { 938e0c4386eSCy Schubert peer->status = PEER_ERROR; 939e0c4386eSCy Schubert return; 940e0c4386eSCy Schubert } 941e0c4386eSCy Schubert do_handshake_step(peer); 942e0c4386eSCy Schubert /* 943e0c4386eSCy Schubert * If status is PEER_RETRY it means we're waiting on the peer to 944e0c4386eSCy Schubert * continue the handshake. As far as setting up the renegotiation is 945e0c4386eSCy Schubert * concerned that is a success. The next step will continue the 946e0c4386eSCy Schubert * handshake to its conclusion. 947e0c4386eSCy Schubert * 948e0c4386eSCy Schubert * If status is PEER_SUCCESS then we are the server and we have 949e0c4386eSCy Schubert * successfully sent the HelloRequest. We need to continue to wait 950e0c4386eSCy Schubert * until the handshake arrives from the client. 951e0c4386eSCy Schubert */ 952e0c4386eSCy Schubert if (peer->status == PEER_RETRY) 953e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 954e0c4386eSCy Schubert else if (peer->status == PEER_SUCCESS) 955e0c4386eSCy Schubert peer->status = PEER_RETRY; 956e0c4386eSCy Schubert return; 957e0c4386eSCy Schubert } 958e0c4386eSCy Schubert } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER 959e0c4386eSCy Schubert || test_ctx->handshake_mode 960e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) { 961e0c4386eSCy Schubert if (SSL_is_server(peer->ssl) 962e0c4386eSCy Schubert != (test_ctx->handshake_mode 963e0c4386eSCy Schubert == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER)) { 964e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 965e0c4386eSCy Schubert return; 966e0c4386eSCy Schubert } 967e0c4386eSCy Schubert 968e0c4386eSCy Schubert ret = SSL_key_update(peer->ssl, test_ctx->key_update_type); 969e0c4386eSCy Schubert if (!ret) { 970e0c4386eSCy Schubert peer->status = PEER_ERROR; 971e0c4386eSCy Schubert return; 972e0c4386eSCy Schubert } 973e0c4386eSCy Schubert do_handshake_step(peer); 974e0c4386eSCy Schubert /* 975e0c4386eSCy Schubert * This is a one step handshake. We shouldn't get anything other than 976e0c4386eSCy Schubert * PEER_SUCCESS 977e0c4386eSCy Schubert */ 978e0c4386eSCy Schubert if (peer->status != PEER_SUCCESS) 979e0c4386eSCy Schubert peer->status = PEER_ERROR; 980e0c4386eSCy Schubert return; 981e0c4386eSCy Schubert } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH) { 982e0c4386eSCy Schubert if (SSL_is_server(peer->ssl)) { 983e0c4386eSCy Schubert /* Make the server believe it's received the extension */ 984e0c4386eSCy Schubert if (test_ctx->extra.server.force_pha) 985e0c4386eSCy Schubert peer->ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; 986e0c4386eSCy Schubert ret = SSL_verify_client_post_handshake(peer->ssl); 987e0c4386eSCy Schubert if (!ret) { 988e0c4386eSCy Schubert peer->status = PEER_ERROR; 989e0c4386eSCy Schubert return; 990e0c4386eSCy Schubert } 991e0c4386eSCy Schubert } 992e0c4386eSCy Schubert do_handshake_step(peer); 993e0c4386eSCy Schubert /* 994e0c4386eSCy Schubert * This is a one step handshake. We shouldn't get anything other than 995e0c4386eSCy Schubert * PEER_SUCCESS 996e0c4386eSCy Schubert */ 997e0c4386eSCy Schubert if (peer->status != PEER_SUCCESS) 998e0c4386eSCy Schubert peer->status = PEER_ERROR; 999e0c4386eSCy Schubert return; 1000e0c4386eSCy Schubert } 1001e0c4386eSCy Schubert 1002e0c4386eSCy Schubert /* 1003e0c4386eSCy Schubert * The SSL object is still expecting app data, even though it's going to 1004e0c4386eSCy Schubert * get a handshake message. We try to read, and it should fail - after which 1005e0c4386eSCy Schubert * we should be in a handshake 1006e0c4386eSCy Schubert */ 1007e0c4386eSCy Schubert ret = SSL_read(peer->ssl, &buf, sizeof(buf)); 1008e0c4386eSCy Schubert if (ret >= 0) { 1009e0c4386eSCy Schubert /* 1010e0c4386eSCy Schubert * We're not actually expecting data - we're expecting a reneg to 1011e0c4386eSCy Schubert * start 1012e0c4386eSCy Schubert */ 1013e0c4386eSCy Schubert peer->status = PEER_ERROR; 1014e0c4386eSCy Schubert return; 1015e0c4386eSCy Schubert } else { 1016e0c4386eSCy Schubert int error = SSL_get_error(peer->ssl, ret); 1017e0c4386eSCy Schubert if (error != SSL_ERROR_WANT_READ) { 1018e0c4386eSCy Schubert peer->status = PEER_ERROR; 1019e0c4386eSCy Schubert return; 1020e0c4386eSCy Schubert } 1021e0c4386eSCy Schubert /* If we're not in init yet then we're not done with setup yet */ 1022e0c4386eSCy Schubert if (!SSL_in_init(peer->ssl)) 1023e0c4386eSCy Schubert return; 1024e0c4386eSCy Schubert } 1025e0c4386eSCy Schubert 1026e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 1027e0c4386eSCy Schubert } 1028e0c4386eSCy Schubert 1029e0c4386eSCy Schubert 1030e0c4386eSCy Schubert /* 1031e0c4386eSCy Schubert * RFC 5246 says: 1032e0c4386eSCy Schubert * 1033e0c4386eSCy Schubert * Note that as of TLS 1.1, 1034e0c4386eSCy Schubert * failure to properly close a connection no longer requires that a 1035e0c4386eSCy Schubert * session not be resumed. This is a change from TLS 1.0 to conform 1036e0c4386eSCy Schubert * with widespread implementation practice. 1037e0c4386eSCy Schubert * 1038e0c4386eSCy Schubert * However, 1039e0c4386eSCy Schubert * (a) OpenSSL requires that a connection be shutdown for all protocol versions. 1040e0c4386eSCy Schubert * (b) We test lower versions, too. 1041e0c4386eSCy Schubert * So we just implement shutdown. We do a full bidirectional shutdown so that we 1042e0c4386eSCy Schubert * can compare sent and received close_notify alerts and get some test coverage 1043e0c4386eSCy Schubert * for SSL_shutdown as a bonus. 1044e0c4386eSCy Schubert */ 1045e0c4386eSCy Schubert static void do_shutdown_step(PEER *peer) 1046e0c4386eSCy Schubert { 1047e0c4386eSCy Schubert int ret; 1048e0c4386eSCy Schubert 1049e0c4386eSCy Schubert if (!TEST_int_eq(peer->status, PEER_RETRY)) { 1050e0c4386eSCy Schubert peer->status = PEER_TEST_FAILURE; 1051e0c4386eSCy Schubert return; 1052e0c4386eSCy Schubert } 1053e0c4386eSCy Schubert ret = SSL_shutdown(peer->ssl); 1054e0c4386eSCy Schubert 1055e0c4386eSCy Schubert if (ret == 1) { 1056e0c4386eSCy Schubert peer->status = PEER_SUCCESS; 1057e0c4386eSCy Schubert } else if (ret < 0) { /* On 0, we retry. */ 1058e0c4386eSCy Schubert int error = SSL_get_error(peer->ssl, ret); 1059e0c4386eSCy Schubert 1060e0c4386eSCy Schubert if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) 1061e0c4386eSCy Schubert peer->status = PEER_ERROR; 1062e0c4386eSCy Schubert } 1063e0c4386eSCy Schubert } 1064e0c4386eSCy Schubert 1065e0c4386eSCy Schubert typedef enum { 1066e0c4386eSCy Schubert HANDSHAKE, 1067e0c4386eSCy Schubert RENEG_APPLICATION_DATA, 1068e0c4386eSCy Schubert RENEG_SETUP, 1069e0c4386eSCy Schubert RENEG_HANDSHAKE, 1070e0c4386eSCy Schubert APPLICATION_DATA, 1071e0c4386eSCy Schubert SHUTDOWN, 1072e0c4386eSCy Schubert CONNECTION_DONE 1073e0c4386eSCy Schubert } connect_phase_t; 1074e0c4386eSCy Schubert 1075e0c4386eSCy Schubert 1076e0c4386eSCy Schubert static int renegotiate_op(const SSL_TEST_CTX *test_ctx) 1077e0c4386eSCy Schubert { 1078e0c4386eSCy Schubert switch (test_ctx->handshake_mode) { 1079e0c4386eSCy Schubert case SSL_TEST_HANDSHAKE_RENEG_SERVER: 1080e0c4386eSCy Schubert case SSL_TEST_HANDSHAKE_RENEG_CLIENT: 1081e0c4386eSCy Schubert return 1; 1082e0c4386eSCy Schubert default: 1083e0c4386eSCy Schubert return 0; 1084e0c4386eSCy Schubert } 1085e0c4386eSCy Schubert } 1086e0c4386eSCy Schubert static int post_handshake_op(const SSL_TEST_CTX *test_ctx) 1087e0c4386eSCy Schubert { 1088e0c4386eSCy Schubert switch (test_ctx->handshake_mode) { 1089e0c4386eSCy Schubert case SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT: 1090e0c4386eSCy Schubert case SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER: 1091e0c4386eSCy Schubert case SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH: 1092e0c4386eSCy Schubert return 1; 1093e0c4386eSCy Schubert default: 1094e0c4386eSCy Schubert return 0; 1095e0c4386eSCy Schubert } 1096e0c4386eSCy Schubert } 1097e0c4386eSCy Schubert 1098e0c4386eSCy Schubert static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx, 1099e0c4386eSCy Schubert connect_phase_t phase) 1100e0c4386eSCy Schubert { 1101e0c4386eSCy Schubert switch (phase) { 1102e0c4386eSCy Schubert case HANDSHAKE: 1103e0c4386eSCy Schubert if (renegotiate_op(test_ctx) || post_handshake_op(test_ctx)) 1104e0c4386eSCy Schubert return RENEG_APPLICATION_DATA; 1105e0c4386eSCy Schubert return APPLICATION_DATA; 1106e0c4386eSCy Schubert case RENEG_APPLICATION_DATA: 1107e0c4386eSCy Schubert return RENEG_SETUP; 1108e0c4386eSCy Schubert case RENEG_SETUP: 1109e0c4386eSCy Schubert if (post_handshake_op(test_ctx)) 1110e0c4386eSCy Schubert return APPLICATION_DATA; 1111e0c4386eSCy Schubert return RENEG_HANDSHAKE; 1112e0c4386eSCy Schubert case RENEG_HANDSHAKE: 1113e0c4386eSCy Schubert return APPLICATION_DATA; 1114e0c4386eSCy Schubert case APPLICATION_DATA: 1115e0c4386eSCy Schubert return SHUTDOWN; 1116e0c4386eSCy Schubert case SHUTDOWN: 1117e0c4386eSCy Schubert return CONNECTION_DONE; 1118e0c4386eSCy Schubert case CONNECTION_DONE: 1119e0c4386eSCy Schubert TEST_error("Trying to progress after connection done"); 1120e0c4386eSCy Schubert break; 1121e0c4386eSCy Schubert } 1122e0c4386eSCy Schubert return -1; 1123e0c4386eSCy Schubert } 1124e0c4386eSCy Schubert 1125e0c4386eSCy Schubert static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer, 1126e0c4386eSCy Schubert connect_phase_t phase) 1127e0c4386eSCy Schubert { 1128e0c4386eSCy Schubert switch (phase) { 1129e0c4386eSCy Schubert case HANDSHAKE: 1130e0c4386eSCy Schubert do_handshake_step(peer); 1131e0c4386eSCy Schubert break; 1132e0c4386eSCy Schubert case RENEG_APPLICATION_DATA: 1133e0c4386eSCy Schubert do_app_data_step(peer); 1134e0c4386eSCy Schubert break; 1135e0c4386eSCy Schubert case RENEG_SETUP: 1136e0c4386eSCy Schubert do_reneg_setup_step(test_ctx, peer); 1137e0c4386eSCy Schubert break; 1138e0c4386eSCy Schubert case RENEG_HANDSHAKE: 1139e0c4386eSCy Schubert do_handshake_step(peer); 1140e0c4386eSCy Schubert break; 1141e0c4386eSCy Schubert case APPLICATION_DATA: 1142e0c4386eSCy Schubert do_app_data_step(peer); 1143e0c4386eSCy Schubert break; 1144e0c4386eSCy Schubert case SHUTDOWN: 1145e0c4386eSCy Schubert do_shutdown_step(peer); 1146e0c4386eSCy Schubert break; 1147e0c4386eSCy Schubert case CONNECTION_DONE: 1148e0c4386eSCy Schubert TEST_error("Action after connection done"); 1149e0c4386eSCy Schubert break; 1150e0c4386eSCy Schubert } 1151e0c4386eSCy Schubert } 1152e0c4386eSCy Schubert 1153e0c4386eSCy Schubert typedef enum { 1154e0c4386eSCy Schubert /* Both parties succeeded. */ 1155e0c4386eSCy Schubert HANDSHAKE_SUCCESS, 1156e0c4386eSCy Schubert /* Client errored. */ 1157e0c4386eSCy Schubert CLIENT_ERROR, 1158e0c4386eSCy Schubert /* Server errored. */ 1159e0c4386eSCy Schubert SERVER_ERROR, 1160e0c4386eSCy Schubert /* Peers are in inconsistent state. */ 1161e0c4386eSCy Schubert INTERNAL_ERROR, 1162e0c4386eSCy Schubert /* One or both peers not done. */ 1163e0c4386eSCy Schubert HANDSHAKE_RETRY 1164e0c4386eSCy Schubert } handshake_status_t; 1165e0c4386eSCy Schubert 1166e0c4386eSCy Schubert /* 1167e0c4386eSCy Schubert * Determine the handshake outcome. 1168e0c4386eSCy Schubert * last_status: the status of the peer to have acted last. 1169e0c4386eSCy Schubert * previous_status: the status of the peer that didn't act last. 1170e0c4386eSCy Schubert * client_spoke_last: 1 if the client went last. 1171e0c4386eSCy Schubert */ 1172e0c4386eSCy Schubert static handshake_status_t handshake_status(peer_status_t last_status, 1173e0c4386eSCy Schubert peer_status_t previous_status, 1174e0c4386eSCy Schubert int client_spoke_last) 1175e0c4386eSCy Schubert { 1176e0c4386eSCy Schubert switch (last_status) { 1177e0c4386eSCy Schubert case PEER_TEST_FAILURE: 1178e0c4386eSCy Schubert return INTERNAL_ERROR; 1179e0c4386eSCy Schubert 1180e0c4386eSCy Schubert case PEER_WAITING: 1181e0c4386eSCy Schubert /* Shouldn't ever happen */ 1182e0c4386eSCy Schubert return INTERNAL_ERROR; 1183e0c4386eSCy Schubert 1184e0c4386eSCy Schubert case PEER_SUCCESS: 1185e0c4386eSCy Schubert switch (previous_status) { 1186e0c4386eSCy Schubert case PEER_TEST_FAILURE: 1187e0c4386eSCy Schubert return INTERNAL_ERROR; 1188e0c4386eSCy Schubert case PEER_SUCCESS: 1189e0c4386eSCy Schubert /* Both succeeded. */ 1190e0c4386eSCy Schubert return HANDSHAKE_SUCCESS; 1191e0c4386eSCy Schubert case PEER_WAITING: 1192e0c4386eSCy Schubert case PEER_RETRY: 1193e0c4386eSCy Schubert /* Let the first peer finish. */ 1194e0c4386eSCy Schubert return HANDSHAKE_RETRY; 1195e0c4386eSCy Schubert case PEER_ERROR: 1196e0c4386eSCy Schubert /* 1197e0c4386eSCy Schubert * Second peer succeeded despite the fact that the first peer 1198e0c4386eSCy Schubert * already errored. This shouldn't happen. 1199e0c4386eSCy Schubert */ 1200e0c4386eSCy Schubert return INTERNAL_ERROR; 1201e0c4386eSCy Schubert } 1202e0c4386eSCy Schubert break; 1203e0c4386eSCy Schubert 1204e0c4386eSCy Schubert case PEER_RETRY: 1205e0c4386eSCy Schubert return HANDSHAKE_RETRY; 1206e0c4386eSCy Schubert 1207e0c4386eSCy Schubert case PEER_ERROR: 1208e0c4386eSCy Schubert switch (previous_status) { 1209e0c4386eSCy Schubert case PEER_TEST_FAILURE: 1210e0c4386eSCy Schubert return INTERNAL_ERROR; 1211e0c4386eSCy Schubert case PEER_WAITING: 1212e0c4386eSCy Schubert /* The client failed immediately before sending the ClientHello */ 1213e0c4386eSCy Schubert return client_spoke_last ? CLIENT_ERROR : INTERNAL_ERROR; 1214e0c4386eSCy Schubert case PEER_SUCCESS: 1215e0c4386eSCy Schubert /* First peer succeeded but second peer errored. */ 1216e0c4386eSCy Schubert return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR; 1217e0c4386eSCy Schubert case PEER_RETRY: 1218e0c4386eSCy Schubert /* We errored; let the peer finish. */ 1219e0c4386eSCy Schubert return HANDSHAKE_RETRY; 1220e0c4386eSCy Schubert case PEER_ERROR: 1221e0c4386eSCy Schubert /* Both peers errored. Return the one that errored first. */ 1222e0c4386eSCy Schubert return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR; 1223e0c4386eSCy Schubert } 1224e0c4386eSCy Schubert } 1225e0c4386eSCy Schubert /* Control should never reach here. */ 1226e0c4386eSCy Schubert return INTERNAL_ERROR; 1227e0c4386eSCy Schubert } 1228e0c4386eSCy Schubert 1229e0c4386eSCy Schubert /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */ 1230e0c4386eSCy Schubert static char *dup_str(const unsigned char *in, size_t len) 1231e0c4386eSCy Schubert { 1232e0c4386eSCy Schubert char *ret = NULL; 1233e0c4386eSCy Schubert 1234e0c4386eSCy Schubert if (len == 0) 1235e0c4386eSCy Schubert return NULL; 1236e0c4386eSCy Schubert 1237e0c4386eSCy Schubert /* Assert that the string does not contain NUL-bytes. */ 1238e0c4386eSCy Schubert if (TEST_size_t_eq(OPENSSL_strnlen((const char*)(in), len), len)) 1239e0c4386eSCy Schubert TEST_ptr(ret = OPENSSL_strndup((const char*)(in), len)); 1240e0c4386eSCy Schubert return ret; 1241e0c4386eSCy Schubert } 1242e0c4386eSCy Schubert 1243e0c4386eSCy Schubert static int pkey_type(EVP_PKEY *pkey) 1244e0c4386eSCy Schubert { 1245e0c4386eSCy Schubert if (EVP_PKEY_is_a(pkey, "EC")) { 1246e0c4386eSCy Schubert char name[80]; 1247e0c4386eSCy Schubert size_t name_len; 1248e0c4386eSCy Schubert 1249e0c4386eSCy Schubert if (!EVP_PKEY_get_group_name(pkey, name, sizeof(name), &name_len)) 1250e0c4386eSCy Schubert return NID_undef; 1251e0c4386eSCy Schubert return OBJ_txt2nid(name); 1252e0c4386eSCy Schubert } 1253e0c4386eSCy Schubert return EVP_PKEY_get_id(pkey); 1254e0c4386eSCy Schubert } 1255e0c4386eSCy Schubert 1256e0c4386eSCy Schubert static int peer_pkey_type(SSL *s) 1257e0c4386eSCy Schubert { 1258e0c4386eSCy Schubert X509 *x = SSL_get0_peer_certificate(s); 1259e0c4386eSCy Schubert 1260e0c4386eSCy Schubert if (x != NULL) 1261e0c4386eSCy Schubert return pkey_type(X509_get0_pubkey(x)); 1262e0c4386eSCy Schubert return NID_undef; 1263e0c4386eSCy Schubert } 1264e0c4386eSCy Schubert 1265e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) 1266e0c4386eSCy Schubert static int set_sock_as_sctp(int sock) 1267e0c4386eSCy Schubert { 1268e0c4386eSCy Schubert struct sctp_assocparams assocparams; 1269e0c4386eSCy Schubert struct sctp_rtoinfo rto_info; 1270e0c4386eSCy Schubert BIO *tmpbio; 1271e0c4386eSCy Schubert 1272e0c4386eSCy Schubert /* 1273e0c4386eSCy Schubert * To allow tests to fail fast (within a second or so), reduce the 1274e0c4386eSCy Schubert * retransmission timeouts and the number of retransmissions. 1275e0c4386eSCy Schubert */ 1276e0c4386eSCy Schubert memset(&rto_info, 0, sizeof(struct sctp_rtoinfo)); 1277e0c4386eSCy Schubert rto_info.srto_initial = 100; 1278e0c4386eSCy Schubert rto_info.srto_max = 200; 1279e0c4386eSCy Schubert rto_info.srto_min = 50; 1280e0c4386eSCy Schubert (void)setsockopt(sock, IPPROTO_SCTP, SCTP_RTOINFO, 1281e0c4386eSCy Schubert (const void *)&rto_info, sizeof(struct sctp_rtoinfo)); 1282e0c4386eSCy Schubert memset(&assocparams, 0, sizeof(struct sctp_assocparams)); 1283e0c4386eSCy Schubert assocparams.sasoc_asocmaxrxt = 2; 1284e0c4386eSCy Schubert (void)setsockopt(sock, IPPROTO_SCTP, SCTP_ASSOCINFO, 1285e0c4386eSCy Schubert (const void *)&assocparams, 1286e0c4386eSCy Schubert sizeof(struct sctp_assocparams)); 1287e0c4386eSCy Schubert 1288e0c4386eSCy Schubert /* 1289e0c4386eSCy Schubert * For SCTP we have to set various options on the socket prior to 1290e0c4386eSCy Schubert * connecting. This is done automatically by BIO_new_dgram_sctp(). 1291e0c4386eSCy Schubert * We don't actually need the created BIO though so we free it again 1292e0c4386eSCy Schubert * immediately. 1293e0c4386eSCy Schubert */ 1294e0c4386eSCy Schubert tmpbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE); 1295e0c4386eSCy Schubert 1296e0c4386eSCy Schubert if (tmpbio == NULL) 1297e0c4386eSCy Schubert return 0; 1298e0c4386eSCy Schubert BIO_free(tmpbio); 1299e0c4386eSCy Schubert 1300e0c4386eSCy Schubert return 1; 1301e0c4386eSCy Schubert } 1302e0c4386eSCy Schubert 1303e0c4386eSCy Schubert static int create_sctp_socks(int *ssock, int *csock) 1304e0c4386eSCy Schubert { 1305e0c4386eSCy Schubert BIO_ADDRINFO *res = NULL; 1306e0c4386eSCy Schubert const BIO_ADDRINFO *ai = NULL; 1307e0c4386eSCy Schubert int lsock = INVALID_SOCKET, asock = INVALID_SOCKET; 1308e0c4386eSCy Schubert int consock = INVALID_SOCKET; 1309e0c4386eSCy Schubert int ret = 0; 1310e0c4386eSCy Schubert int family = 0; 1311e0c4386eSCy Schubert 1312e0c4386eSCy Schubert if (BIO_sock_init() != 1) 1313e0c4386eSCy Schubert return 0; 1314e0c4386eSCy Schubert 1315e0c4386eSCy Schubert /* 1316e0c4386eSCy Schubert * Port is 4463. It could be anything. It will fail if it's already being 1317e0c4386eSCy Schubert * used for some other SCTP service. It seems unlikely though so we don't 1318e0c4386eSCy Schubert * worry about it here. 1319e0c4386eSCy Schubert */ 1320e0c4386eSCy Schubert if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_SERVER, family, SOCK_STREAM, 1321e0c4386eSCy Schubert IPPROTO_SCTP, &res)) 1322e0c4386eSCy Schubert return 0; 1323e0c4386eSCy Schubert 1324e0c4386eSCy Schubert for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) { 1325e0c4386eSCy Schubert family = BIO_ADDRINFO_family(ai); 1326e0c4386eSCy Schubert lsock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); 1327e0c4386eSCy Schubert if (lsock == INVALID_SOCKET) { 1328e0c4386eSCy Schubert /* Maybe the kernel doesn't support the socket family, even if 1329e0c4386eSCy Schubert * BIO_lookup() added it in the returned result... 1330e0c4386eSCy Schubert */ 1331e0c4386eSCy Schubert continue; 1332e0c4386eSCy Schubert } 1333e0c4386eSCy Schubert 1334e0c4386eSCy Schubert if (!set_sock_as_sctp(lsock) 1335e0c4386eSCy Schubert || !BIO_listen(lsock, BIO_ADDRINFO_address(ai), 1336e0c4386eSCy Schubert BIO_SOCK_REUSEADDR)) { 1337e0c4386eSCy Schubert BIO_closesocket(lsock); 1338e0c4386eSCy Schubert lsock = INVALID_SOCKET; 1339e0c4386eSCy Schubert continue; 1340e0c4386eSCy Schubert } 1341e0c4386eSCy Schubert 1342e0c4386eSCy Schubert /* Success, don't try any more addresses */ 1343e0c4386eSCy Schubert break; 1344e0c4386eSCy Schubert } 1345e0c4386eSCy Schubert 1346e0c4386eSCy Schubert if (lsock == INVALID_SOCKET) 1347e0c4386eSCy Schubert goto err; 1348e0c4386eSCy Schubert 1349e0c4386eSCy Schubert BIO_ADDRINFO_free(res); 1350e0c4386eSCy Schubert res = NULL; 1351e0c4386eSCy Schubert 1352e0c4386eSCy Schubert if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_CLIENT, family, SOCK_STREAM, 1353e0c4386eSCy Schubert IPPROTO_SCTP, &res)) 1354e0c4386eSCy Schubert goto err; 1355e0c4386eSCy Schubert 1356e0c4386eSCy Schubert consock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); 1357e0c4386eSCy Schubert if (consock == INVALID_SOCKET) 1358e0c4386eSCy Schubert goto err; 1359e0c4386eSCy Schubert 1360e0c4386eSCy Schubert if (!set_sock_as_sctp(consock) 1361e0c4386eSCy Schubert || !BIO_connect(consock, BIO_ADDRINFO_address(res), 0) 1362e0c4386eSCy Schubert || !BIO_socket_nbio(consock, 1)) 1363e0c4386eSCy Schubert goto err; 1364e0c4386eSCy Schubert 1365e0c4386eSCy Schubert asock = BIO_accept_ex(lsock, NULL, BIO_SOCK_NONBLOCK); 1366e0c4386eSCy Schubert if (asock == INVALID_SOCKET) 1367e0c4386eSCy Schubert goto err; 1368e0c4386eSCy Schubert 1369e0c4386eSCy Schubert *csock = consock; 1370e0c4386eSCy Schubert *ssock = asock; 1371e0c4386eSCy Schubert consock = asock = INVALID_SOCKET; 1372e0c4386eSCy Schubert ret = 1; 1373e0c4386eSCy Schubert 1374e0c4386eSCy Schubert err: 1375e0c4386eSCy Schubert BIO_ADDRINFO_free(res); 1376e0c4386eSCy Schubert if (consock != INVALID_SOCKET) 1377e0c4386eSCy Schubert BIO_closesocket(consock); 1378e0c4386eSCy Schubert if (lsock != INVALID_SOCKET) 1379e0c4386eSCy Schubert BIO_closesocket(lsock); 1380e0c4386eSCy Schubert if (asock != INVALID_SOCKET) 1381e0c4386eSCy Schubert BIO_closesocket(asock); 1382e0c4386eSCy Schubert return ret; 1383e0c4386eSCy Schubert } 1384e0c4386eSCy Schubert #endif 1385e0c4386eSCy Schubert 1386e0c4386eSCy Schubert /* 1387e0c4386eSCy Schubert * Note that |extra| points to the correct client/server configuration 1388e0c4386eSCy Schubert * within |test_ctx|. When configuring the handshake, general mode settings 1389e0c4386eSCy Schubert * are taken from |test_ctx|, and client/server-specific settings should be 1390e0c4386eSCy Schubert * taken from |extra|. 1391e0c4386eSCy Schubert * 1392e0c4386eSCy Schubert * The configuration code should never reach into |test_ctx->extra| or 1393e0c4386eSCy Schubert * |test_ctx->resume_extra| directly. 1394e0c4386eSCy Schubert * 1395e0c4386eSCy Schubert * (We could refactor test mode settings into a substructure. This would result 1396e0c4386eSCy Schubert * in cleaner argument passing but would complicate the test configuration 1397e0c4386eSCy Schubert * parsing.) 1398e0c4386eSCy Schubert */ 1399e0c4386eSCy Schubert static HANDSHAKE_RESULT *do_handshake_internal( 1400e0c4386eSCy Schubert SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx, 1401e0c4386eSCy Schubert const SSL_TEST_CTX *test_ctx, const SSL_TEST_EXTRA_CONF *extra, 1402e0c4386eSCy Schubert SSL_SESSION *session_in, SSL_SESSION *serv_sess_in, 1403e0c4386eSCy Schubert SSL_SESSION **session_out, SSL_SESSION **serv_sess_out) 1404e0c4386eSCy Schubert { 1405e0c4386eSCy Schubert PEER server, client; 1406e0c4386eSCy Schubert BIO *client_to_server = NULL, *server_to_client = NULL; 1407e0c4386eSCy Schubert HANDSHAKE_EX_DATA server_ex_data, client_ex_data; 1408e0c4386eSCy Schubert CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data; 1409e0c4386eSCy Schubert HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new(); 1410e0c4386eSCy Schubert int client_turn = 1, client_turn_count = 0, client_wait_count = 0; 1411e0c4386eSCy Schubert connect_phase_t phase = HANDSHAKE; 1412e0c4386eSCy Schubert handshake_status_t status = HANDSHAKE_RETRY; 1413e0c4386eSCy Schubert const unsigned char* tick = NULL; 1414e0c4386eSCy Schubert size_t tick_len = 0; 1415e0c4386eSCy Schubert const unsigned char* sess_id = NULL; 1416e0c4386eSCy Schubert unsigned int sess_id_len = 0; 1417e0c4386eSCy Schubert SSL_SESSION* sess = NULL; 1418e0c4386eSCy Schubert const unsigned char *proto = NULL; 1419e0c4386eSCy Schubert /* API dictates unsigned int rather than size_t. */ 1420e0c4386eSCy Schubert unsigned int proto_len = 0; 1421e0c4386eSCy Schubert EVP_PKEY *tmp_key; 1422e0c4386eSCy Schubert const STACK_OF(X509_NAME) *names; 1423e0c4386eSCy Schubert time_t start; 1424e0c4386eSCy Schubert const char* cipher; 1425e0c4386eSCy Schubert 1426e0c4386eSCy Schubert if (ret == NULL) 1427e0c4386eSCy Schubert return NULL; 1428e0c4386eSCy Schubert 1429e0c4386eSCy Schubert memset(&server_ctx_data, 0, sizeof(server_ctx_data)); 1430e0c4386eSCy Schubert memset(&server2_ctx_data, 0, sizeof(server2_ctx_data)); 1431e0c4386eSCy Schubert memset(&client_ctx_data, 0, sizeof(client_ctx_data)); 1432e0c4386eSCy Schubert memset(&server, 0, sizeof(server)); 1433e0c4386eSCy Schubert memset(&client, 0, sizeof(client)); 1434e0c4386eSCy Schubert memset(&server_ex_data, 0, sizeof(server_ex_data)); 1435e0c4386eSCy Schubert memset(&client_ex_data, 0, sizeof(client_ex_data)); 1436e0c4386eSCy Schubert 1437e0c4386eSCy Schubert if (!configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, 1438e0c4386eSCy Schubert test_ctx, extra, &server_ctx_data, 1439e0c4386eSCy Schubert &server2_ctx_data, &client_ctx_data)) { 1440e0c4386eSCy Schubert TEST_note("configure_handshake_ctx"); 1441e0c4386eSCy Schubert HANDSHAKE_RESULT_free(ret); 1442e0c4386eSCy Schubert return NULL; 1443e0c4386eSCy Schubert } 1444e0c4386eSCy Schubert 1445e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) 1446e0c4386eSCy Schubert if (test_ctx->enable_client_sctp_label_bug) 1447e0c4386eSCy Schubert SSL_CTX_set_mode(client_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); 1448e0c4386eSCy Schubert if (test_ctx->enable_server_sctp_label_bug) 1449e0c4386eSCy Schubert SSL_CTX_set_mode(server_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG); 1450e0c4386eSCy Schubert #endif 1451e0c4386eSCy Schubert 1452e0c4386eSCy Schubert /* Setup SSL and buffers; additional configuration happens below. */ 1453e0c4386eSCy Schubert if (!create_peer(&server, server_ctx)) { 1454e0c4386eSCy Schubert TEST_note("creating server context"); 1455e0c4386eSCy Schubert goto err; 1456e0c4386eSCy Schubert } 1457e0c4386eSCy Schubert if (!create_peer(&client, client_ctx)) { 1458e0c4386eSCy Schubert TEST_note("creating client context"); 1459e0c4386eSCy Schubert goto err; 1460e0c4386eSCy Schubert } 1461e0c4386eSCy Schubert 1462e0c4386eSCy Schubert server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size; 1463e0c4386eSCy Schubert client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size; 1464e0c4386eSCy Schubert 1465e0c4386eSCy Schubert configure_handshake_ssl(server.ssl, client.ssl, extra); 1466e0c4386eSCy Schubert if (session_in != NULL) { 1467e0c4386eSCy Schubert SSL_SESSION_get_id(serv_sess_in, &sess_id_len); 1468e0c4386eSCy Schubert /* In case we're testing resumption without tickets. */ 1469e0c4386eSCy Schubert if ((sess_id_len > 0 1470e0c4386eSCy Schubert && !TEST_true(SSL_CTX_add_session(server_ctx, 1471e0c4386eSCy Schubert serv_sess_in))) 1472e0c4386eSCy Schubert || !TEST_true(SSL_set_session(client.ssl, session_in))) 1473e0c4386eSCy Schubert goto err; 1474e0c4386eSCy Schubert sess_id_len = 0; 1475e0c4386eSCy Schubert } 1476e0c4386eSCy Schubert 1477e0c4386eSCy Schubert ret->result = SSL_TEST_INTERNAL_ERROR; 1478e0c4386eSCy Schubert 1479e0c4386eSCy Schubert if (test_ctx->use_sctp) { 1480e0c4386eSCy Schubert #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) 1481e0c4386eSCy Schubert int csock, ssock; 1482e0c4386eSCy Schubert 1483e0c4386eSCy Schubert if (create_sctp_socks(&ssock, &csock)) { 1484e0c4386eSCy Schubert client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE); 1485e0c4386eSCy Schubert server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE); 1486e0c4386eSCy Schubert } 1487e0c4386eSCy Schubert #endif 1488e0c4386eSCy Schubert } else { 1489e0c4386eSCy Schubert client_to_server = BIO_new(BIO_s_mem()); 1490e0c4386eSCy Schubert server_to_client = BIO_new(BIO_s_mem()); 1491e0c4386eSCy Schubert } 1492e0c4386eSCy Schubert 1493e0c4386eSCy Schubert if (!TEST_ptr(client_to_server) 1494e0c4386eSCy Schubert || !TEST_ptr(server_to_client)) 1495e0c4386eSCy Schubert goto err; 1496e0c4386eSCy Schubert 1497e0c4386eSCy Schubert /* Non-blocking bio. */ 1498e0c4386eSCy Schubert BIO_set_nbio(client_to_server, 1); 1499e0c4386eSCy Schubert BIO_set_nbio(server_to_client, 1); 1500e0c4386eSCy Schubert 1501e0c4386eSCy Schubert SSL_set_connect_state(client.ssl); 1502e0c4386eSCy Schubert SSL_set_accept_state(server.ssl); 1503e0c4386eSCy Schubert 1504e0c4386eSCy Schubert /* The bios are now owned by the SSL object. */ 1505e0c4386eSCy Schubert if (test_ctx->use_sctp) { 1506e0c4386eSCy Schubert SSL_set_bio(client.ssl, client_to_server, client_to_server); 1507e0c4386eSCy Schubert SSL_set_bio(server.ssl, server_to_client, server_to_client); 1508e0c4386eSCy Schubert } else { 1509e0c4386eSCy Schubert SSL_set_bio(client.ssl, server_to_client, client_to_server); 1510e0c4386eSCy Schubert if (!TEST_int_gt(BIO_up_ref(server_to_client), 0) 1511e0c4386eSCy Schubert || !TEST_int_gt(BIO_up_ref(client_to_server), 0)) 1512e0c4386eSCy Schubert goto err; 1513e0c4386eSCy Schubert SSL_set_bio(server.ssl, client_to_server, server_to_client); 1514e0c4386eSCy Schubert } 1515e0c4386eSCy Schubert 1516e0c4386eSCy Schubert ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL); 1517e0c4386eSCy Schubert if (!TEST_int_ge(ex_data_idx, 0) 1518e0c4386eSCy Schubert || !TEST_int_eq(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data), 1) 1519e0c4386eSCy Schubert || !TEST_int_eq(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data), 1)) 1520e0c4386eSCy Schubert goto err; 1521e0c4386eSCy Schubert 1522e0c4386eSCy Schubert SSL_set_info_callback(server.ssl, &info_cb); 1523e0c4386eSCy Schubert SSL_set_info_callback(client.ssl, &info_cb); 1524e0c4386eSCy Schubert 1525e0c4386eSCy Schubert client.status = PEER_RETRY; 1526e0c4386eSCy Schubert server.status = PEER_WAITING; 1527e0c4386eSCy Schubert 1528e0c4386eSCy Schubert start = time(NULL); 1529e0c4386eSCy Schubert 1530e0c4386eSCy Schubert /* 1531e0c4386eSCy Schubert * Half-duplex handshake loop. 1532e0c4386eSCy Schubert * Client and server speak to each other synchronously in the same process. 1533e0c4386eSCy Schubert * We use non-blocking BIOs, so whenever one peer blocks for read, it 1534e0c4386eSCy Schubert * returns PEER_RETRY to indicate that it's the other peer's turn to write. 1535e0c4386eSCy Schubert * The handshake succeeds once both peers have succeeded. If one peer 1536e0c4386eSCy Schubert * errors out, we also let the other peer retry (and presumably fail). 1537e0c4386eSCy Schubert */ 1538e0c4386eSCy Schubert for(;;) { 1539e0c4386eSCy Schubert if (client_turn) { 1540e0c4386eSCy Schubert do_connect_step(test_ctx, &client, phase); 1541e0c4386eSCy Schubert status = handshake_status(client.status, server.status, 1542e0c4386eSCy Schubert 1 /* client went last */); 1543e0c4386eSCy Schubert if (server.status == PEER_WAITING) 1544e0c4386eSCy Schubert server.status = PEER_RETRY; 1545e0c4386eSCy Schubert } else { 1546e0c4386eSCy Schubert do_connect_step(test_ctx, &server, phase); 1547e0c4386eSCy Schubert status = handshake_status(server.status, client.status, 1548e0c4386eSCy Schubert 0 /* server went last */); 1549e0c4386eSCy Schubert } 1550e0c4386eSCy Schubert 1551e0c4386eSCy Schubert switch (status) { 1552e0c4386eSCy Schubert case HANDSHAKE_SUCCESS: 1553e0c4386eSCy Schubert client_turn_count = 0; 1554e0c4386eSCy Schubert phase = next_phase(test_ctx, phase); 1555e0c4386eSCy Schubert if (phase == CONNECTION_DONE) { 1556e0c4386eSCy Schubert ret->result = SSL_TEST_SUCCESS; 1557e0c4386eSCy Schubert goto err; 1558e0c4386eSCy Schubert } else { 1559e0c4386eSCy Schubert client.status = server.status = PEER_RETRY; 1560e0c4386eSCy Schubert /* 1561e0c4386eSCy Schubert * For now, client starts each phase. Since each phase is 1562e0c4386eSCy Schubert * started separately, we can later control this more 1563e0c4386eSCy Schubert * precisely, for example, to test client-initiated and 1564e0c4386eSCy Schubert * server-initiated shutdown. 1565e0c4386eSCy Schubert */ 1566e0c4386eSCy Schubert client_turn = 1; 1567e0c4386eSCy Schubert break; 1568e0c4386eSCy Schubert } 1569e0c4386eSCy Schubert case CLIENT_ERROR: 1570e0c4386eSCy Schubert ret->result = SSL_TEST_CLIENT_FAIL; 1571e0c4386eSCy Schubert goto err; 1572e0c4386eSCy Schubert case SERVER_ERROR: 1573e0c4386eSCy Schubert ret->result = SSL_TEST_SERVER_FAIL; 1574e0c4386eSCy Schubert goto err; 1575e0c4386eSCy Schubert case INTERNAL_ERROR: 1576e0c4386eSCy Schubert ret->result = SSL_TEST_INTERNAL_ERROR; 1577e0c4386eSCy Schubert goto err; 1578e0c4386eSCy Schubert case HANDSHAKE_RETRY: 1579e0c4386eSCy Schubert if (test_ctx->use_sctp) { 1580e0c4386eSCy Schubert if (time(NULL) - start > 3) { 1581e0c4386eSCy Schubert /* 1582e0c4386eSCy Schubert * We've waited for too long. Give up. 1583e0c4386eSCy Schubert */ 1584e0c4386eSCy Schubert ret->result = SSL_TEST_INTERNAL_ERROR; 1585e0c4386eSCy Schubert goto err; 1586e0c4386eSCy Schubert } 1587e0c4386eSCy Schubert /* 1588e0c4386eSCy Schubert * With "real" sockets we only swap to processing the peer 1589e0c4386eSCy Schubert * if they are expecting to retry. Otherwise we just retry the 1590e0c4386eSCy Schubert * same endpoint again. 1591e0c4386eSCy Schubert */ 1592e0c4386eSCy Schubert if ((client_turn && server.status == PEER_RETRY) 1593e0c4386eSCy Schubert || (!client_turn && client.status == PEER_RETRY)) 1594e0c4386eSCy Schubert client_turn ^= 1; 1595e0c4386eSCy Schubert } else { 1596e0c4386eSCy Schubert if (client_turn_count++ >= 2000) { 1597e0c4386eSCy Schubert /* 1598e0c4386eSCy Schubert * At this point, there's been so many PEER_RETRY in a row 1599e0c4386eSCy Schubert * that it's likely both sides are stuck waiting for a read. 1600e0c4386eSCy Schubert * It's time to give up. 1601e0c4386eSCy Schubert */ 1602e0c4386eSCy Schubert ret->result = SSL_TEST_INTERNAL_ERROR; 1603e0c4386eSCy Schubert goto err; 1604e0c4386eSCy Schubert } 1605e0c4386eSCy Schubert if (client_turn && server.status == PEER_SUCCESS) { 1606e0c4386eSCy Schubert /* 1607e0c4386eSCy Schubert * The server may finish before the client because the 1608e0c4386eSCy Schubert * client spends some turns processing NewSessionTickets. 1609e0c4386eSCy Schubert */ 1610e0c4386eSCy Schubert if (client_wait_count++ >= 2) { 1611e0c4386eSCy Schubert ret->result = SSL_TEST_INTERNAL_ERROR; 1612e0c4386eSCy Schubert goto err; 1613e0c4386eSCy Schubert } 1614e0c4386eSCy Schubert } else { 1615e0c4386eSCy Schubert /* Continue. */ 1616e0c4386eSCy Schubert client_turn ^= 1; 1617e0c4386eSCy Schubert } 1618e0c4386eSCy Schubert } 1619e0c4386eSCy Schubert break; 1620e0c4386eSCy Schubert } 1621e0c4386eSCy Schubert } 1622e0c4386eSCy Schubert err: 1623e0c4386eSCy Schubert ret->server_alert_sent = server_ex_data.alert_sent; 1624e0c4386eSCy Schubert ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent; 1625e0c4386eSCy Schubert ret->server_alert_received = client_ex_data.alert_received; 1626e0c4386eSCy Schubert ret->client_alert_sent = client_ex_data.alert_sent; 1627e0c4386eSCy Schubert ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent; 1628e0c4386eSCy Schubert ret->client_alert_received = server_ex_data.alert_received; 1629e0c4386eSCy Schubert ret->server_protocol = SSL_version(server.ssl); 1630e0c4386eSCy Schubert ret->client_protocol = SSL_version(client.ssl); 1631e0c4386eSCy Schubert ret->servername = server_ex_data.servername; 1632e0c4386eSCy Schubert if ((sess = SSL_get0_session(client.ssl)) != NULL) { 1633e0c4386eSCy Schubert SSL_SESSION_get0_ticket(sess, &tick, &tick_len); 1634e0c4386eSCy Schubert sess_id = SSL_SESSION_get_id(sess, &sess_id_len); 1635e0c4386eSCy Schubert } 1636e0c4386eSCy Schubert if (tick == NULL || tick_len == 0) 1637e0c4386eSCy Schubert ret->session_ticket = SSL_TEST_SESSION_TICKET_NO; 1638e0c4386eSCy Schubert else 1639e0c4386eSCy Schubert ret->session_ticket = SSL_TEST_SESSION_TICKET_YES; 1640e0c4386eSCy Schubert ret->compression = (SSL_get_current_compression(client.ssl) == NULL) 1641e0c4386eSCy Schubert ? SSL_TEST_COMPRESSION_NO 1642e0c4386eSCy Schubert : SSL_TEST_COMPRESSION_YES; 1643e0c4386eSCy Schubert if (sess_id == NULL || sess_id_len == 0) 1644e0c4386eSCy Schubert ret->session_id = SSL_TEST_SESSION_ID_NO; 1645e0c4386eSCy Schubert else 1646e0c4386eSCy Schubert ret->session_id = SSL_TEST_SESSION_ID_YES; 1647e0c4386eSCy Schubert ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call; 1648e0c4386eSCy Schubert 1649e0c4386eSCy Schubert if (extra->client.verify_callback == SSL_TEST_VERIFY_RETRY_ONCE 1650e0c4386eSCy Schubert && n_retries != -1) 1651e0c4386eSCy Schubert ret->result = SSL_TEST_SERVER_FAIL; 1652e0c4386eSCy Schubert 1653e0c4386eSCy Schubert #ifndef OPENSSL_NO_NEXTPROTONEG 1654e0c4386eSCy Schubert SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len); 1655e0c4386eSCy Schubert ret->client_npn_negotiated = dup_str(proto, proto_len); 1656e0c4386eSCy Schubert 1657e0c4386eSCy Schubert SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len); 1658e0c4386eSCy Schubert ret->server_npn_negotiated = dup_str(proto, proto_len); 1659e0c4386eSCy Schubert #endif 1660e0c4386eSCy Schubert 1661e0c4386eSCy Schubert SSL_get0_alpn_selected(client.ssl, &proto, &proto_len); 1662e0c4386eSCy Schubert ret->client_alpn_negotiated = dup_str(proto, proto_len); 1663e0c4386eSCy Schubert 1664e0c4386eSCy Schubert SSL_get0_alpn_selected(server.ssl, &proto, &proto_len); 1665e0c4386eSCy Schubert ret->server_alpn_negotiated = dup_str(proto, proto_len); 1666e0c4386eSCy Schubert 1667e0c4386eSCy Schubert if ((sess = SSL_get0_session(server.ssl)) != NULL) { 1668e0c4386eSCy Schubert SSL_SESSION_get0_ticket_appdata(sess, (void**)&tick, &tick_len); 1669e0c4386eSCy Schubert ret->result_session_ticket_app_data = OPENSSL_strndup((const char*)tick, tick_len); 1670e0c4386eSCy Schubert } 1671e0c4386eSCy Schubert 1672e0c4386eSCy Schubert ret->client_resumed = SSL_session_reused(client.ssl); 1673e0c4386eSCy Schubert ret->server_resumed = SSL_session_reused(server.ssl); 1674e0c4386eSCy Schubert 1675e0c4386eSCy Schubert cipher = SSL_CIPHER_get_name(SSL_get_current_cipher(client.ssl)); 1676e0c4386eSCy Schubert ret->cipher = dup_str((const unsigned char*)cipher, strlen(cipher)); 1677e0c4386eSCy Schubert 1678e0c4386eSCy Schubert if (session_out != NULL) 1679e0c4386eSCy Schubert *session_out = SSL_get1_session(client.ssl); 1680e0c4386eSCy Schubert if (serv_sess_out != NULL) { 1681e0c4386eSCy Schubert SSL_SESSION *tmp = SSL_get_session(server.ssl); 1682e0c4386eSCy Schubert 1683e0c4386eSCy Schubert /* 1684e0c4386eSCy Schubert * We create a fresh copy that is not in the server session ctx linked 1685e0c4386eSCy Schubert * list. 1686e0c4386eSCy Schubert */ 1687e0c4386eSCy Schubert if (tmp != NULL) 1688e0c4386eSCy Schubert *serv_sess_out = SSL_SESSION_dup(tmp); 1689e0c4386eSCy Schubert } 1690e0c4386eSCy Schubert 1691e0c4386eSCy Schubert if (SSL_get_peer_tmp_key(client.ssl, &tmp_key)) { 1692e0c4386eSCy Schubert ret->tmp_key_type = pkey_type(tmp_key); 1693e0c4386eSCy Schubert EVP_PKEY_free(tmp_key); 1694e0c4386eSCy Schubert } 1695e0c4386eSCy Schubert 1696e0c4386eSCy Schubert SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash); 1697e0c4386eSCy Schubert SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash); 1698e0c4386eSCy Schubert 1699e0c4386eSCy Schubert SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type); 1700e0c4386eSCy Schubert SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type); 1701e0c4386eSCy Schubert 1702e0c4386eSCy Schubert names = SSL_get0_peer_CA_list(client.ssl); 1703e0c4386eSCy Schubert if (names == NULL) 1704e0c4386eSCy Schubert ret->client_ca_names = NULL; 1705e0c4386eSCy Schubert else 1706e0c4386eSCy Schubert ret->client_ca_names = SSL_dup_CA_list(names); 1707e0c4386eSCy Schubert 1708e0c4386eSCy Schubert names = SSL_get0_peer_CA_list(server.ssl); 1709e0c4386eSCy Schubert if (names == NULL) 1710e0c4386eSCy Schubert ret->server_ca_names = NULL; 1711e0c4386eSCy Schubert else 1712e0c4386eSCy Schubert ret->server_ca_names = SSL_dup_CA_list(names); 1713e0c4386eSCy Schubert 1714e0c4386eSCy Schubert ret->server_cert_type = peer_pkey_type(client.ssl); 1715e0c4386eSCy Schubert ret->client_cert_type = peer_pkey_type(server.ssl); 1716e0c4386eSCy Schubert 1717e0c4386eSCy Schubert ctx_data_free_data(&server_ctx_data); 1718e0c4386eSCy Schubert ctx_data_free_data(&server2_ctx_data); 1719e0c4386eSCy Schubert ctx_data_free_data(&client_ctx_data); 1720e0c4386eSCy Schubert 1721e0c4386eSCy Schubert peer_free_data(&server); 1722e0c4386eSCy Schubert peer_free_data(&client); 1723e0c4386eSCy Schubert return ret; 1724e0c4386eSCy Schubert } 1725e0c4386eSCy Schubert 1726e0c4386eSCy Schubert HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, 1727e0c4386eSCy Schubert SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx, 1728e0c4386eSCy Schubert SSL_CTX *resume_client_ctx, 1729e0c4386eSCy Schubert const SSL_TEST_CTX *test_ctx) 1730e0c4386eSCy Schubert { 1731e0c4386eSCy Schubert HANDSHAKE_RESULT *result; 1732e0c4386eSCy Schubert SSL_SESSION *session = NULL, *serv_sess = NULL; 1733e0c4386eSCy Schubert 1734e0c4386eSCy Schubert result = do_handshake_internal(server_ctx, server2_ctx, client_ctx, 1735e0c4386eSCy Schubert test_ctx, &test_ctx->extra, 1736e0c4386eSCy Schubert NULL, NULL, &session, &serv_sess); 1737e0c4386eSCy Schubert if (result == NULL 1738e0c4386eSCy Schubert || test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME 1739e0c4386eSCy Schubert || result->result == SSL_TEST_INTERNAL_ERROR) 1740e0c4386eSCy Schubert goto end; 1741e0c4386eSCy Schubert 1742e0c4386eSCy Schubert if (result->result != SSL_TEST_SUCCESS) { 1743e0c4386eSCy Schubert result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED; 1744e0c4386eSCy Schubert goto end; 1745e0c4386eSCy Schubert } 1746e0c4386eSCy Schubert 1747e0c4386eSCy Schubert HANDSHAKE_RESULT_free(result); 1748e0c4386eSCy Schubert /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */ 1749e0c4386eSCy Schubert result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx, 1750e0c4386eSCy Schubert test_ctx, &test_ctx->resume_extra, 1751e0c4386eSCy Schubert session, serv_sess, NULL, NULL); 1752e0c4386eSCy Schubert end: 1753e0c4386eSCy Schubert SSL_SESSION_free(session); 1754e0c4386eSCy Schubert SSL_SESSION_free(serv_sess); 1755e0c4386eSCy Schubert return result; 1756e0c4386eSCy Schubert } 1757