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 
5 #ifndef DEVICE_FIDO_GET_ASSERTION_TASK_H_
6 #define DEVICE_FIDO_GET_ASSERTION_TASK_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/callback.h"
14 #include "base/component_export.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/optional.h"
18 #include "device/fido/ctap_get_assertion_request.h"
19 #include "device/fido/ctap_make_credential_request.h"
20 #include "device/fido/device_operation.h"
21 #include "device/fido/fido_constants.h"
22 #include "device/fido/fido_task.h"
23 
24 namespace cbor {
25 class Value;
26 }
27 
28 namespace device {
29 
30 class AuthenticatorGetAssertionResponse;
31 class AuthenticatorMakeCredentialResponse;
32 
33 // Represents per device sign operation on CTAP1/CTAP2 devices.
34 // https://fidoalliance.org/specs/fido-v2.0-rd-20161004/fido-client-to-authenticator-protocol-v2.0-rd-20161004.html#authenticatorgetassertion
COMPONENT_EXPORT(DEVICE_FIDO)35 class COMPONENT_EXPORT(DEVICE_FIDO) GetAssertionTask : public FidoTask {
36  public:
37   using GetAssertionTaskCallback = base::OnceCallback<void(
38       CtapDeviceResponseCode,
39       base::Optional<AuthenticatorGetAssertionResponse>)>;
40   using SignOperation = DeviceOperation<CtapGetAssertionRequest,
41                                         AuthenticatorGetAssertionResponse>;
42   using RegisterOperation =
43       DeviceOperation<CtapMakeCredentialRequest,
44                       AuthenticatorMakeCredentialResponse>;
45 
46   GetAssertionTask(FidoDevice* device,
47                    CtapGetAssertionRequest request,
48                    GetAssertionTaskCallback callback);
49   ~GetAssertionTask() override;
50 
51   // FidoTask:
52   void Cancel() override;
53 
54   // StringFixupPredicate indicates which fields of a GetAssertion
55   // response may contain truncated UTF-8 strings. See
56   // |Ctap2DeviceOperation::CBORPathPredicate|.
57   static bool StringFixupPredicate(const std::vector<const cbor::Value*>& path);
58 
59  private:
60   // FidoTask:
61   void StartTask() override;
62 
63   void GetAssertion();
64   void U2fSign();
65 
66   CtapGetAssertionRequest NextSilentRequest();
67 
68   // HandleResponse is the callback to a CTAP2 assertion request that requested
69   // user-presence.
70   void HandleResponse(
71       CtapDeviceResponseCode response_code,
72       base::Optional<AuthenticatorGetAssertionResponse> response_data);
73 
74   // HandleResponseToSilentRequest is a callback to a request without user-
75   // presence requested used to silently probe credentials from the allow list.
76   void HandleResponseToSilentRequest(
77       CtapDeviceResponseCode response_code,
78       base::Optional<AuthenticatorGetAssertionResponse> response_data);
79 
80   // HandleDummyMakeCredentialComplete is the callback for the dummy credential
81   // creation request that will be triggered, if needed, to get a touch.
82   void HandleDummyMakeCredentialComplete(
83       CtapDeviceResponseCode response_code,
84       base::Optional<AuthenticatorMakeCredentialResponse> response_data);
85 
86   CtapGetAssertionRequest request_;
87   std::vector<std::vector<PublicKeyCredentialDescriptor>> allow_list_batches_;
88   size_t current_allow_list_batch_ = 0;
89 
90   std::unique_ptr<SignOperation> sign_operation_;
91   std::unique_ptr<RegisterOperation> dummy_register_operation_;
92   GetAssertionTaskCallback callback_;
93 
94   bool canceled_ = false;
95 
96   base::WeakPtrFactory<GetAssertionTask> weak_factory_{this};
97 
98   DISALLOW_COPY_AND_ASSIGN(GetAssertionTask);
99 };
100 
101 }  // namespace device
102 
103 #endif  // DEVICE_FIDO_GET_ASSERTION_TASK_H_
104