Search in sources :

Example 11 with SSLServerSocket

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

the class HttpsURLConnectionTest method testConsequentProxyConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     * 2 HTTPS connections are opened for one URL. For the first time
     * the connection is opened through one proxy,
     * for the second time through another.
     */
public void testConsequentProxyConnection() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55555/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
    // create another SSLServerSocket which will be used by server side
    ss = new ServerSocket(0);
    connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 12 with SSLServerSocket

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

the class HttpsURLConnectionTest method testProxyAuthConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     * Proxy server needs authentication.
     */
public void testProxyAuthConnection() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    Authenticator.setDefault(new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55555/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
    // should silently exit
    connection.connect();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 13 with SSLServerSocket

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

the class SSLSocketTest method test_SSLSocket_reusedNpnSocket.

public void test_SSLSocket_reusedNpnSocket() throws Exception {
    if (StandardNames.IS_RI) {
        // RI does not support NPN/ALPN
        return;
    }
    byte[] npnProtocols = new byte[] { 8, 'h', 't', 't', 'p', '/', '1', '.', '1' };
    final TestSSLContext c = TestSSLContext.create();
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket();
    // Reflection is used so this can compile on the RI
    String expectedClassName = "com.android.org.conscrypt.OpenSSLSocketImpl";
    Class<?> actualClass = client.getClass();
    assertEquals(expectedClassName, actualClass.getName());
    Method setNpnProtocols = actualClass.getMethod("setNpnProtocols", byte[].class);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    // First connection with NPN set on client and server
    {
        setNpnProtocols.invoke(client, npnProtocols);
        client.connect(new InetSocketAddress(c.host, c.port));
        final SSLSocket server = (SSLSocket) c.serverSocket.accept();
        assertEquals(expectedClassName, server.getClass().getName());
        setNpnProtocols.invoke(server, npnProtocols);
        Future<Void> future = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                server.startHandshake();
                return null;
            }
        });
        client.startHandshake();
        future.get();
        client.close();
        server.close();
    }
    // Second connection with client NPN already set on the SSL context, but
    // without server NPN set.
    {
        SSLServerSocket serverSocket = (SSLServerSocket) c.serverContext.getServerSocketFactory().createServerSocket(0);
        InetAddress host = InetAddress.getLocalHost();
        int port = serverSocket.getLocalPort();
        client = (SSLSocket) c.clientContext.getSocketFactory().createSocket();
        client.connect(new InetSocketAddress(host, port));
        final SSLSocket server = (SSLSocket) serverSocket.accept();
        Future<Void> future = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                server.startHandshake();
                return null;
            }
        });
        client.startHandshake();
        future.get();
        client.close();
        server.close();
        serverSocket.close();
    }
    c.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Method(java.lang.reflect.Method) SSLServerSocket(javax.net.ssl.SSLServerSocket) InetAddress(java.net.InetAddress) Callable(java.util.concurrent.Callable)

Example 14 with SSLServerSocket

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

the class SSLServerSocketTest method test_getSupportedProtocols.

/**
     * @throws IOException
     * javax.net.ssl.SSLServerSocket#getSupportedProtocols()
     */
public void test_getSupportedProtocols() throws Exception {
    SSLServerSocket sss = getSSLServerSocket();
    String[] res = sss.getSupportedCipherSuites();
    assertNotNull("NULL result", res);
    assertTrue("no supported protocols available.", res.length > 0);
}
Also used : SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 15 with SSLServerSocket

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

the class SSLServerSocketTest method test_WantClientAuth.

/**
     * @throws IOException
     * javax.net.ssl.SSLServerSocket#setWantClientAuth(boolean want)
     * javax.net.ssl.SSLServerSocket#getWantClientAuthCreation()
     */
public void test_WantClientAuth() throws Exception {
    SSLServerSocket sss = getSSLServerSocket();
    sss.setWantClientAuth(true);
    assertTrue(sss.getWantClientAuth());
    sss.setWantClientAuth(false);
    assertFalse(sss.getWantClientAuth());
}
Also used : SSLServerSocket(javax.net.ssl.SSLServerSocket)

Aggregations

SSLServerSocket (javax.net.ssl.SSLServerSocket)61 SSLContext (javax.net.ssl.SSLContext)23 SSLSocket (javax.net.ssl.SSLSocket)19 InetSocketAddress (java.net.InetSocketAddress)14 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)13 IOException (java.io.IOException)12 ServerSocket (java.net.ServerSocket)10 URL (java.net.URL)10 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)10 SSLEngine (javax.net.ssl.SSLEngine)9 UnknownHostException (java.net.UnknownHostException)7 Proxy (java.net.Proxy)6 Test (org.junit.Test)6 InetAddress (java.net.InetAddress)5 Method (java.lang.reflect.Method)3 KeyManagementException (java.security.KeyManagementException)3 KeyStore (java.security.KeyStore)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ServerSocketFactory (javax.net.ServerSocketFactory)3 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)3