espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.79k stars 7.31k forks source link

mbedtls_ssl_handshake server sticks in failing with MBEDTLS_ERR_RSA_VERIFY_FAILED error after some point (IDFGH-10491) #11738

Closed HamzaHajeir closed 9 months ago

HamzaHajeir commented 1 year ago

Answers checklist.

IDF version.

release/v4.4

Operating System used.

Windows

How did you build your project?

Other (please specify in More Information)

If you are using Windows, please specify command line type.

None

Development Kit.

ESP32DevKit1

Power Supply used.

USB

What is the expected behavior?

I host TLS webserver using LwIP ALTCP TLS - MBEDTLS port, I expect every client (browser) to be able to connect anytime.

In essence, It works correctly to some point, correct behaviour includes making successful handshakes and communication. The problem starts after some amount of time/process, in which causes to deny any new connections, even from browsers were already connected by the means of a reload button.
Already connected connections prior to this point continue to communicate successfully afterwards.

The error code returned by the mbedtls_ssl_handshake is fixed at -17280 (-0x4380) (MBEDTLS_ERR_RSA_VERIFY_FAILED).

I've noticed that some times the preceeding handshake's error code is -17168 (-0x4310), which I've tried to find the represented meaning, but failed to, nearest assumption is MBEDTLS_ERR_RSA_PRIVATE_FAILED | MBEDTLS_ERR_MPI_NEGATIVE_VALUE.

Is there any hint for what's going wrong? With thanks,

What is the actual behavior?

Log shows:

IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3542: mbedtls_pk_sign() returned -17280 (-0x4380)

Or in the other described case:

IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3542: mbedtls_pk_sign() returned -17168 (-0x4310)

Steps to reproduce.

Here's the running ALTCP MBEDTLS Code, pointing to the handshake API call:

https://github.com/HamzaHajeir/esp-lwip/blob/2.1.2-esp-patch/src/apps/altcp_tls/altcp_tls_mbedtls.c#L278

Debug Logs.

Example of a complete verbose log:
(You can expect other data being interfered with UART port at printing, those which are printed from the other core).

