Skip to content

Commit cac01d3

Browse files
committed
Added another overloaded WiFiSTAClass::begin() function that provides an easy way of creating a WPA2 Enterprise connection.
1 parent 4da1051 commit cac01d3

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

libraries/WiFi/src/WiFiGeneric.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
#include "esp_smartconfig.h"
3232
#include "wifi_provisioning/manager.h"
3333

34+
#ifdef ENABLE_WPA2_AUTHENTICATION
35+
#include <WiFiClient.h>
36+
#include <WiFiClientSecure.h>
37+
#include "esp_wpa2.h"
38+
#endif
39+
3440
ESP_EVENT_DECLARE_BASE(ARDUINO_EVENTS);
3541

3642
typedef enum {

libraries/WiFi/src/WiFiSTA.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,78 @@ wl_status_t WiFiSTAClass::status()
145145
return (wl_status_t)xEventGroupClearBits(_sta_status_group, 0);
146146
}
147147

148+
149+
#ifdef ENABLE_WPA2_AUTHENTICATION
150+
static WiFiClientSecure client_secure;
151+
152+
/**
153+
* Start Wifi connection with a WPA2 Enterprise AP
154+
* if passphrase is set the most secure supported mode will be automatically selected
155+
* @param ssid const char* Pointer to the SSID string.
156+
* @param wpa2_identity const char* Pointer to the entity
157+
* @param wpa2_username const char* Pointer to the username
158+
* @param password const char * Pinter to the password.
159+
* @param root_ca const char* Optional. Pointer to the root certificate string.
160+
* @param client_cert const char* Optional. Pointer to the client certificate string.
161+
* @param client_key const char* Optional. Pointer to the client key.
162+
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
163+
* @param channel Optional. Channel of AP
164+
* @param connect Optional. call connect
165+
* @return
166+
*/
167+
wl_status_t WiFiSTAClass::begin(const char* wpa2_ssid, const char* wpa2_identity, const char* wpa2_username, const char *wpa2_password, const char* root_ca, const char* client_cert, const char* client_key, int32_t channel, const uint8_t* bssid, bool connect)
168+
{
169+
if(!WiFi.enableSTA(true)) {
170+
log_e("STA enable failed!");
171+
return WL_CONNECT_FAILED;
172+
}
173+
174+
if(!wpa2_ssid || *wpa2_ssid == 0x00 || strlen(wpa2_ssid) > 32) {
175+
log_e("SSID too long or missing!");
176+
return WL_CONNECT_FAILED;
177+
}
178+
179+
if(wpa2_identity && strlen(wpa2_identity) > 64) {
180+
log_e("identity too long!");
181+
return WL_CONNECT_FAILED;
182+
}
183+
184+
if(wpa2_username && strlen(wpa2_username) > 64) {
185+
log_e("username too long!");
186+
return WL_CONNECT_FAILED;
187+
}
188+
189+
if(wpa2_password && strlen(wpa2_password) > 64) {
190+
log_e("password too long!");
191+
}
192+
193+
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)wpa2_identity, strlen(wpa2_identity));
194+
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)wpa2_username, strlen(wpa2_username));
195+
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)wpa2_password, strlen(wpa2_password));
196+
esp_wifi_sta_wpa2_ent_enable(); //set config settings to enable function
197+
WiFi.begin(wpa2_ssid); //connect to wifi
198+
199+
int cert_count = (root_ca != NULL) + (client_cert != NULL) + (client_key != NULL);
200+
if ( cert_count > 1 ) {
201+
log_e("only one cert method allowed!");
202+
return WL_CONNECT_FAILED;
203+
}
204+
205+
if (root_ca != NULL) {
206+
client_secure.setCACert(root_ca);
207+
}
208+
else if (client_cert != NULL) {
209+
client_secure.setCertificate(client_cert);
210+
}
211+
else if (client_key != NULL) {
212+
client_secure.setPrivateKey(client_key);
213+
}
214+
return status();
215+
}
216+
#endif
217+
218+
219+
148220
/**
149221
* Start Wifi connection
150222
* if passphrase is set the most secure supported mode will be automatically selected

libraries/WiFi/src/WiFiSTA.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class WiFiSTAClass
3939

4040
public:
4141

42+
#ifdef ENABLE_WPA2_AUTHENTICATION
43+
wl_status_t begin(const char* wpa2_ssid, const char* wpa2_identity, const char* wpa2_username, const char *wpa2_password, const char* root_ca=NULL, const char* client_cert=NULL, const char* client_key=NULL,int32_t channel=0, const uint8_t* bssid=0, bool connect=true);
44+
#endif
4245
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4346
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4447
wl_status_t begin();

0 commit comments

Comments
 (0)