win-update.cpp 23 KB

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