Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

20.2 KB
Raw Blame
package opensuite.proxy;

import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.*;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import java.io.*;
import java.math.BigInteger;
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Generates a long-lived CA on first run and mints short leaf certificates
 * per host on demand.
 *
 * The 2008 original shipped one fixed certificate in a bundled keystore,
 * which no modern browser will accept. This is the replacement.
 */
public class CertificateAuthority {

    private static final String KEY_ALG = "RSA";
    private static final int CA_KEY_SIZE = 2048;
    private static final int LEAF_KEY_SIZE = 2048;
    private static final String SIG_ALG = "SHA256WithRSA";
    private static final char[] PASSWORD = "opensuite".toCharArray();
    /** What the store was locked with before the product was renamed; such a store is still opened, then re-locked. */
    private static final char[] OLD_PASSWORD = "opentrap".toCharArray();

    private final Path storePath;
    private X509Certificate caCert;
    private PrivateKey caKey;
    private KeyPair leafKeyPair;

    /**
     * Leaf contexts by host, kept bounded: a long session through many hosts
     * (every third-party domain on every page) would otherwise mint and retain
     * one RSA-backed context per hostname for the whole session. Order is
     * insertion order, for eviction; the hot path is the lock-free
     * {@code cache.get} below, and an evicted host is simply re-minted on its
     * next connection.
     */
    private static final int MAX_CACHED_CONTEXTS = 512;
    private final Map<String, SSLContext> cache = new ConcurrentHashMap<>();
    /** Hosts in insertion order; guarded by {@code this} alongside minting. */
    private final java.util.ArrayDeque<String> cacheOrder = new java.util.ArrayDeque<>();

    static {
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
    }

