diff --git a/programs/aes/aescrypt2.c b/programs/aes/aescrypt2.c index 4acf38dd7..c727f936e 100644 --- a/programs/aes/aescrypt2.c +++ b/programs/aes/aescrypt2.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #include "mbedtls/aes.h" #include "mbedtls/md.h" @@ -71,7 +74,8 @@ int main( void ) #else int main( int argc, char *argv[] ) { - int ret = 1; + int ret = 0; + int exit_code = MBEDTLS_EXIT_FAILURE; unsigned int i, n; int mode, lastn; @@ -429,7 +433,7 @@ int main( int argc, char *argv[] ) } } - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: if( fin ) @@ -452,6 +456,6 @@ exit: mbedtls_aes_free( &aes_ctx ); mbedtls_md_free( &sha_ctx ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */ diff --git a/programs/aes/crypt_and_hash.c b/programs/aes/crypt_and_hash.c index 0e272ebe4..99d30c9a9 100644 --- a/programs/aes/crypt_and_hash.c +++ b/programs/aes/crypt_and_hash.c @@ -30,9 +30,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \ defined(MBEDTLS_FS_IO) @@ -74,6 +77,7 @@ int main( void ) int main( int argc, char *argv[] ) { int ret = 1, i, n; + int exit_code = MBEDTLS_EXIT_FAILURE; int mode; size_t keylen, ilen, olen; FILE *fkey, *fin = NULL, *fout = NULL; @@ -526,7 +530,7 @@ int main( int argc, char *argv[] ) } } - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: if( fin ) @@ -549,6 +553,6 @@ exit: mbedtls_cipher_free( &cipher_ctx ); mbedtls_md_free( &md_ctx ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */ diff --git a/programs/hash/generic_sum.c b/programs/hash/generic_sum.c index d1e81d491..bbe8d92a2 100644 --- a/programs/hash/generic_sum.c +++ b/programs/hash/generic_sum.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/md.h" @@ -169,7 +172,8 @@ static int generic_check( const mbedtls_md_info_t *md_info, char *filename ) int main( int argc, char *argv[] ) { - int ret, i; + int ret = 1, i; + int exit_code = MBEDTLS_EXIT_FAILURE; const mbedtls_md_info_t *md_info; mbedtls_md_context_t md_ctx; @@ -196,7 +200,7 @@ int main( int argc, char *argv[] ) fflush( stdout ); getchar(); #endif - return( 1 ); + return( exit_code ); } /* @@ -206,12 +210,12 @@ int main( int argc, char *argv[] ) if( md_info == NULL ) { mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] ); - return( 1 ); + return( exit_code ); } if( mbedtls_md_setup( &md_ctx, md_info, 0 ) ) { mbedtls_fprintf( stderr, "Failed to initialize context.\n" ); - return( 1 ); + return( exit_code ); } ret = 0; @@ -224,9 +228,12 @@ int main( int argc, char *argv[] ) for( i = 2; i < argc; i++ ) ret |= generic_print( md_info, argv[i] ); + if ( ret == 0 ) + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_md_free( &md_ctx ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/dh_client.c b/programs/pkey/dh_client.c index 0978408c1..3dadf48e6 100644 --- a/programs/pkey/dh_client.c +++ b/programs/pkey/dh_client.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#define mbedtls_time_t time_t -#endif +#include +#define mbedtls_printf printf +#define mbedtls_time_t time_t +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \ @@ -71,7 +74,8 @@ int main( void ) { FILE *f; - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t n, buflen; mbedtls_net_context server_fd; @@ -115,7 +119,6 @@ int main( void ) if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; @@ -191,7 +194,6 @@ int main( void ) if( dhm.len < 64 || dhm.len > 512 ) { - ret = 1; mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" ); goto exit; } @@ -207,7 +209,6 @@ int main( void ) if( ( n = (size_t) ( end - p ) ) != rsa.len ) { - ret = 1; mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" ); goto exit; } @@ -286,6 +287,8 @@ int main( void ) buf[16] = '\0'; mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_net_free( &server_fd ); @@ -301,7 +304,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && diff --git a/programs/pkey/dh_genprime.c b/programs/pkey/dh_genprime.c index 84a94a18b..dbe915338 100644 --- a/programs/pkey/dh_genprime.c +++ b/programs/pkey/dh_genprime.c @@ -30,9 +30,11 @@ #else #include #include -#define mbedtls_printf printf -#define mbedtls_time_t time_t -#endif +#define mbedtls_printf printf +#define mbedtls_time_t time_t +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ @@ -69,6 +71,7 @@ int main( void ) int main( int argc, char **argv ) { int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_mpi G, P, Q; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -86,7 +89,7 @@ int main( int argc, char **argv ) { usage: mbedtls_printf( USAGE ); - return( 1 ); + return( exit_code ); } for( i = 1; i < argc; i++ ) @@ -164,7 +167,6 @@ int main( int argc, char **argv ) if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" ); goto exit; } @@ -180,6 +182,8 @@ int main( int argc, char **argv ) mbedtls_printf( " ok\n\n" ); fclose( fout ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); @@ -191,7 +195,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */ diff --git a/programs/pkey/dh_server.c b/programs/pkey/dh_server.c index 430423154..c4e2c391e 100644 --- a/programs/pkey/dh_server.c +++ b/programs/pkey/dh_server.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#define mbedtls_time_t time_t -#endif +#include +#define mbedtls_printf printf +#define mbedtls_time_t time_t +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \ @@ -71,7 +74,8 @@ int main( void ) { FILE *f; - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t n, buflen; mbedtls_net_context listen_fd, client_fd; @@ -121,7 +125,6 @@ int main( void ) if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; @@ -164,7 +167,6 @@ int main( void ) if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \ " ! Please run dh_genprime first\n\n" ); goto exit; @@ -304,6 +306,8 @@ int main( void ) mbedtls_printf( "\n\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); @@ -323,7 +327,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && diff --git a/programs/pkey/ecdh_curve25519.c b/programs/pkey/ecdh_curve25519.c index e7ead9a93..5db04088f 100644 --- a/programs/pkey/ecdh_curve25519.c +++ b/programs/pkey/ecdh_curve25519.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_ECDH_C) || \ !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ @@ -51,7 +54,8 @@ int main( void ) int main( int argc, char *argv[] ) { - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_ecdh_context ctx_cli, ctx_srv; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -218,6 +222,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; exit: @@ -231,7 +236,7 @@ exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); - return( ret != 0 ); + return( exit_code ); } #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/ecdsa.c b/programs/pkey/ecdsa.c index b47406010..c653df9e4 100644 --- a/programs/pkey/ecdsa.c +++ b/programs/pkey/ecdsa.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_ECDSA_C) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) @@ -98,7 +101,8 @@ static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key ) int main( int argc, char *argv[] ) { - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_ecdsa_context ctx_sign, ctx_verify; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -115,7 +119,6 @@ int main( int argc, char *argv[] ) memset( sig, 0, sizeof( sig ) ); memset( message, 0x25, sizeof( message ) ); - ret = 1; if( argc != 1 ) { @@ -213,8 +216,6 @@ int main( int argc, char *argv[] ) goto exit; } - ret = 0; - /* * Verify signature */ @@ -231,6 +232,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: #if defined(_WIN32) @@ -243,7 +246,7 @@ exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && ECPARAMS */ diff --git a/programs/pkey/gen_key.c b/programs/pkey/gen_key.c index a7f5c90a6..f01bf5fcd 100644 --- a/programs/pkey/gen_key.c +++ b/programs/pkey/gen_key.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) @@ -186,7 +189,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file ) int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_pk_context key; char buf[1024]; int i; @@ -214,7 +218,6 @@ int main( int argc, char *argv[] ) if( argc == 0 ) { usage: - ret = 1; mbedtls_printf( USAGE ); #if defined(MBEDTLS_ECP_C) mbedtls_printf( " available ec_curve values:\n" ); @@ -222,7 +225,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( " %s (default)\n", curve_info->name ); while( ( ++curve_info )->name != NULL ) mbedtls_printf( " %s\n", curve_info->name ); -#endif +#endif /* MBEDTLS_ECP_C */ goto exit; } @@ -411,9 +414,11 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: - if( ret != 0 && ret != 1) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { #ifdef MBEDTLS_ERROR_C mbedtls_strerror( ret, buf, sizeof( buf ) ); @@ -436,7 +441,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO && * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index f1b548d05..7a4cb3976 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && \ defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) @@ -83,7 +86,8 @@ struct options int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; char buf[1024]; int i; char *p, *q; @@ -283,10 +287,12 @@ int main( int argc, char *argv[] ) else goto usage; + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: #if defined(MBEDTLS_ERROR_C) - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_strerror( ret, buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); @@ -303,6 +309,6 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/key_app_writer.c b/programs/pkey/key_app_writer.c index 5f7d2ddb5..13602c2e5 100644 --- a/programs/pkey/key_app_writer.c +++ b/programs/pkey/key_app_writer.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/error.h" @@ -189,7 +192,8 @@ static int write_private_key( mbedtls_pk_context *key, const char *output_file ) int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; char buf[1024]; int i; char *p, *q; @@ -210,7 +214,6 @@ int main( int argc, char *argv[] ) if( argc == 0 ) { usage: - ret = 1; mbedtls_printf( USAGE ); goto exit; } @@ -403,9 +406,11 @@ int main( int argc, char *argv[] ) write_private_key( &key, opt.output_file ); } + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: - if( ret != 0 && ret != 1) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { #ifdef MBEDTLS_ERROR_C mbedtls_strerror( ret, buf, sizeof( buf ) ); @@ -426,6 +431,6 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/mpi_demo.c b/programs/pkey/mpi_demo.c index afe8957e7..365bdc480 100644 --- a/programs/pkey/mpi_demo.c +++ b/programs/pkey/mpi_demo.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/bignum.h" @@ -47,7 +50,8 @@ int main( void ) #else int main( void ) { - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_mpi E, P, Q, N, H, D, X, Y, Z; mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N ); @@ -88,15 +92,16 @@ int main( void ) MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) ); mbedtls_printf( "\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + cleanup: mbedtls_mpi_free( &E ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &D ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_printf( "\nAn error occurred.\n" ); - ret = 1; } #if defined(_WIN32) @@ -104,6 +109,6 @@ cleanup: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/pk_decrypt.c b/programs/pkey/pk_decrypt.c index 32fbc7545..00bd71ed3 100644 --- a/programs/pkey/pk_decrypt.c +++ b/programs/pkey/pk_decrypt.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \ defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \ @@ -59,7 +62,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int ret, c; + int ret = 1, c; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i, olen = 0; mbedtls_pk_context pk; mbedtls_entropy_context entropy; @@ -71,7 +75,6 @@ int main( int argc, char *argv[] ) mbedtls_ctr_drbg_init( &ctr_drbg ); memset(result, 0, sizeof( result ) ); - ret = 1; if( argc != 2 ) { @@ -110,8 +113,6 @@ int main( int argc, char *argv[] ) /* * Extract the RSA encrypted value from the text file */ - ret = 1; - if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) { mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); @@ -143,14 +144,14 @@ int main( int argc, char *argv[] ) mbedtls_printf( "The decrypted result is: '%s'\n\n", result ); - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); #if defined(MBEDTLS_ERROR_C) - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); @@ -162,7 +163,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/pk_encrypt.c b/programs/pkey/pk_encrypt.c index 7ca9d5ad9..400619c5c 100644 --- a/programs/pkey/pk_encrypt.c +++ b/programs/pkey/pk_encrypt.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \ @@ -59,7 +62,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i, olen = 0; mbedtls_pk_context pk; mbedtls_entropy_context entropy; @@ -68,7 +72,6 @@ int main( int argc, char *argv[] ) unsigned char buf[512]; const char *pers = "mbedtls_pk_encrypt"; - ret = 1; mbedtls_ctr_drbg_init( &ctr_drbg ); if( argc != 3 ) @@ -132,7 +135,6 @@ int main( int argc, char *argv[] ) */ if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); goto exit; } @@ -145,12 +147,14 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); #if defined(MBEDTLS_ERROR_C) - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); @@ -162,7 +166,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 55df95e49..7ec46752a 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -30,9 +30,11 @@ #else #include #include -#define mbedtls_snprintf snprintf -#define mbedtls_printf printf -#endif +#define mbedtls_snprintf snprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \ @@ -61,6 +63,7 @@ int main( int argc, char *argv[] ) { FILE *f; int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_pk_context pk; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -134,14 +137,12 @@ 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; } if( fwrite( buf, 1, olen, f ) != olen ) { - ret = 1; mbedtls_printf( "failed\n ! fwrite failed\n\n" ); fclose( f ); goto exit; @@ -151,13 +152,15 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_pk_free( &pk ); mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); #if defined(MBEDTLS_ERROR_C) - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); @@ -169,7 +172,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ? EXIT_FAILURE : EXIT_SUCCESS ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && diff --git a/programs/pkey/pk_verify.c b/programs/pkey/pk_verify.c index d35d17f69..3c7709f9d 100644 --- a/programs/pkey/pk_verify.c +++ b/programs/pkey/pk_verify.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_snprintf snprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_snprintf snprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \ !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \ @@ -56,6 +59,7 @@ int main( int argc, char *argv[] ) { FILE *f; int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i; mbedtls_pk_context pk; unsigned char hash[32]; @@ -87,7 +91,6 @@ int main( int argc, char *argv[] ) /* * Extract the signature from the file */ - ret = 1; mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) @@ -125,13 +128,13 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . OK (the signature is valid)\n\n" ); - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: mbedtls_pk_free( &pk ); #if defined(MBEDTLS_ERROR_C) - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); @@ -143,7 +146,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/rsa_decrypt.c b/programs/pkey/rsa_decrypt.c index 2da3fbf11..0a252d2ad 100644 --- a/programs/pkey/rsa_decrypt.c +++ b/programs/pkey/rsa_decrypt.c @@ -30,11 +30,11 @@ #else #include #include -#define mbedtls_printf printf -#define mbedtls_exit exit +#define mbedtls_printf printf +#define mbedtls_exit exit #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE -#endif +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \ defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \ @@ -61,7 +61,9 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int return_val, exit_val, c; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; + int c; size_t i; mbedtls_rsa_context rsa; mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; @@ -73,7 +75,6 @@ int main( int argc, char *argv[] ) ((void) argv); memset(result, 0, sizeof( result ) ); - exit_val = MBEDTLS_EXIT_SUCCESS; if( argc != 1 ) { @@ -83,7 +84,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n" ); #endif - mbedtls_exit( MBEDTLS_EXIT_FAILURE ); + mbedtls_exit( exit_code ); } mbedtls_printf( "\n . Seeding the random number generator..." ); @@ -96,14 +97,13 @@ int main( int argc, char *argv[] ) mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); - return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, + ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ); - if( return_val != 0 ) + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", - return_val ); + ret ); goto exit; } @@ -112,40 +112,38 @@ int main( int argc, char *argv[] ) if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; } - if( ( return_val = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 ) + if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", - return_val ); + ret ); fclose( f ); goto exit; } fclose( f ); - if( ( return_val = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ) + if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n", - return_val ); + ret ); goto exit; } - if( ( return_val = mbedtls_rsa_complete( &rsa ) ) != 0 ) + if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n", - return_val ); + ret ); goto exit; } @@ -154,7 +152,6 @@ int main( int argc, char *argv[] ) */ if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); goto exit; } @@ -169,7 +166,6 @@ int main( int argc, char *argv[] ) if( i != rsa.len ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( "\n ! Invalid RSA signature format\n\n" ); goto exit; } @@ -180,14 +176,13 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Decrypting the encrypted data" ); fflush( stdout ); - return_val = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, + ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, MBEDTLS_RSA_PRIVATE, &i, buf, result, 1024 ); - if( return_val != 0 ) + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n", - return_val ); + ret ); goto exit; } @@ -195,6 +190,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "The decrypted result is: '%s'\n\n", result ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); @@ -208,6 +205,6 @@ exit: fflush( stdout ); getchar(); #endif - return( exit_val ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/rsa_encrypt.c b/programs/pkey/rsa_encrypt.c index 81c27d888..411657a07 100644 --- a/programs/pkey/rsa_encrypt.c +++ b/programs/pkey/rsa_encrypt.c @@ -30,12 +30,12 @@ #else #include #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#define mbedtls_exit exit -#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS -#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE -#endif +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define mbedtls_exit exit +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \ defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \ @@ -61,7 +61,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int return_val, exit_val; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i; mbedtls_rsa_context rsa; mbedtls_entropy_context entropy; @@ -71,8 +72,6 @@ int main( int argc, char *argv[] ) const char *pers = "rsa_encrypt"; mbedtls_mpi N, E; - exit_val = MBEDTLS_EXIT_SUCCESS; - if( argc != 2 ) { mbedtls_printf( "usage: rsa_encrypt \n" ); @@ -81,7 +80,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n" ); #endif - mbedtls_exit( MBEDTLS_EXIT_FAILURE ); + mbedtls_exit( exit_code ); } mbedtls_printf( "\n . Seeding the random number generator..." ); @@ -92,14 +91,13 @@ int main( int argc, char *argv[] ) mbedtls_ctr_drbg_init( &ctr_drbg ); mbedtls_entropy_init( &entropy ); - return_val = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, - &entropy, (const unsigned char *) pers, - strlen( pers ) ); - if( return_val != 0 ) + ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, + &entropy, (const unsigned char *) pers, + strlen( pers ) ); + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", - return_val ); + ret ); goto exit; } @@ -108,35 +106,30 @@ int main( int argc, char *argv[] ) if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; } - if( ( return_val = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 || - ( return_val = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 ) + if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 || + ( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", - return_val ); + ret ); fclose( f ); goto exit; } fclose( f ); - if( ( return_val = mbedtls_rsa_import( &rsa, &N, NULL, - NULL, NULL, &E ) ) != 0 ) + if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n", - return_val ); + ret ); goto exit; } if( strlen( argv[1] ) > 100 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " Input data larger than 100 characters.\n\n" ); goto exit; } @@ -149,14 +142,13 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Generating the RSA encrypted value" ); fflush( stdout ); - return_val = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random, - &ctr_drbg, MBEDTLS_RSA_PUBLIC, - strlen( argv[1] ), input, buf ); - if( return_val != 0 ) + ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random, + &ctr_drbg, MBEDTLS_RSA_PUBLIC, + strlen( argv[1] ), input, buf ); + if( ret != 0 ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n", - return_val ); + ret ); goto exit; } @@ -165,7 +157,6 @@ int main( int argc, char *argv[] ) */ if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) { - exit_val = MBEDTLS_EXIT_FAILURE; mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); goto exit; } @@ -178,6 +169,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); mbedtls_ctr_drbg_free( &ctr_drbg ); @@ -189,7 +182,7 @@ exit: fflush( stdout ); getchar(); #endif - return( exit_val ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/rsa_genkey.c b/programs/pkey/rsa_genkey.c index 939921761..3359e1407 100644 --- a/programs/pkey/rsa_genkey.c +++ b/programs/pkey/rsa_genkey.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \ defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \ @@ -61,7 +64,8 @@ int main( void ) #else int main( void ) { - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_rsa_context rsa; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -105,14 +109,12 @@ int main( void ) ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 ) { mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); - ret = 1; goto exit; } if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL ) { mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" ); - ret = 1; goto exit; } @@ -129,7 +131,6 @@ int main( void ) if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL ) { mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" ); - ret = 1; goto exit; } @@ -160,6 +161,8 @@ int main( void ) */ mbedtls_printf( " ok\n\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: if( fpub != NULL ) @@ -180,7 +183,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C && MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/rsa_sign.c b/programs/pkey/rsa_sign.c index 89018cb76..b16fe5d22 100644 --- a/programs/pkey/rsa_sign.c +++ b/programs/pkey/rsa_sign.c @@ -29,10 +29,13 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#define mbedtls_snprintf snprintf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define mbedtls_snprintf snprintf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \ @@ -55,7 +58,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i; mbedtls_rsa_context rsa; unsigned char hash[32]; @@ -69,8 +73,6 @@ int main( int argc, char *argv[] ) mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); - ret = 1; - if( argc != 2 ) { mbedtls_printf( "usage: rsa_sign \n" ); @@ -87,7 +89,6 @@ int main( int argc, char *argv[] ) if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) { - ret = 1; mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \ " ! Please run rsa_genkey first\n\n" ); goto exit; @@ -159,7 +160,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", argv[1] ); goto exit; } @@ -172,6 +172,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_rsa_free( &rsa ); @@ -184,7 +186,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/rsa_sign_pss.c b/programs/pkey/rsa_sign_pss.c index 7b6f14dd8..b0b0f7ecf 100644 --- a/programs/pkey/rsa_sign_pss.c +++ b/programs/pkey/rsa_sign_pss.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_snprintf snprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_snprintf snprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \ @@ -61,6 +64,7 @@ int main( int argc, char *argv[] ) { FILE *f; int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_pk_context pk; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -101,7 +105,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 read key from '%s'\n", argv[1] ); mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret ); goto exit; @@ -109,7 +112,6 @@ int main( int argc, char *argv[] ) if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) ) { - ret = 1; mbedtls_printf( " failed\n ! Key is not an RSA key\n" ); goto exit; } @@ -145,7 +147,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; } @@ -161,6 +162,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_pk_free( &pk ); mbedtls_ctr_drbg_free( &ctr_drbg ); @@ -171,7 +174,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && diff --git a/programs/pkey/rsa_verify.c b/programs/pkey/rsa_verify.c index 1f827aa07..6f88345f2 100644 --- a/programs/pkey/rsa_verify.c +++ b/programs/pkey/rsa_verify.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#define mbedtls_snprintf snprintf -#endif +#include +#define mbedtls_printf printf +#define mbedtls_snprintf snprintf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \ @@ -54,7 +57,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int ret, c; + int ret = 1, c; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i; mbedtls_rsa_context rsa; unsigned char hash[32]; @@ -62,7 +66,6 @@ int main( int argc, char *argv[] ) char filename[512]; mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); - ret = 1; if( argc != 2 ) { @@ -100,7 +103,6 @@ int main( int argc, char *argv[] ) /* * Extract the RSA signature from the text file */ - ret = 1; mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) @@ -146,7 +148,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . OK (the signature is valid)\n\n" ); - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: @@ -157,7 +159,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */ diff --git a/programs/pkey/rsa_verify_pss.c b/programs/pkey/rsa_verify_pss.c index 31b720f36..7c9c68f22 100644 --- a/programs/pkey/rsa_verify_pss.c +++ b/programs/pkey/rsa_verify_pss.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_snprintf snprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_snprintf snprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \ @@ -60,6 +63,7 @@ int main( int argc, char *argv[] ) { FILE *f; int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; size_t i; mbedtls_pk_context pk; unsigned char hash[32]; @@ -91,7 +95,6 @@ int main( int argc, char *argv[] ) if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) ) { - ret = 1; mbedtls_printf( " failed\n ! Key is not an RSA key\n" ); goto exit; } @@ -101,7 +104,6 @@ int main( int argc, char *argv[] ) /* * Extract the RSA signature from the file */ - ret = 1; mbedtls_snprintf( filename, 512, "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) @@ -139,7 +141,7 @@ int main( int argc, char *argv[] ) mbedtls_printf( "\n . OK (the signature is valid)\n\n" ); - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; exit: mbedtls_pk_free( &pk ); @@ -149,7 +151,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ diff --git a/programs/random/gen_entropy.c b/programs/random/gen_entropy.c index 792d3818a..a1eb3868a 100644 --- a/programs/random/gen_entropy.c +++ b/programs/random/gen_entropy.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/entropy.h" @@ -49,20 +52,21 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int i, k, ret; + int i, k, ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_entropy_context entropy; unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; if( argc < 2 ) { mbedtls_fprintf( stderr, "usage: %s \n", argv[0] ); - return( 1 ); + return( exit_code ); } if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) { mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] ); - return( 1 ); + return( exit_code ); } mbedtls_entropy_init( &entropy ); @@ -72,7 +76,8 @@ int main( int argc, char *argv[] ) ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) ); if( ret != 0 ) { - mbedtls_printf("failed!\n"); + mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n", + ret ); goto cleanup; } @@ -83,7 +88,7 @@ int main( int argc, char *argv[] ) fflush( stdout ); } - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; cleanup: mbedtls_printf( "\n" ); @@ -91,6 +96,6 @@ cleanup: fclose( f ); mbedtls_entropy_free( &entropy ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_ENTROPY_C */ diff --git a/programs/random/gen_random_ctr_drbg.c b/programs/random/gen_random_ctr_drbg.c index c76f99d09..5ade946a7 100644 --- a/programs/random/gen_random_ctr_drbg.c +++ b/programs/random/gen_random_ctr_drbg.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \ defined(MBEDTLS_FS_IO) @@ -52,7 +55,8 @@ int main( void ) int main( int argc, char *argv[] ) { FILE *f; - int i, k, ret; + int i, k, ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_entropy_context entropy; unsigned char buf[1024]; @@ -62,13 +66,13 @@ int main( int argc, char *argv[] ) if( argc < 2 ) { mbedtls_fprintf( stderr, "usage: %s \n", argv[0] ); - return( 1 ); + return( exit_code ); } if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) { mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] ); - return( 1 ); + return( exit_code ); } mbedtls_entropy_init( &entropy ); @@ -116,7 +120,7 @@ int main( int argc, char *argv[] ) fflush( stdout ); } - ret = 0; + exit_code = MBEDTLS_EXIT_SUCCESS; cleanup: mbedtls_printf("\n"); @@ -125,6 +129,6 @@ cleanup: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */ diff --git a/programs/random/gen_random_havege.c b/programs/random/gen_random_havege.c index 6c3146265..3fb3f0196 100644 --- a/programs/random/gen_random_havege.c +++ b/programs/random/gen_random_havege.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/havege.h" @@ -51,20 +54,21 @@ int main( int argc, char *argv[] ) { FILE *f; time_t t; - int i, k, ret = 0; + int i, k, ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_havege_state hs; unsigned char buf[1024]; if( argc < 2 ) { mbedtls_fprintf( stderr, "usage: %s \n", argv[0] ); - return( 1 ); + return( exit_code ); } if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) { mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] ); - return( 1 ); + return( exit_code ); } mbedtls_havege_init( &hs ); @@ -73,11 +77,10 @@ int main( int argc, char *argv[] ) for( i = 0, k = 768; i < k; i++ ) { - if( mbedtls_havege_random( &hs, buf, sizeof( buf ) ) != 0 ) + if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 ) { - mbedtls_printf( "Failed to get random from source.\n" ); - - ret = 1; + mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X", + -ret ); goto exit; } @@ -93,9 +96,11 @@ int main( int argc, char *argv[] ) mbedtls_printf(" \n "); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_havege_free( &hs ); fclose( f ); - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_HAVEGE_C */ diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c index 01cee1354..bf7c0132a 100644 --- a/programs/ssl/ssl_client1.c +++ b/programs/ssl/ssl_client1.c @@ -30,11 +30,13 @@ #else #include #include -#define mbedtls_time time -#define mbedtls_time_t time_t -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#define mbedtls_time time +#define mbedtls_time_t time_t +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ @@ -80,7 +82,8 @@ static void my_debug( void *ctx, int level, int main( void ) { - int ret, len; + int ret = 1, len; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_net_context server_fd; uint32_t flags; unsigned char buf[1024]; @@ -281,10 +284,12 @@ int main( void ) mbedtls_ssl_close_notify( &ssl ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: #ifdef MBEDTLS_ERROR_C - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { char error_buf[100]; mbedtls_strerror( ret, error_buf, 100 ); @@ -305,7 +310,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c index 7624896a3..1c3a80600 100644 --- a/programs/ssl/ssl_fork_server.c +++ b/programs/ssl/ssl_fork_server.c @@ -29,10 +29,13 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#define mbedtls_time_t time_t -#endif +#include +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define mbedtls_time_t time_t +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) || \ !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \ @@ -95,7 +98,8 @@ static void my_debug( void *ctx, int level, int main( void ) { - int ret, len, cnt = 0, pid; + int ret = 1, len, cnt = 0, pid; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_net_context listen_fd, client_fd; unsigned char buf[1024]; const char *pers = "ssl_fork_server"; @@ -392,6 +396,8 @@ int main( void ) goto exit; } + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_net_free( &client_fd ); mbedtls_net_free( &listen_fd ); @@ -408,7 +414,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C && diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c index 04b847a69..04f891081 100644 --- a/programs/ssl/ssl_mail_client.c +++ b/programs/ssl/ssl_mail_client.c @@ -30,11 +30,13 @@ #else #include #include -#define mbedtls_time time -#define mbedtls_time_t time_t -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#define mbedtls_time time +#define mbedtls_time_t time_t +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ @@ -346,7 +348,8 @@ static int write_and_get_response( mbedtls_net_context *sock_fd, unsigned char * int main( int argc, char *argv[] ) { - int ret = 0, len; + int ret = 1, len; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_net_context server_fd; unsigned char buf[1024]; #if defined(MBEDTLS_BASE64_C) @@ -499,8 +502,8 @@ int main( int argc, char *argv[] ) mbedtls_test_cas_pem_len ); #else { - ret = 1; mbedtls_printf("MBEDTLS_CERTS_C and/or MBEDTLS_PEM_PARSE_C not defined."); + goto exit; } #endif if( ret < 0 ) @@ -529,8 +532,8 @@ int main( int argc, char *argv[] ) mbedtls_test_cli_crt_len ); #else { - ret = -1; mbedtls_printf("MBEDTLS_CERTS_C not defined."); + goto exit; } #endif if( ret != 0 ) @@ -549,8 +552,8 @@ int main( int argc, char *argv[] ) mbedtls_test_cli_key_len, NULL, 0 ); #else { - ret = -1; mbedtls_printf("MBEDTLS_CERTS_C or MBEDTLS_PEM_PARSE_C not defined."); + goto exit; } #endif if( ret != 0 ) @@ -819,6 +822,8 @@ int main( int argc, char *argv[] ) mbedtls_ssl_close_notify( &ssl ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_net_free( &server_fd ); @@ -835,7 +840,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C ** diff --git a/programs/test/ssl_cert_test.c b/programs/test/ssl_cert_test.c index 9cfcd2d55..fd3526f7f 100644 --- a/programs/test/ssl_cert_test.c +++ b/programs/test/ssl_cert_test.c @@ -29,9 +29,12 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_snprintf snprintf -#define mbedtls_printf printf -#endif +#include +#define mbedtls_snprintf snprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_X509_CRT_PARSE_C) && \ defined(MBEDTLS_FS_IO) && defined(MBEDTLS_X509_CRL_PARSE_C) @@ -80,7 +83,8 @@ const char *client_private_keys[MAX_CLIENT_CERTS] = int main( void ) { - int ret, i; + int ret = 1, i; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_x509_crt cacert; mbedtls_x509_crl crl; char buf[10240]; @@ -210,7 +214,6 @@ int main( void ) if( ! mbedtls_pk_can_do( &clicert.pk, MBEDTLS_PK_RSA ) ) { mbedtls_printf( " failed\n ! certificate's key is not RSA\n\n" ); - ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; goto exit; } @@ -241,6 +244,8 @@ int main( void ) mbedtls_pk_free( &pk ); } + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_x509_crt_free( &cacert ); mbedtls_x509_crl_free( &crl ); @@ -250,7 +255,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_RSA_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_X509_CRL_PARSE_C */ diff --git a/programs/test/udp_proxy.c b/programs/test/udp_proxy.c index 20624d227..1865b0f1b 100644 --- a/programs/test/udp_proxy.c +++ b/programs/test/udp_proxy.c @@ -37,10 +37,12 @@ #include #include #include -#define mbedtls_time time -#define mbedtls_time_t time_t -#define mbedtls_printf printf -#endif +#define mbedtls_time time +#define mbedtls_time_t time_t +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_NET_C) int main( void ) @@ -468,7 +470,8 @@ int handle_message( const char *way, int main( int argc, char *argv[] ) { - int ret; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_net_context listen_fd, client_fd, server_fd; @@ -591,10 +594,12 @@ accept: } } + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: #ifdef MBEDTLS_ERROR_C - if( ret != 0 ) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { char error_buf[100]; mbedtls_strerror( ret, error_buf, 100 ); @@ -612,7 +617,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret != 0 ); + return( exit_code ); } #endif /* MBEDTLS_NET_C */ diff --git a/programs/util/pem2der.c b/programs/util/pem2der.c index ad2c6ac13..73a9fb5e0 100644 --- a/programs/util/pem2der.c +++ b/programs/util/pem2der.c @@ -29,10 +29,13 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_free free -#define mbedtls_calloc calloc -#define mbedtls_printf printf -#endif +#include +#define mbedtls_free free +#define mbedtls_calloc calloc +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/error.h" @@ -178,7 +181,8 @@ static int write_file( const char *path, unsigned char *buf, size_t n ) int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; unsigned char *pem_buffer = NULL; unsigned char der_buffer[4096]; char buf[1024]; @@ -273,6 +277,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: free( pem_buffer ); @@ -281,6 +287,6 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */ diff --git a/programs/x509/cert_app.c b/programs/x509/cert_app.c index c893ca8de..c57ecca03 100644 --- a/programs/x509/cert_app.c +++ b/programs/x509/cert_app.c @@ -30,11 +30,13 @@ #else #include #include -#define mbedtls_time time -#define mbedtls_time_t time_t -#define mbedtls_fprintf fprintf -#define mbedtls_printf printf -#endif +#define mbedtls_time time +#define mbedtls_time_t time_t +#define mbedtls_fprintf fprintf +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \ @@ -145,7 +147,8 @@ static int my_verify( void *data, mbedtls_x509_crt *crt, int depth, uint32_t *fl int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_net_context server_fd; unsigned char buf[1024]; mbedtls_entropy_context entropy; @@ -180,7 +183,6 @@ int main( int argc, char *argv[] ) { usage: mbedtls_printf( USAGE ); - ret = 2; goto exit; } @@ -252,19 +254,23 @@ int main( int argc, char *argv[] ) if( strlen( opt.ca_path ) ) { - ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ); + if( ( ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path ) ) < 0 ) + { + mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n", -ret ); + goto exit; + } + verify = 1; } else if( strlen( opt.ca_file ) ) { - ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ); - verify = 1; - } + if( ( ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ) ) < 0 ) + { + mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n", -ret ); + goto exit; + } - if( ret < 0 ) - { - mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); - goto exit; + verify = 1; } mbedtls_printf( " ok (%d skipped)\n", ret ); @@ -332,8 +338,6 @@ int main( int argc, char *argv[] ) cur = cur->next; } - ret = 0; - /* * 1.3 Verify the certificate */ @@ -470,6 +474,8 @@ ssl_exit: else goto usage; + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_net_free( &server_fd ); @@ -485,10 +491,7 @@ exit: fflush( stdout ); getchar(); #endif - if( ret < 0 ) - ret = 1; - - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C && diff --git a/programs/x509/cert_req.c b/programs/x509/cert_req.c index 30df2162a..a32ac505f 100644 --- a/programs/x509/cert_req.c +++ b/programs/x509/cert_req.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \ !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \ @@ -133,7 +136,8 @@ int write_certificate_request( mbedtls_x509write_csr *req, const char *output_fi int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_pk_context key; char buf[1024]; int i; @@ -156,7 +160,6 @@ int main( int argc, char *argv[] ) { usage: mbedtls_printf( USAGE ); - ret = 1; goto exit; } @@ -317,9 +320,11 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: - if( ret != 0 && ret != 1) + if( exit_code != MBEDTLS_EXIT_SUCCESS ) { #ifdef MBEDTLS_ERROR_C mbedtls_strerror( ret, buf, sizeof( buf ) ); @@ -339,7 +344,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */ diff --git a/programs/x509/cert_write.c b/programs/x509/cert_write.c index 9cc582d61..26ab88af9 100644 --- a/programs/x509/cert_write.c +++ b/programs/x509/cert_write.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_X509_CRT_WRITE_C) || \ !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ @@ -211,7 +214,8 @@ int write_certificate( mbedtls_x509write_cert *crt, const char *output_file, int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; mbedtls_x509_crt issuer_crt; mbedtls_pk_context loaded_issuer_key, loaded_subject_key; mbedtls_pk_context *issuer_key = &loaded_issuer_key, @@ -248,7 +252,6 @@ int main( int argc, char *argv[] ) { usage: mbedtls_printf( USAGE ); - ret = 1; goto exit; } @@ -615,7 +618,6 @@ int main( int argc, char *argv[] ) { mbedtls_printf( " failed\n ! issuer_key does not match " "issuer certificate\n\n" ); - ret = -1; goto exit; } } @@ -788,6 +790,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( " ok\n" ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_x509write_crt_free( &crt ); mbedtls_pk_free( &loaded_subject_key ); @@ -801,7 +805,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && diff --git a/programs/x509/crl_app.c b/programs/x509/crl_app.c index 210d19e96..f8316835f 100644 --- a/programs/x509/crl_app.c +++ b/programs/x509/crl_app.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ !defined(MBEDTLS_X509_CRL_PARSE_C) || !defined(MBEDTLS_FS_IO) @@ -67,7 +70,8 @@ struct options int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; unsigned char buf[100000]; mbedtls_x509_crl crl; int i; @@ -131,6 +135,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "%s\n", buf ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_x509_crl_free( &crl ); @@ -139,7 +145,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_FS_IO */ diff --git a/programs/x509/req_app.c b/programs/x509/req_app.c index 8410a5371..0f20c85f5 100644 --- a/programs/x509/req_app.c +++ b/programs/x509/req_app.c @@ -29,8 +29,11 @@ #include "mbedtls/platform.h" #else #include -#define mbedtls_printf printf -#endif +#include +#define mbedtls_printf printf +#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS +#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE +#endif /* MBEDTLS_PLATFORM_C */ #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \ !defined(MBEDTLS_X509_CSR_PARSE_C) || !defined(MBEDTLS_FS_IO) @@ -67,7 +70,8 @@ struct options int main( int argc, char *argv[] ) { - int ret = 0; + int ret = 1; + int exit_code = MBEDTLS_EXIT_FAILURE; unsigned char buf[100000]; mbedtls_x509_csr csr; int i; @@ -131,6 +135,8 @@ int main( int argc, char *argv[] ) mbedtls_printf( "%s\n", buf ); + exit_code = MBEDTLS_EXIT_SUCCESS; + exit: mbedtls_x509_csr_free( &csr ); @@ -139,7 +145,7 @@ exit: fflush( stdout ); getchar(); #endif - return( ret ); + return( exit_code ); } #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_X509_CSR_PARSE_C && MBEDTLS_FS_IO */