1

I'm using the Arduino framework on an ESP32 based board. The board has an SD card slot, but I'm not going to use it.

Anyway, I need to store somewhere the certificates. To the the firmware I've just hardcoded them into the code:

const char *rootCABuff = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\n" \
...
"hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq\n" \
"-----END CERTIFICATE-----\n";

const char certificateBuff[] = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDWTCCAkGgAwIBAgIUZXjPNnJFXMntXOki9BT+QmDFciUwDQYJKoZIhvcNAQEL\n" \
...
"gGXtqlgPnenkZPTaBr9c8o+WjgQXFLM+Jwnw3/53dUI9zBcybTaptI63o3Ct\n" \
"-----END CERTIFICATE-----\n";

const char privateKeyBuff[] = \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"MIIEpAIBAAKCAQEAuuKsFy98/xsJclnWo0d+qm8KImLEILB64BbH7WaYi3NnznLg\n" \
...
"uHfBLQQDrA0HDRvYpAUNw/b2H4dpUK/nGM4FFVMP7V8RM2t2923nhQ==\n" \
"-----END RSA PRIVATE KEY-----\n";

Of course this is not suitable for production where each unit will have a different set of certificates. So wonder what is the best way to store them.

The obviously idea is into flash. So I tried to enhance my settings class, based upon the Preferences library. In the official documentation I read:

String values are currently limited to 4000 bytes. This includes the null terminator. Blob values are limited to 508000 bytes or 97.6% of the partition size - 4000 bytes, whichever is lower.

But if I do something like this:

Preferences prefs;
prefs.begin("settings", false);
Serial.println(prefs.freeEntries());

it return 460, even if I still have plenty of flash:

Flash: [=======   ]  73.8% (used 967234 bytes from 1310720 bytes)

Is there another library more suitable to store such a data that can be used with Preferences?

6
  • 1
    Hi mate, you could also use spiffs memory, check here, it may help you: diyprojects.io/… – k.Cyborg Nov 8 '20 at 8:15
  • 1
    I don't know this libraries but "freeEntries" does not look like something that returns a size in bytes. Looks like it is telling you how many more values you can store. (Similar to max number of files that some filesystems have, not total available free space.) – Mat Nov 8 '20 at 9:07
  • You don't need any library. How many certs you intend to store? For ESP32, any variable that declared as const is stored in flash memory, and internally it does not have to copy the data from flash to RAM but uses the Flash MMU to map the memory segments into either IRAM or normal RAM, so it doesn't take up your ESP32 RAM. Alternatively, you could stored it in SPIFFS or FS as a file. – hcheung Nov 8 '20 at 9:22
  • @hcheung Sorry I explained it wrong. I need to store only those certificates. The problem is I cannot change the code every time a program a new board! So I'm going to "download" the data via serial line, and save it into the flash at run-time. – Mark Nov 8 '20 at 11:33
  • 1
    @k.Cyborg, good advice. I will try spiffs hoping it can be used together the other stuff. – Mark Nov 8 '20 at 11:35
3

I don't know what you need that Preferences thing for, but I would use SPIFFS/FAT anyway. Simply store the certificate in a file in flash and read that file when you need it. The required space is no problem as you can choose the size for file system in the partition table and also you can upload the file (there is a plugin in the arduino IDE for this purpose) after flashing the firmware, so having different certificates on individual devices is no problem.

I saw that you tagged the question with 'eeprom': the esp has no eeprom but emulates it with a blob in flash, so using any flash library is just as fine. The esp also supports wear-leveling, which makes using flash an even better option as the dedicated eeprom-partition, which was used in an older version of the sdk, would wear out orherwise. The "eeprom" is just there to be consistent with the use of other arduino's eeproms.

2
  • Yeah, I've just tried it and it's very handy! Just a clarification: I'm using PlatformIO inside Visual Studio Code and I only found a way to put the desired files into a data folder to be uploaded. Is there something better? Because I will need to replace the files for each unit... – Mark Nov 8 '20 at 15:57
  • 1
    You can use the sdk's bare features, nsmely the tools mkspiffs and esptool.py to create the flash binary and to upload it. See this anser: arduino.stackexchange.com/a/51581/38721 – Sim Son Nov 8 '20 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.