Skip to content

Commit a9f42e3

Browse files
feat(websocket): Make example to use certificate bundle
To easy maintenance, makes the example on websocket client to use certificate bundle by default.
1 parent 5964ead commit a9f42e3

File tree

7 files changed

+15
-40
lines changed

7 files changed

+15
-40
lines changed

components/esp_websocket_client/examples/target/main/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ set(EMBED_FILES "") # Initialize an empty list for files to embed
1010
"certs/client_key.pem")
1111
#endif()
1212

13-
# For testing purpose we are using CA of wss://echo.websocket.events
14-
#if(CONFIG_WS_OVER_TLS_SERVER_AUTH)
15-
list(APPEND EMBED_FILES
16-
"certs/ca_certificate_public_domain.pem")
17-
#endif()
18-
1913
# Register the component with source files, include dirs, and any conditionally added embedded files
2014
idf_component_register(SRCS "${SRC_FILES}"
2115
INCLUDE_DIRS "${INCLUDE_DIRS}"

components/esp_websocket_client/examples/target/main/certs/ca_certificate_public_domain.pem

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "nvs_flash.h"
2020
#include "esp_event.h"
2121
#include "protocol_examples_common.h"
22+
#include "esp_crt_bundle.h"
2223

2324
#include "freertos/FreeRTOS.h"
2425
#include "freertos/task.h"
@@ -145,6 +146,14 @@ static void websocket_app_start(void)
145146
websocket_cfg.uri = CONFIG_WEBSOCKET_URI;
146147
#endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */
147148

149+
#if CONFIG_WS_OVER_TLS_SERVER_AUTH || CONFIG_WS_OVER_TLS_MUTUAL_AUTH
150+
// Using certificate bundle as default server certificate source
151+
websocket_cfg.crt_bundle_attach = esp_crt_bundle_attach;
152+
// If using a custom certificate it could be added to certificate bundle, added to the build simmilar to client certificates in this examples,
153+
// or read from NVS.
154+
/* extern const char cacert_start[] asm("ADDED_CERTIFICATE"); */
155+
/* websocket_cfg.cert_pem = cacert_start; */
156+
#endif
148157
#if CONFIG_WS_OVER_TLS_MUTUAL_AUTH
149158
/* Configuring client certificates for mutual authentification */
150159
extern const char cacert_start[] asm("_binary_ca_cert_pem_start"); // CA certificate
@@ -158,9 +167,6 @@ static void websocket_app_start(void)
158167
websocket_cfg.client_cert_len = cert_end - cert_start;
159168
websocket_cfg.client_key = key_start;
160169
websocket_cfg.client_key_len = key_end - key_start;
161-
#elif CONFIG_WS_OVER_TLS_SERVER_AUTH
162-
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
163-
websocket_cfg.cert_pem = cacert_start;
164170
#endif
165171

166172
#if CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK

components/esp_websocket_client/examples/target/pytest_websocket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def send_data(self, data):
5252
def run(self):
5353
if self.use_tls is True:
5454
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
55-
ssl_context.load_cert_chain(certfile='main/certs/server_cert.pem', keyfile='main/certs/server_key.pem')
55+
ssl_context.load_cert_chain(certfile='main/certs/server/server_cert.pem', keyfile='main/certs/server/server_key.pem')
5656
if self.client_verify is True:
5757
ssl_context.load_verify_locations(cafile='main/certs/ca_cert.pem')
5858
ssl_context.verify = ssl.CERT_REQUIRED

components/esp_websocket_client/examples/target/sdkconfig.ci.mutual_auth

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ CONFIG_EXAMPLE_ETH_PHY_ADDR=1
1313
CONFIG_EXAMPLE_CONNECT_IPV6=y
1414
CONFIG_WS_OVER_TLS_MUTUAL_AUTH=y
1515
CONFIG_WS_OVER_TLS_SKIP_COMMON_NAME_CHECK=y
16+
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
17+
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y
18+
CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y
19+
CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH="main/certs/server/server_cert.pem"
20+
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200

0 commit comments

Comments
 (0)