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

Unified Diff: device/u2f/u2f_packet.cc

Issue 2502103002: Add FIDO U2F message and packet classes (Closed)
Patch Set: Add FIDO U2F message and packet classes Created 4 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« device/u2f/u2f_packet.h ('K') | « device/u2f/u2f_packet.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/u2f/u2f_packet.cc
diff --git a/device/u2f/u2f_packet.cc b/device/u2f/u2f_packet.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4ce6397e28f316d943d5d79b58c2d1aaab8e186a
--- /dev/null
+++ b/device/u2f/u2f_packet.cc
@@ -0,0 +1,110 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <iostream>
+#include "device/u2f/u2f_packet.h"
+
+namespace device {
+
+U2fPacket::U2fPacket(const std::vector<uint8_t> data,
+ const uint8_t channel_id[kChannelIdSize])
+ : data_(data) {
+ for (size_t index = 0; index < kChannelIdSize; ++index)
+ channel_id_[index] = channel_id[index];
+}
+
+U2fPacket::U2fPacket() : data_() {}
+
+U2fPacket::~U2fPacket() {}
+
+scoped_refptr<net::IOBufferWithSize> U2fPacket::GetBuffer() {
+ if (serialized_)
+ return serialized_;
+ else
+ return make_scoped_refptr(new net::IOBufferWithSize(0));
+}
+
+// U2F Initialization packet is defined as:
+// Offset Length
+// 0 4 Channel ID
+// 4 1 Command ID
+// 5 1 High order packet payload size
+// 6 1 Low order packet payload size
+// 7 (s-7) Payload data
+U2fInitPacket::U2fInitPacket(const uint8_t channel_id[kChannelIdSize],
+ const uint8_t cmd,
+ const std::vector<uint8_t> data,
+ const uint16_t payload_length)
+ : U2fPacket(data, channel_id), command_(cmd) {
+ serialized_ = new net::IOBufferWithSize(kPacketSize);
+ size_t index = 0;
+ serialized_->data()[index++] = 0;
+ for (size_t i = 0; i < kChannelIdSize; ++i, index++)
+ serialized_->data()[index] = channel_id_[i];
+
+ serialized_->data()[index++] = command_;
+
+ serialized_->data()[index++] = (payload_length >> 8) & 0xff;
+ serialized_->data()[index++] = payload_length & 0xff;
+ for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx)
+ serialized_->data()[index++] = data_.at(data_idx);
+ while ((int)index < serialized_->size())
Reilly Grant (use Gerrit) 2016/11/30 20:53:47 static_cast<int>
Casey Piper 2016/11/30 22:05:46 Done.
+ serialized_->data()[index++] = 0;
+}
+
+scoped_refptr<U2fInitPacket> U2fInitPacket::Create(
+ scoped_refptr<net::IOBufferWithSize> buf) {
+ if (!buf || buf->size() != kPacketSize)
+ return nullptr;
+
+ return make_scoped_refptr(new U2fInitPacket(buf));
+}
+
+U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf)
+ : U2fPacket(), command_() {
+ size_t index = 1;
+ uint16_t payload_size = 0;
+
+ channel_id_[0] = buf->data()[index++];
+ channel_id_[1] = buf->data()[index++];
+ channel_id_[2] = buf->data()[index++];
+ channel_id_[3] = buf->data()[index++];
+ command_ = buf->data()[index++];
+ payload_size = ((uint16_t)buf->data()[index++]) << 8;
+ payload_size |= buf->data()[index++];
+ // Check to see if payload is smaller than maximum size
+ payload_size = (payload_size + index > kPacketSize) ? (kPacketSize - index)
+ : payload_size;
Reilly Grant (use Gerrit) 2016/11/30 20:53:47 payload_size = std::min(payload_size, kPacketSize
Casey Piper 2016/11/30 22:05:46 Yeah, it was poorly written. To clarify, for paylo
+
+ data_.insert(std::end(data_), buf->data()[index],
+ buf->data()[index + payload_size]);
+}
+
+U2fInitPacket::~U2fInitPacket() {}
+
+// U2F Continuation packet is defined as:
+// Offset Length
+// 0 4 Channel ID
+// 4 1 Packet sequence 0x00..0x7f
+// 5 (s-5) Payload data
+U2fContPacket::U2fContPacket(const uint8_t channel_id[kChannelIdSize],
+ const uint8_t sequence,
+ std::vector<uint8_t> data)
+ : U2fPacket(data, channel_id), sequence_(sequence) {
+ serialized_ = new net::IOBufferWithSize(kPacketSize);
+ size_t index = 0;
+ serialized_->data()[index++] = 0;
+ for (size_t idx = 0; idx < kChannelIdSize; ++idx)
+ serialized_->data()[index++] = channel_id_[idx];
+
+ serialized_->data()[index++] = sequence_;
+ for (size_t idx = 0; idx < data_.size(); ++idx)
+ serialized_->data()[index++] = data_.at(idx);
+
+ while ((int)index < serialized_->size())
Reilly Grant (use Gerrit) 2016/11/30 20:53:47 static_cast<int>
Casey Piper 2016/11/30 22:05:46 Acknowledged.
+ serialized_->data()[index++] = 0;
+}
+
+U2fContPacket::~U2fContPacket() {}
+} // namespace device
« 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