Skip to content

Commit 42c9010

Browse files
committed
Updates to fix several issues. (1) I was doing cert files wrong and went back to the esp-edf wpa2 example to get this corrected. (2) I updated the WiFiClientEnterprise example to use this new method and to add a bunch of comments and commented-out examples about the three most common scenarios.
1 parent 47fa231 commit 42c9010

File tree

3 files changed

+47
-32
lines changed

3 files changed

+47
-32
lines changed

libraries/WiFi/examples/WiFiClientEnterprise/WiFiClientEnterprise.ino

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#include <WiFi.h> //Wifi library
22
#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
33
#define EAP_IDENTITY "login" //if connecting from another corporation, use [email protected] in Eduroam
4+
#define EAP_USERNAME "login" //oftentimes just a repeat of the identity
45
#define EAP_PASSWORD "password" //your Eduroam password
56
const char* ssid = "eduroam"; // Eduroam SSID
67
const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
78
int counter = 0;
9+
10+
// NOTE: For some systems, various certification keys are required to connect to the wifi system.
11+
// Usually you are provided these by the IT department of your organization when certs are required
12+
// and you can't connect with just an identity and password.
13+
// Most eduroam setups we have seen do not require this level of authentication, but you should contact
14+
// your IT department to verify.
15+
// You should uncomment these and populate with the contents of the files if this is required for your scenario (See Example 2 and Example 3 below).
16+
//const char *ca_pem = "insert your CA cert from your .pem file here";
17+
//const char *client_cert = "insert your client cert from your .crt file here";
18+
//const char *client_key = "insert your client key from your .key file here";
19+
820
void setup() {
921
Serial.begin(115200);
1022
delay(10);
@@ -13,11 +25,17 @@ void setup() {
1325
Serial.println(ssid);
1426
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
1527
WiFi.mode(WIFI_STA); //init wifi mode
16-
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
17-
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
18-
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
19-
esp_wifi_sta_wpa2_ent_enable();
20-
WiFi.begin(ssid); //connect to wifi
28+
29+
// Example1 (most common): a cert-file-free eduroam with PEAP (or TTLS)
30+
WiFi.begin(ssid, WPA2_AUTH_PEAP, EAP_IDENTITY, EAP_USERNAME, EAP_PASSWORD);
31+
32+
// Example 2: a cert-file WPA2 Enterprise with PEAP
33+
//WiFi.begin(ssid, WPA2_AUTH_PEAP, EAP_IDENTITY, EAP_USERNAME, EAP_PASSWORD, ca_pem, client_cert, client_key);
34+
35+
// Example 3: TLS with cert-files and no password
36+
//WiFi.begin(ssid, WPA2_AUTH_TLS, EAP_IDENTITY, NULL, NULL, ca_pem, client_cert, client_key);
37+
38+
2139
while (WiFi.status() != WL_CONNECTED) {
2240
delay(500);
2341
Serial.print(".");

libraries/WiFi/src/WiFiSTA.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "WiFi.h"
2626
#include "WiFiGeneric.h"
2727
#include "WiFiSTA.h"
28-
#include <WiFiClientSecure.h>
2928

3029
extern "C" {
3130
#include <stdint.h>
@@ -147,25 +146,23 @@ wl_status_t WiFiSTAClass::status()
147146
return (wl_status_t)xEventGroupClearBits(_sta_status_group, 0);
148147
}
149148

150-
151-
static WiFiClientSecure client_secure;
152-
153149
/**
154150
* Start Wifi connection with a WPA2 Enterprise AP
155151
* if passphrase is set the most secure supported mode will be automatically selected
156152
* @param ssid const char* Pointer to the SSID string.
153+
* @param method wpa2_method_t The authentication method of WPA2 (WPA2_AUTH_TLS, WPA2_AUTH_PEAP, WPA2_AUTH_TTLS)
157154
* @param wpa2_identity const char* Pointer to the entity
158155
* @param wpa2_username const char* Pointer to the username
159-
* @param password const char * Pinter to the password.
160-
* @param root_ca const char* Optional. Pointer to the root certificate string.
161-
* @param client_cert const char* Optional. Pointer to the client certificate string.
162-
* @param client_key const char* Optional. Pointer to the client key.
156+
* @param password const char * Pointer to the password.
157+
* @param ca_pem const char* Pointer to a string with the contents of a .pem file with CA cert
158+
* @param client_crt const char* Pointer to a string with the contents of a .crt file with client cert
159+
* @param client_key const char* Pointer to a string with the contants of a .key file with client key
163160
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
164161
* @param channel Optional. Channel of AP
165162
* @param connect Optional. call connect
166163
* @return
167164
*/
168-
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)
165+
wl_status_t WiFiSTAClass::begin(const char* wpa2_ssid, wpa2_auth_method_t method, const char* wpa2_identity, const char* wpa2_username, const char *wpa2_password, const char* ca_pem, const char* client_crt, const char* client_key, int32_t channel, const uint8_t* bssid, bool connect)
169166
{
170167
if(!WiFi.enableSTA(true)) {
171168
log_e("STA enable failed!");
@@ -191,27 +188,22 @@ wl_status_t WiFiSTAClass::begin(const char* wpa2_ssid, const char* wpa2_identity
191188
log_e("password too long!");
192189
}
193190

194-
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)wpa2_identity, strlen(wpa2_identity));
195-
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)wpa2_username, strlen(wpa2_username));
196-
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)wpa2_password, strlen(wpa2_password));
197-
esp_wifi_sta_wpa2_ent_enable(); //set config settings to enable function
198-
WiFi.begin(wpa2_ssid); //connect to wifi
199-
200-
int cert_count = (root_ca != NULL) + (client_cert != NULL) + (client_key != NULL);
201-
if ( cert_count > 1 ) {
202-
log_e("only one cert method allowed!");
203-
return WL_CONNECT_FAILED;
191+
if(ca_pem) {
192+
esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *)ca_pem, strlen(ca_pem));
204193
}
205194

