-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.cpp
More file actions
497 lines (439 loc) · 11.2 KB
/
Key.cpp
File metadata and controls
497 lines (439 loc) · 11.2 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <map>
#include <cassert>
#include "Key.h"
#include "SyscallWrapper.h"
#include "Path.h"
#include "KeyNotFoundException.h"
#include "AccessDeniedException.h"
#include "ValueNotFoundException.h"
#include "IncompatibleValueTypeException.h"
using libreg::Key;
using libreg::MultiString;
using libreg::Access;
using libreg::Handle;
using libreg::Hive;
using libreg::ValueType;
using libreg::SyscallFailure;
using libreg::KeyNotFoundException;
using libreg::AccessDeniedException;
Key::Key(Handle<HKEY>&& handle, libreg::Hive hive, const MultiString& path, libreg::Access access)
: _handle(std::make_unique<Handle<HKEY>>(std::move(handle))), _hive(hive), _path(path), _access(access)
{
}
void Key::QueryInfo(const Handle<HKEY>& key,
size_t* max_subkey_chars,
size_t* max_value_name_chars,
size_t* max_data_bytes)
{
DWORD dwMaxSubkey = 0;
DWORD dwMaxValueNameLen = 0;
DWORD dwMaxValueLen = 0;
Syscall(RegQueryInfoKeyW,
key.Get(),
nullptr, // Class
nullptr, // Class size
nullptr, // Reserved
nullptr, // Subkeys
&dwMaxSubkey, // Max subkey name size
nullptr, // Max class size
nullptr, // Values count
&dwMaxValueNameLen, // Max value name size
&dwMaxValueLen, // Max value data size
nullptr, // Security descriptor
nullptr); // Last write time)
auto assign = [](size_t* ptr, DWORD value)
{
if (ptr != nullptr)
{
*ptr = static_cast<size_t>(value);
}
};
assign(max_subkey_chars, dwMaxSubkey);
assign(max_value_name_chars, dwMaxValueNameLen);
assign(max_data_bytes, dwMaxValueLen);
}
Key Key::Open(libreg::Hive hive, const MultiString& path, libreg::Access access)
{
HKEY key = nullptr;
try
{
Syscall(RegOpenKeyExW,
reinterpret_cast<HKEY>(hive),
path.Raw(),
0,
static_cast<DWORD>(access),
&key);
}
catch (const SyscallFailure& e)
{
HandleException(e, hive, path, false);
}
return Key(key, hive, path, access);
}
Key Key::Create(libreg::Hive hive, const MultiString& path, libreg::Access access, bool volatile_key)
{
HKEY key = nullptr;
try
{
Syscall(RegCreateKeyExW,
reinterpret_cast<HKEY>(hive),
path.Raw(),
0,
nullptr, //lpClass
volatile_key ? REG_OPTION_VOLATILE : 0,
static_cast<DWORD>(access), // regSamDesired
nullptr, // lpSecurityAttributes
&key,
nullptr);
}
catch (const SyscallFailure& e)
{
HandleException(e, hive, path, false);
}
return Key(key, hive, path, access);
}
Key Key::OpenOrCreate(libreg::Hive hive, const MultiString& path, libreg::Access access, bool volatile_key)
{
try
{
return Open(hive, path, access);
}
catch (const KeyNotFoundException&)
{
return Create(hive, path, access, volatile_key);
}
}
const Handle<HKEY>& Key::NativeHandle() const
{
return *_handle;
}
std::vector<std::pair<MultiString, ValueType>> Key::Values() const
{
size_t max_value_name = 0;
QueryInfo(*_handle, nullptr, &max_value_name, nullptr);
bool more_values = true;
auto check_return = [&more_values](LSTATUS syscal_return)
{
if (syscal_return == ERROR_NO_MORE_ITEMS)
{
more_values = false;
return true;
}
return syscal_return == ERROR_SUCCESS;
};
std::vector<std::pair<MultiString, ValueType>> values;
for (DWORD i = 0; more_values; i++)
{
std::wstring name(max_value_name, '\0');
DWORD size = static_cast<DWORD>(sizeof(wchar_t) * (name.size() + 1));
DWORD type;
SyscallWithExpectedReturn(RegEnumValueW,
check_return,
_handle->Get(),
i,
const_cast<wchar_t*>(name.data()),
&size,
nullptr,
&type,
nullptr,
nullptr
);
if (more_values)
{
name.resize(size);
values.emplace_back(name, static_cast<ValueType>(type));
}
}
return values;
}
std::vector<Key> Key::SubKeys(libreg::Access access) const
{
size_t max_subkey_size = 0;
QueryInfo(*_handle, &max_subkey_size, nullptr, nullptr);
bool more_keys = true;
auto check_return = [&more_keys](LSTATUS syscal_return)
{
if (syscal_return == ERROR_NO_MORE_ITEMS)
{
more_keys = false;
return true;
}
return syscal_return == ERROR_SUCCESS;
};
std::vector<Key> keys;
for(DWORD index = 0; more_keys; index++)
{
std::wstring name(max_subkey_size, '\0');
DWORD chars = static_cast<DWORD>(name.size() + 1);
SyscallWithExpectedReturn(RegEnumKeyExW,
check_return,
_handle->Get(), // Hkey
index, // dwIndw
const_cast<wchar_t*>(name.data()), // lpName
&chars, // lpcchName
nullptr, // lpReserved
nullptr, // lpClass
nullptr, // lpcchClass
nullptr); // lpftLastWriteTime
if (more_keys)
{
name.resize(chars);
keys.emplace_back(OpenSubKey(name, access));
}
}
return keys;
}
Key Key::OpenSubKey(const MultiString& name, libreg::Access access) const
{
HKEY subkey = nullptr;
try
{
Syscall(RegOpenKeyExW,
_handle->Get(),
name.Raw(),
0,
static_cast<DWORD>(access),
&subkey
);
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, Path::Concat(_path, name), false);
}
return Key(subkey, _hive, Path::Concat(_path, name), access);
}
std::optional<Key> Key::TryOpenSubKey(const MultiString& name, libreg::Access access) const
{
auto pred = [](auto result)
{
return result == 0 || result == ERROR_FILE_NOT_FOUND;
};
HKEY subkey = nullptr;
try
{
if (SyscallWithExpectedReturn(RegOpenKeyExW,
pred,
_handle->Get(),
name.Raw(),
0,
static_cast<DWORD>(access),
&subkey
) == ERROR_FILE_NOT_FOUND)
{
return {}; // Subkey does not exist
}
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, Path::Concat(_path, name), false);
}
return Key(subkey, _hive, Path::Concat(_path, name), access);
}
Key Key::CreateSubKey(const MultiString& name, bool volatile_key)
{
HKEY subkey = nullptr;
try
{
Syscall(RegCreateKeyExW,
_handle->Get(), //hKey
name.Raw(), // lpSubkey
0, // dwReserved
nullptr, //lpClass
volatile_key ? REG_OPTION_VOLATILE : 0, //dwOptions
KEY_ALL_ACCESS, //samDesired
nullptr, //lpSecurityAttributes
&subkey, //phKey
nullptr); // lpCreationDisposition
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, _path, false);
}
return Key(Handle<HKEY>(subkey), _hive, Path::Concat(_path, name), Access::AllAccess);
}
Key Key::OpenOrCreateSubkey(const MultiString& name, libreg::Access access, bool volatile_key)
{
return OpenOrCreate(_hive, Path::Concat(_path, name), access, volatile_key);
}
const MultiString& Key::Path() const
{
return _path;
}
MultiString libreg::Key::Name() const
{
// Search for last '\' in string
auto end = _path.Value().find_last_of('\\');
if (end == std::string::npos)
{
return Path(); // If not found, then the key is a root key, return full path
}
return _path.Value().substr(end + 1);
}
Hive Key::Hive() const
{
return _hive;
}
void Key::SetValueImpl(const MultiString& name, const void* data, size_t size, ValueType type)
{
try
{
Syscall(RegSetValueExW,
_handle->Get(), // hKey
name.Raw(), // lpValueName
0, // reserved
static_cast<DWORD>(type), // dwType,
reinterpret_cast<const BYTE*>(data), //lpData,
static_cast<DWORD>(size)); //cbData
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, _path, true);
}
}
void Key::GetValueImpl(const MultiString& name, void* data, size_t& size, ValueType expected_type) const
{
static const std::map<ValueType, DWORD> masks
{
{ValueType::None, RRF_RT_ANY},
{ValueType::Dword, RRF_RT_REG_DWORD},
{ValueType::Qword, RRF_RT_REG_QWORD},
{ValueType::Binary, RRF_RT_REG_BINARY},
{ValueType::Expand_sz, RRF_RT_REG_EXPAND_SZ},
{ValueType::Sz, RRF_RT_REG_SZ},
{ValueType::Multi_sz, RRF_RT_REG_MULTI_SZ},
};
auto mask = masks.find(expected_type);
assert(mask != masks.end());
DWORD dwSize = static_cast<DWORD>(size);
DWORD dwType = 0;
try
{
Syscall(RegGetValueW,
_handle->Get(), // hKey
nullptr, // lpSubkey
name.Raw(), //lpValue
mask->second | RRF_NOEXPAND, // dwFlags
&dwType, // pdwType
data, // pvData
&dwSize); // pcdData
}
catch (const SyscallFailure& e)
{
if (e.ReturnValue() == ERROR_UNSUPPORTED_TYPE)
{
ValueType actualType = static_cast<ValueType>(dwType);
throw IncompatibleValueTypeException(_hive, Path::Concat(_path, name), expected_type, actualType, e);
}
HandleException(e, _hive, Path::Concat(_path, name), true);
}
size = static_cast<size_t>(dwSize);
}
size_t Key::GetValueSize(const MultiString& name) const
{
// Edge case: If an Sz value has been stored without \0, RegGetValue will inject a \0 so
// its size will be max_value_size += max_value_size
// Given that it's the same for multi_sz, we can't guess the maximum buffer (same for expand_sz if expansion is enabled)
// Calling RegGetValueW is simplier
auto routine = [](auto ret)
{
return ret == ERROR_MORE_DATA || ret == ERROR_SUCCESS;
};
DWORD dwSize = 0;
try
{
SyscallWithExpectedReturn(
RegGetValueW,
routine,
_handle->Get(), // hKey
nullptr, // lpSubkey
name.Raw(), //lpValue
RRF_RT_ANY | RRF_NOEXPAND, // dwFlags
nullptr, // pdwType
nullptr, // pvData
&dwSize); // pcdData
}
catch (const SyscallFailure & e)
{
HandleException(e, _hive, Path::Concat(_path, name), true);
}
return static_cast<size_t>(dwSize);
}
void Key::SetValue(const MultiString& name, DWORD value, ValueType type)
{
SetValueImpl(name, &value, sizeof(DWORD), type);
}
void Key::SetValue(const MultiString& name, const std::vector<std::uint8_t>& value, ValueType type)
{
SetValueImpl(name, value.data(), value.size(), type);
}
void Key::SetValue(const MultiString& name, const MultiString& value, ValueType type)
{
SetValueImpl(name, value.Raw(), sizeof(wchar_t) * (value.Value().size() + 1), type);
}
void Key::DeleteSubKey(const MultiString& name)
{
try
{
Syscall(RegDeleteTreeW,
_handle->Get(),
name.Raw());
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, _path, false);
}
}
void Key::DeleteValue(const MultiString& name)
{
try
{
Syscall(RegDeleteValueW,
_handle->Get(), // hKey
name.Raw() // lpSubKey
);
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, _path, true);
}
}
void Key::Delete()
{
try
{
Syscall(RegDeleteTreeW,
reinterpret_cast<HKEY>(_hive), // hKey
_path.Raw() // lpSubKey
);
}
catch (const SyscallFailure& e)
{
HandleException(e, _hive, _path, false);
}
}
Access Key::Access() const
{
return _access;
}
void Key::HandleException(const SyscallFailure& ex, libreg::Hive hive, const MultiString& path, bool value)
{
if (ex.ReturnValue() == ERROR_FILE_NOT_FOUND)
{
if (value)
{
throw ValueNotFoundException(hive, path, ex);
}
else
{
throw KeyNotFoundException(hive, path, ex);
}
}
else if (ex.ReturnValue() == ERROR_ACCESS_DENIED)
{
throw AccessDeniedException(hive, path, ex);
}
else
{
throw ex;
}
}