CertUtil { static CertPack getCertPackForLocalhostSsl(final String sslCertificateAlias, final String storePassword) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, CertificateException, SignatureException, IOException { final PrivateKeyEntry caCertificatePrivateKeyEntry = generateCaCertificate( AF_ROOT_SUBJECT_PRINCIPAL ); final PrivateKeyEntry sslCertificatePrivateKeyEntry = generateSslCertificate( "127.0.0.1", "127.0.0.1", "localhost", caCertificatePrivateKeyEntry ); return new CertPack( caCertificatePrivateKeyEntry.getCertificate(), sslCertificatePrivateKeyEntry, sslCertificateAlias, storePassword.toCharArray() ); }    }
@SuppressWarnings("static-method") @Test public void testCertGeneration() throws Exception { final CertPack cp = CertUtil.getCertPackForLocalhostSsl("AutoFirma", "654321"); System.out.println(AOUtil.getCN((X509Certificate) cp.getCaCertificate())); System.out.println(AOUtil.getCN((X509Certificate) cp.getSslCertificate())); System.out.println(); System.out.println(); System.out.println(AOUtil.hexify(cp.getPkcs12(), true)); try ( OutputStream fos = new FileOutputStream(File.createTempFile("SSLCERT_", ".cer")) ) { fos.write(cp.getSslCertificate().getEncoded()); } try ( OutputStream fos = new FileOutputStream(File.createTempFile("CACERT_", ".cer")) ) { fos.write(cp.getCaCertificate().getEncoded()); } }