use of java.security.NoSuchProviderException in project robovm by robovm.
the class PKCS10CertificationRequest method verify.
/**
* verify the request using the passed in public key and the provider..
*/
public boolean verify(PublicKey pubKey, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
Signature sig;
try {
if (provider == null) {
sig = Signature.getInstance(getSignatureName(sigAlgId));
} else {
sig = Signature.getInstance(getSignatureName(sigAlgId), provider);
}
} catch (NoSuchAlgorithmException e) {
//
if (oids.get(sigAlgId.getObjectId()) != null) {
String signatureAlgorithm = (String) oids.get(sigAlgId.getObjectId());
if (provider == null) {
sig = Signature.getInstance(signatureAlgorithm);
} else {
sig = Signature.getInstance(signatureAlgorithm, provider);
}
} else {
throw e;
}
}
setSignatureParameters(sig, sigAlgId.getParameters());
sig.initVerify(pubKey);
try {
sig.update(reqInfo.getEncoded(ASN1Encoding.DER));
} catch (Exception e) {
throw new SignatureException("exception encoding TBS cert request - " + e);
}
return sig.verify(sigBits.getBytes());
}
use of java.security.NoSuchProviderException in project robovm by robovm.
the class AlgorithmParameterGenerator2Test method testGetInstance02.
/**
* Test for <code>getInstance(String algorithm, String provider)</code>
* method
* Assertions:
* throws NullPointerException must be thrown is null
* throws NoSuchAlgorithmException must be thrown if algorithm is not available
* throws IllegalArgumentException when provider is null;
* throws NoSuchProviderException when provider is available;
* returns AlgorithmParameterGenerator object
*/
public void testGetInstance02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, InvalidAlgorithmParameterException {
try {
AlgorithmParameterGenerator.getInstance(null, mProv.getName());
fail("NullPointerException or NoSuchAlgorithmException should be thrown");
} catch (NullPointerException e) {
} catch (NoSuchAlgorithmException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
AlgorithmParameterGenerator.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 {
AlgorithmParameterGenerator.getInstance(validValues[i], prov);
fail("IllegalArgumentException must be thrown when provider is null (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 {
AlgorithmParameterGenerator.getInstance(validValues[i], invalidValues[j]);
fail("NoSuchProviderException must be thrown (algorithm: ".concat(invalidValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
} catch (NoSuchProviderException e) {
}
}
}
AlgorithmParameterGenerator apG;
for (int i = 0; i < validValues.length; i++) {
apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv.getName());
assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider", apG.getProvider().getName(), mProv.getName());
checkResult(apG);
}
}
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
}
}
Aggregations