Skip to content

Change logic of choosing early key shares #2052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions tls/src/main/java/org/bouncycastle/tls/AbstractTlsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,73 @@ public Vector getEarlyKeyShareGroups()
{
return null;
}

Integer firstKemNamedGroup = null;
int firstKemNamedGroupIndex = -1;

for (int i = 0; i < supportedGroups.size(); i++)
{
Integer group = (Integer) supportedGroups.elementAt(i);
if (NamedGroup.refersToASpecificKem(group))
{
firstKemNamedGroup = group;
firstKemNamedGroupIndex = i;
break;
}
}

Integer firstNonKemNamedGroup = null;
int firstNonKemNamedGroupIndex = -1;

if (supportedGroups.contains(Integers.valueOf(NamedGroup.x25519)))
{
return TlsUtils.vectorOfOne(Integers.valueOf(NamedGroup.x25519));
firstNonKemNamedGroup = Integers.valueOf(NamedGroup.x25519);
firstNonKemNamedGroupIndex = supportedGroups.indexOf(firstNonKemNamedGroup);
}
if (supportedGroups.contains(Integers.valueOf(NamedGroup.secp256r1)))
else if (supportedGroups.contains(Integers.valueOf(NamedGroup.secp256r1)))
{
return TlsUtils.vectorOfOne(Integers.valueOf(NamedGroup.secp256r1));
firstNonKemNamedGroup = Integers.valueOf(NamedGroup.secp256r1);
firstNonKemNamedGroupIndex = supportedGroups.indexOf(firstNonKemNamedGroup);
}
else
{
for (int i = 0; i < supportedGroups.size(); i++)
{
Integer group = (Integer) supportedGroups.elementAt(i);
if (!NamedGroup.refersToASpecificKem(group))
{
firstNonKemNamedGroup = group;
firstNonKemNamedGroupIndex = i;
break;
}
}
}
return TlsUtils.vectorOfOne(supportedGroups.elementAt(0));

Vector<Integer> earlyKeyShareGroups = new Vector<>();

if (firstKemNamedGroupIndex != -1 && firstNonKemNamedGroupIndex != -1)
{
if (firstKemNamedGroupIndex < firstNonKemNamedGroupIndex)
{
earlyKeyShareGroups.add(firstKemNamedGroup);
earlyKeyShareGroups.add(firstNonKemNamedGroup);
}
else
{
earlyKeyShareGroups.add(firstNonKemNamedGroup);
earlyKeyShareGroups.add(firstKemNamedGroup);
}
}
else if (firstKemNamedGroup != null)
{
earlyKeyShareGroups.add(firstKemNamedGroup);
}
else if (firstNonKemNamedGroup != null)
{
earlyKeyShareGroups.add(firstNonKemNamedGroup);
}

return TlsUtils.vectorFromArray(earlyKeyShareGroups.toArray());
}

public boolean shouldUseCompatibilityMode()
Expand Down
10 changes: 10 additions & 0 deletions tls/src/main/java/org/bouncycastle/tls/TlsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,16 @@ public static Vector vectorOfOne(Object obj)
return v;
}

public static Vector vectorFromArray(Object[] array)
{
Vector v = new Vector(array.length);
for (Object obj: array)
{
v.addElement(obj);
}
return v;
}

public static int getCipherType(int cipherSuite)
{
int encryptionAlgorithm = getEncryptionAlgorithm(cipherSuite);
Expand Down