Search in sources :

Example 6 with SSLHelper

use of io.vertx.core.net.impl.SSLHelper in project vert.x by eclipse.

the class SSLEngineTest method doTest.

private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
    server.close();
    HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
    server = vertx.createHttpServer(options);
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    try {
        startServer();
        if (error != null) {
            fail("Was expecting failure: " + error);
        }
    } catch (Exception e) {
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
            return;
        }
    }
    SSLHelper sslHelper = ((HttpServerImpl) server).sslHelper();
    SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
    switch(expectedSslContext != null ? expectedSslContext : "jdk") {
        case "jdk":
            assertTrue(ctx instanceof JdkSslContext);
            break;
        case "openssl":
            assertTrue(ctx instanceof OpenSslContext);
            break;
    }
    client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
    client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(req -> {
        req.send(onSuccess(resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        }));
    }));
    await();
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxException(io.vertx.core.VertxException) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) Test(org.junit.Test) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.core.http.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpMethod(io.vertx.core.http.HttpMethod) Cert(io.vertx.test.tls.Cert) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) JdkSslContext(io.netty.handler.ssl.JdkSslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) VertxException(io.vertx.core.VertxException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext)

Example 7 with SSLHelper

use of io.vertx.core.net.impl.SSLHelper in project vert.x by eclipse.

the class Http1xServerConnection method netSocket.

void netSocket(Promise<NetSocket> promise) {
    context.execute(() -> {
        // Flush out all pending data
        flush();
        // remove old http handlers and replace the old handler with one that handle plain sockets
        ChannelPipeline pipeline = chctx.pipeline();
        ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
        if (compressor != null) {
            pipeline.remove(compressor);
        }
        pipeline.remove("httpDecoder");
        if (pipeline.get("chunkedWriter") != null) {
            pipeline.remove("chunkedWriter");
        }
        pipeline.replace("handler", "handler", VertxHandler.create(ctx -> {
            NetSocketImpl socket = new NetSocketImpl(context, ctx, sslHelper, metrics) {

                @Override
                protected void handleClosed() {
                    if (metrics != null) {
                        Http1xServerRequest request = Http1xServerConnection.this.responseInProgress;
                        metrics.responseEnd(request.metric(), request.response(), request.response().bytesWritten());
                    }
                    super.handleClosed();
                }

                @Override
                public synchronized void handleMessage(Object msg) {
                    if (msg instanceof HttpContent) {
                        ReferenceCountUtil.release(msg);
                        return;
                    }
                    super.handleMessage(msg);
                }
            };
            socket.metric(metric());
            return socket;
        }));
        // check if the encoder can be removed yet or not.
        pipeline.remove("httpEncoder");
        // 
        VertxHandler<NetSocketImpl> handler = (VertxHandler<NetSocketImpl>) pipeline.get("handler");
        promise.complete(handler.getConnection());
    });
}
Also used : NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ServerWebSocket(io.vertx.core.http.ServerWebSocket) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) ContextInternal(io.vertx.core.impl.ContextInternal) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) BAD_REQUEST(io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST) HttpServerMetrics(io.vertx.core.spi.metrics.HttpServerMetrics) METHOD_NOT_ALLOWED(io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED) io.netty.channel(io.netty.channel) HTTP_1_1(io.netty.handler.codec.http.HttpVersion.HTTP_1_1) AsyncResult(io.vertx.core.AsyncResult) TracingPolicy(io.vertx.core.tracing.TracingPolicy) Logger(io.vertx.core.impl.logging.Logger) VertxHandler(io.vertx.core.net.impl.VertxHandler) CONTINUE(io.netty.handler.codec.http.HttpResponseStatus.CONTINUE) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) VertxTracer(io.vertx.core.spi.tracing.VertxTracer) Future(io.vertx.core.Future) SSLHelper(io.vertx.core.net.impl.SSLHelper) UPGRADE_REQUIRED(io.netty.handler.codec.http.HttpResponseStatus.UPGRADE_REQUIRED) DecoderResult(io.netty.handler.codec.DecoderResult) io.netty.handler.codec.http(io.netty.handler.codec.http) Buffer(io.vertx.core.buffer.Buffer) io.netty.handler.codec.http.websocketx(io.netty.handler.codec.http.websocketx) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpServerOptions(io.vertx.core.http.HttpServerOptions) METRICS_ENABLED(io.vertx.core.spi.metrics.Metrics.METRICS_ENABLED) Handler(io.vertx.core.Handler) NetSocket(io.vertx.core.net.NetSocket) VertxHandler(io.vertx.core.net.impl.VertxHandler) NetSocketImpl(io.vertx.core.net.impl.NetSocketImpl)

