From fa4a88a8ebdaf55ebcf24916a9d5e5a03c5c0967 Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Thu, 17 Mar 2016 15:21:39 +0000 Subject: [PATCH] Fix memory leaks in example programs. --- programs/pkey/rsa_decrypt.c | 8 ++++---- programs/pkey/rsa_encrypt.c | 4 ++-- programs/pkey/rsa_sign.c | 5 +++-- programs/pkey/rsa_verify.c | 6 ++++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index dfa475c1a..0ea50074b 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -69,6 +69,8 @@ int main( int argc, char *argv[] ) ((void) argv); memset(result, 0, sizeof( result ) ); + entropy_init( &entropy ); + rsa_init( &rsa, RSA_PKCS_V15, 0 ); ret = 1; if( argc != 1 ) @@ -79,13 +81,12 @@ int main( int argc, char *argv[] ) polarssl_printf( "\n" ); #endif - goto exit; + return ret; } polarssl_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); - entropy_init( &entropy ); if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) @@ -104,8 +105,6 @@ int main( int argc, char *argv[] ) goto exit; } - rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || @@ -171,6 +170,7 @@ int main( int argc, char *argv[] ) exit: ctr_drbg_free( &ctr_drbg ); entropy_free( &entropy ); + rsa_free( &rsa ); #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); diff --git a/programs/pkey/rsa_encrypt.c b/programs/pkey/rsa_encrypt.c index 683183324..0e9b9e3cb 100644 --- a/programs/pkey/rsa_encrypt.c +++ b/programs/pkey/rsa_encrypt.c @@ -68,6 +68,7 @@ int main( int argc, char *argv[] ) unsigned char buf[512]; const char *pers = "rsa_encrypt"; + rsa_init( &rsa, RSA_PKCS_V15, 0 ); ret = 1; if( argc != 2 ) @@ -104,8 +105,6 @@ int main( int argc, char *argv[] ) goto exit; } - rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) { @@ -160,6 +159,7 @@ int main( int argc, char *argv[] ) exit: ctr_drbg_free( &ctr_drbg ); entropy_free( &entropy ); + rsa_free( &rsa ); #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); diff --git a/programs/pkey/rsa_sign.c b/programs/pkey/rsa_sign.c index c9bdd3afe..a3162f263 100644 --- a/programs/pkey/rsa_sign.c +++ b/programs/pkey/rsa_sign.c @@ -67,6 +67,7 @@ int main( int argc, char *argv[] ) unsigned char buf[POLARSSL_MPI_MAX_SIZE]; char filename[512]; + rsa_init( &rsa, RSA_PKCS_V15, 0 ); ret = 1; if( argc != 2 ) @@ -91,8 +92,6 @@ int main( int argc, char *argv[] ) goto exit; } - rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || @@ -160,6 +159,8 @@ int main( int argc, char *argv[] ) exit: + rsa_free( &rsa ); + #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); diff --git a/programs/pkey/rsa_verify.c b/programs/pkey/rsa_verify.c index 4011df6a2..ece1588df 100644 --- a/programs/pkey/rsa_verify.c +++ b/programs/pkey/rsa_verify.c @@ -66,7 +66,9 @@ int main( int argc, char *argv[] ) unsigned char buf[POLARSSL_MPI_MAX_SIZE]; char filename[512]; + rsa_init( &rsa, RSA_PKCS_V15, 0 ); ret = 1; + if( argc != 2 ) { polarssl_printf( "usage: rsa_verify \n" ); @@ -88,8 +90,6 @@ int main( int argc, char *argv[] ) goto exit; } - rsa_init( &rsa, RSA_PKCS_V15, 0 ); - if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) { @@ -152,6 +152,8 @@ int main( int argc, char *argv[] ) exit: + rsa_free( &rsa ); + #if defined(_WIN32) polarssl_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar();