You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/01/30 00:48:30 UTC

[1/4] git commit: [SSHD-283] Support key re-exchange

Updated Branches:
  refs/heads/master 36446ae8d -> ade49e489


[SSHD-283] Support key re-exchange

Make main message handler common between ServerSession and ClientSessionImpl.
Replace the Session#state with an internal kex state variable and send events on SessionListener instead.

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/8d226dd8
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/8d226dd8
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/8d226dd8

Branch: refs/heads/master
Commit: 8d226dd8a2f7f234924f95e88b2206808567e681
Parents: 36446ae
Author: Guillaume Nodet <gn...@apache.org>
Authored: Wed Jan 29 22:21:45 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Wed Jan 29 22:21:45 2014 +0100

----------------------------------------------------------------------
 .../sshd/client/session/ClientSessionImpl.java  | 156 +++++----------
 .../java/org/apache/sshd/common/Session.java    |  17 +-
 .../org/apache/sshd/common/SessionListener.java |  12 +-
 .../org/apache/sshd/common/SshConstants.java    |  35 ++++
 .../session/AbstractConnectionService.java      |  39 ++--
 .../sshd/common/session/AbstractSession.java    | 192 +++++++++++++++----
 .../sshd/server/session/ServerSession.java      | 154 +--------------
 .../session/ServerSessionTimeoutListener.java   |   3 +-
 .../org/apache/sshd/AbstractSessionTest.java    |  21 +-
 .../org/apache/sshd/AuthenticationTest.java     |   3 -
 .../java/org/apache/sshd/KeyReExchangeTest.java | 148 ++++++++++++++
 .../test/java/org/apache/sshd/ServerTest.java   |   4 +-
 .../file/virtualfs/VirtualFileSystemTest.java   |  17 +-
 13 files changed, 479 insertions(+), 322 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index ce3e030..42af0ff 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -24,7 +24,6 @@ import java.security.KeyPair;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
 
 import org.apache.sshd.ClientChannel;
 import org.apache.sshd.ClientSession;
@@ -46,9 +45,9 @@ import org.apache.sshd.client.future.DefaultAuthFuture;
 import org.apache.sshd.client.scp.DefaultScpClient;
 import org.apache.sshd.client.sftp.DefaultSftpClient;
 import org.apache.sshd.common.KeyPairProvider;
-import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.Service;
 import org.apache.sshd.common.ServiceFactory;
+import org.apache.sshd.common.SessionListener;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.SshdSocketAddress;
@@ -69,15 +68,16 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
      */
     private Map<Object, Object> metadataMap = new HashMap<Object, Object>();
 
+    // TODO: clean service support a bit
+    private boolean initialServiceRequestSent;
     private ServiceFactory currentServiceFactory;
-
     private Service nextService;
     private ServiceFactory nextServiceFactory;
 
     protected AuthFuture authFuture;
 
     public ClientSessionImpl(ClientFactoryManager client, IoSession session) throws Exception {
-        super(client, session);
+        super(false, client, session);
         log.info("Session created...");
         // Need to set the initial service early as calling code likes to start trying to
         // manipulate it before the connection has even been established.  For instance, to
@@ -97,6 +97,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         authFuture = new DefaultAuthFuture(lock);
         authFuture.setAuthed(false);
         sendClientIdentification();
+        kexState = KEX_STATE_INIT;
         sendKexInit();
     }
 
@@ -225,99 +226,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
 
     protected void handleMessage(Buffer buffer) throws Exception {
         synchronized (lock) {
-            doHandleMessage(buffer);
-        }
-    }
-
-    protected void doHandleMessage(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        log.debug("Received packet {}", cmd);
-        switch (cmd) {
-            case SSH_MSG_DISCONNECT: {
-                int code = buffer.getInt();
-                String msg = buffer.getString();
-                log.info("Received SSH_MSG_DISCONNECT (reason={}, msg={})", code, msg);
-                close(true);
-                break;
-            }
-            case SSH_MSG_UNIMPLEMENTED: {
-                int code = buffer.getInt();
-                log.info("Received SSH_MSG_UNIMPLEMENTED #{}", code);
-                break;
-            }
-            case SSH_MSG_DEBUG: {
-                boolean display = buffer.getBoolean();
-                String msg = buffer.getString();
-                log.info("Received SSH_MSG_DEBUG (display={}) '{}'", display, msg);
-                break;
-            }
-            case SSH_MSG_IGNORE:
-                log.info("Received SSH_MSG_IGNORE");
-                break;
-            default:
-                switch (getState()) {
-                    case ReceiveKexInit:
-                        if (cmd != SshConstants.Message.SSH_MSG_KEXINIT) {
-                            log.error("Ignoring command {} while waiting for {}", cmd, SshConstants.Message.SSH_MSG_KEXINIT);
-                            break;
-                        }
-                        log.info("Received SSH_MSG_KEXINIT");
-                        receiveKexInit(buffer);
-                        negociate();
-                        kex = NamedFactory.Utils.create(factoryManager.getKeyExchangeFactories(), negociated[SshConstants.PROPOSAL_KEX_ALGS]);
-                        kex.init(this, serverVersion.getBytes(), clientVersion.getBytes(), I_S, I_C);
-                        setState(State.Kex);
-                        break;
-                    case Kex:
-                        buffer.rpos(buffer.rpos() - 1);
-                        if (kex.next(buffer)) {
-                            checkHost();
-                            sendNewKeys();
-                            setState(State.ReceiveNewKeys);
-                        }
-                        break;
-                    case ReceiveNewKeys:
-                        if (cmd != SshConstants.Message.SSH_MSG_NEWKEYS) {
-                            disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Protocol error: expected packet SSH_MSG_NEWKEYS, got " + cmd);
-                            return;
-                        }
-                        log.info("Received SSH_MSG_NEWKEYS");
-                        receiveNewKeys(false);
-                        log.info("Send SSH_MSG_SERVICE_REQUEST for {}", currentServiceFactory.getName());
-                        Buffer request = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_REQUEST, 0);
-                        request.putString(currentServiceFactory.getName());
-                        writePacket(request);
-                        setState(State.ServiceRequestSent);
-                        // Assuming that MINA-SSHD only implements "explicit server authentication" it is permissible
-                        // for the client's service to start sending data before the service-accept has been received.
-                        // If "implicit authentication" were to ever be supported, then this would need to be
-                        // called after service-accept comes back.  See SSH-TRANSPORT.
-                        currentService.start();
-                        break;
-                    case ServiceRequestSent:
-                        if (cmd != SshConstants.Message.SSH_MSG_SERVICE_ACCEPT) {
-                            disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Protocol error: expected packet SSH_MSG_SERVICE_ACCEPT, got " + cmd);
-                            return;
-                        }
-                        log.info("Received SSH_MSG_SERVICE_ACCEPT");
-                        setState(State.Running);
-                        break;
-                    case Running:
-                        switch (cmd) {
-                            case SSH_MSG_REQUEST_SUCCESS:
-                                requestSuccess(buffer);
-                                break;
-                            case SSH_MSG_REQUEST_FAILURE:
-                                requestFailure(buffer);
-                                break;
-                            default:
-                                currentService.process(cmd, buffer);
-                                break;
-                        }
-                        break;
-                    default:
-                        throw new IllegalStateException("Unsupported state: " + getState());
-                }
+            super.handleMessage(buffer);
         }
     }
 
@@ -362,13 +271,6 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         }
     }
 
