win-update.cpp 27 KB

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