Search in sources :

Example 21 with DProblem

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

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