Skip to content

Commit 632dac5

Browse files
committed
Merge branch 'doc/adds_sni_documentation' into 'master'
docs(esp_http): adds documentation regarding sni See merge request espressif/esp-idf!41586 Related #9392
2 parents 8222f88 + e1eca70 commit 632dac5

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

components/esp-tls/esp_tls.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ typedef struct esp_tls_cfg {
203203
const char *common_name; /*!< If non-NULL, server certificate CN must match this name.
204204
If NULL, server certificate CN must match hostname. */
205205

206-
bool skip_common_name; /*!< Skip any validation of server certificate CN field */
206+
bool skip_common_name; /*!< Skip any validation of server certificate CN field.
207+
This field should be set to false for SNI to function correctly. */
207208

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

docs/en/api-reference/protocols/esp_tls.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ ESP-TLS provides multiple options for TLS server verification on the client side
5757

5858
If this option is enabled, there is a risk of establishing a TLS connection with a server that has a fake identity, unless the server certificate is provided through the API or other mechanisms like ``ca_store``.
5959

60+
SNI (Server Name Indication)
61+
----------------------------
62+
63+
SNI is an extension to the TLS protocol that allows the client to specify the hostname it is connecting to during the TLS handshake. This is required when connecting to servers that host multiple domains on the same IP address.
64+
65+
**How to ensure SNI works properly:**
66+
67+
* SNI is enabled by default in ESP-TLS when using HTTPS connections.
68+
* To explicitly set the SNI hostname, use the ``common_name`` field in :cpp:type:`esp_tls_cfg_t`. This ensures that the correct hostname is sent to the server during the handshake.
69+
* The value of ``common_name`` must match the server certificate's CN (Common Name).
70+
* The ``skip_common_name`` field should be set to ``false`` to ensure the server certificate is properly validated against the hostname. This is required for SNI to function correctly.
71+
72+
Example:
73+
74+
.. code-block:: c
75+
76+
esp_tls_cfg_t cfg = {
77+
.cacert_buf = ...,
78+
.cacert_bytes = ...,
79+
.common_name = "example.com", // SNI hostname
80+
.skip_common_name = false, // Ensure certificate is validated
81+
};
82+
6083
ESP-TLS Server Cert Selection Hook
6184
----------------------------------
6285

docs/zh_CN/api-reference/protocols/esp_tls.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ ESP-TLS 在客户端提供了多种验证 TLS 服务器的选项,如验证对
5757

5858
启用 **跳过服务器验证** 选项存在潜在风险,若未通过 API 或 ``ca_store`` 等其他机制提供服务器证书,可能导致设备与伪造身份的服务器建立 TLS 连接。
5959

60+
SNI(服务器名称指示)
61+
----------------------
62+
63+
SNI 是 TLS 协议的一个扩展,它能让客户端在 TLS 握手过程中,主动指定要连接的主机名。当服务器通过同一个 IP 地址托管多个域名时,客户端必须使用这一功能才能成功建立连接。
64+
65+
**如何确保 SNI 正常工作:**
66+
67+
* 使用 HTTPS 连接时,ESP-TLS 默认启用 SNI,无需额外配置。
68+
* 若需手动指定 SNI 主机名,请使用 :cpp:type:`esp_tls_cfg_t` 中的 ``common_name`` 字段进行设置,确保在握手过程中向服务器发送正确的主机名。
69+
* ``common_name`` 的值必须与服务器证书中的通用名称 (CN, Common Name) 完全匹配。
70+
* 需将 ``skip_common_name`` 字段设置为 ``false``,确保服务器证书能通过主机名完成正确验证。这是 SNI 正常运行的必要条件。
71+
72+
示例:
73+
74+
.. code-block:: c
75+
76+
esp_tls_cfg_t cfg = {
77+
.cacert_buf = ...,
78+
.cacert_bytes = ...,
79+
.common_name = "example.com", // SNI 主机名
80+
.skip_common_name = false, // 确保证书验证无误
81+
};
82+
6083
ESP-TLS 服务器证书选择回调
6184
----------------------------------
6285

0 commit comments

Comments
 (0)