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_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_NEXTPROTONEG\n    int next_proto_neg_seen;\n#endif\n#ifndef OPENSSL_NO_EC\n    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n    unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n    int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);\n    using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);\n#endif\n    if (!WPACKET_start_sub_packet_u16(pkt)\n            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) {\n        SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->s3->send_connection_binding &&\n            !ssl_add_serverhello_renegotiate_ext(s, pkt)) {\n        SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->version == SSL3_VERSION)\n        goto done;\n    if (!s->hit && s->servername_done == 1\n            && s->session->tlsext_hostname != NULL) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)\n                || !WPACKET_put_bytes_u16(pkt, 0)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#ifndef OPENSSL_NO_EC\n    if (using_ecc) {\n        const unsigned char *plist;\n        size_t plistlen;\n        tls1_get_formatlist(s, &plist, &plistlen);\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, plist, plistlen)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    if (s->tlsext_ticket_expected && tls_use_ticket(s)) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)\n                || !WPACKET_put_bytes_u16(pkt, 0)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    } else {\n        s->tlsext_ticket_expected = 0;\n    }\n    if (s->tlsext_status_expected) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)\n                || !WPACKET_put_bytes_u16(pkt, 0)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#ifndef OPENSSL_NO_SRTP\n    if (SSL_IS_DTLS(s) && s->srtp_profile) {\n        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_put_bytes_u16(pkt, 2)\n                || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)\n                || !WPACKET_put_bytes_u8(pkt, 0)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n    if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80\n         || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)\n        && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {\n        const unsigned char cryptopro_ext[36] = {\n            0xfd, 0xe8,\n            0x00, 0x20,\n            0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,\n            0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,\n            0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,\n            0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17\n        };\n        if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#ifndef OPENSSL_NO_HEARTBEATS\n    if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {\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_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n    next_proto_neg_seen = s->s3->next_proto_neg_seen;\n    s->s3->next_proto_neg_seen = 0;\n    if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {\n        const unsigned char *npa;\n        unsigned int npalen;\n        int r;\n        r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,\n                                              s->\n                                              ctx->next_protos_advertised_cb_arg);\n        if (r == SSL_TLSEXT_ERR_OK) {\n            if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)\n                    || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {\n                SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n            s->s3->next_proto_neg_seen = 1;\n        }\n    }\n#endif\n    if (!custom_ext_add(s, 1, pkt, al)) {\n        SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {\n        if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD\n            || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4\n            || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT\n            || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)\n            s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;\n        else {\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_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n                return 0;\n            }\n        }\n    }\n    if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {\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_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n    if (s->s3->alpn_selected != NULL) {\n        if (!WPACKET_put_bytes_u16(pkt,\n                    TLSEXT_TYPE_application_layer_protocol_negotiation)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_start_sub_packet_u16(pkt)\n                || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,\n                                          s->s3->alpn_selected_len)\n                || !WPACKET_close(pkt)\n                || !WPACKET_close(pkt)) {\n            SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n            return 0;\n        }\n    }\n done:\n    if (!WPACKET_close(pkt)) {\n        SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n    WPACKET_SUB *sub;\n    unsigned char *lenchars;\n    assert(pkt->subs != NULL);\n    if (pkt->subs == NULL)\n        return 0;\n    sub = OPENSSL_zalloc(sizeof(*sub));\n    if (sub == NULL)\n        return 0;\n    sub->parent = pkt->subs;\n    pkt->subs = sub;\n    sub->pwritten = pkt->written + lenbytes;\n    sub->lenbytes = lenbytes;\n    if (lenbytes == 0) {\n        sub->packet_len = 0;\n        return 1;\n    }\n    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n        return 0;\n    sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;\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_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n                         size_t lenbytes)\n{\n    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n            || !WPACKET_memcpy(pkt, src, len)\n            || !WPACKET_close(pkt))\n        return 0;\n    return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n    unsigned char *dest;\n    if (len == 0)\n        return 1;\n    if (!WPACKET_allocate_bytes(pkt, len, &dest))\n        return 0;\n    memcpy(dest, src, len);\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}']