File: auth_factor.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 (244 lines) | stat: -rw-r--r-- 8,892 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
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
// 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 "chromeos/ash/components/cryptohome/auth_factor.h"

#include <utility>
#include <variant>

#include "base/check_op.h"
#include "base/time/time.h"
#include "chromeos/ash/components/cryptohome/common_types.h"
#include "components/version_info/version_info.h"

namespace cryptohome {

const char kFallbackFactorVersion[] = "0.0.0.0";

// =============== `AuthFactorRef` ===============
AuthFactorRef::AuthFactorRef(AuthFactorType type, KeyLabel label)
    : type_(type), label_(std::move(label)) {
  DCHECK(!label_->empty());
}
AuthFactorRef::AuthFactorRef(AuthFactorRef&&) = default;
AuthFactorRef& AuthFactorRef::operator=(AuthFactorRef&&) = default;
AuthFactorRef::AuthFactorRef(const AuthFactorRef&) = default;
AuthFactorRef& AuthFactorRef::operator=(const AuthFactorRef&) = default;
AuthFactorRef::~AuthFactorRef() = default;

bool AuthFactorRef::operator==(const AuthFactorRef& other) const {
  const bool equal = label_.value() == other.label_.value();
  CHECK(!equal || type_ == other.type_);
  return equal;
}

// =============== `AuthFactorCommonMetadata` ===============
AuthFactorCommonMetadata::AuthFactorCommonMetadata()
    : chrome_version_last_updated_(
          ComponentVersion(std::string(version_info::GetVersionNumber()))) {}

AuthFactorCommonMetadata::AuthFactorCommonMetadata(ComponentVersion chrome,
                                                   ComponentVersion chromeos,
                                                   LockoutPolicy lockout_policy)
    : chrome_version_last_updated_(std::move(chrome)),
      chromeos_version_last_updated_(std::move(chromeos)),
      lockout_policy_(lockout_policy) {}

AuthFactorCommonMetadata::AuthFactorCommonMetadata(
    AuthFactorCommonMetadata&&) noexcept = default;
AuthFactorCommonMetadata& AuthFactorCommonMetadata::operator=(
    AuthFactorCommonMetadata&&) noexcept = default;
AuthFactorCommonMetadata::AuthFactorCommonMetadata(
    const AuthFactorCommonMetadata&) = default;
AuthFactorCommonMetadata& AuthFactorCommonMetadata::operator=(
    const AuthFactorCommonMetadata&) = default;
AuthFactorCommonMetadata::~AuthFactorCommonMetadata() = default;

bool AuthFactorCommonMetadata::operator==(
    const AuthFactorCommonMetadata& other) const {
  return (this->chrome_version_last_updated_ ==
          other.chrome_version_last_updated_) &&
         (this->chromeos_version_last_updated_ ==
          other.chromeos_version_last_updated_);
}

// =============== `Factor-specific Status` ===============
PinStatus::PinStatus() : available_at_(base::Time::Now()) {}

PinStatus::PinStatus(base::TimeDelta available_in)
    : available_at_(base::Time::Now() + available_in) {}

PinStatus::PinStatus(PinStatus&&) noexcept = default;
PinStatus& PinStatus::operator=(PinStatus&&) noexcept = default;
PinStatus::PinStatus(const PinStatus&) = default;
PinStatus& PinStatus::operator=(const PinStatus&) = default;
PinStatus::~PinStatus() = default;

bool PinStatus::IsLockedFactor() const {
  return base::Time::Now() < available_at_;
}

base::Time PinStatus::AvailableAt() const {
  return available_at_;
}

// =============== `Factor-specific Metadata` ===============

PasswordMetadata PasswordMetadata::CreateWithoutSalt() {
  return PasswordMetadata(std::nullopt);
}

PasswordMetadata PasswordMetadata::CreateForOnlinePassword(SystemSalt salt) {
  return PasswordMetadata(KnowledgeFactorHashInfo{
      .algorithm = KnowledgeFactorHashAlgorithmWrapper::kSha256TopHalf,
      .salt = std::move(*salt),
      .should_generate_key_store = false,
  });
}

PasswordMetadata PasswordMetadata::CreateForLocalPassword(SystemSalt salt) {
  return PasswordMetadata(KnowledgeFactorHashInfo{
      .algorithm = KnowledgeFactorHashAlgorithmWrapper::kSha256TopHalf,
      .salt = std::move(*salt),
      .should_generate_key_store = true,
  });
}

PasswordMetadata::PasswordMetadata(
    std::optional<KnowledgeFactorHashInfo> hash_info)
    : hash_info_(std::move(hash_info)) {}
PasswordMetadata::PasswordMetadata(PasswordMetadata&&) noexcept = default;
PasswordMetadata& PasswordMetadata::operator=(PasswordMetadata&&) noexcept =
    default;
PasswordMetadata::PasswordMetadata(const PasswordMetadata&) = default;
PasswordMetadata& PasswordMetadata::operator=(const PasswordMetadata&) =
    default;
PasswordMetadata::~PasswordMetadata() = default;

PinMetadata PinMetadata::CreateWithoutSalt() {
  return PinMetadata(std::nullopt);
}

PinMetadata PinMetadata::Create(PinSalt salt) {
  return PinMetadata(KnowledgeFactorHashInfo{
      .algorithm = KnowledgeFactorHashAlgorithmWrapper::kPbkdf2Aes2561234,
      .salt = std::move(*salt),
      .should_generate_key_store = true,
  });
}

PinMetadata::PinMetadata(std::optional<KnowledgeFactorHashInfo> hash_info)
    : hash_info_(std::move(hash_info)) {}
PinMetadata::PinMetadata(PinMetadata&&) noexcept = default;
PinMetadata& PinMetadata::operator=(PinMetadata&&) noexcept = default;
PinMetadata::PinMetadata(const PinMetadata&) = default;
PinMetadata& PinMetadata::operator=(const PinMetadata&) = default;
PinMetadata::~PinMetadata() = default;

// =============== `AuthFactor` ===============

AuthFactor::AuthFactor(AuthFactorRef ref, AuthFactorCommonMetadata metadata)
    : ref_(std::move(ref)), common_metadata_(std::move(metadata)) {}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       PinMetadata pin_metadata,
                       PinStatus status)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(pin_metadata)),
      factor_status_(std::move(status)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kPin);
}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       SmartCardMetadata factor_metadata)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(factor_metadata)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kSmartCard);
}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       CryptohomeRecoveryMetadata factor_metadata)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(factor_metadata)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kRecovery);
}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       PasswordMetadata factor_metadata)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(factor_metadata)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kPassword);
}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       PinMetadata factor_metadata)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(factor_metadata)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kPin);
}

