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