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.
27 #include "wine/port.h"
37 #include "wine/debug.h"
38 #include "wine/list.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
114 EnumFunc enumContext;
115 DeleteFunc deleteContext;
116 } CONTEXT_STORE, *PCONTEXT_STORE;
118 typedef enum _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
131 typedef struct WINE_CRYPTCERTSTORE
136 HCRYPTPROV cryptProv;
138 PFN_CERT_STORE_PROV_CLOSE closeStore;
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
155 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
157 typedef struct _WINE_REGSTOREINFO
160 HCRYPTPROV cryptProv;
161 PWINECRYPT_CERTSTORE memStore;
165 struct list certsToDelete;
166 struct list crlsToDelete;
167 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
169 typedef struct _WINE_FILESTOREINFO
172 HCRYPTPROV cryptProv;
173 PWINECRYPT_CERTSTORE memStore;
176 } WINE_FILESTOREINFO, *PWINE_FILESTOREINFO;
178 typedef struct _WINE_STORE_LIST_ENTRY
180 PWINECRYPT_CERTSTORE store;
184 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
186 typedef struct _WINE_COLLECTIONSTORE
188 WINECRYPT_CERTSTORE hdr;
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)
211 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
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);
233 context->hCertStore = store;
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;
245 TRACE("(%p, %p)\n", store, pPrev);
247 ret = ContextList_Enum(ms->certs, pPrev);
249 SetLastError(CRYPT_E_NOT_FOUND);
251 TRACE("returning %p\n", ret);
255 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
257 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
259 ContextList_Delete(ms->certs, pCertContext);
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);
274 context->hCertStore = store;
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;
286 TRACE("(%p, %p)\n", store, pPrev);
288 ret = ContextList_Enum(ms->crls, pPrev);
290 SetLastError(CRYPT_E_NOT_FOUND);
292 TRACE("returning %p\n", ret);
296 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
298 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
300 ContextList_Delete(ms->crls, pCrlContext);
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);
316 FIXME("Unimplemented flags: %08x\n", dwFlags);
318 ContextList_Free(store->certs);
319 ContextList_Free(store->crls);
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);
337 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
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,
369 TRACE("closing %p\n", entry);
370 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
373 DeleteCriticalSection(&cs->cs);
377 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
378 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
381 void *ret = Context_CreateLinkContext(contextSize, child,
382 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
385 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
391 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
392 unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
393 void **pChildContext)
396 void *childContext = NULL;
397 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
399 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
400 toReplace, contextSize);
405 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
406 PCONTEXT_STORE contextStore;
408 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
410 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
412 ret = contextStore->addContext(storeEntry->store, context,
413 existingLinked, (const void **)&childContext);
417 PWINE_STORE_LIST_ENTRY entry, next;
419 EnterCriticalSection(&store->cs);
420 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
421 WINE_STORE_LIST_ENTRY, entry)
423 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
425 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
426 (LPBYTE)entry->store + contextStoreOffset);
429 ret = contextStore->addContext(entry->store, context, NULL,
430 (const void **)&childContext);
434 LeaveCriticalSection(&store->cs);
436 SetLastError(E_ACCESSDENIED);
438 *pChildContext = childContext;
442 /* Advances a collection enumeration by one context, if possible, where
444 * - calling the current store's enumeration function once, and returning
445 * the enumerated context if one is returned
446 * - moving to the next store if the current store has no more items, and
447 * recursively calling itself to get the next item.
448 * Returns NULL if the collection contains no more items or on error.
449 * Assumes the collection store's lock is held.
451 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
452 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
453 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
456 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
457 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
460 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
464 /* Ref-counting funny business: "duplicate" (addref) the child, because
465 * the free(pPrev) below can cause the ref count to become negative.
467 child = Context_GetLinkedContext(pPrev, contextSize);
468 contextInterface->duplicate(child);
469 child = contextStore->enumContext(storeEntry->store, child);
470 contextInterface->free(pPrev);
474 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
476 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
481 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
482 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
483 contextInterface, NULL, contextSize);
486 SetLastError(CRYPT_E_NOT_FOUND);
490 TRACE("returning %p\n", ret);
494 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
495 void *toReplace, const void **ppStoreContext)
498 void *childContext = NULL;
499 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
501 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
502 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
503 if (ppStoreContext && childContext)
505 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
506 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
507 PCERT_CONTEXT context =
508 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
509 sizeof(CERT_CONTEXT), TRUE);
512 context->hCertStore = store;
513 *ppStoreContext = context;
515 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
519 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
521 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
524 TRACE("(%p, %p)\n", store, pPrev);
526 EnterCriticalSection(&cs->cs);
529 PWINE_STORE_LIST_ENTRY storeEntry =
530 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
531 sizeof(CERT_CONTEXT));
533 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
534 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
535 sizeof(CERT_CONTEXT));
539 if (!list_empty(&cs->stores))
541 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
542 WINE_STORE_LIST_ENTRY, entry);
544 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
545 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
546 sizeof(CERT_CONTEXT));
550 SetLastError(CRYPT_E_NOT_FOUND);
554 LeaveCriticalSection(&cs->cs);
556 ((PCERT_CONTEXT)ret)->hCertStore = store;
557 TRACE("returning %p\n", ret);
561 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
566 TRACE("(%p, %p)\n", store, pCertContext);
568 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
569 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
573 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
574 void *toReplace, const void **ppStoreContext)
577 void *childContext = NULL;
578 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
580 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
581 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
582 if (ppStoreContext && childContext)
584 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
585 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
586 PCRL_CONTEXT context =
587 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
588 sizeof(CRL_CONTEXT), TRUE);
591 context->hCertStore = store;
592 *ppStoreContext = context;
594 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
598 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
600 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
603 TRACE("(%p, %p)\n", store, pPrev);
605 EnterCriticalSection(&cs->cs);
608 PWINE_STORE_LIST_ENTRY storeEntry =
609 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
610 sizeof(CRL_CONTEXT));
612 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
613 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
614 sizeof(CRL_CONTEXT));
618 if (!list_empty(&cs->stores))
620 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
621 WINE_STORE_LIST_ENTRY, entry);
623 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
624 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
625 sizeof(CRL_CONTEXT));
629 SetLastError(CRYPT_E_NOT_FOUND);
633 LeaveCriticalSection(&cs->cs);
635 ((PCRL_CONTEXT)ret)->hCertStore = store;
636 TRACE("returning %p\n", ret);
640 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
645 TRACE("(%p, %p)\n", store, pCrlContext);
647 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
648 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
652 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
653 DWORD dwFlags, const void *pvPara)
655 PWINE_COLLECTIONSTORE store;
657 if (dwFlags & CERT_STORE_DELETE_FLAG)
659 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
664 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
667 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
668 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
669 StoreTypeCollection);
670 store->hdr.closeStore = CRYPT_CollectionCloseStore;
671 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
672 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
673 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
674 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
675 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
676 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
677 InitializeCriticalSection(&store->cs);
678 list_init(&store->stores);
681 return (PWINECRYPT_CERTSTORE)store;
684 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
686 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
688 TRACE("(%p, %08x)\n", store, dwFlags);
690 if (store->provCloseStore)
691 store->provCloseStore(store->hStoreProv, dwFlags);
692 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
693 CertCloseStore(store->memStore, dwFlags);
697 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
698 void *toReplace, const void **ppStoreContext)
700 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
703 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
706 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
707 (const void **)ppStoreContext);
711 if (ps->provWriteCert)
712 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
713 CERT_STORE_PROV_WRITE_ADD_FLAG);
715 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
716 (const void **)ppStoreContext);
718 /* dirty trick: replace the returned context's hCertStore with
722 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
726 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
728 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
731 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
734 /* same dirty trick: replace the returned context's hCertStore with
737 ((PCERT_CONTEXT)ret)->hCertStore = store;
742 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
744 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
747 TRACE("(%p, %p)\n", store, cert);
749 if (ps->provDeleteCert)
750 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
752 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
756 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
757 void *toReplace, const void **ppStoreContext)
759 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
762 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
765 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
766 (const void **)ppStoreContext);
769 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
771 SetLastError(ERROR_ACCESS_DENIED);
777 if (ps->provWriteCrl)
778 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
779 CERT_STORE_PROV_WRITE_ADD_FLAG);
781 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
782 (const void **)ppStoreContext);
785 /* dirty trick: replace the returned context's hCertStore with
789 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
793 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
795 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
798 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
801 /* same dirty trick: replace the returned context's hCertStore with
804 ((PCERT_CONTEXT)ret)->hCertStore = store;
809 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
811 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
814 TRACE("(%p, %p)\n", store, crl);
816 if (ps->provDeleteCrl)
817 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
819 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
823 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
824 DWORD dwCtrlType, void const *pvCtrlPara)
826 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
829 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
832 if (store->provControl)
833 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
838 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
839 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, PCERT_STORE_PROV_INFO pProvInfo)
841 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
842 sizeof(WINE_PROVIDERSTORE));
846 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
848 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
849 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
851 CertCloseStore(memStore, 0);
852 ret->memStore = NULL;
855 ret->memStore = memStore;
856 ret->hStoreProv = pProvInfo->hStoreProv;
857 ret->hdr.closeStore = CRYPT_ProvCloseStore;
858 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
859 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
860 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
861 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
862 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
863 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
864 ret->hdr.control = CRYPT_ProvControl;
865 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
866 ret->provCloseStore =
867 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
869 ret->provCloseStore = NULL;
870 if (pProvInfo->cStoreProvFunc >
871 CERT_STORE_PROV_WRITE_CERT_FUNC)
872 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
873 CERT_STORE_PROV_WRITE_CERT_FUNC];
875 ret->provWriteCert = NULL;
876 if (pProvInfo->cStoreProvFunc >
877 CERT_STORE_PROV_DELETE_CERT_FUNC)
878 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
879 CERT_STORE_PROV_DELETE_CERT_FUNC];
881 ret->provDeleteCert = NULL;
882 if (pProvInfo->cStoreProvFunc >
883 CERT_STORE_PROV_WRITE_CRL_FUNC)
884 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
885 CERT_STORE_PROV_WRITE_CRL_FUNC];
887 ret->provWriteCert = NULL;
888 if (pProvInfo->cStoreProvFunc >
889 CERT_STORE_PROV_DELETE_CRL_FUNC)
890 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
891 CERT_STORE_PROV_DELETE_CRL_FUNC];
893 ret->provDeleteCert = NULL;
894 if (pProvInfo->cStoreProvFunc >
895 CERT_STORE_PROV_CONTROL_FUNC)
896 ret->provControl = pProvInfo->rgpvStoreProvFunc[
897 CERT_STORE_PROV_CONTROL_FUNC];
899 ret->provControl = NULL;
901 return (PWINECRYPT_CERTSTORE)ret;
904 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
905 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
907 static HCRYPTOIDFUNCSET set = NULL;
908 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
909 HCRYPTOIDFUNCADDR hFunc;
910 PWINECRYPT_CERTSTORE ret = NULL;
913 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
914 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
915 (void **)&provOpenFunc, &hFunc);
918 CERT_STORE_PROV_INFO provInfo = { 0 };
920 provInfo.cbSize = sizeof(provInfo);
921 if (dwFlags & CERT_STORE_DELETE_FLAG)
922 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
923 dwFlags, pvPara, NULL, &provInfo);
928 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
929 CERT_STORE_CREATE_NEW_FLAG, NULL);
932 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
933 dwFlags, pvPara, memStore, &provInfo))
934 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
937 CertCloseStore(memStore, 0);
940 CryptFreeOIDFunctionAddress(hFunc, 0);
943 SetLastError(ERROR_FILE_NOT_FOUND);
947 static void CRYPT_HashToStr(LPBYTE hash, LPWSTR asciiHash)
949 static const WCHAR fmt[] = { '%','0','2','X',0 };
955 for (i = 0; i < 20; i++)
956 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
959 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
961 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
962 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
963 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
965 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store, HKEY key,
970 WCHAR subKeyName[MAX_PATH];
973 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
975 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
981 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
987 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
989 buf = CryptMemAlloc(size);
992 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
999 TRACE("Adding cert with hash %s\n",
1000 debugstr_w(subKeyName));
1001 context = CRYPT_ReadSerializedElement(buf, size,
1002 contextType, &addedType);
1005 const WINE_CONTEXT_INTERFACE *contextInterface;
1010 case CERT_STORE_CERTIFICATE_CONTEXT:
1011 contextInterface = &gCertInterface;
1013 case CERT_STORE_CRL_CONTEXT:
1014 contextInterface = &gCRLInterface;
1016 case CERT_STORE_CTL_CONTEXT:
1017 contextInterface = &gCTLInterface;
1020 contextInterface = NULL;
1022 if (contextInterface)
1024 size = sizeof(hash);
1025 if (contextInterface->getProp(context,
1026 CERT_HASH_PROP_ID, hash, &size))
1028 WCHAR asciiHash[20 * 2 + 1];
1030 CRYPT_HashToStr(hash, asciiHash);
1031 TRACE("comparing %s\n",
1032 debugstr_w(asciiHash));
1033 TRACE("with %s\n", debugstr_w(subKeyName));
1034 if (!lstrcmpW(asciiHash, subKeyName))
1036 TRACE("hash matches, adding\n");
1037 contextInterface->addContextToStore(
1038 store->memStore, context,
1039 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1042 TRACE("hash doesn't match, ignoring\n");
1044 contextInterface->free(context);
1050 RegCloseKey(subKey);
1052 /* Ignore intermediate errors, continue enumerating */
1058 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store)
1060 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1061 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1062 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1065 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1070 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1074 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1080 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1081 static BOOL CRYPT_WriteSerializedToReg(HKEY key, LPBYTE hash, LPBYTE buf,
1084 WCHAR asciiHash[20 * 2 + 1];
1089 CRYPT_HashToStr(hash, asciiHash);
1090 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1094 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1095 RegCloseKey(subKey);
1107 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1108 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1110 const void *context = NULL;
1114 context = contextInterface->enumContextsInStore(memStore, context);
1118 DWORD hashSize = sizeof(hash);
1120 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1127 ret = contextInterface->serialize(context, 0, NULL, &size);
1129 buf = CryptMemAlloc(size);
1132 ret = contextInterface->serialize(context, 0, buf, &size);
1134 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1141 } while (ret && context != NULL);
1143 contextInterface->free(context);
1147 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1149 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1150 static const WINE_CONTEXT_INTERFACE * const interfaces[] = { &gCertInterface,
1151 &gCRLInterface, &gCTLInterface };
1152 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1157 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1160 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1161 KEY_ALL_ACCESS, NULL, &key, NULL);
1165 if (listToDelete[i])
1167 PWINE_HASH_TO_DELETE toDelete, next;
1168 WCHAR asciiHash[20 * 2 + 1];
1170 EnterCriticalSection(&store->cs);
1171 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1172 WINE_HASH_TO_DELETE, entry)
1176 CRYPT_HashToStr(toDelete->hash, asciiHash);
1177 TRACE("Removing %s\n", debugstr_w(asciiHash));
1178 rc = RegDeleteKeyW(key, asciiHash);
1179 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1184 list_remove(&toDelete->entry);
1185 CryptMemFree(toDelete);
1187 LeaveCriticalSection(&store->cs);
1189 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1202 /* If force is true or the registry store is dirty, writes the contents of the
1203 * store to the registry.
1205 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1209 TRACE("(%p, %d)\n", store, force);
1211 if (store->dirty || force)
1212 ret = CRYPT_RegWriteToReg(store);
1218 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1220 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1222 TRACE("(%p, %08x)\n", store, dwFlags);
1224 FIXME("Unimplemented flags: %08x\n", dwFlags);
1226 CRYPT_RegFlushStore(store, FALSE);
1227 RegCloseKey(store->key);
1228 DeleteCriticalSection(&store->cs);
1229 CryptMemFree(store);
1232 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1233 const void *context, DWORD dwFlags)
1237 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1239 store->dirty = TRUE;
1247 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1248 struct list *deleteList, const void *context,
1249 PCWINE_CONTEXT_INTERFACE contextInterface)
1253 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1255 SetLastError(ERROR_ACCESS_DENIED);
1260 PWINE_HASH_TO_DELETE toDelete =
1261 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1265 DWORD size = sizeof(toDelete->hash);
1267 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1268 toDelete->hash, &size);
1271 EnterCriticalSection(&store->cs);
1272 list_add_tail(deleteList, &toDelete->entry);
1273 LeaveCriticalSection(&store->cs);
1277 CryptMemFree(toDelete);
1284 store->dirty = TRUE;
1289 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1290 PCCERT_CONTEXT cert, DWORD dwFlags)
1292 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1294 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1296 return CRYPT_RegWriteContext(store, cert, dwFlags);
1299 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1300 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1302 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1304 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1306 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1310 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1311 PCCRL_CONTEXT crl, DWORD dwFlags)
1313 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1315 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1317 return CRYPT_RegWriteContext(store, crl, dwFlags);
1320 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1321 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1323 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1325 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1327 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1331 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1332 DWORD dwCtrlType, void const *pvCtrlPara)
1334 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1337 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1342 case CERT_STORE_CTRL_RESYNC:
1343 CRYPT_RegFlushStore(store, FALSE);
1344 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1345 CRYPT_RegReadFromReg(store);
1348 case CERT_STORE_CTRL_COMMIT:
1349 ret = CRYPT_RegFlushStore(store,
1350 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1353 FIXME("%d: stub\n", dwCtrlType);
1359 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1360 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1362 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1363 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1366 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1368 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1371 /* Find how many subkeys there are */
1372 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1373 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1377 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1379 /* Name too big: alloc a buffer for it */
1380 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1384 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1387 /* Recursively delete all the subkeys */
1388 for (i = 0; i < dwKeyCount && !dwRet; i++)
1390 dwSize = dwMaxSubkeyLen;
1391 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1394 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1397 if (lpszName != szNameBuf)
1399 /* Free buffer if allocated */
1400 CryptMemFree(lpszName);
1405 RegCloseKey(hSubKey);
1407 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1412 static void *regProvFuncs[] = {
1413 CRYPT_RegCloseStore,
1414 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1416 CRYPT_RegDeleteCert,
1417 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1418 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1421 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1422 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1423 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1424 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1425 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1429 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1430 DWORD dwFlags, const void *pvPara)
1432 PWINECRYPT_CERTSTORE store = NULL;
1434 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1436 if (dwFlags & CERT_STORE_DELETE_FLAG)
1438 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1440 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1441 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1442 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1443 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1444 if (rc == ERROR_NO_MORE_ITEMS)
1452 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1453 GetCurrentProcess(), (LPHANDLE)&key,
1454 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1457 PWINECRYPT_CERTSTORE memStore;
1459 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1462 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1463 sizeof(WINE_REGSTOREINFO));
1467 CERT_STORE_PROV_INFO provInfo = { 0 };
1469 regInfo->dwOpenFlags = dwFlags;
1470 regInfo->cryptProv = hCryptProv;
1471 regInfo->memStore = memStore;
1473 InitializeCriticalSection(®Info->cs);
1474 list_init(®Info->certsToDelete);
1475 list_init(®Info->crlsToDelete);
1476 CRYPT_RegReadFromReg(regInfo);
1477 regInfo->dirty = FALSE;
1478 provInfo.cbSize = sizeof(provInfo);
1479 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1480 sizeof(regProvFuncs[0]);
1481 provInfo.rgpvStoreProvFunc = regProvFuncs;
1482 provInfo.hStoreProv = regInfo;
1483 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1489 TRACE("returning %p\n", store);
1493 /* FIXME: this isn't complete for the Root store, in which the top-level
1494 * self-signed CA certs reside. Adding a cert to the Root store should present
1495 * the user with a dialog indicating the consequences of doing so, and asking
1496 * the user to confirm whether the cert should be added.
1498 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1499 DWORD dwFlags, const void *pvPara)
1501 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1502 LPCWSTR storeName = (LPCWSTR)pvPara;
1504 PWINECRYPT_CERTSTORE store = NULL;
1509 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1510 debugstr_w((LPCWSTR)pvPara));
1514 SetLastError(E_INVALIDARG);
1519 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1521 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1522 root = HKEY_LOCAL_MACHINE;
1523 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1525 case CERT_SYSTEM_STORE_CURRENT_USER:
1526 root = HKEY_CURRENT_USER;
1527 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1529 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1530 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1531 * SystemCertificates
1533 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1534 debugstr_w(storeName));
1536 case CERT_SYSTEM_STORE_SERVICES:
1537 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1538 * SystemCertificates
1540 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1541 debugstr_w(storeName));
1543 case CERT_SYSTEM_STORE_USERS:
1544 /* hku\user sid\Software\Microsoft\SystemCertificates */
1545 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1546 debugstr_w(storeName));
1548 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1549 root = HKEY_CURRENT_USER;
1550 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1552 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1553 root = HKEY_LOCAL_MACHINE;
1554 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1556 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1557 /* hklm\Software\Microsoft\EnterpriseCertificates */
1558 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1559 debugstr_w(storeName));
1562 SetLastError(E_INVALIDARG);
1566 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1572 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1575 wsprintfW(storePath, fmt, base, storeName);
1576 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1577 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1582 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1584 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1585 disp == REG_OPENED_EXISTING_KEY)
1588 rc = ERROR_FILE_EXISTS;
1593 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1598 CryptMemFree(storePath);
1603 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1604 DWORD dwFlags, const void *pvPara)
1607 PWINECRYPT_CERTSTORE ret = NULL;
1609 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1610 debugstr_a((LPCSTR)pvPara));
1614 SetLastError(ERROR_FILE_NOT_FOUND);
1617 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1620 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1624 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1625 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1626 CryptMemFree(storeName);
1632 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1633 DWORD dwFlags, const void *pvPara)
1635 HCERTSTORE store = 0;
1638 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1639 debugstr_w((LPCWSTR)pvPara));
1643 SetLastError(ERROR_FILE_NOT_FOUND);
1646 /* This returns a different error than system registry stores if the
1647 * location is invalid.
1649 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1651 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1652 case CERT_SYSTEM_STORE_CURRENT_USER:
1653 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1654 case CERT_SYSTEM_STORE_SERVICES:
1655 case CERT_SYSTEM_STORE_USERS:
1656 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1657 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1658 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1662 SetLastError(ERROR_FILE_NOT_FOUND);
1667 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1668 0, hCryptProv, dwFlags, pvPara);
1672 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1673 CERT_STORE_CREATE_NEW_FLAG, NULL);
1674 CertAddStoreToCollection(store, regStore,
1675 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1676 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1677 CertCloseStore(regStore, 0);
1678 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1681 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1682 CERT_SYSTEM_STORE_CURRENT_USER)
1684 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1685 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1686 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1687 hCryptProv, dwFlags, pvPara);
1690 CertAddStoreToCollection(store, regStore,
1691 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1692 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1693 CertCloseStore(regStore, 0);
1698 return (PWINECRYPT_CERTSTORE)store;
1701 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1702 DWORD dwFlags, const void *pvPara)
1705 PWINECRYPT_CERTSTORE ret = NULL;
1707 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1708 debugstr_a((LPCSTR)pvPara));
1712 SetLastError(ERROR_FILE_NOT_FOUND);
1715 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1718 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1722 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1723 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1724 CryptMemFree(storeName);
1730 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1732 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1734 TRACE("(%p, %08x)\n", store, dwFlags);
1736 CRYPT_WriteSerializedFile(store->file, store->memStore);
1737 CertCloseStore(store->memStore, dwFlags);
1738 CloseHandle(store->file);
1739 CryptMemFree(store);
1742 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1743 PCCERT_CONTEXT cert, DWORD dwFlags)
1745 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1747 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1748 store->dirty = TRUE;
1752 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1753 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1755 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1757 TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1758 store->dirty = TRUE;
1762 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1763 PCCRL_CONTEXT crl, DWORD dwFlags)
1765 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1767 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1768 store->dirty = TRUE;
1772 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1773 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1775 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1777 TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1778 store->dirty = TRUE;
1782 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1783 DWORD dwCtrlType, void const *pvCtrlPara)
1785 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1788 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1793 case CERT_STORE_CTRL_RESYNC:
1794 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1795 CRYPT_ReadSerializedFile(store->file, store);
1798 case CERT_STORE_CTRL_COMMIT:
1799 if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1801 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1804 else if (store->dirty)
1805 ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1810 FIXME("%d: stub\n", dwCtrlType);
1816 static void *fileProvFuncs[] = {
1817 CRYPT_FileCloseStore,
1818 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1819 CRYPT_FileWriteCert,
1820 CRYPT_FileDeleteCert,
1821 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1822 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1824 CRYPT_FileDeleteCRL,
1825 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1826 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1827 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1828 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1829 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1833 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1834 DWORD dwFlags, const void *pvPara)
1836 PWINECRYPT_CERTSTORE store = NULL;
1837 HANDLE file = (HANDLE)pvPara;
1839 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1843 SetLastError(ERROR_INVALID_HANDLE);
1846 if (dwFlags & CERT_STORE_DELETE_FLAG)
1848 SetLastError(E_INVALIDARG);
1851 if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1852 (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1854 SetLastError(E_INVALIDARG);
1858 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1859 GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1860 GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1862 PWINECRYPT_CERTSTORE memStore;
1864 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1867 if (CRYPT_ReadSerializedFile(file, memStore))
1869 PWINE_FILESTOREINFO info = CryptMemAlloc(
1870 sizeof(WINE_FILESTOREINFO));
1874 CERT_STORE_PROV_INFO provInfo = { 0 };
1876 info->dwOpenFlags = dwFlags;
1877 info->cryptProv = hCryptProv;
1878 info->memStore = memStore;
1880 info->dirty = FALSE;
1881 provInfo.cbSize = sizeof(provInfo);
1882 provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1883 sizeof(fileProvFuncs[0]);
1884 provInfo.rgpvStoreProvFunc = fileProvFuncs;
1885 provInfo.hStoreProv = info;
1886 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1892 TRACE("returning %p\n", store);
1896 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1897 DWORD dwFlags, const void *pvPara)
1899 HCERTSTORE store = 0;
1900 LPCWSTR fileName = (LPCWSTR)pvPara;
1901 DWORD access, create;
1904 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1908 SetLastError(ERROR_PATH_NOT_FOUND);
1911 if (!(dwFlags & (CERT_FILE_STORE_COMMIT_ENABLE_FLAG |
1912 CERT_STORE_READONLY_FLAG)))
1914 SetLastError(ERROR_FILE_NOT_FOUND);
1918 access = GENERIC_READ;
1919 if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1920 access |= GENERIC_WRITE;
1921 if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1922 create = CREATE_NEW;
1923 else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1924 create = OPEN_EXISTING;
1926 create = OPEN_ALWAYS;
1927 file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1928 FILE_ATTRIBUTE_NORMAL, NULL);
1929 if (file != INVALID_HANDLE_VALUE)
1931 /* FIXME: need to check whether it's a serialized store; if not, fall
1932 * back to a PKCS#7 signed message, then to a single serialized cert.
1934 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1938 return (PWINECRYPT_CERTSTORE)store;
1941 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1942 DWORD dwFlags, const void *pvPara)
1945 PWINECRYPT_CERTSTORE ret = NULL;
1947 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1948 debugstr_a((LPCSTR)pvPara));
1952 SetLastError(ERROR_FILE_NOT_FOUND);
1955 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1958 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1962 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1963 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1964 CryptMemFree(storeName);
1970 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
1971 DWORD dwFlags, const void *pvPara)
1973 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
1974 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
1976 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
1977 debugstr_w((LPCWSTR)pvPara));
1981 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1982 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1985 WINECRYPT_CERTSTORE *hcs;
1986 StoreOpenFunc openFunc = NULL;
1988 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
1989 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1991 if (!HIWORD(lpszStoreProvider))
1993 switch (LOWORD(lpszStoreProvider))
1995 case (int)CERT_STORE_PROV_MEMORY:
1996 openFunc = CRYPT_MemOpenStore;
1998 case (int)CERT_STORE_PROV_FILE:
1999 openFunc = CRYPT_FileOpenStore;
2001 case (int)CERT_STORE_PROV_REG:
2002 openFunc = CRYPT_RegOpenStore;
2004 case (int)CERT_STORE_PROV_FILENAME_A:
2005 openFunc = CRYPT_FileNameOpenStoreA;
2007 case (int)CERT_STORE_PROV_FILENAME_W:
2008 openFunc = CRYPT_FileNameOpenStoreW;
2010 case (int)CERT_STORE_PROV_COLLECTION:
2011 openFunc = CRYPT_CollectionOpenStore;
2013 case (int)CERT_STORE_PROV_SYSTEM_A:
2014 openFunc = CRYPT_SysOpenStoreA;
2016 case (int)CERT_STORE_PROV_SYSTEM_W:
2017 openFunc = CRYPT_SysOpenStoreW;
2019 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
2020 openFunc = CRYPT_SysRegOpenStoreA;
2022 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
2023 openFunc = CRYPT_SysRegOpenStoreW;
2025 case (int)CERT_STORE_PROV_PHYSICAL_W:
2026 openFunc = CRYPT_PhysOpenStoreW;
2029 if (LOWORD(lpszStoreProvider))
2030 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
2033 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
2034 openFunc = CRYPT_MemOpenStore;
2035 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
2036 openFunc = CRYPT_FileOpenStore;
2037 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
2038 openFunc = CRYPT_SysOpenStoreW;
2039 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
2040 openFunc = CRYPT_CollectionOpenStore;
2041 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
2042 openFunc = CRYPT_SysRegOpenStoreW;
2045 FIXME("unimplemented type %s\n", lpszStoreProvider);
2050 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2051 hCryptProv, dwFlags, pvPara);
2053 hcs = openFunc(hCryptProv, dwFlags, pvPara);
2054 return (HCERTSTORE)hcs;
2057 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
2058 LPCSTR szSubSystemProtocol)
2060 if (!szSubSystemProtocol)
2062 SetLastError(E_INVALIDARG);
2065 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2066 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2069 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
2070 LPCWSTR szSubSystemProtocol)
2072 if (!szSubSystemProtocol)
2074 SetLastError(E_INVALIDARG);
2077 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2078 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2081 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2082 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2084 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore,
2085 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2089 DWORD CertStore_GetAccessState(HCERTSTORE hCertStore)
2095 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2097 if (store->type != StoreTypeMem &&
2098 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2099 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2104 #define CertContext_CopyProperties(to, from) \
2105 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2107 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2108 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2109 PCCERT_CONTEXT *ppStoreContext)
2111 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2113 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2115 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2116 dwAddDisposition, ppStoreContext);
2118 /* Weird case to pass a test */
2119 if (dwAddDisposition == 0)
2121 SetLastError(STATUS_ACCESS_VIOLATION);
2124 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2127 DWORD size = sizeof(hashToAdd);
2129 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2133 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2135 existing = CertFindCertificateInStore(hCertStore,
2136 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2141 switch (dwAddDisposition)
2143 case CERT_STORE_ADD_ALWAYS:
2144 toAdd = CertDuplicateCertificateContext(pCertContext);
2146 case CERT_STORE_ADD_NEW:
2149 TRACE("found matching certificate, not adding\n");
2150 SetLastError(CRYPT_E_EXISTS);
2154 toAdd = CertDuplicateCertificateContext(pCertContext);
2156 case CERT_STORE_ADD_REPLACE_EXISTING:
2157 toAdd = CertDuplicateCertificateContext(pCertContext);
2159 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2160 toAdd = CertDuplicateCertificateContext(pCertContext);
2162 CertContext_CopyProperties(toAdd, existing);
2164 case CERT_STORE_ADD_USE_EXISTING:
2166 CertContext_CopyProperties(existing, pCertContext);
2169 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2176 ret = store->certs.addContext(store, (void *)toAdd,
2177 (void *)existing, (const void **)ppStoreContext);
2178 else if (ppStoreContext)
2179 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2180 CertFreeCertificateContext(toAdd);
2182 CertFreeCertificateContext(existing);
2184 TRACE("returning %d\n", ret);
2188 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2189 PCCERT_CONTEXT pPrev)
2191 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2194 TRACE("(%p, %p)\n", hCertStore, pPrev);
2197 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2200 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2204 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2208 TRACE("(%p)\n", pCertContext);
2212 else if (!pCertContext->hCertStore)
2215 CertFreeCertificateContext(pCertContext);
2219 PWINECRYPT_CERTSTORE hcs =
2220 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2222 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2225 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2226 CertFreeCertificateContext(pCertContext);
2231 #define CrlContext_CopyProperties(to, from) \
2232 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2234 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2235 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2236 PCCRL_CONTEXT* ppStoreContext)
2238 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2240 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2242 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2243 dwAddDisposition, ppStoreContext);
2245 /* Weird case to pass a test */
2246 if (dwAddDisposition == 0)
2248 SetLastError(STATUS_ACCESS_VIOLATION);
2251 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2253 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2257 switch (dwAddDisposition)
2259 case CERT_STORE_ADD_ALWAYS:
2260 toAdd = CertDuplicateCRLContext(pCrlContext);
2262 case CERT_STORE_ADD_NEW:
2265 TRACE("found matching CRL, not adding\n");
2266 SetLastError(CRYPT_E_EXISTS);
2270 toAdd = CertDuplicateCRLContext(pCrlContext);
2272 case CERT_STORE_ADD_NEWER:
2275 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2276 &pCrlContext->pCrlInfo->ThisUpdate);
2279 toAdd = CertDuplicateCRLContext(pCrlContext);
2282 TRACE("existing CRL is newer, not adding\n");
2283 SetLastError(CRYPT_E_EXISTS);
2288 toAdd = CertDuplicateCRLContext(pCrlContext);
2290 case CERT_STORE_ADD_REPLACE_EXISTING:
2291 toAdd = CertDuplicateCRLContext(pCrlContext);
2293 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2294 toAdd = CertDuplicateCRLContext(pCrlContext);
2296 CrlContext_CopyProperties(toAdd, existing);
2298 case CERT_STORE_ADD_USE_EXISTING:
2300 CrlContext_CopyProperties(existing, pCrlContext);
2303 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2310 ret = store->crls.addContext(store, (void *)toAdd,
2311 (void *)existing, (const void **)ppStoreContext);
2312 else if (ppStoreContext)
2313 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2314 CertFreeCRLContext(toAdd);
2316 CertFreeCRLContext(existing);
2318 TRACE("returning %d\n", ret);
2322 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2326 TRACE("(%p)\n", pCrlContext);
2330 else if (!pCrlContext->hCertStore)
2333 CertFreeCRLContext(pCrlContext);
2337 PWINECRYPT_CERTSTORE hcs =
2338 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2340 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2343 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2344 CertFreeCRLContext(pCrlContext);
2349 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2350 PCCRL_CONTEXT pPrev)
2352 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2355 TRACE("(%p, %p)\n", hCertStore, pPrev);
2358 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2361 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2365 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2366 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2368 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2373 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2374 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2375 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2377 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2378 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2383 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2384 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2385 PCCTL_CONTEXT* ppStoreContext)
2387 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2388 dwAddDisposition, ppStoreContext);
2392 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2394 FIXME("(%p): stub\n", pCtlContext );
2398 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2400 FIXME("(%p): stub\n", pCtlContext );
2404 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2406 FIXME("(%p): stub\n", pCtlContext);
2410 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2411 PCCTL_CONTEXT pPrev)
2413 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2417 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2419 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2421 TRACE("(%p)\n", hCertStore);
2423 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2424 InterlockedIncrement(&hcs->ref);
2428 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2430 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2432 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2437 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2440 if (InterlockedDecrement(&hcs->ref) == 0)
2442 TRACE("%p's ref count is 0, freeing\n", hcs);
2444 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2445 CryptReleaseContext(hcs->cryptProv, 0);
2446 hcs->closeStore(hcs, dwFlags);
2449 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2453 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2454 DWORD dwCtrlType, void const *pvCtrlPara)
2456 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2459 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2464 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2469 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2476 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2479 FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2483 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2484 DWORD dwPropId, void *pvData, DWORD *pcbData)
2486 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2490 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2491 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2493 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2498 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2499 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2501 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2502 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2503 PWINE_STORE_LIST_ENTRY entry;
2506 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2507 dwUpdateFlags, dwPriority);
2509 if (!collection || !sibling)
2511 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2513 SetLastError(E_INVALIDARG);
2516 if (collection->hdr.type != StoreTypeCollection)
2518 SetLastError(E_INVALIDARG);
2521 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2523 SetLastError(E_INVALIDARG);
2527 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2530 InterlockedIncrement(&sibling->ref);
2531 TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2532 entry->store = sibling;
2533 entry->dwUpdateFlags = dwUpdateFlags;
2534 entry->dwPriority = dwPriority;
2535 list_init(&entry->entry);
2536 TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2537 EnterCriticalSection(&collection->cs);
2540 PWINE_STORE_LIST_ENTRY cursor;
2543 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2544 WINE_STORE_LIST_ENTRY, entry)
2546 if (cursor->dwPriority < dwPriority)
2548 list_add_before(&cursor->entry, &entry->entry);
2554 list_add_tail(&collection->stores, &entry->entry);
2557 list_add_tail(&collection->stores, &entry->entry);
2558 LeaveCriticalSection(&collection->cs);
2566 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2567 HCERTSTORE hSiblingStore)
2569 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2570 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2571 PWINE_STORE_LIST_ENTRY store, next;
2573 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2575 if (!collection || !sibling)
2577 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2579 SetLastError(E_INVALIDARG);
2582 if (collection->hdr.type != StoreTypeCollection)
2584 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2586 SetLastError(E_INVALIDARG);
2589 EnterCriticalSection(&collection->cs);
2590 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2591 WINE_STORE_LIST_ENTRY, entry)
2593 if (store->store == sibling)
2595 list_remove(&store->entry);
2596 CertCloseStore(store->store, 0);
2597 CryptMemFree(store);
2601 LeaveCriticalSection(&collection->cs);