def __init__(self, ble_device, peer, security_parameters): """ :type ble_device: blatann.BleDevice :type peer: blatann.peer.Peer :type security_parameters: SecurityParameters """ self.ble_device = ble_device self.peer = peer self._security_params = security_parameters self._pairing_in_process = False self._initiated_encryption = False self._is_previously_bonded_device = False self._on_authentication_complete_event = EventSource( "On Authentication Complete", logger) self._on_passkey_display_event = EventSource("On Passkey Display", logger) self._on_passkey_entry_event = EventSource("On Passkey Entry", logger) self._on_security_level_changed_event = EventSource( "Security Level Changed", logger) self._on_peripheral_security_request_event = EventSource( "Peripheral Security Request", logger) self._on_pairing_request_rejected_event = EventSource( "Pairing Attempt Rejected", logger) self.peer.on_connect.register(self._on_peer_connected) self._auth_key_resolve_thread = threading.Thread(daemon=True) self._peripheral_security_request_thread = threading.Thread( daemon=True) self.keyset = nrf_types.BLEGapSecKeyset() self.bond_db_entry = None self._security_level = SecurityLevel.NO_ACCESS self._private_key = smp_crypto.lesc_generate_private_key() self._public_key = self._private_key.public_key() self.keyset.own_keys.public_key.key = smp_crypto.lesc_pubkey_to_raw( self._public_key)
def use_debug_lesc_key(self): """ Changes the security settings to use the debug public/private key-pair for future LESC pairing interactions. The key is defined in the Core Bluetooth Specification v4.2 Vol.3, Part H, Section 2.3.5.6. .. warning:: Using this key allows Bluetooth sniffers to be able to decode the encrypted traffic over the air """ self._private_key = smp_crypto.LESC_DEBUG_PRIVATE_KEY self._public_key = smp_crypto.LESC_DEBUG_PUBLIC_KEY self.keyset.own_keys.public_key.key = smp_crypto.lesc_pubkey_to_raw(self._public_key)
def _on_peer_connected(self, peer, event_args): # Reset the self._pairing_in_process = False self._initiated_encryption = False self._security_level = SecurityLevel.OPEN self.keyset = nrf_types.BLEGapSecKeyset() self.keyset.own_keys.public_key.key = smp_crypto.lesc_pubkey_to_raw(self._public_key) self.peer.driver_event_subscribe(self._on_security_params_request, nrf_events.GapEvtSecParamsRequest) self.peer.driver_event_subscribe(self._on_authentication_status, nrf_events.GapEvtAuthStatus) self.peer.driver_event_subscribe(self._on_conn_sec_status, nrf_events.GapEvtConnSecUpdate) self.peer.driver_event_subscribe(self._on_auth_key_request, nrf_events.GapEvtAuthKeyRequest) self.peer.driver_event_subscribe(self._on_passkey_display, nrf_events.GapEvtPasskeyDisplay) self.peer.driver_event_subscribe(self._on_security_info_request, nrf_events.GapEvtSecInfoRequest) self.peer.driver_event_subscribe(self._on_lesc_dhkey_request, nrf_events.GapEvtLescDhKeyRequest) self.peer.driver_event_subscribe(self._on_security_request, nrf_events.GapEvtSecRequest) # Search the bonding DB for this peer's info self.bond_db_entry = self._find_db_entry(self.peer.peer_address) if self.bond_db_entry: logger.info("Connected to previously bonded device {}".format(self.bond_db_entry.peer_addr)) self._is_previously_bonded_device = True else: self._is_previously_bonded_device = False