[tor-commits] [stem/master] Rearrange the digest/key copies to be a bit more spec-compliant

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


commit 13cc211070c2cf1d0d571461eed688ac1e69a735
Author: Dave Rolek <dmr-x at riseup.net>
Date:   Sat Aug 18 04:26:42 2018 +0000

    Rearrange the digest/key copies to be a bit more spec-compliant
    
    This arrangement is better, as it better keeps track of the digest/key
    on a cell-by-cell basis, but it's still not spec-compliant in a number
    of ways, such as its potential to drop cells or get corrupted backward
    digest/key state in the case of an exception.
    
    That's ok - this is just an interim implementation, and this change is
    just to make things a bit better for illustrative purposes.
---
 stem/client/__init__.py | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index 91e8c4ca..8c96b8c2 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -236,20 +236,27 @@ class Circuit(object):
     with self.relay._orport_lock:
       orig_forward_digest = self.forward_digest.copy()
       orig_forward_key = copy.copy(self.forward_key)
-      orig_backward_digest = self.backward_digest.copy()
-      orig_backward_key = copy.copy(self.backward_key)
 
       try:
         cell = stem.client.cell.RelayCell(self.id, command, data, stream_id = stream_id)
         encrypted_cell, self.forward_digest, self.forward_key = cell.encrypt(self.forward_digest, self.forward_key)
 
-        reply_cells = []
         self.relay._orport.send(encrypted_cell.pack(self.relay.link_protocol))
-        reply = self.relay._orport.recv()
+      except:
+        self.forward_digest = orig_forward_digest
+        self.forward_key = orig_forward_key
+        raise
+
+      reply = self.relay._orport.recv()
+      reply_cells = []
 
-        relay_cell_cmd = stem.client.cell.RelayCell.VALUE
+      relay_cell_cmd = stem.client.cell.RelayCell.VALUE
 
-        while reply:
+      while reply:
+        orig_backward_digest = self.backward_digest.copy()
+        orig_backward_key = copy.copy(self.backward_key)
+
+        try:
           raw_cell, reply = stem.client.cell.Cell.pop(reply, self.relay.link_protocol)
 
           if raw_cell.VALUE != relay_cell_cmd:
@@ -260,16 +267,14 @@ class Circuit(object):
           decrypted_cell, fully_decrypted, self.backward_digest, self.backward_key = raw_cell.decrypt(self.backward_digest, self.backward_key, interpret = True)
           if not fully_decrypted:
             raise stem.ProtocolError('Response for circuit id %i was not fully decrypted, when expected to be' % self.id)
+        except:
+          self.backward_digest = orig_backward_digest
+          self.backward_key = orig_backward_key
+          raise
 
-          reply_cells.append(decrypted_cell)
+        reply_cells.append(decrypted_cell)
 
-        return reply_cells
-      except:
-        self.forward_digest = orig_forward_digest
-        self.forward_key = orig_forward_key
-        self.backward_digest = orig_backward_digest
-        self.backward_key = orig_backward_key
-        raise
+      return reply_cells
 
   def close(self):
     with self.relay._orport_lock:





More information about the tor-commits mailing list