Search in sources :

Example 26 with SSLSocketFactory

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

the class URLConnectionTest method testHttpsWithCustomTrustManager.

public void testHttpsWithCustomTrustManager() throws Exception {
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    RecordingTrustManager trustManager = new RecordingTrustManager();
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
    HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    try {
        TestSSLContext testSSLContext = TestSSLContext.create();
        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
        server.enqueue(new MockResponse().setBody("ABC"));
        server.enqueue(new MockResponse().setBody("DEF"));
        server.enqueue(new MockResponse().setBody("GHI"));
        server.play();
        URL url = server.getUrl("/");
        assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls);
        assertEquals(Arrays.asList("checkServerTrusted [" + "CN=" + hostName + " 1, " + "CN=Test Intermediate Certificate Authority 1, " + "CN=Test Root Certificate Authority 1" + "] RSA"), trustManager.calls);
    } finally {
        HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
        HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) SSLContext(javax.net.ssl.SSLContext) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 27 with SSLSocketFactory

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

the class HttpsURLConnectionTest method testSetSSLSocketFactory.

/**
     * Tests possibility to set up the SSLSocketFactory
     * to be used by HttpsURLConnection.
     */
public void testSetSSLSocketFactory() throws Throwable {
    // create the SSLServerSocket which will be used by server side
    SSLContext ctx = getContext();
    SSLServerSocket ss = (SSLServerSocket) ctx.getServerSocketFactory().createServerSocket(0);
    // create the HostnameVerifier to check hostname verification
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    // create HttpsURLConnection to be tested
    URL url = new URL("https://localhost:" + ss.getLocalPort());
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    SSLSocketFactory socketFactory = (SSLSocketFactory) ctx.getSocketFactory();
    connection.setSSLSocketFactory(socketFactory);
    TestHostnameVerifier hnv_late = new TestHostnameVerifier();
    // late initialization: should not be used for created connection
    HttpsURLConnection.setDefaultHostnameVerifier(hnv_late);
    // perform the interaction between the peers
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    // check the connection state
    checkConnectionStateParameters(connection, peerSocket);
    // check the verification process
    assertTrue("Hostname verification was not done", hnv.verified);
    assertFalse("Hostname verification should not be done by this verifier", hnv_late.verified);
    // check the used SSLSocketFactory
    assertNotSame("Default SSLSocketFactory should not be used", HttpsURLConnection.getDefaultSSLSocketFactory(), connection.getSSLSocketFactory());
    assertSame("Result differs from expected", socketFactory, connection.getSSLSocketFactory());
    // should silently exit
    connection.connect();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 28 with SSLSocketFactory

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

the class SSLSocketFactoryTest method test_Constructor.

/**
     * javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
     */
public void test_Constructor() {
    try {
        SocketFactory sf = SSLSocketFactory.getDefault();
        assertTrue(sf instanceof SSLSocketFactory);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : SocketFactory(javax.net.SocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 29 with SSLSocketFactory

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

the class SSLSocketFactoryTest method test_getSupportedCipherSuites.

/**
     * javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
     */
public void test_getSupportedCipherSuites() {
    try {
        SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        assertTrue("no supported cipher suites returned", sf.getSupportedCipherSuites().length > 0);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 30 with SSLSocketFactory

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

the class SSLSocketTest method test_SSLSocket_getEnabledProtocols.

public void test_SSLSocket_getEnabledProtocols() throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket ssl = (SSLSocket) sf.createSocket();
    String[] protocols = ssl.getEnabledProtocols();
    StandardNames.assertValidProtocols(StandardNames.SSL_SOCKET_PROTOCOLS, protocols);
    assertNotSame(protocols, ssl.getEnabledProtocols());
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

SSLSocketFactory (javax.net.ssl.SSLSocketFactory)191 SSLSocket (javax.net.ssl.SSLSocket)69 SSLContext (javax.net.ssl.SSLContext)57 IOException (java.io.IOException)45 Socket (java.net.Socket)37 Test (org.junit.Test)33 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)29 HostnameVerifier (javax.net.ssl.HostnameVerifier)27 URL (java.net.URL)23 KeyManagementException (java.security.KeyManagementException)20 OutputStream (java.io.OutputStream)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 InputStream (java.io.InputStream)18 CertificateException (java.security.cert.CertificateException)17 HttpURLConnection (java.net.HttpURLConnection)15 InetSocketAddress (java.net.InetSocketAddress)15 X509TrustManager (javax.net.ssl.X509TrustManager)15 OkHttpClient (okhttp3.OkHttpClient)14 SSLParameters (javax.net.ssl.SSLParameters)13 TrustManager (javax.net.ssl.TrustManager)13