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