ssl/d1_clnt.c:422: error: BUFFER_OVERRUN_L3
  Offset: [-1, +oo] Size: 28 by call to `ssl3_get_key_exchange`.
Showing all 11 steps of the trace


ssl/d1_clnt.c:159:1: Parameter `*s->init_msg`
157.                             dtls1_get_client_method, DTLSv1_2_enc_data)
158.   
159. > int dtls1_connect(SSL *s)
160.   {
161.       BUF_MEM *buf = NULL;

ssl/d1_clnt.c:422:19: Call
420.           case SSL3_ST_CR_KEY_EXCH_A:
421.           case SSL3_ST_CR_KEY_EXCH_B:
422.               ret = ssl3_get_key_exchange(s);
                         ^
423.               if (ret <= 0)
424.                   goto end;

ssl/s3_clnt.c:1378:1: Parameter `*s->init_msg`
  1376.   }
  1377.   
  1378. > int ssl3_get_key_exchange(SSL *s)
  1379.   {
  1380.   #ifndef OPENSSL_NO_RSA

ssl/s3_clnt.c:1797:14: Call
  1795.            * invalid curve. ECParameters is 3 bytes.
  1796.            */
  1797.           if (!tls1_check_curve(s, p, 3)) {
                       ^
  1798.               SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
  1799.               goto f_err;

ssl/t1_lib.c:266:1: Array declaration
    264.   
    265.   /* The client's default curves / the server's 'auto' curves. */
    266. > static const unsigned char eccurves_auto[] = {
    267.       /* Prefer P-256 which has the fastest and most secure implementations. */
    268.       0, 23,                      /* secp256r1 (23) */

ssl/t1_lib.c:507:20: Call
    505.       for (i = 0; i < num_curves; i++, curves += 2) {
    506.           if (p[1] == curves[0] && p[2] == curves[1])
    507.               return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
                              ^
    508.       }
    509.       return 0;

ssl/t1_lib.c:466:1: <Offset trace>
464.   
465.   /* See if curve is allowed by security callback */
466. > static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
467.   {
468.       const tls_curve_info *cinfo;

ssl/t1_lib.c:466:1: Parameter `*curve`
      464.   
      465.   /* See if curve is allowed by security callback */
      466. > static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
      467.   {
      468.       const tls_curve_info *cinfo;

ssl/t1_lib.c:228:1: <Length trace>
226.   # define TLS_CURVE_PRIME         0x0
227.   
228. > static const tls_curve_info nid_list[] = {
229.       {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
230.       {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */

ssl/t1_lib.c:228:1: Array declaration
      226.   # define TLS_CURVE_PRIME         0x0
      227.   
      228. > static const tls_curve_info nid_list[] = {
      229.       {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
      230.       {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */

ssl/t1_lib.c:473:5: Array access: Offset: [-1, +oo] Size: 28 by call to `ssl3_get_key_exchange` 
      471.       if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
      472.           return 0;
      473.       cinfo = &nid_list[curve[1] - 1];
                 ^
      474.   # ifdef OPENSSL_NO_EC2M
      475.       if (cinfo->flags & TLS_CURVE_CHAR2)

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] > OSSL_NELEM(nid_list)))
        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        if (!SSL_clear(s))\n            return -1;\n    }\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                s->state = SSL_ST_ERR;\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                    s->state = SSL_ST_ERR;\n                    goto end;\n                }\n                if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {\n                    ret = -1;\n                    s->state = SSL_ST_ERR;\n                    goto end;\n                }\n                s->init_buf = buf;\n                buf = NULL;\n            }\n            if (!ssl3_setup_buffers(s)) {\n                ret = -1;\n                s->state = SSL_ST_ERR;\n                goto end;\n            }\n            if (!ssl_init_wbio_buffer(s, 0)) {\n                ret = -1;\n                s->state = SSL_ST_ERR;\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->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_CHANGE_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                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            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                s->state = SSL_ST_ERR;\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                s->state = SSL_ST_ERR;\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                s->state = SSL_ST_ERR;\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            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            } 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                if (s->tlsext_ticket_expected)\n                    s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;\n                else\n                    s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;\n            }\n            s->init_num = 0;\n            break;\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_CHANGE_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        case SSL3_ST_CR_CHANGE_A:\n        case SSL3_ST_CR_CHANGE_B:\n            ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,\n                                              SSL3_ST_CR_CHANGE_B);\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_FINISHED_A:\n        case SSL3_ST_CR_FINISHED_B:\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            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        case SSL_ST_ERR:\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    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_EC\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 | SSL_kDHEPSK | SSL_kECDHEPSK)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            goto f_err;\n        }\n        s->s3->tmp.reuse_message = 1;\n        return (1);\n    }\n    param = p = (unsigned char *)s->init_msg;\n#ifndef OPENSSL_NO_RSA\n    RSA_free(s->s3->peer_rsa_tmp);\n    s->s3->peer_rsa_tmp = NULL;\n#endif\n#ifndef OPENSSL_NO_DH\n    DH_free(s->s3->peer_dh_tmp);\n    s->s3->peer_dh_tmp = NULL;\n#endif\n#ifndef OPENSSL_NO_EC\n    EC_KEY_free(s->s3->peer_ecdh_tmp);\n    s->s3->peer_ecdh_tmp = NULL;\n#endif\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_PSK) {\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        OPENSSL_free(s->session->psk_identity_hint);\n        if (i != 0) {\n            s->session->psk_identity_hint = BUF_strndup((char *)p, i);\n            if (s->session->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        } else {\n            s->session->psk_identity_hint = NULL;\n        }\n        p += i;\n        n -= param_len;\n    }\n    if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {\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)) == 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)) == 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)) == 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)) == 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        if (alg_a & (SSL_aRSA|SSL_aDSS))\n            pkey = X509_get_pubkey(s->session->peer);\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)) == 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_RSA_E_LENGTH);\n            goto f_err;\n        }\n        param_len += i;\n        if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == 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 (alg_a & SSL_aRSA)\n            pkey = X509_get_pubkey(s->session->peer);\n        else {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(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        s->s3->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 | SSL_kDHEPSK)) {\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)) == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (BN_is_zero(dh->p)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);\n            goto f_err;\n        }\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)) == NULL) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        p += i;\n        if (BN_is_zero(dh->g)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);\n            goto f_err;\n        }\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)) == 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 (BN_is_zero(dh->pub_key)) {\n            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);\n            goto f_err;\n        }\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        if (alg_a & (SSL_aRSA|SSL_aDSS))\n            pkey = X509_get_pubkey(s->session->peer);\n        s->s3->peer_dh_tmp = dh;\n        dh = NULL;\n    }\n#endif\n#ifndef OPENSSL_NO_EC\n    else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {\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 = X509_get_pubkey(s->session->peer);\n# endif\n# ifndef OPENSSL_NO_EC\n        else if (alg_a & SSL_aECDSA)\n            pkey = X509_get_pubkey(s->session->peer);\n# endif\n        EC_KEY_set_public_key(ecdh, srvr_ecpoint);\n        s->s3->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_PSK)) {\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    RSA_free(rsa);\n#endif\n#ifndef OPENSSL_NO_DH\n    DH_free(dh);\n#endif\n#ifndef OPENSSL_NO_EC\n    BN_CTX_free(bn_ctx);\n    EC_POINT_free(srvr_ecpoint);\n    EC_KEY_free(ecdh);\n#endif\n    EVP_MD_CTX_cleanup(&md_ctx);\n    s->state = SSL_ST_ERR;\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] > OSSL_NELEM(nid_list)))\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}']