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