Skip to content

Commit 6c309b2

Browse files
committed
feat(esp_wifi): Add support to limit EAP methods
1 parent 0fde8d2 commit 6c309b2

File tree

5 files changed

+125
-11
lines changed

5 files changed

+125
-11
lines changed

components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ typedef enum {
2929
ESP_EAP_TTLS_PHASE2_CHAP /**< CHAP (Challenge Handshake Authentication Protocol) */
3030
} esp_eap_ttls_phase2_types;
3131

32+
/**
33+
* @brief Bitmask of supported EAP authentication methods.
34+
*/
35+
typedef enum {
36+
ESP_EAP_TYPE_NONE = 0, /*!< No EAP method defined */
37+
ESP_EAP_TYPE_TLS = (1 << 0), /*!< EAP-TLS method */
38+
ESP_EAP_TYPE_TTLS = (1 << 1), /*!< EAP-TTLS method */
39+
ESP_EAP_TYPE_PEAP = (1 << 2), /*!< EAP-PEAP method */
40+
ESP_EAP_TYPE_FAST = (1 << 3), /*!< EAP-FAST method */
41+
ESP_EAP_TYPE_ALL = (ESP_EAP_TYPE_TLS | ESP_EAP_TYPE_TTLS | ESP_EAP_TYPE_PEAP | ESP_EAP_TYPE_FAST), /*!< All supported EAP methods */
42+
} esp_eap_method_t;
43+
3244
/**
3345
* @brief Configuration settings for EAP-FAST
3446
* (Extensible Authentication Protocol - Flexible Authentication via Secure Tunneling).
@@ -70,6 +82,8 @@ esp_err_t esp_wifi_sta_enterprise_enable(void);
7082
*
7183
* @note Disabling EAP authentication may cause the device to connect to the Wi-Fi
7284
* network using other available authentication methods, if configured using esp_wifi_set_config().
85+
* @note Calling this will reset all eap configuration set using esp_eap_client_xxx APIs.
86+
* Please call esp_eap_client_XXX APIs again to set new config after calling this function.
7387
*
7488
* @return
7589
* - ESP_OK: EAP authentication disabled successfully.
@@ -344,6 +358,24 @@ void esp_wifi_set_okc_support(bool enable);
344358
*/
345359
esp_err_t esp_eap_client_set_domain_name(const char *domain_name);
346360

361+
/**
362+
* @brief Set one or more EAP (Extensible Authentication Protocol) methods to be used by the EAP client.
363+
*
364+
* This API sets the allowed EAP authentication methods using a bitmask.
365+
* Multiple methods can be specified by OR-ing together values from `esp_eap_method_t`.
366+
*
367+
* @param[in] methods Bitmask of EAP methods to enable.
368+
*
369+
* @return
370+
* - ESP_OK on success
371+
* - ESP_ERR_INVALID_ARG if none of the methods are valid
372+
*
373+
* @note
374+
* If this API is not called, all supported EAP methods will be considered.
375+
* If one or more methods are set using this API, only the specified methods will be considered.
376+
*/
377+
esp_err_t esp_eap_client_set_eap_methods(esp_eap_method_t methods);
378+
347379
#ifdef __cplusplus
348380
}
349381
#endif

components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,8 @@ static esp_err_t esp_client_enable_fn(void *arg)
839839
}
840840
#endif
841841
g_wpa_config_changed = true;
842+
/* Enable opportunistic key caching support */
843+
esp_wifi_set_okc_support(true);
842844
return ESP_OK;
843845
}
844846

@@ -859,9 +861,6 @@ esp_err_t esp_wifi_sta_enterprise_enable(void)
859861
esp_err_t ret;
860862
struct wpa_sm *sm = &gWpaSm;
861863

862-
/* Enable opportunistic key caching support */
863-
esp_wifi_set_okc_support(true);
864-
865864
wpa2_api_lock();
866865

867866
if (wpa2_is_enabled()) {
@@ -887,6 +886,58 @@ esp_err_t esp_wifi_sta_enterprise_enable(void)
887886
return ret;
888887
}
889888

