mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2026-01-04 13:45:05 +01:00
Cleaned up location of init and free for some programs to prevent memory
leaks on incorrect arguments
This commit is contained in:
parent
cbe3d0d5cc
commit
0c22610693
19 changed files with 95 additions and 48 deletions
|
|
@ -272,7 +272,9 @@ int main( int argc, char *argv[] )
|
|||
|
||||
exit:
|
||||
|
||||
net_close( server_fd );
|
||||
if( server_fd != -1 )
|
||||
net_close( server_fd );
|
||||
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
entropy_free( &entropy );
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ int main( int argc, char *argv[] )
|
|||
((void) argv);
|
||||
|
||||
mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
|
||||
entropy_init( &entropy );
|
||||
|
||||
if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
|
||||
{
|
||||
|
|
@ -84,7 +85,6 @@ int main( int argc, char *argv[] )
|
|||
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 )
|
||||
|
|
|
|||
|
|
@ -273,7 +273,9 @@ int main( int argc, char *argv[] )
|
|||
|
||||
exit:
|
||||
|
||||
net_close( client_fd );
|
||||
if( client_fd != -1 )
|
||||
net_close( client_fd );
|
||||
|
||||
rsa_free( &rsa );
|
||||
dhm_free( &dhm );
|
||||
entropy_free( &entropy );
|
||||
|
|
|
|||
|
|
@ -146,9 +146,12 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,9 +104,12 @@ static int write_public_key( pk_context *key, const char *output_file )
|
|||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
@ -140,9 +143,12 @@ static int write_private_key( pk_context *key, const char *output_file )
|
|||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ int main( int argc, char *argv[] )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -68,7 +68,8 @@ int main( int argc, char *argv[] )
|
|||
const char *pers = "pk_sign";
|
||||
size_t olen = 0;
|
||||
|
||||
ret = 1;
|
||||
entropy_init( &entropy );
|
||||
pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
|
|
@ -84,7 +85,6 @@ int main( int argc, char *argv[] )
|
|||
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 )
|
||||
|
|
@ -96,8 +96,6 @@ int main( int argc, char *argv[] )
|
|||
printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
|
|
|
|||
|
|
@ -54,14 +54,15 @@ int main( int argc, char *argv[] )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
size_t i;
|
||||
pk_context pk;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
ret = 1;
|
||||
pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
printf( "usage: pk_verify <key_file> <filename>\n" );
|
||||
|
|
@ -76,8 +77,6 @@ int main( int argc, char *argv[] )
|
|||
printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ int main( int argc, char *argv[] )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
pk_context pk;
|
||||
entropy_context entropy;
|
||||
ctr_drbg_context ctr_drbg;
|
||||
|
|
@ -68,7 +68,8 @@ int main( int argc, char *argv[] )
|
|||
const char *pers = "rsa_sign_pss";
|
||||
size_t olen = 0;
|
||||
|
||||
ret = 1;
|
||||
entropy_init( &entropy );
|
||||
pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
|
|
@ -84,7 +85,6 @@ int main( int argc, char *argv[] )
|
|||
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 )
|
||||
|
|
@ -96,8 +96,6 @@ int main( int argc, char *argv[] )
|
|||
printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
|
|
|
|||
|
|
@ -55,14 +55,15 @@ int main( int argc, char *argv[] )
|
|||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret;
|
||||
int ret = 1;
|
||||
size_t i;
|
||||
pk_context pk;
|
||||
unsigned char hash[20];
|
||||
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
ret = 1;
|
||||
pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
|
||||
|
|
@ -77,8 +78,6 @@ int main( int argc, char *argv[] )
|
|||
printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
pk_init( &pk );
|
||||
|
||||
if( ( ret = pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue