Search in sources :

Example 36 with CryptoException

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

the class DViewPrivateKey method pemEncodingPressed.

private void pemEncodingPressed() {
    try {
        DViewPem dViewCsrPem = new DViewPem(this, res.getString("DViewPrivateKey.Pem.Title"), privateKey);
        dViewCsrPem.setLocationRelativeTo(this);
        dViewCsrPem.setVisible(true);
    } catch (CryptoException ex) {
        DError dError = new DError(this, ex);
        dError.setLocationRelativeTo(this);
        dError.setVisible(true);
    }
}
Also used : CryptoException(org.kse.crypto.CryptoException) DError(org.kse.gui.error.DError)

Example 37 with CryptoException

use of org.kse.crypto.CryptoException 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 38 with CryptoException

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

the class KseFrame method updateControls.

/**
 * Update the frame's controls dependent on the state of its open and active
 * KeyStores.
 *
 * @param keyStoreContentsChanged
 *            Have the active KeyStore's contents changed?
 */
public void updateControls(boolean keyStoreContentsChanged) {
    KeyStoreHistory history = getActiveKeyStoreHistory();
    if (history == null) {
        updateControlsNoKeyStoresOpen();
        return;
    }
    KeyStoreState currentState = history.getCurrentState();
    // Reload KeyStore in table if it has changed
    if (keyStoreContentsChanged) {
        try {
            String selectedAlias = getSelectedEntryAlias();
            ((KeyStoreTableModel) getActiveKeyStoreTable().getModel()).load(history);
            // Loading the model loses the selected entry - preserve it
            if (selectedAlias != null) {
                setSelectedEntryByAlias(selectedAlias);
            }
        } catch (GeneralSecurityException ex) {
            DError.displayError(frame, ex);
        } catch (CryptoException ex) {
            DError.displayError(frame, ex);
        }
    }
    // Can save if active KeyStore has not been saved
    if (!currentState.isSavedState()) {
        saveAction.setEnabled(true);
    } else {
        saveAction.setEnabled(false);
    }
    // Can save all if any KeyStore has been changed since saved
    boolean saveAll = false;
    for (int i = 0; i < histories.size(); i++) {
        KeyStoreHistory h = histories.get(i);
        if (!h.getCurrentState().isSavedState()) {
            // Yes - can Save All
            saveAll = true;
            break;
        }
    }
    saveAllAction.setEnabled(saveAll);
    // Can close
    closeAction.setEnabled(true);
    closeAllAction.setEnabled(true);
    // Can close others?
    if (jkstpKeyStores.getTabCount() > 1) {
        closeOthersAction.setEnabled(true);
    } else {
        closeOthersAction.setEnabled(false);
    }
    KeyStore keyStore = currentState.getKeyStore();
    KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());
    // Can Save As
    if (type.isFileBased()) {
        saveAsAction.setEnabled(true);
    }
    // May be able to undo/redo
    updateUndoRedoControls(currentState);
    // May be able to cut/copy/paste
    if (type.isFileBased()) {
        updateCutCopyPasteControls();
    }
    // Can use tools on
    generateKeyPairAction.setEnabled(true);
    generateSecretKeyAction.setEnabled(type.supportsKeyEntries());
    importTrustedCertificateAction.setEnabled(true);
    importKeyPairAction.setEnabled(true);
    propertiesAction.setEnabled(true);
    if (type.isFileBased()) {
        setPasswordAction.setEnabled(true);
    }
    // Show default status bar display
    setDefaultStatusBarText();
    // Passwords, and therefore unlocking, are not relevant for PKCS #12 or KeyStores that are not file-based
    if (!type.hasEntryPasswords() || !type.isFileBased()) {
        unlockKeyPairAction.setEnabled(false);
        setKeyPairPasswordAction.setEnabled(false);
        unlockKeyAction.setEnabled(false);
        setKeyPasswordAction.setEnabled(false);
    } else {
        unlockKeyPairAction.setEnabled(true);
        setKeyPairPasswordAction.setEnabled(true);
        unlockKeyAction.setEnabled(true);
        setKeyPasswordAction.setEnabled(true);
    }
    // Special restrictions for MSCAPI and PKCS#11 type
    if (type == KeyStoreType.MS_CAPI_PERSONAL || type == KeyStoreType.PKCS11) {
        keyPairPrivateKeyDetailsAction.setEnabled(false);
        keyDetailsAction.setEnabled(false);
        renameKeyAction.setEnabled(false);
        renameKeyPairAction.setEnabled(false);
        renameTrustedCertificateAction.setEnabled(false);
        exportKeyPairAction.setEnabled(false);
        exportKeyPairPrivateKeyAction.setEnabled(false);
        jmKeyPairEditCertChain.setEnabled(false);
        appendToCertificateChainAction.setEnabled(false);
        removeFromCertificateChainAction.setEnabled(false);
        // "UnsupportedOperationException" ...
        jmKeyPairImportCaReply.setEnabled(false);
    } else {
        keyPairPrivateKeyDetailsAction.setEnabled(true);
        keyDetailsAction.setEnabled(true);
        renameKeyAction.setEnabled(true);
        renameKeyPairAction.setEnabled(true);
        renameTrustedCertificateAction.setEnabled(true);
        exportKeyPairAction.setEnabled(true);
        exportKeyPairPrivateKeyAction.setEnabled(true);
        jmKeyPairEditCertChain.setEnabled(true);
        appendToCertificateChainAction.setEnabled(true);
        removeFromCertificateChainAction.setEnabled(true);
        jmKeyPairImportCaReply.setEnabled(true);
    }
    // KeyStore type menu items
    if (type.isFileBased()) {
        jmChangeType.setEnabled(true);
        if (type == JKS) {
            jrbmiChangeTypeJks.setSelected(true);
            jrbmiKeyStoreChangeTypeJks.setSelected(true);
        } else if (type == JCEKS) {
            jrbmiChangeTypeJceks.setSelected(true);
            jrbmiKeyStoreChangeTypeJceks.setSelected(true);
        } else if (type == PKCS12) {
            jrbmiChangeTypePkcs12.setSelected(true);
            jrbmiKeyStoreChangeTypePkcs12.setSelected(true);
        } else if (type == BKS_V1) {
            jrbmiChangeTypeBksV1.setSelected(true);
            jrbmiKeyStoreChangeTypeBksV1.setSelected(true);
        } else if (type == BKS) {
            jrbmiChangeTypeBks.setSelected(true);
            jrbmiKeyStoreChangeTypeBks.setSelected(true);
        } else {
            jrbmiChangeTypeUber.setSelected(true);
            jrbmiKeyStoreChangeTypeUber.setSelected(true);
        }
    } else {
        jmKeyStoreChangeType.setEnabled(false);
    }
    // Show KeyStores tabbed pane
    frame.getContentPane().remove(jQuickStart);
    frame.getContentPane().add(jkstpKeyStores, BorderLayout.CENTER);
    updateKeyStoreTabsText();
    updateApplicationTitle();
    frame.repaint();
}
Also used : KeyStoreState(org.kse.utilities.history.KeyStoreState) KeyStoreHistory(org.kse.utilities.history.KeyStoreHistory) KeyStoreType(org.kse.crypto.keystore.KeyStoreType) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore) Point(java.awt.Point)

