fido_hid_device.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. #include "device/fido/hid/fido_hid_device.h"
  5. #include <limits>
  6. #include <vector>
  7. #include "base/bind.h"
  8. #include "base/callback_helpers.h"
  9. #include "base/command_line.h"
  10. #include "base/containers/contains.h"
  11. #include "base/containers/fixed_flat_set.h"
  12. #include "base/logging.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/strings/string_piece.h"
  15. #include "base/strings/string_util.h"
  16. #include "base/threading/thread_task_runner_handle.h"
  17. #include "components/device_event_log/device_event_log.h"
  18. #include "crypto/random.h"
  19. #include "device/fido/hid/fido_hid_message.h"
  20. namespace device {
  21. // U2F devices only provide a single report so specify a report ID of 0 here.
  22. static constexpr uint8_t kReportId = 0x00;
  23. static constexpr uint8_t kWinkCapability = 0x01;
  24. FidoHidDevice::FidoHidDevice(device::mojom::HidDeviceInfoPtr device_info,
  25. device::mojom::HidManager* hid_manager)
  26. : FidoDevice(),
  27. output_report_size_(device_info->max_output_report_size),
  28. hid_manager_(hid_manager),
  29. device_info_(std::move(device_info)) {
  30. DCHECK_GE(std::numeric_limits<decltype(output_report_size_)>::max(),
  31. device_info_->max_output_report_size);
  32. // These limits on the report size are enforced in fido_hid_discovery.cc.
  33. DCHECK_LT(kHidInitPacketHeaderSize, output_report_size_);
  34. DCHECK_GE(kHidMaxPacketSize, output_report_size_);
  35. }
  36. FidoHidDevice::~FidoHidDevice() = default;
  37. FidoDevice::CancelToken FidoHidDevice::DeviceTransact(
  38. std::vector<uint8_t> command,
  39. DeviceCallback callback) {
  40. const CancelToken token = next_cancel_token_++;
  41. const auto command_type = supported_protocol() == ProtocolVersion::kCtap2
  42. ? FidoHidDeviceCommand::kCbor
  43. : FidoHidDeviceCommand::kMsg;
  44. pending_transactions_.emplace_back(command_type, std::move(command),
  45. std::move(callback), token);
  46. Transition();
  47. return token;
  48. }
  49. void FidoHidDevice::Cancel(CancelToken token) {
  50. if (state_ == State::kBusy && current_token_ == token) {
  51. // Sending a Cancel request should cause the outstanding request to return
  52. // with CTAP2_ERR_KEEPALIVE_CANCEL if the device is CTAP2. That error will
  53. // cause the request to complete in the usual way. U2F doesn't have a cancel
  54. // message, but U2F devices are not expected to block on requests and also
  55. // no U2F command alters state in a meaningful way, as CTAP2 commands do.
  56. if (supported_protocol() != ProtocolVersion::kCtap2) {
  57. return;
  58. }
  59. switch (busy_state_) {
  60. case BusyState::kWriting:
  61. // Send a cancelation message once the transmission is complete.
  62. busy_state_ = BusyState::kWritingPendingCancel;
  63. break;
  64. case BusyState::kWritingPendingCancel:
  65. // A cancelation message is already scheduled.
  66. break;
  67. case BusyState::kWaiting:
  68. // Waiting for reply. Send cancelation message.
  69. busy_state_ = BusyState::kReading;
  70. WriteCancel();
  71. break;
  72. case BusyState::kReading:
  73. // Have either already sent a cancel message, or else have started
  74. // reading the response.
  75. break;
  76. }
  77. return;
  78. }
  79. // The request with the given |token| isn't the current request. Remove it
  80. // from the list of pending requests if found.
  81. for (auto it = pending_transactions_.begin();
  82. it != pending_transactions_.end(); it++) {
  83. if (it->token != token) {
  84. continue;
  85. }
  86. auto callback = std::move(it->callback);
  87. pending_transactions_.erase(it);
  88. std::vector<uint8_t> cancel_reply = {
  89. static_cast<uint8_t>(CtapDeviceResponseCode::kCtap2ErrKeepAliveCancel)};
  90. std::move(callback).Run(std::move(cancel_reply));
  91. break;
  92. }
  93. }
  94. void FidoHidDevice::Transition(absl::optional<State> next_state) {
  95. if (next_state) {
  96. state_ = *next_state;
  97. }
  98. switch (state_) {
  99. case State::kInit:
  100. state_ = State::kConnecting;
  101. ArmTimeout();
  102. Connect(base::BindOnce(&FidoHidDevice::OnConnect,
  103. weak_factory_.GetWeakPtr()));
  104. break;
  105. case State::kReady: {
  106. DCHECK(!pending_transactions_.empty());
  107. // Some devices fail when sent a wink command immediately followed by a
  108. // CBOR command. Only try to wink if device claims support and it is
  109. // required to signal user presence.
  110. if (pending_transactions_.front().command_type ==
  111. FidoHidDeviceCommand::kWink &&
  112. !(capabilities_ & kWinkCapability && needs_explicit_wink_)) {
  113. DeviceCallback pending_cb =
  114. std::move(pending_transactions_.front().callback);
  115. pending_transactions_.pop_front();
  116. std::move(pending_cb).Run(absl::nullopt);
  117. break;
  118. }
  119. state_ = State::kBusy;
  120. busy_state_ = BusyState::kWriting;
  121. ArmTimeout();
  122. // Write message to the device.
  123. current_token_ = pending_transactions_.front().token;
  124. auto maybe_message(FidoHidMessage::Create(
  125. channel_id_, pending_transactions_.front().command_type,
  126. output_report_size_,
  127. std::move(pending_transactions_.front().command)));
  128. DCHECK(maybe_message);
  129. WriteMessage(std::move(*maybe_message));
  130. break;
  131. }
  132. case State::kConnecting:
  133. case State::kBusy:
  134. break;
  135. case State::kDeviceError:
  136. case State::kMsgError:
  137. base::WeakPtr<FidoHidDevice> self = weak_factory_.GetWeakPtr();
  138. // Executing callbacks may free |this|. Check |self| first.
  139. while (self && !pending_transactions_.empty()) {
  140. // Respond to any pending requests.
  141. DeviceCallback pending_cb =
  142. std::move(pending_transactions_.front().callback);
  143. pending_transactions_.pop_front();
  144. std::move(pending_cb).Run(absl::nullopt);
  145. }
  146. break;
  147. }
  148. }
  149. FidoHidDevice::PendingTransaction::PendingTransaction(
  150. FidoHidDeviceCommand command_type,
  151. std::vector<uint8_t> in_command,
  152. DeviceCallback in_callback,
  153. CancelToken in_token)
  154. : command_type(command_type),
  155. command(std::move(in_command)),
  156. callback(std::move(in_callback)),
  157. token(in_token) {}
  158. FidoHidDevice::PendingTransaction::~PendingTransaction() = default;
  159. void FidoHidDevice::Connect(
  160. device::mojom::HidManager::ConnectCallback callback) {
  161. DCHECK(hid_manager_);
  162. hid_manager_->Connect(device_info_->guid,
  163. /*connection_client=*/mojo::NullRemote(),
  164. /*watcher=*/mojo::NullRemote(),
  165. /*allow_protected_reports=*/true,
  166. /*allow_fido_reports=*/true, std::move(callback));
  167. }
  168. void FidoHidDevice::OnConnect(
  169. mojo::PendingRemote<device::mojom::HidConnection> connection) {
  170. timeout_callback_.Cancel();
  171. if (!connection) {
  172. Transition(State::kDeviceError);
  173. return;
  174. }
  175. connection_ = base::MakeRefCounted<RefCountedHidConnection>(
  176. mojo::Remote<mojom::HidConnection>(std::move(connection)));
  177. // Send random nonce to device to verify received message.
  178. std::vector<uint8_t> nonce(8);
  179. crypto::RandBytes(nonce.data(), nonce.size());
  180. DCHECK_EQ(State::kConnecting, state_);
  181. ArmTimeout();
  182. FidoHidInitPacket init(kHidBroadcastChannel, FidoHidDeviceCommand::kInit,
  183. nonce, nonce.size());
  184. std::vector<uint8_t> init_packet = init.GetSerializedData();
  185. init_packet.resize(output_report_size_, 0);
  186. connection_->data->Write(
  187. kReportId, std::move(init_packet),
  188. base::BindOnce(&FidoHidDevice::OnInitWriteComplete,
  189. weak_factory_.GetWeakPtr(), std::move(nonce)));
  190. }
  191. void FidoHidDevice::OnInitWriteComplete(std::vector<uint8_t> nonce,
  192. bool success) {
  193. if (state_ == State::kDeviceError) {
  194. return;
  195. }
  196. if (!success) {
  197. Transition(State::kDeviceError);
  198. }
  199. connection_->data->Read(base::BindOnce(&FidoHidDevice::OnPotentialInitReply,
  200. weak_factory_.GetWeakPtr(),
  201. std::move(nonce)));
  202. }
  203. // ParseInitReply parses a potential reply to a U2FHID_INIT message. If the
  204. // reply matches the given nonce then the assigned channel ID is returned.
  205. absl::optional<uint32_t> FidoHidDevice::ParseInitReply(
  206. const std::vector<uint8_t>& nonce,
  207. const std::vector<uint8_t>& buf) {
  208. auto message = FidoHidMessage::CreateFromSerializedData(buf);
  209. if (!message ||
  210. // Any reply will be sent to the broadcast channel.
  211. message->channel_id() != kHidBroadcastChannel ||
  212. // Init replies must fit in a single frame.
  213. !message->MessageComplete() ||
  214. message->cmd() != FidoHidDeviceCommand::kInit) {
  215. return absl::nullopt;
  216. }
  217. auto payload = message->GetMessagePayload();
  218. // The channel allocation response is defined as:
  219. // 0: 8 byte nonce
  220. // 8: 4 byte channel id
  221. // 12: Protocol version id
  222. // 13: Major device version
  223. // 14: Minor device version
  224. // 15: Build device version
  225. // 16: Capabilities
  226. DCHECK_EQ(8u, nonce.size());
  227. if (payload.size() != 17 || memcmp(nonce.data(), payload.data(), 8) != 0) {
  228. return absl::nullopt;
  229. }
  230. capabilities_ = payload[16];
  231. return static_cast<uint32_t>(payload[8]) << 24 |
  232. static_cast<uint32_t>(payload[9]) << 16 |
  233. static_cast<uint32_t>(payload[10]) << 8 |
  234. static_cast<uint32_t>(payload[11]);
  235. }
  236. void FidoHidDevice::OnPotentialInitReply(
  237. std::vector<uint8_t> nonce,
  238. bool success,
  239. uint8_t report_id,
  240. const absl::optional<std::vector<uint8_t>>& buf) {
  241. if (state_ == State::kDeviceError) {
  242. return;
  243. }
  244. if (!success) {
  245. Transition(State::kDeviceError);
  246. return;
  247. }
  248. DCHECK(buf);
  249. absl::optional<uint32_t> maybe_channel_id = ParseInitReply(nonce, *buf);
  250. if (!maybe_channel_id) {
  251. // This instance of Chromium may not be the only process communicating with
  252. // this HID device, but all processes will see all the messages from the
  253. // device. Thus it is not an error to observe unexpected messages from the
  254. // device and they are ignored.
  255. connection_->data->Read(base::BindOnce(&FidoHidDevice::OnPotentialInitReply,
  256. weak_factory_.GetWeakPtr(),
  257. std::move(nonce)));
  258. return;
  259. }
  260. timeout_callback_.Cancel();
  261. channel_id_ = *maybe_channel_id;
  262. Transition(State::kReady);
  263. }
  264. void FidoHidDevice::WriteMessage(FidoHidMessage message) {
  265. DCHECK_EQ(State::kBusy, state_);
  266. DCHECK(message.NumPackets() > 0);
  267. auto packet = message.PopNextPacket();
  268. DCHECK_LE(packet.size(), output_report_size_);
  269. packet.resize(output_report_size_, 0);
  270. connection_->data->Write(
  271. kReportId, packet,
  272. base::BindOnce(&FidoHidDevice::PacketWritten, weak_factory_.GetWeakPtr(),
  273. std::move(message)));
  274. }
  275. void FidoHidDevice::PacketWritten(FidoHidMessage message, bool success) {
  276. if (state_ == State::kDeviceError) {
  277. return;
  278. }
  279. DCHECK_EQ(State::kBusy, state_);
  280. if (!success) {
  281. Transition(State::kDeviceError);
  282. return;
  283. }
  284. if (message.NumPackets() > 0) {
  285. WriteMessage(std::move(message));
  286. return;
  287. }
  288. switch (busy_state_) {
  289. case BusyState::kWriting:
  290. busy_state_ = BusyState::kWaiting;
  291. ReadMessage();
  292. break;
  293. case BusyState::kWritingPendingCancel:
  294. busy_state_ = BusyState::kReading;
  295. WriteCancel();
  296. ReadMessage();
  297. break;
  298. default:
  299. NOTREACHED();
  300. }
  301. }
  302. void FidoHidDevice::ReadMessage() {
  303. connection_->data->Read(
  304. base::BindOnce(&FidoHidDevice::OnRead, weak_factory_.GetWeakPtr()));
  305. }
  306. void FidoHidDevice::OnRead(bool success,
  307. uint8_t report_id,
  308. const absl::optional<std::vector<uint8_t>>& buf) {
  309. if (state_ == State::kDeviceError) {
  310. return;
  311. }
  312. DCHECK_EQ(State::kBusy, state_);
  313. if (!success) {
  314. Transition(State::kDeviceError);
  315. return;
  316. }
  317. DCHECK(buf);
  318. auto message = FidoHidMessage::CreateFromSerializedData(*buf);
  319. if (!message) {
  320. Transition(State::kDeviceError);
  321. return;
  322. }
  323. if (!message->MessageComplete()) {
  324. // Continue reading additional packets.
  325. connection_->data->Read(base::BindOnce(&FidoHidDevice::OnReadContinuation,
  326. weak_factory_.GetWeakPtr(),
  327. std::move(*message)));
  328. return;
  329. }
  330. // Received a message from a different channel, so try again.
  331. if (channel_id_ != message->channel_id()) {
  332. ReadMessage();
  333. return;
  334. }
  335. // If received HID packet is a keep-alive message then reset the timeout and
  336. // read again.
  337. if (supported_protocol() == ProtocolVersion::kCtap2 &&
  338. message->cmd() == FidoHidDeviceCommand::kKeepAlive) {
  339. timeout_callback_.Cancel();
  340. ArmTimeout();
  341. ReadMessage();
  342. return;
  343. }
  344. switch (busy_state_) {
  345. case BusyState::kWaiting:
  346. busy_state_ = BusyState::kReading;
  347. break;
  348. case BusyState::kReading:
  349. break;
  350. default:
  351. NOTREACHED();
  352. }
  353. MessageReceived(std::move(*message));
  354. }
  355. void FidoHidDevice::OnReadContinuation(
  356. FidoHidMessage message,
  357. bool success,
  358. uint8_t report_id,
  359. const absl::optional<std::vector<uint8_t>>& buf) {
  360. if (state_ == State::kDeviceError) {
  361. return;
  362. }
  363. if (!success) {
  364. Transition(State::kDeviceError);
  365. return;
  366. }
  367. DCHECK(buf);
  368. if (!message.AddContinuationPacket(*buf)) {
  369. Transition(State::kDeviceError);
  370. return;
  371. }
  372. if (!message.MessageComplete()) {
  373. connection_->data->Read(base::BindOnce(&FidoHidDevice::OnReadContinuation,
  374. weak_factory_.GetWeakPtr(),
  375. std::move(message)));
  376. return;
  377. }
  378. // Received a message from a different channel, so try again.
  379. if (channel_id_ != message.channel_id()) {
  380. ReadMessage();
  381. return;
  382. }
  383. MessageReceived(std::move(message));
  384. }
  385. void FidoHidDevice::MessageReceived(FidoHidMessage message) {
  386. timeout_callback_.Cancel();
  387. const FidoHidDeviceCommand cmd = message.cmd();
  388. std::vector<uint8_t> response = message.GetMessagePayload();
  389. constexpr FidoHidDeviceCommand kValidCommands[] = {
  390. FidoHidDeviceCommand::kMsg, FidoHidDeviceCommand::kCbor,
  391. FidoHidDeviceCommand::kWink, FidoHidDeviceCommand::kError};
  392. if (!base::Contains(kValidCommands, cmd)) {
  393. FIDO_LOG(ERROR) << "Unknown CTAPHID command: " << static_cast<int>(cmd)
  394. << " " << base::HexEncode(response.data(), response.size());
  395. Transition(State::kDeviceError);
  396. return;
  397. }
  398. if (cmd == FidoHidDeviceCommand::kError) {
  399. if (response.size() != 1) {
  400. FIDO_LOG(ERROR) << "Invalid CTAPHID_ERROR payload: "
  401. << base::HexEncode(response.data(), response.size());
  402. Transition(State::kDeviceError);
  403. return;
  404. }
  405. // HID transport layer error constants that are returned to the client.
  406. // https://fidoalliance.org/specs/fido-v2.0-rd-20170927/fido-client-to-authenticator-protocol-v2.0-rd-20170927.html#ctaphid-commands
  407. enum class HidErrorConstant : uint8_t {
  408. kInvalidCommand = 0x01,
  409. kInvalidParameter = 0x02,
  410. kInvalidLength = 0x03,
  411. kMessageTimeout = 0x05,
  412. kChannelBusy = 0x06,
  413. // (Other errors omitted.)
  414. };
  415. switch (static_cast<HidErrorConstant>(response[0])) {
  416. case HidErrorConstant::kInvalidCommand:
  417. case HidErrorConstant::kInvalidParameter:
  418. case HidErrorConstant::kInvalidLength:
  419. Transition(State::kMsgError);
  420. break;
  421. case HidErrorConstant::kMessageTimeout:
  422. Transition(State::kDeviceError);
  423. break;
  424. case HidErrorConstant::kChannelBusy:
  425. // Retry the pending transaction after a short delay. |state_| is still
  426. // |State::kBusy|, so no other transaction will run in the meantime.
  427. DCHECK_EQ(State::kBusy, state_);
  428. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  429. FROM_HERE,
  430. base::BindOnce(&FidoHidDevice::RetryAfterChannelBusy,
  431. weak_factory_.GetWeakPtr()),
  432. base::Milliseconds(100));
  433. break;
  434. default:
  435. FIDO_LOG(DEBUG) << "Invalid CTAPHID_ERROR "
  436. << static_cast<int>(response[0]);
  437. Transition(State::kDeviceError);
  438. break;
  439. }
  440. return;
  441. }
  442. DCHECK(!pending_transactions_.empty());
  443. auto callback = std::move(pending_transactions_.front().callback);
  444. pending_transactions_.pop_front();
  445. current_token_ = FidoDevice::kInvalidCancelToken;
  446. base::WeakPtr<FidoHidDevice> self = weak_factory_.GetWeakPtr();
  447. // The callback may call back into this object thus |state_| is set ahead of
  448. // time.
  449. state_ = State::kReady;
  450. std::move(callback).Run(std::move(response));
  451. // Executing |callback| may have freed |this|. Check |self| first.
  452. if (self && !pending_transactions_.empty()) {
  453. Transition();
  454. }
  455. }
  456. void FidoHidDevice::RetryAfterChannelBusy() {
  457. DCHECK(!pending_transactions_.empty());
  458. DCHECK_EQ(State::kBusy, state_);
  459. Transition(State::kReady);
  460. }
  461. void FidoHidDevice::TryWink(base::OnceClosure callback) {
  462. const CancelToken token = next_cancel_token_++;
  463. pending_transactions_.emplace_back(
  464. FidoHidDeviceCommand::kWink, std::vector<uint8_t>(),
  465. base::BindOnce(
  466. [](base::OnceClosure cb, absl::optional<std::vector<uint8_t>> data) {
  467. std::move(cb).Run();
  468. },
  469. std::move(callback)),
  470. token);
  471. Transition();
  472. }
  473. void FidoHidDevice::ArmTimeout() {
  474. DCHECK(timeout_callback_.IsCancelled());
  475. timeout_callback_.Reset(
  476. base::BindOnce(&FidoHidDevice::OnTimeout, weak_factory_.GetWeakPtr()));
  477. // Setup timeout task for 3 seconds.
  478. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  479. FROM_HERE, timeout_callback_.callback(), kDeviceTimeout);
  480. }
  481. void FidoHidDevice::OnTimeout() {
  482. FIDO_LOG(ERROR) << "FIDO HID device timeout for " << GetId();
  483. Transition(State::kDeviceError);
  484. }
  485. // WriteCancelComplete is the callback from writing a cancellation message. Its
  486. // primary purpose is to hold a reference to the HidConnection so that the write
  487. // doesn't get discarded. It's a static function because it may be called after
  488. // the destruction of the |FidoHidDevice| that created it.
  489. // static
  490. void FidoHidDevice::WriteCancelComplete(
  491. scoped_refptr<FidoHidDevice::RefCountedHidConnection> connection,
  492. bool success) {
  493. if (!success) {
  494. FIDO_LOG(ERROR) << "Failed to write Cancel message";
  495. }
  496. }
  497. void FidoHidDevice::WriteCancel() {
  498. FidoHidInitPacket cancel(channel_id_, FidoHidDeviceCommand::kCancel, {},
  499. /*payload_length=*/0);
  500. std::vector<uint8_t> cancel_packet = cancel.GetSerializedData();
  501. DCHECK_LE(cancel_packet.size(), output_report_size_);
  502. cancel_packet.resize(output_report_size_, 0);
  503. // This |FidoHidDevice| might be destructed immediately after this call. On
  504. // Windows, pending writes are dropped when the |HidConnection| is destructed.
  505. // Since it's important that the Cancel message actually gets written, this
  506. // callback takes a reference to the HidConnection to hold it open at least
  507. // until the Write completes.
  508. //
  509. // Note that, if this object is in the process of a multi-packet write that
  510. // will eventually be canceled, the packet sequence will still be truncated
  511. // when this object is destroyed. Fixing that would involve reference counting
  512. // this object itself.
  513. connection_->data->Write(kReportId, std::move(cancel_packet),
  514. base::BindOnce(WriteCancelComplete, connection_));
  515. }
  516. // VidPidToString returns the device's vendor and product IDs as formatted by
  517. // the lsusb utility.
  518. static std::string VidPidToString(const mojom::HidDeviceInfoPtr& device_info) {
  519. static_assert(sizeof(device_info->vendor_id) == 2,
  520. "vendor_id must be uint16_t");
  521. static_assert(sizeof(device_info->product_id) == 2,
  522. "product_id must be uint16_t");
  523. uint16_t vendor_id = ((device_info->vendor_id & 0xff) << 8) |
  524. ((device_info->vendor_id & 0xff00) >> 8);
  525. uint16_t product_id = ((device_info->product_id & 0xff) << 8) |
  526. ((device_info->product_id & 0xff00) >> 8);
  527. return base::ToLowerASCII(base::HexEncode(&vendor_id, 2) + ":" +
  528. base::HexEncode(&product_id, 2));
  529. }
  530. std::string FidoHidDevice::GetDisplayName() const {
  531. return "usb-" + VidPidToString(device_info_);
  532. }
  533. std::string FidoHidDevice::GetId() const {
  534. return GetIdForDevice(*device_info_);
  535. }
  536. FidoTransportProtocol FidoHidDevice::DeviceTransport() const {
  537. return FidoTransportProtocol::kUsbHumanInterfaceDevice;
  538. }
  539. void FidoHidDevice::DiscoverSupportedProtocolAndDeviceInfo(
  540. base::OnceClosure done) {
  541. // The following devices cannot handle GetInfo messages.
  542. static constexpr auto kForceU2fCompatibilitySet =
  543. base::MakeFixedFlatSet<base::StringPiece>({
  544. "10c4:8acf", // U2F Zero
  545. "20a0:4287", // Nitrokey FIDO U2F
  546. });
  547. if (base::Contains(kForceU2fCompatibilitySet, VidPidToString(device_info_))) {
  548. supported_protocol_ = ProtocolVersion::kU2f;
  549. DCHECK(SupportedProtocolIsInitialized());
  550. std::move(done).Run();
  551. return;
  552. }
  553. FidoDevice::DiscoverSupportedProtocolAndDeviceInfo(std::move(done));
  554. }
  555. // static
  556. std::string FidoHidDevice::GetIdForDevice(
  557. const device::mojom::HidDeviceInfo& device_info) {
  558. return "hid:" + device_info.guid;
  559. }
  560. base::WeakPtr<FidoDevice> FidoHidDevice::GetWeakPtr() {
  561. return weak_factory_.GetWeakPtr();
  562. }
  563. } // namespace device