blob: 4a9a7491c9975bcf56122e69d136004027733ec4 [file] [log] [blame]
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01001/*
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -05002 * Common and shared functions used by multiple modules in the Mbed TLS
3 * library.
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010019 */
20
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010021/*
Hanno Becker48a816f2018-09-05 15:22:22 +010022 * Ensure gmtime_r is available even with -std=c99; must be defined before
Tom Cosgrove503d7172023-03-07 12:51:11 +000023 * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms
24 * except OpenBSD, where it stops us accessing explicit_bzero.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010025 */
Tom Cosgrove503d7172023-03-07 12:51:11 +000026#if !defined(_POSIX_C_SOURCE) && !defined(__OpenBSD__)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010027#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010028#endif
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010029
Dave Rodgmanf5e531a2023-02-25 17:17:15 +000030#if !defined(_GNU_SOURCE)
Dave Rodgman703f8052023-02-25 15:19:52 +000031/* Clang requires this to get support for explicit_bzero */
32#define _GNU_SOURCE
33#endif
34
Gilles Peskinedb09ef62020-06-03 01:43:33 +020035#include "common.h"
Andres Amaya Garciab1262a32017-10-25 09:51:14 +010036
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050038#include "mbedtls/platform.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010039#include "mbedtls/threading.h"
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010040
41#include <stddef.h>
Dave Rodgmanf55182d2023-02-24 17:42:43 +000042
43#ifndef __STDC_WANT_LIB_EXT1__
Dave Rodgmana6fda162023-02-24 19:00:38 +000044#define __STDC_WANT_LIB_EXT1__ 1 /* Ask for the C11 gmtime_s() and memset_s() if available */
Dave Rodgmanf55182d2023-02-24 17:42:43 +000045#endif
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010046#include <string.h>
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010047
Dave Rodgmanf55182d2023-02-24 17:42:43 +000048#if defined(_WIN32)
Dave Rodgman096e7292023-02-25 17:17:35 +000049#include <windows.h>
Dave Rodgmanf55182d2023-02-24 17:42:43 +000050#endif
51
52// Detect platforms known to support explicit_bzero()
Dave Rodgmanb0d96a22023-03-03 17:06:09 +000053#if defined(__GLIBC__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)
Dave Rodgmanf55182d2023-02-24 17:42:43 +000054#define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
Tom Cosgrove503d7172023-03-07 12:51:11 +000055#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 1100037)) || defined(__OpenBSD__)
Dave Rodgmanf55182d2023-02-24 17:42:43 +000056#define MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO 1
57#endif
Dave Rodgmanf55182d2023-02-24 17:42:43 +000058
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050059#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
Gilles Peskinea8d2ff32023-03-16 10:53:46 +010060
61#undef HAVE_MEMORY_SANITIZER
62#if defined(__has_feature)
63#if __has_feature(memory_sanitizer)
64#include <sanitizer/msan_interface.h>
65#define HAVE_MEMORY_SANITIZER
66#endif
67#endif
68
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010069/*
Dave Rodgman4daca632023-02-24 17:43:00 +000070 * Where possible, we try to detect the presence of a platform-provided
71 * secure memset, such as explicit_bzero(), that is safe against being optimized
72 * out, and use that.
73 *
74 * For other platforms, we provide an implementation that aims not to be
75 * optimized out by the compiler.
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010076 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050077 * This implementation for mbedtls_platform_zeroize() was inspired from Colin
78 * Percival's blog article at:
Andres Amaya Garcia1e8ea5f2018-03-08 20:46:39 +000079 *
80 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
81 *
82 * It uses a volatile function pointer to the standard memset(). Because the
83 * pointer is volatile the compiler expects it to change at
84 * any time and will not optimize out the call that could potentially perform
85 * other operations on the input buffer instead of just setting it to 0.
86 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
87 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
88 * details), optimizations of the following form are still possible:
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010089 *
Dave Rodgman8b6eded2023-02-24 18:07:05 +000090 * if (memset_func != memset)
Dave Rodgman4daca632023-02-24 17:43:00 +000091 * memset_func(buf, 0, len);
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010092 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050093 * Note that it is extremely difficult to guarantee that
Dave Rodgman4daca632023-02-24 17:43:00 +000094 * the memset() call will not be optimized out by aggressive compilers
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050095 * in a portable way. For this reason, Mbed TLS also provides the configuration
96 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
97 * mbedtls_platform_zeroize() to use a suitable implementation for their
98 * platform and needs.
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010099 */
Dave Rodgman528bfa62023-03-02 13:54:43 +0000100#if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !defined(__STDC_LIB_EXT1__) \
Dave Rodgman703f8052023-02-25 15:19:52 +0000101 && !defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static void *(*const volatile memset_func)(void *, int, size_t) = memset;
Dave Rodgman703f8052023-02-25 15:19:52 +0000103#endif
Andres Amaya Garciaecd18912017-10-26 22:43:41 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void mbedtls_platform_zeroize(void *buf, size_t len)
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_INTERNAL_VALIDATE(len == 0 || buf != NULL);
Vikas Katariya52fa1742019-08-15 11:59:08 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (len > 0) {
Dave Rodgmanf55182d2023-02-24 17:42:43 +0000110#if defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO)
111 explicit_bzero(buf, len);
Gilles Peskinea8d2ff32023-03-16 10:53:46 +0100112#if defined(HAVE_MEMORY_SANITIZER)
113 /* You'd think that Msan would recognize explicit_bzero() as
114 * equivalent to bzero(), but it actually doesn't on several
115 * platforms, including Linux (Ubuntu 20.04).
116 * https://github.com/google/sanitizers/issues/1507
117 * https://github.com/openssh/openssh-portable/commit/74433a19bb6f4cef607680fa4d1d7d81ca3826aa
118 */
119 __msan_unpoison(buf, len);
120#endif
Dave Rodgman8a7d26f2023-02-24 18:19:08 +0000121#elif defined(__STDC_LIB_EXT1__)
Dave Rodgmanf55182d2023-02-24 17:42:43 +0000122 memset_s(buf, len, 0, len);
123#elif defined(_WIN32)
124 SecureZeroMemory(buf, len);
125#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memset_func(buf, 0, len);
Dave Rodgmanf55182d2023-02-24 17:42:43 +0000127#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +0100129}
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500130#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100131
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100132void mbedtls_zeroize_and_free(void *buf, size_t len)
133{
134 if (buf != NULL) {
135 mbedtls_platform_zeroize(buf, len);
136 }
137
138 mbedtls_free(buf);
139}
140
Hanno Becker6a739782018-09-05 15:06:19 +0100141#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100142#include <time.h>
Hanno Beckercfeb70c2018-09-05 13:50:22 +0100143#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +0100144 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
145 defined(__MACH__)))
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100146#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +0100147#endif /* !_WIN32 && (unix || __unix || __unix__ ||
148 * (__APPLE__ && __MACH__)) */
Hanno Becker6f705812018-09-06 09:06:33 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
151 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
152 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100153/*
154 * This is a convenience shorthand macro to avoid checking the long
155 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +0100156 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100157 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +0100158 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) || \
161 (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100162#define PLATFORM_UTIL_USE_GMTIME
Dave Rodgmanad8dc482022-05-06 12:59:54 +0100163#endif
Hanno Beckerf5106d52018-09-06 12:09:56 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
166 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jonesd4603232020-11-12 17:10:36 +0000167 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
170 struct tm *tm_buf)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100171{
Cameron Cawleyea5496c2021-03-08 23:29:26 +0000172#if defined(_WIN32) && !defined(PLATFORM_UTIL_USE_GMTIME)
Dave Rodgman4bfb0072022-05-06 12:58:00 +0100173#if defined(__STDC_LIB_EXT1__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return (gmtime_s(tt, tm_buf) == 0) ? NULL : tm_buf;
Dave Rodgman4bfb0072022-05-06 12:58:00 +0100175#else
176 /* MSVC and mingw64 argument order and return value are inconsistent with the C11 standard */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return (gmtime_s(tm_buf, tt) == 0) ? tm_buf : NULL;
Dave Rodgman4bfb0072022-05-06 12:58:00 +0100178#endif
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100179#elif !defined(PLATFORM_UTIL_USE_GMTIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 return gmtime_r(tt, tm_buf);
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100181#else
182 struct tm *lt;
183
184#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (mbedtls_mutex_lock(&mbedtls_threading_gmtime_mutex) != 0) {
186 return NULL;
187 }
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100188#endif /* MBEDTLS_THREADING_C */
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 lt = gmtime(tt);
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (lt != NULL) {
193 memcpy(tm_buf, lt, sizeof(struct tm));
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100194 }
195
196#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (mbedtls_mutex_unlock(&mbedtls_threading_gmtime_mutex) != 0) {
198 return NULL;
199 }
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100200#endif /* MBEDTLS_THREADING_C */
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 return (lt == NULL) ? NULL : tm_buf;
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100203#endif /* _WIN32 && !EFIX64 && !EFI32 */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100204}
Hanno Becker6a739782018-09-05 15:06:19 +0100205#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
TRodziewicz829fe702021-07-13 12:23:12 +0200206
207#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100208void (*mbedtls_test_hook_test_fail)(const char *, int, const char *);
TRodziewicz829fe702021-07-13 12:23:12 +0200209#endif /* MBEDTLS_TEST_HOOKS */
210
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000211/*
Dave Rodgman66433442022-11-24 20:07:39 +0000212 * Provide external definitions of some inline functions so that the compiler
213 * has the option to not inline them
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215extern inline void mbedtls_xor(unsigned char *r,
216 const unsigned char *a,
217 const unsigned char *b,
218 size_t n);
Dave Rodgman66433442022-11-24 20:07:39 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220extern inline uint16_t mbedtls_get_unaligned_uint16(const void *p);
Dave Rodgmana360e192022-11-28 14:44:05 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222extern inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x);
Dave Rodgmana360e192022-11-28 14:44:05 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224extern inline uint32_t mbedtls_get_unaligned_uint32(const void *p);
Dave Rodgman66433442022-11-24 20:07:39 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226extern inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x);
Dave Rodgmana360e192022-11-28 14:44:05 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228extern inline uint64_t mbedtls_get_unaligned_uint64(const void *p);
Dave Rodgmana360e192022-11-28 14:44:05 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230extern inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x);
Jerry Yu38257492022-12-15 17:54:47 +0800231
232#if defined(MBEDTLS_HAVE_TIME) && !defined(MBEDTLS_PLATFORM_MS_TIME_ALT)
233
234#include <time.h>
Jerry Yu3bfe1332023-02-22 15:01:05 +0800235#if !defined(_WIN32) && \
236 (defined(unix) || defined(__unix) || defined(__unix__) || \
237 (defined(__APPLE__) && defined(__MACH__)))
Jerry Yu38257492022-12-15 17:54:47 +0800238#include <unistd.h>
Jerry Yu3bfe1332023-02-22 15:01:05 +0800239#endif /* !_WIN32 && (unix || __unix || __unix__ || (__APPLE__ && __MACH__)) */
Jerry Yu38257492022-12-15 17:54:47 +0800240#if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 199309L)
241mbedtls_ms_time_t mbedtls_ms_time(void)
242{
243 int ret;
244 struct timespec tv;
245 mbedtls_ms_time_t current_ms;
246
Jerry Yu5cc39a62023-04-20 17:11:32 +0800247#if defined(__linux__)
248 ret = clock_gettime(CLOCK_BOOTTIME, &tv);
249#else
Jerry Yu205a7412023-03-15 18:58:51 +0800250 ret = clock_gettime(CLOCK_MONOTONIC, &tv);
Jerry Yu5cc39a62023-04-20 17:11:32 +0800251#endif
Jerry Yu38257492022-12-15 17:54:47 +0800252 if (ret) {
Jerry Yu029e6592023-02-23 11:25:36 +0800253 return time(NULL) * 1000;
Jerry Yu38257492022-12-15 17:54:47 +0800254 }
255
256 current_ms = tv.tv_sec;
257
258 return current_ms*1000 + tv.tv_nsec / 1000000;
259}
260#elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
261 defined(__MINGW32__) || defined(_WIN64)
262#include <windows.h>
263mbedtls_ms_time_t mbedtls_ms_time(void)
264{
Jerry Yu947fd3d2023-01-30 13:27:54 +0800265 FILETIME ct;
266 mbedtls_ms_time_t current_ms;
Jerry Yu38257492022-12-15 17:54:47 +0800267
Jerry Yu947fd3d2023-01-30 13:27:54 +0800268 GetSystemTimeAsFileTime(&ct);
269 current_ms = ((mbedtls_ms_time_t) ct.dwLowDateTime +
Jerry Yuf52078f2023-03-16 11:23:19 +0800270 ((mbedtls_ms_time_t) (ct.dwHighDateTime) << 32LL))/10000;
Jerry Yu947fd3d2023-01-30 13:27:54 +0800271 return current_ms;
Jerry Yu38257492022-12-15 17:54:47 +0800272}
273#else
274#error "No mbedtls_ms_time available"
275#endif
276#endif /* MBEDTLS_HAVE_TIME && !MBEDTLS_PLATFORM_MS_TIME_ALT */