Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
Jerome Forissier | 7901324 | 2021-07-28 10:24:04 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 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. |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 20 | /* |
| 21 | * Ensure gmtime_r is available even with -std=c99; must be defined before |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 22 | * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms. |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 23 | */ |
| 24 | #if !defined(_POSIX_C_SOURCE) |
| 25 | #define _POSIX_C_SOURCE 200112L |
| 26 | #endif |
| 27 | |
Jerome Forissier | 7901324 | 2021-07-28 10:24:04 +0200 | [diff] [blame] | 28 | #include "common.h" |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 29 | |
| 30 | #if defined(MBEDTLS_THREADING_C) |
| 31 | |
| 32 | #include "mbedtls/threading.h" |
| 33 | |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) |
| 35 | |
| 36 | #if !defined(_WIN32) && (defined(unix) || \ |
| 37 | defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ |
| 38 | defined(__MACH__))) |
| 39 | #include <unistd.h> |
| 40 | #endif /* !_WIN32 && (unix || __unix || __unix__ || |
| 41 | * (__APPLE__ && __MACH__)) */ |
| 42 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 43 | #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \ |
| 44 | (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \ |
| 45 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L)) |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 46 | /* |
| 47 | * This is a convenience shorthand macro to avoid checking the long |
| 48 | * preprocessor conditions above. Ideally, we could expose this macro in |
| 49 | * platform_util.h and simply use it in platform_util.c, threading.c and |
| 50 | * threading.h. However, this macro is not part of the Mbed TLS public API, so |
| 51 | * we keep it private by only defining it in this file |
| 52 | */ |
| 53 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 54 | #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)) |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 55 | #define THREADING_USE_GMTIME |
| 56 | #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ |
| 57 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 58 | #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ |
| 59 | ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ |
Jerome Forissier | 7901324 | 2021-07-28 10:24:04 +0200 | [diff] [blame] | 60 | _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */ |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 61 | |
| 62 | #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ |
| 63 | |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 64 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 65 | static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 66 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 67 | if (mutex == NULL) { |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 68 | return; |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 69 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 70 | |
Jerome Forissier | 7901324 | 2021-07-28 10:24:04 +0200 | [diff] [blame] | 71 | /* A nonzero value of is_valid indicates a successfully initialized |
| 72 | * mutex. This is a workaround for not being able to return an error |
| 73 | * code for this function. The lock/unlock functions return an error |
| 74 | * if is_valid is nonzero. The Mbed TLS unit test code uses this field |
| 75 | * to distinguish more states of the mutex; see |
| 76 | * tests/src/threading_helpers for details. */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 77 | mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 80 | static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 81 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 82 | if (mutex == NULL || !mutex->is_valid) { |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 83 | return; |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 84 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 85 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 86 | (void) pthread_mutex_destroy(&mutex->mutex); |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 87 | mutex->is_valid = 0; |
| 88 | } |
| 89 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 90 | static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 91 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 92 | if (mutex == NULL || !mutex->is_valid) { |
| 93 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 94 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 95 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 96 | if (pthread_mutex_lock(&mutex->mutex) != 0) { |
| 97 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 98 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 99 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 100 | return 0; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 103 | static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 104 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 105 | if (mutex == NULL || !mutex->is_valid) { |
| 106 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
| 107 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 108 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 109 | if (pthread_mutex_unlock(&mutex->mutex) != 0) { |
| 110 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 111 | } |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 112 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 113 | return 0; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 116 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread; |
| 117 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread; |
| 118 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread; |
| 119 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 120 | |
| 121 | /* |
Jerome Forissier | 039e02d | 2022-08-09 17:10:15 +0200 | [diff] [blame] | 122 | * With pthreads we can statically initialize mutexes |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 123 | */ |
| 124 | #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } |
| 125 | |
| 126 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 127 | |
| 128 | #if defined(MBEDTLS_THREADING_ALT) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 129 | static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 130 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 131 | ((void) mutex); |
| 132 | return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 133 | } |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 134 | static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 135 | { |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 136 | ((void) mutex); |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 140 | void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 141 | void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy; |
| 142 | int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
| 143 | int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail; |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Set functions pointers and initialize global mutexes |
| 147 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 148 | void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *), |
| 149 | void (*mutex_free)(mbedtls_threading_mutex_t *), |
| 150 | int (*mutex_lock)(mbedtls_threading_mutex_t *), |
| 151 | int (*mutex_unlock)(mbedtls_threading_mutex_t *)) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 152 | { |
| 153 | mbedtls_mutex_init = mutex_init; |
| 154 | mbedtls_mutex_free = mutex_free; |
| 155 | mbedtls_mutex_lock = mutex_lock; |
| 156 | mbedtls_mutex_unlock = mutex_unlock; |
| 157 | |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 158 | #if defined(MBEDTLS_FS_IO) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 159 | mbedtls_mutex_init(&mbedtls_threading_readdir_mutex); |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 160 | #endif |
| 161 | #if defined(THREADING_USE_GMTIME) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 162 | mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex); |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 163 | #endif |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Free global mutexes |
| 168 | */ |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 169 | void mbedtls_threading_free_alt(void) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 170 | { |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 171 | #if defined(MBEDTLS_FS_IO) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 172 | mbedtls_mutex_free(&mbedtls_threading_readdir_mutex); |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 173 | #endif |
| 174 | #if defined(THREADING_USE_GMTIME) |
Jens Wiklander | 32b3180 | 2023-10-06 16:59:46 +0200 | [diff] [blame] | 175 | mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex); |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 176 | #endif |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 177 | } |
| 178 | #endif /* MBEDTLS_THREADING_ALT */ |
| 179 | |
| 180 | /* |
| 181 | * Define global mutexes |
| 182 | */ |
| 183 | #ifndef MUTEX_INIT |
| 184 | #define MUTEX_INIT |
| 185 | #endif |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 186 | #if defined(MBEDTLS_FS_IO) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 187 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 188 | #endif |
| 189 | #if defined(THREADING_USE_GMTIME) |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 190 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; |
Jens Wiklander | 3d3b059 | 2019-03-20 15:30:29 +0100 | [diff] [blame] | 191 | #endif |
Jens Wiklander | 817466c | 2018-05-22 13:49:31 +0200 | [diff] [blame] | 192 | |
| 193 | #endif /* MBEDTLS_THREADING_C */ |