summary refs log tree commit diff
path: root/crypto/src/tls/HandshakeMessageOutput.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/tls/HandshakeMessageOutput.cs')
-rw-r--r--crypto/src/tls/HandshakeMessageOutput.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/crypto/src/tls/HandshakeMessageOutput.cs b/crypto/src/tls/HandshakeMessageOutput.cs
new file mode 100644
index 000000000..ae07b9682
--- /dev/null
+++ b/crypto/src/tls/HandshakeMessageOutput.cs
@@ -0,0 +1,62 @@
+using System;
+using System.IO;
+
+using Org.BouncyCastle.Utilities;
+
+namespace Org.BouncyCastle.Tls
+{
+    internal sealed class HandshakeMessageOutput
+        : MemoryStream
+    {
+        internal static int GetLength(int bodyLength)
+        {
+            return 4 + bodyLength;
+        }
+
+        /// <exception cref="IOException"/>
+        internal static void Send(TlsProtocol protocol, short handshakeType, byte[] body)
+        {
+            HandshakeMessageOutput message = new HandshakeMessageOutput(handshakeType, body.Length);
+            message.Write(body, 0, body.Length);
+            message.Send(protocol);
+        }
+
+        /// <exception cref="IOException"/>
+        internal HandshakeMessageOutput(short handshakeType)
+            : this(handshakeType, 60)
+        {
+        }
+
+        /// <exception cref="IOException"/>
+        internal HandshakeMessageOutput(short handshakeType, int bodyLength)
+            : base(GetLength(bodyLength))
+        {
+            TlsUtilities.CheckUint8(handshakeType);
+            TlsUtilities.WriteUint8(handshakeType, this);
+            // Reserve space for length
+            Seek(3L, SeekOrigin.Current);
+        }
+
+        /// <exception cref="IOException"/>
+        internal void Send(TlsProtocol protocol)
+        {
+            // Patch actual length back in
+            int bodyLength = (int)Length - 4;
+            TlsUtilities.CheckUint24(bodyLength);
+
+            Seek(1L, SeekOrigin.Begin);
+            TlsUtilities.WriteUint24(bodyLength, this);
+
+#if PORTABLE
+            byte[] buf = ToArray();
+            int count = buf.Length;
+#else
+            byte[] buf = GetBuffer();
+            int count = (int)Length;
+#endif
+            protocol.WriteHandshakeMessage(buf, 0, count);
+
+            Platform.Dispose(this);
+        }
+    }
+}