source: trunk/src/crypt32/regstore.c@ 21388

Last change on this file since 21388 was 21354, checked in by rlwalsh, 16 years ago

eliminate VACPP warning & info msgs - see Ticket #1

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