Top Description Inners Fields Constructors Methods
org.apache.tomcat.util.net

public Class SecureNioChannel

extends NioChannel
Class Inheritance
Imports
java.io.EOFException, .IOException, java.net.SocketTimeoutException, java.nio.ByteBuffer, java.nio.channels.SelectionKey, .Selector, .SocketChannel, java.util.Collections, .HashMap, .List, .Map, javax.net.ssl.SSLEngine, .SSLEngineResult, .SSLEngineResult.HandshakeStatus, .SSLEngineResult.Status, .SSLException, .SSLSession, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.buf.ByteBufferUtils, org.apache.tomcat.util.net.NioEndpoint.NioSocketWrapper, .TLSClientHelloExtractor.ExtractorResult, org.apache.tomcat.util.net.openssl.ciphers.Cipher, org.apache.tomcat.util.res.StringManager

Implementation of a secure socket channel

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum

Field Summary

Modifier and TypeField and Description
private final Map<String, List<String>>
protected boolean
protected boolean
private static final int
private final NioEndpoint
protected boolean
protected SSLEngineResult.HandshakeStatus
private static final Log
protected ByteBuffer
protected ByteBuffer
private static final StringManager
protected boolean
protected SSLEngine
Inherited from org.apache.tomcat.util.net.NioChannel:
bufHandlerCLOSED_NIO_CHANNELemptyBufscsocketWrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides org.apache.tomcat.util.net.NioChannel.close.

Implements java.nio.channels.Channel.close.

Sends an SSL close message, will not physically close the connection here.

public void
close(boolean
Should the underlying socket be forcibly closed?
force
)

Overrides org.apache.tomcat.util.net.NioChannel.close.

Close the connection.

private void
protected boolean

Returns:

boolean true if the buffer has been emptied out, false otherwise
flush
(ByteBuffer
ByteBuffer
buf
)

Flushes the buffer to the network, non blocking

public boolean
flushOutbound()

Overrides org.apache.tomcat.util.net.NioChannel.flushOutbound.

Return true if the buffer wrote data.

public void
free()

Overrides org.apache.tomcat.util.net.NioChannel.free.

Free the channel memory

public ByteBuffer
public int
public SSLEngine
public SSLSupport
public int

Returns:

0 if hand shake is complete, -1 if an error (other than an IOException) occurred, otherwise it returns a SelectionKey interestOps value
handshake
(boolean
boolean - true if the underlying channel is readable
read
,
boolean
boolean - true if the underlying channel is writable
write
)

Overrides org.apache.tomcat.util.net.NioChannel.handshake.

Performs SSL handshake, non blocking, but performs NEED_TASK on the same thread.

protected SSLEngineResult

Returns:

the result
handshakeUnwrap
(boolean
boolean
doread
)

Perform handshake unwrap

protected SSLEngineResult

Returns:

the result
handshakeWrap
(boolean
boolean
doWrite
)

Performs the WRAP function

public boolean
public boolean
private int
public int

