From 45d23d62a11d7aa9348537ceadffd6d39c5cdb37 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Thu, 24 Aug 2017 14:20:17 +0300 Subject: [PATCH 1/2] Add check for return code of bignumber code Add check for return code of `mbedtls_mpi_write_file` as commented by @sbutcher-arm --- programs/pkey/key_app.c | 51 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index b6b84464d..7aa7651f8 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -99,7 +99,7 @@ int main( int argc, char *argv[] ) { usage: mbedtls_printf( USAGE ); - goto exit; + goto cleanup; } opt.mode = DFL_MODE; @@ -149,13 +149,13 @@ int main( int argc, char *argv[] ) if( ( f = fopen( opt.password_file, "rb" ) ) == NULL ) { mbedtls_printf( " failed\n ! fopen returned NULL\n" ); - goto exit; + goto cleanup; } if( fgets( buf, sizeof(buf), f ) == NULL ) { fclose( f ); mbedtls_printf( "Error: fgets() failed to retrieve password\n" ); - goto exit; + goto cleanup; } fclose( f ); @@ -176,7 +176,7 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); - goto exit; + goto cleanup; } mbedtls_printf( " ok\n" ); @@ -189,14 +189,15 @@ int main( int argc, char *argv[] ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); - mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ); - mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ); - mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL ); - mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL ); - mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); - mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); - mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); - mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); + + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL ) ); } else #endif @@ -204,16 +205,16 @@ int main( int argc, char *argv[] ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); - mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); - mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); - mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); - mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) ); } else #endif { mbedtls_printf("Do not know how to print key information for this type\n" ); - goto exit; + goto cleanup; } } else if( opt.mode == MODE_PUBLIC ) @@ -229,7 +230,7 @@ int main( int argc, char *argv[] ) if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); - goto exit; + goto cleanup; } mbedtls_printf( " ok\n" ); @@ -239,8 +240,8 @@ int main( int argc, char *argv[] ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); - mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ); - mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) ); } else #endif @@ -248,21 +249,21 @@ int main( int argc, char *argv[] ) if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); - mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); - mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); - mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) ); } else #endif { mbedtls_printf("Do not know how to print key information for this type\n" ); - goto exit; + goto cleanup; } } else goto usage; -exit: +cleanup: #if defined(MBEDTLS_ERROR_C) mbedtls_strerror( ret, buf, sizeof(buf) ); From c24108a8c82f625d05f569bc0accf17d9995c7aa Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Sun, 24 Jun 2018 16:34:15 +0300 Subject: [PATCH 2/2] Fix style issue Add space before and after paranthesis. --- programs/pkey/key_app.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index 7aa7651f8..96c6b70e8 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -266,8 +266,11 @@ int main( int argc, char *argv[] ) cleanup: #if defined(MBEDTLS_ERROR_C) - mbedtls_strerror( ret, buf, sizeof(buf) ); - mbedtls_printf( " ! Last error was: %s\n", buf ); + if( ret != 0 ) + { + mbedtls_strerror( ret, buf, sizeof( buf ) ); + mbedtls_printf( " ! Last error was: %s\n", buf ); + } #endif mbedtls_pk_free( &pk );