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