forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential_management_handler.cc
331 lines (295 loc) · 11.6 KB
/
credential_management_handler.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
// Copyright 2019 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/credential_management_handler.h"
#include <utility>
#include "base/bind.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "components/cbor/reader.h"
#include "components/cbor/values.h"
#include "components/cbor/writer.h"
#include "device/fido/fido_authenticator.h"
#include "device/fido/fido_constants.h"
#include "device/fido/pin.h"
#include "device/fido/public_key_credential_descriptor.h"
namespace device {
CredentialManagementHandler::CredentialManagementHandler(
FidoDiscoveryFactory* fido_discovery_factory,
const base::flat_set<FidoTransportProtocol>& supported_transports,
ReadyCallback ready_callback,
GetPINCallback get_pin_callback,
FinishedCallback finished_callback)
: FidoRequestHandlerBase(fido_discovery_factory, supported_transports),
ready_callback_(std::move(ready_callback)),
get_pin_callback_(std::move(get_pin_callback)),
finished_callback_(std::move(finished_callback)) {
Start();
}
CredentialManagementHandler::~CredentialManagementHandler() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void CredentialManagementHandler::DispatchRequest(
FidoAuthenticator* authenticator) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state_ != State::kWaitingForTouch) {
return;
}
authenticator->GetTouch(base::BindOnce(&CredentialManagementHandler::OnTouch,
weak_factory_.GetWeakPtr(),
authenticator));
}
void CredentialManagementHandler::OnTouch(FidoAuthenticator* authenticator) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state_ != State::kWaitingForTouch) {
return;
}
state_ = State::kGettingRetries;
CancelActiveAuthenticators(authenticator->GetId());
if (authenticator->SupportedProtocol() != ProtocolVersion::kCtap2 ||
!authenticator->Options() ||
!(authenticator->Options()->supports_credential_management ||
authenticator->Options()->supports_credential_management_preview)) {
state_ = State::kFinished;
std::move(finished_callback_)
.Run(CredentialManagementStatus::
kAuthenticatorMissingCredentialManagement);
return;
}
if (authenticator->Options()->client_pin_availability !=
AuthenticatorSupportedOptions::ClientPinAvailability::
kSupportedAndPinSet) {
// The authenticator doesn't have a PIN/UV set up or doesn't support PINs.
// We should implement in-flow PIN setting, but for now just tell the user
// to set a PIN themselves.
state_ = State::kFinished;
std::move(finished_callback_).Run(CredentialManagementStatus::kNoPINSet);
return;
}
if (authenticator->ForcePINChange()) {
state_ = State::kFinished;
std::move(finished_callback_)
.Run(CredentialManagementStatus::kForcePINChange);
return;
}
authenticator_ = authenticator;
authenticator_->GetPinRetries(
base::BindOnce(&CredentialManagementHandler::OnRetriesResponse,
weak_factory_.GetWeakPtr()));
}
void CredentialManagementHandler::OnRetriesResponse(
CtapDeviceResponseCode status,
absl::optional<pin::RetriesResponse> response) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(state_, State::kGettingRetries);
if (status != CtapDeviceResponseCode::kSuccess) {
state_ = State::kFinished;
std::move(finished_callback_)
.Run(CredentialManagementStatus::kAuthenticatorResponseInvalid);
return;
}
if (response->retries == 0) {
state_ = State::kFinished;
std::move(finished_callback_)
.Run(CredentialManagementStatus::kHardPINBlock);
return;
}
state_ = State::kWaitingForPIN;
get_pin_callback_.Run(authenticator_->CurrentMinPINLength(),
response->retries,
base::BindOnce(&CredentialManagementHandler::OnHavePIN,
weak_factory_.GetWeakPtr()));
}
void CredentialManagementHandler::OnHavePIN(std::string pin) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(State::kWaitingForPIN, state_);
if (authenticator_ == nullptr) {
// Authenticator was detached. The request will already have been canceled
// but this callback may have been waiting in a queue.
return;
}
state_ = State::kGettingPINToken;
authenticator_->GetPINToken(
std::move(pin), {pin::Permissions::kCredentialManagement},
/*rp_id=*/absl::nullopt,
base::BindOnce(&CredentialManagementHandler::OnHavePINToken,
weak_factory_.GetWeakPtr()));
}
void CredentialManagementHandler::OnHavePINToken(
CtapDeviceResponseCode status,
absl::optional<pin::TokenResponse> response) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(state_, State::kGettingPINToken);
if (status == CtapDeviceResponseCode::kCtap2ErrPinInvalid) {
state_ = State::kGettingRetries;
authenticator_->GetPinRetries(
base::BindOnce(&CredentialManagementHandler::OnRetriesResponse,
weak_factory_.GetWeakPtr()));
return;
}
if (status != CtapDeviceResponseCode::kSuccess) {
CredentialManagementStatus error;
switch (status) {
case CtapDeviceResponseCode::kCtap2ErrPinAuthBlocked:
error = CredentialManagementStatus::kSoftPINBlock;
break;
case CtapDeviceResponseCode::kCtap2ErrPinBlocked:
error = CredentialManagementStatus::kHardPINBlock;
break;
default:
error = CredentialManagementStatus::kAuthenticatorResponseInvalid;
break;
}
state_ = State::kFinished;
std::move(finished_callback_).Run(error);
return;
}
state_ = State::kReady;
pin_token_ = response;
std::move(ready_callback_).Run();
}
void CredentialManagementHandler::GetCredentials(
GetCredentialsCallback callback) {
DCHECK(state_ == State::kReady && !get_credentials_callback_);
if (!authenticator_) {
// AuthenticatorRemoved() may have been called, but the observer would have
// seen a FidoAuthenticatorRemoved() call.
NOTREACHED();
return;
}
get_credentials_callback_ = std::move(callback);
state_ = State::kGettingCredentials;
authenticator_->GetCredentialsMetadata(
*pin_token_,
base::BindOnce(&CredentialManagementHandler::OnCredentialsMetadata,
weak_factory_.GetWeakPtr()));
}
static void OnDeleteCredential(
CredentialManagementHandler::DeleteCredentialCallback callback,
CtapDeviceResponseCode status,
absl::optional<DeleteCredentialResponse> response) {
std::move(callback).Run(status);
}
void CredentialManagementHandler::DeleteCredential(
const PublicKeyCredentialDescriptor& credential_id,
DeleteCredentialCallback callback) {
DCHECK(state_ == State::kReady && !get_credentials_callback_);
if (!authenticator_) {
// AuthenticatorRemoved() may have been called, but the observer would have
// seen a FidoAuthenticatorRemoved() call.
NOTREACHED();
return;
}
DCHECK(pin_token_);
authenticator_->DeleteCredential(
*pin_token_, credential_id,
base::BindOnce(&OnDeleteCredential, std::move(callback)));
}
void CredentialManagementHandler::OnDeleteCredentials(
std::vector<std::vector<uint8_t>> remaining_credential_ids,
CredentialManagementHandler::DeleteCredentialCallback callback,
CtapDeviceResponseCode status,
absl::optional<DeleteCredentialResponse> response) {
if (status != CtapDeviceResponseCode::kSuccess ||
remaining_credential_ids.empty()) {
std::move(callback).Run(status);
return;
}
if (!authenticator_) {
// |authenticator_| could have been removed during a bulk deletion. The
// observer would have already gotten an AuthenticatorRemoved() call, so no
// need to resolve |callback|.
return;
}
auto credential_id = *PublicKeyCredentialDescriptor::CreateFromCBORValue(
*cbor::Reader::Read(remaining_credential_ids.back()));
remaining_credential_ids.pop_back();
authenticator_->DeleteCredential(
*pin_token_, credential_id,
base::BindOnce(&CredentialManagementHandler::OnDeleteCredentials,
weak_factory_.GetWeakPtr(),
std::move(remaining_credential_ids), std::move(callback)));
}
void CredentialManagementHandler::DeleteCredentials(
std::vector<std::vector<uint8_t>> credential_ids,
DeleteCredentialCallback callback) {
DCHECK(state_ == State::kReady && !get_credentials_callback_);
if (!authenticator_) {
// AuthenticatorRemoved() may have been called, but the observer would have
// seen a FidoAuthenticatorRemoved() call.
NOTREACHED();
return;
}
DCHECK(pin_token_);
if (credential_ids.empty()) {
std::move(callback).Run(CtapDeviceResponseCode::kSuccess);
return;
}
auto credential_id = *PublicKeyCredentialDescriptor::CreateFromCBORValue(
*cbor::Reader::Read(credential_ids.back()));
credential_ids.pop_back();
authenticator_->DeleteCredential(
*pin_token_, credential_id,
base::BindOnce(&CredentialManagementHandler::OnDeleteCredentials,
weak_factory_.GetWeakPtr(), std::move(credential_ids),
std::move(callback)));
}
void CredentialManagementHandler::OnCredentialsMetadata(
CtapDeviceResponseCode status,
absl::optional<CredentialsMetadataResponse> response) {
if (status != CtapDeviceResponseCode::kSuccess) {
state_ = State::kFinished;
std::move(get_credentials_callback_)
.Run(status, absl::nullopt, absl::nullopt);
return;
}
authenticator_->EnumerateCredentials(
*pin_token_,
base::BindOnce(&CredentialManagementHandler::OnEnumerateCredentials,
weak_factory_.GetWeakPtr(), std::move(*response)));
}
void CredentialManagementHandler::OnEnumerateCredentials(
CredentialsMetadataResponse metadata_response,
CtapDeviceResponseCode status,
absl::optional<std::vector<AggregatedEnumerateCredentialsResponse>>
responses) {
if (status != CtapDeviceResponseCode::kSuccess) {
state_ = State::kFinished;
std::move(get_credentials_callback_)
.Run(status, absl::nullopt, absl::nullopt);
return;
}
// Sort credentials by (RP ID, username, user) ascending.
for (auto& response : *responses) {
std::sort(response.credentials.begin(), response.credentials.end(),
[](const EnumerateCredentialsResponse& a,
const EnumerateCredentialsResponse& b) {
if (a.user.name == b.user.name) {
return a.user.id < b.user.id;
}
return a.user.name < b.user.name;
});
}
std::sort(responses->begin(), responses->end(),
[](const AggregatedEnumerateCredentialsResponse& a,
const AggregatedEnumerateCredentialsResponse& b) {
return a.rp.id < b.rp.id;
});
state_ = State::kReady;
std::move(get_credentials_callback_)
.Run(status, std::move(responses),
metadata_response.num_estimated_remaining_credentials);
}
void CredentialManagementHandler::AuthenticatorRemoved(
FidoDiscoveryBase* discovery,
FidoAuthenticator* authenticator) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
FidoRequestHandlerBase::AuthenticatorRemoved(discovery, authenticator);
if (authenticator != authenticator_ || state_ == State::kFinished) {
return;
}
authenticator_ = nullptr;
state_ = State::kFinished;
std::move(finished_callback_).Run(CredentialManagementStatus::kSuccess);
}
} // namespace device