Search in sources :

Example 6 with Service

use of java.security.Provider.Service in project robovm by robovm.

the class KeyPairGeneratorTest method test_getInstance.

public void test_getInstance() throws Exception {
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if (!type.equals("KeyPairGenerator")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            // AndroidKeyStore is tested in CTS.
            if ("AndroidKeyStore".equals(provider.getName())) {
                continue;
            }
            AlgorithmParameterSpec params = null;
            // TODO: detect if we're running in vogar and run the full test
            if ("DH".equals(algorithm)) {
                params = getDHParams();
            }
            try {
                // KeyPairGenerator.getInstance(String)
                KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(algorithm);
                assertEquals(algorithm, kpg1.getAlgorithm());
                if (params != null) {
                    kpg1.initialize(params);
                }
                test_KeyPairGenerator(kpg1);
                // KeyPairGenerator.getInstance(String, Provider)
                KeyPairGenerator kpg2 = KeyPairGenerator.getInstance(algorithm, provider);
                assertEquals(algorithm, kpg2.getAlgorithm());
                assertEquals(provider, kpg2.getProvider());
                if (params != null) {
                    kpg2.initialize(params);
                }
                test_KeyPairGenerator(kpg2);
                // KeyPairGenerator.getInstance(String, String)
                KeyPairGenerator kpg3 = KeyPairGenerator.getInstance(algorithm, provider.getName());
                assertEquals(algorithm, kpg3.getAlgorithm());
                assertEquals(provider, kpg3.getProvider());
                if (params != null) {
                    kpg3.initialize(params);
                }
                test_KeyPairGenerator(kpg3);
            } catch (Exception e) {
                throw new Exception("Problem testing KeyPairGenerator." + algorithm, e);
            }
        }
    }
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) KeyPairGenerator(java.security.KeyPairGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Provider(java.security.Provider)

Example 7 with Service

use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.

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

Example 8 with Service

use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.

the class TransformService method getInstance.

/**
     * Returns a <code>TransformService</code> that supports the specified
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
     * (ex: DOM).
     *
     * <p>This method uses the standard JCA provider lookup mechanism to
     * locate and instantiate a <code>TransformService</code> implementation
     * of the desired algorithm and <code>MechanismType</code> service
     * attribute. It traverses the list of registered security
     * <code>Provider</code>s, starting with the most preferred
     * <code>Provider</code>. A new <code>TransformService</code> object
     * from the first <code>Provider</code> that supports the specified
     * algorithm and mechanism type is returned.
     *
     * <p> Note that the list of registered providers may be retrieved via
     * the {@link Security#getProviders() Security.getProviders()} method.
     *
     * @param algorithm the URI of the algorithm
     * @param mechanismType the type of the XML processing mechanism and
     *   representation
     * @return a new <code>TransformService</code>
     * @throws NullPointerException if <code>algorithm</code> or
     *   <code>mechanismType</code> is  <code>null</code>
     * @throws NoSuchAlgorithmException if no <code>Provider</code> supports a
     *   <code>TransformService</code> implementation for the specified
     *   algorithm and mechanism type
     * @see Provider
     */
public static TransformService getInstance(String algorithm, String mechanismType) throws NoSuchAlgorithmException {
    if (mechanismType == null || algorithm == null) {
        throw new NullPointerException();
    }
    boolean dom = false;
    if (mechanismType.equals("DOM")) {
        dom = true;
    }
    List<Service> services = GetInstance.getServices("TransformService", algorithm);
    for (Iterator<Service> t = services.iterator(); t.hasNext(); ) {
        Service s = t.next();
        String value = s.getAttribute("MechanismType");
        if ((value == null && dom) || (value != null && value.equals(mechanismType))) {
            Instance instance = GetInstance.getInstance(s, null);
            TransformService ts = (TransformService) instance.impl;
            ts.algorithm = algorithm;
            ts.mechanism = mechanismType;
            ts.provider = instance.provider;
            return ts;
        }
    }
    throw new NoSuchAlgorithmException(algorithm + " algorithm and " + mechanismType + " mechanism not available");
}
Also used : Instance(sun.security.jca.GetInstance.Instance) Service(java.security.Provider.Service) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 9 with Service

use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.

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 10 with Service

use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.

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)

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