Skip to content

Commit

Permalink
Added support to set server common name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erlkoenig90 authored and euripedesrocha committed Jun 8, 2023
1 parent ffd7d4d commit 6195762
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ typedef struct esp_mqtt_client_config_t {
bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field, this reduces the
security of TLS and makes the *MQTT* client susceptible to MITM attacks */
const char **alpn_protos; /*!< NULL-terminated list of supported application protocols to be used for ALPN */
const char *common_name; /*!< Pointer to the string containing server certificate common name.
If non-NULL, server certificate CN must match this name,
If NULL, server certificate CN must match hostname.
This is ignored if skip_cert_common_name_check=true. */
} verification; /*!< Security verification of the broker */
} broker; /*!< Broker address and security verification */
/**
Expand Down
6 changes: 6 additions & 0 deletions include/mqtt_supported_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@
#define MQTT_SUPPORTED_FEATURE_CERTIFICATE_BUNDLE
#endif

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
// Features supported in 5.1.0
#define MQTT_SUPPORTED_FEATURE_CRT_CMN_NAME
#endif


#endif /* ESP_IDF_VERSION */
#endif // _MQTT_SUPPORTED_FEATURES_H_
1 change: 1 addition & 0 deletions lib/include/mqtt_client_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef struct {
size_t clientkey_bytes;
const struct psk_key_hint *psk_hint_key;
bool skip_cert_common_name_check;
const char *common_name;
bool use_secure_element;
void *ds_data;
int message_retransmit_timeout;
Expand Down
10 changes: 10 additions & 0 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ static esp_err_t esp_mqtt_set_ssl_transport_properties(esp_transport_list_handle
#endif
}

if (cfg->common_name) {
#if defined(MQTT_SUPPORTED_FEATURE_CRT_CMN_NAME) && MQTT_ENABLE_SSL
esp_transport_ssl_set_common_name(ssl, cfg->common_name);
#else
ESP_LOGE(TAG, "Setting expected certificate common name is not available in IDF version %s", IDF_VER);
goto esp_mqtt_set_transport_failed;
#endif
}

if (cfg->use_secure_element) {
#ifdef MQTT_SUPPORTED_FEATURE_SECURE_ELEMENT
#ifdef CONFIG_ESP_TLS_USE_SECURE_ELEMENT
Expand Down Expand Up @@ -509,6 +518,7 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
client->config->clientkey_buf = config->credentials.authentication.key;
client->config->clientkey_bytes = config->credentials.authentication.key_len;
client->config->skip_cert_common_name_check = config->broker.verification.skip_cert_common_name_check;
client->config->common_name = config->broker.verification.common_name;
client->config->use_secure_element = config->credentials.authentication.use_secure_element;
client->config->ds_data = config->credentials.authentication.ds_data;

Expand Down

0 comments on commit 6195762

Please sign in to comment.