diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index db7d76bef..5372c694e 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,6 +198,136 @@ #error "POLARSSL_PKCS11_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_PLATFORM_EXIT_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_EXIT_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_EXIT_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_EXIT) ||\ + defined(POLARSSL_PLATFORM_EXIT_ALT) ) +#error "POLARSSL_PLATFORM_EXIT_MACRO and POLARSSL_PLATFORM_STD_EXIT/POLARSSL_PLATFORM_EXIT_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_FPRINTF) ||\ + defined(POLARSSL_PLATFORM_FPRINTF_ALT) ) +#error "POLARSSL_PLATFORM_FPRINTF_MACRO and POLARSSL_PLATFORM_STD_FPRINTF/POLARSSL_PLATFORM_FPRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) +#error "POLARSSL_PLATFORM_FREE_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ + defined(POLARSSL_PLATFORM_STD_FREE) +#error "POLARSSL_PLATFORM_FREE_MACRO and POLARSSL_PLATFORM_STD_FREE cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_FREE_MACRO) && !defined(POLARSSL_PLATFORM_MALLOC_MACRO) +#error "POLARSSL_PLATFORM_MALLOC_MACRO must be defined if POLARSSL_PLATFORM_FREE_MACRO is" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) +#error "POLARSSL_PLATFORM_MALLOC_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\ + defined(POLARSSL_PLATFORM_STD_MALLOC) +#error "POLARSSL_PLATFORM_MALLOC_MACRO and POLARSSL_PLATFORM_STD_MALLOC cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) && !defined(POLARSSL_PLATFORM_FREE_MACRO) +#error "POLARSSL_PLATFORM_FREE_MACRO must be defined if POLARSSL_PLATFORM_MALLOC_MACRO is" +#endif + +#if defined(POLARSSL_PLATFORM_MEMORY) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_MEMORY defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_PRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_PRINTF) ||\ + defined(POLARSSL_PLATFORM_PRINTF_ALT) ) +#error "POLARSSL_PLATFORM_PRINTF_MACRO and POLARSSL_PLATFORM_STD_PRINTF/POLARSSL_PLATFORM_PRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && ( defined(_WIN32)\ + && !defined(EFIX64) && !defined(EFI32) ) +#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\ + ( defined(POLARSSL_PLATFORM_STD_SNPRINTF) ||\ + defined(POLARSSL_PLATFORM_SNPRINTF_ALT) ) +#error "POLARSSL_PLATFORM_SNPRINTF_MACRO and POLARSSL_PLATFORM_STD_SNPRINTF/POLARSSL_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MEM_HDR) &&\ + !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) +#error "POLARSSL_PLATFORM_STD_MEM_HDR defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_FREE) && !defined(POLARSSL_PLATFORM_MEMORY) +#error "POLARSSL_PLATFORM_STD_FREE defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_EXIT) &&\ + !defined(POLARSSL_PLATFORM_EXIT_ALT) +#error "POLARSSL_PLATFORM_STD_EXIT defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_FPRINTF) &&\ + !defined(POLARSSL_PLATFORM_FPRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_FPRINTF defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_PRINTF) &&\ + !defined(POLARSSL_PLATFORM_PRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_PRINTF defined, but not all prerequisites" +#endif + +#if defined(POLARSSL_PLATFORM_STD_SNPRINTF) &&\ + !defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +#error "POLARSSL_PLATFORM_STD_SNPRINTF defined, but not all prerequisites" +#endif + #if defined(POLARSSL_RSA_C) && ( !defined(POLARSSL_BIGNUM_C) || \ !defined(POLARSSL_OID_C) ) #error "POLARSSL_RSA_C defined, but not all prerequisites" diff --git a/include/polarssl/config.h b/include/polarssl/config.h index 02e8985af..742550852 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -120,8 +120,14 @@ * This allows different allocators (self-implemented or provided) to be * provided to the platform abstraction layer. * - * Enabling POLARSSL_PLATFORM_MEMORY will provide "platform_set_malloc_free()" - * to allow you to set an alternative malloc() and free() function pointer. + * Enabling POLARSSL_PLATFORM_MEMORY without the + * POLARSSL_PLATFORM_{FREE,MALLOC}_MACROs will provide + * "platform_set_malloc_free()" allowing you to set an alternative malloc() and + * free() function pointer at runtime. + * + * Enabling POLARSSL_PLATFORM_MEMORY and specifying + * POLARSSL_PLATFORM_{MALLOC,FREE}_MACROs will allow you to specify the + * alternate function at compile time. * * Requires: POLARSSL_PLATFORM_C * @@ -138,7 +144,8 @@ * This makes sure there are no linking errors on platforms that do not support * these functions. You will HAVE to provide alternatives, either at runtime * via the platform_set_xxx() functions or at compile time by setting - * the POLARSSL_PLATFORM_STD_XXX defines. + * the POLARSSL_PLATFORM_STD_XXX defines, or enabling a + * POLARSSL_PLATFORM_XXX_MACRO. * * Requires: POLARSSL_PLATFORM_C * @@ -159,11 +166,19 @@ * * All these define require POLARSSL_PLATFORM_C to be defined! * + * WARNING: POLARSSL_PLATFORM_SNPRINTF_ALT is not available on Windows + * for compatibility reasons. + * + * WARNING: POLARSSL_PLATFORM_XXX_ALT cannot be defined at the same time as + * POLARSSL_PLATFORM_XXX_MACRO! + * * Uncomment a macro to enable alternate implementation of specific base * platform function */ -//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_EXIT_ALT //#define POLARSSL_PLATFORM_FPRINTF_ALT +//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_SNPRINTF_ALT /* \} name SECTION: System support */ /** @@ -1890,7 +1905,11 @@ * \def POLARSSL_PLATFORM_C * * Enable the platform abstraction layer that allows you to re-assign - * functions like malloc(), free(), printf(), fprintf() + * functions like malloc(), free(), snprintf(), printf(), fprintf(), exit() + * + * Enabling POLARSSL_PLATFORM_C enables to use of POLARSSL_PLATFORM_XXX_ALT + * or POLARSSL_PLATFORM_XXX_MACRO directives, allowing the functions mentioned + * above to be specified at runtime or compile time respectively. * * Module: library/platform.c * Caller: Most other .c files @@ -2235,11 +2254,22 @@ //#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ /* Platform options */ -//#define POLARSSL_PLATFORM_STD_MEM_HDR /**< Header to include if POLARSSL_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ -//#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ -//#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_MEM_HDR /**< Header to include if POLARSSL_PLATFORM_NO_STD_FUNCTIONS is defined. Don't define if no header is needed. */ +//#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */ + +/* To Use Function Macros POLARSSL_PLATFORM_C must be enabled */ +/* POLARSSL_PLATFORM_XXX_MACRO and POLARSSL_PLATFORM_XXX_ALT cannot both be defined */ +//#define POLARSSL_PLATFORM_MALLOC_MACRO malloc /**< Default allocator macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_FREE_MACRO free /**< Default free macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */ +//#define POLARSSL_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */ /* SSL Cache options */ //#define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 127b7fe3e..3e76c301b 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -50,6 +50,9 @@ extern "C" { #if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS) #include #include +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */ +#endif #if !defined(POLARSSL_PLATFORM_STD_PRINTF) #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #endif @@ -62,6 +65,9 @@ extern "C" { #if !defined(POLARSSL_PLATFORM_STD_FREE) #define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */ #endif +#if !defined(POLARSSL_PLATFORM_STD_EXIT) +#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */ +#endif #else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */ #if defined(POLARSSL_PLATFORM_STD_MEM_HDR) #include POLARSSL_PLATFORM_STD_MEM_HDR @@ -74,6 +80,11 @@ extern "C" { * The function pointers for malloc and free */ #if defined(POLARSSL_PLATFORM_MEMORY) +#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ + defined(POLARSSL_PLATFORM_MALLOC_MACRO) +#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO +#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO +#else extern void * (*polarssl_malloc)( size_t len ); extern void (*polarssl_free)( void *ptr ); @@ -87,10 +98,34 @@ extern void (*polarssl_free)( void *ptr ); */ int platform_set_malloc_free( void * (*malloc_func)( size_t ), void (*free_func)( void * ) ); +#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */ #else /* !POLARSSL_PLATFORM_MEMORY */ -#define polarssl_malloc malloc -#define polarssl_free free -#endif /* POLARSSL_PLATFORM_MEMORY */ +#define polarssl_free free +#define polarssl_malloc malloc +#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */ + +/* + * The function pointers for fprintf + */ +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); + +/** + * \brief Set your own fprintf function pointer + * + * \param fprintf_func the fprintf function implementation + * + * \return 0 + */ +int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, + ... ) ); +#else +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) +#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO +#else +#define polarssl_fprintf fprintf +#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */ +#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ /* * The function pointers for printf @@ -106,21 +141,58 @@ extern int (*polarssl_printf)( const char *format, ... ); * \return 0 */ int platform_set_printf( int (*printf_func)( const char *, ... ) ); -#else /* POLARSSL_PLATFORM_PRINTF_ALT */ +#else /* !POLARSSL_PLATFORM_PRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) +#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO +#else #define polarssl_printf printf +#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */ #endif /* POLARSSL_PLATFORM_PRINTF_ALT */ /* - * The function pointers for fprintf + * The function pointers for snprintf */ -#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) -extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); -int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, - ... ) ); +/** + * \brief Set your own snprintf function pointer + * + * \param snprintf_func the snprintf function implementation + * + * \return 0 + */ +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, ... ) ); +#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) +#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO #else -#define polarssl_fprintf fprintf -#endif +#define polarssl_snprintf snprintf +#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */ +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + +/* + * The function pointers for exit + */ +#if defined(POLARSSL_PLATFORM_EXIT_ALT) +extern void (*polarssl_exit)( int status ); + +/** + * \brief Set your own exit function pointer + * + * \param exit_func the exit function implementation + * + * \return 0 + */ +int platform_set_exit( void (*exit_func)( int status ) ); +#else +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) +#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO +#else +#define polarssl_exit exit +#endif /* POLARSSL_PLATFORM_EXIT_MACRO */ +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ #ifdef __cplusplus } diff --git a/library/debug.c b/library/debug.c index f327baab9..88a9dacf5 100644 --- a/library/debug.c +++ b/library/debug.c @@ -44,6 +44,12 @@ #endif #endif /* _MSC_VER */ +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE; static int debug_threshold = 0; @@ -86,7 +92,7 @@ void debug_print_msg( const ssl_context *ssl, int level, return; } - snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); + polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); str[maxlen] = '\0'; ssl->f_dbg( ssl->p_dbg, level, str ); } @@ -103,9 +109,9 @@ void debug_print_ret( const ssl_context *ssl, int level, return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", + polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", text, ret, -ret ); str[maxlen] = '\0'; @@ -124,9 +130,9 @@ void debug_print_buf( const ssl_context *ssl, int level, return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n", + polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n", text, (unsigned int) len ); str[maxlen] = '\0'; @@ -143,7 +149,7 @@ void debug_print_buf( const ssl_context *ssl, int level, { if( i > 0 ) { - snprintf( str + idx, maxlen - idx, " %s\n", txt ); + polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt ); ssl->f_dbg( ssl->p_dbg, level, str ); idx = 0; @@ -151,14 +157,14 @@ void debug_print_buf( const ssl_context *ssl, int level, } if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - idx += snprintf( str + idx, maxlen - idx, "%04x: ", + idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ", (unsigned int) i ); } - idx += snprintf( str + idx, maxlen - idx, " %02x", + idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) buf[i] ); txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; } @@ -166,9 +172,9 @@ void debug_print_buf( const ssl_context *ssl, int level, if( len > 0 ) { for( /* i = i */; i % 16 != 0; i++ ) - idx += snprintf( str + idx, maxlen - idx, " " ); + idx += polarssl_snprintf( str + idx, maxlen - idx, " " ); - snprintf( str + idx, maxlen - idx, " %s\n", txt ); + polarssl_snprintf( str + idx, maxlen - idx, " %s\n", txt ); ssl->f_dbg( ssl->p_dbg, level, str ); } } @@ -184,11 +190,11 @@ void debug_print_ecp( const ssl_context *ssl, int level, if( ssl->f_dbg == NULL || level > debug_threshold ) return; - snprintf( str, maxlen, "%s(X)", text ); + polarssl_snprintf( str, maxlen, "%s(X)", text ); str[maxlen] = '\0'; debug_print_mpi( ssl, level, file, line, str, &X->X ); - snprintf( str, maxlen, "%s(Y)", text ); + polarssl_snprintf( str, maxlen, "%s(Y)", text ); str[maxlen] = '\0'; debug_print_mpi( ssl, level, file, line, str, &X->Y ); } @@ -215,9 +221,9 @@ void debug_print_mpi( const ssl_context *ssl, int level, break; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", + polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); str[maxlen] = '\0'; @@ -240,16 +246,16 @@ void debug_print_mpi( const ssl_context *ssl, int level, { if( j > 0 ) { - snprintf( str + idx, maxlen - idx, "\n" ); + polarssl_snprintf( str + idx, maxlen - idx, "\n" ); ssl->f_dbg( ssl->p_dbg, level, str ); idx = 0; } if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); } - idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) + idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); j++; @@ -261,13 +267,13 @@ void debug_print_mpi( const ssl_context *ssl, int level, { if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) { - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); } - idx += snprintf( str + idx, maxlen - idx, " 00" ); + idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" ); } - snprintf( str + idx, maxlen - idx, "\n" ); + polarssl_snprintf( str + idx, maxlen - idx, "\n" ); ssl->f_dbg( ssl->p_dbg, level, str ); } #endif /* POLARSSL_BIGNUM_C */ @@ -294,7 +300,7 @@ static void debug_print_pk( const ssl_context *ssl, int level, if( items[i].type == POLARSSL_PK_DEBUG_NONE ) return; - snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); + polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); name[sizeof( name ) - 1] = '\0'; if( items[i].type == POLARSSL_PK_DEBUG_MPI ) @@ -321,7 +327,7 @@ void debug_print_crt( const ssl_context *ssl, int level, if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) { - snprintf( prefix, maxlen, "%s(%04d): ", file, line ); + polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line ); prefix[maxlen] = '\0'; } else @@ -335,9 +341,9 @@ void debug_print_crt( const ssl_context *ssl, int level, x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt ); if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) - idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); + idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line ); - snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", + polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", text, ++i, buf ); str[maxlen] = '\0'; diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index 773e54ff0..bf4888351 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -27,7 +27,6 @@ #endif #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) - #include "polarssl/memory_buffer_alloc.h" /* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C @@ -269,7 +268,7 @@ static void *buffer_alloc_malloc( size_t len ) polarssl_fprintf( stderr, "FATAL: block in free_list but allocated " "data\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } #if defined(POLARSSL_MEMORY_DEBUG) @@ -308,7 +307,7 @@ static void *buffer_alloc_malloc( size_t len ) #endif if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); return( ( (unsigned char *) cur ) + sizeof(memory_header) ); } @@ -363,7 +362,7 @@ static void *buffer_alloc_malloc( size_t len ) #endif if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); return( ( (unsigned char *) cur ) + sizeof(memory_header) ); } @@ -382,14 +381,14 @@ static void buffer_alloc_free( void *ptr ) polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed " "space\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } p -= sizeof(memory_header); hdr = (memory_header *) p; if( verify_header( hdr ) != 0 ) - exit( 1 ); + polarssl_exit( 1 ); if( hdr->alloc != 1 ) { @@ -397,7 +396,7 @@ static void buffer_alloc_free( void *ptr ) polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated " "data\n" ); #endif - exit( 1 ); + polarssl_exit( 1 ); } hdr->alloc = 0; @@ -487,7 +486,7 @@ static void buffer_alloc_free( void *ptr ) #endif if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 ) - exit( 1 ); + polarssl_exit( 1 ); } void memory_buffer_set_verify( int verify ) diff --git a/library/net.c b/library/net.c index a8591ed0d..023e0e3df 100644 --- a/library/net.c +++ b/library/net.c @@ -129,6 +129,12 @@ typedef UINT32 uint32_t; (((unsigned long )(n) & 0xFF000000) >> 24)) #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + unsigned short net_htons( unsigned short n ); unsigned long net_htonl( unsigned long n ); #define net_htons(n) POLARSSL_HTONS(n) @@ -173,7 +179,7 @@ int net_connect( int *fd, const char *host, int port ) /* getaddrinfo expects port as a string */ memset( port_str, 0, sizeof( port_str ) ); - snprintf( port_str, sizeof( port_str ), "%d", port ); + polarssl_snprintf( port_str, sizeof( port_str ), "%d", port ); /* Do name resolution with both IPv6 and IPv4, but only TCP */ memset( &hints, 0, sizeof( hints ) ); @@ -259,7 +265,7 @@ int net_bind( int *fd, const char *bind_ip, int port ) /* getaddrinfo expects port as a string */ memset( port_str, 0, sizeof( port_str ) ); - snprintf( port_str, sizeof( port_str ), "%d", port ); + polarssl_snprintf( port_str, sizeof( port_str ), "%d", port ); /* Bind to IPv6 and/or IPv4, but only in TCP */ memset( &hints, 0, sizeof( hints ) ); diff --git a/library/oid.c b/library/oid.c index 75b0ee0e0..7bb563175 100644 --- a/library/oid.c +++ b/library/oid.c @@ -36,6 +36,12 @@ #include #include +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C) #include "polarssl/x509.h" #endif @@ -653,7 +659,7 @@ int oid_get_numeric_string( char *buf, size_t size, /* First byte contains first two dots */ if( oid->len > 0 ) { - ret = snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 ); + ret = polarssl_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 ); SAFE_SNPRINTF(); } @@ -670,7 +676,7 @@ int oid_get_numeric_string( char *buf, size_t size, if( !( oid->p[i] & 0x80 ) ) { /* Last byte */ - ret = snprintf( p, n, ".%d", value ); + ret = polarssl_snprintf( p, n, ".%d", value ); SAFE_SNPRINTF(); value = 0; } diff --git a/library/platform.c b/library/platform.c index 3eb4b1a8e..34295adc2 100644 --- a/library/platform.c +++ b/library/platform.c @@ -62,6 +62,36 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), } #endif /* POLARSSL_PLATFORM_MEMORY */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_snprintf_uninit( char * s, size_t n, + const char * format, ... ) +{ + ((void) s); + ((void) n); + ((void) format) + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit +#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */ + +int (*polarssl_snprintf)( char * s, size_t n, + const char * format, + ... ) = POLARSSL_PLATFORM_STD_SNPRINTF; + +int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, + const char * format, + ... ) ) +{ + polarssl_snprintf = snprintf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + #if defined(POLARSSL_PLATFORM_PRINTF_ALT) #if !defined(POLARSSL_PLATFORM_STD_PRINTF) /* @@ -110,4 +140,27 @@ int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) } #endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ +#if defined(POLARSSL_PLATFORM_EXIT_ALT) +#if !defined(POLARSSL_STD_EXIT) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static void platform_exit_uninit( int status ) +{ + ((void) status); + return( 0 ); +} + +#define POLARSSL_STD_EXIT platform_exit_uninit +#endif /* !POLARSSL_STD_EXIT */ + +int (*polarssl_exit)( int status ) = POLARSSL_STD_EXIT; + +int platform_set_exit( void (*exit_func)( int status ) ) +{ + polarssl_exit = exit_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ + #endif /* POLARSSL_PLATFORM_C */ diff --git a/library/x509.c b/library/x509.c index 4946ce6ba..955d34962 100644 --- a/library/x509.c +++ b/library/x509.c @@ -54,9 +54,10 @@ #else #include #include -#define polarssl_printf printf -#define polarssl_malloc malloc #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_printf printf +#define polarssl_snprintf snprintf #endif #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) @@ -74,6 +75,8 @@ #endif #endif +#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); } + /* * CertificateSerialNumber ::= INTEGER */ @@ -475,6 +478,16 @@ int x509_get_name( unsigned char **p, const unsigned char *end, } } +static int x509_parse_int(unsigned char **p, unsigned n, int *res){ + *res = 0; + for( ; n > 0; --n ){ + if( ( **p < '0') || ( **p > '9' ) ) return POLARSSL_ERR_X509_INVALID_DATE; + *res *= 10; + *res += (*(*p)++ - '0'); + } + return 0; +} + /* * Time ::= CHOICE { * utcTime UTCTime, @@ -485,7 +498,6 @@ int x509_get_time( unsigned char **p, const unsigned char *end, { int ret; size_t len; - char date[64]; unsigned char tag; if( ( end - *p ) < 1 ) @@ -502,20 +514,19 @@ int x509_get_time( unsigned char **p, const unsigned char *end, if( ret != 0 ) return( POLARSSL_ERR_X509_INVALID_DATE + ret ); - memset( date, 0, sizeof( date ) ); - memcpy( date, *p, ( len < sizeof( date ) - 1 ) ? - len : sizeof( date ) - 1 ); - - if( sscanf( date, "%2d%2d%2d%2d%2d%2dZ", - &time->year, &time->mon, &time->day, - &time->hour, &time->min, &time->sec ) < 5 ) + CHECK( x509_parse_int( p, 2, &time->year ) ); + CHECK( x509_parse_int( p, 2, &time->mon ) ); + CHECK( x509_parse_int( p, 2, &time->day ) ); + CHECK( x509_parse_int( p, 2, &time->hour ) ); + CHECK( x509_parse_int( p, 2, &time->min ) ); + if( len > 10 ) + CHECK( x509_parse_int( p, 2, &time->sec ) ); + if( len > 12 && *(*p)++ != 'Z' ) return( POLARSSL_ERR_X509_INVALID_DATE ); time->year += 100 * ( time->year < 50 ); time->year += 1900; - *p += len; - return( 0 ); } else if( tag == ASN1_GENERALIZED_TIME ) @@ -526,17 +537,16 @@ int x509_get_time( unsigned char **p, const unsigned char *end, if( ret != 0 ) return( POLARSSL_ERR_X509_INVALID_DATE + ret ); - memset( date, 0, sizeof( date ) ); - memcpy( date, *p, ( len < sizeof( date ) - 1 ) ? - len : sizeof( date ) - 1 ); - - if( sscanf( date, "%4d%2d%2d%2d%2d%2dZ", - &time->year, &time->mon, &time->day, - &time->hour, &time->min, &time->sec ) < 5 ) + CHECK( x509_parse_int( p, 4, &time->year ) ); + CHECK( x509_parse_int( p, 2, &time->mon ) ); + CHECK( x509_parse_int( p, 2, &time->day ) ); + CHECK( x509_parse_int( p, 2, &time->hour ) ); + CHECK( x509_parse_int( p, 2, &time->min ) ); + if( len > 12 ) + CHECK( x509_parse_int( p, 2, &time->sec ) ); + if( len > 14 && *(*p)++ != 'Z' ) return( POLARSSL_ERR_X509_INVALID_DATE ); - *p += len; - return( 0 ); } else @@ -736,16 +746,16 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ) if( name != dn ) { - ret = snprintf( p, n, merge ? " + " : ", " ); + ret = polarssl_snprintf( p, n, merge ? " + " : ", " ); SAFE_SNPRINTF(); } ret = oid_get_attr_short_name( &name->oid, &short_name ); if( ret == 0 ) - ret = snprintf( p, n, "%s=", short_name ); + ret = polarssl_snprintf( p, n, "%s=", short_name ); else - ret = snprintf( p, n, "\?\?=" ); + ret = polarssl_snprintf( p, n, "\?\?=" ); SAFE_SNPRINTF(); for( i = 0; i < name->val.len; i++ ) @@ -759,7 +769,7 @@ int x509_dn_gets( char *buf, size_t size, const x509_name *dn ) else s[i] = c; } s[i] = '\0'; - ret = snprintf( p, n, "%s", s ); + ret = polarssl_snprintf( p, n, "%s", s ); SAFE_SNPRINTF(); merge = name->next_merged; @@ -790,14 +800,14 @@ int x509_serial_gets( char *buf, size_t size, const x509_buf *serial ) if( i == 0 && nr > 1 && serial->p[i] == 0x0 ) continue; - ret = snprintf( p, n, "%02X%s", + ret = polarssl_snprintf( p, n, "%02X%s", serial->p[i], ( i < nr - 1 ) ? ":" : "" ); SAFE_SNPRINTF(); } if( nr != serial->len ) { - ret = snprintf( p, n, "...." ); + ret = polarssl_snprintf( p, n, "...." ); SAFE_SNPRINTF(); } @@ -818,9 +828,9 @@ int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, ret = oid_get_sig_alg_desc( sig_oid, &desc ); if( ret != 0 ) - ret = snprintf( p, n, "???" ); + ret = polarssl_snprintf( p, n, "???" ); else - ret = snprintf( p, n, "%s", desc ); + ret = polarssl_snprintf( p, n, "%s", desc ); SAFE_SNPRINTF(); #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) @@ -834,7 +844,7 @@ int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, md_info = md_info_from_type( md_alg ); mgf_md_info = md_info_from_type( pss_opts->mgf1_hash_id ); - ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", + ret = polarssl_snprintf( p, n, " (%s, MGF1-%s, 0x%02X)", md_info ? md_info->name : "???", mgf_md_info ? mgf_md_info->name : "???", pss_opts->expected_salt_len ); @@ -861,7 +871,7 @@ int x509_key_size_helper( char *buf, size_t size, const char *name ) if( strlen( name ) + sizeof( " key size" ) > size ) return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); - ret = snprintf( p, n, "%s key size", name ); + ret = polarssl_snprintf( p, n, "%s key size", name ); SAFE_SNPRINTF(); return( 0 ); diff --git a/library/x509_crl.c b/library/x509_crl.c index b957e3765..ce6df6eae 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -51,8 +51,9 @@ #include "polarssl/platform.h" #else #include -#define polarssl_malloc malloc #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) @@ -630,23 +631,23 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%sCRL version : %d", + ret = polarssl_snprintf( p, n, "%sCRL version : %d", prefix, crl->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissuer name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crl->issuer ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sthis update : " \ + ret = polarssl_snprintf( p, n, "\n%sthis update : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crl->this_update.year, crl->this_update.mon, crl->this_update.day, crl->this_update.hour, crl->this_update.min, crl->this_update.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%snext update : " \ + ret = polarssl_snprintf( p, n, "\n%snext update : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crl->next_update.year, crl->next_update.mon, crl->next_update.day, crl->next_update.hour, @@ -655,20 +656,20 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, entry = &crl->entry; - ret = snprintf( p, n, "\n%sRevoked certificates:", + ret = polarssl_snprintf( p, n, "\n%sRevoked certificates:", prefix ); SAFE_SNPRINTF(); while( entry != NULL && entry->raw.len != 0 ) { - ret = snprintf( p, n, "\n%sserial number: ", + ret = polarssl_snprintf( p, n, "\n%sserial number: ", prefix ); SAFE_SNPRINTF(); ret = x509_serial_gets( p, n, &entry->serial ); SAFE_SNPRINTF(); - ret = snprintf( p, n, " revocation date: " \ + ret = polarssl_snprintf( p, n, " revocation date: " \ "%04d-%02d-%02d %02d:%02d:%02d", entry->revocation_date.year, entry->revocation_date.mon, entry->revocation_date.day, entry->revocation_date.hour, @@ -678,14 +679,14 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, entry = entry->next; } - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, crl->sig_md, crl->sig_opts ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n" ); + ret = polarssl_snprintf( p, n, "\n" ); SAFE_SNPRINTF(); return( (int) ( size - n ) ); diff --git a/library/x509_crt.c b/library/x509_crt.c index ea3b44228..565435cba 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -52,8 +52,9 @@ #include "polarssl/platform.h" #else #include -#define polarssl_malloc malloc #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif #if defined(POLARSSL_THREADING_C) @@ -1040,7 +1041,7 @@ int x509_crt_parse_path( x509_crt *chain, const char *path ) while( ( entry = readdir( dir ) ) != NULL ) { - snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name ); + polarssl_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name ); if( stat( entry_name, &sb ) == -1 ) { @@ -1166,7 +1167,7 @@ static int x509_info_subject_alt_name( char **buf, size_t *size, #define PRINT_ITEM(i) \ { \ - ret = snprintf( p, n, "%s" i, sep ); \ + ret = polarssl_snprintf( p, n, "%s" i, sep ); \ SAFE_SNPRINTF(); \ sep = ", "; \ } @@ -1239,7 +1240,7 @@ static int x509_info_ext_key_usage( char **buf, size_t *size, if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 ) desc = "???"; - ret = snprintf( p, n, "%s%s", sep, desc ); + ret = polarssl_snprintf( p, n, "%s%s", sep, desc ); SAFE_SNPRINTF(); sep = ", "; @@ -1269,41 +1270,41 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%scert. version : %d\n", + ret = polarssl_snprintf( p, n, "%scert. version : %d\n", prefix, crt->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "%sserial number : ", + ret = polarssl_snprintf( p, n, "%sserial number : ", prefix ); SAFE_SNPRINTF(); ret = x509_serial_gets( p, n, &crt->serial ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissuer name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sissuer name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crt->issuer ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssubject name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &crt->subject ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sissued on : " \ + ret = polarssl_snprintf( p, n, "\n%sissued on : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crt->valid_from.year, crt->valid_from.mon, crt->valid_from.day, crt->valid_from.hour, crt->valid_from.min, crt->valid_from.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%sexpires on : " \ + ret = polarssl_snprintf( p, n, "\n%sexpires on : " \ "%04d-%02d-%02d %02d:%02d:%02d", prefix, crt->valid_to.year, crt->valid_to.mon, crt->valid_to.day, crt->valid_to.hour, crt->valid_to.min, crt->valid_to.sec ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk, @@ -1317,7 +1318,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, + ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, (int) pk_get_size( &crt->pk ) ); SAFE_SNPRINTF(); @@ -1327,20 +1328,20 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_BASIC_CONSTRAINTS ) { - ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, + ret = polarssl_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix, crt->ca_istrue ? "true" : "false" ); SAFE_SNPRINTF(); if( crt->max_pathlen > 0 ) { - ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); + ret = polarssl_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 ); SAFE_SNPRINTF(); } } if( crt->ext_types & EXT_SUBJECT_ALT_NAME ) { - ret = snprintf( p, n, "\n%ssubject alt name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject alt name : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_subject_alt_name( &p, &n, @@ -1350,7 +1351,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_NS_CERT_TYPE ) { - ret = snprintf( p, n, "\n%scert. type : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%scert. type : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 ) @@ -1359,7 +1360,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_KEY_USAGE ) { - ret = snprintf( p, n, "\n%skey usage : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%skey usage : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 ) @@ -1368,7 +1369,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, if( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) { - ret = snprintf( p, n, "\n%sext key usage : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%sext key usage : ", prefix ); SAFE_SNPRINTF(); if( ( ret = x509_info_ext_key_usage( &p, &n, @@ -1376,7 +1377,7 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n" ); + ret = polarssl_snprintf( p, n, "\n" ); SAFE_SNPRINTF(); return( (int) ( size - n ) ); diff --git a/library/x509_csr.c b/library/x509_csr.c index a4b8ad754..a5c969367 100644 --- a/library/x509_csr.c +++ b/library/x509_csr.c @@ -51,8 +51,9 @@ #include "polarssl/platform.h" #else #include -#define polarssl_malloc malloc #define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_snprintf snprintf #endif #if defined(POLARSSL_FS_IO) || defined(EFIX64) || defined(EFI32) @@ -388,16 +389,16 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, p = buf; n = size; - ret = snprintf( p, n, "%sCSR version : %d", + ret = polarssl_snprintf( p, n, "%sCSR version : %d", prefix, csr->version ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssubject name : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssubject name : ", prefix ); SAFE_SNPRINTF(); ret = x509_dn_gets( p, n, &csr->subject ); SAFE_SNPRINTF(); - ret = snprintf( p, n, "\n%ssigned using : ", prefix ); + ret = polarssl_snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md, @@ -410,7 +411,7 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, return( ret ); } - ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, + ret = polarssl_snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str, (int) pk_get_size( &csr->pk ) ); SAFE_SNPRINTF(); diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 162db1e98..88561b189 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -30,6 +30,7 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -136,7 +137,7 @@ int main( int argc, char *argv[] ) /* * Write the signature into -sig.txt */ - snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); + polarssl_snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); if( ( f = fopen( filename, "wb+" ) ) == NULL ) { diff --git a/programs/pkey/pk_verify.c b/programs/pkey/pk_verify.c index 6f8755bd1..d8cd9a144 100644 --- a/programs/pkey/pk_verify.c +++ b/programs/pkey/pk_verify.c @@ -30,6 +30,7 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -96,7 +97,7 @@ int main( int argc, char *argv[] ) * Extract the signature from the text file */ ret = 1; - snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); + polarssl_snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) { diff --git a/programs/pkey/rsa_sign_pss.c b/programs/pkey/rsa_sign_pss.c index be42ad18f..243309621 100644 --- a/programs/pkey/rsa_sign_pss.c +++ b/programs/pkey/rsa_sign_pss.c @@ -30,6 +30,7 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -146,7 +147,7 @@ int main( int argc, char *argv[] ) /* * Write the signature into -sig.txt */ - snprintf( filename, 512, "%s.sig", argv[2] ); + polarssl_snprintf( filename, 512, "%s.sig", argv[2] ); if( ( f = fopen( filename, "wb+" ) ) == NULL ) { diff --git a/programs/pkey/rsa_verify_pss.c b/programs/pkey/rsa_verify_pss.c index d679a58ff..65fcfbe87 100644 --- a/programs/pkey/rsa_verify_pss.c +++ b/programs/pkey/rsa_verify_pss.c @@ -30,6 +30,7 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -107,7 +108,7 @@ int main( int argc, char *argv[] ) * Extract the RSA signature from the text file */ ret = 1; - snprintf( filename, 512, "%s.sig", argv[2] ); + polarssl_snprintf( filename, 512, "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) { diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index e0ba186fc..e2dac4312 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -32,6 +32,7 @@ #include #define polarssl_fprintf fprintf #define polarssl_printf printf +#define polarssl_snprintf snprintf #endif #if defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_FS_IO) && \ @@ -1197,7 +1198,7 @@ send_request: polarssl_printf( " > Write to server:" ); fflush( stdout ); - len = snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST, + len = polarssl_snprintf( (char *) buf, sizeof(buf) - 1, GET_REQUEST, opt.request_page ); tail_len = strlen( GET_REQUEST_END ); diff --git a/programs/ssl/ssl_pthread_server.c b/programs/ssl/ssl_pthread_server.c index 091d07a7b..524413c40 100644 --- a/programs/ssl/ssl_pthread_server.c +++ b/programs/ssl/ssl_pthread_server.c @@ -33,6 +33,7 @@ #include #define polarssl_fprintf fprintf #define polarssl_printf printf +#define polarssl_snprintf snprintf #endif #if defined(_WIN32) @@ -141,7 +142,7 @@ static void *handle_ssl_connection( void *data ) memset( &ssl, 0, sizeof( ssl_context ) ); memset( &ctr_drbg, 0, sizeof( ctr_drbg_context ) ); - snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id ); + polarssl_snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id ); polarssl_printf( " [ #%d ] Client FD %d\n", thread_id, client_fd ); polarssl_printf( " [ #%d ] Seeding the random number generator...\n", thread_id ); diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index 2882ecfc7..edb7c0766 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -30,7 +30,9 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf +#define polarssl_exit exit #endif #if defined(POLARSSL_TIMING_C) @@ -314,7 +316,7 @@ int main( int argc, char *argv[] ) aes_init( &aes ); for( keysize = 128; keysize <= 256; keysize += 64 ) { - snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); @@ -332,7 +334,7 @@ int main( int argc, char *argv[] ) gcm_context gcm; for( keysize = 128; keysize <= 256; keysize += 64 ) { - snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); @@ -352,7 +354,7 @@ int main( int argc, char *argv[] ) ccm_context ccm; for( keysize = 128; keysize <= 256; keysize += 64 ) { - snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); @@ -375,7 +377,7 @@ int main( int argc, char *argv[] ) camellia_init( &camellia ); for( keysize = 128; keysize <= 256; keysize += 64 ) { - snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); @@ -397,7 +399,7 @@ int main( int argc, char *argv[] ) for( keysize = 128; keysize <= 256; keysize += 64 ) { - snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); memset( buf, 0, sizeof( buf ) ); memset( tmp, 0, sizeof( tmp ) ); @@ -428,17 +430,17 @@ int main( int argc, char *argv[] ) ctr_drbg_context ctr_drbg; if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); TIME_AND_TSC( "CTR_DRBG (NOPR)", if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); TIME_AND_TSC( "CTR_DRBG (PR)", if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); ctr_drbg_free( &ctr_drbg ); } #endif @@ -451,43 +453,43 @@ int main( int argc, char *argv[] ) #if defined(POLARSSL_SHA1_C) if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL ) - exit(1); + polarssl_exit(1); if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); hmac_drbg_free( &hmac_drbg ); if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); hmac_drbg_set_prediction_resistance( &hmac_drbg, POLARSSL_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); hmac_drbg_free( &hmac_drbg ); #endif #if defined(POLARSSL_SHA256_C) if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL ) - exit(1); + polarssl_exit(1); if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); hmac_drbg_free( &hmac_drbg ); if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) - exit(1); + polarssl_exit(1); hmac_drbg_set_prediction_resistance( &hmac_drbg, POLARSSL_HMAC_DRBG_PR_ON ); TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) - exit(1) ); + polarssl_exit(1) ); hmac_drbg_free( &hmac_drbg ); #endif } @@ -499,7 +501,7 @@ int main( int argc, char *argv[] ) rsa_context rsa; for( keysize = 1024; keysize <= 4096; keysize *= 2 ) { - snprintf( title, sizeof( title ), "RSA-%d", keysize ); + polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize ); rsa_init( &rsa, RSA_PKCS_V15, 0 ); rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); @@ -541,22 +543,22 @@ int main( int argc, char *argv[] ) if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) { - exit( 1 ); + polarssl_exit( 1 ); } dhm.len = mpi_size( &dhm.P ); dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) - exit( 1 ); + polarssl_exit( 1 ); - snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); + polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); TIME_PUBLIC( title, "handshake", olen = sizeof( buf ); ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); - snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); + polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); TIME_PUBLIC( title, "handshake", olen = sizeof( buf ); ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); @@ -582,9 +584,9 @@ int main( int argc, char *argv[] ) ecdsa_init( &ecdsa ); if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) - exit( 1 ); + polarssl_exit( 1 ); - snprintf( title, sizeof( title ), "ECDSA-%s", + polarssl_snprintf( title, sizeof( title ), "ECDSA-%s", curve_info->name ); TIME_PUBLIC( title, "sign", ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size, @@ -617,10 +619,10 @@ int main( int argc, char *argv[] ) myrand, NULL ) != 0 || ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) { - exit( 1 ); + polarssl_exit( 1 ); } - snprintf( title, sizeof( title ), "ECDHE-%s", + polarssl_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name ); TIME_PUBLIC( title, "handshake", ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), @@ -628,7 +630,7 @@ int main( int argc, char *argv[] ) ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), myrand, NULL ) ); - snprintf( title, sizeof( title ), "ECDH-%s", + polarssl_snprintf( title, sizeof( title ), "ECDH-%s", curve_info->name ); TIME_PUBLIC( title, "handshake", ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), diff --git a/programs/test/ssl_cert_test.c b/programs/test/ssl_cert_test.c index 393651fca..782d6f38a 100644 --- a/programs/test/ssl_cert_test.c +++ b/programs/test/ssl_cert_test.c @@ -30,6 +30,7 @@ #include "polarssl/platform.h" #else #include +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -145,7 +146,7 @@ int main( void ) x509_crt_init( &clicert ); pk_init( &pk ); - snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); + polarssl_snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]); polarssl_printf( " . Loading the client certificate %s...", name ); fflush( stdout ); @@ -194,7 +195,7 @@ int main( void ) /* * 1.5. Load own private key */ - snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); + polarssl_snprintf(name, 512, "ssl/test-ca/%s", client_private_keys[i]); polarssl_printf( " . Loading the client private key %s...", name ); fflush( stdout ); diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt index 1b5bb7af8..eeefd7b8f 100644 --- a/scripts/data_files/error.fmt +++ b/scripts/data_files/error.fmt @@ -31,6 +31,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + #if defined(POLARSSL_ERROR_C) #include @@ -67,7 +73,7 @@ HIGH_LEVEL_CODE_CHECKS // END generated code if( strlen( buf ) == 0 ) - snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); + polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); } use_ret = ret & ~0xFF80; @@ -85,7 +91,7 @@ HIGH_LEVEL_CODE_CHECKS if( buflen - len < 5 ) return; - snprintf( buf + len, buflen - len, " : " ); + polarssl_snprintf( buf + len, buflen - len, " : " ); buf += len + 3; buflen -= len + 3; @@ -100,7 +106,7 @@ LOW_LEVEL_CODE_CHECKS if( strlen( buf ) != 0 ) return; - snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); + polarssl_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret ); } #if defined(POLARSSL_ERROR_STRERROR_BC) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index 04591b28b..c0d9685f6 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -152,14 +152,14 @@ while (my $line = ) { ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". "${white_space}\{\n". - "${white_space} snprintf( buf, buflen, \"$module_name - $description\" );\n". + "${white_space} polarssl_snprintf( buf, buflen, \"$module_name - $description\" );\n". "${white_space} return;\n". "${white_space}}\n" } else { ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". - "${white_space} snprintf( buf, buflen, \"$module_name - $description\" );\n" + "${white_space} polarssl_snprintf( buf, buflen, \"$module_name - $description\" );\n" } }; diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 4e1bac1ee..0f074859c 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -6,6 +6,10 @@ #define polarssl_fprintf fprintf #define polarssl_malloc malloc #define polarssl_free free +#define polarssl_exit exit +#define polarssl_fprintf fprintf +#define polarssl_printf printf +#define polarssl_snprintf snprintf #endif #ifdef _MSC_VER @@ -15,10 +19,17 @@ typedef UINT32 uint32_t; #include #endif -#include +#include #include #include +#define assert(a) if( !( a ) ) \ +{ \ + polarssl_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \ + __FILE__, __LINE__, #a ); \ + polarssl_exit( 1 ); \ +} + /* * 32-bit integer manipulation macros (big endian) */ @@ -42,13 +53,13 @@ typedef UINT32 uint32_t; } #endif -static int unhexify(unsigned char *obuf, const char *ibuf) +static int unhexify( unsigned char *obuf, const char *ibuf ) { unsigned char c, c2; - int len = strlen(ibuf) / 2; - assert( strlen(ibuf) % 2 == 0 ); // must be even number of bytes + int len = strlen( ibuf ) / 2; + assert( strlen( ibuf ) % 2 == 0 ); // must be even number of bytes - while (*ibuf != 0) + while( *ibuf != 0 ) { c = *ibuf++; if( c >= '0' && c <= '9' ) diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index 9d9ebe5f8..bb1083acf 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -1,3 +1,15 @@ +#include + +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_exit exit +#define polarssl_free free +#define polarssl_malloc malloc +#define polarssl_fprintf fprintf +#define polarssl_printf printf +#endif + #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) #include "polarssl/memory_buffer_alloc.h" #endif @@ -268,7 +280,7 @@ int main() { polarssl_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); fclose(file); - exit( 2 ); + polarssl_exit( 2 ); } else total_errors++; diff --git a/tests/suites/test_suite_version.function b/tests/suites/test_suite_version.function index f50a6c29b..fd12032e1 100644 --- a/tests/suites/test_suite_version.function +++ b/tests/suites/test_suite_version.function @@ -17,10 +17,10 @@ void check_compiletime_version( char *version_str ) memset( build_str, 0, 100 ); memset( build_str_full, 0, 100 ); - snprintf (build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR, + polarssl_snprintf( build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR, POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH ); - snprintf( build_str_full, 100, "mbed TLS %d.%d.%d", POLARSSL_VERSION_MAJOR, + polarssl_snprintf( build_str_full, 100, "mbed TLS %d.%d.%d", POLARSSL_VERSION_MAJOR, POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH ); build_int = POLARSSL_VERSION_MAJOR << 24 | @@ -52,11 +52,11 @@ void check_runtime_version( char *version_str ) version_get_string( get_str ); version_get_string_full( get_str_full ); - snprintf( build_str, 100, "%d.%d.%d", + polarssl_snprintf( build_str, 100, "%d.%d.%d", (get_int >> 24) & 0xFF, (get_int >> 16) & 0xFF, (get_int >> 8) & 0xFF ); - snprintf( build_str_full, 100, "mbed TLS %s", version_str ); + polarssl_snprintf( build_str_full, 100, "mbed TLS %s", version_str ); TEST_ASSERT( strcmp( build_str, version_str ) == 0 ); TEST_ASSERT( strcmp( build_str_full, get_str_full ) == 0 );