-
Notifications
You must be signed in to change notification settings - Fork 1
/
WinRegistry.cpp
1393 lines (1224 loc) · 39.2 KB
/
WinRegistry.cpp
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the LICENSE file distributed with
* this work for additional information regarding copyright ownership.
*
* Also, if exists, check the Licenses directory for information about
* third-party modules.
*
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "WinRegistry.h"
#include "Process.h"
#include <dpapi.h>
#pragma comment(lib, "crypt32.lib")
#define STATUS_PREDEFINED_HANDLE 0x40000016
#define STATUS_OBJECT_PATH_NOT_FOUND 0xC000003A
#define STATUS_NO_MORE_ENTRIES 0x8000001A
//-----------------------------------------------------------
static const MX_UNICODE_STRING usEmpty = { 0, 0, (PWSTR)L"" };
//-----------------------------------------------------------
static NTSTATUS OpenBaseKey(_In_opt_ HKEY hKey, _In_ DWORD dwAccess, _Out_ PHANDLE lphBaseKey);
static HRESULT RecursiveDeleteKey(_In_ HANDLE hKey, _In_opt_ PUNICODE_STRING SubKey);
static NTSTATUS GetSubKeyName(_In_ HANDLE hKey, _In_ ULONG Index, _Out_ PUNICODE_STRING *pKeyName);
static NTSTATUS GetValueName(_In_ HANDLE hKey, _In_ ULONG Index, _Out_ PUNICODE_STRING *pValueName);
//-----------------------------------------------------------
namespace MX {
CWindowsRegistry::CWindowsRegistry() : CBaseMemObj(), CNonCopyableObj()
{
hKey = NULL;
return;
}
CWindowsRegistry::~CWindowsRegistry()
{
Close();
return;
}
HRESULT CWindowsRegistry::Create(_In_ HKEY hParentKey, _In_z_ LPCWSTR szSubKeyW)
{
Close();
if (szSubKeyW == NULL)
szSubKeyW = L"";
//if we are opening a root key, then use NtXXX method to return a real handle to be able to call other
//functions using NtXXX apis
if (*szSubKeyW == 0 && (hParentKey == HKEY_LOCAL_MACHINE || hParentKey == HKEY_USERS))
{
NTSTATUS nNtStatus;
nNtStatus = OpenBaseKey(hParentKey, KEY_ALL_ACCESS, (PHANDLE)&hKey);
if (!NT_SUCCESS(nNtStatus))
return MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
}
else
{
DWORD dwOsErr;
dwOsErr = (DWORD)::RegCreateKeyExW(hParentKey, szSubKeyW, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::Create(_In_ HKEY hParentKey, _In_ PUNICODE_STRING SubKey)
{
MX_OBJECT_ATTRIBUTES sObjAttr;
NTSTATUS nNtStatus;
Close();
if (SubKey != NULL && SubKey->Buffer == NULL && SubKey->Length > 0)
return E_POINTER;
//prepare
::MxMemSet(&sObjAttr, 0, sizeof(sObjAttr));
sObjAttr.Length = (ULONG)sizeof(sObjAttr);
sObjAttr.Attributes = OBJ_CASE_INSENSITIVE;
//open base key if needed
nNtStatus = OpenBaseKey(hParentKey, KEY_ALL_ACCESS, &(sObjAttr.RootDirectory));
if (NT_SUCCESS(nNtStatus))
{
ULONG nDisposition;
//create key
sObjAttr.ObjectName = (SubKey != NULL) ? (PMX_UNICODE_STRING)SubKey : (PMX_UNICODE_STRING)&usEmpty;
nNtStatus = ::MxNtCreateKey((PHANDLE)&hKey, KEY_ALL_ACCESS, &sObjAttr, 0, NULL, REG_OPTION_NON_VOLATILE,
&nDisposition);
//cleanup
if (sObjAttr.RootDirectory != NULL && sObjAttr.RootDirectory != (HANDLE)hParentKey)
::MxNtClose(sObjAttr.RootDirectory);
}
//done
if (nNtStatus == STATUS_PREDEFINED_HANDLE)
{
if (hKey != NULL)
::MxNtClose(hKey);
hKey = NULL;
return HRESULT_FROM_WIN32(ERROR_PREDEFINED_HANDLE);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
return S_OK;
}
HRESULT CWindowsRegistry::Open(_In_ HKEY hParentKey, _In_opt_z_ LPCWSTR szSubKeyW, _In_opt_ BOOL bWriteAccess)
{
Close();
if (szSubKeyW == NULL)
szSubKeyW = L"";
//if we are opening a root key, then use NtXXX method to return a real handle to be able to call other
//functions using NtXXX apis
if (*szSubKeyW == 0 && (hParentKey == HKEY_LOCAL_MACHINE || hParentKey == HKEY_USERS))
{
NTSTATUS nNtStatus;
nNtStatus = OpenBaseKey(hParentKey, (bWriteAccess != FALSE) ? KEY_ALL_ACCESS : KEY_READ, (PHANDLE)&hKey);
if (!NT_SUCCESS(nNtStatus))
return MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
}
else
{
DWORD dwOsErr;
dwOsErr = (DWORD)::RegOpenKeyExW(hParentKey, szSubKeyW, 0, (bWriteAccess != FALSE) ? KEY_ALL_ACCESS : KEY_READ,
&hKey);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::Open(_In_ HKEY hParentKey, _In_ PUNICODE_STRING SubKey, _In_opt_ BOOL bWriteAccess)
{
MX_OBJECT_ATTRIBUTES sObjAttr;
NTSTATUS nNtStatus;
Close();
if (SubKey != NULL && SubKey->Buffer == NULL && SubKey->Length > 0)
return E_POINTER;
//prepare
::MxMemSet(&sObjAttr, 0, sizeof(sObjAttr));
sObjAttr.Length = (ULONG)sizeof(sObjAttr);
sObjAttr.Attributes = OBJ_CASE_INSENSITIVE;
//open base key if needed
nNtStatus = OpenBaseKey(hParentKey, (bWriteAccess != FALSE) ? KEY_ALL_ACCESS : KEY_READ, &(sObjAttr.RootDirectory));
if (NT_SUCCESS(nNtStatus))
{
//open key
sObjAttr.ObjectName = (SubKey != NULL) ? (PMX_UNICODE_STRING)SubKey : (PMX_UNICODE_STRING)&usEmpty;
nNtStatus = ::MxNtOpenKey((PHANDLE)&hKey, (bWriteAccess != FALSE) ? KEY_ALL_ACCESS : KEY_READ, &sObjAttr);
//cleanup
if (sObjAttr.RootDirectory != NULL && sObjAttr.RootDirectory != (HANDLE)hParentKey)
::MxNtClose(sObjAttr.RootDirectory);
}
//done
if (nNtStatus == STATUS_PREDEFINED_HANDLE)
{
if (hKey != NULL)
::MxNtClose(hKey);
hKey = NULL;
return HRESULT_FROM_WIN32(ERROR_PREDEFINED_HANDLE);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
return S_OK;
}
VOID CWindowsRegistry::Close()
{
if (hKey != NULL)
{
::RegCloseKey(hKey);
hKey = NULL;
}
return;
}
HRESULT CWindowsRegistry::ReadDWord(_In_z_ LPCWSTR szNameW, _Out_ DWORD &dwValue)
{
DWORD dwType, dwDataSize, dwOsErr;
dwValue = 0;
if (hKey == NULL)
return MX_E_NotReady;
dwDataSize = (DWORD)sizeof(DWORD);
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, (LPBYTE)&dwValue, &dwDataSize);
if (dwOsErr != 0)
{
dwValue = 0;
return (dwOsErr == ERROR_MORE_DATA) ? MX_E_InvalidData : MX_HRESULT_FROM_WIN32(dwOsErr);
}
if (dwType != REG_DWORD && dwType != REG_DWORD_BIG_ENDIAN)
{
dwValue = 0;
return MX_E_InvalidData;
}
if (dwType == REG_DWORD_BIG_ENDIAN)
{
dwValue = ((dwValue & 0xFF000000) >> 24) | ((dwValue & 0x00FF0000) >> 8) |
((dwValue & 0x0000FF00) << 8) | ((dwValue & 0x000000FF) << 24);
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadDWord(_In_ PUNICODE_STRING Name, _Out_ DWORD &dwValue)
{
struct {
MX_KEY_VALUE_PARTIAL_INFORMATION Info;
BYTE aData[256];
} s;
ULONG RetLength;
NTSTATUS nNtStatus;
dwValue = 0;
if (hKey == NULL)
return MX_E_NotReady;
if (Name == NULL)
Name = (PUNICODE_STRING)&usEmpty;
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
&s, (ULONG)sizeof(s), &RetLength);
if (!NT_SUCCESS(nNtStatus))
{
dwValue = 0;
return (nNtStatus == STATUS_INFO_LENGTH_MISMATCH || nNtStatus == STATUS_BUFFER_OVERFLOW ||
nNtStatus == STATUS_BUFFER_TOO_SMALL)
? MX_E_InvalidData : HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
}
if (s.Info.Type != REG_DWORD && s.Info.Type != REG_DWORD_BIG_ENDIAN)
{
dwValue = 0;
return MX_E_InvalidData;
}
dwValue = *((DWORD MX_UNALIGNED*)(s.Info.Data));
if (s.Info.Type == REG_DWORD_BIG_ENDIAN)
{
dwValue = ((dwValue & 0xFF000000) >> 24) | ((dwValue & 0x00FF0000) >> 8) |
((dwValue & 0x0000FF00) << 8) | ((dwValue & 0x000000FF) << 24);
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadString(_In_z_ LPCWSTR szNameW, _Out_ CStringW &cStrValueW,
_In_opt_ BOOL bAutoExpandRegSz)
{
DWORD dwType, dwDataSize, dwOsErr;
cStrValueW.Empty();
if (hKey == NULL)
return MX_E_NotReady;
//get string size and type
dwDataSize = 0;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, NULL, &dwDataSize);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
return MX_E_InvalidData;
//prepare buffer
if (cStrValueW.EnsureBuffer(((SIZE_T)dwDataSize/sizeof(WCHAR)) + 1) == FALSE)
return E_OUTOFMEMORY;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, (LPBYTE)((LPWSTR)cStrValueW), &dwDataSize);
if (dwOsErr != 0)
{
cStrValueW.Empty();
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
{
cStrValueW.Empty();
return MX_E_InvalidData;
}
((LPWSTR)cStrValueW)[(SIZE_T)dwDataSize / sizeof(WCHAR)] = 0;
cStrValueW.Refresh();
//auto-expand REG_EXPAND_SZ?
if (dwType == REG_EXPAND_SZ && bAutoExpandRegSz != FALSE && cStrValueW.IsEmpty() == FALSE)
{
HRESULT hRes;
hRes = Process::_ExpandEnvironmentStrings(cStrValueW);
if (FAILED(hRes) && hRes != MX_E_BufferOverflow)
{
cStrValueW.Empty();
return hRes;
}
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadString(_In_ PUNICODE_STRING Name, _Out_ PUNICODE_STRING *pValue,
_In_opt_ BOOL bAutoExpandRegSz)
{
TAutoFreePtr<MX_KEY_VALUE_PARTIAL_INFORMATION> aBuffer;
struct {
MX_KEY_VALUE_PARTIAL_INFORMATION Info;
BYTE aData[256];
} s;
PMX_KEY_VALUE_PARTIAL_INFORMATION lpInfo;
ULONG RetLength;
NTSTATUS nNtStatus;
if (pValue == NULL)
return E_POINTER;
*pValue = NULL;
if (hKey == NULL)
return MX_E_NotReady;
if (Name == NULL)
Name = (PUNICODE_STRING)&usEmpty;
//query
lpInfo = &(s.Info);
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, (ULONG)sizeof(s), &RetLength);
if (nNtStatus == STATUS_BUFFER_OVERFLOW || nNtStatus == STATUS_BUFFER_TOO_SMALL ||
nNtStatus == STATUS_INFO_LENGTH_MISMATCH)
{
//check type
if (lpInfo->Type != REG_SZ && lpInfo->Type != REG_EXPAND_SZ)
return MX_E_InvalidData;
//realloc and retry
if (RetLength < 4096)
RetLength = 4096;
aBuffer.Attach((PMX_KEY_VALUE_PARTIAL_INFORMATION)MX_MALLOC((SIZE_T)RetLength));
if (!aBuffer)
return E_OUTOFMEMORY;
lpInfo = aBuffer.Get();
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, RetLength, &RetLength);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
//check type
if (lpInfo->Type != REG_SZ && lpInfo->Type != REG_EXPAND_SZ)
return MX_E_InvalidData;
//sanitize length (just in case)
lpInfo->DataLength &= ~1;
//crop nul chars at the end
while (lpInfo->DataLength > 0 && ((LPWSTR)(lpInfo->Data))[lpInfo->DataLength / 2 - 1] == 0)
lpInfo->DataLength -= 2;
//auto-expand REG_EXPAND_SZ?
if (lpInfo->Type == REG_EXPAND_SZ && bAutoExpandRegSz != FALSE)
{
TAutoFreePtr<UNICODE_STRING> aTempStr;
CStringW cStrTempW;
LPWSTR sW = (LPWSTR)(lpInfo->Data);
ULONG Idx, StartIdx;
USHORT Cap;
HRESULT hRes;
aTempStr.Attach((PUNICODE_STRING)MX_MALLOC(sizeof(UNICODE_STRING) + 65536));
if (!aTempStr)
return E_OUTOFMEMORY;
aTempStr->Buffer = (PWSTR)(aTempStr.Get() + 1);
aTempStr->Length = 0;
aTempStr->MaximumLength = 65534;
//Size = ((lpInfo->DataLength > 65534) ? 65534 : lpInfo->DataLength) & (~1);
Idx = 0;
while (Idx < lpInfo->DataLength && aTempStr->Length < aTempStr->MaximumLength)
{
//process non-nul characters
StartIdx = Idx;
while (Idx < lpInfo->DataLength && sW[Idx >> 1] != 0)
Idx += 2;
if (Idx > StartIdx)
{
if (cStrTempW.CopyN(sW + StartIdx / 2, (SIZE_T)(Idx - StartIdx) / 2) == FALSE)
return E_OUTOFMEMORY;
hRes = Process::_ExpandEnvironmentStrings(cStrTempW);
if (FAILED(hRes) && hRes != MX_E_BufferOverflow)
return hRes;
Cap = aTempStr->MaximumLength - aTempStr->Length;
if ((ULONG)Cap > cStrTempW.GetLength() * 2)
Cap = (USHORT)(cStrTempW.GetLength() * 2);
::MxMemCopy(aTempStr->Buffer + (SIZE_T)(aTempStr->Length) / 2, (LPCWSTR)cStrTempW, (SIZE_T)Cap);
aTempStr->Length += Cap;
}
//process nul characters
StartIdx = Idx;
while (Idx < lpInfo->DataLength && sW[Idx >> 1] == 0)
Idx += 2;
if (Idx > StartIdx)
{
Cap = aTempStr->MaximumLength - aTempStr->Length;
if ((ULONG)Cap > Idx - StartIdx)
Cap = (USHORT)(Idx - StartIdx);
::MxMemSet(aTempStr->Buffer + (SIZE_T)(aTempStr->Length) / 2, 0, (SIZE_T)Cap);
aTempStr->Length += Cap;
}
}
//copy only required bytes
*pValue = (PUNICODE_STRING)MX_MALLOC(sizeof(UNICODE_STRING) + (SIZE_T)(aTempStr->Length) + 2);
if ((*pValue) == NULL)
return E_OUTOFMEMORY;
//copy key name
(*pValue)->Buffer = (PWSTR)((*pValue) + 1);
(*pValue)->Length = (*pValue)->MaximumLength = aTempStr->Length;
::MxMemCopy((*pValue)->Buffer, aTempStr->Buffer, (SIZE_T)(aTempStr->Length));
(*pValue)->Buffer[aTempStr->Length / 2] = 0;
}
else
{
ULONG Size = ((lpInfo->DataLength > 65534) ? 65534 : lpInfo->DataLength) & (~1);
*pValue = (PUNICODE_STRING)MX_MALLOC(sizeof(UNICODE_STRING) + (SIZE_T)Size + 2);
if ((*pValue) == NULL)
return E_OUTOFMEMORY;
//copy key name
(*pValue)->Buffer = (PWSTR)((*pValue) + 1);
(*pValue)->Length = (*pValue)->MaximumLength = (USHORT)Size;
::MxMemCopy((*pValue)->Buffer, lpInfo->Data, Size);
(*pValue)->Buffer[Size / 2] = 0;
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadPassword(_In_z_ LPCWSTR szNameW, _Out_ CStringW &cStrPasswordW)
{
TAutoFreePtr<BYTE> cBlob;
SIZE_T nBlobSize;
DATA_BLOB sInput, sOutput;
HRESULT hRes;
cStrPasswordW.Empty();
hRes = ReadBlob(szNameW, cBlob, nBlobSize);
if (SUCCEEDED(hRes))
{
if (nBlobSize > 0)
{
::MxMemSet(&sInput, 0, sizeof(sInput));
::MxMemSet(&sOutput, 0, sizeof(sOutput));
sInput.pbData = cBlob.Get();
sInput.cbData = (DWORD)nBlobSize;
if (::CryptUnprotectData(&sInput, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &sOutput) != FALSE)
{
if ((sOutput.cbData & 1) == 0)
{
if (cStrPasswordW.CopyN((LPCWSTR)(sOutput.pbData), (SIZE_T)(sOutput.cbData) / 2) == FALSE)
hRes = E_OUTOFMEMORY;
}
else
{
hRes = MX_E_InvalidData;
}
::MxMemSet(sOutput.pbData, '*', (SIZE_T)(sOutput.cbData));
::LocalFree((HLOCAL)(sOutput.pbData));
}
else
{
hRes = MX_HRESULT_FROM_LASTERROR();
}
}
}
else if (hRes == MX_E_InvalidData)
{
hRes = ReadString(szNameW, cStrPasswordW);
}
//done
return hRes;
}
HRESULT CWindowsRegistry::ReadMultiString(_In_z_ LPCWSTR szNameW, _Out_ TArrayListWithFree<LPWSTR> &aStrValuesList)
{
TAutoFreePtr<WCHAR> cBuf;
LPWSTR sW, szStartW;
CStringW cStrTempW;
DWORD dw, dwType, dwDataSize, dwOsErr;
aStrValuesList.RemoveAllElements();
if (hKey == NULL)
return MX_E_NotReady;
//get string size and type
dwDataSize = 0;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, NULL, &dwDataSize);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
if (dwType != REG_MULTI_SZ)
return MX_E_InvalidData;
//prepare buffer
cBuf.Attach((LPWSTR)MX_MALLOC((SIZE_T)dwDataSize));
if (!cBuf)
return E_OUTOFMEMORY;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, (LPBYTE)cBuf.Get(), &dwDataSize);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
if (dwType != REG_MULTI_SZ)
return MX_E_InvalidData;
dwDataSize /= (DWORD)sizeof(WCHAR);
//extract strings
sW = cBuf.Get();
dw = 0;
while (dw < dwDataSize)
{
while (dw < dwDataSize && sW[dw] == 0)
dw++;
if (dw < dwDataSize)
{
szStartW = &sW[dw];
while (dw < dwDataSize && sW[dw] != 0)
dw++;
if (cStrTempW.CopyN(szStartW, (SIZE_T)(&sW[dw] - szStartW)) == FALSE)
{
aStrValuesList.RemoveAllElements();
return E_OUTOFMEMORY;
}
if (aStrValuesList.AddElement((LPWSTR)cStrTempW) == FALSE)
{
aStrValuesList.RemoveAllElements();
return E_OUTOFMEMORY;
}
cStrTempW.Detach();
}
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadMultiString(_In_ PUNICODE_STRING Name,
_Out_ TArrayListWithFree<PUNICODE_STRING> &aStrValuesList)
{
TAutoFreePtr<MX_KEY_VALUE_PARTIAL_INFORMATION> aBuffer;
struct {
MX_KEY_VALUE_PARTIAL_INFORMATION Info;
BYTE aData[256];
} s;
PMX_KEY_VALUE_PARTIAL_INFORMATION lpInfo;
ULONG RetLength;
DWORD dw, dwDataSize;
SIZE_T nSize;
LPCWSTR sW, szStartW;
PUNICODE_STRING TempStr;
NTSTATUS nNtStatus;
aStrValuesList.RemoveAllElements();
if (hKey == NULL)
return MX_E_NotReady;
if (Name == NULL)
Name = (PUNICODE_STRING)&usEmpty;
//query
lpInfo = &(s.Info);
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, (ULONG)sizeof(s), &RetLength);
if (nNtStatus == STATUS_BUFFER_OVERFLOW || nNtStatus == STATUS_BUFFER_TOO_SMALL)
{
//check type
if (lpInfo->Type != REG_MULTI_SZ)
return MX_E_InvalidData;
//realloc and retry
if (RetLength < 4096)
RetLength = 4096;
aBuffer.Attach((PMX_KEY_VALUE_PARTIAL_INFORMATION)MX_MALLOC((SIZE_T)RetLength));
if (!aBuffer)
return E_OUTOFMEMORY;
lpInfo = aBuffer.Get();
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, RetLength, &RetLength);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
//check type
if (lpInfo->Type != REG_MULTI_SZ)
return MX_E_InvalidData;
//fill list
dwDataSize = lpInfo->DataLength / (DWORD)sizeof(WCHAR);
//extract strings
sW = (LPCWSTR)(lpInfo->Data);
dw = 0;
while (dw < dwDataSize)
{
while (dw < dwDataSize && sW[dw] == 0)
dw++;
if (dw < dwDataSize)
{
szStartW = &sW[dw];
while (dw < dwDataSize && sW[dw] != 0)
dw++;
nSize = (SIZE_T)(&sW[dw] - szStartW);
if (nSize > 32767)
nSize = 32767;
//copy only required bytes
TempStr = (PUNICODE_STRING)MX_MALLOC(sizeof(UNICODE_STRING) + nSize * 2 + 2);
if (TempStr == NULL)
{
aStrValuesList.RemoveAllElements();
return E_OUTOFMEMORY;
}
//copy value
TempStr->Buffer = (PWSTR)(TempStr + 1);
TempStr->Length = TempStr->MaximumLength = (USHORT)nSize * 2;
::MxMemCopy(TempStr->Buffer, szStartW, nSize * 2);
TempStr->Buffer[TempStr->Length / 2] = 0;
//add to list
if (aStrValuesList.AddElement(TempStr) == FALSE)
{
MX_FREE(TempStr);
aStrValuesList.RemoveAllElements();
return E_OUTOFMEMORY;
}
}
}
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadBlob(_In_z_ LPCWSTR szNameW, _Out_ TAutoFreePtr<BYTE> &cBlob, _Out_ SIZE_T &nBlobSize)
{
DWORD dwOsErr, dwDataSize, dwType;
cBlob.Reset();
nBlobSize = 0;
if (hKey == NULL)
return MX_E_NotReady;
//get blob size and type
dwDataSize = 0;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, NULL, &dwDataSize);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
if (dwType != REG_BINARY)
return MX_E_InvalidData;
//prepare buffer
if (dwDataSize > 0)
{
cBlob.Attach((LPBYTE)MX_MALLOC((SIZE_T)dwDataSize));
if (!cBlob)
return E_OUTOFMEMORY;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, cBlob.Get(), &dwDataSize);
if (dwOsErr != 0)
{
cBlob.Reset();
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
}
if (dwType != REG_BINARY)
{
cBlob.Reset();
return MX_E_InvalidData;
}
nBlobSize = (SIZE_T)dwDataSize;
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadBlob(_In_ PUNICODE_STRING Name, _Out_ TAutoFreePtr<BYTE> &cBlob, _Out_ SIZE_T &nBlobSize)
{
TAutoFreePtr<MX_KEY_VALUE_PARTIAL_INFORMATION> aBuffer;
struct {
MX_KEY_VALUE_PARTIAL_INFORMATION Info;
BYTE aData[256];
} s;
PMX_KEY_VALUE_PARTIAL_INFORMATION lpInfo;
ULONG RetLength;
NTSTATUS nNtStatus;
cBlob.Reset();
nBlobSize = 0;
if (hKey == NULL)
return MX_E_NotReady;
if (Name == NULL)
Name = (PUNICODE_STRING)&usEmpty;
//query
lpInfo = &(s.Info);
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, (ULONG)sizeof(s), &RetLength);
if (nNtStatus == STATUS_BUFFER_OVERFLOW || nNtStatus == STATUS_BUFFER_TOO_SMALL)
{
//check type
if (lpInfo->Type != REG_BINARY)
return MX_E_InvalidData;
//realloc and retry
if (RetLength < 4096)
RetLength = 4096;
aBuffer.Attach((PMX_KEY_VALUE_PARTIAL_INFORMATION)MX_MALLOC((SIZE_T)RetLength));
if (!aBuffer)
return E_OUTOFMEMORY;
lpInfo = aBuffer.Get();
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, RetLength, &RetLength);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
//check type
if (lpInfo->Type != REG_BINARY)
return MX_E_InvalidData;
//prepare buffer
if (lpInfo->DataLength > 0)
{
cBlob.Attach((LPBYTE)MX_MALLOC((SIZE_T)(lpInfo->DataLength)));
if (!cBlob)
return E_OUTOFMEMORY;
::MxMemCopy(cBlob.Get(), lpInfo->Data, (SIZE_T)(lpInfo->DataLength));
}
nBlobSize = (SIZE_T)(lpInfo->DataLength);
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadAny(_In_z_ LPCWSTR szNameW, _Out_ DWORD &dwType, _Out_ TAutoFreePtr<BYTE> &cData,
_Out_ SIZE_T &nDataSize)
{
DWORD dwOsErr, dwDataSize;
dwType = REG_NONE;
cData.Reset();
nDataSize = 0;
if (hKey == NULL)
return MX_E_NotReady;
//get data size and type
dwDataSize = 0;
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, NULL, &dwDataSize);
if (dwOsErr != 0)
{
dwType = REG_NONE;
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
//prepare buffer
if (dwDataSize > 0)
{
cData.Attach((LPBYTE)MX_MALLOC((SIZE_T)dwDataSize));
if (!cData)
{
dwType = REG_NONE;
return E_OUTOFMEMORY;
}
dwOsErr = (DWORD)::RegQueryValueExW(hKey, szNameW, NULL, &dwType, cData.Get(), &dwDataSize);
if (dwOsErr != 0)
{
dwType = REG_NONE;
cData.Reset();
return MX_HRESULT_FROM_WIN32(dwOsErr);
}
}
nDataSize = (SIZE_T)dwDataSize;
//done
return S_OK;
}
HRESULT CWindowsRegistry::ReadAny(_In_ PUNICODE_STRING Name, _Out_ DWORD &dwType, _Out_ TAutoFreePtr<BYTE> &cData,
_Out_ SIZE_T &nDataSize)
{
TAutoFreePtr<MX_KEY_VALUE_PARTIAL_INFORMATION> aBuffer;
struct {
MX_KEY_VALUE_PARTIAL_INFORMATION Info;
BYTE aData[256];
} s;
PMX_KEY_VALUE_PARTIAL_INFORMATION lpInfo;
ULONG RetLength;
NTSTATUS nNtStatus;
dwType = REG_NONE;
cData.Reset();
nDataSize = 0;
if (hKey == NULL)
return MX_E_NotReady;
if (Name == NULL)
Name = (PUNICODE_STRING)&usEmpty;
//query
lpInfo = &(s.Info);
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, (ULONG)sizeof(s), &RetLength);
if (nNtStatus == STATUS_BUFFER_OVERFLOW || nNtStatus == STATUS_BUFFER_TOO_SMALL)
{
//realloc and retry
if (RetLength < 4096)
RetLength = 4096;
aBuffer.Attach((PMX_KEY_VALUE_PARTIAL_INFORMATION)MX_MALLOC((SIZE_T)RetLength));
if (!aBuffer)
return E_OUTOFMEMORY;
lpInfo = aBuffer.Get();
nNtStatus = ::MxNtQueryValueKey((HANDLE)hKey, (PMX_UNICODE_STRING)Name, MxKeyValuePartialInformation,
lpInfo, RetLength, &RetLength);
}
if (!NT_SUCCESS(nNtStatus))
return HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
//prepare buffer
if (lpInfo->DataLength > 0)
{
cData.Attach((LPBYTE)MX_MALLOC((SIZE_T)(lpInfo->DataLength)));
if (!cData)
return E_OUTOFMEMORY;
::MxMemCopy(cData.Get(), lpInfo->Data, (SIZE_T)(lpInfo->DataLength));
}
nDataSize = (SIZE_T)(lpInfo->DataLength);
//done
return S_OK;
}
HRESULT CWindowsRegistry::WriteDWord(_In_z_ LPCWSTR szNameW, _In_ DWORD dwValue)
{
DWORD dwOsErr;
if (hKey == NULL)
return MX_E_NotReady;
dwOsErr = ::RegSetValueExW(hKey, szNameW, 0, REG_DWORD, (const LPBYTE)&dwValue, (DWORD)sizeof(DWORD));
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
//done
return S_OK;
}
HRESULT CWindowsRegistry::WriteString(_In_z_ LPCWSTR szNameW, _In_z_ LPCWSTR szValueW)
{
DWORD dwOsErr;
SIZE_T nLen;
if (hKey == NULL)
return MX_E_NotReady;
if (szValueW == NULL)
szValueW = L"";
nLen = (StrLenW(szValueW) + 1); //include NUL char
if (nLen > 0x7FFFFFFF)
return E_FAIL;
dwOsErr = ::RegSetValueExW(hKey, szNameW, 0, REG_SZ, (const LPBYTE)szValueW, (DWORD)(nLen*sizeof(WCHAR)));
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
//done
return S_OK;
}
HRESULT CWindowsRegistry::WriteMultiString(_In_z_ LPCWSTR szNameW, _In_ SIZE_T nValuesCount, _In_ LPCWSTR *lpszValuesW)
{
TAutoFreePtr<BYTE> cBuf;
DWORD dwOsErr;
SIZE_T i, nLen, nThisLen;
if (hKey == NULL)
return MX_E_NotReady;
if (nValuesCount > 0 && lpszValuesW == NULL)
return E_POINTER;
for (i=nLen=0; i<nValuesCount; i++)
{
nLen += StrLenW(lpszValuesW[i]) + 1;
if (nLen > 1048756 / 2)
return MX_E_BadLength;
}
if (nLen + 1 > 1048756 / 2)
return MX_E_BadLength;
//allocate buffer
cBuf.Attach((LPBYTE)MX_MALLOC((nLen+1)*sizeof(WCHAR)));
if (!cBuf)
return E_OUTOFMEMORY;
for (i=nLen=0; i<nValuesCount; i++)
{
nThisLen = (StrLenW(lpszValuesW[i]) + 1) * sizeof(WCHAR);
::MxMemCopy(cBuf.Get()+nLen, lpszValuesW[i], nThisLen);
nLen += nThisLen;
}
::MxMemSet(cBuf.Get()+nLen, 0, 2);
nLen += 2;
//save value
dwOsErr = ::RegSetValueExW(hKey, szNameW, 0, REG_MULTI_SZ, (const LPBYTE)cBuf.Get(), (DWORD)nLen);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
//done
return S_OK;
}
HRESULT CWindowsRegistry::WriteBlob(_In_z_ LPCWSTR szNameW, _In_opt_ LPCVOID lpValue, _In_ SIZE_T nValueLen)
{
return WriteAny(szNameW, REG_BINARY, lpValue, nValueLen);
}
HRESULT CWindowsRegistry::WriteAny(_In_z_ LPCWSTR szNameW, _In_ DWORD dwType, _In_opt_ LPCVOID lpValue,
_In_ SIZE_T nValueLen)
{
DWORD dwOsErr;
if (hKey == NULL)
return MX_E_NotReady;
if (nValueLen > 0 && lpValue == NULL)
return E_POINTER;
if (nValueLen > 0x7FFFFFFFUL)
return E_INVALIDARG;
//save value
dwOsErr = ::RegSetValueExW(hKey, szNameW, 0, dwType, (const BYTE*)lpValue, (DWORD)nValueLen);
if (dwOsErr != 0)
return MX_HRESULT_FROM_WIN32(dwOsErr);
//done
return S_OK;
}
HRESULT CWindowsRegistry::WritePassword(_In_z_ LPCWSTR szNameW, _In_z_ LPCWSTR szPasswordW)
{
DATA_BLOB sInput, sOutput;
HRESULT hRes;
if (szPasswordW == NULL || *szPasswordW == 0)
return WriteBlob(szNameW, NULL, 0);
::MxMemSet(&sInput, 0, sizeof(sInput));
::MxMemSet(&sOutput, 0, sizeof(sOutput));
sInput.pbData = (LPBYTE)szPasswordW;
sInput.cbData = (DWORD)StrLenW(szPasswordW) * 2;
if (::CryptProtectData(&sInput, NULL, NULL, NULL, NULL, CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN,
&sOutput) == FALSE)
{
return MX_HRESULT_FROM_LASTERROR();
}
hRes = WriteBlob(szNameW, sOutput.pbData, (SIZE_T)(sOutput.cbData));
//cleanup
::LocalFree((HLOCAL)(sOutput.pbData));
//done
return hRes;
}
HRESULT CWindowsRegistry::DeleteKey(_In_z_ LPCWSTR szNameW)
{
UNICODE_STRING usTemp;
SIZE_T nLen;
if (hKey == NULL)
return MX_E_NotReady;
if (szNameW == NULL)
return E_POINTER;
nLen = StrLenW(szNameW);
if (nLen == 0 || nLen >= 32768)
return E_INVALIDARG;
usTemp.Buffer = (PWSTR)szNameW;
usTemp.Length = usTemp.MaximumLength = (USHORT)nLen * 2;
return RecursiveDeleteKey(hKey, &usTemp);
}
HRESULT CWindowsRegistry::DeleteKey(_In_ PUNICODE_STRING Name)
{
if (Name == NULL || Name->Buffer == NULL)