Search in sources :

Example 1 with DProblem

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

the class AuthorityCertificatesAction method loadCaCertificatesKeyStore.

private KeyStore loadCaCertificatesKeyStore() {
    File caCertificatesFile = applicationSettings.getCaCertificatesFile();
    KeyStore caCertificatesKeyStore = null;
    try {
        // first try to open cacerts with default password
        try {
            Password password = new Password(AuthorityCertificates.CACERTS_DEFAULT_PWD.toCharArray());
            caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
            if (caCertificatesFile != null) {
                return caCertificatesKeyStore;
            }
        } catch (KeyStoreLoadException ex) {
        // not default password, continue with password dialog
        }
        DGetPassword dGetPassword = new DGetPassword(frame, res.getString("AuthorityCertificatesAction.CaCertificatesKeyStorePassword.Title"));
        dGetPassword.setLocationRelativeTo(frame);
        dGetPassword.setVisible(true);
        Password password = dGetPassword.getPassword();
        if (password == null) {
            return null;
        }
        try {
            caCertificatesKeyStore = KeyStoreUtil.load(caCertificatesFile, password);
        } catch (KeyStoreLoadException ex) {
            String problemStr = MessageFormat.format(res.getString("AuthorityCertificatesAction.NoOpenCaCertificatesKeyStore.Problem"), ex.getKeyStoreType(), caCertificatesFile.getName());
            String[] causes = new String[] { res.getString("AuthorityCertificatesAction.PasswordIncorrectKeyStore.Cause"), res.getString("AuthorityCertificatesAction.CorruptedKeyStore.Cause") };
            Problem problem = new Problem(problemStr, causes, ex);
            DProblem dProblem = new DProblem(frame, res.getString("AuthorityCertificatesAction.ProblemOpeningCaCertificatesKeyStore.Title"), problem);
            dProblem.setLocationRelativeTo(frame);
            dProblem.setVisible(true);
            return null;
        }
        if (caCertificatesKeyStore == null) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.FileNotRecognisedType.message"), caCertificatesFile.getName()), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
            return null;
        }
        return caCertificatesKeyStore;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("AuthorityCertificatesAction.NoReadFile.message"), caCertificatesFile), res.getString("AuthorityCertificatesAction.OpenCaCertificatesKeyStore.Title"), JOptionPane.WARNING_MESSAGE);
        return null;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return null;
    }
}
Also used : DGetPassword(org.kse.gui.password.DGetPassword) KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) FileNotFoundException(java.io.FileNotFoundException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) File(java.io.File) KeyStore(java.security.KeyStore) DProblem(org.kse.gui.error.DProblem) FileNotFoundException(java.io.FileNotFoundException) CryptoException(org.kse.crypto.CryptoException) KeyStoreLoadException(org.kse.crypto.keystore.KeyStoreLoadException) DGetPassword(org.kse.gui.password.DGetPassword) Password(org.kse.crypto.Password)

Example 2 with DProblem

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

the class ImportCaReplyFromClipboardAction method openCaReply.

