From 46b0a8d15a0043fbe4e293de26ebbf3093f10b8d Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 10:47:32 +0000 Subject: [PATCH 01/18] add platform_set_snprintf --- include/polarssl/check_config.h | 5 +++++ include/polarssl/config.h | 7 ++++++- include/polarssl/platform.h | 22 ++++++++++++++++++++++ library/platform.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index 51b124d6d..be7aefeee 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,6 +198,11 @@ #error "POLARSSL_PKCS11_C 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_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..75cf677c5 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -159,9 +159,13 @@ * * All these define require POLARSSL_PLATFORM_C to be defined! * + * WARNING: POLARSSL_PLATFORM_SNPRINTF_ALT is not available on Windows + * for compatibility reasons. + * * Uncomment a macro to enable alternate implementation of specific base * platform function */ +//#define POLARSSL_PLATFORM_SNPRINTF_ALT //#define POLARSSL_PLATFORM_PRINTF_ALT //#define POLARSSL_PLATFORM_FPRINTF_ALT /* \} name SECTION: System support */ @@ -1890,7 +1894,7 @@ * \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() * * Module: library/platform.c * Caller: Most other .c files @@ -2238,6 +2242,7 @@ //#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_SNPRINTF snprintf /**< Default snprintf 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 */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 127b7fe3e..4844d2d01 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 @@ -92,6 +95,25 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), #define polarssl_free free #endif /* POLARSSL_PLATFORM_MEMORY */ +/* + * The function pointers for snprintf + */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); + +/** + * \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 */ +#define polarssl_snprintf snprintf +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + /* * The function pointers for printf */ diff --git a/library/platform.c b/library/platform.c index 3eb4b1a8e..8a26f7b84 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) /* From 8f3a9436a952e8fad6f275082b7ac7b554c387bf Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 10:54:04 +0000 Subject: [PATCH 02/18] modify oid.c to use polarssl_snprintf --- library/oid.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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; } From a18b11f285f3df2b98d2455fd07e6db96c010089 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 10:58:35 +0000 Subject: [PATCH 03/18] modify library/net.c to use polarssl_snprintf --- library/net.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 ) ); From fac657fd5249f09bc7c69ff728edb9f9b0fd39ee Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 11:00:01 +0000 Subject: [PATCH 04/18] modify library/x509*.c to use polarssl_snprintf --- library/x509.c | 25 +++++++++++++------------ library/x509_crl.c | 21 +++++++++++---------- library/x509_crt.c | 39 ++++++++++++++++++++------------------- library/x509_csr.c | 11 ++++++----- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/library/x509.c b/library/x509.c index c9b196f46..3b6cd1bb1 100644 --- a/library/x509.c +++ b/library/x509.c @@ -53,9 +53,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) @@ -734,16 +735,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++ ) @@ -757,7 +758,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; @@ -788,14 +789,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(); } @@ -816,9 +817,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) @@ -832,7 +833,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 ); @@ -859,7 +860,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(); From 2387c7d1054233ea8f580e9c5c1470659f0eb93d Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 11:10:20 +0000 Subject: [PATCH 05/18] modify library/debug.c to use polarssl_snprintf --- library/debug.c | 56 +++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 25 deletions(-) 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'; From 783d9d1c3e03a7cf9f473e452804d7e28b35bf43 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 11:11:57 +0000 Subject: [PATCH 06/18] modify programs/*.c to use polarssl_snprintf --- programs/pkey/pk_sign.c | 3 ++- programs/pkey/pk_verify.c | 3 ++- programs/pkey/rsa_sign_pss.c | 3 ++- programs/pkey/rsa_verify_pss.c | 3 ++- programs/ssl/ssl_client2.c | 3 ++- programs/ssl/ssl_pthread_server.c | 3 ++- programs/test/benchmark.c | 23 ++++++++++++----------- programs/test/ssl_cert_test.c | 5 +++-- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/programs/pkey/pk_sign.c b/programs/pkey/pk_sign.c index 0d0293596..ada29f2a5 100644 --- a/programs/pkey/pk_sign.c +++ b/programs/pkey/pk_sign.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -133,7 +134,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 55f977ccd..88ad575b4 100644 --- a/programs/pkey/pk_verify.c +++ b/programs/pkey/pk_verify.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -94,7 +95,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 de90b7ec0..45e848283 100644 --- a/programs/pkey/rsa_sign_pss.c +++ b/programs/pkey/rsa_sign_pss.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -143,7 +144,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 dd625b480..5595be547 100644 --- a/programs/pkey/rsa_verify_pss.c +++ b/programs/pkey/rsa_verify_pss.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -105,7 +106,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 0d4a0f209..9aa98c7bb 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #define polarssl_fprintf fprintf #endif @@ -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 8c6717364..b75464b7d 100644 --- a/programs/ssl/ssl_pthread_server.c +++ b/programs/ssl/ssl_pthread_server.c @@ -30,6 +30,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #define polarssl_fprintf fprintf #endif @@ -137,7 +138,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 cc83746d7..3acf78bf1 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -313,7 +314,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 ) ); @@ -331,7 +332,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 ) ); @@ -351,7 +352,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 ) ); @@ -374,7 +375,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 ) ); @@ -396,7 +397,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 ) ); @@ -498,7 +499,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 ); @@ -549,14 +550,14 @@ int main( int argc, char *argv[] ) if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) 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 ) ); @@ -584,7 +585,7 @@ int main( int argc, char *argv[] ) if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) 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, @@ -620,7 +621,7 @@ int main( int argc, char *argv[] ) 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 +629,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 037c47483..a646c2554 100644 --- a/programs/test/ssl_cert_test.c +++ b/programs/test/ssl_cert_test.c @@ -29,6 +29,7 @@ #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else +#define polarssl_snprintf snprintf #define polarssl_printf printf #endif @@ -149,7 +150,7 @@ int main( int argc, char *argv[] ) 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 ); @@ -198,7 +199,7 @@ int main( int argc, char *argv[] ) /* * 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 ); From 6aa04bcd853dca13be268aa9bfd56be6374371b3 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 11:18:42 +0000 Subject: [PATCH 07/18] modify scripts/* and tests/* to use polarssl_snprintf --- scripts/data_files/error.fmt | 12 +++++++++--- scripts/generate_errors.pl | 4 ++-- tests/suites/test_suite_version.function | 8 ++++---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt index b7bfbf20f..107b5e115 100644 --- a/scripts/data_files/error.fmt +++ b/scripts/data_files/error.fmt @@ -30,6 +30,12 @@ #include "polarssl/error.h" #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_snprintf snprintf +#endif + #if defined(POLARSSL_ERROR_C) HEADER_INCLUDED @@ -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/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 ); From c39cb4986b922bacf400f9563864b3c9efcabf66 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 12:01:34 +0000 Subject: [PATCH 08/18] add POLARSSL_PLATFORM_EXIT_ALT --- include/polarssl/config.h | 6 ++++-- include/polarssl/platform.h | 30 +++++++++++++++++++++++++++++- library/platform.c | 23 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/include/polarssl/config.h b/include/polarssl/config.h index 75cf677c5..d08937591 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -168,6 +168,7 @@ //#define POLARSSL_PLATFORM_SNPRINTF_ALT //#define POLARSSL_PLATFORM_PRINTF_ALT //#define POLARSSL_PLATFORM_FPRINTF_ALT +//#define POLARSSL_PLATFORM_EXIT_ALT /* \} name SECTION: System support */ /** @@ -1894,7 +1895,7 @@ * \def POLARSSL_PLATFORM_C * * Enable the platform abstraction layer that allows you to re-assign - * functions like malloc(), free(), snprintf(), printf(), fprintf() + * functions like malloc(), free(), snprintf(), printf(), fprintf(), exit() * * Module: library/platform.c * Caller: Most other .c files @@ -2242,9 +2243,10 @@ //#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_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */ +//#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf 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_EXIT exit /**< Default exit 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 4844d2d01..171503005 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -65,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 @@ -138,11 +141,36 @@ int platform_set_printf( int (*printf_func)( const char *, ... ) ); #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 #define polarssl_fprintf fprintf -#endif +#endif /* POLARSSL_PLATFORM_FPRINTF_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 +#define polarssl_exit exit +#endif /* POLARSSL_PLATFORM_EXIT_ALT */ #ifdef __cplusplus } diff --git a/library/platform.c b/library/platform.c index 8a26f7b84..34295adc2 100644 --- a/library/platform.c +++ b/library/platform.c @@ -140,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 */ From 77d3638497270bb8cd51a0199d7d899c1ba8b12f Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 30 Jan 2015 12:12:11 +0000 Subject: [PATCH 09/18] modify library/memory_buffer_alloc.c, benchmark.c and the tests main code to use polarssl_exit --- library/memory_buffer_alloc.c | 16 +++++++------- programs/test/benchmark.c | 37 +++++++++++++++++---------------- tests/suites/main_test.function | 15 ++++++++++++- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index b7d583b00..44deac00f 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -37,9 +37,11 @@ #include "polarssl/platform.h" #else #include +#define polarssl_exit exit #define polarssl_fprintf fprintf #endif /* POLARSSL_PLATFORM_C */ #endif /* POLARSSL_MEMORY_DEBUG */ + #if defined(POLARSSL_MEMORY_BACKTRACE) #include #endif @@ -273,7 +275,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) @@ -312,7 +314,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) ); } @@ -367,7 +369,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) ); } @@ -386,14 +388,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 ) { @@ -401,7 +403,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; @@ -491,7 +493,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/programs/test/benchmark.c b/programs/test/benchmark.c index 3acf78bf1..b6ab1a12d 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -31,6 +31,7 @@ #else #define polarssl_snprintf snprintf #define polarssl_printf printf +#define polarssl_exit exit #endif #include @@ -428,17 +429,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 +452,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 } @@ -542,13 +543,13 @@ 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 ); polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); TIME_PUBLIC( title, "handshake", @@ -583,7 +584,7 @@ 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 ); polarssl_snprintf( title, sizeof( title ), "ECDSA-%s", curve_info->name ); @@ -618,7 +619,7 @@ int main( int argc, char *argv[] ) myrand, NULL ) != 0 || ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) { - exit( 1 ); + polarssl_exit( 1 ); } polarssl_snprintf( title, sizeof( title ), "ECDHE-%s", diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index 9d9ebe5f8..f34688562 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -1,3 +1,16 @@ +#include +#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 +281,7 @@ int main() { polarssl_fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" ); fclose(file); - exit( 2 ); + polarssl_exit( 2 ); } else total_errors++; From 4c09114c32349689e9ff3726150fe370502fd797 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Mon, 2 Feb 2015 12:04:10 +0000 Subject: [PATCH 10/18] add macro definition of assert using polarssl_exit --- tests/suites/helpers.function | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 4e1bac1ee..3ae9a40a3 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -15,10 +15,16 @@ typedef UINT32 uint32_t; #include #endif -#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 +48,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' ) From c0b6da3b439b4971aac241d15656abdcddc1616f Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Tue, 3 Feb 2015 10:58:06 +0000 Subject: [PATCH 11/18] add missing checks to check_config --- include/polarssl/check_config.h | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index be7aefeee..51745c95e 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,11 +198,68 @@ #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_FPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites" +#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_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_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" From 98081c5ec64caf356065ce1834bf4e8fff6b4099 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Tue, 3 Feb 2015 11:00:54 +0000 Subject: [PATCH 12/18] reformat and arrange additions to config alphabetically --- include/polarssl/config.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/polarssl/config.h b/include/polarssl/config.h index d08937591..7db377e09 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -165,10 +165,10 @@ * Uncomment a macro to enable alternate implementation of specific base * platform function */ -//#define POLARSSL_PLATFORM_SNPRINTF_ALT -//#define POLARSSL_PLATFORM_PRINTF_ALT -//#define POLARSSL_PLATFORM_FPRINTF_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 */ /** @@ -2240,13 +2240,13 @@ //#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_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 */ -//#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_EXIT exit /**< Default exit to use, can be undefined */ /* SSL Cache options */ //#define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ From 4cc8a22d8845aee4b26074ae320abf5bdd156e79 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Tue, 3 Feb 2015 11:26:31 +0000 Subject: [PATCH 13/18] add initial symbols to config and checks to check_config to allow use of macros to define standard functions --- include/polarssl/check_config.h | 75 +++++++++++++++++++++++++++++++++ include/polarssl/config.h | 17 ++++++++ 2 files changed, 92 insertions(+) diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index 51745c95e..eb2fecd8d 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,14 +198,65 @@ #error "POLARSSL_PKCS11_C defined, but not all prerequisites" #endif +#if defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) &&\ + !defined(POLARSSL_PLATFORM_C) +#error "POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS defined, but not 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) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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_MALLOC_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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_MEMORY) && !defined(POLARSSL_PLATFORM_C) #error "POLARSSL_PLATFORM_MEMORY defined, but not all prerequisites" #endif @@ -214,6 +265,18 @@ #error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites" #endif +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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 @@ -223,6 +286,18 @@ #error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows" #endif +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\ + ( !defined(POLARSSL_PLATFORM_C) ||\ + !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#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" diff --git a/include/polarssl/config.h b/include/polarssl/config.h index 7db377e09..6f134401e 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -147,6 +147,16 @@ */ //#define POLARSSL_PLATFORM_NO_STD_FUNCTIONS +/** + * \def POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS + * + * TO-DO: ADD DESCRIPTION & ANY WARNINGS ETC + * + * Requires: POLARSSL_PLATFORM_C + * + */ +//#define POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS + /** * \def POLARSSL_PLATFORM_XXX_ALT * @@ -2248,6 +2258,13 @@ //#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 */ +//#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 */ //#define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */ From 3d62e726485d24d67a39ffb8e168eb8bd6114110 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Tue, 3 Feb 2015 11:48:59 +0000 Subject: [PATCH 14/18] fix bug introduced by the addition of snprintf and assert macro which caused tests to fail without polarssl_platform_c defined --- tests/suites/helpers.function | 5 +++++ tests/suites/main_test.function | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index 3ae9a40a3..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,6 +19,7 @@ typedef UINT32 uint32_t; #include #endif +#include #include #include diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index f34688562..bb1083acf 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -1,4 +1,3 @@ -#include #include #if defined(POLARSSL_PLATFORM_C) From 16f8cd8e87493eb34975f7900eef3b91a30df007 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 6 Feb 2015 16:14:34 +0000 Subject: [PATCH 15/18] implemented macro overriding for polarssl_* library functions --- include/polarssl/check_config.h | 35 +++++------ include/polarssl/config.h | 32 ++++++---- include/polarssl/platform.h | 102 ++++++++++++++++++++------------ 3 files changed, 98 insertions(+), 71 deletions(-) diff --git a/include/polarssl/check_config.h b/include/polarssl/check_config.h index eb2fecd8d..818aef3fe 100644 --- a/include/polarssl/check_config.h +++ b/include/polarssl/check_config.h @@ -198,18 +198,11 @@ #error "POLARSSL_PKCS11_C defined, but not all prerequisites" #endif -#if defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) &&\ - !defined(POLARSSL_PLATFORM_C) -#error "POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS defined, but not 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) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#if defined(POLARSSL_PLATFORM_EXIT_MACRO) && !defined(POLARSSL_PLATFORM_C) #error "POLARSSL_PLATFORM_EXIT_MACRO defined, but not all prerequisites" #endif @@ -223,9 +216,7 @@ #error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites" #endif -#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) &&\ - ( !defined(POLARSSL_PLATFORM_C) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) #error "POLARSSL_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites" #endif @@ -236,8 +227,7 @@ #endif #if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\ - ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) #error "POLARSSL_PLATFORM_FREE_MACRO defined, but not all prerequisites" #endif @@ -246,9 +236,12 @@ #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) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) #error "POLARSSL_PLATFORM_MALLOC_MACRO defined, but not all prerequisites" #endif @@ -257,6 +250,10 @@ #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 @@ -265,9 +262,7 @@ #error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites" #endif -#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\ - ( !defined(POLARSSL_PLATFORM_C) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) #error "POLARSSL_PLATFORM_PRINTF_MACRO defined, but not all prerequisites" #endif @@ -286,9 +281,7 @@ #error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows" #endif -#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\ - ( !defined(POLARSSL_PLATFORM_C) ||\ - !defined(POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS) ) +#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C) #error "POLARSSL_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites" #endif diff --git a/include/polarssl/config.h b/include/polarssl/config.h index 6f134401e..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 * @@ -147,16 +154,6 @@ */ //#define POLARSSL_PLATFORM_NO_STD_FUNCTIONS -/** - * \def POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS - * - * TO-DO: ADD DESCRIPTION & ANY WARNINGS ETC - * - * Requires: POLARSSL_PLATFORM_C - * - */ -//#define POLARSSL_PLATFORM_ENABLE_FUNCTION_MACROS - /** * \def POLARSSL_PLATFORM_XXX_ALT * @@ -172,6 +169,9 @@ * 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 */ @@ -1907,6 +1907,10 @@ * Enable the platform abstraction layer that allows you to re-assign * 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 * @@ -2258,6 +2262,8 @@ //#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 */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 171503005..dd7e55808 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -97,43 +97,18 @@ int platform_set_malloc_free( void * (*malloc_func)( size_t ), #define polarssl_malloc malloc #define polarssl_free free #endif /* POLARSSL_PLATFORM_MEMORY */ - -/* - * The function pointers for snprintf - */ -#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) -extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); - -/** - * \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 */ -#define polarssl_snprintf snprintf -#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ - -/* - * The function pointers for printf - */ -#if defined(POLARSSL_PLATFORM_PRINTF_ALT) -extern int (*polarssl_printf)( const char *format, ... ); - -/** - * \brief Set your own printf function pointer - * - * \param printf_func the printf function implementation - * - * \return 0 - */ -int platform_set_printf( int (*printf_func)( const char *, ... ) ); -#else /* POLARSSL_PLATFORM_PRINTF_ALT */ -#define polarssl_printf printf -#endif /* POLARSSL_PLATFORM_PRINTF_ALT */ +#else /* POLARSSL_PLATFORM_ENTROPY */ +#if defined(POLARSSL_PLATFORM_FREE_MACRO) +#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO +#else +#define polarssl_free free +#endif /* POLARSSL_PLATFORM_FREE_MACRO */ +#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) +#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO +#else +#define polarssl_malloc malloc +#endif /* POLARSSL_PLATFORM_MALLOC_MACRO */ +#endif /* POLARSSL_PLATFORM_ENTROPY */ /* * The function pointers for fprintf @@ -151,9 +126,58 @@ extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); 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 + */ +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) +extern int (*polarssl_printf)( const char *format, ... ); + +/** + * \brief Set your own printf function pointer + * + * \param printf_func the printf function implementation + * + * \return 0 + */ +int platform_set_printf( int (*printf_func)( const char *, ... ) ); +#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 snprintf + */ +#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) +extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... ); + +/** + * \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_snprintf snprintf +#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */ +#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */ + /* * The function pointers for exit */ @@ -169,7 +193,11 @@ extern void (*polarssl_exit)( int status ); */ 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 From 401bb90ac00d616313377bc99d6e810973adabac Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Tue, 10 Feb 2015 12:28:15 +0000 Subject: [PATCH 16/18] rebase from development --- include/polarssl/platform.h | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index dd7e55808..3e76c301b 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -80,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 ); @@ -93,22 +98,11 @@ 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 */ -#else /* POLARSSL_PLATFORM_ENTROPY */ -#if defined(POLARSSL_PLATFORM_FREE_MACRO) -#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO -#else #define polarssl_free free -#endif /* POLARSSL_PLATFORM_FREE_MACRO */ -#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) -#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO -#else #define polarssl_malloc malloc -#endif /* POLARSSL_PLATFORM_MALLOC_MACRO */ -#endif /* POLARSSL_PLATFORM_ENTROPY */ +#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */ /* * The function pointers for fprintf From c8ada6d41090223f11967389defd8325d7e9c8c8 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Thu, 12 Feb 2015 12:47:09 +0000 Subject: [PATCH 17/18] Fix extra guard in memory_buffer_alloc --- library/memory_buffer_alloc.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index 44deac00f..456b47137 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -27,12 +27,10 @@ #endif #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) - #include "polarssl/memory_buffer_alloc.h" #include -#if defined(POLARSSL_MEMORY_DEBUG) #if defined(POLARSSL_PLATFORM_C) #include "polarssl/platform.h" #else @@ -40,7 +38,6 @@ #define polarssl_exit exit #define polarssl_fprintf fprintf #endif /* POLARSSL_PLATFORM_C */ -#endif /* POLARSSL_MEMORY_DEBUG */ #if defined(POLARSSL_MEMORY_BACKTRACE) #include From 7d5a55a365736b35f9731cdb8cd91cf419050470 Mon Sep 17 00:00:00 2001 From: Rich Evans Date: Fri, 13 Feb 2015 11:48:02 +0000 Subject: [PATCH 18/18] Remove dependency on sscanf in lib x509 --- library/x509.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/library/x509.c b/library/x509.c index 3b6cd1bb1..46d88fa41 100644 --- a/library/x509.c +++ b/library/x509.c @@ -73,6 +73,8 @@ #endif #endif +#define CHECK(code) if( ( ret = code ) != 0 ){ return( ret ); } + /* * CertificateSerialNumber ::= INTEGER */ @@ -474,6 +476,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, @@ -484,7 +496,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 ) @@ -501,20 +512,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 ) @@ -525,17 +535,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