relay.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. #!/usr/bin/env python3
  2. import random # For simulation, not cryptography!
  3. import math
  4. import nacl.utils
  5. import nacl.signing
  6. import nacl.public
  7. import nacl.hash
  8. import network
  9. import dirauth
  10. class RelayNetMsg(network.NetMsg):
  11. """The subclass of NetMsg for messages between relays and either
  12. relays or clients."""
  13. class RelayGetConsensusMsg(RelayNetMsg):
  14. """The subclass of RelayNetMsg for fetching the consensus."""
  15. class RelayConsensusMsg(RelayNetMsg):
  16. """The subclass of RelayNetMsg for returning the consensus."""
  17. def __init__(self, consensus):
  18. self.consensus = consensus
  19. class RelayRandomHopMsg(RelayNetMsg):
  20. """A message used for testing, that hops from relay to relay
  21. randomly until its TTL expires."""
  22. def __init__(self, ttl):
  23. self.ttl = ttl
  24. def __str__(self):
  25. return "RandomHop TTL=%d" % self.ttl
  26. class CircuitCellMsg(RelayNetMsg):
  27. """Send a message tagged with a circuit id."""
  28. def __init__(self, circuitid, cell):
  29. self.circid = circuitid
  30. self.cell = cell
  31. def __str__(self):
  32. return "C%d:%s" % (self.circid, self.cell)
  33. def size(self):
  34. # circuitids are 4 bytes
  35. return 4 + self.cell.size()
  36. class RelayCell(RelayNetMsg):
  37. """All cells (which are sent inside a CircuitCellMsg, and so do not
  38. need their own circuitid) should be a subclass of this class."""
  39. class StringCell(RelayCell):
  40. """Send an arbitrary string as a cell."""
  41. def __init__(self, str):
  42. self.data = str
  43. def __str__(self):
  44. return self.data.__str__()
  45. class CloseCell(RelayCell):
  46. """Close the circuit this cell was sent on. It should be sent
  47. _unencrypted_ (not within an EncryptedCell), and relays that receive
  48. one should forward it along the adjacent circuit, then close both
  49. the circuit it was received on and the adjacent one."""
  50. # It is intentional that VanillaCreateCircuitMsg is a RelayNetMsg and
  51. # not a RelayCell. This is the message that _creates_ the circuit, so
  52. # it can't be sent as a cell _within_ the circuit.
  53. class VanillaCreateCircuitMsg(RelayNetMsg):
  54. """The message for requesting circuit creation in Vanilla Onion
  55. Routing."""
  56. def __init__(self, circid, ntor_request):
  57. self.circid = circid
  58. self.ntor_request = ntor_request
  59. class VanillaCreatedCircuitCell(RelayCell):
  60. """The message for responding to circuit creation in Vanilla Onion
  61. Routing."""
  62. def __init__(self, ntor_reply):
  63. self.ntor_reply = ntor_reply
  64. class VanillaExtendCircuitCell(RelayCell):
  65. """The message for requesting circuit creation in Vanilla Onion
  66. Routing."""
  67. def __init__(self, hopaddr, ntor_request):
  68. self.hopaddr = hopaddr
  69. self.ntor_request = ntor_request
  70. class VanillaExtendedCircuitCell(RelayCell):
  71. """The message for responding to circuit creation in Vanilla Onion
  72. Routing."""
  73. def __init__(self, ntor_reply):
  74. self.ntor_reply = ntor_reply
  75. class EncryptedCell(RelayCell):
  76. """Send a message encrypted with a symmetric key. In this
  77. implementation, the encryption is not really done. A hash of the
  78. key is stored with the message so that it can be checked at
  79. decryption time."""
  80. def __init__(self, key, msg):
  81. self.keyhash = nacl.hash.sha256(key)
  82. self.plaintext = msg
  83. def decrypt(self, key):
  84. keyhash = nacl.hash.sha256(key)
  85. if keyhash != self.keyhash:
  86. raise ValueError("EncryptedCell key mismatch")
  87. return self.plaintext
  88. def size(self):
  89. # Current Tor actually has no overhead for encryption
  90. return self.plaintext.size()
  91. class RelayFallbackTerminationError(Exception):
  92. """An exception raised when someone tries to terminate a fallback
  93. relay."""
  94. class NTor:
  95. """A class implementing the ntor one-way authenticated key agreement
  96. scheme. The details are not exactly the same as either the ntor
  97. paper or Tor's implementation, but it will agree on keys and have
  98. the same number of public key operations."""
  99. def __init__(self, perfstats):
  100. self.perfstats = perfstats
  101. def request(self):
  102. """Create the ntor request message: X = g^x."""
  103. self.client_ephem_key = nacl.public.PrivateKey.generate()
  104. self.perfstats.keygens += 1
  105. return self.client_ephem_key.public_key
  106. @staticmethod
  107. def reply(onion_privkey, idpubkey, client_pubkey, perfstats):
  108. """The server calls this static method to produce the ntor reply
  109. message: (Y = g^y, B = g^b, A = H(M, "verify")) and the shared
  110. secret S = H(M, "secret") for M = (X^y,X^b,ID,B,X,Y)."""
  111. server_ephem_key = nacl.public.PrivateKey.generate()
  112. perfstats.keygens += 1
  113. xykey = nacl.public.Box(server_ephem_key, client_pubkey).shared_key()
  114. xbkey = nacl.public.Box(onion_privkey, client_pubkey).shared_key()
  115. perfstats.dhs += 2
  116. M = xykey + xbkey + \
  117. idpubkey.encode(encoder=nacl.encoding.RawEncoder) + \
  118. onion_privkey.public_key.encode(encoder=nacl.encoding.RawEncoder) + \
  119. server_ephem_key.public_key.encode(encoder=nacl.encoding.RawEncoder)
  120. A = nacl.hash.sha256(M + b'verify', encoder=nacl.encoding.RawEncoder)
  121. S = nacl.hash.sha256(M + b'secret', encoder=nacl.encoding.RawEncoder)
  122. return ((server_ephem_key.public_key, onion_privkey.public_key, A), S)
  123. def verify(self, reply, onion_pubkey, idpubkey):
  124. """The client calls this method to verify the ntor reply
  125. message, passing the onion and id public keys for the server
  126. it's expecting to be talking to . Returns the shared secret on
  127. success, or raises ValueError on failure."""
  128. server_ephem_pubkey, server_onion_pubkey, authtag = reply
  129. if onion_pubkey != server_onion_pubkey:
  130. raise ValueError("NTor onion pubkey mismatch")
  131. xykey = nacl.public.Box(self.client_ephem_key, server_ephem_pubkey).shared_key()
  132. xbkey = nacl.public.Box(self.client_ephem_key, onion_pubkey).shared_key()
  133. self.perfstats.dhs += 2
  134. M = xykey + xbkey + \
  135. idpubkey.encode(encoder=nacl.encoding.RawEncoder) + \
  136. onion_pubkey.encode(encoder=nacl.encoding.RawEncoder) + \
  137. server_ephem_pubkey.encode(encoder=nacl.encoding.RawEncoder)
  138. Acheck = nacl.hash.sha256(M + b'verify', encoder=nacl.encoding.RawEncoder)
  139. S = nacl.hash.sha256(M + b'secret', encoder=nacl.encoding.RawEncoder)
  140. if Acheck != authtag:
  141. raise ValueError("NTor auth mismatch")
  142. return S
  143. class VanillaExtendCircuitHandler:
  144. """A handler for VanillaExtendCircuitCell cells. It allocates a new
  145. circuit id on the Channel to the requested next hop, connects the
  146. existing and new circuits together, and forwards a
  147. VanillaCreateCircuitMsg to the next hop."""
  148. def received_cell(self, circhandler, cell):
  149. # Remove ourselves from handling a second
  150. # VanillaExtendCircuitCell on this circuit
  151. circhandler.replace_celltype_handler(VanillaExtendCircuitCell, None)
  152. # Allocate a new circuit id to the requested next hop
  153. channelmgr = circhandler.channel.channelmgr
  154. nexthopchannel = channelmgr.get_channel_to(cell.hopaddr)
  155. newcircid, newcirchandler = nexthopchannel.new_circuit()
  156. # Connect the existing and new circuits together
  157. circhandler.adjacent_circuit_handler = newcirchandler
  158. newcirchandler.adjacent_circuit_handler = circhandler
  159. # Set up a handler for when the VanillaCreatedCircuitCell comes
  160. # back
  161. newcirchandler.replace_celltype_handler(
  162. VanillaCreatedCircuitCell,
  163. VanillaCreatedRelayHandler())
  164. # Forward a VanillaCreateCircuitMsg to the next hop
  165. nexthopchannel.send_msg(
  166. VanillaCreateCircuitMsg(newcircid, cell.ntor_request))
  167. class VanillaCreatedRelayHandler:
  168. """Handle a VanillaCreatedCircuitCell received by a _relay_ that
  169. recently received a VanillaExtendCircuitCell from a client, and so
  170. forwarded a VanillaCreateCircuitCell to the next hop."""
  171. def received_cell(self, circhandler, cell):
  172. # Remove ourselves from handling a second
  173. # VanillaCreatedCircuitCell on this circuit
  174. circhandler.replace_celltype_handler(VanillaCreatedCircuitCell, None)
  175. # Just forward a VanillaExtendedCircuitCell back towards the
  176. # client
  177. circhandler.adjacent_circuit_handler.send_cell(
  178. VanillaExtendedCircuitCell(cell.ntor_reply))
  179. class CircuitHandler:
  180. """A class for managing sending and receiving encrypted cells on a
  181. particular circuit."""
  182. class NoCryptLayer:
  183. def encrypt_msg(self, msg): return msg
  184. def decrypt_msg(self, msg): return msg
  185. class CryptLayer:
  186. def __init__(self, enckey, deckey, next_layer):
  187. self.enckey = enckey
  188. self.deckey = deckey
  189. self.next_layer = next_layer
  190. def encrypt_msg(self, msg):
  191. return self.next_layer.encrypt_msg(EncryptedCell(self.enckey, msg))
  192. def decrypt_msg(self, msg):
  193. return self.next_layer.decrypt_msg(msg).decrypt(self.deckey)
  194. def __init__(self, channel, circid):
  195. self.channel = channel
  196. self.circid = circid
  197. # The list of relay descriptors that form the circuit so far
  198. # (client side only)
  199. self.circuit_descs = []
  200. # The dispatch table is indexed by type, and the values are
  201. # objects with received_cell(circhandler, cell) methods.
  202. self.cell_dispatch_table = dict()
  203. # The topmost crypt layer. This is an object with
  204. # encrypt_msg(msg) and decrypt_msg(msg) methods that returns the
  205. # en/decrypted messages respectively. Messages are encrypted
  206. # starting with the last layer that was added (the keys for the
  207. # furthest away relay in the circuit) and are decrypted starting
  208. # with the first layer that was added (the keys for the guard).
  209. self.crypt_layer = self.NoCryptLayer()
  210. # The adjacent CircuitHandler that's connected to this one. If
  211. # we get a cell on one, we forward it to the other (if it's not
  212. # meant for us to handle directly)
  213. self.adjacent_circuit_handler = None
  214. # The function to call when this circuit closes
  215. self.closer = lambda: self.channel.circuithandlers.pop(circid)
  216. def close(self):
  217. """Close the circuit. Sends a CloseCell on the circuit (and its
  218. adjacent circuit, if present) and closes both."""
  219. adjcirchandler = self.adjacent_circuit_handler
  220. self.adjacent_circuit_handler = None
  221. if adjcirchandler is not None:
  222. adjcirchandler.adjacent_circuit_handler = None
  223. self.closer()
  224. self.channel_send_cell(CloseCell())
  225. if adjcirchandler is not None:
  226. adjcirchandler.closer()
  227. adjcirchandler.channel_send_cell(CloseCell())
  228. def send_cell(self, cell):
  229. """Send a cell on this circuit, encrypting as needed."""
  230. self.channel_send_cell(self.crypt_layer.encrypt_msg(cell))
  231. def channel_send_cell(self, cell):
  232. """Send a cell on this circuit directly without encrypting it
  233. first."""
  234. self.channel.send_msg(CircuitCellMsg(self.circid, cell))
  235. def received_cell(self, cell):
  236. """A cell has been received on this circuit. Dispatch it
  237. according to its type."""
  238. if isinstance(cell, EncryptedCell):
  239. cell = self.crypt_layer.decrypt_msg(cell)
  240. print("CircuitHandler: %s received cell %s on circuit %d from %s" % (self.channel.channelmgr.myaddr, cell, self.circid, self.channel.peer.channelmgr.myaddr))
  241. # If it's still encrypted, it's for sure meant for forwarding to
  242. # our adjacent hop, which had better exist.
  243. if isinstance(cell, EncryptedCell):
  244. self.adjacent_circuit_handler.send_cell(cell)
  245. else:
  246. # This is a plaintext cell meant for us. Handle it
  247. # according to the table.
  248. celltype = type(cell)
  249. if celltype in self.cell_dispatch_table:
  250. self.cell_dispatch_table[celltype].received_cell(self, cell)
  251. elif isinstance(cell, StringCell):
  252. # Default handler; just print the message in the cell
  253. print("CircuitHandler: %s received '%s' on circuit %d from %s" \
  254. % (self.channel.channelmgr.myaddr, cell,
  255. self.circid, self.channel.peer.channelmgr.myaddr))
  256. elif isinstance(cell, CloseCell):
  257. # Forward the CloseCell (without encryption) to the
  258. # adjacent circuit, if any, and close both this and the
  259. # adjacent circuit
  260. adjcirchandler = self.adjacent_circuit_handler
  261. self.adjacent_circuit_handler = None
  262. if adjcirchandler is not None:
  263. adjcirchandler.adjacent_circuit_handler = None
  264. self.closer()
  265. if adjcirchandler is not None:
  266. adjcirchandler.closer()
  267. adjcirchandler.channel_send_cell(cell)
  268. else:
  269. # I don't know how to handle this cell?
  270. raise ValueError("CircuitHandler: %s received unknown cell type %s on circuit %d from %s" \
  271. % (self.channel.channelmgr.myaddr, cell,
  272. self.circid, self.channel.peer.channelmgr.myaddr))
  273. def replace_celltype_handler(self, celltype, handler):
  274. """Add an object with a received_cell(circhandler, cell) method
  275. to the cell dispatch table. It replaces anything that's already
  276. there. Passing None as the handler removes the dispatcher for
  277. that cell type."""
  278. if handler is None:
  279. del self.cell_dispatch_table[celltype]
  280. else:
  281. self.cell_dispatch_table[celltype] = handler
  282. def add_crypt_layer(self, enckey, deckey):
  283. """Add a processing layer to this CircuitHandler so that cells
  284. we send will get encrypted with the first given key, and cells
  285. we receive will be decrypted with the other given key."""
  286. current_crypt_layer = self.crypt_layer
  287. self.crypt_layer = self.CryptLayer(enckey, deckey, current_crypt_layer)
  288. class Channel(network.Connection):
  289. """A class representing a channel between a relay and either a
  290. client or a relay, transporting cells from various circuits."""
  291. def __init__(self):
  292. super().__init__()
  293. # The RelayChannelManager managing this Channel
  294. self.channelmgr = None
  295. # The Channel at the other end
  296. self.peer = None
  297. # The function to call when the connection closes
  298. self.closer = lambda: None
  299. # The next circuit id to use on this channel. The party that
  300. # opened the channel uses even numbers; the receiving party uses
  301. # odd numbers.
  302. self.next_circid = None
  303. # A map for CircuitHandlers to use for each open circuit on the
  304. # channel
  305. self.circuithandlers = dict()
  306. def closed(self):
  307. # Close each circuithandler we're managing
  308. while self.circuithandlers:
  309. chitems = iter(self.circuithandlers.items())
  310. circid, circhandler = next(chitems)
  311. print('closing circuit', circid)
  312. circhandler.close()
  313. self.closer()
  314. self.peer = None
  315. def close(self):
  316. peer = self.peer
  317. self.closed()
  318. if peer is not None and peer is not self:
  319. peer.closed()
  320. def new_circuit(self):
  321. """Allocate a new circuit on this channel, returning the new
  322. circuit's id and the new CircuitHandler."""
  323. circid = self.next_circid
  324. self.next_circid += 2
  325. circuithandler = CircuitHandler(self, circid)
  326. self.circuithandlers[circid] = circuithandler
  327. return circid, circuithandler
  328. def new_circuit_with_circid(self, circid):
  329. """Allocate a new circuit on this channel, with the circuit id
  330. received from our peer. Return the new CircuitHandler"""
  331. circuithandler = CircuitHandler(self, circid)
  332. self.circuithandlers[circid] = circuithandler
  333. return circuithandler
  334. def send_cell(self, circid, cell):
  335. """Send the given message on the given circuit, encrypting or
  336. decrypting as needed."""
  337. self.circuithandlers[circid].send_cell(cell)
  338. def send_raw_cell(self, circid, cell):
  339. """Send the given message, tagged for the given circuit id. No
  340. encryption or decryption is done."""
  341. self.send_msg(CircuitCellMsg(self.circid, self.cell))
  342. def send_msg(self, msg):
  343. """Send the given NetMsg on the channel."""
  344. self.channelmgr.perfstats.bytes_sent += msg.size()
  345. self.peer.received(self.channelmgr.myaddr, msg)
  346. def received(self, peeraddr, msg):
  347. """Callback when a message is received from the network."""
  348. print('Channel: %s received %s from %s' % (self.channelmgr.myaddr, msg, peeraddr))
  349. self.channelmgr.perfstats.bytes_received += msg.size()
  350. if isinstance(msg, CircuitCellMsg):
  351. circid, cell = msg.circid, msg.cell
  352. self.circuithandlers[circid].received_cell(cell)
  353. else:
  354. self.channelmgr.received_msg(msg, peeraddr, self)
  355. class ChannelManager:
  356. """The class that manages the channels to other relays and clients.
  357. Relays and clients both use subclasses of this class to both create
  358. on-demand channels to relays, to gracefully handle the closing of
  359. channels, and to handle commands received over the channels."""
  360. def __init__(self, myaddr, dirauthaddrs, perfstats):
  361. # A dictionary of Channels to other hosts, indexed by NetAddr
  362. self.channels = dict()
  363. self.myaddr = myaddr
  364. self.dirauthaddrs = dirauthaddrs
  365. self.consensus = None
  366. self.perfstats = perfstats
  367. def terminate(self):
  368. """Close all connections we're managing."""
  369. while self.channels:
  370. channelitems = iter(self.channels.items())
  371. addr, channel = next(channelitems)
  372. print('closing channel', addr, channel)
  373. channel.close()
  374. def add_channel(self, channel, peeraddr):
  375. """Add the given channel to the list of channels we are
  376. managing. If we are already managing a channel to the same
  377. peer, close it first."""
  378. if peeraddr in self.channels:
  379. self.channels[peeraddr].close()
  380. channel.channelmgr = self
  381. self.channels[peeraddr] = channel
  382. channel.closer = lambda: self.channels.pop(peeraddr)
  383. def get_channel_to(self, addr):
  384. """Get the Channel connected to the given NetAddr, creating one
  385. if none exists right now."""
  386. if addr in self.channels:
  387. return self.channels[addr]
  388. # Create the new channel
  389. newchannel = network.thenetwork.connect(self.myaddr, addr,
  390. self.perfstats)
  391. self.channels[addr] = newchannel
  392. newchannel.closer = lambda: self.channels.pop(addr)
  393. newchannel.channelmgr = self
  394. return newchannel
  395. def received_msg(self, msg, peeraddr, channel):
  396. """Callback when a NetMsg not specific to a circuit is
  397. received."""
  398. print("ChannelManager: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  399. def received_cell(self, circid, cell, peeraddr, channel):
  400. """Callback with a circuit-specific cell is received."""
  401. print("ChannelManager: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  402. def send_msg(self, msg, peeraddr):
  403. """Send a message to the peer with the given address."""
  404. channel = self.get_channel_to(peeraddr)
  405. channel.send_msg(msg)
  406. def send_cell(self, circid, cell, peeraddr):
  407. """Send a cell on the given circuit to the peer with the given
  408. address."""
  409. channel = self.get_channel_to(peeraddr)
  410. channel.send_cell(circid, cell)
  411. class RelayChannelManager(ChannelManager):
  412. """The subclass of ChannelManager for relays."""
  413. def __init__(self, myaddr, dirauthaddrs, onionprivkey, idpubkey, perfstats):
  414. super().__init__(myaddr, dirauthaddrs, perfstats)
  415. self.onionkey = onionprivkey
  416. self.idpubkey = idpubkey
  417. def get_consensus(self):
  418. """Download a fresh consensus from a random dirauth."""
  419. a = random.choice(self.dirauthaddrs)
  420. c = network.thenetwork.connect(self, a, self.perfstats)
  421. self.consensus = c.getconsensus()
  422. dirauth.Consensus.verify(self.consensus,
  423. network.thenetwork.dirauthkeys(), self.perfstats)
  424. c.close()
  425. def received_msg(self, msg, peeraddr, channel):
  426. """Callback when a NetMsg not specific to a circuit is
  427. received."""
  428. print("RelayChannelManager: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  429. if isinstance(msg, RelayRandomHopMsg):
  430. if msg.ttl > 0:
  431. # Pick a random next hop from the consensus
  432. nexthop = random.choice(self.consensus.consdict['relays'])
  433. nextaddr = nexthop.descdict['addr']
  434. self.send_msg(RelayRandomHopMsg(msg.ttl-1), nextaddr)
  435. elif isinstance(msg, RelayGetConsensusMsg):
  436. self.send_msg(RelayConsensusMsg(self.consensus), peeraddr)
  437. elif isinstance(msg, VanillaCreateCircuitMsg):
  438. # A new circuit has arrived
  439. circhandler = channel.new_circuit_with_circid(msg.circid)
  440. # Create the ntor reply
  441. reply, secret = NTor.reply(self.onionkey, self.idpubkey,
  442. msg.ntor_request, self.perfstats)
  443. # Set up the circuit to use the shared secret
  444. enckey = nacl.hash.sha256(secret + b'downstream')
  445. deckey = nacl.hash.sha256(secret + b'upstream')
  446. circhandler.add_crypt_layer(enckey, deckey)
  447. # Add a handler for if an Extend Cell arrives (there should
  448. # be at most one on this circuit).
  449. circhandler.replace_celltype_handler(
  450. VanillaExtendCircuitCell, VanillaExtendCircuitHandler())
  451. # Send the ntor reply
  452. self.send_msg(CircuitCellMsg(msg.circid,
  453. VanillaCreatedCircuitCell(reply)), peeraddr)
  454. else:
  455. return super().received_msg(msg, peeraddr, channel)
  456. def received_cell(self, circid, cell, peeraddr, channel):
  457. """Callback with a circuit-specific cell is received."""
  458. print("RelayChannelManager: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  459. return super().received_cell(circid, cell, peeraddr, channel)
  460. class Relay(network.Server):
  461. """The class representing an onion relay."""
  462. def __init__(self, dirauthaddrs, bw, flags):
  463. # Gather performance statistics
  464. self.perfstats = network.PerfStats(network.EntType.RELAY)
  465. self.perfstats.is_bootstrapping = True
  466. # Create the identity and onion keys
  467. self.idkey = nacl.signing.SigningKey.generate()
  468. self.onionkey = nacl.public.PrivateKey.generate()
  469. self.perfstats.keygens += 2
  470. self.name = self.idkey.verify_key.encode(encoder=nacl.encoding.HexEncoder).decode("ascii")
  471. # Bind to the network to get a network address
  472. self.netaddr = network.thenetwork.bind(self)
  473. self.perfstats.name = "Relay at %s" % self.netaddr
  474. # Our bandwidth and flags
  475. self.bw = bw
  476. self.flags = flags
  477. # Register for epoch change notification
  478. network.thenetwork.wantepochticks(self, True, end=True)
  479. network.thenetwork.wantepochticks(self, True)
  480. # Create the RelayChannelManager connection manager
  481. self.channelmgr = RelayChannelManager(self.netaddr, dirauthaddrs,
  482. self.onionkey, self.idkey.verify_key, self.perfstats)
  483. # Initially, we're not a fallback relay
  484. self.is_fallbackrelay = False
  485. self.uploaddesc()
  486. def terminate(self):
  487. """Stop this relay."""
  488. if self.is_fallbackrelay:
  489. # Fallback relays must not (for now) terminate
  490. raise RelayFallbackTerminationError(self)
  491. # Stop listening for epoch ticks
  492. network.thenetwork.wantepochticks(self, False, end=True)
  493. network.thenetwork.wantepochticks(self, False)
  494. # Tell the dirauths we're going away
  495. self.uploaddesc(False)
  496. # Close connections to other relays
  497. self.channelmgr.terminate()
  498. # Stop listening to our own bound port
  499. self.close()
  500. def set_is_fallbackrelay(self, isfallback = True):
  501. """Set this relay to be a fallback relay (or unset if passed
  502. False)."""
  503. self.is_fallbackrelay = isfallback
  504. def epoch_ending(self, epoch):
  505. # Download the new consensus, which will have been created
  506. # already since the dirauths' epoch_ending callbacks happened
  507. # before the relays'.
  508. self.channelmgr.get_consensus()
  509. def newepoch(self, epoch):
  510. self.uploaddesc()
  511. def uploaddesc(self, upload=True):
  512. # Upload the descriptor for the epoch to come, or delete a
  513. # previous upload if upload=False
  514. descdict = dict();
  515. descdict["epoch"] = network.thenetwork.getepoch() + 1
  516. descdict["idkey"] = self.idkey.verify_key
  517. descdict["onionkey"] = self.onionkey.public_key
  518. descdict["addr"] = self.netaddr
  519. descdict["bw"] = self.bw
  520. descdict["flags"] = self.flags
  521. desc = dirauth.RelayDescriptor(descdict)
  522. desc.sign(self.idkey, self.perfstats)
  523. dirauth.RelayDescriptor.verify(desc, self.perfstats)
  524. if upload:
  525. descmsg = dirauth.DirAuthUploadDescMsg(desc)
  526. else:
  527. # Note that this relies on signatures being deterministic;
  528. # otherwise we'd need to save the descriptor we uploaded
  529. # before so we could tell the airauths to delete the exact
  530. # one
  531. descmsg = dirauth.DirAuthDelDescMsg(desc)
  532. # Upload them
  533. for a in self.channelmgr.dirauthaddrs:
  534. c = network.thenetwork.connect(self, a, self.perfstats)
  535. c.sendmsg(descmsg)
  536. c.close()
  537. def connected(self, peer):
  538. """Callback invoked when someone (client or relay) connects to
  539. us. Create a pair of linked Channels and return the peer half
  540. to the peer."""
  541. # Create the linked pair
  542. if peer is self.netaddr:
  543. # A self-loop? We'll allow it.
  544. peerchannel = Channel()
  545. peerchannel.peer = peerchannel
  546. peerchannel.next_circid = 2
  547. return peerchannel
  548. peerchannel = Channel()
  549. ourchannel = Channel()
  550. peerchannel.peer = ourchannel
  551. peerchannel.next_circid = 2
  552. ourchannel.peer = peerchannel
  553. ourchannel.next_circid = 1
  554. # Add our channel to the RelayChannelManager
  555. self.channelmgr.add_channel(ourchannel, peer)
  556. return peerchannel
  557. if __name__ == '__main__':
  558. perfstats = network.PerfStats(network.EntType.NONE)
  559. # Start some dirauths
  560. numdirauths = 9
  561. dirauthaddrs = []
  562. for i in range(numdirauths):
  563. dira = dirauth.DirAuth(i, numdirauths)
  564. dirauthaddrs.append(dira.netaddr)
  565. # Start some relays
  566. numrelays = 10
  567. relays = []
  568. for i in range(numrelays):
  569. # Relay bandwidths (at least the ones fast enough to get used)
  570. # in the live Tor network (as of Dec 2019) are well approximated
  571. # by (200000-(200000-25000)/3*log10(x)) where x is a
  572. # uniform integer in [1,2500]
  573. x = random.randint(1,2500)
  574. bw = int(200000-(200000-25000)/3*math.log10(x))
  575. relays.append(Relay(dirauthaddrs, bw, 0))
  576. # The fallback relays are a hardcoded list of about 5% of the
  577. # relays, used by clients for bootstrapping
  578. numfallbackrelays = int(numrelays * 0.05) + 1
  579. fallbackrelays = random.sample(relays, numfallbackrelays)
  580. for r in fallbackrelays:
  581. r.set_is_fallbackrelay()
  582. network.thenetwork.setfallbackrelays(fallbackrelays)
  583. # Tick the epoch
  584. network.thenetwork.nextepoch()
  585. dirauth.Consensus.verify(dirauth.DirAuth.consensus,
  586. network.thenetwork.dirauthkeys(), perfstats)
  587. print('ticked; epoch=', network.thenetwork.getepoch())
  588. relays[3].channelmgr.send_msg(RelayRandomHopMsg(30), relays[5].netaddr)
  589. # See what channels exist and do a consistency check
  590. for r in relays:
  591. print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
  592. raddr = r.netaddr
  593. for ad, ch in r.channelmgr.channels.items():
  594. if ch.peer.channelmgr.myaddr != ad:
  595. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  596. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  597. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  598. # Stop some relays
  599. relays[3].terminate()
  600. del relays[3]
  601. relays[5].terminate()
  602. del relays[5]
  603. relays[7].terminate()
  604. del relays[7]
  605. # Tick the epoch
  606. network.thenetwork.nextepoch()
  607. print(dirauth.DirAuth.consensus)
  608. # See what channels exist and do a consistency check
  609. for r in relays:
  610. print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
  611. raddr = r.netaddr
  612. for ad, ch in r.channelmgr.channels.items():
  613. if ch.peer.channelmgr.myaddr != ad:
  614. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  615. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  616. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  617. channel = relays[3].channelmgr.get_channel_to(relays[5].netaddr)
  618. circid, circhandler = channel.new_circuit()
  619. peerchannel = relays[5].channelmgr.get_channel_to(relays[3].netaddr)
  620. peerchannel.new_circuit_with_circid(circid)
  621. relays[3].channelmgr.send_cell(circid, StringCell("test"), relays[5].netaddr)
  622. idpubkey = dirauth.DirAuth.consensus.consdict["relays"][1].descdict["idkey"]
  623. onionpubkey = dirauth.DirAuth.consensus.consdict["relays"][1].descdict["onionkey"]
  624. nt = NTor(perfstats)
  625. req = nt.request()
  626. R, S = NTor.reply(relays[1].onionkey, idpubkey, req, perfstats)
  627. S2 = nt.verify(R, onionpubkey, idpubkey)
  628. print(S == S2)