private X509Certificate[] openCaReply() {
    X509Certificate[] certs = null;
    try {
        // get clip board contents, but only string types, not files
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable t = clipboard.getContents(null);
        if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data;
            data = (String) t.getTransferData(DataFlavor.stringFlavor);
            ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes());
            // try to extract certs from clip board data
            certs = X509CertUtil.loadCertificates(bais);
            if (certs.length == 0) {
                JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("ImportCaReplyFromClipboardAction.NoCertsFound.message"), "Clipboard"), res.getString("ImportCaReplyFromClipboardAction.OpenCaReply.Title"), JOptionPane.WARNING_MESSAGE);
            }
        }
        return certs;
    } catch (Exception ex) {
        String problemStr = MessageFormat.format(res.getString("ImportCaReplyFromClipboardAction.NoOpenCaReply.Problem"), "Clipboard");
        String[] causes = new String[] { res.getString("ImportCaReplyFromClipboardAction.NotCaReply.Cause"), res.getString("ImportCaReplyFromClipboardAction.CorruptedCaReply.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("ImportCaReplyFromClipboardAction.ProblemOpeningCaReply.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
        return null;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Transferable(java.awt.datatransfer.Transferable) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) Clipboard(java.awt.datatransfer.Clipboard) X509Certificate(java.security.cert.X509Certificate) DProblem(org.kse.gui.error.DProblem)

Example 3 with DProblem

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

the class ExamineClipboardAction method showCert.

private void showCert(InputStream is) throws CryptoException {
    X509Certificate[] certs = null;
    try {
        certs = X509CertUtil.loadCertificates(is);
        if (certs.length == 0) {
            JOptionPane.showMessageDialog(frame, res.getString("ExamineClipboardAction.NoCertsFound.message"), res.getString("ExamineClipboardAction.OpenCertificate.Title"), JOptionPane.WARNING_MESSAGE);
        }
    } catch (Exception ex) {
        String problemStr = res.getString("ExamineClipboardAction.NoOpenCert.Problem");
        String[] causes = new String[] { res.getString("ExamineClipboardAction.NotCert.Cause"), res.getString("ExamineClipboardAction.CorruptedCert.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("ExamineClipboardAction.ProblemOpeningCert.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
    }
    if (certs != null && certs.length > 0) {
        DViewCertificate dViewCertificate = new DViewCertificate(frame, res.getString("ExamineClipboardAction.CertDetails.Title"), certs, kseFrame, DViewCertificate.IMPORT);
        dViewCertificate.setLocationRelativeTo(frame);
        dViewCertificate.setVisible(true);
    }
}
Also used : DViewCertificate(org.kse.gui.dialogs.DViewCertificate) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) X509Certificate(java.security.cert.X509Certificate) CryptoException(org.kse.crypto.CryptoException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) DProblem(org.kse.gui.error.DProblem)

Example 4 with DProblem

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

the class SignCsrAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    FileOutputStream fos = null;
    File caReplyFile = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        String alias = kseFrame.getSelectedEntryAlias();
        Password password = getEntryPassword(alias, currentState);
        if (password == null) {
            return;
        }
        KeyStore keyStore = currentState.getKeyStore();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        Certificate[] certs = keyStore.getCertificateChain(alias);
        KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
        File csrFile = chooseCsrFile();
        if (csrFile == null) {
            return;
        }
        PKCS10CertificationRequest pkcs10Csr = null;
        Spkac spkacCsr = null;
        try {
            CryptoFileType fileType = CryptoFileUtil.detectFileType(new FileInputStream(csrFile));
            if (fileType == CryptoFileType.PKCS10_CSR) {
                pkcs10Csr = Pkcs10Util.loadCsr(new FileInputStream(csrFile));
                if (!Pkcs10Util.verifyCsr(pkcs10Csr)) {
                    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.NoVerifyPkcs10Csr.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                    return;
                }
            } else if (fileType == CryptoFileType.SPKAC_CSR) {
                spkacCsr = new Spkac(new FileInputStream(csrFile));
                if (!spkacCsr.verify()) {
                    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.NoVerifySpkacCsr.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                    return;
                }
            } else {
                JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignCsrAction.FileNotRecognisedType.message"), csrFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
                return;
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignCsrAction.NotFile.message"), csrFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
            return;
        } catch (Exception ex) {
            String problemStr = MessageFormat.format(res.getString("SignCsrAction.NoOpenCsr.Problem"), csrFile.getName());
            String[] causes = new String[] { res.getString("SignCsrAction.NotCsr.Cause"), res.getString("SignCsrAction.CorruptedCsr.Cause") };
            Problem problem = new Problem(problemStr, causes, ex);
            DProblem dProblem = new DProblem(frame, res.getString("SignCsrAction.ProblemOpeningCsr.Title"), problem);
            dProblem.setLocationRelativeTo(frame);
            dProblem.setVisible(true);
            return;
        }
        X509Certificate[] signingChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        X509Certificate signingCert = signingChain[0];
        PublicKey publicKey = null;
        X500Name subject = null;
        DSignCsr dSignCsr = null;
        Provider provider = history.getExplicitProvider();
        if (pkcs10Csr != null) {
            publicKey = new JcaPKCS10CertificationRequest(pkcs10Csr).getPublicKey();
            subject = pkcs10Csr.getSubject();
            dSignCsr = new DSignCsr(frame, pkcs10Csr, csrFile, privateKey, keyPairType, signingCert, provider);
        } else {
            publicKey = spkacCsr.getPublicKey();
            subject = spkacCsr.getSubject().getName();
            dSignCsr = new DSignCsr(frame, spkacCsr, csrFile, privateKey, keyPairType, signingCert, provider);
        }
        dSignCsr.setLocationRelativeTo(frame);
        dSignCsr.setVisible(true);
        X509CertificateVersion version = dSignCsr.getVersion();
        SignatureType signatureType = dSignCsr.getSignatureType();
        Date validityStart = dSignCsr.getValidityStart();
        Date validityEnd = dSignCsr.getValidityEnd();
        BigInteger serialNumber = dSignCsr.getSerialNumber();
        caReplyFile = dSignCsr.getCaReplyFile();
        X509ExtensionSet extensions = dSignCsr.getExtensions();
        if (version == null) {
            return;
        }
        X500Name issuer = X500NameUtils.x500PrincipalToX500Name(signingCert.getSubjectX500Principal());
        // CA Reply is a cert with subject from CSR and issuer from signing cert's subject
        X509CertificateGenerator generator = new X509CertificateGenerator(version);
        X509Certificate caReplyCert = generator.generate(subject, issuer, validityStart, validityEnd, publicKey, privateKey, signatureType, serialNumber, extensions, provider);
        X509Certificate[] caReplyChain = new X509Certificate[signingChain.length + 1];
        caReplyChain[0] = caReplyCert;
        // Add all of the signing chain to the reply
        System.arraycopy(signingChain, 0, caReplyChain, 1, signingChain.length);
        byte[] caCertEncoded = X509CertUtil.getCertsEncodedPkcs7(caReplyChain);
        fos = new FileOutputStream(caReplyFile);
        fos.write(caCertEncoded);
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("SignJarAction.NoWriteFile.message"), caReplyFile), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.WARNING_MESSAGE);
        return;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return;
    } finally {
        IOUtils.closeQuietly(fos);
    }
    JOptionPane.showMessageDialog(frame, res.getString("SignCsrAction.SignCsrSuccessful.message"), res.getString("SignCsrAction.SignCsr.Title"), JOptionPane.INFORMATION_MESSAGE);
}
Also used : KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) FileNotFoundException(java.io.FileNotFoundException) X500Name(org.bouncycastle.asn1.x500.X500Name) X509CertificateGenerator(org.kse.crypto.x509.X509CertificateGenerator) X509CertificateVersion(org.kse.crypto.x509.X509CertificateVersion) KeyPairType(org.kse.crypto.keypair.KeyPairType) Password(org.kse.crypto.Password) PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) DSignCsr(org.kse.gui.dialogs.sign.DSignCsr) KeyStoreState(org.kse.utilities.history.KeyStoreState) JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) PublicKey(java.security.PublicKey) SignatureType(org.kse.crypto.signing.SignatureType) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) DProblem(org.kse.gui.error.DProblem) X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) Provider(java.security.Provider) X509ExtensionSet(org.kse.crypto.x509.X509ExtensionSet) Spkac(org.kse.crypto.csr.spkac.Spkac) FileOutputStream(java.io.FileOutputStream) CryptoFileType(org.kse.crypto.filetype.CryptoFileType) BigInteger(java.math.BigInteger) Problem(org.kse.gui.error.Problem) DProblem(org.kse.gui.error.DProblem) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 5 with DProblem

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

