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);
}
['MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)\n{\n    EVP_MD_CTX *md_ctx;\n    int al, j;\n    long 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_EC\n    EVP_PKEY_CTX *pctx = NULL;\n#endif\n    PACKET save_param_start, signature;\n    md_ctx = EVP_MD_CTX_new();\n    if (md_ctx == NULL) {\n        al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n        goto f_err;\n    }\n    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n    save_param_start = *pkt;\n#if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)\n    EVP_PKEY_free(s->s3->peer_tmp);\n    s->s3->peer_tmp = NULL;\n#endif\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        PACKET psk_identity_hint;\n        if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);\n            goto f_err;\n        }\n        if (PACKET_remaining(&psk_identity_hint) == 0) {\n            OPENSSL_free(s->session->psk_identity_hint);\n            s->session->psk_identity_hint = NULL;\n        } else if (!PACKET_strndup(&psk_identity_hint,\n                            &s->session->psk_identity_hint)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            goto f_err;\n        }\n    }\n    if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {\n    } else\n#endif\n    if (0) {\n    }\n#ifndef OPENSSL_NO_SRP\n    else if (alg_k & SSL_kSRP) {\n        PACKET prime, generator, salt, server_pub;\n        if (!PACKET_get_length_prefixed_2(pkt, &prime)\n            || !PACKET_get_length_prefixed_2(pkt, &generator)\n            || !PACKET_get_length_prefixed_1(pkt, &salt)\n            || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if ((s->srp_ctx.N =\n             BN_bin2bn(PACKET_data(&prime),\n                       PACKET_remaining(&prime), NULL)) == NULL\n            || (s->srp_ctx.g =\n                BN_bin2bn(PACKET_data(&generator),\n                          PACKET_remaining(&generator), NULL)) == NULL\n            || (s->srp_ctx.s =\n                BN_bin2bn(PACKET_data(&salt),\n                          PACKET_remaining(&salt), NULL)) == NULL\n            || (s->srp_ctx.B =\n                BN_bin2bn(PACKET_data(&server_pub),\n                          PACKET_remaining(&server_pub), NULL)) == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (!srp_verify_server_param(s, &al)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);\n            goto f_err;\n        }\n        if (alg_a & (SSL_aRSA|SSL_aDSS))\n            pkey = X509_get0_pubkey(s->session->peer);\n    }\n#endif\n#ifndef OPENSSL_NO_DH\n    else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {\n        PACKET prime, generator, pub_key;\n        DH *dh;\n        if (!PACKET_get_length_prefixed_2(pkt, &prime)\n            || !PACKET_get_length_prefixed_2(pkt, &generator)\n            || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        s->s3->peer_tmp = EVP_PKEY_new();\n        dh = DH_new();\n        if (s->s3->peer_tmp == NULL || dh == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);\n            DH_free(dh);\n            goto err;\n        }\n        if (EVP_PKEY_assign_DH(s->s3->peer_tmp, dh) == 0) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);\n            DH_free(dh);\n            goto err;\n        }\n        if ((dh->p = BN_bin2bn(PACKET_data(&prime),\n                               PACKET_remaining(&prime), NULL)) == NULL\n            || (dh->g = BN_bin2bn(PACKET_data(&generator),\n                                  PACKET_remaining(&generator), NULL)) == NULL\n            || (dh->pub_key =\n                BN_bin2bn(PACKET_data(&pub_key),\n                          PACKET_remaining(&pub_key), NULL)) == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);\n            goto err;\n        }\n        if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_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_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);\n            goto f_err;\n        }\n        if (alg_a & (SSL_aRSA|SSL_aDSS))\n            pkey = X509_get0_pubkey(s->session->peer);\n    }\n#endif\n#ifndef OPENSSL_NO_EC\n    else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {\n        PACKET encoded_pt;\n        const unsigned char *ecparams;\n        int curve_nid;\n        if (!PACKET_get_bytes(pkt, &ecparams, 3)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        if (!tls1_check_curve(s, ecparams, 3)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);\n            goto f_err;\n        }\n        curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2));\n        if (curve_nid  == 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,\n                   SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);\n            goto f_err;\n        }\n        pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);\n        if (pctx == NULL\n            || EVP_PKEY_paramgen_init(pctx) <= 0\n            || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0\n            || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);\n            goto f_err;\n        }\n        EVP_PKEY_CTX_free(pctx);\n        pctx = NULL;\n        if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp),\n                           PACKET_data(&encoded_pt),\n                           PACKET_remaining(&encoded_pt), NULL) == 0) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);\n            goto f_err;\n        }\n        if (0) ;\n# ifndef OPENSSL_NO_RSA\n        else if (alg_a & SSL_aRSA)\n            pkey = X509_get0_pubkey(s->session->peer);\n# endif\n# ifndef OPENSSL_NO_EC\n        else if (alg_a & SSL_aECDSA)\n            pkey = X509_get0_pubkey(s->session->peer);\n# endif\n    } else if (alg_k) {\n        al = SSL_AD_UNEXPECTED_MESSAGE;\n        SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);\n        goto f_err;\n    }\n#endif\n    if (pkey != NULL) {\n        PACKET params;\n        if (!PACKET_get_sub_packet(&save_param_start, &params,\n                                   PACKET_remaining(&save_param_start) -\n                                   PACKET_remaining(pkt))) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        if (SSL_USE_SIGALGS(s)) {\n            const unsigned char *sigalgs;\n            int rv;\n            if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {\n                SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);\n                goto f_err;\n            }\n            rv = tls12_check_peer_sigalg(&md, s, sigalgs, 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        } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {\n            md = EVP_md5_sha1();\n        } else {\n            md = EVP_sha1();\n        }\n        if (!PACKET_get_length_prefixed_2(pkt, &signature)\n            || PACKET_remaining(pkt) != 0) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        j = EVP_PKEY_size(pkey);\n        if (j < 0) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        if (PACKET_remaining(&signature) > (size_t)j) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);\n            goto f_err;\n        }\n        if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0\n                || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),\n                                    SSL3_RANDOM_SIZE) <= 0\n                || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),\n                                    SSL3_RANDOM_SIZE) <= 0\n                || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),\n                                    PACKET_remaining(&params)) <= 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);\n            goto f_err;\n        }\n        if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),\n                            PACKET_remaining(&signature), pkey) <= 0) {\n            al = SSL_AD_DECRYPT_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);\n            goto f_err;\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_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (PACKET_remaining(pkt) != 0) {\n            SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);\n            goto f_err;\n        }\n    }\n    EVP_MD_CTX_free(md_ctx);\n    return MSG_PROCESS_CONTINUE_READING;\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n#ifndef OPENSSL_NO_RSA\n    RSA_free(rsa);\n#endif\n#ifndef OPENSSL_NO_EC\n    EVP_PKEY_CTX_free(pctx);\n#endif\n    EVP_MD_CTX_free(md_ctx);\n    ossl_statem_set_error(s);\n    return MSG_PROCESS_ERROR;\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}']