Skip to content
Open
205 changes: 142 additions & 63 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -8202,10 +8202,9 @@ int InitSSL_Suites(WOLFSSL* ssl)
writeDup flag indicating this is a write dup only

WOLFSSL_SUCCESS return value on success */
#ifdef OPENSSL_EXTRA
/* Apply the protocol version mask and downgrade rules. Returns VERSION_ERROR
* when the requested options are inconsistent, 0 otherwise. */
static int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
#ifdef WOLFSSL_TLS13
if (ssl->version.minor == TLSv1_3_MINOR &&
Expand Down Expand Up @@ -8271,7 +8270,6 @@ static int SetSSL_CTX_CheckVersion(WOLFSSL* ssl, WOLFSSL_CTX* ctx)

return 0;
}
#endif /* OPENSSL_EXTRA */

#ifndef NO_CERTS
/* Give the SSL object the certificate, certificate chain and private key of
Expand Down Expand Up @@ -9024,6 +9022,13 @@ int ReinitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
}
(void)ctx;

/* Called on every connect/accept, so only when a connection starts. */
if (!ssl->options.handShakeDone &&
(((ssl->options.side == WOLFSSL_CLIENT_END) ?
ssl->options.connectState : ssl->options.acceptState) == 0)) {
ResetPeerResults(ssl);
}
Comment on lines +9025 to +9030

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Resumed client connections now report X509_V_OK from wolfSSL_get_verify_result(), even when the cached session recorded a verify error. The new ResetPeerResults() call in ReinitSSL runs at the first wolfSSL_connect(). That is after wolfSSL_set_session() has copied session->peerVerifyRet into ssl->peerVerifyRet (ssl_sess.c:1750). A resumption verifies no certificate, so the value stays 0. Fix: keep a value restored from the session (and one set with wolfSSL_set_verify_result()) when the connection starts. For example, skip the peerVerifyRet reset when options.resuming is set, or re-apply ssl->session->peerVerifyRet after the reset.

Why this was flagged

An OPENSSL_EXTRA client using WOLFSSL_VERIFY_NONE completes a first handshake with a bad certificate, so peerVerifyRet is nonzero. For the next connection the app calls wolfSSL_set_session(). SetSession sets options.resuming and copies session->peerVerifyRet into ssl->peerVerifyRet (ssl_sess.c:1750). wolfSSL_connect() then calls ReinitSSL (ssl_api_hs.c:413, tls13.c:16992). At that point handShakeDone == 0 and connectState == CONNECT_BEGIN (0), so the new block at internal.c:9025-9030 calls ResetPeerResults(), which sets peerVerifyRet = 0 (internal.c:10968). The resumed handshake processes no Certificate, so wolfSSL_get_verify_result() returns 0 (X509_V_OK). An app that checks the verify result by hand then accepts an unverified peer. When the session is saved again, ssl_sess.c:4076 writes the 0 back, so the error is gone for every later resumption too. On the base branch, ReinitSSL never touched peerVerifyRet, so the restored error was still reported.

Verification: SetSession restores the result before connect (ssl_sess.c:1750). In wolfSSL_connect (ssl_api_hs.c:413), ReinitSSL runs while handShakeDone==0 and connectState==CONNECT_BEGIN, so the new block at internal.c:9025-9030 calls ResetPeerResults(), which sets ssl->peerVerifyRet = 0; (internal.c:10968). On the base branch this reset did not exist. The new tests check only full handshakes and never resumption.


ssl->options.shutdownDone = 0;
if (ssl->session != NULL)
ssl->session->side = (byte)ssl->options.side;
Expand Down Expand Up @@ -9434,18 +9439,7 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
#endif


#ifndef WOLFSSL_AEAD_ONLY
#ifndef NO_OLD_TLS
ssl->hmac = SSL_hmac; /* default to SSLv3 */
#elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS)
#if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \
!defined(WOLFSSL_RENESAS_TSIP_TLS)
ssl->hmac = TLS_hmac;
#else
ssl->hmac = Renesas_cmn_TLS_hmac;
#endif
#endif
#endif /* WOLFSSL_AEAD_ONLY */
SetDefaultHmac(ssl);

#if defined(WOLFSSL_OPENVPN) && defined(HAVE_KEYING_MATERIAL)
/* Save arrays by default for OpenVPN */
Expand Down Expand Up @@ -10332,6 +10326,40 @@ void FreeKeyExchange(WOLFSSL* ssl)
#endif
}

