Search in sources :

Example 26 with CryptoException

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

the class KeyPairUtil method generateKeyPair.

/**
 * Generate a key pair.
 *
 * @param keyPairType
 *            Key pair type to generate
 * @param keySize
 *            Key size of key pair
 * @return A keypair
 * @param provider
 *         Crypto provider used for key generation
 * @throws CryptoException
 *             If there was a problem generating the key pair
 */
public static KeyPair generateKeyPair(KeyPairType keyPairType, int keySize, Provider provider) throws CryptoException {
    try {
        // Get a key pair generator
        KeyPairGenerator keyPairGen = null;
        if (provider != null) {
            keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce(), provider);
        } else {
            // Always use BC provider for RSA
            if (keyPairType == RSA) {
                keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce(), BOUNCY_CASTLE.jce());
            } else {
                // Use default provider for DSA
                keyPairGen = KeyPairGenerator.getInstance(keyPairType.jce());
            }
        }
        // Create a SecureRandom
        SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
        // Initialise key pair generator with key strength and randomness
        keyPairGen.initialize(keySize, rand);
        // Generate and return the key pair
        KeyPair keyPair = keyPairGen.generateKeyPair();
        return keyPair;
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(MessageFormat.format(res.getString("NoGenerateKeypair.exception.message"), keyPairType), ex);
    }
}
Also used : KeyPair(java.security.KeyPair) GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) CryptoException(org.kse.crypto.CryptoException)

Example 27 with CryptoException

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

the class KeyPairUtil method getKeyInfo.

/**
 * Get the information about the supplied private key.
 *
 * @param privateKey
 *            The private key
 * @return Key information
 * @throws CryptoException
 *             If there is a problem getting the information
 */
public static KeyInfo getKeyInfo(PrivateKey privateKey) throws CryptoException {
    try {
        String algorithm = privateKey.getAlgorithm();
        if (algorithm.equals(RSA.jce())) {
            if (privateKey instanceof RSAPrivateKey) {
                // Using default provider does not work for BKS and UBER resident private keys
                KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
                RSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, RSAPrivateKeySpec.class);
                BigInteger modulus = keySpec.getModulus();
                return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
            } else {
                return new KeyInfo(ASYMMETRIC, algorithm, 0);
            }
        } else if (algorithm.equals(DSA.jce())) {
            // Use SUN (DSA key spec not implemented for BC)
            KeyFactory keyFact = KeyFactory.getInstance(algorithm);
            DSAPrivateKeySpec keySpec = keyFact.getKeySpec(privateKey, DSAPrivateKeySpec.class);
            BigInteger prime = keySpec.getP();
            return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
        } else if (algorithm.equals(EC.jce()) || algorithm.equals(ECDSA.jce())) {
            ECPrivateKey pubk = (ECPrivateKey) privateKey;
            int size = pubk.getParams().getOrder().bitLength();
            return new KeyInfo(ASYMMETRIC, algorithm, size);
        }
        // size unknown
        return new KeyInfo(ASYMMETRIC, algorithm);
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("NoPrivateKeysize.exception.message"), ex);
    }
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeyInfo(org.kse.crypto.KeyInfo) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) CryptoException(org.kse.crypto.CryptoException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 28 with CryptoException

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

the class KeyStoreUtil method copy.

/**
 * Copy a KeyStore.
 *
 * @param keyStore
 *            KeyStore to copy
 * @return Copy
 * @throws CryptoException
 *             Problem encountered copying the KeyStore
 */
public static KeyStore copy(KeyStore keyStore) throws CryptoException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        char[] emptyPassword = {};
        keyStore.store(baos, emptyPassword);
        KeyStore theCopy = KeyStoreUtil.create(KeyStoreType.resolveJce(keyStore.getType()));
        theCopy.load(new ByteArrayInputStream(baos.toByteArray()), emptyPassword);
        return theCopy;
    } catch (CryptoException ex) {
        throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
    } catch (IllegalStateException ex) {
        throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
    } catch (IOException ex) {
        throw new CryptoException(res.getString("NoCopyKeyStore.exception.message"), ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) GeneralSecurityException(java.security.GeneralSecurityException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore)

Example 29 with CryptoException

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

the class DImportKeyPairPkcs8 method certificateDetailsPressed.

private void certificateDetailsPressed() {
    try {
        X509Certificate[] certs = loadCertificates();
        if ((certs != null) && (certs.length != 0)) {
            String path = new File(jtfCertificatePath.getText()).getName();
            DViewCertificate dViewCertificate = new DViewCertificate(this, MessageFormat.format(res.getString("DImportKeyPairPkcs8.ViewCertificateDetails.Title"), path), certs, null, DViewCertificate.NONE);
            dViewCertificate.setLocationRelativeTo(this);
            dViewCertificate.setVisible(true);
        }
    } catch (CryptoException ex) {
        DError.displayError(this, ex);
    }
}
Also used : DViewCertificate(org.kse.gui.dialogs.DViewCertificate) CryptoException(org.kse.crypto.CryptoException) File(java.io.File) X509Certificate(java.security.cert.X509Certificate)

Example 30 with CryptoException

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

the class DProperties method createTrustedCertificatesNodes.

private void createTrustedCertificatesNodes(DefaultMutableTreeNode parentNode) throws CryptoException {
    try {
        KeyStore keyStore = currentState.getKeyStore();
        TreeSet<String> aliases = getAliasesInAlphaOrder();
        DefaultMutableTreeNode trustCertsNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.TrustedCertificates"));
        parentNode.add(trustCertsNode);
        boolean trustCertsPresent = false;
        for (String alias : aliases) {
            if (KeyStoreUtil.isTrustedCertificateEntry(alias, keyStore)) {
                createTrustedCertificateNodes(trustCertsNode, alias);
                trustCertsPresent = true;
            }
        }
        if (!trustCertsPresent) {
            DefaultMutableTreeNode emptyTrustCertsNode = new DefaultMutableTreeNode(res.getString("DProperties.properties.None"));
            trustCertsNode.add(emptyTrustCertsNode);
        }
    } catch (KeyStoreException ex) {
        throw new CryptoException(res.getString("DProperties.NoGetProperties.exception.message"), ex);
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) KeyStoreException(java.security.KeyStoreException) CryptoException(org.kse.crypto.CryptoException) KeyStore(java.security.KeyStore)

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