]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Add record wire format conversions
authorMatthew Endsley <mendsley@gmail.com>
Wed, 13 Jan 2021 09:30:55 +0000 (01:30 -0800)
committerMatthew Endsley <mendsley@gmail.com>
Tue, 2 Feb 2021 16:53:32 +0000 (08:53 -0800)
Hazel/Dtls/Record.cs [new file with mode: 0644]
Hazel/Hazel.csproj

diff --git a/Hazel/Dtls/Record.cs b/Hazel/Dtls/Record.cs
new file mode 100644 (file)
index 0000000..affb8a6
--- /dev/null
@@ -0,0 +1,76 @@
+namespace Hazel.Dtls
+{
+    /// <summary>
+    /// DTLS version constants
+    /// </summary>
+    public enum ProtocolVersion : ushort
+    {
+        /// <summary>
+        /// DTLS 1.2
+        /// </summary>
+        DTLS1_2 = 0xFEFD,
+    }
+
+    /// <summary>
+    /// DTLS record content type
+    /// </summary>
+    public enum ContentType : byte
+    {
+        ChangeCipherSpec = 20,
+        Alert = 21,
+        Handshake = 22,
+        ApplicationData = 23,
+    }
+
+    /// <summary>
+    /// Encode/decode DTLS record header
+    /// </summary>
+    public struct Record
+    {
+        public ContentType ContentType;
+        public ushort Epoch;
+        public ulong SequenceNumber;
+        public ushort Length;
+
+        public const int Size = 13;
+
+        /// <summary>
+        /// Parse a DTLS record from wire format
+        /// </summary>
+        /// <returns>True if we successfully parse the record header. Otherwise false</returns>
+        public static bool Parse(out Record record, ByteSpan span)
+        {
+            record = new Record();
+
+            if (span.Length < Size)
+            {
+                return false;
+            }
+
+            record.ContentType = (ContentType)span[0];
+            ProtocolVersion version = (ProtocolVersion)span.ReadBigEndian16(1);
+            record.Epoch = span.ReadBigEndian16(3);
+            record.SequenceNumber = span.ReadBigEndian48(5);
+            record.Length = span.ReadBigEndian16(11);
+
+            if (version != ProtocolVersion.DTLS1_2)
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        /// <summary>
+        /// Encode a DTLS record to wire format
+        /// </summary>
+        public void Encode(ByteSpan span)
+        {
+            span[0] = (byte)this.ContentType;
+            span.WriteBigEndian16((ushort)ProtocolVersion.DTLS1_2);
+            span.WriteBigEndian16(this.Epoch, 3);
+            span.WriteBigEndian48(this.SequenceNumber, 5);
+            span.WriteBigEndian16(this.Length, 11);
+        }
+    }
+}
index e75bb1e512dcadaf542d66d7b8c0a3e1e0a00bce..e102524b40de3ed944963154e0115d529ded321b 100644 (file)
@@ -81,6 +81,7 @@
     <Compile Include="Dtls\Handshake.cs" />
     <Compile Include="Dtls\IHandshakeCipherSuite.cs" />
     <Compile Include="Dtls\PrfSha256.cs" />
+    <Compile Include="Dtls\Record.cs" />
     <Compile Include="Dtls\X25519EcdheRsaSha256.cs" />
     <Compile Include="FewerThreads\HazelThreadPool.cs" />
     <Compile Include="FewerThreads\ThreadLimitedUdpConnectionListener.cs" />