Skip to content

Commit 032ac8f

Browse files
feat(websocket): Enhance websocket example to test ds-peripherial
1 parent 3057765 commit 032ac8f

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

components/esp_websocket_client/examples/target/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ cmake_minimum_required(VERSION 3.5)
44

55
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
66
project(websocket_example)
7+
8+
# Flash the custom partition named `esp_secure_cert`.
9+
set(partition esp_secure_cert)
10+
idf_build_get_property(project_dir PROJECT_DIR)
11+
set(image_file ${project_dir}/esp_secure_cert_data/${partition}.bin)
12+
partition_table_get_partition_info(offset "--partition-name ${partition}" "offset")
13+
esptool_py_flash_target_image(flash "${partition}" "${offset}" "${image_file}")

components/esp_websocket_client/examples/target/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ Please see the openssl man pages (man openssl) for more details.
6363
It is **strongly recommended** to not reuse the example certificate in your application;
6464
it is included only for demonstration.
6565

66+
#### 3) Configure the DS peripheral
67+
68+
* i) Install the [esp_secure_cert configuration utility](https://github.com/espressif/esp_secure_cert_mgr/tree/main/tools#esp_secure_cert-configuration-tool) with following comma nd:
69+
```
70+
pip install esp-secure-cert-tool
71+
```
72+
* ii) The DS peripheral can be configured by executing the following command:
73+
74+
```
75+
configure_esp_secure_cert.py -p /* Serial port */ --device-cert /* Device cert */ --private-key /* RSA priv key */ --target_chip /* target chip */ --configure_ds --skip_flash
76+
```
77+
This command shall generate a partition named `esp_secure_cert.bin` in the `esp_secure_cert_data` directory. This partition would be aumatically detected by the build system and flashed at appropriate offset when `idf.py flash` command is used. For this process, the command must be executed in the current folder only.
78+
79+
In the command USB COM port is nothing but the serial port to which the ESP chip is connected. see
80+
[check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details.
81+
RSA private key is nothing but the client private key ( RSA ) generated in Step 2.
82+
83+
> Note: More details about the `esp-secure-cert-tool` utility can be found [here](https://github.com/espressif/esp_secure_cert_mgr/tree/main/tools).
84+
85+
#### 4) Connection cofiguration
86+
* Open the project configuration menu (`idf.py menuconfig`)
87+
* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../READM E.md) for more details.
88+
89+
### Build and Flash
90+
91+
Build the project and flash it to the board, then run monitor tool to view serial output:
92+
93+
```
94+
idf.py -p PORT flash monitor
95+
```
96+
97+
(To exit the serial monitor, type ``Ctrl-]``.)
98+
99+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
100+
101+
66102
### Build and Flash
67103

68104
Build the project and flash it to the board, then run monitor tool to view serial output:

components/esp_websocket_client/examples/target/main/idf_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies:
2+
espressif/esp_secure_cert_mgr: "^2.4.1"
23
## Required IDF version
34
idf: ">=5.0"
45
espressif/esp_websocket_client:

components/esp_websocket_client/examples/target/main/websocket_example.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
#include "esp_event.h"
3131
#include <cJSON.h>
3232

33+
#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL
34+
#include "mbedtls/ssl.h"
35+
#include "mbedtls/pk.h"
36+
#include "mbedtls/x509.h"
37+
#include "mbedtls/entropy.h"
38+
#include "mbedtls/ctr_drbg.h"
39+
#include "mbedtls/error.h"
40+
#include "esp_idf_version.h"
41+
#include "esp_secure_cert_read.h"
42+
#include "esp_crt_bundle.h"
43+
#endif
44+
3345
#define NO_DATA_TIMEOUT_SEC 5
3446

3547
static const char *TAG = "websocket";
@@ -69,6 +81,34 @@ static void get_string(char *line, size_t size)
6981

7082
#endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */
7183

