Search in sources :

Example 6 with UnrecoverableKeyException

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

the class KeyStoreTest method test_KeyStore_getEntry.

public void test_KeyStore_getEntry() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.getEntry(null, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        // test odd inputs
        try {
            keyStore.getEntry(null, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        try {
            keyStore.getEntry(null, PARAM_KEY);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        assertNull(keyStore.getEntry("", null));
        assertNull(keyStore.getEntry("", PARAM_KEY));
        // test case sensitive
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
        } else {
            if (isKeyPasswordSupported(keyStore)) {
                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
            } else if (isNullPasswordAllowed(keyStore)) {
                assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
            }
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
            } else {
                assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
            }
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
            } else {
                assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
            }
        }
        // test case insensitive
        if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
            assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
        } else {
            assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
            }
        }
        if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
        } else {
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
            }
        }
        // test with null passwords
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
        } else if (isNullPasswordAllowed(keyStore)) {
            assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
        } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
            assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
        } else if (isKeyPasswordIgnored(keyStore)) {
            try {
                keyStore.getEntry(ALIAS_PRIVATE, null);
                fail(keyStore.getType());
            } catch (Exception e) {
                if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class) {
                    throw e;
                }
            }
        }
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_SECRET, null));
        } else if (isSecretKeyEnabled(keyStore)) {
            try {
                keyStore.getEntry(ALIAS_SECRET, null);
                fail(keyStore.getType());
            } catch (Exception e) {
                if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class) {
                    throw e;
                }
            }
        }
        // test with bad passwords
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
        } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
            assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
        } else if (isKeyPasswordSupported(keyStore)) {
            try {
                keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
                fail(keyStore.getType());
            } catch (UnrecoverableKeyException expected) {
            }
        }
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD));
        } else if (isSecretKeyEnabled(keyStore)) {
            try {
                keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
                fail(keyStore.getType());
            } catch (UnrecoverableKeyException expected) {
            }
        }
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 7 with UnrecoverableKeyException

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

the class SSLParametersImpl method createDefaultKeyManager.

private static X509KeyManager createDefaultKeyManager() throws KeyManagementException {
    try {
        String algorithm = KeyManagerFactory.getDefaultAlgorithm();
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(null, null);
        KeyManager[] kms = kmf.getKeyManagers();
        return findX509KeyManager(kms);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException(e);
    } catch (KeyStoreException e) {
        throw new KeyManagementException(e);
    } catch (UnrecoverableKeyException e) {
        throw new KeyManagementException(e);
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 8 with UnrecoverableKeyException

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

the class KeyStore4Test method testGetKey.

public void testGetKey() {
    try {
        Key key = keyStore.getKey("keyalias", null);
        assertNotNull(key);
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    } catch (UnrecoverableKeyException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.getKey("certalias", null);
        fail("expected NoSuchAlgorithmException");
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchAlgorithmException e) {
    // ok
    } catch (UnrecoverableKeyException e) {
        fail("unexpected exception: " + e);
    }
    try {
        uninitialized.getKey("keyalias", null);
        fail("expected KeyStoreException");
    } catch (KeyStoreException e) {
    // ok
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    } catch (UnrecoverableKeyException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.getKey("unknownalias", null);
        fail("expected NoSuchAlgorithmException");
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchAlgorithmException e) {
    // ok
    } catch (UnrecoverableKeyException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.getKey("unknownalias", "PASSWORD".toCharArray());
        fail("expected UnrecoverableKeyException");
    } catch (UnrecoverableKeyException e) {
    // ok
    } catch (KeyStoreException e) {
        fail("unexpected exception: " + e);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Key(java.security.Key)

Example 9 with UnrecoverableKeyException

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

the class KeyManagerFactorySpiTest method test_engineInit_01.

/**
     * javax.net.ssl.KeyManagerFactorySpi#KengineInit(KeyStore ks, char[] password)
     */
public void test_engineInit_01() {
    KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
    KeyStore ks;
    char[] psw = "password".toCharArray();
    try {
        kmf.engineInit(null, null);
        fail("NoSuchAlgorithmException wasn't thrown");
    } catch (NoSuchAlgorithmException kse) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of NoSuchAlgorithmException");
    }
    try {
        kmf.engineInit(null, psw);
        fail("KeyStoreException wasn't thrown");
    } catch (KeyStoreException uke) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of KeyStoreException");
    }
    try {
        ks = KeyStore.getInstance(KeyStore.getDefaultType());
        kmf.engineInit(ks, null);
        fail("UnrecoverableKeyException wasn't thrown");
    } catch (UnrecoverableKeyException uke) {
    //expected
    } catch (Exception e) {
        fail(e + " was thrown instead of UnrecoverableKeyException");
    }
    try {
        KeyStore kst = KeyStore.getInstance(KeyStore.getDefaultType());
        kst.load(null, null);
        kmf.engineInit(kst, psw);
    } catch (Exception e) {
        fail("Unexpected exception " + e);
    }
}
Also used : KeyManagerFactorySpiImpl(org.apache.harmony.xnet.tests.support.KeyManagerFactorySpiImpl) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 10 with UnrecoverableKeyException

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

the class MySslContext method test_getServerSocketFactory.

/**
     * Test for <code>getServerSocketFactory()</code>
     * <code>getSocketFactory()</code>
     * <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
     * methods Assertion: returns correspondent object
     *
     */
public void test_getServerSocketFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    SSLContext[] sslC = createSSLCon();
    assertNotNull("SSLContext objects were not created", sslC);
    String tAlg = TrustManagerFactory.getDefaultAlgorithm();
    String kAlg = KeyManagerFactory.getDefaultAlgorithm();
    if (tAlg == null) {
        fail("TrustManagerFactory default algorithm is not defined");
        return;
    }
    if (kAlg == null) {
        fail("KeyManagerFactory default algorithm is not defined");
        return;
    }
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        ks.load(null, null);
    } catch (Exception e) {
        fail(e + " was thrown for method load(null, null)");
    }
    kmf.init(ks, new char[10]);
    KeyManager[] kms = kmf.getKeyManagers();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
    tmf.init(ks);
    TrustManager[] tms = tmf.getTrustManagers();
    for (int i = 0; i < sslC.length; i++) {
        sslC[i].init(kms, tms, new SecureRandom());
        assertNotNull("No SSLServerSocketFactory available", sslC[i].getServerSocketFactory());
        assertNotNull("No SSLSocketFactory available", sslC[i].getSocketFactory());
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager)

Aggregations

UnrecoverableKeyException (java.security.UnrecoverableKeyException)99 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)77 KeyStoreException (java.security.KeyStoreException)76 IOException (java.io.IOException)60 CertificateException (java.security.cert.CertificateException)49 InvalidKeyException (java.security.InvalidKeyException)28 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)27 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)26 BadPaddingException (javax.crypto.BadPaddingException)26 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)26 KeyStore (java.security.KeyStore)24 KeyManagementException (java.security.KeyManagementException)19 RemoteException (android.os.RemoteException)15 SecretKey (javax.crypto.SecretKey)15 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)15 SSLContext (javax.net.ssl.SSLContext)14 FileNotFoundException (java.io.FileNotFoundException)13 Key (java.security.Key)12 NoSuchProviderException (java.security.NoSuchProviderException)11 PrivateKey (java.security.PrivateKey)11