def run_exchange(transport, code, app_id, side): # Send the SPAKE2 message spake = SPAKE2_Symmetric( util.to_bytes(code), idSymmetric=util.to_bytes(app_id)) outbound = spake.start() transport.send_json({ 'phase': 'pake', 'body': util.bytes_to_hexstr( util.dict_to_bytes({ 'pake_v1': util.bytes_to_hexstr(outbound), }) ), 'side': side, 'type': 'message', }) # Receive SPAKE2 message pake_msg = transport.receive_json() inbound = util.hexstr_to_bytes( util.bytes_to_dict( util.hexstr_to_bytes(pake_msg['body']) )['pake_v1'] ) spake_key = spake.finish(inbound) # Send the versions message version_phase = 'version' transport.send_json({ 'phase': version_phase, 'body': util.bytes_to_hexstr( encrypt_data( derive_phase_key(spake_key, side, version_phase), util.dict_to_bytes({'app_versions': {}}) ) ), 'side': side, 'type': 'message', }) # Receive the versions message versions = transport.receive_json() their_versions = util.bytes_to_dict( decrypt_data( derive_phase_key(spake_key, versions['side'], versions['phase']), util.hexstr_to_bytes( versions['body'] ), ), ) return their_versions
def run_exchange(transport, code, app_id, side): # Send the SPAKE2 message spake = SPAKE2_Symmetric(util.to_bytes(code), idSymmetric=util.to_bytes(app_id)) outbound = spake.start() transport.send_json({ 'phase': u'pake', 'body': util.bytes_to_hexstr( util.dict_to_bytes({ 'pake_v1': util.bytes_to_hexstr(outbound), })), 'side': side, 'type': 'message', }) # Receive SPAKE2 message pake_msg = transport.receive_json() inbound = util.hexstr_to_bytes( util.bytes_to_dict(util.hexstr_to_bytes(pake_msg['body']))['pake_v1']) spake_key = spake.finish(inbound) transport.send_line(util.bytes_to_hexstr(spake_key))
def test_await_json_works(upstream): """ The Wormhole.await_json method should resolve into a dict parsed from the upstream's return value. """ upstream.get_message = lambda: succeed(dict_to_bytes({'answer': 42})) wormhole = Wormhole() res = yield wormhole.await_json() assert res == {'answer': 42}
def test_send_file_error(upstream, file_path): """ The Wormhole.send_file method should close the wormhole and reject with an appropriate error if the other side sends an error message. """ upstream.get_message = lambda: succeed(dict_to_bytes({'error': '!'})) wormhole = Wormhole() with pytest.raises(SuspiciousOperation) as exc_info: yield wormhole.send_file(file_path) assert str(exc_info.value) == '!' assert upstream.close.called
def test_await_offer_error(upstream): """ The Wormhole.await_offer method should close the wormhole and reject with an appropriate error if the other side sends an error message. """ upstream.get_message = lambda: succeed(dict_to_bytes({'error': '!'})) upstream.derive_key = lambda *args: bytes('key', 'utf-8') wormhole = Wormhole() with pytest.raises(SuspiciousOperation) as exc_info: yield wormhole.await_offer() assert str(exc_info.value) == '!' assert upstream.close.called
def get_message(): deferred = Deferred() reactor.callLater(2, deferred.callback, dict_to_bytes({})) return deferred