Search in sources :

Example 1 with KeyStoreState

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

the class KeyStoreTableModel method load.

/**
 * Load the KeyStoreTableModel with the entries from a KeyStore.
 *
 * @param history
 *            KeyStore history
 * @throws GeneralSecurityException
 *             If a KeyStore problem occurs while accessing the KeyStore's
 *             entries
 * @throws CryptoException
 *             If a crypto problem occurs while accessing the KeyStore's
 *             entries
 */
public void load(KeyStoreHistory history) throws GeneralSecurityException, CryptoException {
    KeyStoreState currentState = history.getCurrentState();
    KeyStore keyStore = currentState.getKeyStore();
    KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
    Enumeration<String> aliases = keyStore.aliases();
    TreeMap<String, String> sortedAliases = new TreeMap<String, String>(new AliasComparator());
    while (aliases.hasMoreElements()) {
        String alias = aliases.nextElement();
        if (!KeyStoreUtil.isSupportedEntryType(alias, keyStore)) {
            continue;
        }
        sortedAliases.put(alias, alias);
    }
    data = new Object[sortedAliases.size()][8];
    int i = 0;
    for (Iterator<Entry<String, String>> itr = sortedAliases.entrySet().iterator(); itr.hasNext(); i++) {
        String alias = itr.next().getKey();
        String entryType = null;
        // Type column
        if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
            entryType = TRUST_CERT_ENTRY;
        } else if (KeyStoreUtil.isKeyPairEntry(alias, keyStore)) {
            entryType = KEY_PAIR_ENTRY;
        } else {
            entryType = KEY_ENTRY;
        }
        data[i][0] = entryType;
        // Lock column - only applies to KeyStores types that actually support passwords for entries
        if ((entryType.equals(KEY_PAIR_ENTRY) || entryType.equals(KEY_ENTRY)) && type.hasEntryPasswords()) {
            if (currentState.getEntryPassword(alias) != null) {
                // Unlocked
                data[i][1] = Boolean.FALSE;
            } else {
                // Locked
                data[i][1] = Boolean.TRUE;
            }
        } else {
            // Lock status does not apply
            data[i][1] = null;
        }
        // Expiry status column
        Date expiry = getCertificateExpiry(alias, keyStore);
        if (expiry == null) {
            // No expiry - must be a key entry
            data[i][2] = null;
        } else if (new Date().after(expiry)) {
            // Expired
            data[i][2] = Boolean.TRUE;
        } else {
            // Not expired
            data[i][2] = Boolean.FALSE;
        }
        // Alias column
        data[i][3] = alias;
        KeyInfo keyInfo = getKeyInfo(alias, keyStore, currentState);
        if (keyInfo != null) {
            // Algorithm column
            data[i][4] = getAlgorithmName(keyInfo);
            // Key Size column
            data[i][5] = keyInfo.getSize();
        }
        // Expiry date column
        if (expiry != null) {
            data[i][6] = expiry;
        } else {
            // No expiry date - must be a key entry
            data[i][6] = null;
        }
        // Modified date column - only applies to non-PKCS #11/#12 KeyStores
        if (!keyStore.getType().equals(KeyStoreType.PKCS12.jce()) && !keyStore.getType().equals(KeyStoreType.PKCS11.jce())) {
            data[i][7] = keyStore.getCreationDate(alias);
        } else {
            data[i][7] = null;
        }
    }
    fireTableDataChanged();
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) TreeMap(java.util.TreeMap) KeyStore(java.security.KeyStore) Date(java.util.Date) Entry(java.util.Map.Entry) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) KeyInfo(org.kse.crypto.KeyInfo)

Example 2 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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 KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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 4 with KeyStoreState

use of org.kse.utilities.history.KeyStoreState 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)

Example 5 with KeyStoreState

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

the class KseFrame method getKeyStoreStatusText.

private String getKeyStoreStatusText(KeyStoreHistory history) {
    // Status Text: 'KeyStore Type, Size, Path'
    KeyStoreState currentState = history.getCurrentState();
    KeyStore ksLoaded = currentState.getKeyStore();
    int size;
    try {
        size = ksLoaded.size();
    } catch (KeyStoreException ex) {
        DError.displayError(frame, ex);
        return "";
    }
    KeyStoreType keyStoreType = currentState.getType();
    return MessageFormat.format(res.getString("KseFrame.entries.statusbar"), keyStoreType.friendly(), size, history.getPath());
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) Point(java.awt.Point)

Aggregations

KeyStoreState (org.kse.utilities.history.KeyStoreState)48 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)41 KeyStore (java.security.KeyStore)39 Password (org.kse.crypto.Password)32 PrivateKey (java.security.PrivateKey)17 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)13 Key (java.security.Key)12 DGetAlias (org.kse.gui.dialogs.DGetAlias)10 File (java.io.File)9 FileNotFoundException (java.io.FileNotFoundException)7 DGetNewPassword (org.kse.gui.password.DGetNewPassword)6 PublicKey (java.security.PublicKey)5 CryptoException (org.kse.crypto.CryptoException)5 GeneralSecurityException (java.security.GeneralSecurityException)4 KeyStoreException (java.security.KeyStoreException)4 Provider (java.security.Provider)4 ArrayList (java.util.ArrayList)3 KeyPairType (org.kse.crypto.keypair.KeyPairType)3