Search in sources :

Example 76 with KeyStore

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

the class KeyStoreTest method test_KeyStore_size.

public void test_KeyStore_size() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.aliases();
            fail(keyStore.getType());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        if (isPersistentStorage(keyStore)) {
            assertTrue("Should successfully query size: " + keyStore.getType(), keyStore.size() >= 0);
        } else if (hasDefaultContents(keyStore)) {
            assertTrue("Should have non-empty store: " + keyStore.getType(), keyStore.size() > 0);
        } else {
            assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
        }
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        if (hasDefaultContents(keyStore)) {
            assertTrue("Should have non-empty store: " + keyStore.getType(), keyStore.size() > 0);
            continue;
        }
        int expected = 0;
        if (isKeyPasswordSupported(keyStore)) {
            expected++;
        }
        if (isNullPasswordAllowed(keyStore)) {
            expected++;
        }
        if (isSecretKeyEnabled(keyStore)) {
            expected++;
            if (isNullPasswordAllowed(keyStore)) {
                expected++;
            }
        }
        if (isCertificateEnabled(keyStore)) {
            expected++;
        }
        assertEquals(expected, keyStore.size());
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore)

Example 77 with KeyStore

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

the class KeyStoreTest method assertEqualsKeyStores.

public void assertEqualsKeyStores(KeyStore expected, ByteArrayOutputStream actual, char[] storePassword) throws Exception {
    KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider());
    ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword);
    assertEqualsKeyStores(expected, ks);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KeyStore(java.security.KeyStore)

Example 78 with KeyStore

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

the class KeyStoreTest method test_KeyStore_getCertificate.

public void test_KeyStore_getCertificate() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.getCertificate(null);
            fail(keyStore.getType());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        // test odd inputs
        try {
            keyStore.getCertificate(null);
            fail(keyStore.getType());
        } catch (Exception e) {
            if (e.getClass() != NullPointerException.class && e.getClass() != IllegalArgumentException.class) {
                throw e;
            }
        }
        assertNull(keyStore.getCertificate(""));
        // test case sensitive
        if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) {
            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
        } else {
            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
        }
        // test case insensitive
        if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
            assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
        } else {
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
            }
        }
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 79 with KeyStore

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

the class KeyStoreTest method test_KeyStore_isKeyEntry.

public void test_KeyStore_isKeyEntry() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.isKeyEntry(null);
            fail(keyStore.getType());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        try {
            keyStore.isKeyEntry(null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        assertFalse(keyStore.isKeyEntry(""));
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        assertFalse(keyStore.isKeyEntry(""));
        if (isReadOnly(keyStore)) {
            assertFalse(keyStore.isKeyEntry(ALIAS_PRIVATE));
            continue;
        }
        if (isKeyPasswordSupported(keyStore)) {
            assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE));
        }
        if (isNullPasswordAllowed(keyStore)) {
            assertTrue(keyStore.isKeyEntry(ALIAS_NO_PASSWORD_PRIVATE));
        }
        assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET));
        assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE));
        assertEquals(!isCaseSensitive(keyStore), keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE));
        assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET));
        assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE));
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore)

Example 80 with KeyStore

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

the class KeyStoreTest method test_KeyStore_getType.

public void test_KeyStore_getType() throws Exception {
    String type = KeyStore.getDefaultType();
    KeyStore ks = KeyStore.getInstance(type);
    assertNotNull(ks.getType());
    assertNotNull(type, ks.getType());
    for (KeyStore keyStore : keyStores()) {
        assertNotNull(keyStore.getType());
    }
}
Also used : KeyStore(java.security.KeyStore)

Aggregations

KeyStore (java.security.KeyStore)738 IOException (java.io.IOException)190 X509Certificate (java.security.cert.X509Certificate)189 FileInputStream (java.io.FileInputStream)163 KeyStoreException (java.security.KeyStoreException)151 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)138 InputStream (java.io.InputStream)125 Certificate (java.security.cert.Certificate)124 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)119 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)114 SSLContext (javax.net.ssl.SSLContext)112 PrivateKey (java.security.PrivateKey)94 CertificateException (java.security.cert.CertificateException)94 File (java.io.File)82 ByteArrayInputStream (java.io.ByteArrayInputStream)75 CertificateFactory (java.security.cert.CertificateFactory)75 Key (java.security.Key)61 UnrecoverableKeyException (java.security.UnrecoverableKeyException)55 TrustManager (javax.net.ssl.TrustManager)47 KeyManagementException (java.security.KeyManagementException)40