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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/trusted_vault/physical_device_recovery_factor.h"
#include "base/task/bind_post_task.h"
#include "components/trusted_vault/proto_string_bytes_conversion.h"
#include "components/trusted_vault/securebox.h"
#include "components/trusted_vault/trusted_vault_connection.h"
namespace trusted_vault {
namespace {
constexpr int kCurrentDeviceRegistrationVersion = 1;
TrustedVaultDownloadKeysStatusForUMA GetDownloadKeysStatusForUMAFromResponse(
TrustedVaultDownloadKeysStatus response_status) {
switch (response_status) {
case TrustedVaultDownloadKeysStatus::kSuccess:
return TrustedVaultDownloadKeysStatusForUMA::kSuccess;
case TrustedVaultDownloadKeysStatus::kMemberNotFound:
return TrustedVaultDownloadKeysStatusForUMA::kMemberNotFound;
case TrustedVaultDownloadKeysStatus::kMembershipNotFound:
return TrustedVaultDownloadKeysStatusForUMA::kMembershipNotFound;
case TrustedVaultDownloadKeysStatus::kMembershipCorrupted:
return TrustedVaultDownloadKeysStatusForUMA::kMembershipCorrupted;
case TrustedVaultDownloadKeysStatus::kMembershipEmpty:
return TrustedVaultDownloadKeysStatusForUMA::kMembershipEmpty;
case TrustedVaultDownloadKeysStatus::kNoNewKeys:
return TrustedVaultDownloadKeysStatusForUMA::kNoNewKeys;
case TrustedVaultDownloadKeysStatus::kKeyProofsVerificationFailed:
return TrustedVaultDownloadKeysStatusForUMA::kKeyProofsVerificationFailed;
case TrustedVaultDownloadKeysStatus::kAccessTokenFetchingFailure:
return TrustedVaultDownloadKeysStatusForUMA::kAccessTokenFetchingFailure;
case TrustedVaultDownloadKeysStatus::kNetworkError:
return TrustedVaultDownloadKeysStatusForUMA::kNetworkError;
case TrustedVaultDownloadKeysStatus::kOtherError:
return TrustedVaultDownloadKeysStatusForUMA::kOtherError;
}
NOTREACHED();
}
} // namespace
PhysicalDeviceRecoveryFactor::PhysicalDeviceRecoveryFactor(
SecurityDomainId security_domain_id,
StandaloneTrustedVaultStorage* storage,
TrustedVaultThrottlingConnection* connection,
CoreAccountInfo primary_account)
: security_domain_id_(security_domain_id),
storage_(storage),
connection_(connection),
primary_account_(primary_account) {
CHECK(storage_);
CHECK(connection_);
}
PhysicalDeviceRecoveryFactor::~PhysicalDeviceRecoveryFactor() = default;
LocalRecoveryFactorType PhysicalDeviceRecoveryFactor::GetRecoveryFactorType()
const {
return LocalRecoveryFactorType::kPhysicalDevice;
}
void PhysicalDeviceRecoveryFactor::AttemptRecovery(AttemptRecoveryCallback cb) {
auto* per_user_vault = GetPrimaryAccountVault();
if (!GetPrimaryAccountVault()
->local_device_registration_info()
.device_registered()) {
FulfillRecoveryWithFailure(
TrustedVaultDownloadKeysStatusForUMA::kDeviceNotRegistered,
std::move(cb));
return;
}
if (connection_->AreRequestsThrottled(primary_account_)) {
FulfillRecoveryWithFailure(
TrustedVaultDownloadKeysStatusForUMA::kThrottledClientSide,
std::move(cb));
return;
}
std::unique_ptr<SecureBoxKeyPair> key_pair =
SecureBoxKeyPair::CreateByPrivateKeyImport(
ProtoStringToBytes(per_user_vault->local_device_registration_info()
.private_key_material()));
if (!key_pair) {
// Corrupted state: device is registered, but `key_pair` can't be imported.
// TODO(crbug.com/40699425): restore from this state (throw away the key
// and trigger device registration again).
FulfillRecoveryWithFailure(
TrustedVaultDownloadKeysStatusForUMA::kCorruptedLocalDeviceRegistration,
std::move(cb));
return;
}
// Guaranteed by `device_registered` check above.
CHECK(!per_user_vault->vault_key().empty());
ongoing_request_ = connection_->DownloadNewKeys(
primary_account_,
TrustedVaultKeyAndVersion(
ProtoStringToBytes(
per_user_vault->vault_key().rbegin()->key_material()),
per_user_vault->last_vault_key_version()),
std::move(key_pair),
// `this` outlives `ongoing_request_`.
base::BindOnce(&PhysicalDeviceRecoveryFactor::OnKeysDownloaded,
base::Unretained(this), std::move(cb)));
CHECK(ongoing_request_);
}
bool PhysicalDeviceRecoveryFactor::IsRegistered() {
auto* per_user_vault = GetPrimaryAccountVault();
return per_user_vault->local_device_registration_info().device_registered();
}
void PhysicalDeviceRecoveryFactor::MarkAsNotRegistered() {
auto* per_user_vault = GetPrimaryAccountVault();
per_user_vault->mutable_local_device_registration_info()
->set_device_registered(false);
per_user_vault->mutable_local_device_registration_info()
->clear_device_registered_version();
storage_->WriteDataToDisk();
}
TrustedVaultRecoveryFactorRegistrationStateForUMA
PhysicalDeviceRecoveryFactor::MaybeRegister(RegisterCallback cb) {
auto* per_user_vault = GetPrimaryAccountVault();
if (per_user_vault->local_device_registration_info().device_registered()) {
static_assert(kCurrentDeviceRegistrationVersion == 1);
return TrustedVaultRecoveryFactorRegistrationStateForUMA::
kAlreadyRegisteredV1;
}
if (per_user_vault->last_registration_returned_local_data_obsolete()) {
// Client already knows that existing vault keys (or their absence) isn't
// sufficient for device registration. Fresh keys should be obtained
// first.
return TrustedVaultRecoveryFactorRegistrationStateForUMA::
kLocalKeysAreStale;
}
if (connection_->AreRequestsThrottled(primary_account_)) {
return TrustedVaultRecoveryFactorRegistrationStateForUMA::
kThrottledClientSide;
}
std::unique_ptr<SecureBoxKeyPair> key_pair;
if (per_user_vault->has_local_device_registration_info()) {
key_pair = SecureBoxKeyPair::CreateByPrivateKeyImport(
/*private_key_bytes=*/ProtoStringToBytes(
per_user_vault->local_device_registration_info()
.private_key_material()));
}
const bool had_generated_key_pair = key_pair != nullptr;
if (!key_pair) {
key_pair = SecureBoxKeyPair::GenerateRandom();
// It's possible that device will be successfully registered, but the
// client won't persist this state (for example response doesn't reach the
// client or registration callback is cancelled). To avoid duplicated
// registrations device key is stored before sending the registration
// request, so the same key will be used for future registration attempts.
AssignBytesToProtoString(
key_pair->private_key().ExportToBytes(),
per_user_vault->mutable_local_device_registration_info()
->mutable_private_key_material());
storage_->WriteDataToDisk();
}
// `this` outlives `ongoing_registration_request_`, so it's safe to
// use base::Unretained() here.
if (StandaloneTrustedVaultStorage::HasNonConstantKey(*per_user_vault)) {
ongoing_registration_request_ = connection_->RegisterAuthenticationFactor(
primary_account_,
GetTrustedVaultKeysWithVersions(
StandaloneTrustedVaultStorage::GetAllVaultKeys(*per_user_vault),
per_user_vault->last_vault_key_version()),
key_pair->public_key(), LocalPhysicalDevice(),
base::BindOnce(&PhysicalDeviceRecoveryFactor::OnRegistered,
base::Unretained(this), std::move(cb), true));
} else {
ongoing_registration_request_ = connection_->RegisterLocalDeviceWithoutKeys(
primary_account_, key_pair->public_key(),
base::BindOnce(&PhysicalDeviceRecoveryFactor::OnRegistered,
base::Unretained(this), std::move(cb), false));
}
CHECK(ongoing_registration_request_);
return had_generated_key_pair
? TrustedVaultRecoveryFactorRegistrationStateForUMA::
kAttemptingRegistrationWithExistingKeyPair
: TrustedVaultRecoveryFactorRegistrationStateForUMA::
kAttemptingRegistrationWithNewKeyPair;
}
trusted_vault_pb::LocalTrustedVaultPerUser*
PhysicalDeviceRecoveryFactor::GetPrimaryAccountVault() {
auto* per_user_vault = storage_->FindUserVault(primary_account_.gaia);
// PhysicalDeviceRecoveryFactor is only constructed by
// StandaloneTrustedVaultBackend when a primary account is set, and it also
// ensures that there is a user vault in storage at the same time.
CHECK(per_user_vault);
return per_user_vault;
}
void PhysicalDeviceRecoveryFactor::OnKeysDownloaded(
AttemptRecoveryCallback cb,
TrustedVaultDownloadKeysStatus status,
const std::vector<std::vector<uint8_t>>& new_vault_keys,
int last_vault_key_version) {
// This method should be called only as a result of
// `ongoing_request_` completion/failure, verify this
// condition and destroy `ongoing_request_` as it's not
// needed anymore.
CHECK(ongoing_request_);
ongoing_request_ = nullptr;
RecordTrustedVaultDownloadKeysStatus(
LocalRecoveryFactorType::kPhysicalDevice, security_domain_id_,
GetDownloadKeysStatusForUMAFromResponse(status));
RecoveryStatus recovery_status = RecoveryStatus::kFailure;
switch (status) {
case TrustedVaultDownloadKeysStatus::kSuccess: {
recovery_status = RecoveryStatus::kSuccess;
break;
}
case TrustedVaultDownloadKeysStatus::kMemberNotFound:
case TrustedVaultDownloadKeysStatus::kMembershipNotFound:
case TrustedVaultDownloadKeysStatus::kMembershipCorrupted:
case TrustedVaultDownloadKeysStatus::kMembershipEmpty:
case TrustedVaultDownloadKeysStatus::kKeyProofsVerificationFailed: {
// Unable to download new keys due to known protocol errors. The only way
// to go out of these states is to receive new vault keys through external
// means. It's safe to mark device as not registered regardless of the
// cause (device registration will be triggered once new vault keys are
// available).
MarkAsNotRegistered();
break;
}
case TrustedVaultDownloadKeysStatus::kNoNewKeys: {
// The registration itself exists, but there's no additional keys to
// download. This is bad because key download attempts are triggered for
// the case where local keys have been marked as stale, which means the
// user is likely in an unrecoverable state.
connection_->RecordFailedRequestForThrottling(primary_account_);
recovery_status = RecoveryStatus::kNoNewKeys;
break;
}
case TrustedVaultDownloadKeysStatus::kAccessTokenFetchingFailure:
case TrustedVaultDownloadKeysStatus::kNetworkError:
// Request wasn't sent to the server, so there is no need for throttling.
break;
case TrustedVaultDownloadKeysStatus::kOtherError:
connection_->RecordFailedRequestForThrottling(primary_account_);
break;
}
std::move(cb).Run(recovery_status, new_vault_keys, last_vault_key_version);
}
void PhysicalDeviceRecoveryFactor::FulfillRecoveryWithFailure(
TrustedVaultDownloadKeysStatusForUMA status_for_uma,
AttemptRecoveryCallback cb) {
RecordTrustedVaultDownloadKeysStatus(LocalRecoveryFactorType::kPhysicalDevice,
security_domain_id_, status_for_uma);
base::BindPostTaskToCurrentDefault(
base::BindOnce(std::move(cb), RecoveryStatus::kFailure,
std::vector<std::vector<uint8_t>>(), 0))
.Run();
}
void PhysicalDeviceRecoveryFactor::OnRegistered(
RegisterCallback cb,
bool had_local_keys,
TrustedVaultRegistrationStatus status,
int key_version) {
// This method should be called only as a result of
// `ongoing_registration_request_` completion/failure, verify this
// condition and destroy `ongoing_registration_request_` as it's not
// needed anymore.
CHECK(ongoing_registration_request_);
ongoing_registration_request_ = nullptr;
auto* per_user_vault = GetPrimaryAccountVault();
// Registration is only attempted if there was no previous failure with
// `kLocalDataObsolete`. If this precondition wasn't guaranteed here, the
// field would need to be reset for some cases below such as `kSuccess` and
// `kAlreadyRegistered`.
CHECK(!per_user_vault->last_registration_returned_local_data_obsolete());
switch (status) {
case TrustedVaultRegistrationStatus::kSuccess:
case TrustedVaultRegistrationStatus::kAlreadyRegistered:
// kAlreadyRegistered handled as success, because it only means that
// client doesn't fully handled successful device registration before.
per_user_vault->mutable_local_device_registration_info()
->set_device_registered(true);
per_user_vault->mutable_local_device_registration_info()
->set_device_registered_version(kCurrentDeviceRegistrationVersion);
storage_->WriteDataToDisk();
break;
case TrustedVaultRegistrationStatus::kLocalDataObsolete:
per_user_vault->set_last_registration_returned_local_data_obsolete(true);
storage_->WriteDataToDisk();
break;
case TrustedVaultRegistrationStatus::kTransientAccessTokenFetchError:
case TrustedVaultRegistrationStatus::kPersistentAccessTokenFetchError:
case TrustedVaultRegistrationStatus::
kPrimaryAccountChangeAccessTokenFetchError:
case TrustedVaultRegistrationStatus::kNetworkError:
case TrustedVaultRegistrationStatus::kOtherError:
break;
}
std::move(cb).Run(status, key_version, had_local_keys);
}
} // namespace trusted_vault
|