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");
276 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
277 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
279 /* Only pSipDispatch NULL */
280 SetLastError(0xdeadbeef);
281 ret = CryptSIPLoad(&subject, 0, NULL);
282 ok ( !ret, "Expected CryptSIPLoad to fail\n");
284 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
285 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
287 /* No NULLs, but nonexistent pgSubject */
288 SetLastError(0xdeadbeef);
289 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
290 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
291 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
292 ret = CryptSIPLoad(&dummySubject, 0, &sdi);
293 ok ( !ret, "Expected CryptSIPLoad to fail\n");
295 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
296 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08x\n", GetLastError());
297 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
299 hCrypt = LoadLibraryA("crypt32.dll");
302 funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
303 funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
304 funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
305 funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
306 funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
308 /* We're not going to use the functions, so we can free already here */
312 SetLastError(0xdeadbeef);
313 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
314 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
315 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
316 ret = CryptSIPLoad(&unknown, 0, &sdi);
319 ok ( ret, "Expected CryptSIPLoad to succeed\n");
320 /* This error will always be there as native searches for the function DllCanUnloadNow
321 * in WINTRUST.DLL (in this case). This function is not available in WINTRUST.DLL.
322 * For now there's no need to implement this is Wine as I doubt any program will rely on
323 * this last error when the call succeeded.
325 ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
326 "Expected ERROR_PROC_NOT_FOUND, got 0x%08x\n", GetLastError());
327 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
330 /* The function addresses returned by CryptSIPLoad are actually the addresses of
331 * crypt32's own functions. A function calling these addresses will end up first
332 * calling crypt32 functions which in it's turn call the equivalent in the SIP
333 * as dictated by the given GUID.
335 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
336 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
338 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
339 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
340 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
341 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
342 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
343 "Expected function addresses to be from crypt32\n");
345 trace("Couldn't load function pointers\n");
347 /* All OK, but different GUID (same SIP though) */
348 SetLastError(0xdeadbeef);
349 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
350 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
351 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
352 ret = CryptSIPLoad(&unknown2, 0, &sdi);
355 ok ( ret, "Expected CryptSIPLoad to succeed\n");
356 /* This call on it's own would have resulted in a ERROR_PROC_NOT_FOUND, but the previous
357 * call to CryptSIPLoad already loaded wintrust.dll. As this information is cached,
358 * CryptSIPLoad will not try to search for the already mentioned DllCanUnloadNow.
361 ok ( GetLastError() == 0xdeadbeef,
362 "Expected 0xdeadbeef, got 0x%08x\n", GetLastError());
364 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
366 /* All OK, but other SIP */
367 SetLastError(0xdeadbeef);
368 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
369 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
370 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
371 ret = CryptSIPLoad(&unknown3, 0, &sdi);
374 /* The SIP is known so we can safely assume that the next tests can be done */
376 /* As msisip.dll is not checked yet by any of the previous calls, the
377 * function DllCanUnloadNow will be checked again in msisip.dll (it's not present)
381 ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
382 "Expected ERROR_PROC_NOT_FOUND, got 0x%08x\n", GetLastError());
383 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
385 /* This is another SIP but this test proves the function addresses are the same as
386 * in the previous test.
388 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
389 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
390 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
391 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
392 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
393 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
394 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
395 "Expected function addresses to be from crypt32\n");
397 trace("Couldn't load function pointers\n");
401 /* Reserved parameter not 0 */
402 SetLastError(0xdeadbeef);
403 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
404 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
405 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
406 ret = CryptSIPLoad(&unknown, 1, &sdi);
407 ok ( !ret, "Expected CryptSIPLoad to fail\n");
409 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
410 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
411 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
416 test_AddRemoveProvider();
417 /* It seems that the caching for loaded dlls is shared between CryptSIPRetrieveSubjectGUID
418 * and CryptSIPLoad. The tests have to be in this order to succeed. This is because in the last
419 * test for CryptSIPRetrieveSubjectGUID, several SIPs will be loaded (on Windows).
422 test_SIPRetrieveSubjectGUID();