Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: device/u2f/u2f_packet.cc

Issue 2502103002: Add FIDO U2F message and packet classes (Closed)
Patch Set: Add basic fuzzer test, and updated constructor for U2fMessage to account for invalid packets Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« device/u2f/u2f_message_unittest.cc ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "device/u2f/u2f_packet.h"
6
7 namespace device {
8
9 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id)
10 : data_(data), channel_id_(channel_id) {}
11
12 U2fPacket::U2fPacket() : data_(), channel_id_() {}
Reilly Grant (use Gerrit) 2016/12/10 00:02:44 nit: These initializers are unnecessary.
Casey Piper 2016/12/10 01:38:03 Done.
13
14 U2fPacket::~U2fPacket() {}
15
16 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() {
17 if (serialized_)
18 return serialized_;
19 else
20 return make_scoped_refptr(new net::IOBufferWithSize(0));
21 }
22
23 std::vector<uint8_t> U2fPacket::GetPacketPayload() const {
24 return data_;
25 }
26
27 // U2F Initialization packet is defined as:
28 // Offset Length
29 // 0 4 Channel ID
30 // 4 1 Command ID
31 // 5 1 High order packet payload size
32 // 6 1 Low order packet payload size
33 // 7 (s-7) Payload data
34 U2fInitPacket::U2fInitPacket(const uint32_t channel_id,
35 const uint8_t cmd,
36 const std::vector<uint8_t> data,
37 const uint16_t payload_length)
38 : U2fPacket(data, channel_id), command_(cmd), payload_length_() {
Reilly Grant (use Gerrit) 2016/12/10 00:02:44 nit: payload_length_() is unnecessary.
Casey Piper 2016/12/10 01:38:03 Done.
39 serialized_ = new net::IOBufferWithSize(kPacketSize);
40 size_t index = 0;
41 // Byte at offset 0 is the report ID, which is always 0
42 serialized_->data()[index++] = 0;
43
44 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
45 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
46 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
47 serialized_->data()[index++] = channel_id_ & 0xff;
48
49 serialized_->data()[index++] = command_;
50 payload_length_ = payload_length;
51 serialized_->data()[index++] = (payload_length >> 8) & 0xff;
52 serialized_->data()[index++] = payload_length & 0xff;
53 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx)
54 serialized_->data()[index++] = data_.at(data_idx);
55 while (static_cast<int>(index) < serialized_->size())
56 serialized_->data()[index++] = 0;
57 }
58
59 // static
60 scoped_refptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData(
61 scoped_refptr<net::IOBufferWithSize> buf,
62 size_t* remaining_size) {
63 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
64 return nullptr;
65
66 return make_scoped_refptr(new U2fInitPacket(buf, remaining_size));
67 }
68
69 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf,
70 size_t* remaining_size)
71 : U2fPacket(), command_(), payload_length_() {
Reilly Grant (use Gerrit) 2016/12/10 00:02:44 nit: None of these initializers are necessary.
Casey Piper 2016/12/10 01:38:03 Done.
72 // Report ID is at index 0, so start at index 1 for channel ID
73 size_t index = 1;
74 uint16_t payload_size = 0;
75 uint16_t data_size = 0;
76
77 channel_id_ = (buf->data()[index++] & 0xff) << 24;
78 channel_id_ |= (buf->data()[index++] & 0xff) << 16;
79 channel_id_ |= (buf->data()[index++] & 0xff) << 8;
80 channel_id_ |= buf->data()[index++] & 0xff;
81 command_ = buf->data()[index++];
82 payload_size = buf->data()[index++] << 8;
83 payload_size |= static_cast<uint8_t>(buf->data()[index++]);
84 payload_length_ = payload_size;
85
86 // Check to see if payload is less than maximum size and padded with 0s
87 data_size =
88 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index));
89 // Update remaining size to determine the payload size of follow on packets
90 *remaining_size = payload_size - data_size;
91
92 data_.insert(data_.end(), &buf->data()[index],
93 &buf->data()[index + data_size]);
94
95 for (int i = index + data_size; i < buf->size(); ++i)
96 buf->data()[i] = 0;
97 serialized_ = buf;
98 }
99
100 U2fInitPacket::~U2fInitPacket() {}
101
102 // U2F Continuation packet is defined as:
103 // Offset Length
104 // 0 4 Channel ID
105 // 4 1 Packet sequence 0x00..0x7f
106 // 5 (s-5) Payload data
107 U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id,
108 const uint8_t sequence,
109 std::vector<uint8_t> data)
110 : U2fPacket(data, channel_id), sequence_(sequence) {
111 serialized_ = new net::IOBufferWithSize(kPacketSize);
112 size_t index = 0;
113 // Byte at offset 0 is the report ID, which is always 0
114 serialized_->data()[index++] = 0;
115
116 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
117 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
118 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
119 serialized_->data()[index++] = channel_id_ & 0xff;
120
121 serialized_->data()[index++] = sequence_;
122 for (size_t idx = 0; idx < data_.size(); ++idx)
123 serialized_->data()[index++] = data_.at(idx);
124
125 while (static_cast<int>(index) < serialized_->size())
126 serialized_->data()[index++] = 0;
127 }
128
129 // static
130 scoped_refptr<U2fContinuationPacket>
131 U2fContinuationPacket::CreateFromSerializedData(
132 scoped_refptr<net::IOBufferWithSize> buf,
133 size_t* remaining_size) {
134 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
135 return nullptr;
136
137 return make_scoped_refptr(new U2fContinuationPacket(buf, remaining_size));
138 }
139
140 U2fContinuationPacket::U2fContinuationPacket(
141 scoped_refptr<net::IOBufferWithSize> buf,
142 size_t* remaining_size)
143 : U2fPacket() {
Reilly Grant (use Gerrit) 2016/12/10 00:02:44 nit: Unnecessary initializer.
Casey Piper 2016/12/10 01:38:03 Done.
144 // Report ID is at index 0, so start at index 1 for channel ID
145 size_t index = 1;
146 size_t data_size;
147
148 channel_id_ = (buf->data()[index++] & 0xff) << 24;
149 channel_id_ |= (buf->data()[index++] & 0xff) << 16;
150 channel_id_ |= (buf->data()[index++] & 0xff) << 8;
151 channel_id_ |= buf->data()[index++] & 0xff;
152 sequence_ = buf->data()[index++];
153
154 // Check to see if packet payload is less than maximum size and padded with 0s
155 data_size = std::min(*remaining_size, kPacketSize - index);
156 *remaining_size -= data_size;
157 data_.insert(std::end(data_), &buf->data()[index],
158 &buf->data()[index + data_size]);
159
160 // Incoming buffer may not be padded with 0's, so manually update buffer
161 for (int i = index + data_size; i < buf->size(); ++i)
162 buf->data()[i] = 0;
163 serialized_ = buf;
164 }
165
166 U2fContinuationPacket::~U2fContinuationPacket() {}
167 } // namespace device
OLDNEW
« device/u2f/u2f_message_unittest.cc ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698