use of org.kse.utilities.buffer.TrustedCertificateBufferEntry in project keystore-explorer by kaikramer.
the class CopyAction method bufferSelectedEntry.
private BufferEntry bufferSelectedEntry() {
try {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
String alias = kseFrame.getSelectedEntryAlias();
if (alias == null) {
return null;
}
BufferEntry bufferEntry = null;
KeyStore keyStore = currentState.getKeyStore();
if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
Password password = getEntryPassword(alias, currentState);
if (password == null) {
return null;
}
Key key = keyStore.getKey(alias, password.toCharArray());
if (key instanceof PrivateKey) {
JOptionPane.showMessageDialog(frame, res.getString("CopyAction.NoCopyKeyEntryWithPrivateKey.message"), res.getString("CopyAction.Copy.Title"), JOptionPane.WARNING_MESSAGE);
return null;
}
bufferEntry = new KeyBufferEntry(alias, false, key, password);
} else if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
Certificate certificate = keyStore.getCertificate(alias);
bufferEntry = new TrustedCertificateBufferEntry(alias, false, certificate);
} else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
Password password = getEntryPassword(alias, currentState);
if (password == null) {
return null;
}
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
Certificate[] certificateChain = keyStore.getCertificateChain(alias);
bufferEntry = new KeyPairBufferEntry(alias, false, privateKey, password, certificateChain);
}
return bufferEntry;
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}
use of org.kse.utilities.buffer.TrustedCertificateBufferEntry in project keystore-explorer by kaikramer.
the class CutAction method bufferSelectedEntry.
private BufferEntry bufferSelectedEntry() {
try {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
String alias = kseFrame.getSelectedEntryAlias();
if (alias == null) {
return null;
}
BufferEntry bufferEntry = null;
KeyStore keyStore = currentState.getKeyStore();
if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
Password password = getEntryPassword(alias, currentState);
if (password == null) {
return null;
}
Key key = keyStore.getKey(alias, password.toCharArray());
if (key instanceof PrivateKey) {
JOptionPane.showMessageDialog(frame, res.getString("CutAction.NoCutKeyEntryWithPrivateKey.message"), res.getString("CutAction.Cut.Title"), JOptionPane.WARNING_MESSAGE);
return null;
}
bufferEntry = new KeyBufferEntry(alias, true, key, password);
} else if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
Certificate certificate = keyStore.getCertificate(alias);
bufferEntry = new TrustedCertificateBufferEntry(alias, true, certificate);
} else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
Password password = getEntryPassword(alias, currentState);
if (password == null) {
return null;
}
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
Certificate[] certificateChain = keyStore.getCertificateChain(alias);
bufferEntry = new KeyPairBufferEntry(alias, true, privateKey, password, certificateChain);
}
KeyStoreState newState = currentState.createBasisForNextState(this);
keyStore = newState.getKeyStore();
keyStore.deleteEntry(alias);
newState.removeEntryPassword(alias);
currentState.append(newState);
return bufferEntry;
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}
use of org.kse.utilities.buffer.TrustedCertificateBufferEntry 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;
}
}
Aggregations