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 #include "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
6 #include "net/base/io_buffer.h" | 6 #include "net/base/io_buffer.h" |
7 | 7 |
8 #include "u2f_packet.h" | 8 #include "u2f_packet.h" |
9 | 9 |
10 namespace device { | 10 namespace device { |
11 | 11 |
12 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id) | 12 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id) |
Reilly Grant (use Gerrit)
2017/03/22 23:08:17
const std::vector<uint8_t>&
The other parameters
Casey Piper
2017/03/23 03:47:24
Done.
| |
13 : data_(data), channel_id_(channel_id) {} | 13 : data_(data), channel_id_(channel_id) {} |
14 | 14 |
15 U2fPacket::U2fPacket() {} | 15 U2fPacket::U2fPacket() {} |
16 | 16 |
17 U2fPacket::~U2fPacket() {} | 17 U2fPacket::~U2fPacket() {} |
18 | 18 |
19 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() { | |
20 if (serialized_) | |
21 return serialized_; | |
22 else | |
23 return make_scoped_refptr(new net::IOBufferWithSize(0)); | |
24 } | |
25 | |
26 std::vector<uint8_t> U2fPacket::GetPacketPayload() const { | 19 std::vector<uint8_t> U2fPacket::GetPacketPayload() const { |
27 return data_; | 20 return data_; |
28 } | 21 } |
29 | 22 |
30 // U2F Initialization packet is defined as: | 23 // U2F Initialization packet is defined as: |
31 // Offset Length | 24 // Offset Length |
32 // 0 4 Channel ID | 25 // 0 4 Channel ID |
33 // 4 1 Command ID | 26 // 4 1 Command ID |
34 // 5 1 High order packet payload size | 27 // 5 1 High order packet payload size |
35 // 6 1 Low order packet payload size | 28 // 6 1 Low order packet payload size |
36 // 7 (s-7) Payload data | 29 // 7 (s-7) Payload data |
37 U2fInitPacket::U2fInitPacket(const uint32_t channel_id, | 30 U2fInitPacket::U2fInitPacket(const uint32_t channel_id, |
38 const uint8_t cmd, | 31 const uint8_t cmd, |
39 const std::vector<uint8_t> data, | 32 const std::vector<uint8_t> data, |
Reilly Grant (use Gerrit)
2017/03/22 23:08:17
const std::vector<uint8_t>&
The other parameters
Casey Piper
2017/03/23 03:47:25
Done.
| |
40 const uint16_t payload_length) | 33 const uint16_t payload_length) |
41 : U2fPacket(data, channel_id), command_(cmd) { | 34 : U2fPacket(data, channel_id), |
42 serialized_ = new net::IOBufferWithSize(kPacketSize); | 35 command_(cmd), |
43 size_t index = 0; | 36 payload_length_(payload_length) {} |
37 | |
38 std::vector<uint8_t> U2fInitPacket::GetSerializedData() { | |
39 std::vector<uint8_t> serialized(kPacketSize); | |
44 // Byte at offset 0 is the report ID, which is always 0 | 40 // Byte at offset 0 is the report ID, which is always 0 |
45 serialized_->data()[index++] = 0; | 41 size_t index = 1; |
42 serialized[index++] = (channel_id_ >> 24) & 0xff; | |
43 serialized[index++] = (channel_id_ >> 16) & 0xff; | |
44 serialized[index++] = (channel_id_ >> 8) & 0xff; | |
45 serialized[index++] = channel_id_ & 0xff; | |
46 | 46 |
47 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff; | 47 serialized[index++] = command_; |
48 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff; | 48 serialized[index++] = (payload_length_ >> 8) & 0xff; |
49 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff; | 49 serialized[index++] = payload_length_ & 0xff; |
50 serialized_->data()[index++] = channel_id_ & 0xff; | |
51 | |
52 serialized_->data()[index++] = command_; | |
53 payload_length_ = payload_length; | |
54 serialized_->data()[index++] = (payload_length >> 8) & 0xff; | |
55 serialized_->data()[index++] = payload_length & 0xff; | |
56 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) | 50 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) |
57 serialized_->data()[index++] = data_.at(data_idx); | 51 serialized[index++] = data_[data_idx]; |
58 while (static_cast<int>(index) < serialized_->size()) | 52 return serialized; |
59 serialized_->data()[index++] = 0; | |
60 } | 53 } |
61 | 54 |
62 // static | 55 // static |
63 std::unique_ptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData( | 56 std::unique_ptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData( |
64 scoped_refptr<net::IOBufferWithSize> buf, | 57 std::vector<uint8_t> serialized, |
65 size_t* remaining_size) { | 58 size_t* remaining_size) { |
66 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) | 59 if (remaining_size == nullptr || serialized.size() != kPacketSize) |
67 return nullptr; | 60 return nullptr; |
68 | 61 |
69 return base::MakeUnique<U2fInitPacket>(buf, remaining_size); | 62 return base::MakeUnique<U2fInitPacket>(serialized, remaining_size); |
70 } | 63 } |
71 | 64 |
72 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, | 65 U2fInitPacket::U2fInitPacket(std::vector<uint8_t> serialized, |
73 size_t* remaining_size) { | 66 size_t* remaining_size) { |
74 // Report ID is at index 0, so start at index 1 for channel ID | 67 // Report ID is at index 0, so start at index 1 for channel ID |
75 size_t index = 1; | 68 size_t index = 1; |
76 uint16_t payload_size = 0; | 69 uint16_t payload_size = 0; |
77 uint16_t data_size = 0; | |
78 | 70 |
79 channel_id_ = (buf->data()[index++] & 0xff) << 24; | 71 channel_id_ = (serialized[index++] & 0xff) << 24; |
80 channel_id_ |= (buf->data()[index++] & 0xff) << 16; | 72 channel_id_ |= (serialized[index++] & 0xff) << 16; |
81 channel_id_ |= (buf->data()[index++] & 0xff) << 8; | 73 channel_id_ |= (serialized[index++] & 0xff) << 8; |
82 channel_id_ |= buf->data()[index++] & 0xff; | 74 channel_id_ |= serialized[index++] & 0xff; |
83 command_ = buf->data()[index++]; | 75 command_ = serialized[index++]; |
84 payload_size = buf->data()[index++] << 8; | 76 payload_size = serialized[index++] << 8; |
85 payload_size |= static_cast<uint8_t>(buf->data()[index++]); | 77 payload_size |= static_cast<uint8_t>(serialized[index++]); |
86 payload_length_ = payload_size; | 78 payload_length_ = payload_size; |
87 | 79 |
88 // Check to see if payload is less than maximum size and padded with 0s | 80 // Check to see if payload is less than maximum size and padded with 0s |
89 data_size = | 81 uint16_t data_size = |
90 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index)); | 82 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index)); |
91 // Update remaining size to determine the payload size of follow on packets | 83 // Update remaining size to determine the payload size of follow on packets |
92 *remaining_size = payload_size - data_size; | 84 *remaining_size = payload_size - data_size; |
93 | 85 |
94 data_.insert(data_.end(), &buf->data()[index], | 86 data_.insert(data_.end(), serialized.begin() + index, |
95 &buf->data()[index + data_size]); | 87 serialized.begin() + index + data_size); |
96 | |
97 for (int i = index + data_size; i < buf->size(); ++i) | |
98 buf->data()[i] = 0; | |
99 serialized_ = buf; | |
100 } | 88 } |
101 | 89 |
102 U2fInitPacket::~U2fInitPacket() {} | 90 U2fInitPacket::~U2fInitPacket() {} |
103 | 91 |
104 // U2F Continuation packet is defined as: | 92 // U2F Continuation packet is defined as: |
105 // Offset Length | 93 // Offset Length |
106 // 0 4 Channel ID | 94 // 0 4 Channel ID |
107 // 4 1 Packet sequence 0x00..0x7f | 95 // 4 1 Packet sequence 0x00..0x7f |
108 // 5 (s-5) Payload data | 96 // 5 (s-5) Payload data |
109 U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id, | 97 U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id, |
110 const uint8_t sequence, | 98 const uint8_t sequence, |
111 std::vector<uint8_t> data) | 99 std::vector<uint8_t> data) |
112 : U2fPacket(data, channel_id), sequence_(sequence) { | 100 : U2fPacket(data, channel_id), sequence_(sequence) {} |
113 serialized_ = new net::IOBufferWithSize(kPacketSize); | 101 |
114 size_t index = 0; | 102 std::vector<uint8_t> U2fContinuationPacket::GetSerializedData() { |
103 std::vector<uint8_t> serialized(kPacketSize); | |
115 // Byte at offset 0 is the report ID, which is always 0 | 104 // Byte at offset 0 is the report ID, which is always 0 |
116 serialized_->data()[index++] = 0; | 105 size_t index = 1; |
106 serialized[index++] = (channel_id_ >> 24) & 0xff; | |
107 serialized[index++] = (channel_id_ >> 16) & 0xff; | |
108 serialized[index++] = (channel_id_ >> 8) & 0xff; | |
109 serialized[index++] = channel_id_ & 0xff; | |
117 | 110 |
118 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff; | 111 serialized[index++] = sequence_; |
119 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff; | |
120 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff; | |
121 serialized_->data()[index++] = channel_id_ & 0xff; | |
122 | |
123 serialized_->data()[index++] = sequence_; | |
124 for (size_t idx = 0; idx < data_.size(); ++idx) | 112 for (size_t idx = 0; idx < data_.size(); ++idx) |
125 serialized_->data()[index++] = data_.at(idx); | 113 serialized[index++] = data_[idx]; |
126 | 114 return serialized; |
127 while (static_cast<int>(index) < serialized_->size()) | |
128 serialized_->data()[index++] = 0; | |
129 } | 115 } |
130 | 116 |
131 // static | 117 // static |
132 std::unique_ptr<U2fContinuationPacket> | 118 std::unique_ptr<U2fContinuationPacket> |
133 U2fContinuationPacket::CreateFromSerializedData( | 119 U2fContinuationPacket::CreateFromSerializedData(std::vector<uint8_t> serialized, |
134 scoped_refptr<net::IOBufferWithSize> buf, | 120 size_t* remaining_size) { |
135 size_t* remaining_size) { | 121 if (remaining_size == nullptr || serialized.size() != kPacketSize) |
136 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) | |
137 return nullptr; | 122 return nullptr; |
138 | 123 |
139 return base::MakeUnique<U2fContinuationPacket>(buf, remaining_size); | 124 return base::MakeUnique<U2fContinuationPacket>(serialized, remaining_size); |
140 } | 125 } |
141 | 126 |
142 U2fContinuationPacket::U2fContinuationPacket( | 127 U2fContinuationPacket::U2fContinuationPacket(std::vector<uint8_t> serialized, |
143 scoped_refptr<net::IOBufferWithSize> buf, | 128 size_t* remaining_size) { |
144 size_t* remaining_size) { | |
145 // Report ID is at index 0, so start at index 1 for channel ID | 129 // Report ID is at index 0, so start at index 1 for channel ID |
146 size_t index = 1; | 130 size_t index = 1; |
147 size_t data_size; | 131 size_t data_size; |
148 | 132 |
149 channel_id_ = (buf->data()[index++] & 0xff) << 24; | 133 channel_id_ = (serialized[index++] & 0xff) << 24; |
150 channel_id_ |= (buf->data()[index++] & 0xff) << 16; | 134 channel_id_ |= (serialized[index++] & 0xff) << 16; |
151 channel_id_ |= (buf->data()[index++] & 0xff) << 8; | 135 channel_id_ |= (serialized[index++] & 0xff) << 8; |
152 channel_id_ |= buf->data()[index++] & 0xff; | 136 channel_id_ |= serialized[index++] & 0xff; |
153 sequence_ = buf->data()[index++]; | 137 sequence_ = serialized[index++]; |
154 | 138 |
155 // Check to see if packet payload is less than maximum size and padded with 0s | 139 // Check to see if packet payload is less than maximum size and padded with 0s |
156 data_size = std::min(*remaining_size, kPacketSize - index); | 140 data_size = std::min(*remaining_size, kPacketSize - index); |
157 *remaining_size -= data_size; | 141 *remaining_size -= data_size; |
158 data_.insert(std::end(data_), &buf->data()[index], | 142 data_.insert(data_.end(), serialized.begin() + index, |
159 &buf->data()[index + data_size]); | 143 serialized.begin() + index + data_size); |
160 | |
161 // Incoming buffer may not be padded with 0's, so manually update buffer | |
162 for (int i = index + data_size; i < buf->size(); ++i) | |
163 buf->data()[i] = 0; | |
164 serialized_ = buf; | |
165 } | 144 } |
166 | 145 |
167 U2fContinuationPacket::~U2fContinuationPacket() {} | 146 U2fContinuationPacket::~U2fContinuationPacket() {} |
168 | 147 |
169 } // namespace device | 148 } // namespace device |
OLD | NEW |