fido_hid_packet.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 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_HID_FIDO_HID_PACKET_H_
  5. #define DEVICE_FIDO_HID_FIDO_HID_PACKET_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/component_export.h"
  11. #include "base/containers/span.h"
  12. #include "device/fido/fido_constants.h"
  13. namespace device {
  14. // HID Packets are defined by the specification at
  15. // https://fidoalliance.org/specs/fido-v2.0-rd-20161004/fido-client-to-authenticator-protocol-v2.0-rd-20161004.html#message-and-packet-structure
  16. // Packets are one of two types, initialization packets and continuation
  17. // packets. HID Packets have header information and a payload. If a
  18. // FidoHidInitPacket cannot store the entire payload, further payload
  19. // information is stored in HidContinuationPackets.
  20. class COMPONENT_EXPORT(DEVICE_FIDO) FidoHidPacket {
  21. public:
  22. FidoHidPacket(std::vector<uint8_t> data, uint32_t channel_id);
  23. FidoHidPacket(const FidoHidPacket&) = delete;
  24. FidoHidPacket& operator=(const FidoHidPacket&) = delete;
  25. virtual ~FidoHidPacket();
  26. virtual std::vector<uint8_t> GetSerializedData() const = 0;
  27. const std::vector<uint8_t>& GetPacketPayload() const { return data_; }
  28. uint32_t channel_id() const { return channel_id_; }
  29. protected:
  30. FidoHidPacket();
  31. std::vector<uint8_t> data_;
  32. uint32_t channel_id_ = kHidBroadcastChannel;
  33. private:
  34. friend class HidMessage;
  35. };
  36. // FidoHidInitPacket, based on the CTAP specification consists of a header with
  37. // data that is serialized into a IOBuffer. A channel identifier is allocated by
  38. // the CTAP device to ensure its system-wide uniqueness. Command identifiers
  39. // determine the type of message the packet corresponds to. Payload length
  40. // is the length of the entire message payload, and the data is only the portion
  41. // of the payload that will fit into the HidInitPacket.
  42. class COMPONENT_EXPORT(DEVICE_FIDO) FidoHidInitPacket final
  43. : public FidoHidPacket {
  44. public:
  45. // Creates a packet from the serialized data of an initialization packet. As
  46. // this is the first packet, the payload length of the entire message will be
  47. // included within the serialized data. Remaining size will be returned to
  48. // inform the callee how many additional packets to expect.
  49. static std::unique_ptr<FidoHidInitPacket> CreateFromSerializedData(
  50. base::span<const uint8_t> serialized,
  51. size_t* remaining_size);
  52. FidoHidInitPacket(uint32_t channel_id,
  53. FidoHidDeviceCommand cmd,
  54. std::vector<uint8_t> data,
  55. uint16_t payload_length);
  56. FidoHidInitPacket(const FidoHidInitPacket&) = delete;
  57. FidoHidInitPacket& operator=(const FidoHidInitPacket&) = delete;
  58. ~FidoHidInitPacket() final;
  59. std::vector<uint8_t> GetSerializedData() const final;
  60. FidoHidDeviceCommand command() const { return command_; }
  61. uint16_t payload_length() const { return payload_length_; }
  62. private:
  63. FidoHidDeviceCommand command_;
  64. uint16_t payload_length_;
  65. };
  66. // FidoHidContinuationPacket, based on the CTAP Specification consists of a
  67. // header with data that is serialized into an IOBuffer. The channel identifier
  68. // will be identical to the identifier in all other packets of the message. The
  69. // packet sequence will be the sequence number of this particular packet, from
  70. // 0x00 to 0x7f.
  71. class COMPONENT_EXPORT(DEVICE_FIDO) FidoHidContinuationPacket final
  72. : public FidoHidPacket {
  73. public:
  74. // Creates a packet from the serialized data of a continuation packet. As an
  75. // HidInitPacket would have arrived earlier with the total payload size,
  76. // the remaining size should be passed to inform the packet of how much data
  77. // to expect.
  78. static std::unique_ptr<FidoHidContinuationPacket> CreateFromSerializedData(
  79. base::span<const uint8_t> serialized,
  80. size_t* remaining_size);
  81. FidoHidContinuationPacket(uint32_t channel_id,
  82. uint8_t sequence,
  83. std::vector<uint8_t> data);
  84. FidoHidContinuationPacket(const FidoHidContinuationPacket&) = delete;
  85. FidoHidContinuationPacket& operator=(const FidoHidContinuationPacket&) =
  86. delete;
  87. ~FidoHidContinuationPacket() final;
  88. std::vector<uint8_t> GetSerializedData() const final;
  89. uint8_t sequence() const { return sequence_; }
  90. private:
  91. uint8_t sequence_;
  92. };
  93. } // namespace device
  94. #endif // DEVICE_FIDO_HID_FIDO_HID_PACKET_H_