Search in sources :

Example 1 with TestKeyStore

use of libcore.java.security.TestKeyStore in project robovm by robovm.

the class X509KeyManagerTest method test_ChooseClientAlias_KeyType.

private void test_ChooseClientAlias_KeyType(String clientKeyType, String caKeyType, String selectedKeyType, boolean succeeds) throws Exception {
    TestKeyStore ca = new TestKeyStore.Builder().keyAlgorithms(caKeyType).build();
    TestKeyStore client = new TestKeyStore.Builder().keyAlgorithms(clientKeyType).signer(ca.getPrivateKey(caKeyType, caKeyType)).build();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(client.keyStore, client.keyPassword);
    String[] keyTypes = new String[] { selectedKeyType };
    KeyManager[] managers = kmf.getKeyManagers();
    for (KeyManager manager : managers) {
        if (manager instanceof X509KeyManager) {
            String alias = ((X509KeyManager) manager).chooseClientAlias(keyTypes, null, null);
            if (succeeds) {
                assertNotNull(alias);
            } else {
                assertNull(alias);
            }
        }
    }
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) X509KeyManager(javax.net.ssl.X509KeyManager) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 2 with TestKeyStore

use of libcore.java.security.TestKeyStore 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 3 with TestKeyStore

use of libcore.java.security.TestKeyStore in project robovm by robovm.

the class KeyStoreBuilderParametersTest method test_init_List.

public void test_init_List() {
    TestKeyStore testKeyStore1 = TestKeyStore.getClient();
    TestKeyStore testKeyStore2 = TestKeyStore.getServer();
    Builder builder1 = Builder.newInstance(testKeyStore1.keyStore, new PasswordProtection(testKeyStore1.storePassword));
    Builder builder2 = Builder.newInstance(testKeyStore2.keyStore, new PasswordProtection(testKeyStore2.storePassword));
    List list = Arrays.asList(builder1, builder2);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(list);
    assertNotNull(ksbp);
    assertNotNull(ksbp.getParameters());
    assertNotSame(list, ksbp.getParameters());
    assertEquals(2, ksbp.getParameters().size());
    assertSame(builder1, ksbp.getParameters().get(0));
    assertSame(builder2, ksbp.getParameters().get(1));
    // confirm result is not modifiable
    try {
        ksbp.getParameters().set(0, builder2);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    // confirm result is a copy of original
    list.set(0, builder2);
    assertSame(builder1, ksbp.getParameters().get(0));
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) List(java.util.List) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 4 with TestKeyStore

use of libcore.java.security.TestKeyStore in project robovm by robovm.

the class SSLSocketTest method test_SSLSocket_getSupportedCipherSuites_connect.

public void test_SSLSocket_getSupportedCipherSuites_connect() throws Exception {
    // note the rare usage of non-RSA keys
    TestKeyStore testKeyStore = new TestKeyStore.Builder().keyAlgorithms("RSA", "DSA", "EC", "EC_RSA").aliasPrefix("rsa-dsa-ec").ca(true).build();
    StringBuilder error = new StringBuilder();
    if (StandardNames.IS_RI) {
        test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, StandardNames.JSSE_PROVIDER_NAME, StandardNames.JSSE_PROVIDER_NAME, true, true, error);
    } else {
        test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "HarmonyJSSE", "HarmonyJSSE", false, false, error);
        test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "AndroidOpenSSL", "AndroidOpenSSL", true, true, error);
        test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "HarmonyJSSE", "AndroidOpenSSL", false, true, error);
        test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "AndroidOpenSSL", "HarmonyJSSE", true, false, error);
    }
    if (error.length() > 0) {
        throw new Exception("One or more problems in " + "test_SSLSocket_getSupportedCipherSuites_connect:\n" + error);
    }
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 5 with TestKeyStore

use of libcore.java.security.TestKeyStore in project robovm by robovm.

the class TrustManagerFactoryTest method test_TrustManagerFactory_extendedKeyUsage.

private void test_TrustManagerFactory_extendedKeyUsage(KeyPurposeId keyPurposeId, boolean critical, boolean client, boolean server) throws Exception {
    String algorithm = "RSA";
    TestKeyStore intermediateCa = TestKeyStore.getIntermediateCa();
    TestKeyStore leaf = new TestKeyStore.Builder().keyAlgorithms(new String[] { algorithm }).aliasPrefix("criticalCodeSigning").signer(intermediateCa.getPrivateKey("RSA", "RSA")).rootCa(intermediateCa.getRootCertificate("RSA")).addExtendedKeyUsage(keyPurposeId, critical).build();
    // leaf.dump("test_TrustManagerFactory_criticalCodeSigning");
    PrivateKeyEntry privateKeyEntry = leaf.getPrivateKey(algorithm, algorithm);
    X509Certificate[] chain = (X509Certificate[]) privateKeyEntry.getCertificateChain();
    TestKeyStore rootCa = TestKeyStore.getRootCa();
    X509TrustManager trustManager = (X509TrustManager) rootCa.trustManagers[0];
    try {
        trustManager.checkClientTrusted(chain, algorithm);
        assertTrue(client);
    } catch (Exception e) {
        assertFalse(client);
    }
    try {
        trustManager.checkServerTrusted(chain, algorithm);
        assertTrue(server);
    } catch (Exception e) {
        assertFalse(server);
    }
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) X509TrustManager(javax.net.ssl.X509TrustManager) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) X509Certificate(java.security.cert.X509Certificate) CertificateException(java.security.cert.CertificateException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Aggregations

TestKeyStore (libcore.java.security.TestKeyStore)7 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 Builder (java.security.KeyStore.Builder)2 PasswordProtection (java.security.KeyStore.PasswordProtection)2 CertificateException (java.security.cert.CertificateException)2 KeyStoreBuilderParameters (javax.net.ssl.KeyStoreBuilderParameters)2 IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 KeyStore (java.security.KeyStore)1 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)1 PKIXParameters (java.security.cert.PKIXParameters)1 X509Certificate (java.security.cert.X509Certificate)1 List (java.util.List)1 KeyManager (javax.net.ssl.KeyManager)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLException (javax.net.ssl.SSLException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 SSLProtocolException (javax.net.ssl.SSLProtocolException)1