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


crypto/cms/cms_sd.c:533:1: start of procedure cms_SignerInfo_content_sign()
531.   }
532.   
533. > static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
534.                                          CMS_SignerInfo *si, BIO *chain)
535.   {

crypto/cms/cms_sd.c:536:5: 
534.                                          CMS_SignerInfo *si, BIO *chain)
535.   {
536. >     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
537.       int r = 0;
538.       EVP_PKEY_CTX *pctx = NULL;

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:194:1: start of procedure CRYPTO_zalloc()
    192.   }
    193.   
    194. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
    195.   {
    196.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:196:5: 
    194.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
    195.   {
    196. >     void *ret = CRYPTO_malloc(num, file, line);
    197.   
    198.       FAILTEST();

crypto/mem.c:166:1: start of procedure CRYPTO_malloc()
      164.   #endif
      165.   
      166. > void *CRYPTO_malloc(size_t num, const char *file, int line)
      167.   {
      168.       void *ret = NULL;

crypto/mem.c:168:5: 
      166.   void *CRYPTO_malloc(size_t num, const char *file, int line)
      167.   {
      168. >     void *ret = NULL;
      169.   
      170.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)

crypto/mem.c:170:9: Taking false branch
      168.       void *ret = NULL;
      169.   
      170.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
                     ^
      171.           return malloc_impl(num, file, line);
      172.   

crypto/mem.c:173:9: Taking false branch
      171.           return malloc_impl(num, file, line);
      172.   
      173.       if (num == 0)
                     ^
      174.           return NULL;
      175.   

crypto/mem.c:177:5: 
      175.   
      176.       FAILTEST();
      177. >     allow_customize = 0;
      178.   #ifndef OPENSSL_NO_CRYPTO_MDEBUG
      179.       if (call_malloc_debug) {

crypto/mem.c:187:5: 
      185.       }
      186.   #else
      187. >     osslargused(file); osslargused(line);
      188.       ret = malloc(num);
      189.   #endif

crypto/mem.c:187:24: 
      185.       }
      186.   #else
      187. >     osslargused(file); osslargused(line);
      188.       ret = malloc(num);
      189.   #endif

crypto/mem.c:188:5: 
      186.   #else
      187.       osslargused(file); osslargused(line);
      188. >     ret = malloc(num);
      189.   #endif
      190.   

crypto/mem.c:191:5: 
      189.   #endif
      190.   
      191. >     return ret;
      192.   }
      193.   

crypto/mem.c:192:1: return from a call to CRYPTO_malloc
      190.   
      191.       return ret;
      192. > }
      193.   
      194.   void *CRYPTO_zalloc(size_t num, const char *file, int line)

crypto/mem.c:199:9: Taking true branch
    197.   
    198.       FAILTEST();
    199.       if (ret != NULL)
                   ^
    200.           memset(ret, 0, num);
    201.       return ret;

crypto/mem.c:200:9: 
    198.       FAILTEST();
    199.       if (ret != NULL)
    200. >         memset(ret, 0, num);
    201.       return ret;
    202.   }

crypto/mem.c:201:5: 
    199.       if (ret != NULL)
    200.           memset(ret, 0, num);
    201. >     return ret;
    202.   }
    203.   

crypto/mem.c:202:1: return from a call to CRYPTO_zalloc
    200.           memset(ret, 0, num);
    201.       return ret;
    202. > }
    203.   
    204.   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:537:5: 
