1 /* Unit test suite for wintrust API functions
3 * Copyright 2006 Paul Vriens
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/test.h"
31 static BOOL (WINAPI * pWintrustAddActionID)(GUID*, DWORD, CRYPT_REGISTER_ACTIONID*);
32 static BOOL (WINAPI * pWintrustAddDefaultForUsage)(const CHAR*,CRYPT_PROVIDER_REGDEFUSAGE*);
33 static void (WINAPI * pWintrustGetRegPolicyFlags)(DWORD *);
34 static BOOL (WINAPI * pWintrustLoadFunctionPointers)(GUID *, CRYPT_PROVIDER_FUNCTIONS *);
35 static BOOL (WINAPI * pWintrustRemoveActionID)(GUID*);
36 static BOOL (WINAPI * pWintrustSetRegPolicyFlags)(DWORD);
38 static void InitFunctionPtrs(void)
40 HMODULE hWintrust = GetModuleHandleA("wintrust.dll");
42 #define WINTRUST_GET_PROC(func) \
43 p ## func = (void*)GetProcAddress(hWintrust, #func); \
45 trace("GetProcAddress(%s) failed\n", #func);
47 WINTRUST_GET_PROC(WintrustAddActionID)
48 WINTRUST_GET_PROC(WintrustAddDefaultForUsage)
49 WINTRUST_GET_PROC(WintrustGetRegPolicyFlags)
50 WINTRUST_GET_PROC(WintrustLoadFunctionPointers)
51 WINTRUST_GET_PROC(WintrustRemoveActionID)
52 WINTRUST_GET_PROC(WintrustSetRegPolicyFlags)
54 #undef WINTRUST_GET_PROC
57 static void test_AddRem_ActionID(void)
59 static WCHAR DummyDllW[] = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
60 static WCHAR DummyFunctionW[] = {'d','u','m','m','y','f','u','n','c','t','i','o','n',0 };
61 GUID ActionID = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
62 CRYPT_REGISTER_ACTIONID ActionIDFunctions;
63 CRYPT_TRUST_REG_ENTRY EmptyProvider = { 0, NULL, NULL };
64 CRYPT_TRUST_REG_ENTRY DummyProvider = { sizeof(CRYPT_TRUST_REG_ENTRY), DummyDllW, DummyFunctionW };
67 if (!pWintrustAddActionID || !pWintrustRemoveActionID)
69 skip("WintrustAddActionID and/or WintrustRemoveActionID are not available\n");
74 SetLastError(0xdeadbeef);
75 ret = pWintrustAddActionID(NULL, 0, NULL);
76 ok (!ret, "Expected WintrustAddActionID to fail.\n");
77 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
78 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
79 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
82 SetLastError(0xdeadbeef);
83 ret = pWintrustAddActionID(&ActionID, 0, NULL);
84 ok (!ret, "Expected WintrustAddActionID to fail.\n");
85 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
86 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
87 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
89 /* All OK (although no functions defined), except cbStruct is not set in ActionIDFunctions */
90 SetLastError(0xdeadbeef);
91 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
92 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
93 ok (!ret, "Expected WintrustAddActionID to fail.\n");
94 ok (GetLastError() == ERROR_INVALID_PARAMETER /* XP/W2K3 */ ||
95 GetLastError() == 0xdeadbeef /* Win98/NT4/W2K */,
96 "Expected ERROR_INVALID_PARAMETER(W2K3) or 0xdeadbeef(Win98/NT4/W2K), got %u.\n", GetLastError());
98 /* All OK (although no functions defined) and cbStruct is set now */
99 SetLastError(0xdeadbeef);
100 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
101 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
102 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
103 ok (ret, "Expected WintrustAddActionID to succeed.\n");
104 ok (GetLastError() == ERROR_INVALID_PARAMETER,
105 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
107 /* All OK and all (but 1) functions are correctly defined. The DLL and entrypoints
110 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
111 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
112 ActionIDFunctions.sInitProvider = DummyProvider;
113 ActionIDFunctions.sObjectProvider = DummyProvider;
114 ActionIDFunctions.sSignatureProvider = EmptyProvider;
115 ActionIDFunctions.sCertificateProvider = DummyProvider;
116 ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
117 ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
118 ActionIDFunctions.sTestPolicyProvider = DummyProvider;
119 ActionIDFunctions.sCleanupProvider = DummyProvider;
120 SetLastError(0xdeadbeef);
121 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
122 ok (ret, "Expected WintrustAddActionID to succeed.\n");
123 ok (GetLastError() == ERROR_INVALID_PARAMETER,
124 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
126 /* All OK and all functions are correctly defined. The DLL and entrypoints
129 memset(&ActionIDFunctions, 0, sizeof(CRYPT_REGISTER_ACTIONID));
130 ActionIDFunctions.cbStruct = sizeof(CRYPT_REGISTER_ACTIONID);
131 ActionIDFunctions.sInitProvider = DummyProvider;
132 ActionIDFunctions.sObjectProvider = DummyProvider;
133 ActionIDFunctions.sSignatureProvider = DummyProvider;
134 ActionIDFunctions.sCertificateProvider = DummyProvider;
135 ActionIDFunctions.sCertificatePolicyProvider = DummyProvider;
136 ActionIDFunctions.sFinalPolicyProvider = DummyProvider;
137 ActionIDFunctions.sTestPolicyProvider = DummyProvider;
138 ActionIDFunctions.sCleanupProvider = DummyProvider;
139 SetLastError(0xdeadbeef);
140 ret = pWintrustAddActionID(&ActionID, 0, &ActionIDFunctions);
141 ok (ret, "Expected WintrustAddActionID to succeed.\n");
142 ok (GetLastError() == 0xdeadbeef,
143 "Expected 0xdeadbeef, got %u.\n", GetLastError());
145 SetLastError(0xdeadbeef);
146 ret = pWintrustRemoveActionID(&ActionID);
147 ok ( ret, "WintrustRemoveActionID failed : %d\n", GetLastError());
148 ok ( GetLastError() == 0xdeadbeef, "Last error should not have been changed: %u\n", GetLastError());
151 SetLastError(0xdeadbeef);
152 ret = pWintrustRemoveActionID(NULL);
153 ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
154 ok (GetLastError() == ERROR_INVALID_PARAMETER,
155 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
157 /* The passed GUID is removed by a previous call, so it's basically a test with a nonexistent Trust provider */
158 SetLastError(0xdeadbeef);
159 ret = pWintrustRemoveActionID(&ActionID);
160 ok (ret, "Expected WintrustRemoveActionID to succeed.\n");
161 ok (GetLastError() == 0xdeadbeef,
162 "Expected 0xdeadbeef, got %u.\n", GetLastError());
165 static void test_AddDefaultForUsage(void)
169 static GUID ActionID = { 0xdeadbeef, 0xdead, 0xbeef, { 0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef }};
170 static WCHAR DummyDllW[] = {'d','e','a','d','b','e','e','f','.','d','l','l',0 };
171 static CHAR DummyFunction[] = "dummyfunction";
172 static const CHAR oid[] = "1.2.3.4.5.6.7.8.9.10";
173 static const CHAR Usages[] = "SOFTWARE\\Microsoft\\Cryptography\\Providers\\Trust\\Usages\\1.2.3.4.5.6.7.8.9.10";
174 static CRYPT_PROVIDER_REGDEFUSAGE DefUsage;
176 if (!pWintrustAddDefaultForUsage)
178 skip("WintrustAddDefaultForUsage is not available\n");
183 SetLastError(0xdeadbeef);
184 ret = pWintrustAddDefaultForUsage(NULL, NULL);
185 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
186 ok (GetLastError() == ERROR_INVALID_PARAMETER,
187 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
190 SetLastError(0xdeadbeef);
191 ret = pWintrustAddDefaultForUsage(oid, NULL);
192 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
193 ok (GetLastError() == ERROR_INVALID_PARAMETER,
194 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
196 /* NULL oid and proper defusage */
197 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
198 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
199 DefUsage.pgActionID = &ActionID;
200 DefUsage.pwszDllName = DummyDllW;
201 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
202 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
203 SetLastError(0xdeadbeef);
204 ret = pWintrustAddDefaultForUsage(NULL, &DefUsage);
205 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
206 ok (GetLastError() == ERROR_INVALID_PARAMETER,
207 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
209 /* Just the ActionID */
210 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
211 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
212 DefUsage.pgActionID = &ActionID;
213 SetLastError(0xdeadbeef);
214 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
215 ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
216 ok (GetLastError() == 0xdeadbeef,
217 "Last error should not have been changed: %u\n", GetLastError());
220 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
221 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
222 DefUsage.pwszDllName = DummyDllW;
223 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
224 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
225 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
226 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
227 ok (GetLastError() == ERROR_INVALID_PARAMETER,
228 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
230 /* cbStruct set to 0 */
231 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
232 DefUsage.cbStruct = 0;
233 DefUsage.pgActionID = &ActionID;
234 DefUsage.pwszDllName = DummyDllW;
235 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
236 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
237 SetLastError(0xdeadbeef);
238 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
239 ok (!ret, "Expected WintrustAddDefaultForUsage to fail.\n");
240 ok (GetLastError() == ERROR_INVALID_PARAMETER,
241 "Expected ERROR_INVALID_PARAMETER, got %u.\n", GetLastError());
244 memset(&DefUsage, 0 , sizeof(CRYPT_PROVIDER_REGDEFUSAGE));
245 DefUsage.cbStruct = sizeof(CRYPT_PROVIDER_REGDEFUSAGE);
246 DefUsage.pgActionID = &ActionID;
247 DefUsage.pwszDllName = DummyDllW;
248 DefUsage.pwszLoadCallbackDataFunctionName = DummyFunction;
249 DefUsage.pwszFreeCallbackDataFunctionName = DummyFunction;
250 SetLastError(0xdeadbeef);
251 ret = pWintrustAddDefaultForUsage(oid, &DefUsage);
252 ok ( ret, "Expected WintrustAddDefaultForUsage to succeed\n");
253 ok (GetLastError() == 0xdeadbeef,
254 "Last error should not have been changed: %u\n", GetLastError());
256 /* There is no corresponding remove for WintrustAddDefaultForUsage
257 * so we delete the registry key manually.
261 res = RegDeleteKeyA(HKEY_LOCAL_MACHINE, Usages);
262 ok (res == ERROR_SUCCESS, "Key delete failed : 0x%08x\n", res);
266 static void test_LoadFunctionPointers(void)
269 CRYPT_PROVIDER_FUNCTIONS funcs;
270 GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2;
272 if (!pWintrustLoadFunctionPointers)
274 skip("WintrustLoadFunctionPointers is not available\n");
277 SetLastError(0xdeadbeef);
278 ret = pWintrustLoadFunctionPointers(NULL, NULL);
279 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
280 SetLastError(0xdeadbeef);
281 ret = pWintrustLoadFunctionPointers(&action, NULL);
282 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
284 SetLastError(0xdeadbeef);
285 ret = pWintrustLoadFunctionPointers(NULL, &funcs);
286 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
287 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
289 SetLastError(0xdeadbeef);
291 ret = pWintrustLoadFunctionPointers(&action, &funcs);
292 ok(!ret && GetLastError() == 0xdeadbeef, "Expected failure\n");
293 SetLastError(0xdeadbeef);
294 funcs.cbStruct = sizeof(funcs);
295 ret = pWintrustLoadFunctionPointers(&action, &funcs);
296 ok(ret, "WintrustLoadFunctionPointers failed: %d\n", GetLastError());
297 ok(funcs.pfnAlloc != NULL, "Expected a pointer\n");
298 ok(funcs.pfnFree != NULL, "Expected a pointer\n");
301 static void test_RegPolicyFlags(void)
303 /* Default state value 0x00023c00, which is
304 * WTPF_IGNOREREVOCATIONONTS |
305 * WTPF_OFFLINEOKNBU_COM |
306 * WTPF_OFFLINEOKNBU_IND |
307 * WTPF_OFFLINEOK_COM |
310 static const CHAR Software_Publishing[] =
311 "Software\\Microsoft\\Windows\\CurrentVersion\\Wintrust\\"
312 "Trust Providers\\Software Publishing";
313 static const CHAR State[] = "State";
316 DWORD flags1, flags2, flags3, size;
319 if (!pWintrustGetRegPolicyFlags || !pWintrustSetRegPolicyFlags)
321 skip("Policy flags functions not present\n");
325 pWintrustGetRegPolicyFlags(&flags2);
327 r = RegOpenKeyExA(HKEY_CURRENT_USER, Software_Publishing, 0, KEY_ALL_ACCESS,
329 ok(!r, "RegOpenKeyEx failed: %d\n", r);
331 size = sizeof(flags1);
332 r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
333 ok(!r, "RegQueryValueEx failed: %d\n", r);
335 ok(flags1 == flags2, "Got %08x flags instead of %08x\n", flags1, flags2);
338 ret = pWintrustSetRegPolicyFlags(flags3);
339 ok(ret, "WintrustSetRegPolicyFlags failed: %d\n", GetLastError());
340 size = sizeof(flags1);
341 r = RegQueryValueExA(key, State, NULL, NULL, (LPBYTE)&flags1, &size);
342 ok(flags1 == flags3, "Got %08x flags instead of %08x\n", flags1, flags3);
344 pWintrustSetRegPolicyFlags(flags2);
353 test_AddRem_ActionID();
354 test_AddDefaultForUsage();
355 test_LoadFunctionPointers();
356 test_RegPolicyFlags();