Skip to content

Commit 14d0a87

Browse files
committed
Refactor the sending of server supported groups
1 parent 4fc041f commit 14d0a87

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
319319

320320
tlsServer.getServerExtensionsForConnection(serverEncryptedExtensions);
321321

322+
/*
323+
* RFC 8446 4.2.7. As of TLS 1.3, servers are permitted to send the "supported_groups" extension to
324+
* the client. [..] If the server has a group it prefers to the ones in the "key_share" extension
325+
* but is still willing to accept the ClientHello, it SHOULD send "supported_groups" to update the
326+
* client's view of its preferences; this extension SHOULD contain all groups the server supports,
327+
* regardless of whether they are currently supported by the client.
328+
*/
329+
if (!afterHelloRetryRequest)
330+
{
331+
int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
332+
333+
if (!TlsUtils.isNullOrEmpty(serverSupportedGroups) &&
334+
clientShare.getNamedGroup() != serverSupportedGroups[0] &&
335+
!serverEncryptedExtensions.containsKey(TlsExtensionsUtils.EXT_supported_groups))
336+
{
337+
TlsExtensionsUtils.addSupportedGroupsExtension(serverEncryptedExtensions, serverSupportedGroups);
338+
}
339+
}
340+
322341
ProtocolVersion serverLegacyVersion = ProtocolVersion.TLSv12;
323342
TlsExtensionsUtils.addSupportedVersionsExtensionServer(serverHelloExtensions, serverVersion);
324343

@@ -392,28 +411,6 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
392411
TlsExtensionsUtils.addKeyShareServerHello(serverHelloExtensions, serverShare);
393412

394413
sharedSecret = agreement.calculateSecret();
395-
396-
/*
397-
* RFC 8446 4.2.7. As of TLS 1.3, servers are permitted to send the "supported_groups" extension to
398-
* the client. Clients MUST NOT act upon any information found in "supported_groups" prior to
399-
* successful completion of the handshake but MAY use the information learned from a successfully
400-
* completed handshake to change what groups they use in their "key_share" extension in subsequent
401-
* connections. If the server has a group it prefers to the ones in the "key_share" extension but is
402-
* still willing to accept the ClientHello, it SHOULD send "supported_groups" to update the client's
403-
* view of its preferences; this extension SHOULD contain all groups the server supports, regardless
404-
* of whether they are currently supported by the client.
405-
*/
406-
if (!afterHelloRetryRequest)
407-
{
408-
int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
409-
410-
if (!TlsUtils.isNullOrEmpty(serverSupportedGroups) &&
411-
namedGroup != serverSupportedGroups[0] &&
412-
!serverEncryptedExtensions.containsKey(TlsExtensionsUtils.EXT_supported_groups))
413-
{
414-
TlsExtensionsUtils.addSupportedGroupsExtension(serverEncryptedExtensions, serverSupportedGroups);
415-
}
416-
}
417414
}
418415

419416
TlsUtils.establish13PhaseSecrets(tlsServerContext, pskEarlySecret, sharedSecret);

0 commit comments

Comments
 (0)