#include "esp32Storage.hpp" namespace firewall { Storage::Storage() { this->mount_spiffs(); } Storage::~Storage() { } void Storage::mount_spiffs() { if (!SPIFFS.begin(false)) { if (!SPIFFS.begin(true)) { log_e("SPIFFS cannot be mounted"); while (true) delay(500); }; } log_i("SPIFFS mounted"); } uint8_t Storage::retrieve_settings_value(const char *key) { uint8_t amount_of_rules; this->memory.begin("settings", true); amount_of_rules = memory.getUChar(key, 0); this->memory.end(); return amount_of_rules; } void Storage::store_settings_value(const char *key, const uint8_t new_amount) { this->memory.begin("settings", false); this->memory.putUChar(key, new_amount); this->memory.end(); } firewall_rule_t *Storage::retrieve_firewall_rule(const uint8_t key) { firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); rule_ptr->key = key; char rulename[9]; // fwRule99\n sprintf(rulename, "fwRule%i", key); this->memory.begin(rulename, true); strcpy(rule_ptr->source, this->memory.getString("source", "0.0.0.0").c_str()); strcpy(rule_ptr->destination, this->memory.getString("destination", "0.0.0.0").c_str()); rule_ptr->protocol = static_cast(this->memory.getUChar("protocol", FW_ALL)); rule_ptr->target = static_cast(this->memory.getUChar("target", FW_REJECT)); this->memory.end(); return rule_ptr; } void Storage::store_all_firewall_rules(firewall_rule_t *head) { firewall_rule_t *temp = head; while (temp != NULL) { store_firewall_rule(temp); temp = temp->next; } } void Storage::store_firewall_rule(firewall_rule_t *rule_ptr) { char rulename[9]; // fwRule99\n sprintf(rulename, "fwRule%i", rule_ptr->key); this->memory.begin(rulename, false); this->memory.putString("source", rule_ptr->source); this->memory.putString("destination", rule_ptr->destination); this->memory.putUChar("protocol", rule_ptr->protocol); this->memory.putUChar("target", rule_ptr->target); this->memory.end(); } httpsserver::SSLCert *Storage::retrieve_certificate() { File keyFile = SPIFFS.open("/key.der"); File certFile = SPIFFS.open("/cert.der"); if (!keyFile || !certFile || keyFile.size() == 0 || certFile.size() == 0) { log_w("No certificate found in SPIFFS"); return NULL; } size_t keySize = keyFile.size(); size_t certSize = certFile.size(); uint8_t *keyBuffer = new uint8_t[keySize]; if (keyBuffer == NULL) { log_w("Not enough memory to load privat key"); return NULL; } uint8_t *certBuffer = new uint8_t[certSize]; if (certBuffer == NULL) { delete[] keyBuffer; log_w("Not enough memory to load certificate"); return NULL; } keyFile.read(keyBuffer, keySize); certFile.read(certBuffer, certSize); keyFile.close(); certFile.close(); return new httpsserver::SSLCert(certBuffer, certSize, keyBuffer, keySize); } void Storage::store_certificate(httpsserver::SSLCert *certificate) { File keyFile = SPIFFS.open("/key.der"); File certFile = SPIFFS.open("/cert.der"); bool failure = false; keyFile = SPIFFS.open("/key.der", FILE_WRITE); if (!keyFile || !keyFile.write(certificate->getPKData(), certificate->getPKLength())) { log_w("Could not write /key.der"); failure = true; } if (keyFile) keyFile.close(); certFile = SPIFFS.open("/cert.der", FILE_WRITE); if (!certFile || !certFile.write(certificate->getCertData(), certificate->getCertLength())) { log_w("Could not write /key.der"); failure = true; } if (certFile) certFile.close(); if (failure) { log_w("Certificate could not be stored permanently, generating new certificate on reboot..."); } } }