Hi,
I am trying to determine what would be an optimal stack and heap allocation for mbedtls.
I realize this changes with algorithms, some configuration macros for the algorithms as well as concurrency.
But is there any information on stack and heap usage that I can use as a starting point? And is there any information on the same for a crypto suite.
Thanks
Michael T
Disclaimer: This message and any files or text attached to it are intended only for the recipients named above and contain information that may be confidential or privileged. If you are not an intended recipient, you must not forward, copy, use or otherwise disclose this communication or the information contained herein. In the event you have received this message in error, please notify the sender immediately by replying to this message, and then delete all copies of it from your system. Thank you.
Hello,
I am currently working on a project using STM32CubeIDE, where I am leveraging mbedTLS v2.16.2 to establish a secure MQTT connection with a Mosquitto broker on port 8883.
During my implementation, I encountered a TLS handshake error with the following details:
mbedtls_ssl_handshake failed. -29312 (-0x7280)
I would like to confirm if mbedTLS v2.16.2, as provided in STM32CubeIDE, supports secure TLS connections (e.g., Secure MQTT) and is compatible with Mosquitto broker configurations. Additionally, I would appreciate guidance on resolving this error.
Here are some specific details about my setup:
1. mbedTLS version: 2.16.2 (as integrated into STM32CubeIDE).
2. Broker: Mosquitto, configured for secure MQTT on port 8883.
3. Issue: TLS handshake fails with the error mentioned above.
4. Certificates: [Specify if you are using self-signed certificates, CA certificates, etc.].
Please let me know if additional information or logs would be helpful for diagnosing the issue.
Thank you for your assistance.
Regards,
Noushad
Embedded systems engineer
Inthings Technologies Private Limited
www.inthings.tech<http://www.inthings.tech>
[cid:image001.png@01DB5D28.96B30030]
Regards,
Noushad
Embedded systems engineer
Inthings Technologies Private Limited
www.inthings.tech<http://www.inthings.tech>
[cid:image001.png@01DB5D28.96B30030]
Dear Mbed TLS Support Team,
I am currently working on a project using Mbed TLS version 3.6 on FreeRTOS,
and I am encountering an issue with handling multiple CA certificates
during the TLS handshake process. My device has a set of built-in
certificates, and I need to try each certificate one by one to establish a
successful connection to my server. However, I am facing difficulties in
this process.
### System Information:
- **Mbed TLS version**: 3.6
- **Operating System**: FreeRTOS
- **Configuration**: Default
- **Compiler and Options**: N/A (using default configuration)
### Expected Behavior:
The first certificate (e.g., `cdotroot.cer`) cannot be verified by Mbed
TLS, while the correct certificate should successfully establish a
connection.
### Actual Behavior:
Both the incorrect and correct CA certificates fail to establish a
connection successfully.
### Steps to Reproduce:
Here is a sample of my code:
```c
static int load_and_verify_certificates(int conn_id, uint8_t *cert_buffer,
size_t buffer_size) {
int ret;
bool connection_established = false;
uint32_t cert_index = 0;
while (!connection_established && cert_index < MAX_CERT_COUNT) {
size_t cert_size = buffer_size;
// Free and initialize the certificate context
mbedtls_x509_crt_free(&cacert[conn_id]);
mbedtls_x509_crt_init(&cacert[conn_id]);
// Load the built-in certificate
ret = try_built_in_certificate(cert_buffer, &cert_size, cert_index);
if (ret == CERT_ERR_INDEX_OUT_OF_RANGE) {
break;
}
if (ret != CERT_SUCCESS) {
cert_index++;
continue;
}
// Parse the certificate
cert_buffer[cert_size] = '\0';
ret = mbedtls_x509_crt_parse(&cacert[conn_id], cert_buffer,
cert_size + 1);
if (ret < 0) {
cert_index++;
continue;
}
// Set the certificate chain
mbedtls_ssl_conf_ca_chain(&conf[conn_id], &cacert[conn_id], NULL);
// Perform the TLS handshake
ret = mbedtls_ssl_handshake(&ssl[conn_id]);
if (ret == 0) {
uint32_t flags = mbedtls_ssl_get_verify_result(&ssl[conn_id]);
if (flags == 0) {
connection_established = true;
// Cache the certificate
cache_certificate(cert_buffer, cert_size, cert_index);
break;
}
} else {
LOGD("Failed to perform handshake with certificate index %d,
error: -0x%x\n", cert_index, -ret);
}
// Reset the SSL session
ret = mbedtls_ssl_session_reset(&ssl[conn_id]);
if (ret != 0) {
LOGD("Failed to reset SSL session, error: -0x%x\n", -ret);
return ret;
}
cert_index++;
}
return connection_established ? 0 : -1;
}
```
### Additional Information:
Here are the logs:
```
Reading certificate 'cdotroot.cer' at address 0x08100650, size: 1348
Failed to perform handshake with certificate index 0, error: -0x2700
Reading certificate 'digicertroot.cer' at address 0x08100B94, size: 1360
Failed to perform handshake with certificate index 1, error: -0x7300
...
```
I suspect that the issue may be related to the limited RAM space available
on my device. I am looking for guidance on how to properly iterate through
and verify the built-in certificates within these constraints. Any
suggestions or best practices for handling multiple certificates in such an
environment would be greatly appreciated.
Thank you for your assistance.
Best regards,
[Tony]
---
Feel free to replace `[Your Name]` with your actual name before sending the
email.
Hello,
I described an issue I have here: https://github.com/Mbed-TLS/mbedtls/issues/9867
During compilation it compiles with the wrong mbedtls_config.h even though I set up this part in my CMakeLists.txt:
#MbedTLS default START
# 1. MbedTLS custom config setup
set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbedtls)
set(MBEDTLS_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/mbedtls_config.h")
add_compile_definitions(
MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}"
MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}"
)
set(ENABLE_TESTING OFF CACHE BOOL "Disable mbedtls testing" FORCE)
set(ENABLE_PROGRAMS OFF CACHE BOOL "Disable mbedtls programs" FORCE)
set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mbedtls)
# Add mbedtls build
add_subdirectory(${MBEDTLS_DIR})
#MbedTLS END
There’s more information on the GitHub link.
Thank you in advance.
Alex
Dear Mbed TLS users,
Mbed TLS has contained the reference implementation of the PSA Crypto API since the early days of the standard. This implementation was experimental at first, but over years of development it reached maturity, and the experimental status was removed three years ago (in version 3.6.1.).
Shortly after that, in 2022, the TF-PSA-Crypto<https://github.com/Mbed-TLS/TF-PSA-Crypto> repository was created just for the PSA Crypto API implementation to make it more accessible for users only interested in that. This repository was only a mirror of the main one and the development of the PSA Crypto API implementation remained integral part of Mbed TLS.
In the background, work has been started to make this repository the place where the development of the PSA Crypto API reference implementation happens. This work has finally been completed and from now on TF-PSA-Crypto<https://github.com/Mbed-TLS/TF-PSA-Crypto> is ready to receive contributions, bug reports and enhancement requests. TF-PSA-Crypto<https://github.com/Mbed-TLS/TF-PSA-Crypto> will now be the development repository for the PSA Crypto reference implementation.
Mbed TLS will continue to rely on TF-PSA-Crypto<https://github.com/Mbed-TLS/TF-PSA-Crypto> and will be using it as a submodule.
Many thanks,
Janos Follath
Mbed TLS developer
Hi,
Can you give me a little advice on how v3 works?
I've updated our mbed library from 2.16.1 to 3.6.2 which was mostly a very
smooth experience, thanks.
The one question in my mind relates to making p_bio private in
mbedtls_ssl_context
*I used to free it before calling mbedtls_ssl_free - is it ok to skip this
now? ie:*
//mbedtls_net_free((mbedtls_net_context *) m_ssl.p_bio);
mbedtls_ssl_free(&m_ssl);
*I also used to poll it before calling mbedtls_ssl_close_notify - is ok to
skip this now? ie:*
//if(mbedtls_net_poll((mbedtls_net_context *) m_ssl.p_bio,
MBEDTLS_NET_POLL_WRITE, 0) < 0)
// return;
int ret;
while((ret = mbedtls_ssl_close_notify(&m_ssl)) < 0)
{
if(ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE)
return;
}
It all seems to run just fine like this, but I could really do with some
confidence boosting validation of my cavalier commenting.
Thank you
Daniel
Dear Mbed TLS team,
I am trying to integrate Mbed TLS in our platform:
- nxp imx rt 1024 CPU (arm based)
- FreeRTOS (10.6)
- Lwip (2.2)
- Mbed TLS (3.6.2)
LWIP supports Mbed TLS when adding defines in their configuration:
/* ---------- TLS ------------------------- */
#define LWIP_ALTCP 1
#define LWIP_ALTCP_TLS 1
#define LWIP_ALTCP_TLS_MBEDTLS 1
But, unfortunately this supports an older version of Mbed TLS (I believe
2.2.x).
When I try to compile against the Mbed TLS 3.6.2 libraries, I get a bunch
of compiler warnings / errors. Most of them are mentioned in the migration
guide, but even then, I have a hard time figuring out how to resolve them
"the right way".
1. Private fields
The migration guide
<https://github.com/Mbed-TLS/mbedtls/blob/mbedtls-3.6/docs/3.0-migration-gui…>
explains that most structs are now considered private.
1.1: The altcp_mbed module in LWIP relies on the *out_left* field of the *ssl
context* struct.
/* calculate TLS overhead part to not send it to application */
overhead = state->overhead_bytes_adjust + state->ssl_context.out_left;
I am correct that this field is simply not exposed through accessor
functions? I can find functions like mbedtls_ssl_get_avail, so I would
expect something like mbedtls_ssl_get_bytes_to_write or something like that?
1.2: The altcp_mbed module in LWIP relies on the *start* field of the *ssl
session* struct.
err_t
altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session
*session)
{
if (session && conn && conn->state) {
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
int ret = -1;
* if (session->data.start)*
ret = mbedtls_ssl_set_session(&state->ssl_context, &session->data);
return ret < 0 ? ERR_VAL : ERR_OK;
}
return ERR_ARG;
}
I also don't know how to obtain this field "the right way". I know this is
a bad idea, but it's all I got
if (session->data.*private_start)* ...
2. parse key expects a RNG function
This is also explained in the migration guide:
*Some functions gained an RNG parameterThis affects users of the following
functions: `mbedtls_ecp_check_pub_priv()`,`mbedtls_pk_check_pair()`,
`mbedtls_pk_parse_key()`, and`mbedtls_pk_parse_keyfile()`.You now need to
pass a properly seeded, cryptographically secure RNG whencalling these
functions. It is used for blinding, a countermeasure againstside-channel
attacks.*
Can you please give me some guidance here? I looked for examples in Mbed
TLS repo and ended up with this (bold parts are added, using the key_app.c
in your repo as leading example):
* mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); if
((ret = mbedtls_ctr_drbg_seed( &ctr_drbg,
mbedtls_entropy_func, &entropy, privkey,
privkey_len)) != 0) { // TODO: handle error } *
ret = mbedtls_pk_parse_key(
conf->pkey, privkey,
privkey_len,
privkey_pass,
privkey_pass_len,
*mbedtls_ctr_drbg_random, &ctr_drbg*);
3. The altcp_mbed module in LWIP relies on mbedtls_ssl_flush_output
The altcp_mbed module had an include on ssl internal.
#include "mbedtls/ssl_internal.h" /* to call mbedtls_flush_output after
ERR_MEM */
This header seems no longer part of the library. But the prototype of the
function is now part of ssl_misc.h, but I don't think I am supposed to rely
on this header either.
I have a couple of options:
- ignore the compiler warning
/home/dev_container/app/thirdparty/lwip/src/apps/altcp_tls/altcp_tls_mbedtls.c:535:5:
warning: implicit declaration of function 'mbedtls_ssl_flush_output'; did
you mean 'mbedtls_ssl_tls_prf'? [-Wimplicit-function-declaration]
535 | mbedtls_ssl_flush_output(&state->ssl_context);
- copy paste the declaration in the LWIP source file
- Or.... don't flush any buffers managed by Mbed TLS in the first place?
For the last one, I could really use your input. Can you say anything
meaningful about the flush (line in bold) In the below function, Is it
mandatory? Or can I trust the Mbed TLS will flush the output buffer
whenever it needs to get flushed?
/** Sent callback from lower connection (i.e. TCP)
* This only informs the upper layer the number of ACKed bytes.
* This now take care of TLS added bytes so application receive
* correct ACKed bytes.
*/
static err_t
altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len)
{
struct altcp_pcb *conn = (struct altcp_pcb *)arg;
LWIP_UNUSED_ARG(inner_conn); /* for LWIP_NOASSERT */
if (conn) {
int overhead;
u16_t app_len;
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
LWIP_ASSERT("state", state != NULL);
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
/* calculate TLS overhead part to not send it to application */
mbedtls_ssl_get_bytes_avail(&state->ssl_context);
overhead = state->overhead_bytes_adjust +
state->ssl_context.private_out_left;
if ((unsigned)overhead > len) {
overhead = len;
}
/* remove ACKed bytes from overhead adjust counter */
state->overhead_bytes_adjust -= len;
/* try to send more if we failed before (may increase overhead adjust
counter) */
*mbedtls_ssl_flush_output(&state->ssl_context); // Does it make sense
this LWIP port is responsible for flushing here??*
/* remove calculated overhead from ACKed bytes len */
app_len = len - (u16_t)overhead;
/* update application write counter and inform application */
if (app_len) {
state->overhead_bytes_adjust += app_len;
if (conn->sent)
return conn->sent(conn->arg, conn, app_len);
}
}
return ERR_OK;
}
After applying the changes I mentioned above, the software compiles against
Mbed TLS. I haven't dared to run anything, I just want to enjoy this short
moment where I have the feeling I achieved something before runtime errors
ruin my day :).
Many thanks in advance for any reply!
Best regards, Bas
[image: image.png]
Hi Team
I am investigating the move from MbedTLS 2.25 to 3.6, however, our client uses the 2.25 currently (looks like they prefer to stay with it) and I need to provide convincing proof to move to 3.6 considering the upgrade issues that might arise. Any suggestions here that is convincing proof to do the migration to 3.6? I would appreciate your comments. Thanks
Michael