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