static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
    pkt->curr += len;
    pkt->remaining -= len;
}
['static int tls_early_post_process_client_hello(SSL *s, int *pal)\n{\n    unsigned int j;\n    int i, al = SSL_AD_INTERNAL_ERROR;\n    int protverr;\n    size_t loop;\n    unsigned long id;\n#ifndef OPENSSL_NO_COMP\n    SSL_COMP *comp = NULL;\n#endif\n    const SSL_CIPHER *c;\n    STACK_OF(SSL_CIPHER) *ciphers = NULL;\n    STACK_OF(SSL_CIPHER) *scsvs = NULL;\n    CLIENTHELLO_MSG *clienthello = s->clienthello;\n    DOWNGRADE dgrd = DOWNGRADE_NONE;\n    if (s->ctx->early_cb != NULL) {\n        int code;\n        code = s->ctx->early_cb(s, &al, s->ctx->early_cb_arg);\n        if (code == 0)\n            goto err;\n        if (code < 0) {\n            s->rwstate = SSL_EARLY_WORK;\n            return code;\n        }\n    }\n    memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);\n    if (clienthello->isv2) {\n        if (clienthello->legacy_version == SSL2_VERSION\n                || (clienthello->legacy_version & 0xff00)\n                   != (SSL3_VERSION_MAJOR << 8)) {\n            SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);\n            goto err;\n        }\n        s->client_version = clienthello->legacy_version;\n    }\n    if (!SSL_IS_DTLS(s)) {\n        protverr = ssl_choose_server_version(s, clienthello, &dgrd);\n    } else if (s->method->version != DTLS_ANY_VERSION &&\n               DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {\n        protverr = SSL_R_VERSION_TOO_LOW;\n    } else {\n        protverr = 0;\n    }\n    if (protverr) {\n        SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);\n        if (SSL_IS_FIRST_HANDSHAKE(s)) {\n            s->version = s->client_version = clienthello->legacy_version;\n        }\n        al = SSL_AD_PROTOCOL_VERSION;\n        goto err;\n    }\n    if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {\n        al = SSL_AD_UNEXPECTED_MESSAGE;\n        SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,\n               SSL_R_NOT_ON_RECORD_BOUNDARY);\n        goto 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_EARLY_POST_PROCESS_CLIENT_HELLO,\n                           SSL_R_COOKIE_MISMATCH);\n                    goto 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_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);\n                goto 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, &dgrd);\n            if (protverr != 0) {\n                SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);\n                s->version = s->client_version;\n                al = SSL_AD_PROTOCOL_VERSION;\n                goto err;\n            }\n        }\n    }\n    s->hit = 0;\n    if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,\n                             SSL_EXT_CLIENT_HELLO,\n                             clienthello->pre_proc_exts, NULL, 0, &al)) {\n        SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n        goto 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, &al);\n        if (i == 1) {\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_cache_cipherlist(s, &clienthello->ciphersuites,\n                              clienthello->isv2, &al) ||\n        !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,\n                             clienthello->isv2, &al)) {\n        goto err;\n    }\n    s->s3->send_connection_binding = 0;\n    if (scsvs != NULL) {\n        for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {\n            c = sk_SSL_CIPHER_value(scsvs, i);\n            if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {\n                if (s->renegotiate) {\n                    SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,\n                           SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);\n                    al = SSL_AD_HANDSHAKE_FAILURE;\n                    goto err;\n                }\n                s->s3->send_connection_binding = 1;\n            } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&\n                       !ssl_check_version_downgrade(s)) {\n                SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,\n                       SSL_R_INAPPROPRIATE_FALLBACK);\n                al = SSL_AD_INAPPROPRIATE_FALLBACK;\n                goto err;\n            }\n        }\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_EARLY_POST_PROCESS_CLIENT_HELLO,\n                   SSL_R_REQUIRED_CIPHER_MISSING);\n            goto 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_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);\n        goto err;\n    }\n#ifndef OPENSSL_NO_EC\n    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)\n        ssl_check_for_safari(s, clienthello);\n#endif\n    if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,\n                                  clienthello->pre_proc_exts, NULL, 0, &al)) {\n        SSLerr(SSL_F_TLS_EARLY_POST_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, dgrd) <= 0) {\n            goto err;\n        }\n    }\n    if (!s->hit && s->version >= TLS1_VERSION && s->ext.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->ext.session_secret_cb(s, s->session->master_key,\n                                     &master_key_length, ciphers,\n                                     &pref_cipher,\n                                     s->ext.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            if (pref_cipher == NULL)\n                pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,\n                                                 SSL_get_ciphers(s));\n            if (pref_cipher == NULL) {\n                al = SSL_AD_HANDSHAKE_FAILURE;\n                SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);\n                goto 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 && !SSL_IS_TLS13(s)) {\n        int m, comp_id = s->session->compress_meth;\n        unsigned int k;\n        if (!ssl_allow_compression(s)) {\n            SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,\n                   SSL_R_INCONSISTENT_COMPRESSION);\n            goto 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_EARLY_POST_PROCESS_CLIENT_HELLO,\n                   SSL_R_INVALID_COMPRESSION_ALGORITHM);\n            goto 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_EARLY_POST_PROCESS_CLIENT_HELLO,\n                   SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);\n            goto err;\n        }\n    } else if (s->hit) {\n        comp = NULL;\n    } else if (ssl_allow_compression(s) && s->ctx->comp_methods\n                   && !SSL_IS_TLS13(s)) {\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_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);\n        goto err;\n    }\n#endif\n    if (!s->hit || s->hello_retry_request) {\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_EARLY_POST_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        ciphers = NULL;\n    }\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        if (!tls1_set_server_sigalgs(s)) {\n            SSLerr(SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);\n            goto err;\n        }\n    }\n    sk_SSL_CIPHER_free(ciphers);\n    sk_SSL_CIPHER_free(scsvs);\n    OPENSSL_free(clienthello->pre_proc_exts);\n    OPENSSL_free(s->clienthello);\n    s->clienthello = NULL;\n    return 1;\n err:\n    ossl_statem_set_error(s);\n    *pal = al;\n    sk_SSL_CIPHER_free(ciphers);\n    sk_SSL_CIPHER_free(scsvs);\n    OPENSSL_free(clienthello->pre_proc_exts);\n    OPENSSL_free(s->clienthello);\n    s->clienthello = NULL;\n    return 0;\n}', 'int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format,\n                         int *al)\n{\n    int n;\n    n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;\n    if (PACKET_remaining(cipher_suites) == 0) {\n        SSLerr(SSL_F_SSL_CACHE_CIPHERLIST, SSL_R_NO_CIPHERS_SPECIFIED);\n        *al = SSL_AD_ILLEGAL_PARAMETER;\n        return 0;\n    }\n    if (PACKET_remaining(cipher_suites) % n != 0) {\n        SSLerr(SSL_F_SSL_CACHE_CIPHERLIST,\n               SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);\n        *al = SSL_AD_DECODE_ERROR;\n        return 0;\n    }\n    OPENSSL_free(s->s3->tmp.ciphers_raw);\n    s->s3->tmp.ciphers_raw = NULL;\n    s->s3->tmp.ciphers_rawlen = 0;\n    if (sslv2format) {\n        size_t numciphers = PACKET_remaining(cipher_suites) / n;\n        PACKET sslv2ciphers = *cipher_suites;\n        unsigned int leadbyte;\n        unsigned char *raw;\n        raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);\n        s->s3->tmp.ciphers_raw = raw;\n        if (raw == NULL) {\n            *al = SSL_AD_INTERNAL_ERROR;\n            goto err;\n        }\n        for (s->s3->tmp.ciphers_rawlen = 0;\n             PACKET_remaining(&sslv2ciphers) > 0;\n             raw += TLS_CIPHER_LEN) {\n            if (!PACKET_get_1(&sslv2ciphers, &leadbyte)\n                    || (leadbyte == 0\n                        && !PACKET_copy_bytes(&sslv2ciphers, raw,\n                                              TLS_CIPHER_LEN))\n                    || (leadbyte != 0\n                        && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {\n                *al = SSL_AD_INTERNAL_ERROR;\n                OPENSSL_free(s->s3->tmp.ciphers_raw);\n                s->s3->tmp.ciphers_raw = NULL;\n                s->s3->tmp.ciphers_rawlen = 0;\n                goto err;\n            }\n            if (leadbyte == 0)\n                s->s3->tmp.ciphers_rawlen += TLS_CIPHER_LEN;\n        }\n    } else if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,\n                           &s->s3->tmp.ciphers_rawlen)) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        goto err;\n    }\n    return 1;\n err:\n    return 0;\n}', "int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,\n                         STACK_OF(SSL_CIPHER) **skp,\n                         STACK_OF(SSL_CIPHER) **scsvs_out,\n                         int sslv2format, int *al)\n{\n    const SSL_CIPHER *c;\n    STACK_OF(SSL_CIPHER) *sk = NULL;\n    STACK_OF(SSL_CIPHER) *scsvs = NULL;\n    int n;\n    unsigned char cipher[SSLV2_CIPHER_LEN];\n    n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;\n    if (PACKET_remaining(cipher_suites) == 0) {\n        SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);\n        *al = SSL_AD_ILLEGAL_PARAMETER;\n        return 0;\n    }\n    if (PACKET_remaining(cipher_suites) % n != 0) {\n        SSLerr(SSL_F_BYTES_TO_CIPHER_LIST,\n               SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);\n        *al = SSL_AD_DECODE_ERROR;\n        return 0;\n    }\n    sk = sk_SSL_CIPHER_new_null();\n    scsvs = sk_SSL_CIPHER_new_null();\n    if (sk == NULL || scsvs == NULL) {\n        SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);\n        *al = SSL_AD_INTERNAL_ERROR;\n        goto err;\n    }\n    while (PACKET_copy_bytes(cipher_suites, cipher, n)) {\n        if (sslv2format && cipher[0] != '\\0')\n            continue;\n        c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);\n        if (c != NULL) {\n            if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||\n                (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {\n                SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);\n                *al = SSL_AD_INTERNAL_ERROR;\n                goto err;\n            }\n        }\n    }\n    if (PACKET_remaining(cipher_suites) > 0) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if (skp != NULL)\n        *skp = sk;\n    else\n        sk_SSL_CIPHER_free(sk);\n    if (scsvs_out != NULL)\n        *scsvs_out = scsvs;\n    else\n        sk_SSL_CIPHER_free(scsvs);\n    return 1;\n err:\n    sk_SSL_CIPHER_free(sk);\n    sk_SSL_CIPHER_free(scsvs);\n    return 0;\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}']