Repository navigation
Add missing fields to wolfSSL_clear to ensure the WOLFSSL struct is fully reset. Ensure ephemeral keys are always freed between each handshake. - #11964
Conversation
|
@claude review |
|
There was a problem hiding this comment.
🟡 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
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.
4beaae9 to
20243e2
Compare
|
@claude review |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved async cleanup defects risk use-after-free and queue races, an unguarded test helper breaks supported builds, and external-PSK handling can leave invalid resumption state.
4 open issues
4 resolved since last review
Review effort: Balanced
| ssl->buffers.peerEd448Key.length = 0; | ||
| #endif | ||
| #endif /* HAVE_PK_CALLBACKS */ | ||
| FreeEphemeralKeys(ssl); |
There was a problem hiding this comment.
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.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
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
| /* 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); | ||
| } |
There was a problem hiding this comment.
🔴 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.
| ret = ResetSessionForFullHandshake(ssl); | ||
| if (ret != 0) | ||
| return ret; |
There was a problem hiding this comment.
🟣 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.
There was a problem hiding this comment.
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.


Description
Thanks to Pu Cheng for the report!
Fixes zd#22693
Testing
Built in + added tests
Checklist