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"
39 #include "wine/exception.h"
40 #include "crypt32_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
44 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
46 static const WINE_CONTEXT_INTERFACE gCertInterface = {
47 (CreateContextFunc)CertCreateCertificateContext,
48 (AddContextToStoreFunc)CertAddCertificateContextToStore,
49 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
50 (DuplicateContextFunc)CertDuplicateCertificateContext,
51 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
52 (EnumPropertiesFunc)CertEnumCertificateContextProperties,
53 (GetContextPropertyFunc)CertGetCertificateContextProperty,
54 (SetContextPropertyFunc)CertSetCertificateContextProperty,
55 (SerializeElementFunc)CertSerializeCertificateStoreElement,
56 (FreeContextFunc)CertFreeCertificateContext,
57 (DeleteContextFunc)CertDeleteCertificateFromStore,
59 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
61 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
62 (CreateContextFunc)CertCreateCRLContext,
63 (AddContextToStoreFunc)CertAddCRLContextToStore,
64 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
65 (DuplicateContextFunc)CertDuplicateCRLContext,
66 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
67 (EnumPropertiesFunc)CertEnumCRLContextProperties,
68 (GetContextPropertyFunc)CertGetCRLContextProperty,
69 (SetContextPropertyFunc)CertSetCRLContextProperty,
70 (SerializeElementFunc)CertSerializeCRLStoreElement,
71 (FreeContextFunc)CertFreeCRLContext,
72 (DeleteContextFunc)CertDeleteCRLFromStore,
74 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
76 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
77 (CreateContextFunc)CertCreateCTLContext,
78 (AddContextToStoreFunc)CertAddCTLContextToStore,
79 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
80 (DuplicateContextFunc)CertDuplicateCTLContext,
81 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
82 (EnumPropertiesFunc)CertEnumCTLContextProperties,
83 (GetContextPropertyFunc)CertGetCTLContextProperty,
84 (SetContextPropertyFunc)CertSetCTLContextProperty,
85 (SerializeElementFunc)CertSerializeCTLStoreElement,
86 (FreeContextFunc)CertFreeCTLContext,
87 (DeleteContextFunc)CertDeleteCTLFromStore,
89 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
91 struct WINE_CRYPTCERTSTORE;
93 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
94 DWORD dwFlags, const void *pvPara);
96 /* Called to enumerate the next context in a store. */
97 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
99 /* Called to add a context to a store. If toReplace is not NULL,
100 * context replaces toReplace in the store, and access checks should not be
101 * performed. Otherwise context is a new context, and it should only be
102 * added if the store allows it. If ppStoreContext is not NULL, the added
103 * context should be returned in *ppStoreContext.
105 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
106 void *toReplace, const void **ppStoreContext);
108 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
110 typedef struct _CONTEXT_STORE
113 EnumFunc enumContext;
114 DeleteFunc deleteContext;
115 } CONTEXT_STORE, *PCONTEXT_STORE;
117 typedef enum _CertStoreType {
123 /* A cert store is polymorphic through the use of function pointers. A type
124 * is still needed to distinguish collection stores from other types.
125 * On the function pointers:
126 * - closeStore is called when the store's ref count becomes 0
127 * - control is optional, but should be implemented by any store that supports
130 typedef struct WINE_CRYPTCERTSTORE
135 HCRYPTPROV cryptProv;
137 PFN_CERT_STORE_PROV_CLOSE closeStore;
140 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
141 PCONTEXT_PROPERTY_LIST properties;
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;
220 store->properties = NULL;
223 static void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store)
225 if (store->properties)
226 ContextPropertyList_Free(store->properties);
230 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
231 void *toReplace, const void **ppStoreContext)
233 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
234 PCERT_CONTEXT context;
236 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
238 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
241 context->hCertStore = store;
243 *ppStoreContext = CertDuplicateCertificateContext(context);
245 return context ? TRUE : FALSE;
248 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
250 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
253 TRACE("(%p, %p)\n", store, pPrev);
255 ret = ContextList_Enum(ms->certs, pPrev);
257 SetLastError(CRYPT_E_NOT_FOUND);
259 TRACE("returning %p\n", ret);
263 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
265 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
267 ContextList_Delete(ms->certs, pCertContext);
271 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
272 void *toReplace, const void **ppStoreContext)
274 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
275 PCRL_CONTEXT context;
277 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
279 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
282 context->hCertStore = store;
284 *ppStoreContext = CertDuplicateCRLContext(context);
286 return context ? TRUE : FALSE;
289 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
291 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
294 TRACE("(%p, %p)\n", store, pPrev);
296 ret = ContextList_Enum(ms->crls, pPrev);
298 SetLastError(CRYPT_E_NOT_FOUND);
300 TRACE("returning %p\n", ret);
304 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
306 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
308 ContextList_Delete(ms->crls, pCrlContext);
312 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
314 ContextList_Empty(store->certs);
315 ContextList_Empty(store->crls);
318 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
320 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
322 TRACE("(%p, %08x)\n", store, dwFlags);
324 FIXME("Unimplemented flags: %08x\n", dwFlags);
326 ContextList_Free(store->certs);
327 ContextList_Free(store->crls);
328 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
331 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
332 DWORD dwFlags, const void *pvPara)
334 PWINE_MEMSTORE store;
336 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
338 if (dwFlags & CERT_STORE_DELETE_FLAG)
340 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
345 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
348 memset(store, 0, sizeof(WINE_MEMSTORE));
349 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
350 store->hdr.closeStore = CRYPT_MemCloseStore;
351 store->hdr.certs.addContext = CRYPT_MemAddCert;
352 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
353 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
354 store->hdr.crls.addContext = CRYPT_MemAddCrl;
355 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
356 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
357 store->hdr.control = NULL;
358 store->certs = ContextList_Create(pCertInterface,
359 sizeof(CERT_CONTEXT));
360 store->crls = ContextList_Create(pCRLInterface,
361 sizeof(CRL_CONTEXT));
364 return (PWINECRYPT_CERTSTORE)store;
367 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
369 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
370 PWINE_STORE_LIST_ENTRY entry, next;
372 TRACE("(%p, %08x)\n", store, dwFlags);
374 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
377 TRACE("closing %p\n", entry);
378 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
381 cs->cs.DebugInfo->Spare[0] = 0;
382 DeleteCriticalSection(&cs->cs);
383 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
386 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
387 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
390 void *ret = Context_CreateLinkContext(contextSize, child,
391 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
394 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
400 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
401 unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
402 void **pChildContext)
405 void *childContext = NULL;
406 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
408 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
409 toReplace, contextSize);
414 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
415 PCONTEXT_STORE contextStore;
417 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
419 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
421 ret = contextStore->addContext(storeEntry->store, context,
422 existingLinked, (const void **)&childContext);
426 PWINE_STORE_LIST_ENTRY entry, next;
428 EnterCriticalSection(&store->cs);
429 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
430 WINE_STORE_LIST_ENTRY, entry)
432 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
434 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
435 (LPBYTE)entry->store + contextStoreOffset);
438 ret = contextStore->addContext(entry->store, context, NULL,
439 (const void **)&childContext);
443 LeaveCriticalSection(&store->cs);
445 SetLastError(E_ACCESSDENIED);
447 *pChildContext = childContext;
451 /* Advances a collection enumeration by one context, if possible, where
453 * - calling the current store's enumeration function once, and returning
454 * the enumerated context if one is returned
455 * - moving to the next store if the current store has no more items, and
456 * recursively calling itself to get the next item.
457 * Returns NULL if the collection contains no more items or on error.
458 * Assumes the collection store's lock is held.
460 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
461 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
462 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
465 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
466 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
469 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
473 /* Ref-counting funny business: "duplicate" (addref) the child, because
474 * the free(pPrev) below can cause the ref count to become negative.
476 child = Context_GetLinkedContext(pPrev, contextSize);
477 contextInterface->duplicate(child);
478 child = contextStore->enumContext(storeEntry->store, child);
479 contextInterface->free(pPrev);
483 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
485 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
490 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
491 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
492 contextInterface, NULL, contextSize);
495 SetLastError(CRYPT_E_NOT_FOUND);
499 TRACE("returning %p\n", ret);
503 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
504 void *toReplace, const void **ppStoreContext)
507 void *childContext = NULL;
508 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
510 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
511 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
512 if (ppStoreContext && childContext)
514 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
515 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
516 PCERT_CONTEXT context =
517 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
518 sizeof(CERT_CONTEXT), TRUE);
521 context->hCertStore = store;
522 *ppStoreContext = context;
524 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
528 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
530 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
533 TRACE("(%p, %p)\n", store, pPrev);
535 EnterCriticalSection(&cs->cs);
538 PWINE_STORE_LIST_ENTRY storeEntry =
539 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
540 sizeof(CERT_CONTEXT));
542 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
543 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
544 sizeof(CERT_CONTEXT));
548 if (!list_empty(&cs->stores))
550 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
551 WINE_STORE_LIST_ENTRY, entry);
553 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
554 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
555 sizeof(CERT_CONTEXT));
559 SetLastError(CRYPT_E_NOT_FOUND);
563 LeaveCriticalSection(&cs->cs);
565 ((PCERT_CONTEXT)ret)->hCertStore = store;
566 TRACE("returning %p\n", ret);
570 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
575 TRACE("(%p, %p)\n", store, pCertContext);
577 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
578 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
582 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
583 void *toReplace, const void **ppStoreContext)
586 void *childContext = NULL;
587 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
589 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
590 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
591 if (ppStoreContext && childContext)
593 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
594 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
595 PCRL_CONTEXT context =
596 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
597 sizeof(CRL_CONTEXT), TRUE);
600 context->hCertStore = store;
601 *ppStoreContext = context;
603 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
607 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
609 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
612 TRACE("(%p, %p)\n", store, pPrev);
614 EnterCriticalSection(&cs->cs);
617 PWINE_STORE_LIST_ENTRY storeEntry =
618 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
619 sizeof(CRL_CONTEXT));
621 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
622 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
623 sizeof(CRL_CONTEXT));
627 if (!list_empty(&cs->stores))
629 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
630 WINE_STORE_LIST_ENTRY, entry);
632 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
633 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
634 sizeof(CRL_CONTEXT));
638 SetLastError(CRYPT_E_NOT_FOUND);
642 LeaveCriticalSection(&cs->cs);
644 ((PCRL_CONTEXT)ret)->hCertStore = store;
645 TRACE("returning %p\n", ret);
649 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
654 TRACE("(%p, %p)\n", store, pCrlContext);
656 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
657 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
661 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
662 DWORD dwFlags, const void *pvPara)
664 PWINE_COLLECTIONSTORE store;
666 if (dwFlags & CERT_STORE_DELETE_FLAG)
668 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
673 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
676 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
677 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
678 StoreTypeCollection);
679 store->hdr.closeStore = CRYPT_CollectionCloseStore;
680 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
681 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
682 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
683 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
684 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
685 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
686 InitializeCriticalSection(&store->cs);
687 store->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_COLLECTIONSTORE->cs");
688 list_init(&store->stores);
691 return (PWINECRYPT_CERTSTORE)store;
694 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
696 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
698 TRACE("(%p, %08x)\n", store, dwFlags);
700 if (store->provCloseStore)
701 store->provCloseStore(store->hStoreProv, dwFlags);
702 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
703 CertCloseStore(store->memStore, dwFlags);
704 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
707 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
708 void *toReplace, const void **ppStoreContext)
710 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
713 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
716 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
717 (const void **)ppStoreContext);
721 if (ps->provWriteCert)
722 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
723 CERT_STORE_PROV_WRITE_ADD_FLAG);
725 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
726 (const void **)ppStoreContext);
728 /* dirty trick: replace the returned context's hCertStore with
732 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
736 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
738 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
741 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
744 /* same dirty trick: replace the returned context's hCertStore with
747 ((PCERT_CONTEXT)ret)->hCertStore = store;
752 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
754 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
757 TRACE("(%p, %p)\n", store, cert);
759 if (ps->provDeleteCert)
760 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
762 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
766 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
767 void *toReplace, const void **ppStoreContext)
769 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
772 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
775 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
776 (const void **)ppStoreContext);
779 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
781 SetLastError(ERROR_ACCESS_DENIED);
787 if (ps->provWriteCrl)
788 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
789 CERT_STORE_PROV_WRITE_ADD_FLAG);
791 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
792 (const void **)ppStoreContext);
795 /* dirty trick: replace the returned context's hCertStore with
799 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
803 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
805 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
808 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
811 /* same dirty trick: replace the returned context's hCertStore with
814 ((PCERT_CONTEXT)ret)->hCertStore = store;
819 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
821 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
824 TRACE("(%p, %p)\n", store, crl);
826 if (ps->provDeleteCrl)
827 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
829 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
833 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
834 DWORD dwCtrlType, void const *pvCtrlPara)
836 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
839 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
842 if (store->provControl)
843 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
848 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
849 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo)
851 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
852 sizeof(WINE_PROVIDERSTORE));
856 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
858 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
859 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
861 CertCloseStore(memStore, 0);
862 ret->memStore = NULL;
865 ret->memStore = memStore;
866 ret->hStoreProv = pProvInfo->hStoreProv;
867 ret->hdr.closeStore = CRYPT_ProvCloseStore;
868 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
869 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
870 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
871 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
872 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
873 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
874 ret->hdr.control = CRYPT_ProvControl;
875 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
876 ret->provCloseStore =
877 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
879 ret->provCloseStore = NULL;
880 if (pProvInfo->cStoreProvFunc >
881 CERT_STORE_PROV_WRITE_CERT_FUNC)
882 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
883 CERT_STORE_PROV_WRITE_CERT_FUNC];
885 ret->provWriteCert = NULL;
886 if (pProvInfo->cStoreProvFunc >
887 CERT_STORE_PROV_DELETE_CERT_FUNC)
888 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
889 CERT_STORE_PROV_DELETE_CERT_FUNC];
891 ret->provDeleteCert = NULL;
892 if (pProvInfo->cStoreProvFunc >
893 CERT_STORE_PROV_WRITE_CRL_FUNC)
894 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
895 CERT_STORE_PROV_WRITE_CRL_FUNC];
897 ret->provWriteCert = NULL;
898 if (pProvInfo->cStoreProvFunc >
899 CERT_STORE_PROV_DELETE_CRL_FUNC)
900 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
901 CERT_STORE_PROV_DELETE_CRL_FUNC];
903 ret->provDeleteCert = NULL;
904 if (pProvInfo->cStoreProvFunc >
905 CERT_STORE_PROV_CONTROL_FUNC)
906 ret->provControl = pProvInfo->rgpvStoreProvFunc[
907 CERT_STORE_PROV_CONTROL_FUNC];
909 ret->provControl = NULL;
911 return (PWINECRYPT_CERTSTORE)ret;
914 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
915 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
917 static HCRYPTOIDFUNCSET set = NULL;
918 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
919 HCRYPTOIDFUNCADDR hFunc;
920 PWINECRYPT_CERTSTORE ret = NULL;
923 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
924 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
925 (void **)&provOpenFunc, &hFunc);
928 CERT_STORE_PROV_INFO provInfo = { 0 };
930 provInfo.cbSize = sizeof(provInfo);
931 if (dwFlags & CERT_STORE_DELETE_FLAG)
932 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
933 dwFlags, pvPara, NULL, &provInfo);
938 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
939 CERT_STORE_CREATE_NEW_FLAG, NULL);
942 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
943 dwFlags, pvPara, memStore, &provInfo))
944 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
947 CertCloseStore(memStore, 0);
950 CryptFreeOIDFunctionAddress(hFunc, 0);
953 SetLastError(ERROR_FILE_NOT_FOUND);
957 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
959 static const WCHAR fmt[] = { '%','0','2','X',0 };
965 for (i = 0; i < 20; i++)
966 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
969 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
971 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
972 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
973 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
975 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO *store, HKEY key,
980 WCHAR subKeyName[MAX_PATH];
983 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
985 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
991 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
997 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
999 buf = CryptMemAlloc(size);
1002 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
1006 const void *context;
1009 TRACE("Adding cert with hash %s\n",
1010 debugstr_w(subKeyName));
1011 context = CRYPT_ReadSerializedElement(buf, size,
1012 contextType, &addedType);
1015 const WINE_CONTEXT_INTERFACE *contextInterface;
1020 case CERT_STORE_CERTIFICATE_CONTEXT:
1021 contextInterface = &gCertInterface;
1023 case CERT_STORE_CRL_CONTEXT:
1024 contextInterface = &gCRLInterface;
1026 case CERT_STORE_CTL_CONTEXT:
1027 contextInterface = &gCTLInterface;
1030 contextInterface = NULL;
1032 if (contextInterface)
1034 size = sizeof(hash);
1035 if (contextInterface->getProp(context,
1036 CERT_HASH_PROP_ID, hash, &size))
1038 WCHAR asciiHash[20 * 2 + 1];
1040 CRYPT_HashToStr(hash, asciiHash);
1041 TRACE("comparing %s\n",
1042 debugstr_w(asciiHash));
1043 TRACE("with %s\n", debugstr_w(subKeyName));
1044 if (!lstrcmpW(asciiHash, subKeyName))
1046 TRACE("hash matches, adding\n");
1047 contextInterface->addContextToStore(
1048 store->memStore, context,
1049 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1052 TRACE("hash doesn't match, ignoring\n");
1054 contextInterface->free(context);
1060 RegCloseKey(subKey);
1062 /* Ignore intermediate errors, continue enumerating */
1068 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO *store)
1070 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1071 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1072 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1075 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1080 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1084 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1090 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1091 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
1094 WCHAR asciiHash[20 * 2 + 1];
1099 CRYPT_HashToStr(hash, asciiHash);
1100 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1104 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1105 RegCloseKey(subKey);
1117 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1118 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1120 const void *context = NULL;
1124 context = contextInterface->enumContextsInStore(memStore, context);
1128 DWORD hashSize = sizeof(hash);
1130 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1137 ret = contextInterface->serialize(context, 0, NULL, &size);
1139 buf = CryptMemAlloc(size);
1142 ret = contextInterface->serialize(context, 0, buf, &size);
1144 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1151 } while (ret && context != NULL);
1153 contextInterface->free(context);
1157 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1159 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1160 static const WINE_CONTEXT_INTERFACE * const interfaces[] = { &gCertInterface,
1161 &gCRLInterface, &gCTLInterface };
1162 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1167 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1170 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1171 KEY_ALL_ACCESS, NULL, &key, NULL);
1175 if (listToDelete[i])
1177 PWINE_HASH_TO_DELETE toDelete, next;
1178 WCHAR asciiHash[20 * 2 + 1];
1180 EnterCriticalSection(&store->cs);
1181 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1182 WINE_HASH_TO_DELETE, entry)
1186 CRYPT_HashToStr(toDelete->hash, asciiHash);
1187 TRACE("Removing %s\n", debugstr_w(asciiHash));
1188 rc = RegDeleteKeyW(key, asciiHash);
1189 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1194 list_remove(&toDelete->entry);
1195 CryptMemFree(toDelete);
1197 LeaveCriticalSection(&store->cs);
1199 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1212 /* If force is true or the registry store is dirty, writes the contents of the
1213 * store to the registry.
1215 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1219 TRACE("(%p, %d)\n", store, force);
1221 if (store->dirty || force)
1222 ret = CRYPT_RegWriteToReg(store);
1228 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1230 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1232 TRACE("(%p, %08x)\n", store, dwFlags);
1234 FIXME("Unimplemented flags: %08x\n", dwFlags);
1236 CRYPT_RegFlushStore(store, FALSE);
1237 RegCloseKey(store->key);
1238 store->cs.DebugInfo->Spare[0] = 0;
1239 DeleteCriticalSection(&store->cs);
1240 CryptMemFree(store);
1243 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1244 const void *context, DWORD dwFlags)
1248 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1250 store->dirty = TRUE;
1258 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1259 struct list *deleteList, const void *context,
1260 PCWINE_CONTEXT_INTERFACE contextInterface)
1264 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1266 SetLastError(ERROR_ACCESS_DENIED);
1271 PWINE_HASH_TO_DELETE toDelete =
1272 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1276 DWORD size = sizeof(toDelete->hash);
1278 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1279 toDelete->hash, &size);
1282 EnterCriticalSection(&store->cs);
1283 list_add_tail(deleteList, &toDelete->entry);
1284 LeaveCriticalSection(&store->cs);
1288 CryptMemFree(toDelete);
1295 store->dirty = TRUE;
1300 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1301 PCCERT_CONTEXT cert, DWORD dwFlags)
1303 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1305 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1307 return CRYPT_RegWriteContext(store, cert, dwFlags);
1310 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1311 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1313 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1315 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1317 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1321 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1322 PCCRL_CONTEXT crl, DWORD dwFlags)
1324 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1326 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1328 return CRYPT_RegWriteContext(store, crl, dwFlags);
1331 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1332 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1334 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1336 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1338 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1342 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1343 DWORD dwCtrlType, void const *pvCtrlPara)
1345 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1348 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1353 case CERT_STORE_CTRL_RESYNC:
1354 CRYPT_RegFlushStore(store, FALSE);
1355 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1356 CRYPT_RegReadFromReg(store);
1359 case CERT_STORE_CTRL_COMMIT:
1360 ret = CRYPT_RegFlushStore(store,
1361 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1364 FIXME("%d: stub\n", dwCtrlType);
1370 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1371 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1373 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1374 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1377 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1379 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1382 /* Find how many subkeys there are */
1383 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1384 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1388 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1390 /* Name too big: alloc a buffer for it */
1391 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1395 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1398 /* Recursively delete all the subkeys */
1399 for (i = 0; i < dwKeyCount && !dwRet; i++)
1401 dwSize = dwMaxSubkeyLen;
1402 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1405 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1408 if (lpszName != szNameBuf)
1410 /* Free buffer if allocated */
1411 CryptMemFree(lpszName);
1416 RegCloseKey(hSubKey);
1418 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1423 static void *regProvFuncs[] = {
1424 CRYPT_RegCloseStore,
1425 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1427 CRYPT_RegDeleteCert,
1428 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1429 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1432 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1433 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1434 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1435 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1436 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1440 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1441 DWORD dwFlags, const void *pvPara)
1443 PWINECRYPT_CERTSTORE store = NULL;
1445 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1447 if (dwFlags & CERT_STORE_DELETE_FLAG)
1449 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1451 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1452 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1453 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1454 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1455 if (rc == ERROR_NO_MORE_ITEMS)
1463 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1464 GetCurrentProcess(), (LPHANDLE)&key,
1465 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1468 PWINECRYPT_CERTSTORE memStore;
1470 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1473 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1474 sizeof(WINE_REGSTOREINFO));
1478 CERT_STORE_PROV_INFO provInfo = { 0 };
1480 regInfo->dwOpenFlags = dwFlags;
1481 regInfo->cryptProv = hCryptProv;
1482 regInfo->memStore = memStore;
1484 InitializeCriticalSection(®Info->cs);
1485 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
1486 list_init(®Info->certsToDelete);
1487 list_init(®Info->crlsToDelete);
1488 CRYPT_RegReadFromReg(regInfo);
1489 regInfo->dirty = FALSE;
1490 provInfo.cbSize = sizeof(provInfo);
1491 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1492 sizeof(regProvFuncs[0]);
1493 provInfo.rgpvStoreProvFunc = regProvFuncs;
1494 provInfo.hStoreProv = regInfo;
1495 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1501 TRACE("returning %p\n", store);
1505 /* FIXME: this isn't complete for the Root store, in which the top-level
1506 * self-signed CA certs reside. Adding a cert to the Root store should present
1507 * the user with a dialog indicating the consequences of doing so, and asking
1508 * the user to confirm whether the cert should be added.
1510 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1511 DWORD dwFlags, const void *pvPara)
1513 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1514 LPCWSTR storeName = (LPCWSTR)pvPara;
1516 PWINECRYPT_CERTSTORE store = NULL;
1521 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1522 debugstr_w((LPCWSTR)pvPara));
1526 SetLastError(E_INVALIDARG);
1531 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1533 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1534 root = HKEY_LOCAL_MACHINE;
1535 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1537 case CERT_SYSTEM_STORE_CURRENT_USER:
1538 root = HKEY_CURRENT_USER;
1539 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1541 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1542 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1543 * SystemCertificates
1545 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1546 debugstr_w(storeName));
1548 case CERT_SYSTEM_STORE_SERVICES:
1549 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1550 * SystemCertificates
1552 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1553 debugstr_w(storeName));
1555 case CERT_SYSTEM_STORE_USERS:
1556 /* hku\user sid\Software\Microsoft\SystemCertificates */
1557 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1558 debugstr_w(storeName));
1560 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1561 root = HKEY_CURRENT_USER;
1562 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1564 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1565 root = HKEY_LOCAL_MACHINE;
1566 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1568 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1569 /* hklm\Software\Microsoft\EnterpriseCertificates */
1570 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1571 debugstr_w(storeName));
1574 SetLastError(E_INVALIDARG);
1578 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1584 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1587 wsprintfW(storePath, fmt, base, storeName);
1588 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1589 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1594 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1596 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1597 disp == REG_OPENED_EXISTING_KEY)
1600 rc = ERROR_FILE_EXISTS;
1605 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1610 CryptMemFree(storePath);
1615 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1616 DWORD dwFlags, const void *pvPara)
1619 PWINECRYPT_CERTSTORE ret = NULL;
1621 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1622 debugstr_a((LPCSTR)pvPara));
1626 SetLastError(ERROR_FILE_NOT_FOUND);
1629 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1632 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1636 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1637 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1638 CryptMemFree(storeName);
1644 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1645 DWORD dwFlags, const void *pvPara)
1647 HCERTSTORE store = 0;
1650 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1651 debugstr_w((LPCWSTR)pvPara));
1655 SetLastError(ERROR_FILE_NOT_FOUND);
1658 /* This returns a different error than system registry stores if the
1659 * location is invalid.
1661 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1663 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1664 case CERT_SYSTEM_STORE_CURRENT_USER:
1665 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1666 case CERT_SYSTEM_STORE_SERVICES:
1667 case CERT_SYSTEM_STORE_USERS:
1668 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1669 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1670 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1674 SetLastError(ERROR_FILE_NOT_FOUND);
1679 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1680 0, hCryptProv, dwFlags, pvPara);
1684 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1685 CERT_STORE_CREATE_NEW_FLAG, NULL);
1686 CertAddStoreToCollection(store, regStore,
1687 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1688 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1689 CertCloseStore(regStore, 0);
1690 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1693 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1694 CERT_SYSTEM_STORE_CURRENT_USER)
1696 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1697 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1698 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1699 hCryptProv, dwFlags, pvPara);
1702 CertAddStoreToCollection(store, regStore,
1703 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1704 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1705 CertCloseStore(regStore, 0);
1710 return (PWINECRYPT_CERTSTORE)store;
1713 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1714 DWORD dwFlags, const void *pvPara)
1717 PWINECRYPT_CERTSTORE ret = NULL;
1719 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1720 debugstr_a((LPCSTR)pvPara));
1724 SetLastError(ERROR_FILE_NOT_FOUND);
1727 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1730 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1734 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1735 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1736 CryptMemFree(storeName);
1742 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1744 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1746 TRACE("(%p, %08x)\n", store, dwFlags);
1748 CRYPT_WriteSerializedFile(store->file, store->memStore);
1749 CertCloseStore(store->memStore, dwFlags);
1750 CloseHandle(store->file);
1751 CryptMemFree(store);
1754 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1755 PCCERT_CONTEXT cert, DWORD dwFlags)
1757 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1759 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1760 store->dirty = TRUE;
1764 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1765 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1767 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1769 TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1770 store->dirty = TRUE;
1774 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1775 PCCRL_CONTEXT crl, DWORD dwFlags)
1777 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1779 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1780 store->dirty = TRUE;
1784 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1785 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1787 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1789 TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1790 store->dirty = TRUE;
1794 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1795 DWORD dwCtrlType, void const *pvCtrlPara)
1797 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1800 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1805 case CERT_STORE_CTRL_RESYNC:
1806 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1807 CRYPT_ReadSerializedFile(store->file, store);
1810 case CERT_STORE_CTRL_COMMIT:
1811 if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1813 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1816 else if (store->dirty)
1817 ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1822 FIXME("%d: stub\n", dwCtrlType);
1828 static void *fileProvFuncs[] = {
1829 CRYPT_FileCloseStore,
1830 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1831 CRYPT_FileWriteCert,
1832 CRYPT_FileDeleteCert,
1833 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1834 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1836 CRYPT_FileDeleteCRL,
1837 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1838 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1839 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1840 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1841 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1845 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1846 DWORD dwFlags, const void *pvPara)
1848 PWINECRYPT_CERTSTORE store = NULL;
1849 HANDLE file = (HANDLE)pvPara;
1851 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1855 SetLastError(ERROR_INVALID_HANDLE);
1858 if (dwFlags & CERT_STORE_DELETE_FLAG)
1860 SetLastError(E_INVALIDARG);
1863 if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1864 (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1866 SetLastError(E_INVALIDARG);
1870 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1871 GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1872 GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1874 PWINECRYPT_CERTSTORE memStore;
1876 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1879 if (CRYPT_ReadSerializedFile(file, memStore))
1881 PWINE_FILESTOREINFO info = CryptMemAlloc(
1882 sizeof(WINE_FILESTOREINFO));
1886 CERT_STORE_PROV_INFO provInfo = { 0 };
1888 info->dwOpenFlags = dwFlags;
1889 info->cryptProv = hCryptProv;
1890 info->memStore = memStore;
1892 info->dirty = FALSE;
1893 provInfo.cbSize = sizeof(provInfo);
1894 provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1895 sizeof(fileProvFuncs[0]);
1896 provInfo.rgpvStoreProvFunc = fileProvFuncs;
1897 provInfo.hStoreProv = info;
1898 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1904 TRACE("returning %p\n", store);
1908 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1909 DWORD dwFlags, const void *pvPara)
1911 HCERTSTORE store = 0;
1912 LPCWSTR fileName = (LPCWSTR)pvPara;
1913 DWORD access, create;
1916 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1920 SetLastError(ERROR_PATH_NOT_FOUND);
1924 access = GENERIC_READ;
1925 if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1926 access |= GENERIC_WRITE;
1927 if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1928 create = CREATE_NEW;
1929 else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1930 create = OPEN_EXISTING;
1932 create = OPEN_ALWAYS;
1933 file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1934 FILE_ATTRIBUTE_NORMAL, NULL);
1935 if (file != INVALID_HANDLE_VALUE)
1937 /* FIXME: need to check whether it's a serialized store; if not, fall
1938 * back to a PKCS#7 signed message, then to a single serialized cert.
1940 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1944 return (PWINECRYPT_CERTSTORE)store;
1947 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1948 DWORD dwFlags, const void *pvPara)
1951 PWINECRYPT_CERTSTORE ret = NULL;
1953 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1954 debugstr_a((LPCSTR)pvPara));
1958 SetLastError(ERROR_FILE_NOT_FOUND);
1961 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1964 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1968 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1969 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1970 CryptMemFree(storeName);
1976 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
1977 DWORD dwFlags, const void *pvPara)
1979 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
1980 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
1982 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
1983 debugstr_w((LPCWSTR)pvPara));
1987 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1988 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1991 WINECRYPT_CERTSTORE *hcs;
1992 StoreOpenFunc openFunc = NULL;
1994 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
1995 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1997 if (!HIWORD(lpszStoreProvider))
1999 switch (LOWORD(lpszStoreProvider))
2001 case (int)CERT_STORE_PROV_MEMORY:
2002 openFunc = CRYPT_MemOpenStore;
2004 case (int)CERT_STORE_PROV_FILE:
2005 openFunc = CRYPT_FileOpenStore;
2007 case (int)CERT_STORE_PROV_REG:
2008 openFunc = CRYPT_RegOpenStore;
2010 case (int)CERT_STORE_PROV_FILENAME_A:
2011 openFunc = CRYPT_FileNameOpenStoreA;
2013 case (int)CERT_STORE_PROV_FILENAME_W:
2014 openFunc = CRYPT_FileNameOpenStoreW;
2016 case (int)CERT_STORE_PROV_COLLECTION:
2017 openFunc = CRYPT_CollectionOpenStore;
2019 case (int)CERT_STORE_PROV_SYSTEM_A:
2020 openFunc = CRYPT_SysOpenStoreA;
2022 case (int)CERT_STORE_PROV_SYSTEM_W:
2023 openFunc = CRYPT_SysOpenStoreW;
2025 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
2026 openFunc = CRYPT_SysRegOpenStoreA;
2028 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
2029 openFunc = CRYPT_SysRegOpenStoreW;
2031 case (int)CERT_STORE_PROV_PHYSICAL_W:
2032 openFunc = CRYPT_PhysOpenStoreW;
2035 if (LOWORD(lpszStoreProvider))
2036 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
2039 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
2040 openFunc = CRYPT_MemOpenStore;
2041 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
2042 openFunc = CRYPT_FileOpenStore;
2043 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
2044 openFunc = CRYPT_SysOpenStoreW;
2045 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
2046 openFunc = CRYPT_CollectionOpenStore;
2047 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
2048 openFunc = CRYPT_SysRegOpenStoreW;
2051 FIXME("unimplemented type %s\n", lpszStoreProvider);
2056 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2057 hCryptProv, dwFlags, pvPara);
2059 hcs = openFunc(hCryptProv, dwFlags, pvPara);
2060 return (HCERTSTORE)hcs;
2063 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
2064 LPCSTR szSubSystemProtocol)
2066 if (!szSubSystemProtocol)
2068 SetLastError(E_INVALIDARG);
2071 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2072 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2075 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
2076 LPCWSTR szSubSystemProtocol)
2078 if (!szSubSystemProtocol)
2080 SetLastError(E_INVALIDARG);
2083 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2084 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2087 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2088 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2090 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore,
2091 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2095 #define CertContext_CopyProperties(to, from) \
2096 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2098 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2099 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2100 PCCERT_CONTEXT *ppStoreContext)
2102 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2104 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2106 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2107 dwAddDisposition, ppStoreContext);
2109 /* Weird case to pass a test */
2110 if (dwAddDisposition == 0)
2112 SetLastError(STATUS_ACCESS_VIOLATION);
2115 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2118 DWORD size = sizeof(hashToAdd);
2120 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2124 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2126 existing = CertFindCertificateInStore(hCertStore,
2127 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2132 switch (dwAddDisposition)
2134 case CERT_STORE_ADD_ALWAYS:
2135 toAdd = CertDuplicateCertificateContext(pCertContext);
2137 case CERT_STORE_ADD_NEW:
2140 TRACE("found matching certificate, not adding\n");
2141 SetLastError(CRYPT_E_EXISTS);
2145 toAdd = CertDuplicateCertificateContext(pCertContext);
2147 case CERT_STORE_ADD_REPLACE_EXISTING:
2148 toAdd = CertDuplicateCertificateContext(pCertContext);
2150 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2151 toAdd = CertDuplicateCertificateContext(pCertContext);
2153 CertContext_CopyProperties(toAdd, existing);
2155 case CERT_STORE_ADD_USE_EXISTING:
2158 CertContext_CopyProperties(existing, pCertContext);
2159 *ppStoreContext = CertDuplicateCertificateContext(existing);
2162 toAdd = CertDuplicateCertificateContext(pCertContext);
2165 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2172 ret = store->certs.addContext(store, (void *)toAdd,
2173 (void *)existing, (const void **)ppStoreContext);
2174 else if (ppStoreContext)
2175 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2176 CertFreeCertificateContext(toAdd);
2178 CertFreeCertificateContext(existing);
2180 TRACE("returning %d\n", ret);
2184 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2185 PCCERT_CONTEXT pPrev)
2187 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2190 TRACE("(%p, %p)\n", hCertStore, pPrev);
2193 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2196 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2200 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2204 TRACE("(%p)\n", pCertContext);
2208 else if (!pCertContext->hCertStore)
2211 CertFreeCertificateContext(pCertContext);
2215 PWINECRYPT_CERTSTORE hcs =
2216 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2218 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2221 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2222 CertFreeCertificateContext(pCertContext);
2227 #define CrlContext_CopyProperties(to, from) \
2228 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2230 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2231 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2232 PCCRL_CONTEXT* ppStoreContext)
2234 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2236 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2238 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2239 dwAddDisposition, ppStoreContext);
2241 /* Weird case to pass a test */
2242 if (dwAddDisposition == 0)
2244 SetLastError(STATUS_ACCESS_VIOLATION);
2247 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2249 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2253 switch (dwAddDisposition)
2255 case CERT_STORE_ADD_ALWAYS:
2256 toAdd = CertDuplicateCRLContext(pCrlContext);
2258 case CERT_STORE_ADD_NEW:
2261 TRACE("found matching CRL, not adding\n");
2262 SetLastError(CRYPT_E_EXISTS);
2266 toAdd = CertDuplicateCRLContext(pCrlContext);
2268 case CERT_STORE_ADD_NEWER:
2271 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2272 &pCrlContext->pCrlInfo->ThisUpdate);
2275 toAdd = CertDuplicateCRLContext(pCrlContext);
2278 TRACE("existing CRL is newer, not adding\n");
2279 SetLastError(CRYPT_E_EXISTS);
2284 toAdd = CertDuplicateCRLContext(pCrlContext);
2286 case CERT_STORE_ADD_REPLACE_EXISTING:
2287 toAdd = CertDuplicateCRLContext(pCrlContext);
2289 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2290 toAdd = CertDuplicateCRLContext(pCrlContext);
2292 CrlContext_CopyProperties(toAdd, existing);
2294 case CERT_STORE_ADD_USE_EXISTING:
2296 CrlContext_CopyProperties(existing, pCrlContext);
2299 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2306 ret = store->crls.addContext(store, (void *)toAdd,
2307 (void *)existing, (const void **)ppStoreContext);
2308 else if (ppStoreContext)
2309 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2310 CertFreeCRLContext(toAdd);
2312 CertFreeCRLContext(existing);
2314 TRACE("returning %d\n", ret);
2318 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2322 TRACE("(%p)\n", pCrlContext);
2326 else if (!pCrlContext->hCertStore)
2329 CertFreeCRLContext(pCrlContext);
2333 PWINECRYPT_CERTSTORE hcs =
2334 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2336 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2339 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2340 CertFreeCRLContext(pCrlContext);
2345 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2346 PCCRL_CONTEXT pPrev)
2348 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2351 TRACE("(%p, %p)\n", hCertStore, pPrev);
2354 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2357 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2361 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2362 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2364 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2369 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2370 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2371 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2373 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2374 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2379 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2380 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2381 PCCTL_CONTEXT* ppStoreContext)
2383 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2384 dwAddDisposition, ppStoreContext);
2388 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2390 FIXME("(%p): stub\n", pCtlContext );
2394 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2396 FIXME("(%p): stub\n", pCtlContext );
2400 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2402 FIXME("(%p): stub\n", pCtlContext);
2406 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2407 PCCTL_CONTEXT pPrev)
2409 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2413 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2415 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2417 TRACE("(%p)\n", hCertStore);
2419 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2420 InterlockedIncrement(&hcs->ref);
2424 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2426 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2428 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2433 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2436 if (InterlockedDecrement(&hcs->ref) == 0)
2438 TRACE("%p's ref count is 0, freeing\n", hcs);
2440 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2441 CryptReleaseContext(hcs->cryptProv, 0);
2442 hcs->closeStore(hcs, dwFlags);
2445 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2449 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2450 DWORD dwCtrlType, void const *pvCtrlPara)
2452 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2455 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2460 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2465 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2472 BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2473 void *pvData, DWORD *pcbData)
2475 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2478 TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);
2482 case CERT_ACCESS_STATE_PROP_ID:
2485 *pcbData = sizeof(DWORD);
2488 else if (*pcbData < sizeof(DWORD))
2490 SetLastError(ERROR_MORE_DATA);
2491 *pcbData = sizeof(DWORD);
2497 if (store->type != StoreTypeMem &&
2498 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2499 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2500 *(DWORD *)pvData = state;
2505 if (store->properties)
2507 CRYPT_DATA_BLOB blob;
2509 ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
2514 *pcbData = blob.cbData;
2515 else if (*pcbData < blob.cbData)
2517 SetLastError(ERROR_MORE_DATA);
2518 *pcbData = blob.cbData;
2523 memcpy(pvData, blob.pbData, blob.cbData);
2524 *pcbData = blob.cbData;
2528 SetLastError(CRYPT_E_NOT_FOUND);
2531 SetLastError(CRYPT_E_NOT_FOUND);
2536 BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2537 DWORD dwFlags, const void *pvData)
2539 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2542 TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);
2544 if (!store->properties)
2545 store->properties = ContextPropertyList_Create();
2548 case CERT_ACCESS_STATE_PROP_ID:
2549 SetLastError(E_INVALIDARG);
2554 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvData;
2556 ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
2557 blob->pbData, blob->cbData);
2561 ContextPropertyList_RemoveProperty(store->properties, dwPropId);
2568 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2571 FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2575 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2576 DWORD dwPropId, void *pvData, DWORD *pcbData)
2578 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2582 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2583 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2585 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2590 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2591 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2593 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2594 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2595 PWINE_STORE_LIST_ENTRY entry;
2598 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2599 dwUpdateFlags, dwPriority);
2601 if (!collection || !sibling)
2603 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2605 SetLastError(E_INVALIDARG);
2608 if (collection->hdr.type != StoreTypeCollection)
2610 SetLastError(E_INVALIDARG);
2613 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2615 SetLastError(E_INVALIDARG);
2619 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2622 InterlockedIncrement(&sibling->ref);
2623 TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2624 entry->store = sibling;
2625 entry->dwUpdateFlags = dwUpdateFlags;
2626 entry->dwPriority = dwPriority;
2627 list_init(&entry->entry);
2628 TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2629 EnterCriticalSection(&collection->cs);
2632 PWINE_STORE_LIST_ENTRY cursor;
2635 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2636 WINE_STORE_LIST_ENTRY, entry)
2638 if (cursor->dwPriority < dwPriority)
2640 list_add_before(&cursor->entry, &entry->entry);
2646 list_add_tail(&collection->stores, &entry->entry);
2649 list_add_tail(&collection->stores, &entry->entry);
2650 LeaveCriticalSection(&collection->cs);
2658 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2659 HCERTSTORE hSiblingStore)
2661 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2662 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2663 PWINE_STORE_LIST_ENTRY store, next;
2665 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2667 if (!collection || !sibling)
2669 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2671 SetLastError(E_INVALIDARG);
2674 if (collection->hdr.type != StoreTypeCollection)
2676 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2678 SetLastError(E_INVALIDARG);
2681 EnterCriticalSection(&collection->cs);
2682 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2683 WINE_STORE_LIST_ENTRY, entry)
2685 if (store->store == sibling)
2687 list_remove(&store->entry);
2688 CertCloseStore(store->store, 0);
2689 CryptMemFree(store);
2693 LeaveCriticalSection(&collection->cs);
2696 static LONG CRYPT_OpenParentStore(DWORD dwFlags,
2697 void *pvSystemStoreLocationPara, HKEY *key)
2702 TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);
2704 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
2706 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
2707 root = HKEY_LOCAL_MACHINE;
2708 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2710 case CERT_SYSTEM_STORE_CURRENT_USER:
2711 root = HKEY_CURRENT_USER;
2712 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2714 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
2715 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2716 * SystemCertificates
2718 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
2719 return ERROR_FILE_NOT_FOUND;
2720 case CERT_SYSTEM_STORE_SERVICES:
2721 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2722 * SystemCertificates
2724 FIXME("CERT_SYSTEM_STORE_SERVICES\n");
2725 return ERROR_FILE_NOT_FOUND;
2726 case CERT_SYSTEM_STORE_USERS:
2727 /* hku\user sid\Software\Microsoft\SystemCertificates */
2728 FIXME("CERT_SYSTEM_STORE_USERS\n");
2729 return ERROR_FILE_NOT_FOUND;
2730 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
2731 root = HKEY_CURRENT_USER;
2732 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2734 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
2735 root = HKEY_LOCAL_MACHINE;
2736 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2738 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
2739 /* hklm\Software\Microsoft\EnterpriseCertificates */
2740 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
2741 return ERROR_FILE_NOT_FOUND;
2743 return ERROR_FILE_NOT_FOUND;
2746 return RegOpenKeyExW(root, base, 0, KEY_READ, key);
2749 BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
2750 void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
2756 TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
2759 rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
2763 CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
2767 WCHAR name[MAX_PATH];
2768 DWORD size = sizeof(name) / sizeof(name[0]);
2770 rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
2773 ret = pfnEnum(name, 0, &info, NULL, pvArg);
2774 } while (ret && !rc);
2775 if (ret && rc != ERROR_NO_MORE_ITEMS)