From 1abb368b8760569a53350f6d7f7cd628812f29d5 Mon Sep 17 00:00:00 2001 From: Andres Amaya Garcia Date: Thu, 16 Aug 2018 21:42:09 +0100 Subject: [PATCH] Make gmtime() configurable at compile-time --- include/mbedtls/config.h | 19 +++++++++++++ include/mbedtls/platform_util.h | 43 +++++++++++++++++++++++++++++ include/mbedtls/threading.h | 4 +-- library/platform_util.c | 49 +++++++++++++++++++++++++++++++++ library/threading.c | 4 +-- library/x509.c | 31 ++------------------- 6 files changed, 117 insertions(+), 33 deletions(-) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 9ee86ff24..18fbf92df 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -3091,6 +3091,25 @@ */ //#define MBEDTLS_PLATFORM_ZEROIZE_ALT +/** + * Uncomment the macro to let Mbed TLS use your alternate implementation of + * mbedtls_platform_gmtime(). This replaces the default implementation in + * platform_util.c. + * + * gmtime() is not a thread safe function as defined in the C standard. The + * library will try to use safer implementations of this function, such as + * gmtime_r() when available. However, if Mbed TLS cannot identify the target + * system, the implementation of mbedtls_platform_gmtime() will default to + * using the standard gmtime(). In this case, calls from the library to + * gmtime() will be guarded by the global mutex mbedtls_threading_gmtime_mutex + * if MBEDTLS_THREADING_C is enable. It is advised that calls from outside the + * library are also guarded with this mutex to avoid race conditions. However, + * if the macro MBEDTLS_PLATFORM_GMTIME_ALT is defined, Mbed TLS will + * unconditionally use the implementation for mbedtls_platform_time() supplied + * at compile time. + */ +//#define MBEDTLS_PLATFORM_GMTIME_ALT + /* \} name SECTION: Customisation configuration options */ /* Target and application specific configurations */ diff --git a/include/mbedtls/platform_util.h b/include/mbedtls/platform_util.h index 84f0732ee..5f26fb82c 100644 --- a/include/mbedtls/platform_util.h +++ b/include/mbedtls/platform_util.h @@ -25,7 +25,18 @@ #ifndef MBEDTLS_PLATFORM_UTIL_H #define MBEDTLS_PLATFORM_UTIL_H +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include "mbedtls/platform_time.h" + #include +#if defined(MBEDTLS_HAVE_TIME_DATE) +#include +#endif /* MBEDTLS_HAVE_TIME_DATE */ #ifdef __cplusplus extern "C" { @@ -55,6 +66,38 @@ extern "C" { */ void mbedtls_platform_zeroize( void *buf, size_t len ); +#if defined(MBEDTLS_HAVE_TIME_DATE) +/** + * \brief Thread safe implementation of gmtime() + * + * The function is an abstraction that when called behaves similar + * to the gmtime() function from the C standard, but is thread + * safe. + * + * Mbed TLS will try to identify the underlying platform and + * configure an appropriate underlying implementation (e.g. + * gmtime_r() for POSIX and gmtime_s() for Windows). If this is + * not possible, then gmtime() will be used. In this case, calls + * from the library to gmtime() will be guarded by the mutex + * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is + * enabled. It is recommended that calls from outside the library + * are also guarded by this mutex. + * + * If MBEDTLS_PLATFORM_GMTIME_ALT is defined, then Mbed TLS will + * unconditionally use the alternative implementation for + * mbedtls_platform_gmtime() supplied by the user at compile time + * + * \param tt Pointer to an object containing time (in seconds) since the + * Epoc to be converted + * \param tm Pointer to an object where the results will be stored + * + * \return Pointer to an object of type struct tm on success, otherwise + * NULL + */ +struct tm *mbedtls_platform_gmtime( const mbedtls_time_t *tt, + struct tm *tm_buf ); +#endif /* MBEDTLS_HAVE_TIME_DATE */ + #ifdef __cplusplus } #endif diff --git a/include/mbedtls/threading.h b/include/mbedtls/threading.h index 4cfaadde2..070715259 100644 --- a/include/mbedtls/threading.h +++ b/include/mbedtls/threading.h @@ -103,9 +103,9 @@ extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex; #if !defined(_WIN32) && (defined(__unix__) || \ (defined(__APPLE__) && defined(__MACH__))) #include -#if !defined(_POSIX_VERSION) +#if !defined(_POSIX_VERSION) || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex; -#endif /* !_POSIX_VERSION */ +#endif /* !_POSIX_VERSION || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS */ #endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */ #endif /* MBEDTLS_HAVE_TIME_DATE */ #endif /* MBEDTLS_THREADING_C */ diff --git a/library/platform_util.c b/library/platform_util.c index 1a57de939..e41f3c49c 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -20,6 +20,12 @@ * This file is part of Mbed TLS (https://tls.mbed.org) */ +/* + * Ensure gmtime_r is available even with -std=c99; must be included before + * config.h, which pulls in glibc's features.h. Harmless on other platforms. + */ +#define _POSIX_C_SOURCE 200112L + #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else @@ -27,6 +33,7 @@ #endif #include "mbedtls/platform_util.h" +#include "mbedtls/threading.h" #include #include @@ -65,3 +72,45 @@ void mbedtls_platform_zeroize( void *buf, size_t len ) memset_func( buf, 0, len ); } #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */ + +#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_ALT) +#include +#if !defined(_WIN32) && (defined(__unix__) || \ + (defined(__APPLE__) && defined(__MACH__))) +#include +#if !defined(_POSIX_VERSION) || _POSIX_C_SOURCE > _POSIX_THREAD_SAFE_FUNCTIONS +#define PLATFORM_UTIL_USE_GMTIME +#endif /* !_POSIX_VERSION || _POSIX_C_SOURCE > _POSIX_THREAD_SAFE_FUNCTIONS */ +#endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */ + +struct tm *mbedtls_platform_gmtime( const mbedtls_time_t *tt, + struct tm *tm_buf ) +{ +#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) + return ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL; +#elif !defined(PLATFORM_UTIL_USE_GMTIME) + return gmtime_r( tt, tm_buf ); +#else + struct tm *lt; + +#if defined(MBEDTLS_THREADING_C) + if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 ) + return( NULL ); +#endif /* MBEDTLS_THREADING_C */ + + lt = gmtime( tt ); + + if( lt != NULL ) + { + memcpy( tm_buf, lt, sizeof( struct tm ) ); + } + +#if defined(MBEDTLS_THREADING_C) + if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 ) + return( NULL ); +#endif /* MBEDTLS_THREADING_C */ + + return ( lt == NULL ) ? NULL : tm_buf; +#endif +} +#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_ALT */ diff --git a/library/threading.c b/library/threading.c index 95ae8d144..3d7f61b2e 100644 --- a/library/threading.c +++ b/library/threading.c @@ -32,9 +32,9 @@ #if !defined(_WIN32) && (defined(__unix__) || \ (defined(__APPLE__) && defined(__MACH__))) #include -#if !defined(_POSIX_VERSION) +#if !defined(_POSIX_VERSION) || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS #define THREADING_USE_GMTIME -#endif /* !_POSIX_VERSION */ +#endif /* !_POSIX_VERSION || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS */ #endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */ #if defined(MBEDTLS_THREADING_PTHREAD) diff --git a/library/x509.c b/library/x509.c index 03c3bbe1d..15c0123c3 100644 --- a/library/x509.c +++ b/library/x509.c @@ -29,10 +29,6 @@ * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf */ -/* Ensure gmtime_r is available even with -std=c99; must be included before - * config.h, which pulls in glibc's features.h. Harmless on other platforms. */ -#define _POSIX_C_SOURCE 200112L - #if !defined(MBEDTLS_CONFIG_FILE) #include "mbedtls/config.h" #else @@ -67,6 +63,7 @@ #include "mbedtls/platform_time.h" #endif #if defined(MBEDTLS_HAVE_TIME_DATE) +#include "mbedtls/platform_util.h" #include #endif @@ -890,14 +887,6 @@ int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name ) } #if defined(MBEDTLS_HAVE_TIME_DATE) -#if !defined(_WIN32) && (defined(__unix__) || \ - (defined(__APPLE__) && defined(__MACH__))) -#include -#if !defined(_POSIX_VERSION) -#define X509_USE_GMTIME -#endif /* !_POSIX_VERSION */ -#endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */ - /* * Set the time structure to the current time. * Return 0 on success, non-zero on failure. @@ -910,19 +899,8 @@ static int x509_get_current_time( mbedtls_x509_time *now ) (void)tm_buf; -#if defined(MBEDTLS_THREADING_C) && defined(X509_USE_GMTIME) - if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 ) - return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); -#endif /* MBEDTLS_THREADING_C && X509_USE_GMTIME */ - tt = mbedtls_time( NULL ); -#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) - lt = gmtime_s( &tm_buf, &tt ) == 0 ? &tm_buf : NULL; -#elif defined(_POSIX_VERSION) - lt = gmtime_r( &tt, &tm_buf ); -#else - lt = gmtime( &tt ); -#endif + lt = mbedtls_platform_gmtime( &tt, &tm_buf ); if( lt == NULL ) ret = -1; @@ -936,11 +914,6 @@ static int x509_get_current_time( mbedtls_x509_time *now ) now->sec = lt->tm_sec; } -#if defined(MBEDTLS_THREADING_C) && defined(X509_USE_GMTIME) - if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 ) - return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); -#endif /* MBEDTLS_THREADING_C && X509_USE_GMTIME */ - return( ret ); }