got a lot of crypt32 stuff working
[reactos.git] / reactos / lib / 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 "precomp.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
23
24 static HCRYPTPROV hDefProv;
25
26 struct OIDToAlgID
27 {
28 LPCSTR oid;
29 DWORD algID;
30 };
31
32 static const struct OIDToAlgID oidToAlgID[] = {
33 { szOID_RSA_RSA, CALG_RSA_KEYX },
34 { szOID_RSA_MD2RSA, CALG_MD2 },
35 { szOID_RSA_MD4RSA, CALG_MD4 },
36 { szOID_RSA_MD5RSA, CALG_MD5 },
37 { szOID_RSA_SHA1RSA, CALG_SHA },
38 { szOID_RSA_DH, CALG_DH_SF },
39 { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM },
40 { szOID_RSA_SMIMEalgCMS3DESwrap, CALG_3DES },
41 { szOID_RSA_SMIMEalgCMSRC2wrap, CALG_RC2 },
42 { szOID_RSA_MD2, CALG_MD2 },
43 { szOID_RSA_MD4, CALG_MD4 },
44 { szOID_RSA_MD5, CALG_MD5 },
45 { szOID_RSA_RC2CBC, CALG_RC2 },
46 { szOID_RSA_RC4, CALG_RC4 },
47 { szOID_RSA_DES_EDE3_CBC, CALG_3DES },
48 { szOID_ANSI_X942_DH, CALG_DH_SF },
49 { szOID_X957_DSA, CALG_DSS_SIGN },
50 { szOID_X957_SHA1DSA, CALG_SHA },
51 { szOID_OIWSEC_md4RSA, CALG_MD4 },
52 { szOID_OIWSEC_md5RSA, CALG_MD5 },
53 { szOID_OIWSEC_md4RSA2, CALG_MD4 },
54 { szOID_OIWSEC_desCBC, CALG_DES },
55 { szOID_OIWSEC_dsa, CALG_DSS_SIGN },
56 { szOID_OIWSEC_shaDSA, CALG_SHA },
57 { szOID_OIWSEC_shaRSA, CALG_SHA },
58 { szOID_OIWSEC_sha, CALG_SHA },
59 { szOID_OIWSEC_rsaXchg, CALG_RSA_KEYX },
60 { szOID_OIWSEC_sha1, CALG_SHA },
61 { szOID_OIWSEC_dsaSHA1, CALG_SHA },
62 { szOID_OIWSEC_sha1RSASign, CALG_SHA },
63 { szOID_OIWDIR_md2RSA, CALG_MD2 },
64 { szOID_INFOSEC_mosaicUpdatedSig, CALG_SHA },
65 { szOID_INFOSEC_mosaicKMandUpdSig, CALG_DSS_SIGN },
66 };
67
68 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
69 {
70 switch (fdwReason)
71 {
72 case DLL_PROCESS_DETACH:
73 if (hDefProv) CryptReleaseContext(hDefProv, 0);
74 break;
75 }
76 return TRUE;
77 }
78
79 HCRYPTPROV CRYPT_GetDefaultProvider(void)
80 {
81 if (!hDefProv)
82 CryptAcquireContextW(&hDefProv, NULL, MS_ENHANCED_PROV_W,
83 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
84 return hDefProv;
85 }
86
87 /* this function is called by Internet Explorer when it is about to verify a downloaded component */
88 BOOL WINAPI I_CryptCreateLruCache(DWORD x, DWORD y)
89 {
90 FIXME("stub!\n");
91 return FALSE;
92 }
93
94 /* these functions all have an unknown number of args */
95 BOOL WINAPI I_CryptFindLruEntryData(DWORD x)
96 {
97 FIXME("stub!\n");
98 return FALSE;
99 }
100
101 BOOL WINAPI I_CryptFlushLruCache(DWORD x)
102 {
103 FIXME("stub!\n");
104 return FALSE;
105 }
106
107 BOOL WINAPI I_CryptFreeLruCache(DWORD x)
108 {
109 FIXME("stub!\n");
110 return FALSE;
111 }
112
113 BOOL WINAPI CryptSIPRemoveProvider(GUID *pgProv)
114 {
115 FIXME("stub!\n");
116 return FALSE;
117 }
118
119 /* convert a guid to a wide character string */
120 static void CRYPT_guid2wstr( LPGUID guid, LPWSTR wstr )
121 {
122 char str[40];
123
124 sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
125 guid->Data1, guid->Data2, guid->Data3,
126 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
127 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
128 MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
129 }
130
131 /*
132 * Helper for CryptSIPAddProvider
133 *
134 * Add a registry key containing a dll name and function under
135 * "Software\\Microsoft\\Cryptography\\OID\\EncodingType 0\\<func>\\<guid>"
136 */
137 static LONG CRYPT_SIPWriteFunction( LPGUID guid, LPCWSTR szKey,
138 LPCWSTR szDll, LPCWSTR szFunction )
139 {
140 static const WCHAR szOID[] = {
141 'S','o','f','t','w','a','r','e','\\',
142 'M','i','c','r','o','s','o','f','t','\\',
143 'C','r','y','p','t','o','g','r','a','p','h','y','\\',
144 'O','I','D','\\',
145 'E','n','c','o','d','i','n','g','T','y','p','e',' ','0','\\',
146 'C','r','y','p','t','S','I','P','D','l','l', 0 };
147 static const WCHAR szBackSlash[] = { '\\', 0 };
148 static const WCHAR szDllName[] = { 'D','l','l',0 };
149 static const WCHAR szFuncName[] = { 'F','u','n','c','N','a','m','e',0 };
150 WCHAR szFullKey[ 0x100 ];
151 LONG r;
152 HKEY hKey;
153
154 if( !szFunction )
155 return ERROR_SUCCESS;
156
157 /* max length of szFullKey depends on our code only, so we won't overrun */
158 lstrcpyW( szFullKey, szOID );
159 lstrcatW( szFullKey, szKey );
160 lstrcatW( szFullKey, szBackSlash );
161 CRYPT_guid2wstr( guid, &szFullKey[ lstrlenW( szFullKey ) ] );
162 lstrcatW( szFullKey, szBackSlash );
163
164 TRACE("key is %s\n", debugstr_w( szFullKey ) );
165
166 r = RegCreateKeyW( HKEY_LOCAL_MACHINE, szFullKey, &hKey );
167 if( r != ERROR_SUCCESS )
168 return r;
169
170 /* write the values */
171 RegSetValueExW( hKey, szFuncName, 0, REG_SZ, (const BYTE*) szFunction,
172 ( lstrlenW( szFunction ) + 1 ) * sizeof (WCHAR) );
173 RegSetValueExW( hKey, szDllName, 0, REG_SZ, (const BYTE*) szDll,
174 ( lstrlenW( szDll ) + 1) * sizeof (WCHAR) );
175
176 RegCloseKey( hKey );
177
178 return ERROR_SUCCESS;
179 }
180
181 BOOL WINAPI CryptSIPAddProvider(SIP_ADD_NEWPROVIDER *psNewProv)
182 {
183 static const WCHAR szCreate[] = {
184 'C','r','e','a','t','e',
185 'I','n','d','i','r','e','c','t','D','a','t','a',0};
186 static const WCHAR szGetSigned[] = {
187 'G','e','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
188 static const WCHAR szIsMyFile[] = {
189 'I','s','M','y','F','i','l','e','T','y','p','e', 0 };
190 static const WCHAR szPutSigned[] = {
191 'P','u','t','S','i','g','n','e','d','D','a','t','a','M','s','g',0};
192 static const WCHAR szRemoveSigned[] = {
193 'R','e','m','o','v','e',
194 'S','i','g','n','e','d','D','a','t','a','M','s','g',0};
195 static const WCHAR szVerify[] = {
196 'V','e','r','i','f','y',
197 'I','n','d','i','r','e','c','t','D','a','t','a',0};
198
199 TRACE("%p\n", psNewProv);
200
201 if( !psNewProv )
202 return FALSE;
203
204 TRACE("%s %s %s %s\n",
205 debugstr_guid( psNewProv->pgSubject ),
206 debugstr_w( psNewProv->pwszDLLFileName ),
207 debugstr_w( psNewProv->pwszMagicNumber ),
208 debugstr_w( psNewProv->pwszIsFunctionName ) );
209
210 #define CRYPT_SIPADDPROV( key, field ) \
211 CRYPT_SIPWriteFunction( psNewProv->pgSubject, key, \
212 psNewProv->pwszDLLFileName, psNewProv->field)
213
214 CRYPT_SIPADDPROV( szGetSigned, pwszGetFuncName );
215 CRYPT_SIPADDPROV( szPutSigned, pwszPutFuncName );
216 CRYPT_SIPADDPROV( szCreate, pwszCreateFuncName );
217 CRYPT_SIPADDPROV( szVerify, pwszVerifyFuncName );
218 CRYPT_SIPADDPROV( szRemoveSigned, pwszRemoveFuncName );
219 CRYPT_SIPADDPROV( szIsMyFile, pwszIsFunctionNameFmt2 );
220
221 #undef CRYPT_SIPADDPROV
222
223 return TRUE;
224 }
225
226 BOOL WINAPI CryptSIPRetrieveSubjectGuid
227 (LPCWSTR FileName, HANDLE hFileIn, GUID *pgSubject)
228 {
229 FIXME("stub!\n");
230 return FALSE;
231 }
232
233 BOOL WINAPI CryptSIPLoad
234 (const GUID *pgSubject, DWORD dwFlags, SIP_DISPATCH_INFO *pSipDispatch)
235 {
236 FIXME("stub!\n");
237 return FALSE;
238 }
239
240 BOOL WINAPI CryptRegisterDefaultOIDFunction(DWORD dwEncodingType,
241 LPCSTR pszFuncName, DWORD dwIndex,
242 LPCWSTR pwszDll)
243 {
244 FIXME("(%lx,%s,%lx,%s) stub!\n", dwEncodingType, pszFuncName, dwIndex,
245 debugstr_w(pwszDll));
246 return FALSE;
247 }
248
249 LPCSTR WINAPI CertAlgIdToOID(DWORD dwAlgId)
250 {
251 switch (dwAlgId)
252 {
253 case CALG_RSA_KEYX:
254 return szOID_RSA_RSA;
255 case CALG_DH_EPHEM:
256 return szOID_RSA_SMIMEalgESDH;
257 case CALG_MD2:
258 return szOID_RSA_MD2;
259 case CALG_MD4:
260 return szOID_RSA_MD4;
261 case CALG_MD5:
262 return szOID_RSA_MD5;
263 case CALG_RC2:
264 return szOID_RSA_RC2CBC;
265 case CALG_RC4:
266 return szOID_RSA_RC4;
267 case CALG_3DES:
268 return szOID_RSA_DES_EDE3_CBC;
269 case CALG_DH_SF:
270 return szOID_ANSI_X942_DH;
271 case CALG_DSS_SIGN:
272 return szOID_X957_DSA;
273 case CALG_DES:
274 return szOID_OIWSEC_desCBC;
275 case CALG_SHA:
276 return szOID_OIWSEC_sha1;
277 default:
278 return NULL;
279 }
280 }
281
282 DWORD WINAPI CertOIDToAlgId(LPCSTR pszObjId)
283 {
284 int i;
285
286 if (pszObjId)
287 {
288 for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++)
289 {
290 if (!strcmp(pszObjId, oidToAlgID[i].oid))
291 return oidToAlgID[i].algID;
292 }
293 }
294 return 0;
295 }
296
297 LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
298 {
299 return HeapAlloc(GetProcessHeap(), 0, cbSize);
300 }
301
302 LPVOID WINAPI CryptMemRealloc(LPVOID pv, ULONG cbSize)
303 {
304 return HeapReAlloc(GetProcessHeap(), 0, pv, cbSize);
305 }
306
307 VOID WINAPI CryptMemFree(LPVOID pv)
308 {
309 HeapFree(GetProcessHeap(), 0, pv);
310 }
311
312 DWORD WINAPI I_CryptAllocTls(void)
313 {
314 return TlsAlloc();
315 }
316
317 LPVOID WINAPI I_CryptDetachTls(DWORD dwTlsIndex)
318 {
319 LPVOID ret;
320
321 ret = TlsGetValue(dwTlsIndex);
322
323 TlsSetValue(dwTlsIndex, NULL);
324 return ret;
325 }
326
327 LPVOID WINAPI I_CryptGetTls(DWORD dwTlsIndex)
328 {
329 return TlsGetValue(dwTlsIndex);
330 }
331
332 BOOL WINAPI I_CryptSetTls(DWORD dwTlsIndex, LPVOID lpTlsValue)
333 {
334 return TlsSetValue(dwTlsIndex, lpTlsValue);
335 }
336
337 BOOL WINAPI I_CryptFreeTls(DWORD dwTlsIndex, DWORD unknown)
338 {
339 TRACE("(%ld, %ld)\n", dwTlsIndex, unknown);
340 return TlsFree(dwTlsIndex);
341 }