forked from espressif/esp-mqtt
add der-format support for tls-certificates/keys
This commit is contained in:
@ -113,9 +113,12 @@ typedef struct {
|
||||
int task_prio; /*!< MQTT task priority, default is 5, can be changed in ``make menuconfig`` */
|
||||
int task_stack; /*!< MQTT task stack size, default is 6144 bytes, can be changed in ``make menuconfig`` */
|
||||
int buffer_size; /*!< size of MQTT send/receive buffer, default is 1024 */
|
||||
const char *cert_pem; /*!< Pointer to certificate data in PEM format for server verify (with SSL), default is NULL, not required to verify the server */
|
||||
const char *client_cert_pem; /*!< Pointer to certificate data in PEM format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key_pem` has to be provided. */
|
||||
const char *client_key_pem; /*!< Pointer to private key data in PEM format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert_pem` has to be provided. */
|
||||
const char *cert_pem; /*!< Pointer to certificate data in PEM or DER format for server verify (with SSL), default is NULL, not required to verify the server. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in cert_len. */
|
||||
size_t cert_len; /*!< Length of the buffer pointed to by cert_pem. May be 0 for null-terminated pem */
|
||||
const char *client_cert_pem; /*!< Pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key_pem` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_cert_len. */
|
||||
size_t client_cert_len; /*!< Length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem */
|
||||
const char *client_key_pem; /*!< Pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert_pem` has to be provided. PEM-format must have a terminating NULL-character. DER-format requires the length to be passed in client_key_len */
|
||||
size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
|
||||
esp_mqtt_transport_t transport; /*!< overrides URI transport */
|
||||
int refresh_connection_after_ms; /*!< Refresh connection after this value (in milliseconds) */
|
||||
const struct psk_key_hint* psk_hint_key; /*!< Pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled */
|
||||
|
@ -42,8 +42,8 @@
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
|
||||
// Features supported in 4.1
|
||||
#define MQTT_SUPPORTED_FEATURE_PSK_AUTHENTICATION
|
||||
#define MQTT_SUPPORTED_FEATURE_DER_CERTIFICATES
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#endif // _MQTT_SUPPORTED_FEATURES_H_
|
@ -26,6 +26,13 @@
|
||||
# define MQTT_API_UNLOCK_FROM_OTHER_TASK(c) { if (c->task_handle != xTaskGetCurrentTaskHandle()) { xSemaphoreGive(c->api_lock); } }
|
||||
#endif /* MQTT_USE_API_LOCKS */
|
||||
|
||||
#ifdef MQTT_SUPPORTED_FEATURE_DER_CERTIFICATES
|
||||
# define MQTT_TRANSPORT_SET_CERT_OR_KEY(setfn, key, len) \
|
||||
{ if (key) { if (len) { setfn##_der(ssl, key, len); } else { setfn(ssl, key, strlen(key)); } } }
|
||||
#else
|
||||
# define MQTT_TRANSPORT_SET_CERT_OR_KEY(setfn, key, len) \
|
||||
{ if (key) { setfn(ssl, key, strlen(key)); } }
|
||||
#endif
|
||||
|
||||
static const char *TAG = "MQTT_CLIENT";
|
||||
|
||||
@ -393,15 +400,18 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co
|
||||
esp_transport_handle_t ssl = esp_transport_ssl_init();
|
||||
ESP_MEM_CHECK(TAG, ssl, goto _mqtt_init_failed);
|
||||
esp_transport_set_default_port(ssl, MQTT_SSL_DEFAULT_PORT);
|
||||
if (config->cert_pem) {
|
||||
esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem));
|
||||
}
|
||||
if (config->client_cert_pem) {
|
||||
esp_transport_ssl_set_client_cert_data(ssl, config->client_cert_pem, strlen(config->client_cert_pem));
|
||||
}
|
||||
if (config->client_key_pem) {
|
||||
esp_transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem));
|
||||
|
||||
#ifndef MQTT_SUPPORTED_FEATURE_DER_CERTIFICATES
|
||||
if (config->cert_len || config->client_cert_len || config->client_key_len) {
|
||||
ESP_LOGE(TAG, "Explicit cert-/key-len is not available in IDF version %s", IDF_VER);
|
||||
goto _mqtt_init_failed;
|
||||
}
|
||||
#endif
|
||||
|
||||
MQTT_TRANSPORT_SET_CERT_OR_KEY(esp_transport_ssl_set_cert_data, config->cert_pem, config->cert_len);
|
||||
MQTT_TRANSPORT_SET_CERT_OR_KEY(esp_transport_ssl_set_client_cert_data, config->client_cert_pem, config->client_cert_len);
|
||||
MQTT_TRANSPORT_SET_CERT_OR_KEY(esp_transport_ssl_set_client_key_data, config->client_key_pem, config->client_key_len);
|
||||
|
||||
if (config->psk_hint_key) {
|
||||
#ifdef MQTT_SUPPORTED_FEATURE_PSK_AUTHENTICATION
|
||||
esp_transport_ssl_set_psk_key_hint(ssl, config->psk_hint_key);
|
||||
|
Reference in New Issue
Block a user