Search in sources :

Example 66 with CryptoException

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

the class OpenSslPubUtil method load.

/**
 * Load an unencrypted OpenSSL public key from the stream. The encoding of
 * the public key may be PEM or DER.
 *
 * @param is
 *            Stream to load the unencrypted public key from
 * @return The public key
 * @throws CryptoException
 *             Problem encountered while loading the public key
 * @throws IOException
 *             An I/O error occurred
 */
public static PublicKey load(InputStream is) throws CryptoException, IOException {
    byte[] streamContents = ReadUtil.readFully(is);
    // Check if stream is PEM encoded
    PemInfo pemInfo = PemUtil.decode(new ByteArrayInputStream(streamContents));
    if (pemInfo != null) {
        // It is - get DER from PEM
        streamContents = pemInfo.getContent();
    }
    try {
        // DER-encoded subjectPublicKeyInfo structure - the OpenSSL format
        SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(streamContents);
        return new JcaPEMKeyConverter().getPublicKey(publicKeyInfo);
    } catch (Exception ex) {
        throw new CryptoException(res.getString("NoLoadOpenSslPublicKey.exception.message"), ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PemInfo(org.kse.utilities.pem.PemInfo) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) CryptoException(org.kse.crypto.CryptoException) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) IOException(java.io.IOException) CryptoException(org.kse.crypto.CryptoException)

Example 67 with CryptoException

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

the class SecretKeyUtil method generateSecretKey.

/**
 * Generate a secret key.
 *
 * @param secretKeyType
 *            Secret key type to generate
 * @param keySize
 *            Key size of secret key
 * @return Secret key
 * @throws CryptoException
 *             If there was a problem generating the secret key
 */
public static SecretKey generateSecretKey(SecretKeyType secretKeyType, int keySize) throws CryptoException {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(secretKeyType.jce(), BOUNCY_CASTLE.jce());
        keyGenerator.init(keySize, SecureRandom.getInstance("SHA1PRNG"));
        return keyGenerator.generateKey();
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(MessageFormat.format(res.getString("NoGenerateSecretKey.exception.message"), secretKeyType), ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException) KeyGenerator(javax.crypto.KeyGenerator)

Example 68 with CryptoException

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

the class MidletSigner method signJarDigest.

private static byte[] signJarDigest(File jarFile, RSAPrivateKey privateKey) throws CryptoException {
    // Create a SHA-1 signature for the supplied JAR file
    try (FileInputStream fis = new FileInputStream(jarFile)) {
        Signature signature = Signature.getInstance(SignatureType.SHA1_RSA.jce());
        signature.initSign(privateKey);
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = fis.read(buffer)) != -1) {
            signature.update(buffer, 0, read);
        }
        return signature.sign();
    } catch (IOException ex) {
        throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex);
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("JarDigestSignatureFailed.exception.message"), ex);
    }
}
Also used : Signature(java.security.Signature) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CryptoException(org.kse.crypto.CryptoException) FileInputStream(java.io.FileInputStream)

Example 69 with CryptoException

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

the class KeyPairUtil method validKeyPair.

/**
 * Check that the supplied private and public keys actually comprise a valid
 * key pair.
 *
 * @param privateKey
 *            Private key
 * @param publicKey
 *            Public key
 * @return True if the private and public keys comprise a valid key pair,
 *         false otherwise.
 * @throws CryptoException
 *             If there is a problem validating the key pair
 */
public static boolean validKeyPair(PrivateKey privateKey, PublicKey publicKey) throws CryptoException {
    try {
        String privateAlgorithm = privateKey.getAlgorithm();
        String publicAlgorithm = publicKey.getAlgorithm();
        if (!privateAlgorithm.equals(publicAlgorithm)) {
            return false;
        }
        // private key and verifying the signature with the public key
        if (privateAlgorithm.equals(RSA.jce())) {
            byte[] toSign = "Rivest Shamir Adleman".getBytes();
            String signatureAlgorithm = "SHA256withRSA";
            byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
            return verify(toSign, signature, publicKey, signatureAlgorithm);
        } else if (privateAlgorithm.equals(DSA.jce())) {
            byte[] toSign = "Digital Signature Algorithm".getBytes();
            String signatureAlgorithm = "SHA1withDSA";
            byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
            return verify(toSign, signature, publicKey, signatureAlgorithm);
        } else if (privateAlgorithm.equals(EC.jce())) {
            byte[] toSign = "EC Digital Signature Algorithm".getBytes();
            String signatureAlgorithm = "SHA256withECDSA";
            byte[] signature = sign(toSign, privateKey, signatureAlgorithm);
            return verify(toSign, signature, publicKey, signatureAlgorithm);
        } else {
            throw new CryptoException(MessageFormat.format(res.getString("NoCheckCompriseValidKeypairAlg.exception.message"), privateAlgorithm));
        }
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("NoCheckCompriseValidKeypair.exception.message"), ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.kse.crypto.CryptoException)

Example 70 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 public key.
 *
 * @param publicKey
 *            The public key
 * @return Key information
 * @throws CryptoException
 *             If there is a problem getting the information
 */
public static KeyInfo getKeyInfo(PublicKey publicKey) throws CryptoException {
    try {
        String algorithm = publicKey.getAlgorithm();
        if (algorithm.equals(RSA.jce())) {
            KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
            RSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
            BigInteger modulus = keySpec.getModulus();
            return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
        } else if (algorithm.equals(DSA.jce())) {
            KeyFactory keyFact = KeyFactory.getInstance(algorithm);
            DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class);
            BigInteger prime = keySpec.getP();
            return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
        } else if (algorithm.equals(EC.jce())) {
            ECPublicKey pubk = (ECPublicKey) publicKey;
            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("NoPublicKeysize.exception.message"), ex);
    }
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) KeyInfo(org.kse.crypto.KeyInfo) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) CryptoException(org.kse.crypto.CryptoException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

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