forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfido_device_authenticator.cc
840 lines (732 loc) · 31.9 KB
/
fido_device_authenticator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/fido/fido_device_authenticator.h"
#include <numeric>
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "device/fido/authenticator_supported_options.h"
#include "device/fido/credential_management.h"
#include "device/fido/ctap_get_assertion_request.h"
#include "device/fido/ctap_make_credential_request.h"
#include "device/fido/features.h"
#include "device/fido/fido_device.h"
#include "device/fido/fido_parsing_utils.h"
#include "device/fido/get_assertion_task.h"
#include "device/fido/make_credential_task.h"
#include "device/fido/pin.h"
#include "device/fido/u2f_command_constructor.h"
namespace device {
namespace {
// Helper method for determining correct bio enrollment version.
BioEnrollmentRequest::Version GetBioEnrollmentRequestVersion(
const AuthenticatorSupportedOptions& options) {
DCHECK(options.bio_enrollment_availability_preview !=
AuthenticatorSupportedOptions::BioEnrollmentAvailability::
kNotSupported ||
options.bio_enrollment_availability !=
AuthenticatorSupportedOptions::BioEnrollmentAvailability::
kNotSupported);
return options.bio_enrollment_availability !=
AuthenticatorSupportedOptions::BioEnrollmentAvailability::
kNotSupported
? BioEnrollmentRequest::kDefault
: BioEnrollmentRequest::kPreview;
}
CredentialManagementRequest::Version GetCredentialManagementRequestVersion(
const AuthenticatorSupportedOptions& options) {
DCHECK(options.supports_credential_management_preview ||
options.supports_credential_management);
return options.supports_credential_management
? CredentialManagementRequest::kDefault
: CredentialManagementRequest::kPreview;
}
uint8_t PermissionsToByte(const std::vector<pin::Permissions>& permissions) {
return std::accumulate(permissions.begin(), permissions.end(), 0,
[](uint8_t byte, pin::Permissions flag) {
return byte |= static_cast<uint8_t>(flag);
});
}
} // namespace
FidoDeviceAuthenticator::FidoDeviceAuthenticator(
std::unique_ptr<FidoDevice> device)
: device_(std::move(device)) {}
FidoDeviceAuthenticator::~FidoDeviceAuthenticator() = default;
void FidoDeviceAuthenticator::InitializeAuthenticator(
base::OnceClosure callback) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(
&FidoDevice::DiscoverSupportedProtocolAndDeviceInfo,
device()->GetWeakPtr(),
base::BindOnce(&FidoDeviceAuthenticator::InitializeAuthenticatorDone,
weak_factory_.GetWeakPtr(), std::move(callback))));
}
void FidoDeviceAuthenticator::InitializeAuthenticatorDone(
base::OnceClosure callback) {
DCHECK(!options_);
switch (device_->supported_protocol()) {
case ProtocolVersion::kU2f:
options_ = AuthenticatorSupportedOptions();
break;
case ProtocolVersion::kCtap2:
DCHECK(device_->device_info()) << "uninitialized device";
options_ = device_->device_info()->options;
break;
case ProtocolVersion::kUnknown:
NOTREACHED() << "uninitialized device";
options_ = AuthenticatorSupportedOptions();
}
std::move(callback).Run();
}
void FidoDeviceAuthenticator::MakeCredential(CtapMakeCredentialRequest request,
MakeCredentialCallback callback) {
// If the authenticator has UV configured then UV will be required in
// order to create a credential (as specified by CTAP 2.0), even if
// user-verification is "discouraged". However, if the request is U2F-only
// then that doesn't apply and UV must be set to discouraged so that the
// request can be translated to U2F.
if (!request.pin_auth &&
options_->user_verification_availability ==
AuthenticatorSupportedOptions::UserVerificationAvailability::
kSupportedAndConfigured &&
!request.is_u2f_only) {
request.user_verification = UserVerificationRequirement::kRequired;
} else {
request.user_verification = UserVerificationRequirement::kDiscouraged;
}
RunTask<MakeCredentialTask, AuthenticatorMakeCredentialResponse,
CtapMakeCredentialRequest>(std::move(request), std::move(callback));
}
void FidoDeviceAuthenticator::GetAssertion(CtapGetAssertionRequest request,
GetAssertionCallback callback) {
if (!request.pin_auth &&
options_->user_verification_availability ==
AuthenticatorSupportedOptions::UserVerificationAvailability::
kSupportedAndConfigured &&
request.user_verification != UserVerificationRequirement::kDiscouraged) {
request.user_verification = UserVerificationRequirement::kRequired;
} else {
request.user_verification = UserVerificationRequirement::kDiscouraged;
}
RunTask<GetAssertionTask, AuthenticatorGetAssertionResponse,
CtapGetAssertionRequest>(std::move(request), std::move(callback));
}
void FidoDeviceAuthenticator::GetNextAssertion(GetAssertionCallback callback) {
RunOperation<CtapGetNextAssertionRequest, AuthenticatorGetAssertionResponse>(
CtapGetNextAssertionRequest(), std::move(callback),
base::BindOnce(&ReadCTAPGetAssertionResponse),
GetAssertionTask::StringFixupPredicate);
}
void FidoDeviceAuthenticator::GetTouch(base::OnceCallback<void()> callback) {
MakeCredential(
MakeCredentialTask::GetTouchRequest(device()),
base::BindOnce(
[](std::string authenticator_id, base::OnceCallback<void()> callback,
CtapDeviceResponseCode status,
base::Optional<AuthenticatorMakeCredentialResponse>) {
// If the device didn't understand/process the request it may
// fail immediately. Rather than count that as a touch, ignore
// those cases completely.
if (status == CtapDeviceResponseCode::kSuccess ||
status == CtapDeviceResponseCode::kCtap2ErrPinNotSet ||
status == CtapDeviceResponseCode::kCtap2ErrPinInvalid ||
status == CtapDeviceResponseCode::kCtap2ErrPinAuthInvalid) {
std::move(callback).Run();
return;
}
FIDO_LOG(DEBUG) << "Ignoring status " << static_cast<int>(status)
<< " from " << authenticator_id;
},
GetId(), std::move(callback)));
}
void FidoDeviceAuthenticator::GetPinRetries(GetRetriesCallback callback) {
DCHECK(Options());
DCHECK(Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::kNotSupported);
RunOperation<pin::PinRetriesRequest, pin::RetriesResponse>(
pin::PinRetriesRequest(), std::move(callback),
base::BindOnce(&pin::RetriesResponse::ParsePinRetries));
}
void FidoDeviceAuthenticator::GetEphemeralKey(
GetEphemeralKeyCallback callback) {
if (cached_ephemeral_key_.has_value()) {
std::move(callback).Run(CtapDeviceResponseCode::kSuccess,
cached_ephemeral_key_);
return;
}
DCHECK(Options());
DCHECK(
Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::kNotSupported ||
Options()->supports_pin_uv_auth_token);
RunOperation<pin::KeyAgreementRequest, pin::KeyAgreementResponse>(
pin::KeyAgreementRequest(),
base::BindOnce(&FidoDeviceAuthenticator::OnHaveEphemeralKey,
weak_factory_.GetWeakPtr(), std::move(callback)),
base::BindOnce(&pin::KeyAgreementResponse::Parse));
}
void FidoDeviceAuthenticator::OnHaveEphemeralKey(
GetEphemeralKeyCallback callback,
CtapDeviceResponseCode status,
base::Optional<pin::KeyAgreementResponse> key) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(callback).Run(status, base::nullopt);
}
DCHECK(key.has_value());
cached_ephemeral_key_.emplace(std::move(key.value()));
std::move(callback).Run(CtapDeviceResponseCode::kSuccess,
cached_ephemeral_key_);
}
void FidoDeviceAuthenticator::GetPINToken(
std::string pin,
const std::vector<pin::Permissions>& permissions,
base::Optional<std::string> rp_id,
GetTokenCallback callback) {
DCHECK(Options());
DCHECK(Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::kNotSupported);
DCHECK_NE(permissions.size(), 0u);
DCHECK(!((base::Contains(permissions, pin::Permissions::kMakeCredential)) ||
base::Contains(permissions, pin::Permissions::kGetAssertion)) ||
rp_id);
GetEphemeralKey(base::BindOnce(
&FidoDeviceAuthenticator::OnHaveEphemeralKeyForGetPINToken,
weak_factory_.GetWeakPtr(), std::move(pin),
PermissionsToByte(permissions), std::move(rp_id), std::move(callback)));
}
void FidoDeviceAuthenticator::OnHaveEphemeralKeyForGetPINToken(
std::string pin,
uint8_t permissions,
base::Optional<std::string> rp_id,
GetTokenCallback callback,
CtapDeviceResponseCode status,
base::Optional<pin::KeyAgreementResponse> key) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(callback).Run(status, base::nullopt);
return;
}
if (Options()->supports_pin_uv_auth_token) {
pin::PinTokenWithPermissionsRequest request(pin, *key, permissions, rp_id);
std::array<uint8_t, 32> shared_key = request.shared_key();
RunOperation<pin::PinTokenWithPermissionsRequest, pin::TokenResponse>(
std::move(request), std::move(callback),
base::BindOnce(&pin::TokenResponse::Parse, std::move(shared_key)));
return;
}
pin::PinTokenRequest request(pin, *key);
std::array<uint8_t, 32> shared_key = request.shared_key();
RunOperation<pin::PinTokenRequest, pin::TokenResponse>(
std::move(request), std::move(callback),
base::BindOnce(&pin::TokenResponse::Parse, std::move(shared_key)));
}
void FidoDeviceAuthenticator::SetPIN(const std::string& pin,
SetPINCallback callback) {
DCHECK(Options());
DCHECK(Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::kNotSupported);
GetEphemeralKey(base::BindOnce(
&FidoDeviceAuthenticator::OnHaveEphemeralKeyForSetPIN,
weak_factory_.GetWeakPtr(), std::move(pin), std::move(callback)));
}
void FidoDeviceAuthenticator::OnHaveEphemeralKeyForSetPIN(
std::string pin,
SetPINCallback callback,
CtapDeviceResponseCode status,
base::Optional<pin::KeyAgreementResponse> key) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(callback).Run(status, base::nullopt);
return;
}
RunOperation<pin::SetRequest, pin::EmptyResponse>(
pin::SetRequest(pin, *key), std::move(callback),
base::BindOnce(&pin::EmptyResponse::Parse));
}
void FidoDeviceAuthenticator::ChangePIN(const std::string& old_pin,
const std::string& new_pin,
SetPINCallback callback) {
DCHECK(Options());
DCHECK(Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::kNotSupported);
GetEphemeralKey(
base::BindOnce(&FidoDeviceAuthenticator::OnHaveEphemeralKeyForChangePIN,
weak_factory_.GetWeakPtr(), std::move(old_pin),
std::move(new_pin), std::move(callback)));
}
void FidoDeviceAuthenticator::OnHaveEphemeralKeyForChangePIN(
std::string old_pin,
std::string new_pin,
SetPINCallback callback,
CtapDeviceResponseCode status,
base::Optional<pin::KeyAgreementResponse> key) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(callback).Run(status, base::nullopt);
return;
}
RunOperation<pin::ChangeRequest, pin::EmptyResponse>(
pin::ChangeRequest(old_pin, new_pin, *key), std::move(callback),
base::BindOnce(&pin::EmptyResponse::Parse));
}
FidoAuthenticator::MakeCredentialPINDisposition
FidoDeviceAuthenticator::WillNeedPINToMakeCredential(
const CtapMakeCredentialRequest& request,
const FidoRequestHandlerBase::Observer* observer) {
using ClientPinAvailability =
AuthenticatorSupportedOptions::ClientPinAvailability;
const auto device_support = Options()->client_pin_availability;
const bool can_collect_pin = observer && observer->SupportsPIN();
// Authenticators with built-in UV can use that.
if (Options()->user_verification_availability ==
AuthenticatorSupportedOptions::UserVerificationAvailability::
kSupportedAndConfigured) {
return device_support == ClientPinAvailability::kSupportedAndPinSet &&
can_collect_pin
? MakeCredentialPINDisposition::kUsePINForFallback
: MakeCredentialPINDisposition::kNoPIN;
}
// CTAP 2.0 requires a PIN for credential creation once a PIN has been set.
// Thus, if fallback to U2F isn't possible, a PIN will be needed if set.
const bool u2f_fallback_possible =
device()->device_info() &&
device()->device_info()->versions.contains(ProtocolVersion::kU2f) &&
IsConvertibleToU2fRegisterCommand(request);
if (device_support == ClientPinAvailability::kSupportedAndPinSet &&
!u2f_fallback_possible) {
if (can_collect_pin) {
return MakeCredentialPINDisposition::kUsePIN;
} else {
return MakeCredentialPINDisposition::kUnsatisfiable;
}
}
// If a PIN cannot be collected, and UV is required, then this request cannot
// be met.
if (request.user_verification == UserVerificationRequirement::kRequired &&
(!can_collect_pin ||
device_support == ClientPinAvailability::kNotSupported)) {
return MakeCredentialPINDisposition::kUnsatisfiable;
}
// If UV is required and a PIN can be set, set it during the MakeCredential
// process.
if (device_support == ClientPinAvailability::kSupportedButPinNotSet &&
request.user_verification == UserVerificationRequirement::kRequired) {
return MakeCredentialPINDisposition::kSetPIN;
}
// If discouraged, then either a PIN isn't set (thus we don't use one), or
// else the device supports U2F (because the alternative was handled above)
// and we'll use a U2F fallback to create a credential without a PIN.
DCHECK(device_support != ClientPinAvailability::kSupportedAndPinSet ||
u2f_fallback_possible);
// TODO(agl): perhaps CTAP2 is indicated when, for example, hmac-secret is
// requested?
if (request.user_verification == UserVerificationRequirement::kDiscouraged) {
return MakeCredentialPINDisposition::kNoPIN;
}
// Otherwise, a PIN will be used only if set.
if (device_support == ClientPinAvailability::kSupportedAndPinSet &&
can_collect_pin) {
return MakeCredentialPINDisposition::kUsePIN;
}
return MakeCredentialPINDisposition::kNoPIN;
}
FidoAuthenticator::GetAssertionPINDisposition
FidoDeviceAuthenticator::WillNeedPINToGetAssertion(
const CtapGetAssertionRequest& request,
const FidoRequestHandlerBase::Observer* observer) {
const bool can_use_pin = (Options()->client_pin_availability ==
AuthenticatorSupportedOptions::
ClientPinAvailability::kSupportedAndPinSet) &&
// The PIN is effectively unavailable if there's no
// UI support for collecting it.
observer && observer->SupportsPIN();
// Authenticators with built-in UV can use that.
if (Options()->user_verification_availability ==
AuthenticatorSupportedOptions::UserVerificationAvailability::
kSupportedAndConfigured) {
return can_use_pin ? GetAssertionPINDisposition::kUsePINForFallback
: GetAssertionPINDisposition::kNoPIN;
}
const bool resident_key_request = request.allow_list.empty();
if (resident_key_request) {
if (can_use_pin) {
return GetAssertionPINDisposition::kUsePIN;
}
return GetAssertionPINDisposition::kUnsatisfiable;
}
// If UV is required then the PIN must be used if set, or else this request
// cannot be satisfied.
if (request.user_verification == UserVerificationRequirement::kRequired) {
if (can_use_pin) {
return GetAssertionPINDisposition::kUsePIN;
}
return GetAssertionPINDisposition::kUnsatisfiable;
}
// If UV is preferred and a PIN is set, use it.
if (request.user_verification == UserVerificationRequirement::kPreferred &&
can_use_pin) {
return GetAssertionPINDisposition::kUsePIN;
}
return GetAssertionPINDisposition::kNoPIN;
}
void FidoDeviceAuthenticator::GetCredentialsMetadata(
base::span<const uint8_t> pin_token,
GetCredentialsMetadataCallback callback) {
DCHECK(Options()->supports_credential_management ||
Options()->supports_credential_management_preview);
RunOperation<CredentialManagementRequest, CredentialsMetadataResponse>(
CredentialManagementRequest::ForGetCredsMetadata(
GetCredentialManagementRequestVersion(*Options()), pin_token),
std::move(callback), base::BindOnce(&CredentialsMetadataResponse::Parse));
}
struct FidoDeviceAuthenticator::EnumerateCredentialsState {
EnumerateCredentialsState() = default;
EnumerateCredentialsState(EnumerateCredentialsState&&) = default;
EnumerateCredentialsState& operator=(EnumerateCredentialsState&&) = default;
std::vector<uint8_t> pin_token;
bool is_first_rp = true;
bool is_first_credential = true;
size_t rp_count;
size_t current_rp_credential_count;
FidoDeviceAuthenticator::EnumerateCredentialsCallback callback;
std::vector<AggregatedEnumerateCredentialsResponse> responses;
};
void FidoDeviceAuthenticator::EnumerateCredentials(
base::span<const uint8_t> pin_token,
EnumerateCredentialsCallback callback) {
DCHECK(Options()->supports_credential_management ||
Options()->supports_credential_management_preview);
EnumerateCredentialsState state;
state.pin_token = fido_parsing_utils::Materialize(pin_token);
state.callback = std::move(callback);
RunOperation<CredentialManagementRequest, EnumerateRPsResponse>(
CredentialManagementRequest::ForEnumerateRPsBegin(
GetCredentialManagementRequestVersion(*Options()), pin_token),
base::BindOnce(&FidoDeviceAuthenticator::OnEnumerateRPsDone,
weak_factory_.GetWeakPtr(), std::move(state)),
base::BindOnce(&EnumerateRPsResponse::Parse, /*expect_rp_count=*/true),
&EnumerateRPsResponse::StringFixupPredicate);
}
// TaskClearProxy interposes |callback| and resets |task_| before it runs.
template <typename... Args>
void FidoDeviceAuthenticator::TaskClearProxy(
base::OnceCallback<void(Args...)> callback,
Args... args) {
DCHECK(task_);
DCHECK(!operation_);
task_.reset();
std::move(callback).Run(std::forward<Args>(args)...);
}
// OperationClearProxy interposes |callback| and resets |operation_| before it
// runs.
template <typename... Args>
void FidoDeviceAuthenticator::OperationClearProxy(
base::OnceCallback<void(Args...)> callback,
Args... args) {
DCHECK(operation_);
DCHECK(!task_);
operation_.reset();
std::move(callback).Run(std::forward<Args>(args)...);
}
// RunTask starts a |FidoTask| and ensures that |task_| is reset when the given
// callback is called.
template <typename Task, typename Response, typename... RequestArgs>
void FidoDeviceAuthenticator::RunTask(
RequestArgs&&... request_args,
base::OnceCallback<void(CtapDeviceResponseCode, base::Optional<Response>)>
callback) {
DCHECK(!task_);
DCHECK(!operation_);
DCHECK(device_->SupportedProtocolIsInitialized())
<< "InitializeAuthenticator() must be called first.";
task_ = std::make_unique<Task>(
device_.get(), std::forward<RequestArgs>(request_args)...,
base::BindOnce(
&FidoDeviceAuthenticator::TaskClearProxy<CtapDeviceResponseCode,
base::Optional<Response>>,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
// RunOperation starts a |Ctap2DeviceOperation| and ensures that |operation_| is
// reset when the given completion callback is called.
template <typename Request, typename Response>
void FidoDeviceAuthenticator::RunOperation(
Request request,
base::OnceCallback<void(CtapDeviceResponseCode, base::Optional<Response>)>
callback,
base::OnceCallback<
base::Optional<Response>(const base::Optional<cbor::Value>&)> parser,
bool (*string_fixup_predicate)(const std::vector<const cbor::Value*>&)) {
DCHECK(!task_);
DCHECK(!operation_);
DCHECK(device_->SupportedProtocolIsInitialized())
<< "InitializeAuthenticator() must be called first.";
operation_ = std::make_unique<Ctap2DeviceOperation<Request, Response>>(
device_.get(), std::move(request),
base::BindOnce(&FidoDeviceAuthenticator::OperationClearProxy<
CtapDeviceResponseCode, base::Optional<Response>>,
weak_factory_.GetWeakPtr(), std::move(callback)),
std::move(parser), string_fixup_predicate);
operation_->Start();
}
void FidoDeviceAuthenticator::OnEnumerateRPsDone(
EnumerateCredentialsState state,
CtapDeviceResponseCode status,
base::Optional<EnumerateRPsResponse> response) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(state.callback).Run(status, base::nullopt);
return;
}
if (state.is_first_rp) {
if (response->rp_count == 0) {
std::move(state.callback).Run(status, std::move(state.responses));
return;
}
state.rp_count = response->rp_count;
state.is_first_rp = false;
}
DCHECK(response->rp);
DCHECK(response->rp_id_hash);
state.is_first_credential = true;
state.responses.emplace_back(std::move(*response->rp));
auto request = CredentialManagementRequest::ForEnumerateCredentialsBegin(
GetCredentialManagementRequestVersion(*Options()), state.pin_token,
std::move(*response->rp_id_hash));
RunOperation<CredentialManagementRequest, EnumerateCredentialsResponse>(
std::move(request),
base::BindOnce(&FidoDeviceAuthenticator::OnEnumerateCredentialsDone,
weak_factory_.GetWeakPtr(), std::move(state)),
base::BindOnce(&EnumerateCredentialsResponse::Parse,
/*expect_credential_count=*/true),
&EnumerateCredentialsResponse::StringFixupPredicate);
}
void FidoDeviceAuthenticator::OnEnumerateCredentialsDone(
EnumerateCredentialsState state,
CtapDeviceResponseCode status,
base::Optional<EnumerateCredentialsResponse> response) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(state.callback).Run(status, base::nullopt);
return;
}
if (state.is_first_credential) {
state.current_rp_credential_count = response->credential_count;
state.is_first_credential = false;
}
state.responses.back().credentials.emplace_back(std::move(*response));
if (state.responses.back().credentials.size() <
state.current_rp_credential_count) {
RunOperation<CredentialManagementRequest, EnumerateCredentialsResponse>(
CredentialManagementRequest::ForEnumerateCredentialsGetNext(
GetCredentialManagementRequestVersion(*Options())),
base::BindOnce(&FidoDeviceAuthenticator::OnEnumerateCredentialsDone,
weak_factory_.GetWeakPtr(), std::move(state)),
base::BindOnce(&EnumerateCredentialsResponse::Parse,
/*expect_credential_count=*/false),
&EnumerateCredentialsResponse::StringFixupPredicate);
return;
}
if (state.responses.size() < state.rp_count) {
RunOperation<CredentialManagementRequest, EnumerateRPsResponse>(
CredentialManagementRequest::ForEnumerateRPsGetNext(
GetCredentialManagementRequestVersion(*Options())),
base::BindOnce(&FidoDeviceAuthenticator::OnEnumerateRPsDone,
weak_factory_.GetWeakPtr(), std::move(state)),
base::BindOnce(&EnumerateRPsResponse::Parse,
/*expect_rp_count=*/false),
&EnumerateRPsResponse::StringFixupPredicate);
return;
}
std::move(state.callback)
.Run(CtapDeviceResponseCode::kSuccess, std::move(state.responses));
return;
}
void FidoDeviceAuthenticator::DeleteCredential(
base::span<const uint8_t> pin_token,
const PublicKeyCredentialDescriptor& credential_id,
DeleteCredentialCallback callback) {
DCHECK(Options()->supports_credential_management ||
Options()->supports_credential_management_preview);
RunOperation<CredentialManagementRequest, DeleteCredentialResponse>(
CredentialManagementRequest::ForDeleteCredential(
GetCredentialManagementRequestVersion(*Options()), pin_token,
credential_id),
std::move(callback), base::BindOnce(&DeleteCredentialResponse::Parse),
/*string_fixup_predicate=*/nullptr);
}
void FidoDeviceAuthenticator::GetModality(BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForGetModality(
GetBioEnrollmentRequestVersion(*Options())),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::GetSensorInfo(BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForGetSensorInfo(
GetBioEnrollmentRequestVersion(*Options())),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::BioEnrollFingerprint(
const pin::TokenResponse& pin_token,
base::Optional<std::vector<uint8_t>> template_id,
BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
template_id ? BioEnrollmentRequest::ForEnrollNextSample(
GetBioEnrollmentRequestVersion(*Options()),
std::move(pin_token), std::move(*template_id))
: BioEnrollmentRequest::ForEnrollBegin(
GetBioEnrollmentRequestVersion(*Options()),
std::move(pin_token)),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::BioEnrollRename(
const pin::TokenResponse& pin_token,
std::vector<uint8_t> id,
std::string name,
BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForRename(
GetBioEnrollmentRequestVersion(*Options()), pin_token, std::move(id),
std::move(name)),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::BioEnrollDelete(
const pin::TokenResponse& pin_token,
std::vector<uint8_t> template_id,
BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForDelete(
GetBioEnrollmentRequestVersion(*Options()), pin_token,
std::move(template_id)),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::BioEnrollCancel(BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForCancel(
GetBioEnrollmentRequestVersion(*Options())),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
void FidoDeviceAuthenticator::BioEnrollEnumerate(
const pin::TokenResponse& response,
BioEnrollmentCallback callback) {
RunOperation<BioEnrollmentRequest, BioEnrollmentResponse>(
BioEnrollmentRequest::ForEnumerate(
GetBioEnrollmentRequestVersion(*Options()), std::move(response)),
std::move(callback), base::BindOnce(&BioEnrollmentResponse::Parse));
}
base::Optional<base::span<const int32_t>>
FidoDeviceAuthenticator::GetAlgorithms() {
if (device_->supported_protocol() == ProtocolVersion::kU2f) {
static constexpr int32_t kU2fAlgorithms[1] = {
static_cast<int32_t>(CoseAlgorithmIdentifier::kEs256)};
return kU2fAlgorithms;
}
const base::Optional<AuthenticatorGetInfoResponse>& get_info_response =
device_->device_info();
if (get_info_response) {
return get_info_response->algorithms;
}
return base::nullopt;
}
void FidoDeviceAuthenticator::Reset(ResetCallback callback) {
DCHECK(device_->SupportedProtocolIsInitialized())
<< "InitializeAuthenticator() must be called first.";
RunOperation<pin::ResetRequest, pin::ResetResponse>(
pin::ResetRequest(), std::move(callback),
base::BindOnce(&pin::ResetResponse::Parse));
}
void FidoDeviceAuthenticator::Cancel() {
if (operation_) {
operation_->Cancel();
}
if (task_) {
task_->Cancel();
}
}
std::string FidoDeviceAuthenticator::GetId() const {
return device_->GetId();
}
base::string16 FidoDeviceAuthenticator::GetDisplayName() const {
return device_->GetDisplayName();
}
ProtocolVersion FidoDeviceAuthenticator::SupportedProtocol() const {
DCHECK(device_->SupportedProtocolIsInitialized());
return device_->supported_protocol();
}
bool FidoDeviceAuthenticator::SupportsHMACSecretExtension() const {
const base::Optional<AuthenticatorGetInfoResponse>& get_info_response =
device_->device_info();
return get_info_response && get_info_response->extensions &&
base::Contains(*get_info_response->extensions, kExtensionHmacSecret);
}
const base::Optional<AuthenticatorSupportedOptions>&
FidoDeviceAuthenticator::Options() const {
return options_;
}
base::Optional<FidoTransportProtocol>
FidoDeviceAuthenticator::AuthenticatorTransport() const {
return device_->DeviceTransport();
}
bool FidoDeviceAuthenticator::IsInPairingMode() const {
return device_->IsInPairingMode();
}
bool FidoDeviceAuthenticator::IsPaired() const {
return device_->IsPaired();
}
bool FidoDeviceAuthenticator::RequiresBlePairingPin() const {
return device_->RequiresBlePairingPin();
}
#if defined(OS_WIN)
bool FidoDeviceAuthenticator::IsWinNativeApiAuthenticator() const {
return false;
}
#endif // defined(OS_WIN)
#if defined(OS_MACOSX)
bool FidoDeviceAuthenticator::IsTouchIdAuthenticator() const {
return false;
}
#endif // defined(OS_MACOSX)
void FidoDeviceAuthenticator::SetTaskForTesting(
std::unique_ptr<FidoTask> task) {
task_ = std::move(task);
}
void FidoDeviceAuthenticator::GetUvRetries(GetRetriesCallback callback) {
DCHECK(Options());
DCHECK(Options()->user_verification_availability !=
AuthenticatorSupportedOptions::UserVerificationAvailability::
kNotSupported);
RunOperation<pin::UvRetriesRequest, pin::RetriesResponse>(
pin::UvRetriesRequest(), std::move(callback),
base::BindOnce(&pin::RetriesResponse::ParseUvRetries));
}
bool FidoDeviceAuthenticator::CanGetUvToken() {
return options_->user_verification_availability ==
AuthenticatorSupportedOptions::UserVerificationAvailability::
kSupportedAndConfigured &&
options_->supports_pin_uv_auth_token;
}
void FidoDeviceAuthenticator::GetUvToken(base::Optional<std::string> rp_id,
GetTokenCallback callback) {
GetEphemeralKey(base::BindOnce(
&FidoDeviceAuthenticator::OnHaveEphemeralKeyForUvToken,
weak_factory_.GetWeakPtr(), std::move(rp_id), std::move(callback)));
}
void FidoDeviceAuthenticator::OnHaveEphemeralKeyForUvToken(
base::Optional<std::string> rp_id,
GetTokenCallback callback,
CtapDeviceResponseCode status,
base::Optional<pin::KeyAgreementResponse> key) {
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(callback).Run(status, base::nullopt);
return;
}
DCHECK(key);
pin::UvTokenRequest request(*key, std::move(rp_id));
std::array<uint8_t, 32> shared_key = request.shared_key();
RunOperation<pin::UvTokenRequest, pin::TokenResponse>(
std::move(request), std::move(callback),
base::BindOnce(&pin::TokenResponse::Parse, std::move(shared_key)));
}
base::WeakPtr<FidoAuthenticator> FidoDeviceAuthenticator::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
} // namespace device