CertificateDelegate extends Delegate { @Override public void applyDelegate(Config config) { Map<String, String> mandatoryParameters = new HashMap<>(); mandatoryParameters.put("keystore", keystore); mandatoryParameters.put("password", password); mandatoryParameters.put("alias", alias); PrivateKey privateKey = null; if (key != null) { LOGGER.debug("Loading private key"); try { privateKey = PemUtil.readPrivateKey(new File(key)); CustomPrivateKey customPrivateKey = CertificateUtils.parseCustomPrivateKey(privateKey); customPrivateKey.adjustInConfig(config, ConnectionEndType.CLIENT); customPrivateKey.adjustInConfig(config, ConnectionEndType.SERVER); } catch (IOException ex) { LOGGER.warn("Could not read private key", ex); } } if (certificate != null) { LOGGER.debug("Loading ceritificate"); try { Certificate cert = PemUtil.readCertificate(new File(certificate)); if (privateKey != null) { config.setDefaultExplicitCertificateKeyPair(new CertificateKeyPair(cert, privateKey)); } else { config.setDefaultExplicitCertificateKeyPair(new CertificateKeyPair(cert)); } config.setAutoSelectCertificate(false); } catch (Exception ex) { LOGGER.warn("Could not read certificate", ex); } } List<String> missingParameters = new ArrayList<>(); for (String p : mandatoryParameters.keySet()) { if (mandatoryParameters.get(p) == null) { missingParameters.add(p); } } if (missingParameters.size() == 3) { return; } else if (!missingParameters.isEmpty()) { throw new ParameterException("The following parameters are required for loading a" + " keystore: " + join(mandatoryParameters.keySet())); } try { ConnectionEndType type; switch (config.getDefaultRunningMode()) { case CLIENT: type = ConnectionEndType.CLIENT; break; case MITM: throw new ConfigurationException("CertificateDelegate is not allowed for MitM running mode"); case SERVER: type = ConnectionEndType.SERVER; break; default: throw new ConfigurationException("Unknown RunningMode"); } KeyStore store = KeystoreHandler.loadKeyStore(keystore, password); Certificate cert = JKSLoader.loadTLSCertificate(store, alias); privateKey = (PrivateKey) store.getKey(alias, password.toCharArray()); CertificateKeyPair pair = new CertificateKeyPair(cert, privateKey); pair.adjustInConfig(config, type); config.setAutoSelectCertificate(false); } catch (UnrecoverableKeyException | KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex) { throw new ConfigurationException("Could not load private Key from Keystore", ex); } } CertificateDelegate(); String getKeystore(); void setKeystore(String keystore); String getPassword(); void setPassword(String password); String getAlias(); void setAlias(String alias); String getCertificate(); void setCertificate(String certificate); String getKey(); void setKey(String key); @Override void applyDelegate(Config config);  }
@Test public void testApplyDelegate() throws NoSuchAlgorithmException, CertificateException, IOException, InvalidKeyException, KeyStoreException, NoSuchProviderException, SignatureException, OperatorCreationException { KeyStore store = KeyStoreGenerator.createKeyStore(KeyStoreGenerator.createRSAKeyPair(1024, random), random); File keyStoreFile = folder.newFile("key.store"); store.store(new FileOutputStream(keyStoreFile), "password".toCharArray()); args = new String[6]; args[0] = "-keystore"; args[1] = keyStoreFile.getAbsolutePath(); args[2] = "-password"; args[3] = "password"; args[4] = "-alias"; args[5] = "alias"; jcommander.parse(args); assertTrue("Keystore parameter gets not parsed correctly", delegate.getKeystore().equals(args[1])); assertTrue("Password parameter gets not parsed correctly", delegate.getPassword().equals(args[3])); assertTrue("Alias parameter gets not parsed correctly", delegate.getAlias().equals(args[5])); Config config = Config.createConfig(); config.setDefaultExplicitCertificateKeyPair(null); delegate.applyDelegate(config); assertNotNull("Ceritifcate could not be loaded", config.getDefaultExplicitCertificateKeyPair()); }
@Test(expected = ConfigurationException.class) public void testApplyDelegateInvalidPassword() { args = new String[6]; args[0] = "-keystore"; args[1] = "../resources/default.jks"; args[2] = "-password"; args[3] = "notthecorrectpassword"; args[4] = "-alias"; args[5] = "default"; jcommander.parse(args); Config config = Config.createConfig(); delegate.applyDelegate(config); }
@Test(expected = ConfigurationException.class) public void testApplyDelegateInvalidAlias() { args = new String[6]; args[0] = "-keystore"; args[1] = "../resources/default.jks"; args[2] = "-password"; args[3] = "password"; args[4] = "-alias"; args[5] = "notthecorrectalias"; jcommander.parse(args); Config config = Config.createConfig(); delegate.applyDelegate(config); }
@Test(expected = ConfigurationException.class) public void testApplyDelegateInvalidJKS() { args = new String[6]; args[0] = "-keystore"; args[1] = "../definetlynotacorrect.jks"; args[2] = "-password"; args[3] = "password"; args[4] = "-alias"; args[5] = "default"; jcommander.parse(args); Config config = Config.createConfig(); delegate.applyDelegate(config); }
@Test public void testNothingSetNothingChanges() { Config config = Config.createConfig(); Config config2 = Config.createConfig(); delegate.applyDelegate(config); assertTrue(EqualsBuilder.reflectionEquals(config, config2)); }