aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.WiFi/EapUserFactory.cs
blob: d29941200669744bd2fa89ef1fe13bf90f0ba844 (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
using Tango.WiFi.Win32.Interop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Tango.WiFi
{
	internal static class EapUserFactory
	{
		/// <summary>
		/// Generates the EAP user XML
		/// </summary>
		internal static string Generate(Dot11CipherAlgorithm cipher, string username, string password, string domain)
		{
			string profile = string.Empty;
			string template = string.Empty;
			
			switch (cipher)
			{
				case Dot11CipherAlgorithm.CCMP: // WPA-2
				case Dot11CipherAlgorithm.TKIP: // WPA
					template = GetTemplate("PEAP-MS-CHAPv2");

					profile = string.Format(template, username, FixPass(password), domain);
					break;
				default:
					throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
			}

			return profile;
		}

		/// <summary>
		/// Fetches the template for an EAP user
		/// </summary>
		private static string GetTemplate(string name)
		{
			string resourceName = string.Format("Tango.WiFi.EapUserXML.{0}.xml", name);

			using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
			{
				return reader.ReadToEnd();
			}
		}

		private static string FixPass(string pass)
		{
			pass = EncodeToBase64(pass);
			pass = pass.Replace("&", "&#038;");
			pass = pass.Replace("<", "&#060;");
			pass = pass.Replace(">", "&#062;");

			return pass;
		}


		private static string EncodeToBase64(string toEncode)
		{
			byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
			string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
			return returnValue;
		}
	}
}