[Asterisk-code-review] res_http_websocket: Add data masking to the websocket client (asterisk[master])

Moises Silva asteriskteam at digium.com
Sat Jun 13 11:42:43 CDT 2020


Moises Silva has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14520 )


Change subject: res_http_websocket: Add data masking to the websocket client
......................................................................

res_http_websocket: Add data masking to the websocket client

ASTERISK-28949

Change-Id: Id465030f2b1997b83d408933fdbabe01827469ca
---
M res/res_http_websocket.c
1 file changed, 29 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/20/14520/1

diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 63fccdd..2bf04d7 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -283,6 +283,9 @@
 	return 0;
 }
 
+/*! \brief Perform payload masking for client sessions */
+static void websocket_mask_payload(struct ast_websocket *session, char *frame, char *payload, uint64_t payload_size);
+
 /*! \brief Close function for websocket session */
 int AST_OPTIONAL_API_NAME(ast_websocket_close)(struct ast_websocket *session, uint16_t reason)
 {
@@ -300,6 +303,8 @@
 	/* If no reason has been specified assume 1000 which is normal closure */
 	put_unaligned_uint16(&frame[2], htons(reason ? reason : 1000));
 
+	websocket_mask_payload(session, frame, &frame[2], 2);
+
 	session->closing = 1;
 	session->close_sent = 1;
 
@@ -341,6 +346,23 @@
 	}
 }
 
+static void websocket_mask_payload(struct ast_websocket *session, char *frame, char *payload, uint64_t payload_size)
+{
+	/* RFC 6455 5.1 - clients MUST mask frame data */
+	if (session->client) {
+		uint64_t i;
+		uint8_t mask_key_idx;
+		uint32_t mask_key = ast_random();
+		uint8_t length = frame[1] & 0x7f;
+		frame[1] |= 0x80; /* set mask bit to 1 */
+		mask_key_idx = length == 126 ? 4 : length == 127 ? 10 : 2;
+		put_unaligned_uint32(&frame[mask_key_idx], mask_key);
+		for (i = 0; i < payload_size; i++) {
+			payload[i] ^= ((char *)&mask_key)[i % 4];
+		}
+	}
+}
+
 /*! \brief Write function for websocket traffic */
 int AST_OPTIONAL_API_NAME(ast_websocket_write)(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t payload_size)
 {
@@ -364,6 +386,11 @@
 		header_size += 8;
 	}
 
+	if (session->client) {
+		/* Additional 4 bytes for the client mask key */
+		header_size += 4;
+	}
+
 	frame_size = header_size + payload_size;
 
 	frame = ast_alloca(frame_size + 1);
@@ -381,6 +408,8 @@
 
 	memcpy(&frame[header_size], payload, payload_size);
 
+	websocket_mask_payload(session, frame, &frame[header_size], payload_size);
+
 	ao2_lock(session);
 	if (session->closing) {
 		ao2_unlock(session);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14520
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Id465030f2b1997b83d408933fdbabe01827469ca
Gerrit-Change-Number: 14520
Gerrit-PatchSet: 1
Gerrit-Owner: Moises Silva <moises.silva at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200613/86ce3c11/attachment.html>


More information about the asterisk-code-review mailing list