[SHELL32] CDrivesFolder: Implement the eject and disconnect menu items. CORE-13841
[reactos.git] / dll / win32 / crypt32 / regstore.c
1 /*
2 * Copyright 2004-2007 Juan Lang
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "crypt32_private.h"
20
21 #include <shlwapi.h>
22
23 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
24
25 typedef struct _WINE_HASH_TO_DELETE
26 {
27 BYTE hash[20];
28 struct list entry;
29 } WINE_HASH_TO_DELETE;
30
31 typedef struct _WINE_REGSTOREINFO
32 {
33 DWORD dwOpenFlags;
34 HCERTSTORE memStore;
35 HKEY key;
36 BOOL dirty;
37 CRITICAL_SECTION cs;
38 struct list certsToDelete;
39 struct list crlsToDelete;
40 struct list ctlsToDelete;
41 } WINE_REGSTOREINFO;
42
43 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
44 {
45 static const WCHAR fmt[] = { '%','0','2','X',0 };
46 DWORD i;
47
48 assert(hash);
49 assert(asciiHash);
50
51 for (i = 0; i < 20; i++)
52 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
53 }
54
55 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
56 0 };
57 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
58 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
59 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
60
61 static void CRYPT_RegReadSerializedFromReg(HKEY key, DWORD contextType,
62 HCERTSTORE store)
63 {
64 LONG rc;
65 DWORD index = 0;
66 WCHAR subKeyName[MAX_PATH];
67
68 do {
69 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
70
71 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
72 NULL);
73 if (!rc)
74 {
75 HKEY subKey;
76
77 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
78 if (!rc)
79 {
80 LPBYTE buf = NULL;
81
82 size = 0;
83 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
84 if (!rc)
85 buf = CryptMemAlloc(size);
86 if (buf)
87 {
88 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
89 &size);
90 if (!rc)
91 {
92 const void *context;
93 DWORD addedType;
94
95 TRACE("Adding cert with hash %s\n",
96 debugstr_w(subKeyName));
97 context = CRYPT_ReadSerializedElement(buf, size,
98 contextType, &addedType);
99 if (context)
100 {
101 const WINE_CONTEXT_INTERFACE *contextInterface;
102 BYTE hash[20];
103
104 switch (addedType)
105 {
106 case CERT_STORE_CERTIFICATE_CONTEXT:
107 contextInterface = pCertInterface;
108 break;
109 case CERT_STORE_CRL_CONTEXT:
110 contextInterface = pCRLInterface;
111 break;
112 case CERT_STORE_CTL_CONTEXT:
113 contextInterface = pCTLInterface;
114 break;
115 default:
116 contextInterface = NULL;
117 }
118 if (contextInterface)
119 {
120 size = sizeof(hash);
121 if (contextInterface->getProp(context,
122 CERT_HASH_PROP_ID, hash, &size))
123 {
124 WCHAR asciiHash[20 * 2 + 1];
125
126 CRYPT_HashToStr(hash, asciiHash);
127 TRACE("comparing %s\n",
128 debugstr_w(asciiHash));
129 TRACE("with %s\n", debugstr_w(subKeyName));
130 if (!lstrcmpW(asciiHash, subKeyName))
131 {
132 TRACE("hash matches, adding\n");
133 contextInterface->addContextToStore(
134 store, context,
135 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
136 }
137 else
138 TRACE("hash doesn't match, ignoring\n");
139 }
140 Context_Release(context_from_ptr(context));
141 }
142 }
143 }
144 CryptMemFree(buf);
145 }
146 RegCloseKey(subKey);
147 }
148 /* Ignore intermediate errors, continue enumerating */
149 rc = ERROR_SUCCESS;
150 }
151 } while (!rc);
152 }
153
154 static void CRYPT_RegReadFromReg(HKEY key, HCERTSTORE store)
155 {
156 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
157 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
158 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
159 DWORD i;
160
161 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
162 {
163 HKEY hKey;
164 LONG rc;
165
166 rc = RegCreateKeyExW(key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
167 &hKey, NULL);
168 if (!rc)
169 {
170 CRYPT_RegReadSerializedFromReg(hKey, contextFlags[i], store);
171 RegCloseKey(hKey);
172 }
173 }
174 }
175
176 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
177 static BOOL CRYPT_WriteSerializedToReg(HKEY key, DWORD flags, const BYTE *hash, const BYTE *buf,
178 DWORD len)
179 {
180 WCHAR asciiHash[20 * 2 + 1];
181 LONG rc;
182 HKEY subKey;
183 BOOL ret;
184
185 CRYPT_HashToStr(hash, asciiHash);
186 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, flags, KEY_ALL_ACCESS, NULL,
187 &subKey, NULL);
188 if (!rc)
189 {
190 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
191 RegCloseKey(subKey);
192 }
193 if (!rc)
194 ret = TRUE;
195 else
196 {
197 SetLastError(rc);
198 ret = FALSE;
199 }
200 return ret;
201 }
202
203 BOOL CRYPT_SerializeContextsToReg(HKEY key, DWORD flags,
204 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
205 {
206 const void *context = NULL;
207 BOOL ret;
208
209 do {
210 context = contextInterface->enumContextsInStore(memStore, context);
211 if (context)
212 {
213 BYTE hash[20];
214 DWORD hashSize = sizeof(hash);
215
216 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
217 &hashSize);
218 if (ret)
219 {
220 DWORD size = 0;
221 LPBYTE buf = NULL;
222
223 ret = contextInterface->serialize(context, 0, NULL, &size);
224 if (size)
225 buf = CryptMemAlloc(size);
226 if (buf)
227 {
228 ret = contextInterface->serialize(context, 0, buf, &size);
229 if (ret)
230 ret = CRYPT_WriteSerializedToReg(key, flags, hash, buf, size);
231 }
232 CryptMemFree(buf);
233 }
234 }
235 else
236 ret = TRUE;
237 } while (ret && context != NULL);
238 if (context)
239 Context_Release(context_from_ptr(context));
240 return ret;
241 }
242
243 static BOOL CRYPT_RegWriteToReg(WINE_REGSTOREINFO *store)
244 {
245 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
246 const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
247 pCRLInterface, pCTLInterface };
248 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
249 &store->ctlsToDelete };
250 BOOL ret = TRUE;
251 DWORD i;
252
253 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
254 {
255 HKEY key;
256 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
257 KEY_ALL_ACCESS, NULL, &key, NULL);
258
259 if (!rc)
260 {
261 if (listToDelete[i])
262 {
263 WINE_HASH_TO_DELETE *toDelete, *next;
264 WCHAR asciiHash[20 * 2 + 1];
265
266 EnterCriticalSection(&store->cs);
267 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
268 WINE_HASH_TO_DELETE, entry)
269 {
270 LONG rc;
271
272 CRYPT_HashToStr(toDelete->hash, asciiHash);
273 TRACE("Removing %s\n", debugstr_w(asciiHash));
274 rc = RegDeleteKeyW(key, asciiHash);
275 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
276 {
277 SetLastError(rc);
278 ret = FALSE;
279 }
280 list_remove(&toDelete->entry);
281 CryptMemFree(toDelete);
282 }
283 LeaveCriticalSection(&store->cs);
284 }
285 ret = CRYPT_SerializeContextsToReg(key, 0, interfaces[i], store->memStore);
286 RegCloseKey(key);
287 }
288 else
289 {
290 SetLastError(rc);
291 ret = FALSE;
292 }
293 }
294 return ret;
295 }
296
297 /* If force is true or the registry store is dirty, writes the contents of the
298 * store to the registry.
299 */
300 static BOOL CRYPT_RegFlushStore(WINE_REGSTOREINFO *store, BOOL force)
301 {
302 BOOL ret;
303
304 TRACE("(%p, %d)\n", store, force);
305
306 if (store->dirty || force)
307 ret = CRYPT_RegWriteToReg(store);
308 else
309 ret = TRUE;
310 return ret;
311 }
312
313 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
314 {
315 WINE_REGSTOREINFO *store = hCertStore;
316
317 TRACE("(%p, %08x)\n", store, dwFlags);
318 if (dwFlags)
319 FIXME("Unimplemented flags: %08x\n", dwFlags);
320
321 CRYPT_RegFlushStore(store, FALSE);
322 RegCloseKey(store->key);
323 store->cs.DebugInfo->Spare[0] = 0;
324 DeleteCriticalSection(&store->cs);
325 CryptMemFree(store);
326 }
327
328 static BOOL CRYPT_RegWriteContext(WINE_REGSTOREINFO *store,
329 const void *context, DWORD dwFlags)
330 {
331 BOOL ret;
332
333 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
334 {
335 store->dirty = TRUE;
336 ret = TRUE;
337 }
338 else
339 ret = FALSE;
340 return ret;
341 }
342
343 static BOOL CRYPT_RegDeleteContext(WINE_REGSTOREINFO *store,
344 struct list *deleteList, const void *context,
345 const WINE_CONTEXT_INTERFACE *contextInterface)
346 {
347 BOOL ret;
348
349 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
350 {
351 SetLastError(ERROR_ACCESS_DENIED);
352 ret = FALSE;
353 }
354 else
355 {
356 WINE_HASH_TO_DELETE *toDelete = CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
357
358 if (toDelete)
359 {
360 DWORD size = sizeof(toDelete->hash);
361
362 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
363 toDelete->hash, &size);
364 if (ret)
365 {
366 EnterCriticalSection(&store->cs);
367 list_add_tail(deleteList, &toDelete->entry);
368 LeaveCriticalSection(&store->cs);
369 }
370 else
371 {
372 CryptMemFree(toDelete);
373 ret = FALSE;
374 }
375 }
376 else
377 ret = FALSE;
378 if (ret)
379 store->dirty = TRUE;
380 }
381 return ret;
382 }
383
384 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
385 PCCERT_CONTEXT cert, DWORD dwFlags)
386 {
387 WINE_REGSTOREINFO *store = hCertStore;
388
389 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
390
391 return CRYPT_RegWriteContext(store, cert, dwFlags);
392 }
393
394 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
395 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
396 {
397 WINE_REGSTOREINFO *store = hCertStore;
398
399 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
400
401 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
402 pCertInterface);
403 }
404
405 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
406 PCCRL_CONTEXT crl, DWORD dwFlags)
407 {
408 WINE_REGSTOREINFO *store = hCertStore;
409
410 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
411
412 return CRYPT_RegWriteContext(store, crl, dwFlags);
413 }
414
415 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
416 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
417 {
418 WINE_REGSTOREINFO *store = hCertStore;
419
420 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
421
422 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
423 pCRLInterface);
424 }
425
426 static BOOL WINAPI CRYPT_RegWriteCTL(HCERTSTORE hCertStore,
427 PCCTL_CONTEXT ctl, DWORD dwFlags)
428 {
429 WINE_REGSTOREINFO *store = hCertStore;
430
431 TRACE("(%p, %p, %d)\n", hCertStore, ctl, dwFlags);
432
433 return CRYPT_RegWriteContext(store, ctl, dwFlags);
434 }
435
436 static BOOL WINAPI CRYPT_RegDeleteCTL(HCERTSTORE hCertStore,
437 PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
438 {
439 WINE_REGSTOREINFO *store = hCertStore;
440
441 TRACE("(%p, %p, %08x)\n", store, pCtlContext, dwFlags);
442
443 return CRYPT_RegDeleteContext(store, &store->ctlsToDelete, pCtlContext,
444 pCTLInterface);
445 }
446
447 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
448 DWORD dwCtrlType, void const *pvCtrlPara)
449 {
450 WINE_REGSTOREINFO *store = hCertStore;
451 BOOL ret;
452
453 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
454 pvCtrlPara);
455
456 switch (dwCtrlType)
457 {
458 case CERT_STORE_CTRL_RESYNC:
459 {
460 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
461 CERT_STORE_CREATE_NEW_FLAG, NULL);
462
463 CRYPT_RegFlushStore(store, FALSE);
464 CRYPT_RegReadFromReg(store->key, memStore);
465 I_CertUpdateStore(store->memStore, memStore, 0, 0);
466 CertCloseStore(memStore, 0);
467 ret = TRUE;
468 break;
469 }
470 case CERT_STORE_CTRL_COMMIT:
471 ret = CRYPT_RegFlushStore(store,
472 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
473 break;
474 case CERT_STORE_CTRL_AUTO_RESYNC:
475 FIXME("CERT_STORE_CTRL_AUTO_RESYNC: stub\n");
476 ret = TRUE;
477 break;
478 default:
479 FIXME("%d: stub\n", dwCtrlType);
480 ret = FALSE;
481 }
482 return ret;
483 }
484
485 static void *regProvFuncs[] = {
486 CRYPT_RegCloseStore,
487 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
488 CRYPT_RegWriteCert,
489 CRYPT_RegDeleteCert,
490 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
491 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
492 CRYPT_RegWriteCRL,
493 CRYPT_RegDeleteCRL,
494 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
495 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
496 CRYPT_RegWriteCTL,
497 CRYPT_RegDeleteCTL,
498 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
499 CRYPT_RegControl,
500 };
501
502 WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
503 const void *pvPara)
504 {
505 WINECRYPT_CERTSTORE *store = NULL;
506
507 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
508
509 if (dwFlags & CERT_STORE_DELETE_FLAG)
510 {
511 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
512
513 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
514 rc = RegDeleteTreeW((HKEY)pvPara, CRLsW);
515 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
516 rc = RegDeleteTreeW((HKEY)pvPara, CTLsW);
517 if (rc == ERROR_NO_MORE_ITEMS)
518 rc = ERROR_SUCCESS;
519 SetLastError(rc);
520 }
521 else
522 {
523 HKEY key;
524
525 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
526 GetCurrentProcess(), (LPHANDLE)&key,
527 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
528 TRUE, 0))
529 {
530 WINECRYPT_CERTSTORE *memStore;
531
532 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, hCryptProv,
533 CERT_STORE_CREATE_NEW_FLAG, NULL);
534 if (memStore)
535 {
536 WINE_REGSTOREINFO *regInfo = CryptMemAlloc(
537 sizeof(WINE_REGSTOREINFO));
538
539 if (regInfo)
540 {
541 CERT_STORE_PROV_INFO provInfo = { 0 };
542
543 regInfo->dwOpenFlags = dwFlags;
544 regInfo->memStore = memStore;
545 regInfo->key = key;
546 InitializeCriticalSection(&regInfo->cs);
547 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
548 list_init(&regInfo->certsToDelete);
549 list_init(&regInfo->crlsToDelete);
550 list_init(&regInfo->ctlsToDelete);
551 CRYPT_RegReadFromReg(regInfo->key, regInfo->memStore);
552 regInfo->dirty = FALSE;
553 provInfo.cbSize = sizeof(provInfo);
554 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
555 sizeof(regProvFuncs[0]);
556 provInfo.rgpvStoreProvFunc = regProvFuncs;
557 provInfo.hStoreProv = regInfo;
558 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
559 /* Reg store doesn't need crypto provider, so close it */
560 if (hCryptProv &&
561 !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
562 CryptReleaseContext(hCryptProv, 0);
563 }
564 }
565 }
566 }
567 TRACE("returning %p\n", store);
568 return store;
569 }