Search in sources :

Example 21 with Problem

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

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

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

the class DImportKeyPairPkcs12 method createLoadPkcs12Problem.

private Problem createLoadPkcs12Problem(Exception exception, File pkcs12File) {
    String problemStr = MessageFormat.format(res.getString("DImportKeyPairPkcs12.NoLoadPkcs12.Problem"), pkcs12File.getName());
    String[] causes = new String[] { res.getString("DImportKeyPairPkcs12.PasswordIncorrectPkcs12.Cause"), res.getString("DImportKeyPairPkcs12.NotPkcs12.Cause"), res.getString("DImportKeyPairPkcs12.CorruptedPkcs12.Cause") };
    Problem problem = new Problem(problemStr, causes, exception);
    return problem;
}
Also used : Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem)

Example 24 with Problem

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

the class DImportKeyPairPkcs8 method createLoadPkcs8Problem.

private Problem createLoadPkcs8Problem(Exception exception, File pkcs8File) {
    String problemStr = null;
    ArrayList<String> causeList = new ArrayList<String>();
    if (jcbEncrypted.isSelected()) {
        problemStr = MessageFormat.format(res.getString("DImportKeyPairPkcs8.NoLoadEncryptedPkcs8.Problem"), pkcs8File.getName());
        causeList.add(res.getString("DImportKeyPairPkcs8.PasswordIncorrectPkcs8.Cause"));
    } else {
        problemStr = MessageFormat.format(res.getString("DImportKeyPairPkcs8.NoLoadUnencryptedPkcs8.Problem"), pkcs8File.getName());
    }
    causeList.add(res.getString("DImportKeyPairPkcs8.NotPkcs8.Cause"));
    causeList.add(res.getString("DImportKeyPairPkcs8.CorruptedPkcs8.Cause"));
    String[] causes = causeList.toArray(new String[causeList.size()]);
    Problem problem = new Problem(problemStr, causes, exception);
    return problem;
}
Also used : ArrayList(java.util.ArrayList) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem)

Example 25 with Problem

use of org.kse.gui.error.Problem 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)

Aggregations

DProblem (org.kse.gui.error.DProblem)29 Problem (org.kse.gui.error.Problem)29 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 ArrayList (java.util.ArrayList)4 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