Correct memory leak in pk_decrypt example program

This commit is contained in:
Hanno Becker 2017-10-08 16:44:10 +01:00
parent 55c11ba283
commit bd336c1fac

View File

@ -73,7 +73,10 @@ int main( int argc, char *argv[] )
const char *pers = "mbedtls_pk_decrypt"; const char *pers = "mbedtls_pk_decrypt";
((void) argv); ((void) argv);
mbedtls_pk_init( &pk );
mbedtls_entropy_init( &entropy );
mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_ctr_drbg_init( &ctr_drbg );
memset(result, 0, sizeof( result ) ); memset(result, 0, sizeof( result ) );
if( argc != 2 ) if( argc != 2 )
@ -90,7 +93,6 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Seeding the random number generator..." ); mbedtls_printf( "\n . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
mbedtls_entropy_init( &entropy );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -102,8 +104,6 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] ); mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
fflush( stdout ); fflush( stdout );
mbedtls_pk_init( &pk );
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
{ {
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
@ -116,6 +116,7 @@ int main( int argc, char *argv[] )
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
{ {
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
ret = 1;
goto exit; goto exit;
} }
@ -147,8 +148,10 @@ int main( int argc, char *argv[] )
exit_code = MBEDTLS_EXIT_SUCCESS; exit_code = MBEDTLS_EXIT_SUCCESS;
exit: exit:
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_pk_free( &pk );
mbedtls_entropy_free( &entropy ); mbedtls_entropy_free( &entropy );
mbedtls_ctr_drbg_free( &ctr_drbg );
#if defined(MBEDTLS_ERROR_C) #if defined(MBEDTLS_ERROR_C)
if( exit_code != MBEDTLS_EXIT_SUCCESS ) if( exit_code != MBEDTLS_EXIT_SUCCESS )