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
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','\\',
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
)
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
);
94 /***********************************************************************
95 * CryptSIPRemoveProvider (CRYPT32.@)
97 * Remove a SIP provider and its functions from the registry.
100 * pgProv [I] Pointer to a GUID for this SIP provider
104 * Failure: FALSE. (Look at GetLastError()).
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
));
119 SetLastError(ERROR_INVALID_PARAMETER
);
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
);
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
;
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
) );
187 /***********************************************************************
188 * CryptSIPAddProvider (CRYPT32.@)
190 * Add a SIP provider and its functions to the registry.
193 * psNewProv [I] Pointer to a structure with information about
194 * the functions this SIP provider can perform.
198 * Failure: FALSE. (Look at GetLastError()).
201 * Registry errors are always reported via SetLastError(). If a
202 * registry error occurs the rest of the registry write operations
205 BOOL WINAPI
CryptSIPAddProvider(SIP_ADD_NEWPROVIDER
*psNewProv
)
207 LONG r
= ERROR_SUCCESS
;
209 TRACE("%p\n", 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
);
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
247 if (r
!= ERROR_SUCCESS
)
256 /***********************************************************************
257 * CryptSIPRetrieveSubjectGuid (CRYPT32.@)
259 * Determine the right SIP GUID for the given file.
262 * FileName [I] Filename.
263 * hFileIn [I] Optional handle to the file.
264 * pgSubject [O] The SIP's GUID.
267 * Success: TRUE. pgSubject contains the SIP GUID.
268 * Failure: FALSE. (Look at GetLastError()).
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
)
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
);
294 /* Set pgSubject to zero's */
295 memset(pgSubject
, 0 , sizeof(GUID
));
298 /* Use the given handle, make sure not to close this one ourselves */
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
);
319 SetLastError(ERROR_INVALID_PARAMETER
);
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
));
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
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 */
347 UnmapViewOfFile(pMapped
);
349 CloseHandle(hFilemapped
);
351 /* If we didn't open this one we shouldn't close it (hFile is a copy) */
352 if (!hFileIn
) CloseHandle(hFile
);
357 /***********************************************************************
358 * CryptSIPLoad (CRYPT32.@)
360 * Load some internal crypt32 functions into a SIP_DISPATCH_INFO structure.
363 * pgSubject [I] The GUID.
365 * pSipDispatch [I] The loaded functions.
368 * Success: TRUE. pSipDispatch contains the functions.
369 * Failure: FALSE. (Look at GetLastError()).
372 * CryptSIPLoad uses caching for the list of GUIDs and whether a SIP is
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
);
387 if (!pgSubject
|| dwFlags
!= 0 || !pSipDispatch
)
389 SetLastError(ERROR_INVALID_PARAMETER
);
396 /***********************************************************************
397 * CryptSIPCreateIndirectData (CRYPT32.@)
399 BOOL WINAPI
CryptSIPCreateIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pcbIndirectData
,
400 SIP_INDIRECT_DATA
* pIndirectData
)
402 FIXME("(%p %p %p) stub\n", pSubjectInfo
, pcbIndirectData
, pIndirectData
);
407 /***********************************************************************
408 * CryptSIPGetSignedDataMsg (CRYPT32.@)
410 BOOL WINAPI
CryptSIPGetSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD
* pdwEncodingType
,
411 DWORD dwIndex
, DWORD
* pcbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
413 FIXME("(%p %p %d %p %p) stub\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
414 pcbSignedDataMsg
, pbSignedDataMsg
);
419 /***********************************************************************
420 * CryptSIPPutSignedDataMsg (CRYPT32.@)
422 BOOL WINAPI
CryptSIPPutSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
, DWORD pdwEncodingType
,
423 DWORD
* pdwIndex
, DWORD cbSignedDataMsg
, BYTE
* pbSignedDataMsg
)
425 FIXME("(%p %d %p %d %p) stub\n", pSubjectInfo
, pdwEncodingType
, pdwIndex
,
426 cbSignedDataMsg
, pbSignedDataMsg
);
431 /***********************************************************************
432 * CryptSIPRemoveSignedDataMsg (CRYPT32.@)
434 BOOL WINAPI
CryptSIPRemoveSignedDataMsg(SIP_SUBJECTINFO
* pSubjectInfo
,
437 FIXME("(%p %d) stub\n", pSubjectInfo
, dwIndex
);
442 /***********************************************************************
443 * CryptSIPVerifyIndirectData (CRYPT32.@)
445 BOOL WINAPI
CryptSIPVerifyIndirectData(SIP_SUBJECTINFO
* pSubjectInfo
,
446 SIP_INDIRECT_DATA
* pIndirectData
)
448 FIXME("(%p %p) stub\n", pSubjectInfo
, pIndirectData
);