Example 8 with SSLHelper

use of io.vertx.core.net.impl.SSLHelper in project vert.x by eclipse.

the class SSLEngineTest method doTest.

private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
    server.close();
    HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
    try {
        server = vertx.createHttpServer(options);
    } catch (VertxException e) {
        e.printStackTrace();
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
        }
        return;
    }
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    server.listen(onSuccess(s -> {
        HttpServerImpl impl = (HttpServerImpl) s;
        SSLHelper sslHelper = impl.getSslHelper();
        SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
        switch(expectedSslContext) {
            case "jdk":
                assertTrue(ctx instanceof JdkSslContext);
                break;
            case "openssl":
                assertTrue(ctx instanceof OpenSslContext);
                break;
        }
        client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
        client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        });
    }));
    await();
}
Also used : VertxException(io.vertx.core.VertxException) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) Test(org.junit.Test) Cert(io.vertx.test.core.tls.Cert) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.test.core.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) VertxException(io.vertx.core.VertxException) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext)

Example 9 with SSLHelper

use of io.vertx.core.net.impl.SSLHelper in project vert.x by eclipse.

the class SSLHelperTest method testPreserveEnabledCipherSuitesOrder.

@Test
public void testPreserveEnabledCipherSuitesOrder() throws Exception {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, null, null);
    SSLEngine engine = context.createSSLEngine();
    HttpServerOptions options = new HttpServerOptions();
    for (String suite : engine.getEnabledCipherSuites()) {
        options.addEnabledCipherSuite(suite);
    }
    assertEquals(new ArrayList<>(options.getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    assertEquals(new ArrayList<>(new HttpServerOptions(options).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    JsonObject json = new JsonObject();
    NetworkOptionsConverter.toJson(options, json);
    TCPSSLOptionsConverter.toJson(options, json);
    NetServerOptionsConverter.toJson(options, json);
    HttpServerOptionsConverter.toJson(options, json);
    assertEquals(new ArrayList<>(new HttpServerOptions(json).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
    SSLHelper helper = new SSLHelper(options, Cert.SERVER_JKS.get(), null);
    assertEquals(Arrays.asList(helper.createSslHandler((VertxInternal) vertx).engine().getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) SSLEngine(javax.net.ssl.SSLEngine) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) SSLContext(javax.net.ssl.SSLContext) Test(org.junit.Test)

Example 10 with SSLHelper

use of io.vertx.core.net.impl.SSLHelper in project vert.x by eclipse.

the class SSLHelperTest method testOpenSslServerSessionContext.

private void testOpenSslServerSessionContext(boolean testDefault) {
    HttpServerOptions httpServerOptions = new HttpServerOptions().setOpenSslEngineOptions(new OpenSSLEngineOptions());
    if (!testDefault) {
        httpServerOptions.setOpenSslEngineOptions(new OpenSSLEngineOptions().setSessionCacheEnabled(false));
    }
    SSLHelper defaultHelper = new SSLHelper(httpServerOptions, Cert.SERVER_PEM.get(), Trust.SERVER_PEM.get());
    SslContext ctx = defaultHelper.getContext((VertxInternal) vertx);
    assertTrue(ctx instanceof OpenSslServerContext);
    SSLSessionContext sslSessionContext = ctx.sessionContext();
    assertTrue(sslSessionContext instanceof OpenSslServerSessionContext);
    if (sslSessionContext instanceof OpenSslServerSessionContext) {
        assertEquals(testDefault, ((OpenSslServerSessionContext) sslSessionContext).isSessionCacheEnabled());
    }
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) SSLSessionContext(javax.net.ssl.SSLSessionContext) OpenSslServerContext(io.netty.handler.ssl.OpenSslServerContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) OpenSslServerSessionContext(io.netty.handler.ssl.OpenSslServerSessionContext) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

SSLHelper (io.vertx.core.net.impl.SSLHelper)14 Test (org.junit.Test)9 SslContext (io.netty.handler.ssl.SslContext)8 HttpServerOptions (io.vertx.core.http.HttpServerOptions)8 HttpClientOptions (io.vertx.core.http.HttpClientOptions)6 VertxInternal (io.vertx.core.impl.VertxInternal)5 SSLContext (javax.net.ssl.SSLContext)4 SSLEngine (javax.net.ssl.SSLEngine)4 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)3 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 Channel (io.netty.channel.Channel)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)2 JdkSslContext (io.netty.handler.ssl.JdkSslContext)2 OpenSslContext (io.netty.handler.ssl.OpenSslContext)2 OpenSslServerContext (io.netty.handler.ssl.OpenSslServerContext)2 OpenSslServerSessionContext (io.netty.handler.ssl.OpenSslServerSessionContext)2