org.apache.mina.filter.support

Class SSLHandler


public class SSLHandler
extends java.lang.Object

A helper class using the SSLEngine API to decrypt/encrypt data.

Each connection has a SSLEngine that is used through the lifetime of the connection. We allocate byte buffers for use as the outbound and inbound network buffers. These buffers handle all of the intermediary data for the SSL connection. To make things easy, we'll require outNetBuffer be completely flushed before trying to wrap any more data.

Constructor Summary

SSLHandler(SSLFilter parent, SSLContext sslc, IoSession session)
Constuctor.

Method Summary

boolean
closeOutbound()
Start SSL shutdown process.
static ByteBuffer
copy(ByteBuffer src)
Creates a new Mina byte buffer that is a deep copy of the remaining bytes in the given buffer (between index buf.position() and buf.limit())
void
destroy()
Release allocated ByteBuffers.
void
encrypt(ByteBuffer src)
Encrypt provided buffer.
void
flushPreHandshakeEvents()
void
flushScheduledEvents()
ByteBuffer
getAppBuffer()
Get decrypted application data.
ByteBuffer
getOutNetBuffer()
Get encrypted data to be sent.
SSLFilter
getParent()
IoSession
getSession()
void
handshake(IoFilter.NextFilter nextFilter)
Perform any handshaking processing.
void
init()
boolean
isHandshakeComplete()
Check if handshake is completed.
boolean
isInboundDone()
boolean
isOutboundDone()
boolean
isWritingEncryptedData()
Check we are writing encrypted data.
void
messageReceived(IoFilter.NextFilter nextFilter, ByteBuffer buf)
Call when data read from net.
boolean
needToCompleteHandshake()
Check if there is any need to complete handshake.
void
scheduleFilterWrite(IoFilter.NextFilter nextFilter, IoFilter.WriteRequest writeRequest)
void
scheduleMessageReceived(IoFilter.NextFilter nextFilter, Object message)
void
schedulePreHandshakeWriteRequest(IoFilter.NextFilter nextFilter, IoFilter.WriteRequest writeRequest)
WriteFuture
writeNetBuffer(IoFilter.NextFilter nextFilter)

Constructor Details

SSLHandler

public SSLHandler(SSLFilter parent,
                  SSLContext sslc,
                  IoSession session)
            throws SSLException
Constuctor.
Parameters:
sslc -

Method Details

closeOutbound

public boolean closeOutbound()
            throws SSLException
Start SSL shutdown process.
Returns:
true if shutdown process is started. false if shutdown process is already finished.

copy

public static ByteBuffer copy(ByteBuffer src)
Creates a new Mina byte buffer that is a deep copy of the remaining bytes in the given buffer (between index buf.position() and buf.limit())
Parameters:
src - the buffer to copy
Returns:
the new buffer, ready to read from

destroy

public void destroy()
Release allocated ByteBuffers.

encrypt

public void encrypt(ByteBuffer src)
            throws SSLException
Encrypt provided buffer. Encytpted data reurned by getOutNetBuffer().
Parameters:
src - data to encrypt

flushPreHandshakeEvents

public void flushPreHandshakeEvents()
            throws SSLException

flushScheduledEvents

public void flushScheduledEvents()

getAppBuffer

public ByteBuffer getAppBuffer()
Get decrypted application data.
Returns:
buffer with data

getOutNetBuffer

public ByteBuffer getOutNetBuffer()
Get encrypted data to be sent.
Returns:
buffer with data

getParent

public SSLFilter getParent()

getSession

public IoSession getSession()

handshake

public void handshake(IoFilter.NextFilter nextFilter)
            throws SSLException
Perform any handshaking processing.

init

public void init()
            throws SSLException

isHandshakeComplete

public boolean isHandshakeComplete()
Check if handshake is completed.

isInboundDone

public boolean isInboundDone()

isOutboundDone

public boolean isOutboundDone()

isWritingEncryptedData

public boolean isWritingEncryptedData()
Check we are writing encrypted data.

messageReceived

public void messageReceived(IoFilter.NextFilter nextFilter,
                            ByteBuffer buf)
            throws SSLException
Call when data read from net. Will perform inial hanshake or decrypt provided Buffer. Decrytpted data reurned by getAppBuffer(), if any.
Parameters:
buf - buffer to decrypt

needToCompleteHandshake

public boolean needToCompleteHandshake()
Check if there is any need to complete handshake.

scheduleFilterWrite

public void scheduleFilterWrite(IoFilter.NextFilter nextFilter,
                                IoFilter.WriteRequest writeRequest)

scheduleMessageReceived

public void scheduleMessageReceived(IoFilter.NextFilter nextFilter,
                                    Object message)

schedulePreHandshakeWriteRequest

public void schedulePreHandshakeWriteRequest(IoFilter.NextFilter nextFilter,
                                             IoFilter.WriteRequest writeRequest)

writeNetBuffer

public WriteFuture writeNetBuffer(IoFilter.NextFilter nextFilter)
            throws SSLException