win-update.cpp 27 KB

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