Commit a752d878 authored by Manuel Pégourié-Gonnard's avatar Manuel Pégourié-Gonnard

Merge branch 'snprintf' into development

* snprintf:
  Rationalize other snprintf() uses
  Rationalize snprintf() usage in X.509 modules
  Add tests for snprintf
  Include fixed snprintf for Windows in platform.c
parents bbc60db2 9dbaf400
......@@ -39,6 +39,10 @@
#error "mbed TLS requires a platform with 8-bit chars"
#endif
#if defined(_WIN32) && !defined(MBEDTLS_PLATFORM_C)
#error "MBEDTLS_PLATFORM_C is required on Windows"
#endif
#if defined(MBEDTLS_DEPRECATED_WARNING) && \
!defined(__GNUC__) && !defined(__clang__)
#error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang"
......
......@@ -142,10 +142,7 @@
*
* All these define require MBEDTLS_PLATFORM_C to be defined!
*
* WARNING: MBEDTLS_PLATFORM_SNPRINTF_ALT is not available on Windows
* for compatibility reasons.
*
* WARNING: MBEDTLS_PLATFORM_XXX_ALT cannot be defined at the same time as
* \warning MBEDTLS_PLATFORM_XXX_ALT cannot be defined at the same time as
* MBEDTLS_PLATFORM_XXX_MACRO!
*
* Uncomment a macro to enable alternate implementation of specific base
......@@ -2004,12 +2001,15 @@
* \def MBEDTLS_PLATFORM_C
*
* Enable the platform abstraction layer that allows you to re-assign
* functions like calloc(), free(), snprintf(), printf(), fprintf(), exit()
* functions like calloc(), free(), snprintf(), printf(), fprintf(), exit().
*
* Enabling MBEDTLS_PLATFORM_C enables to use of MBEDTLS_PLATFORM_XXX_ALT
* or MBEDTLS_PLATFORM_XXX_MACRO directives, allowing the functions mentioned
* above to be specified at runtime or compile time respectively.
*
* \note This abstraction layer must be enabled on Windows (including MSYS2)
* as other module rely on it for a fixed snprintf implementation.
*
* Module: library/platform.c
* Caller: Most other .c files
*
......@@ -2379,6 +2379,7 @@
//#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */
//#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */
/* Note: your snprintf must correclty zero-terminate the buffer! */
//#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use, can be undefined */
/* To Use Function Macros MBEDTLS_PLATFORM_C must be enabled */
......@@ -2388,6 +2389,7 @@
//#define MBEDTLS_PLATFORM_EXIT_MACRO exit /**< Default exit macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_FPRINTF_MACRO fprintf /**< Default fprintf macro to use, can be undefined */
//#define MBEDTLS_PLATFORM_PRINTF_MACRO printf /**< Default printf macro to use, can be undefined */
/* Note: your snprintf must correclty zero-terminate the buffer! */
//#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf /**< Default snprintf macro to use, can be undefined */
/* SSL Cache options */
......
......@@ -72,7 +72,7 @@
* Name ID Nr of Errors
* PEM 1 9
* PKCS#12 1 4 (Started from top)
* X509 2 18
* X509 2 19
* PKCS5 2 4 (Started from top)
* DHM 3 9
* PK 3 14 (Started from top)
......
......@@ -46,8 +46,12 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
#if defined(_WIN32)
#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
#else
#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
#endif
#endif
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
#endif
......@@ -150,7 +154,18 @@ int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
/*
* The function pointers for snprintf
*
* The snprintf implementation should conform to C99:
* - it *must* always correctly zero-terminate the buffer
* (except when n == 0, then it must leave the buffer untouched)
* - however it is acceptable to return -1 instead of the required length when
* the destination buffer is too short.
*/
#if defined(_WIN32)
/* For Windows (inc. MSYS2), we provide our own fixed implementation */
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
......
......@@ -76,6 +76,7 @@
#define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800 /**< Input invalid. */
#define MBEDTLS_ERR_X509_ALLOC_FAILED -0x2880 /**< Allocation of memory failed. */
#define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900 /**< Read/write of file failed. */
#define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL -0x2980 /**< Destination buffer is too small. */
/* \} name */
/**
......@@ -306,6 +307,15 @@ int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
const char *oid, size_t oid_len,
unsigned char *sig, size_t size );
#define MBEDTLS_X509_SAFE_SNPRINTF \
do { \
if( ret < 0 || (size_t) ret >= n ) \
return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL ); \
\
n -= (size_t) ret; \
p += (size_t) ret; \
} while( 0 )
#ifdef __cplusplus
}
#endif
......
......@@ -34,16 +34,6 @@
#include <stdio.h>
#include <string.h>
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
#if !defined snprintf
#define snprintf _snprintf
#endif
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif
#endif /* _MSC_VER */
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
......@@ -67,13 +57,15 @@ char *mbedtls_debug_fmt( const char *format, ... )
{
va_list argp;
static char str[512];
int maxlen = sizeof( str ) - 1;
va_start( argp, format );
vsnprintf( str, maxlen, format, argp );
#if defined(_WIN32)
_vsnprintf_s( str, sizeof( str ), _TRUNCATE, format, argp );
#else
vsnprintf( str, sizeof( str ), format, argp );
#endif
va_end( argp );
str[maxlen] = '\0';
return( str );
}
......@@ -81,7 +73,6 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *text )
{
char str[512];
int maxlen = sizeof( str ) - 1;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
......@@ -92,8 +83,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
return;
}
mbedtls_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(%04d): %s\n", file, line, text );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
......@@ -102,7 +92,6 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
const char *text, int ret )
{
char str[512];
int maxlen = sizeof( str ) - 1;
size_t idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
......@@ -117,12 +106,11 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s() returned %d (-0x%04x)\n",
text, ret, -ret );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
......@@ -132,18 +120,17 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
{
char str[512];
char txt[17];
size_t i, maxlen = sizeof( str ) - 1, idx = 0;
size_t i, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
text, (unsigned int) len );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -157,7 +144,7 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
{
if( i > 0 )
{
mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -165,14 +152,14 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
}
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
idx += mbedtls_snprintf( str + idx, maxlen - idx, "%04x: ",
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
(unsigned int) i );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x",
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
(unsigned int) buf[i] );
txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
}
......@@ -180,9 +167,9 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
if( len > 0 )
{
for( /* i = i */; i % 16 != 0; i++ )
idx += mbedtls_snprintf( str + idx, maxlen - idx, " " );
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
}
......@@ -193,17 +180,14 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_ecp_point *X )
{
char str[512];
int maxlen = sizeof( str ) - 1;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
mbedtls_snprintf( str, maxlen, "%s(X)", text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
mbedtls_snprintf( str, maxlen, "%s(Y)", text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
}
#endif /* MBEDTLS_ECP_C */
......@@ -214,7 +198,7 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_mpi *X )
{
char str[512];
int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
int j, k, zeros = 1;
size_t i, n, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
......@@ -229,12 +213,11 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
break;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -254,16 +237,16 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
{
if( j > 0 )
{
mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
}
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
j++;
......@@ -275,13 +258,13 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
{
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
{
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " 00" );
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
}
mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
#endif /* MBEDTLS_BIGNUM_C */
......@@ -328,33 +311,29 @@ void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_x509_crt *crt )
{
char str[1024], prefix[64];
int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
int i = 0, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
{
mbedtls_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
prefix[maxlen] = '\0';
mbedtls_snprintf( prefix, sizeof( prefix ), "%s(%04d): ", file, line );
}
else
prefix[0] = '\0';
maxlen = sizeof( str ) - 1;
while( crt != NULL )
{
char buf[1024];
mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s #%d:\n%s",
text, ++i, buf );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
......
......@@ -149,10 +149,6 @@
#include "mbedtls/xtea.h"
#endif
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#define snprintf _snprintf
#endif
void mbedtls_strerror( int ret, char *buf, size_t buflen )
{
......@@ -163,8 +159,6 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
return;
memset( buf, 0x00, buflen );
/* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
buflen -= 1;
if( ret < 0 )
ret = -ret;
......@@ -474,6 +468,8 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
mbedtls_snprintf( buf, buflen, "X509 - Allocation of memory failed" );
if( use_ret == -(MBEDTLS_ERR_X509_FILE_IO_ERROR) )
mbedtls_snprintf( buf, buflen, "X509 - Read/write of file failed" );
if( use_ret == -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) )
mbedtls_snprintf( buf, buflen, "X509 - Destination buffer is too small" );
#endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
// END generated code
......
......@@ -77,11 +77,6 @@ static int wsa_init_done = 0;
#include <stdlib.h>
#include <stdio.h>
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#define snprintf _snprintf
#endif
#include <time.h>
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
......
......@@ -598,55 +598,14 @@ FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, mbedtls_md_type_t, md_alg, mbedtls_cipher_type_t, cipher_alg)
#endif /* MBEDTLS_PKCS12_C */
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#include <stdarg.h>
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif // vsnprintf
/*
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
* Result value is not size of buffer needed, but -1 if no fit is possible.
*
* This fuction tries to 'fix' this by at least suggesting enlarging the
* size by 20.
*/
static int compat_snprintf( char *str, size_t size, const char *format, ... )
{
va_list ap;
int res = -1;
va_start( ap, format );
res = vsnprintf( str, size, format, ap );
va_end( ap );
// No quick fix possible
if( res < 0 )
return( (int) size + 20 );
return( res );
}
#define snprintf compat_snprintf
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
#define SAFE_SNPRINTF() \
{ \
if( ret == -1 ) \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
\
if( (unsigned int) ret >= n ) { \
p[n - 1] = '\0'; \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
} \
\
n -= (unsigned int) ret; \
p += (unsigned int) ret; \
}
#define OID_SAFE_SNPRINTF \
do { \
if( ret < 0 || (size_t) ret >= n ) \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
\
n -= (size_t) ret; \
p += (size_t) ret; \
} while( 0 )
/* Return the x.y.z.... style numeric string for the given OID */
int mbedtls_oid_get_numeric_string( char *buf, size_t size,
......@@ -664,7 +623,7 @@ int mbedtls_oid_get_numeric_string( char *buf, size_t size,
if( oid->len > 0 )
{
ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
SAFE_SNPRINTF();
OID_SAFE_SNPRINTF;
}
value = 0;
......@@ -681,7 +640,7 @@ int mbedtls_oid_get_numeric_string( char *buf, size_t size,
{
/* Last byte */
ret = mbedtls_snprintf( p, n, ".%d", value );
SAFE_SNPRINTF();
OID_SAFE_SNPRINTF;
value = 0;
}
}
......
......@@ -63,6 +63,21 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
}
#endif /* MBEDTLS_PLATFORM_MEMORY */
#if defined(_WIN32)
#include <stdarg.h>
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
{
int ret;
va_list argp;
va_start( argp, fmt );
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
va_end( argp );
return( ret );
}
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
/*
......
......@@ -662,58 +662,6 @@ int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
return( 0 );
}
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#include <stdarg.h>
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif // vsnprintf
/*
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
* Result value is not size of buffer needed, but -1 if no fit is possible.
*
* This fuction tries to 'fix' this by at least suggesting enlarging the
* size by 20.
*/
static int compat_snprintf( char *str, size_t size, const char *format, ... )
{
va_list ap;
int res = -1;
va_start( ap, format );
res = vsnprintf( str, size, format, ap );
va_end( ap );
// No quick fix possible
if( res < 0 )
return( (int) size + 20 );
return( res );
}
#define snprintf compat_snprintf
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
#define ERR_BUF_TOO_SMALL -2
#define SAFE_SNPRINTF() \
{ \
if( ret == -1 ) \
return( -1 ); \
\
if( (unsigned int) ret > n ) { \
p[n - 1] = '\0'; \
return( ERR_BUF_TOO_SMALL ); \
} \
\
n -= (unsigned int) ret; \
p += (unsigned int) ret; \
}
/*
* Store the name in printable form into buf; no more
* than size characters will be written
......@@ -744,7 +692,7 @@ int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
if( name != dn )
{
ret = mbedtls_snprintf( p, n, merge ? " + " : ", " );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
}
ret = mbedtls_oid_get_attr_short_name( &name->oid, &short_name );
......@@ -753,7 +701,7 @@ int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
ret = mbedtls_snprintf( p, n, "%s=", short_name );
else
ret = mbedtls_snprintf( p, n, "\?\?=" );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
for( i = 0; i < name->val.len; i++ )
{
......@@ -767,7 +715,7 @@ int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn )
}
s[i] = '\0';
ret = mbedtls_snprintf( p, n, "%s", s );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
merge = name->next_merged;
name = name->next;
......@@ -799,13 +747,13 @@ int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *se
ret = mbedtls_snprintf( p, n, "%02X%s",
serial->p[i], ( i < nr - 1 ) ? ":" : "" );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
}
if( nr != serial->len )
{
ret = mbedtls_snprintf( p, n, "...." );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
}
return( (int) ( size - n ) );
......@@ -828,7 +776,7 @@ int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *s
ret = mbedtls_snprintf( p, n, "???" );
else
ret = mbedtls_snprintf( p, n, "%s", desc );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
if( pk_alg == MBEDTLS_PK_RSASSA_PSS )
......@@ -845,7 +793,7 @@ int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *s
md_info ? mbedtls_md_get_name( md_info ) : "???",
mgf_md_info ? mbedtls_md_get_name( mgf_md_info ) : "???",
pss_opts->expected_salt_len );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
}
#else
((void) pk_alg);
......@@ -865,11 +813,8 @@ int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name )
size_t n = buf_size;
int ret;
if( strlen( name ) + sizeof( " key size" ) > buf_size )
return( ERR_BUF_TOO_SMALL );
ret = mbedtls_snprintf( p, n, "%s key size", name );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
return( 0 );
}
......
......@@ -569,58 +569,6 @@ int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path )
}
#endif /* MBEDTLS_FS_IO */
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#include <stdarg.h>
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif // vsnprintf
/*
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
* Result value is not size of buffer needed, but -1 if no fit is possible.
*
* This fuction tries to 'fix' this by at least suggesting enlarging the
* size by 20.
*/
static int compat_snprintf( char *str, size_t size, const char *format, ... )
{
va_list ap;
int res = -1;
va_start( ap, format );
res = vsnprintf( str, size, format, ap );
va_end( ap );
// No quick fix possible
if( res < 0 )
return( (int) size + 20 );
return( res );
}
#define snprintf compat_snprintf
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
#define ERR_BUF_TOO_SMALL -2
#define SAFE_SNPRINTF() \
{ \
if( ret == -1 ) \
return( -1 ); \
\
if( (unsigned int) ret > n ) { \
p[n - 1] = '\0'; \
return( ERR_BUF_TOO_SMALL ); \
} \
\
n -= (unsigned int) ret; \
p += (unsigned int) ret; \
}
/*
* Return an informational string about the certificate.
*/
......@@ -642,61 +590,61 @@ int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
ret = mbedtls_snprintf( p, n, "%sCRL version : %d",
prefix, crl->version );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_x509_dn_gets( p, n, &crl->issuer );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_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();
MBEDTLS_X509_SAFE_SNPRINTF;
ret = mbedtls_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,
crl->next_update.min, crl->next_update.sec );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;
entry = &crl->entry;
ret = mbedtls_snprintf( p, n, "\n%sRevoked certificates:",
prefix );
SAFE_SNPRINTF();
MBEDTLS_X509_SAFE_SNPRINTF;