Returns:

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
read
(ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Overrides org.apache.tomcat.util.net.NioChannel.read.

Implements java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

public long
read(ByteBuffer[]
The buffers into which bytes are to be transferred
dsts
,
int
The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
length
)

Overrides org.apache.tomcat.util.net.NioChannel.read.

Implements java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

public void
rehandshake(long
- timeout in milliseconds for each socket operation
timeout
)

Force a blocking handshake to take place for this key.

public void
reset(SocketChannel
the socket channel
channel
,
NioEndpoint.NioSocketWrapper
the socket wrapper
socketWrapper
)

Overrides org.apache.tomcat.util.net.NioChannel.reset.

Reset the channel

protected SSLEngineResult.HandshakeStatus

Returns:

the status
tasks
()

Executes all the tasks needed on the same thread.

public int

Returns:

The number of bytes written, possibly zero
write
(ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Overrides org.apache.tomcat.util.net.NioChannel.write.

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

public long
write(ByteBuffer[]
The buffers from which bytes are to be retrieved
srcs
,
int
The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
offset
,
int
The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
length
)

Overrides org.apache.tomcat.util.net.NioChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Inherited from org.apache.tomcat.util.net.NioChannel:
checkInterruptStatusgetAppReadBufHandlergetBufHandlergetIOChannelgetSocketWrapperisOpenreadsetAppReadBufHandlertoStringwrite

Field Detail

additionalTlsAttributesback to summary
private final Map<String, List<String>> additionalTlsAttributes
closedback to summary
protected boolean closed
closingback to summary
protected boolean closing
DEFAULT_NET_BUFFER_SIZEback to summary
private static final int DEFAULT_NET_BUFFER_SIZE
endpointback to summary
private final NioEndpoint endpoint
handshakeCompleteback to summary
protected boolean handshakeComplete
handshakeStatusback to summary
protected SSLEngineResult.HandshakeStatus handshakeStatus
logback to summary
private static final Log log
netInBufferback to summary
protected ByteBuffer netInBuffer
netOutBufferback to summary
protected ByteBuffer netOutBuffer
smback to summary
private static final StringManager sm

Hides org.apache.tomcat.util.net.NioChannel.sm.

sniCompleteback to summary
protected boolean sniComplete
sslEngineback to summary
protected SSLEngine sslEngine

Constructor Detail

SecureNioChannelback to summary
public SecureNioChannel(SocketBufferHandler bufHandler, NioEndpoint endpoint)

Method Detail

closeback to summary
public void close() throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.close.

Implements java.nio.channels.Channel.close.

Sends an SSL close message, will not physically close the connection here.
To close the connection, you could do something like


  close();
  while (isOpen() && !myTimeoutFunction()) Thread.sleep(25);
  if ( isOpen() ) close(true); //forces a close if you timed out
Annotations
@Override
Exceptions
IOException:
  • if an I/O error occurs
  • if there is data on the outgoing network buffer and we are unable to flush it
closeback to summary
public void close(boolean force) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.close.

Doc from org.apache.tomcat.util.net.NioChannel.close.

Close the connection.

Parameters
force:boolean

Should the underlying socket be forcibly closed?

Annotations
@Override
Exceptions
IOException:
If closing the secure channel fails.
closeSilentlyback to summary
private void closeSilently()
flushback to summary
protected boolean flush(ByteBuffer buf) throws IOException

Flushes the buffer to the network, non blocking

Parameters
buf:ByteBuffer

ByteBuffer

Returns:boolean

boolean true if the buffer has been emptied out, false otherwise

Exceptions
IOException:
An IO error occurred writing data
flushOutboundback to summary
public boolean flushOutbound() throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.flushOutbound.

Doc from org.apache.tomcat.util.net.NioChannel.flushOutbound.

Return true if the buffer wrote data. NO-OP for non-secure channel.

Returns:boolean

Always returns false for non-secure channel

Annotations
@Override
Exceptions
IOException:
Never for non-secure channel
freeback to summary
public void free()

Overrides org.apache.tomcat.util.net.NioChannel.free.

Doc from org.apache.tomcat.util.net.NioChannel.free.

Free the channel memory

Annotations
@Override
getEmptyBufback to summary
public ByteBuffer getEmptyBuf()
getOutboundRemainingback to summary
public int getOutboundRemaining()

Overrides org.apache.tomcat.util.net.NioChannel.getOutboundRemaining.

Annotations
@Override
getSslEngineback to summary
public SSLEngine getSslEngine()
getSSLSupportback to summary
public SSLSupport getSSLSupport()
handshakeback to summary
public int handshake(boolean read, boolean write) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.handshake.

Performs 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.

Parameters
read:boolean

boolean - true if the underlying channel is readable

write:boolean

boolean - true if the underlying channel is writable

Returns:int

0 if hand shake is complete, -1 if an error (other than an IOException) occurred, otherwise it returns a SelectionKey interestOps value

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs during the handshake or if the handshake fails during wrapping or unwrapping
handshakeUnwrapback to summary
protected SSLEngineResult handshakeUnwrap(boolean doread) throws IOException

Perform handshake unwrap

Parameters
doread:boolean

boolean

Returns:SSLEngineResult

the result

Exceptions
IOException:
An IO error occurred
handshakeWrapback to summary
protected SSLEngineResult handshakeWrap(boolean doWrite) throws IOException

Performs the WRAP function

Parameters
doWrite:boolean

boolean

Returns:SSLEngineResult

the result

Exceptions
IOException:
An IO error occurred
isClosingback to summary
public boolean isClosing()

Overrides org.apache.tomcat.util.net.NioChannel.isClosing.

Annotations
@Override
isHandshakeCompleteback to summary
public boolean isHandshakeComplete()

Overrides org.apache.tomcat.util.net.NioChannel.isHandshakeComplete.

Annotations
@Override
processSNIback to summary
private int processSNI() throws IOException
readback to summary
public int read(ByteBuffer dst) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.read.

Implements java.nio.channels.ReadableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

Parameters
dst:ByteBuffer

The buffer into which bytes are to be transferred

Returns:int

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
IllegalArgumentException:
if the destination buffer is different than getBufHandler().getReadBuffer()
readback to summary
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.read.

Implements java.nio.channels.ScatteringByteChannel.read.

Doc from java.nio.channels.ScatteringByteChannel.read.

Reads a sequence of bytes from this channel into a subsequence of the given buffers.

An invocation of this method attempts to read up to r bytes from this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

dsts[offset].remaining() + dsts[offset+1].remaining() + ... + dsts[offset+length-1].remaining()
dsts[offset].remaining()
    + dsts[offset+1].remaining()
    + ... + dsts[offset+length-1].remaining()
at the moment that this method is invoked.

Suppose that a byte sequence of length n is read, where 0 <= n <= r. Up to the first dsts[offset].remaining() bytes of this sequence are transferred into buffer dsts[offset], up to the next dsts[offset+1].remaining() bytes are transferred into buffer dsts[offset+1], and so forth, until the entire byte sequence is transferred into the given buffers. As many bytes as possible are transferred into each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.

This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
dsts:ByteBuffer[]

The buffers into which bytes are to be transferred

offset:int

The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length

length:int

The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset

Returns:long

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
rehandshakeback to summary
public void rehandshake(long timeout) throws IOException

Force 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:long

- timeout in milliseconds for each socket operation

Annotations
@SuppressWarnings:null
Exceptions
IOException:
- if an IO exception occurs or if application or network buffers contain data
SocketTimeoutException:
- if a socket operation timed out
resetback to summary
public void reset(SocketChannel channel, NioEndpoint.NioSocketWrapper socketWrapper) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.reset.

Doc from org.apache.tomcat.util.net.NioChannel.reset.

Reset the channel

Parameters
channel:SocketChannel

the socket channel

socketWrapper:NioEndpoint.NioSocketWrapper

the socket wrapper

Annotations
@Override
Exceptions
IOException:
If a problem was encountered resetting the channel
tasksback to summary
protected SSLEngineResult.HandshakeStatus tasks()

Executes all the tasks needed on the same thread.

Returns:SSLEngineResult.HandshakeStatus

the status

writeback to summary
public int write(ByteBuffer src) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.write.

Implements java.nio.channels.WritableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

Parameters
src:ByteBuffer

The buffer from which bytes are to be retrieved

Returns:int

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
writeback to summary
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException

Overrides org.apache.tomcat.util.net.NioChannel.write.

Implements java.nio.channels.GatheringByteChannel.write.

Doc from java.nio.channels.GatheringByteChannel.write.

Writes a sequence of bytes to this channel from a subsequence of the given buffers.

An attempt is made to write up to r bytes to this channel, where r is the total number of bytes remaining in the specified subsequence of the given buffer array, that is,

srcs[offset].remaining() + srcs[offset+1].remaining() + ... + srcs[offset+length-1].remaining()
srcs[offset].remaining()
    + srcs[offset+1].remaining()
    + ... + srcs[offset+length-1].remaining()
at the moment that this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. Up to the first srcs[offset].remaining() bytes of this sequence are written from buffer srcs[offset], up to the next srcs[offset+1].remaining() bytes are written from buffer srcs[offset+1], and so forth, until the entire byte sequence is written. As many bytes as possible are written from each buffer, hence the final position of each updated buffer, except the last updated buffer, is guaranteed to be equal to that buffer's limit.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters
srcs:ByteBuffer[]

The buffers from which bytes are to be retrieved

offset:int

The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length

length:int

The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset

Returns:long

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
org.apache.tomcat.util.net back to summary

private final Enum SecureNioChannel.OverflowState

extends Enum<SecureNioChannel.OverflowState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final SecureNioChannel.OverflowState
public static final SecureNioChannel.OverflowState
public static final SecureNioChannel.OverflowState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static SecureNioChannel.OverflowState
public static SecureNioChannel.OverflowState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DONEback to summary
public static final SecureNioChannel.OverflowState DONE
NONEback to summary
public static final SecureNioChannel.OverflowState NONE
PROCESSINGback to summary
public static final SecureNioChannel.OverflowState PROCESSING

Constructor Detail

OverflowStateback to summary
private OverflowState()

Method Detail

valueOfback to summary
public static SecureNioChannel.OverflowState valueOf(String name)
valuesback to summary
public static SecureNioChannel.OverflowState[] values()