File: auth_factor.h

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 (332 lines) | stat: -rw-r--r-- 11,867 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef CHROMEOS_ASH_COMPONENTS_CRYPTOHOME_AUTH_FACTOR_H_
#define CHROMEOS_ASH_COMPONENTS_CRYPTOHOME_AUTH_FACTOR_H_

#include <optional>
#include <string>
#include <variant>

#include "base/component_export.h"
#include "base/containers/enum_set.h"
#include "base/time/time.h"
#include "chromeos/ash/components/cryptohome/common_types.h"

namespace cryptohome {

// All authfactors created/modified after AuthFactor API is launched should
// have OS/Chrome versions filled in CommonMetadata.
// However, any factor created before the launch would miss such information.
// In order to keep rest of the code simple, this "zero" value would be used
// upon receiving AuthFactor from cryptohome.
// Note that this value should not be passed back to cryptohome, this
// is guarded by CHECKs in serialization code.
COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
extern const char kFallbackFactorVersion[];

enum class AuthFactorType {
  // Special edge case - on old ChromeOS versions Kiosk keys and passwords for
  // regular users had no metadata to distinguish them on cryptohome level,
  // only Chrome can do that based on UserType.
  // This type can be returned when retrieving data from cryptohome,
  // but should not be used in any data passed from chrome to cryptohome.
  // This factor type is not included in `AuthFactorsSet`.
  kUnknownLegacy,

  // This is a synthetic factor, there is no actual key associated with this
  // factor, it is only used as indicator of specific authentication mode.
  // As such it can never be returned by cryptohome, and is not included in
  // `AuthFactorsSet`.
  kLegacyFingerprint,

  kPassword,
  kPin,
  kRecovery,
  kSmartCard,
  kKiosk,
  kFingerprint,
};

enum class LockoutPolicy {
  // Default value.
  kUnknown = 0,
  // Ideally this will be default, but should be explicitly set for an
  // AuthFactor where multiple attempts are allowed without any repercussions.
  kNone,
  // kAttemptLimited is for an AuthFactors that is not available for a
  // user x number of wrong attempts. For example, for PIN we lockout
  // user from PIN usage after five attempts.
  kAttemptLimited,
  // kTimeLimited is when we locked out user from using a certain AuthFactor
  // for a particular time after particular number of wrong attempts.
  // The policy specifics itself can be custom defined by an AuthFactor.
  kTimeLimited,
};

using AuthFactorsSet = base::
    EnumSet<AuthFactorType, AuthFactorType::kPassword, AuthFactorType::kKiosk>;

// Reference to a particular AuthFactor.
// While `label` uniquely identifies factor across all factor types,
// it is convenient to pass AuthFactorType along.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) AuthFactorRef {
 public:
  AuthFactorRef(AuthFactorType type, KeyLabel label);

  AuthFactorRef(AuthFactorRef&&);
  AuthFactorRef& operator=(AuthFactorRef&&);

  AuthFactorRef(const AuthFactorRef&);
  AuthFactorRef& operator=(const AuthFactorRef&);

  ~AuthFactorRef();

  bool operator==(const AuthFactorRef& other) const;

  AuthFactorType type() const { return type_; }

  const KeyLabel& label() const { return label_; }

 private:
  AuthFactorType type_;
  KeyLabel label_;
};

// Each auth factor supported by cryptohome has 4 types of data associated with
// it:
//   * factor identifiers: type and label (though label can be changed by
//     cryptohome);
//   * factor input: part of data that is write-only by Chrome, e.g.
//     during setting up a factor, or attempting an authentication;
//   * factor status: data that is set by cryptohome and is read-only on the
//     Chrome side, e.g. PIN lockout status;
//   * factor metadata: non-identifying data associated with factor that can
//     be both read and written by Chrome.

// Common metadata that should be defined for each auth factor.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    AuthFactorCommonMetadata {
 public:
  // This constructor should be used for AuthFactors created on the Chrome side.
  // It fills in current Chrome version, leaving ChromeOS version empty.
  AuthFactorCommonMetadata();
  AuthFactorCommonMetadata(ComponentVersion chrome,
                           ComponentVersion chromeos,
                           LockoutPolicy lockout_policy);
  ~AuthFactorCommonMetadata();

  AuthFactorCommonMetadata(AuthFactorCommonMetadata&&) noexcept;
  AuthFactorCommonMetadata& operator=(AuthFactorCommonMetadata&&) noexcept;

  AuthFactorCommonMetadata(const AuthFactorCommonMetadata&);
  AuthFactorCommonMetadata& operator=(const AuthFactorCommonMetadata&);

  // Should only be used for testing purposes.
  bool operator==(const AuthFactorCommonMetadata& other) const;

  const ComponentVersion& chrome_version_last_updated() const {
    return chrome_version_last_updated_;
  }

  const ComponentVersion& chromeos_version_last_updated() const {
    return chromeos_version_last_updated_;
  }

  const LockoutPolicy& lockout_policy() const { return lockout_policy_; }

 private:
  ComponentVersion chrome_version_last_updated_;
  ComponentVersion chromeos_version_last_updated_;
  LockoutPolicy lockout_policy_;
};

// Per-factor statuses (read-only properties set by cryptohomed):
// TODO(b/341733466): Individual per-factor statuses are discouraged.
// A general auth factor status is already returned by cryptohomed for
// each AuthFactorWithStatus.

