@@ -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 */
@@ -13603,9 +13611,9 @@ int wolfSSL_Cleanup(void)
1360313611 WOLFSSL_ENTER("wolfSSL_Cleanup");
1360413612
1360513613#ifndef WOLFSSL_MUTEX_INITIALIZER
13606- if (count_mutex_valid == 1) {
13614+ if (inits_count_mutex_valid == 1) {
1360713615#endif
13608- if (wc_LockMutex(&count_mutex ) != 0) {
13616+ if (wc_LockMutex(&inits_count_mutex ) != 0) {
1360913617 WOLFSSL_MSG("Bad Lock Mutex count");
1361013618 return BAD_MUTEX_E;
1361113619 }
@@ -13620,9 +13628,9 @@ int wolfSSL_Cleanup(void)
1362013628 }
1362113629
1362213630#ifndef WOLFSSL_MUTEX_INITIALIZER
13623- if (count_mutex_valid == 1) {
13631+ if (inits_count_mutex_valid == 1) {
1362413632#endif
13625- wc_UnLockMutex(&count_mutex );
13633+ wc_UnLockMutex(&inits_count_mutex );
1362613634#ifndef WOLFSSL_MUTEX_INITIALIZER
1362713635 }
1362813636#endif
@@ -13666,21 +13674,23 @@ int wolfSSL_Cleanup(void)
1366613674 }
1366713675 }
1366813676 #ifndef NO_CLIENT_CACHE
13677+ #ifndef WOLFSSL_MUTEX_INITIALIZER
1366913678 if ((clisession_mutex_valid == 1) &&
1367013679 (wc_FreeMutex(&clisession_mutex) != 0)) {
1367113680 if (ret == WOLFSSL_SUCCESS)
1367213681 ret = BAD_MUTEX_E;
1367313682 }
1367413683 clisession_mutex_valid = 0;
1367513684 #endif
13685+ #endif
1367613686#endif /* !NO_SESSION_CACHE */
1367713687
1367813688#ifndef WOLFSSL_MUTEX_INITIALIZER
13679- if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex ) != 0)) {
13689+ if ((inits_count_mutex_valid == 1) && (wc_FreeMutex(&inits_count_mutex ) != 0)) {
1368013690 if (ret == WOLFSSL_SUCCESS)
1368113691 ret = BAD_MUTEX_E;
1368213692 }
13683- count_mutex_valid = 0;
13693+ inits_count_mutex_valid = 0;
1368413694#endif
1368513695
1368613696#ifdef OPENSSL_EXTRA
@@ -13701,11 +13711,13 @@ int wolfSSL_Cleanup(void)
1370113711#endif
1370213712
1370313713#ifdef HAVE_GLOBAL_RNG
13714+ #ifndef WOLFSSL_MUTEX_INITIALIZER
1370413715 if ((globalRNGMutex_valid == 1) && (wc_FreeMutex(&globalRNGMutex) != 0)) {
1370513716 if (ret == WOLFSSL_SUCCESS)
1370613717 ret = BAD_MUTEX_E;
1370713718 }
1370813719 globalRNGMutex_valid = 0;
13720+ #endif /* !WOLFSSL_MUTEX_INITIALIZER */
1370913721
1371013722 #if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
1371113723 wolfSSL_FIPS_drbg_free(gDrbgDefCtx);
@@ -32932,13 +32944,15 @@ void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
3293232944#if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
3293332945static int wolfSSL_RAND_InitMutex(void)
3293432946{
32947+ #ifndef WOLFSSL_MUTEX_INITIALIZER
3293532948 if (gRandMethodsInit == 0) {
3293632949 if (wc_InitMutex(&gRandMethodMutex) != 0) {
3293732950 WOLFSSL_MSG("Bad Init Mutex rand methods");
3293832951 return BAD_MUTEX_E;
3293932952 }
3294032953 gRandMethodsInit = 1;
3294132954 }
32955+ #endif
3294232956 return 0;
3294332957}
3294432958#endif
@@ -33310,8 +33324,10 @@ void wolfSSL_RAND_Cleanup(void)
3331033324 wc_UnLockMutex(&gRandMethodMutex);
3331133325 }
3331233326
33327+ #ifndef WOLFSSL_MUTEX_INITIALIZER
3331333328 if (wc_FreeMutex(&gRandMethodMutex) == 0)
3331433329 gRandMethodsInit = 0;
33330+ #endif
3331533331#endif
3331633332#ifdef HAVE_GLOBAL_RNG
3331733333 if (wc_LockMutex(&globalRNGMutex) == 0) {
0 commit comments