wolfSSL — Skipped DH Prime Validation on Reused TLS 1.2 Client Objects
A wolfSSL TLS 1.2 client tests the server's Diffie-Hellman prime for primality, but the two flags that control the test are set once per object and never re-armed. After one handshake, a client object reused through wolfSSL_clear() accepts any later server prime, including a composite modulus, without the test.
- Advisory
- BYTERAY-2026-0306
- CWE
- CWE-1271
- CVSS
- 7.5 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-27
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 through 5.9.4, TLS 1.2 clients that negotiate DHE or DHE-PSK and reuse a WOLFSSL object for more than one handshake
- Fixed in
- Master commit 5aa793e52e901e412f1ececb8b2029349ece5e8f (PR 11606, merged 2026-10-19). Not in a wolfSSL stable release as of 2026-12-03.
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
When a TLS 1.2 client uses a DHE cipher suite, the server chooses the Diffie-Hellman prime. wolfSSL checks that the value really is prime before it uses it, unless it is one of the known RFC 7919 groups. A composite modulus with small factors makes the key exchange easy to break for anyone who knows the factors.
The check is controlled by two one-bit flags in the connection object. One records that a prime has been tested. The other is cleared when the server's parameters match a known group. Both are set when the object is created and nothing re-arms them. wolfSSL_clear(), the documented way to reuse an object for a new connection, leaves them as the first handshake left them.
After one handshake, every later handshake on the same object accepts the server's prime with length checks only. The client then completes a key exchange over a modulus that a fresh object would have rejected. Default builds contain this code. An application is exposed only if it reuses client objects and connects to a server whose DH parameters are bad.
Affected Products and Versions
- Product: wolfSSL, TLS 1.2 client,
DhSetKey()andGetDhPublicKey()insrc/internal.c. - Required build: DH and TLS 1.2 with the current prime check, that is, without
WOLFSSL_OLD_PRIME_CHECK,HAVE_FIPSorHAVE_SELFTEST. Builds withWOLFSSL_REQUIRE_FFDHE(--enable-ffdhe-only) reject every non-RFC 7919 prime and are not affected. - Required usage: the application runs a second handshake on the same
WOLFSSLobject, normally afterwolfSSL_clear(). - 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
5aa793e52e901e412f1ececb8b2029349ece5e8f(PR 11606, merged 2026-10-19). No release contains the fix as of 2026-12-03. - A default
./configurebuild includes the code. - Earlier releases were not checked.
The same flags also survive a renegotiation. That path is covered in BYTERAY-2026-0343.
Technical Details
DhSetKey() loads the server's parameters into the client's DH key:
if (ssl->options.dhDoKeyTest &&
!ssl->options.dhKeyTested)
{
ret = wc_DhSetCheckKey(ssl->buffers.serverDH_Key,
ssl->buffers.serverDH_P.buffer,
ssl->buffers.serverDH_P.length,
ssl->buffers.serverDH_G.buffer,
ssl->buffers.serverDH_G.length,
NULL, 0, 0, ssl->rng);
if (ret != 0)
return ret;
ssl->options.dhKeyTested = 1;
The else branch calls wc_DhSetKey() (src/internal.c:34667). That function marks the parameters as trusted (wolfcrypt/src/dh.c:2821-2826), and the primality test is skipped for trusted parameters (wolfcrypt/src/dh.c:2690).
The flags change as follows:
src/internal.c:8771—InitSSL()setsdhDoKeyTestto 1 when the object is created.dhKeyTestedis copied from the context (src/internal.c:8064).src/internal.c:36251—GetDhPublicKey()setsdhDoKeyTestto 0 when the ServerKeyExchange parameters match a compiled FFDHE group.src/internal.c:34662—DhSetKey()setsdhKeyTestedto 1 after a prime passes the test.
Either event disables the test for the rest of the object's life. wolfSSL_clear() (src/ssl.c:5774) resets the handshake state and the negotiated group (src/ssl.c:5864) but neither flag. wolfSSL_connect() calls ReinitSSL() for a reused object (src/ssl_api_hs.c:413), and that does not touch them either.
In the second handshake GetDhPublicKey() stores the new prime after comparing its length with minDhKeySz and maxDhKeySz (src/internal.c:36056-36065). The client then calls DhSetKey() from SendClientKeyExchange(), at src/internal.c:37567 for DHE and at src/internal.c:37648 for DHE-PSK, and takes the untested branch.
Reachability and Preconditions
- The client application reuses a
WOLFSSLobject:wolfSSL_clear()followed bywolfSSL_connect(). In builds withOPENSSL_EXTRAa successfulwolfSSL_shutdown()callswolfSSL_clear()itself (src/ssl_api_rw.c:1203-1210). - An earlier handshake on that object reached the ServerKeyExchange of a DHE suite, with parameters that either matched an RFC 7919 group or passed the test.
- A later handshake on the object negotiates DHE or DHE-PSK in TLS 1.2 or earlier, with a server that sends a prime that is not a real prime.
- Who can supply the prime: in certificate-authenticated DHE the ServerKeyExchange is signed, and the client verifies the signature before it uses the parameters. The prime therefore comes from the authenticated server, or from whoever controls that server's DH parameters. A man in the middle without the server's key cannot substitute it. In DHE-PSK the message is not signed, but a party without the pre-shared key cannot complete the handshake.
- A fresh object for each connection is not affected. TLS 1.3 is not affected; it uses named groups only.
Impact
The client loses a check that protects it from a server with weak or backdoored Diffie-Hellman parameters. If such a server sends a composite modulus with a known factorisation, a party that knows the factors can recover the session keys from a passive capture. The server itself already sees the plaintext, so the exposure is to third parties.
ByteRay reproduced the behaviour on a default build. A fresh client object rejected a 2048-bit composite modulus with small factors, with DH_CHECK_PUB_E (-243). After a first handshake that used an RFC 7919 group, followed by wolfSSL_clear() and wolfSSL_connect(), the same object accepted that modulus, completed the handshake and exchanged application data. The result was the same after a first handshake with a valid custom prime. Reusing the object with a valid prime worked as expected. The sanitizers reported nothing; this is not a memory-safety issue.
Confirmed: the primality test is skipped and the handshake completes. Not demonstrated: recovery of a session key from such a handshake. That step is well understood for a modulus with small factors, but ByteRay did not carry it out.
The published rating treats the supplier of the prime as an unauthenticated network attacker. For certificate-authenticated suites the source shows it must be the server. Integrators should weigh the rating against that and against the reuse precondition.
Remediation and Mitigation
Use a master build that contains 5aa793e52e901e412f1ececb8b2029349ece5e8f (PR 11606). GetDhPublicKey() now clears dhKeyTested every time it installs parameters from a ServerKeyExchange. When the parameters match a known FFDHE group it marks them as tested, and it no longer clears dhDoKeyTest. Each new prime is tested unless it is a known group. The pull request adds a regression test for the reused-object case.
The fix is on master only. No release contains it as of 2026-12-03.
Until an update is possible:
- Create a new
WOLFSSLobject for each connection where DHE may be negotiated. - Or build with
--enable-ffdhe-only, so that the client accepts RFC 7919 groups only. Servers with custom primes will then fail. - Or disable DHE cipher suites on the client and use ECDHE.
Timeline
- 2026-09-27 — Reported to wolfSSL.
- 2026-09-30 — PR 11606 opened. Its description covers the DH prime check on reused objects and renegotiation.
- 2026-10-19 — PR 11606 merged to master.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.
wolfSSL credited ByteRay Ltd. for the reports in PR 11606.
References
- DhSetKey at the tested commit
- GetDhPublicKey, where a known group disables the test
- wolfSSL_clear at the tested commit
- PR 11606
- Fix commit 5aa793e52e
- Related advisory, the renegotiation path: BYTERAY-2026-0343