Search in sources :

Example 41 with InvalidAlgorithmParameterException

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

the class PKIXBuilderParametersTest method testPKIXBuilderParametersKeyStoreCertSelector03.

/**
     * Test #3 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
     * constructor<br>
     * Assertion: <code>InvalidAlgorithmParameterException</code> - if the
     * <code>keystore</code> does not contain at least one trusted certificate
     * entry
     */
public final void testPKIXBuilderParametersKeyStoreCertSelector03() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    try {
        new PKIXBuilderParameters(keyTest, new X509CertSelector());
        fail("InvalidAlgorithmParameterException expected");
    } catch (InvalidAlgorithmParameterException e) {
    // expected
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore)

Example 42 with InvalidAlgorithmParameterException

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

the class PKIXParametersTest method testKeyStoreConstructor.

public void testKeyStoreConstructor() throws Exception {
    TestKeyStore server = TestKeyStore.getServer();
    KeyStore.PrivateKeyEntry pke = server.getPrivateKey("RSA", "RSA");
    char[] password = "password".toCharArray();
    // contains CA and server certificates
    assertEquals(2, new PKIXParameters(server.keyStore).getTrustAnchors().size());
    // just copy server certificates
    KeyStore ks = TestKeyStore.createKeyStore();
    ks.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
    ks.setCertificateEntry("cert", pke.getCertificateChain()[0]);
    assertEquals(1, new PKIXParameters(ks).getTrustAnchors().size());
    // should fail with just key, even though cert is present in key entry
    try {
        KeyStore keyOnly = TestKeyStore.createKeyStore();
        keyOnly.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
        new PKIXParameters(keyOnly);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // should fail with empty KeyStore
    try {
        new PKIXParameters(TestKeyStore.createKeyStore());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PKIXParameters(java.security.cert.PKIXParameters) TestKeyStore(libcore.java.security.TestKeyStore) KeyStore(java.security.KeyStore)

Example 43 with InvalidAlgorithmParameterException

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

the class KeyManagerFactoryTest method test_KeyManagerFactory.

private void test_KeyManagerFactory(KeyManagerFactory kmf) throws Exception {
    assertNotNull(kmf);
    assertNotNull(kmf.getAlgorithm());
    assertNotNull(kmf.getProvider());
    // before init
    try {
        kmf.getKeyManagers();
        fail();
    } catch (IllegalStateException expected) {
    }
    // init with null ManagerFactoryParameters
    try {
        kmf.init(null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with useless ManagerFactoryParameters
    try {
        kmf.init(new UselessManagerFactoryParameters());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with KeyStoreBuilderParameters ManagerFactoryParameters
    PasswordProtection pp = new PasswordProtection(getTestKeyStore().storePassword);
    Builder builder = Builder.newInstance(getTestKeyStore().keyStore, pp);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder);
    if (supportsManagerFactoryParameters(kmf.getAlgorithm())) {
        kmf.init(ksbp);
        test_KeyManagerFactory_getKeyManagers(kmf, false);
    } else {
        try {
            kmf.init(ksbp);
            fail();
        } catch (InvalidAlgorithmParameterException expected) {
        }
    }
    // init with null for default behavior
    kmf.init(null, null);
    test_KeyManagerFactory_getKeyManagers(kmf, true);
    // init with specific key store and password
    kmf.init(getTestKeyStore().keyStore, getTestKeyStore().storePassword);
    test_KeyManagerFactory_getKeyManagers(kmf, false);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 44 with InvalidAlgorithmParameterException

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

the class ECDHKeyAgreementTest method testInit_withUnsupportedAlgorithmParameterSpec.

void testInit_withUnsupportedAlgorithmParameterSpec(Provider provider) throws Exception {
    try {
        getKeyAgreement(provider).init(KAT_PRIVATE_KEY1, new ECGenParameterSpec("prime256v1"));
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ECGenParameterSpec(java.security.spec.ECGenParameterSpec)

Example 45 with InvalidAlgorithmParameterException

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

the class CipherTest method test_initWithKeyAlgorithmParameters.

public void test_initWithKeyAlgorithmParameters() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    assertNotNull(c.getParameters());
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_3DES, ap);
        fail();
    } catch (InvalidKeyException expected) {
    }
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, (AlgorithmParameters) null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)394 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)216 InvalidKeyException (java.security.InvalidKeyException)206 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)130 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)114 BadPaddingException (javax.crypto.BadPaddingException)112 Cipher (javax.crypto.Cipher)101 IvParameterSpec (javax.crypto.spec.IvParameterSpec)100 IOException (java.io.IOException)74 SecretKeySpec (javax.crypto.spec.SecretKeySpec)58 NoSuchProviderException (java.security.NoSuchProviderException)56 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)49 CertificateException (java.security.cert.CertificateException)45 KeyStoreException (java.security.KeyStoreException)43 SecureRandom (java.security.SecureRandom)37 SecretKey (javax.crypto.SecretKey)34 BigInteger (java.math.BigInteger)31 KeyPairGenerator (java.security.KeyPairGenerator)27 UnrecoverableKeyException (java.security.UnrecoverableKeyException)27 X509Certificate (java.security.cert.X509Certificate)27