Skip to content

Commit 610fb1d

Browse files
committed
Add WPA2 Enterprise API support
1 parent 7eec2e1 commit 610fb1d

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

src/WiFi.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase, uint8_t cha
156156
return status;
157157
}
158158

159+
void WiFiClass::config(WPA2Enterprise& data)
160+
{
161+
WiFiDrv::wpa2EntSetIdentity(data.identity.c_str());
162+
WiFiDrv::wpa2EntSetUsername(data.username.c_str());
163+
WiFiDrv::wpa2EntSetPassword(data.password.c_str());
164+
165+
if (data.ca_pem) {
166+
WiFiStorage.remove("/fs/ca.pem");
167+
WiFiStorage.write("/fs/ca.pem", 0, (uint8_t*)data.ca_pem, strlen(data.ca_pem));
168+
}
169+
if (data.client_crt) {
170+
WiFiStorage.remove("/fs/client.crt");
171+
WiFiStorage.write("/fs/client.crt", 0, (uint8_t*)data.client_crt, strlen(data.client_crt));
172+
}
173+
if (data.client_key) {
174+
WiFiStorage.remove("/fs/client.key");
175+
WiFiStorage.write("/fs/client.key", 0, (uint8_t*)data.client_key, strlen(data.client_key));
176+
}
177+
WiFiDrv::wpa2EntEnable();
178+
}
179+
159180
void WiFiClass::config(IPAddress local_ip)
160181
{
161182
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);

