Make gmtime() configurable at compile-time
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index 9ee86ff..18fbf92 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 84f0732..5f26fb8 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 <stddef.h>
+#if defined(MBEDTLS_HAVE_TIME_DATE)
+#include <time.h>
+#endif /* MBEDTLS_HAVE_TIME_DATE */
 
 #ifdef __cplusplus
 extern "C" {
@@ -55,6 +66,38 @@
  */
 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 4cfaadd..0707152 100644
--- a/include/mbedtls/threading.h
+++ b/include/mbedtls/threading.h
@@ -103,9 +103,9 @@
 #if !defined(_WIN32) && (defined(__unix__) || \
     (defined(__APPLE__) && defined(__MACH__)))
 #include <unistd.h>
-#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 */