Skip to content

Commit e6f9fe2

Browse files
authored
Merge pull request #583 from DCSBL/ws-client-common-name
feat(websocket_client): Add option to set and use cert_common_name in Websocket client (IDFGH-12926)
2 parents 25d8423 + 3a6720d commit e6f9fe2

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

components/esp_websocket_client/esp_websocket_client.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ typedef struct {
9595
size_t client_key_len;
9696
bool use_global_ca_store;
9797
bool skip_cert_common_name_check;
98+
const char *cert_common_name;
9899
esp_err_t (*crt_bundle_attach)(void *conf);
99100
} websocket_config_storage_t;
100101

@@ -533,6 +534,13 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
533534
if (client->config->skip_cert_common_name_check) {
534535
esp_transport_ssl_skip_common_name_check(ssl);
535536
}
537+
if (client->config->cert_common_name) {
538+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
539+
esp_transport_ssl_set_common_name(ssl, client->config->cert_common_name);
540+
#else
541+
ESP_LOGE(TAG, "cert_common_name requires ESP-IDF 5.1.0 or later");
542+
#endif
543+
}
536544

537545
esp_transport_handle_t wss = esp_transport_ws_init(ssl);
538546
ESP_WS_CLIENT_MEM_CHECK(TAG, wss, return ESP_ERR_NO_MEM);
@@ -668,6 +676,11 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
668676
}
669677

670678
// configure ssl related parameters
679+
if (config->cert_common_name != NULL && config->skip_cert_common_name_check) {
680+
ESP_LOGE(TAG, "Both cert_common_name and skip_cert_common_name_check are set, only one of them can be set");
681+
goto _websocket_init_fail;
682+
}
683+
671684
client->config->use_global_ca_store = config->use_global_ca_store;
672685
client->config->cert = config->cert_pem;
673686
client->config->cert_len = config->cert_len;
@@ -676,6 +689,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
676689
client->config->client_key = config->client_key;
677690
client->config->client_key_len = config->client_key_len;
678691
client->config->skip_cert_common_name_check = config->skip_cert_common_name_check;
692+
client->config->cert_common_name = config->cert_common_name;
679693
client->config->crt_bundle_attach = config->crt_bundle_attach;
680694

681695
if (config->uri) {

components/esp_websocket_client/include/esp_websocket_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef struct {
118118
bool disable_pingpong_discon; /*!< Disable auto-disconnect due to no PONG received within pingpong_timeout_sec */
119119
bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
120120
esp_err_t (*crt_bundle_attach)(void *conf); /*!< Function pointer to esp_crt_bundle_attach. Enables the use of certification bundle for server verification, MBEDTLS_CERTIFICATE_BUNDLE must be enabled in menuconfig. Include esp_crt_bundle.h, and use `esp_crt_bundle_attach` here to include bundled CA certificates. */
121+
const char *cert_common_name; /*!< Expected common name of the server certificate */
121122
bool skip_cert_common_name_check;/*!< Skip any validation of server certificate CN field */
122123
bool keep_alive_enable; /*!< Enable keep-alive timeout */
123124
int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */

0 commit comments

Comments
 (0)