win-update.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. #include "win-update-helpers.hpp"
  2. #include "update-window.hpp"
  3. #include "remote-text.hpp"
  4. #include "qt-wrappers.hpp"
  5. #include "win-update.hpp"
  6. #include "obs-app.hpp"
  7. #include <QMessageBox>
  8. #include <string>
  9. #include <mutex>
  10. #include <util/windows/WinHandle.hpp>
  11. #include <util/util.hpp>
  12. #include <json11.hpp>
  13. #include <blake2.h>
  14. #include <time.h>
  15. #include <strsafe.h>
  16. #include <winhttp.h>
  17. #include <shellapi.h>
  18. #ifdef BROWSER_AVAILABLE
  19. #include <browser-panel.hpp>
  20. #endif
  21. using namespace std;
  22. using namespace json11;
  23. struct QCef;
  24. extern QCef *cef;
  25. /* ------------------------------------------------------------------------ */
  26. #ifndef WIN_MANIFEST_URL
  27. #define WIN_MANIFEST_URL "https://obsproject.com/update_studio/manifest.json"
  28. #endif
  29. #ifndef WIN_WHATSNEW_URL
  30. #define WIN_WHATSNEW_URL "https://obsproject.com/update_studio/whatsnew.json"
  31. #endif
  32. #ifndef WIN_UPDATER_URL
  33. #define WIN_UPDATER_URL "https://obsproject.com/update_studio/updater.exe"
  34. #endif
  35. static __declspec(thread) HCRYPTPROV provider = 0;
  36. #pragma pack(push, r1, 1)
  37. typedef struct {
  38. BLOBHEADER blobheader;
  39. RSAPUBKEY rsapubkey;
  40. } PUBLICKEYHEADER;
  41. #pragma pack(pop, r1)
  42. #define BLAKE2_HASH_LENGTH 20
  43. #define BLAKE2_HASH_STR_LENGTH ((BLAKE2_HASH_LENGTH * 2) + 1)
  44. #define TEST_BUILD
  45. // Hard coded 4096 bit RSA public key for obsproject.com in PEM format
  46. static const unsigned char obs_pub[] = {
  47. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x50,
  48. 0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d,
  49. 0x2d, 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x43, 0x49, 0x6a, 0x41, 0x4e, 0x42,
  50. 0x67, 0x6b, 0x71, 0x68, 0x6b, 0x69, 0x47, 0x39, 0x77, 0x30, 0x42, 0x41,
  51. 0x51, 0x45, 0x46, 0x41, 0x41, 0x4f, 0x43, 0x41, 0x67, 0x38, 0x41, 0x4d,
  52. 0x49, 0x49, 0x43, 0x43, 0x67, 0x4b, 0x43, 0x41, 0x67, 0x45, 0x41, 0x6c,
  53. 0x33, 0x73, 0x76, 0x65, 0x72, 0x77, 0x39, 0x48, 0x51, 0x2b, 0x72, 0x59,
  54. 0x51, 0x4e, 0x6e, 0x39, 0x43, 0x61, 0x37, 0x0a, 0x39, 0x4c, 0x55, 0x36,
  55. 0x32, 0x6e, 0x47, 0x36, 0x4e, 0x6f, 0x7a, 0x45, 0x2f, 0x46, 0x73, 0x49,
  56. 0x56, 0x4e, 0x65, 0x72, 0x2b, 0x57, 0x2f, 0x68, 0x75, 0x65, 0x45, 0x38,
  57. 0x57, 0x51, 0x31, 0x6d, 0x72, 0x46, 0x50, 0x2b, 0x32, 0x79, 0x41, 0x2b,
  58. 0x69, 0x59, 0x52, 0x75, 0x74, 0x59, 0x50, 0x65, 0x45, 0x67, 0x70, 0x78,
  59. 0x74, 0x6f, 0x64, 0x48, 0x68, 0x67, 0x6b, 0x52, 0x34, 0x70, 0x45, 0x4b,
  60. 0x0a, 0x56, 0x6e, 0x72, 0x72, 0x31, 0x38, 0x71, 0x34, 0x73, 0x7a, 0x6c,
  61. 0x76, 0x38, 0x39, 0x51, 0x49, 0x37, 0x74, 0x38, 0x6c, 0x4d, 0x6f, 0x4c,
  62. 0x54, 0x6c, 0x46, 0x2b, 0x74, 0x31, 0x49, 0x52, 0x30, 0x56, 0x34, 0x77,
  63. 0x4a, 0x56, 0x33, 0x34, 0x49, 0x33, 0x43, 0x2b, 0x33, 0x35, 0x39, 0x4b,
  64. 0x69, 0x78, 0x6e, 0x7a, 0x4c, 0x30, 0x42, 0x6c, 0x39, 0x61, 0x6a, 0x2f,
  65. 0x7a, 0x44, 0x63, 0x72, 0x58, 0x0a, 0x57, 0x6c, 0x35, 0x70, 0x48, 0x54,
  66. 0x69, 0x6f, 0x4a, 0x77, 0x59, 0x4f, 0x67, 0x4d, 0x69, 0x42, 0x47, 0x4c,
  67. 0x79, 0x50, 0x65, 0x69, 0x74, 0x4d, 0x46, 0x64, 0x6a, 0x6a, 0x54, 0x49,
  68. 0x70, 0x43, 0x4d, 0x2b, 0x6d, 0x78, 0x54, 0x57, 0x58, 0x43, 0x72, 0x5a,
  69. 0x39, 0x64, 0x50, 0x55, 0x4b, 0x76, 0x5a, 0x74, 0x67, 0x7a, 0x6a, 0x64,
  70. 0x2b, 0x49, 0x7a, 0x6c, 0x48, 0x69, 0x64, 0x48, 0x74, 0x4f, 0x0a, 0x4f,
  71. 0x52, 0x42, 0x4e, 0x35, 0x6d, 0x52, 0x73, 0x38, 0x4c, 0x4e, 0x4f, 0x35,
  72. 0x38, 0x6b, 0x37, 0x39, 0x72, 0x37, 0x37, 0x44, 0x63, 0x67, 0x51, 0x59,
  73. 0x50, 0x4e, 0x69, 0x69, 0x43, 0x74, 0x57, 0x67, 0x43, 0x2b, 0x59, 0x34,
  74. 0x4b, 0x37, 0x75, 0x53, 0x5a, 0x58, 0x33, 0x48, 0x76, 0x65, 0x6f, 0x6d,
  75. 0x32, 0x74, 0x48, 0x62, 0x56, 0x58, 0x79, 0x30, 0x4c, 0x2f, 0x43, 0x6c,
  76. 0x37, 0x66, 0x4d, 0x0a, 0x48, 0x4b, 0x71, 0x66, 0x63, 0x51, 0x47, 0x75,
  77. 0x79, 0x72, 0x76, 0x75, 0x64, 0x34, 0x32, 0x4f, 0x72, 0x57, 0x61, 0x72,
  78. 0x41, 0x73, 0x6e, 0x32, 0x70, 0x32, 0x45, 0x69, 0x36, 0x4b, 0x7a, 0x78,
  79. 0x62, 0x33, 0x47, 0x36, 0x45, 0x53, 0x43, 0x77, 0x31, 0x35, 0x6e, 0x48,
  80. 0x41, 0x67, 0x4c, 0x61, 0x6c, 0x38, 0x7a, 0x53, 0x71, 0x37, 0x2b, 0x72,
  81. 0x61, 0x45, 0x2f, 0x78, 0x6b, 0x4c, 0x70, 0x43, 0x0a, 0x62, 0x59, 0x67,
  82. 0x35, 0x67, 0x6d, 0x59, 0x36, 0x76, 0x62, 0x6d, 0x57, 0x6e, 0x71, 0x39,
  83. 0x64, 0x71, 0x57, 0x72, 0x55, 0x7a, 0x61, 0x71, 0x4f, 0x66, 0x72, 0x5a,
  84. 0x50, 0x67, 0x76, 0x67, 0x47, 0x30, 0x57, 0x76, 0x6b, 0x42, 0x53, 0x68,
  85. 0x66, 0x61, 0x45, 0x4f, 0x42, 0x61, 0x49, 0x55, 0x78, 0x41, 0x33, 0x51,
  86. 0x42, 0x67, 0x7a, 0x41, 0x5a, 0x68, 0x71, 0x65, 0x65, 0x64, 0x46, 0x39,
  87. 0x68, 0x0a, 0x61, 0x66, 0x4d, 0x47, 0x4d, 0x4d, 0x39, 0x71, 0x56, 0x62,
  88. 0x66, 0x77, 0x75, 0x75, 0x7a, 0x4a, 0x32, 0x75, 0x68, 0x2b, 0x49, 0x6e,
  89. 0x61, 0x47, 0x61, 0x65, 0x48, 0x32, 0x63, 0x30, 0x34, 0x6f, 0x56, 0x63,
  90. 0x44, 0x46, 0x66, 0x65, 0x4f, 0x61, 0x44, 0x75, 0x78, 0x52, 0x6a, 0x43,
  91. 0x43, 0x62, 0x71, 0x72, 0x35, 0x73, 0x4c, 0x53, 0x6f, 0x31, 0x43, 0x57,
  92. 0x6f, 0x6b, 0x79, 0x6e, 0x6a, 0x4e, 0x0a, 0x43, 0x42, 0x2b, 0x62, 0x32,
  93. 0x72, 0x51, 0x46, 0x37, 0x44, 0x50, 0x50, 0x62, 0x44, 0x34, 0x73, 0x2f,
  94. 0x6e, 0x54, 0x39, 0x4e, 0x73, 0x63, 0x6b, 0x2f, 0x4e, 0x46, 0x7a, 0x72,
  95. 0x42, 0x58, 0x52, 0x4f, 0x2b, 0x64, 0x71, 0x6b, 0x65, 0x42, 0x77, 0x44,
  96. 0x55, 0x43, 0x76, 0x37, 0x62, 0x5a, 0x67, 0x57, 0x37, 0x4f, 0x78, 0x75,
  97. 0x4f, 0x58, 0x30, 0x37, 0x4c, 0x54, 0x71, 0x66, 0x70, 0x35, 0x73, 0x0a,
  98. 0x4f, 0x65, 0x47, 0x67, 0x75, 0x62, 0x75, 0x62, 0x69, 0x77, 0x59, 0x33,
  99. 0x55, 0x64, 0x48, 0x59, 0x71, 0x2b, 0x4c, 0x39, 0x4a, 0x71, 0x49, 0x53,
  100. 0x47, 0x31, 0x74, 0x4d, 0x34, 0x48, 0x65, 0x4b, 0x6a, 0x61, 0x48, 0x6a,
  101. 0x75, 0x31, 0x4d, 0x44, 0x6a, 0x76, 0x48, 0x5a, 0x32, 0x44, 0x62, 0x6d,
  102. 0x4c, 0x77, 0x55, 0x78, 0x75, 0x59, 0x61, 0x36, 0x4a, 0x5a, 0x44, 0x4b,
  103. 0x57, 0x73, 0x37, 0x72, 0x0a, 0x49, 0x72, 0x64, 0x44, 0x77, 0x78, 0x33,
  104. 0x4a, 0x77, 0x61, 0x63, 0x46, 0x36, 0x36, 0x68, 0x33, 0x59, 0x55, 0x57,
  105. 0x36, 0x74, 0x7a, 0x55, 0x5a, 0x68, 0x7a, 0x74, 0x63, 0x6d, 0x51, 0x65,
  106. 0x70, 0x50, 0x2f, 0x75, 0x37, 0x42, 0x67, 0x47, 0x72, 0x6b, 0x4f, 0x50,
  107. 0x50, 0x70, 0x59, 0x41, 0x30, 0x4e, 0x45, 0x4a, 0x38, 0x30, 0x53, 0x65,
  108. 0x41, 0x78, 0x37, 0x68, 0x69, 0x4e, 0x34, 0x76, 0x61, 0x0a, 0x65, 0x45,
  109. 0x51, 0x4b, 0x6e, 0x52, 0x6e, 0x2b, 0x45, 0x70, 0x42, 0x4e, 0x36, 0x55,
  110. 0x42, 0x61, 0x35, 0x66, 0x37, 0x4c, 0x6f, 0x4b, 0x38, 0x43, 0x41, 0x77,
  111. 0x45, 0x41, 0x41, 0x51, 0x3d, 0x3d, 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d,
  112. 0x45, 0x4e, 0x44, 0x20, 0x50, 0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b,
  113. 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a};
  114. static const unsigned int obs_pub_len = 800;
  115. /* ------------------------------------------------------------------------ */
  116. static bool QuickWriteFile(const char *file, const void *data, size_t size)
  117. try {
  118. BPtr<wchar_t> w_file;
  119. if (os_utf8_to_wcs_ptr(file, 0, &w_file) == 0)
  120. return false;
  121. WinHandle handle = CreateFileW(w_file, GENERIC_WRITE, 0, nullptr,
  122. CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
  123. nullptr);
  124. if (handle == INVALID_HANDLE_VALUE)
  125. throw strprintf("Failed to open file '%s': %lu", file,
  126. GetLastError());
  127. DWORD written;
  128. if (!WriteFile(handle, data, (DWORD)size, &written, nullptr))
  129. throw strprintf("Failed to write file '%s': %lu", file,
  130. GetLastError());
  131. return true;
  132. } catch (string &text) {
  133. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  134. return false;
  135. }
  136. static bool QuickReadFile(const char *file, string &data)
  137. try {
  138. BPtr<wchar_t> w_file;
  139. if (os_utf8_to_wcs_ptr(file, 0, &w_file) == 0)
  140. return false;
  141. WinHandle handle = CreateFileW(w_file, GENERIC_READ, FILE_SHARE_READ,
  142. nullptr, OPEN_EXISTING, 0, nullptr);
  143. if (handle == INVALID_HANDLE_VALUE)
  144. throw strprintf("Failed to open file '%s': %lu", file,
  145. GetLastError());
  146. DWORD size = GetFileSize(handle, nullptr);
  147. data.resize(size);
  148. DWORD read;
  149. if (!ReadFile(handle, &data[0], size, &read, nullptr))
  150. throw strprintf("Failed to write file '%s': %lu", file,
  151. GetLastError());
  152. return true;
  153. } catch (string &text) {
  154. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  155. return false;
  156. }
  157. static void HashToString(const uint8_t *in, char *out)
  158. {
  159. const char alphabet[] = "0123456789abcdef";
  160. for (int i = 0; i != BLAKE2_HASH_LENGTH; ++i) {
  161. out[2 * i] = alphabet[in[i] / 16];
  162. out[2 * i + 1] = alphabet[in[i] % 16];
  163. }
  164. out[BLAKE2_HASH_LENGTH * 2] = 0;
  165. }
  166. static bool CalculateFileHash(const char *path, uint8_t *hash)
  167. try {
  168. blake2b_state blake2;
  169. if (blake2b_init(&blake2, BLAKE2_HASH_LENGTH) != 0)
  170. return false;
  171. BPtr<wchar_t> w_path;
  172. if (os_utf8_to_wcs_ptr(path, 0, &w_path) == 0)
  173. return false;
  174. WinHandle handle = CreateFileW(w_path, GENERIC_READ, FILE_SHARE_READ,
  175. nullptr, OPEN_EXISTING, 0, nullptr);
  176. if (handle == INVALID_HANDLE_VALUE)
  177. throw strprintf("Failed to open file '%s': %lu", path,
  178. GetLastError());
  179. vector<BYTE> buf;
  180. buf.resize(65536);
  181. for (;;) {
  182. DWORD read = 0;
  183. if (!ReadFile(handle, buf.data(), (DWORD)buf.size(), &read,
  184. nullptr))
  185. throw strprintf("Failed to read file '%s': %lu", path,
  186. GetLastError());
  187. if (!read)
  188. break;
  189. if (blake2b_update(&blake2, buf.data(), read) != 0)
  190. return false;
  191. }
  192. if (blake2b_final(&blake2, hash, BLAKE2_HASH_LENGTH) != 0)
  193. return false;
  194. return true;
  195. } catch (string &text) {
  196. blog(LOG_DEBUG, "%s: %s", __FUNCTION__, text.c_str());
  197. return false;
  198. }
  199. /* ------------------------------------------------------------------------ */
  200. static bool VerifyDigitalSignature(uint8_t *buf, size_t len, uint8_t *sig,
  201. size_t sigLen)
  202. {
  203. /* ASN of PEM public key */
  204. BYTE binaryKey[1024];
  205. DWORD binaryKeyLen = sizeof(binaryKey);
  206. /* Windows X509 public key info from ASN */
  207. LocalPtr<CERT_PUBLIC_KEY_INFO> publicPBLOB;
  208. DWORD iPBLOBSize;
  209. /* RSA BLOB info from X509 public key */
  210. LocalPtr<PUBLICKEYHEADER> rsaPublicBLOB;
  211. DWORD rsaPublicBLOBSize;
  212. /* Handle to public key */
  213. CryptKey keyOut;
  214. /* Handle to hash context */
  215. CryptHash hash;
  216. /* Signature in little-endian format */
  217. vector<BYTE> reversedSig;
  218. if (!CryptStringToBinaryA((LPCSTR)obs_pub, obs_pub_len,
  219. CRYPT_STRING_BASE64HEADER, binaryKey,
  220. &binaryKeyLen, nullptr, nullptr))
  221. return false;
  222. if (!CryptDecodeObjectEx(X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO,
  223. binaryKey, binaryKeyLen,
  224. CRYPT_DECODE_ALLOC_FLAG, nullptr, &publicPBLOB,
  225. &iPBLOBSize))
  226. return false;
  227. if (!CryptDecodeObjectEx(X509_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB,
  228. publicPBLOB->PublicKey.pbData,
  229. publicPBLOB->PublicKey.cbData,
  230. CRYPT_DECODE_ALLOC_FLAG, nullptr,
  231. &rsaPublicBLOB, &rsaPublicBLOBSize))
  232. return false;
  233. if (!CryptImportKey(provider, (const BYTE *)rsaPublicBLOB.get(),
  234. rsaPublicBLOBSize, 0, 0, &keyOut))
  235. return false;
  236. if (!CryptCreateHash(provider, CALG_SHA_512, 0, 0, &hash))
  237. return false;
  238. if (!CryptHashData(hash, buf, (DWORD)len, 0))
  239. return false;
  240. /* Windows requires signature in little-endian. Every other crypto
  241. * provider is big-endian of course. */
  242. reversedSig.resize(sigLen);
  243. for (size_t i = 0; i < sigLen; i++)
  244. reversedSig[i] = sig[sigLen - i - 1];
  245. if (!CryptVerifySignature(hash, reversedSig.data(), (DWORD)sigLen,
  246. keyOut, nullptr, 0))
  247. return false;
  248. return true;
  249. }
  250. static inline void HexToByteArray(const char *hexStr, size_t hexLen,
  251. vector<uint8_t> &out)
  252. {
  253. char ptr[3];
  254. ptr[2] = 0;
  255. for (size_t i = 0; i < hexLen; i += 2) {
  256. ptr[0] = hexStr[i];
  257. ptr[1] = hexStr[i + 1];
  258. out.push_back((uint8_t)strtoul(ptr, nullptr, 16));
  259. }
  260. }
  261. static bool CheckDataSignature(const string &data, const char *name,
  262. const char *hexSig, size_t sigLen)
  263. try {
  264. if (sigLen == 0 || sigLen > 0xFFFF || (sigLen & 1) != 0)
  265. throw strprintf("Missing or invalid signature for %s", name);
  266. /* Convert TCHAR signature to byte array */
  267. vector<uint8_t> signature;
  268. signature.reserve(sigLen);
  269. HexToByteArray(hexSig, sigLen, signature);
  270. if (!VerifyDigitalSignature((uint8_t *)data.data(), data.size(),
  271. signature.data(), signature.size()))
  272. throw strprintf("Signature check failed for %s", name);
  273. return true;
  274. } catch (string &text) {
  275. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  276. return false;
  277. }
  278. /* ------------------------------------------------------------------------ */
  279. static bool FetchUpdaterModule(const char *url)
  280. try {
  281. long responseCode;
  282. uint8_t updateFileHash[BLAKE2_HASH_LENGTH];
  283. vector<string> extraHeaders;
  284. BPtr<char> updateFilePath =
  285. GetConfigPathPtr("obs-studio\\updates\\updater.exe");
  286. if (CalculateFileHash(updateFilePath, updateFileHash)) {
  287. char hashString[BLAKE2_HASH_STR_LENGTH];
  288. HashToString(updateFileHash, hashString);
  289. string header = "If-None-Match: ";
  290. header += hashString;
  291. extraHeaders.push_back(move(header));
  292. }
  293. string signature;
  294. string error;
  295. string data;
  296. bool success = GetRemoteFile(url, data, error, &responseCode, nullptr,
  297. "", nullptr, extraHeaders, &signature);
  298. if (!success || (responseCode != 200 && responseCode != 304)) {
  299. if (responseCode == 404)
  300. return false;
  301. throw strprintf("Could not fetch '%s': %s", url, error.c_str());
  302. }
  303. /* A new file must be digitally signed */
  304. if (responseCode == 200) {
  305. bool valid = CheckDataSignature(data, url, signature.data(),
  306. signature.size());
  307. if (!valid)
  308. throw string("Invalid updater module signature");
  309. if (!QuickWriteFile(updateFilePath, data.data(), data.size()))
  310. return false;
  311. }
  312. return true;
  313. } catch (string &text) {
  314. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  315. return false;
  316. }
  317. /* ------------------------------------------------------------------------ */
  318. static bool ParseUpdateManifest(const char *manifest, bool *updatesAvailable,
  319. string &notes_str, int &updateVer)
  320. try {
  321. string error;
  322. Json root = Json::parse(manifest, error);
  323. if (!error.empty())
  324. throw strprintf("Failed reading json string: %s",
  325. error.c_str());
  326. if (!root.is_object())
  327. throw string("Root of manifest is not an object");
  328. int major = root["version_major"].int_value();
  329. int minor = root["version_minor"].int_value();
  330. int patch = root["version_patch"].int_value();
  331. if (major == 0)
  332. throw strprintf("Invalid version number: %d.%d.%d", major,
  333. minor, patch);
  334. const Json &notes = root["notes"];
  335. if (!notes.is_string())
  336. throw string("'notes' value invalid");
  337. notes_str = notes.string_value();
  338. const Json &packages = root["packages"];
  339. if (!packages.is_array())
  340. throw string("'packages' value invalid");
  341. int cur_ver = LIBOBS_API_VER;
  342. int new_ver = MAKE_SEMANTIC_VERSION(major, minor, patch);
  343. updateVer = new_ver;
  344. *updatesAvailable = new_ver > cur_ver;
  345. return true;
  346. } catch (string &text) {
  347. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  348. return false;
  349. }
  350. /* ------------------------------------------------------------------------ */
  351. void GenerateGUID(string &guid)
  352. {
  353. BYTE junk[20];
  354. if (!CryptGenRandom(provider, sizeof(junk), junk))
  355. return;
  356. guid.resize(41);
  357. HashToString(junk, &guid[0]);
  358. }
  359. string GetProgramGUID()
  360. {
  361. static mutex m;
  362. lock_guard<mutex> lock(m);
  363. /* NOTE: this is an arbitrary random number that we use to count the
  364. * number of unique OBS installations and is not associated with any
  365. * kind of identifiable information */
  366. const char *pguid =
  367. config_get_string(GetGlobalConfig(), "General", "InstallGUID");
  368. string guid;
  369. if (pguid)
  370. guid = pguid;
  371. if (guid.empty()) {
  372. GenerateGUID(guid);
  373. if (!guid.empty())
  374. config_set_string(GetGlobalConfig(), "General",
  375. "InstallGUID", guid.c_str());
  376. }
  377. return guid;
  378. }
  379. void AutoUpdateThread::infoMsg(const QString &title, const QString &text)
  380. {
  381. OBSMessageBox::information(App()->GetMainWindow(), title, text);
  382. }
  383. void AutoUpdateThread::info(const QString &title, const QString &text)
  384. {
  385. QMetaObject::invokeMethod(this, "infoMsg", Qt::BlockingQueuedConnection,
  386. Q_ARG(QString, title), Q_ARG(QString, text));
  387. }
  388. int AutoUpdateThread::queryUpdateSlot(bool localManualUpdate,
  389. const QString &text)
  390. {
  391. OBSUpdate updateDlg(App()->GetMainWindow(), localManualUpdate, text);
  392. return updateDlg.exec();
  393. }
  394. int AutoUpdateThread::queryUpdate(bool localManualUpdate, const char *text_utf8)
  395. {
  396. int ret = OBSUpdate::No;
  397. QString text = text_utf8;
  398. QMetaObject::invokeMethod(this, "queryUpdateSlot",
  399. Qt::BlockingQueuedConnection,
  400. Q_RETURN_ARG(int, ret),
  401. Q_ARG(bool, localManualUpdate),
  402. Q_ARG(QString, text));
  403. return ret;
  404. }
  405. bool AutoUpdateThread::queryRepairSlot()
  406. {
  407. QMessageBox::StandardButton res = OBSMessageBox::question(
  408. App()->GetMainWindow(), QTStr("Updater.RepairConfirm.Title"),
  409. QTStr("Updater.RepairConfirm.Text"),
  410. QMessageBox::Yes | QMessageBox::Cancel);
  411. return res == QMessageBox::Yes;
  412. }
  413. bool AutoUpdateThread::queryRepair()
  414. {
  415. bool ret = false;
  416. QMetaObject::invokeMethod(this, "queryRepairSlot",
  417. Qt::BlockingQueuedConnection,
  418. Q_RETURN_ARG(bool, ret));
  419. return ret;
  420. }
  421. void AutoUpdateThread::run()
  422. try {
  423. long responseCode;
  424. vector<string> extraHeaders;
  425. string text;
  426. string error;
  427. string signature;
  428. CryptProvider localProvider;
  429. BYTE manifestHash[BLAKE2_HASH_LENGTH];
  430. bool updatesAvailable = false;
  431. bool success;
  432. struct FinishedTrigger {
  433. inline ~FinishedTrigger()
  434. {
  435. QMetaObject::invokeMethod(App()->GetMainWindow(),
  436. "updateCheckFinished");
  437. }
  438. } finishedTrigger;
  439. BPtr<char> manifestPath =
  440. GetConfigPathPtr("obs-studio\\updates\\manifest.json");
  441. /* ----------------------------------- *
  442. * create signature provider */
  443. if (!CryptAcquireContext(&localProvider, nullptr, MS_ENH_RSA_AES_PROV,
  444. PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  445. throw strprintf("CryptAcquireContext failed: %lu",
  446. GetLastError());
  447. provider = localProvider;
  448. /* ----------------------------------- *
  449. * avoid downloading manifest again */
  450. if (CalculateFileHash(manifestPath, manifestHash)) {
  451. char hashString[BLAKE2_HASH_STR_LENGTH];
  452. HashToString(manifestHash, hashString);
  453. string header = "If-None-Match: ";
  454. header += hashString;
  455. extraHeaders.push_back(move(header));
  456. }
  457. /* ----------------------------------- *
  458. * get current install GUID */
  459. string guid = GetProgramGUID();
  460. if (!guid.empty()) {
  461. string header = "X-OBS2-GUID: ";
  462. header += guid;
  463. extraHeaders.push_back(move(header));
  464. }
  465. /* allow server to know if this was a manual update check in case
  466. * we want to allow people to bypass a configured rollout rate */
  467. if (manualUpdate)
  468. extraHeaders.emplace_back("X-OBS2-ManualUpdate: 1");
  469. /* ----------------------------------- *
  470. * get manifest from server */
  471. success = GetRemoteFile(WIN_MANIFEST_URL, text, error, &responseCode,
  472. nullptr, "", nullptr, extraHeaders, &signature);
  473. if (!success || (responseCode != 200 && responseCode != 304)) {
  474. if (responseCode == 404)
  475. return;
  476. throw strprintf("Failed to fetch manifest file: %s",
  477. error.c_str());
  478. }
  479. /* ----------------------------------- *
  480. * verify file signature */
  481. /* a new file must be digitally signed */
  482. if (responseCode == 200) {
  483. success = CheckDataSignature(text, "manifest", signature.data(),
  484. signature.size());
  485. if (!success)
  486. throw string("Invalid manifest signature");
  487. }
  488. /* ----------------------------------- *
  489. * write or load manifest */
  490. if (responseCode == 200) {
  491. if (!QuickWriteFile(manifestPath, text.data(), text.size()))
  492. throw strprintf("Could not write file '%s'",
  493. manifestPath.Get());
  494. } else {
  495. if (!QuickReadFile(manifestPath, text))
  496. throw strprintf("Could not read file '%s'",
  497. manifestPath.Get());
  498. }
  499. /* ----------------------------------- *
  500. * check manifest for update */
  501. string notes;
  502. int updateVer = 0;
  503. success = ParseUpdateManifest(text.c_str(), &updatesAvailable, notes,
  504. updateVer);
  505. if (!success)
  506. throw string("Failed to parse manifest");
  507. if (!updatesAvailable && !repairMode) {
  508. if (manualUpdate)
  509. info(QTStr("Updater.NoUpdatesAvailable.Title"),
  510. QTStr("Updater.NoUpdatesAvailable.Text"));
  511. return;
  512. } else if (updatesAvailable && repairMode) {
  513. info(QTStr("Updater.RepairButUpdatesAvailable.Title"),
  514. QTStr("Updater.RepairButUpdatesAvailable.Text"));
  515. return;
  516. }
  517. /* ----------------------------------- *
  518. * skip this version if set to skip */
  519. int skipUpdateVer = config_get_int(GetGlobalConfig(), "General",
  520. "SkipUpdateVersion");
  521. if (!manualUpdate && updateVer == skipUpdateVer && !repairMode)
  522. return;
  523. /* ----------------------------------- *
  524. * fetch updater module */
  525. if (!FetchUpdaterModule(WIN_UPDATER_URL))
  526. return;
  527. /* ----------------------------------- *
  528. * query user for update */
  529. if (repairMode) {
  530. if (!queryRepair())
  531. return;
  532. } else {
  533. int queryResult = queryUpdate(manualUpdate, notes.c_str());
  534. if (queryResult == OBSUpdate::No) {
  535. if (!manualUpdate) {
  536. long long t = (long long)time(nullptr);
  537. config_set_int(GetGlobalConfig(), "General",
  538. "LastUpdateCheck", t);
  539. }
  540. return;
  541. } else if (queryResult == OBSUpdate::Skip) {
  542. config_set_int(GetGlobalConfig(), "General",
  543. "SkipUpdateVersion", updateVer);
  544. return;
  545. }
  546. }
  547. /* ----------------------------------- *
  548. * get working dir */
  549. wchar_t cwd[MAX_PATH];
  550. GetModuleFileNameW(nullptr, cwd, _countof(cwd) - 1);
  551. wchar_t *p = wcsrchr(cwd, '\\');
  552. if (p)
  553. *p = 0;
  554. /* ----------------------------------- *
  555. * execute updater */
  556. BPtr<char> updateFilePath =
  557. GetConfigPathPtr("obs-studio\\updates\\updater.exe");
  558. BPtr<wchar_t> wUpdateFilePath;
  559. size_t size = os_utf8_to_wcs_ptr(updateFilePath, 0, &wUpdateFilePath);
  560. if (!size)
  561. throw string("Could not convert updateFilePath to wide");
  562. /* note, can't use CreateProcess to launch as admin. */
  563. SHELLEXECUTEINFO execInfo = {};
  564. execInfo.cbSize = sizeof(execInfo);
  565. execInfo.lpFile = wUpdateFilePath;
  566. #ifndef UPDATE_CHANNEL
  567. #define UPDATE_ARG_SUFFIX L""
  568. #else
  569. #define UPDATE_ARG_SUFFIX UPDATE_CHANNEL
  570. #endif
  571. if (App()->IsPortableMode())
  572. execInfo.lpParameters = UPDATE_ARG_SUFFIX L" Portable";
  573. else
  574. execInfo.lpParameters = UPDATE_ARG_SUFFIX;
  575. execInfo.lpDirectory = cwd;
  576. execInfo.nShow = SW_SHOWNORMAL;
  577. if (!ShellExecuteEx(&execInfo)) {
  578. QString msg = QTStr("Updater.FailedToLaunch");
  579. info(msg, msg);
  580. throw strprintf("Can't launch updater '%s': %d",
  581. updateFilePath.Get(), GetLastError());
  582. }
  583. /* force OBS to perform another update check immediately after updating
  584. * in case of issues with the new version */
  585. config_set_int(GetGlobalConfig(), "General", "LastUpdateCheck", 0);
  586. config_set_int(GetGlobalConfig(), "General", "SkipUpdateVersion", 0);
  587. config_set_string(GetGlobalConfig(), "General", "InstallGUID",
  588. guid.c_str());
  589. QMetaObject::invokeMethod(App()->GetMainWindow(), "close");
  590. } catch (string &text) {
  591. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  592. }
  593. /* ------------------------------------------------------------------------ */
  594. void WhatsNewInfoThread::run()
  595. try {
  596. long responseCode;
  597. vector<string> extraHeaders;
  598. string text;
  599. string error;
  600. string signature;
  601. CryptProvider localProvider;
  602. BYTE whatsnewHash[BLAKE2_HASH_LENGTH];
  603. bool success;
  604. BPtr<char> whatsnewPath =
  605. GetConfigPathPtr("obs-studio\\updates\\whatsnew.json");
  606. /* ----------------------------------- *
  607. * create signature provider */
  608. if (!CryptAcquireContext(&localProvider, nullptr, MS_ENH_RSA_AES_PROV,
  609. PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  610. throw strprintf("CryptAcquireContext failed: %lu",
  611. GetLastError());
  612. provider = localProvider;
  613. /* ----------------------------------- *
  614. * avoid downloading json again */
  615. if (CalculateFileHash(whatsnewPath, whatsnewHash)) {
  616. char hashString[BLAKE2_HASH_STR_LENGTH];
  617. HashToString(whatsnewHash, hashString);
  618. string header = "If-None-Match: ";
  619. header += hashString;
  620. extraHeaders.push_back(move(header));
  621. }
  622. /* ----------------------------------- *
  623. * get current install GUID */
  624. string guid = GetProgramGUID();
  625. if (!guid.empty()) {
  626. string header = "X-OBS2-GUID: ";
  627. header += guid;
  628. extraHeaders.push_back(move(header));
  629. }
  630. /* ----------------------------------- *
  631. * get json from server */
  632. success = GetRemoteFile(WIN_WHATSNEW_URL, text, error, &responseCode,
  633. nullptr, "", nullptr, extraHeaders, &signature);
  634. if (!success || (responseCode != 200 && responseCode != 304)) {
  635. if (responseCode == 404)
  636. return;
  637. throw strprintf("Failed to fetch whatsnew file: %s",
  638. error.c_str());
  639. }
  640. /* ----------------------------------- *
  641. * verify file signature */
  642. if (responseCode == 200) {
  643. success = CheckDataSignature(text, "whatsnew", signature.data(),
  644. signature.size());
  645. if (!success)
  646. throw string("Invalid whatsnew signature");
  647. }
  648. /* ----------------------------------- *
  649. * write or load json */
  650. if (responseCode == 200) {
  651. if (!QuickWriteFile(whatsnewPath, text.data(), text.size()))
  652. throw strprintf("Could not write file '%s'",
  653. whatsnewPath.Get());
  654. } else {
  655. if (!QuickReadFile(whatsnewPath, text))
  656. throw strprintf("Could not read file '%s'",
  657. whatsnewPath.Get());
  658. }
  659. /* ----------------------------------- *
  660. * success */
  661. emit Result(QString::fromUtf8(text.c_str()));
  662. } catch (string &text) {
  663. blog(LOG_WARNING, "%s: %s", __FUNCTION__, text.c_str());
  664. }
  665. /* ------------------------------------------------------------------------ */
  666. void WhatsNewBrowserInitThread::run()
  667. {
  668. #ifdef BROWSER_AVAILABLE
  669. cef->wait_for_browser_init();
  670. #endif
  671. emit Result(url);
  672. }