crypto/cms/cms_sd.c:849: error: MEMORY_LEAK
  memory dynamically allocated by call to `EVP_MD_CTX_new()` at line 780, column 24 is not reachable after line 849, column 5.
Showing all 94 steps of the trace


crypto/cms/cms_sd.c:777:1: start of procedure CMS_SignerInfo_verify_content()
775.   }
776.   
777. > int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
778.   {
779.       ASN1_OCTET_STRING *os = NULL;

crypto/cms/cms_sd.c:779:5: 
777.   int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
778.   {
779. >     ASN1_OCTET_STRING *os = NULL;
780.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
781.       EVP_PKEY_CTX *pkctx = NULL;

crypto/cms/cms_sd.c:780:5: 
778.   {
779.       ASN1_OCTET_STRING *os = NULL;
780. >     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
781.       EVP_PKEY_CTX *pkctx = NULL;
782.       int r = -1;

crypto/evp/digest.c:44:1: start of procedure EVP_MD_CTX_new()
  42.   }
  43.   
  44. > EVP_MD_CTX *EVP_MD_CTX_new(void)
  45.   {
  46.       return OPENSSL_zalloc(sizeof(EVP_MD_CTX));

crypto/evp/digest.c:46:5: 
  44.   EVP_MD_CTX *EVP_MD_CTX_new(void)
  45.   {
  46. >     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  47.   }
  48.   

crypto/mem.c:218:1: start of procedure CRYPTO_zalloc()
    216.   }
    217.   
    218. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
    219.   {
    220.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:220:5: 
    218.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
    219.   {
    220. >     void *ret = CRYPTO_malloc(num, file, line);
    221.   
    222.       FAILTEST();

crypto/mem.c:189:1: start of procedure CRYPTO_malloc()
      187.   #endif
      188.   
      189. > void *CRYPTO_malloc(size_t num, const char *file, int line)
      190.   {
      191.       void *ret = NULL;

crypto/mem.c:191:5: 
      189.   void *CRYPTO_malloc(size_t num, const char *file, int line)
      190.   {
      191. >     void *ret = NULL;
      192.   
      193.       INCREMENT(malloc_count);

crypto/mem.c:194:9: Taking false branch
      192.   
      193.       INCREMENT(malloc_count);
      194.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
                     ^
      195.           return malloc_impl(num, file, line);
      196.   

crypto/mem.c:197:9: Taking false branch
      195.           return malloc_impl(num, file, line);
      196.   
      197.       if (num == 0)
                     ^
      198.           return NULL;
      199.   

crypto/mem.c:201:5: 
      199.   
      200.       FAILTEST();
      201. >     allow_customize = 0;
      202.   #ifndef OPENSSL_NO_CRYPTO_MDEBUG
      203.       if (call_malloc_debug) {

crypto/mem.c:211:5: 
      209.       }
      210.   #else
      211. >     (void)(file); (void)(line);
      212.       ret = malloc(num);
      213.   #endif

crypto/mem.c:211:19: 
      209.       }
      210.   #else
      211. >     (void)(file); (void)(line);
      212.       ret = malloc(num);
      213.   #endif

crypto/mem.c:212:5: 
      210.   #else
      211.       (void)(file); (void)(line);
      212. >     ret = malloc(num);
      213.   #endif
      214.   

crypto/mem.c:215:5: 
      213.   #endif
      214.   
      215. >     return ret;
      216.   }
      217.   

crypto/mem.c:216:1: return from a call to CRYPTO_malloc
      214.   
      215.       return ret;
      216. > }
      217.   
      218.   void *CRYPTO_zalloc(size_t num, const char *file, int line)

crypto/mem.c:223:9: Taking true branch
    221.   
    222.       FAILTEST();
    223.       if (ret != NULL)
                   ^
    224.           memset(ret, 0, num);
    225.       return ret;

crypto/mem.c:224:9: 
    222.       FAILTEST();
    223.       if (ret != NULL)
    224. >         memset(ret, 0, num);
    225.       return ret;
    226.   }

crypto/mem.c:225:5: 
    223.       if (ret != NULL)
    224.           memset(ret, 0, num);
    225. >     return ret;
    226.   }
    227.   

crypto/mem.c:226:1: return from a call to CRYPTO_zalloc
    224.           memset(ret, 0, num);
    225.       return ret;
    226. > }
    227.   
    228.   void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)

