Skip to content

Add missing fields to wolfSSL_clear to ensure the WOLFSSL struct is fully reset. Ensure ephemeral keys are always freed between each handshake. - #11964

Open
kareem-wolfssl wants to merge 9 commits into
wolfSSL:masterfrom
kareem-wolfssl:zd22693
Open

kareem-wolfssl wants to merge 9 commits into
wolfSSL:masterfrom
kareem-wolfssl:zd22693

Conversation

@kareem-wolfssl

Copy link
Copy Markdown
Contributor

Description

Thanks to Pu Cheng for the report!
Fixes zd#22693

Testing

Built in + added tests

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

@kareem-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m3

  • FLASH: .text +440 B (+0.3%, 134,126 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4

  • FLASH: .text +512 B (+0.2%, 211,501 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +448 B (+0.2%, 199,204 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +512 B (+0.1%, 808,716 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +448 B (+0.1%, 317,360 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +576 B (+0.2%, 348,008 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +448 B (+0.3%, 134,862 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +448 B (+0.2%, 254,087 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7

  • FLASH: .text +448 B (+0.2%, 211,501 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +448 B (+0.1%, 318,192 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +448 B (+0.2%, 254,087 B / 262,144 B, total: 97% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved findings expose use-after-free during pending async key generation, ECC key reuse in DTLS renegotiation, and build failures or crashes in the new tests.

4 open issues
What changed in this PR

This PR strengthens connection reuse by resetting additional WOLFSSL state and separating ephemeral-key cleanup from other handshake resources.

Changes:

  • Reset protocol limits, cached peer data, pending output, and cipher state in wolfSSL_clear().
  • Centralize ephemeral-key cleanup and invalidate cached client cipher lists on new ClientHello messages.
  • Add regression tests for connection reuse and fresh keys during secure renegotiation.
File Description
wolfssl/​internal.h Declare shared version-check and ephemeral-key cleanup helpers.
tests/​api/​test_tls.h Register the new TLS regression tests.
tests/​api/​test_tls.c Test state reset, interrupted handshakes, and renegotiation key freshness.
src/​tls13.c Invalidate cached client cipher lists on TLS 1.3 ClientHello processing.
src/​ssl.c Expand state reset and resource cleanup in wolfSSL_clear().
src/​internal.c Centralize ephemeral-key cleanup and refresh TLS client cipher caches.

Review effort: Balanced

Comment thread src/internal.c Resolved
Comment thread src/ssl.c Resolved
Comment thread tests/api/test_tls.c Resolved
Comment thread tests/api/test_tls.c Resolved
Comment thread src/ssl.c Resolved
Comment thread src/ssl.c Resolved
Comment thread src/ssl.c Resolved
Comment thread src/internal.c Resolved
Before, the early return from secure renegotiation in FreeHandshakeResources was causing the ephemeral keys to not be freed until wolfSSL_free was called.
…ully reset.

DH params currently omitted due to a known bug.
Thanks to Pu Cheng for the report.
… data.

clSuitesStack is used by wolfSSL_get_client_ciphers, it is intentionally kept
through a wolfSSL_clear for compatibility.  Currently it is not being cleared
when a new set of client ciphers arrive, so it is giving stale data.
Also test that a renegotiation uses new ephemeral keys.
…tion.

Avoid resetting queryable members in wolfSSL_shutdown, only reset them in wolfSSL_clear.
Always enforce protocol version mask and downgrade rules in wolfSSL_clear, not just for OPENSSL_EXTRA.
Fix ECC case in added tests.
Clear PK callback keys in wolfSSL_clear.
@kareem-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread src/ssl.c Resolved
Comment thread src/ssl.c
ssl->buffers.peerEd448Key.length = 0;
#endif
#endif /* HAVE_PK_CALLBACKS */
FreeEphemeralKeys(ssl);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unlinking can’t cancel a hardware request, the async layer has no cancel API, and wolfSSL_free() already frees the same keys the same way. Applications have to let a pending operation finish before clearing or freeing.

Comment thread tests/api/test_tls.c Resolved
Comment thread src/tls13.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #11964

Scan targets checked: wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_tls.h
Coverage: 7 of 7 in-scope changed file(s) opened by the reviewer

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread tests/api/test_tls.c Resolved
Comment thread src/internal.c
Comment on lines +9025 to +9030
/* 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);
}

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.

Comment thread tests/api/test_tls.c Resolved
Comment thread src/internal.c
Comment on lines +38376 to +38378
ret = ResetSessionForFullHandshake(ssl);
if (ret != 0)
return ret;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟣 pre-existing, not blocking: pre-existing: TLS 1.2 clients whose session ticket the server declines still keep the old ticket and session ID beside the new handshake's secret. The new ResetSessionForFullHandshake() runs only in the non-resuming branch of CompleteServerHello. With a ticket, DSH_CheckSessionId() returns true (internal.c:37815), so the full handshake is only detected later at internal.c:22964, and that path never calls the reset. Fix: reset the session wherever a client drops resumption, including the Not resuming as thought path at internal.c:22964, and not only in CompleteServerHello.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

Trigger: a TLS 1.2 client calls wolfSSL_set_session() with a ticket session. The server cannot decrypt the ticket (for example a new ticket key, as in test_tls12_resume_ticket_decline_fallback) and does a full handshake with an empty session ID. In CompleteServerHello, ssl->options.resuming is set and DSH_CheckSessionId() returns 1 because ssl->session->ticketLen > 0 (internal.c:37815). It takes the resumption branch and returns before the new ResetSessionForFullHandshake() call at internal.c:38376. When the server's Certificate arrives, internal.c:22960-22966 only sets resuming = 0 and peerAuthGood = 0. ssl->session->ticket still holds the old ticket next to the new master secret. If the server sends no NewSessionTicket, the client caches that pair. A later connection to a server that can decrypt the old ticket then resumes with the old secret while the client uses the new one, so the handshake fails. The base branch behaves the same, but this PR's stated goal of clearing the ticket on every full handshake misses this path, which is the usual one for a TLS 1.2 ticket decline.

Verification: Pre-existing. Trigger: a TLS 1.2 client resumes with a ticket, and the server declines the ticket, replies with an empty session ID and does a full handshake. The base has the same behavior; this PR's new reset just does not reach this path. The full handshake is detected later, in DoHandShakeMsgType (src/internal.c:22926-22966). It never calls ResetSessionForFullHandshake.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Known issue already fixed by #12064. Not duplicating that work here.

Add AsyncDetachEvent to properly unlink async queue event.
Reset session when using external PSK.
Fix test preprocessor guards.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants