1
0

WhatsNewInfoThread.cpp 7.3 KB

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