blob: 130c6963d4193f525983f05c77aee8232a2cf44b [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Threading abstraction layer
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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 Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jens Wiklander3d3b0592019-03-20 15:30:29 +010020/*
21 * Ensure gmtime_r is available even with -std=c99; must be defined before
Jens Wiklander32b31802023-10-06 16:59:46 +020022 * mbedtls_config.h, which pulls in glibc's features.h. Harmless on other platforms.
Jens Wiklander3d3b0592019-03-20 15:30:29 +010023 */
24#if !defined(_POSIX_C_SOURCE)
25#define _POSIX_C_SOURCE 200112L
26#endif
27
Jerome Forissier79013242021-07-28 10:24:04 +020028#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020029
30#if defined(MBEDTLS_THREADING_C)
31
32#include "mbedtls/threading.h"
33
Jens Wiklander3d3b0592019-03-20 15:30:29 +010034#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 Wiklander32b31802023-10-06 16:59:46 +020043#if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
44 (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
45 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
Jens Wiklander3d3b0592019-03-20 15:30:29 +010046/*
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 Wiklander32b31802023-10-06 16:59:46 +020054#if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
Jens Wiklander3d3b0592019-03-20 15:30:29 +010055#define THREADING_USE_GMTIME
56#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
57
Jens Wiklander32b31802023-10-06 16:59:46 +020058#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
59 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Jerome Forissier79013242021-07-28 10:24:04 +020060 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Jens Wiklander3d3b0592019-03-20 15:30:29 +010061
62#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
63
Jens Wiklander817466c2018-05-22 13:49:31 +020064#if defined(MBEDTLS_THREADING_PTHREAD)
Jens Wiklander32b31802023-10-06 16:59:46 +020065static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +020066{
Jens Wiklander32b31802023-10-06 16:59:46 +020067 if (mutex == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +020068 return;
Jens Wiklander32b31802023-10-06 16:59:46 +020069 }
Jens Wiklander817466c2018-05-22 13:49:31 +020070
Jerome Forissier79013242021-07-28 10:24:04 +020071 /* 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 Wiklander32b31802023-10-06 16:59:46 +020077 mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
Jens Wiklander817466c2018-05-22 13:49:31 +020078}
79
Jens Wiklander32b31802023-10-06 16:59:46 +020080static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +020081{
Jens Wiklander32b31802023-10-06 16:59:46 +020082 if (mutex == NULL || !mutex->is_valid) {
Jens Wiklander817466c2018-05-22 13:49:31 +020083 return;
Jens Wiklander32b31802023-10-06 16:59:46 +020084 }
Jens Wiklander817466c2018-05-22 13:49:31 +020085
Jens Wiklander32b31802023-10-06 16:59:46 +020086 (void) pthread_mutex_destroy(&mutex->mutex);
Jens Wiklander817466c2018-05-22 13:49:31 +020087 mutex->is_valid = 0;
88}
89
Jens Wiklander32b31802023-10-06 16:59:46 +020090static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +020091{
Jens Wiklander32b31802023-10-06 16:59:46 +020092 if (mutex == NULL || !mutex->is_valid) {
93 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
94 }
Jens Wiklander817466c2018-05-22 13:49:31 +020095
Jens Wiklander32b31802023-10-06 16:59:46 +020096 if (pthread_mutex_lock(&mutex->mutex) != 0) {
97 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
98 }
Jens Wiklander817466c2018-05-22 13:49:31 +020099
Jens Wiklander32b31802023-10-06 16:59:46 +0200100 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200101}
102
Jens Wiklander32b31802023-10-06 16:59:46 +0200103static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +0200104{
Jens Wiklander32b31802023-10-06 16:59:46 +0200105 if (mutex == NULL || !mutex->is_valid) {
106 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
107 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200108
Jens Wiklander32b31802023-10-06 16:59:46 +0200109 if (pthread_mutex_unlock(&mutex->mutex) != 0) {
110 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
111 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200112
Jens Wiklander32b31802023-10-06 16:59:46 +0200113 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200114}
115
Jens Wiklander32b31802023-10-06 16:59:46 +0200116void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
117void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
118int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
119int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
Jens Wiklander817466c2018-05-22 13:49:31 +0200120
121/*
Jerome Forissier039e02d2022-08-09 17:10:15 +0200122 * With pthreads we can statically initialize mutexes
Jens Wiklander817466c2018-05-22 13:49:31 +0200123 */
124#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
125
126#endif /* MBEDTLS_THREADING_PTHREAD */
127
128#if defined(MBEDTLS_THREADING_ALT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200129static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +0200130{
Jens Wiklander32b31802023-10-06 16:59:46 +0200131 ((void) mutex);
132 return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
Jens Wiklander817466c2018-05-22 13:49:31 +0200133}
Jens Wiklander32b31802023-10-06 16:59:46 +0200134static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
Jens Wiklander817466c2018-05-22 13:49:31 +0200135{
Jens Wiklander32b31802023-10-06 16:59:46 +0200136 ((void) mutex);
Jens Wiklander817466c2018-05-22 13:49:31 +0200137 return;
138}
139
Jens Wiklander32b31802023-10-06 16:59:46 +0200140void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
141void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
142int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
143int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
Jens Wiklander817466c2018-05-22 13:49:31 +0200144
145/*
146 * Set functions pointers and initialize global mutexes
147 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200148void 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 Wiklander817466c2018-05-22 13:49:31 +0200152{
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 Wiklander3d3b0592019-03-20 15:30:29 +0100158#if defined(MBEDTLS_FS_IO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200159 mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100160#endif
161#if defined(THREADING_USE_GMTIME)
Jens Wiklander32b31802023-10-06 16:59:46 +0200162 mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100163#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200164}
165
166/*
167 * Free global mutexes
168 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200169void mbedtls_threading_free_alt(void)
Jens Wiklander817466c2018-05-22 13:49:31 +0200170{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100171#if defined(MBEDTLS_FS_IO)
Jens Wiklander32b31802023-10-06 16:59:46 +0200172 mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100173#endif
174#if defined(THREADING_USE_GMTIME)
Jens Wiklander32b31802023-10-06 16:59:46 +0200175 mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100176#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200177}
178#endif /* MBEDTLS_THREADING_ALT */
179
180/*
181 * Define global mutexes
182 */
183#ifndef MUTEX_INIT
184#define MUTEX_INIT
185#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100186#if defined(MBEDTLS_FS_IO)
Jens Wiklander817466c2018-05-22 13:49:31 +0200187mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100188#endif
189#if defined(THREADING_USE_GMTIME)
Jens Wiklander817466c2018-05-22 13:49:31 +0200190mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100191#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200192
193#endif /* MBEDTLS_THREADING_C */