Dear community,
currently I am trying to connect the XDK to a MQTT Broker over TLS. I used the MQTT Paho example (or here) and it works fine when I don't use the TLS connection.
My plan is that the XDK should connect to the MQTT Broker like this:
- The MQTT Broker issues a .X509 certificate and private key for my XDK
- I put the .X509 certificate and private key on the SD card of my XDK
- I connect the XDK using MQTT over TLS to the MQTT Broker with the stored certificate
- I send MQTT messages to the Broker
The problem is that it doesn't seem to work this way. I quickly found the function in the MQTT Paho example that allows me to connect via TLS in the file mqttXDK.c:
int TLSConnectNetwork(Network *n, char* addr, int port, SlSockSecureFiles_t* certificates, unsigned char sec_method, unsigned int cipher, char server_verify)
The important type here is SlSockSecureFiles_t that comes from socket.h in the Wifi module of the SDK. I read a lot about that and the best is this wiki entry from Texas Instruments that describes how to use the TLS functionalities of socket.h. As it describes it is necessary to use a tool called "Uniflash" from Texas Instruments to flash the certificate onto the XDK as part of the firmware. Once this is done I could use it like that:
SlSockSecureFiles_t SecureFiles;
sockSecureFiles.secureFiles[0] = "/cert/privateKey.key"; // mapping private key, 0 file not exist
sockSecureFiles.secureFiles[1] = "/cert/certifacte.cer"; // mapping certificate, 0 file not exist
sockSecureFiles.secureFiles[2] = 0; // mapping CA, 0 file not exist - I could put here the server CA to valide him as well
sockSecureFiles.secureFiles[3] = 0; // mapping certificate, 0 file not exist
TLSConnectNetwork(&network, MQTT_BROKER_NAME, MQTT_PORT, secureFiles, SL_SO_SEC_METHOD_TLSV1_2, SL_SEC_MASK_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, -453);
But this is not what I want; I need a way to put it on the SD card and read it from there. So I asked around and got the advice to look at the WiFiHostPgm.c file from here. In this software the certificate is flashed at runtime into the Wifi module of the XDK so that a Enterprise Network can be used (function UploadCertificate() ). The certificate for that is stored in a dummyCertificate.h file and can be replaced programmatically.
I could try to use this logic as well; I would first need to read both certificates from the SD card, flash them into Wifi module, restart the Wifi module and then proceed with my normal initialization... No flashing of certificates with some extra software required.
But there are some questions I have in mind, since I haven't implemented the certificate flashing at runtime yet and I appreciate any help on this.
- Why does the WifiHostPgm.c file flashes the Wifi module with a different firmware first? Is that required or could I flash the certificates right away?
- Does anybody can think about an easier way to archive what I want without that much effort?
- Does Uniflash works with the XDK? I could try to flash the certificates once to test if the MQTT connection works with flashed certificates before I invest time into implementing the certificate flashing at runtime.
- Do you think I will run into firmware / RAM size problems when I do all that? It's kind of a big stack I am planning to implement here: MQTT, SD card reading, runtime flashing, certificates in firmware, TLS etc. I am not familiar with embedded programming, but it sounds kinda huge for such small hardware... Do you guys with more experience think this will work?
I'm thankful for any help,
Fabian