MQTT to AWS IoT Core fails with mbedtls_ssl_handshake_error
mbedtls_ssl_handshake error: -2880
I (14912) wifi:Traceback (most recent call last):
File "main.py", line 99, in
Fails on the latest firmware but works on any of the 2019 builds - tried it with esp32-idf3-20191220-v1.12.bin and it works but needed BLE and hence upgraded to 2020 versions and fails on all the March/April releases.
mbedtls_ssl_handshake error: -2880
This is MBEDTLS_ERR_X509_ALLOC_FAILED, so it's running out of RTOS heap memory.
Thanks @dpgeorge - would adding a gc.collect() and checking for gc.free_men() prior to that socket connect help? I am reading certs into variables but assuming those are in the function stack.
Nope, it's the C heap, not the MP heap... Either use a module with PSRAM or reduce mp_task_heap_size by some factor (e.g. 80%) here https://github.com/micropython/micropython/blob/master/ports/esp32/main.c#L103-L104 and rebuild the fw and try that...
Hi @tve - noted. Curiously I tried the same code that worked on earlier firmware (20191220) doesn't work on the latest one 20200416. Getting mbedtls_ssl_handshake error: -10 error. Could it be an SSL wrap_socket issue?
uriously I tried the same code that worked on earlier firmware (20191220) doesn't work on the latest one 20200416
That's most likely due to an update to the ESP-IDF, which changes the heap layout and has less RTOS heap available.
Hi @Aish-Git see the long explanation in #5543
Thanks @Carglglz - incredibly helpful and sorry didn't see that before I logged this one. Let me try some options listed there in terms of a 1096 bit key to keep the cert length small. I am also going to try the recommendation that @tve provided on increasing the C heap size. Will keep this thread posted...
I also received the ssl_handshake_status error. My error log is as the following
ssl_handshake_status: -256
Cannot connect MQTT: [Errno 5] EIO
Traceback (most recent call last):
File "main.py", line 236, in <module>
File "main.py", line 217, in connect_mqtt
File "umqtt/simple.py", line 62, in connect
OSError: [Errno 5] EIO
I'm using AWS IoT with umqtt. Sometimes it get connected but sometime it gives me the above error. Can anyone help me with this?
I make SSL connections and use AmazonIOT and my memory configuration is this. `size_t mp_task_heap_size = 50 * 1024; //heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
void *mp_task_heap = malloc(mp_task_heap_size);`
Downgrading to 1.12 (esp32-idf3-20191220-v1.12.bin) fixed it for me. 1.13 must do something different.
@Aish-Git did you had any success? I tried to increase the heap size using @tve's suggestions but no luck. I'm using a ESP32 WROVER with PSRAM. Did you managed to increase the C heap?
@eliclement I never did get to going down that path as I switched over to a ESP Pico board (TinyPico) that has an onboard SRAM so ended up working well for me.
@Aish-Git OK thanks. I had to remove the BLE in the build and which freed some heap memory and it was sufficient.
I had similar issues when connecting to RabbitMQ (actually I think that AWS IoT is using RabbitMQ under the hood). I am not sure if you have control over the Ciphers. For me, I controlled it server side and using DHE-RSA-AES128-GCM-SHA256 i worked without any changes to heap. But anyway, more RAM would always be great =) .
Is it possible to restrict the Ciphers Mbedtls is using? Then, I think AWS IoT will also allow using DHE-RSA-AES128-GCM-SHA256 because it is still recommended and you can connect without issues.
Downgrading to 1.12 (esp32-idf3-20191220-v1.12.bin) fixed it for me. 1.13 must do something different.
Thank you for that, @srcnet2 . I've been working on this for a while and v1.12 idf3 binary is the only one that's able to reliably connect to AWS using a generic ESP32.
AWS for IoT is the 800 pound gorilla. Sure would be nice to use a generic ESP32 with the latest binary in an AWS application. Using idf4, "esp32-idf4-20210202-v1.14.bin" results in:
OSError: (-17168, 'MBEDTLS_ERR_RSA_PRIVATE_FAILED+MBEDTLS_ERR_MPI_ALLOC_FAILED')
@sactre said...
I make SSL connections and use AmazonIOT and my memory configuration is this. `size_t mp_task_heap_size = 50 * 1024; //heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
void *mp_task_heap = malloc(mp_task_heap_size);`
If that's all it takes, it might be of value to make available updated binaries that are capable of connecting to AWS IoT Core using a generic ESP32.
I have good news and I have bad news! The new version of MicroPython, idf4 v1.15 -- esp32-20210418-v1.15.bin, works with AWS IoT! YAY!!!
v1.15 does not have mqtt built in so you have to install your own. That's OK because it's easy to do.
Now for the bad news. If I drop into REPL a few times while running my tests, eventually I get a WiFi error that looks like this:
Traceback (most recent call last):
File "<stdin>", line 23, in <module>
OSError: Wifi Internal Error
I don't know why, and the message doesn't give me much to go on. I have to machine.reset() to get the WiFi to work again. A soft boot (^D) doesn't do it.
I will open another issue about this specific WiFi problem.
I can also force my WiFi station mode inactive in my case station.active(False), and that works to reset the WiFi, but then my software has to reconnect every time when coming back from REPL. Is it supposed to work like this?
My solution was to make Micropython leave some of the heap for the RTOS, instead of claiming all of free memory for itself. Experimentation showed that
- 2K reserved never works
- 4K reserved sometimes works
- 8K reserved always works
But this probably depends on what other features are available.
diff --git a/ports/esp32/main.c b/ports/esp32/main.c
index ff6dd6957..0149e8a0b 100644
--- a/ports/esp32/main.c
+++ b/ports/esp32/main.c
@@ -121,6 +121,7 @@ void mp_task(void *pvParameter) {
#else
// Allocate the uPy heap using malloc and get the largest available region
size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
+ mp_task_heap_size -= 8192; // Reserve 8K for C heap (mbedTLS needs it)
void *mp_task_heap = malloc(mp_task_heap_size);
#endif
Does it make sense to make this a configuration option?
My board has no SPIRAM, so I have no idea if this is necessary if there is plenty of RAM.
By default mbedtls only uses internal ram on the esp32 (security concerns). However esp-idf provides a config to allow mbedtls to alloc also in the PSRAM space:
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-mbedtls-mem-alloc-mode