Hi,
I am currently working with the MIMXRT1180-EVK and need to implement MQTT over TLS in a bare-metal environment using the LwIP and mbedTLS stack. I have implemented the basic MQTT functionality, But for the TLS part, I noticed that the SDK does not provide an example specifically for MQTT with TLS integration.
I am looking for guidance or an example project that demonstrates how to configure and integrate TLS for secure MQTT communication. If there’s any advice, starting point, or reference that can help with this, it would be greatly appreciated.
Thank you in advance for your support!
Best regards,
Pavan Kumar S.
Hi @PavanKumarS
To securely communicate using MQTT over TLS, you’ll need to:
1. Configure and initialize the LwIP network stack.
2. Set up mbedTLS for secure communication.
3. Modify your existing MQTT client code to use mbedTLS for establishing secure connections.
So i think you can refer to the lwip_httpssrv_mbedTLS_bm_cm33 example.
This example includes the step 1 and step 2, you just need to modify your existing MQTT client code.
BR
Harry
Hi @PavanKumarS
1. You can import the lwip_httpssrv_mbedTLS_bm_cm33 example.
Before line 495,
Configure and initialize the LwIP network stack.
Set up mbedTLS for secure communication.
NEXT, you only need to complete two steps
Establish a Secure TCP Connection
1. Create a Raw TCP Connection:
Use LwIP to connect to the broker on port 8883 (standard MQTT over TLS port).
struct tcp_pcb *tcp_conn = tcp_new();
tcp_connect(tcp_conn, &broker_ip, MQTT_TLS_PORT, tcp_connected_callback);
2. Wrap the TCP Connection with TLS:
Use mbedtls_ssl_set_bio to bind the LwIP socket to the mbedTLS context:
mbedtls_ssl_set_bio(&ssl, tcp_conn, mbedtls_net_send, mbedtls_net_recv, NULL);
3. Perform the TLS Handshake:
int ret = mbedtls_ssl_handshake(&ssl);
if (ret != 0) {
printf("TLS handshake failed: -0x%x\n", -ret);
// Handle error
}
Integrate MQTT with TLS
1. Modify MQTT Client Initialization:
• Pass the mbedTLS ssl context to the MQTT client.
• Replace raw TCP read/write calls with mbedtls_ssl_read and mbedtls_ssl_write.
2. Publish and Subscribe:
• Ensure MQTT publish/subscribe calls are routed through the TLS layer.
3. Handle Broker Disconnects:
• Implement error handling for TLS and MQTT reconnections.
BR
Harry