Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0 Cleanup of CertToolTest

ghidra18ca9077040cc
diff --git a/Ghidra/Framework/Generic/src/test/java/ghidra/net/CertToolTest.java b/Ghidra/Framework/Generic/src/test/java/ghidra/net/CertToolTest.java
index 869f7b7d63..a4c155ecc4 100644
--- a/Ghidra/Framework/Generic/src/test/java/ghidra/net/CertToolTest.java
+++ b/Ghidra/Framework/Generic/src/test/java/ghidra/net/CertToolTest.java
@@ -15,25 +15,19 @@
*/
package ghidra.net;
+import static ghidra.net.PKITestUtils.certificate;
import static org.junit.Assert.*;
import java.io.*;
-import java.math.BigInteger;
import java.nio.file.Files;
import java.security.*;
+import java.security.cert.CertPath;
+import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.*;
-import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.BasicConstraints;
-import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyUsage;
-import org.bouncycastle.cert.jcajce.JcaCertStore;
-import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
-import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
-import org.bouncycastle.cms.CMSAbsentContent;
-import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
@@ -47,7 +41,6 @@ public class CertToolTest {
private static X509Certificate intermediate;
private static X509Certificate leaf;
private static X509Certificate otherRoot;
- private static long serial;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -66,27 +59,6 @@ public class CertToolTest {
otherRoot = certificate("Other", otherKey, "Other", otherKey, true, false);
}
- private static X509Certificate certificate(String subject, KeyPair key, String issuer,
- KeyPair signer, boolean ca, boolean expired) throws Exception {
- return certificate(subject, key, issuer, signer, new BasicConstraints(ca),
- ca ? KeyUsage.keyCertSign : KeyUsage.digitalSignature, expired);
- }
-
- private static X509Certificate certificate(String subject, KeyPair key, String issuer,
- KeyPair signer, BasicConstraints constraints, int usage, boolean expired)
- throws Exception {
- long now = System.currentTimeMillis();
- Date start = new Date(now - 86400000L);
- Date end = new Date(now + (expired ? -3600000L : 86400000L));
- JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
- new X500Name("CN=" + issuer), BigInteger.valueOf(++serial), start, end,
- new X500Name("CN=" + subject), key.getPublic());
- builder.addExtension(Extension.basicConstraints, true, constraints);
- builder.addExtension(Extension.keyUsage, true, new KeyUsage(usage));
- return new JcaX509CertificateConverter().getCertificate(builder.build(
- new JcaContentSignerBuilder("SHA256withRSA").build(signer.getPrivate())));
- }
-
private X509Certificate[] build(X509Certificate... certs) throws GeneralSecurityException {
return CertTool.buildCertificateChain(leafKey.getPublic(), Arrays.asList(certs), List.of());
}
@@ -271,9 +243,10 @@ public class CertToolTest {
@Test
public void testPkcs7BundleImport() throws Exception {
File file = temporaryFolder.newFile("bundle.p7b");
- CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
- generator.addCertificates(new JcaCertStore(List.of(root, leaf, intermediate)));
- Files.write(file.toPath(), generator.generate(new CMSAbsentContent()).getEncoded());
+ // PKCS#7 encodes a certificate set; the input need not be an ordered, validated path.
+ CertificateFactory factory = CertificateFactory.getInstance("X.509");
+ CertPath certificates = factory.generateCertPath(List.of(root, leaf, intermediate));
+ Files.write(file.toPath(), certificates.getEncoded("PKCS7"));
assertArrayEquals(new X509Certificate[] { leaf, intermediate, root },
build(CertTool.loadCertificates(file.getPath())));
assertArrayEquals(new X509Certificate[] { leaf, intermediate, root },
diff --git a/Ghidra/Framework/Generic/src/test/java/ghidra/net/PKITestUtils.java b/Ghidra/Framework/Generic/src/test/java/ghidra/net/PKITestUtils.java
index c945a37f13..2ff8355837 100644
--- a/Ghidra/Framework/Generic/src/test/java/ghidra/net/PKITestUtils.java
+++ b/Ghidra/Framework/Generic/src/test/java/ghidra/net/PKITestUtils.java
@@ -16,23 +16,84 @@
package ghidra.net;
import java.io.File;
+import java.math.BigInteger;
import java.security.*;
import java.security.KeyStore.PasswordProtection;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.cert.X509Certificate;
import java.util.Collection;
+import java.util.Date;
+import java.util.concurrent.atomic.AtomicLong;
import javax.security.auth.DestroyFailedException;
+import org.bouncycastle.asn1.x500.X500Name;
+import org.bouncycastle.asn1.x509.BasicConstraints;
+import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyUsage;
+import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
+import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
+import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import ghidra.util.exception.AssertException;
public class PKITestUtils {
+ private static final AtomicLong CERTIFICATE_SERIAL = new AtomicLong();
+
private PKITestUtils() {
// no construct
}
+
+ /**
+ * Generate a certificate fixture using existing keys. CA certificates permit certificate
+ * signing; other certificates permit digital signatures. The issuer name and signing key
+ * are independent so tests can deliberately construct invalid chains.
+ *
+ * @param subject subject common name
+ * @param key subject key pair
+ * @param issuer issuer common name
+ * @param signer key pair used to sign the certificate
+ * @param ca whether the certificate is a CA
+ * @param expired whether the certificate expired an hour ago rather than expiring tomorrow
+ * @return generated certificate
+ * @throws Exception if certificate construction or signing fails
+ */
+ public static X509Certificate certificate(String subject, KeyPair key, String issuer,
+ KeyPair signer, boolean ca, boolean expired) throws Exception {
+ return certificate(subject, key, issuer, signer, new BasicConstraints(ca),
+ ca ? KeyUsage.keyCertSign : KeyUsage.digitalSignature, expired);
+ }
+
+ /**
+ * Generate a certificate fixture with explicit CA constraints and key usage. This permits
+ * negative tests to construct certificates with inconsistent extensions or signing keys.
+ * Serial numbers are unique within this helper's lifetime, including concurrent callers.
+ *
+ * @param subject subject common name
+ * @param key subject key pair
+ * @param issuer issuer common name
+ * @param signer key pair used to sign the certificate
+ * @param constraints CA indication and optional path-length constraint
+ * @param usage bit mask of {@link KeyUsage} flags
+ * @param expired whether the certificate expired an hour ago rather than expiring tomorrow
+ * @return generated certificate, valid from yesterday until the selected expiration time
+ * @throws Exception if certificate construction or signing fails
+ */
+ public static X509Certificate certificate(String subject, KeyPair key, String issuer,
+ KeyPair signer, BasicConstraints constraints, int usage, boolean expired)
+ throws Exception {
+ long now = System.currentTimeMillis();
+ Date start = new Date(now - 86400000L);
+ Date end = new Date(now + (expired ? -3600000L : 86400000L));
+ JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
+ new X500Name("CN=" + issuer), BigInteger.valueOf(CERTIFICATE_SERIAL.incrementAndGet()),
+ start, end, new X500Name("CN=" + subject), key.getPublic());
+ builder.addExtension(Extension.basicConstraints, true, constraints);
+ builder.addExtension(Extension.keyUsage, true, new KeyUsage(usage));
+ return new JcaX509CertificateConverter().getCertificate(builder.build(
+ new JcaContentSignerBuilder("SHA256withRSA").build(signer.getPrivate())));
+ }
/**
* Generate a new {@link X509Certificate} with RSA {@link KeyPair} and create/update a {@link KeyStore}