From e383fa65d707168142a30859dad75ab15b6af76f Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 27 Apr 2021 14:50:43 +0100 Subject: [PATCH] Move x509_crt_verify_info to ssl_test_common_source.c This function was introduced before ssl_test_common_source.c and so the function is replicated in both ssl_client2.c and ssl_server2.c. Move the function to ssl_test_common_source.c to avoid duplication. Signed-off-by: Chris Jones --- programs/ssl/ssl_client2.c | 36 ------------------------- programs/ssl/ssl_server2.c | 38 --------------------------- programs/ssl/ssl_test_common_source.c | 38 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 74 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 5be162f1e..24c859e38 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -523,42 +523,6 @@ struct options #if defined(MBEDTLS_X509_CRT_PARSE_C) static unsigned char peer_crt_info[1024]; -#if !defined(MBEDTLS_X509_REMOVE_INFO) -int x509_crt_verify_info( char *buf, size_t size, const char *prefix, - uint32_t flags ) -{ - return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); -} -#else /* !MBEDTLS_X509_REMOVE_INFO */ -int x509_crt_verify_info( char *buf, size_t size, const char *prefix, - uint32_t flags ) -{ - int ret; - char *p = buf; - size_t n = size; - -#define X509_CRT_ERROR_INFO( err, err_str, info ) \ - if( ( flags & err ) != 0 ) \ - { \ - ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \ - MBEDTLS_X509_SAFE_SNPRINTF; \ - flags ^= err; \ - } - - MBEDTLS_X509_CRT_ERROR_INFO_LIST -#undef X509_CRT_ERROR_INFO - - if( flags != 0 ) - { - ret = mbedtls_snprintf( p, n, "%sUnknown reason " - "(this should not happen)\n", prefix ); - MBEDTLS_X509_SAFE_SNPRINTF; - } - - return( (int) ( size - n ) ); -} -#endif /* MBEDTLS_X509_REMOVE_INFO */ - /* * Enabled if debug_level > 1 in code below */ diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index dc33381da..8f97541af 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -623,44 +623,6 @@ struct options #include "ssl_test_common_source.c" -#if defined(MBEDTLS_X509_CRT_PARSE_C) -#if !defined(MBEDTLS_X509_REMOVE_INFO) -int x509_crt_verify_info( char *buf, size_t size, const char *prefix, - uint32_t flags ) -{ - return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); -} -#else /* !MBEDTLS_X509_REMOVE_INFO */ -int x509_crt_verify_info( char *buf, size_t size, const char *prefix, - uint32_t flags ) -{ - int ret; - char *p = buf; - size_t n = size; - -#define X509_CRT_ERROR_INFO( err, err_str, info ) \ - if( ( flags & err ) != 0 ) \ - { \ - ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \ - MBEDTLS_X509_SAFE_SNPRINTF; \ - flags ^= err; \ - } - - MBEDTLS_X509_CRT_ERROR_INFO_LIST -#undef X509_CRT_ERROR_INFO - - if( flags != 0 ) - { - ret = mbedtls_snprintf( p, n, "%sUnknown reason " - "(this should not happen)\n", prefix ); - MBEDTLS_X509_SAFE_SNPRINTF; - } - - return( (int) ( size - n ) ); -} -#endif /* MBEDTLS_X509_REMOVE_INFO */ -#endif /* MBEDTLS_X509_CRT_PARSE_C */ - /* * Return authmode from string, or -1 on error */ diff --git a/programs/ssl/ssl_test_common_source.c b/programs/ssl/ssl_test_common_source.c index d9e36078d..35dfa60c2 100644 --- a/programs/ssl/ssl_test_common_source.c +++ b/programs/ssl/ssl_test_common_source.c @@ -303,3 +303,41 @@ int ssl_sig_hashes_for_test[] = { MBEDTLS_MD_NONE }; #endif /* MBEDTLS_X509_CRT_PARSE_C */ + +#if defined(MBEDTLS_X509_CRT_PARSE_C) +#if !defined(MBEDTLS_X509_REMOVE_INFO) +/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function + * for more info. + */ +int x509_crt_verify_info( char *buf, size_t size, const char *prefix, + uint32_t flags ) +{ + return( mbedtls_x509_crt_verify_info( buf, size, prefix, flags ) ); + +#else /* !MBEDTLS_X509_REMOVE_INFO */ + int ret; + char *p = buf; + size_t n = size; + +#define X509_CRT_ERROR_INFO( err, err_str, info ) \ + if( ( flags & err ) != 0 ) \ + { \ + ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, info ); \ + MBEDTLS_X509_SAFE_SNPRINTF; \ + flags ^= err; \ + } + + MBEDTLS_X509_CRT_ERROR_INFO_LIST +#undef X509_CRT_ERROR_INFO + + if( flags != 0 ) + { + ret = mbedtls_snprintf( p, n, "%sUnknown reason " + "(this should not happen)\n", prefix ); + MBEDTLS_X509_SAFE_SNPRINTF; + } + + return( (int) ( size - n ) ); +#endif /* MBEDTLS_X509_REMOVE_INFO */ +} +#endif /* MBEDTLS_X509_CRT_PARSE_C */