Search in sources :

Example 76 with Service

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);
    }
}
Also used : Service(java.security.Provider.Service)

Example 77 with Service

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;
}
Also used : Service(java.security.Provider.Service)

Example 78 with Service

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");
    }
}
Also used : Service(java.security.Provider.Service)

Example 79 with Service

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;
}
Also used : Service(java.security.Provider.Service)

Example 80 with Service

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);
}
Also used : RandomImpl(org.apache.harmony.security.tests.support.RandomImpl) Service(java.security.Provider.Service) Service(java.security.Provider.Service) Provider(java.security.Provider)

Aggregations

Service (java.security.Provider.Service)80 Provider (java.security.Provider)17 Instance (sun.security.jca.GetInstance.Instance)11 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)9 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 DefaultSet (de.carne.jfx.util.DefaultSet)3 Nullable (de.carne.check.Nullable)2 KeyPairGenerator (java.security.KeyPairGenerator)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 DSAPublicKey (java.security.interfaces.DSAPublicKey)2 LinkedList (java.util.LinkedList)2 CoreException (org.eclipse.core.runtime.CoreException)2 KeyFactory (java.security.KeyFactory)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 DSAParams (java.security.interfaces.DSAParams)1 ECPrivateKey (java.security.interfaces.ECPrivateKey)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1