1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2011. All rights reserved.
* Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "WebSocketFrame.h"
#include <wtf/CryptographicallyRandomNumber.h>
#include <wtf/MathExtras.h>
#include <wtf/text/MakeString.h>
#include <wtf/text/ParsingUtilities.h>
namespace WebCore {
// Constants for hybi-10 frame format.
constexpr uint8_t finalBit = 0x80;
constexpr uint8_t compressBit = 0x40;
constexpr uint8_t reserved2Bit = 0x20;
constexpr uint8_t reserved3Bit = 0x10;
constexpr uint8_t opCodeMask = 0xF;
constexpr uint8_t maskBit = 0x80;
constexpr uint8_t payloadLengthMask = 0x7F;
constexpr size_t maxPayloadLengthWithoutExtendedLengthField = 125;
constexpr size_t payloadLengthWithTwoByteExtendedLengthField = 126;
constexpr size_t payloadLengthWithEightByteExtendedLengthField = 127;
constexpr size_t maskingKeyWidthInBytes = 4;
bool WebSocketFrame::needsExtendedLengthField(size_t payloadLength)
{
return payloadLength > maxPayloadLengthWithoutExtendedLengthField;
}
WebSocketFrame::ParseFrameResult WebSocketFrame::parseFrame(std::span<uint8_t> data, WebSocketFrame& frame, const uint8_t*& frameEnd, String& errorString)
{
if (data.size() < 2)
return FrameIncomplete;
auto firstByte = data[0];
auto secondByte = data[1];
skip(data, 2);
bool final = firstByte & finalBit;
bool compress = firstByte & compressBit;
bool reserved2 = firstByte & reserved2Bit;
bool reserved3 = firstByte & reserved3Bit;
auto opCode = firstByte & opCodeMask;
bool masked = secondByte & maskBit;
uint64_t payloadLength64 = secondByte & payloadLengthMask;
if (payloadLength64 > maxPayloadLengthWithoutExtendedLengthField) {
size_t extendedPayloadLengthSize;
if (payloadLength64 == payloadLengthWithTwoByteExtendedLengthField)
extendedPayloadLengthSize = 2;
else {
ASSERT(payloadLength64 == payloadLengthWithEightByteExtendedLengthField);
extendedPayloadLengthSize = 8;
}
if (data.size() < extendedPayloadLengthSize)
return FrameIncomplete;
payloadLength64 = 0;
for (size_t i = 0; i < extendedPayloadLengthSize; ++i) {
payloadLength64 <<= 8;
payloadLength64 |= data[i];
}
skip(data, extendedPayloadLengthSize);
if (extendedPayloadLengthSize == 2 && payloadLength64 <= maxPayloadLengthWithoutExtendedLengthField) {
errorString = "The minimal number of bytes MUST be used to encode the length"_s;
return FrameError;
}
if (extendedPayloadLengthSize == 8 && payloadLength64 <= 0xFFFF) {
errorString = "The minimal number of bytes MUST be used to encode the length"_s;
return FrameError;
}
}
constexpr uint64_t maxPayloadLength = UINT64_C(0x7FFFFFFFFFFFFFFF);
size_t maskingKeyLength = masked ? maskingKeyWidthInBytes : 0;
if (payloadLength64 > maxPayloadLength || payloadLength64 + maskingKeyLength > std::numeric_limits<size_t>::max()) {
errorString = makeString("WebSocket frame length too large: "_s, payloadLength64, " bytes"_s);
return FrameError;
}
size_t payloadLength = static_cast<size_t>(payloadLength64);
if (data.size() < maskingKeyLength + payloadLength)
return FrameIncomplete;
if (masked) {
auto maskingKey = data;
auto payload = data.subspan(maskingKeyWidthInBytes);
for (size_t i = 0; i < payloadLength; ++i)
payload[i] ^= maskingKey[i % maskingKeyWidthInBytes]; // Unmask the payload.
}
frame.opCode = static_cast<WebSocketFrame::OpCode>(opCode);
frame.final = final;
frame.compress = compress;
frame.reserved2 = reserved2;
frame.reserved3 = reserved3;
frame.masked = masked;
frame.payload = data.subspan(maskingKeyLength, payloadLength);
frameEnd = data.subspan(maskingKeyLength + payloadLength).data();
return FrameOK;
}
static void appendFramePayload(const WebSocketFrame& frame, Vector<uint8_t>& frameData)
{
size_t maskingKeyStart = 0;
if (frame.masked) {
maskingKeyStart = frameData.size();
frameData.grow(frameData.size() + maskingKeyWidthInBytes); // Add placeholder for masking key. Will be overwritten.
}
size_t payloadStart = frameData.size();
frameData.append(frame.payload);
if (frame.masked) {
cryptographicallyRandomValues(frameData.mutableSpan().subspan(maskingKeyStart, maskingKeyWidthInBytes));
for (size_t i = 0; i < frame.payload.size(); ++i)
frameData[payloadStart + i] ^= frameData[maskingKeyStart + i % maskingKeyWidthInBytes];
}
}
void WebSocketFrame::makeFrameData(Vector<uint8_t>& frameData)
{
ASSERT(!(opCode & ~opCodeMask)); // Checks whether "opCode" fits in the range of opCodes.
frameData.resize(2);
frameData.at(0) = (final ? finalBit : 0) | (compress ? compressBit : 0) | opCode;
frameData.at(1) = masked ? maskBit : 0;
if (payload.size() <= maxPayloadLengthWithoutExtendedLengthField)
frameData.at(1) |= payload.size();
else if (payload.size() <= 0xFFFF) {
frameData.at(1) |= payloadLengthWithTwoByteExtendedLengthField;
frameData.append((payload.size() & 0xFF00) >> 8);
frameData.append(payload.size() & 0xFF);
} else {
frameData.at(1) |= payloadLengthWithEightByteExtendedLengthField;
std::array<uint8_t, 8> extendedPayloadLength;
size_t remaining = payload.size();
// Fill the length into extendedPayloadLength in the network byte order.
for (int i = 0; i < 8; ++i) {
extendedPayloadLength[7 - i] = remaining & 0xFF;
remaining >>= 8;
}
ASSERT(!remaining);
frameData.append(std::span { extendedPayloadLength });
}
appendFramePayload(*this, frameData);
}
WebSocketFrame::WebSocketFrame(OpCode opCode, bool final, bool compress, bool masked, std::span<const uint8_t> payload)
: opCode(opCode)
, final(final)
, compress(compress)
, reserved2(false)
, reserved3(false)
, masked(masked)
, payload(payload)
{
}
} // namespace WebCore
|