Skip to content

Commit f6b6c2f

Browse files
committed
Decode the MLS codec enums through a public fromValue lookup keyed on each constant's wire value instead of values() ordinal indexing, leaving the GREASE ordinal-offset branches as the sole EnumOrdinal exceptions.
1 parent 829ee0a commit f6b6c2f

27 files changed

Lines changed: 192 additions & 30 deletions

‎mls/src/main/java/org/bouncycastle/mls/TreeKEM/LeafNode.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public LeafNode(MLSInputStream stream)
105105
signature_key = stream.readOpaque();
106106
credential = (Credential)stream.read(Credential.class);
107107
capabilities = (Capabilities)stream.read(Capabilities.class);
108-
leaf_node_source = LeafNodeSource.values()[(byte)stream.read(byte.class)];
108+
leaf_node_source = LeafNodeSource.fromValue((byte)stream.read(byte.class));
109109
switch (leaf_node_source)
110110
{
111111
case KEY_PACKAGE:

‎mls/src/main/java/org/bouncycastle/mls/TreeKEM/LeafNodeSource.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@ public void writeTo(MLSOutputStream stream)
2626
{
2727
stream.write(value);
2828
}
29+
30+
public static LeafNodeSource fromValue(byte value)
31+
{
32+
for (LeafNodeSource element : values())
33+
{
34+
if (element.value == value)
35+
{
36+
return element;
37+
}
38+
}
39+
40+
throw new IllegalArgumentException("unknown LeafNodeSource value: " + value);
41+
}
2942
}

‎mls/src/main/java/org/bouncycastle/mls/TreeKEM/Node.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Node(ParentNode parentNode)
5353
public Node(MLSInputStream stream)
5454
throws IOException
5555
{
56-
this.nodeType = NodeType.values()[(byte)stream.read(byte.class)];
56+
this.nodeType = NodeType.fromValue((byte)stream.read(byte.class));
5757
switch (nodeType)
5858
{
5959
case leaf:

‎mls/src/main/java/org/bouncycastle/mls/TreeKEM/TreeHashInput.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static TreeHashInput forParentNode(ParentNodeHashInput parentNode)
3434
public TreeHashInput(MLSInputStream stream)
3535
throws IOException
3636
{
37-
nodeType = NodeType.values()[(byte)stream.read(byte.class)];
37+
nodeType = NodeType.fromValue((byte)stream.read(byte.class));
3838
switch (nodeType)
3939
{
4040
case leaf:

‎mls/src/main/java/org/bouncycastle/mls/codec/AuthenticatedContent.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public boolean verify(MlsCipherSuite suite, byte[] sigPub, byte[] context)
9494
public AuthenticatedContent(MLSInputStream stream)
9595
throws IOException
9696
{
97-
this.wireFormat = WireFormat.values()[(short)stream.read(short.class)];
97+
this.wireFormat = WireFormat.fromValue((short)stream.read(short.class));
9898
content = (FramedContent)stream.read(FramedContent.class);
9999
auth = new FramedContentAuthData(stream, content.contentType);
100100
}
@@ -127,7 +127,7 @@ public ConfirmedTranscriptHashInput(WireFormat wireFormat, FramedContent content
127127
public ConfirmedTranscriptHashInput(MLSInputStream stream)
128128
throws IOException
129129
{
130-
this.wireFormat = WireFormat.values()[(short)stream.read(short.class)];
130+
this.wireFormat = WireFormat.fromValue((short)stream.read(short.class));
131131
content = (FramedContent)stream.read(FramedContent.class);
132132
signature = stream.readOpaque();
133133
}

‎mls/src/main/java/org/bouncycastle/mls/codec/ContentType.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ public void writeTo(MLSOutputStream stream)
3030
{
3131
stream.write(value);
3232
}
33+
34+
public static ContentType fromValue(byte value)
35+
{
36+
for (ContentType element : values())
37+
{
38+
if (element.value == value)
39+
{
40+
return element;
41+
}
42+
}
43+
44+
throw new IllegalArgumentException("unknown ContentType value: " + value);
45+
}
3346
}

‎mls/src/main/java/org/bouncycastle/mls/codec/Credential.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public Credential(CredentialType credentialType, byte[] identity, List<Certifica
3939
this.certificates = certificates;
4040
}
4141

42-
@SuppressWarnings("unused")
42+
// GREASE values decode by ordinal offset past the named constants (see Grease).
43+
@SuppressWarnings({"unused", "EnumOrdinal"})
4344
Credential(MLSInputStream stream)
4445
throws IOException
4546
{
4647
short credType = (short)stream.read(short.class);
4748
if (Grease.isGrease(credType) == -1)
4849
{
49-
this.credentialType = CredentialType.values()[credType];
50+
this.credentialType = CredentialType.fromValue(credType);
5051
}
5152
else
5253
{

‎mls/src/main/java/org/bouncycastle/mls/codec/CredentialType.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ public void writeTo(MLSOutputStream stream)
4444
{
4545
stream.write(value);
4646
}
47+
48+
public static CredentialType fromValue(short value)
49+
{
50+
for (CredentialType element : values())
51+
{
52+
if (element.value == value)
53+
{
54+
return element;
55+
}
56+
}
57+
58+
throw new IllegalArgumentException("unknown CredentialType value: " + value);
59+
}
4760
}

‎mls/src/main/java/org/bouncycastle/mls/codec/Extension.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public Extension(ExtensionType extensionType, byte[] extension_data)
1818
this.extension_data = extension_data;
1919
}
2020

21+
// GREASE values decode by ordinal offset past the named constants (see Grease).
22+
@SuppressWarnings("EnumOrdinal")
2123
public Extension(int extensionType, byte[] extension_data)
2224
{
2325
short extType = (short)extensionType;
2426
if (Grease.isGrease(extType) == -1)
2527
{
26-
this.extensionType = ExtensionType.values()[extType];
28+
this.extensionType = ExtensionType.fromValue(extType);
2729
}
2830
else
2931
{
@@ -52,14 +54,15 @@ public byte[] getExternalPub()
5254
return null;
5355
}
5456

55-
@SuppressWarnings("unused")
57+
// GREASE values decode by ordinal offset past the named constants (see Grease).
58+
@SuppressWarnings({"unused", "EnumOrdinal"})
5659
Extension(MLSInputStream stream)
5760
throws IOException
5861
{
5962
short extType = (short)stream.read(short.class);
6063
if (Grease.isGrease(extType) == -1)
6164
{
62-
this.extensionType = ExtensionType.values()[extType];
65+
this.extensionType = ExtensionType.fromValue(extType);
6366
}
6467
else
6568
{

‎mls/src/main/java/org/bouncycastle/mls/codec/ExtensionType.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ public short getValue()
5959
{
6060
return value;
6161
}
62+
63+
public static ExtensionType fromValue(short value)
64+
{
65+
for (ExtensionType element : values())
66+
{
67+
if (element.value == value)
68+
{
69+
return element;
70+
}
71+
}
72+
73+
throw new IllegalArgumentException("unknown ExtensionType value: " + value);
74+
}
6275
}

0 commit comments

Comments
 (0)