Search in sources :

Example 86 with Provider

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

the class KeyFactoryTest method testGetInstanceStringProvider.

@SuppressWarnings("unchecked")
public void testGetInstanceStringProvider() {
    try {
        KeyFactory factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME, provider);
        assertNotNull(factory);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    String[] algorithms = { "UnknownKeyFactory", null, TEST_KEYFACTORY_NAME, TEST_KEYFACTORY_NAME };
    Provider[] providers = { provider, provider, existingProvider, null };
    Class[] exceptions = { NoSuchAlgorithmException.class, NullPointerException.class, NoSuchAlgorithmException.class, IllegalArgumentException.class };
    for (int i = 0; i < algorithms.length; i++) {
        String algorithm = algorithms[i];
        Provider provider = providers[i];
        String message = "getInstance(" + (algorithm == null ? "null" : "\"" + algorithm + "\"") + ", " + (provider == null ? "null" : "provider");
        exceptionThrown = false;
        try {
            KeyFactory.getInstance(algorithm, provider);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyFactory(java.security.KeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) Provider(java.security.Provider)

Example 87 with Provider

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

the class KeyPairGenerator2Test method GetInstance03.

/**
     * Test for <code>getInstance(String algorithm, Provider provider)</code>
     * method
     * Assertions:
     * throws NullPointerException  when algorithm is null
     * throws NoSuchAlgorithmException when algorithm is incorrect;
     * throws IllegalArgumentException when provider is null;
     * returns KeyPairGenerator object
     */
private void GetInstance03(int mode) throws NoSuchAlgorithmException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        KeyPairGenerator.getInstance(null, mProv);
        fail("NullPointerException or KeyStoreException must be thrown");
    } catch (NoSuchAlgorithmException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            KeyPairGenerator.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++) {
        String alg = validValues[i].concat(post);
        try {
            KeyPairGenerator.getInstance(alg, prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(alg).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    KeyPairGenerator kpG;
    for (int i = 0; i < validValues.length; i++) {
        String alg = validValues[i].concat(post);
        kpG = KeyPairGenerator.getInstance(alg, mProv);
        assertEquals("Incorrect algorithm", kpG.getAlgorithm().toUpperCase(), (mode <= 2 ? resAlg : alg).toUpperCase());
        assertEquals("Incorrect provider", kpG.getProvider(), mProv);
        checkResult(kpG, mode);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyPairGenerator(java.security.KeyPairGenerator) Provider(java.security.Provider)

Example 88 with Provider

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

the class KeyPairGenerator4Test method test_getProvider.

/**
     * java.security.KeyPairGenerator#getProvider()
     */
public void test_getProvider() throws Exception {
    Provider p = KeyPairGenerator.getInstance("DSA").getProvider();
    assertNotNull("provider is null", p);
}
Also used : Provider(java.security.Provider)

Example 89 with Provider

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

the class KeyFactory2Test method getKeyFactoryAlgorithms.

/*
     * Returns the key algorithms that the given provider supports.
     */
private String[] getKeyFactoryAlgorithms(String providerName) {
    Vector<String> algs = new Vector<String>();
    Provider provider = Security.getProvider(providerName);
    if (provider == null)
        return new String[0];
    Enumeration<Object> e = provider.keys();
    while (e.hasMoreElements()) {
        String algorithm = (String) e.nextElement();
        if (algorithm.startsWith(KEYFACTORY_ID) && !algorithm.contains(" ")) {
            algs.addElement(algorithm.substring(KEYFACTORY_ID.length()));
        }
    }
    return algs.toArray(new String[algs.size()]);
}
Also used : Vector(java.util.Vector) Provider(java.security.Provider)

Example 90 with Provider

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

the class KeyFactory2Test method setUp.

protected void setUp() {
    if (keyfactAlgs == null) {
        Provider[] providers = Security.getProviders();
        // KeyFactory algorithms
        for (Provider provider : providers) {
            providerName = provider.getName();
            keyfactAlgs = getKeyFactoryAlgorithms(providerName);
            if (keyfactAlgs.length != 0) {
                break;
            }
        }
    }
}
Also used : 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