KeyStoreUtil { public static void updateWithCaPem(KeyStore pTrustStore, File pCaCert) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException { InputStream is = new FileInputStream(pCaCert); try { CertificateFactory certFactory = CertificateFactory.getInstance("X509"); Collection<? extends Certificate> certificates = certFactory.generateCertificates(is); for (Certificate c : certificates) { X509Certificate cert = (X509Certificate) c; String alias = cert.getSubjectX500Principal().getName(); pTrustStore.setCertificateEntry(alias, cert); } } finally { is.close(); } } private  KeyStoreUtil(); static void updateWithCaPem(KeyStore pTrustStore, File pCaCert); static void updateWithServerPems(KeyStore pKeyStore, File pServerCert, File pServerKey, String pKeyAlgo, char[] pPassword); static void updateWithSelfSignedServerCertificate(KeyStore pKeyStore);  }
@Test public void testTrustStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { File caPem = getTempFile("ca/cert.pem"); KeyStore keystore = createKeyStore(); KeyStoreUtil.updateWithCaPem(keystore, caPem); List<String> aliases = asList(keystore.aliases()); assertEquals(aliases.size(), 1); String alias = aliases.get(0); assertTrue(alias.contains("ca.test.jolokia.org")); X509Certificate cert = (X509Certificate) keystore.getCertificate(alias); assertTrue(cert.getSubjectDN().getName().contains(CA_CERT_SUBJECT_DN_CN)); RSAPublicKey key = (RSAPublicKey) cert.getPublicKey(); assertEquals(key.getAlgorithm(),"RSA"); }
@Test public void testTrustStoreWithMultipleEntries() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { File caPem = getTempFile("ca/cert-multi.pem"); KeyStore keystore = createKeyStore(); KeyStoreUtil.updateWithCaPem(keystore, caPem); List<String> aliases = asList(keystore.aliases()); assertEquals(aliases.size(), 2); Map<String, String> expectedAliases = new HashMap<String, String>(); expectedAliases.put("ca.test.jolokia.org",CA_CERT_SUBJECT_DN_CN); expectedAliases.put("another.test.jolokia.org","CN=another.test.jolokia.org"); for (String alias : aliases) { String key = findMatchingKeyAsSubstring(expectedAliases, alias); assertNotNull(key); String expectedSubjectDN = expectedAliases.remove(key); assertNotNull(expectedSubjectDN); X509Certificate cert = (X509Certificate) keystore.getCertificate(alias); assertTrue(cert.getSubjectDN().getName().contains(expectedSubjectDN)); RSAPublicKey certPublicKey = (RSAPublicKey) cert.getPublicKey(); assertEquals(certPublicKey.getAlgorithm(),"RSA"); } assertEquals(expectedAliases.size(),0); }