Пример #1
0
    def create_bond_out_of_band(self, address, type, p192_oob_data,
                                p256_oob_data):
        """
            Triggers stack under test to create bond using Out of Band method
        """

        logging.info("DUT: Creating OOB bond to '%s' from '%s'" %
                     (str(address), str(self._device.address)))

        self._device.security.CreateBondOutOfBand(
            OobDataBondMessage(
                address=common.BluetoothAddressWithType(
                    address=common.BluetoothAddress(address=address),
                    type=type),
                p192_data=OobDataMessage(
                    address=common.BluetoothAddressWithType(
                        address=common.BluetoothAddress(address=address),
                        type=type),
                    confirmation_value=bytes(bytearray(p192_oob_data[0])),
                    random_value=bytes(bytearray(p192_oob_data[1]))),
                p256_data=OobDataMessage(
                    address=common.BluetoothAddressWithType(
                        address=common.BluetoothAddress(address=address),
                        type=type),
                    confirmation_value=bytes(bytearray(p256_oob_data[0])),
                    random_value=bytes(bytearray(p256_oob_data[1])))))
Пример #2
0
    def setup_test(self):
        super().setup_test()

        self.dut_l2cap = PyLeL2cap(self.dut)
        self.cert_l2cap = CertLeL2cap(self.cert)
        self.dut_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'D0:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'C0:11:FF:AA:33:22')),
            type=common.RANDOM_DEVICE_ADDRESS)
        dut_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=self.dut_address,
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.dut_l2cap._device.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            dut_privacy_policy)
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=self.cert_address,
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.cert_l2cap._device.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            privacy_policy)
Пример #3
0
    def test_le_ad_scan_dut_scans(self):
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes(b'D0:05:04:03:02:01')),
                type=common.RANDOM_DEVICE_ADDRESS),
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.dut.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            privacy_policy)
        cert_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes(b'C0:05:04:03:02:01')),
                type=common.RANDOM_DEVICE_ADDRESS),
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.cert.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            cert_privacy_policy)
        with EventStream(
                # DUT Scans
                self.dut.hci_le_scanning_manager.StartScan(
                    empty_proto.Empty())) as advertising_event_stream:

            # CERT Advertises
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_The_CERT!'))
            gap_data = le_advertising_facade.GapDataMsg(
                data=bytes(gap_name.Serialize()))
            config = le_advertising_facade.AdvertisingConfig(
                advertisement=[gap_data],
                interval_min=512,
                interval_max=768,
                event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
                address_type=common.RANDOM_DEVICE_ADDRESS,
                channel_map=7,
                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
                ALL_DEVICES)
            request = le_advertising_facade.CreateAdvertiserRequest(
                config=config)

            create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(
                request)

            advertising_event_stream.assert_event_occurs(
                lambda packet: b'Im_The_CERT' in packet.event)

            remove_request = le_advertising_facade.RemoveAdvertiserRequest(
                advertiser_id=create_response.advertiser_id)
            self.cert.hci_le_advertising_manager.RemoveAdvertiser(
                remove_request)
Пример #4
0
    def setup_test(self):
        super().setup_test()

        self.dut_address = self.dut.hci_controller.GetMacAddressSimple()
        cert_address = common.BluetoothAddress(
            address=self.cert.controller_read_only_property.ReadLocalAddress(empty_proto.Empty()).address)

        self.dut_l2cap = PyL2cap(self.dut, cert_address)
        self.cert_l2cap = CertL2cap(self.cert)
        self.dut_le_l2cap = PyLeL2cap(self.dut)
        self.cert_le_l2cap = CertLeL2cap(self.cert)
        self.dut_le_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(address=bytes(b'D0:05:04:03:02:01')), type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(address=bytes(b'C0:11:FF:AA:33:22')), type=common.RANDOM_DEVICE_ADDRESS)
        dut_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.USE_STATIC_ADDRESS,
            address_with_type=self.dut_le_address,
            rotation_irk=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.dut_l2cap._device.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(dut_privacy_policy)
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.USE_STATIC_ADDRESS,
            address_with_type=self.cert_address,
            rotation_irk=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.cert_le_l2cap._device.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(privacy_policy)
