Package org.apache.tomcat.util.net
Class SecureNioChannel
- java.lang.Object
-
- org.apache.tomcat.util.net.NioChannel
-
- org.apache.tomcat.util.net.SecureNioChannel
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.ByteChannel,java.nio.channels.Channel,java.nio.channels.GatheringByteChannel,java.nio.channels.ReadableByteChannel,java.nio.channels.ScatteringByteChannel,java.nio.channels.WritableByteChannel
public class SecureNioChannel extends NioChannel
Implementation of a secure socket channel
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanclosedTrue when the channel is closedprotected booleanclosingTrue when the channel is in the process of closingprotected booleanhandshakeCompleteTrue when the SSL handshake is completeprotected javax.net.ssl.SSLEngineResult.HandshakeStatushandshakeStatusCurrent handshake status, updated during handshake processingprotected booleanneedHandshakeWrapTrue when a handshake wrap is pendingprotected java.nio.ByteBuffernetInBufferNetwork input buffer for encrypted dataprotected java.nio.ByteBuffernetOutBufferNetwork output buffer for encrypted dataprotected booleansniCompleteTrue when SNI processing is completeprotected javax.net.ssl.SSLEnginesslEngineSSL engine for this channel-
Fields inherited from class org.apache.tomcat.util.net.NioChannel
bufHandler, emptyBuf, sc, socketWrapper
-
-
Constructor Summary
Constructors Constructor Description SecureNioChannel(SocketBufferHandler bufHandler, NioEndpoint endpoint)Creates a new secure NIO channel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Sends an SSL close message, will not physically close the connection here.voidclose(boolean force)Close the connection.protected booleanflush(java.nio.ByteBuffer buf)Flushes the buffer to the network, non-blockingbooleanflushOutbound()Return true if the buffer wrote data.voidfree()Free the channel memoryjava.nio.ByteBuffergetEmptyBuf()Returns an empty byte buffer used for SSL wrap operations.intgetOutboundRemaining()Returns the number of bytes remaining in the outbound buffer.javax.net.ssl.SSLEnginegetSslEngine()Returns the SSL engine for this channel.SSLSupportgetSSLSupport()Returns the SSL support object for this channel, ornullif the SSL engine has not been initialized.inthandshake(boolean read, boolean write)Performs SSL handshake, non-blocking, but performs NEED_TASK on the same thread.protected javax.net.ssl.SSLEngineResulthandshakeUnwrap(boolean doread)Perform handshake unwrapprotected javax.net.ssl.SSLEngineResulthandshakeWrap(boolean doWrite)Performs the WRAP functionbooleanisClosing()Indicates whether the channel is in the process of closing.booleanisHandshakeComplete()Indicates whether the SSL handshake is complete.intread(java.nio.ByteBuffer dst)Reads a sequence of bytes from this channel into the given buffer.longread(java.nio.ByteBuffer[] dsts, int offset, int length)voidrehandshake(long timeout)Force a blocking handshake to take place for this key.voidreset(java.nio.channels.SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper)Reset the channelprotected javax.net.ssl.SSLEngineResult.HandshakeStatustasks()Executes all the tasks needed on the same thread.intwrite(java.nio.ByteBuffer src)Writes a sequence of bytes to this channel from the given buffer.longwrite(java.nio.ByteBuffer[] srcs, int offset, int length)-
Methods inherited from class org.apache.tomcat.util.net.NioChannel
checkInterruptStatus, getAppReadBufHandler, getBufHandler, getIOChannel, isOpen, read, setAppReadBufHandler, toString, write
-
-
-
-
Field Detail
-
netInBuffer
protected java.nio.ByteBuffer netInBuffer
Network input buffer for encrypted data
-
netOutBuffer
protected java.nio.ByteBuffer netOutBuffer
Network output buffer for encrypted data
-
sslEngine
protected javax.net.ssl.SSLEngine sslEngine
SSL engine for this channel
-
sniComplete
protected boolean sniComplete
True when SNI processing is complete
-
handshakeComplete
protected boolean handshakeComplete
True when the SSL handshake is complete
-
needHandshakeWrap
protected boolean needHandshakeWrap
True when a handshake wrap is pending
-
handshakeStatus
protected javax.net.ssl.SSLEngineResult.HandshakeStatus handshakeStatus
Current handshake status, updated during handshake processing
-
closed
protected boolean closed
True when the channel is closed
-
closing
protected boolean closing
True when the channel is in the process of closing
-
-
Constructor Detail
-
SecureNioChannel
public SecureNioChannel(SocketBufferHandler bufHandler, NioEndpoint endpoint)
Creates a new secure NIO channel.- Parameters:
bufHandler- Buffer handler for the application buffersendpoint- The NIO endpoint managing this channel
-
-
Method Detail
-
reset
public void reset(java.nio.channels.SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws java.io.IOExceptionDescription copied from class:NioChannelReset the channel- Overrides:
resetin classNioChannel- Parameters:
channel- the socket channelsocketWrapper- the socket wrapper- Throws:
java.io.IOException- If a problem was encountered resetting the channel
-
free
public void free()
Description copied from class:NioChannelFree the channel memory- Overrides:
freein classNioChannel
-
flush
protected boolean flush(java.nio.ByteBuffer buf) throws java.io.IOExceptionFlushes the buffer to the network, non-blocking- Parameters:
buf- ByteBuffer- Returns:
- boolean true if the buffer has been emptied out, false otherwise
- Throws:
java.io.IOException- An IO error occurred writing data
-
handshake
public int handshake(boolean read, boolean write) throws java.io.IOExceptionPerforms SSL handshake, non-blocking, but performs NEED_TASK on the same thread. Hence, you should never call this method using your Acceptor thread, as you would slow down your system significantly. If the return value from this method is positive, the selection key should be registered interestOps given by the return value.- Overrides:
handshakein classNioChannel- Parameters:
read- boolean - true if the underlying channel is readablewrite- boolean - true if the underlying channel is writable- Returns:
- 0 if handshake is complete, -1 if an error (other than an IOException) occurred, otherwise it returns a SelectionKey interestOps value
- Throws:
java.io.IOException- If an I/O error occurs during the handshake or if the handshake fails during wrapping or unwrapping
-
rehandshake
public void rehandshake(long timeout) throws java.io.IOExceptionForce a blocking handshake to take place for this key. This requires that both network and application buffers have been emptied out prior to this call taking place, or a IOException will be thrown.- Parameters:
timeout- - timeout in milliseconds for each socket operation- Throws:
java.io.IOException- - if an IO exception occurs or if application or network buffers contain datajava.net.SocketTimeoutException- - if a socket operation timed out
-
tasks
protected javax.net.ssl.SSLEngineResult.HandshakeStatus tasks()
Executes all the tasks needed on the same thread.- Returns:
- the status
-
handshakeWrap
protected javax.net.ssl.SSLEngineResult handshakeWrap(boolean doWrite) throws java.io.IOExceptionPerforms the WRAP function- Parameters:
doWrite- boolean- Returns:
- the result
- Throws:
java.io.IOException- An IO error occurred
-
handshakeUnwrap
protected javax.net.ssl.SSLEngineResult handshakeUnwrap(boolean doread) throws java.io.IOExceptionPerform handshake unwrap- Parameters:
doread- boolean- Returns:
- the result
- Throws:
java.io.IOException- An IO error occurred
-
getSSLSupport
public SSLSupport getSSLSupport()
Returns the SSL support object for this channel, ornullif the SSL engine has not been initialized.- Returns:
- the SSL support object, or
null
-
close
public void close() throws java.io.IOExceptionSends an SSL close message, will not physically close the connection here.
To close the connection, you could do something likeclose(); while (isOpen() && !myTimeoutFunction()) Thread.sleep(25); if ( isOpen() ) close(true); //forces a close if you timed out- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classNioChannel- Throws:
java.io.IOException- if an I/O error occursjava.io.IOException- if there is data on the outgoing network buffer, and we are unable to flush it
-
close
public void close(boolean force) throws java.io.IOExceptionDescription copied from class:NioChannelClose the connection.- Overrides:
closein classNioChannel- Parameters:
force- Should the underlying socket be forcibly closed?- Throws:
java.io.IOException- If closing the secure channel fails.
-
read
public int read(java.nio.ByteBuffer dst) throws java.io.IOExceptionReads a sequence of bytes from this channel into the given buffer.- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Overrides:
readin classNioChannel- Parameters:
dst- The buffer into which bytes are to be transferred- Returns:
- The number of bytes read, possibly zero, or
-1if the channel has reached end-of-stream - Throws:
java.io.IOException- If some other I/O error occursjava.lang.IllegalStateException- if the handshake was not completed
-
read
public long read(java.nio.ByteBuffer[] dsts, int offset, int length) throws java.io.IOException- Specified by:
readin interfacejava.nio.channels.ScatteringByteChannel- Overrides:
readin classNioChannel- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer src) throws java.io.IOExceptionWrites a sequence of bytes to this channel from the given buffer.- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Overrides:
writein classNioChannel- Parameters:
src- The buffer from which bytes are to be retrieved- Returns:
- The number of bytes written, possibly zero
- Throws:
java.io.IOException- If some other I/O error occurs
-
write
public long write(java.nio.ByteBuffer[] srcs, int offset, int length) throws java.io.IOException- Specified by:
writein interfacejava.nio.channels.GatheringByteChannel- Overrides:
writein classNioChannel- Throws:
java.io.IOException
-
getOutboundRemaining
public int getOutboundRemaining()
Description copied from class:NioChannelReturns the number of bytes remaining in the outbound buffer.- Overrides:
getOutboundRemainingin classNioChannel- Returns:
- Always returns 0 for non-secure channels
-
flushOutbound
public boolean flushOutbound() throws java.io.IOExceptionDescription copied from class:NioChannelReturn true if the buffer wrote data. NO-OP for non-secure channel.- Overrides:
flushOutboundin classNioChannel- Returns:
- Always returns
falsefor non-secure channel - Throws:
java.io.IOException- Never for non-secure channel
-
isHandshakeComplete
public boolean isHandshakeComplete()
Description copied from class:NioChannelIndicates whether the SSL handshake is complete.- Overrides:
isHandshakeCompletein classNioChannel- Returns:
truefor non-secure channels (no handshake required)
-
isClosing
public boolean isClosing()
Description copied from class:NioChannelIndicates whether the channel is in the process of closing.- Overrides:
isClosingin classNioChannel- Returns:
falsefor non-secure channels
-
getSslEngine
public javax.net.ssl.SSLEngine getSslEngine()
Returns the SSL engine for this channel.- Returns:
- the SSL engine, or
nullif not yet created
-
getEmptyBuf
public java.nio.ByteBuffer getEmptyBuf()
Returns an empty byte buffer used for SSL wrap operations.- Returns:
- an empty byte buffer
-
-