#ifdef WOLFSSL_ASYNC_CRYPT
/* Take the object's event off the CTX queue before its owner is freed. A
* completed event can still be queued, and a removed one keeps stale links, so
* look it up in the queue rather than trusting its state. */
int AsyncDetachEvent(WOLFSSL* ssl)
{
WOLF_EVENT* event;
WOLF_EVENT* cur;

if (ssl->asyncDev == NULL)
return 0;
event = &ssl->asyncDev->event;
if (ssl->ctx != NULL) {
WOLF_EVENT_QUEUE* queue = &ssl->ctx->event_queue;
#ifndef SINGLE_THREADED
if (wc_LockMutex(&queue->lock) != 0)
return BAD_MUTEX_E;
#endif
for (cur = queue->head; cur != NULL; cur = cur->next) {
if (cur == event) {
(void)wolfEventQueue_Remove(queue, event);
break;
}
}
#ifndef SINGLE_THREADED
wc_UnLockMutex(&queue->lock);
#endif
}
XMEMSET(event, 0, sizeof(*event));
ssl->asyncDev = NULL;
return 0;
}
#endif

#if defined(WOLFSSL_ASYNC_CRYPT) && \
(!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
defined(HAVE_ED448))
Expand All @@ -10340,17 +10368,8 @@ static void DetachPendingAsyncDev(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev)
if (ssl == NULL || asyncDev == NULL)
return;

/* asyncDev is set when the event is queued. Init alone sets PENDING and
* leaves the event unlinked, so do not remove in that window. */
if (ssl->asyncDev == asyncDev) {
if (asyncDev->event.state == WOLF_EVENT_STATE_PENDING &&
ssl->ctx != NULL) {
(void)wolfEventQueue_Remove(&ssl->ctx->event_queue,
&asyncDev->event);
}
XMEMSET(&asyncDev->event, 0, sizeof(asyncDev->event));
ssl->asyncDev = NULL;
}
if (ssl->asyncDev == asyncDev)
(void)AsyncDetachEvent(ssl);
}
#endif

Expand Down Expand Up @@ -10951,7 +10970,65 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl)
#endif /* WOLFSSL_STATIC_MEMORY */
}

/* The MAC function a new object starts with. */
void SetDefaultHmac(WOLFSSL* ssl)
{
#ifndef WOLFSSL_AEAD_ONLY
#ifndef NO_OLD_TLS
ssl->hmac = SSL_hmac; /* default to SSLv3 */
#elif !defined(WOLFSSL_NO_TLS12) && !defined(NO_TLS)
#if !defined(WOLFSSL_RENESAS_FSPSM_TLS) && \
!defined(WOLFSSL_RENESAS_TSIP_TLS)
ssl->hmac = TLS_hmac;
#else
ssl->hmac = Renesas_cmn_TLS_hmac;
#endif
#endif
#endif /* WOLFSSL_AEAD_ONLY */
(void)ssl;
}

/* Drop what the application could query about the previous connection. */
void ResetPeerResults(WOLFSSL* ssl)
{
ssl->alert_history.last_rx.code = -1;
ssl->alert_history.last_rx.level = -1;
ssl->alert_history.last_tx.code = -1;
ssl->alert_history.last_tx.level = -1;
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
defined(OPENSSL_ALL)
ssl->peerVerifyRet = 0;
#endif
}

/* Free any handshake resources no longer needed */
/* Free the ephemeral key exchange material. Each handshake, a renegotiation
* included, makes its own. */
void FreeEphemeralKeys(WOLFSSL* ssl)
{
#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
/* Present holds the type; only an ECC key is left allocated without it. */
FreeKey(ssl, (ssl->eccTempKeyPresent != 0) ? ssl->eccTempKeyPresent :
DYNAMIC_TYPE_ECC, (void**)&ssl->eccTempKey);
ssl->eccTempKeyPresent = 0;
#endif
#ifndef NO_DH
if (ssl->buffers.serverDH_Priv.buffer != NULL) {
ForceZero(ssl->buffers.serverDH_Priv.buffer,
ssl->buffers.serverDH_Priv.length);
}
XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap,
DYNAMIC_TYPE_PRIVATE_KEY);
ssl->buffers.serverDH_Priv.buffer = NULL;
ssl->buffers.serverDH_Priv.length = 0;
Comment thread
kareem-wolfssl marked this conversation as resolved.
Resolved
Comment thread
kareem-wolfssl marked this conversation as resolved.
Resolved
XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
ssl->buffers.serverDH_Pub.buffer = NULL;
ssl->buffers.serverDH_Pub.length = 0;
FreeKey(ssl, DYNAMIC_TYPE_DH, (void**)&ssl->buffers.serverDH_Key);
#endif
(void)ssl;
}

