CredentialManagerImpl implements CredentialManager,
		Observable<KeystoreChangedEvent> { @Override public void exportKeyPair(String alias, Path exportFile, String pkcs12Password) throws CMException { initialize(); synchronized (keystore) { try { PrivateKey privateKey = (PrivateKey) keystore.getKey(alias, masterPassword.toCharArray()); Certificate[] certChain = getKeyPairsCertificateChain(alias); KeyStore newPkcs12 = KeyStore.getInstance("PKCS12", "BC"); newPkcs12.load(null, null); String sDN = ((X509Certificate) certChain[0]) .getSubjectX500Principal().getName(RFC2253); ParsedDistinguishedName parsedDN = dnParser.parseDN(sDN); String sCN = parsedDN.getCN(); String iDN = ((X509Certificate) certChain[0]) .getIssuerX500Principal().getName(RFC2253); parsedDN = dnParser.parseDN(iDN); String iCN = parsedDN.getCN(); String pkcs12Alias = sCN + "'s " + iCN + " ID"; newPkcs12.setKeyEntry(pkcs12Alias, privateKey, new char[0], certChain); try (OutputStream fos = Files.newOutputStream(exportFile)) { newPkcs12.store(fos, pkcs12Password.toCharArray()); } } catch (Exception ex) { String exMessage = "Failed to export the key pair from the Keystore"; logger.error(exMessage, ex); throw new CMException(exMessage, ex); } } } 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 testExportKeyPair() throws CMException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException { String alias = credentialManager.addKeyPair(privateKey, privateKeyCertChain); File fileToExportTo = new File(credentialManagerDirectory, "test-export-key.p12"); credentialManager.exportKeyPair(alias, fileToExportTo.toPath(), privateKeyAndPKCS12KeystorePassword); assertTrue(fileToExportTo.exists()); KeyStore ks = credentialManager.loadPKCS12Keystore(fileToExportTo.toPath(), privateKeyAndPKCS12KeystorePassword); Enumeration<String> aliases = ks.aliases(); Key newPrivateKey = null; Certificate[] newPrivateKeyCerts = null; while (aliases.hasMoreElements()) { alias = aliases.nextElement(); if (ks.isKeyEntry(alias)) { newPrivateKey = ks.getKey(alias, privateKeyAndPKCS12KeystorePassword.toCharArray()); newPrivateKeyCerts = ks.getCertificateChain(alias); break; } } assertNotNull(newPrivateKey); assertNotNull(newPrivateKeyCerts); assertTrue(newPrivateKey.equals(privateKey)); assertTrue(Arrays.equals(newPrivateKeyCerts, privateKeyCertChain)); }