wolfSSL — Heap Out-of-Bounds Write in TLS 1.2 Server DHE-PSK Key Agreement
On heap-math builds with PSK, a TLS 1.2 server writes the DHE-PSK shared secret at offset 2 of a 578-byte buffer without checking it against its configured DH prime. With a prime above 4096 bits the write passes the end of the buffer on any DHE-PSK ClientKeyExchange, before the pre-shared key is looked up.
- Advisory
- BYTERAY-2026-0295
- 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, TLS 1.2 DHE-PSK servers configured with a DH prime larger than 4096 bits
- 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
In a DHE-PSK key exchange the pre-master secret is built from the Diffie-Hellman shared secret and the pre-shared key. A wolfSSL server computes the DH part when it receives the ClientKeyExchange and writes it into ssl->arrays->preMasterSecret, two bytes in, to leave room for a length field. The buffer has ENCRYPT_LEN bytes. On builds that use the heap big-integer library with PSK that is 578 bytes.
The DH secret is as long as the server's prime. The server does not compare the prime with the space left in the buffer. With a 6144-bit prime it writes 768 bytes at offset 2, which ends 192 bytes past the buffer.
The server looks up the pre-shared key only after this step, so a client needs no valid key or identity to trigger the write. Default builds are not affected. They do not use heap math or PSK, and the server must also have been configured with a prime above 4096 bits.
Affected Products and Versions
- Product: wolfSSL, TLS 1.2 server,
DoClientKeyExchange()insrc/internal.c, key exchangedhe_psk_kea. - Required build: heap math (
--enable-heapmath,USE_INTEGER_HEAP_MATH) and PSK (--enable-psk), with DH and TLS 1.2. - Required configuration: a DHE-PSK cipher suite, and server DH parameters with a prime above 4096 bits, which also needs the DH size limit raised above its default.
- Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same code. - Fixed on master by
044bbfc0fa1636e45c11be8c2babb27674841189(PR 11345, merged 2026-10-09). No release contains the fix as of 2026-12-03. - A default
./configurebuild is not affected. Heap math and PSK are both off by default (configure.ac:1336,configure.ac:6166). - Earlier releases were not checked.
The certificate-authenticated DHE arm of the same function is covered in BYTERAY-2026-0294.
Technical Details
For heap math ENCRYPT_BASE_BITS is the constant 4096 (wolfssl/internal.h:1380-1382). With PSK compiled the buffer size is:
#ifndef NO_PSK
ENCRYPT_LEN = (ENCRYPT_BASE_BITS / 8) + MAX_PSK_KEY_LEN + 2,
#else
ENCRYPT_LEN = (ENCRYPT_BASE_BITS / 8),
#endif
That is 512 + 64 + 2 = 578 bytes. The allowance covers a 4096-bit DH secret and a PSK. It does not cover a larger prime.
In the build phase the dhe_psk_kea case reads the client's PSK identity and DH public value, then loads the server's own DH parameters (src/internal.c:45809-45813). The agreement call is:
ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
ssl->buffers.serverDH_Priv.buffer,
ssl->buffers.serverDH_Priv.length,
input + args->idx,
(word16)args->sigSz,
ssl->arrays->preMasterSecret + OPAQUE16_LEN,
&ssl->arrays->preMasterSz,
ssl->buffers.serverDH_P.buffer,
ssl->buffers.serverDH_P.length);
From there:
src/internal.c:45980—DhAgree()receives the buffer at offset 2 and the server's prime.src/internal.c:7386— after validating the client's public value,DhAgree()callswc_DhAgree()without comparing the space available to the prime.wolfcrypt/src/dh.c:2348—wc_DhAgree_Sync()writes the result withmp_to_unsigned_bin(z, agree)and stores the length in*agreeSzafterwards.
The server's prime is checked against minDhKeySz and maxDhKeySz when it is installed (src/ssl_load.c:5848-5849, src/ssl_load.c:6029) and nowhere against the buffer. wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() accept up to 16000 bits (src/ssl_api_pk.c:1896, src/ssl_api_pk.c:1921). The default limit on heap math is 4096 bits (wolfssl/internal.h:1289), so the overflow needs the limit raised, by the application or by WOLFSSL_MAX_DHKEY_BITS at build time.
A 6144-bit prime overruns the buffer by up to 192 bytes and an 8192-bit prime by up to 448 bytes.
The pre-shared key is fetched later, in AddPSKtoPreMasterSecret() (src/internal.c:39236-39257), called from the finalize step at src/internal.c:46169.
Reachability and Preconditions
- Build: heap math and PSK. SP math and fastmath derive the buffer size from the big-integer size and were not found affected.
- Server configuration: a DHE-PSK cipher suite and DH parameters with a prime above 4096 bits, after the DH size limit was raised. This is the operator's choice. The attacker does not supply the prime.
- Attacker position: any TLS client that can reach the server and negotiate a DHE-PSK suite in TLS 1.2 or earlier.
- Authentication: none. The agreement runs before the server calls its PSK callback, so an unknown identity is enough.
- The client's public value must pass the range check in
DhAgree(). An ordinary DH public value does. - No user interaction.
Impact
A heap out-of-bounds write in the server, on every DHE-PSK handshake that reaches the ClientKeyExchange.
ByteRay reproduced it with a stock OpenSSL 3.0 client against a wolfSSL server that was configured with the FFDHE-6144 prime and a raised DH size limit, on a heap-math build with PSK. 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 DoClientKeyExchange. The same server with a 4096-bit prime and a 32-byte PSK completed the handshake with no report.
Confirmed: memory corruption and a crash, which any client can repeat. Not verified: code execution. The bytes written are the DH shared secret. The client knows them and can vary them through its public value, but it does not choose them freely.
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 passes the remaining space to the DHE-PSK agreement. It also makes wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() reject a limit above the compiled maximum, so a server can no longer be configured with a prime larger than the buffer.
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, keep server DH primes at 4096 bits or below on heap-math builds and do not raise the DH size limit above 4096 bits, or build with SP math. Disabling DHE-PSK suites on the server also removes the path. Plain PSK and ECDHE-PSK do not use this code.
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
- DoClientKeyExchange, the DHE-PSK agreement call at the tested commit
- wc_DhAgree_Sync, the unbounded write
- ENCRYPT_LEN at the tested commit
- PR 11345, pre-master buffer sizing
- Fix commit 044bbfc0fa
- PR 11930, bound on the DH agreement output
- Commit c411cc71b7
- Related advisories with the same root cause: BYTERAY-2026-0293, BYTERAY-2026-0294, BYTERAY-2026-0296, BYTERAY-2026-0297, BYTERAY-2026-0313