ssl/ssl_cert.c:900: error: MEMORY_LEAK
  memory dynamically allocated by call to `X509_STORE_CTX_new()` at line 893, column 34 is not reachable after line 900, column 13.
Showing all 50 steps of the trace


ssl/ssl_cert.c:857:1: start of procedure ssl_add_cert_chain()
855.   
856.   /* Add certificate chain to internal SSL BUF_MEM strcuture */
857. > int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
858.   {
859.       BUF_MEM *buf = s->init_buf;

ssl/ssl_cert.c:859:5: 
857.   int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
858.   {
859. >     BUF_MEM *buf = s->init_buf;
860.       int i, chain_count;
861.       X509 *x;

ssl/ssl_cert.c:863:5: 
861.       X509 *x;
862.       STACK_OF(X509) *extra_certs;
863. >     STACK_OF(X509) *chain = NULL;
864.       X509_STORE *chain_store;
865.   

ssl/ssl_cert.c:867:10: Taking false branch
865.   
866.       /* TLSv1 sends a chain with nothing in it, instead of an alert */
867.       if (!BUF_MEM_grow_clean(buf, 10)) {
                ^
868.           SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
869.           return 0;

ssl/ssl_cert.c:872:10: Taking false branch
870.       }
871.   
872.       if (!cpk || !cpk->x509)
                ^
873.           return 1;
874.   

ssl/ssl_cert.c:872:18: Taking false branch
870.       }
871.   
872.       if (!cpk || !cpk->x509)
                        ^
873.           return 1;
874.   

ssl/ssl_cert.c:875:5: 
873.           return 1;
874.   
875. >     x = cpk->x509;
876.   
877.       /*

ssl/ssl_cert.c:880:9: Taking false branch
878.        * If we have a certificate specific chain use it, else use parent ctx.
879.        */
880.       if (cpk->chain)
               ^
881.           extra_certs = cpk->chain;
882.       else

ssl/ssl_cert.c:883:9: 
881.           extra_certs = cpk->chain;
882.       else
883. >         extra_certs = s->ctx->extra_certs;
884.   
885.       if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)

ssl/ssl_cert.c:885:10: Taking false branch
883.           extra_certs = s->ctx->extra_certs;
884.   
885.       if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
                ^
886.           chain_store = NULL;
887.       else if (s->cert->chain_store)

ssl/ssl_cert.c:885:47: Taking false branch
883.           extra_certs = s->ctx->extra_certs;
884.   
885.       if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
                                                     ^
886.           chain_store = NULL;
887.       else if (s->cert->chain_store)

ssl/ssl_cert.c:887:14: Taking false branch
885.       if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
886.           chain_store = NULL;
887.       else if (s->cert->chain_store)
                    ^
888.           chain_store = s->cert->chain_store;
889.       else

ssl/ssl_cert.c:890:9: 
888.           chain_store = s->cert->chain_store;
889.       else
890. >         chain_store = s->ctx->cert_store;
891.   
892.       if (chain_store) {

ssl/ssl_cert.c:892:9: Taking true branch
890.           chain_store = s->ctx->cert_store;
891.   
892.       if (chain_store) {
               ^
893.           X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();
894.   

ssl/ssl_cert.c:893:9: 
891.   
892.       if (chain_store) {
893. >         X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();
894.   
895.           if (xs_ctx == NULL) {

crypto/x509/x509_vfy.c:2120:1: start of procedure X509_STORE_CTX_new()
  2118.   }
  2119.   
  2120. > X509_STORE_CTX *X509_STORE_CTX_new(void)
  2121.   {
  2122.       X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));

crypto/x509/x509_vfy.c:2122:5: 
  2120.   X509_STORE_CTX *X509_STORE_CTX_new(void)
  2121.   {
  2122. >     X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  2123.   
  2124.       if (ctx == NULL) {

crypto/mem.c:146:1: start of procedure CRYPTO_zalloc()
    144.   }
    145.   
    146. > void *CRYPTO_zalloc(size_t num, const char *file, int line)
    147.   {
    148.       void *ret = CRYPTO_malloc(num, file, line);

crypto/mem.c:148:5: 
    146.   void *CRYPTO_zalloc(size_t num, const char *file, int line)
    147.   {
    148. >     void *ret = CRYPTO_malloc(num, file, line);
    149.   
    150.       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. >     osslargused(file); osslargused(line);
      140.       ret = malloc(num);
      141.   #endif

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

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

crypto/mem.c:143:5: 
      141.   #endif
      142.   
      143. >     return ret;
      144.   }
      145.   

crypto/mem.c:144:1: return from a call to CRYPTO_malloc
      142.   
      143.       return ret;
      144. > }
      145.   
      146.   void *CRYPTO_zalloc(size_t num, const char *file, int line)

crypto/mem.c:150:9: Taking true branch
    148.       void *ret = CRYPTO_malloc(num, file, line);
    149.   
    150.       if (ret != NULL)
                   ^
    151.           memset(ret, 0, num);
    152.       return ret;

crypto/mem.c:151:9: 
    149.   
    150.       if (ret != NULL)
    151. >         memset(ret, 0, num);
    152.       return ret;
    153.   }

crypto/mem.c:152:5: 
    150.       if (ret != NULL)
    151.           memset(ret, 0, num);
    152. >     return ret;
    153.   }
    154.   

crypto/mem.c:153:1: return from a call to CRYPTO_zalloc
    151.           memset(ret, 0, num);
    152.       return ret;
    153. > }
    154.   
    155.   void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)