src/WiFi.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ extern "C" {
3535
#include "WiFiSSLClient.h"
3636
#include "WiFiServer.h"
3737

38+
typedef enum _eap_methods {
39+
EAP_TLS = 0,
40+
EAP_PEAP = 1,
41+
EAP_TTLS = 2,
42+
} eap_method;
43+
44+
class WPA2Enterprise
45+
{
46+
public:
47+
WPA2Enterprise(eap_method method, String identity, String username = "", String password = "",
48+
const char* ca_pem = NULL, const char* client_crt = NULL, const char* client_key = NULL) :
49+
method(method), identity(identity), username(username), password(password),
50+
ca_pem(ca_pem), client_crt(client_crt), client_key(client_key)
51+
{}
52+
WPA2Enterprise(String identity, String username = "", String password = "",
53+
const char* ca_pem = NULL, const char* client_crt = NULL, const char* client_key = NULL) :
54+
method(EAP_TLS), identity(identity), username(username), password(password),
55+
ca_pem(ca_pem), client_crt(client_crt), client_key(client_key)
56+
{}
57+
WPA2Enterprise(String identity, const char* ca_pem = NULL, const char* client_crt = NULL, const char* client_key = NULL) :
58+
method(EAP_TLS), identity(identity), username(""), password(""),
59+
ca_pem(ca_pem), client_crt(client_crt), client_key(client_key)
60+
{}
61+
62+
eap_method method; // TLS: 0, PEAP: 1, TTLS: 2 // looks like it's handled internally
63+
String identity;
64+
String username;
65+
String password;
66+
const char* ca_pem;
67+
const char* client_crt;
68+
const char* client_key;
69+
};
70+
3871
class WiFiClass
3972
{
4073
private:
@@ -80,6 +113,12 @@ class WiFiClass
80113
uint8_t beginAP(const char *ssid, const char* passphrase);
81114
uint8_t beginAP(const char *ssid, const char* passphrase, uint8_t channel);
82115

116+
/* Add WPA2 Enterprise information for next connection
117+
*
118+
* param data: Static ip configuration
119+
*/
120+
void config(WPA2Enterprise& data);
121+
83122
/* Change Ip configuration settings disabling the dhcp client
84123
*
85124
* param local_ip: Static ip configuration

src/utility/wifi_drv.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,4 +1078,114 @@ void WiFiDrv::analogWrite(uint8_t pin, uint8_t value)
10781078
SpiDrv::spiSlaveDeselect();
10791079
}
10801080

1081+
void WiFiDrv::wpa2EntSetIdentity(const char* identity)
1082+
{
1083+
WAIT_FOR_SLAVE_SELECT();
1084+
// Send Command
1085+
SpiDrv::sendCmd(WPA2_ENTERPRISE_SET_IDENTITY, PARAM_NUMS_1);
1086+
SpiDrv::sendParam((uint8_t*)identity, strlen(identity), LAST_PARAM);
1087+
1088+
// pad to multiple of 4
1089+
int commandSize = 5 + strlen(identity);
1090+
while (commandSize % 4) {
1091+
SpiDrv::readChar();
1092+
commandSize++;
1093+
}
1094+
1095+
SpiDrv::spiSlaveDeselect();
1096+
//Wait the reply elaboration
1097+
SpiDrv::waitForSlaveReady();
1098+
SpiDrv::spiSlaveSelect();
1099+
1100+
// Wait for reply
1101+
uint8_t _data = 0;
1102+
uint8_t _dataLen = 0;
1103+
if (!SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_SET_IDENTITY, PARAM_NUMS_1, &_data, &_dataLen))
1104+
{
1105+
WARN("error waitResponse");
1106+
_data = WL_FAILURE;
1107+
}
1108+
SpiDrv::spiSlaveDeselect();
1109+
}
1110+
1111+
void WiFiDrv::wpa2EntSetPassword(const char* password)
1112+
{
1113+
WAIT_FOR_SLAVE_SELECT();
1114+
// Send Command
1115+
SpiDrv::sendCmd(WPA2_ENTERPRISE_SET_PASSWORD, PARAM_NUMS_1);
1116+
SpiDrv::sendParam((uint8_t*)password, strlen(password), LAST_PARAM);
1117+
1118+
// pad to multiple of 4
1119+
int commandSize = 5 + strlen(password);
1120+
while (commandSize % 4) {
1121+
SpiDrv::readChar();
1122+
commandSize++;
1123+
}
1124+
1125+
SpiDrv::spiSlaveDeselect();
1126+
//Wait the reply elaboration
1127+
SpiDrv::waitForSlaveReady();
1128+
SpiDrv::spiSlaveSelect();
1129+
1130+
// Wait for reply
1131+
uint8_t _data = 0;
1132+
uint8_t _dataLen = 0;
1133+
if (!SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_SET_PASSWORD, PARAM_NUMS_1, &_data, &_dataLen))
1134+
{
1135+
WARN("error waitResponse");
1136+
_data = WL_FAILURE;
1137+
}
1138+
SpiDrv::spiSlaveDeselect();
1139+
}
1140+
1141+
void WiFiDrv::wpa2EntSetUsername(const char* username)
1142+
{
1143+
WAIT_FOR_SLAVE_SELECT();
1144+
// Send Command
1145+
SpiDrv::sendCmd(WPA2_ENTERPRISE_SET_USERNAME, PARAM_NUMS_1);
1146+
SpiDrv::sendParam((uint8_t*)username, strlen(username), LAST_PARAM);
1147+
1148+
// pad to multiple of 4
1149+
int commandSize = 5 + strlen(username);
1150+
while (commandSize % 4) {
1151+
SpiDrv::readChar();
1152+
commandSize++;
1153+
}
1154+
1155+
SpiDrv::spiSlaveDeselect();
1156+
//Wait the reply elaboration
1157+
SpiDrv::waitForSlaveReady();
1158+
SpiDrv::spiSlaveSelect();
1159+
1160+
// Wait for reply
1161+
uint8_t _data = 0;
1162+
uint8_t _dataLen = 0;
1163+
if (!SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_SET_USERNAME, PARAM_NUMS_1, &_data, &_dataLen))
1164+
{
1165+
WARN("error waitResponse");
1166+
_data = WL_FAILURE;
1167+
}
1168+
SpiDrv::spiSlaveDeselect();
1169+
}
1170+
1171+
void WiFiDrv::wpa2EntEnable()
1172+
{
1173+
WAIT_FOR_SLAVE_SELECT();
1174+
1175+
// Send Command
1176+
SpiDrv::sendCmd(WPA2_ENTERPRISE_ENABLE, PARAM_NUMS_0);
1177+
1178+
SpiDrv::spiSlaveDeselect();
1179+
//Wait the reply elaboration
1180+
SpiDrv::waitForSlaveReady();
1181+
SpiDrv::spiSlaveSelect();
1182+
1183+
// Wait for reply
1184+
uint8_t _data = 1;
1185+
uint8_t _dataLen = 0;
1186+
SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_ENABLE, PARAM_NUMS_1, &_data, &_dataLen);
1187+
1188+
SpiDrv::spiSlaveDeselect();
1189+
}
1190+
10811191
WiFiDrv wiFiDrv;

src/utility/wifi_drv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ class WiFiDrv
286286
static void digitalWrite(uint8_t pin, uint8_t value);
287287
static void analogWrite(uint8_t pin, uint8_t value);
288288

289+
static void wpa2EntSetIdentity(const char* identity);
290+
static void wpa2EntSetUsername(const char* username);
291+
static void wpa2EntSetPassword(const char* password);
292+
static void wpa2EntEnable();
293+
289294
friend class WiFiUDP;
290295
friend class WiFiClient;
291296
};

src/utility/wifi_spi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ enum {
9797
GET_DATABUF_TCP_CMD = 0x45,
9898
INSERT_DATABUF_CMD = 0x46,
9999

100+
// wpa2 enterprise commands
101+
WPA2_ENTERPRISE_SET_IDENTITY = 0x4A,
102+
WPA2_ENTERPRISE_SET_USERNAME = 0x4B,
103+
WPA2_ENTERPRISE_SET_PASSWORD = 0x4C,
104+
WPA2_ENTERPRISE_SET_CA_CERT = 0x4D,
105+
WPA2_ENTERPRISE_SET_CERT_KEY = 0x4E,
106+
WPA2_ENTERPRISE_ENABLE = 0x4F,
107+
100108
// regular format commands
101109
SET_PIN_MODE = 0x50,
102110
SET_DIGITAL_WRITE = 0x51,

0 commit comments

Comments
 (0)