Skip to content

Commit 87f6317

Browse files
committed
TLS: negotiate group before looking for early share
1 parent 146c93a commit 87f6317

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
import org.bouncycastle.tls.crypto.TlsAgreement;
1212
import org.bouncycastle.tls.crypto.TlsCrypto;
13-
import org.bouncycastle.tls.crypto.TlsDHConfig;
14-
import org.bouncycastle.tls.crypto.TlsECConfig;
15-
import org.bouncycastle.tls.crypto.TlsKemConfig;
1613
import org.bouncycastle.tls.crypto.TlsSecret;
1714
import org.bouncycastle.util.Arrays;
1815

@@ -219,7 +216,7 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
219216
}
220217
this.retryCookie = null;
221218

222-
clientShare = TlsUtils.selectKeyShare(clientShares, retryGroup);
219+
clientShare = TlsUtils.getRetryKeyShare(clientShares, retryGroup);
223220
if (null == clientShare)
224221
{
225222
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
@@ -297,17 +294,18 @@ protected ServerHello generate13ServerHello(ClientHello clientHello, HandshakeMe
297294
int[] clientSupportedGroups = securityParameters.getClientSupportedGroups();
298295
int[] serverSupportedGroups = securityParameters.getServerSupportedGroups();
299296

300-
clientShare = TlsUtils.selectKeyShare(crypto, serverVersion, clientShares, clientSupportedGroups,
297+
int selectedGroup = TlsUtils.selectKeyShareGroup(crypto, serverVersion, clientSupportedGroups,
301298
serverSupportedGroups);
299+
if (selectedGroup < 0)
300+
{
301+
throw new TlsFatalAlert(AlertDescription.handshake_failure);
302+
}
303+
304+
clientShare = TlsUtils.findEarlyKeyShare(clientShares, selectedGroup);
302305

303306
if (null == clientShare)
304307
{
305-
this.retryGroup = TlsUtils.selectKeyShareGroup(crypto, serverVersion, clientSupportedGroups,
306-
serverSupportedGroups);
307-
if (retryGroup < 0)
308-
{
309-
throw new TlsFatalAlert(AlertDescription.handshake_failure);
310-
}
308+
this.retryGroup = selectedGroup;
311309

312310
this.retryCookie = tlsServerContext.getNonceGenerator().generateNonce(16);
313311

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5449,37 +5449,30 @@ else if (NamedGroup.refersToASpecificKem(keyShareGroup))
54495449
return null;
54505450
}
54515451

5452-
static KeyShareEntry selectKeyShare(Vector clientShares, int keyShareGroup)
5452+
static KeyShareEntry findEarlyKeyShare(Vector clientShares, int keyShareGroup)
54535453
{
5454-
if (null != clientShares && 1 == clientShares.size())
5454+
if (null != clientShares)
54555455
{
5456-
KeyShareEntry clientShare = (KeyShareEntry)clientShares.elementAt(0);
5457-
if (null != clientShare && clientShare.getNamedGroup() == keyShareGroup)
5456+
for (int i = 0; i < clientShares.size(); ++i)
54585457
{
5459-
return clientShare;
5458+
KeyShareEntry clientShare = (KeyShareEntry)clientShares.elementAt(i);
5459+
if (null != clientShare && clientShare.getNamedGroup() == keyShareGroup)
5460+
{
5461+
return clientShare;
5462+
}
54605463
}
54615464
}
54625465
return null;
54635466
}
54645467

5465-
static KeyShareEntry selectKeyShare(TlsCrypto crypto, ProtocolVersion negotiatedVersion, Vector clientShares,
5466-
int[] clientSupportedGroups, int[] serverSupportedGroups)
5468+
static KeyShareEntry getRetryKeyShare(Vector clientShares, int keyShareGroup)
54675469
{
5468-
if (null != clientShares && !isNullOrEmpty(clientSupportedGroups) && !isNullOrEmpty(serverSupportedGroups))
5470+
if (null != clientShares && 1 == clientShares.size())
54695471
{
5470-
for (int i = 0; i < clientShares.size(); ++i)
5472+
KeyShareEntry clientShare = (KeyShareEntry)clientShares.elementAt(0);
5473+
if (null != clientShare && clientShare.getNamedGroup() == keyShareGroup)
54715474
{
5472-
KeyShareEntry clientShare = (KeyShareEntry)clientShares.elementAt(i);
5473-
5474-
int group = clientShare.getNamedGroup();
5475-
5476-
if (NamedGroup.canBeNegotiated(group, negotiatedVersion) &&
5477-
Arrays.contains(serverSupportedGroups, group) &&
5478-
Arrays.contains(clientSupportedGroups, group) &&
5479-
supportsKeyShareGroup(crypto, group))
5480-
{
5481-
return clientShare;
5482-
}
5475+
return clientShare;
54835476
}
54845477
}
54855478
return null;

0 commit comments

Comments
 (0)