Search in sources :

Example 21 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState in project keystore-explorer by kaikramer.

the class SaveAllAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    try {
        KeyStoreHistory[] histories = kseFrame.getKeyStoreHistories();
        for (int i = 0; i < histories.length; i++) {
            KeyStoreHistory history = histories[i];
            KeyStoreState currentState = history.getCurrentState();
            // Does KeyStore require saving and has file been saved before?
            if (!currentState.isSavedState()) {
                if (!saveKeyStore(history)) {
                    break;
                }
            }
        }
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory)

Example 22 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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 23 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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 24 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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)

Example 25 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState in project keystore-explorer by kaikramer.

the class SetPasswordAction method setKeyStorePassword.

/**
 * Set the active KeyStore's password.
 *
 * @return True if successful
 * @throws CryptoException
 *             If problem occurred
 */
protected boolean setKeyStorePassword() throws CryptoException {
    KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
    KeyStoreState currentState = history.getCurrentState();
    KeyStoreState newState = currentState.createBasisForNextState(this);
    Password password = getNewKeyStorePassword();
    if (password == null) {
        return false;
    }
    newState.setPassword(password);
    currentState.append(newState);
    kseFrame.updateControls(true);
    return true;
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) Password(org.kse.crypto.Password)

Aggregations

KeyStoreState (org.kse.utilities.history.KeyStoreState)48 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)41 KeyStore (java.security.KeyStore)39 Password (org.kse.crypto.Password)32 PrivateKey (java.security.PrivateKey)17 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)13 Key (java.security.Key)12 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 File (java.io.File)9 FileNotFoundException (java.io.FileNotFoundException)7 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 PublicKey (java.security.PublicKey)5 CryptoException (org.kse.crypto.CryptoException)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyStoreException (java.security.KeyStoreException)4 Provider (java.security.Provider)4 ArrayList (java.util.ArrayList)3 KeyPairType (org.kse.crypto.keypair.KeyPairType)3