com.sshtools.j2ssh.transport
Class TransportProtocolCommon

java.lang.Object
  |
  +--com.sshtools.j2ssh.transport.TransportProtocolCommon
All Implemented Interfaces:
java.lang.Runnable, TransportProtocol
Direct Known Subclasses:
TransportProtocolClient

public abstract class TransportProtocolCommon
extends java.lang.Object
implements TransportProtocol, java.lang.Runnable

The main transport protocol implementation. This abstract class provides the common functionality of both client and server implementations.

Version:
1.0
Author:
Lee David Painter ( lee@sshtools.com )

Field Summary
protected  java.lang.String clientIdent
          The identification string sent by the client
protected  com.sshtools.j2ssh.transport.SshMsgKexInit clientKexInit
          The key exchange init message sent by the client
protected  java.lang.Boolean completeOnNewKeys
          Indicates when either the remote or local side has completed key exchange
protected  byte[] exchangeHash
          The exchange hash output from key exchange
protected  byte[] hostKey
          The servers host key data
protected  HostKeyVerification hosts
          Description of the Field
protected  java.math.BigInteger k
          The secret value k produced during key exchange
protected  SshKeyExchange kex
          The key exchange engine
protected static Logger log
          The log4j log object
protected  SshMessageStore messageStore
          The transport layer's message store
protected  SshPublicKey pk
          The public key object used in host key verification
protected  SshConnectionProperties properties
          The connection properties for the current connection
 java.lang.String PROTOCOL_VERSION
          The protocol version supported
protected  java.lang.String serverIdent
          The identification string sent by the server
protected  com.sshtools.j2ssh.transport.SshMsgKexInit serverKexInit
          The key exchange init message sent by the server
protected  byte[] signature
          The servers signature supplied to verify the host key
 java.lang.String SOFTWARE_VERSION_COMMENTS
          The software version comments that are sent during protocol negotiation
 
Constructor Summary
TransportProtocolCommon(java.net.Socket socket, SshConnectionProperties properties, HostKeyVerification hosts)
          Constructor for the SshTransportProtocol object
 
Method Summary
protected  void beginKeyExchange()
          Sets the transport layer up for performing the key exchange, this is called when either a SSH_MSG_KEXINIT message is received or sent by either party
protected  void completeKeyExchange()
          Completes key exchange by creating keys from the exchange hash and puts them into use
protected  java.lang.String determineAlgorithm(java.util.List clientAlgorithms, java.util.List serverAlgorithms)
          Helper method to determine the first algorithm that appears in the client list that is also supported by the server
 void disconnect(java.lang.String description)
          Disconnects the connection by sending a disconnect message with the BY_APPLICAITON reason.
protected abstract  java.lang.String getDecryptionAlgorithm()
          Abstract method to determine the correct decryption algorithm to use This is found by iterating through the clients supported algorithm and selecting the first supported decryption method that the server also supports.
protected abstract  java.lang.String getEncryptionAlgorithm()
          Abstract method to determine the correct encryption algorithm to use This is found by iterating through the clients supported algorithm and selecting the first supported encryption method that the server also supports.
protected abstract  java.lang.String getInputStreamCompAlgortihm()
          Abtract method for the client/server implmentations to determine the compression algorithm for the input stream.
protected abstract  java.lang.String getInputStreamMacAlgorithm()
          Abtract method for the client/server implmentations to determine the message authentication algorithm for the input stream.
protected  java.lang.String getKexAlgorithm()
          Determines the correct key exchange algorithm to use
protected abstract  java.lang.String getLocalIdent()
          Abstract method to return the local identification string which is used in protocol negotiation and in computing the exchange hash.
protected abstract  com.sshtools.j2ssh.transport.SshMsgKexInit getLocalKexInit()
          Abstract method to get the local kex init msg which is used in computing the exchange hash.
protected abstract  java.lang.String getOutputStreamCompAlgorithm()
          Abtract method for the client/server implmentations to determine the compression algorithm for the output stream.
protected abstract  java.lang.String getOutputStreamMacAlgorithm()
          Abtract method for the client/server implmentations to determine the message authentication algorithm for the output stream.
protected abstract  java.lang.String getRemoteIdent()
          Abstract method to return the remote identification string which is used in protocol negotiation and in computing the exchange hash.
protected abstract  com.sshtools.j2ssh.transport.SshMsgKexInit getRemoteKexInit()
          Abstract method to get the remote kex init msg which is used in computing the exchange hash.
 TransportProtocolState getState()
          Gets the state attribute of the TransportProtocolCommon object
protected abstract  boolean onBeginKeyExchange(SshKeyExchange kex)
          Abstract method called when key exchange has begun
protected  void onChildThreadException(java.lang.Exception e)
          Called if an inputstream error occurs
