Search in sources :

Example 26 with Provider

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

the class MyProvider method test_engineInit_02.

/**
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
     */
public void test_engineInit_02() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    Parameters pr = null;
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        pr = new Parameters(ks);
        tmf.init(pr);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
    assertTrue(factory.isEngineInitCalled());
    assertEquals(pr, factory.getSpec());
    factory.reset();
    tmf.init((ManagerFactoryParameters) null);
    assertTrue(factory.isEngineInitCalled());
    assertNull(factory.getSpec());
}
Also used : Parameters(org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi.Parameters) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 27 with Provider

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

the class MyProvider method test_engineGetTrustManagers.

/**
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineGetTrustManagers()
     */
public void test_engineGetTrustManagers() throws NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    TrustManager[] tm = tmf.getTrustManagers();
    assertTrue(factory.isEngineGetTrustManagersCalled());
    factory.reset();
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        tmf.init(ks);
        tm = tmf.getTrustManagers();
        assertTrue(factory.isEngineGetTrustManagersCalled());
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider) TrustManager(javax.net.ssl.TrustManager)

Example 28 with Provider

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

the class TrustManagerFactory2Test method testLjava_lang_StringLjava_security_Provider.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertions:
     * throws NullPointerException when algorithm is null;
     * throws NoSuchAlgorithmException when algorithm is not correct;
     * throws IllegalArgumentException when provider is null;
     * returns TrustManagerFactory object
     */
public void testLjava_lang_StringLjava_security_Provider() throws Exception {
    try {
        TrustManagerFactory.getInstance(null, mProv);
        fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    } catch (NoSuchAlgorithmException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            TrustManagerFactory.getInstance(invalidValues[i], mProv);
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    Provider prov = null;
    for (int i = 0; i < validValues.length; i++) {
        try {
            TrustManagerFactory.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    TrustManagerFactory tmf;
    for (int i = 0; i < validValues.length; i++) {
        tmf = TrustManagerFactory.getInstance(validValues[i], mProv);
        assertTrue("Not instanceof TrustManagerFactory object", tmf instanceof TrustManagerFactory);
        assertEquals("Incorrect algorithm", tmf.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", tmf.getProvider(), mProv);
        checkResult(tmf);
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 29 with Provider

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

the class ProviderTest method test_Provider_Properties.

/**
     * Makes sure all provider properties either point to a class
     * implementation that exists or are aliases to known algorithms.
     */
public void test_Provider_Properties() throws Exception {
    /*
         * A useful reference on Provider properties
         * <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/HowToImplAProvider.html>
         * How to Implement a Provider in the Java &trade; Cryptography Architecture
         * </a>
         */
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        // check Provider.id proprieties
        assertEquals(provider.getName(), provider.get("Provider.id name"));
        assertEquals(String.valueOf(provider.getVersion()), provider.get("Provider.id version"));
        assertEquals(provider.getInfo(), provider.get("Provider.id info"));
        assertEquals(provider.getClass().getName(), provider.get("Provider.id className"));
        // build map of all known aliases and implementations
        Map<String, String> aliases = new HashMap<String, String>();
        Map<String, String> implementations = new HashMap<String, String>();
        for (Entry<Object, Object> entry : provider.entrySet()) {
            Object k = entry.getKey();
            Object v = entry.getValue();
            assertEquals(String.class, k.getClass());
            assertEquals(String.class, v.getClass());
            String key = (String) k;
            String value = (String) v;
            // skip Provider.id keys, we check well known ones values above
            if (key.startsWith("Provider.id ")) {
                continue;
            }
            // skip property settings such as: "Signature.SHA1withDSA ImplementedIn" "Software"
            if (key.indexOf(' ') != -1) {
                continue;
            }
            Matcher m = alias.matcher(key);
            if (m.find()) {
                String type = m.group(1);
                aliases.put(key, type + "." + value);
            } else {
                implementations.put(key, value);
            }
        }
        // verify implementation classes are available
        for (Entry<String, String> entry : implementations.entrySet()) {
            String typeAndAlgorithm = entry.getKey();
            String className = entry.getValue();
            try {
                assertNotNull(Class.forName(className, true, provider.getClass().getClassLoader()));
            } catch (ClassNotFoundException e) {
                // Sun forgot their own class
                if (!className.equals("sun.security.pkcs11.P11MAC")) {
                    fail("Could not find class " + className + " for " + typeAndAlgorithm);
                }
            }
        }
        // make sure all aliases point to some known implementation
        for (Entry<String, String> entry : aliases.entrySet()) {
            String alias = entry.getKey();
            String actual = entry.getValue();
            assertTrue("Could not find implementation " + actual + " for alias " + alias, implementations.containsKey(actual));
        }
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) Provider(java.security.Provider)

Example 30 with Provider

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

the class SecureRandomTest method test_getInstance.

public void test_getInstance() throws Exception {
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if (!type.equals("SecureRandom")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            try {
                SecureRandom sr1 = SecureRandom.getInstance(algorithm);
                assertEquals(algorithm, sr1.getAlgorithm());
                test_SecureRandom(sr1);
                // SecureRandom.getInstance(String, Provider)
                SecureRandom sr2 = SecureRandom.getInstance(algorithm, provider);
                assertEquals(algorithm, sr2.getAlgorithm());
                assertEquals(provider, sr2.getProvider());
                test_SecureRandom(sr2);
                // SecureRandom.getInstance(String, String)
                SecureRandom sr3 = SecureRandom.getInstance(algorithm, provider.getName());
                assertEquals(algorithm, sr3.getAlgorithm());
                assertEquals(provider, sr3.getProvider());
                test_SecureRandom(sr3);
                System.out.println("SecureRandomTest " + algorithm + " and provider " + provider.getName());
            } catch (Exception e) {
                throw new Exception("Problem testing SecureRandom." + algorithm + ", provider: " + provider.getName(), e);
            }
        }
    }
}
Also used : SecureRandom(java.security.SecureRandom) Provider(java.security.Provider)

Aggregations

Provider (java.security.Provider)229 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)49 ArrayList (java.util.ArrayList)25 MessageDigest (java.security.MessageDigest)21 KeyStore (java.security.KeyStore)19 List (java.util.List)18 Service (java.security.Provider.Service)15 ExemptionMechanism (javax.crypto.ExemptionMechanism)14 SpiEngUtils (org.apache.harmony.security.tests.support.SpiEngUtils)14 InvalidKeyException (java.security.InvalidKeyException)13 IOException (java.io.IOException)12 Key (java.security.Key)12 NoSuchProviderException (java.security.NoSuchProviderException)12 SecureRandom (java.security.SecureRandom)12 SecretKey (javax.crypto.SecretKey)12 KeyStoreException (java.security.KeyStoreException)11 Cipher (javax.crypto.Cipher)11 KeyGenerator (javax.crypto.KeyGenerator)11 MyExemptionMechanismSpi.tmpKey (org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey)11 CertificateException (java.security.cert.CertificateException)10