kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / crypt32 / main.c
blobd64fbbd2e6960c755227632a686f2145889b8881
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 "config.h"
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 "winuser.h"
29 #include "i_cryptasn1tls.h"
30 #include "crypt32_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
35 static HCRYPTPROV hDefProv;
36 HINSTANCE hInstance;
38 static CRITICAL_SECTION prov_param_cs;
39 static CRITICAL_SECTION_DEBUG prov_param_cs_debug =
41 0, 0, &prov_param_cs,
42 { &prov_param_cs_debug.ProcessLocksList,
43 &prov_param_cs_debug.ProcessLocksList },
44 0, 0, { (DWORD_PTR)(__FILE__ ": prov_param_cs") }
46 static CRITICAL_SECTION prov_param_cs = { &prov_param_cs_debug, -1, 0, 0, 0, 0 };
48 BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwReason, PVOID pvReserved)
50 switch (fdwReason)
52 case DLL_PROCESS_ATTACH:
53 hInstance = hInst;
54 DisableThreadLibraryCalls(hInst);
55 init_empty_store();
56 crypt_oid_init();
57 #ifdef SONAME_LIBGNUTLS
58 gnutls_initialize();
59 #endif
60 break;
61 case DLL_PROCESS_DETACH:
62 if (pvReserved) break;
63 crypt_oid_free();
64 crypt_sip_free();
65 default_chain_engine_free();
66 if (hDefProv) CryptReleaseContext(hDefProv, 0);
67 #ifdef SONAME_LIBGNUTLS
68 gnutls_uninitialize();
69 #endif
70 break;
72 return TRUE;
75 static HCRYPTPROV CRYPT_GetDefaultProvider(void)
77 if (!hDefProv)
79 HCRYPTPROV prov;
81 if (!CryptAcquireContextW(&prov, NULL, MS_ENH_RSA_AES_PROV_W,
82 PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
83 return hDefProv;
84 InterlockedCompareExchangePointer((PVOID *)&hDefProv, (PVOID)prov,
85 NULL);
86 if (hDefProv != prov)
87 CryptReleaseContext(prov, 0);
89 return hDefProv;
92 typedef void * HLRUCACHE;
94 /* this function is called by Internet Explorer when it is about to verify a
95 * downloaded component. The first parameter appears to be a pointer to an
96 * unknown type, native fails unless it points to a buffer of at least 20 bytes.
97 * The second parameter appears to be an out parameter, whatever it's set to is
98 * passed (by cryptnet.dll) to I_CryptFlushLruCache.
100 BOOL WINAPI I_CryptCreateLruCache(void *unknown, HLRUCACHE *out)
102 FIXME("(%p, %p): stub!\n", unknown, out);
103 *out = (void *)0xbaadf00d;
104 return TRUE;
107 BOOL WINAPI I_CryptFindLruEntry(DWORD unk0, DWORD unk1)
109 FIXME("(%08x, %08x): stub!\n", unk0, unk1);
110 return FALSE;
113 BOOL WINAPI I_CryptFindLruEntryData(DWORD unk0, DWORD unk1, DWORD unk2)
115 FIXME("(%08x, %08x, %08x): stub!\n", unk0, unk1, unk2);
116 return FALSE;
119 BOOL WINAPI I_CryptCreateLruEntry(HLRUCACHE h, DWORD unk0, DWORD unk1)
121 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
122 return FALSE;
125 DWORD WINAPI I_CryptFlushLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
127 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
128 return 0;
131 HLRUCACHE WINAPI I_CryptFreeLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
133 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
134 return h;
137 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
139 return HeapAlloc(GetProcessHeap(), 0, cbSize);
142 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
144 return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
147 VOID WINAPI CryptMemFree(LPVOID pv)
149 HeapFree(GetProcessHeap(), 0, pv);
152 DWORD WINAPI I_CryptAllocTls(void)
154 return TlsAlloc();
157 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
159 LPVOID ret;
161 ret = TlsGetValue(dwTlsIndex);
162 TlsSetValue(dwTlsIndex, NULL);
163 return ret;
166 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
168 return TlsGetValue(dwTlsIndex);
171 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
173 return TlsSetValue(dwTlsIndex, lpTlsValue);
176 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
178 BOOL ret;
180 TRACE("(%d, %d)\n", dwTlsIndex, unknown);
182 ret = TlsFree(dwTlsIndex);
183 if (!ret) SetLastError( E_INVALIDARG );
184 return ret;
187 BOOL WINAPI I_CryptGetOssGlobal(DWORD x)
189 FIXME("%08x\n", x);
190 return FALSE;
193 static BOOL is_supported_algid(HCRYPTPROV prov, ALG_ID algid)
195 PROV_ENUMALGS prov_algs;
196 DWORD size = sizeof(prov_algs);
197 BOOL ret = FALSE;
199 /* This enumeration is not thread safe */
200 EnterCriticalSection(&prov_param_cs);
201 if (CryptGetProvParam(prov, PP_ENUMALGS, (BYTE *)&prov_algs, &size, CRYPT_FIRST))
205 if (prov_algs.aiAlgid == algid)
207 ret = TRUE;
208 break;
210 } while (CryptGetProvParam(prov, PP_ENUMALGS, (BYTE *)&prov_algs, &size, CRYPT_NEXT));
212 LeaveCriticalSection(&prov_param_cs);
213 return ret;
216 HCRYPTPROV WINAPI DECLSPEC_HOTPATCH I_CryptGetDefaultCryptProv(ALG_ID algid)
218 HCRYPTPROV prov, defprov;
220 TRACE("(%08x)\n", algid);
222 defprov = CRYPT_GetDefaultProvider();
224 if (algid && !is_supported_algid(defprov, algid))
226 DWORD i = 0, type, size;
228 while (CryptEnumProvidersW(i, NULL, 0, &type, NULL, &size))
230 WCHAR *name = CryptMemAlloc(size);
231 if (name)
233 if (CryptEnumProvidersW(i, NULL, 0, &type, name, &size))
235 if (CryptAcquireContextW(&prov, NULL, name, type, CRYPT_VERIFYCONTEXT))
237 if (is_supported_algid(prov, algid))
239 CryptMemFree(name);
240 return prov;
242 CryptReleaseContext(prov, 0);
245 CryptMemFree(name);
247 i++;
250 SetLastError(E_INVALIDARG);
251 return 0;
254 CryptContextAddRef(defprov, NULL, 0);
255 return defprov;
258 BOOL WINAPI I_CryptReadTrustedPublisherDWORDValueFromRegistry(LPCWSTR name,
259 DWORD *value)
261 static const WCHAR safer[] = {
262 'S','o','f','t','w','a','r','e','\\','P','o','l','i','c','i','e','s','\\',
263 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
264 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
265 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r','\\',
266 'S','a','f','e','r',0 };
267 HKEY key;
268 LONG rc;
269 BOOL ret = FALSE;
271 TRACE("(%s, %p)\n", debugstr_w(name), value);
273 *value = 0;
274 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, safer, &key);
275 if (rc == ERROR_SUCCESS)
277 DWORD size = sizeof(DWORD);
279 if (!RegQueryValueExW(key, name, NULL, NULL, (LPBYTE)value, &size))
280 ret = TRUE;
281 RegCloseKey(key);
283 return ret;
286 DWORD WINAPI I_CryptInstallOssGlobal(DWORD x, DWORD y, DWORD z)
288 static int ret = 8;
289 ret++;
290 FIXME("%08x %08x %08x, return value %d\n", x, y, z,ret);
291 return ret;
294 BOOL WINAPI I_CryptInstallAsn1Module(ASN1module_t x, DWORD y, void* z)
296 FIXME("(%p %08x %p): stub\n", x, y, z);
297 return TRUE;
300 BOOL WINAPI I_CryptUninstallAsn1Module(HCRYPTASN1MODULE x)
302 FIXME("(%08x): stub\n", x);
303 return TRUE;
306 ASN1decoding_t WINAPI I_CryptGetAsn1Decoder(HCRYPTASN1MODULE x)
308 FIXME("(%08x): stub\n", x);
309 return NULL;
312 ASN1encoding_t WINAPI I_CryptGetAsn1Encoder(HCRYPTASN1MODULE x)
314 FIXME("(%08x): stub\n", x);
315 return NULL;
318 BOOL WINAPI CryptProtectMemory(void *data, DWORD len, DWORD flags)
320 static int fixme_once;
321 if (!fixme_once++) FIXME("(%p %u %08x): stub\n", data, len, flags);
322 return TRUE;
325 BOOL WINAPI CryptUnprotectMemory(void *data, DWORD len, DWORD flags)
327 static int fixme_once;
328 if (!fixme_once++) FIXME("(%p %u %08x): stub\n", data, len, flags);
329 return TRUE;