|
| 1 | +package org.bouncycastle.cms.test; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.security.KeyPair; |
| 5 | +import java.security.KeyPairGenerator; |
| 6 | +import java.security.PrivateKey; |
| 7 | +import java.security.PublicKey; |
| 8 | +import java.security.Security; |
| 9 | +import java.security.cert.CertPath; |
| 10 | +import java.security.cert.CertPathValidator; |
| 11 | +import java.security.cert.CertPathValidatorException; |
| 12 | +import java.security.cert.CertificateFactory; |
| 13 | +import java.security.cert.PKIXParameters; |
| 14 | +import java.security.cert.TrustAnchor; |
| 15 | +import java.security.cert.X509Certificate; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import junit.framework.TestCase; |
| 24 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 25 | +import org.bouncycastle.asn1.iana.IANAObjectIdentifiers; |
| 26 | +import org.bouncycastle.asn1.x500.X500Name; |
| 27 | +import org.bouncycastle.asn1.x509.BasicConstraints; |
| 28 | +import org.bouncycastle.asn1.x509.Extension; |
| 29 | +import org.bouncycastle.asn1.x509.KeyUsage; |
| 30 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 31 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 32 | +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; |
| 33 | +import org.bouncycastle.cms.CMSProcessableByteArray; |
| 34 | +import org.bouncycastle.cms.CMSSignedData; |
| 35 | +import org.bouncycastle.cms.CMSSignedDataGenerator; |
| 36 | +import org.bouncycastle.cms.SignerInformation; |
| 37 | +import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder; |
| 38 | +import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder; |
| 39 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 40 | +import org.bouncycastle.operator.ContentSigner; |
| 41 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
| 42 | +import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
| 43 | +import org.bouncycastle.util.CollectionStore; |
| 44 | + |
| 45 | +/** |
| 46 | + * CMS SignedData round-trip and PKIX CertPath validation tests for the eighteen Composite ML-DSA |
| 47 | + * parameter sets (draft-ietf-lamps-pq-composite-sigs). Plain JCA sign/verify for these sets is |
| 48 | + * already covered by CompositeSignaturesTest, and CompositeKEMEnvelopedDataTest covers the Composite |
| 49 | + * ML-KEM EnvelopedData path, but the Composite ML-DSA OIDs had no CMS SignedData or PKIX CertPath |
| 50 | + * (chain) coverage even though they are wired through DefaultCMSSignatureAlgorithmNameGenerator and |
| 51 | + * the DefaultSignatureAlgorithmIdentifierFinder used on the CMS and cert-path paths. |
| 52 | + */ |
| 53 | +public class CompositeMLDSASignedDataTest |
| 54 | + extends TestCase |
| 55 | +{ |
| 56 | + private static final String BC = BouncyCastleProvider.PROVIDER_NAME; |
| 57 | + private static final byte[] DATA = "the composite ML-DSA cat sat on the CMS mat".getBytes(); |
| 58 | + |
| 59 | + private static final String[] NAMES = { |
| 60 | + "MLDSA44-ECDSA-P256-SHA256", "MLDSA44-Ed25519-SHA512", "MLDSA44-RSA2048-PKCS15-SHA256", "MLDSA44-RSA2048-PSS-SHA256", |
| 61 | + "MLDSA65-ECDSA-P256-SHA512", "MLDSA65-ECDSA-P384-SHA512", "MLDSA65-ECDSA-brainpoolP256r1-SHA512", "MLDSA65-Ed25519-SHA512", |
| 62 | + "MLDSA65-RSA3072-PKCS15-SHA512", "MLDSA65-RSA3072-PSS-SHA512", "MLDSA65-RSA4096-PKCS15-SHA512", "MLDSA65-RSA4096-PSS-SHA512", |
| 63 | + "MLDSA87-ECDSA-P384-SHA512", "MLDSA87-ECDSA-P521-SHA512", "MLDSA87-ECDSA-brainpoolP384r1-SHA512", "MLDSA87-Ed448-SHAKE256", |
| 64 | + "MLDSA87-RSA3072-PSS-SHA512", "MLDSA87-RSA4096-PSS-SHA512" |
| 65 | + }; |
| 66 | + |
| 67 | + private static final ASN1ObjectIdentifier[] OIDS = { |
| 68 | + IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, |
| 69 | + IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, |
| 70 | + IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, |
| 71 | + IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, |
| 72 | + IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, |
| 73 | + IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, |
| 74 | + IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, |
| 75 | + IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, |
| 76 | + IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512 |
| 77 | + }; |
| 78 | + |
| 79 | + public void setUp() |
| 80 | + { |
| 81 | + if (Security.getProvider(BC) == null) |
| 82 | + { |
| 83 | + Security.addProvider(new BouncyCastleProvider()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static X509Certificate buildCert(X500Name issuer, PrivateKey issuerKey, String sigName, |
| 88 | + X500Name subject, PublicKey subjectKey, boolean ca, long serial) |
| 89 | + throws Exception |
| 90 | + { |
| 91 | + long now = System.currentTimeMillis(); |
| 92 | + JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder( |
| 93 | + issuer, BigInteger.valueOf(serial), new Date(now - 3600000L), new Date(now + 365L * 24 * 3600000L), |
| 94 | + subject, subjectKey); |
| 95 | + builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(ca)); |
| 96 | + builder.addExtension(Extension.keyUsage, true, |
| 97 | + new KeyUsage(ca ? (KeyUsage.keyCertSign | KeyUsage.digitalSignature) : KeyUsage.digitalSignature)); |
| 98 | + ContentSigner signer = new JcaContentSignerBuilder(sigName).setProvider(BC).build(issuerKey); |
| 99 | + return new JcaX509CertificateConverter().setProvider(BC).getCertificate(builder.build(signer)); |
| 100 | + } |
| 101 | + |
| 102 | + public void testCompositeMLDSASignedData() |
| 103 | + throws Exception |
| 104 | + { |
| 105 | + for (int i = 0; i != NAMES.length; i++) |
| 106 | + { |
| 107 | + KeyPair kp = KeyPairGenerator.getInstance(NAMES[i], BC).generateKeyPair(); |
| 108 | + X500Name dn = new X500Name("CN=" + NAMES[i]); |
| 109 | + X509Certificate cert = buildCert(dn, kp.getPrivate(), NAMES[i], dn, kp.getPublic(), true, 1); |
| 110 | + |
| 111 | + ContentSigner signer = new JcaContentSignerBuilder(NAMES[i]).setProvider(BC).build(kp.getPrivate()); |
| 112 | + CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); |
| 113 | + gen.addSignerInfoGenerator( |
| 114 | + new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build()) |
| 115 | + .build(signer, new X509CertificateHolder(cert.getEncoded()))); |
| 116 | + List certList = new ArrayList(); |
| 117 | + certList.add(new X509CertificateHolder(cert.getEncoded())); |
| 118 | + gen.addCertificates(new CollectionStore(certList)); |
| 119 | + |
| 120 | + CMSSignedData sd = new CMSSignedData( |
| 121 | + gen.generate(new CMSProcessableByteArray(DATA), true).getEncoded()); |
| 122 | + |
| 123 | + SignerInformation signerInfo = sd.getSignerInfos().getSigners().iterator().next(); |
| 124 | + |
| 125 | + assertEquals(NAMES[i] + ": SignerInfo signatureAlgorithm OID", |
| 126 | + OIDS[i].getId(), signerInfo.getEncryptionAlgOID()); |
| 127 | + assertTrue(NAMES[i] + ": composite signature did not verify", |
| 128 | + signerInfo.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(cert))); |
| 129 | + |
| 130 | + // negative control: the same signature must not verify against a different composite key |
| 131 | + KeyPair other = KeyPairGenerator.getInstance(NAMES[i], BC).generateKeyPair(); |
| 132 | + X509Certificate otherCert = buildCert(dn, other.getPrivate(), NAMES[i], dn, other.getPublic(), true, 9); |
| 133 | + assertFalse(NAMES[i] + ": composite signature verified against the wrong key", |
| 134 | + signerInfo.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(otherCert))); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public void testCompositeMLDSACertPath() |
| 139 | + throws Exception |
| 140 | + { |
| 141 | + CertificateFactory cf = CertificateFactory.getInstance("X.509", BC); |
| 142 | + for (int i = 0; i != NAMES.length; i++) |
| 143 | + { |
| 144 | + KeyPair caKp = KeyPairGenerator.getInstance(NAMES[i], BC).generateKeyPair(); |
| 145 | + X500Name caDn = new X500Name("CN=Composite CA " + NAMES[i]); |
| 146 | + X509Certificate caCert = buildCert(caDn, caKp.getPrivate(), NAMES[i], caDn, caKp.getPublic(), true, 1); |
| 147 | + |
| 148 | + KeyPair eeKp = KeyPairGenerator.getInstance(NAMES[i], BC).generateKeyPair(); |
| 149 | + X509Certificate eeCert = buildCert(caDn, caKp.getPrivate(), NAMES[i], |
| 150 | + new X500Name("CN=Composite EE " + NAMES[i]), eeKp.getPublic(), false, 2); |
| 151 | + |
| 152 | + Set trust = new HashSet(); |
| 153 | + trust.add(new TrustAnchor(caCert, null)); |
| 154 | + |
| 155 | + CertPath cp = cf.generateCertPath(Collections.singletonList(eeCert)); |
| 156 | + PKIXParameters params = new PKIXParameters(trust); |
| 157 | + params.setRevocationEnabled(false); |
| 158 | + CertPathValidator.getInstance("PKIX", BC).validate(cp, params); |
| 159 | + |
| 160 | + // negative control: an EE certificate signed by a different CA key must not validate |
| 161 | + KeyPair rogueKp = KeyPairGenerator.getInstance(NAMES[i], BC).generateKeyPair(); |
| 162 | + X509Certificate rogueEe = buildCert(caDn, rogueKp.getPrivate(), NAMES[i], |
| 163 | + new X500Name("CN=Rogue EE " + NAMES[i]), eeKp.getPublic(), false, 3); |
| 164 | + CertPath badCp = cf.generateCertPath(Collections.singletonList(rogueEe)); |
| 165 | + try |
| 166 | + { |
| 167 | + CertPathValidator.getInstance("PKIX", BC).validate(badCp, params); |
| 168 | + fail(NAMES[i] + ": CertPath validated an EE signed by a key outside the anchor"); |
| 169 | + } |
| 170 | + catch (CertPathValidatorException expected) |
| 171 | + { |
| 172 | + // expected |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public static void main(String[] args) |
| 178 | + { |
| 179 | + junit.textui.TestRunner.run(CompositeMLDSASignedDataTest.class); |
| 180 | + } |
| 181 | +} |
0 commit comments