wrc: Store version and characteristics as simple integers.
[wine.git] / dlls / crypt32 / regstore.c
blob3899c805531da23cb5899a04b885c85fec4bf6fe
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 "crypt32_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
30 typedef struct _WINE_HASH_TO_DELETE
32 BYTE hash[20];
33 struct list entry;
34 } WINE_HASH_TO_DELETE;
36 typedef struct _WINE_REGSTOREINFO
38 DWORD dwOpenFlags;
39 HCERTSTORE memStore;
40 HKEY key;
41 BOOL dirty;
42 CRITICAL_SECTION cs;
43 struct list certsToDelete;
44 struct list crlsToDelete;
45 struct list ctlsToDelete;
46 } WINE_REGSTOREINFO;
48 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
50 DWORD i;
52 assert(hash);
53 assert(asciiHash);
55 for (i = 0; i < 20; i++)
56 wsprintfW(asciiHash + i * 2, L"%02X", hash[i]);
59 static void CRYPT_RegReadSerializedFromReg(HKEY key, DWORD contextType,
60 HCERTSTORE store)
62 LONG rc;
63 DWORD index = 0;
64 WCHAR subKeyName[MAX_PATH];
66 do {
67 DWORD size = ARRAY_SIZE(subKeyName);
69 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
70 NULL);
71 if (!rc)
73 HKEY subKey;
75 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
76 if (!rc)
78 LPBYTE buf = NULL;
80 size = 0;
81 rc = RegQueryValueExW(subKey, L"Blob", NULL, NULL, NULL, &size);
82 if (!rc)
83 buf = CryptMemAlloc(size);
84 if (buf)
86 rc = RegQueryValueExW(subKey, L"Blob", NULL, NULL, buf,
87 &size);
88 if (!rc)
90 const void *context;
91 DWORD addedType;
93 TRACE("Adding cert with hash %s\n",
94 debugstr_w(subKeyName));
95 context = CRYPT_ReadSerializedElement(buf, size,
96 contextType, &addedType);
97 if (context)
99 const WINE_CONTEXT_INTERFACE *contextInterface;
100 BYTE hash[20];
102 switch (addedType)
104 case CERT_STORE_CERTIFICATE_CONTEXT:
105 contextInterface = pCertInterface;
106 break;
107 case CERT_STORE_CRL_CONTEXT:
108 contextInterface = pCRLInterface;
109 break;
110 case CERT_STORE_CTL_CONTEXT:
111 contextInterface = pCTLInterface;
112 break;
113 default:
114 contextInterface = NULL;
116 if (contextInterface)
118 size = sizeof(hash);
119 if (contextInterface->getProp(context,
120 CERT_HASH_PROP_ID, hash, &size))
122 WCHAR asciiHash[20 * 2 + 1];
124 CRYPT_HashToStr(hash, asciiHash);
125 TRACE("comparing %s\n",
126 debugstr_w(asciiHash));
127 TRACE("with %s\n", debugstr_w(subKeyName));
128 if (!wcscmp(asciiHash, subKeyName))
130 TRACE("hash matches, adding\n");
131 contextInterface->addContextToStore(
132 store, context,
133 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
135 else
136 TRACE("hash doesn't match, ignoring\n");
138 Context_Release(context_from_ptr(context));
142 CryptMemFree(buf);
144 RegCloseKey(subKey);
146 /* Ignore intermediate errors, continue enumerating */
147 rc = ERROR_SUCCESS;
149 } while (!rc);
152 static void CRYPT_RegReadFromReg(HKEY key, HCERTSTORE store)
154 static const WCHAR * const subKeys[] = { L"Certificates", L"CRLs", L"CTLs" };
155 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
156 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
157 DWORD i;
159 for (i = 0; i < ARRAY_SIZE(subKeys); i++)
161 HKEY hKey;
162 LONG rc;
164 rc = RegCreateKeyExW(key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
165 &hKey, NULL);
166 if (!rc)
168 CRYPT_RegReadSerializedFromReg(hKey, contextFlags[i], store);
169 RegCloseKey(hKey);
174 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
175 static BOOL CRYPT_WriteSerializedToReg(HKEY key, DWORD flags, const BYTE *hash, const BYTE *buf,
176 DWORD len)
178 WCHAR asciiHash[20 * 2 + 1];
179 LONG rc;
180 HKEY subKey;
181 BOOL ret;
183 CRYPT_HashToStr(hash, asciiHash);
184 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, flags, KEY_ALL_ACCESS, NULL,
185 &subKey, NULL);
186 if (!rc)
188 rc = RegSetValueExW(subKey, L"Blob", 0, REG_BINARY, buf, len);
189 RegCloseKey(subKey);
191 if (!rc)
192 ret = TRUE;
193 else
195 SetLastError(rc);
196 ret = FALSE;
198 return ret;
201 BOOL CRYPT_SerializeContextsToReg(HKEY key, DWORD flags,
202 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
204 const void *context = NULL;
205 BOOL ret;
207 do {
208 context = contextInterface->enumContextsInStore(memStore, context);
209 if (context)
211 BYTE hash[20];
212 DWORD hashSize = sizeof(hash);
214 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
215 &hashSize);
216 if (ret)
218 DWORD size = 0;
219 LPBYTE buf = NULL;
221 ret = contextInterface->serialize(context, 0, NULL, &size);
222 if (size)
223 buf = CryptMemAlloc(size);
224 if (buf)
226 ret = contextInterface->serialize(context, 0, buf, &size);
227 if (ret)
228 ret = CRYPT_WriteSerializedToReg(key, flags, hash, buf, size);
230 CryptMemFree(buf);
233 else
234 ret = TRUE;
235 } while (ret && context != NULL);
236 if (context)
237 Context_Release(context_from_ptr(context));
238 return ret;
241 static BOOL CRYPT_RegWriteToReg(WINE_REGSTOREINFO *store)
243 static const WCHAR * const subKeys[] = { L"Certificates", L"CRLs", L"CTLs" };
244 const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
245 pCRLInterface, pCTLInterface };
246 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
247 &store->ctlsToDelete };
248 BOOL ret = TRUE;
249 DWORD i;
251 for (i = 0; ret && i < ARRAY_SIZE(subKeys); i++)
253 HKEY key;
254 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
255 KEY_ALL_ACCESS, NULL, &key, NULL);
257 if (!rc)
259 if (listToDelete[i])
261 WINE_HASH_TO_DELETE *toDelete, *next;
262 WCHAR asciiHash[20 * 2 + 1];
264 EnterCriticalSection(&store->cs);
265 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
266 WINE_HASH_TO_DELETE, entry)
268 LONG rc;
270 CRYPT_HashToStr(toDelete->hash, asciiHash);
271 TRACE("Removing %s\n", debugstr_w(asciiHash));
272 rc = RegDeleteKeyW(key, asciiHash);
273 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
275 SetLastError(rc);
276 ret = FALSE;
278 list_remove(&toDelete->entry);
279 CryptMemFree(toDelete);
281 LeaveCriticalSection(&store->cs);
283 ret = CRYPT_SerializeContextsToReg(key, 0, interfaces[i], store->memStore);
284 RegCloseKey(key);
286 else
288 SetLastError(rc);
289 ret = FALSE;
292 return ret;
295 /* If force is true or the registry store is dirty, writes the contents of the
296 * store to the registry.
298 static BOOL CRYPT_RegFlushStore(WINE_REGSTOREINFO *store, BOOL force)
300 BOOL ret;
302 TRACE("(%p, %d)\n", store, force);
304 if (store->dirty || force)
306 ret = CRYPT_RegWriteToReg(store);
307 if (ret)
308 store->dirty = FALSE;
310 else
311 ret = TRUE;
312 return ret;
315 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
317 WINE_REGSTOREINFO *store = hCertStore;
319 TRACE("(%p, %08lx)\n", store, dwFlags);
320 if (dwFlags)
321 FIXME("Unimplemented flags: %08lx\n", dwFlags);
323 CRYPT_RegFlushStore(store, FALSE);
324 RegCloseKey(store->key);
325 store->cs.DebugInfo->Spare[0] = 0;
326 DeleteCriticalSection(&store->cs);
327 CryptMemFree(store);
330 static BOOL CRYPT_RegWriteContext(WINE_REGSTOREINFO *store,
331 const void *context, DWORD dwFlags)
333 BOOL ret;
335 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
337 store->dirty = TRUE;
338 ret = TRUE;
340 else
341 ret = FALSE;
342 return ret;
345 static BOOL CRYPT_RegDeleteContext(WINE_REGSTOREINFO *store,
346 struct list *deleteList, const void *context,
347 const WINE_CONTEXT_INTERFACE *contextInterface)
349 BOOL ret;
351 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
353 SetLastError(ERROR_ACCESS_DENIED);
354 ret = FALSE;
356 else
358 WINE_HASH_TO_DELETE *toDelete = CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
360 if (toDelete)
362 DWORD size = sizeof(toDelete->hash);
364 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
365 toDelete->hash, &size);
366 if (ret)
368 EnterCriticalSection(&store->cs);
369 list_add_tail(deleteList, &toDelete->entry);
370 LeaveCriticalSection(&store->cs);
372 else
374 CryptMemFree(toDelete);
375 ret = FALSE;
378 else
379 ret = FALSE;
380 if (ret)
381 store->dirty = TRUE;
383 return ret;
386 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
387 PCCERT_CONTEXT cert, DWORD dwFlags)
389 WINE_REGSTOREINFO *store = hCertStore;
391 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
393 return CRYPT_RegWriteContext(store, cert, dwFlags);
396 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
397 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
399 WINE_REGSTOREINFO *store = hCertStore;
401 TRACE("(%p, %p, %08lx)\n", store, pCertContext, dwFlags);
403 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
404 pCertInterface);
407 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
408 PCCRL_CONTEXT crl, DWORD dwFlags)
410 WINE_REGSTOREINFO *store = hCertStore;
412 TRACE("(%p, %p, %ld)\n", hCertStore, crl, dwFlags);
414 return CRYPT_RegWriteContext(store, crl, dwFlags);
417 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
418 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
420 WINE_REGSTOREINFO *store = hCertStore;
422 TRACE("(%p, %p, %08lx)\n", store, pCrlContext, dwFlags);
424 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
425 pCRLInterface);
428 static BOOL WINAPI CRYPT_RegWriteCTL(HCERTSTORE hCertStore,
429 PCCTL_CONTEXT ctl, DWORD dwFlags)
431 WINE_REGSTOREINFO *store = hCertStore;
433 TRACE("(%p, %p, %ld)\n", hCertStore, ctl, dwFlags);
435 return CRYPT_RegWriteContext(store, ctl, dwFlags);
438 static BOOL WINAPI CRYPT_RegDeleteCTL(HCERTSTORE hCertStore,
439 PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
441 WINE_REGSTOREINFO *store = hCertStore;
443 TRACE("(%p, %p, %08lx)\n", store, pCtlContext, dwFlags);
445 return CRYPT_RegDeleteContext(store, &store->ctlsToDelete, pCtlContext,
446 pCTLInterface);
449 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
450 DWORD dwCtrlType, void const *pvCtrlPara)
452 WINE_REGSTOREINFO *store = hCertStore;
453 BOOL ret = TRUE;
455 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
456 pvCtrlPara);
458 switch (dwCtrlType)
460 case CERT_STORE_CTRL_RESYNC:
462 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
463 CERT_STORE_CREATE_NEW_FLAG, NULL);
465 CRYPT_RegFlushStore(store, FALSE);
466 CRYPT_RegReadFromReg(store->key, memStore);
467 I_CertUpdateStore(store->memStore, memStore, 0, 0);
468 CertCloseStore(memStore, 0);
469 break;
471 case CERT_STORE_CTRL_COMMIT:
472 ret = CRYPT_RegFlushStore(store,
473 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
474 break;
475 case CERT_STORE_CTRL_AUTO_RESYNC:
476 FIXME("CERT_STORE_CTRL_AUTO_RESYNC: stub\n");
477 break;
478 case CERT_STORE_CTRL_NOTIFY_CHANGE:
479 FIXME("CERT_STORE_CTRL_NOTIFY_CHANGE: stub\n");
480 break;
481 default:
482 FIXME("%lu: stub\n", dwCtrlType);
483 ret = FALSE;
485 return ret;
488 static void *regProvFuncs[] = {
489 CRYPT_RegCloseStore,
490 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
491 CRYPT_RegWriteCert,
492 CRYPT_RegDeleteCert,
493 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
494 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
495 CRYPT_RegWriteCRL,
496 CRYPT_RegDeleteCRL,
497 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
498 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
499 CRYPT_RegWriteCTL,
500 CRYPT_RegDeleteCTL,
501 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
502 CRYPT_RegControl,
505 WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
506 const void *pvPara)
508 WINECRYPT_CERTSTORE *store = NULL;
510 TRACE("(%Id, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
512 if (dwFlags & CERT_STORE_DELETE_FLAG)
514 DWORD rc = RegDeleteTreeW((HKEY)pvPara, L"Certificates");
516 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
517 rc = RegDeleteTreeW((HKEY)pvPara, L"CRLs");
518 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
519 rc = RegDeleteTreeW((HKEY)pvPara, L"CTLs");
520 if (rc == ERROR_NO_MORE_ITEMS)
521 rc = ERROR_SUCCESS;
522 SetLastError(rc);
524 else
526 HKEY key;
528 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
529 GetCurrentProcess(), (LPHANDLE)&key,
530 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
531 TRUE, 0))
533 WINECRYPT_CERTSTORE *memStore;
535 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, hCryptProv,
536 CERT_STORE_CREATE_NEW_FLAG, NULL);
537 if (memStore)
539 WINE_REGSTOREINFO *regInfo = CryptMemAlloc(
540 sizeof(WINE_REGSTOREINFO));
542 if (regInfo)
544 CERT_STORE_PROV_INFO provInfo = { 0 };
546 regInfo->dwOpenFlags = dwFlags;
547 regInfo->memStore = memStore;
548 regInfo->key = key;
549 InitializeCriticalSection(&regInfo->cs);
550 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
551 list_init(&regInfo->certsToDelete);
552 list_init(&regInfo->crlsToDelete);
553 list_init(&regInfo->ctlsToDelete);
554 CRYPT_RegReadFromReg(regInfo->key, regInfo->memStore);
555 regInfo->dirty = FALSE;
556 provInfo.cbSize = sizeof(provInfo);
557 provInfo.cStoreProvFunc = ARRAY_SIZE(regProvFuncs);
558 provInfo.rgpvStoreProvFunc = regProvFuncs;
559 provInfo.hStoreProv = regInfo;
560 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
561 /* Reg store doesn't need crypto provider, so close it */
562 if (hCryptProv &&
563 !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
564 CryptReleaseContext(hCryptProv, 0);
569 TRACE("returning %p\n", store);
570 return store;