From 7c8c479b969ba631712ce98c39b111f06aba1d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Wed, 19 Oct 2022 15:39:06 +0200 Subject: [PATCH] wire in OEM.proto keystore --- src/graphics/Screen.cpp | 7 ------- src/mesh/Channels.cpp | 24 ++++++++++++++++-------- src/mesh/NodeDB.cpp | 6 ++++++ src/mesh/NodeDB.h | 1 + 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 601b83f9c..0f6c96f0f 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -84,10 +84,6 @@ static char ourId[5]; // GeoCoord object for the screen GeoCoord geoCoord; -// OEM Config File -static const char *oemConfigFile = "/oem/oem.proto"; -OEMStore oemStore; - #ifdef SHOW_REDRAWS static bool heartbeat = false; #endif @@ -928,9 +924,6 @@ void Screen::setup() dispdev.setDetected(screen_model); #endif - // Load OEM config from Proto file if existent - loadProto(oemConfigFile, OEMStore_size, sizeof(oemConfigFile), OEMStore_fields, &oemStore); - // Initialising the UI will init the display too. ui.init(); diff --git a/src/mesh/Channels.cpp b/src/mesh/Channels.cpp index a87d66b14..200184720 100644 --- a/src/mesh/Channels.cpp +++ b/src/mesh/Channels.cpp @@ -62,13 +62,6 @@ Channel &Channels::fixupChannel(ChannelIndex chIndex) // Convert the old string "Default" to our new short representation if (strcmp(channelSettings.name, "Default") == 0) *channelSettings.name = '\0'; - - /* Convert any old usage of the defaultpsk into our new short representation. - if (channelSettings.psk.size == sizeof(defaultpsk) && - memcmp(channelSettings.psk.bytes, defaultpsk, sizeof(defaultpsk)) == 0) { - *channelSettings.psk.bytes = 1; - channelSettings.psk.size = 1; - } */ } hashes[chIndex] = generateHash(chIndex); @@ -124,7 +117,22 @@ CryptoKey Channels::getKey(ChannelIndex chIndex) DEBUG_MSG("Expanding short PSK #%d\n", pskIndex); if (pskIndex == 0) k.length = 0; // Turn off encryption - else { + else if (oemStore.oem_aes_key.size > 1) { + // Use the OEM key + DEBUG_MSG("Using OEM Key with %d bytes\n", oemStore.oem_aes_key.size); + memcpy(k.bytes, oemStore.oem_aes_key.bytes , oemStore.oem_aes_key.size); + k.length = oemStore.oem_aes_key.size; + // Bump up the last byte of PSK as needed + uint8_t *last = k.bytes + oemStore.oem_aes_key.size - 1; + *last = *last + pskIndex - 1; // index of 1 means no change vs defaultPSK + if (k.length < 16) { + DEBUG_MSG("Warning: OEM provided a too short AES128 key - padding\n"); + k.length = 16; + } else if (k.length < 32 && k.length != 16) { + DEBUG_MSG("Warning: OEM provided a too short AES256 key - padding\n"); + k.length = 32; + } + } else { memcpy(k.bytes, defaultpsk, sizeof(defaultpsk)); k.length = sizeof(defaultpsk); // Bump up the last byte of PSK as needed diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 92115d127..319bb096b 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -38,6 +38,7 @@ MyNodeInfo &myNodeInfo = devicestate.my_node; LocalConfig config; LocalModuleConfig moduleConfig; ChannelFile channelFile; +OEMStore oemStore; /** The current change # for radio settings. Starts at 0 on boot and any time the radio settings * might have changed is incremented. Allows others to detect they might now be on a new channel. @@ -354,6 +355,8 @@ static const char *prefFileName = "/prefs/db.proto"; static const char *configFileName = "/prefs/config.proto"; static const char *moduleConfigFileName = "/prefs/module.proto"; static const char *channelFileName = "/prefs/channels.proto"; +static const char *oemConfigFile = "/oem/oem.proto"; + /** Load a protobuf from a file, return true for success */ bool loadProto(const char *filename, size_t protoSize, size_t objSize, const pb_msgdesc_t *fields, void *dest_struct) @@ -433,6 +436,9 @@ void NodeDB::loadFromDisk() DEBUG_MSG("Loaded saved channelFile version %d\n", channelFile.version); } } + + if (loadProto(oemConfigFile, OEMStore_size, sizeof(OEMStore), OEMStore_fields, &oemStore)) + DEBUG_MSG("Loaded OEMStore\n"); } /** Save a protobuf from a file, return true for success */ diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 574d6fe54..6b9744565 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -26,6 +26,7 @@ extern ChannelFile channelFile; extern MyNodeInfo &myNodeInfo; extern LocalConfig config; extern LocalModuleConfig moduleConfig; +extern OEMStore oemStore; extern User &owner; /// Given a node, return how many seconds in the past (vs now) that we last heard from it