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