@@ -1255,9 +1255,11 @@ private void initHandshakeInternal(SSLSocket socket, SSLEngine engine)
12551255 * @throws SSLException if setUseClientMode() has not been called or
12561256 * on native socket error
12571257 * @throws SocketTimeoutException if socket timed out
1258+ *
1259+ * @throws WolfSSLException if it fails to check the DH key size after the handshake.
12581260 */
12591261 protected synchronized int doHandshake (int isSSLEngine , int timeout )
1260- throws SSLException , SocketTimeoutException {
1262+ throws SSLException , SocketTimeoutException , WolfSSLException {
12611263
12621264 int ret , err ;
12631265 byte [] serverId = null ;
@@ -1343,10 +1345,13 @@ else if (peerAddr != null) {
13431345 /* may throw SocketTimeoutException on socket timeout */
13441346 ret = this .ssl .connect (timeout );
13451347
1348+ checkKeySize (ssl , this .clientMode );
13461349 } else {
13471350 WolfSSLDebug .log (getClass (), WolfSSLDebug .INFO ,
13481351 "calling native wolfSSL_accept()" );
13491352 ret = this .ssl .accept (timeout );
1353+
1354+ checkKeySize (ssl , this .clientMode );
13501355 }
13511356 err = ssl .getError (ret );
13521357
@@ -1369,6 +1374,51 @@ else if (peerAddr != null) {
13691374 return ret ;
13701375 }
13711376
1377+ private void checkKeySize (WolfSSLSession ssl , boolean clientMode ) throws SSLException , WolfSSLException {
1378+ int keySize = this .ssl .getKeySize ();
1379+
1380+ // Before we update the cached values, and return from the handshake, we
1381+ // check if we are running a legacy cipher suite, if so, we make sure
1382+ // that the actual key size is at least 1024 bits.
1383+ String [] cipherSuites = getCiphers ();
1384+
1385+ if (containsDHECiphers (cipherSuites )) {
1386+ // Get the minimum DH key size from security settings
1387+ int minDHEKeySize ;
1388+ try {
1389+ minDHEKeySize = WolfSSLUtil .getDisabledAlgorithmsKeySizeLimit ("DH" );
1390+
1391+ // If we're trying to use DHE with insufficient key size, throw early
1392+ if (isLegacyDHEnabled () && keySize < minDHEKeySize ) {
1393+ if (clientMode ) {
1394+ throw new SSLHandshakeException (
1395+ "DH ServerKeyExchange does not comply to algorithm constraints" );
1396+ } else {
1397+ throw new SSLHandshakeException (
1398+ "Received fatal alert: insufficient_security" );
1399+ }
1400+ }
1401+ } catch (WolfSSLException e ) {
1402+ throw new WolfSSLException ("Failed to check DH key size constraints: " , e );
1403+ }
1404+ }
1405+ }
1406+
1407+ private boolean containsDHECiphers (String [] cipherSuites ) {
1408+ for (String suite : cipherSuites ) {
1409+ if (suite .contains ("_DHE_" )) {
1410+ return true ;
1411+ }
1412+ }
1413+ return false ;
1414+ }
1415+
1416+ private boolean isLegacyDHEnabled () {
1417+ // Check if legacy DH is enabled through system properties
1418+ String dhKeySize = System .getProperty ("jdk.tls.ephemeralDHKeySize" );
1419+ return "legacy" .equals (dhKeySize );
1420+ }
1421+
13721422 /**
13731423 * Unset the native verify callback and reset internal verify
13741424 * callback state.
0 commit comments