HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
                               SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx)
{
    SSL *server, *client;
    BIO *client_to_server, *server_to_client;
    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;
    peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;
    handshake_status_t status = HANDSHAKE_RETRY;
    unsigned char* tick = NULL;
    size_t tick_len = 0;
    SSL_SESSION* sess = NULL;
    const unsigned char *proto = NULL;
    unsigned int proto_len = 0;
    memset(&server_ctx_data, 0, sizeof(server_ctx_data));
    memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
    memset(&client_ctx_data, 0, sizeof(client_ctx_data));
    configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx,
                            &server_ctx_data, &server2_ctx_data, &client_ctx_data);
    server = SSL_new(server_ctx);
    client = SSL_new(client_ctx);
    OPENSSL_assert(server != NULL && client != NULL);
    configure_handshake_ssl(server, client, test_ctx);
    memset(&server_ex_data, 0, sizeof(server_ex_data));
    memset(&client_ex_data, 0, sizeof(client_ex_data));
    ret->result = SSL_TEST_INTERNAL_ERROR;
    client_to_server = BIO_new(BIO_s_mem());
    server_to_client = BIO_new(BIO_s_mem());
    OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);
    BIO_set_nbio(client_to_server, 1);
    BIO_set_nbio(server_to_client, 1);
    SSL_set_connect_state(client);
    SSL_set_accept_state(server);
    SSL_set_bio(client, server_to_client, client_to_server);
    OPENSSL_assert(BIO_up_ref(server_to_client) > 0);
    OPENSSL_assert(BIO_up_ref(client_to_server) > 0);
    SSL_set_bio(server, client_to_server, server_to_client);
    ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
    OPENSSL_assert(ex_data_idx >= 0);
    OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,
                                   &server_ex_data) == 1);
    OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,
                                   &client_ex_data) == 1);
    SSL_set_info_callback(server, &info_cb);
    SSL_set_info_callback(client, &info_cb);
    for(;;) {
        if (client_turn) {
            client_status = do_handshake_step(client);
            status = handshake_status(client_status, server_status,
                                      1  );
        } else {
            server_status = do_handshake_step(server);
            status = handshake_status(server_status, client_status,
                                      0  );
        }
        switch (status) {
        case HANDSHAKE_SUCCESS:
            ret->result = SSL_TEST_SUCCESS;
            goto err;
        case CLIENT_ERROR:
            ret->result = SSL_TEST_CLIENT_FAIL;
            goto err;
        case SERVER_ERROR:
            ret->result = SSL_TEST_SERVER_FAIL;
            goto err;
        case INTERNAL_ERROR:
            ret->result = SSL_TEST_INTERNAL_ERROR;
            goto err;
        case HANDSHAKE_RETRY:
            client_turn ^= 1;
            break;
        }
    }
 err:
    ret->server_alert_sent = server_ex_data.alert_sent;
    ret->server_alert_received = client_ex_data.alert_received;
    ret->client_alert_sent = client_ex_data.alert_sent;
    ret->client_alert_received = server_ex_data.alert_received;
    ret->server_protocol = SSL_version(server);
    ret->client_protocol = SSL_version(client);
    ret->servername = server_ex_data.servername;
    if ((sess = SSL_get0_session(client)) != NULL)
        SSL_SESSION_get0_ticket(sess, &tick, &tick_len);
    if (tick == NULL || tick_len == 0)
        ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;
    else
        ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
    ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
    SSL_get0_next_proto_negotiated(client, &proto, &proto_len);
    ret->client_npn_negotiated = dup_str(proto, proto_len);
    SSL_get0_next_proto_negotiated(server, &proto, &proto_len);
    ret->server_npn_negotiated = dup_str(proto, proto_len);
    SSL_get0_alpn_selected(client, &proto, &proto_len);
    ret->client_alpn_negotiated = dup_str(proto, proto_len);
    SSL_get0_alpn_selected(server, &proto, &proto_len);
    ret->server_alpn_negotiated = dup_str(proto, proto_len);
    ctx_data_free_data(&server_ctx_data);
    ctx_data_free_data(&server2_ctx_data);
    ctx_data_free_data(&client_ctx_data);
    SSL_free(server);
    SSL_free(client);
    return ret;
}
['HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,\n                               SSL_CTX *client_ctx, const SSL_TEST_CTX *test_ctx)\n{\n    SSL *server, *client;\n    BIO *client_to_server, *server_to_client;\n    HANDSHAKE_EX_DATA server_ex_data, client_ex_data;\n    CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;\n    HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();\n    int client_turn = 1;\n    peer_status_t client_status = PEER_RETRY, server_status = PEER_RETRY;\n    handshake_status_t status = HANDSHAKE_RETRY;\n    unsigned char* tick = NULL;\n    size_t tick_len = 0;\n    SSL_SESSION* sess = NULL;\n    const unsigned char *proto = NULL;\n    unsigned int proto_len = 0;\n    memset(&server_ctx_data, 0, sizeof(server_ctx_data));\n    memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));\n    memset(&client_ctx_data, 0, sizeof(client_ctx_data));\n    configure_handshake_ctx(server_ctx, server2_ctx, client_ctx, test_ctx,\n                            &server_ctx_data, &server2_ctx_data, &client_ctx_data);\n    server = SSL_new(server_ctx);\n    client = SSL_new(client_ctx);\n    OPENSSL_assert(server != NULL && client != NULL);\n    configure_handshake_ssl(server, client, test_ctx);\n    memset(&server_ex_data, 0, sizeof(server_ex_data));\n    memset(&client_ex_data, 0, sizeof(client_ex_data));\n    ret->result = SSL_TEST_INTERNAL_ERROR;\n    client_to_server = BIO_new(BIO_s_mem());\n    server_to_client = BIO_new(BIO_s_mem());\n    OPENSSL_assert(client_to_server != NULL && server_to_client != NULL);\n    BIO_set_nbio(client_to_server, 1);\n    BIO_set_nbio(server_to_client, 1);\n    SSL_set_connect_state(client);\n    SSL_set_accept_state(server);\n    SSL_set_bio(client, server_to_client, client_to_server);\n    OPENSSL_assert(BIO_up_ref(server_to_client) > 0);\n    OPENSSL_assert(BIO_up_ref(client_to_server) > 0);\n    SSL_set_bio(server, client_to_server, server_to_client);\n    ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);\n    OPENSSL_assert(ex_data_idx >= 0);\n    OPENSSL_assert(SSL_set_ex_data(server, ex_data_idx,\n                                   &server_ex_data) == 1);\n    OPENSSL_assert(SSL_set_ex_data(client, ex_data_idx,\n                                   &client_ex_data) == 1);\n    SSL_set_info_callback(server, &info_cb);\n    SSL_set_info_callback(client, &info_cb);\n    for(;;) {\n        if (client_turn) {\n            client_status = do_handshake_step(client);\n            status = handshake_status(client_status, server_status,\n                                      1  );\n        } else {\n            server_status = do_handshake_step(server);\n            status = handshake_status(server_status, client_status,\n                                      0  );\n        }\n        switch (status) {\n        case HANDSHAKE_SUCCESS:\n            ret->result = SSL_TEST_SUCCESS;\n            goto err;\n        case CLIENT_ERROR:\n            ret->result = SSL_TEST_CLIENT_FAIL;\n            goto err;\n        case SERVER_ERROR:\n            ret->result = SSL_TEST_SERVER_FAIL;\n            goto err;\n        case INTERNAL_ERROR:\n            ret->result = SSL_TEST_INTERNAL_ERROR;\n            goto err;\n        case HANDSHAKE_RETRY:\n            client_turn ^= 1;\n            break;\n        }\n    }\n err:\n    ret->server_alert_sent = server_ex_data.alert_sent;\n    ret->server_alert_received = client_ex_data.alert_received;\n    ret->client_alert_sent = client_ex_data.alert_sent;\n    ret->client_alert_received = server_ex_data.alert_received;\n    ret->server_protocol = SSL_version(server);\n    ret->client_protocol = SSL_version(client);\n    ret->servername = server_ex_data.servername;\n    if ((sess = SSL_get0_session(client)) != NULL)\n        SSL_SESSION_get0_ticket(sess, &tick, &tick_len);\n    if (tick == NULL || tick_len == 0)\n        ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;\n    else\n        ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;\n    ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;\n    SSL_get0_next_proto_negotiated(client, &proto, &proto_len);\n    ret->client_npn_negotiated = dup_str(proto, proto_len);\n    SSL_get0_next_proto_negotiated(server, &proto, &proto_len);\n    ret->server_npn_negotiated = dup_str(proto, proto_len);\n    SSL_get0_alpn_selected(client, &proto, &proto_len);\n    ret->client_alpn_negotiated = dup_str(proto, proto_len);\n    SSL_get0_alpn_selected(server, &proto, &proto_len);\n    ret->server_alpn_negotiated = dup_str(proto, proto_len);\n    ctx_data_free_data(&server_ctx_data);\n    ctx_data_free_data(&server2_ctx_data);\n    ctx_data_free_data(&client_ctx_data);\n    SSL_free(server);\n    SSL_free(client);\n    return ret;\n}']