wininet: Fix szCacheContent in URLCacheContainer_OpenIndex.
[wine/hacks.git] / dlls / crypt32 / store.c
blob3b9bcb815ce8f6d6706e0fa9a87c6cbf7779cd76
1 /*
2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2004-2006 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * FIXME:
20 * - The concept of physical stores and locations isn't implemented. (This
21 * doesn't mean registry stores et al aren't implemented. See the PSDK for
22 * registering and enumerating physical stores and locations.)
23 * - Many flags, options and whatnot are unimplemented.
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "winuser.h"
36 #include "wincrypt.h"
37 #include "wine/debug.h"
38 #include "wine/list.h"
39 #include "excpt.h"
40 #include "wine/exception.h"
41 #include "crypt32_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
45 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
47 static const WINE_CONTEXT_INTERFACE gCertInterface = {
48 (CreateContextFunc)CertCreateCertificateContext,
49 (AddContextToStoreFunc)CertAddCertificateContextToStore,
50 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
51 (DuplicateContextFunc)CertDuplicateCertificateContext,
52 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
53 (EnumPropertiesFunc)CertEnumCertificateContextProperties,
54 (GetContextPropertyFunc)CertGetCertificateContextProperty,
55 (SetContextPropertyFunc)CertSetCertificateContextProperty,
56 (SerializeElementFunc)CertSerializeCertificateStoreElement,
57 (FreeContextFunc)CertFreeCertificateContext,
58 (DeleteContextFunc)CertDeleteCertificateFromStore,
60 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
62 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
63 (CreateContextFunc)CertCreateCRLContext,
64 (AddContextToStoreFunc)CertAddCRLContextToStore,
65 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
66 (DuplicateContextFunc)CertDuplicateCRLContext,
67 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
68 (EnumPropertiesFunc)CertEnumCRLContextProperties,
69 (GetContextPropertyFunc)CertGetCRLContextProperty,
70 (SetContextPropertyFunc)CertSetCRLContextProperty,
71 (SerializeElementFunc)CertSerializeCRLStoreElement,
72 (FreeContextFunc)CertFreeCRLContext,
73 (DeleteContextFunc)CertDeleteCRLFromStore,
75 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
77 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
78 (CreateContextFunc)CertCreateCTLContext,
79 (AddContextToStoreFunc)CertAddCTLContextToStore,
80 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
81 (DuplicateContextFunc)CertDuplicateCTLContext,
82 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
83 (EnumPropertiesFunc)CertEnumCTLContextProperties,
84 (GetContextPropertyFunc)CertGetCTLContextProperty,
85 (SetContextPropertyFunc)CertSetCTLContextProperty,
86 (SerializeElementFunc)CertSerializeCTLStoreElement,
87 (FreeContextFunc)CertFreeCTLContext,
88 (DeleteContextFunc)CertDeleteCTLFromStore,
90 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
92 struct WINE_CRYPTCERTSTORE;
94 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
95 DWORD dwFlags, const void *pvPara);
97 /* Called to enumerate the next context in a store. */
98 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
100 /* Called to add a context to a store. If toReplace is not NULL,
101 * context replaces toReplace in the store, and access checks should not be
102 * performed. Otherwise context is a new context, and it should only be
103 * added if the store allows it. If ppStoreContext is not NULL, the added
104 * context should be returned in *ppStoreContext.
106 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
107 void *toReplace, const void **ppStoreContext);
109 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
111 typedef struct _CONTEXT_STORE
113 AddFunc addContext;
114 EnumFunc enumContext;
115 DeleteFunc deleteContext;
116 } CONTEXT_STORE, *PCONTEXT_STORE;
118 typedef enum _CertStoreType {
119 StoreTypeMem,
120 StoreTypeCollection,
121 StoreTypeProvider,
122 } CertStoreType;
124 /* A cert store is polymorphic through the use of function pointers. A type
125 * is still needed to distinguish collection stores from other types.
126 * On the function pointers:
127 * - closeStore is called when the store's ref count becomes 0
128 * - control is optional, but should be implemented by any store that supports
129 * persistence
131 typedef struct WINE_CRYPTCERTSTORE
133 DWORD dwMagic;
134 LONG ref;
135 DWORD dwOpenFlags;
136 HCRYPTPROV cryptProv;
137 CertStoreType type;
138 PFN_CERT_STORE_PROV_CLOSE closeStore;
139 CONTEXT_STORE certs;
140 CONTEXT_STORE crls;
141 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
142 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
144 typedef struct _WINE_MEMSTORE
146 WINECRYPT_CERTSTORE hdr;
147 struct ContextList *certs;
148 struct ContextList *crls;
149 } WINE_MEMSTORE, *PWINE_MEMSTORE;
151 typedef struct _WINE_HASH_TO_DELETE
153 BYTE hash[20];
154 struct list entry;
155 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
157 typedef struct _WINE_REGSTOREINFO
159 DWORD dwOpenFlags;
160 HCRYPTPROV cryptProv;
161 PWINECRYPT_CERTSTORE memStore;
162 HKEY key;
163 BOOL dirty;
164 CRITICAL_SECTION cs;
165 struct list certsToDelete;
166 struct list crlsToDelete;
167 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
169 typedef struct _WINE_FILESTOREINFO
171 DWORD dwOpenFlags;
172 HCRYPTPROV cryptProv;
173 PWINECRYPT_CERTSTORE memStore;
174 HANDLE file;
175 BOOL dirty;
176 } WINE_FILESTOREINFO, *PWINE_FILESTOREINFO;
178 typedef struct _WINE_STORE_LIST_ENTRY
180 PWINECRYPT_CERTSTORE store;
181 DWORD dwUpdateFlags;
182 DWORD dwPriority;
183 struct list entry;
184 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
186 typedef struct _WINE_COLLECTIONSTORE
188 WINECRYPT_CERTSTORE hdr;
189 CRITICAL_SECTION cs;
190 struct list stores;
191 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
193 typedef struct _WINE_PROVIDERSTORE
195 WINECRYPT_CERTSTORE hdr;
196 DWORD dwStoreProvFlags;
197 PWINECRYPT_CERTSTORE memStore;
198 HCERTSTOREPROV hStoreProv;
199 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
200 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
201 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
202 PFN_CERT_STORE_PROV_WRITE_CRL provWriteCrl;
203 PFN_CERT_STORE_PROV_DELETE_CRL provDeleteCrl;
204 PFN_CERT_STORE_PROV_CONTROL provControl;
205 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
207 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
208 DWORD dwFlags, CertStoreType type)
210 store->ref = 1;
211 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
212 store->type = type;
213 if (!hCryptProv)
215 hCryptProv = CRYPT_GetDefaultProvider();
216 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
218 store->cryptProv = hCryptProv;
219 store->dwOpenFlags = dwFlags;
222 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
223 void *toReplace, const void **ppStoreContext)
225 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
226 PCERT_CONTEXT context;
228 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
230 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
231 if (context)
233 context->hCertStore = store;
234 if (ppStoreContext)
235 *ppStoreContext = CertDuplicateCertificateContext(context);
237 return context ? TRUE : FALSE;
240 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
242 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
243 void *ret;
245 TRACE("(%p, %p)\n", store, pPrev);
247 ret = ContextList_Enum(ms->certs, pPrev);
248 if (!ret)
249 SetLastError(CRYPT_E_NOT_FOUND);
251 TRACE("returning %p\n", ret);
252 return ret;
255 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
257 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
259 ContextList_Delete(ms->certs, pCertContext);
260 return TRUE;
263 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
264 void *toReplace, const void **ppStoreContext)
266 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
267 PCRL_CONTEXT context;
269 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
271 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
272 if (context)
274 context->hCertStore = store;
275 if (ppStoreContext)
276 *ppStoreContext = CertDuplicateCRLContext(context);
278 return context ? TRUE : FALSE;
281 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
283 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
284 void *ret;
286 TRACE("(%p, %p)\n", store, pPrev);
288 ret = ContextList_Enum(ms->crls, pPrev);
289 if (!ret)
290 SetLastError(CRYPT_E_NOT_FOUND);
292 TRACE("returning %p\n", ret);
293 return ret;
296 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
298 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
300 ContextList_Delete(ms->crls, pCrlContext);
301 return TRUE;
304 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
306 ContextList_Empty(store->certs);
307 ContextList_Empty(store->crls);
310 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
312 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
314 TRACE("(%p, %08x)\n", store, dwFlags);
315 if (dwFlags)
316 FIXME("Unimplemented flags: %08x\n", dwFlags);
318 ContextList_Free(store->certs);
319 ContextList_Free(store->crls);
320 CryptMemFree(store);
323 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
324 DWORD dwFlags, const void *pvPara)
326 PWINE_MEMSTORE store;
328 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
330 if (dwFlags & CERT_STORE_DELETE_FLAG)
332 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
333 store = NULL;
335 else
337 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
338 if (store)
340 memset(store, 0, sizeof(WINE_MEMSTORE));
341 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
342 store->hdr.closeStore = CRYPT_MemCloseStore;
343 store->hdr.certs.addContext = CRYPT_MemAddCert;
344 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
345 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
346 store->hdr.crls.addContext = CRYPT_MemAddCrl;
347 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
348 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
349 store->hdr.control = NULL;
350 store->certs = ContextList_Create(pCertInterface,
351 sizeof(CERT_CONTEXT));
352 store->crls = ContextList_Create(pCRLInterface,
353 sizeof(CRL_CONTEXT));
356 return (PWINECRYPT_CERTSTORE)store;
359 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
361 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
362 PWINE_STORE_LIST_ENTRY entry, next;
364 TRACE("(%p, %08x)\n", store, dwFlags);
366 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
367 entry)
369 TRACE("closing %p\n", entry);
370 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
371 CryptMemFree(entry);
373 cs->cs.DebugInfo->Spare[0] = 0;
374 DeleteCriticalSection(&cs->cs);
375 CryptMemFree(cs);
378 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
379 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
380 BOOL addRef)
382 void *ret = Context_CreateLinkContext(contextSize, child,
383 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
385 if (ret)
386 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
387 = storeEntry;
389 return ret;
392 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
393 unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
394 void **pChildContext)
396 BOOL ret;
397 void *childContext = NULL;
398 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
400 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
401 toReplace, contextSize);
403 ret = FALSE;
404 if (toReplace)
406 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
407 PCONTEXT_STORE contextStore;
409 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
410 contextSize);
411 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
412 contextStoreOffset);
413 ret = contextStore->addContext(storeEntry->store, context,
414 existingLinked, (const void **)&childContext);
416 else
418 PWINE_STORE_LIST_ENTRY entry, next;
420 EnterCriticalSection(&store->cs);
421 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
422 WINE_STORE_LIST_ENTRY, entry)
424 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
426 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
427 (LPBYTE)entry->store + contextStoreOffset);
429 storeEntry = entry;
430 ret = contextStore->addContext(entry->store, context, NULL,
431 (const void **)&childContext);
432 break;
435 LeaveCriticalSection(&store->cs);
436 if (!storeEntry)
437 SetLastError(E_ACCESSDENIED);
439 *pChildContext = childContext;
440 return ret;
443 /* Advances a collection enumeration by one context, if possible, where
444 * advancing means:
445 * - calling the current store's enumeration function once, and returning
446 * the enumerated context if one is returned
447 * - moving to the next store if the current store has no more items, and
448 * recursively calling itself to get the next item.
449 * Returns NULL if the collection contains no more items or on error.
450 * Assumes the collection store's lock is held.
452 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
453 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
454 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
456 void *ret, *child;
457 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
458 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
459 contextStoreOffset);
461 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
463 if (pPrev)
465 /* Ref-counting funny business: "duplicate" (addref) the child, because
466 * the free(pPrev) below can cause the ref count to become negative.
468 child = Context_GetLinkedContext(pPrev, contextSize);
469 contextInterface->duplicate(child);
470 child = contextStore->enumContext(storeEntry->store, child);
471 contextInterface->free(pPrev);
472 pPrev = NULL;
474 else
475 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
476 if (child)
477 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
478 contextSize, FALSE);
479 else
481 if (storeNext)
482 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
483 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
484 contextInterface, NULL, contextSize);
485 else
487 SetLastError(CRYPT_E_NOT_FOUND);
488 ret = NULL;
491 TRACE("returning %p\n", ret);
492 return ret;
495 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
496 void *toReplace, const void **ppStoreContext)
498 BOOL ret;
499 void *childContext = NULL;
500 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
502 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
503 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
504 if (ppStoreContext && childContext)
506 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
507 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
508 PCERT_CONTEXT context =
509 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
510 sizeof(CERT_CONTEXT), TRUE);
512 if (context)
513 context->hCertStore = store;
514 *ppStoreContext = context;
516 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
517 return ret;
520 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
522 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
523 void *ret;
525 TRACE("(%p, %p)\n", store, pPrev);
527 EnterCriticalSection(&cs->cs);
528 if (pPrev)
530 PWINE_STORE_LIST_ENTRY storeEntry =
531 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
532 sizeof(CERT_CONTEXT));
534 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
535 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
536 sizeof(CERT_CONTEXT));
538 else
540 if (!list_empty(&cs->stores))
542 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
543 WINE_STORE_LIST_ENTRY, entry);
545 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
546 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
547 sizeof(CERT_CONTEXT));
549 else
551 SetLastError(CRYPT_E_NOT_FOUND);
552 ret = NULL;
555 LeaveCriticalSection(&cs->cs);
556 if (ret)
557 ((PCERT_CONTEXT)ret)->hCertStore = store;
558 TRACE("returning %p\n", ret);
559 return ret;
562 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
563 void *pCertContext)
565 BOOL ret;
567 TRACE("(%p, %p)\n", store, pCertContext);
569 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
570 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
571 return ret;
574 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
575 void *toReplace, const void **ppStoreContext)
577 BOOL ret;
578 void *childContext = NULL;
579 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
581 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
582 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
583 if (ppStoreContext && childContext)
585 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
586 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
587 PCRL_CONTEXT context =
588 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
589 sizeof(CRL_CONTEXT), TRUE);
591 if (context)
592 context->hCertStore = store;
593 *ppStoreContext = context;
595 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
596 return ret;
599 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
601 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
602 void *ret;
604 TRACE("(%p, %p)\n", store, pPrev);
606 EnterCriticalSection(&cs->cs);
607 if (pPrev)
609 PWINE_STORE_LIST_ENTRY storeEntry =
610 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
611 sizeof(CRL_CONTEXT));
613 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
614 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
615 sizeof(CRL_CONTEXT));
617 else
619 if (!list_empty(&cs->stores))
621 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
622 WINE_STORE_LIST_ENTRY, entry);
624 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
625 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
626 sizeof(CRL_CONTEXT));
628 else
630 SetLastError(CRYPT_E_NOT_FOUND);
631 ret = NULL;
634 LeaveCriticalSection(&cs->cs);
635 if (ret)
636 ((PCRL_CONTEXT)ret)->hCertStore = store;
637 TRACE("returning %p\n", ret);
638 return ret;
641 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
642 void *pCrlContext)
644 BOOL ret;
646 TRACE("(%p, %p)\n", store, pCrlContext);
648 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
649 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
650 return ret;
653 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
654 DWORD dwFlags, const void *pvPara)
656 PWINE_COLLECTIONSTORE store;
658 if (dwFlags & CERT_STORE_DELETE_FLAG)
660 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
661 store = NULL;
663 else
665 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
666 if (store)
668 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
669 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
670 StoreTypeCollection);
671 store->hdr.closeStore = CRYPT_CollectionCloseStore;
672 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
673 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
674 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
675 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
676 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
677 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
678 InitializeCriticalSection(&store->cs);
679 store->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_COLLECTIONSTORE->cs");
680 list_init(&store->stores);
683 return (PWINECRYPT_CERTSTORE)store;
686 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
688 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
690 TRACE("(%p, %08x)\n", store, dwFlags);
692 if (store->provCloseStore)
693 store->provCloseStore(store->hStoreProv, dwFlags);
694 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
695 CertCloseStore(store->memStore, dwFlags);
696 CryptMemFree(store);
699 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
700 void *toReplace, const void **ppStoreContext)
702 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
703 BOOL ret;
705 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
707 if (toReplace)
708 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
709 (const void **)ppStoreContext);
710 else
712 ret = TRUE;
713 if (ps->provWriteCert)
714 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
715 CERT_STORE_PROV_WRITE_ADD_FLAG);
716 if (ret)
717 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
718 (const void **)ppStoreContext);
720 /* dirty trick: replace the returned context's hCertStore with
721 * store.
723 if (ppStoreContext)
724 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
725 return ret;
728 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
730 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
731 void *ret;
733 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
734 if (ret)
736 /* same dirty trick: replace the returned context's hCertStore with
737 * store.
739 ((PCERT_CONTEXT)ret)->hCertStore = store;
741 return ret;
744 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
746 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
747 BOOL ret = TRUE;
749 TRACE("(%p, %p)\n", store, cert);
751 if (ps->provDeleteCert)
752 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
753 if (ret)
754 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
755 return ret;
758 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
759 void *toReplace, const void **ppStoreContext)
761 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
762 BOOL ret;
764 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
766 if (toReplace)
767 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
768 (const void **)ppStoreContext);
769 else
771 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
773 SetLastError(ERROR_ACCESS_DENIED);
774 ret = FALSE;
776 else
778 ret = TRUE;
779 if (ps->provWriteCrl)
780 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
781 CERT_STORE_PROV_WRITE_ADD_FLAG);
782 if (ret)
783 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
784 (const void **)ppStoreContext);
787 /* dirty trick: replace the returned context's hCertStore with
788 * store.
790 if (ppStoreContext)
791 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
792 return ret;
795 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
797 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
798 void *ret;
800 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
801 if (ret)
803 /* same dirty trick: replace the returned context's hCertStore with
804 * store.
806 ((PCERT_CONTEXT)ret)->hCertStore = store;
808 return ret;
811 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
813 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
814 BOOL ret = TRUE;
816 TRACE("(%p, %p)\n", store, crl);
818 if (ps->provDeleteCrl)
819 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
820 if (ret)
821 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
822 return ret;
825 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
826 DWORD dwCtrlType, void const *pvCtrlPara)
828 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
829 BOOL ret = TRUE;
831 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
832 pvCtrlPara);
834 if (store->provControl)
835 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
836 pvCtrlPara);
837 return ret;
840 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
841 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo)
843 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
844 sizeof(WINE_PROVIDERSTORE));
846 if (ret)
848 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
849 StoreTypeProvider);
850 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
851 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
853 CertCloseStore(memStore, 0);
854 ret->memStore = NULL;
856 else
857 ret->memStore = memStore;
858 ret->hStoreProv = pProvInfo->hStoreProv;
859 ret->hdr.closeStore = CRYPT_ProvCloseStore;
860 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
861 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
862 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
863 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
864 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
865 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
866 ret->hdr.control = CRYPT_ProvControl;
867 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
868 ret->provCloseStore =
869 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
870 else
871 ret->provCloseStore = NULL;
872 if (pProvInfo->cStoreProvFunc >
873 CERT_STORE_PROV_WRITE_CERT_FUNC)
874 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
875 CERT_STORE_PROV_WRITE_CERT_FUNC];
876 else
877 ret->provWriteCert = NULL;
878 if (pProvInfo->cStoreProvFunc >
879 CERT_STORE_PROV_DELETE_CERT_FUNC)
880 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
881 CERT_STORE_PROV_DELETE_CERT_FUNC];
882 else
883 ret->provDeleteCert = NULL;
884 if (pProvInfo->cStoreProvFunc >
885 CERT_STORE_PROV_WRITE_CRL_FUNC)
886 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
887 CERT_STORE_PROV_WRITE_CRL_FUNC];
888 else
889 ret->provWriteCert = NULL;
890 if (pProvInfo->cStoreProvFunc >
891 CERT_STORE_PROV_DELETE_CRL_FUNC)
892 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
893 CERT_STORE_PROV_DELETE_CRL_FUNC];
894 else
895 ret->provDeleteCert = NULL;
896 if (pProvInfo->cStoreProvFunc >
897 CERT_STORE_PROV_CONTROL_FUNC)
898 ret->provControl = pProvInfo->rgpvStoreProvFunc[
899 CERT_STORE_PROV_CONTROL_FUNC];
900 else
901 ret->provControl = NULL;
903 return (PWINECRYPT_CERTSTORE)ret;
906 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
907 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
909 static HCRYPTOIDFUNCSET set = NULL;
910 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
911 HCRYPTOIDFUNCADDR hFunc;
912 PWINECRYPT_CERTSTORE ret = NULL;
914 if (!set)
915 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
916 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
917 (void **)&provOpenFunc, &hFunc);
918 if (provOpenFunc)
920 CERT_STORE_PROV_INFO provInfo = { 0 };
922 provInfo.cbSize = sizeof(provInfo);
923 if (dwFlags & CERT_STORE_DELETE_FLAG)
924 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
925 dwFlags, pvPara, NULL, &provInfo);
926 else
928 HCERTSTORE memStore;
930 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
931 CERT_STORE_CREATE_NEW_FLAG, NULL);
932 if (memStore)
934 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
935 dwFlags, pvPara, memStore, &provInfo))
936 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
937 &provInfo);
938 else
939 CertCloseStore(memStore, 0);
942 CryptFreeOIDFunctionAddress(hFunc, 0);
944 else
945 SetLastError(ERROR_FILE_NOT_FOUND);
946 return ret;
949 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
951 static const WCHAR fmt[] = { '%','0','2','X',0 };
952 DWORD i;
954 assert(hash);
955 assert(asciiHash);
957 for (i = 0; i < 20; i++)
958 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
961 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
962 0 };
963 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
964 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
965 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
967 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO *store, HKEY key,
968 DWORD contextType)
970 LONG rc;
971 DWORD index = 0;
972 WCHAR subKeyName[MAX_PATH];
974 do {
975 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
977 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
978 NULL);
979 if (!rc)
981 HKEY subKey;
983 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
984 if (!rc)
986 LPBYTE buf = NULL;
988 size = 0;
989 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
990 if (!rc)
991 buf = CryptMemAlloc(size);
992 if (buf)
994 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
995 &size);
996 if (!rc)
998 const void *context;
999 DWORD addedType;
1001 TRACE("Adding cert with hash %s\n",
1002 debugstr_w(subKeyName));
1003 context = CRYPT_ReadSerializedElement(buf, size,
1004 contextType, &addedType);
1005 if (context)
1007 const WINE_CONTEXT_INTERFACE *contextInterface;
1008 BYTE hash[20];
1010 switch (addedType)
1012 case CERT_STORE_CERTIFICATE_CONTEXT:
1013 contextInterface = &gCertInterface;
1014 break;
1015 case CERT_STORE_CRL_CONTEXT:
1016 contextInterface = &gCRLInterface;
1017 break;
1018 case CERT_STORE_CTL_CONTEXT:
1019 contextInterface = &gCTLInterface;
1020 break;
1021 default:
1022 contextInterface = NULL;
1024 if (contextInterface)
1026 size = sizeof(hash);
1027 if (contextInterface->getProp(context,
1028 CERT_HASH_PROP_ID, hash, &size))
1030 WCHAR asciiHash[20 * 2 + 1];
1032 CRYPT_HashToStr(hash, asciiHash);
1033 TRACE("comparing %s\n",
1034 debugstr_w(asciiHash));
1035 TRACE("with %s\n", debugstr_w(subKeyName));
1036 if (!lstrcmpW(asciiHash, subKeyName))
1038 TRACE("hash matches, adding\n");
1039 contextInterface->addContextToStore(
1040 store->memStore, context,
1041 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1043 else
1044 TRACE("hash doesn't match, ignoring\n");
1046 contextInterface->free(context);
1050 CryptMemFree(buf);
1052 RegCloseKey(subKey);
1054 /* Ignore intermediate errors, continue enumerating */
1055 rc = ERROR_SUCCESS;
1057 } while (!rc);
1060 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO *store)
1062 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1063 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1064 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1065 DWORD i;
1067 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1069 HKEY key;
1070 LONG rc;
1072 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1073 &key, NULL);
1074 if (!rc)
1076 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1077 RegCloseKey(key);
1082 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1083 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
1084 DWORD len)
1086 WCHAR asciiHash[20 * 2 + 1];
1087 LONG rc;
1088 HKEY subKey;
1089 BOOL ret;
1091 CRYPT_HashToStr(hash, asciiHash);
1092 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1093 &subKey, NULL);
1094 if (!rc)
1096 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1097 RegCloseKey(subKey);
1099 if (!rc)
1100 ret = TRUE;
1101 else
1103 SetLastError(rc);
1104 ret = FALSE;
1106 return ret;
1109 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1110 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1112 const void *context = NULL;
1113 BOOL ret;
1115 do {
1116 context = contextInterface->enumContextsInStore(memStore, context);
1117 if (context)
1119 BYTE hash[20];
1120 DWORD hashSize = sizeof(hash);
1122 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1123 &hashSize);
1124 if (ret)
1126 DWORD size = 0;
1127 LPBYTE buf = NULL;
1129 ret = contextInterface->serialize(context, 0, NULL, &size);
1130 if (size)
1131 buf = CryptMemAlloc(size);
1132 if (buf)
1134 ret = contextInterface->serialize(context, 0, buf, &size);
1135 if (ret)
1136 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1138 CryptMemFree(buf);
1141 else
1142 ret = TRUE;
1143 } while (ret && context != NULL);
1144 if (context)
1145 contextInterface->free(context);
1146 return ret;
1149 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1151 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1152 static const WINE_CONTEXT_INTERFACE * const interfaces[] = { &gCertInterface,
1153 &gCRLInterface, &gCTLInterface };
1154 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1155 NULL };
1156 BOOL ret = TRUE;
1157 DWORD i;
1159 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1161 HKEY key;
1162 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1163 KEY_ALL_ACCESS, NULL, &key, NULL);
1165 if (!rc)
1167 if (listToDelete[i])
1169 PWINE_HASH_TO_DELETE toDelete, next;
1170 WCHAR asciiHash[20 * 2 + 1];
1172 EnterCriticalSection(&store->cs);
1173 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1174 WINE_HASH_TO_DELETE, entry)
1176 LONG rc;
1178 CRYPT_HashToStr(toDelete->hash, asciiHash);
1179 TRACE("Removing %s\n", debugstr_w(asciiHash));
1180 rc = RegDeleteKeyW(key, asciiHash);
1181 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1183 SetLastError(rc);
1184 ret = FALSE;
1186 list_remove(&toDelete->entry);
1187 CryptMemFree(toDelete);
1189 LeaveCriticalSection(&store->cs);
1191 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1192 store->memStore);
1193 RegCloseKey(key);
1195 else
1197 SetLastError(rc);
1198 ret = FALSE;
1201 return ret;
1204 /* If force is true or the registry store is dirty, writes the contents of the
1205 * store to the registry.
1207 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1209 BOOL ret;
1211 TRACE("(%p, %d)\n", store, force);
1213 if (store->dirty || force)
1214 ret = CRYPT_RegWriteToReg(store);
1215 else
1216 ret = TRUE;
1217 return ret;
1220 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1222 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1224 TRACE("(%p, %08x)\n", store, dwFlags);
1225 if (dwFlags)
1226 FIXME("Unimplemented flags: %08x\n", dwFlags);
1228 CRYPT_RegFlushStore(store, FALSE);
1229 RegCloseKey(store->key);
1230 store->cs.DebugInfo->Spare[0] = 0;
1231 DeleteCriticalSection(&store->cs);
1232 CryptMemFree(store);
1235 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1236 const void *context, DWORD dwFlags)
1238 BOOL ret;
1240 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1242 store->dirty = TRUE;
1243 ret = TRUE;
1245 else
1246 ret = FALSE;
1247 return ret;
1250 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1251 struct list *deleteList, const void *context,
1252 PCWINE_CONTEXT_INTERFACE contextInterface)
1254 BOOL ret;
1256 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1258 SetLastError(ERROR_ACCESS_DENIED);
1259 ret = FALSE;
1261 else
1263 PWINE_HASH_TO_DELETE toDelete =
1264 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1266 if (toDelete)
1268 DWORD size = sizeof(toDelete->hash);
1270 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1271 toDelete->hash, &size);
1272 if (ret)
1274 EnterCriticalSection(&store->cs);
1275 list_add_tail(deleteList, &toDelete->entry);
1276 LeaveCriticalSection(&store->cs);
1278 else
1280 CryptMemFree(toDelete);
1281 ret = FALSE;
1284 else
1285 ret = FALSE;
1286 if (ret)
1287 store->dirty = TRUE;
1289 return ret;
1292 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1293 PCCERT_CONTEXT cert, DWORD dwFlags)
1295 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1297 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1299 return CRYPT_RegWriteContext(store, cert, dwFlags);
1302 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1303 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1305 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1307 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1309 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1310 pCertInterface);
1313 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1314 PCCRL_CONTEXT crl, DWORD dwFlags)
1316 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1318 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1320 return CRYPT_RegWriteContext(store, crl, dwFlags);
1323 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1324 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1326 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1328 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1330 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1331 pCRLInterface);
1334 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1335 DWORD dwCtrlType, void const *pvCtrlPara)
1337 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1338 BOOL ret;
1340 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1341 pvCtrlPara);
1343 switch (dwCtrlType)
1345 case CERT_STORE_CTRL_RESYNC:
1346 CRYPT_RegFlushStore(store, FALSE);
1347 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1348 CRYPT_RegReadFromReg(store);
1349 ret = TRUE;
1350 break;
1351 case CERT_STORE_CTRL_COMMIT:
1352 ret = CRYPT_RegFlushStore(store,
1353 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1354 break;
1355 default:
1356 FIXME("%d: stub\n", dwCtrlType);
1357 ret = FALSE;
1359 return ret;
1362 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1363 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1365 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1366 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1367 HKEY hSubKey = 0;
1369 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1371 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1372 if (!dwRet)
1374 /* Find how many subkeys there are */
1375 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1376 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1377 if (!dwRet)
1379 dwMaxSubkeyLen++;
1380 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1382 /* Name too big: alloc a buffer for it */
1383 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1386 if (!lpszName)
1387 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1388 else
1390 /* Recursively delete all the subkeys */
1391 for (i = 0; i < dwKeyCount && !dwRet; i++)
1393 dwSize = dwMaxSubkeyLen;
1394 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1395 NULL, NULL, NULL);
1396 if (!dwRet)
1397 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1400 if (lpszName != szNameBuf)
1402 /* Free buffer if allocated */
1403 CryptMemFree(lpszName);
1408 RegCloseKey(hSubKey);
1409 if (!dwRet)
1410 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1412 return dwRet;
1415 static void *regProvFuncs[] = {
1416 CRYPT_RegCloseStore,
1417 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1418 CRYPT_RegWriteCert,
1419 CRYPT_RegDeleteCert,
1420 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1421 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1422 CRYPT_RegWriteCRL,
1423 CRYPT_RegDeleteCRL,
1424 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1425 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1426 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1427 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1428 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1429 CRYPT_RegControl,
1432 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1433 DWORD dwFlags, const void *pvPara)
1435 PWINECRYPT_CERTSTORE store = NULL;
1437 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1439 if (dwFlags & CERT_STORE_DELETE_FLAG)
1441 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1443 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1444 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1445 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1446 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1447 if (rc == ERROR_NO_MORE_ITEMS)
1448 rc = ERROR_SUCCESS;
1449 SetLastError(rc);
1451 else
1453 HKEY key;
1455 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1456 GetCurrentProcess(), (LPHANDLE)&key,
1457 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1458 TRUE, 0))
1460 PWINECRYPT_CERTSTORE memStore;
1462 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1463 if (memStore)
1465 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1466 sizeof(WINE_REGSTOREINFO));
1468 if (regInfo)
1470 CERT_STORE_PROV_INFO provInfo = { 0 };
1472 regInfo->dwOpenFlags = dwFlags;
1473 regInfo->cryptProv = hCryptProv;
1474 regInfo->memStore = memStore;
1475 regInfo->key = key;
1476 InitializeCriticalSection(&regInfo->cs);
1477 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
1478 list_init(&regInfo->certsToDelete);
1479 list_init(&regInfo->crlsToDelete);
1480 CRYPT_RegReadFromReg(regInfo);
1481 regInfo->dirty = FALSE;
1482 provInfo.cbSize = sizeof(provInfo);
1483 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1484 sizeof(regProvFuncs[0]);
1485 provInfo.rgpvStoreProvFunc = regProvFuncs;
1486 provInfo.hStoreProv = regInfo;
1487 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1488 &provInfo);
1493 TRACE("returning %p\n", store);
1494 return store;
1497 /* FIXME: this isn't complete for the Root store, in which the top-level
1498 * self-signed CA certs reside. Adding a cert to the Root store should present
1499 * the user with a dialog indicating the consequences of doing so, and asking
1500 * the user to confirm whether the cert should be added.
1502 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1503 DWORD dwFlags, const void *pvPara)
1505 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1506 LPCWSTR storeName = (LPCWSTR)pvPara;
1507 LPWSTR storePath;
1508 PWINECRYPT_CERTSTORE store = NULL;
1509 HKEY root;
1510 LPCWSTR base;
1511 BOOL ret;
1513 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1514 debugstr_w((LPCWSTR)pvPara));
1516 if (!pvPara)
1518 SetLastError(E_INVALIDARG);
1519 return NULL;
1522 ret = TRUE;
1523 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1525 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1526 root = HKEY_LOCAL_MACHINE;
1527 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1528 break;
1529 case CERT_SYSTEM_STORE_CURRENT_USER:
1530 root = HKEY_CURRENT_USER;
1531 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1532 break;
1533 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1534 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1535 * SystemCertificates
1537 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1538 debugstr_w(storeName));
1539 return NULL;
1540 case CERT_SYSTEM_STORE_SERVICES:
1541 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1542 * SystemCertificates
1544 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1545 debugstr_w(storeName));
1546 return NULL;
1547 case CERT_SYSTEM_STORE_USERS:
1548 /* hku\user sid\Software\Microsoft\SystemCertificates */
1549 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1550 debugstr_w(storeName));
1551 return NULL;
1552 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1553 root = HKEY_CURRENT_USER;
1554 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1555 break;
1556 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1557 root = HKEY_LOCAL_MACHINE;
1558 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1559 break;
1560 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1561 /* hklm\Software\Microsoft\EnterpriseCertificates */
1562 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1563 debugstr_w(storeName));
1564 return NULL;
1565 default:
1566 SetLastError(E_INVALIDARG);
1567 return NULL;
1570 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1571 sizeof(WCHAR));
1572 if (storePath)
1574 LONG rc;
1575 HKEY key;
1576 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1577 KEY_ALL_ACCESS;
1579 wsprintfW(storePath, fmt, base, storeName);
1580 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1581 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1582 else
1584 DWORD disp;
1586 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1587 &key, &disp);
1588 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1589 disp == REG_OPENED_EXISTING_KEY)
1591 RegCloseKey(key);
1592 rc = ERROR_FILE_EXISTS;
1595 if (!rc)
1597 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1598 RegCloseKey(key);
1600 else
1601 SetLastError(rc);
1602 CryptMemFree(storePath);
1604 return store;
1607 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1608 DWORD dwFlags, const void *pvPara)
1610 int len;
1611 PWINECRYPT_CERTSTORE ret = NULL;
1613 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1614 debugstr_a((LPCSTR)pvPara));
1616 if (!pvPara)
1618 SetLastError(ERROR_FILE_NOT_FOUND);
1619 return NULL;
1621 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1622 if (len)
1624 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1626 if (storeName)
1628 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1629 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1630 CryptMemFree(storeName);
1633 return ret;
1636 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1637 DWORD dwFlags, const void *pvPara)
1639 HCERTSTORE store = 0;
1640 BOOL ret;
1642 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1643 debugstr_w((LPCWSTR)pvPara));
1645 if (!pvPara)
1647 SetLastError(ERROR_FILE_NOT_FOUND);
1648 return NULL;
1650 /* This returns a different error than system registry stores if the
1651 * location is invalid.
1653 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1655 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1656 case CERT_SYSTEM_STORE_CURRENT_USER:
1657 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1658 case CERT_SYSTEM_STORE_SERVICES:
1659 case CERT_SYSTEM_STORE_USERS:
1660 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1661 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1662 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1663 ret = TRUE;
1664 break;
1665 default:
1666 SetLastError(ERROR_FILE_NOT_FOUND);
1667 ret = FALSE;
1669 if (ret)
1671 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1672 0, hCryptProv, dwFlags, pvPara);
1674 if (regStore)
1676 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1677 CERT_STORE_CREATE_NEW_FLAG, NULL);
1678 CertAddStoreToCollection(store, regStore,
1679 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1680 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1681 CertCloseStore(regStore, 0);
1682 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1683 * stores.
1685 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1686 CERT_SYSTEM_STORE_CURRENT_USER)
1688 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1689 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1690 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1691 hCryptProv, dwFlags, pvPara);
1692 if (regStore)
1694 CertAddStoreToCollection(store, regStore,
1695 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1696 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1697 CertCloseStore(regStore, 0);
1702 return (PWINECRYPT_CERTSTORE)store;
1705 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1706 DWORD dwFlags, const void *pvPara)
1708 int len;
1709 PWINECRYPT_CERTSTORE ret = NULL;
1711 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1712 debugstr_a((LPCSTR)pvPara));
1714 if (!pvPara)
1716 SetLastError(ERROR_FILE_NOT_FOUND);
1717 return NULL;
1719 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1720 if (len)
1722 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1724 if (storeName)
1726 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1727 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1728 CryptMemFree(storeName);
1731 return ret;
1734 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1736 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1738 TRACE("(%p, %08x)\n", store, dwFlags);
1739 if (store->dirty)
1740 CRYPT_WriteSerializedFile(store->file, store->memStore);
1741 CertCloseStore(store->memStore, dwFlags);
1742 CloseHandle(store->file);
1743 CryptMemFree(store);
1746 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1747 PCCERT_CONTEXT cert, DWORD dwFlags)
1749 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1751 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1752 store->dirty = TRUE;
1753 return TRUE;
1756 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1757 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1759 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1761 TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1762 store->dirty = TRUE;
1763 return TRUE;
1766 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1767 PCCRL_CONTEXT crl, DWORD dwFlags)
1769 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1771 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1772 store->dirty = TRUE;
1773 return TRUE;
1776 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1777 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1779 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1781 TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1782 store->dirty = TRUE;
1783 return TRUE;
1786 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1787 DWORD dwCtrlType, void const *pvCtrlPara)
1789 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1790 BOOL ret;
1792 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1793 pvCtrlPara);
1795 switch (dwCtrlType)
1797 case CERT_STORE_CTRL_RESYNC:
1798 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1799 CRYPT_ReadSerializedFile(store->file, store);
1800 ret = TRUE;
1801 break;
1802 case CERT_STORE_CTRL_COMMIT:
1803 if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1805 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1806 ret = FALSE;
1808 else if (store->dirty)
1809 ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1810 else
1811 ret = TRUE;
1812 break;
1813 default:
1814 FIXME("%d: stub\n", dwCtrlType);
1815 ret = FALSE;
1817 return ret;
1820 static void *fileProvFuncs[] = {
1821 CRYPT_FileCloseStore,
1822 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1823 CRYPT_FileWriteCert,
1824 CRYPT_FileDeleteCert,
1825 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1826 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1827 CRYPT_FileWriteCRL,
1828 CRYPT_FileDeleteCRL,
1829 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1830 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1831 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1832 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1833 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1834 CRYPT_FileControl,
1837 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1838 DWORD dwFlags, const void *pvPara)
1840 PWINECRYPT_CERTSTORE store = NULL;
1841 HANDLE file = (HANDLE)pvPara;
1843 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1845 if (!pvPara)
1847 SetLastError(ERROR_INVALID_HANDLE);
1848 return NULL;
1850 if (dwFlags & CERT_STORE_DELETE_FLAG)
1852 SetLastError(E_INVALIDARG);
1853 return NULL;
1855 if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1856 (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1858 SetLastError(E_INVALIDARG);
1859 return NULL;
1862 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1863 GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1864 GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1866 PWINECRYPT_CERTSTORE memStore;
1868 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1869 if (memStore)
1871 if (CRYPT_ReadSerializedFile(file, memStore))
1873 PWINE_FILESTOREINFO info = CryptMemAlloc(
1874 sizeof(WINE_FILESTOREINFO));
1876 if (info)
1878 CERT_STORE_PROV_INFO provInfo = { 0 };
1880 info->dwOpenFlags = dwFlags;
1881 info->cryptProv = hCryptProv;
1882 info->memStore = memStore;
1883 info->file = file;
1884 info->dirty = FALSE;
1885 provInfo.cbSize = sizeof(provInfo);
1886 provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1887 sizeof(fileProvFuncs[0]);
1888 provInfo.rgpvStoreProvFunc = fileProvFuncs;
1889 provInfo.hStoreProv = info;
1890 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1891 &provInfo);
1896 TRACE("returning %p\n", store);
1897 return store;
1900 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1901 DWORD dwFlags, const void *pvPara)
1903 HCERTSTORE store = 0;
1904 LPCWSTR fileName = (LPCWSTR)pvPara;
1905 DWORD access, create;
1906 HANDLE file;
1908 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1910 if (!fileName)
1912 SetLastError(ERROR_PATH_NOT_FOUND);
1913 return NULL;
1915 if (!(dwFlags & (CERT_FILE_STORE_COMMIT_ENABLE_FLAG |
1916 CERT_STORE_READONLY_FLAG)))
1918 SetLastError(ERROR_FILE_NOT_FOUND);
1919 return NULL;
1922 access = GENERIC_READ;
1923 if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1924 access |= GENERIC_WRITE;
1925 if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1926 create = CREATE_NEW;
1927 else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1928 create = OPEN_EXISTING;
1929 else
1930 create = OPEN_ALWAYS;
1931 file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1932 FILE_ATTRIBUTE_NORMAL, NULL);
1933 if (file != INVALID_HANDLE_VALUE)
1935 /* FIXME: need to check whether it's a serialized store; if not, fall
1936 * back to a PKCS#7 signed message, then to a single serialized cert.
1938 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1939 file);
1940 CloseHandle(file);
1942 return (PWINECRYPT_CERTSTORE)store;
1945 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1946 DWORD dwFlags, const void *pvPara)
1948 int len;
1949 PWINECRYPT_CERTSTORE ret = NULL;
1951 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1952 debugstr_a((LPCSTR)pvPara));
1954 if (!pvPara)
1956 SetLastError(ERROR_FILE_NOT_FOUND);
1957 return NULL;
1959 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1960 if (len)
1962 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1964 if (storeName)
1966 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1967 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1968 CryptMemFree(storeName);
1971 return ret;
1974 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
1975 DWORD dwFlags, const void *pvPara)
1977 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
1978 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
1979 else
1980 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
1981 debugstr_w((LPCWSTR)pvPara));
1982 return NULL;
1985 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1986 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1987 const void* pvPara)
1989 WINECRYPT_CERTSTORE *hcs;
1990 StoreOpenFunc openFunc = NULL;
1992 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
1993 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1995 if (!HIWORD(lpszStoreProvider))
1997 switch (LOWORD(lpszStoreProvider))
1999 case (int)CERT_STORE_PROV_MEMORY:
2000 openFunc = CRYPT_MemOpenStore;
2001 break;
2002 case (int)CERT_STORE_PROV_FILE:
2003 openFunc = CRYPT_FileOpenStore;
2004 break;
2005 case (int)CERT_STORE_PROV_REG:
2006 openFunc = CRYPT_RegOpenStore;
2007 break;
2008 case (int)CERT_STORE_PROV_FILENAME_A:
2009 openFunc = CRYPT_FileNameOpenStoreA;
2010 break;
2011 case (int)CERT_STORE_PROV_FILENAME_W:
2012 openFunc = CRYPT_FileNameOpenStoreW;
2013 break;
2014 case (int)CERT_STORE_PROV_COLLECTION:
2015 openFunc = CRYPT_CollectionOpenStore;
2016 break;
2017 case (int)CERT_STORE_PROV_SYSTEM_A:
2018 openFunc = CRYPT_SysOpenStoreA;
2019 break;
2020 case (int)CERT_STORE_PROV_SYSTEM_W:
2021 openFunc = CRYPT_SysOpenStoreW;
2022 break;
2023 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
2024 openFunc = CRYPT_SysRegOpenStoreA;
2025 break;
2026 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
2027 openFunc = CRYPT_SysRegOpenStoreW;
2028 break;
2029 case (int)CERT_STORE_PROV_PHYSICAL_W:
2030 openFunc = CRYPT_PhysOpenStoreW;
2031 break;
2032 default:
2033 if (LOWORD(lpszStoreProvider))
2034 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
2037 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
2038 openFunc = CRYPT_MemOpenStore;
2039 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
2040 openFunc = CRYPT_FileOpenStore;
2041 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
2042 openFunc = CRYPT_SysOpenStoreW;
2043 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
2044 openFunc = CRYPT_CollectionOpenStore;
2045 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
2046 openFunc = CRYPT_SysRegOpenStoreW;
2047 else
2049 FIXME("unimplemented type %s\n", lpszStoreProvider);
2050 openFunc = NULL;
2053 if (!openFunc)
2054 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2055 hCryptProv, dwFlags, pvPara);
2056 else
2057 hcs = openFunc(hCryptProv, dwFlags, pvPara);
2058 return (HCERTSTORE)hcs;
2061 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
2062 LPCSTR szSubSystemProtocol)
2064 if (!szSubSystemProtocol)
2066 SetLastError(E_INVALIDARG);
2067 return 0;
2069 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2070 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2073 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
2074 LPCWSTR szSubSystemProtocol)
2076 if (!szSubSystemProtocol)
2078 SetLastError(E_INVALIDARG);
2079 return 0;
2081 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2082 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2085 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2086 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2088 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore,
2089 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2090 return TRUE;
2093 DWORD CertStore_GetAccessState(HCERTSTORE hCertStore)
2095 DWORD state = 0;
2097 if (hCertStore)
2099 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2101 if (store->type != StoreTypeMem &&
2102 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2103 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2105 return state;
2108 #define CertContext_CopyProperties(to, from) \
2109 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2111 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2112 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2113 PCCERT_CONTEXT *ppStoreContext)
2115 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2116 BOOL ret = TRUE;
2117 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2119 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2120 dwAddDisposition, ppStoreContext);
2122 /* Weird case to pass a test */
2123 if (dwAddDisposition == 0)
2125 SetLastError(STATUS_ACCESS_VIOLATION);
2126 return FALSE;
2128 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2130 BYTE hashToAdd[20];
2131 DWORD size = sizeof(hashToAdd);
2133 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2134 hashToAdd, &size);
2135 if (ret)
2137 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2139 existing = CertFindCertificateInStore(hCertStore,
2140 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2141 NULL);
2145 switch (dwAddDisposition)
2147 case CERT_STORE_ADD_ALWAYS:
2148 toAdd = CertDuplicateCertificateContext(pCertContext);
2149 break;
2150 case CERT_STORE_ADD_NEW:
2151 if (existing)
2153 TRACE("found matching certificate, not adding\n");
2154 SetLastError(CRYPT_E_EXISTS);
2155 ret = FALSE;
2157 else
2158 toAdd = CertDuplicateCertificateContext(pCertContext);
2159 break;
2160 case CERT_STORE_ADD_REPLACE_EXISTING:
2161 toAdd = CertDuplicateCertificateContext(pCertContext);
2162 break;
2163 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2164 toAdd = CertDuplicateCertificateContext(pCertContext);
2165 if (existing)
2166 CertContext_CopyProperties(toAdd, existing);
2167 break;
2168 case CERT_STORE_ADD_USE_EXISTING:
2169 if (existing)
2170 CertContext_CopyProperties(existing, pCertContext);
2171 break;
2172 default:
2173 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2174 ret = FALSE;
2177 if (toAdd)
2179 if (store)
2180 ret = store->certs.addContext(store, (void *)toAdd,
2181 (void *)existing, (const void **)ppStoreContext);
2182 else if (ppStoreContext)
2183 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2184 CertFreeCertificateContext(toAdd);
2186 CertFreeCertificateContext(existing);
2188 TRACE("returning %d\n", ret);
2189 return ret;
2192 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2193 PCCERT_CONTEXT pPrev)
2195 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2196 PCCERT_CONTEXT ret;
2198 TRACE("(%p, %p)\n", hCertStore, pPrev);
2199 if (!hCertStore)
2200 ret = NULL;
2201 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2202 ret = NULL;
2203 else
2204 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2205 return ret;
2208 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2210 BOOL ret;
2212 TRACE("(%p)\n", pCertContext);
2214 if (!pCertContext)
2215 ret = TRUE;
2216 else if (!pCertContext->hCertStore)
2218 ret = TRUE;
2219 CertFreeCertificateContext(pCertContext);
2221 else
2223 PWINECRYPT_CERTSTORE hcs =
2224 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2226 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2227 ret = FALSE;
2228 else
2229 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2230 CertFreeCertificateContext(pCertContext);
2232 return ret;
2235 #define CrlContext_CopyProperties(to, from) \
2236 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2238 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2239 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2240 PCCRL_CONTEXT* ppStoreContext)
2242 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2243 BOOL ret = TRUE;
2244 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2246 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2247 dwAddDisposition, ppStoreContext);
2249 /* Weird case to pass a test */
2250 if (dwAddDisposition == 0)
2252 SetLastError(STATUS_ACCESS_VIOLATION);
2253 return FALSE;
2255 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2257 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2258 pCrlContext, NULL);
2261 switch (dwAddDisposition)
2263 case CERT_STORE_ADD_ALWAYS:
2264 toAdd = CertDuplicateCRLContext(pCrlContext);
2265 break;
2266 case CERT_STORE_ADD_NEW:
2267 if (existing)
2269 TRACE("found matching CRL, not adding\n");
2270 SetLastError(CRYPT_E_EXISTS);
2271 ret = FALSE;
2273 else
2274 toAdd = CertDuplicateCRLContext(pCrlContext);
2275 break;
2276 case CERT_STORE_ADD_NEWER:
2277 if (existing)
2279 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2280 &pCrlContext->pCrlInfo->ThisUpdate);
2282 if (newer < 0)
2283 toAdd = CertDuplicateCRLContext(pCrlContext);
2284 else
2286 TRACE("existing CRL is newer, not adding\n");
2287 SetLastError(CRYPT_E_EXISTS);
2288 ret = FALSE;
2291 else
2292 toAdd = CertDuplicateCRLContext(pCrlContext);
2293 break;
2294 case CERT_STORE_ADD_REPLACE_EXISTING:
2295 toAdd = CertDuplicateCRLContext(pCrlContext);
2296 break;
2297 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2298 toAdd = CertDuplicateCRLContext(pCrlContext);
2299 if (existing)
2300 CrlContext_CopyProperties(toAdd, existing);
2301 break;
2302 case CERT_STORE_ADD_USE_EXISTING:
2303 if (existing)
2304 CrlContext_CopyProperties(existing, pCrlContext);
2305 break;
2306 default:
2307 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2308 ret = FALSE;
2311 if (toAdd)
2313 if (store)
2314 ret = store->crls.addContext(store, (void *)toAdd,
2315 (void *)existing, (const void **)ppStoreContext);
2316 else if (ppStoreContext)
2317 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2318 CertFreeCRLContext(toAdd);
2320 CertFreeCRLContext(existing);
2322 TRACE("returning %d\n", ret);
2323 return ret;
2326 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2328 BOOL ret;
2330 TRACE("(%p)\n", pCrlContext);
2332 if (!pCrlContext)
2333 ret = TRUE;
2334 else if (!pCrlContext->hCertStore)
2336 ret = TRUE;
2337 CertFreeCRLContext(pCrlContext);
2339 else
2341 PWINECRYPT_CERTSTORE hcs =
2342 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2344 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2345 ret = FALSE;
2346 else
2347 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2348 CertFreeCRLContext(pCrlContext);
2350 return ret;
2353 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2354 PCCRL_CONTEXT pPrev)
2356 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2357 PCCRL_CONTEXT ret;
2359 TRACE("(%p, %p)\n", hCertStore, pPrev);
2360 if (!hCertStore)
2361 ret = NULL;
2362 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2363 ret = NULL;
2364 else
2365 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2366 return ret;
2369 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2370 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2372 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2373 cbCtlEncoded);
2374 return NULL;
2377 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2378 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2379 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2381 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2382 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2383 ppCtlContext);
2384 return FALSE;
2387 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2388 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2389 PCCTL_CONTEXT* ppStoreContext)
2391 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2392 dwAddDisposition, ppStoreContext);
2393 return TRUE;
2396 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2398 FIXME("(%p): stub\n", pCtlContext );
2399 return pCtlContext;
2402 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2404 FIXME("(%p): stub\n", pCtlContext );
2405 return TRUE;
2408 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2410 FIXME("(%p): stub\n", pCtlContext);
2411 return TRUE;
2414 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2415 PCCTL_CONTEXT pPrev)
2417 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2418 return NULL;
2421 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2423 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2425 TRACE("(%p)\n", hCertStore);
2427 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2428 InterlockedIncrement(&hcs->ref);
2429 return hCertStore;
2432 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2434 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2436 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2438 if( ! hCertStore )
2439 return TRUE;
2441 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2442 return FALSE;
2444 if (InterlockedDecrement(&hcs->ref) == 0)
2446 TRACE("%p's ref count is 0, freeing\n", hcs);
2447 hcs->dwMagic = 0;
2448 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2449 CryptReleaseContext(hcs->cryptProv, 0);
2450 hcs->closeStore(hcs, dwFlags);
2452 else
2453 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2454 return TRUE;
2457 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2458 DWORD dwCtrlType, void const *pvCtrlPara)
2460 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2461 BOOL ret;
2463 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2464 pvCtrlPara);
2466 if (!hcs)
2467 ret = FALSE;
2468 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2469 ret = FALSE;
2470 else
2472 if (hcs->control)
2473 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2474 else
2475 ret = TRUE;
2477 return ret;
2480 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2481 DWORD dwPropId)
2483 FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2484 return 0;
2487 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2488 DWORD dwPropId, void *pvData, DWORD *pcbData)
2490 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2491 return FALSE;
2494 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2495 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2497 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2498 pvData);
2499 return FALSE;
2502 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2503 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2505 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2506 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2507 PWINE_STORE_LIST_ENTRY entry;
2508 BOOL ret;
2510 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2511 dwUpdateFlags, dwPriority);
2513 if (!collection || !sibling)
2514 return TRUE;
2515 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2517 SetLastError(E_INVALIDARG);
2518 return FALSE;
2520 if (collection->hdr.type != StoreTypeCollection)
2522 SetLastError(E_INVALIDARG);
2523 return FALSE;
2525 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2527 SetLastError(E_INVALIDARG);
2528 return FALSE;
2531 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2532 if (entry)
2534 InterlockedIncrement(&sibling->ref);
2535 TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2536 entry->store = sibling;
2537 entry->dwUpdateFlags = dwUpdateFlags;
2538 entry->dwPriority = dwPriority;
2539 list_init(&entry->entry);
2540 TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2541 EnterCriticalSection(&collection->cs);
2542 if (dwPriority)
2544 PWINE_STORE_LIST_ENTRY cursor;
2545 BOOL added = FALSE;
2547 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2548 WINE_STORE_LIST_ENTRY, entry)
2550 if (cursor->dwPriority < dwPriority)
2552 list_add_before(&cursor->entry, &entry->entry);
2553 added = TRUE;
2554 break;
2557 if (!added)
2558 list_add_tail(&collection->stores, &entry->entry);
2560 else
2561 list_add_tail(&collection->stores, &entry->entry);
2562 LeaveCriticalSection(&collection->cs);
2563 ret = TRUE;
2565 else
2566 ret = FALSE;
2567 return ret;
2570 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2571 HCERTSTORE hSiblingStore)
2573 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2574 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2575 PWINE_STORE_LIST_ENTRY store, next;
2577 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2579 if (!collection || !sibling)
2580 return;
2581 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2583 SetLastError(E_INVALIDARG);
2584 return;
2586 if (collection->hdr.type != StoreTypeCollection)
2587 return;
2588 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2590 SetLastError(E_INVALIDARG);
2591 return;
2593 EnterCriticalSection(&collection->cs);
2594 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2595 WINE_STORE_LIST_ENTRY, entry)
2597 if (store->store == sibling)
2599 list_remove(&store->entry);
2600 CertCloseStore(store->store, 0);
2601 CryptMemFree(store);
2602 break;
2605 LeaveCriticalSection(&collection->cs);