int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
    assert(pkt->subs != NULL && len != 0);
    if (pkt->subs == NULL || len == 0)
        return 0;
    if (pkt->maxsize - pkt->written < len)
        return 0;
    if (pkt->buf->length - pkt->written < len) {
        size_t newlen;
        size_t reflen;
        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
        if (reflen > SIZE_MAX / 2) {
            newlen = SIZE_MAX;
        } else {
            newlen = reflen * 2;
            if (newlen < DEFAULT_BUF_SIZE)
                newlen = DEFAULT_BUF_SIZE;
        }
        if (BUF_MEM_grow(pkt->buf, newlen) == 0)
            return 0;
    }
    *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;
    pkt->written += len;
    pkt->curr += len;
    return 1;
}
['int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_EC\n    int using_ecc = 0;\n    if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {\n        int i;\n        unsigned long alg_k, alg_a;\n        STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);\n        for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {\n            const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);\n            alg_k = c->algorithm_mkey;\n            alg_a = c->algorithm_auth;\n            if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))\n                || (alg_a & SSL_aECDSA)) {\n                using_ecc = 1;\n                break;\n            }\n        }\n    }\n#endif\n    if (s->renegotiate) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,\n                                   s->s3->previous_client_finished_len)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n    if (s->client_version == SSL3_VERSION)\n        goto done;\n    if (s->tlsext_hostname != NULL) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)\n                || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,\n                                           strlen(s->tlsext_hostname))\n                || !WPACKET_close(pkt)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#ifndef OPENSSL_NO_SRP\n    if (s->srp_ctx.login != NULL) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u8(pkt)\n                || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)\n                || !WPACKET_memcpy(pkt, s->srp_ctx.login,\n                                   strlen(s->srp_ctx.login))\n                || !WPACKET_close(pkt)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n#ifndef OPENSSL_NO_EC\n    if (using_ecc) {\n        const unsigned char *pcurves, *pformats;\n        size_t num_curves, num_formats;\n        size_t i;\n        tls1_get_formatlist(s, &pformats, &num_formats);\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        pcurves = s->tlsext_ellipticcurvelist;\n        if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_elliptic_curves)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        for (i = 0; i < num_curves; i++, pcurves += 2) {\n            if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {\n                if (!WPACKET_put_bytes_u8(pkt, pcurves[0])\n                    || !WPACKET_put_bytes_u8(pkt, pcurves[1])) {\n                        SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,\n                               ERR_R_INTERNAL_ERROR);\n                        return 0;\n                    }\n            }\n        }\n        if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    if (tls_use_ticket(s)) {\n        int ticklen;\n        if (!s->new_session && s->session && s->session->tlsext_tick)\n            ticklen = s->session->tlsext_ticklen;\n        else if (s->session && s->tlsext_session_ticket &&\n                 s->tlsext_session_ticket->data) {\n            ticklen = s->tlsext_session_ticket->length;\n            s->session->tlsext_tick = OPENSSL_malloc(ticklen);\n            if (s->session->tlsext_tick == NULL) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n            memcpy(s->session->tlsext_tick,\n                   s->tlsext_session_ticket->data, ticklen);\n            s->session->tlsext_ticklen = ticklen;\n        } else\n            ticklen = 0;\n        if (ticklen == 0 && s->tlsext_session_ticket &&\n            s->tlsext_session_ticket->data == NULL)\n            goto skip_ext;\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)\n                || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick,\n                                           ticklen)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n skip_ext:\n    if (SSL_CLIENT_USE_SIGALGS(s)) {\n        size_t salglen;\n        const unsigned char *salg;\n        salglen = tls12_get_psigalgs(s, &salg);\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !tls12_copy_sigalgs(s, pkt, salg, salglen)\n                || !WPACKET_close(pkt)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#ifndef OPENSSL_NO_OCSP\n    if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {\n        int i;\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)\n                || !WPACKET_start_sub_packet_u16(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {\n            unsigned char *idbytes;\n            int idlen;\n            OCSP_RESPID *id;\n            id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);\n            idlen = i2d_OCSP_RESPID(id, NULL);\n            if (idlen <= 0\n                    || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)\n                    || i2d_OCSP_RESPID(id, &idbytes) != idlen) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n        }\n        if (!WPACKET_close(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        if (s->tlsext_ocsp_exts) {\n            unsigned char *extbytes;\n            int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);\n            if (extlen < 0) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n            if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)\n                    || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)\n                       != extlen) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n           }\n        }\n        if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n#ifndef OPENSSL_NO_HEARTBEATS\n    if (SSL_IS_DTLS(s)) {\n        unsigned int mode;\n        if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)\n            mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;\n        else\n            mode = SSL_DTLSEXT_HB_ENABLED;\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_put_bytes_u8(pkt, mode)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n    if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)\n                || !WPACKET_put_bytes_u16(pkt, 0)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {\n        if (!WPACKET_put_bytes_u16(pkt,\n                    TLSEXT_TYPE_application_layer_protocol_negotiation)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,\n                                           s->alpn_client_proto_list_len)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        s->s3->alpn_sent = 1;\n    }\n#ifndef OPENSSL_NO_SRTP\n    if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {\n        STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = 0;\n        SRTP_PROTECTION_PROFILE *prof;\n        int i, ct;\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);\n        for (i = 0; i < ct; i++) {\n            prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);\n            if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n        }\n        if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    custom_ext_init(&s->cert->cli_ext);\n    if (!custom_ext_add(s, 0, pkt, al)) {\n        SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)\n            || !WPACKET_put_bytes_u16(pkt, 0)) {\n        SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n#ifndef OPENSSL_NO_CT\n    if (s->ct_validation_callback != NULL) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)\n                || !WPACKET_put_bytes_u16(pkt, 0)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)\n            || !WPACKET_put_bytes_u16(pkt, 0)) {\n        SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->options & SSL_OP_TLSEXT_PADDING) {\n        unsigned char *padbytes;\n        size_t hlen;\n        if (!WPACKET_get_total_written(pkt, &hlen)) {\n            SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n        if (hlen > 0xff && hlen < 0x200) {\n            hlen = 0x200 - hlen;\n            if (hlen >= 4)\n                hlen -= 4;\n            else\n                hlen = 0;\n            if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)\n                    || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {\n                SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n            memset(padbytes, 0, hlen);\n        }\n    }\n done:\n    return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n    unsigned char *data;\n    assert(size <= sizeof(unsigned int));\n    if (size > sizeof(unsigned int)\n            || !WPACKET_allocate_bytes(pkt, size, &data)\n            || !put_value(data, val, size))\n        return 0;\n    return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n    assert(pkt->subs != NULL && len != 0);\n    if (pkt->subs == NULL || len == 0)\n        return 0;\n    if (pkt->maxsize - pkt->written < len)\n        return 0;\n    if (pkt->buf->length - pkt->written < len) {\n        size_t newlen;\n        size_t reflen;\n        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n        if (reflen > SIZE_MAX / 2) {\n            newlen = SIZE_MAX;\n        } else {\n            newlen = reflen * 2;\n            if (newlen < DEFAULT_BUF_SIZE)\n                newlen = DEFAULT_BUF_SIZE;\n        }\n        if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n            return 0;\n    }\n    *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;\n    pkt->written += len;\n    pkt->curr += len;\n    return 1;\n}']