public void Read_Protected_ResultBytesAreExpected()
        {
            //Arrange
            var messageBytes        = Utils.ParseHexString(GetProtectedMessageHex());
            var destConnectionId    = PacketConnectionId.Empty;
            var packetNumbers       = GetPacketNumbersHex();
            var currentPacketNumber = PacketNumber.Parse(Utils.ParseHexString(packetNumbers.Current));
            var largestPacketNumber = PacketNumber.Parse(Utils.ParseHexString(packetNumbers.Largest));
            var secrets             = GetSecrets();
            var aead   = Cipher.TLS_CHACHA20_POLY1305_SHA256.CreateAead(Utils.ParseHexString(secrets.Iv), Utils.ParseHexString(secrets.Key));
            var cipher = Cipher.TLS_CHACHA20_POLY1305_SHA256.CreateCipher(Utils.ParseHexString(secrets.Hp));

            //Act
            var cursor = new MemoryCursor(messageBytes);

            var result = ShortPacket.TryParseProtectedByConnectionId(aead, cipher, cursor, destConnectionId, largestPacketNumber, out var packet);

            using (packet.Payload.SetCursor(cursor))
            {
                result &= PingFrame.TryParse(cursor);

                result &= cursor.IsEnd();
            }

            result &= cursor.IsEnd();

            //Assert
            Assert.True(result);
            Assert.Equal(destConnectionId, packet.DestinationConnectionId);
            Assert.Equal(currentPacketNumber, packet.Number);
        }
        public void Write_Encrypted_ResultBytesAreExpected()
        {
            //Arrange
            var expectedBytes      = GetProtectedMessageHex();
            var buffer             = new byte[PacketBuffer.MaxPacketSize];
            var version            = PacketVersion.CreateByDraft(32);
            var connectionIds      = GetConnectionIdsHex();
            var sourceConnectionId = PacketConnectionId.Parse(Utils.ParseHexString(connectionIds.SourceConnectionIdHex));
            var destConnectionId   = PacketConnectionId.Parse(Utils.ParseHexString(connectionIds.DestConnectionIdHex));
            var packetNumber       = PacketNumber.Parse(Utils.ParseHexString(GetPacketNumberHex()));
            var token            = PacketToken.Empty;
            var clientHelloBytes = Utils.ParseHexString(GetTlsClientHelloHex());
            var secrets          = GetSecrets();
            var aead             = Cipher.TLS_AES_128_GCM_SHA256.CreateAead(Utils.ParseHexString(secrets.Iv), Utils.ParseHexString(secrets.Key));
            var cipher           = Cipher.TLS_AES_128_GCM_SHA256.CreateCipher(Utils.ParseHexString(secrets.Hp));

            //Act
            var cursor = new MemoryCursor(buffer);

            using (InitialPacket.StartProtectedWriting(aead, cipher, cursor, version, destConnectionId, sourceConnectionId, packetNumber, token))
            {
                using (PaddingFrame.EnsureLength(cursor, 1162))
                {
                    using (CryptoFrame.StartWriting(cursor, 0))
                    {
                        clientHelloBytes.CopyTo(cursor);
                    }
                }
            }

            //Assert
            Assert.Equal(expectedBytes, Utils.ToHexString(cursor.PeekStart().ToArray()), true);
        }
Example #3
0
        public void Encode_ByLargestAcknowledged_ResultIsExpected(string initial, string largest, string expected)
        {
            //Arrange
            var initialNumber  = PacketNumber.Parse(Utils.ParseHexString(initial));
            var largestNumber  = PacketNumber.Parse(Utils.ParseHexString(largest));
            var expectedNumber = PacketNumber.Parse(Utils.ParseHexString(expected));

            //Act
            var resultNumber = initialNumber.EncodeByLargestAcknowledged(largestNumber);

            //Assert
            Assert.Equal(expectedNumber, resultNumber);
        }
