Release 0.9.14.
[wine/multimedia.git] / dlls / crypt32 / store.c
blob160cfddab7404fb0fb8048fc596cb91f4b1a13c7
1 /*
2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2004-2006 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * FIXME:
20 * - 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.
29 #include <assert.h>
30 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "winuser.h"
36 #include "wincrypt.h"
37 #include "wine/debug.h"
38 #include "wine/list.h"
39 #include "excpt.h"
40 #include "wine/exception.h"
41 #include "crypt32_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
45 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
47 static const WINE_CONTEXT_INTERFACE gCertInterface = {
48 (CreateContextFunc)CertCreateCertificateContext,
49 (AddContextToStoreFunc)CertAddCertificateContextToStore,
50 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
51 (DuplicateContextFunc)CertDuplicateCertificateContext,
52 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
53 (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 (GetContextPropertyFunc)CertGetCRLContextProperty,
68 (SetContextPropertyFunc)CertSetCRLContextProperty,
69 (SerializeElementFunc)CertSerializeCRLStoreElement,
70 (FreeContextFunc)CertFreeCRLContext,
71 (DeleteContextFunc)CertDeleteCRLFromStore,
73 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
75 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
76 (CreateContextFunc)CertCreateCTLContext,
77 (AddContextToStoreFunc)CertAddCTLContextToStore,
78 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
79 (DuplicateContextFunc)CertDuplicateCTLContext,
80 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
81 (GetContextPropertyFunc)CertGetCTLContextProperty,
82 (SetContextPropertyFunc)CertSetCTLContextProperty,
83 (SerializeElementFunc)CertSerializeCTLStoreElement,
84 (FreeContextFunc)CertFreeCTLContext,
85 (DeleteContextFunc)CertDeleteCTLFromStore,
87 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
89 struct WINE_CRYPTCERTSTORE;
91 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
92 DWORD dwFlags, const void *pvPara);
94 /* Called to enumerate the next context in a store. */
95 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
97 /* Called to add a context to a store. If toReplace is not NULL,
98 * context replaces toReplace in the store, and access checks should not be
99 * performed. Otherwise context is a new context, and it should only be
100 * added if the store allows it. If ppStoreContext is not NULL, the added
101 * context should be returned in *ppStoreContext.
103 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
104 void *toReplace, const void **ppStoreContext);
106 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
108 typedef struct _CONTEXT_STORE
110 AddFunc addContext;
111 EnumFunc enumContext;
112 DeleteFunc deleteContext;
113 } CONTEXT_STORE, *PCONTEXT_STORE;
115 typedef enum _CertStoreType {
116 StoreTypeMem,
117 StoreTypeCollection,
118 StoreTypeProvider,
119 } CertStoreType;
121 /* A cert store is polymorphic through the use of function pointers. A type
122 * is still needed to distinguish collection stores from other types.
123 * On the function pointers:
124 * - closeStore is called when the store's ref count becomes 0
125 * - control is optional, but should be implemented by any store that supports
126 * persistence
128 typedef struct WINE_CRYPTCERTSTORE
130 DWORD dwMagic;
131 LONG ref;
132 DWORD dwOpenFlags;
133 HCRYPTPROV cryptProv;
134 CertStoreType type;
135 PFN_CERT_STORE_PROV_CLOSE closeStore;
136 CONTEXT_STORE certs;
137 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
138 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
140 typedef struct _WINE_MEMSTORE
142 WINECRYPT_CERTSTORE hdr;
143 struct ContextList *certs;
144 } WINE_MEMSTORE, *PWINE_MEMSTORE;
146 typedef struct _WINE_HASH_TO_DELETE
148 BYTE hash[20];
149 struct list entry;
150 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
152 typedef struct _WINE_REGSTOREINFO
154 DWORD dwOpenFlags;
155 HCRYPTPROV cryptProv;
156 PWINECRYPT_CERTSTORE memStore;
157 HKEY key;
158 BOOL dirty;
159 CRITICAL_SECTION cs;
160 struct list certsToDelete;
161 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
163 typedef struct _WINE_STORE_LIST_ENTRY
165 PWINECRYPT_CERTSTORE store;
166 DWORD dwUpdateFlags;
167 DWORD dwPriority;
168 struct list entry;
169 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
171 typedef struct _WINE_COLLECTIONSTORE
173 WINECRYPT_CERTSTORE hdr;
174 CRITICAL_SECTION cs;
175 struct list stores;
176 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
178 typedef struct _WINE_PROVIDERSTORE
180 WINECRYPT_CERTSTORE hdr;
181 DWORD dwStoreProvFlags;
182 PWINECRYPT_CERTSTORE memStore;
183 HCERTSTOREPROV hStoreProv;
184 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
185 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
186 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
187 PFN_CERT_STORE_PROV_CONTROL provControl;
188 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
190 /* Internal version of CertGetCertificateContextProperty that gets properties
191 * directly from the context (or the context it's linked to, depending on its
192 * type.) Doesn't handle special-case properties, since they are handled by
193 * CertGetCertificateContextProperty, and are particular to the store in which
194 * the property exists (which is separate from the context.)
196 static BOOL WINAPI CertContext_GetProperty(void *context, DWORD dwPropId,
197 void *pvData, DWORD *pcbData);
199 /* Internal version of CertSetCertificateContextProperty that sets properties
200 * directly on the context (or the context it's linked to, depending on its
201 * type.) Doesn't handle special cases, since they're handled by
202 * CertSetCertificateContextProperty anyway.
204 static BOOL WINAPI CertContext_SetProperty(void *context, DWORD dwPropId,
205 DWORD dwFlags, const void *pvData);
207 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
208 DWORD dwFlags, CertStoreType type)
210 store->ref = 1;
211 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
212 store->type = type;
213 if (!hCryptProv)
215 hCryptProv = CRYPT_GetDefaultProvider();
216 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
218 store->cryptProv = hCryptProv;
219 store->dwOpenFlags = dwFlags;
222 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
223 void *toReplace, const void **ppStoreContext)
225 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
226 PCERT_CONTEXT context;
228 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
230 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
231 if (context)
233 context->hCertStore = store;
234 if (ppStoreContext)
235 *ppStoreContext =
236 CertDuplicateCertificateContext(context);
238 return context ? TRUE : FALSE;
241 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
243 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
244 void *ret;
246 TRACE("(%p, %p)\n", store, pPrev);
248 ret = ContextList_Enum(ms->certs, pPrev);
249 if (!ret)
250 SetLastError(CRYPT_E_NOT_FOUND);
252 TRACE("returning %p\n", ret);
253 return ret;
256 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
258 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
260 ContextList_Delete(ms->certs, pCertContext);
261 return TRUE;
264 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
266 ContextList_Empty(store->certs);
269 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
271 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
273 TRACE("(%p, %08lx)\n", store, dwFlags);
274 if (dwFlags)
275 FIXME("Unimplemented flags: %08lx\n", dwFlags);
277 ContextList_Free(store->certs);
278 CryptMemFree(store);
281 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
282 DWORD dwFlags, const void *pvPara)
284 PWINE_MEMSTORE store;
286 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
288 if (dwFlags & CERT_STORE_DELETE_FLAG)
290 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
291 store = NULL;
293 else
295 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
296 if (store)
298 memset(store, 0, sizeof(WINE_MEMSTORE));
299 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
300 store->hdr.closeStore = CRYPT_MemCloseStore;
301 store->hdr.certs.addContext = CRYPT_MemAddCert;
302 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
303 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
304 store->hdr.control = NULL;
305 store->certs = ContextList_Create(pCertInterface,
306 sizeof(CERT_CONTEXT));
309 return (PWINECRYPT_CERTSTORE)store;
312 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
314 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
315 PWINE_STORE_LIST_ENTRY entry, next;
317 TRACE("(%p, %08lx)\n", store, dwFlags);
319 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
320 entry)
322 TRACE("closing %p\n", entry);
323 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
324 CryptMemFree(entry);
326 DeleteCriticalSection(&cs->cs);
327 CryptMemFree(cs);
330 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
331 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
332 BOOL addRef)
334 void *ret = Context_CreateLinkContext(contextSize, child,
335 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
337 if (ret)
338 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
339 = storeEntry;
341 return ret;
344 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
345 size_t contextStoreOffset, void *context, void *toReplace, size_t contextSize,
346 void **pChildContext)
348 BOOL ret;
349 void *childContext = NULL;
350 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
352 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
353 toReplace, contextSize);
355 ret = FALSE;
356 if (toReplace)
358 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
359 PCONTEXT_STORE contextStore;
361 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
362 contextSize);
363 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
364 contextStoreOffset);
365 ret = contextStore->addContext(storeEntry->store, context,
366 existingLinked, (const void **)&childContext);
368 else
370 PWINE_STORE_LIST_ENTRY entry, next;
372 EnterCriticalSection(&store->cs);
373 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
374 WINE_STORE_LIST_ENTRY, entry)
376 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
378 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
379 (LPBYTE)entry->store + contextStoreOffset);
381 storeEntry = entry;
382 ret = contextStore->addContext(entry->store, context, NULL,
383 (const void **)&childContext);
384 break;
387 LeaveCriticalSection(&store->cs);
388 if (!storeEntry)
389 SetLastError(E_ACCESSDENIED);
391 *pChildContext = childContext;
392 return ret;
395 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
396 void *toReplace, const void **ppStoreContext)
398 BOOL ret;
399 void *childContext = NULL;
400 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
402 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
403 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
404 if (ppStoreContext && childContext)
406 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
407 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
408 PCERT_CONTEXT context =
409 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
410 sizeof(CERT_CONTEXT), TRUE);
412 if (context)
413 context->hCertStore = store;
414 *ppStoreContext = context;
416 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
417 return ret;
420 /* Advances a collection enumeration by one context, if possible, where
421 * advancing means:
422 * - calling the current store's enumeration function once, and returning
423 * the enumerated context if one is returned
424 * - moving to the next store if the current store has no more items, and
425 * recursively calling itself to get the next item.
426 * Returns NULL if the collection contains no more items or on error.
427 * Assumes the collection store's lock is held.
429 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
430 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
431 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
433 void *ret, *child;
434 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
435 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
436 contextStoreOffset);
438 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
440 if (pPrev)
442 /* Ref-counting funny business: "duplicate" (addref) the child, because
443 * the free(pPrev) below can cause the ref count to become negative.
445 child = Context_GetLinkedContext(pPrev, contextSize);
446 contextInterface->duplicate(child);
447 child = contextStore->enumContext(storeEntry->store, child);
448 contextInterface->free(pPrev);
449 pPrev = NULL;
451 else
452 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
453 if (child)
454 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
455 contextSize, FALSE);
456 else
458 if (storeNext)
459 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
460 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
461 contextInterface, NULL, contextSize);
462 else
464 SetLastError(CRYPT_E_NOT_FOUND);
465 ret = NULL;
468 TRACE("returning %p\n", ret);
469 return ret;
472 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
474 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
475 void *ret;
477 TRACE("(%p, %p)\n", store, pPrev);
479 EnterCriticalSection(&cs->cs);
480 if (pPrev)
482 PWINE_STORE_LIST_ENTRY storeEntry =
483 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
484 sizeof(CERT_CONTEXT));
486 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
487 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
488 sizeof(CERT_CONTEXT));
490 else
492 if (!list_empty(&cs->stores))
494 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
495 WINE_STORE_LIST_ENTRY, entry);
497 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
498 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
499 sizeof(CERT_CONTEXT));
501 else
503 SetLastError(CRYPT_E_NOT_FOUND);
504 ret = NULL;
507 LeaveCriticalSection(&cs->cs);
508 if (ret)
509 ((PCERT_CONTEXT)ret)->hCertStore = store;
510 TRACE("returning %p\n", ret);
511 return ret;
514 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
515 void *pCertContext)
517 BOOL ret;
519 TRACE("(%p, %p)\n", store, pCertContext);
521 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
522 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
523 return ret;
526 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
527 DWORD dwFlags, const void *pvPara)
529 PWINE_COLLECTIONSTORE store;
531 if (dwFlags & CERT_STORE_DELETE_FLAG)
533 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
534 store = NULL;
536 else
538 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
539 if (store)
541 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
542 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
543 StoreTypeCollection);
544 store->hdr.closeStore = CRYPT_CollectionCloseStore;
545 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
546 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
547 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
548 InitializeCriticalSection(&store->cs);
549 list_init(&store->stores);
552 return (PWINECRYPT_CERTSTORE)store;
555 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
557 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
559 TRACE("(%p, %08lx)\n", store, dwFlags);
561 if (store->provCloseStore)
562 store->provCloseStore(store->hStoreProv, dwFlags);
563 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
564 CertCloseStore(store->memStore, dwFlags);
565 CryptMemFree(store);
568 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
569 void *toReplace, const void **ppStoreContext)
571 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
572 BOOL ret;
574 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
576 if (toReplace)
577 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
578 (const void **)ppStoreContext);
579 else
581 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
583 SetLastError(ERROR_ACCESS_DENIED);
584 ret = FALSE;
586 else
588 ret = TRUE;
589 if (ps->provWriteCert)
590 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
591 CERT_STORE_PROV_WRITE_ADD_FLAG);
592 if (ret)
593 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
594 (const void **)ppStoreContext);
597 /* dirty trick: replace the returned context's hCertStore with
598 * store.
600 if (ppStoreContext)
601 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
602 return ret;
605 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
607 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
608 void *ret;
610 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
611 if (ret)
613 /* same dirty trick: replace the returned context's hCertStore with
614 * store.
616 ((PCERT_CONTEXT)ret)->hCertStore = store;
618 return ret;
621 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store,
622 void *cert)
624 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
625 BOOL ret = TRUE;
627 TRACE("(%p, %p)\n", store, cert);
629 if (ps->provDeleteCert)
630 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
631 if (ret)
632 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
633 return ret;
636 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
637 DWORD dwCtrlType, void const *pvCtrlPara)
639 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
640 BOOL ret = TRUE;
642 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
643 pvCtrlPara);
645 if (store->provControl)
646 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
647 pvCtrlPara);
648 return ret;
651 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
652 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, PCERT_STORE_PROV_INFO pProvInfo)
654 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
655 sizeof(WINE_PROVIDERSTORE));
657 if (ret)
659 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
660 StoreTypeProvider);
661 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
662 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
664 CertCloseStore(memStore, 0);
665 ret->memStore = NULL;
667 else
668 ret->memStore = memStore;
669 ret->hStoreProv = pProvInfo->hStoreProv;
670 ret->hdr.closeStore = CRYPT_ProvCloseStore;
671 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
672 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
673 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
674 ret->hdr.control = CRYPT_ProvControl;
675 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
676 ret->provCloseStore =
677 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
678 else
679 ret->provCloseStore = NULL;
680 if (pProvInfo->cStoreProvFunc >
681 CERT_STORE_PROV_WRITE_CERT_FUNC)
682 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
683 CERT_STORE_PROV_WRITE_CERT_FUNC];
684 else
685 ret->provWriteCert = NULL;
686 if (pProvInfo->cStoreProvFunc >
687 CERT_STORE_PROV_DELETE_CERT_FUNC)
688 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
689 CERT_STORE_PROV_DELETE_CERT_FUNC];
690 else
691 ret->provDeleteCert = NULL;
692 if (pProvInfo->cStoreProvFunc >
693 CERT_STORE_PROV_CONTROL_FUNC)
694 ret->provControl = pProvInfo->rgpvStoreProvFunc[
695 CERT_STORE_PROV_CONTROL_FUNC];
696 else
697 ret->provControl = NULL;
699 return (PWINECRYPT_CERTSTORE)ret;
702 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
703 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
705 static HCRYPTOIDFUNCSET set = NULL;
706 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
707 HCRYPTOIDFUNCADDR hFunc;
708 PWINECRYPT_CERTSTORE ret = NULL;
710 if (!set)
711 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
712 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
713 (void **)&provOpenFunc, &hFunc);
714 if (provOpenFunc)
716 CERT_STORE_PROV_INFO provInfo = { 0 };
718 provInfo.cbSize = sizeof(provInfo);
719 if (dwFlags & CERT_STORE_DELETE_FLAG)
720 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
721 dwFlags, pvPara, NULL, &provInfo);
722 else
724 HCERTSTORE memStore;
726 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
727 CERT_STORE_CREATE_NEW_FLAG, NULL);
728 if (memStore)
730 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
731 dwFlags, pvPara, memStore, &provInfo))
732 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
733 &provInfo);
734 else
735 CertCloseStore(memStore, 0);
738 CryptFreeOIDFunctionAddress(hFunc, 0);
740 else
741 SetLastError(ERROR_FILE_NOT_FOUND);
742 return ret;
745 static void CRYPT_HashToStr(LPBYTE hash, LPWSTR asciiHash)
747 static const WCHAR fmt[] = { '%','0','2','X',0 };
748 DWORD i;
750 assert(hash);
751 assert(asciiHash);
753 for (i = 0; i < 20; i++)
754 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
757 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
758 0 };
759 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
760 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
761 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
763 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store, HKEY key,
764 DWORD contextType)
766 LONG rc;
767 DWORD index = 0;
768 WCHAR subKeyName[MAX_PATH];
770 do {
771 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
773 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
774 NULL);
775 if (!rc)
777 HKEY subKey;
779 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
780 if (!rc)
782 LPBYTE buf = NULL;
784 size = 0;
785 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
786 if (!rc)
787 buf = CryptMemAlloc(size);
788 if (buf)
790 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
791 &size);
792 if (!rc)
794 const void *context;
795 DWORD addedType;
797 TRACE("Adding cert with hash %s\n",
798 debugstr_w(subKeyName));
799 context = CRYPT_ReadSerializedElement(buf, size,
800 contextType, &addedType);
801 if (context)
803 const WINE_CONTEXT_INTERFACE *contextInterface;
804 BYTE hash[20];
806 switch (addedType)
808 case CERT_STORE_CERTIFICATE_CONTEXT:
809 contextInterface = &gCertInterface;
810 break;
811 case CERT_STORE_CRL_CONTEXT:
812 contextInterface = &gCRLInterface;
813 break;
814 case CERT_STORE_CTL_CONTEXT:
815 contextInterface = &gCTLInterface;
816 break;
817 default:
818 contextInterface = NULL;
820 if (contextInterface)
822 size = sizeof(hash);
823 if (contextInterface->getProp(context,
824 CERT_HASH_PROP_ID, hash, &size))
826 WCHAR asciiHash[20 * 2 + 1];
828 CRYPT_HashToStr(hash, asciiHash);
829 TRACE("comparing %s\n",
830 debugstr_w(asciiHash));
831 TRACE("with %s\n", debugstr_w(subKeyName));
832 if (!lstrcmpW(asciiHash, subKeyName))
834 TRACE("hash matches, adding\n");
835 contextInterface->addContextToStore(
836 store->memStore, context,
837 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
839 else
840 TRACE("hash doesn't match, ignoring\n");
842 contextInterface->free(context);
846 CryptMemFree(buf);
848 RegCloseKey(subKey);
850 /* Ignore intermediate errors, continue enumerating */
851 rc = ERROR_SUCCESS;
853 } while (!rc);
856 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store)
858 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
859 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
860 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
861 DWORD i;
863 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
865 HKEY key;
866 LONG rc;
868 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
869 &key, NULL);
870 if (!rc)
872 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
873 RegCloseKey(key);
878 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
879 static BOOL CRYPT_WriteSerializedToReg(HKEY key, LPBYTE hash, LPBYTE buf,
880 DWORD len)
882 WCHAR asciiHash[20 * 2 + 1];
883 LONG rc;
884 HKEY subKey;
885 BOOL ret;
887 CRYPT_HashToStr(hash, asciiHash);
888 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
889 &subKey, NULL);
890 if (!rc)
892 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
893 RegCloseKey(subKey);
895 if (!rc)
896 ret = TRUE;
897 else
899 SetLastError(rc);
900 ret = FALSE;
902 return ret;
905 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
906 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
908 const void *context = NULL;
909 BOOL ret;
911 do {
912 context = contextInterface->enumContextsInStore(memStore, context);
913 if (context)
915 BYTE hash[20];
916 DWORD hashSize = sizeof(hash);
918 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
919 &hashSize);
920 if (ret)
922 DWORD size = 0;
923 LPBYTE buf = NULL;
925 ret = contextInterface->serialize(context, 0, NULL, &size);
926 if (size)
927 buf = CryptMemAlloc(size);
928 if (buf)
930 ret = contextInterface->serialize(context, 0, buf, &size);
931 if (ret)
932 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
934 CryptMemFree(buf);
937 else
938 ret = TRUE;
939 } while (ret && context != NULL);
940 if (context)
941 contextInterface->free(context);
942 return ret;
945 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
947 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
948 static const WINE_CONTEXT_INTERFACE *interfaces[] = { &gCertInterface,
949 &gCRLInterface, &gCTLInterface };
950 struct list *listToDelete[] = { &store->certsToDelete, NULL, NULL };
951 BOOL ret = TRUE;
952 DWORD i;
954 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
956 HKEY key;
957 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
958 KEY_ALL_ACCESS, NULL, &key, NULL);
960 if (!rc)
962 if (listToDelete[i])
964 PWINE_HASH_TO_DELETE toDelete, next;
965 WCHAR asciiHash[20 * 2 + 1];
967 EnterCriticalSection(&store->cs);
968 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
969 WINE_HASH_TO_DELETE, entry)
971 LONG rc;
973 CRYPT_HashToStr(toDelete->hash, asciiHash);
974 TRACE("Removing %s\n", debugstr_w(asciiHash));
975 rc = RegDeleteKeyW(key, asciiHash);
976 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
978 SetLastError(rc);
979 ret = FALSE;
981 list_remove(&toDelete->entry);
982 CryptMemFree(toDelete);
984 LeaveCriticalSection(&store->cs);
986 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
987 store->memStore);
988 RegCloseKey(key);
990 else
992 SetLastError(rc);
993 ret = FALSE;
996 return ret;
999 /* If force is true or the registry store is dirty, writes the contents of the
1000 * store to the registry.
1002 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1004 BOOL ret;
1006 TRACE("(%p, %d)\n", store, force);
1008 if (store->dirty || force)
1009 ret = CRYPT_RegWriteToReg(store);
1010 else
1011 ret = TRUE;
1012 return ret;
1015 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1017 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1019 TRACE("(%p, %08lx)\n", store, dwFlags);
1020 if (dwFlags)
1021 FIXME("Unimplemented flags: %08lx\n", dwFlags);
1023 CRYPT_RegFlushStore(store, FALSE);
1024 RegCloseKey(store->key);
1025 DeleteCriticalSection(&store->cs);
1026 CryptMemFree(store);
1029 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1030 PCCERT_CONTEXT cert, DWORD dwFlags)
1032 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1033 BOOL ret;
1035 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
1037 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1039 store->dirty = TRUE;
1040 ret = TRUE;
1042 else
1043 ret = FALSE;
1044 return ret;
1047 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1048 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1050 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1051 BOOL ret;
1053 TRACE("(%p, %p, %08lx)\n", store, pCertContext, dwFlags);
1055 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1057 SetLastError(ERROR_ACCESS_DENIED);
1058 ret = FALSE;
1060 else
1062 PWINE_HASH_TO_DELETE toDelete =
1063 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1065 if (toDelete)
1067 DWORD size = sizeof(toDelete->hash);
1069 ret = CertGetCertificateContextProperty(pCertContext,
1070 CERT_HASH_PROP_ID, toDelete->hash, &size);
1071 if (ret)
1073 EnterCriticalSection(&store->cs);
1074 list_add_tail(&store->certsToDelete, &toDelete->entry);
1075 LeaveCriticalSection(&store->cs);
1077 else
1079 CryptMemFree(toDelete);
1080 ret = FALSE;
1083 else
1084 ret = FALSE;
1085 if (ret)
1086 store->dirty = TRUE;
1088 return ret;
1091 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1092 DWORD dwCtrlType, void const *pvCtrlPara)
1094 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1095 BOOL ret;
1097 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
1098 pvCtrlPara);
1100 switch (dwCtrlType)
1102 case CERT_STORE_CTRL_RESYNC:
1103 CRYPT_RegFlushStore(store, FALSE);
1104 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1105 CRYPT_RegReadFromReg(store);
1106 ret = TRUE;
1107 break;
1108 case CERT_STORE_CTRL_COMMIT:
1109 ret = CRYPT_RegFlushStore(store,
1110 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1111 break;
1112 default:
1113 FIXME("%ld: stub\n", dwCtrlType);
1114 ret = FALSE;
1116 return ret;
1119 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1120 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1122 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1123 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1124 HKEY hSubKey = 0;
1126 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1128 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1129 if (!dwRet)
1131 /* Find how many subkeys there are */
1132 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1133 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1134 if (!dwRet)
1136 dwMaxSubkeyLen++;
1137 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1139 /* Name too big: alloc a buffer for it */
1140 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1143 if (!lpszName)
1144 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1145 else
1147 /* Recursively delete all the subkeys */
1148 for (i = 0; i < dwKeyCount && !dwRet; i++)
1150 dwSize = dwMaxSubkeyLen;
1151 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1152 NULL, NULL, NULL);
1153 if (!dwRet)
1154 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1157 if (lpszName != szNameBuf)
1159 /* Free buffer if allocated */
1160 CryptMemFree(lpszName);
1165 RegCloseKey(hSubKey);
1166 if (!dwRet)
1167 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1169 return dwRet;
1172 static void *regProvFuncs[] = {
1173 CRYPT_RegCloseStore,
1174 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1175 CRYPT_RegWriteCert,
1176 CRYPT_RegDeleteCert,
1177 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1178 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1179 NULL, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1180 NULL, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1181 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1182 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1183 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1184 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1185 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1186 CRYPT_RegControl,
1189 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1190 DWORD dwFlags, const void *pvPara)
1192 PWINECRYPT_CERTSTORE store = NULL;
1194 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
1196 if (dwFlags & CERT_STORE_DELETE_FLAG)
1198 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1200 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1201 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1202 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1203 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1204 if (rc == ERROR_NO_MORE_ITEMS)
1205 rc = ERROR_SUCCESS;
1206 SetLastError(rc);
1208 else
1210 HKEY key;
1212 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1213 GetCurrentProcess(), (LPHANDLE)&key,
1214 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1215 TRUE, 0))
1217 PWINECRYPT_CERTSTORE memStore;
1219 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1220 if (memStore)
1222 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1223 sizeof(WINE_REGSTOREINFO));
1225 if (regInfo)
1227 CERT_STORE_PROV_INFO provInfo = { 0 };
1229 regInfo->dwOpenFlags = dwFlags;
1230 regInfo->cryptProv = hCryptProv;
1231 regInfo->memStore = memStore;
1232 regInfo->key = key;
1233 InitializeCriticalSection(&regInfo->cs);
1234 list_init(&regInfo->certsToDelete);
1235 CRYPT_RegReadFromReg(regInfo);
1236 regInfo->dirty = FALSE;
1237 provInfo.cbSize = sizeof(provInfo);
1238 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1239 sizeof(regProvFuncs[0]);
1240 provInfo.rgpvStoreProvFunc = regProvFuncs;
1241 provInfo.hStoreProv = regInfo;
1242 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1243 &provInfo);
1248 TRACE("returning %p\n", store);
1249 return store;
1252 /* FIXME: this isn't complete for the Root store, in which the top-level
1253 * self-signed CA certs reside. Adding a cert to the Root store should present
1254 * the user with a dialog indicating the consequences of doing so, and asking
1255 * the user to confirm whether the cert should be added.
1257 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1258 DWORD dwFlags, const void *pvPara)
1260 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1261 LPCWSTR storeName = (LPCWSTR)pvPara;
1262 LPWSTR storePath;
1263 PWINECRYPT_CERTSTORE store = NULL;
1264 HKEY root;
1265 LPCWSTR base;
1266 BOOL ret;
1268 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1269 debugstr_w((LPCWSTR)pvPara));
1271 if (!pvPara)
1273 SetLastError(E_INVALIDARG);
1274 return NULL;
1277 ret = TRUE;
1278 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1280 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1281 root = HKEY_LOCAL_MACHINE;
1282 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1283 break;
1284 case CERT_SYSTEM_STORE_CURRENT_USER:
1285 root = HKEY_CURRENT_USER;
1286 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1287 break;
1288 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1289 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1290 * SystemCertificates
1292 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1293 debugstr_w(storeName));
1294 return NULL;
1295 case CERT_SYSTEM_STORE_SERVICES:
1296 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1297 * SystemCertificates
1299 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1300 debugstr_w(storeName));
1301 return NULL;
1302 case CERT_SYSTEM_STORE_USERS:
1303 /* hku\user sid\Software\Microsoft\SystemCertificates */
1304 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1305 debugstr_w(storeName));
1306 return NULL;
1307 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1308 root = HKEY_CURRENT_USER;
1309 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1310 break;
1311 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1312 root = HKEY_LOCAL_MACHINE;
1313 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1314 break;
1315 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1316 /* hklm\Software\Microsoft\EnterpriseCertificates */
1317 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1318 debugstr_w(storeName));
1319 return NULL;
1320 default:
1321 SetLastError(E_INVALIDARG);
1322 return NULL;
1325 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1326 sizeof(WCHAR));
1327 if (storePath)
1329 LONG rc;
1330 HKEY key;
1331 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1332 KEY_ALL_ACCESS;
1334 wsprintfW(storePath, fmt, base, storeName);
1335 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1336 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1337 else
1339 DWORD disp;
1341 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1342 &key, &disp);
1343 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1344 disp == REG_OPENED_EXISTING_KEY)
1346 RegCloseKey(key);
1347 rc = ERROR_FILE_EXISTS;
1350 if (!rc)
1352 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1353 RegCloseKey(key);
1355 else
1356 SetLastError(rc);
1357 CryptMemFree(storePath);
1359 return store;
1362 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1363 DWORD dwFlags, const void *pvPara)
1365 int len;
1366 PWINECRYPT_CERTSTORE ret = NULL;
1368 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1369 debugstr_a((LPCSTR)pvPara));
1371 if (!pvPara)
1373 SetLastError(ERROR_FILE_NOT_FOUND);
1374 return NULL;
1376 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1377 if (len)
1379 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1381 if (storeName)
1383 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1384 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1385 CryptMemFree(storeName);
1388 return ret;
1391 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1392 DWORD dwFlags, const void *pvPara)
1394 HCERTSTORE store = 0;
1395 BOOL ret;
1397 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1398 debugstr_w((LPCWSTR)pvPara));
1400 if (!pvPara)
1402 SetLastError(ERROR_FILE_NOT_FOUND);
1403 return NULL;
1405 /* This returns a different error than system registry stores if the
1406 * location is invalid.
1408 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1410 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1411 case CERT_SYSTEM_STORE_CURRENT_USER:
1412 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1413 case CERT_SYSTEM_STORE_SERVICES:
1414 case CERT_SYSTEM_STORE_USERS:
1415 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1416 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1417 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1418 ret = TRUE;
1419 break;
1420 default:
1421 SetLastError(ERROR_FILE_NOT_FOUND);
1422 ret = FALSE;
1424 if (ret)
1426 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1427 0, hCryptProv, dwFlags, pvPara);
1429 if (regStore)
1431 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1432 CERT_STORE_CREATE_NEW_FLAG, NULL);
1433 CertAddStoreToCollection(store, regStore,
1434 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1435 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1436 CertCloseStore(regStore, 0);
1437 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1438 * stores.
1440 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1441 CERT_SYSTEM_STORE_CURRENT_USER)
1443 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1444 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1445 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1446 hCryptProv, dwFlags, pvPara);
1447 if (regStore)
1449 CertAddStoreToCollection(store, regStore,
1450 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1451 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1452 CertCloseStore(regStore, 0);
1457 return (PWINECRYPT_CERTSTORE)store;
1460 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1461 DWORD dwFlags, const void *pvPara)
1463 int len;
1464 PWINECRYPT_CERTSTORE ret = NULL;
1466 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1467 debugstr_a((LPCSTR)pvPara));
1469 if (!pvPara)
1471 SetLastError(ERROR_FILE_NOT_FOUND);
1472 return NULL;
1474 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1475 if (len)
1477 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1479 if (storeName)
1481 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1482 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1483 CryptMemFree(storeName);
1486 return ret;
1489 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1490 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1491 const void* pvPara)
1493 WINECRYPT_CERTSTORE *hcs;
1494 StoreOpenFunc openFunc = NULL;
1496 TRACE("(%s, %08lx, %08lx, %08lx, %p)\n", debugstr_a(lpszStoreProvider),
1497 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1499 if (!HIWORD(lpszStoreProvider))
1501 switch (LOWORD(lpszStoreProvider))
1503 case (int)CERT_STORE_PROV_MEMORY:
1504 openFunc = CRYPT_MemOpenStore;
1505 break;
1506 case (int)CERT_STORE_PROV_REG:
1507 openFunc = CRYPT_RegOpenStore;
1508 break;
1509 case (int)CERT_STORE_PROV_COLLECTION:
1510 openFunc = CRYPT_CollectionOpenStore;
1511 break;
1512 case (int)CERT_STORE_PROV_SYSTEM_A:
1513 openFunc = CRYPT_SysOpenStoreA;
1514 break;
1515 case (int)CERT_STORE_PROV_SYSTEM_W:
1516 openFunc = CRYPT_SysOpenStoreW;
1517 break;
1518 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
1519 openFunc = CRYPT_SysRegOpenStoreA;
1520 break;
1521 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
1522 openFunc = CRYPT_SysRegOpenStoreW;
1523 break;
1524 default:
1525 if (LOWORD(lpszStoreProvider))
1526 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
1529 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
1530 openFunc = CRYPT_MemOpenStore;
1531 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
1532 openFunc = CRYPT_SysOpenStoreW;
1533 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
1534 openFunc = CRYPT_CollectionOpenStore;
1535 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
1536 openFunc = CRYPT_SysRegOpenStoreW;
1537 else
1539 FIXME("unimplemented type %s\n", lpszStoreProvider);
1540 openFunc = NULL;
1543 if (!openFunc)
1544 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
1545 hCryptProv, dwFlags, pvPara);
1546 else
1547 hcs = openFunc(hCryptProv, dwFlags, pvPara);
1548 return (HCERTSTORE)hcs;
1551 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
1552 LPCSTR szSubSystemProtocol)
1554 if (!szSubSystemProtocol)
1556 SetLastError(E_INVALIDARG);
1557 return 0;
1559 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
1560 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1563 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
1564 LPCWSTR szSubSystemProtocol)
1566 if (!szSubSystemProtocol)
1568 SetLastError(E_INVALIDARG);
1569 return 0;
1571 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
1572 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1575 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
1576 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
1578 FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore,
1579 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
1580 return TRUE;
1583 PCCRL_CONTEXT WINAPI CertCreateCRLContext( DWORD dwCertEncodingType,
1584 const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
1586 PCRL_CONTEXT pcrl;
1587 BYTE* data;
1589 TRACE("%08lx %p %08lx\n", dwCertEncodingType, pbCrlEncoded, cbCrlEncoded);
1591 /* FIXME: semi-stub, need to use CryptDecodeObjectEx to decode the CRL. */
1592 pcrl = CryptMemAlloc( sizeof (CRL_CONTEXT) );
1593 if( !pcrl )
1594 return NULL;
1596 data = CryptMemAlloc( cbCrlEncoded );
1597 if( !data )
1599 CryptMemFree( pcrl );
1600 return NULL;
1603 pcrl->dwCertEncodingType = dwCertEncodingType;
1604 pcrl->pbCrlEncoded = data;
1605 pcrl->cbCrlEncoded = cbCrlEncoded;
1606 pcrl->pCrlInfo = NULL;
1607 pcrl->hCertStore = 0;
1609 return pcrl;
1612 PCCERT_CONTEXT WINAPI CertCreateCertificateContext(DWORD dwCertEncodingType,
1613 const BYTE *pbCertEncoded, DWORD cbCertEncoded)
1615 PCERT_CONTEXT cert = NULL;
1616 BOOL ret;
1617 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
1618 PCERT_INFO certInfo = NULL;
1619 DWORD size = 0;
1621 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCertEncoded,
1622 cbCertEncoded);
1624 /* First try to decode it as a signed cert. */
1625 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT, pbCertEncoded,
1626 cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&signedCert, &size);
1627 if (ret)
1629 size = 0;
1630 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
1631 signedCert->ToBeSigned.pbData, signedCert->ToBeSigned.cbData,
1632 CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&certInfo, &size);
1633 LocalFree(signedCert);
1635 /* Failing that, try it as an unsigned cert */
1636 if (!ret)
1638 size = 0;
1639 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT_TO_BE_SIGNED,
1640 pbCertEncoded, cbCertEncoded,
1641 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
1642 (BYTE *)&certInfo, &size);
1644 if (ret)
1646 BYTE *data = NULL;
1648 cert = (PCERT_CONTEXT)Context_CreateDataContext(sizeof(CERT_CONTEXT));
1649 if (!cert)
1650 goto end;
1651 data = CryptMemAlloc(cbCertEncoded);
1652 if (!data)
1654 CryptMemFree(cert);
1655 cert = NULL;
1656 goto end;
1658 memcpy(data, pbCertEncoded, cbCertEncoded);
1659 cert->dwCertEncodingType = dwCertEncodingType;
1660 cert->pbCertEncoded = data;
1661 cert->cbCertEncoded = cbCertEncoded;
1662 cert->pCertInfo = certInfo;
1663 cert->hCertStore = 0;
1666 end:
1667 return (PCCERT_CONTEXT)cert;
1670 DWORD WINAPI CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext,
1671 DWORD dwPropId)
1673 PCONTEXT_PROPERTY_LIST properties = Context_GetProperties(
1674 (void *)pCertContext, sizeof(CERT_CONTEXT));
1675 DWORD ret;
1677 TRACE("(%p, %ld)\n", pCertContext, dwPropId);
1679 if (properties)
1680 ret = ContextPropertyList_EnumPropIDs(properties, dwPropId);
1681 else
1682 ret = 0;
1683 return ret;
1686 static BOOL CertContext_GetHashProp(void *context, DWORD dwPropId,
1687 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
1688 DWORD *pcbData)
1690 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
1691 pcbData);
1692 if (ret)
1694 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
1696 ret = CertContext_SetProperty(context, dwPropId, 0, &blob);
1698 return ret;
1701 static BOOL WINAPI CertContext_GetProperty(void *context, DWORD dwPropId,
1702 void *pvData, DWORD *pcbData)
1704 PCCERT_CONTEXT pCertContext = (PCCERT_CONTEXT)context;
1705 PCONTEXT_PROPERTY_LIST properties =
1706 Context_GetProperties(context, sizeof(CERT_CONTEXT));
1707 BOOL ret;
1708 CRYPT_DATA_BLOB blob;
1710 TRACE("(%p, %ld, %p, %p)\n", context, dwPropId, pvData, pcbData);
1712 if (properties)
1713 ret = ContextPropertyList_FindProperty(properties, dwPropId,
1714 &blob);
1715 else
1716 ret = FALSE;
1717 if (ret)
1719 if (!pvData)
1721 *pcbData = blob.cbData;
1722 ret = TRUE;
1724 else if (*pcbData < blob.cbData)
1726 SetLastError(ERROR_MORE_DATA);
1727 *pcbData = blob.cbData;
1729 else
1731 memcpy(pvData, blob.pbData, blob.cbData);
1732 *pcbData = blob.cbData;
1733 ret = TRUE;
1736 else
1738 /* Implicit properties */
1739 switch (dwPropId)
1741 case CERT_SHA1_HASH_PROP_ID:
1742 ret = CertContext_GetHashProp(context, dwPropId, CALG_SHA1,
1743 pCertContext->pbCertEncoded, pCertContext->cbCertEncoded, pvData,
1744 pcbData);
1745 break;
1746 case CERT_MD5_HASH_PROP_ID:
1747 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1748 pCertContext->pbCertEncoded, pCertContext->cbCertEncoded, pvData,
1749 pcbData);
1750 break;
1751 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
1752 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1753 pCertContext->pCertInfo->Subject.pbData,
1754 pCertContext->pCertInfo->Subject.cbData,
1755 pvData, pcbData);
1756 break;
1757 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
1758 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1759 pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
1760 pCertContext->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
1761 pvData, pcbData);
1762 break;
1763 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID:
1764 ret = CertContext_GetHashProp(context, dwPropId, CALG_MD5,
1765 pCertContext->pCertInfo->SerialNumber.pbData,
1766 pCertContext->pCertInfo->SerialNumber.cbData,
1767 pvData, pcbData);
1768 break;
1769 case CERT_SIGNATURE_HASH_PROP_ID:
1770 FIXME("CERT_SIGNATURE_HASH_PROP_ID unimplemented\n");
1771 SetLastError(CRYPT_E_NOT_FOUND);
1772 break;
1773 default:
1774 SetLastError(CRYPT_E_NOT_FOUND);
1777 TRACE("returning %d\n", ret);
1778 return ret;
1781 /* info is assumed to be a CRYPT_KEY_PROV_INFO, followed by its container name,
1782 * provider name, and any provider parameters, in a contiguous buffer, but
1783 * where info's pointers are assumed to be invalid. Upon return, info's
1784 * pointers point to the appropriate memory locations.
1786 static void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info)
1788 DWORD i, containerLen, provNameLen;
1789 LPBYTE data = (LPBYTE)info + sizeof(CRYPT_KEY_PROV_INFO);
1791 info->pwszContainerName = (LPWSTR)data;
1792 containerLen = (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
1793 data += containerLen;
1795 info->pwszProvName = (LPWSTR)data;
1796 provNameLen = (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
1797 data += provNameLen;
1799 info->rgProvParam = (PCRYPT_KEY_PROV_PARAM)data;
1800 data += info->cProvParam * sizeof(CRYPT_KEY_PROV_PARAM);
1802 for (i = 0; i < info->cProvParam; i++)
1804 info->rgProvParam[i].pbData = data;
1805 data += info->rgProvParam[i].cbData;
1809 BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
1810 DWORD dwPropId, void *pvData, DWORD *pcbData)
1812 BOOL ret;
1814 TRACE("(%p, %ld, %p, %p)\n", pCertContext, dwPropId, pvData, pcbData);
1816 switch (dwPropId)
1818 case 0:
1819 case CERT_CERT_PROP_ID:
1820 case CERT_CRL_PROP_ID:
1821 case CERT_CTL_PROP_ID:
1822 SetLastError(E_INVALIDARG);
1823 ret = FALSE;
1824 break;
1825 case CERT_ACCESS_STATE_PROP_ID:
1826 if (!pvData)
1828 *pcbData = sizeof(DWORD);
1829 ret = TRUE;
1831 else if (*pcbData < sizeof(DWORD))
1833 SetLastError(ERROR_MORE_DATA);
1834 *pcbData = sizeof(DWORD);
1835 ret = FALSE;
1837 else
1839 DWORD state = 0;
1841 if (pCertContext->hCertStore)
1843 PWINECRYPT_CERTSTORE store =
1844 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
1846 if (!(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
1847 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
1849 *(DWORD *)pvData = state;
1850 ret = TRUE;
1852 break;
1853 case CERT_KEY_PROV_INFO_PROP_ID:
1854 ret = CertContext_GetProperty((void *)pCertContext, dwPropId, pvData,
1855 pcbData);
1856 if (ret && pvData)
1857 CRYPT_FixKeyProvInfoPointers((PCRYPT_KEY_PROV_INFO)pvData);
1858 break;
1859 default:
1860 ret = CertContext_GetProperty((void *)pCertContext, dwPropId, pvData,
1861 pcbData);
1864 TRACE("returning %d\n", ret);
1865 return ret;
1868 /* Copies key provider info from from into to, where to is assumed to be a
1869 * contiguous buffer of memory large enough for from and all its associated
1870 * data, but whose pointers are uninitialized.
1871 * Upon return, to contains a contiguous copy of from, packed in the following
1872 * order:
1873 * - CRYPT_KEY_PROV_INFO
1874 * - pwszContainerName
1875 * - pwszProvName
1876 * - rgProvParam[0]...
1878 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to,
1879 PCRYPT_KEY_PROV_INFO from)
1881 DWORD i;
1882 LPBYTE nextData = (LPBYTE)to + sizeof(CRYPT_KEY_PROV_INFO);
1884 to->pwszContainerName = (LPWSTR)nextData;
1885 lstrcpyW(to->pwszContainerName, from->pwszContainerName);
1886 nextData += (lstrlenW(from->pwszContainerName) + 1) * sizeof(WCHAR);
1887 to->pwszProvName = (LPWSTR)nextData;
1888 lstrcpyW(to->pwszProvName, from->pwszProvName);
1889 nextData += (lstrlenW(from->pwszProvName) + 1) * sizeof(WCHAR);
1890 to->dwProvType = from->dwProvType;
1891 to->dwFlags = from->dwFlags;
1892 to->cProvParam = from->cProvParam;
1893 to->rgProvParam = (PCRYPT_KEY_PROV_PARAM)nextData;
1894 nextData += to->cProvParam * sizeof(CRYPT_KEY_PROV_PARAM);
1895 to->dwKeySpec = from->dwKeySpec;
1896 for (i = 0; i < to->cProvParam; i++)
1898 memcpy(&to->rgProvParam[i], &from->rgProvParam[i],
1899 sizeof(CRYPT_KEY_PROV_PARAM));
1900 to->rgProvParam[i].pbData = nextData;
1901 memcpy(to->rgProvParam[i].pbData, from->rgProvParam[i].pbData,
1902 from->rgProvParam[i].cbData);
1903 nextData += from->rgProvParam[i].cbData;
1907 static BOOL CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties,
1908 PCRYPT_KEY_PROV_INFO info)
1910 BOOL ret;
1911 LPBYTE buf = NULL;
1912 DWORD size = sizeof(CRYPT_KEY_PROV_INFO), i, containerSize, provNameSize;
1914 containerSize = (lstrlenW(info->pwszContainerName) + 1) * sizeof(WCHAR);
1915 provNameSize = (lstrlenW(info->pwszProvName) + 1) * sizeof(WCHAR);
1916 size += containerSize + provNameSize;
1917 for (i = 0; i < info->cProvParam; i++)
1918 size += sizeof(CRYPT_KEY_PROV_PARAM) + info->rgProvParam[i].cbData;
1919 buf = CryptMemAlloc(size);
1920 if (buf)
1922 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO)buf, info);
1923 ret = ContextPropertyList_SetProperty(properties,
1924 CERT_KEY_PROV_INFO_PROP_ID, buf, size);
1925 CryptMemFree(buf);
1927 else
1928 ret = FALSE;
1929 return ret;
1932 static BOOL WINAPI CertContext_SetProperty(void *context, DWORD dwPropId,
1933 DWORD dwFlags, const void *pvData)
1935 PCONTEXT_PROPERTY_LIST properties =
1936 Context_GetProperties(context, sizeof(CERT_CONTEXT));
1937 BOOL ret;
1939 TRACE("(%p, %ld, %08lx, %p)\n", context, dwPropId, dwFlags, pvData);
1941 if (!properties)
1942 ret = FALSE;
1943 else if (!pvData)
1945 ContextPropertyList_RemoveProperty(properties, dwPropId);
1946 ret = TRUE;
1948 else
1950 switch (dwPropId)
1952 case CERT_AUTO_ENROLL_PROP_ID:
1953 case CERT_CTL_USAGE_PROP_ID: /* same as CERT_ENHKEY_USAGE_PROP_ID */
1954 case CERT_DESCRIPTION_PROP_ID:
1955 case CERT_FRIENDLY_NAME_PROP_ID:
1956 case CERT_HASH_PROP_ID:
1957 case CERT_KEY_IDENTIFIER_PROP_ID:
1958 case CERT_MD5_HASH_PROP_ID:
1959 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
1960 case CERT_PUBKEY_ALG_PARA_PROP_ID:
1961 case CERT_PVK_FILE_PROP_ID:
1962 case CERT_SIGNATURE_HASH_PROP_ID:
1963 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
1964 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
1965 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
1966 case CERT_ENROLLMENT_PROP_ID:
1967 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
1968 case CERT_RENEWAL_PROP_ID:
1970 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
1972 ret = ContextPropertyList_SetProperty(properties, dwPropId,
1973 blob->pbData, blob->cbData);
1974 break;
1976 case CERT_DATE_STAMP_PROP_ID:
1977 ret = ContextPropertyList_SetProperty(properties, dwPropId,
1978 (LPBYTE)pvData, sizeof(FILETIME));
1979 break;
1980 case CERT_KEY_PROV_INFO_PROP_ID:
1981 ret = CertContext_SetKeyProvInfoProperty(properties,
1982 (PCRYPT_KEY_PROV_INFO)pvData);
1983 break;
1984 default:
1985 FIXME("%ld: stub\n", dwPropId);
1986 ret = FALSE;
1989 TRACE("returning %d\n", ret);
1990 return ret;
1993 BOOL WINAPI CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
1994 DWORD dwPropId, DWORD dwFlags, const void *pvData)
1996 BOOL ret;
1998 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext, dwPropId, dwFlags, pvData);
2000 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
2001 * crashes on most of these, I'll be safer.
2003 switch (dwPropId)
2005 case 0:
2006 case CERT_ACCESS_STATE_PROP_ID:
2007 case CERT_CERT_PROP_ID:
2008 case CERT_CRL_PROP_ID:
2009 case CERT_CTL_PROP_ID:
2010 SetLastError(E_INVALIDARG);
2011 return FALSE;
2013 ret = CertContext_SetProperty((void *)pCertContext, dwPropId, dwFlags,
2014 pvData);
2015 TRACE("returning %d\n", ret);
2016 return ret;
2019 PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(
2020 PCCERT_CONTEXT pCertContext)
2022 TRACE("(%p)\n", pCertContext);
2023 Context_AddRef((void *)pCertContext, sizeof(CERT_CONTEXT));
2024 return pCertContext;
2027 static void CertContext_CopyProperties(PCCERT_CONTEXT to, PCCERT_CONTEXT from)
2029 PCONTEXT_PROPERTY_LIST toProperties, fromProperties;
2031 toProperties = Context_GetProperties((void *)to, sizeof(CERT_CONTEXT));
2032 fromProperties = Context_GetProperties((void *)from, sizeof(CERT_CONTEXT));
2033 ContextPropertyList_Copy(toProperties, fromProperties);
2036 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2037 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2038 PCCERT_CONTEXT *ppStoreContext)
2040 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2041 BOOL ret = TRUE;
2042 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2044 TRACE("(%p, %p, %08lx, %p)\n", hCertStore, pCertContext,
2045 dwAddDisposition, ppStoreContext);
2047 /* Weird case to pass a test */
2048 if (dwAddDisposition == 0)
2050 SetLastError(STATUS_ACCESS_VIOLATION);
2051 return FALSE;
2053 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2055 BYTE hashToAdd[20];
2056 DWORD size = sizeof(hashToAdd);
2058 ret = CertContext_GetProperty((void *)pCertContext, CERT_HASH_PROP_ID,
2059 hashToAdd, &size);
2060 if (ret)
2062 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2064 existing = CertFindCertificateInStore(hCertStore,
2065 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2066 NULL);
2070 switch (dwAddDisposition)
2072 case CERT_STORE_ADD_ALWAYS:
2073 toAdd = CertDuplicateCertificateContext(pCertContext);
2074 break;
2075 case CERT_STORE_ADD_NEW:
2076 if (existing)
2078 TRACE("found matching certificate, not adding\n");
2079 SetLastError(CRYPT_E_EXISTS);
2080 ret = FALSE;
2082 else
2083 toAdd = CertDuplicateCertificateContext(pCertContext);
2084 break;
2085 case CERT_STORE_ADD_REPLACE_EXISTING:
2086 toAdd = CertDuplicateCertificateContext(pCertContext);
2087 break;
2088 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2089 toAdd = CertDuplicateCertificateContext(pCertContext);
2090 if (existing)
2091 CertContext_CopyProperties(toAdd, existing);
2092 break;
2093 case CERT_STORE_ADD_USE_EXISTING:
2094 if (existing)
2095 CertContext_CopyProperties(existing, pCertContext);
2096 break;
2097 default:
2098 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition);
2099 ret = FALSE;
2102 if (toAdd)
2104 if (store)
2105 ret = store->certs.addContext(store, (void *)toAdd,
2106 (void *)existing, (const void **)ppStoreContext);
2107 else if (ppStoreContext)
2108 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2109 CertFreeCertificateContext(toAdd);
2111 CertFreeCertificateContext(existing);
2113 TRACE("returning %d\n", ret);
2114 return ret;
2117 BOOL WINAPI CertAddEncodedCertificateToStore(HCERTSTORE hCertStore,
2118 DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded,
2119 DWORD dwAddDisposition, PCCERT_CONTEXT *ppCertContext)
2121 PCCERT_CONTEXT cert = CertCreateCertificateContext(dwCertEncodingType,
2122 pbCertEncoded, cbCertEncoded);
2123 BOOL ret;
2125 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore, dwCertEncodingType,
2126 pbCertEncoded, cbCertEncoded, dwAddDisposition, ppCertContext);
2128 if (cert)
2130 ret = CertAddCertificateContextToStore(hCertStore, cert,
2131 dwAddDisposition, ppCertContext);
2132 CertFreeCertificateContext(cert);
2134 else
2135 ret = FALSE;
2136 return ret;
2139 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2140 PCCERT_CONTEXT pPrev)
2142 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2143 PCCERT_CONTEXT ret;
2145 TRACE("(%p, %p)\n", hCertStore, pPrev);
2146 if (!hCertStore)
2147 ret = NULL;
2148 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2149 ret = NULL;
2150 else
2151 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2152 return ret;
2155 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2157 BOOL ret;
2159 TRACE("(%p)\n", pCertContext);
2161 if (!pCertContext)
2162 ret = TRUE;
2163 else if (!pCertContext->hCertStore)
2165 ret = TRUE;
2166 CertFreeCertificateContext(pCertContext);
2168 else
2170 PWINECRYPT_CERTSTORE hcs =
2171 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2173 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2174 ret = FALSE;
2175 else
2176 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2177 CertFreeCertificateContext(pCertContext);
2179 return ret;
2182 BOOL WINAPI CertAddEncodedCRLToStore(HCERTSTORE hCertStore,
2183 DWORD dwCertEncodingType, const BYTE *pbCrlEncoded, DWORD cbCrlEncoded,
2184 DWORD dwAddDisposition, PCCRL_CONTEXT *ppCrlContext)
2186 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2187 dwCertEncodingType, pbCrlEncoded, cbCrlEncoded, dwAddDisposition,
2188 ppCrlContext);
2189 return FALSE;
2192 BOOL WINAPI CertAddCRLContextToStore( HCERTSTORE hCertStore,
2193 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2194 PCCRL_CONTEXT* ppStoreContext )
2196 FIXME("%p %p %08lx %p\n", hCertStore, pCrlContext,
2197 dwAddDisposition, ppStoreContext);
2198 return TRUE;
2201 PCCRL_CONTEXT WINAPI CertDuplicateCRLContext(PCCRL_CONTEXT pCrlContext)
2203 FIXME("(%p): stub\n", pCrlContext);
2204 return pCrlContext;
2207 BOOL WINAPI CertFreeCRLContext( PCCRL_CONTEXT pCrlContext)
2209 FIXME("%p\n", pCrlContext );
2211 return TRUE;
2214 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2216 FIXME("(%p): stub\n", pCrlContext);
2217 return TRUE;
2220 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2221 PCCRL_CONTEXT pPrev)
2223 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2224 return NULL;
2227 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2228 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2230 FIXME("(%08lx, %p, %08lx): stub\n", dwCertEncodingType, pbCtlEncoded,
2231 cbCtlEncoded);
2232 return NULL;
2235 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2236 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2237 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2239 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2240 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2241 ppCtlContext);
2242 return FALSE;
2245 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2246 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2247 PCCTL_CONTEXT* ppStoreContext)
2249 FIXME("(%p, %p, %08lx, %p): stub\n", hCertStore, pCtlContext,
2250 dwAddDisposition, ppStoreContext);
2251 return TRUE;
2254 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2256 FIXME("(%p): stub\n", pCtlContext );
2257 return pCtlContext;
2260 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2262 FIXME("(%p): stub\n", pCtlContext );
2263 return TRUE;
2266 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2268 FIXME("(%p): stub\n", pCtlContext);
2269 return TRUE;
2272 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2273 PCCTL_CONTEXT pPrev)
2275 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2276 return NULL;
2279 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2281 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2283 TRACE("(%p)\n", hCertStore);
2285 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2286 InterlockedIncrement(&hcs->ref);
2287 return hCertStore;
2290 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2292 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2294 TRACE("(%p, %08lx)\n", hCertStore, dwFlags);
2296 if( ! hCertStore )
2297 return TRUE;
2299 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2300 return FALSE;
2302 if (InterlockedDecrement(&hcs->ref) == 0)
2304 TRACE("%p's ref count is 0, freeing\n", hcs);
2305 hcs->dwMagic = 0;
2306 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2307 CryptReleaseContext(hcs->cryptProv, 0);
2308 hcs->closeStore(hcs, dwFlags);
2310 else
2311 TRACE("%p's ref count is %ld\n", hcs, hcs->ref);
2312 return TRUE;
2315 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2316 DWORD dwCtrlType, void const *pvCtrlPara)
2318 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2319 BOOL ret;
2321 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
2322 pvCtrlPara);
2324 if (!hcs)
2325 ret = FALSE;
2326 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2327 ret = FALSE;
2328 else
2330 if (hcs->control)
2331 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2332 else
2333 ret = TRUE;
2335 return ret;
2338 BOOL WINAPI CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2339 DWORD dwPropId, void *pvData, DWORD *pcbData)
2341 FIXME("(%p, %ld, %p, %p): stub\n", pCRLContext, dwPropId, pvData, pcbData);
2342 return FALSE;
2345 BOOL WINAPI CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2346 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2348 FIXME("(%p, %ld, %08lx, %p): stub\n", pCRLContext, dwPropId, dwFlags,
2349 pvData);
2350 return FALSE;
2353 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2354 DWORD dwPropId, void *pvData, DWORD *pcbData)
2356 FIXME("(%p, %ld, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2357 return FALSE;
2360 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2361 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2363 FIXME("(%p, %ld, %08lx, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2364 pvData);
2365 return FALSE;
2368 static void CertDataContext_Free(void *context)
2370 PCERT_CONTEXT certContext = (PCERT_CONTEXT)context;
2372 CryptMemFree(certContext->pbCertEncoded);
2373 LocalFree(certContext->pCertInfo);
2376 BOOL WINAPI CertFreeCertificateContext(PCCERT_CONTEXT pCertContext)
2378 TRACE("(%p)\n", pCertContext);
2380 if (pCertContext)
2381 Context_Release((void *)pCertContext, sizeof(CERT_CONTEXT),
2382 CertDataContext_Free);
2383 return TRUE;
2386 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2387 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2389 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2390 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2391 PWINE_STORE_LIST_ENTRY entry;
2392 BOOL ret;
2394 TRACE("(%p, %p, %08lx, %ld)\n", hCollectionStore, hSiblingStore,
2395 dwUpdateFlags, dwPriority);
2397 if (!collection || !sibling)
2398 return TRUE;
2399 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2401 SetLastError(E_INVALIDARG);
2402 return FALSE;
2404 if (collection->hdr.type != StoreTypeCollection)
2406 SetLastError(E_INVALIDARG);
2407 return FALSE;
2409 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2411 SetLastError(E_INVALIDARG);
2412 return FALSE;
2415 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2416 if (entry)
2418 InterlockedIncrement(&sibling->ref);
2419 TRACE("sibling %p's ref count is %ld\n", sibling, sibling->ref);
2420 entry->store = sibling;
2421 entry->dwUpdateFlags = dwUpdateFlags;
2422 entry->dwPriority = dwPriority;
2423 list_init(&entry->entry);
2424 TRACE("%p: adding %p, priority %ld\n", collection, entry, dwPriority);
2425 EnterCriticalSection(&collection->cs);
2426 if (dwPriority)
2428 PWINE_STORE_LIST_ENTRY cursor;
2429 BOOL added = FALSE;
2431 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2432 WINE_STORE_LIST_ENTRY, entry)
2434 if (cursor->dwPriority < dwPriority)
2436 list_add_before(&cursor->entry, &entry->entry);
2437 added = TRUE;
2438 break;
2441 if (!added)
2442 list_add_tail(&collection->stores, &entry->entry);
2444 else
2445 list_add_tail(&collection->stores, &entry->entry);
2446 LeaveCriticalSection(&collection->cs);
2447 ret = TRUE;
2449 else
2450 ret = FALSE;
2451 return ret;
2454 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2455 HCERTSTORE hSiblingStore)
2457 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2458 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2459 PWINE_STORE_LIST_ENTRY store, next;
2461 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2463 if (!collection || !sibling)
2464 return;
2465 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2467 SetLastError(E_INVALIDARG);
2468 return;
2470 if (collection->hdr.type != StoreTypeCollection)
2471 return;
2472 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2474 SetLastError(E_INVALIDARG);
2475 return;
2477 EnterCriticalSection(&collection->cs);
2478 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2479 WINE_STORE_LIST_ENTRY, entry)
2481 if (store->store == sibling)
2483 list_remove(&store->entry);
2484 CertCloseStore(store->store, 0);
2485 CryptMemFree(store);
2486 break;
2489 LeaveCriticalSection(&collection->cs);