Skip to content

016: Implement no compression

Shane DeSeranno edited this page Feb 15, 2017 · 3 revisions

This one is an easy section. We don't need to support compression, but we do need to tell the client that we support "none" as an option. So create an folder called Compressions and create an interface ICompression that implements IAlgorithm:

    public interface ICompression : IAlgorithm
    {
        byte[] Compress(byte[] data);
        byte[] Decompress(byte[] data);
    }

This interface just supports Compress() and Decompress(). Now create a NoCompression class that inherits from ICompression:

    public class NoCompression : ICompression
    {
        public string Name
        {
            get
            {
                return "none";
            }
        }

        public byte[] Compress(byte[] data)
        {
            return data;
        }

        public byte[] Decompress(byte[] data)
        {
            return data;
        }
    }

This is about as easy at they get! Now add the SupportedCompressions to the server:

        public static IReadOnlyList<Type> SupportedCompressions { get; private set; } = new List<Type>()
        {
            typeof(NoCompression)
        };

And add the supported compressions to the KEX packet in Client:

    m_KexInitServerToClient.CompressionAlgorithmsClientToServer.AddRange(Server.GetNames(Server.SupportedCompressions));
    m_KexInitServerToClient.CompressionAlgorithmsServerToClient.AddRange(Server.GetNames(Server.SupportedCompressions));

Once again, run the server and you won't see any change but OpenSSH reveals the truth:

debug2: KEX algorithms: diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa
debug2: ciphers ctos: 3des-cbc
debug2: ciphers stoc: 3des-cbc
debug2: MACs ctos: hmac-sha1
debug2: MACs stoc: hmac-sha1
debug2: compression ctos: none
debug2: compression stoc: none
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: diffie-hellman-group14-sha1
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: 3des-cbc MAC: hmac-sha1 compression: none
debug1: kex: client->server cipher: 3des-cbc MAC: hmac-sha1 compression: none
debug1: sending SSH2_MSG_KEXDH_INIT
debug2: bits set: 1039/2048
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEXDH_REPLY
Connection closed by 127.0.0.1 port 22

It selected all of our algorithms and sent a SSH2_MSG_KEXDH_INIT (Packet Type 30) and is waiting for our reply of SSH2_MSG_KEXDH_REPLY! This is great! The code for this page is tagged Implement_No_Compression. The next step is going to require a bit of explanation and a few walls of code, but we need to plumb through something I call the ExchangeContext. When you are ready proceed to setting up the exchange context.