"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/BaseCom.cpp" (10 Oct 2018, 9492 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 "BaseCom.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 <comutil.h>
   16 #include <windows.h>
   17 #include "BaseCom.h"
   18 #include "BootEncryption.h"
   19 #include "Dlgcode.h"
   20 #include "Registry.h"
   21 
   22 using namespace VeraCrypt;
   23 
   24 HRESULT CreateElevatedComObject (HWND hwnd, REFGUID guid, REFIID iid, void **ppv)
   25 {
   26     WCHAR monikerName[1024];
   27     WCHAR clsid[1024];
   28     BIND_OPTS3 bo;
   29 
   30     StringFromGUID2 (guid, clsid, sizeof (clsid) / 2);
   31     swprintf_s (monikerName, sizeof (monikerName) / 2, L"Elevation:Administrator!new:%s", clsid);
   32 
   33     memset (&bo, 0, sizeof (bo));
   34     bo.cbStruct = sizeof (bo);
   35     bo.hwnd = hwnd;
   36     bo.dwClassContext = CLSCTX_LOCAL_SERVER;
   37 
   38     // Prevent the GUI from being half-rendered when the UAC prompt "freezes" it
   39     ProcessPaintMessages (hwnd, 5000);
   40 
   41     return CoGetObject (monikerName, &bo, iid, ppv);
   42 }
   43 
   44 
   45 BOOL ComGetInstanceBase (HWND hWnd, REFCLSID clsid, REFIID iid, void **tcServer)
   46 {
   47     BOOL r;
   48 
   49     if (IsUacSupported ())
   50     {
   51         while (true)
   52         {
   53             r = CreateElevatedComObject (hWnd, clsid, iid, tcServer) == S_OK;
   54             if (r)
   55                 break;
   56             else
   57             {
   58                 if (IDRETRY == ErrorRetryCancel ("UAC_INIT_ERROR", hWnd))
   59                     continue;
   60                 else
   61                     break;
   62             }
   63         }
   64     }
   65     else
   66     {
   67         r = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER, iid, tcServer) == S_OK;
   68         if (!r)
   69             Error ("UAC_INIT_ERROR", hWnd);
   70     }
   71 
   72     return r;
   73 }
   74 
   75 
   76 DWORD BaseCom::CallDriver (DWORD ioctl, BSTR input, BSTR *output)
   77 {
   78     try
   79     {
   80         BootEncryption bootEnc (NULL);
   81         bootEnc.CallDriver (ioctl,
   82             (BYTE *) input, !(BYTE *) input ? 0 : ((DWORD *) ((BYTE *) input))[-1],
   83             (BYTE *) *output, !(BYTE *) *output ? 0 : ((DWORD *) ((BYTE *) *output))[-1]);
   84     }
   85     catch (SystemException &)
   86     {
   87         return GetLastError();
   88     }
   89     catch (Exception &e)
   90     {
   91         e.Show (NULL);
   92         return ERROR_EXCEPTION_IN_SERVICE;
   93     }
   94     catch (...)
   95     {
   96         return ERROR_EXCEPTION_IN_SERVICE;
   97     }
   98 
   99     return ERROR_SUCCESS;
  100 }
  101 
  102 
  103 DWORD BaseCom::CopyFile (BSTR sourceFile, BSTR destinationFile)
  104 {
  105 
  106     if (!::CopyFileW (sourceFile, destinationFile, FALSE))
  107         return GetLastError();
  108 
  109     return ERROR_SUCCESS;
  110 }
  111 
  112 
  113 DWORD BaseCom::DeleteFile (BSTR file)
  114 {
  115 
  116     if (!::DeleteFileW (file))
  117         return GetLastError();
  118 
  119     return ERROR_SUCCESS;
  120 }
  121 
  122 
  123 BOOL BaseCom::IsPagingFileActive (BOOL checkNonWindowsPartitionsOnly)
  124 {
  125     return ::IsPagingFileActive (checkNonWindowsPartitionsOnly);
  126 }
  127 
  128 
  129 DWORD BaseCom::ReadWriteFile (BOOL write, BOOL device, BSTR filePath, BSTR *bufferBstr, unsigned __int64 offset, unsigned __int32 size, DWORD *sizeDone)
  130 {
  131     try
  132     {
  133         auto_ptr <File> file (device ? new Device (filePath, !write) : new File (filePath, !write));
  134         file->CheckOpened (SRC_POS);
  135         file->SeekAt (offset);
  136 
  137         if (write)
  138         {
  139             file->Write ((BYTE *) *bufferBstr, size);
  140             *sizeDone = size;
  141         }
  142         else
  143         {
  144             *sizeDone = file->Read ((BYTE *) *bufferBstr, size);
  145         }
  146     }
  147     catch (SystemException &)
  148     {
  149         return GetLastError();
  150     }
  151     catch (Exception &e)
  152     {
  153         e.Show (NULL);
  154         return ERROR_EXCEPTION_IN_SERVICE;
  155     }
  156     catch (...)
  157     {
  158         return ERROR_EXCEPTION_IN_SERVICE;
  159     }
  160 
  161     return ERROR_SUCCESS;
  162 }
  163 
  164 DWORD BaseCom::GetFileSize (BSTR filePath, unsigned __int64 *pSize)
  165 {
  166     if (!pSize)
  167         return ERROR_INVALID_PARAMETER;
  168 
  169     try
  170     {
  171         std::wstring path (filePath);
  172         File file(filePath, true);
  173         file.CheckOpened (SRC_POS);
  174         file.GetFileSize (*pSize);
  175     }
  176     catch (SystemException &)
  177     {
  178         return GetLastError();
  179     }
  180     catch (Exception &e)
  181     {
  182         e.Show (NULL);
  183         return ERROR_EXCEPTION_IN_SERVICE;
  184     }
  185     catch (...)
  186     {
  187         return ERROR_EXCEPTION_IN_SERVICE;
  188     }
  189 
  190     return ERROR_SUCCESS;
  191 }
  192 
  193 DWORD BaseCom::DeviceIoControl (BOOL readOnly, BOOL device, BSTR filePath, DWORD dwIoControlCode, BSTR input, BSTR *output)
  194 {
  195     try
  196     {
  197         auto_ptr <File> file (device ? new Device (filePath, readOnly == TRUE) : new File (filePath, readOnly == TRUE));
  198         file->CheckOpened (SRC_POS);
  199         if (!file->IoCtl (dwIoControlCode, (BYTE *) input, !(BYTE *) input ? 0 : ((DWORD *) ((BYTE *) input))[-1],
  200             (BYTE *) *output, !(BYTE *) *output ? 0 : ((DWORD *) ((BYTE *) *output))[-1]))
  201         {
  202             return GetLastError();
  203         }
  204     }
  205     catch (SystemException &)
  206     {
  207         return GetLastError();
  208     }
  209     catch (Exception &e)
  210     {
  211         e.Show (NULL);
  212         return ERROR_EXCEPTION_IN_SERVICE;
  213     }
  214     catch (...)
  215     {
  216         return ERROR_EXCEPTION_IN_SERVICE;
  217     }
  218 
  219     return ERROR_SUCCESS;
  220 }
  221 
  222 DWORD BaseCom::RegisterFilterDriver (BOOL registerDriver, int filterType)
  223 {
  224     try
  225     {
  226         BootEncryption bootEnc (NULL);
  227         bootEnc.RegisterFilterDriver (registerDriver ? true : false, (BootEncryption::FilterType) filterType);
  228     }
  229     catch (SystemException &)
  230     {
  231         return GetLastError();
  232     }
  233     catch (Exception &e)
  234     {
  235         e.Show (NULL);
  236         return ERROR_EXCEPTION_IN_SERVICE;
  237     }
  238     catch (...)
  239     {
  240         return ERROR_EXCEPTION_IN_SERVICE;
  241     }
  242 
  243     return ERROR_SUCCESS;
  244 }
  245 
  246 
  247 DWORD BaseCom::RegisterSystemFavoritesService (BOOL registerService)
  248 {
  249     try
  250     {
  251         BootEncryption bootEnc (NULL);
  252         bootEnc.RegisterSystemFavoritesService (registerService);
  253     }
  254     catch (SystemException &)
  255     {
  256         return GetLastError();
  257     }
  258     catch (Exception &e)
  259     {
  260         e.Show (NULL);
  261         return ERROR_EXCEPTION_IN_SERVICE;
  262     }
  263     catch (...)
  264     {
  265         return ERROR_EXCEPTION_IN_SERVICE;
  266     }
  267 
  268     return ERROR_SUCCESS;
  269 }
  270 
  271 
  272 DWORD BaseCom::SetDriverServiceStartType (DWORD startType)
  273 {
  274     try
  275     {
  276         BootEncryption bootEnc (NULL);
  277         bootEnc.SetDriverServiceStartType (startType);
  278     }
  279     catch (SystemException &)
  280     {
  281         return GetLastError();
  282     }
  283     catch (Exception &e)
  284     {
  285         e.Show (NULL);
  286         return ERROR_EXCEPTION_IN_SERVICE;
  287     }
  288     catch (...)
  289     {
  290         return ERROR_EXCEPTION_IN_SERVICE;
  291     }
  292 
  293     return ERROR_SUCCESS;
  294 }
  295 
  296 
  297 DWORD BaseCom::WriteLocalMachineRegistryDwordValue (BSTR keyPath, BSTR valueName, DWORD value)
  298 {
  299     if (!::WriteLocalMachineRegistryDword (keyPath, valueName, value))
  300         return GetLastError();
  301 
  302     return ERROR_SUCCESS;
  303 }
  304 DWORD BaseCom::InstallEfiBootLoader (BOOL preserveUserConfig, BOOL hiddenOSCreation, int pim, int hashAlg)
  305 {
  306     try
  307     {
  308         BootEncryption bootEnc (NULL);
  309         bootEnc.InstallBootLoader (preserveUserConfig? true : false, hiddenOSCreation? true : false, pim, hashAlg);
  310     }
  311     catch (SystemException &)
  312     {
  313         return GetLastError();
  314     }
  315     catch (Exception &e)
  316     {
  317         e.Show (NULL);
  318         return ERROR_EXCEPTION_IN_SERVICE;
  319     }
  320     catch (...)
  321     {
  322         return ERROR_EXCEPTION_IN_SERVICE;
  323     }
  324 
  325     return ERROR_SUCCESS;
  326 }
  327 
  328 DWORD BaseCom::BackupEfiSystemLoader ()
  329 {
  330     try
  331     {
  332         BootEncryption bootEnc (NULL);
  333         bootEnc.BackupSystemLoader ();
  334     }
  335     catch (SystemException &)
  336     {
  337         return GetLastError();
  338     }
  339     catch (UserAbort&)
  340     {
  341         return ERROR_CANCELLED;
  342     }
  343     catch (Exception &e)
  344     {
  345         e.Show (NULL);
  346         return ERROR_EXCEPTION_IN_SERVICE;
  347     }
  348     catch (...)
  349     {
  350         return ERROR_EXCEPTION_IN_SERVICE;
  351     }
  352 
  353     return ERROR_SUCCESS;
  354 }
  355 
  356 DWORD BaseCom::RestoreEfiSystemLoader ()
  357 {
  358     try
  359     {
  360         BootEncryption bootEnc (NULL);
  361         bootEnc.RestoreSystemLoader ();
  362     }
  363     catch (SystemException &)
  364     {
  365         return GetLastError();
  366     }
  367     catch (Exception &e)
  368     {
  369         e.Show (NULL);
  370         return ERROR_EXCEPTION_IN_SERVICE;
  371     }
  372     catch (...)
  373     {
  374         return ERROR_EXCEPTION_IN_SERVICE;
  375     }
  376 
  377     return ERROR_SUCCESS;
  378 }
  379 
  380 DWORD BaseCom::GetEfiBootDeviceNumber (BSTR* pSdn)
  381 {
  382     if (!pSdn || !(*pSdn) || ((((DWORD *) ((BYTE *) *pSdn))[-1]) < sizeof (STORAGE_DEVICE_NUMBER)))
  383         return ERROR_INVALID_PARAMETER;
  384 
  385     try
  386     {
  387         BootEncryption bootEnc (NULL);
  388         bootEnc.GetEfiBootDeviceNumber ((PSTORAGE_DEVICE_NUMBER) *pSdn);
  389     }
  390     catch (SystemException &)
  391     {
  392         return GetLastError();
  393     }
  394     catch (Exception &e)
  395     {
  396         e.Show (NULL);
  397         return ERROR_EXCEPTION_IN_SERVICE;
  398     }
  399     catch (...)
  400     {
  401         return ERROR_EXCEPTION_IN_SERVICE;
  402     }
  403 
  404     return ERROR_SUCCESS;
  405 }
  406 
  407 DWORD BaseCom::GetSecureBootConfig (BOOL* pSecureBootEnabled, BOOL *pVeraCryptKeysLoaded)
  408 {
  409     if (!pSecureBootEnabled || !pVeraCryptKeysLoaded)
  410         return ERROR_INVALID_PARAMETER;
  411 
  412     try
  413     {
  414         BootEncryption bootEnc (NULL);
  415         bootEnc.GetSecureBootConfig (pSecureBootEnabled, pVeraCryptKeysLoaded);
  416     }
  417     catch (SystemException &)
  418     {
  419         return GetLastError();
  420     }
  421     catch (Exception &e)
  422     {
  423         e.Show (NULL);
  424         return ERROR_EXCEPTION_IN_SERVICE;
  425     }
  426     catch (...)
  427     {
  428         return ERROR_EXCEPTION_IN_SERVICE;
  429     }
  430 
  431     return ERROR_SUCCESS;
  432 }
  433 
  434 DWORD BaseCom::WriteEfiBootSectorUserConfig (DWORD userConfig, BSTR customUserMessage, int pim, int hashAlg)
  435 {
  436     if (!customUserMessage)
  437         return ERROR_INVALID_PARAMETER;
  438 
  439     try
  440     {
  441         DWORD maxSize = ((DWORD *) ((BYTE *) customUserMessage))[-1];
  442         char* msg = (char*) *customUserMessage;
  443         if (maxSize > 0)
  444             msg [maxSize - 1] = 0;
  445         std::string msgStr = maxSize > 0 ? msg : "";
  446         BootEncryption bootEnc (NULL);
  447         bootEnc.WriteEfiBootSectorUserConfig ((byte) userConfig,  msgStr, pim, hashAlg);
  448     }
  449     catch (SystemException &)
  450     {
  451         return GetLastError();
  452     }
  453     catch (Exception &e)
  454     {
  455         e.Show (NULL);
  456         return ERROR_EXCEPTION_IN_SERVICE;
  457     }
  458     catch (...)
  459     {
  460         return ERROR_EXCEPTION_IN_SERVICE;
  461     }
  462 
  463     return ERROR_SUCCESS;
  464 }
  465 
  466 DWORD BaseCom::UpdateSetupConfigFile (BOOL bForInstall)
  467 {
  468     try
  469     {
  470         BootEncryption bootEnc (NULL);
  471         bootEnc.UpdateSetupConfigFile (bForInstall? true : false);
  472     }
  473     catch (SystemException &)
  474     {
  475         return GetLastError();
  476     }
  477     catch (Exception &e)
  478     {
  479         e.Show (NULL);
  480         return ERROR_EXCEPTION_IN_SERVICE;
  481     }
  482     catch (...)
  483     {
  484         return ERROR_EXCEPTION_IN_SERVICE;
  485     }
  486 
  487     return ERROR_SUCCESS;
  488 }