Skip to content

Commit 2ffc818

Browse files
authored
Merge pull request #7069 from douzzer/20231213-misc-fixes
20231213-misc-fixes
2 parents 27c6ee4 + 64e4796 commit 2ffc818

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

examples/client/client.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37003700
if (wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR_OCSP,
37013701
WOLFSSL_CSR_OCSP_USE_NONCE) != WOLFSSL_SUCCESS) {
37023702
wolfSSL_free(ssl); ssl = NULL;
3703+
CloseSocket(sockfd);
37033704
wolfSSL_CTX_free(ctx); ctx = NULL;
37043705
err_sys("UseCertificateStatusRequest failed");
37053706
}
@@ -3711,6 +3712,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37113712
WOLFSSL_CSR2_OCSP, WOLFSSL_CSR2_OCSP_USE_NONCE)
37123713
!= WOLFSSL_SUCCESS) {
37133714
wolfSSL_free(ssl); ssl = NULL;
3715+
CloseSocket(sockfd);
37143716
wolfSSL_CTX_free(ctx); ctx = NULL;
37153717
err_sys("UseCertificateStatusRequest failed");
37163718
}
@@ -3720,6 +3722,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37203722
WOLFSSL_CSR2_OCSP_MULTI, 0)
37213723
!= WOLFSSL_SUCCESS) {
37223724
wolfSSL_free(ssl); ssl = NULL;
3725+
CloseSocket(sockfd);
37233726
wolfSSL_CTX_free(ctx); ctx = NULL;
37243727
err_sys("UseCertificateStatusRequest failed");
37253728
}
@@ -3748,6 +3751,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37483751
tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, ssl);
37493752
if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
37503753
wolfSSL_free(ssl); ssl = NULL;
3754+
CloseSocket(sockfd);
37513755
wolfSSL_CTX_free(ctx); ctx = NULL;
37523756
err_sys("error in setting fd");
37533757
}
@@ -3763,6 +3767,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37633767
if (doSTARTTLS) {
37643768
if (StartTLS_Init(&sockfd) != WOLFSSL_SUCCESS) {
37653769
wolfSSL_free(ssl); ssl = NULL;
3770+
CloseSocket(sockfd);
37663771
wolfSSL_CTX_free(ctx); ctx = NULL;
37673772
err_sys("error during STARTTLS protocol");
37683773
}
@@ -3776,17 +3781,20 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37763781

37773782
if (wolfSSL_EnableCRL(ssl, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS) {
37783783
wolfSSL_free(ssl); ssl = NULL;
3784+
CloseSocket(sockfd);
37793785
wolfSSL_CTX_free(ctx); ctx = NULL;
37803786
err_sys("can't enable crl check");
37813787
}
37823788
if (wolfSSL_LoadCRL(ssl, crlPemDir, WOLFSSL_FILETYPE_PEM, 0)
37833789
!= WOLFSSL_SUCCESS) {
37843790
wolfSSL_free(ssl); ssl = NULL;
3791+
CloseSocket(sockfd);
37853792
wolfSSL_CTX_free(ctx); ctx = NULL;
37863793
err_sys("can't load crl, check crlfile and date validity");
37873794
}
37883795
if (wolfSSL_SetCRL_Cb(ssl, CRL_CallBack) != WOLFSSL_SUCCESS) {
37893796
wolfSSL_free(ssl); ssl = NULL;
3797+
CloseSocket(sockfd);
37903798
wolfSSL_CTX_free(ctx); ctx = NULL;
37913799
err_sys("can't set crl callback");
37923800
}
@@ -3796,6 +3804,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
37963804
if (scr) {
37973805
if (wolfSSL_UseSecureRenegotiation(ssl) != WOLFSSL_SUCCESS) {
37983806
wolfSSL_free(ssl); ssl = NULL;
3807+
CloseSocket(sockfd);
37993808
wolfSSL_CTX_free(ctx); ctx = NULL;
38003809
err_sys("can't enable secure renegotiation");
38013810
}
@@ -3948,13 +3957,15 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
39483957
size = wolfSSL_get_client_random(NULL, NULL, 0);
39493958
if (size == 0) {
39503959
wolfSSL_free(ssl); ssl = NULL;
3960+
CloseSocket(sockfd);
39513961
wolfSSL_CTX_free(ctx); ctx = NULL;
39523962
err_sys("error getting client random buffer size");
39533963
}
39543964

39553965
rnd = (byte*)XMALLOC(size, NULL, DYNAMIC_TYPE_TMP_BUFFER);
39563966
if (rnd == NULL) {
39573967
wolfSSL_free(ssl); ssl = NULL;
3968+
CloseSocket(sockfd);
39583969
wolfSSL_CTX_free(ctx); ctx = NULL;
39593970
err_sys("error creating client random buffer");
39603971
}
@@ -3963,6 +3974,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
39633974
if (size == 0) {
39643975
XFREE(rnd, NULL, DYNAMIC_TYPE_TMP_BUFFER);
39653976
wolfSSL_free(ssl); ssl = NULL;
3977+
CloseSocket(sockfd);
39663978
wolfSSL_CTX_free(ctx); ctx = NULL;
39673979
err_sys("error getting client random buffer");
39683980
}
@@ -4001,6 +4013,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
40014013
if (XSTRCMP(starttlsProt, "smtp") == 0) {
40024014
if (SMTP_Shutdown(ssl, wc_shutdown) != WOLFSSL_SUCCESS) {
40034015
wolfSSL_free(ssl); ssl = NULL;
4016+
CloseSocket(sockfd);
40044017
wolfSSL_CTX_free(ctx); ctx = NULL;
40054018
err_sys("error closing STARTTLS connection");
40064019
}
@@ -4115,6 +4128,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
41154128
LOG_ERROR("wolfSSL_Rehandshake error %d, %s\n", err,
41164129
wolfSSL_ERR_error_string(err, buffer));
41174130
wolfSSL_free(ssl); ssl = NULL;
4131+
CloseSocket(sockfd);
41184132
wolfSSL_CTX_free(ctx); ctx = NULL;
41194133
err_sys("non-blocking wolfSSL_Rehandshake failed");
41204134
}
@@ -4145,6 +4159,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
41454159
printf("err = %d, %s\n", err,
41464160
wolfSSL_ERR_error_string(err, buffer));
41474161
wolfSSL_free(ssl); ssl = NULL;
4162+
CloseSocket(sockfd);
41484163
wolfSSL_CTX_free(ctx); ctx = NULL;
41494164
err_sys("wolfSSL_Rehandshake failed");
41504165
}
@@ -4174,6 +4189,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
41744189
printf("err = %d, %s\n", err,
41754190
wolfSSL_ERR_error_string(err, buffer));
41764191
wolfSSL_free(ssl); ssl = NULL;
4192+
CloseSocket(sockfd);
41774193
wolfSSL_CTX_free(ctx); ctx = NULL;
41784194
err_sys("wolfSSL_SecureResume failed");
41794195
}
@@ -4210,6 +4226,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
42104226
if (exitWithRet) {
42114227
((func_args*)args)->return_code = err;
42124228
wolfSSL_free(ssl); ssl = NULL;
4229+
CloseSocket(sockfd);
42134230
wolfSSL_CTX_free(ctx); ctx = NULL;
42144231
goto exit;
42154232
}
@@ -4229,6 +4246,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
42294246
if (exitWithRet && (err != 0)) {
42304247
((func_args*)args)->return_code = err;
42314248
wolfSSL_free(ssl); ssl = NULL;
4249+
CloseSocket(sockfd);
42324250
wolfSSL_CTX_free(ctx); ctx = NULL;
42334251
goto exit;
42344252
}
@@ -4346,6 +4364,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
43464364
tcp_connect(&sockfd, host, port, dtlsUDP, dtlsSCTP, sslResume);
43474365
if (wolfSSL_set_fd(sslResume, sockfd) != WOLFSSL_SUCCESS) {
43484366
wolfSSL_free(sslResume); sslResume = NULL;
4367+
CloseSocket(sockfd);
43494368
wolfSSL_CTX_free(ctx); ctx = NULL;
43504369
err_sys("error in setting fd");
43514370
}
@@ -4366,6 +4385,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
43664385
if (scr) {
43674386
if (wolfSSL_UseSecureRenegotiation(sslResume) != WOLFSSL_SUCCESS) {
43684387
wolfSSL_free(sslResume); sslResume = NULL;
4388+
CloseSocket(sockfd);
43694389
wolfSSL_CTX_free(ctx); ctx = NULL;
43704390
err_sys("can't enable secure renegotiation");
43714391
}
@@ -4440,6 +4460,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
44404460
LOG_ERROR("wolfSSL_connect resume error %d, %s\n", err,
44414461
wolfSSL_ERR_error_string(err, buffer));
44424462
wolfSSL_free(sslResume); sslResume = NULL;
4463+
CloseSocket(sockfd);
44434464
wolfSSL_CTX_free(ctx); ctx = NULL;
44444465
err_sys("wolfSSL_connect resume failed");
44454466
}
@@ -4488,6 +4509,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
44884509
LOG_ERROR("err = %d, %s\n", err,
44894510
wolfSSL_ERR_error_string(err, buffer));
44904511
wolfSSL_free(sslResume); sslResume = NULL;
4512+
CloseSocket(sockfd);
44914513
wolfSSL_CTX_free(ctx); ctx = NULL;
44924514
err_sys("wolfSSL_Rehandshake failed");
44934515
}
@@ -4502,6 +4524,7 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
45024524
LOG_ERROR("err = %d, %s\n", err,
45034525
wolfSSL_ERR_error_string(err, buffer));
45044526
wolfSSL_free(sslResume); sslResume = NULL;
4527+
CloseSocket(sockfd);
45054528
wolfSSL_CTX_free(ctx); ctx = NULL;
45064529
err_sys("wolfSSL_SecureResume failed");
45074530
}

