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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include "shared-update.hpp"
#include "crypto-helpers.hpp"
#include "update-helpers.hpp"
#include "obs-app.hpp"
#include "remote-text.hpp"
#include "platform.hpp"
#include <util/util.hpp>
#include <blake2.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <QRandomGenerator>
#include <QByteArray>
#include <QString>
#ifdef BROWSER_AVAILABLE
#include <browser-panel.hpp>
struct QCef;
extern QCef *cef;
#endif
#ifndef MAC_WHATSNEW_URL
#define MAC_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifndef WIN_WHATSNEW_URL
#define WIN_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifndef LINUX_WHATSNEW_URL
#define LINUX_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
#endif
#ifdef __APPLE__
#define WHATSNEW_URL MAC_WHATSNEW_URL
#elif defined(_WIN32)
#define WHATSNEW_URL WIN_WHATSNEW_URL
#else
#define WHATSNEW_URL LINUX_WHATSNEW_URL
#endif
#define HASH_READ_BUF_SIZE 65536
#define BLAKE2_HASH_LENGTH 20
/* ------------------------------------------------------------------------ */
static bool QuickWriteFile(const char *file, const std::string &data)
try {
std::ofstream fileStream(std::filesystem::u8path(file),
std::ios::binary);
if (fileStream.fail())
throw strprintf("Failed to open file '%s': %s", file,
strerror(errno));
fileStream.write(data.data(), data.size());
if (fileStream.fail())
throw strprintf("Failed to write file '%s': %s", file,
strerror(errno));
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
static bool QuickReadFile(const char *file, std::string &data)
try {
std::ifstream fileStream(std::filesystem::u8path(file),
std::ios::binary);
if (!fileStream.is_open() || fileStream.fail())
throw strprintf("Failed to open file '%s': %s", file,
strerror(errno));
fileStream.seekg(0, fileStream.end);
size_t size = fileStream.tellg();
fileStream.seekg(0);
data.resize(size);
fileStream.read(&data[0], size);
if (fileStream.fail())
throw strprintf("Failed to write file '%s': %s", file,
strerror(errno));
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
static bool CalculateFileHash(const char *path, uint8_t *hash)
try {
blake2b_state blake2;
if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
return false;
std::ifstream file(std::filesystem::u8path(path), std::ios::binary);
if (!file.is_open() || file.fail())
return false;
char buf[HASH_READ_BUF_SIZE];
for (;;) {
file.read(buf, HASH_READ_BUF_SIZE);
size_t read = file.gcount();
if (blake2b_update(&blake2, &buf, read) != 0)
return false;
if (file.eof())
break;
}
if (blake2b_final(&blake2, hash, BLAKE2_HASH_LENGTH) != 0)
return false;
return true;
} catch (std::string &text) {
blog(LOG_DEBUG, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
/* ------------------------------------------------------------------------ */
void GenerateGUID(std::string &guid)
{
const char alphabet[] = "0123456789abcdef";
QRandomGenerator *rng = QRandomGenerator::system();
guid.resize(40);
for (size_t i = 0; i < 40; i++) {
guid[i] = alphabet[rng->bounded(0, 16)];
}
}
std::string GetProgramGUID()
{
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
/* NOTE: this is an arbitrary random number that we use to count the
* number of unique OBS installations and is not associated with any
* kind of identifiable information */
const char *pguid =
config_get_string(GetGlobalConfig(), "General", "InstallGUID");
std::string guid;
if (pguid)
guid = pguid;
if (guid.empty()) {
GenerateGUID(guid);
if (!guid.empty())
config_set_string(GetGlobalConfig(), "General",
"InstallGUID", guid.c_str());
}
return guid;
}
/* ------------------------------------------------------------------------ */
static void LoadPublicKey(std::string &pubkey)
{
std::string pemFilePath;
if (!GetDataFilePath("OBSPublicRSAKey.pem", pemFilePath))
throw std::string("Could not find OBS public key file!");
if (!QuickReadFile(pemFilePath.c_str(), pubkey))
throw std::string("Could not read OBS public key file!");
}
static bool CheckDataSignature(const char *name, const std::string &data,
const std::string &hexSig)
try {
static std::mutex pubkey_mutex;
static std::string obsPubKey;
if (hexSig.empty() || hexSig.length() > 0xFFFF ||
(hexSig.length() & 1) != 0)
throw strprintf("Missing or invalid signature for %s: %s", name,
hexSig.c_str());
std::scoped_lock lock(pubkey_mutex);
if (obsPubKey.empty())
LoadPublicKey(obsPubKey);
// Convert hex string to bytes
auto signature = QByteArray::fromHex(hexSig.data());
if (!VerifySignature((uint8_t *)obsPubKey.data(), obsPubKey.size(),
(uint8_t *)data.data(), data.size(),
(uint8_t *)signature.data(), signature.size()))
throw strprintf("Signature check failed for %s", name);
return true;
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
return false;
}
/* ------------------------------------------------------------------------ */
bool FetchAndVerifyFile(const char *name, const char *file, const char *url,
std::string *out,
const std::vector<std::string> &extraHeaders)
{
long responseCode;
std::vector<std::string> headers;
std::string error;
std::string signature;
std::string data;
uint8_t fileHash[BLAKE2_HASH_LENGTH];
bool success;
BPtr<char> filePath = GetConfigPathPtr(file);
if (!extraHeaders.empty()) {
headers.insert(headers.end(), extraHeaders.begin(),
extraHeaders.end());
}
/* ----------------------------------- *
* avoid downloading file again */
if (CalculateFileHash(filePath, fileHash)) {
auto hash = QByteArray::fromRawData((const char *)fileHash,
BLAKE2_HASH_LENGTH);
QString header = "If-None-Match: " + hash.toHex();
headers.push_back(header.toStdString());
}
/* ----------------------------------- *
* get current install GUID */
std::string guid = GetProgramGUID();
if (!guid.empty()) {
std::string header = "X-OBS2-GUID: " + guid;
headers.push_back(std::move(header));
}
/* ----------------------------------- *
* get file from server */
success = GetRemoteFile(url, data, error, &responseCode, nullptr, "",
nullptr, headers, &signature);
if (!success || (responseCode != 200 && responseCode != 304)) {
if (responseCode == 404)
return false;
throw strprintf("Failed to fetch %s file: %s", name,
error.c_str());
}
/* ----------------------------------- *
* verify file signature */
if (responseCode == 200) {
success = CheckDataSignature(name, data, signature);
if (!success)
throw strprintf("Invalid %s signature", name);
}
/* ----------------------------------- *
* write or load file */
if (responseCode == 200) {
if (!QuickWriteFile(filePath, data))
throw strprintf("Could not write file '%s'",
filePath.Get());
} else if (out) { /* Only read file if caller wants data */
if (!QuickReadFile(filePath, data))
throw strprintf("Could not read file '%s'",
filePath.Get());
}
if (out)
*out = data;
/* ----------------------------------- *
* success */
return true;
}
void WhatsNewInfoThread::run()
try {
std::string text;
if (FetchAndVerifyFile("whatsnew", "obs-studio/updates/whatsnew.json",
WHATSNEW_URL, &text)) {
emit Result(QString::fromStdString(text));
}
} catch (std::string &text) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
}
/* ------------------------------------------------------------------------ */
void WhatsNewBrowserInitThread::run()
{
#ifdef BROWSER_AVAILABLE
cef->wait_for_browser_init();
#endif
emit Result(url);
}
|