206-
if (root_ca != NULL) {
207-
client_secure.setCACert(root_ca);
195+
if(client_crt) {
196+
esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *)client_crt, strlen(client_crt), (uint8_t *)client_key, strlen(client_key), NULL, 0);
208197
}
209-
else if (client_cert != NULL) {
210-
client_secure.setCertificate(client_cert);
211-
}
212-
else if (client_key != NULL) {
213-
client_secure.setPrivateKey(client_key);
198+
199+
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)wpa2_identity, strlen(wpa2_identity));
200+
if(method == WPA2_AUTH_PEAP || method == WPA2_AUTH_TTLS) {
201+
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)wpa2_username, strlen(wpa2_username));
202+
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)wpa2_password, strlen(wpa2_password));
214203
}
204+
esp_wifi_sta_wpa2_ent_enable(); //set config settings to enable function
205+
WiFi.begin(wpa2_ssid); //connect to wifi
206+
215207
return status();
216208
}
217209

libraries/WiFi/src/WiFiSTA.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#include "esp_event.h"
3131
#endif
3232

33+
typedef enum {
34+
WPA2_AUTH_TLS = 0,
35+
WPA2_AUTH_PEAP = 1,
36+
WPA2_AUTH_TTLS = 2
37+
} wpa2_auth_method_t;
3338

3439
class WiFiSTAClass
3540
{
@@ -39,7 +44,7 @@ class WiFiSTAClass
3944

4045
public:
4146

42-
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);
47+
wl_status_t begin(const char* wpa2_ssid, wpa2_auth_method_t method, const char* wpa2_identity=NULL, const char* wpa2_username=NULL, const char *wpa2_password=NULL, const char* ca_pem=NULL, const char* client_crt=NULL, const char* client_key=NULL, int32_t channel=0, const uint8_t* bssid=0, bool connect=true);
4348
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4449
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
4550
wl_status_t begin();

0 commit comments

Comments
 (0)