aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.WiFi/AuthRequest.cs
blob: 5ef18108506fde80a7d379b82b423664c080f1a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Tango.WiFi.Win32;
using Tango.WiFi.Win32.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tango.WiFi
{
	public class AuthRequest
	{
		private bool _isPasswordRequired, _isUsernameRequired, _isDomainSupported, _isEAPStore;
		private string _password, _username, _domain;
		private WlanAvailableNetwork _network;
		private WlanInterface _interface;

		public AuthRequest(AccessPoint ap)
		{	
			_network	= ap.Network;
			_interface	= ap.Interface;

			_isPasswordRequired = 
				_network.securityEnabled &&
				_network.dot11DefaultCipherAlgorithm != Dot11CipherAlgorithm.None;

			_isEAPStore =
				_network.dot11DefaultAuthAlgorithm == Dot11AuthAlgorithm.RSNA ||
				_network.dot11DefaultAuthAlgorithm == Dot11AuthAlgorithm.WPA;

			_isUsernameRequired = _isEAPStore;
			_isDomainSupported	= _isEAPStore;
		}
		
		public bool IsPasswordRequired	{ get { return _isPasswordRequired; } }
		public bool IsUsernameRequired	{ get { return _isUsernameRequired; } }
		public bool IsDomainSupported	{ get { return _isDomainSupported; } }

		public string Password
		{
			get { return _password; }
			set { _password = value; }
		}

		public string Username
		{
			get { return _username; }
			set { _username = value; }
		}

		public string Domain
		{
			get { return _domain; }
			set { _domain = value; }
		}

		public bool IsPasswordValid
		{
			get
			{
				return PasswordHelper.IsValid(_password, _network.dot11DefaultCipherAlgorithm);
			}
		}
		
		private bool SaveToEAP() 
		{
			if (!_isEAPStore || !IsPasswordValid)
				return false;

			string userXML = EapUserFactory.Generate(_network.dot11DefaultCipherAlgorithm, _username, _password, _domain);
			_interface.SetEAP(_network.profileName, userXML);

			return true;		
		}

		internal bool Process()
		{
			if (!IsPasswordValid)
				return false;
			
			string profileXML = ProfileFactory.Generate(_network, _password);
			_interface.SetProfile(WlanProfileFlags.AllUser, profileXML, true);

			if (_isEAPStore && !SaveToEAP())
				return false;			
			
			return true;
		}
	}

	public static class PasswordHelper
	{
		/// <summary>
		/// Checks if a password is valid for a cipher type.
		/// </summary>
		public static bool IsValid(string password, Dot11CipherAlgorithm cipherAlgorithm)
		{
			switch (cipherAlgorithm)
			{
				case Dot11CipherAlgorithm.None:
					return true;
				case Dot11CipherAlgorithm.WEP: // WEP key is 10, 26 or 40 hex digits long.
					if (string.IsNullOrEmpty(password))
						return false;

					int len = password.Length;

					bool correctLength = len == 10 || len == 26 || len == 40;
					bool onlyHex = new Regex("^[0-9A-F]+$").IsMatch(password);

					return correctLength && onlyHex;
				case Dot11CipherAlgorithm.CCMP: // WPA2-PSK 8 to 63 ASCII characters					
				case Dot11CipherAlgorithm.TKIP: // WPA-PSK 8 to 63 ASCII characters
					if (string.IsNullOrEmpty(password))
						return false;

					return 8 <= password.Length && password.Length <= 63;
				default:
					return true;
			}
		}
	}
}