Search in sources :

Example 61 with Provider

use of java.security.Provider in project poi by apache.

the class CryptoFunctions method registerBouncyCastle.

@SuppressWarnings("unchecked")
public static void registerBouncyCastle() {
    if (Security.getProvider("BC") != null) {
        return;
    }
    try {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String bcProviderName = "org.bouncycastle.jce.provider.BouncyCastleProvider";
        Class<Provider> clazz = (Class<Provider>) cl.loadClass(bcProviderName);
        Security.addProvider(clazz.newInstance());
    } catch (Exception e) {
        throw new EncryptedDocumentException("Only the BouncyCastle provider supports your encryption settings - please add it to the classpath.", e);
    }
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) GeneralSecurityException(java.security.GeneralSecurityException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) DigestException(java.security.DigestException) Provider(java.security.Provider)

Example 62 with Provider

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

the class TrustManagerFactoryTest method test_TrustManagerFactory_getInstance.

public void test_TrustManagerFactory_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("TrustManagerFactory")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            {
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
                assertEquals(algorithm, tmf.getAlgorithm());
                test_TrustManagerFactory(tmf);
            }
            {
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm, provider);
                assertEquals(algorithm, tmf.getAlgorithm());
                assertEquals(provider, tmf.getProvider());
                test_TrustManagerFactory(tmf);
            }
            {
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm, provider.getName());
                assertEquals(algorithm, tmf.getAlgorithm());
                assertEquals(provider, tmf.getProvider());
                test_TrustManagerFactory(tmf);
            }
        }
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Provider(java.security.Provider)

Example 63 with Provider

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

the class Security2Test method getProvidersCount.

private int getProvidersCount(Map filterMap) {
    int result = 0;
    Provider[] allProviders = Security.getProviders();
    // for each provider
    for (Provider provider : allProviders) {
        Set allProviderKeys = provider.keySet();
        boolean noMatchFoundForFilterEntry = false;
        // for each filter item
        for (Object filter : filterMap.keySet()) {
            String filterString = (String) filter;
            // but I have seen them)
            if (filterString.endsWith("=")) {
                filterString = filterString.substring(0, filterString.length() - 1);
            }
            if (filterString != null) {
                if (filterString.indexOf(" ") == -1) {
                    // current provider ?
                    if (!allProviderKeys.contains(filterString)) {
                        // alias.
                        if (!allProviderKeys.contains("Alg.Alias." + filterString)) {
                            noMatchFoundForFilterEntry = true;
                            // out of while loop
                            break;
                        }
                    }
                } else {
                    // handle filter strings with attribute names
                    if (allProviderKeys.contains(filterString)) {
                        // Does the corresponding values match ?
                        String filterVal = (String) filterMap.get(filterString);
                        String providerVal = (String) provider.get(filterString);
                        if (providerVal == null || !providerVal.equals(filterVal)) {
                            noMatchFoundForFilterEntry = true;
                            // out of while loop
                            break;
                        }
                    }
                // end if filter string with named attribute is
                // found
                }
            // end else
            }
        // end if non-null key
        }
        if (!noMatchFoundForFilterEntry) {
            // Current provider is a match for the filterMap
            result++;
        }
    }
    return result;
}
Also used : Set(java.util.Set) Support_TestProvider(tests.support.Support_TestProvider) Provider(java.security.Provider)

Example 64 with Provider

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

the class SecurityTest method testGetAlgorithmPropertyLjava_lang_String_java_lang_String.

/**
     * java.security.Security#getAlgorithmProperty(String algName, String
     *        propName)
     * @disabled because Security.getAlgorithmProperty looks for
     *           "propName.algName" instead of "Alg.propName.algName"
     */
@SuppressWarnings("deprecation")
public final void testGetAlgorithmPropertyLjava_lang_String_java_lang_String() {
    Provider provider = new MyProvider();
    Map<String, String> m = new HashMap<String, String>();
    m.clear();
    m.put("Alg.propName.algName", "value");
    provider.putAll(m);
    try {
        Security.addProvider(provider);
        assertNotNull(Security.getAlgorithmProperty("algName", "propName"));
        assertNull(Security.getAlgorithmProperty("DSA", null));
        assertNull(Security.getAlgorithmProperty("DSA", "propName"));
    } finally {
        Security.removeProvider(provider.getName());
    }
}
Also used : HashMap(java.util.HashMap) Provider(java.security.Provider)

Example 65 with Provider

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

the class SecurityTest method testGetProviders.

/**
     * Test that Security.getProviders does case sensitive operations
     * independent of its locale.
     */
private void testGetProviders(Locale locale) {
    Locale defaultLocale = Locale.getDefault();
    Locale.setDefault(locale);
    Provider p = new MyProvider();
    try {
        Security.addProvider(p);
        String filter = "MyService.MyAlgorithm";
        assertTrue(filter, Arrays.equals(new Provider[] { p }, Security.getProviders(filter)));
        filter = "MyService.MyAlgorithm KeySize:512";
        assertTrue(filter, Arrays.equals(new Provider[] { p }, Security.getProviders(filter)));
        filter = "MyService.MyAlgorithm KeySize:1025";
        assertNull(filter, Security.getProviders(filter));
        // attribute name and value are case insensitive
        filter = "MyService.MyAlgorithm imPLementedIn:softWARE";
        assertTrue(filter, Arrays.equals(new Provider[] { p }, Security.getProviders(filter)));
        filter = "MyService.MyAlgorithm ATTribute:attributeVALUE";
        assertTrue(filter, Arrays.equals(new Provider[] { p }, Security.getProviders(filter)));
        // Turkish dotless i
        filter = "MyService.MyAlgorithm İmPLementedın:softWARE";
        assertTrue(filter, Arrays.equals(new Provider[] { p }, Security.getProviders(filter)));
        // Regression for HARMONY-2761
        filter = "MyService.NoKeySize KeySize:512";
        assertNull(filter, Security.getProviders(filter));
        filter = "MyService.NoImplementedIn ImplementedIn:Software";
        assertNull(filter, Security.getProviders(filter));
        filter = "ABCService.NoAttribute Attribute:ABC";
        assertNull(filter, Security.getProviders(filter));
    } finally {
        // clean up
        Security.removeProvider(p.getName());
        Locale.setDefault(defaultLocale);
    }
}
Also used : Locale(java.util.Locale) 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