msxml3/tests: Tests for IMXAttributes::clear().
[wine/multimedia.git] / dlls / crypt32 / regstore.c
blobf4b4295835ac9f9ae57b6775852fbb1e5a270f60
1 /*
2 * Copyright 2004-2007 Juan Lang
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.
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.
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
18 #include <assert.h>
19 #include <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "wincrypt.h"
23 #include "winreg.h"
24 #include "winuser.h"
25 #include "wine/debug.h"
26 #include "wine/list.h"
27 #include "crypt32_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
31 typedef struct _WINE_HASH_TO_DELETE
33 BYTE hash[20];
34 struct list entry;
35 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
37 typedef struct _WINE_REGSTOREINFO
39 DWORD dwOpenFlags;
40 HCERTSTORE memStore;
41 HKEY key;
42 BOOL dirty;
43 CRITICAL_SECTION cs;
44 struct list certsToDelete;
45 struct list crlsToDelete;
46 struct list ctlsToDelete;
47 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
49 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
51 static const WCHAR fmt[] = { '%','0','2','X',0 };
52 DWORD i;
54 assert(hash);
55 assert(asciiHash);
57 for (i = 0; i < 20; i++)
58 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
61 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
62 0 };
63 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
64 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
65 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
67 static void CRYPT_RegReadSerializedFromReg(HKEY key, DWORD contextType,
68 HCERTSTORE store)
70 LONG rc;
71 DWORD index = 0;
72 WCHAR subKeyName[MAX_PATH];
74 do {
75 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
77 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
78 NULL);
79 if (!rc)
81 HKEY subKey;
83 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
84 if (!rc)
86 LPBYTE buf = NULL;
88 size = 0;
89 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
90 if (!rc)
91 buf = CryptMemAlloc(size);
92 if (buf)
94 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
95 &size);
96 if (!rc)
98 const void *context;
99 DWORD addedType;
101 TRACE("Adding cert with hash %s\n",
102 debugstr_w(subKeyName));
103 context = CRYPT_ReadSerializedElement(buf, size,
104 contextType, &addedType);
105 if (context)
107 const WINE_CONTEXT_INTERFACE *contextInterface;
108 BYTE hash[20];
110 switch (addedType)
112 case CERT_STORE_CERTIFICATE_CONTEXT:
113 contextInterface = pCertInterface;
114 break;
115 case CERT_STORE_CRL_CONTEXT:
116 contextInterface = pCRLInterface;
117 break;
118 case CERT_STORE_CTL_CONTEXT:
119 contextInterface = pCTLInterface;
120 break;
121 default:
122 contextInterface = NULL;
124 if (contextInterface)
126 size = sizeof(hash);
127 if (contextInterface->getProp(context,
128 CERT_HASH_PROP_ID, hash, &size))
130 WCHAR asciiHash[20 * 2 + 1];
132 CRYPT_HashToStr(hash, asciiHash);
133 TRACE("comparing %s\n",
134 debugstr_w(asciiHash));
135 TRACE("with %s\n", debugstr_w(subKeyName));
136 if (!lstrcmpW(asciiHash, subKeyName))
138 TRACE("hash matches, adding\n");
139 contextInterface->addContextToStore(
140 store, context,
141 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
143 else
144 TRACE("hash doesn't match, ignoring\n");
146 contextInterface->free(context);
150 CryptMemFree(buf);
152 RegCloseKey(subKey);
154 /* Ignore intermediate errors, continue enumerating */
155 rc = ERROR_SUCCESS;
157 } while (!rc);
160 static void CRYPT_RegReadFromReg(HKEY key, HCERTSTORE store)
162 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
163 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
164 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
165 DWORD i;
167 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
169 HKEY hKey;
170 LONG rc;
172 rc = RegCreateKeyExW(key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
173 &hKey, NULL);
174 if (!rc)
176 CRYPT_RegReadSerializedFromReg(hKey, contextFlags[i], store);
177 RegCloseKey(hKey);
182 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
183 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
184 DWORD len)
186 WCHAR asciiHash[20 * 2 + 1];
187 LONG rc;
188 HKEY subKey;
189 BOOL ret;
191 CRYPT_HashToStr(hash, asciiHash);
192 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
193 &subKey, NULL);
194 if (!rc)
196 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
197 RegCloseKey(subKey);
199 if (!rc)
200 ret = TRUE;
201 else
203 SetLastError(rc);
204 ret = FALSE;
206 return ret;
209 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
210 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
212 const void *context = NULL;
213 BOOL ret;
215 do {
216 context = contextInterface->enumContextsInStore(memStore, context);
217 if (context)
219 BYTE hash[20];
220 DWORD hashSize = sizeof(hash);
222 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
223 &hashSize);
224 if (ret)
226 DWORD size = 0;
227 LPBYTE buf = NULL;
229 ret = contextInterface->serialize(context, 0, NULL, &size);
230 if (size)
231 buf = CryptMemAlloc(size);
232 if (buf)
234 ret = contextInterface->serialize(context, 0, buf, &size);
235 if (ret)
236 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
238 CryptMemFree(buf);
241 else
242 ret = TRUE;
243 } while (ret && context != NULL);
244 if (context)
245 contextInterface->free(context);
246 return ret;
249 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
251 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
252 const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
253 pCRLInterface, pCTLInterface };
254 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
255 &store->ctlsToDelete };
256 BOOL ret = TRUE;
257 DWORD i;
259 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
261 HKEY key;
262 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
263 KEY_ALL_ACCESS, NULL, &key, NULL);
265 if (!rc)
267 if (listToDelete[i])
269 PWINE_HASH_TO_DELETE toDelete, next;
270 WCHAR asciiHash[20 * 2 + 1];
272 EnterCriticalSection(&store->cs);
273 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
274 WINE_HASH_TO_DELETE, entry)
276 LONG rc;
278 CRYPT_HashToStr(toDelete->hash, asciiHash);
279 TRACE("Removing %s\n", debugstr_w(asciiHash));
280 rc = RegDeleteKeyW(key, asciiHash);
281 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
283 SetLastError(rc);
284 ret = FALSE;
286 list_remove(&toDelete->entry);
287 CryptMemFree(toDelete);
289 LeaveCriticalSection(&store->cs);
291 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
292 store->memStore);
293 RegCloseKey(key);
295 else
297 SetLastError(rc);
298 ret = FALSE;
301 return ret;
304 /* If force is true or the registry store is dirty, writes the contents of the
305 * store to the registry.
307 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
309 BOOL ret;
311 TRACE("(%p, %d)\n", store, force);
313 if (store->dirty || force)
314 ret = CRYPT_RegWriteToReg(store);
315 else
316 ret = TRUE;
317 return ret;
320 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
322 PWINE_REGSTOREINFO store = hCertStore;
324 TRACE("(%p, %08x)\n", store, dwFlags);
325 if (dwFlags)
326 FIXME("Unimplemented flags: %08x\n", dwFlags);
328 CRYPT_RegFlushStore(store, FALSE);
329 RegCloseKey(store->key);
330 store->cs.DebugInfo->Spare[0] = 0;
331 DeleteCriticalSection(&store->cs);
332 CryptMemFree(store);
335 static BOOL CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
336 const void *context, DWORD dwFlags)
338 BOOL ret;
340 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
342 store->dirty = TRUE;
343 ret = TRUE;
345 else
346 ret = FALSE;
347 return ret;
350 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
351 struct list *deleteList, const void *context,
352 PCWINE_CONTEXT_INTERFACE contextInterface)
354 BOOL ret;
356 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
358 SetLastError(ERROR_ACCESS_DENIED);
359 ret = FALSE;
361 else
363 PWINE_HASH_TO_DELETE toDelete =
364 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
366 if (toDelete)
368 DWORD size = sizeof(toDelete->hash);
370 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
371 toDelete->hash, &size);
372 if (ret)
374 EnterCriticalSection(&store->cs);
375 list_add_tail(deleteList, &toDelete->entry);
376 LeaveCriticalSection(&store->cs);
378 else
380 CryptMemFree(toDelete);
381 ret = FALSE;
384 else
385 ret = FALSE;
386 if (ret)
387 store->dirty = TRUE;
389 return ret;
392 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
393 PCCERT_CONTEXT cert, DWORD dwFlags)
395 PWINE_REGSTOREINFO store = hCertStore;
397 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
399 return CRYPT_RegWriteContext(store, cert, dwFlags);
402 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
403 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
405 PWINE_REGSTOREINFO store = hCertStore;
407 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
409 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
410 pCertInterface);
413 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
414 PCCRL_CONTEXT crl, DWORD dwFlags)
416 PWINE_REGSTOREINFO store = hCertStore;
418 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
420 return CRYPT_RegWriteContext(store, crl, dwFlags);
423 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
424 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
426 PWINE_REGSTOREINFO store = hCertStore;
428 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
430 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
431 pCRLInterface);
434 static BOOL WINAPI CRYPT_RegWriteCTL(HCERTSTORE hCertStore,
435 PCCTL_CONTEXT ctl, DWORD dwFlags)
437 PWINE_REGSTOREINFO store = hCertStore;
439 TRACE("(%p, %p, %d)\n", hCertStore, ctl, dwFlags);
441 return CRYPT_RegWriteContext(store, ctl, dwFlags);
444 static BOOL WINAPI CRYPT_RegDeleteCTL(HCERTSTORE hCertStore,
445 PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
447 PWINE_REGSTOREINFO store = hCertStore;
449 TRACE("(%p, %p, %08x)\n", store, pCtlContext, dwFlags);
451 return CRYPT_RegDeleteContext(store, &store->ctlsToDelete, pCtlContext,
452 pCTLInterface);
455 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
456 DWORD dwCtrlType, void const *pvCtrlPara)
458 PWINE_REGSTOREINFO store = hCertStore;
459 BOOL ret;
461 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
462 pvCtrlPara);
464 switch (dwCtrlType)
466 case CERT_STORE_CTRL_RESYNC:
468 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
469 CERT_STORE_CREATE_NEW_FLAG, NULL);
471 CRYPT_RegFlushStore(store, FALSE);
472 CRYPT_RegReadFromReg(store->key, memStore);
473 I_CertUpdateStore(store->memStore, memStore, 0, 0);
474 CertCloseStore(memStore, 0);
475 ret = TRUE;
476 break;
478 case CERT_STORE_CTRL_COMMIT:
479 ret = CRYPT_RegFlushStore(store,
480 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
481 break;
482 case CERT_STORE_CTRL_AUTO_RESYNC:
483 FIXME("CERT_STORE_CTRL_AUTO_RESYNC: stub\n");
484 ret = TRUE;
485 break;
486 default:
487 FIXME("%d: stub\n", dwCtrlType);
488 ret = FALSE;
490 return ret;
493 static void *regProvFuncs[] = {
494 CRYPT_RegCloseStore,
495 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
496 CRYPT_RegWriteCert,
497 CRYPT_RegDeleteCert,
498 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
499 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
500 CRYPT_RegWriteCRL,
501 CRYPT_RegDeleteCRL,
502 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
503 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
504 CRYPT_RegWriteCTL,
505 CRYPT_RegDeleteCTL,
506 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
507 CRYPT_RegControl,
510 PWINECRYPT_CERTSTORE CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
511 const void *pvPara)
513 PWINECRYPT_CERTSTORE store = NULL;
515 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
517 if (dwFlags & CERT_STORE_DELETE_FLAG)
519 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
521 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
522 rc = RegDeleteTreeW((HKEY)pvPara, CRLsW);
523 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
524 rc = RegDeleteTreeW((HKEY)pvPara, CTLsW);
525 if (rc == ERROR_NO_MORE_ITEMS)
526 rc = ERROR_SUCCESS;
527 SetLastError(rc);
529 else
531 HKEY key;
533 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
534 GetCurrentProcess(), (LPHANDLE)&key,
535 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
536 TRUE, 0))
538 PWINECRYPT_CERTSTORE memStore;
540 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, hCryptProv,
541 CERT_STORE_CREATE_NEW_FLAG, NULL);
542 if (memStore)
544 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
545 sizeof(WINE_REGSTOREINFO));
547 if (regInfo)
549 CERT_STORE_PROV_INFO provInfo = { 0 };
551 regInfo->dwOpenFlags = dwFlags;
552 regInfo->memStore = memStore;
553 regInfo->key = key;
554 InitializeCriticalSection(&regInfo->cs);
555 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
556 list_init(&regInfo->certsToDelete);
557 list_init(&regInfo->crlsToDelete);
558 list_init(&regInfo->ctlsToDelete);
559 CRYPT_RegReadFromReg(regInfo->key, regInfo->memStore);
560 regInfo->dirty = FALSE;
561 provInfo.cbSize = sizeof(provInfo);
562 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
563 sizeof(regProvFuncs[0]);
564 provInfo.rgpvStoreProvFunc = regProvFuncs;
565 provInfo.hStoreProv = regInfo;
566 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
567 /* Reg store doesn't need crypto provider, so close it */
568 if (hCryptProv &&
569 !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
570 CryptReleaseContext(hCryptProv, 0);
575 TRACE("returning %p\n", store);
576 return store;