| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "u2f_hid_device.h" | 5 #include "u2f_hid_device.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "crypto/random.h" | 11 #include "crypto/random.h" |
| 11 #include "device/base/device_client.h" | 12 #include "device/base/device_client.h" |
| 12 #include "device/hid/hid_connection.h" | 13 #include "device/hid/hid_connection.h" |
| 13 #include "u2f_apdu_command.h" | 14 #include "u2f_apdu_command.h" |
| 14 #include "u2f_message.h" | 15 #include "u2f_message.h" |
| 15 | 16 |
| 16 namespace device { | 17 namespace device { |
| 17 | 18 |
| 18 namespace switches { | 19 namespace switches { |
| 19 static constexpr char kEnableU2fHidTest[] = "enable-u2f-hid-tests"; | 20 static constexpr char kEnableU2fHidTest[] = "enable-u2f-hid-tests"; |
| 20 } // namespace switches | 21 } // namespace switches |
| 21 | 22 |
| 22 U2fHidDevice::U2fHidDevice(scoped_refptr<HidDeviceInfo> device_info) | 23 U2fHidDevice::U2fHidDevice(scoped_refptr<HidDeviceInfo> device_info) |
| 23 : U2fDevice(), | 24 : U2fDevice(), |
| 24 state_(State::INIT), | 25 state_(State::INIT), |
| 25 device_info_(device_info), | 26 device_info_(device_info), |
| 26 weak_factory_(this) { | 27 weak_factory_(this) { |
| 27 channel_id_ = kBroadcastChannel; | 28 channel_id_ = kBroadcastChannel; |
| 28 } | 29 } |
| 29 | 30 |
| 30 U2fHidDevice::~U2fHidDevice() { | 31 U2fHidDevice::~U2fHidDevice() { |
| 31 // Cleanup connection | 32 // Cleanup connection |
| 32 if (connection_) | 33 if (connection_ && !connection_->closed()) |
| 33 connection_->Close(); | 34 connection_->Close(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void U2fHidDevice::DeviceTransact(std::unique_ptr<U2fApduCommand> command, | 37 void U2fHidDevice::DeviceTransact(std::unique_ptr<U2fApduCommand> command, |
| 37 const DeviceCallback& callback) { | 38 const DeviceCallback& callback) { |
| 38 Transition(std::move(command), callback); | 39 Transition(std::move(command), callback); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void U2fHidDevice::Transition(std::unique_ptr<U2fApduCommand> command, | 42 void U2fHidDevice::Transition(std::unique_ptr<U2fApduCommand> command, |
| 42 const DeviceCallback& callback) { | 43 const DeviceCallback& callback) { |
| 43 switch (state_) { | 44 switch (state_) { |
| 44 case State::INIT: | 45 case State::INIT: |
| 45 state_ = State::BUSY; | 46 state_ = State::BUSY; |
| 47 ArmTimeout(callback); |
| 46 Connect(base::Bind(&U2fHidDevice::OnConnect, weak_factory_.GetWeakPtr(), | 48 Connect(base::Bind(&U2fHidDevice::OnConnect, weak_factory_.GetWeakPtr(), |
| 47 base::Passed(&command), callback)); | 49 base::Passed(&command), callback)); |
| 48 break; | 50 break; |
| 49 case State::CONNECTED: | 51 case State::CONNECTED: |
| 50 state_ = State::BUSY; | 52 state_ = State::BUSY; |
| 53 ArmTimeout(callback); |
| 51 AllocateChannel(std::move(command), callback); | 54 AllocateChannel(std::move(command), callback); |
| 52 break; | 55 break; |
| 53 case State::IDLE: { | 56 case State::IDLE: { |
| 54 state_ = State::BUSY; | 57 state_ = State::BUSY; |
| 55 std::unique_ptr<U2fMessage> msg = U2fMessage::Create( | 58 std::unique_ptr<U2fMessage> msg = U2fMessage::Create( |
| 56 channel_id_, U2fMessage::Type::CMD_MSG, command->GetEncodedCommand()); | 59 channel_id_, U2fMessage::Type::CMD_MSG, command->GetEncodedCommand()); |
| 60 |
| 61 ArmTimeout(callback); |
| 62 // Write message to the device |
| 57 WriteMessage(std::move(msg), true, | 63 WriteMessage(std::move(msg), true, |
| 58 base::Bind(&U2fHidDevice::MessageReceived, | 64 base::Bind(&U2fHidDevice::MessageReceived, |
| 59 weak_factory_.GetWeakPtr(), callback)); | 65 weak_factory_.GetWeakPtr(), callback)); |
| 60 break; | 66 break; |
| 61 } | 67 } |
| 62 case State::BUSY: | 68 case State::BUSY: |
| 63 pending_transactions_.push_back({std::move(command), callback}); | 69 pending_transactions_.push_back({std::move(command), callback}); |
| 64 break; | 70 break; |
| 65 case State::DEVICE_ERROR: | 71 case State::DEVICE_ERROR: |
| 66 default: | 72 default: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 79 | 85 |
| 80 void U2fHidDevice::Connect(const HidService::ConnectCallback& callback) { | 86 void U2fHidDevice::Connect(const HidService::ConnectCallback& callback) { |
| 81 HidService* hid_service = DeviceClient::Get()->GetHidService(); | 87 HidService* hid_service = DeviceClient::Get()->GetHidService(); |
| 82 | 88 |
| 83 hid_service->Connect(device_info_->device_id(), callback); | 89 hid_service->Connect(device_info_->device_id(), callback); |
| 84 } | 90 } |
| 85 | 91 |
| 86 void U2fHidDevice::OnConnect(std::unique_ptr<U2fApduCommand> command, | 92 void U2fHidDevice::OnConnect(std::unique_ptr<U2fApduCommand> command, |
| 87 const DeviceCallback& callback, | 93 const DeviceCallback& callback, |
| 88 scoped_refptr<HidConnection> connection) { | 94 scoped_refptr<HidConnection> connection) { |
| 95 if (state_ == State::DEVICE_ERROR) |
| 96 return; |
| 97 timeout_callback_.Cancel(); |
| 98 |
| 89 if (connection) { | 99 if (connection) { |
| 90 connection_ = connection; | 100 connection_ = connection; |
| 91 state_ = State::CONNECTED; | 101 state_ = State::CONNECTED; |
| 92 } else { | 102 } else { |
| 93 state_ = State::DEVICE_ERROR; | 103 state_ = State::DEVICE_ERROR; |
| 94 } | 104 } |
| 95 Transition(std::move(command), callback); | 105 Transition(std::move(command), callback); |
| 96 } | 106 } |
| 97 | 107 |
| 98 void U2fHidDevice::AllocateChannel(std::unique_ptr<U2fApduCommand> command, | 108 void U2fHidDevice::AllocateChannel(std::unique_ptr<U2fApduCommand> command, |
| 99 const DeviceCallback& callback) { | 109 const DeviceCallback& callback) { |
| 100 // Send random nonce to device to verify received message | 110 // Send random nonce to device to verify received message |
| 101 std::vector<uint8_t> nonce(8); | 111 std::vector<uint8_t> nonce(8); |
| 102 crypto::RandBytes(nonce.data(), nonce.size()); | 112 crypto::RandBytes(nonce.data(), nonce.size()); |
| 103 std::unique_ptr<U2fMessage> message = | 113 std::unique_ptr<U2fMessage> message = |
| 104 U2fMessage::Create(channel_id_, U2fMessage::Type::CMD_INIT, nonce); | 114 U2fMessage::Create(channel_id_, U2fMessage::Type::CMD_INIT, nonce); |
| 105 | 115 |
| 106 WriteMessage( | 116 WriteMessage( |
| 107 std::move(message), true, | 117 std::move(message), true, |
| 108 base::Bind(&U2fHidDevice::OnAllocateChannel, weak_factory_.GetWeakPtr(), | 118 base::Bind(&U2fHidDevice::OnAllocateChannel, weak_factory_.GetWeakPtr(), |
| 109 nonce, base::Passed(&command), callback)); | 119 nonce, base::Passed(&command), callback)); |
| 110 } | 120 } |
| 111 | 121 |
| 112 void U2fHidDevice::OnAllocateChannel(std::vector<uint8_t> nonce, | 122 void U2fHidDevice::OnAllocateChannel(std::vector<uint8_t> nonce, |
| 113 std::unique_ptr<U2fApduCommand> command, | 123 std::unique_ptr<U2fApduCommand> command, |
| 114 const DeviceCallback& callback, | 124 const DeviceCallback& callback, |
| 115 bool success, | 125 bool success, |
| 116 std::unique_ptr<U2fMessage> message) { | 126 std::unique_ptr<U2fMessage> message) { |
| 127 if (state_ == State::DEVICE_ERROR) |
| 128 return; |
| 129 timeout_callback_.Cancel(); |
| 130 |
| 117 if (!success || !message) { | 131 if (!success || !message) { |
| 118 state_ = State::DEVICE_ERROR; | 132 state_ = State::DEVICE_ERROR; |
| 119 Transition(nullptr, callback); | 133 Transition(nullptr, callback); |
| 120 return; | 134 return; |
| 121 } | 135 } |
| 122 // Channel allocation response is defined as: | 136 // Channel allocation response is defined as: |
| 123 // 0: 8 byte nonce | 137 // 0: 8 byte nonce |
| 124 // 8: 4 byte channel id | 138 // 8: 4 byte channel id |
| 125 // 12: Protocol version id | 139 // 12: Protocol version id |
| 126 // 13: Major device version | 140 // 13: Major device version |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 return; | 261 return; |
| 248 } | 262 } |
| 249 connection_->Read( | 263 connection_->Read( |
| 250 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), | 264 base::Bind(&U2fHidDevice::OnReadContinuation, weak_factory_.GetWeakPtr(), |
| 251 base::Passed(&message), base::Passed(&callback))); | 265 base::Passed(&message), base::Passed(&callback))); |
| 252 } | 266 } |
| 253 | 267 |
| 254 void U2fHidDevice::MessageReceived(const DeviceCallback& callback, | 268 void U2fHidDevice::MessageReceived(const DeviceCallback& callback, |
| 255 bool success, | 269 bool success, |
| 256 std::unique_ptr<U2fMessage> message) { | 270 std::unique_ptr<U2fMessage> message) { |
| 271 if (state_ == State::DEVICE_ERROR) |
| 272 return; |
| 273 timeout_callback_.Cancel(); |
| 274 |
| 257 if (!success) { | 275 if (!success) { |
| 258 state_ = State::DEVICE_ERROR; | 276 state_ = State::DEVICE_ERROR; |
| 259 Transition(nullptr, callback); | 277 Transition(nullptr, callback); |
| 260 return; | 278 return; |
| 261 } | 279 } |
| 280 |
| 262 std::unique_ptr<U2fApduResponse> response = nullptr; | 281 std::unique_ptr<U2fApduResponse> response = nullptr; |
| 263 if (message) | 282 if (message) |
| 264 response = U2fApduResponse::CreateFromMessage(message->GetMessagePayload()); | 283 response = U2fApduResponse::CreateFromMessage(message->GetMessagePayload()); |
| 265 state_ = State::IDLE; | 284 state_ = State::IDLE; |
| 266 base::WeakPtr<U2fHidDevice> self = weak_factory_.GetWeakPtr(); | 285 base::WeakPtr<U2fHidDevice> self = weak_factory_.GetWeakPtr(); |
| 267 callback.Run(success, std::move(response)); | 286 callback.Run(success, std::move(response)); |
| 268 | 287 |
| 269 // Executing |callback| may have freed |this|. Check |self| first. | 288 // Executing |callback| may have freed |this|. Check |self| first. |
| 270 if (self && !pending_transactions_.empty()) { | 289 if (self && !pending_transactions_.empty()) { |
| 271 // If any transactions were queued, process the first one | 290 // If any transactions were queued, process the first one |
| (...skipping 18 matching lines...) Expand all Loading... |
| 290 std::move(wink_message), true, | 309 std::move(wink_message), true, |
| 291 base::Bind(&U2fHidDevice::OnWink, weak_factory_.GetWeakPtr(), callback)); | 310 base::Bind(&U2fHidDevice::OnWink, weak_factory_.GetWeakPtr(), callback)); |
| 292 } | 311 } |
| 293 | 312 |
| 294 void U2fHidDevice::OnWink(const WinkCallback& callback, | 313 void U2fHidDevice::OnWink(const WinkCallback& callback, |
| 295 bool success, | 314 bool success, |
| 296 std::unique_ptr<U2fMessage> response) { | 315 std::unique_ptr<U2fMessage> response) { |
| 297 callback.Run(); | 316 callback.Run(); |
| 298 } | 317 } |
| 299 | 318 |
| 319 void U2fHidDevice::ArmTimeout(const DeviceCallback& callback) { |
| 320 DCHECK(timeout_callback_.IsCancelled()); |
| 321 timeout_callback_.Reset(base::Bind(&U2fHidDevice::OnTimeout, |
| 322 weak_factory_.GetWeakPtr(), callback)); |
| 323 // Setup timeout task for 3 seconds |
| 324 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 325 FROM_HERE, timeout_callback_.callback(), |
| 326 base::TimeDelta::FromMilliseconds(3000)); |
| 327 } |
| 328 |
| 329 void U2fHidDevice::OnTimeout(const DeviceCallback& callback) { |
| 330 state_ = State::DEVICE_ERROR; |
| 331 Transition(nullptr, callback); |
| 332 } |
| 333 |
| 300 std::string U2fHidDevice::GetId() { | 334 std::string U2fHidDevice::GetId() { |
| 301 std::ostringstream id("hid:"); | 335 std::ostringstream id("hid:", std::ios::ate); |
| 302 id << device_info_->device_id(); | 336 id << device_info_->device_id(); |
| 303 return id.str(); | 337 return id.str(); |
| 304 } | 338 } |
| 305 | 339 |
| 306 // static | 340 // static |
| 307 bool U2fHidDevice::IsTestEnabled() { | 341 bool U2fHidDevice::IsTestEnabled() { |
| 308 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 342 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 309 return command_line->HasSwitch(switches::kEnableU2fHidTest); | 343 return command_line->HasSwitch(switches::kEnableU2fHidTest); |
| 310 } | 344 } |
| 311 | 345 |
| 312 } // namespace device | 346 } // namespace device |
| OLD | NEW |