535.   {
536.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
537. >     int r = 0;
538.       EVP_PKEY_CTX *pctx = NULL;
539.   

crypto/cms/cms_sd.c:538:5: 
536.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
537.       int r = 0;
538. >     EVP_PKEY_CTX *pctx = NULL;
539.   
540.       if (mctx == NULL) {

crypto/cms/cms_sd.c:540:9: Taking false branch
538.       EVP_PKEY_CTX *pctx = NULL;
539.   
540.       if (mctx == NULL) {
               ^
541.           CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
542.           return 0;

crypto/cms/cms_sd.c:545:10: Taking false branch
543.       }
544.   
545.       if (!si->pkey) {
                ^
546.           CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
547.           goto err;

crypto/cms/cms_sd.c:550:10: 
548.       }
549.   
550. >     if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
551.           goto err;
552.       /* Set SignerInfo algorithm details if we used custom parameter */

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:134:1: start of procedure OPENSSL_LH_retrieve()
        132.   }
        133.   
        134. > void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
        135.   {
        136.       unsigned long hash;

crypto/lhash/lhash.c:141:5: 
        139.       int scratch;
        140.   
        141. >     lh->error = 0;
        142.       rn = getrn(lh, data, &hash);
        143.   

crypto/lhash/lhash.c:142:5: Skipping getrn(): empty list of specs
        140.   
        141.       lh->error = 0;
        142.       rn = getrn(lh, data, &hash);
                   ^
        143.   
        144.       if (*rn == NULL) {

crypto/lhash/lhash.c:144:9: Taking false branch
        142.       rn = getrn(lh, data, &hash);
        143.   
        144.       if (*rn == NULL) {
                       ^
        145.           CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);
        146.           return NULL;

crypto/lhash/lhash.c:148:9: 
        146.           return NULL;
        147.       } else {
        148. >         ret = (*rn)->data;
        149.           CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
        150.       }

crypto/lhash/lhash.c:149:9: 
        147.       } else {
        148.           ret = (*rn)->data;
        149. >         CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
        150.       }
        151.       return ret;

crypto/threads_pthread.c:152:1: start of procedure CRYPTO_atomic_add()
          150.   }
          151.   
          152. > int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
          153.   {
          154.   # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)

crypto/threads_pthread.c:155:9: Taking false branch
          153.   {
          154.   # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
          155.       if (__atomic_is_lock_free(sizeof(*val), val)) {
                         ^
          156.           *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
          157.           return 1;

crypto/threads_pthread.c:160:10: 
          158.       }
          159.   # endif
          160. >     if (!CRYPTO_THREAD_write_lock(lock))
          161.           return 0;
          162.   

crypto/threads_pthread.c:64:1: start of procedure CRYPTO_THREAD_write_lock()
            62.   }
            63.   
            64. > int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
            65.   {
            66.   # ifdef USE_RWLOCK

crypto/threads_pthread.c:67:9: Taking true branch
            65.   {
            66.   # ifdef USE_RWLOCK
            67.       if (pthread_rwlock_wrlock(lock) != 0)
                          ^
            68.           return 0;
            69.   # else

crypto/threads_pthread.c:68:9: 
            66.   # ifdef USE_RWLOCK
            67.       if (pthread_rwlock_wrlock(lock) != 0)
            68. >         return 0;
            69.   # else
            70.       if (pthread_mutex_lock(lock) != 0)

crypto/threads_pthread.c:75:1: return from a call to CRYPTO_THREAD_write_lock
            73.   
            74.       return 1;
            75. > }
            76.   
            77.   int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)

crypto/threads_pthread.c:160:10: Taking true branch
          158.       }
          159.   # endif
          160.       if (!CRYPTO_THREAD_write_lock(lock))
                          ^
          161.           return 0;
          162.   

crypto/threads_pthread.c:161:9: 
          159.   # endif
          160.       if (!CRYPTO_THREAD_write_lock(lock))
          161. >         return 0;
          162.   
          163.       *val += amount;

crypto/threads_pthread.c:170:1: return from a call to CRYPTO_atomic_add
          168.   
          169.       return 1;
          170. > }
          171.   
          172.   int openssl_init_fork_handlers(void)

crypto/lhash/lhash.c:151:5: 
        149.           CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
        150.       }
        151. >     return ret;
        152.   }
        153.   

crypto/lhash/lhash.c:152:1: return from a call to OPENSSL_LH_retrieve
        150.       }
        151.       return ret;
        152. > }
        153.   
        154.   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:550:10: Taking true branch
548.       }
549.   
550.       if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
                ^
551.           goto err;
552.       /* Set SignerInfo algorithm details if we used custom parameter */

crypto/cms/cms_sd.c:613:2: 
611.       r = 1;
612.   
613. >  err:
614.       EVP_MD_CTX_free(mctx);
615.       EVP_PKEY_CTX_free(pctx);

crypto/cms/cms_sd.c:614:5: 
612.   
613.    err:
614. >     EVP_MD_CTX_free(mctx);
615.       EVP_PKEY_CTX_free(pctx);
616.       return r;

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:261:1: start of procedure CRYPTO_free()
    259.   }
    260.   
    261. > void CRYPTO_free(void *str, const char *file, int line)
    262.   {
    263.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:263:9: Taking true branch
    261.   void CRYPTO_free(void *str, const char *file, int line)
    262.   {
    263.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                   ^
    264.           free_impl(str, file, line);
    265.           return;

crypto/mem.c:263:30: Taking true branch
    261.   void CRYPTO_free(void *str, const char *file, int line)
    262.   {
    263.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                                        ^
    264.           free_impl(str, file, line);
    265.           return;

crypto/mem.c:264:9: Skipping __function_pointer__(): unresolved function pointer
    262.   {
    263.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    264.           free_impl(str, file, line);
                   ^
    265.           return;
    266.       }

crypto/mem.c:265:9: 
    263.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    264.           free_impl(str, file, line);
    265. >         return;
    266.       }
    267.   

crypto/mem.c:279:1: return from a call to CRYPTO_free
    277.       free(str);
    278.   #endif
    279. > }
    280.   
    281.   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)

