wow32: Use spec file imports.
[wine.git] / dlls / crypt32 / main.c
blobb320c895e155c1bc832b74e3292b0cbebceeffaf
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "wincrypt.h"
28 #include "winreg.h"
29 #include "winuser.h"
30 #include "i_cryptasn1tls.h"
31 #include "crypt32_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
36 static HCRYPTPROV hDefProv;
37 HINSTANCE hInstance;
39 static CRITICAL_SECTION prov_param_cs;
40 static CRITICAL_SECTION_DEBUG prov_param_cs_debug =
42 0, 0, &prov_param_cs,
43 { &prov_param_cs_debug.ProcessLocksList,
44 &prov_param_cs_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": prov_param_cs") }
47 static CRITICAL_SECTION prov_param_cs = { &prov_param_cs_debug, -1, 0, 0, 0, 0 };
49 BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, PVOID pvReserved)
51 switch (reason)
53 case DLL_PROCESS_ATTACH:
54 hInstance = hInst;
55 DisableThreadLibraryCalls(hInst);
56 init_empty_store();
57 crypt_oid_init();
58 if (__wine_init_unix_call())
59 return FALSE;
60 CRYPT32_CALL( process_attach, NULL );
61 break;
62 case DLL_PROCESS_DETACH:
63 if (pvReserved) break;
64 crypt_oid_free();
65 crypt_sip_free();
66 default_chain_engine_free();
67 if (hDefProv) CryptReleaseContext(hDefProv, 0);
68 CRYPT32_CALL( process_detach, NULL );
69 break;
71 return TRUE;
74 static HCRYPTPROV CRYPT_GetDefaultProvider(void)
76 if (!hDefProv)
78 HCRYPTPROV prov;
80 if (!CryptAcquireContextW(&prov, NULL, MS_ENH_RSA_AES_PROV_W,
81 PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
82 return hDefProv;
83 InterlockedCompareExchangePointer((PVOID *)&hDefProv, (PVOID)prov,
84 NULL);
85 if (hDefProv != prov)
86 CryptReleaseContext(prov, 0);
88 return hDefProv;
91 typedef void * HLRUCACHE;
93 /* this function is called by Internet Explorer when it is about to verify a
94 * downloaded component. The first parameter appears to be a pointer to an
95 * unknown type, native fails unless it points to a buffer of at least 20 bytes.
96 * The second parameter appears to be an out parameter, whatever it's set to is
97 * passed (by cryptnet.dll) to I_CryptFlushLruCache.
99 BOOL WINAPI I_CryptCreateLruCache(void *unknown, HLRUCACHE *out)
101 FIXME("(%p, %p): stub!\n", unknown, out);
102 *out = (void *)0xbaadf00d;
103 return TRUE;
106 BOOL WINAPI I_CryptFindLruEntry(DWORD unk0, DWORD unk1)
108 FIXME("(%08lx, %08lx): stub!\n", unk0, unk1);
109 return FALSE;
112 BOOL WINAPI I_CryptFindLruEntryData(DWORD unk0, DWORD unk1, DWORD unk2)
114 FIXME("(%08lx, %08lx, %08lx): stub!\n", unk0, unk1, unk2);
115 return FALSE;
118 BOOL WINAPI I_CryptCreateLruEntry(HLRUCACHE h, DWORD unk0, DWORD unk1)
120 FIXME("(%p, %08lx, %08lx): stub!\n", h, unk0, unk1);
121 return FALSE;
124 DWORD WINAPI I_CryptFlushLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
126 FIXME("(%p, %08lx, %08lx): stub!\n", h, unk0, unk1);
127 return 0;
130 HLRUCACHE WINAPI I_CryptFreeLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
132 FIXME("(%p, %08lx, %08lx): stub!\n", h, unk0, unk1);
133 return h;
136 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
138 return malloc(cbSize);
141 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
143 return realloc(pv, cbSize);
146 VOID WINAPI CryptMemFree(LPVOID pv)
148 free(pv);
151 DWORD WINAPI I_CryptAllocTls(void)
153 return TlsAlloc();
156 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
158 LPVOID ret;
160 ret = TlsGetValue(dwTlsIndex);
161 TlsSetValue(dwTlsIndex, NULL);
162 return ret;
165 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
167 return TlsGetValue(dwTlsIndex);
170 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
172 return TlsSetValue(dwTlsIndex, lpTlsValue);
175 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
177 BOOL ret;
179 TRACE("(%ld, %ld)\n", dwTlsIndex, unknown);
181 ret = TlsFree(dwTlsIndex);
182 if (!ret) SetLastError( E_INVALIDARG );
183 return ret;
186 BOOL WINAPI I_CryptGetOssGlobal(DWORD x)
188 FIXME("%08lx\n", x);
189 return FALSE;
192 static BOOL is_supported_algid(HCRYPTPROV prov, ALG_ID algid)
194 PROV_ENUMALGS prov_algs;
195 DWORD size = sizeof(prov_algs);
196 BOOL ret = FALSE;
198 /* This enumeration is not thread safe */
199 EnterCriticalSection(&prov_param_cs);
200 if (CryptGetProvParam(prov, PP_ENUMALGS, (BYTE *)&prov_algs, &size, CRYPT_FIRST))
204 if (prov_algs.aiAlgid == algid)
206 ret = TRUE;
207 break;
209 } while (CryptGetProvParam(prov, PP_ENUMALGS, (BYTE *)&prov_algs, &size, CRYPT_NEXT));
211 LeaveCriticalSection(&prov_param_cs);
212 return ret;
215 HCRYPTPROV WINAPI DECLSPEC_HOTPATCH I_CryptGetDefaultCryptProv(ALG_ID algid)
217 HCRYPTPROV prov, defprov;
219 TRACE("(%08x)\n", algid);
221 defprov = CRYPT_GetDefaultProvider();
223 if (algid && !is_supported_algid(defprov, algid))
225 DWORD i = 0, type, size;
227 while (CryptEnumProvidersW(i, NULL, 0, &type, NULL, &size))
229 WCHAR *name = CryptMemAlloc(size);
230 if (name)
232 if (CryptEnumProvidersW(i, NULL, 0, &type, name, &size))
234 if (CryptAcquireContextW(&prov, NULL, name, type, CRYPT_VERIFYCONTEXT))
236 if (is_supported_algid(prov, algid))
238 CryptMemFree(name);
239 return prov;
241 CryptReleaseContext(prov, 0);
244 CryptMemFree(name);
246 i++;
249 SetLastError(E_INVALIDARG);
250 return 0;
253 CryptContextAddRef(defprov, NULL, 0);
254 return defprov;
257 BOOL WINAPI I_CryptReadTrustedPublisherDWORDValueFromRegistry(LPCWSTR name,
258 DWORD *value)
260 HKEY key;
261 LONG rc;
262 BOOL ret = FALSE;
264 TRACE("(%s, %p)\n", debugstr_w(name), value);
266 *value = 0;
267 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, L"Software\\Policies\\Microsoft\\SystemCertificates\\TrustedPublisher\\Safer", &key);
268 if (rc == ERROR_SUCCESS)
270 DWORD size = sizeof(DWORD);
272 if (!RegQueryValueExW(key, name, NULL, NULL, (LPBYTE)value, &size))
273 ret = TRUE;
274 RegCloseKey(key);
276 return ret;
279 DWORD WINAPI I_CryptInstallOssGlobal(DWORD x, DWORD y, DWORD z)
281 static int ret = 8;
282 ret++;
283 FIXME("%08lx %08lx %08lx, return value %d\n", x, y, z,ret);
284 return ret;
287 BOOL WINAPI I_CryptInstallAsn1Module(ASN1module_t x, DWORD y, void* z)
289 FIXME("(%p %08lx %p): stub\n", x, y, z);
290 return TRUE;
293 BOOL WINAPI I_CryptUninstallAsn1Module(HCRYPTASN1MODULE x)
295 FIXME("(%08lx): stub\n", x);
296 return TRUE;
299 ASN1decoding_t WINAPI I_CryptGetAsn1Decoder(HCRYPTASN1MODULE x)
301 FIXME("(%08lx): stub\n", x);
302 return NULL;
305 ASN1encoding_t WINAPI I_CryptGetAsn1Encoder(HCRYPTASN1MODULE x)
307 FIXME("(%08lx): stub\n", x);
308 return NULL;
311 BOOL WINAPI CryptProtectMemory(void *data, DWORD len, DWORD flags)
313 static int fixme_once;
314 if (!fixme_once++) FIXME("(%p %lu %08lx): stub\n", data, len, flags);
315 return TRUE;
318 BOOL WINAPI CryptUnprotectMemory(void *data, DWORD len, DWORD flags)
320 static int fixme_once;
321 if (!fixme_once++) FIXME("(%p %lu %08lx): stub\n", data, len, flags);
322 return TRUE;