KeyStoreBuilder { public KeyStore buildKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException { KeyStore store = KeyStore.getInstance("JKS"); InputStream is = null; try { is = opener.openResource("keystore.jks"); if (is == null) { LOG.debug("NO KEYSTORE FOUND - initialising empty keystore"); } else { LOG.debug("FOUND KEYSTORE"); } store.load(is, getKeyStorePassword()); } finally { if (is != null) { try { is.close(); } catch (IOException e) { LOG.error("Error closing keystore resource", e); } } } Map<String, PublicKey> publicKeys = getConfiguredPublicKeys(); if (!publicKeys.isEmpty()) { LOG.info("Found " + publicKeys.size() + " encoded keys"); String dn = "CN=" + options.getProperty(PROJECT_TITLE, DEFAULT_TITLE); dn = dn.replaceAll("\\s", ""); int oneYear = 365; String alias = options.getProperty(SECURITY_PRIVATEKEY_ALIAS); PrivateKey signingKey = getOrGeneratePrivateKey(store, alias); KeySigner signer = new KeySigner(signingKey, dn, oneYear, KeySigner.DEFAULT_ALGORITHM); for (Entry<String, PublicKey> pair: publicKeys.entrySet()) { try { store.setCertificateEntry(pair.getKey(), signer.generateCertificate(pair.getKey(), pair.getValue())); } catch (SigningException e) { throw new CertificateException(CANT_GEN_CERT + pair.getKey()); } } } int n = 0; for (Enumeration<String> es = store.aliases(); es.hasMoreElements(); es.nextElement()) { n++; } LOG.debug("Finished configuring KEYSTORE - it contains " + n + " certificates"); return store; } KeyStoreBuilder(Properties options, ResourceOpener opener); KeyStore buildKeyStore();  }
@Test public void testUnconfigured() throws Exception { KeyStoreBuilder builder = new KeyStoreBuilder(new Properties(), nullOpener); KeyStore store = builder.buildKeyStore(); Enumeration<String> aliases = store.aliases(); assertFalse(aliases.hasMoreElements()); }
@Test public void testKeyInProperties() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.test", encoded); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore store = builder.buildKeyStore(); Enumeration<String> aliases = store.aliases(); assertTrue("There is at least one alias", aliases.hasMoreElements()); assertEquals("test", aliases.nextElement()); Certificate cert = store.getCertificate("test"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); }
@Test public void testMultipleKeysInProperties() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.test1", encoded); options.setProperty("security.publickey.test2", encoded2); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore store = builder.buildKeyStore(); Enumeration<String> aliases = store.aliases(); assertTrue("There is at least one alias", aliases.hasMoreElements()); Certificate cert = store.getCertificate("test1"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); Certificate cert2 = store.getCertificate("test2"); assertNotNull("Could not find certificate", cert); assertEquals(key2, cert2.getPublicKey()); }
@Test public void testKeyInPropertiesLaxDecoding() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.test", "NOT A KEY"); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore store = builder.buildKeyStore(); Enumeration<String> aliases = store.aliases(); assertFalse(aliases.hasMoreElements()); }
@Test public void testMixedKeysInProperties() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.good", encoded); options.setProperty("security.publickey.bad", "NOT A KEY"); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore store = builder.buildKeyStore(); Enumeration<String> aliases = store.aliases(); assertTrue("There is at least one alias", aliases.hasMoreElements()); assertEquals("good", aliases.nextElement()); assertFalse("There are no more than one aliases", aliases.hasMoreElements()); Certificate cert = store.getCertificate("good"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); assertNull(store.getCertificate("bad")); }
@Test public void testKeyInPropertiesStrictDecoding() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.test", "NOT A KEY"); options.setProperty("keystore.strictpublickeydecoding", "true"); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); try { builder.buildKeyStore(); fail("Expected an exception"); } catch (RuntimeException e) { assertEquals("Could not decode key for test", e.getMessage()); } }
@Test public void testKeyInProtectedKeyStore() throws Exception { String password = "pa$$w0rd"; Properties options = new Properties(); options.setProperty("security.publickey.test", encoded); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore source = builder.buildKeyStore(); ByteArrayOutputStream os = new ByteArrayOutputStream(); source.store(os, password.toCharArray()); ResourceOpener opener = new ConstantOpener(new ByteArrayInputStream(os.toByteArray())); options = new Properties(); options.setProperty("security.keystore.password", password); builder = new KeyStoreBuilder(options, opener); KeyStore store = builder.buildKeyStore(); Certificate cert = store.getCertificate("test"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); }
@Test public void testKeyInUnprotectedKeyStore() throws Exception { Properties options = new Properties(); options.setProperty("security.publickey.test", encoded); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore source = builder.buildKeyStore(); ByteArrayOutputStream os = new ByteArrayOutputStream(); source.store(os, new char[0]); ResourceOpener opener = new ConstantOpener(new ByteArrayInputStream(os.toByteArray())); options = new Properties(); builder = new KeyStoreBuilder(options, opener); KeyStore store = builder.buildKeyStore(); Certificate cert = store.getCertificate("test"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); }
@Test public void testKeyStoreAndProperties() throws Exception { String password = "s3kvre"; Properties options = new Properties(); options.setProperty("security.publickey.from-store", encoded); KeyStoreBuilder builder = new KeyStoreBuilder(options, nullOpener); KeyStore source = builder.buildKeyStore(); ByteArrayOutputStream os = new ByteArrayOutputStream(); source.store(os, password.toCharArray()); ResourceOpener opener = new ConstantOpener(new ByteArrayInputStream(os.toByteArray())); options = new Properties(); options.setProperty("security.keystore.password", password); options.setProperty("security.publickey.from-properties", encoded2); builder = new KeyStoreBuilder(options, opener); KeyStore store = builder.buildKeyStore(); Certificate cert = store.getCertificate("from-store"); assertNotNull("Could not find certificate", cert); assertEquals(key, cert.getPublicKey()); Certificate cert2 = store.getCertificate("from-properties"); assertNotNull("Could not find certificate", cert2); assertEquals(key2, cert2.getPublicKey()); }