Search in sources :

Example 6 with DGetAlias

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

the class ImportKeyPairAction method importKeyPairOpenSsl.

private void importKeyPairOpenSsl() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairOpenSsl dImportKeyPairOpenSsl = new DImportKeyPairOpenSsl(frame);
        dImportKeyPairOpenSsl.setLocationRelativeTo(frame);
        dImportKeyPairOpenSsl.setVisible(true);
        PrivateKey privateKey = dImportKeyPairOpenSsl.getPrivateKey();
        Certificate[] certs = dImportKeyPairOpenSsl.getCertificateChain();
        if ((privateKey == null) || (certs == null)) {
            return;
        }
        X509Certificate[] x509Certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(x509Certs[0]));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("ImportKeyPairAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }
        Password password = new Password((char[]) null);
        KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
        if (type.hasEntryPasswords()) {
            DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryPassword.Title"), applicationSettings.getPasswordQualityConfig());
            dGetNewPassword.setLocationRelativeTo(frame);
            dGetNewPassword.setVisible(true);
            password = dGetNewPassword.getPassword();
            if (password == null) {
                return;
            }
        }
        if (keyStore.containsAlias(alias)) {
            keyStore.deleteEntry(alias);
            newState.removeEntryPassword(alias);
        }
        keyStore.setKeyEntry(alias, privateKey, password.toCharArray(), x509Certs);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ImportKeyPairAction.KeyPairImportSuccessful.message"), res.getString("ImportKeyPairAction.ImportKeyPair.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) DGetAlias(org.kse.gui.dialogs.DGetAlias) DImportKeyPairOpenSsl(org.kse.gui.dialogs.importexport.DImportKeyPairOpenSsl) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DGetNewPassword(org.kse.gui.password.DGetNewPassword) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 7 with DGetAlias

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

the class ImportKeyPairAction method importKeyPairPvk.

private void importKeyPairPvk() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        DImportKeyPairPvk dImportKeyPairPvk = new DImportKeyPairPvk(frame);
        dImportKeyPairPvk.setLocationRelativeTo(frame);
        dImportKeyPairPvk.setVisible(true);
        PrivateKey privateKey = dImportKeyPairPvk.getPrivateKey();
        Certificate[] certs = dImportKeyPairPvk.getCertificateChain();
        if ((privateKey == null) || (certs == null)) {
            return;
        }
        X509Certificate[] x509Certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(x509Certs[0]));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("ImportKeyPairAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("ImportKeyPairAction.NewKeyPairEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }
        Password password = new Password((char[]) null);
        KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
        if (type.hasEntryPasswords()) {
            DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("ImportKeyPairAction.NewKeyPairEntryPassword.Title"), applicationSettings.getPasswordQualityConfig());
            dGetNewPassword.setLocationRelativeTo(frame);
            dGetNewPassword.setVisible(true);
            password = dGetNewPassword.getPassword();
            if (password == null) {
                return;
            }
        }
        if (keyStore.containsAlias(alias)) {
            keyStore.deleteEntry(alias);
            newState.removeEntryPassword(alias);
        }
        keyStore.setKeyEntry(alias, privateKey, password.toCharArray(), x509Certs);
        newState.setEntryPassword(alias, password);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ImportKeyPairAction.KeyPairImportSuccessful.message"), res.getString("ImportKeyPairAction.ImportKeyPair.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) DImportKeyPairPvk(org.kse.gui.dialogs.importexport.DImportKeyPairPvk) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DGetNewPassword(org.kse.gui.password.DGetNewPassword) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) DGetNewPassword(org.kse.gui.password.DGetNewPassword) Password(org.kse.crypto.Password)

Example 8 with DGetAlias

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

