AuthRequest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Text.RegularExpressions;
  2. using vCloud.Sonopost.Wireless.Win32;
  3. using vCloud.Sonopost.Wireless.Win32.Interop;
  4. namespace vCloud.Sonopost.Wireless
  5. {
  6. public class AuthRequest
  7. {
  8. private bool _isPasswordRequired, _isUsernameRequired, _isDomainSupported, _isEAPStore;
  9. private string _password, _username, _domain;
  10. private WlanAvailableNetwork _network;
  11. private WlanInterface _interface;
  12. public AuthRequest(AccessPoint ap)
  13. {
  14. _network = ap.Network;
  15. _interface = ap.Interface;
  16. _isPasswordRequired =
  17. _network.securityEnabled &&
  18. _network.dot11DefaultCipherAlgorithm != Dot11CipherAlgorithm.None;
  19. _isEAPStore =
  20. _network.dot11DefaultAuthAlgorithm == Dot11AuthAlgorithm.RSNA ||
  21. _network.dot11DefaultAuthAlgorithm == Dot11AuthAlgorithm.WPA;
  22. _isUsernameRequired = _isEAPStore;
  23. _isDomainSupported = _isEAPStore;
  24. }
  25. public bool IsPasswordRequired { get { return _isPasswordRequired; } }
  26. public bool IsUsernameRequired { get { return _isUsernameRequired; } }
  27. public bool IsDomainSupported { get { return _isDomainSupported; } }
  28. public string Password
  29. {
  30. get { return _password; }
  31. set { _password = value; }
  32. }
  33. public string Username
  34. {
  35. get { return _username; }
  36. set { _username = value; }
  37. }
  38. public string Domain
  39. {
  40. get { return _domain; }
  41. set { _domain = value; }
  42. }
  43. public bool IsPasswordValid
  44. {
  45. get
  46. {
  47. #warning Robin: Not sure that Enterprise networks have the same requirements on the password complexity as standard ones.
  48. return PasswordHelper.IsValid(_password, _network.dot11DefaultCipherAlgorithm);
  49. }
  50. }
  51. private bool SaveToEAP()
  52. {
  53. if (!_isEAPStore || !IsPasswordValid)
  54. return false;
  55. string userXML = EapUserFactory.Generate(_network.dot11DefaultCipherAlgorithm, _username, _password, _domain);
  56. _interface.SetEAP(_network.profileName, userXML);
  57. return true;
  58. }
  59. internal bool Process()
  60. {
  61. if (!IsPasswordValid)
  62. return false;
  63. string profileXML = ProfileFactory.Generate(_network, _password);
  64. _interface.SetProfile(WlanProfileFlags.AllUser, profileXML, true);
  65. if (_isEAPStore && !SaveToEAP())
  66. return false;
  67. return true;
  68. }
  69. }
  70. public static class PasswordHelper
  71. {
  72. /// <summary>
  73. /// Checks if a password is valid for a cipher type.
  74. /// </summary>
  75. public static bool IsValid(string password, Dot11CipherAlgorithm cipherAlgorithm)
  76. {
  77. switch (cipherAlgorithm)
  78. {
  79. case Dot11CipherAlgorithm.None:
  80. return true;
  81. case Dot11CipherAlgorithm.WEP: // WEP key is 10, 26 or 40 hex digits long.
  82. if (string.IsNullOrEmpty(password))
  83. return false;
  84. int len = password.Length;
  85. bool correctLength = len == 10 || len == 26 || len == 40;
  86. bool onlyHex = new Regex("^[0-9A-F]+$").IsMatch(password);
  87. return correctLength && onlyHex;
  88. case Dot11CipherAlgorithm.CCMP: // WPA2-PSK 8 to 63 ASCII characters
  89. case Dot11CipherAlgorithm.TKIP: // WPA-PSK 8 to 63 ASCII characters
  90. if (string.IsNullOrEmpty(password))
  91. return false;
  92. return 8 <= password.Length && password.Length <= 63;
  93. default:
  94. return true;
  95. }
  96. }
  97. }
  98. }