Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket: add masking bytes also while enabling MASKING_FRAME flag #1426

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Source/websocket/WebSocketLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,8 @@ namespace Web {
::memmove(&dataFrame[2], &(dataFrame[4]), usedSize);
}
} else {
uint32_t value;
// Generate a new mask value
uint8_t maskKey[4];
Crypto::Random(value);
maskKey[0] = value & 0xFF;
maskKey[1] = (value >> 8) & 0xFF;
maskKey[2] = (value >> 16) & 0xFF;
maskKey[3] = (value >> 24) & 0xFF;
GenerateMaskKey(maskKey);

// Mask and insert the bytes on the right spots
uint8_t* source = &dataFrame[usedSize - 1 + 4];
Expand Down Expand Up @@ -138,7 +132,7 @@ namespace Web {
result += usedSize;
}

if (((_controlStatus & (REQUEST_CLOSE | REQUEST_PING | REQUEST_PONG)) != 0) && ((result + 1) < maxSendSize)) {
if (((_controlStatus & (REQUEST_CLOSE | REQUEST_PING | REQUEST_PONG)) != 0) && (result + (((_setFlags & MASKING_FRAME) != 0) ? 6 : 2) < maxSendSize)) {
if ((_controlStatus & REQUEST_CLOSE) != 0) {
dataFrame[result++] = FINISHING_FRAME | Protocol::CLOSE;
_controlStatus &= (~REQUEST_CLOSE);
Expand All @@ -154,6 +148,14 @@ namespace Web {
_controlStatus &= (~REQUEST_PONG);
dataFrame[result++] = (_setFlags & MASKING_FRAME);
}

if ((_setFlags & MASKING_FRAME) != 0) {
// Now it seems only control message, hence append with masking keys
uint8_t maskKey[4];
GenerateMaskKey(maskKey);
::memcpy(&dataFrame[result], &maskKey, 4);
result += 4;
}
}

return (result);
Expand Down
12 changes: 12 additions & 0 deletions Source/websocket/WebSocketLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ namespace Web {
uint16_t Encoder(uint8_t* dataFrame, const uint16_t maxSendSize, const uint16_t usedSize);
uint16_t Decoder(uint8_t* dataFrame, uint16_t& receivedSize);

private:
inline void GenerateMaskKey(uint8_t *maskKey)
{
uint32_t value;
// Generate a new mask value
Crypto::Random(value);
maskKey[0] = value & 0xFF;
maskKey[1] = (value >> 8) & 0xFF;
maskKey[2] = (value >> 16) & 0xFF;
maskKey[3] = (value >> 24) & 0xFF;
}

private:
uint8_t _setFlags;
uint8_t _progressInfo;
Expand Down