- define additional shell paths for CSIDL_... constants
[wine] / dlls / crypt32 / main.c
1 /*
2  * Copyright 2002 Mike McCormack for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20 #include <stdarg.h>
21 #include <stdio.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wincrypt.h"
26 #include "winreg.h"
27 #include "winnls.h"
28 #include "mssip.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32
33 /* this function is called by Internet Explorer when it is about to verify a downloaded component */
34 BOOL WINAPI I_CryptCreateLruCache(DWORD x, DWORD y)
35 {
36     FIXME("stub!\n");
37     return FALSE;
38 }
39
40 /* these functions all have an unknown number of args */
41 BOOL WINAPI I_CryptFindLruEntryData(DWORD x)
42 {
43     FIXME("stub!\n");
44     return FALSE;
45 }
46
47 BOOL WINAPI I_CryptFlushLruCache(DWORD x)
48 {
49     FIXME("stub!\n");
50     return FALSE;
51 }
52
53 BOOL WINAPI I_CryptFreeLruCache(DWORD x)
54 {
55     FIXME("stub!\n");
56     return FALSE;
57 }
58
59 BOOL WINAPI CryptProtectData(DATA_BLOB* pDataIn, LPCWSTR szDataDescr, DATA_BLOB* pOptionalEntropy,
60                              PVOID pvReserved, CRYPTPROTECT_PROMPTSTRUCT* pPromptStruct,
61                              DWORD dwFlags, DATA_BLOB* pDataOut)
62 {
63     FIXME("stub!\n");
64     return FALSE;
65 }
66
67 /*
68  * (0x1001350, %eax, 0, 0, 9);
69  *
70  */
71 BOOL WINAPI CertOpenStore(LPSTR dw1, DWORD dw2, DWORD dw3, DWORD dw4, DWORD dw5)
72 {
73   FIXME("(%s, %ld, %ld, %ld, %ld), stub.\n", debugstr_a(dw1), dw2, dw3, dw4, dw5);
74   return TRUE;
75 }
76
77 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
78 {
79     FIXME("stub!\n");
80     return FALSE;
81 }
82
83 /* convert a guid to a wide character string */
84 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
85 {
86     char str[40];
87
88     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
89            guid->Data1, guid->Data2, guid->Data3,
90            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
91            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
92     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
93 }
94
95 /*
96  * Helper for CryptSIPAddProvider
97  *
98  * Add a registry key containing a dll name and function under
99  *  "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
100  */
101 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey, 
102               LPCWSTR szDll, LPCWSTR szFunction )
103 {
104     const WCHAR szOID[] = {
105         'S','o','f','t','w','a','r','e','\\',
106         'M','i','c','r','o','s','o','f','t','\\',
107         'C','r','y','p','t','o','g','r','a','p','h','y','\\',
108         'O','I','D','\\',
109         'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
110         'C','r','y','p','t','S','I','P','D','l','l', 0 };
111     const WCHAR szBackSlash[] = { '\\', 0 };
112     const WCHAR szDllName[] = { 'D','l','l',0 };
113     const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
114     WCHAR szFullKey[ 0x100 ];
115     LONG r;
116     HKEY hKey;
117
118     if( !szFunction )
119          return ERROR_SUCCESS;
120
121     /* max length of szFullKey depends on our code only, so we won't overrun */
122     lstrcpyW( szFullKey, szOID );
123     lstrcatW( szFullKey, szKey );
124     lstrcatW( szFullKey, szBackSlash );
125     CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
126     lstrcatW( szFullKey, szBackSlash );
127
128     TRACE("key is %s\n", debugstr_w( szFullKey ) );
129
130     r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
131     if( r != ERROR_SUCCESS )
132         return r;
133
134     /* write the values */
135     RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (LPBYTE) szFunction,
136                     ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
137     RegSetValueExW( hKey, szDllName, 0, REG_SZ, (LPBYTE) szDll,
138                     ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
139
140     RegCloseKey( hKey );
141
142     return ERROR_SUCCESS;
143 }
144
145 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
146 {
147     const WCHAR szCreate[] = { 
148        'C','r','e','a','t','e',
149        'I','n','d','i','r','e','c','t','D','a','t','a',0};
150     const WCHAR szGetSigned[] = { 
151        'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
152     const WCHAR szIsMyFile[] = { 
153        'I','s','M','y','F','i','l','e','T','y','p','e', 0 };
154     const WCHAR szPutSigned[] = { 
155        'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
156     const WCHAR szRemoveSigned[] = {
157        'R','e','m','o','v','e',
158        'S','i','g','n','e','d','D','a','t','a','M','s','g',0};
159     const WCHAR szVerify[] = {
160        'V','e','r','i','f','y',
161        'I','n','d','i','r','e','c','t','D','a','t','a',0};
162
163     TRACE("%p\n", psNewProv);
164
165     if( !psNewProv )
166         return FALSE;
167
168     TRACE("%s %s %s %s\n",
169           debugstr_guid( psNewProv->pgSubject ),
170           debugstr_w( psNewProv->pwszDLLFileName ),
171           debugstr_w( psNewProv->pwszMagicNumber ),
172           debugstr_w( psNewProv->pwszIsFunctionName ) );
173
174 #define CRYPT_SIPADDPROV( key, field ) \
175     CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
176            psNewProv->pwszDLLFileName, psNewProv->field)
177
178     CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
179     CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
180     CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
181     CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
182     CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
183     CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionNameFmt2 );
184
185 #undef CRYPT_SIPADDPROV
186
187     return TRUE;
188 }
189
190 BOOL WINAPI CryptSIPRetrieveSubjectGuid
191       (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
192 {
193     FIXME("stub!\n");
194     return FALSE;
195 }
196
197 BOOL WINAPI CryptSIPLoad
198        (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
199 {
200     FIXME("stub!\n");
201     return FALSE;
202 }
203
204 BOOL WINAPI CryptGetOIDFunctionValue(DWORD dwEncodingType, LPCSTR pszFuncName,
205                                      LPCSTR pszOID, LPCWSTR pwszValueName,
206                                      DWORD *pdwValueType, BYTE *pbValueData,
207                                      DWORD *pcbValueData)
208 {
209     FIXME("(%lx,%s,%s,%s,%p,%p,%p) stub!\n", dwEncodingType, pszFuncName, pszOID,
210           debugstr_w(pwszValueName), pdwValueType, pbValueData, pcbValueData);
211     return FALSE;
212 }
213
214 BOOL WINAPI CryptRegisterDefaultOIDFunction(DWORD dwEncodingType,
215                                      LPCSTR pszFuncName, DWORD dwIndex,
216                                      LPCWSTR pwszDll)
217 {
218     FIXME("(%lx,%s,%lx,%s) stub!\n", dwEncodingType, pszFuncName, dwIndex,
219           debugstr_w(pwszDll));
220     return FALSE;
221 }
222
223 BOOL WINAPI CryptRegisterOIDFunction(DWORD dwEncodingType, LPCSTR pszFuncName,
224                   LPCSTR pszOID, LPCWSTR pwszDll, LPCSTR pszOverrideFuncName)
225 {
226     LONG r;
227     const char szOID[] = "Software\\Microsoft\\Cryptography\\OID";
228     const char szType1[] = "EncodingType 1";
229     const WCHAR szDllName[] = { 'D','l','l',0 };
230     HKEY hKey;
231     LPSTR szKey;
232     UINT len;
233
234     TRACE("%lx %s %s %s %s\n", dwEncodingType, pszFuncName, pszOID,
235           debugstr_w(pwszDll), pszOverrideFuncName);
236
237     if( dwEncodingType & PKCS_7_ASN_ENCODING )
238         FIXME("PKCS_7_ASN_ENCODING not implemented\n");
239
240     if( dwEncodingType & X509_ASN_ENCODING )
241     {
242         /* construct the name of the key */
243         len = sizeof szOID + sizeof szType1 + 2 +
244               lstrlenA( pszFuncName ) + lstrlenA( pszOID );
245         szKey = HeapAlloc( GetProcessHeap(), 0, len );
246         if( !szKey )
247             return FALSE;
248         sprintf( szKey, "%s\\%s\\%s\\%s",
249                    szOID, szType1, pszFuncName, pszOID );
250
251         TRACE("Key name is %s\n", debugstr_a( szKey ) );
252
253         r = RegCreateKeyA( HKEY_LOCAL_MACHINE, szKey, &hKey );
254         HeapFree( GetProcessHeap(), 0, szKey );
255         if( r != ERROR_SUCCESS )
256             return FALSE;
257
258         /* write the values */
259         RegSetValueExA( hKey, "FuncName", 0, REG_SZ, pszOverrideFuncName,
260                         lstrlenA( pszOverrideFuncName ) + 1 );
261         RegSetValueExW( hKey, szDllName, 0, REG_SZ, (LPBYTE) pwszDll,
262                         (lstrlenW( pwszDll ) + 1) * sizeof (WCHAR) );
263
264         RegCloseKey( hKey );
265     }
266
267     return TRUE;
268 }
269
270 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore, PCCERT_CONTEXT pPrev)
271 {
272     FIXME("(%p,%p)\n", hCertStore, pPrev);
273     return NULL;
274 }
275
276 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
277              DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
278 {
279     FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore, 
280           dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
281     return TRUE;
282 }
283
284 PCCRL_CONTEXT WINAPI CertCreateCRLContext( DWORD dwCertEncodingType,
285   const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
286 {
287     FIXME("%08lx %p %08lx\n", dwCertEncodingType, pbCrlEncoded, cbCrlEncoded);
288     return NULL;
289 }
290
291 BOOL WINAPI CertCloseStore( HCERTSTORE hCertStore, DWORD dwFlags )
292 {
293     FIXME("%p %08lx\n", hCertStore, dwFlags );
294     return TRUE;
295 }
296
297 BOOL WINAPI CertFreeCertificateContext( PCCERT_CONTEXT pCertContext )
298 {
299     FIXME("%p stub\n", pCertContext);
300     return TRUE;
301 }