use of io.netty.handler.ssl.util.SelfSignedCertificate in project neo4j by neo4j.
the class TestSslCertificateFactory method shouldLoadBinaryCertificates.
/**
* For backwards-compatibility reasons, we support both PEM-encoded certificates *and* raw binary files containing
* the certificate data.
*
* @throws Throwable
*/
@Test
public void shouldLoadBinaryCertificates() throws Throwable {
// Given
SelfSignedCertificate cert = new SelfSignedCertificate("example.com");
Certificates certs = new Certificates();
File cPath = tmpDir.newFile("certificate");
byte[] raw = certs.loadCertificates(cert.certificate())[0].getEncoded();
try (FileChannel ch = FileChannel.open(cPath.toPath(), WRITE)) {
FileUtils.writeAll(ch, ByteBuffer.wrap(raw));
}
// When
Certificate[] certificates = certs.loadCertificates(cPath);
// Then
assertThat(certificates.length, equalTo(1));
}
use of io.netty.handler.ssl.util.SelfSignedCertificate in project neo4j by neo4j.
the class TestSslCertificateFactory method shouldLoadPEMCertificates.
@Test
public void shouldLoadPEMCertificates() throws Throwable {
// Given
SelfSignedCertificate cert = new SelfSignedCertificate("example.com");
Certificates certs = new Certificates();
File pemCertificate = cert.certificate();
// When
Certificate[] certificates = certs.loadCertificates(pemCertificate);
// Then
assertThat(certificates.length, equalTo(1));
}
use of io.netty.handler.ssl.util.SelfSignedCertificate in project neo4j by neo4j.
the class TestSslCertificateFactory method shouldLoadBinaryPrivateKey.
/**
* For backwards-compatibility reasons, we support both PEM-encoded private keys *and* raw binary files containing
* the private key data
*
* @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);
}
use of io.netty.handler.ssl.util.SelfSignedCertificate in project netty by netty.
the class ObjectEchoServer method main.
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
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 ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoServerHandler());
}
});
// Bind and start to accept incoming connections.
b.bind(PORT).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
use of io.netty.handler.ssl.util.SelfSignedCertificate in project netty by netty.
the class Http2Server method main.
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
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, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)).build();
} else {
sslCtx = null;
}
// Configure the server.
EventLoopGroup group = new NioEventLoopGroup();
try {
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));
Channel ch = b.bind(PORT).sync().channel();
System.err.println("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/');
ch.closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
Aggregations