crypt32: Move SIP related functions to their own file.
[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 szBackSlash[] = { '\\', 0 };
45
46 static const WCHAR szPutSigned[] = {
47     'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
48 static const WCHAR szGetSigned[] = {
49     'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
50 static const WCHAR szRemoveSigned[] = {
51     'R','e','m','o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
52 static const WCHAR szCreate[] = {
53     'C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a',0};
54 static const WCHAR szVerify[] = {
55     'V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a',0};
56 static const WCHAR szIsMyFile[] = {
57     'I','s','M','y','F','i','l','e','T','y','p','e', 0 };
58 static const WCHAR szIsMyFile2[] = {
59     'I','s','M','y','F','i','l','e','T','y','p','e','2', 0};
60
61 /* convert a guid to a wide character string */
62 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
63 {
64     char str[40];
65
66     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
67            guid->Data1, guid->Data2, guid->Data3,
68            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
69            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
70     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
71 }
72
73 /***********************************************************************
74  *              CRYPT_SIPDeleteFunction
75  *
76  * Helper function for CryptSIPRemoveProvider
77  */
78 static LONG CRYPT_SIPDeleteFunction( LPGUID guid, LPCWSTR szKey )
79 {
80     WCHAR szFullKey[ 0x100 ];
81     LONG r = ERROR_SUCCESS;
82
83     /* max length of szFullKey depends on our code only, so we won't overrun */
84     lstrcpyW( szFullKey, szOID );
85     lstrcatW( szFullKey, szKey );
86     lstrcatW( szFullKey, szBackSlash );
87     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
88     lstrcatW( szFullKey, szBackSlash );
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( LPGUID guid, LPCWSTR szKey, 
155               LPCWSTR szDll, LPCWSTR szFunction )
156 {
157     static const WCHAR szDllName[] = { 'D','l','l',0 };
158     static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
159     WCHAR szFullKey[ 0x100 ];
160     LONG r = ERROR_SUCCESS;
161     HKEY hKey;
162
163     if( !szFunction )
164          return ERROR_SUCCESS;
165
166     /* max length of szFullKey depends on our code only, so we won't overrun */
167     lstrcpyW( szFullKey, szOID );
168     lstrcatW( szFullKey, szKey );
169     lstrcatW( szFullKey, szBackSlash );
170     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
171     lstrcatW( szFullKey, szBackSlash );
172
173     TRACE("key is %s\n", debugstr_w( szFullKey ) );
174
175     r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
176     if( r != ERROR_SUCCESS ) goto error_close_key;
177
178     /* write the values */
179     r = RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
180                         ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
181     if( r != ERROR_SUCCESS ) goto error_close_key;
182     r = RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
183                         ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
184
185 error_close_key:
186
187     RegCloseKey( hKey );
188
189     return r;
190 }
191
192 /***********************************************************************
193  *             CryptSIPAddProvider (CRYPT32.@)
194  *
195  * Add a SIP provider and its functions to the registry.
196  *
197  * PARAMS
198  *  psNewProv       [I] Pointer to a structure with information about
199  *                      the functions this SIP provider can perform.
200  *
201  * RETURNS
202  *  Success: TRUE.
203  *  Failure: FALSE. (Look at GetLastError()).
204  *
205  * NOTES
206  *  Registry errors are always reported via SetLastError(). If a
207  *  registry error occurs the rest of the registry write operations
208  *  will be skipped.
209  */
210 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
211 {
212     LONG r = ERROR_SUCCESS;
213
214     TRACE("%p\n", psNewProv);
215
216     if (!psNewProv ||
217         psNewProv->cbStruct != sizeof(SIP_ADD_NEWPROVIDER) ||
218         !psNewProv->pwszGetFuncName ||
219         !psNewProv->pwszPutFuncName ||
220         !psNewProv->pwszCreateFuncName ||
221         !psNewProv->pwszVerifyFuncName ||
222         !psNewProv->pwszRemoveFuncName)
223     {
224         SetLastError(ERROR_INVALID_PARAMETER);
225         return FALSE;
226     }
227
228     TRACE("%s %s %s %s %s\n",
229           debugstr_guid( psNewProv->pgSubject ),
230           debugstr_w( psNewProv->pwszDLLFileName ),
231           debugstr_w( psNewProv->pwszMagicNumber ),
232           debugstr_w( psNewProv->pwszIsFunctionName ),
233           debugstr_w( psNewProv->pwszIsFunctionNameFmt2 ) );
234
235 #define CRYPT_SIPADDPROV( key, field ) \
236     r = CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
237            psNewProv->pwszDLLFileName, psNewProv->field); \
238     if (r != ERROR_SUCCESS) goto end_function
239
240     CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
241     CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
242     CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
243     CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
244     CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
245     CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionName );
246     CRYPT_SIPADDPROV( szIsMyFile2, pwszIsFunctionNameFmt2 );
247
248 #undef CRYPT_SIPADDPROV
249
250 end_function:
251
252     if (r != ERROR_SUCCESS)
253     {
254         SetLastError(r);
255         return FALSE;
256     }
257
258     return TRUE;
259 }
260
261 /***********************************************************************
262  *             CryptSIPRetrieveSubjectGuid (CRYPT32.@)
263  *
264  * Determine the right SIP GUID for the given file.
265  *
266  * PARAMS
267  *  FileName   [I] Filename.
268  *  hFileIn    [I] Optional handle to the file.
269  *  pgSubject  [O] The SIP's GUID.
270  *
271  * RETURNS
272  *  Success: TRUE. pgSubject contains the SIP GUID.
273  *  Failure: FALSE. (Look at GetLastError()).
274  *
275  */
276 BOOL WINAPI CryptSIPRetrieveSubjectGuid
277       (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
278 {
279     FIXME("(%s %p %p) stub!\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
280     return FALSE;
281 }
282
283 /***********************************************************************
284  *             CryptSIPLoad (CRYPT32.@)
285  *
286  * Load the functions for the given SIP.
287  *
288  * PARAMS
289  *  pgSubject    [I] The GUID.
290  *  dwFlags      [I] Flags.
291  *  pSipDispatch [I] The loaded functions.
292  *
293  * RETURNS
294  *  Success: TRUE. pSipDispatch contains the functions.
295  *  Failure: FALSE. (Look at GetLastError()).
296  *
297  * NOTES
298  *  Testing shows that (somehow) the table of functions is cached between
299  *  calls.
300  *
301  */
302 BOOL WINAPI CryptSIPLoad
303        (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
304 {
305     FIXME("(%s %ld %p) stub!\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);
306     return FALSE;
307 }