Search in sources :

Example 46 with Service

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

the class SecureRandom method getInstanceFromCryptoProvider.

private static SecureRandom getInstanceFromCryptoProvider(String algorithm) throws NoSuchAlgorithmException {
    Provider cryptoProvider;
    try {
        cryptoProvider = (Provider) SecureRandom.class.getClassLoader().loadClass("org.apache.harmony.security.provider.crypto.CryptoProvider").newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Service service = cryptoProvider.getService("SecureRandom", algorithm);
    Instance instance = GetInstance.getInstance(service, SecureRandomSpi.class);
    return new SecureRandom((SecureRandomSpi) instance.impl, instance.provider, algorithm);
}
Also used : Instance(sun.security.jca.GetInstance.Instance) Service(java.security.Provider.Service)

Example 47 with Service

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

the class ProviderTest method testGetServices.

// END android-added
// BEGIN android-added
public final void testGetServices() {
    MyProvider myProvider = new MyProvider(null, 1, null);
    Set<Provider.Service> services = myProvider.getServices();
    assertEquals(0, services.size());
    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]);
    actual = myProvider.getServices();
    assertEquals(1, 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]));
}
Also used : Service(java.security.Provider.Service) Service(java.security.Provider.Service) Provider(java.security.Provider)

Example 48 with Service

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

the class ProviderTest method testPutObjectObject.

/*
     * Class under test for Object put(Object, Object)
     */
public final void testPutObjectObject() {
    p.put("MessageDigest.SHA-1", "aaa.bbb.ccc.ddd");
    p.put("Type.Algorithm", "className");
    assertEquals("aaa.bbb.ccc.ddd", p.getProperty("MessageDigest.SHA-1").trim());
    Set services = p.getServices();
    assertEquals(3, services.size());
    for (Iterator it = services.iterator(); it.hasNext(); ) {
        Provider.Service s = (Provider.Service) it.next();
        if ("Type".equals(s.getType()) && "Algorithm".equals(s.getAlgorithm()) && "className".equals(s.getClassName())) {
            continue;
        }
        if ("MessageDigest".equals(s.getType()) && "SHA-1".equals(s.getAlgorithm()) && "aaa.bbb.ccc.ddd".equals(s.getClassName())) {
            continue;
        }
        if ("MessageDigest".equals(s.getType()) && "abc".equals(s.getAlgorithm()) && "SomeClassName".equals(s.getClassName())) {
            continue;
        }
        fail("Incorrect service");
    }
}
Also used : Set(java.util.Set) Iterator(java.util.Iterator) Service(java.security.Provider.Service) Service(java.security.Provider.Service) Provider(java.security.Provider)

Example 49 with Service

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

the class KeyPairGeneratorTest method test_KeyWithAllKeyFactories.

private void test_KeyWithAllKeyFactories(Key k) throws Exception {
    byte[] encoded = k.getEncoded();
    String keyAlgo = k.getAlgorithm();
    Provider[] providers = Security.getProviders();
    for (Provider p : providers) {
        Set<Provider.Service> services = p.getServices();
        for (Provider.Service service : services) {
            if (!"KeyFactory".equals(service.getType())) {
                continue;
            }
            if (!service.getAlgorithm().equals(keyAlgo)) {
                continue;
            }
            if ("PKCS#8".equals(k.getFormat())) {
                PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded);
                KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
                PrivateKey privKey = kf.generatePrivate(spec);
                assertNotNull(k.getAlgorithm() + ", provider=" + p.getName(), privKey);
                /*
                     * EC keys are unique because they can have explicit parameters or a curve
                     * name. Check them specially so this test can continue to function.
                     */
                if (k instanceof ECPrivateKey) {
                    assertECPrivateKeyEquals((ECPrivateKey) k, (ECPrivateKey) privKey);
                } else {
                    assertEquals(k.getAlgorithm() + ", provider=" + p.getName(), Arrays.toString(encoded), Arrays.toString(privKey.getEncoded()));
                }
            } else if ("X.509".equals(k.getFormat())) {
                X509EncodedKeySpec spec = new X509EncodedKeySpec(encoded);
                KeyFactory kf = KeyFactory.getInstance(k.getAlgorithm(), p);
                PublicKey pubKey = kf.generatePublic(spec);
                assertNotNull(pubKey);
                assertTrue(Arrays.equals(encoded, pubKey.getEncoded()));
            }
        }
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) Service(java.security.Provider.Service) Service(java.security.Provider.Service) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) Provider(java.security.Provider) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 50 with Service

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

the class KeyPairGeneratorTest method testDSAGeneratorWithParams.

public void testDSAGeneratorWithParams() throws Exception {
    final DSAParameterSpec dsaSpec = new DSAParameterSpec(DSA_P, DSA_Q, DSA_G);
    boolean failure = false;
    final Provider[] providers = Security.getProviders();
    for (final Provider p : providers) {
        Service s = p.getService("KeyPairGenerator", "DSA");
        if (s == null) {
            continue;
        }
        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
        kpg.initialize(dsaSpec);
        KeyPair pair = kpg.generateKeyPair();
        DSAPrivateKey privKey = (DSAPrivateKey) pair.getPrivate();
        DSAPublicKey pubKey = (DSAPublicKey) pair.getPublic();
        DSAParams actualParams = privKey.getParams();
        assertNotNull("DSA params should not be null", actualParams);
        assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
        assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
        assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
        actualParams = pubKey.getParams();
        assertNotNull("DSA params should not be null", actualParams);
        assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
        assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
        assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
    }
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPair(java.security.KeyPair) Service(java.security.Provider.Service) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) Provider(java.security.Provider) DSAPublicKey(java.security.interfaces.DSAPublicKey)

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