Search in sources :

Example 61 with TrustManager

use of javax.net.ssl.TrustManager 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)

Example 62 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class SSLContextSpiTest method test_commonTest_02.

/**
     * SSLContextSpi#engineCreateSSLEngine()
     * SSLContextSpi#engineCreateSSLEngine(String host, int port)
     * SSLContextSpi#engineGetClientSessionContext()
     * SSLContextSpi#engineGetServerSessionContext()
     * SSLContextSpi#engineGetServerSocketFactory()
     * SSLContextSpi#engineGetSocketFactory()
     */
public void test_commonTest_02() {
    SSLContextSpiImpl ssl = new SSLContextSpiImpl();
    String defaultAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    try {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
        char[] pass = "password".toCharArray();
        kmf.init(null, pass);
        KeyManager[] km = kmf.getKeyManagers();
        defaultAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
        TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        trustMF.init(ks);
        TrustManager[] tm = trustMF.getTrustManagers();
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        ssl.engineInit(km, tm, sr);
    } catch (Exception ex) {
        fail(ex + " unexpected exception");
    }
    try {
        assertNotNull("Subtest_01: Object is NULL", ssl.engineCreateSSLEngine());
        SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", 1080);
        assertNotNull("Subtest_02: Object is NULL", sleng);
        assertEquals(sleng.getPeerPort(), 1080);
        assertEquals(sleng.getPeerHost(), "localhost");
        assertNull("Subtest_03: Object not NULL", ssl.engineGetClientSessionContext());
        assertNull("Subtest_04: Object not NULL", ssl.engineGetServerSessionContext());
        assertNull("Subtest_05: Object not NULL", ssl.engineGetServerSocketFactory());
        assertNull("Subtest_06: Object not NULL", ssl.engineGetSocketFactory());
    } catch (Exception e) {
        fail("Unexpected exception " + e);
    }
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) SecureRandom(java.security.SecureRandom) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager) SSLContextSpiImpl(org.apache.harmony.xnet.tests.support.SSLContextSpiImpl)

Example 63 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class SSLParametersImpl method createDefaultTrustManager.

private static X509TrustManager createDefaultTrustManager() throws KeyManagementException {
    try {
        String algorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
        tmf.init((KeyStore) null);
        TrustManager[] tms = tmf.getTrustManagers();
        X509TrustManager trustManager = findX509TrustManager(tms);
        return trustManager;
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException(e);
    } catch (KeyStoreException e) {
        throw new KeyManagementException(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 64 with TrustManager

use of javax.net.ssl.TrustManager in project muzei by romannurik.

the class OkHttpClientFactory method enableTls12.

/**
     * Enable TLS on the OKHttp builder by setting a custom SocketFactory
     */
private static OkHttpClient.Builder enableTls12(OkHttpClient.Builder client) {
    Log.i(TAG, "Enabling HTTPS compatibility mode");
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
        client.sslSocketFactory(new TLSSocketFactory(), trustManager);
        ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1).build();
        List<ConnectionSpec> specs = new ArrayList<>();
        specs.add(cs);
        specs.add(ConnectionSpec.COMPATIBLE_TLS);
        specs.add(ConnectionSpec.CLEARTEXT);
        client.connectionSpecs(specs);
    } catch (Exception exc) {
        Log.e(TAG, "Error while setting TLS", exc);
    }
    return client;
}
Also used : ConnectionSpec(okhttp3.ConnectionSpec) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ArrayList(java.util.ArrayList) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 65 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class HttpsURLConnectionTest method getContext.

/**
     * Builds and returns the context used for secure socket creation.
     */
private static SSLContext getContext() throws Exception {
    String type = KeyStore.getDefaultType();
    String keyStore = getKeyStoreFileName();
    File keyStoreFile = new File(keyStore);
    FileInputStream fis = new FileInputStream(keyStoreFile);
    KeyStore ks = KeyStore.getInstance(type);
    ks.load(fis, KS_PASSWORD.toCharArray());
    fis.close();
    if (DO_LOG && false) {
        TestKeyStore.dump("HttpsURLConnection.getContext", ks, KS_PASSWORD.toCharArray());
    }
    String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
    kmf.init(ks, KS_PASSWORD.toCharArray());
    KeyManager[] keyManagers = kmf.getKeyManagers();
    String tmfAlgorthm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorthm);
    tmf.init(ks);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    if (DO_LOG) {
        trustManagers = TestTrustManager.wrap(trustManagers);
    }
    SSLContext ctx = SSLContext.getInstance("TLSv1");
    ctx.init(keyManagers, trustManagers, null);
    return ctx;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) File(java.io.File) TestKeyStore(libcore.java.security.TestKeyStore) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TestTrustManager(libcore.javax.net.ssl.TestTrustManager)

Aggregations

TrustManager (javax.net.ssl.TrustManager)229 SSLContext (javax.net.ssl.SSLContext)139 X509TrustManager (javax.net.ssl.X509TrustManager)139 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)90 X509Certificate (java.security.cert.X509Certificate)70 IOException (java.io.IOException)60 KeyStore (java.security.KeyStore)60 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)60 SecureRandom (java.security.SecureRandom)58 KeyManagementException (java.security.KeyManagementException)54 KeyManager (javax.net.ssl.KeyManager)52 CertificateException (java.security.cert.CertificateException)43 KeyStoreException (java.security.KeyStoreException)37 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)34 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 URL (java.net.URL)22 GeneralSecurityException (java.security.GeneralSecurityException)22 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)20 InputStream (java.io.InputStream)18 FileInputStream (java.io.FileInputStream)16