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 * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms
12 * except OpenBSD, where it stops us accessing explicit_bzero.
13 */
14 #if !defined(_POSIX_C_SOURCE) && !defined(__OpenBSD__)
15 #define _POSIX_C_SOURCE 200112L
16 #endif
17
18 #if !defined(_GNU_SOURCE)
19 /* Clang requires this to get support for explicit_bzero */
20 #define _GNU_SOURCE
21 #endif
22
23 #include "common.h"
24
25 #include "mbedtls/platform_util.h"
26 #include "mbedtls/platform.h"
27 #include "mbedtls/threading.h"
28
29 #include <stddef.h>
30
31 #ifndef __STDC_WANT_LIB_EXT1__
32 #define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() and memset_s() if available */
33 #endif
34 #include <string.h>
35
36 #if defined(_WIN32)
37 #include <windows.h>
38 #endif
39
40 // Detect platforms known to support explicit_bzero()
41 #if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
42 #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
43 #elif (defined(__FreeBSD__) && (__FreeBSD_version >= 1100037)) || defined(__OpenBSD__)
44 #define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
45 #endif
46
47 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
48
49 #undef HAVE_MEMORY_SANITIZER
50 #if defined(__has_feature)
51 #if __has_feature(memory_sanitizer)
52 #include <sanitizer/msan_interface.h>
53 #define HAVE_MEMORY_SANITIZER
54 #endif
55 #endif
56
57 /*
58 * Where possible, we try to detect the presence of a platform-provided
59 * secure memset, such as explicit_bzero(), that is safe against being optimized
60 * out, and use that.
61 *
62 * For other platforms, we provide an implementation that aims not to be
63 * optimized out by the compiler.
64 *
65 * This implementation for mbedtls_platform_zeroize() was inspired from Colin
66 * Percival's blog article at:
67 *
68 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
69 *
70 * It uses a volatile function pointer to the standard memset(). Because the
71 * pointer is volatile the compiler expects it to change at
72 * any time and will not optimize out the call that could potentially perform
73 * other operations on the input buffer instead of just setting it to 0.
74 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
75 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
76 * details), optimizations of the following form are still possible:
77 *
78 * if (memset_func != memset)
79 * memset_func(buf, 0, len);
80 *
81 * Note that it is extremely difficult to guarantee that
82 * the memset() call will not be optimized out by aggressive compilers
83 * in a portable way. For this reason, Mbed TLS also provides the configuration
84 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
85 * mbedtls_platform_zeroize() to use a suitable implementation for their
86 * platform and needs.
87 */
88 #if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !(defined(__STDC_LIB_EXT1__) && \
89 !defined(__IAR_SYSTEMS_ICC__)) \
90 && !defined(_WIN32)
91 void *(*const volatile memset_func)(void *, int, size_t) = memset;
92 #endif
93
mbedtls_platform_zeroize(void * buf,size_t len)94 void mbedtls_platform_zeroize(void *buf, size_t len)
95 {
96 MBEDTLS_INTERNAL_VALIDATE(len == 0 || buf != NULL);
97
98 if (len > 0) {
99 #if defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO)
100 explicit_bzero(buf, len);
101 #if defined(HAVE_MEMORY_SANITIZER)
102 /* You'd think that Msan would recognize explicit_bzero() as
103 * equivalent to bzero(), but it actually doesn't on several
104 * platforms, including Linux (Ubuntu 20.04).
105 * https://github.com/google/sanitizers/issues/1507
106 * https://github.com/openssh/openssh-portable/commit/74433a19bb6f4cef607680fa4d1d7d81ca3826aa
107 */
108 __msan_unpoison(buf, len);
109 #endif
110 #elif defined(__STDC_LIB_EXT1__) && !defined(__IAR_SYSTEMS_ICC__)
111 memset_s(buf, len, 0, len);
112 #elif defined(_WIN32)
113 SecureZeroMemory(buf, len);
114 #else
115 memset_func(buf, 0, len);
116 #endif
117
118 #if defined(__GNUC__)
119 /* For clang and recent gcc, pretend that we have some assembly that reads the
120 * zero'd memory as an additional protection against being optimised away. */
121 #if defined(__clang__) || (__GNUC__ >= 10)
122 #if defined(__clang__)
123 #pragma clang diagnostic push
124 #pragma clang diagnostic ignored "-Wvla"
125 #elif defined(MBEDTLS_COMPILER_IS_GCC)
126 #pragma GCC diagnostic push
127 #pragma GCC diagnostic ignored "-Wvla"
128 #endif
129 asm volatile ("" : : "m" (*(char (*)[len]) buf) :);
130 #if defined(__clang__)
131 #pragma clang diagnostic pop
132 #elif defined(MBEDTLS_COMPILER_IS_GCC)
133 #pragma GCC diagnostic pop
134 #endif
135 #endif
136 #endif
137 }
138 }
139 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
140
mbedtls_zeroize_and_free(void * buf,size_t len)141 void mbedtls_zeroize_and_free(void *buf, size_t len)
142 {
143 if (buf != NULL) {
144 mbedtls_platform_zeroize(buf, len);
145 }
146
147 mbedtls_free(buf);
148 }
149
150 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
151 #include <time.h>
152 #if !defined(_WIN32) && (defined(unix) || \
153 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
154 defined(__MACH__)))
155 #include <unistd.h>
156 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
157 * (__APPLE__ && __MACH__)) */
158
159 #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
160 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
161 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
162 /*
163 * This is a convenience shorthand macro to avoid checking the long
164 * preprocessor conditions above. Ideally, we could expose this macro in
165 * platform_util.h and simply use it in platform_util.c, threading.c and
166 * threading.h. However, this macro is not part of the Mbed TLS public API, so
167 * we keep it private by only defining it in this file
168 */
169 #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) || \
170 (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
171 #define PLATFORM_UTIL_USE_GMTIME
172 #endif
173
174 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
175 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
176 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
177
mbedtls_platform_gmtime_r(const mbedtls_time_t * tt,struct tm * tm_buf)178 struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
179 struct tm *tm_buf)
180 {
181 #if defined(_WIN32) && !defined(PLATFORM_UTIL_USE_GMTIME)
182 #if defined(__STDC_LIB_EXT1__)
183 return (gmtime_s(tt, tm_buf) == 0) ? NULL : tm_buf;
184 #else
185 /* MSVC and mingw64 argument order and return value are inconsistent with the C11 standard */
186 return (gmtime_s(tm_buf, tt) == 0) ? tm_buf : NULL;
187 #endif
188 #elif !defined(PLATFORM_UTIL_USE_GMTIME)
189 return gmtime_r(tt, tm_buf);
190 #else
191 struct tm *lt;
192
193 #if defined(MBEDTLS_THREADING_C)
194 if (mbedtls_mutex_lock(&mbedtls_threading_gmtime_mutex) != 0) {
195 return NULL;
196 }
197 #endif /* MBEDTLS_THREADING_C */
198
199 lt = gmtime(tt);
200
201 if (lt != NULL) {
202 memcpy(tm_buf, lt, sizeof(struct tm));
203 }
204
205 #if defined(MBEDTLS_THREADING_C)
206 if (mbedtls_mutex_unlock(&mbedtls_threading_gmtime_mutex) != 0) {
207 return NULL;
208 }
209 #endif /* MBEDTLS_THREADING_C */
210
211 return (lt == NULL) ? NULL : tm_buf;
212 #endif /* _WIN32 && !EFIX64 && !EFI32 */
213 }
214 #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
215
216 #if defined(MBEDTLS_TEST_HOOKS)
217 void (*mbedtls_test_hook_test_fail)(const char *, int, const char *);
218 #endif /* MBEDTLS_TEST_HOOKS */
219
220 /*
221 * Provide external definitions of some inline functions so that the compiler
222 * has the option to not inline them
223 */
224 extern inline void mbedtls_xor(unsigned char *r,
225 const unsigned char *a,
226 const unsigned char *b,
227 size_t n);
228
229 extern inline uint16_t mbedtls_get_unaligned_uint16(const void *p);
230
231 extern inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x);
232
233 extern inline uint32_t mbedtls_get_unaligned_uint32(const void *p);
234
235 extern inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x);
236
237 extern inline uint64_t mbedtls_get_unaligned_uint64(const void *p);
238
239 extern inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x);
240
241 #if defined(MBEDTLS_HAVE_TIME) && !defined(MBEDTLS_PLATFORM_MS_TIME_ALT)
242
243 #include <time.h>
244 #if !defined(_WIN32) && \
245 (defined(unix) || defined(__unix) || defined(__unix__) || \
246 (defined(__APPLE__) && defined(__MACH__)))
247 #include <unistd.h>
248 #endif /* !_WIN32 && (unix || __unix || __unix__ || (__APPLE__ && __MACH__)) */
249 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 199309L)
mbedtls_ms_time(void)250 mbedtls_ms_time_t mbedtls_ms_time(void)
251 {
252 int ret;
253 struct timespec tv;
254 mbedtls_ms_time_t current_ms;
255
256 #if defined(__linux__)
257 ret = clock_gettime(CLOCK_BOOTTIME, &tv);
258 #else
259 ret = clock_gettime(CLOCK_MONOTONIC, &tv);
260 #endif
261 if (ret) {
262 return time(NULL) * 1000;
263 }
264
265 current_ms = tv.tv_sec;
266
267 return current_ms*1000 + tv.tv_nsec / 1000000;
268 }
269 #elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
270 defined(__MINGW32__) || defined(_WIN64)
271 #include <windows.h>
mbedtls_ms_time(void)272 mbedtls_ms_time_t mbedtls_ms_time(void)
273 {
274 FILETIME ct;
275 mbedtls_ms_time_t current_ms;
276
277 GetSystemTimeAsFileTime(&ct);
278 current_ms = ((mbedtls_ms_time_t) ct.dwLowDateTime +
279 ((mbedtls_ms_time_t) (ct.dwHighDateTime) << 32LL))/10000;
280 return current_ms;
281 }
282 #else
283 #error "No mbedtls_ms_time available"
284 #endif
285 #endif /* MBEDTLS_HAVE_TIME && !MBEDTLS_PLATFORM_MS_TIME_ALT */
286