Skip to content

Commit bbf7f9b

Browse files
committed
fix(esp_wifi): Code cleanup for PR#15550 PR#15551
Closes #15550 Closes #15551
1 parent b7ed8a5 commit bbf7f9b

File tree

8 files changed

+64
-51
lines changed

8 files changed

+64
-51
lines changed

components/wpa_supplicant/esp_supplicant/include/esp_eap_client.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,28 +327,22 @@ esp_err_t esp_eap_client_use_default_cert_bundle(bool use_default_bundle);
327327
void esp_wifi_set_okc_support(bool enable);
328328

329329
/**
330-
* @brief Set name for certificate domain name validation
330+
* @brief Set the domain name for certificate validation
331331
*
332-
* Enabling this option will only accept certificate with the provided subject name
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.
333334
*
334-
* @param[in] domain_match The expected domain name
335-
* @param[in] len Length of the domain name (limited to 1~127 bytes).
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.
336338
*
337339
* @return
338-
* - ESP_OK: The identity was set successfully.
339-
* - ESP_ERR_INVALID_ARG: Invalid argument (len <= 0 or len >= 128).
340+
* - ESP_OK: The domain match was set successfully.
341+
* - ESP_ERR_INVALID_ARG: Invalid argument (length > 255).
340342
* - ESP_ERR_NO_MEM: Memory allocation failure.
343+
* - ESP_ERR_NOT_SUPPORTED: Feature not supported.
341344
*/
342-
esp_err_t esp_eap_client_set_domain_match(const char *domain_match);
343-
344-
/**
345-
* @brief Clear the domain name for certificate validation
346-
*
347-
* This function clears the domain name that was previously set for the EAP client.
348-
* After calling this function, the EAP client will no longer use the previously
349-
* configured domain name during the authentication process.
350-
*/
351-
void esp_eap_client_clear_domain_match(void);
345+
esp_err_t esp_eap_client_set_domain_name(const char *domain_name);
352346

353347
#ifdef __cplusplus
354348
}

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

Lines changed: 8 additions & 8 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,18 @@ 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-
532-
if (cfg->domain_match) {
533-
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
534-
mbedtls_ssl_set_hostname(&tls->ssl, cfg->domain_match);
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;
535535
}
536536

537537
#ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE

components/wpa_supplicant/esp_supplicant/src/esp_eap_client.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,9 @@ esp_err_t esp_eap_client_set_ca_cert(const unsigned char *ca_cert, int ca_cert_l
946946
g_wpa_ca_cert_len = ca_cert_len;
947947
}
948948

949+
/* CA certs Set/updated, flushing current PMK cache */
950+
wpa_sm_pmksa_cache_flush(get_wpa_sm(), NULL);
951+
949952
return ESP_OK;
950953
}
951954

@@ -1206,32 +1209,36 @@ esp_err_t esp_eap_client_use_default_cert_bundle(bool use_default_bundle)
12061209
#endif
12071210
}
12081211

1209-
#define MAX_DOMAIN_MATCH_LEN 128
1210-
esp_err_t esp_eap_client_set_domain_match(const char *domain_match)
1212+
#define MAX_DOMAIN_MATCH_LEN 255 /* Maximum host name defined in RFC 1035 */
1213+
esp_err_t esp_eap_client_set_domain_name(const char *domain_name)
12111214
{
1215+
#ifdef CONFIG_TLS_INTERNAL_CLIENT
1216+
return ESP_ERR_NOT_SUPPORTED;
1217+
#else
1218+
int len = domain_name ? os_strnlen(domain_name, MAX_DOMAIN_MATCH_LEN + 1) : 0;
1219+
if (len > MAX_DOMAIN_MATCH_LEN) {
1220+
return ESP_ERR_INVALID_ARG;
1221+
}
1222+
if (g_wpa_domain_match && domain_name && os_strcmp(g_wpa_domain_match, domain_name) == 0) {
1223+
return ESP_OK;
1224+
}
12121225
if (g_wpa_domain_match) {
12131226
os_free(g_wpa_domain_match);
12141227
g_wpa_domain_match = NULL;
12151228
}
12161229

1217-
int len = os_strlen(domain_match);
1218-
if (len > MAX_DOMAIN_MATCH_LEN) {
1219-
return ESP_ERR_INVALID_ARG;
1230+
if (!domain_name) {
1231+
return ESP_OK;
12201232
}
1221-
g_wpa_domain_match = (char *)os_zalloc(len+1);
1222-
if (g_wpa_domain_match == NULL) {
1233+
g_wpa_domain_match = os_strdup(domain_name);
1234+
if (!g_wpa_domain_match) {
12231235
return ESP_ERR_NO_MEM;
12241236
}
12251237

1226-
os_strlcpy(g_wpa_domain_match, domain_match, len+1);
1238+
/* flushing the PMK only needed when going for a better security ie no-domain name to domain name
1239+
* or changing the domain name */
1240+
wpa_sm_pmksa_cache_flush(get_wpa_sm(), NULL);
12271241

12281242
return ESP_OK;
1243+
#endif
12291244
}
1230-
1231-
void esp_eap_client_clear_domain_match(void)
1232-
{
1233-
if (g_wpa_domain_match) {
1234-
os_free(g_wpa_domain_match);
1235-
}
1236-
g_wpa_domain_match = NULL;
1237-
}

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))

