Search in sources :

Example 71 with Service

use of java.security.Provider.Service in project j2objc by google.

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.
 *
 * @param algorithm the standard string name of the algorithm.
 * See the KeyPairGenerator section in the <a href=
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyPairGenerator">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard algorithm names.
 *
 * @return the new KeyPairGenerator object.
 *
 * @exception NoSuchAlgorithmException if no Provider supports a
 *          KeyPairGeneratorSpi implementation for the
 *          specified algorithm.
 *
 * @see Provider
 */
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException {
    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 72 with Service

use of java.security.Provider.Service in project j2objc by google.

the class Signature method getInstance.

/**
 * Returns a Signature object that implements the specified signature
 * algorithm.
 *
 * <p> This method traverses the list of registered security Providers,
 * starting with the most preferred Provider.
 * A new Signature object encapsulating the
 * SignatureSpi 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.
 *
 * @param algorithm the standard name of the algorithm requested.
 * See the Signature section in the <a href=
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard algorithm names.
 *
 * @return the new Signature object.
 *
 * @exception NoSuchAlgorithmException if no Provider supports a
 *          Signature implementation for the
 *          specified algorithm.
 *
 * @see Provider
 */
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException {
    List<Service> list;
    if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) {
        list = GetInstance.getServices(rsaIds);
    } else {
        list = GetInstance.getServices("Signature", algorithm);
    }
    Iterator<Service> t = list.iterator();
    if (t.hasNext() == false) {
        throw new NoSuchAlgorithmException(algorithm + " Signature not available");
    }
    // try services until we find an Spi or a working Signature subclass
    NoSuchAlgorithmException failure;
    do {
        Service s = t.next();
        if (isSpi(s)) {
            // return new Delegate(s, t, algorithm);
            return new Delegate(algorithm);
        } else {
            // must be a subclass of Signature, disable dynamic selection
            try {
                Instance instance = GetInstance.getInstance(s, SignatureSpi.class);
                return getInstance(instance, algorithm);
            } catch (NoSuchAlgorithmException e) {
                failure = e;
            }
        }
    } while (t.hasNext());
    throw failure;
}
Also used : Instance(sun.security.jca.GetInstance.Instance) Service(java.security.Provider.Service)

Example 73 with Service

use of java.security.Provider.Service in project j2objc by google.

the class KeyAgreement method chooseProvider.

private void chooseProvider(int initType, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    synchronized (lock) {
        // if (spi != null) {
        if (spi != null && key == null) {
            implInit(spi, initType, key, params, random);
            return;
        }
        Exception lastException = null;
        // Android-changed: Provider selection; loop over a new list each time.
        for (Service s : GetInstance.getServices("KeyAgreement", algorithm)) {
            // 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 74 with Service

use of java.security.Provider.Service in project j2objc by google.

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;
        }
        // Android-removed: this debugging mechanism is not used in Android.
        /*
            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;
        // Android-changed: Provider selection; loop over a new list each time.
        for (Service s : GetInstance.getServices("KeyAgreement", algorithm)) {
            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 75 with Service

use of java.security.Provider.Service in project j2objc by google.

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)) {
    if (spi != null || lock == null) {
        return;
    }
    synchronized (lock) {
        if (spi != null) {
            return;
        }
        // Android-removed: this debugging mechanism is not used in Android.
        /*
            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;
        // Android-changed: Provider selection; loop over a new list each time.
        for (Service s : GetInstance.getServices("Mac", algorithm)) {
            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)

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