fido_device.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/fido_device.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/containers/contains.h"
  8. #include "components/device_event_log/device_event_log.h"
  9. #include "device/fido/device_response_converter.h"
  10. #include "device/fido/fido_constants.h"
  11. namespace device {
  12. FidoDevice::FidoDevice() = default;
  13. FidoDevice::~FidoDevice() = default;
  14. void FidoDevice::TryWink(base::OnceClosure callback) {
  15. std::move(callback).Run();
  16. }
  17. std::string FidoDevice::GetDisplayName() const {
  18. return GetId();
  19. }
  20. bool FidoDevice::IsInPairingMode() const {
  21. NOTREACHED();
  22. return false;
  23. }
  24. bool FidoDevice::IsPaired() const {
  25. NOTREACHED();
  26. return false;
  27. }
  28. bool FidoDevice::RequiresBlePairingPin() const {
  29. NOTREACHED();
  30. return true;
  31. }
  32. void FidoDevice::DiscoverSupportedProtocolAndDeviceInfo(
  33. base::OnceClosure done) {
  34. // Set the protocol version to CTAP2 for the purpose of sending the GetInfo
  35. // request. The correct value will be set in the callback based on the
  36. // device response.
  37. supported_protocol_ = ProtocolVersion::kCtap2;
  38. FIDO_LOG(DEBUG)
  39. << "Sending CTAP2 AuthenticatorGetInfo request to authenticator.";
  40. DeviceTransact(
  41. {static_cast<uint8_t>(CtapRequestCommand::kAuthenticatorGetInfo)},
  42. base::BindOnce(&FidoDevice::OnDeviceInfoReceived, GetWeakPtr(),
  43. std::move(done)));
  44. }
  45. bool FidoDevice::SupportedProtocolIsInitialized() {
  46. return (supported_protocol_ == ProtocolVersion::kU2f && !device_info_) ||
  47. (supported_protocol_ == ProtocolVersion::kCtap2 && device_info_);
  48. }
  49. void FidoDevice::OnDeviceInfoReceived(
  50. base::OnceClosure done,
  51. absl::optional<std::vector<uint8_t>> response) {
  52. // TODO(hongjunchoi): Add tests that verify this behavior.
  53. if (state_ == FidoDevice::State::kDeviceError)
  54. return;
  55. state_ = FidoDevice::State::kReady;
  56. absl::optional<AuthenticatorGetInfoResponse> get_info_response =
  57. response ? ReadCTAPGetInfoResponse(*response) : absl::nullopt;
  58. if (!get_info_response ||
  59. !base::Contains(get_info_response->versions, ProtocolVersion::kCtap2)) {
  60. supported_protocol_ = ProtocolVersion::kU2f;
  61. needs_explicit_wink_ = true;
  62. FIDO_LOG(DEBUG) << "The device only supports the U2F protocol.";
  63. } else {
  64. supported_protocol_ = ProtocolVersion::kCtap2;
  65. device_info_ = std::move(*get_info_response);
  66. FIDO_LOG(DEBUG) << "The device supports the CTAP2 protocol.";
  67. }
  68. std::move(done).Run();
  69. }
  70. void FidoDevice::SetDeviceInfo(AuthenticatorGetInfoResponse device_info) {
  71. device_info_ = std::move(device_info);
  72. }
  73. bool FidoDevice::NoSilentRequests() const {
  74. // caBLE devices do not support silent requests.
  75. const auto transport = DeviceTransport();
  76. return transport == FidoTransportProtocol::kHybrid ||
  77. transport == FidoTransportProtocol::kAndroidAccessory;
  78. }
  79. // static
  80. bool FidoDevice::IsStatusForUnrecognisedCredentialID(
  81. CtapDeviceResponseCode status) {
  82. return status == CtapDeviceResponseCode::kCtap2ErrInvalidCredential ||
  83. status == CtapDeviceResponseCode::kCtap2ErrNoCredentials ||
  84. status == CtapDeviceResponseCode::kCtap2ErrLimitExceeded ||
  85. status == CtapDeviceResponseCode::kCtap2ErrRequestTooLarge;
  86. }
  87. } // namespace device