-    public void setState(State newState) {
-        synchronized (lock) {
-            super.setState(newState);
-            lock.notifyAll();
-        }
-    }
-
     protected boolean readIdentification(Buffer buffer) throws IOException {
         serverVersion = doReadIdentification(buffer);
         if (serverVersion == null) {
@@ -387,17 +289,18 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         sendIdentification(clientVersion);
     }
 
-    private void sendKexInit() throws Exception {
+    protected void sendKexInit() throws IOException {
         clientProposal = createProposal(KeyPairProvider.SSH_RSA + "," + KeyPairProvider.SSH_DSS);
         I_C = sendKexInit(clientProposal);
     }
 
-    private void receiveKexInit(Buffer buffer) throws Exception {
+    protected void receiveKexInit(Buffer buffer) throws IOException {
         serverProposal = new String[SshConstants.PROPOSAL_MAX];
         I_S = receiveKexInit(buffer, serverProposal);
     }
 
-    private void checkHost() throws SshException {
+    @Override
+    protected void checkKeys() throws SshException {
         ServerKeyVerifier serverKeyVerifier = getClientFactoryManager().getServerKeyVerifier();
         SocketAddress remoteAddress = ioSession.getRemoteAddress();
 
@@ -406,7 +309,44 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         }
     }
 
-	public Map<Object, Object> getMetadataMap() {
+    @Override
+    protected void sendEvent(SessionListener.Event event) throws IOException {
+        if (event == SessionListener.Event.KeyEstablished) {
+            sendInitialServiceRequest();
+        }
+        synchronized (lock) {
+            lock.notifyAll();
+        }
+        super.sendEvent(event);
+    }
+
+    protected void sendInitialServiceRequest() throws IOException {
+        if (initialServiceRequestSent) {
+            return;
+        }
+        initialServiceRequestSent = true;
+        log.info("Send SSH_MSG_SERVICE_REQUEST for {}", currentServiceFactory.getName());
+        Buffer request = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_REQUEST, 0);
+        request.putString(currentServiceFactory.getName());
+        writePacket(request);
+        // Assuming that MINA-SSHD only implements "explicit server authentication" it is permissible
+        // for the client's service to start sending data before the service-accept has been received.
+        // If "implicit authentication" were to ever be supported, then this would need to be
+        // called after service-accept comes back.  See SSH-TRANSPORT.
+        currentService.start();
+    }
+
+    @Override
+    public void startService(String name) throws Exception {
+        throw new IllegalStateException("Starting services is not supported on the client side");
+    }
+
+    @Override
+    public void resetIdleTimeout() {
+
+    }
+
+    public Map<Object, Object> getMetadataMap() {
 		return metadataMap;
 	}
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/common/Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Session.java b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
index cc968e3..9b8bcae 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
@@ -20,6 +20,7 @@ package org.apache.sshd.common;
 
 import java.io.IOException;
 
+import org.apache.sshd.common.future.SshFuture;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.util.Buffer;
 
@@ -30,17 +31,6 @@ import org.apache.sshd.common.util.Buffer;
  */
 public interface Session {
 
-    public enum State {
-        ReceiveKexInit, Kex, ReceiveNewKeys, ServiceRequestSent, WaitForServiceRequest, Running, Closed
-    }
-
-    /**
-     * Retrieve the state of this session.
-     * @return the session's state
-     * @see SessionListener
-     */
-    State getState();
-
     /**
      * Returns the value of the user-defined attribute of this session.
      *
@@ -153,6 +143,11 @@ public interface Session {
     void removeListener(SessionListener listener);
 
     /**
+     * Initiate a new key exchange.
+     */
+    SshFuture reExchangeKeys() throws IOException;
+
+    /**
      * Type safe key for storage within the user attributes of {@link org.apache.sshd.common.session.AbstractSession}.
      * Typically it is used as a static variable that is shared between the producer
      * and the consumer. To further restrict access the setting or getting it from

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java b/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
index 96c96eb..dd30b48 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/SessionListener.java
@@ -25,6 +25,10 @@ package org.apache.sshd.common;
  */
 public interface SessionListener {
 
+    enum Event {
+        KeyEstablished, Authenticated
+    }
+
     /**
      * A new session just been created
      * @param session
@@ -32,11 +36,11 @@ public interface SessionListener {
     void sessionCreated(Session session);
 
     /**
-     * A session state has changed
-     * @param session
-     * @see org.apache.sshd.common.Session#getState()
+     * An event has been triggered
+     * @param sesssion
+     * @param event
      */
-    void sessionChanged(Session session);
+    void sessionEvent(Session sesssion, Event event);
 
     /**
      * A session has been closed

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java b/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
index 241c61e..26148fd 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
@@ -97,6 +97,41 @@ public interface SshConstants {
         }
     }
 
+    static final int SSH_MSG_DISCONNECT=                      1;
+    static final int SSH_MSG_IGNORE=                          2;
+    static final int SSH_MSG_UNIMPLEMENTED=                   3;
+    static final int SSH_MSG_DEBUG=                           4;
+    static final int SSH_MSG_SERVICE_REQUEST=                 5;
+    static final int SSH_MSG_SERVICE_ACCEPT=                  6;
+    static final int SSH_MSG_KEXINIT=                        20;
+    static final int SSH_MSG_NEWKEYS=                        21;
+
+    static final int SSH_MSG_KEX_FIRST=                      30;
+    static final int SSH_MSG_KEX_LAST=                       49;
+
+    static final int SSH_MSG_KEXDH_INIT=                     30;
+    static final int SSH_MSG_KEXDH_REPLY=                    31;
+
+    static final int SSH_MSG_KEX_DH_GEX_GROUP=               31;
+    static final int SSH_MSG_KEX_DH_GEX_INIT=                32;
+    static final int SSH_MSG_KEX_DH_GEX_REPLY=               33;
+    static final int SSH_MSG_KEX_DH_GEX_REQUEST=             34;
+
+    static final int SSH_MSG_GLOBAL_REQUEST=                 80;
+    static final int SSH_MSG_REQUEST_SUCCESS=                81;
+    static final int SSH_MSG_REQUEST_FAILURE=                82;
+    static final int SSH_MSG_CHANNEL_OPEN=                   90;
+    static final int SSH_MSG_CHANNEL_OPEN_CONFIRMATION=      91;
+    static final int SSH_MSG_CHANNEL_OPEN_FAILURE=           92;
+    static final int SSH_MSG_CHANNEL_WINDOW_ADJUST=          93;
+    static final int SSH_MSG_CHANNEL_DATA=                   94;
+    static final int SSH_MSG_CHANNEL_EXTENDED_DATA=          95;
+    static final int SSH_MSG_CHANNEL_EOF=                    96;
+    static final int SSH_MSG_CHANNEL_CLOSE=                  97;
+    static final int SSH_MSG_CHANNEL_REQUEST=                98;
+    static final int SSH_MSG_CHANNEL_SUCCESS=                99;
+    static final int SSH_MSG_CHANNEL_FAILURE=               100;
+
     //
     // Values for the algorithms negociation 
     //

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
index 0149adb..ce68594 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
@@ -182,6 +182,12 @@ public abstract class AbstractConnectionService implements ConnectionService {
             case SSH_MSG_GLOBAL_REQUEST:
                 globalRequest(buffer);
                 break;
+            case SSH_MSG_REQUEST_SUCCESS:
+                requestSuccess(buffer);
+                break;
+            case SSH_MSG_REQUEST_FAILURE:
+                requestFailure(buffer);
+                break;
             default:
                 throw new IllegalStateException("Unsupported command: " + cmd);
         }
@@ -356,18 +362,21 @@ public abstract class AbstractConnectionService implements ConnectionService {
                         buf.putInt(channel.getLocalWindow().getSize());
                         buf.putInt(channel.getLocalWindow().getPacketSize());
                         session.writePacket(buf);
-                    } else if (future.getException() != null) {
-                        Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
-                        buf.putInt(id);
-                        if (future.getException() instanceof OpenChannelException) {
-                            buf.putInt(((OpenChannelException) future.getException()).getReasonCode());
-                            buf.putString(future.getException().getMessage());
-                        } else {
-                            buf.putInt(0);
-                            buf.putString("Error opening channel: " + future.getException().getMessage());
+                    } else {
+                        Throwable exception = future.getException();
+                        if (exception != null) {
+                            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+                            buf.putInt(id);
+                            if (exception instanceof OpenChannelException) {
+                                buf.putInt(((OpenChannelException) exception).getReasonCode());
+                                buf.putString(exception.getMessage());
+                            } else {
+                                buf.putInt(0);
+                                buf.putString("Error opening channel: " + exception.getMessage());
+                            }
+                            buf.putString("");
+                            session.writePacket(buf);
                         }
-                        buf.putString("");
-                        session.writePacket(buf);
                     }
                 } catch (IOException e) {
                     session.exceptionCaught(e);
@@ -421,4 +430,12 @@ public abstract class AbstractConnectionService implements ConnectionService {
         }
     }
 
+    protected void requestSuccess(Buffer buffer) throws Exception {
+        ((AbstractSession) session).requestSuccess(buffer);
+    }
+
+    protected void requestFailure(Buffer buffer) throws Exception {
+        ((AbstractSession) session).requestFailure(buffer);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index 942809e..b703dff 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.sshd.common.Cipher;
@@ -41,6 +42,8 @@ import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
+import org.apache.sshd.common.future.DefaultSshFuture;
+import org.apache.sshd.common.future.SshFuture;
 import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.io.IoCloseFuture;
 import org.apache.sshd.common.io.IoSession;
@@ -72,6 +75,14 @@ public abstract class AbstractSession implements Session {
      * and {@link #attachSession(IoSession, AbstractSession)}.
      */
     public static final String SESSION = "org.apache.sshd.session";
+
+    protected static final int KEX_STATE_INIT = 1;
+    protected static final int KEX_STATE_RUN =  2;
+    protected static final int KEX_STATE_KEYS = 3;
+    protected static final int KEX_STATE_DONE = 4;
+
+    /** Client or server side */
+    protected final boolean isServer;
     /** Our logger */
     protected final Logger log = LoggerFactory.getLogger(getClass());
     /** The factory manager used to retrieve factories of Ciphers, Macs and other objects */
@@ -94,7 +105,7 @@ public abstract class AbstractSession implements Session {
     protected String username;
 
     /** Session listener */
-    protected final List<SessionListener> listeners = new ArrayList<SessionListener>();
+    protected final List<SessionListener> listeners = new CopyOnWriteArrayList<SessionListener>();
 
     //
     // Key exchange support
@@ -108,6 +119,8 @@ public abstract class AbstractSession implements Session {
     protected byte[] I_C; // the payload of the client's SSH_MSG_KEXINIT
     protected byte[] I_S; // the payload of the factoryManager's SSH_MSG_KEXINIT
     protected KeyExchange kex;
+    protected int kexState;
+    protected DefaultSshFuture reexchangeFuture;
 
     //
     // SSH packets encoding / decoding support
@@ -135,15 +148,14 @@ public abstract class AbstractSession implements Session {
 
     protected Service currentService;
 
-    private volatile State state = State.ReceiveKexInit;
-
     /**
      * Create a new session.
      *
      * @param factoryManager the factory manager
      * @param ioSession the underlying MINA session
      */
-    public AbstractSession(FactoryManager factoryManager, IoSession ioSession) {
+    public AbstractSession(boolean isServer, FactoryManager factoryManager, IoSession ioSession) {
+        this.isServer = isServer;
         this.factoryManager = factoryManager;
         this.ioSession = ioSession;
         this.random = factoryManager.getRandomFactory().create();
@@ -191,18 +203,6 @@ public abstract class AbstractSession implements Session {
         ioSession.setAttribute(SESSION, session);
     }
 
-    public State getState() {
-        return state;
-    }
-
-    protected void setState(State state) {
-        this.state = state;
-        final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
-        for (SessionListener sl : l) {
-            sl.sessionChanged(this);
-        }
-    }
-
     public String getServerVersion() {
         return serverVersion;
     }
@@ -242,9 +242,10 @@ public abstract class AbstractSession implements Session {
         return authed;
     }
 
-    public void setAuthenticated(String username) {
+    public void setAuthenticated(String username) throws IOException {
         this.authed = true;
         this.username = username;
+        sendEvent(SessionListener.Event.Authenticated);
     }
 
     /**
@@ -285,7 +286,102 @@ public abstract class AbstractSession implements Session {
      * @param buffer the buffer containing the packet
      * @throws Exception if an exeption occurs while handling this packet.
      */
-    protected abstract void handleMessage(Buffer buffer) throws Exception;
+    protected void handleMessage(Buffer buffer) throws Exception {
+        SshConstants.Message cmd = buffer.getCommand();
+        switch (cmd) {
+            case SSH_MSG_DISCONNECT: {
+                int code = buffer.getInt();
+                String msg = buffer.getString();
+                log.debug("Received SSH_MSG_DISCONNECT (reason={}, msg={})", code, msg);
+                close(true);
+                break;
+            }
+            case SSH_MSG_IGNORE: {
+                log.debug("Received SSH_MSG_IGNORE");
+                break;
+            }
+            case SSH_MSG_UNIMPLEMENTED: {
+                int code = buffer.getInt();
+                log.debug("Received SSH_MSG_UNIMPLEMENTED #{}", code);
+                break;
+            }
+            case SSH_MSG_DEBUG: {
+                boolean display = buffer.getBoolean();
+                String msg = buffer.getString();
+                log.debug("Received SSH_MSG_DEBUG (display={}) '{}'", display, msg);
+                break;
+            }
+            case SSH_MSG_SERVICE_REQUEST:
+                String service = buffer.getString();
+                log.debug("Received SSH_MSG_SERVICE_REQUEST '{}'", service);
+                if (kexState != KEX_STATE_DONE) {
+                    throw new IllegalStateException("Received command " + cmd + " before key exchange is finished");
+                }
+                try {
+                    startService(service);
+                } catch (Exception e) {
+                    log.debug("Service " + service + " rejected", e);
+                    disconnect(SshConstants.SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE, "Bad service request: " + service);
+                    break;
+                }
+                log.debug("Accepted service {}", service);
+                Buffer response = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT, 0);
+                response.putString(service);
+                writePacket(response);
+                break;
+            case SSH_MSG_SERVICE_ACCEPT:
+                log.debug("Received SSH_MSG_SERVICE_ACCEPT");
+                if (kexState != KEX_STATE_DONE) {
+                    throw new IllegalStateException("Received command " + cmd + " before key exchange is finished");
+                }
+                serviceAccept();
+                break;
+            case SSH_MSG_KEXINIT:
+                log.debug("Received SSH_MSG_KEXINIT");
+                receiveKexInit(buffer);
+                if (kexState == KEX_STATE_DONE) {
+                    sendKexInit();
+                } else if (kexState != KEX_STATE_INIT) {
+                    throw new IllegalStateException("Received SSH_MSG_KEXINIT while key exchange is running");
+                }
+                kexState = KEX_STATE_RUN;
+                negociate();
+                kex = NamedFactory.Utils.create(factoryManager.getKeyExchangeFactories(), negociated[SshConstants.PROPOSAL_KEX_ALGS]);
+                kex.init(this, serverVersion.getBytes(), clientVersion.getBytes(), I_S, I_C);
+                break;
+            case SSH_MSG_NEWKEYS:
+                log.debug("Received SSH_MSG_NEWKEYS");
+                if (kexState != KEX_STATE_KEYS) {
+                    throw new IllegalStateException("Received command " + cmd + " before key exchange is finished");
+                }
+                receiveNewKeys();
+                kexState = KEX_STATE_DONE;
+                if (reexchangeFuture != null) {
+                    reexchangeFuture.setValue(true);
+                }
+                sendEvent(SessionListener.Event.KeyEstablished);
+                break;
+            default:
+                log.debug("Received {}", cmd);
+                if (cmd.toByte() >= SshConstants.SSH_MSG_KEX_FIRST && cmd.toByte() <= SshConstants.SSH_MSG_KEX_LAST) {
+                    if (kexState != KEX_STATE_RUN) {
+                        throw new IllegalStateException("Received kex command " + cmd.toByte() + " while not in key exchange");
+                    }
+                    buffer.rpos(buffer.rpos() - 1);
+                    if (kex.next(buffer)) {
+                        checkKeys();
+                        sendNewKeys();
+                        kexState = KEX_STATE_KEYS;
+                    }
+                } else if (currentService != null) {
+                    currentService.process(cmd, buffer);
+                    resetIdleTimeout();
+                } else {
+                    throw new IllegalStateException("Unsupported command " + cmd);
+                }
+                break;
+        }
+    }
 
     /**
      * Handle any exceptions that occured on this session.
@@ -331,8 +427,7 @@ public abstract class AbstractSession implements Session {
                     closeFuture.setClosed();
                     lock.notifyAll();
                 }
-                state = State.Closed;
-                log.info("SMessession {}@{} closed", s.getUsername(), s.getIoSession().getRemoteAddress());
+                log.info("Session {}@{} closed", s.getUsername(), s.getIoSession().getRemoteAddress());
                 // Fire 'close' event
                 final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
                 for (SessionListener sl : l) {
@@ -373,12 +468,16 @@ public abstract class AbstractSession implements Session {
      * @throws java.io.IOException if an error occured when encoding sending the packet
      */
     public IoWriteFuture writePacket(Buffer buffer) throws IOException {
-        // Synchronize all write requests as needed by the encoding algorithm
-        // and also queue the write request in this synchronized block to ensure
-        // packets are sent in the correct order
-        synchronized (encodeLock) {
-            encode(buffer);
-            return ioSession.write(buffer);
+        try {
+            // Synchronize all write requests as needed by the encoding algorithm
+            // and also queue the write request in this synchronized block to ensure
+            // packets are sent in the correct order
+            synchronized (encodeLock) {
+                encode(buffer);
+                return ioSession.write(buffer);
+            }
+        } finally {
+            resetIdleTimeout();
         }
     }
 
@@ -768,10 +867,9 @@ public abstract class AbstractSession implements Session {
      * This method will intialize the ciphers, digests, macs and compression
      * according to the negociated server and client proposals.
      *
-     * @param isServer boolean indicating if this session is on the server or the client side
      * @throws Exception if an error occurs
      */
-    protected void receiveNewKeys(boolean isServer) throws Exception {
+    protected void receiveNewKeys() throws Exception {
         byte[] IVc2s;
         byte[] IVs2c;
         byte[] Ec2s;
@@ -1050,19 +1148,47 @@ public abstract class AbstractSession implements Session {
         if (listener == null) {
             throw new IllegalArgumentException();
         }
+        this.listeners.add(listener);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void removeListener(SessionListener listener) {
+        this.listeners.remove(listener);
+    }
 
-        synchronized (this.listeners) {
-            this.listeners.add(listener);
+    protected void sendEvent(SessionListener.Event event) throws IOException {
+        for (SessionListener sl : listeners) {
+            sl.sessionEvent(this, event);
         }
     }
 
+
     /**
      * {@inheritDoc}
      */
-    public void removeListener(SessionListener listener) {
-        synchronized (this.listeners) {
-            this.listeners.remove(listener);
+    public SshFuture reExchangeKeys() throws IOException {
+        if (kexState != KEX_STATE_DONE) {
+            throw new IllegalStateException("Can not perform key re-exchange while key exchange is already running");
         }
+        kexState = KEX_STATE_INIT;
+        sendKexInit();
+        reexchangeFuture = new DefaultSshFuture(null);
+        return reexchangeFuture;
     }
 
+    protected abstract void sendKexInit() throws IOException;
+
+    protected abstract void checkKeys() throws IOException;
+
+    protected abstract void receiveKexInit(Buffer buffer) throws IOException;
+
+    protected void serviceAccept() throws IOException {
+    }
+
+    public abstract void startService(String name) throws Exception;
+
+    public abstract void resetIdleTimeout();
+
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
index 9fc2a14..30c8311 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
@@ -20,33 +20,16 @@ package org.apache.sshd.server.session;
 
 import java.io.IOException;
 import java.security.KeyPair;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.concurrent.ScheduledExecutorService;
 
-import org.apache.sshd.SshServer;
-import org.apache.sshd.agent.common.AgentForwardSupport;
-import org.apache.sshd.agent.local.ChannelAgentForwarding;
-import org.apache.sshd.client.future.OpenFuture;
-import org.apache.sshd.common.Channel;
 import org.apache.sshd.common.NamedFactory;
-import org.apache.sshd.common.Service;
 import org.apache.sshd.common.ServiceFactory;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
-import org.apache.sshd.common.SshdSocketAddress;
-import org.apache.sshd.common.future.CloseFuture;
-import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.server.ServerFactoryManager;
-import org.apache.sshd.server.UserAuth;
-import org.apache.sshd.server.channel.OpenChannelException;
-import org.apache.sshd.server.x11.X11ForwardSupport;
 
 /**
  *
@@ -68,12 +51,13 @@ public class ServerSession extends AbstractSession {
     private int idleTimeoutMs = 10 * 60 * 1000; // 10 minutes in milliseconds
 
     public ServerSession(ServerFactoryManager server, IoSession ioSession) throws Exception {
-        super(server, ioSession);
+        super(true, server, ioSession);
         authTimeoutMs = getIntProperty(ServerFactoryManager.AUTH_TIMEOUT, authTimeoutMs);
         authTimeoutTimestamp = System.currentTimeMillis() + authTimeoutMs;
         idleTimeoutMs = getIntProperty(ServerFactoryManager.IDLE_TIMEOUT, idleTimeoutMs);
         log.info("Session created from {}", ioSession.getRemoteAddress());
         sendServerIdentification();
+        kexState = KEX_STATE_INIT;
         sendKexInit();
     }
 
@@ -85,135 +69,17 @@ public class ServerSession extends AbstractSession {
         return (ServerFactoryManager) factoryManager;
     }
 
-    protected ScheduledExecutorService getScheduledExecutorService() {
-        return getServerFactoryManager().getScheduledExecutorService();
-    }
-
-    @Override
-    public IoWriteFuture writePacket(Buffer buffer) throws IOException {
-        boolean rescheduleIdleTimer = getState() == State.Running;
-        if (rescheduleIdleTimer) {
-            resetIdleTimeout();
-        }
-        IoWriteFuture future = super.writePacket(buffer);
-        if (rescheduleIdleTimer) {
-            resetIdleTimeout();
-        }
-        return future;
-    }
-
-    protected void handleMessage(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        log.debug("Received packet {}", cmd);
-        switch (cmd) {
-            case SSH_MSG_DISCONNECT: {
-                int code = buffer.getInt();
-                String msg = buffer.getString();
-                log.debug("Received SSH_MSG_DISCONNECT (reason={}, msg={})", code, msg);
-                close(true);
-                break;
-            }
-            case SSH_MSG_UNIMPLEMENTED: {
-                int code = buffer.getInt();
-                log.debug("Received SSH_MSG_UNIMPLEMENTED #{}", code);
-                break;
-            }
-            case SSH_MSG_DEBUG: {
-                boolean display = buffer.getBoolean();
-                String msg = buffer.getString();
-                log.debug("Received SSH_MSG_DEBUG (display={}) '{}'", display, msg);
-                break;
-            }
-            case SSH_MSG_IGNORE:
-                log.debug("Received SSH_MSG_IGNORE");
-                break;
-            default:
-                switch (getState()) {
-                    case ReceiveKexInit:
-                        if (cmd != SshConstants.Message.SSH_MSG_KEXINIT) {
-                            log.warn("Ignoring command " + cmd + " while waiting for " + SshConstants.Message.SSH_MSG_KEXINIT);
-                            break;
-                        }
-                        log.debug("Received SSH_MSG_KEXINIT");
-                        receiveKexInit(buffer);
-                        negociate();
-                        kex = NamedFactory.Utils.create(factoryManager.getKeyExchangeFactories(), negociated[SshConstants.PROPOSAL_KEX_ALGS]);
-                        kex.init(this, serverVersion.getBytes(), clientVersion.getBytes(), I_S, I_C);
-                        setState(State.Kex);
-                        break;
-                    case Kex:
-                        buffer.rpos(buffer.rpos() - 1);
-                        if (kex.next(buffer)) {
-                            sendNewKeys();
-                            setState(State.ReceiveNewKeys);
-                        }
-                        break;
-                    case ReceiveNewKeys:
-                        if (cmd != SshConstants.Message.SSH_MSG_NEWKEYS) {
-                            disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Protocol error: expected packet " + SshConstants.Message.SSH_MSG_NEWKEYS + ", got " + cmd);
-                            return;
-                        }
-                        log.debug("Received SSH_MSG_NEWKEYS");
-                        receiveNewKeys(true);
-                        setState(State.WaitForServiceRequest);
-                        break;
-                    case WaitForServiceRequest:
-                        if (cmd != SshConstants.Message.SSH_MSG_SERVICE_REQUEST) {
-                            log.debug("Expecting a {}, but received {}", SshConstants.Message.SSH_MSG_SERVICE_REQUEST, cmd);
-                            notImplemented();
-                        } else {
-                            String service = buffer.getString();
-                            log.debug("Received SSH_MSG_SERVICE_REQUEST '{}'", service);
-                            try {
-                                startService(service);
-                            } catch (Exception e) {
-                                log.debug("Service " + service + " rejected", e);
-                                disconnect(SshConstants.SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE, "Bad service request: " + service);
-                                break;
-                            }
-                            log.debug("Accepted service {}", service);
-                            Buffer response = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT, 0);
-                            response.putString(service);
-                            writePacket(response);
-                            setState(State.Running);
-                        }
-                        break;
-                    case Running:
-                        running(cmd, buffer);
-                        resetIdleTimeout();
-                        break;
-                    default:
-                        throw new IllegalStateException("Unsupported state: " + getState());
-                }
-        }
+    protected void checkKeys() {
     }
 
     public void startService(String name) throws Exception {
         currentService = ServiceFactory.Utils.create(getFactoryManager().getServiceFactories(), name, this);
     }
 
-    private void running(SshConstants.Message cmd, Buffer buffer) throws Exception {
-        switch (cmd) {
-            case SSH_MSG_KEXINIT:
-                receiveKexInit(buffer);
-                sendKexInit();
-                negociate();
-                kex = NamedFactory.Utils.create(factoryManager.getKeyExchangeFactories(), negociated[SshConstants.PROPOSAL_KEX_ALGS]);
-                kex.init(this, serverVersion.getBytes(), clientVersion.getBytes(), I_S, I_C);
-                break;
-            case SSH_MSG_KEXDH_INIT:
-                buffer.rpos(buffer.rpos() - 1);
-                if (kex.next(buffer)) {
-                    sendNewKeys();
-                }
-                break;
-            case SSH_MSG_NEWKEYS:
-                receiveNewKeys(true);
-                break;
-            default:
-                currentService.process(cmd, buffer);
-                break;
-        }
+    @Override
+    protected void serviceAccept() throws IOException {
+        // TODO: can services be initiated by the server-side ?
+        disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Unsupported packet: SSH_MSG_SERVICE_ACCEPT");
     }
 
     /**
@@ -223,7 +89,7 @@ public class ServerSession extends AbstractSession {
      * @throws IOException
      */
     protected void checkForTimeouts() throws IOException {
-        if (getState() != State.Closed) {
+        if (!closing) {
             long now = System.currentTimeMillis();
             if (!authed && now > authTimeoutTimestamp) {
                 disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Session has timed out waiting for authentication after " + authTimeoutMs + " ms.");
@@ -247,7 +113,7 @@ public class ServerSession extends AbstractSession {
         sendIdentification(serverVersion);
     }
 
-    private void sendKexInit() throws IOException {
+    protected void sendKexInit() throws IOException {
         serverProposal = createProposal(factoryManager.getKeyPairProvider().getKeyTypes());
         I_S = sendKexInit(serverProposal);
     }
@@ -265,7 +131,7 @@ public class ServerSession extends AbstractSession {
         return true;
     }
 
-    private void receiveKexInit(Buffer buffer) throws IOException {
+    protected void receiveKexInit(Buffer buffer) throws IOException {
         clientProposal = new String[SshConstants.PROPOSAL_MAX];
         I_C = receiveKexInit(buffer, clientProposal);
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSessionTimeoutListener.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSessionTimeoutListener.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSessionTimeoutListener.java
index 580249c..d85315f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSessionTimeoutListener.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSessionTimeoutListener.java
@@ -44,8 +44,7 @@ public class ServerSessionTimeoutListener implements SessionListener, Runnable {
         }
     }
 
-    public void sessionChanged(Session session) {
-        // ignore
+    public void sessionEvent(Session sesssion, Event event) {
     }
 
     public void sessionClosed(Session s) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
index 1c4486a..8943be5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sshd;
 
+import java.io.IOException;
+
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.common.util.Buffer;
@@ -102,9 +104,7 @@ public class AbstractSessionTest {
 
     public static class MySession extends AbstractSession {
         public MySession() {
-            super(SshServer.setUpDefaultServer(), null);
-        }
-        public void messageReceived(IoBuffer byteBuffer) throws Exception {
+            super(true, SshServer.setUpDefaultServer(), null);
         }
         protected void handleMessage(Buffer buffer) throws Exception {
         }
@@ -114,5 +114,20 @@ public class AbstractSessionTest {
         public String doReadIdentification(Buffer buffer) {
             return super.doReadIdentification(buffer);
         }
+        @Override
+        protected void sendKexInit() throws IOException {
+        }
+        @Override
+        protected void checkKeys() {
+        }
+        @Override
+        protected void receiveKexInit(Buffer buffer) throws IOException {
+        }
+        @Override
+        public void startService(String name) throws Exception {
+        }
+        @Override
+        public void resetIdleTimeout() {
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
index bbbd863..1d429c3 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
@@ -136,9 +136,6 @@ public class AuthenticationTest {
         public TestSession(ServerFactoryManager server, IoSession ioSession) throws Exception {
             super(server, ioSession);
         }
-        public void setState(State state) {
-            super.setState(state);
-        }
         public void handleMessage(Buffer buffer) throws Exception {
             super.handleMessage(buffer);
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
new file mode 100644
index 0000000..e6b1c54
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.util.Collections;
+
+import com.jcraft.jsch.JSch;
+import org.apache.sshd.client.channel.ChannelShell;
+import org.apache.sshd.client.kex.DHG1;
+import org.apache.sshd.client.kex.DHG14;
+import org.apache.sshd.client.kex.DHGEX;
+import org.apache.sshd.client.kex.DHGEX256;
+import org.apache.sshd.client.kex.ECDHP256;
+import org.apache.sshd.client.kex.ECDHP384;
+import org.apache.sshd.client.kex.ECDHP521;
+import org.apache.sshd.common.KeyExchange;
+import org.apache.sshd.common.NamedFactory;
+import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.util.BogusPasswordAuthenticator;
+import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.JSchLogger;
+import org.apache.sshd.util.SimpleUserInfo;
+import org.apache.sshd.util.TeeOutputStream;
+import org.apache.sshd.util.Utils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test key exchange algorithms.
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class KeyReExchangeTest {
+
+    private SshServer sshd;
+    private int port;
+
+    @Before
+    public void setUp() throws Exception {
+        port = Utils.getFreePort();
+
+        sshd = SshServer.setUpDefaultServer();
+        sshd.setPort(port);
+        sshd.setKeyPairProvider(Utils.createTestHostKeyProvider());
+        sshd.setShellFactory(new EchoShellFactory());
+        sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator());
+        sshd.start();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        sshd.stop();
+    }
+
+    @Test
+    public void testReExchangeFromClient() throws Exception {
+        JSchLogger.init();
+        JSch.setConfig("kex", "diffie-hellman-group-exchange-sha1");
+        JSch sch = new JSch();
+        com.jcraft.jsch.Session s = sch.getSession("smx", "localhost", port);
+        s.setUserInfo(new SimpleUserInfo("smx"));
+        s.connect();
+        com.jcraft.jsch.Channel c = s.openChannel("shell");
+        c.connect();
+        OutputStream os = c.getOutputStream();
+        InputStream is = c.getInputStream();
+        for (int i = 0; i < 10; i++) {
+            os.write("this is my command\n".getBytes());
+            os.flush();
+            byte[] data = new byte[512];
+            int len = is.read(data);
+            String str = new String(data, 0, len);
+            assertEquals("this is my command\n", str);
+            s.rekey();
+        }
+        c.disconnect();
+        s.disconnect();
+    }
+
+    @Test
+    public void testReExchangeFromNativeClient() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("localhost", port).await().getSession();
+        session.authPassword("smx", "smx").await();
+        ChannelShell channel = session.createShellChannel();
+
+        ByteArrayOutputStream sent = new ByteArrayOutputStream();
+        PipedOutputStream pipedIn = new PipedOutputStream();
+        channel.setIn(new PipedInputStream(pipedIn));
+        OutputStream teeOut = new TeeOutputStream(sent, pipedIn);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ByteArrayOutputStream err = new ByteArrayOutputStream();
+        channel.setOut(out);
+        channel.setErr(err);
+        channel.open();
+
+        teeOut.write("this is my command\n".getBytes());
+        teeOut.flush();
+
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < 10; i++) {
+            sb.append("0123456789");
+        }
+        sb.append("\n");
+
+        for (int i = 0; i < 10; i++) {
+            teeOut.write(sb.toString().getBytes());
+            teeOut.flush();
+            session.reExchangeKeys().await();
+        }
+        teeOut.write("exit\n".getBytes());
+        teeOut.flush();
+
+        channel.waitFor(ClientChannel.CLOSED, 0);
+
+        channel.close(false);
+        client.stop();
+
+        assertArrayEquals(sent.toByteArray(), out.toByteArray());
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
index d7fea87..6cd2a08 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
@@ -150,8 +150,8 @@ public class ServerTest {
             public void sessionCreated(Session session) {
                 System.out.println("Session created");
             }
-            public void sessionChanged(Session session) {
-                System.out.println("Session changed");
+            public void sessionEvent(Session sesssion, Event event) {
+                System.out.println("Session event: " + event);
             }
             public void sessionClosed(Session session) {
                 System.out.println("Session closed");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8d226dd8/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
index 222fce0..c9e887e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
@@ -77,7 +77,7 @@ public class VirtualFileSystemTest {
 
     static class TestSession extends AbstractSession {
         TestSession() {
-            super(SshServer.setUpDefaultServer(), null);
+            super(true, SshServer.setUpDefaultServer(), null);
             this.username = "userName";
         }
         @Override
@@ -87,6 +87,21 @@ public class VirtualFileSystemTest {
         protected boolean readIdentification(Buffer buffer) throws IOException {
             return false;
         }
+        @Override
+        protected void sendKexInit() throws IOException {
+        }
+        @Override
+        protected void checkKeys() {
+        }
+        @Override
+        protected void receiveKexInit(Buffer buffer) throws IOException {
+        }
+        @Override
+        public void startService(String name) throws Exception {
+        }
+        @Override
+        public void resetIdleTimeout() {
+        }
     }
 
     static class TestFactoryManager extends AbstractFactoryManager {


[3/4] [SSHD-284] Do not use an enum for SSH commands to avoid clashes

Posted by gn...@apache.org.
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
index e747a97..6fc48e5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -383,7 +383,7 @@ public class ClientTest {
 
 //            ((AbstractSession) session).disconnect(SshConstants.SSH2_DISCONNECT_BY_APPLICATION, "Cancel");
             AbstractSession cs = (AbstractSession) session;
-            Buffer buffer = cs.createBuffer(SshConstants.Message.SSH_MSG_DISCONNECT, 0);
+            Buffer buffer = cs.createBuffer(SshConstants.SSH_MSG_DISCONNECT, 0);
             buffer.putInt(SshConstants.SSH2_DISCONNECT_BY_APPLICATION);
             buffer.putString("Cancel");
             buffer.putString("");


[2/4] git commit: Minor javadoc fix

Posted by gn...@apache.org.
Minor javadoc fix

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/8a8765ba
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/8a8765ba
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/8a8765ba

Branch: refs/heads/master
Commit: 8a8765bae3fa7d69df0d90fdb43290351f36fc10
Parents: 8d226dd
Author: Guillaume Nodet <gn...@apache.org>
Authored: Wed Jan 29 22:21:55 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Wed Jan 29 22:21:55 2014 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/sshd/client/kex/AbstractDHGClient.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8a8765ba/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
index c4d32b8..5cbd930 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
@@ -42,7 +42,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Base class for DHG key exchange algorithms.
  * Implementations will only have to configure the required data on the
- * {@link DH} class in the {@link #initDH(org.apache.sshd.common.kex.DH)} method.
+ * {@link DH} class in the {@link #getDH()} method.
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */


[4/4] git commit: [SSHD-284] Do not use an enum for SSH commands to avoid clashes

Posted by gn...@apache.org.
[SSHD-284] Do not use an enum for SSH commands to avoid clashes

Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/ade49e48
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/ade49e48
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/ade49e48

Branch: refs/heads/master
Commit: ade49e489abbf049dc761a55bdf3ee52a3aadbfe
Parents: 8a8765b
Author: Guillaume Nodet <gn...@apache.org>
Authored: Thu Jan 30 00:48:19 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Thu Jan 30 00:48:19 2014 +0100

----------------------------------------------------------------------
 .../sshd/agent/local/AgentForwardedChannel.java |   2 +-
 .../agent/local/ChannelAgentForwarding.java     |   2 +-
 .../sshd/agent/unix/AgentForwardedChannel.java  |   2 +-
 .../sshd/agent/unix/ChannelAgentForwarding.java |   2 +-
 .../apache/sshd/client/auth/UserAuthAgent.java  |  14 +-
 .../auth/UserAuthKeyboardInteractive.java       |  14 +-
 .../sshd/client/auth/UserAuthPassword.java      |  12 +-
 .../sshd/client/auth/UserAuthPublicKey.java     |  14 +-
 .../client/channel/AbstractClientChannel.java   |   2 +-
 .../sshd/client/channel/ChannelDirectTcpip.java |   4 +-
 .../apache/sshd/client/channel/ChannelExec.java |   2 +-
 .../sshd/client/channel/ChannelSession.java     |   2 +-
 .../sshd/client/channel/ChannelShell.java       |   8 +-
 .../sshd/client/channel/ChannelSubsystem.java   |   2 +-
 .../sshd/client/kex/AbstractDHGClient.java      |   8 +-
 .../java/org/apache/sshd/client/kex/DHGEX.java  |  19 +--
 .../client/session/ClientConnectionService.java |   2 +-
 .../sshd/client/session/ClientSessionImpl.java  |   2 +-
 .../client/session/ClientUserAuthService.java   |   4 +-
 .../java/org/apache/sshd/common/Service.java    |   2 +-
 .../java/org/apache/sshd/common/Session.java    |   2 +-
 .../org/apache/sshd/common/SshConstants.java    | 154 ++++++-------------
 .../sshd/common/channel/AbstractChannel.java    |  14 +-
 .../common/channel/ChannelOutputStream.java     |   8 +-
 .../common/forward/DefaultTcpipForwarder.java   |   4 +-
 .../sshd/common/forward/TcpipClientChannel.java |   4 +-
 .../sshd/common/forward/TcpipServerChannel.java |   2 +-
 .../session/AbstractConnectionService.java      |  22 +--
 .../sshd/common/session/AbstractSession.java    |  24 +--
 .../org/apache/sshd/common/util/Buffer.java     |  13 --
 .../auth/UserAuthKeyboardInteractive.java       |   6 +-
 .../sshd/server/auth/UserAuthPublicKey.java     |   4 +-
 .../sshd/server/auth/gss/UserAuthGSS.java       |  14 +-
 .../server/channel/AbstractServerChannel.java   |   2 +-
 .../sshd/server/channel/ChannelSession.java     |   4 +-
 .../global/CancelTcpipForwardHandler.java       |   2 +-
 .../sshd/server/global/TcpipForwardHandler.java |   2 +-
 .../sshd/server/kex/AbstractDHGServer.java      |   8 +-
 .../java/org/apache/sshd/server/kex/DHGEX.java  |  21 +--
 .../server/session/ServerUserAuthService.java   |  12 +-
 .../sshd/server/x11/X11ForwardSupport.java      |   4 +-
 .../test/java/org/apache/sshd/ClientTest.java   |   2 +-
 42 files changed, 196 insertions(+), 251 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentForwardedChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentForwardedChannel.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentForwardedChannel.java
index 8b29f70..84d51eb 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentForwardedChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/AgentForwardedChannel.java
@@ -75,7 +75,7 @@ public class AgentForwardedChannel extends AbstractClientChannel {
 
     @Override
     protected void doOpen() throws IOException {
-        invertedIn = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        invertedIn = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
     }
 
     protected void doWriteData(byte[] data, int off, int len) throws IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java b/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java
index 61aea86..23c8ee6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/local/ChannelAgentForwarding.java
@@ -65,7 +65,7 @@ public class ChannelAgentForwarding extends AbstractServerChannel {
     protected OpenFuture doInit(Buffer buffer) {
         final OpenFuture f = new DefaultOpenFuture(this);
         try {
-            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
             agent = session.getFactoryManager().getAgentFactory().createClient(session.getFactoryManager());
             client = new AgentClient();
             f.setOpened();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentForwardedChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentForwardedChannel.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentForwardedChannel.java
index 4f23584..d9f5e6f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentForwardedChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AgentForwardedChannel.java
@@ -62,7 +62,7 @@ public class AgentForwardedChannel extends AbstractClientChannel implements Runn
 
     @Override
     protected synchronized void doOpen() throws IOException {
-        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java
index 5dff9db..0887a1d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/ChannelAgentForwarding.java
@@ -66,7 +66,7 @@ public class ChannelAgentForwarding extends AbstractServerChannel {
     protected OpenFuture doInit(Buffer buffer) {
         final OpenFuture f = new DefaultOpenFuture(this);
         try {
-            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
             authSocket = session.getFactoryManager().getProperties().get(SshAgent.SSH_AUTHSOCKET_ENV_NAME);
             pool = Pool.create(AprLibrary.getInstance().getRootPool());
             handle = Local.create(authSocket, pool);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
index 5fe5926..7ceac6a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthAgent.java
@@ -49,7 +49,7 @@ public class UserAuthAgent extends AbstractUserAuth {
     protected void sendNextKey(PublicKey key) throws IOException {
         try {
             log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-            Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
             int pos1 = buffer.wpos() - 1;
             buffer.putString(username);
             buffer.putString(service);
@@ -62,7 +62,7 @@ public class UserAuthAgent extends AbstractUserAuth {
 
             Buffer bs = new Buffer();
             bs.putString(session.getKex().getH());
-            bs.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
+            bs.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             bs.putString(username);
             bs.putString(service);
             bs.putString("publickey");
@@ -93,12 +93,13 @@ public class UserAuthAgent extends AbstractUserAuth {
                 return Result.Failure;
             }
         } else {
-            SshConstants.Message cmd = buffer.getCommand();
-            log.info("Received {}", cmd);
-            if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+            byte cmd = buffer.getByte();
+            if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
+                log.info("Received SSH_MSG_USERAUTH_SUCCESS");
                 agent.close();
                 return Result.Success;
-            } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+            } if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
+                log.info("Received SSH_MSG_USERAUTH_FAILURE");
                 if (keys.hasNext()) {
                     sendNextKey(keys.next().getFirst());
                     return Result.Continued;
@@ -108,6 +109,7 @@ public class UserAuthAgent extends AbstractUserAuth {
                 }
             } else {
                 // TODO: check packets
+                log.info("Received unknown packet: {}", cmd);
                 return Result.Continued;
             }
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
index d3fa707..36cd4bb 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
@@ -23,9 +23,10 @@ import java.io.IOException;
 import org.apache.sshd.client.UserInteraction;
 import org.apache.sshd.client.session.ClientSessionImpl;
 import org.apache.sshd.common.SshConstants;
-import org.apache.sshd.common.SshConstants.Message;
 import org.apache.sshd.common.util.Buffer;
 
+import static org.apache.sshd.common.SshConstants.*;
+
 /**
  * Userauth with keyboard-interactive method.
  *
@@ -44,7 +45,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
     public Result next(Buffer buffer) throws IOException {
         if (buffer == null) {
             log.info("Send SSH_MSG_USERAUTH_REQUEST for password");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
             buffer.putString(username);
             buffer.putString(service);
             buffer.putString("keyboard-interactive");
@@ -53,10 +54,10 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
             session.writePacket(buffer);
             return Result.Continued;
         } else {
-            SshConstants.Message cmd = buffer.getCommand();
-            log.info("Received {}", cmd);
+            byte cmd = buffer.getByte();
             switch (cmd) {
                 case SSH_MSG_USERAUTH_INFO_REQUEST:
+                    log.info("Received SSH_MSG_USERAUTH_INFO_REQUEST");
                     String name = buffer.getString();
                     String instruction = buffer.getString();
                     String language_tag = buffer.getString();
@@ -87,7 +88,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
                         return Result.Failure;
                     }
 
-                    buffer = session.createBuffer(Message.SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
+                    buffer = session.createBuffer(SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
                     buffer.putInt(rep.length);
                     for (String r : rep) {
                         buffer.putString(r);
@@ -95,10 +96,13 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
                     session.writePacket(buffer);
                     return Result.Continued;
                 case SSH_MSG_USERAUTH_SUCCESS:
+                    log.info("Received SSH_MSG_USERAUTH_SUCCESS");
                     return Result.Success;
                 case SSH_MSG_USERAUTH_FAILURE:
+                    log.info("Received SSH_MSG_USERAUTH_FAILURE");
                     return Result.Failure;
                 default:
+                    log.info("Received unknown packet {}", cmd);
                     return Result.Continued;
             }
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
index cb50ab3..900e36c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
@@ -46,7 +46,7 @@ public class UserAuthPassword extends AbstractUserAuth {
     public Result next(Buffer buffer) throws IOException {
         if (buffer == null) {
             log.info("Send SSH_MSG_USERAUTH_REQUEST for password");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
             buffer.putString(username);
             buffer.putString(service);
             buffer.putString("password");
@@ -55,13 +55,15 @@ public class UserAuthPassword extends AbstractUserAuth {
             session.writePacket(buffer);
             return Result.Continued;
         } else {
-            SshConstants.Message cmd = buffer.getCommand();
-            log.info("Received {}", cmd);
-            if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+            byte cmd = buffer.getByte();
+            if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
+                log.info("Received SSH_MSG_USERAUTH_SUCCESS");
                 return Result.Success;
-            } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+            } if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
+                log.info("Received SSH_MSG_USERAUTH_FAILURE");
                 return Result.Failure;
             } else {
+                log.info("Received unkown packet {}", cmd);
                 // TODO: check packets
                 return Result.Continued;
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
index 97816a9..9804d2f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
@@ -52,7 +52,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
         if (buffer == null) {
             try {
                 log.info("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-                buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
                 int pos1 = buffer.wpos() - 1;
                 buffer.putString(username);
                 buffer.putString(service);
@@ -67,7 +67,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
 
                 Buffer bs = new Buffer();
                 bs.putString(session.getKex().getH());
-                bs.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
+                bs.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 bs.putString(username);
                 bs.putString(service);
                 bs.putString("publickey");
@@ -89,13 +89,15 @@ public class UserAuthPublicKey extends AbstractUserAuth {
                 throw (IOException) new IOException("Error performing public key authentication").initCause(e);
             }
         } else {
-            SshConstants.Message cmd = buffer.getCommand();
-            log.info("Received {}", cmd);
-            if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS) {
+            byte cmd = buffer.getByte();
+            if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
+                log.info("Received SSH_MSG_USERAUTH_SUCCESS");
                 return Result.Success;
-            } if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_FAILURE) {
+            } if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
+                log.info("Received SSH_MSG_USERAUTH_FAILURE");
                 return Result.Failure;
             } else {
+                log.info("Received unknown packet {}", cmd);
                 // TODO: check packets
                 return Result.Continued;
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
index 84c0a06..43c38d4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
@@ -188,7 +188,7 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
         }
         openFuture = new DefaultOpenFuture(lock);
         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
index d262d42..aa90440 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
@@ -66,7 +66,7 @@ public class ChannelDirectTcpip extends AbstractClientChannel {
         }
         openFuture = new DefaultOpenFuture(lock);
         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());
@@ -81,7 +81,7 @@ public class ChannelDirectTcpip extends AbstractClientChannel {
 
     @Override
     protected void doOpen() throws IOException {
-        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
         in = new PipedInputStream(pipe);
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
index 3650313..0708b2d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
@@ -41,7 +41,7 @@ public class ChannelExec extends ChannelSession {
 
     protected void doOpen() throws IOException {
         log.info("Send SSH_MSG_CHANNEL_REQUEST exec");
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
         buffer.putInt(recipient);
         buffer.putString("exec");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
index e18378a..a93a138 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
@@ -48,7 +48,7 @@ public class ChannelSession extends AbstractClientChannel {
 
     @Override
     protected void doOpen() throws IOException {
-        invertedIn = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        invertedIn = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
         if (out == null) {
             ChannelPipedInputStream pis = new ChannelPipedInputStream(localWindow);
             ChannelPipedOutputStream pos = new ChannelPipedOutputStream(pis);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
index a4ec085..f05a9af 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
@@ -153,7 +153,7 @@ public class ChannelShell extends ChannelSession {
 
         if (agentForwarding) {
             log.info("Send agent forwarding request");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
             buffer.putInt(recipient);
             buffer.putString("auth-agent-req@openssh.com");
             buffer.putBoolean(false);
@@ -162,7 +162,7 @@ public class ChannelShell extends ChannelSession {
 
         if (usePty) {
             log.info("Send SSH_MSG_CHANNEL_REQUEST pty-req");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
             buffer.putInt(recipient);
             buffer.putString("pty-req");
             buffer.putBoolean(false);
@@ -184,7 +184,7 @@ public class ChannelShell extends ChannelSession {
         if (!env.isEmpty()) {
             log.info("Send SSH_MSG_CHANNEL_REQUEST env");
             for (Map.Entry<String, String> entry : env.entrySet()) {
-                buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
                 buffer.putInt(recipient);
                 buffer.putString("env");
                 buffer.putBoolean(false);
@@ -195,7 +195,7 @@ public class ChannelShell extends ChannelSession {
         }
 
         log.info("Send SSH_MSG_CHANNEL_REQUEST shell");
-        buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
         buffer.putInt(recipient);
         buffer.putString("shell");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
index 965d486..304ee49 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
@@ -43,7 +43,7 @@ public class ChannelSubsystem extends ChannelSession {
 
     protected void doOpen() throws IOException {
         log.info("Send SSH_MSG_CHANNEL_REQUEST exec");
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
         buffer.putInt(recipient);
         buffer.putString("subsystem");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
index 5cbd930..f93bd83 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
@@ -78,7 +78,7 @@ public abstract class AbstractDHGClient implements KeyExchange {
         e = dh.getE();
 
         log.info("Send SSH_MSG_KEXDH_INIT");
-        Buffer buffer = s.createBuffer(SshConstants.Message.SSH_MSG_KEXDH_INIT, 0);
+        Buffer buffer = s.createBuffer(SshConstants.SSH_MSG_KEXDH_INIT, 0);
         buffer.putMPInt(e);
         session.writePacket(buffer);
     }
@@ -86,10 +86,10 @@ public abstract class AbstractDHGClient implements KeyExchange {
     protected abstract AbstractDH getDH() throws Exception;
 
     public boolean next(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        if (cmd != SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP) {
+        byte cmd = buffer.getByte();
+        if (cmd != SshConstants.SSH_MSG_KEXDH_REPLY) {
             throw new SshException(SshConstants.SSH2_DISCONNECT_KEY_EXCHANGE_FAILED,
-                                   "Protocol error: expected packet " + SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP + ", got " + cmd);
+                                   "Protocol error: expected packet SSH_MSG_KEXDH_REPLY, got " + cmd);
         }
 
         log.info("Received SSH_MSG_KEXDH_REPLY");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java b/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
index 4e82453..4eb033a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
@@ -78,7 +78,7 @@ public class DHGEX implements KeyExchange {
     private byte[] K;
     private byte[] H;
     private PublicKey serverKey;
-    private SshConstants.Message expected;
+    private byte expected;
 
     int min = 1024;
     int prf = 4096;
@@ -95,24 +95,24 @@ public class DHGEX implements KeyExchange {
         this.I_C = I_C;
 
         log.info("Send SSH_MSG_KEX_DH_GEX_REQUEST");
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_KEX_DH_GEX_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_REQUEST, 0);
         buffer.putInt(min);
         buffer.putInt(prf);
         buffer.putInt(max);
         session.writePacket(buffer);
 
-        expected = SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP;
+        expected = SshConstants.SSH_MSG_KEX_DH_GEX_GROUP;
     }
 
     public boolean next(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        log.info("Received " + cmd);
+        byte cmd = buffer.getByte();
         if (cmd != expected) {
             throw new SshException(SshConstants.SSH2_DISCONNECT_KEY_EXCHANGE_FAILED,
                     "Protocol error: expected packet " + expected + ", got " + cmd);
         }
 
-        if (cmd == SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP) {
+        if (cmd == SshConstants.SSH_MSG_KEX_DH_GEX_GROUP) {
+            log.info("Received SSH_MSG_KEX_DH_GEX_GROUP");
             p = buffer.getMPIntAsBytes();
             g = buffer.getMPIntAsBytes();
 
@@ -122,14 +122,15 @@ public class DHGEX implements KeyExchange {
             e = dh.getE();
 
             log.info("Send SSH_MSG_KEX_DH_GEX_INIT");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_KEX_DH_GEX_INIT, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_INIT, 0);
             buffer.putMPInt(e);
             session.writePacket(buffer);
-            expected = SshConstants.Message.SSH_MSG_KEX_DH_GEX_REPLY;
+            expected = SshConstants.SSH_MSG_KEX_DH_GEX_REPLY;
             return false;
         }
 
-        if (cmd == SshConstants.Message.SSH_MSG_KEX_DH_GEX_REPLY) {
+        if (cmd == SshConstants.SSH_MSG_KEX_DH_GEX_REPLY) {
+            log.info("Received SSH_MSG_KEX_DH_GEX_REPLY");
             byte[] K_S = buffer.getBytes();
             f = buffer.getMPIntAsBytes();
             byte[] sig = buffer.getBytes();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
index d183284..f6e6346 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
@@ -86,7 +86,7 @@ public class ClientConnectionService extends AbstractConnectionService {
 
     protected void sendHeartBeat() {
         try {
-            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_GLOBAL_REQUEST, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
             String request = session.getFactoryManager().getProperties().get(ClientFactoryManager.HEARTBEAT_REQUEST);
             if (request == null) {
                 request = "keepalive@sshd.apache.org";

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 42af0ff..6da6c0c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -326,7 +326,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         }
         initialServiceRequestSent = true;
         log.info("Send SSH_MSG_SERVICE_REQUEST for {}", currentServiceFactory.getName());
-        Buffer request = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_REQUEST, 0);
+        Buffer request = createBuffer(SshConstants.SSH_MSG_SERVICE_REQUEST, 0);
         request.putString(currentServiceFactory.getName());
         writePacket(request);
         // Assuming that MINA-SSHD only implements "explicit server authentication" it is permissible

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
index 9527d34..d98c510 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
@@ -92,13 +92,13 @@ public class ClientUserAuthService implements Service {
         }
     }
 
-    public void process(SshConstants.Message cmd, Buffer buffer) throws Exception {
+    public void process(byte cmd, Buffer buffer) throws Exception {
         if (this.authFuture.isSuccess()) {
             throw new IllegalStateException("UserAuth message delivered to authenticated client");
         } else if (this.authFuture.isDone()) {
             log.debug("Ignoring random message");
             // ignore for now; TODO: random packets
-        } else if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_BANNER) {
+        } else if (cmd == SshConstants.SSH_MSG_USERAUTH_BANNER) {
             String welcome = buffer.getString();
             String lang = buffer.getString();
             log.debug("Welcome banner: {}", welcome);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/Service.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Service.java b/sshd-core/src/main/java/org/apache/sshd/common/Service.java
index e3f60b3..02f2052 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Service.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Service.java
@@ -41,7 +41,7 @@ public interface Service {
      * @param buffer
      * @throws Exception
      */
-    void process(SshConstants.Message cmd, Buffer buffer) throws Exception;
+    void process(byte cmd, Buffer buffer) throws Exception;
 
     /**
      * Close the service.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Session.java b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
index 9b8bcae..9451fd8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
@@ -94,7 +94,7 @@ public interface Session {
      * @param estimatedSize estimated number of bytes the buffer will hold, 0 if unknown.
      * @return a new buffer ready for write
      */
-    Buffer createBuffer(SshConstants.Message cmd, int estimatedSize);
+    Buffer createBuffer(byte cmd, int estimatedSize);
 
     /**
      * Encode and send the given buffer.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java b/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
index 26148fd..1eda338 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/SshConstants.java
@@ -25,112 +25,54 @@ package org.apache.sshd.common;
  */
 public interface SshConstants {
 
-    /**
-     * SSH message identifiers
-     */
-    public enum Message {
-
-        SSH_MSG_DISCONNECT(1),
-        SSH_MSG_IGNORE(2),
-        SSH_MSG_UNIMPLEMENTED(3),
-        SSH_MSG_DEBUG(4),
-        SSH_MSG_SERVICE_REQUEST(5),
-        SSH_MSG_SERVICE_ACCEPT(6),
-        SSH_MSG_KEXINIT(20),
-        SSH_MSG_NEWKEYS(21),
-
-        SSH_MSG_KEXDH_INIT(30),
-
-        // KEXDH_REPLY and KEX_DH_GEX_GROUP have the same command ID
-        SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP(31),
-
-        SSH_MSG_KEX_DH_GEX_INIT(32),
-        SSH_MSG_KEX_DH_GEX_REPLY(33),
-        SSH_MSG_KEX_DH_GEX_REQUEST(34),
-
-        SSH_MSG_USERAUTH_REQUEST(50),
-        SSH_MSG_USERAUTH_FAILURE(51),
-        SSH_MSG_USERAUTH_SUCCESS(52),
-        SSH_MSG_USERAUTH_BANNER(53),
-        SSH_MSG_USERAUTH_INFO_REQUEST(60),
-        SSH_MSG_USERAUTH_INFO_RESPONSE(61),
-        SSH_MSG_USERAUTH_PK_OK(60),
-
-        SSH_MSG_GLOBAL_REQUEST(80),
-        SSH_MSG_REQUEST_SUCCESS(81),
-        SSH_MSG_REQUEST_FAILURE(82),
-        
-        SSH_MSG_USERAUTH_GSSAPI_MIC(66),        
-
-        SSH_MSG_CHANNEL_OPEN(90),
-        SSH_MSG_CHANNEL_OPEN_CONFIRMATION(91),
-        SSH_MSG_CHANNEL_OPEN_FAILURE(92),
-        SSH_MSG_CHANNEL_WINDOW_ADJUST(93),
-        SSH_MSG_CHANNEL_DATA(94),
-        SSH_MSG_CHANNEL_EXTENDED_DATA(95),
-        SSH_MSG_CHANNEL_EOF(96),
-        SSH_MSG_CHANNEL_CLOSE(97),
-        SSH_MSG_CHANNEL_REQUEST(98),
-        SSH_MSG_CHANNEL_SUCCESS(99),
-        SSH_MSG_CHANNEL_FAILURE(100);
-
-        private byte b;
-        private Message(int b) {
-            this.b = (byte) b;
-        }
-
-        public byte toByte() {
-            return b;
-        }
-
-        static Message[] commands;
-        static {
-            commands = new Message[256];
-            for (Message c : Message.values()) {
-                if (commands[c.toByte()] == null) {
-                    commands[c.toByte()] = c;
-                }
-            }
-        }
-        public static Message fromByte(byte b) {
-            return commands[b];
-        }
-    }
-
-    static final int SSH_MSG_DISCONNECT=                      1;
-    static final int SSH_MSG_IGNORE=                          2;
-    static final int SSH_MSG_UNIMPLEMENTED=                   3;
-    static final int SSH_MSG_DEBUG=                           4;
-    static final int SSH_MSG_SERVICE_REQUEST=                 5;
-    static final int SSH_MSG_SERVICE_ACCEPT=                  6;
-    static final int SSH_MSG_KEXINIT=                        20;
-    static final int SSH_MSG_NEWKEYS=                        21;
-
-    static final int SSH_MSG_KEX_FIRST=                      30;
-    static final int SSH_MSG_KEX_LAST=                       49;
-
-    static final int SSH_MSG_KEXDH_INIT=                     30;
-    static final int SSH_MSG_KEXDH_REPLY=                    31;
-
-    static final int SSH_MSG_KEX_DH_GEX_GROUP=               31;
-    static final int SSH_MSG_KEX_DH_GEX_INIT=                32;
-    static final int SSH_MSG_KEX_DH_GEX_REPLY=               33;
-    static final int SSH_MSG_KEX_DH_GEX_REQUEST=             34;
-
-    static final int SSH_MSG_GLOBAL_REQUEST=                 80;
-    static final int SSH_MSG_REQUEST_SUCCESS=                81;
-    static final int SSH_MSG_REQUEST_FAILURE=                82;
-    static final int SSH_MSG_CHANNEL_OPEN=                   90;
-    static final int SSH_MSG_CHANNEL_OPEN_CONFIRMATION=      91;
-    static final int SSH_MSG_CHANNEL_OPEN_FAILURE=           92;
-    static final int SSH_MSG_CHANNEL_WINDOW_ADJUST=          93;
-    static final int SSH_MSG_CHANNEL_DATA=                   94;
-    static final int SSH_MSG_CHANNEL_EXTENDED_DATA=          95;
-    static final int SSH_MSG_CHANNEL_EOF=                    96;
-    static final int SSH_MSG_CHANNEL_CLOSE=                  97;
-    static final int SSH_MSG_CHANNEL_REQUEST=                98;
-    static final int SSH_MSG_CHANNEL_SUCCESS=                99;
-    static final int SSH_MSG_CHANNEL_FAILURE=               100;
+    //
+    // SSH message identifiers
+    //
+
+    static final byte SSH_MSG_DISCONNECT=                      1;
+    static final byte SSH_MSG_IGNORE=                          2;
+    static final byte SSH_MSG_UNIMPLEMENTED=                   3;
+    static final byte SSH_MSG_DEBUG=                           4;
+    static final byte SSH_MSG_SERVICE_REQUEST=                 5;
+    static final byte SSH_MSG_SERVICE_ACCEPT=                  6;
+    static final byte SSH_MSG_KEXINIT=                        20;
+    static final byte SSH_MSG_NEWKEYS=                        21;
+
+    static final byte SSH_MSG_KEX_FIRST=                      30;
+    static final byte SSH_MSG_KEX_LAST=                       49;
+
+    static final byte SSH_MSG_KEXDH_INIT=                     30;
+    static final byte SSH_MSG_KEXDH_REPLY=                    31;
+
+    static final byte SSH_MSG_KEX_DH_GEX_GROUP=               31;
+    static final byte SSH_MSG_KEX_DH_GEX_INIT=                32;
+    static final byte SSH_MSG_KEX_DH_GEX_REPLY=               33;
+    static final byte SSH_MSG_KEX_DH_GEX_REQUEST=             34;
+
+    static final byte SSH_MSG_USERAUTH_REQUEST=               50;
+    static final byte SSH_MSG_USERAUTH_FAILURE=               51;
+    static final byte SSH_MSG_USERAUTH_SUCCESS=               52;
+    static final byte SSH_MSG_USERAUTH_BANNER=                53;
+    static final byte SSH_MSG_USERAUTH_INFO_REQUEST=          60;
+    static final byte SSH_MSG_USERAUTH_INFO_RESPONSE=         61;
+    static final byte SSH_MSG_USERAUTH_PK_OK=                 60;
+
+    static final byte SSH_MSG_USERAUTH_GSSAPI_MIC=            66;
+
+    static final byte SSH_MSG_GLOBAL_REQUEST=                 80;
+    static final byte SSH_MSG_REQUEST_SUCCESS=                81;
+    static final byte SSH_MSG_REQUEST_FAILURE=                82;
+    static final byte SSH_MSG_CHANNEL_OPEN=                   90;
+    static final byte SSH_MSG_CHANNEL_OPEN_CONFIRMATION=      91;
+    static final byte SSH_MSG_CHANNEL_OPEN_FAILURE=           92;
+    static final byte SSH_MSG_CHANNEL_WINDOW_ADJUST=          93;
+    static final byte SSH_MSG_CHANNEL_DATA=                   94;
+    static final byte SSH_MSG_CHANNEL_EXTENDED_DATA=          95;
+    static final byte SSH_MSG_CHANNEL_EOF=                    96;
+    static final byte SSH_MSG_CHANNEL_CLOSE=                  97;
+    static final byte SSH_MSG_CHANNEL_REQUEST=                98;
+    static final byte SSH_MSG_CHANNEL_SUCCESS=                99;
+    static final byte SSH_MSG_CHANNEL_FAILURE=               100;
 
     //
     // Values for the algorithms negociation 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
index 4e610bb..c1fdd84 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
@@ -103,14 +103,14 @@ public abstract class AbstractChannel implements Channel {
                     return;
                 case ReplySuccess:
                     if (wantReply) {
-                        buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_SUCCESS, 0);
+                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_SUCCESS, 0);
                         buffer.putInt(recipient);
                         session.writePacket(buffer);
                     }
                     return;
                 case ReplyFailure:
                     if (wantReply) {
-                        buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
+                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
                         buffer.putInt(recipient);
                         session.writePacket(buffer);
                     }
@@ -119,7 +119,7 @@ public abstract class AbstractChannel implements Channel {
         }
         log.warn("Unknown channel request: {}", req);
         if (wantReply) {
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
             buffer.putInt(recipient);
             session.writePacket(buffer);
         }
@@ -155,7 +155,7 @@ public abstract class AbstractChannel implements Channel {
                 preClose(immediately).addListener(new SshFutureListener<CloseFuture>() {
                     public void operationComplete(CloseFuture future) {
                         log.debug("Send SSH_MSG_CHANNEL_CLOSE on channel {}", id);
-                        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_CLOSE, 0);
+                        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE, 0);
                         buffer.putInt(recipient);
                         try {
                             session.writePacket(buffer).addListener(new SshFutureListener<IoWriteFuture>() {
@@ -227,7 +227,7 @@ public abstract class AbstractChannel implements Channel {
         // Only accept extended data for stderr
         if (ex != 1) {
             log.debug("Send SSH_MSG_CHANNEL_FAILURE on channel {}", id);
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
             buffer.putInt(recipient);
             writePacket(buffer);
             return;
@@ -266,7 +266,7 @@ public abstract class AbstractChannel implements Channel {
 
     protected void sendEof() throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_EOF on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_EOF, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_EOF, 0);
         buffer.putInt(recipient);
         writePacket(buffer);
     }
@@ -279,7 +279,7 @@ public abstract class AbstractChannel implements Channel {
 
     protected void sendWindowAdjust(int len) throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_WINDOW_ADJUST on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_WINDOW_ADJUST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_WINDOW_ADJUST, 0);
         buffer.putInt(recipient);
         buffer.putInt(len);
         writePacket(buffer);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
index e0441ba..6aeafbc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
@@ -37,7 +37,7 @@ public class ChannelOutputStream extends OutputStream {
     private final AbstractChannel channel;
     private final Window remoteWindow;
     private final Logger log;
-    private final SshConstants.Message cmd;
+    private final byte cmd;
     private final byte[] b = new byte[1];
     private Buffer buffer;
     private boolean closed;
@@ -45,7 +45,7 @@ public class ChannelOutputStream extends OutputStream {
     private int lastSize;
     private boolean noDelay = false;
 
-    public ChannelOutputStream(AbstractChannel channel, Window remoteWindow, Logger log, SshConstants.Message cmd) {
+    public ChannelOutputStream(AbstractChannel channel, Window remoteWindow, Logger log, byte cmd) {
         this.channel = channel;
         this.remoteWindow = remoteWindow;
         this.log = log;
@@ -115,7 +115,7 @@ public class ChannelOutputStream extends OutputStream {
                 int total = bufferLength;
                 int length = Math.min(Math.min(remoteWindow.waitForSpace(), total), remoteWindow.getPacketSize());
                 int pos = buf.wpos();
-                buf.wpos(cmd == SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA ? 14 : 10);
+                buf.wpos(cmd == SshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA ? 14 : 10);
                 buf.putInt(length);
                 buf.wpos(buf.wpos() + length);
                 if (total == length) {
@@ -150,7 +150,7 @@ public class ChannelOutputStream extends OutputStream {
     private void newBuffer(int size) {
         buffer = channel.getSession().createBuffer(cmd, size <= 0 ? 0 : 12 + size);
         buffer.putInt(channel.getRecipient());
-        if (cmd == SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA) {
+        if (cmd == SshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA) {
             buffer.putInt(1);
         }
         buffer.putInt(0);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
index 45d8612..1909f21 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
@@ -94,7 +94,7 @@ public class DefaultTcpipForwarder implements TcpipForwarder, IoHandler {
     }
 
     public synchronized SshdSocketAddress startRemotePortForwarding(SshdSocketAddress remote, SshdSocketAddress local) throws IOException {
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_GLOBAL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
         buffer.putString("tcpip-forward");
         buffer.putBoolean(true);
         buffer.putString(remote.getHostName());
@@ -111,7 +111,7 @@ public class DefaultTcpipForwarder implements TcpipForwarder, IoHandler {
 
     public synchronized void stopRemotePortForwarding(SshdSocketAddress remote) throws IOException {
         if (remoteToLocal.remove(remote.getPort()) != null) {
-            Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_GLOBAL_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
             buffer.putString("cancel-tcpip-forward");
             buffer.putBoolean(false);
             buffer.putString(remote.getHostName());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
index 93ab19d..e4badbf 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
@@ -80,7 +80,7 @@ public class TcpipClientChannel extends AbstractClientChannel {
         }
         openFuture = new DefaultOpenFuture(lock);
         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());
@@ -95,7 +95,7 @@ public class TcpipClientChannel extends AbstractClientChannel {
 
     @Override
     protected synchronized void doOpen() throws IOException {
-        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
index 48ee313..3ee8429 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
@@ -107,7 +107,7 @@ public class TcpipServerChannel extends AbstractServerChannel {
             return f;
         }
 
-        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
         IoHandler handler = new IoHandler() {
             public void messageReceived(IoSession session, Readable message) throws Exception {
                 if (closing.get()) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
index ce68594..a53c0a5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
@@ -44,6 +44,8 @@ import org.apache.sshd.server.x11.X11ForwardSupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.sshd.common.SshConstants.*;
+
 /**
  * Base implementation of ConnectionService.
  *
@@ -147,7 +149,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         channels.remove(channel.getId());
     }
 
-    public void process(SshConstants.Message cmd, Buffer buffer) throws Exception {
+    public void process(byte cmd, Buffer buffer) throws Exception {
         switch (cmd) {
             case SSH_MSG_CHANNEL_OPEN:
                 channelOpen(buffer);
@@ -307,7 +309,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         Channel channel = channels.get(recipient);
         if (channel == null) {
             buffer.rpos(buffer.rpos() - 5);
-            SshConstants.Message cmd = buffer.getCommand();
+            byte cmd = buffer.getByte();
             throw new SshException("Received " + cmd + " on unknown channel " + recipient);
         }
         return channel;
@@ -322,7 +324,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         log.debug("Received SSH_MSG_CHANNEL_OPEN {}", type);
 
         if (closing) {
-            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
             buf.putString("SSH server is shutting down: " + type);
@@ -331,7 +333,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
             return;
         }
         if (!allowMoreSessions) {
-            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
             buf.putString("additional sessions disabled");
@@ -342,7 +344,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
 
         final Channel channel = NamedFactory.Utils.create(session.getFactoryManager().getChannelFactories(), type);
         if (channel == null) {
-            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE);
             buf.putString("Unsupported channel type: " + type);
@@ -356,7 +358,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
             public void operationComplete(OpenFuture future) {
                 try {
                     if (future.isOpened()) {
-                        Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
+                        Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
                         buf.putInt(id);
                         buf.putInt(channelId);
                         buf.putInt(channel.getLocalWindow().getSize());
@@ -365,7 +367,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
                     } else {
                         Throwable exception = future.getException();
                         if (exception != null) {
-                            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+                            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
                             buf.putInt(id);
                             if (exception instanceof OpenChannelException) {
                                 buf.putInt(((OpenChannelException) exception).getReasonCode());
@@ -410,13 +412,13 @@ public abstract class AbstractConnectionService implements ConnectionService {
                         return;
                     case ReplySuccess:
                         if (wantReply) {
-                            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS, 0);
+                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
                             session.writePacket(buffer);
                         }
                         return;
                     case ReplyFailure:
                         if (wantReply) {
-                            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE, 0);
+                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE, 0);
                             session.writePacket(buffer);
                         }
                         return;
@@ -425,7 +427,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         }
         log.warn("Unknown global request: {}", req);
         if (wantReply) {
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_REQUEST_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE, 0);
             session.writePacket(buffer);
         }
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index b703dff..4fa7ff8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -54,6 +54,8 @@ import org.apache.sshd.common.util.Readable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.sshd.common.SshConstants.*;
+
 /**
  * The AbstractSession handles all the basic SSH protocol such as key exchange, authentication,
  * encoding and decoding. Both server side and client side sessions should inherit from this
@@ -287,7 +289,7 @@ public abstract class AbstractSession implements Session {
      * @throws Exception if an exeption occurs while handling this packet.
      */
     protected void handleMessage(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
+        byte cmd = buffer.getByte();
         switch (cmd) {
             case SSH_MSG_DISCONNECT: {
                 int code = buffer.getInt();
@@ -325,7 +327,7 @@ public abstract class AbstractSession implements Session {
                     break;
                 }
                 log.debug("Accepted service {}", service);
-                Buffer response = createBuffer(SshConstants.Message.SSH_MSG_SERVICE_ACCEPT, 0);
+                Buffer response = createBuffer(SshConstants.SSH_MSG_SERVICE_ACCEPT, 0);
                 response.putString(service);
                 writePacket(response);
                 break;
@@ -363,9 +365,9 @@ public abstract class AbstractSession implements Session {
                 break;
             default:
                 log.debug("Received {}", cmd);
-                if (cmd.toByte() >= SshConstants.SSH_MSG_KEX_FIRST && cmd.toByte() <= SshConstants.SSH_MSG_KEX_LAST) {
+                if (cmd >= SshConstants.SSH_MSG_KEX_FIRST && cmd <= SshConstants.SSH_MSG_KEX_LAST) {
                     if (kexState != KEX_STATE_RUN) {
-                        throw new IllegalStateException("Received kex command " + cmd.toByte() + " while not in key exchange");
+                        throw new IllegalStateException("Received kex command " + cmd + " while not in key exchange");
                     }
                     buffer.rpos(buffer.rpos() - 1);
                     if (kex.next(buffer)) {
@@ -512,7 +514,7 @@ public abstract class AbstractSession implements Session {
      * @param len estimated number of bytes the buffer will hold, 0 if unknown.
      * @return a new buffer ready for write
      */
-    public Buffer createBuffer(SshConstants.Message cmd, int len) {
+    public Buffer createBuffer(byte cmd, int len) {
         Buffer buffer;
         if (len <= 0) {
             buffer = new Buffer();
@@ -536,7 +538,7 @@ public abstract class AbstractSession implements Session {
         }
         buffer.rpos(5);
         buffer.wpos(5);
-        buffer.putByte(cmd.toByte());
+        buffer.putByte(cmd);
         return buffer;
     }
 
@@ -805,7 +807,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occured sending the packet
      */
     protected byte[] sendKexInit(String[] proposal) throws IOException {
-        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_KEXINIT, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_KEXINIT, 0);
         int p = buffer.wpos();
         buffer.wpos(p + 16);
         random.fill(buffer.array(), p, 16);
@@ -832,7 +834,7 @@ public abstract class AbstractSession implements Session {
         int size = 22;
         byte[] d = buffer.array();
         byte[] data = new byte[buffer.available() + 1];
-        data[0] = SshConstants.Message.SSH_MSG_KEXINIT.toByte();
+        data[0] = SshConstants.SSH_MSG_KEXINIT;
         System.arraycopy(d, buffer.rpos(), data, 1, data.length - 1);
         // Skip 16 bytes of random data
         buffer.rpos(buffer.rpos() + 16);
@@ -858,7 +860,7 @@ public abstract class AbstractSession implements Session {
      */
     protected void sendNewKeys() throws IOException {
         log.debug("Send SSH_MSG_NEWKEYS");
-        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_NEWKEYS, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_NEWKEYS, 0);
         writePacket(buffer);
     }
 
@@ -1006,7 +1008,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occured sending the packet
      */
     public void disconnect(int reason, String msg) throws IOException {
-        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_DISCONNECT, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_DISCONNECT, 0);
         buffer.putInt(reason);
         buffer.putString(msg);
         buffer.putString("");
@@ -1025,7 +1027,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occured sending the packet
      */
     protected void notImplemented() throws IOException {
-        Buffer buffer = createBuffer(SshConstants.Message.SSH_MSG_UNIMPLEMENTED, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_UNIMPLEMENTED, 0);
         buffer.putInt(seqi - 1);
         writePacket(buffer);
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/common/util/Buffer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/Buffer.java b/sshd-core/src/main/java/org/apache/sshd/common/util/Buffer.java
index 804eec6..42c0d50 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/Buffer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/Buffer.java
@@ -356,15 +356,6 @@ public final class Buffer implements Readable {
         return new KeyPair(pubKey, privKey);
     }
 
-    public SshConstants.Message getCommand() {
-        byte b = getByte();
-        SshConstants.Message cmd = SshConstants.Message.fromByte(b);
-        if (cmd == null) {
-            throw new IllegalStateException("Unknown command code: " + b);
-        }
-        return cmd;
-    }
-
     private void ensureAvailable(int a) {
         if (available() < a) {
             throw new BufferException("Underflow");
@@ -530,10 +521,6 @@ public final class Buffer implements Readable {
         }
     }
 
-    public void putCommand(SshConstants.Message cmd) {
-        putByte(cmd.toByte());
-    }
-
     private void ensureCapacity(int capacity) {
         if (data.length - wpos < capacity) {
             int cw = wpos + capacity;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
index fd12e59..989d4a6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
@@ -49,7 +49,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
         }
         if (init) {
             // Prompt for password
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
             buffer.putString("Password authentication");
             buffer.putString("");
             buffer.putString("en-US");
@@ -59,8 +59,8 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
             session.writePacket(buffer);
             return null;
         } else {
-            SshConstants.Message cmd = buffer.getCommand();
-            if (cmd != SshConstants.Message.SSH_MSG_USERAUTH_INFO_RESPONSE) {
+            byte cmd = buffer.getByte();
+            if (cmd != SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE) {
                 throw new SshException("Received unexepected message: " + cmd);
             }
             int num = buffer.getInt();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
index bcf8a19..e71957c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
@@ -79,7 +79,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
             return false;
         }
         if (!hasSig) {
-            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_PK_OK, 0);
             buf.putString(alg);
             buf.putRawBytes(buffer.array(), oldPos, 4 + len);
             session.writePacket(buf);
@@ -87,7 +87,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
         } else {
             Buffer buf = new Buffer();
             buf.putString(session.getKex().getH());
-            buf.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
+            buf.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             buf.putString(username);
             buf.putString("ssh-connection");
             buf.putString("publickey");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
index de22a2a..ca6bb41 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
@@ -85,7 +85,7 @@ public class UserAuthGSS extends AbstractUserAuth {
 
                     // Send the matching mechanism back to the client
 
-                    Buffer b = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
+                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
                     byte[] out = oid.getDER();
 
                     b.putBytes(out);
@@ -101,9 +101,9 @@ public class UserAuthGSS extends AbstractUserAuth {
         }
         else
         {
-            SshConstants.Message msg = buffer.getCommand();
-            if (!(msg == SshConstants.Message.SSH_MSG_USERAUTH_INFO_RESPONSE ||
-                    msg == SshConstants.Message.SSH_MSG_USERAUTH_GSSAPI_MIC && context.isEstablished())) {
+            byte msg = buffer.getByte();
+            if (!(msg == SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE ||
+                    msg == SshConstants.SSH_MSG_USERAUTH_GSSAPI_MIC && context.isEstablished())) {
                 throw new SshException(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR,
                         "Packet not supported by user authentication method");
             }
@@ -114,7 +114,7 @@ public class UserAuthGSS extends AbstractUserAuth {
 
             if (context.isEstablished()) {
 
-                if (msg != SshConstants.Message.SSH_MSG_USERAUTH_GSSAPI_MIC) {
+                if (msg != SshConstants.SSH_MSG_USERAUTH_GSSAPI_MIC) {
                     return Boolean.FALSE;
                 }
 
@@ -123,7 +123,7 @@ public class UserAuthGSS extends AbstractUserAuth {
                 Buffer msgbuf = new Buffer();
 
                 msgbuf.putString(session.getSessionId());
-                msgbuf.putByte(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST.toByte());
+                msgbuf.putByte(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 msgbuf.putString(username.getBytes("UTF-8"));
                 msgbuf.putString(service);
                 msgbuf.putString("gssapi-with-mic");
@@ -162,7 +162,7 @@ public class UserAuthGSS extends AbstractUserAuth {
                 // Send return token if necessary
 
                 if (out != null && out.length > 0) {
-                    Buffer b = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
+                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
 
                     b.putBytes(out);
                     session.writePacket(b);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
index e96f483..9655ab3 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
@@ -60,7 +60,7 @@ public abstract class AbstractServerChannel extends AbstractChannel {
         if (!exitStatusSent) {
             exitStatusSent = true;
             log.debug("Send SSH_MSG_CHANNEL_REQUEST exit-status on channel {}", id);
-            Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
             buffer.putInt(recipient);
             buffer.putString("exit-status");
             buffer.putByte((byte) 0);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
index 86e6ea6..a3e9144 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
@@ -446,8 +446,8 @@ public class ChannelSession extends AbstractServerChannel {
             FileSystemFactory factory = ((ServerSession) session).getServerFactoryManager().getFileSystemFactory();
             ((FileSystemAware) command).setFileSystemView(factory.createFileSystemView(session));
         }
-        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
-        err = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_EXTENDED_DATA);
+        out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
+        err = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA);
         if (log != null && log.isTraceEnabled()) {
             // Wrap in logging filters
             out = new LoggingFilterOutputStream(out, "OUT:", log);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java b/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
index 75fea9f..8e49f45 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
@@ -37,7 +37,7 @@ public class CancelTcpipForwardHandler implements RequestHandler<ConnectionServi
             int port = buffer.getInt();
             connectionService.getTcpipForwarder().localPortForwardingCancelled(new SshdSocketAddress(address, port));
             if (wantReply){
-                buffer = connectionService.getSession().createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS, 0);
+                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
                 buffer.putInt(port);
                 connectionService.getSession().writePacket(buffer);
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java b/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
index e94a197..2ee9478 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
@@ -38,7 +38,7 @@ public class TcpipForwardHandler implements RequestHandler<ConnectionService> {
             SshdSocketAddress bound = connectionService.getTcpipForwarder().localPortForwardingRequested(new SshdSocketAddress(address, port));
             port = bound.getPort();
             if (wantReply){
-                buffer = connectionService.getSession().createBuffer(SshConstants.Message.SSH_MSG_REQUEST_SUCCESS, 0);
+                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
                 buffer.putInt(port);
                 connectionService.getSession().writePacket(buffer);
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/kex/AbstractDHGServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/kex/AbstractDHGServer.java b/sshd-core/src/main/java/org/apache/sshd/server/kex/AbstractDHGServer.java
index 16e107a..dd42317 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/kex/AbstractDHGServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/kex/AbstractDHGServer.java
@@ -74,10 +74,10 @@ public abstract class AbstractDHGServer implements KeyExchange {
     protected abstract AbstractDH getDH() throws Exception;
 
     public boolean next(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        if (cmd != SshConstants.Message.SSH_MSG_KEXDH_INIT) {
+        byte cmd = buffer.getByte();
+        if (cmd != SshConstants.SSH_MSG_KEXDH_INIT) {
             throw new SshException(SshConstants.SSH2_DISCONNECT_KEY_EXCHANGE_FAILED, 
-                                   "Protocol error: expected packet " + SshConstants.Message.SSH_MSG_KEXDH_INIT + ", got " + cmd);
+                                   "Protocol error: expected packet " + SshConstants.SSH_MSG_KEXDH_INIT + ", got " + cmd);
         }
         log.debug("Received SSH_MSG_KEXDH_INIT");
         e = buffer.getMPIntAsBytes();
@@ -124,7 +124,7 @@ public abstract class AbstractDHGServer implements KeyExchange {
         buffer.clear();
         buffer.rpos(5);
         buffer.wpos(5);
-        buffer.putCommand(SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP);
+        buffer.putByte(SshConstants.SSH_MSG_KEXDH_REPLY);
         buffer.putString(K_S);
         buffer.putString(f);
         buffer.putString(sigH);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
index a75fe6a..592ce75 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
@@ -78,7 +78,7 @@ public class DHGEX implements KeyExchange {
     int min;
     int prf;
     int max;
-    private SshConstants.Message expected;
+    private byte expected;
 
     public void init(AbstractSession s, byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) throws Exception {
         if (!(s instanceof ServerSession)) {
@@ -90,18 +90,18 @@ public class DHGEX implements KeyExchange {
         this.I_S = I_S;
         this.I_C = I_C;
 
-        expected = SshConstants.Message.SSH_MSG_KEX_DH_GEX_REQUEST;
+        expected = SshConstants.SSH_MSG_KEX_DH_GEX_REQUEST;
     }
 
     public boolean next(Buffer buffer) throws Exception {
-        SshConstants.Message cmd = buffer.getCommand();
-        log.info("Received " + cmd);
+        byte cmd = buffer.getByte();
         if (cmd != expected) {
             throw new SshException(SshConstants.SSH2_DISCONNECT_KEY_EXCHANGE_FAILED,
                     "Protocol error: expected packet " + expected + ", got " + cmd);
         }
 
-        if (cmd == SshConstants.Message.SSH_MSG_KEX_DH_GEX_REQUEST) {
+        if (cmd == SshConstants.SSH_MSG_KEX_DH_GEX_REQUEST) {
+            log.info("Received SSH_MSG_KEX_DH_GEX_REQUEST");
             min = buffer.getInt();
             prf = buffer.getInt();
             max = buffer.getInt();
@@ -114,17 +114,18 @@ public class DHGEX implements KeyExchange {
             hash = dh.getHash();
             hash.init();
 
-            log.info("Send SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP");
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_KEXDH_REPLY_KEX_DH_GEX_GROUP, 0);
+            log.info("Send SSH_MSG_KEX_DH_GEX_GROUP");
+            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_GROUP, 0);
             buffer.putMPInt(dh.getP());
             buffer.putMPInt(dh.getG());
             session.writePacket(buffer);
 
-            expected = SshConstants.Message.SSH_MSG_KEX_DH_GEX_INIT;
+            expected = SshConstants.SSH_MSG_KEX_DH_GEX_INIT;
             return false;
         }
 
-        if (cmd == SshConstants.Message.SSH_MSG_KEX_DH_GEX_INIT) {
+        if (cmd == SshConstants.SSH_MSG_KEX_DH_GEX_INIT) {
+            log.info("Received SSH_MSG_KEX_DH_GEX_INIT");
             e = buffer.getMPIntAsBytes();
             dh.setF(e);
             K = dh.getK();
@@ -175,7 +176,7 @@ public class DHGEX implements KeyExchange {
             buffer.clear();
             buffer.rpos(5);
             buffer.wpos(5);
-            buffer.putCommand(SshConstants.Message.SSH_MSG_KEX_DH_GEX_REPLY);
+            buffer.putByte(SshConstants.SSH_MSG_KEX_DH_GEX_REPLY);
             buffer.putString(K_S);
             buffer.putString(f);
             buffer.putString(sigH);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
index 37a4e2e..5ad87ad 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
@@ -109,10 +109,10 @@ public class ServerUserAuthService implements Service {
         return session;
     }
 
-    public void process(SshConstants.Message cmd, Buffer buffer) throws Exception {
+    public void process(byte cmd, Buffer buffer) throws Exception {
         Boolean authed = Boolean.FALSE;
 
-        if (cmd == SshConstants.Message.SSH_MSG_USERAUTH_REQUEST) {
+        if (cmd == SshConstants.SSH_MSG_USERAUTH_REQUEST) {
             if (this.currentAuth != null) {
                 this.currentAuth.destroy();
                 this.currentAuth = null;
@@ -190,13 +190,13 @@ public class ServerUserAuthService implements Service {
 
                 String welcomeBanner = getFactoryManager().getProperties().get(ServerFactoryManager.WELCOME_BANNER);
                 if (welcomeBanner != null) {
-                    buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_BANNER, 0);
+                    buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_BANNER, 0);
                     buffer.putString(welcomeBanner);
                     buffer.putString("en");
                     session.writePacket(buffer);
                 }
 
-                buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_SUCCESS, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_SUCCESS, 0);
                 session.writePacket(buffer);
                 session.setAuthenticated(username);
                 session.startService(authService);
@@ -204,7 +204,7 @@ public class ServerUserAuthService implements Service {
                 log.info("Session {}@{} authenticated", username, session.getIoSession().getRemoteAddress());
 
             } else {
-                buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_FAILURE, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE, 0);
                 StringBuilder sb = new StringBuilder();
                 for (List<String> l : authMethods) {
                     if (!l.isEmpty()) {
@@ -224,7 +224,7 @@ public class ServerUserAuthService implements Service {
         } else {
             log.debug("Authentication failed");
 
-            buffer = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE, 0);
             StringBuilder sb = new StringBuilder();
             for (List<String> l : authMethods) {
                 if (!l.isEmpty()) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/ade49e48/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
index 7b5de22..7cc9e22 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
@@ -178,7 +178,7 @@ public class X11ForwardSupport implements IoHandler {
             }
             openFuture = new DefaultOpenFuture(lock);
             log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-            Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
             buffer.putString(type);
             buffer.putInt(id);
             buffer.putInt(localWindow.getSize());
@@ -191,7 +191,7 @@ public class X11ForwardSupport implements IoHandler {
 
         @Override
         protected synchronized void doOpen() throws IOException {
-            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
+            out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
         }
 
         @Override