-
Notifications
You must be signed in to change notification settings - Fork 83
/
EapUserFactory.cs
69 lines (58 loc) · 1.79 KB
/
EapUserFactory.cs
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
using SimpleWifi.Win32.Interop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SimpleWifi
{
internal static class EapUserFactory
{
/// <summary>
/// Generates the EAP user XML
/// </summary>
internal static string Generate(Dot11CipherAlgorithm cipher, string username, string password, string domain)
{
#warning Robin: Probably not properly implemented, only supports WPA- and WPA-2 Enterprise with PEAP-MSCHAPv2
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("SimpleWifi.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("&", "&");
pass = pass.Replace("<", "<");
pass = pass.Replace(">", ">");
return pass;
}
private static string EncodeToBase64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
}
}