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.
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 );
return -1;
}
/* 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);
The call to mbedtls_ssl_setup(&stssl, &stconf); should have assigned&stconf to ssl->conf, so it shouldn’t have been NULL.
Have you checked that mbedtls_ssl_config_defaults() didn’t fail?
Regards,
Hi Ron,
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?
Hi Ron,
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 );
return -1;
}
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:
Hi @urvishah
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.
Hi @roneld01
In my project: #define MBEDTLS_THREADING_ALT
//#define MBEDTLS_THREADING_PTHREAD
I have not used ‘mbedtls_threading_set_alt()’ yet. But I tried to implement it as:
/* Setup the thread callbacks */
mbedtls_threading_set_alt(threading_mutex_init_pthread,
threading_mutex_free_pthread, threading_mutex_lock_pthread,
threading_mutex_unlock_pthread);
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 defined(MBEDTLS_THREADING_ALT)
#if !defined(MBEDTLS_THREADING_C) || defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_ALT defined, but not all prerequisites"
#endif
#define MBEDTLS_THREADING_IMPL
#endif
#if defined(MBEDTLS_THREADING_C) && !defined(MBEDTLS_THREADING_IMPL)
#error "MBEDTLS_THREADING_C defined, single threading implementation required"
#endif
#undef MBEDTLS_THREADING_IMPL
You can’t have both MBEDTLS_THREADING_PTHREAD and MBEDTLS_THREADING_ALT defined.
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 MBEDTLS_THREADING_ALT defined.
Regards
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
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.
Regards
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.
I have a question regarding Tivaware with MQTT support. I have downloaded tivaware 2.2.0.295 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.
In tivaware 2.2.0.295 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.
Create 1 folder namely: ‘mqtt’ in C:\ti\TivaWare_C_Series-2.2.0.295\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 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.