wgl: Move wglGetCurrentDC to gdi32.
[wine/dibdrv.git] / dlls / crypt32 / sip.c
blob8fe4b073446504dcb45769b870c45c16aac3acfd
1 /*
2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2005 Juan Lang
4 * Copyright 2006 Paul Vriens
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.
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.
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
21 #include <stdarg.h>
22 #include <stdio.h>
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"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
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 };
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};
59 static const WCHAR szDllName[] = { 'D','l','l',0 };
60 static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
62 /* convert a guid to a wide character string */
63 static void CRYPT_guid2wstr( const GUID *guid, LPWSTR wstr )
65 char str[40];
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 );
74 /***********************************************************************
75 * CRYPT_SIPDeleteFunction
77 * Helper function for CryptSIPRemoveProvider
79 static LONG CRYPT_SIPDeleteFunction( LPGUID guid, LPCWSTR szKey )
81 WCHAR szFullKey[ 0x100 ];
82 LONG r = ERROR_SUCCESS;
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 ) ] );
89 r = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szFullKey);
91 return r;
94 /***********************************************************************
95 * CryptSIPRemoveProvider (CRYPT32.@)
97 * Remove a SIP provider and its functions from the registry.
99 * PARAMS
100 * pgProv [I] Pointer to a GUID for this SIP provider
102 * RETURNS
103 * Success: TRUE.
104 * Failure: FALSE. (Look at GetLastError()).
106 * NOTES
107 * Registry errors are always reported via SetLastError(). Every registry
108 * deletion will be tried.
110 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
112 LONG r = ERROR_SUCCESS;
113 LONG remove_error = ERROR_SUCCESS;
115 TRACE("%s\n", debugstr_guid(pgProv));
117 if (!pgProv)
119 SetLastError(ERROR_INVALID_PARAMETER);
120 return FALSE;
124 #define CRYPT_SIPREMOVEPROV( key ) \
125 r = CRYPT_SIPDeleteFunction( pgProv, key); \
126 if (r != ERROR_SUCCESS) remove_error = r
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);
136 #undef CRYPT_SIPREMOVEPROV
138 if (remove_error != ERROR_SUCCESS)
140 SetLastError(remove_error);
141 return FALSE;
144 return TRUE;
148 * Helper for CryptSIPAddProvider
150 * Add a registry key containing a dll name and function under
151 * "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
153 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey,
154 LPCWSTR szDll, LPCWSTR szFunction )
156 WCHAR szFullKey[ 0x100 ];
157 LONG r = ERROR_SUCCESS;
158 HKEY hKey;
160 if( !szFunction )
161 return ERROR_SUCCESS;
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 ) ] );
168 TRACE("key is %s\n", debugstr_w( szFullKey ) );
170 r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
171 if( r != ERROR_SUCCESS ) goto error_close_key;
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) );
180 error_close_key:
182 RegCloseKey( hKey );
184 return r;
187 /***********************************************************************
188 * CryptSIPAddProvider (CRYPT32.@)
190 * Add a SIP provider and its functions to the registry.
192 * PARAMS
193 * psNewProv [I] Pointer to a structure with information about
194 * the functions this SIP provider can perform.
196 * RETURNS
197 * Success: TRUE.
198 * Failure: FALSE. (Look at GetLastError()).
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.
205 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
207 LONG r = ERROR_SUCCESS;
209 TRACE("%p\n", psNewProv);
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)
219 SetLastError(ERROR_INVALID_PARAMETER);
220 return FALSE;
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 ) );
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
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 );
243 #undef CRYPT_SIPADDPROV
245 end_function:
247 if (r != ERROR_SUCCESS)
249 SetLastError(r);
250 return FALSE;
253 return TRUE;
256 /***********************************************************************
257 * CryptSIPRetrieveSubjectGuid (CRYPT32.@)
259 * Determine the right SIP GUID for the given file.
261 * PARAMS
262 * FileName [I] Filename.
263 * hFileIn [I] Optional handle to the file.
264 * pgSubject [O] The SIP's GUID.
266 * RETURNS
267 * Success: TRUE. pgSubject contains the SIP GUID.
268 * Failure: FALSE. (Look at GetLastError()).
270 * NOTES
271 * On failure pgSubject will contain a NULL GUID.
272 * The handle is always preferred above the filename.
274 BOOL WINAPI CryptSIPRetrieveSubjectGuid
275 (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
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 }};
286 TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
288 if (!pgSubject || (!FileName && !hFileIn))
290 SetLastError(ERROR_INVALID_PARAMETER);
291 return FALSE;
294 /* Set pgSubject to zero's */
295 memset(pgSubject, 0 , sizeof(GUID));
297 if (hFileIn)
298 /* Use the given handle, make sure not to close this one ourselves */
299 hFile = hFileIn;
300 else
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;
307 hFilemapped = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
308 /* Last error is set by CreateFileMapping */
309 if (!hFilemapped) goto cleanup3;
311 pMapped = MapViewOfFile(hFilemapped, FILE_MAP_READ, 0, 0, 0);
312 /* Last error is set by MapViewOfFile */
313 if (!pMapped) goto cleanup2;
315 /* Native checks it right here */
316 fileSize = GetFileSize(hFile, NULL);
317 if (fileSize < 4)
319 SetLastError(ERROR_INVALID_PARAMETER);
320 goto cleanup1;
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)
327 memcpy(pgSubject, &unknown, sizeof(GUID));
328 SetLastError(S_OK);
329 bRet = TRUE;
330 goto cleanup1;
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.
342 /* Let's set the most common error for now */
343 SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
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);
354 return bRet;
357 /***********************************************************************
358 * CryptSIPLoad (CRYPT32.@)
360 * Load some internal crypt32 functions into a SIP_DISPATCH_INFO structure.
362 * PARAMS
363 * pgSubject [I] The GUID.
364 * dwFlags [I] Flags.
365 * pSipDispatch [I] The loaded functions.
367 * RETURNS
368 * Success: TRUE. pSipDispatch contains the functions.
369 * Failure: FALSE. (Look at GetLastError()).
371 * NOTES
372 * CryptSIPLoad uses caching for the list of GUIDs and whether a SIP is
373 * already loaded.
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.
379 * CryptSIPLoad will load the needed SIP but doesn't unload this dll. The unloading
380 * is done when crypt32 is unloaded.
382 BOOL WINAPI CryptSIPLoad
383 (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
385 FIXME("(%s %d %p) stub!\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);
386 return FALSE;
389 /***********************************************************************
390 * CryptSIPCreateIndirectData (CRYPT32.@)
392 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
393 SIP_INDIRECT_DATA* pIndirectData)
395 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
397 return FALSE;
400 /***********************************************************************
401 * CryptSIPGetSignedDataMsg (CRYPT32.@)
403 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
404 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
406 FIXME("(%p %p %d %p %p) stub\n", pSubjectInfo, pdwEncodingType, dwIndex,
407 pcbSignedDataMsg, pbSignedDataMsg);
409 return FALSE;
412 /***********************************************************************
413 * CryptSIPPutSignedDataMsg (CRYPT32.@)
415 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
416 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
418 FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo, pdwEncodingType, pdwIndex,
419 cbSignedDataMsg, pbSignedDataMsg);
421 return FALSE;
424 /***********************************************************************
425 * CryptSIPRemoveSignedDataMsg (CRYPT32.@)
427 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
428 DWORD dwIndex)
430 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
432 return FALSE;
435 /***********************************************************************
436 * CryptSIPVerifyIndirectData (CRYPT32.@)
438 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
439 SIP_INDIRECT_DATA* pIndirectData)
441 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
443 return FALSE;