Top Description Fields Constructors Methods
org.xnio.ssl

pack-priv final Class JsseSslConduitEngine

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, java.security.cert.CertPathBuilderException, java.util.concurrent.TimeUnit, java.util.concurrent.atomic.AtomicIntegerFieldUpdater, .AtomicReferenceFieldUpdater, javax.net.ssl.SSLEngine, .SSLEngineResult, .SSLEngineResult.HandshakeStatus, .SSLException, .SSLHandshakeException, .SSLSession, org.jboss.logging.Logger, org.xnio.Buffers, .Pool, .Pooled, org.xnio.conduits.StreamSinkConduit, .StreamSourceConduit

SSLEngine wrapper, used by Jsse SSL conduits.
Author
Flavia Rainone

Field Summary

Modifier and TypeField and Description
private static final int
private final JsseSslStreamConnection
private static final ByteBuffer
private final SSLEngine
engine

The SSL engine.

private static final int
private ByteBuffer
expandedSendBuffer

An expanded non-final buffer from which outbound SSL data is sent when large fragments handling is enabled in the underlying SSL Engine.

private int
private static final int
private static final String
private static final Logger
private static final int
private static final int
private static final int
private static final int
private final Pooled<ByteBuffer>
readBuffer

References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.
The buffer into which inbound clear data is written.
private volatile Thread
private static final AtomicReferenceFieldUpdater<JsseSslConduitEngine, Thread>
private final Pooled<ByteBuffer>
receiveBuffer

References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.
The buffer into which incoming SSL data is written.
private final Pooled<ByteBuffer>
sendBuffer

References Deprecated Pooled is deprecated or references (maybe indirectly) at least one deprecated element.
The buffer from which outbound SSL data is sent.
private final StreamSinkConduit
private final StreamSourceConduit
private volatile int
private static final AtomicIntegerFieldUpdater<JsseSslConduitEngine>
private static final int
private static final int
private static final int
private volatile Thread
private static final AtomicReferenceFieldUpdater<JsseSslConduitEngine, Thread>

Constructor Summary

AccessConstructor and Description
pack-priv
JsseSslConduitEngine(final JsseSslStreamConnection
the ssl connection associated with this engine
connection
,
final StreamSinkConduit
the sink channel to use for write operations
sinkConduit
,
final StreamSourceConduit
the source channel to use for read operations
sourceConduit
,
final SSLEngine
the SSL engine to use
engine
,
final Pool<ByteBuffer>
the socket buffer pool
socketBufferPool
,
final Pool<ByteBuffer>
the application buffer pool
applicationBufferPool
)
References Deprecated Pool is deprecated or references (maybe indirectly) at least one deprecated element.

Construct a new instance.

Method Summary

Modifier and TypeMethod and Description
public void
awaitCanUnwrap()

Block until this engine can unwrap messages.

public void
awaitCanUnwrap(long
timeout for blocking
time
,
TimeUnit
timeout unit
timeUnit
)

Block until this engine can unwrap messages.

public void
awaitCanWrap()

Block until this engine can wrap messages.

public void
awaitCanWrap(long
timeout for blocking
time
,
TimeUnit
timeout unit
timeUnit
)

Block until this engine can wrap messages.

public void
beginHandshake()

Begins handshake.

private int
clearFlags(int flags)

private void
clearNeedUnwrap()

Indicate that the engine no longer requires a successful unwrap to proceed with wrap operations.

private void
clearNeedWrap()

Indicate that the engine no longer requires a successful wrap to proceed with unwrap operations.

pack-priv void
close()

Closes the engine for both inbound and outbound connection, and discards this engine's internal resources immediately without handshaking if this engine has not been used.

private void
closeEngine()

Closes this engine for both inbound and outbound, clearing the buffers.

public void
closeInbound()

Signals that no inbound data will be read

public void
closeOutbound()

Signals that no outbound data will be sent.

private int

Returns:

the amount of copied bytes
copyUnwrappedData
(final ByteBuffer[]
destine of copy
dsts
,
final int
offset
offset
,
final int
length
length
,
ByteBuffer
source from where byte will be copied
unwrappedBuffer
)

Copy unwrapped data from unwrappedBuffer into dsts.

private boolean

Returns:

true if all data available has been flushed
doFlush
()

Flushes all data in the wrapped bytes buffer.

private SSLEngineResult
engineUnwrap(final ByteBuffer buffer, final ByteBuffer unwrappedBuffer)

Invoke inner SSL engine to unwrap.

private SSLEngineResult
engineWrap(final ByteBuffer[] srcs, final int offset, final int length, final ByteBuffer dest)

Invoke inner SSL engine to wrap.

private SSLEngineResult
engineWrap(final ByteBuffer src, final ByteBuffer dest)

Invoke inner SSL engine to wrap.

public boolean

Returns:

true if there is no left data to be flushed; false if for some reason the engine was unable to flush all data
flush
()

Flush any data needed for handshaking into the wrapped buffer.

pack-priv SSLEngine
private final ByteBuffer
public SSLSession
getSession()

Returns the engine's session.

public ByteBuffer

Returns:

the buffer containing bytes to be unwrapped
getUnwrapBuffer
()

Returns the buffer that contains the data to be unwrapped.

public Object

Returns:

lock for protecting access to the unwrap buffer
getUnwrapLock
()

Returns the unwrap lock, that must be used whenever the unwrap buffer is being accessed.

public Object

Returns:

lock for protecting access to the unwrapped buffer
getWrapLock
()

Returns the wrap lock, that must be used whenever the wrapped buffer is being accessed.

public ByteBuffer

Returns:

the buffer containing wrapped bytes
getWrappedBuffer
()

Returns the buffer that contains the wrapped data.

private boolean

Returns:

true to indicate that caller should rerun the previous wrap or unwrap operation, hence producing a new result; false to indicate otherwise
handleHandshake
(SSLEngineResult
the wrap/unwrap result
result
,
boolean
if true, indicates caller executed a wrap operation; if false, indicates caller executed an unwrap operation
write
)

Handle handshake process, after a wrap or an unwrap operation.

private int

Returns:

the amount of bytes consumed by unwrap. If the engine is closed and no bytes were consumed, returns -1.
handleUnwrapResult
(final SSLEngineResult
the unwrap result
result
)

Handles the unwrap result, indicating how many bytes have been consumed.

private boolean

Returns:

true if a new attempt to wrap should be made
handleWrapResult
(SSLEngineResult
the wrap result
result
,
boolean
is a closed engine result expected
closeExpected
)

Handles the wrap result, indicating if caller should perform a new attempt to wrap.

public boolean
isClosed()

Indicates if engine is closed.

public boolean
public boolean
public boolean
isInboundClosed()

Indicates if inbound is closed.

public boolean
isOutboundClosed()

Indicates if outbound is closed.

private boolean
isUnderflow()

Indicates that even though there is data available there is not enough to form a complete packet

private boolean
isUnwrapNeeded()

Indicate if the engine can wrap.

private boolean
isWrapNeeded()

Indicate if the engine can unwrap.

private void
needUnwrap()

Indicate that the engine will not be able wrap before a successful unwrap is performed.

private void
needWrap()

Indicate that the engine will not be able unwrap before a successful wrap is performed.

private int
setFlags(int flags)

public int

Returns:

the amount of resulting unwrapped bytes
unwrap
(final ByteBuffer
where the resulting unwrapped bytes will be copied to
dst
)

Unwraps the bytes contained in getUnwrapBuffer(), copying the resulting unwrapped bytes into dst.

public long

Returns:

the amount of resulting unwrapped bytes
unwrap
(final ByteBuffer[]
where the resulting unwrapped bytes will be copied to
dsts
,
final int
offset
offset
,
final int
length
length
)

