Java Code Examples for java.security.Provider.Service#getProvider()

The following examples show how to use java.security.Provider.Service#getProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetInstance.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static Instance getInstance(Service s, Class<?> clazz)
        throws NoSuchAlgorithmException {
    Object instance = s.newInstance(null);
    checkSuperClass(s, instance.getClass(), clazz);
    return new Instance(s.getProvider(), instance);
}
 
Example 2
Source File: Cipher.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Cipher</code> 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>DES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if <code>transformation</code>
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if <code>transformation</code>
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the <code>provider</code>
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 3
Source File: GetInstance.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static Instance getInstance(Service s, Class<?> clazz,
        Object param) throws NoSuchAlgorithmException {
    Object instance = s.newInstance(param);
    checkSuperClass(s, instance.getClass(), clazz);
    return new Instance(s.getProvider(), instance);
}
 
Example 4
Source File: GetInstance.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static Instance getInstance(Service s, Class<?> clazz)
        throws NoSuchAlgorithmException {
    Object instance = s.newInstance(null);
    checkSuperClass(s, instance.getClass(), clazz);
    return new Instance(s.getProvider(), instance);
}
 
Example 5
Source File: Signature.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key,
        AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        if (sigSpi != null) {
            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 (key != null && s.supportsParameter(key) == false) {
                continue;
            }
            // if instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                tryOperation(spi, type, key, params, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        if (lastException instanceof InvalidAlgorithmParameterException) {
            throw (InvalidAlgorithmParameterException)lastException;
        }

        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 6
Source File: Signature.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key, SecureRandom random)
        throws InvalidKeyException {
    synchronized (lock) {
        if (sigSpi != null) {
            init(sigSpi, type, key, 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 instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                init(spi, type, key, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 7
Source File: Signature.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key,
        AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        if (sigSpi != null) {
            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 (key != null && s.supportsParameter(key) == false) {
                continue;
            }
            // if instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                tryOperation(spi, type, key, params, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        if (lastException instanceof InvalidAlgorithmParameterException) {
            throw (InvalidAlgorithmParameterException)lastException;
        }

        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 8
Source File: Cipher.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Cipher</code> 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>DES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if <code>transformation</code>
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if <code>transformation</code>
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the <code>provider</code>
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 9
Source File: Cipher.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                // 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);
    }
}
 
Example 10
Source File: GetInstance.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static Instance getInstance(Service s, Class<?> clazz,
        Object param) throws NoSuchAlgorithmException {
    Object instance = s.newInstance(param);
    checkSuperClass(s, instance.getClass(), clazz);
    return new Instance(s.getProvider(), instance);
}
 
Example 11
Source File: Signature.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key, SecureRandom random)
        throws InvalidKeyException {
    synchronized (lock) {
        if (sigSpi != null) {
            init(sigSpi, type, key, 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 instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                init(spi, type, key, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 12
Source File: Signature.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key, SecureRandom random)
        throws InvalidKeyException {
    synchronized (lock) {
        if (sigSpi != null) {
            init(sigSpi, type, key, 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 instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                init(spi, type, key, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 13
Source File: Cipher.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if {@code transformation}
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if {@code transformation}
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the {@code provider}
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 14
Source File: Cipher.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Cipher</code> 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>DES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if <code>transformation</code>
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if <code>transformation</code>
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the <code>provider</code>
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 15
Source File: GetInstance.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static Instance getInstance(Service s, Class<?> clazz)
        throws NoSuchAlgorithmException {
    Object instance = s.newInstance(null);
    checkSuperClass(s, instance.getClass(), clazz);
    return new Instance(s.getProvider(), instance);
}
 
Example 16
Source File: Cipher.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Cipher</code> 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>DES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if <code>transformation</code>
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if <code>transformation</code>
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the <code>provider</code>
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 17
Source File: Signature.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key, SecureRandom random)
        throws InvalidKeyException {
    synchronized (lock) {
        if (sigSpi != null) {
            init(sigSpi, type, key, 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 instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                init(spi, type, key, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}
 
Example 18
Source File: Cipher.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                // 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);
    }
}
 
Example 19
Source File: Cipher.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a <code>Cipher</code> 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>DES/CBC/PKCS5Padding</i>.
 * See the Cipher section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Cipher">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard transformation names.
 *
 * @param provider the provider.
 *
 * @return a cipher that implements the requested transformation.
 *
 * @exception NoSuchAlgorithmException if <code>transformation</code>
 *          is null, empty, in an invalid format,
 *          or if a CipherSpi implementation for the specified algorithm
 *          is not available from the specified Provider object.
 *
 * @exception NoSuchPaddingException if <code>transformation</code>
 *          contains a padding scheme that is not available.
 *
 * @exception IllegalArgumentException if the <code>provider</code>
 *          is null.
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation,
                                       Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException
{
    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);
}
 
Example 20
Source File: Signature.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void chooseProvider(int type, Key key, SecureRandom random)
        throws InvalidKeyException {
    synchronized (lock) {
        if (sigSpi != null) {
            init(sigSpi, type, key, 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 instance is not a SignatureSpi, ignore it
            if (isSpi(s) == false) {
                continue;
            }
            try {
                SignatureSpi spi = newInstance(s);
                init(spi, type, key, random);
                provider = s.getProvider();
                sigSpi = spi;
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                // NoSuchAlgorithmException from newInstance()
                // InvalidKeyException from init()
                // RuntimeException (ProviderException) from init()
                if (lastException == null) {
                    lastException = e;
                }
            }
        }
        // no working provider found, fail
        if (lastException instanceof InvalidKeyException) {
            throw (InvalidKeyException)lastException;
        }
        if (lastException instanceof RuntimeException) {
            throw (RuntimeException)lastException;
        }
        String k = (key != null) ? key.getClass().getName() : "(null)";
        throw new InvalidKeyException
            ("No installed provider supports this key: "
            + k, lastException);
    }
}