Search in sources :

Example 1 with KeyStoreLoadException

use of org.kse.crypto.keystore.KeyStoreLoadException in project keystore-explorer by kaikramer.

the class AuthorityCertificatesAction method loadCaCertificatesKeyStore.

private KeyStore loadCaCertificatesKeyStore() {
    File caCertificatesFile = applicationSettings.getCaCertificatesFile();
    KeyStore caCertificatesKeyStore = null;
    try {
        // first try to open cacerts with default password
        try {
            Password password = new Password(AuthorityCertificates.CACERTS_DEFAULT_PWD.toCharArray());
            caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
            if (caCertificatesFile != null) {
                return caCertificatesKeyStore;
            }
        } catch (KeyStoreLoadException ex) {
        // not default password, continue with password dialog
        }
        DGetPassword dGetPassword = new DGetPassword(frame, res.getString("AuthorityCertificatesAction.CaCertificatesKeyStorePassword.Title"));
        dGetPassword.setLocationRelativeTo(frame);
        dGetPassword.setVisible(true);
        Password password = dGetPassword.getPassword();
        if (password == null) {
            return null;
        }
        try {
            caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
        } catch (KeyStoreLoadException ex) {
            String problemStr = MessageFormat.format(res.getString("AuthorityCertificatesAction.NoOpenCaCertificatesKeyStore.Problem"), ex.getKeyStoreType(), caCertificatesFile.getName());
            String[] causes = new String[] { res.getString("AuthorityCertificatesAction.PasswordIncorrectKeyStore.Cause"), res.getString("AuthorityCertificatesAction.CorruptedKeyStore.Cause") };
            Problem problem = new Problem(problemStr, causes, ex);
            DProblem dProblem = new DProblem(frame, res.getString("AuthorityCertificatesAction.ProblemOpeningCaCertificatesKeyStore.Title"), problem);
            dProblem.setLocationRelativeTo(frame);
            dProblem.setVisible(true);
            return null;
        }
        if (caCertificatesKeyStore == null) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.FileNotRecognisedType.message"), caCertificatesFile.getName()), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
            return null;
        }
        return caCertificatesKeyStore;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.NoReadFile.message"), caCertificatesFile), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return null;
    }
}
Also used : DGetPassword(org.kse.gui.password.DGetPassword) KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) FileNotFoundException(java.io.FileNotFoundException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) File(java.io.File) KeyStore(java.security.KeyStore) DProblem(org.kse.gui.error.DProblem) FileNotFoundException(java.io.FileNotFoundException) CryptoException(org.kse.crypto.CryptoException) KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) DGetPassword(org.kse.gui.password.DGetPassword) Password(org.kse.crypto.Password)

Example 2 with KeyStoreLoadException

use of org.kse.crypto.keystore.KeyStoreLoadException 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)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 KeyStore (java.security.KeyStore)2 Password (org.kse.crypto.Password)2 KeyStoreLoadException (org.kse.crypto.keystore.KeyStoreLoadException)2 DGetPassword (org.kse.gui.password.DGetPassword)2 File (java.io.File)1 CryptoException (org.kse.crypto.CryptoException)1 DProblem (org.kse.gui.error.DProblem)1 Problem (org.kse.gui.error.Problem)1