Procházet zdrojové kódy

Merge branch 'bugfix/dpp_pkey_api_fix' into 'master'

Move adding prefix/postfix of DPP key to example

Closes IDFGH-7116

See merge request espressif/esp-idf!17383
Nachiket Kukade před 3 roky
rodič
revize
bd40793771

+ 1 - 1
components/wpa_supplicant/esp_supplicant/include/esp_dpp.h

@@ -71,7 +71,7 @@ void esp_supp_dpp_deinit(void);
   *
   * @param chan_list List of channels device will be available on for listening
   * @param type Bootstrap method type, only QR Code method is supported for now.
-  * @param key (Optional) Private Key used to generate a Bootstrapping Public Key
+  * @param key (Optional) 32 byte Raw Private Key for generating a Bootstrapping Public Key
   * @param info (Optional) Ancilliary Device Information like Serial Number
   *
   * @return

+ 2 - 31
components/wpa_supplicant/esp_supplicant/src/esp_dpp.c

@@ -349,9 +349,7 @@ static void esp_dpp_task(void *pvParameters )
 
     for (;;) {
         if (xQueueReceive(s_dpp_evt_queue, &evt, portMAX_DELAY) == pdTRUE) {
-            if (evt->id < SIG_DPP_MAX) {
-                DPP_API_LOCK();
-            } else {
+            if (evt->id >= SIG_DPP_MAX) {
                 os_free(evt);
                 continue;
             }
@@ -394,7 +392,6 @@ static void esp_dpp_task(void *pvParameters )
             }
 
             os_free(evt);
-            DPP_API_UNLOCK();
 
             if (task_del) {
                 break;
@@ -559,27 +556,9 @@ esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type,
         }
     }
 
-    if (key) {
-        params->key_len = strlen(key);
-        if (params->key_len) {
-            char prefix[] = "30310201010420";
-            char postfix[] = "a00a06082a8648ce3d030107";
-
-            params->key = os_zalloc(params->key_len +
-                                    sizeof(prefix) + sizeof(postfix));
-            if (!params->key) {
-                os_free(command);
-                ret = ESP_ERR_NO_MEM;
-                goto fail;
-            }
-            sprintf(params->key, "%s%s%s", prefix, key, postfix);
-        }
-    }
-
     sprintf(command, "type=qrcode mac=" MACSTR "%s%s%s%s%s",
             MAC2STR(params->mac), uri_chan_list,
-            params->key_len ? "key=" : "",
-            params->key_len ? params->key : "",
+            key ? "key=" : "", key ? key : "",
             params->info_len ? " info=" : "",
             params->info_len ? params->info : "");
 
@@ -590,10 +569,6 @@ esp_supp_dpp_bootstrap_gen(const char *chan_list, enum dpp_bootstrap_type type,
             os_free(params->info);
             params->info = NULL;
         }
-        if (params->key) {
-            os_free(params->key);
-            params->key = NULL;
-        }
         goto fail;
     }
 
@@ -667,10 +642,6 @@ void esp_supp_dpp_deinit(void)
         os_free(params->info);
         params->info = NULL;
     }
-    if (params->key) {
-        os_free(params->key);
-        params->key = NULL;
-    }
 
     esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_ACTION_TX_STATUS,
                                &offchan_event_handler);

+ 5 - 15
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-2022 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
 
 #ifndef ESP_DPP_I_H
 #define ESP_DPP_I_H
@@ -47,8 +39,6 @@ struct dpp_bootstrap_params_t {
     uint8_t chan_list[14];
     uint8_t num_chan;
     uint8_t mac[6];
-    uint32_t key_len;
-    char *key;
     uint32_t info_len;
     char *info;
 };

+ 1 - 1
examples/wifi/wifi_easy_connect/dpp-enrollee/main/Kconfig.projbuild

@@ -8,7 +8,7 @@ menu "Example Configuration"
     config ESP_DPP_BOOTSTRAPPING_KEY
         string "Bootstrapping key"
         help
-            Private key string for DPP Bootstrapping in PEM format.
+            64 hex digits (or 32 bytes) of raw private key for DPP Bootstrapping.
 
     config ESP_DPP_DEVICE_INFO
         string "Additional Device Info"

+ 37 - 4
examples/wifi/wifi_easy_connect/dpp-enrollee/main/dpp_enrollee_main.c

@@ -36,6 +36,8 @@
 #define EXAMPLE_DPP_DEVICE_INFO      0
 #endif
 
+#define CURVE_SEC256R1_PKEY_HEX_DIGITS     64
+
 static const char *TAG = "wifi dpp-enrollee";
 wifi_config_t s_dpp_wifi_config;
 
@@ -104,6 +106,40 @@ void dpp_enrollee_event_cb(esp_supp_dpp_event_t event, void *data)
     }
 }
 
+esp_err_t dpp_enrollee_bootstrap(void)
+{
+    esp_err_t ret;
+    size_t pkey_len = strlen(EXAMPLE_DPP_BOOTSTRAPPING_KEY);
+    char *key = NULL;
+
+    if (pkey_len) {
+        /* Currently only NIST P-256 curve is supported, add prefix/postfix accordingly */
+        char prefix[] = "30310201010420";
+        char postfix[] = "a00a06082a8648ce3d030107";
+
+        if (pkey_len != CURVE_SEC256R1_PKEY_HEX_DIGITS) {
+            ESP_LOGI(TAG, "Invalid key length! Private key needs to be 32 bytes (or 64 hex digits) long");
+            return ESP_FAIL;
+        }
+
+        key = malloc(sizeof(prefix) + pkey_len + sizeof(postfix));
+        if (!key) {
+            ESP_LOGI(TAG, "Failed to allocate for bootstrapping key");
+            return ESP_ERR_NO_MEM;
+        }
+        sprintf(key, "%s%s%s", prefix, EXAMPLE_DPP_BOOTSTRAPPING_KEY, postfix);
+    }
+
+    /* Currently only supported method is QR Code */
+    ret = esp_supp_dpp_bootstrap_gen(EXAMPLE_DPP_LISTEN_CHANNEL_LIST, DPP_BOOTSTRAP_QR_CODE,
+                                     key, EXAMPLE_DPP_DEVICE_INFO);
+
+    if (key)
+        free(key);
+
+    return ret;
+}
+
 void dpp_enrollee_init(void)
 {
     s_dpp_event_group = xEventGroupCreate();
@@ -120,10 +156,7 @@ void dpp_enrollee_init(void)
     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
 
     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(dpp_enrollee_bootstrap());
     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
     ESP_ERROR_CHECK(esp_wifi_start());
 

+ 0 - 1
tools/ci/check_copyright_ignore.txt

@@ -1694,7 +1694,6 @@ components/wifi_provisioning/src/wifi_provisioning_priv.h
 components/wifi_provisioning/src/wifi_scan.c
 components/wpa_supplicant/esp_supplicant/include/esp_rrm.h
 components/wpa_supplicant/esp_supplicant/include/esp_wps.h
-components/wpa_supplicant/esp_supplicant/src/esp_dpp_i.h
 components/wpa_supplicant/esp_supplicant/src/esp_scan_i.h
 components/wpa_supplicant/esp_supplicant/src/esp_wpa3.c
 components/wpa_supplicant/esp_supplicant/src/esp_wpa3_i.h