Пример #5
0
    def setup_test(self):
        super().setup_test()

        self.dut_security = PyLeSecurity(self.dut)
        self.cert_security = PyLeSecurity(self.cert)
        self.dut_hci = PyHci(self.dut)

        self.dut_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'DD:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=self.dut_address)
        self.dut.security.SetLeInitiatorAddressPolicy(privacy_policy)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'C5:11:FF:AA:33:22')),
            type=common.RANDOM_DEVICE_ADDRESS)
        cert_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=self.cert_address)
        self.cert.security.SetLeInitiatorAddressPolicy(cert_privacy_policy)
    def setup_test(self):
        super().setup_test()

        self.dut.address = self.dut.controller_read_only_property.ReadLocalAddress(
            empty_proto.Empty()).address
        self.cert.address = self.cert.controller_read_only_property.ReadLocalAddress(
            empty_proto.Empty()).address

        self.dut.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))
        self.cert.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))

        self.dut_address = common.BluetoothAddress(address=self.dut.address)
        self.cert_address = common.BluetoothAddress(address=self.cert.address)

        self.dut_address_with_type = common.BluetoothAddressWithType()
        self.dut_address_with_type.address.CopyFrom(self.dut_address)
        self.dut_address_with_type.type = common.BluetoothPeerAddressTypeEnum.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS

        self.cert_address_with_type = common.BluetoothAddressWithType()
        self.cert_address_with_type.address.CopyFrom(self.cert_address)
        self.cert_address_with_type.type = common.BluetoothPeerAddressTypeEnum.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS

        self.dut.wait_channel_ready()
        self.cert.wait_channel_ready()

        self.cert_name = b'ImTheCert'
        self.cert.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.cert_name))
        self.dut_name = b'ImTheDUT'
        self.dut.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.dut_name))
Пример #7
0
    def setup_test(self):
        super().setup_test()

        self.dut_security = PyLeSecurity(self.dut)
        self.cert_security = PyLeSecurity(self.cert)

        self.dut_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'0D:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'55:11:FF:AA:33:22')),
            type=common.RANDOM_DEVICE_ADDRESS)
Пример #8
0
    def setup_test(self):
        super().setup_test()

        self.dut_l2cap = PyLeL2cap(self.dut)
        self.cert_l2cap = CertLeL2cap(self.cert)
        self.dut_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'0D:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'55:11:FF:AA:33:22')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_l2cap._device.hci_le_acl_manager.SetInitiatorAddress(
            self.cert_address)
Пример #9
0
 def remove_bond(self, address, type):
     """
         Removes bond from stack under test
     """
     self._device.security.RemoveBond(
         common.BluetoothAddressWithType(
             address=common.BluetoothAddress(address=address), type=type))
Пример #10
0
    def setup_test(self):
        super().setup_test()

        self.dut.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))
        self.cert.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))

        self.dut.name = b'DUT Device'
        self.dut.address = self.dut.hci_controller.GetMacAddress(
            empty_proto.Empty()).address
        self.cert.name = b'Cert Device'
        self.cert.address = self.cert.hci_controller.GetMacAddress(
            empty_proto.Empty()).address

        # TODO(optedoblivion): Make this happen in PySecurity or GdDevice
        self.dut.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.dut.name))
        self.cert.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.cert.name))

        self.dut_security = PySecurity(self.dut)
        self.cert_security = CertSecurity(self.cert)

        self.dut_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'DD:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=self.dut_address)
        self.dut.security.SetLeInitiatorAddressPolicy(privacy_policy)
Пример #11
0
 def enforce_security_policy(self, address, type, policy):
     """
         Call to enforce classic security policy
     """
     self._device.security.EnforceSecurityPolicy(
         SecurityPolicyMessage(address=common.BluetoothAddressWithType(
             address=common.BluetoothAddress(address=address), type=type),
                               policy=policy))
Пример #12
0
 def create_bond(self, address, type):
     """
         Triggers stack under test to create bond
     """
     logging.info("DUT: Creating bond to '%s' from '%s'" %
                  (str(address), str(self._device.address)))
     self._device.security.CreateBond(
         common.BluetoothAddressWithType(
             address=common.BluetoothAddress(address=address), type=type))
 def set_cert_privacy_policy_with_random_address(self, random_address):
     private_policy = le_initiator_address_facade.PrivacyPolicy(
         address_policy=le_initiator_address_facade.AddressPolicy.
         USE_STATIC_ADDRESS,
         address_with_type=common.BluetoothAddressWithType(
             address=common.BluetoothAddress(
                 address=bytes(random_address, encoding='utf8')),
             type=common.RANDOM_DEVICE_ADDRESS))
     self.cert.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
         private_policy)