crypto/x509/x509_vfy.c:2124:9: Taking false branch
  2122.       X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  2123.   
  2124.       if (ctx == NULL) {
                  ^
  2125.           X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2126.           return NULL;

crypto/x509/x509_vfy.c:2128:5: 
  2126.           return NULL;
  2127.       }
  2128. >     return ctx;
  2129.   }
  2130.   

crypto/x509/x509_vfy.c:2129:1: return from a call to X509_STORE_CTX_new
  2127.       }
  2128.       return ctx;
  2129. > }
  2130.   
  2131.   void X509_STORE_CTX_free(X509_STORE_CTX *ctx)

ssl/ssl_cert.c:895:13: Taking false branch
893.           X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();
894.   
895.           if (xs_ctx == NULL) {
                   ^
896.               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
897.               return (0);

ssl/ssl_cert.c:899:14: Taking true branch
897.               return (0);
898.           }
899.           if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
                    ^
900.               X509_STORE_CTX_free(xs_ctx);
901.               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);

ssl/ssl_cert.c:900:13: 
898.           }
899.           if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
900. >             X509_STORE_CTX_free(xs_ctx);
901.               SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
902.               return (0);

crypto/x509/x509_vfy.c:2131:1: start of procedure X509_STORE_CTX_free()
  2129.   }
  2130.   
  2131. > void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  2132.   {
  2133.       if (ctx == NULL)

crypto/x509/x509_vfy.c:2133:9: Taking false branch
  2131.   void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  2132.   {
  2133.       if (ctx == NULL)
                  ^
  2134.           return;
  2135.   

crypto/x509/x509_vfy.c:2136:5: Skipping X509_STORE_CTX_cleanup(): empty list of specs
  2134.           return;
  2135.   
  2136.       X509_STORE_CTX_cleanup(ctx);
              ^
  2137.       OPENSSL_free(ctx);
  2138.   }

crypto/x509/x509_vfy.c:2137:5: 
  2135.   
  2136.       X509_STORE_CTX_cleanup(ctx);
  2137. >     OPENSSL_free(ctx);
  2138.   }
  2139.   

