Search in sources :

Example 71 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project cloudstack by apache.

the class SAMLUtils method loadPublicKey.

public static PublicKey loadPublicKey(String publicKey) {
    byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes);
    KeyFactory keyFact = SAMLUtils.getKeyFactory();
    if (keyFact == null)
        return null;
    try {
        return keyFact.generatePublic(x509KeySpec);
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 72 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project robovm by robovm.

the class SecretKeyFactoryTest method test_PBKDF2_required_parameters.

public void test_PBKDF2_required_parameters() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    // PBEKeySpecs password only constructor
    try {
        KeySpec ks = new PBEKeySpec(null);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0]);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    // PBEKeySpecs constructor without key length
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS, KEY_LENGTH);
    factory.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 73 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project robovm by robovm.

the class KeyFactoryTest method testGeneratePublic.

@SuppressWarnings("unchecked")
public void testGeneratePublic() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    try {
        TestPublicKey key = new TestPublicKey();
        TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
        PublicKey publicKey = factory.generatePublic(keySpec);
        assertNotNull(publicKey);
        assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
    } catch (InvalidKeySpecException e) {
        fail("unexpected exception: " + e);
    }
    KeySpec[] keySpecs = { new TestPrivateKeySpec(new TestPrivateKey()), null, new DSAPublicKeySpec(null, null, null, null) };
    Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
    for (int i = 0; i < keySpecs.length; i++) {
        KeySpec keySpec = keySpecs[i];
        String message = "generatePublic(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
        try {
            PublicKey generatePublic = factory.generatePublic(keySpec);
            assertNotNull(generatePublic);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 74 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project robovm by robovm.

the class KeyFactory method engineGeneratePrivate.

protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof PKCS8EncodedKeySpec) {
        try {
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec) keySpec).getEncoded());
            PrivateKey key = BouncyCastleProvider.getPrivateKey(info);
            if (key != null) {
                return key;
            }
            throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm());
        } catch (Exception e) {
            throw new InvalidKeySpecException(e.toString());
        }
    }
    throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName());
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException)

Example 75 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project robovm by robovm.

the class SignatureTest method testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure.

public void testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, null, RSA_2048_primeP, RSA_2048_primeQ, null, null, null);
    // Failing on this key early is okay.
    final PrivateKey privKey;
    try {
        privKey = kf.generatePrivate(keySpec);
    } catch (NullPointerException e) {
        return;
    } catch (InvalidKeySpecException e) {
        return;
    }
    Signature sig = Signature.getInstance("SHA1withRSA");
    try {
        sig.initSign(privKey);
        fail("Should throw error when private exponent is not available");
    } catch (InvalidKeyException expected) {
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Aggregations

InvalidKeySpecException (java.security.spec.InvalidKeySpecException)237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)147 KeyFactory (java.security.KeyFactory)99 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)93 InvalidKeyException (java.security.InvalidKeyException)62 PublicKey (java.security.PublicKey)57 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)56 IOException (java.io.IOException)51 PrivateKey (java.security.PrivateKey)40 SecretKeyFactory (javax.crypto.SecretKeyFactory)30 PBEKeySpec (javax.crypto.spec.PBEKeySpec)27 SignatureException (java.security.SignatureException)22 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 KeySpec (java.security.spec.KeySpec)19 BadPaddingException (javax.crypto.BadPaddingException)19 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)19 BigInteger (java.math.BigInteger)17 SecretKeySpec (javax.crypto.spec.SecretKeySpec)16 NoSuchProviderException (java.security.NoSuchProviderException)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)15