Search in sources :

Example 1 with DExportKeyPair

use of org.kse.gui.dialogs.importexport.DExportKeyPair in project keystore-explorer by kaikramer.

the class ExportKeyPairAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    File exportFile = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        Certificate[] certificates = keyStore.getCertificateChain(alias);
        DExportKeyPair dExportKeyPair = new DExportKeyPair(frame, alias, applicationSettings.getPasswordQualityConfig());
        dExportKeyPair.setLocationRelativeTo(frame);
        dExportKeyPair.setVisible(true);
        if (!dExportKeyPair.exportSelected()) {
            return;
        }
        exportFile = dExportKeyPair.getExportFile();
        Password exportPassword = dExportKeyPair.getExportPassword();
        KeyStore pkcs12 = KeyStoreUtil.create(KeyStoreType.PKCS12);
        certificates = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certificates));
        pkcs12.setKeyEntry(alias, privateKey, exportPassword.toCharArray(), certificates);
        KeyStoreUtil.save(pkcs12, exportFile, exportPassword);
        JOptionPane.showMessageDialog(frame, res.getString("ExportKeyPairAction.ExportKeyPairSuccessful.message"), res.getString("ExportKeyPairAction.ExportKeyPair.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (FileNotFoundException ex) {
        String message = MessageFormat.format(res.getString("ExportKeyPairAction.NoWriteFile.message"), exportFile);
        JOptionPane.showMessageDialog(frame, message, res.getString("ExportKeyPairAction.ExportKeyPair.Title"), JOptionPane.WARNING_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) DExportKeyPair(org.kse.gui.dialogs.importexport.DExportKeyPair) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) KeyStore(java.security.KeyStore) FileNotFoundException(java.io.FileNotFoundException) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 Certificate (java.security.cert.Certificate)1 Password (org.kse.crypto.Password)1 DExportKeyPair (org.kse.gui.dialogs.importexport.DExportKeyPair)1 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)1 KeyStoreState (org.kse.utilities.history.KeyStoreState)1