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


crypto/cms/cms_sd.c:577:1: start of procedure cms_SignerInfo_content_sign()
575.   }
576.   
577. > static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
578.                                          CMS_SignerInfo *si, BIO *chain)
579.   {

crypto/cms/cms_sd.c:580:5: 
578.                                          CMS_SignerInfo *si, BIO *chain)
579.   {
580. >     EVP_MD_CTX *mctx = EVP_MD_CTX_new();
581.       int r = 0;
582.       EVP_PKEY_CTX *pctx = NULL;

crypto/evp/digest.c:152:1: start of procedure EVP_MD_CTX_new()
  150.   }
  151.   
  152. > EVP_MD_CTX *EVP_MD_CTX_new(void)
  153.   {
  154.       return OPENSSL_zalloc(sizeof(EVP_MD_CTX));

crypto/evp/digest.c:154:5: 
  152.   EVP_MD_CTX *EVP_MD_CTX_new(void)
  153.   {
  154. >     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  155.   }
  156.   

crypto/mem.c:159:1: start of procedure CRYPTO_zalloc()
    157.   }
    158.   
    159. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
    160.   {
    161.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:161:5: 
    159.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
    160.   {
    161. >     void *ret = CRYPTO_malloc(num, file, line);
    162.   
    163.       if (ret != NULL)

crypto/mem.c:119:1: start of procedure CRYPTO_malloc()
      117.   }
      118.   
      119. > void *CRYPTO_malloc(size_t num, const char *file, int line)
      120.   {
      121.       void *ret = NULL;

crypto/mem.c:121:5: 
      119.   void *CRYPTO_malloc(size_t num, const char *file, int line)
      120.   {
      121. >     void *ret = NULL;
      122.   
      123.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)

crypto/mem.c:123:9: Taking false branch
      121.       void *ret = NULL;
      122.   
      123.       if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
                     ^
      124.           return malloc_impl(num, file, line);
      125.   

crypto/mem.c:126:9: Taking false branch
      124.           return malloc_impl(num, file, line);
      125.   
      126.       if (num <= 0)
                     ^
      127.           return NULL;
      128.   

crypto/mem.c:129:5: 
      127.           return NULL;
      128.   
      129. >     allow_customize = 0;
      130.   #ifndef OPENSSL_NO_CRYPTO_MDEBUG
      131.       if (call_malloc_debug) {

crypto/mem.c:139:5: 
      137.       }
      138.   #else
      139. >     (void)file;
      140.       (void)line;
      141.       ret = malloc(num);

crypto/mem.c:140:5: 
      138.   #else
      139.       (void)file;
      140. >     (void)line;
      141.       ret = malloc(num);
      142.   #endif

crypto/mem.c:141:5: 
      139.       (void)file;
      140.       (void)line;
      141. >     ret = malloc(num);
      142.   #endif
      143.   

crypto/mem.c:156:5: 
      154.   #endif
      155.   
      156. >     return ret;
      157.   }
      158.   

crypto/mem.c:157:1: return from a call to CRYPTO_malloc
      155.   
      156.       return ret;
      157. > }
      158.   
      159.   void *CRYPTO_zalloc(size_t num, const char *file, int line)

crypto/mem.c:163:9: Taking true branch
    161.       void *ret = CRYPTO_malloc(num, file, line);
    162.   
    163.       if (ret != NULL)
                   ^
    164.           memset(ret, 0, num);
    165.       return ret;

crypto/mem.c:164:9: 
    162.   
    163.       if (ret != NULL)
    164. >         memset(ret, 0, num);
    165.       return ret;
    166.   }

crypto/mem.c:165:5: 
    163.       if (ret != NULL)
    164.           memset(ret, 0, num);
    165. >     return ret;
    166.   }
    167.   

crypto/mem.c:166:1: return from a call to CRYPTO_zalloc
    164.           memset(ret, 0, num);
    165.       return ret;
    166. > }
    167.   
    168.   void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)

crypto/evp/digest.c:155:1: return from a call to EVP_MD_CTX_new
  153.   {
  154.       return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
  155. > }
  156.   
  157.   void EVP_MD_CTX_free(EVP_MD_CTX *ctx)

