Search in sources :

Example 36 with Password

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);
    }
}
Also used : KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) FileNotFoundException(java.io.FileNotFoundException) KeyStore(java.security.KeyStore) FileNotFoundException(java.io.FileNotFoundException) KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) DGetPassword(org.kse.gui.password.DGetPassword) Password(org.kse.crypto.Password)

Example 37 with Password

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);
    }
}
Also used : DNewKeyStoreType(org.kse.gui.dialogs.DNewKeyStoreType) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) DNewKeyStoreType(org.kse.gui.dialogs.DNewKeyStoreType) File(java.io.File) KeyStore(java.security.KeyStore) Password(org.kse.crypto.Password)

Example 38 with Password

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;
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) KeyBufferEntry(org.kse.utilities.buffer.KeyBufferEntry) KeyPairBufferEntry(org.kse.utilities.buffer.KeyPairBufferEntry) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) TrustedCertificateBufferEntry(org.kse.utilities.buffer.TrustedCertificateBufferEntry) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Password(org.kse.crypto.Password)

Example 39 with Password

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);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) Key(java.security.Key) X509Certificate(java.security.cert.X509Certificate) Password(org.kse.crypto.Password)

Example 40 with Password

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);
    }
}
Also used : DGetAlias(org.kse.gui.dialogs.DGetAlias) KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStore(java.security.KeyStore) Key(java.security.Key) Password(org.kse.crypto.Password)

Aggregations

Password (org.kse.crypto.Password)60 KeyStore (java.security.KeyStore)35 KeyStoreState (org.kse.utilities.history.KeyStoreState)32 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)31 PrivateKey (java.security.PrivateKey)24 File (java.io.File)23 FileNotFoundException (java.io.FileNotFoundException)16 Key (java.security.Key)15 X509Certificate (java.security.cert.X509Certificate)15 Certificate (java.security.cert.Certificate)13 KeyStoreType (org.kse.crypto.keystore.KeyStoreType)12 CryptoException (org.kse.crypto.CryptoException)9 DProblem (org.kse.gui.error.DProblem)9 Problem (org.kse.gui.error.Problem)9 DGetAlias (org.kse.gui.dialogs.DGetAlias)8 DGetNewPassword (org.kse.gui.password.DGetNewPassword)8 JPasswordField (javax.swing.JPasswordField)6 DViewPrivateKey (org.kse.gui.dialogs.DViewPrivateKey)6 DGetPassword (org.kse.gui.password.DGetPassword)6 FileInputStream (java.io.FileInputStream)5