Search in sources :

Example 61 with Service

use of java.security.Provider.Service in project certmgr by hdecarne.

the class SignatureAlgorithm method getDefaultSet.

/**
 * Get the available signature algorithms.
 *
 * @param keyPairAlgorithm The key pair algorithm to get the signature algorithms for.
 * @param defaultHint The default to return (may be {@code null}). If this algorithm is contained in the default
 *        set, it is also set as the default.
 * @param expertMode Whether only standard algorithms are considered ({@code false}) or all algorithms available on
 *        the current platform ({@code true}).
 * @return The available signature algorithms
 */
public static DefaultSet<SignatureAlgorithm> getDefaultSet(String keyPairAlgorithm, @Nullable String defaultHint, boolean expertMode) {
    DefaultSet<SignatureAlgorithm> signatureAlgorithms = new DefaultSet<>();
    DefaultSet<String> defaultNames = SecurityDefaults.getSignatureAlgorithmNames(keyPairAlgorithm);
    @Nullable String defaultName = (defaultHint != null && defaultNames.contains(defaultHint) ? defaultHint : defaultNames.getDefault());
    if (defaultName != null) {
        defaultName = defaultName.toUpperCase();
    }
    for (Provider provider : SecurityDefaults.getProviders(expertMode)) {
        for (Provider.Service service : provider.getServices()) {
            if (!SERVICE_TYPE_SIGNATURE.equals(service.getType())) {
                continue;
            }
            String upperCaseAlgorithm = service.getAlgorithm().toUpperCase();
            if (!expertMode && !defaultNames.contains(upperCaseAlgorithm)) {
                continue;
            }
            SignatureAlgorithm signatureAlgorithm = (expertMode ? new ExpertKeyPairAlgorithm(service) : new StandardKeyPairAlgorithm(service));
            if (upperCaseAlgorithm.equals(defaultName)) {
                signatureAlgorithms.addDefault(signatureAlgorithm);
            } else {
                signatureAlgorithms.add(signatureAlgorithm);
            }
        }
    }
    return signatureAlgorithms;
}
Also used : DefaultSet(de.carne.jfx.util.DefaultSet) Service(java.security.Provider.Service) Nullable(de.carne.check.Nullable) Provider(java.security.Provider)

Example 62 with Service

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

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

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

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) {
    ArrayList<PreferredEntry> pList = null;
    int i;
    // Preferred provider list
    if (preferredPropList != null && (pList = preferredPropList.getAll(type, name)) != null) {
        for (i = 0; i < pList.size(); i++) {
            Provider p = getProvider(pList.get(i).provider);
            Service s = p.getService(type, name);
            if (s != null) {
                return s;
            }
        }
    }
    for (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) Provider(java.security.Provider)

Example 64 with Service

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

the class Cipher method chooseFirstProvider.

/**
 * Choose the Spi from the first provider available. Used if
 * delayed provider selection is not possible because init()
 * is not the first method called.
 */
void chooseFirstProvider() {
    if (spi != null) {
        return;
    }
    synchronized (lock) {
        if (spi != null) {
            return;
        }
        if (debug != null) {
            int w = --warnCount;
            if (w >= 0) {
                debug.println("Cipher.init() not first method " + "called, disabling delayed provider selection");
                if (w == 0) {
                    debug.println("Further warnings of this type will " + "be suppressed");
                }
                new Exception("Call trace").printStackTrace();
            }
        }
        Exception lastException = null;
        while ((firstService != null) || serviceIterator.hasNext()) {
            Service s;
            CipherSpi thisSpi;
            if (firstService != null) {
                s = firstService;
                thisSpi = firstSpi;
                firstService = null;
                firstSpi = null;
            } else {
                s = serviceIterator.next();
                thisSpi = null;
            }
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                continue;
            }
            Transform tr = getTransform(s, transforms);
            if (tr == null) {
                // should never happen
                continue;
            }
            if (tr.supportsModePadding(s) == S_NO) {
                continue;
            }
            try {
                if (thisSpi == null) {
                    Object obj = s.newInstance(null);
                    if (obj instanceof CipherSpi == false) {
                        continue;
                    }
                    thisSpi = (CipherSpi) obj;
                }
                tr.setModePadding(thisSpi);
                initCryptoPermission();
                spi = thisSpi;
                provider = s.getProvider();
                // not needed any more
                firstService = null;
                serviceIterator = null;
                transforms = null;
                return;
            } catch (Exception e) {
                lastException = e;
            }
        }
        ProviderException e = new ProviderException("Could not construct CipherSpi instance");
        if (lastException != null) {
            e.initCause(lastException);
        }
        throw e;
    }
}
Also used : Service(java.security.Provider.Service) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException)

Example 65 with Service

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

the class Cipher method chooseProvider.

private void chooseProvider(int initType, int opmode, Key key, AlgorithmParameterSpec paramSpec, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        if (spi != null) {
            implInit(spi, initType, opmode, key, paramSpec, params, random);
            return;
        }
        Exception lastException = null;
        while ((firstService != null) || serviceIterator.hasNext()) {
            Service s;
            CipherSpi thisSpi;
            if (firstService != null) {
                s = firstService;
                thisSpi = firstSpi;
                firstService = null;
                firstSpi = null;
            } else {
                s = serviceIterator.next();
                thisSpi = null;
            }
            // if provider says it does not support this key, ignore it
            if (s.supportsParameter(key) == false) {
                continue;
            }
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                continue;
            }
            Transform tr = getTransform(s, transforms);
            if (tr == null) {
                // should never happen
                continue;
            }
            if (tr.supportsModePadding(s) == S_NO) {
                continue;
            }
            try {
                if (thisSpi == null) {
                    thisSpi = (CipherSpi) s.newInstance(null);
                }
                tr.setModePadding(thisSpi);
                initCryptoPermission();
                implInit(thisSpi, initType, opmode, key, paramSpec, params, random);
                provider = s.getProvider();
                this.spi = thisSpi;
                firstService = null;
                serviceIterator = null;
                transforms = null;
                return;
            } catch (Exception e) {
                // SecurityException from crypto permission check
                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) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException)

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