← ALL ADVISORIES

HIGH BYTERAY-2026-0313Patched

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

A TLS 1.2 DHE client writes the DH shared secret into a 512-byte buffer (heap-math builds) without checking it against the prime from the 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-0313
CWE
CWE-787
CVSS
8.1 (CVSS:3.1/AV:N/AC:H/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 PK callbacks, TLS 1.2 DHE 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 cipher suite takes the Diffie-Hellman prime from the server's signed ServerKeyExchange message. It computes the shared secret into ssl->arrays->preMasterSecret, a heap buffer of ENCRYPT_LEN bytes. On builds that use the heap big-integer library that is 512 bytes, or 578 with PSK compiled. The agreement call does not compare the prime with 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 into a second buffer of the same size, and wolfSSL's key generation refuses a prime that is larger than that buffer. The handshake 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 can cause a heap out-of-bounds write of about 256 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 diffie_hellman_kea.
  • Required build: heap math (--enable-heapmath) and PK callbacks (--enable-pkcallbacks, HAVE_PK_CALLBACKS).
  • Required configuration: a DHE 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 and PK callbacks are both off by default.
  • Earlier releases were not checked.

The DHE-PSK arm of the same function is covered in BYTERAY-2026-0296.

Technical Details

For heap math ENCRYPT_BASE_BITS is the constant 4096 (wolfssl/internal.h:1380-1382), and ENCRYPT_LEN is that value in bytes, plus 66 when PSK is compiled (wolfssl/internal.h:1563-1567).

The client accepts the server's prime in GetDhPublicKey() after two length checks, against minDhKeySz and maxDhKeySz:

    if (length > ssl->options.maxDhKeySz) {
        WOLFSSL_MSG("Server using a DH key that is too big");
        SendAlert(ssl, alert_fatal, handshake_failure);
        ERROR_OUT(DH_KEY_SIZE_E, exit_gdpk);
    }

The limit is a policy value. wolfSSL_CTX_SetMaxDhKey_Sz() accepts up to 16000 bits (src/ssl_api_pk.c:1896) and is not related to the buffer. 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,
                        &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).

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

  1. src/internal.c:37491 — the buffer for the client's public value, encSecret, is MAX_ENCRYPT_SZ bytes. MAX_ENCRYPT_SZ is ENCRYPT_LEN (wolfssl/internal.h:2158).
  2. src/internal.c:37569 — DhGenKeyPair() generates the key pair with that capacity.
  3. wolfcrypt/src/dh.c:1394 — wolfCrypt's key generation returns WC_KEY_SIZE_E when the capacity is smaller than the prime.

The two buffers have the same size. Every prime that would overflow the pre-master buffer is too large for the public-value buffer, 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 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 cipher suite in TLS 1.2 or earlier.
  • Attacker position: a server the client connects to. The ServerKeyExchange is signed and the client verifies the signature before it builds its key exchange, so the attacker needs a certificate the client accepts. A man in the middle without one cannot substitute the prime.
  • 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 PK callbacks, in a client with a DH key-generation callback registered. The server sent a correctly signed ServerKeyExchange that carried the FFDHE-6144 prime. AddressSanitizer reported a heap-buffer-overflow write that started at the first byte past the 512-byte buffer, in mp_to_unsigned_bin called from wc_DhAgree_Sync, DhAgree and SendClientKeyExchange. On the same source built without PK callbacks, the same server flight ended the handshake with WC_KEY_SIZE_E (-234) and no memory error. A 2048-bit prime completed the client's key exchange with no report.

Confirmed: memory corruption in the callback configuration, and a clean error without it. Not verified: code execution. The bytes written are the DH shared secret, which the server can compute and vary but not choose 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 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 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, 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