EapUserFactory.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Vinno.FIS.Sonopost.Wireless.Win32.Interop;
  5. namespace Vinno.FIS.Sonopost.Wireless
  6. {
  7. internal static class EapUserFactory
  8. {
  9. /// <summary>
  10. /// Generates the EAP user XML
  11. /// </summary>
  12. internal static string Generate(Dot11CipherAlgorithm cipher, string username, string password, string domain)
  13. {
  14. #warning Robin: Probably not properly implemented, only supports WPA- and WPA-2 Enterprise with PEAP-MSCHAPv2
  15. string profile = string.Empty;
  16. string template = string.Empty;
  17. switch (cipher)
  18. {
  19. case Dot11CipherAlgorithm.CCMP: // WPA-2
  20. case Dot11CipherAlgorithm.TKIP: // WPA
  21. template = GetTemplate("PEAP-MS-CHAPv2");
  22. profile = string.Format(template, username, FixPass(password), domain);
  23. break;
  24. default:
  25. throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
  26. }
  27. return profile;
  28. }
  29. /// <summary>
  30. /// Fetches the template for an EAP user
  31. /// </summary>
  32. private static string GetTemplate(string name)
  33. {
  34. string resourceName = string.Format("SimpleWifi.EapUserXML.{0}.xml", name);
  35. using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
  36. {
  37. return reader.ReadToEnd();
  38. }
  39. }
  40. private static string FixPass(string pass)
  41. {
  42. pass = EncodeToBase64(pass);
  43. pass = pass.Replace("&", "&#038;");
  44. pass = pass.Replace("<", "&#060;");
  45. pass = pass.Replace(">", "&#062;");
  46. return pass;
  47. }
  48. private static string EncodeToBase64(string toEncode)
  49. {
  50. byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
  51. string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
  52. return returnValue;
  53. }
  54. }
  55. }