crypto/pkcs12/p12_key.c:133: error: MEMORY_LEAK
  memory dynamically allocated to `return` by call to `EVP_MD_CTX_new()` at line 118, column 11 is not reachable after line 133, column 9.
Showing all 36 steps of the trace


crypto/pkcs12/p12_key.c:103:1: start of procedure PKCS12_key_gen_uni()
101.   }
102.   
103. > int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
104.                          int saltlen, int id, int iter, int n,
105.                          unsigned char *out, const EVP_MD *md_type)

crypto/pkcs12/p12_key.c:107:5: 
105.                          unsigned char *out, const EVP_MD *md_type)
106.   {
107. >     unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
108.       int Slen, Plen, Ilen, Ijlen;
109.       int i, j, u, v;

crypto/pkcs12/p12_key.c:110:5: 
108.       int Slen, Plen, Ilen, Ijlen;
109.       int i, j, u, v;
110. >     int ret = 0;
111.       BIGNUM *Ij = NULL, *Bpl1 = NULL; /* These hold Ij and B + 1 */
112.       EVP_MD_CTX *ctx = NULL;

crypto/pkcs12/p12_key.c:111:5: 
109.       int i, j, u, v;
110.       int ret = 0;
111. >     BIGNUM *Ij = NULL, *Bpl1 = NULL; /* These hold Ij and B + 1 */
112.       EVP_MD_CTX *ctx = NULL;
113.   #ifdef  DEBUG_KEYGEN

crypto/pkcs12/p12_key.c:112:5: 
110.       int ret = 0;
111.       BIGNUM *Ij = NULL, *Bpl1 = NULL; /* These hold Ij and B + 1 */
112. >     EVP_MD_CTX *ctx = NULL;
113.   #ifdef  DEBUG_KEYGEN
114.       unsigned char *tmpout = out;

crypto/pkcs12/p12_key.c:118:5: 
116.   #endif
117.   
118. >     ctx = EVP_MD_CTX_new();
119.       if (ctx == NULL)
120.           goto err;

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

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

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

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

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

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

crypto/mem.c:124:9: Taking false branch
      122.       void *ret = NULL;
      123.   
      124.       if (num <= 0)
                     ^
      125.           return NULL;
      126.   

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

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

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

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

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

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

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

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

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

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

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

crypto/pkcs12/p12_key.c:119:9: Taking false branch
117.   
118.       ctx = EVP_MD_CTX_new();
119.       if (ctx == NULL)
               ^
120.           goto err;
121.   

crypto/pkcs12/p12_key.c:130:5: 
128.       h__dump(salt, saltlen);
129.   #endif
130. >     v = EVP_MD_block_size(md_type);
131.       u = EVP_MD_size(md_type);
132.       if (u < 0)

crypto/evp/evp_lib.c:285:1: start of procedure EVP_MD_block_size()
  283.   }
  284.   
  285. > int EVP_MD_block_size(const EVP_MD *md)
  286.   {
  287.       return md->block_size;

crypto/evp/evp_lib.c:287:5: 
  285.   int EVP_MD_block_size(const EVP_MD *md)
  286.   {
  287. >     return md->block_size;
  288.   }
  289.   

crypto/evp/evp_lib.c:288:1: return from a call to EVP_MD_block_size
  286.   {
  287.       return md->block_size;
  288. > }
  289.   
  290.   int EVP_MD_type(const EVP_MD *md)

crypto/pkcs12/p12_key.c:131:5: 
129.   #endif
130.       v = EVP_MD_block_size(md_type);
131. >     u = EVP_MD_size(md_type);
132.       if (u < 0)
133.           return 0;

crypto/evp/evp_lib.c:300:1: start of procedure EVP_MD_size()
  298.   }
  299.   
  300. > int EVP_MD_size(const EVP_MD *md)
  301.   {
  302.       if (!md) {

crypto/evp/evp_lib.c:302:10: Taking false branch
  300.   int EVP_MD_size(const EVP_MD *md)
  301.   {
  302.       if (!md) {
                  ^
  303.           EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
  304.           return -1;

crypto/evp/evp_lib.c:306:5: 
  304.           return -1;
  305.       }
  306. >     return md->md_size;
  307.   }
  308.   

crypto/evp/evp_lib.c:307:1: return from a call to EVP_MD_size
  305.       }
  306.       return md->md_size;
  307. > }
  308.   
  309.   unsigned long EVP_MD_flags(const EVP_MD *md)

crypto/pkcs12/p12_key.c:132:9: Taking true branch
130.       v = EVP_MD_block_size(md_type);
131.       u = EVP_MD_size(md_type);
132.       if (u < 0)
               ^
