Search in sources :

Example 81 with SslContextFactory

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

the class SecuredRedirectHandlerTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    // Setup SSL
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    sslContextFactory.setTrustStorePath(keystore.getAbsolutePath());
    sslContextFactory.setTrustStorePassword("storepwd");
    server = new Server();
    int port = 32080;
    int securePort = 32443;
    // Setup HTTP Configuration
    HttpConfiguration httpConf = new HttpConfiguration();
    httpConf.setSecurePort(securePort);
    httpConf.setSecureScheme("https");
    ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConf));
    httpConnector.setName("unsecured");
    httpConnector.setPort(port);
    // Setup HTTPS Configuration
    HttpConfiguration httpsConf = new HttpConfiguration(httpConf);
    httpsConf.addCustomizer(new SecureRequestCustomizer());
    ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(httpsConf));
    httpsConnector.setName("secured");
    httpsConnector.setPort(securePort);
    // Add connectors
    server.setConnectors(new Connector[] { httpConnector, httpsConnector });
    // Wire up contexts
    String[] secureHosts = new String[] { "@secured" };
    ContextHandler test1Context = new ContextHandler();
    test1Context.setContextPath("/test1");
    test1Context.setHandler(new HelloHandler("Hello1"));
    test1Context.setVirtualHosts(secureHosts);
    ContextHandler test2Context = new ContextHandler();
    test2Context.setContextPath("/test2");
    test2Context.setHandler(new HelloHandler("Hello2"));
    test2Context.setVirtualHosts(secureHosts);
    ContextHandler rootContext = new ContextHandler();
    rootContext.setContextPath("/");
    rootContext.setHandler(new RootHandler("/test1", "/test2"));
    rootContext.setVirtualHosts(secureHosts);
    // Wire up context for unsecure handling to only
    // the named 'unsecured' connector
    ContextHandler redirectHandler = new ContextHandler();
    redirectHandler.setContextPath("/");
    redirectHandler.setHandler(new SecuredRedirectHandler());
    redirectHandler.setVirtualHosts(new String[] { "@unsecured" });
    // Establish all handlers that have a context
    ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
    contextHandlers.setHandlers(new Handler[] { redirectHandler, rootContext, test1Context, test2Context });
    // Create server level handler tree
    HandlerList handlers = new HandlerList();
    handlers.addHandler(contextHandlers);
    // round things out
    handlers.addHandler(new DefaultHandler());
    server.setHandler(handlers);
    server.start();
    // calculate serverUri
    String host = httpConnector.getHost();
    if (host == null) {
        host = "localhost";
    }
    serverHttpUri = new URI(String.format("http://%s:%d/", host, httpConnector.getLocalPort()));
    serverHttpsUri = new URI(String.format("https://%s:%d/", host, httpsConnector.getLocalPort()));
    origVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    origSsf = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new AllowAllVerifier());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContextFactory.getSslContext().getSocketFactory());
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 82 with SslContextFactory

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

the class SslConnectionFactoryTest method getResponse.

private String getResponse(String sniHost, String reqHost, String cn) throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port);
    if (cn != null) {
        SNIHostName serverName = new SNIHostName(sniHost);
        List<SNIServerName> serverNames = new ArrayList<>();
        serverNames.add(serverName);
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(serverNames);
        sslSocket.setSSLParameters(params);
    }
    sslSocket.startHandshake();
    if (cn != null) {
        X509Certificate cert = ((X509Certificate) sslSocket.getSession().getPeerCertificates()[0]);
        Assert.assertThat(cert.getSubjectX500Principal().getName("CANONICAL"), Matchers.startsWith("cn=" + cn));
    }
    sslSocket.getOutputStream().write(("GET /ctx/path HTTP/1.0\r\nHost: " + reqHost + ":" + _port + "\r\n\r\n").getBytes(StandardCharsets.ISO_8859_1));
    String response = IO.toString(sslSocket.getInputStream());
    sslSocket.close();
    clientContextFactory.stop();
    return response;
}
Also used : SNIServerName(javax.net.ssl.SNIServerName) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) SSLSocket(javax.net.ssl.SSLSocket) ArrayList(java.util.ArrayList) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) X509Certificate(java.security.cert.X509Certificate)

Example 83 with SslContextFactory

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

the class SslContextFactoryReloadTest method start.

private void start(Handler handler) throws Exception {
    server = new Server();
    sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(KEYSTORE_1);
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyStoreType("JKS");
    sslContextFactory.setKeyStoreProvider(null);
    HttpConfiguration httpsConfig = new HttpConfiguration();
    httpsConfig.addCustomizer(new SecureRequestCustomizer());
    connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
    server.addConnector(connector);
    server.setHandler(handler);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 84 with SslContextFactory

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

the class SslUploadTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    File keystore = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    sslContextFactory.setTrustStorePath(keystore.getAbsolutePath());
    sslContextFactory.setTrustStorePassword("storepwd");
    server = new Server();
    connector = new ServerConnector(server, sslContextFactory);
    server.addConnector(connector);
    server.setHandler(new EmptyHandler());
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Server(org.eclipse.jetty.server.Server) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 85 with SslContextFactory

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

the class DebugHandlerTest method startServer.

@SuppressWarnings("deprecation")
@Before
public void startServer() throws Exception {
    server = new Server();
    ServerConnector httpConnector = new ServerConnector(server);
    httpConnector.setPort(0);
    server.addConnector(httpConnector);
    File keystorePath = MavenTestingUtils.getTestResourceFile("keystore");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keystorePath.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.setKeyManagerPassword("keypwd");
    sslContextFactory.setTrustStorePath(keystorePath.getAbsolutePath());
    sslContextFactory.setTrustStorePassword("storepwd");
    ByteBufferPool pool = new LeakTrackingByteBufferPool(new MappedByteBufferPool.Tagged());
    ServerConnector sslConnector = new ServerConnector(server, (Executor) null, (Scheduler) null, pool, 1, 1, AbstractConnectionFactory.getFactories(sslContextFactory, new HttpConnectionFactory()));
    server.addConnector(sslConnector);
    debugHandler = new DebugHandler();
    capturedLog = new ByteArrayOutputStream();
    debugHandler.setOutputStream(capturedLog);
    debugHandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setStatus(HttpStatus.OK_200);
        }
    });
    server.setHandler(debugHandler);
    server.start();
    String host = httpConnector.getHost();
    if (host == null)
        host = "localhost";
    serverURI = URI.create(String.format("http://%s:%d/", host, httpConnector.getLocalPort()));
    secureServerURI = URI.create(String.format("https://%s:%d/", host, sslConnector.getLocalPort()));
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try (InputStream stream = sslContextFactory.getKeyStoreResource().getInputStream()) {
        keystore.load(stream, "storepwd".toCharArray());
    }
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keystore);
    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(__hostnameverifier);
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, SslContextFactory.TRUST_ALL_CERTS, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) Server(org.eclipse.jetty.server.Server) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InputStream(java.io.InputStream) LeakTrackingByteBufferPool(org.eclipse.jetty.io.LeakTrackingByteBufferPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) 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