File: cryptohome_pin_engine.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download | duplicates (7)
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
// 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/engines/cryptohome_pin_engine.h"

#include <optional>
#include <string>
#include <utility>

#include "ash/constants/ash_pref_names.h"
#include "base/check.h"
#include "base/logging.h"
#include "chromeos/ash/components/cryptohome/auth_factor.h"
#include "chromeos/ash/components/login/auth/auth_performer.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/impl/engines/cryptohome_based_engine.h"
#include "chromeos/ash/components/osauth/public/auth_factor_engine.h"
#include "chromeos/ash/components/osauth/public/common_types.h"
#include "chromeos/ash/components/osauth/public/cryptohome_core.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/known_user.h"

namespace ash {

CryptohomePinEngine::CryptohomePinEngine(CryptohomeCore& core,
                                         PrefService* local_state)
    : CryptohomeBasedEngine(core, AshAuthFactor::kCryptohomePin),
      local_state_(local_state) {}

CryptohomePinEngine::~CryptohomePinEngine() = default;

std::optional<cryptohome::AuthFactorRef> CryptohomePinEngine::LookUpFactor(
    UserContext& context) {
  const cryptohome::AuthFactor* pin_factor =
      context.GetAuthFactorsData().FindPinFactor();
  if (!pin_factor) {
    return std::nullopt;
  }
  return pin_factor->ref();
}

void CryptohomePinEngine::OnAuthFactorUpdate(cryptohome::AuthFactorRef factor) {
}

bool CryptohomePinEngine::IsDisabledByPolicy() {
  return false;
}

bool CryptohomePinEngine::IsLockedOut() {
  return false;
}

bool CryptohomePinEngine::IsFactorSpecificRestricted() {
  return false;
}

void CryptohomePinEngine::PerformPinAttempt(const std::string& raw_pin) {
  if (get_usage_allowed() != UsageAllowed::kEnabled) {
    LOG(ERROR) << "Ignoring pin attempt as factor is disabled";
    return;
  }
  CHECK(get_ref().has_value());
  get_observer()->OnFactorAttempt(GetFactor());
  get_core()->BorrowContext(
      base::BindOnce(&CryptohomePinEngine::PerformAuthenticationAttempt,
                     weak_factory_.GetWeakPtr(), raw_pin));
}

void CryptohomePinEngine::OnAuthAttempt(
    std::unique_ptr<UserContext> context,
    std::optional<AuthenticationError> error) {
  get_core()->ReturnContext(std::move(context));
  get_observer()->OnFactorAttemptResult(GetFactor(),
                                        /* success= */ !error.has_value());
}

void CryptohomePinEngine::PerformAuthenticationAttempt(
    const std::string& raw_pin,
    std::unique_ptr<UserContext> context) {
  const AccountId& account_id = context->GetAccountId();
  get_core()->GetAuthPerformer()->AuthenticateWithPin(
      raw_pin, GetUserSalt(account_id, local_state_), std::move(context),
      base::BindOnce(&CryptohomePinEngine::OnAuthAttempt,
                     weak_factory_.GetWeakPtr()));
}

std::string CryptohomePinEngine::GetUserSalt(const AccountId& account_id,
                                             PrefService* local_state) const {
  user_manager::KnownUser known_user(local_state);
  if (const std::string* salt =
          known_user.FindStringPath(account_id, prefs::kQuickUnlockPinSalt)) {
    return *salt;
  }
  return {};
}

CryptohomePinEngineFactory::CryptohomePinEngineFactory(PrefService* local_state)
    : local_state_(local_state) {}

CryptohomePinEngineFactory::~CryptohomePinEngineFactory() = default;

AshAuthFactor CryptohomePinEngineFactory::GetFactor() {
  return AshAuthFactor::kCryptohomePin;
}

std::unique_ptr<AuthFactorEngine> CryptohomePinEngineFactory::CreateEngine(
    AuthHubMode mode) {
  CHECK(CryptohomeCore::Get());
  return std::make_unique<CryptohomePinEngine>(*CryptohomeCore::Get(),
                                               local_state_);
}

}  // namespace ash