2 * Subject Interface Package tests
4 * Copyright 2006 Paul Vriens
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
30 #include "wine/test.h"
32 static BOOL (WINAPI * funcCryptSIPGetSignedDataMsg)(SIP_SUBJECTINFO *,DWORD *,DWORD,DWORD *,BYTE *);
33 static BOOL (WINAPI * funcCryptSIPPutSignedDataMsg)(SIP_SUBJECTINFO *,DWORD,DWORD *,DWORD,BYTE *);
34 static BOOL (WINAPI * funcCryptSIPCreateIndirectData)(SIP_SUBJECTINFO *,DWORD *,SIP_INDIRECT_DATA *);
35 static BOOL (WINAPI * funcCryptSIPVerifyIndirectData)(SIP_SUBJECTINFO *,SIP_INDIRECT_DATA *);
36 static BOOL (WINAPI * funcCryptSIPRemoveSignedDataMsg)(SIP_SUBJECTINFO *,DWORD);
38 static char *show_guid(const GUID *guid)
40 static char guidstring[39];
43 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
44 guid->Data1, guid->Data2, guid->Data3,
45 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
46 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
51 static void test_AddRemoveProvider(void)
54 SIP_ADD_NEWPROVIDER newprov;
55 GUID actionid = { 0xdeadbe, 0xefde, 0xadbe, { 0xef,0xde,0xad,0xbe,0xef,0xde,0xad,0xbe }};
56 static WCHAR dummydll[] = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
57 static WCHAR dummyfunction[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
60 SetLastError(0xdeadbeef);
61 ret = CryptSIPRemoveProvider(NULL);
62 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
63 ok (GetLastError() == ERROR_INVALID_PARAMETER,
64 "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
66 /* nonexistent provider should result in a registry error */
67 SetLastError(0xdeadbeef);
68 ret = CryptSIPRemoveProvider(&actionid);
69 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
70 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
71 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
73 /* Everything OK, pwszIsFunctionName and pwszIsFunctionNameFmt2 are left NULL
76 memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
77 newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
78 newprov.pgSubject = &actionid;
79 newprov.pwszDLLFileName = dummydll;
80 newprov.pwszGetFuncName = dummyfunction;
81 newprov.pwszPutFuncName = dummyfunction;
82 newprov.pwszCreateFuncName = dummyfunction;
83 newprov.pwszVerifyFuncName = dummyfunction;
84 newprov.pwszRemoveFuncName = dummyfunction;
85 SetLastError(0xdeadbeef);
86 ret = CryptSIPAddProvider(&newprov);
87 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
88 ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n",
91 /* Dummy provider will be deleted, but the function still fails because
92 * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
95 SetLastError(0xdeadbeef);
96 ret = CryptSIPRemoveProvider(&actionid);
97 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
98 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
99 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
102 memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
103 newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
104 newprov.pgSubject = &actionid;
105 newprov.pwszDLLFileName = dummydll;
106 newprov.pwszGetFuncName = dummyfunction;
107 newprov.pwszPutFuncName = dummyfunction;
108 newprov.pwszCreateFuncName = dummyfunction;
109 newprov.pwszVerifyFuncName = dummyfunction;
110 newprov.pwszRemoveFuncName = dummyfunction;
111 newprov.pwszIsFunctionNameFmt2 = dummyfunction;
112 newprov.pwszIsFunctionName = dummyfunction;
113 SetLastError(0xdeadbeef);
114 ret = CryptSIPAddProvider(&newprov);
115 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
116 ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n",
119 /* Dummy provider should be deleted */
120 SetLastError(0xdeadbeef);
121 ret = CryptSIPRemoveProvider(&actionid);
122 ok ( ret, "CryptSIPRemoveProvider should have succeeded\n");
123 ok ( GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n",
127 static void test_SIPRetrieveSubjectGUID(void)
132 static const CHAR windir[] = "windir";
133 static const CHAR regeditExe[] = "regedit.exe";
134 static const GUID nullSubject = { 0x0, 0x0, 0x0, { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 }};
135 static const WCHAR deadbeef[] = { 'c',':','\\','d','e','a','d','b','e','e','f','.','d','b','f',0 };
136 /* Couldn't find a name for this GUID, it's the one used for 95% of the files */
137 static const GUID unknownGUID = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
138 static CHAR regeditPath[MAX_PATH];
139 static WCHAR regeditPathW[MAX_PATH];
140 static CHAR path[MAX_PATH];
141 static CHAR tempfile[MAX_PATH];
142 static WCHAR tempfileW[MAX_PATH];
146 SetLastError(0xdeadbeef);
147 ret = CryptSIPRetrieveSubjectGuid(NULL, NULL, NULL);
148 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
149 ok (GetLastError() == ERROR_INVALID_PARAMETER,
150 "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
152 /* Test with a nonexistent file (hopefully) */
153 SetLastError(0xdeadbeef);
154 /* Set subject to something other than zeros */
155 memset(&subject, 1, sizeof(GUID));
156 ret = CryptSIPRetrieveSubjectGuid(deadbeef, NULL, &subject);
157 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
158 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
159 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
160 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
161 "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
163 /* Now with an executable that should exist
165 * Use A-functions where possible as that should be available on all platforms
167 ret = GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
168 ok (ret > 0, "expected GEVA(windir) to succeed, last error %d\n", GetLastError());
169 strcat(regeditPath, "\\");
170 strcat(regeditPath, regeditExe);
171 MultiByteToWideChar( CP_ACP, 0, regeditPath,
172 strlen(regeditPath)+1, regeditPathW,
173 sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
175 SetLastError(0xdeadbeef);
176 memset(&subject, 1, sizeof(GUID));
177 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
178 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
179 ok ( GetLastError() == ERROR_SUCCESS,
180 "Expected ERROR_SUCCESS, got 0x%08x\n", GetLastError());
181 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
182 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
184 /* The same thing but now with a handle instead of a filename */
185 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
186 SetLastError(0xdeadbeef);
187 memset(&subject, 1, sizeof(GUID));
188 ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
189 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
190 ok ( GetLastError() == ERROR_SUCCESS,
191 "Expected ERROR_SUCCESS, got 0x%08x\n", GetLastError());
192 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
193 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
197 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
198 SetLastError(0xdeadbeef);
199 memset(&subject, 1, sizeof(GUID));
200 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
201 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
202 ok ( GetLastError() == ERROR_SUCCESS,
203 "Expected ERROR_SUCCESS, got 0x%08x\n", GetLastError());
204 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
205 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
208 /* Now with an empty file */
209 GetTempPathA(sizeof(path), path);
210 GetTempFileNameA(path, "sip", 0 , tempfile);
211 MultiByteToWideChar( CP_ACP, 0, tempfile,
212 strlen(tempfile)+1, tempfileW,
213 sizeof(tempfileW)/sizeof(tempfileW[0]) );
215 SetLastError(0xdeadbeef);
216 memset(&subject, 1, sizeof(GUID));
217 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
218 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
219 ok ( GetLastError() == ERROR_FILE_INVALID ||
220 GetLastError() == S_OK /* Win98 */,
221 "Expected ERROR_FILE_INVALID or S_OK, got 0x%08x\n", GetLastError());
222 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
223 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
225 /* Use a file with a size of 3 (at least < 4) */
226 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
227 WriteFile(file, "123", 3, &written, NULL);
230 SetLastError(0xdeadbeef);
231 memset(&subject, 1, sizeof(GUID));
232 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
233 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
234 ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
235 GetLastError() == S_OK /* Win98 */,
236 "Expected ERROR_INVALID_PARAMETER or S_OK, got 0x%08x\n", GetLastError());
237 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
238 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
241 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
242 WriteFile(file, "1234", 4, &written, NULL);
245 SetLastError(0xdeadbeef);
246 memset(&subject, 1, sizeof(GUID));
247 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
248 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
249 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
250 GetLastError() == S_OK /* Win98 */,
251 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or S_OK, got 0x%08x\n", GetLastError());
252 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
253 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
256 DeleteFileA(tempfile);
259 static void test_SIPLoad(void)
263 static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
264 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
265 static GUID unknown2 = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
266 /* The next SIP is available on Windows (not on a clean Wine install) */
267 static GUID unknown3 = { 0x000C10F1, 0x0000, 0x0000, { 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }}; /* MSISIP.DLL */
268 SIP_DISPATCH_INFO sdi;
272 SetLastError(0xdeadbeef);
273 ret = CryptSIPLoad(NULL, 0, NULL);
274 ok ( !ret, "Expected CryptSIPLoad to fail\n");
275 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
276 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
278 /* Only pSipDispatch NULL */
279 SetLastError(0xdeadbeef);
280 ret = CryptSIPLoad(&subject, 0, NULL);
281 ok ( !ret, "Expected CryptSIPLoad to fail\n");
282 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
283 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
285 /* No NULLs, but nonexistent pgSubject */
286 SetLastError(0xdeadbeef);
287 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
288 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
289 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
290 ret = CryptSIPLoad(&dummySubject, 0, &sdi);
291 ok ( !ret, "Expected CryptSIPLoad to fail\n");
293 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
294 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08x\n", GetLastError());
295 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
297 hCrypt = LoadLibraryA("crypt32.dll");
300 funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
301 funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
302 funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
303 funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
304 funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
306 /* We're not going to use the functions, so we can free already here */
310 SetLastError(0xdeadbeef);
311 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
312 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
313 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
314 ret = CryptSIPLoad(&unknown, 0, &sdi);
317 ok ( ret, "Expected CryptSIPLoad to succeed\n");
318 /* This error will always be there as native searches for the function DllCanUnloadNow
319 * in WINTRUST.DLL (in this case). This function is not available in WINTRUST.DLL.
320 * For now there's no need to implement this is Wine as I doubt any program will rely on
321 * this last error when the call succeeded.
323 ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
324 "Expected ERROR_PROC_NOT_FOUND, got 0x%08x\n", GetLastError());
325 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
328 /* The function addresses returned by CryptSIPLoad are actually the addresses of
329 * crypt32's own functions. A function calling these addresses will end up first
330 * calling crypt32 functions which in it's turn call the equivalent in the SIP
331 * as dictated by the given GUID.
333 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
334 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
336 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
337 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
338 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
339 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
340 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
341 "Expected function addresses to be from crypt32\n");
343 trace("Couldn't load function pointers\n");
345 /* All OK, but different GUID (same SIP though) */
346 SetLastError(0xdeadbeef);
347 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
348 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
349 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
350 ret = CryptSIPLoad(&unknown2, 0, &sdi);
353 ok ( ret, "Expected CryptSIPLoad to succeed\n");
354 /* This call on it's own would have resulted in a ERROR_PROC_NOT_FOUND, but the previous
355 * call to CryptSIPLoad already loaded wintrust.dll. As this information is cached,
356 * CryptSIPLoad will not try to search for the already mentioned DllCanUnloadNow.
359 ok ( GetLastError() == 0xdeadbeef,
360 "Expected 0xdeadbeef, got 0x%08x\n", GetLastError());
362 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
364 /* All OK, but other SIP */
365 SetLastError(0xdeadbeef);
366 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
367 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
368 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
369 ret = CryptSIPLoad(&unknown3, 0, &sdi);
372 /* The SIP is known so we can safely assume that the next tests can be done */
374 /* As msisip.dll is not checked yet by any of the previous calls, the
375 * function DllCanUnloadNow will be checked again in msisip.dll (it's not present)
379 ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
380 "Expected ERROR_PROC_NOT_FOUND, got 0x%08x\n", GetLastError());
381 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
383 /* This is another SIP but this test proves the function addresses are the same as
384 * in the previous test.
386 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
387 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
388 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
389 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
390 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
391 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
392 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
393 "Expected function addresses to be from crypt32\n");
395 trace("Couldn't load function pointers\n");
399 /* Reserved parameter not 0 */
400 SetLastError(0xdeadbeef);
401 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
402 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
403 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
404 ret = CryptSIPLoad(&unknown, 1, &sdi);
405 ok ( !ret, "Expected CryptSIPLoad to fail\n");
406 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
407 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
408 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
413 test_AddRemoveProvider();
414 /* It seems that the caching for loaded dlls is shared between CryptSIPRetrieveSubjectGUID
415 * and CryptSIPLoad. The tests have to be in this order to succeed. This is because in the last
416 * test for CryptSIPRetrieveSubjectGUID, several SIPs will be loaded (on Windows).
419 test_SIPRetrieveSubjectGUID();