@@ -309,8 +309,10 @@ int wc_OBJ_sn2nid(const char *sn)
309309static WC_RNG globalRNG;
310310static int initGlobalRNG = 0;
311311
312- static wolfSSL_Mutex globalRNGMutex;
312+ static WC_MAYBE_UNUSED wolfSSL_Mutex globalRNGMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(globalRNGMutex);
313+ #ifndef WOLFSSL_MUTEX_INITIALIZER
313314static int globalRNGMutex_valid = 0;
315+ #endif
314316
315317#if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
316318static WOLFSSL_DRBG_CTX* gDrbgDefCtx = NULL;
@@ -406,8 +408,10 @@ WC_RNG* wolfssl_make_rng(WC_RNG* rng, int* local)
406408 * OPENSSL_EXTRA where RAND callbacks are not used */
407409 #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
408410 static const WOLFSSL_RAND_METHOD* gRandMethods = NULL;
411+ static wolfSSL_Mutex gRandMethodMutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(gRandMethodMutex);
412+ #ifndef WOLFSSL_MUTEX_INITIALIZER
409413 static int gRandMethodsInit = 0;
410- static wolfSSL_Mutex gRandMethodMutex;
414+ #endif
411415 #endif /* !WOLFSSL_NO_OPENSSL_RAND_CB */
412416#endif /* OPENSSL_EXTRA */
413417
@@ -1265,11 +1269,9 @@ int wolfSSL_send_session(WOLFSSL* ssl)
12651269
12661270/* prevent multiple mutex initializations */
12671271static volatile WOLFSSL_GLOBAL int initRefCount = 0;
1268- #ifdef WOLFSSL_MUTEX_INITIALIZER
1269- static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex = WOLFSSL_MUTEX_INITIALIZER;
1270- #else
1271- static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex; /* init ref count mutex */
1272- static WOLFSSL_GLOBAL int count_mutex_valid = 0;
1272+ static WOLFSSL_GLOBAL wolfSSL_Mutex inits_count_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(inits_count_mutex); /* init ref count mutex */
1273+ #ifndef WOLFSSL_MUTEX_INITIALIZER
1274+ static WOLFSSL_GLOBAL int inits_count_mutex_valid = 0;
12731275#endif
12741276
12751277/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
@@ -6164,8 +6166,10 @@ int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
61646166 /* Client Cache */
61656167 /* uses session mutex */
61666168
6167- static WOLFSSL_GLOBAL wolfSSL_Mutex clisession_mutex; /* ClientCache mutex */
6169+ static WOLFSSL_GLOBAL wolfSSL_Mutex clisession_mutex WOLFSSL_MUTEX_INITIALIZER_CLAUSE(clisession_mutex); /* ClientCache mutex */
6170+ #ifndef WOLFSSL_MUTEX_INITIALIZER
61686171 static WOLFSSL_GLOBAL int clisession_mutex_valid = 0;
6172+ #endif
61696173 #endif /* !NO_CLIENT_CACHE */
61706174
61716175 void EvictSessionFromCache(WOLFSSL_SESSION* session)
@@ -6223,22 +6227,40 @@ int wolfSSL_Init(void)
62236227
62246228 WOLFSSL_ENTER("wolfSSL_Init");
62256229
6230+ #ifndef WOLFSSL_MUTEX_INITIALIZER
6231+ if (inits_count_mutex_valid == 0) {
6232+ if (wc_InitMutex(&inits_count_mutex) != 0) {
6233+ WOLFSSL_MSG("Bad Init Mutex count");
6234+ return BAD_MUTEX_E;
6235+ }
6236+ else {
6237+ inits_count_mutex_valid = 1;
6238+ }
6239+ }
6240+ #endif /* !WOLFSSL_MUTEX_INITIALIZER */
6241+
6242+ if (wc_LockMutex(&inits_count_mutex) != 0) {
6243+ WOLFSSL_MSG("Bad Lock Mutex count");
6244+ return BAD_MUTEX_E;
6245+ }
6246+
6247+ if ((ret == WOLFSSL_SUCCESS) && (initRefCount == 0)) {
6248+ /* Initialize crypto for use with TLS connection */
6249+
62266250 #if FIPS_VERSION_GE(5,1)
62276251 ret = wolfCrypt_SetPrivateKeyReadEnable_fips(1, WC_KEYTYPE_ALL);
6228- if (ret != 0)
6229- return ret;
6230- else
6252+ if (ret == 0)
62316253 ret = WOLFSSL_SUCCESS;
62326254 #endif
62336255
6234- if (initRefCount == 0 ) {
6235- /* Initialize crypto for use with TLS connection */
6236- if (wolfCrypt_Init() != 0) {
6237- WOLFSSL_MSG("Bad wolfCrypt Init") ;
6238- ret = WC_INIT_E;
6256+ if (ret == WOLFSSL_SUCCESS ) {
6257+ if (wolfCrypt_Init() != 0) {
6258+ WOLFSSL_MSG("Bad wolfCrypt Init");
6259+ ret = WC_INIT_E ;
6260+ }
62396261 }
62406262
6241- #ifdef HAVE_GLOBAL_RNG
6263+ #if defined( HAVE_GLOBAL_RNG) && !defined(WOLFSSL_MUTEX_INITIALIZER)
62426264 if (ret == WOLFSSL_SUCCESS) {
62436265 if (wc_InitMutex(&globalRNGMutex) != 0) {
62446266 WOLFSSL_MSG("Bad Init Mutex rng");
@@ -6293,6 +6315,7 @@ int wolfSSL_Init(void)
62936315 }
62946316 #endif
62956317 #ifndef NO_CLIENT_CACHE
6318+ #ifndef WOLFSSL_MUTEX_INITIALIZER
62966319 if (ret == WOLFSSL_SUCCESS) {
62976320 if (wc_InitMutex(&clisession_mutex) != 0) {
62986321 WOLFSSL_MSG("Bad Init Mutex session");
@@ -6302,19 +6325,9 @@ int wolfSSL_Init(void)
63026325 clisession_mutex_valid = 1;
63036326 }
63046327 }
6328+ #endif
63056329 #endif
63066330#endif
6307- #ifndef WOLFSSL_MUTEX_INITIALIZER
6308- if (ret == WOLFSSL_SUCCESS) {
6309- if (wc_InitMutex(&count_mutex) != 0) {
6310- WOLFSSL_MSG("Bad Init Mutex count");
6311- ret = BAD_MUTEX_E;
6312- }
6313- else {
6314- count_mutex_valid = 1;
6315- }
6316- }
6317- #endif /* !WOLFSSL_MUTEX_INITIALIZER */
63186331#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
63196332 /* OpenSSL registers cleanup using atexit */
63206333 if ((ret == WOLFSSL_SUCCESS) && (atexit(AtExitCleanup) != 0)) {
@@ -6325,16 +6338,11 @@ int wolfSSL_Init(void)
63256338 }
63266339
63276340 if (ret == WOLFSSL_SUCCESS) {
6328- if (wc_LockMutex(&count_mutex) != 0) {
6329- WOLFSSL_MSG("Bad Lock Mutex count");
6330- ret = BAD_MUTEX_E;
6331- }
6332- else {
6333- initRefCount++;
6334- wc_UnLockMutex(&count_mutex);
6335- }
6341+ initRefCount++;
63366342 }
63376343
6344+ wc_UnLockMutex(&inits_count_mutex);
6345+
63386346 if (ret != WOLFSSL_SUCCESS) {
63396347 initRefCount = 1; /* Force cleanup */
63406348 (void)wolfSSL_Cleanup(); /* Ignore any error from cleanup */
@@ -13620,9 +13628,9 @@ int wolfSSL_Cleanup(void)
1362013628 WOLFSSL_ENTER("wolfSSL_Cleanup");
1362113629
1362213630#ifndef WOLFSSL_MUTEX_INITIALIZER
13623- if (count_mutex_valid == 1) {
13631+ if (inits_count_mutex_valid == 1) {
1362413632#endif
13625- if (wc_LockMutex(&count_mutex ) != 0) {
13633+ if (wc_LockMutex(&inits_count_mutex ) != 0) {
1362613634 WOLFSSL_MSG("Bad Lock Mutex count");
1362713635 return BAD_MUTEX_E;
1362813636 }
@@ -13637,9 +13645,9 @@ int wolfSSL_Cleanup(void)
1363713645 }
1363813646
1363913647#ifndef WOLFSSL_MUTEX_INITIALIZER
13640- if (count_mutex_valid == 1) {
13648+ if (inits_count_mutex_valid == 1) {
1364113649#endif
13642- wc_UnLockMutex(&count_mutex );
13650+ wc_UnLockMutex(&inits_count_mutex );
1364313651#ifndef WOLFSSL_MUTEX_INITIALIZER
1364413652 }
1364513653#endif
@@ -13683,21 +13691,23 @@ int wolfSSL_Cleanup(void)
1368313691 }
1368413692 }
1368513693 #ifndef NO_CLIENT_CACHE
13694+ #ifndef WOLFSSL_MUTEX_INITIALIZER
1368613695 if ((clisession_mutex_valid == 1) &&
1368713696 (wc_FreeMutex(&clisession_mutex) != 0)) {
1368813697 if (ret == WOLFSSL_SUCCESS)
1368913698 ret = BAD_MUTEX_E;
1369013699 }
1369113700 clisession_mutex_valid = 0;
1369213701 #endif
13702+ #endif
1369313703#endif /* !NO_SESSION_CACHE */
1369413704
1369513705#ifndef WOLFSSL_MUTEX_INITIALIZER
13696- if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex ) != 0)) {
13706+ if ((inits_count_mutex_valid == 1) && (wc_FreeMutex(&inits_count_mutex ) != 0)) {
1369713707 if (ret == WOLFSSL_SUCCESS)
1369813708 ret = BAD_MUTEX_E;
1369913709 }
13700- count_mutex_valid = 0;
13710+ inits_count_mutex_valid = 0;
1370113711#endif
1370213712
1370313713#ifdef OPENSSL_EXTRA
@@ -13718,11 +13728,13 @@ int wolfSSL_Cleanup(void)
1371813728#endif
1371913729
1372013730#ifdef HAVE_GLOBAL_RNG
13731+ #ifndef WOLFSSL_MUTEX_INITIALIZER
1372113732 if ((globalRNGMutex_valid == 1) && (wc_FreeMutex(&globalRNGMutex) != 0)) {
1372213733 if (ret == WOLFSSL_SUCCESS)
1372313734 ret = BAD_MUTEX_E;
1372413735 }
1372513736 globalRNGMutex_valid = 0;
13737+ #endif /* !WOLFSSL_MUTEX_INITIALIZER */
1372613738
1372713739 #if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
1372813740 wolfSSL_FIPS_drbg_free(gDrbgDefCtx);
@@ -32949,13 +32961,15 @@ void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
3294932961#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
3295032962static int wolfSSL_RAND_InitMutex(void)
3295132963{
32964+ #ifndef WOLFSSL_MUTEX_INITIALIZER
3295232965 if (gRandMethodsInit == 0) {
3295332966 if (wc_InitMutex(&gRandMethodMutex) != 0) {
3295432967 WOLFSSL_MSG("Bad Init Mutex rand methods");
3295532968 return BAD_MUTEX_E;
3295632969 }
3295732970 gRandMethodsInit = 1;
3295832971 }
32972+ #endif
3295932973 return 0;
3296032974}
3296132975#endif
@@ -33327,8 +33341,10 @@ void wolfSSL_RAND_Cleanup(void)
3332733341 wc_UnLockMutex(&gRandMethodMutex);
3332833342 }
3332933343
33344+ #ifndef WOLFSSL_MUTEX_INITIALIZER
3333033345 if (wc_FreeMutex(&gRandMethodMutex) == 0)
3333133346 gRandMethodsInit = 0;
33347+ #endif
3333233348#endif
3333333349#ifdef HAVE_GLOBAL_RNG
3333433350 if (wc_LockMutex(&globalRNGMutex) == 0) {
0 commit comments