From 37e7db23d356f4eac95e548feb556cdd864d972d Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 5 Mar 2019 16:10:27 +0000 Subject: [PATCH] ssl_client2: Fail gracefully if no PEM-encoded CRTs are available --- programs/ssl/ssl_client2.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 94230b842..c63c4f75a 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -1085,7 +1085,7 @@ int main( int argc, char *argv[] ) ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file ); else #endif -#if defined(MBEDTLS_CERTS_C) +#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C) for( i = 0; mbedtls_test_cas[i] != NULL; i++ ) { ret = mbedtls_x509_crt_parse( &cacert, @@ -1097,9 +1097,13 @@ int main( int argc, char *argv[] ) #else { ret = 1; - mbedtls_printf("MBEDTLS_CERTS_C not defined."); +#if !defined(MBEDTLS_CERTS_C) + mbedtls_printf( "MBEDTLS_CERTS_C not defined." ); +#else + mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." ); } -#endif +#endif /* MBEDTLS_CERTS_C */ +#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ if( ret < 0 ) { mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); @@ -1124,15 +1128,19 @@ int main( int argc, char *argv[] ) ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file ); else #endif -#if defined(MBEDTLS_CERTS_C) +#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C) ret = mbedtls_x509_crt_parse( &clicert, (const unsigned char *) mbedtls_test_cli_crt, mbedtls_test_cli_crt_len ); #else { ret = 1; - mbedtls_printf("MBEDTLS_CERTS_C not defined."); +#if !defined(MBEDTLS_CERTS_C) + mbedtls_printf( "MBEDTLS_CERTS_C not defined." ); +#else + mbedtls_printf( "All test CRTs loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." ); } -#endif +#endif /* MBEDTLS_CERTS_C */ +#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", -ret ); @@ -1147,15 +1155,19 @@ int main( int argc, char *argv[] ) ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" ); else #endif -#if defined(MBEDTLS_CERTS_C) +#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_PEM_PARSE_C) ret = mbedtls_pk_parse_key( &pkey, (const unsigned char *) mbedtls_test_cli_key, mbedtls_test_cli_key_len, NULL, 0 ); #else { ret = 1; - mbedtls_printf("MBEDTLS_CERTS_C not defined."); +#if !defined(MBEDTLS_CERTS_C) + mbedtls_printf( "MBEDTLS_CERTS_C not defined." ); +#else + mbedtls_printf( "All test keys loaded via MBEDTLS_CERTS_C are PEM-encoded, but MBEDTLS_PEM_PARSE_C is disabled." ); } -#endif +#endif /* MBEDTLS_CERTS_C */ +#endif /* MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C */ if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n", -ret );