Example #4
0
        public void Decode_ByLargestAcknowledged_ResultIsExpected(string truncated, string largest, string expected)
        {
            //Arrange
            var truncatedNumber = PacketNumber.Parse(Utils.ParseHexString(truncated));
            var largestNumber   = PacketNumber.Parse(Utils.ParseHexString(largest));
            var expectedNumber  = PacketNumber.Parse(Utils.ParseHexString(expected));

            //Act
            var resultNumber = truncatedNumber.DecodeByLargestAcknowledged(largestNumber);

            //Assert
            Assert.Equal(expectedNumber, resultNumber);
        }
        public void Read_Protected_ResultsAreExpected()
        {
            //Arrange
            var messageBytes       = Utils.ParseHexString(GetProtectedMessageHex());
            var version            = PacketVersion.CreateByDraft(32);
            var connectionIds      = GetConnectionIdsHex();
            var sourceConnectionId = PacketConnectionId.Parse(Utils.ParseHexString(connectionIds.SourceConnectionIdHex));
            var destConnectionId   = PacketConnectionId.Parse(Utils.ParseHexString(connectionIds.DestConnectionIdHex));
            var packetNumber       = PacketNumber.Parse(Utils.ParseHexString(GetPacketNumberHex()));
            var token             = PacketToken.Empty;
            var clientHelloBytes  = GetTlsClientHelloHex();
            var cryptoFrame       = new CryptoFrame();
            var paddingFrameCount = 0;
            var secrets           = GetSecrets();
            var aead   = Cipher.TLS_AES_128_GCM_SHA256.CreateAead(Utils.ParseHexString(secrets.Iv), Utils.ParseHexString(secrets.Key));
            var cipher = Cipher.TLS_AES_128_GCM_SHA256.CreateCipher(Utils.ParseHexString(secrets.Hp));

            //Act
            var cursor = new MemoryCursor(messageBytes);
            var result = InitialPacket.TryParseProtected(aead, cipher, cursor, out var packet);

            using (packet.Payload.SetCursor(cursor))
            {
                result &= CryptoFrame.TryParse(cursor, out cryptoFrame);

                paddingFrameCount = PaddingFrame.SkipRange(cursor);

                result &= cursor.IsEnd();
            }

            result &= cursor.IsEnd();

            //Assert
            Assert.True(result);
            Assert.Equal(917, paddingFrameCount);
            Assert.Equal(version, packet.Version);
            Assert.Equal(sourceConnectionId, packet.SourceConnectionId);
            Assert.Equal(destConnectionId, packet.DestinationConnectionId);
            Assert.Equal(packetNumber, packet.Number);
            Assert.Equal(token, packet.Token);
            Assert.Equal(0, cryptoFrame.Offset);
            Assert.Equal(clientHelloBytes, Utils.ToHexString(cryptoFrame.Data.Read(cursor).ToArray()), true);
        }
        public void Write_Protected_ResultBytesAreExpected()
        {
            //Arrange
            var expectedBytes       = GetProtectedMessageHex();
            var buffer              = new byte[PacketBuffer.MaxPacketSize];
            var destConnectionId    = PacketConnectionId.Empty;
            var packetNumbers       = GetPacketNumbersHex();
            var currentPacketNumber = PacketNumber.Parse(Utils.ParseHexString(packetNumbers.Current));
            var largestPacketNumber = PacketNumber.Parse(Utils.ParseHexString(packetNumbers.Largest));
            var secrets             = GetSecrets();
            var aead   = Cipher.TLS_CHACHA20_POLY1305_SHA256.CreateAead(Utils.ParseHexString(secrets.Iv), Utils.ParseHexString(secrets.Key));
            var cipher = Cipher.TLS_CHACHA20_POLY1305_SHA256.CreateCipher(Utils.ParseHexString(secrets.Hp));

            //Act
            var cursor = new MemoryCursor(buffer);

            using (ShortPacket.StartProtectedWriting(aead, cipher, cursor, destConnectionId, currentPacketNumber, largestPacketNumber))
            {
                PingFrame.Write(cursor);
            }

            //Assert
            Assert.Equal(expectedBytes, Utils.ToHexString(cursor.PeekStart().ToArray()), true);
        }