889+
static void eap_globals_reset(void)
890+
{
891+
os_free(g_wpa_anonymous_identity);
892+
g_wpa_anonymous_identity = NULL;
893+
g_wpa_anonymous_identity_len = 0;
894+
895+
os_free(g_wpa_username);
896+
g_wpa_username = NULL;
897+
g_wpa_username_len = 0;
898+
899+
g_wpa_client_cert = NULL;
900+
g_wpa_client_cert_len = 0;
901+
902+
g_wpa_private_key = NULL;
903+
g_wpa_private_key_len = 0;
904+
905+
g_wpa_private_key_passwd = NULL;
906+
g_wpa_private_key_passwd_len = 0;
907+
908+
g_wpa_ca_cert = NULL;
909+
g_wpa_ca_cert_len = 0;
910+
911+
os_free(g_wpa_password);
912+
g_wpa_password = NULL;
913+
g_wpa_password_len = 0;
914+
915+
os_free(g_wpa_new_password);
916+
g_wpa_new_password = NULL;
917+
g_wpa_new_password_len = 0;
918+
919+
g_wpa_ttls_phase2_type = NULL;
920+
os_free(g_wpa_phase1_options);
921+
g_wpa_phase1_options = NULL;
922+
923+
os_free(g_wpa_pac_file);
924+
g_wpa_pac_file = NULL;
925+
g_wpa_pac_file_len = 0;
926+
927+
g_wpa_suiteb_certification = false;
928+
929+
#ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
930+
g_wpa_default_cert_bundle = false;
931+
esp_crt_bundle_attach_fn = NULL;
932+
#endif
933+
934+
#ifndef CONFIG_TLS_INTERNAL_CLIENT
935+
os_free(g_wpa_domain_match);
936+
g_wpa_domain_match = NULL;
937+
#endif
938+
g_eap_method_mask = ESP_EAP_TYPE_ALL;
939+
}
940+
890941
static esp_err_t eap_client_disable_fn(void *param)
891942
{
892943
struct wpa_sm *sm = &gWpaSm;
@@ -897,6 +948,7 @@ static esp_err_t eap_client_disable_fn(void *param)
897948
eap_peer_sm_deinit();
898949
}
899950

951+
eap_globals_reset();
900952
#ifdef EAP_PEER_METHOD
901953
eap_peer_unregister_methods();
902954
#endif
@@ -915,6 +967,7 @@ esp_err_t esp_wifi_sta_enterprise_disable(void)
915967

916968
if (wpa2_is_disabled()) {
917969
wpa_printf(MSG_INFO, "EAP: already disabled");
970+
eap_globals_reset();
918971
wpa2_api_unlock();
919972
return ESP_OK;
920973
}
@@ -1294,3 +1347,14 @@ esp_err_t esp_eap_client_set_domain_name(const char *domain_name)
12941347
return ESP_OK;
12951348
#endif
12961349
}
1350+
1351+
esp_err_t esp_eap_client_set_eap_methods(esp_eap_method_t methods)
1352+
{
1353+
1354+
if ((methods & ~ESP_EAP_TYPE_ALL) != 0) {
1355+
return ESP_ERR_INVALID_ARG;
1356+
}
1357+
1358+
g_eap_method_mask = methods;
1359+
return ESP_OK;
1360+
}

components/wpa_supplicant/src/eap_peer/eap.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#endif
4242

4343
#include "supplicant_opt.h"
44+
#include "esp_eap_client.h"
4445

4546
u8 *g_wpa_anonymous_identity;
4647
int g_wpa_anonymous_identity_len;
@@ -70,6 +71,7 @@ int (*esp_crt_bundle_attach_fn)(void *conf);
7071
#ifndef CONFIG_TLS_INTERNAL_CLIENT
7172
char *g_wpa_domain_match;
7273
#endif
74+
uint32_t g_eap_method_mask = ESP_EAP_TYPE_ALL;
7375

