Search in sources :

Example 41 with Service

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

the class Mac method getInstance.

/**
 * Returns a <code>Mac</code> object that implements the
 * specified MAC algorithm.
 *
 * <p> This method traverses the list of registered security Providers,
 * starting with the most preferred Provider.
 * A new Mac object encapsulating the
 * MacSpi 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 requested MAC algorithm.
 * See the Mac section in the <a href=
 *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Mac">
 * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
 * for information about standard algorithm names.
 *
 * @return the new <code>Mac</code> object.
 *
 * @exception NoSuchAlgorithmException if no Provider supports a
 *          MacSpi implementation for the
 *          specified algorithm.
 *
 * @see java.security.Provider
 */
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException {
    List<Service> services = GetInstance.getServices("Mac", algorithm);
    // make sure there is at least one service from a signed provider
    Iterator<Service> t = services.iterator();
    while (t.hasNext()) {
        Service s = t.next();
        if (JceSecurity.canUseProvider(s.getProvider()) == false) {
            continue;
        }
        // return new Mac(s, t, algorithm);
        return new Mac(algorithm);
    }
    throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available");
}
Also used : Service(java.security.Provider.Service)

Example 42 with Service

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

the class GetInstance method getService.

public static Service getService(String type, String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
    if ((provider == null) || (provider.length() == 0)) {
        throw new IllegalArgumentException("missing provider");
    }
    Provider p = Providers.getProviderList().getProvider(provider);
    if (p == null) {
        throw new NoSuchProviderException("no such provider: " + provider);
    }
    Service s = p.getService(type, algorithm);
    if (s == null) {
        throw new NoSuchAlgorithmException("no such algorithm: " + algorithm + " for provider " + provider);
    }
    return s;
}
Also used : Service(java.security.Provider.Service)

Example 43 with Service

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

the class ProviderServiceTest method testNewlyCreatedServiceDoesntGetsRegistered.

public void testNewlyCreatedServiceDoesntGetsRegistered() throws Exception {
    String type = "SecureRandom";
    String algorithm = "algorithm";
    MyProvider p = new MyProvider();
    Provider.Service s = new Provider.Service(p, type, algorithm, "org.apache.harmony.security.tests.support.RandomImpl", null, null);
    assertSame(p, s.getProvider());
    assertNull(p.getService(type, algorithm));
    try {
        Object o = s.newInstance(null);
        fail("Expected " + NoSuchAlgorithmException.class.getName() + " as the service is not " + "registered with the provider");
    } catch (NoSuchAlgorithmException e) {
    // Expected.
    }
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 44 with Service

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

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

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

the class GetInstance method getInstance.

public static Instance getInstance(String type, Class clazz, String algorithm, Object param) throws NoSuchAlgorithmException {
    List<Service> services = getServices(type, algorithm);
    NoSuchAlgorithmException failure = null;
    for (Service s : services) {
        try {
            return getInstance(s, clazz, param);
        } catch (NoSuchAlgorithmException e) {
            failure = e;
        }
    }
    if (failure != null) {
        throw failure;
    } else {
        throw new NoSuchAlgorithmException(algorithm + " " + type + " not available");
    }
}
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