Skip to content

Commit ad94cc9

Browse files
committed
feat(asio): Add mbedtls specific APIs to use TLS stack specific features
Use mbedtls specific API to configure hostname for verification
1 parent 4885d28 commit ad94cc9

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

components/asio/examples/ssl_client_server/main/asio_ssl_main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "asio/ssl.hpp"
1818
#include "asio/buffer.hpp"
1919
#include "esp_pthread.h"
20+
// allows for direct access to mbedtls specifics
21+
#include "asio/ssl/mbedtls_specific.hpp"
2022

2123
extern const unsigned char server_pem_start[] asm("_binary_srv_crt_start");
2224
extern const unsigned char server_pem_end[] asm("_binary_srv_crt_end");
@@ -217,6 +219,7 @@ void ssl_server_thread()
217219
io_context.run();
218220
}
219221

222+
220223
void ssl_client_thread()
221224
{
222225
asio::io_context io_context;
@@ -229,6 +232,11 @@ void ssl_client_thread()
229232
asio::ssl::context ctx(asio::ssl::context::tls_client);
230233
#if CONFIG_EXAMPLE_CLIENT_VERIFY_PEER
231234
ctx.add_certificate_authority(cert_chain);
235+
// mbedtls (from 3.6.3) requires hostname to be set when performing TLS handshake with verify-peer option
236+
// asio::ssl allows for name verification using verification callback, i.e. socket_.set_verify_callback(asio::ssl::host_name_verification()),
237+
// - which is not supported in Espressif ASIO port yet.
238+
// Therefore we provide a way to directly use mbedtls API and here we just configure the expected hostname to verify
239+
asio::ssl::mbedtls::set_hostname(ctx.native_handle(), server_ip);
232240
#endif // CONFIG_EXAMPLE_CLIENT_VERIFY_PEER
233241

234242
Client c(io_context, ctx, endpoints);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
//
4+
// SPDX-License-Identifier: BSL-1.0
5+
//
6+
7+
#pragma once
8+
9+
#include "asio/ssl/context_base.hpp"
10+
#include "asio/ssl/context.hpp"
11+
#include "asio/ssl/detail/openssl_types.hpp"
12+
13+
namespace asio {
14+
namespace ssl {
15+
namespace mbedtls {
16+
17+
/**
18+
* @brief Configures specific hostname to be used in peer verification
19+
*
20+
* @param handle asio::ssl context handle type
21+
* @param name hostname to be verified (std::string ownership will be moved to ssl::context)
22+
*
23+
* @return true on success
24+
*/
25+
bool set_hostname(asio::ssl::context::native_handle_type handle, std::string name);
26+
27+
};
28+
};
29+
} // namespace asio::ssl::mbedtls

components/asio/port/mbedtls/include/mbedtls_context.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
// SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
//
@@ -52,6 +52,12 @@ class context {
5252
return nullptr;
5353
}
5454

55+
bool set_hostname(std::string hostname)
56+
{
57+
hostname_ = std::move(hostname);
58+
return true;
59+
}
60+
5561
std::size_t size(container c) const
5662
{
5763
switch (c) {
@@ -70,6 +76,7 @@ class context {
7076
const_buffer cert_chain_;
7177
const_buffer private_key_;
7278
const_buffer ca_cert_;
79+
std::string hostname_;
7380
};
7481

7582
/**

components/asio/port/mbedtls/include/mbedtls_engine.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
// SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
//
44
// SPDX-License-Identifier: BSL-1.0
55
//
@@ -16,6 +16,11 @@ namespace asio {
1616
namespace ssl {
1717
namespace mbedtls {
1818

19+
bool set_hostname(asio::ssl::context::native_handle_type handle, std::string name)
20+
{
21+
return handle->get()->set_hostname(std::move(name));
22+
}
23+
1924
const char *error_message(int error_code)
2025
{
2126
static char error_buf[100];
@@ -25,7 +30,7 @@ const char *error_message(int error_code)
2530

2631
void throw_alloc_failure(const char *location)
2732
{
28-
asio::error_code ec( MBEDTLS_ERR_SSL_ALLOC_FAILED, asio::error::get_mbedtls_category());
33+
asio::error_code ec(MBEDTLS_ERR_SSL_ALLOC_FAILED, asio::error::get_mbedtls_category());
2934
asio::detail::throw_error(ec, location);
3035
}
3136

@@ -269,6 +274,16 @@ class engine {
269274
} else {
270275
mbedtls_ssl_conf_ca_chain(&conf_, nullptr, nullptr);
271276
}
277+
278+
// Configure hostname before handshake if users pre-configured any
279+
// use NULL if not set (to preserve the default behaviour of mbedtls < v3.6.3)
280+
const char* hostname = !ctx->hostname_.empty() ? ctx->hostname_.c_str() : NULL;
281+
ret = mbedtls_ssl_set_hostname(&ssl_, hostname);
282+
if (ret < 0) {
283+
print_error("mbedtls_ssl_set_hostname", ret);
284+
return false;
285+
}
286+
272287
ret = mbedtls_ssl_setup(&ssl_, &conf_);
273288
if (ret) {
274289
print_error("mbedtls_ssl_setup", ret);

0 commit comments

Comments
 (0)