From da44de60b125b2c9dc8d7b08b7bd7b67935c476f Mon Sep 17 00:00:00 2001 From: Kevin Luty Date: Fri, 13 Oct 2017 13:18:28 -0500 Subject: [PATCH 1/5] Fix for returning correct error code --- programs/pkey/pk_sign.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index daf08a905..5cc190eee 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -100,7 +100,6 @@ int main( int argc, char *argv[] ) if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) { - ret = 1; mbedtls_printf( " failed\n ! Could not open '%s'\n", argv[1] ); goto exit; } @@ -134,7 +133,6 @@ int main( int argc, char *argv[] ) if( ( f = fopen( filename, "wb+" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not create %s\n\n", filename ); goto exit; } From 1e7059fedd6d397f1ee09f5d0bb53c2148b5d6e0 Mon Sep 17 00:00:00 2001 From: Kevin Luty Date: Wed, 21 Mar 2018 10:01:38 -0500 Subject: [PATCH 2/5] Adding requested changes --- programs/pkey/pk_sign.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 5cc190eee..dc5641805 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -100,7 +100,7 @@ int main( int argc, char *argv[] ) if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) { - mbedtls_printf( " failed\n ! Could not open '%s'\n", argv[1] ); + mbedtls_printf( " failed\n ! Could not parse '%s'\n", argv[1] ); goto exit; } @@ -133,6 +133,7 @@ int main( int argc, char *argv[] ) if( ( f = fopen( filename, "wb+" ) ) == NULL ) { + ret = 1; mbedtls_printf( " failed\n ! Could not create %s\n\n", filename ); goto exit; } From d9d5c554382306b9b9f856746d253c2e3b742ea2 Mon Sep 17 00:00:00 2001 From: Kevin Luty Date: Thu, 22 Mar 2018 09:56:26 -0500 Subject: [PATCH 3/5] Assign error return value for failed write --- programs/pkey/pk_sign.c | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index dc5641805..06ad3ee22 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -140,6 +140,7 @@ int main( int argc, char *argv[] ) if( fwrite( buf, 1, olen, f ) != olen ) { + ret = 1; mbedtls_printf( "failed\n ! fwrite failed\n\n" ); fclose( f ); goto exit; From 0cbe816bfc8ba361d25920d09114eb8bc3ffb99a Mon Sep 17 00:00:00 2001 From: Kevin Luty Date: Mon, 2 Apr 2018 10:01:16 -0500 Subject: [PATCH 4/5] ChangeLog updated and returning proper value --- ChangeLog | 2 ++ programs/pkey/pk_sign.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index e7abd5ce6..8d9ffd6ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,8 @@ Bugfix * Fix leap year calculation in x509_date_is_valid() to ensure that invalid dates on leap years with 100 and 400 intervals are handled correctly. Found by Nicholas Wilson. #694 + * Fix overriding and ignoring return values when parsing and writing to + a file in pk_sign program. Found by kevlut in #1142. = mbed TLS 2.6.0 branch released 2017-08-10 diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 06ad3ee22..1d97be757 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -168,7 +168,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( ret ? EXIT_FAILURE : EXIT_SUCCESS ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && From b364053a8718bb76e364dfe3df01e0f4d3eab97a Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Tue, 3 Apr 2018 06:16:04 -0400 Subject: [PATCH 5/5] pk_sign: add stdlib include --- programs/pkey/pk_sign.c | 1 + 1 file changed, 1 insertion(+) diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 1d97be757..55df95e49 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -29,6 +29,7 @@ #include "mbedtls/platform.h" #else #include +#include #define mbedtls_snprintf snprintf #define mbedtls_printf printf #endif