Skip to content

Commit 8ce71cc

Browse files
committed
Call HaveUniqueSessionObj when we need to have a unique session object
1 parent 06d81f7 commit 8ce71cc

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/internal.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27456,6 +27456,20 @@ static int HashSkeData(WOLFSSL* ssl, enum wc_HashType hashType,
2745627456
/* client only parts */
2745727457
#ifndef NO_WOLFSSL_CLIENT
2745827458

27459+
int HaveUniqueSessionObj(WOLFSSL* ssl)
27460+
{
27461+
if (ssl->session->ref.count > 1) {
27462+
WOLFSSL_SESSION* newSession = wolfSSL_SESSION_dup(ssl->session);
27463+
if (newSession == NULL) {
27464+
WOLFSSL_MSG("Session duplicate failed");
27465+
return 0;
27466+
}
27467+
wolfSSL_FreeSession(ssl->ctx, ssl->session);
27468+
ssl->session = newSession;
27469+
}
27470+
return 1;
27471+
}
27472+
2745927473
#ifndef WOLFSSL_NO_TLS12
2746027474

2746127475
/* handle generation of client_hello (1) */
@@ -28295,6 +28309,11 @@ static int HashSkeData(WOLFSSL* ssl, enum wc_HashType hashType,
2829528309
else {
2829628310
if (DSH_CheckSessionId(ssl)) {
2829728311
if (SetCipherSpecs(ssl) == 0) {
28312+
if (!HaveUniqueSessionObj(ssl)) {
28313+
WOLFSSL_MSG("Unable to have unique session object");
28314+
WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR);
28315+
return MEMORY_ERROR;
28316+
}
2829828317

2829928318
XMEMCPY(ssl->arrays->masterSecret,
2830028319
ssl->session->masterSecret, SECRET_LEN);
@@ -31810,14 +31829,8 @@ int SendCertificateVerify(WOLFSSL* ssl)
3181031829
#ifdef HAVE_SESSION_TICKET
3181131830
int SetTicket(WOLFSSL* ssl, const byte* ticket, word32 length)
3181231831
{
31813-
/* If the session is shared, we need to copy-on-write */
31814-
if (ssl->session->ref.count > 1) {
31815-
WOLFSSL_SESSION* nsession = wolfSSL_SESSION_dup(ssl->session);
31816-
if (nsession == NULL)
31817-
return MEMORY_E;
31818-
wolfSSL_FreeSession(ssl->ctx, ssl->session);
31819-
ssl->session = nsession;
31820-
}
31832+
if (!HaveUniqueSessionObj(ssl))
31833+
return MEMORY_ERROR;
3182131834

3182231835
/* Free old dynamic ticket if we already had one */
3182331836
if (ssl->session->ticketLenAlloc > 0) {

src/ssl.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14173,21 +14173,15 @@ int wolfSSL_SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session)
1417314173
if (ssl->session == session) {
1417414174
WOLFSSL_MSG("ssl->session and session same");
1417514175
}
14176-
else
14177-
#ifdef HAVE_STUNNEL
14178-
/* stunnel depends on the ex_data not being duplicated. Copy OpenSSL
14179-
* behaviour for now. */
14180-
if (session->type != WOLFSSL_SESSION_TYPE_CACHE) {
14176+
else if (session->type != WOLFSSL_SESSION_TYPE_CACHE) {
1418114177
if (wolfSSL_SESSION_up_ref(session) == WOLFSSL_SUCCESS) {
1418214178
wolfSSL_FreeSession(ssl->ctx, ssl->session);
1418314179
ssl->session = session;
1418414180
}
1418514181
else
1418614182
ret = WOLFSSL_FAILURE;
1418714183
}
14188-
else
14189-
#endif
14190-
{
14184+
else {
1419114185
ret = wolfSSL_DupSession(session, ssl->session, 0);
1419214186
if (ret != WOLFSSL_SUCCESS)
1419314187
WOLFSSL_MSG("Session duplicate failed");
@@ -20607,7 +20601,6 @@ int wolfSSL_DupSession(const WOLFSSL_SESSION* input, WOLFSSL_SESSION* output,
2060720601

2060820602
WOLFSSL_SESSION* wolfSSL_SESSION_dup(WOLFSSL_SESSION* session)
2060920603
{
20610-
#ifdef HAVE_EXT_CACHE
2061120604
WOLFSSL_SESSION* copy;
2061220605

2061320606
WOLFSSL_ENTER("wolfSSL_SESSION_dup");
@@ -20630,11 +20623,6 @@ WOLFSSL_SESSION* wolfSSL_SESSION_dup(WOLFSSL_SESSION* session)
2063020623
copy = NULL;
2063120624
}
2063220625
return copy;
20633-
#else
20634-
WOLFSSL_MSG("wolfSSL_SESSION_dup feature not compiled in");
20635-
(void)session;
20636-
return NULL;
20637-
#endif /* HAVE_EXT_CACHE */
2063820626
}
2063920627

2064020628
void wolfSSL_FreeSession(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)

src/tls13.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,12 @@ static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk, int clientHello)
37043704
if (psk == NULL)
37053705
return BAD_FUNC_ARG;
37063706

3707+
if (!HaveUniqueSessionObj(ssl)) {
3708+
WOLFSSL_MSG("Unable to have unique session object");
3709+
WOLFSSL_ERROR_VERBOSE(MEMORY_ERROR);
3710+
return MEMORY_ERROR;
3711+
}
3712+
37073713
suite[0] = ssl->options.cipherSuite0;
37083714
suite[1] = ssl->options.cipherSuite;
37093715

wolfssl/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6209,6 +6209,7 @@ WOLFSSL_LOCAL void DoCertFatalAlert(WOLFSSL* ssl, int ret);
62096209
WOLFSSL_LOCAL int cipherExtraData(WOLFSSL* ssl);
62106210

62116211
#ifndef NO_WOLFSSL_CLIENT
6212+
WOLFSSL_LOCAL int HaveUniqueSessionObj(WOLFSSL* ssl);
62126213
WOLFSSL_LOCAL int SendClientHello(WOLFSSL* ssl);
62136214
WOLFSSL_LOCAL int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
62146215
word32 size);

0 commit comments

Comments
 (0)