Search in sources :

Example 91 with Provider

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

the class myCertPathBuilder method testCertPathBuilder08.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code> method
     * Assertion: throws IllegalArgumentException when provider is null
     */
public void testCertPathBuilder08() throws NoSuchAlgorithmException {
    if (!PKIXSupport) {
        fail(NotSupportMsg);
        return;
    }
    Provider prov = null;
    for (int t = 0; t < validValues.length; t++) {
        try {
            CertPathBuilder.getInstance(validValues[t], prov);
            fail("IllegalArgumentException must be thrown");
        } catch (IllegalArgumentException e1) {
        }
    }
}
Also used : Provider(java.security.Provider)

Example 92 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 93 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)

Example 94 with Provider

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

the class KeyStoreTest method test_KeyStore_create.

public void test_KeyStore_create() 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("KeyStore")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            KeyStore ks = KeyStore.getInstance(algorithm, provider);
            assertEquals(provider, ks.getProvider());
            assertEquals(algorithm, ks.getType());
        }
    }
}
Also used : KeyStore(java.security.KeyStore) Provider(java.security.Provider)

Example 95 with Provider

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

the class KeyStoreTest method keyStores.

public static List<KeyStore> keyStores() throws Exception {
    List<KeyStore> keyStores = new ArrayList<KeyStore>();
    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("KeyStore")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            KeyStore ks = KeyStore.getInstance(algorithm, provider);
            assertEquals(provider, ks.getProvider());
            assertEquals(algorithm, ks.getType());
            if (!isUnsupported(ks)) {
                keyStores.add(ks);
            }
        }
    }
    return keyStores;
}
Also used : ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) Provider(java.security.Provider)

Aggregations

Provider (java.security.Provider)243 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)49 ArrayList (java.util.ArrayList)26 MessageDigest (java.security.MessageDigest)21 List (java.util.List)20 Key (java.security.Key)19 KeyStore (java.security.KeyStore)19 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 SecureRandom (java.security.SecureRandom)13 IOException (java.io.IOException)12 NoSuchProviderException (java.security.NoSuchProviderException)12 SecretKey (javax.crypto.SecretKey)12 KeyStoreException (java.security.KeyStoreException)11 CertificateException (java.security.cert.CertificateException)11 Cipher (javax.crypto.Cipher)11 KeyGenerator (javax.crypto.KeyGenerator)11 MyExemptionMechanismSpi.tmpKey (org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey)11