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
29 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
30
31 typedef struct _WINE_HASH_TO_DELETE
32 {
33 BYTE hash[20];
34 struct list entry;
35 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
36
37 typedef struct _WINE_REGSTOREINFO
38 {
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;
48
49 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
50 {
51 static const WCHAR fmt[] = { '%','','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
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 };
66
67 static 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->free(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
160 static 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) */
183 static 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, 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
209 static 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->free(context);
246 return ret;
247 }
248
249 static 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(&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(&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 */
307 static 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
320 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
321 {
322 PWINE_REGSTOREINFO store = 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(&store->cs);
332 CryptMemFree(store);
333 }
334
335 static 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
350 static 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(&store->cs);
375 list_add_tail(deleteList, &toDelete->entry);
376 LeaveCriticalSection(&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
392 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
393 PCCERT_CONTEXT cert, DWORD dwFlags)
394 {
395 PWINE_REGSTOREINFO store = hCertStore;
396
397 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
398
399 return CRYPT_RegWriteContext(store, cert, dwFlags);
400 }
401
402 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
403 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
404 {
405 PWINE_REGSTOREINFO store = hCertStore;
406
407 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
408
409 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
410 pCertInterface);
411 }
412
413 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
414 PCCRL_CONTEXT crl, DWORD dwFlags)
415 {
416 PWINE_REGSTOREINFO store = hCertStore;
417
418 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
419
420 return CRYPT_RegWriteContext(store, crl, dwFlags);
421 }
422
423 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
424 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
425 {
426 PWINE_REGSTOREINFO store = hCertStore;
427
428 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
429
430 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
431 pCRLInterface);
432 }
433
434 static BOOL WINAPI CRYPT_RegWriteCTL(HCERTSTORE hCertStore,
435 PCCTL_CONTEXT ctl, DWORD dwFlags)
436 {
437 PWINE_REGSTOREINFO store = hCertStore;
438
439 TRACE("(%p, %p, %d)\n", hCertStore, ctl, dwFlags);
440
441 return CRYPT_RegWriteContext(store, ctl, dwFlags);
442 }
443
444 static BOOL WINAPI CRYPT_RegDeleteCTL(HCERTSTORE hCertStore,
445 PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
446 {
447 PWINE_REGSTOREINFO store = hCertStore;
448
449 TRACE("(%p, %p, %08x)\n", store, pCtlContext, dwFlags);
450
451 return CRYPT_RegDeleteContext(store, &store->ctlsToDelete, pCtlContext,
452 pCTLInterface);
453 }
454
455 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
456 DWORD dwCtrlType, void const *pvCtrlPara)
457 {
458 PWINE_REGSTOREINFO store = 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 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;
489 }
490 return ret;
491 }
492
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,
508 };
509
510 PWINECRYPT_CERTSTORE CRYPT_RegOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags,
511 const void *pvPara)
512 {
513 PWINECRYPT_CERTSTORE store = NULL;
514
515 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
516
517 if (dwFlags & CERT_STORE_DELETE_FLAG)
518 {
519 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
520
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);
528 }
529 else
530 {
531 HKEY key;
532
533 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
534 GetCurrentProcess(), (LPHANDLE)&key,
535 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
536 TRUE, 0))
537 {
538 PWINECRYPT_CERTSTORE memStore;
539
540 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, hCryptProv,
541 CERT_STORE_CREATE_NEW_FLAG, NULL);
542 if (memStore)
543 {
544 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
545 sizeof(WINE_REGSTOREINFO));
546
547 if (regInfo)
548 {
549 CERT_STORE_PROV_INFO provInfo = { 0 };
550
551 regInfo->dwOpenFlags = dwFlags;
552 regInfo->memStore = memStore;
553 regInfo->key = key;
554 InitializeCriticalSection(®Info->cs);
555 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
556 list_init(®Info->certsToDelete);
557 list_init(®Info->crlsToDelete);
558 list_init(®Info->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);
571 }
572 }
573 }
574 }
575 TRACE("returning %p\n", store);
576 return store;
577 }
578
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.