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
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.
34 #include "wine/debug.h"
35 #include "wine/list.h"
37 #include "wine/exception.h"
38 #include "crypt32_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
42 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
44 static const WINE_CONTEXT_INTERFACE gCertInterface = {
45 (CreateContextFunc)CertCreateCertificateContext,
46 (AddContextToStoreFunc)CertAddCertificateContextToStore,
47 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
48 (DuplicateContextFunc)CertDuplicateCertificateContext,
49 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
50 (EnumPropertiesFunc)CertEnumCertificateContextProperties,
51 (GetContextPropertyFunc)CertGetCertificateContextProperty,
52 (SetContextPropertyFunc)CertSetCertificateContextProperty,
53 (SerializeElementFunc)CertSerializeCertificateStoreElement,
54 (FreeContextFunc)CertFreeCertificateContext,
55 (DeleteContextFunc)CertDeleteCertificateFromStore,
57 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
59 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
60 (CreateContextFunc)CertCreateCRLContext,
61 (AddContextToStoreFunc)CertAddCRLContextToStore,
62 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
63 (DuplicateContextFunc)CertDuplicateCRLContext,
64 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
65 (EnumPropertiesFunc)CertEnumCRLContextProperties,
66 (GetContextPropertyFunc)CertGetCRLContextProperty,
67 (SetContextPropertyFunc)CertSetCRLContextProperty,
68 (SerializeElementFunc)CertSerializeCRLStoreElement,
69 (FreeContextFunc)CertFreeCRLContext,
70 (DeleteContextFunc)CertDeleteCRLFromStore,
72 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
74 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
75 (CreateContextFunc)CertCreateCTLContext,
76 (AddContextToStoreFunc)CertAddCTLContextToStore,
77 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
78 (DuplicateContextFunc)CertDuplicateCTLContext,
79 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
80 (EnumPropertiesFunc)CertEnumCTLContextProperties,
81 (GetContextPropertyFunc)CertGetCTLContextProperty,
82 (SetContextPropertyFunc)CertSetCTLContextProperty,
83 (SerializeElementFunc)CertSerializeCTLStoreElement,
84 (FreeContextFunc)CertFreeCTLContext,
85 (DeleteContextFunc)CertDeleteCTLFromStore,
87 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
89 struct WINE_CRYPTCERTSTORE;
91 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
92 DWORD dwFlags, const void *pvPara);
94 /* Called to enumerate the next context in a store. */
95 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
97 /* Called to add a context to a store. If toReplace is not NULL,
98 * context replaces toReplace in the store, and access checks should not be
99 * performed. Otherwise context is a new context, and it should only be
100 * added if the store allows it. If ppStoreContext is not NULL, the added
101 * context should be returned in *ppStoreContext.
103 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
104 void *toReplace, const void **ppStoreContext);
106 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
108 typedef struct _CONTEXT_STORE
111 EnumFunc enumContext;
112 DeleteFunc deleteContext;
113 } CONTEXT_STORE, *PCONTEXT_STORE;
115 typedef enum _CertStoreType {
121 /* A cert store is polymorphic through the use of function pointers. A type
122 * is still needed to distinguish collection stores from other types.
123 * On the function pointers:
124 * - closeStore is called when the store's ref count becomes 0
125 * - control is optional, but should be implemented by any store that supports
128 typedef struct WINE_CRYPTCERTSTORE
133 HCRYPTPROV cryptProv;
135 PFN_CERT_STORE_PROV_CLOSE closeStore;
138 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
139 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
141 typedef struct _WINE_MEMSTORE
143 WINECRYPT_CERTSTORE hdr;
144 struct ContextList *certs;
145 struct ContextList *crls;
146 } WINE_MEMSTORE, *PWINE_MEMSTORE;
148 typedef struct _WINE_HASH_TO_DELETE
152 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
154 typedef struct _WINE_REGSTOREINFO
157 HCRYPTPROV cryptProv;
158 PWINECRYPT_CERTSTORE memStore;
162 struct list certsToDelete;
163 struct list crlsToDelete;
164 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
166 typedef struct _WINE_STORE_LIST_ENTRY
168 PWINECRYPT_CERTSTORE store;
172 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
174 typedef struct _WINE_COLLECTIONSTORE
176 WINECRYPT_CERTSTORE hdr;
179 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
181 typedef struct _WINE_PROVIDERSTORE
183 WINECRYPT_CERTSTORE hdr;
184 DWORD dwStoreProvFlags;
185 PWINECRYPT_CERTSTORE memStore;
186 HCERTSTOREPROV hStoreProv;
187 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
188 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
189 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
190 PFN_CERT_STORE_PROV_WRITE_CRL provWriteCrl;
191 PFN_CERT_STORE_PROV_DELETE_CRL provDeleteCrl;
192 PFN_CERT_STORE_PROV_CONTROL provControl;
193 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
195 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
196 DWORD dwFlags, CertStoreType type)
199 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
203 hCryptProv = CRYPT_GetDefaultProvider();
204 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
206 store->cryptProv = hCryptProv;
207 store->dwOpenFlags = dwFlags;
210 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
211 void *toReplace, const void **ppStoreContext)
213 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
214 PCERT_CONTEXT context;
216 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
218 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
221 context->hCertStore = store;
223 *ppStoreContext = CertDuplicateCertificateContext(context);
225 return context ? TRUE : FALSE;
228 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
230 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
233 TRACE("(%p, %p)\n", store, pPrev);
235 ret = ContextList_Enum(ms->certs, pPrev);
237 SetLastError(CRYPT_E_NOT_FOUND);
239 TRACE("returning %p\n", ret);
243 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
245 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
247 ContextList_Delete(ms->certs, pCertContext);
251 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
252 void *toReplace, const void **ppStoreContext)
254 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
255 PCRL_CONTEXT context;
257 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
259 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
262 context->hCertStore = store;
264 *ppStoreContext = CertDuplicateCRLContext(context);
266 return context ? TRUE : FALSE;
269 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
271 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
274 TRACE("(%p, %p)\n", store, pPrev);
276 ret = ContextList_Enum(ms->crls, pPrev);
278 SetLastError(CRYPT_E_NOT_FOUND);
280 TRACE("returning %p\n", ret);
284 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
286 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
288 ContextList_Delete(ms->crls, pCrlContext);
292 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
294 ContextList_Empty(store->certs);
295 ContextList_Empty(store->crls);
298 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
300 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
302 TRACE("(%p, %08lx)\n", store, dwFlags);
304 FIXME("Unimplemented flags: %08lx\n", dwFlags);
306 ContextList_Free(store->certs);
307 ContextList_Free(store->crls);
311 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
312 DWORD dwFlags, const void *pvPara)
314 PWINE_MEMSTORE store;
316 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
318 if (dwFlags & CERT_STORE_DELETE_FLAG)
320 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
325 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
328 memset(store, 0, sizeof(WINE_MEMSTORE));
329 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
330 store->hdr.closeStore = CRYPT_MemCloseStore;
331 store->hdr.certs.addContext = CRYPT_MemAddCert;
332 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
333 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
334 store->hdr.crls.addContext = CRYPT_MemAddCrl;
335 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
336 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
337 store->hdr.control = NULL;
338 store->certs = ContextList_Create(pCertInterface,
339 sizeof(CERT_CONTEXT));
340 store->crls = ContextList_Create(pCRLInterface,
341 sizeof(CRL_CONTEXT));
344 return (PWINECRYPT_CERTSTORE)store;
347 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
349 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
350 PWINE_STORE_LIST_ENTRY entry, next;
352 TRACE("(%p, %08lx)\n", store, dwFlags);
354 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
357 TRACE("closing %p\n", entry);
358 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
361 DeleteCriticalSection(&cs->cs);
365 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
366 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
369 void *ret = Context_CreateLinkContext(contextSize, child,
370 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
373 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
379 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
380 unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
381 void **pChildContext)
384 void *childContext = NULL;
385 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
387 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
388 toReplace, contextSize);
393 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
394 PCONTEXT_STORE contextStore;
396 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
398 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
400 ret = contextStore->addContext(storeEntry->store, context,
401 existingLinked, (const void **)&childContext);
405 PWINE_STORE_LIST_ENTRY entry, next;
407 EnterCriticalSection(&store->cs);
408 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
409 WINE_STORE_LIST_ENTRY, entry)
411 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
413 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
414 (LPBYTE)entry->store + contextStoreOffset);
417 ret = contextStore->addContext(entry->store, context, NULL,
418 (const void **)&childContext);
422 LeaveCriticalSection(&store->cs);
424 SetLastError(E_ACCESSDENIED);
426 *pChildContext = childContext;
430 /* Advances a collection enumeration by one context, if possible, where
432 * - calling the current store's enumeration function once, and returning
433 * the enumerated context if one is returned
434 * - moving to the next store if the current store has no more items, and
435 * recursively calling itself to get the next item.
436 * Returns NULL if the collection contains no more items or on error.
437 * Assumes the collection store's lock is held.
439 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
440 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
441 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
444 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
445 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
448 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
452 /* Ref-counting funny business: "duplicate" (addref) the child, because
453 * the free(pPrev) below can cause the ref count to become negative.
455 child = Context_GetLinkedContext(pPrev, contextSize);
456 contextInterface->duplicate(child);
457 child = contextStore->enumContext(storeEntry->store, child);
458 contextInterface->free(pPrev);
462 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
464 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
469 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
470 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
471 contextInterface, NULL, contextSize);
474 SetLastError(CRYPT_E_NOT_FOUND);
478 TRACE("returning %p\n", ret);
482 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
483 void *toReplace, const void **ppStoreContext)
486 void *childContext = NULL;
487 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
489 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
490 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
491 if (ppStoreContext && childContext)
493 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
494 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
495 PCERT_CONTEXT context =
496 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
497 sizeof(CERT_CONTEXT), TRUE);
500 context->hCertStore = store;
501 *ppStoreContext = context;
503 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
507 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
509 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
512 TRACE("(%p, %p)\n", store, pPrev);
514 EnterCriticalSection(&cs->cs);
517 PWINE_STORE_LIST_ENTRY storeEntry =
518 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
519 sizeof(CERT_CONTEXT));
521 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
522 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
523 sizeof(CERT_CONTEXT));
527 if (!list_empty(&cs->stores))
529 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
530 WINE_STORE_LIST_ENTRY, entry);
532 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
533 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
534 sizeof(CERT_CONTEXT));
538 SetLastError(CRYPT_E_NOT_FOUND);
542 LeaveCriticalSection(&cs->cs);
544 ((PCERT_CONTEXT)ret)->hCertStore = store;
545 TRACE("returning %p\n", ret);
549 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
554 TRACE("(%p, %p)\n", store, pCertContext);
556 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
557 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
561 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
562 void *toReplace, const void **ppStoreContext)
565 void *childContext = NULL;
566 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
568 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
569 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
570 if (ppStoreContext && childContext)
572 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
573 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
574 PCRL_CONTEXT context =
575 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
576 sizeof(CRL_CONTEXT), TRUE);
579 context->hCertStore = store;
580 *ppStoreContext = context;
582 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
586 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
588 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
591 TRACE("(%p, %p)\n", store, pPrev);
593 EnterCriticalSection(&cs->cs);
596 PWINE_STORE_LIST_ENTRY storeEntry =
597 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
598 sizeof(CRL_CONTEXT));
600 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
601 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
602 sizeof(CRL_CONTEXT));
606 if (!list_empty(&cs->stores))
608 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
609 WINE_STORE_LIST_ENTRY, entry);
611 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
612 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
613 sizeof(CRL_CONTEXT));
617 SetLastError(CRYPT_E_NOT_FOUND);
621 LeaveCriticalSection(&cs->cs);
623 ((PCRL_CONTEXT)ret)->hCertStore = store;
624 TRACE("returning %p\n", ret);
628 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
633 TRACE("(%p, %p)\n", store, pCrlContext);
635 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
636 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
640 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
641 DWORD dwFlags, const void *pvPara)
643 PWINE_COLLECTIONSTORE store;
645 if (dwFlags & CERT_STORE_DELETE_FLAG)
647 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
652 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
655 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
656 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
657 StoreTypeCollection);
658 store->hdr.closeStore = CRYPT_CollectionCloseStore;
659 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
660 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
661 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
662 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
663 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
664 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
665 InitializeCriticalSection(&store->cs);
666 list_init(&store->stores);
669 return (PWINECRYPT_CERTSTORE)store;
672 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
674 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
676 TRACE("(%p, %08lx)\n", store, dwFlags);
678 if (store->provCloseStore)
679 store->provCloseStore(store->hStoreProv, dwFlags);
680 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
681 CertCloseStore(store->memStore, dwFlags);
685 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
686 void *toReplace, const void **ppStoreContext)
688 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
691 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
694 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
695 (const void **)ppStoreContext);
698 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
700 SetLastError(ERROR_ACCESS_DENIED);
706 if (ps->provWriteCert)
707 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
708 CERT_STORE_PROV_WRITE_ADD_FLAG);
710 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
711 (const void **)ppStoreContext);
714 /* dirty trick: replace the returned context's hCertStore with
718 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
722 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
724 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
727 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
730 /* same dirty trick: replace the returned context's hCertStore with
733 ((PCERT_CONTEXT)ret)->hCertStore = store;
738 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
740 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
743 TRACE("(%p, %p)\n", store, cert);
745 if (ps->provDeleteCert)
746 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
748 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
752 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
753 void *toReplace, const void **ppStoreContext)
755 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
758 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
761 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
762 (const void **)ppStoreContext);
765 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
767 SetLastError(ERROR_ACCESS_DENIED);
773 if (ps->provWriteCrl)
774 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
775 CERT_STORE_PROV_WRITE_ADD_FLAG);
777 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
778 (const void **)ppStoreContext);
781 /* dirty trick: replace the returned context's hCertStore with
785 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
789 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
791 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
794 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
797 /* same dirty trick: replace the returned context's hCertStore with
800 ((PCERT_CONTEXT)ret)->hCertStore = store;
805 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
807 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
810 TRACE("(%p, %p)\n", store, crl);
812 if (ps->provDeleteCrl)
813 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
815 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
819 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
820 DWORD dwCtrlType, void const *pvCtrlPara)
822 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
825 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
828 if (store->provControl)
829 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
834 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
835 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, PCERT_STORE_PROV_INFO pProvInfo)
837 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
838 sizeof(WINE_PROVIDERSTORE));
842 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
844 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
845 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
847 CertCloseStore(memStore, 0);
848 ret->memStore = NULL;
851 ret->memStore = memStore;
852 ret->hStoreProv = pProvInfo->hStoreProv;
853 ret->hdr.closeStore = CRYPT_ProvCloseStore;
854 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
855 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
856 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
857 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
858 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
859 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
860 ret->hdr.control = CRYPT_ProvControl;
861 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
862 ret->provCloseStore =
863 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
865 ret->provCloseStore = NULL;
866 if (pProvInfo->cStoreProvFunc >
867 CERT_STORE_PROV_WRITE_CERT_FUNC)
868 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
869 CERT_STORE_PROV_WRITE_CERT_FUNC];
871 ret->provWriteCert = NULL;
872 if (pProvInfo->cStoreProvFunc >
873 CERT_STORE_PROV_DELETE_CERT_FUNC)
874 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
875 CERT_STORE_PROV_DELETE_CERT_FUNC];
877 ret->provDeleteCert = NULL;
878 if (pProvInfo->cStoreProvFunc >
879 CERT_STORE_PROV_WRITE_CRL_FUNC)
880 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
881 CERT_STORE_PROV_WRITE_CRL_FUNC];
883 ret->provWriteCert = NULL;
884 if (pProvInfo->cStoreProvFunc >
885 CERT_STORE_PROV_DELETE_CRL_FUNC)
886 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
887 CERT_STORE_PROV_DELETE_CRL_FUNC];
889 ret->provDeleteCert = NULL;
890 if (pProvInfo->cStoreProvFunc >
891 CERT_STORE_PROV_CONTROL_FUNC)
892 ret->provControl = pProvInfo->rgpvStoreProvFunc[
893 CERT_STORE_PROV_CONTROL_FUNC];
895 ret->provControl = NULL;
897 return (PWINECRYPT_CERTSTORE)ret;
900 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
901 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
903 static HCRYPTOIDFUNCSET set = NULL;
904 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
905 HCRYPTOIDFUNCADDR hFunc;
906 PWINECRYPT_CERTSTORE ret = NULL;
909 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
910 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
911 (void **)&provOpenFunc, &hFunc);
914 CERT_STORE_PROV_INFO provInfo = { 0 };
916 provInfo.cbSize = sizeof(provInfo);
917 if (dwFlags & CERT_STORE_DELETE_FLAG)
918 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
919 dwFlags, pvPara, NULL, &provInfo);
924 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
925 CERT_STORE_CREATE_NEW_FLAG, NULL);
928 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
929 dwFlags, pvPara, memStore, &provInfo))
930 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
933 CertCloseStore(memStore, 0);
936 CryptFreeOIDFunctionAddress(hFunc, 0);
939 SetLastError(ERROR_FILE_NOT_FOUND);
943 static void CRYPT_HashToStr(LPBYTE hash, LPWSTR asciiHash)
945 static const WCHAR fmt[] = { '%','0','2','X',0 };
951 for (i = 0; i < 20; i++)
952 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
955 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
957 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
958 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
959 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
961 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store, HKEY key,
966 WCHAR subKeyName[MAX_PATH];
969 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
971 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
977 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
983 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
985 buf = CryptMemAlloc(size);
988 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
995 TRACE("Adding cert with hash %s\n",
996 debugstr_w(subKeyName));
997 context = CRYPT_ReadSerializedElement(buf, size,
998 contextType, &addedType);
1001 const WINE_CONTEXT_INTERFACE *contextInterface;
1006 case CERT_STORE_CERTIFICATE_CONTEXT:
1007 contextInterface = &gCertInterface;
1009 case CERT_STORE_CRL_CONTEXT:
1010 contextInterface = &gCRLInterface;
1012 case CERT_STORE_CTL_CONTEXT:
1013 contextInterface = &gCTLInterface;
1016 contextInterface = NULL;
1018 if (contextInterface)
1020 size = sizeof(hash);
1021 if (contextInterface->getProp(context,
1022 CERT_HASH_PROP_ID, hash, &size))
1024 WCHAR asciiHash[20 * 2 + 1];
1026 CRYPT_HashToStr(hash, asciiHash);
1027 TRACE("comparing %s\n",
1028 debugstr_w(asciiHash));
1029 TRACE("with %s\n", debugstr_w(subKeyName));
1030 if (!lstrcmpW(asciiHash, subKeyName))
1032 TRACE("hash matches, adding\n");
1033 contextInterface->addContextToStore(
1034 store->memStore, context,
1035 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1038 TRACE("hash doesn't match, ignoring\n");
1040 contextInterface->free(context);
1046 RegCloseKey(subKey);
1048 /* Ignore intermediate errors, continue enumerating */
1054 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store)
1056 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
1057 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1058 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1061 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1066 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1070 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1076 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1077 static BOOL CRYPT_WriteSerializedToReg(HKEY key, LPBYTE hash, LPBYTE buf,
1080 WCHAR asciiHash[20 * 2 + 1];
1085 CRYPT_HashToStr(hash, asciiHash);
1086 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1090 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1091 RegCloseKey(subKey);
1103 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1104 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1106 const void *context = NULL;
1110 context = contextInterface->enumContextsInStore(memStore, context);
1114 DWORD hashSize = sizeof(hash);
1116 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1123 ret = contextInterface->serialize(context, 0, NULL, &size);
1125 buf = CryptMemAlloc(size);
1128 ret = contextInterface->serialize(context, 0, buf, &size);
1130 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1137 } while (ret && context != NULL);
1139 contextInterface->free(context);
1143 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1145 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
1146 static const WINE_CONTEXT_INTERFACE *interfaces[] = { &gCertInterface,
1147 &gCRLInterface, &gCTLInterface };
1148 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1153 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1156 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1157 KEY_ALL_ACCESS, NULL, &key, NULL);
1161 if (listToDelete[i])
1163 PWINE_HASH_TO_DELETE toDelete, next;
1164 WCHAR asciiHash[20 * 2 + 1];
1166 EnterCriticalSection(&store->cs);
1167 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1168 WINE_HASH_TO_DELETE, entry)
1172 CRYPT_HashToStr(toDelete->hash, asciiHash);
1173 TRACE("Removing %s\n", debugstr_w(asciiHash));
1174 rc = RegDeleteKeyW(key, asciiHash);
1175 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1180 list_remove(&toDelete->entry);
1181 CryptMemFree(toDelete);
1183 LeaveCriticalSection(&store->cs);
1185 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1198 /* If force is true or the registry store is dirty, writes the contents of the
1199 * store to the registry.
1201 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1205 TRACE("(%p, %d)\n", store, force);
1207 if (store->dirty || force)
1208 ret = CRYPT_RegWriteToReg(store);
1214 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1216 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1218 TRACE("(%p, %08lx)\n", store, dwFlags);
1220 FIXME("Unimplemented flags: %08lx\n", dwFlags);
1222 CRYPT_RegFlushStore(store, FALSE);
1223 RegCloseKey(store->key);
1224 DeleteCriticalSection(&store->cs);
1225 CryptMemFree(store);
1228 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1229 const void *context, DWORD dwFlags)
1233 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1235 store->dirty = TRUE;
1243 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1244 struct list *deleteList, const void *context,
1245 PCWINE_CONTEXT_INTERFACE contextInterface)
1249 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1251 SetLastError(ERROR_ACCESS_DENIED);
1256 PWINE_HASH_TO_DELETE toDelete =
1257 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1261 DWORD size = sizeof(toDelete->hash);
1263 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1264 toDelete->hash, &size);
1267 EnterCriticalSection(&store->cs);
1268 list_add_tail(deleteList, &toDelete->entry);
1269 LeaveCriticalSection(&store->cs);
1273 CryptMemFree(toDelete);
1280 store->dirty = TRUE;
1285 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1286 PCCERT_CONTEXT cert, DWORD dwFlags)
1288 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1290 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
1292 return CRYPT_RegWriteContext(store, cert, dwFlags);
1295 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1296 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1298 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1300 TRACE("(%p, %p, %08lx)\n", store, pCertContext, dwFlags);
1302 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1306 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1307 PCCRL_CONTEXT crl, DWORD dwFlags)
1309 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1311 TRACE("(%p, %p, %ld)\n", hCertStore, crl, dwFlags);
1313 return CRYPT_RegWriteContext(store, crl, dwFlags);
1316 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1317 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1319 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1321 TRACE("(%p, %p, %08lx)\n", store, pCrlContext, dwFlags);
1323 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1327 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1328 DWORD dwCtrlType, void const *pvCtrlPara)
1330 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1333 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
1338 case CERT_STORE_CTRL_RESYNC:
1339 CRYPT_RegFlushStore(store, FALSE);
1340 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1341 CRYPT_RegReadFromReg(store);
1344 case CERT_STORE_CTRL_COMMIT:
1345 ret = CRYPT_RegFlushStore(store,
1346 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1349 FIXME("%ld: stub\n", dwCtrlType);
1355 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1356 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1358 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1359 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1362 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1364 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1367 /* Find how many subkeys there are */
1368 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1369 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1373 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1375 /* Name too big: alloc a buffer for it */
1376 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1380 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1383 /* Recursively delete all the subkeys */
1384 for (i = 0; i < dwKeyCount && !dwRet; i++)
1386 dwSize = dwMaxSubkeyLen;
1387 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1390 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1393 if (lpszName != szNameBuf)
1395 /* Free buffer if allocated */
1396 CryptMemFree(lpszName);
1401 RegCloseKey(hSubKey);
1403 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1408 static void *regProvFuncs[] = {
1409 CRYPT_RegCloseStore,
1410 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1412 CRYPT_RegDeleteCert,
1413 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1414 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1417 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1418 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1419 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1420 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1421 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1425 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1426 DWORD dwFlags, const void *pvPara)
1428 PWINECRYPT_CERTSTORE store = NULL;
1430 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
1432 if (dwFlags & CERT_STORE_DELETE_FLAG)
1434 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1436 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1437 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1438 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1439 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1440 if (rc == ERROR_NO_MORE_ITEMS)
1448 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1449 GetCurrentProcess(), (LPHANDLE)&key,
1450 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1453 PWINECRYPT_CERTSTORE memStore;
1455 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1458 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1459 sizeof(WINE_REGSTOREINFO));
1463 CERT_STORE_PROV_INFO provInfo = { 0 };
1465 regInfo->dwOpenFlags = dwFlags;
1466 regInfo->cryptProv = hCryptProv;
1467 regInfo->memStore = memStore;
1469 InitializeCriticalSection(®Info->cs);
1470 list_init(®Info->certsToDelete);
1471 list_init(®Info->crlsToDelete);
1472 CRYPT_RegReadFromReg(regInfo);
1473 regInfo->dirty = FALSE;
1474 provInfo.cbSize = sizeof(provInfo);
1475 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1476 sizeof(regProvFuncs[0]);
1477 provInfo.rgpvStoreProvFunc = regProvFuncs;
1478 provInfo.hStoreProv = regInfo;
1479 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1485 TRACE("returning %p\n", store);
1489 /* FIXME: this isn't complete for the Root store, in which the top-level
1490 * self-signed CA certs reside. Adding a cert to the Root store should present
1491 * the user with a dialog indicating the consequences of doing so, and asking
1492 * the user to confirm whether the cert should be added.
1494 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1495 DWORD dwFlags, const void *pvPara)
1497 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1498 LPCWSTR storeName = (LPCWSTR)pvPara;
1500 PWINECRYPT_CERTSTORE store = NULL;
1505 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1506 debugstr_w((LPCWSTR)pvPara));
1510 SetLastError(E_INVALIDARG);
1515 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1517 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1518 root = HKEY_LOCAL_MACHINE;
1519 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1521 case CERT_SYSTEM_STORE_CURRENT_USER:
1522 root = HKEY_CURRENT_USER;
1523 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1525 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1526 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1527 * SystemCertificates
1529 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1530 debugstr_w(storeName));
1532 case CERT_SYSTEM_STORE_SERVICES:
1533 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1534 * SystemCertificates
1536 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1537 debugstr_w(storeName));
1539 case CERT_SYSTEM_STORE_USERS:
1540 /* hku\user sid\Software\Microsoft\SystemCertificates */
1541 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1542 debugstr_w(storeName));
1544 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1545 root = HKEY_CURRENT_USER;
1546 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1548 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1549 root = HKEY_LOCAL_MACHINE;
1550 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1552 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1553 /* hklm\Software\Microsoft\EnterpriseCertificates */
1554 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1555 debugstr_w(storeName));
1558 SetLastError(E_INVALIDARG);
1562 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1568 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1571 wsprintfW(storePath, fmt, base, storeName);
1572 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1573 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1578 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1580 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1581 disp == REG_OPENED_EXISTING_KEY)
1584 rc = ERROR_FILE_EXISTS;
1589 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1594 CryptMemFree(storePath);
1599 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1600 DWORD dwFlags, const void *pvPara)
1603 PWINECRYPT_CERTSTORE ret = NULL;
1605 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1606 debugstr_a((LPCSTR)pvPara));
1610 SetLastError(ERROR_FILE_NOT_FOUND);
1613 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1616 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1620 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1621 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1622 CryptMemFree(storeName);
1628 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1629 DWORD dwFlags, const void *pvPara)
1631 HCERTSTORE store = 0;
1634 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1635 debugstr_w((LPCWSTR)pvPara));
1639 SetLastError(ERROR_FILE_NOT_FOUND);
1642 /* This returns a different error than system registry stores if the
1643 * location is invalid.
1645 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1647 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1648 case CERT_SYSTEM_STORE_CURRENT_USER:
1649 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1650 case CERT_SYSTEM_STORE_SERVICES:
1651 case CERT_SYSTEM_STORE_USERS:
1652 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1653 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1654 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1658 SetLastError(ERROR_FILE_NOT_FOUND);
1663 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1664 0, hCryptProv, dwFlags, pvPara);
1668 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1669 CERT_STORE_CREATE_NEW_FLAG, NULL);
1670 CertAddStoreToCollection(store, regStore,
1671 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1672 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1673 CertCloseStore(regStore, 0);
1674 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1677 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1678 CERT_SYSTEM_STORE_CURRENT_USER)
1680 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1681 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1682 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1683 hCryptProv, dwFlags, pvPara);
1686 CertAddStoreToCollection(store, regStore,
1687 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1688 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1689 CertCloseStore(regStore, 0);
1694 return (PWINECRYPT_CERTSTORE)store;
1697 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1698 DWORD dwFlags, const void *pvPara)
1701 PWINECRYPT_CERTSTORE ret = NULL;
1703 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1704 debugstr_a((LPCSTR)pvPara));
1708 SetLastError(ERROR_FILE_NOT_FOUND);
1711 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1714 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1718 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1719 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1720 CryptMemFree(storeName);
1726 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1727 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1730 WINECRYPT_CERTSTORE *hcs;
1731 StoreOpenFunc openFunc = NULL;
1733 TRACE("(%s, %08lx, %08lx, %08lx, %p)\n", debugstr_a(lpszStoreProvider),
1734 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1736 if (!HIWORD(lpszStoreProvider))
1738 switch (LOWORD(lpszStoreProvider))
1740 case (int)CERT_STORE_PROV_MEMORY:
1741 openFunc = CRYPT_MemOpenStore;
1743 case (int)CERT_STORE_PROV_REG:
1744 openFunc = CRYPT_RegOpenStore;
1746 case (int)CERT_STORE_PROV_COLLECTION:
1747 openFunc = CRYPT_CollectionOpenStore;
1749 case (int)CERT_STORE_PROV_SYSTEM_A:
1750 openFunc = CRYPT_SysOpenStoreA;
1752 case (int)CERT_STORE_PROV_SYSTEM_W:
1753 openFunc = CRYPT_SysOpenStoreW;
1755 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
1756 openFunc = CRYPT_SysRegOpenStoreA;
1758 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
1759 openFunc = CRYPT_SysRegOpenStoreW;
1762 if (LOWORD(lpszStoreProvider))
1763 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
1766 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
1767 openFunc = CRYPT_MemOpenStore;
1768 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
1769 openFunc = CRYPT_SysOpenStoreW;
1770 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
1771 openFunc = CRYPT_CollectionOpenStore;
1772 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
1773 openFunc = CRYPT_SysRegOpenStoreW;
1776 FIXME("unimplemented type %s\n", lpszStoreProvider);
1781 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
1782 hCryptProv, dwFlags, pvPara);
1784 hcs = openFunc(hCryptProv, dwFlags, pvPara);
1785 return (HCERTSTORE)hcs;
1788 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
1789 LPCSTR szSubSystemProtocol)
1791 if (!szSubSystemProtocol)
1793 SetLastError(E_INVALIDARG);
1796 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
1797 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1800 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
1801 LPCWSTR szSubSystemProtocol)
1803 if (!szSubSystemProtocol)
1805 SetLastError(E_INVALIDARG);
1808 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
1809 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1812 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
1813 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
1815 FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore,
1816 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
1820 DWORD CertStore_GetAccessState(HCERTSTORE hCertStore)
1826 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
1828 if (store->type != StoreTypeMem &&
1829 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
1830 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
1835 #define CertContext_CopyProperties(to, from) \
1836 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
1838 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
1839 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
1840 PCCERT_CONTEXT *ppStoreContext)
1842 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
1844 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
1846 TRACE("(%p, %p, %08lx, %p)\n", hCertStore, pCertContext,
1847 dwAddDisposition, ppStoreContext);
1849 /* Weird case to pass a test */
1850 if (dwAddDisposition == 0)
1852 SetLastError(STATUS_ACCESS_VIOLATION);
1855 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
1858 DWORD size = sizeof(hashToAdd);
1860 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
1864 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
1866 existing = CertFindCertificateInStore(hCertStore,
1867 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
1872 switch (dwAddDisposition)
1874 case CERT_STORE_ADD_ALWAYS:
1875 toAdd = CertDuplicateCertificateContext(pCertContext);
1877 case CERT_STORE_ADD_NEW:
1880 TRACE("found matching certificate, not adding\n");
1881 SetLastError(CRYPT_E_EXISTS);
1885 toAdd = CertDuplicateCertificateContext(pCertContext);
1887 case CERT_STORE_ADD_REPLACE_EXISTING:
1888 toAdd = CertDuplicateCertificateContext(pCertContext);
1890 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
1891 toAdd = CertDuplicateCertificateContext(pCertContext);
1893 CertContext_CopyProperties(toAdd, existing);
1895 case CERT_STORE_ADD_USE_EXISTING:
1897 CertContext_CopyProperties(existing, pCertContext);
1900 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition);
1907 ret = store->certs.addContext(store, (void *)toAdd,
1908 (void *)existing, (const void **)ppStoreContext);
1909 else if (ppStoreContext)
1910 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
1911 CertFreeCertificateContext(toAdd);
1913 CertFreeCertificateContext(existing);
1915 TRACE("returning %d\n", ret);
1919 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
1920 PCCERT_CONTEXT pPrev)
1922 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
1925 TRACE("(%p, %p)\n", hCertStore, pPrev);
1928 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
1931 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
1935 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
1939 TRACE("(%p)\n", pCertContext);
1943 else if (!pCertContext->hCertStore)
1946 CertFreeCertificateContext(pCertContext);
1950 PWINECRYPT_CERTSTORE hcs =
1951 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
1953 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
1956 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
1957 CertFreeCertificateContext(pCertContext);
1962 #define CrlContext_CopyProperties(to, from) \
1963 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
1965 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
1966 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
1967 PCCRL_CONTEXT* ppStoreContext)
1969 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
1971 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
1973 TRACE("(%p, %p, %08lx, %p)\n", hCertStore, pCrlContext,
1974 dwAddDisposition, ppStoreContext);
1976 /* Weird case to pass a test */
1977 if (dwAddDisposition == 0)
1979 SetLastError(STATUS_ACCESS_VIOLATION);
1982 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
1984 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
1988 switch (dwAddDisposition)
1990 case CERT_STORE_ADD_ALWAYS:
1991 toAdd = CertDuplicateCRLContext(pCrlContext);
1993 case CERT_STORE_ADD_NEW:
1996 TRACE("found matching CRL, not adding\n");
1997 SetLastError(CRYPT_E_EXISTS);
2001 toAdd = CertDuplicateCRLContext(pCrlContext);
2003 case CERT_STORE_ADD_NEWER:
2006 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2007 &pCrlContext->pCrlInfo->ThisUpdate);
2010 toAdd = CertDuplicateCRLContext(pCrlContext);
2013 TRACE("existing CRL is newer, not adding\n");
2014 SetLastError(CRYPT_E_EXISTS);
2019 toAdd = CertDuplicateCRLContext(pCrlContext);
2021 case CERT_STORE_ADD_REPLACE_EXISTING:
2022 toAdd = CertDuplicateCRLContext(pCrlContext);
2024 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2025 toAdd = CertDuplicateCRLContext(pCrlContext);
2027 CrlContext_CopyProperties(toAdd, existing);
2029 case CERT_STORE_ADD_USE_EXISTING:
2031 CrlContext_CopyProperties(existing, pCrlContext);
2034 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition);
2041 ret = store->crls.addContext(store, (void *)toAdd,
2042 (void *)existing, (const void **)ppStoreContext);
2043 else if (ppStoreContext)
2044 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2045 CertFreeCRLContext(toAdd);
2047 CertFreeCRLContext(existing);
2049 TRACE("returning %d\n", ret);
2053 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2057 TRACE("(%p)\n", pCrlContext);
2061 else if (!pCrlContext->hCertStore)
2064 CertFreeCRLContext(pCrlContext);
2068 PWINECRYPT_CERTSTORE hcs =
2069 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2071 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2074 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2075 CertFreeCRLContext(pCrlContext);
2080 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2081 PCCRL_CONTEXT pPrev)
2083 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2086 TRACE("(%p, %p)\n", hCertStore, pPrev);
2089 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2092 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2096 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2097 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2099 FIXME("(%08lx, %p, %08lx): stub\n", dwCertEncodingType, pbCtlEncoded,
2104 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2105 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2106 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2108 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2109 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2114 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2115 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2116 PCCTL_CONTEXT* ppStoreContext)
2118 FIXME("(%p, %p, %08lx, %p): stub\n", hCertStore, pCtlContext,
2119 dwAddDisposition, ppStoreContext);
2123 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2125 FIXME("(%p): stub\n", pCtlContext );
2129 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2131 FIXME("(%p): stub\n", pCtlContext );
2135 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2137 FIXME("(%p): stub\n", pCtlContext);
2141 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2142 PCCTL_CONTEXT pPrev)
2144 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2148 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2150 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2152 TRACE("(%p)\n", hCertStore);
2154 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2155 InterlockedIncrement(&hcs->ref);
2159 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2161 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2163 TRACE("(%p, %08lx)\n", hCertStore, dwFlags);
2168 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2171 if (InterlockedDecrement(&hcs->ref) == 0)
2173 TRACE("%p's ref count is 0, freeing\n", hcs);
2175 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2176 CryptReleaseContext(hcs->cryptProv, 0);
2177 hcs->closeStore(hcs, dwFlags);
2180 TRACE("%p's ref count is %ld\n", hcs, hcs->ref);
2184 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2185 DWORD dwCtrlType, void const *pvCtrlPara)
2187 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2190 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
2195 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2200 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2207 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2210 FIXME("(%p, %ld): stub\n", pCTLContext, dwPropId);
2214 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2215 DWORD dwPropId, void *pvData, DWORD *pcbData)
2217 FIXME("(%p, %ld, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2221 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2222 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2224 FIXME("(%p, %ld, %08lx, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2229 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2230 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2232 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2233 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2234 PWINE_STORE_LIST_ENTRY entry;
2237 TRACE("(%p, %p, %08lx, %ld)\n", hCollectionStore, hSiblingStore,
2238 dwUpdateFlags, dwPriority);
2240 if (!collection || !sibling)
2242 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2244 SetLastError(E_INVALIDARG);
2247 if (collection->hdr.type != StoreTypeCollection)
2249 SetLastError(E_INVALIDARG);
2252 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2254 SetLastError(E_INVALIDARG);
2258 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2261 InterlockedIncrement(&sibling->ref);
2262 TRACE("sibling %p's ref count is %ld\n", sibling, sibling->ref);
2263 entry->store = sibling;
2264 entry->dwUpdateFlags = dwUpdateFlags;
2265 entry->dwPriority = dwPriority;
2266 list_init(&entry->entry);
2267 TRACE("%p: adding %p, priority %ld\n", collection, entry, dwPriority);
2268 EnterCriticalSection(&collection->cs);
2271 PWINE_STORE_LIST_ENTRY cursor;
2274 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2275 WINE_STORE_LIST_ENTRY, entry)
2277 if (cursor->dwPriority < dwPriority)
2279 list_add_before(&cursor->entry, &entry->entry);
2285 list_add_tail(&collection->stores, &entry->entry);
2288 list_add_tail(&collection->stores, &entry->entry);
2289 LeaveCriticalSection(&collection->cs);
2297 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2298 HCERTSTORE hSiblingStore)
2300 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2301 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2302 PWINE_STORE_LIST_ENTRY store, next;
2304 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2306 if (!collection || !sibling)
2308 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2310 SetLastError(E_INVALIDARG);
2313 if (collection->hdr.type != StoreTypeCollection)
2315 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2317 SetLastError(E_INVALIDARG);
2320 EnterCriticalSection(&collection->cs);
2321 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2322 WINE_STORE_LIST_ENTRY, entry)
2324 if (store->store == sibling)
2326 list_remove(&store->entry);
2327 CertCloseStore(store->store, 0);
2328 CryptMemFree(store);
2332 LeaveCriticalSection(&collection->cs);