Skip to content

Add a check for ca certificates file on storage #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 61 additions & 8 deletions src/flashFormatter/C33FlashFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if defined(ARDUINO_PORTENTA_C33)
#include "C33FlashFormatter.h"
#define BD_ERROR_OK 0
#include "certificates.h"

C33FlashFormatter::C33FlashFormatter():
_root(BlockDevice::get_default_instance()),
Expand All @@ -24,14 +25,11 @@ bool C33FlashFormatter::checkPartition()
return false;
}

if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
if(!checkCACertificatesPartition())
{
return false;
}

_sys_fs.unmount();
_sys_bd.deinit();

if (_user_bd.init() != BD_ERROR_OK)
{
return false;
Expand All @@ -55,14 +53,13 @@ bool C33FlashFormatter::formatPartition() {
MBRBlockDevice::partition(_root, 2, 0x0B, 5 * 1024 * 1024, 15 * 1024 * 1024);
MBRBlockDevice::partition(_root, 3, 0x0B, 15 * 1024 * 1024, 16 * 1024 * 1024);

int err = _sys_fs.reformat(&_sys_bd);
if (err) {
if(!flashCACertificates())
{
return false;
}

_sys_fs.unmount();
_user_data_fs = new LittleFileSystem("user");
err = _user_data_fs->reformat(&_user_bd);
int err = _user_data_fs->reformat(&_user_bd);
if (err) {
return false;
}
Expand All @@ -71,4 +68,60 @@ bool C33FlashFormatter::formatPartition() {
return true;
}

bool C33FlashFormatter::checkCACertificatesPartition()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it would be better to add a slightly modified version of checkCACertificatesPartition and flashCACertificates to the base class and reuse the same code for the H7 and C33. Do you agree?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering adding new interfaces to the base class like checkCACertificatesPartition(MBRBlockDevice bd, String base_path) and flashCACertificates(MBRBlockDevice bd, String base_path), I don't think it's possible to move the checkCACertificatesPartition and flashCACertificates to the base class for two reasons:

  • the base class is used for all other architectures, so they could not have the FATFileSystem and MBRBlockDevice lib.
  • moving the flashCACertificates to the base class it requires to include the certificates.h file for every architectures and this could cause an increase of storage usage on every targets

{
/*Inspired by the CertificateUploader.ino example for Portenta C33*/
if (_sys_bd.init() != BD_ERROR_OK || _sys_fs.mount(&_sys_bd) != FR_OK)
{
return false;
}

DIR *dir;
struct dirent *ent;

if ((dir = opendir("/sys")) == NULL) {
return false;
}

bool foundCert = false;
while ((ent = readdir (dir)) != NULL) {
String fullname = "/sys/" + String(ent->d_name);
if (fullname == "/sys/cacert.pem") {
foundCert = true;
break;
}
}
closedir (dir);

_sys_fs.unmount();
_sys_bd.deinit();
return foundCert;
}

bool C33FlashFormatter::flashCACertificates()
{
int err = _sys_fs.reformat(&_sys_bd);
if (err) {
return false;
}

int chunck_size = 128;
int byte_count = 0;
FILE* fp = fopen("/sys/cacert.pem", "wb");
while (byte_count < cacert_pem_len) {
if(byte_count + chunck_size > cacert_pem_len)
chunck_size = cacert_pem_len - byte_count;
int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp);
if (ret != 1) {
return false;
}
byte_count += chunck_size;
}
fclose(fp);

_sys_fs.unmount();

return true;
}

#endif
2 changes: 2 additions & 0 deletions src/flashFormatter/C33FlashFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class C33FlashFormatter : public FlashFormatterClass {
bool checkPartition() override;
bool formatPartition() override;
private:
bool checkCACertificatesPartition();
bool flashCACertificates();
BlockDevice* _root;
MBRBlockDevice _sys_bd;
MBRBlockDevice _user_bd;
Expand Down
16 changes: 13 additions & 3 deletions src/flashFormatter/H7FlashFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,28 @@ bool MBEDH7FlashFormatter::checkWifiPartition() {
return false;
}

bool found = false;
bool foundBin = false;
while ((ent = readdir (dir)) != NULL) {
String fullname = "/wlan/" + String(ent->d_name);
if (fullname == "/wlan/4343WA1.BIN") {
found = true;
foundBin = true;
break;
}
}

// Check if the ca certificates file is present
bool foundCert = false;
while ((ent = readdir (dir)) != NULL) {
String fullname = "/wlan/" + String(ent->d_name);
if (fullname == "/wlan/cacert.pem") {
foundCert = true;
break;
}
}

closedir (dir);
_wifi_data_fs.unmount();
return found;
return foundBin & foundCert;
}

bool MBEDH7FlashFormatter::formatWifiPartition() {
Expand Down
Loading