crypt32/tests: Vista uses a different error code.
[wine.git] / dlls / crypt32 / tests / sip.c
blobcce058e9f6523b097fb271aee0643f72e25cf5f6
1 /*
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
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <winnls.h>
27 #include <wincrypt.h>
28 #include <mssip.h>
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];
42 sprintf(guidstring,
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] );
48 return guidstring;
51 static void test_AddRemoveProvider(void)
53 BOOL ret;
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 };
59 /* NULL check */
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
74 * as allowed */
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 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
89 skip("Need admin rights\n");
90 return;
92 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
94 /* Dummy provider will be deleted, but the function still fails because
95 * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
96 * registry.
98 SetLastError(0xdeadbeef);
99 ret = CryptSIPRemoveProvider(&actionid);
100 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
101 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
102 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
104 /* Everything OK */
105 memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
106 newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
107 newprov.pgSubject = &actionid;
108 newprov.pwszDLLFileName = dummydll;
109 newprov.pwszGetFuncName = dummyfunction;
110 newprov.pwszPutFuncName = dummyfunction;
111 newprov.pwszCreateFuncName = dummyfunction;
112 newprov.pwszVerifyFuncName = dummyfunction;
113 newprov.pwszRemoveFuncName = dummyfunction;
114 newprov.pwszIsFunctionNameFmt2 = dummyfunction;
115 newprov.pwszIsFunctionName = dummyfunction;
116 SetLastError(0xdeadbeef);
117 ret = CryptSIPAddProvider(&newprov);
118 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
120 /* Dummy provider should be deleted */
121 SetLastError(0xdeadbeef);
122 ret = CryptSIPRemoveProvider(&actionid);
123 ok ( ret, "CryptSIPRemoveProvider should have succeeded\n");
126 static void test_SIPRetrieveSubjectGUID(void)
128 BOOL ret;
129 GUID subject;
130 HANDLE file;
131 static const CHAR windir[] = "windir";
132 static const CHAR regeditExe[] = "regedit.exe";
133 static const GUID nullSubject = { 0x0, 0x0, 0x0, { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 }};
134 static const WCHAR deadbeef[] = { 'c',':','\\','d','e','a','d','b','e','e','f','.','d','b','f',0 };
135 /* Couldn't find a name for this GUID, it's the one used for 95% of the files */
136 static const GUID unknownGUID = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
137 static CHAR regeditPath[MAX_PATH];
138 static WCHAR regeditPathW[MAX_PATH];
139 static CHAR path[MAX_PATH];
140 static CHAR tempfile[MAX_PATH];
141 static WCHAR tempfileW[MAX_PATH];
142 DWORD written;
144 /* NULL check */
145 SetLastError(0xdeadbeef);
146 ret = CryptSIPRetrieveSubjectGuid(NULL, NULL, NULL);
147 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
148 ok (GetLastError() == ERROR_INVALID_PARAMETER,
149 "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
151 /* Test with a nonexistent file (hopefully) */
152 SetLastError(0xdeadbeef);
153 /* Set subject to something other than zeros */
154 memset(&subject, 1, sizeof(GUID));
155 ret = CryptSIPRetrieveSubjectGuid(deadbeef, NULL, &subject);
156 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
157 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
158 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
159 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
160 "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
162 /* Now with an executable that should exist
164 * Use A-functions where possible as that should be available on all platforms
166 ret = GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
167 ok (ret > 0, "expected GEVA(windir) to succeed, last error %d\n", GetLastError());
168 strcat(regeditPath, "\\");
169 strcat(regeditPath, regeditExe);
170 MultiByteToWideChar( CP_ACP, 0, regeditPath,
171 strlen(regeditPath)+1, regeditPathW,
172 sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
174 SetLastError(0xdeadbeef);
175 memset(&subject, 1, sizeof(GUID));
176 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
177 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
178 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
179 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
181 /* The same thing but now with a handle instead of a filename */
182 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
183 SetLastError(0xdeadbeef);
184 memset(&subject, 1, sizeof(GUID));
185 ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
186 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
187 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
188 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
189 CloseHandle(file);
191 /* And both */
192 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
193 SetLastError(0xdeadbeef);
194 memset(&subject, 1, sizeof(GUID));
195 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
196 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
197 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
198 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
199 CloseHandle(file);
201 /* Now with an empty file */
202 GetTempPathA(sizeof(path), path);
203 GetTempFileNameA(path, "sip", 0 , tempfile);
204 MultiByteToWideChar( CP_ACP, 0, tempfile,
205 strlen(tempfile)+1, tempfileW,
206 sizeof(tempfileW)/sizeof(tempfileW[0]) );
208 SetLastError(0xdeadbeef);
209 memset(&subject, 1, sizeof(GUID));
210 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
211 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
212 ok ( GetLastError() == ERROR_FILE_INVALID ||
213 GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ||
214 GetLastError() == ERROR_SUCCESS /* Win98 */,
215 "Expected ERROR_FILE_INVALID, ERROR_INVALID_PARAMETER or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
216 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
217 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
219 /* Use a file with a size of 3 (at least < 4) */
220 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
221 WriteFile(file, "123", 3, &written, NULL);
222 CloseHandle(file);
224 SetLastError(0xdeadbeef);
225 memset(&subject, 1, sizeof(GUID));
226 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
227 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
228 ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
229 GetLastError() == ERROR_SUCCESS /* Win98 */,
230 "Expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
231 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
232 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
234 /* And now >= 4 */
235 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
236 WriteFile(file, "1234", 4, &written, NULL);
237 CloseHandle(file);
239 SetLastError(0xdeadbeef);
240 memset(&subject, 1, sizeof(GUID));
241 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
242 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
243 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
244 GetLastError() == ERROR_SUCCESS /* Win98 */,
245 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
246 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
247 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
249 /* Clean up */
250 DeleteFileA(tempfile);
253 static void test_SIPLoad(void)
255 BOOL ret;
256 GUID subject;
257 static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
258 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
259 static GUID unknown2 = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
260 /* The next SIP is available on Windows (not on a clean Wine install) */
261 static GUID unknown3 = { 0x000C10F1, 0x0000, 0x0000, { 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }}; /* MSISIP.DLL */
262 SIP_DISPATCH_INFO sdi;
263 HMODULE hCrypt;
265 /* All NULL */
266 SetLastError(0xdeadbeef);
267 ret = CryptSIPLoad(NULL, 0, NULL);
268 ok ( !ret, "Expected CryptSIPLoad to fail\n");
269 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
270 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
272 /* Only pSipDispatch NULL */
273 SetLastError(0xdeadbeef);
274 ret = CryptSIPLoad(&subject, 0, NULL);
275 ok ( !ret, "Expected CryptSIPLoad to fail\n");
276 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
277 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
279 /* No NULLs, but nonexistent pgSubject */
280 SetLastError(0xdeadbeef);
281 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
282 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
283 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
284 ret = CryptSIPLoad(&dummySubject, 0, &sdi);
285 ok ( !ret, "Expected CryptSIPLoad to fail\n");
286 todo_wine
287 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
288 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08x\n", GetLastError());
289 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
291 hCrypt = LoadLibraryA("crypt32.dll");
292 if (hCrypt)
294 funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
295 funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
296 funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
297 funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
298 funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
300 /* We're not going to use the functions, so we can free already here */
301 FreeLibrary(hCrypt);
303 /* All OK */
304 SetLastError(0xdeadbeef);
305 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
306 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
307 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
308 ret = CryptSIPLoad(&unknown, 0, &sdi);
309 todo_wine
311 ok ( ret, "Expected CryptSIPLoad to succeed\n");
312 /* On native the last error will always be ERROR_PROC_NOT_FOUND as native searches for the function DllCanUnloadNow
313 * in WINTRUST.DLL (in this case). This function is not available in WINTRUST.DLL.
314 * For now there's no need to implement this is Wine as I doubt any program will rely on
315 * this last error when the call succeeded.
317 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
320 /* The function addresses returned by CryptSIPLoad are actually the addresses of
321 * crypt32's own functions. A function calling these addresses will end up first
322 * calling crypt32 functions which in its turn call the equivalent in the SIP
323 * as dictated by the given GUID.
325 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
326 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
327 todo_wine
328 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
329 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
330 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
331 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
332 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
333 "Expected function addresses to be from crypt32\n");
334 else
335 trace("Couldn't load function pointers\n");
337 /* All OK, but different GUID (same SIP though) */
338 SetLastError(0xdeadbeef);
339 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
340 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
341 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
342 ret = CryptSIPLoad(&unknown2, 0, &sdi);
343 todo_wine
345 ok ( ret, "Expected CryptSIPLoad to succeed\n");
346 /* This call on its own would have resulted in an ERROR_PROC_NOT_FOUND, but the previous
347 * call to CryptSIPLoad already loaded wintrust.dll. As this information is cached,
348 * CryptSIPLoad will not try to search for the already mentioned DllCanUnloadNow.
351 todo_wine
352 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
354 /* All OK, but other SIP */
355 SetLastError(0xdeadbeef);
356 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
357 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
358 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
359 ret = CryptSIPLoad(&unknown3, 0, &sdi);
360 if (ret)
362 /* The SIP is known so we can safely assume that the next tests can be done */
364 /* As msisip.dll is not checked yet by any of the previous calls, the
365 * function DllCanUnloadNow will be checked again in msisip.dll (it's not present)
367 todo_wine
369 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
371 /* This is another SIP but this test proves the function addresses are the same as
372 * in the previous test.
374 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
375 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
376 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
377 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
378 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
379 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
380 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
381 "Expected function addresses to be from crypt32\n");
382 else
383 trace("Couldn't load function pointers\n");
387 /* Reserved parameter not 0 */
388 SetLastError(0xdeadbeef);
389 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
390 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
391 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
392 ret = CryptSIPLoad(&unknown, 1, &sdi);
393 ok ( !ret, "Expected CryptSIPLoad to fail\n");
394 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
395 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
396 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
399 START_TEST(sip)
401 test_AddRemoveProvider();
402 /* It seems that the caching for loaded dlls is shared between CryptSIPRetrieveSubjectGUID
403 * and CryptSIPLoad. The tests have to be in this order to succeed. This is because in the last
404 * test for CryptSIPRetrieveSubjectGUID, several SIPs will be loaded (on Windows).
406 test_SIPLoad();
407 test_SIPRetrieveSubjectGUID();