Search in sources :

Example 11 with NoSuchProviderException

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

the class X509CertificateTest method testVerifyPublicKey.

/**
     * @throws SignatureException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws CertificateException
     * {@link Certificate#verify(PublicKey)}
     */
@SideEffect("Destroys MD5 provider, hurts succeeding tests")
public void testVerifyPublicKey() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, CertificateException {
    // Preconditions
    assertNotNull(javaxCert.getPublicKey());
    assertNotNull(javaxSSCert.getPublicKey());
    // here not self signed:
    try {
        javaxCert.verify(javaxCert.getPublicKey());
    } catch (SignatureException e) {
    // ok
    }
    PublicKey k = javaxCert.getPublicKey();
    MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k);
    changedEncoding.setEncoding(new byte[javaxCert.getEncoded().length - 1]);
    try {
        javaxCert.verify(tbt_cert.getPublicKey());
    } catch (InvalidKeyException e) {
    // ok
    }
    try {
        javaxCert.verify(null);
    } catch (Exception e) {
    // ok
    }
    try {
        javaxCert.verify(changedEncoding);
        fail("Exception expected");
    } catch (Exception e) {
    // ok
    }
// following test doesn't work because the algorithm is derived from
// somewhere else.
// MyModifiablePublicKey changedAlgo = new MyModifiablePublicKey(k);
// changedAlgo.setAlgorithm("MD5withBla");
// try {
//     javaxCert.verify(changedAlgo);
//     fail("Exception expected");
// } catch (SignatureException e) {
//     // ok
// }
// Security.removeProvider(mySSProvider.getName());
// try {
//     javaxSSCert.verify(javaxSSCert.getPublicKey());
// } catch (NoSuchProviderException e) {
//     // ok
// }
// Security.addProvider(mySSProvider);
// must always evaluate true for self signed
// javaxSSCert.verify(javaxSSCert.getPublicKey());
}
Also used : PublicKey(java.security.PublicKey) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateExpiredException(javax.security.cert.CertificateExpiredException) CertificateNotYetValidException(javax.security.cert.CertificateNotYetValidException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) CertificateException(javax.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) SideEffect(dalvik.annotation.SideEffect)

Example 12 with NoSuchProviderException

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

the class TrustManagerFactory2Test method test_getInstanceLjava_lang_StringLjava_lang_String.

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

Example 13 with NoSuchProviderException

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

the class KeyFactoryTest method testGetInstanceStringString.

@SuppressWarnings("unchecked")
public void testGetInstanceStringString() {
    try {
        KeyFactory factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME, TEST_PROVIDER_NAME);
        assertNotNull(factory);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    }
    String[][] combinations = { { "UnknownKeyFactory", TEST_PROVIDER_NAME }, { TEST_KEYFACTORY_NAME, "UnknownProvider" }, { TEST_KEYFACTORY_NAME, existingProvider.getName() }, { null, TEST_PROVIDER_NAME }, { TEST_KEYFACTORY_NAME, null }, { null, null } };
    Class[] exceptions = { NoSuchAlgorithmException.class, NoSuchProviderException.class, NoSuchAlgorithmException.class, NullPointerException.class, IllegalArgumentException.class, IllegalArgumentException.class };
    for (int i = 0; i < combinations.length; i++) {
        String[] combination = combinations[i];
        String message = "getInstance(\"" + combination[0] + "\", \"" + combination[1] + "\")";
        exceptionThrown = false;
        try {
            KeyFactory.getInstance(combination[0], combination[1]);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) 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)

Example 14 with NoSuchProviderException

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

the class CertPathBuilder2Test method testGetInstance02.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code> method
     * Assertions:
     * throws NullPointerException when algorithm is null
     * throws NoSuchAlgorithmException when algorithm  is not correct
     * throws IllegalArgumentException when provider is null or empty;
     * throws NoSuchProviderException when provider is available;
     * returns CertPathBuilder object
     */
public void testGetInstance02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, InvalidAlgorithmParameterException, CertPathBuilderException {
    try {
        CertPathBuilder.getInstance(null, mProv.getName());
        fail("NullPointerException or NoSuchAlgorithmException must be thrown when algorithm is null");
    } catch (NullPointerException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            CertPathBuilder.getInstance(invalidValues[i], mProv.getName());
            fail("NoSuchAlgorithmException must be thrown (type: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    String prov = null;
    for (int i = 0; i < validValues.length; i++) {
        try {
            CertPathBuilder.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (type: ".concat(validValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
        try {
            CertPathBuilder.getInstance(validValues[i], "");
            fail("IllegalArgumentException must be thrown when provider is empty (type: ".concat(validValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    for (int i = 0; i < validValues.length; i++) {
        for (int j = 1; j < invalidValues.length; j++) {
            try {
                CertPathBuilder.getInstance(validValues[i], invalidValues[j]);
                fail("NoSuchProviderException must be thrown (type: ".concat(validValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
            } catch (NoSuchProviderException e) {
            }
        }
    }
    CertPathBuilder cerPB;
    for (int i = 0; i < validValues.length; i++) {
        cerPB = CertPathBuilder.getInstance(validValues[i], mProv.getName());
        assertEquals("Incorrect type", cerPB.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", cerPB.getProvider().getName(), mProv.getName());
        checkResult(cerPB);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertPathBuilder(java.security.cert.CertPathBuilder) NoSuchProviderException(java.security.NoSuchProviderException)

Example 15 with NoSuchProviderException

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

the class KeyStore4Test method testGetInstanceStringString.

public void testGetInstanceStringString() {
    try {
        KeyStore ks = KeyStore.getInstance("TestKeyStore", provider.getName());
        assertNotNull("keystore is null", ks);
        assertEquals("KeyStore is not of expected type", "TestKeyStore", ks.getType());
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    }
    try {
        KeyStore.getInstance("UnknownKeyStore", provider.getName());
        fail("expected KeyStoreException");
    } catch (KeyStoreException e) {
    // ok
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    }
    try {
        KeyStore.getInstance("TestKeyStore", (String) null);
        fail("expected IllegalArgumentException");
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    } catch (IllegalArgumentException e) {
    // ok
    }
    try {
        KeyStore.getInstance("TestKeyStore", "");
        fail("expected IllegalArgumentException");
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    } catch (IllegalArgumentException e) {
    // ok
    }
    try {
        KeyStore.getInstance(null, provider.getName());
        fail("expected KeyStoreException");
    } catch (KeyStoreException e) {
    // ok
    } catch (NoSuchProviderException e) {
        fail("unexpected exception: " + e);
    } catch (NullPointerException e) {
    // also ok
    }
    try {
        KeyStore.getInstance("TestKeyStore", "UnknownProvider");
        fail("expected NoSuchProviderException");
    } catch (NoSuchProviderException e) {
    // ok
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) NoSuchProviderException(java.security.NoSuchProviderException) KeyStore(java.security.KeyStore)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)93 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)67 InvalidKeyException (java.security.InvalidKeyException)30 IOException (java.io.IOException)28 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 CertificateException (java.security.cert.CertificateException)18 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 KeyStoreException (java.security.KeyStoreException)11 SignatureException (java.security.SignatureException)10 X509Certificate (java.security.cert.X509Certificate)10 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8 KeyStore (java.security.KeyStore)7 Provider (java.security.Provider)7