fast_pair_data_parser.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 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 ASH_SERVICES_QUICK_PAIR_FAST_PAIR_DATA_PARSER_H_
  5. #define ASH_SERVICES_QUICK_PAIR_FAST_PAIR_DATA_PARSER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <array>
  9. #include <vector>
  10. #include "ash/services/quick_pair/public/mojom/fast_pair_data_parser.mojom.h"
  11. #include "base/containers/span.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "mojo/public/cpp/bindings/receiver.h"
  14. inline constexpr int kEncryptedDataByteSize = 16;
  15. inline constexpr int kAesBlockByteSize = 16;
  16. namespace ash {
  17. namespace quick_pair {
  18. // This class is responsible for parsing the untrusted bytes from a Bluetooth
  19. // device during Fast Pair.
  20. class FastPairDataParser : public mojom::FastPairDataParser {
  21. public:
  22. explicit FastPairDataParser(
  23. mojo::PendingReceiver<mojom::FastPairDataParser> receiver);
  24. ~FastPairDataParser() override;
  25. FastPairDataParser(const FastPairDataParser&) = delete;
  26. FastPairDataParser& operator=(const FastPairDataParser&) = delete;
  27. // Gets the hex string representation of the device's model ID from the
  28. // service data.
  29. void GetHexModelIdFromServiceData(
  30. const std::vector<uint8_t>& service_data,
  31. GetHexModelIdFromServiceDataCallback callback) override;
  32. // Decrypts |encrypted_response_bytes| using |aes_key| and returns a parsed
  33. // DecryptedResponse instance if possible.
  34. void ParseDecryptedResponse(
  35. const std::vector<uint8_t>& aes_key_bytes,
  36. const std::vector<uint8_t>& encrypted_response_bytes,
  37. ParseDecryptedResponseCallback callback) override;
  38. // Decrypts |encrypted_passkey_bytes| using |aes_key| and returns a parsed
  39. // DecryptedPasskey instance if possible.
  40. void ParseDecryptedPasskey(
  41. const std::vector<uint8_t>& aes_key_bytes,
  42. const std::vector<uint8_t>& encrypted_passkey_bytes,
  43. ParseDecryptedPasskeyCallback callback) override;
  44. // Attempts to parse a 'Not Discoverable' advertisement from |service_data|.
  45. // If the advertisement does not contain information about salt, use the
  46. // |address| as salt instead.
  47. void ParseNotDiscoverableAdvertisement(
  48. const std::vector<uint8_t>& service_data,
  49. const std::string& address,
  50. ParseNotDiscoverableAdvertisementCallback callback) override;
  51. // Attempts to parse MessageStreamMessage instances from |message_bytes| and
  52. // stores results in array to pass to callback on success.
  53. void ParseMessageStreamMessages(
  54. const std::vector<uint8_t>& message_bytes,
  55. ParseMessageStreamMessagesCallback callback) override;
  56. private:
  57. mojom::MessageStreamMessagePtr ParseMessageStreamMessage(
  58. mojom::MessageGroup message_group,
  59. uint8_t message_code,
  60. const base::span<uint8_t>& additional_data);
  61. mojom::MessageStreamMessagePtr ParseBluetoothEvent(uint8_t message_code);
  62. mojom::MessageStreamMessagePtr ParseCompanionAppEvent(uint8_t message_code);
  63. mojom::MessageStreamMessagePtr ParseDeviceInformationEvent(
  64. uint8_t message_code,
  65. const base::span<uint8_t>& additional_data);
  66. mojom::MessageStreamMessagePtr ParseDeviceActionEvent(
  67. uint8_t message_code,
  68. const base::span<uint8_t>& additional_data);
  69. mojom::MessageStreamMessagePtr ParseAcknowledgementEvent(
  70. uint8_t message_code,
  71. const base::span<uint8_t>& additional_data);
  72. mojo::Receiver<mojom::FastPairDataParser> receiver_;
  73. };
  74. } // namespace quick_pair
  75. } // namespace ash
  76. #endif // ASH_SERVICES_QUICK_PAIR_FAST_PAIR_DATA_PARSER_H_