Search in sources :

Example 26 with Problem

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

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

Example 28 with Problem

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

the class DSignJar method okPressed.

private void okPressed() {
    String inputJar = jtfInputJar.getText().trim();
    if (inputJar.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DSignJar.InputJarRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    File inputJarFile = new File(inputJar);
    if (!inputJarFile.isFile()) {
        JOptionPane.showMessageDialog(this, MessageFormat.format(res.getString("DSignJar.InputJarNotFile.message"), inputJarFile), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(inputJarFile);
    } catch (IOException ex) {
        String problemStr = MessageFormat.format(res.getString("DSignJar.NoOpenJar.Problem"), inputJarFile.getName());
        String[] causes = new String[] { res.getString("DSignJar.NotJar.Cause"), res.getString("DSignJar.CorruptedJar.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(this, res.getString("DSignJar.ProblemOpeningJar.Title"), problem);
        dProblem.setLocationRelativeTo(this);
        dProblem.setVisible(true);
        return;
    } finally {
        IOUtils.closeQuietly(jarFile);
    }
    boolean signDirectly = jcbSignDirectly.isSelected();
    File outputJarFile;
    if (signDirectly) {
        outputJarFile = inputJarFile;
    } else {
        String outputJar = jtfOutputJar.getText().trim();
        if (outputJar.length() == 0) {
            JOptionPane.showMessageDialog(this, res.getString("DSignJar.OutputJarRequired.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return;
        }
        outputJarFile = new File(outputJar);
    }
    String signatureName = jtfSignatureName.getText().trim();
    if (signatureName.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DSignJar.ValReqSignatureName.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    if (!verifySignatureName(signatureName)) {
        JOptionPane.showMessageDialog(this, res.getString("DSignJar.ValJarSignatureName.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    if (!signDirectly && outputJarFile.isFile()) {
        String message = MessageFormat.format(res.getString("DSignJar.OverWriteOutputJarFile.message"), outputJarFile);
        int selected = JOptionPane.showConfirmDialog(this, message, getTitle(), JOptionPane.YES_NO_OPTION);
        if (selected != JOptionPane.YES_OPTION) {
            return;
        }
    }
    try {
        if (JarSigner.hasSignature(new File(inputJar), signatureName)) {
            String message = MessageFormat.format(res.getString("DSignJar.SignatureOverwrite.message"), signatureName);
            int selected = JOptionPane.showConfirmDialog(this, message, getTitle(), JOptionPane.YES_NO_OPTION);
            if (selected != JOptionPane.YES_OPTION) {
                return;
            }
        }
    } catch (IOException ex) {
        DError.displayError(this, ex);
        return;
    }
    if (jcbAddTimestamp.isSelected() && jcbTimestampServerUrl.getSelectedItem().toString().isEmpty()) {
        JOptionPane.showMessageDialog(this, res.getString("DSignJar.EmptyTimestampUrl.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return;
    }
    this.inputJarFile = inputJarFile;
    this.outputJarFile = outputJarFile;
    this.signatureName = signatureName;
    signatureType = (SignatureType) jcbSignatureAlgorithm.getSelectedItem();
    digestType = (DigestType) jcbDigestAlgorithm.getSelectedItem();
    if (jcbAddTimestamp.isSelected()) {
        tsaUrl = jcbTimestampServerUrl.getSelectedItem().toString();
    }
    closeDialog();
}
Also used : Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) DProblem(org.kse.gui.error.DProblem)

Example 29 with Problem

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

the class DOpenPkcs11KeyStore method okPressed.

private void okPressed() {
    final String selectedLib = ((String) jtfP11Library.getSelectedItem()).trim();
    try {
        if (jrbUseExisting.isSelected()) {
            String providerName = (String) jcbPkcs11Provider.getSelectedItem();
            selectedProvider = Security.getProvider(providerName);
            if (selectedProvider == null) {
                JOptionPane.showMessageDialog(this, res.getString("DOpenPkcs11KeyStore.providerNotInstalled.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            }
        } else {
            if (selectedLib.isEmpty()) {
                JOptionPane.showMessageDialog(this, res.getString("DOpenPkcs11KeyStore.noLibSelected.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            String pkcs11ConfigSettings = "name = Slot" + jspSlotListIndex.getValue() + "\n" + "library = " + selectedLib + "\n" + "slotListIndex = " + jspSlotListIndex.getValue() + "";
            ByteArrayInputStream confStream = new ByteArrayInputStream(pkcs11ConfigSettings.getBytes());
            // instantiate the provider
            Provider p11Provider = null;
            if (JavaVersion.getJreVersion().isAtLeast(JavaVersion.JRE_VERSION_9)) {
                p11Provider = Security.getProvider("SunPKCS11");
                // add marker ("--") for inline config
                pkcs11ConfigSettings = "--" + pkcs11ConfigSettings;
                // p11Provider.configure(pkcs11ConfigSettings);
                Method method = Provider.class.getMethod("configure", String.class);
                p11Provider = (Provider) method.invoke(p11Provider, pkcs11ConfigSettings);
            } else {
                Class<?> cl = Class.forName("sun.security.pkcs11.SunPKCS11");
                Constructor<?> cons = cl.getConstructor(InputStream.class);
                p11Provider = (Provider) cons.newInstance(confStream);
            }
            Security.addProvider(p11Provider);
            selectedProvider = p11Provider;
            // save library in preferences
            applicationSettings.addP11Lib(selectedLib);
        }
        closeDialog();
    } catch (final Exception e) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                if (DOpenPkcs11KeyStore.this.isShowing()) {
                    String problemStr = MessageFormat.format(res.getString("DOpenPkcs11KeyStore.ProblemLoadingProvider.Problem"), selectedLib);
                    String[] causes = new String[] { res.getString("DOpenPkcs11KeyStore.NotPkcs11Lib.Cause"), res.getString("DOpenPkcs11KeyStore.32with64bit.Cause"), res.getString("DOpenPkcs11KeyStore.64bitBeforeJRE8.Cause"), res.getString("DOpenPkcs11KeyStore.WrongConfiguration.Cause") };
                    Problem problem = new Problem(problemStr, causes, e);
                    DProblem dProblem = new DProblem(DOpenPkcs11KeyStore.this, res.getString("DOpenPkcs11KeyStore.ProblemLoadingProvider.Title"), problem);
                    dProblem.setLocationRelativeTo(DOpenPkcs11KeyStore.this);
                    dProblem.setVisible(true);
                }
            }
        });
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) Method(java.lang.reflect.Method) DProblem(org.kse.gui.error.DProblem) Provider(java.security.Provider)

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