ReactOS 0.4.17-dev-966-gf06eace
integrity.c File Reference
#include "wine/test.h"
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winerror.h"
#include "winnt.h"
#include "winver.h"
#include "imagehlp.h"
#include "psapi.h"
Include dependency graph for integrity.c:

Go to the source code of this file.

Macros

#define COBJMACROS
 
#define PSAPI_VERSION   1
 

Functions

static BOOL copy_dll_file (void)
 
static DWORD get_file_size (void)
 
static DWORD test_add_certificate (const char *cert_data, int len)
 
static void test_get_certificate (const char *cert_data, int index)
 
static void test_remove_certificate (int index)
 
static void test_pe_checksum (void)
 
 START_TEST (integrity)
 

Variables

static char test_dll_path [MAX_PATH]
 
static const char test_cert_data []
 
static const char test_cert_data_2 [] = {0xDE,0xAD,0xBE,0xEF,0x01,0x02,0x03}
 
static char test_pe_executable []
 

Macro Definition Documentation

◆ COBJMACROS

#define COBJMACROS

Definition at line 21 of file integrity.c.

◆ PSAPI_VERSION

#define PSAPI_VERSION   1

Definition at line 30 of file integrity.c.

Function Documentation

◆ copy_dll_file()

static BOOL copy_dll_file ( void  )
static

Definition at line 110 of file integrity.c.

