Skip to content

Commit

Permalink
Skeleton code for MFi setup (feature bit 51).
Browse files Browse the repository at this point in the history
  • Loading branch information
systemcrash committed Jan 4, 2022
1 parent ead0377 commit bd3ce4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ap2-receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,9 @@ def handle_pair_SV(self, op):
self.end_headers()
self.wfile.write(res)

if self.server.hap.encrypted:
if self.server.hap.encrypted and self.server.hap.mfi_setup:
SCR_LOG.warning('MFi setup not yet possible. Disable feature bit 51.')
elif self.server.hap.encrypted:
hexdump(self.server.hap.accessory_shared_key) if DEBUG else ''
self.upgrade_to_encrypted(self.server.hap.accessory_shared_key)

Expand Down
20 changes: 20 additions & 0 deletions ap2/pairing/hap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
ACCESSORY_SECRET = "accessory-secret"


class MFiUnhandledException(Exception):
pass


class PairingMethod:
PAIR_SETUP = b'\x00'
PAIR_SETUP_AUTH = b'\x01'
Expand Down Expand Up @@ -282,6 +286,7 @@ def __init__(self, identifier, isDebug=False):
self.isDebug = isDebug
self.transient = False
self.encrypted = False
self.mfi_setup = False
self.pair_setup_steps_n = 5
if self.isDebug:
self.logger = get_screen_logger('HAP', level='DEBUG')
Expand Down Expand Up @@ -398,6 +403,16 @@ def pair_setup(self, req):
PairingFlags(flags) == PairingFlags.TRANSIENT:
self.transient = True
self.pair_setup_steps_n = 2
elif req[Tlv8.Tag.STATE] == PairingState.M1 and \
req[Tlv8.Tag.METHOD] == PairingMethod.PAIR_SETUP_AUTH and \
Tlv8.Tag.FLAGS in req and \
PairingFlags(flags) == PairingFlags.TRANSIENT:
"""MFi setup - bitflag 51 was enabled
result will be wrong, but we can safely set these params.
"""
self.pair_setup_steps_n = 2
self.transient = True
self.mfi_setup = True

if req[Tlv8.Tag.STATE] == PairingState.M1:
self.logger.debug(f"-----\tPair-Setup [1/{self.pair_setup_steps_n}]")
Expand All @@ -407,6 +422,11 @@ def pair_setup(self, req):
res = self.pair_setup_m3_m4(req[Tlv8.Tag.PUBLICKEY], req[Tlv8.Tag.PROOF])
if self.transient:
self.encrypted = True
if self.mfi_setup:
try:
raise MFiUnhandledException()
except MFiUnhandledException:
self.logger.error("MFi setup is not yet possible.")
elif req[Tlv8.Tag.STATE] == PairingState.M5:
res = self.pair_setup_m5_m6(req[Tlv8.Tag.ENCRYPTEDDATA])
return Tlv8.encode(res)
Expand Down

0 comments on commit bd3ce4e

Please sign in to comment.