00:31:38.711 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:5804: => handshake
00:31:38.713 > H4AT:1: H=58248 M=42996 B=42996 S=5292 NEW CONNECTION 0x3ffdeb8c --> IDF/components/mbedtls/mbedtls/library/ssl_srv.c:4511: server state: 0
00:31:38.714 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.715 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2042: <= flush output
00:31:38.716 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:4511: server state: 1
00:31:38.717 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.718 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2042: <= flush output
00:31:38.718 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1397: => parse client hello
00:31:38.719 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1835: => fetch input
00:31:38.720 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1977: in_left: 0, nb_want: 5
00:31:38.721 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1997: in_left: 0, nb_want: 5
00:31:38.722 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1998: ssl->f_recv(_timeout)() returned 5 (-0xfffffffb)
00:31:38.723 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2020: <= fetch input
00:31:38.724 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1449: client hello v3, message type: 22
00:31:38.725 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1457: client hello v3, message len.: 512
00:31:38.726 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1460: client hello v3, protocol version: [3:1]
00:31:38.727 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1835: => fetch input
00:31:38.727 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1977: in_left: 5, nb_want: 517
00:31:38.728 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1997: in_left: 5, nb_want: 517
00:31:38.729 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:1998: ssl->f_recv(_timeout)() returned 512 (-0xfffffe00)
00:31:38.730 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2020: <= fetch input
00:31:38.731 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1551: client hello v3, handshake type: 1
00:31:38.731 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1559: client hello v3, handshake len.: 508
00:31:38.732 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1653: dumping 'client hello, version' (2 bytes)
00:31:38.733 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1653: 0000:  03 03                                            ..
00:31:38.734 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1682: dumping 'client hello, random bytes' (32 bytes)
00:31:38.735 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1682: 0000:  1c 39 2f 13 eb 46 bf 57 d8 cf 3d 30 cb de 2b 88  .9/..F.W..=0..+.
00:31:38.737 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1682: 0010:  c6 a2 c4 94 3e 99 d9 29 a6 d1 81 2e 34 f8 98 07  ....>..)....4...
00:31:38.738 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1699: dumping 'client hello, session id' (32 bytes)
00:31:38.739 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1699: 0000:  b9 2a d6 fc 5a 13 29 91 8d 7f 9f a6 c5 32 06 2b  .*..Z.)......2.+
00:31:38.741 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1699: 0010:  9e b0 63 d8 d4 c2 9f 2d 10 ee 72 5a 60 83 1c 6a  ..c....-..rZ`..j
00:31:38.742 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1774: dumping 'client hello, ciphersuitelist' (32 bytes)
00:31:38.743 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1774: 0000:  4a 4a 13 01 13 02 13 03 c0 2b c0 2f c0 2c c0 30  JJ.......+./.,.0
00:31:38.745 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1774: 0010:  cc a9 cc a8 c0 13 c0 14 00 9c 00 9d 00 2f 00 35  ............./.5
00:31:38.747 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1793: dumping 'client hello, compression' (1 bytes)
00:31:38.748 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1793: 0000:  00                                               .
00:31:38.749 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: dumping 'client hello extensions' (403 bytes)
00:31:38.751 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0000:  5a 5a 00 00 00 0d 00 14 00 12 04 03 08 04 04 01  ZZ..............
00:31:38.752 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0010:  05 03 08 05 05 01 08 06 06 01 02 01 00 0a 00 0a  ................
00:31:38.753 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0020:  00 08 6a 6a 00 1d 00 17 00 18 00 23 00 00 ff 01  ..jj.......#....
00:31:38.755 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0030:  00 01 00 44 69 00 05 00 03 02 68 32 00 17 00 00  ...Di.....h2....
00:31:38.756 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0040:  00 33 00 2b 00 29 6a 6a 00 01 00 00 1d 00 20 49  .3.+.)jj...... I
00:31:38.758 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0050:  ae af b9 7e 87 5d a5 53 fa 10 d3 5a b6 74 6f e8  ...~.].S...Z.to.
00:31:38.759 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0060:  7c 32 b6 43 90 85 aa 14 08 9c ba a3 ea d7 7a 00  |2.C..........z.
00:31:38.760 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0070:  05 00 05 01 00 00 00 00 00 12 00 00 00 0b 00 02  ................
00:31:38.762 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0080:  01 00 00 10 00 0e 00 0c 02 68 32 08 68 74 74 70  .........h2.http
00:31:38.763 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0090:  2f 31 2e 31 00 2d 00 02 01 01 00 1b 00 03 02 00  /1.1.-..........
00:31:38.765 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00a0:  02 00 2b 00 07 06 da da 03 04 03 03 6a 6a 00 01  ..+.........jj..
00:31:38.766 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00b0:pcb=0x3ffdac80 state=4
00:31:38.766 >   00 00 15 00 de 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.767 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00c0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.769 > H=59200 M=42996 S=IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00d0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.771 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00e0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.773 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 00f0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.774 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0100:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.775 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0110:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.777 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0120:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.778 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0130:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.779 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0140:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.781 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0150:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.782 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0160:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.784 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0170:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.785 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0180:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00:31:38.786 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1842: 0190:  00 00 00                                         ...
00:31:38.787 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 23130 (ignoring)
00:31:38.788 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1892: found signature_algorithms extension
00:31:38.789 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 4 and hash 6
00:31:38.790 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0278: client hello v3, signature_algorithm ext unknown sig alg encoding 4
00:31:38.791 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 1 and hash 6
00:31:38.793 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 4 and hash 7
00:31:38.794 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0278: client hello v3, signature_algorithm ext unknown sig alg encoding 5
00:31:38.795 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 1 and hash 7
00:31:38.797 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0278: client hello v3, signature_algorithm ext unknown sig alg encoding 6
00:31:38.798 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 1 and hash 8
00:31:38.799 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0294: client hello v3, signature_algorithm ext: match sig 1 and hash 4
00:31:38.801 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1907: found supported elliptic curves extension
00:31:38.802 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1995: found session ticket extension
00:31:38.804 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1878: found renegotiation extension
00:31:38.805 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 17513 (ignoring)
00:31:38.806 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1984: found extended master secret extension
00:31:38.806 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 51 (ignoring)
00:31:38.807 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 5 (ignoring)
00:31:38.808 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 18 (ignoring)
00:31:38.809 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1916: found supported point formats extension
00:31:38.810 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2006: found alpn extension
00:31:38.811 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 45 (ignoring)
00:31:38.812 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 27 (ignoring)
00:31:38.813 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 43 (ignoring)
00:31:38.814 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 27242 (ignoring)
00:31:38.815 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2028: unknown extension found: 21 (ignoring)
00:31:38.816 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1013: trying ciphersuite: 0xc02c (TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384)
00:31:38.817 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0914: ciphersuite requires certificate
00:31:38.818 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: candidate certificate chain, certificate #1:
00:31:38.819 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: cert. version     : 3
00:31:38.820 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: serial number     : 4D:39:38:95:3A:2D:82:2B:5B:9C:C3:FE:12:DF:DE:84:D7:57:3B:01
00:31:38.822 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: issuer name       : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.823 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: subject name      : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.824 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: issued  on        : 2023-06-10 14:08:27
00:31:38.825 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: expires on        : 2024-06-09 14:08:27
00:31:38.827 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: signed using      : RSA with SHA-256
00:31:38.827 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: RSA key size      : 2048 bits
00:31:38.828 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: basic constraints : CA=true
00:31:38.829 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: value of 'crt->rsa.N' (2048 bits) is:
00:31:38.830 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  bd 0d 52 b2 bd 26 b0 87 7a 8b 04 93 56 80 3c ff
00:31:38.831 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  a2 83 09 63 a8 4d a9 1c 71 fb df c4 bc a7 52 af
00:31:38.832 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  40 d6 91 b0 45 4e 2e 71 ab 1d ce a8 cb 5a 44 33
00:31:38.834 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  9e 6f 93 f0 20 d5 0a c4 05 a2 1b 09 60 d0 3a c8
00:31:38.835 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  df 3e 2d 0a 2a 5b 9b e2 79 f9 bc 26 9e df 6a 79
00:31:38.836 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  f1 9a 4d 08 87 c4 e8 ac 87 84 75 17 c9 30 88 80
00:31:38.837 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  e2 30 b9 27 95 59 82 21 60 4e 1a 16 b4 89 ce 8e
00:31:38.838 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  65 55 ad e1 43 3b b8 00 01 59 b6 64 fa a4 f7 f9
00:31:38.839 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  66 40 a0 26 3b af c0 28 b5 b6 67 81 ad bd b9 c1
00:31:38.840 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  be 12 53 8f 77 23 fc ec 63 af 02 bb b5 ae 83 2a
00:31:38.841 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  21 d8 df 5b 9c bc 8a 36 53 32 df 14 7c 2c 71 b6
00:31:38.844 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  d6 ca 8c 59 f3 a7 46 2a b1 a7 92 d0 87 91 8b 63
00:31:38.844 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  88 6e 3d f5 56 df f8 c9 55 7f 1c 32 de 45 40 fd
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  e5 24 5a 8c 2a be ba b7 55 6a 59 73 24 d9 e0 31
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  ad cb 0a 87 c8 74 f2 90 03 d9 81 ab cb 41 6c 78
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  03 03 9d ea 4e 8a b6 43 3a 82 3d e3 ca 97 dc cf
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: value of 'crt->rsa.E' (17 bits) is:
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  01 00 01
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0927: certificate mismatch: key type
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1094: ciphersuite mismatch: no suitable certificate
00:31:38.852 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:1013: trying ciphersuite: 0xc030 (TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384)
00:31:38.854 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0914: ciphersuite requires certificate
00:31:38.855 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: candidate certificate chain, certificate #1:
00:31:38.856 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: cert. version     : 3
00:31:38.857 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: serial number     : 4D:39:38:95:3A:2D:82:2B:5B:9C:C3:FE:12:DF:DE:84:D7:57:3B:01
00:31:38.859 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: issuer name       : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.860 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: subject name      : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.861 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: issued  on        : 2023-06-10 14:08:27
00:31:38.862 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: expires on        : 2024-06-09 14:08:27
00:31:38.863 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: signed using      : RSA with SHA-256
00:31:38.864 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: RSA key size      : 2048 bits
00:31:38.865 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: basic constraints : CA=true
00:31:38.866 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: value of 'crt->rsa.N' (2048 bits) is:
00:31:38.867 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  bd 0d 52 b2 bd 26 b0 87 7a 8b 04 93 56 80 3c ff
00:31:38.868 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  a2 5292 
00:31:38.869 > 83 09 63 a8 4d a9 1c 71 fb df c4 bc a7 52 af
00:31:38.870 > SLOG: heap 0x00004000 56884
00:31:38.870 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  40 d6 91 b0 45 4e 2e 71 ab 1d ce a8 cb 5a 44 33
00:31:38.871 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  9e 6f 93 f0 20 d5 0a c4 05 a2 1b 09 60 d0 3a c8
00:31:38.873 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  df 3e 2d 0a 2a 5b 9b e2 79 f9 bc 26 9e df 6a 79
00:31:38.874 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  f1 9a 4d 08 87 c4 e8 ac 87 84 75 17 c9 30 88 80
00:31:38.875 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  e2 30 b9 27 95 59 82 21 60 4e 1a 16 b4 89 ce 8e
00:31:38.876 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  65 55 ad e1 43 3b b8 00 01 59 b6 64 fa a4 f7 f9
00:31:38.877 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  66 40 a0 26 3b af c0 28 b5 b6 67 81 ad bd b9 c1
00:31:38.878 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  be 12 53 8f 77 23 fc ec 63 af 02 bb b5 ae 83 2a
00:31:38.878 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  21 d8 df 5b 9c bc 8a 36 53 32 df 14 7c 2c 71 b6
00:31:38.881 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  d6 ca 8c 59 f3 a7 46 2a b1 a7 92 d0 87 91 8b 63
00:31:38.882 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  88 6e 3d f5 56 df f8 c9 55 7f 1c 32 de 45 40 fd
00:31:38.883 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  e5 24 5a 8c 2a be ba b7 55 6a 59 73 24 d9 e0 31
00:31:38.884 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  ad cb 0a 87 c8 74 f2 90 03 d9 81 ab cb 41 6c 78
00:31:38.885 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  03 03 9d ea 4e 8a b6 43 3a 82 3d e3 ca 97 dc cf
00:31:38.886 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924: value of 'crt->rsa.E' (17 bits) is:
00:31:38.887 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0924:  01 00 01
00:31:38.888 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: selected certificate chain, certificate #1:
00:31:38.889 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: cert. version     : 3
00:31:38.890 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: serial number     : 4D:39:38:95:3A:2D:82:2B:5B:9C:C3:FE:12:DF:DE:84:D7:57:3B:01
00:31:38.891 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: issuer name       : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.893 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: subject name      : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.894 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: issued  on        : 2023-06-10 14:08:27
00:31:38.895 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: expires on        : 2024-06-09 14:08:27
00:31:38.896 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: signed using      : RSA with SHA-256
00:31:38.897 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: RSA key size      : 2048 bits
00:31:38.898 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: basic constraints : CA=true
00:31:38.899 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: value of 'crt->rsa.N' (2048 bits) is:
00:31:38.900 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  bd 0d 52 b2 bd 26 b0 87 7a 8b 04 93 56 80 3c ff
00:31:38.901 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  a2 83 09 63 a8 4d a9 1c 71 fb df c4 bc a7 52 af
00:31:38.902 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  40 d6 91 b0 45 4e 2e 71 ab 1d ce a8 cb 5a 44 33
00:31:38.903 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  9e 6f 93 f0 20 d5 0a c4 05 a2 1b 09 60 d0 3a c8
00:31:38.904 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  df 3e 2d 0a 2a 5b 9b e2 79 f9 bc 26 9e df 6a 79
00:31:38.905 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  f1 9a 4d 08 87 c4 e8 ac 87 84 75 17 c9 30 88 80
00:31:38.907 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  e2 30 b9 27 95 59 82 21 60 4e 1a 16 b4 89 ce 8e
00:31:38.908 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  65 55 ad e1 43 3b b8 00 01 59 b6 64 fa a4 f7 f9
00:31:38.909 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  66 40 a0 26 3b af c0 28 b5 b6 67 81 ad bd b9 c1
00:31:38.910 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  be 12 53 8f 77 23 fc ec 63 af 02 bb b5 ae 83 2a
00:31:38.911 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  21 d8 df 5b 9c bc 8a 36 53 32 df 14 7c 2c 71 b6
00:31:38.912 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  d6 ca 8c 59 f3 a7 46 2a b1 a7 92 d0 87 91 8b 63
00:31:38.913 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  88 6e 3d f5 56 df f8 c9 55 7f 1c 32 de 45 40 fd
00:31:38.915 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  e5 24 5a 8c 2a be ba b7 55 6a 59 73 24 d9 e0 31
00:31:38.916 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  ad cb 0a 87 c8 74 f2 90 03 d9 81 ab cb 41 6c 78
00:31:38.917 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  03 03 9d ea 4e 8a b6 43 3a 82 3d e3 ca 97 dc cf
00:31:38.918 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983: value of 'crt->rsa.E' (17 bits) is:
00:31:38.919 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:0983:  01 00 01
00:31:38.920 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2192: selected ciphersuite: TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384
00:31:38.921 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2215: client hello v3, signature_algorithm ext: 4
00:31:38.922 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2223: <= parse client hello
00:31:38.923 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:4511: server state: 2
00:31:38.924 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.924 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2042: <= flush output
00:31:38.925 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2755: => write server hello
00:31:38.926 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2787: server hello, chosen version: [3:3]
00:31:38.927 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2795: server hello, current time: 204
00:31:38.928 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2812: dumping 'server hello, random bytes' (32 bytes)
00:31:38.929 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2812: 0000:  00 00 00 cc 2d 5a cb ed 9c fc 1f ee 13 df b1 b5  ....-Z..........
00:31:38.930 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2812: 0010:  99 87 11 0f 44 13 c4 09 9b cc 22 b3 41 6d 3a 16  ....D.....".Am:.
00:31:38.931 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2865: server hello, session id len.: 32
00:31:38.932 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2866: dumping 'server hello, session id' (32 bytes)
00:31:38.934 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2866: 0000:  ff 28 07 e3 8a 79 08 36 27 f3 7c 2a 57 b3 4c 26  .(...y.6'.|*W.L&
00:31:38.935 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2866: 0010:  bc ea 95 7e 97 08 01 ff 14 90 57 be 2b a9 15 41  ...~......W.+..A
00:31:38.937 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2868: no session has been resumed
00:31:38.938 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2875: server hello, chosen ciphersuite: TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384
00:31:38.939 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2877: server hello, compress alg.: 0x00
00:31:38.940 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2403: server hello, secure renegotiation extension
00:31:38.941 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2356: server hello, adding extended master secret extension
00:31:38.942 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2470: server hello, supported_point_formats extension
00:31:38.944 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2945: server hello, total extension length: 15
00:31:38.945 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2451: => write handshake message
00:31:38.946 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2598: => write record
00:31:38.946 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2706: output record: msgtype = 22, version = [3:3], msglen = 91
00:31:38.948 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.948 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2049: message length: 96, out_left: 96
00:31:38.949 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2054: ssl->f_send() returned 96 (-0xffffffa0)
00:31:38.950 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2081: <= flush output
00:31:38.951 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2756: <= write record
00:31:38.952 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2575: <= write handshake message
00:31:38.953 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:2962: <= write server hello
00:31:38.954 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:4511: server state: 3
00:31:38.954 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.955 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2042: <= flush output
00:31:38.956 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2161: => write certificate
00:31:38.957 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: own certificate #1:
00:31:38.958 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: cert. version     : 3
00:31:38.958 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: serial number     : 4D:39:38:95:3A:2D:82:2B:5B:9C:C3:FE:12:DF:DE:84:D7:57:3B:01
00:31:38.960 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: issuer name       : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.961 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: subject name      : C=JO, ST=AMM, L=AMM, O=H4, CN=H4Tester
00:31:38.962 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: issued  on        : 2023-06-10 14:08:27
00:31:38.963 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: expires on        : 2024-06-09 14:08:27
00:31:38.965 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: signed using      : RSA with SHA-256
00:31:38.966 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: RSA key size      : 2048 bits
00:31:38.966 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: basic constraints : CA=true
00:31:38.967 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: value of 'crt->rsa.N' (2048 bits) is:
00:31:38.968 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  bd 0d 52 b2 bd 26 b0 87 7a 8b 04 93 56 80 3c ff
00:31:38.970 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  a2 83 09 63 a8 4d a9 1c 71 fb df c4 bc a7 52 af
00:31:38.971 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  40 d6 91 b0 45 4e 2e 71 ab 1d ce a8 cb 5a 44 33
00:31:38.972 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  9e 6f 93 f0 20 d5 0a c4 05 a2 1b 09 60 d0 3a c8
00:31:38.974 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  df 3e 2d 0a 2a 5b 9b e2 79 f9 bc 26 9e df 6a 79
00:31:38.975 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  f1 9a 4d 08 87 c4 e8 ac 87 84 75 17 c9 30 88 80
00:31:38.976 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  e2 30 b9 27 95 59 82 21 60 4e 1a 16 b4 89 ce 8e
00:31:38.978 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  65 55 ad e1 43 3b b8 00 01 59 b6 64 fa a4 f7 f9
00:31:38.979 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  66 40 a0 26 3b af c0 28 b5 b6 67 81 ad bd b9 c1
00:31:38.980 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  be 12 53 8f 77 23 fc ec 63 af 02 bb b5 ae 83 2a
00:31:38.981 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  21 d8 df 5b 9c bc 8a 36 53 32 df 14 7c 2c 71 b6
00:31:38.982 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  d6 ca 8c 59 f3 a7 46 2a b1 a7 92 d0 87 91 8b 63
00:31:38.982 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  88 6e 3d f5 56 df f8 c9 55 7f 1c 32 de 45 40 fd
00:31:38.983 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  e5 24 5a 8c 2a be ba b7 55 6a 59 73 24 d9 e0 31
00:31:38.985 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  ad cb 0a 87 c8 74 f2 90 03 d9 81 ab cb 41 6c 78
00:31:38.986 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  03 03 9d ea 4e 8a b6 43 3a 82 3d e3 ca 97 dc cf
00:31:38.987 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204: value of 'crt->rsa.E' (17 bits) is:
00:31:38.988 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2204:  01 00 01
00:31:38.989 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2451: => write handshake message
00:31:38.989 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2598: => write record
00:31:38.990 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2706: output record: msgtype = 22, version = [3:3], msglen = 897
00:31:38.991 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:38.992 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2049: message length: 902, out_left: 902
00:31:38.993 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2054: ssl->f_send() returned 902 (-0xfffffc7a)
00:31:38.994 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2081: <= flush output
00:31:38.995 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2756: <= write record
00:31:38.996 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2575: <= write handshake message
00:31:38.997 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:2254: <= write certificate
00:31:38.998 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:4511: server state: 4
00:31:38.998 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2033: => flush output
00:31:39.000 > IDF/components/mbedtls/mbedtls/library/ssl_msg.c:2042: <= flush output
00:31:39.000 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3565: => write server key exchange
00:31:39.002 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3353: ECDHE curve: secp384r1
00:31:39.031 > SLOG: tick 0x80000000 205
00:31:39.032 > SLOG: upTime 0x00004000   0 00:03:25
00:31:39.169 > H=53216 M=42996 S=5292 
00:31:39.171 > SLOG: heap 0x00004000 52996
00:31:39.469 > H=51752 M=42996 S=5292 
00:31:39.470 > SLOG: heap 0x00004000 51420
00:31:39.771 > H=50792 M=42996 S=5292 
00:31:39.772 > SLOG: heap 0x00004000 50716
00:31:39.932 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377: value of 'ECDH: Q(X)' (384 bits) is:
00:31:39.934 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  d3 8d 3c 55 6b 98 68 fb 51 cf 3d ee 04 20 58 52
00:31:39.935 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  a4 71 d5 66 c0 14 f0 60 d6 90 e3 6b 5b ad cd 03
00:31:39.936 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  d8 70 7a 3f a6 53 ec 31 e9 cf 5e 6e 3e 81 83 a9
00:31:39.937 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377: value of 'ECDH: Q(Y)' (383 bits) is:
00:31:39.938 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  47 46 41 b3 5a c0 3e 16 b8 cb 31 94 fc 6e e0 79
00:31:39.939 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  6d 41 f8 e8 9a 53 fe 4a de 64 ae 38 63 f7 a1 b4
00:31:39.941 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3377:  bd e6 f3 4e a0 6f 17 36 7f 39 68 9e 24 3c 3c 06
00:31:39.942 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3438: pick hash algorithm 6 for signing
00:31:39.943 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:7575: Perform mbedtls-based computation of digest of ServerKeyExchange
00:31:39.944 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3474: dumping 'parameters hash' (32 bytes)
00:31:39.945 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3474: 0000:  55 6e 08 d5 49 ea 86 99 3e 1a 89 da cd c8 16 77  Un..I...>......w
00:31:39.946 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3474: 0010:  ee 74 15 b9 28 53 09 f5 2c de 7c 3c b3 26 96 15  .t..(S..,.|<.&..
00:31:40.031 > SLOG: tick 0x80000000 206
00:31:40.033 > SLOG: upTime 0x00004000   0 00:03:26
00:31:40.072 > H=47440 M=42996 S=5292 
00:31:40.073 > SLOG: heap 0x00004000 47492
00:31:40.342 > IDF/components/mbedtls/mbedtls/library/ssl_srv.c:3542: mbedtls_pk_sign() returned -17280 (-0x4380)
00:31:40.344 > IDF/components/mbedtls/mbedtls/library/ssl_tls.c:5815: <= handshake
00:31:40.345 > tcp_recved: received 517 bytes, wnd 5744 (0).
00:31:40.345 > mbedtls_ssl_handshake failed: -17280

More Information.

Building was done using espressif/esp32-arduino-lib-builder tools, with patched LwIP code (linked above). mbedTLS version is 2.28.3

Meanwhile this issue, the MCU is able to reconnect to a MQTT server by TLS with successful handshakes.

Restarting the webserver with fresh SSL context allows for successful handshakes again.

My investigation of a very long log confirms that this problem leaks memory too.

HamzaHajeir commented 1 year ago

Here's an issue raised in parallel to mbedTLS, I'm not sure where the issue but suspecting either mbedTLS or LwIP ALTCP TLS port, LwIP comes with with less probability because I've did trace the memory alloc/free there.

https://github.com/Mbed-TLS/mbedtls/issues/7833

AdityaHPatwardhan commented 1 year ago

Hi @HamzaHajeir

Looking at the the context, it looks related to memory leaking/corruption. Can you share your sdkconfig file? What are the H, M, S values? are those the Free heap and Minimum heap values?

Also can you share the free heap and minimum free heap sizes at the time of the issue?

Also, you mentioned that the previously connected devices continue the connection without any problem, Have you tried disconnecting those clients and trying to establish the connection once again after you have faced the above issue?

HamzaHajeir commented 1 year ago

Hi @AdityaHPatwardhan,

Looking at the the context, it looks related to memory leaking/corruption. Can you share your sdkconfig file? What are the H, M, S values? are those the Free heap and Minimum heap values?

H: Free Heap, M: Max heap block, S: Stack watermark level. The heap values are measured with MALLOC_CAP_DEFAULT capability.

Here's the sdkconfig, I'm now experimenting the same issue under ESP32S3 (This sdkconfig belongs to ESP32S3 environment): sdkconfig.txt

Also can you share the free heap and minimum free heap sizes at the time of the issue? I'll Insert that for the upcoming logs.

Also, you mentioned that the previously connected devices continue the connection without any problem, Have you tried disconnecting those clients and trying to establish the connection once again after you have faced the above issue? When that happened, even connected clients cannot reconnect after a disconnect (As Shift+F5 Refresh).

HamzaHajeir commented 1 year ago

When the memory leak of 164 bytes happens, I've notices unchanged minimum heap block size of 2137063 (PSRAM is activated), while the heap decreases by 164 bytes per new connection/handshake.

I'm not sure why the minimum heap block is unchanged.

** Just got an instance where the leak is 176 bytes, the modules debug is off for current builds, please ask for it if needed.

AdityaHPatwardhan commented 1 year ago

Please get the readings for MALLOC_CAP_INTERNAL, In your sdkconfig I can see that the option CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC has been set to 1 ( Which is the default behaviour for mbedTLS) Minimum heap block notifies the lowest value for free_heap_size through its lifetime, so it can remain constant but would be lower than free_heap_size.

HamzaHajeir commented 1 year ago

Here's a log with the changes. I've manually marked the reference heap point, which is the value prior to a new curl HTTPS request.

You can look for marks ===== REFRENCE POINT ===== and ==== AFTER SCAVENGE ==== (XXXXXX)

Wherein the scavenge occurs after the connection goes idle and got cleaned by the system internals.

device-monitor-230703-155511 copy.log

Note that the debug is turned off, and further I've manually removed unrelated logs (publishes and "H4P" logs).

AdityaHPatwardhan commented 1 year ago

Hi @HamzaHajeir I dont think the error stems from a code that is a part of esp-idf. We have not seen such error with any of the esp-idf examples. It is highly likely that the issue is caused by the patch that you have applied. I would recommend playing close attention to the part of code which is triggered when 1) New session is created 2) Old session is destroyed

I am closing the issue since it doesn't seem to be related to esp-idf. Please feel free to re-open in you still think it is an esp-idf issue and would be fixed by changing some part of code from esp-idf.

Thanks, Aditya

HamzaHajeir commented 1 year ago

Hi Aditya,

I've read the code again, there's no session initiation in altcp_tls_mbedtls.c, nor in application code.

It's proven by logs (by inserted prints) that altcp_mbedtls_dealloc() being called per connection close, which do call mbedtls_ssl_free().

As servers share the configurations, corresponding keys and certificated wont be freed unless the LISTEN connection gets closed.

It is highly likely that the issue is caused by the patch that you have applied.

Does esp-idf use LWIP_ALTCP, or have someone tested with it and confirmed the issue absence? If so, please inform with the exact version of LWIP_ALTCP used in tests.

Alternatively, can you confirm a successful memory leak test is done for TLS servers? Please inform with such projects.

Regards,

AdityaHPatwardhan commented 1 year ago

Hi @HamzaHajeir,

esp-idf example are tested against possible leaks in multiple areas. Though I won't say every leak always get caught. Just to check here, I have attached a log where I have attempted more than 200 connections to a TLS server created with https_server example. I dont observe any leak other than some memory changes due to socket buffers (which are released after some time hence might be confused with memory leak). leak_test.log I am re-opening the issue just in case, but I think if we are making changes to lwip and then some leak is occurring, wouldn't it be better to raise issue at https://github.com/lwip-tcpip/lwip (through appropriate means) In order to be sure about that, is it possible for you to provide some kind of reproducible code which does not use any external patch but reproduces memory leak?

reinismu commented 1 year ago

I have noticed similar issues in my app. I host HTTPS server with WebSocket. At some times the API, just stops working. (Other logic from what I can tell is still fine) Seems to be random, but I have noticed if there are more tasks happening in parallel the higher risk of API breaking.

esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780
esp_https_server: esp_tls_create_server_session failed
httpd: httpd_accept_conn: session creation failed
httpd: httpd_server: error accepting new connection
esp_https_server: performing session handshake

esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x0050
esp_https_server: esp_tls_create_server_session failed
httpd: httpd_accept_conn: session creation failed
httpd: httpd_server: error accepting new connection

It works fine even after these errors

From what I can tell there is a pattern where it breaks

esp_https_server: performing session handshake
...
esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780
esp_https_server: esp_tls_create_server_session failed

It doesn't log anything about httpd and at this point API is broken

I use v4.4

reinismu commented 1 year ago

I switched to WolfSSL https://github.com/esp-rs/esp-idf-sys/issues/226 and I still get the same issue

Failed after (Interesting that with error code NO_PRIVATE_KEY = -317, /* need the private key */)

E (158110) esp-tls-wolfssl: wolfSSL_accept returned -1, error code: -313
E (158110) esp_https_server: esp_tls_create_server_session failed

other times (Here it handlres it correctly without stucking)

I (565640) esp_https_server: performing session handshake
E (567170) esp-tls-wolfssl: wolfSSL_accept returned -1, error code: -308
E (567170) esp_https_server: esp_tls_create_server_session failed
W (567175) httpd: httpd_accept_conn: session creation failed
W (567180) httpd: httpd_server: error accepting new connection
I (567190) esp_https_server: performing session handshake
E (568720) esp-tls-wolfssl: wolfSSL_accept returned -1, error code: -313
E (568720) esp_https_server: esp_tls_create_server_session failed
W (568725) httpd: httpd_accept_conn: session creation failed
W (568730) httpd: httpd_server: error accepting new connection

I reproduce these errors by refreshing https endpoint in Firefox without accepted certificate

=== At one point I got

W (898340) httpd_ws: httpd_ws_get_frame_type: Failed to read header byte (socket FD invalid), closing socket now

API stopped working as well

HamzaHajeir commented 1 year ago

I have attached a log where I have attempted more than 200 connections to a TLS server created with https_server example. I dont observe any leak other than some memory changes due to socket buffers (which are released after some time hence might be confused with memory leak). leak_test.log

I've checked it, it depends on latest ESP-IDF (v5.2) which is accompanied with major updated mbedTLS version (v3 instead of v2.28.3), so it won't be helpful to ensure the issue. (It could be any of ESP-IDF v4.4, mbedTLS 2.28.3 or lwip and furthers).

HamzaHajeir commented 1 year ago

I have noticed similar issues in my app. I host HTTPS server with WebSocket. At some times the API, just stops working. (Other logic from what I can tell is still fine) Seems to be random, but I have noticed if there are more tasks happening in parallel the higher risk of API breaking.

esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780
esp_https_server: esp_tls_create_server_session failed
httpd: httpd_accept_conn: session creation failed
httpd: httpd_server: error accepting new connection
esp_https_server: performing session handshake

esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x0050
esp_https_server: esp_tls_create_server_session failed
httpd: httpd_accept_conn: session creation failed
httpd: httpd_server: error accepting new connection

It works fine even after these errors

From what I can tell there is a pattern where it breaks

esp_https_server: performing session handshake
...
esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780
esp_https_server: esp_tls_create_server_session failed

It doesn't log anything about httpd and at this point API is broken

I use v4.4

Per error, check this line.

You might activate mbedTLS debug to see what does really happen after that point.

reinismu commented 1 year ago

Per error, check this line.

You might activate mbedTLS debug to see what does really happen after that point.

Based that WolfSSL had the same issue as mbedtls I assume issue is in my code or esp-idf. Could be my issue is not related with your. You could try sending multiple failed SSL handshakes and see if that breaks stuff

Update: I set CONFIG_FREERTOS_UNICODE=y and I can't seem to reproduce the issue anymore. Will try more, but it could be some race condition related

Warning (355257) httpd_ws: httpd_ws_get_frame_type: Failed to read header byte (socket FD invalid), closing socket now still kills the API :/

AdityaHPatwardhan commented 1 year ago

Thanks for the update @reinismu @HamzaHajeir, I will take a detailed look once again and let you know. It could be that I might have missed something last time.

reinismu commented 1 year ago

Thanks for the update @reinismu @HamzaHajeir, I will take a detailed look once again and let you know. It could be that I might have missed something last time.

Thanks for looking into it :)

I started to check this again and noticed that if I enable debug logs CONFIG_MBEDTLS_DEBUG=y I don't get esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780 and server doesn't get stuck anymore...

Maybe this could be related that I run at 240Mhz?

CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240=y
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ=240

Update: Forgot to set WSS connection to ESP32S3. With websocket connection active it still silently crashed

Steps I did:

output.log

reinismu commented 1 year ago

Ahh this bug is funny... Now I added a bit more logging around the suspected place and if I turn verbose logging for "httpd_sess" (esp_idf_svc::log::set_target_level("httpd_sess", log::LevelFilter::Trace);) it doesn't crash anymore. Now I'm not even sure how to dig deeper.

Maybe someone has had similar issues?

I assume it's not compiler related as debugging code is there, just I don't log it. Maybe some timing thing somewhere

Update:

Managed to get logs to work. Maybe just a coincidence. It seems that my code gets stuck at hd->config.close_fn(hd, session->fd); From what I know this callback is set in https://github.com/esp-rs/esp-idf-svc/blob/master/src/http/server.rs#L264

Will investigate further. Seems that issue could be related to it

AdityaHPatwardhan commented 10 months ago

Hi @HamzaHajeir, I have tested the functioning of esp_https_server at my end. I was not able to reproduce this issue. @reinismu were you able to reproduce this once again?

reinismu commented 10 months ago

I did solve my issue. In the end it was my code. It did deadlock in some cases. ESP websockets were not threadsafe, so I had to change how I used them.

HamzaHajeir commented 10 months ago

Hi @AdityaHPatwardhan, I'm a way of HTTPS webserver really, but I've just launched a normal test (trying to stress manually, debug off), I'll update with further notes.

HamzaHajeir commented 10 months ago

@AdityaHPatwardhan The testbed which run for ~30 hours started perfectly but ended up with serious issues:

The performance was in degradation over time, At first it started perfectly with 6 websockets secure (wss) clients simultaneously, this number has degraded over time due to memory leaks in the internal memory (For the certificates to take place), until there was only one client can connect at a time.

It's worth noting:

If you believe it's not an issue in esp-idf, mbedtls port, or mbedtls itself, please feel free to close.

AdityaHPatwardhan commented 9 months ago

Hi @HamzaHajeir, I tried the https server again with using esp-idf as baseline. I did not find any visual leaks that might explain the issue. I am closing the issue since I am unable to reproduce the issue at my end and my own tests did not give any indication of memory leak. Thanks for the issue. Please feel free to re-open if you find any additional material with which I can reproduce the issue at my end.