From 49b1f4c5aff5d511272b0fa20da103fee51ff504 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sat, 6 Mar 2021 14:21:20 +0800 Subject: [PATCH] oops - fix failed text message rx --- docs/software/TODO.md | 4 ++-- src/esp32/ESP32CryptoEngine.cpp | 2 +- src/mesh/Channels.cpp | 3 ++- src/mesh/CryptoEngine.cpp | 4 ++++ src/mesh/Router.cpp | 42 +++++++++++++++++++++++---------- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/docs/software/TODO.md b/docs/software/TODO.md index d256cd15..26fb0885 100644 --- a/docs/software/TODO.md +++ b/docs/software/TODO.md @@ -34,8 +34,8 @@ You probably don't care about this section - skip to the next one. * DONE combine acks and responses in a single message if possible (do routing plugin LAST and drop ACK if someone else has already replied) * DONE don't send packets we received from the phone BACK TOWARDS THE PHONE (possibly use fromnode 0 for packets the phone sends?) * fix 1.1.50 android debug panel display -* test android channel setting -* release to users +* DONE test android channel setting +* DONE release to users * DONE warn in android app about unset regions * DONE use set-channel from android * DONE add gui in android app for setting region diff --git a/src/esp32/ESP32CryptoEngine.cpp b/src/esp32/ESP32CryptoEngine.cpp index 9d86ffeb..f04614c7 100644 --- a/src/esp32/ESP32CryptoEngine.cpp +++ b/src/esp32/ESP32CryptoEngine.cpp @@ -54,7 +54,7 @@ class ESP32CryptoEngine : public CryptoEngine static uint8_t scratch[MAX_BLOCKSIZE]; size_t nc_off = 0; - // DEBUG_MSG("ESP32 encrypt!\n"); + // DEBUG_MSG("ESP32 crypt fr=%x, num=%x, numBytes=%d!\n", fromNode, (uint32_t) packetNum, numBytes); initNonce(fromNode, packetNum); assert(numBytes <= MAX_BLOCKSIZE); memcpy(scratch, bytes, numBytes); diff --git a/src/mesh/Channels.cpp b/src/mesh/Channels.cpp index 76ea3fb9..41493018 100644 --- a/src/mesh/Channels.cpp +++ b/src/mesh/Channels.cpp @@ -275,10 +275,11 @@ const char *Channels::getPrimaryName() bool Channels::decryptForHash(ChannelIndex chIndex, ChannelHash channelHash) { if(chIndex > getNumChannels() || getHash(chIndex) != channelHash) { - DEBUG_MSG("Skipping channel %d due to invalid hash/index\n", chIndex); + // DEBUG_MSG("Skipping channel %d (hash %x) due to invalid hash/index, want=%x\n", chIndex, getHash(chIndex), channelHash); return false; } else { + DEBUG_MSG("Using channel %d (hash 0x%x)\n", chIndex, channelHash); setCrypto(chIndex); return true; } diff --git a/src/mesh/CryptoEngine.cpp b/src/mesh/CryptoEngine.cpp index 74f4b783..59cb7ad8 100644 --- a/src/mesh/CryptoEngine.cpp +++ b/src/mesh/CryptoEngine.cpp @@ -4,6 +4,10 @@ void CryptoEngine::setKey(const CryptoKey &k) { DEBUG_MSG("Installing AES%d key!\n", k.length * 8); + /* for(uint8_t i = 0; i < k.length; i++) + DEBUG_MSG("%02x ", k.bytes[i]); + DEBUG_MSG("\n"); */ + key = k; } diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index d08124b4..8f566dff 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -143,6 +143,13 @@ ErrorCode Router::sendLocal(MeshPacket *p) } } +void printBytes(const char *label, const uint8_t *p, size_t numbytes) { + DEBUG_MSG("%s: ", label); + for(size_t i = 0; i < numbytes; i++) + DEBUG_MSG("%02x ", p[i]); + DEBUG_MSG("\n"); +} + /** * Send a packet on a suitable interface. This routine will * later free() the packet to pool. This routine is not allowed to stall. @@ -173,6 +180,8 @@ ErrorCode Router::send(MeshPacket *p) if (p->which_payloadVariant == MeshPacket_decoded_tag) { static uint8_t bytes[MAX_RHPACKETLEN]; // we have to use a scratch buffer because a union + // printPacket("pre encrypt", p); // portnum valid here + size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), Data_fields, &p->decoded); if (numbytes > MAX_RHPACKETLEN) { @@ -180,6 +189,8 @@ ErrorCode Router::send(MeshPacket *p) return ERRNO_TOO_LARGE; } + //printBytes("plaintext", bytes, numbytes); + auto hash = channels.setActiveByIndex(p->channel); if (hash < 0) { // No suitable channel could be found for sending @@ -189,7 +200,7 @@ ErrorCode Router::send(MeshPacket *p) // Now that we are encrypting the packet channel should be the hash (no longer the index) p->channel = hash; - crypto->encrypt(p->from, p->id, numbytes, bytes); + crypto->encrypt(getFrom(p), p->id, numbytes, bytes); // Copy back into the packet and set the variant type memcpy(p->encrypted.bytes, bytes, numbytes); @@ -230,20 +241,26 @@ bool Router::perhapsDecode(MeshPacket *p) if (channels.decryptForHash(chIndex, p->channel)) { // Try to decrypt the packet if we can static uint8_t bytes[MAX_RHPACKETLEN]; + size_t rawSize = p->encrypted.size; + assert(rawSize <= sizeof(bytes)); memcpy(bytes, p->encrypted.bytes, - p->encrypted - .size); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf - crypto->decrypt(p->from, p->id, p->encrypted.size, bytes); + rawSize); // we have to copy into a scratch buffer, because these bytes are a union with the decoded protobuf + crypto->decrypt(p->from, p->id, rawSize, bytes); + + //printBytes("plaintext", bytes, p->encrypted.size); // Take those raw bytes and convert them back into a well structured protobuf we can understand - memset(&p->decoded, 0, sizeof(p->decoded)); - if (!pb_decode_from_bytes(bytes, p->encrypted.size, Data_fields, &p->decoded)) { - DEBUG_MSG("Invalid protobufs in received mesh packet (bad psk?!\n"); - } else { + memset(&p->decoded, 0, sizeof(p->decoded)); + if (!pb_decode_from_bytes(bytes, rawSize, Data_fields, &p->decoded)) { + DEBUG_MSG("Invalid protobufs in received mesh packet (bad psk?)!\n"); + } else if(p->decoded.portnum == PortNum_UNKNOWN_APP) { + DEBUG_MSG("Invalid portnum (bad psk?)!\n"); + } + else { // parsing was successful + p->which_payloadVariant = MeshPacket_decoded_tag; // change type to decoded p->channel = chIndex; // change to store the index instead of the hash - // printPacket("decoded message", p); - p->which_payloadVariant = MeshPacket_decoded_tag; + printPacket("decoded message", p); return true; } } @@ -268,11 +285,10 @@ void Router::handleReceived(MeshPacket *p) p->rx_time = getValidTime(RTCQualityFromNet); // store the arrival timestamp for the phone // Take those raw bytes and convert them back into a well structured protobuf we can understand - bool decoded = perhapsDecode(p); - printPacket("handleReceived", p); - DEBUG_MSG("decoded=%d\n", decoded); + bool decoded = perhapsDecode(p); if (decoded) { // parsing was successful, queue for our recipient + printPacket("handleReceived", p); // call any promiscious plugins here, make a (non promisiocous) plugin for forwarding messages to phone api // sniffReceived(p);