Search in sources :

Example 1 with Service

use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.

the class SupportsParameter method main.

public static void main(String[] args) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(512);
    KeyPair kp = kpg.generateKeyPair();
    PublicKey dsaPublicKey = kp.getPublic();
    PrivateKey dsaPrivateKey = kp.getPrivate();
    PublicKey myPublicKey = new MyPublicKey();
    PrivateKey myPrivateKey = new MyPrivateKey();
    SecretKey mySecretKey = new MySecretKey();
    Provider p = new MyProvider();
    Service s;
    // none specified, always true
    s = p.getService("Signature", "DSA0");
    checkSupports(s, null, true);
    checkSupports(s, dsaPublicKey, true);
    checkSupports(s, dsaPrivateKey, true);
    checkSupports(s, myPublicKey, true);
    checkSupports(s, myPrivateKey, true);
    checkSupports(s, mySecretKey, true);
    // DSAPublic/PrivateKey specified as classes
    s = p.getService("Signature", "DSA");
    checkSupports(s, dsaPublicKey, true);
    checkSupports(s, dsaPrivateKey, true);
    checkSupports(s, myPublicKey, false);
    checkSupports(s, myPrivateKey, false);
    checkSupports(s, mySecretKey, false);
    // X.509/PKCS#8 specified as formats
    s = p.getService("Signature", "DSA2");
    checkSupports(s, dsaPublicKey, true);
    checkSupports(s, dsaPrivateKey, true);
    checkSupports(s, myPublicKey, false);
    checkSupports(s, myPrivateKey, false);
    checkSupports(s, mySecretKey, false);
    // MyPublic/PrivateKey + DSAPublicKey specified as classes
    s = p.getService("Signature", "DSA3");
    checkSupports(s, dsaPublicKey, true);
    checkSupports(s, dsaPrivateKey, false);
    checkSupports(s, myPublicKey, true);
    checkSupports(s, myPrivateKey, true);
    checkSupports(s, mySecretKey, false);
    // MyPublic/PrivateKey + DSAPublicKey specified as classes
    s = p.getService("Cipher", "DES");
    checkSupports(s, dsaPublicKey, false);
    checkSupports(s, dsaPrivateKey, false);
    checkSupports(s, myPublicKey, false);
    checkSupports(s, myPrivateKey, false);
    checkSupports(s, mySecretKey, true);
    Key secretKeySpec = new SecretKeySpec(new byte[8], "DES");
    checkSupports(s, secretKeySpec, true);
}
Also used : Service(java.security.Provider.Service) SecretKeySpec(javax.crypto.spec.SecretKeySpec)

Example 2 with Service

use of java.security.Provider.Service in project robovm by robovm.

the class ProviderTest method testRemoveService.

// END android-added
// BEGIN android-added
public final void testRemoveService() {
    MyProvider myProvider = new MyProvider(null, 1, null);
    try {
        myProvider.removeService(null);
        fail("NullPoiterException expected");
    } catch (NullPointerException e) {
    // expected
    }
    Provider.Service[] s = new Provider.Service[3];
    s[0] = new Provider.Service(p, "type0", "algorithm0", "className0", null, null);
    s[1] = new Provider.Service(p, "type1", "algorithm1", "className1", null, null);
    s[2] = new Provider.Service(p, "type2", "algorithm2", "className2", null, null);
    try {
        myProvider.removeService(s[0]);
    } catch (NullPointerException e) {
        fail("Unexpected exception");
    }
    myProvider.putService(s[0]);
    myProvider.putService(s[1]);
    myProvider.putService(s[2]);
    assertEquals(3, myProvider.getNumServices());
    Set<Service> actual = myProvider.getServices();
    assertTrue(actual.contains(s[0]));
    assertTrue(actual.contains(s[1]));
    assertTrue(actual.contains(s[2]));
    myProvider.removeService(s[1]);
    assertEquals(2, myProvider.getNumServices());
    actual = myProvider.getServices();
    assertTrue(actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(actual.contains(s[2]));
    myProvider.removeService(s[0]);
    assertEquals(1, myProvider.getNumServices());
    actual = myProvider.getServices();
    assertTrue(!actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(actual.contains(s[2]));
    myProvider.removeService(s[2]);
    assertEquals(0, myProvider.getNumServices());
    actual = myProvider.getServices();
    assertTrue(!actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(!actual.contains(s[2]));
    try {
        myProvider.removeService(null);
        fail("NullPoiterException expected");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) Provider(java.security.Provider)

Example 3 with Service

use of java.security.Provider.Service in project robovm by robovm.

the class ProviderTest method testPutService.

// END android-added
// BEGIN android-added
public final void testPutService() {
    MyProvider myProvider = new MyProvider(null, 1, null);
    Provider.Service[] s = new Provider.Service[3];
    s[0] = new Provider.Service(p, "type1", "algorithm1", "className1", null, null);
    s[1] = new Provider.Service(p, "type2", "algorithm2", "className2", null, null);
    s[2] = new Provider.Service(p, "type3", "algorithm3", "className3", null, null);
    myProvider.putService(s[0]);
    myProvider.putService(s[1]);
    assertEquals(2, myProvider.getNumServices());
    Set<Service> actual = myProvider.getServices();
    assertTrue(actual.contains(s[0]));
    assertTrue(actual.contains(s[1]));
    assertTrue(!actual.contains(s[2]));
    myProvider.removeService(s[1]);
    assertEquals(1, myProvider.getNumServices());
    actual = myProvider.getServices();
    assertTrue(actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(!actual.contains(s[2]));
    myProvider.putService(s[2]);
    actual = myProvider.getServices();
    assertEquals(2, myProvider.getNumServices());
    assertTrue(actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(actual.contains(s[2]));
    myProvider.putService(s[2]);
    actual = myProvider.getServices();
    assertEquals(2, myProvider.getNumServices());
    assertTrue(actual.contains(s[0]));
    assertTrue(!actual.contains(s[1]));
    assertTrue(actual.contains(s[2]));
    try {
        myProvider.putService(null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) Provider(java.security.Provider)

Example 4 with Service

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

the class JceSecurity method getInstance.

static Instance getInstance(String type, Class clazz, String algorithm) throws NoSuchAlgorithmException {
    List services = GetInstance.getServices(type, algorithm);
    NoSuchAlgorithmException failure = null;
    for (Iterator t = services.iterator(); t.hasNext(); ) {
        Service s = (Service) t.next();
        if (canUseProvider(s.getProvider()) == false) {
            // allow only signed providers
            continue;
        }
        try {
            Instance instance = GetInstance.getInstance(s, clazz);
            return instance;
        } catch (NoSuchAlgorithmException e) {
            failure = e;
        }
    }
    throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available", failure);
}
Also used : Instance(sun.security.jca.GetInstance.Instance) Service(java.security.Provider.Service)

Example 5 with Service

use of java.security.Provider.Service in project camel by apache.

the class ApnsUtils method getAlgorithm.

public static String getAlgorithm() {
    List<String> keys = new LinkedList<String>();
    List<String> trusts = new LinkedList<String>();
    for (Provider p : Security.getProviders()) {
        for (Service s : p.getServices()) {
            if ("KeyManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                keys.add(s.getAlgorithm());
            } else if ("TrustManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                trusts.add(s.getAlgorithm());
            }
        }
    }
    keys.retainAll(trusts);
    return keys.get(0);
}
Also used : Service(java.security.Provider.Service) LinkedList(java.util.LinkedList) Provider(java.security.Provider)

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