crypto/cms/cms_sd.c:581:5: 
579.   {
580.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
581. >     int r = 0;
582.       EVP_PKEY_CTX *pctx = NULL;
583.   

crypto/cms/cms_sd.c:582:5: 
580.       EVP_MD_CTX *mctx = EVP_MD_CTX_new();
581.       int r = 0;
582. >     EVP_PKEY_CTX *pctx = NULL;
583.   
584.       if (mctx == NULL) {

crypto/cms/cms_sd.c:584:9: Taking false branch
582.       EVP_PKEY_CTX *pctx = NULL;
583.   
584.       if (mctx == NULL) {
               ^
585.           CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
586.           return 0;

crypto/cms/cms_sd.c:589:10: Taking false branch
587.       }
588.   
589.       if (!si->pkey) {
                ^
590.           CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
591.           return 0;

crypto/cms/cms_sd.c:594:10: 
592.       }
593.   
594. >     if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
595.           goto err;
596.       /* Set SignerInfo algorithm details if we used custom parameter */

crypto/cms/cms_lib.c:355:1: start of procedure cms_DigestAlgorithm_find_ctx()
  353.   /* Locate a message digest content from a BIO chain based on SignerInfo */
  354.   
  355. > int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  356.                                    X509_ALGOR *mdalg)
  357.   {

crypto/cms/cms_lib.c:360:5: 
  358.       int nid;
  359.       ASN1_OBJECT *mdoid;
  360. >     X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  361.       nid = OBJ_obj2nid(mdoid);
  362.       /* Look for digest type to match signature */

crypto/asn1/x_algor.c:102:1: start of procedure X509_ALGOR_get0()
    100.   }
    101.   
    102. > void X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, void **ppval,
    103.                        X509_ALGOR *algor)
    104.   {

crypto/asn1/x_algor.c:105:9: Taking true branch
    103.                        X509_ALGOR *algor)
    104.   {
    105.       if (paobj)
                   ^
    106.           *paobj = algor->algorithm;
    107.       if (pptype) {

crypto/asn1/x_algor.c:106:9: 
    104.   {
    105.       if (paobj)
    106. >         *paobj = algor->algorithm;
    107.       if (pptype) {
    108.           if (algor->parameter == NULL) {

crypto/asn1/x_algor.c:107:9: Taking false branch
    105.       if (paobj)
    106.           *paobj = algor->algorithm;
    107.       if (pptype) {
                   ^
    108.           if (algor->parameter == NULL) {
    109.               *pptype = V_ASN1_UNDEF;

crypto/asn1/x_algor.c:107:5: 
    105.       if (paobj)
    106.           *paobj = algor->algorithm;
    107. >     if (pptype) {
    108.           if (algor->parameter == NULL) {
    109.               *pptype = V_ASN1_UNDEF;

crypto/asn1/x_algor.c:116:1: return from a call to X509_ALGOR_get0
    114.               *ppval = algor->parameter->value.ptr;
    115.       }
    116. > }
    117.   
    118.   /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */

crypto/cms/cms_lib.c:361:5: 
  359.       ASN1_OBJECT *mdoid;
  360.       X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
  361. >     nid = OBJ_obj2nid(mdoid);
  362.       /* Look for digest type to match signature */
  363.       for (;;) {

crypto/objects/obj_dat.c:381:1: start of procedure OBJ_obj2nid()
    379.   IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
    380.   
    381. > int OBJ_obj2nid(const ASN1_OBJECT *a)
    382.   {
    383.       const unsigned int *op;

crypto/objects/obj_dat.c:386:9: Taking false branch
    384.       ADDED_OBJ ad, *adp;
    385.   
    386.       if (a == NULL)
                   ^
    387.           return (NID_undef);
    388.       if (a->nid != 0)

crypto/objects/obj_dat.c:388:9: Taking false branch
    386.       if (a == NULL)
    387.           return (NID_undef);
    388.       if (a->nid != 0)
                   ^
    389.           return (a->nid);
    390.   

crypto/objects/obj_dat.c:391:9: Taking false branch
    389.           return (a->nid);
    390.   
    391.       if (a->length == 0)
                   ^
    392.           return NID_undef;
    393.   

crypto/objects/obj_dat.c:394:9: Taking true branch
    392.           return NID_undef;
    393.   
    394.       if (added != NULL) {
                   ^
    395.           ad.type = ADDED_DATA;
    396.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */

crypto/objects/obj_dat.c:395:9: 
    393.   
    394.       if (added != NULL) {
    395. >         ad.type = ADDED_DATA;
    396.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    397.           adp = lh_ADDED_OBJ_retrieve(added, &ad);

crypto/objects/obj_dat.c:396:9: 
    394.       if (added != NULL) {
    395.           ad.type = ADDED_DATA;
    396. >         ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    397.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    398.           if (adp != NULL)

crypto/objects/obj_dat.c:397:9: 
    395.           ad.type = ADDED_DATA;
    396.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    397. >         adp = lh_ADDED_OBJ_retrieve(added, &ad);
    398.           if (adp != NULL)
    399.               return (adp->obj->nid);

crypto/objects/obj_lcl.h:7:1: start of procedure lh_ADDED_OBJ_retrieve()
      5.   DEFINE_LHASH_OF(OBJ_NAME);
      6.   typedef struct added_obj_st ADDED_OBJ;
      7. > DEFINE_LHASH_OF(ADDED_OBJ);

crypto/lhash/lhash.c:216:1: start of procedure lh_retrieve()
        214.   }
        215.   
        216. > void *lh_retrieve(_LHASH *lh, const void *data)
        217.   {
        218.       unsigned long hash;

crypto/lhash/lhash.c:222:5: 
        220.       void *ret;
        221.   
        222. >     lh->error = 0;
        223.       rn = getrn(lh, data, &hash);
        224.   

crypto/lhash/lhash.c:223:5: Skipping getrn(): empty list of specs
        221.   
        222.       lh->error = 0;
        223.       rn = getrn(lh, data, &hash);
                   ^
        224.   
        225.       if (*rn == NULL) {

crypto/lhash/lhash.c:225:9: Taking false branch
        223.       rn = getrn(lh, data, &hash);
        224.   
        225.       if (*rn == NULL) {
                       ^
        226.           lh->num_retrieve_miss++;
        227.           return (NULL);

crypto/lhash/lhash.c:229:9: 
        227.           return (NULL);
        228.       } else {
        229. >         ret = (*rn)->data;
        230.           lh->num_retrieve++;
        231.       }

crypto/lhash/lhash.c:230:9: 
        228.       } else {
        229.           ret = (*rn)->data;
        230. >         lh->num_retrieve++;
        231.       }
        232.       return (ret);

crypto/lhash/lhash.c:232:5: 
        230.           lh->num_retrieve++;
        231.       }
        232. >     return (ret);
        233.   }
        234.   

crypto/lhash/lhash.c:233:1: return from a call to lh_retrieve
        231.       }
        232.       return (ret);
        233. > }
        234.   
        235.   static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,

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

crypto/objects/obj_dat.c:398:13: Taking true branch
    396.           ad.obj = (ASN1_OBJECT *)a; /* XXX: ugly but harmless */
    397.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    398.           if (adp != NULL)
                       ^
    399.               return (adp->obj->nid);
    400.       }

crypto/objects/obj_dat.c:399:13: 
    397.           adp = lh_ADDED_OBJ_retrieve(added, &ad);
    398.           if (adp != NULL)
    399. >             return (adp->obj->nid);
    400.       }
    401.       op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);

crypto/objects/obj_dat.c:405:1: return from a call to OBJ_obj2nid
    403.           return (NID_undef);
    404.       return (nid_objs[*op].nid);
    405. > }
    406.   
    407.   /*

crypto/cms/cms_lib.c:380:5: Loop condition is true. Entering loop body
  378.               return EVP_MD_CTX_copy_ex(mctx, mtmp);
  379.           chain = BIO_next(chain);
  380.       }
             ^
  381.   }
  382.   

crypto/cms/cms_lib.c:365:9: Skipping BIO_find_type(): empty list of specs
  363.       for (;;) {
  364.           EVP_MD_CTX *mtmp;
  365.           chain = BIO_find_type(chain, BIO_TYPE_MD);
                 ^
  366.           if (chain == NULL) {
  367.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,

crypto/cms/cms_lib.c:366:13: Taking true branch
  364.           EVP_MD_CTX *mtmp;
  365.           chain = BIO_find_type(chain, BIO_TYPE_MD);
  366.           if (chain == NULL) {
                     ^
  367.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  368.                      CMS_R_NO_MATCHING_DIGEST);

crypto/cms/cms_lib.c:367:13: Skipping ERR_put_error(): empty list of specs
  365.           chain = BIO_find_type(chain, BIO_TYPE_MD);
  366.           if (chain == NULL) {
  367.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
                     ^
  368.                      CMS_R_NO_MATCHING_DIGEST);
  369.               return 0;

crypto/cms/cms_lib.c:369:13: 
  367.               CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
  368.                      CMS_R_NO_MATCHING_DIGEST);
  369. >             return 0;
  370.           }
  371.           BIO_get_md_ctx(chain, &mtmp);

crypto/cms/cms_lib.c:381:1: return from a call to cms_DigestAlgorithm_find_ctx
  379.           chain = BIO_next(chain);
  380.       }
  381. > }
  382.   
  383.   static STACK_OF(CMS_CertificateChoices)

crypto/cms/cms_sd.c:594:10: Taking true branch
592.       }
593.   
594.       if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
                ^
595.           goto err;
596.       /* Set SignerInfo algorithm details if we used custom parameter */

crypto/cms/cms_sd.c:657:2: 
655.       r = 1;
656.   
657. >  err:
658.       EVP_MD_CTX_free(mctx);
659.       EVP_PKEY_CTX_free(pctx);

crypto/cms/cms_sd.c:658:5: 
656.   
657.    err:
658. >     EVP_MD_CTX_free(mctx);
659.       EVP_PKEY_CTX_free(pctx);
660.       return r;

crypto/evp/digest.c:157:1: start of procedure EVP_MD_CTX_free()
  155.   }
  156.   
  157. > void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  158.   {
  159.       EVP_MD_CTX_reset(ctx);

crypto/evp/digest.c:159:5: Skipping EVP_MD_CTX_reset(): empty list of specs
  157.   void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  158.   {
  159.       EVP_MD_CTX_reset(ctx);
             ^
  160.       OPENSSL_free(ctx);
  161.   }

crypto/evp/digest.c:160:5: 
  158.   {
  159.       EVP_MD_CTX_reset(ctx);
  160. >     OPENSSL_free(ctx);
  161.   }
  162.   

crypto/mem.c:225:1: start of procedure CRYPTO_free()
    223.   }
    224.   
    225. > void CRYPTO_free(void *str, const char *file, int line)
    226.   {
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:227:9: Taking true branch
    225.   void CRYPTO_free(void *str, const char *file, int line)
    226.   {
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                   ^
    228.           free_impl(str, file, line);
    229.           return;

crypto/mem.c:227:30: Taking true branch
    225.   void CRYPTO_free(void *str, const char *file, int line)
    226.   {
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                                        ^
    228.           free_impl(str, file, line);
    229.           return;

crypto/mem.c:228:9: Skipping __function_pointer__(): unresolved function pointer
    226.   {
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    228.           free_impl(str, file, line);
                   ^
    229.           return;
    230.       }

crypto/mem.c:229:9: 
    227.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    228.           free_impl(str, file, line);
    229. >         return;
    230.       }
    231.   

crypto/mem.c:243:1: return from a call to CRYPTO_free
    241.       free(str);
    242.   #endif
    243. > }
    244.   
    245.   void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)

crypto/evp/digest.c:161:1: return from a call to EVP_MD_CTX_free
  159.       EVP_MD_CTX_reset(ctx);
  160.       OPENSSL_free(ctx);
  161. > }
  162.   
  163.   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);
        return 0;
    }
    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        return 0;\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    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    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;\n    (void)line;\n    ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n    if (ret && (num > 2048)) {\n        extern unsigned char cleanse_ctr;\n        ((unsigned char *)ret)[0] = cleanse_ctr;\n    }\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    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(ASN1_OBJECT **paobj, int *pptype, void **ppval,\n                     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 *lh_retrieve(_LHASH *lh, const void *data)\n{\n    unsigned long hash;\n    LHASH_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_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}']