diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c index 7a416861de..366dd0408c 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_dpp.c @@ -1,16 +1,16 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ -#include "esp_dpp_i.h" #include "esp_dpp.h" #include "esp_wpa.h" #include "esp_timer.h" #include "esp_event.h" #include "esp_wifi.h" #include "common/ieee802_11_defs.h" +#include "esp_dpp_i.h" #ifdef CONFIG_DPP static TaskHandle_t s_dpp_task_hdl = NULL; @@ -19,7 +19,7 @@ static void *s_dpp_api_lock = NULL; static bool s_dpp_stop_listening; static int s_dpp_auth_retries; -struct esp_dpp_context_t s_dpp_ctx; +static struct esp_dpp_context_t s_dpp_ctx; static wifi_action_rx_cb_t s_action_rx_cb = esp_supp_rx_action; #define DPP_API_LOCK() xSemaphoreTakeRecursive(s_dpp_api_lock, portMAX_DELAY) @@ -32,6 +32,7 @@ struct action_rx_param { u32 vendor_data_len; struct ieee80211_action *action_frm; }; +extern bool is_wps_enabled(void); static int esp_dpp_post_evt(uint32_t evt_id, uint32_t data) { @@ -176,6 +177,7 @@ static int esp_dpp_handle_config_obj(struct dpp_authentication *auth, { wifi_config_t *wifi_cfg = &s_dpp_ctx.wifi_cfg; + os_memset(wifi_cfg, 0, sizeof(wifi_config_t)); if (conf->ssid_len) { os_memcpy(wifi_cfg->sta.ssid, conf->ssid, conf->ssid_len); } @@ -378,6 +380,10 @@ static void esp_dpp_task(void *pvParameters ) static int counter; int channel; + if (p->num_chan <= 0) { + wpa_printf(MSG_ERROR, "Listen channel not set"); + break; + } channel = p->chan_list[counter++ % p->num_chan]; esp_wifi_remain_on_channel(WIFI_IF_STA, WIFI_ROC_REQ, channel, BOOTSTRAP_ROC_WAIT_TIME, s_action_rx_cb); @@ -518,6 +524,10 @@ esp_err_t esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type, const char *key, const char *uri_info) { + if (!s_dpp_ctx.dpp_global) { + wpa_printf(MSG_ERROR, "DPP: failed to bootstrap as dpp not initialized."); + return ESP_FAIL; + } struct dpp_bootstrap_params_t *params = &s_dpp_ctx.bootstrap_params; char *uri_chan_list = esp_dpp_parse_chan_list(chan_list); char *command = os_zalloc(1200); @@ -603,6 +613,11 @@ fail: esp_err_t esp_supp_dpp_start_listen(void) { + if (!s_dpp_ctx.dpp_global || s_dpp_ctx.id < 1) { + wpa_printf(MSG_ERROR, "DPP: failed to start listen as dpp not initialized or bootstrapped."); + return ESP_FAIL; + } + if (esp_wifi_get_user_init_flag_internal() == 0) { wpa_printf(MSG_ERROR, "DPP: ROC not possible before wifi is started"); return ESP_ERR_INVALID_STATE; @@ -618,8 +633,29 @@ void esp_supp_dpp_stop_listen(void) esp_wifi_remain_on_channel(WIFI_IF_STA, WIFI_ROC_CANCEL, 0, 0, NULL); } +#ifdef CONFIG_WPA_DPP_SUPPORT +bool is_dpp_enabled(void) +{ + return (s_dpp_ctx.dpp_global ? true : false); +} +#endif + esp_err_t esp_supp_dpp_init(esp_supp_dpp_event_cb_t cb) { + wifi_mode_t mode = 0; + if (esp_wifi_get_mode(&mode) || ((mode != WIFI_MODE_STA) && (mode != WIFI_MODE_APSTA))) { + wpa_printf(MSG_ERROR, "DPP: failed to init as not in station mode."); + return ESP_FAIL; + } + + if (is_wps_enabled()) { + wpa_printf(MSG_ERROR, "DPP: failed to init since WPS is enabled"); + return ESP_FAIL; + } + if (s_dpp_ctx.dpp_global) { + wpa_printf(MSG_ERROR, "DPP: failed to init as init already done."); + return ESP_FAIL; + } struct dpp_global_config cfg = {0}; int ret; @@ -658,7 +694,6 @@ esp_err_t esp_supp_dpp_init(esp_supp_dpp_event_cb_t cb) void esp_supp_dpp_deinit(void) { struct dpp_bootstrap_params_t *params = &s_dpp_ctx.bootstrap_params; - if (params->info) { os_free(params->info); params->info = NULL; @@ -673,7 +708,10 @@ void esp_supp_dpp_deinit(void) esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_ROC_DONE, &offchan_event_handler); s_dpp_auth_retries = 0; - dpp_global_deinit(s_dpp_ctx.dpp_global); - esp_dpp_post_evt(SIG_DPP_DEL_TASK, 0); + if (s_dpp_ctx.dpp_global) { + dpp_global_deinit(s_dpp_ctx.dpp_global); + s_dpp_ctx.dpp_global = NULL; + esp_dpp_post_evt(SIG_DPP_DEL_TASK, 0); + } } #endif diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h b/components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h index 3c7aea750a..91517d4caf 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h +++ b/components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h @@ -1,16 +1,8 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef ESP_DPP_I_H #define ESP_DPP_I_H @@ -65,4 +57,12 @@ struct esp_dpp_context_t { int esp_supp_rx_action(uint8_t *hdr, uint8_t *payload, size_t len, uint8_t channel); +#ifdef CONFIG_WPA_DPP_SUPPORT +bool is_dpp_enabled(void); +#else +static inline bool is_dpp_enabled(void) +{ + return false; +} +#endif #endif /* ESP_DPP_I_H */ diff --git a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c index 48566c8774..64c6969fb6 100644 --- a/components/wpa_supplicant/esp_supplicant/src/esp_wps.c +++ b/components/wpa_supplicant/esp_supplicant/src/esp_wps.c @@ -14,6 +14,7 @@ #include "common/ieee802_11_defs.h" #include "crypto/dh_group5.h" #include "wps/wps_i.h" +#include "esp_dpp_i.h" #include "wps/wps_dev_attr.h" #include "eap_peer/eap_defs.h" #include "eap_peer/eap_common.h" @@ -2147,6 +2148,11 @@ int esp_wifi_wps_enable(const esp_wps_config_t *config) #endif } +bool is_wps_enabled(void) +{ + return s_wps_enabled; +} + int wifi_wps_enable_internal(const esp_wps_config_t *config) { int ret = 0; @@ -2163,6 +2169,10 @@ int wifi_wps_enable_internal(const esp_wps_config_t *config) return ESP_ERR_WIFI_WPS_TYPE; } + if (is_dpp_enabled()) { + wpa_printf(MSG_ERROR, "wps enabled failed since DPP is initialized"); + return ESP_FAIL; + } wpa_printf(MSG_DEBUG, "Set factory information."); ret = wps_set_factory_info(config); if (ret != 0) { diff --git a/components/wpa_supplicant/src/common/dpp.c b/components/wpa_supplicant/src/common/dpp.c index 887add165f..2a6fbdbfcb 100644 --- a/components/wpa_supplicant/src/common/dpp.c +++ b/components/wpa_supplicant/src/common/dpp.c @@ -43,7 +43,7 @@ struct dpp_global { static const struct dpp_curve_params dpp_curves[] = { /* The mandatory to support and the default NIST P-256 curve needs to * be the first entry on this list. */ - { "sec256r1", 32, 32, 16, 32, "P-256", 19, "ES256" }, + { "secp256r1", 32, 32, 16, 32, "P-256", 19, "ES256" }, { "secp384r1", 48, 48, 24, 48, "P-384", 20, "ES384" }, { "secp521r1", 64, 64, 32, 66, "P-521", 21, "ES512" }, { "brainpoolP256r1", 32, 32, 16, 32, "BP-256", 28, "BS256" }, @@ -4669,7 +4669,8 @@ static struct crypto_key * dpp_parse_jwk(struct json_token *jwk, { struct json_token *token; const struct dpp_curve_params *curve; - struct wpabuf *x = NULL, *y = NULL, *a = NULL; + struct wpabuf *x = NULL, *y = NULL; + unsigned char *a = NULL; struct crypto_ec_group *group; struct crypto_key *pkey = NULL; size_t len; @@ -4731,17 +4732,19 @@ static struct crypto_key * dpp_parse_jwk(struct json_token *jwk, goto fail; } - len = wpabuf_len(x); - a = wpabuf_concat(x, y); - pkey = crypto_ec_set_pubkey_point(group, wpabuf_head(a), - len); + len = wpabuf_len(x) + wpabuf_len(y); + a = os_zalloc(len); + os_memcpy(a, wpabuf_head(x), wpabuf_len(x)); + os_memcpy(a + wpabuf_len(x), wpabuf_head(y), wpabuf_len(y)); + pkey = crypto_ec_set_pubkey_point(group, a, len); + crypto_ec_deinit((struct crypto_ec *)group); *key_curve = curve; fail: - wpabuf_free(a); wpabuf_free(x); wpabuf_free(y); + os_free(a); return pkey; } diff --git a/examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c b/examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c index ae9f99d73c..543d685845 100644 --- a/examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c +++ b/examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c @@ -119,12 +119,12 @@ void dpp_enrollee_init(void) wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_supp_dpp_init(dpp_enrollee_event_cb)); /* Currently only supported method is QR Code */ ESP_ERROR_CHECK(esp_supp_dpp_bootstrap_gen(EXAMPLE_DPP_LISTEN_CHANNEL_LIST, DPP_BOOTSTRAP_QR_CODE, EXAMPLE_DPP_BOOTSTRAPPING_KEY, EXAMPLE_DPP_DEVICE_INFO)); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum