1 /* Unit test suite for wintrust crypt functions
3 * Copyright 2007 Paul Vriens
4 * Copyright 2008 Hans Leidekker for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
30 static char selfname[MAX_PATH];
31 static CHAR CURR_DIR[MAX_PATH];
32 static CHAR catroot[MAX_PATH];
33 static CHAR catroot2[MAX_PATH];
35 static const WCHAR hashmeW[] = {'h','a','s','h','m','e',0};
36 static const WCHAR attr1W[] = {'a','t','t','r','1',0};
37 static const WCHAR attr2W[] = {'a','t','t','r','2',0};
40 * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
41 * convert to DOS line endings and run 'makecat /cat winetest.cdf'
48 PublicVersion=0x00000001
50 CATATTR1=0x10010001:attr1:value1
51 CATATTR2=0x10010001:attr2:value2
57 static const CHAR test_cdf[] =
59 "Name=winetest.cat\r\n"
61 "PublicVersion=0x00000001\r\n"
63 "CATATTR1=0x10010001:attr1:value1\r\n"
64 "CATATTR2=0x10010001:attr2:value2\r\n"
67 "hashme=.\\winetest.cdf\r\n";
69 static const BYTE test_catalog[] = {
70 0x30, 0x82, 0x01, 0xbc, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
71 0x82, 0x01, 0xad, 0x30, 0x82, 0x01, 0xa9, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x9e,
72 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0a, 0x01, 0xa0, 0x82, 0x01, 0x8f, 0x30,
73 0x82, 0x01, 0x8b, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01,
74 0x01, 0x04, 0x10, 0xfa, 0x55, 0x2c, 0xc2, 0xf6, 0xcc, 0xdd, 0x11, 0x2a, 0x9c, 0x00, 0x14, 0x22,
75 0xec, 0x8f, 0x3b, 0x17, 0x0d, 0x30, 0x38, 0x31, 0x32, 0x31, 0x38, 0x31, 0x31, 0x32, 0x36, 0x34,
76 0x38, 0x5a, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01, 0x02,
77 0x05, 0x00, 0x30, 0x81, 0xdd, 0x30, 0x81, 0xda, 0x04, 0x0e, 0x68, 0x00, 0x61, 0x00, 0x73, 0x00,
78 0x68, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x00, 0x00, 0x31, 0x81, 0xc7, 0x30, 0x61, 0x06, 0x0a, 0x2b,
79 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x53, 0x30, 0x51, 0x30, 0x2c, 0x06,
80 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xa2, 0x1e, 0x80, 0x1c, 0x00,
81 0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c, 0x00,
82 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e, 0x30, 0x21, 0x30, 0x09, 0x06,
83 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xed, 0xd6, 0x9c, 0x9c, 0xb2, 0xfc,
84 0xaa, 0x03, 0xe8, 0xd3, 0x20, 0xf6, 0xab, 0x28, 0xc3, 0xff, 0xbd, 0x07, 0x36, 0xf5, 0x30, 0x62,
85 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52,
86 0x1e, 0x4c, 0x00, 0x7b, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41,
87 0x00, 0x34, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2d,
88 0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34,
89 0x00, 0x37, 0x00, 0x2d, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46,
90 0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7d, 0x02, 0x02,
91 0x02, 0x00, 0xa0, 0x6a, 0x30, 0x68, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
92 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61, 0x00, 0x74, 0x00, 0x74,
93 0x00, 0x72, 0x00, 0x32, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e, 0x76, 0x00, 0x61, 0x00,
94 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x32, 0x00, 0x00, 0x00, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06,
95 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61,
96 0x00, 0x74, 0x00, 0x74, 0x00, 0x72, 0x00, 0x31, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e,
97 0x76, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x31, 0x00, 0x00, 0x00, 0x31, 0x00,
100 static BOOL (WINAPI * pCryptCATAdminAcquireContext)(HCATADMIN*, const GUID*, DWORD);
101 static BOOL (WINAPI * pCryptCATAdminReleaseContext)(HCATADMIN, DWORD);
102 static BOOL (WINAPI * pCryptCATAdminCalcHashFromFileHandle)(HANDLE hFile, DWORD*, BYTE*, DWORD);
103 static HCATINFO (WINAPI * pCryptCATAdminAddCatalog)(HCATADMIN, PWSTR, PWSTR, DWORD);
104 static BOOL (WINAPI * pCryptCATAdminRemoveCatalog)(HCATADMIN, LPCWSTR, DWORD);
105 static BOOL (WINAPI * pCryptCATAdminReleaseCatalogContext)(HCATADMIN, HCATINFO, DWORD);
106 static HANDLE (WINAPI * pCryptCATOpen)(LPWSTR, DWORD, HCRYPTPROV, DWORD, DWORD);
107 static BOOL (WINAPI * pCryptCATCatalogInfoFromContext)(HCATINFO, CATALOG_INFO *, DWORD);
108 static BOOL (WINAPI * pCryptCATCDFClose)(CRYPTCATCDF *);
109 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATCDFEnumCatAttributes)(CRYPTCATCDF *, CRYPTCATATTRIBUTE *,
110 PFN_CDF_PARSE_ERROR_CALLBACK);
111 static LPWSTR (WINAPI * pCryptCATCDFEnumMembersByCDFTagEx)(CRYPTCATCDF *, LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK,
112 CRYPTCATMEMBER **, BOOL, LPVOID);
113 static CRYPTCATCDF * (WINAPI * pCryptCATCDFOpen)(LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK);
114 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateCatAttr)(HANDLE, CRYPTCATATTRIBUTE *);
115 static CRYPTCATMEMBER * (WINAPI * pCryptCATEnumerateMember)(HANDLE, CRYPTCATMEMBER *);
116 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateAttr)(HANDLE, CRYPTCATMEMBER *, CRYPTCATATTRIBUTE *);
117 static BOOL (WINAPI * pCryptCATClose)(HANDLE);
119 static void InitFunctionPtrs(void)
121 HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
123 #define WINTRUST_GET_PROC(func) \
124 p ## func = (void*)GetProcAddress(hWintrust, #func); \
126 trace("GetProcAddress(%s) failed\n", #func); \
129 WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
130 WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
131 WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
132 WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
133 WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
134 WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
135 WINTRUST_GET_PROC(CryptCATOpen)
136 WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
137 WINTRUST_GET_PROC(CryptCATCDFClose)
138 WINTRUST_GET_PROC(CryptCATCDFEnumCatAttributes)
139 WINTRUST_GET_PROC(CryptCATCDFEnumMembersByCDFTagEx)
140 WINTRUST_GET_PROC(CryptCATCDFOpen)
141 WINTRUST_GET_PROC(CryptCATEnumerateCatAttr)
142 WINTRUST_GET_PROC(CryptCATEnumerateMember)
143 WINTRUST_GET_PROC(CryptCATEnumerateAttr)
144 WINTRUST_GET_PROC(CryptCATClose)
146 #undef WINTRUST_GET_PROC
149 static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
151 static void test_context(void)
155 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
156 CHAR dummydir[MAX_PATH];
159 /* When CryptCATAdminAcquireContext is successful it will create
160 * several directories if they don't exist:
162 * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
163 * ...\system32\CatRoot2\{GUID} (WinXP and up), here we find the catalog database for that GUID
165 * Windows Vista uses lowercase catroot and catroot2.
167 * When passed a NULL GUID it will create the following directories although on
168 * WinXP and up these directories are already present when Windows is installed:
170 * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
171 * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
173 * TODO: Find out what this GUID is/does.
175 * On WinXP and up there is also a TimeStamp file in some of directories that
176 * seem to indicate the last change to the catalog database for that GUID.
178 * On Windows 2000 some files are created/updated:
180 * ...\system32\CatRoot\SYSMAST.cbk
181 * ...\system32\CatRoot\SYSMAST.cbd
182 * ...\system32\CatRoot\{GUID}\CATMAST.cbk
183 * ...\system32\CatRoot\{GUID}\CATMAST.cbd
188 SetLastError(0xdeadbeef);
189 ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
190 ok(!ret, "Expected failure\n");
191 ok(GetLastError() == ERROR_INVALID_PARAMETER,
192 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
195 ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
196 ok(ret, "Expected success\n");
197 ok(hca != NULL, "Expected a context handle, got NULL\n");
200 SetLastError(0xdeadbeef);
201 ret = pCryptCATAdminReleaseContext(NULL, 0);
202 ok(!ret, "Expected failure\n");
203 ok(GetLastError() == ERROR_INVALID_PARAMETER,
204 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
207 SetLastError(0xdeadbeef);
208 ret = pCryptCATAdminReleaseContext(hca, 0);
209 ok(ret, "Expected success\n");
210 ok(GetLastError() == 0xdeadbeef,
211 "Expected no change in last error, got %d\n", GetLastError());
213 /* Try to release a second time */
214 SetLastError(0xdeadbeef);
215 ret = pCryptCATAdminReleaseContext(hca, 0);
216 ok(!ret, "Expected failure\n");
217 ok(GetLastError() == ERROR_INVALID_PARAMETER,
218 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
220 /* NULL context handle and dummy GUID */
221 SetLastError(0xdeadbeef);
222 ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
223 ok(!ret, "Expected failure\n");
224 ok(GetLastError() == ERROR_INVALID_PARAMETER,
225 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
227 /* Correct context handle and dummy GUID
229 * The tests run in the past unfortunately made sure that some directories were created.
232 * We don't want to mess too much with these for now so we should delete only the ones
233 * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
234 * save to remove files and directories from CatRoot/CatRoot2.
237 ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
238 ok(ret, "Expected success\n");
239 ok(hca != NULL, "Expected a context handle, got NULL\n");
241 attrs = GetFileAttributes(catroot);
242 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
244 /* Windows creates the GUID directory in capitals */
245 lstrcpyA(dummydir, catroot);
246 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
247 attrs = GetFileAttributes(dummydir);
248 ok(attrs != INVALID_FILE_ATTRIBUTES,
249 "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
251 /* Only present on XP or higher. */
252 attrs = GetFileAttributes(catroot2);
253 if (attrs != INVALID_FILE_ATTRIBUTES)
255 lstrcpyA(dummydir, catroot2);
256 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
257 attrs = GetFileAttributes(dummydir);
258 ok(attrs != INVALID_FILE_ATTRIBUTES,
259 "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
262 ret = pCryptCATAdminReleaseContext(hca, 0);
263 ok(ret, "Expected success\n");
265 /* Correct context handle and GUID */
266 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
267 ok(ret, "Expected success\n");
268 ok(hca != NULL, "Expected a context handle, got NULL\n");
270 ret = pCryptCATAdminReleaseContext(hca, 0);
271 ok(ret, "Expected success\n");
273 /* Flags not equal to 0 */
274 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
275 ok(ret, "Expected success\n");
276 ok(hca != NULL, "Expected a context handle, got NULL\n");
278 ret = pCryptCATAdminReleaseContext(hca, 0);
279 ok(ret, "Expected success\n");
282 /* TODO: Check whether SHA-1 is the algorithm that's always used */
283 static void test_calchash(void)
289 BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
294 SetLastError(0xdeadbeef);
295 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
296 ok(!ret, "Expected failure\n");
297 ok(GetLastError() == ERROR_INVALID_PARAMETER,
298 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
300 /* NULL filehandle, rest is legal */
301 SetLastError(0xdeadbeef);
302 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
303 ok(!ret, "Expected failure\n");
304 ok(GetLastError() == ERROR_INVALID_PARAMETER,
305 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
307 /* Correct filehandle, rest is NULL */
308 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
309 SetLastError(0xdeadbeef);
310 ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
311 ok(!ret, "Expected failure\n");
312 ok(GetLastError() == ERROR_INVALID_PARAMETER,
313 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
316 /* All OK, but dwFlags set to 1 */
317 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
318 SetLastError(0xdeadbeef);
319 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
320 ok(!ret, "Expected failure\n");
321 ok(GetLastError() == ERROR_INVALID_PARAMETER,
322 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
325 /* All OK, requesting the size of the hash */
326 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
327 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
328 SetLastError(0xdeadbeef);
329 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
330 ok(ret, "Expected success %u\n", GetLastError());
331 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
332 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
333 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
336 /* All OK, retrieve the hash
337 * Double the hash buffer to see what happens to the size parameter
339 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
341 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
342 SetLastError(0xdeadbeef);
343 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
344 ok(ret, "Expected success %u\n", GetLastError());
345 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
346 ok(GetLastError() == ERROR_SUCCESS,
347 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
349 HeapFree(GetProcessHeap(), 0, hash);
351 /* Do the same test with a file created and filled by ourselves (and we thus
352 * have a known hash for).
354 GetTempFileNameA(CURR_DIR, "hsh", 0, temp);
355 file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
356 WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
359 /* All OK, first request the size and then retrieve the hash */
360 file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
362 pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
363 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
364 SetLastError(0xdeadbeef);
365 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
366 ok(ret, "Expected success\n");
367 ok(GetLastError() == ERROR_SUCCESS,
368 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
369 ok(hashsize == sizeof(expectedhash) &&
370 !memcmp(hash, expectedhash, sizeof(expectedhash)),
371 "Hashes didn't match\n");
374 HeapFree(GetProcessHeap(), 0, hash);
378 static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
382 static void test_CryptCATCDF_params(void)
384 static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
385 static CHAR cdffileA[] = "tempfile.cdf";
386 static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
391 if (!pCryptCATCDFOpen)
393 win_skip("CryptCATCDFOpen is not available\n");
397 SetLastError(0xdeadbeef);
398 catcdf = pCryptCATCDFOpen(NULL, NULL);
399 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
401 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
403 SetLastError(0xdeadbeef);
404 catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
405 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
407 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
409 /* File doesn't exist */
410 SetLastError(0xdeadbeef);
411 catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
412 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
414 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
417 DeleteFileA(cdffileA);
418 file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
419 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
422 SetLastError(0xdeadbeef);
423 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
424 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
426 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
427 DeleteFileA(cdffileA);
429 SetLastError(0xdeadbeef);
430 ret = pCryptCATCDFClose(NULL);
431 ok(!ret, "Expected failure\n");
433 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
436 SetLastError(0xdeadbeef);
437 ret = pCryptCATCDFClose(catcdf);
438 ok(!ret, "Expected failure\n");
440 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
443 /* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
444 static void test_CryptCATAdminAddRemoveCatalog(void)
446 static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
447 static CHAR basename[] = "winetest.cat";
451 WCHAR tmpfileW[MAX_PATH];
452 char tmpfile[MAX_PATH];
453 char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
454 WCHAR catfileW[MAX_PATH];
456 DWORD error, written;
460 if (!pCryptCATAdminRemoveCatalog)
462 /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
463 win_skip("CryptCATAdminRemoveCatalog is not available\n");
467 if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
468 DeleteFileA(tmpfile);
469 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
470 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
473 ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
474 ok(ret, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
476 SetLastError(0xdeadbeef);
477 hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
478 error = GetLastError();
479 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
480 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMTER\n", GetLastError());
482 SetLastError(0xdeadbeef);
483 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
484 error = GetLastError();
485 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
486 ok(error == ERROR_INVALID_PARAMETER, "got %u expected INVALID_PARAMTER\n", GetLastError());
488 MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);
490 SetLastError(0xdeadbeef);
491 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
492 error = GetLastError();
494 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
495 ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
498 SetLastError(0xdeadbeef);
499 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
500 error = GetLastError();
501 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
502 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMTER\n", GetLastError());
504 SetLastError(0xdeadbeef);
505 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
506 error = GetLastError();
507 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
508 todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
510 DeleteFileA(tmpfile);
511 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
512 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
513 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
516 /* Unique name will be created */
517 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
518 todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
520 info.cbStruct = sizeof(info);
521 info.wszCatalogFile[0] = 0;
522 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
525 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
526 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
528 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
529 if ((p = strrchr(catfile, '\\'))) p++;
530 memset(catfileW, 0, sizeof(catfileW));
531 MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);
533 /* winetest.cat will be created */
534 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
535 ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
537 lstrcpyA(catfilepath, catroot);
538 lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
539 attrs = GetFileAttributes(catfilepath);
540 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
542 info.cbStruct = sizeof(info);
543 info.wszCatalogFile[0] = 0;
544 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
545 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
546 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
547 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
548 if ((p = strrchr(catfile, '\\'))) p++;
549 ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
551 ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
552 ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
554 /* Remove the catalog file with the unique name */
555 ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
556 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
558 /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
559 * according to MSDN */
560 ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
561 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
562 /* The call succeeded with the full path but the file is not removed */
563 attrs = GetFileAttributes(catfilepath);
564 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
565 /* Given only the filename the file is removed */
566 ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
567 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
568 attrs = GetFileAttributes(catfilepath);
569 ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
571 ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
572 ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
574 DeleteFileA(tmpfile);
577 static void test_catalog_properties(CHAR *catfile, int attributes, int members)
579 static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
583 CRYPTCATATTRIBUTE *attr;
584 char catalog[MAX_PATH];
585 WCHAR catalogW[MAX_PATH];
588 int attrcount = 0, membercount = 0;
590 /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
593 attrs = GetFileAttributesA(catfile);
594 if (attrs == INVALID_FILE_ATTRIBUTES)
599 trace("Creating the catalog file\n");
600 if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
601 file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
602 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
603 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
608 MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
612 MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
616 hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
617 ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
619 hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
620 ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
622 m = pCryptCATEnumerateMember(NULL, NULL);
623 ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
626 while ((m = pCryptCATEnumerateMember(hcat, m)))
628 ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
629 todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
630 ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
631 ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
632 ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
633 ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
634 ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
636 attr = pCryptCATEnumerateAttr(hcat, m, NULL);
637 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
641 ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
643 attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
644 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
646 attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
647 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
650 while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
652 ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
653 !lstrcmpW(attr->pwszReferenceTag, attr2W),
654 "Expected 'attr1' or 'attr2'\n");
659 ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
661 ret = pCryptCATClose(hcat);
662 ok(ret, "CryptCATClose failed\n");
663 if (catalog[0]) DeleteFileA( catalog );
666 static void test_create_catalog_file(void)
668 static CHAR catfileA[] = "winetest.cat";
669 static CHAR cdffileA[] = "winetest.cdf";
670 static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
672 CRYPTCATATTRIBUTE *catattr;
673 CRYPTCATMEMBER *catmember;
675 DWORD written, attrs;
678 int attrcount, membercount;
680 if (!pCryptCATCDFOpen)
682 win_skip("CryptCATCDFOpen is not available\n");
686 /* Create the cdf file */
687 file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
688 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
689 WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
692 /* Don't enumerate attributes and members */
693 trace("No attribs and members\n");
694 SetLastError(0xdeadbeef);
695 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
698 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
699 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
702 ret = pCryptCATCDFClose(catcdf);
705 ok(ret, "Expected success\n");
706 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
709 attrs = GetFileAttributesA(catfileA);
711 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
713 test_catalog_properties(catfileA, 0, 0);
714 DeleteFileA(catfileA);
716 /* Only enumerate the attributes */
717 trace("Only attributes\n");
718 attrcount = membercount = 0;
719 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
722 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
724 ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
725 !lstrcmpW(catattr->pwszReferenceTag, attr2W),
726 "Expected 'attr1' or 'attr2'\n");
731 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
733 pCryptCATCDFClose(catcdf);
734 /* Eventhough the resulting catalogfile shows the attributes, they will not be enumerated */
735 test_catalog_properties(catfileA, 0, 0);
736 DeleteFileA(catfileA);
738 /* Only enumerate the members */
739 trace("Only members\n");
740 attrcount = membercount = 0;
741 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
745 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
747 ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
751 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
753 pCryptCATCDFClose(catcdf);
754 test_catalog_properties(catfileA, 0, 1);
755 DeleteFileA(catfileA);
757 /* Enumerate members and attributes */
758 trace("Attributes and members\n");
759 attrcount = membercount = 0;
760 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
763 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
766 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
770 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
773 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
775 pCryptCATCDFClose(catcdf);
776 test_catalog_properties(catfileA, 2, 1);
777 DeleteFileA(catfileA);
779 DeleteFileA(cdffileA);
787 char windir[MAX_PATH];
791 if (!pCryptCATAdminAcquireContext)
793 win_skip("CryptCATAdmin functions are not available\n");
797 GetWindowsDirectoryA(windir, MAX_PATH);
798 lstrcpyA(catroot, windir);
799 lstrcatA(catroot, "\\system32\\CatRoot");
800 lstrcpyA(catroot2, windir);
801 lstrcatA(catroot2, "\\system32\\CatRoot2");
803 myARGC = winetest_get_mainargs(&myARGV);
804 strcpy(selfname, myARGV[0]);
806 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
810 /* Parameter checking only */
811 test_CryptCATCDF_params();
812 /* Create a catalogfile out of our own catalog definition file */
813 test_create_catalog_file();
814 test_CryptCATAdminAddRemoveCatalog();