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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/pairing_registry_delegate_win.h"
#include <windows.h>
#include <optional>
#include <string>
#include <utility>
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/win/registry.h"
namespace remoting {
namespace {
// Duplicates a registry key handle (returned by RegCreateXxx/RegOpenXxx).
// The returned handle cannot be inherited and has the same permissions as
// the source one.
bool DuplicateKeyHandle(HKEY source, base::win::RegKey* dest) {
HANDLE handle;
if (!DuplicateHandle(GetCurrentProcess(),
source,
GetCurrentProcess(),
&handle,
0,
FALSE,
DUPLICATE_SAME_ACCESS)) {
PLOG(ERROR) << "Failed to duplicate a registry key handle";
return false;
}
dest->Set(reinterpret_cast<HKEY>(handle));
return true;
}
// Reads value |value_name| from |key| as a JSON string and returns it as
// |base::Value|.
std::optional<base::Value::Dict> ReadValue(const base::win::RegKey& key,
const wchar_t* value_name) {
// presubmit: allow wstring
std::wstring value_json;
LONG result = key.ReadValue(value_name, &value_json);
if (result != ERROR_SUCCESS) {
SetLastError(result);
PLOG(ERROR) << "Cannot read value '" << value_name << "'";
return std::nullopt;
}
// Parse the value.
std::string value_json_utf8 = base::WideToUTF8(value_json);
base::JSONReader::Result value =
base::JSONReader::ReadAndReturnValueWithError(value_json_utf8);
if (!value.has_value()) {
LOG(ERROR) << "Failed to parse '" << value_name
<< "': " << value.error().ToString();
return std::nullopt;
}
if (!value->is_dict()) {
LOG(ERROR) << "Failed to parse '" << value_name << "': not a dictionary.";
return std::nullopt;
}
return std::move(*value).TakeDict();
}
// Serializes |value| into a JSON string and writes it as value |value_name|
// under |key|.
bool WriteValue(base::win::RegKey& key,
const wchar_t* value_name,
const base::Value::Dict& value) {
std::optional<std::string> value_json_utf8 = base::WriteJson(value);
if (!value_json_utf8.has_value()) {
LOG(ERROR) << "Failed to serialize '" << value_name << "'";
return false;
}
// presubmit: allow wstring
std::wstring value_json = base::UTF8ToWide(*value_json_utf8);
LONG result = key.WriteValue(value_name, value_json.c_str());
if (result != ERROR_SUCCESS) {
SetLastError(result);
PLOG(ERROR) << "Cannot write value '" << value_name << "'";
return false;
}
return true;
}
} // namespace
using protocol::PairingRegistry;
PairingRegistryDelegateWin::PairingRegistryDelegateWin() {}
PairingRegistryDelegateWin::~PairingRegistryDelegateWin() {}
bool PairingRegistryDelegateWin::SetRootKeys(HKEY privileged,
HKEY unprivileged) {
DCHECK(!privileged_.Valid());
DCHECK(!unprivileged_.Valid());
DCHECK(unprivileged);
if (!DuplicateKeyHandle(unprivileged, &unprivileged_)) {
return false;
}
if (privileged) {
if (!DuplicateKeyHandle(privileged, &privileged_)) {
return false;
}
}
return true;
}
base::Value::List PairingRegistryDelegateWin::LoadAll() {
base::Value::List pairings;
// Enumerate and parse all values under the unprivileged key.
DWORD count = unprivileged_.GetValueCount().value_or(0);
for (DWORD index = 0; index < count; ++index) {
// presubmit: allow wstring
std::wstring value_name;
LONG result = unprivileged_.GetValueNameAt(index, &value_name);
if (result != ERROR_SUCCESS) {
SetLastError(result);
PLOG(ERROR) << "Cannot get the name of value " << index;
continue;
}
PairingRegistry::Pairing pairing = Load(base::WideToUTF8(value_name));
if (pairing.is_valid()) {
pairings.Append(pairing.ToValue());
}
}
return pairings;
}
bool PairingRegistryDelegateWin::DeleteAll() {
if (!privileged_.Valid()) {
LOG(ERROR) << "Cannot delete pairings: the delegate is read-only.";
return false;
}
// Enumerate and delete the values in the privileged and unprivileged keys
// separately in case they get out of sync.
bool success = true;
DWORD count = unprivileged_.GetValueCount().value_or(0);
while (count > 0) {
// presubmit: allow wstring
std::wstring value_name;
LONG result = unprivileged_.GetValueNameAt(0, &value_name);
if (result == ERROR_SUCCESS) {
result = unprivileged_.DeleteValue(value_name.c_str());
}
success = success && (result == ERROR_SUCCESS);
count = unprivileged_.GetValueCount().value_or(0);
}
count = privileged_.GetValueCount().value_or(0);
while (count > 0) {
// presubmit: allow wstring
std::wstring value_name;
LONG result = privileged_.GetValueNameAt(0, &value_name);
if (result == ERROR_SUCCESS) {
result = privileged_.DeleteValue(value_name.c_str());
}
success = success && (result == ERROR_SUCCESS);
count = privileged_.GetValueCount().value_or(0);
}
return success;
}
PairingRegistry::Pairing PairingRegistryDelegateWin::Load(
const std::string& client_id) {
// presubmit: allow wstring
std::wstring value_name = base::UTF8ToWide(client_id);
// Read unprivileged fields first.
std::optional<base::Value::Dict> pairing =
ReadValue(unprivileged_, value_name.c_str());
if (!pairing) {
return PairingRegistry::Pairing();
}
// Read the shared secret.
if (privileged_.Valid()) {
std::optional<base::Value::Dict> secret =
ReadValue(privileged_, value_name.c_str());
if (!secret) {
return PairingRegistry::Pairing();
}
// Merge the two dictionaries.
pairing->Merge(std::move(*secret));
}
return PairingRegistry::Pairing::CreateFromValue(*pairing);
}
bool PairingRegistryDelegateWin::Save(const PairingRegistry::Pairing& pairing) {
if (!privileged_.Valid()) {
LOG(ERROR) << "Cannot save pairing entry '" << pairing.client_id()
<< "': the pairing registry privileged key is invalid.";
return false;
}
// Convert pairing to JSON.
base::Value::Dict pairing_json = pairing.ToValue();
// Extract the shared secret to a separate dictionary.
std::optional<base::Value> secret_key =
pairing_json.Extract(PairingRegistry::kSharedSecretKey);
CHECK(secret_key.has_value());
base::Value::Dict secret_json;
secret_json.Set(PairingRegistry::kSharedSecretKey, std::move(*secret_key));
// presubmit: allow wstring
std::wstring value_name = base::UTF8ToWide(pairing.client_id());
// Write pairing to the registry.
if (!WriteValue(privileged_, value_name.c_str(), std::move(secret_json)) ||
!WriteValue(unprivileged_, value_name.c_str(), std::move(pairing_json))) {
return false;
}
return true;
}
bool PairingRegistryDelegateWin::Delete(const std::string& client_id) {
if (!privileged_.Valid()) {
LOG(ERROR) << "Cannot delete pairing entry '" << client_id
<< "': the delegate is read-only.";
return false;
}
// presubmit: allow wstring
std::wstring value_name = base::UTF8ToWide(client_id);
LONG result = privileged_.DeleteValue(value_name.c_str());
if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND &&
result != ERROR_PATH_NOT_FOUND) {
SetLastError(result);
PLOG(ERROR) << "Cannot delete pairing entry '" << client_id << "'";
return false;
}
result = unprivileged_.DeleteValue(value_name.c_str());
if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND &&
result != ERROR_PATH_NOT_FOUND) {
SetLastError(result);
PLOG(ERROR) << "Cannot delete pairing entry '" << client_id << "'";
return false;
}
return true;
}
std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
return std::make_unique<PairingRegistryDelegateWin>();
}
} // namespace remoting
|