use of javax.net.ssl.SSLEngine in project pravega by pravega.
the class ConnectionFactoryImplTest method setUp.
@Before
public void setUp() throws Exception {
// Configure SSL.
port = TestUtils.getAvailableListenPort();
final SslContext sslCtx;
if (ssl) {
try {
sslCtx = SslContextBuilder.forServer(new File("../config/cert.pem"), new File("../config/key.pem")).build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
} else {
sslCtx = null;
}
boolean nio = false;
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
try {
bossGroup = new EpollEventLoopGroup(1);
workerGroup = new EpollEventLoopGroup();
} catch (ExceptionInInitializerError | UnsatisfiedLinkError | NoClassDefFoundError e) {
nio = true;
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(nio ? NioServerSocketChannel.class : EpollServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
SslHandler handler = sslCtx.newHandler(ch.alloc());
SSLEngine sslEngine = handler.engine();
SSLParameters sslParameters = sslEngine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("LDAPS");
sslEngine.setSSLParameters(sslParameters);
p.addLast(handler);
}
}
});
// Start the server.
serverChannel = b.bind("localhost", port).awaitUninterruptibly().channel();
}
use of javax.net.ssl.SSLEngine in project baseio by generallycloud.
the class SslHandler method wrap.
public ByteBuf wrap(SocketChannel channel, ByteBuf src) throws IOException {
SSLEngine engine = channel.getSSLEngine();
ByteBuf dst = getTempDst(engine);
ByteBuf out = null;
try {
for (; ; ) {
dst.clear();
SSLEngineResult result = engine.wrap(src.nioBuffer(), dst.nioBuffer());
Status status = result.getStatus();
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
synchByteBuf(result, src, dst);
if (status == Status.CLOSED) {
return gc(channel, dst.flip());
}
if (handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
if (out != null) {
out.read(dst.flip());
return out.flip();
}
return gc(channel, dst.flip());
} else if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
if (out == null) {
out = allocate(channel, 256);
}
out.read(dst.flip());
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
channel.finishHandshake(null);
out.read(dst.flip());
return out.flip();
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
runDelegatedTasks(engine);
continue;
}
}
if (src.hasRemaining()) {
if (out == null) {
int outLength = ((src.limit() / src.position()) + 1) * (dst.position() - src.position()) + src.limit();
out = allocate(channel, outLength);
}
out.read(dst.flip());
continue;
}
if (out != null) {
out.read(dst.flip());
return out.flip();
}
return gc(channel, dst.flip());
}
} catch (Throwable e) {
ReleaseUtil.release(out);
if (e instanceof IOException) {
throw (IOException) e;
}
throw new IOException(e);
}
}
use of javax.net.ssl.SSLEngine in project baseio by generallycloud.
the class SslHandler method unwrap.
public ByteBuf unwrap(SocketChannel channel, ByteBuf src) throws IOException {
SSLEngine sslEngine = channel.getSSLEngine();
ByteBuf dst = getTempDst(sslEngine);
for (; ; ) {
dst.clear();
SSLEngineResult result = sslEngine.unwrap(src.nioBuffer(), dst.nioBuffer());
HandshakeStatus handshakeStatus = result.getHandshakeStatus();
synchByteBuf(result, src, dst);
if (handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
channel.doFlush(forgeFuture.duplicate());
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_TASK) {
runDelegatedTasks(sslEngine);
continue;
} else if (handshakeStatus == HandshakeStatus.FINISHED) {
channel.finishHandshake(null);
return null;
} else if (handshakeStatus == HandshakeStatus.NEED_UNWRAP) {
return null;
}
}
return dst.flip();
}
}
use of javax.net.ssl.SSLEngine in project smscgateway by RestComm.
the class TestSmppClient method createSession.
protected DefaultSmppSession createSession(Channel channel, SmppSessionConfiguration config, SmppSessionHandler sessionHandler) throws SmppTimeoutException, SmppChannelException, InterruptedException {
TestSmppSession session = new TestSmppSession(SmppSession.Type.CLIENT, config, channel, sessionHandler, monitorExecutor);
// add SSL handler
if (config.isUseSsl()) {
SslConfiguration sslConfig = config.getSslConfiguration();
if (sslConfig == null)
throw new IllegalStateException("sslConfiguration must be set");
try {
SslContextFactory factory = new SslContextFactory(sslConfig);
SSLEngine sslEngine = factory.newSslEngine();
sslEngine.setUseClientMode(true);
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_SSL_NAME, new SslHandler(sslEngine));
} catch (Exception e) {
throw new SmppChannelConnectException("Unable to create SSL session]: " + e.getMessage(), e);
}
}
// add the thread renamer portion to the pipeline
if (config.getName() != null) {
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_THREAD_RENAMER_NAME, new SmppSessionThreadRenamer(config.getName()));
} else {
// logger.warn("Session configuration did not have a name set - skipping threadRenamer in pipeline");
}
// create the logging handler (for bytes sent/received on wire)
SmppSessionLogger loggingHandler = new SmppSessionLogger(DefaultSmppSession.class.getCanonicalName(), config.getLoggingOptions());
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_LOGGER_NAME, loggingHandler);
// add a writeTimeout handler after the logger
if (config.getWriteTimeout() > 0) {
WriteTimeoutHandler writeTimeoutHandler = new WriteTimeoutHandler(new org.jboss.netty.util.HashedWheelTimer(), /* writeTimeoutTimer */
config.getWriteTimeout(), TimeUnit.MILLISECONDS);
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_WRITE_TIMEOUT_NAME, writeTimeoutHandler);
}
// add a new instance of a decoder (that takes care of handling frames)
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_PDU_DECODER_NAME, new SmppSessionPduDecoder(session.getTranscoder()));
// create a new wrapper around a session to pass the pdu up the chain
channel.getPipeline().addLast(SmppChannelConstants.PIPELINE_SESSION_WRAPPER_NAME, new SmppSessionWrapper(session));
return session;
}
use of javax.net.ssl.SSLEngine in project incubator-servicecomb-java-chassis by apache.
the class SSLManager method createSSLEngine.
public static SSLEngine createSSLEngine(SSLOption option, SSLCustom custom, String peerHost, int peerPort) {
SSLContext context = createSSLContext(option, custom);
SSLEngine engine = context.createSSLEngine(peerHost, peerPort);
engine.setEnabledProtocols(option.getProtocols().split(","));
String[] supported = engine.getSupportedCipherSuites();
String[] eanbled = option.getCiphers().split(",");
engine.setEnabledCipherSuites(getEnabledCiphers(supported, eanbled));
engine.setNeedClientAuth(option.isAuthPeer());
return engine;
}
Aggregations