Search in sources :

Example 96 with SslContextFactory

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

the class ALPNNegotiationTest method testGentleCloseDuringHandshake.

@Test
public void testGentleCloseDuringHandshake() throws Exception {
    InetSocketAddress address = prepare();
    SslContextFactory sslContextFactory = newSslContextFactory();
    sslContextFactory.start();
    SSLEngine sslEngine = sslContextFactory.newSSLEngine(address);
    sslEngine.setUseClientMode(true);
    ALPN.put(sslEngine, new ALPN.ClientProvider() {

        @Override
        public void unsupported() {
        }

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

        @Override
        public void selected(String protocol) {
        }
    });
    sslEngine.beginHandshake();
    ByteBuffer encrypted = ByteBuffer.allocate(sslEngine.getSession().getPacketBufferSize());
    sslEngine.wrap(BufferUtil.EMPTY_BUFFER, encrypted);
    encrypted.flip();
    try (SocketChannel channel = SocketChannel.open(address)) {
        // Send ClientHello, immediately followed by TLS Close Alert and then by FIN
        channel.write(encrypted);
        sslEngine.closeOutbound();
        encrypted.clear();
        sslEngine.wrap(BufferUtil.EMPTY_BUFFER, encrypted);
        encrypted.flip();
        channel.write(encrypted);
        channel.shutdownOutput();
        // Read ServerHello from server
        encrypted.clear();
        int read = channel.read(encrypted);
        encrypted.flip();
        Assert.assertTrue(read > 0);
        // Cannot decrypt, as the SSLEngine has been already closed
        // It may happen that the read() above read both the ServerHello and the TLS Close Alert.
        // Now if we can read more, we should read the TLS Close Alert and then the TCP FIN.
        encrypted.clear();
        read = channel.read(encrypted);
        if (read > 0) {
            encrypted.flip();
            Assert.assertEquals(21, encrypted.get());
            encrypted.clear();
            Assert.assertEquals(-1, channel.read(encrypted));
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) ALPN(org.eclipse.jetty.alpn.ALPN) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 97 with SslContextFactory

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

the class AbstractALPNTest method newSslContextFactory.

protected SslContextFactory newSslContextFactory() {
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/keystore.jks");
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setTrustStorePath("src/test/resources/truststore.jks");
    sslContextFactory.setTrustStorePassword("storepwd");
    sslContextFactory.setIncludeProtocols("TLSv1.2");
    // The mandatory HTTP/2 cipher.
    sslContextFactory.setIncludeCipherSuites("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
    return sslContextFactory;
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory)

Example 98 with SslContextFactory

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

the class ConnectHandlerSSLTest method prepare.

@Before
public void prepare() throws Exception {
    sslContextFactory = new SslContextFactory();
    String keyStorePath = MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath();
    sslContextFactory.setKeyStorePath(keyStorePath);
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    server = new Server();
    serverConnector = new ServerConnector(server, sslContextFactory);
    server.addConnector(serverConnector);
    server.setHandler(new ServerHandler());
    server.start();
    prepareProxy();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) Before(org.junit.Before)

Example 99 with SslContextFactory

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

the class TestOSGiUtil method newSslContextFactory.

protected static SslContextFactory newSslContextFactory() {
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    sslContextFactory.setEndpointIdentificationAlgorithm("");
    return sslContextFactory;
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory)

Example 100 with SslContextFactory

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

the class SslBytesClientTest method init.

@Before
public void init() throws Exception {
    threadPool = Executors.newCachedThreadPool();
    client = new HttpClient(new SslContextFactory(true));
    client.setMaxConnectionsPerDestination(1);
    File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
    sslContextFactory = client.getSslContextFactory();
    sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    client.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    acceptor = (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket(0);
    int serverPort = acceptor.getLocalPort();
    proxy = new SimpleProxy(threadPool, "localhost", serverPort);
    proxy.start();
    logger.info(":{} <==> :{}", proxy.getPort(), serverPort);
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpClient(org.eclipse.jetty.client.HttpClient) SSLContext(javax.net.ssl.SSLContext) File(java.io.File) Before(org.junit.Before)

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