From 8c60bdff5b28fde4f018ffb427d9a250cf34426c 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 | 6 +++--- programs/pkey/rsa_encrypt.c | 4 ++-- programs/pkey/rsa_sign.c | 5 +++-- programs/pkey/rsa_verify.c | 6 ++++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index 94431e0ce..37227e8a5 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -69,6 +69,8 @@ int main( int argc, char *argv[] ) memset(result, 0, sizeof( result ) ); mbedtls_ctr_drbg_init( &ctr_drbg ); + mbedtls_entropy_init( &entropy ); + mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); ret = 1; if( argc != 1 ) @@ -85,7 +87,6 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); - mbedtls_entropy_init( &entropy ); if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) @@ -104,8 +105,6 @@ int main( int argc, char *argv[] ) goto exit; } - mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); - if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 || @@ -171,6 +170,7 @@ int main( int argc, char *argv[] ) exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); + mbedtls_rsa_free( &rsa ); #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); diff --git a/programs/pkey/rsa_encrypt.c b/programs/pkey/rsa_encrypt.c index 796343f1b..033ca11b5 100644 --- a/programs/pkey/rsa_encrypt.c +++ b/programs/pkey/rsa_encrypt.c @@ -67,6 +67,7 @@ int main( int argc, char *argv[] ) unsigned char buf[512]; const char *pers = "rsa_encrypt"; + mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); mbedtls_ctr_drbg_init( &ctr_drbg ); ret = 1; @@ -104,8 +105,6 @@ int main( int argc, char *argv[] ) goto exit; } - mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); - if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 ) { @@ -160,6 +159,7 @@ int main( int argc, char *argv[] ) exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); + mbedtls_rsa_free( &rsa ); #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); diff --git a/programs/pkey/rsa_sign.c b/programs/pkey/rsa_sign.c index e897c6519..27f356632 100644 --- a/programs/pkey/rsa_sign.c +++ b/programs/pkey/rsa_sign.c @@ -62,6 +62,7 @@ int main( int argc, char *argv[] ) unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; char filename[512]; + mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); ret = 1; if( argc != 2 ) @@ -86,8 +87,6 @@ int main( int argc, char *argv[] ) goto exit; } - mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); - if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 || @@ -157,6 +156,8 @@ int main( int argc, char *argv[] ) exit: + mbedtls_rsa_free( &rsa ); + #if defined(_WIN32) mbedtls_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 ade36dc83..a22b55521 100644 --- a/programs/pkey/rsa_verify.c +++ b/programs/pkey/rsa_verify.c @@ -61,7 +61,9 @@ int main( int argc, char *argv[] ) unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; char filename[512]; + mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); ret = 1; + if( argc != 2 ) { mbedtls_printf( "usage: rsa_verify \n" ); @@ -83,8 +85,6 @@ int main( int argc, char *argv[] ) goto exit; } - mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); - if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 || ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 ) { @@ -149,6 +149,8 @@ int main( int argc, char *argv[] ) exit: + mbedtls_rsa_free( &rsa ); + #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar();