| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_U2F_U2F_PACKET_H_ | 5 #ifndef DEVICE_U2F_U2F_PACKET_H_ |
| 6 #define DEVICE_U2F_U2F_PACKET_H_ | 6 #define DEVICE_U2F_U2F_PACKET_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 class IOBufferWithSize; | 14 class IOBufferWithSize; |
| 15 } // namespace net | 15 } // namespace net |
| 16 | 16 |
| 17 namespace device { | 17 namespace device { |
| 18 | 18 |
| 19 // U2fPackets are defined by the specification at | 19 // U2fPackets are defined by the specification at |
| 20 // http://fidoalliance.org/specs/u2f-specs-1.0-bt-nfc-id-amendment/fido-u2f-hid-
protocol.html | 20 // http://fidoalliance.org/specs/u2f-specs-1.0-bt-nfc-id-amendment/fido-u2f-hid-
protocol.html |
| 21 // Packets are one of two types, initialization packets and continuation | 21 // Packets are one of two types, initialization packets and continuation |
| 22 // packets, | 22 // packets, |
| 23 // (U2fInitPacket and U2fContinuationPacket). U2fPackets have header information | 23 // (U2fInitPacket and U2fContinuationPacket). U2fPackets have header information |
| 24 // and a payload. If a U2fInitPacket cannot store the entire payload, further | 24 // and a payload. If a U2fInitPacket cannot store the entire payload, further |
| 25 // payload information is stored in U2fContinuationPackets. | 25 // payload information is stored in U2fContinuationPackets. |
| 26 class U2fPacket { | 26 class U2fPacket { |
| 27 public: | 27 public: |
| 28 U2fPacket(const std::vector<uint8_t> data, uint32_t channel_id); | 28 U2fPacket(const std::vector<uint8_t>& data, uint32_t channel_id); |
| 29 virtual ~U2fPacket(); | 29 virtual ~U2fPacket(); |
| 30 | 30 |
| 31 scoped_refptr<net::IOBufferWithSize> GetSerializedBuffer(); | 31 virtual scoped_refptr<net::IOBufferWithSize> GetSerializedData() = 0; |
| 32 std::vector<uint8_t> GetPacketPayload() const; | 32 std::vector<uint8_t> GetPacketPayload() const; |
| 33 uint32_t channel_id() { return channel_id_; } | 33 uint32_t channel_id() { return channel_id_; } |
| 34 | 34 |
| 35 protected: | 35 protected: |
| 36 U2fPacket(); | 36 U2fPacket(); |
| 37 | 37 |
| 38 // Packet size of 64 bytes + 1 byte report ID | 38 // Packet size of 64 bytes + 1 byte report ID |
| 39 static constexpr size_t kPacketSize = 65; | 39 static constexpr size_t kPacketSize = 65; |
| 40 std::vector<uint8_t> data_; | 40 std::vector<uint8_t> data_; |
| 41 uint32_t channel_id_; | 41 uint32_t channel_id_; |
| 42 scoped_refptr<net::IOBufferWithSize> serialized_; | |
| 43 | 42 |
| 44 private: | 43 private: |
| 45 friend class U2fMessage; | 44 friend class U2fMessage; |
| 46 }; | 45 }; |
| 47 | 46 |
| 48 // U2fInitPacket, based on the U2f specification consists of a header with data | 47 // U2fInitPacket, based on the U2f specification consists of a header with data |
| 49 // that is serialized into a IOBuffer. A channel identifier is allocated by the | 48 // that is serialized into a IOBuffer. A channel identifier is allocated by the |
| 50 // U2F device to ensure its system-wide uniqueness. Command identifiers | 49 // U2F device to ensure its system-wide uniqueness. Command identifiers |
| 51 // determine the type of message the packet corresponds to. Payload length | 50 // determine the type of message the packet corresponds to. Payload length |
| 52 // is the length of the entire message payload, and the data is only the portion | 51 // is the length of the entire message payload, and the data is only the portion |
| 53 // of the payload that will fit into the U2fInitPacket. | 52 // of the payload that will fit into the U2fInitPacket. |
| 54 class U2fInitPacket : public U2fPacket { | 53 class U2fInitPacket : public U2fPacket { |
| 55 public: | 54 public: |
| 56 U2fInitPacket(uint32_t channel_id, | 55 U2fInitPacket(uint32_t channel_id, |
| 57 uint8_t cmd, | 56 uint8_t cmd, |
| 58 const std::vector<uint8_t> data, | 57 const std::vector<uint8_t>& data, |
| 59 uint16_t payload_length); | 58 uint16_t payload_length); |
| 60 U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, | 59 U2fInitPacket(const std::vector<uint8_t>& serialized, size_t* remaining_size); |
| 61 size_t* remaining_size); | |
| 62 ~U2fInitPacket() final; | 60 ~U2fInitPacket() final; |
| 63 | 61 |
| 64 // Creates a packet from the serialized data of an initialization packet. As | 62 // Creates a packet from the serialized data of an initialization packet. As |
| 65 // this is the first packet, the payload length of the entire message will be | 63 // this is the first packet, the payload length of the entire message will be |
| 66 // included within the serialized data. Remaining size will be returned to | 64 // included within the serialized data. Remaining size will be returned to |
| 67 // inform the callee how many additional packets to expect. | 65 // inform the callee how many additional packets to expect. |
| 68 static std::unique_ptr<U2fInitPacket> CreateFromSerializedData( | 66 static std::unique_ptr<U2fInitPacket> CreateFromSerializedData( |
| 69 scoped_refptr<net::IOBufferWithSize> buf, | 67 const std::vector<uint8_t>& serialized, |
| 70 size_t* remaining_size); | 68 size_t* remaining_size); |
| 69 scoped_refptr<net::IOBufferWithSize> GetSerializedData() final; |
| 71 uint8_t command() { return command_; } | 70 uint8_t command() { return command_; } |
| 72 uint16_t payload_length() { return payload_length_; } | 71 uint16_t payload_length() { return payload_length_; } |
| 73 | 72 |
| 74 private: | 73 private: |
| 75 uint8_t command_; | 74 uint8_t command_; |
| 76 uint16_t payload_length_; | 75 uint16_t payload_length_; |
| 77 }; | 76 }; |
| 78 | 77 |
| 79 // U2fContinuationPacket, based on the U2f Specification consists of a header | 78 // U2fContinuationPacket, based on the U2f Specification consists of a header |
| 80 // with data that is serialized into an IOBuffer. The channel identifier will | 79 // with data that is serialized into an IOBuffer. The channel identifier will |
| 81 // be identical to the identifier in all other packets of the message. The | 80 // be identical to the identifier in all other packets of the message. The |
| 82 // packet sequence will be the sequence number of this particular packet, from | 81 // packet sequence will be the sequence number of this particular packet, from |
| 83 // 0x00 to 0x7f. | 82 // 0x00 to 0x7f. |
| 84 class U2fContinuationPacket : public U2fPacket { | 83 class U2fContinuationPacket : public U2fPacket { |
| 85 public: | 84 public: |
| 86 U2fContinuationPacket(const uint32_t channel_id, | 85 U2fContinuationPacket(uint32_t channel_id, |
| 87 const uint8_t sequence, | 86 uint8_t sequence, |
| 88 std::vector<uint8_t> data); | 87 const std::vector<uint8_t>& data); |
| 89 U2fContinuationPacket(scoped_refptr<net::IOBufferWithSize> buf, | 88 U2fContinuationPacket(const std::vector<uint8_t>& serialized, |
| 90 size_t* remaining_size); | 89 size_t* remaining_size); |
| 91 ~U2fContinuationPacket() final; | 90 ~U2fContinuationPacket() final; |
| 92 | 91 |
| 93 // Creates a packet from the serialized data of a continuation packet. As an | 92 // Creates a packet from the serialized data of a continuation packet. As an |
| 94 // U2fInitPacket would have arrived earlier with the total payload size, | 93 // U2fInitPacket would have arrived earlier with the total payload size, |
| 95 // the remaining size should be passed to inform the packet of how much data | 94 // the remaining size should be passed to inform the packet of how much data |
| 96 // to expect. | 95 // to expect. |
| 97 static std::unique_ptr<U2fContinuationPacket> CreateFromSerializedData( | 96 static std::unique_ptr<U2fContinuationPacket> CreateFromSerializedData( |
| 98 scoped_refptr<net::IOBufferWithSize> buf, | 97 const std::vector<uint8_t>& serialized, |
| 99 size_t* remaining_size); | 98 size_t* remaining_size); |
| 99 scoped_refptr<net::IOBufferWithSize> GetSerializedData() final; |
| 100 uint8_t sequence() { return sequence_; } | 100 uint8_t sequence() { return sequence_; } |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 uint8_t sequence_; | 103 uint8_t sequence_; |
| 104 }; | 104 }; |
| 105 } // namespace device | 105 } // namespace device |
| 106 | 106 |
| 107 #endif // DEVICE_U2F_U2F_PACKET_H_ | 107 #endif // DEVICE_U2F_U2F_PACKET_H_ |
| OLD | NEW |