/** Returns an SSL client for this host's localhost address. */ public static synchronized SslClient localhost() { if (localhost != null) return localhost; try { // Generate a self-signed cert for the server to serve and the client to trust. HeldCertificate heldCertificate = new HeldCertificate.Builder() .serialNumber("1") .commonName(InetAddress.getByName("localhost").getCanonicalHostName()) .build(); localhost = new Builder() .certificateChain(heldCertificate.keyPair, heldCertificate.certificate) .addTrustedCertificate(heldCertificate.certificate) .build(); return localhost; } catch (GeneralSecurityException | UnknownHostException e) { throw new RuntimeException(e); } }