X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fhandshake_helper.c;h=509a8f6ede357e64683b8f382a609f79cd91c389;hp=90e18fcaaaf52f885270dacd3bb90b573f9ad049;hb=a69de3f2014ab55329f43633714c9c153cb5cb30;hpb=767ccc3b77cde82c46ab4af541663f6c80e538d3 diff --git a/test/handshake_helper.c b/test/handshake_helper.c index 90e18fcaaa..509a8f6ede 100644 --- a/test/handshake_helper.c +++ b/test/handshake_helper.c @@ -12,6 +12,14 @@ #include #include #include +#ifndef OPENSSL_NO_SRP +#include +#endif + +#ifndef OPENSSL_NO_SOCK +# define USE_SOCKETS +# include "e_os.h" +#endif #include "handshake_helper.h" #include "testutil.h" @@ -31,6 +39,8 @@ void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result) OPENSSL_free(result->server_npn_negotiated); OPENSSL_free(result->client_alpn_negotiated); OPENSSL_free(result->server_alpn_negotiated); + sk_X509_NAME_pop_free(result->server_ca_names, X509_NAME_free); + sk_X509_NAME_pop_free(result->client_ca_names, X509_NAME_free); OPENSSL_free(result); } @@ -52,6 +62,8 @@ typedef struct ctx_data_st { size_t npn_protocols_len; unsigned char *alpn_protocols; size_t alpn_protocols_len; + char *srp_user; + char *srp_password; } CTX_DATA; /* |ctx_data| itself is stack-allocated. */ @@ -61,6 +73,10 @@ static void ctx_data_free_data(CTX_DATA *ctx_data) ctx_data->npn_protocols = NULL; OPENSSL_free(ctx_data->alpn_protocols); ctx_data->alpn_protocols = NULL; + OPENSSL_free(ctx_data->srp_user); + ctx_data->srp_user = NULL; + OPENSSL_free(ctx_data->srp_password); + ctx_data->srp_password = NULL; } static int ex_data_idx; @@ -123,6 +139,67 @@ static int select_server_ctx(SSL *s, void *arg, int ignore) } } +static int early_select_server_ctx(SSL *s, void *arg, int ignore) +{ + const char *servername; + const unsigned char *p; + size_t len, remaining; + HANDSHAKE_EX_DATA *ex_data = + (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); + + /* + * The server_name extension was given too much extensibility when it + * was written, so parsing the normal case is a bit complex. + */ + if (!SSL_early_get0_ext(s, TLSEXT_TYPE_server_name, &p, &remaining) || + remaining <= 2) + return 0; + /* Extract the length of the supplied list of names. */ + len = (*(p++) << 1); + len += *(p++); + if (len + 2 != remaining) + return 0; + remaining = len; + /* + * The list in practice only has a single element, so we only consider + * the first one. + */ + if (remaining == 0 || *p++ != TLSEXT_NAMETYPE_host_name) + return 0; + remaining--; + /* Now we can finally pull out the byte array with the actual hostname. */ + if (remaining <= 2) + return 0; + len = (*(p++) << 1); + len += *(p++); + if (len + 2 > remaining) + return 0; + remaining = len; + servername = (const char *)p; + + if (len == strlen("server2") && strncmp(servername, "server2", len) == 0) { + SSL_CTX *new_ctx = arg; + SSL_set_SSL_CTX(s, new_ctx); + /* + * Copy over all the SSL_CTX options - reasonable behavior + * allows testing of cases where the options between two + * contexts differ/conflict + */ + SSL_clear_options(s, 0xFFFFFFFFL); + SSL_set_options(s, SSL_CTX_get_options(new_ctx)); + + ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; + return 1; + } else if (len == strlen("server1") && + strncmp(servername, "server1", len) == 0) { + ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; + return 1; + } else if (ignore) { + ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; + return 1; + } + return 0; +} /* * (RFC 6066): * If the server understood the ClientHello extension but @@ -144,6 +221,50 @@ static int servername_reject_cb(SSL *s, int *ad, void *arg) return select_server_ctx(s, arg, 0); } +static int early_ignore_cb(SSL *s, int *al, void *arg) +{ + if (!early_select_server_ctx(s, arg, 1)) { + *al = SSL_AD_UNRECOGNIZED_NAME; + return 0; + } + return 1; +} + +static int early_reject_cb(SSL *s, int *al, void *arg) +{ + if (!early_select_server_ctx(s, arg, 0)) { + *al = SSL_AD_UNRECOGNIZED_NAME; + return 0; + } + return 1; +} + +static int early_nov12_cb(SSL *s, int *al, void *arg) +{ + int ret; + unsigned int v; + const unsigned char *p; + + v = SSL_early_get0_legacy_version(s); + if (v > TLS1_2_VERSION || v < SSL3_VERSION) { + *al = SSL_AD_PROTOCOL_VERSION; + return 0; + } + (void)SSL_early_get0_session_id(s, &p); + if (p == NULL || + SSL_early_get0_random(s, &p) == 0 || + SSL_early_get0_ciphers(s, &p) == 0 || + SSL_early_get0_compression_methods(s, &p) == 0) { + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + ret = early_select_server_ctx(s, arg, 0); + SSL_set_max_proto_version(s, TLS1_1_VERSION); + if (!ret) + *al = SSL_AD_UNRECOGNIZED_NAME; + return ret; +} + static unsigned char dummy_ocsp_resp_good_val = 0xff; static unsigned char dummy_ocsp_resp_bad_val = 0xfe; @@ -297,8 +418,30 @@ static int server_alpn_cb(SSL *s, const unsigned char **out, *out = tmp_out; /* Unlike NPN, we don't tolerate a mismatch. */ return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK - : SSL_TLSEXT_ERR_NOACK; + : SSL_TLSEXT_ERR_ALERT_FATAL; +} + +#ifndef OPENSSL_NO_SRP +static char *client_srp_cb(SSL *s, void *arg) +{ + CTX_DATA *ctx_data = (CTX_DATA*)(arg); + return OPENSSL_strdup(ctx_data->srp_password); +} + +static int server_srp_cb(SSL *s, int *ad, void *arg) +{ + CTX_DATA *ctx_data = (CTX_DATA*)(arg); + if (strcmp(ctx_data->srp_user, SSL_get_srp_username(s)) != 0) + return SSL3_AL_FATAL; + if (SSL_set_srp_server_param_pw(s, ctx_data->srp_user, + ctx_data->srp_password, + "2048" /* known group */) < 0) { + *ad = SSL_AD_INTERNAL_ERROR; + return SSL3_AL_FATAL; + } + return SSL_ERROR_NONE; } +#endif /* !OPENSSL_NO_SRP */ /* * Configure callbacks and other properties that can't be set directly @@ -333,11 +476,14 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, NULL); break; - default: + case SSL_TEST_VERIFY_NONE: break; } - /* link the two contexts for SNI purposes */ + /* + * Link the two contexts for SNI purposes. + * Also do early callbacks here, as setting both early and SNI is bad. + */ switch (extra->server.servername_callback) { case SSL_TEST_SERVERNAME_IGNORE_MISMATCH: SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb); @@ -347,8 +493,16 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb); SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx); break; - default: + case SSL_TEST_SERVERNAME_CB_NONE: + break; + case SSL_TEST_SERVERNAME_EARLY_IGNORE_MISMATCH: + SSL_CTX_set_early_cb(server_ctx, early_ignore_cb, server2_ctx); + break; + case SSL_TEST_SERVERNAME_EARLY_REJECT_MISMATCH: + SSL_CTX_set_early_cb(server_ctx, early_reject_cb, server2_ctx); break; + case SSL_TEST_SERVERNAME_EARLY_NO_V12: + SSL_CTX_set_early_cb(server_ctx, early_nov12_cb, server2_ctx); } if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) { @@ -378,16 +532,16 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, parse_protos(extra->server.npn_protocols, &server_ctx_data->npn_protocols, &server_ctx_data->npn_protocols_len); - SSL_CTX_set_next_protos_advertised_cb(server_ctx, server_npn_cb, - server_ctx_data); + SSL_CTX_set_npn_advertised_cb(server_ctx, server_npn_cb, + server_ctx_data); } if (extra->server2.npn_protocols != NULL) { parse_protos(extra->server2.npn_protocols, &server2_ctx_data->npn_protocols, &server2_ctx_data->npn_protocols_len); TEST_check(server2_ctx != NULL); - SSL_CTX_set_next_protos_advertised_cb(server2_ctx, server_npn_cb, - server2_ctx_data); + SSL_CTX_set_npn_advertised_cb(server2_ctx, server_npn_cb, + server2_ctx_data); } if (extra->client.npn_protocols != NULL) { parse_protos(extra->client.npn_protocols, @@ -446,6 +600,27 @@ static void configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, break; } #endif +#ifndef OPENSSL_NO_SRP + if (extra->server.srp_user != NULL) { + SSL_CTX_set_srp_username_callback(server_ctx, server_srp_cb); + server_ctx_data->srp_user = OPENSSL_strdup(extra->server.srp_user); + server_ctx_data->srp_password = OPENSSL_strdup(extra->server.srp_password); + SSL_CTX_set_srp_cb_arg(server_ctx, server_ctx_data); + } + if (extra->server2.srp_user != NULL) { + TEST_check(server2_ctx != NULL); + SSL_CTX_set_srp_username_callback(server2_ctx, server_srp_cb); + server2_ctx_data->srp_user = OPENSSL_strdup(extra->server2.srp_user); + server2_ctx_data->srp_password = OPENSSL_strdup(extra->server2.srp_password); + SSL_CTX_set_srp_cb_arg(server2_ctx, server2_ctx_data); + } + if (extra->client.srp_user != NULL) { + TEST_check(SSL_CTX_set_srp_username(client_ctx, extra->client.srp_user)); + SSL_CTX_set_srp_client_pwd_callback(client_ctx, client_srp_cb); + client_ctx_data->srp_password = OPENSSL_strdup(extra->client.srp_password); + SSL_CTX_set_srp_cb_arg(client_ctx, client_ctx_data); + } +#endif /* !OPENSSL_NO_SRP */ } /* Configure per-SSL callbacks and other properties. */ @@ -461,7 +636,8 @@ static void configure_handshake_ssl(SSL *server, SSL *client, typedef enum { PEER_SUCCESS, PEER_RETRY, - PEER_ERROR + PEER_ERROR, + PEER_WAITING } peer_status_t; /* An SSL object and associated read-write buffers. */ @@ -583,6 +759,136 @@ static void do_app_data_step(PEER *peer) } } +static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer) +{ + int ret; + char buf; + + if (peer->status == PEER_SUCCESS) { + /* + * We are a client that succeeded this step previously, but the server + * wanted to retry. Probably there is a no_renegotiation warning alert + * waiting for us. Attempt to continue the handshake. + */ + peer->status = PEER_RETRY; + do_handshake_step(peer); + return; + } + + TEST_check(peer->status == PEER_RETRY); + TEST_check(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER + || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT); + + /* Reset the count of the amount of app data we need to read/write */ + peer->bytes_to_write = peer->bytes_to_read = test_ctx->app_data_size; + + /* Check if we are the peer that is going to initiate */ + if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER + && SSL_is_server(peer->ssl)) + || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT + && !SSL_is_server(peer->ssl))) { + /* + * If we already asked for a renegotiation then fall through to the + * SSL_read() below. + */ + if (!SSL_renegotiate_pending(peer->ssl)) { + /* + * If we are the client we will always attempt to resume the + * session. The server may or may not resume dependant on the + * setting of SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION + */ + if (SSL_is_server(peer->ssl)) { + ret = SSL_renegotiate(peer->ssl); + } else { + if (test_ctx->extra.client.reneg_ciphers != NULL) { + if (!SSL_set_cipher_list(peer->ssl, + test_ctx->extra.client.reneg_ciphers)) { + peer->status = PEER_ERROR; + return; + } + ret = SSL_renegotiate(peer->ssl); + } else { + ret = SSL_renegotiate_abbreviated(peer->ssl); + } + } + if (!ret) { + peer->status = PEER_ERROR; + return; + } + do_handshake_step(peer); + /* + * If status is PEER_RETRY it means we're waiting on the peer to + * continue the handshake. As far as setting up the renegotiation is + * concerned that is a success. The next step will continue the + * handshake to its conclusion. + * + * If status is PEER_SUCCESS then we are the server and we have + * successfully sent the HelloRequest. We need to continue to wait + * until the handshake arrives from the client. + */ + if (peer->status == PEER_RETRY) + peer->status = PEER_SUCCESS; + else if (peer->status == PEER_SUCCESS) + peer->status = PEER_RETRY; + return; + } + } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) { + if (SSL_is_server(peer->ssl) + != (test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER)) { + peer->status = PEER_SUCCESS; + return; + } + + ret = SSL_key_update(peer->ssl, test_ctx->key_update_type); + if (!ret) { + peer->status = PEER_ERROR; + return; + } + do_handshake_step(peer); + /* + * This is a one step handshake. We shouldn't get anything other than + * PEER_SUCCESS + */ + if (peer->status != PEER_SUCCESS) + peer->status = PEER_ERROR; + return; + } + + /* + * The SSL object is still expecting app data, even though it's going to + * get a handshake message. We try to read, and it should fail - after which + * we should be in a handshake + */ + ret = SSL_read(peer->ssl, &buf, sizeof(buf)); + if (ret >= 0) { + /* + * We're not actually expecting data - we're expecting a reneg to + * start + */ + peer->status = PEER_ERROR; + return; + } else { + int error = SSL_get_error(peer->ssl, ret); + if (error != SSL_ERROR_WANT_READ) { + peer->status = PEER_ERROR; + return; + } + /* If we're not in init yet then we're not done with setup yet */ + if (!SSL_in_init(peer->ssl)) + return; + } + + peer->status = PEER_SUCCESS; +} + + /* * RFC 5246 says: * @@ -609,47 +915,81 @@ static void do_shutdown_step(PEER *peer) peer->status = PEER_SUCCESS; } else if (ret < 0) { /* On 0, we retry. */ int error = SSL_get_error(peer->ssl, ret); - /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */ - if (error != SSL_ERROR_WANT_READ) + + if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE) peer->status = PEER_ERROR; } } typedef enum { HANDSHAKE, + RENEG_APPLICATION_DATA, + RENEG_SETUP, + RENEG_HANDSHAKE, APPLICATION_DATA, SHUTDOWN, CONNECTION_DONE } connect_phase_t; -static connect_phase_t next_phase(connect_phase_t phase) +static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx, + connect_phase_t phase) { switch (phase) { case HANDSHAKE: + if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER + || test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER) + return RENEG_APPLICATION_DATA; + return APPLICATION_DATA; + case RENEG_APPLICATION_DATA: + return RENEG_SETUP; + case RENEG_SETUP: + if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER + || test_ctx->handshake_mode + == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) + return APPLICATION_DATA; + return RENEG_HANDSHAKE; + case RENEG_HANDSHAKE: return APPLICATION_DATA; case APPLICATION_DATA: return SHUTDOWN; case SHUTDOWN: return CONNECTION_DONE; - default: - TEST_check(0); /* Should never call next_phase when done. */ + case CONNECTION_DONE: + TEST_check(0); + break; } + return -1; } -static void do_connect_step(PEER *peer, connect_phase_t phase) +static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer, + connect_phase_t phase) { switch (phase) { case HANDSHAKE: do_handshake_step(peer); break; + case RENEG_APPLICATION_DATA: + do_app_data_step(peer); + break; + case RENEG_SETUP: + do_reneg_setup_step(test_ctx, peer); + break; + case RENEG_HANDSHAKE: + do_handshake_step(peer); + break; case APPLICATION_DATA: do_app_data_step(peer); break; case SHUTDOWN: do_shutdown_step(peer); break; - default: + case CONNECTION_DONE: TEST_check(0); + break; } } @@ -677,11 +1017,16 @@ static handshake_status_t handshake_status(peer_status_t last_status, int client_spoke_last) { switch (last_status) { + case PEER_WAITING: + /* Shouldn't ever happen */ + return INTERNAL_ERROR; + case PEER_SUCCESS: switch (previous_status) { case PEER_SUCCESS: /* Both succeeded. */ return HANDSHAKE_SUCCESS; + case PEER_WAITING: case PEER_RETRY: /* Let the first peer finish. */ return HANDSHAKE_RETRY; @@ -694,18 +1039,13 @@ static handshake_status_t handshake_status(peer_status_t last_status, } case PEER_RETRY: - if (previous_status == PEER_RETRY) { - /* Neither peer is done. */ - return HANDSHAKE_RETRY; - } else { - /* - * Deadlock: second peer is waiting for more input while first - * peer thinks they're done (no more input is coming). - */ - return INTERNAL_ERROR; - } + return HANDSHAKE_RETRY; + case PEER_ERROR: switch (previous_status) { + case PEER_WAITING: + /* The client failed immediately before sending the ClientHello */ + return client_spoke_last ? CLIENT_ERROR : INTERNAL_ERROR; case PEER_SUCCESS: /* * First peer succeeded but second peer errored. @@ -732,7 +1072,7 @@ static char *dup_str(const unsigned char *in, size_t len) { char *ret; - if(len == 0) + if (len == 0) return NULL; /* Assert that the string does not contain NUL-bytes. */ @@ -742,6 +1082,133 @@ static char *dup_str(const unsigned char *in, size_t len) return ret; } +static int pkey_type(EVP_PKEY *pkey) +{ + int nid = EVP_PKEY_id(pkey); + +#ifndef OPENSSL_NO_EC + if (nid == EVP_PKEY_EC) { + const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); + return EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); + } +#endif + return nid; +} + +static int peer_pkey_type(SSL *s) +{ + X509 *x = SSL_get_peer_certificate(s); + + if (x != NULL) { + int nid = pkey_type(X509_get0_pubkey(x)); + + X509_free(x); + return nid; + } + return NID_undef; +} + +#if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) +static int set_sock_as_sctp(int sock) +{ + /* + * For SCTP we have to set various options on the socket prior to + * connecting. This is done automatically by BIO_new_dgram_sctp(). + * We don't actually need the created BIO though so we free it again + * immediately. + */ + BIO *tmpbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE); + + if (tmpbio == NULL) + return 0; + BIO_free(tmpbio); + + return 1; +} + +static int create_sctp_socks(int *ssock, int *csock) +{ + BIO_ADDRINFO *res = NULL; + const BIO_ADDRINFO *ai = NULL; + int lsock = INVALID_SOCKET, asock = INVALID_SOCKET; + int consock = INVALID_SOCKET; + int ret = 0; + int family = 0; + + if (!BIO_sock_init()) + return 0; + + /* + * Port is 4463. It could be anything. It will fail if it's already being + * used for some other SCTP service. It seems unlikely though so we don't + * worry about it here. + */ + if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_SERVER, family, SOCK_STREAM, + IPPROTO_SCTP, &res)) + return 0; + + for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) { + family = BIO_ADDRINFO_family(ai); + lsock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); + if (lsock == INVALID_SOCKET) { + /* Maybe the kernel doesn't support the socket family, even if + * BIO_lookup() added it in the returned result... + */ + continue; + } + + if (!set_sock_as_sctp(lsock) + || !BIO_listen(lsock, BIO_ADDRINFO_address(ai), + BIO_SOCK_REUSEADDR)) { + BIO_closesocket(lsock); + lsock = INVALID_SOCKET; + continue; + } + + /* Success, don't try any more addresses */ + break; + } + + if (lsock == INVALID_SOCKET) + goto err; + + BIO_ADDRINFO_free(res); + res = NULL; + + if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_CLIENT, family, SOCK_STREAM, + IPPROTO_SCTP, &res)) + goto err; + + consock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0); + if (consock == INVALID_SOCKET) + goto err; + + if (!set_sock_as_sctp(consock) + || !BIO_connect(consock, BIO_ADDRINFO_address(res), 0) + || !BIO_socket_nbio(consock, 1)) + goto err; + + asock = BIO_accept_ex(lsock, NULL, BIO_SOCK_NONBLOCK); + if (asock == INVALID_SOCKET) + goto err; + + *csock = consock; + *ssock = asock; + consock = asock = INVALID_SOCKET; + ret = 1; + + err: + BIO_ADDRINFO_free(res); + if (consock != INVALID_SOCKET) + BIO_closesocket(consock); + if (lsock != INVALID_SOCKET) + BIO_closesocket(lsock); + if (asock != INVALID_SOCKET) + BIO_closesocket(asock); + return ret; +} +#endif + /* * Note that |extra| points to the correct client/server configuration * within |test_ctx|. When configuring the handshake, general mode settings @@ -761,11 +1228,11 @@ static HANDSHAKE_RESULT *do_handshake_internal( SSL_SESSION *session_in, SSL_SESSION **session_out) { PEER server, client; - BIO *client_to_server, *server_to_client; + BIO *client_to_server = NULL, *server_to_client = NULL; HANDSHAKE_EX_DATA server_ex_data, client_ex_data; CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data; HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new(); - int client_turn = 1; + int client_turn = 1, client_turn_count = 0; connect_phase_t phase = HANDSHAKE; handshake_status_t status = HANDSHAKE_RETRY; const unsigned char* tick = NULL; @@ -774,6 +1241,9 @@ static HANDSHAKE_RESULT *do_handshake_internal( const unsigned char *proto = NULL; /* API dictates unsigned int rather than size_t. */ unsigned int proto_len = 0; + EVP_PKEY *tmp_key; + const STACK_OF(X509_NAME) *names; + time_t start; memset(&server_ctx_data, 0, sizeof(server_ctx_data)); memset(&server2_ctx_data, 0, sizeof(server2_ctx_data)); @@ -803,8 +1273,19 @@ static HANDSHAKE_RESULT *do_handshake_internal( ret->result = SSL_TEST_INTERNAL_ERROR; - client_to_server = BIO_new(BIO_s_mem()); - server_to_client = BIO_new(BIO_s_mem()); + if (test_ctx->use_sctp) { +#if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK) + int csock, ssock; + + if (create_sctp_socks(&ssock, &csock)) { + client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE); + server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE); + } +#endif + } else { + client_to_server = BIO_new(BIO_s_mem()); + server_to_client = BIO_new(BIO_s_mem()); + } TEST_check(client_to_server != NULL); TEST_check(server_to_client != NULL); @@ -817,10 +1298,15 @@ static HANDSHAKE_RESULT *do_handshake_internal( SSL_set_accept_state(server.ssl); /* The bios are now owned by the SSL object. */ - SSL_set_bio(client.ssl, server_to_client, client_to_server); - TEST_check(BIO_up_ref(server_to_client) > 0); - TEST_check(BIO_up_ref(client_to_server) > 0); - SSL_set_bio(server.ssl, client_to_server, server_to_client); + if (test_ctx->use_sctp) { + SSL_set_bio(client.ssl, client_to_server, client_to_server); + SSL_set_bio(server.ssl, server_to_client, server_to_client); + } else { + SSL_set_bio(client.ssl, server_to_client, client_to_server); + TEST_check(BIO_up_ref(server_to_client) > 0); + TEST_check(BIO_up_ref(client_to_server) > 0); + SSL_set_bio(server.ssl, client_to_server, server_to_client); + } ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL); TEST_check(ex_data_idx >= 0); @@ -831,7 +1317,10 @@ static HANDSHAKE_RESULT *do_handshake_internal( SSL_set_info_callback(server.ssl, &info_cb); SSL_set_info_callback(client.ssl, &info_cb); - client.status = server.status = PEER_RETRY; + client.status = PEER_RETRY; + server.status = PEER_WAITING; + + start = time(NULL); /* * Half-duplex handshake loop. @@ -843,18 +1332,21 @@ static HANDSHAKE_RESULT *do_handshake_internal( */ for(;;) { if (client_turn) { - do_connect_step(&client, phase); + do_connect_step(test_ctx, &client, phase); status = handshake_status(client.status, server.status, 1 /* client went last */); + if (server.status == PEER_WAITING) + server.status = PEER_RETRY; } else { - do_connect_step(&server, phase); + do_connect_step(test_ctx, &server, phase); status = handshake_status(server.status, client.status, 0 /* server went last */); } switch (status) { case HANDSHAKE_SUCCESS: - phase = next_phase(phase); + client_turn_count = 0; + phase = next_phase(test_ctx, phase); if (phase == CONNECTION_DONE) { ret->result = SSL_TEST_SUCCESS; goto err; @@ -879,8 +1371,36 @@ static HANDSHAKE_RESULT *do_handshake_internal( ret->result = SSL_TEST_INTERNAL_ERROR; goto err; case HANDSHAKE_RETRY: - /* Continue. */ - client_turn ^= 1; + if (test_ctx->use_sctp) { + if (time(NULL) - start > 3) { + /* + * We've waited for too long. Give up. + */ + ret->result = SSL_TEST_INTERNAL_ERROR; + goto err; + } + /* + * With "real" sockets we only swap to processing the peer + * if they are expecting to retry. Otherwise we just retry the + * same endpoint again. + */ + if ((client_turn && server.status == PEER_RETRY) + || (!client_turn && client.status == PEER_RETRY)) + client_turn ^= 1; + } else { + if (client_turn_count++ >= 2000) { + /* + * At this point, there's been so many PEER_RETRY in a row + * that it's likely both sides are stuck waiting for a read. + * It's time to give up. + */ + ret->result = SSL_TEST_INTERNAL_ERROR; + goto err; + } + + /* Continue. */ + client_turn ^= 1; + } break; } } @@ -900,6 +1420,9 @@ static HANDSHAKE_RESULT *do_handshake_internal( ret->session_ticket = SSL_TEST_SESSION_TICKET_NO; else ret->session_ticket = SSL_TEST_SESSION_TICKET_YES; + ret->compression = (SSL_get_current_compression(client.ssl) == NULL) + ? SSL_TEST_COMPRESSION_NO + : SSL_TEST_COMPRESSION_YES; ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call; #ifndef OPENSSL_NO_NEXTPROTONEG @@ -922,6 +1445,32 @@ static HANDSHAKE_RESULT *do_handshake_internal( if (session_out != NULL) *session_out = SSL_get1_session(client.ssl); + if (SSL_get_server_tmp_key(client.ssl, &tmp_key)) { + ret->tmp_key_type = pkey_type(tmp_key); + EVP_PKEY_free(tmp_key); + } + + SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash); + SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash); + + SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type); + SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type); + + names = SSL_get0_peer_CA_list(client.ssl); + if (names == NULL) + ret->client_ca_names = NULL; + else + ret->client_ca_names = SSL_dup_CA_list(names); + + names = SSL_get0_peer_CA_list(server.ssl); + if (names == NULL) + ret->server_ca_names = NULL; + else + ret->server_ca_names = SSL_dup_CA_list(names); + + ret->server_cert_type = peer_pkey_type(client.ssl); + ret->client_cert_type = peer_pkey_type(server.ssl); + ctx_data_free_data(&server_ctx_data); ctx_data_free_data(&server2_ctx_data); ctx_data_free_data(&client_ctx_data); @@ -942,11 +1491,9 @@ HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx, result = do_handshake_internal(server_ctx, server2_ctx, client_ctx, test_ctx, &test_ctx->extra, NULL, &session); - if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_SIMPLE) + if (test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME) goto end; - TEST_check(test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RESUME); - if (result->result != SSL_TEST_SUCCESS) { result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED; goto end;