Class TransportProtocol<T extends SshContext>

java.lang.Object
com.sshtools.common.ssh.ExecutorOperationSupport<SshContext>
com.sshtools.synergy.ssh.TransportProtocol<T>
All Implemented Interfaces:
IdleStateListener, ProtocolEngine, SshTransport<T>

public abstract class TransportProtocol<T extends SshContext> extends ExecutorOperationSupport<SshContext> implements ProtocolEngine, IdleStateListener, SshTransport<T>
This class implements the SSH Transport Protocol for the SSHD server.
  • Field Details

    • CHARSET_ENCODING

      public static String CHARSET_ENCODING
      Character set encoding. All input/output strings created by the API are created with this encoding. The default is "UTF-8" and it may be changed, the results however are unknown.
    • localIdentification

      protected String localIdentification
    • remoteIdentification

      protected StringBuffer remoteIdentification
    • receivedRemoteIdentification

      protected boolean receivedRemoteIdentification
    • sentLocalIdentification

      protected boolean sentLocalIdentification
    • localkex

      protected byte[] localkex
    • remotekex

      protected byte[] remotekex
    • sessionIdentifier

      protected byte[] sessionIdentifier
    • uuid

      protected UUID uuid
    • hasExtensionCapability

      protected boolean hasExtensionCapability
    • enableExtensionCapability

      protected boolean enableExtensionCapability
    • activeService

      protected Service activeService
    • SSH_MSG_SERVICE_REQUEST

      protected static final int SSH_MSG_SERVICE_REQUEST
      See Also:
    • SSH_MSG_SERVICE_ACCEPT

      public static final int SSH_MSG_SERVICE_ACCEPT
      See Also:
    • SSH_MSG_EXT_INFO

      public static final int SSH_MSG_EXT_INFO
      See Also:
    • completedFirstKeyExchange

      protected boolean completedFirstKeyExchange
    • disconnectStarted

      protected Date disconnectStarted
    • NEGOTIATING_PROTOCOL

      public static final int NEGOTIATING_PROTOCOL
      Protocol state: Negotation of the protocol version
      See Also:
    • PERFORMING_KEYEXCHANGE

      public static final int PERFORMING_KEYEXCHANGE
      Protocol state: The protocol is performing key exchange
      See Also:
    • CONNECTED

      public static final int CONNECTED
      Protocol state: The transport protocol is connected and services can be started or may already be active.
      See Also:
    • DISCONNECTED

      public static final int DISCONNECTED
      Protocol state: The transport protocol has disconnected.
      See Also:
    • hostKey

      protected SshPublicKey hostKey
    • cipherCS

      protected String cipherCS
    • cipherSC

      protected String cipherSC
    • macCS

      protected String macCS
    • macSC

      protected String macSC
    • compressionCS

      protected String compressionCS
    • compressionSC

      protected String compressionSC
    • keyExchangeAlgorithm

      protected String keyExchangeAlgorithm
    • publicKey

      protected String publicKey
    • con

      protected Connection<T extends SshContext> con
    • HOST_NOT_ALLOWED

      public static final int HOST_NOT_ALLOWED
      Disconnect reason: The host is not allowed
      See Also:
    • PROTOCOL_ERROR

      public static final int PROTOCOL_ERROR
      Disconnect reason: A protocol error occurred
      See Also:
    • KEY_EXCHANGE_FAILED

      public static final int KEY_EXCHANGE_FAILED
      Disconnect reason: Key exchange failed
      See Also:
    • RESERVED

      public static final int RESERVED
      Disconnect reason: Reserved
      See Also:
    • MAC_ERROR

      public static final int MAC_ERROR
      Disconnect reason: An error occurred verifying the MAC
      See Also:
    • COMPRESSION_ERROR

      public static final int COMPRESSION_ERROR
      Disconnect reason: A compression error occurred
      See Also:
    • SERVICE_NOT_AVAILABLE

      public static final int SERVICE_NOT_AVAILABLE
      Disconnect reason: The requested service is not available
      See Also:
    • PROTOCOL_VERSION_NOT_SUPPORTED

      public static final int PROTOCOL_VERSION_NOT_SUPPORTED
      Disconnect reason: The protocol version is not supported
      See Also:
    • HOST_KEY_NOT_VERIFIABLE

      public static final int HOST_KEY_NOT_VERIFIABLE
      Disconnect reason: The host key supplied could not be verified
      See Also:
    • CONNECTION_LOST

      public static final int CONNECTION_LOST
      Disconnect reason: The connection was lost
      See Also:
    • BY_APPLICATION

      public static final int BY_APPLICATION
      Disconnect reason: The application disconnected
      See Also:
    • TOO_MANY_CONNECTIONS

      public static final int TOO_MANY_CONNECTIONS
      Disconnect reason: Too many connections, try later
      See Also:
    • AUTH_CANCELLED_BY_USER

      public static final int AUTH_CANCELLED_BY_USER
      Disconnect reason: Authentication was cancelled
      See Also:
    • NO_MORE_AUTH_METHODS_AVAILABLE

      public static final int NO_MORE_AUTH_METHODS_AVAILABLE
      Disconnect reason: No more authentication methods are available
      See Also:
    • ILLEGAL_USER_NAME

      public static final int ILLEGAL_USER_NAME
      Disconnect reason: The user's name is illegal
      See Also:
    • sshContext

      protected T extends SshContext sshContext
    • socketConnection

      protected SocketConnection socketConnection
    • lock

      public static Object lock
  • Constructor Details

    • TransportProtocol

      public TransportProtocol(T sshContext, ConnectRequestFuture connectFuture)
      Create a default transport protocol instance in CLIENT_MODE.
      Throws:
      IOException
  • Method Details

    • transferState

      protected void transferState(TransportProtocol<? extends SshContext> transport)
    • getConnectFuture

      public ConnectRequestFuture getConnectFuture()
      Specified by:
      getConnectFuture in interface ProtocolEngine
    • getDisconnectFuture

      public DisconnectRequestFuture getDisconnectFuture()
      Specified by:
      getDisconnectFuture in interface ProtocolEngine
    • getSocketConnection

      public SocketConnection getSocketConnection()
      Description copied from interface: ProtocolEngine
      Get the SocketConnection for this connection.
      Specified by:
      getSocketConnection in interface ProtocolEngine
      Returns:
      SocketConnection
    • addEventListener

      public void addEventListener(TransportProtocolListener listener)
    • getRemoteAddress

      public SocketAddress getRemoteAddress()
    • getRemotePort

      public int getRemotePort()
      Returns the remote port of the connected socket.
      Returns:
      int
    • getContext

      public T getContext()
      Specified by:
      getContext in interface ProtocolEngine
      Specified by:
      getContext in interface SshTransport<T extends SshContext>
      Specified by:
      getContext in class ExecutorOperationSupport<SshContext>
    • getConnection

      public Connection<T> getConnection()
      Specified by:
      getConnection in interface ProtocolEngine
    • canConnect

      protected abstract boolean canConnect(SocketConnection connection)
    • onConnected

      protected abstract void onConnected()
    • onDisconnected

      protected abstract void onDisconnected()
    • onSocketConnect

      public void onSocketConnect(SocketConnection connection)
      Description copied from interface: ProtocolEngine
      The socket is connected and the protocol can now start.
      Specified by:
      onSocketConnect in interface ProtocolEngine
      Parameters:
      connection - SocketConnection
    • canSendKeyExchangeInit

      protected boolean canSendKeyExchangeInit()
    • onSocketRead

      public boolean onSocketRead(ByteBuffer incomingData)
      Called when the socket channel is reported to be ready for reading.
      Specified by:
      onSocketRead in interface ProtocolEngine
      Parameters:
      incomingData - ByteBuffer
      Returns:
      boolean to determine if protocol wants to write to the socket
    • isDisonnecting

      public boolean isDisonnecting()
      Get if disconnecting has started but not yet complete.
      Returns:
      disconnecting
    • isConnected

      public boolean isConnected()
      Determine if the protocol is still connected
      Specified by:
      isConnected in interface ProtocolEngine
      Returns:
      boolean
    • processNegotiationString

      protected void processNegotiationString(String value) throws IOException
      Throws:
      IOException
    • startBinaryProtocol

      protected boolean startBinaryProtocol()
    • onRemoteIdentificationReceived

      protected void onRemoteIdentificationReceived(String remoteIdentification)
    • wantsToWrite

      public boolean wantsToWrite()
      Description copied from interface: ProtocolEngine
      Determines whether the protocol wants to write to the socket. The value of this method determines the write state of the socket. Only return a true value when the protocol needs to write data to the socket.
      Specified by:
      wantsToWrite in interface ProtocolEngine
      Returns:
      boolean
    • getQueueSizes

      public int getQueueSizes()
    • idle

      public boolean idle()
      Called when the selector framework is idle. We take the opportunity to send an SSH_MSG_IGNORE message in the hope that we can detect any sockets that may have closed.
      Specified by:
      idle in interface IdleStateListener
    • onSocketWrite

      public SocketWriteCallback onSocketWrite(ByteBuffer outgoingMessage)
      Called when the socket channel is reported to be ready for writing.
      Specified by:
      onSocketWrite in interface ProtocolEngine
      Parameters:
      outgoingMessage - ByteBuffer
    • getState

      public int getState()
    • getLocalAddress

      public SocketAddress getLocalAddress()
      Returns the local address to which the remote socket is connected.
      Returns:
      InetAddress
    • getLocalPort

      public int getLocalPort()
      Returns the local port to which the remote socket is connected.
      Returns:
      int
    • getRemoteIdentification

      public String getRemoteIdentification()
    • getUUID

      public String getUUID()
    • disconnect

      public void disconnect(int reason, String description)
      Disconnect from the remote host. No more messages can be sent after this method has been called.
      Specified by:
      disconnect in interface ProtocolEngine
      Specified by:
      disconnect in interface SshTransport<T extends SshContext>
      Parameters:
      reason -
      description -
      Throws:
      IOException
    • onSocketClose

      public void onSocketClose()
      Disconnects everything internally
      Specified by:
      onSocketClose in interface ProtocolEngine
    • getRND

      public SecureRandom getRND()
      Gets the secure random number generator for this transport.
      Returns:
      the secure RND
    • initializeKeyExchange

      protected abstract void initializeKeyExchange(SshKeyExchange<T> keyExchange, boolean firstPacketFollows, boolean useFirstPacket) throws IOException, SshException
      Throws:
      IOException
      SshException
    • getExtensionNegotiationString

      protected abstract String getExtensionNegotiationString()
    • isExtensionNegotiationSupported

      protected abstract boolean isExtensionNegotiationSupported()
    • onKeyExchangeInit

      protected abstract void onKeyExchangeInit() throws SshException
      Throws:
      SshException
    • keyExchangeInitialized

      protected void keyExchangeInitialized()
    • disconnected

      protected abstract void disconnected()
    • onNewKeysReceived

      protected abstract void onNewKeysReceived()
    • processTransportMessage

      protected abstract boolean processTransportMessage(int msgid, byte[] msg) throws IOException, SshException
      Throws:
      IOException
      SshException
    • processMessage

      public void processMessage(byte[] msg, long sequenceNo) throws SshException, IOException, WriteOperationRequest
      Process a message. This should be called when reading messages from outside of the transport protocol so that the transport protocol can parse its own messages.
      Parameters:
      msg -
      Throws:
      IOException
      SshException
      WriteOperationRequest
    • onNewKeysSent

      protected abstract void onNewKeysSent()
    • sendNewKeys

      public void sendNewKeys()
      Specified by:
      sendNewKeys in interface SshTransport<T extends SshContext>
    • getSshContext

      public T getSshContext()
    • selectNegotiatedComponent

      protected String selectNegotiatedComponent(String clientlist, String serverlist) throws IOException
      Throws:
      IOException
    • onKeyExchangeComplete

      protected void onKeyExchangeComplete()
    • completeKeyExchange

      protected void completeKeyExchange(SshKeyExchange<T> keyExchange)
    • generateNewKeysServerOut

      protected void generateNewKeysServerOut()
    • generateNewKeysServerIn

      protected void generateNewKeysServerIn()
    • generateNewKeysClientOut

      protected void generateNewKeysClientOut()
    • generateNewKeysClientIn

      protected void generateNewKeysClientIn()
    • isServerMode

      protected abstract boolean isServerMode()
    • getCipherCS

      public String getCipherCS()
    • getCipherSC

      public String getCipherSC()
    • getMacCS

      public String getMacCS()
    • getMacSC

      public String getMacSC()
    • getCompressionCS

      public String getCompressionCS()
    • getCompressionSC

      public String getCompressionSC()
    • postMessage

      public void postMessage(SshMessage msg)
      Specified by:
      postMessage in interface SshTransport<T extends SshContext>
    • postMessage

      public void postMessage(SshMessage msg, boolean kex)
      Specified by:
      postMessage in interface SshTransport<T extends SshContext>
    • getSessionKey

      public byte[] getSessionKey()
    • getBytes

      public static byte[] getBytes(String str, String charset)
    • kill

      public void kill()
    • getHostKeyAlgorithm

      public String getHostKeyAlgorithm()
    • getHostKey

      public SshPublicKey getHostKey()
    • getKeyExchangeAlgorithm

      public String getKeyExchangeAlgorithm()
    • getRemoteKeyExchanges

      public String[] getRemoteKeyExchanges()
    • getRemotePublicKeys

      public String[] getRemotePublicKeys()
    • getRemoteCiphersCS

      public String[] getRemoteCiphersCS()
    • getRemoteCiphersSC

      public String[] getRemoteCiphersSC()
    • getRemoteMacsCS

      public String[] getRemoteMacsCS()
    • getRemoteMacsSC

      public String[] getRemoteMacsSC()
    • getRemoteCompressionsCS

      public String[] getRemoteCompressionsCS()
    • getRemoteCompressionsSC

      public String[] getRemoteCompressionsSC()
    • hasCompletedKeyExchange

      public boolean hasCompletedKeyExchange()
    • getExecutor

      public ExecutorOperationSupport<?> getExecutor()
      Specified by:
      getExecutor in interface ProtocolEngine
    • registerIdleStateListener

      public void registerIdleStateListener(IdleStateListener listener)
    • removeIdleStateListener

      public void removeIdleStateListener(IdleStateListener listener)
    • resetIdleState

      public void resetIdleState(IdleStateListener listener)
    • isSelectorThread

      public boolean isSelectorThread()
    • getKeyExchangeInUse

      public String getKeyExchangeInUse()
    • getHostKeyInUse

      public String getHostKeyInUse()
    • getLocalIdentification

      public String getLocalIdentification()
    • getAuthenticatedFuture

      public AuthenticatedFuture getAuthenticatedFuture()