win-update.cpp 24 KB

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