Search in sources :

Example 66 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> This method traverses the list of registered security Providers,
 * starting with the most preferred Provider.
 * A new Cipher object encapsulating the
 * CipherSpi 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 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.
 *
 * @return a cipher that implements the requested transformation
 *
 * @throws NoSuchAlgorithmException if {@code transformation}
 *         is {@code null}, empty, in an invalid format,
 *         or if no {@code Provider} supports a {@code CipherSpi}
 *         implementation for the specified algorithm
 *
 * @throws NoSuchPaddingException if {@code transformation}
 *         contains a padding scheme that is not available
 *
 * @see java.security.Provider
 */
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException {
    if ((transformation == null) || transformation.equals("")) {
        throw new NoSuchAlgorithmException("Null or empty transformation");
    }
    List<Transform> transforms = getTransforms(transformation);
    List<ServiceId> cipherServices = new ArrayList<>(transforms.size());
    for (Transform transform : transforms) {
        cipherServices.add(new ServiceId("Cipher", transform.transform));
    }
    List<Service> services = GetInstance.getServices(cipherServices);
    // make sure there is at least one service from a signed provider
    // and that it can use the specified mode and padding
    Iterator<Service> t = services.iterator();
    Exception failure = null;
    while (t.hasNext()) {
        Service s = t.next();
        if (JceSecurity.canUseProvider(s.getProvider()) == false) {
            continue;
        }
        Transform tr = getTransform(s, transforms);
        if (tr == null) {
            // should never happen
            continue;
        }
        int canuse = tr.supportsModePadding(s);
        if (canuse == S_NO) {
            // does not support mode or padding we need, ignore
            continue;
        }
        if (canuse == S_YES) {
            return new Cipher(null, s, t, transformation, transforms);
        } else {
            // S_MAYBE, try out if it works
            try {
                CipherSpi spi = (CipherSpi) s.newInstance(null);
                tr.setModePadding(spi);
                return new Cipher(spi, s, t, transformation, transforms);
            } catch (Exception e) {
                failure = e;
            }
        }
    }
    throw new NoSuchAlgorithmException("Cannot find any provider supporting " + transformation, failure);
}
Also used : Service(java.security.Provider.Service) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) ReadOnlyBufferException(java.nio.ReadOnlyBufferException)

Example 67 with Service

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

the class KeyAgreement 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("KeyAgreement.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;
            if (firstService != null) {
                s = firstService;
                firstService = null;
            } else {
                s = serviceIterator.next();
            }
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                continue;
            }
            try {
                Object obj = s.newInstance(null);
                if (obj instanceof KeyAgreementSpi == false) {
                    continue;
                }
                spi = (KeyAgreementSpi) obj;
                provider = s.getProvider();
                // not needed any more
                firstService = null;
                serviceIterator = null;
                return;
            } catch (Exception e) {
                lastException = e;
            }
        }
        ProviderException e = new ProviderException("Could not construct KeyAgreementSpi instance");
        if (lastException != null) {
            e.initCause(lastException);
        }
        throw e;
    }
}
Also used : Service(java.security.Provider.Service)

Example 68 with Service

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

the class KeyGenerator method nextSpi.

/**
 * Update the active spi of this class and return the next
 * implementation for failover. If no more implementations are
 * available, this method returns null. However, the active spi of
 * this class is never set to null.
 */
private KeyGeneratorSpi nextSpi(KeyGeneratorSpi oldSpi, boolean reinit) {
    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 inst = s.newInstance(null);
                // ignore non-spis
                if (inst instanceof KeyGeneratorSpi == false) {
                    continue;
                }
                KeyGeneratorSpi spi = (KeyGeneratorSpi) inst;
                if (reinit) {
                    if (initType == I_SIZE) {
                        spi.engineInit(initKeySize, initRandom);
                    } else if (initType == I_PARAMS) {
                        spi.engineInit(initParams, initRandom);
                    } else if (initType == I_RANDOM) {
                        spi.engineInit(initRandom);
                    } else if (initType != I_NONE) {
                        throw new AssertionError("KeyGenerator initType: " + initType);
                    }
                }
                provider = s.getProvider();
                this.spi = spi;
                return spi;
            } catch (Exception e) {
            // ignore
            }
        }
        disableFailover();
        return null;
    }
}
Also used : Service(java.security.Provider.Service)

Example 69 with Service

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

the class Mac 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) || (serviceIterator == null)) {
        return;
    }
    synchronized (lock) {
        if (spi != null) {
            return;
        }
        if (debug != null) {
            int w = --warnCount;
            if (w >= 0) {
                debug.println("Mac.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;
            if (firstService != null) {
                s = firstService;
                firstService = null;
            } else {
                s = serviceIterator.next();
            }
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
                continue;
            }
            try {
                Object obj = s.newInstance(null);
                if (obj instanceof MacSpi == false) {
                    continue;
                }
                spi = (MacSpi) obj;
                provider = s.getProvider();
                // not needed any more
                firstService = null;
                serviceIterator = null;
                return;
            } catch (NoSuchAlgorithmException e) {
                lastException = e;
            }
        }
        ProviderException e = new ProviderException("Could not construct MacSpi instance");
        if (lastException != null) {
            e.initCause(lastException);
        }
        throw e;
    }
}
Also used : Service(java.security.Provider.Service)

Example 70 with Service

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

the class ProvidersManager method isServiceProvidedByDefault.

/**
 * Checks whether the default provider supports the specified service.
 *
 * @param type The type of the service
 * @param algorithmName The algorithm name of the service
 * @return <code>true</code>, when the default provider supports the specified service
 */
public boolean isServiceProvidedByDefault(String type, String algorithmName) {
    if (defaultProvider == null) {
        // $NON-NLS-1$
        LogUtil.logInfo("getting default provider");
        try {
            defaultProvider = getDefaultProvider();
            Service service = null;
            if (defaultProvider != null) {
                // $NON-NLS-1$
                LogUtil.logInfo("defaultProvider " + defaultProvider.getName());
                service = defaultProvider.getService(type, algorithmName);
            } else {
                // $NON-NLS-1$
                LogUtil.logInfo("dp is null");
            }
            return service != null;
        } catch (CoreException e) {
            // $NON-NLS-1$
            LogUtil.logError(OperationsPlugin.PLUGIN_ID, "Unable to create the factory default provider", e, false);
        }
    } else {
        // $NON-NLS-1$
        LogUtil.logInfo("defaultProvider != null");
        try {
            // precautionary measure. the default provider might not be
            // null,
            // but the preferences might have changed
            defaultProvider = getDefaultProvider();
        } catch (CoreException e) {
            // $NON-NLS-1$
            LogUtil.logError(OperationsPlugin.PLUGIN_ID, "Unable to access default provider", e, false);
        }
        Service service = defaultProvider.getService(type, algorithmName);
        boolean result = (service != null);
        // $NON-NLS-1$
        LogUtil.logInfo("returning " + result);
        return result;
    }
    return false;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) 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