UTF-8 resource files must explicitly say so with a pragma.
[wine.git] / dlls / crypt32 / regstore.c
blob35928b8960a303dedeabfea9a768e02bd2763528
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 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
48 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
50 static const WCHAR fmt[] = { '%','0','2','X',0 };
51 DWORD i;
53 assert(hash);
54 assert(asciiHash);
56 for (i = 0; i < 20; i++)
57 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
60 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
61 0 };
62 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
63 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
64 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
66 static void CRYPT_RegReadSerializedFromReg(HKEY key, DWORD contextType,
67 HCERTSTORE store)
69 LONG rc;
70 DWORD index = 0;
71 WCHAR subKeyName[MAX_PATH];
73 do {
74 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
76 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
77 NULL);
78 if (!rc)
80 HKEY subKey;
82 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
83 if (!rc)
85 LPBYTE buf = NULL;
87 size = 0;
88 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
89 if (!rc)
90 buf = CryptMemAlloc(size);
91 if (buf)
93 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
94 &size);
95 if (!rc)
97 const void *context;
98 DWORD addedType;
100 TRACE("Adding cert with hash %s\n",
101 debugstr_w(subKeyName));
102 context = CRYPT_ReadSerializedElement(buf, size,
103 contextType, &addedType);
104 if (context)
106 const WINE_CONTEXT_INTERFACE *contextInterface;
107 BYTE hash[20];
109 switch (addedType)
111 case CERT_STORE_CERTIFICATE_CONTEXT:
112 contextInterface = pCertInterface;
113 break;
114 case CERT_STORE_CRL_CONTEXT:
115 contextInterface = pCRLInterface;
116 break;
117 case CERT_STORE_CTL_CONTEXT:
118 contextInterface = pCTLInterface;
119 break;
120 default:
121 contextInterface = NULL;
123 if (contextInterface)
125 size = sizeof(hash);
126 if (contextInterface->getProp(context,
127 CERT_HASH_PROP_ID, hash, &size))
129 WCHAR asciiHash[20 * 2 + 1];
131 CRYPT_HashToStr(hash, asciiHash);
132 TRACE("comparing %s\n",
133 debugstr_w(asciiHash));
134 TRACE("with %s\n", debugstr_w(subKeyName));
135 if (!lstrcmpW(asciiHash, subKeyName))
137 TRACE("hash matches, adding\n");
138 contextInterface->addContextToStore(
139 store, context,
140 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
142 else
143 TRACE("hash doesn't match, ignoring\n");
145 contextInterface->free(context);
149 CryptMemFree(buf);
151 RegCloseKey(subKey);
153 /* Ignore intermediate errors, continue enumerating */
154 rc = ERROR_SUCCESS;
156 } while (!rc);
159 static void CRYPT_RegReadFromReg(HKEY key, HCERTSTORE store)
161 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
162 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
163 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
164 DWORD i;
166 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
168 HKEY hKey;
169 LONG rc;
171 rc = RegCreateKeyExW(key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
172 &hKey, NULL);
173 if (!rc)
175 CRYPT_RegReadSerializedFromReg(hKey, contextFlags[i], store);
176 RegCloseKey(hKey);
181 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
182 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
183 DWORD len)
185 WCHAR asciiHash[20 * 2 + 1];
186 LONG rc;
187 HKEY subKey;
188 BOOL ret;
190 CRYPT_HashToStr(hash, asciiHash);
191 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
192 &subKey, NULL);
193 if (!rc)
195 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
196 RegCloseKey(subKey);
198 if (!rc)
199 ret = TRUE;
200 else
202 SetLastError(rc);
203 ret = FALSE;
205 return ret;
208 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
209 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
211 const void *context = NULL;
212 BOOL ret;
214 do {
215 context = contextInterface->enumContextsInStore(memStore, context);
216 if (context)
218 BYTE hash[20];
219 DWORD hashSize = sizeof(hash);
221 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
222 &hashSize);
223 if (ret)
225 DWORD size = 0;
226 LPBYTE buf = NULL;
228 ret = contextInterface->serialize(context, 0, NULL, &size);
229 if (size)
230 buf = CryptMemAlloc(size);
231 if (buf)
233 ret = contextInterface->serialize(context, 0, buf, &size);
234 if (ret)
235 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
237 CryptMemFree(buf);
240 else
241 ret = TRUE;
242 } while (ret && context != NULL);
243 if (context)
244 contextInterface->free(context);
245 return ret;
248 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
250 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
251 const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
252 pCRLInterface, pCTLInterface };
253 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
254 NULL };
255 BOOL ret = TRUE;
256 DWORD i;
258 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
260 HKEY key;
261 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
262 KEY_ALL_ACCESS, NULL, &key, NULL);
264 if (!rc)
266 if (listToDelete[i])
268 PWINE_HASH_TO_DELETE toDelete, next;
269 WCHAR asciiHash[20 * 2 + 1];
271 EnterCriticalSection(&store->cs);
272 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
273 WINE_HASH_TO_DELETE, entry)
275 LONG rc;
277 CRYPT_HashToStr(toDelete->hash, asciiHash);
278 TRACE("Removing %s\n", debugstr_w(asciiHash));
279 rc = RegDeleteKeyW(key, asciiHash);
280 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
282 SetLastError(rc);
283 ret = FALSE;
285 list_remove(&toDelete->entry);
286 CryptMemFree(toDelete);
288 LeaveCriticalSection(&store->cs);
290 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
291 store->memStore);
292 RegCloseKey(key);
294 else
296 SetLastError(rc);
297 ret = FALSE;
300 return ret;
303 /* If force is true or the registry store is dirty, writes the contents of the
304 * store to the registry.
306 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
308 BOOL ret;
310 TRACE("(%p, %d)\n", store, force);
312 if (store->dirty || force)
313 ret = CRYPT_RegWriteToReg(store);
314 else
315 ret = TRUE;
316 return ret;
319 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
321 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
323 TRACE("(%p, %08x)\n", store, dwFlags);
324 if (dwFlags)
325 FIXME("Unimplemented flags: %08x\n", dwFlags);
327 CRYPT_RegFlushStore(store, FALSE);
328 RegCloseKey(store->key);
329 store->cs.DebugInfo->Spare[0] = 0;
330 DeleteCriticalSection(&store->cs);
331 CryptMemFree(store);
334 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
335 const void *context, DWORD dwFlags)
337 BOOL ret;
339 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
341 store->dirty = TRUE;
342 ret = TRUE;
344 else
345 ret = FALSE;
346 return ret;
349 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
350 struct list *deleteList, const void *context,
351 PCWINE_CONTEXT_INTERFACE contextInterface)
353 BOOL ret;
355 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
357 SetLastError(ERROR_ACCESS_DENIED);
358 ret = FALSE;
360 else
362 PWINE_HASH_TO_DELETE toDelete =
363 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
365 if (toDelete)
367 DWORD size = sizeof(toDelete->hash);
369 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
370 toDelete->hash, &size);
371 if (ret)
373 EnterCriticalSection(&store->cs);
374 list_add_tail(deleteList, &toDelete->entry);
375 LeaveCriticalSection(&store->cs);
377 else
379 CryptMemFree(toDelete);
380 ret = FALSE;
383 else
384 ret = FALSE;
385 if (ret)
386 store->dirty = TRUE;
388 return ret;
391 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
392 PCCERT_CONTEXT cert, DWORD dwFlags)
394 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
396 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
398 return CRYPT_RegWriteContext(store, cert, dwFlags);
401 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
402 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
404 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
406 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
408 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
409 pCertInterface);
412 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
413 PCCRL_CONTEXT crl, DWORD dwFlags)
415 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
417 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
419 return CRYPT_RegWriteContext(store, crl, dwFlags);
422 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
423 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
425 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
427 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
429 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
430 pCRLInterface);
433 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
434 DWORD dwCtrlType, void const *pvCtrlPara)
436 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
437 BOOL ret;
439 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
440 pvCtrlPara);
442 switch (dwCtrlType)
444 case CERT_STORE_CTRL_RESYNC:
446 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
447 CERT_STORE_CREATE_NEW_FLAG, NULL);
449 CRYPT_RegFlushStore(store, FALSE);
450 CRYPT_RegReadFromReg(store->key, memStore);
451 I_CertUpdateStore(store->memStore, memStore, 0, 0);
452 CertCloseStore(memStore, 0);
453 ret = TRUE;
454 break;
456 case CERT_STORE_CTRL_COMMIT:
457 ret = CRYPT_RegFlushStore(store,
458 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
459 break;
460 default:
461 FIXME("%d: stub\n", dwCtrlType);
462 ret = FALSE;
464 return ret;
467 static void *regProvFuncs[] = {
468 CRYPT_RegCloseStore,
469 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
470 CRYPT_RegWriteCert,
471 CRYPT_RegDeleteCert,
472 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
473 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
474 CRYPT_RegWriteCRL,
475 CRYPT_RegDeleteCRL,
476 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
477 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
478 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
479 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
480 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
481 CRYPT_RegControl,
484 PWINECRYPT_CERTSTORE CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
485 const void *pvPara)
487 PWINECRYPT_CERTSTORE store = NULL;
489 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
491 if (dwFlags & CERT_STORE_DELETE_FLAG)
493 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
495 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
496 rc = RegDeleteTreeW((HKEY)pvPara, CRLsW);
497 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
498 rc = RegDeleteTreeW((HKEY)pvPara, CTLsW);
499 if (rc == ERROR_NO_MORE_ITEMS)
500 rc = ERROR_SUCCESS;
501 SetLastError(rc);
503 else
505 HKEY key;
507 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
508 GetCurrentProcess(), (LPHANDLE)&key,
509 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
510 TRUE, 0))
512 PWINECRYPT_CERTSTORE memStore;
514 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, hCryptProv,
515 CERT_STORE_CREATE_NEW_FLAG, NULL);
516 if (memStore)
518 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
519 sizeof(WINE_REGSTOREINFO));
521 if (regInfo)
523 CERT_STORE_PROV_INFO provInfo = { 0 };
525 regInfo->dwOpenFlags = dwFlags;
526 regInfo->memStore = memStore;
527 regInfo->key = key;
528 InitializeCriticalSection(&regInfo->cs);
529 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
530 list_init(&regInfo->certsToDelete);
531 list_init(&regInfo->crlsToDelete);
532 CRYPT_RegReadFromReg(regInfo->key, regInfo->memStore);
533 regInfo->dirty = FALSE;
534 provInfo.cbSize = sizeof(provInfo);
535 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
536 sizeof(regProvFuncs[0]);
537 provInfo.rgpvStoreProvFunc = regProvFuncs;
538 provInfo.hStoreProv = regInfo;
539 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
540 /* Reg store doesn't need crypto provider, so close it */
541 if (hCryptProv &&
542 !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
543 CryptReleaseContext(hCryptProv, 0);
548 TRACE("returning %p\n", store);
549 return store;