crypto/cms/cms_smime.c:637: error: BUFFER_OVERRUN_L3
  Offset added: [4, +oo] (⇐ 4 + [0, +oo]) Size: [1, +oo] by call to `CMS_RecipientInfo_decrypt`.
Showing all 17 steps of the trace


crypto/cms/cms_smime.c:635:28: Call
633.            * all.
634.            */
635.           else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
                                  ^
636.               CMS_RecipientInfo_set0_pkey(ri, pk);
637.               r = CMS_RecipientInfo_decrypt(cms, ri);

crypto/cms/cms_env.c:269:1: Parameter `ri->d.ktri->rid->d.issuerAndSerialNumber->issuer->length`
  267.   }
  268.   
  269. > int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
  270.   {
  271.       if (ri->type != CMS_RECIPINFO_TRANS) {

crypto/cms/cms_smime.c:637:17: Call
635.           else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
636.               CMS_RecipientInfo_set0_pkey(ri, pk);
637.               r = CMS_RecipientInfo_decrypt(cms, ri);
                       ^
638.               CMS_RecipientInfo_set0_pkey(ri, NULL);
639.               if (cert) {

crypto/cms/cms_env.c:738:1: Parameter `ri->d.pwri->encryptedKey->length`
  736.   }
  737.   
  738. > int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  739.   {
  740.       switch (ri->type) {

crypto/cms/cms_env.c:748:16: Call
  746.   
  747.       case CMS_RECIPINFO_PASS:
  748.           return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
                        ^
  749.   
  750.       default:

crypto/cms/cms_pwri.c:275:1: Parameter `ri->d.pwri->encryptedKey->length`
    273.   /* Encrypt/Decrypt content key in PWRI recipient info */
    274.   
    275. > int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
    276.                                    int en_de)
    277.   {

crypto/cms/cms_pwri.c:368:14: Call
    366.               goto err;
    367.           }
    368.           if (!kek_unwrap_key(key, &keylen,
                        ^
    369.                               pwri->encryptedKey->data,
    370.                               pwri->encryptedKey->length, kekctx)) {

crypto/cms/cms_pwri.c:210:13: <Offset trace>
208.           || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
209.           /* Decrypt again */
210.           || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
                   ^
211.           goto err;
212.       /* Check check bytes */

crypto/cms/cms_pwri.c:210:13: Call
      208.           || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
      209.           /* Decrypt again */
      210.           || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
                         ^
      211.           goto err;
      212.       /* Check check bytes */

crypto/evp/evp_enc.c:419:1: Parameter `ctx->buf[*]`
        417.   }
        418.   
        419. > int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
        420.                         const unsigned char *in, int inl)
        421.   {

crypto/cms/cms_pwri.c:221:5: Assignment
      219.           goto err;
      220.       }
      221.       *outlen = (size_t)tmp[0];
                 ^
      222.       memcpy(out, tmp + 4, *outlen);
      223.       rv = 1;

crypto/cms/cms_pwri.c:176:1: <Length trace>
174.    */
175.   
176. > static int kek_unwrap_key(unsigned char *out, size_t *outlen,
177.                             const unsigned char *in, size_t inlen,
178.                             EVP_CIPHER_CTX *ctx)

crypto/cms/cms_pwri.c:176:1: Parameter `inlen`
      174.    */
      175.   
      176. > static int kek_unwrap_key(unsigned char *out, size_t *outlen,
      177.                             const unsigned char *in, size_t inlen,
      178.                             EVP_CIPHER_CTX *ctx)

crypto/cms/cms_pwri.c:191:11: Call
      189.           return 0;
      190.       }
      191.       tmp = OPENSSL_malloc(inlen);
                       ^
      192.       if (tmp == NULL)
      193.           return 0;

crypto/mem.c:165:9: Assignment
        163.   
        164.       if (num <= 0)
        165.           return NULL;
                       ^
        166.   
        167.       FAILTEST();

crypto/cms/cms_pwri.c:191:5: Assignment
      189.           return 0;
      190.       }
      191.       tmp = OPENSSL_malloc(inlen);
                 ^
      192.       if (tmp == NULL)
      193.           return 0;

crypto/cms/cms_pwri.c:222:5: Array access: Offset added: [4, +oo] (⇐ 4 + [0, +oo]) Size: [1, +oo] by call to `CMS_RecipientInfo_decrypt` 
      220.       }
      221.       *outlen = (size_t)tmp[0];
      222.       memcpy(out, tmp + 4, *outlen);
                 ^
      223.       rv = 1;
      224.    err:

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;
    }
    tmp = OPENSSL_malloc(inlen);
    if (tmp == NULL)
        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            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 (match_ri && !cert && !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_RECPIENTINFO_TYPE);\n        return 0;\n    }\n}', 'int cms_RecipientInfo_pwri_crypt(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        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    tmp = OPENSSL_malloc(inlen);\n    if (tmp == NULL)\n        return 0;\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;\n    unsigned int b;\n    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n        if (is_partially_overlapping(out, in, inl)) {\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_EncryptUpdate(ctx, out, outl, in, inl);\n    b = ctx->cipher->block_size;\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_EncryptUpdate(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    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    allow_customize = 0;\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    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}']