use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.
the class KseFrame method dragSelectedEntry.
/**
* Get a the selected entry as a drag entry for DnD.
*
* @return Drag entry or null if entry could not be dragged
*/
public DragEntry dragSelectedEntry() {
try {
KeyStoreHistory history = getActiveKeyStoreHistory();
if (history == null) {
// No KeyStore to drag from
return null;
}
KeyStoreState currentState = history.getCurrentState();
KeyStore keyStore = currentState.getKeyStore();
String alias = getSelectedEntryAlias();
KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
if (alias == null) {
// No selected entry to drag
return null;
}
if (KeyStoreUtil.isKeyEntry(alias, keyStore)) {
JOptionPane.showMessageDialog(frame, res.getString("KseFrame.NoDragKeyEntry.message"), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
return null;
}
if (KeyStoreUtil.isKeyPairEntry(alias, keyStore) && type.hasExportablePrivateKeys()) {
// Otherwise entry must already be unlocked to get password
Password password = currentState.getEntryPassword(alias);
if (password == null && type.hasEntryPasswords()) {
JOptionPane.showMessageDialog(frame, res.getString("KseFrame.NoDragLockedKeyPairEntry.message"), KSE.getApplicationName(), JOptionPane.WARNING_MESSAGE);
return null;
}
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
Certificate[] certificateChain = keyStore.getCertificateChain(alias);
return new DragKeyPairEntry(alias, privateKey, password, certificateChain);
} else {
Certificate trustedCertificate = keyStore.getCertificate(alias);
return new DragTrustedCertificateEntry(alias, trustedCertificate);
}
} catch (Exception ex) {
DError.displayError(frame, ex);
return null;
}
}
use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.
the class KseFrame method updateControls.
/**
* Update the frame's controls dependent on the state of its open and active
* KeyStores.
*
* @param keyStoreContentsChanged
* Have the active KeyStore's contents changed?
*/
public void updateControls(boolean keyStoreContentsChanged) {
KeyStoreHistory history = getActiveKeyStoreHistory();
if (history == null) {
updateControlsNoKeyStoresOpen();
return;
}
KeyStoreState currentState = history.getCurrentState();
// Reload KeyStore in table if it has changed
if (keyStoreContentsChanged) {
try {
String selectedAlias = getSelectedEntryAlias();
((KeyStoreTableModel) getActiveKeyStoreTable().getModel()).load(history);
// Loading the model loses the selected entry - preserve it
if (selectedAlias != null) {
setSelectedEntryByAlias(selectedAlias);
}
} catch (GeneralSecurityException ex) {
DError.displayError(frame, ex);
} catch (CryptoException ex) {
DError.displayError(frame, ex);
}
}
// Can save if active KeyStore has not been saved
if (!currentState.isSavedState()) {
saveAction.setEnabled(true);
} else {
saveAction.setEnabled(false);
}
// Can save all if any KeyStore has been changed since saved
boolean saveAll = false;
for (int i = 0; i < histories.size(); i++) {
KeyStoreHistory h = histories.get(i);
if (!h.getCurrentState().isSavedState()) {
// Yes - can Save All
saveAll = true;
break;
}
}
saveAllAction.setEnabled(saveAll);
// Can close
closeAction.setEnabled(true);
closeAllAction.setEnabled(true);
// Can close others?
if (jkstpKeyStores.getTabCount() > 1) {
closeOthersAction.setEnabled(true);
} else {
closeOthersAction.setEnabled(false);
}
KeyStore keyStore = currentState.getKeyStore();
KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
// Can Save As
if (type.isFileBased()) {
saveAsAction.setEnabled(true);
}
// May be able to undo/redo
updateUndoRedoControls(currentState);
// May be able to cut/copy/paste
if (type.isFileBased()) {
updateCutCopyPasteControls();
}
// Can use tools on
generateKeyPairAction.setEnabled(true);
generateSecretKeyAction.setEnabled(type.supportsKeyEntries());
importTrustedCertificateAction.setEnabled(true);
importKeyPairAction.setEnabled(true);
propertiesAction.setEnabled(true);
if (type.isFileBased()) {
setPasswordAction.setEnabled(true);
}
// Show default status bar display
setDefaultStatusBarText();
// Passwords, and therefore unlocking, are not relevant for PKCS #12 or KeyStores that are not file-based
if (!type.hasEntryPasswords() || !type.isFileBased()) {
unlockKeyPairAction.setEnabled(false);
setKeyPairPasswordAction.setEnabled(false);
unlockKeyAction.setEnabled(false);
setKeyPasswordAction.setEnabled(false);
} else {
unlockKeyPairAction.setEnabled(true);
setKeyPairPasswordAction.setEnabled(true);
unlockKeyAction.setEnabled(true);
setKeyPasswordAction.setEnabled(true);
}
// Special restrictions for MSCAPI and PKCS#11 type
if (type == KeyStoreType.MS_CAPI_PERSONAL || type == KeyStoreType.PKCS11) {
keyPairPrivateKeyDetailsAction.setEnabled(false);
keyDetailsAction.setEnabled(false);
renameKeyAction.setEnabled(false);
renameKeyPairAction.setEnabled(false);
renameTrustedCertificateAction.setEnabled(false);
exportKeyPairAction.setEnabled(false);
exportKeyPairPrivateKeyAction.setEnabled(false);
jmKeyPairEditCertChain.setEnabled(false);
appendToCertificateChainAction.setEnabled(false);
removeFromCertificateChainAction.setEnabled(false);
// "UnsupportedOperationException" ...
jmKeyPairImportCaReply.setEnabled(false);
} else {
keyPairPrivateKeyDetailsAction.setEnabled(true);
keyDetailsAction.setEnabled(true);
renameKeyAction.setEnabled(true);
renameKeyPairAction.setEnabled(true);
renameTrustedCertificateAction.setEnabled(true);
exportKeyPairAction.setEnabled(true);
exportKeyPairPrivateKeyAction.setEnabled(true);
jmKeyPairEditCertChain.setEnabled(true);
appendToCertificateChainAction.setEnabled(true);
removeFromCertificateChainAction.setEnabled(true);
jmKeyPairImportCaReply.setEnabled(true);
}
// KeyStore type menu items
if (type.isFileBased()) {
jmChangeType.setEnabled(true);
if (type == JKS) {
jrbmiChangeTypeJks.setSelected(true);
jrbmiKeyStoreChangeTypeJks.setSelected(true);
} else if (type == JCEKS) {
jrbmiChangeTypeJceks.setSelected(true);
jrbmiKeyStoreChangeTypeJceks.setSelected(true);
} else if (type == PKCS12) {
jrbmiChangeTypePkcs12.setSelected(true);
jrbmiKeyStoreChangeTypePkcs12.setSelected(true);
} else if (type == BKS_V1) {
jrbmiChangeTypeBksV1.setSelected(true);
jrbmiKeyStoreChangeTypeBksV1.setSelected(true);
} else if (type == BKS) {
jrbmiChangeTypeBks.setSelected(true);
jrbmiKeyStoreChangeTypeBks.setSelected(true);
} else {
jrbmiChangeTypeUber.setSelected(true);
jrbmiKeyStoreChangeTypeUber.setSelected(true);
}
} else {
jmKeyStoreChangeType.setEnabled(false);
}
// Show KeyStores tabbed pane
frame.getContentPane().remove(jQuickStart);
frame.getContentPane().add(jkstpKeyStores, BorderLayout.CENTER);
updateKeyStoreTabsText();
updateApplicationTitle();
frame.repaint();
}
use of org.kse.utilities.history.KeyStoreHistory 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.history.KeyStoreHistory in project keystore-explorer by kaikramer.
the class GenerateCsrAction method doAction.
/**
* Do action.
*/
@Override
protected void doAction() {
File csrFile = null;
FileOutputStream fos = null;
try {
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
KeyStoreState currentState = history.getCurrentState();
Provider provider = history.getExplicitProvider();
String alias = kseFrame.getSelectedEntryAlias();
Password password = getEntryPassword(alias, currentState);
if (password == null) {
return;
}
KeyStore keyStore = currentState.getKeyStore();
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
String keyPairAlg = privateKey.getAlgorithm();
KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
if (keyPairType == null) {
throw new CryptoException(MessageFormat.format(res.getString("GenerateCsrAction.NoCsrForKeyPairAlg.message"), keyPairAlg));
}
// determine dir of current keystore as proposal for CSR file location
String path = CurrentDirectory.get().getAbsolutePath();
File keyStoreFile = history.getFile();
if (keyStoreFile != null) {
path = keyStoreFile.getAbsoluteFile().getParent();
}
DGenerateCsr dGenerateCsr = new DGenerateCsr(frame, alias, privateKey, keyPairType, path, provider);
dGenerateCsr.setLocationRelativeTo(frame);
dGenerateCsr.setVisible(true);
if (!dGenerateCsr.generateSelected()) {
return;
}
CsrType format = dGenerateCsr.getFormat();
SignatureType signatureType = dGenerateCsr.getSignatureType();
String challenge = dGenerateCsr.getChallenge();
String unstructuredName = dGenerateCsr.getUnstructuredName();
boolean useCertificateExtensions = dGenerateCsr.isAddExtensionsWanted();
csrFile = dGenerateCsr.getCsrFile();
X509Certificate firstCertInChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias)))[0];
fos = new FileOutputStream(csrFile);
if (format == CsrType.PKCS10) {
String csr = Pkcs10Util.getCsrEncodedDerPem(Pkcs10Util.generateCsr(firstCertInChain, privateKey, signatureType, challenge, unstructuredName, useCertificateExtensions, provider));
fos.write(csr.getBytes());
} else {
SpkacSubject subject = new SpkacSubject(X500NameUtils.x500PrincipalToX500Name(firstCertInChain.getSubjectX500Principal()));
PublicKey publicKey = firstCertInChain.getPublicKey();
// TODO handle other providers (PKCS11 etc)
Spkac spkac = new Spkac(challenge, signatureType, subject, publicKey, privateKey);
spkac.output(fos);
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("GenerateCsrAction.NoWriteFile.message"), csrFile), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.WARNING_MESSAGE);
return;
} catch (Exception ex) {
DError.displayError(frame, ex);
return;
} finally {
IOUtils.closeQuietly(fos);
}
JOptionPane.showMessageDialog(frame, res.getString("GenerateCsrAction.CsrGenerationSuccessful.message"), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.INFORMATION_MESSAGE);
}
use of org.kse.utilities.history.KeyStoreHistory in project keystore-explorer by kaikramer.
the class GenerateKeyPairAction method generateKeyPair.
/**
* Generate a key pair (with certificate) in the currently opened KeyStore.
*
* @param issuerCert
* Issuer certificate for signing the new certificate
* @param issuerCertChain
* Chain of issuer certificate
* @param issuerPrivateKey
* Issuer's private key for signing
* @return Alias of new key pair
*/
public String generateKeyPair(X509Certificate issuerCert, X509Certificate[] issuerCertChain, PrivateKey issuerPrivateKey) {
String alias = "";
try {
int keyPairSize = applicationSettings.getGenerateKeyPairSize();
KeyPairType keyPairType = applicationSettings.getGenerateKeyPairType();
KeyStore activeKeyStore = kseFrame.getActiveKeyStore();
KeyStoreType activeKeyStoreType = KeyStoreType.resolveJce(activeKeyStore.getType());
KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
Provider provider = history.getExplicitProvider();
DGenerateKeyPair dGenerateKeyPair = new DGenerateKeyPair(frame, activeKeyStoreType, keyPairType, keyPairSize);
dGenerateKeyPair.setLocationRelativeTo(frame);
dGenerateKeyPair.setVisible(true);
if (!dGenerateKeyPair.isSuccessful()) {
return "";
}
keyPairType = dGenerateKeyPair.getKeyPairType();
DGeneratingKeyPair dGeneratingKeyPair;
if (keyPairType != KeyPairType.EC) {
keyPairSize = dGenerateKeyPair.getKeyPairSize();
dGeneratingKeyPair = new DGeneratingKeyPair(frame, keyPairType, keyPairSize, provider);
applicationSettings.setGenerateKeyPairSize(keyPairSize);
applicationSettings.setGenerateKeyPairType(keyPairType);
} else {
String curveName = dGenerateKeyPair.getCurveName();
dGeneratingKeyPair = new DGeneratingKeyPair(frame, keyPairType, curveName, provider);
}
dGeneratingKeyPair.setLocationRelativeTo(frame);
dGeneratingKeyPair.startKeyPairGeneration();
dGeneratingKeyPair.setVisible(true);
KeyPair keyPair = dGeneratingKeyPair.getKeyPair();
if (keyPair == null) {
return "";
}
DGenerateKeyPairCert dGenerateKeyPairCert = new DGenerateKeyPairCert(frame, res.getString("GenerateKeyPairAction.GenerateKeyPairCert.Title"), keyPair, keyPairType, issuerCert, issuerPrivateKey, provider);
dGenerateKeyPairCert.setLocationRelativeTo(frame);
dGenerateKeyPairCert.setVisible(true);
X509Certificate certificate = dGenerateKeyPairCert.getCertificate();
if (certificate == null) {
return "";
}
KeyStoreState currentState = history.getCurrentState();
KeyStoreState newState = currentState.createBasisForNextState(this);
KeyStore keyStore = newState.getKeyStore();
DGetAlias dGetAlias = new DGetAlias(frame, res.getString("GenerateKeyPairAction.NewKeyPairEntryAlias.Title"), X509CertUtil.getCertificateAlias(certificate));
dGetAlias.setLocationRelativeTo(frame);
dGetAlias.setVisible(true);
alias = dGetAlias.getAlias();
if (alias == null) {
return "";
}
if (keyStore.containsAlias(alias)) {
String message = MessageFormat.format(res.getString("GenerateKeyPairAction.OverWriteEntry.message"), alias);
int selected = JOptionPane.showConfirmDialog(frame, message, res.getString("GenerateKeyPairAction.NewKeyPairEntryAlias.Title"), JOptionPane.YES_NO_OPTION);
if (selected != JOptionPane.YES_OPTION) {
return "";
}
}
Password password = new Password((char[]) null);
KeyStoreType keyStoreType = KeyStoreType.resolveJce(activeKeyStore.getType());
if (keyStoreType.hasEntryPasswords()) {
DGetNewPassword dGetNewPassword = new DGetNewPassword(frame, res.getString("GenerateKeyPairAction.NewKeyPairEntryPassword.Title"), applicationSettings.getPasswordQualityConfig());
dGetNewPassword.setLocationRelativeTo(frame);
dGetNewPassword.setVisible(true);
password = dGetNewPassword.getPassword();
if (password == null) {
return "";
}
}
if (keyStore.containsAlias(alias)) {
keyStore.deleteEntry(alias);
newState.removeEntryPassword(alias);
}
// create new chain with certificates from issuer chain
X509Certificate[] newCertChain = null;
if (issuerCertChain != null) {
newCertChain = new X509Certificate[issuerCertChain.length + 1];
System.arraycopy(issuerCertChain, 0, newCertChain, 1, issuerCertChain.length);
newCertChain[0] = certificate;
} else {
newCertChain = new X509Certificate[] { certificate };
}
keyStore.setKeyEntry(alias, keyPair.getPrivate(), password.toCharArray(), newCertChain);
newState.setEntryPassword(alias, password);
currentState.append(newState);
kseFrame.updateControls(true);
JOptionPane.showMessageDialog(frame, res.getString("GenerateKeyPairAction.KeyPairGenerationSuccessful.message"), res.getString("GenerateKeyPairAction.GenerateKeyPair.Title"), JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
DError.displayError(frame, ex);
}
return alias;
}
Aggregations