use of java.security.Provider.Service in project j2objc by google.
the class Cipher method tryCombinations.
/**
* Tries to find the correct {@code Cipher} transform to use. Returns a
* {@link org.apache.harmony.security.fortress.Engine.SpiAndProvider}, throws the first exception that was
* encountered during attempted initialization, or {@code null} if there are
* no providers that support the {@code initParams}.
* <p>
* {@code tokenizedTransformation} must be in the format returned by
* {@link Cipher#checkTransformation(String)}. The combinations of mode strings
* tried are as follows:
* <ul>
* <li><code>[cipher]/[mode]/[padding]</code>
* <li><code>[cipher]/[mode]</code>
* <li><code>[cipher]//[padding]</code>
* <li><code>[cipher]</code>
* </ul>
* {@code services} is a list of cipher services. Needs to be non-null only if
* {@code provider != null}
*/
static CipherSpiAndProvider tryCombinations(InitParams initParams, Provider provider, String[] tokenizedTransformation) throws InvalidKeyException, InvalidAlgorithmParameterException {
// Enumerate all the transforms we need to try
ArrayList<Transform> transforms = new ArrayList<Transform>();
if (tokenizedTransformation[1] != null && tokenizedTransformation[2] != null) {
transforms.add(new Transform(tokenizedTransformation[0] + "/" + tokenizedTransformation[1] + "/" + tokenizedTransformation[2], NeedToSet.NONE));
}
if (tokenizedTransformation[1] != null) {
transforms.add(new Transform(tokenizedTransformation[0] + "/" + tokenizedTransformation[1], NeedToSet.PADDING));
}
if (tokenizedTransformation[2] != null) {
transforms.add(new Transform(tokenizedTransformation[0] + "//" + tokenizedTransformation[2], NeedToSet.MODE));
}
transforms.add(new Transform(tokenizedTransformation[0], NeedToSet.BOTH));
// Try each of the transforms and keep track of the first exception
// encountered.
Exception cause = null;
if (provider != null) {
for (Transform transform : transforms) {
Provider.Service service = provider.getService("Cipher", transform.name);
if (service == null) {
continue;
}
return tryTransformWithProvider(initParams, tokenizedTransformation, transform.needToSet, service);
}
} else {
for (Provider prov : Security.getProviders()) {
for (Transform transform : transforms) {
Provider.Service service = prov.getService("Cipher", transform.name);
if (service == null) {
continue;
}
if (initParams == null || initParams.key == null || service.supportsParameter(initParams.key)) {
try {
CipherSpiAndProvider sap = tryTransformWithProvider(initParams, tokenizedTransformation, transform.needToSet, service);
if (sap != null) {
return sap;
}
} catch (Exception e) {
if (cause == null) {
cause = e;
}
}
}
}
}
}
if (cause instanceof InvalidKeyException) {
throw (InvalidKeyException) cause;
} else if (cause instanceof InvalidAlgorithmParameterException) {
throw (InvalidAlgorithmParameterException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause != null) {
throw new InvalidKeyException("No provider can be initialized with given key", cause);
} else if (initParams == null || initParams.key == null) {
return null;
} else {
// and it is an InvalidKeyException.
throw new InvalidKeyException("No provider offers " + Arrays.toString(tokenizedTransformation) + " for " + initParams.key.getAlgorithm() + " key of class " + initParams.key.getClass().getName() + " and export format " + initParams.key.getFormat());
}
}
use of java.security.Provider.Service in project j2objc by google.
the class JceSecurity method getInstance.
static Instance getInstance(String type, Class<?> clazz, String algorithm) throws NoSuchAlgorithmException {
List<Service> services = GetInstance.getServices(type, algorithm);
NoSuchAlgorithmException failure = null;
for (Service s : services) {
if (canUseProvider(s.getProvider()) == false) {
// allow only signed providers
continue;
}
try {
Instance instance = GetInstance.getInstance(s, clazz);
return instance;
} catch (NoSuchAlgorithmException e) {
failure = e;
}
}
throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available", failure);
}
use of java.security.Provider.Service in project j2objc by google.
the class SecretKeyFactory method nextSpi.
/**
* Update the active spi of this class and return the next
* implementation for failover. If no more implemenations are
* available, this method returns null. However, the active spi of
* this class is never set to null.
*/
private SecretKeyFactorySpi nextSpi(SecretKeyFactorySpi oldSpi) {
synchronized (lock) {
// try that spi now
if ((oldSpi != null) && (oldSpi != spi)) {
return spi;
}
if (serviceIterator == null) {
return null;
}
while (serviceIterator.hasNext()) {
Service s = serviceIterator.next();
if (JceSecurity.canUseProvider(s.getProvider()) == false) {
continue;
}
try {
Object obj = s.newInstance(null);
if (obj instanceof SecretKeyFactorySpi == false) {
continue;
}
SecretKeyFactorySpi spi = (SecretKeyFactorySpi) obj;
provider = s.getProvider();
this.spi = spi;
return spi;
} catch (NoSuchAlgorithmException e) {
// ignore
}
}
serviceIterator = null;
return null;
}
}
use of java.security.Provider.Service in project j2objc by google.
the class KeyAgreement method getInstance.
/**
* Returns a <code>KeyAgreement</code> object that implements the
* specified key agreement algorithm.
*
* <p> This method traverses the list of registered security Providers,
* starting with the most preferred Provider.
* A new KeyAgreement object encapsulating the
* KeyAgreementSpi implementation from the first
* Provider that supports the specified algorithm is returned.
*
* <p> Note that the list of registered providers may be retrieved via
* the {@link Security#getProviders() Security.getProviders()} method.
*
* @param algorithm the standard name of the requested key agreement
* algorithm.
* See the KeyAgreement section in the <a href=
* "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyAgreement">
* Java Cryptography Architecture Standard Algorithm Name Documentation</a>
* for information about standard algorithm names.
*
* @return the new <code>KeyAgreement</code> object.
*
* @exception NullPointerException if the specified algorithm
* is null.
*
* @exception NoSuchAlgorithmException if no Provider supports a
* KeyAgreementSpi implementation for the
* specified algorithm.
*
* @see java.security.Provider
*/
public static final KeyAgreement getInstance(String algorithm) throws NoSuchAlgorithmException {
List<Service> services = GetInstance.getServices("KeyAgreement", algorithm);
// make sure there is at least one service from a signed provider
Iterator<Service> t = services.iterator();
while (t.hasNext()) {
Service s = t.next();
if (JceSecurity.canUseProvider(s.getProvider()) == false) {
continue;
}
// return new KeyAgreement(s, t, algorithm);
return new KeyAgreement(algorithm);
}
throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available");
}
use of java.security.Provider.Service in project j2objc by google.
the class KeyGenerator method nextSpi.
/**
* Update the active spi of this class and return the next
* implementation for failover. If no more implemenations are
* available, this method returns null. However, the active spi of
* this class is never set to null.
*/
private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi, boolean reinit) {
synchronized (lock) {
// try that spi now
if ((oldSpi != null) && (oldSpi != spi)) {
return spi;
}
if (serviceIterator == null) {
return null;
}
while (serviceIterator.hasNext()) {
Service s = serviceIterator.next();
if (JceSecurity.canUseProvider(s.getProvider()) == false) {
continue;
}
try {
Object inst = s.newInstance(null);
// ignore non-spis
if (inst instanceof KeyGeneratorSpi == false) {
continue;
}
KeyGeneratorSpi spi = (KeyGeneratorSpi) inst;
if (reinit) {
if (initType == I_SIZE) {
spi.engineInit(initKeySize, initRandom);
} else if (initType == I_PARAMS) {
spi.engineInit(initParams, initRandom);
} else if (initType == I_RANDOM) {
spi.engineInit(initRandom);
} else if (initType != I_NONE) {
throw new AssertionError("KeyGenerator initType: " + initType);
}
}
provider = s.getProvider();
this.spi = spi;
return spi;
} catch (Exception e) {
// ignore
}
}
disableFailover();
return null;
}
}
Aggregations