133.           return 0;
134.       D = OPENSSL_malloc(v);

crypto/pkcs12/p12_key.c:133:9: 
131.       u = EVP_MD_size(md_type);
132.       if (u < 0)
133. >         return 0;
134.       D = OPENSSL_malloc(v);
135.       Ai = OPENSSL_malloc(u);

int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
                       int saltlen, int id, int iter, int n,
                       unsigned char *out, const EVP_MD *md_type)
{
    unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;
    int Slen, Plen, Ilen, Ijlen;
    int i, j, u, v;
    int ret = 0;
    BIGNUM *Ij = NULL, *Bpl1 = NULL;
    EVP_MD_CTX *ctx = NULL;
#ifdef  DEBUG_KEYGEN
    unsigned char *tmpout = out;
    int tmpn = n;
#endif
    ctx = EVP_MD_CTX_new();
    if (ctx == NULL)
        goto err;
#ifdef  DEBUG_KEYGEN
    fprintf(stderr, "KEYGEN DEBUG\n");
    fprintf(stderr, "ID %d, ITER %d\n", id, iter);
    fprintf(stderr, "Password (length %d):\n", passlen);
    h__dump(pass, passlen);
    fprintf(stderr, "Salt (length %d):\n", saltlen);
    h__dump(salt, saltlen);
#endif
    v = EVP_MD_block_size(md_type);
    u = EVP_MD_size(md_type);
    if (u < 0)
        return 0;
    D = OPENSSL_malloc(v);
    Ai = OPENSSL_malloc(u);
    B = OPENSSL_malloc(v + 1);
    Slen = v * ((saltlen + v - 1) / v);
    if (passlen)
        Plen = v * ((passlen + v - 1) / v);
    else
        Plen = 0;
    Ilen = Slen + Plen;
    I = OPENSSL_malloc(Ilen);
    Ij = BN_new();
    Bpl1 = BN_new();
    if (D == NULL || Ai == NULL || B == NULL || I == NULL || Ij == NULL
            || Bpl1 == NULL)
        goto err;
    for (i = 0; i < v; i++)
        D[i] = id;
    p = I;
    for (i = 0; i < Slen; i++)
        *p++ = salt[i % saltlen];
    for (i = 0; i < Plen; i++)
        *p++ = pass[i % passlen];
    for (;;) {
        if (!EVP_DigestInit_ex(ctx, md_type, NULL)
            || !EVP_DigestUpdate(ctx, D, v)
            || !EVP_DigestUpdate(ctx, I, Ilen)
            || !EVP_DigestFinal_ex(ctx, Ai, NULL))
            goto err;
        for (j = 1; j < iter; j++) {
            if (!EVP_DigestInit_ex(ctx, md_type, NULL)
                || !EVP_DigestUpdate(ctx, Ai, u)
                || !EVP_DigestFinal_ex(ctx, Ai, NULL))
                goto err;
        }
        memcpy(out, Ai, min(n, u));
        if (u >= n) {
#ifdef DEBUG_KEYGEN
            fprintf(stderr, "Output KEY (length %d)\n", tmpn);
            h__dump(tmpout, tmpn);
#endif
            ret = 1;
            goto end;
        }
        n -= u;
        out += u;
        for (j = 0; j < v; j++)
            B[j] = Ai[j % u];
        if (!BN_bin2bn(B, v, Bpl1))
            goto err;
        if (!BN_add_word(Bpl1, 1))
            goto err;
        for (j = 0; j < Ilen; j += v) {
            if (!BN_bin2bn(I + j, v, Ij))
                goto err;
            if (!BN_add(Ij, Ij, Bpl1))
                goto err;
            if (!BN_bn2bin(Ij, B))
                goto err;
            Ijlen = BN_num_bytes(Ij);
            if (Ijlen > v) {
                if (!BN_bn2bin(Ij, B))
                    goto err;
                memcpy(I + j, B + 1, v);
#ifndef PKCS12_BROKEN_KEYGEN
            } else if (Ijlen < v) {
                memset(I + j, 0, v - Ijlen);
                if (!BN_bn2bin(Ij, I + j + v - Ijlen))
                    goto err;
#endif
            } else if (!BN_bn2bin(Ij, I + j))
                goto err;
        }
    }
 err:
    PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);
 end:
    OPENSSL_free(Ai);
    OPENSSL_free(B);
    OPENSSL_free(D);
    OPENSSL_free(I);
    BN_free(Ij);
    BN_free(Bpl1);
    EVP_MD_CTX_free(ctx);
    return ret;
}
['int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,\n                       int saltlen, int id, int iter, int n,\n                       unsigned char *out, const EVP_MD *md_type)\n{\n    unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL;\n    int Slen, Plen, Ilen, Ijlen;\n    int i, j, u, v;\n    int ret = 0;\n    BIGNUM *Ij = NULL, *Bpl1 = NULL;\n    EVP_MD_CTX *ctx = NULL;\n#ifdef  DEBUG_KEYGEN\n    unsigned char *tmpout = out;\n    int tmpn = n;\n#endif\n    ctx = EVP_MD_CTX_new();\n    if (ctx == NULL)\n        goto err;\n#ifdef  DEBUG_KEYGEN\n    fprintf(stderr, "KEYGEN DEBUG\\n");\n    fprintf(stderr, "ID %d, ITER %d\\n", id, iter);\n    fprintf(stderr, "Password (length %d):\\n", passlen);\n    h__dump(pass, passlen);\n    fprintf(stderr, "Salt (length %d):\\n", saltlen);\n    h__dump(salt, saltlen);\n#endif\n    v = EVP_MD_block_size(md_type);\n    u = EVP_MD_size(md_type);\n    if (u < 0)\n        return 0;\n    D = OPENSSL_malloc(v);\n    Ai = OPENSSL_malloc(u);\n    B = OPENSSL_malloc(v + 1);\n    Slen = v * ((saltlen + v - 1) / v);\n    if (passlen)\n        Plen = v * ((passlen + v - 1) / v);\n    else\n        Plen = 0;\n    Ilen = Slen + Plen;\n    I = OPENSSL_malloc(Ilen);\n    Ij = BN_new();\n    Bpl1 = BN_new();\n    if (D == NULL || Ai == NULL || B == NULL || I == NULL || Ij == NULL\n            || Bpl1 == NULL)\n        goto err;\n    for (i = 0; i < v; i++)\n        D[i] = id;\n    p = I;\n    for (i = 0; i < Slen; i++)\n        *p++ = salt[i % saltlen];\n    for (i = 0; i < Plen; i++)\n        *p++ = pass[i % passlen];\n    for (;;) {\n        if (!EVP_DigestInit_ex(ctx, md_type, NULL)\n            || !EVP_DigestUpdate(ctx, D, v)\n            || !EVP_DigestUpdate(ctx, I, Ilen)\n            || !EVP_DigestFinal_ex(ctx, Ai, NULL))\n            goto err;\n        for (j = 1; j < iter; j++) {\n            if (!EVP_DigestInit_ex(ctx, md_type, NULL)\n                || !EVP_DigestUpdate(ctx, Ai, u)\n                || !EVP_DigestFinal_ex(ctx, Ai, NULL))\n                goto err;\n        }\n        memcpy(out, Ai, min(n, u));\n        if (u >= n) {\n#ifdef DEBUG_KEYGEN\n            fprintf(stderr, "Output KEY (length %d)\\n", tmpn);\n            h__dump(tmpout, tmpn);\n#endif\n            ret = 1;\n            goto end;\n        }\n        n -= u;\n        out += u;\n        for (j = 0; j < v; j++)\n            B[j] = Ai[j % u];\n        if (!BN_bin2bn(B, v, Bpl1))\n            goto err;\n        if (!BN_add_word(Bpl1, 1))\n            goto err;\n        for (j = 0; j < Ilen; j += v) {\n            if (!BN_bin2bn(I + j, v, Ij))\n                goto err;\n            if (!BN_add(Ij, Ij, Bpl1))\n                goto err;\n            if (!BN_bn2bin(Ij, B))\n                goto err;\n            Ijlen = BN_num_bytes(Ij);\n            if (Ijlen > v) {\n                if (!BN_bn2bin(Ij, B))\n                    goto err;\n                memcpy(I + j, B + 1, v);\n#ifndef PKCS12_BROKEN_KEYGEN\n            } else if (Ijlen < v) {\n                memset(I + j, 0, v - Ijlen);\n                if (!BN_bn2bin(Ij, I + j + v - Ijlen))\n                    goto err;\n#endif\n            } else if (!BN_bn2bin(Ij, I + j))\n                goto err;\n        }\n    }\n err:\n    PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI, ERR_R_MALLOC_FAILURE);\n end:\n    OPENSSL_free(Ai);\n    OPENSSL_free(B);\n    OPENSSL_free(D);\n    OPENSSL_free(I);\n    BN_free(Ij);\n    BN_free(Bpl1);\n    EVP_MD_CTX_free(ctx);\n    return ret;\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 (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 EVP_MD_block_size(const EVP_MD *md)\n{\n    return md->block_size;\n}', 'int EVP_MD_size(const EVP_MD *md)\n{\n    if (!md) {\n        EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);\n        return -1;\n    }\n    return md->md_size;\n}']