Пример #14
0
    def setup_test(self):
        self.device_under_test.rootservice.StartStack(
            facade_rootservice_pb2.StartStackRequest(
                module_under_test=facade_rootservice_pb2.BluetoothModule.Value(
                    'SECURITY'), ))
        self.cert_device.rootservice.StartStack(
            facade_rootservice_pb2.StartStackRequest(
                module_under_test=facade_rootservice_pb2.BluetoothModule.Value(
                    'L2CAP'), ))

        self.device_under_test.address = self.device_under_test.controller_read_only_property.ReadLocalAddress(
            empty_proto.Empty()).address
        self.cert_device.address = self.cert_device.controller_read_only_property.ReadLocalAddress(
            empty_proto.Empty()).address

        self.device_under_test.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))
        self.cert_device.neighbor.EnablePageScan(
            neighbor_facade.EnableMsg(enabled=True))

        self.dut_address = common.BluetoothAddress(
            address=self.device_under_test.address)
        self.cert_address = common.BluetoothAddress(
            address=self.cert_device.address)

        self.dut_address_with_type = common.BluetoothAddressWithType()
        self.dut_address_with_type.address.CopyFrom(self.dut_address)
        self.dut_address_with_type.type = common.BluetoothPeerAddressTypeEnum.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS

        self.cert_address_with_type = common.BluetoothAddressWithType()
        self.cert_address_with_type.address.CopyFrom(self.cert_address)
        self.cert_address_with_type.type = common.BluetoothPeerAddressTypeEnum.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS

        self.device_under_test.wait_channel_ready()
        self.cert_device.wait_channel_ready()

        self.cert_name = b'ImTheCert'
        self.cert_device.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.cert_name))
        self.dut_name = b'ImTheDUT'
        self.device_under_test.hci_controller.WriteLocalName(
            controller_facade.NameMsg(name=self.dut_name))
Пример #15
0
 def set_privacy_policy_static(self):
     self.dut_address = b'd0:05:04:03:02:01'
     private_policy = le_initiator_address_facade.PrivacyPolicy(
         address_policy=le_initiator_address_facade.AddressPolicy.
         USE_STATIC_ADDRESS,
         address_with_type=common.BluetoothAddressWithType(
             address=common.BluetoothAddress(
                 address=bytes(self.dut_address)),
             type=common.RANDOM_DEVICE_ADDRESS))
     self.dut.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
         private_policy)
Пример #16
0
    def setup_test(self):
        super().setup_test()

        self.dut_address = self.dut.hci_controller.GetMacAddressSimple()
        cert_address = common.BluetoothAddress(
            address=self.cert.controller_read_only_property.ReadLocalAddress(
                empty_proto.Empty()).address)

        self.dut_l2cap = PyL2cap(self.dut, cert_address)
        self.cert_l2cap = CertL2cap(self.cert)
        self.dut_le_l2cap = PyLeL2cap(self.dut)
        self.cert_le_l2cap = CertLeL2cap(self.cert)
        self.dut_le_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'0D:05:04:03:02:01')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_address = common.BluetoothAddressWithType(
            address=common.BluetoothAddress(
                address=bytes(b'55:11:FF:AA:33:22')),
            type=common.RANDOM_DEVICE_ADDRESS)
        self.cert_le_l2cap._device.hci_le_acl_manager.SetInitiatorAddress(
            self.cert_address)
 def set_cert_privacy_policy_with_public_address(self):
     public_address_bytes = self.cert.hci_controller.GetMacAddress(
         empty_proto.Empty()).address
     private_policy = le_initiator_address_facade.PrivacyPolicy(
         address_policy=le_initiator_address_facade.AddressPolicy.
         USE_PUBLIC_ADDRESS,
         address_with_type=common.BluetoothAddressWithType(
             address=common.BluetoothAddress(address=public_address_bytes),
             type=common.PUBLIC_DEVICE_ADDRESS))
     self.cert.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
         private_policy)
     # Bluetooth MAC address must be upper case
     return public_address_bytes.decode('utf-8').upper()
Пример #18
0
 def send_ui_callback(self, address, callback_type, b, uid):
     """
         Send a callback from the UI as if the user pressed a button on the dialog
     """
     logging.info("DUT: Sending user input response uid: %d; response: %s" %
                  (uid, b))
     self._device.security.SendUiCallback(
         UiCallbackMsg(message_type=callback_type,
                       boolean=b,
                       unique_id=uid,
                       address=common.BluetoothAddressWithType(
                           address=common.BluetoothAddress(address=address),
                           type=common.BluetoothAddressTypeEnum.
                           PUBLIC_DEVICE_ADDRESS)))