protected  void onCorruptMac()
          This is called when a corrupt Mac has been received on the input stream.
protected  void onMessageData(int messageId, ByteArrayReader bar)
          Called by the framework when a new message is received
protected abstract  void onMessageReceived(SshMessage msg)
          Called by the framework when a new message is received.
 void registerMessage(java.lang.Integer messageId, java.lang.Class implementor, SshMessageStore store)
          Implements the TransportProtocol interface method to allow external SSH implementations to receive message notificaitons.
 void removeServiceThread(Service service)
           Removes thes service thread from the exception notification list.
protected  void reportDisconnect(java.lang.String reason)
           Reports a disconnect to service threads.
protected  void reportException(java.lang.Exception e)
           Reports an exception to service threads
 void run()
          Main processing method for the TransportProtocolCommon object
protected  void sendDisconnect(int reason, java.lang.String description)
          Sends a disconnect message
protected  void sendKeyExchangeInit()
          Sends the key exchange init message
 void sendMessage(SshMessage msg, java.lang.Object sender)
          Send an SSH message, if the state doen't allow it because of key exchange then the message is stored and sent as soon as the state changes
protected  void sendNewKeys()
          Sends the SSH_MSG_NEWKEYS message to indicate that new keys are now in operation
protected abstract  void setLocalIdent()
          Abstract method that requires a derived class to set value of the local identification string.
protected abstract  void setLocalKexInit(com.sshtools.j2ssh.transport.SshMsgKexInit msg)
          Abstract method to set the local kex init msg which is used in computing the exchange hash.
protected abstract  void setRemoteIdent(java.lang.String ident)
          Abstract method that requires a derived class to set value of the remote identification string.
protected abstract  void setRemoteKexInit(com.sshtools.j2ssh.transport.SshMsgKexInit msg)
          Abstract method to set the remote kex init msg which is used in computing the exchange hash.
 void setServiceThread(Service service)
           Sets a service thread so that it receives notifications of exceptions.
protected  void stop()
          Stops the transport layer
 void unregisterMessage(java.lang.Integer messageId, SshMessageStore store)
          Implements the TransportProtocol method to allow external SSH implementations to unregister a message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hosts

protected HostKeyVerification hosts
Description of the Field


messageStore

protected SshMessageStore messageStore
The transport layer's message store


log

protected static Logger log
The log4j log object


SOFTWARE_VERSION_COMMENTS

public final java.lang.String SOFTWARE_VERSION_COMMENTS
The software version comments that are sent during protocol negotiation

See Also:
Constant Field Values

PROTOCOL_VERSION

public final java.lang.String PROTOCOL_VERSION
The protocol version supported

See Also:
Constant Field Values

serverIdent

protected java.lang.String serverIdent
The identification string sent by the server


clientIdent

protected java.lang.String clientIdent
The identification string sent by the client


clientKexInit

protected com.sshtools.j2ssh.transport.SshMsgKexInit clientKexInit
The key exchange init message sent by the client


serverKexInit

protected com.sshtools.j2ssh.transport.SshMsgKexInit serverKexInit
The key exchange init message sent by the server


completeOnNewKeys

protected java.lang.Boolean completeOnNewKeys
Indicates when either the remote or local side has completed key exchange


exchangeHash

protected byte[] exchangeHash
The exchange hash output from key exchange


hostKey

protected byte[] hostKey
The servers host key data


signature

protected byte[] signature
The servers signature supplied to verify the host key


pk

protected SshPublicKey pk
The public key object used in host key verification


k

protected java.math.BigInteger k
The secret value k produced during key exchange


kex

protected SshKeyExchange kex
The key exchange engine


properties

protected SshConnectionProperties properties
The connection properties for the current connection

Constructor Detail

TransportProtocolCommon

public TransportProtocolCommon(java.net.Socket socket,
                               SshConnectionProperties properties,
                               HostKeyVerification hosts)
                        throws TransportProtocolException
Constructor for the SshTransportProtocol object

Parameters:
socket - A connected socket to the remote host
properties - The connection properties of the ssh connection
hosts - Description of the Parameter
Throws:
TransportProtocolException - Description of the Exception
Method Detail

setLocalIdent

protected abstract void setLocalIdent()
Abstract method that requires a derived class to set value of the local identification string. If the class implementing this method is a client then it should set the clientIdent protected member variable, if the class is implementing a server it should set the protected member serverIdent.


setRemoteIdent

protected abstract void setRemoteIdent(java.lang.String ident)
Abstract method that requires a derived class to set value of the remote identification string. If the class implementing this method is a client then it should set the serverIdent protected member variable, if the class is implementing a server it should set the protected member clientIdent.

Parameters:
ident - The identifiaction string received from the remote host

getLocalIdent

