mshtml: Wine Gecko 2.34-beta2 release.
[wine/wine-gecko.git] / dlls / wintrust / tests / crypt.c
blob0e77f931b795f1d8a10cb1e8ce36f6968c54b8bf
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
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "windows.h"
26 #include "wincrypt.h"
27 #include "mscat.h"
29 #include "wine/test.h"
31 static char selfname[MAX_PATH];
32 static CHAR CURR_DIR[MAX_PATH];
33 static CHAR catroot[MAX_PATH];
34 static CHAR catroot2[MAX_PATH];
36 static const WCHAR hashmeW[] = {'h','a','s','h','m','e',0};
37 static const WCHAR attr1W[] = {'a','t','t','r','1',0};
38 static const WCHAR attr2W[] = {'a','t','t','r','2',0};
41 * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
42 * convert to DOS line endings and run 'makecat /cat winetest.cdf'
46 [CatalogHeader]
47 Name=winetest.cat
48 ResultDir=.\
49 PublicVersion=0x00000001
50 EncodingType=
51 CATATTR1=0x10010001:attr1:value1
52 CATATTR2=0x10010001:attr2:value2
54 [CatalogFiles]
55 hashme=.\winetest.cdf
58 static const CHAR test_cdf[] =
59 "[CatalogHeader]\r\n"
60 "Name=winetest.cat\r\n"
61 "ResultDir=.\\\r\n"
62 "PublicVersion=0x00000001\r\n"
63 "EncodingType=\r\n"
64 "CATATTR1=0x10010001:attr1:value1\r\n"
65 "CATATTR2=0x10010001:attr2:value2\r\n"
66 "\r\n"
67 "[CatalogFiles]\r\n"
68 "hashme=.\\winetest.cdf\r\n";
70 static const BYTE test_catalog[] = {
71 0x30, 0x82, 0x01, 0xbc, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
72 0x82, 0x01, 0xad, 0x30, 0x82, 0x01, 0xa9, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x82, 0x01, 0x9e,
73 0x06, 0x09, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0a, 0x01, 0xa0, 0x82, 0x01, 0x8f, 0x30,
74 0x82, 0x01, 0x8b, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01,
75 0x01, 0x04, 0x10, 0xfa, 0x55, 0x2c, 0xc2, 0xf6, 0xcc, 0xdd, 0x11, 0x2a, 0x9c, 0x00, 0x14, 0x22,
76 0xec, 0x8f, 0x3b, 0x17, 0x0d, 0x30, 0x38, 0x31, 0x32, 0x31, 0x38, 0x31, 0x31, 0x32, 0x36, 0x34,
77 0x38, 0x5a, 0x30, 0x0e, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x01, 0x02,
78 0x05, 0x00, 0x30, 0x81, 0xdd, 0x30, 0x81, 0xda, 0x04, 0x0e, 0x68, 0x00, 0x61, 0x00, 0x73, 0x00,
79 0x68, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x00, 0x00, 0x31, 0x81, 0xc7, 0x30, 0x61, 0x06, 0x0a, 0x2b,
80 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x04, 0x31, 0x53, 0x30, 0x51, 0x30, 0x2c, 0x06,
81 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x01, 0x19, 0xa2, 0x1e, 0x80, 0x1c, 0x00,
82 0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c, 0x00,
83 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e, 0x30, 0x21, 0x30, 0x09, 0x06,
84 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xed, 0xd6, 0x9c, 0x9c, 0xb2, 0xfc,
85 0xaa, 0x03, 0xe8, 0xd3, 0x20, 0xf6, 0xab, 0x28, 0xc3, 0xff, 0xbd, 0x07, 0x36, 0xf5, 0x30, 0x62,
86 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x02, 0x31, 0x54, 0x30, 0x52,
87 0x1e, 0x4c, 0x00, 0x7b, 0x00, 0x44, 0x00, 0x45, 0x00, 0x33, 0x00, 0x35, 0x00, 0x31, 0x00, 0x41,
88 0x00, 0x34, 0x00, 0x32, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x45, 0x00, 0x35, 0x00, 0x39, 0x00, 0x2d,
89 0x00, 0x31, 0x00, 0x31, 0x00, 0x44, 0x00, 0x30, 0x00, 0x2d, 0x00, 0x38, 0x00, 0x43, 0x00, 0x34,
90 0x00, 0x37, 0x00, 0x2d, 0x00, 0x30, 0x00, 0x30, 0x00, 0x43, 0x00, 0x30, 0x00, 0x34, 0x00, 0x46,
91 0x00, 0x43, 0x00, 0x32, 0x00, 0x39, 0x00, 0x35, 0x00, 0x45, 0x00, 0x45, 0x00, 0x7d, 0x02, 0x02,
92 0x02, 0x00, 0xa0, 0x6a, 0x30, 0x68, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
93 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61, 0x00, 0x74, 0x00, 0x74,
94 0x00, 0x72, 0x00, 0x32, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e, 0x76, 0x00, 0x61, 0x00,
95 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x32, 0x00, 0x00, 0x00, 0x30, 0x32, 0x06, 0x0a, 0x2b, 0x06,
96 0x01, 0x04, 0x01, 0x82, 0x37, 0x0c, 0x02, 0x01, 0x04, 0x24, 0x30, 0x22, 0x1e, 0x0a, 0x00, 0x61,
97 0x00, 0x74, 0x00, 0x74, 0x00, 0x72, 0x00, 0x31, 0x02, 0x04, 0x10, 0x01, 0x00, 0x01, 0x04, 0x0e,
98 0x76, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x75, 0x00, 0x65, 0x00, 0x31, 0x00, 0x00, 0x00, 0x31, 0x00,
101 static BOOL (WINAPI * pCryptCATAdminAcquireContext)(HCATADMIN*, const GUID*, DWORD);
102 static BOOL (WINAPI * pCryptCATAdminReleaseContext)(HCATADMIN, DWORD);
103 static BOOL (WINAPI * pCryptCATAdminCalcHashFromFileHandle)(HANDLE hFile, DWORD*, BYTE*, DWORD);
104 static HCATINFO (WINAPI * pCryptCATAdminAddCatalog)(HCATADMIN, PWSTR, PWSTR, DWORD);
105 static BOOL (WINAPI * pCryptCATAdminRemoveCatalog)(HCATADMIN, LPCWSTR, DWORD);
106 static BOOL (WINAPI * pCryptCATAdminReleaseCatalogContext)(HCATADMIN, HCATINFO, DWORD);
107 static HANDLE (WINAPI * pCryptCATOpen)(LPWSTR, DWORD, HCRYPTPROV, DWORD, DWORD);
108 static BOOL (WINAPI * pCryptCATCatalogInfoFromContext)(HCATINFO, CATALOG_INFO *, DWORD);
109 static BOOL (WINAPI * pCryptCATCDFClose)(CRYPTCATCDF *);
110 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATCDFEnumCatAttributes)(CRYPTCATCDF *, CRYPTCATATTRIBUTE *,
111 PFN_CDF_PARSE_ERROR_CALLBACK);
112 static LPWSTR (WINAPI * pCryptCATCDFEnumMembersByCDFTagEx)(CRYPTCATCDF *, LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK,
113 CRYPTCATMEMBER **, BOOL, LPVOID);
114 static CRYPTCATCDF * (WINAPI * pCryptCATCDFOpen)(LPWSTR, PFN_CDF_PARSE_ERROR_CALLBACK);
115 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateCatAttr)(HANDLE, CRYPTCATATTRIBUTE *);
116 static CRYPTCATMEMBER * (WINAPI * pCryptCATEnumerateMember)(HANDLE, CRYPTCATMEMBER *);
117 static CRYPTCATATTRIBUTE * (WINAPI * pCryptCATEnumerateAttr)(HANDLE, CRYPTCATMEMBER *, CRYPTCATATTRIBUTE *);
118 static BOOL (WINAPI * pCryptCATClose)(HANDLE);
119 static pCryptSIPGetSignedDataMsg pGetSignedDataMsg;
120 static pCryptSIPPutSignedDataMsg pPutSignedDataMsg;
122 static void InitFunctionPtrs(void)
124 HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
126 #define WINTRUST_GET_PROC(func) \
127 p ## func = (void*)GetProcAddress(hWintrust, #func); \
128 if(!p ## func) { \
129 trace("GetProcAddress(%s) failed\n", #func); \
132 WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
133 WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
134 WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
135 WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
136 WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
137 WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
138 WINTRUST_GET_PROC(CryptCATOpen)
139 WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
140 WINTRUST_GET_PROC(CryptCATCDFClose)
141 WINTRUST_GET_PROC(CryptCATCDFEnumCatAttributes)
142 WINTRUST_GET_PROC(CryptCATCDFEnumMembersByCDFTagEx)
143 WINTRUST_GET_PROC(CryptCATCDFOpen)
144 WINTRUST_GET_PROC(CryptCATEnumerateCatAttr)
145 WINTRUST_GET_PROC(CryptCATEnumerateMember)
146 WINTRUST_GET_PROC(CryptCATEnumerateAttr)
147 WINTRUST_GET_PROC(CryptCATClose)
149 #undef WINTRUST_GET_PROC
151 pGetSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPGetSignedDataMsg");
152 if(!pGetSignedDataMsg)
153 trace("GetProcAddress(CryptSIPGetSignedDataMsg) failed\n");
155 pPutSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPPutSignedDataMsg");
156 if(!pPutSignedDataMsg)
157 trace("GetProcAddress(CryptSIPPutSignedDataMsg) failed\n");
160 static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
162 static void test_context(void)
164 BOOL ret;
165 HCATADMIN hca;
166 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
167 CHAR dummydir[MAX_PATH];
168 DWORD attrs;
170 /* When CryptCATAdminAcquireContext is successful it will create
171 * several directories if they don't exist:
173 * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
174 * ...\system32\CatRoot2\{GUID} (WinXP and up), here we find the catalog database for that GUID
176 * Windows Vista uses lowercase catroot and catroot2.
178 * When passed a NULL GUID it will create the following directories although on
179 * WinXP and up these directories are already present when Windows is installed:
181 * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
182 * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
184 * TODO: Find out what this GUID is/does.
186 * On WinXP and up there is also a TimeStamp file in some of directories that
187 * seem to indicate the last change to the catalog database for that GUID.
189 * On Windows 2000 some files are created/updated:
191 * ...\system32\CatRoot\SYSMAST.cbk
192 * ...\system32\CatRoot\SYSMAST.cbd
193 * ...\system32\CatRoot\{GUID}\CATMAST.cbk
194 * ...\system32\CatRoot\{GUID}\CATMAST.cbd
198 /* All NULL */
199 SetLastError(0xdeadbeef);
200 ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
201 ok(!ret, "Expected failure\n");
202 ok(GetLastError() == ERROR_INVALID_PARAMETER,
203 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
205 /* NULL GUID */
206 ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
207 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
208 ok(hca != NULL, "Expected a context handle, got NULL\n");
210 /* All NULL */
211 SetLastError(0xdeadbeef);
212 ret = pCryptCATAdminReleaseContext(NULL, 0);
213 ok(!ret, "Expected failure\n");
214 ok(GetLastError() == ERROR_INVALID_PARAMETER,
215 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
217 /* Proper release */
218 SetLastError(0xdeadbeef);
219 ret = pCryptCATAdminReleaseContext(hca, 0);
220 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
222 /* Try to release a second time */
223 SetLastError(0xdeadbeef);
224 ret = pCryptCATAdminReleaseContext(hca, 0);
225 ok(!ret, "Expected failure\n");
226 ok(GetLastError() == ERROR_INVALID_PARAMETER,
227 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 /* NULL context handle and dummy GUID */
230 SetLastError(0xdeadbeef);
231 ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
232 ok(!ret, "Expected failure\n");
233 ok(GetLastError() == ERROR_INVALID_PARAMETER,
234 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
236 /* Correct context handle and dummy GUID
238 * The tests run in the past unfortunately made sure that some directories were created.
240 * FIXME:
241 * We don't want to mess too much with these for now so we should delete only the ones
242 * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
243 * safe to remove files and directories from CatRoot/CatRoot2.
246 ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
247 ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
248 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
250 win_skip("Not running as administrator\n");
251 return;
253 ok(hca != NULL, "Expected a context handle, got NULL\n");
255 attrs = GetFileAttributesA(catroot);
256 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
258 /* Windows creates the GUID directory in capitals */
259 lstrcpyA(dummydir, catroot);
260 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
261 attrs = GetFileAttributesA(dummydir);
262 ok(attrs != INVALID_FILE_ATTRIBUTES,
263 "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
265 /* Only present on XP or higher. */
266 attrs = GetFileAttributesA(catroot2);
267 if (attrs != INVALID_FILE_ATTRIBUTES)
269 lstrcpyA(dummydir, catroot2);
270 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
271 attrs = GetFileAttributesA(dummydir);
272 ok(attrs != INVALID_FILE_ATTRIBUTES,
273 "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
276 ret = pCryptCATAdminReleaseContext(hca, 0);
277 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
279 /* Correct context handle and GUID */
280 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
281 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
282 ok(hca != NULL, "Expected a context handle, got NULL\n");
284 ret = pCryptCATAdminReleaseContext(hca, 0);
285 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
287 hca = (void *) 0xdeadbeef;
288 SetLastError(0xdeadbeef);
289 /* Flags is documented as unused, but the parameter is checked since win8 */
290 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
291 ok((!ret && (GetLastError() == ERROR_INVALID_PARAMETER) && (hca == (void *) 0xdeadbeef)) ||
292 broken(ret && hca != NULL && hca != (void *) 0xdeadbeef),
293 "Expected FALSE and ERROR_INVALID_PARAMETER with untouched handle, got %d and %u with %p\n",
294 ret, GetLastError(), hca);
296 if (ret && hca)
298 SetLastError(0xdeadbeef);
299 ret = pCryptCATAdminReleaseContext(hca, 0);
300 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
304 /* TODO: Check whether SHA-1 is the algorithm that's always used */
305 static void test_calchash(void)
307 BOOL ret;
308 HANDLE file;
309 DWORD hashsize = 0;
310 BYTE* hash;
311 BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
312 CHAR temp[MAX_PATH];
313 DWORD written;
315 /* All NULL */
316 SetLastError(0xdeadbeef);
317 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
318 ok(!ret, "Expected failure\n");
319 ok(GetLastError() == ERROR_INVALID_PARAMETER,
320 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
322 /* NULL filehandle, rest is legal */
323 SetLastError(0xdeadbeef);
324 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
325 ok(!ret, "Expected failure\n");
326 ok(GetLastError() == ERROR_INVALID_PARAMETER,
327 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
329 /* Correct filehandle, rest is NULL */
330 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
331 SetLastError(0xdeadbeef);
332 ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
333 ok(!ret, "Expected failure\n");
334 ok(GetLastError() == ERROR_INVALID_PARAMETER,
335 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
336 CloseHandle(file);
338 /* All OK, but dwFlags set to 1 */
339 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
340 SetLastError(0xdeadbeef);
341 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
342 ok(!ret, "Expected failure\n");
343 ok(GetLastError() == ERROR_INVALID_PARAMETER,
344 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
345 CloseHandle(file);
347 /* All OK, requesting the size of the hash */
348 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
349 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
350 SetLastError(0xdeadbeef);
351 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
352 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
353 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
354 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
355 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
356 CloseHandle(file);
358 /* All OK, retrieve the hash
359 * Double the hash buffer to see what happens to the size parameter
361 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
362 hashsize *= 2;
363 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
364 SetLastError(0xdeadbeef);
365 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
366 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
367 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
368 ok(GetLastError() == ERROR_SUCCESS,
369 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
370 CloseHandle(file);
371 HeapFree(GetProcessHeap(), 0, hash);
373 /* Do the same test with a file created and filled by ourselves (and we thus
374 * have a known hash for).
376 GetTempFileNameA(CURR_DIR, "hsh", 0, temp);
377 file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
378 WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
379 CloseHandle(file);
381 /* All OK, first request the size and then retrieve the hash */
382 file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
383 hashsize = 0;
384 pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
385 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
386 SetLastError(0xdeadbeef);
387 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
388 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
389 ok(GetLastError() == ERROR_SUCCESS,
390 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
391 ok(hashsize == sizeof(expectedhash) &&
392 !memcmp(hash, expectedhash, sizeof(expectedhash)),
393 "Hashes didn't match\n");
394 CloseHandle(file);
396 HeapFree(GetProcessHeap(), 0, hash);
397 DeleteFileA(temp);
400 static void test_CryptCATOpen(void)
402 HANDLE hcat;
403 char empty[MAX_PATH];
404 WCHAR emptyW[MAX_PATH];
405 HANDLE file;
406 BOOL ret;
408 SetLastError(0xdeadbeef);
409 hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
410 ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
411 ok(GetLastError() == ERROR_INVALID_PARAMETER,
412 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
414 if (!GetTempFileNameA(CURR_DIR, "cat", 0, empty)) return;
416 file = CreateFileA(empty, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
417 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
418 CloseHandle(file);
419 MultiByteToWideChar(CP_ACP, 0, empty, -1, emptyW, MAX_PATH);
421 hcat = pCryptCATOpen(emptyW, 0, 0, 0, 0);
422 todo_wine
423 ok(hcat != INVALID_HANDLE_VALUE, "Expected a correct handle\n");
425 ret = pCryptCATClose(hcat);
426 todo_wine
427 ok(ret, "CryptCATClose failed\n");
428 DeleteFileA(empty);
431 static DWORD error_area;
432 static DWORD local_error;
434 static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
436 ok(error_area != -2, "Didn't expect cdf_callback() to be called (%08x, %08x)\n",
437 area, error);
439 error_area = area;
440 local_error = error;
443 static void test_CryptCATCDF_params(void)
445 static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
446 CRYPTCATCDF *catcdf;
447 BOOL ret;
449 if (!pCryptCATCDFOpen)
451 win_skip("CryptCATCDFOpen is not available\n");
452 return;
455 SetLastError(0xdeadbeef);
456 catcdf = pCryptCATCDFOpen(NULL, NULL);
457 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
458 todo_wine
459 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
461 SetLastError(0xdeadbeef);
462 catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
463 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
464 todo_wine
465 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
467 /* File doesn't exist */
468 SetLastError(0xdeadbeef);
469 catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
470 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
471 todo_wine
472 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
474 SetLastError(0xdeadbeef);
475 ret = pCryptCATCDFClose(NULL);
476 ok(!ret, "Expected failure\n");
477 todo_wine
478 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
480 catcdf = NULL;
481 SetLastError(0xdeadbeef);
482 ret = pCryptCATCDFClose(catcdf);
483 ok(!ret, "Expected failure\n");
484 todo_wine
485 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
488 /* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
489 static void test_CryptCATAdminAddRemoveCatalog(void)
491 static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
492 static const char basename[] = "winetest.cat";
493 HCATADMIN hcatadmin;
494 HCATINFO hcatinfo;
495 CATALOG_INFO info;
496 WCHAR tmpfileW[MAX_PATH];
497 char tmpfile[MAX_PATH];
498 char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
499 WCHAR catfileW[MAX_PATH];
500 HANDLE file;
501 DWORD error, written;
502 BOOL ret;
503 DWORD attrs;
505 if (!pCryptCATAdminRemoveCatalog)
507 /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
508 win_skip("CryptCATAdminRemoveCatalog is not available\n");
509 return;
512 if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
513 DeleteFileA(tmpfile);
514 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
515 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
516 CloseHandle(file);
518 ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
519 ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
520 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
522 win_skip("Not running as administrator\n");
523 return;
526 SetLastError(0xdeadbeef);
527 hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
528 error = GetLastError();
529 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
530 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
532 SetLastError(0xdeadbeef);
533 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
534 error = GetLastError();
535 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
536 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
538 MultiByteToWideChar(CP_ACP, 0, tmpfile, -1, tmpfileW, MAX_PATH);
540 SetLastError(0xdeadbeef);
541 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
542 error = GetLastError();
543 todo_wine {
544 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
545 ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
547 if (hcatinfo != NULL)
548 pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
550 SetLastError(0xdeadbeef);
551 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
552 error = GetLastError();
553 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
554 ok(error == ERROR_INVALID_PARAMETER ||
555 error == ERROR_BAD_FORMAT, /* win 8 */
556 "got %u\n", GetLastError());
558 SetLastError(0xdeadbeef);
559 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
560 error = GetLastError();
561 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
562 todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
564 DeleteFileA(tmpfile);
565 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
566 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
567 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
568 CloseHandle(file);
570 /* Unique name will be created */
571 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
572 if (!hcatinfo && (GetLastError() == ERROR_ACCESS_DENIED))
574 win_skip("Not enough rights\n");
575 goto cleanup;
577 todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
579 info.cbStruct = sizeof(info);
580 info.wszCatalogFile[0] = 0;
581 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
582 todo_wine
584 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
585 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
587 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, NULL, NULL);
588 if ((p = strrchr(catfile, '\\'))) p++;
589 memset(catfileW, 0, sizeof(catfileW));
590 MultiByteToWideChar(CP_ACP, 0, p, -1, catfileW, MAX_PATH);
592 /* Set the file attributes so we can check what happens with them during the 'copy' */
593 attrs = FILE_ATTRIBUTE_READONLY;
594 ret = SetFileAttributesA(tmpfile, attrs);
595 ok(ret, "SetFileAttributesA failed : %u\n", GetLastError());
597 /* winetest.cat will be created */
598 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
599 ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
601 lstrcpyA(catfilepath, catroot);
602 lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
603 attrs = GetFileAttributesA(catfilepath);
604 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
605 todo_wine
606 ok(attrs == FILE_ATTRIBUTE_SYSTEM ||
607 attrs == (FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_SYSTEM), /* Vista */
608 "File has wrong attributes : %08x\n", attrs);
610 info.cbStruct = sizeof(info);
611 info.wszCatalogFile[0] = 0;
612 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
613 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
614 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
615 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, NULL, NULL);
616 if ((p = strrchr(catfile, '\\'))) p++;
617 ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
619 ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
620 ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
622 /* Remove the catalog file with the unique name */
623 ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
624 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
626 /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
627 * according to MSDN */
628 ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
629 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
630 /* The call succeeded with the full path but the file is not removed */
631 attrs = GetFileAttributesA(catfilepath);
632 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
633 /* Given only the filename the file is removed */
634 ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
635 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
636 attrs = GetFileAttributesA(catfilepath);
637 ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
639 cleanup:
640 ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
641 ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
643 /* Set the attributes so we can delete the file */
644 attrs = FILE_ATTRIBUTE_NORMAL;
645 ret = SetFileAttributesA(tmpfile, attrs);
646 ok(ret, "SetFileAttributesA failed %u\n", GetLastError());
647 DeleteFileA(tmpfile);
650 static void test_catalog_properties(const char *catfile, int attributes, int members)
652 static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
654 HANDLE hcat;
655 CRYPTCATMEMBER *m;
656 CRYPTCATATTRIBUTE *attr;
657 char catalog[MAX_PATH];
658 WCHAR catalogW[MAX_PATH];
659 DWORD attrs;
660 BOOL ret;
661 int attrcount = 0, membercount = 0;
663 /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
664 * once wine is fixed
666 attrs = GetFileAttributesA(catfile);
667 if (attrs == INVALID_FILE_ATTRIBUTES)
669 HANDLE file;
670 DWORD written;
672 trace("Creating the catalog file\n");
673 if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
674 file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
675 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
676 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
677 CloseHandle(file);
679 attributes = 2;
680 members = 1;
681 MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
683 else
685 MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
686 catalog[0] = 0;
689 hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
690 if (hcat == INVALID_HANDLE_VALUE && members == 0)
692 win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
693 return;
695 ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
697 m = pCryptCATEnumerateMember(NULL, NULL);
698 ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
700 m = NULL;
701 while ((m = pCryptCATEnumerateMember(hcat, m)))
703 ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
704 todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
705 ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
706 ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
707 ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
708 ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
709 ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
711 attr = pCryptCATEnumerateAttr(hcat, m, NULL);
712 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
714 membercount++;
716 ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
718 attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
719 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
721 attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
722 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
724 attr = NULL;
725 while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
727 ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
728 !lstrcmpW(attr->pwszReferenceTag, attr2W),
729 "Expected 'attr1' or 'attr2'\n");
731 attrcount++;
733 todo_wine
734 ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
736 ret = pCryptCATClose(hcat);
737 ok(ret, "CryptCATClose failed\n");
738 if (catalog[0]) DeleteFileA( catalog );
741 static void test_create_catalog_file(void)
743 static const char catfileA[] = "winetest.cat";
744 static const char cdffileA[] = "winetest.cdf";
745 static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
746 CRYPTCATCDF *catcdf;
747 CRYPTCATATTRIBUTE *catattr;
748 CRYPTCATMEMBER *catmember;
749 WCHAR *catmembertag;
750 DWORD written, attrs;
751 HANDLE file;
752 BOOL ret;
753 int attrcount, membercount;
755 if (!pCryptCATCDFOpen)
757 win_skip("CryptCATCDFOpen is not available\n");
758 return;
761 /* Create the cdf file */
762 file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
763 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
764 WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
765 CloseHandle(file);
767 /* Don't enumerate attributes and members */
768 trace("No attribs and members\n");
769 SetLastError(0xdeadbeef);
770 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
771 todo_wine
773 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
774 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
777 ret = pCryptCATCDFClose(catcdf);
778 todo_wine
780 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
781 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
784 attrs = GetFileAttributesA(catfileA);
785 todo_wine
786 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
788 test_catalog_properties(catfileA, 0, 0);
789 DeleteFileA(catfileA);
791 /* Only enumerate the attributes */
792 trace("Only attributes\n");
793 attrcount = membercount = 0;
794 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
796 catattr = NULL;
797 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
799 ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
800 !lstrcmpW(catattr->pwszReferenceTag, attr2W),
801 "Expected 'attr1' or 'attr2'\n");
803 attrcount++;
805 todo_wine
806 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
808 pCryptCATCDFClose(catcdf);
809 /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
810 test_catalog_properties(catfileA, 0, 0);
811 DeleteFileA(catfileA);
813 /* Only enumerate the members */
814 trace("Only members\n");
815 membercount = 0;
816 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
818 catmember = NULL;
819 catmembertag = NULL;
820 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
822 ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
823 membercount++;
825 todo_wine
826 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
828 pCryptCATCDFClose(catcdf);
829 test_catalog_properties(catfileA, 0, 1);
830 DeleteFileA(catfileA);
832 /* Enumerate members and attributes */
833 trace("Attributes and members\n");
834 attrcount = membercount = 0;
835 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
837 catattr = NULL;
838 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
839 attrcount++;
840 todo_wine
841 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
843 catmember = NULL;
844 catmembertag = NULL;
845 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
846 membercount++;
847 todo_wine
848 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
850 pCryptCATCDFClose(catcdf);
851 test_catalog_properties(catfileA, 2, 1);
852 DeleteFileA(catfileA);
854 DeleteFileA(cdffileA);
857 static void create_cdf_file(const CHAR *filename, const CHAR *contents)
859 HANDLE file;
860 DWORD written;
862 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
863 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
864 WriteFile(file, contents, lstrlenA(contents), &written, NULL);
865 CloseHandle(file);
868 #define CHECK_EXPECT(a, b) \
869 do { \
870 ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
871 ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
872 } while (0)
874 /* Clear the variables (can't use 0) */
875 #define CLEAR_EXPECT \
876 error_area = local_error = -1
878 /* Set both variables so the callback routine can check if a call to it was unexpected */
879 #define SET_UNEXPECTED \
880 error_area = local_error = -2
882 static void test_cdf_parsing(void)
884 static const char catfileA[] = "tempfile.cat";
885 static const char cdffileA[] = "tempfile.cdf";
886 static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
887 CHAR cdf_contents[4096];
888 CRYPTCATCDF *catcdf;
889 CRYPTCATATTRIBUTE *catattr;
890 CRYPTCATMEMBER *catmember;
891 WCHAR *catmembertag;
893 if (!pCryptCATCDFOpen)
895 win_skip("CryptCATCDFOpen is not available\n");
896 return;
899 /* Empty file */
900 DeleteFileA(cdffileA);
901 create_cdf_file(cdffileA, "");
903 CLEAR_EXPECT;
904 SetLastError(0xdeadbeef);
905 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
906 todo_wine
907 CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
908 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
909 todo_wine
910 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
911 DeleteFileA(cdffileA);
912 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
914 /* Just the header */
915 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
916 create_cdf_file(cdffileA, cdf_contents);
918 SET_UNEXPECTED;
919 SetLastError(0xdeadbeef);
920 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
921 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
922 todo_wine
923 ok(GetLastError() == ERROR_SHARING_VIOLATION,
924 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
925 DeleteFileA(cdffileA);
927 /* Header and member only */
928 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
929 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
930 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
931 create_cdf_file(cdffileA, cdf_contents);
933 SET_UNEXPECTED;
934 SetLastError(0xdeadbeef);
935 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
936 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
937 todo_wine
938 ok(GetLastError() == ERROR_SHARING_VIOLATION,
939 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
940 DeleteFileA(cdffileA);
941 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
943 /* Header and Name (no value) */
944 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
945 lstrcatA(cdf_contents, "Name=\r\n");
946 create_cdf_file(cdffileA, cdf_contents);
948 SET_UNEXPECTED;
949 SetLastError(0xdeadbeef);
950 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
951 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
952 todo_wine
953 ok(GetLastError() == ERROR_SHARING_VIOLATION,
954 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
955 DeleteFileA(cdffileA);
956 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
958 /* Header and Name */
959 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
960 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
961 create_cdf_file(cdffileA, cdf_contents);
963 SET_UNEXPECTED;
964 SetLastError(0xdeadbeef);
965 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
966 todo_wine
968 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
969 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
971 pCryptCATCDFClose(catcdf);
972 DeleteFileA(cdffileA);
973 todo_wine
974 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
976 /* Header and nonexistent member */
977 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
978 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
979 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
980 lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
981 create_cdf_file(cdffileA, cdf_contents);
983 SET_UNEXPECTED;
984 SetLastError(0xdeadbeef);
985 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
986 todo_wine
988 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
989 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
991 /* Loop through the members */
992 CLEAR_EXPECT;
993 catmember = NULL;
994 catmembertag = NULL;
995 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
996 todo_wine
997 CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
998 pCryptCATCDFClose(catcdf);
999 DeleteFileA(cdffileA);
1000 todo_wine
1001 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1003 /* Header, correct member but no explicit newline */
1004 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1005 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1006 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1007 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
1008 create_cdf_file(cdffileA, cdf_contents);
1010 SET_UNEXPECTED;
1011 SetLastError(0xdeadbeef);
1012 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1013 todo_wine
1015 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1016 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1018 /* Loop through the members */
1019 CLEAR_EXPECT;
1020 catmember = NULL;
1021 catmembertag = NULL;
1022 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1023 todo_wine
1024 CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
1025 pCryptCATCDFClose(catcdf);
1026 DeleteFileA(cdffileA);
1027 todo_wine
1028 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1030 /* Header and 2 duplicate members */
1031 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1032 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1033 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1034 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1035 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1036 create_cdf_file(cdffileA, cdf_contents);
1038 SET_UNEXPECTED;
1039 SetLastError(0xdeadbeef);
1040 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1041 todo_wine
1043 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1044 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1046 /* Loop through the members */
1047 SET_UNEXPECTED;
1048 catmember = NULL;
1049 catmembertag = NULL;
1050 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1051 pCryptCATCDFClose(catcdf);
1052 test_catalog_properties(catfileA, 0, 1);
1053 DeleteFileA(cdffileA);
1054 todo_wine
1055 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1057 /* Wrong attribute */
1058 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1059 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1060 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
1061 create_cdf_file(cdffileA, cdf_contents);
1063 SET_UNEXPECTED;
1064 SetLastError(0xdeadbeef);
1065 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1066 todo_wine
1068 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1069 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1071 /* Loop through the attributes */
1072 CLEAR_EXPECT;
1073 catattr = NULL;
1074 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1075 todo_wine
1076 CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
1077 pCryptCATCDFClose(catcdf);
1078 DeleteFileA(cdffileA);
1079 todo_wine
1080 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1082 /* Two identical attributes */
1083 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1084 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1085 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1086 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1087 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1088 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1089 create_cdf_file(cdffileA, cdf_contents);
1091 SET_UNEXPECTED;
1092 SetLastError(0xdeadbeef);
1093 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1094 todo_wine
1096 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1097 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1099 /* Loop through the members */
1100 SET_UNEXPECTED;
1101 catmember = NULL;
1102 catmembertag = NULL;
1103 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1104 /* Loop through the attributes */
1105 SET_UNEXPECTED;
1106 catattr = NULL;
1107 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1108 pCryptCATCDFClose(catcdf);
1109 test_catalog_properties(catfileA, 1, 1);
1110 DeleteFileA(cdffileA);
1111 todo_wine
1112 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1114 /* Two different attribute values with the same tag */
1115 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1116 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1117 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1118 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
1119 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1120 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1121 create_cdf_file(cdffileA, cdf_contents);
1123 SET_UNEXPECTED;
1124 SetLastError(0xdeadbeef);
1125 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1126 todo_wine
1128 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1129 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1131 /* Loop through the members */
1132 SET_UNEXPECTED;
1133 catmember = NULL;
1134 catmembertag = NULL;
1135 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1136 /* Loop through the attributes */
1137 SET_UNEXPECTED;
1138 catattr = NULL;
1139 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1140 pCryptCATCDFClose(catcdf);
1141 test_catalog_properties(catfileA, 1, 1);
1142 DeleteFileA(cdffileA);
1143 todo_wine
1144 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1147 static const struct
1149 WORD e_magic; /* 00: MZ Header signature */
1150 WORD unused[29];
1151 DWORD e_lfanew; /* 3c: Offset to extended header */
1152 } dos_header =
1154 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
1157 static IMAGE_NT_HEADERS nt_header =
1159 IMAGE_NT_SIGNATURE, /* Signature */
1161 IMAGE_FILE_MACHINE_I386, /* Machine */
1162 1, /* NumberOfSections */
1163 0, /* TimeDateStamp */
1164 0, /* PointerToSymbolTable */
1165 0, /* NumberOfSymbols */
1166 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
1167 IMAGE_FILE_EXECUTABLE_IMAGE /* Characteristics */
1170 IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
1171 2, /* MajorLinkerVersion */
1172 15, /* MinorLinkerVersion */
1173 0, /* SizeOfCode */
1174 0, /* SizeOfInitializedData */
1175 0, /* SizeOfUninitializedData */
1176 0, /* AddressOfEntryPoint */
1177 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
1178 #ifndef _WIN64
1179 0, /* BaseOfData */
1180 #endif
1181 0x10000000, /* ImageBase */
1182 0, /* SectionAlignment */
1183 0, /* FileAlignment */
1184 4, /* MajorOperatingSystemVersion */
1185 0, /* MinorOperatingSystemVersion */
1186 1, /* MajorImageVersion */
1187 0, /* MinorImageVersion */
1188 4, /* MajorSubsystemVersion */
1189 0, /* MinorSubsystemVersion */
1190 0, /* Win32VersionValue */
1191 0x200, /* SizeOfImage */
1192 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
1193 0, /* CheckSum */
1194 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
1195 0, /* DllCharacteristics */
1196 0, /* SizeOfStackReserve */
1197 0, /* SizeOfStackCommit */
1198 0, /* SizeOfHeapReserve */
1199 3, /* SizeOfHeapCommit */
1200 2, /* LoaderFlags */
1201 1, /* NumberOfRvaAndSizes */
1202 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
1206 static void test_sip(void)
1208 static const WCHAR nameW[] = {'t','e','s','t','.','e','x','e',0};
1209 SIP_SUBJECTINFO info;
1210 DWORD index, encoding, size;
1211 HANDLE file;
1212 GUID guid;
1213 BOOL ret;
1214 char buf[1024];
1216 file = CreateFileW(nameW, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1217 ok(file != INVALID_HANDLE_VALUE, "can't create file\n");
1218 if(file == INVALID_HANDLE_VALUE)
1219 return;
1220 WriteFile(file, &dos_header, sizeof(dos_header), &size, NULL);
1221 WriteFile(file, &nt_header, sizeof(nt_header), &size, NULL);
1222 memset(buf, 0, sizeof(buf));
1223 WriteFile(file, buf, 0x200 - sizeof(dos_header) - sizeof(nt_header), &size, NULL);
1224 CloseHandle(file);
1226 file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1227 ok(file != INVALID_HANDLE_VALUE, "can't open file\n");
1229 memset(&info, 0, sizeof(SIP_SUBJECTINFO));
1230 info.cbSize = sizeof(SIP_SUBJECTINFO);
1231 info.pgSubjectType = &guid;
1232 ret = CryptSIPRetrieveSubjectGuid(NULL, file, info.pgSubjectType);
1233 ok(ret, "CryptSIPRetrieveSubjectGuid failed (%x)\n", GetLastError());
1235 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1236 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1237 index = GetLastError();
1238 ok(index == ERROR_PATH_NOT_FOUND, "GetLastError returned %x\n", index);
1240 info.hFile = file;
1241 info.pwsFileName = nameW;
1242 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1243 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1244 index = GetLastError();
1245 todo_wine ok(index == ERROR_INVALID_PARAMETER, "GetLastError returned %x\n", index);
1247 info.hFile = INVALID_HANDLE_VALUE;
1248 info.pwsFileName = nameW;
1249 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1250 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1251 index = GetLastError();
1252 ok(index == ERROR_SHARING_VIOLATION, "GetLastError returned %x\n", index);
1254 CloseHandle(file);
1255 file= CreateFileW(nameW, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1257 info.hFile = file;
1258 info.pwsFileName = (void*)0xdeadbeef;
1259 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1260 ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
1261 ok(index == 0, "index = %x\n", index);
1263 CloseHandle(file);
1264 file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1266 info.hFile = INVALID_HANDLE_VALUE;
1267 info.pwsFileName = nameW;
1268 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 14, (BYTE*)"longer message");
1269 ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
1270 ok(index == 1, "index = %x\n", index);
1272 size = 0;
1273 encoding = 0xdeadbeef;
1274 ret = pGetSignedDataMsg(&info, &encoding, 0, &size, NULL);
1275 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1276 ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
1277 ok(size == 16, "size = %d\n", size);
1279 ret = pGetSignedDataMsg(&info, &encoding, 0, &size, (BYTE*)buf);
1280 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1281 ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
1282 ok(size == 8, "size = %d\n", size);
1283 ok(!memcmp(buf, "test\0\0\0\0", 8), "buf = %s\n", buf);
1285 size = 0;
1286 encoding = 0xdeadbeef;
1287 ret = pGetSignedDataMsg(&info, &encoding, 1, &size, NULL);
1288 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1289 ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
1290 ok(size == 24, "size = %d\n", size);
1292 ret = pGetSignedDataMsg(&info, &encoding, 1, &size, (BYTE*)buf);
1293 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1294 ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
1295 ok(size == 16, "size = %d\n", size);
1296 ok(!strcmp(buf, "longer message"), "buf = %s\n", buf);
1298 CryptReleaseContext(info.hProv, 0);
1299 CloseHandle(file);
1300 DeleteFileW(nameW);
1303 START_TEST(crypt)
1305 char** myARGV;
1306 char sysdir[MAX_PATH];
1308 InitFunctionPtrs();
1310 if (!pCryptCATAdminAcquireContext)
1312 win_skip("CryptCATAdmin functions are not available\n");
1313 return;
1316 GetSystemDirectoryA(sysdir, MAX_PATH);
1317 lstrcpyA(catroot, sysdir);
1318 lstrcatA(catroot, "\\CatRoot");
1319 lstrcpyA(catroot2, sysdir);
1320 lstrcatA(catroot2, "\\CatRoot2");
1322 winetest_get_mainargs(&myARGV);
1323 strcpy(selfname, myARGV[0]);
1325 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1327 test_context();
1328 test_calchash();
1329 test_CryptCATOpen();
1330 /* Parameter checking only */
1331 test_CryptCATCDF_params();
1332 /* Test the parsing of a cdf file */
1333 test_cdf_parsing();
1334 /* Create a catalog file out of our own catalog definition file */
1335 test_create_catalog_file();
1336 test_CryptCATAdminAddRemoveCatalog();
1337 test_sip();