Search in sources :

Example 1 with KeyStoreHistory

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

the class KseFrame method updateApplicationTitle.

private void updateApplicationTitle() {
    // Title: "[KeyStore Name [*] - ] Application Name and Version"
    String appName = MessageFormat.format("{0} {1}", KSE.getApplicationName(), KSE.getApplicationVersion());
    KeyStoreHistory history = getActiveKeyStoreHistory();
    if (history == null) {
        frame.setTitle(appName);
    } else {
        String keyStoreName = history.getName();
        if (!history.getCurrentState().isSavedState()) {
            frame.setTitle(MessageFormat.format("{0} * - {1}", keyStoreName, appName));
        } else {
            frame.setTitle(MessageFormat.format("{0} - {1}", keyStoreName, appName));
        }
    }
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory)

Example 2 with KeyStoreHistory

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

the class KseFrame method getActiveKeyStore.

/**
 * Get the active KeyStore.
 *
 * @return The KeyStore or null if no KeyStore is active
 */
public KeyStore getActiveKeyStore() {
    KeyStoreHistory history = getActiveKeyStoreHistory();
    if (history == null) {
        return null;
    }
    KeyStoreState currentState = history.getCurrentState();
    KeyStore keyStore = currentState.getKeyStore();
    return keyStore;
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore)

Example 3 with KeyStoreHistory

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

the class KseFrame method handleDeleteSelectedEntry.

private void handleDeleteSelectedEntry() {
    KeyStoreHistory history = getActiveKeyStoreHistory();
    KeyStore keyStore = history.getCurrentState().getKeyStore();
    String alias = getSelectedEntryAlias();
    try {
        if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
            deleteKeyPairAction.deleteSelectedEntry();
        } else if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
            deleteTrustedCertificateAction.deleteSelectedEntry();
        } else {
            deleteKeyAction.deleteSelectedEntry();
        }
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException)

Example 4 with KeyStoreHistory

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

the class AppendToCertificateChainAction 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)));
        // Certificate to append to is the end one in the chain
        X509Certificate certToAppendTo = certChain[certChain.length - 1];
        if (X509CertUtil.isCertificateSelfSigned(certToAppendTo)) {
            JOptionPane.showMessageDialog(frame, res.getString("AppendToCertificateChainAction.CannotAppendCertSelfSigned.message"), res.getString("AppendToCertificateChainAction.AppendToCertificateChain.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        File certFile = chooseAppendCertificateFile();
        if (certFile == null) {
            return;
        }
        X509Certificate[] certs = openCertificate(certFile);
        if ((certs == null) || (certs.length == 0)) {
            return;
        }
        if (certs.length > 1) {
            JOptionPane.showMessageDialog(frame, res.getString("AppendToCertificateChainAction.NoMultipleAppendCert.message"), res.getString("AppendToCertificateChainAction.AppendToCertificateChain.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        X509Certificate certToAppend = certs[0];
        if (!X509CertUtil.verifyCertificate(certToAppendTo, certToAppend)) {
            JOptionPane.showMessageDialog(frame, res.getString("AppendToCertificateChainAction.AppendCertNotSigner.message"), res.getString("AppendToCertificateChainAction.AppendToCertificateChain.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        }
        X509Certificate[] newCertChain = new X509Certificate[certChain.length + 1];
        System.arraycopy(certChain, 0, newCertChain, 0, certChain.length);
        newCertChain[newCertChain.length - 1] = certToAppend;
        keyStore.deleteEntry(alias);
        keyStore.setKeyEntry(alias, privKey, password.toCharArray(), newCertChain);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("AppendToCertificateChainAction.AppendToCertificateChainSuccessful.message"), res.getString("AppendToCertificateChainAction.AppendToCertificateChain.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) File(java.io.File) Key(java.security.Key) X509Certificate(java.security.cert.X509Certificate) Password(org.kse.crypto.Password)

Example 5 with KeyStoreHistory

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

the class ChangeTypeAction method changeKeyStoreType.

private boolean changeKeyStoreType(KeyStoreType newKeyStoreType) {
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStore currentKeyStore = currentState.getKeyStore();
        String currentType = currentState.getKeyStore().getType();
        KeyStore newKeyStore = KeyStoreUtil.create(newKeyStoreType);
        // Only warn the user once
        resetWarnings();
        // Copy all entries to the new KeyStore: Trusted certs, key pairs and secret keys
        for (Enumeration<String> aliases = currentKeyStore.aliases(); aliases.hasMoreElements(); ) {
            String alias = aliases.nextElement();
            if (KeyStoreUtil.isTrustedCertificateEntry(alias, currentKeyStore)) {
                Certificate trustedCertificate = currentKeyStore.getCertificate(alias);
                newKeyStore.setCertificateEntry(alias, trustedCertificate);
            } else if (KeyStoreUtil.isKeyPairEntry(alias, currentKeyStore)) {
                if (!copyKeyPairEntry(newKeyStoreType, currentState, currentKeyStore, currentType, newKeyStore, alias)) {
                    return false;
                }
            } else if (KeyStoreUtil.isKeyEntry(alias, currentKeyStore)) {
                if (!copySecretKeyEntry(newKeyStoreType, currentState, currentKeyStore, newKeyStore, alias)) {
                    return false;
                }
            }
        }
        KeyStoreState newState = currentState.createBasisForNextState(this);
        newState.setKeyStore(newKeyStore);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("ChangeTypeAction.ChangeKeyStoreTypeSuccessful.message"), res.getString("ChangeTypeAction.ChangeKeyStoreType.Title"), JOptionPane.INFORMATION_MESSAGE);
        return true;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return false;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) CryptoException(org.kse.crypto.CryptoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) Certificate(java.security.cert.Certificate)

Aggregations

KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)60 KeyStore (java.security.KeyStore)45 KeyStoreState (org.kse.utilities.history.KeyStoreState)41 Password (org.kse.crypto.Password)31 X509Certificate (java.security.cert.X509Certificate)21 PrivateKey (java.security.PrivateKey)17 Certificate (java.security.cert.Certificate)13 Key (java.security.Key)12 CryptoException (org.kse.crypto.CryptoException)11 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)11 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 KeyStoreException (java.security.KeyStoreException)9 File (java.io.File)7 GeneralSecurityException (java.security.GeneralSecurityException)6 DViewCertificate (org.kse.gui.dialogs.DViewCertificate)6 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 FileNotFoundException (java.io.FileNotFoundException)5 PublicKey (java.security.PublicKey)5 Provider (java.security.Provider)4 ArrayList (java.util.ArrayList)3