Search in sources :

Example 86 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_interrupt_read.

/**
     * b/7014266 Test to confirm that an SSLSocket.close() on one
     * thread will interupt another thread blocked reading on the same
     * socket.
     */
public void test_SSLSocket_interrupt_read() throws Exception {
    TestSSLContext c = TestSSLContext.create();
    final Socket underlying = new Socket(c.host, c.port);
    final SSLSocket wrapping = (SSLSocket) c.clientContext.getSocketFactory().createSocket(underlying, c.host.getHostName(), c.port, false);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> clientFuture = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                wrapping.startHandshake();
                assertFalse(StandardNames.IS_RI);
                wrapping.setSoTimeout(5 * 1000);
                assertEquals(-1, wrapping.getInputStream().read());
            } catch (Exception e) {
                assertTrue(StandardNames.IS_RI);
            }
            return null;
        }
    });
    executor.shutdown();
    SSLSocket server = (SSLSocket) c.serverSocket.accept();
    server.startHandshake();
    wrapping.close();
    clientFuture.get();
    server.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) 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 87 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_getSupportedCipherSuites_names.

public void test_SSLSocket_getSupportedCipherSuites_names() throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket ssl = (SSLSocket) sf.createSocket();
    String[] cipherSuites = ssl.getSupportedCipherSuites();
    StandardNames.assertSupportedCipherSuites(StandardNames.CIPHER_SUITES, cipherSuites);
    assertNotSame(cipherSuites, ssl.getSupportedCipherSuites());
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 88 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_untrustedServer.

public void test_SSLSocket_untrustedServer() throws Exception {
    TestSSLContext c = TestSSLContext.create(TestKeyStore.getClientCA2(), TestKeyStore.getServer());
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                server.startHandshake();
                assertFalse(StandardNames.IS_RI);
            } catch (SSLHandshakeException expected) {
                assertTrue(StandardNames.IS_RI);
            }
            return null;
        }
    });
    executor.shutdown();
    try {
        client.startHandshake();
        fail();
    } catch (SSLHandshakeException expected) {
        assertTrue(expected.getCause() instanceof CertificateException);
    }
    client.close();
    server.close();
    future.get();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) CertificateException(java.security.cert.CertificateException) 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) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 89 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_startHandshake_noKeyStore.

public void test_SSLSocket_startHandshake_noKeyStore() throws Exception {
    TestSSLContext c = TestSSLContext.create(null, null, null, null, null, null, null, null, SSLContext.getDefault(), SSLContext.getDefault());
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    // RI used to throw SSLException on accept, now throws on startHandshake
    if (StandardNames.IS_RI) {
        final SSLSocket server = (SSLSocket) c.serverSocket.accept();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Void> future = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                try {
                    server.startHandshake();
                    fail();
                } catch (SSLHandshakeException expected) {
                }
                return null;
            }
        });
        executor.shutdown();
        try {
            client.startHandshake();
            fail();
        } catch (SSLHandshakeException expected) {
        }
        future.get();
        server.close();
    } else {
        try {
            c.serverSocket.accept();
            fail();
        } catch (SSLException expected) {
        }
    }
    client.close();
    c.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) SSLException(javax.net.ssl.SSLException) 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) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 90 with SSLSocket

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

Aggregations

SSLSocket (javax.net.ssl.SSLSocket)326 IOException (java.io.IOException)101 Test (org.junit.Test)62 SSLContext (javax.net.ssl.SSLContext)59 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)59 Socket (java.net.Socket)57 OutputStream (java.io.OutputStream)50 InetSocketAddress (java.net.InetSocketAddress)39 CertificateException (java.security.cert.CertificateException)33 SSLException (javax.net.ssl.SSLException)32 SSLSession (javax.net.ssl.SSLSession)31 InputStream (java.io.InputStream)30 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)30 SSLServerSocket (javax.net.ssl.SSLServerSocket)27 SocketTimeoutException (java.net.SocketTimeoutException)24 SocketException (java.net.SocketException)23 ServerSocket (java.net.ServerSocket)22 UnknownHostException (java.net.UnknownHostException)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 InputStreamReader (java.io.InputStreamReader)19