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

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

Issue 2502103002: Add FIDO U2F message and packet classes (Closed)
Patch Set: Change channel id to uint32_t from uint8_t[4] and fix additional review comments 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_packet.h ('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_() {}
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 const std::vector<uint8_t> U2fPacket::GetPacketPayload() {
24 return data_;
25 }
26
27 uint32_t U2fPacket::GetChannelId() {
28 return channel_id_;
29 }
30
31 uint16_t U2fInitPacket::GetPayloadLength() {
32 return payload_length_;
33 }
34
35 // U2F Initialization packet is defined as:
36 // Offset Length
37 // 0 4 Channel ID
38 // 4 1 Command ID
39 // 5 1 High order packet payload size
40 // 6 1 Low order packet payload size
41 // 7 (s-7) Payload data
42 U2fInitPacket::U2fInitPacket(const uint32_t channel_id,
43 const uint8_t cmd,
44 const std::vector<uint8_t> data,
45 const uint16_t payload_length)
46 : U2fPacket(data, channel_id), command_(cmd), payload_length_() {
47 serialized_ = new net::IOBufferWithSize(kPacketSize);
48 size_t index = 0;
49 serialized_->data()[index++] = 0;
Reilly Grant (use Gerrit) 2016/12/09 01:19:43 Add a comment about why the byte at offset 0 is al
Casey Piper 2016/12/09 18:56:50 Done.
50
51 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
52 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
53 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
54 serialized_->data()[index++] = channel_id_ & 0xff;
55
56 serialized_->data()[index++] = command_;
57 payload_length_ = payload_length;
58 serialized_->data()[index++] = (payload_length >> 8) & 0xff;
59 serialized_->data()[index++] = payload_length & 0xff;
60 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx)
61 serialized_->data()[index++] = data_.at(data_idx);
62 while (static_cast<int>(index) < serialized_->size())
63 serialized_->data()[index++] = 0;
64 }
65
66 // static
67 scoped_refptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData(
68 scoped_refptr<net::IOBufferWithSize> buf,
69 size_t* remaining_size) {
70 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
71 return nullptr;
72
73 return make_scoped_refptr(new U2fInitPacket(buf, remaining_size));
74 }
75
76 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf,
77 size_t* remaining_size)
78 : U2fPacket(), command_(), payload_length_() {
79 size_t index = 1;
Reilly Grant (use Gerrit) 2016/12/09 01:19:43 Again, note about the report ID.
Casey Piper 2016/12/09 18:56:50 Done.
80 uint16_t payload_size = 0;
81 uint16_t data_size = 0;
82
83 channel_id_ = (buf->data()[index++] & 0xff) << 24;
84 channel_id_ |= (buf->data()[index++] & 0xff) << 16;
85 channel_id_ |= (buf->data()[index++] & 0xff) << 8;
86 channel_id_ |= buf->data()[index++] & 0xff;
87 command_ = buf->data()[index++];
88 payload_size = buf->data()[index++] << 8;
89 payload_size |= static_cast<uint8_t>(buf->data()[index++]);
90 payload_length_ = payload_size;
91
92 // Check to see if payload is less than maximum size and padded with 0s
93 data_size =
94 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index));
95 // Update remaining size to determine the payload size of follow on packets
96 *remaining_size = payload_size - data_size;
97
98 data_.insert(data_.end(), &buf->data()[index],
99 &buf->data()[index + data_size]);
100
101 for (int i = index + data_size; i < buf->size(); ++i)
102 buf->data()[i] = 0;
103 serialized_ = buf;
104 }
105
106 uint8_t U2fInitPacket::GetCommand() {
107 return command_;
108 }
109
110 U2fInitPacket::~U2fInitPacket() {}
111
112 // U2F Continuation packet is defined as:
113 // Offset Length
114 // 0 4 Channel ID
115 // 4 1 Packet sequence 0x00..0x7f
116 // 5 (s-5) Payload data
117 U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id,
118 const uint8_t sequence,
119 std::vector<uint8_t> data)
120 : U2fPacket(data, channel_id), sequence_(sequence) {
121 serialized_ = new net::IOBufferWithSize(kPacketSize);
122 size_t index = 0;
123 serialized_->data()[index++] = 0;
124
125 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff;
126 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff;
127 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff;
128 serialized_->data()[index++] = channel_id_ & 0xff;
129
130 serialized_->data()[index++] = sequence_;
131 for (size_t idx = 0; idx < data_.size(); ++idx)
132 serialized_->data()[index++] = data_.at(idx);
133
134 while (static_cast<int>(index) < serialized_->size())
135 serialized_->data()[index++] = 0;
136 }
137
138 // static
139 scoped_refptr<U2fContinuationPacket>
140 U2fContinuationPacket::CreateFromSerializedData(
141 scoped_refptr<net::IOBufferWithSize> buf,
142 size_t* remaining_size) {
143 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
144 return nullptr;
145
146 return make_scoped_refptr(new U2fContinuationPacket(buf, remaining_size));
147 }
148
149 U2fContinuationPacket::U2fContinuationPacket(
150 scoped_refptr<net::IOBufferWithSize> buf,
151 size_t* remaining_size)
152 : U2fPacket() {
153 size_t index = 1, data_size;
154
155 channel_id_ = (buf->data()[index++] & 0xff) << 24;
156 channel_id_ |= (buf->data()[index++] & 0xff) << 16;
157 channel_id_ |= (buf->data()[index++] & 0xff) << 8;
158 channel_id_ |= buf->data()[index++] & 0xff;
159 sequence_ = buf->data()[index++];
160
161 data_size = std::min(*remaining_size, kPacketSize - index);
162 *remaining_size -= data_size;
163 data_.insert(std::end(data_), &buf->data()[index],
164 &buf->data()[index + data_size]);
165
166 // Incoming buffer may not be padded with 0's, so manually update buffer
167 for (int i = index + data_size; i < buf->size(); ++i)
168 buf->data()[i] = 0;
169 serialized_ = buf;
170 }
171
172 uint8_t U2fContinuationPacket::GetSequence() {
173 return sequence_;
174 }
175
176 U2fContinuationPacket::~U2fContinuationPacket() {}
177 } // namespace device
OLDNEW
« device/u2f/u2f_packet.h ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698