    public CertificateAuthority(Path dir) throws Exception {
        Files.createDirectories(dir);
        this.storePath = dir.resolve("ca.p12");
        if (!Files.exists(storePath)) generateCa();
        else {
            try { loadCa(); }
            catch (Exception unreadable) {
                // A store that cannot be read (replaced by some other file, cut
                // short by a full disk) would otherwise stop the proxy for good.
                // It is kept under another name, and a new authority takes its place.
                Path aside = storePath.resolveSibling("ca.p12.unreadable-" + System.currentTimeMillis());
                Files.move(storePath, aside, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
                generateCa();
                setAside = aside;
            }
        }
        // One leaf key pair is reused across hosts. Generating RSA keys is the
        // expensive part; reusing the key while varying the certificate keeps
        // first contact with a new host fast.
        KeyPairGenerator g = KeyPairGenerator.getInstance(KEY_ALG);
        g.initialize(LEAF_KEY_SIZE);
        this.leafKeyPair = g.generateKeyPair();
    }

    private Path setAside;

    /** Where an unreadable store was moved at start, when a new authority had to replace it; null otherwise. */
    public Path setAside() { return setAside; }

    /** A CA certificate with its private key, read from files and checked, not yet in use. */
    public record Pair(X509Certificate cert, PrivateKey key) {}

    /** A certificate and its private key from two files, DER or PEM. */
    public static Pair readFiles(Path certFile, Path keyFile) throws Exception {
        X509Certificate cert;
        try (InputStream in = Files.newInputStream(certFile)) {
            cert = (X509Certificate) java.security.cert.CertificateFactory.getInstance("X.509").generateCertificate(in);
        } catch (Exception e) {
            throw new IOException("The certificate file is not an X.509 certificate.");
        }
        byte[] raw = Files.readAllBytes(keyFile);
        String text = new String(raw, java.nio.charset.StandardCharsets.ISO_8859_1);
        if (text.contains("-----BEGIN PRIVATE KEY-----")) {
            raw = java.util.Base64.getMimeDecoder().decode(text.replaceAll("-----[A-Z ]+-----", ""));
        }
        PrivateKey key;
        try { key = java.security.KeyFactory.getInstance(KEY_ALG).generatePrivate(new java.security.spec.PKCS8EncodedKeySpec(raw)); }
        catch (Exception e) { throw new IOException("The key file is not an RSA private key in PKCS#8 format."); }
        return checked(cert, key);
    }

    /** The certificate and private key in a PKCS#12 store. */
    public static Pair readStore(Path store, char[] password) throws Exception {
        KeyStore ks = KeyStore.getInstance("PKCS12");
        try (InputStream in = Files.newInputStream(store)) { ks.load(in, password); }
        catch (IOException e) {
            throw new IOException(e.getCause() instanceof java.security.UnrecoverableKeyException
                    ? "The password is wrong." : "The file is not a PKCS#12 keystore.");
        }
        for (String alias : java.util.Collections.list(ks.aliases())) {
            if (!ks.isKeyEntry(alias)) continue;
            if (ks.getCertificate(alias) instanceof X509Certificate cert && ks.getKey(alias, password) instanceof PrivateKey key) {
                return checked(cert, key);
            }
        }
        throw new IOException("The keystore holds no certificate with a private key.");
    }

    /** Refuse what this proxy could not sign with: a key of another kind, a key of another certificate, a certificate that is no authority. */
    private static Pair checked(X509Certificate cert, PrivateKey key) throws Exception {
        if (!(key instanceof java.security.interfaces.RSAPrivateKey rsa)
                || !(cert.getPublicKey() instanceof java.security.interfaces.RSAPublicKey pub)) {
            throw new IOException("Only an RSA certificate and key can be used.");
        }
        if (!rsa.getModulus().equals(pub.getModulus())) throw new IOException("The private key does not belong to the certificate.");
        if (cert.getBasicConstraints() < 0) throw new IOException("The certificate is not a CA certificate.");
        return new Pair(cert, key);
    }

    /** Take a checked certificate and key as this installation's authority, from now on. */
    public synchronized void adopt(Pair pair) throws Exception {
        X509Certificate oldCert = caCert; PrivateKey oldKey = caKey;
        caCert = pair.cert(); caKey = pair.key();
        try { saveCa(); }
        catch (Exception e) { caCert = oldCert; caKey = oldKey; throw e; }
        cache.clear();
        cacheOrder.clear();
    }

    /** The CA's private key, PKCS#8 DER. */
    public byte[] caKeyDer() { return caKey.getEncoded(); }

    /** The CA certificate and private key as a PKCS#12 store locked with the given password. */
    public byte[] storeBytes(char[] password) throws Exception {
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(null, password);
        ks.setKeyEntry("cacert", caKey, password, new Certificate[]{caCert});
        java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
        ks.store(out, password);
        return out.toByteArray();
    }

    public X509Certificate caCertificate() { return caCert; }

    /** Where this installation's CA lives on disk, so it can be replaced. */
    public Path storePath() { return storePath; }

    /** DER bytes of the CA certificate, for the browser-facing download. */
    public byte[] caCertificateDer() throws Exception { return caCert.getEncoded(); }

    /**
     * Base64 SHA-256 of the CA's SubjectPublicKeyInfo. This is the value for
     * Chromium's --ignore-certificate-errors-spki-list flag, which makes the
     * browser trust leaf certificates under this one CA and no other.
     */
    public String spkiFingerprint() throws Exception {
        byte[] spki = caCert.getPublicKey().getEncoded();
        byte[] hash = java.security.MessageDigest.getInstance("SHA-256").digest(spki);
        return java.util.Base64.getEncoder().encodeToString(hash);
    }

    public String caCertificatePem() throws Exception {
        String b64 = java.util.Base64.getMimeEncoder(64, new byte[]{'\n'})
                .encodeToString(caCert.getEncoded());
        return "-----BEGIN CERTIFICATE-----\n" + b64 + "\n-----END CERTIFICATE-----\n";
    }

    private void loadCa() throws Exception {
        KeyStore ks = KeyStore.getInstance("PKCS12");
        char[] opened = PASSWORD;
        try (InputStream in = Files.newInputStream(storePath)) {
            ks.load(in, PASSWORD);
        } catch (java.io.IOException wrong) {
            // A store made before the rename: the same authority is kept, so the
            // certificate already trusted in browsers goes on working.
            ks = KeyStore.getInstance("PKCS12");
            try (InputStream in = Files.newInputStream(storePath)) { ks.load(in, OLD_PASSWORD); }
            opened = OLD_PASSWORD;
        }
        caCert = (X509Certificate) ks.getCertificate("ca");
        caKey = (PrivateKey) ks.getKey("ca", opened);
        if (caCert == null || caKey == null) { generateCa(); return; }
        if (opened == OLD_PASSWORD) saveCa();
    }

    /** Write the authority's key and certificate to the store. */
    private void saveCa() throws Exception {
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(null, PASSWORD);
        ks.setKeyEntry("ca", caKey, PASSWORD, new Certificate[]{caCert});
        // Written beside the store and moved over it: a write that fails part way leaves the old store whole.
        Path fresh = storePath.resolveSibling("ca.p12.new");
        try (OutputStream out = Files.newOutputStream(fresh,
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
            ks.store(out, PASSWORD);
        }
        ownerOnly(fresh);
        Files.move(fresh, storePath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
        ownerOnly(storePath);
    }

    /** Leave a file readable and writable by its owner alone, in whichever way this system's files are protected. */
    static void ownerOnly(Path file) {
        try {
            java.nio.file.attribute.AclFileAttributeView acl =
                    Files.getFileAttributeView(file, java.nio.file.attribute.AclFileAttributeView.class);
            if (Files.getFileAttributeView(file, java.nio.file.attribute.PosixFileAttributeView.class) != null) {
                Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-------"));
            } else if (acl != null) {
                // Windows: one entry, the owner with full access. Setting the list drops what the folder passed down.
                acl.setAcl(java.util.List.of(java.nio.file.attribute.AclEntry.newBuilder()
                        .setType(java.nio.file.attribute.AclEntryType.ALLOW)
                        .setPrincipal(Files.getOwner(file))
                        .setPermissions(java.util.EnumSet.allOf(java.nio.file.attribute.AclEntryPermission.class))
                        .build()));
            }
        } catch (Exception ignored) {}
    }

    private void generateCa() throws Exception {
        KeyPairGenerator g = KeyPairGenerator.getInstance(KEY_ALG);
        g.initialize(CA_KEY_SIZE);
        KeyPair kp = g.generateKeyPair();

        X500Name name = new X500Name("CN=Open Suite Proxy CA, O=Open Suite, OU=Certificate Authority");
        long now = System.currentTimeMillis();
        Date from = new Date(now - 24L * 3600 * 1000);
        Date to = new Date(now + 3650L * 24 * 3600 * 1000);

        JcaX509v3CertificateBuilder b = new JcaX509v3CertificateBuilder(
                name, BigInteger.valueOf(now), from, to, name, kp.getPublic());
        b.addExtension(Extension.basicConstraints, true, new BasicConstraints(true));
        b.addExtension(Extension.keyUsage, true,
                new KeyUsage(KeyUsage.keyCertSign | KeyUsage.cRLSign | KeyUsage.digitalSignature));
        JcaX509ExtensionUtils u = new JcaX509ExtensionUtils();
        b.addExtension(Extension.subjectKeyIdentifier, false,
                u.createSubjectKeyIdentifier(kp.getPublic()));

        ContentSigner signer = new JcaContentSignerBuilder(SIG_ALG).build(kp.getPrivate());
        caCert = new JcaX509CertificateConverter().getCertificate(b.build(signer));
        caKey = kp.getPrivate();
        saveCa();
    }

    /** A TLS context presenting a CA-signed certificate valid for this host. */
    public SSLContext contextFor(String host) throws Exception {
        return contextFor(host, null);
    }

    /**
     * A TLS context for this host, built the way the listener asks for.
     *
     * <p>Per-host is the default and signs a fresh leaf with this
     * installation's CA. A specific hostname signs one leaf and presents it
     * whatever host is asked for, which is what a client that pins a name
     * wants. Self-signed skips the CA altogether, so a client sees an
     * untrusted certificate unless it was told to accept that one. A custom
     * store presents a PKCS#12 the tester supplies, untouched.
     */
    public SSLContext contextFor(String host, ProxyListener cfg) throws Exception {
        String key = cacheKey(host, cfg);
        SSLContext f = cache.get(key);
        if (f != null) return f;
        synchronized (this) {
            f = cache.get(key);
            if (f != null) return f;
            f = build(host, cfg);
            cache.put(key, f);
            // Bounded, oldest first: keep the cache from growing one context
            // per hostname ever visited.
            cacheOrder.addLast(key);
            while (cacheOrder.size() > MAX_CACHED_CONTEXTS) {
                String evict = cacheOrder.pollFirst();
                if (evict != null) cache.remove(evict);
            }
            return f;
        }
    }

    /**
     * The cache is keyed by the certificate the listener will present, not by
     * host alone: changing the mode on a listener must not keep serving the
     * context minted under the previous one.
     */
    static String cacheKey(String host, ProxyListener cfg) {
        if (cfg == null) return "per-host\u0000" + host;
        return switch (cfg.cert) {
            case PER_HOST -> "per-host\u0000" + host;
            case SELF_SIGNED -> "self\u0000" + host;
            case SPECIFIC_HOSTNAME -> "named\u0000" + cfg.certHostname;
            case CUSTOM -> "custom\u0000" + cfg.certFile;
        };
    }

    private SSLContext build(String host, ProxyListener cfg) throws Exception {
        ProxyListener.Cert mode = cfg == null ? ProxyListener.Cert.PER_HOST : cfg.cert;
        if (mode == ProxyListener.Cert.CUSTOM) return customContext(cfg);

        String subject = mode == ProxyListener.Cert.SPECIFIC_HOSTNAME
                && cfg.certHostname != null && !cfg.certHostname.isBlank()
                ? cfg.certHostname.trim() : host;
        boolean selfSigned = mode == ProxyListener.Cert.SELF_SIGNED;

        X509Certificate leaf = selfSigned ? mintSelfSigned(subject) : mint(subject);
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(null, PASSWORD);
        // A self-signed leaf stands alone; a CA-signed one carries the CA so
        // the client can build the chain.
        ks.setKeyEntry("leaf", leafKeyPair.getPrivate(), PASSWORD,
                selfSigned ? new Certificate[]{leaf} : new Certificate[]{leaf, caCert});
        return contextOf(ks, PASSWORD);
    }

    /** A PKCS#12 the tester supplied, presented as it is. */
    private SSLContext customContext(ProxyListener cfg) throws Exception {
        if (cfg.certFile == null || cfg.certFile.isBlank()) {
            throw new IOException("No certificate file is set for this listener.");
        }
        char[] pw = (cfg.certPassword == null ? "" : cfg.certPassword).toCharArray();
        KeyStore ks = KeyStore.getInstance("PKCS12");
        try (InputStream in = Files.newInputStream(Path.of(cfg.certFile))) {
            ks.load(in, pw);
        }
        return contextOf(ks, pw);
    }

    private static SSLContext contextOf(KeyStore ks, char[] password) throws Exception {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(
                KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, password);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(kmf.getKeyManagers(), null, new SecureRandom());
        return sc;
    }

    /**
     * A leaf that signs itself. No CA is involved, so a client that trusts
     * this installation's CA still will not trust this: that is the point of
     * the choice, and it is how a server with its own certificate behaves.
     */
    private X509Certificate mintSelfSigned(String host) throws Exception {
        long now = System.currentTimeMillis();
        Date from = new Date(now - 24L * 3600 * 1000);
        Date to = new Date(now + 825L * 24 * 3600 * 1000);
        X500Name subject = new X500Name("CN=" + host.replace(",", "") + ", O=Open Suite");

        X509v3CertificateBuilder b = new JcaX509v3CertificateBuilder(
                subject, BigInteger.valueOf(now + host.hashCode()), from, to,
                subject, leafKeyPair.getPublic());
        b.addExtension(Extension.basicConstraints, false, new BasicConstraints(false));
        b.addExtension(Extension.keyUsage, true,
                new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
        b.addExtension(Extension.extendedKeyUsage, false,
                new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));
        GeneralName gn = isIpAddress(host)
                ? new GeneralName(GeneralName.iPAddress, host)
                : new GeneralName(GeneralName.dNSName, host);
        b.addExtension(Extension.subjectAlternativeName, false, new GeneralNames(gn));

        ContentSigner signer = new JcaContentSignerBuilder(SIG_ALG).build(leafKeyPair.getPrivate());
        return new JcaX509CertificateConverter().getCertificate(b.build(signer));
    }

    private X509Certificate mint(String host) throws Exception {
        long now = System.currentTimeMillis();
        Date from = new Date(now - 24L * 3600 * 1000);
        Date to = new Date(now + 825L * 24 * 3600 * 1000);

        X500Name subject = new X500Name("CN=" + host.replace(",", "") + ", O=Open Suite");
        // Use the CA's exact subject encoding as the issuer, so the chain links
        // byte-for-byte and the keystore accepts {leaf, ca}.
        X500Name issuer = new JcaX509CertificateHolder(caCert).getSubject();
        X509v3CertificateBuilder b = new JcaX509v3CertificateBuilder(
                issuer,
                BigInteger.valueOf(now + host.hashCode()),
                from, to, subject, leafKeyPair.getPublic());

        b.addExtension(Extension.basicConstraints, false, new BasicConstraints(false));
        b.addExtension(Extension.keyUsage, true,
                new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
        b.addExtension(Extension.extendedKeyUsage, false,
                new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));

        GeneralName gn = isIpAddress(host)
                ? new GeneralName(GeneralName.iPAddress, host)
                : new GeneralName(GeneralName.dNSName, host);
        b.addExtension(Extension.subjectAlternativeName, false,
                new GeneralNames(gn));

        ContentSigner signer = new JcaContentSignerBuilder(SIG_ALG).build(caKey);
        return new JcaX509CertificateConverter().getCertificate(b.build(signer));
    }

    private static boolean isIpAddress(String s) {
        return s.matches("\\d{1,3}(\\.\\d{1,3}){3}") || s.contains(":");
    }
}