#include "google/live/ws_handshake.h" #include #include #include #include #include #include #include "google/live/ws_error.h" #include "security/crypto/Base64.h" namespace molo::google::live { using K = WsError::Kind; using molo::security::crypto::Base64; namespace { std::array sha1(const std::string& msg) { std::uint32_t h[5] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0}; std::string data = msg; data.push_back((char)0x80); while (data.size() % 64 != 56) data.push_back(0); std::uint64_t bits = (std::uint64_t)msg.size() * 8; for (int i = 7; i >= 0; --i) data.push_back((char)((bits >> (i * 8)) & 0xFF)); auto rol = [](std::uint32_t v, int s) { return (v << s) | (v >> (32 - s)); }; for (std::size_t off = 0; off < data.size(); off += 64) { std::uint32_t w[80]; for (int i = 0; i < 16; ++i) w[i] = ((std::uint32_t)(std::uint8_t)data[off + i * 4] << 24) | ((std::uint32_t)(std::uint8_t)data[off + i * 4 + 1] << 16) | ((std::uint32_t)(std::uint8_t)data[off + i * 4 + 2] << 8) | (std::uint32_t)(std::uint8_t)data[off + i * 4 + 3]; for (int i = 16; i < 80; ++i) w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); std::uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4]; for (int i = 0; i < 80; ++i) { std::uint32_t f, k; if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; } else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; } else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } else { f = b ^ c ^ d; k = 0xCA62C1D6; } std::uint32_t t = rol(a, 5) + f + e + k + w[i]; e = d; d = c; c = rol(b, 30); b = a; a = t; } h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; } std::array out{}; for (int i = 0; i < 5; ++i) for (int j = 0; j < 4; ++j) out[i * 4 + j] = (std::uint8_t)(h[i] >> (24 - j * 8)); return out; } std::string lower(std::string s) { for (auto& c : s) c = (char)std::tolower((unsigned char)c); return s; } std::string trim(const std::string& s) { std::size_t a = s.find_first_not_of(" \t"); if (a == std::string::npos) return ""; std::size_t b = s.find_last_not_of(" \t\r"); return s.substr(a, b - a + 1); } } // namespace std::string WsHandshake::make_key() { std::uint8_t raw[16]; std::size_t got = 0; while (got < sizeof raw) { ssize_t n = ::getrandom(raw + got, sizeof raw - got, 0); if (n <= 0) throw WsError(K::Io, "no entropy for handshake key"); got += (std::size_t)n; } return Base64::encode(raw, sizeof raw); } std::string WsHandshake::accept_for(const std::string& key) { auto d = sha1(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); return Base64::encode(d.data(), d.size()); } std::string WsHandshake::build_request(const WsUrl& url, const std::string& key, const WsHeaders& headers) { std::string host = url.host; if (url.port != (url.secure ? 443 : 80)) host += ":" + std::to_string(url.port); std::string out = "GET " + url.target + " HTTP/1.1\r\nHost: " + host + "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" "Sec-WebSocket-Key: " + key + "\r\nSec-WebSocket-Version: 13\r\n"; for (const auto& [name, value] : headers) { if (name.empty() || name.find_first_of("\r\n:") != std::string::npos || value.find_first_of("\r\n") != std::string::npos) throw WsError(K::Invalid, "header with forbidden characters"); out += name + ": " + value + "\r\n"; } return out + "\r\n"; } void WsHandshake::check_response(const std::string& head, const std::string& key) { std::size_t eol = head.find("\r\n"); if (eol == std::string::npos) throw WsError(K::Handshake, "malformed response"); std::string status_line = head.substr(0, eol); int status = 0; if (status_line.rfind("HTTP/1.1 ", 0) != 0 || status_line.size() < 12) throw WsError(K::Handshake, "malformed status line"); for (int i = 9; i < 12; ++i) { if (!std::isdigit((unsigned char)status_line[i])) throw WsError(K::Handshake, "malformed status line"); status = status * 10 + (status_line[i] - '0'); } if (status != 101) throw WsError(K::Handshake, "upgrade refused with HTTP " + std::to_string(status), status); std::string upgrade, connection, accept; std::size_t pos = eol + 2; while (pos < head.size()) { std::size_t e = head.find("\r\n", pos); if (e == std::string::npos || e == pos) break; std::string line = head.substr(pos, e - pos); pos = e + 2; std::size_t c = line.find(':'); if (c == std::string::npos) continue; std::string name = lower(trim(line.substr(0, c))); std::string value = trim(line.substr(c + 1)); if (name == "upgrade") upgrade = lower(value); else if (name == "connection") connection = lower(value); else if (name == "sec-websocket-accept") accept = value; } if (upgrade != "websocket") throw WsError(K::Handshake, "missing Upgrade: websocket", 101); if (connection.find("upgrade") == std::string::npos) throw WsError(K::Handshake, "missing Connection: Upgrade", 101); if (accept != accept_for(key)) throw WsError(K::Handshake, "Sec-WebSocket-Accept mismatch", 101); } } // namespace molo::google::live