Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 20 | /* |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 21 | * Ensure gmtime_r is available even with -std=c99; must be defined before |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 22 | * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other |
| 23 | * platforms. |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 24 | */ |
Andres Amaya Garcia | 94b540a | 2018-09-05 12:27:32 +0100 | [diff] [blame] | 25 | #if !defined(_POSIX_C_SOURCE) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 26 | # define _POSIX_C_SOURCE 200112L |
Andres Amaya Garcia | 94b540a | 2018-09-05 12:27:32 +0100 | [diff] [blame] | 27 | #endif |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 28 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 29 | #include "common.h" |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_THREADING_C) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 32 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 33 | # include "mbedtls/threading.h" |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 34 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 35 | # if defined(MBEDTLS_HAVE_TIME_DATE) && \ |
| 36 | !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) |
Hanno Becker | 323d801 | 2018-09-06 11:30:57 +0100 | [diff] [blame] | 37 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | # if !defined(_WIN32) && \ |
| 39 | (defined(unix) || defined(__unix) || defined(__unix__) || \ |
| 40 | (defined(__APPLE__) && defined(__MACH__))) |
| 41 | # include <unistd.h> |
| 42 | # endif /* !_WIN32 && (unix || __unix || __unix__ || \ |
| 43 | * (__APPLE__ && __MACH__)) */ |
Hanno Becker | 323d801 | 2018-09-06 11:30:57 +0100 | [diff] [blame] | 44 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 45 | # if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \ |
| 46 | (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \ |
| 47 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L)) |
Andres Amaya Garcia | ca04a01 | 2018-09-05 11:43:57 +0100 | [diff] [blame] | 48 | /* |
| 49 | * This is a convenience shorthand macro to avoid checking the long |
| 50 | * preprocessor conditions above. Ideally, we could expose this macro in |
Hanno Becker | 7dd82b4 | 2018-09-05 16:25:50 +0100 | [diff] [blame] | 51 | * platform_util.h and simply use it in platform_util.c, threading.c and |
Andres Amaya Garcia | ca04a01 | 2018-09-05 11:43:57 +0100 | [diff] [blame] | 52 | * threading.h. However, this macro is not part of the Mbed TLS public API, so |
Andres Amaya Garcia | 3c9733a | 2018-09-05 11:52:07 +0100 | [diff] [blame] | 53 | * we keep it private by only defining it in this file |
Andres Amaya Garcia | ca04a01 | 2018-09-05 11:43:57 +0100 | [diff] [blame] | 54 | */ |
Hanno Becker | f5106d5 | 2018-09-06 12:09:56 +0100 | [diff] [blame] | 55 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 56 | # if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) |
| 57 | # define THREADING_USE_GMTIME |
| 58 | # endif /* ! ( defined(_WIN32) && !defined(EFIX64) && \ |
| 59 | !defined(EFI32) ) */ |
Hanno Becker | f5106d5 | 2018-09-06 12:09:56 +0100 | [diff] [blame] | 60 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 61 | # endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) \ |
| 62 | || ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ |
| 63 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */ |
Hanno Becker | 323d801 | 2018-09-06 11:30:57 +0100 | [diff] [blame] | 64 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 65 | # endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 66 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 67 | # if defined(MBEDTLS_THREADING_PTHREAD) |
| 68 | static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 69 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | if (mutex == NULL) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 71 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 72 | |
Gilles Peskine | 39a1a26 | 2021-02-09 15:35:29 +0100 | [diff] [blame] | 73 | /* A nonzero value of is_valid indicates a successfully initialized |
| 74 | * mutex. This is a workaround for not being able to return an error |
| 75 | * code for this function. The lock/unlock functions return an error |
| 76 | * if is_valid is nonzero. The Mbed TLS unit test code uses this field |
| 77 | * to distinguish more states of the mutex; see |
| 78 | * tests/src/threading_helpers for details. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 79 | mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 83 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 84 | if (mutex == NULL || !mutex->is_valid) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 85 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 86 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 87 | (void)pthread_mutex_destroy(&mutex->mutex); |
Janos Follath | 5437a75 | 2016-09-26 09:15:44 +0100 | [diff] [blame] | 88 | mutex->is_valid = 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 91 | static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 92 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 93 | if (mutex == NULL || !mutex->is_valid) |
| 94 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 95 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 96 | if (pthread_mutex_lock(&mutex->mutex) != 0) |
| 97 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 98 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 99 | return 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 102 | static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 103 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | if (mutex == NULL || !mutex->is_valid) |
| 105 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 106 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 107 | if (pthread_mutex_unlock(&mutex->mutex) != 0) |
| 108 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 109 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 110 | return 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = |
| 114 | threading_mutex_init_pthread; |
| 115 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = |
| 116 | threading_mutex_free_pthread; |
| 117 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = |
| 118 | threading_mutex_lock_pthread; |
| 119 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = |
| 120 | threading_mutex_unlock_pthread; |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * With phtreads we can statically initialize mutexes |
| 124 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 125 | # define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 126 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 127 | # endif /* MBEDTLS_THREADING_PTHREAD */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 128 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 129 | # if defined(MBEDTLS_THREADING_ALT) |
| 130 | static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex) |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 131 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 132 | ((void)mutex); |
| 133 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 134 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 135 | static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 136 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 137 | ((void)mutex); |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 138 | return; |
| 139 | } |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 140 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 141 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 142 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 143 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
| 144 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Set functions pointers and initialize global mutexes |
| 148 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 149 | void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *), |
| 150 | void (*mutex_free)(mbedtls_threading_mutex_t *), |
| 151 | int (*mutex_lock)(mbedtls_threading_mutex_t *), |
| 152 | int (*mutex_unlock)(mbedtls_threading_mutex_t *)) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 153 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | mbedtls_mutex_init = mutex_init; |
| 155 | mbedtls_mutex_free = mutex_free; |
| 156 | mbedtls_mutex_lock = mutex_lock; |
| 157 | mbedtls_mutex_unlock = mutex_unlock; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 158 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 159 | # if defined(MBEDTLS_FS_IO) |
| 160 | mbedtls_mutex_init(&mbedtls_threading_readdir_mutex); |
| 161 | # endif |
| 162 | # if defined(THREADING_USE_GMTIME) |
| 163 | mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex); |
| 164 | # endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Free global mutexes |
| 169 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 170 | void mbedtls_threading_free_alt(void) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 171 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 172 | # if defined(MBEDTLS_FS_IO) |
| 173 | mbedtls_mutex_free(&mbedtls_threading_readdir_mutex); |
| 174 | # endif |
| 175 | # if defined(THREADING_USE_GMTIME) |
| 176 | mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex); |
| 177 | # endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 178 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 179 | # endif /* MBEDTLS_THREADING_ALT */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 181 | /* |
| 182 | * Define global mutexes |
| 183 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 184 | # ifndef MUTEX_INIT |
| 185 | # define MUTEX_INIT |
| 186 | # endif |
| 187 | # if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 188 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 189 | # endif |
| 190 | # if defined(THREADING_USE_GMTIME) |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 191 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 192 | # endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #endif /* MBEDTLS_THREADING_C */ |