Search in sources :

Example 16 with Password

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

the class KeyPairPrivateKeyDetailsAction 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;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        DViewPrivateKey dViewPrivateKey = new DViewPrivateKey(frame, MessageFormat.format(res.getString("KeyPairPrivateKeyDetailsAction.PrivKeyDetailsEntry.Title"), alias), privKey, history.getExplicitProvider());
        dViewPrivateKey.setLocationRelativeTo(frame);
        dViewPrivateKey.setVisible(true);
    } 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) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) KeyStore(java.security.KeyStore) Password(org.kse.crypto.Password)

Example 17 with Password

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

the class SaveAction method saveKeyStore.

/**
 * Save the supplied KeyStore back to the file it was originally opened
 * from.
 *
 * @param history
 *            KeyStore history
 * @return True if the KeyStore is saved to disk, false otherwise
 */
protected boolean saveKeyStore(KeyStoreHistory history) {
    File saveFile = null;
    try {
        KeyStoreState currentState = history.getCurrentState();
        kseFrame.focusOnKeyStore(currentState.getKeyStore());
        saveFile = history.getFile();
        if (saveFile == null) {
            return saveKeyStoreAs(history);
        }
        Password password = currentState.getPassword();
        if (password == null) {
            SetPasswordAction setPasswordAction = new SetPasswordAction(kseFrame);
            if (setPasswordAction.setKeyStorePassword()) {
                currentState = history.getCurrentState();
                password = currentState.getPassword();
            } else {
                return false;
            }
        }
        KeyStoreUtil.save(currentState.getKeyStore(), saveFile, password);
        currentState.setPassword(password);
        currentState.setAsSavedState();
        kseFrame.updateControls(false);
        return true;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SaveAction.NoWriteFile.message"), saveFile), res.getString("SaveAction.SaveKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
        return false;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return false;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Password(org.kse.crypto.Password)

Example 18 with Password

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

the class SaveAsAction method saveKeyStoreAs.

/**
 * Save the supplied opened KeyStore to disk to what may be a different file
 * from the one it was opened from (if any).
 *
 * @param history
 *            KeyStore history
 * @return True if the KeyStore is saved to disk, false otherwise
 */
protected boolean saveKeyStoreAs(KeyStoreHistory history) {
    File saveFile = null;
    try {
        KeyStoreState currentState = history.getCurrentState();
        Password password = currentState.getPassword();
        if (password == null) {
            SetPasswordAction setPasswordAction = new SetPasswordAction(kseFrame);
            if (setPasswordAction.setKeyStorePassword()) {
                currentState = history.getCurrentState();
                password = currentState.getPassword();
            } else {
                return false;
            }
        }
        JFileChooser chooser = FileChooserFactory.getKeyStoreFileChooser();
        chooser.setCurrentDirectory(CurrentDirectory.get());
        chooser.setDialogTitle(res.getString("SaveAsAction.SaveKeyStoreAs.Title"));
        chooser.setMultiSelectionEnabled(false);
        int rtnValue = chooser.showSaveDialog(frame);
        if (rtnValue != JFileChooser.APPROVE_OPTION) {
            return false;
        }
        saveFile = chooser.getSelectedFile();
        CurrentDirectory.updateForFile(saveFile);
        if (saveFile.isFile()) {
            String message = MessageFormat.format(res.getString("SaveAsAction.OverWriteFile.message"), saveFile);
            int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("SaveAsAction.SaveKeyStoreAs.Title"), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return false;
            }
        }
        if (isKeyStoreFileOpen(saveFile)) {
            JOptionPane.showMessageDialog(frame, res.getString("SaveAsAction.NoSaveKeyStoreAlreadyOpen.message"), res.getString("SaveAsAction.SaveKeyStoreAs.Title"), JOptionPane.WARNING_MESSAGE);
            return false;
        }
        KeyStoreUtil.save(currentState.getKeyStore(), saveFile, password);
        currentState.setPassword(password);
        history.setFile(saveFile);
        currentState.setAsSavedState();
        kseFrame.updateControls(false);
        kseFrame.addRecentFile(saveFile);
        return true;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SaveAsAction.NoWriteFile.message"), saveFile), res.getString("SaveAsAction.SaveKeyStoreAs.Title"), JOptionPane.WARNING_MESSAGE);
        return false;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return false;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) JFileChooser(javax.swing.JFileChooser) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Password(org.kse.crypto.Password)

Example 19 with Password

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

the class SetKeyPairPasswordAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    String alias = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        alias = kseFrame.getSelectedEntryAlias();
        Password oldPassword = newState.getEntryPassword(alias);
        DChangePassword dChangePassword = new DChangePassword(frame, DOCUMENT_MODAL, res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), oldPassword, applicationSettings.getPasswordQualityConfig());
        dChangePassword.setLocationRelativeTo(frame);
        dChangePassword.setVisible(true);
        if (oldPassword == null) {
            oldPassword = dChangePassword.getOldPassword();
        }
        Password newPassword = dChangePassword.getNewPassword();
        if ((oldPassword == null) || (newPassword == null)) {
            return;
        }
        // Change the password by recreating the entry
        Certificate[] certs = keyStore.getCertificateChain(alias);
        certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        Key privateKey = keyStore.getKey(alias, oldPassword.toCharArray());
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        keyStore.setKeyEntry(alias, privateKey, newPassword.toCharArray(), certs);
        if (currentState.getEntryPassword(alias) == null) {
            currentState.setEntryPassword(alias, oldPassword);
        }
        newState.setEntryPassword(alias, newPassword);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("SetKeyPairPasswordAction.SetKeyPairPasswordSuccessful.message"), res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (GeneralSecurityException ex) {
        String problemStr = MessageFormat.format(res.getString("SetKeyPairPasswordAction.NoSetPasswordKeyPairEntry.Problem"), alias);
        String[] causes = new String[] { res.getString("SetKeyPairPasswordAction.PasswordIncorrectKeyPairEntry.Cause"), res.getString("SetKeyPairPasswordAction.NotSupportedAlgorithmKeyPairEntry.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("SetKeyPairPasswordAction.ProblemSettingPasswordKeyPairEntry.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) GeneralSecurityException(java.security.GeneralSecurityException) KeyStore(java.security.KeyStore) DProblem(org.kse.gui.error.DProblem) GeneralSecurityException(java.security.GeneralSecurityException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) DChangePassword(org.kse.gui.password.DChangePassword) Key(java.security.Key) DChangePassword(org.kse.gui.password.DChangePassword) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

Example 20 with Password

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

the class SetKeyPasswordAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    String alias = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        alias = kseFrame.getSelectedEntryAlias();
        Password oldPassword = newState.getEntryPassword(alias);
        DChangePassword dChangePassword = new DChangePassword(frame, DOCUMENT_MODAL, res.getString("SetKeyPasswordAction.SetKeyPassword.Title"), oldPassword, applicationSettings.getPasswordQualityConfig());
        dChangePassword.setLocationRelativeTo(frame);
        dChangePassword.setVisible(true);
        if (oldPassword == null) {
            oldPassword = dChangePassword.getOldPassword();
        }
        Password newPassword = dChangePassword.getNewPassword();
        if ((oldPassword == null) || (newPassword == null)) {
            return;
        }
        // Change the password by recreating the entry
        Key key = keyStore.getKey(alias, oldPassword.toCharArray());
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        keyStore.setKeyEntry(alias, key, newPassword.toCharArray(), null);
        if (currentState.getEntryPassword(alias) == null) {
            currentState.setEntryPassword(alias, oldPassword);
        }
        newState.setEntryPassword(alias, newPassword);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("SetKeyPasswordAction.SetKeyPasswordSuccessful.message"), res.getString("SetKeyPasswordAction.SetKeyPassword.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (GeneralSecurityException ex) {
        String problemStr = MessageFormat.format(res.getString("SetKeyPasswordAction.NoSetPasswordKeyEntry.Problem"), alias);
        String[] causes = new String[] { res.getString("SetKeyPasswordAction.PasswordIncorrectKeyEntry.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("SetKeyPasswordAction.ProblemSettingPasswordKeyEntry.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) GeneralSecurityException(java.security.GeneralSecurityException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) DChangePassword(org.kse.gui.password.DChangePassword) KeyStore(java.security.KeyStore) Key(java.security.Key) DProblem(org.kse.gui.error.DProblem) GeneralSecurityException(java.security.GeneralSecurityException) DChangePassword(org.kse.gui.password.DChangePassword) 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