Skip to content

Commit 80c8c62

Browse files
committed
Proper initial_ctx clean up
- Call wolfSSL_CTX_free on ssl->initial_ctx so that it decrements the counter and free's the object - Clean up where ssl->initial_ctx is free'd. It only needs to be free'd when the ssl object is being free'd
1 parent 96205fc commit 80c8c62

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/internal.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6455,7 +6455,7 @@ int InitSSL_Suites(WOLFSSL* ssl)
64556455
WOLFSSL_SUCCESS return value on success */
64566456
int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
64576457
{
6458-
int ret;
6458+
int ret = WOLFSSL_SUCCESS; /* set default ret */
64596459
byte newSSL;
64606460

64616461
WOLFSSL_ENTER("SetSSL_CTX");
@@ -6475,38 +6475,35 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
64756475
if (!newSSL) {
64766476
WOLFSSL_MSG("freeing old ctx to decrement reference count. Switching ctx.");
64776477
wolfSSL_CTX_free(ssl->ctx);
6478-
#if defined(WOLFSSL_HAPROXY)
6479-
wolfSSL_CTX_free(ssl->initial_ctx);
6480-
#endif
64816478
}
64826479

64836480
/* increment CTX reference count */
6484-
wolfSSL_RefInc(&ctx->ref, &ret);
6481+
ret = wolfSSL_CTX_up_ref(ctx);
64856482
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
6486-
if (ret < 0) {
6483+
if (ret != WOLFSSL_SUCCESS) {
64876484
return ret;
64886485
}
64896486
#else
64906487
(void)ret;
64916488
#endif
6492-
ret = WOLFSSL_SUCCESS; /* set default ret */
64936489

64946490
ssl->ctx = ctx; /* only for passing to calls, options could change */
64956491
/* Don't change version on a SSL object that has already started a
64966492
* handshake */
64976493
#if defined(WOLFSSL_HAPROXY)
6498-
ret = wolfSSL_CTX_up_ref(ctx);
6499-
if (ret == WOLFSSL_SUCCESS) {
6500-
ssl->initial_ctx = ctx; /* Save access to session key materials */
6501-
}
6502-
else {
6503-
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
6504-
return ret;
6505-
#else
6506-
(void)ret;
6507-
#endif
6494+
if (ssl->initial_ctx == NULL) {
6495+
ret = wolfSSL_CTX_up_ref(ctx);
6496+
if (ret == WOLFSSL_SUCCESS) {
6497+
ssl->initial_ctx = ctx; /* Save access to session key materials */
6498+
}
6499+
else {
6500+
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
6501+
return ret;
6502+
#else
6503+
(void)ret;
6504+
#endif
6505+
}
65086506
}
6509-
65106507
#endif
65116508
if (!ssl->msgsReceived.got_client_hello &&
65126509
!ssl->msgsReceived.got_server_hello)
@@ -8244,6 +8241,10 @@ void SSL_ResourceFree(WOLFSSL* ssl)
82448241
#ifdef WOLFSSL_QUIC
82458242
wolfSSL_quic_free(ssl);
82468243
#endif
8244+
#if defined(WOLFSSL_HAPROXY)
8245+
wolfSSL_CTX_free(ssl->initial_ctx);
8246+
ssl->initial_ctx = NULL;
8247+
#endif
82478248
}
82488249

82498250
/* Free any handshake resources no longer needed */

src/ssl.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30246,12 +30246,8 @@ WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
3024630246
#else
3024730247
(void)ret;
3024830248
#endif
30249-
if (ssl->ctx) {
30249+
if (ssl->ctx != NULL)
3025030250
wolfSSL_CTX_free(ssl->ctx);
30251-
#if defined(WOLFSSL_HAPROXY)
30252-
wolfSSL_CTX_free(ssl->initial_ctx);
30253-
#endif
30254-
}
3025530251
ssl->ctx = ctx;
3025630252

3025730253
#ifndef NO_CERTS

0 commit comments

Comments
 (0)