113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci * Copyright (C) Hoi-Ho Chan, <hoiho.chan@gmail.com>
1013498266Sopenharmony_ci *
1113498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1213498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1313498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1413498266Sopenharmony_ci *
1513498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1613498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1713498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1813498266Sopenharmony_ci *
1913498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2013498266Sopenharmony_ci * KIND, either express or implied.
2113498266Sopenharmony_ci *
2213498266Sopenharmony_ci * SPDX-License-Identifier: curl
2313498266Sopenharmony_ci *
2413498266Sopenharmony_ci ***************************************************************************/
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#if defined(USE_MBEDTLS) &&                                     \
2813498266Sopenharmony_ci  ((defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) ||   \
2913498266Sopenharmony_ci    defined(USE_THREADS_WIN32))
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
3213498266Sopenharmony_ci#  include <pthread.h>
3313498266Sopenharmony_ci#  define MBEDTLS_MUTEX_T pthread_mutex_t
3413498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
3513498266Sopenharmony_ci#  define MBEDTLS_MUTEX_T HANDLE
3613498266Sopenharmony_ci#endif
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci#include "mbedtls_threadlock.h"
3913498266Sopenharmony_ci#include "curl_printf.h"
4013498266Sopenharmony_ci#include "curl_memory.h"
4113498266Sopenharmony_ci/* The last #include file should be: */
4213498266Sopenharmony_ci#include "memdebug.h"
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci/* number of thread locks */
4513498266Sopenharmony_ci#define NUMT                    2
4613498266Sopenharmony_ci
4713498266Sopenharmony_ci/* This array will store all of the mutexes available to Mbedtls. */
4813498266Sopenharmony_cistatic MBEDTLS_MUTEX_T *mutex_buf = NULL;
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciint Curl_mbedtlsthreadlock_thread_setup(void)
5113498266Sopenharmony_ci{
5213498266Sopenharmony_ci  int i;
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci  mutex_buf = calloc(1, NUMT * sizeof(MBEDTLS_MUTEX_T));
5513498266Sopenharmony_ci  if(!mutex_buf)
5613498266Sopenharmony_ci    return 0;     /* error, no number of threads defined */
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci  for(i = 0;  i < NUMT;  i++) {
5913498266Sopenharmony_ci#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
6013498266Sopenharmony_ci    if(pthread_mutex_init(&mutex_buf[i], NULL))
6113498266Sopenharmony_ci      return 0; /* pthread_mutex_init failed */
6213498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
6313498266Sopenharmony_ci    mutex_buf[i] = CreateMutex(0, FALSE, 0);
6413498266Sopenharmony_ci    if(mutex_buf[i] == 0)
6513498266Sopenharmony_ci      return 0;  /* CreateMutex failed */
6613498266Sopenharmony_ci#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
6713498266Sopenharmony_ci  }
6813498266Sopenharmony_ci
6913498266Sopenharmony_ci  return 1; /* OK */
7013498266Sopenharmony_ci}
7113498266Sopenharmony_ci
7213498266Sopenharmony_ciint Curl_mbedtlsthreadlock_thread_cleanup(void)
7313498266Sopenharmony_ci{
7413498266Sopenharmony_ci  int i;
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  if(!mutex_buf)
7713498266Sopenharmony_ci    return 0; /* error, no threads locks defined */
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci  for(i = 0; i < NUMT; i++) {
8013498266Sopenharmony_ci#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
8113498266Sopenharmony_ci    if(pthread_mutex_destroy(&mutex_buf[i]))
8213498266Sopenharmony_ci      return 0; /* pthread_mutex_destroy failed */
8313498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
8413498266Sopenharmony_ci    if(!CloseHandle(mutex_buf[i]))
8513498266Sopenharmony_ci      return 0; /* CloseHandle failed */
8613498266Sopenharmony_ci#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
8713498266Sopenharmony_ci  }
8813498266Sopenharmony_ci  free(mutex_buf);
8913498266Sopenharmony_ci  mutex_buf = NULL;
9013498266Sopenharmony_ci
9113498266Sopenharmony_ci  return 1; /* OK */
9213498266Sopenharmony_ci}
9313498266Sopenharmony_ci
9413498266Sopenharmony_ciint Curl_mbedtlsthreadlock_lock_function(int n)
9513498266Sopenharmony_ci{
9613498266Sopenharmony_ci  if(n < NUMT) {
9713498266Sopenharmony_ci#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
9813498266Sopenharmony_ci    if(pthread_mutex_lock(&mutex_buf[n])) {
9913498266Sopenharmony_ci      DEBUGF(fprintf(stderr,
10013498266Sopenharmony_ci                     "Error: mbedtlsthreadlock_lock_function failed\n"));
10113498266Sopenharmony_ci      return 0; /* pthread_mutex_lock failed */
10213498266Sopenharmony_ci    }
10313498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
10413498266Sopenharmony_ci    if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
10513498266Sopenharmony_ci      DEBUGF(fprintf(stderr,
10613498266Sopenharmony_ci                     "Error: mbedtlsthreadlock_lock_function failed\n"));
10713498266Sopenharmony_ci      return 0; /* pthread_mutex_lock failed */
10813498266Sopenharmony_ci    }
10913498266Sopenharmony_ci#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
11013498266Sopenharmony_ci  }
11113498266Sopenharmony_ci  return 1; /* OK */
11213498266Sopenharmony_ci}
11313498266Sopenharmony_ci
11413498266Sopenharmony_ciint Curl_mbedtlsthreadlock_unlock_function(int n)
11513498266Sopenharmony_ci{
11613498266Sopenharmony_ci  if(n < NUMT) {
11713498266Sopenharmony_ci#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
11813498266Sopenharmony_ci    if(pthread_mutex_unlock(&mutex_buf[n])) {
11913498266Sopenharmony_ci      DEBUGF(fprintf(stderr,
12013498266Sopenharmony_ci                     "Error: mbedtlsthreadlock_unlock_function failed\n"));
12113498266Sopenharmony_ci      return 0; /* pthread_mutex_unlock failed */
12213498266Sopenharmony_ci    }
12313498266Sopenharmony_ci#elif defined(USE_THREADS_WIN32)
12413498266Sopenharmony_ci    if(!ReleaseMutex(mutex_buf[n])) {
12513498266Sopenharmony_ci      DEBUGF(fprintf(stderr,
12613498266Sopenharmony_ci                     "Error: mbedtlsthreadlock_unlock_function failed\n"));
12713498266Sopenharmony_ci      return 0; /* pthread_mutex_lock failed */
12813498266Sopenharmony_ci    }
12913498266Sopenharmony_ci#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
13013498266Sopenharmony_ci  }
13113498266Sopenharmony_ci  return 1; /* OK */
13213498266Sopenharmony_ci}
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci#endif /* USE_MBEDTLS */
135