crypto/cms/cms_env.c:866: error: BUFFER_OVERRUN_L3
  Offset: 6 Size: [1, +oo] by call to `CMS_RecipientInfo_encrypt`.
Showing all 13 steps of the trace


crypto/cms/cms_env.c:866:13: Call
864.       for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
865.           ri = sk_CMS_RecipientInfo_value(rinfos, i);
866.           if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
                   ^
867.               CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
868.                      CMS_R_ERROR_SETTING_RECIPIENTINFO);

crypto/cms/cms_env.c:757:1: Parameter `ri->d.pwri->encryptedKey->length`
  755.   }
  756.   
  757. > int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  758.   {
  759.       switch (ri->type) {

crypto/cms/cms_env.c:866:13: Call
864.       for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
865.           ri = sk_CMS_RecipientInfo_value(rinfos, i);
866.           if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
                   ^
867.               CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
868.                      CMS_R_ERROR_SETTING_RECIPIENTINFO);

crypto/cms/cms_env.c:757:1: Parameter `ri->d.pwri->encryptedKey->length`
  755.   }
  756.   
  757. > int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
  758.   {
  759.       switch (ri->type) {

crypto/cms/cms_env.c:770:16: Call
  768.   
  769.       case CMS_RECIPINFO_PASS:
  770.           return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
                        ^
  771.   
  772.       default:

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

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

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:16: Call
      189.           return 0;
      190.       }
      191.       if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
                            ^
      192.           CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);
      193.           return 0;

crypto/mem.c:201:9: Assignment
        199.   
        200.       if (num == 0)
        201.           return NULL;
                       ^
        202.   
        203.       FAILTEST();

crypto/cms/cms_pwri.c:191:10: Assignment
      189.           return 0;
      190.       }
      191.       if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
                      ^
      192.           CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);
      193.           return 0;

crypto/cms/cms_pwri.c:214:60: Array access: Offset: 6 Size: [1, +oo] by call to `CMS_RecipientInfo_encrypt` 
      212.           goto err;
      213.       /* Check check bytes */
      214.       if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
                                                                        ^
      215.           /* Check byte failure */
      216.           goto 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;
    }
    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;
}
['BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)\n{\n    CMS_EncryptedContentInfo *ec;\n    STACK_OF(CMS_RecipientInfo) *rinfos;\n    CMS_RecipientInfo *ri;\n    int i, ok = 0;\n    BIO *ret;\n    ec = cms->d.envelopedData->encryptedContentInfo;\n    ret = cms_EncryptedContent_init_bio(ec);\n    if (!ret || !ec->cipher)\n        return ret;\n    rinfos = cms->d.envelopedData->recipientInfos;\n    for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {\n        ri = sk_CMS_RecipientInfo_value(rinfos, i);\n        if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {\n            CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,\n                   CMS_R_ERROR_SETTING_RECIPIENTINFO);\n            goto err;\n        }\n    }\n    cms_env_set_version(cms->d.envelopedData);\n    ok = 1;\n err:\n    ec->cipher = NULL;\n    OPENSSL_clear_free(ec->key, ec->keylen);\n    ec->key = NULL;\n    ec->keylen = 0;\n    if (ok)\n        return ret;\n    BIO_free(ret);\n    return NULL;\n}', 'int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)\n{\n    switch (ri->type) {\n    case CMS_RECIPINFO_TRANS:\n        return cms_RecipientInfo_ktri_encrypt(cms, ri);\n    case CMS_RECIPINFO_AGREE:\n        return cms_RecipientInfo_kari_encrypt(cms, ri);\n    case CMS_RECIPINFO_KEK:\n        return cms_RecipientInfo_kekri_encrypt(cms, ri);\n    case CMS_RECIPINFO_PASS:\n        return cms_RecipientInfo_pwri_crypt(cms, ri, 1);\n    default:\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,\n               CMS_R_UNSUPPORTED_RECIPIENT_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    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}', '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}']