crypto/mem.c:211:1: start of procedure CRYPTO_free()
    209.   }
    210.   
    211. > void CRYPTO_free(void *str, const char *file, int line)
    212.   {
    213.       if (free_impl != NULL && free_impl != &CRYPTO_free) {

crypto/mem.c:213:9: Taking true branch
    211.   void CRYPTO_free(void *str, const char *file, int line)
    212.   {
    213.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                   ^
    214.           free_impl(str, file, line);
    215.           return;

crypto/mem.c:213:30: Taking true branch
    211.   void CRYPTO_free(void *str, const char *file, int line)
    212.   {
    213.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
                                        ^
    214.           free_impl(str, file, line);
    215.           return;

crypto/mem.c:214:9: Skipping __function_pointer__(): unresolved function pointer
    212.   {
    213.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    214.           free_impl(str, file, line);
                   ^
    215.           return;
    216.       }

crypto/mem.c:215:9: 
    213.       if (free_impl != NULL && free_impl != &CRYPTO_free) {
    214.           free_impl(str, file, line);
    215. >         return;
    216.       }
    217.   

crypto/mem.c:229:1: return from a call to CRYPTO_free
    227.       free(str);
    228.   #endif
    229. > }
    230.   
    231.   void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)

crypto/x509/x509_vfy.c:2138:1: return from a call to X509_STORE_CTX_free
  2136.       X509_STORE_CTX_cleanup(ctx);
  2137.       OPENSSL_free(ctx);
  2138. > }
  2139.   
  2140.   int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,

int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
{
    BUF_MEM *buf = s->init_buf;
    int i, chain_count;
    X509 *x;
    STACK_OF(X509) *extra_certs;
    STACK_OF(X509) *chain = NULL;
    X509_STORE *chain_store;
    if (!BUF_MEM_grow_clean(buf, 10)) {
        SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
        return 0;
    }
    if (!cpk || !cpk->x509)
        return 1;
    x = cpk->x509;
    if (cpk->chain)
        extra_certs = cpk->chain;
    else
        extra_certs = s->ctx->extra_certs;
    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
        chain_store = NULL;
    else if (s->cert->chain_store)
        chain_store = s->cert->chain_store;
    else
        chain_store = s->ctx->cert_store;
    if (chain_store) {
        X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();
        if (xs_ctx == NULL) {
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
            return (0);
        }
        if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
            X509_STORE_CTX_free(xs_ctx);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
            return (0);
        }
        (void) X509_verify_cert(xs_ctx);
        ERR_clear_error();
        chain =  X509_STORE_CTX_get0_chain(xs_ctx);
        i = ssl_security_cert_chain(s, chain, NULL, 0);
        if (i != 1) {
#if 0
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
#endif
            X509_STORE_CTX_free(xs_ctx);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
            return 0;
        }
        chain_count = sk_X509_num(chain);
        for (i = 0; i < chain_count; i++) {
            x = sk_X509_value(chain, i);
            if (!ssl_add_cert_to_buf(buf, l, x)) {
                X509_STORE_CTX_free(xs_ctx);
                return 0;
            }
        }
        X509_STORE_CTX_free(xs_ctx);
    } else {
        i = ssl_security_cert_chain(s, extra_certs, x, 0);
        if (i != 1) {
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
            return 0;
        }
        if (!ssl_add_cert_to_buf(buf, l, x))
            return 0;
        for (i = 0; i < sk_X509_num(extra_certs); i++) {
            x = sk_X509_value(extra_certs, i);
            if (!ssl_add_cert_to_buf(buf, l, x))
                return 0;
        }
    }
    return 1;
}
['int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)\n{\n    BUF_MEM *buf = s->init_buf;\n    int i, chain_count;\n    X509 *x;\n    STACK_OF(X509) *extra_certs;\n    STACK_OF(X509) *chain = NULL;\n    X509_STORE *chain_store;\n    if (!BUF_MEM_grow_clean(buf, 10)) {\n        SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);\n        return 0;\n    }\n    if (!cpk || !cpk->x509)\n        return 1;\n    x = cpk->x509;\n    if (cpk->chain)\n        extra_certs = cpk->chain;\n    else\n        extra_certs = s->ctx->extra_certs;\n    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)\n        chain_store = NULL;\n    else if (s->cert->chain_store)\n        chain_store = s->cert->chain_store;\n    else\n        chain_store = s->ctx->cert_store;\n    if (chain_store) {\n        X509_STORE_CTX* xs_ctx = X509_STORE_CTX_new();\n        if (xs_ctx == NULL) {\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);\n            return (0);\n        }\n        if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {\n            X509_STORE_CTX_free(xs_ctx);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);\n            return (0);\n        }\n        (void) X509_verify_cert(xs_ctx);\n        ERR_clear_error();\n        chain =  X509_STORE_CTX_get0_chain(xs_ctx);\n        i = ssl_security_cert_chain(s, chain, NULL, 0);\n        if (i != 1) {\n#if 0\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);\n#endif\n            X509_STORE_CTX_free(xs_ctx);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n            return 0;\n        }\n        chain_count = sk_X509_num(chain);\n        for (i = 0; i < chain_count; i++) {\n            x = sk_X509_value(chain, i);\n            if (!ssl_add_cert_to_buf(buf, l, x)) {\n                X509_STORE_CTX_free(xs_ctx);\n                return 0;\n            }\n        }\n        X509_STORE_CTX_free(xs_ctx);\n    } else {\n        i = ssl_security_cert_chain(s, extra_certs, x, 0);\n        if (i != 1) {\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n            return 0;\n        }\n        if (!ssl_add_cert_to_buf(buf, l, x))\n            return 0;\n        for (i = 0; i < sk_X509_num(extra_certs); i++) {\n            x = sk_X509_value(extra_certs, i);\n            if (!ssl_add_cert_to_buf(buf, l, x))\n                return 0;\n        }\n    }\n    return 1;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n    X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n    if (ctx == NULL) {\n        X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    return 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    osslargused(file); osslargused(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    X509_STORE_CTX_cleanup(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}']