111{
112 char sys_dir[MAX_PATH+15];
113 char temp_path[MAX_PATH];
114
115 if (GetSystemDirectoryA(sys_dir, MAX_PATH) == 0)
116 {
117 skip("Failed to get system directory. Skipping certificate/PE image tests.\n");
118 return FALSE;
119 }
120
121 if (sys_dir[lstrlenA(sys_dir) - 1] != '\\')
122 lstrcatA(sys_dir, "\\");
123
124 lstrcatA(sys_dir, "imagehlp.dll");
125
126 /* Copy DLL to a temp file */
129
130 if (CopyFileA(sys_dir, test_dll_path, FALSE) == 0)
131 {
132 skip("Unable to create copy of imagehlp.dll for tests.\n");
133 return FALSE;
134 }
135
136 return TRUE;
137}
#define skip(...)
Definition: atltest.h:64
BOOL WINAPI CopyFileA(IN LPCSTR lpExistingFileName, IN LPCSTR lpNewFileName, IN BOOL bFailIfExists)
Definition: copyansi.c:58
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define MAX_PATH
Definition: compat.h:34
DWORD WINAPI GetTempPathA(IN DWORD nBufferLength, OUT LPSTR lpBuffer)
Definition: path.c:2072
UINT WINAPI GetSystemDirectoryA(OUT LPSTR lpBuffer, IN UINT uSize)
Definition: path.c:2301
UINT WINAPI GetTempFileNameA(IN LPCSTR lpPathName, IN LPCSTR lpPrefixString, IN UINT uUnique, OUT LPSTR lpTempFileName)
Definition: filename.c:26
LPSTR WINAPI lstrcatA(LPSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:123
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
static char test_dll_path[MAX_PATH]
Definition: integrity.c:33
char temp_path[MAX_PATH]
Definition: mspatcha.c:123

Referenced by START_TEST().

◆ get_file_size()

static DWORD get_file_size ( void  )
static

Definition at line 139 of file integrity.c.

140{
141 HANDLE file;
142 DWORD filesize = 0;
143
147 return 0;
148
149 filesize = GetFileSize(file, NULL);
151
152 return filesize;
153}
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:740
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
unsigned long DWORD
Definition: ntddk_ex.h:95
Definition: fci.c:123

Referenced by get_file_sizes_cab(), get_file_sizes_lz(), get_file_sizes_none(), START_TEST(), and test_SetupQuerySpaceRequiredOnDriveA().

◆ START_TEST()

START_TEST ( integrity  )

Definition at line 369 of file integrity.c.

370{
371 DWORD file_size, file_size_orig, first, second;
372
373 if (!copy_dll_file())
374 return;
375
376 file_size_orig = get_file_size();
377
381
383 ok(file_size == file_size_orig, "File size different after add and remove (old: %ld; new: %ld)\n", file_size_orig, file_size);
384
385 /* Try adding multiple certificates */
388 ok(second == first + 1, "got %ld %ld\n", first, second);
389
392
393 /* Remove the first one and verify the second certificate is intact */
395 second--;
397
399
401 ok(file_size == file_size_orig, "File size different after add and remove (old: %ld; new: %ld)\n", file_size_orig, file_size);
402
404
406}
#define ok(value,...)
Definition: atltest.h:57
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
const GLint * first
Definition: glext.h:5794
static void test_remove_certificate(int index)
Definition: integrity.c:233
static void test_get_certificate(const char *cert_data, int index)
Definition: integrity.c:195
static const char test_cert_data[]
Definition: integrity.c:35
static DWORD test_add_certificate(const char *cert_data, int len)
Definition: integrity.c:155
static DWORD get_file_size(void)
Definition: integrity.c:139
static void test_pe_checksum(void)
Definition: integrity.c:259
static BOOL copy_dll_file(void)
Definition: integrity.c:110
static const char test_cert_data_2[]
Definition: integrity.c:87
static unsigned int file_size
Definition: regtests2xml.c:47

◆ test_add_certificate()

static DWORD test_add_certificate ( const char *  cert_data,
int  len 
)
static

Definition at line 155 of file integrity.c.

156{
159 DWORD cert_len;
160 DWORD index;
161 BOOL ret;
162
164
166 {
167 skip("Unable to open %s, skipping test\n", test_dll_path);
168 return ~0;
169 }
170
171 cert_len = sizeof(WIN_CERTIFICATE) + len;
173
174 if (!cert)
175 {
176 skip("Unable to allocate memory, skipping test\n");
178 return ~0;
179 }
180
181 cert->dwLength = cert_len;
182 cert->wRevision = WIN_CERT_REVISION_1_0;
183 cert->wCertificateType = WIN_CERT_TYPE_PKCS_SIGNED_DATA;
184 CopyMemory(cert->bCertificate, cert_data, len);
185
187 ok(ret, "Unable to add certificate to image, error %lx\n", GetLastError());
188 trace("added cert index %ld\n", index);
189
192 return index;
193}
#define trace
Definition: atltest.h:70
#define index(s, c)
Definition: various.h:29
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
BOOL WINAPI ImageAddCertificate(HANDLE FileHandle, LPWIN_CERTIFICATE Certificate, PDWORD Index)
Definition: integrity.c:381
return ret
Definition: mutex.c:147
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint index
Definition: glext.h:6031
GLenum GLsizei len
Definition: glext.h:6722
#define CopyMemory
Definition: minwinbase.h:29
static BYTE cert[]
Definition: msg.c:1374
_In_ HANDLE hFile
Definition: mswsock.h:90
#define GENERIC_WRITE
Definition: nt_native.h:90
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WIN_CERT_REVISION_1_0
Definition: wintrust.h:622
#define WIN_CERT_TYPE_PKCS_SIGNED_DATA
Definition: wintrust.h:626

Referenced by START_TEST().

◆ test_get_certificate()

static void test_get_certificate ( const char *  cert_data,
int  index 
)
static

Definition at line 195 of file integrity.c.

196{
199 DWORD cert_len = 0;
200 DWORD err;
201 BOOL ret;
202
204
206 {
207 skip("Unable to open %s, skipping test\n", test_dll_path);
208 return;
209 }
210
212 err = GetLastError();
213
214 ok ((ret == FALSE) && (err == ERROR_INSUFFICIENT_BUFFER), "ImageGetCertificateData gave unexpected result; ret=%d / err=%lx\n", ret, err);
215
217
218 if (!cert)
219 {
220 skip("Unable to allocate memory, skipping test\n");
222 return;
223 }
224
226 ok(ret, "Unable to retrieve certificate; err=%lx\n", GetLastError());
227 ok(memcmp(cert->bCertificate, cert_data, cert_len - sizeof(WIN_CERTIFICATE)) == 0, "Certificate retrieved did not match original\n");
228
231}
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
BOOL WINAPI ImageGetCertificateData(HANDLE handle, DWORD Index, LPWIN_CERTIFICATE Certificate, PDWORD RequiredLength)
Definition: integrity.c:554
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2807
#define err(...)

Referenced by START_TEST().

◆ test_pe_checksum()

static void test_pe_checksum ( void  )
static

Definition at line 259 of file integrity.c.

260{
261 DWORD checksum_orig, checksum_new, checksum_correct;
265 char buffer[20];
266 BOOL ret_bool;
267
268 SetLastError(0xdeadbeef);
269 ret = CheckSumMappedFile(NULL, 0, &checksum_orig, &checksum_new);
270 ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
271 ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
272 "Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %lx\n", GetLastError());
273
274 SetLastError(0xdeadbeef);
275 ret = CheckSumMappedFile((void *)0xdeadbeef, 0, &checksum_orig, &checksum_new);
276 ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
277 ok(((GetLastError() == ERROR_INVALID_PARAMETER)||(GetLastError() == 0xdeadbeef)),
278 "Expected 0xdeadbeef (XP) or ERROR_INVALID_PARAMETER (Vista+), got %lx\n", GetLastError());
279
280 /* basic checksum tests */
281 memset(buffer, 0x11, sizeof(buffer));
282 checksum_orig = checksum_new = 0xdeadbeef;
283 ret = CheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
284 ok(ret == NULL, "Expected NULL, got %p\n", ret);
285 todo_wine ok(checksum_orig == 0, "Expected 0, got %lx\n", checksum_orig);
286 todo_wine ok(checksum_new == 0xaabe, "Expected 0xaabe, got %lx\n", checksum_new);
287
288 memset(buffer, 0x22, sizeof(buffer));
289 checksum_orig = checksum_new = 0xdeadbeef;
290 ret = CheckSumMappedFile(buffer, sizeof(buffer), &checksum_orig, &checksum_new);
291 ok(ret == NULL, "Expected NULL, got %p\n", ret);
292 todo_wine ok(checksum_orig == 0, "Expected 0, got %lx\n", checksum_orig);
293 todo_wine ok(checksum_new == 0x5569, "Expected 0x5569, got %lx\n", checksum_new);
294
295 memset(buffer, 0x22, sizeof(buffer));
296 checksum_orig = checksum_new = 0xdeadbeef;
297 ret = CheckSumMappedFile(buffer, 10, &checksum_orig, &checksum_new);
298 ok(ret == NULL, "Expected NULL, got %p\n", ret);
299 todo_wine ok(checksum_orig == 0, "Expected 0, got %lx\n", checksum_orig);
300 todo_wine ok(checksum_new == 0xaab4, "Expected 0xaab4, got %lx\n", checksum_new);
301
302 memset(buffer, 0x22, sizeof(buffer));
303 checksum_orig = checksum_new = 0xdeadbeef;
304 ret = CheckSumMappedFile(buffer, 11, &checksum_orig, &checksum_new);
305 ok(ret == NULL, "Expected NULL, got %p\n", ret);
306 todo_wine ok(checksum_orig == 0, "Expected 0, got %lx\n", checksum_orig);
307 todo_wine ok(checksum_new == 0xaad7, "Expected 0xaad7, got %lx\n", checksum_new);
308
309 /* test checksum of PE module */
310 checksum_orig = checksum_new = 0xdeadbeef;
312 &checksum_orig, &checksum_new);
313 ok((char *)ret == test_pe_executable + 0x80, "Expected %p, got %p\n", test_pe_executable + 0x80, ret);
314 ok(checksum_orig == 0xabcdef11, "Expected 0xabcdef11, got %lx\n", checksum_orig);
315 ok(checksum_new == 0xaa4, "Expected 0xaa4, got %lx\n", checksum_new);
316
318 &modinfo, sizeof(modinfo));
319 ok(ret_bool, "GetModuleInformation failed, error: %lx\n", GetLastError());
320
321 SetLastError(0xdeadbeef);
322 checksum_orig = checksum_new = 0xdeadbeef;
323 ret = CheckSumMappedFile(modinfo.lpBaseOfDll, modinfo.SizeOfImage, &checksum_orig, &checksum_new);
324 ok(ret != NULL, "Expected CheckSumMappedFile to succeed\n");
325 ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %lx\n", GetLastError());
326 ok(checksum_orig != 0xdeadbeef, "Expected orig checksum != 0xdeadbeef\n");
327 ok(checksum_new != 0xdeadbeef, "Expected new checksum != 0xdeadbeef\n");
328
329 SetLastError(0xdeadbeef);
330 checksum_orig = checksum_new = 0xdeadbeef;
331 ret = CheckSumMappedFile((char *)modinfo.lpBaseOfDll + 100, modinfo.SizeOfImage - 100,
332 &checksum_orig, &checksum_new);
333 ok(!ret, "Expected CheckSumMappedFile to fail, got %p\n", ret);
334 ok(GetLastError() == 0xdeadbeef, "Expected err=0xdeadbeef, got %lx\n", GetLastError());
335 todo_wine ok(checksum_orig == 0, "Expected 0, got %lx\n", checksum_orig);
336 todo_wine ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %lx\n", checksum_new);
337
338 nt_header = ImageNtHeader( modinfo.lpBaseOfDll );
339 checksum_correct = nt_header->OptionalHeader.CheckSum;
340
341 checksum_orig = checksum_new = 0xdeadbeef;
342 ret = CheckSumMappedFile(modinfo.lpBaseOfDll, (char *)nt_header - (char *)modinfo.lpBaseOfDll,
343 &checksum_orig, &checksum_new);
344 ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
345 ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %lx, got %lx\n", checksum_correct, checksum_orig);
346 ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %lx\n", checksum_new);
347
348 checksum_orig = checksum_new = 0xdeadbeef;
349 ret = CheckSumMappedFile(modinfo.lpBaseOfDll, sizeof(IMAGE_DOS_HEADER),
350 &checksum_orig, &checksum_new);
351 ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
352 ok((checksum_orig == 0) || (checksum_orig == checksum_correct), "Expected %lx, got %lx\n", checksum_correct, checksum_orig);
353 ok(checksum_new != 0 && checksum_new != 0xdeadbeef, "Got unexpected value %lx\n", checksum_new);
354
355 checksum_orig = checksum_new = 0xdeadbeef;
356 ret = CheckSumMappedFile(modinfo.lpBaseOfDll, 0, &checksum_orig, &checksum_new);
357 ok(!ret || (ret == nt_header), "Expected CheckSumMappedFile to fail, got %p\n", ret);
358 ok((checksum_orig == 0xdeadbeef) || (checksum_orig == checksum_correct), "Expected %lx, got %lx\n", checksum_correct, checksum_orig);
359 ok((checksum_new == 0xdeadbeef) || (checksum_new != 0 && checksum_new != 0xdeadbeef), "Got unexpected value %lx\n", checksum_new);
360
361 checksum_orig = checksum_new = 0xdeadbeef;
362 ret = CheckSumMappedFile((char *)modinfo.lpBaseOfDll + 1, 0,
363 &checksum_orig, &checksum_new);
364 ok(ret == NULL, "Expected NULL, got %p\n", ret);
365 ok((checksum_orig == 0) || (checksum_orig == 0xdeadbeef), "Expected 0, got %lx\n", checksum_orig);
366 ok((checksum_new == 0) || (checksum_new == 0xdeadbeef), "Expected 0, got %lx\n", checksum_new);
367}
PIMAGE_NT_HEADERS WINAPI ImageNtHeader(_In_ PVOID)
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
#define GetCurrentProcess()
Definition: compat.h:759
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
BOOL WINAPI GetModuleInformation(HANDLE process, HMODULE module, MODULEINFO *modinfo, DWORD count)
Definition: debug.c:1503
GLuint buffer
Definition: glext.h:5915
PIMAGE_NT_HEADERS WINAPI CheckSumMappedFile(LPVOID BaseAddress, DWORD FileLength, LPDWORD HeaderSum, LPDWORD CheckSum)
Definition: modify.c:164
#define todo_wine
Definition: minitest.h:80
static IMAGE_NT_HEADERS32 nt_header
Definition: data.c:36
static char test_pe_executable[]
Definition: integrity.c:89
static HMODULE MODULEINFO * modinfo
Definition: module.c:38
#define memset(x, y, z)
Definition: compat.h:39
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184

