Files
firmware/src/modules/AdminModule.cpp

1127 lines
49 KiB
C++
Raw Normal View History

#include "AdminModule.h"
2021-02-26 15:34:00 +08:00
#include "Channels.h"
2021-02-21 14:03:44 +08:00
#include "MeshService.h"
#include "NodeDB.h"
2023-02-03 08:50:10 -06:00
#include "PowerFSM.h"
2024-08-17 05:52:36 -05:00
#include "RTC.h"
2024-08-19 11:12:42 -05:00
#include "meshUtils.h"
#include <FSCommon.h>
#if defined(ARCH_ESP32) && !MESHTASTIC_EXCLUDE_BLUETOOTH
2022-10-08 11:10:29 +02:00
#include "BleOta.h"
2022-10-09 11:19:33 +02:00
#endif
2021-02-21 14:03:44 +08:00
#include "Router.h"
2022-05-02 08:53:44 +10:00
#include "configuration.h"
2021-02-21 14:03:44 +08:00
#include "main.h"
2023-02-03 08:50:10 -06:00
#ifdef ARCH_NRF52
#include "main.h"
#endif
#ifdef ARCH_PORTDUINO
2021-03-18 19:09:31 +08:00
#include "unistd.h"
#endif
#include "../userPrefs.h"
#include "Default.h"
#include "TypeConversions.h"
2021-03-18 19:09:31 +08:00
#if !MESHTASTIC_EXCLUDE_MQTT
2023-02-02 14:11:48 -06:00
#include "mqtt/MQTT.h"
#endif
2023-02-02 14:11:48 -06:00
#if !MESHTASTIC_EXCLUDE_GPS
#include "GPS.h"
#endif
#if MESHTASTIC_EXCLUDE_GPS
#include "modules/PositionModule.h"
#endif
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_I2C
#include "motion/AccelerometerThread.h"
#endif
2022-02-27 02:21:02 -08:00
AdminModule *adminModule;
bool hasOpenEditTransaction;
2021-02-21 14:03:44 +08:00
/// A special reserved string to indicate strings we can not share with external nodes. We will use this 'reserved' word instead.
2022-05-02 08:53:44 +10:00
/// Also, to make setting work correctly, if someone tries to set a string to this reserved value we assume they don't really want
/// a change.
static const char *secretReserved = "sekrit";
/// If buf is the reserved secret word, replace the buffer with currentVal
2023-01-16 11:08:48 +01:00
static void writeSecret(char *buf, size_t bufsz, const char *currentVal)
2022-05-02 08:53:44 +10:00
{
2023-02-03 14:10:19 -06:00
if (strcmp(buf, secretReserved) == 0) {
2023-01-16 11:08:48 +01:00
strncpy(buf, currentVal, bufsz);
}
}
2022-05-02 22:00:24 +10:00
/**
* @brief Handle received protobuf message
2022-05-02 22:00:24 +10:00
*
* @param mp Received MeshPacket
2022-05-02 22:00:24 +10:00
* @param r Decoded AdminMessage
* @return bool
*/
bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *r)
2021-02-21 14:03:44 +08:00
{
// if handled == false, then let others look at this message also if they want
bool handled = false;
2021-02-21 14:03:44 +08:00
assert(r);
bool fromOthers = !isFromUs(&mp);
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
if (mp.which_payload_variant != meshtastic_MeshPacket_decoded_tag) {
return handled;
}
meshtastic_Channel *ch = &channels.getByIndex(mp.channel);
// Could tighten this up further by tracking the last public_key we went an AdminMessage request to
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
// and only allowing responses from that remote.
if (messageIsResponse(r)) {
LOG_DEBUG("Allow admin response message");
} else if (mp.from == 0) {
if (config.security.is_managed) {
LOG_INFO("Ignore local admin payload because is_managed");
return handled;
}
} else if (strcasecmp(ch->settings.name, Channels::adminChannel) == 0) {
if (!config.security.admin_channel_enabled) {
LOG_INFO("Ignore admin channel, legacy admin is disabled");
myReply = allocErrorResponse(meshtastic_Routing_Error_NOT_AUTHORIZED, &mp);
return handled;
}
} else if (mp.pki_encrypted) {
if ((config.security.admin_key[0].size == 32 &&
memcmp(mp.public_key.bytes, config.security.admin_key[0].bytes, 32) == 0) ||
(config.security.admin_key[1].size == 32 &&
memcmp(mp.public_key.bytes, config.security.admin_key[1].bytes, 32) == 0) ||
(config.security.admin_key[2].size == 32 &&
memcmp(mp.public_key.bytes, config.security.admin_key[2].bytes, 32) == 0)) {
LOG_INFO("PKC admin payload with authorized sender key");
} else {
myReply = allocErrorResponse(meshtastic_Routing_Error_ADMIN_PUBLIC_KEY_UNAUTHORIZED, &mp);
LOG_INFO("Received PKC admin payload, but the sender public key does not match the admin authorized key!");
return handled;
}
} else {
LOG_INFO("Ignore unauthorized admin payload %i", r->which_payload_variant);
myReply = allocErrorResponse(meshtastic_Routing_Error_NOT_AUTHORIZED, &mp);
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
return handled;
}
LOG_INFO("Handle admin payload %i", r->which_payload_variant);
// all of the get and set messages, including those for other modules, flow through here first.
// any message that changes state, we want to check the passkey for
if (mp.from != 0 && !messageIsRequest(r) && !messageIsResponse(r)) {
if (!checkPassKey(r)) {
LOG_WARN("Admin message without session_key!");
myReply = allocErrorResponse(meshtastic_Routing_Error_ADMIN_BAD_SESSION_KEY, &mp);
return handled;
}
}
2023-02-03 14:10:19 -06:00
switch (r->which_payload_variant) {
2022-05-02 22:00:24 +10:00
/**
* Getters
*/
case meshtastic_AdminMessage_get_owner_request_tag:
LOG_INFO("Client got owner");
2022-05-02 22:00:24 +10:00
handleGetOwner(mp);
2021-02-26 15:34:00 +08:00
break;
case meshtastic_AdminMessage_get_config_request_tag:
LOG_INFO("Client got config");
2022-05-29 16:31:30 +02:00
handleGetConfig(mp, r->get_config_request);
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_get_module_config_request_tag:
LOG_INFO("Client got module config");
2022-05-03 22:16:50 +10:00
handleGetModuleConfig(mp, r->get_module_config_request);
2021-02-26 15:34:00 +08:00
break;
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_get_channel_request_tag: {
uint32_t i = r->get_channel_request - 1;
LOG_INFO("Client got channel %u", i);
if (i >= MAX_NUM_CHANNELS)
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
else
handleGetChannel(mp, i);
2021-02-26 15:34:00 +08:00
break;
}
2021-02-26 15:34:00 +08:00
2022-05-02 22:00:24 +10:00
/**
* Setters
*/
case meshtastic_AdminMessage_set_owner_tag:
LOG_INFO("Client set owner");
2022-05-02 22:00:24 +10:00
handleSetOwner(r->set_owner);
2021-02-26 15:34:00 +08:00
break;
case meshtastic_AdminMessage_set_config_tag:
LOG_INFO("Client set config");
2022-05-02 08:53:44 +10:00
handleSetConfig(r->set_config);
break;
case meshtastic_AdminMessage_set_module_config_tag:
LOG_INFO("Client set module config");
2022-05-02 22:00:24 +10:00
handleSetModuleConfig(r->set_module_config);
2022-03-10 09:14:56 +11:00
break;
case meshtastic_AdminMessage_set_channel_tag:
LOG_INFO("Client set channel %d", r->set_channel.index);
2022-05-02 22:00:24 +10:00
if (r->set_channel.index < 0 || r->set_channel.index >= (int)MAX_NUM_CHANNELS)
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
2022-05-02 22:00:24 +10:00
else
handleSetChannel(r->set_channel);
break;
2023-02-04 15:11:36 -06:00
case meshtastic_AdminMessage_set_ham_mode_tag:
LOG_INFO("Client set ham mode");
2023-02-04 15:11:36 -06:00
handleSetHamMode(r->set_ham_mode);
break;
2022-05-02 22:00:24 +10:00
/**
* Other
*/
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_reboot_seconds_tag: {
2022-10-19 19:19:04 -05:00
reboot(r->reboot_seconds);
2021-03-27 10:19:59 +08:00
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_reboot_ota_seconds_tag: {
2022-10-08 11:10:29 +02:00
int32_t s = r->reboot_ota_seconds;
#if defined(ARCH_ESP32) && !MESHTASTIC_EXCLUDE_BLUETOOTH
2023-02-03 14:10:19 -06:00
if (BleOta::getOtaAppVersion().isEmpty()) {
LOG_INFO("No OTA firmware available, scheduling regular reboot in %d seconds", s);
screen->startAlert("Rebooting...");
2023-02-03 14:10:19 -06:00
} else {
screen->startFirmwareUpdateScreen();
2022-10-08 11:10:29 +02:00
BleOta::switchToOtaApp();
LOG_INFO("Reboot to OTA in %d seconds", s);
2022-10-08 11:10:29 +02:00
}
#else
LOG_INFO("Not on ESP32, scheduling regular reboot in %d seconds", s);
screen->startAlert("Rebooting...");
#endif
2022-10-08 11:10:29 +02:00
rebootAtMsec = (s < 0) ? 0 : (millis() + s * 1000);
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_shutdown_seconds_tag: {
int32_t s = r->shutdown_seconds;
LOG_INFO("Shutdown in %d seconds", s);
shutdownAtMsec = (s < 0) ? 0 : (millis() + s * 1000);
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_get_device_metadata_request_tag: {
LOG_INFO("Client got device metadata");
handleGetDeviceMetadata(mp);
break;
}
case meshtastic_AdminMessage_factory_reset_config_tag: {
disableBluetooth();
LOG_INFO("Initiate factory config reset");
nodeDB->factoryReset();
LOG_INFO("Factory config reset finished, rebooting soon");
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
case meshtastic_AdminMessage_factory_reset_device_tag: {
disableBluetooth();
LOG_INFO("Initiate full factory reset");
nodeDB->factoryReset(true);
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_nodedb_reset_tag: {
disableBluetooth();
LOG_INFO("Initiate node-db reset");
nodeDB->resetNodes();
2022-11-23 14:12:44 -06:00
reboot(DEFAULT_REBOOT_SECONDS);
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_begin_edit_settings_tag: {
LOG_INFO("Begin transaction for editing settings");
hasOpenEditTransaction = true;
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_commit_edit_settings_tag: {
disableBluetooth();
LOG_INFO("Commit transaction for edited settings");
hasOpenEditTransaction = false;
saveChanges(SEGMENT_CONFIG | SEGMENT_MODULECONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS);
2022-09-28 16:47:26 -05:00
break;
}
2023-02-03 14:10:19 -06:00
case meshtastic_AdminMessage_get_device_connection_status_request_tag: {
LOG_INFO("Client got device connection status");
2023-02-03 08:50:10 -06:00
handleGetDeviceConnectionStatus(mp);
break;
}
case meshtastic_AdminMessage_get_module_config_response_tag: {
LOG_INFO("Client received a get_module_config response");
if (fromOthers && r->get_module_config_response.which_payload_variant ==
meshtastic_AdminMessage_ModuleConfigType_REMOTEHARDWARE_CONFIG) {
handleGetModuleConfigResponse(mp, r);
}
break;
}
case meshtastic_AdminMessage_remove_by_nodenum_tag: {
LOG_INFO("Client received remove_nodenum command");
nodeDB->removeNodeByNum(r->remove_by_nodenum);
Show specific frame when updating screen (#4264) * Updated setFrames in Screen.cpp Added code to attempt to revert back to the same frame that user was on prior to setFrame reload. * Space added Screen.cpp * Update Screen.cpp Make screen to revert to Frame 0 if the originally displayed frame is no longer there. * Update Screen.cpp Inserted boolean holdPosition into setFrames to indicate the requirement to stay on the same frame ( if =true) or else it will switch to new frame . Only Screen::handleStatusUpdate calls with setFrame(true). ( For Node Updates) All other types of updates call as before setFrame(), so it will change focus as needed. * Hold position, even if number of frames increases * Hold position, if handling an outgoing text message * Update Screen.cpp * Reverted chnages related to devicestate.has_rx_text_message * Reset to master * CannedMessages only handles routing packets when waiting for ACK Previously, this was calling Screen::setFrames at unexpected times * Gather position info about screen frames while regenerating * Make admin module observable Notify only when relevant. Currently: only to handle remove_nodenum. * Optionally specify which frame to focus when setFrames runs * UIFrameEvent uses enum instead of multiple booleans * Allow modules to request their own frame to be focussed This is done internally by calling MeshModule::requestFocus() Easier this way, insteady of passing the info in the UIFrameEvent: * Modules don't always know whether they should be focussed until after the UIFrameEvent has been raised, in dramFrame * Don't have to pass reference to module instance as parameter though several methods * E-Ink screensaver uses FOCUS_PRESERVE Previously, it had its own basic implementation of this. * Spelling: regional variant * trunk * Fix HAS_SCREEN guarding * More HAS_SCREEN guarding --------- Co-authored-by: BIST <77391720+slash-bit@users.noreply.github.com> Co-authored-by: Ben Meadors <benmmeadors@gmail.com> Co-authored-by: slash-bit <v-b2@live.com>
2024-07-12 11:51:26 +12:00
this->notifyObservers(r); // Observed by screen
break;
}
case meshtastic_AdminMessage_set_favorite_node_tag: {
LOG_INFO("Client received set_favorite_node command");
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(r->set_favorite_node);
if (node != NULL) {
node->is_favorite = true;
saveChanges(SEGMENT_DEVICESTATE, false);
}
break;
}
case meshtastic_AdminMessage_remove_favorite_node_tag: {
LOG_INFO("Client received remove_favorite_node command");
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(r->remove_favorite_node);
if (node != NULL) {
node->is_favorite = false;
saveChanges(SEGMENT_DEVICESTATE, false);
}
break;
}
case meshtastic_AdminMessage_set_ignored_node_tag: {
LOG_INFO("Client received set_ignored_node command");
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(r->set_ignored_node);
if (node != NULL) {
node->is_ignored = true;
node->has_device_metrics = false;
node->has_position = false;
node->user.public_key.size = 0;
node->user.public_key.bytes[0] = 0;
saveChanges(SEGMENT_DEVICESTATE, false);
}
break;
}
case meshtastic_AdminMessage_remove_ignored_node_tag: {
LOG_INFO("Client received remove_ignored_node command");
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(r->remove_ignored_node);
if (node != NULL) {
node->is_ignored = false;
saveChanges(SEGMENT_DEVICESTATE, false);
}
break;
}
case meshtastic_AdminMessage_set_fixed_position_tag: {
LOG_INFO("Client received set_fixed_position command");
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(nodeDB->getNodeNum());
node->has_position = true;
node->position = TypeConversions::ConvertToPositionLite(r->set_fixed_position);
nodeDB->setLocalPosition(r->set_fixed_position);
config.position.fixed_position = true;
saveChanges(SEGMENT_DEVICESTATE | SEGMENT_CONFIG, false);
#if !MESHTASTIC_EXCLUDE_GPS
if (gps != nullptr)
gps->enable();
// Send our new fixed position to the mesh for good measure
positionModule->sendOurPosition();
#endif
break;
}
case meshtastic_AdminMessage_remove_fixed_position_tag: {
LOG_INFO("Client received remove_fixed_position command");
nodeDB->clearLocalPosition();
config.position.fixed_position = false;
saveChanges(SEGMENT_DEVICESTATE | SEGMENT_CONFIG, false);
break;
}
2024-08-17 05:52:36 -05:00
case meshtastic_AdminMessage_set_time_only_tag: {
LOG_INFO("Client received set_time_only command");
2024-08-17 05:52:36 -05:00
struct timeval tv;
tv.tv_sec = r->set_time_only;
tv.tv_usec = 0;
perhapsSetRTC(RTCQualityNTP, &tv, false);
2024-08-17 05:52:36 -05:00
break;
}
case meshtastic_AdminMessage_enter_dfu_mode_request_tag: {
LOG_INFO("Client requesting to enter DFU mode");
#if defined(ARCH_NRF52) || defined(ARCH_RP2040)
enterDfuMode();
#endif
break;
}
case meshtastic_AdminMessage_delete_file_request_tag: {
LOG_DEBUG("Client requesting to delete file: %s", r->delete_file_request);
Make STM compile again and update toolchain. (#2960) * Make STM compile again and update toolchain. The binary is too big for the flash. WIP * Making progress with OSFS, still WIP * more progress, still too big. Adding RAK3172 to the equasion * Make STM compile again and update toolchain. The binary is too big for the flash. WIP * Making progress with OSFS, still WIP * more progress, still too big. Adding RAK3172 to the equasion * still too big * minimize build * trunk fmt * fix a couple of symbol clashes * trunk fmt * down to 101% with a release vs. debug build and omitting the flash strings * fix compilation * fix compilation once more * update protobufs linkage * - Toolchain updated - Fixed macro error * silence compiler warning note: do something about this assert... * new toolkit and fix Power.cpp * STM32WL make it fit (#4330) * Add option to exclude I2C parts The I2C hals and related code uses a significant amount of flash space and aren't required for a basic node. * Add option to disable Admin and NodeInfo modules Disabled by default in minimal build. This saves a significant amount of flash * Disable unused hals These use up significant flash * Add float support for printf for debugging Makes serial look nice for debugging * This breaks my build for some reason * These build flags can save a bit of flash * Don't disable NodeInfo and Admin modules in minimal build They fit in flash * Don't include printf float support by default Only useful for debugging --------- Co-authored-by: Adam Lawson <dev@goshawk22.uk> --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> Co-authored-by: Adam Lawson <dev@goshawk22.uk>
2024-07-26 03:16:21 +02:00
#ifdef FSCom
if (FSCom.remove(r->delete_file_request)) {
LOG_DEBUG("Successfully deleted file");
} else {
LOG_DEBUG("Failed to delete file");
}
Make STM compile again and update toolchain. (#2960) * Make STM compile again and update toolchain. The binary is too big for the flash. WIP * Making progress with OSFS, still WIP * more progress, still too big. Adding RAK3172 to the equasion * Make STM compile again and update toolchain. The binary is too big for the flash. WIP * Making progress with OSFS, still WIP * more progress, still too big. Adding RAK3172 to the equasion * still too big * minimize build * trunk fmt * fix a couple of symbol clashes * trunk fmt * down to 101% with a release vs. debug build and omitting the flash strings * fix compilation * fix compilation once more * update protobufs linkage * - Toolchain updated - Fixed macro error * silence compiler warning note: do something about this assert... * new toolkit and fix Power.cpp * STM32WL make it fit (#4330) * Add option to exclude I2C parts The I2C hals and related code uses a significant amount of flash space and aren't required for a basic node. * Add option to disable Admin and NodeInfo modules Disabled by default in minimal build. This saves a significant amount of flash * Disable unused hals These use up significant flash * Add float support for printf for debugging Makes serial look nice for debugging * This breaks my build for some reason * These build flags can save a bit of flash * Don't disable NodeInfo and Admin modules in minimal build They fit in flash * Don't include printf float support by default Only useful for debugging --------- Co-authored-by: Adam Lawson <dev@goshawk22.uk> --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> Co-authored-by: Adam Lawson <dev@goshawk22.uk>
2024-07-26 03:16:21 +02:00
#endif
break;
}
#ifdef ARCH_PORTDUINO
2023-01-21 18:49:28 +01:00
case meshtastic_AdminMessage_exit_simulator_tag:
LOG_INFO("Exiting simulator");
2021-03-18 19:09:31 +08:00
_exit(0);
break;
#endif
2021-02-26 15:34:00 +08:00
default:
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
AdminMessageHandleResult handleResult = MeshModule::handleAdminMessageForAllModules(mp, r, &res);
2023-02-03 14:10:19 -06:00
if (handleResult == AdminMessageHandleResult::HANDLED_WITH_RESPONSE) {
setPassKey(&res);
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2023-02-03 14:10:19 -06:00
} else if (mp.decoded.want_response) {
LOG_DEBUG("Did not responded to a request that wanted a respond. req.variant=%d", r->which_payload_variant);
2023-02-03 14:10:19 -06:00
} else if (handleResult != AdminMessageHandleResult::HANDLED) {
// Probably a message sent by us or sent to our local node. FIXME, we should avoid scanning these messages
LOG_INFO("Ignore irrelevant admin %d", r->which_payload_variant);
}
2021-02-26 15:34:00 +08:00
break;
2021-02-21 14:03:44 +08:00
}
2022-11-25 20:33:12 +01:00
// If asked for a response and it is not yet set, generate an 'ACK' response
2023-02-03 14:10:19 -06:00
if (mp.decoded.want_response && !myReply) {
myReply = allocErrorResponse(meshtastic_Routing_Error_NONE, &mp);
2022-11-25 20:33:12 +01:00
}
return handled;
2021-02-21 14:03:44 +08:00
}
void AdminModule::handleGetModuleConfigResponse(const meshtastic_MeshPacket &mp, meshtastic_AdminMessage *r)
{
// Skip if it's disabled or no pins are exposed
if (!r->get_module_config_response.payload_variant.remote_hardware.enabled ||
r->get_module_config_response.payload_variant.remote_hardware.available_pins_count == 0) {
LOG_DEBUG("Remote hardware module disabled or no available_pins. Skip");
return;
}
for (uint8_t i = 0; i < devicestate.node_remote_hardware_pins_count; i++) {
if (devicestate.node_remote_hardware_pins[i].node_num == 0 || !devicestate.node_remote_hardware_pins[i].has_pin) {
continue;
}
for (uint8_t j = 0; j < r->get_module_config_response.payload_variant.remote_hardware.available_pins_count; j++) {
auto availablePin = r->get_module_config_response.payload_variant.remote_hardware.available_pins[j];
if (i < devicestate.node_remote_hardware_pins_count) {
devicestate.node_remote_hardware_pins[i].node_num = mp.from;
devicestate.node_remote_hardware_pins[i].pin = availablePin;
}
i++;
}
}
}
2022-05-02 22:00:24 +10:00
/**
* Setter methods
*/
void AdminModule::handleSetOwner(const meshtastic_User &o)
2021-02-21 14:03:44 +08:00
{
int changed = 0;
2023-02-03 14:10:19 -06:00
if (*o.long_name) {
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.long_name, o.long_name);
strncpy(owner.long_name, o.long_name, sizeof(owner.long_name));
2021-02-21 14:03:44 +08:00
}
2023-02-03 14:10:19 -06:00
if (*o.short_name) {
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.short_name, o.short_name);
strncpy(owner.short_name, o.short_name, sizeof(owner.short_name));
2021-02-21 14:03:44 +08:00
}
2023-02-03 14:10:19 -06:00
if (*o.id) {
2021-02-21 14:03:44 +08:00
changed |= strcmp(owner.id, o.id);
strncpy(owner.id, o.id, sizeof(owner.id));
2021-02-21 14:03:44 +08:00
}
2023-02-03 14:10:19 -06:00
if (owner.is_licensed != o.is_licensed) {
2023-02-09 10:48:17 -06:00
changed = 1;
2021-04-10 11:39:13 +08:00
owner.is_licensed = o.is_licensed;
}
2021-02-21 14:03:44 +08:00
2023-02-03 14:10:19 -06:00
if (changed) { // If nothing really changed, don't broadcast on the network or write to flash
service->reloadOwner(!hasOpenEditTransaction);
saveChanges(SEGMENT_DEVICESTATE);
2022-09-22 08:02:58 -05:00
}
2021-02-21 14:03:44 +08:00
}
void AdminModule::handleSetConfig(const meshtastic_Config &c)
2022-05-02 08:53:44 +10:00
{
auto changes = SEGMENT_CONFIG;
2023-01-28 14:32:57 -06:00
auto existingRole = config.device.role;
bool isRegionUnset = (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET);
bool requiresReboot = true;
2023-02-03 14:10:19 -06:00
switch (c.which_payload_variant) {
case meshtastic_Config_device_tag:
LOG_INFO("Set config: Device");
config.has_device = true;
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
if (config.device.double_tap_as_button_press == false && c.payload_variant.device.double_tap_as_button_press == true &&
accelerometerThread->enabled == false) {
config.device.double_tap_as_button_press = c.payload_variant.device.double_tap_as_button_press;
accelerometerThread->enabled = true;
accelerometerThread->start();
}
#endif
#ifdef LED_PIN
// Turn LED off if heartbeat by config
if (c.payload_variant.device.led_heartbeat_disabled) {
digitalWrite(LED_PIN, HIGH ^ LED_STATE_ON);
}
#endif
if (config.device.button_gpio == c.payload_variant.device.button_gpio &&
config.device.buzzer_gpio == c.payload_variant.device.buzzer_gpio &&
config.device.role == c.payload_variant.device.role &&
config.device.disable_triple_click == c.payload_variant.device.disable_triple_click &&
config.device.rebroadcast_mode == c.payload_variant.device.rebroadcast_mode) {
requiresReboot = false;
}
config.device = c.payload_variant.device;
if (config.device.rebroadcast_mode == meshtastic_Config_DeviceConfig_RebroadcastMode_NONE &&
IS_ONE_OF(config.device.role, meshtastic_Config_DeviceConfig_Role_ROUTER,
meshtastic_Config_DeviceConfig_Role_REPEATER)) {
config.device.rebroadcast_mode = meshtastic_Config_DeviceConfig_RebroadcastMode_ALL;
const char *warning = "Rebroadcast mode can't be set to NONE for a router or repeater\n";
LOG_WARN(warning);
sendWarning(warning);
}
// If we're setting router role for the first time, install its intervals
2023-01-28 14:32:57 -06:00
if (existingRole != c.payload_variant.device.role)
nodeDB->installRoleDefaults(c.payload_variant.device.role);
if (config.device.node_info_broadcast_secs < min_node_info_broadcast_secs) {
LOG_DEBUG("Tried to set node_info_broadcast_secs too low, setting to %d", min_node_info_broadcast_secs);
config.device.node_info_broadcast_secs = min_node_info_broadcast_secs;
}
// Router Client is deprecated; Set it to client
if (c.payload_variant.device.role == meshtastic_Config_DeviceConfig_Role_ROUTER_CLIENT) {
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
if (moduleConfig.store_forward.enabled && !moduleConfig.store_forward.is_server) {
moduleConfig.store_forward.is_server = true;
changes |= SEGMENT_MODULECONFIG;
requiresReboot = true;
}
}
#if USERPREFS_EVENT_MODE
// If we're in event mode, nobody is a Router or Repeater
if (config.device.role == meshtastic_Config_DeviceConfig_Role_ROUTER ||
config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER) {
config.device.role = meshtastic_Config_DeviceConfig_Role_CLIENT;
}
#endif
break;
case meshtastic_Config_position_tag:
LOG_INFO("Set config: Position");
config.has_position = true;
config.position = c.payload_variant.position;
// Save nodedb as well in case we got a fixed position packet
saveChanges(SEGMENT_DEVICESTATE, false);
break;
case meshtastic_Config_power_tag:
LOG_INFO("Set config: Power");
config.has_power = true;
// Really just the adc override is the only thing that can change without a reboot
if (config.power.device_battery_ina_address == c.payload_variant.power.device_battery_ina_address &&
config.power.is_power_saving == c.payload_variant.power.is_power_saving &&
config.power.ls_secs == c.payload_variant.power.ls_secs &&
config.power.min_wake_secs == c.payload_variant.power.min_wake_secs &&
config.power.on_battery_shutdown_after_secs == c.payload_variant.power.on_battery_shutdown_after_secs &&
config.power.sds_secs == c.payload_variant.power.sds_secs &&
config.power.wait_bluetooth_secs == c.payload_variant.power.wait_bluetooth_secs) {
requiresReboot = false;
}
config.power = c.payload_variant.power;
if (c.payload_variant.power.on_battery_shutdown_after_secs > 0 &&
c.payload_variant.power.on_battery_shutdown_after_secs < 30) {
LOG_WARN("Tried to set on_battery_shutdown_after_secs too low, set to min 30 seconds");
config.power.on_battery_shutdown_after_secs = 30;
}
break;
case meshtastic_Config_network_tag:
LOG_INFO("Set config: WiFi");
config.has_network = true;
config.network = c.payload_variant.network;
break;
case meshtastic_Config_display_tag:
LOG_INFO("Set config: Display");
config.has_display = true;
if (config.display.screen_on_secs == c.payload_variant.display.screen_on_secs &&
config.display.flip_screen == c.payload_variant.display.flip_screen &&
config.display.oled == c.payload_variant.display.oled) {
requiresReboot = false;
}
#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR
if (config.display.wake_on_tap_or_motion == false && c.payload_variant.display.wake_on_tap_or_motion == true &&
accelerometerThread->enabled == false) {
config.display.wake_on_tap_or_motion = c.payload_variant.display.wake_on_tap_or_motion;
accelerometerThread->enabled = true;
accelerometerThread->start();
}
#endif
config.display = c.payload_variant.display;
break;
case meshtastic_Config_lora_tag:
LOG_INFO("Set config: LoRa");
config.has_lora = true;
// If no lora radio parameters change, don't need to reboot
if (config.lora.use_preset == c.payload_variant.lora.use_preset && config.lora.region == c.payload_variant.lora.region &&
config.lora.modem_preset == c.payload_variant.lora.modem_preset &&
config.lora.bandwidth == c.payload_variant.lora.bandwidth &&
config.lora.spread_factor == c.payload_variant.lora.spread_factor &&
config.lora.coding_rate == c.payload_variant.lora.coding_rate &&
config.lora.tx_power == c.payload_variant.lora.tx_power &&
config.lora.frequency_offset == c.payload_variant.lora.frequency_offset &&
config.lora.override_frequency == c.payload_variant.lora.override_frequency &&
config.lora.channel_num == c.payload_variant.lora.channel_num &&
config.lora.sx126x_rx_boosted_gain == c.payload_variant.lora.sx126x_rx_boosted_gain) {
requiresReboot = false;
}
#ifdef RF95_FAN_EN
// Turn PA off if disabled by config
if (c.payload_variant.lora.pa_fan_disabled) {
digitalWrite(RF95_FAN_EN, LOW ^ 0);
} else {
digitalWrite(RF95_FAN_EN, HIGH ^ 0);
}
#endif
config.lora = c.payload_variant.lora;
// If we're setting region for the first time, init the region
2023-02-03 14:10:19 -06:00
if (isRegionUnset && config.lora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET) {
config.lora.tx_enabled = true;
initRegion();
if (myRegion->dutyCycle < 100) {
config.lora.ignore_mqtt = true; // Ignore MQTT by default if region has a duty cycle limit
}
if (strcmp(moduleConfig.mqtt.root, default_mqtt_root) == 0) {
sprintf(moduleConfig.mqtt.root, "%s/%s", default_mqtt_root, myRegion->name);
changes = SEGMENT_CONFIG | SEGMENT_MODULECONFIG;
}
}
break;
case meshtastic_Config_bluetooth_tag:
LOG_INFO("Set config: Bluetooth");
config.has_bluetooth = true;
config.bluetooth = c.payload_variant.bluetooth;
break;
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
case meshtastic_Config_security_tag:
LOG_INFO("Set config: Security");
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
config.security = c.payload_variant.security;
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN) && !(MESHTASTIC_EXCLUDE_PKI)
// We check for a potentially valid private key, and a blank public key, and regen the public key if needed.
if (config.security.private_key.size == 32 && !memfll(config.security.private_key.bytes, 0, 32) &&
(config.security.public_key.size == 0 || memfll(config.security.public_key.bytes, 0, 32))) {
if (crypto->regeneratePublicKey(config.security.public_key.bytes, config.security.private_key.bytes)) {
config.security.public_key.size = 32;
}
}
#endif
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
owner.public_key.size = config.security.public_key.size;
memcpy(owner.public_key.bytes, config.security.public_key.bytes, config.security.public_key.size);
2024-08-18 15:51:43 -05:00
#if !MESHTASTIC_EXCLUDE_PKI
crypto->setDHPrivateKey(config.security.private_key.bytes);
2024-08-18 15:51:43 -05:00
#endif
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
if (config.security.debug_log_api_enabled == c.payload_variant.security.debug_log_api_enabled &&
config.security.serial_enabled == c.payload_variant.security.serial_enabled)
requiresReboot = false;
break;
2022-05-02 22:00:24 +10:00
}
if (requiresReboot && !hasOpenEditTransaction) {
disableBluetooth();
}
saveChanges(changes, requiresReboot);
2022-05-02 22:00:24 +10:00
}
void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c)
2022-05-02 22:00:24 +10:00
{
if (!hasOpenEditTransaction)
disableBluetooth();
2023-02-03 14:10:19 -06:00
switch (c.which_payload_variant) {
case meshtastic_ModuleConfig_mqtt_tag:
LOG_INFO("Set module config: MQTT");
moduleConfig.has_mqtt = true;
moduleConfig.mqtt = c.payload_variant.mqtt;
break;
case meshtastic_ModuleConfig_serial_tag:
LOG_INFO("Set module config: Serial");
moduleConfig.has_serial = true;
moduleConfig.serial = c.payload_variant.serial;
break;
case meshtastic_ModuleConfig_external_notification_tag:
LOG_INFO("Set module config: External Notification");
moduleConfig.has_external_notification = true;
moduleConfig.external_notification = c.payload_variant.external_notification;
break;
case meshtastic_ModuleConfig_store_forward_tag:
LOG_INFO("Set module config: Store & Forward");
moduleConfig.has_store_forward = true;
moduleConfig.store_forward = c.payload_variant.store_forward;
break;
case meshtastic_ModuleConfig_range_test_tag:
LOG_INFO("Set module config: Range Test");
moduleConfig.has_range_test = true;
moduleConfig.range_test = c.payload_variant.range_test;
break;
case meshtastic_ModuleConfig_telemetry_tag:
LOG_INFO("Set module config: Telemetry");
moduleConfig.has_telemetry = true;
moduleConfig.telemetry = c.payload_variant.telemetry;
break;
case meshtastic_ModuleConfig_canned_message_tag:
LOG_INFO("Set module config: Canned Message");
moduleConfig.has_canned_message = true;
moduleConfig.canned_message = c.payload_variant.canned_message;
break;
case meshtastic_ModuleConfig_audio_tag:
LOG_INFO("Set module config: Audio");
moduleConfig.has_audio = true;
moduleConfig.audio = c.payload_variant.audio;
break;
case meshtastic_ModuleConfig_remote_hardware_tag:
LOG_INFO("Set module config: Remote Hardware");
moduleConfig.has_remote_hardware = true;
moduleConfig.remote_hardware = c.payload_variant.remote_hardware;
break;
2023-06-25 15:41:19 +02:00
case meshtastic_ModuleConfig_neighbor_info_tag:
LOG_INFO("Set module config: Neighbor Info");
2023-06-25 15:41:19 +02:00
moduleConfig.has_neighbor_info = true;
if (moduleConfig.neighbor_info.update_interval < min_neighbor_info_broadcast_secs) {
LOG_DEBUG("Tried to set update_interval too low, setting to %d", default_neighbor_info_broadcast_secs);
moduleConfig.neighbor_info.update_interval = default_neighbor_info_broadcast_secs;
}
2023-06-25 15:41:19 +02:00
moduleConfig.neighbor_info = c.payload_variant.neighbor_info;
break;
case meshtastic_ModuleConfig_detection_sensor_tag:
LOG_INFO("Set module config: Detection Sensor");
moduleConfig.has_detection_sensor = true;
moduleConfig.detection_sensor = c.payload_variant.detection_sensor;
break;
case meshtastic_ModuleConfig_ambient_lighting_tag:
LOG_INFO("Set module config: Ambient Lighting");
moduleConfig.has_ambient_lighting = true;
moduleConfig.ambient_lighting = c.payload_variant.ambient_lighting;
break;
2023-12-17 17:11:45 +01:00
case meshtastic_ModuleConfig_paxcounter_tag:
LOG_INFO("Set module config: Paxcounter");
2023-12-17 17:11:45 +01:00
moduleConfig.has_paxcounter = true;
moduleConfig.paxcounter = c.payload_variant.paxcounter;
break;
2022-05-02 08:53:44 +10:00
}
saveChanges(SEGMENT_MODULECONFIG);
2022-05-02 08:53:44 +10:00
}
void AdminModule::handleSetChannel(const meshtastic_Channel &cc)
2022-05-02 22:00:24 +10:00
{
channels.setChannel(cc);
channels.onConfigChanged(); // tell the radios about this change
saveChanges(SEGMENT_CHANNELS, false);
2022-05-02 22:00:24 +10:00
}
/**
2022-05-03 22:16:50 +10:00
* Getters
2022-05-02 22:00:24 +10:00
*/
void AdminModule::handleGetOwner(const meshtastic_MeshPacket &req)
2022-05-02 22:00:24 +10:00
{
2023-02-03 14:10:19 -06:00
if (req.decoded.want_response) {
2022-05-02 22:00:24 +10:00
// We create the reply here
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-03 22:16:50 +10:00
res.get_owner_response = owner;
2022-05-02 22:00:24 +10:00
res.which_payload_variant = meshtastic_AdminMessage_get_owner_response_tag;
setPassKey(&res);
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetConfig(const meshtastic_MeshPacket &req, const uint32_t configType)
2022-05-02 22:00:24 +10:00
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
2023-02-03 14:10:19 -06:00
if (req.decoded.want_response) {
switch (configType) {
case meshtastic_AdminMessage_ConfigType_DEVICE_CONFIG:
LOG_INFO("Get config: Device");
res.get_config_response.which_payload_variant = meshtastic_Config_device_tag;
res.get_config_response.payload_variant.device = config.device;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_POSITION_CONFIG:
LOG_INFO("Get config: Position");
res.get_config_response.which_payload_variant = meshtastic_Config_position_tag;
res.get_config_response.payload_variant.position = config.position;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_POWER_CONFIG:
LOG_INFO("Get config: Power");
res.get_config_response.which_payload_variant = meshtastic_Config_power_tag;
res.get_config_response.payload_variant.power = config.power;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_NETWORK_CONFIG:
LOG_INFO("Get config: Network");
res.get_config_response.which_payload_variant = meshtastic_Config_network_tag;
res.get_config_response.payload_variant.network = config.network;
writeSecret(res.get_config_response.payload_variant.network.wifi_psk,
sizeof(res.get_config_response.payload_variant.network.wifi_psk), config.network.wifi_psk);
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_DISPLAY_CONFIG:
LOG_INFO("Get config: Display");
res.get_config_response.which_payload_variant = meshtastic_Config_display_tag;
res.get_config_response.payload_variant.display = config.display;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_LORA_CONFIG:
LOG_INFO("Get config: LoRa");
res.get_config_response.which_payload_variant = meshtastic_Config_lora_tag;
res.get_config_response.payload_variant.lora = config.lora;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ConfigType_BLUETOOTH_CONFIG:
LOG_INFO("Get config: Bluetooth");
res.get_config_response.which_payload_variant = meshtastic_Config_bluetooth_tag;
res.get_config_response.payload_variant.bluetooth = config.bluetooth;
break;
case meshtastic_AdminMessage_ConfigType_SECURITY_CONFIG:
LOG_INFO("Get config: Security");
res.get_config_response.which_payload_variant = meshtastic_Config_security_tag;
res.get_config_response.payload_variant.security = config.security;
break;
case meshtastic_AdminMessage_ConfigType_SESSIONKEY_CONFIG:
LOG_INFO("Get config: Sessionkey");
res.get_config_response.which_payload_variant = meshtastic_Config_sessionkey_tag;
break;
2022-05-02 22:00:24 +10:00
}
// NOTE: The phone app needs to know the ls_secs value so it can properly expect sleep behavior.
2022-05-02 22:00:24 +10:00
// So even if we internally use 0 to represent 'use default' we still need to send the value we are
// using to the app (so that even old phone apps work with new device loads).
// r.get_radio_response.preferences.ls_secs = getPref_ls_secs();
// hideSecret(r.get_radio_response.preferences.wifi_ssid); // hmm - leave public for now, because only minimally private
// and useful for users to know current provisioning) hideSecret(r.get_radio_response.preferences.wifi_password);
2022-05-07 20:31:21 +10:00
// r.get_config_response.which_payloadVariant = Config_ModuleConfig_telemetry_tag;
res.which_payload_variant = meshtastic_AdminMessage_get_config_response_tag;
setPassKey(&res);
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetModuleConfig(const meshtastic_MeshPacket &req, const uint32_t configType)
2022-05-02 22:00:24 +10:00
{
meshtastic_AdminMessage res = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
2023-02-03 14:10:19 -06:00
if (req.decoded.want_response) {
switch (configType) {
case meshtastic_AdminMessage_ModuleConfigType_MQTT_CONFIG:
LOG_INFO("Get module config: MQTT");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_mqtt_tag;
res.get_module_config_response.payload_variant.mqtt = moduleConfig.mqtt;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_SERIAL_CONFIG:
LOG_INFO("Get module config: Serial");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_serial_tag;
res.get_module_config_response.payload_variant.serial = moduleConfig.serial;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_EXTNOTIF_CONFIG:
LOG_INFO("Get module config: External Notification");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_external_notification_tag;
res.get_module_config_response.payload_variant.external_notification = moduleConfig.external_notification;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_STOREFORWARD_CONFIG:
LOG_INFO("Get module config: Store & Forward");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_store_forward_tag;
res.get_module_config_response.payload_variant.store_forward = moduleConfig.store_forward;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_RANGETEST_CONFIG:
LOG_INFO("Get module config: Range Test");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_range_test_tag;
res.get_module_config_response.payload_variant.range_test = moduleConfig.range_test;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_TELEMETRY_CONFIG:
LOG_INFO("Get module config: Telemetry");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_telemetry_tag;
res.get_module_config_response.payload_variant.telemetry = moduleConfig.telemetry;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_CANNEDMSG_CONFIG:
LOG_INFO("Get module config: Canned Message");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_canned_message_tag;
res.get_module_config_response.payload_variant.canned_message = moduleConfig.canned_message;
2022-05-02 22:00:24 +10:00
break;
case meshtastic_AdminMessage_ModuleConfigType_AUDIO_CONFIG:
LOG_INFO("Get module config: Audio");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_audio_tag;
2022-11-08 15:04:24 -06:00
res.get_module_config_response.payload_variant.audio = moduleConfig.audio;
break;
case meshtastic_AdminMessage_ModuleConfigType_REMOTEHARDWARE_CONFIG:
LOG_INFO("Get module config: Remote Hardware");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_remote_hardware_tag;
res.get_module_config_response.payload_variant.remote_hardware = moduleConfig.remote_hardware;
break;
case meshtastic_AdminMessage_ModuleConfigType_NEIGHBORINFO_CONFIG:
LOG_INFO("Get module config: Neighbor Info");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_neighbor_info_tag;
res.get_module_config_response.payload_variant.neighbor_info = moduleConfig.neighbor_info;
break;
case meshtastic_AdminMessage_ModuleConfigType_DETECTIONSENSOR_CONFIG:
LOG_INFO("Get module config: Detection Sensor");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_detection_sensor_tag;
res.get_module_config_response.payload_variant.detection_sensor = moduleConfig.detection_sensor;
break;
case meshtastic_AdminMessage_ModuleConfigType_AMBIENTLIGHTING_CONFIG:
LOG_INFO("Get module config: Ambient Lighting");
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_ambient_lighting_tag;
res.get_module_config_response.payload_variant.ambient_lighting = moduleConfig.ambient_lighting;
break;
2023-12-17 17:11:45 +01:00
case meshtastic_AdminMessage_ModuleConfigType_PAXCOUNTER_CONFIG:
LOG_INFO("Get module config: Paxcounter");
2023-12-17 17:11:45 +01:00
res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_paxcounter_tag;
res.get_module_config_response.payload_variant.paxcounter = moduleConfig.paxcounter;
break;
2022-05-02 22:00:24 +10:00
}
// NOTE: The phone app needs to know the ls_secsvalue so it can properly expect sleep behavior.
2022-05-02 22:00:24 +10:00
// So even if we internally use 0 to represent 'use default' we still need to send the value we are
// using to the app (so that even old phone apps work with new device loads).
// r.get_radio_response.preferences.ls_secs = getPref_ls_secs();
// hideSecret(r.get_radio_response.preferences.wifi_ssid); // hmm - leave public for now, because only minimally private
// and useful for users to know current provisioning) hideSecret(r.get_radio_response.preferences.wifi_password);
2022-05-07 20:31:21 +10:00
// r.get_config_response.which_payloadVariant = Config_ModuleConfig_telemetry_tag;
res.which_payload_variant = meshtastic_AdminMessage_get_module_config_response_tag;
setPassKey(&res);
2022-05-03 22:16:50 +10:00
myReply = allocDataProtobuf(res);
2022-05-02 22:00:24 +10:00
}
}
void AdminModule::handleGetNodeRemoteHardwarePins(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
r.which_payload_variant = meshtastic_AdminMessage_get_node_remote_hardware_pins_response_tag;
2023-05-23 16:18:14 -05:00
for (uint8_t i = 0; i < devicestate.node_remote_hardware_pins_count; i++) {
if (devicestate.node_remote_hardware_pins[i].node_num == 0 || !devicestate.node_remote_hardware_pins[i].has_pin) {
continue;
}
r.get_node_remote_hardware_pins_response.node_remote_hardware_pins[i] = devicestate.node_remote_hardware_pins[i];
}
for (uint8_t i = 0; i < moduleConfig.remote_hardware.available_pins_count; i++) {
if (!moduleConfig.remote_hardware.available_pins[i].gpio_pin) {
continue;
}
meshtastic_NodeRemoteHardwarePin nodePin = meshtastic_NodeRemoteHardwarePin_init_default;
nodePin.node_num = nodeDB->getNodeNum();
2023-05-23 16:18:14 -05:00
nodePin.pin = moduleConfig.remote_hardware.available_pins[i];
r.get_node_remote_hardware_pins_response.node_remote_hardware_pins[i + 12] = nodePin;
}
setPassKey(&r);
myReply = allocDataProtobuf(r);
}
void AdminModule::handleGetDeviceMetadata(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
r.get_device_metadata_response = getDeviceMetadata();
r.which_payload_variant = meshtastic_AdminMessage_get_device_metadata_response_tag;
setPassKey(&r);
myReply = allocDataProtobuf(r);
}
2023-02-02 14:05:58 -06:00
void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &req)
{
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
2023-05-24 02:48:48 +02:00
meshtastic_DeviceConnectionStatus conn = meshtastic_DeviceConnectionStatus_init_zero;
2023-02-02 14:05:58 -06:00
#if HAS_WIFI
2023-02-02 14:40:18 -06:00
conn.has_wifi = true;
2023-02-03 10:49:44 -06:00
conn.wifi.has_status = true;
2023-02-03 11:00:05 -06:00
#ifdef ARCH_PORTDUINO
conn.wifi.status.is_connected = true;
2023-02-03 11:11:09 -06:00
#else
conn.wifi.status.is_connected = WiFi.status() == WL_CONNECTED;
2023-02-03 11:00:05 -06:00
#endif
2023-02-02 14:11:48 -06:00
strncpy(conn.wifi.ssid, config.network.wifi_ssid, 33);
2023-02-03 14:10:19 -06:00
if (conn.wifi.status.is_connected) {
2023-02-02 14:40:18 -06:00
conn.wifi.rssi = WiFi.RSSI();
2023-02-03 10:49:44 -06:00
conn.wifi.status.ip_address = WiFi.localIP();
#ifndef MESHTASTIC_EXCLUDE_MQTT
conn.wifi.status.is_mqtt_connected = mqtt && mqtt->isConnectedDirectly();
#endif
2023-02-03 10:49:44 -06:00
conn.wifi.status.is_syslog_connected = false; // FIXME wire this up
2023-02-02 14:11:48 -06:00
}
2023-02-02 14:05:58 -06:00
#endif
2023-02-02 14:40:18 -06:00
#if HAS_ETHERNET
conn.has_ethernet = true;
2023-02-03 10:49:44 -06:00
conn.ethernet.has_status = true;
2023-02-03 14:10:19 -06:00
if (Ethernet.linkStatus() == LinkON) {
2023-02-03 09:04:22 -06:00
conn.ethernet.status.is_connected = true;
conn.ethernet.status.ip_address = Ethernet.localIP();
#if !MESHTASTIC_EXCLUDE_MQTT
conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->isConnectedDirectly();
#endif
2023-02-03 09:17:28 -06:00
conn.ethernet.status.is_syslog_connected = false; // FIXME wire this up
2023-02-03 14:10:19 -06:00
} else {
2023-02-03 10:49:44 -06:00
conn.ethernet.status.is_connected = false;
}
2023-02-02 14:40:18 -06:00
#endif
2023-02-02 14:05:58 -06:00
#if HAS_BLUETOOTH
2023-02-03 10:49:44 -06:00
conn.has_bluetooth = true;
2023-02-03 08:50:10 -06:00
conn.bluetooth.pin = config.bluetooth.fixed_pin;
#ifdef ARCH_ESP32
if (config.bluetooth.enabled && nimbleBluetooth) {
conn.bluetooth.is_connected = nimbleBluetooth->isConnected();
conn.bluetooth.rssi = nimbleBluetooth->getRssi();
}
2023-02-03 08:50:10 -06:00
#elif defined(ARCH_NRF52)
if (config.bluetooth.enabled && nrf52Bluetooth) {
conn.bluetooth.is_connected = nrf52Bluetooth->isConnected();
}
#endif
2023-02-02 14:05:58 -06:00
#endif
2023-02-02 14:11:48 -06:00
conn.has_serial = true; // No serial-less devices
#if !EXCLUDE_POWER_FSM
2023-02-03 08:50:10 -06:00
conn.serial.is_connected = powerFSM.getState() == &stateSERIAL;
#else
conn.serial.is_connected = powerFSM.getState();
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
#endif
conn.serial.baud = SERIAL_BAUD;
2023-02-02 14:05:58 -06:00
2023-02-03 13:17:39 -06:00
r.get_device_connection_status_response = conn;
2023-02-02 14:05:58 -06:00
r.which_payload_variant = meshtastic_AdminMessage_get_device_connection_status_response_tag;
setPassKey(&r);
2023-02-02 14:05:58 -06:00
myReply = allocDataProtobuf(r);
}
void AdminModule::handleGetChannel(const meshtastic_MeshPacket &req, uint32_t channelIndex)
2022-05-02 22:00:24 +10:00
{
2023-02-03 14:10:19 -06:00
if (req.decoded.want_response) {
2022-05-02 22:00:24 +10:00
// We create the reply here
meshtastic_AdminMessage r = meshtastic_AdminMessage_init_default;
2022-05-02 22:00:24 +10:00
r.get_channel_response = channels.getByIndex(channelIndex);
r.which_payload_variant = meshtastic_AdminMessage_get_channel_response_tag;
setPassKey(&r);
2022-05-02 22:00:24 +10:00
myReply = allocDataProtobuf(r);
}
}
void AdminModule::reboot(int32_t seconds)
2022-10-19 19:19:04 -05:00
{
LOG_INFO("Reboot in %d seconds", seconds);
screen->startAlert("Rebooting...");
2022-10-19 19:19:04 -05:00
rebootAtMsec = (seconds < 0) ? 0 : (millis() + seconds * 1000);
}
void AdminModule::saveChanges(int saveWhat, bool shouldReboot)
{
2023-02-03 14:10:19 -06:00
if (!hasOpenEditTransaction) {
LOG_INFO("Save changes to disk");
service->reloadConfig(saveWhat); // Calls saveToDisk among other things
2023-02-03 14:10:19 -06:00
} else {
LOG_INFO("Delay save of changes to disk until the open transaction is committed");
}
if (shouldReboot && !hasOpenEditTransaction) {
reboot(DEFAULT_REBOOT_SECONDS);
}
}
2023-02-04 15:11:36 -06:00
void AdminModule::handleSetHamMode(const meshtastic_HamParameters &p)
{
// Set call sign and override lora limitations for licensed use
2023-02-04 15:11:36 -06:00
strncpy(owner.long_name, p.call_sign, sizeof(owner.long_name));
2023-02-09 19:32:32 -06:00
strncpy(owner.short_name, p.short_name, sizeof(owner.short_name));
2023-02-04 15:11:36 -06:00
owner.is_licensed = true;
config.lora.override_duty_cycle = true;
config.lora.tx_power = p.tx_power;
config.lora.override_frequency = p.frequency;
// Set node info broadcast interval to 10 minutes
// For FCC minimum call-sign announcement
config.device.node_info_broadcast_secs = 600;
2023-02-04 15:11:36 -06:00
config.device.rebroadcast_mode = meshtastic_Config_DeviceConfig_RebroadcastMode_LOCAL_ONLY;
2023-02-08 15:29:33 -06:00
// Remove PSK of primary channel for plaintext amateur usage
auto primaryChannel = channels.getByIndex(channels.getPrimaryIndex());
2023-02-08 20:21:33 -06:00
auto &channelSettings = primaryChannel.settings;
2023-02-08 15:29:33 -06:00
channelSettings.psk.bytes[0] = 0;
channelSettings.psk.size = 0;
channels.setChannel(primaryChannel);
channels.onConfigChanged();
service->reloadOwner(false);
saveChanges(SEGMENT_CONFIG | SEGMENT_DEVICESTATE | SEGMENT_CHANNELS);
2023-02-04 15:11:36 -06:00
}
AdminModule::AdminModule() : ProtobufModule("Admin", meshtastic_PortNum_ADMIN_APP, &meshtastic_AdminMessage_msg)
2021-02-26 15:34:00 +08:00
{
// restrict to the admin channel for rx
Re-implement PKI from #1509 (#4379) * Re-implement PKI from #1509 co-authored-by: edinnen <ethanjdinnen@protonmail.com> * Set the key lengnth to actually make PKI work. * Remove unused variable and initialize keys to null * move printBytes() to meshUtils * Don't reset PKI key son reboot unless needed. * Remove double encryption for PKI messages * Cleanup encrypt logic * Add the MESHTASTIC_EXCLUDE_PKI option, and set it for minimal builds. Required for STM32 targets for now. * Use SHA-256 for PKI key hashing, and add MESHTASTIC_EXCLUDE_PKI_KEYGEN for STM32 * Fix a crash when node is null * Don't send PKI encrypted packets while licensed * use chIndex 8 for PKI * Don't be so clever, that you corrupt incoming packets * Pass on channel 8 for now * Typo * Lock keys once non-zero * We in fact need 2 scratch buffers, to store the encrypted bytes, unencrypted bytes, and decoded protobuf. * Lighter approach to retaining known key * Attach the public key to PKI decrypted packets in device memory * Turn PKI back off for STM32 :( * Don't just memcp over a protobuf * Don't PKI encrypt nodeinfo packets * Add a bit more memory logging around nodeDB * Use the proper macro to refer to NODENUM_BROADCAST * Typo fix * Don't PKI encrypt ROUTING (naks and acks) * Adds SecurityConfig protobuf * Add admin messages over PKI * Disable PKI for the WIO-e5 * Add MINIMUM_SAFE_FREE_HEAP macro and set to safe 1.5k * Add missed "has_security" * Add the admin_channel_enabled option * STM32 again * add missed configuration.h at the top of files * Add EXCLUDE_TZ and RTC * Enable PKI build on STM32 once again * Attempt 1 at moving PKI to aes-ccm * Fix buffers for encrypt/decrypt * Eliminate unused aes variable * Add debugging lines * Set hash to 0 for PKI * Fix debug lines so they don't print pointers. * logic fix and more debug * Rather important typo * Check for short packets before attempting decrypt * Don't forget to give cryptoEngine the keys! * Use the right scratch buffer * Cleanup * moar cleanups * Minor hardening * Remove some in-progress stuff * Turn PKI back off on STM32 * Return false * 2.5 protos * Sync up protos * Add initial cryptography test vector tests * re-add MINIMUM_SAFE_FREE_HEAP * Housekeeping and comment fixes * Add explanatory comment about weak dh25519 keys --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
2024-08-10 13:45:41 -05:00
// boundChannel = Channels::adminChannel;
}
void AdminModule::setPassKey(meshtastic_AdminMessage *res)
{
if (session_time == 0 || millis() / 1000 > session_time + 150) {
for (int i = 0; i < 8; i++) {
session_passkey[i] = random();
}
session_time = millis() / 1000;
}
memcpy(res->session_passkey.bytes, session_passkey, 8);
res->session_passkey.size = 8;
printBytes("Set admin key to ", res->session_passkey.bytes, 8);
// if halfway to session_expire, regenerate session_passkey, reset the timeout
// set the key in the packet
}
bool AdminModule::checkPassKey(meshtastic_AdminMessage *res)
{ // check that the key in the packet is still valid
2024-08-19 11:12:42 -05:00
printBytes("Incoming session key: ", res->session_passkey.bytes, 8);
printBytes("Expected session key: ", session_passkey, 8);
return (session_time + 300 > millis() / 1000 && res->session_passkey.size == 8 &&
memcmp(res->session_passkey.bytes, session_passkey, 8) == 0);
}
bool AdminModule::messageIsResponse(const meshtastic_AdminMessage *r)
{
if (r->which_payload_variant == meshtastic_AdminMessage_get_channel_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_owner_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_config_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_module_config_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_canned_message_module_messages_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_device_metadata_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_ringtone_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_device_connection_status_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_node_remote_hardware_pins_response_tag ||
r->which_payload_variant == meshtastic_NodeRemoteHardwarePinsResponse_node_remote_hardware_pins_tag)
return true;
else
return false;
}
bool AdminModule::messageIsRequest(const meshtastic_AdminMessage *r)
{
if (r->which_payload_variant == meshtastic_AdminMessage_get_channel_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_owner_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_config_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_module_config_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_canned_message_module_messages_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_device_metadata_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_ringtone_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_device_connection_status_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_node_remote_hardware_pins_request_tag)
return true;
else
return false;
}
void AdminModule::sendWarning(const char *message)
{
meshtastic_ClientNotification *cn = clientNotificationPool.allocZeroed();
cn->level = meshtastic_LogRecord_Level_WARNING;
cn->time = getValidTime(RTCQualityFromNet);
strncpy(cn->message, message, sizeof(cn->message));
service->sendClientNotification(cn);
}
void disableBluetooth()
{
#if HAS_BLUETOOTH
#ifdef ARCH_ESP32
if (nimbleBluetooth)
nimbleBluetooth->deinit();
#elif defined(ARCH_NRF52)
if (nrf52Bluetooth)
nrf52Bluetooth->shutdown();
#endif
#endif
}