|
| 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