Skip to content

Commit 77632a5

Browse files
committed
Require an MLS X.509 credential's end-entity certificate to certify the LeafNode signature key, so a leaf carrying one party's certificate while signed by an unrelated key is no longer accepted under that party's identity through KeyPackage.verify and the Group leaf-validation path.
1 parent 7c7c8c2 commit 77632a5

6 files changed

Lines changed: 176 additions & 2 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h2>2.0 Release History</h2>
2323
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2026, TBD
2424
<h3>2.1.2 Defects Fixed</h3>
2525
<ul>
26+
<li>The MLS implementation did not bind an X.509 credential to the LeafNode's signature_key. LeafNode.verify() checked a leaf's signature against the signature_key declared in the leaf itself, while the X.509 credential's certificate chain was stored but never parsed or checked, so the certificate's public key was never required to match signature_key (RFC 9420 sec. 5.3). A leaf could therefore carry one party's certificate while being signed by an unrelated key and still be accepted under that party's identity through KeyPackage.verify() and the Group leaf-validation path. LeafNode.verify() now requires the end-entity certificate's subject public key, in the cipher suite's signature encoding, to equal signature_key for an X.509 credential, and rejects the leaf otherwise - including an empty chain or a certificate whose key type does not match the cipher suite; certificate-chain and identity validation to a trust anchor remain the application's responsibility per RFC 9420 sec. 5.3.1. A public org.bouncycastle.mls.codec.Certificate(byte[]) constructor and a Credential.getCertificates() accessor are added so callers can build and inspect X.509 credentials. Basic credentials are unaffected.</li>
2627
<li>The SecureRandom supplied to org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder.setSecureRandom() did not drive the content IV / nonce for any algorithm other than RC2. EnvelopedDataHelper.generateParameters passed the caller's SecureRandom to the AlgorithmParameterGenerator only in the RC2_CBC branch; every other content-encryption algorithm - AES-CBC, AES-GCM, AES-CCM, Camellia, ARIA, SEED and the rest - reached pGen.generateParameters() on an uninitialised generator, so the IV / nonce was drawn from a default SecureRandom and setSecureRandom() was silently ignored (the builder's javadoc states that random is used for IV/nonce generation). The generator is now initialised with the supplied random on the general path as well, so a caller who provides a specific randomness source - for a controlled or FIPS-approved DRBG, say - has it honoured for the content IV / nonce. The session-key generation path was unaffected and already used the supplied random.</li>
2728
<li>The bcmail module descriptor did not declare its javax.mail/javax.activation dependences, so a modular (module-path) consumer of the jar hit IllegalAccessError/module-resolution failures when the S/MIME classes touched the mail API. The descriptor now requires them optionally (requires static) under all four module names those libraries are known by - the automatic names mail and activation carried by the javax.mail:mail / javax.activation:activation artifacts, and the explicit names java.mail and java.activation carried by the newer com.sun.mail / com.sun.activation ones - a hard requires on any one name would break users of the others (github #2389).</li>
2829
<li>Four type-coercion helpers in the OER / IEEE 1609.2 (ITS) decoder tested the wrong type in the identity fast path that lets a getInstance() factory return an argument that is already of the target type. org.bouncycastle.oer.its.ieee1609dot2.basetypes.UINT32.getInstance and org.bouncycastle.oer.its.etsi102941.basetypes.Version.getInstance guarded on UINT8 - a sibling of UINT32 under UintBase, and unrelated to Version - so passing a UINT8 threw ClassCastException, while passing an actual UINT32 or Version missed the fast path and fell through to ASN1Integer.getInstance, which rejects them: neither factory accepted its own type. org.bouncycastle.oer.its.etsi103097.EtsiTs103097DataEncryptedUnicast.getInstance guarded on its sibling EtsiTs103097DataEncrypted and then cast to the unicast type, so an EtsiTs103097DataEncrypted threw ClassCastException. org.bouncycastle.oer.OEROptional.getObject(Class) called value.getClass().isInstance(type) with the arguments transposed, which is always false because the argument is a java.lang.Class, so the cast path was dead and every optional field was resolved reflectively, failing with IllegalStateException for a target type with no static getInstance. Each guard now names the type it returns, matching the sibling UINT8 / UINT16 / UINT64 and EtsiTs103097DataEncrypted factories (github #2373).</li>

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import java.util.Arrays;
66
import java.util.List;
77

8+
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
9+
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
10+
import org.bouncycastle.crypto.util.PublicKeyFactory;
811
import org.bouncycastle.mls.codec.Capabilities;
12+
import org.bouncycastle.mls.codec.Certificate;
913
import org.bouncycastle.mls.codec.Credential;
1014
import org.bouncycastle.mls.codec.CredentialType;
1115
import org.bouncycastle.mls.codec.Extension;
@@ -208,14 +212,42 @@ public boolean verifyLifetime()
208212
public boolean verify(MlsCipherSuite suite, byte[] tbs)
209213
throws IOException
210214
{
211-
if (getCredentialType() == CredentialType.x509)
215+
// RFC 9420 sec. 5.3: the leaf's signature_key must be the public key certified by the
216+
// credential's end-entity certificate, otherwise an X.509 credential authenticates nothing -
217+
// a signer could present any identity's certificate while signing with its own key.
218+
if (getCredentialType() == CredentialType.x509 && !x509CredentialBindsSignatureKey(suite))
212219
{
213-
//TODO: get credential and check if it's signature scheme matches the cipher suite signature scheme
220+
return false;
214221
}
215222

216223
return suite.verifyWithLabel(signature_key, "LeafNodeTBS", tbs, signature);
217224
}
218225

226+
private boolean x509CredentialBindsSignatureKey(MlsCipherSuite suite)
227+
{
228+
List<Certificate> certificates = credential.getCertificates();
229+
if (certificates == null || certificates.isEmpty())
230+
{
231+
return false;
232+
}
233+
234+
try
235+
{
236+
// the end-entity certificate is first (RFC 9420 sec. 5.3.3); its subject public key,
237+
// serialised in the cipher suite's signature encoding, must equal signature_key
238+
SubjectPublicKeyInfo spki = org.bouncycastle.asn1.x509.Certificate.getInstance(
239+
certificates.get(0).getCertData()).getSubjectPublicKeyInfo();
240+
AsymmetricKeyParameter certKey = PublicKeyFactory.createKey(spki);
241+
242+
return java.util.Arrays.equals(signature_key, suite.serializeSignaturePublicKey(certKey));
243+
}
244+
catch (Exception e)
245+
{
246+
// a malformed certificate, or a key whose type does not match the cipher suite's signer
247+
return false;
248+
}
249+
}
250+
219251
public LeafNode forCommit(MlsCipherSuite suite, byte[] groupId, LeafIndex leafIndex, byte[] encKeyIn, byte[] parentHash, Group.LeafNodeOptions options, byte[] sigPriv)
220252
throws Exception
221253
{

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ public class Certificate
1313
cert_data = stream.readOpaque();
1414
}
1515

16+
/**
17+
* @param certData the DER encoding of a single X.509 certificate in the credential's chain.
18+
*/
19+
public Certificate(byte[] certData)
20+
{
21+
this.cert_data = certData;
22+
}
23+
24+
public byte[] getCertData()
25+
{
26+
return cert_data;
27+
}
28+
1629
@Override
1730
public void writeTo(MLSOutputStream stream)
1831
throws IOException

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public byte[] getIdentity()
2222
return identity;
2323
}
2424

25+
public List<Certificate> getCertificates()
26+
{
27+
return certificates;
28+
}
29+
2530
public static Credential forBasic(byte[] identity)
2631
{
2732
return new Credential(CredentialType.basic, identity, new ArrayList<Certificate>());

‎mls/src/test/java/org/bouncycastle/mls/test/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static Test suite()
2222
suite.addTestSuite(ExternalCommitRemoveTest.class);
2323
suite.addTestSuite(GroupKeySetTest.class);
2424
suite.addTestSuite(LeafIndexRangeTest.class);
25+
suite.addTestSuite(LeafNodeX509BindingTest.class);
2526
suite.addTestSuite(MessageProtectionTest.class);
2627
suite.addTestSuite(MLSInputStreamTest.class);
2728
suite.addTestSuite(NewMemberMessageNPETest.class);
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.bouncycastle.mls.test;
2+
3+
import java.math.BigInteger;
4+
import java.util.ArrayList;
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
import junit.framework.TestCase;
9+
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
10+
import org.bouncycastle.asn1.x500.X500Name;
11+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
12+
import org.bouncycastle.cert.X509CertificateHolder;
13+
import org.bouncycastle.cert.X509v3CertificateBuilder;
14+
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
15+
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
16+
import org.bouncycastle.mls.TreeKEM.LeafNode;
17+
import org.bouncycastle.mls.TreeKEM.LifeTime;
18+
import org.bouncycastle.mls.codec.Capabilities;
19+
import org.bouncycastle.mls.codec.Certificate;
20+
import org.bouncycastle.mls.codec.Credential;
21+
import org.bouncycastle.mls.codec.CredentialType;
22+
import org.bouncycastle.mls.codec.Extension;
23+
import org.bouncycastle.mls.crypto.MlsCipherSuite;
24+
import org.bouncycastle.operator.ContentSigner;
25+
import org.bouncycastle.operator.bc.BcEdECContentSignerBuilder;
26+
27+
/**
28+
* RFC 9420 sec. 5.3 requires a LeafNode's signature_key to be the public key certified by its X.509
29+
* credential's end-entity certificate. Without that binding the credential authenticates nothing: a
30+
* signer could attach any victim's certificate while signing the leaf with its own key, so
31+
* verification against the self-declared signature_key would succeed and the leaf would impersonate
32+
* the certificate's subject (and, through the resynchronization credential comparison, evict them).
33+
* These tests exercise LeafNode.verify() for an X.509 credential whose certificate key matches the
34+
* signature_key (accepted) and one where it does not (rejected).
35+
*/
36+
public class LeafNodeX509BindingTest
37+
extends TestCase
38+
{
39+
public void testX509LeafWithMatchingCertificateKeyVerifies()
40+
throws Exception
41+
{
42+
MlsCipherSuite suite = suite();
43+
AsymmetricCipherKeyPair signing = suite.generateSignatureKeyPair();
44+
Credential credential = x509Credential("CN=alice@example.com", signing);
45+
46+
LeafNode leaf = leafNode(suite, credential, signing);
47+
48+
assertTrue("a leaf whose certificate certifies its signature_key must verify",
49+
leaf.verify(suite, leaf.toBeSigned(new byte[0], -1)));
50+
}
51+
52+
public void testX509LeafWithForeignCertificateKeyRejected()
53+
throws Exception
54+
{
55+
MlsCipherSuite suite = suite();
56+
57+
// the victim's certificate, over the victim's key
58+
AsymmetricCipherKeyPair victim = suite.generateSignatureKeyPair();
59+
Credential victimCredential = x509Credential("CN=victim@example.com", victim);
60+
61+
// the attacker builds a self-consistent leaf - signed with its own key, declaring its own
62+
// signature_key - but carries the victim's certificate as the credential
63+
AsymmetricCipherKeyPair attacker = suite.generateSignatureKeyPair();
64+
LeafNode forged = leafNode(suite, victimCredential, attacker);
65+
66+
assertFalse("a leaf carrying a certificate that does not certify its signature_key must be rejected",
67+
forged.verify(suite, forged.toBeSigned(new byte[0], -1)));
68+
}
69+
70+
public void testX509LeafWithEmptyChainRejected()
71+
throws Exception
72+
{
73+
MlsCipherSuite suite = suite();
74+
AsymmetricCipherKeyPair signing = suite.generateSignatureKeyPair();
75+
Credential empty = new Credential(CredentialType.x509, null, new ArrayList<Certificate>());
76+
77+
LeafNode leaf = leafNode(suite, empty, signing);
78+
79+
assertFalse("an X.509 credential with no certificate binds no key and must be rejected",
80+
leaf.verify(suite, leaf.toBeSigned(new byte[0], -1)));
81+
}
82+
83+
private static MlsCipherSuite suite()
84+
throws Exception
85+
{
86+
return MlsCipherSuite.getSuite(MlsCipherSuite.MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519);
87+
}
88+
89+
private LeafNode leafNode(MlsCipherSuite suite, Credential credential, AsymmetricCipherKeyPair signing)
90+
throws Exception
91+
{
92+
AsymmetricCipherKeyPair encryption = suite.getHPKE().generatePrivateKey();
93+
94+
return new LeafNode(
95+
suite,
96+
suite.getHPKE().serializePublicKey(encryption.getPublic()),
97+
suite.serializeSignaturePublicKey(signing.getPublic()),
98+
credential,
99+
new Capabilities(),
100+
new LifeTime(),
101+
new ArrayList<Extension>(),
102+
suite.serializeSignaturePrivateKey(signing.getPrivate()));
103+
}
104+
105+
private Credential x509Credential(String dn, AsymmetricCipherKeyPair keyPair)
106+
throws Exception
107+
{
108+
X500Name name = new X500Name(dn);
109+
long now = System.currentTimeMillis();
110+
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(
111+
name, BigInteger.ONE, new Date(now - 60000L), new Date(now + 86400000L), name,
112+
SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(keyPair.getPublic()));
113+
ContentSigner signer = new BcEdECContentSignerBuilder(
114+
new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519)).build(keyPair.getPrivate());
115+
X509CertificateHolder holder = builder.build(signer);
116+
117+
List<Certificate> certificates = new ArrayList<Certificate>();
118+
certificates.add(new Certificate(holder.getEncoded()));
119+
120+
return new Credential(CredentialType.x509, null, certificates);
121+
}
122+
}

0 commit comments

Comments
 (0)