Skip to content

Commit d8ac9ff

Browse files
committed
WPA2Ent: explicitely set certificates
1 parent 33ff7d3 commit d8ac9ff

File tree

4 files changed

+95
-43
lines changed

4 files changed

+95
-43
lines changed

src/WiFi.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ extern "C" {
2727
#include "utility/debug.h"
2828
}
2929

30+
void WPA2EnterpriseClass::addCACertificate(const char* ca_pem) {
31+
WiFiDrv::wpa2EntSetCACertificate(ca_pem);
32+
}
33+
34+
void WPA2EnterpriseClass::addClientCertificate(const char* client_crt, const char* client_key) {
35+
// TODO: make sure that client_crt is not bigger tahn 4050bytes
36+
WiFiDrv::wpa2EntSetClientCertificate(client_crt, client_key);
37+
}
38+
39+
// singleton
40+
WPA2EnterpriseClass WPA2Enterprise;
41+
3042
WiFiClass::WiFiClass() : _timeout(50000)
3143
{
3244
}
@@ -156,25 +168,13 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase, uint8_t cha
156168
return status;
157169
}
158170

159-
void WiFiClass::config(WPA2Enterprise& data)
171+
int WiFiClass::beginEnterprise(const char *ssid, const char* username, const char* password)
160172
{
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-
}
173+
WiFiDrv::wpa2EntSetIdentity(username);
174+
WiFiDrv::wpa2EntSetUsername(username);
175+
WiFiDrv::wpa2EntSetPassword(password);
177176
WiFiDrv::wpa2EntEnable();
177+
return begin(ssid);
178178
}
179179

180180
void WiFiClass::config(IPAddress local_ip)

src/WiFi.h

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,17 @@ typedef enum _eap_methods {
4141
EAP_TTLS = 2,
4242
} eap_method;
4343

44-
class WPA2Enterprise
44+
class WPA2EnterpriseClass
4545
{
4646
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;
47+
void clear();
48+
void save();
49+
void addCACertificate(const char* ca_pem);
50+
void addClientCertificate(const char* client_crt, const char* client_key);
6951
};
7052

53+
extern WPA2EnterpriseClass WPA2Enterprise;
54+
7155
class WiFiClass
7256
{
7357
private:
@@ -113,11 +97,13 @@ class WiFiClass
11397
uint8_t beginAP(const char *ssid, const char* passphrase);
11498
uint8_t beginAP(const char *ssid, const char* passphrase, uint8_t channel);
11599

116-
/* Add WPA2 Enterprise information for next connection
100+
/* Start Wifi connection with wpa2 enterprise
117101
*
118-
* param data: Static ip configuration
102+
* helper function for most university WPA2 connections
103+
* if a fine-grained configuration is needed (like adding certificates)
104+
* use WPA2Enterprise functions and then call begin() normally
119105
*/
120-
void config(WPA2Enterprise& data);
106+
int beginEnterprise(const char *ssid, const char* username, const char* password);
121107

122108
/* Change Ip configuration settings disabling the dhcp client
123109
*

src/utility/wifi_drv.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,70 @@ void WiFiDrv::wpa2EntSetUsername(const char* username)
11681168
SpiDrv::spiSlaveDeselect();
11691169
}
11701170

1171+
void WiFiDrv::wpa2EntSetClientCertificate(const char* client_crt, const char* client_key)
1172+
{
1173+
WAIT_FOR_SLAVE_SELECT();
1174+
// Send Command
1175+
SpiDrv::sendCmd(WPA2_ENTERPRISE_SET_CERT_KEY, PARAM_NUMS_2);
1176+
SpiDrv::sendParamLen16(strlen(client_crt));
1177+
SpiDrv::sendParamLen16(strlen(client_key));
1178+
SpiDrv::sendParamNoLen((uint8_t*)client_crt, strlen(client_crt), NO_LAST_PARAM);
1179+
SpiDrv::sendParamNoLen((uint8_t*)client_key, strlen(client_key), LAST_PARAM);
1180+
1181+
// pad to multiple of 4
1182+
int commandSize = 5 + strlen(client_crt) + strlen(client_key);
1183+
while (commandSize % 4) {
1184+
SpiDrv::readChar();
1185+
commandSize++;
1186+
}
1187+
1188+
SpiDrv::spiSlaveDeselect();
1189+
//Wait the reply elaboration
1190+
SpiDrv::waitForSlaveReady();
1191+
SpiDrv::spiSlaveSelect();
1192+
1193+
// Wait for reply
1194+
uint8_t _data = 0;
1195+
uint8_t _dataLen = 0;
1196+
if (!SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_SET_CERT_KEY, PARAM_NUMS_1, &_data, &_dataLen))
1197+
{
1198+
WARN("error waitResponse");
1199+
_data = WL_FAILURE;
1200+
}
1201+
SpiDrv::spiSlaveDeselect();
1202+
}
1203+
1204+
void WiFiDrv::wpa2EntSetCACertificate(const char* ca_pem)
1205+
{
1206+
WAIT_FOR_SLAVE_SELECT();
1207+
// Send Command
1208+
SpiDrv::sendCmd(WPA2_ENTERPRISE_SET_CA_CERT, PARAM_NUMS_1);
1209+
SpiDrv::sendParamLen16(strlen(ca_pem));
1210+
SpiDrv::sendParamNoLen((uint8_t*)ca_pem, strlen(ca_pem), LAST_PARAM);
1211+
1212+
// pad to multiple of 4
1213+
int commandSize = 5 + strlen(ca_pem);
1214+
while (commandSize % 4) {
1215+
SpiDrv::readChar();
1216+
commandSize++;
1217+
}
1218+
1219+
SpiDrv::spiSlaveDeselect();
1220+
//Wait the reply elaboration
1221+
SpiDrv::waitForSlaveReady();
1222+
SpiDrv::spiSlaveSelect();
1223+
1224+
// Wait for reply
1225+
uint8_t _data = 0;
1226+
uint8_t _dataLen = 0;
1227+
if (!SpiDrv::waitResponseCmd(WPA2_ENTERPRISE_SET_CA_CERT, PARAM_NUMS_1, &_data, &_dataLen))
1228+
{
1229+
WARN("error waitResponse");
1230+
_data = WL_FAILURE;
1231+
}
1232+
SpiDrv::spiSlaveDeselect();
1233+
}
1234+
11711235
void WiFiDrv::wpa2EntEnable()
11721236
{
11731237
WAIT_FOR_SLAVE_SELECT();

src/utility/wifi_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ class WiFiDrv
289289
static void wpa2EntSetIdentity(const char* identity);
290290
static void wpa2EntSetUsername(const char* username);
291291
static void wpa2EntSetPassword(const char* password);
292+
static void wpa2EntSetCACertificate(const char* ca_pem);
293+
static void wpa2EntSetClientCertificate(const char* client_crt, const char* client_key);
292294
static void wpa2EntEnable();
293295

294296
friend class WiFiUDP;

0 commit comments

Comments
 (0)