protected abstract java.lang.String getLocalIdent()
Abstract method to return the local identification string which is used in protocol negotiation and in computing the exchange hash. Implementations should return either the protected member variable clientIdent or serverIdent

Returns:
The local computers idnetification string, used in protocol negotiation

getRemoteIdent

protected abstract java.lang.String getRemoteIdent()
Abstract method to return the remote identification string which is used in protocol negotiation and in computing the exchange hash. Implementations should return either the protected member variable clientIdent or serverIdent

Returns:
The local computers idnetification string, used in protocol negotiation

setLocalKexInit

protected abstract void setLocalKexInit(com.sshtools.j2ssh.transport.SshMsgKexInit msg)
Abstract method to set the local kex init msg which is used in computing the exchange hash. Implementations should set the appropriate client or server member variable

Parameters:
msg - The local computers kex init message

setRemoteKexInit

protected abstract void setRemoteKexInit(com.sshtools.j2ssh.transport.SshMsgKexInit msg)
Abstract method to set the remote kex init msg which is used in computing the exchange hash. Implementations should set the appropriate client or server member variable

Parameters:
msg - The remote computers kex init message

getLocalKexInit

protected abstract com.sshtools.j2ssh.transport.SshMsgKexInit getLocalKexInit()
Abstract method to get the local kex init msg which is used in computing the exchange hash. Implementations should return the appropriate client or server member variable

Returns:
The local computers kex init message

getRemoteKexInit

protected abstract com.sshtools.j2ssh.transport.SshMsgKexInit getRemoteKexInit()
Abstract method to get the remote kex init msg which is used in computing the exchange hash. Implementations should return the appropriate client or server member variable

Returns:
The local computers kex init message

onBeginKeyExchange

protected abstract boolean onBeginKeyExchange(SshKeyExchange kex)
                                       throws TransportProtocolException
Abstract method called when key exchange has begun

Parameters:
kex - Description of the Parameter
Returns:
Description of the Return Value
Throws:
TransportProtocolException - Description of the Exception

getKexAlgorithm

protected java.lang.String getKexAlgorithm()
                                    throws AlgorithmNotAgreedException
Determines the correct key exchange algorithm to use

Returns:
A string containing the algorithm name i.e. "diffie-hellman-group1.sha1"
Throws:
AlgorithmNotAgreedException - When no algorithm is agreed between the two parties an exception is thrown

getEncryptionAlgorithm

protected abstract java.lang.String getEncryptionAlgorithm()
                                                    throws AlgorithmNotAgreedException
Abstract method to determine the correct encryption algorithm to use This is found by iterating through the clients supported algorithm and selecting the first supported encryption method that the server also supports. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object object

Returns:
The encryption algorithm to use i.e. "3des-cbc"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getDecryptionAlgorithm

protected abstract java.lang.String getDecryptionAlgorithm()
                                                    throws AlgorithmNotAgreedException
Abstract method to determine the correct decryption algorithm to use This is found by iterating through the clients supported algorithm and selecting the first supported decryption method that the server also supports. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object.

Returns:
The decryption algorithm to use i.e. "3des-cbc"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getOutputStreamMacAlgorithm

protected abstract java.lang.String getOutputStreamMacAlgorithm()
                                                         throws AlgorithmNotAgreedException
Abtract method for the client/server implmentations to determine the message authentication algorithm for the output stream. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object.

Returns:
The mac algorithm to use i.e. "hmac-sha1"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getInputStreamMacAlgorithm

protected abstract java.lang.String getInputStreamMacAlgorithm()
                                                        throws AlgorithmNotAgreedException
Abtract method for the client/server implmentations to determine the message authentication algorithm for the input stream. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object.

Returns:
The mac algorithm to use i.e. "hmac-sha1"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getOutputStreamCompAlgorithm

protected abstract java.lang.String getOutputStreamCompAlgorithm()
                                                          throws AlgorithmNotAgreedException
Abtract method for the client/server implmentations to determine the compression algorithm for the output stream. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object.

Returns:
The compression algorithm to use i.e. "zlib"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getInputStreamCompAlgortihm

protected abstract java.lang.String getInputStreamCompAlgortihm()
                                                         throws AlgorithmNotAgreedException
Abtract method for the client/server implmentations to determine the compression algorithm for the input stream. Client and server implementations should define this method using the determineAlgorithm method to pass either the CS or SC methods of the SshMsgKexInit object.

Returns:
The compression algorithm to use i.e. "zlib"
Throws:
AlgorithmNotAgreedException - When an algorithm cannot be agreed an exception is thrown

getState

public TransportProtocolState getState()
Gets the state attribute of the TransportProtocolCommon object

Returns:
The state value

run

public void run()
Main processing method for the TransportProtocolCommon object