Referenced by START_TEST().

◆ test_remove_certificate()

static void test_remove_certificate ( int  index)
static

Definition at line 233 of file integrity.c.

234{
235 DWORD orig_count = 0, count = 0;
237 BOOL ret;
238
240
242 {
243 skip("Unable to open %s, skipping test\n", test_dll_path);
244 return;
245 }
246
248 ok (ret, "Unable to enumerate certificates in file; err=%lx\n", GetLastError());
250 ok (ret, "Unable to remove certificate from file; err=%lx\n", GetLastError());
251
252 /* Test to see if the certificate has actually been removed */
254 ok (count == orig_count - 1, "Certificate count mismatch; orig=%ld new=%ld\n", orig_count, count);
255
257}
BOOL WINAPI ImageEnumerateCertificates(HANDLE handle, WORD TypeFilter, PDWORD CertificateCount, PDWORD Indices, DWORD IndexCount)
Definition: integrity.c:487
BOOL WINAPI ImageRemoveCertificate(HANDLE FileHandle, DWORD Index)
Definition: integrity.c:887
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define CERT_SECTION_TYPE_ANY
Definition: imagehlp.h:47

Referenced by START_TEST().

Variable Documentation

◆ test_cert_data

const char test_cert_data[]
static

Definition at line 35 of file integrity.c.

