@Test public void constructCert() throws Exception { Security.addProvider(new BouncyCastleProvider()); ((Logger)LoggerFactory.getLogger(CertificateGenerator.class)).setLevel(Level.DEBUG); File file = new File("/tmp/dm-agent.jks"); KeyPair keypair = createKeypair(); JcaX509v3CertificateBuilder cb = createRootCert(keypair); ContentSigner signer = new JcaContentSignerBuilder("SHA256withRSA").build(keypair.getPrivate()); X509CertificateHolder rootCert = cb.build(signer); KeystoreConfig cert = CertificateGenerator.constructCert(rootCert, keypair.getPrivate(), file, ImmutableSet.of("test1", "test2")); assertNotNull(cert); }
static KeystoreConfig constructCert(X509CertificateHolder rootCert, PrivateKey rootKey, File keystoreFile, Set<String> names) throws Exception { log.debug("Create certificate in {} keystore for names: {}", keystoreFile.getAbsolutePath(), names); KeystoreConfig.Builder cb = KeystoreConfig.builder(); KeyStore ks = KeyStore.Builder.newInstance("JKS", null, new KeyStore.PasswordProtection(null)).getKeyStore(); Certificate jceRootCert = toJava(rootCert); String keypass = "123456"; String kspass = "123456"; KeyPair keyPair = createKeypair(); X509CertificateHolder serverCert = createServerCert(rootKey, rootCert, keyPair, names); Certificate jceServerCert = toJava(serverCert); ks.setKeyEntry("key", keyPair.getPrivate(), keypass.toCharArray(), new Certificate[]{jceServerCert, jceRootCert}); cb.keystorePassword(kspass); cb.keyPassword(keypass); try(FileOutputStream fos = new FileOutputStream(keystoreFile)) { ks.store(fos, kspass.toCharArray()); } cb.keystore(keystoreFile); return cb.build(); }
CertificateGenerator { static KeystoreConfig constructCert(X509CertificateHolder rootCert, PrivateKey rootKey, File keystoreFile, Set<String> names) throws Exception { log.debug("Create certificate in {} keystore for names: {}", keystoreFile.getAbsolutePath(), names); KeystoreConfig.Builder cb = KeystoreConfig.builder(); KeyStore ks = KeyStore.Builder.newInstance("JKS", null, new KeyStore.PasswordProtection(null)).getKeyStore(); Certificate jceRootCert = toJava(rootCert); String keypass = "123456"; String kspass = "123456"; KeyPair keyPair = createKeypair(); X509CertificateHolder serverCert = createServerCert(rootKey, rootCert, keyPair, names); Certificate jceServerCert = toJava(serverCert); ks.setKeyEntry("key", keyPair.getPrivate(), keypass.toCharArray(), new Certificate[]{jceServerCert, jceRootCert}); cb.keystorePassword(kspass); cb.keyPassword(keypass); try(FileOutputStream fos = new FileOutputStream(keystoreFile)) { ks.store(fos, kspass.toCharArray()); } cb.keystore(keystoreFile); return cb.build(); } }
CertificateGenerator { static KeystoreConfig constructCert(X509CertificateHolder rootCert, PrivateKey rootKey, File keystoreFile, Set<String> names) throws Exception { log.debug("Create certificate in {} keystore for names: {}", keystoreFile.getAbsolutePath(), names); KeystoreConfig.Builder cb = KeystoreConfig.builder(); KeyStore ks = KeyStore.Builder.newInstance("JKS", null, new KeyStore.PasswordProtection(null)).getKeyStore(); Certificate jceRootCert = toJava(rootCert); String keypass = "123456"; String kspass = "123456"; KeyPair keyPair = createKeypair(); X509CertificateHolder serverCert = createServerCert(rootKey, rootCert, keyPair, names); Certificate jceServerCert = toJava(serverCert); ks.setKeyEntry("key", keyPair.getPrivate(), keypass.toCharArray(), new Certificate[]{jceServerCert, jceRootCert}); cb.keystorePassword(kspass); cb.keyPassword(keypass); try(FileOutputStream fos = new FileOutputStream(keystoreFile)) { ks.store(fos, kspass.toCharArray()); } cb.keystore(keystoreFile); return cb.build(); }  }
CertificateGenerator { static KeystoreConfig constructCert(X509CertificateHolder rootCert, PrivateKey rootKey, File keystoreFile, Set<String> names) throws Exception { log.debug("Create certificate in {} keystore for names: {}", keystoreFile.getAbsolutePath(), names); KeystoreConfig.Builder cb = KeystoreConfig.builder(); KeyStore ks = KeyStore.Builder.newInstance("JKS", null, new KeyStore.PasswordProtection(null)).getKeyStore(); Certificate jceRootCert = toJava(rootCert); String keypass = "123456"; String kspass = "123456"; KeyPair keyPair = createKeypair(); X509CertificateHolder serverCert = createServerCert(rootKey, rootCert, keyPair, names); Certificate jceServerCert = toJava(serverCert); ks.setKeyEntry("key", keyPair.getPrivate(), keypass.toCharArray(), new Certificate[]{jceServerCert, jceRootCert}); cb.keystorePassword(kspass); cb.keyPassword(keypass); try(FileOutputStream fos = new FileOutputStream(keystoreFile)) { ks.store(fos, kspass.toCharArray()); } cb.keystore(keystoreFile); return cb.build(); }   }
CertificateGenerator { static KeystoreConfig constructCert(X509CertificateHolder rootCert, PrivateKey rootKey, File keystoreFile, Set<String> names) throws Exception { log.debug("Create certificate in {} keystore for names: {}", keystoreFile.getAbsolutePath(), names); KeystoreConfig.Builder cb = KeystoreConfig.builder(); KeyStore ks = KeyStore.Builder.newInstance("JKS", null, new KeyStore.PasswordProtection(null)).getKeyStore(); Certificate jceRootCert = toJava(rootCert); String keypass = "123456"; String kspass = "123456"; KeyPair keyPair = createKeypair(); X509CertificateHolder serverCert = createServerCert(rootKey, rootCert, keyPair, names); Certificate jceServerCert = toJava(serverCert); ks.setKeyEntry("key", keyPair.getPrivate(), keypass.toCharArray(), new Certificate[]{jceServerCert, jceRootCert}); cb.keystorePassword(kspass); cb.keyPassword(keypass); try(FileOutputStream fos = new FileOutputStream(keystoreFile)) { ks.store(fos, kspass.toCharArray()); } cb.keystore(keystoreFile); return cb.build(); }    }