← ALL ADVISORIES

LOW BYTERAY-2026-0520Disclosed

wolfSSL — Incomplete Reset of Key-Exchange State in wolfSSL_clear

wolfSSL_clear() does not release the handshake key slot and the related key-exchange buffers. If a handshake is abandoned while suspended and the object is recycled, the next handshake fails in AllocKey with BAD_STATE_E, and the previous connection's ephemeral key material stays allocated and unwiped until then.

Advisory
BYTERAY-2026-0520
CWE
CWE-665
CVSS
3.7 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:L)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-28
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 (tested at master commit d95483f), applications that reuse WOLFSSL objects with wolfSSL_clear() after a handshake was abandoned at a WANT_WRITE or asynchronous-crypto return
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

wolfSSL_clear() resets a WOLFSSL object so that it can carry another connection. It does not release the key-exchange state that FreeKeyExchange() owns: the handshake key slot ssl->hsKey, the signature and digest buffers, and the server DH key.

Usually those slots are already empty, because every handler that fills them frees them on its way out. The exception is a handshake that is suspended at a WANT_WRITE or asynchronous-crypto return. If the application gives up on that handshake and calls wolfSSL_clear(), the slots stay occupied. The next handshake on the object reaches AllocKey(), which refuses a non-NULL destination and returns BAD_STATE_E.

One handshake fails. The failing handler's exit path frees the stale slot, so the object works again afterwards. Until then, the previous connection's ephemeral key material remains allocated and is not zeroized. There is no key reuse and no memory corruption.

Affected Products and Versions

  • Component: src/ssl.c, wolfSSL_clear(). The failure surfaces in AllocKey() in src/internal.c.
  • Required conditions: the application recycles WOLFSSL objects with wolfSSL_clear(), and a handshake can stop at a WANT_WRITE return (non-blocking I/O) or a WC_PENDING_E return (WOLFSSL_ASYNC_CRYPT).
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The WANT_WRITE return is not behind a build option. Applications that free the object and allocate a new one for each connection are not affected.
  • Other releases were not checked.

Technical Details

FreeKeyExchange() (src/internal.c:9621-9654) frees ssl->hsKey, ssl->buffers.sig.buffer, ssl->buffers.digest.buffer and ssl->buffers.serverDH_Key, and zeroizes the signature buffer (src/internal.c:9627). Final teardown calls it (src/internal.c:9914). wolfSSL_clear() (src/ssl.c:5774-6089) resets options, keys, secrets, pending messages, handshake hashes and peer certificates, and does not refer to any of these four slots.

The next handshake allocates into the slot with AllocKey(ssl, ssl->hsType, &ssl->hsKey) (src/internal.c:37278). AllocKey() checks the destination first (src/internal.c:9242-9251):

    if (*pKey != NULL) {
        WOLFSSL_MSG("Key already present!");
        ...
        return BAD_STATE_E;
    }

The elided lines return 0 for WC_PENDING_E re-entry in asynchronous builds. The sequence:

  1. A handshake on the object allocates ssl->hsKey and then returns early with WANT_WRITE or WC_PENDING_E. Those returns skip the handler's cleanup (src/internal.c:38791-38796, src/internal.c:40940-40943).
  2. The application abandons the connection, for example on its own timeout, and calls wolfSSL_clear(). The slot is not released.
  3. The next handshake reaches key generation. AllocKey() finds the slot occupied and returns BAD_STATE_E. The handshake fails.
  4. The failing handler runs its exit block, which calls FreeKeyExchange() (src/internal.c:38214). The slot is now empty and later handshakes on the object succeed.

Handlers that run to completion clean up on both success and error: SendClientKeyExchange() (src/internal.c:38214), SendCertificateVerify() (src/internal.c:38818), SendServerKeyExchange() (src/internal.c:40961) and the TLS 1.3 handlers (src/tls13.c:12308, src/tls13.c:13441). Alerts, disconnects and errors inside a handler therefore do not leave the slot occupied.

Reachability and Preconditions

  • The application reuses a WOLFSSL object with wolfSSL_clear().
  • A handshake on that object was suspended at a WANT_WRITE or WC_PENDING_E return with the key slot allocated, and was abandoned in that state.
  • A remote peer can cause the suspension by stalling the connection, for example by not reading, until the application times out. No authentication is involved. Each abandoned and recycled handshake costs one later connection.

Impact

The connection that next uses the object fails with BAD_STATE_E. The failure is deterministic and the object recovers after it. While the slot is stale, the previous connection's hsKey private material and signature buffer remain in memory without being zeroized, until that cleanup or the final free of the object. The guard in AllocKey() prevents the stale key from being used.

ByteRay did not build a proof of concept for this issue. The analysis is from source review.

Remediation and Mitigation

ByteRay has not confirmed a fixed wolfSSL release for this issue as of 2026-12-03, and has not checked it against wolfSSL changes made after the tested commit.

The fix direction is for wolfSSL_clear() to release the same slots that teardown releases, with the logic of FreeKeyExchange(). AllocKey() deliberately tolerates an occupied slot during asynchronous re-entry, so the reset must run only when no handshake is in progress. Applications can avoid the condition by freeing the object with wolfSSL_free() and allocating a new one after an abandoned handshake, instead of calling wolfSSL_clear().

Timeline

  • 2026-09-28 — Reported to wolfSSL.
  • 2026-12-03 — Public disclosure.

Credits

Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.

References