Manifest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using Newtonsoft.Json;
  2. using SteamAuth;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Steam_Desktop_Authenticator
  11. {
  12. public class Manifest
  13. {
  14. [JsonProperty("encrypted")]
  15. public bool Encrypted { get; set; }
  16. [JsonProperty("first_run")]
  17. public bool FirstRun { get; set; } = true;
  18. [JsonProperty("entries")]
  19. public List<ManifestEntry> Entries { get; set; }
  20. [JsonProperty("periodic_checking")]
  21. public bool PeriodicChecking { get; set; } = false;
  22. [JsonProperty("periodic_checking_interval")]
  23. public int PeriodicCheckingInterval { get; set; } = 5;
  24. [JsonProperty("periodic_checking_checkall")]
  25. public bool CheckAllAccounts { get; set; } = false;
  26. [JsonProperty("auto_confirm_market_transactions")]
  27. public bool AutoConfirmMarketTransactions { get; set; } = false;
  28. [JsonProperty("auto_confirm_trades")]
  29. public bool AutoConfirmTrades { get; set; } = false;
  30. private static Manifest _manifest { get; set; }
  31. public static string GetExecutableDir()
  32. {
  33. return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
  34. }
  35. public static Manifest GetManifest(bool forceLoad = false)
  36. {
  37. // Check if already staticly loaded
  38. if (_manifest != null && !forceLoad)
  39. {
  40. return _manifest;
  41. }
  42. // Find config dir and manifest file
  43. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  44. string manifestFile = maDir + "manifest.json";
  45. // If there's no config dir, create it
  46. if (!Directory.Exists(maDir))
  47. {
  48. _manifest = GenerateNewManifest(false);
  49. return _manifest;
  50. }
  51. // If there's no manifest, throw exception
  52. if (!File.Exists(manifestFile))
  53. {
  54. throw new ManifestParseException();
  55. }
  56. try
  57. {
  58. string manifestContents = File.ReadAllText(manifestFile);
  59. _manifest = JsonConvert.DeserializeObject<Manifest>(manifestContents);
  60. if (_manifest.Encrypted && _manifest.Entries.Count == 0)
  61. {
  62. _manifest.Encrypted = false;
  63. _manifest.Save();
  64. }
  65. _manifest.RecomputeExistingEntries();
  66. return _manifest;
  67. }
  68. catch (Exception)
  69. {
  70. throw new ManifestParseException();
  71. }
  72. }
  73. public static Manifest GenerateNewManifest(bool scanDir = false)
  74. {
  75. // No directory means no manifest file anyways.
  76. Manifest newManifest = new Manifest();
  77. newManifest.Encrypted = false;
  78. newManifest.PeriodicCheckingInterval = 5;
  79. newManifest.PeriodicChecking = false;
  80. newManifest.AutoConfirmMarketTransactions = false;
  81. newManifest.AutoConfirmTrades = false;
  82. newManifest.Entries = new List<ManifestEntry>();
  83. newManifest.FirstRun = true;
  84. // Take a pre-manifest version and generate a manifest for it.
  85. if (scanDir)
  86. {
  87. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  88. if (Directory.Exists(maDir))
  89. {
  90. DirectoryInfo dir = new DirectoryInfo(maDir);
  91. var files = dir.GetFiles();
  92. foreach (var file in files)
  93. {
  94. if (file.Extension != ".maFile") continue;
  95. string contents = File.ReadAllText(file.FullName);
  96. try
  97. {
  98. SteamGuardAccount account = JsonConvert.DeserializeObject<SteamGuardAccount>(contents);
  99. ManifestEntry newEntry = new ManifestEntry()
  100. {
  101. Filename = file.Name,
  102. SteamID = account.Session.SteamID
  103. };
  104. newManifest.Entries.Add(newEntry);
  105. }
  106. catch (Exception)
  107. {
  108. throw new MaFileEncryptedException();
  109. }
  110. }
  111. if (newManifest.Entries.Count > 0)
  112. {
  113. newManifest.Save();
  114. newManifest.PromptSetupPassKey("This version of SDA has encryption. Please enter a passkey below, or hit cancel to remain unencrypted");
  115. }
  116. }
  117. }
  118. if (newManifest.Save())
  119. {
  120. return newManifest;
  121. }
  122. return null;
  123. }
  124. public class IncorrectPassKeyException : Exception { }
  125. public class ManifestNotEncryptedException : Exception { }
  126. public string PromptForPassKey()
  127. {
  128. if (!this.Encrypted)
  129. {
  130. throw new ManifestNotEncryptedException();
  131. }
  132. bool passKeyValid = false;
  133. string passKey = null;
  134. while (!passKeyValid)
  135. {
  136. InputForm passKeyForm = new InputForm("Please enter your encryption passkey.", true);
  137. passKeyForm.ShowDialog();
  138. if (!passKeyForm.Canceled)
  139. {
  140. passKey = passKeyForm.txtBox.Text;
  141. passKeyValid = this.VerifyPasskey(passKey);
  142. if (!passKeyValid)
  143. {
  144. MessageBox.Show("That passkey is invalid.");
  145. }
  146. }
  147. else
  148. {
  149. return null;
  150. }
  151. }
  152. return passKey;
  153. }
  154. public string PromptSetupPassKey(string initialPrompt = "Enter passkey, or hit cancel to remain unencrypted.")
  155. {
  156. InputForm newPassKeyForm = new InputForm(initialPrompt);
  157. newPassKeyForm.ShowDialog();
  158. if (newPassKeyForm.Canceled || newPassKeyForm.txtBox.Text.Length == 0)
  159. {
  160. MessageBox.Show("WARNING: You chose to not encrypt your files. Doing so imposes a security risk for yourself. If an attacker were to gain access to your computer, they could completely lock you out of your account and steal all your items.");
  161. return null;
  162. }
  163. InputForm newPassKeyForm2 = new InputForm("Confirm new passkey.");
  164. newPassKeyForm2.ShowDialog();
  165. if (newPassKeyForm2.Canceled)
  166. {
  167. MessageBox.Show("WARNING: You chose to not encrypt your files. Doing so imposes a security risk for yourself. If an attacker were to gain access to your computer, they could completely lock you out of your account and steal all your items.");
  168. return null;
  169. }
  170. string newPassKey = newPassKeyForm.txtBox.Text;
  171. string confirmPassKey = newPassKeyForm2.txtBox.Text;
  172. if (newPassKey != confirmPassKey)
  173. {
  174. MessageBox.Show("Passkeys do not match.");
  175. return null;
  176. }
  177. if (!this.ChangeEncryptionKey(null, newPassKey))
  178. {
  179. MessageBox.Show("Unable to set passkey.");
  180. return null;
  181. }
  182. else
  183. {
  184. MessageBox.Show("Passkey successfully set.");
  185. }
  186. return newPassKey;
  187. }
  188. public SteamAuth.SteamGuardAccount[] GetAllAccounts(string passKey = null, int limit = -1)
  189. {
  190. if (passKey == null && this.Encrypted) return new SteamGuardAccount[0];
  191. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  192. List<SteamAuth.SteamGuardAccount> accounts = new List<SteamAuth.SteamGuardAccount>();
  193. foreach (var entry in this.Entries)
  194. {
  195. string fileText = File.ReadAllText(maDir + entry.Filename);
  196. if (this.Encrypted)
  197. {
  198. string decryptedText = FileEncryptor.DecryptData(passKey, entry.Salt, entry.IV, fileText);
  199. if (decryptedText == null) return new SteamGuardAccount[0];
  200. fileText = decryptedText;
  201. }
  202. var account = JsonConvert.DeserializeObject<SteamAuth.SteamGuardAccount>(fileText);
  203. if (account == null) continue;
  204. accounts.Add(account);
  205. if (limit != -1 && limit >= accounts.Count)
  206. break;
  207. }
  208. return accounts.ToArray();
  209. }
  210. public bool ChangeEncryptionKey(string oldKey, string newKey)
  211. {
  212. if (this.Encrypted)
  213. {
  214. if (!this.VerifyPasskey(oldKey))
  215. {
  216. return false;
  217. }
  218. }
  219. bool toEncrypt = newKey != null;
  220. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  221. for (int i = 0; i < this.Entries.Count; i++)
  222. {
  223. ManifestEntry entry = this.Entries[i];
  224. string filename = maDir + entry.Filename;
  225. if (!File.Exists(filename)) continue;
  226. string fileContents = File.ReadAllText(filename);
  227. if (this.Encrypted)
  228. {
  229. fileContents = FileEncryptor.DecryptData(oldKey, entry.Salt, entry.IV, fileContents);
  230. }
  231. string newSalt = null;
  232. string newIV = null;
  233. string toWriteFileContents = fileContents;
  234. if (toEncrypt)
  235. {
  236. newSalt = FileEncryptor.GetRandomSalt();
  237. newIV = FileEncryptor.GetInitializationVector();
  238. toWriteFileContents = FileEncryptor.EncryptData(newKey, newSalt, newIV, fileContents);
  239. }
  240. File.WriteAllText(filename, toWriteFileContents);
  241. entry.IV = newIV;
  242. entry.Salt = newSalt;
  243. }
  244. this.Encrypted = toEncrypt;
  245. this.Save();
  246. return true;
  247. }
  248. public bool VerifyPasskey(string passkey)
  249. {
  250. if (!this.Encrypted || this.Entries.Count == 0) return true;
  251. var accounts = this.GetAllAccounts(passkey, 1);
  252. return accounts != null && accounts.Length == 1;
  253. }
  254. public bool RemoveAccount(SteamGuardAccount account, bool deleteMaFile = true)
  255. {
  256. ManifestEntry entry = (from e in this.Entries where e.SteamID == account.Session.SteamID select e).FirstOrDefault();
  257. if (entry == null) return true; // If something never existed, did you do what they asked?
  258. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  259. string filename = maDir + entry.Filename;
  260. this.Entries.Remove(entry);
  261. if (this.Entries.Count == 0)
  262. {
  263. this.Encrypted = false;
  264. }
  265. if (this.Save() && deleteMaFile)
  266. {
  267. try
  268. {
  269. File.Delete(filename);
  270. return true;
  271. }
  272. catch (Exception)
  273. {
  274. return false;
  275. }
  276. }
  277. return false;
  278. }
  279. public bool SaveAccount(SteamGuardAccount account, bool encrypt, string passKey = null)
  280. {
  281. if (encrypt && String.IsNullOrEmpty(passKey)) return false;
  282. if (!encrypt && this.Encrypted) return false;
  283. string salt = null;
  284. string iV = null;
  285. string jsonAccount = JsonConvert.SerializeObject(account);
  286. if (encrypt)
  287. {
  288. salt = FileEncryptor.GetRandomSalt();
  289. iV = FileEncryptor.GetInitializationVector();
  290. string encrypted = FileEncryptor.EncryptData(passKey, salt, iV, jsonAccount);
  291. if (encrypted == null) return false;
  292. jsonAccount = encrypted;
  293. }
  294. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  295. string filename = account.Session.SteamID.ToString() + ".maFile";
  296. ManifestEntry newEntry = new ManifestEntry()
  297. {
  298. SteamID = account.Session.SteamID,
  299. IV = iV,
  300. Salt = salt,
  301. Filename = filename
  302. };
  303. bool foundExistingEntry = false;
  304. for (int i = 0; i < this.Entries.Count; i++)
  305. {
  306. if (this.Entries[i].SteamID == account.Session.SteamID)
  307. {
  308. this.Entries[i] = newEntry;
  309. foundExistingEntry = true;
  310. break;
  311. }
  312. }
  313. if (!foundExistingEntry)
  314. {
  315. this.Entries.Add(newEntry);
  316. }
  317. bool wasEncrypted = this.Encrypted;
  318. this.Encrypted = encrypt || this.Encrypted;
  319. if (!this.Save())
  320. {
  321. this.Encrypted = wasEncrypted;
  322. return false;
  323. }
  324. try
  325. {
  326. File.WriteAllText(maDir + filename, jsonAccount);
  327. return true;
  328. }
  329. catch (Exception)
  330. {
  331. return false;
  332. }
  333. }
  334. public bool Save()
  335. {
  336. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  337. string filename = maDir + "manifest.json";
  338. if (!Directory.Exists(maDir))
  339. {
  340. try
  341. {
  342. Directory.CreateDirectory(maDir);
  343. }
  344. catch (Exception)
  345. {
  346. return false;
  347. }
  348. }
  349. try
  350. {
  351. string contents = JsonConvert.SerializeObject(this);
  352. File.WriteAllText(filename, contents);
  353. return true;
  354. }
  355. catch (Exception)
  356. {
  357. return false;
  358. }
  359. }
  360. private void RecomputeExistingEntries()
  361. {
  362. List<ManifestEntry> newEntries = new List<ManifestEntry>();
  363. string maDir = Manifest.GetExecutableDir() + "/maFiles/";
  364. foreach (var entry in this.Entries)
  365. {
  366. string filename = maDir + entry.Filename;
  367. if (File.Exists(filename))
  368. {
  369. newEntries.Add(entry);
  370. }
  371. }
  372. this.Entries = newEntries;
  373. if (this.Entries.Count == 0)
  374. {
  375. this.Encrypted = false;
  376. }
  377. }
  378. public void MoveEntry(int from, int to)
  379. {
  380. if (from < 0 || to < 0 || from > Entries.Count || to > Entries.Count - 1) return;
  381. ManifestEntry sel = Entries[from];
  382. Entries.RemoveAt(from);
  383. Entries.Insert(to, sel);
  384. Save();
  385. }
  386. public class ManifestEntry
  387. {
  388. [JsonProperty("encryption_iv")]
  389. public string IV { get; set; }
  390. [JsonProperty("encryption_salt")]
  391. public string Salt { get; set; }
  392. [JsonProperty("filename")]
  393. public string Filename { get; set; }
  394. [JsonProperty("steamid")]
  395. public ulong SteamID { get; set; }
  396. }
  397. }
  398. }