From 3b720c081ba1906dc6d64345c233e145f056f9ff Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 9 Jun 2021 22:33:58 -0700 Subject: [PATCH] Added WebSocket masking. --- WebSocketClient.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/WebSocketClient.cs b/WebSocketClient.cs index 950344c..61fd8a9 100644 --- a/WebSocketClient.cs +++ b/WebSocketClient.cs @@ -62,6 +62,7 @@ namespace MeshCentralRouter public long PendingSendLength { get { return (pendingSendBuffer == null)? 0 : pendingSendBuffer.Length; } } private bool readPaused = false; private bool shouldRead = false; + private RNGCryptoServiceProvider CryptoRandom = new RNGCryptoServiceProvider(); // Outside variables public object tag = null; @@ -644,6 +645,12 @@ namespace MeshCentralRouter // Check that everything is ok if (len < 0) { Dispose(); return 0; } + // Set the mask to a cryptographic random value and XOR the data + byte[] rand = new byte[4]; + CryptoRandom.GetBytes(rand); + Array.Copy(rand, 0, buf, 10, 4); + for (int x = 0; x < len; x++) { buf[x + 14] ^= rand[x % 4]; } + if (len < 126) { // Small fragment