win-update.cpp 26 KB

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