Skip to content

Commit 810b1f8

Browse files
author
Scott Powell
committed
* Mesh::onAnonDataRecv() slight optimisation, so that shared-secret calc doesn't need to be repeated
* SensporMesh: req_type now optionally encoded in anon_req payload (so can send various requests without a prior login)
1 parent 7fb7b69 commit 810b1f8

File tree

6 files changed

+76
-60
lines changed

6 files changed

+76
-60
lines changed

examples/simple_repeater/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
149149
oldest->id = id;
150150
oldest->out_path_len = -1; // initially out_path is unknown
151151
oldest->last_timestamp = 0;
152-
self_id.calcSharedSecret(oldest->secret, id); // calc ECDH shared secret
153152
return oldest;
154153
}
155154

@@ -341,8 +340,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
341340
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
342341
}
343342

344-
void onAnonDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Identity& sender, uint8_t* data, size_t len) override {
345-
if (type == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
343+
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override {
344+
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
346345
uint32_t timestamp;
347346
memcpy(&timestamp, data, 4);
348347

@@ -369,6 +368,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
369368
client->last_timestamp = timestamp;
370369
client->last_activity = getRTCClock()->getCurrentTime();
371370
client->is_admin = is_admin;
371+
memcpy(client->secret, secret, PUB_KEY_SIZE);
372372

373373
uint32_t now = getRTCClock()->getCurrentTimeUnique();
374374
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp

examples/simple_room_server/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
188188
newClient->id = id;
189189
newClient->out_path_len = -1; // initially out_path is unknown
190190
newClient->last_timestamp = 0;
191-
self_id.calcSharedSecret(newClient->secret, id); // calc ECDH shared secret
192191
return newClient;
193192
}
194193

@@ -432,8 +431,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
432431
return true;
433432
}
434433

435-
void onAnonDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Identity& sender, uint8_t* data, size_t len) override {
436-
if (type == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
434+
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override {
435+
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
437436
uint32_t sender_timestamp, sender_sync_since;
438437
memcpy(&sender_timestamp, data, 4);
439438
memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp
@@ -465,6 +464,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
465464
client->sync_since = sender_sync_since;
466465
client->pending_ack = 0;
467466
client->push_failures = 0;
467+
memcpy(client->secret, secret, PUB_KEY_SIZE);
468468

469469
uint32_t now = getRTCClock()->getCurrentTime();
470470
client->last_activity = now;

examples/simple_sensor/SensorMesh.cpp

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
/* ------------------------------ Code -------------------------------- */
4848

49+
#define REQ_TYPE_LOGIN 0x00
4950
#define REQ_TYPE_GET_STATUS 0x01
5051
#define REQ_TYPE_KEEP_ALIVE 0x02
5152
#define REQ_TYPE_GET_TELEMETRY_DATA 0x03
@@ -139,12 +140,10 @@ void SensorMesh::saveContacts() {
139140
}
140141
}
141142

142-
int SensorMesh::handleRequest(ContactInfo& sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len) {
143-
// uint32_t now = getRTCClock()->getCurrentTimeUnique();
144-
// memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
143+
uint8_t SensorMesh::handleRequest(bool is_admin, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) {
145144
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
146145

147-
switch (payload[0]) {
146+
switch (req_type) {
148147
case REQ_TYPE_GET_TELEMETRY_DATA: {
149148
telemetry.reset();
150149
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
@@ -253,63 +252,79 @@ int SensorMesh::getAGCResetInterval() const {
253252
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
254253
}
255254

256-
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Identity& sender, uint8_t* data, size_t len) {
257-
if (type == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
255+
uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data) {
256+
bool is_admin;
257+
if (strcmp((char *) data, _prefs.password) == 0) { // check for valid password
258+
is_admin = true;
259+
} else if (strcmp((char *) data, _prefs.guest_password) == 0) { // check guest password
260+
is_admin = false;
261+
} else {
262+
#if MESH_DEBUG
263+
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]);
264+
#endif
265+
return 0;
266+
}
267+
268+
auto client = putContact(sender); // add to contacts (if not already known)
269+
if (sender_timestamp <= client->last_timestamp) {
270+
MESH_DEBUG_PRINTLN("Possible login replay attack!");
271+
return 0; // FATAL: client table is full -OR- replay attack
272+
}
273+
274+
MESH_DEBUG_PRINTLN("Login success!");
275+
client->last_timestamp = sender_timestamp;
276+
client->last_activity = getRTCClock()->getCurrentTime();
277+
client->type = is_admin ? 1 : 0;
278+
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
279+
280+
if (is_admin) {
281+
// only need to saveContacts() if this is an admin
282+
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
283+
}
284+
285+
uint32_t now = getRTCClock()->getCurrentTimeUnique();
286+
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
287+
reply_data[4] = RESP_SERVER_LOGIN_OK;
288+
reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
289+
reply_data[6] = client->type;
290+
reply_data[7] = 0; // FUTURE: reserved
291+
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
292+
293+
return 12; // reply length
294+
}
295+
296+
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) {
297+
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
258298
uint32_t timestamp;
259299
memcpy(&timestamp, data, 4);
260300

261-
bool is_admin;
262301
data[len] = 0; // ensure null terminator
263-
if (strcmp((char *) &data[4], _prefs.password) == 0) { // check for valid password
264-
is_admin = true;
265-
} else if (strcmp((char *) &data[4], _prefs.guest_password) == 0) { // check guest password
266-
is_admin = false;
267-
} else {
268-
#if MESH_DEBUG
269-
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]);
270-
#endif
271-
return;
272-
}
273302

274-
auto client = putContact(sender); // add to contacts (if not already known)
275-
if (timestamp <= client->last_timestamp) {
276-
MESH_DEBUG_PRINTLN("Possible login replay attack!");
277-
return; // FATAL: client table is full -OR- replay attack
303+
uint8_t req_code;
304+
uint8_t i = 4;
305+
if (data[4] < 32) { // non-print char, is a request code
306+
req_code = data[i++];
307+
} else {
308+
req_code = REQ_TYPE_LOGIN;
278309
}
279310

280-
MESH_DEBUG_PRINTLN("Login success!");
281-
client->last_timestamp = timestamp;
282-
client->last_activity = getRTCClock()->getCurrentTime();
283-
client->type = is_admin ? 1 : 0;
284-
self_id.calcSharedSecret(client->shared_secret, client->id); // calc ECDH shared secret
285-
286-
if (is_admin) {
287-
// only need to saveContacts() if this is an admin
288-
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
311+
uint8_t reply_len;
312+
if (req_code == REQ_TYPE_LOGIN) {
313+
reply_len = handleLoginReq(sender, secret, timestamp, &data[i]);
314+
} else {
315+
reply_len = handleRequest(false, timestamp, req_code, &data[i], len - i);
289316
}
290317

291-
uint32_t now = getRTCClock()->getCurrentTimeUnique();
292-
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
293-
reply_data[4] = RESP_SERVER_LOGIN_OK;
294-
reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
295-
reply_data[6] = client->type;
296-
reply_data[7] = 0; // FUTURE: reserved
297-
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
318+
if (reply_len == 0) return; // invalid request
298319

299320
if (packet->isRouteFlood()) {
300321
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
301-
mesh::Packet* path = createPathReturn(sender, client->shared_secret, packet->path, packet->path_len,
302-
PAYLOAD_TYPE_RESPONSE, reply_data, 12);
322+
mesh::Packet* path = createPathReturn(sender, secret, packet->path, packet->path_len,
323+
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
303324
if (path) sendFlood(path, SERVER_RESPONSE_DELAY);
304325
} else {
305-
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, client->shared_secret, reply_data, 12);
306-
if (reply) {
307-
if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
308-
sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
309-
} else {
310-
sendFlood(reply, SERVER_RESPONSE_DELAY);
311-
}
312-
}
326+
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
327+
if (reply) sendFlood(reply, SERVER_RESPONSE_DELAY);
313328
}
314329
}
315330
}
@@ -361,7 +376,7 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
361376
memcpy(&timestamp, data, 4);
362377

363378
if (timestamp > from.last_timestamp) { // prevent replay attacks
364-
int reply_len = handleRequest(from, timestamp, &data[4], len - 4);
379+
uint8_t reply_len = handleRequest(from.isAdmin(), timestamp, data[4], &data[5], len - 5);
365380
if (reply_len == 0) return; // invalid command
366381

367382
from.last_timestamp = timestamp;

examples/simple_sensor/SensorMesh.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
103103
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
104104
int getInterferenceThreshold() const override;
105105
int getAGCResetInterval() const override;
106-
void onAnonDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Identity& sender, uint8_t* data, size_t len) override;
106+
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
107107
int searchPeersByHash(const uint8_t* hash) override;
108108
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
109-
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len);
109+
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len) override;
110110
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
111111
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
112112

