Search in sources :

Example 6 with SignatureType

use of org.kse.crypto.signing.SignatureType in project keystore-explorer by kaikramer.

the class DGenerateCsr method initComponents.

private void initComponents() throws CryptoException {
    jlFormat = new JLabel(res.getString("DGenerateCsr.jlFormat.text"));
    jrbPkcs10 = new JRadioButton(res.getString("DGenerateCsr.jrbPkcs10.text"), false);
    PlatformUtil.setMnemonic(jrbPkcs10, res.getString("DGenerateCsr.jrbPkcs10.mnemonic").charAt(0));
    jrbPkcs10.setToolTipText(res.getString("DGenerateCsr.jrbPkcs10.tooltip"));
    jrbSpkac = new JRadioButton(res.getString("DGenerateCsr.jrbSpkac.text"), true);
    PlatformUtil.setMnemonic(jrbSpkac, res.getString("DGenerateCsr.jrbSpkac.mnemonic").charAt(0));
    jrbSpkac.setToolTipText(res.getString("DGenerateCsr.jrbSpkac.tooltip"));
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(jrbPkcs10);
    buttonGroup.add(jrbSpkac);
    jrbPkcs10.setSelected(true);
    jlSignatureAlgorithm = new JLabel(res.getString("DGenerateCsr.jlSignatureAlgorithm.text"));
    jcbSignatureAlgorithm = new JComboBox<SignatureType>();
    jcbSignatureAlgorithm.setMaximumRowCount(10);
    jcbSignatureAlgorithm.setToolTipText(res.getString("DGenerateCsr.jcbSignatureAlgorithm.tooltip"));
    DialogHelper.populateSigAlgs(keyPairType, privateKey, provider, jcbSignatureAlgorithm);
    jlChallenge = new JLabel(res.getString("DGenerateCsr.jlChallenge.text"));
    jtfChallenge = new JTextField(15);
    jtfChallenge.setToolTipText(res.getString("DGenerateCsr.jtfChallenge.tooltip"));
    jlUnstructuredName = new JLabel(res.getString("DGenerateCsr.jlUnstructuredName.text"));
    jtfUnstructuredName = new JTextField(30);
    jtfUnstructuredName.setToolTipText(res.getString("DGenerateCsr.jtfUnstructuredName.tooltip"));
    jlExtensions = new JLabel(res.getString("DGenerateCsr.jlExtensions.text"));
    jcbExtensions = new JCheckBox(res.getString("DGenerateCsr.jcbExtensions.text"));
    jcbExtensions.setToolTipText(res.getString("DGenerateCsr.jcbExtensions.tooltip"));
    jlCsrFile = new JLabel(res.getString("DGenerateCsr.jlCsrFile.text"));
    jtfCsrFile = new JTextField(30);
    jtfCsrFile.setToolTipText(res.getString("DGenerateCsr.jtfCsrFile.tooltip"));
    populateCsrFileName();
    jbBrowse = new JButton(res.getString("DGenerateCsr.jbBrowse.text"));
    jbBrowse.setToolTipText(res.getString("DGenerateCsr.jbBrowse.tooltip"));
    PlatformUtil.setMnemonic(jbBrowse, res.getString("DGenerateCsr.jbBrowse.mnemonic").charAt(0));
    jbOK = new JButton(res.getString("DGenerateCsr.jbOK.text"));
    jbCancel = new JButton(res.getString("DGenerateCsr.jbCancel.text"));
    // layout
    Container pane = getContentPane();
    pane.setLayout(new MigLayout("insets dialog, fill", "[right]unrel[]", "[]unrel[]"));
    pane.add(jlFormat, "");
    pane.add(jrbPkcs10, "split 2");
    pane.add(jrbSpkac, "wrap");
    pane.add(jlSignatureAlgorithm, "");
    pane.add(jcbSignatureAlgorithm, "wrap");
    pane.add(jlChallenge, "");
    pane.add(jtfChallenge, "wrap");
    pane.add(jlUnstructuredName, "");
    pane.add(jtfUnstructuredName, "wrap");
    // pane.add(jlExtensions, "");
    pane.add(jcbExtensions, "skip, wrap");
    pane.add(jlCsrFile, "");
    pane.add(jtfCsrFile, "");
    pane.add(jbBrowse, "wrap");
    pane.add(new JSeparator(), "spanx, growx, wrap 15:push");
    pane.add(jbCancel, "spanx, split 2, tag cancel");
    pane.add(jbOK, "tag ok");
    // actions
    jrbPkcs10.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            // unstructured name and extensions are Pkcs10-only
            if (jrbPkcs10.isSelected()) {
                jlUnstructuredName.setEnabled(true);
                jtfUnstructuredName.setEnabled(true);
                jlExtensions.setEnabled(true);
                jcbExtensions.setEnabled(true);
            } else {
                jlUnstructuredName.setEnabled(false);
                jtfUnstructuredName.setEnabled(false);
                jlExtensions.setEnabled(false);
                jcbExtensions.setEnabled(false);
            }
        }
    });
    jbBrowse.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                CursorUtil.setCursorBusy(DGenerateCsr.this);
                browsePressed();
            } finally {
                CursorUtil.setCursorFree(DGenerateCsr.this);
            }
        }
    });
    jbOK.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            okPressed();
        }
    });
    jbCancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });
    jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY);
    jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent evt) {
            closeDialog();
        }
    });
    setResizable(false);
    getRootPane().setDefaultButton(jbOK);
    pack();
}
Also used : JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) MigLayout(net.miginfocom.swing.MigLayout) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) WindowAdapter(java.awt.event.WindowAdapter) SignatureType(org.kse.crypto.signing.SignatureType) JTextField(javax.swing.JTextField) JSeparator(javax.swing.JSeparator) JCheckBox(javax.swing.JCheckBox) Container(java.awt.Container) ChangeEvent(javax.swing.event.ChangeEvent) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) WindowEvent(java.awt.event.WindowEvent) ChangeListener(javax.swing.event.ChangeListener) AbstractAction(javax.swing.AbstractAction)

