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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/device_reauth/win/authenticator_win.h"
#include <objbase.h>
#include <UserConsentVerifierInterop.h>
#include <windows.foundation.h>
#include <windows.security.credentials.ui.h>
#include <windows.storage.streams.h>
#include <wrl/client.h>
#include <wrl/event.h>
#include <string>
#include <utility>
#include "authenticator_win.h"
#include "base/barrier_callback.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_thread_priority.h"
#include "base/win/core_winrt_util.h"
#include "base/win/hstring_reference.h"
#include "base/win/post_async_results.h"
#include "base/win/registry.h"
#include "base/win/scoped_hstring.h"
#include "base/win/scoped_winrt_initializer.h"
#include "base/win/windows_types.h"
#include "base/win/windows_version.h"
#include "chrome/browser/password_manager/password_manager_util_win.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "ui/aura/window.h"
#include "ui/views/win/hwnd_util.h"
namespace {
// Enum specifying possible states of Windows Hello authentication. These
// values are persisted to logs. Entries should not be renumbered and numeric
// values should never be reused.
enum class AuthenticationStateWin {
kStarted = 0,
kFinished = 1,
kMaxValue = kFinished,
};
using AvailabilityCallback = AuthenticatorWinInterface::AvailabilityCallback;
using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Security::Credentials::UI::IUserConsentVerifierStatics;
using ABI::Windows::Security::Credentials::UI::UserConsentVerifierAvailability;
using enum ABI::Windows::Security::Credentials::UI::
UserConsentVerifierAvailability;
using ABI::Windows::Security::Credentials::UI::UserConsentVerificationResult;
using enum ABI::Windows::Security::Credentials::UI::
UserConsentVerificationResult;
using Microsoft::WRL::ComPtr;
BiometricAuthenticationStatusWin ConvertUserConsentVerifierAvailability(
UserConsentVerifierAvailability availability) {
switch (availability) {
case UserConsentVerifierAvailability_Available:
return BiometricAuthenticationStatusWin::kAvailable;
case UserConsentVerifierAvailability_DeviceBusy:
return BiometricAuthenticationStatusWin::kDeviceBusy;
case UserConsentVerifierAvailability_DeviceNotPresent:
return BiometricAuthenticationStatusWin::kDeviceNotPresent;
case UserConsentVerifierAvailability_DisabledByPolicy:
return BiometricAuthenticationStatusWin::kDisabledByPolicy;
case UserConsentVerifierAvailability_NotConfiguredForUser:
return BiometricAuthenticationStatusWin::kNotConfiguredForUser;
default:
return BiometricAuthenticationStatusWin::kUnknown;
}
}
AuthenticationResultStatusWin ConvertUserConsentVerificationResult(
UserConsentVerificationResult result) {
switch (result) {
case UserConsentVerificationResult_Verified:
return AuthenticationResultStatusWin::kVerified;
case UserConsentVerificationResult_DeviceNotPresent:
return AuthenticationResultStatusWin::kDeviceNotPresent;
case UserConsentVerificationResult_NotConfiguredForUser:
return AuthenticationResultStatusWin::kNotConfiguredForUser;
case UserConsentVerificationResult_DisabledByPolicy:
return AuthenticationResultStatusWin::kDisabledByPolicy;
case UserConsentVerificationResult_DeviceBusy:
return AuthenticationResultStatusWin::kDeviceBusy;
case UserConsentVerificationResult_RetriesExhausted:
return AuthenticationResultStatusWin::kRetriesExhausted;
case UserConsentVerificationResult_Canceled:
return AuthenticationResultStatusWin::kCanceled;
default:
return AuthenticationResultStatusWin::kUnknown;
}
}
void RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin result) {
base::UmaHistogramEnumeration(
"PasswordManager.RequestVerificationAsyncResult", result);
}
void RecordAuthenticationState(AuthenticationStateWin state) {
base::UmaHistogramEnumeration("PasswordManager.AuthenticationStateWin",
state);
}
void RecordAuthenticationAsyncOpFailureReson(HRESULT hr) {
base::UmaHistogramSparse("PasswordManager.AuthenticationAsyncOpFailureReson",
hr);
}
void ReturnAvailabilityValue(AvailabilityCallback callback,
UserConsentVerifierAvailability availability) {
std::move(callback).Run(ConvertUserConsentVerifierAvailability(availability));
}
void OnAvailabilityReceived(scoped_refptr<base::SequencedTaskRunner> thread,
AvailabilityCallback callback,
UserConsentVerifierAvailability availability) {
thread->PostTask(FROM_HERE,
base::BindOnce(&ReturnAvailabilityValue, std::move(callback),
availability));
}
void ReportCantCheckAvailability(
scoped_refptr<base::SequencedTaskRunner> thread,
AvailabilityCallback callback) {
thread->PostTask(FROM_HERE,
base::BindOnce(std::move(callback),
BiometricAuthenticationStatusWin::kUnknown));
}
bool VectorToFirstElement(std::vector<bool> result) {
CHECK_EQ(result.size(), 1u);
return result[0];
}
// Asks operating system if user has configured and enabled Windows Hello on
// their machine. Runs `callback` on `thread`.
void GetBiometricAvailabilityFromWindows(
AvailabilityCallback callback,
scoped_refptr<base::SequencedTaskRunner> thread) {
// Mitigate the issues caused by loading DLLs on a background thread
// (http://crbug/973868).
SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY();
ComPtr<IUserConsentVerifierStatics> factory;
HRESULT hr = base::win::GetActivationFactory<
IUserConsentVerifierStatics,
RuntimeClass_Windows_Security_Credentials_UI_UserConsentVerifier>(
&factory);
if (FAILED(hr)) {
ReportCantCheckAvailability(thread, std::move(callback));
return;
}
ComPtr<IAsyncOperation<UserConsentVerifierAvailability>> async_op;
hr = factory->CheckAvailabilityAsync(&async_op);
if (FAILED(hr)) {
ReportCantCheckAvailability(thread, std::move(callback));
return;
}
base::win::PostAsyncResults(
std::move(async_op),
base::BindOnce(&OnAvailabilityReceived, thread, std::move(callback)));
}
void AuthenticateWithLegacyApi(const std::u16string& message,
base::OnceCallback<void(bool)> result_callback) {
Browser* browser = chrome::FindLastActive();
if (!browser) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(result_callback), /*success=*/false));
return;
}
gfx::NativeWindow window = browser->window()->GetNativeWindow();
base::SequencedTaskRunner::GetCurrentDefault()->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&password_manager_util_win::AuthenticateUser, window,
message),
std::move(result_callback));
}
void OnAuthenticationReceived(base::OnceCallback<void(bool)> callback,
const std::u16string& message,
UserConsentVerificationResult result) {
AuthenticationResultStatusWin authentication_result =
ConvertUserConsentVerificationResult(result);
RecordWindowsHelloAuthenticationResult(authentication_result);
switch (authentication_result) {
case AuthenticationResultStatusWin::kVerified:
std::move(callback).Run(/*success=*/true);
return;
case AuthenticationResultStatusWin::kCanceled:
case AuthenticationResultStatusWin::kRetriesExhausted:
std::move(callback).Run(/*success=*/false);
return;
case AuthenticationResultStatusWin::kDeviceNotPresent:
case AuthenticationResultStatusWin::kNotConfiguredForUser:
case AuthenticationResultStatusWin::kDisabledByPolicy:
case AuthenticationResultStatusWin::kDeviceBusy:
case AuthenticationResultStatusWin::kUnknown:
// Windows Hello is not available so there should be a fallback to the old
// API.
AuthenticateWithLegacyApi(message, std::move(callback));
return;
case AuthenticationResultStatusWin::kFailedToCallAPI:
case AuthenticationResultStatusWin::kFailedToCreateFactory:
case AuthenticationResultStatusWin::kFailedToPostTask:
case AuthenticationResultStatusWin::kAsyncOperationFailed:
case AuthenticationResultStatusWin::kFailedToFindBrowser:
case AuthenticationResultStatusWin::kFailedToFindHWNDForNativeWindow:
// This values are not returned by UserConsentVerifier API.
NOTREACHED();
}
}
void OnAuthenticationAsyncOpFail(
base::OnceCallback<void(bool)> callback,
const std::u16string& message,
HRESULT hr) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kAsyncOperationFailed);
RecordAuthenticationAsyncOpFailureReson(hr);
AuthenticateWithLegacyApi(message, std::move(callback));
}
// TODO(b/349728186): Cleanup after Win11 solution is launched.
void PerformWindowsHelloAuthenticationAsync(
base::OnceCallback<void(bool)> callback,
const std::u16string& message) {
ComPtr<IUserConsentVerifierStatics> factory;
HRESULT hr = base::win::GetActivationFactory<
IUserConsentVerifierStatics,
RuntimeClass_Windows_Security_Credentials_UI_UserConsentVerifier>(
&factory);
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToCreateFactory);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
ComPtr<IAsyncOperation<UserConsentVerificationResult>> async_op;
hr = factory->RequestVerificationAsync(
base::win::HStringReference(base::UTF16ToWide(message).c_str()).Get(),
&async_op);
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToCallAPI);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
// In order to pass the callback to both the PostAsyncHandlers call and
// to the AuthenticateWithLegacyApi (if the former fails to post the task) a
// RepeatingCallback is needed. The callback will be executed at most once.
auto barrier_callback = base::BarrierCallback<bool>(
1, base::BindOnce(&VectorToFirstElement).Then(std::move(callback)));
hr = base::win::PostAsyncHandlers(
async_op.Get(),
base::BindOnce(&OnAuthenticationReceived, barrier_callback, message),
base::BindOnce(&OnAuthenticationAsyncOpFail, barrier_callback, message));
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToPostTask);
AuthenticateWithLegacyApi(message, barrier_callback);
return;
}
}
void PerformInteropWindowsHelloAuthenticationAsync(
base::OnceCallback<void(bool)> callback,
const std::u16string& message) {
ComPtr<IUserConsentVerifierInterop> factory;
HRESULT hr = base::win::GetActivationFactory<
IUserConsentVerifierInterop,
RuntimeClass_Windows_Security_Credentials_UI_UserConsentVerifier>(
&factory);
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToCreateFactory);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
ComPtr<IAsyncOperation<UserConsentVerificationResult>> async_op;
Browser* browser = chrome::FindLastActive();
if (!browser) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToFindBrowser);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
HWND hwnd = views::HWNDForNativeWindow(browser->window()->GetNativeWindow());
if (!hwnd) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToFindHWNDForNativeWindow);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
hr = factory->RequestVerificationForWindowAsync(
hwnd,
base::win::HStringReference(base::UTF16ToWide(message).c_str()).Get(),
__uuidof(IAsyncOperation<UserConsentVerificationResult>), &async_op);
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToCallAPI);
AuthenticateWithLegacyApi(message, std::move(callback));
return;
}
// In order to pass the callback to both the PostAsyncHandlers call and
// to the AuthenticateWithLegacyApi (if the former fails to post the task) a
// RepeatingCallback is needed. The callback will be executed at most once.
auto barrier_callback = base::BarrierCallback<bool>(
1, base::BindOnce(&VectorToFirstElement).Then(std::move(callback)));
hr = base::win::PostAsyncHandlers(
async_op.Get(),
base::BindOnce(&OnAuthenticationReceived, barrier_callback, message),
base::BindOnce(&OnAuthenticationAsyncOpFail, barrier_callback, message));
if (FAILED(hr)) {
RecordWindowsHelloAuthenticationResult(
AuthenticationResultStatusWin::kFailedToPostTask);
AuthenticateWithLegacyApi(message, barrier_callback);
return;
}
}
void PerformWin11Authentication(
const std::u16string& message,
base::OnceCallback<void(bool)> result_callback) {
PerformInteropWindowsHelloAuthenticationAsync(std::move(result_callback),
message);
}
void PerformWin10Authentication(
const std::u16string& message,
base::OnceCallback<void(bool)> result_callback) {
// Posting authentication using the new API on a background thread causes
// Windows Hello dialog not to attach to Chrome's UI and instead it is
// visible behind it. Running it on the default thread isn't that bad
// because the thread itself is not blocked and there are operations
// happening while the win hello dialog is visible.
PerformWindowsHelloAuthenticationAsync(std::move(result_callback), message);
}
} // namespace
AuthenticatorWin::AuthenticatorWin() = default;
AuthenticatorWin::~AuthenticatorWin() = default;
void AuthenticatorWin::AuthenticateUser(
const std::u16string& message,
base::OnceCallback<void(bool)> result_callback) {
RecordAuthenticationState(AuthenticationStateWin::kStarted);
// TODO(b/349728186): Cleanup after Win11 solution is launched.
if (base::win::GetVersion() >= base::win::Version::WIN11) {
PerformWin11Authentication(
message, std::move(result_callback)
.Then(base::BindOnce(RecordAuthenticationState,
AuthenticationStateWin::kFinished)));
return;
}
PerformWin10Authentication(
message, std::move(result_callback)
.Then(base::BindOnce(RecordAuthenticationState,
AuthenticationStateWin::kFinished)));
}
void AuthenticatorWin::CheckIfBiometricsAvailable(
AvailabilityCallback callback) {
scoped_refptr<base::SequencedTaskRunner> background_task_runner =
base::ThreadPool::CreateCOMSTATaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT});
DCHECK(background_task_runner);
background_task_runner->PostTask(
FROM_HERE,
base::BindOnce(&GetBiometricAvailabilityFromWindows, std::move(callback),
base::SequencedTaskRunner::GetCurrentDefault()));
}
bool AuthenticatorWin::CanAuthenticateWithScreenLock() {
return password_manager_util_win::CanAuthenticateWithScreenLock();
}
|