@@ -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,55 @@ 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+ /*
1381+ * Before we update the cached values, and return from the handshake,
1382+ * we check if we are running a legacy cipher suite, if so, we make sure
1383+ * that the actual key size is at least 1024 bits.
1384+ */
1385+ String [] cipherSuites = getCiphers ();
1386+
1387+ if (containsDHECiphers (cipherSuites )) {
1388+ /* Get the minimum DH key size from security settings. */
1389+ int minDHEKeySize ;
1390+ try {
1391+ minDHEKeySize = WolfSSLUtil .getDisabledAlgorithmsKeySizeLimit ("DH" );
1392+
1393+ /*
1394+ * If we're trying to use DHE with
1395+ * insufficient key size, throw early. */
1396+ if (isLegacyDHEnabled () && keySize < minDHEKeySize ) {
1397+ if (clientMode ) {
1398+ throw new SSLHandshakeException (
1399+ "DH ServerKeyExchange does not comply to algorithm constraints" );
1400+ } else {
1401+ throw new SSLHandshakeException (
1402+ "Received fatal alert: insufficient_security" );
1403+ }
1404+ }
1405+ } catch (WolfSSLException e ) {
1406+ throw new WolfSSLException ("Failed to check DH key size constraints: " , e );
1407+ }
1408+ }
1409+ }
1410+
1411+ private boolean containsDHECiphers (String [] cipherSuites ) {
1412+ for (String suite : cipherSuites ) {
1413+ if (suite .contains ("_DHE_" )) {
1414+ return true ;
1415+ }
1416+ }
1417+ return false ;
1418+ }
1419+
1420+ private boolean isLegacyDHEnabled () {
1421+ /* Check if legacy DH is enabled through system properties. */
1422+ String dhKeySize = System .getProperty ("jdk.tls.ephemeralDHKeySize" );
1423+ return "legacy" .equals (dhKeySize );
1424+ }
1425+
13721426 /**
13731427 * Unset the native verify callback and reset internal verify
13741428 * callback state.
0 commit comments