ntprint: Implement PSetupEnumMonitor.
[wine/testsucceed.git] / dlls / crypt32 / store.c
blobb2be1529b8c858ea7f452783117135050cc62640
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 * - The concept of physical stores and locations isn't implemented. (This
21 * doesn't mean registry stores et al aren't implemented. See the PSDK for
22 * registering and enumerating physical stores and locations.)
23 * - Many flags, options and whatnot are unimplemented.
26 #include "config.h"
27 #include "wine/port.h"
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 "wine/exception.h"
40 #include "crypt32_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
44 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
46 static const WINE_CONTEXT_INTERFACE gCertInterface = {
47 (CreateContextFunc)CertCreateCertificateContext,
48 (AddContextToStoreFunc)CertAddCertificateContextToStore,
49 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
50 (DuplicateContextFunc)CertDuplicateCertificateContext,
51 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
52 (EnumPropertiesFunc)CertEnumCertificateContextProperties,
53 (GetContextPropertyFunc)CertGetCertificateContextProperty,
54 (SetContextPropertyFunc)CertSetCertificateContextProperty,
55 (SerializeElementFunc)CertSerializeCertificateStoreElement,
56 (FreeContextFunc)CertFreeCertificateContext,
57 (DeleteContextFunc)CertDeleteCertificateFromStore,
59 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
61 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
62 (CreateContextFunc)CertCreateCRLContext,
63 (AddContextToStoreFunc)CertAddCRLContextToStore,
64 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
65 (DuplicateContextFunc)CertDuplicateCRLContext,
66 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
67 (EnumPropertiesFunc)CertEnumCRLContextProperties,
68 (GetContextPropertyFunc)CertGetCRLContextProperty,
69 (SetContextPropertyFunc)CertSetCRLContextProperty,
70 (SerializeElementFunc)CertSerializeCRLStoreElement,
71 (FreeContextFunc)CertFreeCRLContext,
72 (DeleteContextFunc)CertDeleteCRLFromStore,
74 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
76 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
77 (CreateContextFunc)CertCreateCTLContext,
78 (AddContextToStoreFunc)CertAddCTLContextToStore,
79 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
80 (DuplicateContextFunc)CertDuplicateCTLContext,
81 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
82 (EnumPropertiesFunc)CertEnumCTLContextProperties,
83 (GetContextPropertyFunc)CertGetCTLContextProperty,
84 (SetContextPropertyFunc)CertSetCTLContextProperty,
85 (SerializeElementFunc)CertSerializeCTLStoreElement,
86 (FreeContextFunc)CertFreeCTLContext,
87 (DeleteContextFunc)CertDeleteCTLFromStore,
89 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
91 struct WINE_CRYPTCERTSTORE;
93 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
94 DWORD dwFlags, const void *pvPara);
96 /* Called to enumerate the next context in a store. */
97 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
99 /* Called to add a context to a store. If toReplace is not NULL,
100 * context replaces toReplace in the store, and access checks should not be
101 * performed. Otherwise context is a new context, and it should only be
102 * added if the store allows it. If ppStoreContext is not NULL, the added
103 * context should be returned in *ppStoreContext.
105 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
106 void *toReplace, const void **ppStoreContext);
108 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
110 typedef struct _CONTEXT_STORE
112 AddFunc addContext;
113 EnumFunc enumContext;
114 DeleteFunc deleteContext;
115 } CONTEXT_STORE, *PCONTEXT_STORE;
117 typedef enum _CertStoreType {
118 StoreTypeMem,
119 StoreTypeCollection,
120 StoreTypeProvider,
121 } CertStoreType;
123 /* A cert store is polymorphic through the use of function pointers. A type
124 * is still needed to distinguish collection stores from other types.
125 * On the function pointers:
126 * - closeStore is called when the store's ref count becomes 0
127 * - control is optional, but should be implemented by any store that supports
128 * persistence
130 typedef struct WINE_CRYPTCERTSTORE
132 DWORD dwMagic;
133 LONG ref;
134 DWORD dwOpenFlags;
135 HCRYPTPROV cryptProv;
136 CertStoreType type;
137 PFN_CERT_STORE_PROV_CLOSE closeStore;
138 CONTEXT_STORE certs;
139 CONTEXT_STORE crls;
140 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
141 PCONTEXT_PROPERTY_LIST properties;
142 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
144 typedef struct _WINE_MEMSTORE
146 WINECRYPT_CERTSTORE hdr;
147 struct ContextList *certs;
148 struct ContextList *crls;
149 } WINE_MEMSTORE, *PWINE_MEMSTORE;
151 typedef struct _WINE_HASH_TO_DELETE
153 BYTE hash[20];
154 struct list entry;
155 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
157 typedef struct _WINE_REGSTOREINFO
159 DWORD dwOpenFlags;
160 HCRYPTPROV cryptProv;
161 PWINECRYPT_CERTSTORE memStore;
162 HKEY key;
163 BOOL dirty;
164 CRITICAL_SECTION cs;
165 struct list certsToDelete;
166 struct list crlsToDelete;
167 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
169 typedef struct _WINE_FILESTOREINFO
171 DWORD dwOpenFlags;
172 HCRYPTPROV cryptProv;
173 PWINECRYPT_CERTSTORE memStore;
174 HANDLE file;
175 BOOL dirty;
176 } WINE_FILESTOREINFO, *PWINE_FILESTOREINFO;
178 typedef struct _WINE_STORE_LIST_ENTRY
180 PWINECRYPT_CERTSTORE store;
181 DWORD dwUpdateFlags;
182 DWORD dwPriority;
183 struct list entry;
184 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
186 typedef struct _WINE_COLLECTIONSTORE
188 WINECRYPT_CERTSTORE hdr;
189 CRITICAL_SECTION cs;
190 struct list stores;
191 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
193 typedef struct _WINE_PROVIDERSTORE
195 WINECRYPT_CERTSTORE hdr;
196 DWORD dwStoreProvFlags;
197 PWINECRYPT_CERTSTORE memStore;
198 HCERTSTOREPROV hStoreProv;
199 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
200 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
201 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
202 PFN_CERT_STORE_PROV_WRITE_CRL provWriteCrl;
203 PFN_CERT_STORE_PROV_DELETE_CRL provDeleteCrl;
204 PFN_CERT_STORE_PROV_CONTROL provControl;
205 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
207 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
208 DWORD dwFlags, CertStoreType type)
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;
220 store->properties = NULL;
223 static void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store)
225 if (store->properties)
226 ContextPropertyList_Free(store->properties);
227 CryptMemFree(store);
230 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
231 void *toReplace, const void **ppStoreContext)
233 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
234 PCERT_CONTEXT context;
236 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
238 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
239 if (context)
241 context->hCertStore = store;
242 if (ppStoreContext)
243 *ppStoreContext = CertDuplicateCertificateContext(context);
245 return context ? TRUE : FALSE;
248 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
250 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
251 void *ret;
253 TRACE("(%p, %p)\n", store, pPrev);
255 ret = ContextList_Enum(ms->certs, pPrev);
256 if (!ret)
257 SetLastError(CRYPT_E_NOT_FOUND);
259 TRACE("returning %p\n", ret);
260 return ret;
263 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
265 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
267 ContextList_Delete(ms->certs, pCertContext);
268 return TRUE;
271 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
272 void *toReplace, const void **ppStoreContext)
274 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
275 PCRL_CONTEXT context;
277 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
279 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
280 if (context)
282 context->hCertStore = store;
283 if (ppStoreContext)
284 *ppStoreContext = CertDuplicateCRLContext(context);
286 return context ? TRUE : FALSE;
289 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
291 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
292 void *ret;
294 TRACE("(%p, %p)\n", store, pPrev);
296 ret = ContextList_Enum(ms->crls, pPrev);
297 if (!ret)
298 SetLastError(CRYPT_E_NOT_FOUND);
300 TRACE("returning %p\n", ret);
301 return ret;
304 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
306 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
308 ContextList_Delete(ms->crls, pCrlContext);
309 return TRUE;
312 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
314 ContextList_Empty(store->certs);
315 ContextList_Empty(store->crls);
318 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
320 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
322 TRACE("(%p, %08x)\n", store, dwFlags);
323 if (dwFlags)
324 FIXME("Unimplemented flags: %08x\n", dwFlags);
326 ContextList_Free(store->certs);
327 ContextList_Free(store->crls);
328 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
331 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
332 DWORD dwFlags, const void *pvPara)
334 PWINE_MEMSTORE store;
336 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
338 if (dwFlags & CERT_STORE_DELETE_FLAG)
340 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
341 store = NULL;
343 else
345 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
346 if (store)
348 memset(store, 0, sizeof(WINE_MEMSTORE));
349 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
350 store->hdr.closeStore = CRYPT_MemCloseStore;
351 store->hdr.certs.addContext = CRYPT_MemAddCert;
352 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
353 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
354 store->hdr.crls.addContext = CRYPT_MemAddCrl;
355 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
356 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
357 store->hdr.control = NULL;
358 store->certs = ContextList_Create(pCertInterface,
359 sizeof(CERT_CONTEXT));
360 store->crls = ContextList_Create(pCRLInterface,
361 sizeof(CRL_CONTEXT));
364 return (PWINECRYPT_CERTSTORE)store;
367 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
369 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
370 PWINE_STORE_LIST_ENTRY entry, next;
372 TRACE("(%p, %08x)\n", store, dwFlags);
374 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
375 entry)
377 TRACE("closing %p\n", entry);
378 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
379 CryptMemFree(entry);
381 cs->cs.DebugInfo->Spare[0] = 0;
382 DeleteCriticalSection(&cs->cs);
383 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
386 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
387 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
388 BOOL addRef)
390 void *ret = Context_CreateLinkContext(contextSize, child,
391 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
393 if (ret)
394 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
395 = storeEntry;
397 return ret;
400 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
401 unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
402 void **pChildContext)
404 BOOL ret;
405 void *childContext = NULL;
406 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
408 TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
409 toReplace, contextSize);
411 ret = FALSE;
412 if (toReplace)
414 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
415 PCONTEXT_STORE contextStore;
417 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
418 contextSize);
419 contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
420 contextStoreOffset);
421 ret = contextStore->addContext(storeEntry->store, context,
422 existingLinked, (const void **)&childContext);
424 else
426 PWINE_STORE_LIST_ENTRY entry, next;
428 EnterCriticalSection(&store->cs);
429 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
430 WINE_STORE_LIST_ENTRY, entry)
432 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
434 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
435 (LPBYTE)entry->store + contextStoreOffset);
437 storeEntry = entry;
438 ret = contextStore->addContext(entry->store, context, NULL,
439 (const void **)&childContext);
440 break;
443 LeaveCriticalSection(&store->cs);
444 if (!storeEntry)
445 SetLastError(E_ACCESSDENIED);
447 *pChildContext = childContext;
448 return ret;
451 /* Advances a collection enumeration by one context, if possible, where
452 * advancing means:
453 * - calling the current store's enumeration function once, and returning
454 * the enumerated context if one is returned
455 * - moving to the next store if the current store has no more items, and
456 * recursively calling itself to get the next item.
457 * Returns NULL if the collection contains no more items or on error.
458 * Assumes the collection store's lock is held.
460 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
461 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextStoreOffset,
462 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
464 void *ret, *child;
465 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
466 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)((LPBYTE)storeEntry->store +
467 contextStoreOffset);
469 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
471 if (pPrev)
473 /* Ref-counting funny business: "duplicate" (addref) the child, because
474 * the free(pPrev) below can cause the ref count to become negative.
476 child = Context_GetLinkedContext(pPrev, contextSize);
477 contextInterface->duplicate(child);
478 child = contextStore->enumContext(storeEntry->store, child);
479 contextInterface->free(pPrev);
480 pPrev = NULL;
482 else
483 child = storeEntry->store->certs.enumContext(storeEntry->store, NULL);
484 if (child)
485 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
486 contextSize, FALSE);
487 else
489 if (storeNext)
490 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
491 WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
492 contextInterface, NULL, contextSize);
493 else
495 SetLastError(CRYPT_E_NOT_FOUND);
496 ret = NULL;
499 TRACE("returning %p\n", ret);
500 return ret;
503 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
504 void *toReplace, const void **ppStoreContext)
506 BOOL ret;
507 void *childContext = NULL;
508 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
510 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
511 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
512 if (ppStoreContext && childContext)
514 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
515 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
516 PCERT_CONTEXT context =
517 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
518 sizeof(CERT_CONTEXT), TRUE);
520 if (context)
521 context->hCertStore = store;
522 *ppStoreContext = context;
524 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
525 return ret;
528 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
530 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
531 void *ret;
533 TRACE("(%p, %p)\n", store, pPrev);
535 EnterCriticalSection(&cs->cs);
536 if (pPrev)
538 PWINE_STORE_LIST_ENTRY storeEntry =
539 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
540 sizeof(CERT_CONTEXT));
542 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
543 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
544 sizeof(CERT_CONTEXT));
546 else
548 if (!list_empty(&cs->stores))
550 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
551 WINE_STORE_LIST_ENTRY, entry);
553 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
554 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
555 sizeof(CERT_CONTEXT));
557 else
559 SetLastError(CRYPT_E_NOT_FOUND);
560 ret = NULL;
563 LeaveCriticalSection(&cs->cs);
564 if (ret)
565 ((PCERT_CONTEXT)ret)->hCertStore = store;
566 TRACE("returning %p\n", ret);
567 return ret;
570 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
571 void *pCertContext)
573 BOOL ret;
575 TRACE("(%p, %p)\n", store, pCertContext);
577 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
578 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
579 return ret;
582 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
583 void *toReplace, const void **ppStoreContext)
585 BOOL ret;
586 void *childContext = NULL;
587 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
589 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
590 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
591 if (ppStoreContext && childContext)
593 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
594 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
595 PCRL_CONTEXT context =
596 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
597 sizeof(CRL_CONTEXT), TRUE);
599 if (context)
600 context->hCertStore = store;
601 *ppStoreContext = context;
603 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
604 return ret;
607 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
609 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
610 void *ret;
612 TRACE("(%p, %p)\n", store, pPrev);
614 EnterCriticalSection(&cs->cs);
615 if (pPrev)
617 PWINE_STORE_LIST_ENTRY storeEntry =
618 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
619 sizeof(CRL_CONTEXT));
621 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
622 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
623 sizeof(CRL_CONTEXT));
625 else
627 if (!list_empty(&cs->stores))
629 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
630 WINE_STORE_LIST_ENTRY, entry);
632 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
633 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
634 sizeof(CRL_CONTEXT));
636 else
638 SetLastError(CRYPT_E_NOT_FOUND);
639 ret = NULL;
642 LeaveCriticalSection(&cs->cs);
643 if (ret)
644 ((PCRL_CONTEXT)ret)->hCertStore = store;
645 TRACE("returning %p\n", ret);
646 return ret;
649 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
650 void *pCrlContext)
652 BOOL ret;
654 TRACE("(%p, %p)\n", store, pCrlContext);
656 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
657 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
658 return ret;
661 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
662 DWORD dwFlags, const void *pvPara)
664 PWINE_COLLECTIONSTORE store;
666 if (dwFlags & CERT_STORE_DELETE_FLAG)
668 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
669 store = NULL;
671 else
673 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
674 if (store)
676 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
677 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
678 StoreTypeCollection);
679 store->hdr.closeStore = CRYPT_CollectionCloseStore;
680 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
681 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
682 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
683 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
684 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
685 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
686 InitializeCriticalSection(&store->cs);
687 store->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_COLLECTIONSTORE->cs");
688 list_init(&store->stores);
691 return (PWINECRYPT_CERTSTORE)store;
694 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
696 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
698 TRACE("(%p, %08x)\n", store, dwFlags);
700 if (store->provCloseStore)
701 store->provCloseStore(store->hStoreProv, dwFlags);
702 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
703 CertCloseStore(store->memStore, dwFlags);
704 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
707 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
708 void *toReplace, const void **ppStoreContext)
710 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
711 BOOL ret;
713 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
715 if (toReplace)
716 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
717 (const void **)ppStoreContext);
718 else
720 ret = TRUE;
721 if (ps->provWriteCert)
722 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
723 CERT_STORE_PROV_WRITE_ADD_FLAG);
724 if (ret)
725 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
726 (const void **)ppStoreContext);
728 /* dirty trick: replace the returned context's hCertStore with
729 * store.
731 if (ppStoreContext)
732 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
733 return ret;
736 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
738 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
739 void *ret;
741 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
742 if (ret)
744 /* same dirty trick: replace the returned context's hCertStore with
745 * store.
747 ((PCERT_CONTEXT)ret)->hCertStore = store;
749 return ret;
752 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
754 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
755 BOOL ret = TRUE;
757 TRACE("(%p, %p)\n", store, cert);
759 if (ps->provDeleteCert)
760 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
761 if (ret)
762 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
763 return ret;
766 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
767 void *toReplace, const void **ppStoreContext)
769 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
770 BOOL ret;
772 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
774 if (toReplace)
775 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
776 (const void **)ppStoreContext);
777 else
779 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
781 SetLastError(ERROR_ACCESS_DENIED);
782 ret = FALSE;
784 else
786 ret = TRUE;
787 if (ps->provWriteCrl)
788 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
789 CERT_STORE_PROV_WRITE_ADD_FLAG);
790 if (ret)
791 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
792 (const void **)ppStoreContext);
795 /* dirty trick: replace the returned context's hCertStore with
796 * store.
798 if (ppStoreContext)
799 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
800 return ret;
803 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
805 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
806 void *ret;
808 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
809 if (ret)
811 /* same dirty trick: replace the returned context's hCertStore with
812 * store.
814 ((PCERT_CONTEXT)ret)->hCertStore = store;
816 return ret;
819 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
821 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
822 BOOL ret = TRUE;
824 TRACE("(%p, %p)\n", store, crl);
826 if (ps->provDeleteCrl)
827 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
828 if (ret)
829 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
830 return ret;
833 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
834 DWORD dwCtrlType, void const *pvCtrlPara)
836 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
837 BOOL ret = TRUE;
839 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
840 pvCtrlPara);
842 if (store->provControl)
843 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
844 pvCtrlPara);
845 return ret;
848 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
849 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo)
851 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
852 sizeof(WINE_PROVIDERSTORE));
854 if (ret)
856 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
857 StoreTypeProvider);
858 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
859 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
861 CertCloseStore(memStore, 0);
862 ret->memStore = NULL;
864 else
865 ret->memStore = memStore;
866 ret->hStoreProv = pProvInfo->hStoreProv;
867 ret->hdr.closeStore = CRYPT_ProvCloseStore;
868 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
869 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
870 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
871 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
872 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
873 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
874 ret->hdr.control = CRYPT_ProvControl;
875 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
876 ret->provCloseStore =
877 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
878 else
879 ret->provCloseStore = NULL;
880 if (pProvInfo->cStoreProvFunc >
881 CERT_STORE_PROV_WRITE_CERT_FUNC)
882 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
883 CERT_STORE_PROV_WRITE_CERT_FUNC];
884 else
885 ret->provWriteCert = NULL;
886 if (pProvInfo->cStoreProvFunc >
887 CERT_STORE_PROV_DELETE_CERT_FUNC)
888 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
889 CERT_STORE_PROV_DELETE_CERT_FUNC];
890 else
891 ret->provDeleteCert = NULL;
892 if (pProvInfo->cStoreProvFunc >
893 CERT_STORE_PROV_WRITE_CRL_FUNC)
894 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
895 CERT_STORE_PROV_WRITE_CRL_FUNC];
896 else
897 ret->provWriteCert = NULL;
898 if (pProvInfo->cStoreProvFunc >
899 CERT_STORE_PROV_DELETE_CRL_FUNC)
900 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
901 CERT_STORE_PROV_DELETE_CRL_FUNC];
902 else
903 ret->provDeleteCert = NULL;
904 if (pProvInfo->cStoreProvFunc >
905 CERT_STORE_PROV_CONTROL_FUNC)
906 ret->provControl = pProvInfo->rgpvStoreProvFunc[
907 CERT_STORE_PROV_CONTROL_FUNC];
908 else
909 ret->provControl = NULL;
911 return (PWINECRYPT_CERTSTORE)ret;
914 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
915 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
917 static HCRYPTOIDFUNCSET set = NULL;
918 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
919 HCRYPTOIDFUNCADDR hFunc;
920 PWINECRYPT_CERTSTORE ret = NULL;
922 if (!set)
923 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
924 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
925 (void **)&provOpenFunc, &hFunc);
926 if (provOpenFunc)
928 CERT_STORE_PROV_INFO provInfo = { 0 };
930 provInfo.cbSize = sizeof(provInfo);
931 if (dwFlags & CERT_STORE_DELETE_FLAG)
932 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
933 dwFlags, pvPara, NULL, &provInfo);
934 else
936 HCERTSTORE memStore;
938 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
939 CERT_STORE_CREATE_NEW_FLAG, NULL);
940 if (memStore)
942 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
943 dwFlags, pvPara, memStore, &provInfo))
944 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
945 &provInfo);
946 else
947 CertCloseStore(memStore, 0);
950 CryptFreeOIDFunctionAddress(hFunc, 0);
952 else
953 SetLastError(ERROR_FILE_NOT_FOUND);
954 return ret;
957 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
959 static const WCHAR fmt[] = { '%','0','2','X',0 };
960 DWORD i;
962 assert(hash);
963 assert(asciiHash);
965 for (i = 0; i < 20; i++)
966 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
969 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
970 0 };
971 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
972 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
973 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
975 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO *store, HKEY key,
976 DWORD contextType)
978 LONG rc;
979 DWORD index = 0;
980 WCHAR subKeyName[MAX_PATH];
982 do {
983 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
985 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
986 NULL);
987 if (!rc)
989 HKEY subKey;
991 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
992 if (!rc)
994 LPBYTE buf = NULL;
996 size = 0;
997 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
998 if (!rc)
999 buf = CryptMemAlloc(size);
1000 if (buf)
1002 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
1003 &size);
1004 if (!rc)
1006 const void *context;
1007 DWORD addedType;
1009 TRACE("Adding cert with hash %s\n",
1010 debugstr_w(subKeyName));
1011 context = CRYPT_ReadSerializedElement(buf, size,
1012 contextType, &addedType);
1013 if (context)
1015 const WINE_CONTEXT_INTERFACE *contextInterface;
1016 BYTE hash[20];
1018 switch (addedType)
1020 case CERT_STORE_CERTIFICATE_CONTEXT:
1021 contextInterface = &gCertInterface;
1022 break;
1023 case CERT_STORE_CRL_CONTEXT:
1024 contextInterface = &gCRLInterface;
1025 break;
1026 case CERT_STORE_CTL_CONTEXT:
1027 contextInterface = &gCTLInterface;
1028 break;
1029 default:
1030 contextInterface = NULL;
1032 if (contextInterface)
1034 size = sizeof(hash);
1035 if (contextInterface->getProp(context,
1036 CERT_HASH_PROP_ID, hash, &size))
1038 WCHAR asciiHash[20 * 2 + 1];
1040 CRYPT_HashToStr(hash, asciiHash);
1041 TRACE("comparing %s\n",
1042 debugstr_w(asciiHash));
1043 TRACE("with %s\n", debugstr_w(subKeyName));
1044 if (!lstrcmpW(asciiHash, subKeyName))
1046 TRACE("hash matches, adding\n");
1047 contextInterface->addContextToStore(
1048 store->memStore, context,
1049 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1051 else
1052 TRACE("hash doesn't match, ignoring\n");
1054 contextInterface->free(context);
1058 CryptMemFree(buf);
1060 RegCloseKey(subKey);
1062 /* Ignore intermediate errors, continue enumerating */
1063 rc = ERROR_SUCCESS;
1065 } while (!rc);
1068 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO *store)
1070 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1071 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1072 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1073 DWORD i;
1075 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1077 HKEY key;
1078 LONG rc;
1080 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1081 &key, NULL);
1082 if (!rc)
1084 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1085 RegCloseKey(key);
1090 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1091 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
1092 DWORD len)
1094 WCHAR asciiHash[20 * 2 + 1];
1095 LONG rc;
1096 HKEY subKey;
1097 BOOL ret;
1099 CRYPT_HashToStr(hash, asciiHash);
1100 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1101 &subKey, NULL);
1102 if (!rc)
1104 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1105 RegCloseKey(subKey);
1107 if (!rc)
1108 ret = TRUE;
1109 else
1111 SetLastError(rc);
1112 ret = FALSE;
1114 return ret;
1117 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1118 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1120 const void *context = NULL;
1121 BOOL ret;
1123 do {
1124 context = contextInterface->enumContextsInStore(memStore, context);
1125 if (context)
1127 BYTE hash[20];
1128 DWORD hashSize = sizeof(hash);
1130 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1131 &hashSize);
1132 if (ret)
1134 DWORD size = 0;
1135 LPBYTE buf = NULL;
1137 ret = contextInterface->serialize(context, 0, NULL, &size);
1138 if (size)
1139 buf = CryptMemAlloc(size);
1140 if (buf)
1142 ret = contextInterface->serialize(context, 0, buf, &size);
1143 if (ret)
1144 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1146 CryptMemFree(buf);
1149 else
1150 ret = TRUE;
1151 } while (ret && context != NULL);
1152 if (context)
1153 contextInterface->free(context);
1154 return ret;
1157 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1159 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1160 static const WINE_CONTEXT_INTERFACE * const interfaces[] = { &gCertInterface,
1161 &gCRLInterface, &gCTLInterface };
1162 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1163 NULL };
1164 BOOL ret = TRUE;
1165 DWORD i;
1167 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1169 HKEY key;
1170 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1171 KEY_ALL_ACCESS, NULL, &key, NULL);
1173 if (!rc)
1175 if (listToDelete[i])
1177 PWINE_HASH_TO_DELETE toDelete, next;
1178 WCHAR asciiHash[20 * 2 + 1];
1180 EnterCriticalSection(&store->cs);
1181 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1182 WINE_HASH_TO_DELETE, entry)
1184 LONG rc;
1186 CRYPT_HashToStr(toDelete->hash, asciiHash);
1187 TRACE("Removing %s\n", debugstr_w(asciiHash));
1188 rc = RegDeleteKeyW(key, asciiHash);
1189 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1191 SetLastError(rc);
1192 ret = FALSE;
1194 list_remove(&toDelete->entry);
1195 CryptMemFree(toDelete);
1197 LeaveCriticalSection(&store->cs);
1199 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1200 store->memStore);
1201 RegCloseKey(key);
1203 else
1205 SetLastError(rc);
1206 ret = FALSE;
1209 return ret;
1212 /* If force is true or the registry store is dirty, writes the contents of the
1213 * store to the registry.
1215 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1217 BOOL ret;
1219 TRACE("(%p, %d)\n", store, force);
1221 if (store->dirty || force)
1222 ret = CRYPT_RegWriteToReg(store);
1223 else
1224 ret = TRUE;
1225 return ret;
1228 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1230 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1232 TRACE("(%p, %08x)\n", store, dwFlags);
1233 if (dwFlags)
1234 FIXME("Unimplemented flags: %08x\n", dwFlags);
1236 CRYPT_RegFlushStore(store, FALSE);
1237 RegCloseKey(store->key);
1238 store->cs.DebugInfo->Spare[0] = 0;
1239 DeleteCriticalSection(&store->cs);
1240 CryptMemFree(store);
1243 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1244 const void *context, DWORD dwFlags)
1246 BOOL ret;
1248 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1250 store->dirty = TRUE;
1251 ret = TRUE;
1253 else
1254 ret = FALSE;
1255 return ret;
1258 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1259 struct list *deleteList, const void *context,
1260 PCWINE_CONTEXT_INTERFACE contextInterface)
1262 BOOL ret;
1264 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1266 SetLastError(ERROR_ACCESS_DENIED);
1267 ret = FALSE;
1269 else
1271 PWINE_HASH_TO_DELETE toDelete =
1272 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1274 if (toDelete)
1276 DWORD size = sizeof(toDelete->hash);
1278 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1279 toDelete->hash, &size);
1280 if (ret)
1282 EnterCriticalSection(&store->cs);
1283 list_add_tail(deleteList, &toDelete->entry);
1284 LeaveCriticalSection(&store->cs);
1286 else
1288 CryptMemFree(toDelete);
1289 ret = FALSE;
1292 else
1293 ret = FALSE;
1294 if (ret)
1295 store->dirty = TRUE;
1297 return ret;
1300 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1301 PCCERT_CONTEXT cert, DWORD dwFlags)
1303 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1305 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1307 return CRYPT_RegWriteContext(store, cert, dwFlags);
1310 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1311 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1313 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1315 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1317 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1318 pCertInterface);
1321 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1322 PCCRL_CONTEXT crl, DWORD dwFlags)
1324 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1326 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1328 return CRYPT_RegWriteContext(store, crl, dwFlags);
1331 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1332 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1334 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1336 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1338 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1339 pCRLInterface);
1342 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1343 DWORD dwCtrlType, void const *pvCtrlPara)
1345 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1346 BOOL ret;
1348 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1349 pvCtrlPara);
1351 switch (dwCtrlType)
1353 case CERT_STORE_CTRL_RESYNC:
1354 CRYPT_RegFlushStore(store, FALSE);
1355 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1356 CRYPT_RegReadFromReg(store);
1357 ret = TRUE;
1358 break;
1359 case CERT_STORE_CTRL_COMMIT:
1360 ret = CRYPT_RegFlushStore(store,
1361 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1362 break;
1363 default:
1364 FIXME("%d: stub\n", dwCtrlType);
1365 ret = FALSE;
1367 return ret;
1370 static void *regProvFuncs[] = {
1371 CRYPT_RegCloseStore,
1372 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1373 CRYPT_RegWriteCert,
1374 CRYPT_RegDeleteCert,
1375 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1376 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1377 CRYPT_RegWriteCRL,
1378 CRYPT_RegDeleteCRL,
1379 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1380 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1381 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1382 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1383 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1384 CRYPT_RegControl,
1387 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1388 DWORD dwFlags, const void *pvPara)
1390 PWINECRYPT_CERTSTORE store = NULL;
1392 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1394 if (dwFlags & CERT_STORE_DELETE_FLAG)
1396 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
1398 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1399 rc = RegDeleteTreeW((HKEY)pvPara, CRLsW);
1400 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1401 rc = RegDeleteTreeW((HKEY)pvPara, CTLsW);
1402 if (rc == ERROR_NO_MORE_ITEMS)
1403 rc = ERROR_SUCCESS;
1404 SetLastError(rc);
1406 else
1408 HKEY key;
1410 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1411 GetCurrentProcess(), (LPHANDLE)&key,
1412 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1413 TRUE, 0))
1415 PWINECRYPT_CERTSTORE memStore;
1417 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1418 if (memStore)
1420 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1421 sizeof(WINE_REGSTOREINFO));
1423 if (regInfo)
1425 CERT_STORE_PROV_INFO provInfo = { 0 };
1427 regInfo->dwOpenFlags = dwFlags;
1428 regInfo->cryptProv = hCryptProv;
1429 regInfo->memStore = memStore;
1430 regInfo->key = key;
1431 InitializeCriticalSection(&regInfo->cs);
1432 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
1433 list_init(&regInfo->certsToDelete);
1434 list_init(&regInfo->crlsToDelete);
1435 CRYPT_RegReadFromReg(regInfo);
1436 regInfo->dirty = FALSE;
1437 provInfo.cbSize = sizeof(provInfo);
1438 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1439 sizeof(regProvFuncs[0]);
1440 provInfo.rgpvStoreProvFunc = regProvFuncs;
1441 provInfo.hStoreProv = regInfo;
1442 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1443 &provInfo);
1448 TRACE("returning %p\n", store);
1449 return store;
1452 /* FIXME: this isn't complete for the Root store, in which the top-level
1453 * self-signed CA certs reside. Adding a cert to the Root store should present
1454 * the user with a dialog indicating the consequences of doing so, and asking
1455 * the user to confirm whether the cert should be added.
1457 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1458 DWORD dwFlags, const void *pvPara)
1460 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1461 LPCWSTR storeName = (LPCWSTR)pvPara;
1462 LPWSTR storePath;
1463 PWINECRYPT_CERTSTORE store = NULL;
1464 HKEY root;
1465 LPCWSTR base;
1466 BOOL ret;
1468 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1469 debugstr_w((LPCWSTR)pvPara));
1471 if (!pvPara)
1473 SetLastError(E_INVALIDARG);
1474 return NULL;
1477 ret = TRUE;
1478 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1480 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1481 root = HKEY_LOCAL_MACHINE;
1482 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1483 break;
1484 case CERT_SYSTEM_STORE_CURRENT_USER:
1485 root = HKEY_CURRENT_USER;
1486 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1487 break;
1488 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1489 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1490 * SystemCertificates
1492 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1493 debugstr_w(storeName));
1494 return NULL;
1495 case CERT_SYSTEM_STORE_SERVICES:
1496 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1497 * SystemCertificates
1499 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1500 debugstr_w(storeName));
1501 return NULL;
1502 case CERT_SYSTEM_STORE_USERS:
1503 /* hku\user sid\Software\Microsoft\SystemCertificates */
1504 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1505 debugstr_w(storeName));
1506 return NULL;
1507 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1508 root = HKEY_CURRENT_USER;
1509 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1510 break;
1511 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1512 root = HKEY_LOCAL_MACHINE;
1513 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1514 break;
1515 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1516 /* hklm\Software\Microsoft\EnterpriseCertificates */
1517 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1518 debugstr_w(storeName));
1519 return NULL;
1520 default:
1521 SetLastError(E_INVALIDARG);
1522 return NULL;
1525 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1526 sizeof(WCHAR));
1527 if (storePath)
1529 LONG rc;
1530 HKEY key;
1531 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1532 KEY_ALL_ACCESS;
1534 wsprintfW(storePath, fmt, base, storeName);
1535 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1536 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1537 else
1539 DWORD disp;
1541 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1542 &key, &disp);
1543 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1544 disp == REG_OPENED_EXISTING_KEY)
1546 RegCloseKey(key);
1547 rc = ERROR_FILE_EXISTS;
1550 if (!rc)
1552 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1553 RegCloseKey(key);
1555 else
1556 SetLastError(rc);
1557 CryptMemFree(storePath);
1559 return store;
1562 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1563 DWORD dwFlags, const void *pvPara)
1565 int len;
1566 PWINECRYPT_CERTSTORE ret = NULL;
1568 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1569 debugstr_a((LPCSTR)pvPara));
1571 if (!pvPara)
1573 SetLastError(ERROR_FILE_NOT_FOUND);
1574 return NULL;
1576 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1577 if (len)
1579 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1581 if (storeName)
1583 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1584 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1585 CryptMemFree(storeName);
1588 return ret;
1591 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1592 DWORD dwFlags, const void *pvPara)
1594 HCERTSTORE store = 0;
1595 BOOL ret;
1597 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1598 debugstr_w((LPCWSTR)pvPara));
1600 if (!pvPara)
1602 SetLastError(ERROR_FILE_NOT_FOUND);
1603 return NULL;
1605 /* This returns a different error than system registry stores if the
1606 * location is invalid.
1608 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1610 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1611 case CERT_SYSTEM_STORE_CURRENT_USER:
1612 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1613 case CERT_SYSTEM_STORE_SERVICES:
1614 case CERT_SYSTEM_STORE_USERS:
1615 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1616 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1617 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1618 ret = TRUE;
1619 break;
1620 default:
1621 SetLastError(ERROR_FILE_NOT_FOUND);
1622 ret = FALSE;
1624 if (ret)
1626 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1627 0, hCryptProv, dwFlags, pvPara);
1629 if (regStore)
1631 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1632 CERT_STORE_CREATE_NEW_FLAG, NULL);
1633 CertAddStoreToCollection(store, regStore,
1634 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1635 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1636 CertCloseStore(regStore, 0);
1637 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1638 * stores.
1640 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1641 CERT_SYSTEM_STORE_CURRENT_USER)
1643 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1644 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1645 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1646 hCryptProv, dwFlags, pvPara);
1647 if (regStore)
1649 CertAddStoreToCollection(store, regStore,
1650 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1651 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1652 CertCloseStore(regStore, 0);
1657 return (PWINECRYPT_CERTSTORE)store;
1660 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1661 DWORD dwFlags, const void *pvPara)
1663 int len;
1664 PWINECRYPT_CERTSTORE ret = NULL;
1666 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1667 debugstr_a((LPCSTR)pvPara));
1669 if (!pvPara)
1671 SetLastError(ERROR_FILE_NOT_FOUND);
1672 return NULL;
1674 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1675 if (len)
1677 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1679 if (storeName)
1681 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1682 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1683 CryptMemFree(storeName);
1686 return ret;
1689 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1691 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1693 TRACE("(%p, %08x)\n", store, dwFlags);
1694 if (store->dirty)
1695 CRYPT_WriteSerializedFile(store->file, store->memStore);
1696 CertCloseStore(store->memStore, dwFlags);
1697 CloseHandle(store->file);
1698 CryptMemFree(store);
1701 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1702 PCCERT_CONTEXT cert, DWORD dwFlags)
1704 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1706 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1707 store->dirty = TRUE;
1708 return TRUE;
1711 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1712 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1714 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1716 TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1717 store->dirty = TRUE;
1718 return TRUE;
1721 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1722 PCCRL_CONTEXT crl, DWORD dwFlags)
1724 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1726 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1727 store->dirty = TRUE;
1728 return TRUE;
1731 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1732 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1734 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1736 TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1737 store->dirty = TRUE;
1738 return TRUE;
1741 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1742 DWORD dwCtrlType, void const *pvCtrlPara)
1744 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1745 BOOL ret;
1747 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1748 pvCtrlPara);
1750 switch (dwCtrlType)
1752 case CERT_STORE_CTRL_RESYNC:
1753 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1754 CRYPT_ReadSerializedFile(store->file, store);
1755 ret = TRUE;
1756 break;
1757 case CERT_STORE_CTRL_COMMIT:
1758 if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1760 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1761 ret = FALSE;
1763 else if (store->dirty)
1764 ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1765 else
1766 ret = TRUE;
1767 break;
1768 default:
1769 FIXME("%d: stub\n", dwCtrlType);
1770 ret = FALSE;
1772 return ret;
1775 static void *fileProvFuncs[] = {
1776 CRYPT_FileCloseStore,
1777 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1778 CRYPT_FileWriteCert,
1779 CRYPT_FileDeleteCert,
1780 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1781 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1782 CRYPT_FileWriteCRL,
1783 CRYPT_FileDeleteCRL,
1784 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1785 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1786 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1787 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1788 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1789 CRYPT_FileControl,
1792 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1793 DWORD dwFlags, const void *pvPara)
1795 PWINECRYPT_CERTSTORE store = NULL;
1796 HANDLE file = (HANDLE)pvPara;
1798 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1800 if (!pvPara)
1802 SetLastError(ERROR_INVALID_HANDLE);
1803 return NULL;
1805 if (dwFlags & CERT_STORE_DELETE_FLAG)
1807 SetLastError(E_INVALIDARG);
1808 return NULL;
1810 if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1811 (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1813 SetLastError(E_INVALIDARG);
1814 return NULL;
1817 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1818 GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1819 GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1821 PWINECRYPT_CERTSTORE memStore;
1823 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1824 if (memStore)
1826 if (CRYPT_ReadSerializedFile(file, memStore))
1828 PWINE_FILESTOREINFO info = CryptMemAlloc(
1829 sizeof(WINE_FILESTOREINFO));
1831 if (info)
1833 CERT_STORE_PROV_INFO provInfo = { 0 };
1835 info->dwOpenFlags = dwFlags;
1836 info->cryptProv = hCryptProv;
1837 info->memStore = memStore;
1838 info->file = file;
1839 info->dirty = FALSE;
1840 provInfo.cbSize = sizeof(provInfo);
1841 provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1842 sizeof(fileProvFuncs[0]);
1843 provInfo.rgpvStoreProvFunc = fileProvFuncs;
1844 provInfo.hStoreProv = info;
1845 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1846 &provInfo);
1851 TRACE("returning %p\n", store);
1852 return store;
1855 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1856 DWORD dwFlags, const void *pvPara)
1858 HCERTSTORE store = 0;
1859 LPCWSTR fileName = (LPCWSTR)pvPara;
1860 DWORD access, create;
1861 HANDLE file;
1863 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1865 if (!fileName)
1867 SetLastError(ERROR_PATH_NOT_FOUND);
1868 return NULL;
1871 access = GENERIC_READ;
1872 if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1873 access |= GENERIC_WRITE;
1874 if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1875 create = CREATE_NEW;
1876 else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1877 create = OPEN_EXISTING;
1878 else
1879 create = OPEN_ALWAYS;
1880 file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1881 FILE_ATTRIBUTE_NORMAL, NULL);
1882 if (file != INVALID_HANDLE_VALUE)
1884 /* FIXME: need to check whether it's a serialized store; if not, fall
1885 * back to a PKCS#7 signed message, then to a single serialized cert.
1887 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1888 file);
1889 CloseHandle(file);
1891 return (PWINECRYPT_CERTSTORE)store;
1894 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1895 DWORD dwFlags, const void *pvPara)
1897 int len;
1898 PWINECRYPT_CERTSTORE ret = NULL;
1900 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1901 debugstr_a((LPCSTR)pvPara));
1903 if (!pvPara)
1905 SetLastError(ERROR_FILE_NOT_FOUND);
1906 return NULL;
1908 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1909 if (len)
1911 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1913 if (storeName)
1915 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1916 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1917 CryptMemFree(storeName);
1920 return ret;
1923 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
1924 DWORD dwFlags, const void *pvPara)
1926 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
1927 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
1928 else
1929 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
1930 debugstr_w((LPCWSTR)pvPara));
1931 return NULL;
1934 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1935 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1936 const void* pvPara)
1938 WINECRYPT_CERTSTORE *hcs;
1939 StoreOpenFunc openFunc = NULL;
1941 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
1942 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1944 if (!HIWORD(lpszStoreProvider))
1946 switch (LOWORD(lpszStoreProvider))
1948 case (int)CERT_STORE_PROV_MEMORY:
1949 openFunc = CRYPT_MemOpenStore;
1950 break;
1951 case (int)CERT_STORE_PROV_FILE:
1952 openFunc = CRYPT_FileOpenStore;
1953 break;
1954 case (int)CERT_STORE_PROV_REG:
1955 openFunc = CRYPT_RegOpenStore;
1956 break;
1957 case (int)CERT_STORE_PROV_FILENAME_A:
1958 openFunc = CRYPT_FileNameOpenStoreA;
1959 break;
1960 case (int)CERT_STORE_PROV_FILENAME_W:
1961 openFunc = CRYPT_FileNameOpenStoreW;
1962 break;
1963 case (int)CERT_STORE_PROV_COLLECTION:
1964 openFunc = CRYPT_CollectionOpenStore;
1965 break;
1966 case (int)CERT_STORE_PROV_SYSTEM_A:
1967 openFunc = CRYPT_SysOpenStoreA;
1968 break;
1969 case (int)CERT_STORE_PROV_SYSTEM_W:
1970 openFunc = CRYPT_SysOpenStoreW;
1971 break;
1972 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
1973 openFunc = CRYPT_SysRegOpenStoreA;
1974 break;
1975 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
1976 openFunc = CRYPT_SysRegOpenStoreW;
1977 break;
1978 case (int)CERT_STORE_PROV_PHYSICAL_W:
1979 openFunc = CRYPT_PhysOpenStoreW;
1980 break;
1981 default:
1982 if (LOWORD(lpszStoreProvider))
1983 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
1986 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
1987 openFunc = CRYPT_MemOpenStore;
1988 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
1989 openFunc = CRYPT_FileOpenStore;
1990 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
1991 openFunc = CRYPT_SysOpenStoreW;
1992 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
1993 openFunc = CRYPT_CollectionOpenStore;
1994 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
1995 openFunc = CRYPT_SysRegOpenStoreW;
1996 else
1998 FIXME("unimplemented type %s\n", lpszStoreProvider);
1999 openFunc = NULL;
2002 if (!openFunc)
2003 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2004 hCryptProv, dwFlags, pvPara);
2005 else
2006 hcs = openFunc(hCryptProv, dwFlags, pvPara);
2007 return (HCERTSTORE)hcs;
2010 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
2011 LPCSTR szSubSystemProtocol)
2013 if (!szSubSystemProtocol)
2015 SetLastError(E_INVALIDARG);
2016 return 0;
2018 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2019 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2022 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
2023 LPCWSTR szSubSystemProtocol)
2025 if (!szSubSystemProtocol)
2027 SetLastError(E_INVALIDARG);
2028 return 0;
2030 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2031 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2034 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2035 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2037 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore,
2038 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2039 return TRUE;
2042 #define CertContext_CopyProperties(to, from) \
2043 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2045 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2046 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2047 PCCERT_CONTEXT *ppStoreContext)
2049 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2050 BOOL ret = TRUE;
2051 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2053 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2054 dwAddDisposition, ppStoreContext);
2056 /* Weird case to pass a test */
2057 if (dwAddDisposition == 0)
2059 SetLastError(STATUS_ACCESS_VIOLATION);
2060 return FALSE;
2062 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2064 BYTE hashToAdd[20];
2065 DWORD size = sizeof(hashToAdd);
2067 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2068 hashToAdd, &size);
2069 if (ret)
2071 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2073 existing = CertFindCertificateInStore(hCertStore,
2074 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2075 NULL);
2079 switch (dwAddDisposition)
2081 case CERT_STORE_ADD_ALWAYS:
2082 toAdd = CertDuplicateCertificateContext(pCertContext);
2083 break;
2084 case CERT_STORE_ADD_NEW:
2085 if (existing)
2087 TRACE("found matching certificate, not adding\n");
2088 SetLastError(CRYPT_E_EXISTS);
2089 ret = FALSE;
2091 else
2092 toAdd = CertDuplicateCertificateContext(pCertContext);
2093 break;
2094 case CERT_STORE_ADD_REPLACE_EXISTING:
2095 toAdd = CertDuplicateCertificateContext(pCertContext);
2096 break;
2097 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2098 toAdd = CertDuplicateCertificateContext(pCertContext);
2099 if (existing)
2100 CertContext_CopyProperties(toAdd, existing);
2101 break;
2102 case CERT_STORE_ADD_USE_EXISTING:
2103 if (existing)
2105 CertContext_CopyProperties(existing, pCertContext);
2106 *ppStoreContext = CertDuplicateCertificateContext(existing);
2108 else
2109 toAdd = CertDuplicateCertificateContext(pCertContext);
2110 break;
2111 default:
2112 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2113 ret = FALSE;
2116 if (toAdd)
2118 if (store)
2119 ret = store->certs.addContext(store, (void *)toAdd,
2120 (void *)existing, (const void **)ppStoreContext);
2121 else if (ppStoreContext)
2122 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2123 CertFreeCertificateContext(toAdd);
2125 CertFreeCertificateContext(existing);
2127 TRACE("returning %d\n", ret);
2128 return ret;
2131 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2132 PCCERT_CONTEXT pPrev)
2134 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2135 PCCERT_CONTEXT ret;
2137 TRACE("(%p, %p)\n", hCertStore, pPrev);
2138 if (!hCertStore)
2139 ret = NULL;
2140 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2141 ret = NULL;
2142 else
2143 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2144 return ret;
2147 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2149 BOOL ret;
2151 TRACE("(%p)\n", pCertContext);
2153 if (!pCertContext)
2154 ret = TRUE;
2155 else if (!pCertContext->hCertStore)
2157 ret = TRUE;
2158 CertFreeCertificateContext(pCertContext);
2160 else
2162 PWINECRYPT_CERTSTORE hcs =
2163 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2165 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2166 ret = FALSE;
2167 else
2168 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2169 CertFreeCertificateContext(pCertContext);
2171 return ret;
2174 #define CrlContext_CopyProperties(to, from) \
2175 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2177 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2178 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2179 PCCRL_CONTEXT* ppStoreContext)
2181 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2182 BOOL ret = TRUE;
2183 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2185 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2186 dwAddDisposition, ppStoreContext);
2188 /* Weird case to pass a test */
2189 if (dwAddDisposition == 0)
2191 SetLastError(STATUS_ACCESS_VIOLATION);
2192 return FALSE;
2194 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2196 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2197 pCrlContext, NULL);
2200 switch (dwAddDisposition)
2202 case CERT_STORE_ADD_ALWAYS:
2203 toAdd = CertDuplicateCRLContext(pCrlContext);
2204 break;
2205 case CERT_STORE_ADD_NEW:
2206 if (existing)
2208 TRACE("found matching CRL, not adding\n");
2209 SetLastError(CRYPT_E_EXISTS);
2210 ret = FALSE;
2212 else
2213 toAdd = CertDuplicateCRLContext(pCrlContext);
2214 break;
2215 case CERT_STORE_ADD_NEWER:
2216 if (existing)
2218 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2219 &pCrlContext->pCrlInfo->ThisUpdate);
2221 if (newer < 0)
2222 toAdd = CertDuplicateCRLContext(pCrlContext);
2223 else
2225 TRACE("existing CRL is newer, not adding\n");
2226 SetLastError(CRYPT_E_EXISTS);
2227 ret = FALSE;
2230 else
2231 toAdd = CertDuplicateCRLContext(pCrlContext);
2232 break;
2233 case CERT_STORE_ADD_REPLACE_EXISTING:
2234 toAdd = CertDuplicateCRLContext(pCrlContext);
2235 break;
2236 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2237 toAdd = CertDuplicateCRLContext(pCrlContext);
2238 if (existing)
2239 CrlContext_CopyProperties(toAdd, existing);
2240 break;
2241 case CERT_STORE_ADD_USE_EXISTING:
2242 if (existing)
2243 CrlContext_CopyProperties(existing, pCrlContext);
2244 break;
2245 default:
2246 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2247 ret = FALSE;
2250 if (toAdd)
2252 if (store)
2253 ret = store->crls.addContext(store, (void *)toAdd,
2254 (void *)existing, (const void **)ppStoreContext);
2255 else if (ppStoreContext)
2256 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2257 CertFreeCRLContext(toAdd);
2259 CertFreeCRLContext(existing);
2261 TRACE("returning %d\n", ret);
2262 return ret;
2265 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2267 BOOL ret;
2269 TRACE("(%p)\n", pCrlContext);
2271 if (!pCrlContext)
2272 ret = TRUE;
2273 else if (!pCrlContext->hCertStore)
2275 ret = TRUE;
2276 CertFreeCRLContext(pCrlContext);
2278 else
2280 PWINECRYPT_CERTSTORE hcs =
2281 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2283 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2284 ret = FALSE;
2285 else
2286 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2287 CertFreeCRLContext(pCrlContext);
2289 return ret;
2292 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2293 PCCRL_CONTEXT pPrev)
2295 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2296 PCCRL_CONTEXT ret;
2298 TRACE("(%p, %p)\n", hCertStore, pPrev);
2299 if (!hCertStore)
2300 ret = NULL;
2301 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2302 ret = NULL;
2303 else
2304 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2305 return ret;
2308 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2309 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2311 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2312 cbCtlEncoded);
2313 return NULL;
2316 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2317 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2318 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2320 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2321 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2322 ppCtlContext);
2323 return FALSE;
2326 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2327 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2328 PCCTL_CONTEXT* ppStoreContext)
2330 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2331 dwAddDisposition, ppStoreContext);
2332 return TRUE;
2335 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2337 FIXME("(%p): stub\n", pCtlContext );
2338 return pCtlContext;
2341 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2343 FIXME("(%p): stub\n", pCtlContext );
2344 return TRUE;
2347 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2349 FIXME("(%p): stub\n", pCtlContext);
2350 return TRUE;
2353 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2354 PCCTL_CONTEXT pPrev)
2356 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2357 return NULL;
2360 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2362 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2364 TRACE("(%p)\n", hCertStore);
2366 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2367 InterlockedIncrement(&hcs->ref);
2368 return hCertStore;
2371 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2373 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2375 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2377 if( ! hCertStore )
2378 return TRUE;
2380 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2381 return FALSE;
2383 if (InterlockedDecrement(&hcs->ref) == 0)
2385 TRACE("%p's ref count is 0, freeing\n", hcs);
2386 hcs->dwMagic = 0;
2387 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2388 CryptReleaseContext(hcs->cryptProv, 0);
2389 hcs->closeStore(hcs, dwFlags);
2391 else
2392 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2393 return TRUE;
2396 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2397 DWORD dwCtrlType, void const *pvCtrlPara)
2399 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2400 BOOL ret;
2402 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2403 pvCtrlPara);
2405 if (!hcs)
2406 ret = FALSE;
2407 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2408 ret = FALSE;
2409 else
2411 if (hcs->control)
2412 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2413 else
2414 ret = TRUE;
2416 return ret;
2419 BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2420 void *pvData, DWORD *pcbData)
2422 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2423 BOOL ret = FALSE;
2425 TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);
2427 switch (dwPropId)
2429 case CERT_ACCESS_STATE_PROP_ID:
2430 if (!pvData)
2432 *pcbData = sizeof(DWORD);
2433 ret = TRUE;
2435 else if (*pcbData < sizeof(DWORD))
2437 SetLastError(ERROR_MORE_DATA);
2438 *pcbData = sizeof(DWORD);
2440 else
2442 DWORD state = 0;
2444 if (store->type != StoreTypeMem &&
2445 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2446 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2447 *(DWORD *)pvData = state;
2448 ret = TRUE;
2450 break;
2451 default:
2452 if (store->properties)
2454 CRYPT_DATA_BLOB blob;
2456 ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
2457 &blob);
2458 if (ret)
2460 if (!pvData)
2461 *pcbData = blob.cbData;
2462 else if (*pcbData < blob.cbData)
2464 SetLastError(ERROR_MORE_DATA);
2465 *pcbData = blob.cbData;
2466 ret = FALSE;
2468 else
2470 memcpy(pvData, blob.pbData, blob.cbData);
2471 *pcbData = blob.cbData;
2474 else
2475 SetLastError(CRYPT_E_NOT_FOUND);
2477 else
2478 SetLastError(CRYPT_E_NOT_FOUND);
2480 return ret;
2483 BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2484 DWORD dwFlags, const void *pvData)
2486 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2487 BOOL ret = FALSE;
2489 TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);
2491 if (!store->properties)
2492 store->properties = ContextPropertyList_Create();
2493 switch (dwPropId)
2495 case CERT_ACCESS_STATE_PROP_ID:
2496 SetLastError(E_INVALIDARG);
2497 break;
2498 default:
2499 if (pvData)
2501 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvData;
2503 ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
2504 blob->pbData, blob->cbData);
2506 else
2508 ContextPropertyList_RemoveProperty(store->properties, dwPropId);
2509 ret = TRUE;
2512 return ret;
2515 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2516 DWORD dwPropId)
2518 FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2519 return 0;
2522 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2523 DWORD dwPropId, void *pvData, DWORD *pcbData)
2525 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2526 return FALSE;
2529 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2530 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2532 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2533 pvData);
2534 return FALSE;
2537 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2538 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2540 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2541 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2542 PWINE_STORE_LIST_ENTRY entry;
2543 BOOL ret;
2545 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2546 dwUpdateFlags, dwPriority);
2548 if (!collection || !sibling)
2549 return TRUE;
2550 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2552 SetLastError(E_INVALIDARG);
2553 return FALSE;
2555 if (collection->hdr.type != StoreTypeCollection)
2557 SetLastError(E_INVALIDARG);
2558 return FALSE;
2560 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2562 SetLastError(E_INVALIDARG);
2563 return FALSE;
2566 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2567 if (entry)
2569 InterlockedIncrement(&sibling->ref);
2570 TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2571 entry->store = sibling;
2572 entry->dwUpdateFlags = dwUpdateFlags;
2573 entry->dwPriority = dwPriority;
2574 list_init(&entry->entry);
2575 TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2576 EnterCriticalSection(&collection->cs);
2577 if (dwPriority)
2579 PWINE_STORE_LIST_ENTRY cursor;
2580 BOOL added = FALSE;
2582 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2583 WINE_STORE_LIST_ENTRY, entry)
2585 if (cursor->dwPriority < dwPriority)
2587 list_add_before(&cursor->entry, &entry->entry);
2588 added = TRUE;
2589 break;
2592 if (!added)
2593 list_add_tail(&collection->stores, &entry->entry);
2595 else
2596 list_add_tail(&collection->stores, &entry->entry);
2597 LeaveCriticalSection(&collection->cs);
2598 ret = TRUE;
2600 else
2601 ret = FALSE;
2602 return ret;
2605 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2606 HCERTSTORE hSiblingStore)
2608 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2609 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2610 PWINE_STORE_LIST_ENTRY store, next;
2612 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2614 if (!collection || !sibling)
2615 return;
2616 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2618 SetLastError(E_INVALIDARG);
2619 return;
2621 if (collection->hdr.type != StoreTypeCollection)
2622 return;
2623 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2625 SetLastError(E_INVALIDARG);
2626 return;
2628 EnterCriticalSection(&collection->cs);
2629 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2630 WINE_STORE_LIST_ENTRY, entry)
2632 if (store->store == sibling)
2634 list_remove(&store->entry);
2635 CertCloseStore(store->store, 0);
2636 CryptMemFree(store);
2637 break;
2640 LeaveCriticalSection(&collection->cs);
2643 static LONG CRYPT_OpenParentStore(DWORD dwFlags,
2644 void *pvSystemStoreLocationPara, HKEY *key)
2646 HKEY root;
2647 LPCWSTR base;
2649 TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);
2651 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
2653 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
2654 root = HKEY_LOCAL_MACHINE;
2655 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2656 break;
2657 case CERT_SYSTEM_STORE_CURRENT_USER:
2658 root = HKEY_CURRENT_USER;
2659 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2660 break;
2661 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
2662 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2663 * SystemCertificates
2665 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
2666 return ERROR_FILE_NOT_FOUND;
2667 case CERT_SYSTEM_STORE_SERVICES:
2668 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2669 * SystemCertificates
2671 FIXME("CERT_SYSTEM_STORE_SERVICES\n");
2672 return ERROR_FILE_NOT_FOUND;
2673 case CERT_SYSTEM_STORE_USERS:
2674 /* hku\user sid\Software\Microsoft\SystemCertificates */
2675 FIXME("CERT_SYSTEM_STORE_USERS\n");
2676 return ERROR_FILE_NOT_FOUND;
2677 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
2678 root = HKEY_CURRENT_USER;
2679 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2680 break;
2681 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
2682 root = HKEY_LOCAL_MACHINE;
2683 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2684 break;
2685 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
2686 /* hklm\Software\Microsoft\EnterpriseCertificates */
2687 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
2688 return ERROR_FILE_NOT_FOUND;
2689 default:
2690 return ERROR_FILE_NOT_FOUND;
2693 return RegOpenKeyExW(root, base, 0, KEY_READ, key);
2696 BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
2697 void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
2699 BOOL ret = FALSE;
2700 LONG rc;
2701 HKEY key;
2703 TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
2704 pfnEnum);
2706 rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
2707 if (!rc)
2709 DWORD index = 0;
2710 CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
2712 ret = TRUE;
2713 do {
2714 WCHAR name[MAX_PATH];
2715 DWORD size = sizeof(name) / sizeof(name[0]);
2717 rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
2718 NULL);
2719 if (!rc)
2720 ret = pfnEnum(name, 0, &info, NULL, pvArg);
2721 } while (ret && !rc);
2722 if (ret && rc != ERROR_NO_MORE_ITEMS)
2723 SetLastError(rc);
2725 else
2726 SetLastError(rc);
2727 return ret;