Hi, I am trying to implement tls/mqtt using mbedtls library. I got reference from below link: “https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/MCUXpresso/FRDM-K64F/FRDM-K64F_lwip_lwip_mqtt_bm/source”
In this, after performing “mqtt_do_connect()” step, it gives me error in “mqtt_do_tls_handshake()” step.
In mbedtls_ssl_handshake() function, ‘if( ssl == NULL || ssl->conf == NULL )’ this condition is set to true because ssl->conf is null and it returns me an error MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
Here is my TLS init code:
mbedtls_entropy_init( &stentropy );
mbedtls_ctr_drbg_init( &stctr_drbg );
mbedtls_x509_crt_init( &stcacert );
mbedtls_ssl_config_init( &stconf );
mbedtls_ssl_init( &stssl );
if( ( ret = mbedtls_ssl_config_defaults( &stconf,
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
LWIP_DEBUGF(MQTT_APP_DEBUG_TRACE,( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ));
printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
mbedtls_ssl_conf_rng( &stconf, mbedtls_ctr_drbg_random, &stctr_drbg );
mbedtls_ssl_conf_dbg( &stconf, my_debug, stdout );
if(CONFIG_BROKER_HOST_NAME!=NULL && ( ret = mbedtls_ssl_set_hostname(&stssl, CONFIG_BROKER_HOST_NAME) ) != 0 )
LWIP_DEBUGF(MQTT_APP_DEBUG_TRACE,( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret ));
printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
/* the SSL context needs to know the input and output functions it needs to use for sending out network traffic. */
// mbedtls_ssl_set_bio(&ssl, &mqtt_client, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_ssl_set_bio(&stssl, stAPP_MQTT_Msg_t->pstmqtt_client, MT_Mbedtls_Send, MT_Mbedtls_Recv, NULL);
Thanks in advance.
Your TLS init seems to be correct.
The call to
mbedtls_ssl_setup(&stssl, &stconf); should have assigned
ssl->conf, so it shouldn’t have been NULL.
Have you checked that
mbedtls_ssl_config_defaults() didn’t fail?
Mbed TLS Support
My this function ‘mbedtls_ssl_setup’ executing successfully now. Actually it was a problem with heap size memory, I increased heap memory size and now this function is working properly.
But now as I move further for initializing, I am again facing issue while tls handshaking. My
- TLS_Init function processed properly without error
- It tries to connect to mqtt broker i.e. mosquitto on my local machine using ‘mqtt_do_connect’ and it return ok
- It then goes for handshake using ‘mqtt_do_tls_handshake’; in this
a. mbedtls_ssl_handshake-> mbedtls_ssl_handshake_step-> ssl_write_client_hello->
mbedtls_ssl_write_record-> ssl_generate_random it executes this loops
b. In ‘ssl_generate_random’ function: it executes ‘if( ( ret = ssl->conf->f_rng( ssl->conf-
>p_rng, p, 28 ) ) != 0 )’
(i) in this: mbedtls_ctr_drbg_random-> mbedtls_mutex_lock-> threading_mutex_fail
THIS RETURNS ME ERROR: “MBEDTLS_ERR_THREADING_BAD_INPUT_DATA”
i.e. -28 error code
What could be the problem and it’s relevant solution for this error?
I compared my TLS_init code with the code that I refered from internet. I checked that I have not included below lines of code:
if( ( ret = mbedtls_ctr_drbg_seed( &stctr_drbg, mbedtls_entropy_func, &stentropy,
(const unsigned char *) pers,
strlen(pers ) ) ) != 0 )
LWIP_DEBUGF(MQTT_APP_DEBUG_TRACE,( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ));
printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Now I added this and got error no. -52; so i debug my code and found that the same function i.e. mbedtls_mutex_lock-> threading_mutex_fail returns me an error.
The sequence of code flow is like:
mbedtls_ctr_drbg_seed-> mbedtls_ctr_drbg_reseed-> 0 != ctx->f_entropy( ctx->p_entropy, seed,
ctx->entropy_len ) -> mbedtls_entropy_func-> mbedtls_mutex_lock-> threading_mutex_fail and error “MBEDTLS_ERR_THREADING_BAD_INPUT_DATA”
kindly guide me through this.
AS the errors imply, these are threading issues. Does your system support pthread? How is threading configured in your system for Mbed TLS?
AS you can see,
MBEDTLS_ERR_THREADING_BAD_INPUT_DATA is returned in case
MBEDTLS_THREADING_PTHREAD is defined if:
( mutex == NULL || ! mutex->is_valid ) however since you call
mbedtls_entropy_init() so this shouldn’t be the case.
Is it possible you have defined
MBEDTLS_THREADING_ALT but forgot to set your alternative implementation of threading with
mbedtls_threading_set_alt()? In this case, the defult lock functionality will always fail with the error code you received because
threading_mutex_fail is set by default.
In my project:
I have not used ‘mbedtls_threading_set_alt()’ yet. But I tried to implement it as:
/* Setup the thread callbacks */
It gives me below errors:
undefined first referenced
symbol in file
In threading.c file, all these functions are declared under macro definition of:
#if defined(MBEDTLS_THREADING_PTHREAD)… and in my project this macro is undefined.
Then I tried to define this macro and compile my code again. It gives me 77 error in diferent mbedtls files:
gmake: *** [Third Party/mbedtls/library/ecp_curves.obj] Error 1
“C:\ti…\third_party\mbedtls\include\mbedtls\check_config.h”, line 616: fatal error #35: #error directive: “MBEDTLS_THREADING_ALT defined, but not all prerequisites”
If you define
MBEDTLS_THREADING_PTHREAD, and your system has pthread, why do you need
As you can see in the
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
#if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_C defined, single threading implementation required"
You can’t have both
If your syustem doesn’t have pthread, you will need to supply your own threading implementation which is not pthread. If your system has pthread, you don’t need
I am unaware of mbedtls library’s detail i.e. which macro is used for which purpose. I am trying to connect to my secure mqtt broker using mbedtls over lwip, and for that I have referred internet examples i.e. github. If you have any link which explains in short and simple way that how to use mbedtls with examples then do share it. I referred below links:
Now, I have commented “MBEDTLS_THREADING_ALT” macro and uncommented “MBEDTLS_THREADING_PTHREAD” macro in config.h file. So it gives me below errors:
- identifier “PTHREAD_MUTEX_INITIALIZER” is undefined threading.c
- invalid redeclaration of type name “mbedtls_threading_mutex_t” (declared at line 49 of “C:\ti…\third_party\mbedtls\include\mbedtls\threading.h”) external location: C:\ti…\third_party\mbedtls\ti\port\threading_alt.h C/C++ Problem
in the Mbed TLS Knowledge base you will find several articles that will interest you.
I would suggest you start with the following:
As for the compilation errors you are receiving:
PTHREAD_MUTEX_INITIALIZER is part of the pthread library. If you don’t have it, then you probably don’t have pthread in your system, and you should keep the previous configiuration of
MBEDTLS_THREADING_ALT defined and
MBEDTLS_THREADING_PTHREAD undefined. You should call
mbedtls_threading_set_alt with the threading functions that are relevant for your platform.
- This error is because you already defined
mbedtls_threading_mutex_t in your
threading_alt.h file ( which is not part of the Mbed TLS release), and it collides with the default structure in Mbed TLS, when
MBEDTLS_THREADING_PTHREAD is defined.
Were you able to achieve this? I’m trying to implement the MQTT on TM4C1294NCPDT without RTOS.
I have implemented HTTPS but now we want yo have MQTT.
Can you guide me with this?
you can also email me directly at firstname.lastname@example.org.
For implementing MQTT on TM4C1294NCPDT you required below things:
- LWIP v2
- Latest tivaware having mqtt library support
- Set heap size >= 72K bytes
- If you want to use secure mqtt, then you must have mbedtls library
Thank you for responding to my question.
I’m using LWIP 2.0.0
Tivaware: which version are you using? I can download the same.
Heap size shouldn’t be problem for me I think.
I have implemented mbedTLS library to support HTTPS on my board. So I can rebuild it as per my requirements.
Can you share the the function or file that is making the call and getting the subscription to the broker and getting the data to the client? basically the implementation of mqtt on tivaC. I just want to know what are the steps required and how to execute them. This will save me a lot of time. You can email me as well if you don’t want to share here.
Thank you again.
I am using tivaware TivaWare_C_Series-220.127.116.11.
For code/function I have used/referred below link and implemented same in my code & it worked for me:
I have a question regarding Tivaware with MQTT support. I have downloaded tivaware 18.104.22.1685 which is the latest release and I didn’t find anything related to MQTT. So what did you mean by MQTT support? As far as I know, tiva doesn’t support MQTT on tivaC.
Can you please elaborate what I need to do?
In tivaware 22.214.171.1245 they have given lwip-1.4.1; replace it with lwip 2.0. Or else download lwip 2.0 and copy mqtt relevant files to tivaware and use it.
By Mqtt relevant files do you mean lqip_mqtt.c, mqtt.c from mcuoneclipse/Examples/MCUXpresso/FRDM-K64F/FRDM-K64F_lwip_lwip_mqtt_bm/source at master · ErichStyger/mcuoneclipse · GitHub ??
Because as I said earlier there’s no reference of mqtt in lwip 2.0.0
Create 1 folder namely: ‘mqtt’ in C:\ti\TivaWare_C_Series-126.96.36.1995\third_party\lwip-1.4.1\apps.
Rename mqtt_new.c/.h files (from given lnik) as mqtt.c/.h and then Copy mqtt.c and mqtt.h files in this folder.
Add lwip_mqtt.c/.h files to your project.
Thank you for the response. I did that and I have some questions regarding some missing files.
I copied the mqtt_new.c and there are few files which I cant locate anywhere.
and these files are not included in mqtt.c
can you tell me where I can find these and if not, is it okay to use mqtt.c instead of mqtt_new.c from that link?
Modify include files as below and try again:
// #include “rng1.h”
Thank you for the reply. I updated my file with these change.
I have question regarding other include files, namely RNG1.c/h . I believe the code uses ring buffer to get data using mbedtls_net_recv function. If I include this file, it has various include files such as CS1.h Do i need those files?
If not then how have you implemented the data to be sent and received by TLS and MQTT layers.