WhatsNewBrowserInitThread.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "moc_shared-update.cpp"
  2. #include "crypto-helpers.hpp"
  3. #include "update-helpers.hpp"
  4. #include "obs-app.hpp"
  5. #include "remote-text.hpp"
  6. #include "platform.hpp"
  7. #include <util/util.hpp>
  8. #include <blake2.h>
  9. #include <iostream>
  10. #include <fstream>
  11. #include <filesystem>
  12. #include <QRandomGenerator>
  13. #include <QByteArray>
  14. #include <QString>
  15. #ifdef BROWSER_AVAILABLE
  16. #include <browser-panel.hpp>
  17. struct QCef;
  18. extern QCef *cef;
  19. #endif
  20. #ifndef MAC_WHATSNEW_URL
  21. #define MAC_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
  22. #endif
  23. #ifndef WIN_WHATSNEW_URL
  24. #define WIN_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
  25. #endif
  26. #ifndef LINUX_WHATSNEW_URL
  27. #define LINUX_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
  28. #endif
  29. #ifdef __APPLE__
  30. #define WHATSNEW_URL MAC_WHATSNEW_URL
  31. #elif defined(_WIN32)
  32. #define WHATSNEW_URL WIN_WHATSNEW_URL
  33. #else
  34. #define WHATSNEW_URL LINUX_WHATSNEW_URL
  35. #endif
  36. #define HASH_READ_BUF_SIZE 65536
  37. #define BLAKE2_HASH_LENGTH 20
  38. /* ------------------------------------------------------------------------ */
  39. static bool QuickWriteFile(const char *file, const std::string &data)
  40. try {
  41. std::ofstream fileStream(std::filesystem::u8path(file), std::ios::binary);
  42. if (fileStream.fail())
  43. throw strprintf("Failed to open file '%s': %s", file, strerror(errno));
  44. fileStream.write(data.data(), data.size());
  45. if (fileStream.fail())
  46. throw strprintf("Failed to write file '%s': %s", file, strerror(errno));
  47. return true;
  48. } catch (std::string &text) {
  49. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  50. return false;
  51. }
  52. static bool QuickReadFile(const char *file, std::string &data)
  53. try {
  54. std::ifstream fileStream(std::filesystem::u8path(file), std::ios::binary);
  55. if (!fileStream.is_open() || fileStream.fail())
  56. throw strprintf("Failed to open file '%s': %s", file, strerror(errno));
  57. fileStream.seekg(0, fileStream.end);
  58. size_t size = fileStream.tellg();
  59. fileStream.seekg(0);
  60. data.resize(size);
  61. fileStream.read(&data[0], size);
  62. if (fileStream.fail())
  63. throw strprintf("Failed to write file '%s': %s", file, strerror(errno));
  64. return true;
  65. } catch (std::string &text) {
  66. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  67. return false;
  68. }
  69. static bool CalculateFileHash(const char *path, uint8_t *hash)
  70. try {
  71. blake2b_state blake2;
  72. if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
  73. return false;
  74. std::ifstream file(std::filesystem::u8path(path), std::ios::binary);
  75. if (!file.is_open() || file.fail())
  76. return false;
  77. char buf[HASH_READ_BUF_SIZE];
  78. for (;;) {
  79. file.read(buf, HASH_READ_BUF_SIZE);
  80. size_t read = file.gcount();
  81. if (blake2b_update(&blake2, &buf, read) != 0)
  82. return false;
  83. if (file.eof())
  84. break;
  85. }
  86. if (blake2b_final(&blake2, hash, BLAKE2_HASH_LENGTH) != 0)
  87. return false;
  88. return true;
  89. } catch (std::string &text) {
  90. blog(LOG_DEBUG, "%s: %s", __FUNCTION__, text.c_str());
  91. return false;
  92. }
  93. /* ------------------------------------------------------------------------ */
  94. void GenerateGUID(std::string &guid)
  95. {
  96. const char alphabet[] = "0123456789abcdef";
  97. QRandomGenerator *rng = QRandomGenerator::system();
  98. guid.resize(40);
  99. for (size_t i = 0; i < 40; i++) {
  100. guid[i] = alphabet[rng->bounded(0, 16)];
  101. }
  102. }
  103. std::string GetProgramGUID()
  104. {
  105. static std::mutex m;
  106. std::lock_guard<std::mutex> lock(m);
  107. /* NOTE: this is an arbitrary random number that we use to count the
  108. * number of unique OBS installations and is not associated with any
  109. * kind of identifiable information */
  110. const char *pguid = config_get_string(App()->GetAppConfig(), "General", "InstallGUID");
  111. std::string guid;
  112. if (pguid)
  113. guid = pguid;
  114. if (guid.empty()) {
  115. GenerateGUID(guid);
  116. if (!guid.empty())
  117. config_set_string(App()->GetAppConfig(), "General", "InstallGUID", guid.c_str());
  118. }
  119. return guid;
  120. }
  121. /* ------------------------------------------------------------------------ */
  122. static void LoadPublicKey(std::string &pubkey)
  123. {
  124. std::string pemFilePath;
  125. if (!GetDataFilePath("OBSPublicRSAKey.pem", pemFilePath))
  126. throw std::string("Could not find OBS public key file!");
  127. if (!QuickReadFile(pemFilePath.c_str(), pubkey))
  128. throw std::string("Could not read OBS public key file!");
  129. }
  130. static bool CheckDataSignature(const char *name, const std::string &data, const std::string &hexSig)
  131. try {
  132. static std::mutex pubkey_mutex;
  133. static std::string obsPubKey;
  134. if (hexSig.empty() || hexSig.length() > 0xFFFF || (hexSig.length() & 1) != 0)
  135. throw strprintf("Missing or invalid signature for %s: %s", name, hexSig.c_str());
  136. std::scoped_lock lock(pubkey_mutex);
  137. if (obsPubKey.empty())
  138. LoadPublicKey(obsPubKey);
  139. // Convert hex string to bytes
  140. auto signature = QByteArray::fromHex(hexSig.data());
  141. if (!VerifySignature((uint8_t *)obsPubKey.data(), obsPubKey.size(), (uint8_t *)data.data(), data.size(),
  142. (uint8_t *)signature.data(), signature.size()))
  143. throw strprintf("Signature check failed for %s", name);
  144. return true;
  145. } catch (std::string &text) {
  146. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  147. return false;
  148. }
  149. /* ------------------------------------------------------------------------ */
  150. bool FetchAndVerifyFile(const char *name, const char *file, const char *url, std::string *out,
  151. const std::vector<std::string> &extraHeaders)
  152. {
  153. long responseCode;
  154. std::vector<std::string> headers;
  155. std::string error;
  156. std::string signature;
  157. std::string data;
  158. uint8_t fileHash[BLAKE2_HASH_LENGTH];
  159. bool success;
  160. BPtr<char> filePath = GetAppConfigPathPtr(file);
  161. if (!extraHeaders.empty()) {
  162. headers.insert(headers.end(), extraHeaders.begin(), extraHeaders.end());
  163. }
  164. /* ----------------------------------- *
  165. * avoid downloading file again */
  166. if (CalculateFileHash(filePath, fileHash)) {
  167. auto hash = QByteArray::fromRawData((const char *)fileHash, BLAKE2_HASH_LENGTH);
  168. QString header = "If-None-Match: " + hash.toHex();
  169. headers.push_back(header.toStdString());
  170. }
  171. /* ----------------------------------- *
  172. * get current install GUID */
  173. std::string guid = GetProgramGUID();
  174. if (!guid.empty()) {
  175. std::string header = "X-OBS2-GUID: " + guid;
  176. headers.push_back(std::move(header));
  177. }
  178. /* ----------------------------------- *
  179. * get file from server */
  180. success = GetRemoteFile(url, data, error, &responseCode, nullptr, "", nullptr, headers, &signature);
  181. if (!success || (responseCode != 200 && responseCode != 304)) {
  182. if (responseCode == 404)
  183. return false;
  184. throw strprintf("Failed to fetch %s file: %s", name, error.c_str());
  185. }
  186. /* ----------------------------------- *
  187. * verify file signature */
  188. if (responseCode == 200) {
  189. success = CheckDataSignature(name, data, signature);
  190. if (!success)
  191. throw strprintf("Invalid %s signature", name);
  192. }
  193. /* ----------------------------------- *
  194. * write or load file */
  195. if (responseCode == 200) {
  196. if (!QuickWriteFile(filePath, data))
  197. throw strprintf("Could not write file '%s'", filePath.Get());
  198. } else if (out) { /* Only read file if caller wants data */
  199. if (!QuickReadFile(filePath, data))
  200. throw strprintf("Could not read file '%s'", filePath.Get());
  201. }
  202. if (out)
  203. *out = data;
  204. /* ----------------------------------- *
  205. * success */
  206. return true;
  207. }
  208. void WhatsNewInfoThread::run()
  209. try {
  210. std::string text;
  211. if (FetchAndVerifyFile("whatsnew", "obs-studio/updates/whatsnew.json", WHATSNEW_URL, &text)) {
  212. emit Result(QString::fromStdString(text));
  213. }
  214. } catch (std::string &text) {
  215. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  216. }
  217. /* ------------------------------------------------------------------------ */
  218. void WhatsNewBrowserInitThread::run()
  219. {
  220. #ifdef BROWSER_AVAILABLE
  221. cef->wait_for_browser_init();
  222. #endif
  223. emit Result(url);
  224. }