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
|
// 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.
#include "chrome/elevation_service/elevator.h"
#include <dpapi.h>
#include <oleauto.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/process.h"
#include "base/strings/strcat.h"
#include "base/strings/sys_string_conversions.h"
#include "base/version_info/version_info.h"
#include "base/win/scoped_localalloc.h"
#include "base/win/windows_handle_util.h"
#include "build/branding_buildflags.h"
#include "chrome/elevation_service/caller_validation.h"
#include "chrome/elevation_service/elevated_recovery_impl.h"
#include "chrome/install_static/install_util.h"
#include "chrome/windows_services/service_program/get_calling_process.h"
#include "chrome/windows_services/service_program/scoped_client_impersonation.h"
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/elevation_service/internal/elevation_service_internal.h"
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
namespace elevation_service {
namespace {
ProtectionLevel RemoveFlags(ProtectionLevel protection_level,
EncryptFlags& flags) {
const uint32_t flag_value = internal::ExtractFlags(protection_level);
if (flag_value & internal::kFlagUseLatestKey) {
flags.use_latest_key = true;
}
return static_cast<ProtectionLevel>(
internal::ExtractProtectionLevel(protection_level));
}
} // namespace
HRESULT Elevator::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) {
base::win::ScopedHandle scoped_proc_handle;
HRESULT hr = RunChromeRecoveryCRX(base::FilePath(crx_path), browser_appid,
browser_version, session_id, caller_proc_id,
&scoped_proc_handle);
*proc_handle = base::win::HandleToUint32(scoped_proc_handle.Take());
return hr;
}
HRESULT Elevator::EncryptData(ProtectionLevel protection_level,
const BSTR plaintext,
BSTR* ciphertext,
DWORD* last_error) {
EncryptFlags flags;
protection_level = RemoveFlags(protection_level, flags);
if (protection_level >= ProtectionLevel::PROTECTION_MAX) {
return kErrorUnsupportedProtectionLevel;
}
UINT length = ::SysStringByteLen(plaintext);
if (!length)
return E_INVALIDARG;
std::string plaintext_str(reinterpret_cast<char*>(plaintext), length);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
InternalFlags pre_process_flags{.use_latest_encryption =
flags.use_latest_key};
auto pre_process_result = PreProcessData(plaintext_str, &pre_process_flags);
if (!pre_process_result.has_value()) {
return pre_process_result.error();
}
plaintext_str.swap(*pre_process_result);
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
DATA_BLOB intermediate = {};
if (ScopedClientImpersonation impersonate; impersonate.is_valid()) {
const auto calling_process = GetCallingProcess();
if (!calling_process.IsValid())
return kErrorCouldNotObtainCallingProcess;
const auto validation_data =
GenerateValidationData(protection_level, calling_process);
if (!validation_data.has_value()) {
return validation_data.error();
}
const auto data =
std::string(validation_data->cbegin(), validation_data->cend());
std::string data_to_encrypt;
AppendStringWithLength(data, data_to_encrypt);
AppendStringWithLength(plaintext_str, data_to_encrypt);
DATA_BLOB input = {};
input.cbData = base::checked_cast<DWORD>(data_to_encrypt.length());
input.pbData = const_cast<BYTE*>(
reinterpret_cast<const BYTE*>(data_to_encrypt.data()));
if (!::CryptProtectData(
&input, /*szDataDescr=*/
base::SysUTF8ToWide(base::StrCat({version_info::GetProductName(),
version_info::IsOfficialBuild()
? ""
: " (Developer Build)"}))
.c_str(),
nullptr, nullptr, nullptr, /*dwFlags=*/CRYPTPROTECT_AUDIT,
&intermediate)) {
*last_error = ::GetLastError();
return kErrorCouldNotEncryptWithUserContext;
}
} else {
return impersonate.result();
}
DATA_BLOB output = {};
{
base::win::ScopedLocalAlloc intermediate_freer(intermediate.pbData);
if (!::CryptProtectData(
&intermediate,
/*szDataDescr=*/
base::SysUTF8ToWide(version_info::GetProductName()).c_str(),
nullptr, nullptr, nullptr, /*dwFlags=*/CRYPTPROTECT_AUDIT,
&output)) {
*last_error = ::GetLastError();
return kErrorCouldNotEncryptWithSystemContext;
}
}
base::win::ScopedLocalAlloc output_freer(output.pbData);
*ciphertext = ::SysAllocStringByteLen(reinterpret_cast<LPCSTR>(output.pbData),
output.cbData);
if (!*ciphertext)
return E_OUTOFMEMORY;
return S_OK;
}
HRESULT Elevator::DecryptData(const BSTR ciphertext,
BSTR* plaintext,
DWORD* last_error) {
UINT length = ::SysStringByteLen(ciphertext);
if (!length)
return E_INVALIDARG;
DATA_BLOB input = {};
input.cbData = length;
input.pbData = reinterpret_cast<BYTE*>(ciphertext);
DATA_BLOB intermediate = {};
// Decrypt using the SYSTEM dpapi store.
if (!::CryptUnprotectData(&input, nullptr, nullptr, nullptr, nullptr, 0,
&intermediate)) {
*last_error = ::GetLastError();
return kErrorCouldNotDecryptWithSystemContext;
}
base::win::ScopedLocalAlloc intermediate_freer(intermediate.pbData);
std::string plaintext_str;
if (ScopedClientImpersonation impersonate; impersonate.is_valid()) {
DATA_BLOB output = {};
// Decrypt using the user store.
if (!::CryptUnprotectData(&intermediate, nullptr, nullptr, nullptr, nullptr,
0, &output)) {
*last_error = ::GetLastError();
return kErrorCouldNotDecryptWithUserContext;
}
base::win::ScopedLocalAlloc output_freer(output.pbData);
std::string mutable_plaintext(reinterpret_cast<char*>(output.pbData),
output.cbData);
const std::string validation_data = PopFromStringFront(mutable_plaintext);
if (validation_data.empty()) {
return E_INVALIDARG;
}
const auto data =
std::vector<uint8_t>(validation_data.cbegin(), validation_data.cend());
const auto process = GetCallingProcess();
if (!process.IsValid()) {
*last_error = ::GetLastError();
return kErrorCouldNotObtainCallingProcess;
}
// Note: Validation should always be done using caller impersonation token.
HRESULT validation_result = ValidateData(process, data);
if (FAILED(validation_result)) {
*last_error = ::GetLastError();
return validation_result;
}
plaintext_str = PopFromStringFront(mutable_plaintext);
} else {
return impersonate.result();
}
bool should_reencrypt = false;
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
InternalFlags flags;
auto post_process_result = PostProcessData(plaintext_str, &flags);
if (!post_process_result.has_value()) {
return post_process_result.error();
}
plaintext_str.swap(*post_process_result);
if (flags.post_process_should_reencrypt) {
should_reencrypt = true;
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
*plaintext =
::SysAllocStringByteLen(plaintext_str.c_str(), plaintext_str.length());
if (!*plaintext)
return E_OUTOFMEMORY;
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kFakeReencryptForTestingSwitch)) {
should_reencrypt = true;
}
return should_reencrypt ? kSuccessShouldReencrypt : S_OK;
}
// static
void Elevator::AppendStringWithLength(const std::string& to_append,
std::string& base) {
uint32_t size = base::checked_cast<uint32_t>(to_append.length());
base.append(reinterpret_cast<char*>(&size), sizeof(size));
base.append(to_append);
}
// static
std::string Elevator::PopFromStringFront(std::string& str) {
uint32_t size;
if (str.length() < sizeof(size))
return std::string();
auto it = str.begin();
// Obtain the size.
UNSAFE_TODO(memcpy(&size, str.c_str(), sizeof(size)));
// Skip over the size field.
std::string value;
if (size) {
it += sizeof(size);
// Pull the string out.
value.assign(it, it + size);
DCHECK_EQ(value.length(), base::checked_cast<std::string::size_type>(size));
}
// Trim the string to the remainder.
str = str.substr(sizeof(size) + size);
return value;
}
} // namespace elevation_service
|