|
@@ -10,10 +10,21 @@ import nacl.signing
|
|
|
import nacl.public
|
|
|
import nacl.hash
|
|
|
import nacl.bindings
|
|
|
+import hashlib
|
|
|
|
|
|
import network
|
|
|
import dirauth
|
|
|
|
|
|
+class VRF():
|
|
|
+ def __init__(self, private_key):
|
|
|
+ self.private_key = private_key
|
|
|
+
|
|
|
+ def get_output(self, vrf_input):
|
|
|
+ output = random.choice(str(vrf_input))
|
|
|
+ m = hashlib.sha256()
|
|
|
+ m.update(str(output).encode())
|
|
|
+ m.digest()
|
|
|
+
|
|
|
class RelayNetMsg(network.NetMsg):
|
|
|
"""The subclass of NetMsg for messages between relays and either
|
|
|
relays or clients."""
|
|
@@ -160,7 +171,7 @@ class TelescopingCreatedCircuitCell(RelayCell):
|
|
|
|
|
|
class TelescopingExtendCircuitCell(RelayCell):
|
|
|
"""The message for requesting circuit extension in Telescoping Walking
|
|
|
- Onionss."""
|
|
|
+ Onions."""
|
|
|
|
|
|
def __init__(self, idx, ntor_request):
|
|
|
self.idx = idx
|
|
@@ -176,20 +187,20 @@ class TelescopingExtendedCircuitCell(RelayCell):
|
|
|
|
|
|
class SinglePassCreateCircuitMsg(RelayNetMsg):
|
|
|
"""The message for requesting circuit creation in Single Pass Onion
|
|
|
- Routing."""
|
|
|
+ Routing. This is used to extend a Single-Pass circuit by clients."""
|
|
|
|
|
|
- def __init__(self, circid, ntor_request, client_path_selection_key):
|
|
|
+ def __init__(self, circid, ntor_request, client_path_selection_key, ttl):
|
|
|
self.circid = circid
|
|
|
self.ntor_request = ntor_request
|
|
|
self.client_path_selection_key = client_path_selection_key
|
|
|
+ self.ttl = ttl
|
|
|
|
|
|
class SinglePassCreatedCircuitCell(RelayCell):
|
|
|
"""The message for responding to circuit creation in Single-Pass Walking
|
|
|
Onions."""
|
|
|
|
|
|
- def __init__(self, ntor_reply, circuit_snips):
|
|
|
+ def __init__(self, ntor_reply):
|
|
|
self.ntor_reply = ntor_reply
|
|
|
- self.circuit_snips = circuit_snips
|
|
|
|
|
|
|
|
|
class EncryptedCell(RelayCell):
|
|
@@ -498,7 +509,7 @@ class TelescopingCreatedRelayHandler:
|
|
|
def received_cell(self, circhandler, cell):
|
|
|
logging.debug("Handle a TelescopingCreatedCircuit received by a relay")
|
|
|
# Remove ourselves from handling a second
|
|
|
- # VanillaCreatedCircuitCell on this circuit
|
|
|
+ # TelescopingCreatedCircuitCell on this circuit
|
|
|
circhandler.replace_celltype_handler(TelescopingCreatedCircuitCell, None)
|
|
|
|
|
|
# Just forward a TelescopingExtendedCircuitCell back towards the
|
|
@@ -506,6 +517,28 @@ class TelescopingCreatedRelayHandler:
|
|
|
circhandler.adjacent_circuit_handler.send_cell(
|
|
|
TelescopingExtendedCircuitCell(cell.ntor_reply, self.next_snip))
|
|
|
|
|
|
+class SinglePassCreatedRelayHandler:
|
|
|
+ """Handle a SinglePassCreatedCircuitCell received by a _relay_ that
|
|
|
+ recently received a SinglePassCreateCircuitMsg from this relay."""
|
|
|
+
|
|
|
+ def __init__(self, ntorreply, next_snip, vrf_output):
|
|
|
+ self.next_snip = next_snip
|
|
|
+ self.vrf_output = vrf_output
|
|
|
+ self.ntorreply = ntorreply
|
|
|
+
|
|
|
+ def received_cell(self, circhandler, cell):
|
|
|
+ logging.debug("Handle a SinglePassCreatedCircuitCell received by a relay")
|
|
|
+ # Remove ourselves from handling a second
|
|
|
+ # SinglePassCreatedCircuitCell on this circuit
|
|
|
+ circhandler.replace_celltype_handler(SinglePassCreatedCircuitCell, None)
|
|
|
+
|
|
|
+ # Just forward a SinglePassCreatedCircuitCell back towards the
|
|
|
+ # client
|
|
|
+ circhandler.adjacent_circuit_handler.send_cell(
|
|
|
+ SinglePassCreatedCircuitCell(cell.ntor_reply, self.next_snip))
|
|
|
+
|
|
|
+ sys.exit("have not yet implemented circuit handling for single-pass in relays")
|
|
|
+
|
|
|
class CircuitHandler:
|
|
|
"""A class for managing sending and receiving encrypted cells on a
|
|
|
particular circuit."""
|
|
@@ -879,11 +912,37 @@ class RelayChannelManager(ChannelManager):
|
|
|
# Send the ntor reply
|
|
|
self.send_msg(CircuitCellMsg(msg.circid,
|
|
|
TelescopingCreatedCircuitCell(reply)), peeraddr)
|
|
|
- elif isinstance(msg, SinglePassCreateCircuitMsg):
|
|
|
+ elif isinstance(msg, SinglePassCreateCircuitMsg) and msg.ttl == 0:
|
|
|
+ # we are the end of the circuit, just establish a shared key and
|
|
|
+ # return
|
|
|
+ logging.debug("RelayChannelManager: Single-Pass TTL is 0, replying without extending")
|
|
|
+
|
|
|
# A new circuit has arrived
|
|
|
circhandler = channel.new_circuit_with_circid(msg.circid)
|
|
|
+
|
|
|
+ # Create the ntor reply
|
|
|
+ reply, secret = NTor.reply(self.onionkey, self.idpubkey,
|
|
|
+ msg.ntor_request, self.perfstats)
|
|
|
+ # Set up the circuit to use the shared secret
|
|
|
+ enckey = nacl.hash.sha256(secret + b'downstream')
|
|
|
+ deckey = nacl.hash.sha256(secret + b'upstream')
|
|
|
+ circhandler.add_crypt_layer(enckey, deckey)
|
|
|
+
|
|
|
+ # Send the ntor reply
|
|
|
+ self.send_msg(CircuitCellMsg(msg.circid,
|
|
|
+ SinglePassCreatedCircuitCell(reply)), peeraddr)
|
|
|
+
|
|
|
+ elif isinstance(msg, SinglePassCreateCircuitMsg) and msg.ttl > 0:
|
|
|
+ # A new circuit has arrived
|
|
|
+ circhandler = channel.new_circuit_with_circid(msg.circid)
|
|
|
+
|
|
|
+ # because the ttl is greater than 0, we need to extend the circuit. To do this, we
|
|
|
+ # need to derive the client's blinded keys, deterministically
|
|
|
+ # select the next relay in the circuit, etc.
|
|
|
+ logging.debug("RelayChannelManager: Single-Pass TTL is greater than 0; extending")
|
|
|
+
|
|
|
# Create the ntor reply for the circuit-extension key
|
|
|
- (reply, secret), blinded_client_pubkey = NTor.reply(self.onionkey, self.idpubkey,
|
|
|
+ (ntorreply, secret), blinded_client_encr_key = NTor.reply(self.onionkey, self.idpubkey,
|
|
|
msg.ntor_request, self.perfstats, b'circuit')
|
|
|
|
|
|
# Set up the circuit to use the shared secret established from the
|
|
@@ -892,45 +951,45 @@ class RelayChannelManager(ChannelManager):
|
|
|
deckey = nacl.hash.sha256(secret + b'upstream')
|
|
|
circhandler.add_crypt_layer(enckey, deckey)
|
|
|
|
|
|
- logging.debug("WARNING: Unimplemented! Should check the TTL, if it isn't \
|
|
|
- zero, decriment it and pass it along. Otherwise, just reply \
|
|
|
- without extending.")
|
|
|
-
|
|
|
- # here, we will directly extend the circuit ourselves, after doing
|
|
|
- # the following:
|
|
|
- # 1. determining the next relay using the client's path selection
|
|
|
- # key in conjunction with our own
|
|
|
- # 2. blinding each of the client's public keys to send to the next
|
|
|
- # hop.
|
|
|
- idx_as_hex, blinded_client_pubkey = Sphinx.server(msg.client_path_selection_key,
|
|
|
+ # here, we will directly extend the circuit ourselves, after
|
|
|
+ # determining the next relay using the client's path selection
|
|
|
+ # key in conjunction with our own
|
|
|
+ idx_as_hex, blinded_client_path_selection_key = Sphinx.server(msg.client_path_selection_key,
|
|
|
self.path_selection_key, b'circuit', False, self.perfstats)
|
|
|
|
|
|
logging.debug("RelayChannelManager: Unimplemented! need to translate idx into endive index")
|
|
|
logging.debug("RelayChannelManager: Unimplemented! need to pick the next relay using the shared secret between the client and the relay.")
|
|
|
|
|
|
- nexthop = self.relaypicker.pick_weighted_relay()
|
|
|
- if nexthop == None:
|
|
|
- logging.debug("WARNING: Unimplemented! Need to validate next hop is not null, if it is, we should send a CLOSE cell.")
|
|
|
+ # TODO temporary
|
|
|
+ next_hop = None
|
|
|
+ while next_hop == None or next_hop.snipdict["idkey"] == self.idpubkey:
|
|
|
+ logging.debug("WARNING: Unimplemented! Need to validate next hop is not null or ourselves, if it is, we should send a CLOSE cell.")
|
|
|
+ idx = self.relaypicker.pick_weighted_relay_index()
|
|
|
+ next_hop = self.relaypicker.pick_relay_by_uniform_index(idx)
|
|
|
+
|
|
|
+ # simpulate the VRF output for now
|
|
|
+ vrf_output = VRF(self.onionkey).get_output(idx)
|
|
|
+
|
|
|
+ # Allocate a new circuit id to the requested next hop
|
|
|
+ channelmgr = circhandler.channel.channelmgr
|
|
|
+ nexthopchannel = channelmgr.get_channel_to(next_hop.snipdict["addr"])
|
|
|
+ newcircid, newcirchandler = nexthopchannel.new_circuit()
|
|
|
+
|
|
|
+ # Connect the existing and new circuits together
|
|
|
+ circhandler.adjacent_circuit_handler = newcirchandler
|
|
|
+ newcirchandler.adjacent_circuit_handler = circhandler
|
|
|
|
|
|
# Add a handler for once the next relay replies to say that the
|
|
|
# circuit has been created
|
|
|
# be at most one on this circuit).
|
|
|
circhandler.replace_celltype_handler(
|
|
|
SinglePassCreatedCircuitCell,
|
|
|
- SinglePassCreatedCircuitHandler(ntorreply, next_hop))
|
|
|
-
|
|
|
-# # Allocate a new circuit id to the requested next hop
|
|
|
-# channelmgr = circhandler.channel.channelmgr
|
|
|
-# nexthopchannel = channelmgr.get_channel_to(next_snip.snipdict["addr"])
|
|
|
-# newcircid, newcirchandler = nexthopchannel.new_circuit()
|
|
|
-#
|
|
|
-# # Send the next create message to the next hop
|
|
|
-# # TODO add the correct interface here
|
|
|
-# self.send_msg(CircuitCellMsg(msg.circid,
|
|
|
-# SinglePassCreateCircuitCell(newcircid, ntorrequest,
|
|
|
-# next_client_path_selection_key)), peeraddr)
|
|
|
-#
|
|
|
- sys.exit("have not yet implemented circuit handling for single-pass in relays")
|
|
|
+ SinglePassCreatedRelayHandler(ntorreply, next_hop, vrf_output))
|
|
|
+
|
|
|
+ # Send the next create message to the next hop
|
|
|
+ nexthopchannel.send_msg(SinglePassCreateCircuitMsg(newcircid,
|
|
|
+ blinded_client_encr_key, blinded_client_path_selection_key,
|
|
|
+ msg.ttl-1))
|
|
|
else:
|
|
|
return super().received_msg(msg, peeraddr, channel)
|
|
|
|