Skip to content

Commit 6b5ac56

Browse files
committed
Merge branch 'feat/eap_domain_check' into 'master'
esp_wifi: Add EAP domain validation support Closes IDFGH-14822 See merge request espressif/esp-idf!37668
2 parents 190a129 + 7f2814a commit 6b5ac56

File tree

11 files changed

+162
-15
lines changed

11 files changed

+162
-15
lines changed

components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ esp_err_t esp_eap_client_use_default_cert_bundle(bool use_default_bundle);
326326
*/
327327
void esp_wifi_set_okc_support(bool enable);
328328

329+
/**
330+
* @brief Set the domain name for certificate validation
331+
*
332+
* This function sets the expected domain name for validating the certificate's subject name.
333+
* If the provided domain name does not match the certificate's subject name, validation will fail.
334+
*
335+
* @attention 1. The `domain_name` should be a NULL-terminated string.
336+
*
337+
* @param[in] domain_name The expected domain name. Pass `NULL` to clear the domain matching.
338+
*
339+
* @return
340+
* - ESP_OK: The domain match was set successfully.
341+
* - ESP_ERR_INVALID_ARG: Invalid argument (length > 255).
342+
* - ESP_ERR_NO_MEM: Memory allocation failure.
343+
* - ESP_ERR_NOT_SUPPORTED: Feature not supported.
344+
*/
345+
esp_err_t esp_eap_client_set_domain_name(const char *domain_name);
346+
329347
#ifdef __cplusplus
330348
}
331349
#endif

components/wpa_supplicant/esp_supplicant/src/crypto/tls_mbedtls.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -520,18 +520,24 @@ static int set_client_config(const struct tls_connection_params *cfg, tls_contex
520520
}
521521
}
522522

523-
/* Usages of default ciphersuites can take a lot of time on low end device
524-
* and can cause watchdog. Enabling the ciphers which are secured enough
525-
* but doesn't take that much processing power */
523+
/* The use of default ciphersuites may take a lot of time on low-end devices
524+
* and may trigger the watchdog timer. Enable ciphers that are secure enough
525+
* but require less processing power. */
526526
tls_set_ciphersuite(cfg, tls);
527527

528528
#ifdef CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK
529529
mbedtls_ssl_set_verify(&tls->ssl, tls_disable_key_usages, NULL);
530530
#endif /*CONFIG_ESP_WIFI_DISABLE_KEY_USAGE_CHECK*/
531+
ret = mbedtls_ssl_set_hostname(&tls->ssl, cfg->domain_match);
532+
if (ret != 0) {
533+
wpa_printf(MSG_ERROR, "Failed to set hostname");
534+
return ret;
535+
}
531536

532537
#ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
533538
if (cfg->flags & TLS_CONN_USE_DEFAULT_CERT_BUNDLE) {
534539
wpa_printf(MSG_INFO, "Using default cert bundle");
540+
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
535541
if (esp_crt_bundle_attach_fn) {
536542
ret = (*esp_crt_bundle_attach_fn)(&tls->conf);
537543
}

components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "esp_wpas_glue.h"
3939
#include "esp_eap_client_i.h"
4040
#include "esp_eap_client.h"
41+
#include "eloop.h"
4142

4243
#define WPA2_VERSION "v2.0"
4344

@@ -63,6 +64,7 @@ static void eap_peer_sm_deinit(void);
6364
static int eap_sm_rx_eapol_internal(u8 *src_addr, u8 *buf, u32 len, uint8_t *bssid);
6465
static int wpa2_start_eapol_internal(void);
6566
int wpa2_post(uint32_t sig, uint32_t par);
67+
extern bool g_wpa_config_changed;
6668

6769
#ifdef USE_WPA2_TASK
6870
#define WPA2_TASK_PRIORITY 7
@@ -73,6 +75,11 @@ static void *s_wpa2_api_lock = NULL;
7375
static void *s_wifi_wpa2_sync_sem = NULL;
7476
static bool s_disable_time_check = true;
7577

78+
static void config_changed_handler(void *ctx, void *data)
79+
{
80+
g_wpa_config_changed = true;
81+
}
82+
7683
static void wpa2_api_lock(void)
7784
{
7885
if (s_wpa2_api_lock == NULL) {
@@ -812,6 +819,7 @@ static esp_err_t esp_client_enable_fn(void *arg)
812819
wpa_printf(MSG_ERROR, "Register EAP Peer methods Failure");
813820
}
814821
#endif
822+
g_wpa_config_changed = true;
815823
return ESP_OK;
816824
}
817825

@@ -875,6 +883,7 @@ static esp_err_t eap_client_disable_fn(void *param)
875883
#endif
876884

877885
sm->wpa_sm_eap_disable = NULL;
886+
g_wpa_config_changed = true;
878887
return ESP_OK;
879888
}
880889

