File: app_bound_encryption_provider_win.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (109 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_OS_CRYPT_APP_BOUND_ENCRYPTION_PROVIDER_WIN_H_
#define CHROME_BROWSER_OS_CRYPT_APP_BOUND_ENCRYPTION_PROVIDER_WIN_H_

#include <optional>
#include <string>
#include <tuple>

#include "base/containers/span.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/threading/sequence_bound.h"
#include "base/types/expected.h"
#include "base/win/windows_types.h"
#include "chrome/browser/os_crypt/app_bound_encryption_win.h"
#include "components/os_crypt/async/browser/key_provider.h"

class PrefService;
class PrefRegistrySimple;

namespace os_crypt {
FORWARD_DECLARE_TEST(AppBoundEncryptionWinReencryptTest, KeyProviderTest);
FORWARD_DECLARE_TEST(AppBoundEncryptionProvider, Basic);
}  // namespace os_crypt

namespace os_crypt_async {

namespace features {

// If enabled, App-Bound encryption will request that the version 3 key be used
// for data encryption by the elevated service.
BASE_DECLARE_FEATURE(kAppBoundEncryptionKeyV3);

// If enabled, will re-generate a new key for catastrophic failures. See
// `DetermineErrorType` in the cc file for the two current cases.
BASE_DECLARE_FEATURE(kRegenerateKeyForCatastrophicFailures);

}  // namespace features

class AppBoundEncryptionProviderWin : public os_crypt_async::KeyProvider {
 public:
  explicit AppBoundEncryptionProviderWin(PrefService* local_state);
  ~AppBoundEncryptionProviderWin() override;

  // Not copyable.
  AppBoundEncryptionProviderWin(const AppBoundEncryptionProviderWin&) = delete;
  AppBoundEncryptionProviderWin& operator=(
      const AppBoundEncryptionProviderWin&) = delete;

  static void RegisterLocalPrefs(PrefRegistrySimple* registry);

 private:
  FRIEND_TEST_ALL_PREFIXES(os_crypt::AppBoundEncryptionWinReencryptTest,
                           KeyProviderTest);
  FRIEND_TEST_ALL_PREFIXES(os_crypt::AppBoundEncryptionProvider, Basic);

  using ReadOnlyKeyData = const std::vector<uint8_t>;
  using ReadWriteKeyData = std::vector<uint8_t>;
  using OptionalReadWriteKeyData = std::optional<ReadWriteKeyData>;
  using OptionalReadOnlyKeyData = std::optional<ReadOnlyKeyData>;

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class KeyRetrievalStatus {
    kSuccess = 0,
    kKeyNotFound = 1,
    kKeyDecodeFailure = 2,
    kInvalidKeyHeader = 3,
    kMaxValue = kInvalidKeyHeader,
  };

  // os_crypt_async::KeyProvider interface.
  void GetKey(KeyCallback callback) override;
  bool UseForEncryption() override;
  bool IsCompatibleWithOsCryptSync() override;

  void GenerateAndPersistNewKeyInternal(KeyCallback callback);
  base::expected<std::vector<uint8_t>, KeyRetrievalStatus>
  RetrieveEncryptedKey();
  void HandleEncryptedKey(ReadWriteKeyData decrypted_key,
                          KeyCallback callback,
                          const OptionalReadOnlyKeyData& encrypted_key);
  void StoreAndReplyWithKey(
      KeyCallback callback,
      base::expected<
          std::tuple<ReadWriteKeyData, const OptionalReadOnlyKeyData&>,
          KeyProvider::KeyError> key_pair);
  void StoreKey(base::span<const uint8_t> encrypted_key);

  raw_ptr<PrefService> local_state_ GUARDED_BY_CONTEXT(sequence_checker_);

  class COMWorker;
  base::SequenceBound<COMWorker> com_worker_;

  const os_crypt::SupportLevel support_level_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<AppBoundEncryptionProviderWin> weak_factory_{this};
};

}  // namespace os_crypt_async

#endif  // CHROME_BROWSER_OS_CRYPT_APP_BOUND_ENCRYPTION_PROVIDER_WIN_H_