static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
                                       CMS_SignerInfo *si, BIO *chain)
{
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
    int r = 0;
    EVP_PKEY_CTX *pctx = NULL;
    if (mctx == NULL) {
        CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
        return 0;
    }
    if (!si->pkey) {
        CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
        goto err;
    }
    if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
        goto err;
    if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
        goto err;
    if (CMS_signed_get_attr_count(si) >= 0) {
        ASN1_OBJECT *ctype =
            cms->d.signedData->encapContentInfo->eContentType;
        unsigned char md[EVP_MAX_MD_SIZE];
        unsigned int mdlen;
        if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
            goto err;
        if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
                                         V_ASN1_OCTET_STRING, md, mdlen))
            goto err;
        if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
                                        V_ASN1_OBJECT, ctype, -1) <= 0)
            goto err;
        if (!CMS_SignerInfo_sign(si))
            goto err;
    } else if (si->pctx) {
        unsigned char *sig;
        size_t siglen;
        unsigned char md[EVP_MAX_MD_SIZE];
        unsigned int mdlen;
        pctx = si->pctx;
        if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
            goto err;
        siglen = EVP_PKEY_size(si->pkey);
        sig = OPENSSL_malloc(siglen);
        if (sig == NULL) {
            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
            OPENSSL_free(sig);
            goto err;
        }
        ASN1_STRING_set0(si->signature, sig, siglen);
    } else {
        unsigned char *sig;
        unsigned int siglen;
        sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
        if (sig == NULL) {
            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
            OPENSSL_free(sig);
            goto err;
        }
        ASN1_STRING_set0(si->signature, sig, siglen);
    }
    r = 1;
 err:
    EVP_MD_CTX_free(mctx);
    EVP_PKEY_CTX_free(pctx);
    return r;
}
['static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,\n                                       CMS_SignerInfo *si, BIO *chain)\n{\n    EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n    int r = 0;\n    EVP_PKEY_CTX *pctx = NULL;\n    if (mctx == NULL) {\n        CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    if (!si->pkey) {\n        CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);\n        goto err;\n    }\n    if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))\n        goto err;\n    if (si->pctx && !cms_sd_asn1_ctrl(si, 0))\n        goto err;\n    if (CMS_signed_get_attr_count(si) >= 0) {\n        ASN1_OBJECT *ctype =\n            cms->d.signedData->encapContentInfo->eContentType;\n        unsigned char md[EVP_MAX_MD_SIZE];\n        unsigned int mdlen;\n        if (!EVP_DigestFinal_ex(mctx, md, &mdlen))\n            goto err;\n        if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,\n                                         V_ASN1_OCTET_STRING, md, mdlen))\n            goto err;\n        if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,\n                                        V_ASN1_OBJECT, ctype, -1) <= 0)\n            goto err;\n        if (!CMS_SignerInfo_sign(si))\n            goto err;\n    } else if (si->pctx) {\n        unsigned char *sig;\n        size_t siglen;\n        unsigned char md[EVP_MAX_MD_SIZE];\n        unsigned int mdlen;\n        pctx = si->pctx;\n        if (!EVP_DigestFinal_ex(mctx, md, &mdlen))\n            goto err;\n        siglen = EVP_PKEY_size(si->pkey);\n        sig = OPENSSL_malloc(siglen);\n        if (sig == NULL) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {\n            OPENSSL_free(sig);\n            goto err;\n        }\n        ASN1_STRING_set0(si->signature, sig, siglen);\n    } else {\n        unsigned char *sig;\n        unsigned int siglen;\n        sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));\n        if (sig == NULL) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {\n            CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);\n            OPENSSL_free(sig);\n            goto err;\n        }\n        ASN1_STRING_set0(si->signature, sig, siglen);\n    }\n    r = 1;\n err:\n    EVP_MD_CTX_free(mctx);\n    EVP_PKEY_CTX_free(pctx);\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    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}', '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    int scratch;\n    lh->error = 0;\n    rn = getrn(lh, data, &hash);\n    if (*rn == NULL) {\n        CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);\n        return NULL;\n    } else {\n        ret = (*rn)->data;\n        CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);\n    }\n    return ret;\n}', 'int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)\n{\n# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)\n    if (__atomic_is_lock_free(sizeof(*val), val)) {\n        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);\n        return 1;\n    }\n# endif\n    if (!CRYPTO_THREAD_write_lock(lock))\n        return 0;\n    *val += amount;\n    *ret  = *val;\n    if (!CRYPTO_THREAD_unlock(lock))\n        return 0;\n    return 1;\n}', 'int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)\n{\n# ifdef USE_RWLOCK\n    if (pthread_rwlock_wrlock(lock) != 0)\n        return 0;\n# else\n    if (pthread_mutex_lock(lock) != 0)\n        return 0;\n# endif\n    return 1;\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    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}']