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