• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Common and shared functions used by multiple modules in the Mbed TLS
3  * library.
4  *
5  *  Copyright The Mbed TLS Contributors
6  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7  */
8 
9 /*
10  * Ensure gmtime_r is available even with -std=c99; must be defined before
11  * config.h, which pulls in glibc's features.h. Harmless on other platforms.
12  */
13 #if !defined(_POSIX_C_SOURCE)
14 #define _POSIX_C_SOURCE 200112L
15 #endif
16 
17 #include "common.h"
18 
19 #include "mbedtls/platform_util.h"
20 #include "mbedtls/platform.h"
21 #include "mbedtls/threading.h"
22 
23 #include <stddef.h>
24 #include <string.h>
25 
26 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
27 /*
28  * This implementation should never be optimized out by the compiler
29  *
30  * This implementation for mbedtls_platform_zeroize() was inspired from Colin
31  * Percival's blog article at:
32  *
33  * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
34  *
35  * It uses a volatile function pointer to the standard memset(). Because the
36  * pointer is volatile the compiler expects it to change at
37  * any time and will not optimize out the call that could potentially perform
38  * other operations on the input buffer instead of just setting it to 0.
39  * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
40  * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
41  * details), optimizations of the following form are still possible:
42  *
43  * if( memset_func != memset )
44  *     memset_func( buf, 0, len );
45  *
46  * Note that it is extremely difficult to guarantee that
47  * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
48  * in a portable way. For this reason, Mbed TLS also provides the configuration
49  * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
50  * mbedtls_platform_zeroize() to use a suitable implementation for their
51  * platform and needs.
52  */
53 static void *(*const volatile memset_func)(void *, int, size_t) = memset;
54 
mbedtls_platform_zeroize(void * buf,size_t len)55 void mbedtls_platform_zeroize(void *buf, size_t len)
56 {
57     MBEDTLS_INTERNAL_VALIDATE(len == 0 || buf != NULL);
58 
59     if (len > 0) {
60         memset_func(buf, 0, len);
61     }
62 }
63 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
64 
65 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
66 #include <time.h>
67 #if !defined(_WIN32) && (defined(unix) || \
68     defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
69     defined(__MACH__)))
70 #include <unistd.h>
71 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
72         * (__APPLE__ && __MACH__)) */
73 
74 #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) ||     \
75     (defined(_POSIX_THREAD_SAFE_FUNCTIONS) &&                     \
76     _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
77 /*
78  * This is a convenience shorthand macro to avoid checking the long
79  * preprocessor conditions above. Ideally, we could expose this macro in
80  * platform_util.h and simply use it in platform_util.c, threading.c and
81  * threading.h. However, this macro is not part of the Mbed TLS public API, so
82  * we keep it private by only defining it in this file
83  */
84 #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
85 #define PLATFORM_UTIL_USE_GMTIME
86 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
87 
88 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
89              ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
90                 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
91 
mbedtls_platform_gmtime_r(const mbedtls_time_t * tt,struct tm * tm_buf)92 struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
93                                      struct tm *tm_buf)
94 {
95 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
96     return (gmtime_s(tm_buf, tt) == 0) ? tm_buf : NULL;
97 #elif !defined(PLATFORM_UTIL_USE_GMTIME)
98     return gmtime_r(tt, tm_buf);
99 #else
100     struct tm *lt;
101 
102 #if defined(MBEDTLS_THREADING_C)
103     if (mbedtls_mutex_lock(&mbedtls_threading_gmtime_mutex) != 0) {
104         return NULL;
105     }
106 #endif /* MBEDTLS_THREADING_C */
107 
108     lt = gmtime(tt);
109 
110     if (lt != NULL) {
111         memcpy(tm_buf, lt, sizeof(struct tm));
112     }
113 
114 #if defined(MBEDTLS_THREADING_C)
115     if (mbedtls_mutex_unlock(&mbedtls_threading_gmtime_mutex) != 0) {
116         return NULL;
117     }
118 #endif /* MBEDTLS_THREADING_C */
119 
120     return (lt == NULL) ? NULL : tm_buf;
121 #endif /* _WIN32 && !EFIX64 && !EFI32 */
122 }
123 #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
124