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 : public base::RefCountedThreadSafe<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 | 30 |
30 scoped_refptr<net::IOBufferWithSize> GetSerializedBuffer(); | 31 scoped_refptr<net::IOBufferWithSize> GetSerializedBuffer(); |
31 std::vector<uint8_t> GetPacketPayload() const; | 32 std::vector<uint8_t> GetPacketPayload() const; |
32 uint32_t channel_id() { return channel_id_; } | 33 uint32_t channel_id() { return channel_id_; } |
33 | 34 |
34 protected: | 35 protected: |
35 U2fPacket(); | 36 U2fPacket(); |
36 virtual ~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_; | 42 scoped_refptr<net::IOBufferWithSize> serialized_; |
43 | 43 |
44 private: | 44 private: |
45 friend class base::RefCountedThreadSafe<U2fPacket>; | |
46 friend class U2fMessage; | 45 friend class U2fMessage; |
47 }; | 46 }; |
48 | 47 |
49 // U2fInitPacket, based on the U2f specification consists of a header with data | 48 // 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 | 49 // that is serialized into a IOBuffer. A channel identifier is allocated by the |
51 // U2F device to ensure its system-wide uniqueness. Command identifiers | 50 // U2F device to ensure its system-wide uniqueness. Command identifiers |
52 // determine the type of message the packet corresponds to. Payload length | 51 // 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 | 52 // 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. | 53 // of the payload that will fit into the U2fInitPacket. |
55 class U2fInitPacket : public U2fPacket { | 54 class U2fInitPacket : public U2fPacket { |
56 public: | 55 public: |
57 U2fInitPacket(uint32_t channel_id, | 56 U2fInitPacket(uint32_t channel_id, |
58 uint8_t cmd, | 57 uint8_t cmd, |
59 const std::vector<uint8_t> data, | 58 const std::vector<uint8_t> data, |
60 uint16_t payload_length); | 59 uint16_t payload_length); |
| 60 U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, |
| 61 size_t* remaining_size); |
| 62 ~U2fInitPacket() final; |
61 | 63 |
62 // Creates a packet from the serialized data of an initialization packet. As | 64 // 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 | 65 // 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 | 66 // included within the serialized data. Remaining size will be returned to |
65 // inform the callee how many additional packets to expect. | 67 // inform the callee how many additional packets to expect. |
66 static scoped_refptr<U2fInitPacket> CreateFromSerializedData( | 68 static std::unique_ptr<U2fInitPacket> CreateFromSerializedData( |
67 scoped_refptr<net::IOBufferWithSize> buf, | 69 scoped_refptr<net::IOBufferWithSize> buf, |
68 size_t* remaining_size); | 70 size_t* remaining_size); |
69 uint8_t command() { return command_; } | 71 uint8_t command() { return command_; } |
70 uint16_t payload_length() { return payload_length_; } | 72 uint16_t payload_length() { return payload_length_; } |
71 | 73 |
72 protected: | |
73 ~U2fInitPacket() final; | |
74 | |
75 private: | 74 private: |
76 U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, | |
77 size_t* remaining_size); | |
78 | |
79 uint8_t command_; | 75 uint8_t command_; |
80 uint16_t payload_length_; | 76 uint16_t payload_length_; |
81 }; | 77 }; |
82 | 78 |
83 // U2fContinuationPacket, based on the U2f Specification consists of a header | 79 // U2fContinuationPacket, based on the U2f Specification consists of a header |
84 // with data that is serialized into an IOBuffer. The channel identifier will | 80 // 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 | 81 // 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 | 82 // packet sequence will be the sequence number of this particular packet, from |
87 // 0x00 to 0x7f. | 83 // 0x00 to 0x7f. |
88 class U2fContinuationPacket : public U2fPacket { | 84 class U2fContinuationPacket : public U2fPacket { |
89 public: | 85 public: |
90 U2fContinuationPacket(const uint32_t channel_id, | 86 U2fContinuationPacket(const uint32_t channel_id, |
91 const uint8_t sequence, | 87 const uint8_t sequence, |
92 std::vector<uint8_t> data); | 88 std::vector<uint8_t> data); |
| 89 U2fContinuationPacket(scoped_refptr<net::IOBufferWithSize> buf, |
| 90 size_t* remaining_size); |
| 91 ~U2fContinuationPacket() final; |
93 | 92 |
94 // Creates a packet from the serialized data of a continuation packet. As an | 93 // Creates a packet from the serialized data of a continuation packet. As an |
95 // U2fInitPacket would have arrived earlier with the total payload size, | 94 // 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 | 95 // the remaining size should be passed to inform the packet of how much data |
97 // to expect. | 96 // to expect. |
98 static scoped_refptr<U2fContinuationPacket> CreateFromSerializedData( | 97 static std::unique_ptr<U2fContinuationPacket> CreateFromSerializedData( |
99 scoped_refptr<net::IOBufferWithSize> buf, | 98 scoped_refptr<net::IOBufferWithSize> buf, |
100 size_t* remaining_size); | 99 size_t* remaining_size); |
101 uint8_t sequence() { return sequence_; } | 100 uint8_t sequence() { return sequence_; } |
102 | 101 |
103 protected: | |
104 ~U2fContinuationPacket() final; | |
105 | |
106 private: | 102 private: |
107 U2fContinuationPacket(scoped_refptr<net::IOBufferWithSize> buf, | |
108 size_t* remaining_size); | |
109 | |
110 uint8_t sequence_; | 103 uint8_t sequence_; |
111 }; | 104 }; |
112 } // namespace device | 105 } // namespace device |
113 | 106 |
114 #endif // DEVICE_U2F_U2F_PACKET_H_ | 107 #endif // DEVICE_U2F_U2F_PACKET_H_ |
OLD | NEW |