Search in sources :

Example 26 with Service

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

the class Cipher method getInstance.

/**
 * Returns a {@code Cipher} object that implements the specified
 * transformation.
 *
 * <p> A new Cipher object encapsulating the
 * CipherSpi implementation from the specified Provider
 * object is returned.  Note that the specified Provider object
 * does not have to be registered in the provider list.
 *
 * @param transformation the name of the transformation,
 * e.g., <i>AES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../specs/security/standard-names.html#cipher-algorithm-names">
 * Java Security Standard Algorithm Names Specification</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation
 *
 * @throws IllegalArgumentException if the {@code provider}
 *         is {@code null}
 *
 * @throws NoSuchAlgorithmException if {@code transformation}
 *         is {@code null}, empty, in an invalid format,
 *         or if a {@code CipherSpi} implementation for the
 *         specified algorithm is not available from the specified
 *         {@code Provider} object
 *
 * @throws NoSuchPaddingException if {@code transformation}
 *         contains a padding scheme that is not available
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation, Provider provider) throws NoSuchAlgorithmException, NoSuchPaddingException {
    if ((transformation == null) || transformation.equals("")) {
        throw new NoSuchAlgorithmException("Null or empty transformation");
    }
    if (provider == null) {
        throw new IllegalArgumentException("Missing provider");
    }
    Exception failure = null;
    List<Transform> transforms = getTransforms(transformation);
    boolean providerChecked = false;
    String paddingError = null;
    for (Transform tr : transforms) {
        Service s = provider.getService("Cipher", tr.transform);
        if (s == null) {
            continue;
        }
        if (providerChecked == false) {
            // for compatibility, first do the lookup and then verify
            // the provider. this makes the difference between a NSAE
            // and a SecurityException if the
            // provider does not support the algorithm.
            Exception ve = JceSecurity.getVerificationResult(provider);
            if (ve != null) {
                String msg = "JCE cannot authenticate the provider " + provider.getName();
                throw new SecurityException(msg, ve);
            }
            providerChecked = true;
        }
        if (tr.supportsMode(s) == S_NO) {
            continue;
        }
        if (tr.supportsPadding(s) == S_NO) {
            paddingError = tr.pad;
            continue;
        }
        try {
            CipherSpi spi = (CipherSpi) s.newInstance(null);
            tr.setModePadding(spi);
            Cipher cipher = new Cipher(spi, transformation);
            cipher.provider = s.getProvider();
            cipher.initCryptoPermission();
            return cipher;
        } catch (Exception e) {
            failure = e;
        }
    }
    // throw NoSuchPaddingException if the problem is with padding
    if (failure instanceof NoSuchPaddingException) {
        throw (NoSuchPaddingException) failure;
    }
    if (paddingError != null) {
        throw new NoSuchPaddingException("Padding not supported: " + paddingError);
    }
    throw new NoSuchAlgorithmException("No such algorithm: " + transformation, failure);
}
Also used : Service(java.security.Provider.Service) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException)

Example 27 with Service

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

the class Mac method chooseProvider.

private void chooseProvider(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        if (spi != null) {
            spi.engineInit(key, params);
            return;
        }
        Exception lastException = null;
        while ((firstService != null) || serviceIterator.hasNext()) {
            Service s;
            if (firstService != null) {
                s = firstService;
                firstService = null;
            } else {
                s = serviceIterator.next();
            }
            // 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 28 with Service

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

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

Example 29 with Service

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

the class KeyAgreement method chooseProvider.

private void chooseProvider(int initType, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        if (spi != null) {
            implInit(spi, initType, key, params, random);
            return;
        }
        Exception lastException = null;
        while ((firstService != null) || serviceIterator.hasNext()) {
            Service s;
            if (firstService != null) {
                s = firstService;
                firstService = null;
            } else {
                s = serviceIterator.next();
            }
            // 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 {
                KeyAgreementSpi spi = (KeyAgreementSpi) s.newInstance(null);
                implInit(spi, initType, key, params, random);
                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 30 with Service

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

the class GetInstance method getService.

public static Service getService(String type, String algorithm) throws NoSuchAlgorithmException {
    ProviderList list = Providers.getProviderList();
    Service s = list.getService(type, algorithm);
    if (s == null) {
        throw new NoSuchAlgorithmException(algorithm + " " + type + " not available");
    }
    return s;
}
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