magic-wormhole/src/wormhole/_receive.py

85 lines
2.8 KiB
Python
Raw Normal View History

2017-02-24 02:11:07 +00:00
from __future__ import print_function, absolute_import, unicode_literals
from zope.interface import implementer
2017-02-23 00:56:39 +00:00
from attr import attrs, attrib
from attr.validators import provides, instance_of
from automat import MethodicalMachine
from . import _interfaces
from ._key import derive_phase_key, decrypt_data, CryptoError
2017-02-23 00:56:39 +00:00
@attrs
@implementer(_interfaces.IReceive)
class Receive(object):
2017-02-23 00:56:39 +00:00
_side = attrib(validator=instance_of(type(u"")))
_timing = attrib(validator=provides(_interfaces.ITiming))
m = MethodicalMachine()
2017-02-23 00:56:39 +00:00
2017-02-24 02:11:07 +00:00
def __attrs_post_init__(self):
self._key = None
2017-02-23 00:56:39 +00:00
def wire(self, boss, key, send):
self._B = _interfaces.IBoss(boss)
self._K = _interfaces.IKey(key)
self._S = _interfaces.ISend(send)
@m.state(initial=True)
def S0_unknown_key(self): pass
@m.state()
def S1_unverified_key(self): pass
@m.state()
def S2_verified_key(self): pass
@m.state(terminal=True)
def S3_scared(self): pass
# from Ordering
2017-02-22 20:51:53 +00:00
def got_message(self, phase, body):
assert isinstance(phase, type("")), type(phase)
assert isinstance(body, type(b"")), type(body)
assert self._key
2017-02-25 02:30:00 +00:00
data_key = derive_phase_key(self._key, self._side, phase)
try:
plaintext = decrypt_data(data_key, body)
except CryptoError:
self.got_message_bad()
return
self.got_message_good(phase, plaintext)
@m.input()
def got_message_good(self, phase, plaintext): pass
@m.input()
def got_message_bad(self): pass
# from Key
@m.input()
def got_key(self, key): pass
@m.output()
def record_key(self, key):
self._key = key
@m.output()
def S_got_verified_key(self, phase, plaintext):
assert self._key
self._S.got_verified_key(self._key)
@m.output()
def W_happy(self, phase, plaintext):
self._B.happy()
@m.output()
def W_got_message(self, phase, plaintext):
2017-02-22 20:51:53 +00:00
assert isinstance(phase, type("")), type(phase)
assert isinstance(plaintext, type(b"")), type(plaintext)
self._B.got_message(phase, plaintext)
@m.output()
def W_scared(self):
self._B.scared()
S0_unknown_key.upon(got_key, enter=S1_unverified_key, outputs=[record_key])
S1_unverified_key.upon(got_message_good, enter=S2_verified_key,
outputs=[S_got_verified_key, W_happy, W_got_message])
S1_unverified_key.upon(got_message_bad, enter=S3_scared,
outputs=[W_scared])
S2_verified_key.upon(got_message_bad, enter=S3_scared,
outputs=[W_scared])
S2_verified_key.upon(got_message_good, enter=S2_verified_key,
outputs=[W_got_message])
S3_scared.upon(got_message_good, enter=S3_scared, outputs=[])
S3_scared.upon(got_message_bad, enter=S3_scared, outputs=[])