84+
#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL
85+
static esp_err_t test_ciphertext_validity(esp_ds_data_ctx_t *ds_data, unsigned char *dev_cert, size_t dev_cert_len)
86+
{
87+
mbedtls_x509_crt crt;
88+
mbedtls_x509_crt_init(&crt);
89+
unsigned char *sig = NULL;
90+
91+
if (ds_data == NULL || dev_cert == NULL) {
92+
return ESP_ERR_INVALID_ARG;
93+
}
94+
95+
int ret = mbedtls_x509_crt_parse(&crt, dev_cert, dev_cert_len);
96+
if (ret < 0) {
97+
ESP_LOGE(TAG, "Parsing of device certificate failed, returned %02X", ret);
98+
}
99+
100+
esp_err_t esp_ret = esp_ds_init_data_ctx(ds_data);
101+
if (esp_ret != ESP_OK) {
102+
ESP_LOGE(TAG, "Failed to initialze the DS context");
103+
return esp_ret;
104+
}
105+
106+
const size_t sig_len = 256;
107+
uint32_t hash[8] = {[0 ... 7] = 0xAABBCCDD};
108+
return esp_ret;
109+
}
110+
#endif
111+
72112
static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
73113
{
74114
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
@@ -163,6 +203,49 @@ static void websocket_app_start(void)
163203
websocket_cfg.cert_pem = cacert_start;
164204
#endif
165205

206+
#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL
207+
uint32_t len = 0;
208+
char *addr = NULL;
209+
esp_err_t esp_ret = ESP_FAIL;
210+
211+
esp_ds_data_ctx_t *ds_data = NULL;
212+
ESP_LOGI(TAG, "Successfully obtained the ds context before");
213+
ds_data = esp_secure_cert_get_ds_ctx();
214+
ESP_LOGI(TAG, "Successfully obtained the ds context after");
215+
if (ds_data != NULL) {
216+
ESP_LOGI(TAG, "Successfully obtained the ds context");
217+
ESP_LOG_BUFFER_HEX_LEVEL(TAG, ds_data->esp_ds_data->c, ESP_DS_C_LEN, ESP_LOG_DEBUG);
218+
ESP_LOG_BUFFER_HEX_LEVEL(TAG, ds_data->esp_ds_data->iv, ESP_DS_IV_LEN, ESP_LOG_DEBUG);
219+
ESP_LOGI(TAG, "The value of rsa length is %d", ds_data->rsa_length_bits);
220+
ESP_LOGI(TAG, "The value of efuse key id is %d", ds_data->efuse_key_id);
221+
} else {
222+
ESP_LOGE(TAG, "Failed to obtain the ds context");
223+
}
224+
225+
/* Read the dev_cert addr again */
226+
esp_ret = esp_secure_cert_get_device_cert(&addr, &len);
227+
if (esp_ret != ESP_OK) {
228+
ESP_LOGE(TAG, "Failed to obtain the dev cert flash address");
229+
}
230+
231+
esp_ret = test_ciphertext_validity(ds_data, (unsigned char *)addr, len);
232+
if (esp_ret != ESP_OK) {
233+
ESP_LOGE(TAG, "Failed to validate ciphertext");
234+
} else {
235+
ESP_LOGI(TAG, "Ciphertext validated succcessfully");
236+
}
237+
websocket_cfg.client_cert = addr;
238+
websocket_cfg.client_ds_data = ds_data;
239+
// websocket_cfg.crt_bundle_attach = esp_crt_bundle_attach;
240+
websocket_cfg.cert_pem = addr;
241+
// extern const char cacert_start[] asm("_binary_ca_cert_pem_start"); // CA certificate
242+
// websocket_cfg.cert_pem = cacert_start;
243+
// websocket_cfg.client_key = NULL;
244+
245+
// extern const char cacert_start[] asm("_binary_ca_certificate_public_domain_pem_start"); // CA cert of wss://echo.websocket.event, modify it if using another server
246+
// websocket_cfg.cert_pem = cacert_start;
247+
#endif
248+
166249
#if CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK
167250
websocket_cfg.skip_cert_common_name_check = true;
168251
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=y
2+
CONFIG_PARTITION_TABLE_CUSTOM=y
3+
CONFIG_PARTITION_TABLE_OFFSET=0xC000
4+
CONFIG_IDF_TARGET="esp32s2"
5+
CONFIG_WEBSOCKET_URI_FROM_STRING=y
6+
CONFIG_WEBSOCKET_URI="wss://echo.websocket.events"

0 commit comments

Comments
 (0)