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