Cleaned up location of init and free for some programs to prevent memory

leaks on incorrect arguments
This commit is contained in:
Paul Bakker 2014-04-17 16:02:36 +02:00
parent 993f02cda0
commit 3914840d78
13 changed files with 51 additions and 30 deletions

View File

@ -136,6 +136,8 @@ static int md5_check( char *filename )
n = sizeof( line ); n = sizeof( line );
} }
fclose( f );
if( nb_err1 != 0 ) if( nb_err1 != 0 )
{ {
printf( "WARNING: %d (out of %d) input files could " printf( "WARNING: %d (out of %d) input files could "

View File

@ -136,6 +136,8 @@ static int sha1_check( char *filename )
n = sizeof( line ); n = sizeof( line );
} }
fclose( f );
if( nb_err1 != 0 ) if( nb_err1 != 0 )
{ {
printf( "WARNING: %d (out of %d) input files could " printf( "WARNING: %d (out of %d) input files could "

View File

@ -136,6 +136,8 @@ static int sha2_check( char *filename )
n = sizeof( line ); n = sizeof( line );
} }
fclose( f );
if( nb_err1 != 0 ) if( nb_err1 != 0 )
{ {
printf( "WARNING: %d (out of %d) input files could " printf( "WARNING: %d (out of %d) input files could "

View File

@ -275,7 +275,9 @@ int main( int argc, char *argv[] )
exit: exit:
net_close( server_fd ); if( server_fd != -1 )
net_close( server_fd );
rsa_free( &rsa ); rsa_free( &rsa );
dhm_free( &dhm ); dhm_free( &dhm );

View File

@ -69,6 +69,7 @@ int main( int argc, char *argv[] )
((void) argv); ((void) argv);
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
entropy_init( &entropy );
if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
{ {
@ -86,7 +87,6 @@ int main( int argc, char *argv[] )
printf( "\n . Seeding the random number generator..." ); printf( "\n . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
entropy_init( &entropy );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )

View File

@ -276,7 +276,9 @@ int main( int argc, char *argv[] )
exit: exit:
net_close( client_fd ); if( client_fd != -1 )
net_close( client_fd );
rsa_free( &rsa ); rsa_free( &rsa );
dhm_free( &dhm ); dhm_free( &dhm );

View File

@ -62,7 +62,7 @@ int main( int argc, char *argv[] )
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
FILE *f; FILE *f;
int ret; int ret = 1;
rsa_context rsa; rsa_context rsa;
entropy_context entropy; entropy_context entropy;
ctr_drbg_context ctr_drbg; ctr_drbg_context ctr_drbg;
@ -71,7 +71,8 @@ int main( int argc, char *argv[] )
char filename[512]; char filename[512];
const char *pers = "rsa_sign_pss"; const char *pers = "rsa_sign_pss";
ret = 1; entropy_init( &entropy );
rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
if( argc != 3 ) if( argc != 3 )
{ {
@ -87,7 +88,6 @@ int main( int argc, char *argv[] )
printf( "\n . Seeding the random number generator..." ); printf( "\n . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
entropy_init( &entropy );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -99,8 +99,6 @@ int main( int argc, char *argv[] )
printf( "\n . Reading private key from '%s'", argv[1] ); printf( "\n . Reading private key from '%s'", argv[1] );
fflush( stdout ); fflush( stdout );
rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 ) if( ( ret = x509parse_keyfile( &rsa, argv[1], "" ) ) != 0 )
{ {
ret = 1; ret = 1;

View File

@ -59,14 +59,15 @@ int main( int argc, char *argv[] )
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
FILE *f; FILE *f;
int ret; int ret = 1;
size_t i; size_t i;
rsa_context rsa; rsa_context rsa;
unsigned char hash[20]; unsigned char hash[20];
unsigned char buf[POLARSSL_MPI_MAX_SIZE]; unsigned char buf[POLARSSL_MPI_MAX_SIZE];
char filename[512]; char filename[512];
ret = 1; rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
if( argc != 3 ) if( argc != 3 )
{ {
printf( "usage: rsa_verify_pss <key_file> <filename>\n" ); printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
@ -81,8 +82,6 @@ int main( int argc, char *argv[] )
printf( "\n . Reading public key from '%s'", argv[1] ); printf( "\n . Reading public key from '%s'", argv[1] );
fflush( stdout ); fflush( stdout );
rsa_init( &rsa, RSA_PKCS_V21, POLARSSL_MD_SHA1 );
if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 ) if( ( ret = x509parse_public_keyfile( &rsa, argv[1] ) ) != 0 )
{ {
printf( " failed\n ! x509parse_public_key returned %d\n\n", ret ); printf( " failed\n ! x509parse_public_key returned %d\n\n", ret );

View File

@ -279,8 +279,10 @@ exit:
} }
#endif #endif
if( server_fd != -1 )
net_close( server_fd );
x509_free( &cacert ); x509_free( &cacert );
net_close( server_fd );
ssl_free( &ssl ); ssl_free( &ssl );
memset( &ssl, 0, sizeof( ssl ) ); memset( &ssl, 0, sizeof( ssl ) );

View File

@ -110,6 +110,12 @@ int main( int argc, char *argv[] )
((void) argc); ((void) argc);
((void) argv); ((void) argv);
memset( &ssl, 0, sizeof(ssl_context) );
entropy_init( &entropy );
rsa_init( &rsa, RSA_PKCS_V15, 0 );
memset( &srvcert, 0, sizeof( x509_cert ) );
signal( SIGCHLD, SIG_IGN ); signal( SIGCHLD, SIG_IGN );
/* /*
@ -118,7 +124,6 @@ int main( int argc, char *argv[] )
printf( "\n . Initial seeding of the random generator..." ); printf( "\n . Initial seeding of the random generator..." );
fflush( stdout ); fflush( stdout );
entropy_init( &entropy );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -135,8 +140,6 @@ int main( int argc, char *argv[] )
printf( " . Loading the server cert. and key..." ); printf( " . Loading the server cert. and key..." );
fflush( stdout ); fflush( stdout );
memset( &srvcert, 0, sizeof( x509_cert ) );
/* /*
* This demonstration program uses embedded test certificates. * This demonstration program uses embedded test certificates.
* Instead, you may want to use x509parse_crtfile() to read the * Instead, you may want to use x509parse_crtfile() to read the
@ -158,7 +161,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key, ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
strlen( test_srv_key ), NULL, 0 ); strlen( test_srv_key ), NULL, 0 );
if( ret != 0 ) if( ret != 0 )
@ -362,7 +364,9 @@ int main( int argc, char *argv[] )
exit: exit:
net_close( client_fd ); if( client_fd != -1 )
net_close( client_fd );
x509_free( &srvcert ); x509_free( &srvcert );
rsa_free( &rsa ); rsa_free( &rsa );
ssl_free( &ssl ); ssl_free( &ssl );

View File

@ -101,9 +101,13 @@ int main( int argc, char *argv[] )
((void) argc); ((void) argc);
((void) argv); ((void) argv);
memset( &ssl, 0, sizeof(ssl_context) );
#if defined(POLARSSL_SSL_CACHE_C) #if defined(POLARSSL_SSL_CACHE_C)
ssl_cache_init( &cache ); ssl_cache_init( &cache );
#endif #endif
memset( &srvcert, 0, sizeof( x509_cert ) );
rsa_init( &rsa, RSA_PKCS_V15, 0 );
entropy_init( &entropy );
/* /*
* 1. Load the certificates and private RSA key * 1. Load the certificates and private RSA key
@ -111,8 +115,6 @@ int main( int argc, char *argv[] )
printf( "\n . Loading the server cert. and key..." ); printf( "\n . Loading the server cert. and key..." );
fflush( stdout ); fflush( stdout );
memset( &srvcert, 0, sizeof( x509_cert ) );
/* /*
* This demonstration program uses embedded test certificates. * This demonstration program uses embedded test certificates.
* Instead, you may want to use x509parse_crtfile() to read the * Instead, you may want to use x509parse_crtfile() to read the
@ -134,7 +136,6 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
rsa_init( &rsa, RSA_PKCS_V15, 0 );
ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key, ret = x509parse_key( &rsa, (const unsigned char *) test_srv_key,
strlen( test_srv_key ), NULL, 0 ); strlen( test_srv_key ), NULL, 0 );
if( ret != 0 ) if( ret != 0 )
@ -165,7 +166,6 @@ int main( int argc, char *argv[] )
printf( " . Seeding the random number generator..." ); printf( " . Seeding the random number generator..." );
fflush( stdout ); fflush( stdout );
entropy_init( &entropy );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -353,7 +353,9 @@ exit:
} }
#endif #endif
net_close( client_fd ); if( client_fd != -1 )
net_close( client_fd );
x509_free( &srvcert ); x509_free( &srvcert );
rsa_free( &rsa ); rsa_free( &rsa );
ssl_free( &ssl ); ssl_free( &ssl );

View File

@ -712,7 +712,10 @@ exit:
} }
#endif #endif
net_close( client_fd );
if( client_fd != -1 )
net_close( client_fd );
x509_free( &srvcert ); x509_free( &srvcert );
x509_free( &cacert ); x509_free( &cacert );
rsa_free( &rsa ); rsa_free( &rsa );

View File

@ -172,7 +172,11 @@ static int ssl_test( struct options *opt )
ret = 1; ret = 1;
memset( &ssl, 0, sizeof(ssl_context) );
entropy_init( &entropy ); entropy_init( &entropy );
memset( &srvcert, 0, sizeof( x509_cert ) );
memset( &rsa, 0, sizeof( rsa_context ) );
if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) strlen( pers ) ) ) != 0 )
@ -186,9 +190,6 @@ static int ssl_test( struct options *opt )
memset( read_state, 0, sizeof( read_state ) ); memset( read_state, 0, sizeof( read_state ) );
memset( write_state, 0, sizeof( write_state ) ); memset( write_state, 0, sizeof( write_state ) );
memset( &srvcert, 0, sizeof( x509_cert ) );
memset( &rsa, 0, sizeof( rsa_context ) );
if( opt->opmode == OPMODE_CLIENT ) if( opt->opmode == OPMODE_CLIENT )
{ {
if( ( ret = net_connect( &client_fd, opt->server_name, if( ( ret = net_connect( &client_fd, opt->server_name,
@ -201,7 +202,7 @@ static int ssl_test( struct options *opt )
if( ( ret = ssl_init( &ssl ) ) != 0 ) if( ( ret = ssl_init( &ssl ) ) != 0 )
{ {
printf( " ! ssl_init returned %d\n\n", ret ); printf( " ! ssl_init returned %d\n\n", ret );
return( ret ); goto exit;
} }
ssl_set_endpoint( &ssl, SSL_IS_CLIENT ); ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
@ -408,7 +409,9 @@ exit:
x509_free( &srvcert ); x509_free( &srvcert );
rsa_free( &rsa ); rsa_free( &rsa );
ssl_free( &ssl ); ssl_free( &ssl );
net_close( client_fd );
if( client_fd != -1 )
net_close( client_fd );
return( ret ); return( ret );
} }