"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Registry.c" (10 Oct 2018, 7838 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 "Registry.c" see the Fossies "Dox" file reference documentation.

    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 "Tcdefs.h"
   14 #include "Registry.h"
   15 #include <Strsafe.h>
   16 
   17 BOOL ReadLocalMachineRegistryDword (wchar_t *subKey, wchar_t *name, DWORD *value)
   18 {
   19     HKEY hkey = 0;
   20     DWORD size = sizeof (*value);
   21     DWORD type;
   22 
   23     if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
   24         return FALSE;
   25 
   26     if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) value, &size) != ERROR_SUCCESS)
   27     {
   28         RegCloseKey (hkey);
   29         return FALSE;
   30     }
   31 
   32     RegCloseKey (hkey);
   33     return type == REG_DWORD;
   34 }
   35 
   36 BOOL ReadLocalMachineRegistryMultiString (wchar_t *subKey, wchar_t *name, wchar_t *value, DWORD *size)
   37 {
   38     HKEY hkey = 0;
   39     DWORD type;
   40 
   41     if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
   42         return FALSE;
   43 
   44     if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) value, size) != ERROR_SUCCESS)
   45     {
   46         RegCloseKey (hkey);
   47         return FALSE;
   48     }
   49 
   50     RegCloseKey (hkey);
   51     return type == REG_MULTI_SZ;
   52 }
   53 
   54 BOOL ReadLocalMachineRegistryString (const wchar_t *subKey, wchar_t *name, wchar_t *str, DWORD *size)
   55 {
   56     HKEY hkey = 0;
   57     DWORD type;
   58 
   59     if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
   60         return FALSE;
   61 
   62     if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) str, size) != ERROR_SUCCESS)
   63     {
   64         RegCloseKey (hkey);
   65         return FALSE;
   66     }
   67 
   68     RegCloseKey (hkey);
   69     return type == REG_SZ;
   70 }
   71 
   72 BOOL ReadLocalMachineRegistryStringNonReflected (const wchar_t *subKey, wchar_t *name, wchar_t *str, DWORD *size, BOOL b32bitApp)
   73 {
   74     HKEY hkey = 0;
   75     DWORD type;
   76 
   77     if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ | (b32bitApp? KEY_WOW64_32KEY: KEY_WOW64_64KEY), &hkey) != ERROR_SUCCESS)
   78         return FALSE;
   79 
   80     if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) str, size) != ERROR_SUCCESS)
   81     {
   82         RegCloseKey (hkey);
   83         return FALSE;
   84     }
   85 
   86     RegCloseKey (hkey);
   87     return type == REG_SZ;
   88 }
   89 
   90 int ReadRegistryInt (wchar_t *subKey, wchar_t *name, int defaultValue)
   91 {
   92     HKEY hkey = 0;
   93     DWORD value, size = sizeof (DWORD);
   94 
   95     if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
   96         0, KEY_READ, &hkey) != ERROR_SUCCESS)
   97         return defaultValue;
   98 
   99     if (RegQueryValueEx (hkey, name, 0, 0, (LPBYTE) &value, &size) != ERROR_SUCCESS)
  100         value = defaultValue;
  101 
  102     RegCloseKey (hkey);
  103     return value;
  104 }
  105 
  106 wchar_t *ReadRegistryString (wchar_t *subKey, wchar_t *name, wchar_t *defaultValue, wchar_t *str, int maxLen)
  107 {
  108     HKEY hkey = 0;
  109     wchar_t value[MAX_PATH*4];
  110     DWORD size = sizeof (value);
  111 
  112    str[maxLen/2-1] = 0;
  113     StringCbCopyW (str, maxLen, defaultValue);
  114 
  115     ZeroMemory (value, sizeof value);
  116     if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
  117         0, KEY_READ, &hkey) == ERROR_SUCCESS)
  118         if (RegQueryValueEx (hkey, name, 0, 0, (LPBYTE) value,  &size) == ERROR_SUCCESS)
  119             StringCbCopyW (str, maxLen,value);
  120 
  121     if (hkey)
  122         RegCloseKey (hkey);
  123     return str;
  124 }
  125 
  126 DWORD ReadRegistryBytes (wchar_t *path, wchar_t *name, char *value, int maxLen)
  127 {
  128     HKEY hkey = 0;
  129     DWORD size = maxLen;
  130     BOOL success = FALSE;
  131 
  132     if (RegOpenKeyEx (HKEY_CURRENT_USER, path, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
  133         return 0;
  134 
  135     success = (RegQueryValueEx (hkey, name, 0,  0, (LPBYTE) value,  &size) == ERROR_SUCCESS);
  136     RegCloseKey (hkey);
  137 
  138     return success ? size : 0;
  139 }
  140 
  141 void WriteRegistryInt (wchar_t *subKey, wchar_t *name, int value)
  142 {
  143     HKEY hkey = 0;
  144     DWORD disp;
  145 
  146     if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
  147         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
  148         return;
  149 
  150     RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value);
  151     RegCloseKey (hkey);
  152 }
  153 
  154 BOOL WriteLocalMachineRegistryDword (wchar_t *subKey, wchar_t *name, DWORD value)
  155 {
  156     HKEY hkey = 0;
  157     DWORD disp;
  158     LONG status;
  159 
  160     if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
  161         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
  162     {
  163         SetLastError (status);
  164         return FALSE;
  165     }
  166 
  167     if ((status = RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value)) != ERROR_SUCCESS)
  168     {
  169         RegCloseKey (hkey);
  170         SetLastError (status);
  171         return FALSE;
  172     }
  173 
  174     RegCloseKey (hkey);
  175     return TRUE;
  176 }
  177 
  178 BOOL WriteLocalMachineRegistryMultiString (wchar_t *subKey, wchar_t *name, wchar_t *multiString, DWORD size)
  179 {
  180     HKEY hkey = 0;
  181     DWORD disp;
  182     LONG status;
  183 
  184     if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
  185         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
  186     {
  187         SetLastError (status);
  188         return FALSE;
  189     }
  190 
  191     if ((status = RegSetValueEx (hkey, name, 0, REG_MULTI_SZ, (BYTE *) multiString, size)) != ERROR_SUCCESS)
  192     {
  193         RegCloseKey (hkey);
  194         SetLastError (status);
  195         return FALSE;
  196     }
  197 
  198     RegCloseKey (hkey);
  199     return TRUE;
  200 }
  201 
  202 BOOL WriteLocalMachineRegistryString (wchar_t *subKey, wchar_t *name, wchar_t *str, BOOL expandable)
  203 {
  204     HKEY hkey = 0;
  205     DWORD disp;
  206     LONG status;
  207 
  208     if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
  209         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
  210     {
  211         SetLastError (status);
  212         return FALSE;
  213     }
  214 
  215     if ((status = RegSetValueEx (hkey, name, 0, expandable ? REG_EXPAND_SZ : REG_SZ, (BYTE *) str, (DWORD) (wcslen (str) + 1) * sizeof (wchar_t))) != ERROR_SUCCESS)
  216     {
  217         RegCloseKey (hkey);
  218         SetLastError (status);
  219         return FALSE;
  220     }
  221 
  222     RegCloseKey (hkey);
  223     return TRUE;
  224 }
  225 
  226 void WriteRegistryString (wchar_t *subKey, wchar_t *name, wchar_t *str)
  227 {
  228     HKEY hkey = 0;
  229     DWORD disp;
  230 
  231     if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
  232         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
  233         return;
  234 
  235     RegSetValueEx (hkey, name, 0, REG_SZ, (BYTE *) str, (DWORD) (wcslen (str) + 1) * sizeof (wchar_t));
  236     RegCloseKey (hkey);
  237 }
  238 
  239 BOOL WriteRegistryBytes (wchar_t *path, wchar_t *name, char *str, DWORD size)
  240 {
  241     HKEY hkey = 0;
  242     DWORD disp;
  243     BOOL res;
  244 
  245     if (RegCreateKeyEx (HKEY_CURRENT_USER, path,
  246         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
  247         return FALSE;
  248 
  249     res = RegSetValueEx (hkey, name, 0, REG_BINARY, (BYTE *) str, size);
  250     RegCloseKey (hkey);
  251     return res == ERROR_SUCCESS;
  252 }
  253 
  254 BOOL DeleteLocalMachineRegistryKey (wchar_t *parentKey, wchar_t *subKeyToDelete)
  255 {
  256     LONG status;
  257     HKEY hkey = 0;
  258 
  259     if ((status = RegOpenKeyEx (HKEY_LOCAL_MACHINE, parentKey, 0, KEY_WRITE, &hkey)) != ERROR_SUCCESS)
  260     {
  261         SetLastError (status);
  262         return FALSE;
  263     }
  264 
  265     if ((status = RegDeleteKey (hkey, subKeyToDelete)) != ERROR_SUCCESS)
  266     {
  267         RegCloseKey (hkey);
  268         SetLastError (status);
  269         return FALSE;
  270     }
  271 
  272     RegCloseKey (hkey);
  273     return TRUE;
  274 }
  275 
  276 void DeleteRegistryValue (wchar_t *subKey, wchar_t *name)
  277 {
  278     HKEY hkey = 0;
  279 
  280     if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey, 0, KEY_WRITE, &hkey) != ERROR_SUCCESS)
  281         return;
  282 
  283     RegDeleteValue (hkey, name);
  284     RegCloseKey (hkey);
  285 }
  286 
  287 
  288 void GetStartupRegKeyName (wchar_t *regk, size_t cbRegk)
  289 {
  290     // The string is split in order to prevent some antivirus packages from falsely reporting
  291     // VeraCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan).
  292     StringCbPrintfW (regk, cbRegk,L"%s%s", L"Software\\Microsoft\\Windows\\Curren", L"tVersion\\Run");
  293 }
  294 
  295 void GetRestorePointRegKeyName (wchar_t *regk, size_t cbRegk)
  296 {
  297     // The string is split in order to prevent some antivirus packages from falsely reporting
  298     // VeraCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan).
  299     StringCbPrintfW (regk, cbRegk,L"%s%s%s%s", L"Software\\Microsoft\\Windows", L" NT\\Curren", L"tVersion\\Sy", L"stemRestore");
  300 }