Use gmtime when target is not windows or posix

This commit is contained in:
Andres Amaya Garcia 2018-08-07 20:26:55 +01:00
parent b363382ba4
commit ce6eebb0b8
3 changed files with 49 additions and 1 deletions

View File

@ -99,6 +99,15 @@ extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
#if defined(MBEDTLS_FS_IO)
extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
#endif
#if defined(MBEDTLS_HAVE_TIME_DATE)
#if !defined(_WIN32) && (defined(__unix__) || \
(defined(__APPLE__) && defined(__MACH__)))
#include <unistd.h>
#if !defined(_POSIX_VERSION)
extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
#endif /* !_POSIX_VERSION */
#endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */
#endif /* MBEDTLS_HAVE_TIME_DATE */
#endif /* MBEDTLS_THREADING_C */
#ifdef __cplusplus

View File

@ -29,6 +29,14 @@
#include "mbedtls/threading.h"
#if !defined(_WIN32) && (defined(__unix__) || \
(defined(__APPLE__) && defined(__MACH__)))
#include <unistd.h>
#if !defined(_POSIX_VERSION)
#define MBEDTLS_THREADING_USE_GMTIME
#endif /* !_POSIX_VERSION */
#endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */
#if defined(MBEDTLS_THREADING_PTHREAD)
static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
{
@ -114,6 +122,9 @@ void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t *
#if defined(MBEDTLS_FS_IO)
mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
#endif
#if defined(MBEDTLS_THREADING_USE_GMTIME)
mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
#endif
}
/*
@ -124,6 +135,9 @@ void mbedtls_threading_free_alt( void )
#if defined(MBEDTLS_FS_IO)
mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
#endif
#if defined(MBEDTLS_THREADING_USE_GMTIME)
mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
#endif
}
#endif /* MBEDTLS_THREADING_ALT */
@ -136,5 +150,8 @@ void mbedtls_threading_free_alt( void )
#if defined(MBEDTLS_FS_IO)
mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
#endif
#if defined(MBEDTLS_THREADING_USE_GMTIME)
mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
#endif
#endif /* MBEDTLS_THREADING_C */

View File

@ -890,6 +890,14 @@ 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 <unistd.h>
#if !defined(_POSIX_VERSION)
#define MBEDTLS_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.
@ -900,11 +908,20 @@ static int x509_get_current_time( mbedtls_x509_time *now )
mbedtls_time_t tt;
int ret = 0;
(void)tm_buf;
#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_X509_USE_GMTIME)
if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif /* MBEDTLS_THREADING_C && MBEDTLS_X509_USE_GMTIME */
tt = mbedtls_time( NULL );
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
lt = gmtime_s( &tm_buf, &tt ) == 0 ? &tm_buf : NULL;
#else
#elif defined(_POSIX_VERSION)
lt = gmtime_r( &tt, &tm_buf );
#else
lt = gmtime( &tt );
#endif
if( lt == NULL )
@ -919,6 +936,11 @@ static int x509_get_current_time( mbedtls_x509_time *now )
now->sec = lt->tm_sec;
}
#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_X509_USE_GMTIME)
if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
#endif /* MBEDTLS_THREADING_C && MBEDTLS_X509_USE_GMTIME */
return( ret );
}