- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.ByteToMessageDecoder
-
- io.netty5.handler.ssl.SslHandler
-
- All Implemented Interfaces:
io.netty5.channel.ChannelHandler
public class SslHandler extends io.netty5.handler.codec.ByteToMessageDecoderAdds SSL · TLS and StartTLS support to aChannel. Please refer to the "SecureChat" example in the distribution or the web site for the detailed usage.Beginning the handshake
Beside using the handshake
Futureto get notified about the completion of the handshake it's also possible to detect it by implement theChannelHandler.channelInboundEvent(ChannelHandlerContext, Object)method and check for aSslHandshakeCompletionEvent.Handshake
The handshake will be automatically issued for you once the
Channelis active andSSLEngine.getUseClientMode()returnstrue. So no need to bother with it by your self.Closing the session
To close the SSL session, the
closeOutbound()method should be called to send theclose_notifymessage to the remote peer. One exception is when you close theChannel-SslHandlerintercepts the close request and send theclose_notifymessage before the channel closure automatically. Once the SSL session is closed, it is not reusable, and consequently you should create a newSslHandlerwith a newSSLEngineas explained in the following section.Restarting the session
To restart the SSL session, you must remove the existing closed
SslHandlerfrom theChannelPipeline, insert a newSslHandlerwith a newSSLEngineinto the pipeline, and start the handshake process as described in the first section.Implementing StartTLS
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:
- Client sends a StartTLS request to server.
- Server sends a StartTLS response to client.
- Client begins SSL handshake.
- create a new
SslHandlerinstance withstartTlsflag set totrue, - insert the
SslHandlerto theChannelPipeline, and - write a StartTLS response.
SslHandlerbefore sending the StartTLS response. Otherwise the client can send begin SSL handshake beforeSslHandleris inserted to theChannelPipeline, causing data corruption.The client-side implementation is much simpler.
- Write a StartTLS request,
- wait for the StartTLS response,
- create a new
SslHandlerinstance withstartTlsflag set tofalse, - insert the
SslHandlerto theChannelPipeline, and - Initiate SSL handshake.
Known issues
Because 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.
-
-
Constructor Summary
Constructors Constructor Description SslHandler(SSLEngine engine)Creates a new instance which runs all delegated tasks directly on theEventExecutor.SslHandler(SSLEngine engine, boolean startTls)Creates a new instance which runs all delegated tasks directly on theEventExecutor.SslHandler(SSLEngine engine, boolean startTls, Executor delegatedTaskExecutor)Creates a new instance.SslHandler(SSLEngine engine, Executor delegatedTaskExecutor)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description StringapplicationProtocol()Returns the name of the current application-level protocol.voidchannelActive(io.netty5.channel.ChannelHandlerContext ctx)Issues an initial TLS handshake once connected when used in client-modevoidchannelExceptionCaught(io.netty5.channel.ChannelHandlerContext ctx, Throwable cause)voidchannelInactive(io.netty5.channel.ChannelHandlerContext ctx)voidchannelReadComplete(io.netty5.channel.ChannelHandlerContext ctx)io.netty5.util.concurrent.Future<Void>close(io.netty5.channel.ChannelHandlerContext ctx)io.netty5.util.concurrent.Future<Void>closeOutbound()Sends an SSLclose_notifymessage to the specified channel and destroys the underlyingSSLEngine.protected voiddecode(io.netty5.channel.ChannelHandlerContext ctx, io.netty5.buffer.Buffer in)io.netty5.util.concurrent.Future<Void>disconnect(io.netty5.channel.ChannelHandlerContext ctx)SSLEngineengine()Returns theSSLEnginewhich is used by this handler.voidflush(io.netty5.channel.ChannelHandlerContext ctx)longgetCloseNotifyFlushTimeoutMillis()Gets the timeout for flushing the close_notify that was triggered by closing theChannel.longgetCloseNotifyReadTimeoutMillis()Gets the timeout (in ms) for receiving the response for the close_notify that was triggered by closing theChannel.longgetCloseNotifyTimeoutMillis()Deprecated.longgetHandshakeTimeoutMillis()voidhandlerAdded0(io.netty5.channel.ChannelHandlerContext ctx)voidhandlerRemoved0(io.netty5.channel.ChannelHandlerContext ctx)io.netty5.util.concurrent.Future<io.netty5.channel.Channel>handshakeFuture()Returns aFuturethat will get notified once the current TLS handshake completes.static booleanisEncrypted(io.netty5.buffer.Buffer buffer)Returnstrueif the givenBufferis encrypted.longpendingOutboundBytes(io.netty5.channel.ChannelHandlerContext ctx)voidread(io.netty5.channel.ChannelHandlerContext ctx, io.netty5.channel.ReadBufferAllocator readBufferAllocator)io.netty5.util.concurrent.Future<io.netty5.channel.Channel>renegotiate()Performs TLS renegotiation.io.netty5.util.concurrent.Future<io.netty5.channel.Channel>renegotiate(io.netty5.util.concurrent.Promise<io.netty5.channel.Channel> promise)Performs TLS renegotiation.voidsetCloseNotifyFlushTimeout(long closeNotifyFlushTimeout, TimeUnit unit)Sets the timeout for flushing the close_notify that was triggered by closing theChannel.voidsetCloseNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis)voidsetCloseNotifyReadTimeout(long closeNotifyReadTimeout, TimeUnit unit)Sets the timeout for receiving the response for the close_notify that was triggered by closing theChannel.voidsetCloseNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis)voidsetCloseNotifyTimeout(long closeNotifyTimeout, TimeUnit unit)Deprecated.voidsetCloseNotifyTimeoutMillis(long closeNotifyFlushTimeoutMillis)Deprecated.voidsetHandshakeTimeout(long handshakeTimeout, TimeUnit unit)voidsetHandshakeTimeoutMillis(long handshakeTimeoutMillis)voidsetWrapDataSize(int wrapDataSize)Sets the number of bytes to pass to eachSSLEngine.wrap(ByteBuffer[], int, int, ByteBuffer)call.io.netty5.util.concurrent.Future<io.netty5.channel.Channel>sslCloseFuture()Return theFuturethat will get notified if the inbound of theSSLEngineis closed.io.netty5.util.concurrent.Future<Void>write(io.netty5.channel.ChannelHandlerContext ctx, Object msg)-
Methods inherited from class io.netty5.handler.codec.ByteToMessageDecoder
actualReadableBytes, channelRead, channelShutdown, decodeLast, discardSomeReadBytes, handlerAdded, handlerRemoved, internalBuffer, isSharable, isSingleDecode, setSingleDecode
-
-
-
-
Constructor Detail
-
SslHandler
public SslHandler(SSLEngine engine)
Creates a new instance which runs all delegated tasks directly on theEventExecutor.- Parameters:
engine- theSSLEnginethis handler will use
-
SslHandler
public SslHandler(SSLEngine engine, boolean startTls)
Creates a new instance which runs all delegated tasks directly on theEventExecutor.
-
SslHandler
public SslHandler(SSLEngine engine, Executor delegatedTaskExecutor)
Creates a new instance.- Parameters:
engine- theSSLEnginethis handler will usedelegatedTaskExecutor- theExecutorthat will be used to execute tasks that are returned bySSLEngine.getDelegatedTask().
-
SslHandler
public SslHandler(SSLEngine engine, boolean startTls, Executor delegatedTaskExecutor)
Creates a new instance.- Parameters:
engine- theSSLEnginethis handler will usestartTls-trueif the first write request shouldn't be encrypted by theSSLEnginedelegatedTaskExecutor- theExecutorthat will be used to execute tasks that are returned bySSLEngine.getDelegatedTask().
-
-
Method Detail
-
getHandshakeTimeoutMillis
public long getHandshakeTimeoutMillis()
-
setHandshakeTimeout
public void setHandshakeTimeout(long handshakeTimeout, TimeUnit unit)
-
setHandshakeTimeoutMillis
public void setHandshakeTimeoutMillis(long handshakeTimeoutMillis)
-
setWrapDataSize
@UnstableApi public final void setWrapDataSize(int wrapDataSize)
Sets the number of bytes to pass to eachSSLEngine.wrap(ByteBuffer[], int, int, ByteBuffer)call.This value will partition data which is passed to write
write(ChannelHandlerContext, Object). The partitioning will work as follows:- If
wrapDataSize <= 0then we will write each data chunk as is. - If
wrapDataSize > data sizethen we will attempt to aggregate multiple data chunks together. - If
wrapDataSize > data sizeElse ifwrapDataSize <= data sizethen we will divide the data into chunks ofwrapDataSizewhen writing.
If the
SSLEnginedoesn't support a gather wrap operation (e.g.SslProvider.OPENSSL) then aggregating data before wrapping can help reduce the ratio between TLS overhead vs data payload which will lead to better goodput. Writing fixed chunks of data can also help target the underlying transport's (e.g. TCP) frame size. Under lossy/congested network conditions this may help the peer get full TLS packets earlier and be able to do work sooner, as opposed to waiting for the all the pieces of the TLS packet to arrive.- Parameters:
wrapDataSize- the number of bytes which will be passed to eachSSLEngine.wrap(ByteBuffer[], int, int, ByteBuffer)call.
- If
-
getCloseNotifyTimeoutMillis
@Deprecated public long getCloseNotifyTimeoutMillis()
Deprecated.
-
setCloseNotifyTimeout
@Deprecated public void setCloseNotifyTimeout(long closeNotifyTimeout, TimeUnit unit)
Deprecated.
-
setCloseNotifyTimeoutMillis
@Deprecated public void setCloseNotifyTimeoutMillis(long closeNotifyFlushTimeoutMillis)
Deprecated.
-
getCloseNotifyFlushTimeoutMillis
public final long getCloseNotifyFlushTimeoutMillis()
Gets the timeout for flushing the close_notify that was triggered by closing theChannel. If the close_notify was not flushed in the given timeout theChannelwill be closed forcibly.
-
setCloseNotifyFlushTimeout
public final void setCloseNotifyFlushTimeout(long closeNotifyFlushTimeout, TimeUnit unit)Sets the timeout for flushing the close_notify that was triggered by closing theChannel. If the close_notify was not flushed in the given timeout theChannelwill be closed forcibly.
-
setCloseNotifyFlushTimeoutMillis
public final void setCloseNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis)
-
getCloseNotifyReadTimeoutMillis
public final long getCloseNotifyReadTimeoutMillis()
Gets the timeout (in ms) for receiving the response for the close_notify that was triggered by closing theChannel. This timeout starts after the close_notify message was successfully written to the remote peer. Use0to directly close theChanneland not wait for the response.
-
setCloseNotifyReadTimeout
public final void setCloseNotifyReadTimeout(long closeNotifyReadTimeout, TimeUnit unit)Sets the timeout for receiving the response for the close_notify that was triggered by closing theChannel. This timeout starts after the close_notify message was successfully written to the remote peer. Use0to directly close theChanneland not wait for the response.
-
setCloseNotifyReadTimeoutMillis
public final void setCloseNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis)
-
applicationProtocol
public String applicationProtocol()
Returns the name of the current application-level protocol.- Returns:
- the protocol name or
nullif application-level protocol has not been negotiated
-
handshakeFuture
public io.netty5.util.concurrent.Future<io.netty5.channel.Channel> handshakeFuture()
Returns aFuturethat will get notified once the current TLS handshake completes.- Returns:
- the
Futurefor the initial TLS handshake ifrenegotiate()was not invoked. TheFuturefor the most recent TLS renegotiation otherwise.
-
closeOutbound
public io.netty5.util.concurrent.Future<Void> closeOutbound()
Sends an SSLclose_notifymessage to the specified channel and destroys the underlyingSSLEngine. This will not close the underlyingChannel. If you want to also close theChanneluseChannel.close()orChannelOutboundInvoker.close()
-
sslCloseFuture
public io.netty5.util.concurrent.Future<io.netty5.channel.Channel> sslCloseFuture()
Return theFuturethat will get notified if the inbound of theSSLEngineis closed. This method will return the sameFutureall the time.- See Also:
SSLEngine
-
handlerRemoved0
public void handlerRemoved0(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Overrides:
handlerRemoved0in classio.netty5.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
disconnect
public io.netty5.util.concurrent.Future<Void> disconnect(io.netty5.channel.ChannelHandlerContext ctx)
-
close
public io.netty5.util.concurrent.Future<Void> close(io.netty5.channel.ChannelHandlerContext ctx)
-
read
public void read(io.netty5.channel.ChannelHandlerContext ctx, io.netty5.channel.ReadBufferAllocator readBufferAllocator)
-
write
public io.netty5.util.concurrent.Future<Void> write(io.netty5.channel.ChannelHandlerContext ctx, Object msg)
-
flush
public void flush(io.netty5.channel.ChannelHandlerContext ctx)
-
channelInactive
public void channelInactive(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelInactivein interfaceio.netty5.channel.ChannelHandler- Overrides:
channelInactivein classio.netty5.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
channelExceptionCaught
public void channelExceptionCaught(io.netty5.channel.ChannelHandlerContext ctx, Throwable cause) throws Exception- Throws:
Exception
-
isEncrypted
public static boolean isEncrypted(io.netty5.buffer.Buffer buffer)
Returnstrueif the givenBufferis encrypted. Be aware that this method will not increase the readerIndex of the givenBuffer.- Parameters:
buffer- TheBufferto read from. Be aware that it must have at least 5 bytes to read, otherwise it will throw anIllegalArgumentException.- Returns:
- encrypted
trueif theBufferis encrypted,falseotherwise. - Throws:
IllegalArgumentException- Is thrown if the givenBufferhas not at least 5 bytes to read.
-
decode
protected void decode(io.netty5.channel.ChannelHandlerContext ctx, io.netty5.buffer.Buffer in) throws SSLException- Specified by:
decodein classio.netty5.handler.codec.ByteToMessageDecoder- Throws:
SSLException
-
channelReadComplete
public void channelReadComplete(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Specified by:
channelReadCompletein interfaceio.netty5.channel.ChannelHandler- Overrides:
channelReadCompletein classio.netty5.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
handlerAdded0
public void handlerAdded0(io.netty5.channel.ChannelHandlerContext ctx) throws Exception- Overrides:
handlerAdded0in classio.netty5.handler.codec.ByteToMessageDecoder- Throws:
Exception
-
renegotiate
public io.netty5.util.concurrent.Future<io.netty5.channel.Channel> renegotiate()
Performs TLS renegotiation.
-
renegotiate
public io.netty5.util.concurrent.Future<io.netty5.channel.Channel> renegotiate(io.netty5.util.concurrent.Promise<io.netty5.channel.Channel> promise)
Performs TLS renegotiation.
-
channelActive
public void channelActive(io.netty5.channel.ChannelHandlerContext ctx) throws ExceptionIssues an initial TLS handshake once connected when used in client-mode- Throws:
Exception
-
pendingOutboundBytes
public long pendingOutboundBytes(io.netty5.channel.ChannelHandlerContext ctx)
-
-