Skip to content

Commit d9fda90

Browse files
authored
CBL-8183: Upgrade mbedTLS to 3.6.6 (#2486)
Also merged in the following commit: Windows threading abstraction for mbedTLS (#2435) 1. Cmake will now use config.py to switch PTHREAD for ALT in the mbedTLS threading implementation (ifdef causes python errors) 2. Add a static instance that will set up the threading model (mbedThreading.cc) 3. Add a threading_alt.h header that CMake will copy into mbedTLS's include directory so that the downstream library can find it 4. Bump mbedTLS submodule to get rid of header ifdef
1 parent 9490d0d commit d9fda90

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

MSVC/mbedThreading.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "threading_alt.h"
2+
#include "mbedtls/threading.h"
3+
4+
#if defined(_WIN32) && defined(MBEDTLS_THREADING_ALT)
5+
6+
extern "C" {
7+
static void windows_mutex_init(mbedtls_threading_mutex_t* mutex) {
8+
if ( mutex == NULL ) { return; }
9+
10+
InitializeCriticalSection(&mutex->MBEDTLS_PRIVATE(mutex));
11+
}
12+
13+
static void windows_mutex_free(mbedtls_threading_mutex_t* mutex) {
14+
if ( mutex == NULL ) { return; }
15+
16+
DeleteCriticalSection(&mutex->MBEDTLS_PRIVATE(mutex));
17+
}
18+
19+
// NOTE: The lock and unlock depend on windows_mutex_init being called first.
20+
// That is out of our hands though. Failure to do so is undefined behavior.
21+
static int windows_mutex_lock(mbedtls_threading_mutex_t* mutex) {
22+
if ( mutex == NULL ) { return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; }
23+
24+
EnterCriticalSection(&mutex->MBEDTLS_PRIVATE(mutex));
25+
return 0;
26+
}
27+
28+
static int windows_mutex_unlock(mbedtls_threading_mutex_t* mutex) {
29+
if ( mutex == NULL ) { return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA; }
30+
31+
LeaveCriticalSection(&mutex->MBEDTLS_PRIVATE(mutex));
32+
return 0;
33+
}
34+
}
35+
36+
struct MbedTLSInit {
37+
MbedTLSInit() {
38+
mbedtls_threading_set_alt(windows_mutex_init, windows_mutex_free, windows_mutex_lock, windows_mutex_unlock);
39+
}
40+
41+
~MbedTLSInit() { mbedtls_threading_free_alt(); }
42+
};
43+
44+
static MbedTLSInit sMbedTLSInit;
45+
#endif

MSVC/threading_alt.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* \file threading_alt.h
3+
*
4+
* \brief Alternate threading abstraction layer
5+
*/
6+
/*
7+
* Copyright Couchbase, Inc.
8+
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9+
*/
10+
11+
// NOTE: CMake should copy this file into the mbedtls include directory
12+
// so that the mbedtls library can find it
13+
14+
#ifndef MBEDTLS_THREADING_ALT_H
15+
#define MBEDTLS_THREADING_ALT_H
16+
17+
#ifdef _WIN32
18+
# include "mbedtls/private_access.h"
19+
20+
# include "mbedtls/build_info.h"
21+
# include <windows.h>
22+
23+
# ifdef __cplusplus
24+
extern "C" {
25+
# endif
26+
27+
# if defined(MBEDTLS_THREADING_ALT)
28+
typedef struct mbedtls_threading_mutex_t {
29+
CRITICAL_SECTION MBEDTLS_PRIVATE(mutex);
30+
} mbedtls_threading_mutex_t;
31+
32+
# endif // MBEDTLS_THREADING_ALT
33+
#endif //_WIN32
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif
38+
39+
#endif // MBEDTLS_THREADING_ALT_H

cmake/platform_win.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,28 @@ function(set_litecore_source)
1919
MSVC/mkstemp.cc
2020
MSVC/mkdtemp.cc
2121
MSVC/strlcat.c
22+
MSVC/mbedThreading.cc
2223
LiteCore/Support/StringUtil_winapi.cc
2324
Crypto/PublicKey+Windows.cc
2425
PARENT_SCOPE
2526
)
2627
endfunction()
2728

2829
function(setup_globals)
30+
find_package(Python3 COMPONENTS Interpreter REQUIRED)
31+
message(STATUS "Configuring mbedTLS for Windows threading model")
32+
set(_mbedtls_config_py "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../vendor/mbedtls/scripts/config.py")
33+
execute_process(
34+
COMMAND "${Python3_EXECUTABLE}" "${_mbedtls_config_py}" unset MBEDTLS_THREADING_PTHREAD
35+
WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/.."
36+
)
37+
execute_process(
38+
COMMAND "${Python3_EXECUTABLE}" "${_mbedtls_config_py}" set MBEDTLS_THREADING_ALT
39+
WORKING_DIRECTORY "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/.."
40+
)
41+
file(COPY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../MSVC/threading_alt.h
42+
DESTINATION ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../vendor/mbedtls/include/mbedtls)
43+
2944
add_compile_options(/MP)
3045
add_link_options(/CGTHREADS:8)
3146
set(CMAKE_C_FLAGS_MINSIZEREL "/MD /O1 /Ob1 /DNDEBUG /Zi /GL /MP" CACHE INTERNAL "")

jenkins/couchbase-lite-core-black-duck-manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ components:
2323
parent-repo: vendor/sqlite3-unicodesn
2424
mbedtls:
2525
bd-id: 98e2dba0-77c4-402f-96f3-ceb702a6e6e7
26-
versions: [ 3.6.5 ]
26+
versions: [ 3.6.6 ]
2727
src-path: vendor/mbedtls
2828
sockpp:
2929
bd-id: c5b8e378-60ad-4941-b37d-71216e7d9df4

vendor/mbedtls

Submodule mbedtls updated 131 files

0 commit comments

Comments
 (0)