Crossfire JXClient, Trunk
Protected Member Functions | Package Functions | Static Package Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
ChannelIOSecure Class Reference
Inheritance diagram for ChannelIOSecure:
Inheritance graph
Collaboration diagram for ChannelIOSecure:
Collaboration graph

Protected Member Functions

 ChannelIOSecure (SocketChannel sc, boolean blocking, SSLContext sslc) throws IOException
 
void resizeRequestBB ()
 
- Protected Member Functions inherited from ChannelIO
 ChannelIO (SocketChannel sc, boolean blocking) throws IOException
 
void resizeRequestBB (int remaining)
 

Package Functions

boolean dataFlush () throws IOException
 
boolean doHandshake () throws IOException
 
boolean doHandshake (SelectionKey sk) throws IOException
 
int read () throws IOException
 
boolean shutdown () throws IOException
 
long transferTo (FileChannel fc, long pos, long len) throws IOException
 
int write (ByteBuffer src) throws IOException
 
- Package Functions inherited from ChannelIO
void close () throws IOException
 
ByteBuffer getReadBuf ()
 
SocketChannel getSocketChannel ()
 

Static Package Functions

static ChannelIOSecure getInstance (SocketChannel sc, boolean blocking, SSLContext sslc) throws IOException
 
- Static Package Functions inherited from ChannelIO
static ChannelIO getInstance (SocketChannel sc, boolean blocking) throws IOException
 

Private Member Functions

SSLEngineResult.HandshakeStatus doTasks ()
 
int doWrite (ByteBuffer src) throws IOException
 
void resizeResponseBB ()
 
boolean tryFlush (ByteBuffer bb) throws IOException
 

Private Attributes

int appBBSize
 
ByteBuffer fileChannelBB = null
 
boolean initialHSComplete
 
HandshakeStatus initialHSStatus
 
ByteBuffer inNetBB
 
int netBBSize
 
ByteBuffer outNetBB
 
boolean shutdown = false
 
SSLEngine sslEngine = null
 

Static Private Attributes

static ByteBuffer hsBB = ByteBuffer.allocate(0)
 

Additional Inherited Members

- Protected Attributes inherited from ChannelIO
ByteBuffer requestBB
 
SocketChannel sc
 

Detailed Description

A helper class which performs I/O using the SSLEngine API.

Each connection has a SocketChannel and a SSLEngine that is used through the lifetime of the Channel. We allocate byte buffers for use as the outbound and inbound network buffers.

              Application Data
              src      requestBB
               |           ^
               |     |     |
               v     |     |
          +----+-----|-----+----+
          |          |          |
          |       SSL|Engine    |
  wrap()  |          |          |  unwrap()
          | OUTBOUND | INBOUND  |
          |          |          |
          +----+-----|-----+----+
               |     |     ^
               |     |     |
               v           |
           outNetBB     inNetBB
                  Net data

These buffers handle all of the intermediary data for the SSL connection. To make things easy, we'll require outNetBB be completely flushed before trying to wrap any more data, but we could certainly remove that restriction by using larger buffers.

There are many, many ways to handle compute and I/O strategies. What follows is a relatively simple one. The reader is encouraged to develop the strategy that best fits the application.

In most of the non-blocking operations in this class, we let the Selector tell us when we're ready to attempt an I/O operation (by the application repeatedly calling our methods). Another option would be to attempt the operation and return from the method when no forward progress can be made.

There's lots of room for enhancements and improvement in this example.

We're checking for SSL/TLS end-of-stream truncation attacks via sslEngine.closeInbound(). When you reach the end of a input stream via a read() returning -1 or an IOException, we call sslEngine.closeInbound() to signal to the sslEngine that no more input will be available. If the peer's close_notify message has not yet been received, this could indicate a trucation attack, in which an attacker is trying to prematurely close the connection. The closeInbound() will throw an exception if this condition were present.

Author
Brad R. Wetmore
Mark Reinhold

Definition at line 104 of file ChannelIOSecure.java.

Constructor & Destructor Documentation

◆ ChannelIOSecure()

ChannelIOSecure.ChannelIOSecure ( SocketChannel  sc,
boolean  blocking,
SSLContext  sslc 
) throws IOException
protected

Definition at line 156 of file ChannelIOSecure.java.

References initialHSComplete, initialHSStatus, inNetBB, netBBSize, outNetBB, ChannelIO.sc, and sslEngine.

Referenced by getInstance().

Here is the caller graph for this function:

Member Function Documentation

◆ dataFlush()

boolean ChannelIOSecure.dataFlush ( ) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 592 of file ChannelIOSecure.java.

References doWrite(), fileChannelBB, outNetBB, and tryFlush().

Here is the call graph for this function:

◆ doHandshake() [1/2]

boolean ChannelIOSecure.doHandshake ( ) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 235 of file ChannelIOSecure.java.

◆ doHandshake() [2/2]

boolean ChannelIOSecure.doHandshake ( SelectionKey  sk) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 256 of file ChannelIOSecure.java.

References appBBSize, doTasks(), hsBB, initialHSComplete, initialHSStatus, inNetBB, netBBSize, outNetBB, ChannelIO.requestBB, resizeRequestBB(), resizeResponseBB(), result, ChannelIO.sc, sslEngine, and tryFlush().