Unwraps the bytes contained in getUnwrapBuffer(), copying the resulting unwrapped bytes into dsts.

public int

Returns:

the amount of wrapped bytes.
wrap
(final ByteBuffer
the bytes to be wrapped
src
)

Attempt to wrap the bytes in src.

public long

Returns:

the amount of wrapped bytes.
wrap
(final ByteBuffer[]
contains the bytes to be wrapped
srcs
,
final int
offset
offset
,
final int
length
length
)

Attempt to wrap the bytes in srcs.

private int

Returns:

the amount of resulting wrapped bytes
wrap
(final ByteBuffer
contains the bytes to be wrapped
src
,
boolean
indicates if close is expected, information that is used to perform special handling when closing the engine
isCloseExpected
)

Wrap operation used internally.

private boolean

Returns:

true only if all bytes concerning close handshake messages have been wrapped.
wrapCloseMessage
()

Attempt to finish wrapping close handshake bytes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_UNDERFLOWback to summary
private static final int BUFFER_UNDERFLOW
connectionback to summary
private final JsseSslStreamConnection connection
EMPTY_BUFFERback to summary
private static final ByteBuffer EMPTY_BUFFER
engineback to summary
private final SSLEngine engine

The SSL engine.

ENGINE_CLOSEDback to summary
private static final int ENGINE_CLOSED
expandedSendBufferback to summary
private ByteBuffer expandedSendBuffer

An expanded non-final buffer from which outbound SSL data is sent when large fragments handling is enabled in the underlying SSL Engine. When this happens, we need a specific buffer with expanded capacity.

failureCountback to summary
private int failureCount
FIRST_HANDSHAKEback to summary
private static final int FIRST_HANDSHAKE
FQCNback to summary
private static final String FQCN
logback to summary
private static final Logger log
NEED_UNWRAPback to summary
private static final int NEED_UNWRAP
NEED_WRAPback to summary
private static final int NEED_WRAP
READ_FLAGSback to summary
private static final int READ_FLAGS
Annotations
@SuppressWarnings:unused
READ_SHUT_DOWNback to summary
private static final int READ_SHUT_DOWN
readBufferback to summary
private final Pooled<ByteBuffer> readBuffer

References Deprecated

Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

The buffer into which inbound clear data is written.

readWaiterback to summary
private volatile Thread readWaiter
Annotations
@SuppressWarnings:unused
readWaiterUpdaterback to summary
private static final AtomicReferenceFieldUpdater<JsseSslConduitEngine, Thread> readWaiterUpdater
receiveBufferback to summary
private final Pooled<ByteBuffer> receiveBuffer

References Deprecated

Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

The buffer into which incoming SSL data is written.

sendBufferback to summary
private final Pooled<ByteBuffer> sendBuffer

References Deprecated

Pooled is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

The buffer from which outbound SSL data is sent.

sinkConduitback to summary
private final StreamSinkConduit sinkConduit
sourceConduitback to summary
private final StreamSourceConduit sourceConduit
stateback to summary
private volatile int state
stateUpdaterback to summary
private static final AtomicIntegerFieldUpdater<JsseSslConduitEngine> stateUpdater
WRITE_COMPLETEback to summary
private static final int WRITE_COMPLETE
WRITE_FLAGSback to summary
private static final int WRITE_FLAGS
Annotations
@SuppressWarnings:unused
WRITE_SHUT_DOWNback to summary
private static final int WRITE_SHUT_DOWN
writeWaiterback to summary
private volatile Thread writeWaiter
Annotations
@SuppressWarnings:unused
writeWaiterUpdaterback to summary
private static final AtomicReferenceFieldUpdater<JsseSslConduitEngine, Thread> writeWaiterUpdater

Constructor Detail

JsseSslConduitEngineback to summary
pack-priv JsseSslConduitEngine(final JsseSslStreamConnection connection, final StreamSinkConduit sinkConduit, final StreamSourceConduit sourceConduit, final SSLEngine engine, final Pool<ByteBuffer> socketBufferPool, final Pool<ByteBuffer> applicationBufferPool)

