winecfg: Disable the desktop option when configuring a specific application.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "winnls.h"
29 #include "mssip.h"
30 #include "crypt32_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
34
35 static HCRYPTPROV hDefProv;
36
37 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
38 {
39     switch (fdwReason)
40     {
41         case DLL_PROCESS_ATTACH:
42             DisableThreadLibraryCalls(hInstance);
43             crypt_oid_init(hInstance);
44             break;
45         case DLL_PROCESS_DETACH:
46             crypt_oid_free();
47             if (hDefProv) CryptReleaseContext(hDefProv, 0);
48             break;
49     }
50     return TRUE;
51 }
52
53 HCRYPTPROV CRYPT_GetDefaultProvider(void)
54 {
55     if (!hDefProv)
56         CryptAcquireContextW(&hDefProv, NULL, MS_ENHANCED_PROV_W,
57          PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
58     return hDefProv;
59 }
60
61 typedef void * HLRUCACHE;
62
63 /* this function is called by Internet Explorer when it is about to verify a
64  * downloaded component.  The first parameter appears to be a pointer to an
65  * unknown type, native fails unless it points to a buffer of at least 20 bytes.
66  * The second parameter appears to be an out parameter, whatever it's set to is
67  * passed (by cryptnet.dll) to I_CryptFlushLruCache.
68  */
69 BOOL WINAPI I_CryptCreateLruCache(void *unknown, HLRUCACHE *out)
70 {
71     FIXME("(%p, %p): stub!\n", unknown, out);
72     *out = (void *)0xbaadf00d;
73     return TRUE;
74 }
75
76 BOOL WINAPI I_CryptFindLruEntryData(DWORD unk0, DWORD unk1, DWORD unk2)
77 {
78     FIXME("(%08lx, %08lx, %08lx): stub!\n", unk0, unk1, unk2);
79     return FALSE;
80 }
81
82 DWORD WINAPI I_CryptFlushLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
83 {
84     FIXME("(%p, %08lx, %08lx): stub!\n", h, unk0, unk1);
85     return 0;
86 }
87
88 HLRUCACHE WINAPI I_CryptFreeLruCache(HLRUCACHE h, DWORD unk0, DWORD unk1)
89 {
90     FIXME("(%p, %08lx, %08lx): stub!\n", h, unk0, unk1);
91     return h;
92 }
93
94 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
95 {
96     FIXME("stub!\n");
97     return FALSE;
98 }
99
100 /* convert a guid to a wide character string */
101 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
102 {
103     char str[40];
104
105     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
106            guid->Data1, guid->Data2, guid->Data3,
107            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
108            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
109     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
110 }
111
112 /*
113  * Helper for CryptSIPAddProvider
114  *
115  * Add a registry key containing a dll name and function under
116  *  "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
117  */
118 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey, 
119               LPCWSTR szDll, LPCWSTR szFunction )
120 {
121     static const WCHAR szOID[] = {
122         'S','o','f','t','w','a','r','e','\\',
123         'M','i','c','r','o','s','o','f','t','\\',
124         'C','r','y','p','t','o','g','r','a','p','h','y','\\',
125         'O','I','D','\\',
126         'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
127         'C','r','y','p','t','S','I','P','D','l','l', 0 };
128     static const WCHAR szBackSlash[] = { '\\', 0 };
129     static const WCHAR szDllName[] = { 'D','l','l',0 };
130     static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
131     WCHAR szFullKey[ 0x100 ];
132     LONG r;
133     HKEY hKey;
134
135     if( !szFunction )
136          return ERROR_SUCCESS;
137
138     /* max length of szFullKey depends on our code only, so we won't overrun */
139     lstrcpyW( szFullKey, szOID );
140     lstrcatW( szFullKey, szKey );
141     lstrcatW( szFullKey, szBackSlash );
142     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
143     lstrcatW( szFullKey, szBackSlash );
144
145     TRACE("key is %s\n", debugstr_w( szFullKey ) );
146
147     r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
148     if( r != ERROR_SUCCESS )
149         return r;
150
151     /* write the values */
152     RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
153                     ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
154     RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
155                     ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
156
157     RegCloseKey( hKey );
158
159     return ERROR_SUCCESS;
160 }
161
162 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
163 {
164     static const WCHAR szCreate[] = {
165        'C','r','e','a','t','e',
166        'I','n','d','i','r','e','c','t','D','a','t','a',0};
167     static const WCHAR szGetSigned[] = {
168        'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
169     static const WCHAR szIsMyFile[] = {
170        'I','s','M','y','F','i','l','e','T','y','p','e', 0 };
171     static const WCHAR szPutSigned[] = {
172        'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
173     static const WCHAR szRemoveSigned[] = {
174        'R','e','m','o','v','e',
175        'S','i','g','n','e','d','D','a','t','a','M','s','g',0};
176     static const WCHAR szVerify[] = {
177        'V','e','r','i','f','y',
178        'I','n','d','i','r','e','c','t','D','a','t','a',0};
179
180     TRACE("%p\n", psNewProv);
181
182     if( !psNewProv )
183         return FALSE;
184
185     TRACE("%s %s %s %s\n",
186           debugstr_guid( psNewProv->pgSubject ),
187           debugstr_w( psNewProv->pwszDLLFileName ),
188           debugstr_w( psNewProv->pwszMagicNumber ),
189           debugstr_w( psNewProv->pwszIsFunctionName ) );
190
191 #define CRYPT_SIPADDPROV( key, field ) \
192     CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
193            psNewProv->pwszDLLFileName, psNewProv->field)
194
195     CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
196     CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
197     CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
198     CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
199     CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
200     CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionNameFmt2 );
201
202 #undef CRYPT_SIPADDPROV
203
204     return TRUE;
205 }
206
207 BOOL WINAPI CryptSIPRetrieveSubjectGuid
208       (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
209 {
210     FIXME("stub!\n");
211     return FALSE;
212 }
213
214 BOOL WINAPI CryptSIPLoad
215        (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
216 {
217     FIXME("stub!\n");
218     return FALSE;
219 }
220
221 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
222 {
223     return HeapAlloc(GetProcessHeap(), 0, cbSize);
224 }
225
226 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
227 {
228     return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
229 }
230
231 VOID WINAPI CryptMemFree(LPVOID pv)
232 {
233     HeapFree(GetProcessHeap(), 0, pv);
234 }
235
236 DWORD WINAPI I_CryptAllocTls(void)
237 {
238     return TlsAlloc();
239 }
240
241 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
242 {
243     LPVOID ret;
244
245     ret = TlsGetValue(dwTlsIndex);
246     TlsSetValue(dwTlsIndex, NULL);
247     return ret;
248 }
249
250 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
251 {
252     return TlsGetValue(dwTlsIndex);
253 }
254
255 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
256 {
257     return TlsSetValue(dwTlsIndex, lpTlsValue);
258 }
259
260 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
261 {
262     TRACE("(%ld, %ld)\n", dwTlsIndex, unknown);
263     return TlsFree(dwTlsIndex);
264 }
265
266 BOOL WINAPI I_CryptGetOssGlobal(DWORD x)
267 {
268     FIXME("%08lx\n", x);
269     return FALSE;
270 }
271
272 BOOL WINAPI I_CryptInstallOssGlobal(DWORD x, DWORD y, DWORD z)
273 {
274     FIXME("%08lx %08lx %08lx\n", x, y, z);
275     return FALSE;
276 }
277
278 BOOL WINAPI I_CryptInstallAsn1Module(void *x, DWORD y, DWORD z)
279 {
280     FIXME("%p %08lx %08lx\n", x, y, z);
281     return TRUE;
282 }
283
284 BOOL WINAPI CryptQueryObject(DWORD dwObjectType, const void* pvObject,
285     DWORD dwExpectedContentTypeFlags, DWORD dwExpectedFormatTypeFlags,
286     DWORD dwFlags, DWORD* pdwMsgAndCertEncodingType, DWORD* pdwContentType,
287     DWORD* pdwFormatType, HCERTSTORE* phCertStore, HCRYPTMSG* phMsg,
288     const void** ppvContext)
289 {
290     FIXME( "%08lx %p %08lx %08lx %08lx %p %p %p %p %p %p", dwObjectType,
291            pvObject, dwExpectedContentTypeFlags, dwExpectedFormatTypeFlags,
292            dwFlags, pdwMsgAndCertEncodingType, pdwContentType, pdwFormatType,
293            phCertStore, phMsg, ppvContext);
294     return FALSE;
295 }
296
297 BOOL WINAPI CryptVerifyMessageSignature(/*PCRYPT_VERIFY_MESSAGE_PARA*/ void* pVerifyPara,
298           DWORD dwSignerIndex, const BYTE* pbSignedBlob, DWORD cbSignedBlob,
299           BYTE* pbDecoded, DWORD* pcbDecoded, PCCERT_CONTEXT* ppSignerCert)
300 {
301     FIXME("stub: %p, %ld, %p, %ld, %p, %p, %p\n",
302         pVerifyPara, dwSignerIndex, pbSignedBlob, cbSignedBlob,
303         pbDecoded, pcbDecoded, ppSignerCert);
304     return FALSE;
305 }