the class SetKeyPairPasswordAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    String alias = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        KeyStoreState newState = currentState.createBasisForNextState(this);
        KeyStore keyStore = newState.getKeyStore();
        alias = kseFrame.getSelectedEntryAlias();
        Password oldPassword = newState.getEntryPassword(alias);
        DChangePassword dChangePassword = new DChangePassword(frame, DOCUMENT_MODAL, res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), oldPassword, applicationSettings.getPasswordQualityConfig());
        dChangePassword.setLocationRelativeTo(frame);
        dChangePassword.setVisible(true);
        if (oldPassword == null) {
            oldPassword = dChangePassword.getOldPassword();
        }
        Password newPassword = dChangePassword.getNewPassword();
        if ((oldPassword == null) || (newPassword == null)) {
            return;
        }
        // Change the password by recreating the entry
        Certificate[] certs = keyStore.getCertificateChain(alias);
        certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(certs));
        Key privateKey = keyStore.getKey(alias, oldPassword.toCharArray());
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
        keyStore.setKeyEntry(alias, privateKey, newPassword.toCharArray(), certs);
        if (currentState.getEntryPassword(alias) == null) {
            currentState.setEntryPassword(alias, oldPassword);
        }
        newState.setEntryPassword(alias, newPassword);
        currentState.append(newState);
        kseFrame.updateControls(true);
        JOptionPane.showMessageDialog(frame, res.getString("SetKeyPairPasswordAction.SetKeyPairPasswordSuccessful.message"), res.getString("SetKeyPairPasswordAction.SetKeyPairPassword.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (GeneralSecurityException ex) {
        String problemStr = MessageFormat.format(res.getString("SetKeyPairPasswordAction.NoSetPasswordKeyPairEntry.Problem"), alias);
        String[] causes = new String[] { res.getString("SetKeyPairPasswordAction.PasswordIncorrectKeyPairEntry.Cause"), res.getString("SetKeyPairPasswordAction.NotSupportedAlgorithmKeyPairEntry.Cause") };
        Problem problem = new Problem(problemStr, causes, ex);
        DProblem dProblem = new DProblem(frame, res.getString("SetKeyPairPasswordAction.ProblemSettingPasswordKeyPairEntry.Title"), problem);
        dProblem.setLocationRelativeTo(frame);
        dProblem.setVisible(true);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) GeneralSecurityException(java.security.GeneralSecurityException) KeyStore(java.security.KeyStore) DProblem(org.kse.gui.error.DProblem) GeneralSecurityException(java.security.GeneralSecurityException) DProblem(org.kse.gui.error.DProblem) Problem(org.kse.gui.error.Problem) DChangePassword(org.kse.gui.password.DChangePassword) Key(java.security.Key) DChangePassword(org.kse.gui.password.DChangePassword) Password(org.kse.crypto.Password) Certificate(java.security.cert.Certificate)

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