win-update.cpp 23 KB

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