OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
6 #include "device/u2f/u2f_packet.h" | 6 #include "device/u2f/u2f_packet.h" |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 | 8 |
9 #include "u2f_message.h" | 9 #include "u2f_message.h" |
10 | 10 |
11 namespace device { | 11 namespace device { |
12 | 12 |
13 // static | 13 // static |
14 std::unique_ptr<U2fMessage> U2fMessage::Create( | 14 std::unique_ptr<U2fMessage> U2fMessage::Create( |
15 uint32_t channel_id, | 15 uint32_t channel_id, |
16 Type type, | 16 Type type, |
17 const std::vector<uint8_t>& data) { | 17 const std::vector<uint8_t>& data) { |
18 if (data.size() > kMaxMessageSize) | 18 if (data.size() > kMaxMessageSize) |
19 return nullptr; | 19 return nullptr; |
20 | 20 |
21 return base::MakeUnique<U2fMessage>(channel_id, type, data); | 21 return base::MakeUnique<U2fMessage>(channel_id, type, data); |
22 } | 22 } |
23 | 23 |
24 // static | 24 // static |
25 std::unique_ptr<U2fMessage> U2fMessage::CreateFromSerializedData( | 25 std::unique_ptr<U2fMessage> U2fMessage::CreateFromSerializedData( |
26 scoped_refptr<net::IOBufferWithSize> buf) { | 26 const std::vector<uint8_t>& buf) { |
27 size_t remaining_size = 0; | 27 size_t remaining_size = 0; |
28 if (buf == nullptr || | 28 if (buf.size() > U2fPacket::kPacketSize || buf.size() < kInitPacketHeader) |
29 static_cast<size_t>(buf->size()) > U2fPacket::kPacketSize || | |
30 static_cast<size_t>(buf->size()) < kInitPacketHeader) | |
31 return nullptr; | 29 return nullptr; |
32 | 30 |
33 std::unique_ptr<U2fInitPacket> init_packet = | 31 std::unique_ptr<U2fInitPacket> init_packet = |
34 U2fInitPacket::CreateFromSerializedData(buf, &remaining_size); | 32 U2fInitPacket::CreateFromSerializedData(buf, &remaining_size); |
35 if (init_packet == nullptr) | 33 if (init_packet == nullptr) |
36 return nullptr; | 34 return nullptr; |
37 | 35 |
38 return base::MakeUnique<U2fMessage>(std::move(init_packet), remaining_size); | 36 return base::MakeUnique<U2fMessage>(std::move(init_packet), remaining_size); |
39 } | 37 } |
40 | 38 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 std::list<std::unique_ptr<U2fPacket>>::const_iterator U2fMessage::begin() { | 86 std::list<std::unique_ptr<U2fPacket>>::const_iterator U2fMessage::begin() { |
89 return packets_.cbegin(); | 87 return packets_.cbegin(); |
90 } | 88 } |
91 | 89 |
92 std::list<std::unique_ptr<U2fPacket>>::const_iterator U2fMessage::end() { | 90 std::list<std::unique_ptr<U2fPacket>>::const_iterator U2fMessage::end() { |
93 return packets_.cend(); | 91 return packets_.cend(); |
94 } | 92 } |
95 | 93 |
96 scoped_refptr<net::IOBufferWithSize> U2fMessage::PopNextPacket() { | 94 scoped_refptr<net::IOBufferWithSize> U2fMessage::PopNextPacket() { |
97 if (NumPackets() > 0) { | 95 if (NumPackets() > 0) { |
98 scoped_refptr<net::IOBufferWithSize> buf = | 96 scoped_refptr<net::IOBufferWithSize> data = |
99 packets_.front()->GetSerializedBuffer(); | 97 packets_.front()->GetSerializedData(); |
| 98 |
100 packets_.pop_front(); | 99 packets_.pop_front(); |
101 return buf; | 100 return data; |
102 } | 101 } |
103 return nullptr; | 102 return nullptr; |
104 } | 103 } |
105 | 104 |
106 bool U2fMessage::AddContinuationPacket( | 105 bool U2fMessage::AddContinuationPacket(const std::vector<uint8_t>& buf) { |
107 scoped_refptr<net::IOBufferWithSize> buf) { | |
108 size_t remaining_size = remaining_size_; | 106 size_t remaining_size = remaining_size_; |
109 std::unique_ptr<U2fContinuationPacket> cont_packet = | 107 std::unique_ptr<U2fContinuationPacket> cont_packet = |
110 U2fContinuationPacket::CreateFromSerializedData(buf, &remaining_size); | 108 U2fContinuationPacket::CreateFromSerializedData(buf, &remaining_size); |
111 | 109 |
112 // Reject packets with a different channel id | 110 // Reject packets with a different channel id |
113 if (cont_packet == nullptr || channel_id_ != cont_packet->channel_id()) | 111 if (cont_packet == nullptr || channel_id_ != cont_packet->channel_id()) |
114 return false; | 112 return false; |
115 | 113 |
116 remaining_size_ = remaining_size; | 114 remaining_size_ = remaining_size; |
117 packets_.push_back(std::move(cont_packet)); | 115 packets_.push_back(std::move(cont_packet)); |
(...skipping 13 matching lines...) Expand all Loading... |
131 } | 129 } |
132 | 130 |
133 return data; | 131 return data; |
134 } | 132 } |
135 | 133 |
136 size_t U2fMessage::NumPackets() { | 134 size_t U2fMessage::NumPackets() { |
137 return packets_.size(); | 135 return packets_.size(); |
138 } | 136 } |
139 | 137 |
140 } // namespace device | 138 } // namespace device |
OLD | NEW |