Skip to content

IllegalArgumentException when Building Certificate with Delta Certificate Extension if Subject and Issuer are the Same #1683

Description

@NoahFreising

While playing around with delta certificates, I noticed that BC throws an IllegalArgumentException when building a certificate with a delta certificate extension constructed using DeltaCertificateTool.makeDeltaCertificateExtension(). This occurs when both the Issuer and Subject of the base and delta certificate are the same. The exception is not thrown, and the certificate is constructed correctly, if only one of the Issuer and Subject is the same or if they are different. The relevant Internet-Draft appears to allow the same Subject and Issuer to be present in both the delta and base certificate, meaning that neither will be present in the constructed extension.

BC version: 1.78.1.

Stack trace:

Exception in thread "main" java.lang.IllegalArgumentException: illegal object in getInstance: org.bouncycastle.asn1.x509.SubjectPublicKeyInfo
at org.bouncycastle.asn1.ASN1BitString.getInstance(Unknown Source)
at org.bouncycastle.asn1.x509.DeltaCertificateDescriptor.<init>(Unknown Source)
at org.bouncycastle.asn1.x509.DeltaCertificateDescriptor.trimTo(Unknown Source)
at org.bouncycastle.cert.X509v3CertificateBuilder.build(Unknown Source)
at ch.freising.pqcthesis.DeltaCertMinimalTest.main(DeltaCertMinimalTest.java:52)

Minimum example that throws an exception:

packagech.freising.pqcthesis;
importorg.bouncycastle.asn1.x500.X500Name;
importorg.bouncycastle.asn1.x509.Extension;
importorg.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
importorg.bouncycastle.cert.DeltaCertificateTool;
importorg.bouncycastle.cert.X509CertificateHolder;
importorg.bouncycastle.cert.X509v3CertificateBuilder;
importorg.bouncycastle.jcajce.provider.asymmetric.rsa.KeyPairGeneratorSpi;
importorg.bouncycastle.operator.ContentSigner;
importorg.bouncycastle.operator.OperatorCreationException;
importorg.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
importjava.io.IOException;
publicclassDeltaCertMinimalTest {
publicstaticvoidmain(String[] args) throwsOperatorCreationException, IOException {
// Generate RSA KeyPairsKeyPairGeneratorSpirsaKeyGen = newKeyPairGeneratorSpi();
rsaKeyGen.initialize(2048, newjava.security.SecureRandom());
java.security.KeyPairdeltaKeyPair = rsaKeyGen.generateKeyPair();
java.security.KeyPairbaseKeyPair = rsaKeyGen.generateKeyPair();
// Generate a self-signed Delta CertificateX509v3CertificateBuilderdeltaCertBuilder = newX509v3CertificateBuilder(
newX500Name("CN=Issuer"),
java.math.BigInteger.valueOf(1L),
newjava.util.Date(System.currentTimeMillis()),
newjava.util.Date(System.currentTimeMillis() + 365L * 24 * 60 * 60 * 1000),
newX500Name("CN=Subject"),
SubjectPublicKeyInfo.getInstance(deltaKeyPair.getPublic().getEncoded())
);
ContentSignerdeltaRootSigner = newJcaContentSignerBuilder("SHA256withRSA").build(deltaKeyPair.getPrivate());
X509CertificateHolderdeltaCert = deltaCertBuilder.build(deltaRootSigner);
// Generate a self-signed Base CertificateX509v3CertificateBuilderbaseCertBuilder = newX509v3CertificateBuilder(
newX500Name("CN=Issuer"), // Same as Delta Certificatejava.math.BigInteger.valueOf(2L),
newjava.util.Date(System.currentTimeMillis()),
newjava.util.Date(System.currentTimeMillis() + 365L * 24 * 60 * 60 * 1000),
newX500Name("CN=Subject"), // Same as Delta CertificateSubjectPublicKeyInfo.getInstance(baseKeyPair.getPublic().getEncoded())
);
// Create Delta ExtensionExtensiondeltaCertExtension = DeltaCertificateTool.makeDeltaCertificateExtension(false, deltaCert);
// Add Delta Extension to Base CertificatebaseCertBuilder.addExtension(deltaCertExtension);
// Build Base CertificateContentSignerbaseRootSigner = newJcaContentSignerBuilder("SHA256withRSA").build(baseKeyPair.getPrivate());
X509CertificateHolderbaseCert = baseCertBuilder.build(baseRootSigner); // <= Exception thrown here
}
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions