Skip to content

Commit 887bcdc

Browse files
committed
Windows: Fix WebSocket secure shared messages
The encryption of WebSocket secure would be applied to the original message. This was bad news if it was a message that was going to be forwarded to clients after them in the channel's client list, as it meant corrupted messages. This reflects SortaCore/MMF2Exts@982ec38.
1 parent 33864cc commit 887bcdc

File tree

1 file changed

+19
-1
lines changed
  • Lacewing/src/windows/ssl

1 file changed

+19
-1
lines changed

Lacewing/src/windows/ssl/ssl.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,29 @@ static size_t def_upstream_sink_data (lw_stream upstream,
2424
if (!ctx->handshake_complete)
2525
return 0; /* can't send anything right now */
2626

27+
// We cannot encrypt in-place, as the buffer is const, and anything reading back from it will get indecipherable data
28+
// In Blue, this caused a bug when a secure WebSocket client was in peer list, a peer join/leave would be sent to all
29+
// before WS client fine, but the encryption would corrupt the message for clients after.
30+
BYTE* copy = _malloca(size);
31+
if (!copy)
32+
{
33+
lw_error err = lw_error_new();
34+
lw_error_addf(err, "Out of memory, couldn't alloc %zu bytes", size);
35+
lw_error_addf(err, "Encrypting message failed");
36+
if (ctx->handle_error && ctx->client)
37+
ctx->handle_error(ctx->client, err);
38+
lw_error_delete(err);
39+
return size;
40+
}
41+
memcpy(copy, buffer, size);
42+
2743
SecBuffer buffers [4];
2844

2945
buffers [0].pvBuffer = ctx->header;
3046
buffers [0].cbBuffer = ctx->sizes.cbHeader;
3147
buffers [0].BufferType = SECBUFFER_STREAM_HEADER;
3248

33-
buffers [1].pvBuffer = (BYTE *) buffer;
49+
buffers [1].pvBuffer = copy;
3450
buffers [1].cbBuffer = (unsigned long)size;
3551
buffers [1].BufferType = SECBUFFER_DATA;
3652

@@ -51,6 +67,7 @@ static size_t def_upstream_sink_data (lw_stream upstream,
5167

5268
if (status != SEC_E_OK)
5369
{
70+
_freea(copy);
5471
lw_error err = lw_error_new();
5572
lw_error_add(err, status);
5673
lw_error_addf(err, "Encrypting message failed");
@@ -66,6 +83,7 @@ static size_t def_upstream_sink_data (lw_stream upstream,
6683
lw_stream_data (upstream, (char *) buffers [2].pvBuffer, buffers [2].cbBuffer);
6784
lw_stream_data (upstream, (char *) buffers [3].pvBuffer, buffers [3].cbBuffer);
6885

86+
_freea(copy);
6987
return size;
7088
}
7189

0 commit comments

Comments
 (0)