load root CA from SPIFFS and pass to WiFiClientSecure

KevinHunter
Posts: 1
Joined: Mon Oct 01, 2018 8:02 am

load root CA from SPIFFS and pass to WiFiClientSecure

Postby KevinHunter » Mon Oct 01, 2018 8:20 am

Hi all,
Im still a newbie and Im trying no move working code I wrote for the ESP8266 to the ESP32 but its not working.

I have a root certificate that I have stored in SPIFFS and I want to pass it to WifiClientSecure. On the ESP8266 I do it this way (which works):

Code: Select all

  // Load CA file from SPIFFS
  File ca = SPIFFS.open("/site.cer", "r"); 
  if (!ca) {
    Serial.println("Failed to open ca ");
  }
  else
    Serial.println("Success to open ca");
  delay(1000);

  // Set server CA file
  if (wifiClient.loadCACert(ca))
    Serial.println("ca loaded");
  else
    Serial.println("ca failed");

}
My first problem was that loadCACert() isnt supported by WiFiClientSecure on the ESP32 so I changed it to setCACert(). The problem I have now is that when I pass the file into this method ( wifiClient.setCACert(ca);) i get the following error:

no suitable conversion function from "fs::File" to "const char *" exists

So my question is , how do I convert a File to a const char ??

Thanks
Kev

spestano
Posts: 1
Joined: Tue Oct 09, 2018 12:31 am

Re: load root CA from SPIFFS and pass to WiFiClientSecure

Postby spestano » Tue Oct 09, 2018 12:35 am

Try, reading your "ca" and store it in a buffer of type char then pass that buffer in your function.

xmoulin
Posts: 1
Joined: Wed Feb 17, 2021 6:01 pm

Re: load root CA from SPIFFS and pass to WiFiClientSecure

Postby xmoulin » Wed Feb 17, 2021 6:03 pm

I have the same issue... Do you solve your problem?

kfine100
Posts: 4
Joined: Wed May 20, 2020 7:00 am

Re: load root CA from SPIFFS and pass to WiFiClientSecure

Postby kfine100 » Thu May 13, 2021 8:13 am

I do this with the ESP32. Spestano is correct, you just have to load into a buffer. To give you an example, I define

char myCertificate[N_BYTES_CERTIFICATE];

where N_BYTES_CERTIFICATE needs to be about 1500, and then read and load the certificate like this

Code: Select all

myFServer.read2String(SPIFFS, REMOTE_CERT_FILE, myCertificate);
 //Serial.printf("\n%s\n", myCertificate);
 http_client_Secure.setCACert(myCertificate);
myFServer is just a class I created using SPIFFS.h and FS.h. The relevant read2String function is this:

Code: Select all

void fServices::read2String(fs::FS &fs, const char * path, char *myString){
;
    File file = fs.open(path);
    if(!file || file.isDirectory()){
        return;
    }

    int iChar = 0;
    while(file.available()){
      myString[iChar] = file.read();
      iChar++;
    }

    file.close();
}
Hope this helps.

Who is online

Users browsing this forum: No registered users and 53 guests