From 6a9257bc5719dcdcc44dd2c0f52208012b1bffe9 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Thu, 24 Aug 2017 14:20:17 +0300 Subject: [PATCH 1/4] 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 | 50 ++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index f1b548d05..b93ea8f75 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -105,7 +105,7 @@ int main( int argc, char *argv[] ) { usage: mbedtls_printf( USAGE ); - goto exit; + goto cleanup; } opt.mode = DFL_MODE; @@ -155,13 +155,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 ); @@ -182,7 +182,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" ); @@ -203,14 +203,14 @@ int main( int argc, char *argv[] ) goto exit; } - mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); - mbedtls_mpi_write_file( "E: ", &E, 16, NULL ); - mbedtls_mpi_write_file( "D: ", &D, 16, NULL ); - mbedtls_mpi_write_file( "P: ", &P, 16, NULL ); - mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ); - mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ); - mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ); - mbedtls_mpi_write_file( "QP: ", &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 @@ -218,16 +218,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 ) @@ -243,7 +243,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" ); @@ -260,8 +260,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); goto exit; } - mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); - mbedtls_mpi_write_file( "E: ", &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 @@ -269,21 +269,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) if( ret != 0 ) From 7a81426a1aecbf156e8b94f496472b59b3eda6e2 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Sun, 24 Jun 2018 16:34:15 +0300 Subject: [PATCH 2/4] Fix style issue Add space before and after paranthesis. --- programs/pkey/key_app.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index b93ea8f75..4dbbdfbda 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -288,7 +288,7 @@ cleanup: #if defined(MBEDTLS_ERROR_C) if( ret != 0 ) { - mbedtls_strerror( ret, buf, sizeof(buf) ); + mbedtls_strerror( ret, buf, sizeof( buf ) ); mbedtls_printf( " ! Last error was: %s\n", buf ); } #endif From a522147f58764a0e9d866445ad63aa4d2f274ef6 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Wed, 27 Jun 2018 08:49:00 +0300 Subject: [PATCH 3/4] Fix compilation errors after updating Fix compilation errorsthat happened after new code introduced by updating the branch. Replaced `exit` label with `cleanup`. --- programs/pkey/key_app.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index 4dbbdfbda..f57dba145 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -200,7 +200,7 @@ int main( int argc, char *argv[] ) ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 ) { mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); - goto exit; + goto cleanup; } MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) ); @@ -258,7 +258,7 @@ int main( int argc, char *argv[] ) NULL, &E ) ) != 0 ) { mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); - goto exit; + goto cleanup; } MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ) ); From bf4709978ce67669a738ede94d498658ac2d7507 Mon Sep 17 00:00:00 2001 From: Ron Eldor Date: Wed, 27 Jun 2018 11:51:46 +0300 Subject: [PATCH 4/4] Adjust to new RSA infrastructure Don't access the rsa cotext parameters directly, but use the local `mbedtls_mpi` variable that were exported. --- programs/pkey/key_app.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index f57dba145..3a74f2770 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -203,14 +203,14 @@ int main( int argc, char *argv[] ) goto cleanup; } - 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 ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) ); } else #endif @@ -260,8 +260,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); goto cleanup; } - 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( "N: ", &N, 16, NULL ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) ); } else #endif