Example 7 with SignatureType

use of org.kse.crypto.signing.SignatureType in project keystore-explorer by kaikramer.

the class DGenerateKeyPairCert method generateCertificate.

private boolean generateCertificate() {
    Date validityStart = jdtValidityStart.getDateTime();
    Date validityEnd = jdtValidityEnd.getDateTime();
    String serialNumberStr = jtfSerialNumber.getText().trim();
    if (serialNumberStr.length() == 0) {
        JOptionPane.showMessageDialog(this, res.getString("DGenerateKeyPairCert.ValReqSerialNumber.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return false;
    }
    BigInteger serialNumber;
    try {
        serialNumber = new BigInteger(serialNumberStr);
        if (serialNumber.compareTo(BigInteger.ONE) < 0) {
            JOptionPane.showMessageDialog(this, res.getString("DGenerateKeyPairCert.SerialNumberNonZero.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        }
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(this, res.getString("DGenerateKeyPairCert.SerialNumberNotInteger.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return false;
    }
    X500Name x500Name = jdnName.getDistinguishedName();
    if (x500Name == null || x500Name.toString().isEmpty()) {
        JOptionPane.showMessageDialog(this, res.getString("DGenerateKeyPairCert.NameValueReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
        return false;
    }
    try {
        SignatureType signatureType = ((SignatureType) jcbSignatureAlgorithm.getSelectedItem());
        X509CertificateGenerator generator;
        if (jrbVersion1.isSelected()) {
            generator = new X509CertificateGenerator(VERSION1);
        } else {
            generator = new X509CertificateGenerator(VERSION3);
        }
        // self-signed or signed by other key pair?
        if (issuerPrivateKey == null) {
            certificate = generator.generateSelfSigned(x500Name, validityStart, validityEnd, keyPair.getPublic(), keyPair.getPrivate(), signatureType, serialNumber, extensions, provider);
        } else {
            certificate = generator.generate(x500Name, X500NameUtils.x500PrincipalToX500Name(issuerCert.getSubjectX500Principal()), validityStart, validityEnd, keyPair.getPublic(), issuerPrivateKey, signatureType, serialNumber, extensions, provider);
        }
    } catch (CryptoException ex) {
        DError dError = new DError(this, ex);
        dError.setLocationRelativeTo(getParent());
        dError.setVisible(true);
        closeDialog();
    }
    return true;
}
Also used : BigInteger(java.math.BigInteger) X500Name(org.bouncycastle.asn1.x500.X500Name) SignatureType(org.kse.crypto.signing.SignatureType) CryptoException(org.kse.crypto.CryptoException) Date(java.util.Date) X509CertificateGenerator(org.kse.crypto.x509.X509CertificateGenerator) DError(org.kse.gui.error.DError)

Example 8 with SignatureType

use of org.kse.crypto.signing.SignatureType in project keystore-explorer by kaikramer.

the class GenerateCsrAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    File csrFile = null;
    FileOutputStream fos = null;
    try {
        KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();
        KeyStoreState currentState = history.getCurrentState();
        Provider provider = history.getExplicitProvider();
        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());
        String keyPairAlg = privateKey.getAlgorithm();
        KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
        if (keyPairType == null) {
            throw new CryptoException(MessageFormat.format(res.getString("GenerateCsrAction.NoCsrForKeyPairAlg.message"), keyPairAlg));
        }
        // determine dir of current keystore as proposal for CSR file location
        String path = CurrentDirectory.get().getAbsolutePath();
        File keyStoreFile = history.getFile();
        if (keyStoreFile != null) {
            path = keyStoreFile.getAbsoluteFile().getParent();
        }
        DGenerateCsr dGenerateCsr = new DGenerateCsr(frame, alias, privateKey, keyPairType, path, provider);
        dGenerateCsr.setLocationRelativeTo(frame);
        dGenerateCsr.setVisible(true);
        if (!dGenerateCsr.generateSelected()) {
            return;
        }
        CsrType format = dGenerateCsr.getFormat();
        SignatureType signatureType = dGenerateCsr.getSignatureType();
        String challenge = dGenerateCsr.getChallenge();
        String unstructuredName = dGenerateCsr.getUnstructuredName();
        boolean useCertificateExtensions = dGenerateCsr.isAddExtensionsWanted();
        csrFile = dGenerateCsr.getCsrFile();
        X509Certificate firstCertInChain = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias)))[0];
        fos = new FileOutputStream(csrFile);
        if (format == CsrType.PKCS10) {
            String csr = Pkcs10Util.getCsrEncodedDerPem(Pkcs10Util.generateCsr(firstCertInChain, privateKey, signatureType, challenge, unstructuredName, useCertificateExtensions, provider));
            fos.write(csr.getBytes());
        } else {
            SpkacSubject subject = new SpkacSubject(X500NameUtils.x500PrincipalToX500Name(firstCertInChain.getSubjectX500Principal()));
            PublicKey publicKey = firstCertInChain.getPublicKey();
            // TODO handle other providers (PKCS11 etc)
            Spkac spkac = new Spkac(challenge, signatureType, subject, publicKey, privateKey);
            spkac.output(fos);
        }
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(frame, MessageFormat.format(res.getString("GenerateCsrAction.NoWriteFile.message"), csrFile), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.WARNING_MESSAGE);
        return;
    } catch (Exception ex) {
        DError.displayError(frame, ex);
        return;
    } finally {
        IOUtils.closeQuietly(fos);
    }
    JOptionPane.showMessageDialog(frame, res.getString("GenerateCsrAction.CsrGenerationSuccessful.message"), res.getString("GenerateCsrAction.GenerateCsr.Title"), JOptionPane.INFORMATION_MESSAGE);
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) SpkacSubject(org.kse.crypto.csr.spkac.SpkacSubject) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) FileNotFoundException(java.io.FileNotFoundException) SignatureType(org.kse.crypto.signing.SignatureType) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) CryptoException(org.kse.crypto.CryptoException) FileNotFoundException(java.io.FileNotFoundException) Provider(java.security.Provider) CsrType(org.kse.crypto.csr.CsrType) Spkac(org.kse.crypto.csr.spkac.Spkac) FileOutputStream(java.io.FileOutputStream) DGenerateCsr(org.kse.gui.dialogs.DGenerateCsr) KeyPairType(org.kse.crypto.keypair.KeyPairType) CryptoException(org.kse.crypto.CryptoException) File(java.io.File) Password(org.kse.crypto.Password)

Example 9 with SignatureType

use of org.kse.crypto.signing.SignatureType in project keystore-explorer by kaikramer.

the class SignJarAction method doAction.

/**
 * Do action.
 */
@Override
protected void doAction() {
    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();
        Provider provider = history.getExplicitProvider();
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
        X509Certificate[] certs = X509CertUtil.orderX509CertChain(X509CertUtil.convertCertificates(keyStore.getCertificateChain(alias)));
        KeyPairType keyPairType = KeyPairUtil.getKeyPairType(privateKey);
        DSignJar dSignJar = new DSignJar(frame, privateKey, keyPairType, alias, provider);
        dSignJar.setLocationRelativeTo(frame);
        dSignJar.setVisible(true);
        SignatureType signatureType = dSignJar.getSignatureType();
        String signatureName = dSignJar.getSignatureName();
        File inputJarFile = dSignJar.getInputJar();
        File outputJarFile = dSignJar.getOutputJar();
        String tsaUrl = dSignJar.getTimestampingServerUrl();
        if (signatureType == null) {
            return;
        }
        String signer = KSE.getFullApplicationName();
        DigestType digestType = dSignJar.getDigestType();
        if (inputJarFile.equals(outputJarFile)) {
            JarSigner.sign(inputJarFile, privateKey, certs, signatureType, signatureName, signer, digestType, tsaUrl, provider);
        } else {
            JarSigner.sign(inputJarFile, outputJarFile, privateKey, certs, signatureType, signatureName, signer, digestType, tsaUrl, provider);
        }
        JOptionPane.showMessageDialog(frame, res.getString("SignJarAction.SignJarSuccessful.message"), res.getString("SignJarAction.SignJar.Title"), JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
        DError.displayError(frame, ex);
    }
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) PrivateKey(java.security.PrivateKey) SignatureType(org.kse.crypto.signing.SignatureType) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Provider(java.security.Provider) DigestType(org.kse.crypto.digest.DigestType) DSignJar(org.kse.gui.dialogs.sign.DSignJar) KeyPairType(org.kse.crypto.keypair.KeyPairType) File(java.io.File) Password(org.kse.crypto.Password)

Example 10 with SignatureType

use of org.kse.crypto.signing.SignatureType in project keystore-explorer by kaikramer.

the class DSignCsr method populatePkcs10CsrDetails.

private void populatePkcs10CsrDetails() throws CryptoException {
    jtfCsrFormat.setText(res.getString("DSignCsr.jtfCsrFormat.Pkcs10.text"));
    jtfCsrFormat.setCaretPosition(0);
    jdnCsrSubject.setDistinguishedName(pkcs10Csr.getSubject());
    try {
        csrPublicKey = new JcaPKCS10CertificationRequest(pkcs10Csr).getPublicKey();
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("DSignCsr.NoGetCsrPublicKey.message"), ex);
    }
    populatePublicKey();
    String sigAlgId = pkcs10Csr.getSignatureAlgorithm().getAlgorithm().getId();
    SignatureType sigAlg = SignatureType.resolveOid(sigAlgId);
    if (sigAlg != null) {
        jtfCsrSignatureAlgorithm.setText(sigAlg.friendly());
    } else {
        jtfCsrSignatureAlgorithm.setText(sigAlgId);
    }
    jtfCsrSignatureAlgorithm.setCaretPosition(0);
    DialogHelper.populatePkcs10Challenge(pkcs10Csr.getAttributes(), jtfCsrChallenge);
}
Also used : JcaPKCS10CertificationRequest(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequest) GeneralSecurityException(java.security.GeneralSecurityException) SignatureType(org.kse.crypto.signing.SignatureType) CryptoException(org.kse.crypto.CryptoException)

Aggregations

SignatureType (org.kse.crypto.signing.SignatureType)12 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 WindowAdapter (java.awt.event.WindowAdapter)4 WindowEvent (java.awt.event.WindowEvent)4 Date (java.util.Date)4 AbstractAction (javax.swing.AbstractAction)4 JButton (javax.swing.JButton)4 JLabel (javax.swing.JLabel)4 JTextField (javax.swing.JTextField)4 File (java.io.File)3 KeyStore (java.security.KeyStore)3 PrivateKey (java.security.PrivateKey)3 Provider (java.security.Provider)3 X509Certificate (java.security.cert.X509Certificate)3 JRadioButton (javax.swing.JRadioButton)3 ChangeEvent (javax.swing.event.ChangeEvent)3 ChangeListener (javax.swing.event.ChangeListener)3 MigLayout (net.miginfocom.swing.MigLayout)3 KeyPairType (org.kse.crypto.keypair.KeyPairType)3