[WINTRUST_WINETEST]
[reactos.git] / rostests / winetests / wintrust / crypt.c
1 /* Unit test suite for wintrust crypt functions
2 *
3 * Copyright 2007 Paul Vriens
4 * Copyright 2008 Hans Leidekker for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22 #include <stdarg.h>
23 #include <stdio.h>
24
25 #include "windows.h"
26 #include "mscat.h"
27
28 #include "wine/test.h"
29
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];
34
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};
38
39 /*
40 * Minimalistic catalog file. To reconstruct, save text below as winetest.cdf,
41 * convert to DOS line endings and run 'makecat /cat winetest.cdf'
42 */
43
44 /*
45 [CatalogHeader]
46 Name=winetest.cat
47 ResultDir=.\
48 PublicVersion=0x00000001
49 EncodingType=
50 CATATTR1=0x10010001:attr1:value1
51 CATATTR2=0x10010001:attr2:value2
52
53 [CatalogFiles]
54 hashme=.\winetest.cdf
55 */
56
57 static const CHAR test_cdf[] =
58 "[CatalogHeader]\r\n"
59 "Name=winetest.cat\r\n"
60 "ResultDir=.\\\r\n"
61 "PublicVersion=0x00000001\r\n"
62 "EncodingType=\r\n"
63 "CATATTR1=0x10010001:attr1:value1\r\n"
64 "CATATTR2=0x10010001:attr2:value2\r\n"
65 "\r\n"
66 "[CatalogFiles]\r\n"
67 "hashme=.\\winetest.cdf\r\n";
68
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,
98 };
99
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);
118 static pCryptSIPGetSignedDataMsg pGetSignedDataMsg;
119 static pCryptSIPPutSignedDataMsg pPutSignedDataMsg;
120
121 static void InitFunctionPtrs(void)
122 {
123 HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
124
125 #define WINTRUST_GET_PROC(func) \
126 p ## func = (void*)GetProcAddress(hWintrust, #func); \
127 if(!p ## func) { \
128 trace("GetProcAddress(%s) failed\n", #func); \
129 }
130
131 WINTRUST_GET_PROC(CryptCATAdminAcquireContext)
132 WINTRUST_GET_PROC(CryptCATAdminReleaseContext)
133 WINTRUST_GET_PROC(CryptCATAdminCalcHashFromFileHandle)
134 WINTRUST_GET_PROC(CryptCATAdminAddCatalog)
135 WINTRUST_GET_PROC(CryptCATAdminRemoveCatalog)
136 WINTRUST_GET_PROC(CryptCATAdminReleaseCatalogContext)
137 WINTRUST_GET_PROC(CryptCATOpen)
138 WINTRUST_GET_PROC(CryptCATCatalogInfoFromContext)
139 WINTRUST_GET_PROC(CryptCATCDFClose)
140 WINTRUST_GET_PROC(CryptCATCDFEnumCatAttributes)
141 WINTRUST_GET_PROC(CryptCATCDFEnumMembersByCDFTagEx)
142 WINTRUST_GET_PROC(CryptCATCDFOpen)
143 WINTRUST_GET_PROC(CryptCATEnumerateCatAttr)
144 WINTRUST_GET_PROC(CryptCATEnumerateMember)
145 WINTRUST_GET_PROC(CryptCATEnumerateAttr)
146 WINTRUST_GET_PROC(CryptCATClose)
147
148 #undef WINTRUST_GET_PROC
149
150 pGetSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPGetSignedDataMsg");
151 if(!pGetSignedDataMsg)
152 trace("GetProcAddress(CryptSIPGetSignedDataMsg) failed\n");
153
154 pPutSignedDataMsg = (void*)GetProcAddress(hWintrust, "CryptSIPPutSignedDataMsg");
155 if(!pPutSignedDataMsg)
156 trace("GetProcAddress(CryptSIPPutSignedDataMsg) failed\n");
157 }
158
159 static GUID dummy = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
160
161 static void test_context(void)
162 {
163 BOOL ret;
164 HCATADMIN hca;
165 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
166 CHAR dummydir[MAX_PATH];
167 DWORD attrs;
168
169 /* When CryptCATAdminAcquireContext is successful it will create
170 * several directories if they don't exist:
171 *
172 * ...\system32\CatRoot\{GUID}, this directory holds the .cat files
173 * ...\system32\CatRoot2\{GUID} (WinXP and up), here we find the catalog database for that GUID
174 *
175 * Windows Vista uses lowercase catroot and catroot2.
176 *
177 * When passed a NULL GUID it will create the following directories although on
178 * WinXP and up these directories are already present when Windows is installed:
179 *
180 * ...\system32\CatRoot\{127D0A1D-4EF2-11D1-8608-00C04FC295EE}
181 * ...\system32\CatRoot2\{127D0A1D-4EF2-11D1-8608-00C04FC295EE} (WinXP up)
182 *
183 * TODO: Find out what this GUID is/does.
184 *
185 * On WinXP and up there is also a TimeStamp file in some of directories that
186 * seem to indicate the last change to the catalog database for that GUID.
187 *
188 * On Windows 2000 some files are created/updated:
189 *
190 * ...\system32\CatRoot\SYSMAST.cbk
191 * ...\system32\CatRoot\SYSMAST.cbd
192 * ...\system32\CatRoot\{GUID}\CATMAST.cbk
193 * ...\system32\CatRoot\{GUID}\CATMAST.cbd
194 *
195 */
196
197 /* All NULL */
198 SetLastError(0xdeadbeef);
199 ret = pCryptCATAdminAcquireContext(NULL, NULL, 0);
200 ok(!ret, "Expected failure\n");
201 ok(GetLastError() == ERROR_INVALID_PARAMETER,
202 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
203
204 /* NULL GUID */
205 ret = pCryptCATAdminAcquireContext(&hca, NULL, 0);
206 ok(ret, "Expected success\n");
207 ok(hca != NULL, "Expected a context handle, got NULL\n");
208
209 /* All NULL */
210 SetLastError(0xdeadbeef);
211 ret = pCryptCATAdminReleaseContext(NULL, 0);
212 ok(!ret, "Expected failure\n");
213 ok(GetLastError() == ERROR_INVALID_PARAMETER,
214 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
215
216 /* Proper release */
217 SetLastError(0xdeadbeef);
218 ret = pCryptCATAdminReleaseContext(hca, 0);
219 ok(ret, "Expected success, got FALSE with %d\n", GetLastError());
220
221 /* Try to release a second time */
222 SetLastError(0xdeadbeef);
223 ret = pCryptCATAdminReleaseContext(hca, 0);
224 ok(!ret, "Expected failure\n");
225 ok(GetLastError() == ERROR_INVALID_PARAMETER,
226 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
227
228 /* NULL context handle and dummy GUID */
229 SetLastError(0xdeadbeef);
230 ret = pCryptCATAdminAcquireContext(NULL, &dummy, 0);
231 ok(!ret, "Expected failure\n");
232 ok(GetLastError() == ERROR_INVALID_PARAMETER,
233 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
234
235 /* Correct context handle and dummy GUID
236 *
237 * The tests run in the past unfortunately made sure that some directories were created.
238 *
239 * FIXME:
240 * We don't want to mess too much with these for now so we should delete only the ones
241 * that shouldn't be there like the deadbeef ones. We first have to figure out if it's
242 * save to remove files and directories from CatRoot/CatRoot2.
243 */
244
245 ret = pCryptCATAdminAcquireContext(&hca, &dummy, 0);
246 ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
247 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
248 {
249 win_skip("Not running as administrator\n");
250 return;
251 }
252 ok(hca != NULL, "Expected a context handle, got NULL\n");
253
254 attrs = GetFileAttributes(catroot);
255 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the CatRoot directory to exist\n");
256
257 /* Windows creates the GUID directory in capitals */
258 lstrcpyA(dummydir, catroot);
259 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
260 attrs = GetFileAttributes(dummydir);
261 ok(attrs != INVALID_FILE_ATTRIBUTES,
262 "Expected CatRoot\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
263
264 /* Only present on XP or higher. */
265 attrs = GetFileAttributes(catroot2);
266 if (attrs != INVALID_FILE_ATTRIBUTES)
267 {
268 lstrcpyA(dummydir, catroot2);
269 lstrcatA(dummydir, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}");
270 attrs = GetFileAttributes(dummydir);
271 ok(attrs != INVALID_FILE_ATTRIBUTES,
272 "Expected CatRoot2\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF} directory to exist\n");
273 }
274
275 ret = pCryptCATAdminReleaseContext(hca, 0);
276 ok(ret, "Expected success\n");
277
278 /* Correct context handle and GUID */
279 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 0);
280 ok(ret, "Expected success\n");
281 ok(hca != NULL, "Expected a context handle, got NULL\n");
282
283 ret = pCryptCATAdminReleaseContext(hca, 0);
284 ok(ret, "Expected success\n");
285
286 /* Flags not equal to 0 */
287 ret = pCryptCATAdminAcquireContext(&hca, &unknown, 1);
288 ok(ret, "Expected success\n");
289 ok(hca != NULL, "Expected a context handle, got NULL\n");
290
291 ret = pCryptCATAdminReleaseContext(hca, 0);
292 ok(ret, "Expected success\n");
293 }
294
295 /* TODO: Check whether SHA-1 is the algorithm that's always used */
296 static void test_calchash(void)
297 {
298 BOOL ret;
299 HANDLE file;
300 DWORD hashsize = 0;
301 BYTE* hash;
302 BYTE expectedhash[20] = {0x3a,0xa1,0x19,0x08,0xec,0xa6,0x0d,0x2e,0x7e,0xcc,0x7a,0xca,0xf5,0xb8,0x2e,0x62,0x6a,0xda,0xf0,0x19};
303 CHAR temp[MAX_PATH];
304 DWORD written;
305
306 /* All NULL */
307 SetLastError(0xdeadbeef);
308 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, NULL, NULL, 0);
309 ok(!ret, "Expected failure\n");
310 ok(GetLastError() == ERROR_INVALID_PARAMETER,
311 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
312
313 /* NULL filehandle, rest is legal */
314 SetLastError(0xdeadbeef);
315 ret = pCryptCATAdminCalcHashFromFileHandle(NULL, &hashsize, NULL, 0);
316 ok(!ret, "Expected failure\n");
317 ok(GetLastError() == ERROR_INVALID_PARAMETER,
318 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
319
320 /* Correct filehandle, rest is NULL */
321 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
322 SetLastError(0xdeadbeef);
323 ret = pCryptCATAdminCalcHashFromFileHandle(file, NULL, NULL, 0);
324 ok(!ret, "Expected failure\n");
325 ok(GetLastError() == ERROR_INVALID_PARAMETER,
326 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
327 CloseHandle(file);
328
329 /* All OK, but dwFlags set to 1 */
330 file = CreateFileA(selfname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
331 SetLastError(0xdeadbeef);
332 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 1);
333 ok(!ret, "Expected failure\n");
334 ok(GetLastError() == ERROR_INVALID_PARAMETER,
335 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
336 CloseHandle(file);
337
338 /* All OK, requesting the size of the hash */
339 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
340 ok(file != INVALID_HANDLE_VALUE, "CreateFile failed %u\n", GetLastError());
341 SetLastError(0xdeadbeef);
342 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
343 ok(ret, "Expected success %u\n", GetLastError());
344 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
345 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
346 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
347 CloseHandle(file);
348
349 /* All OK, retrieve the hash
350 * Double the hash buffer to see what happens to the size parameter
351 */
352 file = CreateFileA(selfname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
353 hashsize *= 2;
354 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
355 SetLastError(0xdeadbeef);
356 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
357 ok(ret, "Expected success %u\n", GetLastError());
358 ok(hashsize == 20," Expected a hash size of 20, got %d\n", hashsize);
359 ok(GetLastError() == ERROR_SUCCESS,
360 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
361 CloseHandle(file);
362 HeapFree(GetProcessHeap(), 0, hash);
363
364 /* Do the same test with a file created and filled by ourselves (and we thus
365 * have a known hash for).
366 */
367 GetTempFileNameA(CURR_DIR, "hsh", 0, temp);
368 file = CreateFileA(temp, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
369 WriteFile(file, "Text in this file is needed to create a know hash", 49, &written, NULL);
370 CloseHandle(file);
371
372 /* All OK, first request the size and then retrieve the hash */
373 file = CreateFileA(temp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
374 hashsize = 0;
375 pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, NULL, 0);
376 hash = HeapAlloc(GetProcessHeap(), 0, hashsize);
377 SetLastError(0xdeadbeef);
378 ret = pCryptCATAdminCalcHashFromFileHandle(file, &hashsize, hash, 0);
379 ok(ret, "Expected success\n");
380 ok(GetLastError() == ERROR_SUCCESS,
381 "Expected ERROR_SUCCESS, got %d\n", GetLastError());
382 ok(hashsize == sizeof(expectedhash) &&
383 !memcmp(hash, expectedhash, sizeof(expectedhash)),
384 "Hashes didn't match\n");
385 CloseHandle(file);
386
387 HeapFree(GetProcessHeap(), 0, hash);
388 DeleteFileA(temp);
389 }
390
391 static void test_CryptCATOpen(void)
392 {
393 HANDLE hcat;
394 char empty[MAX_PATH];
395 WCHAR emptyW[MAX_PATH];
396 HANDLE file;
397 BOOL ret;
398
399 SetLastError(0xdeadbeef);
400 hcat = pCryptCATOpen(NULL, 0, 0, 0, 0);
401 ok(hcat == INVALID_HANDLE_VALUE, "CryptCATOpen succeeded\n");
402 ok(GetLastError() == ERROR_INVALID_PARAMETER,
403 "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
404
405 if (!GetTempFileNameA(CURR_DIR, "cat", 0, empty)) return;
406
407 file = CreateFileA(empty, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
408 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
409 CloseHandle(file);
410 MultiByteToWideChar(CP_ACP, 0, empty, -1, emptyW, MAX_PATH);
411
412 hcat = pCryptCATOpen(emptyW, 0, 0, 0, 0);
413 todo_wine
414 ok(hcat != INVALID_HANDLE_VALUE, "Expected a correct handle\n");
415
416 ret = pCryptCATClose(hcat);
417 todo_wine
418 ok(ret, "CryptCATClose failed\n");
419 DeleteFileA(empty);
420 }
421
422 static DWORD error_area;
423 static DWORD local_error;
424
425 static void WINAPI cdf_callback(DWORD area, DWORD error, WCHAR* line)
426 {
427 ok(error_area != -2, "Didn't expect cdf_callback() to be called (%08x, %08x)\n",
428 area, error);
429
430 error_area = area;
431 local_error = error;
432 }
433
434 static void test_CryptCATCDF_params(void)
435 {
436 static WCHAR nonexistent[] = {'d','e','a','d','b','e','e','f','.','c','d','f',0};
437 CRYPTCATCDF *catcdf;
438 BOOL ret;
439
440 if (!pCryptCATCDFOpen)
441 {
442 win_skip("CryptCATCDFOpen is not available\n");
443 return;
444 }
445
446 SetLastError(0xdeadbeef);
447 catcdf = pCryptCATCDFOpen(NULL, NULL);
448 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
449 todo_wine
450 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
451
452 SetLastError(0xdeadbeef);
453 catcdf = pCryptCATCDFOpen(NULL, cdf_callback);
454 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
455 todo_wine
456 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
457
458 /* File doesn't exist */
459 SetLastError(0xdeadbeef);
460 catcdf = pCryptCATCDFOpen(nonexistent, cdf_callback);
461 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
462 todo_wine
463 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
464
465 SetLastError(0xdeadbeef);
466 ret = pCryptCATCDFClose(NULL);
467 ok(!ret, "Expected failure\n");
468 todo_wine
469 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
470
471 catcdf = NULL;
472 SetLastError(0xdeadbeef);
473 ret = pCryptCATCDFClose(catcdf);
474 ok(!ret, "Expected failure\n");
475 todo_wine
476 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
477 }
478
479 /* FIXME: Once Wine can create catalog files we should use the created catalog file in this test */
480 static void test_CryptCATAdminAddRemoveCatalog(void)
481 {
482 static WCHAR basenameW[] = {'w','i','n','e','t','e','s','t','.','c','a','t',0};
483 static CHAR basename[] = "winetest.cat";
484 HCATADMIN hcatadmin;
485 HCATINFO hcatinfo;
486 CATALOG_INFO info;
487 WCHAR tmpfileW[MAX_PATH];
488 char tmpfile[MAX_PATH];
489 char catfile[MAX_PATH], catfilepath[MAX_PATH], *p;
490 WCHAR catfileW[MAX_PATH];
491 HANDLE file;
492 DWORD error, written;
493 BOOL ret;
494 DWORD attrs;
495
496 if (!pCryptCATAdminRemoveCatalog)
497 {
498 /* NT4 and W2K do have CryptCATAdminAddCatalog !! */
499 win_skip("CryptCATAdminRemoveCatalog is not available\n");
500 return;
501 }
502
503 if (!GetTempFileNameA(CURR_DIR, "cat", 0, tmpfile)) return;
504 DeleteFileA(tmpfile);
505 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
506 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
507 CloseHandle(file);
508
509 ret = pCryptCATAdminAcquireContext(&hcatadmin, &dummy, 0);
510 ok(ret || GetLastError() == ERROR_ACCESS_DENIED, "CryptCATAdminAcquireContext failed %u\n", GetLastError());
511 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
512 {
513 win_skip("Not running as administrator\n");
514 return;
515 }
516
517 SetLastError(0xdeadbeef);
518 hcatinfo = pCryptCATAdminAddCatalog(NULL, NULL, NULL, 0);
519 error = GetLastError();
520 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
521 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
522
523 SetLastError(0xdeadbeef);
524 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, NULL, NULL, 0);
525 error = GetLastError();
526 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
527 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
528
529 MultiByteToWideChar(0, 0, tmpfile, -1, tmpfileW, MAX_PATH);
530
531 SetLastError(0xdeadbeef);
532 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
533 error = GetLastError();
534 todo_wine {
535 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
536 ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
537 }
538 if (hcatinfo != NULL)
539 pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
540
541 SetLastError(0xdeadbeef);
542 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 1);
543 error = GetLastError();
544 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
545 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", GetLastError());
546
547 SetLastError(0xdeadbeef);
548 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
549 error = GetLastError();
550 ok(hcatinfo == NULL, "CryptCATAdminAddCatalog succeeded\n");
551 todo_wine ok(error == ERROR_BAD_FORMAT, "got %u expected ERROR_BAD_FORMAT\n", GetLastError());
552
553 DeleteFileA(tmpfile);
554 file = CreateFileA(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
555 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
556 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
557 CloseHandle(file);
558
559 /* Unique name will be created */
560 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, NULL, 0);
561 todo_wine ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
562
563 info.cbStruct = sizeof(info);
564 info.wszCatalogFile[0] = 0;
565 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
566 todo_wine
567 {
568 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
569 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
570 }
571 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
572 if ((p = strrchr(catfile, '\\'))) p++;
573 memset(catfileW, 0, sizeof(catfileW));
574 MultiByteToWideChar(0, 0, p, -1, catfileW, MAX_PATH);
575
576 /* Set the file attributes so we can check what happens with them during the 'copy' */
577 attrs = FILE_ATTRIBUTE_READONLY;
578 ret = SetFileAttributesA(tmpfile, attrs);
579 ok(ret, "SetFileAttributesA failed : %u\n", GetLastError());
580
581 /* winetest.cat will be created */
582 hcatinfo = pCryptCATAdminAddCatalog(hcatadmin, tmpfileW, basenameW, 0);
583 ok(hcatinfo != NULL, "CryptCATAdminAddCatalog failed %u\n", GetLastError());
584
585 lstrcpyA(catfilepath, catroot);
586 lstrcatA(catfilepath, "\\{DEADBEEF-DEAD-BEEF-DEAD-BEEFDEADBEEF}\\winetest.cat");
587 attrs = GetFileAttributes(catfilepath);
588 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
589 todo_wine
590 ok(attrs == FILE_ATTRIBUTE_SYSTEM ||
591 attrs == (FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_SYSTEM), /* Vista */
592 "File has wrong attributes : %08x\n", attrs);
593
594 info.cbStruct = sizeof(info);
595 info.wszCatalogFile[0] = 0;
596 ret = pCryptCATCatalogInfoFromContext(hcatinfo, &info, 0);
597 ok(ret, "CryptCATCatalogInfoFromContext failed %u\n", GetLastError());
598 ok(info.wszCatalogFile[0] != 0, "Expected a filename\n");
599 WideCharToMultiByte(CP_ACP, 0, info.wszCatalogFile, -1, catfile, MAX_PATH, 0, 0);
600 if ((p = strrchr(catfile, '\\'))) p++;
601 ok(!lstrcmpA(basename, p), "Expected %s, got %s\n", basename, p);
602
603 ret = pCryptCATAdminReleaseCatalogContext(hcatadmin, hcatinfo, 0);
604 ok(ret, "CryptCATAdminReleaseCatalogContext failed %u\n", GetLastError());
605
606 /* Remove the catalog file with the unique name */
607 ret = pCryptCATAdminRemoveCatalog(hcatadmin, catfileW, 0);
608 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
609
610 /* Remove the winetest.cat catalog file, first with the full path. This should not succeed
611 * according to MSDN */
612 ret = pCryptCATAdminRemoveCatalog(hcatadmin, info.wszCatalogFile, 0);
613 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
614 /* The call succeeded with the full path but the file is not removed */
615 attrs = GetFileAttributes(catfilepath);
616 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected %s to exist\n", catfilepath);
617 /* Given only the filename the file is removed */
618 ret = pCryptCATAdminRemoveCatalog(hcatadmin, basenameW, 0);
619 ok(ret, "CryptCATAdminRemoveCatalog failed %u\n", GetLastError());
620 attrs = GetFileAttributes(catfilepath);
621 ok(attrs == INVALID_FILE_ATTRIBUTES, "Expected %s to be removed\n", catfilepath);
622
623 ret = pCryptCATAdminReleaseContext(hcatadmin, 0);
624 ok(ret, "CryptCATAdminReleaseContext failed %u\n", GetLastError());
625
626 /* Set the attributes so we can delete the file */
627 attrs = FILE_ATTRIBUTE_NORMAL;
628 ret = SetFileAttributesA(tmpfile, attrs);
629 ok(ret, "SetFileAttributesA failed %u\n", GetLastError());
630 DeleteFileA(tmpfile);
631 }
632
633 static void test_catalog_properties(CHAR *catfile, int attributes, int members)
634 {
635 static const GUID subject = {0xde351a42,0x8e59,0x11d0,{0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee}};
636
637 HANDLE hcat;
638 CRYPTCATMEMBER *m;
639 CRYPTCATATTRIBUTE *attr;
640 char catalog[MAX_PATH];
641 WCHAR catalogW[MAX_PATH];
642 DWORD attrs;
643 BOOL ret;
644 int attrcount = 0, membercount = 0;
645
646 /* FIXME: Wine can't create catalog files out of catalog definition files yet. Remove this piece
647 * once wine is fixed
648 */
649 attrs = GetFileAttributesA(catfile);
650 if (attrs == INVALID_FILE_ATTRIBUTES)
651 {
652 HANDLE file;
653 DWORD written;
654
655 trace("Creating the catalog file\n");
656 if (!GetTempFileNameA(CURR_DIR, "cat", 0, catalog)) return;
657 file = CreateFileA(catalog, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
658 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
659 WriteFile(file, test_catalog, sizeof(test_catalog), &written, NULL);
660 CloseHandle(file);
661
662 attributes = 2;
663 members = 1;
664 MultiByteToWideChar(CP_ACP, 0, catalog, -1, catalogW, MAX_PATH);
665 }
666 else
667 {
668 MultiByteToWideChar(CP_ACP, 0, catfile, -1, catalogW, MAX_PATH);
669 catalog[0] = 0;
670 }
671
672 hcat = pCryptCATOpen(catalogW, 0, 0, 0, 0);
673 if (hcat == INVALID_HANDLE_VALUE && members == 0)
674 {
675 win_skip("CryptCATOpen on W2K can't handle catalog files with no members\n");
676 return;
677 }
678 ok(hcat != INVALID_HANDLE_VALUE, "CryptCATOpen failed %u\n", GetLastError());
679
680 m = pCryptCATEnumerateMember(NULL, NULL);
681 ok(m == NULL, "CryptCATEnumerateMember succeeded\n");
682
683 m = NULL;
684 while ((m = pCryptCATEnumerateMember(hcat, m)))
685 {
686 ok(m->cbStruct == sizeof(CRYPTCATMEMBER), "unexpected size %u\n", m->cbStruct);
687 todo_wine ok(!lstrcmpW(m->pwszReferenceTag, hashmeW), "unexpected tag\n");
688 ok(!memcmp(&m->gSubjectType, &subject, sizeof(subject)), "guid differs\n");
689 ok(!m->fdwMemberFlags, "got %x expected 0\n", m->fdwMemberFlags);
690 ok(m->dwCertVersion == 0x200, "got %x expected 0x200\n", m->dwCertVersion);
691 ok(!m->dwReserved, "got %x expected 0\n", m->dwReserved);
692 ok(m->hReserved == NULL, "got %p expected NULL\n", m->hReserved);
693
694 attr = pCryptCATEnumerateAttr(hcat, m, NULL);
695 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
696
697 membercount++;
698 }
699 ok(membercount == members, "Expected %d members, got %d\n", members, membercount);
700
701 attr = pCryptCATEnumerateAttr(NULL, NULL, NULL);
702 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
703
704 attr = pCryptCATEnumerateAttr(hcat, NULL, NULL);
705 ok(attr == NULL, "CryptCATEnumerateAttr succeeded\n");
706
707 attr = NULL;
708 while ((attr = pCryptCATEnumerateCatAttr(hcat, attr)))
709 {
710 ok(!lstrcmpW(attr->pwszReferenceTag, attr1W) ||
711 !lstrcmpW(attr->pwszReferenceTag, attr2W),
712 "Expected 'attr1' or 'attr2'\n");
713
714 attrcount++;
715 }
716 todo_wine
717 ok(attrcount == attributes, "Expected %d catalog attributes, got %d\n", attributes, attrcount);
718
719 ret = pCryptCATClose(hcat);
720 ok(ret, "CryptCATClose failed\n");
721 if (catalog[0]) DeleteFileA( catalog );
722 }
723
724 static void test_create_catalog_file(void)
725 {
726 static CHAR catfileA[] = "winetest.cat";
727 static CHAR cdffileA[] = "winetest.cdf";
728 static WCHAR cdffileW[] = {'w','i','n','e','t','e','s','t','.','c','d','f',0};
729 CRYPTCATCDF *catcdf;
730 CRYPTCATATTRIBUTE *catattr;
731 CRYPTCATMEMBER *catmember;
732 WCHAR *catmembertag;
733 DWORD written, attrs;
734 HANDLE file;
735 BOOL ret;
736 int attrcount, membercount;
737
738 if (!pCryptCATCDFOpen)
739 {
740 win_skip("CryptCATCDFOpen is not available\n");
741 return;
742 }
743
744 /* Create the cdf file */
745 file = CreateFileA(cdffileA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
746 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
747 WriteFile(file, test_cdf, sizeof(test_cdf) - 1, &written, NULL);
748 CloseHandle(file);
749
750 /* Don't enumerate attributes and members */
751 trace("No attribs and members\n");
752 SetLastError(0xdeadbeef);
753 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
754 todo_wine
755 {
756 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
757 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
758 }
759
760 ret = pCryptCATCDFClose(catcdf);
761 todo_wine
762 {
763 ok(ret, "Expected success\n");
764 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
765 }
766
767 attrs = GetFileAttributesA(catfileA);
768 todo_wine
769 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected the catalog file to exist\n");
770
771 test_catalog_properties(catfileA, 0, 0);
772 DeleteFileA(catfileA);
773
774 /* Only enumerate the attributes */
775 trace("Only attributes\n");
776 attrcount = membercount = 0;
777 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
778
779 catattr = NULL;
780 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
781 {
782 ok(!lstrcmpW(catattr->pwszReferenceTag, attr1W) ||
783 !lstrcmpW(catattr->pwszReferenceTag, attr2W),
784 "Expected 'attr1' or 'attr2'\n");
785
786 attrcount++;
787 }
788 todo_wine
789 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
790
791 pCryptCATCDFClose(catcdf);
792 /* Even though the resulting catalog file shows the attributes, they will not be enumerated */
793 test_catalog_properties(catfileA, 0, 0);
794 DeleteFileA(catfileA);
795
796 /* Only enumerate the members */
797 trace("Only members\n");
798 membercount = 0;
799 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
800
801 catmember = NULL;
802 catmembertag = NULL;
803 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
804 {
805 ok(!lstrcmpW(catmembertag, hashmeW), "Expected 'hashme'\n");
806 membercount++;
807 }
808 todo_wine
809 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
810
811 pCryptCATCDFClose(catcdf);
812 test_catalog_properties(catfileA, 0, 1);
813 DeleteFileA(catfileA);
814
815 /* Enumerate members and attributes */
816 trace("Attributes and members\n");
817 attrcount = membercount = 0;
818 catcdf = pCryptCATCDFOpen(cdffileW, NULL);
819
820 catattr = NULL;
821 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, NULL)))
822 attrcount++;
823 todo_wine
824 ok(attrcount == 2, "Expected 2 attributes, got %d\n", attrcount);
825
826 catmember = NULL;
827 catmembertag = NULL;
828 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, NULL, &catmember, FALSE, NULL)))
829 membercount++;
830 todo_wine
831 ok(membercount == 1, "Expected 1 member, got %d\n", membercount);
832
833 pCryptCATCDFClose(catcdf);
834 test_catalog_properties(catfileA, 2, 1);
835 DeleteFileA(catfileA);
836
837 DeleteFileA(cdffileA);
838 }
839
840 static void create_cdf_file(const CHAR *filename, const CHAR *contents)
841 {
842 HANDLE file;
843 DWORD written;
844
845 file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
846 ok(file != INVALID_HANDLE_VALUE, "CreateFileA failed %u\n", GetLastError());
847 WriteFile(file, contents, lstrlenA(contents), &written, NULL);
848 CloseHandle(file);
849 }
850
851 #define CHECK_EXPECT(a, b) \
852 do { \
853 ok(a == error_area, "Expected %08x, got %08x\n", a, error_area); \
854 ok(b == local_error, "Expected %08x, got %08x\n", b, local_error); \
855 } while (0)
856
857 /* Clear the variables (can't use 0) */
858 #define CLEAR_EXPECT \
859 error_area = local_error = -1
860
861 /* Set both variables so the callback routine can check if a call to it was unexpected */
862 #define SET_UNEXPECTED \
863 error_area = local_error = -2
864
865 static void test_cdf_parsing(void)
866 {
867 static CHAR catfileA[] = "tempfile.cat";
868 static CHAR cdffileA[] = "tempfile.cdf";
869 static WCHAR cdffileW[] = {'t','e','m','p','f','i','l','e','.','c','d','f',0};
870 CHAR cdf_contents[4096];
871 CRYPTCATCDF *catcdf;
872 CRYPTCATATTRIBUTE *catattr;
873 CRYPTCATMEMBER *catmember;
874 WCHAR *catmembertag;
875
876 if (!pCryptCATCDFOpen)
877 {
878 win_skip("CryptCATCDFOpen is not available\n");
879 return;
880 }
881
882 /* Empty file */
883 DeleteFileA(cdffileA);
884 create_cdf_file(cdffileA, "");
885
886 CLEAR_EXPECT;
887 SetLastError(0xdeadbeef);
888 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
889 todo_wine
890 CHECK_EXPECT(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND);
891 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
892 todo_wine
893 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
894 DeleteFileA(cdffileA);
895 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
896
897 /* Just the header */
898 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
899 create_cdf_file(cdffileA, cdf_contents);
900
901 SET_UNEXPECTED;
902 SetLastError(0xdeadbeef);
903 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
904 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
905 todo_wine
906 ok(GetLastError() == ERROR_SHARING_VIOLATION,
907 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
908 DeleteFileA(cdffileA);
909
910 /* Header and member only */
911 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
912 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
913 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
914 create_cdf_file(cdffileA, cdf_contents);
915
916 SET_UNEXPECTED;
917 SetLastError(0xdeadbeef);
918 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
919 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
920 todo_wine
921 ok(GetLastError() == ERROR_SHARING_VIOLATION,
922 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
923 DeleteFileA(cdffileA);
924 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
925
926 /* Header and Name (no value) */
927 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
928 lstrcatA(cdf_contents, "Name=\r\n");
929 create_cdf_file(cdffileA, cdf_contents);
930
931 SET_UNEXPECTED;
932 SetLastError(0xdeadbeef);
933 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
934 ok(catcdf == NULL, "CryptCATCDFOpen succeeded\n");
935 todo_wine
936 ok(GetLastError() == ERROR_SHARING_VIOLATION,
937 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
938 DeleteFileA(cdffileA);
939 ok(!DeleteFileA(catfileA), "Didn't expect a catalog file to be created\n");
940
941 /* Header and Name */
942 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
943 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
944 create_cdf_file(cdffileA, cdf_contents);
945
946 SET_UNEXPECTED;
947 SetLastError(0xdeadbeef);
948 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
949 todo_wine
950 {
951 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
952 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
953 }
954 pCryptCATCDFClose(catcdf);
955 DeleteFileA(cdffileA);
956 todo_wine
957 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
958
959 /* Header and nonexistent member */
960 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
961 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
962 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
963 lstrcatA(cdf_contents, "hashme=.\\deadbeef.cdf\r\n");
964 create_cdf_file(cdffileA, cdf_contents);
965
966 SET_UNEXPECTED;
967 SetLastError(0xdeadbeef);
968 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
969 todo_wine
970 {
971 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
972 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
973 }
974 /* Loop through the members */
975 CLEAR_EXPECT;
976 catmember = NULL;
977 catmembertag = NULL;
978 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
979 todo_wine
980 CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND);
981 pCryptCATCDFClose(catcdf);
982 DeleteFileA(cdffileA);
983 todo_wine
984 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
985
986 /* Header, correct member but no explicit newline */
987 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
988 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
989 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
990 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r");
991 create_cdf_file(cdffileA, cdf_contents);
992
993 SET_UNEXPECTED;
994 SetLastError(0xdeadbeef);
995 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
996 todo_wine
997 {
998 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
999 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1000 }
1001 /* Loop through the members */
1002 CLEAR_EXPECT;
1003 catmember = NULL;
1004 catmembertag = NULL;
1005 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1006 todo_wine
1007 CHECK_EXPECT(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH);
1008 pCryptCATCDFClose(catcdf);
1009 DeleteFileA(cdffileA);
1010 todo_wine
1011 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1012
1013 /* Header and 2 duplicate members */
1014 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1015 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1016 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1017 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1018 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1019 create_cdf_file(cdffileA, cdf_contents);
1020
1021 SET_UNEXPECTED;
1022 SetLastError(0xdeadbeef);
1023 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1024 todo_wine
1025 {
1026 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1027 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1028 }
1029 /* Loop through the members */
1030 SET_UNEXPECTED;
1031 catmember = NULL;
1032 catmembertag = NULL;
1033 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1034 pCryptCATCDFClose(catcdf);
1035 test_catalog_properties(catfileA, 0, 1);
1036 DeleteFileA(cdffileA);
1037 todo_wine
1038 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1039
1040 /* Wrong attribute */
1041 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1042 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1043 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1\r\n");
1044 create_cdf_file(cdffileA, cdf_contents);
1045
1046 SET_UNEXPECTED;
1047 SetLastError(0xdeadbeef);
1048 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1049 todo_wine
1050 {
1051 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1052 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1053 }
1054 /* Loop through the attributes */
1055 CLEAR_EXPECT;
1056 catattr = NULL;
1057 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1058 todo_wine
1059 CHECK_EXPECT(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES);
1060 pCryptCATCDFClose(catcdf);
1061 DeleteFileA(cdffileA);
1062 todo_wine
1063 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1064
1065 /* Two identical attributes */
1066 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1067 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1068 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1069 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1070 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1071 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1072 create_cdf_file(cdffileA, cdf_contents);
1073
1074 SET_UNEXPECTED;
1075 SetLastError(0xdeadbeef);
1076 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1077 todo_wine
1078 {
1079 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1080 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1081 }
1082 /* Loop through the members */
1083 SET_UNEXPECTED;
1084 catmember = NULL;
1085 catmembertag = NULL;
1086 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1087 /* Loop through the attributes */
1088 SET_UNEXPECTED;
1089 catattr = NULL;
1090 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1091 pCryptCATCDFClose(catcdf);
1092 test_catalog_properties(catfileA, 1, 1);
1093 DeleteFileA(cdffileA);
1094 todo_wine
1095 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1096
1097 /* Two different attribute values with the same tag */
1098 lstrcpyA(cdf_contents, "[CatalogHeader]\r\n");
1099 lstrcatA(cdf_contents, "Name=tempfile.cat\r\n");
1100 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr1:value1\r\n");
1101 lstrcatA(cdf_contents, "CATATTR1=0x10010001:attr2:value2\r\n");
1102 lstrcatA(cdf_contents, "[CatalogFiles]\r\n");
1103 lstrcatA(cdf_contents, "hashme=.\\tempfile.cdf\r\n");
1104 create_cdf_file(cdffileA, cdf_contents);
1105
1106 SET_UNEXPECTED;
1107 SetLastError(0xdeadbeef);
1108 catcdf = pCryptCATCDFOpen(cdffileW, cdf_callback);
1109 todo_wine
1110 {
1111 ok(catcdf != NULL, "CryptCATCDFOpen failed\n");
1112 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", GetLastError());
1113 }
1114 /* Loop through the members */
1115 SET_UNEXPECTED;
1116 catmember = NULL;
1117 catmembertag = NULL;
1118 while ((catmembertag = pCryptCATCDFEnumMembersByCDFTagEx(catcdf, catmembertag, cdf_callback, &catmember, FALSE, NULL))) ;
1119 /* Loop through the attributes */
1120 SET_UNEXPECTED;
1121 catattr = NULL;
1122 while ((catattr = pCryptCATCDFEnumCatAttributes(catcdf, catattr, cdf_callback))) ;
1123 pCryptCATCDFClose(catcdf);
1124 test_catalog_properties(catfileA, 1, 1);
1125 DeleteFileA(cdffileA);
1126 todo_wine
1127 ok(DeleteFileA(catfileA), "Expected a catalog file to be created\n");
1128 }
1129
1130 static const struct
1131 {
1132 WORD e_magic; /* 00: MZ Header signature */
1133 WORD unused[29];
1134 DWORD e_lfanew; /* 3c: Offset to extended header */
1135 } dos_header =
1136 {
1137 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
1138 };
1139
1140 static IMAGE_NT_HEADERS nt_header =
1141 {
1142 IMAGE_NT_SIGNATURE, /* Signature */
1143 {
1144 IMAGE_FILE_MACHINE_I386, /* Machine */
1145 1, /* NumberOfSections */
1146 0, /* TimeDateStamp */
1147 0, /* PointerToSymbolTable */
1148 0, /* NumberOfSymbols */
1149 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
1150 IMAGE_FILE_EXECUTABLE_IMAGE /* Characteristics */
1151 },
1152 {
1153 IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
1154 2, /* MajorLinkerVersion */
1155 15, /* MinorLinkerVersion */
1156 0, /* SizeOfCode */
1157 0, /* SizeOfInitializedData */
1158 0, /* SizeOfUninitializedData */
1159 0, /* AddressOfEntryPoint */
1160 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
1161 #ifndef _WIN64
1162 0, /* BaseOfData */
1163 #endif
1164 0x10000000, /* ImageBase */
1165 0, /* SectionAlignment */
1166 0, /* FileAlignment */
1167 4, /* MajorOperatingSystemVersion */
1168 0, /* MinorOperatingSystemVersion */
1169 1, /* MajorImageVersion */
1170 0, /* MinorImageVersion */
1171 4, /* MajorSubsystemVersion */
1172 0, /* MinorSubsystemVersion */
1173 0, /* Win32VersionValue */
1174 0x200, /* SizeOfImage */
1175 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
1176 0, /* CheckSum */
1177 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
1178 0, /* DllCharacteristics */
1179 0, /* SizeOfStackReserve */
1180 0, /* SizeOfStackCommit */
1181 0, /* SizeOfHeapReserve */
1182 3, /* SizeOfHeapCommit */
1183 2, /* LoaderFlags */
1184 1, /* NumberOfRvaAndSizes */
1185 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
1186 }
1187 };
1188
1189 static void test_sip(void)
1190 {
1191 static WCHAR nameW[] = {'t','e','s','t','.','e','x','e',0};
1192 SIP_SUBJECTINFO info;
1193 DWORD index, encoding, size;
1194 HANDLE file;
1195 GUID guid;
1196 BOOL ret;
1197 char buf[1024];
1198
1199 file = CreateFileW(nameW, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1200 ok(file != INVALID_HANDLE_VALUE, "can't create file\n");
1201 if(file == INVALID_HANDLE_VALUE)
1202 return;
1203 WriteFile(file, &dos_header, sizeof(dos_header), &size, NULL);
1204 WriteFile(file, &nt_header, sizeof(nt_header), &size, NULL);
1205 memset(buf, 0, sizeof(buf));
1206 WriteFile(file, buf, 0x200 - sizeof(dos_header) - sizeof(nt_header), &size, NULL);
1207 CloseHandle(file);
1208
1209 file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1210 ok(file != INVALID_HANDLE_VALUE, "can't open file\n");
1211
1212 memset(&info, 0, sizeof(SIP_SUBJECTINFO));
1213 info.cbSize = sizeof(SIP_SUBJECTINFO);
1214 info.pgSubjectType = &guid;
1215 ret = CryptSIPRetrieveSubjectGuid(NULL, file, info.pgSubjectType);
1216 ok(ret, "CryptSIPRetrieveSubjectGuid failed (%x)\n", GetLastError());
1217
1218 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1219 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1220 index = GetLastError();
1221 ok(index == ERROR_PATH_NOT_FOUND, "GetLastError returned %x\n", index);
1222
1223 info.hFile = file;
1224 info.pwsFileName = nameW;
1225 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1226 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1227 index = GetLastError();
1228 todo_wine ok(index == ERROR_INVALID_PARAMETER, "GetLastError returned %x\n", index);
1229
1230 info.hFile = INVALID_HANDLE_VALUE;
1231 info.pwsFileName = nameW;
1232 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1233 ok(!ret, "CryptSIPPutSignedDataMsg succeedded\n");
1234 index = GetLastError();
1235 ok(index == ERROR_SHARING_VIOLATION, "GetLastError returned %x\n", index);
1236
1237 CloseHandle(file);
1238 file= CreateFileW(nameW, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1239
1240 info.hFile = file;
1241 info.pwsFileName = (void*)0xdeadbeef;
1242 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 4, (BYTE*)"test");
1243 ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
1244 ok(index == 0, "index = %x\n", index);
1245
1246 CloseHandle(file);
1247 file= CreateFileW(nameW, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1248
1249 info.hFile = INVALID_HANDLE_VALUE;
1250 info.pwsFileName = nameW;
1251 ret = pPutSignedDataMsg(&info, X509_ASN_ENCODING, &index, 14, (BYTE*)"longer message");
1252 ok(ret, "CryptSIPPutSignedDataMsg failed (%x)\n", GetLastError());
1253 ok(index == 1, "index = %x\n", index);
1254
1255 size = 0;
1256 encoding = 0xdeadbeef;
1257 ret = pGetSignedDataMsg(&info, &encoding, 0, &size, NULL);
1258 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1259 ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
1260 ok(size == 16, "size = %d\n", size);
1261
1262 ret = pGetSignedDataMsg(&info, &encoding, 0, &size, (BYTE*)buf);
1263 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1264 ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
1265 ok(size == 8, "size = %d\n", size);
1266 ok(!memcmp(buf, "test\0\0\0\0", 8), "buf = %s\n", buf);
1267
1268 size = 0;
1269 encoding = 0xdeadbeef;
1270 ret = pGetSignedDataMsg(&info, &encoding, 1, &size, NULL);
1271 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1272 ok(encoding == 0xdeadbeef, "encoding = %x\n", encoding);
1273 ok(size == 24, "size = %d\n", size);
1274
1275 ret = pGetSignedDataMsg(&info, &encoding, 1, &size, (BYTE*)buf);
1276 ok(ret, "CryptSIPGetSignedDataMsg failed (%x)\n", GetLastError());
1277 ok(encoding == (X509_ASN_ENCODING|PKCS_7_ASN_ENCODING), "encoding = %x\n", encoding);
1278 ok(size == 16, "size = %d\n", size);
1279 ok(!strcmp(buf, "longer message"), "buf = %s\n", buf);
1280
1281 CryptReleaseContext(info.hProv, 0);
1282 CloseHandle(file);
1283 DeleteFileW(nameW);
1284 }
1285
1286 START_TEST(crypt)
1287 {
1288 char** myARGV;
1289 char sysdir[MAX_PATH];
1290
1291 InitFunctionPtrs();
1292
1293 if (!pCryptCATAdminAcquireContext)
1294 {
1295 win_skip("CryptCATAdmin functions are not available\n");
1296 return;
1297 }
1298
1299 GetSystemDirectoryA(sysdir, MAX_PATH);
1300 lstrcpyA(catroot, sysdir);
1301 lstrcatA(catroot, "\\CatRoot");
1302 lstrcpyA(catroot2, sysdir);
1303 lstrcatA(catroot2, "\\CatRoot2");
1304
1305 winetest_get_mainargs(&myARGV);
1306 strcpy(selfname, myARGV[0]);
1307
1308 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1309
1310 test_context();
1311 test_calchash();
1312 test_CryptCATOpen();
1313 /* Parameter checking only */
1314 test_CryptCATCDF_params();
1315 /* Test the parsing of a cdf file */
1316 test_cdf_parsing();
1317 /* Create a catalog file out of our own catalog definition file */
1318 test_create_catalog_file();
1319 test_CryptCATAdminAddRemoveCatalog();
1320 test_sip();
1321 }