"Fossies" - the Fresh Open Source Software Archive 
1 /*
2 Derived from source code of TrueCrypt 7.1a, which is
3 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
4 by the TrueCrypt License 3.0.
5
6 Modifications and additions to the original source code (contained in this file)
7 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
8 and are governed by the Apache License 2.0 the full text of which is
9 contained in the file License.txt included in VeraCrypt binary and source
10 code distribution packages.
11 */
12
13 #include <atlcomcli.h>
14 #include <atlconv.h>
15 #include <comutil.h>
16 #include <windows.h>
17 #include "BaseCom.h"
18 #include "BootEncryption.h"
19 #include "Dlgcode.h"
20 #include "Format.h"
21 #include "Progress.h"
22 #include "TcFormat.h"
23 #include "FormatCom.h"
24 #include "FormatCom_h.h"
25 #include "FormatCom_i.c"
26
27 using namespace VeraCrypt;
28
29 static volatile LONG ObjectCount = 0;
30
31 class TrueCryptFormatCom : public ITrueCryptFormatCom
32 {
33
34 public:
35 TrueCryptFormatCom (DWORD messageThreadId) : RefCount (0),
36 MessageThreadId (messageThreadId),
37 CallBack (NULL)
38 {
39 InterlockedIncrement (&ObjectCount);
40 }
41
42 virtual ~TrueCryptFormatCom ()
43 {
44 if (InterlockedDecrement (&ObjectCount) == 0)
45 PostThreadMessage (MessageThreadId, WM_APP, 0, 0);
46 }
47
48 virtual ULONG STDMETHODCALLTYPE AddRef ()
49 {
50 return InterlockedIncrement (&RefCount);
51 }
52
53 virtual ULONG STDMETHODCALLTYPE Release ()
54 {
55 if (!InterlockedDecrement (&RefCount))
56 {
57 delete this;
58 return 0;
59 }
60
61 return RefCount;
62 }
63
64 virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID riid, void **ppvObject)
65 {
66 if (riid == IID_IUnknown || riid == IID_ITrueCryptFormatCom)
67 *ppvObject = this;
68 else
69 {
70 *ppvObject = NULL;
71 return E_NOINTERFACE;
72 }
73
74 AddRef ();
75 return S_OK;
76 }
77
78 virtual DWORD STDMETHODCALLTYPE CallDriver (DWORD ioctl, BSTR input, BSTR *output)
79 {
80 return BaseCom::CallDriver (ioctl, input, output);
81 }
82
83 virtual DWORD STDMETHODCALLTYPE CopyFile (BSTR sourceFile, BSTR destinationFile)
84 {
85 return BaseCom::CopyFile (sourceFile, destinationFile);
86 }
87
88 virtual DWORD STDMETHODCALLTYPE DeleteFile (BSTR file)
89 {
90 return BaseCom::DeleteFile (file);
91 }
92
93 virtual BOOL STDMETHODCALLTYPE FormatNtfs (int driveNo, int clusterSize)
94 {
95 return ::FormatNtfs (driveNo, clusterSize);
96 }
97
98 virtual int STDMETHODCALLTYPE AnalyzeHiddenVolumeHost (
99 LONG_PTR hwndDlg, int *driveNo, __int64 hiddenVolHostSize, int *realClusterSize, __int64 *nbrFreeClusters)
100 {
101 return ::AnalyzeHiddenVolumeHost (
102 (HWND) hwndDlg, driveNo, hiddenVolHostSize, realClusterSize, nbrFreeClusters);
103 }
104
105 virtual DWORD STDMETHODCALLTYPE ReadWriteFile (BOOL write, BOOL device, BSTR filePath, BSTR *bufferBstr, unsigned __int64 offset, unsigned __int32 size, DWORD *sizeDone)
106 {
107 return BaseCom::ReadWriteFile (write, device, filePath, bufferBstr, offset, size, sizeDone);
108 }
109
110 virtual DWORD STDMETHODCALLTYPE RegisterFilterDriver (BOOL registerDriver, int filterType)
111 {
112 return BaseCom::RegisterFilterDriver (registerDriver, filterType);
113 }
114
115 virtual DWORD STDMETHODCALLTYPE RegisterSystemFavoritesService (BOOL registerService)
116 {
117 return BaseCom::RegisterSystemFavoritesService (registerService);
118 }
119
120 virtual DWORD STDMETHODCALLTYPE SetDriverServiceStartType (DWORD startType)
121 {
122 return BaseCom::SetDriverServiceStartType (startType);
123 }
124
125 virtual BOOL STDMETHODCALLTYPE IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly)
126 {
127 return BaseCom::IsPagingFileActive (checkNonWindowsPartitionsOnly);
128 }
129
130 virtual DWORD STDMETHODCALLTYPE WriteLocalMachineRegistryDwordValue (BSTR keyPath, BSTR valueName, DWORD value)
131 {
132 return BaseCom::WriteLocalMachineRegistryDwordValue (keyPath, valueName, value);
133 }
134
135 virtual BOOL STDMETHODCALLTYPE FormatFs (int driveNo, int clusterSize, int fsType)
136 {
137 return ::FormatFs (driveNo, clusterSize, fsType);
138 }
139
140 virtual DWORD STDMETHODCALLTYPE GetFileSize (BSTR filePath, unsigned __int64 *pSize)
141 {
142 return BaseCom::GetFileSize (filePath, pSize);
143 }
144
145 virtual DWORD STDMETHODCALLTYPE DeviceIoControl (BOOL readOnly, BOOL device, BSTR filePath, DWORD dwIoControlCode, BSTR input, BSTR *output)
146 {
147 return BaseCom::DeviceIoControl (readOnly, device, filePath, dwIoControlCode, input, output);
148 }
149
150 virtual DWORD STDMETHODCALLTYPE InstallEfiBootLoader (BOOL preserveUserConfig, BOOL hiddenOSCreation, int pim, int hashAlg)
151 {
152 return BaseCom::InstallEfiBootLoader (preserveUserConfig, hiddenOSCreation, pim, hashAlg);
153 }
154
155 virtual DWORD STDMETHODCALLTYPE BackupEfiSystemLoader ()
156 {
157 return BaseCom::BackupEfiSystemLoader ();
158 }
159
160 virtual DWORD STDMETHODCALLTYPE RestoreEfiSystemLoader ()
161 {
162 return BaseCom::RestoreEfiSystemLoader ();
163 }
164
165 virtual DWORD STDMETHODCALLTYPE GetEfiBootDeviceNumber (BSTR* pSdn)
166 {
167 return BaseCom::GetEfiBootDeviceNumber (pSdn);
168 }
169
170 virtual DWORD STDMETHODCALLTYPE GetSecureBootConfig (BOOL* pSecureBootEnabled, BOOL *pVeraCryptKeysLoaded)
171 {
172 return BaseCom::GetSecureBootConfig (pSecureBootEnabled, pVeraCryptKeysLoaded);
173 }
174
175 virtual DWORD STDMETHODCALLTYPE WriteEfiBootSectorUserConfig (DWORD userConfig, BSTR customUserMessage, int pim, int hashAlg)
176 {
177 return BaseCom::WriteEfiBootSectorUserConfig (userConfig, customUserMessage,pim, hashAlg);
178 }
179
180 virtual DWORD STDMETHODCALLTYPE UpdateSetupConfigFile (BOOL bForInstall)
181 {
182 return BaseCom::UpdateSetupConfigFile (bForInstall);
183 }
184
185 protected:
186 DWORD MessageThreadId;
187 LONG RefCount;
188 ITrueCryptFormatCom *CallBack;
189 };
190
191
192 extern "C" BOOL ComServerFormat ()
193 {
194 SetProcessShutdownParameters (0x100, 0);
195
196 TrueCryptFactory<TrueCryptFormatCom> factory (GetCurrentThreadId ());
197 DWORD cookie;
198
199 if (IsUacSupported ())
200 UacElevated = TRUE;
201
202 if (CoRegisterClassObject (CLSID_TrueCryptFormatCom, (LPUNKNOWN) &factory,
203 CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
204 return FALSE;
205
206 MSG msg;
207 while (int r = GetMessageW (&msg, NULL, 0, 0))
208 {
209 if (r == -1)
210 return FALSE;
211
212 TranslateMessage (&msg);
213 DispatchMessageW (&msg);
214
215 if (msg.message == WM_APP
216 && ObjectCount < 1
217 && !factory.IsServerLocked ())
218 break;
219 }
220 CoRevokeClassObject (cookie);
221
222 return TRUE;
223 }
224
225
226 static BOOL ComGetInstance (HWND hWnd, ITrueCryptFormatCom **tcServer)
227 {
228 return ComGetInstanceBase (hWnd, CLSID_TrueCryptFormatCom, IID_ITrueCryptFormatCom, (void **) tcServer);
229 }
230
231
232 ITrueCryptFormatCom *GetElevatedInstance (HWND parent)
233 {
234 ITrueCryptFormatCom *instance;
235
236 if (!ComGetInstance (parent, &instance))
237 throw UserAbort (SRC_POS);
238
239 return instance;
240 }
241
242
243 extern "C" int UacFormatNtfs (HWND hWnd, int driveNo, int clusterSize)
244 {
245 CComPtr<ITrueCryptFormatCom> tc;
246 int r;
247
248 CoInitialize (NULL);
249
250 if (ComGetInstance (hWnd, &tc))
251 r = tc->FormatNtfs (driveNo, clusterSize);
252 else
253 r = 0;
254
255 CoUninitialize ();
256
257 return r;
258 }
259
260 extern "C" int UacFormatFs (HWND hWnd, int driveNo, int clusterSize, int fsType)
261 {
262 CComPtr<ITrueCryptFormatCom> tc;
263 int r;
264
265 CoInitialize (NULL);
266
267 if (ComGetInstance (hWnd, &tc))
268 r = tc->FormatFs (driveNo, clusterSize, fsType);
269 else
270 r = 0;
271
272 CoUninitialize ();
273
274 return r;
275 }
276
277
278 extern "C" int UacAnalyzeHiddenVolumeHost (HWND hwndDlg, int *driveNo, __int64 hiddenVolHostSize, int *realClusterSize, __int64 *nbrFreeClusters)
279 {
280 CComPtr<ITrueCryptFormatCom> tc;
281 int r;
282
283 CoInitialize (NULL);
284
285 if (ComGetInstance (hwndDlg, &tc))
286 r = tc->AnalyzeHiddenVolumeHost ((LONG_PTR) hwndDlg, driveNo, hiddenVolHostSize, realClusterSize, nbrFreeClusters);
287 else
288 r = 0;
289
290 CoUninitialize ();
291
292 return r;
293 }