Search in sources :

Example 41 with Password

use of org.kse.crypto.Password in project keystore-explorer by kaikramer.

the class UnlockKeyAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
    KeyStoreState currentState = history.getCurrentState();
    String alias = kseFrame.getSelectedEntryAlias();
    Password password = currentState.getEntryPassword(alias);
    if (password != null) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("UnlockKeyAction.KeyAlreadyUnlocked.message"), alias), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    unlockEntry(alias, currentState);
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) Password(org.kse.crypto.Password)

Example 42 with Password

use of org.kse.crypto.Password in project keystore-explorer by kaikramer.

the class UnlockKeyPairAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
    KeyStoreState currentState = history.getCurrentState();
    String alias = kseFrame.getSelectedEntryAlias();
    Password password = currentState.getEntryPassword(alias);
    if (password != null) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("UnlockKeyPairAction.KeyPairAlreadyUnlocked.message"), alias), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    unlockEntry(alias, currentState);
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) Password(org.kse.crypto.Password)

Example 43 with Password

use of org.kse.crypto.Password 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 44 with Password

use of org.kse.crypto.Password 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 45 with Password

use of org.kse.crypto.Password in project keystore-explorer by kaikramer.

the class KeyStoreExplorerAction method unlockEntry.

/**
 * Unlock a key or key pair entry. Updates the KeyStore history with the
 * password.
 *
 * @param alias
 *            Entry's alias
 * @param state
 *            KeyStore state
 * @return Key pair password if successful, null otherwise
 */
protected Password unlockEntry(String alias, KeyStoreState state) {
    try {
        KeyStore keyStore = state.getKeyStore();
        DGetPassword dGetPassword = new DGetPassword(frame, MessageFormat.format(res.getString("KeyStoreExplorerAction.UnlockEntry.Title"), alias));
        dGetPassword.setLocationRelativeTo(frame);
        dGetPassword.setVisible(true);
        Password password = dGetPassword.getPassword();
        if (password == null) {
            return null;
        }
        // Test password is correct
        keyStore.getKey(alias, password.toCharArray());
        state.setEntryPassword(alias, password);
        kseFrame.updateControls(true);
        return password;
    } catch (GeneralSecurityException ex) {
        String problemStr = MessageFormat.format(res.getString("KeyStoreExplorerAction.NoUnlockEntry.Problem"), alias);
        String[] causes = new String[] { res.getString("KeyStoreExplorerAction.PasswordIncorrectEntry.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("KeyStoreExplorerAction.ProblemUnlockingEntry.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : DGetPassword(org.kse.gui.password.DGetPassword) GeneralSecurityException(java.security.GeneralSecurityException) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) KeyStore(java.security.KeyStore) DProblem(org.kse.gui.error.DProblem) DGetNewPassword(org.kse.gui.password.DGetNewPassword) DGetPassword(org.kse.gui.password.DGetPassword) Password(org.kse.crypto.Password)

Aggregations

Password (org.kse.crypto.Password)60 KeyStore (java.security.KeyStore)35 KeyStoreState (org.kse.utilities.history.KeyStoreState)32 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)31 PrivateKey (java.security.PrivateKey)24 File (java.io.File)23 FileNotFoundException (java.io.FileNotFoundException)16 Key (java.security.Key)15 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)12 CryptoException (org.kse.crypto.CryptoException)9 DProblem (org.kse.gui.error.DProblem)9 Problem (org.kse.gui.error.Problem)9 DGetAlias (org.kse.gui.dialogs.DGetAlias)8 DGetNewPassword (org.kse.gui.password.DGetNewPassword)8 JPasswordField (javax.swing.JPasswordField)6 DViewPrivateKey (org.kse.gui.dialogs.DViewPrivateKey)6 DGetPassword (org.kse.gui.password.DGetPassword)6 FileInputStream (java.io.FileInputStream)5