refactor: _get_message() (singular) does unhexlify too

This commit is contained in:
Brian Warner 2015-07-24 16:33:29 -07:00
parent 5e1690cad8
commit a46a405487

View File

@ -172,12 +172,13 @@ class SymmetricWormhole:
url += "/" + msgnum url += "/" + msgnum
return url return url
def _get_messages(self, old_msgs, verb, msgnum): def _get_message(self, old_msgs, verb, msgnum):
# fire with a list of messages that match verb/msgnum, which either # fire with a bytestring of the first message that matches
# came from old_msgs, or from an EventSource that we attached to the # verb/msgnum, which either came from old_msgs, or from an
# corresponding URL # EventSource that we attached to the corresponding URL
if old_msgs: if old_msgs:
return defer.succeed(old_msgs) msg = unhexlify(old_msgs[0].encode("ascii"))
return defer.succeed(msg)
d = defer.Deferred() d = defer.Deferred()
msgs = [] msgs = []
def _handle(name, data): def _handle(name, data):
@ -192,7 +193,7 @@ class SymmetricWormhole:
es.activate() es.activate()
d.addCallback(lambda _: es.deactivate()) d.addCallback(lambda _: es.deactivate())
d.addCallback(lambda _: es.stopService()) d.addCallback(lambda _: es.stopService())
d.addCallback(lambda _: msgs) d.addCallback(lambda _: unhexlify(msgs[0].encode("ascii")))
return d return d
def derive_key(self, purpose, length=SecretBox.KEY_SIZE): def derive_key(self, purpose, length=SecretBox.KEY_SIZE):
@ -221,9 +222,8 @@ class SymmetricWormhole:
return defer.succeed(self.key) return defer.succeed(self.key)
data = {"message": hexlify(self.msg1).decode("ascii")} data = {"message": hexlify(self.msg1).decode("ascii")}
d = self._post_json(self._url("post", "pake"), data) d = self._post_json(self._url("post", "pake"), data)
d.addCallback(lambda j: self._get_messages(j["messages"], "poll", "pake")) d.addCallback(lambda j: self._get_message(j["messages"], "poll", "pake"))
def _got_pake(msgs): def _got_pake(pake_msg):
pake_msg = unhexlify(msgs[0].encode("ascii"))
key = self.sp.finish(pake_msg) key = self.sp.finish(pake_msg)
self.key = key self.key = key
self.verifier = self.derive_key(self.appid+b":Verifier") self.verifier = self.derive_key(self.appid+b":Verifier")
@ -252,9 +252,8 @@ class SymmetricWormhole:
outbound_encrypted = self._encrypt_data(data_key, outbound_data) outbound_encrypted = self._encrypt_data(data_key, outbound_data)
data = {"message": hexlify(outbound_encrypted).decode("ascii")} data = {"message": hexlify(outbound_encrypted).decode("ascii")}
d = self._post_json(self._url("post", "data"), data) d = self._post_json(self._url("post", "data"), data)
d.addCallback(lambda j: self._get_messages(j["messages"], "poll", "data")) d.addCallback(lambda j: self._get_message(j["messages"], "poll", "data"))
def _got_data(msgs): def _got_data(inbound_encrypted):
inbound_encrypted = unhexlify(msgs[0].encode("ascii"))
if inbound_encrypted == outbound_encrypted: if inbound_encrypted == outbound_encrypted:
raise ReflectionAttack raise ReflectionAttack
try: try: