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