2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2005 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "crypt32_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
35 static HCRYPTPROV hDefProv;
37 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
41 case DLL_PROCESS_DETACH:
42 if (hDefProv) CryptReleaseContext(hDefProv, 0);
48 HCRYPTPROV CRYPT_GetDefaultProvider(void)
51 CryptAcquireContextW(&hDefProv, NULL, MS_ENHANCED_PROV_W,
52 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
56 /* this function is called by Internet Explorer when it is about to verify a downloaded component */
57 BOOL WINAPI I_CryptCreateLruCache(DWORD x, DWORD y)
63 /* these functions all have an unknown number of args */
64 BOOL WINAPI I_CryptFindLruEntryData(DWORD x)
70 BOOL WINAPI I_CryptFlushLruCache(DWORD x)
76 BOOL WINAPI I_CryptFreeLruCache(DWORD x)
82 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
88 /* convert a guid to a wide character string */
89 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
93 sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
94 guid->Data1, guid->Data2, guid->Data3,
95 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
96 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
97 MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
101 * Helper for CryptSIPAddProvider
103 * Add a registry key containing a dll name and function under
104 * "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
106 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey,
107 LPCWSTR szDll, LPCWSTR szFunction )
109 static const WCHAR szOID[] = {
110 'S','o','f','t','w','a','r','e','\\',
111 'M','i','c','r','o','s','o','f','t','\\',
112 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
114 'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
115 'C','r','y','p','t','S','I','P','D','l','l', 0 };
116 static const WCHAR szBackSlash[] = { '\\', 0 };
117 static const WCHAR szDllName[] = { 'D','l','l',0 };
118 static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
119 WCHAR szFullKey[ 0x100 ];
124 return ERROR_SUCCESS;
126 /* max length of szFullKey depends on our code only, so we won't overrun */
127 lstrcpyW( szFullKey, szOID );
128 lstrcatW( szFullKey, szKey );
129 lstrcatW( szFullKey, szBackSlash );
130 CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
131 lstrcatW( szFullKey, szBackSlash );
133 TRACE("key is %s\n", debugstr_w( szFullKey ) );
135 r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
136 if( r != ERROR_SUCCESS )
139 /* write the values */
140 RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
141 ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
142 RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
143 ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
147 return ERROR_SUCCESS;
150 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
152 static const WCHAR szCreate[] = {
153 'C','r','e','a','t','e',
154 'I','n','d','i','r','e','c','t','D','a','t','a',0};
155 static const WCHAR szGetSigned[] = {
156 'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
157 static const WCHAR szIsMyFile[] = {
158 'I','s','M','y','F','i','l','e','T','y','p','e', 0 };
159 static const WCHAR szPutSigned[] = {
160 'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
161 static const WCHAR szRemoveSigned[] = {
162 'R','e','m','o','v','e',
163 'S','i','g','n','e','d','D','a','t','a','M','s','g',0};
164 static const WCHAR szVerify[] = {
165 'V','e','r','i','f','y',
166 'I','n','d','i','r','e','c','t','D','a','t','a',0};
168 TRACE("%p\n", psNewProv);
173 TRACE("%s %s %s %s\n",
174 debugstr_guid( psNewProv->pgSubject ),
175 debugstr_w( psNewProv->pwszDLLFileName ),
176 debugstr_w( psNewProv->pwszMagicNumber ),
177 debugstr_w( psNewProv->pwszIsFunctionName ) );
179 #define CRYPT_SIPADDPROV( key, field ) \
180 CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
181 psNewProv->pwszDLLFileName, psNewProv->field)
183 CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
184 CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
185 CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
186 CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
187 CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
188 CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionNameFmt2 );
190 #undef CRYPT_SIPADDPROV
195 BOOL WINAPI CryptSIPRetrieveSubjectGuid
196 (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
202 BOOL WINAPI CryptSIPLoad
203 (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
209 BOOL WINAPI CryptRegisterDefaultOIDFunction(DWORD dwEncodingType,
210 LPCSTR pszFuncName, DWORD dwIndex,
213 FIXME("(%lx,%s,%lx,%s) stub!\n", dwEncodingType, pszFuncName, dwIndex,
214 debugstr_w(pwszDll));
224 static const struct OIDToAlgID oidToAlgID[] = {
225 { szOID_RSA_RSA, CALG_RSA_KEYX },
226 { szOID_RSA_MD2RSA, CALG_MD2 },
227 { szOID_RSA_MD4RSA, CALG_MD4 },
228 { szOID_RSA_MD5RSA, CALG_MD5 },
229 { szOID_RSA_SHA1RSA, CALG_SHA },
230 { szOID_RSA_DH, CALG_DH_SF },
231 { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM },
232 { szOID_RSA_SMIMEalgCMS3DESwrap, CALG_3DES },
233 { szOID_RSA_SMIMEalgCMSRC2wrap, CALG_RC2 },
234 { szOID_RSA_MD2, CALG_MD2 },
235 { szOID_RSA_MD4, CALG_MD4 },
236 { szOID_RSA_MD5, CALG_MD5 },
237 { szOID_RSA_RC2CBC, CALG_RC2 },
238 { szOID_RSA_RC4, CALG_RC4 },
239 { szOID_RSA_DES_EDE3_CBC, CALG_3DES },
240 { szOID_ANSI_X942_DH, CALG_DH_SF },
241 { szOID_X957_DSA, CALG_DSS_SIGN },
242 { szOID_X957_SHA1DSA, CALG_SHA },
243 { szOID_OIWSEC_md4RSA, CALG_MD4 },
244 { szOID_OIWSEC_md5RSA, CALG_MD5 },
245 { szOID_OIWSEC_md4RSA2, CALG_MD4 },
246 { szOID_OIWSEC_desCBC, CALG_DES },
247 { szOID_OIWSEC_dsa, CALG_DSS_SIGN },
248 { szOID_OIWSEC_shaDSA, CALG_SHA },
249 { szOID_OIWSEC_shaRSA, CALG_SHA },
250 { szOID_OIWSEC_sha, CALG_SHA },
251 { szOID_OIWSEC_rsaXchg, CALG_RSA_KEYX },
252 { szOID_OIWSEC_sha1, CALG_SHA },
253 { szOID_OIWSEC_dsaSHA1, CALG_SHA },
254 { szOID_OIWSEC_sha1RSASign, CALG_SHA },
255 { szOID_OIWDIR_md2RSA, CALG_MD2 },
256 { szOID_INFOSEC_mosaicUpdatedSig, CALG_SHA },
257 { szOID_INFOSEC_mosaicKMandUpdSig, CALG_DSS_SIGN },
260 LPCSTR WINAPI CertAlgIdToOID(DWORD dwAlgId)
265 return szOID_RSA_RSA;
267 return szOID_RSA_SMIMEalgESDH;
269 return szOID_RSA_MD2;
271 return szOID_RSA_MD4;
273 return szOID_RSA_MD5;
275 return szOID_RSA_RC2CBC;
277 return szOID_RSA_RC4;
279 return szOID_RSA_DES_EDE3_CBC;
281 return szOID_ANSI_X942_DH;
283 return szOID_X957_DSA;
285 return szOID_OIWSEC_desCBC;
287 return szOID_OIWSEC_sha1;
293 DWORD WINAPI CertOIDToAlgId(LPCSTR pszObjId)
299 for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++)
301 if (!strcmp(pszObjId, oidToAlgID[i].oid))
302 return oidToAlgID[i].algID;
308 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
310 return HeapAlloc(GetProcessHeap(), 0, cbSize);
313 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
315 return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
318 VOID WINAPI CryptMemFree(LPVOID pv)
320 HeapFree(GetProcessHeap(), 0, pv);
323 DWORD WINAPI I_CryptAllocTls(void)
328 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
332 ret = TlsGetValue(dwTlsIndex);
333 TlsSetValue(dwTlsIndex, NULL);
337 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
339 return TlsGetValue(dwTlsIndex);
342 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
344 return TlsSetValue(dwTlsIndex, lpTlsValue);
347 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
349 TRACE("(%ld, %ld)\n", dwTlsIndex, unknown);
350 return TlsFree(dwTlsIndex);