Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: device/u2f/u2f_hid_device.cc

Issue 2721223002: Add support for U2fHidDevice interaction (Closed)
Patch Set: Use MockHidConnection to fail writes in unittest Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/u2f/u2f_hid_device.h ('k') | device/u2f/u2f_hid_device_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "u2f_hid_device.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "crypto/random.h"
11 #include "device/base/device_client.h"
12 #include "device/hid/hid_connection.h"
13 #include "u2f_apdu_command.h"
14 #include "u2f_message.h"
15
16 namespace device {
17
18 namespace switches {
19 static constexpr char kEnableU2fHidTest[] = "enable-u2f-hid-tests";
20 } // namespace switches
21
22 U2fHidDevice::U2fHidDevice(scoped_refptr<HidDeviceInfo> device_info)
23 : U2fDevice(),
24 state_(State::INIT),
25 device_info_(device_info),
26 weak_factory_(this) {
27 channel_id_ = kBroadcastChannel;
28 }
29
30 U2fHidDevice::~U2fHidDevice() {
31 // Cleanup connection
32 if (connection_)
33 connection_->Close();
34 }
35
36 void U2fHidDevice::DeviceTransact(scoped_refptr<U2fApduCommand> command,
37 const DeviceCallback& callback) {
38 Transition(command, callback);
39 }
40
41 void U2fHidDevice::Transition(scoped_refptr<U2fApduCommand> command,
42 const DeviceCallback& callback) {
43 switch (state_) {
44 case State::INIT:
45 state_ = State::BUSY;
46 Connect(base::Bind(&U2fHidDevice::OnConnect, weak_factory_.GetWeakPtr(),
47 command, callback));
48 break;
49 case State::CONNECTED:
50 state_ = State::BUSY;
51 AllocateChannel(command, callback);
52 break;
53 case State::IDLE: {
54 state_ = State::BUSY;
55 scoped_refptr<U2fMessage> msg = U2fMessage::Create(
56 channel_id_, U2fMessage::Type::CMD_MSG, command->GetEncodedCommand());
57 WriteMessage(msg, true,
58 base::Bind(&U2fHidDevice::MessageReceived,
59 weak_factory_.GetWeakPtr(), callback));
60 break;
61 }
62 case State::BUSY:
63 pending_transactions_.push_back({std::move(command), callback});
64 break;
65 case State::DEVICE_ERROR:
66 default:
67 base::WeakPtr<U2fHidDevice> self = weak_factory_.GetWeakPtr();
68 callback.Run(false, nullptr);
69 // Executing callbacks may free |this|. Check |self| first.
70 while (self && !pending_transactions_.empty()) {
71 // Respond to any pending requests
72 DeviceCallback pending_cb = pending_transactions_.front().second;
73 pending_transactions_.pop_front();
74 pending_cb.Run(false, nullptr);
75 }
76 break;
77 }
78 }
79
80 void U2fHidDevice::Connect(const HidService::ConnectCallback& callback) {
81 HidService* hid_service = DeviceClient::Get()->GetHidService();
82
83 hid_service->Connect(device_info_->device_id(), callback);
84 }
85
86 void U2fHidDevice::OnConnect(scoped_refptr<U2fApduCommand> command,
87 const DeviceCallback& callback,
88 scoped_refptr<HidConnection> connection) {
89 if (connection) {
90 connection_ = connection;
91 state_ = State::CONNECTED;
92 } else {
93 state_ = State::DEVICE_ERROR;
94 }
95 Transition(command, callback);
96 }
97
98 void U2fHidDevice::AllocateChannel(scoped_refptr<U2fApduCommand> command,
99 const DeviceCallback& callback) {
100 // Send random nonce to device to verify received message
101 std::vector<uint8_t> nonce(8);
102 crypto::RandBytes(nonce.data(), nonce.size());
103 scoped_refptr<U2fMessage> message =
104 U2fMessage::Create(channel_id_, U2fMessage::Type::CMD_INIT, nonce);
105
106 WriteMessage(
107 message, true,
108 base::Bind(&U2fHidDevice::OnAllocateChannel, weak_factory_.GetWeakPtr(),
109 nonce, command, callback));
110 }
111
112 void U2fHidDevice::OnAllocateChannel(std::vector<uint8_t> nonce,
113 scoped_refptr<U2fApduCommand> command,
114 const DeviceCallback& callback,
115 bool success,
116 scoped_refptr<U2fMessage> message) {
117 if (!success || !message) {
118 state_ = State::DEVICE_ERROR;
119 Transition(nullptr, callback);
120 return;
121 }
122 // Channel allocation response is defined as:
123 // 0: 8 byte nonce
124 // 8: 4 byte channel id
125 // 12: Protocol version id
126 // 13: Major device version
127 // 14: Minor device version
128 // 15: Build device version
129 // 16: Capabilities
130 std::vector<uint8_t> payload = message->GetMessagePayload();
131 if (payload.size() != 17) {
132 state_ = State::DEVICE_ERROR;
133 Transition(nullptr, callback);
134 return;
135 }
136
137 std::vector<uint8_t> received_nonce(std::begin(payload),
138 std::begin(payload) + 8);
139 if (nonce != received_nonce) {
140 state_ = State::DEVICE_ERROR;
141 Transition(nullptr, callback);
142 return;
143 }
144
145 size_t index = 8;
146 channel_id_ = payload[index++] << 24;
147 channel_id_ |= payload[index++] << 16;
148 channel_id_ |= payload[index++] << 8;
149 channel_id_ |= payload[index++];
150 capabilities_ = payload[16];
151
152 state_ = State::IDLE;
153 Transition(command, callback);
154 }
155
156 void U2fHidDevice::WriteMessage(scoped_refptr<U2fMessage> message,
157 bool response_expected,
158 U2fHidMessageCallback callback) {
159 if (!connection_ || !message || message->NumPackets() == 0) {
160 std::move(callback).Run(false, nullptr);
161 return;
162 }
163
164 scoped_refptr<net::IOBufferWithSize> buffer = message->PopNextPacket();
165
166 connection_->Write(
167 buffer, buffer->size(),
168 base::Bind(&U2fHidDevice::PacketWritten, weak_factory_.GetWeakPtr(),
169 message, true, base::Passed(std::move(callback))));
170 }
171
172 void U2fHidDevice::PacketWritten(scoped_refptr<U2fMessage> message,
173 bool response_expected,
174 U2fHidMessageCallback callback,
175 bool success) {
176 if (success && message->NumPackets() > 0) {
177 WriteMessage(message, response_expected, std::move(callback));
178 } else if (success && response_expected) {
179 ReadMessage(std::move(callback));
180 } else {
181 std::move(callback).Run(success, nullptr);
182 }
183 }
184
185 void U2fHidDevice::ReadMessage(U2fHidMessageCallback callback) {
186 if (!connection_) {
187 std::move(callback).Run(false, nullptr);
188 return;
189 }
190
191 connection_->Read(base::Bind(&U2fHidDevice::OnRead,
192 weak_factory_.GetWeakPtr(),
193 base::Passed(std::move(callback))));
194 }
195
196 void U2fHidDevice::OnRead(U2fHidMessageCallback callback,
197 bool success,
198 scoped_refptr<net::IOBuffer> buf,
199 size_t size) {
200 if (!success) {
201 std::move(callback).Run(success, nullptr);
202 return;
203 }
204
205 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(size));
206 memcpy(buffer->data(), buf->data(), size);
207 scoped_refptr<U2fMessage> read_message =
208 U2fMessage::CreateFromSerializedData(buffer);
209
210 if (!read_message) {
211 std::move(callback).Run(false, nullptr);
212 return;
213 }
214
215 // Received a message from a different channel, so try again
216 if (channel_id_ != read_message->channel_id()) {
217 connection_->Read(base::Bind(&U2fHidDevice::OnRead,
218 weak_factory_.GetWeakPtr(),
219 base::Passed(std::move(callback))));
220 return;
221 }
222
223 if (read_message->MessageComplete()) {
224 std::move(callback).Run(success, read_message);
225 return;
226 }
227
228 // Continue reading additional packets
229 connection_->Read(base::Bind(&U2fHidDevice::OnReadContinuation,
230 weak_factory_.GetWeakPtr(), read_message,
231 base::Passed(std::move(callback))));
232 }
233
234 void U2fHidDevice::OnReadContinuation(scoped_refptr<U2fMessage> message,
235 U2fHidMessageCallback callback,
236 bool success,
237 scoped_refptr<net::IOBuffer> buf,
238 size_t size) {
239 if (!success) {
240 std::move(callback).Run(success, nullptr);
241 return;
242 }
243
244 scoped_refptr<net::IOBufferWithSize> buffer(new net::IOBufferWithSize(size));
245 memcpy(buffer->data(), buf->data(), size);
246 message->AddContinuationPacket(buffer);
247 if (message->MessageComplete()) {
248 std::move(callback).Run(success, message);
249 return;
250 }
251 connection_->Read(base::Bind(&U2fHidDevice::OnReadContinuation,
252 weak_factory_.GetWeakPtr(), message,
253 base::Passed(std::move(callback))));
254 }
255
256 void U2fHidDevice::MessageReceived(const DeviceCallback& callback,
257 bool success,
258 scoped_refptr<U2fMessage> message) {
259 if (!success) {
260 state_ = State::DEVICE_ERROR;
261 Transition(nullptr, callback);
262 return;
263 }
264 scoped_refptr<U2fApduResponse> response = nullptr;
265 if (message)
266 response = U2fApduResponse::CreateFromMessage(message->GetMessagePayload());
267 state_ = State::IDLE;
268 base::WeakPtr<U2fHidDevice> self = weak_factory_.GetWeakPtr();
269 callback.Run(success, response);
270
271 // Executing |callback| may have freed |this|. Check |self| first.
272 if (self && !pending_transactions_.empty()) {
273 // If any transactions were queued, process the first one
274 scoped_refptr<U2fApduCommand> pending_cmd =
275 std::move(pending_transactions_.front().first);
276 DeviceCallback pending_cb = pending_transactions_.front().second;
277 pending_transactions_.pop_front();
278 Transition(pending_cmd, pending_cb);
279 }
280 }
281
282 void U2fHidDevice::TryWink(const WinkCallback& callback) {
283 // Only try to wink if device claims support
284 if (!(capabilities_ & kWinkCapability) || state_ != State::IDLE) {
285 callback.Run();
286 return;
287 }
288
289 scoped_refptr<U2fMessage> wink_message = device::U2fMessage::Create(
290 channel_id_, U2fMessage::Type::CMD_WINK, std::vector<uint8_t>());
291 WriteMessage(
292 wink_message, true,
293 base::Bind(&U2fHidDevice::OnWink, weak_factory_.GetWeakPtr(), callback));
294 }
295
296 void U2fHidDevice::OnWink(const WinkCallback& callback,
297 bool success,
298 scoped_refptr<U2fMessage> response) {
299 callback.Run();
300 }
301
302 std::string U2fHidDevice::GetId() {
303 std::ostringstream id("hid:");
304 id << device_info_->device_id();
305 return id.str();
306 }
307
308 // static
309 bool U2fHidDevice::IsTestEnabled() {
310 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
311 return command_line->HasSwitch(switches::kEnableU2fHidTest);
312 }
313
314 } // namespace device
OLDNEW
« no previous file with comments | « device/u2f/u2f_hid_device.h ('k') | device/u2f/u2f_hid_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698