// PinStatus provides the pin status info returned from cryptohomed.
// PinStatus only represents a status snapshot at the time of its
// construction. Care must be taken in checking the freshness of
// any PinStatus object.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) PinStatus {
 public:
  // Default constructor: the pin factor is immediately available.
  PinStatus();
  // Constructor takes one TimeDelta value:
  // |available_in| indicates a timeout after which the factor will become
  // available.
  //   0 means the factor is immediately available.
  //   TimeDelta::Max() means the factor is locked out indefinitely.
  PinStatus(base::TimeDelta available_in);

  PinStatus(PinStatus&&) noexcept;
  PinStatus& operator=(PinStatus&&) noexcept;

  PinStatus(const PinStatus&);
  PinStatus& operator=(const PinStatus&);

  ~PinStatus();

  // The time when the pin auth factor will be available.
  // If locked out indefinitely, return Time::Max().
  base::Time AvailableAt() const;

  // Indicates a not-avaiable pin.
  bool IsLockedFactor() const;

 private:
  base::Time available_at_;
};

// Represents the time when the pin auth factor will be available. If the field
// is not present, it means the PIN is enabled or disabled permanently.
using PinLockAvailability = std::optional<base::Time>;

// Common types used in factor-specific metadata:

// The hash information of an auth factor that is used to generate the actual
// secret. This can be used to generate the recoverable key store for the auth
// factor.
struct COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    KnowledgeFactorHashInfo {
  KnowledgeFactorHashAlgorithmWrapper algorithm;
  std::string salt;
  bool should_generate_key_store;
};

// Factor-specific metadata:

struct COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) SmartCardMetadata {
  std::string public_key_spki_der;
};

struct COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME)
    CryptohomeRecoveryMetadata {
  std::string mediator_pub_key;
};

class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) PasswordMetadata {
 public:
  // Constructors that do/don't include the hash info field. Difference with
  // online and local password metadata is that only local password metadata
  // needs to generate recoverable key stores.
  // Generating recoverable key stores will make the password a recovery
  // factor for some Google sync services, and we only want to do it for local
  // passwords but not GAIA passwords. This is because the user already needs
  // their GAIA password to access the Google sync services, so making the GAIA
  // password a sync recovery factor doesn't provide any extra layer of
  // security.
  static PasswordMetadata CreateWithoutSalt();
  static PasswordMetadata CreateForOnlinePassword(SystemSalt salt);
  static PasswordMetadata CreateForLocalPassword(SystemSalt salt);

  PasswordMetadata(PasswordMetadata&&) noexcept;
  PasswordMetadata& operator=(PasswordMetadata&&) noexcept;

  PasswordMetadata(const PasswordMetadata&);
  PasswordMetadata& operator=(const PasswordMetadata&);

  ~PasswordMetadata();

  const std::optional<KnowledgeFactorHashInfo>& hash_info() const {
    return hash_info_;
  }

 private:
  PasswordMetadata(std::optional<KnowledgeFactorHashInfo> hash_info);

  std::optional<KnowledgeFactorHashInfo> hash_info_;
};

class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) PinMetadata {
 public:
  static PinMetadata CreateWithoutSalt();
  static PinMetadata Create(PinSalt salt);

  PinMetadata(PinMetadata&&) noexcept;
  PinMetadata& operator=(PinMetadata&&) noexcept;

  PinMetadata(const PinMetadata&);
  PinMetadata& operator=(const PinMetadata&);

  ~PinMetadata();

  const std::optional<KnowledgeFactorHashInfo>& hash_info() const {
    return hash_info_;
  }

 private:
  PinMetadata(std::optional<KnowledgeFactorHashInfo> hash_info);

  std::optional<KnowledgeFactorHashInfo> hash_info_;
};

class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) FingerprintMetadata {
};

// AuthFactor definition.
// If it is obtainted from `cryptohome` it will contain factor-specific status,
// otherwise it would only contain identity and metadata.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_CRYPTOHOME) AuthFactor {
 public:
  AuthFactor(AuthFactorRef ref, AuthFactorCommonMetadata metadata);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             SmartCardMetadata smartcard_metadata);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             CryptohomeRecoveryMetadata recovery_metadata);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             PasswordMetadata password_metadata);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             PinMetadata pin_metadata);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             PinMetadata pin_metadata,
             PinStatus status);
  AuthFactor(AuthFactorRef ref,
             AuthFactorCommonMetadata metadata,
             FingerprintMetadata fingerprint_metadata);

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

  ~AuthFactor();

  // Should only be used for testing purposes.
  bool operator==(const AuthFactor& other) const;

  const AuthFactorRef& ref() const;
  const AuthFactorCommonMetadata& GetCommonMetadata() const;

  // Fails if type does not match:
  const PinStatus& GetPinStatus() const;
  const SmartCardMetadata& GetSmartCardMetadata() const;
  const CryptohomeRecoveryMetadata& GetCryptohomeRecoveryMetadata() const;
  const PasswordMetadata& GetPasswordMetadata() const;
  const PinMetadata& GetPinMetadata() const;
  const FingerprintMetadata& GetFingerprintMetadata() const;

 private:
  AuthFactorRef ref_;
  AuthFactorCommonMetadata common_metadata_;
  std::variant<std::monostate,
               SmartCardMetadata,
               CryptohomeRecoveryMetadata,
               PasswordMetadata,
               PinMetadata,
               FingerprintMetadata>
      factor_metadata_;
  std::variant<std::monostate, PinStatus> factor_status_;
};

}  // namespace cryptohome

#endif  // CHROMEOS_ASH_COMPONENTS_CRYPTOHOME_AUTH_FACTOR_H_