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->staticbuf == NULL && (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;
    }
    if (allocbytes != NULL)
        *allocbytes = WPACKET_get_curr(pkt);
    return 1;
}
['int tls_construct_cert_verify(SSL *s, WPACKET *pkt)\n{\n    EVP_PKEY *pkey;\n    const EVP_MD *md;\n    EVP_MD_CTX *mctx = NULL;\n    EVP_PKEY_CTX *pctx = NULL;\n    size_t hdatalen = 0, siglen = 0;\n    void *hdata;\n    unsigned char *sig = NULL;\n    unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];\n    int pktype, ispss = 0;\n    if (s->server) {\n        pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md);\n        if (pkey == NULL)\n            goto err;\n    } else {\n        md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];\n        pkey = s->cert->key->privatekey;\n    }\n    pktype = EVP_PKEY_id(pkey);\n    mctx = EVP_MD_CTX_new();\n    if (mctx == NULL) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if (SSL_USE_SIGALGS(s) && !tls12_get_sigandhash(s, pkt, pkey, md, &ispss)) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n#ifdef SSL_DEBUG\n    fprintf(stderr, "Using client alg %s\\n", EVP_MD_name(md));\n#endif\n    siglen = EVP_PKEY_size(pkey);\n    sig = OPENSSL_malloc(siglen);\n    if (sig == NULL) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0\n            || EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);\n        goto err;\n    }\n    if (ispss) {\n        if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0\n                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1) <= 0) {\n            SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);\n            goto err;\n        }\n    } else if (s->version == SSL3_VERSION) {\n        if (!EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,\n                             (int)s->session->master_key_length,\n                             s->session->master_key)) {\n            SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);\n            goto err;\n        }\n    }\n    if (EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_EVP_LIB);\n        goto err;\n    }\n#ifndef OPENSSL_NO_GOST\n    {\n        if (pktype == NID_id_GostR3410_2001\n            || pktype == NID_id_GostR3410_2012_256\n            || pktype == NID_id_GostR3410_2012_512)\n            BUF_reverse(sig, NULL, siglen);\n    }\n#endif\n    if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if (!ssl3_digest_cached_records(s, 0))\n        goto err;\n    OPENSSL_free(sig);\n    EVP_MD_CTX_free(mctx);\n    return 1;\n err:\n    OPENSSL_free(sig);\n    EVP_MD_CTX_free(mctx);\n    ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n    return 0;\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_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 - GETBUF(pkt);\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    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->staticbuf == NULL && (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    if (allocbytes != NULL)\n        *allocbytes = WPACKET_get_curr(pkt);\n    return 1;\n}']