Here is the call graph for this function:

◆ doTasks()

SSLEngineResult.HandshakeStatus ChannelIOSecure.doTasks ( )
private

Definition at line 408 of file ChannelIOSecure.java.

References sslEngine.

Referenced by doHandshake(), doWrite(), and read().

Here is the caller graph for this function:

◆ doWrite()

int ChannelIOSecure.doWrite ( ByteBuffer  src) throws IOException
private

Definition at line 508 of file ChannelIOSecure.java.

References doTasks(), outNetBB, result, sslEngine, and tryFlush().

Referenced by dataFlush(), transferTo(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getInstance()

static ChannelIOSecure ChannelIOSecure.getInstance ( SocketChannel  sc,
boolean  blocking,
SSLContext  sslc 
) throws IOException
staticpackage

Definition at line 187 of file ChannelIOSecure.java.

References appBBSize, ChannelIOSecure(), ChannelIO.requestBB, ChannelIO.sc, and sslEngine.

Referenced by AcceptHandler.handle(), Acceptor.run(), B1.runServer(), BN.runServer(), and BP.runServer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

int ChannelIOSecure.read ( ) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 431 of file ChannelIOSecure.java.

References appBBSize, doTasks(), initialHSComplete, inNetBB, netBBSize, ChannelIO.requestBB, resizeRequestBB(), resizeResponseBB(), result, ChannelIO.sc, and sslEngine.

Here is the call graph for this function:

◆ resizeRequestBB()

void ChannelIOSecure.resizeRequestBB ( )
protected

Definition at line 205 of file ChannelIOSecure.java.

References appBBSize.

Referenced by doHandshake(), and read().

Here is the caller graph for this function:

◆ resizeResponseBB()

void ChannelIOSecure.resizeResponseBB ( )
private

Definition at line 212 of file ChannelIOSecure.java.

References inNetBB, and netBBSize.

Referenced by doHandshake(), and read().

Here is the caller graph for this function:

◆ shutdown()

boolean ChannelIOSecure.shutdown ( ) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 613 of file ChannelIOSecure.java.

References hsBB, outNetBB, result, shutdown, sslEngine, and tryFlush().

Here is the call graph for this function:

◆ transferTo()

long ChannelIOSecure.transferTo ( FileChannel  fc,
long  pos,
long  len 
) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 562 of file ChannelIOSecure.java.

References appBBSize, doWrite(), fileChannelBB, and initialHSComplete.

Here is the call graph for this function:

◆ tryFlush()

boolean ChannelIOSecure.tryFlush ( ByteBuffer  bb) throws IOException
private

Definition at line 224 of file ChannelIOSecure.java.

Referenced by dataFlush(), doHandshake(), doWrite(), and shutdown().

Here is the caller graph for this function:

◆ write()

int ChannelIOSecure.write ( ByteBuffer  src) throws IOException
package

Reimplemented from ChannelIO.

Definition at line 491 of file ChannelIOSecure.java.

References doWrite(), and initialHSComplete.

Here is the call graph for this function:

Member Data Documentation

◆ appBBSize

int ChannelIOSecure.appBBSize
private

Definition at line 108 of file ChannelIOSecure.java.

Referenced by doHandshake(), getInstance(), read(), resizeRequestBB(), and transferTo().

◆ fileChannelBB

ByteBuffer ChannelIOSecure.fileChannelBB = null
private

Definition at line 133 of file ChannelIOSecure.java.

Referenced by dataFlush(), and transferTo().

◆ hsBB

ByteBuffer ChannelIOSecure.hsBB = ByteBuffer.allocate(0)
staticprivate

Definition at line 128 of file ChannelIOSecure.java.

Referenced by doHandshake(), and shutdown().

◆ initialHSComplete

boolean ChannelIOSecure.initialHSComplete
private

Definition at line 145 of file ChannelIOSecure.java.

Referenced by ChannelIOSecure(), doHandshake(), read(), transferTo(), and write().

◆ initialHSStatus

HandshakeStatus ChannelIOSecure.initialHSStatus
private

Definition at line 144 of file ChannelIOSecure.java.

Referenced by ChannelIOSecure(), and doHandshake().

◆ inNetBB

ByteBuffer ChannelIOSecure.inNetBB
private

Definition at line 120 of file ChannelIOSecure.java.

Referenced by ChannelIOSecure(), doHandshake(), read(), and resizeResponseBB().

◆ netBBSize

int ChannelIOSecure.netBBSize
private

Definition at line 109 of file ChannelIOSecure.java.

Referenced by ChannelIOSecure(), doHandshake(), read(), and resizeResponseBB().

◆ outNetBB

ByteBuffer ChannelIOSecure.outNetBB
private

Definition at line 121 of file ChannelIOSecure.java.

Referenced by ChannelIOSecure(), dataFlush(), doHandshake(), doWrite(), and shutdown().

◆ shutdown

boolean ChannelIOSecure.shutdown = false
private

Definition at line 151 of file ChannelIOSecure.java.

Referenced by shutdown().

◆ sslEngine

SSLEngine ChannelIOSecure.sslEngine = null
private

The documentation for this class was generated from the following file: