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