dmcompos: Replaced && 0xff by & 0xff.
[wine] / dlls / crypt32 / sip.c
1 /*
2  * Copyright 2002 Mike McCormack for CodeWeavers
3  * Copyright 2005 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
34 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
35
36 static const WCHAR szOID[] = {
37     'S','o','f','t','w','a','r','e','\\',
38     'M','i','c','r','o','s','o','f','t','\\',
39     'C','r','y','p','t','o','g','r','a','p','h','y','\\',
40     'O','I','D','\\',
41     'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
42     'C','r','y','p','t','S','I','P','D','l','l', 0 };
43
44 static const WCHAR szPutSigned[] = {
45     'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
46 static const WCHAR szGetSigned[] = {
47     'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
48 static const WCHAR szRemoveSigned[] = {
49     'R','e','m','o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g','\\',0};
50 static const WCHAR szCreate[] = {
51     'C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a','\\',0};
52 static const WCHAR szVerify[] = {
53     'V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a','\\',0};
54 static const WCHAR szIsMyFile[] = {
55     'I','s','M','y','F','i','l','e','T','y','p','e','\\',0};
56 static const WCHAR szIsMyFile2[] = {
57     'I','s','M','y','F','i','l','e','T','y','p','e','2','\\',0};
58
59 static const WCHAR szDllName[] = { 'D','l','l',0 };
60 static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
61
62 /* convert a guid to a wide character string */
63 static void CRYPT_guid2wstr( const GUID *guid, LPWSTR wstr )
64 {
65     char str[40];
66
67     sprintf(str, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
68            guid->Data1, guid->Data2, guid->Data3,
69            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
70            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
71     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
72 }
73
74 /***********************************************************************
75  *              CRYPT_SIPDeleteFunction
76  *
77  * Helper function for CryptSIPRemoveProvider
78  */
79 static LONG CRYPT_SIPDeleteFunction( LPGUID guid, LPCWSTR szKey )
80 {
81     WCHAR szFullKey[ 0x100 ];
82     LONG r = ERROR_SUCCESS;
83
84     /* max length of szFullKey depends on our code only, so we won't overrun */
85     lstrcpyW( szFullKey, szOID );
86     lstrcatW( szFullKey, szKey );
87     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
88
89     r = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szFullKey);
90
91     return r;
92 }
93
94 /***********************************************************************
95  *             CryptSIPRemoveProvider (CRYPT32.@)
96  *
97  * Remove a SIP provider and its functions from the registry.
98  *
99  * PARAMS
100  *  pgProv     [I] Pointer to a GUID for this SIP provider
101  *
102  * RETURNS
103  *  Success: TRUE.
104  *  Failure: FALSE. (Look at GetLastError()).
105  *
106  * NOTES
107  *  Registry errors are always reported via SetLastError(). Every registry
108  *  deletion will be tried.
109  */
110 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
111 {
112     LONG r = ERROR_SUCCESS;
113     LONG remove_error = ERROR_SUCCESS;
114
115     TRACE("%s\n", debugstr_guid(pgProv));
116
117     if (!pgProv)
118     {
119         SetLastError(ERROR_INVALID_PARAMETER);
120         return FALSE;
121     }
122
123
124 #define CRYPT_SIPREMOVEPROV( key ) \
125     r = CRYPT_SIPDeleteFunction( pgProv, key); \
126     if (r != ERROR_SUCCESS) remove_error = r
127
128     CRYPT_SIPREMOVEPROV( szPutSigned);
129     CRYPT_SIPREMOVEPROV( szGetSigned);
130     CRYPT_SIPREMOVEPROV( szRemoveSigned);
131     CRYPT_SIPREMOVEPROV( szCreate);
132     CRYPT_SIPREMOVEPROV( szVerify);
133     CRYPT_SIPREMOVEPROV( szIsMyFile);
134     CRYPT_SIPREMOVEPROV( szIsMyFile2);
135
136 #undef CRYPT_SIPREMOVEPROV
137
138     if (remove_error != ERROR_SUCCESS)
139     {
140         SetLastError(remove_error);
141         return FALSE;
142     }
143
144     return TRUE;
145 }
146
147 /*
148  * Helper for CryptSIPAddProvider
149  *
150  * Add a registry key containing a dll name and function under
151  *  "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
152  */
153 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey, 
154               LPCWSTR szDll, LPCWSTR szFunction )
155 {
156     WCHAR szFullKey[ 0x100 ];
157     LONG r = ERROR_SUCCESS;
158     HKEY hKey;
159
160     if( !szFunction )
161          return ERROR_SUCCESS;
162
163     /* max length of szFullKey depends on our code only, so we won't overrun */
164     lstrcpyW( szFullKey, szOID );
165     lstrcatW( szFullKey, szKey );
166     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
167
168     TRACE("key is %s\n", debugstr_w( szFullKey ) );
169
170     r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
171     if( r != ERROR_SUCCESS ) goto error_close_key;
172
173     /* write the values */
174     r = RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
175                         ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
176     if( r != ERROR_SUCCESS ) goto error_close_key;
177     r = RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
178                         ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
179
180 error_close_key:
181
182     RegCloseKey( hKey );
183
184     return r;
185 }
186
187 /***********************************************************************
188  *             CryptSIPAddProvider (CRYPT32.@)
189  *
190  * Add a SIP provider and its functions to the registry.
191  *
192  * PARAMS
193  *  psNewProv       [I] Pointer to a structure with information about
194  *                      the functions this SIP provider can perform.
195  *
196  * RETURNS
197  *  Success: TRUE.
198  *  Failure: FALSE. (Look at GetLastError()).
199  *
200  * NOTES
201  *  Registry errors are always reported via SetLastError(). If a
202  *  registry error occurs the rest of the registry write operations
203  *  will be skipped.
204  */
205 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
206 {
207     LONG r = ERROR_SUCCESS;
208
209     TRACE("%p\n", psNewProv);
210
211     if (!psNewProv ||
212         psNewProv->cbStruct != sizeof(SIP_ADD_NEWPROVIDER) ||
213         !psNewProv->pwszGetFuncName ||
214         !psNewProv->pwszPutFuncName ||
215         !psNewProv->pwszCreateFuncName ||
216         !psNewProv->pwszVerifyFuncName ||
217         !psNewProv->pwszRemoveFuncName)
218     {
219         SetLastError(ERROR_INVALID_PARAMETER);
220         return FALSE;
221     }
222
223     TRACE("%s %s %s %s %s\n",
224           debugstr_guid( psNewProv->pgSubject ),
225           debugstr_w( psNewProv->pwszDLLFileName ),
226           debugstr_w( psNewProv->pwszMagicNumber ),
227           debugstr_w( psNewProv->pwszIsFunctionName ),
228           debugstr_w( psNewProv->pwszIsFunctionNameFmt2 ) );
229
230 #define CRYPT_SIPADDPROV( key, field ) \
231     r = CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
232            psNewProv->pwszDLLFileName, psNewProv->field); \
233     if (r != ERROR_SUCCESS) goto end_function
234
235     CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
236     CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
237     CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
238     CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
239     CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
240     CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionName );
241     CRYPT_SIPADDPROV( szIsMyFile2, pwszIsFunctionNameFmt2 );
242
243 #undef CRYPT_SIPADDPROV
244
245 end_function:
246
247     if (r != ERROR_SUCCESS)
248     {
249         SetLastError(r);
250         return FALSE;
251     }
252
253     return TRUE;
254 }
255
256 /***********************************************************************
257  *             CryptSIPRetrieveSubjectGuid (CRYPT32.@)
258  *
259  * Determine the right SIP GUID for the given file.
260  *
261  * PARAMS
262  *  FileName   [I] Filename.
263  *  hFileIn    [I] Optional handle to the file.
264  *  pgSubject  [O] The SIP's GUID.
265  *
266  * RETURNS
267  *  Success: TRUE. pgSubject contains the SIP GUID.
268  *  Failure: FALSE. (Look at GetLastError()).
269  *
270  * NOTES
271  *  On failure pgSubject will contain a NULL GUID.
272  *  The handle is always preferred above the filename.
273  */
274 BOOL WINAPI CryptSIPRetrieveSubjectGuid
275       (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
276 {
277     HANDLE hFile;
278     HANDLE hFilemapped;
279     LPVOID pMapped;
280     BOOL   bRet = FALSE;
281     DWORD  fileSize;
282     IMAGE_DOS_HEADER *dos;
283     /* FIXME, find out if there is a name for this GUID */
284     static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,0x00,0xC0,0x4F,0xC2,0x95,0xEE }};
285
286     TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
287
288     if (!pgSubject || (!FileName && !hFileIn))
289     {
290         SetLastError(ERROR_INVALID_PARAMETER);
291         return FALSE;
292     }
293
294     /* Set pgSubject to zero's */
295     memset(pgSubject, 0 , sizeof(GUID));
296
297     if (hFileIn)
298         /* Use the given handle, make sure not to close this one ourselves */
299         hFile = hFileIn;
300     else
301     {
302         hFile = CreateFileW(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
303         /* Last error is set by CreateFile */
304         if (hFile == INVALID_HANDLE_VALUE) return FALSE;
305     }
306
307     hFilemapped = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
308     /* Last error is set by CreateFileMapping */
309     if (!hFilemapped) goto cleanup3;
310
311     pMapped = MapViewOfFile(hFilemapped, FILE_MAP_READ, 0, 0, 0);
312     /* Last error is set by MapViewOfFile */
313     if (!pMapped) goto cleanup2;
314
315     /* Native checks it right here */
316     fileSize = GetFileSize(hFile, NULL);
317     if (fileSize < 4)
318     {
319         SetLastError(ERROR_INVALID_PARAMETER);
320         goto cleanup1;
321     }
322
323     /* As everything is in place now we start looking at the file header */
324     dos = (IMAGE_DOS_HEADER *)pMapped;
325     if (dos->e_magic == IMAGE_DOS_SIGNATURE)
326     {
327         memcpy(pgSubject, &unknown, sizeof(GUID));
328         SetLastError(S_OK);
329         bRet = TRUE;
330         goto cleanup1;
331     }
332
333     /* FIXME
334      * There is a lot more to be checked:
335      * - Check for MSFC in the header
336      * - Check for the keys CryptSIPDllIsMyFileType and CryptSIPDllIsMyFileType2
337      *   under HKLM\Software\Microsoft\Cryptography\OID\EncodingType 0. Here are 
338      *   functions listed that need check if a SIP Provider can deal with the 
339      *   given file.
340      */
341
342     /* Let's set the most common error for now */
343     SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
344
345     /* The 3 different cleanups are here because we shouldn't overwrite the last error */
346 cleanup1:
347     UnmapViewOfFile(pMapped);
348 cleanup2:
349     CloseHandle(hFilemapped);
350 cleanup3:
351     /* If we didn't open this one we shouldn't close it (hFile is a copy) */
352     if (!hFileIn) CloseHandle(hFile);
353
354     return bRet;
355 }
356
357 /***********************************************************************
358  *             CryptSIPLoad (CRYPT32.@)
359  *
360  * Load some internal crypt32 functions into a SIP_DISPATCH_INFO structure.
361  *
362  * PARAMS
363  *  pgSubject    [I] The GUID.
364  *  dwFlags      [I] Flags.
365  *  pSipDispatch [I] The loaded functions.
366  *
367  * RETURNS
368  *  Success: TRUE. pSipDispatch contains the functions.
369  *  Failure: FALSE. (Look at GetLastError()).
370  *
371  * NOTES
372  *  CryptSIPLoad uses caching for the list of GUIDs and whether a SIP is
373  *  already loaded.
374  *
375  *  An application calls CryptSipLoad which will return a structure with the
376  *  function addresses of some internal crypt32 functions. The application will
377  *  then call these functions which will be forwarded to the appropriate SIP.
378  *
379  *  CryptSIPLoad will load the needed SIP but doesn't unload this dll. The unloading
380  *  is done when crypt32 is unloaded.
381  */
382 BOOL WINAPI CryptSIPLoad
383        (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
384 {
385     FIXME("(%s %d %p) stub!\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);
386
387     if (!pgSubject || dwFlags != 0 || !pSipDispatch)
388     {
389         SetLastError(ERROR_INVALID_PARAMETER);
390         return FALSE;
391     }
392
393     return FALSE;
394 }
395
396 /***********************************************************************
397  *             CryptSIPCreateIndirectData (CRYPT32.@)
398  */
399 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
400                                        SIP_INDIRECT_DATA* pIndirectData)
401 {
402     FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
403
404     return FALSE;
405 }
406
407 /***********************************************************************
408  *             CryptSIPGetSignedDataMsg (CRYPT32.@)
409  */
410 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
411                                        DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
412 {
413     FIXME("(%p %p %d %p %p) stub\n", pSubjectInfo, pdwEncodingType, dwIndex,
414           pcbSignedDataMsg, pbSignedDataMsg);
415
416     return FALSE;
417 }
418
419 /***********************************************************************
420  *             CryptSIPPutSignedDataMsg (CRYPT32.@)
421  */
422 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
423                                        DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
424 {
425     FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo, pdwEncodingType, pdwIndex,
426           cbSignedDataMsg, pbSignedDataMsg);
427
428     return FALSE;
429 }
430
431 /***********************************************************************
432  *             CryptSIPRemoveSignedDataMsg (CRYPT32.@)
433  */
434 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
435                                        DWORD dwIndex)
436 {
437     FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
438
439     return FALSE;
440 }
441
442 /***********************************************************************
443  *             CryptSIPVerifyIndirectData (CRYPT32.@)
444  */
445 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
446                                        SIP_INDIRECT_DATA* pIndirectData)
447 {
448     FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
449
450     return FALSE;
451 }