diff --git a/lib_acl_cpp/src/stream/mbedtls_conf.cpp b/lib_acl_cpp/src/stream/mbedtls_conf.cpp index dada9abae..f05c05654 100644 --- a/lib_acl_cpp/src/stream/mbedtls_conf.cpp +++ b/lib_acl_cpp/src/stream/mbedtls_conf.cpp @@ -31,6 +31,7 @@ # ifdef HAS_MBEDTLS_DLL +# define THREADING_SET_ALT_NAME "mbedtls_threading_set_alt" # define PK_INIT_NAME "mbedtls_pk_init" # define PK_FREE_NAME "mbedtls_pk_free" # define PK_PARSE_KEYFILE_NAME "mbedtls_pk_parse_keyfile" @@ -76,6 +77,11 @@ # define SSL_SETUP_NAME "mbedtls_ssl_setup" +typedef void (*threading_set_alt_fn)( + void (*init)(mbedtls_threading_mutex_t *), + void (*free)(mbedtls_threading_mutex_t *), + int (*lock)(mbedtls_threading_mutex_t *), + int (*unlock)( mbedtls_threading_mutex_t *)); typedef void (*pk_init_fn)(PKEY*); typedef void (*pk_free_fn)(PKEY*); typedef int (*pk_parse_keyfile_fn)(PKEY*, const char*, const char*); @@ -127,6 +133,7 @@ typedef int (*ssl_cache_get_fn)(void*, mbedtls_ssl_session*); typedef int (*ssl_setup_fn)(mbedtls_ssl_context*, mbedtls_ssl_config*); +static threading_set_alt_fn __threading_set_alt; static pk_init_fn __pk_init; static pk_free_fn __pk_free; static pk_parse_keyfile_fn __pk_parse_keyfile; @@ -250,6 +257,7 @@ extern bool mbedtls_dll_load_io(void); // defined in mbedtls_io.cpp static bool load_from_crypto(void) { + LOAD_CRYPTO(THREADING_SET_ALT_NAME, threading_set_alt_fn, __threading_set_alt); LOAD_CRYPTO(PK_INIT_NAME, pk_init_fn, __pk_init); LOAD_CRYPTO(PK_FREE_NAME, pk_free_fn, __pk_free); LOAD_CRYPTO(PK_PARSE_KEYFILE_NAME, pk_parse_keyfile_fn, __pk_parse_keyfile); @@ -374,6 +382,7 @@ static void mbedtls_dll_load(void) # else // !HAS_MBEDTLS_DLL && HAS_MBEDTLS +# define __threading_set_alt ::mbedtls_threading_set_alt # define __pk_init ::mbedtls_pk_init # define __pk_free ::mbedtls_pk_free # define __pk_parse_keyfile ::mbedtls_pk_parse_keyfile @@ -519,6 +528,30 @@ static void my_debug( void *ctx, int level, const char* fname, int line, #define CONF_INIT_OK 1 #define CONF_INIT_ERR 2 +static void mutex_init(mbedtls_threading_mutex_t* mutex) +{ + acl_pthread_mutex_t* m = (acl_pthread_mutex_t*) mutex; + acl_pthread_mutex_init(m, NULL); +} + +static void mutex_free(mbedtls_threading_mutex_t* mutex) +{ + acl_pthread_mutex_t* m = (acl_pthread_mutex_t*) mutex; + acl_pthread_mutex_destroy(m); +} + +static int mutex_lock(mbedtls_threading_mutex_t* mutex) +{ + acl_pthread_mutex_t* m = (acl_pthread_mutex_t*) mutex; + return acl_pthread_mutex_lock(m); +} + +static int mutex_unlock(mbedtls_threading_mutex_t* mutex) +{ + acl_pthread_mutex_t* m = (acl_pthread_mutex_t*) mutex; + return acl_pthread_mutex_unlock(m); +} + bool mbedtls_conf::init_once(void) { if (!load()) { @@ -532,6 +565,8 @@ bool mbedtls_conf::init_once(void) return false; } + __threading_set_alt(mutex_init, mutex_free, mutex_lock, mutex_unlock); + #ifdef HAS_MBEDTLS __ssl_config_init((mbedtls_ssl_config*) conf_); __entropy_init((mbedtls_entropy_context*) entropy_);