7476
void eap_peer_config_deinit(struct eap_sm *sm);
7577
void eap_peer_blob_deinit(struct eap_sm *sm);
@@ -623,22 +625,30 @@ int eap_peer_config_init(
623625

624626
if (g_wpa_username) {
625627
//set EAP-PEAP
626-
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
627-
config_methods[allowed_method_count++].method = EAP_TYPE_PEAP;
628+
if (g_eap_method_mask & ESP_EAP_TYPE_PEAP) {
629+
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
630+
config_methods[allowed_method_count++].method = EAP_TYPE_PEAP;
631+
}
628632
//set EAP-TTLS
629-
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
630-
config_methods[allowed_method_count++].method = EAP_TYPE_TTLS;
633+
if (g_eap_method_mask & ESP_EAP_TYPE_TTLS) {
634+
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
635+
config_methods[allowed_method_count++].method = EAP_TYPE_TTLS;
636+
}
631637
}
632638
if (g_wpa_private_key) {
633639
//set EAP-TLS
634-
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
635-
config_methods[allowed_method_count++].method = EAP_TYPE_TLS;
640+
if (g_eap_method_mask & ESP_EAP_TYPE_TLS) {
641+
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
642+
config_methods[allowed_method_count++].method = EAP_TYPE_TLS;
643+
}
636644
}
637645
#ifdef EAP_FAST
638646
if (g_wpa_pac_file) {
639647
//set EAP-FAST
640-
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
641-
config_methods[allowed_method_count++].method = EAP_TYPE_FAST;
648+
if (g_eap_method_mask & ESP_EAP_TYPE_FAST) {
649+
config_methods[allowed_method_count].vendor = EAP_VENDOR_IETF;
650+
config_methods[allowed_method_count++].method = EAP_TYPE_FAST;
651+
}
642652
}
643653
#endif
644654
// Terminate the allowed method list

components/wpa_supplicant/src/eap_peer/eap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern int (*esp_crt_bundle_attach_fn)(void *conf);
5252
#ifndef CONFIG_TLS_INTERNAL_CLIENT
5353
extern char *g_wpa_domain_match;
5454
#endif
55+
extern uint32_t g_eap_method_mask;
5556

5657
const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len);
5758
void eap_deinit_prev_method(struct eap_sm *sm, const char *txt);

examples/wifi/wifi_enterprise/main/wifi_enterprise_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ static void event_handler(void* arg, esp_event_base_t event_base,
9696

9797
static void initialise_wifi(void)
9898
{
99+
esp_eap_method_t eap_methods = ESP_EAP_TYPE_ALL;
99100
#ifdef SERVER_CERT_VALIDATION_ENABLED
100101
unsigned int ca_pem_bytes = ca_pem_end - ca_pem_start;
101102
#endif /* SERVER_CERT_VALIDATION_ENABLED */
102103

103104
#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
104105
unsigned int client_crt_bytes = client_crt_end - client_crt_start;
105106
unsigned int client_key_bytes = client_key_end - client_key_start;
107+
eap_methods = ESP_EAP_TYPE_TLS;
106108
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
107109

108110
ESP_ERROR_CHECK(esp_netif_init());
@@ -148,7 +150,11 @@ static void initialise_wifi(void)
148150

149151
#if defined CONFIG_EXAMPLE_EAP_METHOD_TTLS
150152
ESP_ERROR_CHECK(esp_eap_client_set_ttls_phase2_method(TTLS_PHASE2_METHOD) );
153+
eap_methods = ESP_EAP_TYPE_TTLS;
151154
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TTLS */
155+
#if defined (CONFIG_EXAMPLE_EAP_METHOD_PEAP)
156+
eap_methods = ESP_EAP_TYPE_PEAP;
157+
#endif /* CONFIG_EXAMPLE_EAP_METHOD_PEAP */
152158

153159
#if defined (CONFIG_EXAMPLE_WPA3_192BIT_ENTERPRISE)
154160
ESP_LOGI(TAG, "Enabling 192 bit certification");
@@ -160,6 +166,7 @@ static void initialise_wifi(void)
160166
#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT_DOMAIN
161167
ESP_ERROR_CHECK(esp_eap_client_set_domain_name(EXAMPLE_SERVER_CERT_DOMAIN));
162168
#endif
169+
ESP_ERROR_CHECK(esp_eap_client_set_eap_methods(eap_methods));
163170
ESP_ERROR_CHECK(esp_wifi_sta_enterprise_enable());
164171
ESP_ERROR_CHECK(esp_wifi_start());
165172
}

0 commit comments

Comments
 (0)