"Fossies" - the Fresh Open Source Software Archive

Member "src/Mount/MainCom.cpp" (10 Oct 2018, 9139 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MainCom.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.22_Source_vs_1.23_Source.

    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 <windows.h>
   16 #include "BaseCom.h"
   17 #include "BootEncryption.h"
   18 #include "Dlgcode.h"
   19 #include "MainCom.h"
   20 #include "MainCom_h.h"
   21 #include "MainCom_i.c"
   22 #include "Mount.h"
   23 #include "Password.h"
   24 
   25 using namespace VeraCrypt;
   26 
   27 static volatile LONG ObjectCount = 0;
   28 
   29 class TrueCryptMainCom : public ITrueCryptMainCom
   30 {
   31 
   32 public:
   33     TrueCryptMainCom (DWORD messageThreadId) : RefCount (0), MessageThreadId (messageThreadId)
   34     {
   35         InterlockedIncrement (&ObjectCount);
   36     }
   37 
   38     virtual ~TrueCryptMainCom ()
   39     {
   40         if (InterlockedDecrement (&ObjectCount) == 0)
   41             PostThreadMessage (MessageThreadId, WM_APP, 0, 0);
   42     }
   43 
   44     virtual ULONG STDMETHODCALLTYPE AddRef ()
   45     {
   46         return InterlockedIncrement (&RefCount);
   47     }
   48 
   49     virtual ULONG STDMETHODCALLTYPE Release ()
   50     {
   51         if (!InterlockedDecrement (&RefCount))
   52         {
   53             delete this;
   54             return 0;
   55         }
   56 
   57         return RefCount;
   58     }
   59 
   60     virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID riid, void **ppvObject)
   61     {
   62         if (riid == IID_IUnknown || riid == IID_ITrueCryptMainCom)
   63             *ppvObject = this;
   64         else
   65         {
   66             *ppvObject = NULL;
   67             return E_NOINTERFACE;
   68         }
   69 
   70         AddRef ();
   71         return S_OK;
   72     }
   73 
   74     virtual void STDMETHODCALLTYPE AnalyzeKernelMiniDump (LONG_PTR hwndDlg)
   75     {
   76         // Do nothing
   77         MainDlg = (HWND) hwndDlg;
   78     }
   79 
   80     virtual int STDMETHODCALLTYPE BackupVolumeHeader (LONG_PTR hwndDlg, BOOL bRequireConfirmation, BSTR lpszVolume)
   81     {
   82         MainDlg = (HWND) hwndDlg;
   83         return ::BackupVolumeHeader ((HWND) hwndDlg, bRequireConfirmation, lpszVolume);
   84     }
   85 
   86     virtual int STDMETHODCALLTYPE RestoreVolumeHeader (LONG_PTR hwndDlg, BSTR lpszVolume)
   87     {
   88         MainDlg = (HWND) hwndDlg;
   89         return ::RestoreVolumeHeader ((HWND) hwndDlg, lpszVolume);
   90     }
   91 
   92     virtual DWORD STDMETHODCALLTYPE CallDriver (DWORD ioctl, BSTR input, BSTR *output)
   93     {
   94         return BaseCom::CallDriver (ioctl, input, output);
   95     }
   96 
   97     virtual int STDMETHODCALLTYPE ChangePassword (BSTR volumePath, Password *oldPassword, Password *newPassword, int pkcs5, int wipePassCount, LONG_PTR hWnd)
   98     {
   99         MainDlg = (HWND) hWnd;
  100         return ::ChangePwd (volumePath, oldPassword, 0, 0, FALSE, newPassword, pkcs5, 0, wipePassCount, (HWND) hWnd);
  101     }
  102 
  103     virtual DWORD STDMETHODCALLTYPE CopyFile (BSTR sourceFile, BSTR destinationFile)
  104     {
  105         return BaseCom::CopyFile (sourceFile, destinationFile);
  106     }
  107 
  108     virtual DWORD STDMETHODCALLTYPE DeleteFile (BSTR file)
  109     {
  110         return BaseCom::DeleteFile (file);
  111     }
  112 
  113     virtual BOOL STDMETHODCALLTYPE IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly)
  114     {
  115         return BaseCom::IsPagingFileActive (checkNonWindowsPartitionsOnly);
  116     }
  117 
  118     virtual DWORD STDMETHODCALLTYPE ReadWriteFile (BOOL write, BOOL device, BSTR filePath, BSTR *bufferBstr, unsigned __int64 offset, unsigned __int32 size, DWORD *sizeDone)
  119     {
  120         return BaseCom::ReadWriteFile (write, device, filePath, bufferBstr, offset, size, sizeDone);
  121     }
  122 
  123     virtual DWORD STDMETHODCALLTYPE RegisterFilterDriver (BOOL registerDriver, int filterType)
  124     {
  125         return BaseCom::RegisterFilterDriver (registerDriver, filterType);
  126     }
  127 
  128     virtual DWORD STDMETHODCALLTYPE RegisterSystemFavoritesService (BOOL registerService)
  129     {
  130         return BaseCom::RegisterSystemFavoritesService (registerService);
  131     }
  132 
  133     virtual DWORD STDMETHODCALLTYPE SetDriverServiceStartType (DWORD startType)
  134     {
  135         return BaseCom::SetDriverServiceStartType (startType);
  136     }
  137 
  138     virtual DWORD STDMETHODCALLTYPE WriteLocalMachineRegistryDwordValue (BSTR keyPath, BSTR valueName, DWORD value)
  139     {
  140         return BaseCom::WriteLocalMachineRegistryDwordValue (keyPath, valueName, value);
  141     }
  142 
  143     virtual int STDMETHODCALLTYPE ChangePasswordEx (BSTR volumePath, Password *oldPassword, int old_pkcs5, Password *newPassword, int pkcs5, int wipePassCount, LONG_PTR hWnd)
  144     {
  145         MainDlg = (HWND) hWnd;
  146         return ::ChangePwd (volumePath, oldPassword, old_pkcs5, 0, FALSE, newPassword, pkcs5, 0, wipePassCount, (HWND) hWnd);
  147     }
  148 
  149     virtual int STDMETHODCALLTYPE ChangePasswordEx2 (BSTR volumePath, Password *oldPassword, int old_pkcs5, BOOL truecryptMode, Password *newPassword, int pkcs5, int wipePassCount, LONG_PTR hWnd)
  150     {
  151         MainDlg = (HWND) hWnd;
  152         return ::ChangePwd (volumePath, oldPassword, old_pkcs5, 0, truecryptMode, newPassword, pkcs5, 0, wipePassCount, (HWND) hWnd);
  153     }
  154 
  155     virtual int STDMETHODCALLTYPE ChangePasswordEx3 (BSTR volumePath, Password *oldPassword, int old_pkcs5, int old_pim, BOOL truecryptMode, Password *newPassword, int pkcs5, int pim, int wipePassCount, LONG_PTR hWnd)
  156     {
  157         MainDlg = (HWND) hWnd;
  158         return ::ChangePwd (volumePath, oldPassword, old_pkcs5, old_pim, truecryptMode, newPassword, pkcs5, pim, wipePassCount, (HWND) hWnd);
  159     }
  160 
  161     virtual DWORD STDMETHODCALLTYPE GetFileSize (BSTR filePath, unsigned __int64 *pSize)
  162     {
  163         return BaseCom::GetFileSize (filePath, pSize);
  164     }
  165 
  166     virtual DWORD STDMETHODCALLTYPE DeviceIoControl (BOOL readOnly, BOOL device, BSTR filePath, DWORD dwIoControlCode, BSTR input, BSTR *output)
  167     {
  168         return BaseCom::DeviceIoControl (readOnly, device, filePath, dwIoControlCode, input, output);
  169     }
  170 
  171     virtual DWORD STDMETHODCALLTYPE InstallEfiBootLoader (BOOL preserveUserConfig, BOOL hiddenOSCreation, int pim, int hashAlg)
  172     {
  173         return BaseCom::InstallEfiBootLoader (preserveUserConfig, hiddenOSCreation, pim, hashAlg);
  174     }
  175 
  176     virtual DWORD STDMETHODCALLTYPE BackupEfiSystemLoader ()
  177     {
  178         return BaseCom::BackupEfiSystemLoader ();
  179     }
  180 
  181     virtual DWORD STDMETHODCALLTYPE RestoreEfiSystemLoader ()
  182     {
  183         return BaseCom::RestoreEfiSystemLoader ();
  184     }
  185 
  186     virtual DWORD STDMETHODCALLTYPE GetEfiBootDeviceNumber (BSTR* pSdn)
  187     {
  188         return BaseCom::GetEfiBootDeviceNumber (pSdn);
  189     }
  190 
  191     virtual DWORD STDMETHODCALLTYPE GetSecureBootConfig (BOOL* pSecureBootEnabled, BOOL *pVeraCryptKeysLoaded)
  192     {
  193         return BaseCom::GetSecureBootConfig (pSecureBootEnabled, pVeraCryptKeysLoaded);
  194     }
  195 
  196     virtual DWORD STDMETHODCALLTYPE WriteEfiBootSectorUserConfig (DWORD userConfig, BSTR customUserMessage, int pim, int hashAlg)
  197     {
  198         return BaseCom::WriteEfiBootSectorUserConfig (userConfig, customUserMessage,pim, hashAlg);
  199     }
  200 
  201     virtual DWORD STDMETHODCALLTYPE UpdateSetupConfigFile (BOOL bForInstall)
  202     {
  203         return BaseCom::UpdateSetupConfigFile (bForInstall);
  204     }
  205 
  206 protected:
  207     DWORD MessageThreadId;
  208     LONG RefCount;
  209 };
  210 
  211 
  212 extern "C" BOOL ComServerMain ()
  213 {
  214     SetProcessShutdownParameters (0x100, 0);
  215 
  216     TrueCryptFactory<TrueCryptMainCom> factory (GetCurrentThreadId ());
  217     DWORD cookie;
  218 
  219     if (IsUacSupported ())
  220         UacElevated = TRUE;
  221 
  222     if (CoRegisterClassObject (CLSID_TrueCryptMainCom, (LPUNKNOWN) &factory,
  223         CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &cookie) != S_OK)
  224         return FALSE;
  225 
  226     MSG msg;
  227     while (int r = GetMessageW (&msg, NULL, 0, 0))
  228     {
  229         if (r == -1)
  230             return FALSE;
  231 
  232         TranslateMessage (&msg);
  233         DispatchMessageW (&msg);
  234 
  235         if (msg.message == WM_APP
  236             && ObjectCount < 1
  237             && !factory.IsServerLocked ())
  238             break;
  239     }
  240     CoRevokeClassObject (cookie);
  241 
  242     return TRUE;
  243 }
  244 
  245 
  246 static BOOL ComGetInstance (HWND hWnd, ITrueCryptMainCom **tcServer)
  247 {
  248     return ComGetInstanceBase (hWnd, CLSID_TrueCryptMainCom, IID_ITrueCryptMainCom, (void **) tcServer);
  249 }
  250 
  251 
  252 ITrueCryptMainCom *GetElevatedInstance (HWND parent)
  253 {
  254     ITrueCryptMainCom *instance;
  255 
  256     if (!ComGetInstance (parent, &instance))
  257         throw UserAbort (SRC_POS);
  258 
  259     return instance;
  260 }
  261 
  262 
  263 extern "C" int UacBackupVolumeHeader (HWND hwndDlg, BOOL bRequireConfirmation, wchar_t *lpszVolume)
  264 {
  265     CComPtr<ITrueCryptMainCom> tc;
  266     int r;
  267 
  268     CoInitialize (NULL);
  269 
  270     if (ComGetInstance (hwndDlg, &tc))
  271     {
  272         CComBSTR volumeBstr;
  273         BSTR bstr = W2BSTR(lpszVolume);
  274         if (bstr)
  275         {
  276             volumeBstr.Attach (bstr);
  277             r = tc->BackupVolumeHeader ((LONG_PTR) hwndDlg, bRequireConfirmation, volumeBstr);
  278         }
  279         else
  280             r = ERR_OUTOFMEMORY;
  281     }
  282     else
  283         r = -1;
  284 
  285     CoUninitialize ();
  286 
  287     return r;
  288 }
  289 
  290 
  291 extern "C" int UacRestoreVolumeHeader (HWND hwndDlg, wchar_t *lpszVolume)
  292 {
  293     CComPtr<ITrueCryptMainCom> tc;
  294     int r;
  295 
  296     CoInitialize (NULL);
  297 
  298     if (ComGetInstance (hwndDlg, &tc))
  299     {
  300         CComBSTR volumeBstr;
  301         BSTR bstr = W2BSTR(lpszVolume);
  302         if (bstr)
  303         {
  304             volumeBstr.Attach (bstr);
  305             r = tc->RestoreVolumeHeader ((LONG_PTR) hwndDlg, volumeBstr);
  306         }
  307         else
  308             r = ERR_OUTOFMEMORY;
  309     }
  310     else
  311         r = -1;
  312 
  313     CoUninitialize ();
  314 
  315     return r;
  316 }
  317 
  318 
  319 extern "C" int UacChangePwd (wchar_t *lpszVolume, Password *oldPassword, int old_pkcs5, int old_pim, BOOL truecryptMode, Password *newPassword, int pkcs5, int pim, int wipePassCount, HWND hwndDlg)
  320 {
  321     CComPtr<ITrueCryptMainCom> tc;
  322     int r;
  323 
  324     CoInitialize (NULL);
  325 
  326     if (ComGetInstance (hwndDlg, &tc))
  327     {
  328         CComBSTR bstrVolume (lpszVolume);
  329         WaitCursor ();
  330         r = tc->ChangePasswordEx3 (bstrVolume, oldPassword, old_pkcs5, old_pim, truecryptMode, newPassword, pkcs5, pim, wipePassCount, (LONG_PTR) hwndDlg);
  331         NormalCursor ();
  332     }
  333     else
  334         r = -1;
  335 
  336     CoUninitialize ();
  337 
  338     return r;
  339 }