crypt32: Win64 printf format warning fixes.
[wine/hacks.git] / dlls / crypt32 / sip.c
blobd7936e090542a730c7a4ae28919d3590d4a4c556
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 /* convert a guid to a wide character string */
60 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
62 char str[40];
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 );
71 /***********************************************************************
72 * CRYPT_SIPDeleteFunction
74 * Helper function for CryptSIPRemoveProvider
76 static LONG CRYPT_SIPDeleteFunction( LPGUID guid, LPCWSTR szKey )
78 WCHAR szFullKey[ 0x100 ];
79 LONG r = ERROR_SUCCESS;
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 ) ] );
86 r = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szFullKey);
88 return r;
91 /***********************************************************************
92 * CryptSIPRemoveProvider (CRYPT32.@)
94 * Remove a SIP provider and its functions from the registry.
96 * PARAMS
97 * pgProv [I] Pointer to a GUID for this SIP provider
99 * RETURNS
100 * Success: TRUE.
101 * Failure: FALSE. (Look at GetLastError()).
103 * NOTES
104 * Registry errors are always reported via SetLastError(). Every registry
105 * deletion will be tried.
107 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
109 LONG r = ERROR_SUCCESS;
110 LONG remove_error = ERROR_SUCCESS;
112 TRACE("%s\n", debugstr_guid(pgProv));
114 if (!pgProv)
116 SetLastError(ERROR_INVALID_PARAMETER);
117 return FALSE;
121 #define CRYPT_SIPREMOVEPROV( key ) \
122 r = CRYPT_SIPDeleteFunction( pgProv, key); \
123 if (r != ERROR_SUCCESS) remove_error = r
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);
133 #undef CRYPT_SIPREMOVEPROV
135 if (remove_error != ERROR_SUCCESS)
137 SetLastError(remove_error);
138 return FALSE;
141 return TRUE;
145 * Helper for CryptSIPAddProvider
147 * Add a registry key containing a dll name and function under
148 * "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
150 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey,
151 LPCWSTR szDll, LPCWSTR szFunction )
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;
159 if( !szFunction )
160 return ERROR_SUCCESS;
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 ) ] );
167 TRACE("key is %s\n", debugstr_w( szFullKey ) );
169 r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
170 if( r != ERROR_SUCCESS ) goto error_close_key;
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) );
179 error_close_key:
181 RegCloseKey( hKey );
183 return r;
186 /***********************************************************************
187 * CryptSIPAddProvider (CRYPT32.@)
189 * Add a SIP provider and its functions to the registry.
191 * PARAMS
192 * psNewProv [I] Pointer to a structure with information about
193 * the functions this SIP provider can perform.
195 * RETURNS
196 * Success: TRUE.
197 * Failure: FALSE. (Look at GetLastError()).
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.
204 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
206 LONG r = ERROR_SUCCESS;
208 TRACE("%p\n", psNewProv);
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)
218 SetLastError(ERROR_INVALID_PARAMETER);
219 return FALSE;
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 ) );
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
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 );
242 #undef CRYPT_SIPADDPROV
244 end_function:
246 if (r != ERROR_SUCCESS)
248 SetLastError(r);
249 return FALSE;
252 return TRUE;
255 /***********************************************************************
256 * CryptSIPRetrieveSubjectGuid (CRYPT32.@)
258 * Determine the right SIP GUID for the given file.
260 * PARAMS
261 * FileName [I] Filename.
262 * hFileIn [I] Optional handle to the file.
263 * pgSubject [O] The SIP's GUID.
265 * RETURNS
266 * Success: TRUE. pgSubject contains the SIP GUID.
267 * Failure: FALSE. (Look at GetLastError()).
269 * NOTES
270 * On failure pgSubject will contain a NULL GUID.
271 * The handle is always preferred above the filename.
273 BOOL WINAPI CryptSIPRetrieveSubjectGuid
274 (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
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 }};
285 TRACE("(%s %p %p)\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
287 if (!pgSubject || (!FileName && !hFileIn))
289 SetLastError(ERROR_INVALID_PARAMETER);
290 return FALSE;
293 /* Set pgSubject to zero's */
294 memset(pgSubject, 0 , sizeof(GUID));
296 if (hFileIn)
297 /* Use the given handle, make sure not to close this one ourselves */
298 hFile = hFileIn;
299 else
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;
306 hFilemapped = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
307 /* Last error is set by CreateFileMapping */
308 if (!hFilemapped) goto cleanup3;
310 pMapped = MapViewOfFile(hFilemapped, FILE_MAP_READ, 0, 0, 0);
311 /* Last error is set by MapViewOfFile */
312 if (!pMapped) goto cleanup2;
314 /* Native checks it right here */
315 fileSize = GetFileSize(hFile, NULL);
316 if (fileSize < 4)
318 SetLastError(ERROR_INVALID_PARAMETER);
319 goto cleanup1;
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)
326 memcpy(pgSubject, &unknown, sizeof(GUID));
327 SetLastError(S_OK);
328 bRet = TRUE;
329 goto cleanup1;
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.
341 /* Let's set the most common error for now */
342 SetLastError(TRUST_E_SUBJECT_FORM_UNKNOWN);
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);
353 return bRet;
356 /***********************************************************************
357 * CryptSIPLoad (CRYPT32.@)
359 * Load the functions for the given SIP.
361 * PARAMS
362 * pgSubject [I] The GUID.
363 * dwFlags [I] Flags.
364 * pSipDispatch [I] The loaded functions.
366 * RETURNS
367 * Success: TRUE. pSipDispatch contains the functions.
368 * Failure: FALSE. (Look at GetLastError()).
370 * NOTES
371 * Testing shows that (somehow) the table of functions is cached between
372 * calls.
375 BOOL WINAPI CryptSIPLoad
376 (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
378 FIXME("(%s %d %p) stub!\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);
379 return FALSE;
382 /***********************************************************************
383 * CryptSIPCreateIndirectData (CRYPT32.@)
385 BOOL WINAPI CryptSIPCreateIndirectData(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pcbIndirectData,
386 SIP_INDIRECT_DATA* pIndirectData)
388 FIXME("(%p %p %p) stub\n", pSubjectInfo, pcbIndirectData, pIndirectData);
390 return FALSE;
393 /***********************************************************************
394 * CryptSIPGetSignedDataMsg (CRYPT32.@)
396 BOOL WINAPI CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD* pdwEncodingType,
397 DWORD dwIndex, DWORD* pcbSignedDataMsg, BYTE* pbSignedDataMsg)
399 FIXME("(%p %p %d %p %p) stub\n", pSubjectInfo, pdwEncodingType, dwIndex,
400 pcbSignedDataMsg, pbSignedDataMsg);
402 return FALSE;
405 /***********************************************************************
406 * CryptSIPPutSignedDataMsg (CRYPT32.@)
408 BOOL WINAPI CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo, DWORD pdwEncodingType,
409 DWORD* pdwIndex, DWORD cbSignedDataMsg, BYTE* pbSignedDataMsg)
411 FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo, pdwEncodingType, pdwIndex,
412 cbSignedDataMsg, pbSignedDataMsg);
414 return FALSE;
417 /***********************************************************************
418 * CryptSIPRemoveSignedDataMsg (CRYPT32.@)
420 BOOL WINAPI CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO* pSubjectInfo,
421 DWORD dwIndex)
423 FIXME("(%p %d) stub\n", pSubjectInfo, dwIndex);
425 return FALSE;
428 /***********************************************************************
429 * CryptSIPVerifyIndirectData (CRYPT32.@)
431 BOOL WINAPI CryptSIPVerifyIndirectData(SIP_SUBJECTINFO* pSubjectInfo,
432 SIP_INDIRECT_DATA* pIndirectData)
434 FIXME("(%p %p) stub\n", pSubjectInfo, pIndirectData);
436 return FALSE;