CredentialManagerImpl implements CredentialManager,
		Observable<KeystoreChangedEvent> { @Override public void changeMasterPassword(String newMasterPassword) throws CMException { initialize(); String oldMasterPassword = masterPassword; KeyStore oldKeystore = keystore; KeyStore oldTruststore = truststore; try { synchronized (keystore) { KeyStore newKeystore = null; try { newKeystore = KeyStore.getInstance("UBER", "BC"); } catch (Exception ex) { String exMessage = "Failed to instantiate a new Bouncy Castle Keystore when changing master password."; throw new CMException(exMessage, ex); } try { newKeystore.load(null, null); } catch (Exception ex) { String exMessage = "Failed to create a new empty Keystore to copy over the entries from the current one."; throw new CMException(exMessage, ex); } Enumeration<String> aliases = keystore.aliases(); while (aliases.hasMoreElements()) { String alias = aliases.nextElement(); if (REALLY_DISABLED) { if (alias.startsWith("password#")) { SecretKeySpec passwordKey = (((SecretKeySpec) keystore .getKey(alias, masterPassword.toCharArray()))); newKeystore.setKeyEntry(alias, passwordKey, newMasterPassword.toCharArray(), null); } else if (alias.startsWith("keypair#")) { PrivateKey privateKey = (PrivateKey) keystore .getKey(alias, masterPassword.toCharArray()); Certificate[] certChain = keystore .getCertificateChain(alias); newKeystore.setKeyEntry(alias, privateKey, newMasterPassword.toCharArray(), certChain); } } newKeystore.setEntry( alias, keystore.getEntry(alias, new KeyStore.PasswordProtection( masterPassword.toCharArray())), new KeyStore.PasswordProtection(newMasterPassword .toCharArray())); } try (FileOutputStream fos = new FileOutputStream(keystoreFile)) { newKeystore.store(fos, newMasterPassword.toCharArray()); } keystore = newKeystore; } synchronized (truststore) { try (FileOutputStream fos = new FileOutputStream(truststoreFile)) { truststore.store(fos, newMasterPassword.toCharArray()); } } masterPassword = newMasterPassword; } catch (Exception ex) { keystore = oldKeystore; truststore = oldTruststore; masterPassword = oldMasterPassword; saveKeystore(KEYSTORE); saveKeystore(TRUSTSTORE); String exMessage = "Failed to change maaster password - reverting to the old one"; logger.error(exMessage, ex); throw (new CMException(exMessage)); } } CredentialManagerImpl(); void installAuthenticator(); void deleteRevokedCertificates(); @Deprecated String[] getUsernameAndPasswordForService(String serviceURL); @Override UsernamePassword getUsernameAndPasswordForService(URI serviceURI,
			boolean usePathRecursion, String requestingMessage); void addFragmentedURI(LinkedHashSet<URI> possibles, URI uri,
			String rawFragment); @Deprecated ArrayList<String> getServiceURLsforAllUsernameAndPasswordPairs(); @Override String addUsernameAndPasswordForService(
			UsernamePassword usernamePassword, URI serviceURI); @Deprecated String saveUsernameAndPasswordForService(String username,
			String password, String serviceURL); @Override void deleteUsernameAndPasswordForService(URI serviceURI); @Deprecated void deleteUsernameAndPasswordForService(String serviceURL); @Override String addKeyPair(Key privateKey, Certificate[] certs); @Override boolean hasKeyPair(Key privateKey, Certificate[] certs); @Override void deleteKeyPair(String alias); @Override void deleteKeyPair(Key privateKey, Certificate[] certs); @Override void exportKeyPair(String alias, Path exportFile,
			String pkcs12Password); @Override Certificate getCertificate(KeystoreType ksType, String alias); @Override Certificate[] getKeyPairsCertificateChain(String alias); @Override Key getKeyPairsPrivateKey(String alias); @Override String addTrustedCertificate(X509Certificate cert); @Override String createKeyPairAlias(Key privateKey, Certificate certs[]); @Override String createTrustedCertificateAlias(X509Certificate cert); @Override boolean hasTrustedCertificate(Certificate cert); @Override void deleteTrustedCertificate(String alias); @Override void deleteTrustedCertificate(X509Certificate cert); @Override boolean isKeyEntry(String alias); @Override boolean hasEntryWithAlias(KeystoreType ksType, String alias); @Override ArrayList<String> getAliases(KeystoreType ksType); @Override List<URI> getServiceURIsForAllUsernameAndPasswordPairs(); @Override KeyStore loadPKCS12Keystore(Path pkcs12File, String pkcs12Password); @Override void addObserver(Observer<KeystoreChangedEvent> observer); @Override List<Observer<KeystoreChangedEvent>> getObservers(); @Override void removeObserver(Observer<KeystoreChangedEvent> observer); @Override boolean confirmMasterPassword(String password); @Override void changeMasterPassword(String newMasterPassword); @Override void initializeSSL(); @Override SSLSocketFactory getTavernaSSLSocketFactory(); @Override Authenticator getAuthenticator(); URI normalizeServiceURI(URI serviceURI); @Override boolean resetAuthCache(); @Override boolean hasUsernamePasswordForService(URI serviceURI); @Override void setConfigurationDirectoryPath(Path credentialManagerPath); void setMasterPasswordProviders(
			List<MasterPasswordProvider> masterPasswordProviders); List<MasterPasswordProvider> getMasterPasswordProviders(); void setJavaTruststorePasswordProviders(
			List<JavaTruststorePasswordProvider> javaTruststorePasswordProvider); List<JavaTruststorePasswordProvider> getJavaTruststorePasswordProviders(); void setServiceUsernameAndPasswordProviders(
			List<ServiceUsernameAndPasswordProvider> serviceUsernameAndPasswordProviders); List<ServiceUsernameAndPasswordProvider> getServiceUsernameAndPasswordProviders(); void setTrustConfirmationProviders(
			List<TrustConfirmationProvider> trustConfirmationProviders); List<TrustConfirmationProvider> getTrustConfirmationProviders(); void setApplicationConfiguration(
			ApplicationConfiguration applicationConfiguration); static List<String> defaultTrustStorePasswords; static final String OLD_TRUSTSTORE_PASSWORD; static final String OLD_T2TRUSTSTORE_FILE; }
