Hi, I wonder if anyone can tell me what I'm doing wrong.
I use a modified client1.c for getting payment objects from an aws
address, curl says that the connection uses "SSL connection using
TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256" if it's any use.
I can retrieve any single payment object which has a content length of
about 443 but when I try to get the entire record of payments of which
I only need the first or latest payment to check that I'm in sync, I
get "Last error was: -28928 - SSL - Bad input parameters to function".
This started at a certain point in the payment object accumulation
don't quite know when, the current record of all payments is 22526 and
it grows with every transaction.
The GET request for the all the payment objects ends in "/payments/"
and for a single object /payments/ has the objects id appended to it.
There must be a way to receive a truncated record of all payments.
Here is my latest config.h:
#ifndef MBEDTLS_CONFIG_H
#define MBEDTLS_CONFIG_H
/* System support */
//#define MBEDTLS_HAVE_ASM
#define MBEDTLS_HAVE_TIME
#define MBEDTLS_NO_PLATFORM_ENTROPY
#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
#define MBEDTLS_TEST_NULL_ENTROPY
#define MBEDTLS_ERROR_C
#define MBEDTLS_PLATFORM_C
//#define MBEDTLS_PLATFORM_EXIT_ALT
//#define MBEDTLS_PLATFORM_TIME_ALT
//#define MBEDTLS_PLATFORM_FPRINTF_ALT
//#define MBEDTLS_PLATFORM_PRINTF_ALT
//#define MBEDTLS_PLATFORM_SNPRINTF_ALT
//#define MBEDTLS_PLATFORM_VSNPRINTF_ALT
//#define MBEDTLS_PLATFORM_NV_SEED_ALT
//#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
/* mbed TLS feature support */
#define MBEDTLS_CIPHER_MODE_CBC
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
#define MBEDTLS_SSL_PROTO_TLS1_2
#define MBEDTLS_SSL_CLI_C
#define MBEDTLS_SSL_TLS_C
#define MBEDTLS_ENTROPY_C
#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
//experiment
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#define MBEDTLS_ECDH_C
#define MBEDTLS_ECP_C
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
#define MBEDTLS_RSA_C
#define MBEDTLS_OID_C
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_X509_CRT_PARSE_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_MD_C
#define MBEDTLS_BIGNUM_C
#define MBEDTLS_SHA256_C
#define MBEDTLS_PK_PARSE_C
#define MBEDTLS_PK_C
#define MBEDTLS_ASN1_PARSE_C
/* mbed TLS modules */
#define MBEDTLS_AES_C
/*
#define MBEDTLS_ASN1_WRITE_C
#define MBEDTLS_BIGNUM_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_CTR_DRBG_C
#define MBEDTLS_DES_C
#define MBEDTLS_MD_C
#define MBEDTLS_MD5_C
//#define MBEDTLS_RSA_C
#define MBEDTLS_SHA1_C
#define MBEDTLS_SHA256_C
*/
#define MBEDTLS_X509_USE_C
/* For test certificates */
#define MBEDTLS_BASE64_C
#define MBEDTLS_CERTS_C
#define MBEDTLS_PEM_PARSE_C
/* Limit memory use*/
#define MBEDTLS_SSL_MAX_CONTENT_LEN 16384
/* For testing with compat.sh */
//#define MBEDTLS_FS_IO
#include "check_config.h"
#endif /* MBEDTLS_CONFIG_H */
Thanks,
Dave P
Hi,
I'd like to compare a signing cert in a cert chain against a pinned cert.
Given two mbedtls_x509_crt (or two mbedtls_rsa_context), what is the
recommended way to compare them?
Thanks!
--
Nassim Eddequiouaq
Hello,
I don't see anything obviously wrong with the code. Heap fragmentation
is a possibility. A memory leak is also a possibility; we do fairly
extensive testing for memory leaks in unit tests, but this doesn't catch
unusual conditions (especially not recovering after a low-memory condition).
I think the first tool to investigate is to enable MBEDTLS_MEMORY_DEBUG.
This has a small cost in code size and gives you access to some
introspection functions mbedtls_memory_buffer_alloc_status() and
mbedtls_memory_buffer_alloc_max_get(). Call these functions on an error,
and also perhaps at other times for comparison.
There's also an option MBEDTLS_MEMORY_BACKTRACE which creates extremely
verbose logs. Those logs might be helpful, but they're so verbose that
they often aren't practical in a real-world application.
I notice that the allocation is for a little over 16kB. The default size
of the SSL input/output buffers is 16kB because that's the maximum size
of a message according to the specification. However you can usually get
away with a lot less, especially on IoT networks where the
infrastructure is geared towards much smaller messages. See
https://tls.mbed.org/kb/how-to/controlling_package_size
<https://tls.mbed.org/kb/how-to/controlling_package_size> for more
information on message sizes and buffer sizes. If the problem is a
memory leak, smaller buffers will only delay the failure. But if the
problem is that the application just needs a bit more heap space, this
could solve the problem.
Concretely, try setting MBEDTLS_SSL_IN_CONTENT_LEN and
MBEDTLS_SSL_OUT_CONTENT_LEN in the compile-time configuration to much
lower values. MBEDTLS_SSL_OUT_CONTENT_LEN can be as low as you like as
long as the handshake messages fit. MBEDTLS_SSL_IN_CONTENT_LEN has to be
large enough for the messages that you're sent.
Alternatively, enable the option MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH,
which results in smaller buffers but can make fragmentation worse due to
reallocations.
Hope this helps.
--
Gilles Peskine
Mbed TLS developer
On 01/08/2021 22:40, Alan Chen via mbed-tls wrote:
>
> I posted the question on the mbedTLS forum, only to realized that
> mbedTLS is now maintained by the project�s mailing list. Here is the
> copy of what I wrote:
>
> �
>
> *Occasionally*� I am getting MBEDTLS_ERR_SSL_ALLOC_FAILED from
> mbedtls_ssl_setup() during repeated HTTP partial content download.
> Since this problem happens very rarely, it is a bit difficult to
> troubleshoot.
>
> �
>
> I am running mbedTLS on a Microchip PIC32MZ MCU, connected to a
> LTE-M/NB-IoT modem. I have 128K static memory reserved for the library
> with MBEDTLS_PLATFORM_MEMORY defined in the config.h file. The MCU
> runs two main tasks - MQTT client, talking to the AWS MQTT broker, and
> HTTPS client, for downloading new firmware image from the AWS S3
> bucket over the air.
>
> �
>
> Due to the slowness and limited bandwidth of the LTE-M and NB-IoT
> technologies, the HTTP file download has to use the partial content
> GET, basically 2KB per request, until all ~700KB of data are received.
> During the course of the file download, one can see as many as 30
> disconnect and reconnect, and each time the TLS session would close
> down and re-open once the cell network is established. Here are some
> of my functions:
>
> �
>
> ```
>
> #define MBEDTLS_MAX_MEMORY_ALLOCATED��� (1024 * 128)
>
> static uint8_t tls_memory_buf[MBEDTLS_MAX_MEMORY_ALLOCATED];
>
> �
>
> // called in main()
>
> void mbedtls_mem_init(void)
>
> {
>
> ��� mbedtls_memory_buffer_alloc_init(tls_memory_buf, sizeof
> tls_memory_buf);
>
> }
>
> �
>
> void HTTPS_TLS_CLOSE(void)
>
> {
>
> ��� if (server_fd_https.fd != -1)
>
> ��� {�������
>
> ��������mbedtls_entropy_free(&entropy_https);
>
> ������� mbedtls_x509_crt_free(&cacert_https);
>
> ������� mbedtls_ctr_drbg_free(&ctr_drbg_https);
>
> ������� mbedtls_ssl_config_free(&conf_https);
>
> ������� mbedtls_ssl_free(&ssl_https);
>
> �
>
> ������� server_fd_https.fd = -1;
>
> ��� }
>
> }
>
> �
>
> bool HTTPS_TLS_OPEN(void)
>
> {
>
> ��� int ret;
>
> ��� const char *pers = "https_tls_wrapper";
>
> �
>
> �
>
> ��� server_fd_https.fd = 1;
>
> ��� mbedtls_debug_set_threshold(1);
>
> �
>
> ��� mbedtls_ssl_init(&ssl_https);
>
> ��� mbedtls_ssl_config_init(&conf_https);
>
> ��� mbedtls_ctr_drbg_init(&ctr_drbg_https);
>
> ��� mbedtls_x509_crt_init(&cacert_https);
>
> ���
>
> ����mbedtls_entropy_init(&entropy_https);���
>
> ����mbedtls_entropy_add_source(&entropy_https, my_https_entropy, NULL,
> sizeof my_https_random, MBEDTLS_ENTROPY_SOURCE_STRONG);
>
> ���
>
> ����ret = mbedtls_ctr_drbg_seed(&ctr_drbg_https, mbedtls_entropy_func,
> &entropy_https, (const unsigned char *)pers, strlen(pers));
>
> ��� if (ret != 0)
>
> ��� {
>
> ������� printf("%s: mbedtls_ctr_drbg_seed ERROR -0x%x\r\n",
> __FUNCTION__, -ret);
>
> ������� return false;
>
> ��� }
>
> �
>
> ��� ret = mbedtls_x509_crt_parse(&cacert_https, TRUSTED_ROOT_CA,
> TRUSTED_ROOT_CA_SIZE);
>
> ��� if (ret != 0)
>
> ��� {
>
> ������� printf("%s: mbedtls_x509_crt_parse cacert ERROR -0x%x\r\n",
> __FUNCTION__, -ret);
>
> ������� return false;
>
> ��� }
>
> ���
>
> ����ret = mbedtls_ssl_config_defaults(&conf_https,
> MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
> MBEDTLS_SSL_PRESET_DEFAULT);
>
> ��� if (ret != 0)
>
> ��� {
>
> ������� printf("%s: mbedtls_ssl_config_defaults ERROR -0x%x\r\n",
> __FUNCTION__, -ret);
>
> ������� return false;
>
> ��� }
>
> �
>
> ��� mbedtls_ssl_conf_verify(&conf_https, NULL, NULL);
>
> ��� mbedtls_ssl_conf_authmode(&conf_https, MBEDTLS_SSL_VERIFY_REQUIRED);
>
> ��� mbedtls_ssl_conf_rng(&conf_https, mbedtls_ctr_drbg_random,
> &ctr_drbg_https);
>
> ��� mbedtls_ssl_conf_dbg(&conf_https, my_https_debug, stdout);
>
> ��� mbedtls_ssl_conf_ca_chain(&conf_https, &cacert_https, NULL);
>
> �������
>
> ����mbedtls_ssl_conf_read_timeout(&conf_https, TLS_TIMEOUT_MS);
>
> �
>
> ��� HTTPS_SetHostname(); /* calling mbedtls_ssl_set_hostname */
>
> ���
>
> ����ret = mbedtls_ssl_setup(&ssl_https, &conf_https);
>
> ��� if (ret != 0)
>
> ��� {
>
> ������� printf("%s: mbedtls_ssl_setup ERROR -0x%x\r\n", __FUNCTION__,
> -ret);
>
> ������� return false;
>
> ��� }
>
> �
>
> ��� mbedtls_ssl_set_bio(&ssl_https, &server_fd_https,
> mbedtls_https_send, mbedtls_https_recv, NULL);
>
> �
>
> ��� return true;
>
> }
>
> ```
>
> Can someone please tell me if I am doing something inappropriate here?
> I am speculating that perhaps there is a memory leak or the heap
> becomes so fragmented that it fails on mbedtls_calloc(). The exact
> error message in my case is:
>
> �
>
> > ../mbedtls_lib/ssl_tls.c:5661: alloc(16717 bytes) failed
>
> �
>
> Thanks.
>
> �
>
> Alan Chen
>
>
>
> ------------------------------------------------------------------------
> <https://home.mcafee.com/utm_medium=email&utm_source=link&utm_campaign=sig-e…>
> Scanned by McAfee
> <https://home.mcafee.com/utm_medium=email&utm_source=link&utm_campaign=sig-e…>
> and confirmed virus-free.
>
>
Hi Dave and Gilles,
Perfect, so I will wait for the last 2.x (presumably the 2.28.x) strand
of the version later this year.
Thanks again.
Regards,
Matteo
------ Messaggio Originale ------
Da: mbed-tls(a)lists.trustedfirmware.org
A: Gilles.Peskine(a)arm.com; mbed-tls(a)lists.trustedfirmware.org
Inviato: giovedì 29 luglio 2021 15:33
Oggetto: Re: [mbed-tls] Mbed TLS: long term support versions
Sorry, just realised that myself! Gilles is correct, I should
have said 2.28.
Thanks
Dave
On 29/07/2021, 14:25, "mbed-tls on behalf of Gilles Peskine via
mbed-tls" <mbed-tls-bounces(a)lists.trustedfirmware.org on behalf of
mbed-tls(a)lists.trustedfirmware.org> wrote:
Off-by-one error! The current 2.x release is 2.27.0. Most
development
work is happening on 3.x but there will be at least one more 2.x
release: 2.28.0. The last 2.x release will become an LTS.
--
Gilles Peskine
Mbed TLS developer
On 29/07/2021 15:05, Dave Rodgman via mbed-tls wrote:
>
> Hi Matteo,
>
>
>
> We expect to release an LTS later this year. It’s likely to be
2.27,
> and very likely will be supported for the usual LTS period of 3
years.
>
>
>
> So if you are considering updating to a new LTS, you could use
2.26
> for prototyping in the short term until the LTS becomes
available. The
> upcoming LTS will be API-compatible with 2.26.
>
>
>
> Hope this helps,
>
>
>
> Dave
>
>
>
> *From: *mbed-tls <mbed-tls-bounces(a)lists.trustedfirmware.org> on
> behalf of "matteo.cogi--- via mbed-tls"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Reply to: *"matteo.cogi(a)alice.it" <matteo.cogi(a)alice.it>
> *Date: *Tuesday, 27 July 2021 at 07:44
> *To: *"mbed-tls(a)lists.trustedfirmware.org"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Subject: *[mbed-tls] Mbed TLS: long term support versions
>
>
>
> Dear all,
> I wish to know which are the future LTS (Long Term Support)
versions
> for Mbed TLS.
> In these last years I have been working with the 2.16.x, but I
read it
> will be maintained until at least the end of 2021 (
>
https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…
>
<https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…>
> ), so I am considering an update to a newer LTS version.
> However I don’t find which are the next LTS version and for how
much
> time they will be maintained.
> Thanks in advance.
> Regards,
> Matteo
>
>
--
mbed-tls mailing list
mbed-tls(a)lists.trustedfirmware.org
https://lists.trustedfirmware.org/mailman/listinfo/mbed-tls
--
mbed-tls mailing list
mbed-tls(a)lists.trustedfirmware.org
https://lists.trustedfirmware.org/mailman/listinfo/mbed-tls
(Note: I'm replying to the list. Please keep list conversations on the
list.)
Writing memory allocators is a specialty. It's not my specialty, and
apparently it's not your specialty either, so if we design a memory
allocator we're likely to end up with poor performance. On most systems,
the standard library malloc is designed by experts, and often fine-tuned
for the type of platform and usage (e.g. large or small pools, with or
without MMU, with or without cache).
memory_buffer_alloc uses an array to implement malloc.
One possible explanation for poor performance is if your allocator
doesn't align data nicely. Some platforms supports unaligned accesses
(e.g. accessing a 4-byte value at an address that isn't a multiple of 4)
but with a significant performance penalty (most others just crash). If
there's a cache, alignment with cache line boundaries can also help.
Another potential reason in a multithreaded program is not doing
synchronization efficiently, or doing it too often. Note that you do NOT
need synchronization if each thread has its own memory pool and no
thread ever modifies the data of another thread. Note, however, that
Mbed TLS caches some data in public/private key objects, so doing an
operation with a key modifies the key object, therefore each thread
would need to have its own copy of the key. There are undoubtedly other
likely reasons that I'm not thinking of.
Best regards,
--
Gilles Peskine
Mbed TLS developer
On 29/07/2021 22:38, Shariful Alam wrote:
> Hi Gilles,
> Hope you are well. The project that I'm currently working on requires
> static memory allocation instead of dynamic memory allocation. I was
> trying to use "memory_buffer_alloc()". Since memory_buffer_alloc()
> doesn't support multiple memory pools, seems like I can't use this
> function in my project.
>
> I have a working version of a modified bignum.c & bignum.h, where
> malloc is replaced with an array. The library is working. However, the
> performance is poor (~4 times slower). I was wondering what could
> cause this slow performance. I mean, I understand it will be slow, but
> I did not expect it to be 4 times slower.
>
> Is there any version, where an array was used instead of malloc? Or if
> you could point out some of the reasons for this library to slow down
> while using an array, I will be very grateful.
>
> Best regards,
> Shariful
>
>
> On Tue, Jul 20, 2021 at 2:22 PM Gilles Peskine <gilles.peskine(a)arm.com
> <mailto:gilles.peskine@arm.com>> wrote:
>
> Hi Shariful,
>
> You just call mbedtls_calloc() (or let other functions call it).
> It will use the memory pool set by mbedtls_memory_buffer_alloc_init().
>
> The memory_buffer_alloc module does not support multiple memory
> pools. If you want a separate pool for each thread's allocation
> for performance reasons on a multicore system, you'd better rely
> on your platform's built-in calloc(). It's likely to be
> fined-tuned for multicore operation. The built-in allocator in
> Mbed TLS is intended for highly resource-constrained systems where
> the basic platform doesn't even include an allocator.
>
> Best regards,
>
> --
> Gilles Peskine
> Mbed TLS developer
>
> On 20/07/2021 22:10, Shariful Alam wrote:
>> Hi Gilles,
>> Thank you very much, for your reply. Sorry to bother you again. I
>> am trying to follow your instructions. I have a question
>> regarding your suggestions.
>>
>> You said "applications call mbedtls_memory_buffer_alloc_init() in
>> the startup code of the initial thread, before creating other
>> threads. The alloc/free functions are thread-safe, but the
>> initialization and deinitialization functions aren't."
>>
>> I'm a little confused here. Say, if I call
>> mbedtls_memory_buffer_alloc_init() with a buffer in the main
>> thread, how did all other threads use this memory (or how do all
>> the threads know which memory block to use)?
>>
>> After calling mbedtls_memory_buffer_alloc_init() in the main
>> thread, I can get the address of the buffer and pass it to the
>> threads, do I have to call mbedtls_memory_buffer_alloc_init()
>> again inside each thread?
>>
>> Thanks,
>> Shariful
>>
>> On Mon, Jul 19, 2021 at 3:48 AM Gilles Peskine via mbed-tls
>> <mbed-tls(a)lists.trustedfirmware.org
>> <mailto:mbed-tls@lists.trustedfirmware.org>> wrote:
>>
>> Hi Shariful,
>>
>> First, please note that the library called PolarSSL with
>> functions like
>> rsa_private() and memory_buffer_alloc_init() has not been
>> supported for
>> several years. You should upgrade to Mbed TLS, with functions
>> like
>> mbedtls_rsa_private() and mbedtls_memory_buffer_alloc_init().
>> That being
>> said, the memory_buffer_alloc module works in the same way.
>>
>> Normally, applications call
>> mbedtls_memory_buffer_alloc_init() in the
>> startup code of the initial thread, before creating other
>> threads. The
>> alloc/free functions are thread-safe, but the initialization and
>> deinitialization functions aren't. If you must call
>> mbedtls_memory_buffer_alloc_init() after creating other
>> threads, make
>> sure that no thread calls mbedtls_calloc until
>> mbedtls_memory_buffer_alloc_init() has returned.
>>
>> The same principle applies to other parts of Mbed TLS that are
>> thread-safe. For example, only the RSA operations (encryption,
>> decryption, signature, verification, and also the low-level
>> functions
>> mbedtls_rsa_public() and mbedtls_rsa_private()) are
>> protected. So you
>> must finish setting up the RSA key inside one thread before
>> you pass a
>> pointer to other threads. Similarly, only
>> mbedtls_xxx_drbg_random() is
>> thread-safe, and the RNG setup (including
>> mbedtls_xxx_drgb_seed())
>> should be done as part of the initial application startup.
>>
>> Finally, note that mbedtls_rsa_private() alone cannot decrypt
>> a message:
>> all it does it to apply the private key operation. To decrypt
>> a simple
>> message encrypted with RSA-OAEP, call
>> mbedtls_rsa_rsaes_oaep_decrypt()
>> or mbedtls_rsa_pkcs1_decrypt() with a key set up for
>> MBEDTLS_RSA_PKCS_V21 encoding. To use the legacy PKCS#1v1.5
>> mechanism,
>> call mbedtls_rsa_rsaes_pkcs1_v15_decrypt() or
>> mbedtls_rsa_pkcs1_decrypt() with a key set up for
>> .MBEDTLS_RSA_PKCS_V15.
>> To decrypt a message using a RSA FDH hybrid scheme, you do
>> need to call
>> mbedtls_rsa_private() since Mbed TLS doesn't support it
>> natively, but
>> what this gives you is the intermediate secret from which you
>> then need
>> to derive a symmetric key, not the message itself.
>>
>> Best regards,
>>
>> --
>> Gilles Peskine
>> Mbed TLS developer
>>
>> On 18/07/2021 07:16, Shariful Alam via mbed-tls wrote:
>> > Hello,
>> > I have a simple example code to decrypt an
>> encrypted message using
>> > *rsa_private()*. I use *memory_buffer_alloc_init(), *in
>> order to use
>> > a static memory for the computation. I want to run my code
>> > concurrently. My code works with a single pthread. However,
>> when I try
>> > to run more than one thread my program fails to decrypt.
>> >
>> > ** I check the same code
>> without *memory_buffer_alloc_init(), *it
>> > works concurrently, without any issues at all.
>> >
>> > Therefore, I believe, the issue that I'm facing is
>> coming from the use
>> > of static memory(e.g. *memory_buffer_alloc_init()*). The
>> documentation
>> > of memorry_buffer_alloc.h shows,
>> >
>> > /**
>> >
>> > * \brief Initialize use of stack-based memory allocator.
>> >
>> > * The stack-based allocator does memory
>> management
>> > inside the
>> >
>> > * presented buffer and does not call malloc()
>> and free().
>> >
>> > * It sets the global polarssl_malloc() and
>> > polarssl_free() pointers
>> >
>> > * to its own functions.
>> >
>> > * (Provided polarssl_malloc() and
>> polarssl_free() are
>> > thread-safe if
>> >
>> > * POLARSSL_THREADING_C is defined)
>> >
>> > *
>> >
>> > * \note This code is not optimized and provides a
>> straight-forward
>> >
>> > * implementation of a stack-based memory
>> allocator.
>> >
>> > *
>> >
>> > * \param buf buffer to use as heap
>> >
>> > * \param len size of the buffer
>> >
>> > *
>> >
>> > * \return 0 if successful
>> >
>> > */
>> >
>> >
>> > So, I added the following configuration to the *config.h*
>> file
>> >
>> > 1. #define POLARSSL_THREADING_PTHREAD
>> > 2. #define POLARSSL_THREADING_C
>> >
>> > But I'm still getting errors while decrypting. Any help on
>> how to fix
>> > this? or what else should I add into the config.h file to
>> > make *memory_buffer_alloc_init() *thread-safe? Here is my
>> sample
>> > code: https://pastebin.com/uyW3vknt
>> <https://pastebin.com/uyW3vknt>
>> <https://pastebin.com/uyW3vknt <https://pastebin.com/uyW3vknt>>
>> >
>> > Thanks,
>> > Shariful
>> >
>>
>> --
>> mbed-tls mailing list
>> mbed-tls(a)lists.trustedfirmware.org
>> <mailto:mbed-tls@lists.trustedfirmware.org>
>> https://lists.trustedfirmware.org/mailman/listinfo/mbed-tls
>> <https://lists.trustedfirmware.org/mailman/listinfo/mbed-tls>
>>
>
I posted the question on the mbedTLS forum, only to realized that mbedTLS is now maintained by the project's mailing list. Here is the copy of what I wrote:
*Occasionally* I am getting MBEDTLS_ERR_SSL_ALLOC_FAILED from mbedtls_ssl_setup() during repeated HTTP partial content download. Since this problem happens very rarely, it is a bit difficult to troubleshoot.
I am running mbedTLS on a Microchip PIC32MZ MCU, connected to a LTE-M/NB-IoT modem. I have 128K static memory reserved for the library with MBEDTLS_PLATFORM_MEMORY defined in the config.h file. The MCU runs two main tasks - MQTT client, talking to the AWS MQTT broker, and HTTPS client, for downloading new firmware image from the AWS S3 bucket over the air.
Due to the slowness and limited bandwidth of the LTE-M and NB-IoT technologies, the HTTP file download has to use the partial content GET, basically 2KB per request, until all ~700KB of data are received. During the course of the file download, one can see as many as 30 disconnect and reconnect, and each time the TLS session would close down and re-open once the cell network is established. Here are some of my functions:
```
#define MBEDTLS_MAX_MEMORY_ALLOCATED (1024 * 128)
static uint8_t tls_memory_buf[MBEDTLS_MAX_MEMORY_ALLOCATED];
// called in main()
void mbedtls_mem_init(void)
{
mbedtls_memory_buffer_alloc_init(tls_memory_buf, sizeof tls_memory_buf);
}
void HTTPS_TLS_CLOSE(void)
{
if (server_fd_https.fd != -1)
{
mbedtls_entropy_free(&entropy_https);
mbedtls_x509_crt_free(&cacert_https);
mbedtls_ctr_drbg_free(&ctr_drbg_https);
mbedtls_ssl_config_free(&conf_https);
mbedtls_ssl_free(&ssl_https);
server_fd_https.fd = -1;
}
}
bool HTTPS_TLS_OPEN(void)
{
int ret;
const char *pers = "https_tls_wrapper";
server_fd_https.fd = 1;
mbedtls_debug_set_threshold(1);
mbedtls_ssl_init(&ssl_https);
mbedtls_ssl_config_init(&conf_https);
mbedtls_ctr_drbg_init(&ctr_drbg_https);
mbedtls_x509_crt_init(&cacert_https);
mbedtls_entropy_init(&entropy_https);
mbedtls_entropy_add_source(&entropy_https, my_https_entropy, NULL, sizeof my_https_random, MBEDTLS_ENTROPY_SOURCE_STRONG);
ret = mbedtls_ctr_drbg_seed(&ctr_drbg_https, mbedtls_entropy_func, &entropy_https, (const unsigned char *)pers, strlen(pers));
if (ret != 0)
{
printf("%s: mbedtls_ctr_drbg_seed ERROR -0x%x\r\n", __FUNCTION__, -ret);
return false;
}
ret = mbedtls_x509_crt_parse(&cacert_https, TRUSTED_ROOT_CA, TRUSTED_ROOT_CA_SIZE);
if (ret != 0)
{
printf("%s: mbedtls_x509_crt_parse cacert ERROR -0x%x\r\n", __FUNCTION__, -ret);
return false;
}
ret = mbedtls_ssl_config_defaults(&conf_https, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
if (ret != 0)
{
printf("%s: mbedtls_ssl_config_defaults ERROR -0x%x\r\n", __FUNCTION__, -ret);
return false;
}
mbedtls_ssl_conf_verify(&conf_https, NULL, NULL);
mbedtls_ssl_conf_authmode(&conf_https, MBEDTLS_SSL_VERIFY_REQUIRED);
mbedtls_ssl_conf_rng(&conf_https, mbedtls_ctr_drbg_random, &ctr_drbg_https);
mbedtls_ssl_conf_dbg(&conf_https, my_https_debug, stdout);
mbedtls_ssl_conf_ca_chain(&conf_https, &cacert_https, NULL);
mbedtls_ssl_conf_read_timeout(&conf_https, TLS_TIMEOUT_MS);
HTTPS_SetHostname(); /* calling mbedtls_ssl_set_hostname */
ret = mbedtls_ssl_setup(&ssl_https, &conf_https);
if (ret != 0)
{
printf("%s: mbedtls_ssl_setup ERROR -0x%x\r\n", __FUNCTION__, -ret);
return false;
}
mbedtls_ssl_set_bio(&ssl_https, &server_fd_https, mbedtls_https_send, mbedtls_https_recv, NULL);
return true;
}
```
Can someone please tell me if I am doing something inappropriate here? I am speculating that perhaps there is a memory leak or the heap becomes so fragmented that it fails on mbedtls_calloc(). The exact error message in my case is:
> ../mbedtls_lib/ssl_tls.c:5661: alloc(16717 bytes) failed
Thanks.
Alan Chen
________________________________
[https://secureimages.mcafee.com/common/affiliateImages/mfe/logo.png]<https://home.mcafee.com/utm_medium=email&utm_source=link&utm_campaign=sig-e…> Scanned by McAfee<https://home.mcafee.com/utm_medium=email&utm_source=link&utm_campaign=sig-e…> and confirmed virus-free.
Sorry, just realised that myself! Gilles is correct, I should have said 2.28.
Thanks
Dave
On 29/07/2021, 14:25, "mbed-tls on behalf of Gilles Peskine via mbed-tls" <mbed-tls-bounces(a)lists.trustedfirmware.org on behalf of mbed-tls(a)lists.trustedfirmware.org> wrote:
Off-by-one error! The current 2.x release is 2.27.0. Most development
work is happening on 3.x but there will be at least one more 2.x
release: 2.28.0. The last 2.x release will become an LTS.
--
Gilles Peskine
Mbed TLS developer
On 29/07/2021 15:05, Dave Rodgman via mbed-tls wrote:
>
> Hi Matteo,
>
>
>
> We expect to release an LTS later this year. It’s likely to be 2.27,
> and very likely will be supported for the usual LTS period of 3 years.
>
>
>
> So if you are considering updating to a new LTS, you could use 2.26
> for prototyping in the short term until the LTS becomes available. The
> upcoming LTS will be API-compatible with 2.26.
>
>
>
> Hope this helps,
>
>
>
> Dave
>
>
>
> *From: *mbed-tls <mbed-tls-bounces(a)lists.trustedfirmware.org> on
> behalf of "matteo.cogi--- via mbed-tls"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Reply to: *"matteo.cogi(a)alice.it" <matteo.cogi(a)alice.it>
> *Date: *Tuesday, 27 July 2021 at 07:44
> *To: *"mbed-tls(a)lists.trustedfirmware.org"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Subject: *[mbed-tls] Mbed TLS: long term support versions
>
>
>
> Dear all,
> I wish to know which are the future LTS (Long Term Support) versions
> for Mbed TLS.
> In these last years I have been working with the 2.16.x, but I read it
> will be maintained until at least the end of 2021 (
> https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…
> <https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…>
> ), so I am considering an update to a newer LTS version.
> However I don’t find which are the next LTS version and for how much
> time they will be maintained.
> Thanks in advance.
> Regards,
> Matteo
>
>
--
mbed-tls mailing list
mbed-tls(a)lists.trustedfirmware.org
https://lists.trustedfirmware.org/mailman/listinfo/mbed-tls
Off-by-one error! The current 2.x release is 2.27.0. Most development
work is happening on 3.x but there will be at least one more 2.x
release: 2.28.0. The last 2.x release will become an LTS.
--
Gilles Peskine
Mbed TLS developer
On 29/07/2021 15:05, Dave Rodgman via mbed-tls wrote:
>
> Hi Matteo,
>
>
>
> We expect to release an LTS later this year. It’s likely to be 2.27,
> and very likely will be supported for the usual LTS period of 3 years.
>
>
>
> So if you are considering updating to a new LTS, you could use 2.26
> for prototyping in the short term until the LTS becomes available. The
> upcoming LTS will be API-compatible with 2.26.
>
>
>
> Hope this helps,
>
>
>
> Dave
>
>
>
> *From: *mbed-tls <mbed-tls-bounces(a)lists.trustedfirmware.org> on
> behalf of "matteo.cogi--- via mbed-tls"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Reply to: *"matteo.cogi(a)alice.it" <matteo.cogi(a)alice.it>
> *Date: *Tuesday, 27 July 2021 at 07:44
> *To: *"mbed-tls(a)lists.trustedfirmware.org"
> <mbed-tls(a)lists.trustedfirmware.org>
> *Subject: *[mbed-tls] Mbed TLS: long term support versions
>
>
>
> Dear all,
> I wish to know which are the future LTS (Long Term Support) versions
> for Mbed TLS.
> In these last years I have been working with the 2.16.x, but I read it
> will be maintained until at least the end of 2021 (
> https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…
> <https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra…>
> ), so I am considering an update to a newer LTS version.
> However I don’t find which are the next LTS version and for how much
> time they will be maintained.
> Thanks in advance.
> Regards,
> Matteo
>
>
Hi Matteo,
We expect to release an LTS later this year. It’s likely to be 2.27, and very likely will be supported for the usual LTS period of 3 years.
So if you are considering updating to a new LTS, you could use 2.26 for prototyping in the short term until the LTS becomes available. The upcoming LTS will be API-compatible with 2.26.
Hope this helps,
Dave
From: mbed-tls <mbed-tls-bounces(a)lists.trustedfirmware.org> on behalf of "matteo.cogi--- via mbed-tls" <mbed-tls(a)lists.trustedfirmware.org>
Reply to: "matteo.cogi(a)alice.it" <matteo.cogi(a)alice.it>
Date: Tuesday, 27 July 2021 at 07:44
To: "mbed-tls(a)lists.trustedfirmware.org" <mbed-tls(a)lists.trustedfirmware.org>
Subject: [mbed-tls] Mbed TLS: long term support versions
Dear all,
I wish to know which are the future LTS (Long Term Support) versions for Mbed TLS.
In these last years I have been working with the 2.16.x, but I read it will be maintained until at least the end of 2021 ( https://github.com/ARMmbed/mbedtls/blob/development/BRANCHES.md#current-bra… ), so I am considering an update to a newer LTS version.
However I don’t find which are the next LTS version and for how much time they will be maintained.
Thanks in advance.
Regards,
Matteo