@@ -125,7 +125,8 @@ class SensorMesh : public mesh::Mesh, public CommonCLICallbacks {
125125

126126
void loadContacts();
127127
void saveContacts();
128-
int handleRequest(ContactInfo& sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
128+
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data);
129+
uint8_t handleRequest(bool is_admin, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len);
129130
mesh::Packet* createSelfAdvert();
130131
ContactInfo* putContact(const mesh::Identity& id);
131132

src/Mesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) {
181181
uint8_t data[MAX_PACKET_PAYLOAD];
182182
int len = Utils::MACThenDecrypt(secret, data, macAndData, pkt->payload_len - i);
183183
if (len > 0) { // success!
184-
onAnonDataRecv(pkt, pkt->getPayloadType(), sender, data, len);
184+
onAnonDataRecv(pkt, secret, sender, data, len);
185185
pkt->markDoNotRetransmit();
186186
}
187187
}

src/Mesh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ class Mesh : public Dispatcher {
107107
/**
108108
* \brief A (now decrypted) data packet has been received.
109109
* NOTE: these can be received multiple times (per sender/contents), via different routes
110-
* \param type one of: PAYLOAD_TYPE_ANON_REQ
110+
* \param secret ECDH shared secret
111111
* \param sender public key provided by sender
112112
*/
113-
virtual void onAnonDataRecv(Packet* packet, uint8_t type, const Identity& sender, uint8_t* data, size_t len) { }
113+
virtual void onAnonDataRecv(Packet* packet, const uint8_t* secret, const Identity& sender, uint8_t* data, size_t len) { }
114114

115115
/**
116116
* \brief A path TO 'sender' has been received. (also with optional 'extra' data encoded)

0 commit comments

Comments
 (0)