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);
}
}
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;
}
}
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);
}
}
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);
}
}
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;
}
Aggregations