Search in sources :

Example 51 with Password

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

the class DImportKeyPairOpenSsl method loadPrivateKey.

private PrivateKey loadPrivateKey() {
    String privateKeyPath = jtfPrivateKeyPath.getText().trim();
    if (privateKeyPath.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DImportKeyPairOpenSsl.PrivateKeyRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    }
    File privateKeyFile = new File(privateKeyPath);
    try {
        PrivateKey privateKey = null;
        if (!jcbEncrypted.isSelected()) {
            privateKey = OpenSslPvkUtil.load(new FileInputStream(privateKeyFile));
        } else {
            Password password = new Password(jpfPassword.getPassword());
            privateKey = OpenSslPvkUtil.loadEncrypted(new FileInputStream(privateKeyFile), password);
        }
        return privateKey;
    } catch (PrivateKeyEncryptedException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairOpenSsl.PrivateKeyEncrypted.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        jcbEncrypted.setSelected(true);
        return null;
    } catch (PrivateKeyUnencryptedException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairOpenSsl.PrivateKeyNotEncrypted.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        jcbEncrypted.setSelected(false);
        return null;
    } catch (PrivateKeyPbeNotSupportedException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairOpenSsl.PrivateKeyPbeNotSupported.message"), ex.getUnsupportedPbe()), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairOpenSsl.NoReadFile.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        Problem problem = createLoadOpenSslProblem(ex, privateKeyFile);
        DProblem dProblem = new DProblem(this, res.getString("DImportKeyPairOpenSsl.ProblemLoadingOpenSsl.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : PrivateKeyEncryptedException(org.kse.crypto.privatekey.PrivateKeyEncryptedException) PrivateKeyUnencryptedException(org.kse.crypto.privatekey.PrivateKeyUnencryptedException) PrivateKey(java.security.PrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) FileNotFoundException(java.io.FileNotFoundException) PrivateKeyPbeNotSupportedException(org.kse.crypto.privatekey.PrivateKeyPbeNotSupportedException) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) FileInputStream(java.io.FileInputStream) CryptoException(org.kse.crypto.CryptoException) PrivateKeyUnencryptedException(org.kse.crypto.privatekey.PrivateKeyUnencryptedException) PrivateKeyPbeNotSupportedException(org.kse.crypto.privatekey.PrivateKeyPbeNotSupportedException) FileNotFoundException(java.io.FileNotFoundException) PrivateKeyEncryptedException(org.kse.crypto.privatekey.PrivateKeyEncryptedException) DProblem(org.kse.gui.error.DProblem) Password(org.kse.crypto.Password)

Example 52 with Password

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

the class DImportKeyPairPkcs12 method loadKeyPair.

private Keypair loadKeyPair() {
    String pkcs12Path = jtfPkcs12Path.getText().trim();
    if (pkcs12Path.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DImportKeyPairPkcs12.KeyPairRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    }
    File pkcs12File = new File(pkcs12Path);
    try {
        Password pkcs12Password = new Password(jpfPassword.getPassword());
        KeyStore pkcs12 = KeyStoreUtil.load(pkcs12File, pkcs12Password, KeyStoreType.PKCS12);
        // Find a key pair in the PKCS #12 KeyStore
        PrivateKey privKey = null;
        ArrayList<Certificate> certsList = new ArrayList<Certificate>();
        // Look for key pair entries first
        for (Enumeration<?> aliases = pkcs12.aliases(); aliases.hasMoreElements(); ) {
            String alias = (String) aliases.nextElement();
            if (pkcs12.isKeyEntry(alias)) {
                privKey = (PrivateKey) pkcs12.getKey(alias, pkcs12Password.toCharArray());
                Certificate[] certs = pkcs12.getCertificateChain(alias);
                if ((certs != null) && (certs.length > 0)) {
                    Collections.addAll(certsList, certs);
                    break;
                }
            }
        }
        // entries
        if ((privKey == null) || (certsList.size() == 0)) {
            for (Enumeration<?> aliases = pkcs12.aliases(); aliases.hasMoreElements(); ) {
                String alias = (String) aliases.nextElement();
                certsList.add(pkcs12.getCertificate(alias));
            }
        }
        if ((privKey == null) || (certsList.size() == 0)) {
            JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPkcs12.NoKeyPairPkcs12File.message"), pkcs12File.getName()), getTitle(), JOptionPane.INFORMATION_MESSAGE);
            return null;
        }
        X509Certificate[] certs = X509CertUtil.convertCertificates(certsList.toArray(new Certificate[certsList.size()]));
        return new Keypair(privKey, certs);
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPkcs12.NoReadFile.message"), pkcs12File), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        Problem problem = createLoadPkcs12Problem(ex, pkcs12File);
        DProblem dProblem = new DProblem(this, res.getString("DImportKeyPairPkcs12.ProblemLoadingPkcs12.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) FileNotFoundException(java.io.FileNotFoundException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) Password(org.kse.crypto.Password) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 53 with Password

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

the class DImportKeyPairPvk method loadPrivateKey.

private PrivateKey loadPrivateKey() {
    String privateKeyPath = jtfPrivateKeyPath.getText().trim();
    if (privateKeyPath.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DImportKeyPairPvk.PrivateKeyRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    }
    File privateKeyFile = new File(privateKeyPath);
    try {
        PrivateKey privateKey = null;
        if (!jcbEncrypted.isSelected()) {
            privateKey = MsPvkUtil.load(new FileInputStream(privateKeyFile));
        } else {
            Password password = new Password(jpfPassword.getPassword());
            privateKey = MsPvkUtil.loadEncrypted(new FileInputStream(privateKeyFile), password);
        }
        return privateKey;
    } catch (PrivateKeyEncryptedException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPvk.PrivateKeyEncrypted.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        jcbEncrypted.setSelected(true);
        return null;
    } catch (PrivateKeyUnencryptedException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPvk.PrivateKeyNotEncrypted.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        jcbEncrypted.setSelected(false);
        return null;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPvk.NoReadFile.message"), privateKeyFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        Problem problem = createLoadPvkProblem(ex, privateKeyFile);
        DProblem dProblem = new DProblem(this, res.getString("DImportKeyPairPvk.ProblemLoadingPvk.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : PrivateKeyEncryptedException(org.kse.crypto.privatekey.PrivateKeyEncryptedException) PrivateKeyUnencryptedException(org.kse.crypto.privatekey.PrivateKeyUnencryptedException) PrivateKey(java.security.PrivateKey) DViewPrivateKey(org.kse.gui.dialogs.DViewPrivateKey) FileNotFoundException(java.io.FileNotFoundException) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) FileInputStream(java.io.FileInputStream) CryptoException(org.kse.crypto.CryptoException) PrivateKeyUnencryptedException(org.kse.crypto.privatekey.PrivateKeyUnencryptedException) FileNotFoundException(java.io.FileNotFoundException) PrivateKeyEncryptedException(org.kse.crypto.privatekey.PrivateKeyEncryptedException) DProblem(org.kse.gui.error.DProblem) Password(org.kse.crypto.Password)

Example 54 with Password

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

the class DChangePassword method checkPassword.

private boolean checkPassword() {
    Password oldPassword = new Password(jpfOld.getPassword());
    Password firstPassword;
    if (jpfFirst instanceof JPasswordQualityField) {
        char[] firstPasswordChars = ((JPasswordQualityField) jpfFirst).getPassword();
        if (firstPasswordChars == null) {
            JOptionPane.showMessageDialog(this, res.getString("MinimumPasswordQualityNotMet.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        }
        firstPassword = new Password(firstPasswordChars);
    } else {
        firstPassword = new Password(((JPasswordField) jpfFirst).getPassword());
    }
    Password confirmPassword = new Password(jpfConfirm.getPassword());
    if (firstPassword.equals(confirmPassword)) {
        this.oldPassword = oldPassword;
        newPassword = firstPassword;
        return true;
    }
    JOptionPane.showMessageDialog(this, res.getString("PasswordsNoMatch.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
    return false;
}
Also used : JPasswordField(javax.swing.JPasswordField) Password(org.kse.crypto.Password)

Example 55 with Password

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

the class DGetNewPassword method checkPassword.

private boolean checkPassword() {
    Password firstPassword;
    if (jpfFirst instanceof JPasswordQualityField) {
        char[] firstPasswordChars = ((JPasswordQualityField) jpfFirst).getPassword();
        if (firstPasswordChars == null) {
            JOptionPane.showMessageDialog(this, res.getString("MinimumPasswordQualityNotMet.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        }
        firstPassword = new Password(firstPasswordChars);
    } else {
        firstPassword = new Password(((JPasswordField) jpfFirst).getPassword());
    }
    Password confirmPassword = new Password(jpfConfirm.getPassword());
    if (firstPassword.equals(confirmPassword)) {
        password = firstPassword;
        return true;
    }
    JOptionPane.showMessageDialog(this, res.getString("PasswordsNoMatch.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
    return false;
}
Also used : JPasswordField(javax.swing.JPasswordField) 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