@@ -922,6 +931,7 @@ esp_err_t esp_eap_client_set_certificate_and_key(const unsigned char *client_cer
922931
g_wpa_private_key_passwd = private_key_passwd;
923932
g_wpa_private_key_passwd_len = private_key_passwd_len;
924933
}
934+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
925935

926936
return ESP_OK;
927937
}
@@ -937,6 +947,7 @@ void esp_eap_client_clear_certificate_and_key(void)
937947
os_free(g_wpa_pac_file);
938948
g_wpa_pac_file = NULL;
939949
g_wpa_pac_file_len = 0;
950+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
940951
}
941952

942953
esp_err_t esp_eap_client_set_ca_cert(const unsigned char *ca_cert, int ca_cert_len)
@@ -946,13 +957,17 @@ esp_err_t esp_eap_client_set_ca_cert(const unsigned char *ca_cert, int ca_cert_l
946957
g_wpa_ca_cert_len = ca_cert_len;
947958
}
948959

960+
/* CA certs Set/updated, flushing current PMK cache */
961+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
962+
949963
return ESP_OK;
950964
}
951965

952966
void esp_eap_client_clear_ca_cert(void)
953967
{
954968
g_wpa_ca_cert = NULL;
955969
g_wpa_ca_cert_len = 0;
970+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
956971
}
957972

958973
#define ANONYMOUS_ID_LEN_MAX 128
@@ -969,23 +984,27 @@ esp_err_t esp_eap_client_set_identity(const unsigned char *identity, int len)
969984

970985
g_wpa_anonymous_identity = (u8 *)os_zalloc(len);
971986
if (g_wpa_anonymous_identity == NULL) {
987+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
972988
return ESP_ERR_NO_MEM;
973989
}
974990

975991
os_memcpy(g_wpa_anonymous_identity, identity, len);
976992
g_wpa_anonymous_identity_len = len;
993+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
977994

978995
return ESP_OK;
979996
}
980997

981998
void esp_eap_client_clear_identity(void)
982999
{
983-
if (g_wpa_anonymous_identity) {
984-
os_free(g_wpa_anonymous_identity);
1000+
if (!g_wpa_anonymous_identity) {
1001+
return;
9851002
}
9861003

1004+
os_free(g_wpa_anonymous_identity);
9871005
g_wpa_anonymous_identity = NULL;
9881006
g_wpa_anonymous_identity_len = 0;
1007+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
9891008
}
9901009

9911010
#define USERNAME_LEN_MAX 128
@@ -1002,11 +1021,13 @@ esp_err_t esp_eap_client_set_username(const unsigned char *username, int len)
10021021

10031022
g_wpa_username = (u8 *)os_zalloc(len);
10041023
if (g_wpa_username == NULL) {
1024+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10051025
return ESP_ERR_NO_MEM;
10061026
}
10071027

10081028
os_memcpy(g_wpa_username, username, len);
10091029
g_wpa_username_len = len;
1030+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10101031

10111032
return ESP_OK;
10121033
}
@@ -1019,6 +1040,7 @@ void esp_eap_client_clear_username(void)
10191040

10201041
g_wpa_username = NULL;
10211042
g_wpa_username_len = 0;
1043+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10221044
}
10231045