AuthFactor::AuthFactor(AuthFactorRef ref,
                       AuthFactorCommonMetadata metadata,
                       FingerprintMetadata fingerprint_metadata)
    : ref_(std::move(ref)),
      common_metadata_(std::move(metadata)),
      factor_metadata_(std::move(fingerprint_metadata)) {
  CHECK_EQ(ref_.type(), AuthFactorType::kFingerprint);
}

AuthFactor::AuthFactor(AuthFactor&&) noexcept = default;
AuthFactor& AuthFactor::operator=(AuthFactor&&) noexcept = default;
AuthFactor::AuthFactor(const AuthFactor&) = default;
AuthFactor& AuthFactor::operator=(const AuthFactor&) = default;

AuthFactor::~AuthFactor() = default;

bool AuthFactor::operator==(const AuthFactor& other) const {
  return ref_ == other.ref_ && common_metadata_ == other.common_metadata_;
}

const AuthFactorRef& AuthFactor::ref() const {
  return ref_;
}

const AuthFactorCommonMetadata& AuthFactor::GetCommonMetadata() const {
  return common_metadata_;
}

const PinStatus& AuthFactor::GetPinStatus() const {
  return std::get<PinStatus>(factor_status_);
}

const SmartCardMetadata& AuthFactor::GetSmartCardMetadata() const {
  return std::get<SmartCardMetadata>(factor_metadata_);
}

const CryptohomeRecoveryMetadata& AuthFactor::GetCryptohomeRecoveryMetadata()
    const {
  return std::get<CryptohomeRecoveryMetadata>(factor_metadata_);
}

const PasswordMetadata& AuthFactor::GetPasswordMetadata() const {
  return std::get<PasswordMetadata>(factor_metadata_);
}

const PinMetadata& AuthFactor::GetPinMetadata() const {
  return std::get<PinMetadata>(factor_metadata_);
}

const FingerprintMetadata& AuthFactor::GetFingerprintMetadata() const {
  return std::get<FingerprintMetadata>(factor_metadata_);
}

}  // namespace cryptohome