summaryrefslogtreecommitdiff
path: root/library/platform_util.c
diff options
context:
space:
mode:
authorAndres Amaya Garcia <Andres.AmayaGarcia@arm.com>2018-08-16 21:42:09 +0100
committerAndres Amaya Garcia <Andres.AmayaGarcia@arm.com>2018-08-16 21:42:09 +0100
commit1abb368b8760569a53350f6d7f7cd628812f29d5 (patch)
treece8b89995991306b33598a6eda0de335790b5e0c /library/platform_util.c
parentd7177435e3eb9ec7c1c34e16da9b6385003543e9 (diff)
Make gmtime() configurable at compile-time
Diffstat (limited to 'library/platform_util.c')
-rw-r--r--library/platform_util.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/library/platform_util.c b/library/platform_util.c
index 1a57de93..e41f3c49 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 <stddef.h>
#include <string.h>
@@ -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 <time.h>
+#if !defined(_WIN32) && (defined(__unix__) || \
+ (defined(__APPLE__) && defined(__MACH__)))
+#include <unistd.h>
+#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 */