[tor-commits] [stem/master] Refactor 'unused' and padding packing into the RELAY cell payload

atagar at torproject.org atagar at torproject.org
Sun Aug 26 20:49:21 UTC 2018


commit 09c171d60c2b1414aae791d2347b6385caca03de
Author: Dave Rolek <dmr-x at riseup.net>
Date:   Thu Aug 9 20:28:15 2018 +0000

    Refactor 'unused' and padding packing into the RELAY cell payload
    
    This is more appropriate since the spec allows for (and recommends!)
    different behavior for these bytes than other padding bytes.
    
    However, presently the handling is identical.
    
    It is also important that the _pack_payload() method be able to produce
    a full-size payload, for simplifying encryption.
---
 stem/client/cell.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index 7c09d0e0..42b95571 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -424,9 +424,9 @@ class RelayCell(CircuitCell):
     return digest
 
   def pack(self, link_protocol):
-    payload = RelayCell._pack_payload(self.command_int, self.recognized, self.stream_id, self.digest, len(self.data), self.data)
+    payload = RelayCell._pack_payload(self.command_int, self.recognized, self.stream_id, self.digest, len(self.data), self.data, self.unused)
 
-    return RelayCell._pack(link_protocol, payload, self.unused, self.circ_id)
+    return RelayCell._pack(link_protocol, payload, unused = b'', circ_id = self.circ_id)
 
   @classmethod
   def _unpack(cls, content, circ_id, link_protocol):
@@ -457,7 +457,7 @@ class RelayCell(CircuitCell):
     return command, recognized, stream_id, digest, data_len, data, unused
 
   @staticmethod
-  def _pack_payload(command_int, recognized, stream_id, digest, data_len, data):
+  def _pack_payload(command_int, recognized, stream_id, digest, data_len, data, unused = b'', pad_remainder = True):
     """
     Directly pack the payload without any validation beyond Size constraints.
 
@@ -467,6 +467,8 @@ class RelayCell(CircuitCell):
     :param HASH,str,int digest: running digest held with the relay
     :param int data_len: length of body data
     :param bytes data: body data of the cell
+    :param bytes unused: padding bytes to include after data
+    :param bool pad_remaining: pads up to payload size if **True**
 
     :returns: **bytes** with the packed payload
     """
@@ -478,6 +480,16 @@ class RelayCell(CircuitCell):
     payload += Size.LONG.pack(RelayCell._coerce_digest(digest))
     payload += Size.SHORT.pack(data_len)
     payload += data
+    payload += unused
+
+    if len(payload) > FIXED_PAYLOAD_LEN:
+      raise ValueError('Payload is too large (%i bytes), must not be more than %i.' % (len(payload), FIXED_PAYLOAD_LEN))
+
+    if pad_remainder:
+      # right now, it is acceptable to pad the remaining portion with ZEROs instead of random
+      # this is done due to threat model and simplifying some implementation
+      # however: in the future (TODO), this may become against the spec; see prop 289
+      payload += ZERO * (FIXED_PAYLOAD_LEN - len(payload))
 
     return bytes(payload)
 





More information about the tor-commits mailing list