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

Side by Side Diff: device/u2f/u2f_message.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
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(
12 const uint8_t channel_id[U2fPacket::kChannelIdSize],
13 const Type type,
14 const std::vector<uint8_t>& data) {
15 if (data.size() > kMaxMessageSize)
16 return nullptr;
17
18 return make_scoped_refptr(new U2fMessage(channel_id, type, data));
19 }
20
21 // static
22 scoped_refptr<U2fMessage> U2fMessage::Create(
23 scoped_refptr<net::IOBufferWithSize> buf) {
24 if (buf == nullptr ||
25 static_cast<size_t>(buf->size()) > U2fPacket::kPacketSize ||
26 static_cast<size_t>(buf->size()) < kInitPacketHeader)
27 return nullptr;
28
29 return make_scoped_refptr(new U2fMessage(buf));
30 }
31
32 U2fMessage::U2fMessage(scoped_refptr<net::IOBufferWithSize> buf)
33 : packets_(), remaining_size_(0) {
34 scoped_refptr<U2fInitPacket> init_packet =
35 U2fInitPacket::Create(buf, &remaining_size_);
36 for (size_t i = 0; i < U2fPacket::kChannelIdSize; i++)
37 channel_id_[i] = init_packet->GetChannelId()[i];
38
39 packets_.push_back(init_packet);
40 }
41
42 U2fMessage::U2fMessage(const uint8_t channel_id[U2fPacket::kChannelIdSize],
43 const Type type,
44 const std::vector<uint8_t>& data)
45 : packets_(), remaining_size_() {
46 remaining_size_ = data.size();
47 uint8_t sequence = 0;
48 for (size_t i = 0; i < U2fPacket::kChannelIdSize; i++)
49 channel_id_[i] = channel_id[i];
50
51 std::vector<uint8_t>::const_iterator first = data.begin();
52 std::vector<uint8_t>::const_iterator last;
53
54 if (remaining_size_ > kInitPacketDataSize) {
55 last = data.begin() + kInitPacketDataSize;
56 remaining_size_ -= kInitPacketDataSize;
57 } else {
58 last = data.begin() + remaining_size_;
59 remaining_size_ = 0;
60 }
61
62 packets_.push_back(make_scoped_refptr(
63 new U2fInitPacket(channel_id, static_cast<uint8_t>(type),
64 std::vector<uint8_t>(first, last), data.size())));
65
66 while (remaining_size_ > 0) {
67 first = last;
68 if (remaining_size_ > kContPacketDataSize) {
69 last = first + kContPacketDataSize;
70 remaining_size_ -= kContPacketDataSize;
71 } else {
72 last = first + remaining_size_;
73 remaining_size_ = 0;
74 }
75
76 packets_.push_back(make_scoped_refptr(new U2fContPacket(
77 channel_id, sequence, std::vector<uint8_t>(first, last))));
78 sequence++;
79 }
80 }
81
82 U2fMessage::~U2fMessage() {}
83
84 std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::begin() {
85 return packets_.cbegin();
86 }
87
88 std::list<scoped_refptr<U2fPacket>>::const_iterator U2fMessage::end() {
89 return packets_.cend();
90 }
91
92 const uint8_t* U2fMessage::GetChannelId() {
93 return channel_id_;
94 }
95
96 scoped_refptr<net::IOBufferWithSize> U2fMessage::PopNextPacket() {
97 if (NumPackets() > 0) {
98 scoped_refptr<net::IOBufferWithSize> buf = packets_.front()->GetBuffer();
99 packets_.pop_front();
100 return buf;
101 }
102 return nullptr;
103 }
104
105 bool U2fMessage::AddContPacket(scoped_refptr<net::IOBufferWithSize> buf) {
106 size_t remaining_size = remaining_size_;
107 scoped_refptr<U2fContPacket> cont_packet =
108 U2fContPacket::Create(buf, &remaining_size_);
109
110 // Reject packets with a different channel id
111 if (!std::equal(channel_id_, channel_id_ + U2fPacket::kChannelIdSize,
112 cont_packet->GetChannelId())) {
113 remaining_size_ = remaining_size;
114 return false;
115 }
116
117 packets_.push_back(cont_packet);
118 return true;
119 }
120
121 bool U2fMessage::MessageComplete() {
122 return remaining_size_ == 0;
123 }
124
125 const std::vector<uint8_t> U2fMessage::GetData() {
126 std::vector<uint8_t> data;
127
128 for (std::list<scoped_refptr<U2fPacket>>::const_iterator it = begin();
129 it != end(); ++it) {
Reilly Grant (use Gerrit) 2016/12/07 22:33:48 Since this is internal I would just use for (const
Casey Piper 2016/12/09 00:15:54 Done.
130 std::vector<uint8_t> packet_data = (*it)->GetData();
131
132 data.insert(std::end(data), packet_data.cbegin(), packet_data.cend());
133 }
134
135 return data;
136 }
137
138 size_t U2fMessage::NumPackets() {
139 return packets_.size();
140 }
141
142 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698