Пример #19
0
    def dut_connects(self, check_address):
        self.register_for_le_event(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        self.register_for_le_event(
            hci_packets.SubeventCode.ENHANCED_CONNECTION_COMPLETE)

        # Cert Advertises
        advertising_handle = 0
        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                advertising_handle,
                hci_packets.LegacyAdvertisingProperties.ADV_IND,
                400,
                450,
                7,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.PeerAddressType.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                '00:00:00:00:00:00',
                hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                0xF8,
                1,  #SID
                hci_packets.Enable.DISABLED  # Scan request notification
            ))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                advertising_handle, '0C:05:04:03:02:01'))

        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(b'Im_A_Cert'))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingDataBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_name]))

        gap_short_name = hci_packets.GapData()
        gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
        gap_short_name.data = list(bytes(b'Im_A_C'))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_short_name]))

        enabled_set = hci_packets.EnabledSet()
        enabled_set.advertising_handle = advertising_handle
        enabled_set.duration = 0
        enabled_set.max_extended_advertising_events = 0
        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                hci_packets.Enable.ENABLED, [enabled_set]))

        self.dut_le_acl = self.dut_le_acl_manager.connect_to_remote(
            remote_addr=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes('0C:05:04:03:02:01', 'utf8')),
                type=int(hci_packets.AddressType.RANDOM_DEVICE_ADDRESS)))

        # Cert gets ConnectionComplete with a handle and sends ACL data
        handle = 0xfff
        address = hci_packets.Address()

        def get_handle(packet):
            packet_bytes = packet.payload
            nonlocal handle
            nonlocal address
            if b'\x3e\x13\x01\x00' in packet_bytes:
                cc_view = hci_packets.LeConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                address = cc_view.GetPeerAddress()
                return True
            if b'\x3e\x13\x0A\x00' in packet_bytes:
                cc_view = hci_packets.LeEnhancedConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                address = cc_view.GetPeerResolvablePrivateAddress()
                return True
            return False

        self.cert_hci_le_event_stream.assert_event_occurs(get_handle)
        self.cert_handle = handle
        dut_address_from_complete = address
        if check_address:
            assertThat(dut_address_from_complete).isEqualTo(
                self.dut_address.decode())
    def pair_justworks(self, cert_iocap_reply, expected_ui_event):
        # Cert event registration
        self.register_for_event(hci_packets.EventCode.LINK_KEY_REQUEST)
        self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_REQUEST)
        self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_RESPONSE)
        self.register_for_event(
            hci_packets.EventCode.USER_PASSKEY_NOTIFICATION)
        self.register_for_event(
            hci_packets.EventCode.USER_CONFIRMATION_REQUEST)
        self.register_for_event(
            hci_packets.EventCode.REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION)
        self.register_for_event(hci_packets.EventCode.LINK_KEY_NOTIFICATION)
        self.register_for_event(hci_packets.EventCode.SIMPLE_PAIRING_COMPLETE)
        with EventStream(self.dut.security.FetchUiEvents(empty_proto.Empty())) as dut_ui_stream, \
            EventStream(self.dut.security.FetchBondEvents(empty_proto.Empty())) as dut_bond_stream, \
            EventStream(self.dut.neighbor.GetRemoteNameEvents(empty_proto.Empty())) as dut_name_stream, \
            EventStream(self.cert.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream:

            dut_address = self.dut.hci_controller.GetMacAddress(
                empty_proto.Empty()).address
            cert_address = self.cert.hci_controller.GetMacAddress(
                empty_proto.Empty()).address

            # Enable Simple Secure Pairing
            self.enqueue_hci_command(
                hci_packets.WriteSimplePairingModeBuilder(
                    hci_packets.Enable.ENABLED), True)

            cert_hci_event_stream.assert_event_occurs(
                lambda msg: b'\x0e\x04\x01\x56\x0c' in msg.event)

            # Get the name
            self.dut.neighbor.ReadRemoteName(
                neighbor_facade.RemoteNameRequestMsg(
                    address=cert_address,
                    page_scan_repetition_mode=1,
                    clock_offset=0x6855))

            dut_name_stream.assert_event_occurs(
                lambda msg: self.cert_name in msg.name)

            self.dut.security.CreateBond(
                common.BluetoothAddressWithType(
                    address=common.BluetoothAddress(address=cert_address),
                    type=common.BluetoothAddressTypeEnum.PUBLIC_DEVICE_ADDRESS)
            )

            cert_hci_event_stream.assert_event_occurs(
                lambda event: logging.debug(event.event) or hci_packets.
                EventCode.IO_CAPABILITY_REQUEST in event.event)

            self.enqueue_hci_command(cert_iocap_reply, True)

            cert_hci_event_stream.assert_event_occurs(
                lambda event: logging.debug(event.event) or hci_packets.
                EventCode.USER_CONFIRMATION_REQUEST in event.event)
            self.enqueue_hci_command(
                hci_packets.UserConfirmationRequestReplyBuilder(
                    dut_address.decode('utf8')), True)

            logging.info("Waiting for UI event")
            ui_id = -1

            def get_unique_id(event):
                if (event.message_type == expected_ui_event):
                    nonlocal ui_id
                    ui_id = event.unique_id
                    return True
                return False

            dut_ui_stream.assert_event_occurs(get_unique_id)

            logging.info("Sending UI response")
            self.dut.security.SendUiCallback(
                security_facade.UiCallbackMsg(
                    message_type=security_facade.UiCallbackType.YES_NO,
                    boolean=True,
                    unique_id=ui_id,
                    address=common.BluetoothAddressWithType(
                        address=common.BluetoothAddress(address=cert_address),
                        type=common.BluetoothAddressTypeEnum.
                        PUBLIC_DEVICE_ADDRESS)))