Skip to content

Commit 36d5d8c

Browse files
author
David Čermák
committed
Merge branch 'feat/esp_tls_add_psk' into 'master'
feat(esp_tls): Add support for PSK authentication on server side Closes IDFGH-14083 See merge request espressif/esp-idf!34996
2 parents 56349e6 + 7801d11 commit 36d5d8c

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

components/esp-tls/esp_tls.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -187,9 +187,11 @@ typedef struct esp_tls_cfg {
187187

188188
tls_keep_alive_cfg_t *keep_alive_cfg; /*!< Enable TCP keep-alive timeout for SSL connection */
189189

190+
#if defined(CONFIG_ESP_TLS_PSK_VERIFICATION)
190191
const psk_hint_key_t* psk_hint_key; /*!< Pointer to PSK hint and key. if not NULL (and certificates are NULL)
191192
then PSK authentication is enabled with configured setup.
192193
Important note: the pointer must be valid for connection */
194+
#endif /* CONFIG_ESP_TLS_PSK_VERIFICATION */
193195

194196
esp_err_t (*crt_bundle_attach)(void *conf);
195197
/*!< Function pointer to esp_crt_bundle_attach. Enables the use of certification
@@ -322,6 +324,12 @@ typedef struct esp_tls_cfg_server {
322324
TLS extensions, such as ALPN and server_certificate_type . */
323325
#endif
324326

327+
#if defined(CONFIG_ESP_TLS_PSK_VERIFICATION)
328+
const psk_hint_key_t* psk_hint_key; /*!< Pointer to PSK hint and key. if not NULL (and the certificate/key is NULL)
329+
then PSK authentication is enabled with configured setup.
330+
Important note: the pointer must be valid for connection */
331+
#endif
332+
325333
} esp_tls_cfg_server_t;
326334

327335
/**
@@ -464,7 +472,7 @@ int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_t
464472
* - >=0 if write operation was successful, the return value is the number
465473
* of bytes actually written to the TLS/SSL connection.
466474
* - <0 if write operation was not successful, because either an
467-
* error occured or an action must be taken by the calling process.
475+
* error occurred or an action must be taken by the calling process.
468476
* - ESP_TLS_ERR_SSL_WANT_READ/
469477
* ESP_TLS_ERR_SSL_WANT_WRITE.
470478
* if the handshake is incomplete and waiting for data to be available for reading.
@@ -485,7 +493,7 @@ ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen);
485493
* - 0 if read operation was not successful. The underlying
486494
* connection was closed.
487495
* - <0 if read operation was not successful, because either an
488-
* error occured or an action must be taken by the calling process.
496+
* error occurred or an action must be taken by the calling process.
489497
*/
490498
ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen);
491499

@@ -537,7 +545,7 @@ esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd);
537545
*
538546
* @param[in] sockfd sockfd value to set.
539547
*
540-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
548+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
541549
* - ESP_ERR_INVALID_ARG if (tls == NULL || sockfd < 0)
542550
*/
543551
esp_err_t esp_tls_set_conn_sockfd(esp_tls_t *tls, int sockfd);
@@ -549,7 +557,7 @@ esp_err_t esp_tls_set_conn_sockfd(esp_tls_t *tls, int sockfd);
549557
*
550558
* @param[out] conn_state pointer to the connection state value.
551559
*
552-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
560+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
553561
* - ESP_ERR_INVALID_ARG (Invalid arguments)
554562
*/
555563
esp_err_t esp_tls_get_conn_state(esp_tls_t *tls, esp_tls_conn_state_t *conn_state);
@@ -561,7 +569,7 @@ esp_err_t esp_tls_get_conn_state(esp_tls_t *tls, esp_tls_conn_state_t *conn_stat
561569
*
562570
* @param[in] conn_state connection state value to set.
563571
*
564-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
572+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
565573
* - ESP_ERR_INVALID_ARG (Invalid arguments)
566574
*/
567575
esp_err_t esp_tls_set_conn_state(esp_tls_t *tls, esp_tls_conn_state_t conn_state);
@@ -586,7 +594,7 @@ void *esp_tls_get_ssl_context(esp_tls_t *tls);
586594
*
587595
* @return
588596
* - ESP_OK if creating global CA store was successful.
589-
* - ESP_ERR_NO_MEM if an error occured when allocating the mbedTLS resources.
597+
* - ESP_ERR_NO_MEM if an error occurred when allocating the mbedTLS resources.
590598
*/
591599
esp_err_t esp_tls_init_global_ca_store(void);
592600

@@ -605,7 +613,7 @@ esp_err_t esp_tls_init_global_ca_store(void);
605613
*
606614
* @return
607615
* - ESP_OK if adding certificates was successful.
608-
* - Other if an error occured or an action must be taken by the calling process.
616+
* - Other if an error occurred or an action must be taken by the calling process.
609617
*/
610618
esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes);
611619

components/esp-tls/esp_tls_mbedtls.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,18 @@ static esp_err_t set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
659659
ESP_LOGE(TAG, "Failed to set server pki context");
660660
return esp_ret;
661661
}
662+
#if defined(CONFIG_ESP_TLS_PSK_VERIFICATION)
663+
} else if (cfg->psk_hint_key) {
664+
ESP_LOGD(TAG, "PSK authentication");
665+
ret = mbedtls_ssl_conf_psk(&tls->conf, cfg->psk_hint_key->key, cfg->psk_hint_key->key_size,
666+
(const unsigned char *)cfg->psk_hint_key->hint, strlen(cfg->psk_hint_key->hint));
667+
if (ret != 0) {
668+
ESP_LOGE(TAG, "mbedtls_ssl_conf_psk returned -0x%04X", -ret);
669+
mbedtls_print_error_msg(ret);
670+
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_MBEDTLS, -ret);
671+
return ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED;
672+
}
673+
#endif
662674
} else {
663675
#if defined(CONFIG_ESP_TLS_SERVER_CERT_SELECT_HOOK)
664676
if (cfg->cert_select_cb == NULL) {
@@ -770,8 +782,8 @@ esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
770782
return esp_ret;
771783
}
772784
mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
773-
} else if (cfg->psk_hint_key) {
774785
#if defined(CONFIG_ESP_TLS_PSK_VERIFICATION)
786+
} else if (cfg->psk_hint_key) {
775787
//
776788
// PSK encryption mode is configured only if no certificate supplied and psk pointer not null
777789
ESP_LOGD(TAG, "ssl psk authentication");
@@ -783,13 +795,10 @@ esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
783795
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ESP_TLS_ERR_TYPE_MBEDTLS, -ret);
784796
return ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED;
785797
}
786-
#else
787-
ESP_LOGE(TAG, "psk_hint_key configured but not enabled in menuconfig: Please enable ESP_TLS_PSK_VERIFICATION option");
788-
return ESP_ERR_INVALID_STATE;
789798
#endif
790799
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
791800
} else if (cfg->client_session != NULL) {
792-
ESP_LOGD(TAG, "Resuing the saved client session");
801+
ESP_LOGD(TAG, "Reusing the saved client session");
793802
#endif
794803
} else {
795804
#ifdef CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY

0 commit comments

Comments
 (0)