References Deprecated

Pool is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Construct a new instance.

Parameters
connection:JsseSslStreamConnection

the ssl connection associated with this engine

sinkConduit:StreamSinkConduit

the sink channel to use for write operations

sourceConduit:StreamSourceConduit

the source channel to use for read operations

engine:SSLEngine

the SSL engine to use

socketBufferPool:Pool<ByteBuffer>

the socket buffer pool

applicationBufferPool:Pool<ByteBuffer>

the application buffer pool

Method Detail

awaitCanUnwrapback to summary
public void awaitCanUnwrap() throws IOException

Block until this engine can unwrap messages.

Exceptions
IOException:
if an IO exception occurs during await
awaitCanUnwrapback to summary
public void awaitCanUnwrap(long time, TimeUnit timeUnit) throws IOException

Block until this engine can unwrap messages.

Parameters
time:long

timeout for blocking

timeUnit:TimeUnit

timeout unit

Exceptions
IOException:
if an IO exception occurs during await
awaitCanWrapback to summary
public void awaitCanWrap() throws IOException

Block until this engine can wrap messages.

The engine may be unable to wrap if a handshake message needs to be unwrapped first. In this case, this thread will be awakened as soon as the message is made available for reading by the internal source conduit.

Exceptions
IOException:
if an IO exception occurs during await
awaitCanWrapback to summary
public void awaitCanWrap(long time, TimeUnit timeUnit) throws IOException

Block until this engine can wrap messages.

The engine may be unable to wrap if a handshake message needs to be unwrapped first. In this case, this thread will be awakened as soon as the message is made available for reading by the internal source conduit.

Parameters
time:long

timeout for blocking

timeUnit:TimeUnit

timeout unit

Exceptions
IOException:
if an IO exception occurs during await
beginHandshakeback to summary
public void beginHandshake() throws IOException

Begins handshake.

Exceptions
IOException:
if an I/O error occurs
clearFlagsback to summary
private int clearFlags(int flags)
clearNeedUnwrapback to summary
private void clearNeedUnwrap()

Indicate that the engine no longer requires a successful unwrap to proceed with wrap operations.

clearNeedWrapback to summary
private void clearNeedWrap()

Indicate that the engine no longer requires a successful wrap to proceed with unwrap operations.

closeback to summary
pack-priv void close() throws IOException

Closes the engine for both inbound and outbound connection, and discards this engine's internal resources immediately without handshaking if this engine has not been used.

closeEngineback to summary
private void closeEngine() throws IOException

Closes this engine for both inbound and outbound, clearing the buffers.

closeInboundback to summary
public void closeInbound() throws IOException

Signals that no inbound data will be read

Exceptions
IOException:
if an IO exception occurs
closeOutboundback to summary
public void closeOutbound() throws IOException

Signals that no outbound data will be sent.

Exceptions
IOException:
if an IO exception occurs
copyUnwrappedDataback to summary
private int copyUnwrappedData(final ByteBuffer[] dsts, final int offset, final int length, ByteBuffer unwrappedBuffer)

Copy unwrapped data from unwrappedBuffer into dsts.

Parameters
dsts:ByteBuffer[]

destine of copy

offset:int

offset

length:int

length

unwrappedBuffer:ByteBuffer

source from where byte will be copied

Returns:int

the amount of copied bytes

doFlushback to summary
private boolean doFlush() throws IOException

Flushes all data in the wrapped bytes buffer.

Returns:boolean

true if all data available has been flushed

Exceptions
IOException:
if an unexpected IO exception occurs
engineUnwrapback to summary
private SSLEngineResult engineUnwrap(final ByteBuffer buffer, final ByteBuffer unwrappedBuffer) throws IOException

Invoke inner SSL engine to unwrap.

engineWrapback to summary
private SSLEngineResult engineWrap(final ByteBuffer[] srcs, final int offset, final int length, final ByteBuffer dest) throws SSLException