components/wpa_supplicant/src/eap_peer/eap.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ bool g_wpa_suiteb_certification;
6767
bool g_wpa_default_cert_bundle;
6868
int (*esp_crt_bundle_attach_fn)(void *conf);
6969
#endif
70+
#ifndef CONFIG_TLS_INTERNAL_CLIENT
7071
char *g_wpa_domain_match;
72+
#endif
7173

7274
void eap_peer_config_deinit(struct eap_sm *sm);
7375
void eap_peer_blob_deinit(struct eap_sm *sm);
@@ -530,7 +532,9 @@ int eap_peer_config_init(
530532
sm->config.identity = NULL;
531533
sm->config.password = NULL;
532534
sm->config.new_password = NULL;
535+
#ifndef CONFIG_TLS_INTERNAL_CLIENT
533536
sm->config.domain_match = g_wpa_domain_match;
537+
#endif
534538
sm->config.private_key_passwd = private_key_passwd;
535539
sm->config.client_cert = (u8 *)sm->blob[0].name;
536540
sm->config.private_key = (u8 *)sm->blob[1].name;
@@ -593,7 +597,6 @@ int eap_peer_config_init(
593597
sm->config.flags |= TLS_CONN_USE_DEFAULT_CERT_BUNDLE;
594598
}
595599
#endif
596-
597600
/* To be used only for EAP-FAST */
598601
if (g_wpa_phase1_options) {
599602
sm->config.phase1 = g_wpa_phase1_options;

components/wpa_supplicant/src/eap_peer/eap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ extern bool g_wpa_suiteb_certification;
4949
extern bool g_wpa_default_cert_bundle;
5050
extern int (*esp_crt_bundle_attach_fn)(void *conf);
5151

52+
#ifndef CONFIG_TLS_INTERNAL_CLIENT
5253
extern char *g_wpa_domain_match;
54+
#endif
5355

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

examples/wifi/wifi_enterprise/main/Kconfig.projbuild

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,18 @@ menu "Example Configuration"
100100
help
101101
Use default CA certificate bundle for WiFi enterprise connection
102102

103-
config EXAMPLE_USE_SERVER_DOMAIN_MATCH
104-
bool "Validate server cert domain"
103+
config EXAMPLE_VALIDATE_SERVER_CERT_DOMAIN
104+
bool "Enable server certificate domain validation"
105+
depends on EXAMPLE_VALIDATE_SERVER_CERT
106+
default n
105107
help
106-
Validate the certificate domain
108+
Enable validation of the server certificate's domain name.
107109

108-
config EXAMPLE_SERVER_DOMAIN_MATCH_VALUE
109-
string "Server cert domain"
110-
depends on EXAMPLE_USE_SERVER_DOMAIN_MATCH
110+
config EXAMPLE_SERVER_CERT_DOMAIN
111+
string "Expected server certificate domain"
112+
depends on EXAMPLE_VALIDATE_SERVER_CERT_DOMAIN
111113
default "espressif.com"
112114
help
113-
Accept only server certificates matching this domain
115+
Specify the expected domain name for the server certificate.
116+
The connection will be accepted only if the server certificate matches this domain.
114117
endmenu

examples/wifi/wifi_enterprise/main/wifi_enterprise_main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* SPDX-FileCopyrightText: 2006-2016 ARM Limited
3-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
@@ -33,6 +33,7 @@
3333
#define EXAMPLE_EAP_ID CONFIG_EXAMPLE_EAP_ID
3434
#define EXAMPLE_EAP_USERNAME CONFIG_EXAMPLE_EAP_USERNAME
3535
#define EXAMPLE_EAP_PASSWORD CONFIG_EXAMPLE_EAP_PASSWORD
36+
#define EXAMPLE_SERVER_CERT_DOMAIN CONFIG_EXAMPLE_SERVER_CERT_DOMAIN
3637

3738
/* FreeRTOS event group to signal when we are connected & ready to make a request */
3839
static EventGroupHandle_t wifi_event_group;
@@ -156,8 +157,8 @@ static void initialise_wifi(void)
156157
#ifdef CONFIG_EXAMPLE_USE_DEFAULT_CERT_BUNDLE
157158
ESP_ERROR_CHECK(esp_eap_client_use_default_cert_bundle(true));
158159
#endif
159-
#ifdef CONFIG_EXAMPLE_USE_SERVER_DOMAIN_MATCH
160-
ESP_ERROR_CHECK(esp_eap_client_set_domain_match(CONFIG_EXAMPLE_SERVER_DOMAIN_MATCH_VALUE));
160+
#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT_DOMAIN
161+
ESP_ERROR_CHECK(esp_eap_client_set_domain_name(EXAMPLE_SERVER_CERT_DOMAIN));
161162
#endif
162163
ESP_ERROR_CHECK(esp_wifi_sta_enterprise_enable());
163164
ESP_ERROR_CHECK(esp_wifi_start());

0 commit comments

Comments
 (0)