File: app_bound_encryption_provider_win.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,770 bytes parent folder | download | duplicates (3)
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
// 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 <utility>
#include <vector>

#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 {
class AppBoundEncryptionProviderTest;
FORWARD_DECLARE_TEST(AppBoundEncryptionWinReencryptTest, KeyProviderTest);
}  // 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 class os_crypt::AppBoundEncryptionProviderTest;

  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,
    kKeyTooShort = 4,
    kMaxValue = kKeyTooShort,
  };

  // 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,
                          OptionalReadOnlyKeyData encrypted_key);
  void StoreAndReplyWithKey(
      KeyCallback callback,
      base::expected<std::pair<ReadWriteKeyData, 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_