/// <summary>
        /// Initializes a new instance of the <see cref="KeyExchangeInitMessage"/> class.
        /// </summary>
        public KeyExchangeInitMessage()
        {
            var cookie = new byte[16];

            CryptoAbstraction.GenerateRandom(cookie);
            Cookie = cookie;
        }
        public void GetBytes()
        {
            var random = new Random();

            var localChannelNumber = (uint)random.Next(0, int.MaxValue);
            var data   = CryptoAbstraction.GenerateRandom(random.Next(10, 20));
            var offset = random.Next(0, data.Length - 1);
            var size   = random.Next(0, data.Length - offset);

            var target = new ChannelDataMessage(localChannelNumber, data, offset, size);

            var bytes = target.GetBytes();

            var expectedBytesLength = 1; // Type

            expectedBytesLength += 4;    // LocalChannelNumber
            expectedBytesLength += 4;    // Data length
            expectedBytesLength += size; // Data

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(ChannelDataMessage.MessageNumber, sshDataStream.ReadByte());
            Assert.AreEqual(localChannelNumber, sshDataStream.ReadUInt32());
            Assert.AreEqual((uint)size, sshDataStream.ReadUInt32());

            var actualData = new byte[size];

            sshDataStream.Read(actualData, 0, size);
            Assert.IsTrue(actualData.SequenceEqual(data.Take(offset, size)));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
        private void SetupData()
        {
            var random = new Random();

            #region SftpSession.Connect()

            _operationTimeout = random.Next(100, 500);
            _protocolVersion  = (uint)random.Next(0, 3);
            _encoding         = Encoding.UTF8;

            _sftpInitRequestBytes = new SftpInitRequestBuilder().WithVersion(SftpSession.MaximumSupportedVersion)
                                    .Build()
                                    .GetBytes();
            _sftpVersionResponse = new SftpVersionResponseBuilder().WithVersion(_protocolVersion)
                                   .Build();
            _sftpRealPathRequestBytes = new SftpRealPathRequestBuilder().WithProtocolVersion(_protocolVersion)
                                        .WithRequestId(1)
                                        .WithPath(".")
                                        .WithEncoding(_encoding)
                                        .Build()
                                        .GetBytes();
            _sftpNameResponse = new SftpNameResponseBuilder().WithProtocolVersion(_protocolVersion)
                                .WithResponseId(1)
                                .WithEncoding(_encoding)
                                .WithFile("/ABC", SftpFileAttributes.Empty)
                                .Build();

            #endregion SftpSession.Connect()

            _path   = random.Next().ToString();
            _handle = CryptoAbstraction.GenerateRandom(4);
            _offset = (uint)random.Next(1, 5);
            _length = (uint)random.Next(30, 50);
            _data   = CryptoAbstraction.GenerateRandom(200);
            _sftpOpenRequestBytes = new SftpOpenRequestBuilder().WithProtocolVersion(_protocolVersion)
                                    .WithRequestId(2)
                                    .WithFileName(_path)
                                    .WithFlags(Flags.Read)
                                    .WithEncoding(_encoding)
                                    .Build()
                                    .GetBytes();
            _sftpHandleResponseBytes = new SftpHandleResponseBuilder().WithProtocolVersion(_protocolVersion)
                                       .WithResponseId(2)
                                       .WithHandle(_handle)
                                       .Build()
                                       .GetBytes();
            _sftpReadRequestBytes = new SftpReadRequestBuilder().WithProtocolVersion(_protocolVersion)
                                    .WithRequestId(3)
                                    .WithHandle(_handle)
                                    .WithOffset(_offset)
                                    .WithLength(_length)
                                    .Build()
                                    .GetBytes();
            _sftpDataResponseBytes = new SftpDataResponseBuilder().WithProtocolVersion(_protocolVersion)
                                     .WithResponseId(3)
                                     .WithData(_data)
                                     .Build()
                                     .GetBytes();
        }
Exemple #4
0
        public void ShouldGenerateRandomSequenceOfValues()
        {
            var dataLength = new Random().Next(1, 100);
            var dataA      = new byte[dataLength];
            var dataB      = new byte[dataLength];

            CryptoAbstraction.GenerateRandom(dataA);
            CryptoAbstraction.GenerateRandom(dataB);

            Assert.IsFalse(dataA.SequenceEqual(dataB));
        }
        private void SetupData()
        {
            var random = new Random();

            _bufferSize      = (uint)random.Next(1, int.MaxValue);
            _openAsyncResult = new SftpOpenAsyncResult(null, null);
            _handle          = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
            _statAsyncResult = new SFtpStatAsyncResult(null, null);
            _fileName        = random.Next().ToString();
            _chunkSize       = (uint)random.Next(1, int.MaxValue);
        }
        private void SetupData()
        {
            var random = new Random();

            _bufferSize      = (uint)random.Next(1, int.MaxValue);
            _openAsyncResult = new SftpOpenAsyncResult(null, null);
            _handle          = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
            _statAsyncResult = new SFtpStatAsyncResult(null, null);
            _fileName        = random.Next().ToString();
            _chunkSize       = (uint)random.Next(1000, 5000);
            _fileSize        = (_chunkSize * 5) + 10;
            _fileAttributes  = new SftpFileAttributesBuilder().WithSize(_fileSize).Build();
        }
Exemple #7
0
        public void ShouldThrowArgumentNullExceptionWhenDataIsNull()
        {
            const byte[] data = null;

            try
            {
                CryptoAbstraction.GenerateRandom(data);
                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.IsNull(ex.InnerException);
                Assert.AreEqual("data", ex.ParamName);
            }
        }
Exemple #8
0
        private void SetupData()
        {
            var random = new Random();

            _terminalName  = random.Next().ToString();
            _widthColumns  = (uint)random.Next();
            _heightRows    = (uint)random.Next();
            _widthPixels   = (uint)random.Next();
            _heightPixels  = (uint)random.Next();
            _terminalModes = new Dictionary <TerminalModes, uint>();
            _bufferSize    = random.Next(100, 1000);

            _data   = CryptoAbstraction.GenerateRandom(_bufferSize);
            _offset = 0;
            _count  = _data.Length;
        }
        public void Load()
        {
            var random = new Random();

            var localChannelNumber = (uint)random.Next(0, int.MaxValue);
            var data = CryptoAbstraction.GenerateRandom(random.Next(10, 20));

            var offset             = random.Next(0, data.Length - 1);
            var size               = random.Next(0, data.Length - offset);
            var channelDataMessage = new ChannelDataMessage(localChannelNumber, data, offset, size);
            var bytes              = channelDataMessage.GetBytes();
            var target             = new ChannelDataMessage();

            target.Load(bytes, 1, bytes.Length - 1); // skip message type

            Assert.IsTrue(target.Data.SequenceEqual(data.Take(offset, size)));
            Assert.AreEqual(0, target.Offset);
            Assert.AreEqual(size, target.Size);
        }
        private void SetupData()
        {
            var random = new Random();

            _terminalName  = random.Next().ToString();
            _widthColumns  = (uint)random.Next();
            _heightRows    = (uint)random.Next();
            _widthPixels   = (uint)random.Next();
            _heightPixels  = (uint)random.Next();
            _terminalModes = new Dictionary <TerminalModes, uint>();
            _bufferSize    = random.Next(100, 1000);

            _bufferData = CryptoAbstraction.GenerateRandom(_bufferSize - 60);
            _data       = CryptoAbstraction.GenerateRandom(_bufferSize - _bufferData.Length + random.Next(1, 10));
            _offset     = 0;
            _count      = _data.Length;

            _expectedBytesSent = new ArrayBuilder <byte>().Add(_bufferData)
                                 .Add(_data, 0, _bufferSize - _bufferData.Length)
                                 .Build();
        }
Exemple #11
0
        public void ShouldPerformNoOpWhenDataIsZeroLength()
        {
            var data = new byte[0];

            CryptoAbstraction.GenerateRandom(data);
        }
Exemple #12
0
        internal byte[] GetPacket(byte paddingMultiplier, Compressor compressor)
        {
            const int outboundPacketSequenceSize = 4;

            var messageLength = BufferCapacity;

            SshDataStream sshDataStream;

            if (messageLength == -1 || compressor != null)
            {
                sshDataStream = new SshDataStream(DefaultCapacity);

                // skip:
                // * 4 bytes for the outbound packet sequence
                // * 4 bytes for the packet data length
                // * one byte for the packet padding length
                sshDataStream.Seek(outboundPacketSequenceSize + 4 + 1, SeekOrigin.Begin);

                if (compressor != null)
                {
                    // obtain uncompressed message payload
                    var uncompressedDataStream = new SshDataStream(messageLength != -1 ? messageLength : DefaultCapacity);
                    WriteBytes(uncompressedDataStream);

                    // compress message payload
                    var compressedMessageData = compressor.Compress(uncompressedDataStream.ToArray());

                    // add compressed message payload
                    sshDataStream.Write(compressedMessageData, 0, compressedMessageData.Length);
                }
                else
                {
                    // add message payload
                    WriteBytes(sshDataStream);
                }

                messageLength = (int)sshDataStream.Length - (outboundPacketSequenceSize + 4 + 1);

                var packetLength = messageLength + 4 + 1;

                // determine the padding length
                var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);

                // add padding bytes
                var paddingBytes = new byte[paddingLength];
                CryptoAbstraction.GenerateRandom(paddingBytes);
                sshDataStream.Write(paddingBytes, 0, paddingLength);

                var packetDataLength = GetPacketDataLength(messageLength, paddingLength);

                // skip bytes for outbound packet sequence
                sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);

                // add packet data length
                sshDataStream.Write(packetDataLength.GetBytes(), 0, 4);

                //  add packet padding length
                sshDataStream.WriteByte(paddingLength);
            }
            else
            {
                var packetLength = messageLength + 4 + 1;

                // determine the padding length
                var paddingLength = GetPaddingLength(paddingMultiplier, packetLength);

                var packetDataLength = GetPacketDataLength(messageLength, paddingLength);

                // lets construct an SSH data stream of the exact size required
                sshDataStream = new SshDataStream(packetLength + paddingLength + outboundPacketSequenceSize);

                // skip bytes for outbound packet sequenceSize
                sshDataStream.Seek(outboundPacketSequenceSize, SeekOrigin.Begin);

                // add packet data length
                sshDataStream.Write(packetDataLength.GetBytes(), 0, 4);

                //  add packet padding length
                sshDataStream.WriteByte(paddingLength);

                // add message payload
                WriteBytes(sshDataStream);

                // add padding bytes
                var paddingBytes = new byte[paddingLength];
                CryptoAbstraction.GenerateRandom(paddingBytes);
                sshDataStream.Write(paddingBytes, 0, paddingLength);
            }

            return(sshDataStream.ToArray());
        }