fido_cable_device.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_DEVICE_H_
  5. #define DEVICE_FIDO_CABLE_FIDO_CABLE_DEVICE_H_
  6. #include <array>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/containers/span.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "crypto/aead.h"
  15. #include "device/fido/cable/fido_ble_connection.h"
  16. #include "device/fido/cable/fido_ble_transaction.h"
  17. #include "device/fido/fido_device.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace device {
  20. class BluetoothAdapter;
  21. class FidoBleFrame;
  22. class COMPONENT_EXPORT(DEVICE_FIDO) FidoCableDevice : public FidoDevice {
  23. public:
  24. using FrameCallback = FidoBleTransaction::FrameCallback;
  25. class Observer {
  26. public:
  27. virtual void FidoCableDeviceConnected(FidoCableDevice* device,
  28. bool success) {}
  29. virtual void FidoCableDeviceTimeout(FidoCableDevice* device) {}
  30. };
  31. FidoCableDevice(BluetoothAdapter* adapter, std::string address);
  32. // Constructor used for testing purposes.
  33. FidoCableDevice(std::unique_ptr<FidoBleConnection> connection);
  34. FidoCableDevice(const FidoCableDevice&) = delete;
  35. FidoCableDevice& operator=(const FidoCableDevice&) = delete;
  36. ~FidoCableDevice() override;
  37. // Returns FidoDevice::GetId() for a given FidoBleConnection address.
  38. static std::string GetIdForAddress(const std::string& ble_address);
  39. std::string GetAddress();
  40. void Connect();
  41. void SendPing(std::vector<uint8_t> data, DeviceCallback callback);
  42. FidoBleConnection::ReadCallback GetReadCallbackForTesting();
  43. void set_observer(Observer* observer);
  44. // FidoDevice:
  45. void Cancel(CancelToken token) override;
  46. std::string GetId() const override;
  47. FidoTransportProtocol DeviceTransport() const override;
  48. CancelToken DeviceTransact(std::vector<uint8_t> command,
  49. DeviceCallback callback) override;
  50. void SendHandshakeMessage(std::vector<uint8_t> handshake_message,
  51. DeviceCallback callback);
  52. // Configure caBLE v1 keys.
  53. void SetV1EncryptionData(base::span<const uint8_t, 32> session_key,
  54. base::span<const uint8_t, 8> nonce);
  55. // SetCountersForTesting allows tests to set the message counters. Non-test
  56. // code must not call this function.
  57. void SetSequenceNumbersForTesting(uint32_t read_counter,
  58. uint32_t write_counter);
  59. base::WeakPtr<FidoDevice> GetWeakPtr() override;
  60. private:
  61. struct PendingFrame {
  62. PendingFrame(FidoBleFrame frame, FrameCallback callback, CancelToken token);
  63. PendingFrame(PendingFrame&&);
  64. ~PendingFrame();
  65. FidoBleFrame frame;
  66. FrameCallback callback;
  67. CancelToken token;
  68. };
  69. // Encapsulates state FidoCableDevice maintains to encrypt and decrypt
  70. // data within FidoBleFrame.
  71. struct EncryptionData {
  72. EncryptionData();
  73. ~EncryptionData();
  74. std::array<uint8_t, 32> read_key;
  75. std::array<uint8_t, 32> write_key;
  76. std::array<uint8_t, 8> nonce;
  77. uint32_t write_sequence_num = 0;
  78. uint32_t read_sequence_num = 0;
  79. };
  80. void OnResponseFrame(FrameCallback callback,
  81. absl::optional<FidoBleFrame> frame);
  82. void Transition();
  83. CancelToken AddToPendingFrames(FidoBleDeviceCommand cmd,
  84. std::vector<uint8_t> request,
  85. DeviceCallback callback);
  86. void ResetTransaction();
  87. void OnConnected(bool success);
  88. void OnStatusMessage(std::vector<uint8_t> data);
  89. void OnReadControlPointLength(absl::optional<uint16_t> length);
  90. void SendRequestFrame(FidoBleFrame frame, FrameCallback callback);
  91. void StartTimeout();
  92. void StopTimeout();
  93. void OnTimeout();
  94. void OnBleResponseReceived(DeviceCallback callback,
  95. absl::optional<FidoBleFrame> frame);
  96. void ProcessBleDeviceError(base::span<const uint8_t> data);
  97. bool EncryptOutgoingMessage(std::vector<uint8_t>* message_to_encrypt);
  98. bool DecryptIncomingMessage(FidoBleFrame* incoming_frame);
  99. base::OneShotTimer timer_;
  100. std::unique_ptr<FidoBleConnection> connection_;
  101. uint16_t control_point_length_ = 0;
  102. // pending_frames_ contains frames that have not yet been sent, i.e. the
  103. // current frame is not included at the head of the list.
  104. std::list<PendingFrame> pending_frames_;
  105. // current_token_ contains the cancelation token of the currently running
  106. // request, or else is empty if no request is currently pending.
  107. absl::optional<CancelToken> current_token_;
  108. absl::optional<FidoBleTransaction> transaction_;
  109. raw_ptr<Observer> observer_ = nullptr;
  110. absl::optional<EncryptionData> encryption_data_;
  111. base::WeakPtrFactory<FidoCableDevice> weak_factory_{this};
  112. };
  113. } // namespace device
  114. #endif // DEVICE_FIDO_CABLE_FIDO_CABLE_DEVICE_H_