make_credential_task.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2018 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. #ifndef DEVICE_FIDO_MAKE_CREDENTIAL_TASK_H_
  5. #define DEVICE_FIDO_MAKE_CREDENTIAL_TASK_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/component_export.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "device/fido/authenticator_get_assertion_response.h"
  13. #include "device/fido/authenticator_make_credential_response.h"
  14. #include "device/fido/ctap_get_assertion_request.h"
  15. #include "device/fido/ctap_make_credential_request.h"
  16. #include "device/fido/device_operation.h"
  17. #include "device/fido/fido_constants.h"
  18. #include "device/fido/fido_task.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace device {
  21. // Represents one register operation on one single CTAP 1.0/2.0 authenticator.
  22. // https://fidoalliance.org/specs/fido-v2.0-rd-20161004/fido-client-to-authenticator-protocol-v2.0-rd-20161004.html#authenticatormakecredential
  23. class COMPONENT_EXPORT(DEVICE_FIDO) MakeCredentialTask : public FidoTask {
  24. public:
  25. using MakeCredentialTaskCallback = base::OnceCallback<void(
  26. CtapDeviceResponseCode,
  27. absl::optional<AuthenticatorMakeCredentialResponse>)>;
  28. using SignOperation = DeviceOperation<CtapGetAssertionRequest,
  29. AuthenticatorGetAssertionResponse>;
  30. using RegisterOperation =
  31. DeviceOperation<CtapMakeCredentialRequest,
  32. AuthenticatorMakeCredentialResponse>;
  33. MakeCredentialTask(FidoDevice* device,
  34. CtapMakeCredentialRequest request,
  35. MakeCredentialOptions options,
  36. MakeCredentialTaskCallback callback);
  37. MakeCredentialTask(const MakeCredentialTask&) = delete;
  38. MakeCredentialTask& operator=(const MakeCredentialTask&) = delete;
  39. ~MakeCredentialTask() override;
  40. // GetTouchRequest returns a request that will cause a device to flash and
  41. // wait for a touch.
  42. static CtapMakeCredentialRequest GetTouchRequest(const FidoDevice* device);
  43. // WillUseCTAP2 returns true iff |MakeCredentialTask| will use the CTAP2
  44. // protocol to satisfy the given |request|.
  45. static bool WillUseCTAP2(const FidoDevice* device,
  46. const CtapMakeCredentialRequest& request,
  47. const MakeCredentialOptions& options);
  48. // FidoTask:
  49. void Cancel() override;
  50. private:
  51. // FidoTask:
  52. void StartTask() final;
  53. void MakeCredential();
  54. CtapGetAssertionRequest NextSilentRequest();
  55. void HandleResponseToSilentSignRequest(
  56. CtapDeviceResponseCode response_code,
  57. absl::optional<AuthenticatorGetAssertionResponse> response_data);
  58. void HandleResponseToDummyTouch(
  59. CtapDeviceResponseCode response_code,
  60. absl::optional<AuthenticatorMakeCredentialResponse> response_data);
  61. void U2fRegister();
  62. void MaybeRevertU2fFallback(
  63. CtapDeviceResponseCode status,
  64. absl::optional<AuthenticatorMakeCredentialResponse> response);
  65. CtapMakeCredentialRequest request_;
  66. MakeCredentialOptions options_;
  67. std::vector<std::vector<PublicKeyCredentialDescriptor>> exclude_list_batches_;
  68. size_t current_exclude_list_batch_ = 0;
  69. std::unique_ptr<RegisterOperation> register_operation_;
  70. std::unique_ptr<SignOperation> silent_sign_operation_;
  71. MakeCredentialTaskCallback callback_;
  72. bool canceled_ = false;
  73. base::WeakPtrFactory<MakeCredentialTask> weak_factory_{this};
  74. };
  75. // FilterAndBatchCredentialDescriptors splits a list of
  76. // PublicKeyCredentialDescriptors such that each chunk is guaranteed to fit into
  77. // an allowList parameter of a GetAssertion request for the given |device|.
  78. //
  79. // |device| must be a fully initialized CTAP2 device, i.e. its device_info()
  80. // method must return an AuthenticatorGetInfoResponse.
  81. //
  82. // The result will never be empty. It will, at least, contain a single empty
  83. // vector.
  84. std::vector<std::vector<PublicKeyCredentialDescriptor>>
  85. FilterAndBatchCredentialDescriptors(
  86. const std::vector<PublicKeyCredentialDescriptor>& in,
  87. const FidoDevice& device);
  88. } // namespace device
  89. #endif // DEVICE_FIDO_MAKE_CREDENTIAL_TASK_H_