* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / crypt32 / main.c
1 /*
2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2005 Juan Lang
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include "crypt32_private.h"
21
22 #include "i_cryptasn1tls.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
25
26 static HCRYPTPROV hDefProv;
27 HINSTANCE hInstance;
28
29 BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwReason, PVOID pvReserved)
30 {
31 switch (fdwReason)
32 {
33 case DLL_PROCESS_ATTACH:
34 hInstance = hInst;
35 DisableThreadLibraryCalls(hInst);
36 init_empty_store();
37 crypt_oid_init();
38 break;
39 case DLL_PROCESS_DETACH:
40 if (pvReserved) break;
41 crypt_oid_free();
42 crypt_sip_free();
43 root_store_free();
44 default_chain_engine_free();
45 if (hDefProv) CryptReleaseContext(hDefProv, 0);
46 break;
47 }
48 return TRUE;
49 }
50
51 HCRYPTPROV CRYPT_GetDefaultProvider(void)
52 {
53 if (!hDefProv)
54 {
55 HCRYPTPROV prov;
56
57 if (!CryptAcquireContextW(&prov, NULL, MS_ENH_RSA_AES_PROV_W,
58 PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
59 return hDefProv;
60 InterlockedCompareExchangePointer((PVOID *)&hDefProv, (PVOID)prov,
61 NULL);
62 if (hDefProv != prov)
63 CryptReleaseContext(prov, 0);
64 }
65 return hDefProv;
66 }
67
68 typedef void * HLRUCACHE;
69
70 /* this function is called by Internet Explorer when it is about to verify a
71 * downloaded component. The first parameter appears to be a pointer to an
72 * unknown type, native fails unless it points to a buffer of at least 20 bytes.
73 * The second parameter appears to be an out parameter, whatever it's set to is
74 * passed (by cryptnet.dll) to I_CryptFlushLruCache.
75 */
76 BOOL WINAPI I_CryptCreateLruCache(void *unknown, HLRUCACHE *out)
77 {
78 FIXME("(%p, %p): stub!\n", unknown, out);
79 *out = (void *)0xbaadf00d;
80 return TRUE;
81 }
82
83 BOOL WINAPI I_CryptFindLruEntry(DWORD unk0, DWORD unk1)
84 {
85 FIXME("(%08x, %08x): stub!\n", unk0, unk1);
86 return FALSE;
87 }
88
89 BOOL WINAPI I_CryptFindLruEntryData(DWORD unk0, DWORD unk1, DWORD unk2)
90 {
91 FIXME("(%08x, %08x, %08x): stub!\n", unk0, unk1, unk2);
92 return FALSE;
93 }
94
95 BOOL WINAPI I_CryptCreateLruEntry(HLRUCACHE h, DWORD unk0, DWORD unk1)
96 {
97 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
98 return FALSE;
99 }
100
101 DWORD WINAPI I_CryptFlushLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
102 {
103 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
104 return 0;
105 }
106
107 HLRUCACHE WINAPI I_CryptFreeLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
108 {
109 FIXME("(%p, %08x, %08x): stub!\n", h, unk0, unk1);
110 return h;
111 }
112
113 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
114 {
115 return HeapAlloc(GetProcessHeap(), 0, cbSize);
116 }
117
118 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
119 {
120 return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
121 }
122
123 VOID WINAPI CryptMemFree(LPVOID pv)
124 {
125 HeapFree(GetProcessHeap(), 0, pv);
126 }
127
128 DWORD WINAPI I_CryptAllocTls(void)
129 {
130 return TlsAlloc();
131 }
132
133 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
134 {
135 LPVOID ret;
136
137 ret = TlsGetValue(dwTlsIndex);
138 TlsSetValue(dwTlsIndex, NULL);
139 return ret;
140 }
141
142 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
143 {
144 return TlsGetValue(dwTlsIndex);
145 }
146
147 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
148 {
149 return TlsSetValue(dwTlsIndex, lpTlsValue);
150 }
151
152 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
153 {
154 BOOL ret;
155
156 TRACE("(%d, %d)\n", dwTlsIndex, unknown);
157
158 ret = TlsFree(dwTlsIndex);
159 if (!ret) SetLastError( E_INVALIDARG );
160 return ret;
161 }
162
163 BOOL WINAPI I_CryptGetOssGlobal(DWORD x)
164 {
165 FIXME("%08x\n", x);
166 return FALSE;
167 }
168
169 HCRYPTPROV WINAPI I_CryptGetDefaultCryptProv(DWORD reserved)
170 {
171 HCRYPTPROV ret;
172
173 TRACE("(%08x)\n", reserved);
174
175 if (reserved)
176 {
177 SetLastError(E_INVALIDARG);
178 return 0;
179 }
180 ret = CRYPT_GetDefaultProvider();
181 CryptContextAddRef(ret, NULL, 0);
182 return ret;
183 }
184
185 BOOL WINAPI I_CryptReadTrustedPublisherDWORDValueFromRegistry(LPCWSTR name,
186 DWORD *value)
187 {
188 static const WCHAR safer[] = {
189 'S','o','f','t','w','a','r','e','\\','P','o','l','i','c','i','e','s','\\',
190 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m',
191 'C','e','r','t','i','f','i','c','a','t','e','s','\\',
192 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r','\\',
193 'S','a','f','e','r',0 };
194 HKEY key;
195 LONG rc;
196 BOOL ret = FALSE;
197
198 TRACE("(%s, %p)\n", debugstr_w(name), value);
199
200 *value = 0;
201 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, safer, &key);
202 if (rc == ERROR_SUCCESS)
203 {
204 DWORD size = sizeof(DWORD);
205
206 if (!RegQueryValueExW(key, name, NULL, NULL, (LPBYTE)value, &size))
207 ret = TRUE;
208 RegCloseKey(key);
209 }
210 return ret;
211 }
212
213 DWORD WINAPI I_CryptInstallOssGlobal(DWORD x, DWORD y, DWORD z)
214 {
215 static int ret = 8;
216 ret++;
217 FIXME("%08x %08x %08x, return value %d\n", x, y, z,ret);
218 return ret;
219 }
220
221 BOOL WINAPI I_CryptInstallAsn1Module(ASN1module_t x, DWORD y, void* z)
222 {
223 FIXME("(%p %08x %p): stub\n", x, y, z);
224 return TRUE;
225 }
226
227 BOOL WINAPI I_CryptUninstallAsn1Module(HCRYPTASN1MODULE x)
228 {
229 FIXME("(%08x): stub\n", x);
230 return TRUE;
231 }
232
233 ASN1decoding_t WINAPI I_CryptGetAsn1Decoder(HCRYPTASN1MODULE x)
234 {
235 FIXME("(%08x): stub\n", x);
236 return NULL;
237 }
238
239 ASN1encoding_t WINAPI I_CryptGetAsn1Encoder(HCRYPTASN1MODULE x)
240 {
241 FIXME("(%08x): stub\n", x);
242 return NULL;
243 }