src/ssl.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,12 @@ int wolfSSL_send_session(WOLFSSL* ssl)
12651265

12661266
/* prevent multiple mutex initializations */
12671267
static volatile WOLFSSL_GLOBAL int initRefCount = 0;
1268+
#ifdef WOLFSSL_MUTEX_INITIALIZER
1269+
static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex = WOLFSSL_MUTEX_INITIALIZER;
1270+
#else
12681271
static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex; /* init ref count mutex */
12691272
static WOLFSSL_GLOBAL int count_mutex_valid = 0;
1273+
#endif
12701274

12711275
/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
12721276
WOLFSSL_METHOD pointer passed in is given to ctx to manage.
@@ -6258,6 +6262,7 @@ int wolfSSL_Init(void)
62586262
}
62596263
#endif
62606264
#endif
6265+
#ifndef WOLFSSL_MUTEX_INITIALIZER
62616266
if (ret == WOLFSSL_SUCCESS) {
62626267
if (wc_InitMutex(&count_mutex) != 0) {
62636268
WOLFSSL_MSG("Bad Init Mutex count");
@@ -6267,6 +6272,7 @@ int wolfSSL_Init(void)
62676272
count_mutex_valid = 1;
62686273
}
62696274
}
6275+
#endif /* !WOLFSSL_MUTEX_INITIALIZER */
62706276
#if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
62716277
/* OpenSSL registers cleanup using atexit */
62726278
if ((ret == WOLFSSL_SUCCESS) && (atexit(AtExitCleanup) != 0)) {
@@ -13378,21 +13384,30 @@ int wolfSSL_Cleanup(void)
1337813384

1337913385
WOLFSSL_ENTER("wolfSSL_Cleanup");
1338013386

13381-
if (initRefCount == 0)
13382-
return ret; /* possibly no init yet, but not failure either way */
13383-
13384-
if ((count_mutex_valid == 1) && (wc_LockMutex(&count_mutex) != 0)) {
13385-
WOLFSSL_MSG("Bad Lock Mutex count");
13386-
ret = BAD_MUTEX_E;
13387+
#ifndef WOLFSSL_MUTEX_INITIALIZER
13388+
if (count_mutex_valid == 1) {
13389+
#endif
13390+
if (wc_LockMutex(&count_mutex) != 0) {
13391+
WOLFSSL_MSG("Bad Lock Mutex count");
13392+
return BAD_MUTEX_E;
13393+
}
13394+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1338713395
}
13396+
#endif
1338813397

13389-
release = initRefCount-- == 1;
13390-
if (initRefCount < 0)
13391-
initRefCount = 0;
13398+
if (initRefCount > 0) {
13399+
--initRefCount;
13400+
if (initRefCount == 0)
13401+
release = 1;
13402+
}
1339213403

13404+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1339313405
if (count_mutex_valid == 1) {
13406+
#endif
1339413407
wc_UnLockMutex(&count_mutex);
13408+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1339513409
}
13410+
#endif
1339613411

1339713412
if (!release)
1339813413
return ret;
@@ -13442,11 +13457,13 @@ int wolfSSL_Cleanup(void)
1344213457
#endif
1344313458
#endif /* !NO_SESSION_CACHE */
1344413459

13460+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1344513461
if ((count_mutex_valid == 1) && (wc_FreeMutex(&count_mutex) != 0)) {
1344613462
if (ret == WOLFSSL_SUCCESS)
1344713463
ret = BAD_MUTEX_E;
1344813464
}
1344913465
count_mutex_valid = 0;
13466+
#endif
1345013467

1345113468
#ifdef OPENSSL_EXTRA
1345213469
wolfSSL_RAND_Cleanup();
@@ -14339,6 +14356,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1433914356
{
1434014357
int error = -1;
1434114358
word32 clientRow = 0, clientIdx = 0;
14359+
ClientSession* ret = NULL;
14360+
1434214361
(void)useTicket;
1434314362
if (side == WOLFSSL_CLIENT_END
1434414363
&& row != INVALID_SESSION_ROW
@@ -14392,6 +14411,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1439214411
ClientCache[clientRow].nextIdx %= CLIENT_SESSIONS_PER_ROW;
1439314412
}
1439414413

14414+
ret = &ClientCache[clientRow].Clients[clientIdx];
14415+
1439514416
wc_UnLockMutex(&clisession_mutex);
1439614417
}
1439714418
else {
@@ -14402,10 +14423,8 @@ ClientSession* AddSessionToClientCache(int side, int row, int idx, byte* serverI
1440214423
else {
1440314424
WOLFSSL_MSG("Skipping client cache");
1440414425
}
14405-
if (error == 0)
14406-
return &ClientCache[clientRow].Clients[clientIdx];
14407-
else
14408-
return NULL;
14426+
14427+
return ret;
1440914428
}
1441014429
#endif /* !NO_CLIENT_CACHE */
1441114430

tests/api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45495,6 +45495,8 @@ static int test_wolfSSL_SESSION(void)
4549545495
wolfSSL_shutdown(ssl);
4549645496
wolfSSL_free(ssl); ssl = NULL;
4549745497

45498+
CloseSocket(sockfd);
45499+
4549845500
join_thread(serverThread);
4549945501

4550045502
FreeTcpReady(&ready);

wolfssl/wolfcrypt/wc_port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@
307307
#if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
308308
typedef wolfSSL_Mutex wolfSSL_RwLock;
309309
#endif
310+
#ifdef WOLFSSL_PTHREADS
311+
#define WOLFSSL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
312+
#endif
310313

311314
#ifndef WOLFSSL_NO_ATOMICS
312315
#ifdef HAVE_C___ATOMIC

0 commit comments

Comments
 (0)