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