| 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_packet.h" |
| 6 #include "net/base/io_buffer.h" |
| 7 |
| 8 namespace device { |
| 9 |
| 10 U2fPacket::U2fPacket(const std::vector<uint8_t> data, const uint32_t channel_id) |
| 11 : data_(data), channel_id_(channel_id) {} |
| 12 |
| 13 U2fPacket::U2fPacket() {} |
| 14 |
| 15 U2fPacket::~U2fPacket() {} |
| 16 |
| 17 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetSerializedBuffer() { |
| 18 if (serialized_) |
| 19 return serialized_; |
| 20 else |
| 21 return make_scoped_refptr(new net::IOBufferWithSize(0)); |
| 22 } |
| 23 |
| 24 std::vector<uint8_t> U2fPacket::GetPacketPayload() const { |
| 25 return data_; |
| 26 } |
| 27 |
| 28 // U2F Initialization packet is defined as: |
| 29 // Offset Length |
| 30 // 0 4 Channel ID |
| 31 // 4 1 Command ID |
| 32 // 5 1 High order packet payload size |
| 33 // 6 1 Low order packet payload size |
| 34 // 7 (s-7) Payload data |
| 35 U2fInitPacket::U2fInitPacket(const uint32_t channel_id, |
| 36 const uint8_t cmd, |
| 37 const std::vector<uint8_t> data, |
| 38 const uint16_t payload_length) |
| 39 : U2fPacket(data, channel_id), command_(cmd) { |
| 40 serialized_ = new net::IOBufferWithSize(kPacketSize); |
| 41 size_t index = 0; |
| 42 // Byte at offset 0 is the report ID, which is always 0 |
| 43 serialized_->data()[index++] = 0; |
| 44 |
| 45 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff; |
| 46 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff; |
| 47 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff; |
| 48 serialized_->data()[index++] = channel_id_ & 0xff; |
| 49 |
| 50 serialized_->data()[index++] = command_; |
| 51 payload_length_ = payload_length; |
| 52 serialized_->data()[index++] = (payload_length >> 8) & 0xff; |
| 53 serialized_->data()[index++] = payload_length & 0xff; |
| 54 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) |
| 55 serialized_->data()[index++] = data_.at(data_idx); |
| 56 while (static_cast<int>(index) < serialized_->size()) |
| 57 serialized_->data()[index++] = 0; |
| 58 } |
| 59 |
| 60 // static |
| 61 scoped_refptr<U2fInitPacket> U2fInitPacket::CreateFromSerializedData( |
| 62 scoped_refptr<net::IOBufferWithSize> buf, |
| 63 size_t* remaining_size) { |
| 64 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) |
| 65 return nullptr; |
| 66 |
| 67 return make_scoped_refptr(new U2fInitPacket(buf, remaining_size)); |
| 68 } |
| 69 |
| 70 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf, |
| 71 size_t* remaining_size) { |
| 72 // Report ID is at index 0, so start at index 1 for channel ID |
| 73 size_t index = 1; |
| 74 uint16_t payload_size = 0; |
| 75 uint16_t data_size = 0; |
| 76 |
| 77 channel_id_ = (buf->data()[index++] & 0xff) << 24; |
| 78 channel_id_ |= (buf->data()[index++] & 0xff) << 16; |
| 79 channel_id_ |= (buf->data()[index++] & 0xff) << 8; |
| 80 channel_id_ |= buf->data()[index++] & 0xff; |
| 81 command_ = buf->data()[index++]; |
| 82 payload_size = buf->data()[index++] << 8; |
| 83 payload_size |= static_cast<uint8_t>(buf->data()[index++]); |
| 84 payload_length_ = payload_size; |
| 85 |
| 86 // Check to see if payload is less than maximum size and padded with 0s |
| 87 data_size = |
| 88 std::min(payload_size, static_cast<uint16_t>(kPacketSize - index)); |
| 89 // Update remaining size to determine the payload size of follow on packets |
| 90 *remaining_size = payload_size - data_size; |
| 91 |
| 92 data_.insert(data_.end(), &buf->data()[index], |
| 93 &buf->data()[index + data_size]); |
| 94 |
| 95 for (int i = index + data_size; i < buf->size(); ++i) |
| 96 buf->data()[i] = 0; |
| 97 serialized_ = buf; |
| 98 } |
| 99 |
| 100 U2fInitPacket::~U2fInitPacket() {} |
| 101 |
| 102 // U2F Continuation packet is defined as: |
| 103 // Offset Length |
| 104 // 0 4 Channel ID |
| 105 // 4 1 Packet sequence 0x00..0x7f |
| 106 // 5 (s-5) Payload data |
| 107 U2fContinuationPacket::U2fContinuationPacket(const uint32_t channel_id, |
| 108 const uint8_t sequence, |
| 109 std::vector<uint8_t> data) |
| 110 : U2fPacket(data, channel_id), sequence_(sequence) { |
| 111 serialized_ = new net::IOBufferWithSize(kPacketSize); |
| 112 size_t index = 0; |
| 113 // Byte at offset 0 is the report ID, which is always 0 |
| 114 serialized_->data()[index++] = 0; |
| 115 |
| 116 serialized_->data()[index++] = (channel_id_ >> 24) & 0xff; |
| 117 serialized_->data()[index++] = (channel_id_ >> 16) & 0xff; |
| 118 serialized_->data()[index++] = (channel_id_ >> 8) & 0xff; |
| 119 serialized_->data()[index++] = channel_id_ & 0xff; |
| 120 |
| 121 serialized_->data()[index++] = sequence_; |
| 122 for (size_t idx = 0; idx < data_.size(); ++idx) |
| 123 serialized_->data()[index++] = data_.at(idx); |
| 124 |
| 125 while (static_cast<int>(index) < serialized_->size()) |
| 126 serialized_->data()[index++] = 0; |
| 127 } |
| 128 |
| 129 // static |
| 130 scoped_refptr<U2fContinuationPacket> |
| 131 U2fContinuationPacket::CreateFromSerializedData( |
| 132 scoped_refptr<net::IOBufferWithSize> buf, |
| 133 size_t* remaining_size) { |
| 134 if (buf == nullptr || remaining_size == nullptr || buf->size() != kPacketSize) |
| 135 return nullptr; |
| 136 |
| 137 return make_scoped_refptr(new U2fContinuationPacket(buf, remaining_size)); |
| 138 } |
| 139 |
| 140 U2fContinuationPacket::U2fContinuationPacket( |
| 141 scoped_refptr<net::IOBufferWithSize> buf, |
| 142 size_t* remaining_size) { |
| 143 // Report ID is at index 0, so start at index 1 for channel ID |
| 144 size_t index = 1; |
| 145 size_t data_size; |
| 146 |
| 147 channel_id_ = (buf->data()[index++] & 0xff) << 24; |
| 148 channel_id_ |= (buf->data()[index++] & 0xff) << 16; |
| 149 channel_id_ |= (buf->data()[index++] & 0xff) << 8; |
| 150 channel_id_ |= buf->data()[index++] & 0xff; |
| 151 sequence_ = buf->data()[index++]; |
| 152 |
| 153 // Check to see if packet payload is less than maximum size and padded with 0s |
| 154 data_size = std::min(*remaining_size, kPacketSize - index); |
| 155 *remaining_size -= data_size; |
| 156 data_.insert(std::end(data_), &buf->data()[index], |
| 157 &buf->data()[index + data_size]); |
| 158 |
| 159 // Incoming buffer may not be padded with 0's, so manually update buffer |
| 160 for (int i = index + data_size; i < buf->size(); ++i) |
| 161 buf->data()[i] = 0; |
| 162 serialized_ = buf; |
| 163 } |
| 164 |
| 165 U2fContinuationPacket::~U2fContinuationPacket() {} |
| 166 } // namespace device |
| OLD | NEW |