micropython icon indicating copy to clipboard operation
micropython copied to clipboard

MQTT to AWS IoT Core fails with mbedtls_ssl_handshake_error

Open Aish-Git opened this issue 6 years ago • 25 comments

mbedtls_ssl_handshake error: -2880 I (14912) wifi:Traceback (most recent call last): File "main.py", line 99, in File "main.py", line 96, in File "main.py", line 69, in mqtt File "umqtt/simple.py", line 61, in connect OSError: [Errno 5] EIO

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.

Aish-Git avatar Apr 16 '20 04:04 Aish-Git

mbedtls_ssl_handshake error: -2880

This is MBEDTLS_ERR_X509_ALLOC_FAILED, so it's running out of RTOS heap memory.

dpgeorge avatar Apr 16 '20 04:04 dpgeorge

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.

Aish-Git avatar Apr 16 '20 05:04 Aish-Git

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...

tve avatar Apr 16 '20 05:04 tve

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?

Aish-Git avatar Apr 16 '20 18:04 Aish-Git

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.

dpgeorge avatar Apr 18 '20 14:04 dpgeorge

Hi @Aish-Git see the long explanation in #5543

Carglglz avatar Apr 19 '20 15:04 Carglglz

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...

Aish-Git avatar Apr 19 '20 17:04 Aish-Git

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?

rahulsinh-jadeja avatar Jul 07 '20 08:07 rahulsinh-jadeja

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);`

sactre avatar Sep 18 '20 14:09 sactre

Downgrading to 1.12 (esp32-idf3-20191220-v1.12.bin) fixed it for me. 1.13 must do something different.

srcnet2 avatar Dec 04 '20 03:12 srcnet2

@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 avatar Jan 19 '21 12:01 eliclement

@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 avatar Jan 20 '21 01:01 Aish-Git

@Aish-Git OK thanks. I had to remove the BLE in the build and which freed some heap memory and it was sufficient.

eliclement avatar Jan 20 '21 09:01 eliclement

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.

chtinnes avatar Mar 16 '21 09:03 chtinnes

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')

the-stanely avatar Apr 18 '21 18:04 the-stanely

@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.

the-stanely avatar Apr 18 '21 22:04 the-stanely

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.

the-stanely avatar Apr 20 '21 11:04 the-stanely

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?

the-stanely avatar Apr 20 '21 12:04 the-stanely

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.

ironss avatar Aug 31 '21 04:08 ironss

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

pumelo avatar Sep 01 '21 18:09 pumelo