Class SslHandler


  • class SslHandler
    extends java.lang.Object
    No qualifier
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
        A logger for this class
      • sslFilter

        private final SslFilter sslFilter
        The SSL Filter which has created this handler
      • session

        private final IoSession session
        The current session
      • preHandshakeEventQueue

        private final java.util.Queue<IoFilterEvent> preHandshakeEventQueue
      • filterWriteEventQueue

        private final java.util.Queue<IoFilterEvent> filterWriteEventQueue
      • messageReceivedEventQueue

        private final java.util.Queue<IoFilterEvent> messageReceivedEventQueue
        A queue used to stack all the incoming data until the SSL session is established
      • sslEngine

        private javax.net.ssl.SSLEngine sslEngine
      • inNetBuffer

        private IoBuffer inNetBuffer
        Encrypted data from the net
      • outNetBuffer

        private IoBuffer outNetBuffer
        Encrypted data to be written to the net
      • appBuffer

        private IoBuffer appBuffer
        Application cleartext data to be read by application
      • emptyBuffer

        private final IoBuffer emptyBuffer
        Empty buffer used during initial handshake and close operations
      • handshakeStatus

        private javax.net.ssl.SSLEngineResult.HandshakeStatus handshakeStatus
      • firstSSLNegociation

        private boolean firstSSLNegociation
        A flag set to true when the first SSL handshake has been completed This is used to avoid sending a notification to the application handler when we switch to a SECURE or UNSECURE session.
      • handshakeComplete

        private boolean handshakeComplete
        A flag set to true when a SSL Handshake has been completed
      • writingEncryptedData

        private boolean writingEncryptedData
        A flag used to indicate to the SslFilter that the buffer it will write is already encrypted (this will be the case for data being produced during the handshake).
    • Constructor Detail

      • SslHandler

        SslHandler​(SslFilter sslFilter,
                   IoSession session)
        Create a new SSL Handler, and initialize it.
        Parameters:
        sslContext -
        Throws:
        javax.net.ssl.SSLException
    • Method Detail

      • init

        void init()
           throws javax.net.ssl.SSLException
        Initialize the SSL handshake.
        Throws:
        javax.net.ssl.SSLException - If the underlying SSLEngine handshake initialization failed
      • destroy

        void destroy()
        Release allocated buffers.
      • getSslFilter

        SslFilter getSslFilter()
        Returns:
        The SSL filter which has created this handler
      • isWritingEncryptedData

        boolean isWritingEncryptedData()
        Check if we are writing encrypted data.
      • isHandshakeComplete

        boolean isHandshakeComplete()
        Check if handshake is completed.
      • notHandshaking

        boolean notHandshaking()
        Check if handshake is on going.
      • isInboundDone

        boolean isInboundDone()
      • isOutboundDone

        boolean isOutboundDone()
      • needToCompleteHandshake

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

        void flushPreHandshakeEvents()
                              throws javax.net.ssl.SSLException
        Throws:
        javax.net.ssl.SSLException
      • flushFilterWrite

        void flushFilterWrite()
      • scheduleMessageReceived

        void scheduleMessageReceived​(IoFilter.NextFilter nextFilter,
                                     java.lang.Object message)
        Push the newly received data into a queue, waiting for the SSL session to be fully established
        Parameters:
        nextFilter - The next filter to call
        message - The incoming data
      • flushMessageReceived

        void flushMessageReceived()
      • messageReceived

        void messageReceived​(IoFilter.NextFilter nextFilter,
                             java.nio.ByteBuffer buf)
                      throws javax.net.ssl.SSLException
        Call when data are read from net. It will perform the initial hanshake or decrypt the data if SSL has been initialiaed.
        Parameters:
        buf - buffer to decrypt
        nextFilter - Next filter in chain
        Throws:
        javax.net.ssl.SSLException - on errors
      • fetchAppBuffer

        IoBuffer fetchAppBuffer()
        Get decrypted application data.
        Returns:
        buffer with data
      • fetchOutNetBuffer

        IoBuffer fetchOutNetBuffer()
        Get encrypted data to be sent.
        Returns:
        buffer with data
      • encrypt

        void encrypt​(java.nio.ByteBuffer src)
              throws javax.net.ssl.SSLException
        Encrypt provided buffer. Encrypted data returned by getOutNetBuffer().
        Parameters:
        src - data to encrypt
        Throws:
        javax.net.ssl.SSLException - on errors
      • closeOutbound

        boolean closeOutbound()
                       throws javax.net.ssl.SSLException
        Start SSL shutdown process.
        Returns:
        true if shutdown process is started. false if shutdown process is already finished.
        Throws:
        javax.net.ssl.SSLException - on errors
      • checkStatus

        private void checkStatus​(javax.net.ssl.SSLEngineResult res)
                          throws javax.net.ssl.SSLException
        Parameters:
        res -
        Throws:
        javax.net.ssl.SSLException
      • handshake

        void handshake​(IoFilter.NextFilter nextFilter)
                throws javax.net.ssl.SSLException
        Perform any handshaking processing.
        Throws:
        javax.net.ssl.SSLException
      • createOutNetBuffer

        private void createOutNetBuffer​(int expectedRemaining)
      • unwrapHandshake

        private javax.net.ssl.SSLEngineResult.Status unwrapHandshake​(IoFilter.NextFilter nextFilter)
                                                              throws javax.net.ssl.SSLException
        Throws:
        javax.net.ssl.SSLException
      • renegotiateIfNeeded

        private void renegotiateIfNeeded​(IoFilter.NextFilter nextFilter,
                                         javax.net.ssl.SSLEngineResult res)
                                  throws javax.net.ssl.SSLException
        Throws:
        javax.net.ssl.SSLException
      • unwrap

        private javax.net.ssl.SSLEngineResult unwrap()
                                              throws javax.net.ssl.SSLException
        Decrypt the incoming buffer and move the decrypted data to an application buffer.
        Throws:
        javax.net.ssl.SSLException
      • doTasks

        private javax.net.ssl.SSLEngineResult.HandshakeStatus doTasks()
        Do all the outstanding handshake tasks in the current Thread.
      • copy

        static IoBuffer copy​(java.nio.ByteBuffer src)
        Creates a new MINA 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
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • release

        void release()
        Free the allocated buffers