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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/osauth/impl/cryptohome_core_impl.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"
#include "chromeos/ash/components/login/auth/auth_factor_editor.h"
#include "chromeos/ash/components/login/auth/auth_performer.h"
#include "chromeos/ash/components/login/auth/public/auth_session_intent.h"
#include "chromeos/ash/components/login/auth/public/authentication_error.h"
#include "chromeos/ash/components/login/auth/public/user_context.h"
#include "chromeos/ash/components/osauth/public/auth_session_storage.h"
#include "chromeos/ash/components/osauth/public/common_types.h"
#include "components/user_manager/user_manager.h"
namespace ash {
namespace {
AuthSessionIntent MapPurposeToIntent(AuthPurpose purpose) {
switch (purpose) {
case AuthPurpose::kLogin:
case AuthPurpose::kAuthSettings:
return AuthSessionIntent::kDecrypt;
case AuthPurpose::kWebAuthN:
return AuthSessionIntent::kWebAuthn;
case AuthPurpose::kUserVerification:
case AuthPurpose::kScreenUnlock:
return AuthSessionIntent::kVerifyOnly;
}
}
} // namespace
CryptohomeCoreImpl::CryptohomeCoreImpl(UserDataAuthClient* client)
: dbus_client_(client),
performer_(std::make_unique<AuthPerformer>(dbus_client_)),
editor_(std::make_unique<AuthFactorEditor>(dbus_client_)) {}
CryptohomeCoreImpl::~CryptohomeCoreImpl() = default;
void CryptohomeCoreImpl::WaitForService(ServiceAvailabilityCallback callback) {
dbus_client_->WaitForServiceToBeAvailable(
base::BindOnce(&CryptohomeCoreImpl::OnServiceStatus,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void CryptohomeCoreImpl::OnServiceStatus(ServiceAvailabilityCallback callback,
bool service_is_available) {
std::move(callback).Run(service_is_available);
}
void CryptohomeCoreImpl::StartAuthSession(const AuthAttemptVector& attempt,
Client* client) {
if (current_attempt_.has_value()) {
CHECK(attempt == *current_attempt_)
<< "Cryptohome core does not support parallel attempts";
} else {
current_attempt_ = attempt;
was_authenticated_ = false;
performer_->InvalidateCurrentAttempts();
}
DCHECK(!clients_.contains(client));
if (current_stage_ == Stage::kAuthSessionRequested ||
current_stage_ == Stage::kAuthFactorConfigurationRequested) {
// All events would be sent in OnGetAuthFactorsConfiguration.
clients_.insert(client);
return;
}
if (current_stage_ == Stage::kFinished) {
if (auth_session_started_) {
clients_.insert(client);
client->OnCryptohomeAuthSessionStarted();
} else {
client->OnAuthSessionStartFailure();
}
return;
}
CHECK_EQ(current_stage_, Stage::kIdle);
current_stage_ = Stage::kAuthSessionRequested;
CHECK(!auth_session_started_);
clients_.insert(client);
const user_manager::User* user =
user_manager::UserManager::Get()->FindUser(attempt.account);
CHECK(user) << "Cryptohome core should only be used for existing users";
context_ = std::make_unique<UserContext>(user->GetType(), attempt.account);
bool ephemeral = user_manager::UserManager::Get()->IsEphemeralUser(user);
performer_->StartAuthSession(
std::move(context_), ephemeral, MapPurposeToIntent(attempt.purpose),
base::BindOnce(&CryptohomeCoreImpl::OnAuthSessionStarted,
weak_factory_.GetWeakPtr()));
}
void CryptohomeCoreImpl::OnAuthSessionStarted(
bool user_exists,
std::unique_ptr<UserContext> context,
std::optional<AuthenticationError> error) {
CHECK_EQ(current_stage_, Stage::kAuthSessionRequested);
current_stage_ = Stage::kAuthFactorConfigurationRequested;
if (!user_exists) {
// Somehow user home directory does not exist.
LOG(ERROR) << "Cryptohome Core: user does not exist";
for (auto& client : clients_) {
client->OnAuthSessionStartFailure();
}
clients_.clear();
return;
}
if (error.has_value()) {
// Error is already logged by Authenticator.
for (auto& client : clients_) {
client->OnAuthSessionStartFailure();
}
clients_.clear();
return;
}
// Next step after starting the session is to load the factor configuration.
editor_->GetAuthFactorsConfiguration(
std::move(context),
base::BindOnce(&CryptohomeCoreImpl::OnGetAuthFactorsConfiguration,
weak_factory_.GetWeakPtr()));
}
void CryptohomeCoreImpl::OnGetAuthFactorsConfiguration(
std::unique_ptr<UserContext> context,
std::optional<AuthenticationError> error) {
CHECK_EQ(current_stage_, Stage::kAuthFactorConfigurationRequested);
current_stage_ = Stage::kFinished;
if (error.has_value()) {
// Error is already logged by Authenticator.
for (auto& client : clients_) {
client->OnAuthSessionStartFailure();
}
clients_.clear();
return;
}
// Everything is now fully started and loaded, signal all the clients.
context_ = std::move(context);
auth_session_started_ = true;
for (auto& client : clients_) {
client->OnCryptohomeAuthSessionStarted();
}
}
void CryptohomeCoreImpl::EndAuthSession(Client* client) {
DCHECK(clients_.contains(client));
DCHECK(!clients_being_removed_.contains(client));
clients_.erase(client);
clients_being_removed_.insert(client);
if (!clients_.empty()) {
// Wait for all clients to issue EndAuthSession.
return;
}
CHECK_NE(current_stage_, Stage::kIdle);
if (current_stage_ == Stage::kAuthSessionRequested) {
performer_->InvalidateCurrentAttempts();
}
current_stage_ = Stage::kIdle;
auth_session_started_ = false;
if (context_) {
performer_->InvalidateAuthSession(
std::move(context_),
base::BindOnce(&CryptohomeCoreImpl::OnInvalidateAuthSession,
weak_factory_.GetWeakPtr()));
return;
}
// We should have no context only when session is authorized and
// one of the clients requested `StoreAuthenticatedContext`.
CHECK(was_authenticated_);
EndAuthSessionImpl();
}
void CryptohomeCoreImpl::OnInvalidateAuthSession(
std::unique_ptr<UserContext> context,
std::optional<AuthenticationError> error) {
if (error.has_value()) {
LOG(ERROR) << "Error during authsession invalidation";
}
EndAuthSessionImpl();
}
void CryptohomeCoreImpl::EndAuthSessionImpl() {
// Remove elements as we go, as calling
// `OnCryptohomeAuthSessionFinished` might result
// in engines being deleted and raw_ptr becoming
// dangling.
while (!clients_being_removed_.empty()) {
auto it = clients_being_removed_.begin();
Client* client = it->get();
clients_being_removed_.erase(it);
client->OnCryptohomeAuthSessionFinished();
}
CHECK(clients_being_removed_.empty());
CHECK(clients_.empty());
current_attempt_ = std::nullopt;
was_authenticated_ = false;
}
AuthPerformer* CryptohomeCoreImpl::GetAuthPerformer() const {
CHECK(performer_);
return performer_.get();
}
UserContext* CryptohomeCoreImpl::GetCurrentContext() const {
CHECK(context_);
return context_.get();
}
AuthProofToken CryptohomeCoreImpl::StoreAuthenticationContext() {
CHECK(context_);
was_authenticated_ = true;
return AuthSessionStorage::Get()->Store(std::move(context_));
}
void CryptohomeCoreImpl::BorrowContext(BorrowContextCallback callback) {
if (!context_) {
borrow_callback_queue_.emplace(std::move(callback));
return;
}
BorrowContextAndRun(std::move(callback));
return;
}
void CryptohomeCoreImpl::ReturnContext(std::unique_ptr<UserContext> context) {
CHECK(!context_);
context_ = std::move(context);
if (!borrow_callback_queue_.empty()) {
auto callback = std::move(borrow_callback_queue_.front());
borrow_callback_queue_.pop();
BorrowContextAndRun(std::move(callback));
return;
}
}
void CryptohomeCoreImpl::BorrowContextAndRun(BorrowContextCallback callback) {
CHECK(context_);
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), std::move(context_)));
}
} // namespace ash
|