Skip to content

Commit e818379

Browse files
author
Valentin
committed
Send VC_SERVICE_CONTROL_BUILD_DEVICE_LIST to VeraCrypt service
This commit adds functionality to send the VC_SERVICE_CONTROL_BUILD_DEVICE_LIST command to the VeraCrypt service when locking the station, so that reconnecting devices does not cause encryption keys to be cleared.
1 parent 2d6a94e commit e818379

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

VCEnhancedKeyClear/VCEnhancedKeyClear.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// VeraCrypt defines
2+
#define TC_SYSTEM_FAVORITES_SERVICE_NAME L"VeraCryptSystemFavorites"
13
#define VC_DRIVER_CONFIG_CLEAR_KEYS_ON_NEW_DEVICE_INSERTION 0x400
4+
#define VC_SERVICE_CONTROL_BUILD_DEVICE_LIST 128
25

6+
// VCEKC defines
37
#define VCEKC_CLASSNAME _T("VCEnhancedKeyClear_WndClass")
48
#define VCEKC_WINDOWNAME _T("VCEnhancedKeyClear_Wnd")
59
#define VCEKC_MSGTITLE _T("VeraCrypt Enhanced Key Clear")
@@ -18,6 +22,33 @@
1822

1923
NOTIFYICONDATA nid{};
2024

25+
bool VeraCryptBuildDeviceList()
26+
{
27+
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
28+
bool bRet = false;
29+
30+
if (hSCManager != NULL)
31+
{
32+
SC_HANDLE hService = OpenService(hSCManager, TC_SYSTEM_FAVORITES_SERVICE_NAME, SERVICE_ALL_ACCESS);
33+
34+
if (hService != NULL)
35+
{
36+
SERVICE_STATUS serviceStatus{};
37+
38+
if (ControlService(hService, VC_SERVICE_CONTROL_BUILD_DEVICE_LIST, &serviceStatus))
39+
{
40+
bRet = true;
41+
}
42+
43+
CloseServiceHandle(hService);
44+
}
45+
46+
CloseServiceHandle(hSCManager);
47+
}
48+
49+
return bRet;
50+
}
51+
2152
bool SetClearKeysFlag(bool state)
2253
{
2354
HKEY hkey = NULL;
@@ -58,24 +89,31 @@ LRESULT CALLBACK VcekcWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
5889
{
5990
case WM_WTSSESSION_CHANGE:
6091
{
61-
bool res = true;
92+
bool resSetConfig = true;
93+
bool resBuildDeviceList = true;
6294

6395
switch (wParam)
6496
{
6597
default: break;
6698
case WTS_SESSION_UNLOCK:
67-
res = SetClearKeysFlag(false);
99+
resSetConfig = SetClearKeysFlag(false);
68100
break;
69101
case WTS_SESSION_LOCK:
70-
res = SetClearKeysFlag(true);
102+
resBuildDeviceList = VeraCryptBuildDeviceList();
103+
resSetConfig = SetClearKeysFlag(true);
71104
break;
72105
}
73106

74-
if (!res)
107+
if (!resSetConfig)
75108
{
76109
MessageBox(NULL, _T("Failed to change VC_DRIVER_CONFIG_CLEAR_KEYS_ON_NEW_DEVICE_INSERTION"), VCEKC_MSGTITLE, MB_ICONEXCLAMATION | MB_OK);
77110
}
78111

112+
if (!resBuildDeviceList)
113+
{
114+
MessageBox(NULL, _T("Failed to send VC_SERVICE_CONTROL_BUILD_DEVICE_LIST"), VCEKC_MSGTITLE, MB_ICONEXCLAMATION | MB_OK);
115+
}
116+
79117
break;
80118
}
81119
case VCEKC_SHELLICONMSG:

0 commit comments

Comments
 (0)