public class SslHandler extends ByteToMessageDecoder implements ChannelOutboundHandler
Channel
. Please refer
to the "SecureChat" example in the distribution or the web
site for the detailed usage.
Beside using the handshake ChannelFuture
to get notified about the completion of the handshake it's
also possible to detect it by implement the
ChannelInboundHandler.userEventTriggered(ChannelHandlerContext, Object)
method and check for a SslHandshakeCompletionEvent
.
The handshake will be automatically issued for you once the Channel
is active and
SSLEngine.getUseClientMode()
returns true
.
So no need to bother with it by your self.
To close the SSL session, the close()
method should be
called to send the close_notify
message to the remote peer. One
exception is when you close the Channel
- SslHandler
intercepts the close request and send the close_notify
message
before the channel closure automatically. Once the SSL session is closed,
it is not reusable, and consequently you should create a new
SslHandler
with a new SSLEngine
as explained in the
following section.
To restart the SSL session, you must remove the existing closed
SslHandler
from the ChannelPipeline
, insert a new
SslHandler
with a new SSLEngine
into the pipeline,
and start the handshake process as described in the first section.
StartTLS is the communication pattern that secures the wire in the middle of the plaintext connection. Please note that it is different from SSL · TLS, that secures the wire from the beginning of the connection. Typically, StartTLS is composed of three steps:
SslHandler
instance with startTls
flag set
to true
,SslHandler
to the ChannelPipeline
, andSslHandler
before sending
the StartTLS response. Otherwise the client can send begin SSL handshake
before SslHandler
is inserted to the ChannelPipeline
, causing
data corruption.
The client-side implementation is much simpler.
SslHandler
instance with startTls
flag set
to false
,SslHandler
to the ChannelPipeline
, andBecause of a known issue with the current implementation of the SslEngine that comes with Java it may be possible that you see blocked IO-Threads while a full GC is done.
So if you are affected you can workaround this problem by adjust the cache settings like shown below:
SslContext context = ...; context.getServerSessionContext().setSessionCacheSize(someSaneSize); context.getServerSessionContext().setSessionTime(someSameTimeout);
What values to use here depends on the nature of your application and should be set based on monitoring and debugging of it. For more details see #832 in our issue tracker.
ByteToMessageDecoder.Cumulator
ChannelHandler.Sharable
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
Constructor and Description |
---|
SslHandler(SSLEngine engine)
Creates a new instance.
|
SslHandler(SSLEngine engine,
boolean startTls)
Creates a new instance.
|
SslHandler(SSLEngine engine,
boolean startTls,
Executor delegatedTaskExecutor)
Deprecated.
Use
SslHandler(SSLEngine, boolean) instead. |
SslHandler(SSLEngine engine,
Executor delegatedTaskExecutor)
Deprecated.
Use
SslHandler(SSLEngine) instead. |
Modifier and Type | Method and Description |
---|---|
String |
applicationProtocol()
Returns the name of the current application-level protocol.
|
void |
bind(ChannelHandlerContext ctx,
SocketAddress localAddress,
ChannelPromise promise)
Called once a bind operation is made.
|
void |
channelActive(ChannelHandlerContext ctx)
Issues an initial TLS handshake once connected when used in client-mode
|
void |
channelInactive(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelInactive() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelReadComplete(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelReadComplete() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
ChannelFuture |
close()
Deprecated.
|
void |
close(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a close operation is made.
|
ChannelFuture |
close(ChannelPromise promise)
Deprecated.
|
void |
connect(ChannelHandlerContext ctx,
SocketAddress remoteAddress,
SocketAddress localAddress,
ChannelPromise promise)
Called once a connect operation is made.
|
protected void |
decode(ChannelHandlerContext ctx,
ByteBuf in,
List<Object> out)
Decode the from one
ByteBuf to an other. |
void |
deregister(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a deregister operation is made from the current registered
EventLoop . |
void |
disconnect(ChannelHandlerContext ctx,
ChannelPromise promise)
Called once a disconnect operation is made.
|
SSLEngine |
engine()
Returns the
SSLEngine which is used by this handler. |
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause)
Calls
ChannelHandlerContext.fireExceptionCaught(Throwable) to forward
to the next ChannelHandler in the ChannelPipeline . |
void |
flush(ChannelHandlerContext ctx)
Called once a flush operation is made.
|
long |
getCloseNotifyFlushTimeoutMillis()
Gets the timeout for flushing the close_notify that was triggered by closing the
Channel . |
long |
getCloseNotifyReadTimeoutMillis()
Gets the timeout (in ms) for receiving the response for the close_notify that was triggered by closing the
Channel . |
long |
getCloseNotifyTimeoutMillis()
Deprecated.
|
long |
getHandshakeTimeoutMillis() |
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
handlerRemoved0(ChannelHandlerContext ctx)
Gets called after the
ByteToMessageDecoder was removed from the actual context and it doesn't handle
events anymore. |
Future<Channel> |
handshakeFuture()
Returns a
Future that will get notified once the current TLS handshake completes. |
static boolean |
isEncrypted(ByteBuf buffer)
Returns
true if the given ByteBuf is encrypted. |
void |
read(ChannelHandlerContext ctx)
Intercepts
ChannelHandlerContext.read() . |
Future<Channel> |
renegotiate()
Performs TLS renegotiation.
|
Future<Channel> |
renegotiate(Promise<Channel> promise)
Performs TLS renegotiation.
|
void |
setCloseNotifyFlushTimeout(long closeNotifyFlushTimeout,
TimeUnit unit)
Sets the timeout for flushing the close_notify that was triggered by closing the
Channel . |
void |
setCloseNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis)
|
void |
setCloseNotifyReadTimeout(long closeNotifyReadTimeout,
TimeUnit unit)
Sets the timeout for receiving the response for the close_notify that was triggered by closing the
Channel . |
void |
setCloseNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis)
|
void |
setCloseNotifyTimeout(long closeNotifyTimeout,
TimeUnit unit)
Deprecated.
|
void |
setCloseNotifyTimeoutMillis(long closeNotifyFlushTimeoutMillis)
Deprecated.
|
void |
setHandshakeTimeout(long handshakeTimeout,
TimeUnit unit) |
void |
setHandshakeTimeoutMillis(long handshakeTimeoutMillis) |
Future<Channel> |
sslCloseFuture()
|
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise)
Called once a write operation is made.
|
actualReadableBytes, callDecode, channelRead, decodeLast, discardSomeReadBytes, handlerRemoved, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
channelRegistered, channelUnregistered, channelWritabilityChanged
ensureNotSharable, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerRemoved
public SslHandler(SSLEngine engine)
engine
- the SSLEngine
this handler will usepublic SslHandler(SSLEngine engine, boolean startTls)
@Deprecated public SslHandler(SSLEngine engine, Executor delegatedTaskExecutor)
SslHandler(SSLEngine)
instead.@Deprecated public SslHandler(SSLEngine engine, boolean startTls, Executor delegatedTaskExecutor)
SslHandler(SSLEngine, boolean)
instead.public long getHandshakeTimeoutMillis()
public void setHandshakeTimeout(long handshakeTimeout, TimeUnit unit)
public void setHandshakeTimeoutMillis(long handshakeTimeoutMillis)
@Deprecated public long getCloseNotifyTimeoutMillis()
getCloseNotifyFlushTimeoutMillis()
@Deprecated public void setCloseNotifyTimeout(long closeNotifyTimeout, TimeUnit unit)
setCloseNotifyFlushTimeout(long, TimeUnit)
@Deprecated public void setCloseNotifyTimeoutMillis(long closeNotifyFlushTimeoutMillis)
setCloseNotifyFlushTimeoutMillis(long)
public final long getCloseNotifyFlushTimeoutMillis()
public final void setCloseNotifyFlushTimeout(long closeNotifyFlushTimeout, TimeUnit unit)
public final void setCloseNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis)
public final long getCloseNotifyReadTimeoutMillis()
public final void setCloseNotifyReadTimeout(long closeNotifyReadTimeout, TimeUnit unit)
public final void setCloseNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis)
public String applicationProtocol()
null
if application-level protocol has not been negotiatedpublic Future<Channel> handshakeFuture()
Future
that will get notified once the current TLS handshake completes.Future
for the initial TLS handshake if renegotiate()
was not invoked.
The Future
for the most recent TLS renegotiation otherwise.@Deprecated public ChannelFuture close()
ChannelOutboundInvoker.close()
or ChannelOutboundInvoker.close()
close_notify
message to the specified channel and
destroys the underlying SSLEngine
.@Deprecated public ChannelFuture close(ChannelPromise promise)
ChannelOutboundInvoker.close()
or ChannelOutboundInvoker.close()
close()
public Future<Channel> sslCloseFuture()
Future
that will get notified if the inbound of the SSLEngine
is closed.
This method will return the same Future
all the time.SSLEngine
public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception
ByteToMessageDecoder
ByteToMessageDecoder
was removed from the actual context and it doesn't handle
events anymore.handlerRemoved0
in class ByteToMessageDecoder
Exception
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception
ChannelOutboundHandler
bind
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the bind operation is madelocalAddress
- the SocketAddress
to which it should boundpromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception
ChannelOutboundHandler
connect
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the connect operation is maderemoteAddress
- the SocketAddress
to which it should connectlocalAddress
- the SocketAddress
which is used as source on connectpromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandler
EventLoop
.deregister
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the close operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandler
disconnect
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the disconnect operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception
ChannelOutboundHandler
close
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the close operation is madepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void read(ChannelHandlerContext ctx) throws Exception
ChannelOutboundHandler
ChannelHandlerContext.read()
.read
in interface ChannelOutboundHandler
Exception
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelOutboundHandler
ChannelPipeline
. Those are then ready to be flushed to the actual Channel
once
Channel.flush()
is calledwrite
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occurspublic void flush(ChannelHandlerContext ctx) throws Exception
ChannelOutboundHandler
flush
in interface ChannelOutboundHandler
ctx
- the ChannelHandlerContext
for which the flush operation is madeException
- thrown if an error occurspublic void channelInactive(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelInactive()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelInactive
in interface ChannelInboundHandler
channelInactive
in class ByteToMessageDecoder
Exception
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireExceptionCaught(Throwable)
to forward
to the next ChannelHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.exceptionCaught
in interface ChannelHandler
exceptionCaught
in interface ChannelInboundHandler
exceptionCaught
in class ChannelInboundHandlerAdapter
Exception
public static boolean isEncrypted(ByteBuf buffer)
true
if the given ByteBuf
is encrypted. Be aware that this method
will not increase the readerIndex of the given ByteBuf
.buffer
- The ByteBuf
to read from. Be aware that it must have at least 5 bytes to read,
otherwise it will throw an IllegalArgumentException
.true
if the ByteBuf
is encrypted, false
otherwise.IllegalArgumentException
- Is thrown if the given ByteBuf
has not at least 5 bytes to read.protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws SSLException
ByteToMessageDecoder
ByteBuf
to an other. This method will be called till either the input
ByteBuf
has nothing to read when return from this method or till nothing was read from the input
ByteBuf
.decode
in class ByteToMessageDecoder
ctx
- the ChannelHandlerContext
which this ByteToMessageDecoder
belongs toin
- the ByteBuf
from which to read dataout
- the List
to which decoded messages should be addedSSLException
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelReadComplete()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelReadComplete
in interface ChannelInboundHandler
channelReadComplete
in class ByteToMessageDecoder
Exception
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public Future<Channel> renegotiate(Promise<Channel> promise)
public void channelActive(ChannelHandlerContext ctx) throws Exception
channelActive
in interface ChannelInboundHandler
channelActive
in class ChannelInboundHandlerAdapter
Exception
Copyright © 2008–2017 The Netty Project. All rights reserved.