crypt32/tests: Fix a test on Vista.
[wine] / dlls / crypt32 / sip.c
1 /*
2  * Copyright 2002 Mike McCormack for CodeWeavers
3  * Copyright 2005-2008 Juan Lang
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 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wincrypt.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "mssip.h"
30 #include "winuser.h"
31
32 #include "wine/debug.h"
33 #include "wine/list.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
36
37 static const WCHAR szOID[] = {
38     'S','o','f','t','w','a','r','e','\\',
39     'M','i','c','r','o','s','o','f','t','\\',
40     'C','r','y','p','t','o','g','r','a','p','h','y','\\',
41     'O','I','D','\\',
42     'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
43     'C','r','y','p','t','S','I','P','D','l','l', 0 };
44
45 static const WCHAR szPutSigned[] = {
46     'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
47 static const WCHAR szGetSigned[] = {
48     'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
49 static const WCHAR szRemoveSigned[] = {
50     'R','e','m','o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
51 static const WCHAR szCreate[] = {
52     'C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a','\\',0};
53 static const WCHAR szVerify[] = {
54     'V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a','\\',0};
55 static const WCHAR szIsMyFile[] = {
56     'I','s','M','y','F','i','l','e','T','y','p','e','\\',0};
57 static const WCHAR szIsMyFile2[] = {
58     'I','s','M','y','F','i','l','e','T','y','p','e','2','\\',0};
59
60 static const WCHAR szDllName[] = { 'D','l','l',0 };
61 static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
62
63 /* convert a guid to a wide character string */
64 static void CRYPT_guid2wstr( const GUID *guid, LPWSTR wstr )
65 {
66     char str[40];
67
68     sprintf(str, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
69            guid->Data1, guid->Data2, guid->Data3,
70            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
71            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
72     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
73 }
74
75 /***********************************************************************
76  *              CRYPT_SIPDeleteFunction
77  *
78  * Helper function for CryptSIPRemoveProvider
79  */
80 static LONG CRYPT_SIPDeleteFunction( const GUID *guid, LPCWSTR szKey )
81 {
82     WCHAR szFullKey[ 0x100 ];
83     LONG r = ERROR_SUCCESS;
84
85     /* max length of szFullKey depends on our code only, so we won't overrun */
86     lstrcpyW( szFullKey, szOID );
87     lstrcatW( szFullKey, szKey );
88     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
89
90     r = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szFullKey);
91
92     return r;
93 }
94
95 /***********************************************************************
96  *             CryptSIPRemoveProvider (CRYPT32.@)
97  *
98  * Remove a SIP provider and its functions from the registry.
99  *
100  * PARAMS
101  *  pgProv     [I] Pointer to a GUID for this SIP provider
102  *
103  * RETURNS
104  *  Success: TRUE.
105  *  Failure: FALSE. (Look at GetLastError()).
106  *
107  * NOTES
108  *  Registry errors are always reported via SetLastError(). Every registry
109  *  deletion will be tried.
110  */
111 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
112 {
113     LONG r = ERROR_SUCCESS;
114     LONG remove_error = ERROR_SUCCESS;
115
116     TRACE("%s\n", debugstr_guid(pgProv));
117
118     if (!pgProv)
119     {
120         SetLastError(ERROR_INVALID_PARAMETER);
121         return FALSE;
122     }
123
124
125 #define CRYPT_SIPREMOVEPROV( key ) \
126     r = CRYPT_SIPDeleteFunction( pgProv, key); \
127     if (r != ERROR_SUCCESS) remove_error = r
128
129     CRYPT_SIPREMOVEPROV( szPutSigned);
130     CRYPT_SIPREMOVEPROV( szGetSigned);
131     CRYPT_SIPREMOVEPROV( szRemoveSigned);
132     CRYPT_SIPREMOVEPROV( szCreate);
133     CRYPT_SIPREMOVEPROV( szVerify);
134     CRYPT_SIPREMOVEPROV( szIsMyFile);
135     CRYPT_SIPREMOVEPROV( szIsMyFile2);
136
137 #undef CRYPT_SIPREMOVEPROV
138
139     if (remove_error != ERROR_SUCCESS)
140     {
141         SetLastError(remove_error);
142         return FALSE;
143     }
144
145     return TRUE;
146 }
147
148 /*
149  * Helper for CryptSIPAddProvider
150  *
151  * Add a registry key containing a dll name and function under
152  *  "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
153  */
154 static LONG CRYPT_SIPWriteFunction( const GUID *guid, LPCWSTR szKey,
155               LPCWSTR szDll, LPCWSTR szFunction )
156 {
157     WCHAR szFullKey[ 0x100 ];
158     LONG r = ERROR_SUCCESS;
159     HKEY hKey;
160
161     if( !szFunction )
162          return ERROR_SUCCESS;
163
164     /* max length of szFullKey depends on our code only, so we won't overrun */
165     lstrcpyW( szFullKey, szOID );
166     lstrcatW( szFullKey, szKey );
167     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
168
169     TRACE("key is %s\n", debugstr_w( szFullKey ) );
170
171     r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
172     if( r != ERROR_SUCCESS ) goto error_close_key;
173
174     /* write the values */
175     r = RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
176                         ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
177     if( r != ERROR_SUCCESS ) goto error_close_key;
178     r = RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
179                         ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
180
181 error_close_key:
182
183     RegCloseKey( hKey );
184
185     return r;
186 }
187
188 /***********************************************************************
189  *             CryptSIPAddProvider (CRYPT32.@)
190  *
191  * Add a SIP provider and its functions to the registry.
192  *
193  * PARAMS
194  *  psNewProv       [I] Pointer to a structure with information about
195  *                      the functions this SIP provider can perform.
196  *
197  * RETURNS
198  *  Success: TRUE.
199  *  Failure: FALSE. (Look at GetLastError()).
200  *
201  * NOTES
202  *  Registry errors are always reported via SetLastError(). If a
203  *  registry error occurs the rest of the registry write operations
204  *  will be skipped.
205  */
206 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
207 {
208     LONG r = ERROR_SUCCESS;
209
210     TRACE("%p\n", psNewProv);
211
212     if (!psNewProv ||
213         psNewProv->cbStruct != sizeof(SIP_ADD_NEWPROVIDER) ||
214         !psNewProv->pwszGetFuncName ||
215         !psNewProv->pwszPutFuncName ||
216         !psNewProv->pwszCreateFuncName ||
217         !psNewProv->pwszVerifyFuncName ||
218         !psNewProv->pwszRemoveFuncName)
219     {
220         SetLastError(ERROR_INVALID_PARAMETER);
221         return FALSE;
222     }
223
224     TRACE("%s %s %s %s %s\n",
225           debugstr_guid( psNewProv->pgSubject ),
226           debugstr_w( psNewProv->pwszDLLFileName ),
227           debugstr_w( psNewProv->pwszMagicNumber ),
228           debugstr_w( psNewProv->pwszIsFunctionName ),
229           debugstr_w( psNewProv->pwszIsFunctionNameFmt2 ) );
230
231 #define CRYPT_SIPADDPROV( key, field ) \
232     r = CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
233            psNewProv->pwszDLLFileName, psNewProv->field); \
234     if (r != ERROR_SUCCESS) goto end_function
235
236     CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
237     CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
238     CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
239     CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
240     CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
241     CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionName );
242     CRYPT_SIPADDPROV( szIsMyFile2, pwszIsFunctionNameFmt2 );
243
244 #undef CRYPT_SIPADDPROV
245
246 end_function:
247
248     if (r != ERROR_SUCCESS)
249     {
250         SetLastError(r);
251         return FALSE;
252     }
253
254     return TRUE;
255 }
256
257 static void *CRYPT_LoadSIPFuncFromKey(HKEY key, HMODULE *pLib)
258 {
259     LONG r;
260     DWORD size;
261     WCHAR dllName[MAX_PATH];
262     char functionName[MAX_PATH];
263     HMODULE lib;
264     void *func = NULL;
265
266     /* Read the DLL entry */
267     size = sizeof(dllName);
268     r = RegQueryValueExW(key, szDllName, NULL, NULL, (LPBYTE)dllName, &size);
269     if (r) goto end;
270
271     /* Read the Function entry */
272     size = sizeof(functionName);
273     r = RegQueryValueExA(key, "FuncName", NULL, NULL, (LPBYTE)functionName,
274      &size);
275     if (r) goto end;
276
277     lib = LoadLibraryW(dllName);
278     if (!lib)
279         goto end;
280     func = GetProcAddress(lib, functionName);
281     if (func)
282         *pLib = lib;
283     else
284         FreeLibrary(lib);
285
286 end:
287     return func;
288 }
289
290 /***********************************************************************
291  *             CryptSIPRetrieveSubjectGuid (CRYPT32.@)
292  *
293  * Determine the right SIP GUID for the given file.
294  *
295  * PARAMS
296  *  FileName   [I] Filename.
297  *  hFileIn    [I] Optional handle to the file.
298  *  pgSubject  [O] The SIP's GUID.
299  *
300  * RETURNS
301  *  Success: TRUE. pgSubject contains the SIP GUID.
302  *  Failure: FALSE. (Look at GetLastError()).
303  *
304  * NOTES
305  *  On failure pgSubject will contain a NULL GUID.
306  *  The handle is always preferred above the filename.
307  */
308 BOOL WINAPI CryptSIPRetrieveSubjectGuid
309       (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
310 {
311     HANDLE hFile;
312     BOOL   bRet = FALSE;
313     DWORD  count;
314     LARGE_INTEGER zero, oldPos;
315     /* FIXME, find out if there is a name for this GUID */
316     static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
317     static const GUID cabGUID = { 0xc689aaba, 0x8e78, 0x11d0, {0x8c,0x47,0x00,0xc0,0x4f,0xc2,0x95,0xee }};
318     static const WORD dosHdr = IMAGE_DOS_SIGNATURE;
319     static const BYTE cabHdr[] = { 'M','S','C','F' };
320     BYTE hdr[SIP_MAX_MAGIC_NUMBER];
321     WCHAR szFullKey[ 0x100 ];
322     LONG r = ERROR_SUCCESS;
323     HKEY key;
324
325     TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
326
327     if (!pgSubject || (!FileName && !hFileIn))
328     {
329         SetLastError(ERROR_INVALID_PARAMETER);
330         return FALSE;
331     }
332
333     /* Set pgSubject to zero's */
334     memset(pgSubject, 0 , sizeof(GUID));
335
336     if (hFileIn)
337         /* Use the given handle, make sure not to close this one ourselves */
338         hFile = hFileIn;
339     else
340     {
341         hFile = CreateFileW(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
342         /* Last error is set by CreateFile */
343         if (hFile == INVALID_HANDLE_VALUE) return FALSE;
344     }
345
346     zero.QuadPart = 0;
347     SetFilePointerEx(hFile, zero, &oldPos, FILE_CURRENT);
348     SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
349     if (!ReadFile(hFile, hdr, sizeof(hdr), &count, NULL))
350         goto cleanup;
351
352     if (count < SIP_MAX_MAGIC_NUMBER)
353     {
354         SetLastError(ERROR_INVALID_PARAMETER);
355         goto cleanup;
356     }
357
358     /* As everything is in place now we start looking at the file header */
359     if (!memcmp(hdr, &dosHdr, sizeof(dosHdr)))
360     {
361         *pgSubject = unknown;
362         SetLastError(S_OK);
363         bRet = TRUE;
364         goto cleanup;
365     }
366     /* Quick-n-dirty check for a cab file. */
367     if (!memcmp(hdr, cabHdr, sizeof(cabHdr)))
368     {
369         *pgSubject = cabGUID;
370         SetLastError(S_OK);
371         bRet = TRUE;
372         goto cleanup;
373     }
374
375     /* Check for supported functions using CryptSIPDllIsMyFileType */
376     /* max length of szFullKey depends on our code only, so we won't overrun */
377     lstrcpyW(szFullKey, szOID);
378     lstrcatW(szFullKey, szIsMyFile);
379     r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szFullKey, 0, KEY_READ, &key);
380     if (r == ERROR_SUCCESS)
381     {
382         DWORD index = 0, size;
383         WCHAR subKeyName[MAX_PATH];
384
385         do {
386             size = sizeof(subKeyName) / sizeof(subKeyName[0]);
387             r = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL,
388              NULL, NULL);
389             if (r == ERROR_SUCCESS)
390             {
391                 HKEY subKey;
392
393                 r = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
394                 if (r == ERROR_SUCCESS)
395                 {
396                     HMODULE lib;
397                     pfnIsFileSupported isMy = CRYPT_LoadSIPFuncFromKey(subKey,
398                      &lib);
399
400                     if (isMy)
401                     {
402                         bRet = isMy(hFile, pgSubject);
403                         FreeLibrary(lib);
404                     }
405                     RegCloseKey(subKey);
406                 }
407             }
408         } while (!bRet && r == ERROR_SUCCESS);
409         RegCloseKey(key);
410     }
411
412     /* Check for supported functions using CryptSIPDllIsMyFileType2 */
413     if (!bRet)
414     {
415         lstrcpyW(szFullKey, szOID);
416         lstrcatW(szFullKey, szIsMyFile2);
417         r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, szFullKey, 0, KEY_READ, &key);
418         if (r == ERROR_SUCCESS)
419         {
420             DWORD index = 0, size;
421             WCHAR subKeyName[MAX_PATH];
422
423             do {
424                 size = sizeof(subKeyName) / sizeof(subKeyName[0]);
425                 r = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL,
426                  NULL, NULL);
427                 if (r == ERROR_SUCCESS)
428                 {
429                     HKEY subKey;
430
431                     r = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
432                     if (r == ERROR_SUCCESS)
433                     {
434                         HMODULE lib;
435                         pfnIsFileSupportedName isMy2 =
436                          CRYPT_LoadSIPFuncFromKey(subKey, &lib);
437
438                         if (isMy2)
439                         {
440                             bRet = isMy2((LPWSTR)FileName, pgSubject);
441                             FreeLibrary(lib);
442                         }
443                         RegCloseKey(subKey);
444                     }
445                 }
446             } while (!bRet && r == ERROR_SUCCESS);
447             RegCloseKey(key);
448         }
449     }
450
451     if (!bRet)
452         SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
453
454 cleanup:
455     /* If we didn't open this one we shouldn't close it (hFile is a copy),
456      * but we should reset the file pointer to its original position.
457      */
458     if (!hFileIn)
459         CloseHandle(hFile);
460     else
461         SetFilePointerEx(hFile, oldPos, NULL, FILE_BEGIN);
462
463     return bRet;
464 }
465
466 static LONG CRYPT_OpenSIPFunctionKey(const GUID *guid, LPCWSTR function,
467  HKEY *key)
468 {
469     WCHAR szFullKey[ 0x100 ];
470
471     lstrcpyW(szFullKey, szOID);
472     lstrcatW(szFullKey, function);
473     CRYPT_guid2wstr(guid, &szFullKey[lstrlenW(szFullKey)]);
474     return RegOpenKeyExW(HKEY_LOCAL_MACHINE, szFullKey, 0, KEY_READ, key);
475 }
476
477 /* Loads the function named function for the SIP specified by pgSubject, and
478  * returns it if found.  Returns NULL on error.  If the function is loaded,
479  * *pLib is set to the library in which it is found.
480  */
481 static void *CRYPT_LoadSIPFunc(const GUID *pgSubject, LPCWSTR function,
482  HMODULE *pLib)
483 {
484     LONG r;
485     HKEY key;
486     void *func = NULL;
487
488     TRACE("(%s, %s)\n", debugstr_guid(pgSubject), debugstr_w(function));
489
490     r = CRYPT_OpenSIPFunctionKey(pgSubject, function, &key);
491     if (!r)
492     {
493         func = CRYPT_LoadSIPFuncFromKey(key, pLib);
494         RegCloseKey(key);
495     }
496     TRACE("returning %p\n", func);
497     return func;
498 }
499
500 typedef struct _WINE_SIP_PROVIDER {
501     GUID              subject;
502     SIP_DISPATCH_INFO info;
503     struct list       entry;
504 } WINE_SIP_PROVIDER;
505
506 static struct list providers = { &providers, &providers };
507 static CRITICAL_SECTION providers_cs;
508 static CRITICAL_SECTION_DEBUG providers_cs_debug =
509 {
510     0, 0, &providers_cs,
511     { &providers_cs_debug.ProcessLocksList,
512     &providers_cs_debug.ProcessLocksList },
513     0, 0, { (DWORD_PTR)(__FILE__ ": providers_cs") }
514 };
515 static CRITICAL_SECTION providers_cs = { &providers_cs_debug, -1, 0, 0, 0, 0 };
516
517 static void CRYPT_CacheSIP(const GUID *pgSubject, SIP_DISPATCH_INFO *info)
518 {
519     WINE_SIP_PROVIDER *prov = CryptMemAlloc(sizeof(WINE_SIP_PROVIDER));
520
521     if (prov)
522     {
523         prov->subject = *pgSubject;
524         prov->info = *info;
525         EnterCriticalSection(&providers_cs);
526         list_add_tail(&providers, &prov->entry);
527         LeaveCriticalSection(&providers_cs);
528     }
529 }
530
531 static WINE_SIP_PROVIDER *CRYPT_GetCachedSIP(const GUID *pgSubject)
532 {
533     WINE_SIP_PROVIDER *provider = NULL, *ret = NULL;
534
535     EnterCriticalSection(&providers_cs);
536     LIST_FOR_EACH_ENTRY(provider, &providers, WINE_SIP_PROVIDER, entry)
537     {
538         if (IsEqualGUID(pgSubject, &provider->subject))
539             break;
540     }
541     if (provider && IsEqualGUID(pgSubject, &provider->subject))
542         ret = provider;
543     LeaveCriticalSection(&providers_cs);
544     return ret;
545 }
546
547 static inline BOOL CRYPT_IsSIPCached(const GUID *pgSubject)
548 {
549     return CRYPT_GetCachedSIP(pgSubject) != NULL;
550 }
551
552 void crypt_sip_free(void)
553 {
554     WINE_SIP_PROVIDER *prov, *next;
555
556     LIST_FOR_EACH_ENTRY_SAFE(prov, next, &providers, WINE_SIP_PROVIDER, entry)
557     {
558         list_remove(&prov->entry);
559         FreeLibrary(prov->info.hSIP);
560         CryptMemFree(prov);
561     }
562 }
563
564 /* Loads the SIP for pgSubject into the global cache.  Returns FALSE if the
565  * SIP isn't registered or is invalid.
566  */
567 static BOOL CRYPT_LoadSIP(const GUID *pgSubject)
568 {
569     SIP_DISPATCH_INFO sip = { 0 };
570     HMODULE lib = NULL, temp = NULL;
571
572     sip.pfGet = CRYPT_LoadSIPFunc(pgSubject, szGetSigned, &lib);
573     if (!sip.pfGet)
574         goto error;
575     sip.pfPut = CRYPT_LoadSIPFunc(pgSubject, szPutSigned, &temp);
576     if (!sip.pfPut || temp != lib)
577         goto error;
578     FreeLibrary(temp);
579     sip.pfCreate = CRYPT_LoadSIPFunc(pgSubject, szCreate, &temp);
580     if (!sip.pfCreate || temp != lib)
581         goto error;
582     FreeLibrary(temp);
583     sip.pfVerify = CRYPT_LoadSIPFunc(pgSubject, szVerify, &temp);
584     if (!sip.pfVerify || temp != lib)
585         goto error;
586     FreeLibrary(temp);
587     sip.pfRemove = CRYPT_LoadSIPFunc(pgSubject, szRemoveSigned, &temp);
588     if (!sip.pfRemove || temp != lib)
589         goto error;
590     FreeLibrary(temp);
591     sip.hSIP = lib;
592     CRYPT_CacheSIP(pgSubject, &sip);
593     return TRUE;
594
595 error:
596     FreeLibrary(lib);
597     FreeLibrary(temp);
598     SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
599     return FALSE;
600 }
601
602 /***********************************************************************
603  *             CryptSIPLoad (CRYPT32.@)
604  *
605  * Load some internal crypt32 functions into a SIP_DISPATCH_INFO structure.
606  *
607  * PARAMS
608  *  pgSubject    [I] The GUID.
609  *  dwFlags      [I] Flags.
610  *  pSipDispatch [I] The loaded functions.
611  *
612  * RETURNS
613  *  Success: TRUE. pSipDispatch contains the functions.
614  *  Failure: FALSE. (Look at GetLastError()).
615  *
616  * NOTES
617  *  CryptSIPLoad uses caching for the list of GUIDs and whether a SIP is
618  *  already loaded.
619  *
620  *  An application calls CryptSipLoad which will return a structure with the
621  *  function addresses of some internal crypt32 functions. The application will
622  *  then call these functions which will be forwarded to the appropriate SIP.
623  *
624  *  CryptSIPLoad will load the needed SIP but doesn't unload this dll. The unloading
625  *  is done when crypt32 is unloaded.
626  */
627 BOOL WINAPI CryptSIPLoad
628        (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
629 {
630     TRACE("(%s %d %p)\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);
631
632     if (!pgSubject || dwFlags != 0 || !pSipDispatch)
633     {
634         SetLastError(ERROR_INVALID_PARAMETER);
635         return FALSE;
636     }
637     if (!CRYPT_IsSIPCached(pgSubject) && !CRYPT_LoadSIP(pgSubject))
638         return FALSE;
639
640     pSipDispatch->hSIP = NULL;
641     pSipDispatch->pfGet = CryptSIPGetSignedDataMsg;
642     pSipDispatch->pfPut = CryptSIPPutSignedDataMsg;
643     pSipDispatch->pfCreate = CryptSIPCreateIndirectData;
644     pSipDispatch->pfVerify = CryptSIPVerifyIndirectData;
645     pSipDispatch->pfRemove = CryptSIPRemoveSignedDataMsg;
646
647     return TRUE;
648 }
649
650 /***********************************************************************
651  *             CryptSIPCreateIndirectData (CRYPT32.@)
652  */
653 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
654                                        SIP_INDIRECT_DATA* pIndirectData)
655 {
656     WINE_SIP_PROVIDER *sip;
657     BOOL ret = FALSE;
658
659     TRACE("(%p %p %p)\n", pSubjectInfo, pcbIndirectData, pIndirectData);
660
661     if ((sip = CRYPT_GetCachedSIP(pSubjectInfo->pgSubjectType)))
662         ret = sip->info.pfCreate(pSubjectInfo, pcbIndirectData, pIndirectData);
663     TRACE("returning %d\n", ret);
664     return ret;
665 }
666
667 /***********************************************************************
668  *             CryptSIPGetSignedDataMsg (CRYPT32.@)
669  */
670 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
671                                        DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
672 {
673     WINE_SIP_PROVIDER *sip;
674     BOOL ret = FALSE;
675
676     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
677           pcbSignedDataMsg, pbSignedDataMsg);
678
679     if ((sip = CRYPT_GetCachedSIP(pSubjectInfo->pgSubjectType)))
680         ret = sip->info.pfGet(pSubjectInfo, pdwEncodingType, dwIndex,
681          pcbSignedDataMsg, pbSignedDataMsg);
682     TRACE("returning %d\n", ret);
683     return ret;
684 }
685
686 /***********************************************************************
687  *             CryptSIPPutSignedDataMsg (CRYPT32.@)
688  */
689 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
690                                        DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
691 {
692     WINE_SIP_PROVIDER *sip;
693     BOOL ret = FALSE;
694
695     TRACE("(%p %d %p %d %p)\n", pSubjectInfo, pdwEncodingType, pdwIndex,
696           cbSignedDataMsg, pbSignedDataMsg);
697
698     if ((sip = CRYPT_GetCachedSIP(pSubjectInfo->pgSubjectType)))
699         ret = sip->info.pfPut(pSubjectInfo, pdwEncodingType, pdwIndex,
700          cbSignedDataMsg, pbSignedDataMsg);
701     TRACE("returning %d\n", ret);
702     return ret;
703 }
704
705 /***********************************************************************
706  *             CryptSIPRemoveSignedDataMsg (CRYPT32.@)
707  */
708 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
709                                        DWORD dwIndex)
710 {
711     WINE_SIP_PROVIDER *sip;
712     BOOL ret = FALSE;
713
714     TRACE("(%p %d)\n", pSubjectInfo, dwIndex);
715
716     if ((sip = CRYPT_GetCachedSIP(pSubjectInfo->pgSubjectType)))
717         ret = sip->info.pfRemove(pSubjectInfo, dwIndex);
718     TRACE("returning %d\n", ret);
719     return ret;
720 }
721
722 /***********************************************************************
723  *             CryptSIPVerifyIndirectData (CRYPT32.@)
724  */
725 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
726                                        SIP_INDIRECT_DATA* pIndirectData)
727 {
728     WINE_SIP_PROVIDER *sip;
729     BOOL ret = FALSE;
730
731     TRACE("(%p %p)\n", pSubjectInfo, pIndirectData);
732
733     if ((sip = CRYPT_GetCachedSIP(pSubjectInfo->pgSubjectType)))
734         ret = sip->info.pfVerify(pSubjectInfo, pIndirectData);
735     TRACE("returning %d\n", ret);
736     return ret;
737 }