Builder API for Certificates and Keystores

So I've been writing a bunch on JSSE and JCA:

To make life a little easier on myself, I've written a fluent builder API for creating X.509 certificates, keystores and SSLContext. It is available at and you add it to your project with either Maven:

    <version>1.0.0</version><!-- see badge for latest version -->

or SBT:

libraryDependencies += "com.tersesystems.securitybuilder" % "securitybuilder" % "1.0.0"

Best way to describe the builder API is to take a look at the X509CertificateCreator:

public class X509CertificateCreatorTest {
  public void testFunctionalStyle() throws Exception {
    FinalStage<RSAKeyPair> keyPairCreator = KeyPairCreator.creator().withRSA().withKeySize(2048);
    RSAKeyPair rootKeyPair = keyPairCreator.create();
    RSAKeyPair intermediateKeyPair = keyPairCreator.create();
    RSAKeyPair eePair = keyPairCreator.create();

    IssuerStage<RSAPrivateKey> creator =

    String issuer = "CN=letsencrypt.derp,O=Root CA";
    X509Certificate[] chain =
            .withRootCA(issuer, rootKeyPair, 2)
                rootCreator ->
                        .withSubject("OU=intermediate CA")
                            intCreator ->

    PrivateKeyStore privateKeyStore =
        PrivateKeyStore.create("", eePair.getPrivate(), chain);
    TrustStore trustStore = TrustStore.create(singletonList(chain[2]), cert -> "letsencrypt.derp");

    try {
      final PKIXCertPathValidatorResult result = CertificateChainValidator.validator()
          .withAnchor(new TrustAnchor(issuer, rootKeyPair.getPublic(), null))
      final PublicKey subjectPublicKey = result.getPublicKey();
    } catch (final CertPathValidatorException cpve) {
      fail("Cannot test exception", cpve);

    SSLContext sslContext =

The fluent API is staged builder pattern, so it's easy to autocomplete, and the defaults are sane. There are fixes for setting up specific passwords in KeyManagerKeyStoreBuilder, and keypair creation returns a KeyPair of the given type so you don't need to cast:

final RSAKeyPair keyPair = KeyPairCreator.creator().withRSA().withKeySize(2048).create();
RSAPublicKey publicKey = keyPair.getPublic();

There's only one dependency, on throwable-interfaces, but it's not exposed publically (it's used for passing suppliers around internally.)

Threads on Reddit for reference.

More …