Fix premature fopen() call in mbedtls_entropy_write_seed_file #3175
Signed-off-by: Victor Krasnoshchok <ct3da21164@protonmail.ch>
This commit is contained in:
parent
0f343ac87f
commit
e79812ed4d
1 changed files with 10 additions and 11 deletions
|
@ -466,28 +466,27 @@ int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx )
|
||||||
#if defined(MBEDTLS_FS_IO)
|
#if defined(MBEDTLS_FS_IO)
|
||||||
int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
|
int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path )
|
||||||
{
|
{
|
||||||
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
int ret;
|
||||||
FILE *f;
|
FILE *f = NULL;
|
||||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||||
|
|
||||||
if( ( f = fopen( path, "wb" ) ) == NULL )
|
|
||||||
return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR );
|
|
||||||
|
|
||||||
if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 )
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
||||||
if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
|
|
||||||
{
|
|
||||||
ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
||||||
|
if( ( f = fopen( path, "wb" ) ) != NULL )
|
||||||
|
{
|
||||||
|
if( fwrite( buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f ) != MBEDTLS_ENTROPY_BLOCK_SIZE )
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||||
|
|
||||||
|
if( f )
|
||||||
fclose( f );
|
fclose( f );
|
||||||
|
|
||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue