Skip to content
Merged
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
29 changes: 28 additions & 1 deletion SmartEVSE-3/src/esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <MicroOcppMongooseClient.h>
#include <MicroOcpp/Core/Configuration.h>
#include <MicroOcpp/Core/Context.h>
#include <MicroOcpp/Operations/GetConfiguration.h>
#include "ocpp_logic.h"
#include "rfid_redact.h"
#include "ocpp_telemetry.h"
Expand Down Expand Up @@ -1734,7 +1735,11 @@ static const char *ocppMeterSerialToReport(void) {
// everything else. Debug builds only; see platformio.ini.
static void ocpp_console_out(const char *msg) {
if (msg) {
_LOG_A_NO_FUNC("%s", msg);
// OCPP traffic can carry the AuthorizationKey; never print it (issue #203)
char line[MO_CUSTOM_CONSOLE_MAXMSGSIZE];
snprintf(line, sizeof(line), "%s", msg);
ocpp_redact_auth_key(line);
_LOG_A_NO_FUNC("%s", line);
}
}
#endif
Expand Down Expand Up @@ -1793,6 +1798,24 @@ static void ocppResetTransactionWatch() {
}
}

// GetConfiguration that never returns the value of a secret key (issue #203).
// MicroOcppMongoose declares AuthorizationKey readable; OCPP 1.6 makes it
// write-only, so the entry is reported without its optional "value".
class OcppGetConfigurationNoSecrets : public MicroOcpp::Ocpp16::GetConfiguration {
public:
std::unique_ptr<MicroOcpp::JsonDoc> createConf() override {
auto doc = MicroOcpp::Ocpp16::GetConfiguration::createConf();
if (doc) {
for (JsonObject entry : (*doc)["configurationKey"].as<JsonArray>()) {
if (ocpp_config_key_is_secret(entry["key"] | "")) {
entry.remove("value");
}
}
}
return doc;
}
};

void ocppInit() {

// A re-init after silent reconnects keeps the counters since boot (issue #201)
Expand Down Expand Up @@ -1826,6 +1849,10 @@ void ocppInit() {
ocppMeterSerialToReport(), ocppMeterTypeToReport()),
filesystem);

// Replaces MicroOcpp's own handler, registered by mocpp_initialize()
getOcppContext()->getOperationRegistry().registerOperation("GetConfiguration", [] () {
return new OcppGetConfigurationNoSecrets();});

//setup OCPP hardware bindings

setEnergyMeterInput([] () { //Input of the electricity meter register in Wh
Expand Down
8 changes: 3 additions & 5 deletions SmartEVSE-3/src/http_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@ bool handle_URI(struct mg_connection *c, struct mg_http_message *hm, webServerR
// any client. Mirror the mqtt.password_set pattern — caller can tell if
// a key is configured without being able to read it. Before this fix
// any LAN client calling GET /settings obtained the plaintext key.
{
const char *ak = OcppWsClient ? OcppWsClient->getAuthKey() : "";
doc["ocpp"]["auth_key_set"] = (ak != NULL && ak[0] != '\0');
}
doc["ocpp"]["auth_key_set"] = ocpp_auth_key_is_set(OcppWsClient ? OcppWsClient->getAuthKey() : "");

{
auto freevendMode = MicroOcpp::getConfigurationPublic(MO_CONFIG_EXT_PREFIX "FreeVendActive");
Expand Down Expand Up @@ -841,7 +838,8 @@ bool handle_URI(struct mg_connection *c, struct mg_http_message *hm, webServerR
: "Invalid auth key";
} else if (OcppWsClient) {
OcppWsClient->setAuthKey(auth_key);
doc["ocpp_auth_key"] = OcppWsClient->getAuthKey();
// SECURITY C-2: confirm without echoing the key back
doc["ocpp_auth_key_set"] = ocpp_auth_key_is_set(OcppWsClient->getAuthKey());
} else {
doc["ocpp_auth_key"] = "Can only update when OCPP enabled";
}
Expand Down
89 changes: 89 additions & 0 deletions SmartEVSE-3/src/ocpp_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,92 @@ bool ocpp_silence_should_reinit(uint8_t silent_reconnects,
}
return !session_in_progress && !charging;
}

/* ---- Secrets in replies and console output (issue #203) ---- */

#define OCPP_SECRET_KEY_TOKEN "\"AuthorizationKey\""
#define OCPP_AUTH_TOKEN_PREFIX "auth Token="

bool ocpp_config_key_is_secret(const char *key) {
return key != NULL && strcmp(key, "AuthorizationKey") == 0;
}

/* Replace [start, end) with "***", or with as many '*' when shorter. */
static void ocpp_mask_span(char *start, char *end) {
size_t len = (size_t)(end - start);
if (len <= 3) {
memset(start, '*', len);
return;
}
memcpy(start, "***", 3);
memmove(start + 3, end, strlen(end) + 1);
}

/* End of a JSON string body that starts at s: its closing quote, or the NUL
* when the line was cut off inside it. */
static char *ocpp_json_string_end(char *s) {
while (*s != '\0' && *s != '"') {
if (*s == '\\' && s[1] != '\0') {
s++;
}
s++;
}
return s;
}

bool ocpp_auth_key_is_set(const char *auth_key) {
return auth_key != NULL && auth_key[0] != '\0';
}

size_t ocpp_redact_auth_key(char *line) {
if (!line) {
return 0;
}

size_t count = 0;
char *search = line;
char *key;
while ((key = strstr(search, OCPP_SECRET_KEY_TOKEN)) != NULL) {
/* The JSON object holding the key: from the '{' before it to the '}'
* after it, or to the end of a line cut off by the console buffer. */
char *obj = key;
while (obj > line && *obj != '{') {
obj--;
}
const char *obj_end = strchr(key, '}');
if (!obj_end) {
obj_end = line + strlen(line);
}

char *field = strstr(obj, "\"value\"");
if (field && field < obj_end) {
char *p = field + strlen("\"value\"");
while (*p == ' ') p++;
if (*p == ':') {
p++;
while (*p == ' ') p++;
if (*p == '"') {
p++;
ocpp_mask_span(p, ocpp_json_string_end(p));
count++;
}
}
}

/* The value may have come before the key and shifted it: find it again. */
search = strstr(obj, OCPP_SECRET_KEY_TOKEN) + strlen(OCPP_SECRET_KEY_TOKEN);
}

char *token = strstr(line, OCPP_AUTH_TOKEN_PREFIX);
if (token) {
char *colon = strchr(token + strlen(OCPP_AUTH_TOKEN_PREFIX), ':');
if (colon) {
char *end = colon + 1;
while (*end != '\0' && *end != ' ' && *end != '\n') end++;
ocpp_mask_span(colon + 1, end);
count++;
}
}

return count;
}
26 changes: 26 additions & 0 deletions SmartEVSE-3/src/ocpp_logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,32 @@ bool ocpp_silence_should_reinit(uint8_t silent_reconnects,
bool session_in_progress,
bool charging);

/* ---- Secrets in replies and console output (issue #203) ---- */

/*
* True for configuration keys whose value must never leave the charger.
* OCPP 1.6 treats AuthorizationKey as write-only, but MicroOcppMongoose
* declares it readable, so the firmware's GetConfiguration handler drops the
* value of every key this returns true for.
*/
bool ocpp_config_key_is_secret(const char *key);

/*
* Redact the AuthorizationKey value in a console line, in place. Handles
* GetConfiguration replies and ChangeConfiguration requests in either field
* order, lines cut off inside the value by the console buffer, and the
* "auth Token=<id>:<key>" debug line of MicroOcppMongoose. A value becomes
* "***" (shorter values become as many '*'), so the line never grows.
* Returns the number of values redacted.
*/
size_t ocpp_redact_auth_key(char *line);

/*
* True when an OCPP auth key is configured. HTTP responses report only this,
* never the key itself (SECURITY C-2), also right after the key was set.
*/
bool ocpp_auth_key_is_set(const char *auth_key);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions SmartEVSE-3/test/native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ $(BUILD)/test_ocpp_stop_sync: $(TEST_DIR)/test_ocpp_stop_sync.c $(OCPP_LOGIC_SRC
$(BUILD)/test_ocpp_tx_sync: $(TEST_DIR)/test_ocpp_tx_sync.c $(OCPP_LOGIC_SRC) $(SRC_DIR)/ocpp_logic.h include/*.h | $(BUILD)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_ocpp_tx_sync.c $(OCPP_LOGIC_SRC)

$(BUILD)/test_ocpp_secrets: $(TEST_DIR)/test_ocpp_secrets.c $(OCPP_LOGIC_SRC) $(SRC_DIR)/ocpp_logic.h include/*.h | $(BUILD)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_ocpp_secrets.c $(OCPP_LOGIC_SRC)

$(BUILD)/test_net_watchdog: $(TEST_DIR)/test_net_watchdog.c $(NET_WATCHDOG_SRC) $(SRC_DIR)/net_watchdog.h include/*.h | $(BUILD)
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $(TEST_DIR)/test_net_watchdog.c $(NET_WATCHDOG_SRC)

Expand Down
Loading
Loading