Search in sources :

Example 96 with KeyStore

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

the class KeyStoreTest method test_KeyStore_deleteEntry.

public void test_KeyStore_deleteEntry() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.deleteEntry(null);
            fail(keyStore.getType());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        if (isReadOnly(keyStore)) {
            try {
                keyStore.deleteEntry(null);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            continue;
        }
        // test odd inputs
        try {
            keyStore.deleteEntry(null);
            fail(keyStore.getType());
        } catch (Exception e) {
            if (e.getClass() != NullPointerException.class && e.getClass() != KeyStoreException.class) {
                throw e;
            }
        }
        keyStore.deleteEntry("");
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        if (isReadOnly(keyStore)) {
            try {
                keyStore.deleteEntry(ALIAS_PRIVATE);
            } catch (UnsupportedOperationException e) {
            }
            continue;
        }
        // test case sensitive
        if (isKeyPasswordSupported(keyStore)) {
            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
            keyStore.deleteEntry(ALIAS_PRIVATE);
            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
        }
        if (isNullPasswordAllowed(keyStore)) {
            assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
            keyStore.deleteEntry(ALIAS_NO_PASSWORD_PRIVATE);
            assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
        }
        if (isSecretKeyEnabled(keyStore)) {
            assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            keyStore.deleteEntry(ALIAS_SECRET);
            assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
        } else {
            keyStore.deleteEntry(ALIAS_SECRET);
        }
        if (isCertificateEnabled(keyStore)) {
            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
            keyStore.deleteEntry(ALIAS_CERTIFICATE);
            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
        } else {
            keyStore.deleteEntry(ALIAS_CERTIFICATE);
        }
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        if (isCaseSensitive(keyStore)) {
            if (isKeyPasswordSupported(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            }
            if (isNullPasswordAllowed(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                keyStore.deleteEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE);
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
            }
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            } else {
                keyStore.deleteEntry(ALIAS_SECRET);
            }
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
                keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
            } else {
                keyStore.deleteEntry(ALIAS_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 97 with KeyStore

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

the class KeyStoreTest method test_KeyStore_isCertificateEntry.

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

Example 98 with KeyStore

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

the class KeyStoreTest method keyStores.

public static List<KeyStore> keyStores() throws Exception {
    List<KeyStore> keyStores = new ArrayList<KeyStore>();
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if (!type.equals("KeyStore")) {
                continue;
            }
            String algorithm = service.getAlgorithm();
            KeyStore ks = KeyStore.getInstance(algorithm, provider);
            assertEquals(provider, ks.getProvider());
            assertEquals(algorithm, ks.getType());
            if (!isUnsupported(ks)) {
                keyStores.add(ks);
            }
        }
    }
    return keyStores;
}
Also used : ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) Provider(java.security.Provider)

Example 99 with KeyStore

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

the class KeyStoreTest method test_KeyStore_getDefaultType.

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

Example 100 with KeyStore

use of java.security.KeyStore 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)

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