|
@@ -38,6 +38,26 @@ class RelayRandomHopMsg(RelayNetMsg):
|
|
|
return "RandomHop TTL=%d" % self.ttl
|
|
|
|
|
|
|
|
|
+class CircuitCellMsg(RelayNetMsg):
|
|
|
+ """Send a message tagged with a circuit id."""
|
|
|
+
|
|
|
+ def __init__(self, circuitid, cell):
|
|
|
+ self.circid = circuitid
|
|
|
+ self.cell = cell
|
|
|
+
|
|
|
+ def __str__(self):
|
|
|
+ return "C%d:%s" % (self.circid, self.cell)
|
|
|
+
|
|
|
+ def size(self):
|
|
|
+ # circuitids are 4 bytes
|
|
|
+ return 4 + self.cell.size()
|
|
|
+
|
|
|
+
|
|
|
+class RelayCell(RelayNetMsg):
|
|
|
+ """All cells (which are sent inside a CircuitCellMsg, and so do not
|
|
|
+ need their own circuitid) should be a subclass of this class."""
|
|
|
+
|
|
|
+
|
|
|
class VanillaCreateCircuitMsg(RelayNetMsg):
|
|
|
"""The message for requesting circuit creation in Vanilla Onion
|
|
|
Routing."""
|
|
@@ -47,7 +67,7 @@ class VanillaCreateCircuitMsg(RelayNetMsg):
|
|
|
self.ntor_request = ntor_request
|
|
|
|
|
|
|
|
|
-class VanillaCreatedCircuitMsg(RelayNetMsg):
|
|
|
+class VanillaCreatedCircuitCell(RelayCell):
|
|
|
"""The message for responding to circuit creation in Vanilla Onion
|
|
|
Routing."""
|
|
|
|
|
@@ -55,7 +75,7 @@ class VanillaCreatedCircuitMsg(RelayNetMsg):
|
|
|
self.ntor_reply = ntor_reply
|
|
|
|
|
|
|
|
|
-class VanillaExtendCircuitMsg(RelayNetMsg):
|
|
|
+class VanillaExtendCircuitCell(RelayCell):
|
|
|
"""The message for requesting circuit creation in Vanilla Onion
|
|
|
Routing."""
|
|
|
|
|
@@ -64,7 +84,7 @@ class VanillaExtendCircuitMsg(RelayNetMsg):
|
|
|
self.ntor_request = ntor_request
|
|
|
|
|
|
|
|
|
-class VanillaExtendedCircuitMsg(RelayNetMsg):
|
|
|
+class VanillaExtendedCircuitCell(RelayCell):
|
|
|
"""The message for responding to circuit creation in Vanilla Onion
|
|
|
Routing."""
|
|
|
|
|
@@ -72,22 +92,7 @@ class VanillaExtendedCircuitMsg(RelayNetMsg):
|
|
|
self.ntor_reply = ntor_reply
|
|
|
|
|
|
|
|
|
-class CircuitCellMsg(RelayNetMsg):
|
|
|
- """Send a message tagged with a circuit id."""
|
|
|
-
|
|
|
- def __init__(self, circuitid, cell):
|
|
|
- self.circid = circuitid
|
|
|
- self.cell = cell
|
|
|
-
|
|
|
- def __str__(self):
|
|
|
- return "C%d:%s" % (self.circid, self.cell)
|
|
|
-
|
|
|
- def size(self):
|
|
|
- # circuitids are 4 bytes
|
|
|
- return 4 + self.cell.size()
|
|
|
-
|
|
|
-
|
|
|
-class EncryptedMsg(RelayNetMsg):
|
|
|
+class EncryptedCell(RelayCell):
|
|
|
"""Send a message encrypted with a symmetric key. In this
|
|
|
implementation, the encryption is not really done. A hash of the
|
|
|
key is stored with the message so that it can be checked at
|
|
@@ -100,7 +105,7 @@ class EncryptedMsg(RelayNetMsg):
|
|
|
def decrypt(self, key):
|
|
|
keyhash = nacl.hash.sha256(key)
|
|
|
if keyhash != self.keyhash:
|
|
|
- raise ValueError("EncryptedMsg key mismatch")
|
|
|
+ raise ValueError("EncryptedCell key mismatch")
|
|
|
return self.plaintext
|
|
|
|
|
|
|
|
@@ -178,7 +183,7 @@ class CircuitHandler:
|
|
|
self.deckey = deckey
|
|
|
self.next_layer = next_layer
|
|
|
def encrypt_msg(self, msg):
|
|
|
- return self.next_layer.encrypt_msg(EncryptedMsg(self.enckey, msg))
|
|
|
+ return self.next_layer.encrypt_msg(EncryptedCell(self.enckey, msg))
|
|
|
def decrypt_msg(self, msg):
|
|
|
return self.next_layer.decrypt_msg(msg).decrypt(self.deckey)
|
|
|
|
|
@@ -211,7 +216,7 @@ class CircuitHandler:
|
|
|
def received_cell(self, cell):
|
|
|
"""A cell has been received on this circuit. Dispatch it
|
|
|
according to its type."""
|
|
|
- if isinstance(cell, EncryptedMsg):
|
|
|
+ if isinstance(cell, EncryptedCell):
|
|
|
cell = self.crypt_layer.decrypt_msg(cell)
|
|
|
|
|
|
print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.cellhandler.myaddr, cell, self.circid, self.channel.peer.cellhandler.myaddr))
|
|
@@ -407,7 +412,8 @@ class CellRelay(CellHandler):
|
|
|
deckey = nacl.hash.sha256(secret + b'upstream')
|
|
|
circhandler.add_crypt_layer(enckey, deckey)
|
|
|
# Send the ntor reply
|
|
|
- self.send_msg(CircuitCellMsg(msg.circid, VanillaCreatedCircuitMsg(reply)), peeraddr)
|
|
|
+ self.send_msg(CircuitCellMsg(msg.circid, \
|
|
|
+ VanillaCreatedCircuitCell(reply)), peeraddr)
|
|
|
else:
|
|
|
return super().received_msg(msg, peeraddr, channel)
|
|
|
|