int WPACKET_reserve_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;
    return 1;
}
['static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_GOST\n    EVP_PKEY_CTX *pkey_ctx = NULL;\n    X509 *peer_cert;\n    size_t msglen;\n    unsigned int md_len;\n    unsigned char shared_ukm[32], tmp[256];\n    EVP_MD_CTX *ukm_hash = NULL;\n    int dgst_nid = NID_id_GostR3411_94;\n    unsigned char *pms = NULL;\n    size_t pmslen = 0;\n    if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)\n        dgst_nid = NID_id_GostR3411_2012_256;\n    peer_cert = s->session->peer;\n    if (!peer_cert) {\n        *al = SSL_AD_HANDSHAKE_FAILURE;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,\n               SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);\n        return 0;\n    }\n    pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);\n    if (pkey_ctx == NULL) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    pmslen = 32;\n    pms = OPENSSL_malloc(pmslen);\n    if (pms == NULL) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0\n        || RAND_bytes(pms, pmslen) <= 0) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);\n        goto err;\n    };\n    ukm_hash = EVP_MD_CTX_new();\n    if (ukm_hash == NULL\n        || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0\n        || EVP_DigestUpdate(ukm_hash, s->s3->client_random,\n                            SSL3_RANDOM_SIZE) <= 0\n        || EVP_DigestUpdate(ukm_hash, s->s3->server_random,\n                            SSL3_RANDOM_SIZE) <= 0\n        || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    EVP_MD_CTX_free(ukm_hash);\n    ukm_hash = NULL;\n    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,\n                          EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);\n        goto err;\n    }\n    msglen = 255;\n    if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);\n        goto err;\n    }\n    if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)\n            || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))\n            || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {\n        *al = SSL_AD_INTERNAL_ERROR;\n        SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    EVP_PKEY_CTX_free(pkey_ctx);\n    s->s3->tmp.pms = pms;\n    s->s3->tmp.pmslen = pmslen;\n    return 1;\n err:\n    EVP_PKEY_CTX_free(pkey_ctx);\n    OPENSSL_clear_free(pms, pmslen);\n    EVP_MD_CTX_free(ukm_hash);\n    return 0;\n#else\n    SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);\n    *al = SSL_AD_INTERNAL_ERROR;\n    return 0;\n#endif\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    if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n        return 0;\n    pkt->written += len;\n    pkt->curr += len;\n    return 1;\n}', 'int WPACKET_reserve_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    return 1;\n}']