blob: 0dc5488c1a988f95d5ea3d4e899444c4fe5f1511 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker2466d932013-09-28 14:40:38 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker2466d932013-09-28 14:40:38 +020045 */
46
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010047/*
Hanno Becker48a816f2018-09-05 15:22:22 +010048 * Ensure gmtime_r is available even with -std=c99; must be defined before
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010049 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
50 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010051#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010052#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010053#endif
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker2466d932013-09-28 14:40:38 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020062
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020064
Hanno Becker6a739782018-09-05 15:06:19 +010065#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +010066
Hanno Beckercfeb70c2018-09-05 13:50:22 +010067#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +010068 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
69 defined(__MACH__)))
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010070#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +010071#endif /* !_WIN32 && (unix || __unix || __unix__ ||
72 * (__APPLE__ && __MACH__)) */
73
Hanno Becker6f705812018-09-06 09:06:33 +010074#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
75 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jones0a635602020-11-13 08:52:00 +000076 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) )
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010077/*
78 * This is a convenience shorthand macro to avoid checking the long
79 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +010080 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010081 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +010082 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +010083 */
Hanno Beckerf5106d52018-09-06 12:09:56 +010084
85#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
Andres Amaya Garciad7177432018-08-08 09:41:17 +010086#define THREADING_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +010087#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
88
Hanno Becker6f705812018-09-06 09:06:33 +010089#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
90 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Chris Jones0a635602020-11-13 08:52:00 +000091 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Hanno Becker323d8012018-09-06 11:30:57 +010092
Hanno Becker6a739782018-09-05 15:06:19 +010093#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020096static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020097{
Janos Follath6e876982016-10-28 14:59:12 +010098 if( mutex == NULL )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020099 return;
Paul Bakker2466d932013-09-28 14:40:38 +0200100
Gilles Peskine7ba73e52021-02-09 15:35:29 +0100101 /* A nonzero value of is_valid indicates a successfully initialized
102 * mutex. This is a workaround for not being able to return an error
103 * code for this function. The lock/unlock functions return an error
104 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
105 * to distinguish more states of the mutex; see helpers.function for
106 * details. */
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200107 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200108}
109
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200110static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +0200111{
Janos Follath5437a752016-09-26 09:15:44 +0100112 if( mutex == NULL || !mutex->is_valid )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200113 return;
Paul Bakker2466d932013-09-28 14:40:38 +0200114
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200115 (void) pthread_mutex_destroy( &mutex->mutex );
Janos Follath5437a752016-09-26 09:15:44 +0100116 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +0200117}
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +0200120{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200121 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +0200123
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200124 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +0200126
127 return( 0 );
128}
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +0200131{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200132 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +0200134
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200135 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +0200137
138 return( 0 );
139}
140
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200141void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
142void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
144int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200145
146/*
147 * With phtreads we can statically initialize mutexes
148 */
149#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_THREADING_ALT)
154static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
Paul Bakkerc78c8422013-12-31 11:55:27 +0100155{
156 ((void) mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakkerc78c8422013-12-31 11:55:27 +0100158}
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200159static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
160{
161 ((void) mutex );
162 return;
163}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100164
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200165void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
166void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
168int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200169
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200170/*
171 * Set functions pointers and initialize global mutexes
172 */
173void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
174 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
176 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
Paul Bakker2466d932013-09-28 14:40:38 +0200177{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_mutex_init = mutex_init;
179 mbedtls_mutex_free = mutex_free;
180 mbedtls_mutex_lock = mutex_lock;
181 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200182
Gergely Budai13f7fb32017-08-23 14:23:58 +0200183#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200184 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Gergely Budai13f7fb32017-08-23 14:23:58 +0200185#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100186#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100187 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
188#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200189}
190
191/*
192 * Free global mutexes
193 */
194void mbedtls_threading_free_alt( void )
195{
Gergely Budai13f7fb32017-08-23 14:23:58 +0200196#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200197 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Gergely Budai13f7fb32017-08-23 14:23:58 +0200198#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100199#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100200 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
201#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200204
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200205/*
206 * Define global mutexes
207 */
208#ifndef MUTEX_INIT
209#define MUTEX_INIT
210#endif
Gergely Budai13f7fb32017-08-23 14:23:58 +0200211#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200212mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200213#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100214#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100215mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
216#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#endif /* MBEDTLS_THREADING_C */