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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * - As you can see in the stubs below, support for CRLs and CTLs is missing.
21 * Mostly this should be copy-paste work, and some code (e.g. extended
22 * properties) could be shared between them.
23 * - The concept of physical stores and locations isn't implemented. (This
24 * doesn't mean registry stores et al aren't implemented. See the PSDK for
25 * registering and enumerating physical stores and locations.)
26 * - Many flags, options and whatnot are unimplemented.
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 /* Some typedefs that make it easier to abstract which type of context we're
50 typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
51 const BYTE *pbCertEncoded, DWORD cbCertEncoded);
52 typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
53 const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
54 typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
55 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
56 DWORD dwAddDisposition, const void **ppContext);
57 typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
58 const void *pPrevContext);
59 typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
60 DWORD dwPropID, void *pvData, DWORD *pcbData);
61 typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
62 DWORD dwPropID, DWORD dwFlags, const void *pvData);
63 typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
64 BYTE *pbElement, DWORD *pcbElement);
65 typedef BOOL (WINAPI *FreeContextFunc)(const void *context);
66 typedef BOOL (WINAPI *DeleteContextFunc)(const void *context);
68 /* An abstract context (certificate, CRL, or CTL) interface */
69 typedef struct _WINE_CONTEXT_INTERFACE
71 CreateContextFunc create;
72 AddContextToStoreFunc addContextToStore;
73 AddEncodedContextToStoreFunc addEncodedToStore;
74 EnumContextsInStoreFunc enumContextsInStore;
75 GetContextPropertyFunc getProp;
76 SetContextPropertyFunc setProp;
77 SerializeElementFunc serialize;
79 DeleteContextFunc deleteFromStore;
80 } WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE;
82 static const WINE_CONTEXT_INTERFACE gCertInterface = {
83 (CreateContextFunc)CertCreateCertificateContext,
84 (AddContextToStoreFunc)CertAddCertificateContextToStore,
85 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
86 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
87 (GetContextPropertyFunc)CertGetCertificateContextProperty,
88 (SetContextPropertyFunc)CertSetCertificateContextProperty,
89 (SerializeElementFunc)CertSerializeCertificateStoreElement,
90 (FreeContextFunc)CertFreeCertificateContext,
91 (DeleteContextFunc)CertDeleteCertificateFromStore,
94 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
95 (CreateContextFunc)CertCreateCRLContext,
96 (AddContextToStoreFunc)CertAddCRLContextToStore,
97 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
98 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
99 (GetContextPropertyFunc)CertGetCRLContextProperty,
100 (SetContextPropertyFunc)CertSetCRLContextProperty,
101 (SerializeElementFunc)CertSerializeCRLStoreElement,
102 (FreeContextFunc)CertFreeCRLContext,
103 (DeleteContextFunc)CertDeleteCRLFromStore,
106 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
107 (CreateContextFunc)CertCreateCTLContext,
108 (AddContextToStoreFunc)CertAddCTLContextToStore,
109 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
110 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
111 (GetContextPropertyFunc)CertGetCTLContextProperty,
112 (SetContextPropertyFunc)CertSetCTLContextProperty,
113 (SerializeElementFunc)CertSerializeCTLStoreElement,
114 (FreeContextFunc)CertFreeCTLContext,
115 (DeleteContextFunc)CertDeleteCTLFromStore,
118 struct WINE_CRYPTCERTSTORE;
120 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
121 DWORD dwFlags, const void *pvPara);
123 struct _WINE_CERT_CONTEXT;
125 /* Called to enumerate the next certificate in a store. */
126 typedef struct _WINE_CERT_CONTEXT * (*EnumCertFunc)
127 (struct WINE_CRYPTCERTSTORE *store, struct _WINE_CERT_CONTEXT *pPrev);
129 /* Called to add a certificate context to a store. If toReplace is not NULL,
130 * context replaces toReplace in the store, and access checks should not be
131 * performed. Otherwise context is a new context, and it should only be
132 * added if the store allows it. If ppStoreContext is not NULL, the added
133 * context should be returned in *ppStoreContext.
135 typedef BOOL (*AddCertFunc)(struct WINE_CRYPTCERTSTORE *store,
136 struct _WINE_CERT_CONTEXT *context, struct _WINE_CERT_CONTEXT *toReplace,
137 PCCERT_CONTEXT *ppStoreContext);
139 typedef enum _CertStoreType {
145 /* A cert store is polymorphic through the use of function pointers. A type
146 * is still needed to distinguish collection stores from other types.
147 * On the function pointers:
148 * - closeStore is called when the store's ref count becomes 0
149 * - control is optional, but should be implemented by any store that supports
152 typedef struct WINE_CRYPTCERTSTORE
157 HCRYPTPROV cryptProv;
159 PFN_CERT_STORE_PROV_CLOSE closeStore;
161 EnumCertFunc enumCert;
162 PFN_CERT_STORE_PROV_DELETE_CERT deleteCert;
163 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
164 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
166 typedef enum _ContextType {
171 /* A certificate context. This is the base type, and the two real types
172 * (data and link) derive from it. Each one can be cast to a PCCERT_CONTEXT.
174 typedef struct _WINE_CERT_CONTEXT
179 } WINE_CERT_CONTEXT, *PWINE_CERT_CONTEXT;
180 typedef const struct _WINE_CERT_CONTEXT *PCWINE_CERT_CONTEXT;
182 typedef struct _WINE_CERT_CONTEXT_DATA
186 ContextType type; /* always ContextTypeData */
187 PCONTEXT_PROPERTY_LIST properties;
188 } WINE_CERT_CONTEXT_DATA, *PWINE_CERT_CONTEXT_DATA;
189 typedef const struct _WINE_CERT_CONTEXT_DATA PCWINE_CERT_CONTEXT_DATA;
191 typedef struct _WINE_CERT_CONTEXT_LINK
195 ContextType type; /* always ContextTypeLink */
196 PWINE_CERT_CONTEXT linked;
197 } WINE_CERT_CONTEXT_LINK, *PWINE_CERT_CONTEXT_LINK;
198 typedef const struct _WINE_CERT_CONTEXT_LINK PCWINE_CERT_CONTEXT_LINK;
200 /* A mem store has a list of these. They're also returned by the mem store
201 * during enumeration.
203 typedef struct _WINE_CERT_LIST_ENTRY
205 WINE_CERT_CONTEXT_LINK cert;
207 } WINE_CERT_LIST_ENTRY, *PWINE_CERT_LIST_ENTRY;
209 typedef struct _WINE_MEMSTORE
211 WINECRYPT_CERTSTORE hdr;
214 } WINE_MEMSTORE, *PWINE_MEMSTORE;
216 typedef struct _WINE_HASH_TO_DELETE
220 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
222 typedef struct _WINE_REGSTOREINFO
225 HCRYPTPROV cryptProv;
226 PWINECRYPT_CERTSTORE memStore;
230 struct list certsToDelete;
231 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
233 typedef struct _WINE_STORE_LIST_ENTRY
235 PWINECRYPT_CERTSTORE store;
239 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
241 /* Returned by a collection store during enumeration.
242 * Note: relies on the list entry being valid after use, which a number of
243 * conditions might make untrue (reentrancy, closing a collection store before
244 * continuing an enumeration on it, ...). The tests seem to indicate this
245 * sort of unsafety is okay, since Windows isn't well-behaved in these
248 typedef struct _WINE_COLLECTION_CERT_CONTEXT
250 WINE_CERT_CONTEXT_LINK cert;
251 PWINE_STORE_LIST_ENTRY storeEntry;
252 } WINE_COLLECTION_CERT_CONTEXT, *PWINE_COLLECTION_CERT_CONTEXT;
254 typedef struct _WINE_COLLECTIONSTORE
256 WINECRYPT_CERTSTORE hdr;
259 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
261 typedef struct _WINE_PROVIDERSTORE
263 WINECRYPT_CERTSTORE hdr;
264 DWORD dwStoreProvFlags;
265 PWINECRYPT_CERTSTORE memStore;
266 HCERTSTOREPROV hStoreProv;
267 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
268 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
269 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
270 PFN_CERT_STORE_PROV_CONTROL provControl;
271 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
273 /* Internal version of CertGetCertificateContextProperty that gets properties
274 * directly from the context (or the context it's linked to, depending on its
275 * type.) Doesn't handle special-case properties, since they are handled by
276 * CertGetCertificateContextProperty, and are particular to the store in which
277 * the property exists (which is separate from the context.)
279 static BOOL WINAPI CertContext_GetProperty(PWINE_CERT_CONTEXT context,
280 DWORD dwPropId, void *pvData, DWORD *pcbData);
282 /* Internal version of CertSetCertificateContextProperty that sets properties
283 * directly on the context (or the context it's linked to, depending on its
284 * type.) Doesn't handle special cases, since they're handled by
285 * CertSetCertificateContextProperty anyway.
287 static BOOL WINAPI CertContext_SetProperty(PWINE_CERT_CONTEXT context,
288 DWORD dwPropId, DWORD dwFlags, const void *pvData);
290 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
291 DWORD dwFlags, CertStoreType type)
294 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
298 hCryptProv = CRYPT_GetDefaultProvider();
299 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
301 store->cryptProv = hCryptProv;
302 store->dwOpenFlags = dwFlags;
305 /* Initializes the reference ref to point to context, and increments context's
306 * reference count. Also sets the hCertStore member of the reference to store.
308 static void CRYPT_InitCertRef(PWINE_CERT_CONTEXT_LINK ref,
309 PWINE_CERT_CONTEXT context, HCERTSTORE store)
311 TRACE("(%p, %p)\n", ref, context);
312 memcpy(&ref->cert, context, sizeof(ref->cert));
314 ref->type = ContextTypeLink;
315 ref->linked = context;
316 InterlockedIncrement(&context->ref);
317 TRACE("%p's ref count is %ld\n", context, context->ref);
318 ref->cert.hCertStore = store;
321 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store,
322 PWINE_CERT_CONTEXT cert, PWINE_CERT_CONTEXT toReplace,
323 PCCERT_CONTEXT *ppStoreContext)
325 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
326 PWINE_CERT_LIST_ENTRY entry = CryptMemAlloc(sizeof(WINE_CERT_LIST_ENTRY));
329 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
333 PWINE_CERT_LIST_ENTRY existing = (PWINE_CERT_LIST_ENTRY)toReplace;
335 TRACE("adding %p\n", entry);
336 CRYPT_InitCertRef(&entry->cert, (PWINE_CERT_CONTEXT)cert, store);
337 EnterCriticalSection(&ms->cs);
340 entry->entry.prev = existing->entry.prev;
341 entry->entry.next = existing->entry.next;
342 entry->entry.prev->next = &entry->entry;
343 entry->entry.next->prev = &entry->entry;
344 existing->entry.prev = existing->entry.next = &existing->entry;
345 CertFreeCertificateContext((PCCERT_CONTEXT)existing);
348 list_add_tail(&ms->certs, &entry->entry);
349 LeaveCriticalSection(&ms->cs);
352 CertDuplicateCertificateContext((PCCERT_CONTEXT)entry);
357 TRACE("returning %d\n", ret);
361 static PWINE_CERT_CONTEXT CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store,
362 PWINE_CERT_CONTEXT pPrev)
364 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
365 PWINE_CERT_LIST_ENTRY prevEntry = (PWINE_CERT_LIST_ENTRY)pPrev;
366 PWINE_CERT_CONTEXT ret;
367 struct list *listNext;
369 TRACE("(%p, %p)\n", store, pPrev);
370 EnterCriticalSection(&ms->cs);
373 listNext = list_next(&ms->certs, &prevEntry->entry);
374 CertFreeCertificateContext((PCCERT_CONTEXT)pPrev);
377 listNext = list_next(&ms->certs, &ms->certs);
379 ret = (PWINE_CERT_CONTEXT)CertDuplicateCertificateContext(
380 (PCCERT_CONTEXT)LIST_ENTRY(listNext, WINE_CERT_LIST_ENTRY, entry));
383 SetLastError(CRYPT_E_NOT_FOUND);
386 LeaveCriticalSection(&ms->cs);
388 TRACE("returning %p\n", ret);
392 static BOOL WINAPI CRYPT_MemDeleteCert(HCERTSTORE hCertStore,
393 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
395 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
396 PWINE_CERT_LIST_ENTRY cert = (PWINE_CERT_LIST_ENTRY)pCertContext;
399 /* The passed-in context is itself a list entry, so just remove it. */
400 EnterCriticalSection(&store->cs);
401 list_remove(&cert->entry);
402 ret = CertFreeCertificateContext(pCertContext);
403 LeaveCriticalSection(&store->cs);
407 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
409 PWINE_CERT_LIST_ENTRY cert, next;
411 EnterCriticalSection(&store->cs);
412 LIST_FOR_EACH_ENTRY_SAFE(cert, next, &store->certs, WINE_CERT_LIST_ENTRY,
415 TRACE("removing %p\n", cert);
416 list_remove(&cert->entry);
417 CertFreeCertificateContext((PCCERT_CONTEXT)cert);
419 LeaveCriticalSection(&store->cs);
422 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
424 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
426 TRACE("(%p, %08lx)\n", store, dwFlags);
428 FIXME("Unimplemented flags: %08lx\n", dwFlags);
430 CRYPT_MemEmptyStore(store);
431 DeleteCriticalSection(&store->cs);
435 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
436 DWORD dwFlags, const void *pvPara)
438 PWINE_MEMSTORE store;
440 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
442 if (dwFlags & CERT_STORE_DELETE_FLAG)
444 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
449 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
452 memset(store, 0, sizeof(WINE_MEMSTORE));
453 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
454 store->hdr.closeStore = CRYPT_MemCloseStore;
455 store->hdr.addCert = CRYPT_MemAddCert;
456 store->hdr.enumCert = CRYPT_MemEnumCert;
457 store->hdr.deleteCert = CRYPT_MemDeleteCert;
458 store->hdr.control = NULL;
459 InitializeCriticalSection(&store->cs);
460 list_init(&store->certs);
463 return (PWINECRYPT_CERTSTORE)store;
466 static PWINE_COLLECTION_CERT_CONTEXT CRYPT_CollectionCreateContextFromChild(
467 PWINE_COLLECTIONSTORE store, PWINE_STORE_LIST_ENTRY storeEntry,
468 PWINE_CERT_CONTEXT child)
470 PWINE_COLLECTION_CERT_CONTEXT ret =
471 CryptMemAlloc(sizeof(WINE_COLLECTION_CERT_CONTEXT));
475 CRYPT_InitCertRef((PWINE_CERT_CONTEXT_LINK)ret, child, store);
476 /* The child has already been addref'd, and CRYPT_InitCertRef does
477 * again, so free child once to get the ref count right. (Not doing so
478 * will leak memory if the caller calls CertFreeCertificateContext
479 * rather than CertEnumCertificatesInStore.)
481 CertFreeCertificateContext((PCCERT_CONTEXT)child);
482 ret->storeEntry = storeEntry;
485 CertFreeCertificateContext((PCCERT_CONTEXT)child);
489 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store,
490 PWINE_CERT_CONTEXT cert, PWINE_CERT_CONTEXT toReplace,
491 PCCERT_CONTEXT *ppStoreContext)
494 PCCERT_CONTEXT childContext = NULL;
495 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
497 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
502 PWINE_COLLECTION_CERT_CONTEXT existing =
503 (PWINE_COLLECTION_CERT_CONTEXT)toReplace;
505 storeEntry = existing->storeEntry;
506 ret = storeEntry->store->addCert(storeEntry->store, cert,
507 existing->cert.linked, &childContext);
511 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
512 PWINE_STORE_LIST_ENTRY entry, next;
514 EnterCriticalSection(&cs->cs);
515 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores,
516 WINE_STORE_LIST_ENTRY, entry)
518 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
521 ret = entry->store->addCert(entry->store, cert, NULL,
526 LeaveCriticalSection(&cs->cs);
528 SetLastError(HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED));
530 if (ppStoreContext && childContext)
533 (PCCERT_CONTEXT)CRYPT_CollectionCreateContextFromChild(
534 (PWINE_COLLECTIONSTORE)store, storeEntry,
535 (PWINE_CERT_CONTEXT)childContext);
537 CertFreeCertificateContext(childContext);
541 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
543 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
544 PWINE_STORE_LIST_ENTRY entry, next;
546 TRACE("(%p, %08lx)\n", store, dwFlags);
548 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
551 TRACE("closing %p\n", entry);
552 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
555 DeleteCriticalSection(&cs->cs);
559 /* Advances a collection enumeration by one cert, if possible, where advancing
561 * - calling the current store's enumeration function once, and returning
562 * the enumerated cert if one is returned
563 * - moving to the next store if the current store has no more items, and
564 * recursively calling itself to get the next item.
565 * Returns NULL if the collection contains no more items or on error.
566 * Assumes the collection store's lock is held.
568 static PWINE_COLLECTION_CERT_CONTEXT CRYPT_CollectionAdvanceEnum(
569 PWINE_COLLECTIONSTORE store, PWINE_STORE_LIST_ENTRY storeEntry,
570 PWINE_COLLECTION_CERT_CONTEXT pPrev)
572 PWINE_COLLECTION_CERT_CONTEXT ret;
573 PWINE_CERT_CONTEXT child;
574 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
576 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
580 /* Ref-counting funny business: "duplicate" (addref) the child, because
581 * the CertFreeCertificateContext(pPrev) below can cause the ref count
582 * to become negative. See comment in
583 * CRYPT_CollectionCreateContextFromChild as well.
585 child = ((PWINE_COLLECTION_CERT_CONTEXT)pPrev)->cert.linked;
586 CertDuplicateCertificateContext((PCCERT_CONTEXT)child);
587 child = storeEntry->store->enumCert((HCERTSTORE)storeEntry->store,
589 CertFreeCertificateContext((PCCERT_CONTEXT)pPrev);
593 child = storeEntry->store->enumCert((HCERTSTORE)storeEntry->store,
596 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child);
601 storeEntry = LIST_ENTRY(storeNext, WINE_STORE_LIST_ENTRY, entry);
602 ret = CRYPT_CollectionAdvanceEnum(store, storeEntry, NULL);
606 SetLastError(CRYPT_E_NOT_FOUND);
610 TRACE("returning %p\n", ret);
614 static PWINE_CERT_CONTEXT CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store,
615 PWINE_CERT_CONTEXT pPrev)
617 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
618 PWINE_COLLECTION_CERT_CONTEXT prevEntry =
619 (PWINE_COLLECTION_CERT_CONTEXT)pPrev, ret;
621 TRACE("(%p, %p)\n", store, pPrev);
625 EnterCriticalSection(&cs->cs);
626 ret = CRYPT_CollectionAdvanceEnum(cs, prevEntry->storeEntry, prevEntry);
627 LeaveCriticalSection(&cs->cs);
631 EnterCriticalSection(&cs->cs);
632 if (!list_empty(&cs->stores))
634 PWINE_STORE_LIST_ENTRY storeEntry;
636 storeEntry = LIST_ENTRY(cs->stores.next, WINE_STORE_LIST_ENTRY,
638 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry, NULL);
642 SetLastError(CRYPT_E_NOT_FOUND);
645 LeaveCriticalSection(&cs->cs);
647 TRACE("returning %p\n", ret);
648 return (PWINE_CERT_CONTEXT)ret;
651 static BOOL WINAPI CRYPT_CollectionDeleteCert(HCERTSTORE hCertStore,
652 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
654 PWINE_COLLECTION_CERT_CONTEXT context =
655 (PWINE_COLLECTION_CERT_CONTEXT)pCertContext;
658 TRACE("(%p, %p, %08lx)\n", hCertStore, pCertContext, dwFlags);
660 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)context->cert.linked);
664 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
665 DWORD dwFlags, const void *pvPara)
667 PWINE_COLLECTIONSTORE store;
669 if (dwFlags & CERT_STORE_DELETE_FLAG)
671 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
676 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
679 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
680 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
681 StoreTypeCollection);
682 store->hdr.closeStore = CRYPT_CollectionCloseStore;
683 store->hdr.addCert = CRYPT_CollectionAddCert;
684 store->hdr.enumCert = CRYPT_CollectionEnumCert;
685 store->hdr.deleteCert = CRYPT_CollectionDeleteCert;
686 InitializeCriticalSection(&store->cs);
687 list_init(&store->stores);
690 return (PWINECRYPT_CERTSTORE)store;
693 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
695 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
697 TRACE("(%p, %08lx)\n", store, dwFlags);
699 if (store->provCloseStore)
700 store->provCloseStore(store->hStoreProv, dwFlags);
701 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
702 CertCloseStore(store->memStore, dwFlags);
706 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store,
707 PWINE_CERT_CONTEXT cert, PWINE_CERT_CONTEXT toReplace,
708 PCCERT_CONTEXT *ppStoreContext)
710 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
713 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
716 ret = ps->memStore->addCert(ps->memStore, cert, toReplace,
720 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
722 SetLastError(ERROR_ACCESS_DENIED);
728 if (ps->provWriteCert)
729 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
730 CERT_STORE_PROV_WRITE_ADD_FLAG);
732 ret = ps->memStore->addCert(ps->memStore, cert, NULL,
736 /* dirty trick: replace the returned context's hCertStore with
740 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
744 static PWINE_CERT_CONTEXT CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store,
745 PWINE_CERT_CONTEXT pPrev)
747 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
748 PWINE_CERT_CONTEXT ret;
750 ret = ps->memStore->enumCert(ps->memStore, pPrev);
753 /* same dirty trick: replace the returned context's hCertStore with
756 ret->cert.hCertStore = store;
761 static BOOL WINAPI CRYPT_ProvDeleteCert(HCERTSTORE hCertStore,
762 PCCERT_CONTEXT cert, DWORD dwFlags)
764 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
767 TRACE("(%p, %p, %08lx)\n", hCertStore, cert, dwFlags);
769 if (store->provDeleteCert)
770 ret = store->provDeleteCert(store->hStoreProv, cert, dwFlags);
772 ret = store->memStore->deleteCert(store->memStore, cert, dwFlags);
776 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
777 DWORD dwCtrlType, void const *pvCtrlPara)
779 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
782 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
785 if (store->provControl)
786 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
791 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
792 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, PCERT_STORE_PROV_INFO pProvInfo)
794 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
795 sizeof(WINE_PROVIDERSTORE));
799 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
801 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
802 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
804 CertCloseStore(memStore, 0);
805 ret->memStore = NULL;
808 ret->memStore = memStore;
809 ret->hStoreProv = pProvInfo->hStoreProv;
810 ret->hdr.closeStore = CRYPT_ProvCloseStore;
811 ret->hdr.addCert = CRYPT_ProvAddCert;
812 ret->hdr.enumCert = CRYPT_ProvEnumCert;
813 ret->hdr.deleteCert = CRYPT_ProvDeleteCert;
814 ret->hdr.control = CRYPT_ProvControl;
815 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
816 ret->provCloseStore =
817 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
819 ret->provCloseStore = NULL;
820 if (pProvInfo->cStoreProvFunc >
821 CERT_STORE_PROV_WRITE_CERT_FUNC)
822 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
823 CERT_STORE_PROV_WRITE_CERT_FUNC];
825 ret->provWriteCert = NULL;
826 if (pProvInfo->cStoreProvFunc >
827 CERT_STORE_PROV_DELETE_CERT_FUNC)
828 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
829 CERT_STORE_PROV_DELETE_CERT_FUNC];
831 ret->provDeleteCert = NULL;
832 if (pProvInfo->cStoreProvFunc >
833 CERT_STORE_PROV_CONTROL_FUNC)
834 ret->provControl = pProvInfo->rgpvStoreProvFunc[
835 CERT_STORE_PROV_CONTROL_FUNC];
837 ret->provControl = NULL;
839 return (PWINECRYPT_CERTSTORE)ret;
842 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
843 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
845 static HCRYPTOIDFUNCSET set = NULL;
846 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
847 HCRYPTOIDFUNCADDR hFunc;
848 PWINECRYPT_CERTSTORE ret = NULL;
851 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
852 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
853 (void **)&provOpenFunc, &hFunc);
856 CERT_STORE_PROV_INFO provInfo = { 0 };
858 provInfo.cbSize = sizeof(provInfo);
859 if (dwFlags & CERT_STORE_DELETE_FLAG)
860 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
861 dwFlags, pvPara, NULL, &provInfo);
866 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
867 CERT_STORE_CREATE_NEW_FLAG, NULL);
870 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
871 dwFlags, pvPara, memStore, &provInfo))
872 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
875 CertCloseStore(memStore, 0);
878 CryptFreeOIDFunctionAddress(hFunc, 0);
881 SetLastError(ERROR_FILE_NOT_FOUND);
885 static void CRYPT_HashToStr(LPBYTE hash, LPWSTR asciiHash)
887 static const WCHAR fmt[] = { '%','0','2','X',0 };
893 for (i = 0; i < 20; i++)
894 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
897 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
899 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
900 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
901 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
903 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store, HKEY key,
908 WCHAR subKeyName[MAX_PATH];
911 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
913 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
919 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
925 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
927 buf = CryptMemAlloc(size);
930 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
937 TRACE("Adding cert with hash %s\n",
938 debugstr_w(subKeyName));
939 context = CRYPT_ReadSerializedElement(buf, size,
940 contextType, &addedType);
943 const WINE_CONTEXT_INTERFACE *contextInterface;
948 case CERT_STORE_CERTIFICATE_CONTEXT:
949 contextInterface = &gCertInterface;
951 case CERT_STORE_CRL_CONTEXT:
952 contextInterface = &gCRLInterface;
954 case CERT_STORE_CTL_CONTEXT:
955 contextInterface = &gCTLInterface;
958 contextInterface = NULL;
960 if (contextInterface)
963 if (contextInterface->getProp(context,
964 CERT_HASH_PROP_ID, hash, &size))
966 WCHAR asciiHash[20 * 2 + 1];
968 CRYPT_HashToStr(hash, asciiHash);
969 TRACE("comparing %s\n",
970 debugstr_w(asciiHash));
971 TRACE("with %s\n", debugstr_w(subKeyName));
972 if (!lstrcmpW(asciiHash, subKeyName))
974 TRACE("hash matches, adding\n");
975 contextInterface->addContextToStore(
976 store->memStore, context,
977 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
980 TRACE("hash doesn't match, ignoring\n");
982 contextInterface->free(context);
990 /* Ignore intermediate errors, continue enumerating */
996 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store)
998 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
999 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1000 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1003 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1008 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1012 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1018 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1019 static BOOL CRYPT_WriteSerializedToReg(HKEY key, LPBYTE hash, LPBYTE buf,
1022 WCHAR asciiHash[20 * 2 + 1];
1027 CRYPT_HashToStr(hash, asciiHash);
1028 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1032 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1033 RegCloseKey(subKey);
1045 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1046 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1048 const void *context = NULL;
1052 context = contextInterface->enumContextsInStore(memStore, context);
1056 DWORD hashSize = sizeof(hash);
1058 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1065 ret = contextInterface->serialize(context, 0, NULL, &size);
1067 buf = CryptMemAlloc(size);
1070 ret = contextInterface->serialize(context, 0, buf, &size);
1072 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1079 } while (ret && context != NULL);
1081 contextInterface->free(context);
1085 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1087 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
1088 static const WINE_CONTEXT_INTERFACE *interfaces[] = { &gCertInterface,
1089 &gCRLInterface, &gCTLInterface };
1090 struct list *listToDelete[] = { &store->certsToDelete, NULL, NULL };
1094 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1097 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1098 KEY_ALL_ACCESS, NULL, &key, NULL);
1102 if (listToDelete[i])
1104 PWINE_HASH_TO_DELETE toDelete, next;
1105 WCHAR asciiHash[20 * 2 + 1];
1107 EnterCriticalSection(&store->cs);
1108 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1109 WINE_HASH_TO_DELETE, entry)
1113 CRYPT_HashToStr(toDelete->hash, asciiHash);
1114 TRACE("Removing %s\n", debugstr_w(asciiHash));
1115 rc = RegDeleteKeyW(key, asciiHash);
1116 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1121 list_remove(&toDelete->entry);
1122 CryptMemFree(toDelete);
1124 LeaveCriticalSection(&store->cs);
1126 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1139 /* If force is true or the registry store is dirty, writes the contents of the
1140 * store to the registry.
1142 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1146 TRACE("(%p, %d)\n", store, force);
1148 if (store->dirty || force)
1149 ret = CRYPT_RegWriteToReg(store);
1155 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1157 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1159 TRACE("(%p, %08lx)\n", store, dwFlags);
1161 FIXME("Unimplemented flags: %08lx\n", dwFlags);
1163 CRYPT_RegFlushStore(store, FALSE);
1164 RegCloseKey(store->key);
1165 DeleteCriticalSection(&store->cs);
1166 CryptMemFree(store);
1169 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1170 PCCERT_CONTEXT cert, DWORD dwFlags)
1172 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1175 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
1177 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1179 store->dirty = TRUE;
1187 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1188 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1190 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1193 TRACE("(%p, %p, %08lx)\n", store, pCertContext, dwFlags);
1195 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1197 SetLastError(ERROR_ACCESS_DENIED);
1202 PWINE_HASH_TO_DELETE toDelete =
1203 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1207 DWORD size = sizeof(toDelete->hash);
1209 ret = CertGetCertificateContextProperty(pCertContext,
1210 CERT_HASH_PROP_ID, toDelete->hash, &size);
1213 EnterCriticalSection(&store->cs);
1214 list_add_tail(&store->certsToDelete, &toDelete->entry);
1215 LeaveCriticalSection(&store->cs);
1219 CryptMemFree(toDelete);
1226 store->dirty = TRUE;
1231 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1232 DWORD dwCtrlType, void const *pvCtrlPara)
1234 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1237 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
1242 case CERT_STORE_CTRL_RESYNC:
1243 CRYPT_RegFlushStore(store, FALSE);
1244 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1245 CRYPT_RegReadFromReg(store);
1248 case CERT_STORE_CTRL_COMMIT:
1249 ret = CRYPT_RegFlushStore(store,
1250 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1253 FIXME("%ld: stub\n", dwCtrlType);
1259 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1260 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1262 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1263 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1266 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1268 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1271 /* Find how many subkeys there are */
1272 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1273 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1277 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1279 /* Name too big: alloc a buffer for it */
1280 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1284 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1287 /* Recursively delete all the subkeys */
1288 for (i = 0; i < dwKeyCount && !dwRet; i++)
1290 dwSize = dwMaxSubkeyLen;
1291 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1294 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1297 if (lpszName != szNameBuf)
1299 /* Free buffer if allocated */
1300 CryptMemFree(lpszName);
1305 RegCloseKey(hSubKey);
1307 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1312 static void *regProvFuncs[] = {
1313 CRYPT_RegCloseStore,
1314 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1316 CRYPT_RegDeleteCert,
1317 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1318 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1319 NULL, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1320 NULL, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1321 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1322 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1323 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1324 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1325 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1329 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1330 DWORD dwFlags, const void *pvPara)
1332 PWINECRYPT_CERTSTORE store = NULL;
1334 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
1336 if (dwFlags & CERT_STORE_DELETE_FLAG)
1338 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1340 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1341 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1342 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1343 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1344 if (rc == ERROR_NO_MORE_ITEMS)
1352 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1353 GetCurrentProcess(), (LPHANDLE)&key,
1354 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1357 PWINECRYPT_CERTSTORE memStore;
1359 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1362 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1363 sizeof(WINE_REGSTOREINFO));
1367 CERT_STORE_PROV_INFO provInfo = { 0 };
1369 regInfo->dwOpenFlags = dwFlags;
1370 regInfo->cryptProv = hCryptProv;
1371 regInfo->memStore = memStore;
1373 InitializeCriticalSection(®Info->cs);
1374 list_init(®Info->certsToDelete);
1375 CRYPT_RegReadFromReg(regInfo);
1376 regInfo->dirty = FALSE;
1377 provInfo.cbSize = sizeof(provInfo);
1378 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1379 sizeof(regProvFuncs[0]);
1380 provInfo.rgpvStoreProvFunc = regProvFuncs;
1381 provInfo.hStoreProv = regInfo;
1382 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1388 TRACE("returning %p\n", store);
1392 /* FIXME: this isn't complete for the Root store, in which the top-level
1393 * self-signed CA certs reside. Adding a cert to the Root store should present
1394 * the user with a dialog indicating the consequences of doing so, and asking
1395 * the user to confirm whether the cert should be added.
1397 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1398 DWORD dwFlags, const void *pvPara)
1400 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1401 LPCWSTR storeName = (LPCWSTR)pvPara;
1403 PWINECRYPT_CERTSTORE store = NULL;
1408 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1409 debugstr_w((LPCWSTR)pvPara));
1413 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1418 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1420 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1421 root = HKEY_LOCAL_MACHINE;
1422 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1424 case CERT_SYSTEM_STORE_CURRENT_USER:
1425 root = HKEY_CURRENT_USER;
1426 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1428 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1429 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1430 * SystemCertificates
1432 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1433 debugstr_w(storeName));
1435 case CERT_SYSTEM_STORE_SERVICES:
1436 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1437 * SystemCertificates
1439 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1440 debugstr_w(storeName));
1442 case CERT_SYSTEM_STORE_USERS:
1443 /* hku\user sid\Software\Microsoft\SystemCertificates */
1444 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1445 debugstr_w(storeName));
1447 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1448 root = HKEY_CURRENT_USER;
1449 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1451 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1452 root = HKEY_LOCAL_MACHINE;
1453 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1455 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1456 /* hklm\Software\Microsoft\EnterpriseCertificates */
1457 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1458 debugstr_w(storeName));
1461 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1465 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1471 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1474 wsprintfW(storePath, fmt, base, storeName);
1475 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1476 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1481 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1483 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1484 disp == REG_OPENED_EXISTING_KEY)
1487 rc = ERROR_FILE_EXISTS;
1492 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1497 CryptMemFree(storePath);
1502 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1503 DWORD dwFlags, const void *pvPara)
1506 PWINECRYPT_CERTSTORE ret = NULL;
1508 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1509 debugstr_a((LPCSTR)pvPara));
1513 SetLastError(ERROR_FILE_NOT_FOUND);
1516 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1519 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1523 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1524 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1525 CryptMemFree(storeName);
1531 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1532 DWORD dwFlags, const void *pvPara)
1534 HCERTSTORE store = 0;
1537 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1538 debugstr_w((LPCWSTR)pvPara));
1542 SetLastError(ERROR_FILE_NOT_FOUND);
1545 /* This returns a different error than system registry stores if the
1546 * location is invalid.
1548 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1550 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1551 case CERT_SYSTEM_STORE_CURRENT_USER:
1552 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1553 case CERT_SYSTEM_STORE_SERVICES:
1554 case CERT_SYSTEM_STORE_USERS:
1555 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1556 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1557 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1561 SetLastError(ERROR_FILE_NOT_FOUND);
1566 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1567 0, hCryptProv, dwFlags, pvPara);
1571 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1572 CERT_STORE_CREATE_NEW_FLAG, NULL);
1573 CertAddStoreToCollection(store, regStore,
1574 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1575 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1576 CertCloseStore(regStore, 0);
1577 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1580 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1581 CERT_SYSTEM_STORE_CURRENT_USER)
1583 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1584 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1585 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1586 hCryptProv, dwFlags, pvPara);
1589 CertAddStoreToCollection(store, regStore,
1590 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1591 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1592 CertCloseStore(regStore, 0);
1597 return (PWINECRYPT_CERTSTORE)store;
1600 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1601 DWORD dwFlags, const void *pvPara)
1604 PWINECRYPT_CERTSTORE ret = NULL;
1606 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1607 debugstr_a((LPCSTR)pvPara));
1611 SetLastError(ERROR_FILE_NOT_FOUND);
1614 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1617 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1621 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1622 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1623 CryptMemFree(storeName);
1629 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1630 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1633 WINECRYPT_CERTSTORE *hcs;
1634 StoreOpenFunc openFunc = NULL;
1636 TRACE("(%s, %08lx, %08lx, %08lx, %p)\n", debugstr_a(lpszStoreProvider),
1637 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1639 if (!HIWORD(lpszStoreProvider))
1641 switch (LOWORD(lpszStoreProvider))
1643 case (int)CERT_STORE_PROV_MEMORY:
1644 openFunc = CRYPT_MemOpenStore;
1646 case (int)CERT_STORE_PROV_REG:
1647 openFunc = CRYPT_RegOpenStore;
1649 case (int)CERT_STORE_PROV_COLLECTION:
1650 openFunc = CRYPT_CollectionOpenStore;
1652 case (int)CERT_STORE_PROV_SYSTEM_A:
1653 openFunc = CRYPT_SysOpenStoreA;
1655 case (int)CERT_STORE_PROV_SYSTEM_W:
1656 openFunc = CRYPT_SysOpenStoreW;
1658 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
1659 openFunc = CRYPT_SysRegOpenStoreA;
1661 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
1662 openFunc = CRYPT_SysRegOpenStoreW;
1665 if (LOWORD(lpszStoreProvider))
1666 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
1669 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
1670 openFunc = CRYPT_MemOpenStore;
1671 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
1672 openFunc = CRYPT_SysOpenStoreW;
1673 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
1674 openFunc = CRYPT_CollectionOpenStore;
1675 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
1676 openFunc = CRYPT_SysRegOpenStoreW;
1679 FIXME("unimplemented type %s\n", lpszStoreProvider);
1684 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
1685 hCryptProv, dwFlags, pvPara);
1687 hcs = openFunc(hCryptProv, dwFlags, pvPara);
1688 return (HCERTSTORE)hcs;
1691 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
1692 LPCSTR szSubSystemProtocol)
1694 if (!szSubSystemProtocol)
1696 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1699 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
1700 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1703 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
1704 LPCWSTR szSubSystemProtocol)
1706 if (!szSubSystemProtocol)
1708 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1711 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
1712 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1715 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
1716 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
1718 FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore,
1719 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
1723 PCCRL_CONTEXT WINAPI CertCreateCRLContext( DWORD dwCertEncodingType,
1724 const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
1729 TRACE("%08lx %p %08lx\n", dwCertEncodingType, pbCrlEncoded, cbCrlEncoded);
1731 /* FIXME: semi-stub, need to use CryptDecodeObjectEx to decode the CRL. */
1732 pcrl = CryptMemAlloc( sizeof (CRL_CONTEXT) );
1736 data = CryptMemAlloc( cbCrlEncoded );
1739 CryptMemFree( pcrl );
1743 pcrl->dwCertEncodingType = dwCertEncodingType;
1744 pcrl->pbCrlEncoded = data;
1745 pcrl->cbCrlEncoded = cbCrlEncoded;
1746 pcrl->pCrlInfo = NULL;
1747 pcrl->hCertStore = 0;
1752 /* Decodes the encoded certificate and creates the certificate context for it.
1754 static PWINE_CERT_CONTEXT CRYPT_CreateCertificateContext(
1755 DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded)
1757 PWINE_CERT_CONTEXT_DATA cert = NULL;
1759 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
1760 PCERT_INFO certInfo = NULL;
1763 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCertEncoded,
1766 /* First try to decode it as a signed cert. */
1767 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT, pbCertEncoded,
1768 cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&signedCert, &size);
1772 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1773 signedCert->ToBeSigned.pbData, signedCert->ToBeSigned.cbData,
1774 CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&certInfo, &size);
1775 LocalFree(signedCert);
1777 /* Failing that, try it as an unsigned cert */
1781 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1782 pbCertEncoded, cbCertEncoded,
1783 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
1784 (BYTE *)&certInfo, &size);
1790 cert = CryptMemAlloc(sizeof(WINE_CERT_CONTEXT_DATA));
1793 data = CryptMemAlloc(cbCertEncoded);
1800 memcpy(data, pbCertEncoded, cbCertEncoded);
1801 cert->cert.dwCertEncodingType = dwCertEncodingType;
1802 cert->cert.pbCertEncoded = data;
1803 cert->cert.cbCertEncoded = cbCertEncoded;
1804 cert->cert.pCertInfo = certInfo;
1805 cert->cert.hCertStore = 0;
1807 cert->type = ContextTypeData;
1808 cert->properties = ContextPropertyList_Create();
1812 return (PWINE_CERT_CONTEXT)cert;
1815 PCCERT_CONTEXT WINAPI CertCreateCertificateContext(DWORD dwCertEncodingType,
1816 const BYTE *pbCertEncoded, DWORD cbCertEncoded)
1818 PWINE_CERT_CONTEXT cert;
1820 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCertEncoded,
1823 cert = CRYPT_CreateCertificateContext(dwCertEncodingType, pbCertEncoded,
1825 return (PCCERT_CONTEXT)cert;
1828 /* If context is a link, follows it to its linked context (recursively, if
1829 * necessary) and returns the data context associated with the link.
1830 * Otherwise just returns context.
1832 static inline PWINE_CERT_CONTEXT_DATA CertContext_GetDataContext(
1833 PWINE_CERT_CONTEXT context)
1835 PWINE_CERT_CONTEXT ptr = context;
1837 while (ptr && ptr->type == ContextTypeLink)
1838 ptr = ((PWINE_CERT_CONTEXT_LINK)ptr)->linked;
1839 return (ptr && ptr->type == ContextTypeData) ?
1840 (PWINE_CERT_CONTEXT_DATA)ptr : NULL;
1843 DWORD WINAPI CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext,
1846 PWINE_CERT_CONTEXT_DATA linked = CertContext_GetDataContext(
1847 (PWINE_CERT_CONTEXT)pCertContext);
1850 TRACE("(%p, %ld)\n", pCertContext, dwPropId);
1853 ret = ContextPropertyList_EnumPropIDs(linked->properties, dwPropId);
1859 static BOOL CertContext_GetHashProp(PWINE_CERT_CONTEXT context, DWORD dwPropId,
1860 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
1863 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
1867 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
1869 ret = CertContext_SetProperty(context, dwPropId, 0, &blob);
1874 static BOOL WINAPI CertContext_GetProperty(PWINE_CERT_CONTEXT context,
1875 DWORD dwPropId, void *pvData, DWORD *pcbData)
1877 PWINE_CERT_CONTEXT_DATA linked = CertContext_GetDataContext(context);
1879 CRYPT_DATA_BLOB blob;
1881 TRACE("(%p, %ld, %p, %p)\n", context, dwPropId, pvData, pcbData);
1884 ret = ContextPropertyList_FindProperty(linked->properties, dwPropId,
1892 *pcbData = blob.cbData;
1895 else if (*pcbData < blob.cbData)
1897 SetLastError(ERROR_MORE_DATA);
1898 *pcbData = blob.cbData;
1902 memcpy(pvData, blob.pbData, blob.cbData);
1903 *pcbData = blob.cbData;
1909 /* Implicit properties */
1912 case CERT_SHA1_HASH_PROP_ID:
1913 ret = CertContext_GetHashProp(context, dwPropId, CALG_SHA1,
1914 context->cert.pbCertEncoded, context->cert.cbCertEncoded, pvData,
1917 case CERT_MD5_HASH_PROP_ID:
1918 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1919 context->cert.pbCertEncoded, context->cert.cbCertEncoded, pvData,
1922 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
1923 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1924 context->cert.pCertInfo->Subject.pbData,
1925 context->cert.pCertInfo->Subject.cbData,
1928 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
1929 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1930 context->cert.pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
1931 context->cert.pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
1934 case CERT_SIGNATURE_HASH_PROP_ID:
1935 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID:
1936 FIXME("implicit property %ld\n", dwPropId);
1937 SetLastError(CRYPT_E_NOT_FOUND);
1940 SetLastError(CRYPT_E_NOT_FOUND);
1943 TRACE("returning %d\n", ret);
1947 /* info is assumed to be a CRYPT_KEY_PROV_INFO, followed by its container name,
1948 * provider name, and any provider parameters, in a contiguous buffer, but
1949 * where info's pointers are assumed to be invalid. Upon return, info's
1950 * pointers point to the appropriate memory locations.
1952 static void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info)
1954 DWORD i, containerLen, provNameLen;
1955 LPBYTE data = (LPBYTE)info + sizeof(CRYPT_KEY_PROV_INFO);
1957 info->pwszContainerName = (LPWSTR)data;
1958 containerLen = (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
1959 data += containerLen;
1961 info->pwszProvName = (LPWSTR)data;
1962 provNameLen = (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
1963 data += provNameLen;
1965 info->rgProvParam = (PCRYPT_KEY_PROV_PARAM)data;
1966 data += info->cProvParam * sizeof(CRYPT_KEY_PROV_PARAM);
1968 for (i = 0; i < info->cProvParam; i++)
1970 info->rgProvParam[i].pbData = data;
1971 data += info->rgProvParam[i].cbData;
1975 BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
1976 DWORD dwPropId, void *pvData, DWORD *pcbData)
1980 TRACE("(%p, %ld, %p, %p)\n", pCertContext, dwPropId, pvData, pcbData);
1985 case CERT_CERT_PROP_ID:
1986 case CERT_CRL_PROP_ID:
1987 case CERT_CTL_PROP_ID:
1988 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1991 case CERT_ACCESS_STATE_PROP_ID:
1994 *pcbData = sizeof(DWORD);
1997 else if (*pcbData < sizeof(DWORD))
1999 SetLastError(ERROR_MORE_DATA);
2000 *pcbData = sizeof(DWORD);
2007 if (pCertContext->hCertStore)
2009 PWINECRYPT_CERTSTORE store =
2010 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2012 if (!(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2013 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2015 *(DWORD *)pvData = state;
2019 case CERT_KEY_PROV_INFO_PROP_ID:
2020 ret = CertContext_GetProperty((PWINE_CERT_CONTEXT)pCertContext,
2021 dwPropId, pvData, pcbData);
2023 CRYPT_FixKeyProvInfoPointers((PCRYPT_KEY_PROV_INFO)pvData);
2026 ret = CertContext_GetProperty((PWINE_CERT_CONTEXT)pCertContext,
2027 dwPropId, pvData, pcbData);
2030 TRACE("returning %d\n", ret);
2034 /* Copies key provider info from from into to, where to is assumed to be a
2035 * contiguous buffer of memory large enough for from and all its associated
2036 * data, but whose pointers are uninitialized.
2037 * Upon return, to contains a contiguous copy of from, packed in the following
2039 * - CRYPT_KEY_PROV_INFO
2040 * - pwszContainerName
2042 * - rgProvParam[0]...
2044 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to,
2045 PCRYPT_KEY_PROV_INFO from)
2048 LPBYTE nextData = (LPBYTE)to + sizeof(CRYPT_KEY_PROV_INFO);
2050 to->pwszContainerName = (LPWSTR)nextData;
2051 lstrcpyW(to->pwszContainerName, from->pwszContainerName);
2052 nextData += (lstrlenW(from->pwszContainerName) + 1) * sizeof(WCHAR);
2053 to->pwszProvName = (LPWSTR)nextData;
2054 lstrcpyW(to->pwszProvName, from->pwszProvName);
2055 nextData += (lstrlenW(from->pwszProvName) + 1) * sizeof(WCHAR);
2056 to->dwProvType = from->dwProvType;
2057 to->dwFlags = from->dwFlags;
2058 to->cProvParam = from->cProvParam;
2059 to->rgProvParam = (PCRYPT_KEY_PROV_PARAM)nextData;
2060 nextData += to->cProvParam * sizeof(CRYPT_KEY_PROV_PARAM);
2061 to->dwKeySpec = from->dwKeySpec;
2062 for (i = 0; i < to->cProvParam; i++)
2064 memcpy(&to->rgProvParam[i], &from->rgProvParam[i],
2065 sizeof(CRYPT_KEY_PROV_PARAM));
2066 to->rgProvParam[i].pbData = nextData;
2067 memcpy(to->rgProvParam[i].pbData, from->rgProvParam[i].pbData,
2068 from->rgProvParam[i].cbData);
2069 nextData += from->rgProvParam[i].cbData;
2073 static BOOL CertContext_SetKeyProvInfoProperty(PWINE_CERT_CONTEXT_DATA linked,
2074 PCRYPT_KEY_PROV_INFO info)
2078 DWORD size = sizeof(CRYPT_KEY_PROV_INFO), i, containerSize, provNameSize;
2080 containerSize = (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
2081 provNameSize = (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
2082 size += containerSize + provNameSize;
2083 for (i = 0; i < info->cProvParam; i++)
2084 size += sizeof(CRYPT_KEY_PROV_PARAM) + info->rgProvParam[i].cbData;
2085 buf = CryptMemAlloc(size);
2088 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO)buf, info);
2089 ret = ContextPropertyList_SetProperty(linked->properties,
2090 CERT_KEY_PROV_INFO_PROP_ID, buf, size);
2098 static BOOL WINAPI CertContext_SetProperty(PWINE_CERT_CONTEXT context,
2099 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2101 PWINE_CERT_CONTEXT_DATA linked = CertContext_GetDataContext(context);
2104 TRACE("(%p, %ld, %08lx, %p)\n", context, dwPropId, dwFlags, pvData);
2110 ContextPropertyList_RemoveProperty(linked->properties, dwPropId);
2117 case CERT_AUTO_ENROLL_PROP_ID:
2118 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
2119 case CERT_DESCRIPTION_PROP_ID:
2120 case CERT_FRIENDLY_NAME_PROP_ID:
2121 case CERT_HASH_PROP_ID:
2122 case CERT_KEY_IDENTIFIER_PROP_ID:
2123 case CERT_MD5_HASH_PROP_ID:
2124 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
2125 case CERT_PUBKEY_ALG_PARA_PROP_ID:
2126 case CERT_PVK_FILE_PROP_ID:
2127 case CERT_SIGNATURE_HASH_PROP_ID:
2128 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
2129 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
2130 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
2131 case CERT_ENROLLMENT_PROP_ID:
2132 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
2133 case CERT_RENEWAL_PROP_ID:
2135 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
2137 ret = ContextPropertyList_SetProperty(linked->properties, dwPropId,
2138 blob->pbData, blob->cbData);
2141 case CERT_DATE_STAMP_PROP_ID:
2142 ret = ContextPropertyList_SetProperty(linked->properties, dwPropId,
2143 (LPBYTE)pvData, sizeof(FILETIME));
2145 case CERT_KEY_PROV_INFO_PROP_ID:
2146 ret = CertContext_SetKeyProvInfoProperty(linked,
2147 (PCRYPT_KEY_PROV_INFO)pvData);
2150 FIXME("%ld: stub\n", dwPropId);
2154 TRACE("returning %d\n", ret);
2158 BOOL WINAPI CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
2159 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2163 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext, dwPropId, dwFlags, pvData);
2165 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
2166 * crashes on most of these, I'll be safer.
2171 case CERT_ACCESS_STATE_PROP_ID:
2172 case CERT_CERT_PROP_ID:
2173 case CERT_CRL_PROP_ID:
2174 case CERT_CTL_PROP_ID:
2175 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2178 ret = CertContext_SetProperty((PWINE_CERT_CONTEXT)pCertContext, dwPropId,
2180 TRACE("returning %d\n", ret);
2184 PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(
2185 PCCERT_CONTEXT pCertContext)
2187 PWINE_CERT_CONTEXT context = (PWINE_CERT_CONTEXT)pCertContext;
2189 TRACE("(%p)\n", pCertContext);
2190 InterlockedIncrement(&context->ref);
2191 return pCertContext;
2194 static void CertContext_CopyProperties(PCCERT_CONTEXT to, PCCERT_CONTEXT from)
2196 PWINE_CERT_CONTEXT_DATA toData, fromData;
2198 toData = CertContext_GetDataContext((PWINE_CERT_CONTEXT)to);
2199 fromData = CertContext_GetDataContext((PWINE_CERT_CONTEXT)from);
2200 ContextPropertyList_Copy(toData->properties, fromData->properties);
2203 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2204 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2205 PCCERT_CONTEXT *ppStoreContext)
2207 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2209 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2211 TRACE("(%p, %p, %08lx, %p)\n", hCertStore, pCertContext,
2212 dwAddDisposition, ppStoreContext);
2214 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2217 DWORD size = sizeof(hashToAdd);
2219 ret = CertContext_GetProperty((PWINE_CERT_CONTEXT)pCertContext,
2220 CERT_HASH_PROP_ID, hashToAdd, &size);
2223 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2225 existing = CertFindCertificateInStore(hCertStore,
2226 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2231 switch (dwAddDisposition)
2233 case CERT_STORE_ADD_ALWAYS:
2234 toAdd = CertDuplicateCertificateContext(pCertContext);
2236 case CERT_STORE_ADD_NEW:
2239 TRACE("found matching certificate, not adding\n");
2240 SetLastError(CRYPT_E_EXISTS);
2244 toAdd = CertDuplicateCertificateContext(pCertContext);
2246 case CERT_STORE_ADD_REPLACE_EXISTING:
2247 toAdd = CertDuplicateCertificateContext(pCertContext);
2249 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2250 toAdd = CertDuplicateCertificateContext(pCertContext);
2252 CertContext_CopyProperties(toAdd, existing);
2254 case CERT_STORE_ADD_USE_EXISTING:
2256 CertContext_CopyProperties(existing, pCertContext);
2259 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition);
2265 ret = store->addCert(store, (PWINE_CERT_CONTEXT)toAdd,
2266 (PWINE_CERT_CONTEXT)existing, ppStoreContext);
2267 CertFreeCertificateContext(toAdd);
2269 CertFreeCertificateContext(existing);
2271 TRACE("returning %d\n", ret);
2275 BOOL WINAPI CertAddEncodedCertificateToStore(HCERTSTORE hCertStore,
2276 DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded,
2277 DWORD dwAddDisposition, PCCERT_CONTEXT *ppCertContext)
2279 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2282 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore, dwCertEncodingType,
2283 pbCertEncoded, cbCertEncoded, dwAddDisposition, ppCertContext);
2287 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2291 PWINE_CERT_CONTEXT cert = CRYPT_CreateCertificateContext(
2292 dwCertEncodingType, pbCertEncoded, cbCertEncoded);
2296 ret = CertAddCertificateContextToStore(hCertStore,
2297 (PCCERT_CONTEXT)cert, dwAddDisposition, ppCertContext);
2298 CertFreeCertificateContext((PCCERT_CONTEXT)cert);
2306 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2307 PCCERT_CONTEXT pPrev)
2309 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2312 TRACE("(%p, %p)\n", hCertStore, pPrev);
2315 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2318 ret = (PCCERT_CONTEXT)hcs->enumCert(hcs, (PWINE_CERT_CONTEXT)pPrev);
2322 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2326 TRACE("(%p)\n", pCertContext);
2330 else if (!pCertContext->hCertStore)
2333 CertFreeCertificateContext(pCertContext);
2337 PWINECRYPT_CERTSTORE hcs =
2338 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2340 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2343 ret = hcs->deleteCert(hcs, pCertContext, 0);
2344 CertFreeCertificateContext(pCertContext);
2349 BOOL WINAPI CertAddEncodedCRLToStore(HCERTSTORE hCertStore,
2350 DWORD dwCertEncodingType, const BYTE *pbCrlEncoded, DWORD cbCrlEncoded,
2351 DWORD dwAddDisposition, PCCRL_CONTEXT *ppCrlContext)
2353 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2354 dwCertEncodingType, pbCrlEncoded, cbCrlEncoded, dwAddDisposition,
2359 BOOL WINAPI CertAddCRLContextToStore( HCERTSTORE hCertStore,
2360 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2361 PCCRL_CONTEXT* ppStoreContext )
2363 FIXME("%p %p %08lx %p\n", hCertStore, pCrlContext,
2364 dwAddDisposition, ppStoreContext);
2368 BOOL WINAPI CertFreeCRLContext( PCCRL_CONTEXT pCrlContext)
2370 FIXME("%p\n", pCrlContext );
2375 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2377 FIXME("(%p): stub\n", pCrlContext);
2381 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2382 PCCRL_CONTEXT pPrev)
2384 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2388 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2389 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2391 FIXME("(%08lx, %p, %08lx): stub\n", dwCertEncodingType, pbCtlEncoded,
2396 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2397 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2398 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2400 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2401 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2406 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2407 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2408 PCCTL_CONTEXT* ppStoreContext)
2410 FIXME("(%p, %p, %08lx, %p): stub\n", hCertStore, pCtlContext,
2411 dwAddDisposition, ppStoreContext);
2415 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2417 FIXME("(%p): stub\n", pCtlContext );
2421 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2423 FIXME("(%p): stub\n", pCtlContext);
2427 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2428 PCCTL_CONTEXT pPrev)
2430 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2434 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2436 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2438 TRACE("(%p)\n", hCertStore);
2440 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2441 InterlockedIncrement(&hcs->ref);
2445 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2447 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2449 TRACE("(%p, %08lx)\n", hCertStore, dwFlags);
2454 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2457 if (InterlockedDecrement(&hcs->ref) == 0)
2459 TRACE("%p's ref count is 0, freeing\n", hcs);
2461 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2462 CryptReleaseContext(hcs->cryptProv, 0);
2463 hcs->closeStore(hcs, dwFlags);
2466 TRACE("%p's ref count is %ld\n", hcs, hcs->ref);
2470 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2471 DWORD dwCtrlType, void const *pvCtrlPara)
2473 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2476 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
2481 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2486 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2493 BOOL WINAPI CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2494 DWORD dwPropId, void *pvData, DWORD *pcbData)
2496 FIXME("(%p, %ld, %p, %p): stub\n", pCRLContext, dwPropId, pvData, pcbData);
2500 BOOL WINAPI CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2501 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2503 FIXME("(%p, %ld, %08lx, %p): stub\n", pCRLContext, dwPropId, dwFlags,
2508 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2509 DWORD dwPropId, void *pvData, DWORD *pcbData)
2511 FIXME("(%p, %ld, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2515 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2516 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2518 FIXME("(%p, %ld, %08lx, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2523 static void CertDataContext_Free(PWINE_CERT_CONTEXT_DATA context)
2525 CryptMemFree(context->cert.pbCertEncoded);
2526 LocalFree(context->cert.pCertInfo);
2527 ContextPropertyList_Free(context->properties);
2528 CryptMemFree(context);
2531 static void CertLinkContext_Free(PWINE_CERT_CONTEXT_LINK context)
2533 CertFreeCertificateContext((PCCERT_CONTEXT)context->linked);
2534 CryptMemFree(context);
2537 static void CertContext_Release(PWINE_CERT_CONTEXT context)
2539 if (InterlockedDecrement(&context->ref) == 0)
2541 TRACE("freeing %p\n", context);
2542 switch (context->type)
2544 case ContextTypeData:
2545 CertDataContext_Free((PWINE_CERT_CONTEXT_DATA)context);
2547 case ContextTypeLink:
2548 CertLinkContext_Free((PWINE_CERT_CONTEXT_LINK)context);
2555 TRACE("%p's ref count is %ld\n", context, context->ref);
2558 BOOL WINAPI CertFreeCertificateContext(PCCERT_CONTEXT pCertContext)
2560 TRACE("(%p)\n", pCertContext);
2563 CertContext_Release((PWINE_CERT_CONTEXT)pCertContext);
2567 typedef BOOL (*CertCompareFunc)(PCCERT_CONTEXT pCertContext, DWORD dwType,
2568 DWORD dwFlags, const void *pvPara);
2570 static BOOL compare_cert_any(PCCERT_CONTEXT pCertContext, DWORD dwType,
2571 DWORD dwFlags, const void *pvPara)
2576 static BOOL compare_cert_by_md5_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
2577 DWORD dwFlags, const void *pvPara)
2581 DWORD size = sizeof(hash);
2583 ret = CertGetCertificateContextProperty(pCertContext,
2584 CERT_MD5_HASH_PROP_ID, hash, &size);
2587 const CRYPT_HASH_BLOB *pHash = (const CRYPT_HASH_BLOB *)pvPara;
2589 if (size == pHash->cbData)
2590 ret = !memcmp(pHash->pbData, hash, size);
2597 static BOOL compare_cert_by_sha1_hash(PCCERT_CONTEXT pCertContext, DWORD dwType,
2598 DWORD dwFlags, const void *pvPara)
2602 DWORD size = sizeof(hash);
2604 ret = CertGetCertificateContextProperty(pCertContext,
2605 CERT_SHA1_HASH_PROP_ID, hash, &size);
2608 const CRYPT_HASH_BLOB *pHash = (const CRYPT_HASH_BLOB *)pvPara;
2610 if (size == pHash->cbData)
2611 ret = !memcmp(pHash->pbData, hash, size);
2618 static BOOL compare_cert_by_name(PCCERT_CONTEXT pCertContext, DWORD dwType,
2619 DWORD dwFlags, const void *pvPara)
2621 const CERT_NAME_BLOB *blob = (const CERT_NAME_BLOB *)pvPara, *toCompare;
2624 if (dwType & CERT_INFO_SUBJECT_FLAG)
2625 toCompare = &pCertContext->pCertInfo->Subject;
2627 toCompare = &pCertContext->pCertInfo->Issuer;
2628 if (toCompare->cbData == blob->cbData)
2629 ret = !memcmp(toCompare->pbData, blob->pbData, blob->cbData);
2635 static BOOL compare_cert_by_subject_cert(PCCERT_CONTEXT pCertContext,
2636 DWORD dwType, DWORD dwFlags, const void *pvPara)
2638 const CERT_INFO *pCertInfo = (const CERT_INFO *)pvPara;
2641 if (pCertInfo->Issuer.cbData == pCertContext->pCertInfo->Subject.cbData)
2642 ret = !memcmp(pCertInfo->Issuer.pbData,
2643 pCertContext->pCertInfo->Subject.pbData, pCertInfo->Issuer.cbData);
2649 PCCERT_CONTEXT WINAPI CertFindCertificateInStore(HCERTSTORE hCertStore,
2650 DWORD dwCertEncodingType, DWORD dwFlags, DWORD dwType,
2651 const void *pvPara, PCCERT_CONTEXT pPrevCertContext)
2654 CertCompareFunc compare;
2656 TRACE("(%p, %ld, %ld, %ld, %p, %p)\n", hCertStore, dwCertEncodingType,
2657 dwFlags, dwType, pvPara, pPrevCertContext);
2659 switch (dwType >> CERT_COMPARE_SHIFT)
2661 case CERT_COMPARE_ANY:
2662 compare = compare_cert_any;
2664 case CERT_COMPARE_MD5_HASH:
2665 compare = compare_cert_by_md5_hash;
2667 case CERT_COMPARE_SHA1_HASH:
2668 compare = compare_cert_by_sha1_hash;
2670 case CERT_COMPARE_NAME:
2671 compare = compare_cert_by_name;
2673 case CERT_COMPARE_SUBJECT_CERT:
2674 compare = compare_cert_by_subject_cert;
2677 FIXME("find type %08lx unimplemented\n", dwType);
2683 BOOL matches = FALSE;
2685 ret = pPrevCertContext;
2687 ret = CertEnumCertificatesInStore(hCertStore, ret);
2689 matches = compare(ret, dwType, dwFlags, pvPara);
2690 } while (ret != NULL && !matches);
2692 SetLastError(CRYPT_E_NOT_FOUND);
2696 SetLastError(CRYPT_E_NOT_FOUND);
2702 PCCERT_CONTEXT WINAPI CertGetSubjectCertificateFromStore(HCERTSTORE hCertStore,
2703 DWORD dwCertEncodingType, PCERT_INFO pCertId)
2705 TRACE("(%p, %08lx, %p)\n", hCertStore, dwCertEncodingType, pCertId);
2709 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2712 return CertFindCertificateInStore(hCertStore, dwCertEncodingType, 0,
2713 CERT_FIND_SUBJECT_CERT, pCertId, NULL);
2716 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2717 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2719 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2720 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2721 PWINE_STORE_LIST_ENTRY entry;
2724 TRACE("(%p, %p, %08lx, %ld)\n", hCollectionStore, hSiblingStore,
2725 dwUpdateFlags, dwPriority);
2727 if (!collection || !sibling)
2729 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2731 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2734 if (collection->hdr.type != StoreTypeCollection)
2736 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2739 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2741 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2745 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2748 InterlockedIncrement(&sibling->ref);
2749 TRACE("sibling %p's ref count is %ld\n", sibling, sibling->ref);
2750 entry->store = sibling;
2751 entry->dwUpdateFlags = dwUpdateFlags;
2752 entry->dwPriority = dwPriority;
2753 list_init(&entry->entry);
2754 TRACE("%p: adding %p, priority %ld\n", collection, entry, dwPriority);
2755 EnterCriticalSection(&collection->cs);
2758 PWINE_STORE_LIST_ENTRY cursor;
2761 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2762 WINE_STORE_LIST_ENTRY, entry)
2764 if (cursor->dwPriority < dwPriority)
2766 list_add_before(&cursor->entry, &entry->entry);
2772 list_add_tail(&collection->stores, &entry->entry);
2775 list_add_tail(&collection->stores, &entry->entry);
2776 LeaveCriticalSection(&collection->cs);
2784 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2785 HCERTSTORE hSiblingStore)
2787 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2788 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2789 PWINE_STORE_LIST_ENTRY store, next;
2791 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2793 if (!collection || !sibling)
2795 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2797 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2800 if (collection->hdr.type != StoreTypeCollection)
2802 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2804 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2807 EnterCriticalSection(&collection->cs);
2808 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2809 WINE_STORE_LIST_ENTRY, entry)
2811 if (store->store == sibling)
2813 list_remove(&store->entry);
2814 CertCloseStore(store->store, 0);
2815 CryptMemFree(store);
2819 LeaveCriticalSection(&collection->cs);