Search in sources :

Example 66 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class NativeCryptoTest method test_SSL_do_handshake_with_channel_id_not_enabled_by_client.

public void test_SSL_do_handshake_with_channel_id_not_enabled_by_client() throws Exception {
    initChannelIdKey();
    // Client does not use TLS Channel ID when the server has the extension enabled/offered.
    final ServerSocket listener = new ServerSocket(0);
    Hooks cHooks = new Hooks();
    cHooks.channelIdPrivateKey = null;
    ServerHooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
    sHooks.channelIdEnabled = true;
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    TestSSLHandshakeCallbacks clientCallback = client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    TestSSLHandshakeCallbacks serverCallback = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    assertTrue(clientCallback.verifyCertificateChainCalled);
    assertEqualCertificateChains(getServerCertificates(), clientCallback.asn1DerEncodedCertificateChain);
    assertEquals("RSA", clientCallback.authMethod);
    assertFalse(serverCallback.verifyCertificateChainCalled);
    assertFalse(clientCallback.clientCertificateRequestedCalled);
    assertFalse(serverCallback.clientCertificateRequestedCalled);
    assertTrue(clientCallback.handshakeCompletedCalled);
    assertTrue(serverCallback.handshakeCompletedCalled);
    assertNull(sHooks.channelIdAfterHandshakeException);
    assertNull(sHooks.channelIdAfterHandshake);
}
Also used : ServerSocket(java.net.ServerSocket)

Example 67 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class NativeCryptoTest method test_SSL_SESSION_session_id.

public void test_SSL_SESSION_session_id() throws Exception {
    try {
        NativeCrypto.SSL_SESSION_session_id(NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    final ServerSocket listener = new ServerSocket(0);
    Hooks cHooks = new Hooks() {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[] id = NativeCrypto.SSL_SESSION_session_id(session);
            assertNotNull(id);
            assertEquals(32, id.length);
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 68 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class NativeCryptoTest method test_SSL_SESSION_get_time.

public void test_SSL_SESSION_get_time() throws Exception {
    try {
        NativeCrypto.SSL_SESSION_get_time(NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    final ServerSocket listener = new ServerSocket(0);
    {
        Hooks cHooks = new Hooks() {

            @Override
            public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                long time = NativeCrypto.SSL_SESSION_get_time(session);
                assertTrue(time != 0);
                assertTrue(time < System.currentTimeMillis());
                super.afterHandshake(session, s, c, sock, fd, callback);
            }
        };
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
        Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
        Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
        client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) Future(java.util.concurrent.Future) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException)

Example 69 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class NativeCryptoTest method test_SSL_do_handshake_server_timeout.

public void test_SSL_do_handshake_server_timeout() throws Exception {
    // server timeout
    final ServerSocket listener = new ServerSocket(0);
    Socket clientSocket = null;
    try {
        Hooks cHooks = new Hooks();
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
        Future<TestSSLHandshakeCallbacks> client = handshake(listener, -1, true, cHooks, null, null);
        Future<TestSSLHandshakeCallbacks> server = handshake(listener, 1, false, sHooks, null, null);
        clientSocket = client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getSocket();
        server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        fail();
    } catch (ExecutionException expected) {
        assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
    } finally {
        // Manually close peer socket when testing timeout
        IoUtils.closeQuietly(clientSocket);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 70 with ServerSocket

use of java.net.ServerSocket in project robovm by robovm.

the class NativeCryptoTest method test_SSL_do_handshake_missing_required_certificate.

public void test_SSL_do_handshake_missing_required_certificate() throws Exception {
    // required client certificate negative case
    final ServerSocket listener = new ServerSocket(0);
    try {
        Hooks cHooks = new Hooks();
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

            @Override
            public long beforeHandshake(long c) throws SSLException {
                long s = super.beforeHandshake(c);
                NativeCrypto.SSL_set_client_CA_list(s, getCaPrincipals());
                NativeCrypto.SSL_set_verify(s, NativeCrypto.SSL_VERIFY_PEER | NativeCrypto.SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
                return s;
            }
        };
        Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
        Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
        server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        fail();
    } catch (ExecutionException expected) {
        assertEquals(SSLProtocolException.class, expected.getCause().getClass());
    }
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) ServerSocket(java.net.ServerSocket) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ServerSocket (java.net.ServerSocket)736 IOException (java.io.IOException)336 Socket (java.net.Socket)265 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 File (java.io.File)23 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16