Search in sources :

Example 36 with SslContextFactory

use of org.eclipse.jetty.util.ssl.SslContextFactory in project jetty.project by eclipse.

the class SSLCloseTest method testClose.

@Test
public void testClose() throws Exception {
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStoreResource(Resource.newResource(keystore));
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server, sslContextFactory);
    connector.setPort(0);
    server.addConnector(connector);
    server.setHandler(new WriteHandler());
    server.start();
    SSLContext ctx = SSLContext.getInstance("TLSv1.2");
    ctx.init(null, SslContextFactory.TRUST_ALL_CERTS, new java.security.SecureRandom());
    int port = connector.getLocalPort();
    Socket socket = ctx.getSocketFactory().createSocket("localhost", port);
    OutputStream os = socket.getOutputStream();
    os.write(("GET /test HTTP/1.1\r\n" + "Host:test\r\n" + "Connection:close\r\n\r\n").getBytes());
    os.flush();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        if (line.trim().length() == 0)
            break;
    }
    Thread.sleep(2000);
    while (in.readLine() != null) Thread.yield();
}
Also used : Server(org.eclipse.jetty.server.Server) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) SSLContext(javax.net.ssl.SSLContext) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) BufferedReader(java.io.BufferedReader) File(java.io.File) Socket(java.net.Socket) Test(org.junit.Test)

Example 37 with SslContextFactory

use of org.eclipse.jetty.util.ssl.SslContextFactory in project jetty.project by eclipse.

the class SSLEngineTest method startServer.

@Before
public void startServer() throws Exception {
    String keystore = MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath();
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystore);
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    server = new Server();
    HttpConnectionFactory http = new HttpConnectionFactory();
    http.setInputBufferSize(512);
    http.getHttpConfiguration().setRequestHeaderSize(512);
    connector = new ServerConnector(server, sslContextFactory, http);
    connector.setPort(0);
    connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setSendDateHeader(false);
    server.addConnector(connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) Before(org.junit.Before)

Example 38 with SslContextFactory

use of org.eclipse.jetty.util.ssl.SslContextFactory in project jetty.project by eclipse.

the class SSLSelectChannelConnectorLoadTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    String keystorePath = System.getProperty("basedir", ".") + "/src/test/resources/keystore";
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystorePath);
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    sslContextFactory.setTrustStorePath(keystorePath);
    sslContextFactory.setTrustStorePassword("storepwd");
    server = new Server();
    connector = new ServerConnector(server, sslContextFactory);
    server.addConnector(connector);
    server.setHandler(new EmptyHandler());
    server.start();
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream stream = new FileInputStream(keystorePath)) {
        keystore.load(stream, "storepwd".toCharArray());
    }
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keystore);
    sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) BeforeClass(org.junit.BeforeClass)

Example 39 with SslContextFactory

use of org.eclipse.jetty.util.ssl.SslContextFactory in project jetty.project by eclipse.

the class SlowClientsTest method testSlowClientsWithSmallThreadPool.

@Test(timeout = 10000)
public void testSlowClientsWithSmallThreadPool() throws Exception {
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    int maxThreads = 6;
    int contentLength = 8 * 1024 * 1024;
    QueuedThreadPool serverThreads = new QueuedThreadPool(maxThreads);
    serverThreads.setDetailedDump(true);
    Server server = new Server(serverThreads);
    try {
        ServerConnector connector = new ServerConnector(server, 1, 1, sslContextFactory);
        connector.setPort(8888);
        server.addConnector(connector);
        server.setHandler(new AbstractHandler() {

            @Override
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                baseRequest.setHandled(true);
                logger.info("SERVING {}", target);
                // Write some big content.
                response.getOutputStream().write(new byte[contentLength]);
                logger.info("SERVED {}", target);
            }
        });
        server.start();
        SSLContext sslContext = sslContextFactory.getSslContext();
        CompletableFuture[] futures = new CompletableFuture[2 * maxThreads];
        ExecutorService executor = Executors.newFixedThreadPool(futures.length);
        for (int i = 0; i < futures.length; i++) {
            int k = i;
            futures[i] = CompletableFuture.runAsync(() -> {
                try (SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
                    socket.setSoTimeout(contentLength / 1024);
                    OutputStream output = socket.getOutputStream();
                    String target = "/" + k;
                    String request = "GET " + target + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
                    output.write(request.getBytes(StandardCharsets.UTF_8));
                    output.flush();
                    while (serverThreads.getIdleThreads() > 0) Thread.sleep(50);
                    InputStream input = socket.getInputStream();
                    while (true) {
                        int read = input.read();
                        if (read < 0)
                            break;
                    }
                    logger.info("FINISHED {}", target);
                } catch (IOException x) {
                    throw new UncheckedIOException(x);
                } catch (InterruptedException x) {
                    throw new UncheckedIOException(new InterruptedIOException());
                }
            }, executor);
        }
        CompletableFuture.allOf(futures).join();
    } finally {
        server.stop();
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Server(org.eclipse.jetty.server.Server) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SSLContext(javax.net.ssl.SSLContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) CompletableFuture(java.util.concurrent.CompletableFuture) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) Test(org.junit.Test)

Example 40 with SslContextFactory

use of org.eclipse.jetty.util.ssl.SslContextFactory in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method testSameConnectionRequestsForManyWildDomains.

@Test
public void testSameConnectionRequestsForManyWildDomains() throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    try (SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port)) {
        SNIHostName serverName = new SNIHostName("www.domain.com");
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(Collections.singletonList(serverName));
        sslSocket.setSSLParameters(params);
        sslSocket.startHandshake();
        String request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.domain.com\r\n" + "\r\n";
        OutputStream output = sslSocket.getOutputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = sslSocket.getInputStream();
        String response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now, on the same socket, send a request for a different valid domain.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: assets.domain.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now make a request for an invalid domain for this connection.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 400 "));
        Assert.assertThat(response, Matchers.containsString("Host does not match SNI"));
    } finally {
        clientContextFactory.stop();
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Aggregations

SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)139 ServerConnector (org.eclipse.jetty.server.ServerConnector)54 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)44 Server (org.eclipse.jetty.server.Server)43 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)43 Test (org.junit.Test)40 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)37 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)35 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)23 InputStream (java.io.InputStream)18 IOException (java.io.IOException)17 File (java.io.File)15 SSLContext (javax.net.ssl.SSLContext)15 ServletException (javax.servlet.ServletException)15 OutputStream (java.io.OutputStream)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)13 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)11 InetSocketAddress (java.net.InetSocketAddress)10