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

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

Issue 2502103002: Add FIDO U2F message and packet classes (Closed)
Patch Set: Adding constructors and fix issues when creating messages and packets from IOBuffers 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,
10 const uint8_t channel_id[kChannelIdSize])
11 : data_(data) {
12 for (size_t index = 0; index < kChannelIdSize; ++index)
13 channel_id_[index] = channel_id[index];
14 }
15
16 U2fPacket::U2fPacket() : data_() {}
17
18 U2fPacket::~U2fPacket() {}
19
20 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetBuffer() {
21 if (serialized_)
22 return serialized_;
23 else
24 return make_scoped_refptr(new net::IOBufferWithSize(0));
25 }
26
27 const std::vector<uint8_t> U2fPacket::GetData() {
28 return data_;
29 }
30
31 uint8_t* U2fPacket::GetChannelId() {
32 return channel_id_;
33 }
34
35 uint16_t U2fInitPacket::GetPayloadLength() {
36 return payload_length_;
37 }
38
39 // U2F Initialization packet is defined as:
40 // Offset Length
41 // 0 4 Channel ID
42 // 4 1 Command ID
43 // 5 1 High order packet payload size
44 // 6 1 Low order packet payload size
45 // 7 (s-7) Payload data
46 U2fInitPacket::U2fInitPacket(const uint8_t channel_id[kChannelIdSize],
47 const uint8_t cmd,
48 const std::vector<uint8_t> data,
49 const uint16_t payload_length)
50 : U2fPacket(data, channel_id), command_(cmd), payload_length_() {
51 serialized_ = new net::IOBufferWithSize(kPacketSize);
52 size_t index = 0;
53 serialized_->data()[index++] = 0;
54 for (size_t i = 0; i < kChannelIdSize; ++i, index++)
55 serialized_->data()[index] = channel_id_[i];
56
57 serialized_->data()[index++] = command_;
58 payload_length_ = payload_length;
59 serialized_->data()[index++] = (payload_length >> 8) & 0xff;
60 serialized_->data()[index++] = payload_length & 0xff;
61 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx)
62 serialized_->data()[index++] = data_.at(data_idx);
63 while (static_cast<int>(index) < serialized_->size())
64 serialized_->data()[index++] = 0;
65 }
66
67 // static
68 scoped_refptr<U2fInitPacket> U2fInitPacket::Create(
69 scoped_refptr<net::IOBufferWithSize> buf,
70 size_t* remaining_size) {
71 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
72 return nullptr;
73
74 return make_scoped_refptr(new U2fInitPacket(buf, remaining_size));
75 }
76
77 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf,
78 size_t* remaining_size)
79 : U2fPacket(), command_(), payload_length_() {
80 size_t index = 1;
81 uint16_t payload_size = 0;
82 uint16_t data_size = 0;
83
84 channel_id_[0] = buf->data()[index++];
85 channel_id_[1] = buf->data()[index++];
86 channel_id_[2] = buf->data()[index++];
87 channel_id_[3] = buf->data()[index++];
88 command_ = buf->data()[index++];
89 payload_size = buf->data()[index++] << 8;
90 payload_size |= static_cast<uint8_t>(buf->data()[index++]);
91 payload_length_ = payload_size;
92
93 // Check to see if payload is less than maximum size and padded with 0s
94 data_size =
95 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index));
96 // Update remaining size to determine the payload size of follow on packets
97 *remaining_size = payload_size - data_size;
98
99 data_.insert(data_.end(), &buf->data()[index],
100 &buf->data()[index + data_size]);
101 serialized_ = buf;
102 }
103
104 uint8_t U2fInitPacket::GetCommand() {
105 return command_;
106 }
107
108 U2fInitPacket::~U2fInitPacket() {}
109
110 // U2F Continuation packet is defined as:
111 // Offset Length
112 // 0 4 Channel ID
113 // 4 1 Packet sequence 0x00..0x7f
114 // 5 (s-5) Payload data
115 U2fContPacket::U2fContPacket(const uint8_t channel_id[kChannelIdSize],
116 const uint8_t sequence,
117 std::vector<uint8_t> data)
118 : U2fPacket(data, channel_id), sequence_(sequence) {
119 serialized_ = new net::IOBufferWithSize(kPacketSize);
120 size_t index = 0;
121 serialized_->data()[index++] = 0;
122 for (size_t idx = 0; idx < kChannelIdSize; ++idx)
123 serialized_->data()[index++] = channel_id_[idx];
124
125 serialized_->data()[index++] = sequence_;
126 for (size_t idx = 0; idx < data_.size(); ++idx)
127 serialized_->data()[index++] = data_.at(idx);
128
129 while (static_cast<int>(index) < serialized_->size())
130 serialized_->data()[index++] = 0;
131 }
132
133 // static
134 scoped_refptr<U2fContPacket> U2fContPacket::Create(
135 scoped_refptr<net::IOBufferWithSize> buf,
136 size_t* remaining_size) {
137 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize)
138 return nullptr;
139
140 return make_scoped_refptr(new U2fContPacket(buf, remaining_size));
141 }
142
143 U2fContPacket::U2fContPacket(scoped_refptr<net::IOBufferWithSize> buf,
144 size_t* remaining_size)
145 : U2fPacket() {
146 size_t index = 1, data_size;
147
148 channel_id_[0] = buf->data()[index++];
149 channel_id_[1] = buf->data()[index++];
150 channel_id_[2] = buf->data()[index++];
151 channel_id_[3] = buf->data()[index++];
152 sequence_ = buf->data()[index++];
153
154 data_size = std::min(*remaining_size, kPacketSize - index);
155 *remaining_size -= data_size;
156 data_.insert(std::end(data_), &buf->data()[index],
157 &buf->data()[index + data_size]);
158
159 // Incoming buffer may not be padded with 0's, so manually update buffer
160 for (int i = index + data_size; i < buf->size(); ++i) {
161 buf->data()[i] = 0;
162 }
163 serialized_ = buf;
164 }
165
166 uint8_t U2fContPacket::GetSequence() {
167 return sequence_;
168 }
169
170 U2fContPacket::~U2fContPacket() {}
171 } // 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