Invoke inner SSL engine to wrap.

engineWrapback to summary
private SSLEngineResult engineWrap(final ByteBuffer src, final ByteBuffer dest) throws SSLException

Invoke inner SSL engine to wrap.

flushback to summary
public boolean flush() throws IOException

Flush any data needed for handshaking into the wrapped buffer.

The flushed data can later be retrieved by reading the buffer. If the engine is closed, this method can be used to flush all data associated to engine close messages.

Returns:boolean

true if there is no left data to be flushed; false if for some reason the engine was unable to flush all data

Exceptions
IOException:
if an IO exception occurs during attempt to flush
getEngineback to summary
pack-priv SSLEngine getEngine()
getSendBufferback to summary
private final ByteBuffer getSendBuffer()
getSessionback to summary
public SSLSession getSession()

Returns the engine's session.

getUnwrapBufferback to summary
public ByteBuffer getUnwrapBuffer()

Returns the buffer that contains the data to be unwrapped.

Retrieval and manipulation of this buffer should always be protected by the unwrap lock.

Returns:ByteBuffer

the buffer containing bytes to be unwrapped

getUnwrapLockback to summary
public Object getUnwrapLock()

Returns the unwrap lock, that must be used whenever the unwrap buffer is being accessed.

This lock is also used internally by unwrapping operations, thus guaranteeing safe concurrent execution of both wrap and unwrap operations, specially during handshake handling.

Returns:Object

lock for protecting access to the unwrap buffer

getWrapLockback to summary
public Object getWrapLock()

Returns the wrap lock, that must be used whenever the wrapped buffer is being accessed.

This lock is also used internally by wrapping operations, thus guaranteeing safe concurrent execution of both wrap and unwrap operations, specially during handshake handling.

Returns:Object

lock for protecting access to the unwrapped buffer

getWrappedBufferback to summary
public ByteBuffer getWrappedBuffer()

Returns the buffer that contains the wrapped data.

Retrieval and manipulation of this buffer should always be protected by the wrap lock.

Returns:ByteBuffer

the buffer containing wrapped bytes

handleHandshakeback to summary
private boolean handleHandshake(SSLEngineResult result, boolean write) throws IOException

Handle handshake process, after a wrap or an unwrap operation.

Parameters
result:SSLEngineResult

the wrap/unwrap result

write:boolean

if true, indicates caller executed a wrap operation; if false, indicates caller executed an unwrap operation

Returns:boolean

true to indicate that caller should rerun the previous wrap or unwrap operation, hence producing a new result; false to indicate otherwise

Exceptions
IOException:
if an IO error occurs during handshake handling
handleUnwrapResultback to summary
private int handleUnwrapResult(final SSLEngineResult result) throws IOException

Handles the unwrap result, indicating how many bytes have been consumed.

Parameters
result:SSLEngineResult

the unwrap result

Returns:int

the amount of bytes consumed by unwrap. If the engine is closed and no bytes were consumed, returns -1.

Exceptions
IOException:
if an IO exception occurs
handleWrapResultback to summary
private boolean handleWrapResult(SSLEngineResult result, boolean closeExpected) throws IOException

Handles the wrap result, indicating if caller should perform a new attempt to wrap.

Parameters
result:SSLEngineResult

the wrap result

closeExpected:boolean

is a closed engine result expected

Returns:boolean

true if a new attempt to wrap should be made

Exceptions
IOException:
if an IO exception occurs
isClosedback to summary
public boolean isClosed()

Indicates if engine is closed.

isDataAvailableback to summary
public boolean isDataAvailable()
isFirstHandshakeback to summary
public boolean isFirstHandshake()
isInboundClosedback to summary
public boolean isInboundClosed()

Indicates if inbound is closed.

Exceptions
IOException:
if an IO exception occurs
isOutboundClosedback to summary
public boolean isOutboundClosed()

