← ALL ADVISORIES

CRITICAL BYTERAY-2026-0296Patched

wolfSSL — Heap Out-of-Bounds Write in TLS 1.2 Client DHE-PSK Key Agreement

A TLS 1.2 DHE-PSK client writes the DH shared secret at offset 2 of a 578-byte buffer (heap-math builds) without checking it against the prime from the unsigned ServerKeyExchange. With wolfSSL's own key generation an oversized prime fails earlier with an error. The write is reachable when a PK callback generates the client's DH key pair.

Advisory
BYTERAY-2026-0296
CWE
CWE-787
CVSS
9.8 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)
Vendor
wolfSSL
Product
wolfSSL
Reported
2026-09-27
Disclosure
2026-12-03
Affected
wolfSSL 5.9.2 through 5.9.4, heap-math builds (--enable-heapmath) with PSK and PK callbacks, TLS 1.2 DHE-PSK clients that register a DH key-generation callback
Fixed in
Master commit 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345, merged 2026-10-09). Not in a wolfSSL stable release as of 2026-12-03.
Credit
Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.

Executive Summary

A TLS 1.2 client that uses a DHE-PSK cipher suite takes the Diffie-Hellman prime from the ServerKeyExchange message. It then computes the shared secret into ssl->arrays->preMasterSecret, two bytes in. The buffer has ENCRYPT_LEN bytes, 578 on builds that use the heap big-integer library with PSK. The agreement call does not compare the prime with the space left in the buffer, so a prime above 4096 bits leads to a write past its end.

On most builds this write cannot be reached. Before the agreement the client generates its own DH key pair, and wolfSSL's key generation refuses a prime that is larger than the buffer for the public value. That buffer is always smaller than any prime that would overflow the pre-master buffer. The handshake then fails with a key size error and no memory is corrupted.

The write is reachable when key generation is delegated: the library is built with PK callbacks and the application registers a DH key-generation callback that succeeds for the large prime. In that configuration a server, or a man in the middle, can cause a heap out-of-bounds write of about 192 bytes with a 6144-bit prime. Default builds are not affected.

Affected Products and Versions

  • Product: wolfSSL, TLS 1.2 client, SendClientKeyExchange() in src/internal.c, key exchange dhe_psk_kea.
  • Required build: heap math (--enable-heapmath), PSK (--enable-psk) and PK callbacks (--enable-pkcallbacks, HAVE_PK_CALLBACKS).
  • Required configuration: a DHE-PSK cipher suite, a DH size limit raised above 4096 bits, a registered DH key-generation callback that accepts the oversized prime, and no DH agreement callback.
  • Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
  • The v5.9.2-stable and v5.9.4-stable tags contain the same agreement call, and the same key-generation guard.
  • Fixed on master by 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345, merged 2026-10-09). No release contains the fix as of 2026-12-03.
  • A default ./configure build is not affected. Heap math, PSK and PK callbacks are all off by default.
  • Earlier releases were not checked.

Technical Details

For heap math ENCRYPT_BASE_BITS is the constant 4096 (wolfssl/internal.h:1380-1382). With PSK, ENCRYPT_LEN is 512 + 64 + 2 = 578 bytes (wolfssl/internal.h:1563-1564).

The client accepts the server's prime in GetDhPublicKey() after two length checks, against minDhKeySz and maxDhKeySz (src/internal.c:36056-36065). PSK key exchanges carry no signature over these parameters. The DO phase of SendClientKeyExchange() then runs:

                    ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
                        ssl->buffers.sig.buffer, ssl->buffers.sig.length,
                        ssl->buffers.serverDH_Pub.buffer,
                        ssl->buffers.serverDH_Pub.length,
                        ssl->arrays->preMasterSecret + OPAQUE16_LEN,
                        &ssl->arrays->preMasterSz,
                        ssl->buffers.serverDH_P.buffer,
                        ssl->buffers.serverDH_P.length);

DhAgree() calls wc_DhAgree() without a capacity check (src/internal.c:7386), and wc_DhAgree_Sync() writes the prime-sized result with mp_to_unsigned_bin(z, agree) before it stores the length (wolfcrypt/src/dh.c:2347-2351). A 768-byte result at offset 2 ends 192 bytes past a 578-byte buffer.

