Skip to content

Commit e138599

Browse files
authored
Merge pull request #243 from gasbytes/LegacyDHEKeyExchange-patch
Added check for legacy DHE keys (for cipher suites using keys less than 1024 bits)
2 parents 9db7ff1 + 967f75e commit e138599

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/java/com/wolfssl/provider/jsse/WolfSSLEngine.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,13 @@ else if (!this.needInit && !this.handshakeFinished) {
15521552
try {
15531553
WolfSSLDebug.log(getClass(), WolfSSLDebug.INFO,
15541554
"calling engineHelper.doHandshake()");
1555-
int ret = this.engineHelper.doHandshake(1, 0);
1555+
1556+
int ret;
1557+
try {
1558+
ret = this.engineHelper.doHandshake(1, 0);
1559+
} catch (WolfSSLException e) {
1560+
throw new SSLException("Handshake failed: " + e.getMessage(), e);
1561+
}
15561562
SetHandshakeStatus(ret);
15571563

15581564
/* Mark that the user has explicitly started the handshake

src/java/com/wolfssl/provider/jsse/WolfSSLEngineHelper.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.

src/java/com/wolfssl/provider/jsse/WolfSSLServerSocket.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ synchronized public void setEnabledProtocols(String[] protocols)
240240

241241
/* sanitize protocol array for unsupported strings */
242242
List<String> supported;
243+
243244
supported = Arrays.asList(
244245
WolfSSLUtil.sanitizeProtocols(WolfSSL.getProtocols()));
245246

src/java/com/wolfssl/provider/jsse/WolfSSLSocket.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ public synchronized void startHandshake() throws IOException {
15721572
err + ", TID " + Thread.currentThread().getId() + ")");
15731573
close();
15741574
throw e;
1575+
} catch (WolfSSLException e) {
1576+
/* close socket if the handshake is unsuccessful */
1577+
close();
1578+
throw new SSLException("Handshake failed: " + e.getMessage(), e);
15751579
}
15761580

15771581
if (ret != WolfSSL.SSL_SUCCESS) {

0 commit comments

Comments
 (0)