MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
{
    int al = SSL_AD_INTERNAL_ERROR;
    PACKET session_id, compression, extensions, cookie;
    static const unsigned char null_compression = 0;
    CLIENTHELLO_MSG *clienthello;
    clienthello = OPENSSL_zalloc(sizeof(*clienthello));
    if (clienthello == NULL) {
        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
        goto err;
    }
    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
        s->renegotiate = 1;
        s->new_session = 1;
    }
    clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
    PACKET_null_init(&cookie);
    if (clienthello->isv2) {
        unsigned int mt;
        if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {
            al = SSL_AD_HANDSHAKE_FAILURE;
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
            goto f_err;
        }
        if (!PACKET_get_1(pkt, &mt)
            || mt != SSL2_MT_CLIENT_HELLO) {
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
            goto err;
        }
    }
    if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
        al = SSL_AD_DECODE_ERROR;
        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
        goto err;
    }
    if (clienthello->isv2) {
        unsigned int ciphersuite_len, session_id_len, challenge_len;
        PACKET challenge;
        if (!PACKET_get_net_2(pkt, &ciphersuite_len)
            || !PACKET_get_net_2(pkt, &session_id_len)
            || !PACKET_get_net_2(pkt, &challenge_len)) {
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
                   SSL_R_RECORD_LENGTH_MISMATCH);
            al = SSL_AD_DECODE_ERROR;
            goto f_err;
        }
        if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
            al = SSL_AD_DECODE_ERROR;
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
            goto f_err;
        }
        if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
                                   ciphersuite_len)
            || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
            || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
            || PACKET_remaining(pkt) != 0) {
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
                   SSL_R_RECORD_LENGTH_MISMATCH);
            al = SSL_AD_DECODE_ERROR;
            goto f_err;
        }
        clienthello->session_id_len = session_id_len;
        challenge_len = challenge_len > SSL3_RANDOM_SIZE
                        ? SSL3_RANDOM_SIZE : challenge_len;
        memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
        if (!PACKET_copy_bytes(&challenge,
                               clienthello->random + SSL3_RANDOM_SIZE -
                               challenge_len, challenge_len)
            || !PACKET_buf_init(&compression, &null_compression, 1)) {
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
            al = SSL_AD_INTERNAL_ERROR;
            goto f_err;
        }
        PACKET_null_init(&clienthello->extensions);
    } else {
        if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
            || !PACKET_get_length_prefixed_1(pkt, &session_id)
            || !PACKET_copy_all(&session_id, clienthello->session_id,
                    SSL_MAX_SSL_SESSION_ID_LENGTH,
                    &clienthello->session_id_len)) {
            al = SSL_AD_DECODE_ERROR;
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
            goto f_err;
        }
        if (SSL_IS_DTLS(s)) {
            if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
                al = SSL_AD_DECODE_ERROR;
                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
                goto f_err;
            }
            if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
                                 DTLS1_COOKIE_LENGTH,
                                 &clienthello->dtls_cookie_len)) {
                al = SSL_AD_DECODE_ERROR;
                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
                goto f_err;
            }
            if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
                if (clienthello->dtls_cookie_len == 0)
                    return 1;
            }
        }
        if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
            al = SSL_AD_DECODE_ERROR;
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
            goto f_err;
        }
        if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
            al = SSL_AD_DECODE_ERROR;
            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
            goto f_err;
        }
        if (PACKET_remaining(pkt) == 0) {
            PACKET_null_init(&clienthello->extensions);
        } else {
            if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)) {
                al = SSL_AD_DECODE_ERROR;
                SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
                goto f_err;
            }
        }
    }
    if (!PACKET_copy_all(&compression, clienthello->compressions,
                         MAX_COMPRESSIONS_SIZE,
                         &clienthello->compressions_len)) {
        al = SSL_AD_DECODE_ERROR;
        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
        goto f_err;
    }
    extensions = clienthello->extensions;
    if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
                                &clienthello->pre_proc_exts, &al,
                                &clienthello->pre_proc_exts_len, 1)) {
        goto f_err;
    }
    s->clienthello = clienthello;
    return MSG_PROCESS_CONTINUE_PROCESSING;
 f_err:
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
 err:
    ossl_statem_set_error(s);
    OPENSSL_free(clienthello->pre_proc_exts);
    OPENSSL_free(clienthello);
    return MSG_PROCESS_ERROR;
}
['MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)\n{\n    int al = SSL_AD_INTERNAL_ERROR;\n    PACKET session_id, compression, extensions, cookie;\n    static const unsigned char null_compression = 0;\n    CLIENTHELLO_MSG *clienthello;\n    clienthello = OPENSSL_zalloc(sizeof(*clienthello));\n    if (clienthello == NULL) {\n        SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {\n        s->renegotiate = 1;\n        s->new_session = 1;\n    }\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 (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {\n            al = SSL_AD_HANDSHAKE_FAILURE;\n            SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);\n            goto f_err;\n        }\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->legacy_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        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_copy_bytes(pkt, clienthello->session_id, 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        clienthello->session_id_len = session_id_len;\n        challenge_len = challenge_len > SSL3_RANDOM_SIZE\n                        ? SSL3_RANDOM_SIZE : challenge_len;\n        memset(clienthello->random, 0, SSL3_RANDOM_SIZE);\n        if (!PACKET_copy_bytes(&challenge,\n                               clienthello->random + SSL3_RANDOM_SIZE -\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            || !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        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,\n                         &clienthello->compressions_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(s, &extensions, SSL_EXT_CLIENT_HELLO,\n                                &clienthello->pre_proc_exts, &al,\n                                &clienthello->pre_proc_exts_len, 1)) {\n        goto f_err;\n    }\n    s->clienthello = clienthello;\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    OPENSSL_free(clienthello->pre_proc_exts);\n    OPENSSL_free(clienthello);\n    return MSG_PROCESS_ERROR;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\n    return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}', 'void ossl_statem_set_error(SSL *s)\n{\n    s->statem.state = MSG_FLOW_ERROR;\n}']