pairing: always ask user for confirmation, even in JUST_WORKS method

This commit is contained in:
Abel Lucas
2022-11-09 23:00:14 +00:00
parent ee54df2d08
commit 8bbba7c84c
2 changed files with 38 additions and 11 deletions

View File

@@ -2067,13 +2067,13 @@ class Device(CompositeEventEmitter):
# Ask what the pairing config should be for this connection # Ask what the pairing config should be for this connection
pairing_config = self.pairing_config_factory(connection) pairing_config = self.pairing_config_factory(connection)
can_confirm = pairing_config.delegate.io_capability not in { can_compare = pairing_config.delegate.io_capability not in {
smp.SMP_NO_INPUT_NO_OUTPUT_IO_CAPABILITY, smp.SMP_NO_INPUT_NO_OUTPUT_IO_CAPABILITY,
smp.SMP_DISPLAY_ONLY_IO_CAPABILITY smp.SMP_DISPLAY_ONLY_IO_CAPABILITY
} }
# Respond # Respond
if can_confirm and pairing_config.delegate: if can_compare:
async def compare_numbers(): async def compare_numbers():
numbers_match = await pairing_config.delegate.compare_numbers(code, digits=6) numbers_match = await pairing_config.delegate.compare_numbers(code, digits=6)
if numbers_match: if numbers_match:
@@ -2087,9 +2087,18 @@ class Device(CompositeEventEmitter):
asyncio.create_task(compare_numbers()) asyncio.create_task(compare_numbers())
else: else:
self.host.send_command_sync( async def confirm():
HCI_User_Confirmation_Request_Reply_Command(bd_addr=connection.peer_address) confirm = await pairing_config.delegate.confirm()
) if confirm:
self.host.send_command_sync(
HCI_User_Confirmation_Request_Reply_Command(bd_addr=connection.peer_address)
)
else:
self.host.send_command_sync(
HCI_User_Confirmation_Request_Negative_Reply_Command(bd_addr=connection.peer_address)
)
asyncio.create_task(confirm())
# [Classic only] # [Classic only]
@host_event_handler @host_event_handler
@@ -2104,7 +2113,7 @@ class Device(CompositeEventEmitter):
} }
# Respond # Respond
if can_input and pairing_config.delegate: if can_input:
async def get_number(): async def get_number():
number = await pairing_config.delegate.get_number() number = await pairing_config.delegate.get_number()
if number is not None: if number is not None:

View File

@@ -477,6 +477,9 @@ class PairingDelegate:
async def accept(self): async def accept(self):
return True return True
async def confirm(self):
return True
async def compare_numbers(self, number, digits=6): async def compare_numbers(self, number, digits=6):
return True return True
@@ -715,6 +718,21 @@ class Session:
return False return False
return True return True
def prompt_user_for_confirmation(self, next_steps):
async def prompt():
logger.debug('ask for confirmation')
try:
response = await self.pairing_config.delegate.confirm()
if response:
next_steps()
return
except Exception as error:
logger.warn(f'exception while confirm: {error}')
self.send_pairing_failed(SMP_CONFIRM_VALUE_FAILED_ERROR)
asyncio.create_task(prompt())
def prompt_user_for_numeric_comparison(self, code, next_steps): def prompt_user_for_numeric_comparison(self, code, next_steps):
async def prompt(): async def prompt():
logger.debug(f'verification code: {code}') logger.debug(f'verification code: {code}')
@@ -1387,12 +1405,12 @@ class Session:
# Compute the 6-digit code # Compute the 6-digit code
code = crypto.g2(self.pka, self.pkb, self.na, self.nb) % 1000000 code = crypto.g2(self.pka, self.pkb, self.na, self.nb) % 1000000
if self.pairing_method == self.NUMERIC_COMPARISON: # Ask for user confirmation
# Ask for user confirmation self.wait_before_continuing = asyncio.get_running_loop().create_future()
self.wait_before_continuing = asyncio.get_running_loop().create_future() if self.pairing_method == self.JUST_WORKS:
self.prompt_user_for_numeric_comparison(code, next_steps) self.prompt_user_for_confirmation(next_steps)
else: else:
next_steps() self.prompt_user_for_numeric_comparison(code, next_steps)
else: else:
next_steps() next_steps()