Search in sources :

Example 26 with SSLSocket

use of javax.net.ssl.SSLSocket in project jetty.project by eclipse.

the class SslBytesServerTest method testRequestWithBigContentWithSplitBoundary.

@Test(timeout = 60000)
public void testRequestWithBigContentWithSplitBoundary() throws Exception {
    final SSLSocket client = newClient();
    SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
    client.startHandshake();
    Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
    // Use a content that is larger than the TLS record which is 2^14 (around 16k)
    byte[] data = new byte[128 * 1024];
    Arrays.fill(data, (byte) 'X');
    final String content = new String(data, StandardCharsets.UTF_8);
    Future<Object> request = threadPool.submit(() -> {
        OutputStream clientOutput = client.getOutputStream();
        clientOutput.write(("" + "POST / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + content.length() + "\r\n" + "\r\n" + content).getBytes(StandardCharsets.UTF_8));
        clientOutput.flush();
        return null;
    });
    // Nine TLSRecords will be generated for the request
    for (int i = 0; i < 9; ++i) {
        // Application data
        TLSRecord record = proxy.readFromClient();
        byte[] bytes = record.getBytes();
        byte[] chunk1 = new byte[2 * bytes.length / 3];
        System.arraycopy(bytes, 0, chunk1, 0, chunk1.length);
        byte[] chunk2 = new byte[bytes.length - chunk1.length];
        System.arraycopy(bytes, chunk1.length, chunk2, 0, chunk2.length);
        proxy.flushToServer(100, chunk1);
        proxy.flushToServer(100, chunk2);
    }
    // Check that we did not spin
    TimeUnit.MILLISECONDS.sleep(500);
    Assert.assertThat(sslFills.get(), Matchers.lessThan(100));
    Assert.assertThat(sslFlushes.get(), Matchers.lessThan(50));
    Assert.assertThat(httpParses.get(), Matchers.lessThan(100));
    Assert.assertNull(request.get(5, TimeUnit.SECONDS));
    TLSRecord record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType());
    proxy.flushToClient(record);
    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8));
    String line = reader.readLine();
    Assert.assertNotNull(line);
    Assert.assertTrue(line.startsWith("HTTP/1.1 200 "));
    while ((line = reader.readLine()) != null) {
        if (line.trim().length() == 0)
            break;
    }
    // Check that we did not spin
    TimeUnit.MILLISECONDS.sleep(500);
    Assert.assertThat(sslFills.get(), Matchers.lessThan(100));
    Assert.assertThat(sslFlushes.get(), Matchers.lessThan(50));
    Assert.assertThat(httpParses.get(), Matchers.lessThan(100));
    closeClient(client);
}
Also used : InputStreamReader(java.io.InputStreamReader) SSLSocket(javax.net.ssl.SSLSocket) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) EndPoint(org.eclipse.jetty.io.EndPoint) ChannelEndPoint(org.eclipse.jetty.io.ChannelEndPoint) Test(org.junit.Test)

Example 27 with SSLSocket

use of javax.net.ssl.SSLSocket in project jetty.project by eclipse.

the class ALPNNegotiationTest method testClientAdvertisingMultipleProtocolsServerSpeaksHTTPWhenNegotiated.

@Test
public void testClientAdvertisingMultipleProtocolsServerSpeaksHTTPWhenNegotiated() throws Exception {
    InetSocketAddress address = prepare();
    SslContextFactory sslContextFactory = newSslContextFactory();
    sslContextFactory.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    try (SSLSocket client = (SSLSocket) sslContext.getSocketFactory().createSocket(address.getAddress(), address.getPort())) {
        client.setUseClientMode(true);
        client.setSoTimeout(5000);
        ALPN.put(client, new ALPN.ClientProvider() {

            @Override
            public void unsupported() {
            }

            @Override
            public List<String> protocols() {
                return Arrays.asList("unknown/1.0", "http/1.1");
            }

            @Override
            public void selected(String protocol) {
                Assert.assertEquals("http/1.1", protocol);
            }
        });
        client.startHandshake();
        // Verify that the server really speaks http/1.1
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost:" + address.getPort() + "\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String line = reader.readLine();
        Assert.assertTrue(line.contains(" 404 "));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ALPN(org.eclipse.jetty.alpn.ALPN) BufferedReader(java.io.BufferedReader) List(java.util.List) Test(org.junit.Test)

Example 28 with SSLSocket

use of javax.net.ssl.SSLSocket in project jetty.project by eclipse.

the class ALPNNegotiationTest method testClientAdvertisingHTTPServerSpeaksHTTP.

@Test
public void testClientAdvertisingHTTPServerSpeaksHTTP() throws Exception {
    InetSocketAddress address = prepare();
    SslContextFactory sslContextFactory = newSslContextFactory();
    sslContextFactory.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    try (SSLSocket client = (SSLSocket) sslContext.getSocketFactory().createSocket(address.getAddress(), address.getPort())) {
        client.setUseClientMode(true);
        client.setSoTimeout(5000);
        ALPN.put(client, new ALPN.ClientProvider() {

            @Override
            public void unsupported() {
            }

            @Override
            public List<String> protocols() {
                return Arrays.asList("http/1.1");
            }

            @Override
            public void selected(String protocol) {
                Assert.assertEquals("http/1.1", protocol);
            }
        });
        client.startHandshake();
        // Verify that the server really speaks http/1.1
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost:" + address.getPort() + "\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String line = reader.readLine();
        Assert.assertTrue(line.contains(" 404 "));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ALPN(org.eclipse.jetty.alpn.ALPN) BufferedReader(java.io.BufferedReader) List(java.util.List) Test(org.junit.Test)

Example 29 with SSLSocket

use of javax.net.ssl.SSLSocket in project jetty.project by eclipse.

the class SelectChannelEndPointSslTest method newClient.

@Override
protected Socket newClient() throws IOException {
    SSLSocket socket = __sslCtxFactory.newSslSocket();
    socket.connect(_connector.socket().getLocalSocketAddress());
    return socket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket)

Example 30 with SSLSocket

use of javax.net.ssl.SSLSocket in project jetty.project by eclipse.

the class SslConnectionTest method newClient.

protected Socket newClient() throws IOException {
    SSLSocket socket = __sslCtxFactory.newSslSocket();
    socket.connect(_connector.socket().getLocalSocketAddress());
    return socket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket)

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