Referenced by START_TEST().

◆ test_cert_data_2

const char test_cert_data_2[] = {0xDE,0xAD,0xBE,0xEF,0x01,0x02,0x03}
static

Definition at line 87 of file integrity.c.

Referenced by START_TEST().

◆ test_dll_path

◆ test_pe_executable

char test_pe_executable[]
static
Initial value:
=
{
0x4d,0x5a,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xff,0xff,0x00,
0x00,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x00,0x00,0x00,0x0e,0x1f,0xba,0x0e,0x00,0xb4,0x09,0xcd,0x21,0xb8,0x01,
0x4c,0xcd,0x21,0x54,0x68,0x69,0x73,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,
0x20,0x63,0x61,0x6e,0x6e,0x6f,0x74,0x20,0x62,0x65,0x20,0x72,0x75,0x6e,0x20,
0x69,0x6e,0x20,0x44,0x4f,0x53,0x20,0x6d,0x6f,0x64,0x65,0x2e,0x0d,0x0d,0x0a,
0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x45,0x00,0x00,0x4c,0x01,0x0f,
0x00,0xfd,0x38,0xc9,0x55,0x00,0x24,0x01,0x00,0xea,0x04,0x00,0x00,0xe0,0x00,
0x07,0x01,0x0b,0x01,0x02,0x18,0x00,0x1a,0x00,0x00,0x00,0x2c,0x00,0x00,0x00,
0x06,0x00,0x00,0xe0,0x14,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x30,0x00,0x00,
0x00,0x00,0x40,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x04,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,
0x01,0x00,0x00,0x04,0x00,0x00, 0x11,0xEF,0xCD,0xAB,0x03,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00
}

Definition at line 89 of file integrity.c.

Referenced by test_pe_checksum().