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
|
using Tango.WiFi.Win32;
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 ProfileFactory
{
/// <summary>
/// Generates the profile XML for the access point and password
/// </summary>
internal static string Generate(WlanAvailableNetwork network, string password)
{
string profile = string.Empty;
string template = string.Empty;
string name = Encoding.ASCII.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
string hex = GetHexString(network.dot11Ssid.SSID);
var authAlgo = network.dot11DefaultAuthAlgorithm;
switch (network.dot11DefaultCipherAlgorithm)
{
case Dot11CipherAlgorithm.None:
template = GetTemplate("OPEN");
profile = string.Format(template, name, hex);
break;
case Dot11CipherAlgorithm.WEP:
template = GetTemplate("WEP");
profile = string.Format(template, name, hex, password);
break;
case Dot11CipherAlgorithm.CCMP:
if (authAlgo == Dot11AuthAlgorithm.RSNA)
{
template = GetTemplate("WPA2-Enterprise-PEAP-MSCHAPv2");
profile = string.Format(template, name);
}
else // PSK
{
template = GetTemplate("WPA2-PSK");
profile = string.Format(template, name, password);
}
break;
case Dot11CipherAlgorithm.TKIP:
if (authAlgo == Dot11AuthAlgorithm.RSNA)
{
template = GetTemplate("WPA-Enterprise-PEAP-MSCHAPv2");
profile = string.Format(template, name);
}
else // PSK
{
template = GetTemplate("WPA-PSK");
profile = string.Format(template, name, password);
}
break;
default:
throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
}
return profile;
}
/// <summary>
/// Fetches the template for an wireless connection profile.
/// </summary>
private static string GetTemplate(string name)
{
string resourceName = string.Format("Tango.WiFi.ProfileXML.{0}.xml", name);
using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
{
return reader.ReadToEnd();
}
}
/// <summary>
/// Converts an byte array into the hex representation, ex: [255, 255] -> "FFFF"
/// </summary>
private static string GetHexString(byte[] ba)
{
StringBuilder sb = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
if (b == 0)
break;
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
}
}
|