Skip to content

Commit dcdbca5

Browse files
committed
TLS: 1.3 server may send supported_groups
1 parent 87f6317 commit dcdbca5

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ public static void addSupportedGroupsExtension(Hashtable extensions, Vector name
250250
extensions.put(EXT_supported_groups, createSupportedGroupsExtension(namedGroups));
251251
}
252252

253+
public static void addSupportedGroupsExtension(Hashtable extensions, int[] namedGroups) throws IOException
254+
{
255+
extensions.put(EXT_supported_groups, createSupportedGroupsExtension(namedGroups));
256+
}
257+
253258
public static void addSupportedPointFormatsExtension(Hashtable extensions, short[] ecPointFormats)
254259
throws IOException
255260
{
@@ -934,6 +939,16 @@ public static byte[] createSupportedGroupsExtension(Vector namedGroups) throws I
934939
return TlsUtils.encodeUint16ArrayWithUint16Length(values);
935940
}
936941

942+
public static byte[] createSupportedGroupsExtension(int[] namedGroups) throws IOException
943+
{
944+
if (TlsUtils.isNullOrEmpty(namedGroups))
945+
{
946+
throw new TlsFatalAlert(AlertDescription.internal_error);
947+
}
948+
949+
return TlsUtils.encodeUint16ArrayWithUint16Length(namedGroups);
950+
}
951+
937952
public static byte[] createSupportedPointFormatsExtension(short[] ecPointFormats) throws IOException
938953
{
939954
if (ecPointFormats == null || !Arrays.contains(ecPointFormats, ECPointFormat.uncompressed))

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,22 +311,6 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
311311

312312
return generate13HelloRetryRequest(clientHello);
313313
}
314-
315-
if (clientShare.getNamedGroup() != serverSupportedGroups[0])
316-
{
317-
/*
318-
* TODO[tls13] RFC 8446 4.2.7. As of TLS 1.3, servers are permitted to send the
319-
* "supported_groups" extension to the client. Clients MUST NOT act upon any
320-
* information found in "supported_groups" prior to successful completion of the
321-
* handshake but MAY use the information learned from a successfully completed
322-
* handshake to change what groups they use in their "key_share" extension in
323-
* subsequent connections. If the server has a group it prefers to the ones in the
324-
* "key_share" extension but is still willing to accept the ClientHello, it SHOULD
325-
* send "supported_groups" to update the client's view of its preferences; this
326-
* extension SHOULD contain all groups the server supports, regardless of whether
327-
* they are currently supported by the client.
328-
*/
329-
}
330314
}
331315

332316

@@ -408,6 +392,28 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
408392
TlsExtensionsUtils.addKeyShareServerHello(serverHelloExtensions, serverShare);
409393

410394
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+
}
411417
}
412418

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

0 commit comments

Comments
 (0)