the class ImportTrustedCertificateAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        // handle case that no keystore is currently opened (-> create new keystore)
        if (history == null) {
            new NewAction(kseFrame).actionPerformed(null);
            history = kseFrame.getActiveKeyStoreHistory();
            // cancel pressed => abort
            if (history == null) {
                return;
            }
        }
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        // use either cert that was passed to c-tor or the one from file selection dialog
        X509Certificate trustCert = null;
        if (trustCertFromConstructor == null) {
            trustCert = showFileSelectionDialog();
            if (trustCert == null) {
                return;
            }
        } else {
            trustCert = trustCertFromConstructor;
        }
        if (applicationSettings.getEnableImportTrustedCertTrustCheck()) {
            String matchAlias = X509CertUtil.matchCertificate(keyStore, trustCert);
            if (matchAlias != null) {
                int selected = JOptionPane.showConfirmDialog(frame, MessageFormat.format(res.getString("ImportTrustedCertificateAction.TrustCertExistsConfirm.message"), matchAlias), res.getString("ImportTrustedCertificateAction.ImportTrustCert.Title"), JOptionPane.YES_NO_OPTION);
                if (selected != JOptionPane.YES_OPTION) {
                    return;
                }
            }
            KeyStore caCertificates = getCaCertificates();
            KeyStore windowsTrustedRootCertificates = getWindowsTrustedRootCertificates();
            // Establish against current KeyStore
            ArrayList<KeyStore> compKeyStores = new ArrayList<KeyStore>();
            compKeyStores.add(keyStore);
            if (caCertificates != null) {
                // Establish trust against CA Certificates KeyStore
                compKeyStores.add(caCertificates);
            }
            if (windowsTrustedRootCertificates != null) {
                // Establish trust against Windows Trusted Root Certificates KeyStore
                compKeyStores.add(windowsTrustedRootCertificates);
            }
            // Can we establish trust for the certificate?
            if (X509CertUtil.establishTrust(trustCert, compKeyStores.toArray(new KeyStore[compKeyStores.size()])) == null) {
                // there is no need to present it again to the user
                if (certFile != null) {
                    // display the certificate to the user for confirmation
                    JOptionPane.showMessageDialog(frame, res.getString("ImportTrustedCertificateAction.NoTrustPathCertConfirm.message"), res.getString("ImportTrustedCertificateAction.ImportTrustCert.Title"), JOptionPane.INFORMATION_MESSAGE);
                    DViewCertificate dViewCertificate = new DViewCertificate(frame, MessageFormat.format(res.getString("ImportTrustedCertificateAction.CertDetailsFile.Title"), certFile.getName()), new X509Certificate[] { trustCert }, null, DViewCertificate.NONE);
                    dViewCertificate.setLocationRelativeTo(frame);
                    dViewCertificate.setVisible(true);
                }
                int selected = JOptionPane.showConfirmDialog(frame, res.getString("ImportTrustedCertificateAction.AcceptTrustCert.message"), res.getString("ImportTrustedCertificateAction.ImportTrustCert.Title"), JOptionPane.YES_NO_OPTION);
                if (selected != JOptionPane.YES_OPTION) {
                    return;
                }
            }
        }
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("ImportTrustedCertificateAction.TrustCertEntryAlias.Title"), X509CertUtil.getCertificateAlias(trustCert));
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String alias = dGetAlias.getAlias();
        if (alias == null) {
            return;
        }
        if (keyStore.containsAlias(alias)) {
            String message = MessageFormat.format(res.getString("ImportTrustedCertificateAction.OverWriteEntry.message"), alias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("ImportTrustedCertificateAction.ImportTrustCert.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
            keyStore.deleteEntry(alias);
            newState.removeEntryPassword(alias);
        }
        keyStore.setCertificateEntry(alias, trustCert);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ImportTrustedCertificateAction.ImportTrustCertSuccessful.message"), res.getString("ImportTrustedCertificateAction.ImportTrustCert.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) DViewCertificate(org.kse.gui.dialogs.DViewCertificate) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 9 with DGetAlias

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

the class RenameKeyPairAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        Key privateKey = keyStore.getKey(alias, password.toCharArray());
        Certificate[] certs = keyStore.getCertificateChain(alias);
        certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("RenameKeyPairAction.NewEntryAlias.Title"), alias);
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String newAlias = dGetAlias.getAlias();
        if (newAlias == null) {
            return;
        }
        if (newAlias.equalsIgnoreCase(alias)) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("RenameKeyPairAction.RenameAliasIdentical.message"), alias), res.getString("RenameKeyPairAction.RenameEntry.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (keyStore.containsAlias(newAlias)) {
            String message = MessageFormat.format(res.getString("RenameKeyPairAction.OverWriteEntry.message"), newAlias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("RenameKeyPairAction.RenameEntry.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
            keyStore.deleteEntry(newAlias);
            newState.removeEntryPassword(newAlias);
        }
        keyStore.setKeyEntry(newAlias, privateKey, password.toCharArray(), certs);
        newState.setEntryPassword(newAlias, new Password(password));
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        currentState.append(newState);
        kseFrame.updateControls(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) Key(java.security.Key) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Example 10 with DGetAlias

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

the class RenameTrustedCertificateAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        String alias = kseFrame.getSelectedEntryAlias();
        DGetAlias dGetAlias = new DGetAlias(frame, res.getString("RenameTrustedCertificateAction.NewEntryAlias.Title"), alias);
        dGetAlias.setLocationRelativeTo(frame);
        dGetAlias.setVisible(true);
        String newAlias = dGetAlias.getAlias();
        if (newAlias == null) {
            return;
        }
        if (newAlias.equalsIgnoreCase(alias)) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("RenameTrustedCertificateAction.RenameAliasIdentical.message"), alias), res.getString("RenameTrustedCertificateAction.RenameEntry.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (keyStore.containsAlias(newAlias)) {
            String message = MessageFormat.format(res.getString("RenameTrustedCertificateAction.OverWriteEntry.message"), newAlias);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("RenameTrustedCertificateAction.RenameEntry.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
            keyStore.deleteEntry(newAlias);
            newState.removeEntryPassword(newAlias);
        }
        Certificate cert = keyStore.getCertificate(alias);
        keyStore.setCertificateEntry(newAlias, cert);
        keyStore.deleteEntry(alias);
        currentState.append(newState);
        kseFrame.updateControls(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) Certificate(java.security.cert.Certificate)

Aggregations

KeyStore (java.security.KeyStore)10 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)10 KeyStoreState (org.kse.utilities.history.KeyStoreState)10 Password (org.kse.crypto.Password)8 X509Certificate (java.security.cert.X509Certificate)6 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)6 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 Certificate (java.security.cert.Certificate)5 PrivateKey (java.security.PrivateKey)4 Key (java.security.Key)2 KeyPair (java.security.KeyPair)1 Provider (java.security.Provider)1 ArrayList (java.util.ArrayList)1 SecretKey (javax.crypto.SecretKey)1 KeyPairType (org.kse.crypto.keypair.KeyPairType)1 SecretKeyType (org.kse.crypto.secretkey.SecretKeyType)1 DGenerateKeyPair (org.kse.gui.dialogs.DGenerateKeyPair)1 DGenerateKeyPairCert (org.kse.gui.dialogs.DGenerateKeyPairCert)1 DGenerateSecretKey (org.kse.gui.dialogs.DGenerateSecretKey)1