Indicates if outbound is closed.

Exceptions
IOException:
if an IO exception occurs
isUnderflowback to summary
private boolean isUnderflow()

Indicates that even though there is data available there is not enough to form a complete packet

isUnwrapNeededback to summary
private boolean isUnwrapNeeded()

Indicate if the engine can wrap.

isWrapNeededback to summary
private boolean isWrapNeeded()

Indicate if the engine can unwrap.

needUnwrapback to summary
private void needUnwrap()

Indicate that the engine will not be able wrap before a successful unwrap is performed.

needWrapback to summary
private void needWrap()

Indicate that the engine will not be able unwrap before a successful wrap is performed.

setFlagsback to summary
private int setFlags(int flags)
unwrapback to summary
public int unwrap(final ByteBuffer dst) throws IOException

Unwraps the bytes contained in getUnwrapBuffer(), copying the resulting unwrapped bytes into dst.

If the engine is performing handshake during this request, not all bytes could be unwrapped. In this case, a later call can be performed to attempt to unwrap more bytes.

This method must not be invoked inside the unwrap lock, or else unexpected behavior could occur.

Parameters
dst:ByteBuffer

where the resulting unwrapped bytes will be copied to

Returns:int

the amount of resulting unwrapped bytes

Exceptions
IOException:
if an IO exception occurs during unwrapping
unwrapback to summary
public long unwrap(final ByteBuffer[] dsts, final int offset, final int length) throws IOException

Unwraps the bytes contained in getUnwrapBuffer(), copying the resulting unwrapped bytes into dsts.

If the engine is performing handshake during this request, not all bytes could be unwrapped. In this case, a later call can be performed to attempt to unwrap more bytes.

This method must not be invoked inside the unwrap lock, or else unexpected behavior could occur.

Parameters
dsts:ByteBuffer[]

where the resulting unwrapped bytes will be copied to

offset:int

offset

length:int

length

Returns:long

the amount of resulting unwrapped bytes

Exceptions
IOException:
if an IO exception occurs during unwrapping
wrapback to summary
public int wrap(final ByteBuffer src) throws IOException

Attempt to wrap the bytes in src. The wrapped bytes can be later retrieved by calling getWrappedBuffer().

If the engine is performing handshake during this request, not all bytes could be wrapped. In this case, a later call can be performed to attempt to wrap more bytes.

This method must not be invoked inside the wrap lock, or else unexpected behavior could occur.

Parameters
src:ByteBuffer

the bytes to be wrapped

Returns:int

the amount of wrapped bytes.

Exceptions
IOException:
if an IO exception occurs during wrapping
wrapback to summary
public long wrap(final ByteBuffer[] srcs, final int offset, final int length) throws IOException

Attempt to wrap the bytes in srcs. The wrapped bytes can be later retrieved by calling getWrappedBuffer().

If the engine is performing handshake during this request, not all bytes could be wrapped. In this case, a later call can be performed to attempt to wrap more bytes.

This method must not be invoked inside the wrap lock, or else unexpected behavior could occur.

Parameters
srcs:ByteBuffer[]

contains the bytes to be wrapped

offset:int

offset

length:int

length

Returns:long

the amount of wrapped bytes.

Exceptions
IOException:
if an IO exception occurs during wrapping
wrapback to summary
private int wrap(final ByteBuffer src, boolean isCloseExpected) throws IOException

Wrap operation used internally.

Parameters
src:ByteBuffer

contains the bytes to be wrapped

isCloseExpected:boolean

indicates if close is expected, information that is used to perform special handling when closing the engine

Returns:int

the amount of resulting wrapped bytes

Exceptions
IOException:
if an IO exception occurs during wrapping
wrapCloseMessageback to summary
private boolean wrapCloseMessage() throws IOException

Attempt to finish wrapping close handshake bytes.

Returns:boolean

true only if all bytes concerning close handshake messages have been wrapped.

Exceptions
IOException:
if an unexpected IO exception occurs