11/*
2- * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
1313#include <esp_err.h>
1414#include <mbedtls/sha1.h>
1515#include <mbedtls/base64.h>
16+ #include <mbedtls/error.h>
1617
1718#include <esp_http_server.h>
1819#include "esp_httpd_priv.h"
1920#include "freertos/event_groups.h"
21+ #include "sdkconfig.h"
2022
2123#ifdef CONFIG_HTTPD_WS_SUPPORT
2224
@@ -51,23 +53,23 @@ static const char *TAG="httpd_ws";
5153 */
5254static const char ws_magic_uuid [] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ;
5355
54- /* Checks if any subprotocols from the comma seperated list matches the supported one
56+ /* Checks if any subprotocols from the comma separated list matches the supported one
5557 *
5658 * Returns true if the response should contain a protocol field
5759*/
5860
5961/**
60- * @brief Checks if any subprotocols from the comma seperated list matches the supported one
62+ * @brief Checks if any subprotocols from the comma separated list matches the supported one
6163 *
6264 * @param supported_subprotocol[in] The subprotocol supported by the URI
63- * @param subprotocol[in], [in]: A comma seperate list of subprotocols requested
65+ * @param subprotocol[in], [in]: A comma separate list of subprotocols requested
6466 * @param buf_len Length of the buffer
6567 * @return true: found a matching subprotocol
6668 * @return false
6769 */
6870static bool httpd_ws_get_response_subprotocol (const char * supported_subprotocol , char * subprotocol , size_t buf_len )
6971{
70- /* Request didnt contain any subprotocols */
72+ /* Request didn't contain any subprotocols */
7173 if (strnlen (subprotocol , buf_len ) == 0 ) {
7274 return false;
7375 }
@@ -77,7 +79,7 @@ static bool httpd_ws_get_response_subprotocol(const char *supported_subprotocol,
7779 return false;
7880 }
7981
80- /* Get first subprotocol from comma seperated list */
82+ /* Get first subprotocol from comma separated list */
8183 char * rest = NULL ;
8284 char * s = strtok_r (subprotocol , ", " , & rest );
8385 do {
@@ -143,7 +145,34 @@ esp_err_t httpd_ws_respond_server_handshake(httpd_req_t *req, const char *suppor
143145
144146 /* Generate SHA-1 first and then encode to Base64 */
145147 size_t key_len = strlen (server_raw_text );
146- mbedtls_sha1 ((uint8_t * )server_raw_text , key_len , server_key_hash );
148+
149+ #if CONFIG_MBEDTLS_SHA1_C || CONFIG_MBEDTLS_HARDWARE_SHA
150+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ;
151+ mbedtls_sha1_context ctx ;
152+ mbedtls_sha1_init (& ctx );
153+
154+ if ((ret = mbedtls_sha1_starts (& ctx )) != 0 ) {
155+ goto sha_end ;
156+ }
157+
158+ if ((ret = mbedtls_sha1_update (& ctx , (uint8_t * )server_raw_text , key_len )) != 0 ) {
159+ goto sha_end ;
160+ }
161+
162+ if ((ret = mbedtls_sha1_finish (& ctx , server_key_hash )) != 0 ) {
163+ goto sha_end ;
164+ }
165+
166+ sha_end :
167+ mbedtls_sha1_free (& ctx );
168+ if (ret != 0 ) {
169+ ESP_LOGE (TAG , "Error in calculating SHA1 sum , returned 0x%02X" , ret );
170+ return ESP_FAIL ;
171+ }
172+ #else
173+ ESP_LOGE (TAG , "Please enable CONFIG_MBEDTLS_SHA1_C or CONFIG_MBEDTLS_HARDWARE_SHA to support SHA1 operations" );
174+ return ESP_FAIL ;
175+ #endif /* CONFIG_MBEDTLS_SHA1_C || CONFIG_MBEDTLS_HARDWARE_SHA */
147176
148177 size_t encoded_len = 0 ;
149178 mbedtls_base64_encode ((uint8_t * )server_key_encoded , sizeof (server_key_encoded ), & encoded_len ,
@@ -153,7 +182,7 @@ esp_err_t httpd_ws_respond_server_handshake(httpd_req_t *req, const char *suppor
153182
154183 char subprotocol [50 ] = { '\0' };
155184 if (httpd_req_get_hdr_value_str (req , "Sec-WebSocket-Protocol" , subprotocol , sizeof (subprotocol ) - 1 ) == ESP_ERR_HTTPD_RESULT_TRUNC ) {
156- ESP_LOGW (TAG , "Sec-WebSocket-Protocol length exceeded buffer size of %" NEWLIB_NANO_COMPAT_FORMAT ", was trunctated " , NEWLIB_NANO_COMPAT_CAST (sizeof (subprotocol )));
185+ ESP_LOGW (TAG , "Sec-WebSocket-Protocol length exceeded buffer size of %" NEWLIB_NANO_COMPAT_FORMAT ", was truncated " , NEWLIB_NANO_COMPAT_CAST (sizeof (subprotocol )));
157186 }
158187
159188
0 commit comments