forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfido_request_handler_base.cc
367 lines (315 loc) · 13.3 KB
/
fido_request_handler_base.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
// 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_request_handler_base.h"
#include <utility>
#include "base/barrier_closure.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/string_piece.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/device_event_log/device_event_log.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/fido/ble_adapter_manager.h"
#include "device/fido/fido_authenticator.h"
#include "device/fido/fido_discovery_factory.h"
#if defined(OS_WIN)
#include "device/fido/win/authenticator.h"
#endif
namespace device {
// FidoRequestHandlerBase::TransportAvailabilityInfo --------------------------
FidoRequestHandlerBase::TransportAvailabilityInfo::TransportAvailabilityInfo() =
default;
FidoRequestHandlerBase::TransportAvailabilityInfo::TransportAvailabilityInfo(
const TransportAvailabilityInfo& data) = default;
FidoRequestHandlerBase::TransportAvailabilityInfo&
FidoRequestHandlerBase::TransportAvailabilityInfo::operator=(
const TransportAvailabilityInfo& other) = default;
FidoRequestHandlerBase::TransportAvailabilityInfo::
~TransportAvailabilityInfo() = default;
// FidoRequestHandlerBase::Observer -------------------------------------------
FidoRequestHandlerBase::Observer::~Observer() = default;
// FidoRequestHandlerBase -----------------------------------------------------
FidoRequestHandlerBase::FidoRequestHandlerBase() = default;
FidoRequestHandlerBase::FidoRequestHandlerBase(
FidoDiscoveryFactory* fido_discovery_factory,
const base::flat_set<FidoTransportProtocol>& available_transports) {
InitDiscoveries(fido_discovery_factory, available_transports);
}
void FidoRequestHandlerBase::InitDiscoveries(
FidoDiscoveryFactory* fido_discovery_factory,
base::flat_set<FidoTransportProtocol> available_transports) {
#if defined(OS_WIN)
// Try to instantiate the discovery for proxying requests to the native
// Windows WebAuthn API; or fall back to using the regular device transport
// discoveries if the API is unavailable.
auto win_discovery =
fido_discovery_factory->MaybeCreateWinWebAuthnApiDiscovery();
if (win_discovery) {
// The Windows WebAuthn API is available. On this platform, communicating
// with authenticator devices directly is blocked by the OS, so we need to
// go through the native API instead. No device discoveries may be
// instantiated.
win_discovery->set_observer(this);
discoveries_.push_back(std::move(win_discovery));
// Setting |has_win_native_api_authenticator| ensures
// NotifyObserverTransportAvailability() will not be invoked before
// Windows Authenticator has been added. The embedder will be
// responsible for dispatch of the authenticator and whether they
// display any UI in addition to the one provided by the OS.
transport_availability_info_.has_win_native_api_authenticator = true;
// Allow caBLE as a potential additional transport if requested by
// the implementing class because it is not subject to the OS'
// device communication block (only GetAssertionRequestHandler uses
// caBLE). Otherwise, do not instantiate any other transports.
base::EraseIf(available_transports, [](auto transport) {
return transport !=
FidoTransportProtocol::kCloudAssistedBluetoothLowEnergy;
});
}
#endif // defined(OS_WIN)
transport_availability_info_.available_transports = available_transports;
for (const auto transport : available_transports) {
std::vector<std::unique_ptr<FidoDiscoveryBase>> discoveries =
fido_discovery_factory->Create(transport);
if (discoveries.empty()) {
// This can occur in tests when a ScopedVirtualU2fDevice is in effect and
// HID transports are not configured or when caBLE discovery data isn't
// available.
transport_availability_info_.available_transports.erase(transport);
continue;
}
for (auto& discovery : discoveries) {
discovery->set_observer(this);
discoveries_.emplace_back(std::move(discovery));
}
}
// Check if the platform supports BLE before trying to get a power manager.
// CaBLE might be in |available_transports| without actual BLE support under
// the virtual environment.
// TODO(nsatragno): Move the BLE power manager logic to CableDiscoveryFactory
// so we don't need this additional check.
bool has_ble = false;
if (device::BluetoothAdapterFactory::Get()->IsLowEnergySupported() &&
base::Contains(transport_availability_info_.available_transports,
FidoTransportProtocol::kCloudAssistedBluetoothLowEnergy)) {
has_ble = true;
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&FidoRequestHandlerBase::ConstructBleAdapterPowerManager,
weak_factory_.GetWeakPtr()));
}
bool has_platform =
base::Contains(transport_availability_info_.available_transports,
FidoTransportProtocol::kInternal);
// Initialize |notify_observer_callback_| with the number of times it has to
// be invoked before Observer::OnTransportAvailabilityEnumerated is
// dispatched.
// Essentially this is used to wait until the parts
// |transport_availability_info_| are filled out; the
// |notify_observer_callback_| is invoked once for each discovery once it is
// ready, and additionally:
//
// 1) [If BLE or caBLE are enabled] once BLE adapters have been enumerated
// 2) [If platform transport requested] When
// OnHasRecognizedPlatformCredentialFilled() runs.
// 3) When |observer_| is set, so that OnTransportAvailabilityEnumerated is
// never called before it is set.
notify_observer_callback_ = base::BarrierClosure(
discoveries_.size() + has_ble + has_platform + 1,
base::BindOnce(
&FidoRequestHandlerBase::NotifyObserverTransportAvailability,
weak_factory_.GetWeakPtr()));
}
FidoRequestHandlerBase::~FidoRequestHandlerBase() {
CancelActiveAuthenticators();
}
void FidoRequestHandlerBase::StartAuthenticatorRequest(
const std::string& authenticator_id) {
InitializeAuthenticatorAndDispatchRequest(authenticator_id);
}
void FidoRequestHandlerBase::CancelActiveAuthenticators(
base::StringPiece exclude_device_id) {
for (auto task_it = active_authenticators_.begin();
task_it != active_authenticators_.end();) {
DCHECK(!task_it->first.empty());
if (task_it->first != exclude_device_id) {
DCHECK(task_it->second);
task_it->second->Cancel();
// Note that the pointer being erased is non-owning. The actual
// FidoAuthenticator instance is owned by its discovery (which in turn is
// owned by |discoveries_|.
task_it = active_authenticators_.erase(task_it);
} else {
++task_it;
}
}
}
void FidoRequestHandlerBase::OnBluetoothAdapterEnumerated(
bool is_present,
bool is_powered_on,
bool can_power_on,
bool is_peripheral_role_supported) {
if (!is_present) {
transport_availability_info_.available_transports.erase(
FidoTransportProtocol::kCloudAssistedBluetoothLowEnergy);
}
transport_availability_info_.is_ble_powered = is_powered_on;
transport_availability_info_.can_power_on_ble_adapter = can_power_on;
notify_observer_callback_.Run();
}
void FidoRequestHandlerBase::OnBluetoothAdapterPowerChanged(
bool is_powered_on) {
transport_availability_info_.is_ble_powered = is_powered_on;
if (observer_)
observer_->BluetoothAdapterPowerChanged(is_powered_on);
}
void FidoRequestHandlerBase::PowerOnBluetoothAdapter() {
if (!bluetooth_adapter_manager_)
return;
bluetooth_adapter_manager_->SetAdapterPower(true /* set_power_on */);
}
base::WeakPtr<FidoRequestHandlerBase> FidoRequestHandlerBase::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void FidoRequestHandlerBase::Start() {
for (const auto& discovery : discoveries_)
discovery->Start();
}
void FidoRequestHandlerBase::AuthenticatorRemoved(
FidoDiscoveryBase* discovery,
FidoAuthenticator* authenticator) {
// Device connection has been lost or device has already been removed.
// Thus, calling CancelTask() is not necessary. Also, below
// ongoing_tasks_.erase() will have no effect for the devices that have been
// already removed due to processing error or due to invocation of
// CancelOngoingTasks().
auto authenticator_it = active_authenticators_.find(authenticator->GetId());
if (authenticator_it == active_authenticators_.end()) {
NOTREACHED();
FIDO_LOG(ERROR) << "AuthenticatorRemoved() for unknown authenticator "
<< authenticator->GetId();
return;
}
DCHECK_EQ(authenticator_it->second, authenticator);
active_authenticators_.erase(authenticator_it);
if (observer_) {
observer_->FidoAuthenticatorRemoved(authenticator->GetId());
}
}
void FidoRequestHandlerBase::DiscoveryStarted(
FidoDiscoveryBase* discovery,
bool success,
std::vector<FidoAuthenticator*> authenticators) {
if (!success) {
transport_availability_info_.available_transports.erase(
discovery->transport());
} else {
for (auto* authenticator : authenticators) {
AuthenticatorAdded(discovery, authenticator);
}
}
// Allow GetAssertionRequestHandler to asynchronously check for known platform
// credentials and defer |notify_observer_callback_| until that check is done.
// (But don't bother if platform discovery failed to start.)
if (discovery->transport() == FidoTransportProtocol::kInternal) {
if (success) {
FillHasRecognizedPlatformCredential(base::BindOnce(
&FidoRequestHandlerBase::OnHasRecognizedPlatformCredentialFilled,
GetWeakPtr()));
} else {
FidoRequestHandlerBase::OnHasRecognizedPlatformCredentialFilled();
}
}
DCHECK(notify_observer_callback_);
notify_observer_callback_.Run();
}
void FidoRequestHandlerBase::AuthenticatorAdded(
FidoDiscoveryBase* discovery,
FidoAuthenticator* authenticator) {
DCHECK(!authenticator->GetId().empty());
bool was_inserted;
std::tie(std::ignore, was_inserted) =
active_authenticators_.insert({authenticator->GetId(), authenticator});
if (!was_inserted) {
NOTREACHED();
FIDO_LOG(ERROR) << "Authenticator with duplicate ID "
<< authenticator->GetId();
return;
}
// If |observer_| exists, dispatching request to |authenticator| is
// delegated to |observer_|. Else, dispatch request to |authenticator|
// immediately.
bool embedder_controls_dispatch = false;
if (observer_) {
embedder_controls_dispatch =
observer_->EmbedderControlsAuthenticatorDispatch(*authenticator);
observer_->FidoAuthenticatorAdded(*authenticator);
}
if (!embedder_controls_dispatch) {
// Post |InitializeAuthenticatorAndDispatchRequest| into its own task. This
// avoids hairpinning, even if the authenticator immediately invokes the
// request callback.
VLOG(2)
<< "Request handler dispatching request to authenticator immediately.";
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(
&FidoRequestHandlerBase::InitializeAuthenticatorAndDispatchRequest,
GetWeakPtr(), authenticator->GetId()));
} else {
VLOG(2) << "Embedder controls the dispatch.";
}
#if defined(OS_WIN)
if (authenticator->IsWinNativeApiAuthenticator()) {
DCHECK(transport_availability_info_.has_win_native_api_authenticator);
transport_availability_info_.win_native_api_authenticator_id =
authenticator->GetId();
transport_availability_info_
.win_native_ui_shows_resident_credential_notice =
static_cast<WinWebAuthnApiAuthenticator*>(authenticator)
->ShowsPrivacyNotice();
}
#endif // defined(OS_WIN)
}
bool FidoRequestHandlerBase::HasAuthenticator(
const std::string& authenticator_id) const {
return base::Contains(active_authenticators_, authenticator_id);
}
void FidoRequestHandlerBase::FillHasRecognizedPlatformCredential(
base::OnceCallback<void()> done_callback) {
std::move(done_callback).Run();
}
void FidoRequestHandlerBase::NotifyObserverTransportAvailability() {
DCHECK(observer_);
observer_->OnTransportAvailabilityEnumerated(transport_availability_info_);
}
void FidoRequestHandlerBase::InitializeAuthenticatorAndDispatchRequest(
const std::string& authenticator_id) {
auto authenticator_it = active_authenticators_.find(authenticator_id);
if (authenticator_it == active_authenticators_.end()) {
return;
}
FidoAuthenticator* authenticator = authenticator_it->second;
authenticator->InitializeAuthenticator(
base::BindOnce(&FidoRequestHandlerBase::DispatchRequest,
weak_factory_.GetWeakPtr(), authenticator));
}
void FidoRequestHandlerBase::ConstructBleAdapterPowerManager() {
bluetooth_adapter_manager_ = std::make_unique<BleAdapterManager>(this);
}
void FidoRequestHandlerBase::OnHasRecognizedPlatformCredentialFilled() {
notify_observer_callback_.Run();
}
void FidoRequestHandlerBase::StopDiscoveries() {
for (const auto& discovery : discoveries_) {
discovery->MaybeStop();
}
}
constexpr base::TimeDelta
FidoRequestHandlerBase::kMinExpectedAuthenticatorResponseTime;
} // namespace device