Skip to content

Commit 0b9fde5

Browse files
committed
Fix CACert load
Signed-off-by: John Walicki <[email protected]>
1 parent f73cf26 commit 0b9fde5

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

firmware/WatsonIoT/src/main.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,24 @@ void Connect2MQTTbroker() {
505505
}
506506

507507

508+
template<typename L> void loadFromFile(const char* fname, L&& load) {
509+
if (SPIFFS.exists(fname)) {
510+
File f = SPIFFS.open(fname);
511+
load(f, f.size());
512+
f.close();
513+
}
514+
}
515+
516+
517+
void loadCertificates(WiFiClientSecure* client) {
518+
SPIFFS.begin();
519+
loadFromFile(WATSON_IOT_PLATFORM_CA_PEM, [client](Stream& stream, size_t size){return client->loadCACert(stream, size);});
520+
//loadFromFile("/client.cert.pem", [client](Stream& stream, size_t size){return client->loadCertificate(stream, size);});
521+
//loadFromFile("/private.key.pem", [client](Stream& stream, size_t size){return client->loadPrivateKey(stream, size);});
522+
SPIFFS.end();
523+
}
524+
525+
508526
void Send10Seconds2Cloud() {
509527
// DynamicJsonDocument is stored on the heap
510528
// Allocate a ArduinoJson buffer large enough to 10 seconds of Accelerometer trace data
@@ -756,30 +774,7 @@ void setup() {
756774
#else
757775
sprintf(MQTT_HOST,"%s.messaging.internetofthings.ibmcloud.com",MQTT_ORGID); // Centrally managed
758776

759-
if( SPIFFS.begin(true) ) {
760-
Serial.printf("Opening Watson IoT Root CA PEM Chain : %s\r\n", WATSON_IOT_PLATFORM_CA_PEM);
761-
File pemfile = SPIFFS.open( WATSON_IOT_PLATFORM_CA_PEM );
762-
if( pemfile ) {
763-
char *RootCAPemChain = nullptr;
764-
size_t pemSize = pemfile.size();
765-
RootCAPemChain = (char *)malloc(pemSize);
766-
767-
if( pemSize != pemfile.readBytes(RootCAPemChain, pemSize) ) {
768-
Serial.printf("Reading %s pem server certificate chain failed.\r\n",WATSON_IOT_PLATFORM_CA_PEM);
769-
} else {
770-
Serial.printf("Read %s Root CA server certificate chain\r\n",WATSON_IOT_PLATFORM_CA_PEM);
771-
//Serial.println( RootCAPemChain );
772-
wifiClient.setCACert(RootCAPemChain);
773-
}
774-
free( RootCAPemChain );
775-
} else {
776-
Serial.println("Failed to open server pem chain.");
777-
}
778-
pemfile.close();
779-
} else {
780-
Serial.println("An error has occurred while mounting SPIFFS");
781-
}
782-
777+
loadCertificates( &wifiClient ); // Load the Watson IoT messaging.pem CA Cert from SPIFFS
783778
#endif
784779

785780
char mqttparams[100]; // Allocate a buffer large enough for this string ~95 chars

0 commit comments

Comments
 (0)