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