Skip to content

Commit aa31efa

Browse files
committed
Switch TLS 1.3 server to use psk_ke instead of psk_dhe_ke
Update TlsUtils to advertise and select psk_ke. Disable DHE key agreement logic in TlsServerProtocol.
1 parent bfdbedb commit aa31efa

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

tls/src/main/java/org/bouncycastle/tls/TlsServerProtocol.java

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -291,29 +291,27 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
291291
TlsUtils.negotiatedCipherSuite(securityParameters, cipherSuite);
292292
}
293293

294-
int[] clientSupportedGroups = securityParameters.getClientSupportedGroups();
295-
int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
296-
boolean useServerOrder = tlsServer.preferLocalSupportedGroups();
297-
298-
int selectedGroup = TlsUtils.selectKeyShareGroup(crypto, serverVersion, clientSupportedGroups,
299-
serverSupportedGroups, useServerOrder);
300-
if (selectedGroup < 0)
301-
{
302-
throw new TlsFatalAlert(AlertDescription.handshake_failure);
303-
}
304-
305-
securityParameters.negotiatedGroup = selectedGroup;
306-
307-
clientShare = TlsUtils.findEarlyKeyShare(clientShares, selectedGroup);
308-
309-
if (null == clientShare)
310-
{
311-
this.retryGroup = selectedGroup;
312-
313-
this.retryCookie = tlsServerContext.getNonceGenerator().generateNonce(16);
314-
315-
return generate13HelloRetryRequest(clientHello);
316-
}
294+
// int[] clientSupportedGroups = securityParameters.getClientSupportedGroups();
295+
// int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
296+
// boolean useServerOrder = tlsServer.preferLocalSupportedGroups();
297+
//
298+
// int selectedGroup = TlsUtils.selectKeyShareGroup(crypto, serverVersion, clientSupportedGroups,
299+
// serverSupportedGroups, useServerOrder);
300+
// if (selectedGroup < 0)
301+
// {
302+
// throw new TlsFatalAlert(AlertDescription.handshake_failure);
303+
// }
304+
//
305+
// clientShare = TlsUtils.findEarlyKeyShare(clientShares, selectedGroup);
306+
//
307+
// if (null == clientShare)
308+
// {
309+
// this.retryGroup = selectedGroup;
310+
//
311+
// this.retryCookie = tlsServerContext.getNonceGenerator().generateNonce(16);
312+
//
313+
// return generate13HelloRetryRequest(clientHello);
314+
// }
317315
}
318316

319317

@@ -329,17 +327,17 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
329327
* client's view of its preferences; this extension SHOULD contain all groups the server supports,
330328
* regardless of whether they are currently supported by the client.
331329
*/
332-
if (!afterHelloRetryRequest)
333-
{
334-
int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
335-
336-
if (!TlsUtils.isNullOrEmpty(serverSupportedGroups) &&
337-
serverSupportedGroups[0] != securityParameters.getNegotiatedGroup() &&
338-
!serverEncryptedExtensions.containsKey(TlsExtensionsUtils.EXT_supported_groups))
339-
{
340-
TlsExtensionsUtils.addSupportedGroupsExtension(serverEncryptedExtensions, serverSupportedGroups);
341-
}
342-
}
330+
// if (!afterHelloRetryRequest)
331+
// {
332+
// int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
333+
//
334+
// if (!TlsUtils.isNullOrEmpty(serverSupportedGroups) &&
335+
// serverSupportedGroups[0] != securityParameters.getNegotiatedGroup() &&
336+
// !serverEncryptedExtensions.containsKey(TlsExtensionsUtils.EXT_supported_groups))
337+
// {
338+
// TlsExtensionsUtils.addSupportedGroupsExtension(serverEncryptedExtensions, serverSupportedGroups);
339+
// }
340+
// }
343341

344342
ProtocolVersion serverLegacyVersion = ProtocolVersion.TLSv12;
345343
TlsExtensionsUtils.addSupportedVersionsExtensionServer(serverHelloExtensions, serverVersion);
@@ -397,29 +395,29 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
397395
TlsExtensionsUtils.addPreSharedKeyServerHello(serverHelloExtensions, selectedPSK.index);
398396
}
399397

400-
TlsSecret sharedSecret;
401-
{
402-
int negotiatedGroup = securityParameters.getNegotiatedGroup();
403-
404-
if (clientShare.getNamedGroup() != negotiatedGroup)
405-
{
406-
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
407-
}
408-
409-
TlsAgreement agreement = TlsUtils.createKeyShare(crypto, negotiatedGroup, true);
410-
if (agreement == null)
411-
{
412-
throw new TlsFatalAlert(AlertDescription.internal_error);
413-
}
414-
415-
agreement.receivePeerValue(clientShare.getKeyExchange());
416-
417-
byte[] key_exchange = agreement.generateEphemeral();
418-
KeyShareEntry serverShare = new KeyShareEntry(negotiatedGroup, key_exchange);
419-
TlsExtensionsUtils.addKeyShareServerHello(serverHelloExtensions, serverShare);
420-
421-
sharedSecret = agreement.calculateSecret();
422-
}
398+
TlsSecret sharedSecret = null;
399+
// {
400+
// int negotiatedGroup = securityParameters.getNegotiatedGroup();
401+
//
402+
// if (clientShare.getNamedGroup() != negotiatedGroup)
403+
// {
404+
// throw new TlsFatalAlert(AlertDescription.illegal_parameter);
405+
// }
406+
//
407+
// TlsAgreement agreement = TlsUtils.createKeyShare(crypto, negotiatedGroup, true);
408+
// if (agreement == null)
409+
// {
410+
// throw new TlsFatalAlert(AlertDescription.internal_error);
411+
// }
412+
//
413+
// agreement.receivePeerValue(clientShare.getKeyExchange());
414+
//
415+
// byte[] key_exchange = agreement.generateEphemeral();
416+
// KeyShareEntry serverShare = new KeyShareEntry(negotiatedGroup, key_exchange);
417+
// TlsExtensionsUtils.addKeyShareServerHello(serverHelloExtensions, serverShare);
418+
//
419+
// sharedSecret = agreement.calculateSecret();
420+
// }
423421

424422
TlsUtils.establish13PhaseSecrets(tlsServerContext, pskEarlySecret, sharedSecret);
425423

tls/src/main/java/org/bouncycastle/tls/TlsUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6145,13 +6145,13 @@ static OfferedPsks.SelectedConfig selectPreSharedKey(TlsServerContext serverCont
61456145
}
61466146

61476147
// TODO[tls13] Fetch these from 'server'
6148-
short[] serverSupportedModes = { PskKeyExchangeMode.psk_dhe_ke };
6148+
short[] serverSupportedModes = { PskKeyExchangeMode.psk_ke };
61496149
boolean useServerOrder = false;
61506150

61516151
short selectedMode = selectPreSharedKeyMode(pskKeyExchangeModes, serverSupportedModes, useServerOrder);
61526152

61536153
// TODO[tls13] Add support for psk_ke?
6154-
if (PskKeyExchangeMode.psk_dhe_ke == selectedMode)
6154+
if (PskKeyExchangeMode.psk_ke == selectedMode)
61556155
{
61566156
// TODO[tls13] Prefer to get the exact index from the server?
61576157
TlsPSKExternal psk = server.getExternalPSK(offeredPsks.getIdentities());

0 commit comments

Comments
 (0)