use of java.security.Provider.Service in project j2objc by google.
the class Mac method chooseProvider.
private void chooseProvider(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
synchronized (lock) {
// if (spi != null) {
if (spi != null && (key == null || lock == null)) {
spi.engineInit(key, params);
return;
}
Exception lastException = null;
// Android-changed: Provider selection; loop over a new list each time.
for (Service s : GetInstance.getServices("Mac", algorithm)) {
// if provider says it does not support this key, ignore it
if (s.supportsParameter(key) == false) {
continue;
}
if (JceSecurity.canUseProvider(s.getProvider()) == false) {
continue;
}
try {
MacSpi spi = (MacSpi) s.newInstance(null);
spi.engineInit(key, params);
provider = s.getProvider();
this.spi = spi;
/*
firstService = null;
serviceIterator = null;
*/
return;
} catch (Exception e) {
// RuntimeException (ProviderException) from init()
if (lastException == null) {
lastException = e;
}
}
}
// no working provider found, fail
if (lastException instanceof InvalidKeyException) {
throw (InvalidKeyException) lastException;
}
if (lastException instanceof InvalidAlgorithmParameterException) {
throw (InvalidAlgorithmParameterException) lastException;
}
if (lastException instanceof RuntimeException) {
throw (RuntimeException) lastException;
}
String kName = (key != null) ? key.getClass().getName() : "(null)";
throw new InvalidKeyException("No installed provider supports this key: " + kName, lastException);
}
}
use of java.security.Provider.Service in project j2objc by google.
the class GetInstance method getInstance.
/*
* For all the getInstance() methods below:
* @param type the type of engine (e.g. MessageDigest)
* @param clazz the Spi class that the implementation must subclass
* (e.g. MessageDigestSpi.class) or null if no superclass check
* is required
* @param algorithm the name of the algorithm (or alias), e.g. MD5
* @param provider the provider (String or Provider object)
* @param param the parameter to pass to the Spi constructor
* (for CertStores)
*
* There are overloaded methods for all the permutations.
*/
public static Instance getInstance(String type, Class<?> clazz, String algorithm) throws NoSuchAlgorithmException {
// in the almost all cases, the first service will work
// avoid taking long path if so
ProviderList list = Providers.getProviderList();
Service firstService = list.getService(type, algorithm);
if (firstService == null) {
throw new NoSuchAlgorithmException(algorithm + " " + type + " not available");
}
NoSuchAlgorithmException failure;
try {
return getInstance(firstService, clazz);
} catch (NoSuchAlgorithmException e) {
failure = e;
}
// fail over to the next
for (Service s : list.getServices(type, algorithm)) {
if (s == firstService) {
// do not retry initial failed service
continue;
}
try {
return getInstance(s, clazz);
} catch (NoSuchAlgorithmException e) {
failure = e;
}
}
throw failure;
}
use of java.security.Provider.Service in project j2objc by google.
the class GetInstance method getInstance.
public static Instance getInstance(String type, Class<?> clazz, String algorithm, Object param) throws NoSuchAlgorithmException {
List<Service> services = getServices(type, algorithm);
NoSuchAlgorithmException failure = null;
for (Service s : services) {
try {
return getInstance(s, clazz, param);
} catch (NoSuchAlgorithmException e) {
failure = e;
}
}
if (failure != null) {
throw failure;
} else {
throw new NoSuchAlgorithmException(algorithm + " " + type + " not available");
}
}
use of java.security.Provider.Service in project j2objc by google.
the class ProviderList method getService.
/**
* Return a Service describing an implementation of the specified
* algorithm from the Provider with the highest precedence that
* supports that algorithm. Return null if no Provider supports this
* algorithm.
*/
public Service getService(String type, String name) {
for (int i = 0; i < configs.length; i++) {
Provider p = getProvider(i);
Service s = p.getService(type, name);
if (s != null) {
return s;
}
}
return null;
}
use of java.security.Provider.Service in project j2objc by google.
the class ProviderServiceTest method testNewInstance.
public void testNewInstance() throws Exception {
String type = "SecureRandom";
String algorithm = "algorithm";
MyProvider p = new MyProvider();
Provider.Service s = new Provider.Service(p, type, algorithm, "org.apache.harmony.security.tests.support.RandomImpl", null, null);
p.putService(s);
// Check success and correct type.
Object o = s.newInstance(null);
assertTrue("new service instance is of incorrect class " + o.getClass().getName(), o instanceof RandomImpl);
}
Aggregations