ProfileFactory.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using Vinno.FIS.Sonopost.Wireless.Win32.Interop;
  6. namespace Vinno.FIS.Sonopost.Wireless
  7. {
  8. internal static class ProfileFactory
  9. {
  10. /// <summary>
  11. /// Generates the profile XML for the access point and password
  12. /// </summary>
  13. internal static string Generate(WlanAvailableNetwork network, string password)
  14. {
  15. string profile = string.Empty;
  16. string template = string.Empty;
  17. string name = Encoding.UTF8.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
  18. string hex = GetHexString(network.dot11Ssid.SSID);
  19. var authAlgo = network.dot11DefaultAuthAlgorithm;
  20. switch (network.dot11DefaultCipherAlgorithm)
  21. {
  22. case Dot11CipherAlgorithm.None:
  23. template = GetTemplate("OPEN");
  24. profile = string.Format(template, name, hex);
  25. break;
  26. case Dot11CipherAlgorithm.WEP:
  27. template = GetTemplate("WEP");
  28. profile = string.Format(template, name, hex, password);
  29. break;
  30. case Dot11CipherAlgorithm.CCMP:
  31. if (authAlgo == Dot11AuthAlgorithm.RSNA)
  32. {
  33. template = GetTemplate("WPA2-Enterprise-PEAP-MSCHAPv2");
  34. profile = string.Format(template, name);
  35. }
  36. else // PSK
  37. {
  38. template = GetTemplate("WPA2-PSK");
  39. profile = string.Format(template, name, password);
  40. }
  41. break;
  42. case Dot11CipherAlgorithm.TKIP:
  43. #warning Robin: Not sure WPA uses RSNA
  44. if (authAlgo == Dot11AuthAlgorithm.RSNA)
  45. {
  46. template = GetTemplate("WPA-Enterprise-PEAP-MSCHAPv2");
  47. profile = string.Format(template, name);
  48. }
  49. else // PSK
  50. {
  51. template = GetTemplate("WPA-PSK");
  52. profile = string.Format(template, name, password);
  53. }
  54. break;
  55. default:
  56. throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
  57. }
  58. return profile;
  59. }
  60. /// <summary>
  61. /// Fetches the template for an wireless connection profile.
  62. /// </summary>
  63. private static string GetTemplate(string name)
  64. {
  65. var projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
  66. string resourceName = $"{projectName}.ProfileXML.{name}.xml";
  67. using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
  68. {
  69. return reader.ReadToEnd();
  70. }
  71. }
  72. /// <summary>
  73. /// Converts an byte array into the hex representation, ex: [255, 255] -> "FFFF"
  74. /// </summary>
  75. private static string GetHexString(byte[] ba)
  76. {
  77. StringBuilder sb = new StringBuilder(ba.Length * 2);
  78. foreach (byte b in ba)
  79. {
  80. if (b == 0)
  81. break;
  82. sb.AppendFormat("{0:x2}", b);
  83. }
  84. return sb.ToString();
  85. }
  86. }
  87. }