@Test public void testChangeMasterPassword() throws CMException { credentialManager.changeMasterPassword("blah"); credentialManager.confirmMasterPassword("blah"); String keyPairAlias = credentialManager.addKeyPair(privateKey, privateKeyCertChain); credentialManager.addUsernameAndPasswordForService(usernamePassword, serviceURI); credentialManager.changeMasterPassword("hlab"); assertArrayEquals(credentialManager.getUsernameAndPasswordForService(serviceURI, false, "").getPassword(), usernamePassword.getPassword()); assertEquals(privateKey, credentialManager.getKeyPairsPrivateKey(keyPairAlias)); assertTrue(Arrays.equals(privateKeyCertChain, credentialManager.getKeyPairsCertificateChain(keyPairAlias))); CredentialManagerImpl credentialManagerNew = null; try { credentialManagerNew = new CredentialManagerImpl(); } catch (CMException e) { System.out.println(e.getStackTrace()); } try { credentialManagerNew .setConfigurationDirectoryPath(credentialManagerDirectory.toPath()); } catch (CMException e) { System.out.println(e.getStackTrace()); } masterPasswordProvider = new DummyMasterPasswordProvider(); masterPasswordProvider.setMasterPassword("hlab"); List<MasterPasswordProvider> masterPasswordProviders = new ArrayList<MasterPasswordProvider>(); masterPasswordProviders.add(masterPasswordProvider); credentialManager.setMasterPasswordProviders(masterPasswordProviders); credentialManagerNew.setServiceUsernameAndPasswordProviders(new ArrayList<ServiceUsernameAndPasswordProvider>()); credentialManager.setJavaTruststorePasswordProviders(new ArrayList<JavaTruststorePasswordProvider>()); credentialManager.setTrustConfirmationProviders(new ArrayList<TrustConfirmationProvider>()); assertArrayEquals(credentialManager.getUsernameAndPasswordForService(serviceURI, false, "").getPassword(), usernamePassword.getPassword()); assertEquals(privateKey, credentialManager.getKeyPairsPrivateKey(keyPairAlias)); assertTrue(Arrays.equals(privateKeyCertChain, credentialManager.getKeyPairsCertificateChain(keyPairAlias))); }