use of java.security.NoSuchProviderException in project robovm by robovm.
the class SecureRandomTest method testGetInstanceStringString.
/*
* Class under test for SecureRandom getInstance(String, String)
*/
public final void testGetInstanceStringString() throws Exception {
SecureRandom sr = SecureRandom.getInstance("someRandom", "SRProvider");
if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
fail("getInstance failed");
}
try {
SecureRandom r = SecureRandom.getInstance("anotherRandom", "SRProvider");
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (NoSuchProviderException e) {
fail("unexpected: " + e);
} catch (IllegalArgumentException e) {
fail("unexpected: " + e);
} catch (NullPointerException e) {
fail("unexpected: " + e);
}
try {
SecureRandom r = SecureRandom.getInstance("someRandom", "UnknownProvider");
fail("expected NoSuchProviderException");
} catch (NoSuchProviderException e) {
// ok
} catch (NoSuchAlgorithmException e) {
fail("unexpected: " + e);
} catch (IllegalArgumentException e) {
fail("unexpected: " + e);
} catch (NullPointerException e) {
fail("unexpected: " + e);
}
try {
SecureRandom r = SecureRandom.getInstance("someRandom", (String) null);
fail("expected IllegalArgumentException");
} catch (NoSuchProviderException e) {
fail("unexpected: " + e);
} catch (NoSuchAlgorithmException e) {
fail("unexpected: " + e);
} catch (IllegalArgumentException e) {
// ok
} catch (NullPointerException e) {
fail("unexpected: " + e);
}
try {
SecureRandom r = SecureRandom.getInstance(null, "SRProvider");
fail("expected NullPointerException");
} catch (NoSuchProviderException e) {
fail("unexpected: " + e);
} catch (NoSuchAlgorithmException e) {
fail("unexpected: " + e);
} catch (IllegalArgumentException e) {
fail("unexpected: " + e);
} catch (NullPointerException e) {
// ok
}
}
use of java.security.NoSuchProviderException in project robovm by robovm.
the class X509CertificateObject method verify.
public final void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
Signature signature;
String sigName = X509SignatureUtil.getSignatureName(c.getSignatureAlgorithm());
try {
signature = Signature.getInstance(sigName, BouncyCastleProvider.PROVIDER_NAME);
} catch (Exception e) {
signature = Signature.getInstance(sigName);
}
checkSignature(key, signature);
}
use of java.security.NoSuchProviderException in project robovm by robovm.
the class SealedObjectTest method testGetObject3.
/**
* getObject(Key key, String provider) method testing. Tests if the proper
* exception is thrown in the case of incorrect input parameters and if the
* object sealed with encryption algorithm can be retrieved by specifying
* the cryptographic key and provider name.
*/
public void testGetObject3() throws Exception {
try {
new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), null);
fail("IllegalArgumentException should be thrown in the case of " + "null provider.");
} catch (IllegalArgumentException e) {
}
try {
new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), "");
fail("IllegalArgumentException should be thrown in the case of " + "empty provider.");
} catch (IllegalArgumentException e) {
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
String provider = cipher.getProvider().getName();
cipher.init(Cipher.ENCRYPT_MODE, key);
String secret = "secret string";
SealedObject so = new SealedObject(secret, cipher);
cipher.init(Cipher.DECRYPT_MODE, key);
assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(key, provider));
kg = KeyGenerator.getInstance("DESede");
key = kg.generateKey();
try {
so.getObject(key, provider);
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
so.getObject(key, "Wrong provider name");
fail("NoSuchProviderException expected");
} catch (NoSuchProviderException e) {
//expected
}
}
use of java.security.NoSuchProviderException in project robovm by robovm.
the class X509CertificateTest method testVerifyPublicKey.
/**
* @throws SignatureException
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws CertificateException
* {@link Certificate#verify(PublicKey)}
*/
@SideEffect("Destroys MD5 provider, hurts succeeding tests")
public void testVerifyPublicKey() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, CertificateException {
// Preconditions
assertNotNull(javaxCert.getPublicKey());
assertNotNull(javaxSSCert.getPublicKey());
// here not self signed:
try {
javaxCert.verify(javaxCert.getPublicKey());
} catch (SignatureException e) {
// ok
}
PublicKey k = javaxCert.getPublicKey();
MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k);
changedEncoding.setEncoding(new byte[javaxCert.getEncoded().length - 1]);
try {
javaxCert.verify(tbt_cert.getPublicKey());
} catch (InvalidKeyException e) {
// ok
}
try {
javaxCert.verify(null);
} catch (Exception e) {
// ok
}
try {
javaxCert.verify(changedEncoding);
fail("Exception expected");
} catch (Exception e) {
// ok
}
// following test doesn't work because the algorithm is derived from
// somewhere else.
// MyModifiablePublicKey changedAlgo = new MyModifiablePublicKey(k);
// changedAlgo.setAlgorithm("MD5withBla");
// try {
// javaxCert.verify(changedAlgo);
// fail("Exception expected");
// } catch (SignatureException e) {
// // ok
// }
// Security.removeProvider(mySSProvider.getName());
// try {
// javaxSSCert.verify(javaxSSCert.getPublicKey());
// } catch (NoSuchProviderException e) {
// // ok
// }
// Security.addProvider(mySSProvider);
// must always evaluate true for self signed
// javaxSSCert.verify(javaxSSCert.getPublicKey());
}
use of java.security.NoSuchProviderException in project robovm by robovm.
the class TrustManagerFactory2Test method test_getInstanceLjava_lang_StringLjava_lang_String.
/**
* Test for <code>getInstance(String algorithm, String provider)</code>
* method
* Assertions:
* throws NullPointerException when algorithm is null;
* throws NoSuchAlgorithmException when algorithm is not correct;
* throws IllegalArgumentException when provider is null or empty;
* throws NoSuchProviderException when provider is available;
* returns TrustManagerFactory object
*/
public void test_getInstanceLjava_lang_StringLjava_lang_String() throws Exception {
try {
TrustManagerFactory.getInstance(null, mProv.getName());
fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
} catch (NoSuchAlgorithmException e) {
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
TrustManagerFactory.getInstance(invalidValues[i], mProv.getName());
fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (NoSuchAlgorithmException e) {
}
}
String prov = null;
for (int i = 0; i < validValues.length; i++) {
try {
TrustManagerFactory.getInstance(validValues[i], prov);
fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (IllegalArgumentException e) {
}
try {
TrustManagerFactory.getInstance(validValues[i], "");
fail("IllegalArgumentException must be thrown when provider is empty (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (IllegalArgumentException e) {
}
}
for (int i = 0; i < validValues.length; i++) {
for (int j = 1; j < invalidValues.length; j++) {
try {
TrustManagerFactory.getInstance(validValues[i], invalidValues[j]);
fail("NoSuchProviderException must be thrown (algorithm: ".concat(invalidValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
} catch (NoSuchProviderException e) {
}
}
}
TrustManagerFactory tmf;
for (int i = 0; i < validValues.length; i++) {
tmf = TrustManagerFactory.getInstance(validValues[i], mProv.getName());
assertTrue("Not instanceof TrustManagerFactory object", tmf instanceof TrustManagerFactory);
assertEquals("Incorrect algorithm", tmf.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider", tmf.getProvider().getName(), mProv.getName());
checkResult(tmf);
}
}
Aggregations