File: elevator.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 (157 lines) | stat: -rw-r--r-- 6,165 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
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
// Copyright 2018 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_ELEVATION_SERVICE_ELEVATOR_H_
#define CHROME_ELEVATION_SERVICE_ELEVATOR_H_

#include <windows.h>

#include <wrl/implements.h>

#include <string>

#include "base/gtest_prod_util.h"
#include "chrome/elevation_service/elevation_service_idl.h"

namespace elevation_service {

// These flags can modify the EncryptData operation. They are packed inside the
// `protection_level` argument. Access these via the `EncryptAppBoundString` API
// in chrome.
struct EncryptFlags {
  // Specify that the Encrypt operation should always use the latest key.
  bool use_latest_key = false;
};

inline constexpr IID kTestElevatorClsid = {
    0x416C51AC,
    0x4DEF,
    0x43CA,
    {0xE7, 0x35, 0xE7, 0x35, 0x21, 0x0A, 0xB2,
     0x57}};  // Elevator Test CLSID. {416C51AC-4DEF-43CA-96E8-E735210AB257}

namespace switches {
inline constexpr char kElevatorClsIdForTestingSwitch[] =
    "elevator-clsid-for-testing";
inline constexpr char kFakeReencryptForTestingSwitch[] =
    "elevator-fake-reencrypt-for-testing";
}  // namespace switches

namespace internal {

inline constexpr uint32_t kFlagUseLatestKey = 1 << 23;

// Update this each time a new flag is added.
inline constexpr uint32_t kMaxFlag = kFlagUseLatestKey;

// A static assert verifies the flags can always fit into 24 bits.
static_assert((kMaxFlag & 0xFFFFFF) == kMaxFlag);

constexpr uint32_t PackFlagsAndProtectionLevel(uint32_t flags,
                                               uint8_t protection_level) {
  return ((flags & 0xFFFFFF) << 8) | protection_level;
}

constexpr uint32_t ExtractFlags(uint32_t packed) {
  return (packed >> 8) & 0xFFFFFF;
}

constexpr uint8_t ExtractProtectionLevel(uint32_t packed) {
  return static_cast<uint8_t>(packed & 0xFF);
}

// Tests for these basic functions can be static asserts.
static_assert(ExtractProtectionLevel(PackFlagsAndProtectionLevel(0, 0)) == 0);
static_assert(ExtractFlags(PackFlagsAndProtectionLevel(0x123456, 0x01)) ==
              0x123456);
static_assert(ExtractFlags(PackFlagsAndProtectionLevel(0xAA123456, 0)) ==
              0x123456);
static_assert(ExtractFlags(PackFlagsAndProtectionLevel(0, 0)) == 0);
static_assert(ExtractProtectionLevel(PackFlagsAndProtectionLevel(0, 0x01)) ==
              0x01);
static_assert(
    ExtractProtectionLevel(PackFlagsAndProtectionLevel(0x1234, 0x01)) == 0x01);
static_assert(ExtractProtectionLevel(PackFlagsAndProtectionLevel(0x12345678,
                                                                 0x01)) ==
              0x01);

}  // namespace internal

class Elevator
    : public Microsoft::WRL::RuntimeClass<
          Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
          IElevator,
          IElevatorChromium,
          IElevatorChrome,
          IElevatorChromeBeta,
          IElevatorChromeDev,
          IElevatorChromeCanary> {
 public:
  // Failure codes.
  static constexpr HRESULT kErrorCouldNotObtainCallingProcess =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA001);
  static constexpr HRESULT kErrorCouldNotGenerateValidationData =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA002);
  static constexpr HRESULT kErrorCouldNotDecryptWithUserContext =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA003);
  static constexpr HRESULT kErrorCouldNotDecryptWithSystemContext =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA004);
  static constexpr HRESULT kErrorCouldNotEncryptWithUserContext =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA005);
  static constexpr HRESULT kErrorCouldNotEncryptWithSystemContext =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA006);
  static constexpr HRESULT kValidationDidNotPass =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA007);
  static constexpr HRESULT kErrorCouldNotObtainPath =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA008);
  static constexpr HRESULT kErrorUnsupportedFilePath =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA009);
  static constexpr HRESULT kErrorUnsupportedProtectionLevel =
      MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0xA00A);

  // Success codes.
  static constexpr HRESULT kSuccessShouldReencrypt =
      MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_ITF, 0xA001);

  Elevator() = default;

  Elevator(const Elevator&) = delete;
  Elevator& operator=(const Elevator&) = delete;

  // Securely validates and runs the provided Chrome Recovery CRX elevated, by
  // first copying the CRX to a secure directory under %ProgramFiles% to
  // validate and unpack the CRX.
  IFACEMETHODIMP RunRecoveryCRXElevated(const wchar_t* crx_path,
                                        const wchar_t* browser_appid,
                                        const wchar_t* browser_version,
                                        const wchar_t* session_id,
                                        DWORD caller_proc_id,
                                        ULONG_PTR* proc_handle) override;
  IFACEMETHODIMP EncryptData(ProtectionLevel protection_level,
                             const BSTR plaintext,
                             BSTR* ciphertext,
                             DWORD* last_error) override;
  IFACEMETHODIMP DecryptData(const BSTR ciphertext,
                             BSTR* plaintext,
                             DWORD* last_error) override;

 private:
  FRIEND_TEST_ALL_PREFIXES(ElevatorTest, StringHandlingTest);
  ~Elevator() override = default;

  // Appends a string `to_append` to an existing string `base`, first the four
  // byte length then the string.
  static void AppendStringWithLength(const std::string& to_append,
                                     std::string& base);

  // Pulls a string from the start of the string, `str` is shortened to the
  // remainder of the string. `str` should have been a string previously passed
  // to AppendStringWithLength, so that it contains the four byte prefix of
  // length.
  static std::string PopFromStringFront(std::string& str);
};

}  // namespace elevation_service

#endif  // CHROME_ELEVATION_SERVICE_ELEVATOR_H_