|
30 | 30 | #include "esp_event.h"
|
31 | 31 | #include <cJSON.h>
|
32 | 32 |
|
| 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 | + |
33 | 45 | #define NO_DATA_TIMEOUT_SEC 5
|
34 | 46 |
|
35 | 47 | static const char *TAG = "websocket";
|
@@ -69,6 +81,34 @@ static void get_string(char *line, size_t size)
|
69 | 81 |
|
70 | 82 | #endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */
|
71 | 83 |
|
| 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 | + |
72 | 112 | static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
73 | 113 | {
|
74 | 114 | esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
|
@@ -163,6 +203,49 @@ static void websocket_app_start(void)
|
163 | 203 | websocket_cfg.cert_pem = cacert_start;
|
164 | 204 | #endif
|
165 | 205 |
|
| 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 | + |
166 | 249 | #if CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK
|
167 | 250 | websocket_cfg.skip_cert_common_name_check = true;
|
168 | 251 | #endif
|
|
0 commit comments