Example usage for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate

List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate

Introduction

In this page you can find the example usage for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate.

Prototype

public SelfSignedCertificate() throws CertificateException 

Source Link

Document

Creates a new instance.

Usage

From source file:org.kaazing.messaging.driver.transport.netty.tcp.NettyTransportContext.java

License:Apache License

public NettyTransportContext() {
    super();//ww  w .  j a  v a2s  .c  om

    if (USE_SSL) {
        SelfSignedCertificate ssc = null;
        try {
            ssc = new SelfSignedCertificate();
            serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
            clientSslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
                    .build();
        } catch (CertificateException e) {
            LOGGER.error("CertificateException", e);
            throw new IllegalArgumentException("Error creating transport context", e);
        } catch (SSLException e) {
            LOGGER.error("SSLException", e);
            throw new IllegalArgumentException("Error creating transport context", e);
        }
    } else {
        serverSslCtx = null;
        clientSslCtx = null;
    }

    // Configure the server.
    serverBossGroup = new NioEventLoopGroup(1);
    serverWorkerGroup = new NioEventLoopGroup();

    serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(serverBossGroup, serverWorkerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    final ChannelPipeline p = ch.pipeline();
                    if (serverSslCtx != null) {
                        p.addLast(serverSslCtx.newHandler(ch.alloc()));
                    }
                    p.addLast(new LengthFieldBasedFrameDecoder(1000000, 0, 4, 0, 4));
                    serverReceivingTransportsLock.readLock().lock();
                    try {
                        serverReceivingTransports.forEach((nettyReceivingTransport) -> {
                            if (ch.localAddress().equals(nettyReceivingTransport.getInetSocketAddress())
                                    || nettyReceivingTransport.isInAddrAny()
                                            && ch.localAddress().getPort() == nettyReceivingTransport
                                                    .getInetSocketAddress().getPort()) {
                                p.addLast(nettyReceivingTransport.getNettyChannelHandler());
                            }
                        });
                    } finally {
                        serverReceivingTransportsLock.readLock().unlock();
                    }

                }
            });

    bootstrap = new Bootstrap();
    group = new NioEventLoopGroup();
    bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    if (clientSslCtx != null) {
                        p.addLast(clientSslCtx.newHandler(ch.alloc()));
                    }
                }
            });
}

From source file:org.nepu.chat.SecureChatServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    //SelfSignedCertificate
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    ///*from  w w  w  . j  a  va2s.c o m*/
    SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();//
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SecureChatServerInitializer(sslCtx));

        b.bind(PORT).sync().channel().closeFuture().sync();
        //bindchannnel
        //syncfuture futurefuture
        //channel futureiochannel
        //closefuture future
        //

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:org.nexxy.http.reverseproxy.HttpReverseProxyServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {// w ww.  ja  v a  2s.  c om
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // Configure the cache
    Cache.init();

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpReverseProxyServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.err.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:"
                + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:org.nosceon.titanite.AbstractHttpServerBuilder.java

License:Apache License

private SslContext sslContext(Settings.Connector connector) {
    return callUnchecked(() -> {
        if (connector.certificatePath() == null || connector.keyPath() == null) {

            Titanite.LOG//w w w .j  a  va 2s . c  o  m
                    .warn(id + " ssl certificate path or key path is missing, using self-signed certificate");

            SelfSignedCertificate ssc = new SelfSignedCertificate();
            return SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } else {
            return SslContext.newServerContext(connector.certificatePath(), connector.keyPath(),
                    connector.keyPassword());
        }
    });
}

From source file:org.nosceon.titanite.AbstractMultiE2ETest.java

License:Apache License

@BeforeClass
public static void setupSelfSignedCertificate() throws CertificateException {
    ssc = new SelfSignedCertificate();
}

From source file:org.robotbrains.support.web.server.netty.NettyWebServer.java

License:Apache License

@Override
public void startup() {
    try {/*from w w w .j av a  2 s  .c  om*/
        // Configure SSL.
        SslContext sslCtx;
        if (SSL) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } else {
            sslCtx = null;
        }

        serverHandler = new NettyWebServerHandler(this);

        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();

        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(ServerChannelWithId.class)
                .childHandler(new NettyWebServerInitializer(sslCtx, this, serverHandler));

        b.bind(port).sync();
    } catch (Throwable e) {
        throw SmartSpacesException.newFormattedException(e, "Could not create web server");
    }
}

From source file:org.springframework.cloud.stream.app.websocket.sink.WebsocketSinkServerInitializer.java

License:Apache License

private SslContext configureSslContext() throws CertificateException, SSLException {
    if (properties.isSsl()) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        return SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {/*from   ww w . ja v a  2 s .c o m*/
        return null;
    }
}

From source file:org.springframework.http.server.reactive.bootstrap.ReactorHttpsServer.java

License:Apache License

@Override
protected void initServer() throws Exception {

    SelfSignedCertificate cert = new SelfSignedCertificate();
    SslContextBuilder builder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey());

    this.reactorHandler = createHttpHandlerAdapter();
    this.reactorServer = reactor.netty.http.server.HttpServer.create().host(getHost()).port(getPort())
            .secure(spec -> spec.sslContext(builder).defaultConfiguration(DefaultConfigurationType.TCP));
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.management.Http2EndpointManager.java

License:Open Source License

public SslContext getSSLContext(InboundWebsocketSSLConfiguration sslconfig) {
    SslContext sslContext = null;//w  w  w .  j ava  2s .c  om
    SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
    try {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SSLHandlerFactory handlerFactory = new SSLHandlerFactory(sslconfig);
        sslContext = SslContextBuilder.forServer(handlerFactory.getKeyStoreFactory())
                .trustManager(handlerFactory.getTrustStoreFactory()).sslProvider(provider)
                /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
                 * Please refer to the HTTP/2 specification for cipher requirements. */
                .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
                .applicationProtocolConfig(
                        new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN,
                                // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
                                ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
                                // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
                                ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT,
                                ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1))
                .build();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (SSLException e) {
        e.printStackTrace();
    }
    return sslContext;
}

From source file:org.wso2.esb.integration.common.utils.servers.Http2Server.java

License:Open Source License

public void startServer() throws Exception {
    final SslContext sslCtx;
    if (SSL) {/*  w  w w .jav a 2  s  .c  o  m*/
        SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(provider)
                .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
                .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN,
                        SelectorFailureBehavior.NO_ADVERTISE, SelectedListenerFailureBehavior.ACCEPT,
                        ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1))
                .build();
    } else {
        sslCtx = null;
    }
    group = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new Http2ServerInitializer(sslCtx));

    b.bind("127.0.0.5", PORT).sync().channel();

}