static int kek_unwrap_key(unsigned char *out, size_t *outlen,
                          const unsigned char *in, size_t inlen,
                          EVP_CIPHER_CTX *ctx)
{
    size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
    unsigned char *tmp;
    int outl, rv = 0;
    if (inlen < 2 * blocklen) {
        return 0;
    }
    if (inlen % blocklen) {
        return 0;
    }
    if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
        CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
                           in + inlen - 2 * blocklen, blocklen * 2)
        || !EVP_DecryptUpdate(ctx, tmp, &outl,
                              tmp + inlen - blocklen, blocklen)
        || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
        || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
        || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
        goto err;
    if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
        goto err;
    }
    if (inlen < (size_t)(tmp[0] - 4)) {
        goto err;
    }
    *outlen = (size_t)tmp[0];
    memcpy(out, tmp + 4, *outlen);
    rv = 1;
 err:
    OPENSSL_clear_free(tmp, inlen);
    return rv;
}
['int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)\n{\n    STACK_OF(CMS_RecipientInfo) *ris;\n    CMS_RecipientInfo *ri;\n    int i, r, ri_type;\n    int debug = 0, match_ri = 0;\n    ris = CMS_get0_RecipientInfos(cms);\n    if (ris)\n        debug = cms->d.envelopedData->encryptedContentInfo->debug;\n    ri_type = cms_pkey_get_ri_type(pk);\n    if (ri_type == CMS_RECIPINFO_NONE) {\n        CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,\n               CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);\n        return 0;\n    }\n    for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {\n        ri = sk_CMS_RecipientInfo_value(ris, i);\n        if (CMS_RecipientInfo_type(ri) != ri_type)\n            continue;\n        match_ri = 1;\n        if (ri_type == CMS_RECIPINFO_AGREE) {\n            r = cms_kari_set1_pkey(cms, ri, pk, cert);\n            if (r > 0)\n                return 1;\n            if (r < 0)\n                return 0;\n        }\n        else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {\n            EVP_PKEY_up_ref(pk);\n            CMS_RecipientInfo_set0_pkey(ri, pk);\n            r = CMS_RecipientInfo_decrypt(cms, ri);\n            CMS_RecipientInfo_set0_pkey(ri, NULL);\n            if (cert) {\n                if (!debug) {\n                    ERR_clear_error();\n                    return 1;\n                }\n                if (r > 0)\n                    return 1;\n                CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);\n                return 0;\n            }\n            else if (r > 0 && debug)\n                return 1;\n        }\n    }\n    if (cert == NULL && ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) {\n        ERR_clear_error();\n        return 1;\n    }\n    CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);\n    return 0;\n}', 'int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)\n{\n    if (ri->type != CMS_RECIPINFO_TRANS) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,\n               CMS_R_NOT_KEY_TRANSPORT);\n        return -2;\n    }\n    return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);\n}', 'int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)\n{\n    switch (ri->type) {\n    case CMS_RECIPINFO_TRANS:\n        return cms_RecipientInfo_ktri_decrypt(cms, ri);\n    case CMS_RECIPINFO_KEK:\n        return cms_RecipientInfo_kekri_decrypt(cms, ri);\n    case CMS_RECIPINFO_PASS:\n        return cms_RecipientInfo_pwri_crypt(cms, ri, 0);\n    default:\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,\n               CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);\n        return 0;\n    }\n}', 'int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri,\n                                 int en_de)\n{\n    CMS_EncryptedContentInfo *ec;\n    CMS_PasswordRecipientInfo *pwri;\n    int r = 0;\n    X509_ALGOR *algtmp, *kekalg = NULL;\n    EVP_CIPHER_CTX *kekctx = NULL;\n    const EVP_CIPHER *kekcipher;\n    unsigned char *key = NULL;\n    size_t keylen;\n    ec = cms->d.envelopedData->encryptedContentInfo;\n    pwri = ri->d.pwri;\n    if (!pwri->pass) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);\n        return 0;\n    }\n    algtmp = pwri->keyEncryptionAlgorithm;\n    if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);\n        return 0;\n    }\n    kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),\n                                       algtmp->parameter);\n    if (kekalg == NULL) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);\n        return 0;\n    }\n    kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);\n    if (!kekcipher) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);\n        return 0;\n    }\n    kekctx = EVP_CIPHER_CTX_new();\n    if (kekctx == NULL) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))\n        goto err;\n    EVP_CIPHER_CTX_set_padding(kekctx, 0);\n    if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n               CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n        goto err;\n    }\n    algtmp = pwri->keyDerivationAlgorithm;\n    if (EVP_PBE_CipherInit(algtmp->algorithm,\n                           (char *)pwri->pass, pwri->passlen,\n                           algtmp->parameter, kekctx, en_de) < 0) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);\n        goto err;\n    }\n    if (en_de) {\n        if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))\n            goto err;\n        key = OPENSSL_malloc(keylen);\n        if (key == NULL)\n            goto err;\n        if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))\n            goto err;\n        pwri->encryptedKey->data = key;\n        pwri->encryptedKey->length = keylen;\n    } else {\n        key = OPENSSL_malloc(pwri->encryptedKey->length);\n        if (key == NULL) {\n            CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (!kek_unwrap_key(key, &keylen,\n                            pwri->encryptedKey->data,\n                            pwri->encryptedKey->length, kekctx)) {\n            CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);\n            goto err;\n        }\n        OPENSSL_clear_free(ec->key, ec->keylen);\n        ec->key = key;\n        ec->keylen = keylen;\n    }\n    r = 1;\n err:\n    EVP_CIPHER_CTX_free(kekctx);\n    if (!r)\n        OPENSSL_free(key);\n    X509_ALGOR_free(kekalg);\n    return r;\n}', 'static int kek_unwrap_key(unsigned char *out, size_t *outlen,\n                          const unsigned char *in, size_t inlen,\n                          EVP_CIPHER_CTX *ctx)\n{\n    size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);\n    unsigned char *tmp;\n    int outl, rv = 0;\n    if (inlen < 2 * blocklen) {\n        return 0;\n    }\n    if (inlen % blocklen) {\n        return 0;\n    }\n    if ((tmp = OPENSSL_malloc(inlen)) == NULL) {\n        CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,\n                           in + inlen - 2 * blocklen, blocklen * 2)\n        || !EVP_DecryptUpdate(ctx, tmp, &outl,\n                              tmp + inlen - blocklen, blocklen)\n        || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)\n        || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)\n        || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))\n        goto err;\n    if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {\n        goto err;\n    }\n    if (inlen < (size_t)(tmp[0] - 4)) {\n        goto err;\n    }\n    *outlen = (size_t)tmp[0];\n    memcpy(out, tmp + 4, *outlen);\n    rv = 1;\n err:\n    OPENSSL_clear_free(tmp, inlen);\n    return rv;\n}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n                      const unsigned char *in, int inl)\n{\n    int fix_len, cmpl = inl;\n    unsigned int b;\n    if (ctx->encrypt) {\n        EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);\n        return 0;\n    }\n    b = ctx->cipher->block_size;\n    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))\n        cmpl = (cmpl + 7) / 8;\n    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n        if (b == 1 && is_partially_overlapping(out, in, cmpl)) {\n            EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n            return 0;\n        }\n        fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n        if (fix_len < 0) {\n            *outl = 0;\n            return 0;\n        } else\n            *outl = fix_len;\n        return 1;\n    }\n    if (inl <= 0) {\n        *outl = 0;\n        return inl == 0;\n    }\n    if (ctx->flags & EVP_CIPH_NO_PADDING)\n        return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);\n    OPENSSL_assert(b <= sizeof(ctx->final));\n    if (ctx->final_used) {\n        if (((PTRDIFF_T)out == (PTRDIFF_T)in)\n            || is_partially_overlapping(out, in, b)) {\n            EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n            return 0;\n        }\n        memcpy(out, ctx->final, b);\n        out += b;\n        fix_len = 1;\n    } else\n        fix_len = 0;\n    if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))\n        return 0;\n    if (b > 1 && !ctx->buf_len) {\n        *outl -= b;\n        ctx->final_used = 1;\n        memcpy(ctx->final, &out[*outl], b);\n    } else\n        ctx->final_used = 0;\n    if (fix_len)\n        *outl += b;\n    return 1;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    INCREMENT(malloc_count);\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    if (allow_customize) {\n        allow_customize = 0;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']