10241046
esp_err_t esp_eap_client_set_password(const unsigned char *password, int len)
@@ -1034,11 +1056,13 @@ esp_err_t esp_eap_client_set_password(const unsigned char *password, int len)
10341056

10351057
g_wpa_password = (u8 *)os_zalloc(len);
10361058
if (g_wpa_password == NULL) {
1059+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10371060
return ESP_ERR_NO_MEM;
10381061
}
10391062

10401063
os_memcpy(g_wpa_password, password, len);
10411064
g_wpa_password_len = len;
1065+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10421066

10431067
return ESP_OK;
10441068
}
@@ -1050,6 +1074,7 @@ void esp_eap_client_clear_password(void)
10501074
}
10511075
g_wpa_password = NULL;
10521076
g_wpa_password_len = 0;
1077+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10531078
}
10541079

10551080
esp_err_t esp_eap_client_set_new_password(const unsigned char *new_password, int len)
@@ -1065,11 +1090,13 @@ esp_err_t esp_eap_client_set_new_password(const unsigned char *new_password, int
10651090

10661091
g_wpa_new_password = (u8 *)os_zalloc(len);
10671092
if (g_wpa_new_password == NULL) {
1093+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10681094
return ESP_ERR_NO_MEM;
10691095
}
10701096

10711097
os_memcpy(g_wpa_new_password, new_password, len);
10721098
g_wpa_password_len = len;
1099+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10731100

10741101
return ESP_OK;
10751102
}
@@ -1081,11 +1108,13 @@ void esp_eap_client_clear_new_password(void)
10811108
}
10821109
g_wpa_new_password = NULL;
10831110
g_wpa_new_password_len = 0;
1111+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10841112
}
10851113

10861114
esp_err_t esp_eap_client_set_disable_time_check(bool disable)
10871115
{
10881116
s_disable_time_check = disable;
1117+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
10891118
return ESP_OK;
10901119
}
10911120

@@ -1122,13 +1151,15 @@ esp_err_t esp_eap_client_set_ttls_phase2_method(esp_eap_ttls_phase2_types type)
11221151
g_wpa_ttls_phase2_type = "auth=MSCHAPV2";
11231152
break;
11241153
}
1154+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11251155
return ESP_OK;
11261156
}
11271157

11281158
esp_err_t esp_eap_client_set_suiteb_192bit_certification(bool enable)
11291159
{
11301160
#ifdef CONFIG_SUITEB192
11311161
g_wpa_suiteb_certification = enable;
1162+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11321163
return ESP_OK;
11331164
#else
11341165
return ESP_FAIL;
@@ -1147,6 +1178,7 @@ esp_err_t esp_eap_client_set_pac_file(const unsigned char *pac_file, int pac_fil
11471178
} else { // The file contains pac data
11481179
g_wpa_pac_file = (u8 *)os_zalloc(pac_file_len);
11491180
if (g_wpa_pac_file == NULL) {
1181+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11501182
return ESP_ERR_NO_MEM;
11511183
}
11521184
os_memcpy(g_wpa_pac_file, pac_file, pac_file_len);
@@ -1155,6 +1187,7 @@ esp_err_t esp_eap_client_set_pac_file(const unsigned char *pac_file, int pac_fil
11551187
} else {
11561188
return ESP_FAIL;
11571189
}
1190+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11581191

11591192
return ESP_OK;
11601193
}
@@ -1184,9 +1217,11 @@ esp_err_t esp_eap_client_set_fast_params(esp_eap_fast_config config)
11841217
}
11851218
g_wpa_phase1_options = (char *)os_zalloc(sizeof(config_for_supplicant));
11861219
if (g_wpa_phase1_options == NULL) {
1220+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11871221
return ESP_ERR_NO_MEM;
11881222
}
11891223
os_memcpy(g_wpa_phase1_options, &config_for_supplicant, sizeof(config_for_supplicant));
1224+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
11901225
return ESP_OK;
11911226

