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;
}
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);
}
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]);
}
}
}
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());
}
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) {
}
}
Aggregations