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

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

Introduction

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

Prototype

File privateKey

To view the source code for io.netty.handler.ssl.util SelfSignedCertificate privateKey.

Click Source Link

Usage

From source file:org.ftccommunity.services.DevConsole.java

License:Apache License

/**
 * Start the service./* w  w  w  .ja  v  a  2 s .  c  om*/
 */
@Override
protected void startUp() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new TelnetServerInitializer(sslCtx));
    mainThread = Thread.currentThread();
}

From source file:org.graylog2.plugin.inputs.transports.AbstractTcpTransport.java

License:Open Source License

private Callable<ChannelHandler> getSslHandlerCallable(MessageInput input) {
    final File certFile;
    final File keyFile;
    if (tlsCertFile.exists() && tlsKeyFile.exists()) {
        certFile = tlsCertFile;//from www  .  j  a  v a2s . c o m
        keyFile = tlsKeyFile;
    } else {
        LOG.warn(
                "TLS key file or certificate file does not exist, creating a self-signed certificate for input [{}/{}].",
                input.getName(), input.getId());

        final String tmpDir = System.getProperty("java.io.tmpdir");
        checkState(tmpDir != null, "The temporary directory must not be null!");
        final Path tmpPath = Paths.get(tmpDir);
        if (!Files.isDirectory(tmpPath) || !Files.isWritable(tmpPath)) {
            throw new IllegalStateException(
                    "Couldn't write to temporary directory: " + tmpPath.toAbsolutePath());
        }

        try {
            final SelfSignedCertificate ssc = new SelfSignedCertificate(
                    configuration.getString(CK_BIND_ADDRESS) + ":" + configuration.getString(CK_PORT));
            certFile = ssc.certificate();
            keyFile = ssc.privateKey();
        } catch (CertificateException e) {
            final String msg = String.format(Locale.ENGLISH,
                    "Problem creating a self-signed certificate for input [%s/%s].", input.getName(),
                    input.getId());
            throw new IllegalStateException(msg, e);
        }
    }

    final ClientAuth clientAuth;
    switch (tlsClientAuth) {
    case TLS_CLIENT_AUTH_DISABLED:
        LOG.debug("Not using TLS client authentication");
        clientAuth = ClientAuth.NONE;
        break;
    case TLS_CLIENT_AUTH_OPTIONAL:
        LOG.debug("Using optional TLS client authentication");
        clientAuth = ClientAuth.OPTIONAL;
        break;
    case TLS_CLIENT_AUTH_REQUIRED:
        LOG.debug("Using mandatory TLS client authentication");
        clientAuth = ClientAuth.REQUIRE;
        break;
    default:
        throw new IllegalArgumentException("Unknown TLS client authentication mode: " + tlsClientAuth);
    }

    return buildSslHandlerCallable(nettyTransportConfiguration.getTlsProvider(), certFile, keyFile,
            tlsKeyPassword, clientAuth, tlsClientAuthCertFile);
}

From source file:org.jocean.http.server.HttpTestServer.java

License:Apache License

public HttpTestServer(final boolean enableSSL, final SocketAddress localAddress, final EventLoopGroup bossGroup,
        final EventLoopGroup workerGroup, final Class<? extends ServerChannel> serverChannelType,
        final Callable<ChannelInboundHandler> newHandler) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (enableSSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {//from w ww .j a  va2s .c  o  m
        sslCtx = null;
    }

    // Configure the server.
    _bossGroup = bossGroup;
    _workerGroup = workerGroup;

    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(_bossGroup, _workerGroup).channel(serverChannelType).handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new HttpTestServerInitializer(sslCtx, newHandler));

    b.bind(localAddress).sync();
}

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

License:Apache License

public NettyTransportContext() {
    super();/*from  w  w 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.neo4j.bolt.security.ssl.TestSslCertificateFactory.java

License:Open Source License

@Test
public void shouldLoadPEMPrivateKey() throws Throwable {
    // Given//from  w  ww .j a v a 2 s.  co m
    SelfSignedCertificate cert = new SelfSignedCertificate("example.com");
    Certificates certs = new Certificates();

    File privateKey = cert.privateKey();

    // When
    PrivateKey pk = certs.loadPrivateKey(privateKey);

    // Then
    assertNotNull(pk);
}

From source file:org.neo4j.bolt.security.ssl.TestSslCertificateFactory.java

License:Open Source License

/**
 * For backwards-compatibility reasons, we support both PEM-encoded private keys *and* raw binary files containing
 * the private key data/* www.ja  v  a 2s.  c  o  m*/
 *
 * @throws Throwable
 */
@Test
public void shouldLoadBinaryPrivateKey() throws Throwable {
    // Given
    SelfSignedCertificate cert = new SelfSignedCertificate("example.com");
    Certificates certs = new Certificates();

    File keyFile = tmpDir.newFile("certificate");
    byte[] raw = certs.loadPrivateKey(cert.privateKey()).getEncoded();

    try (FileChannel ch = FileChannel.open(keyFile.toPath(), WRITE)) {
        FileUtils.writeAll(ch, ByteBuffer.wrap(raw));
    }

    // When
    PrivateKey pk = certs.loadPrivateKey(keyFile);

    // Then
    assertNotNull(pk);
}

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 va 2  s .  com
    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) {/*from  ww  w .j a  v a 2 s  .com*/
        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  .ja va 2s .  c  om*/
                    .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.robotbrains.support.web.server.netty.NettyWebServer.java

License:Apache License

@Override
public void startup() {
    try {/*from   w w  w.j  a  va 2s.  co  m*/
        // 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");
    }
}