Skip to content

Commit e336c4b

Browse files
authored
avoid copying TLS cert to heap (#10)
* change cert to zero-copy attribute * update changelog
1 parent 1d00acc commit e336c4b

File tree

3 files changed

+11
-44
lines changed

3 files changed

+11
-44
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66

77
- Adopt `bool isConnected()` from `Connection` interface ([#7](https://github.com/matth-x/MicroOcppMongoose/pull/7))
8+
- Do not copy cert into heap memory ([#10](https://github.com/matth-x/MicroOcppMongoose/pull/10))
89

910
### Added
1011

@@ -15,6 +16,7 @@
1516
### Removed
1617

1718
- FTP moved into a new project [MicroFtp](https://github.com/matth-x/MicroFtp) ([#5](https://github.com/matth-x/MicroOcppMongoose/pull/5))
19+
- Custom config `Cst_CaCert` ([#10](https://github.com/matth-x/MicroOcppMongoose/pull/10))
1820

1921
## [1.0.0] - 2023-10-20
2022

src/MicroOcppMongooseClient.cpp

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
2222
const char *backend_url_factory,
2323
const char *charge_box_id_factory,
2424
const char *auth_key_factory,
25-
const char *CA_cert_factory,
25+
const char *ca_certificate,
2626
std::shared_ptr<FilesystemAdapter> filesystem,
2727
ProtocolVersion protocolVersion) : mgr(mgr), protocolVersion(protocolVersion) {
2828

@@ -45,13 +45,6 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
4545
MO_CONFIG_EXT_PREFIX "ChargeBoxId", charge_box_id_factory, MO_WSCONN_FN, readonly, true);
4646
setting_auth_key_str = declareConfiguration<const char*>(
4747
"AuthorizationKey", auth_key_factory, MO_WSCONN_FN, readonly, true);
48-
#if !MO_CA_CERT_LOCAL
49-
setting_ca_cert_str = declareConfiguration<const char*>(
50-
MO_CONFIG_EXT_PREFIX "CaCert", CA_cert_factory, MO_WSCONN_FN, readonly, true);
51-
#else
52-
ca_cert = CA_cert_factory ? CA_cert_factory : "";
53-
#endif
54-
5548
ws_ping_interval_int = declareConfiguration<int>(
5649
"WebSocketPingInterval", 5, MO_WSCONN_FN);
5750
reconnect_interval_int = declareConfiguration<int>(
@@ -61,6 +54,8 @@ MOcppMongooseClient::MOcppMongooseClient(struct mg_mgr *mgr,
6154

6255
configuration_load(MO_WSCONN_FN); //load configs with values stored on flash
6356

57+
ca_cert = ca_certificate;
58+
6459
reloadConfigs(); //load WS creds with configs values
6560

6661
#if defined(MO_MG_VERSION_614)
@@ -166,7 +161,7 @@ void MOcppMongooseClient::maintainWsConn() {
166161
struct mg_connect_opts opts;
167162
memset(&opts, 0, sizeof(opts));
168163

169-
const char *ca_string = ca_cert.empty() ? "*" : ca_cert.c_str();
164+
const char *ca_string = ca_cert ? ca_cert : "*"; //"*" enables TLS but disables CA verification
170165

171166
//Check if SSL is disabled, i.e. if URL starts with "ws:"
172167
if (url.length() >= strlen("ws:") &&
@@ -270,19 +265,7 @@ void MOcppMongooseClient::setAuthKey(const char *auth_key_cstr) {
270265
}
271266

272267
void MOcppMongooseClient::setCaCert(const char *ca_cert_cstr) {
273-
if (!ca_cert_cstr) {
274-
MO_DBG_ERR("invalid argument");
275-
return;
276-
}
277-
278-
#if !MO_CA_CERT_LOCAL
279-
if (setting_ca_cert_str) {
280-
setting_ca_cert_str->setString(ca_cert_cstr);
281-
configuration_save();
282-
}
283-
#else
284268
ca_cert = ca_cert_cstr; //updated ca_cert takes immediate effect
285-
#endif
286269
}
287270

288271
void MOcppMongooseClient::reloadConfigs() {
@@ -304,12 +287,6 @@ void MOcppMongooseClient::reloadConfigs() {
304287
auth_key = setting_auth_key_str->getString();
305288
}
306289

307-
#if !MO_CA_CERT_LOCAL
308-
if (setting_ca_cert_str) {
309-
ca_cert = setting_ca_cert_str->getString();
310-
}
311-
#endif
312-
313290
/*
314291
* determine new URL and auth token with updated WS credentials
315292
*/
@@ -464,7 +441,7 @@ void ws_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
464441
// If target URL is SSL/TLS, command client connection to use TLS
465442
if (mg_url_is_ssl(osock->getUrl())) {
466443
const char *ca_string = osock->getCaCert();
467-
if (ca_string && *ca_string == '\0') { //check if certificate validation is disabled by passing an empty string
444+
if (ca_string && *ca_string == '\0') { //check if certificate verification is disabled (cert string is empty)
468445
//yes, disabled
469446
ca_string = nullptr;
470447
}

src/MicroOcppMongooseClient.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef MO_MONGOOSECLIENT_H
66
#define MO_MONGOOSECLIENT_H
77

8-
#if defined(ARDUINO) //fix for conflicting defitions of IPAddress on Arduino
8+
#if defined(ARDUINO) //fix for conflicting definitions of IPAddress on Arduino
99
#include <Arduino.h>
1010
#include <IPAddress.h>
1111
#endif
@@ -21,15 +21,6 @@
2121
#define MO_WSCONN_FN (MO_FILENAME_PREFIX "ws-conn.jsn")
2222
#endif
2323

24-
/*
25-
* If you prefer not to have the TLS-certificate managed by OCPP, store it into
26-
* a file on the flash filesystem, define the following build flag as 1 and
27-
* pass the filename to the constructor instead of a default plain-text certificate.
28-
*/
29-
#ifndef MO_CA_CERT_LOCAL
30-
#define MO_CA_CERT_LOCAL 0
31-
#endif
32-
3324
namespace MicroOcpp {
3425

3526
class FilesystemAdapter;
@@ -44,13 +35,10 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
4435
std::string url; //url = backend_url + '/' + cb_id
4536
std::string auth_key;
4637
std::string basic_auth64;
47-
std::string ca_cert;
38+
const char *ca_cert; //zero-copy. The host system must ensure that this pointer remains valid during the lifetime of this class
4839
std::shared_ptr<Configuration> setting_backend_url_str;
4940
std::shared_ptr<Configuration> setting_cb_id_str;
5041
std::shared_ptr<Configuration> setting_auth_key_str;
51-
#if !MO_CA_CERT_LOCAL
52-
std::shared_ptr<Configuration> setting_ca_cert_str;
53-
#endif
5442
unsigned long last_status_dbg_msg {0}, last_recv {0};
5543
std::shared_ptr<Configuration> reconnect_interval_int; //minimum time between two connect trials in s
5644
unsigned long last_reconnection_attempt {-1UL / 2UL};
@@ -73,7 +61,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
7361
const char *backend_url_factory = nullptr,
7462
const char *charge_box_id_factory = nullptr,
7563
const char *auth_key_factory = nullptr,
76-
const char *CA_cert_factory = nullptr, //forwards this string to Mongoose as ssl_ca_cert (see https://github.com/cesanta/mongoose/blob/ab650ec5c99ceb52bb9dc59e8e8ec92a2724932b/mongoose.h#L4192)
64+
const char *ca_cert = nullptr, //zero-copy, the string must outlive this class and mg_mgr. Forwards this string to Mongoose as ssl_ca_cert (see https://github.com/cesanta/mongoose/blob/ab650ec5c99ceb52bb9dc59e8e8ec92a2724932b/mongoose.h#L4192)
7765
std::shared_ptr<MicroOcpp::FilesystemAdapter> filesystem = nullptr,
7866
ProtocolVersion protocolVersion = ProtocolVersion(1,6));
7967

@@ -102,7 +90,7 @@ class MOcppMongooseClient : public MicroOcpp::Connection {
10290
const char *getBackendUrl() {return backend_url.c_str();}
10391
const char *getChargeBoxId() {return cb_id.c_str();}
10492
const char *getAuthKey() {return auth_key.c_str();}
105-
const char *getCaCert() {return ca_cert.c_str();}
93+
const char *getCaCert() {return ca_cert ? ca_cert : "";}
10694

10795
const char *getUrl() {return url.c_str();}
10896

0 commit comments

Comments
 (0)