Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 22 | /* |
| 23 | * Ensure gmtime_r is available even with -std=c99; must be included before |
| 24 | * config.h, which pulls in glibc's features.h. Harmless on other platforms. |
| 25 | */ |
Andres Amaya Garcia | 94b540a | 2018-09-05 12:27:32 +0100 | [diff] [blame] | 26 | #if !defined(_POSIX_C_SOURCE) |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 27 | #define _POSIX_C_SOURCE 200112L |
Andres Amaya Garcia | 94b540a | 2018-09-05 12:27:32 +0100 | [diff] [blame] | 28 | #endif |
Andres Amaya Garcia | e9b10b2 | 2018-09-05 11:25:30 +0100 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 34 | #endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_THREADING_C) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/threading.h" |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 39 | |
Hanno Becker | cfeb70c | 2018-09-05 13:50:22 +0100 | [diff] [blame] | 40 | #if !defined(_WIN32) && (defined(unix) || \ |
Andres Amaya Garcia | 433f911 | 2018-09-05 12:01:57 +0100 | [diff] [blame] | 41 | defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ |
| 42 | defined(__MACH__))) |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 43 | #include <unistd.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 44 | #if !defined(_POSIX_VERSION) || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS |
Andres Amaya Garcia | ca04a01 | 2018-09-05 11:43:57 +0100 | [diff] [blame] | 45 | /* |
| 46 | * This is a convenience shorthand macro to avoid checking the long |
| 47 | * preprocessor conditions above. Ideally, we could expose this macro in |
| 48 | * platform_utils.h and simply use it in platform_utils.c, threading.c and |
| 49 | * 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] | 50 | * we keep it private by only defining it in this file |
Andres Amaya Garcia | ca04a01 | 2018-09-05 11:43:57 +0100 | [diff] [blame] | 51 | */ |
Andres Amaya Garcia | d717743 | 2018-08-08 09:41:17 +0100 | [diff] [blame] | 52 | #define THREADING_USE_GMTIME |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 53 | #endif /* !_POSIX_VERSION || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS */ |
Hanno Becker | cfeb70c | 2018-09-05 13:50:22 +0100 | [diff] [blame] | 54 | #endif /* !_WIN32 && (unix || __unix || __unix__ || |
Andres Amaya Garcia | 433f911 | 2018-09-05 12:01:57 +0100 | [diff] [blame] | 55 | * (__APPLE__ && __MACH__)) */ |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 58 | static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 59 | { |
Janos Follath | 6e87698 | 2016-10-28 14:59:12 +0100 | [diff] [blame] | 60 | if( mutex == NULL ) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 61 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 63 | mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 66 | static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 67 | { |
Janos Follath | 5437a75 | 2016-09-26 09:15:44 +0100 | [diff] [blame] | 68 | if( mutex == NULL || !mutex->is_valid ) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 69 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 71 | (void) pthread_mutex_destroy( &mutex->mutex ); |
Janos Follath | 5437a75 | 2016-09-26 09:15:44 +0100 | [diff] [blame] | 72 | mutex->is_valid = 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 76 | { |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 77 | if( mutex == NULL || ! mutex->is_valid ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 80 | if( pthread_mutex_lock( &mutex->mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 82 | |
| 83 | return( 0 ); |
| 84 | } |
| 85 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 87 | { |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 88 | if( mutex == NULL || ! mutex->is_valid ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 90 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 91 | if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 93 | |
| 94 | return( 0 ); |
| 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 97 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; |
| 98 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; |
| 100 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 101 | |
| 102 | /* |
| 103 | * With phtreads we can statically initialize mutexes |
| 104 | */ |
| 105 | #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } |
| 106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | #if defined(MBEDTLS_THREADING_ALT) |
| 110 | static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 111 | { |
| 112 | ((void) mutex ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 114 | } |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 115 | static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) |
| 116 | { |
| 117 | ((void) mutex ); |
| 118 | return; |
| 119 | } |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 120 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 121 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; |
| 122 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; |
| 124 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 125 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Set functions pointers and initialize global mutexes |
| 128 | */ |
| 129 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), |
| 130 | void (*mutex_free)( mbedtls_threading_mutex_t * ), |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), |
| 132 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | mbedtls_mutex_init = mutex_init; |
| 135 | mbedtls_mutex_free = mutex_free; |
| 136 | mbedtls_mutex_lock = mutex_lock; |
| 137 | mbedtls_mutex_unlock = mutex_unlock; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 138 | |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 140 | mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 141 | #endif |
Andres Amaya Garcia | d717743 | 2018-08-08 09:41:17 +0100 | [diff] [blame] | 142 | #if defined(THREADING_USE_GMTIME) |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 143 | mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); |
| 144 | #endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Free global mutexes |
| 149 | */ |
| 150 | void mbedtls_threading_free_alt( void ) |
| 151 | { |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 152 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 153 | mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 154 | #endif |
Andres Amaya Garcia | d717743 | 2018-08-08 09:41:17 +0100 | [diff] [blame] | 155 | #if defined(THREADING_USE_GMTIME) |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 156 | mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); |
| 157 | #endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 158 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #endif /* MBEDTLS_THREADING_ALT */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 161 | /* |
| 162 | * Define global mutexes |
| 163 | */ |
| 164 | #ifndef MUTEX_INIT |
| 165 | #define MUTEX_INIT |
| 166 | #endif |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 168 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; |
Gergely Budai | 13f7fb3 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 169 | #endif |
Andres Amaya Garcia | d717743 | 2018-08-08 09:41:17 +0100 | [diff] [blame] | 170 | #if defined(THREADING_USE_GMTIME) |
Andres Amaya Garcia | ce6eebb | 2018-08-07 20:26:55 +0100 | [diff] [blame] | 171 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; |
| 172 | #endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | #endif /* MBEDTLS_THREADING_C */ |