Search in sources :

Example 16 with DProblem

use of org.kse.gui.error.DProblem 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 17 with DProblem

use of org.kse.gui.error.DProblem 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 18 with DProblem

use of org.kse.gui.error.DProblem in project keystore-explorer by kaikramer.

the class DImportKeyPairPkcs8 method loadCertificates.

private X509Certificate[] loadCertificates() {
    String certificatePath = jtfCertificatePath.getText().trim();
    if (certificatePath.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DImportKeyPairPkcs8.CertificateRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    }
    File certificateFile = new File(certificatePath);
    try {
        X509Certificate[] certs = X509CertUtil.loadCertificates(new FileInputStream(certificateFile));
        if (certs.length == 0) {
            JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPkcs8.NoCertsFound.message"), certificateFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        }
        return certs;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPkcs8.NoReadFile.message"), certificateFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        Problem problem = createLoadCertsProblem(ex, certificateFile);
        DProblem dProblem = new DProblem(this, res.getString("DImportKeyPairPkcs8.ProblemLoadingCerts.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) 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)

Example 19 with DProblem

use of org.kse.gui.error.DProblem 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 20 with DProblem

use of org.kse.gui.error.DProblem in project keystore-explorer by kaikramer.

the class DImportKeyPairPvk method loadCertificates.

private X509Certificate[] loadCertificates() {
    String certificatePath = jtfCertificatePath.getText().trim();
    if (certificatePath.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DImportKeyPairPvk.CertificateRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    }
    File certificateFile = new File(certificatePath);
    try {
        X509Certificate[] certs = X509CertUtil.loadCertificates(new FileInputStream(certificateFile));
        if (certs.length == 0) {
            JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPvk.NoCertsFound.message"), certificateFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        }
        return certs;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DImportKeyPairPvk.NoReadFile.message"), certificateFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        Problem problem = createLoadCertsProblem(ex, certificateFile);
        DProblem dProblem = new DProblem(this, res.getString("DImportKeyPairPvk.ProblemLoadingCerts.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) 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)

Aggregations

DProblem (org.kse.gui.error.DProblem)22 Problem (org.kse.gui.error.Problem)22 CryptoException (org.kse.crypto.CryptoException)12 File (java.io.File)11 FileNotFoundException (java.io.FileNotFoundException)11 FileInputStream (java.io.FileInputStream)9 Password (org.kse.crypto.Password)9 IOException (java.io.IOException)7 X509Certificate (java.security.cert.X509Certificate)7 KeyStore (java.security.KeyStore)6 PrivateKeyEncryptedException (org.kse.crypto.privatekey.PrivateKeyEncryptedException)6 PrivateKeyUnencryptedException (org.kse.crypto.privatekey.PrivateKeyUnencryptedException)6 PrivateKey (java.security.PrivateKey)5 PrivateKeyPbeNotSupportedException (org.kse.crypto.privatekey.PrivateKeyPbeNotSupportedException)4 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)3 GeneralSecurityException (java.security.GeneralSecurityException)3 Certificate (java.security.cert.Certificate)3 PKCS10CertificationRequest (org.bouncycastle.pkcs.PKCS10CertificationRequest)3 Spkac (org.kse.crypto.csr.spkac.Spkac)3 KeyStoreHistory (org.kse.utilities.history.KeyStoreHistory)3