Skip to content

Commit 7ef2379

Browse files
committed
feat(esp_tls): Add support for PSK authentication on server side
1 parent e8800ac commit 7ef2379

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

components/esp-tls/esp_tls.h

Lines changed: 14 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
*/
@@ -322,6 +322,12 @@ typedef struct esp_tls_cfg_server {
322322
TLS extensions, such as ALPN and server_certificate_type . */
323323
#endif
324324

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

327333
/**
@@ -464,7 +470,7 @@ int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_t
464470
* - >=0 if write operation was successful, the return value is the number
465471
* of bytes actually written to the TLS/SSL connection.
466472
* - <0 if write operation was not successful, because either an
467-
* error occured or an action must be taken by the calling process.
473+
* error occurred or an action must be taken by the calling process.
468474
* - ESP_TLS_ERR_SSL_WANT_READ/
469475
* ESP_TLS_ERR_SSL_WANT_WRITE.
470476
* if the handshake is incomplete and waiting for data to be available for reading.
@@ -485,7 +491,7 @@ ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen);
485491
* - 0 if read operation was not successful. The underlying
486492
* connection was closed.
487493
* - <0 if read operation was not successful, because either an
488-
* error occured or an action must be taken by the calling process.
494+
* error occurred or an action must be taken by the calling process.
489495
*/
490496
ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen);
491497

@@ -537,7 +543,7 @@ esp_err_t esp_tls_get_conn_sockfd(esp_tls_t *tls, int *sockfd);
537543
*
538544
* @param[in] sockfd sockfd value to set.
539545
*
540-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
546+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
541547
* - ESP_ERR_INVALID_ARG if (tls == NULL || sockfd < 0)
542548
*/
543549
esp_err_t esp_tls_set_conn_sockfd(esp_tls_t *tls, int sockfd);
@@ -549,7 +555,7 @@ esp_err_t esp_tls_set_conn_sockfd(esp_tls_t *tls, int sockfd);
549555
*
550556
* @param[out] conn_state pointer to the connection state value.
551557
*
552-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
558+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
553559
* - ESP_ERR_INVALID_ARG (Invalid arguments)
554560
*/
555561
esp_err_t esp_tls_get_conn_state(esp_tls_t *tls, esp_tls_conn_state_t *conn_state);
@@ -561,7 +567,7 @@ esp_err_t esp_tls_get_conn_state(esp_tls_t *tls, esp_tls_conn_state_t *conn_stat
561567
*
562568
* @param[in] conn_state connection state value to set.
563569
*
564-
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated withthe provided value
570+
* @return - ESP_OK on success and value of sockfd for the tls connection shall updated with the provided value
565571
* - ESP_ERR_INVALID_ARG (Invalid arguments)
566572
*/
567573
esp_err_t esp_tls_set_conn_state(esp_tls_t *tls, esp_tls_conn_state_t conn_state);
@@ -586,7 +592,7 @@ void *esp_tls_get_ssl_context(esp_tls_t *tls);
586592
*
587593
* @return
588594
* - ESP_OK if creating global CA store was successful.
589-
* - ESP_ERR_NO_MEM if an error occured when allocating the mbedTLS resources.
595+
* - ESP_ERR_NO_MEM if an error occurred when allocating the mbedTLS resources.
590596
*/
591597
esp_err_t esp_tls_init_global_ca_store(void);
592598

@@ -605,7 +611,7 @@ esp_err_t esp_tls_init_global_ca_store(void);
605611
*
606612
* @return
607613
* - ESP_OK if adding certificates was successful.
608-
* - Other if an error occured or an action must be taken by the calling process.
614+
* - Other if an error occurred or an action must be taken by the calling process.
609615
*/
610616
esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const unsigned int cacert_pem_bytes);
611617

components/esp-tls/esp_tls_mbedtls.c

Lines changed: 13 additions & 1 deletion
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) {
@@ -789,7 +801,7 @@ esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
789801
#endif
790802
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
791803
} else if (cfg->client_session != NULL) {
792-
ESP_LOGD(TAG, "Resuing the saved client session");
804+
ESP_LOGD(TAG, "Resuming the saved client session");
793805
#endif
794806
} else {
795807
#ifdef CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY

0 commit comments

Comments
 (0)