wolfSSL — Heap Out-of-Bounds Write in TLS 1.2 Server DHE Key Agreement
On heap-math builds a TLS 1.2 server computes the DHE pre-master secret into a 512-byte buffer (578 with PSK) without checking it against its configured DH prime. A server that uses a prime above 4096 bits writes past the buffer whenever a client completes a DHE key exchange, before the client is authenticated.
- Advisory
- BYTERAY-2026-0294
- 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), TLS 1.2 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
A TLS 1.2 server that uses ephemeral Diffie-Hellman computes the shared secret when it receives the ClientKeyExchange message. wolfSSL writes that secret into ssl->arrays->preMasterSecret, a heap buffer of ENCRYPT_LEN bytes. On builds that use the heap big-integer library ENCRYPT_LEN is 512 bytes, or 578 bytes when PSK support is compiled.
The secret is as long as the server's DH prime. The server accepts any prime up to its DH size limit, and that limit can be raised to 16000 bits with a public API that knows nothing about the buffer. With a 6144-bit prime the server writes 768 bytes into the 512-byte buffer.
Any client that negotiates a DHE cipher suite triggers the write by sending its public value. Default builds are not affected. They do not use heap math, and a 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 exchangediffie_hellman_kea(the DHE cipher suites that are not PSK-based). - Required build: heap math (
--enable-heapmath,USE_INTEGER_HEAP_MATH) with DH and TLS 1.2. - Required configuration: 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 compiles the function but is not affected, because heap math is off by default (configure.ac:1336). - Earlier releases were not checked.
The DHE-PSK arm of the same function is covered in BYTERAY-2026-0295.
Technical Details
For heap math the buffer size comes from a constant, ENCRYPT_BASE_BITS 4096 (wolfssl/internal.h:1380-1382). ENCRYPT_LEN is that value in bytes, plus MAX_PSK_KEY_LEN + 2 when PSK is compiled (wolfssl/internal.h:1563-1567).
In the build phase of DoClientKeyExchange() the server loads its own parameters and records the buffer size:
ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
ssl->buffers.serverDH_P.buffer,
ssl->buffers.serverDH_P.length,
ssl->buffers.serverDH_G.buffer,
ssl->buffers.serverDH_G.length);
/* set the max agree result size */
ssl->arrays->preMasterSz = ENCRYPT_LEN;
The comment states the intent, but the value is never enforced. The agreement call follows:
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,
&ssl->arrays->preMasterSz,
ssl->buffers.serverDH_P.buffer,
ssl->buffers.serverDH_P.length);
From there:
src/internal.c:45965—DhAgree()receives the fixed buffer and the server's prime.src/internal.c:7386— after validating the client's public value,DhAgree()callswc_DhAgree()without comparing the buffer size 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.
Nothing ties the prime to the buffer. The setters for server DH parameters check the prime against minDhKeySz and maxDhKeySz only (src/ssl_load.c:5848-5849, src/ssl_load.c:6029). The RFC 7919 named-group selection uses the same limit (src/tls.c:5508-5509). wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() accept any multiple of 8 up to 16000 bits (src/ssl_api_pk.c:1896, src/ssl_api_pk.c:1921).
On heap math the default limit is 4096 bits (wolfssl/internal.h:1289), which equals the buffer. The overflow therefore needs the limit raised, by the application or by WOLFSSL_MAX_DHKEY_BITS at build time. A 6144-bit prime then overruns a 512-byte buffer by up to 256 bytes, and an 8192-bit prime by up to 512 bytes.
Reachability and Preconditions
- Build: heap math. SP math and fastmath derive the buffer size from the big-integer size and were not found affected.
- Server configuration: DH parameters with a prime above 4096 bits, loaded through
wolfSSL_CTX_SetTmpDH()or a sibling, or selected as an RFC 7919 group, 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
DHEcipher suite in TLS 1.2 or earlier. - Authentication: none. The ClientKeyExchange is processed before the client's Finished message and, where a client certificate is requested, before the CertificateVerify message that proves possession of its key.
- 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 handshake that reaches the ClientKeyExchange.
ByteRay reproduced it with a stock OpenSSL client against a wolfSSL server that was configured with the FFDHE-6144 prime and a raised DH size limit, on a heap-math build without PSK. AddressSanitizer reported a heap-buffer-overflow write that started 0 bytes past the 512-byte buffer, in mp_to_unsigned_bin called from wc_DhAgree_Sync, DhAgree and DoClientKeyExchange. The same server with a 2048-bit prime 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 makes wolfSSL_CTX_SetMaxDhKey_Sz() and wolfSSL_SetMaxDhKey_Sz() reject a limit above that maximum. A server can then 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 buffer 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 cipher suites on the server also removes the path.
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 agreement call at the tested commit
- wc_DhAgree_Sync, the unbounded write
- wolfSSL_CTX_SetMaxDhKey_Sz 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-0295, BYTERAY-2026-0296, BYTERAY-2026-0297, BYTERAY-2026-0313