The guard is in the BUILD phase of the same case, which runs first:

  1. src/internal.c:37491 — the buffer for the outgoing message, encSecret, is MAX_ENCRYPT_SZ bytes, the same 578.
  2. src/internal.c:37642 — the space for the client's DH public value is that size less the PSK identity and two length fields, so at most 574 bytes.
  3. src/internal.c:37650 — DhGenKeyPair() generates the key pair with that capacity.
  4. wolfcrypt/src/dh.c:1394 — wolfCrypt's key generation rejects the request:
    if (*pubSz < (word32)mp_unsigned_bin_size(&key->p)) {
        return WC_KEY_SIZE_E;
    }

The pre-master write overflows only when the prime is longer than 576 bytes. Every such prime is longer than the 574-byte capacity, so the software key generation always fails first.

DhGenKeyPair() uses an application callback instead of wolfCrypt when one is registered (src/internal.c:7308-7313). If that callback returns success for the oversized prime, the handshake continues into the unchecked agreement.

Reachability and Preconditions

  • Build: heap math, PSK and PK callbacks. SP math and fastmath derive the buffer size from the big-integer size and were not found affected.
  • The client application registered a DH key-generation callback with wolfSSL_CTX_SetDhGenerateKeyPair(), and that callback succeeds for a prime larger than the public-value buffer it is given. It did not register a DH agreement callback, so DhAgree() uses the wolfCrypt path.
  • The client's DH size limit is above 4096 bits. The heap-math default is 4096 (wolfssl/internal.h:1289); it is raised with wolfSSL_CTX_SetMaxDhKey_Sz() or WOLFSSL_MAX_DHKEY_BITS.
  • The client negotiates a DHE-PSK cipher suite in TLS 1.2 or earlier.
  • Attacker position: the server, or a man in the middle who replaces the ServerKeyExchange. The message is not signed, and the write happens before the Finished messages would expose the change. The attacker does not need the pre-shared key.
  • Without the callback condition the same input produces a handshake error and nothing else.

Impact

Where the preconditions hold, a heap out-of-bounds write in the client during the handshake. Where they do not, a failed handshake.

ByteRay reproduced the write on a heap-math build with PSK and PK callbacks, in a client with a DH key-generation callback registered. The client received an unsigned ServerKeyExchange that carried the FFDHE-6144 prime. AddressSanitizer reported a heap-buffer-overflow write that started 0 bytes past the 578-byte buffer, in mp_to_unsigned_bin called from wc_DhAgree_Sync, DhAgree and SendClientKeyExchange. Handshakes with 2048 and 4096-bit primes and a 32-byte PSK completed with no report. ByteRay did not reproduce the write on a build without the callback. That such builds stop at key generation is from source review, and it matches what ByteRay observed for the sibling DHE path in BYTERAY-2026-0313.

Confirmed: memory corruption in the callback configuration. Not verified: code execution. The bytes written are the DH shared secret, which the attacker can compute and vary but not choose freely.

The published rating assumes the write is reachable by any network attacker. Integrators should weigh it against the preconditions above.

Remediation and Mitigation

Use a master build that contains 044bbfc0fa1636e45c11be8c2babb27674841189 (PR 11345). The commit sizes the pre-master buffer from the compiled key exchanges, including the compiled maximum DH size and the PSK framing, and stops wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() from raising the limit above that maximum. A prime that the client accepts then always fits.

PR 11930 (commit c411cc71b7e6c8b8050ff98c6ffdb6c91632cc6e, merged 2026-11-03) adds a check in the TLS-layer DhAgree(), which now returns BUFFER_E when the output space is smaller than the prime.

Both changes are on master only. No release contains them as of 2026-12-03.

Until an update is possible, leave the client's DH size limit at 4096 bits or below on heap-math builds. Applications that supply a DH key-generation callback should make it fail when the public-value buffer is smaller than the prime.

Timeline

  • 2026-09-27 — Reported to wolfSSL.
  • 2026-10-09 — PR 11345 merged to master. It had been opened on 2026-09-01, before this report.
  • 2026-10-29 — PR 11930 opened.
  • 2026-11-03 — PR 11930 merged to master.
  • 2026-12-03 — Public disclosure.

Credits

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

References