| 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 <iostream> |
| 6 #include "device/u2f/u2f_packet.h" |
| 7 |
| 8 namespace device { |
| 9 |
| 10 U2fPacket::U2fPacket(const std::vector<uint8_t> data, |
| 11 const char channel_id[kChannelIdSize]) |
| 12 : data_(data) { |
| 13 for (size_t index = 0; index < kChannelIdSize; ++index) |
| 14 channel_id_[index] = channel_id[index]; |
| 15 } |
| 16 |
| 17 U2fPacket::U2fPacket() : data_() {} |
| 18 |
| 19 U2fPacket::~U2fPacket() {} |
| 20 |
| 21 scoped_refptr<net::IOBufferWithSize> U2fPacket::GetBuffer() { |
| 22 if (serialized_) |
| 23 return serialized_; |
| 24 else |
| 25 return make_scoped_refptr(new net::IOBufferWithSize(0)); |
| 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 char channel_id[kChannelIdSize], |
| 36 const char 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 serialized_->data()[index++] = 0; |
| 43 for (size_t i = 0; i < kChannelIdSize; ++i, index++) |
| 44 serialized_->data()[index] = channel_id_[i]; |
| 45 |
| 46 serialized_->data()[index++] = command_; |
| 47 |
| 48 serialized_->data()[index++] = (payload_length >> 8) & 0xff; |
| 49 serialized_->data()[index++] = payload_length & 0xff; |
| 50 for (size_t data_idx = 0; data_idx < data_.size(); ++data_idx) |
| 51 serialized_->data()[index++] = data_.at(data_idx); |
| 52 while ((int)index < serialized_->size()) |
| 53 serialized_->data()[index++] = 0; |
| 54 } |
| 55 |
| 56 scoped_refptr<U2fInitPacket> U2fInitPacket::Create( |
| 57 scoped_refptr<net::IOBufferWithSize> buf) { |
| 58 if (!buf || buf->size() != kPacketSize) |
| 59 return nullptr; |
| 60 |
| 61 return make_scoped_refptr(new U2fInitPacket(buf)); |
| 62 } |
| 63 |
| 64 U2fInitPacket::U2fInitPacket(scoped_refptr<net::IOBufferWithSize> buf) |
| 65 : U2fPacket(), command_() { |
| 66 size_t index = 1; |
| 67 uint16_t payload_size = 0; |
| 68 |
| 69 channel_id_[0] = buf->data()[index++]; |
| 70 channel_id_[1] = buf->data()[index++]; |
| 71 channel_id_[2] = buf->data()[index++]; |
| 72 channel_id_[3] = buf->data()[index++]; |
| 73 command_ = buf->data()[index++]; |
| 74 payload_size = ((uint16_t)buf->data()[index++]) << 8; |
| 75 payload_size |= buf->data()[index++]; |
| 76 // Check to see if payload is smaller than maximum size |
| 77 payload_size = (payload_size + index > kPacketSize) ? (kPacketSize - index) |
| 78 : payload_size; |
| 79 |
| 80 data_.insert(std::end(data_), buf->data()[index], |
| 81 buf->data()[index + payload_size]); |
| 82 } |
| 83 |
| 84 U2fInitPacket::~U2fInitPacket() {} |
| 85 |
| 86 // U2F Continuation packet is defined as: |
| 87 // Offset Length |
| 88 // 0 4 Channel ID |
| 89 // 4 1 Packet sequence 0x00..0x7f |
| 90 // 5 (s-5) Payload data |
| 91 U2fContPacket::U2fContPacket(const char channel_id[kChannelIdSize], |
| 92 const char sequence, |
| 93 std::vector<uint8_t> data) |
| 94 : U2fPacket(data, channel_id), sequence_(sequence) { |
| 95 serialized_ = new net::IOBufferWithSize(kPacketSize); |
| 96 size_t index = 0; |
| 97 serialized_->data()[index++] = 0; |
| 98 for (size_t idx = 0; idx < kChannelIdSize; ++idx) |
| 99 serialized_->data()[index++] = channel_id_[idx]; |
| 100 |
| 101 serialized_->data()[index++] = sequence_; |
| 102 for (size_t idx = 0; idx < data_.size(); ++idx) |
| 103 serialized_->data()[index++] = data_.at(idx); |
| 104 |
| 105 while ((int)index < serialized_->size()) |
| 106 serialized_->data()[index++] = 0; |
| 107 } |
| 108 |
| 109 U2fContPacket::~U2fContPacket() {} |
| 110 } // namespace device |
| OLD | NEW |