added missing calls to EVP_CIPHER_CTX_cleanup() to release memory after decryption, causing memory leaks

This commit is contained in:
csoler 2016-04-21 19:41:21 -04:00
parent 3fae108cb1
commit 0c711a4c61

View File

@ -509,7 +509,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// move to end // move to end
out_offset += out_currOffset; out_offset += out_currOffset;
// make sure offset has not gone passed valid memory bounds // make sure offset has not gone passed valid memory bounds
if(out_offset > max_outlen) if(out_offset > max_outlen)
{ {
@ -521,6 +521,8 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// free encrypted key data // free encrypted key data
free(ek); free(ek);
EVP_CIPHER_CTX_cleanup(&ctx);
outlen = out_offset; outlen = out_offset;
return true; return true;
} }
@ -538,6 +540,8 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
// //
out = NULL ; out = NULL ;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
try try
{ {
@ -561,9 +565,7 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
} }
} }
EVP_CIPHER_CTX ctx;
unsigned char iv[EVP_MAX_IV_LENGTH]; unsigned char iv[EVP_MAX_IV_LENGTH];
EVP_CIPHER_CTX_init(&ctx);
std::vector<unsigned char *> ek(keys.size(),NULL) ; std::vector<unsigned char *> ek(keys.size(),NULL) ;
std::vector<int> eklen(keys.size(),0) ; std::vector<int> eklen(keys.size(),0) ;
@ -653,12 +655,16 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u
if(ek[i]) free(ek[i]); if(ek[i]) free(ek[i]);
outlen = out_offset; outlen = out_offset;
EVP_CIPHER_CTX_cleanup(&ctx);
return true; return true;
} }
catch(std::exception& e) catch(std::exception& e)
{ {
std::cerr << "(EE) Exception caught while encrypting: " << e.what() << std::endl; std::cerr << "(EE) Exception caught while encrypting: " << e.what() << std::endl;
EVP_CIPHER_CTX_cleanup(&ctx);
if(out) free(out) ; if(out) free(out) ;
out = NULL ; out = NULL ;
@ -767,6 +773,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
outlen += out_currOffset; outlen += out_currOffset;
free(ek); free(ek);
EVP_CIPHER_CTX_cleanup(&ctx);
return true; return true;
} }
@ -783,9 +790,13 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
#ifdef DISTRIB_DEBUG #ifdef DISTRIB_DEBUG
std::cerr << "GxsSecurity::decrypt() " << std::endl; std::cerr << "GxsSecurity::decrypt() " << std::endl;
#endif #endif
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
try try
{ {
out = NULL ;
// check that the input block has a valid format. // check that the input block has a valid format.
uint32_t offset = 0 ; uint32_t offset = 0 ;
@ -826,8 +837,6 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
// decrypt // decrypt
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
bool succeed = false; bool succeed = false;
for(uint32_t j=0;j<keys.size() && !succeed;++j) for(uint32_t j=0;j<keys.size() && !succeed;++j)
@ -890,6 +899,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
#ifdef GXS_SECURITY_DEBUG #ifdef GXS_SECURITY_DEBUG
std::cerr << " successfully decrypted block of size " << outlen << std::endl; std::cerr << " successfully decrypted block of size " << outlen << std::endl;
#endif #endif
EVP_CIPHER_CTX_cleanup(&ctx);
return true; return true;
} }
catch(std::exception& e) catch(std::exception& e)
@ -905,6 +915,7 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in,
out = NULL ; out = NULL ;
} }
EVP_CIPHER_CTX_cleanup(&ctx);
return false; return false;
} }
} }