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

Side by Side Diff: device/u2f/u2f_message.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
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_message.h"
6 #include "device/u2f/u2f_packet.h"
7
8 namespace device {
9
10 // static
11 scoped_refptr<U2fMessage> U2fMessage::Create(const uint32_t channel_id,
12 const Type type,
13 const std::vector<uint8_t>& data) {
14 if (data.size() > kMaxMessageSize)
15 return nullptr;
16
17 return make_scoped_refptr(new U2fMessage(channel_id, type, data));
18 }
19
20 // static
21 scoped_refptr<U2fMessage> U2fMessage::CreateFromSerializedData(
22 scoped_refptr<net::IOBufferWithSize> buf) {
23 if (buf == nullptr ||
24 static_cast<size_t>(buf->size()) > U2fPacket::kPacketSize ||
25 static_cast<size_t>(buf->size()) < kInitPacketHeader)
26 return nullptr;
27
28 return make_scoped_refptr(new U2fMessage(buf));
29 }
30
31 U2fMessage::U2fMessage(scoped_refptr<net::IOBufferWithSize> buf)
32 : packets_(), remaining_size_(0), channel_id_() {
33 scoped_refptr<U2fInitPacket> init_packet =
34 U2fInitPacket::CreateFromSerializedData(buf, &remaining_size_);
35 channel_id_ = init_packet->GetChannelId();
36
37 packets_.push_back(init_packet);
38 }
39
40 U2fMessage::U2fMessage(const uint32_t channel_id,
41 const Type type,
42 const std::vector<uint8_t>& data)
43 : packets_(), remaining_size_(), channel_id_(channel_id) {
44 remaining_size_ = data.size();
45 uint8_t sequence = 0;
46
47 std::vector<uint8_t>::const_iterator first = data.begin();
48 std::vector<uint8_t>::const_iterator last;
49
50 if (remaining_size_ > kInitPacketDataSize) {
51 last = data.begin() + kInitPacketDataSize;
52 remaining_size_ -= kInitPacketDataSize;
53 } else {
54 last = data.begin() + remaining_size_;
55 remaining_size_ = 0;
56 }
57
58 packets_.push_back(make_scoped_refptr(
59 new U2fInitPacket(channel_id, static_cast<uint8_t>(type),
60 std::vector<uint8_t>(first, last), data.size())));
61
62 while (remaining_size_ > 0) {
63 first = last;
64 if (remaining_size_ > kContinuationPacketDataSize) {
65 last = first + kContinuationPacketDataSize;
66 remaining_size_ -= kContinuationPacketDataSize;
67 } else {
68 last = first + remaining_size_;
69 remaining_size_ = 0;
70 }
71
72 packets_.push_back(make_scoped_refptr(new U2fContinuationPacket(
73 channel_id, sequence, std::vector<uint8_t>(first, last))));
74 sequence++;
75 }
76 }
77
78 U2fMessage::~U2fMessage() {}
79
80 std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::begin() {
81 return packets_.cbegin();
82 }
83
84 std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::end() {
85 return packets_.cend();
86 }
87
88 uint32_t U2fMessage::GetChannelId() {
89 return channel_id_;
90 }
91
92 scoped_refptr<net::IOBufferWithSize> U2fMessage::PopNextPacket() {
93 if (NumPackets() > 0) {
94 scoped_refptr<net::IOBufferWithSize> buf =
95 packets_.front()->GetSerializedBuffer();
96 packets_.pop_front();
97 return buf;
98 }
99 return nullptr;
100 }
101
102 bool U2fMessage::AddContinuationPacket(
103 scoped_refptr<net::IOBufferWithSize> buf) {
104 size_t remaining_size = remaining_size_;
105 scoped_refptr<U2fContinuationPacket> cont_packet =
106 U2fContinuationPacket::CreateFromSerializedData(buf, &remaining_size_);
107
108 // Reject packets with a different channel id
109 if (channel_id_ != cont_packet->GetChannelId()) {
110 remaining_size_ = remaining_size;
111 return false;
112 }
113
114 packets_.push_back(cont_packet);
115 return true;
116 }
117
118 bool U2fMessage::MessageComplete() {
119 return remaining_size_ == 0;
Reilly Grant (use Gerrit) 2016/12/09 01:19:43 This method is never called and it seems dubious t
Casey Piper 2016/12/09 18:56:50 This method is intended to use when constructing a
120 }
121
122 const std::vector<uint8_t> U2fMessage::GetMessagePayload() {
123 std::vector<uint8_t> data;
124
125 for (const auto& packet : packets_) {
126 std::vector<uint8_t> packet_data = packet->GetPacketPayload();
127
Reilly Grant (use Gerrit) 2016/12/09 01:19:43 nit: this blank line isn't really necessary.
Casey Piper 2016/12/09 18:56:50 Acknowledged.
128 data.insert(std::end(data), packet_data.cbegin(), packet_data.cend());
129 }
130
131 return data;
132 }
133
134 size_t U2fMessage::NumPackets() {
135 return packets_.size();
136 }
137
138 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698