shared-update.cpp 7.8 KB

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