push 4d5485f9b89f417d46b39b93e8d940437007f325
[wine/hacks.git] / dlls / crypt32 / tests / sip.c
blobd47b6fca57586773192e3c26e503a180b01f0d44
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 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
71 /* Apparently the needed rights are checked before the existence of the provider */
72 skip("Need admin rights\n");
74 else
76 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
77 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
78 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
81 /* Everything OK, pwszIsFunctionName and pwszIsFunctionNameFmt2 are left NULL
82 * as allowed */
84 memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
85 newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
86 newprov.pgSubject = &actionid;
87 newprov.pwszDLLFileName = dummydll;
88 newprov.pwszGetFuncName = dummyfunction;
89 newprov.pwszPutFuncName = dummyfunction;
90 newprov.pwszCreateFuncName = dummyfunction;
91 newprov.pwszVerifyFuncName = dummyfunction;
92 newprov.pwszRemoveFuncName = dummyfunction;
93 SetLastError(0xdeadbeef);
94 ret = CryptSIPAddProvider(&newprov);
95 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
97 skip("Need admin rights\n");
98 return;
100 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
102 /* Dummy provider will be deleted, but the function still fails because
103 * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
104 * registry.
106 SetLastError(0xdeadbeef);
107 ret = CryptSIPRemoveProvider(&actionid);
108 ok (!ret, "Expected CryptSIPRemoveProvider to fail.\n");
109 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
110 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
112 /* Everything OK */
113 memset(&newprov, 0, sizeof(SIP_ADD_NEWPROVIDER));
114 newprov.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
115 newprov.pgSubject = &actionid;
116 newprov.pwszDLLFileName = dummydll;
117 newprov.pwszGetFuncName = dummyfunction;
118 newprov.pwszPutFuncName = dummyfunction;
119 newprov.pwszCreateFuncName = dummyfunction;
120 newprov.pwszVerifyFuncName = dummyfunction;
121 newprov.pwszRemoveFuncName = dummyfunction;
122 newprov.pwszIsFunctionNameFmt2 = dummyfunction;
123 newprov.pwszIsFunctionName = dummyfunction;
124 SetLastError(0xdeadbeef);
125 ret = CryptSIPAddProvider(&newprov);
126 ok ( ret, "CryptSIPAddProvider should have succeeded\n");
128 /* Dummy provider should be deleted */
129 SetLastError(0xdeadbeef);
130 ret = CryptSIPRemoveProvider(&actionid);
131 ok ( ret, "CryptSIPRemoveProvider should have succeeded\n");
134 static void test_SIPRetrieveSubjectGUID(void)
136 BOOL ret;
137 GUID subject;
138 HANDLE file;
139 static const CHAR windir[] = "windir";
140 static const CHAR regeditExe[] = "regedit.exe";
141 static const GUID nullSubject = { 0x0, 0x0, 0x0, { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 }};
142 static const WCHAR deadbeef[] = { 'c',':','\\','d','e','a','d','b','e','e','f','.','d','b','f',0 };
143 /* Couldn't find a name for this GUID, it's the one used for 95% of the files */
144 static const GUID unknownGUID = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
145 static CHAR regeditPath[MAX_PATH];
146 static WCHAR regeditPathW[MAX_PATH];
147 static CHAR path[MAX_PATH];
148 static CHAR tempfile[MAX_PATH];
149 static WCHAR tempfileW[MAX_PATH];
150 DWORD written;
152 /* NULL check */
153 SetLastError(0xdeadbeef);
154 ret = CryptSIPRetrieveSubjectGuid(NULL, NULL, NULL);
155 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
156 ok (GetLastError() == ERROR_INVALID_PARAMETER,
157 "Expected ERROR_INVALID_PARAMETER, got %d.\n", GetLastError());
159 /* Test with a nonexistent file (hopefully) */
160 SetLastError(0xdeadbeef);
161 /* Set subject to something other than zeros */
162 memset(&subject, 1, sizeof(GUID));
163 ret = CryptSIPRetrieveSubjectGuid(deadbeef, NULL, &subject);
164 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
165 ok (GetLastError() == ERROR_FILE_NOT_FOUND,
166 "Expected ERROR_FILE_NOT_FOUND, got %d.\n", GetLastError());
167 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
168 "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
170 /* Now with an executable that should exist
172 * Use A-functions where possible as that should be available on all platforms
174 ret = GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
175 ok (ret > 0, "expected GEVA(windir) to succeed, last error %d\n", GetLastError());
176 strcat(regeditPath, "\\");
177 strcat(regeditPath, regeditExe);
178 MultiByteToWideChar( CP_ACP, 0, regeditPath,
179 strlen(regeditPath)+1, regeditPathW,
180 sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
182 SetLastError(0xdeadbeef);
183 memset(&subject, 1, sizeof(GUID));
184 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
185 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
186 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
187 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
189 /* The same thing but now with a handle instead of a filename */
190 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
191 SetLastError(0xdeadbeef);
192 memset(&subject, 1, sizeof(GUID));
193 ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
194 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
195 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
196 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
197 CloseHandle(file);
199 /* And both */
200 file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
201 SetLastError(0xdeadbeef);
202 memset(&subject, 1, sizeof(GUID));
203 ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
204 ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
205 ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
206 "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
207 CloseHandle(file);
209 /* Now with an empty file */
210 GetTempPathA(sizeof(path), path);
211 GetTempFileNameA(path, "sip", 0 , tempfile);
212 MultiByteToWideChar( CP_ACP, 0, tempfile,
213 strlen(tempfile)+1, tempfileW,
214 sizeof(tempfileW)/sizeof(tempfileW[0]) );
216 SetLastError(0xdeadbeef);
217 memset(&subject, 1, sizeof(GUID));
218 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
219 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
220 ok ( GetLastError() == ERROR_FILE_INVALID ||
221 GetLastError() == ERROR_INVALID_PARAMETER /* Vista */ ||
222 GetLastError() == ERROR_SUCCESS /* Win98 */,
223 "Expected ERROR_FILE_INVALID, ERROR_INVALID_PARAMETER or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
224 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
225 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
227 /* Use a file with a size of 3 (at least < 4) */
228 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
229 WriteFile(file, "123", 3, &written, NULL);
230 CloseHandle(file);
232 SetLastError(0xdeadbeef);
233 memset(&subject, 1, sizeof(GUID));
234 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
235 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
236 ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
237 GetLastError() == ERROR_SUCCESS /* Win98 */,
238 "Expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
239 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
240 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
242 /* And now >= 4 */
243 file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
244 WriteFile(file, "1234", 4, &written, NULL);
245 CloseHandle(file);
247 SetLastError(0xdeadbeef);
248 memset(&subject, 1, sizeof(GUID));
249 ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
250 ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
251 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
252 GetLastError() == ERROR_SUCCESS /* Win98 */,
253 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or ERROR_SUCCESS, got 0x%08x\n", GetLastError());
254 ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
255 "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
257 /* Clean up */
258 DeleteFileA(tempfile);
261 static void test_SIPLoad(void)
263 BOOL ret;
264 GUID subject;
265 static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
266 static GUID unknown = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
267 static GUID unknown2 = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }}; /* WINTRUST.DLL */
268 /* The next SIP is available on Windows (not on a clean Wine install) */
269 static GUID unknown3 = { 0x000C10F1, 0x0000, 0x0000, { 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }}; /* MSISIP.DLL */
270 SIP_DISPATCH_INFO sdi;
271 HMODULE hCrypt;
273 /* All NULL */
274 SetLastError(0xdeadbeef);
275 ret = CryptSIPLoad(NULL, 0, NULL);
276 ok ( !ret, "Expected CryptSIPLoad to fail\n");
277 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
278 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
280 /* Only pSipDispatch NULL */
281 SetLastError(0xdeadbeef);
282 ret = CryptSIPLoad(&subject, 0, NULL);
283 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");
294 ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
295 "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08x\n", GetLastError());
296 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
298 hCrypt = GetModuleHandleA("crypt32.dll");
299 funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
300 funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
301 funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
302 funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
303 funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
305 /* All OK */
306 SetLastError(0xdeadbeef);
307 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
308 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
309 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
310 ret = CryptSIPLoad(&unknown, 0, &sdi);
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");
319 /* The function addresses returned by CryptSIPLoad are actually the addresses of
320 * crypt32's own functions. A function calling these addresses will end up first
321 * calling crypt32 functions which in its turn call the equivalent in the SIP
322 * as dictated by the given GUID.
324 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
325 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
326 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
327 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
328 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
329 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
330 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
331 "Expected function addresses to be from crypt32\n");
332 else
333 trace("Couldn't load function pointers\n");
335 /* All OK, but different GUID (same SIP though) */
336 SetLastError(0xdeadbeef);
337 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
338 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
339 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
340 ret = CryptSIPLoad(&unknown2, 0, &sdi);
341 ok ( ret, "Expected CryptSIPLoad to succeed\n");
342 /* This call on its own would have resulted in an ERROR_PROC_NOT_FOUND, but the previous
343 * call to CryptSIPLoad already loaded wintrust.dll. As this information is cached,
344 * CryptSIPLoad will not try to search for the already mentioned DllCanUnloadNow.
346 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
348 /* All OK, but other SIP */
349 SetLastError(0xdeadbeef);
350 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
351 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
352 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
353 ret = CryptSIPLoad(&unknown3, 0, &sdi);
354 if (ret)
356 /* The SIP is known so we can safely assume that the next tests can be done */
358 /* As msisip.dll is not checked yet by any of the previous calls, the
359 * function DllCanUnloadNow will be checked again in msisip.dll (it's not present)
361 todo_wine
363 ok( sdi.pfGet != (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected a function pointer to be loaded.\n");
365 /* This is another SIP but this test proves the function addresses are the same as
366 * in the previous test.
368 if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
369 funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
370 ok (sdi.pfGet == funcCryptSIPGetSignedDataMsg &&
371 sdi.pfPut == funcCryptSIPPutSignedDataMsg &&
372 sdi.pfCreate == funcCryptSIPCreateIndirectData &&
373 sdi.pfVerify == funcCryptSIPVerifyIndirectData &&
374 sdi.pfRemove == funcCryptSIPRemoveSignedDataMsg,
375 "Expected function addresses to be from crypt32\n");
376 else
377 trace("Couldn't load function pointers\n");
381 /* Reserved parameter not 0 */
382 SetLastError(0xdeadbeef);
383 memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
384 sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
385 sdi.pfGet = (pCryptSIPGetSignedDataMsg)0xdeadbeef;
386 ret = CryptSIPLoad(&unknown, 1, &sdi);
387 ok ( !ret, "Expected CryptSIPLoad to fail\n");
388 ok ( GetLastError() == ERROR_INVALID_PARAMETER,
389 "Expected ERROR_INVALID_PARAMETER, got 0x%08x\n", GetLastError());
390 ok( sdi.pfGet == (pCryptSIPGetSignedDataMsg)0xdeadbeef, "Expected no change to the function pointer\n");
393 START_TEST(sip)
395 test_AddRemoveProvider();
396 /* It seems that the caching for loaded dlls is shared between CryptSIPRetrieveSubjectGUID
397 * and CryptSIPLoad. The tests have to be in this order to succeed. This is because in the last
398 * test for CryptSIPRetrieveSubjectGUID, several SIPs will be loaded (on Windows).
400 test_SIPLoad();
401 test_SIPRetrieveSubjectGUID();