static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
{
    const tls_curve_info *cinfo;
    if (curve[0])
        return 1;
    if ((curve[1] < 1) || ((size_t)curve[1] >
                           sizeof(nid_list) / sizeof(nid_list[0])))
        return 0;
    cinfo = &nid_list[curve[1] - 1];
# ifdef OPENSSL_NO_EC2M
    if (cinfo->flags & TLS_CURVE_CHAR2)
        return 0;
# endif
    return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
}
['int dtls1_connect(SSL *s)\n{\n    BUF_MEM *buf = NULL;\n    unsigned long Time = (unsigned long)time(NULL);\n    void (*cb) (const SSL *ssl, int type, int val) = NULL;\n    int ret = -1;\n    int new_state, state, skip = 0;\n#ifndef OPENSSL_NO_SCTP\n    unsigned char sctpauthkey[64];\n    char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];\n#endif\n    RAND_add(&Time, sizeof(Time), 0);\n    ERR_clear_error();\n    clear_sys_error();\n    if (s->info_callback != NULL)\n        cb = s->info_callback;\n    else if (s->ctx->info_callback != NULL)\n        cb = s->ctx->info_callback;\n    s->in_handshake++;\n    if (!SSL_in_init(s) || SSL_in_before(s))\n        SSL_clear(s);\n#ifndef OPENSSL_NO_SCTP\n    BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,\n             s->in_handshake, NULL);\n#endif\n#ifndef OPENSSL_NO_HEARTBEATS\n    if (s->tlsext_hb_pending) {\n        dtls1_stop_timer(s);\n        s->tlsext_hb_pending = 0;\n        s->tlsext_hb_seq++;\n    }\n#endif\n    for (;;) {\n        state = s->state;\n        switch (s->state) {\n        case SSL_ST_RENEGOTIATE:\n            s->renegotiate = 1;\n            s->state = SSL_ST_CONNECT;\n            s->ctx->stats.sess_connect_renegotiate++;\n        case SSL_ST_BEFORE:\n        case SSL_ST_CONNECT:\n        case SSL_ST_BEFORE | SSL_ST_CONNECT:\n        case SSL_ST_OK | SSL_ST_CONNECT:\n            s->server = 0;\n            if (cb != NULL)\n                cb(s, SSL_CB_HANDSHAKE_START, 1);\n            if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&\n                (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) {\n                SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);\n                ret = -1;\n                goto end;\n            }\n            s->type = SSL_ST_CONNECT;\n            if (s->init_buf == NULL) {\n                if ((buf = BUF_MEM_new()) == NULL) {\n                    ret = -1;\n                    goto end;\n                }\n                if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {\n                    ret = -1;\n                    goto end;\n                }\n                s->init_buf = buf;\n                buf = NULL;\n            }\n            if (!ssl3_setup_buffers(s)) {\n                ret = -1;\n                goto end;\n            }\n            if (!ssl_init_wbio_buffer(s, 0)) {\n                ret = -1;\n                goto end;\n            }\n            s->state = SSL3_ST_CW_CLNT_HELLO_A;\n            s->ctx->stats.sess_connect++;\n            s->init_num = 0;\n            memset(s->s3->client_random, 0, sizeof(s->s3->client_random));\n            s->d1->send_cookie = 0;\n            s->hit = 0;\n            s->d1->change_cipher_spec_ok = 0;\n            s->s3->change_cipher_spec = 0;\n            break;\n#ifndef OPENSSL_NO_SCTP\n        case DTLS1_SCTP_ST_CR_READ_SOCK:\n            if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n                s->s3->in_read_app_data = 2;\n                s->rwstate = SSL_READING;\n                BIO_clear_retry_flags(SSL_get_rbio(s));\n                BIO_set_retry_read(SSL_get_rbio(s));\n                ret = -1;\n                goto end;\n            }\n            s->state = s->s3->tmp.next_state;\n            break;\n        case DTLS1_SCTP_ST_CW_WRITE_SOCK:\n            ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));\n            if (ret < 0)\n                goto end;\n            if (ret == 0) {\n                s->s3->in_read_app_data = 2;\n                s->rwstate = SSL_READING;\n                BIO_clear_retry_flags(SSL_get_rbio(s));\n                BIO_set_retry_read(SSL_get_rbio(s));\n                ret = -1;\n                goto end;\n            }\n            s->state = s->d1->next_state;\n            break;\n#endif\n        case SSL3_ST_CW_CLNT_HELLO_A:\n        case SSL3_ST_CW_CLNT_HELLO_B:\n            s->shutdown = 0;\n            ssl3_init_finished_mac(s);\n            dtls1_start_timer(s);\n            ret = ssl3_client_hello(s);\n            if (ret <= 0)\n                goto end;\n            if (s->d1->send_cookie) {\n                s->state = SSL3_ST_CW_FLUSH;\n                s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;\n            } else\n                s->state = SSL3_ST_CR_SRVR_HELLO_A;\n            s->init_num = 0;\n#ifndef OPENSSL_NO_SCTP\n            if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) {\n#endif\n                if (s->bbio != s->wbio)\n                    s->wbio = BIO_push(s->bbio, s->wbio);\n#ifndef OPENSSL_NO_SCTP\n            }\n#endif\n            break;\n        case SSL3_ST_CR_SRVR_HELLO_A:\n        case SSL3_ST_CR_SRVR_HELLO_B:\n            ret = ssl3_get_server_hello(s);\n            if (ret <= 0)\n                goto end;\n            else {\n                if (s->hit) {\n#ifndef OPENSSL_NO_SCTP\n                    snprintf((char *)labelbuffer,\n                             sizeof(DTLS1_SCTP_AUTH_LABEL),\n                             DTLS1_SCTP_AUTH_LABEL);\n                    SSL_export_keying_material(s, sctpauthkey,\n                                               sizeof(sctpauthkey),\n                                               labelbuffer,\n                                               sizeof(labelbuffer), NULL, 0,\n                                               0);\n                    BIO_ctrl(SSL_get_wbio(s),\n                             BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,\n                             sizeof(sctpauthkey), sctpauthkey);\n#endif\n                    s->state = SSL3_ST_CR_FINISHED_A;\n                } else\n                    s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;\n            }\n            s->init_num = 0;\n            break;\n        case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:\n        case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:\n            ret = dtls1_get_hello_verify(s);\n            if (ret <= 0)\n                goto end;\n            dtls1_stop_timer(s);\n            if (s->d1->send_cookie)\n                s->state = SSL3_ST_CW_CLNT_HELLO_A;\n            else\n                s->state = SSL3_ST_CR_CERT_A;\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CR_CERT_A:\n        case SSL3_ST_CR_CERT_B:\n            if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&\n                !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {\n                ret = ssl3_get_server_certificate(s);\n                if (ret <= 0)\n                    goto end;\n#ifndef OPENSSL_NO_TLSEXT\n                if (s->tlsext_status_expected)\n                    s->state = SSL3_ST_CR_CERT_STATUS_A;\n                else\n                    s->state = SSL3_ST_CR_KEY_EXCH_A;\n            } else {\n                skip = 1;\n                s->state = SSL3_ST_CR_KEY_EXCH_A;\n            }\n#else\n            } else\n                skip = 1;\n            s->state = SSL3_ST_CR_KEY_EXCH_A;\n#endif\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CR_KEY_EXCH_A:\n        case SSL3_ST_CR_KEY_EXCH_B:\n            ret = ssl3_get_key_exchange(s);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CR_CERT_REQ_A;\n            s->init_num = 0;\n            if (!ssl3_check_cert_and_algorithm(s)) {\n                ret = -1;\n                goto end;\n            }\n            break;\n        case SSL3_ST_CR_CERT_REQ_A:\n        case SSL3_ST_CR_CERT_REQ_B:\n            ret = ssl3_get_certificate_request(s);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CR_SRVR_DONE_A;\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CR_SRVR_DONE_A:\n        case SSL3_ST_CR_SRVR_DONE_B:\n            ret = ssl3_get_server_done(s);\n            if (ret <= 0)\n                goto end;\n            dtls1_stop_timer(s);\n            if (s->s3->tmp.cert_req)\n                s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;\n            else\n                s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;\n            s->init_num = 0;\n#ifndef OPENSSL_NO_SCTP\n            if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&\n                state == SSL_ST_RENEGOTIATE)\n                s->state = DTLS1_SCTP_ST_CR_READ_SOCK;\n            else\n#endif\n                s->state = s->s3->tmp.next_state;\n            break;\n        case SSL3_ST_CW_CERT_A:\n        case SSL3_ST_CW_CERT_B:\n        case SSL3_ST_CW_CERT_C:\n        case SSL3_ST_CW_CERT_D:\n            dtls1_start_timer(s);\n            ret = ssl3_send_client_certificate(s);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CW_KEY_EXCH_A;\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CW_KEY_EXCH_A:\n        case SSL3_ST_CW_KEY_EXCH_B:\n            dtls1_start_timer(s);\n            ret = ssl3_send_client_key_exchange(s);\n            if (ret <= 0)\n                goto end;\n#ifndef OPENSSL_NO_SCTP\n            snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),\n                     DTLS1_SCTP_AUTH_LABEL);\n            SSL_export_keying_material(s, sctpauthkey,\n                                       sizeof(sctpauthkey), labelbuffer,\n                                       sizeof(labelbuffer), NULL, 0, 0);\n            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,\n                     sizeof(sctpauthkey), sctpauthkey);\n#endif\n            if (s->s3->tmp.cert_req == 1) {\n                s->state = SSL3_ST_CW_CERT_VRFY_A;\n            } else {\n#ifndef OPENSSL_NO_SCTP\n                if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {\n                    s->d1->next_state = SSL3_ST_CW_CHANGE_A;\n                    s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;\n                } else\n#endif\n                    s->state = SSL3_ST_CW_CHANGE_A;\n            }\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CW_CERT_VRFY_A:\n        case SSL3_ST_CW_CERT_VRFY_B:\n            dtls1_start_timer(s);\n            ret = ssl3_send_client_verify(s);\n            if (ret <= 0)\n                goto end;\n#ifndef OPENSSL_NO_SCTP\n            if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {\n                s->d1->next_state = SSL3_ST_CW_CHANGE_A;\n                s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;\n            } else\n#endif\n                s->state = SSL3_ST_CW_CHANGE_A;\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CW_CHANGE_A:\n        case SSL3_ST_CW_CHANGE_B:\n            if (!s->hit)\n                dtls1_start_timer(s);\n            ret = dtls1_send_change_cipher_spec(s,\n                                                SSL3_ST_CW_CHANGE_A,\n                                                SSL3_ST_CW_CHANGE_B);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CW_FINISHED_A;\n            s->init_num = 0;\n            s->session->cipher = s->s3->tmp.new_cipher;\n#ifdef OPENSSL_NO_COMP\n            s->session->compress_meth = 0;\n#else\n            if (s->s3->tmp.new_compression == NULL)\n                s->session->compress_meth = 0;\n            else\n                s->session->compress_meth = s->s3->tmp.new_compression->id;\n#endif\n            if (!s->method->ssl3_enc->setup_key_block(s)) {\n                ret = -1;\n                goto end;\n            }\n            if (!s->method->ssl3_enc->change_cipher_state(s,\n                                                          SSL3_CHANGE_CIPHER_CLIENT_WRITE))\n            {\n                ret = -1;\n                goto end;\n            }\n#ifndef OPENSSL_NO_SCTP\n            if (s->hit) {\n                BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,\n                         0, NULL);\n            }\n#endif\n            dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);\n            break;\n        case SSL3_ST_CW_FINISHED_A:\n        case SSL3_ST_CW_FINISHED_B:\n            if (!s->hit)\n                dtls1_start_timer(s);\n            ret = ssl3_send_finished(s,\n                                     SSL3_ST_CW_FINISHED_A,\n                                     SSL3_ST_CW_FINISHED_B,\n                                     s->method->\n                                     ssl3_enc->client_finished_label,\n                                     s->method->\n                                     ssl3_enc->client_finished_label_len);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CW_FLUSH;\n            s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;\n            if (s->hit) {\n                s->s3->tmp.next_state = SSL_ST_OK;\n#ifndef OPENSSL_NO_SCTP\n                if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {\n                    s->d1->next_state = s->s3->tmp.next_state;\n                    s->s3->tmp.next_state = DTLS1_SCTP_ST_CW_WRITE_SOCK;\n                }\n#endif\n                if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {\n                    s->state = SSL_ST_OK;\n#ifndef OPENSSL_NO_SCTP\n                    if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {\n                        s->d1->next_state = SSL_ST_OK;\n                        s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;\n                    }\n#endif\n                    s->s3->flags |= SSL3_FLAGS_POP_BUFFER;\n                    s->s3->delay_buf_pop_ret = 0;\n                }\n            } else {\n#ifndef OPENSSL_NO_SCTP\n                BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,\n                         0, NULL);\n#endif\n#ifndef OPENSSL_NO_TLSEXT\n                if (s->tlsext_ticket_expected)\n                    s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;\n                else\n#endif\n                    s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;\n            }\n            s->init_num = 0;\n            break;\n#ifndef OPENSSL_NO_TLSEXT\n        case SSL3_ST_CR_SESSION_TICKET_A:\n        case SSL3_ST_CR_SESSION_TICKET_B:\n            ret = ssl3_get_new_session_ticket(s);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CR_FINISHED_A;\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CR_CERT_STATUS_A:\n        case SSL3_ST_CR_CERT_STATUS_B:\n            ret = ssl3_get_cert_status(s);\n            if (ret <= 0)\n                goto end;\n            s->state = SSL3_ST_CR_KEY_EXCH_A;\n            s->init_num = 0;\n            break;\n#endif\n        case SSL3_ST_CR_FINISHED_A:\n        case SSL3_ST_CR_FINISHED_B:\n            s->d1->change_cipher_spec_ok = 1;\n            ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,\n                                    SSL3_ST_CR_FINISHED_B);\n            if (ret <= 0)\n                goto end;\n            dtls1_stop_timer(s);\n            if (s->hit)\n                s->state = SSL3_ST_CW_CHANGE_A;\n            else\n                s->state = SSL_ST_OK;\n#ifndef OPENSSL_NO_SCTP\n            if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&\n                state == SSL_ST_RENEGOTIATE) {\n                s->d1->next_state = s->state;\n                s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;\n            }\n#endif\n            s->init_num = 0;\n            break;\n        case SSL3_ST_CW_FLUSH:\n            s->rwstate = SSL_WRITING;\n            if (BIO_flush(s->wbio) <= 0) {\n                if (!BIO_should_retry(s->wbio)) {\n                    s->rwstate = SSL_NOTHING;\n                    s->state = s->s3->tmp.next_state;\n                }\n                ret = -1;\n                goto end;\n            }\n            s->rwstate = SSL_NOTHING;\n            s->state = s->s3->tmp.next_state;\n            break;\n        case SSL_ST_OK:\n            ssl3_cleanup_key_block(s);\n#if 0\n            if (s->init_buf != NULL) {\n                BUF_MEM_free(s->init_buf);\n                s->init_buf = NULL;\n            }\n#endif\n            if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))\n                ssl_free_wbio_buffer(s);\n            s->init_num = 0;\n            s->renegotiate = 0;\n            s->new_session = 0;\n            ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);\n            if (s->hit)\n                s->ctx->stats.sess_hit++;\n            ret = 1;\n            s->handshake_func = dtls1_connect;\n            s->ctx->stats.sess_connect_good++;\n            if (cb != NULL)\n                cb(s, SSL_CB_HANDSHAKE_DONE, 1);\n            s->d1->handshake_read_seq = 0;\n            s->d1->next_handshake_write_seq = 0;\n            goto end;\n        default:\n            SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);\n            ret = -1;\n            goto end;\n        }\n        if (!s->s3->tmp.reuse_message && !skip) {\n            if (s->debug) {\n                if ((ret = BIO_flush(s->wbio)) <= 0)\n                    goto end;\n            }\n            if ((cb != NULL) && (s->state != state)) {\n                new_state = s->state;\n                s->state = state;\n                cb(s, SSL_CB_CONNECT_LOOP, 1);\n                s->state = new_state;\n            }\n        }\n        skip = 0;\n    }\n end:\n    s->in_handshake--;\n#ifndef OPENSSL_NO_SCTP\n    BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,\n             s->in_handshake, NULL);\n#endif\n    if (buf != NULL)\n        BUF_MEM_free(buf);\n    if (cb != NULL)\n        cb(s, SSL_CB_CONNECT_EXIT, ret);\n    return (ret);\n}', 'int ssl3_get_key_exchange(SSL *s)\n{\n#ifndef OPENSSL_NO_RSA\n    unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];\n#endif\n    EVP_MD_CTX md_ctx;\n    unsigned char *param, *p;\n    int al, j, ok;\n    long i, param_len, n, alg_k, alg_a;\n    EVP_PKEY *pkey = NULL;\n    const EVP_MD *md = NULL;\n#ifndef OPENSSL_NO_RSA\n    RSA *rsa = NULL;\n#endif\n#ifndef OPENSSL_NO_DH\n    DH *dh = NULL;\n#endif\n#ifndef OPENSSL_NO_ECDH\n    EC_KEY *ecdh = NULL;\n    BN_CTX *bn_ctx = NULL;\n    EC_POINT *srvr_ecpoint = NULL;\n    int curve_nid = 0;\n    int encoded_pt_len = 0;\n#endif\n    EVP_MD_CTX_init(&md_ctx);\n    n = s->method->ssl_get_message(s,\n                                   SSL3_ST_CR_KEY_EXCH_A,\n                                   SSL3_ST_CR_KEY_EXCH_B,\n                                   -1, s->max_cert_list, &ok);\n    if (!ok)\n        return ((int)n);\n    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n    if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {\n        if (alg_k & (SSL_kDHE | SSL_kECDHE)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            goto f_err;\n        }\n#ifndef OPENSSL_NO_PSK\n        if (alg_k & SSL_kPSK) {\n            s->session->sess_cert = ssl_sess_cert_new();\n            if (s->ctx->psk_identity_hint)\n                OPENSSL_free(s->ctx->psk_identity_hint);\n            s->ctx->psk_identity_hint = NULL;\n        }\n#endif\n        s->s3->tmp.reuse_message = 1;\n        return (1);\n    }\n    param = p = (unsigned char *)s->init_msg;\n    if (s->session->sess_cert != NULL) {\n#ifndef OPENSSL_NO_RSA\n        if (s->session->sess_cert->peer_rsa_tmp != NULL) {\n            RSA_free(s->session->sess_cert->peer_rsa_tmp);\n            s->session->sess_cert->peer_rsa_tmp = NULL;\n        }\n#endif\n#ifndef OPENSSL_NO_DH\n        if (s->session->sess_cert->peer_dh_tmp) {\n            DH_free(s->session->sess_cert->peer_dh_tmp);\n            s->session->sess_cert->peer_dh_tmp = NULL;\n        }\n#endif\n#ifndef OPENSSL_NO_ECDH\n        if (s->session->sess_cert->peer_ecdh_tmp) {\n            EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);\n            s->session->sess_cert->peer_ecdh_tmp = NULL;\n        }\n#endif\n    } else {\n        s->session->sess_cert = ssl_sess_cert_new();\n    }\n    param_len = 0;\n    alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n    al = SSL_AD_DECODE_ERROR;\n#ifndef OPENSSL_NO_PSK\n    if (alg_k & SSL_kPSK) {\n        char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1];\n        param_len = 2;\n        if (param_len > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        n2s(p, i);\n        if (i > PSK_MAX_IDENTITY_LEN) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);\n            goto f_err;\n        }\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,\n                   SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        memcpy(tmp_id_hint, p, i);\n        memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);\n        if (s->ctx->psk_identity_hint != NULL)\n            OPENSSL_free(s->ctx->psk_identity_hint);\n        s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);\n        if (s->ctx->psk_identity_hint == NULL) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto f_err;\n        }\n        p += i;\n        n -= param_len;\n    } else\n#endif\n#ifndef OPENSSL_NO_SRP\n    if (alg_k & SSL_kSRP) {\n        param_len = 2;\n        if (param_len > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (2 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 2;\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (1 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 1;\n        i = (unsigned int)(p[0]);\n        p++;\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (2 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 2;\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        n -= param_len;\n        if (!srp_verify_server_param(s, &al)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);\n            goto f_err;\n        }\n# ifndef OPENSSL_NO_RSA\n        if (alg_a & SSL_aRSA)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n# else\n        if (0) ;\n# endif\n# ifndef OPENSSL_NO_DSA\n        else if (alg_a & SSL_aDSS)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].\n                                x509);\n# endif\n    } else\n#endif\n#ifndef OPENSSL_NO_RSA\n    if (alg_k & SSL_kRSA) {\n        if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);\n            goto f_err;\n        }\n        if ((rsa = RSA_new()) == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        param_len = 2;\n        if (param_len > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (2 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 2;\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        n -= param_len;\n        if (alg_a & SSL_aRSA)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n        else {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        s->session->sess_cert->peer_rsa_tmp = rsa;\n        rsa = NULL;\n    }\n#else\n    if (0) ;\n#endif\n#ifndef OPENSSL_NO_DH\n    else if (alg_k & SSL_kDHE) {\n        if ((dh = DH_new()) == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);\n            goto err;\n        }\n        param_len = 2;\n        if (param_len > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(dh->p = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (2 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 2;\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(dh->g = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (2 > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        param_len += 2;\n        n2s(p, i);\n        if (i > n - param_len) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        n -= param_len;\n        if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);\n            goto f_err;\n        }\n# ifndef OPENSSL_NO_RSA\n        if (alg_a & SSL_aRSA)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n# else\n        if (0) ;\n# endif\n# ifndef OPENSSL_NO_DSA\n        else if (alg_a & SSL_aDSS)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].\n                                x509);\n# endif\n        s->session->sess_cert->peer_dh_tmp = dh;\n        dh = NULL;\n    } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {\n        al = SSL_AD_ILLEGAL_PARAMETER;\n        SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,\n               SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n        goto f_err;\n    }\n#endif\n#ifndef OPENSSL_NO_ECDH\n    else if (alg_k & SSL_kECDHE) {\n        EC_GROUP *ngroup;\n        const EC_GROUP *group;\n        if ((ecdh = EC_KEY_new()) == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        param_len = 4;\n        if (param_len > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        if (!tls1_check_curve(s, p, 3)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);\n            goto f_err;\n        }\n        if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,\n                   SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);\n            goto f_err;\n        }\n        ngroup = EC_GROUP_new_by_curve_name(curve_nid);\n        if (ngroup == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);\n            goto err;\n        }\n        if (EC_KEY_set_group(ecdh, ngroup) == 0) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);\n            goto err;\n        }\n        EC_GROUP_free(ngroup);\n        group = EC_KEY_get0_group(ecdh);\n        if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&\n            (EC_GROUP_get_degree(group) > 163)) {\n            al = SSL_AD_EXPORT_RESTRICTION;\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,\n                   SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);\n            goto f_err;\n        }\n        p += 3;\n        if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||\n            ((bn_ctx = BN_CTX_new()) == NULL)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        encoded_pt_len = *p;\n        p += 1;\n        if ((encoded_pt_len > n - param_len) ||\n            (EC_POINT_oct2point(group, srvr_ecpoint,\n                                p, encoded_pt_len, bn_ctx) == 0)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);\n            goto f_err;\n        }\n        param_len += encoded_pt_len;\n        n -= param_len;\n        p += encoded_pt_len;\n        if (0) ;\n# ifndef OPENSSL_NO_RSA\n        else if (alg_a & SSL_aRSA)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n# endif\n# ifndef OPENSSL_NO_ECDSA\n        else if (alg_a & SSL_aECDSA)\n            pkey =\n                X509_get_pubkey(s->session->\n                                sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);\n# endif\n        EC_KEY_set_public_key(ecdh, srvr_ecpoint);\n        s->session->sess_cert->peer_ecdh_tmp = ecdh;\n        ecdh = NULL;\n        BN_CTX_free(bn_ctx);\n        bn_ctx = NULL;\n        EC_POINT_free(srvr_ecpoint);\n        srvr_ecpoint = NULL;\n    } else if (alg_k) {\n        al = SSL_AD_UNEXPECTED_MESSAGE;\n        SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);\n        goto f_err;\n    }\n#endif\n    if (pkey != NULL) {\n        if (SSL_USE_SIGALGS(s)) {\n            int rv;\n            if (2 > n) {\n                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n                goto f_err;\n            }\n            rv = tls12_check_peer_sigalg(&md, s, p, pkey);\n            if (rv == -1)\n                goto err;\n            else if (rv == 0) {\n                goto f_err;\n            }\n#ifdef SSL_DEBUG\n            fprintf(stderr, "USING TLSv1.2 HASH %s\\n", EVP_MD_name(md));\n#endif\n            p += 2;\n            n -= 2;\n        } else\n            md = EVP_sha1();\n        if (2 > n) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        n2s(p, i);\n        n -= 2;\n        j = EVP_PKEY_size(pkey);\n        if ((i != n) || (n > j) || (n <= 0)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);\n            goto f_err;\n        }\n#ifndef OPENSSL_NO_RSA\n        if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {\n            int num;\n            unsigned int size;\n            j = 0;\n            q = md_buf;\n            for (num = 2; num > 0; num--) {\n                EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);\n                EVP_DigestInit_ex(&md_ctx, (num == 2)\n                                  ? s->ctx->md5 : s->ctx->sha1, NULL);\n                EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),\n                                 SSL3_RANDOM_SIZE);\n                EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),\n                                 SSL3_RANDOM_SIZE);\n                EVP_DigestUpdate(&md_ctx, param, param_len);\n                EVP_DigestFinal_ex(&md_ctx, q, &size);\n                q += size;\n                j += size;\n            }\n            i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);\n            if (i < 0) {\n                al = SSL_AD_DECRYPT_ERROR;\n                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);\n                goto f_err;\n            }\n            if (i == 0) {\n                al = SSL_AD_DECRYPT_ERROR;\n                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);\n                goto f_err;\n            }\n        } else\n#endif\n        {\n            EVP_VerifyInit_ex(&md_ctx, md, NULL);\n            EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),\n                             SSL3_RANDOM_SIZE);\n            EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),\n                             SSL3_RANDOM_SIZE);\n            EVP_VerifyUpdate(&md_ctx, param, param_len);\n            if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {\n                al = SSL_AD_DECRYPT_ERROR;\n                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);\n                goto f_err;\n            }\n        }\n    } else {\n        if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {\n            if (ssl3_check_cert_and_algorithm(s))\n                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (n != 0) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);\n            goto f_err;\n        }\n    }\n    EVP_PKEY_free(pkey);\n    EVP_MD_CTX_cleanup(&md_ctx);\n    return (1);\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n    EVP_PKEY_free(pkey);\n#ifndef OPENSSL_NO_RSA\n    if (rsa != NULL)\n        RSA_free(rsa);\n#endif\n#ifndef OPENSSL_NO_DH\n    if (dh != NULL)\n        DH_free(dh);\n#endif\n#ifndef OPENSSL_NO_ECDH\n    BN_CTX_free(bn_ctx);\n    EC_POINT_free(srvr_ecpoint);\n    if (ecdh != NULL)\n        EC_KEY_free(ecdh);\n#endif\n    EVP_MD_CTX_cleanup(&md_ctx);\n    return (-1);\n}', 'int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)\n{\n    const unsigned char *curves;\n    size_t num_curves, i;\n    unsigned int suiteb_flags = tls1_suiteb(s);\n    if (len != 3 || p[0] != NAMED_CURVE_TYPE)\n        return 0;\n    if (suiteb_flags) {\n        unsigned long cid = s->s3->tmp.new_cipher->id;\n        if (p[1])\n            return 0;\n        if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {\n            if (p[2] != TLSEXT_curve_P_256)\n                return 0;\n        } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {\n            if (p[2] != TLSEXT_curve_P_384)\n                return 0;\n        } else\n            return 0;\n    }\n    if (!tls1_get_curvelist(s, 0, &curves, &num_curves))\n        return 0;\n    for (i = 0; i < num_curves; i++, curves += 2) {\n        if (p[1] == curves[0] && p[2] == curves[1])\n            return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);\n    }\n    return 0;\n}', 'static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)\n{\n    const tls_curve_info *cinfo;\n    if (curve[0])\n        return 1;\n    if ((curve[1] < 1) || ((size_t)curve[1] >\n                           sizeof(nid_list) / sizeof(nid_list[0])))\n        return 0;\n    cinfo = &nid_list[curve[1] - 1];\n# ifdef OPENSSL_NO_EC2M\n    if (cinfo->flags & TLS_CURVE_CHAR2)\n        return 0;\n# endif\n    return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);\n}']