Skip to content

Commit 79ac4c5

Browse files
committed
Make encryption of Wi-Fi passwords optional
1 parent 00e7e84 commit 79ac4c5

5 files changed

Lines changed: 106 additions & 65 deletions

File tree

src/MainForm.Designer.cs

Lines changed: 33 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MainForm.cs

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,50 @@ private void UpdateGuiState()
6161
|| passwordTextBox.Text.Length == 0
6262
|| passwordTextBox2.Text.Length == 0);
6363

64-
startButton.Enabled = !isEmpty;
64+
applyButton.Enabled = !isEmpty;
65+
}
66+
67+
private bool ValidateSettings(string bootDrive)
68+
{
69+
if (!Directory.Exists(bootDrive))
70+
{
71+
MessageBox.Show("The drive you have selected is no longer accessible.", "Error",
72+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
73+
return false;
74+
}
75+
76+
if (!File.Exists(Path.Combine(bootDrive, "kernel.img")))
77+
{
78+
MessageBox.Show("The drive you have selected is not a Raspbian SD card.", "Error",
79+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
80+
return false;
81+
}
82+
83+
if (passwordTextBox.Text != passwordTextBox2.Text)
84+
{
85+
MessageBox.Show("The passwords you have entered do not match.", "Error",
86+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
87+
return false;
88+
}
89+
90+
if (typeComboBox.SelectedIndex == 0 && (passwordTextBox.Text.Length < 8
91+
|| passwordTextBox.Text.Length > 63))
92+
{
93+
MessageBox.Show("Password must be between 8 and 63 characters long for WPA " +
94+
"Personal network.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
95+
return false;
96+
}
97+
98+
if (typeComboBox.SelectedIndex == 1 && passwordTextBox.Text.Length > 14
99+
&& encryptCheckBox.Checked)
100+
{
101+
MessageBox.Show("Password cannot exceed 14 characters in length for WPA " +
102+
"Enterprise network when encryption is enabled.", "Error",
103+
MessageBoxButtons.OK, MessageBoxIcon.Warning);
104+
return false;
105+
}
106+
107+
return true;
65108
}
66109

67110
private void ConfigureImage(string bootDrive, string networkConfig, bool enableSsh)
@@ -73,7 +116,7 @@ private void ConfigureImage(string bootDrive, string networkConfig, bool enableS
73116
{
74117
DialogResult result = MessageBox.Show("The file /boot/wpa_supplicant.conf " +
75118
"already exists on the SD card. Are you sure you want to overwrite it?",
76-
"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
119+
"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
77120
writeConfig = (result == DialogResult.Yes);
78121
}
79122

@@ -102,62 +145,33 @@ private void refreshButton_Click(object sender, EventArgs e)
102145
UpdateDriveList();
103146
}
104147

105-
private void startButton_Click(object sender, EventArgs e)
148+
private void applyButton_Click(object sender, EventArgs e)
106149
{
107150
string bootDrive = driveComboBox.GetItemText(
108151
driveComboBox.SelectedItem).Split(' ')[0].TrimEnd('\\');
109-
110-
if (!Directory.Exists(bootDrive))
111-
{
112-
MessageBox.Show("The drive you have selected is no longer accessible.", "Error",
113-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
114-
return;
115-
}
116-
117-
if (!File.Exists(Path.Combine(bootDrive, "kernel.img")))
118-
{
119-
MessageBox.Show("The drive you have selected is not a Raspbian SD card.", "Error",
120-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
121-
return;
122-
}
123-
124-
if (passwordTextBox.Text != passwordTextBox2.Text)
152+
153+
if (!ValidateSettings(bootDrive))
125154
{
126-
MessageBox.Show("The passwords you have entered do not match.", "Error",
127-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
128-
return;
129-
}
130-
131-
if (typeComboBox.SelectedIndex == 0 && (passwordTextBox.Text.Length < 8
132-
|| passwordTextBox.Text.Length > 63))
133-
{
134-
MessageBox.Show("Password must be between 8 and 63 characters long.", "Error",
135-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
136-
return;
137-
}
138-
else if (typeComboBox.SelectedIndex == 1 && passwordTextBox.Text.Length > 14)
139-
{
140-
MessageBox.Show("Password cannot exceed 14 characters in length.", "Error",
141-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
142155
return;
143156
}
144157

145158
string networkName = nameTextBox.Text;
146159
string username = userTextBox.Text;
147160
string password = passwordTextBox.Text;
161+
bool shouldEncrypt = encryptCheckBox.Checked;
148162
string networkConfig;
149163

150164
if (typeComboBox.SelectedIndex == 0)
151165
{
152-
networkConfig = WpaPersonal.GetConfig(networkName, password);
166+
networkConfig = WpaPersonal.GetConfig(networkName, password, shouldEncrypt);
153167
}
154168
else
155169
{
156-
networkConfig = WpaEnterprise.GetConfig(networkName, username, password);
170+
networkConfig = WpaEnterprise.GetConfig(networkName, username, password,
171+
shouldEncrypt);
157172
}
158173

159174
ConfigureImage(bootDrive, networkConfig, sshCheckBox.Checked);
160-
161175
MessageBox.Show("SD card image has been configured successfully.", "Info");
162176
}
163177

src/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[assembly: AssemblyConfiguration("")]
1111
[assembly: AssemblyCompany("")]
1212
[assembly: AssemblyProduct("PiBootstrapper")]
13-
[assembly: AssemblyCopyright("Copyright © 2018 Timothy Johnson")]
13+
[assembly: AssemblyCopyright("Copyright © 2019 Timothy Johnson")]
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2018.1.1")]
35+
[assembly: AssemblyVersion("2019.1")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

src/WpaEnterprise.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,29 @@ private static string ComputeHash(string password)
5050
return BitConverter.ToString(outBytes).Replace("-", "").ToLower();
5151
}
5252

53-
public static string GetConfig(string networkName, string username, string password)
53+
public static string GetConfig(string networkName, string username, string password,
54+
bool shouldEncrypt)
5455
{
55-
List<string> config = new List<string>()
56+
if (shouldEncrypt)
57+
{
58+
password = "hash:" + ComputeHash(password);
59+
}
60+
61+
string[] config = new string[]
5662
{
5763
"network={",
5864
"\tssid=\"" + networkName + "\"",
5965
"\tscan_ssid=1",
6066
"\tkey_mgmt=WPA-EAP",
6167
"\teap=PEAP",
6268
"\tidentity=\"" + username + "\"",
63-
"\tpassword=hash:" + ComputeHash(password),
69+
"\tpassword=" + password,
6470
"\tphase1=\"peaplabel=0\"",
6571
"\tphase2=\"auth=MSCHAPV2\"",
6672
"}"
6773
};
6874

69-
return String.Join("\n", config);
75+
return string.Join("\n", config);
7076
}
7177
}
7278
}

src/WpaPersonal.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,24 @@ private static string ComputeHash(string password, string networkName)
3535
return BitConverter.ToString(outBytes).Replace("-", "").ToLower();
3636
}
3737

38-
public static string GetConfig(string networkName, string password)
38+
public static string GetConfig(string networkName, string password, bool shouldEncrypt)
3939
{
40-
List<string> config = new List<string>()
40+
if (shouldEncrypt)
41+
{
42+
password = ComputeHash(password, networkName);
43+
}
44+
45+
string[] config = new string[]
4146
{
4247
"network={",
4348
"\tssid=\"" + networkName + "\"",
4449
"\tscan_ssid=1",
4550
"\tkey_mgmt=WPA-PSK",
46-
"\tpsk=" + ComputeHash(password, networkName),
51+
"\tpsk=" + password,
4752
"}"
4853
};
4954

50-
return String.Join("\n", config);
55+
return string.Join("\n", config);
5156
}
5257
}
5358
}

0 commit comments

Comments
 (0)