Fix SSL memory leak.

This commit is contained in:
Ben Laurie 2001-08-28 13:45:41 +00:00
parent 5e2c4e23f4
commit 1f3b65801b
4 changed files with 29 additions and 2 deletions

View File

@ -80,4 +80,8 @@ ENGINE *ENGINE_openbsd_dev_crypto(void)
return engine; return engine;
} }
#endif /* defined(OPENSSL_OPENBSD_DEV_CRYPTO) */ #else /* !defined(OPENSSL_OPENBSD_DEV_CRYPTO) */
static void *dummy=&dummy;
#endif /* !defined(OPENSSL_OPENBSD_DEV_CRYPTO) */

View File

@ -75,13 +75,22 @@ EVP_MD_CTX *EVP_MD_CTX_create(void)
return ctx; return ctx;
} }
#ifdef CRYPTO_MDEBUG
int EVP_DigestInit_dbg(EVP_MD_CTX *ctx, const EVP_MD *type,const char *file,
int line)
#else
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)
#endif
{ {
if(ctx->digest != type) if(ctx->digest != type)
{ {
OPENSSL_free(ctx->md_data); OPENSSL_free(ctx->md_data);
ctx->digest=type; ctx->digest=type;
#ifdef CRYPTO_MDEBUG
ctx->md_data=CRYPTO_malloc(type->ctx_size,file,line);
#else
ctx->md_data=OPENSSL_malloc(type->ctx_size); ctx->md_data=OPENSSL_malloc(type->ctx_size);
#endif
} }
return type->init(ctx->md_data); return type->init(ctx->md_data);
} }
@ -142,7 +151,12 @@ void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
/* This call frees resources associated with the context */ /* This call frees resources associated with the context */
int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
{ {
/* assume ctx->md_data was cleaned in EVP_Digest_Final */ /* Don't assume ctx->md_data was cleaned in EVP_Digest_Final,
* because sometimes only copies of the context are ever finalised.
*/
if(ctx->md_data)
memset(ctx->md_data,0,ctx->digest->ctx_size);
OPENSSL_free(ctx->md_data); OPENSSL_free(ctx->md_data);
memset(ctx,'\0',sizeof *ctx); memset(ctx,'\0',sizeof *ctx);

View File

@ -443,7 +443,13 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
EVP_MD_CTX *EVP_MD_CTX_create(void); EVP_MD_CTX *EVP_MD_CTX_create(void);
void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx); void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
int EVP_MD_CTX_copy(EVP_MD_CTX *out,const EVP_MD_CTX *in); int EVP_MD_CTX_copy(EVP_MD_CTX *out,const EVP_MD_CTX *in);
#ifdef CRYPTO_MDEBUG
int EVP_DigestInit_dbg(EVP_MD_CTX *ctx, const EVP_MD *type,
const char *file,int line);
#define EVP_DigestInit(ctx,type) EVP_DigestInit_dbg(ctx,type,__FILE__,__LINE__)
#else
int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
#endif
int EVP_DigestUpdate(EVP_MD_CTX *ctx,const void *d, int EVP_DigestUpdate(EVP_MD_CTX *ctx,const void *d,
unsigned int cnt); unsigned int cnt);
int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s); int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);

View File

@ -1009,6 +1009,9 @@ void ssl3_clear(SSL *s)
rp=s->s3->rbuf.buf; rp=s->s3->rbuf.buf;
wp=s->s3->wbuf.buf; wp=s->s3->wbuf.buf;
EVP_MD_CTX_cleanup(&s->s3->finish_dgst1);
EVP_MD_CTX_cleanup(&s->s3->finish_dgst2);
memset(s->s3,0,sizeof *s->s3); memset(s->s3,0,sizeof *s->s3);
if (rp != NULL) s->s3->rbuf.buf=rp; if (rp != NULL) s->s3->rbuf.buf=rp;
if (wp != NULL) s->s3->wbuf.buf=wp; if (wp != NULL) s->s3->wbuf.buf=wp;