kernel32/tests: Add tests for console codepages.
[wine] / dlls / wintrust / register.c
1 /*
2  * Register related wintrust functions
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 <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winnls.h"
29
30 #include "guiddef.h"
31 #include "wintrust.h"
32 #include "softpub.h"
33 #include "mssip.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
38
39 static CRYPT_TRUST_REG_ENTRY SoftpubInitialization;
40 static CRYPT_TRUST_REG_ENTRY SoftpubMessage;
41 static CRYPT_TRUST_REG_ENTRY SoftpubSignature;
42 static CRYPT_TRUST_REG_ENTRY SoftpubCertficate;
43 static CRYPT_TRUST_REG_ENTRY SoftpubCertCheck;
44 static CRYPT_TRUST_REG_ENTRY SoftpubFinalPolicy;
45 static CRYPT_TRUST_REG_ENTRY SoftpubCleanup;
46
47 static CRYPT_TRUST_REG_ENTRY SoftpubDefCertInit;
48
49 static CRYPT_TRUST_REG_ENTRY SoftpubDumpStructure;
50
51 static CRYPT_TRUST_REG_ENTRY HTTPSCertificateTrust;
52 static CRYPT_TRUST_REG_ENTRY HTTPSFinalProv;
53
54 static CRYPT_TRUST_REG_ENTRY OfficeInitializePolicy;
55 static CRYPT_TRUST_REG_ENTRY OfficeCleanupPolicy;
56
57 static CRYPT_TRUST_REG_ENTRY DriverInitializePolicy;
58 static CRYPT_TRUST_REG_ENTRY DriverFinalPolicy;
59 static CRYPT_TRUST_REG_ENTRY DriverCleanupPolicy;
60
61 static CRYPT_TRUST_REG_ENTRY GenericChainCertificateTrust;
62 static CRYPT_TRUST_REG_ENTRY GenericChainFinalProv;
63
64 static const WCHAR Trust[]            = {'S','o','f','t','w','a','r','e','\\',
65                                          'M','i','c','r','o','s','o','f','t','\\',
66                                          'C','r','y','p','t','o','g','r','a','p','h','y','\\',
67                                          'P','r','o','v','i','d','e','r','s','\\',
68                                          'T','r','u','s','t','\\', 0 };
69
70 static const WCHAR Initialization[]   = {'I','n','i','t','i','a','l','i','z','a','t','i','o','n','\\', 0};
71 static const WCHAR Message[]          = {'M','e','s','s','a','g','e','\\', 0};
72 static const WCHAR Signature[]        = {'S','i','g','n','a','t','u','r','e','\\', 0};
73 static const WCHAR Certificate[]      = {'C','e','r','t','i','f','i','c','a','t','e','\\', 0};
74 static const WCHAR CertCheck[]        = {'C','e','r','t','C','h','e','c','k','\\', 0};
75 static const WCHAR FinalPolicy[]      = {'F','i','n','a','l','P','o','l','i','c','y','\\', 0};
76 static const WCHAR DiagnosticPolicy[] = {'D','i','a','g','n','o','s','t','i','c','P','o','l','i','c','y','\\', 0};
77 static const WCHAR Cleanup[]          = {'C','l','e','a','n','u','p','\\', 0};
78
79 static const WCHAR DefaultId[]        = {'D','e','f','a','u','l','t','I','d', 0};
80 static const WCHAR Dll[]              = {'$','D','L','L', 0};
81
82 /***********************************************************************
83  *              WINTRUST_InitRegStructs
84  *
85  * Helper function to allocate and initialize the members of the
86  * CRYPT_TRUST_REG_ENTRY structs.
87  */
88 static void WINTRUST_InitRegStructs(void)
89 {
90 #define WINTRUST_INITREGENTRY( action, dllname, functionname ) \
91     action.cbStruct = sizeof(CRYPT_TRUST_REG_ENTRY); \
92     action.pwszDLLName = HeapAlloc(GetProcessHeap(), 0, sizeof(dllname)); \
93     lstrcpyW(action.pwszDLLName, dllname); \
94     action.pwszFunctionName = HeapAlloc(GetProcessHeap(), 0, sizeof(functionname)); \
95     lstrcpyW(action.pwszFunctionName, functionname);
96
97     WINTRUST_INITREGENTRY(SoftpubInitialization, SP_POLICY_PROVIDER_DLL_NAME, SP_INIT_FUNCTION)
98     WINTRUST_INITREGENTRY(SoftpubMessage, SP_POLICY_PROVIDER_DLL_NAME, SP_OBJTRUST_FUNCTION)
99     WINTRUST_INITREGENTRY(SoftpubSignature, SP_POLICY_PROVIDER_DLL_NAME, SP_SIGTRUST_FUNCTION)
100     WINTRUST_INITREGENTRY(SoftpubCertficate, SP_POLICY_PROVIDER_DLL_NAME, WT_PROVIDER_CERTTRUST_FUNCTION)
101     WINTRUST_INITREGENTRY(SoftpubCertCheck, SP_POLICY_PROVIDER_DLL_NAME, SP_CHKCERT_FUNCTION)
102     WINTRUST_INITREGENTRY(SoftpubFinalPolicy, SP_POLICY_PROVIDER_DLL_NAME, SP_FINALPOLICY_FUNCTION)
103     WINTRUST_INITREGENTRY(SoftpubCleanup, SP_POLICY_PROVIDER_DLL_NAME, SP_CLEANUPPOLICY_FUNCTION)
104     WINTRUST_INITREGENTRY(SoftpubDefCertInit, SP_POLICY_PROVIDER_DLL_NAME, SP_GENERIC_CERT_INIT_FUNCTION)
105     WINTRUST_INITREGENTRY(SoftpubDumpStructure, SP_POLICY_PROVIDER_DLL_NAME, SP_TESTDUMPPOLICY_FUNCTION_TEST)
106     WINTRUST_INITREGENTRY(HTTPSCertificateTrust, SP_POLICY_PROVIDER_DLL_NAME, HTTPS_CERTTRUST_FUNCTION)
107     WINTRUST_INITREGENTRY(HTTPSFinalProv, SP_POLICY_PROVIDER_DLL_NAME, HTTPS_FINALPOLICY_FUNCTION)
108     WINTRUST_INITREGENTRY(OfficeInitializePolicy, OFFICE_POLICY_PROVIDER_DLL_NAME, OFFICE_INITPROV_FUNCTION)
109     WINTRUST_INITREGENTRY(OfficeCleanupPolicy, OFFICE_POLICY_PROVIDER_DLL_NAME, OFFICE_CLEANUPPOLICY_FUNCTION)
110     WINTRUST_INITREGENTRY(DriverInitializePolicy, SP_POLICY_PROVIDER_DLL_NAME, DRIVER_INITPROV_FUNCTION)
111     WINTRUST_INITREGENTRY(DriverFinalPolicy, SP_POLICY_PROVIDER_DLL_NAME, DRIVER_FINALPOLPROV_FUNCTION)
112     WINTRUST_INITREGENTRY(DriverCleanupPolicy, SP_POLICY_PROVIDER_DLL_NAME, DRIVER_CLEANUPPOLICY_FUNCTION)
113     WINTRUST_INITREGENTRY(GenericChainCertificateTrust, SP_POLICY_PROVIDER_DLL_NAME, GENERIC_CHAIN_CERTTRUST_FUNCTION)
114     WINTRUST_INITREGENTRY(GenericChainFinalProv, SP_POLICY_PROVIDER_DLL_NAME, GENERIC_CHAIN_FINALPOLICY_FUNCTION)
115
116 #undef WINTRUST_INITREGENTRY
117 }
118
119 /***********************************************************************
120  *              WINTRUST_FreeRegStructs
121  *
122  * Helper function to free 2 members of the CRYPT_TRUST_REG_ENTRY
123  * structs.
124  */
125 static void WINTRUST_FreeRegStructs(void)
126 {
127 #define WINTRUST_FREEREGENTRY( action ) \
128     HeapFree(GetProcessHeap(), 0, action.pwszDLLName); \
129     HeapFree(GetProcessHeap(), 0, action.pwszFunctionName);
130
131     WINTRUST_FREEREGENTRY(SoftpubInitialization);
132     WINTRUST_FREEREGENTRY(SoftpubMessage);
133     WINTRUST_FREEREGENTRY(SoftpubSignature);
134     WINTRUST_FREEREGENTRY(SoftpubCertficate);
135     WINTRUST_FREEREGENTRY(SoftpubCertCheck);
136     WINTRUST_FREEREGENTRY(SoftpubFinalPolicy);
137     WINTRUST_FREEREGENTRY(SoftpubCleanup);
138     WINTRUST_FREEREGENTRY(SoftpubDefCertInit);
139     WINTRUST_FREEREGENTRY(SoftpubDumpStructure);
140     WINTRUST_FREEREGENTRY(HTTPSCertificateTrust);
141     WINTRUST_FREEREGENTRY(HTTPSFinalProv);
142     WINTRUST_FREEREGENTRY(OfficeInitializePolicy);
143     WINTRUST_FREEREGENTRY(OfficeCleanupPolicy);
144     WINTRUST_FREEREGENTRY(DriverInitializePolicy);
145     WINTRUST_FREEREGENTRY(DriverFinalPolicy);
146     WINTRUST_FREEREGENTRY(DriverCleanupPolicy);
147     WINTRUST_FREEREGENTRY(GenericChainCertificateTrust);
148     WINTRUST_FREEREGENTRY(GenericChainFinalProv);
149
150 #undef WINTRUST_FREEREGENTRY
151 }
152
153 /***********************************************************************
154  *              WINTRUST_guid2wstr
155  *
156  * Create a wide-string from a GUID
157  *
158  */
159 static void WINTRUST_Guid2Wstr(const GUID* pgActionID, WCHAR* GuidString)
160
161     static const WCHAR wszFormat[] = {'{','%','0','8','l','X','-','%','0','4','X','-','%','0','4','X','-',
162                                       '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X','%','0','2','X','%','0','2',
163                                       'X','%','0','2','X','%','0','2','X','}', 0};
164
165     wsprintfW(GuidString, wszFormat, pgActionID->Data1, pgActionID->Data2, pgActionID->Data3,
166         pgActionID->Data4[0], pgActionID->Data4[1], pgActionID->Data4[2], pgActionID->Data4[3],
167         pgActionID->Data4[4], pgActionID->Data4[5], pgActionID->Data4[6], pgActionID->Data4[7]);
168 }
169
170 /***********************************************************************
171  *              WINTRUST_WriteProviderToReg
172  *
173  * Helper function for WintrustAddActionID
174  *
175  */
176 static LONG WINTRUST_WriteProviderToReg(WCHAR* GuidString,
177                                         const WCHAR* FunctionType,
178                                         CRYPT_TRUST_REG_ENTRY RegEntry)
179 {
180     static const WCHAR Function[] = {'$','F','u','n','c','t','i','o','n', 0};
181     WCHAR ProvKey[MAX_PATH];
182     HKEY Key;
183     LONG Res = ERROR_SUCCESS;
184
185     /* Create the needed key string */
186     ProvKey[0]='\0';
187     lstrcatW(ProvKey, Trust);
188     lstrcatW(ProvKey, FunctionType);
189     lstrcatW(ProvKey, GuidString);
190
191     if (!RegEntry.pwszDLLName || !RegEntry.pwszFunctionName)
192         return ERROR_INVALID_PARAMETER;
193
194     Res = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProvKey, 0, NULL, 0, KEY_WRITE, NULL, &Key, NULL);
195     if (Res != ERROR_SUCCESS) goto error_close_key;
196
197     /* Create the $DLL entry */
198     Res = RegSetValueExW(Key, Dll, 0, REG_SZ, (BYTE*)RegEntry.pwszDLLName,
199         (lstrlenW(RegEntry.pwszDLLName) + 1)*sizeof(WCHAR));
200     if (Res != ERROR_SUCCESS) goto error_close_key;
201
202     /* Create the $Function entry */
203     Res = RegSetValueExW(Key, Function, 0, REG_SZ, (BYTE*)RegEntry.pwszFunctionName,
204         (lstrlenW(RegEntry.pwszFunctionName) + 1)*sizeof(WCHAR));
205
206 error_close_key:
207     RegCloseKey(Key);
208
209     return Res;
210 }
211
212 /***********************************************************************
213  *              WintrustAddActionID (WINTRUST.@)
214  *
215  * Add the definitions of the actions a Trust provider can perform to
216  * the registry.
217  *
218  * PARAMS
219  *   pgActionID [I] Pointer to a GUID for the Trust provider.
220  *   fdwFlags   [I] Flag to indicate whether registry errors are passed on.
221  *   psProvInfo [I] Pointer to a structure with information about DLL
222  *                  name and functions.
223  *
224  * RETURNS
225  *   Success: TRUE.
226  *   Failure: FALSE. (Use GetLastError() for more information)
227  *
228  * NOTES
229  *   Adding definitions is basically only adding relevant information
230  *   to the registry. No verification takes place whether a DLL or it's
231  *   entrypoints exist.
232  *   Information in the registry will always be overwritten.
233  *
234  */
235 BOOL WINAPI WintrustAddActionID( GUID* pgActionID, DWORD fdwFlags,
236                                  CRYPT_REGISTER_ACTIONID* psProvInfo)
237 {
238     WCHAR GuidString[39];
239     LONG Res;
240     LONG WriteActionError = ERROR_SUCCESS;
241
242     TRACE("%s %x %p\n", debugstr_guid(pgActionID), fdwFlags, psProvInfo);
243
244     /* Some sanity checks.
245      * We use the W2K3 last error as it makes more sense (W2K leaves the last error
246      * as is).
247      */
248     if (!pgActionID ||
249         !psProvInfo ||
250         (psProvInfo->cbStruct != sizeof(CRYPT_REGISTER_ACTIONID)))
251     {
252         SetLastError(ERROR_INVALID_PARAMETER);
253         return FALSE;
254     }
255
256     /* Create this string only once, instead of in the helper function */
257     WINTRUST_Guid2Wstr( pgActionID, GuidString);
258
259     /* Write the information to the registry */
260     Res = WINTRUST_WriteProviderToReg(GuidString, Initialization  , psProvInfo->sInitProvider);
261     if (Res != ERROR_SUCCESS) WriteActionError = Res;
262     Res = WINTRUST_WriteProviderToReg(GuidString, Message         , psProvInfo->sObjectProvider);
263     if (Res != ERROR_SUCCESS) WriteActionError = Res;
264     Res = WINTRUST_WriteProviderToReg(GuidString, Signature       , psProvInfo->sSignatureProvider);
265     if (Res != ERROR_SUCCESS) WriteActionError = Res;
266     Res = WINTRUST_WriteProviderToReg(GuidString, Certificate     , psProvInfo->sCertificateProvider);
267     if (Res != ERROR_SUCCESS) WriteActionError = Res;
268     Res = WINTRUST_WriteProviderToReg(GuidString, CertCheck       , psProvInfo->sCertificatePolicyProvider);
269     if (Res != ERROR_SUCCESS) WriteActionError = Res;
270     Res = WINTRUST_WriteProviderToReg(GuidString, FinalPolicy     , psProvInfo->sFinalPolicyProvider);
271     if (Res != ERROR_SUCCESS) WriteActionError = Res;
272     Res = WINTRUST_WriteProviderToReg(GuidString, DiagnosticPolicy, psProvInfo->sTestPolicyProvider);
273     if (Res != ERROR_SUCCESS) WriteActionError = Res;
274     Res = WINTRUST_WriteProviderToReg(GuidString, Cleanup         , psProvInfo->sCleanupProvider);
275     if (Res != ERROR_SUCCESS) WriteActionError = Res;
276
277     /* Testing (by restricting access to the registry for some keys) shows that the last failing function
278      * will be used for last error.
279      * If the flag WT_ADD_ACTION_ID_RET_RESULT_FLAG is set and there are errors when adding the action
280      * we have to return FALSE. Errors includes both invalid entries as well as registry errors.
281      * Testing also showed that one error doesn't stop the registry writes. Every action will be dealt with.
282      */
283
284     if (WriteActionError != ERROR_SUCCESS)
285     {
286         SetLastError(WriteActionError);
287
288         if (fdwFlags == WT_ADD_ACTION_ID_RET_RESULT_FLAG)
289             return FALSE;
290     }
291
292     return TRUE;
293 }
294
295 /***********************************************************************
296  *              WINTRUST_RemoveProviderFromReg
297  *
298  * Helper function for WintrustRemoveActionID
299  *
300  */
301 static void WINTRUST_RemoveProviderFromReg(WCHAR* GuidString,
302                                            const WCHAR* FunctionType)
303 {
304     WCHAR ProvKey[MAX_PATH];
305
306     /* Create the needed key string */
307     ProvKey[0]='\0';
308     lstrcatW(ProvKey, Trust);
309     lstrcatW(ProvKey, FunctionType);
310     lstrcatW(ProvKey, GuidString);
311
312     /* We don't care about success or failure */
313     RegDeleteKeyW(HKEY_LOCAL_MACHINE, ProvKey);
314 }
315
316 /***********************************************************************
317  *              WintrustRemoveActionID (WINTRUST.@)
318  *
319  * Remove the definitions of the actions a Trust provider can perform
320  * from the registry.
321  *
322  * PARAMS
323  *   pgActionID [I] Pointer to a GUID for the Trust provider.
324  *
325  * RETURNS
326  *   Success: TRUE. (Use GetLastError() for more information)
327  *   Failure: FALSE. (Use GetLastError() for more information)
328  *
329  * NOTES
330  *   Testing shows that WintrustRemoveActionID always returns TRUE and
331  *   that a possible error should be retrieved via GetLastError().
332  *   There are no checks if the definitions are in the registry.
333  */
334 BOOL WINAPI WintrustRemoveActionID( GUID* pgActionID )
335 {
336     WCHAR GuidString[39];
337
338     TRACE("(%s)\n", debugstr_guid(pgActionID));
339  
340     if (!pgActionID)
341     {
342         SetLastError(ERROR_INVALID_PARAMETER);
343         return TRUE;
344     }
345
346     /* Create this string only once, instead of in the helper function */
347     WINTRUST_Guid2Wstr( pgActionID, GuidString);
348
349     /* We don't care about success or failure */
350     WINTRUST_RemoveProviderFromReg(GuidString, Initialization);
351     WINTRUST_RemoveProviderFromReg(GuidString, Message);
352     WINTRUST_RemoveProviderFromReg(GuidString, Signature);
353     WINTRUST_RemoveProviderFromReg(GuidString, Certificate);
354     WINTRUST_RemoveProviderFromReg(GuidString, CertCheck);
355     WINTRUST_RemoveProviderFromReg(GuidString, FinalPolicy);
356     WINTRUST_RemoveProviderFromReg(GuidString, DiagnosticPolicy);
357     WINTRUST_RemoveProviderFromReg(GuidString, Cleanup);
358
359     return TRUE;
360 }
361
362 /***********************************************************************
363  *              WINTRUST_WriteSingleUsageEntry
364  *
365  * Helper for WintrustAddDefaultForUsage, writes a single value and its
366  * data to:
367  *
368  * HKLM\Software\Microsoft\Cryptography\Trust\Usages\<OID>
369  */
370 static LONG WINTRUST_WriteSingleUsageEntry(LPCSTR OID,
371                                            const WCHAR* Value,
372                                            WCHAR* Data)
373 {
374     static const WCHAR Usages[] = {'U','s','a','g','e','s','\\', 0};
375     WCHAR* UsageKey;
376     HKEY Key;
377     LONG Res = ERROR_SUCCESS;
378     WCHAR* OIDW;
379     DWORD Len;
380
381     /* Turn OID into a wide-character string */
382     Len = MultiByteToWideChar( CP_ACP, 0, OID, -1, NULL, 0 );
383     OIDW = HeapAlloc( GetProcessHeap(), 0, Len * sizeof(WCHAR) );
384     MultiByteToWideChar( CP_ACP, 0, OID, -1, OIDW, Len );
385
386     /* Allocate the needed space for UsageKey */
387     UsageKey = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Trust) + lstrlenW(Usages) + Len) * sizeof(WCHAR));
388     /* Create the key string */
389     lstrcpyW(UsageKey, Trust);
390     lstrcatW(UsageKey, Usages);
391     lstrcatW(UsageKey, OIDW);
392
393     Res = RegCreateKeyExW(HKEY_LOCAL_MACHINE, UsageKey, 0, NULL, 0, KEY_WRITE, NULL, &Key, NULL);
394     if (Res == ERROR_SUCCESS)
395     {
396         /* Create the Value entry */
397         Res = RegSetValueExW(Key, Value, 0, REG_SZ, (BYTE*)Data,
398                              (lstrlenW(Data) + 1)*sizeof(WCHAR));
399     }
400     RegCloseKey(Key);
401
402     HeapFree(GetProcessHeap(), 0, OIDW);
403     HeapFree(GetProcessHeap(), 0, UsageKey);
404
405     return Res;
406 }
407
408 /***************************************************************************
409  *              WINTRUST_RegisterGenVerifyV2
410  *
411  * Register WINTRUST_ACTION_GENERIC_VERIFY_V2 actions and usages.
412  *
413  * NOTES
414  *   WINTRUST_ACTION_GENERIC_VERIFY_V2 ({00AAC56B-CD44-11D0-8CC2-00C04FC295EE}
415  *   is defined in softpub.h
416  */
417 static BOOL WINTRUST_RegisterGenVerifyV2(void)
418 {
419     BOOL RegisteredOK = TRUE;
420     static GUID ProvGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
421     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
422                                          SoftpubInitialization,
423                                          SoftpubMessage,
424                                          SoftpubSignature,
425                                          SoftpubCertficate,
426                                          SoftpubCertCheck,
427                                          SoftpubFinalPolicy,
428                                          { 0, NULL, NULL }, /* No diagnostic policy */
429                                          SoftpubCleanup };
430     CRYPT_PROVIDER_REGDEFUSAGE DefUsage = { sizeof(CRYPT_PROVIDER_REGDEFUSAGE),
431                                             &ProvGUID,
432                                             NULL,   /* No Dll provided */
433                                             NULL,   /* No load callback function */
434                                             NULL }; /* No free callback function */
435
436     if (!WintrustAddDefaultForUsage(szOID_PKIX_KP_CODE_SIGNING, &DefUsage))
437         RegisteredOK = FALSE;
438
439     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
440         RegisteredOK = FALSE;
441
442     return RegisteredOK;
443 }
444
445 /***************************************************************************
446  *              WINTRUST_RegisterPublishedSoftware
447  *
448  * Register WIN_SPUB_ACTION_PUBLISHED_SOFTWARE actions and usages.
449  *
450  * NOTES
451  *   WIN_SPUB_ACTION_PUBLISHED_SOFTWARE ({64B9D180-8DA2-11CF-8736-00AA00A485EB})
452  *   is defined in wintrust.h
453  */
454 static BOOL WINTRUST_RegisterPublishedSoftware(void)
455 {
456     static GUID ProvGUID = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE;
457     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
458                                          SoftpubInitialization,
459                                          SoftpubMessage,
460                                          SoftpubSignature,
461                                          SoftpubCertficate,
462                                          SoftpubCertCheck,
463                                          SoftpubFinalPolicy,
464                                          { 0, NULL, NULL }, /* No diagnostic policy */
465                                          SoftpubCleanup };
466
467     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
468         return FALSE;
469
470     return TRUE;
471 }
472
473 #define WIN_SPUB_ACTION_PUBLISHED_SOFTWARE_NOBADUI { 0xc6b2e8d0, 0xe005, 0x11cf, { 0xa1,0x34,0x00,0xc0,0x4f,0xd7,0xbf,0x43 }}
474
475 /***************************************************************************
476  *              WINTRUST_RegisterPublishedSoftwareNoBadUi
477  *
478  * Register WIN_SPUB_ACTION_PUBLISHED_SOFTWARE_NOBADUI actions and usages.
479  *
480  * NOTES
481  *   WIN_SPUB_ACTION_PUBLISHED_SOFTWARE_NOBADUI ({C6B2E8D0-E005-11CF-A134-00C04FD7BF43})
482  *   is not defined in any include file. (FIXME: Find out if the name is correct).
483  */
484 static BOOL WINTRUST_RegisterPublishedSoftwareNoBadUi(void)
485 {
486     static GUID ProvGUID = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE_NOBADUI;
487     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
488                                          SoftpubInitialization,
489                                          SoftpubMessage,
490                                          SoftpubSignature,
491                                          SoftpubCertficate,
492                                          SoftpubCertCheck,
493                                          SoftpubFinalPolicy,
494                                          { 0, NULL, NULL }, /* No diagnostic policy */
495                                          SoftpubCleanup };
496
497     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
498         return FALSE;
499
500     return TRUE;
501 }
502
503 /***************************************************************************
504  *              WINTRUST_RegisterGenCertVerify
505  *
506  * Register WINTRUST_ACTION_GENERIC_CERT_VERIFY actions and usages.
507  *
508  * NOTES
509  *   WINTRUST_ACTION_GENERIC_CERT_VERIFY ({189A3842-3041-11D1-85E1-00C04FC295EE})
510  *   is defined in softpub.h
511  */
512 static BOOL WINTRUST_RegisterGenCertVerify(void)
513 {
514     static GUID ProvGUID = WINTRUST_ACTION_GENERIC_CERT_VERIFY;
515     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
516                                          SoftpubDefCertInit,
517                                          SoftpubMessage,
518                                          SoftpubSignature,
519                                          SoftpubCertficate,
520                                          SoftpubCertCheck,
521                                          SoftpubFinalPolicy,
522                                          { 0, NULL, NULL }, /* No diagnostic policy */
523                                          SoftpubCleanup };
524
525     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
526         return FALSE;
527
528     return TRUE;
529 }
530
531 /***************************************************************************
532  *              WINTRUST_RegisterTrustProviderTest
533  *
534  * Register WINTRUST_ACTION_TRUSTPROVIDER_TEST actions and usages.
535  *
536  * NOTES
537  *   WINTRUST_ACTION_TRUSTPROVIDER_TEST ({573E31F8-DDBA-11D0-8CCB-00C04FC295EE})
538  *   is defined in softpub.h
539  */
540 static BOOL WINTRUST_RegisterTrustProviderTest(void)
541 {
542     static GUID ProvGUID = WINTRUST_ACTION_TRUSTPROVIDER_TEST;
543     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
544                                          SoftpubInitialization,
545                                          SoftpubMessage,
546                                          SoftpubSignature,
547                                          SoftpubCertficate,
548                                          SoftpubCertCheck,
549                                          SoftpubFinalPolicy,
550                                          SoftpubDumpStructure,
551                                          SoftpubCleanup };
552
553     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
554         return FALSE;
555
556     return TRUE;
557 }
558
559 /***************************************************************************
560  *              WINTRUST_RegisterHttpsProv
561  *
562  * Register HTTPSPROV_ACTION actions and usages.
563  *
564  * NOTES
565  *   HTTPSPROV_ACTION ({573E31F8-AABA-11D0-8CCB-00C04FC295EE})
566  *   is defined in softpub.h
567  */
568 static BOOL WINTRUST_RegisterHttpsProv(void)
569 {
570     BOOL RegisteredOK = TRUE;
571     static CHAR SoftpubLoadUsage[] = "SoftpubLoadDefUsageCallData";
572     static CHAR SoftpubFreeUsage[] = "SoftpubFreeDefUsageCallData";
573     static GUID ProvGUID = HTTPSPROV_ACTION;
574     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
575                                          SoftpubInitialization,
576                                          SoftpubMessage,
577                                          SoftpubSignature,
578                                          HTTPSCertificateTrust,
579                                          SoftpubCertCheck,
580                                          HTTPSFinalProv,
581                                          { 0, NULL, NULL }, /* No diagnostic policy */
582                                          SoftpubCleanup };
583     CRYPT_PROVIDER_REGDEFUSAGE DefUsage = { sizeof(CRYPT_PROVIDER_REGDEFUSAGE),
584                                             &ProvGUID,
585                                             NULL, /* Will be filled later */
586                                             SoftpubLoadUsage,
587                                             SoftpubFreeUsage };
588
589     DefUsage.pwszDllName = HeapAlloc(GetProcessHeap(), 0, sizeof(SP_POLICY_PROVIDER_DLL_NAME));
590     lstrcpyW(DefUsage.pwszDllName, SP_POLICY_PROVIDER_DLL_NAME);
591
592     if (!WintrustAddDefaultForUsage(szOID_PKIX_KP_SERVER_AUTH, &DefUsage))
593         RegisteredOK = FALSE;
594     if (!WintrustAddDefaultForUsage(szOID_PKIX_KP_CLIENT_AUTH, &DefUsage))
595         RegisteredOK = FALSE;
596     if (!WintrustAddDefaultForUsage(szOID_SERVER_GATED_CRYPTO, &DefUsage))
597         RegisteredOK = FALSE;
598     if (!WintrustAddDefaultForUsage(szOID_SGC_NETSCAPE, &DefUsage))
599         RegisteredOK = FALSE;
600
601     HeapFree(GetProcessHeap(), 0, DefUsage.pwszDllName);
602
603     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
604         RegisteredOK = FALSE;
605
606     return RegisteredOK;
607 }
608
609 /***************************************************************************
610  *              WINTRUST_RegisterOfficeSignVerify
611  *
612  * Register OFFICESIGN_ACTION_VERIFY actions and usages.
613  *
614  * NOTES
615  *   OFFICESIGN_ACTION_VERIFY ({5555C2CD-17FB-11D1-85C4-00C04FC295EE})
616  *   is defined in softpub.h
617  */
618 static BOOL WINTRUST_RegisterOfficeSignVerify(void)
619 {
620     static GUID ProvGUID = OFFICESIGN_ACTION_VERIFY;
621     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
622                                          OfficeInitializePolicy,
623                                          SoftpubMessage,
624                                          SoftpubSignature,
625                                          SoftpubCertficate,
626                                          SoftpubCertCheck,
627                                          SoftpubFinalPolicy,
628                                          { 0, NULL, NULL }, /* No diagnostic policy */
629                                          OfficeCleanupPolicy };
630
631     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
632         return FALSE;
633
634     return TRUE;
635 }
636
637 /***************************************************************************
638  *              WINTRUST_RegisterDriverVerify
639  *
640  * Register DRIVER_ACTION_VERIFY actions and usages.
641  *
642  * NOTES
643  *   DRIVER_ACTION_VERIFY ({F750E6C3-38EE-11D1-85E5-00C04FC295EE})
644  *   is defined in softpub.h
645  */
646 static BOOL WINTRUST_RegisterDriverVerify(void)
647 {
648     static GUID ProvGUID = DRIVER_ACTION_VERIFY;
649     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
650                                          DriverInitializePolicy,
651                                          SoftpubMessage,
652                                          SoftpubSignature,
653                                          SoftpubCertficate,
654                                          SoftpubCertCheck,
655                                          DriverFinalPolicy,
656                                          { 0, NULL, NULL }, /* No diagnostic policy */
657                                          DriverCleanupPolicy };
658
659     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
660         return FALSE;
661
662     return TRUE;
663 }
664
665 /***************************************************************************
666  *              WINTRUST_RegisterGenChainVerify
667  *
668  * Register WINTRUST_ACTION_GENERIC_CHAIN_VERIFY actions and usages.
669  *
670  * NOTES
671  *   WINTRUST_ACTION_GENERIC_CHAIN_VERIFY ({FC451C16-AC75-11D1-B4B8-00C04FB66EA0})
672  *   is defined in softpub.h
673  */
674 static BOOL WINTRUST_RegisterGenChainVerify(void)
675 {
676     static GUID ProvGUID = WINTRUST_ACTION_GENERIC_CHAIN_VERIFY;
677     CRYPT_REGISTER_ACTIONID ProvInfo = { sizeof(CRYPT_REGISTER_ACTIONID),
678                                          SoftpubInitialization,
679                                          SoftpubMessage,
680                                          SoftpubSignature,
681                                          GenericChainCertificateTrust,
682                                          SoftpubCertCheck,
683                                          GenericChainFinalProv,
684                                          { 0, NULL, NULL }, /* No diagnostic policy */
685                                          SoftpubCleanup };
686
687     if (!WintrustAddActionID(&ProvGUID, 0, &ProvInfo))
688         return FALSE;
689
690     return TRUE;
691 }
692
693 /***********************************************************************
694  *              WintrustAddDefaultForUsage (WINTRUST.@)
695  *
696  * Write OID and callback functions to the registry.
697  *
698  * PARAMS
699  *   pszUsageOID [I] Pointer to a GUID.
700  *   psDefUsage  [I] Pointer to a structure that specifies the callback functions.
701  *
702  * RETURNS
703  *   Success: TRUE.
704  *   Failure: FALSE.
705  *
706  * NOTES
707  *   WintrustAddDefaultForUsage will only return TRUE or FALSE, no last 
708  *   error is set, not even when the registry cannot be written to.
709  */
710 BOOL WINAPI WintrustAddDefaultForUsage(const char *pszUsageOID,
711                                        CRYPT_PROVIDER_REGDEFUSAGE *psDefUsage)
712 {
713     static const WCHAR CBAlloc[]    = {'C','a','l','l','b','a','c','k','A','l','l','o','c','F','u','n','c','t','i','o','n', 0};
714     static const WCHAR CBFree[]     = {'C','a','l','l','b','a','c','k','F','r','e','e','F','u','n','c','t','i','o','n', 0};
715     LONG Res = ERROR_SUCCESS;
716     LONG WriteUsageError = ERROR_SUCCESS;
717     DWORD Len;
718     WCHAR GuidString[39];
719
720     TRACE("(%s %p)\n", debugstr_a(pszUsageOID), psDefUsage);
721
722     /* Some sanity checks. */
723     if (!pszUsageOID ||
724         !psDefUsage ||
725         !psDefUsage->pgActionID ||
726         (psDefUsage->cbStruct != sizeof(CRYPT_PROVIDER_REGDEFUSAGE)))
727     {
728         SetLastError(ERROR_INVALID_PARAMETER);
729         return FALSE;
730     }
731
732     if (psDefUsage->pwszDllName)
733     {
734         Res = WINTRUST_WriteSingleUsageEntry(pszUsageOID, Dll, psDefUsage->pwszDllName);
735         if (Res != ERROR_SUCCESS) WriteUsageError = Res;
736     }
737     if (psDefUsage->pwszLoadCallbackDataFunctionName)
738     {
739         WCHAR* CallbackW;
740
741         Len = MultiByteToWideChar( CP_ACP, 0, psDefUsage->pwszLoadCallbackDataFunctionName, -1, NULL, 0 );
742         CallbackW = HeapAlloc( GetProcessHeap(), 0, Len * sizeof(WCHAR) );
743         MultiByteToWideChar( CP_ACP, 0, psDefUsage->pwszLoadCallbackDataFunctionName, -1, CallbackW, Len );
744
745         Res = WINTRUST_WriteSingleUsageEntry(pszUsageOID, CBAlloc, CallbackW);
746         if (Res != ERROR_SUCCESS) WriteUsageError = Res;
747
748         HeapFree(GetProcessHeap(), 0, CallbackW);
749     }
750     if (psDefUsage->pwszFreeCallbackDataFunctionName)
751     {
752         WCHAR* CallbackW;
753
754         Len = MultiByteToWideChar( CP_ACP, 0, psDefUsage->pwszFreeCallbackDataFunctionName, -1, NULL, 0 );
755         CallbackW = HeapAlloc( GetProcessHeap(), 0, Len * sizeof(WCHAR) );
756         MultiByteToWideChar( CP_ACP, 0, psDefUsage->pwszFreeCallbackDataFunctionName, -1, CallbackW, Len );
757
758         Res = WINTRUST_WriteSingleUsageEntry(pszUsageOID, CBFree, CallbackW);
759         if (Res != ERROR_SUCCESS) WriteUsageError = Res;
760
761         HeapFree(GetProcessHeap(), 0, CallbackW);
762     }
763
764     WINTRUST_Guid2Wstr(psDefUsage->pgActionID, GuidString);
765     Res = WINTRUST_WriteSingleUsageEntry(pszUsageOID, DefaultId, GuidString);
766     if (Res != ERROR_SUCCESS) WriteUsageError = Res;
767
768     if (WriteUsageError != ERROR_SUCCESS)
769         return FALSE;
770
771     return TRUE;
772 }
773
774 static FARPROC WINTRUST_ReadProviderFromReg(WCHAR *GuidString, const WCHAR *FunctionType)
775 {
776     WCHAR ProvKey[MAX_PATH], DllName[MAX_PATH];
777     char FunctionName[MAX_PATH];
778     HKEY Key;
779     LONG Res = ERROR_SUCCESS;
780     DWORD Size;
781     HMODULE Lib;
782     FARPROC Func = NULL;
783
784     /* Create the needed key string */
785     ProvKey[0]='\0';
786     lstrcatW(ProvKey, Trust);
787     lstrcatW(ProvKey, FunctionType);
788     lstrcatW(ProvKey, GuidString);
789
790     Res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, ProvKey, 0, KEY_READ, &Key);
791     if (Res != ERROR_SUCCESS) goto error_close_key;
792
793     /* Read the $DLL entry */
794     Size = sizeof(DllName);
795     Res = RegQueryValueExW(Key, Dll, NULL, NULL, (LPBYTE)DllName, &Size);
796     if (Res != ERROR_SUCCESS) goto error_close_key;
797
798     /* Read the $Function entry */
799     Size = sizeof(FunctionName);
800     Res = RegQueryValueExA(Key, "$Function", NULL, NULL, (LPBYTE)FunctionName, &Size);
801     if (Res != ERROR_SUCCESS) goto error_close_key;
802
803     /* Load the library - there appears to be no way to close a provider, so
804      * just leak the module handle.
805      */
806     Lib = LoadLibraryW(DllName);
807     Func = GetProcAddress(Lib, FunctionName);
808
809 error_close_key:
810     RegCloseKey(Key);
811
812     return Func;
813 }
814
815 /***********************************************************************
816  *              WintrustLoadFunctionPointers (WINTRUST.@)
817  */
818 BOOL WINAPI WintrustLoadFunctionPointers( GUID* pgActionID,
819                                           CRYPT_PROVIDER_FUNCTIONS* pPfns )
820 {
821     WCHAR GuidString[39];
822
823     TRACE("(%s %p)\n", debugstr_guid(pgActionID), pPfns);
824
825     if (!pPfns) return FALSE;
826     if (!pgActionID)
827     {
828         SetLastError(ERROR_INVALID_PARAMETER);
829         return FALSE;
830     }
831     if (pPfns->cbStruct != sizeof(CRYPT_PROVIDER_FUNCTIONS)) return FALSE;
832
833     /* Create this string only once, instead of in the helper function */
834     WINTRUST_Guid2Wstr( pgActionID, GuidString);
835
836     /* Get the function pointers from the registry, where applicable */
837     pPfns->pfnAlloc = NULL;
838     pPfns->pfnFree = NULL;
839     pPfns->pfnAddStore2Chain = NULL;
840     pPfns->pfnAddSgnr2Chain = NULL;
841     pPfns->pfnAddCert2Chain = NULL;
842     pPfns->pfnAddPrivData2Chain = NULL;
843     pPfns->psUIpfns = NULL;
844     pPfns->pfnInitialize = (PFN_PROVIDER_INIT_CALL)WINTRUST_ReadProviderFromReg(GuidString, Initialization);
845     pPfns->pfnObjectTrust = (PFN_PROVIDER_OBJTRUST_CALL)WINTRUST_ReadProviderFromReg(GuidString, Message);
846     pPfns->pfnSignatureTrust = (PFN_PROVIDER_SIGTRUST_CALL)WINTRUST_ReadProviderFromReg(GuidString, Signature);
847     pPfns->pfnCertificateTrust = (PFN_PROVIDER_CERTTRUST_CALL)WINTRUST_ReadProviderFromReg(GuidString, Certificate);
848     pPfns->pfnCertCheckPolicy = (PFN_PROVIDER_CERTCHKPOLICY_CALL)WINTRUST_ReadProviderFromReg(GuidString, CertCheck);
849     pPfns->pfnFinalPolicy = (PFN_PROVIDER_FINALPOLICY_CALL)WINTRUST_ReadProviderFromReg(GuidString, FinalPolicy);
850     pPfns->pfnTestFinalPolicy = (PFN_PROVIDER_TESTFINALPOLICY_CALL)WINTRUST_ReadProviderFromReg(GuidString, DiagnosticPolicy);
851     pPfns->pfnCleanupPolicy = (PFN_PROVIDER_CLEANUP_CALL)WINTRUST_ReadProviderFromReg(GuidString, Cleanup);
852
853     return TRUE;
854 }
855
856 /***********************************************************************
857  *              WINTRUST_SIPPAddProvider
858  *
859  * Helper for DllRegisterServer.
860  */
861 static BOOL WINTRUST_SIPPAddProvider(GUID* Subject, WCHAR* MagicNumber)
862 {
863     static WCHAR CryptSIPGetSignedDataMsg[] =
864         {'C','r','y','p','t','S','I','P','G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g', 0};
865     static WCHAR CryptSIPPutSignedDataMsg[] =
866         {'C','r','y','p','t','S','I','P','P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g', 0};
867     static WCHAR CryptSIPCreateIndirectData[] =
868         {'C','r','y','p','t','S','I','P','C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a', 0};
869     static WCHAR CryptSIPVerifyIndirectData[] =
870         {'C','r','y','p','t','S','I','P','V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a', 0};
871     static WCHAR CryptSIPRemoveSignedDataMsg[] =
872         {'C','r','y','p','t','S','I','P','R','e','m','o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g', 0};
873     SIP_ADD_NEWPROVIDER NewProv;
874     BOOL Ret;
875
876     /* Clear and initialize the structure */
877     memset(&NewProv, 0, sizeof(SIP_ADD_NEWPROVIDER));
878     NewProv.cbStruct = sizeof(SIP_ADD_NEWPROVIDER);
879     NewProv.pwszDLLFileName = HeapAlloc(GetProcessHeap(), 0, sizeof(SP_POLICY_PROVIDER_DLL_NAME));
880     /* Fill the structure */
881     NewProv.pgSubject              = Subject;
882     lstrcpyW(NewProv.pwszDLLFileName, SP_POLICY_PROVIDER_DLL_NAME);
883     NewProv.pwszMagicNumber        = MagicNumber;
884     NewProv.pwszIsFunctionName     = NULL;
885     NewProv.pwszGetFuncName        = CryptSIPGetSignedDataMsg;
886     NewProv.pwszPutFuncName        = CryptSIPPutSignedDataMsg;
887     NewProv.pwszCreateFuncName     = CryptSIPCreateIndirectData;
888     NewProv.pwszVerifyFuncName     = CryptSIPVerifyIndirectData;
889     NewProv.pwszRemoveFuncName     = CryptSIPRemoveSignedDataMsg;
890     NewProv.pwszIsFunctionNameFmt2 = NULL;
891
892     Ret = CryptSIPAddProvider(&NewProv);
893
894     HeapFree(GetProcessHeap(), 0, NewProv.pwszDLLFileName);
895  
896     return Ret;
897 }
898
899 /***********************************************************************
900  *              DllRegisterServer (WINTRUST.@)
901  */
902 HRESULT WINAPI DllRegisterServer(void)
903 {
904     static const CHAR SpcPeImageDataEncode[]           = "WVTAsn1SpcPeImageDataEncode";
905     static const CHAR SpcPeImageDataDecode[]           = "WVTAsn1SpcPeImageDataDecode";
906     static const CHAR SpcLinkEncode[]                  = "WVTAsn1SpcLinkEncode";
907     static const CHAR SpcLinkDecode[]                  = "WVTAsn1SpcLinkDecode";
908     static const CHAR SpcSigInfoEncode[]               = "WVTAsn1SpcSigInfoEncode";
909     static const CHAR SpcSigInfoDecode[]               = "WVTAsn1SpcSigInfoDecode";
910     static const CHAR SpcIndirectDataContentEncode[]   = "WVTAsn1SpcIndirectDataContentEncode";
911     static const CHAR SpcIndirectDataContentDecode[]   = "WVTAsn1SpcIndirectDataContentDecode";
912     static const CHAR SpcSpAgencyInfoEncode[]          = "WVTAsn1SpcSpAgencyInfoEncode";
913     static const CHAR SpcSpAgencyInfoDecode[]          = "WVTAsn1SpcSpAgencyInfoDecode";
914     static const CHAR SpcMinimalCriteriaInfoEncode[]   = "WVTAsn1SpcMinimalCriteriaInfoEncode";
915     static const CHAR SpcMinimalCriteriaInfoDecode[]   = "WVTAsn1SpcMinimalCriteriaInfoDecode";
916     static const CHAR SpcFinancialCriteriaInfoEncode[] = "WVTAsn1SpcFinancialCriteriaInfoEncode";
917     static const CHAR SpcFinancialCriteriaInfoDecode[] = "WVTAsn1SpcFinancialCriteriaInfoDecode";
918     static const CHAR SpcStatementTypeEncode[]         = "WVTAsn1SpcStatementTypeEncode";
919     static const CHAR SpcStatementTypeDecode[]         = "WVTAsn1SpcStatementTypeDecode";
920     static const CHAR CatNameValueEncode[]             = "WVTAsn1CatNameValueEncode";
921     static const CHAR CatNameValueDecode[]             = "WVTAsn1CatNameValueDecode";
922     static const CHAR CatMemberInfoEncode[]            = "WVTAsn1CatMemberInfoEncode";
923     static const CHAR CatMemberInfoDecode[]            = "WVTAsn1CatMemberInfoDecode";
924     static const CHAR SpcSpOpusInfoEncode[]            = "WVTAsn1SpcSpOpusInfoEncode";
925     static const CHAR SpcSpOpusInfoDecode[]            = "WVTAsn1SpcSpOpusInfoDecode";
926     static GUID Unknown1 = { 0xDE351A42, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
927     static GUID Unknown2 = { 0xC689AABA, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
928     static GUID Unknown3 = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
929     static GUID Unknown4 = { 0xC689AAB9, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
930     static GUID Unknown5 = { 0xDE351A43, 0x8E59, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
931     static GUID Unknown6 = { 0x9BA61D3F, 0xE73A, 0x11D0, { 0x8C,0xD2,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
932     static WCHAR MagicNumber2[] = {'M','S','C','F', 0};
933     static WCHAR MagicNumber3[] = {'0','x','0','0','0','0','4','5','5','0', 0};
934     static WCHAR CafeBabe[] = {'0','x','c','a','f','e','b','a','b','e', 0};
935
936     HRESULT CryptRegisterRes = S_OK;
937     HRESULT TrustProviderRes = S_OK;
938     HRESULT SIPAddProviderRes = S_OK;
939
940     TRACE("\n");
941
942     /* Testing on native shows that when an error is encountered in one of the CryptRegisterOIDFunction calls
943      * the rest of these calls is skipped. Registering is however continued for the trust providers.
944      *
945      * We are not totally in line with native as there all decoding functions are registered after all encoding
946      * functions.
947      */
948 #define WINTRUST_REGISTEROID( oid, encode_funcname, decode_funcname ) \
949     do { \
950         if (!CryptRegisterOIDFunction(X509_ASN_ENCODING, CRYPT_OID_ENCODE_OBJECT_FUNC, oid, SP_POLICY_PROVIDER_DLL_NAME, encode_funcname)) \
951         {                                                               \
952             CryptRegisterRes = HRESULT_FROM_WIN32(GetLastError());      \
953             goto add_trust_providers;                                   \
954         }                                                               \
955         if (!CryptRegisterOIDFunction(X509_ASN_ENCODING, CRYPT_OID_DECODE_OBJECT_FUNC, oid, SP_POLICY_PROVIDER_DLL_NAME, decode_funcname)) \
956         {                                                               \
957             CryptRegisterRes = HRESULT_FROM_WIN32(GetLastError());      \
958             goto add_trust_providers;                                   \
959         }                                                               \
960     } while (0)
961
962     WINTRUST_REGISTEROID(SPC_PE_IMAGE_DATA_OBJID, SpcPeImageDataEncode, SpcPeImageDataDecode);
963     WINTRUST_REGISTEROID(SPC_PE_IMAGE_DATA_STRUCT, SpcPeImageDataEncode, SpcPeImageDataDecode);
964     WINTRUST_REGISTEROID(SPC_CAB_DATA_OBJID, SpcLinkEncode, SpcLinkDecode);
965     WINTRUST_REGISTEROID(SPC_CAB_DATA_STRUCT, SpcLinkEncode, SpcLinkDecode);
966     WINTRUST_REGISTEROID(SPC_JAVA_CLASS_DATA_OBJID, SpcLinkEncode, SpcLinkDecode);
967     WINTRUST_REGISTEROID(SPC_JAVA_CLASS_DATA_STRUCT, SpcLinkEncode, SpcLinkDecode);
968     WINTRUST_REGISTEROID(SPC_LINK_OBJID, SpcLinkEncode, SpcLinkDecode);
969     WINTRUST_REGISTEROID(SPC_LINK_STRUCT, SpcLinkEncode, SpcLinkDecode);
970     WINTRUST_REGISTEROID(SPC_SIGINFO_OBJID, SpcSigInfoEncode, SpcSigInfoDecode);
971     WINTRUST_REGISTEROID(SPC_SIGINFO_STRUCT, SpcSigInfoEncode, SpcSigInfoDecode);
972     WINTRUST_REGISTEROID(SPC_INDIRECT_DATA_OBJID, SpcIndirectDataContentEncode, SpcIndirectDataContentDecode);
973     WINTRUST_REGISTEROID(SPC_INDIRECT_DATA_CONTENT_STRUCT, SpcIndirectDataContentEncode, SpcIndirectDataContentDecode);
974     WINTRUST_REGISTEROID(SPC_SP_AGENCY_INFO_OBJID, SpcSpAgencyInfoEncode, SpcSpAgencyInfoDecode);
975     WINTRUST_REGISTEROID(SPC_SP_AGENCY_INFO_STRUCT, SpcSpAgencyInfoEncode, SpcSpAgencyInfoDecode);
976     WINTRUST_REGISTEROID(SPC_MINIMAL_CRITERIA_OBJID, SpcMinimalCriteriaInfoEncode, SpcMinimalCriteriaInfoDecode);
977     WINTRUST_REGISTEROID(SPC_MINIMAL_CRITERIA_STRUCT, SpcMinimalCriteriaInfoEncode, SpcMinimalCriteriaInfoDecode);
978     WINTRUST_REGISTEROID(SPC_FINANCIAL_CRITERIA_OBJID, SpcFinancialCriteriaInfoEncode, SpcFinancialCriteriaInfoDecode);
979     WINTRUST_REGISTEROID(SPC_FINANCIAL_CRITERIA_STRUCT, SpcFinancialCriteriaInfoEncode, SpcFinancialCriteriaInfoDecode);
980     WINTRUST_REGISTEROID(SPC_STATEMENT_TYPE_OBJID, SpcStatementTypeEncode, SpcStatementTypeDecode);
981     WINTRUST_REGISTEROID(SPC_STATEMENT_TYPE_STRUCT, SpcStatementTypeEncode, SpcStatementTypeDecode);
982     WINTRUST_REGISTEROID(CAT_NAMEVALUE_OBJID, CatNameValueEncode, CatNameValueDecode);
983     WINTRUST_REGISTEROID(CAT_NAMEVALUE_STRUCT, CatNameValueEncode, CatNameValueDecode);
984     WINTRUST_REGISTEROID(CAT_MEMBERINFO_OBJID, CatMemberInfoEncode, CatMemberInfoDecode);
985     WINTRUST_REGISTEROID(CAT_MEMBERINFO_STRUCT, CatMemberInfoEncode, CatMemberInfoDecode);
986     WINTRUST_REGISTEROID(SPC_SP_OPUS_INFO_OBJID, SpcSpOpusInfoEncode, SpcSpOpusInfoDecode);
987     WINTRUST_REGISTEROID(SPC_SP_OPUS_INFO_STRUCT, SpcSpOpusInfoEncode,  SpcSpOpusInfoDecode);
988
989 #undef WINTRUST_REGISTEROID
990
991 add_trust_providers:
992
993     /* Testing on W2K3 shows:
994      * All registry writes are tried. If one fails this part will return S_FALSE.
995      *
996      * Last error is set to the last error encountered, regardless if the first
997      * part failed or not.
998      */
999
1000     /* Create the necessary action registry structures */
1001     WINTRUST_InitRegStructs();
1002
1003     /* Register several Trust Provider actions */
1004     if (!WINTRUST_RegisterGenVerifyV2())
1005         TrustProviderRes = S_FALSE;
1006     if (!WINTRUST_RegisterPublishedSoftware())
1007         TrustProviderRes = S_FALSE;
1008     if (!WINTRUST_RegisterPublishedSoftwareNoBadUi())
1009         TrustProviderRes = S_FALSE;
1010     if (!WINTRUST_RegisterGenCertVerify())
1011         TrustProviderRes = S_FALSE;
1012     if (!WINTRUST_RegisterTrustProviderTest())
1013         TrustProviderRes = S_FALSE;
1014     if (!WINTRUST_RegisterHttpsProv())
1015         TrustProviderRes = S_FALSE;
1016     if (!WINTRUST_RegisterOfficeSignVerify())
1017         TrustProviderRes = S_FALSE;
1018     if (!WINTRUST_RegisterDriverVerify())
1019         TrustProviderRes = S_FALSE;
1020     if (!WINTRUST_RegisterGenChainVerify())
1021         TrustProviderRes = S_FALSE;
1022
1023     /* Free the registry structures */
1024     WINTRUST_FreeRegStructs();
1025
1026     /* Testing on W2K3 shows:
1027      * All registry writes are tried. If one fails this part will return S_FALSE.
1028      *
1029      * Last error is set to the last error encountered, regardless if the previous
1030      * parts failed or not.
1031      */
1032
1033     if (!WINTRUST_SIPPAddProvider(&Unknown1, NULL))
1034         SIPAddProviderRes = S_FALSE;
1035     if (!WINTRUST_SIPPAddProvider(&Unknown2, MagicNumber2))
1036         SIPAddProviderRes = S_FALSE;
1037     if (!WINTRUST_SIPPAddProvider(&Unknown3, MagicNumber3))
1038         SIPAddProviderRes = S_FALSE;
1039     if (!WINTRUST_SIPPAddProvider(&Unknown4, CafeBabe))
1040         SIPAddProviderRes = S_FALSE;
1041     if (!WINTRUST_SIPPAddProvider(&Unknown5, CafeBabe))
1042         SIPAddProviderRes = S_FALSE;
1043     if (!WINTRUST_SIPPAddProvider(&Unknown6, CafeBabe))
1044         SIPAddProviderRes = S_FALSE;
1045
1046     /* Native does a CryptSIPRemoveProvider here for {941C2937-1292-11D1-85BE-00C04FC295EE}.
1047      * This SIP Provider is however not found on up-to-date window install and native will
1048      * set the last error to ERROR_FILE_NOT_FOUND.
1049      * Wine has the last error set to ERROR_INVALID_PARAMETER. There shouldn't be an app
1050      * depending on this last error though so there is no need to imitate native to the full extent.
1051      *
1052      * (The ERROR_INVALID_PARAMETER for Wine it totally valid as we (and native) do register
1053      * a trust provider without a diagnostic policy).
1054      */
1055
1056     /* If CryptRegisterRes is not S_OK it will always overrule the return value. */
1057     if (CryptRegisterRes != S_OK)
1058         return CryptRegisterRes;
1059     else if (SIPAddProviderRes == S_OK)
1060         return TrustProviderRes;
1061     else 
1062         return SIPAddProviderRes;
1063 }
1064
1065 /***********************************************************************
1066  *              DllUnregisterServer (WINTRUST.@)
1067  */
1068 HRESULT WINAPI DllUnregisterServer(void)
1069 {
1070      FIXME("stub\n");
1071      return S_OK;
1072 }
1073
1074 /***********************************************************************
1075  *              SoftpubDllRegisterServer (WINTRUST.@)
1076  *
1077  * Registers softpub.dll
1078  *
1079  * PARAMS
1080  *
1081  * RETURNS
1082  *  Success: S_OK.
1083  *  Failure: S_FALSE. (See also GetLastError()).
1084  *
1085  * NOTES
1086  *  DllRegisterServer in softpub.dll will call this function.
1087  *  See comments in DllRegisterServer.
1088  */
1089 HRESULT WINAPI SoftpubDllRegisterServer(void)
1090 {
1091     HRESULT TrustProviderRes = S_OK;
1092
1093     TRACE("\n");
1094
1095     /* Create the necessary action registry structures */
1096     WINTRUST_InitRegStructs();
1097
1098     /* Register several Trust Provider actions */
1099     if (!WINTRUST_RegisterGenVerifyV2())
1100         TrustProviderRes = S_FALSE;
1101     if (!WINTRUST_RegisterPublishedSoftware())
1102         TrustProviderRes = S_FALSE;
1103     if (!WINTRUST_RegisterPublishedSoftwareNoBadUi())
1104         TrustProviderRes = S_FALSE;
1105     if (!WINTRUST_RegisterGenCertVerify())
1106         TrustProviderRes = S_FALSE;
1107     if (!WINTRUST_RegisterTrustProviderTest())
1108         TrustProviderRes = S_FALSE;
1109     if (!WINTRUST_RegisterHttpsProv())
1110         TrustProviderRes = S_FALSE;
1111     if (!WINTRUST_RegisterOfficeSignVerify())
1112         TrustProviderRes = S_FALSE;
1113     if (!WINTRUST_RegisterDriverVerify())
1114         TrustProviderRes = S_FALSE;
1115     if (!WINTRUST_RegisterGenChainVerify())
1116         TrustProviderRes = S_FALSE;
1117
1118     /* Free the registry structures */
1119     WINTRUST_FreeRegStructs();
1120
1121     return TrustProviderRes;
1122 }
1123
1124 /***********************************************************************
1125  *              SoftpubDllUnregisterServer (WINTRUST.@)
1126  */
1127 HRESULT WINAPI SoftpubDllUnregisterServer(void)
1128 {
1129      FIXME("stub\n");
1130      return S_OK;
1131 }
1132
1133 /***********************************************************************
1134  *              mscat32DllRegisterServer (WINTRUST.@)
1135  */
1136 HRESULT WINAPI mscat32DllRegisterServer(void)
1137 {
1138      FIXME("stub\n");
1139      return S_OK;
1140 }
1141
1142 /***********************************************************************
1143  *              mscat32DllUnregisterServer (WINTRUST.@)
1144  */
1145 HRESULT WINAPI mscat32DllUnregisterServer(void)
1146 {
1147      FIXME("stub\n");
1148      return S_OK;
1149 }
1150
1151 /***********************************************************************
1152  *              mssip32DllRegisterServer (WINTRUST.@)
1153  */
1154 HRESULT WINAPI mssip32DllRegisterServer(void)
1155 {
1156      FIXME("stub\n");
1157      return S_OK;
1158 }
1159
1160 /***********************************************************************
1161  *              mssip32DllUnregisterServer (WINTRUST.@)
1162  */
1163 HRESULT WINAPI mssip32DllUnregisterServer(void)
1164 {
1165      FIXME("stub\n");
1166      return S_OK;
1167 }