fido_cable_discovery.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef DEVICE_FIDO_CABLE_FIDO_CABLE_DISCOVERY_H_
  5. #define DEVICE_FIDO_CABLE_FIDO_CABLE_DISCOVERY_H_
  6. #include <stdint.h>
  7. #include <array>
  8. #include <map>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include "base/component_export.h"
  13. #include "base/containers/span.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "device/bluetooth/bluetooth_adapter.h"
  17. #include "device/fido/cable/cable_discovery_data.h"
  18. #include "device/fido/cable/fido_cable_device.h"
  19. #include "device/fido/cable/v2_constants.h"
  20. #include "device/fido/fido_device_discovery.h"
  21. namespace device {
  22. class BluetoothDevice;
  23. class BluetoothAdvertisement;
  24. class FidoCableHandshakeHandler;
  25. class COMPONENT_EXPORT(DEVICE_FIDO) FidoCableDiscovery
  26. : public FidoDeviceDiscovery,
  27. public BluetoothAdapter::Observer,
  28. public FidoCableDevice::Observer {
  29. public:
  30. explicit FidoCableDiscovery(std::vector<CableDiscoveryData> discovery_data);
  31. FidoCableDiscovery(const FidoCableDiscovery&) = delete;
  32. FidoCableDiscovery& operator=(const FidoCableDiscovery&) = delete;
  33. ~FidoCableDiscovery() override;
  34. // FidoDeviceDiscovery:
  35. bool MaybeStop() override;
  36. // GetV2AdvertStream returns a stream of caBLEv2 BLE adverts. Only a single
  37. // stream is supported.
  38. std::unique_ptr<FidoDeviceDiscovery::EventStream<
  39. base::span<const uint8_t, cablev2::kAdvertSize>>>
  40. GetV2AdvertStream();
  41. const std::map<CableEidArray, scoped_refptr<BluetoothAdvertisement>>&
  42. AdvertisementsForTesting() const {
  43. return advertisements_;
  44. }
  45. protected:
  46. virtual std::unique_ptr<FidoCableHandshakeHandler> CreateV1HandshakeHandler(
  47. FidoCableDevice* device,
  48. const CableDiscoveryData& discovery_data,
  49. const CableEidArray& authenticator_eid);
  50. private:
  51. enum class CableV1DiscoveryEvent : int;
  52. // V1DiscoveryDataAndEID represents a match against caBLEv1 pairing data. It
  53. // contains the CableDiscoveryData that matched and the BLE EID that triggered
  54. // the match.
  55. using V1DiscoveryDataAndEID = std::pair<CableDiscoveryData, CableEidArray>;
  56. // ObservedDeviceData contains potential EIDs observed from a BLE device. This
  57. // information is kept in order to de-duplicate device-log entries and make
  58. // debugging easier.
  59. struct ObservedDeviceData {
  60. ObservedDeviceData();
  61. ~ObservedDeviceData();
  62. absl::optional<CableEidArray> service_data;
  63. std::vector<CableEidArray> uuids;
  64. };
  65. static const BluetoothUUID& GoogleCableUUID();
  66. static const BluetoothUUID& FIDOCableUUID();
  67. static bool IsCableDevice(const BluetoothDevice* device);
  68. // ResultDebugString returns a string containing a hex dump of |eid| and a
  69. // description of |result|, if present.
  70. static std::string ResultDebugString(
  71. const CableEidArray& eid,
  72. const absl::optional<V1DiscoveryDataAndEID>& result);
  73. static absl::optional<CableEidArray> MaybeGetEidFromServiceData(
  74. const BluetoothDevice* device);
  75. static std::vector<CableEidArray> GetUUIDs(const BluetoothDevice* device);
  76. void StartCableDiscovery();
  77. void OnStartDiscoverySession(std::unique_ptr<BluetoothDiscoverySession>);
  78. void OnStartDiscoverySessionError();
  79. void StartAdvertisement();
  80. void OnAdvertisementRegistered(
  81. const CableEidArray& client_eid,
  82. scoped_refptr<BluetoothAdvertisement> advertisement);
  83. void OnGetAdapter(scoped_refptr<BluetoothAdapter> adapter);
  84. void OnSetPowered();
  85. void SetDiscoverySession(
  86. std::unique_ptr<BluetoothDiscoverySession> discovery_session);
  87. BluetoothAdapter* adapter() { return adapter_.get(); }
  88. // Attempt to stop all on-going advertisements in best-effort basis.
  89. // Once all the callbacks for Unregister() function is received, invoke
  90. // |callback|.
  91. void StopAdvertisements(base::OnceClosure callback);
  92. void OnAdvertisementsStopped(base::OnceClosure callback);
  93. void CableDeviceFound(BluetoothAdapter* adapter, BluetoothDevice* device);
  94. void ConductEncryptionHandshake(FidoCableHandshakeHandler* handshake_handler,
  95. CableDiscoveryData::Version cable_version);
  96. void ValidateAuthenticatorHandshakeMessage(
  97. CableDiscoveryData::Version cable_version,
  98. FidoCableHandshakeHandler* handshake_handler,
  99. absl::optional<std::vector<uint8_t>> handshake_response);
  100. absl::optional<V1DiscoveryDataAndEID> GetCableDiscoveryData(
  101. const BluetoothDevice* device);
  102. absl::optional<V1DiscoveryDataAndEID>
  103. GetCableDiscoveryDataFromAuthenticatorEid(CableEidArray authenticator_eid);
  104. void RecordCableV1DiscoveryEventOnce(CableV1DiscoveryEvent event);
  105. // FidoDeviceDiscovery:
  106. void StartInternal() override;
  107. // BluetoothAdapter::Observer:
  108. void DeviceAdded(BluetoothAdapter* adapter, BluetoothDevice* device) override;
  109. void DeviceChanged(BluetoothAdapter* adapter,
  110. BluetoothDevice* device) override;
  111. void DeviceRemoved(BluetoothAdapter* adapter,
  112. BluetoothDevice* device) override;
  113. void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override;
  114. void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
  115. bool discovering) override;
  116. // FidoCableDevice::Observer:
  117. void FidoCableDeviceConnected(FidoCableDevice* device, bool success) override;
  118. void FidoCableDeviceTimeout(FidoCableDevice* device) override;
  119. scoped_refptr<BluetoothAdapter> adapter_;
  120. std::unique_ptr<BluetoothDiscoverySession> discovery_session_;
  121. std::vector<CableDiscoveryData> discovery_data_;
  122. base::RepeatingCallback<void(base::span<const uint8_t, cablev2::kAdvertSize>)>
  123. advert_callback_;
  124. // active_authenticator_eids_ contains authenticator EIDs for which a
  125. // handshake is currently running. Further advertisements for the same EIDs
  126. // will be ignored.
  127. std::set<CableEidArray> active_authenticator_eids_;
  128. // active_devices_ contains the BLE addresses of devices for which a
  129. // handshake is already running. Further advertisements from these devices
  130. // will be ignored. However, devices may rotate their BLE address at will so
  131. // this is not completely effective.
  132. std::set<std::string> active_devices_;
  133. // Note that on Windows, |advertisements_| is the only reference holder of
  134. // BluetoothAdvertisement.
  135. std::map<CableEidArray, scoped_refptr<BluetoothAdvertisement>>
  136. advertisements_;
  137. std::vector<std::pair<std::unique_ptr<FidoCableDevice>,
  138. std::unique_ptr<FidoCableHandshakeHandler>>>
  139. active_handshakes_;
  140. // observed_devices_ caches the information from observed caBLE devices so
  141. // that the device-log isn't spammed.
  142. base::flat_map<std::string, std::unique_ptr<ObservedDeviceData>>
  143. observed_devices_;
  144. bool has_v1_discovery_data_ = false;
  145. base::flat_set<CableV1DiscoveryEvent> recorded_events_;
  146. base::WeakPtrFactory<FidoCableDiscovery> weak_factory_{this};
  147. };
  148. } // namespace device
  149. #endif // DEVICE_FIDO_CABLE_FIDO_CABLE_DISCOVERY_H_