]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Add unit test to confirm malformed GCM record bug
authorMatthew Endsley <mendsley@gmail.com>
Wed, 31 Mar 2021 19:16:11 +0000 (12:16 -0700)
committerMatthew Endsley <mendsley@gmail.com>
Wed, 31 Mar 2021 19:46:04 +0000 (12:46 -0700)
Hazel.UnitTests/Dtls/ConnectionTests.cs
Hazel/FewerThreads/ThreadLimitedUdpServerConnection.cs

index 0a8077eb92d1eab2950e2982c6332fa9aaa68b8b..3279ce2430802d72036b306a845675928e016657 100644 (file)
@@ -2,6 +2,7 @@ using Hazel.Dtls;
 using Hazel.Udp;
 using Hazel.Udp.FewerThreads;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
 using System.Net;
 using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
@@ -148,5 +149,82 @@ IsdbLCwHYD3GVgk/D7NVxyU=
                 Assert.IsFalse(serverDisconnected);
             }
         }
+
+        class MalformedDTLSListener : DtlsConnectionListener
+        {
+            public MalformedDTLSListener(int numWorkers, IPEndPoint endPoint, ILogger logger, IPMode ipMode = IPMode.IPv4)
+                : base(numWorkers, endPoint, logger, ipMode)
+            {
+            }
+
+            public void InjectPacket(ByteSpan packet, IPEndPoint peerAddress, ConnectionId connectionId)
+            {
+                MessageReader reader = MessageReader.GetSized(packet.Length);
+                reader.Length = packet.Length;
+                Array.Copy(packet.GetUnderlyingArray(), packet.Offset, reader.Buffer, reader.Offset, packet.Length);
+
+                this.ProcessIncomingMessageFromOtherThread(reader, peerAddress, connectionId);
+            }
+
+            protected override void ProcessIncomingMessageFromOtherThread(MessageReader reader, IPEndPoint peerAddress, ConnectionId connectionId)
+            {
+                base.ProcessIncomingMessageFromOtherThread(reader, peerAddress, connectionId);
+            }
+        }
+
+        [TestMethod]
+        public void TestMalformedApplicationData()
+        {
+            IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 27510);
+
+            IPEndPoint connectionEndPoint = ep;
+            DtlsConnectionListener.ConnectionId connectionId = new ThreadLimitedUdpConnectionListener.ConnectionId();
+
+            Semaphore signal = new Semaphore(0, int.MaxValue);
+
+            using (MalformedDTLSListener listener = new MalformedDTLSListener(2, new IPEndPoint(IPAddress.Any, ep.Port), new TestLogger()))
+            using (DtlsUnityConnection connection = new DtlsUnityConnection(new TestLogger(), ep))
+            {
+                listener.SetCertificate(GetCertificateForServer());
+                connection.SetValidServerCertificates(GetCertificateForClient());
+
+                listener.NewConnection += (evt) =>
+                {
+                    connectionEndPoint = evt.Connection.EndPoint;
+                    connectionId = ((ThreadLimitedUdpServerConnection)evt.Connection).ConnectionId;
+
+                    signal.Release();
+                    evt.Connection.Disconnected += (o, et) => {
+                    };
+                };
+                connection.Disconnected += (o, evt) => {
+                    signal.Release();
+                };
+
+                listener.Start();
+                connection.Connect();
+
+                // wait for the client to connect
+                signal.WaitOne(10);
+
+                ByteSpan data = new byte[5] { 0x01, 0x02, 0x03, 0x04, 0x05 };
+
+                Record record = new Record();
+                record.ContentType = ContentType.ApplicationData;
+                record.Epoch = 1;
+                record.SequenceNumber = 10;
+                record.Length = (ushort)data.Length;
+
+                ByteSpan encoded = new byte[Record.Size + data.Length];
+                record.Encode(encoded);
+                data.CopyTo(encoded.Slice(Record.Size));
+
+                listener.InjectPacket(encoded, connectionEndPoint, connectionId);
+
+                // wait for the client to disconnect
+                listener.Dispose();
+                signal.WaitOne(100);
+            }
+        }
     }
 }
index 0cb864f12cdbd7838e25cd61e02de24ff032854e..2f57794dc7781545eba711a26ca35aacb8ec0c99 100644 (file)
@@ -20,7 +20,7 @@ namespace Hazel.Udp.FewerThreads
         /// </remarks>
         public ThreadLimitedUdpConnectionListener Listener { get; private set; }
 
-        private ThreadLimitedUdpConnectionListener.ConnectionId ConnectionId;
+        public ThreadLimitedUdpConnectionListener.ConnectionId ConnectionId { get; private set; }
 
         /// <summary>
         ///     Creates a UdpConnection for the virtual connection to the endpoint.