Search in sources :

Example 91 with KeyStore

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

the class PKIXBuilderParametersTest method testGetMaxPathLength.

/**
     * Test for <code>getMaxPathLength()</code>
     */
public final void testGetMaxPathLength() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
    ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    keyTest.setCertificateEntry("alias1", cert[0]);
    keyTest.setCertificateEntry("alias2", cert[0]);
    keyTest.setCertificateEntry("alias3", cert[1]);
    PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
    assertEquals(5, p.getMaxPathLength());
    p.setMaxPathLength(10);
    assertEquals(10, p.getMaxPathLength());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 92 with KeyStore

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

the class KeyStoreTest method test_KeyStore_cacerts.

public void test_KeyStore_cacerts() throws Exception {
    if (StandardNames.IS_RI) {
        return;
    }
    KeyStore ks = KeyStore.getInstance("AndroidCAStore");
    assertEquals("AndroidCAStore", ks.getType());
    assertEquals("HarmonyJSSE", ks.getProvider().getName());
    ks.load(null, null);
    for (String alias : Collections.list(ks.aliases())) {
        Certificate c = null;
        try {
            c = ks.getCertificate(alias);
            assertNotNull(c);
            assertTrue(ks.isCertificateEntry(alias));
            assertTrue(ks.entryInstanceOf(alias, TrustedCertificateEntry.class));
            assertEquals(alias, ks.getCertificateAlias(c));
            assertTrue(c instanceof X509Certificate);
            X509Certificate cert = (X509Certificate) c;
            assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID());
            assertNotNull(cert.getPublicKey());
            assertTrue(ks.containsAlias(alias));
            assertNotNull(ks.getCreationDate(alias));
            assertNotNull(ks.getEntry(alias, null));
            assertFalse(ks.isKeyEntry(alias));
            assertNull(ks.getKey(alias, null));
            assertNull(ks.getCertificateChain(alias));
        } catch (Throwable t) {
            throw new Exception("alias=" + alias + " cert=" + c, t);
        }
    }
}
Also used : KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry)

Example 93 with KeyStore

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

the class KeyStoreTest method test_KeyStore_create.

public void test_KeyStore_create() throws Exception {
    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());
        }
    }
}
Also used : KeyStore(java.security.KeyStore) Provider(java.security.Provider)

Example 94 with KeyStore

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

the class KeyStoreTest method test_KeyStore_setKeyEntry_array.

public void test_KeyStore_setKeyEntry_array() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        try {
            keyStore.setKeyEntry(null, null, null);
            fail(keyStore.getType());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        if (isReadOnly(keyStore)) {
            try {
                keyStore.setKeyEntry(null, null, null);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            continue;
        }
        // test odd inputs
        try {
            keyStore.setKeyEntry(null, null, null);
            fail(keyStore.getType());
        } catch (Exception e) {
            if (e.getClass() != NullPointerException.class && e.getClass() != IllegalArgumentException.class && e.getClass() != KeyStoreException.class && e.getClass() != RuntimeException.class) {
                throw e;
            }
        }
    }
    for (KeyStore keyStore : keyStores()) {
        if (!isNullPasswordAllowed(keyStore)) {
            // password is required.
            continue;
        }
        if (isSetKeyByteArrayUnimplemented(keyStore)) {
            continue;
        }
        clearKeyStore(keyStore);
        // test case sensitive
        if (isKeyPasswordSupported(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
        }
        if (isNullPasswordAllowed(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
        }
        if (isReadOnly(keyStore)) {
            try {
                setPrivateKeyBytes(keyStore);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            continue;
        }
        if (isKeyPasswordSupported(keyStore)) {
            setPrivateKeyBytes(keyStore);
            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
        }
        if (isNullPasswordAllowed(keyStore)) {
            setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey());
            assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
        }
        if (isSecretKeyEnabled(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            setSecretKeyBytes(keyStore);
            assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
        } else {
            try {
                keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null);
                fail(keyStore.getType());
            } catch (KeyStoreException expected) {
            }
        }
    }
    for (KeyStore keyStore : keyStores()) {
        if (!isNullPasswordAllowed(keyStore)) {
            // password is required.
            continue;
        }
        if (isSetKeyByteArrayUnimplemented(keyStore)) {
            continue;
        }
        populate(keyStore);
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
        } else if (isCaseSensitive(keyStore)) {
            if (isKeyPasswordSupported(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            }
            if (isNullPasswordAllowed(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
                setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2());
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
            }
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
                setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
            }
        } else {
            if (isKeyPasswordSupported(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            }
            if (isNullPasswordAllowed(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
                setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2());
                assertPrivateKey2(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
            }
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
                setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
                assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
            }
        }
    }
}
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 95 with KeyStore

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

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