Specified by:
run in interface java.lang.Runnable

stop

protected final void stop()
Stops the transport layer


beginKeyExchange

protected void beginKeyExchange()
                         throws TransportProtocolException
Sets the transport layer up for performing the key exchange, this is called when either a SSH_MSG_KEXINIT message is received or sent by either party

Throws:
TransportProtocolException - Description of the Exception

sendNewKeys

protected void sendNewKeys()
                    throws TransportProtocolException
Sends the SSH_MSG_NEWKEYS message to indicate that new keys are now in operation

Throws:
TransportProtocolException - Description of the Exception

completeKeyExchange

protected void completeKeyExchange()
                            throws TransportProtocolException
Completes key exchange by creating keys from the exchange hash and puts them into use

Throws:
TransportProtocolException - Description of the Exception

determineAlgorithm

protected java.lang.String determineAlgorithm(java.util.List clientAlgorithms,
                                              java.util.List serverAlgorithms)
                                       throws AlgorithmNotAgreedException
Helper method to determine the first algorithm that appears in the client list that is also supported by the server

Parameters:
clientAlgorithms - The list of client algorithms
serverAlgorithms - The list of server algorithms
Returns:
The determined algrithms
Throws:
AlgorithmNotAgreedException - Thrown if the algorithm cannot be agreed

sendMessage

public void sendMessage(SshMessage msg,
                        java.lang.Object sender)
                 throws TransportProtocolException
Send an SSH message, if the state doen't allow it because of key exchange then the message is stored and sent as soon as the state changes

Specified by:
sendMessage in interface TransportProtocol
Parameters:
msg - The SshMessage to send
sender - Description of the Parameter
Throws:
TransportProtocolException - Description of the Exception

onCorruptMac

protected void onCorruptMac()
This is called when a corrupt Mac has been received on the input stream. In this instance we will send a disconnect message.


registerMessage

public void registerMessage(java.lang.Integer messageId,
                            java.lang.Class implementor,
                            SshMessageStore store)
                     throws MessageAlreadyRegisteredException
Implements the TransportProtocol interface method to allow external SSH implementations to receive message notificaitons.

Specified by:
registerMessage in interface TransportProtocol
Parameters:
messageId - The messageId of the registered message
implementor - The class that implements the message
store - The message store to receive notificaiton
Throws:
MessageAlreadyRegisteredException - Thrown if the message cannot be registered.

unregisterMessage

public void unregisterMessage(java.lang.Integer messageId,
                              SshMessageStore store)
                       throws MessageNotRegisteredException
Implements the TransportProtocol method to allow external SSH implementations to unregister a message.

Specified by:
unregisterMessage in interface TransportProtocol
Parameters:
messageId - The message id of the message
store - The message store receiving the notifications.
Throws:
MessageNotRegisteredException - Thrown if the message is not registered.

sendDisconnect

protected void sendDisconnect(int reason,
                              java.lang.String description)
Sends a disconnect message

Parameters:
reason - The reason code.
description - The readable reason description.

disconnect

public void disconnect(java.lang.String description)
Disconnects the connection by sending a disconnect message with the BY_APPLICAITON reason.

Specified by:
disconnect in interface TransportProtocol
Parameters:
description - The description of the reason

onMessageData

protected void onMessageData(int messageId,
                             ByteArrayReader bar)
                      throws InvalidMessageException,
                             TransportProtocolException
Called by the framework when a new message is received

Parameters:
messageId - Description of the Parameter
bar - Description of the Parameter
Throws:
InvalidMessageException - Description of the Exception
TransportProtocolException - Description of the Exception

onMessageReceived

protected abstract void onMessageReceived(SshMessage msg)
Called by the framework when a new message is received.

Parameters:
msg - The message recevied

sendKeyExchangeInit

protected void sendKeyExchangeInit()
                            throws TransportProtocolException
Sends the key exchange init message

Throws:
TransportProtocolException - Description of the Exception

onChildThreadException

protected void onChildThreadException(java.lang.Exception e)
Called if an inputstream error occurs

Parameters:
e - Description of the Parameter

setServiceThread

public void setServiceThread(Service service)

Sets a service thread so that it receives notifications of exceptions.

Specified by:
setServiceThread in interface TransportProtocol
Parameters:
service -

removeServiceThread

public void removeServiceThread(Service service)

Removes thes service thread from the exception notification list.

Specified by:
removeServiceThread in interface TransportProtocol
Parameters:
service -

reportException

protected void reportException(java.lang.Exception e)

Reports an exception to service threads

Parameters:
e -

reportDisconnect

protected void reportDisconnect(java.lang.String reason)

Reports a disconnect to service threads.

Parameters:
reason -


Copyright © 2002 Sshtools.com. All Rights Reserved.