win-update.cpp 25 KB

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