static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
    pkt->curr += len;
    pkt->remaining -= len;
}
['MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)\n{\n    int i, al = SSL_AD_INTERNAL_ERROR;\n    unsigned int j;\n    size_t loop;\n    unsigned long id;\n    const SSL_CIPHER *c;\n#ifndef OPENSSL_NO_COMP\n    SSL_COMP *comp = NULL;\n#endif\n    STACK_OF(SSL_CIPHER) *ciphers = NULL;\n    int protverr;\n    PACKET session_id, compression, extensions, cookie;\n    static const unsigned char null_compression = 0;\n    CLIENTHELLO_MSG clienthello;\n    memset(&clienthello, 0, sizeof(clienthello));\n    clienthello.isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);\n    PACKET_null_init(&cookie);\n    if (clienthello.isv2) {\n        unsigned int mt;\n        if (!PACKET_get_1(pkt, &mt)\n            || mt != SSL2_MT_CLIENT_HELLO) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n    }\n    if (!PACKET_get_net_2(pkt, &clienthello.version)) {\n        al = SSL_AD_DECODE_ERROR;\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);\n        goto err;\n    }\n    if (clienthello.isv2) {\n        unsigned int ciphersuite_len, session_id_len, challenge_len;\n        PACKET challenge;\n        if (!PACKET_get_net_2(pkt, &ciphersuite_len)\n            || !PACKET_get_net_2(pkt, &session_id_len)\n            || !PACKET_get_net_2(pkt, &challenge_len)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n        clienthello.session_id_len = session_id_len;\n        if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (!PACKET_get_sub_packet(pkt, &clienthello.ciphersuites,\n                                   ciphersuite_len)\n            || !PACKET_get_sub_packet(pkt, &session_id,\n                                      clienthello.session_id_len)\n            || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)\n            || PACKET_remaining(pkt) != 0) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_RECORD_LENGTH_MISMATCH);\n            al = SSL_AD_DECODE_ERROR;\n            goto f_err;\n        }\n        challenge_len = challenge_len > sizeof(clienthello.random)\n                        ? sizeof(clienthello.random) : challenge_len;\n        memset(clienthello.random, 0, sizeof(clienthello.random));\n        if (!PACKET_copy_bytes(&challenge,\n                               clienthello.random + sizeof(clienthello.random) -\n                               challenge_len, challenge_len)\n            || !PACKET_buf_init(&compression, &null_compression, 1)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            al = SSL_AD_INTERNAL_ERROR;\n            goto f_err;\n        }\n        PACKET_null_init(&clienthello.extensions);\n    } else {\n        if (!PACKET_copy_bytes(pkt, clienthello.random, SSL3_RANDOM_SIZE)\n            || !PACKET_get_length_prefixed_1(pkt, &session_id)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (SSL_IS_DTLS(s)) {\n            if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n                goto f_err;\n            }\n            if (!PACKET_copy_all(&cookie, clienthello.dtls_cookie,\n                                 DTLS1_COOKIE_LENGTH,\n                                 &clienthello.dtls_cookie_len)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n                goto f_err;\n            }\n            if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {\n                if (clienthello.dtls_cookie_len == 0)\n                    return 1;\n            }\n        }\n        if (!PACKET_get_length_prefixed_2(pkt, &clienthello.ciphersuites)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (!PACKET_get_length_prefixed_1(pkt, &compression)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n            goto f_err;\n        }\n        if (PACKET_remaining(pkt) == 0) {\n            PACKET_null_init(&clienthello.extensions);\n        } else {\n            if (!PACKET_get_length_prefixed_2(pkt, &clienthello.extensions)) {\n                al = SSL_AD_DECODE_ERROR;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n                goto f_err;\n            }\n        }\n    }\n    if (!PACKET_copy_all(&compression, clienthello.compressions,\n                         MAX_COMPRESSIONS_SIZE, &clienthello.compressions_len)\n            || !PACKET_copy_all(&session_id, clienthello.session_id,\n                                SSL_MAX_SSL_SESSION_ID_LENGTH,\n                                &clienthello.session_id_len)) {\n        al = SSL_AD_DECODE_ERROR;\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n        goto f_err;\n    }\n    extensions = clienthello.extensions;\n    if (!tls_collect_extensions(&extensions, &clienthello.pre_proc_exts,\n                                  &clienthello.num_extensions, &al)) {\n        goto f_err;\n    }\n    memcpy(s->s3->client_random, clienthello.random, SSL3_RANDOM_SIZE);\n    if (clienthello.isv2) {\n        if (clienthello.version == SSL2_VERSION\n                || (clienthello.version & 0xff00)\n                   != (SSL3_VERSION_MAJOR << 8)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n            goto err;\n        }\n        s->client_version = clienthello.version;\n    }\n    if (!SSL_IS_DTLS(s)) {\n        protverr = ssl_choose_server_version(s, &clienthello);\n    } else if (s->method->version != DTLS_ANY_VERSION &&\n               DTLS_VERSION_LT((int)clienthello.version, s->version)) {\n        protverr = SSL_R_VERSION_TOO_LOW;\n    } else {\n        protverr = 0;\n    }\n    if (protverr) {\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);\n        if ((!s->enc_write_ctx && !s->write_hash)) {\n            s->version = s->client_version = clienthello.version;\n        }\n        al = SSL_AD_PROTOCOL_VERSION;\n        goto f_err;\n    }\n    if (SSL_IS_DTLS(s)) {\n        if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {\n            if (s->ctx->app_verify_cookie_cb != NULL) {\n                if (s->ctx->app_verify_cookie_cb(s, clienthello.dtls_cookie,\n                        clienthello.dtls_cookie_len) == 0) {\n                    al = SSL_AD_HANDSHAKE_FAILURE;\n                    SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                           SSL_R_COOKIE_MISMATCH);\n                    goto f_err;\n                }\n            } else if (s->d1->cookie_len != clienthello.dtls_cookie_len\n                    || memcmp(clienthello.dtls_cookie, s->d1->cookie,\n                              s->d1->cookie_len) != 0) {\n                al = SSL_AD_HANDSHAKE_FAILURE;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n                goto f_err;\n            }\n            s->d1->cookie_verified = 1;\n        }\n        if (s->method->version == DTLS_ANY_VERSION) {\n            protverr = ssl_choose_server_version(s, &clienthello);\n            if (protverr != 0) {\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);\n                s->version = s->client_version;\n                al = SSL_AD_PROTOCOL_VERSION;\n                goto f_err;\n            }\n        }\n    }\n    s->hit = 0;\n    if (!tls_check_client_ems_support(s, &clienthello)) {\n        al = SSL_AD_DECODE_ERROR;\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n        goto f_err;\n    }\n    if (clienthello.isv2 ||\n        (s->new_session &&\n         (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {\n        if (!ssl_get_new_session(s, 1))\n            goto err;\n    } else {\n        i = ssl_get_prev_session(s, &clienthello);\n        if (i == 1 && s->version == s->session->ssl_version) {\n            s->hit = 1;\n        } else if (i == -1) {\n            goto err;\n        } else {\n            if (!ssl_get_new_session(s, 1))\n                goto err;\n        }\n    }\n    if (ssl_bytes_to_cipher_list(s, &clienthello.ciphersuites, &ciphers,\n                                 clienthello.isv2, &al) == NULL) {\n        goto f_err;\n    }\n    if (s->hit) {\n        j = 0;\n        id = s->session->cipher->id;\n#ifdef CIPHER_DEBUG\n        fprintf(stderr, "client sent %d ciphers\\n", sk_SSL_CIPHER_num(ciphers));\n#endif\n        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {\n            c = sk_SSL_CIPHER_value(ciphers, i);\n#ifdef CIPHER_DEBUG\n            fprintf(stderr, "client [%2d of %2d]:%s\\n",\n                    i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));\n#endif\n            if (c->id == id) {\n                j = 1;\n                break;\n            }\n        }\n        if (j == 0) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_REQUIRED_CIPHER_MISSING);\n            goto f_err;\n        }\n    }\n    for (loop = 0; loop < clienthello.compressions_len; loop++) {\n        if (clienthello.compressions[loop] == 0)\n            break;\n    }\n    if (loop >= clienthello.compressions_len) {\n        al = SSL_AD_DECODE_ERROR;\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);\n        goto f_err;\n    }\n    if (!ssl_parse_clienthello_tlsext(s, &clienthello)) {\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);\n        goto err;\n    }\n    {\n        unsigned char *pos;\n        pos = s->s3->server_random;\n        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {\n            goto f_err;\n        }\n    }\n    if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {\n        const SSL_CIPHER *pref_cipher = NULL;\n        int master_key_length;\n        master_key_length = sizeof(s->session->master_key);\n        if (s->tls_session_secret_cb(s, s->session->master_key,\n                                     &master_key_length, ciphers,\n                                     &pref_cipher,\n                                     s->tls_session_secret_cb_arg)\n                && master_key_length > 0) {\n            s->session->master_key_length = master_key_length;\n            s->hit = 1;\n            s->session->ciphers = ciphers;\n            s->session->verify_result = X509_V_OK;\n            ciphers = NULL;\n            pref_cipher =\n                pref_cipher ? pref_cipher : ssl3_choose_cipher(s,\n                                                               s->\n                                                               session->ciphers,\n                                                               SSL_get_ciphers\n                                                               (s));\n            if (pref_cipher == NULL) {\n                al = SSL_AD_HANDSHAKE_FAILURE;\n                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);\n                goto f_err;\n            }\n            s->session->cipher = pref_cipher;\n            sk_SSL_CIPHER_free(s->cipher_list);\n            s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);\n            sk_SSL_CIPHER_free(s->cipher_list_by_id);\n            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);\n        }\n    }\n    s->s3->tmp.new_compression = NULL;\n#ifndef OPENSSL_NO_COMP\n    if (s->session->compress_meth != 0) {\n        int m, comp_id = s->session->compress_meth;\n        unsigned int k;\n        if (!ssl_allow_compression(s)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_INCONSISTENT_COMPRESSION);\n            goto f_err;\n        }\n        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {\n            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);\n            if (comp_id == comp->id) {\n                s->s3->tmp.new_compression = comp;\n                break;\n            }\n        }\n        if (s->s3->tmp.new_compression == NULL) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_INVALID_COMPRESSION_ALGORITHM);\n            goto f_err;\n        }\n        for (k = 0; k < clienthello.compressions_len; k++) {\n            if (clienthello.compressions[k] == comp_id)\n                break;\n        }\n        if (k >= clienthello.compressions_len) {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,\n                   SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);\n            goto f_err;\n        }\n    } else if (s->hit)\n        comp = NULL;\n    else if (ssl_allow_compression(s) && s->ctx->comp_methods) {\n        int m, nn, v, done = 0;\n        unsigned int o;\n        nn = sk_SSL_COMP_num(s->ctx->comp_methods);\n        for (m = 0; m < nn; m++) {\n            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);\n            v = comp->id;\n            for (o = 0; o < clienthello.compressions_len; o++) {\n                if (v == clienthello.compressions[o]) {\n                    done = 1;\n                    break;\n                }\n            }\n            if (done)\n                break;\n        }\n        if (done)\n            s->s3->tmp.new_compression = comp;\n        else\n            comp = NULL;\n    }\n#else\n    if (s->session->compress_meth != 0) {\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);\n        goto f_err;\n    }\n#endif\n    if (!s->hit) {\n#ifdef OPENSSL_NO_COMP\n        s->session->compress_meth = 0;\n#else\n        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;\n#endif\n        sk_SSL_CIPHER_free(s->session->ciphers);\n        s->session->ciphers = ciphers;\n        if (ciphers == NULL) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        ciphers = NULL;\n        if (!tls1_set_server_sigalgs(s)) {\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n            goto err;\n        }\n    }\n    sk_SSL_CIPHER_free(ciphers);\n    OPENSSL_free(clienthello.pre_proc_exts);\n    return MSG_PROCESS_CONTINUE_PROCESSING;\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n    ossl_statem_set_error(s);\n    sk_SSL_CIPHER_free(ciphers);\n    OPENSSL_free(clienthello.pre_proc_exts);\n    return MSG_PROCESS_ERROR;\n}', 'static ossl_inline int PACKET_copy_bytes(PACKET *pkt,\n                                                unsigned char *data, size_t len)\n{\n    if (!PACKET_peek_copy_bytes(pkt, data, len))\n        return 0;\n    packet_forward(pkt, len);\n    return 1;\n}', 'static ossl_inline void packet_forward(PACKET *pkt, size_t len)\n{\n    pkt->curr += len;\n    pkt->remaining -= len;\n}']