Example 39 with CryptoException

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

the class X509CertUtil method loadCRL.

/**
 * Load a CRL from the specified stream.
 *
 * @param is
 *            Stream to load CRL from
 * @return The CRL
 * @throws CryptoException
 *             Problem encountered while loading the CRL
 */
public static X509CRL loadCRL(InputStream is) throws CryptoException {
    try {
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE);
        X509CRL crl = (X509CRL) cf.generateCRL(is);
        return crl;
    } catch (CertificateException ex) {
        throw new CryptoException(res.getString("NoLoadCrl.exception.message"), ex);
    } catch (CRLException ex) {
        throw new CryptoException(res.getString("NoLoadCrl.exception.message"), ex);
    } finally {
        IOUtils.closeQuietly(is);
    }
}
Also used : X509CRL(java.security.cert.X509CRL) CertificateException(java.security.cert.CertificateException) CryptoException(org.kse.crypto.CryptoException) CertificateFactory(java.security.cert.CertificateFactory) CRLException(java.security.cert.CRLException)

Example 40 with CryptoException

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

the class X509CertUtil method getCertsEncodedPkiPath.

/**
 * PKI Path encode a number of certificates.
 *
 * @return The encoding
 * @param certs
 *            The certificates
 * @throws CryptoException
 *             If there was a problem encoding the certificates
 */
public static byte[] getCertsEncodedPkiPath(X509Certificate[] certs) throws CryptoException {
    try {
        ArrayList<Certificate> encodedCerts = new ArrayList<Certificate>();
        Collections.addAll(encodedCerts, certs);
        CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce());
        CertPath cp = cf.generateCertPath(encodedCerts);
        return cp.getEncoded(PKI_PATH_ENCODING);
    } catch (CertificateException | NoSuchProviderException e) {
        throw new CryptoException(res.getString("NoPkcs7Encode.exception.message"), e);
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertPath(java.security.cert.CertPath) NoSuchProviderException(java.security.NoSuchProviderException) CryptoException(org.kse.crypto.CryptoException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

CryptoException (org.kse.crypto.CryptoException)80 GeneralSecurityException (java.security.GeneralSecurityException)22 IOException (java.io.IOException)21 X509Certificate (java.security.cert.X509Certificate)21 KeyStore (java.security.KeyStore)16 KeyStoreException (java.security.KeyStoreException)13 BigInteger (java.math.BigInteger)11 DError (org.kse.gui.error.DError)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 File (java.io.File)9 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)9 ByteBuffer (java.nio.ByteBuffer)8 CertificateException (java.security.cert.CertificateException)8 PrivateKey (java.security.PrivateKey)7 KeyFactory (java.security.KeyFactory)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 CertificateFactory (java.security.cert.CertificateFactory)6 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)6 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)6 Cipher (javax.crypto.Cipher)6