1 /*
2  * PROJECT:     ReactOS Applications Manager
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Various integrity check mechanisms
5  * COPYRIGHT:   Copyright Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
6  *              Copyright Mark Jansen
7  */
8 
9 #include "rapps.h"
10 
11 #include <sha1.h>
12 
13 BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName)
14 {
15     BOOL ret = FALSE;
16 
17     /* first off, does it exist at all? */
18     HANDLE file = CreateFileW(lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
19 
20     if (file == INVALID_HANDLE_VALUE)
21         return FALSE;
22 
23     /* let's grab the actual file size to organize the mmap'ing rounds */
24     LARGE_INTEGER size;
25     GetFileSizeEx(file, &size);
26 
27     /* retrieve a handle to map the file contents to memory */
28     HANDLE map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
29     if (map)
30     {
31         /* map that thing in address space */
32         const unsigned char *file_map = static_cast<const unsigned char *>(MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0));
33         if (file_map)
34         {
35             SHA_CTX ctx;
36             /* initialize the SHA-1 context */
37             A_SHAInit(&ctx);
38 
39             /* feed the data to the cookie monster */
40             A_SHAUpdate(&ctx, file_map, size.LowPart);
41 
42             /* cool, we don't need this anymore */
43             UnmapViewOfFile(file_map);
44 
45             /* we're done, compute the final hash */
46             ULONG sha[5];
47             A_SHAFinal(&ctx, sha);
48 
49             WCHAR buf[(sizeof(sha) * 2) + 1];
50             for (UINT i = 0; i < sizeof(sha); i++)
51                 swprintf(buf + 2 * i, L"%02x", ((unsigned char *) sha)[i]);
52             /* does the resulting SHA1 match with the provided one? */
53             if (!_wcsicmp(buf, lpSHA1Hash))
54                 ret = TRUE;
55         }
56         CloseHandle(map);
57     }
58     CloseHandle(file);
59     return ret;
60 }
61