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 szBackSlash[] = { '\\', 0 };
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};
61 /* convert a guid to a wide character string */
62 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
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 );
73 /***********************************************************************
74 * CRYPT_SIPDeleteFunction
76 * Helper function for CryptSIPRemoveProvider
78 static LONG CRYPT_SIPDeleteFunction( LPGUID guid, LPCWSTR szKey )
80 WCHAR szFullKey[ 0x100 ];
81 LONG r = ERROR_SUCCESS;
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 );
90 r = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szFullKey);
95 /***********************************************************************
96 * CryptSIPRemoveProvider (CRYPT32.@)
98 * Remove a SIP provider and its functions from the registry.
101 * pgProv [I] Pointer to a GUID for this SIP provider
105 * Failure: FALSE. (Look at GetLastError()).
108 * Registry errors are always reported via SetLastError(). Every registry
109 * deletion will be tried.
111 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
113 LONG r = ERROR_SUCCESS;
114 LONG remove_error = ERROR_SUCCESS;
116 TRACE("%s\n", debugstr_guid(pgProv));
120 SetLastError(ERROR_INVALID_PARAMETER);
125 #define CRYPT_SIPREMOVEPROV( key ) \
126 r = CRYPT_SIPDeleteFunction( pgProv, key); \
127 if (r != ERROR_SUCCESS) remove_error = r
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);
137 #undef CRYPT_SIPREMOVEPROV
139 if (remove_error != ERROR_SUCCESS)
141 SetLastError(remove_error);
149 * Helper for CryptSIPAddProvider
151 * Add a registry key containing a dll name and function under
152 * "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
154 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey,
155 LPCWSTR szDll, LPCWSTR szFunction )
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;
164 return ERROR_SUCCESS;
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 );
173 TRACE("key is %s\n", debugstr_w( szFullKey ) );
175 r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
176 if( r != ERROR_SUCCESS ) goto error_close_key;
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) );
192 /***********************************************************************
193 * CryptSIPAddProvider (CRYPT32.@)
195 * Add a SIP provider and its functions to the registry.
198 * psNewProv [I] Pointer to a structure with information about
199 * the functions this SIP provider can perform.
203 * Failure: FALSE. (Look at GetLastError()).
206 * Registry errors are always reported via SetLastError(). If a
207 * registry error occurs the rest of the registry write operations
210 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
212 LONG r = ERROR_SUCCESS;
214 TRACE("%p\n", psNewProv);
217 psNewProv->cbStruct != sizeof(SIP_ADD_NEWPROVIDER) ||
218 !psNewProv->pwszGetFuncName ||
219 !psNewProv->pwszPutFuncName ||
220 !psNewProv->pwszCreateFuncName ||
221 !psNewProv->pwszVerifyFuncName ||
222 !psNewProv->pwszRemoveFuncName)
224 SetLastError(ERROR_INVALID_PARAMETER);
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 ) );
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
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 );
248 #undef CRYPT_SIPADDPROV
252 if (r != ERROR_SUCCESS)
261 /***********************************************************************
262 * CryptSIPRetrieveSubjectGuid (CRYPT32.@)
264 * Determine the right SIP GUID for the given file.
267 * FileName [I] Filename.
268 * hFileIn [I] Optional handle to the file.
269 * pgSubject [O] The SIP's GUID.
272 * Success: TRUE. pgSubject contains the SIP GUID.
273 * Failure: FALSE. (Look at GetLastError()).
276 BOOL WINAPI CryptSIPRetrieveSubjectGuid
277 (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
279 FIXME("(%s %p %p) stub!\n", wine_dbgstr_w(FileName), hFileIn, pgSubject);
283 /***********************************************************************
284 * CryptSIPLoad (CRYPT32.@)
286 * Load the functions for the given SIP.
289 * pgSubject [I] The GUID.
291 * pSipDispatch [I] The loaded functions.
294 * Success: TRUE. pSipDispatch contains the functions.
295 * Failure: FALSE. (Look at GetLastError()).
298 * Testing shows that (somehow) the table of functions is cached between
302 BOOL WINAPI CryptSIPLoad
303 (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
305 FIXME("(%s %ld %p) stub!\n", debugstr_guid(pgSubject), dwFlags, pSipDispatch);