Search in sources :

Example 6 with JdkSslContext

use of io.netty.handler.ssl.JdkSslContext in project netty by netty.

the class SocketSslSessionReuseTest method testSslSessionReuse.

public void testSslSessionReuse(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final ReadAndDiscardHandler sh = new ReadAndDiscardHandler(true, true);
    final ReadAndDiscardHandler ch = new ReadAndDiscardHandler(false, true);
    final String[] protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
    sb.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel sch) throws Exception {
            SSLEngine engine = serverCtx.newEngine(sch.alloc());
            engine.setUseClientMode(false);
            engine.setEnabledProtocols(protocols);
            sch.pipeline().addLast(new SslHandler(engine));
            sch.pipeline().addLast(sh);
        }
    });
    final Channel sc = sb.bind().sync().channel();
    cb.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel sch) throws Exception {
            InetSocketAddress serverAddr = (InetSocketAddress) sc.localAddress();
            SSLEngine engine = clientCtx.newEngine(sch.alloc(), serverAddr.getHostString(), serverAddr.getPort());
            engine.setUseClientMode(true);
            engine.setEnabledProtocols(protocols);
            sch.pipeline().addLast(new SslHandler(engine));
            sch.pipeline().addLast(ch);
        }
    });
    try {
        SSLSessionContext clientSessionCtx = ((JdkSslContext) clientCtx).sessionContext();
        ByteBuf msg = Unpooled.wrappedBuffer(new byte[] { 0xa, 0xb, 0xc, 0xd }, 0, 4);
        Channel cc = cb.connect().sync().channel();
        cc.writeAndFlush(msg).sync();
        cc.closeFuture().sync();
        rethrowHandlerExceptions(sh, ch);
        Set<String> sessions = sessionIdSet(clientSessionCtx.getIds());
        msg = Unpooled.wrappedBuffer(new byte[] { 0xa, 0xb, 0xc, 0xd }, 0, 4);
        cc = cb.connect().sync().channel();
        cc.writeAndFlush(msg).sync();
        cc.closeFuture().sync();
        assertEquals("Expected no new sessions", sessions, sessionIdSet(clientSessionCtx.getIds()));
        rethrowHandlerExceptions(sh, ch);
    } finally {
        sc.close().awaitUninterruptibly();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SSLSessionContext(javax.net.ssl.SSLSessionContext) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SslHandler(io.netty.handler.ssl.SslHandler)

Example 7 with JdkSslContext

use of io.netty.handler.ssl.JdkSslContext in project camel by apache.

the class AhcEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (client == null) {
        AsyncHttpClientConfig config = null;
        if (clientConfig != null) {
            DefaultAsyncHttpClientConfig.Builder builder = AhcComponent.cloneConfig(clientConfig);
            if (sslContextParameters != null) {
                SSLContext sslContext = sslContextParameters.createSSLContext(getCamelContext());
                JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
                builder.setSslContext(ssl);
            }
            config = builder.build();
        } else {
            if (sslContextParameters != null) {
                DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
                SSLContext sslContext = sslContextParameters.createSSLContext(getCamelContext());
                JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
                builder.setSslContext(ssl);
                config = builder.build();
            }
        }
        client = createClient(config);
    }
}
Also used : JdkSslContext(io.netty.handler.ssl.JdkSslContext) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) SSLContext(javax.net.ssl.SSLContext)

Aggregations

JdkSslContext (io.netty.handler.ssl.JdkSslContext)7 SSLContext (javax.net.ssl.SSLContext)3 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)3 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)3 Channel (io.netty.channel.Channel)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 SslHandler (io.netty.handler.ssl.SslHandler)2 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)2 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)2 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)1