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

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

Issue 2502103002: Add FIDO U2F message and packet classes (Closed)
Patch Set: Add includes to u2f_packet 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(uint32_t channel_id,
12 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 size_t remaining_size = 0;
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 scoped_refptr<U2fInitPacket> init_packet =
30 U2fInitPacket::CreateFromSerializedData(buf, &remaining_size);
31 if (init_packet == nullptr)
32 return nullptr;
33
34 return make_scoped_refptr(new U2fMessage(init_packet, remaining_size));
35 }
36
37 U2fMessage::U2fMessage(scoped_refptr<U2fInitPacket> init_packet,
38 size_t remaining_size)
39 : remaining_size_(remaining_size) {
40 channel_id_ = init_packet->channel_id();
41 packets_.push_back(init_packet);
42 }
43
44 U2fMessage::U2fMessage(uint32_t channel_id,
45 Type type,
46 const std::vector<uint8_t>& data)
47 : packets_(), remaining_size_(), channel_id_(channel_id) {
48 size_t remaining_bytes = data.size();
49 uint8_t sequence = 0;
50
51 std::vector<uint8_t>::const_iterator first = data.begin();
52 std::vector<uint8_t>::const_iterator last;
53
54 if (remaining_bytes > kInitPacketDataSize) {
55 last = data.begin() + kInitPacketDataSize;
56 remaining_bytes -= kInitPacketDataSize;
57 } else {
58 last = data.begin() + remaining_bytes;
59 remaining_bytes = 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_bytes > 0) {
67 first = last;
68 if (remaining_bytes > kContinuationPacketDataSize) {
69 last = first + kContinuationPacketDataSize;
70 remaining_bytes -= kContinuationPacketDataSize;
71 } else {
72 last = first + remaining_bytes;
73 remaining_bytes = 0;
74 }
75
76 packets_.push_back(make_scoped_refptr(new U2fContinuationPacket(
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 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 (cont_packet == nullptr || channel_id_ != cont_packet->channel_id())
110 return false;
111
112 remaining_size_ = remaining_size;
113 packets_.push_back(cont_packet);
114 return true;
115 }
116
117 bool U2fMessage::MessageComplete() {
118 return remaining_size_ == 0;
119 }
120
121 std::vector<uint8_t> U2fMessage::GetMessagePayload() const {
122 std::vector<uint8_t> data;
123
124 for (const auto& packet : packets_) {
125 std::vector<uint8_t> packet_data = packet->GetPacketPayload();
126 data.insert(std::end(data), packet_data.cbegin(), packet_data.cend());
127 }
128
129 return data;
130 }
131
132 size_t U2fMessage::NumPackets() {
133 return packets_.size();
134 }
135
136 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698