Search in sources :

Example 21 with Service

use of java.security.Provider.Service in project core by jcryptool.

the class ProvidersManager method getSupportingProvider.

/**
 * Returns the provider with the highest priority that supports the specified service. <br>
 * The default provider is queried first and only if it does not support the service, the other available providers
 * are queried in their priority order.
 *
 * @param type The type of the service
 * @param algorithmName The algorithm name of the service
 * @return The highest-ordered provider supporting the given service
 * @throws NoSuchAlgorithmException Thrown, when no available provider supports the specified service
 */
public Provider getSupportingProvider(String type, String algorithmName) throws NoSuchAlgorithmException {
    // $NON-NLS-1$
    LogUtil.logInfo("getting service provider");
    if (isServiceProvidedByDefault(type, algorithmName)) {
        // the default provider supports the operation
        // $NON-NLS-1$
        LogUtil.logInfo("returning the default provider: " + defaultProvider.getName());
        return defaultProvider;
    } else {
        // the default provider does not support the operation
        // query the provider hierarchy
        // $NON-NLS-1$
        LogUtil.logInfo("The default provider does not support the service");
        Provider provider;
        Service service;
        Iterator<String> it = availableProviderNames.iterator();
        String providerName;
        while (it.hasNext()) {
            try {
                providerName = it.next();
                provider = getProvider(providerName);
                service = provider.getService(type, algorithmName);
                if (service != null) {
                    // $NON-NLS-1$
                    LogUtil.logInfo(providerName + " supports the service!");
                    LogUtil.logInfo(provider.getName());
                    return provider;
                }
            } catch (CoreException e) {
                // $NON-NLS-1$
                LogUtil.logError(OperationsPlugin.PLUGIN_ID, "Unable to load a provider", e, false);
            }
        }
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    throw new NoSuchAlgorithmException("No available provider supports " + algorithmName + " of type " + type);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) Service(java.security.Provider.Service) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 22 with Service

use of java.security.Provider.Service in project wildfly-camel by wildfly-extras.

the class ApnsUtils method getAlgorithm.

public static String getAlgorithm() {
    List<String> keys = new LinkedList<>();
    List<String> trusts = new LinkedList<>();
    for (Provider p : Security.getProviders()) {
        if (p.getServices() != null) {
            for (Service s : p.getServices()) {
                if ("KeyManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                    keys.add(s.getAlgorithm());
                } else if ("TrustManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                    trusts.add(s.getAlgorithm());
                }
            }
        }
    }
    keys.retainAll(trusts);
    return keys.get(0);
}
Also used : Service(java.security.Provider.Service) LinkedList(java.util.LinkedList) Provider(java.security.Provider)

Example 23 with Service

use of java.security.Provider.Service in project Bytecoder by mirkosertic.

the class KeyFactory method nextSpi.

/**
 * Update the active KeyFactorySpi 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 KeyFactorySpi nextSpi(KeyFactorySpi 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();
            try {
                Object obj = s.newInstance(null);
                if (obj instanceof KeyFactorySpi == false) {
                    continue;
                }
                KeyFactorySpi spi = (KeyFactorySpi) obj;
                provider = s.getProvider();
                this.spi = spi;
                return spi;
            } catch (NoSuchAlgorithmException e) {
            // ignore
            }
        }
        serviceIterator = null;
        return null;
    }
}
Also used : Service(java.security.Provider.Service)

Example 24 with Service

use of java.security.Provider.Service in project Bytecoder by mirkosertic.

the class KeyPairGenerator method getInstance.

/**
 * Returns a KeyPairGenerator object that generates public/private
 * key pairs for the specified algorithm.
 *
 * <p> This method traverses the list of registered security Providers,
 * starting with the most preferred Provider.
 * A new KeyPairGenerator object encapsulating the
 * KeyPairGeneratorSpi 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.
 *
 * @implNote
 * The JDK Reference Implementation additionally uses the
 * {@code jdk.security.provider.preferred}
 * {@link Security#getProperty(String) Security} property to determine
 * the preferred provider order for the specified algorithm. This
 * may be different than the order of providers returned by
 * {@link Security#getProviders() Security.getProviders()}.
 *
 * @param algorithm the standard string name of the algorithm.
 * See the KeyPairGenerator section in the <a href=
 * "{@docRoot}/../specs/security/standard-names.html#keypairgenerator-algorithms">
 * Java Security Standard Algorithm Names Specification</a>
 * for information about standard algorithm names.
 *
 * @return the new {@code KeyPairGenerator} object
 *
 * @throws NoSuchAlgorithmException if no {@code Provider} supports a
 *         {@code KeyPairGeneratorSpi} implementation for the
 *         specified algorithm
 *
 * @throws NullPointerException if {@code algorithm} is {@code null}
 *
 * @see Provider
 */
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException {
    Objects.requireNonNull(algorithm, "null algorithm name");
    List<Service> list = GetInstance.getServices("KeyPairGenerator", algorithm);
    Iterator<Service> t = list.iterator();
    if (t.hasNext() == false) {
        throw new NoSuchAlgorithmException(algorithm + " KeyPairGenerator not available");
    }
    // find a working Spi or KeyPairGenerator subclass
    NoSuchAlgorithmException failure = null;
    do {
        Service s = t.next();
        try {
            Instance instance = GetInstance.getInstance(s, KeyPairGeneratorSpi.class);
            if (instance.impl instanceof KeyPairGenerator) {
                return getInstance(instance, algorithm);
            } else {
                return new Delegate(instance, t, algorithm);
            }
        } catch (NoSuchAlgorithmException e) {
            if (failure == null) {
                failure = e;
            }
        }
    } while (t.hasNext());
    throw failure;
}
Also used : Instance(sun.security.jca.GetInstance.Instance) Service(java.security.Provider.Service)

Example 25 with Service

use of java.security.Provider.Service in project Bytecoder by mirkosertic.

the class X509TrustManagerComSunWrapper method getImpl.

/**
 * Returns an array of objects: the first object in the array is
 * an instance of an implementation of the requested algorithm
 * and type, and the second object in the array identifies the provider
 * of that implementation.
 * The <code>provName</code> argument can be null, in which case all
 * configured providers will be searched in order of preference.
 */
static Object[] getImpl(String algName, String engineType, String provName) throws NoSuchAlgorithmException, NoSuchProviderException {
    Service service;
    if (provName != null) {
        ProviderList list = Providers.getProviderList();
        Provider prov = list.getProvider(provName);
        if (prov == null) {
            throw new NoSuchProviderException("No such provider: " + provName);
        }
        service = prov.getService(engineType, algName);
    } else {
        service = getService(engineType, algName);
    }
    if (service == null) {
        throw new NoSuchAlgorithmException("Algorithm " + algName + " not available");
    }
    return getImpl1(algName, engineType, service);
}
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