Class Handshake

  • All Implemented Interfaces:
    java.lang.Runnable, Negotiation, Operation

    class Handshake
    extends java.lang.Object
    implements Negotiation
    The Handshake object is used to perform secure SSL negotiations on a pipeline or Transport. This can be used to perform an SSL handshake. To perform the negotiation this uses an SSL engine provided with the transport to direct the conversation. The SSL engine tells the negotiation what is expected next, whether this is a response to the client or a message from it. During the negotiation this may need to wait for either a write ready event or a read ready event. Event notification is done using the processor provided.
    See Also:
    TransportProcessor
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private class  Handshake.Committer
      The Committer task is used to transfer the transport created to the processor.
      private class  Handshake.Consumer
      The Consumer task is used to schedule the negotiation for a read operation.
      private class  Handshake.Producer
      The Producer is used to schedule the negotiation for a write operation.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.channels.SocketChannel channel
      This is the socket channel used to read and write data to.
      private boolean client
      This determines if the handshake is from the client side.
      private java.nio.ByteBuffer empty
      This is an empty byte buffer used to generate a response.
      private javax.net.ssl.SSLEngine engine
      This is the SSL engine used to direct the conversation.
      private java.nio.ByteBuffer input
      This is the input buffer used to read data from the socket.
      private java.nio.ByteBuffer output
      This is the output buffer used to generate data to.
      private TransportProcessor processor
      This is the processor used to process the secure transport.
      private Reactor reactor
      This is the reactor used to register for I/O notifications.
      private NegotiationState state
      This is the certificate associated with this negotiation.
      private Trace trace
      This is the trace that is used to monitor handshake events.
      private Transport transport
      This is the transport dispatched when the negotiation ends.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void begin()
      This is used to start the negotation.
      void cancel()
      This is used to terminate the negotiation.
      void commit()
      This method is invoked when the negotiation is done and the next phase of the connection is to take place.
      private void complete()
      This is used to execute the completion task after a challenge for the clients X509 certificate.
      private void dispatch()
      This method is invoked when the negotiation is done and the next phase of the connection is to take place.
      private PhaseType exchange()
      This is the main point of execution within the negotiation.
      private void execute()
      This is used to execute the delegated tasks.
      java.nio.channels.SelectableChannel getChannel()
      This returns the socket channel for the connected pipeline.
      Trace getTrace()
      This is used to acquire the trace object that is associated with the operation.
      private java.lang.Runnable process()
      This is the main point of execution within the negotiation.
      private PhaseType read()
      This is used to perform the read part of the negotiation.
      private PhaseType read​(int count)
      This is used to perform the read part of the negotiation.
      boolean receive()
      This is used to receive data from the client.
      void resume()
      This is the main point of execution within the negotiation.
      void run()
      This is used to start the negotiation.
      boolean send()
      Here we attempt to send all data within the output buffer.
      private void terminate()
      This method is used to terminate the handshake.
      private PhaseType write()
      This is used to perform the write part of the negotiation.
      private PhaseType write​(int count)
      This is used to perform the write part of the negotiation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • processor

        private final TransportProcessor processor
        This is the processor used to process the secure transport.
      • state

        private final NegotiationState state
        This is the certificate associated with this negotiation.
      • channel

        private final java.nio.channels.SocketChannel channel
        This is the socket channel used to read and write data to.
      • transport

        private final Transport transport
        This is the transport dispatched when the negotiation ends.
      • reactor

        private final Reactor reactor
        This is the reactor used to register for I/O notifications.
      • output

        private final java.nio.ByteBuffer output
        This is the output buffer used to generate data to.
      • input

        private final java.nio.ByteBuffer input
        This is the input buffer used to read data from the socket.
      • empty

        private final java.nio.ByteBuffer empty
        This is an empty byte buffer used to generate a response.
      • engine

        private final javax.net.ssl.SSLEngine engine
        This is the SSL engine used to direct the conversation.
      • trace

        private final Trace trace
        This is the trace that is used to monitor handshake events.
      • client

        private final boolean client
        This determines if the handshake is from the client side.
    • Constructor Detail

      • Handshake

        public Handshake​(TransportProcessor processor,
                         Transport transport,
                         Reactor reactor)
        Constructor for the Handshake object. This is used to create an operation capable of performing negotiations for SSL connections. Typically this is used to perform request response negotiations, such as a handshake or termination.
        Parameters:
        processor - the processor used to dispatch the transport
        transport - the transport to perform the negotiation for
        reactor - this is the reactor used for I/O notifications
      • Handshake

        public Handshake​(TransportProcessor processor,
                         Transport transport,
                         Reactor reactor,
                         int size)
        Constructor for the Handshake object. This is used to create an operation capable of performing negotiations for SSL connections. Typically this is used to perform request response negotiations, such as a handshake or termination.
        Parameters:
        transport - the transport to perform the negotiation for
        processor - the processor used to dispatch the transport
        reactor - this is the reactor used for I/O notifications
        size - the size of the buffers used for the negotiation
      • Handshake

        public Handshake​(TransportProcessor processor,
                         Transport transport,
                         Reactor reactor,
                         boolean client)
        Constructor for the Handshake object. This is used to create an operation capable of performing negotiations for SSL connections. Typically this is used to perform request response negotiations, such as a handshake or termination.
        Parameters:
        transport - the transport to perform the negotiation for
        processor - the processor used to dispatch the transport
        reactor - this is the reactor used for I/O notifications
        client - determines the side of the SSL handshake
      • Handshake

        public Handshake​(TransportProcessor processor,
                         Transport transport,
                         Reactor reactor,
                         int size,
                         boolean client)
        Constructor for the Handshake object. This is used to create an operation capable of performing negotiations for SSL connections. Typically this is used to perform request response negotiations, such as a handshake or termination.
        Parameters:
        transport - the transport to perform the negotiation for
        processor - the processor used to dispatch the transport
        reactor - this is the reactor used for I/O notifications
        size - the size of the buffers used for the negotiation
        client - determines the side of the SSL handshake
    • Method Detail

      • getTrace

        public Trace getTrace()
        This is used to acquire the trace object that is associated with the operation. A trace object is used to collection details on what operations are being performed. For instance it may contain information relating to I/O events or errors.
        Specified by:
        getTrace in interface Operation
        Returns:
        this returns the trace associated with this operation
      • getChannel

        public java.nio.channels.SelectableChannel getChannel()
        This returns the socket channel for the connected pipeline. It is this channel that is used to determine if there are bytes that can be read. When closed this is no longer selectable.
        Specified by:
        getChannel in interface Operation
        Returns:
        this returns the connected channel for the pipeline
      • run

        public void run()
        This is used to start the negotiation. Once started this will send a message to the other side, once sent the negotiation reads the response. However if the response is not yet ready this will schedule the negotiation for a selectable operation ensuring that it can resume execution when ready.
        Specified by:
        run in interface java.lang.Runnable
      • cancel

        public void cancel()
        This is used to terminate the negotiation. This is excecuted when the negotiation times out. When the negotiation expires it is rejected by the processor and must be canceled. Canceling is basically termination of the connection to free resources.
        Specified by:
        cancel in interface Operation
      • begin

        private void begin()
        This is used to start the negotation. Once started this will send a message to the other side, once sent the negotiation reads the response. However if the response is not yet ready this will schedule the negotiation for a selectable operation ensuring that it can resume execution when ready.
      • resume

        public void resume()
                    throws java.io.IOException
        This is the main point of execution within the negotiation. It is where the negotiation is performed. Negotiations are done by performing a request response flow, governed by the SSL engine associated with the pipeline. Typically the client is the one to initiate the handshake and the server initiates the termination sequence. This may be executed several times depending on whether reading or writing blocks.
        Specified by:
        resume in interface Negotiation
        Throws:
        java.io.IOException
      • process

        private java.lang.Runnable process()
                                    throws java.io.IOException
        This is the main point of execution within the negotiation. It is where the negotiation is performed. Negotiations are done by performing a request response flow, governed by the SSL engine associated with the transport. Typically the client is the one to initiate the handshake and the server initiates the termination sequence. This may be executed several times depending on whether reading or writing blocks.
        Returns:
        this returns a task used to execute the next phase
        Throws:
        java.io.IOException
      • exchange

        private PhaseType exchange()
                            throws java.io.IOException
        This is the main point of execution within the negotiation. It is where the negotiation is performed. Negotiations are done by performing a request response flow, governed by the SSL engine associated with the transport. Typically the client is the one to initiate the handshake and the server initiates the termination sequence. This may be executed several times depending on whether reading or writing blocks.
        Returns:
        this returns what is expected next in the negotiation
        Throws:
        java.io.IOException
      • read

        private PhaseType read()
                        throws java.io.IOException
        This is used to perform the read part of the negotiation. The read part is where the other side sends information where it is consumed and is used to determine what action to take. Typically it is the SSL engine that determines what action is to be taken depending on the data send from the other side.
        Returns:
        the next action that should be taken by the handshake
        Throws:
        java.io.IOException
      • read

        private PhaseType read​(int count)
                        throws java.io.IOException
        This is used to perform the read part of the negotiation. The read part is where the other side sends information where it is consumed and is used to determine what action to take. Typically it is the SSL engine that determines what action is to be taken depending on the data send from the other side.
        Parameters:
        count - this is the number of times a read can repeat
        Returns:
        the next action that should be taken by the handshake
        Throws:
        java.io.IOException
      • write

        private PhaseType write()
                         throws java.io.IOException
        This is used to perform the write part of the negotiation. The read part is where the this sends information to the other side and the other side interprets the data and determines what action to take. After a write the negotiation typically completes or waits for the next response from the other side.
        Returns:
        the next action that should be taken by the handshake
        Throws:
        java.io.IOException
      • write

        private PhaseType write​(int count)
                         throws java.io.IOException
        This is used to perform the write part of the negotiation. The read part is where the this sends information to the other side and the other side interprets the data and determines what action to take. After a write the negotiation typically completes or waits for the next response from the other side.
        Parameters:
        count - this is the number of times a read can repeat
        Returns:
        the next action that should be taken by the handshake
        Throws:
        java.io.IOException
      • execute

        private void execute()
                      throws java.io.IOException
        This is used to execute the delegated tasks. These tasks are used to digest the information received from the client in order to generate a response. This may need to execute several tasks from the associated SSL engine.
        Throws:
        java.io.IOException
      • receive

        public boolean receive()
                        throws java.io.IOException
        This is used to receive data from the client. If at any point during the negotiation a message is required that can not be read immediately this is used to asynchronously read the data when a select operation is signalled.
        Specified by:
        receive in interface Negotiation
        Returns:
        this returns true when the message has been read
        Throws:
        java.io.IOException
      • send

        public boolean send()
                     throws java.io.IOException
        Here we attempt to send all data within the output buffer. If all of the data is delivered to the other side then this will return true. If however there is content yet to be sent to the other side then this returns false, telling the negotiation that in order to resume it must attempt to send the content again after a write ready operation on the underlying socket.
        Specified by:
        send in interface Negotiation
        Returns:
        this returns true if all of the content is delivered
        Throws:
        java.io.IOException
      • dispatch

        private void dispatch()
                       throws java.io.IOException
        This method is invoked when the negotiation is done and the next phase of the connection is to take place. This will be invoked when the SSL handshake has completed and the new secure transport is to be handed to the processor.
        Throws:
        java.io.IOException
      • terminate

        private void terminate()
                        throws java.io.IOException
        This method is used to terminate the handshake. Termination typically occurs when there has been some error in the handshake or when there is a timeout on some event, such as waiting for for a read or write operation to occur. As a result the TCP channel is closed and any challenge future is cancelled.
        Throws:
        java.io.IOException
      • complete

        private void complete()
                       throws java.io.IOException
        This is used to execute the completion task after a challenge for the clients X509 certificate. Execution of the completion task in this way allows any challanger to be notified that the handshake has complete.
        Throws:
        java.io.IOException
      • commit

        public void commit()
                    throws java.io.IOException
        This method is invoked when the negotiation is done and the next phase of the connection is to take place. If a certificate challenge was issued then the completion task is executed, if this was the handshake for the initial connection a transport is created and handed to the processor.
        Specified by:
        commit in interface Negotiation
        Throws:
        java.io.IOException