11921227
}
@@ -1200,8 +1235,43 @@ esp_err_t esp_eap_client_use_default_cert_bundle(bool use_default_bundle)
12001235
} else {
12011236
esp_crt_bundle_attach_fn = NULL;
12021237
}
1238+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
12031239
return ESP_OK;
12041240
#else
12051241
return ESP_FAIL;
12061242
#endif
12071243
}
1244+
1245+
#define MAX_DOMAIN_MATCH_LEN 255 /* Maximum host name defined in RFC 1035 */
1246+
esp_err_t esp_eap_client_set_domain_name(const char *domain_name)
1247+
{
1248+
#ifdef CONFIG_TLS_INTERNAL_CLIENT
1249+
return ESP_ERR_NOT_SUPPORTED;
1250+
#else
1251+
int len = domain_name ? os_strnlen(domain_name, MAX_DOMAIN_MATCH_LEN + 1) : 0;
1252+
if (len > MAX_DOMAIN_MATCH_LEN) {
1253+
return ESP_ERR_INVALID_ARG;
1254+
}
1255+
if (g_wpa_domain_match && domain_name && os_strcmp(g_wpa_domain_match, domain_name) == 0) {
1256+
return ESP_OK;
1257+
}
1258+
if (g_wpa_domain_match) {
1259+
os_free(g_wpa_domain_match);
1260+
g_wpa_domain_match = NULL;
1261+
}
1262+
1263+
if (!domain_name) {
1264+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
1265+
return ESP_OK;
1266+
}
1267+
g_wpa_domain_match = os_strdup(domain_name);
1268+
if (!g_wpa_domain_match) {
1269+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
1270+
return ESP_ERR_NO_MEM;
1271+
}
1272+
1273+
eloop_register_timeout(0, 0, config_changed_handler, NULL, NULL);
1274+
1275+
return ESP_OK;
1276+
#endif
1277+
}

components/wpa_supplicant/esp_supplicant/src/esp_wpa_main.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -50,6 +50,7 @@
5050
bool g_wpa_pmk_caching_disabled = 0;
5151
const wifi_osi_funcs_t *wifi_funcs;
5252
struct wpa_funcs *wpa_cb;
53+
bool g_wpa_config_changed;
5354

5455
void wpa_install_key(enum wpa_alg alg, u8 *addr, int key_idx, int set_tx,
5556
u8 *seq, size_t seq_len, u8 *key, size_t key_len, enum key_flag key_flag)
@@ -226,11 +227,22 @@ int dpp_connect(uint8_t *bssid, bool pdr_done)
226227
}
227228
#endif
228229

230+
static void wpa_config_reload(void)
231+
{
232+
struct wpa_sm *sm = &gWpaSm;
233+
wpa_sm_pmksa_cache_flush(sm, NULL);
234+
}
235+
229236
int wpa_sta_connect(uint8_t *bssid)
230237
{
231238
/* use this API to set AP specific IEs during connection */
232239
int ret = 0;
233240
ret = wpa_config_profile(bssid);
241+
242+
if (g_wpa_config_changed) {
243+
wpa_config_reload();
244+
g_wpa_config_changed = false;
245+
}
234246
if (ret == 0) {
235247
ret = wpa_config_bss(bssid);
236248
if (ret) {
@@ -444,12 +456,6 @@ static bool hostap_sta_join(void **sta, u8 *bssid, u8 *wpa_ie, u8 wpa_ie_len, u8
444456
}
445457
#endif
446458

447-
static void wpa_config_reload(void)
448-
{
449-
struct wpa_sm *sm = &gWpaSm;
450-
wpa_sm_pmksa_cache_flush(sm, NULL);
451-
}
452-
453459
int esp_supplicant_init(void)
454460
{
455461
int ret = ESP_OK;

components/wpa_supplicant/port/include/os.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ char * ets_strdup(const char *s);
261261
#ifndef os_strlen
262262
#define os_strlen(s) strlen(s)
263263
#endif
264+
#ifndef os_strnlen
265+
#define os_strnlen(s, n) strnlen((s), (n))
266+
#endif
264267
#ifndef os_strcasecmp
265268
#ifdef _MSC_VER
266269
#define os_strcasecmp(s1, s2) _stricmp((s1), (s2))

0 commit comments

Comments
 (0)