2022-04-20 14:37:21 +02:00
|
|
|
#include "esp32Storage.hpp"
|
|
|
|
|
|
|
|
namespace firewall
|
|
|
|
{
|
2022-04-20 15:43:02 +02:00
|
|
|
Storage::Storage()
|
2022-04-20 14:37:21 +02:00
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
if (this->mount_spiffs() == ERROR)
|
|
|
|
endless_loop();
|
2022-04-20 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage::~Storage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:16:54 +02:00
|
|
|
ok_t Storage::mount_spiffs()
|
2022-04-20 21:17:34 +02:00
|
|
|
{
|
|
|
|
if (!SPIFFS.begin(false))
|
|
|
|
{
|
|
|
|
if (!SPIFFS.begin(true))
|
|
|
|
{
|
|
|
|
log_e("SPIFFS cannot be mounted");
|
2022-04-21 15:16:54 +02:00
|
|
|
return ERROR;
|
2022-04-20 21:17:34 +02:00
|
|
|
};
|
|
|
|
}
|
2022-04-21 15:16:54 +02:00
|
|
|
return SUCCESS;
|
2022-04-20 21:17:34 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:42:30 +02:00
|
|
|
uint8_t Storage::retrieve_settings_value(const char *key)
|
2022-04-20 14:37:21 +02:00
|
|
|
{
|
2022-04-20 15:43:02 +02:00
|
|
|
uint8_t amount_of_rules;
|
2022-04-20 19:42:30 +02:00
|
|
|
|
|
|
|
this->memory.begin("settings", true);
|
|
|
|
amount_of_rules = memory.getUChar(key, 0);
|
|
|
|
this->memory.end();
|
|
|
|
|
2022-04-20 15:43:02 +02:00
|
|
|
return amount_of_rules;
|
2022-04-20 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 19:42:30 +02:00
|
|
|
void Storage::store_settings_value(const char *key, const uint8_t new_amount)
|
2022-04-20 14:37:21 +02:00
|
|
|
{
|
2022-04-20 19:42:30 +02:00
|
|
|
this->memory.begin("settings", false);
|
|
|
|
this->memory.putUChar(key, new_amount);
|
|
|
|
this->memory.end();
|
2022-04-20 14:37:21 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 15:43:02 +02:00
|
|
|
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;
|
2022-04-20 19:42:30 +02:00
|
|
|
|
|
|
|
char rulename[9]; // fwRule99\n
|
|
|
|
sprintf(rulename, "fwRule%i", key);
|
|
|
|
|
|
|
|
this->memory.begin(rulename, true);
|
2022-04-21 15:16:54 +02:00
|
|
|
strncpy(rule_ptr->source, this->memory.getString("source", "0.0.0.0").c_str(), sizeof(rule_ptr->source));
|
|
|
|
strncpy(rule_ptr->destination, this->memory.getString("destination", "0.0.0.0").c_str(), sizeof(rule_ptr->source));
|
2022-04-20 19:42:30 +02:00
|
|
|
rule_ptr->protocol = static_cast<firewall_protocol_t>(this->memory.getUChar("protocol", FW_ALL));
|
|
|
|
rule_ptr->target = static_cast<firewall_target_t>(this->memory.getUChar("target", FW_REJECT));
|
|
|
|
this->memory.end();
|
|
|
|
|
2022-04-20 15:43:02 +02:00
|
|
|
return rule_ptr;
|
|
|
|
}
|
2022-04-20 14:37:21 +02:00
|
|
|
|
2022-04-20 21:35:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:42:30 +02:00
|
|
|
void Storage::store_firewall_rule(firewall_rule_t *rule_ptr)
|
2022-04-20 15:43:02 +02:00
|
|
|
{
|
2022-04-20 19:42:30 +02:00
|
|
|
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();
|
2022-04-20 15:43:02 +02:00
|
|
|
}
|
2022-04-20 15:53:54 +02:00
|
|
|
|
2022-04-20 16:02:10 +02:00
|
|
|
httpsserver::SSLCert *Storage::retrieve_certificate()
|
|
|
|
{
|
2022-04-20 21:17:34 +02:00
|
|
|
File keyFile = SPIFFS.open("/key.der");
|
|
|
|
File certFile = SPIFFS.open("/cert.der");
|
|
|
|
if (!keyFile || !certFile || keyFile.size() == 0 || certFile.size() == 0)
|
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
log_e("No server-certificate found in SPIFFS");
|
2022-04-20 21:17:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
size_t keySize = keyFile.size();
|
|
|
|
size_t certSize = certFile.size();
|
|
|
|
|
|
|
|
uint8_t *keyBuffer = new uint8_t[keySize];
|
|
|
|
if (keyBuffer == NULL)
|
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
log_w("Not enough memory to load private key");
|
2022-04-20 21:17:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint8_t *certBuffer = new uint8_t[certSize];
|
|
|
|
if (certBuffer == NULL)
|
|
|
|
{
|
|
|
|
delete[] keyBuffer;
|
2022-04-21 15:16:54 +02:00
|
|
|
log_w("Not enough memory to load server-certificate");
|
2022-04-20 21:17:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
keyFile.read(keyBuffer, keySize);
|
|
|
|
certFile.read(certBuffer, certSize);
|
|
|
|
|
|
|
|
keyFile.close();
|
|
|
|
certFile.close();
|
|
|
|
return new httpsserver::SSLCert(certBuffer, certSize, keyBuffer, keySize);
|
2022-04-20 16:02:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 15:53:54 +02:00
|
|
|
void Storage::store_certificate(httpsserver::SSLCert *certificate)
|
|
|
|
{
|
2022-04-20 21:17:34 +02:00
|
|
|
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()))
|
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
log_w("Cannot write /key.der");
|
2022-04-20 21:17:34 +02:00
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
if (keyFile)
|
|
|
|
keyFile.close();
|
|
|
|
|
|
|
|
certFile = SPIFFS.open("/cert.der", FILE_WRITE);
|
|
|
|
if (!certFile || !certFile.write(certificate->getCertData(), certificate->getCertLength()))
|
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
log_w("Cannot write /cert.der");
|
2022-04-20 21:17:34 +02:00
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
if (certFile)
|
|
|
|
certFile.close();
|
|
|
|
|
|
|
|
if (failure)
|
|
|
|
{
|
2022-04-21 15:16:54 +02:00
|
|
|
log_w("Server-certificate could not be stored permanently, generating new certificate on reboot...");
|
2022-04-20 21:17:34 +02:00
|
|
|
}
|
2022-04-20 15:53:54 +02:00
|
|
|
}
|
2022-04-20 14:37:21 +02:00
|
|
|
}
|