shared-update.cpp 7.6 KB

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