use of org.kse.crypto.Password in project keystore-explorer by kaikramer.
the class OpenAction method openKeyStore.
/**
* Open the supplied KeyStore file from disk.
*
* @param keyStoreFile
* The KeyStore file
*/
public void openKeyStore(File keyStoreFile, String defaultPassword) {
try {
if (!keyStoreFile.isFile()) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("OpenAction.NotFile.message"), keyStoreFile), res.getString("OpenAction.OpenKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
return;
}
if (isKeyStoreFileOpen(keyStoreFile)) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("OpenAction.NoOpenKeyStoreAlreadyOpen.message"), keyStoreFile), res.getString("OpenAction.OpenKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
return;
}
// use (optional) default password for first try
Password password = (defaultPassword != null) ? new Password(defaultPassword.toCharArray()) : null;
KeyStore openedKeyStore = null;
boolean firstTry = true;
while (true) {
// show password dialog if no default password was passed or if last try to unlock ks has failed
if (password == null) {
password = showPasswordDialog(keyStoreFile);
}
// user did not enter password -> abort
if (password == null) {
return;
}
// try to load keystore
try {
openedKeyStore = KeyStoreUtil.load(keyStoreFile, password);
break;
} catch (KeyStoreLoadException klex) {
// show error message only after first try with default password or if no default password set
if (defaultPassword == null || !firstTry) {
int tryAgainChoice = showErrorMessage(keyStoreFile, klex);
if (tryAgainChoice == JOptionPane.NO_OPTION) {
return;
}
}
}
// failure, reset password
password.nullPassword();
password = null;
firstTry = false;
}
if (openedKeyStore == null) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("OpenAction.FileNotRecognisedType.message"), keyStoreFile.getName()), res.getString("OpenAction.OpenKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
return;
}
kseFrame.addKeyStore(openedKeyStore, keyStoreFile, password);
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("OpenAction.NoReadFile.message"), keyStoreFile), res.getString("OpenAction.OpenKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
} catch (Exception ex) {
DError.displayError(frame, ex);
}
}
use of org.kse.crypto.Password in project keystore-explorer by kaikramer.
the class OpenDefaultAction method doAction.
/**
* Do action.
*/
@Override
protected void doAction() {
File defaultKeyStoreFile = new File(System.getProperty("user.home"), ".keystore");
if (defaultKeyStoreFile.isFile()) {
openKeyStore(defaultKeyStoreFile);
return;
}
int selected = JOptionPane.showConfirmDialog(frame, res.getString("OpenDefaultAction.NoDefaultKeyStoreCreate.message"), res.getString("OpenDefaultAction.OpenDefaultKeyStore.Title"), JOptionPane.YES_NO_OPTION);
if (selected != JOptionPane.YES_OPTION) {
return;
}
try {
DNewKeyStoreType dNewKeyStoreType = new DNewKeyStoreType(frame);
dNewKeyStoreType.setLocationRelativeTo(frame);
dNewKeyStoreType.setVisible(true);
KeyStoreType keyStoreType = dNewKeyStoreType.getKeyStoreType();
if (keyStoreType == null) {
return;
}
Password password = getNewKeyStorePassword();
if (password == null) {
return;
}
KeyStore defaultKeyStore = KeyStoreUtil.create(keyStoreType);
KeyStoreUtil.save(defaultKeyStore, defaultKeyStoreFile, password);
kseFrame.addKeyStore(defaultKeyStore, defaultKeyStoreFile, password);
} catch (Exception ex) {
DError.displayError(frame, ex);
}
}
use of org.kse.crypto.Password in project keystore-explorer by kaikramer.
the class PasteAction method pasteEntry.
private boolean pasteEntry(BufferEntry bufferEntry) {
try {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
KeyStoreState newState = currentState.createBasisForNextState(this);
KeyStore keyStore = newState.getKeyStore();
String alias = bufferEntry.getName();
if (keyStore.containsAlias(alias)) {
if (bufferEntry.isCut()) {
int selected = JOptionPane.showConfirmDialog(frame, MessageFormat.format(res.getString("PasteAction.PasteExistsReplace.message"), alias), res.getString("PasteAction.Paste.Title"), JOptionPane.YES_NO_OPTION);
if (selected != JOptionPane.YES_OPTION) {
return false;
}
keyStore.deleteEntry(alias);
newState.removeEntryPassword(alias);
} else {
alias = getUniqueEntryName(alias, keyStore);
}
}
if (bufferEntry instanceof KeyBufferEntry) {
KeyStoreType keyStoreType = KeyStoreType.resolveJce(keyStore.getType());
if (!keyStoreType.supportsKeyEntries()) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("PasteAction.NoPasteKeyEntry.message"), keyStoreType.friendly()), res.getString("PasteAction.Paste.Title"), JOptionPane.WARNING_MESSAGE);
return false;
}
KeyBufferEntry keyBufferEntry = (KeyBufferEntry) bufferEntry;
Key key = keyBufferEntry.getKey();
Password password = keyBufferEntry.getPassword();
keyStore.setKeyEntry(alias, key, password.toCharArray(), null);
newState.setEntryPassword(alias, password);
} else if (bufferEntry instanceof KeyPairBufferEntry) {
KeyPairBufferEntry keyPairBufferEntry = (KeyPairBufferEntry) bufferEntry;
PrivateKey privateKey = keyPairBufferEntry.getPrivateKey();
Password password = keyPairBufferEntry.getPassword();
Certificate[] certificateChain = keyPairBufferEntry.getCertificateChain();
keyStore.setKeyEntry(alias, privateKey, password.toCharArray(), certificateChain);
newState.setEntryPassword(alias, password);
} else {
TrustedCertificateBufferEntry certBufferEntry = (TrustedCertificateBufferEntry) bufferEntry;
keyStore.setCertificateEntry(alias, certBufferEntry.getTrustedCertificate());
}
if (bufferEntry.isCut()) {
Buffer.clear();
}
currentState.append(newState);
kseFrame.updateControls(true);
return true;
} catch (Exception ex) {
DError.displayError(frame, ex);
return false;
}
}
use of org.kse.crypto.Password in project keystore-explorer by kaikramer.
the class RemoveFromCertificateChainAction 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 privKey = keyStore.getKey(alias, password.toCharArray());
X509Certificate[] certChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias)));
if (certChain.length == 1) {
JOptionPane.showMessageDialog(frame, res.getString("RemoveFromCertificateChainAction.CannotRemoveOnlyCert.message"), res.getString("RemoveFromCertificateChainAction.RemoveFromCertificateChain.Title"), JOptionPane.WARNING_MESSAGE);
return;
}
// Certificate to remove is the end one in the chain
X509Certificate[] newCertChain = new X509Certificate[certChain.length - 1];
System.arraycopy(certChain, 0, newCertChain, 0, newCertChain.length);
keyStore.deleteEntry(alias);
keyStore.setKeyEntry(alias, privKey, password.toCharArray(), newCertChain);
currentState.append(newState);
kseFrame.updateControls(true);
JOptionPane.showMessageDialog(frame, res.getString("RemoveFromCertificateChainAction.RemoveFromCertificateChainSuccessful.message"), res.getString("RemoveFromCertificateChainAction.RemoveFromCertificateChain.Title"), JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
DError.displayError(frame, ex);
}
}
use of org.kse.crypto.Password in project keystore-explorer by kaikramer.
the class RenameKeyAction 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 key = keyStore.getKey(alias, password.toCharArray());
DGetAlias dGetAlias = new DGetAlias(frame, res.getString("RenameKeyAction.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("RenameKeyAction.RenameAliasIdentical.message"), alias), res.getString("RenameKeyAction.RenameEntry.Title"), JOptionPane.WARNING_MESSAGE);
return;
}
if (keyStore.containsAlias(newAlias)) {
String message = MessageFormat.format(res.getString("RenameKeyAction.OverWriteEntry.message"), newAlias);
int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("RenameKeyAction.RenameEntry.Title"), JOptionPane.YES_NO_OPTION);
if (selected != JOptionPane.YES_OPTION) {
return;
}
keyStore.deleteEntry(newAlias);
newState.removeEntryPassword(newAlias);
}
keyStore.setKeyEntry(newAlias, key, password.toCharArray(), null);
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);
}
}
Aggregations