crypto/evp/digest.c:47:1: return from a call to EVP_MD_CTX_new
  45.   {
  46.       return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  47. > }
  48.   
  49.   void EVP_MD_CTX_free(EVP_MD_CTX *ctx)

crypto/cms/cms_sd.c:781:5: 
779.       ASN1_OCTET_STRING *os = NULL;
780.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
781. >     EVP_PKEY_CTX *pkctx = NULL;
782.       int r = -1;
783.       unsigned char mval[EVP_MAX_MD_SIZE];

crypto/cms/cms_sd.c:782:5: 
780.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
781.       EVP_PKEY_CTX *pkctx = NULL;
782. >     int r = -1;
783.       unsigned char mval[EVP_MAX_MD_SIZE];
784.       unsigned int mlen;

crypto/cms/cms_sd.c:786:9: Taking false branch
784.       unsigned int mlen;
785.   
786.       if (mctx == NULL) {
               ^
787.           CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
788.           goto err;

crypto/cms/cms_sd.c:791:9: 
789.       }
790.       /* If we have any signed attributes look for messageDigest value */
791. >     if (CMS_signed_get_attr_count(si) >= 0) {
792.           os = CMS_signed_get0_data_by_OBJ(si,
793.                                            OBJ_nid2obj(NID_pkcs9_messageDigest),

crypto/cms/cms_att.c:19:1: start of procedure CMS_signed_get_attr_count()
  17.   /* CMS SignedData Attribute utilities */
  18.   
  19. > int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
  20.   {
  21.       return X509at_get_attr_count(si->signedAttrs);

crypto/cms/cms_att.c:21:5: 
  19.   int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
  20.   {
  21. >     return X509at_get_attr_count(si->signedAttrs);
  22.   }
  23.   

crypto/x509/x509_att.c:20:1: start of procedure X509at_get_attr_count()
    18.   #include "x509_lcl.h"
    19.   
    20. > int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
    21.   {
    22.       return sk_X509_ATTRIBUTE_num(x);

crypto/x509/x509_att.c:22:5: 
    20.   int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
    21.   {
    22. >     return sk_X509_ATTRIBUTE_num(x);
    23.   }
    24.   

include/openssl/x509.h:89:1: start of procedure sk_X509_ATTRIBUTE_num()
      87.   typedef struct x509_attributes_st X509_ATTRIBUTE;
      88.   
      89. > DEFINE_STACK_OF(X509_ATTRIBUTE)
      90.   
      91.   typedef struct X509_req_info_st X509_REQ_INFO;

crypto/stack/stack.c:375:1: start of procedure OPENSSL_sk_num()
        373.   }
        374.   
        375. > int OPENSSL_sk_num(const OPENSSL_STACK *st)
        376.   {
        377.       return st == NULL ? -1 : st->num;

crypto/stack/stack.c:377:12: Condition is true
        375.   int OPENSSL_sk_num(const OPENSSL_STACK *st)
        376.   {
        377.       return st == NULL ? -1 : st->num;
                          ^
        378.   }
        379.   

crypto/stack/stack.c:377:5: 
        375.   int OPENSSL_sk_num(const OPENSSL_STACK *st)
        376.   {
        377. >     return st == NULL ? -1 : st->num;
        378.   }
        379.   

crypto/stack/stack.c:378:1: return from a call to OPENSSL_sk_num
        376.   {
        377.       return st == NULL ? -1 : st->num;
        378. > }
        379.   
        380.   void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)

include/openssl/x509.h:89:1: return from a call to sk_X509_ATTRIBUTE_num
      87.   typedef struct x509_attributes_st X509_ATTRIBUTE;
      88.   
      89. > DEFINE_STACK_OF(X509_ATTRIBUTE)
      90.   
      91.   typedef struct X509_req_info_st X509_REQ_INFO;

crypto/x509/x509_att.c:23:1: return from a call to X509at_get_attr_count
    21.   {
    22.       return sk_X509_ATTRIBUTE_num(x);
    23. > }
    24.   
    25.   int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,

crypto/cms/cms_att.c:22:1: return from a call to CMS_signed_get_attr_count
  20.   {
  21.       return X509at_get_attr_count(si->signedAttrs);
  22. > }
  23.   
  24.   int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)

crypto/cms/cms_sd.c:791:9: Taking false branch
789.       }
790.       /* If we have any signed attributes look for messageDigest value */
791.       if (CMS_signed_get_attr_count(si) >= 0) {
               ^
792.           os = CMS_signed_get0_data_by_OBJ(si,
793.                                            OBJ_nid2obj(NID_pkcs9_messageDigest),

crypto/cms/cms_sd.c:802:10: 
800.       }
801.   
802. >     if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
803.           goto err;
804.   

crypto/cms/cms_lib.c:311:1: start of procedure cms_DigestAlgorithm_find_ctx()
  309.   /* Locate a message digest content from a BIO chain based on SignerInfo */
  310.   
  311. > int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  312.                                    X509_ALGOR *mdalg)
  313.   {

crypto/cms/cms_lib.c:316:5: 
  314.       int nid;
  315.       const ASN1_OBJECT *mdoid;
  316. >     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  317.       nid = OBJ_obj2nid(mdoid);
  318.       /* Look for digest type to match signature */

crypto/asn1/x_algor.c:53:1: start of procedure X509_ALGOR_get0()
    51.   }
    52.   
    53. > void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,
    54.                        const void **ppval, const X509_ALGOR *algor)
    55.   {

crypto/asn1/x_algor.c:56:9: Taking true branch
    54.                        const void **ppval, const X509_ALGOR *algor)
    55.   {
    56.       if (paobj)
                  ^
    57.           *paobj = algor->algorithm;
    58.       if (pptype) {

crypto/asn1/x_algor.c:57:9: 
    55.   {
    56.       if (paobj)
    57. >         *paobj = algor->algorithm;
    58.       if (pptype) {
    59.           if (algor->parameter == NULL) {

crypto/asn1/x_algor.c:58:9: Taking false branch
    56.       if (paobj)
    57.           *paobj = algor->algorithm;
    58.       if (pptype) {
                  ^
    59.           if (algor->parameter == NULL) {
    60.               *pptype = V_ASN1_UNDEF;

crypto/asn1/x_algor.c:58:5: 
    56.       if (paobj)
    57.           *paobj = algor->algorithm;
    58. >     if (pptype) {
    59.           if (algor->parameter == NULL) {
    60.               *pptype = V_ASN1_UNDEF;

crypto/asn1/x_algor.c:67:1: return from a call to X509_ALGOR_get0
    65.               *ppval = algor->parameter->value.ptr;
    66.       }
    67. > }
    68.   
    69.   /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */

crypto/cms/cms_lib.c:317:5: 
  315.       const ASN1_OBJECT *mdoid;
  316.       X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  317. >     nid = OBJ_obj2nid(mdoid);
  318.       /* Look for digest type to match signature */
  319.       for (;;) {

crypto/objects/obj_dat.c:317:1: start of procedure OBJ_obj2nid()
    315.   IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
    316.   
    317. > int OBJ_obj2nid(const ASN1_OBJECT *a)
    318.   {
    319.       const unsigned int *op;

crypto/objects/obj_dat.c:322:9: Taking false branch
    320.       ADDED_OBJ ad, *adp;
    321.   
    322.       if (a == NULL)
                   ^
    323.           return NID_undef;
    324.       if (a->nid != 0)

crypto/objects/obj_dat.c:324:9: Taking false branch
    322.       if (a == NULL)
    323.           return NID_undef;
    324.       if (a->nid != 0)
                   ^
    325.           return a->nid;
    326.   

crypto/objects/obj_dat.c:327:9: Taking false branch
    325.           return a->nid;
    326.   
    327.       if (a->length == 0)
                   ^
    328.           return NID_undef;
    329.   

crypto/objects/obj_dat.c:330:9: Taking true branch
    328.           return NID_undef;
    329.   
    330.       if (added != NULL) {
                   ^
    331.           ad.type = ADDED_DATA;
    332.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */

crypto/objects/obj_dat.c:331:9: 
    329.   
    330.       if (added != NULL) {
    331. >         ad.type = ADDED_DATA;
    332.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    333.           adp = lh_ADDED_OBJ_retrieve(added, &ad);

crypto/objects/obj_dat.c:332:9: 
    330.       if (added != NULL) {
    331.           ad.type = ADDED_DATA;
    332. >         ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    333.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    334.           if (adp != NULL)

crypto/objects/obj_dat.c:333:9: 
    331.           ad.type = ADDED_DATA;
    332.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    333. >         adp = lh_ADDED_OBJ_retrieve(added, &ad);
    334.           if (adp != NULL)
    335.               return adp->obj->nid;

crypto/objects/obj_lcl.h:14:1: start of procedure lh_ADDED_OBJ_retrieve()
      12.   DEFINE_LHASH_OF(OBJ_NAME);
      13.   typedef struct added_obj_st ADDED_OBJ;
      14. > DEFINE_LHASH_OF(ADDED_OBJ);

crypto/lhash/lhash.c:147:1: start of procedure OPENSSL_LH_retrieve()
        145.   }
        146.   
        147. > void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
        148.   {
        149.       unsigned long hash;

crypto/lhash/lhash.c:153:5: 
        151.       void *ret;
        152.   
        153. >     lh->error = 0;
        154.       rn = getrn(lh, data, &hash);
        155.   

crypto/lhash/lhash.c:154:5: Skipping getrn(): empty list of specs
        152.   
        153.       lh->error = 0;
        154.       rn = getrn(lh, data, &hash);
                   ^
        155.   
        156.       if (*rn == NULL) {

crypto/lhash/lhash.c:156:9: Taking false branch
        154.       rn = getrn(lh, data, &hash);
        155.   
        156.       if (*rn == NULL) {
                       ^
        157.           lh->num_retrieve_miss++;
        158.           return NULL;

crypto/lhash/lhash.c:160:9: 
        158.           return NULL;
        159.       } else {
        160. >         ret = (*rn)->data;
        161.           lh->num_retrieve++;
        162.       }

crypto/lhash/lhash.c:161:9: 
        159.       } else {
        160.           ret = (*rn)->data;
        161. >         lh->num_retrieve++;
        162.       }
        163.       return ret;

crypto/lhash/lhash.c:163:5: 
        161.           lh->num_retrieve++;
        162.       }
        163. >     return ret;
        164.   }
        165.   

crypto/lhash/lhash.c:164:1: return from a call to OPENSSL_LH_retrieve
        162.       }
        163.       return ret;
        164. > }
        165.   
        166.   static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,

crypto/objects/obj_lcl.h:14:1: return from a call to lh_ADDED_OBJ_retrieve
      12.   DEFINE_LHASH_OF(OBJ_NAME);
      13.   typedef struct added_obj_st ADDED_OBJ;
      14. > DEFINE_LHASH_OF(ADDED_OBJ);

crypto/objects/obj_dat.c:334:13: Taking true branch
    332.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    333.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    334.           if (adp != NULL)
                       ^
    335.               return adp->obj->nid;
    336.       }

crypto/objects/obj_dat.c:335:13: 
    333.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    334.           if (adp != NULL)
    335. >             return adp->obj->nid;
    336.       }
    337.       op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);

crypto/objects/obj_dat.c:341:1: return from a call to OBJ_obj2nid
    339.           return NID_undef;
    340.       return nid_objs[*op].nid;
    341. > }
    342.   
    343.   /*

crypto/cms/cms_lib.c:336:5: Loop condition is true. Entering loop body
  334.               return EVP_MD_CTX_copy_ex(mctx, mtmp);
  335.           chain = BIO_next(chain);
  336.       }
             ^
  337.   }
  338.   

crypto/cms/cms_lib.c:321:9: Skipping BIO_find_type(): empty list of specs
  319.       for (;;) {
  320.           EVP_MD_CTX *mtmp;
  321.           chain = BIO_find_type(chain, BIO_TYPE_MD);
                 ^
  322.           if (chain == NULL) {
  323.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,

crypto/cms/cms_lib.c:322:13: Taking true branch
  320.           EVP_MD_CTX *mtmp;
  321.           chain = BIO_find_type(chain, BIO_TYPE_MD);
  322.           if (chain == NULL) {
                     ^
  323.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  324.                      CMS_R_NO_MATCHING_DIGEST);

crypto/cms/cms_lib.c:323:13: Skipping ERR_put_error(): empty list of specs
  321.           chain = BIO_find_type(chain, BIO_TYPE_MD);
  322.           if (chain == NULL) {
  323.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
                     ^
  324.                      CMS_R_NO_MATCHING_DIGEST);
  325.               return 0;

crypto/cms/cms_lib.c:325:13: 
  323.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  324.                      CMS_R_NO_MATCHING_DIGEST);
  325. >             return 0;
  326.           }
  327.           BIO_get_md_ctx(chain, &mtmp);

crypto/cms/cms_lib.c:337:1: return from a call to cms_DigestAlgorithm_find_ctx
  335.           chain = BIO_next(chain);
  336.       }
  337. > }
  338.   
  339.   static STACK_OF(CMS_CertificateChoices)

crypto/cms/cms_sd.c:802:10: Taking true branch
800.       }
801.   
802.       if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
                ^
803.           goto err;
804.   

crypto/cms/cms_sd.c:847:2: 
845.       }
846.   
847. >  err:
848.       EVP_PKEY_CTX_free(pkctx);
849.       EVP_MD_CTX_free(mctx);

crypto/cms/cms_sd.c:848:5: 
846.   
847.    err:
848. >     EVP_PKEY_CTX_free(pkctx);
849.       EVP_MD_CTX_free(mctx);
850.       return r;

crypto/evp/pmeth_lib.c:339:1: start of procedure EVP_PKEY_CTX_free()
  337.   }
  338.   
  339. > void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  340.   {
  341.       if (ctx == NULL)

crypto/evp/pmeth_lib.c:341:9: Taking true branch
  339.   void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  340.   {
  341.       if (ctx == NULL)
                 ^
  342.           return;
  343.       if (ctx->pmeth && ctx->pmeth->cleanup)

crypto/evp/pmeth_lib.c:342:9: 
  340.   {
  341.       if (ctx == NULL)
  342. >         return;
  343.       if (ctx->pmeth && ctx->pmeth->cleanup)
  344.           ctx->pmeth->cleanup(ctx);

crypto/evp/pmeth_lib.c:351:1: return from a call to EVP_PKEY_CTX_free
  349.   #endif
  350.       OPENSSL_free(ctx);
  351. > }
  352.   
  353.   int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,

crypto/cms/cms_sd.c:849:5: 
847.    err:
848.       EVP_PKEY_CTX_free(pkctx);
849. >     EVP_MD_CTX_free(mctx);
850.       return r;
851.   

crypto/evp/digest.c:49:1: start of procedure EVP_MD_CTX_free()
  47.   }
  48.   
  49. > void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  50.   {
  51.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:51:5: Skipping EVP_MD_CTX_reset(): empty list of specs
  49.   void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  50.   {
  51.       EVP_MD_CTX_reset(ctx);
            ^
  52.       OPENSSL_free(ctx);
  53.   }

crypto/evp/digest.c:52:5: 
  50.   {
  51.       EVP_MD_CTX_reset(ctx);
  52. >     OPENSSL_free(ctx);
  53.   }
  54.   

crypto/mem.c:286:1: start of procedure CRYPTO_free()
    284.   }
    285.   
    286. > void CRYPTO_free(void *str, const char *file, int line)
    287.   {
    288.       INCREMENT(free_count);

crypto/mem.c:289:9: Taking true branch
    287.   {
    288.       INCREMENT(free_count);
    289.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                   ^
    290.           free_impl(str, file, line);
    291.           return;

crypto/mem.c:289:30: Taking true branch
    287.   {
    288.       INCREMENT(free_count);
    289.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                                        ^
    290.           free_impl(str, file, line);
    291.           return;

crypto/mem.c:290:9: Skipping __function_pointer__(): unresolved function pointer
    288.       INCREMENT(free_count);
    289.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    290.           free_impl(str, file, line);
                   ^
    291.           return;
    292.       }

crypto/mem.c:291:9: 
    289.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    290.           free_impl(str, file, line);
    291. >         return;
    292.       }
    293.   

crypto/mem.c:305:1: return from a call to CRYPTO_free
    303.       free(str);
    304.   #endif
    305. > }
    306.   
    307.   void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)

crypto/evp/digest.c:53:1: return from a call to EVP_MD_CTX_free
  51.       EVP_MD_CTX_reset(ctx);
  52.       OPENSSL_free(ctx);
  53. > }
  54.   
  55.   int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)

int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
{
    ASN1_OCTET_STRING *os = NULL;
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
    EVP_PKEY_CTX *pkctx = NULL;
    int r = -1;
    unsigned char mval[EVP_MAX_MD_SIZE];
    unsigned int mlen;
    if (mctx == NULL) {
        CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
        goto err;
    }
    if (CMS_signed_get_attr_count(si) >= 0) {
        os = CMS_signed_get0_data_by_OBJ(si,
                                         OBJ_nid2obj(NID_pkcs9_messageDigest),
                                         -3, V_ASN1_OCTET_STRING);
        if (!os) {
            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
                   CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
            goto err;
        }
    }
    if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
        goto err;
    if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
        CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
               CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
        goto err;
    }
    if (os) {
        if (mlen != (unsigned int)os->length) {
            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
                   CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
            goto err;
        }
        if (memcmp(mval, os->data, mlen)) {
            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
                   CMS_R_VERIFICATION_FAILURE);
            r = 0;
        } else
            r = 1;
    } else {
        const EVP_MD *md = EVP_MD_CTX_md(mctx);
        pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
        if (pkctx == NULL)
            goto err;
        if (EVP_PKEY_verify_init(pkctx) <= 0)
            goto err;
        if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
            goto err;
        si->pctx = pkctx;
        if (!cms_sd_asn1_ctrl(si, 1))
            goto err;
        r = EVP_PKEY_verify(pkctx, si->signature->data,
                            si->signature->length, mval, mlen);
        if (r <= 0) {
            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
                   CMS_R_VERIFICATION_FAILURE);
            r = 0;
        }
    }
 err:
    EVP_PKEY_CTX_free(pkctx);
    EVP_MD_CTX_free(mctx);
    return r;
}
['int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)\n{\n    ASN1_OCTET_STRING *os = NULL;\n    EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n    EVP_PKEY_CTX *pkctx = NULL;\n    int r = -1;\n    unsigned char mval[EVP_MAX_MD_SIZE];\n    unsigned int mlen;\n    if (mctx == NULL) {\n        CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (CMS_signed_get_attr_count(si) >= 0) {\n        os = CMS_signed_get0_data_by_OBJ(si,\n                                         OBJ_nid2obj(NID_pkcs9_messageDigest),\n                                         -3, V_ASN1_OCTET_STRING);\n        if (!os) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n                   CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);\n            goto err;\n        }\n    }\n    if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))\n        goto err;\n    if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {\n        CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n               CMS_R_UNABLE_TO_FINALIZE_CONTEXT);\n        goto err;\n    }\n    if (os) {\n        if (mlen != (unsigned int)os->length) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n                   CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);\n            goto err;\n        }\n        if (memcmp(mval, os->data, mlen)) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n                   CMS_R_VERIFICATION_FAILURE);\n            r = 0;\n        } else\n            r = 1;\n    } else {\n        const EVP_MD *md = EVP_MD_CTX_md(mctx);\n        pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);\n        if (pkctx == NULL)\n            goto err;\n        if (EVP_PKEY_verify_init(pkctx) <= 0)\n            goto err;\n        if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)\n            goto err;\n        si->pctx = pkctx;\n        if (!cms_sd_asn1_ctrl(si, 1))\n            goto err;\n        r = EVP_PKEY_verify(pkctx, si->signature->data,\n                            si->signature->length, mval, mlen);\n        if (r <= 0) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,\n                   CMS_R_VERIFICATION_FAILURE);\n            r = 0;\n        }\n    }\n err:\n    EVP_PKEY_CTX_free(pkctx);\n    EVP_MD_CTX_free(mctx);\n    return r;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\n    return ret;\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    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    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}', 'int CMS_signed_get_attr_count(const CMS_SignerInfo *si)\n{\n    return X509at_get_attr_count(si->signedAttrs);\n}', 'int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)\n{\n    return sk_X509_ATTRIBUTE_num(x);\n}', 'int OPENSSL_sk_num(const OPENSSL_STACK *st)\n{\n    return st == NULL ? -1 : st->num;\n}', 'int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,\n                                 X509_ALGOR *mdalg)\n{\n    int nid;\n    const ASN1_OBJECT *mdoid;\n    X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);\n    nid = OBJ_obj2nid(mdoid);\n    for (;;) {\n        EVP_MD_CTX *mtmp;\n        chain = BIO_find_type(chain, BIO_TYPE_MD);\n        if (chain == NULL) {\n            CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,\n                   CMS_R_NO_MATCHING_DIGEST);\n            return 0;\n        }\n        BIO_get_md_ctx(chain, &mtmp);\n        if (EVP_MD_CTX_type(mtmp) == nid\n            || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)\n            return EVP_MD_CTX_copy_ex(mctx, mtmp);\n        chain = BIO_next(chain);\n    }\n}', 'void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,\n                     const void **ppval, const X509_ALGOR *algor)\n{\n    if (paobj)\n        *paobj = algor->algorithm;\n    if (pptype) {\n        if (algor->parameter == NULL) {\n            *pptype = V_ASN1_UNDEF;\n            return;\n        } else\n            *pptype = algor->parameter->type;\n        if (ppval)\n            *ppval = algor->parameter->value.ptr;\n    }\n}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n{\n    const unsigned int *op;\n    ADDED_OBJ ad, *adp;\n    if (a == NULL)\n        return NID_undef;\n    if (a->nid != 0)\n        return a->nid;\n    if (a->length == 0)\n        return NID_undef;\n    if (added != NULL) {\n        ad.type = ADDED_DATA;\n        ad.obj = (ASN1_OBJECT *)a;\n        adp = lh_ADDED_OBJ_retrieve(added, &ad);\n        if (adp != NULL)\n            return adp->obj->nid;\n    }\n    op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);\n    if (op == NULL)\n        return NID_undef;\n    return nid_objs[*op].nid;\n}', 'DEFINE_LHASH_OF(ADDED_OBJ)', 'void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)\n{\n    unsigned long hash;\n    OPENSSL_LH_NODE **rn;\n    void *ret;\n    lh->error = 0;\n    rn = getrn(lh, data, &hash);\n    if (*rn == NULL) {\n        lh->num_retrieve_miss++;\n        return NULL;\n    } else {\n        ret = (*rn)->data;\n        lh->num_retrieve++;\n    }\n    return ret;\n}', 'void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    if (ctx->pmeth && ctx->pmeth->cleanup)\n        ctx->pmeth->cleanup(ctx);\n    EVP_PKEY_free(ctx->pkey);\n    EVP_PKEY_free(ctx->peerkey);\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(ctx->engine);\n#endif\n    OPENSSL_free(ctx);\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n    EVP_MD_CTX_reset(ctx);\n    OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}']