crypt32: Partly implement CryptSIPRetrieveSubjectGuid.
[wine] / dlls / crypt32 / tests / sip.c
1 /*
2  * Subject Interface Package tests
3  *
4  * Copyright 2006 Paul Vriens
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 #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>
29
30 #include "wine/test.h"
31
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);
37
38 static char *show_guid(const GUID *guid)
39 {
40     static char guidstring[39];
41
42     sprintf(guidstring,
43         "{%08lx-%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] );
47
48     return guidstring;
49 }
50
51 static void test_AddRemoveProvider(void)
52 {
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 };
58
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 %ld.\n", GetLastError());
65
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 %ld.\n", GetLastError());
72
73     /* Everything OK, pwszIsFunctionName and pwszIsFunctionNameFmt2 are left NULL
74      * as allowed */
75
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 %ld\n",
89      GetLastError());
90
91     /* Dummy provider will be deleted, but the function still fails because
92      * pwszIsFunctionName and pwszIsFunctionNameFmt2 are not present in the
93      * registry.
94      */
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 %ld.\n", GetLastError());
100
101     /* Everything OK */
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 %ld\n",
117      GetLastError());
118
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 %ld\n",
124      GetLastError());
125 }
126
127 static void test_SIPRetrieveSubjectGUID(void)
128 {
129     BOOL ret;
130     GUID subject;
131     HANDLE file;
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];
143     DWORD written;
144
145     /* NULL check */
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 %ld.\n", GetLastError());
151
152     /* Test with a non-existent file (hopefully) */
153     SetLastError(0xdeadbeef);
154     /* Set subject to something other than zero's */
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 %ld.\n", GetLastError());
160     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
161         "Expected a NULL GUID for c:\\deadbeef.dbf, not %s\n", show_guid(&subject));
162
163     /* Now with an executable that should exist
164      *
165      * Use A-functions where possible as that should be available on all platforms
166      */
167     GetEnvironmentVariableA(windir, regeditPath, MAX_PATH);
168     sprintf(regeditPath, "%s\\%s", regeditPath, regeditExe);
169     MultiByteToWideChar( CP_ACP, 0, regeditPath,
170                          strlen(regeditPath)+1, regeditPathW,
171                          sizeof(regeditPathW)/sizeof(regeditPathW[0]) );
172
173     SetLastError(0xdeadbeef);
174     memset(&subject, 1, sizeof(GUID));
175     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, NULL, &subject);
176     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
177     ok ( GetLastError() == ERROR_SUCCESS,
178         "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
179     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
180         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
181
182     /* The same thing but now with a handle instead of a filename */
183     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
184     SetLastError(0xdeadbeef);
185     memset(&subject, 1, sizeof(GUID));
186     ret = CryptSIPRetrieveSubjectGuid(NULL, file, &subject);
187     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
188     ok ( GetLastError() == ERROR_SUCCESS,
189         "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
190     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
191         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
192     CloseHandle(file);
193
194     /* And both */
195     file = CreateFileA(regeditPath, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
196     SetLastError(0xdeadbeef);
197     memset(&subject, 1, sizeof(GUID));
198     ret = CryptSIPRetrieveSubjectGuid(regeditPathW, file, &subject);
199     ok ( ret, "Expected CryptSIPRetrieveSubjectGuid to succeed\n");
200     ok ( GetLastError() == ERROR_SUCCESS,
201         "Expected ERROR_SUCCESS, got 0x%08lx\n", GetLastError());
202     ok ( !memcmp(&subject, &unknownGUID, sizeof(GUID)),
203         "Expected (%s), got (%s).\n", show_guid(&unknownGUID), show_guid(&subject));
204     CloseHandle(file);
205
206     /* Now with an empty file */
207     GetTempPathA(sizeof(path), path);
208     GetTempFileNameA(path, "sip", 0 , tempfile);
209     MultiByteToWideChar( CP_ACP, 0, tempfile,
210                          strlen(tempfile)+1, tempfileW,
211                          sizeof(tempfileW)/sizeof(tempfileW[0]) );
212
213     SetLastError(0xdeadbeef);
214     memset(&subject, 1, sizeof(GUID));
215     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
216     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
217     ok ( GetLastError() == ERROR_FILE_INVALID ||
218          GetLastError() == S_OK /* Win98 */,
219         "Expected ERROR_FILE_INVALID or S_OK, got 0x%08lx\n", GetLastError());
220     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
221         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
222
223     /* Use a file with a size of 3 (at least < 4) */
224     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
225     WriteFile(file, "123", 3, &written, NULL);
226     CloseHandle(file);
227
228     SetLastError(0xdeadbeef);
229     memset(&subject, 1, sizeof(GUID));
230     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
231     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
232     ok ( GetLastError() == ERROR_INVALID_PARAMETER ||
233          GetLastError() == S_OK /* Win98 */,
234         "Expected ERROR_INVALID_PARAMETER or S_OK, got 0x%08lx\n", GetLastError());
235     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
236         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
237
238     /* And now >= 4 */
239     file = CreateFileA(tempfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
240     WriteFile(file, "1234", 4, &written, NULL);
241     CloseHandle(file);
242
243     SetLastError(0xdeadbeef);
244     memset(&subject, 1, sizeof(GUID));
245     ret = CryptSIPRetrieveSubjectGuid(tempfileW, NULL, &subject);
246     ok ( !ret, "Expected CryptSIPRetrieveSubjectGuid to fail\n");
247     ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN ||
248          GetLastError() == S_OK /* Win98 */,
249         "Expected TRUST_E_SUBJECT_FORM_UNKNOWN or S_OK, got 0x%08lx\n", GetLastError());
250     ok ( !memcmp(&subject, &nullSubject, sizeof(GUID)),
251         "Expected a NULL GUID for empty file %s, not %s\n", tempfile, show_guid(&subject));
252
253     /* Clean up */
254     DeleteFileA(tempfile);
255 }
256
257 static void test_SIPLoad(void)
258 {
259     BOOL ret;
260     GUID subject;
261     static GUID dummySubject = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
262     static GUID unknown      = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
263     SIP_DISPATCH_INFO sdi;
264     HMODULE hCrypt;
265
266     /* All NULL */
267     SetLastError(0xdeadbeef);
268     ret = CryptSIPLoad(NULL, 0, NULL);
269     ok ( !ret, "Expected CryptSIPLoad to fail\n");
270     todo_wine
271         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
272             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
273
274     /* Only pSipDispatch NULL */
275     SetLastError(0xdeadbeef);
276     ret = CryptSIPLoad(&subject, 0, NULL);
277     ok ( !ret, "Expected CryptSIPLoad to fail\n");
278     todo_wine
279         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
280             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
281
282     /* No NULLs, but nonexistent pgSubject */
283     SetLastError(0xdeadbeef);
284     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
285     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
286     ret = CryptSIPLoad(&dummySubject, 0, &sdi);
287     ok ( !ret, "Expected CryptSIPLoad to fail\n");
288     todo_wine
289         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
290             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08lx\n", GetLastError());
291
292     /* cbSize not initialized */
293     SetLastError(0xdeadbeef);
294     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
295     ret = CryptSIPLoad(&dummySubject, 0, &sdi);
296     ok ( !ret, "Expected CryptSIPLoad to fail\n");
297     todo_wine
298         ok ( GetLastError() == TRUST_E_SUBJECT_FORM_UNKNOWN,
299             "Expected TRUST_E_SUBJECT_FORM_UNKNOWN, got 0x%08lx\n", GetLastError());
300
301     /* cbSize not initialized, but valid subject (named unknown but registered by wintrust) */
302     SetLastError(0xdeadbeef);
303     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
304     ret = CryptSIPLoad(&unknown, 0, &sdi);
305     todo_wine
306     {
307         ok ( ret, "Expected CryptSIPLoad to succeed\n");
308         ok ( GetLastError() == ERROR_PROC_NOT_FOUND,
309             "Expected ERROR_PROC_NOT_FOUND, got 0x%08lx\n", GetLastError());
310     }
311
312     /* All OK */
313     SetLastError(0xdeadbeef);
314     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
315     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
316     ret = CryptSIPLoad(&unknown, 0, &sdi);
317     todo_wine
318         ok ( ret, "Expected CryptSIPLoad to succeed\n");
319     ok ( GetLastError() == 0xdeadbeef,
320         "Expected 0xdeadbeef, got 0x%08lx\n", GetLastError());
321
322     /* The function addresses returned by CryptSIPLoad are actually the addresses of
323      * crypt32's own functions. A function calling these addresses will end up first
324      * calling crypt32 functions which in it's turn call the equivalent in the SIP
325      * as dictated by the given GUID.
326      */
327     hCrypt = LoadLibrary("crypt32.dll");
328     if (hCrypt)
329     {
330         funcCryptSIPGetSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPGetSignedDataMsg");
331         funcCryptSIPPutSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPPutSignedDataMsg");
332         funcCryptSIPCreateIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPCreateIndirectData");
333         funcCryptSIPVerifyIndirectData = (void*)GetProcAddress(hCrypt, "CryptSIPVerifyIndirectData");
334         funcCryptSIPRemoveSignedDataMsg = (void*)GetProcAddress(hCrypt, "CryptSIPRemoveSignedDataMsg");
335         if (funcCryptSIPGetSignedDataMsg && funcCryptSIPPutSignedDataMsg && funcCryptSIPCreateIndirectData &&
336             funcCryptSIPVerifyIndirectData && funcCryptSIPRemoveSignedDataMsg)
337             todo_wine
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");
344         else
345             trace("Couldn't load function pointers\n");
346  
347         FreeLibrary(hCrypt);
348     }
349
350     /* Reserved parameter not 0 */
351     SetLastError(0xdeadbeef);
352     memset(&sdi, 0, sizeof(SIP_DISPATCH_INFO));
353     sdi.cbSize = sizeof(SIP_DISPATCH_INFO);
354     ret = CryptSIPLoad(&unknown, 1, &sdi);
355     ok ( !ret, "Expected CryptSIPLoad to fail\n");
356     todo_wine
357         ok ( GetLastError() == ERROR_INVALID_PARAMETER,
358             "Expected ERROR_INVALID_PARAMETER, got 0x%08lx\n", GetLastError());
359 }
360
361 START_TEST(sip)
362 {
363     test_AddRemoveProvider();
364     test_SIPRetrieveSubjectGUID();
365     test_SIPLoad();
366 }