Search in sources :

Example 21 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)

Example 22 with SSLServerSocket

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

the class SSLServerSocketTest method getSSLServerSocket.

private SSLServerSocket getSSLServerSocket() throws Exception {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(getKeyManagers(), null, null);
    SSLServerSocket sss = (SSLServerSocket) context.getServerSocketFactory().createServerSocket();
    return sss;
}
Also used : SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 23 with SSLServerSocket

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

the class SSLServerSocketTest method test_EnabledProtocols.

/**
     * @throws IOException
     * javax.net.ssl.SSLServerSocket#getEnabledProtocols()
     * javax.net.ssl.SSLServerSocket#setEnabledProtocols(String[] protocols)
     */
public void test_EnabledProtocols() throws Exception {
    SSLServerSocket sss = getSSLServerSocket();
    try {
        sss.setEnabledProtocols(null);
    } catch (IllegalArgumentException iae) {
    //expected
    }
    String[] unsupportedProtocols = { "unsupported" };
    try {
        sss.setEnabledProtocols(unsupportedProtocols);
    } catch (IllegalArgumentException iae) {
    //expected
    }
    int count = sss.getSupportedProtocols().length;
    assertTrue("No supported protocols", count > 0);
    sss.setEnabledProtocols(sss.getSupportedProtocols());
    String[] res = sss.getEnabledProtocols();
    assertNotNull("NULL result", res);
    assertTrue("no enabled protocols.", res.length == count);
}
Also used : SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 24 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 25 with SSLServerSocket

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

Aggregations

SSLServerSocket (javax.net.ssl.SSLServerSocket)67 SSLContext (javax.net.ssl.SSLContext)24 SSLSocket (javax.net.ssl.SSLSocket)19 InetSocketAddress (java.net.InetSocketAddress)15 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)14 IOException (java.io.IOException)13 ServerSocket (java.net.ServerSocket)12 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 ArrayList (java.util.ArrayList)3 ServerSocketFactory (javax.net.ServerSocketFactory)3