void FreeHandshakeResources(WOLFSSL* ssl)
{
WOLFSSL_ENTER("FreeHandshakeResources");
Expand All @@ -10975,6 +11052,8 @@ void FreeHandshakeResources(WOLFSSL* ssl)
}
#endif

FreeEphemeralKeys(ssl);

#ifdef HAVE_SECURE_RENEGOTIATION
/* advertiseOnly clients never renegotiate, so do not retain (and leave
* unzeroed) the master secret and other handshake resources for them. */
Expand Down Expand Up @@ -11048,35 +11127,6 @@ void FreeHandshakeResources(WOLFSSL* ssl)
FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccKey);
ssl->peerEccKeyPresent = 0;
#endif
#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
{
int dtype;
#ifdef HAVE_ECC
dtype = DYNAMIC_TYPE_ECC;
#elif defined(HAVE_CURVE25519)
dtype = DYNAMIC_TYPE_CURVE25519;
#else
dtype = DYNAMIC_TYPE_CURVE448;
#endif
#if defined(HAVE_ECC) && defined(HAVE_CURVE25519)
if (ssl->peerX25519KeyPresent ||
ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE25519)
{
dtype = DYNAMIC_TYPE_CURVE25519;
}
#endif
#if (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
defined(HAVE_CURVE448)
if (ssl->peerX448KeyPresent ||
ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE448)
{
dtype = DYNAMIC_TYPE_CURVE448;
}
#endif
FreeKey(ssl, dtype, (void**)&ssl->eccTempKey);
ssl->eccTempKeyPresent = 0;
}
#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
#ifdef HAVE_CURVE25519
FreeKey(ssl, DYNAMIC_TYPE_CURVE25519, (void**)&ssl->peerX25519Key);
ssl->peerX25519KeyPresent = 0;
Expand All @@ -11087,14 +11137,6 @@ void FreeHandshakeResources(WOLFSSL* ssl)
#endif

#ifndef NO_DH
if (ssl->buffers.serverDH_Priv.buffer) {
ForceZero(ssl->buffers.serverDH_Priv.buffer,
ssl->buffers.serverDH_Priv.length);
}
XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
ssl->buffers.serverDH_Priv.buffer = NULL;
XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
ssl->buffers.serverDH_Pub.buffer = NULL;
/* Release the parameters (p,g) back here rather than hold them for
* the life of the connection. */
if (ssl->buffers.weOwnDH) {
Expand Down Expand Up @@ -22732,6 +22774,7 @@ static int ResetHandshakeStateForReneg(WOLFSSL* ssl)
ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
ssl->options.handShakeState = NULL_STATE;
ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
FreeEphemeralKeys(ssl);
/* Reset for the renegotiation_info presence check below. */
ssl->secure_renegotiation->renegInfoSeen = 0;

Expand Down Expand Up @@ -37359,6 +37402,34 @@ static int IsTls13CipherSuite(byte first, byte second)
return 1;
}

/* A full handshake makes a new session. Drop what would resume the
* previous one, so it is not kept beside this handshake's secret. */
int ResetSessionForFullHandshake(WOLFSSL* ssl)
{
WOLFSSL_SESSION* session = ssl->session;

if ((session->sessionIDSz == 0) && !session->haveAltSessionID
#ifdef HAVE_SESSION_TICKET
&& (session->ticketLen == 0)
#endif
) {
return 0;
}
if (!HaveUniqueSessionObj(ssl))
return MEMORY_ERROR;
session = ssl->session;

ForceZero(session->sessionID, ID_LEN);
session->sessionIDSz = 0;
ForceZero(session->altSessionID, ID_LEN);
session->haveAltSessionID = 0;
#ifdef HAVE_SESSION_TICKET
return SetSessionTicket(session, NULL, 0);
#else
return 0;
#endif
}

#ifndef WOLFSSL_NO_TLS12

/* handle generation of client_hello (1) */
Expand Down Expand Up @@ -38334,6 +38405,9 @@ static int IsTls13CipherSuite(byte first, byte second)
ssl->options.resuming = 0; /* server denied resumption try */
}
}
ret = ResetSessionForFullHandshake(ssl);
if (ret != 0)
return ret;
return SetCipherSpecs(ssl);
}

Expand Down Expand Up @@ -45034,6 +45108,11 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
}

XMEMCPY(ssl->clSuites->suites, input + i, ssl->clSuites->suiteSz);
#ifdef OPENSSL_EXTRA
/* Built from the previous hello's list. */
wolfSSL_sk_SSL_CIPHER_free(ssl->clSuitesStack);
ssl->clSuitesStack = NULL;
#endif

#ifdef HAVE_SERVER_RENEGOTIATION_INFO
/* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */
Expand Down
Loading
Loading