From 13b98a6bc2fa044ea47c754115f82a6ed0be2845 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 15 Nov 2026 12:25:53 +0100 Subject: [PATCH] Cleaned up tunnel synthesis handler --- RNS/Interfaces/Interface.py | 1 + RNS/Transport.py | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/RNS/Interfaces/Interface.py b/RNS/Interfaces/Interface.py index c5ee4349..3b817731 100755 --- a/RNS/Interfaces/Interface.py +++ b/RNS/Interfaces/Interface.py @@ -131,6 +131,7 @@ class Interface: self.tunnel_id = None self.local_tunnel_id = None self.tunnels_allowed = False + self.synthesis_received = False self.wants_tunnel = False self.remote_transport = None self.ingress_control = True diff --git a/RNS/Transport.py b/RNS/Transport.py index 8d4ddcbd..a777b677 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -3155,19 +3155,27 @@ class Transport: def tunnel_synthesize_handler(data, packet): try: if not packet.receiving_interface.tunnels_allowed: return - if packet.receiving_interface.remote_transport: - RNS.log(f"Tunnel already synthesized on {packet.receiving_interface}, ignoring synthesis packet", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None - return + if packet.receiving_interface.remote_transport: return packet.receiving_interface.protocol_violation(f"Tunnel synthesis packet on already established tunnel") + if packet.receiving_interface.synthesis_received: return packet.receiving_interface.protocol_violation(f"Additional synthesis packet received") + packet.receiving_interface.synthesis_received = True - expected_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8 - if len(data) == expected_length: - public_key = data[:RNS.Identity.KEYSIZE//8] - interface_hash = data[RNS.Identity.KEYSIZE//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8] + max_length = RNS.Packet.MDU + required_length = RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8+RNS.Identity.SIGLENGTH//8 + if len(data) > max_length: return packet.receiving_interface.protocol_violation("Excessive tunnel synthesis packet size") + if len(data) < required_length: return packet.receiving_interface.protocol_violation("Insufficient tunnel synthesis packet size") + else: + key_size = RNS.Identity.KEYSIZE//8 + sig_len = RNS.Identity.SIGLENGTH//8 + hash_len = RNS.Identity.HASHLENGTH//8 + trunc_hash_len = RNS.Reticulum.TRUNCATED_HASHLENGTH//8 + + public_key = data[:key_size] + interface_hash = data[key_size:key_size+hash_len] tunnel_id_data = public_key+interface_hash tunnel_id = RNS.Identity.full_hash(tunnel_id_data) - random_hash = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8:RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8] + random_hash = data[key_size+hash_len:key_size+hash_len+trunc_hash_len] - signature = data[RNS.Identity.KEYSIZE//8+RNS.Identity.HASHLENGTH//8+RNS.Reticulum.TRUNCATED_HASHLENGTH//8:expected_length] + signature = data[key_size+hash_len+trunc_hash_len:key_size+hash_len+trunc_hash_len+sig_len] signed_data = tunnel_id_data+random_hash remote_transport_identity = RNS.Identity(create_keys=False)