crypt32: Remove some unnecessary casts.
[wine/multimedia.git] / dlls / crypt32 / store.c
blobcc5fccd3a4890de326d45295368b9895fea65190
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_FUNCS
112 AddFunc addContext;
113 EnumFunc enumContext;
114 DeleteFunc deleteContext;
115 } CONTEXT_FUNCS, *PCONTEXT_FUNCS;
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_FUNCS certs;
139 CONTEXT_FUNCS 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_MSGSTOREINFO
180 DWORD dwOpenFlags;
181 HCRYPTPROV cryptProv;
182 PWINECRYPT_CERTSTORE memStore;
183 HCRYPTMSG msg;
184 } WINE_MSGSTOREINFO, *PWINE_MSGSTOREINFO;
186 typedef struct _WINE_STORE_LIST_ENTRY
188 PWINECRYPT_CERTSTORE store;
189 DWORD dwUpdateFlags;
190 DWORD dwPriority;
191 struct list entry;
192 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
194 typedef struct _WINE_COLLECTIONSTORE
196 WINECRYPT_CERTSTORE hdr;
197 CRITICAL_SECTION cs;
198 struct list stores;
199 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
201 typedef struct _WINE_PROVIDERSTORE
203 WINECRYPT_CERTSTORE hdr;
204 DWORD dwStoreProvFlags;
205 PWINECRYPT_CERTSTORE memStore;
206 HCERTSTOREPROV hStoreProv;
207 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
208 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
209 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
210 PFN_CERT_STORE_PROV_WRITE_CRL provWriteCrl;
211 PFN_CERT_STORE_PROV_DELETE_CRL provDeleteCrl;
212 PFN_CERT_STORE_PROV_CONTROL provControl;
213 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
215 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
216 DWORD dwFlags, CertStoreType type)
218 store->ref = 1;
219 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
220 store->type = type;
221 if (!hCryptProv)
223 hCryptProv = CRYPT_GetDefaultProvider();
224 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
226 store->cryptProv = hCryptProv;
227 store->dwOpenFlags = dwFlags;
228 store->properties = NULL;
231 static void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store)
233 if (store->properties)
234 ContextPropertyList_Free(store->properties);
235 CryptMemFree(store);
238 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
239 void *toReplace, const void **ppStoreContext)
241 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
242 PCERT_CONTEXT context;
244 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
246 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
247 if (context)
249 context->hCertStore = store;
250 if (ppStoreContext)
251 *ppStoreContext = CertDuplicateCertificateContext(context);
253 return context ? TRUE : FALSE;
256 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
258 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
259 void *ret;
261 TRACE("(%p, %p)\n", store, pPrev);
263 ret = ContextList_Enum(ms->certs, pPrev);
264 if (!ret)
265 SetLastError(CRYPT_E_NOT_FOUND);
267 TRACE("returning %p\n", ret);
268 return ret;
271 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
273 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
275 ContextList_Delete(ms->certs, pCertContext);
276 return TRUE;
279 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
280 void *toReplace, const void **ppStoreContext)
282 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
283 PCRL_CONTEXT context;
285 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
287 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
288 if (context)
290 context->hCertStore = store;
291 if (ppStoreContext)
292 *ppStoreContext = CertDuplicateCRLContext(context);
294 return context ? TRUE : FALSE;
297 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
299 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
300 void *ret;
302 TRACE("(%p, %p)\n", store, pPrev);
304 ret = ContextList_Enum(ms->crls, pPrev);
305 if (!ret)
306 SetLastError(CRYPT_E_NOT_FOUND);
308 TRACE("returning %p\n", ret);
309 return ret;
312 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
314 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
316 ContextList_Delete(ms->crls, pCrlContext);
317 return TRUE;
320 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
322 ContextList_Empty(store->certs);
323 ContextList_Empty(store->crls);
326 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
328 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
330 TRACE("(%p, %08x)\n", store, dwFlags);
331 if (dwFlags)
332 FIXME("Unimplemented flags: %08x\n", dwFlags);
334 ContextList_Free(store->certs);
335 ContextList_Free(store->crls);
336 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
339 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
340 DWORD dwFlags, const void *pvPara)
342 PWINE_MEMSTORE store;
344 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
346 if (dwFlags & CERT_STORE_DELETE_FLAG)
348 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
349 store = NULL;
351 else
353 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
354 if (store)
356 memset(store, 0, sizeof(WINE_MEMSTORE));
357 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
358 store->hdr.closeStore = CRYPT_MemCloseStore;
359 store->hdr.certs.addContext = CRYPT_MemAddCert;
360 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
361 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
362 store->hdr.crls.addContext = CRYPT_MemAddCrl;
363 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
364 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
365 store->hdr.control = NULL;
366 store->certs = ContextList_Create(pCertInterface,
367 sizeof(CERT_CONTEXT));
368 store->crls = ContextList_Create(pCRLInterface,
369 sizeof(CRL_CONTEXT));
372 return (PWINECRYPT_CERTSTORE)store;
375 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
377 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
378 PWINE_STORE_LIST_ENTRY entry, next;
380 TRACE("(%p, %08x)\n", store, dwFlags);
382 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
383 entry)
385 TRACE("closing %p\n", entry);
386 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
387 CryptMemFree(entry);
389 cs->cs.DebugInfo->Spare[0] = 0;
390 DeleteCriticalSection(&cs->cs);
391 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
394 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
395 PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
396 BOOL addRef)
398 void *ret = Context_CreateLinkContext(contextSize, child,
399 sizeof(PWINE_STORE_LIST_ENTRY), addRef);
401 if (ret)
402 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
403 = storeEntry;
405 return ret;
408 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
409 unsigned int contextFuncsOffset, void *context, void *toReplace, unsigned int contextSize,
410 void **pChildContext)
412 BOOL ret;
413 void *childContext = NULL;
414 PWINE_STORE_LIST_ENTRY storeEntry = NULL;
416 TRACE("(%p, %d, %p, %p, %d)\n", store, contextFuncsOffset, context,
417 toReplace, contextSize);
419 ret = FALSE;
420 if (toReplace)
422 void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
423 PCONTEXT_FUNCS contextFuncs;
425 storeEntry = *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(toReplace,
426 contextSize);
427 contextFuncs = (PCONTEXT_FUNCS)((LPBYTE)storeEntry->store +
428 contextFuncsOffset);
429 ret = contextFuncs->addContext(storeEntry->store, context,
430 existingLinked, childContext);
432 else
434 PWINE_STORE_LIST_ENTRY entry, next;
436 EnterCriticalSection(&store->cs);
437 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
438 WINE_STORE_LIST_ENTRY, entry)
440 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
442 PCONTEXT_FUNCS contextFuncs = (PCONTEXT_FUNCS)(
443 (LPBYTE)entry->store + contextFuncsOffset);
445 storeEntry = entry;
446 ret = contextFuncs->addContext(entry->store, context, NULL,
447 (const void **)&childContext);
448 break;
451 LeaveCriticalSection(&store->cs);
452 if (!storeEntry)
453 SetLastError(E_ACCESSDENIED);
455 *pChildContext = childContext;
456 return ret;
459 /* Advances a collection enumeration by one context, if possible, where
460 * advancing means:
461 * - calling the current store's enumeration function once, and returning
462 * the enumerated context if one is returned
463 * - moving to the next store if the current store has no more items, and
464 * recursively calling itself to get the next item.
465 * Returns NULL if the collection contains no more items or on error.
466 * Assumes the collection store's lock is held.
468 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store,
469 PWINE_STORE_LIST_ENTRY storeEntry, size_t contextFuncsOffset,
470 PCWINE_CONTEXT_INTERFACE contextInterface, void *pPrev, size_t contextSize)
472 void *ret, *child;
473 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
474 PCONTEXT_FUNCS contextFuncs = (PCONTEXT_FUNCS)((LPBYTE)storeEntry->store +
475 contextFuncsOffset);
477 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
479 if (pPrev)
481 /* Ref-counting funny business: "duplicate" (addref) the child, because
482 * the free(pPrev) below can cause the ref count to become negative.
484 child = Context_GetLinkedContext(pPrev, contextSize);
485 contextInterface->duplicate(child);
486 child = contextFuncs->enumContext(storeEntry->store, child);
487 contextInterface->free(pPrev);
488 pPrev = NULL;
490 else
491 child = contextFuncs->enumContext(storeEntry->store, NULL);
492 if (child)
493 ret = CRYPT_CollectionCreateContextFromChild(store, storeEntry, child,
494 contextSize, FALSE);
495 else
497 if (storeNext)
498 ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
499 WINE_STORE_LIST_ENTRY, entry), contextFuncsOffset,
500 contextInterface, NULL, contextSize);
501 else
503 SetLastError(CRYPT_E_NOT_FOUND);
504 ret = NULL;
507 TRACE("returning %p\n", ret);
508 return ret;
511 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
512 void *toReplace, const void **ppStoreContext)
514 BOOL ret;
515 void *childContext = NULL;
516 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
518 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
519 cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
520 if (ppStoreContext && childContext)
522 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
523 Context_GetExtra(childContext, sizeof(CERT_CONTEXT));
524 PCERT_CONTEXT context =
525 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
526 sizeof(CERT_CONTEXT), TRUE);
528 if (context)
529 context->hCertStore = store;
530 *ppStoreContext = context;
532 CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
533 return ret;
536 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
538 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
539 void *ret;
541 TRACE("(%p, %p)\n", store, pPrev);
543 EnterCriticalSection(&cs->cs);
544 if (pPrev)
546 PWINE_STORE_LIST_ENTRY storeEntry =
547 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
548 sizeof(CERT_CONTEXT));
550 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
551 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
552 sizeof(CERT_CONTEXT));
554 else
556 if (!list_empty(&cs->stores))
558 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
559 WINE_STORE_LIST_ENTRY, entry);
561 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
562 offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
563 sizeof(CERT_CONTEXT));
565 else
567 SetLastError(CRYPT_E_NOT_FOUND);
568 ret = NULL;
571 LeaveCriticalSection(&cs->cs);
572 if (ret)
573 ((PCERT_CONTEXT)ret)->hCertStore = store;
574 TRACE("returning %p\n", ret);
575 return ret;
578 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
579 void *pCertContext)
581 BOOL ret;
583 TRACE("(%p, %p)\n", store, pCertContext);
585 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
586 Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
587 return ret;
590 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
591 void *toReplace, const void **ppStoreContext)
593 BOOL ret;
594 void *childContext = NULL;
595 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
597 ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
598 crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
599 if (ppStoreContext && childContext)
601 PWINE_STORE_LIST_ENTRY storeEntry = *(PWINE_STORE_LIST_ENTRY *)
602 Context_GetExtra(childContext, sizeof(CRL_CONTEXT));
603 PCRL_CONTEXT context =
604 CRYPT_CollectionCreateContextFromChild(cs, storeEntry, childContext,
605 sizeof(CRL_CONTEXT), TRUE);
607 if (context)
608 context->hCertStore = store;
609 *ppStoreContext = context;
611 CertFreeCRLContext((PCCRL_CONTEXT)childContext);
612 return ret;
615 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
617 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
618 void *ret;
620 TRACE("(%p, %p)\n", store, pPrev);
622 EnterCriticalSection(&cs->cs);
623 if (pPrev)
625 PWINE_STORE_LIST_ENTRY storeEntry =
626 *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
627 sizeof(CRL_CONTEXT));
629 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
630 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
631 sizeof(CRL_CONTEXT));
633 else
635 if (!list_empty(&cs->stores))
637 PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
638 WINE_STORE_LIST_ENTRY, entry);
640 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
641 offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
642 sizeof(CRL_CONTEXT));
644 else
646 SetLastError(CRYPT_E_NOT_FOUND);
647 ret = NULL;
650 LeaveCriticalSection(&cs->cs);
651 if (ret)
652 ((PCRL_CONTEXT)ret)->hCertStore = store;
653 TRACE("returning %p\n", ret);
654 return ret;
657 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
658 void *pCrlContext)
660 BOOL ret;
662 TRACE("(%p, %p)\n", store, pCrlContext);
664 ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
665 Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
666 return ret;
669 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
670 DWORD dwFlags, const void *pvPara)
672 PWINE_COLLECTIONSTORE store;
674 if (dwFlags & CERT_STORE_DELETE_FLAG)
676 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
677 store = NULL;
679 else
681 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
682 if (store)
684 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
685 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
686 StoreTypeCollection);
687 store->hdr.closeStore = CRYPT_CollectionCloseStore;
688 store->hdr.certs.addContext = CRYPT_CollectionAddCert;
689 store->hdr.certs.enumContext = CRYPT_CollectionEnumCert;
690 store->hdr.certs.deleteContext = CRYPT_CollectionDeleteCert;
691 store->hdr.crls.addContext = CRYPT_CollectionAddCRL;
692 store->hdr.crls.enumContext = CRYPT_CollectionEnumCRL;
693 store->hdr.crls.deleteContext = CRYPT_CollectionDeleteCRL;
694 InitializeCriticalSection(&store->cs);
695 store->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_COLLECTIONSTORE->cs");
696 list_init(&store->stores);
699 return (PWINECRYPT_CERTSTORE)store;
702 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
704 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
706 TRACE("(%p, %08x)\n", store, dwFlags);
708 if (store->provCloseStore)
709 store->provCloseStore(store->hStoreProv, dwFlags);
710 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
711 CertCloseStore(store->memStore, dwFlags);
712 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
715 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
716 void *toReplace, const void **ppStoreContext)
718 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
719 BOOL ret;
721 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
723 if (toReplace)
724 ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
725 ppStoreContext);
726 else
728 ret = TRUE;
729 if (ps->provWriteCert)
730 ret = ps->provWriteCert(ps->hStoreProv, (PCCERT_CONTEXT)cert,
731 CERT_STORE_PROV_WRITE_ADD_FLAG);
732 if (ret)
733 ret = ps->memStore->certs.addContext(ps->memStore, cert, NULL,
734 ppStoreContext);
736 /* dirty trick: replace the returned context's hCertStore with
737 * store.
739 if (ppStoreContext)
740 (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
741 return ret;
744 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
746 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
747 void *ret;
749 ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
750 if (ret)
752 /* same dirty trick: replace the returned context's hCertStore with
753 * store.
755 ((PCERT_CONTEXT)ret)->hCertStore = store;
757 return ret;
760 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
762 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
763 BOOL ret = TRUE;
765 TRACE("(%p, %p)\n", store, cert);
767 if (ps->provDeleteCert)
768 ret = ps->provDeleteCert(ps->hStoreProv, cert, 0);
769 if (ret)
770 ret = ps->memStore->certs.deleteContext(ps->memStore, cert);
771 return ret;
774 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
775 void *toReplace, const void **ppStoreContext)
777 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
778 BOOL ret;
780 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
782 if (toReplace)
783 ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
784 ppStoreContext);
785 else
787 if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
789 SetLastError(ERROR_ACCESS_DENIED);
790 ret = FALSE;
792 else
794 ret = TRUE;
795 if (ps->provWriteCrl)
796 ret = ps->provWriteCrl(ps->hStoreProv, (PCCRL_CONTEXT)crl,
797 CERT_STORE_PROV_WRITE_ADD_FLAG);
798 if (ret)
799 ret = ps->memStore->crls.addContext(ps->memStore, crl, NULL,
800 ppStoreContext);
803 /* dirty trick: replace the returned context's hCertStore with
804 * store.
806 if (ppStoreContext)
807 (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
808 return ret;
811 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
813 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
814 void *ret;
816 ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
817 if (ret)
819 /* same dirty trick: replace the returned context's hCertStore with
820 * store.
822 ((PCRL_CONTEXT)ret)->hCertStore = store;
824 return ret;
827 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
829 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
830 BOOL ret = TRUE;
832 TRACE("(%p, %p)\n", store, crl);
834 if (ps->provDeleteCrl)
835 ret = ps->provDeleteCrl(ps->hStoreProv, crl, 0);
836 if (ret)
837 ret = ps->memStore->crls.deleteContext(ps->memStore, crl);
838 return ret;
841 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
842 DWORD dwCtrlType, void const *pvCtrlPara)
844 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
845 BOOL ret = TRUE;
847 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
848 pvCtrlPara);
850 if (store->provControl)
851 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
852 pvCtrlPara);
853 return ret;
856 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
857 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo)
859 PWINE_PROVIDERSTORE ret = CryptMemAlloc(sizeof(WINE_PROVIDERSTORE));
861 if (ret)
863 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
864 StoreTypeProvider);
865 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
866 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
868 CertCloseStore(memStore, 0);
869 ret->memStore = NULL;
871 else
872 ret->memStore = memStore;
873 ret->hStoreProv = pProvInfo->hStoreProv;
874 ret->hdr.closeStore = CRYPT_ProvCloseStore;
875 ret->hdr.certs.addContext = CRYPT_ProvAddCert;
876 ret->hdr.certs.enumContext = CRYPT_ProvEnumCert;
877 ret->hdr.certs.deleteContext = CRYPT_ProvDeleteCert;
878 ret->hdr.crls.addContext = CRYPT_ProvAddCRL;
879 ret->hdr.crls.enumContext = CRYPT_ProvEnumCRL;
880 ret->hdr.crls.deleteContext = CRYPT_ProvDeleteCRL;
881 ret->hdr.control = CRYPT_ProvControl;
882 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
883 ret->provCloseStore =
884 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
885 else
886 ret->provCloseStore = NULL;
887 if (pProvInfo->cStoreProvFunc >
888 CERT_STORE_PROV_WRITE_CERT_FUNC)
889 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
890 CERT_STORE_PROV_WRITE_CERT_FUNC];
891 else
892 ret->provWriteCert = NULL;
893 if (pProvInfo->cStoreProvFunc >
894 CERT_STORE_PROV_DELETE_CERT_FUNC)
895 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
896 CERT_STORE_PROV_DELETE_CERT_FUNC];
897 else
898 ret->provDeleteCert = NULL;
899 if (pProvInfo->cStoreProvFunc >
900 CERT_STORE_PROV_WRITE_CRL_FUNC)
901 ret->provWriteCrl = pProvInfo->rgpvStoreProvFunc[
902 CERT_STORE_PROV_WRITE_CRL_FUNC];
903 else
904 ret->provWriteCert = NULL;
905 if (pProvInfo->cStoreProvFunc >
906 CERT_STORE_PROV_DELETE_CRL_FUNC)
907 ret->provDeleteCrl = pProvInfo->rgpvStoreProvFunc[
908 CERT_STORE_PROV_DELETE_CRL_FUNC];
909 else
910 ret->provDeleteCert = NULL;
911 if (pProvInfo->cStoreProvFunc >
912 CERT_STORE_PROV_CONTROL_FUNC)
913 ret->provControl = pProvInfo->rgpvStoreProvFunc[
914 CERT_STORE_PROV_CONTROL_FUNC];
915 else
916 ret->provControl = NULL;
918 return (PWINECRYPT_CERTSTORE)ret;
921 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
922 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
924 static HCRYPTOIDFUNCSET set = NULL;
925 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
926 HCRYPTOIDFUNCADDR hFunc;
927 PWINECRYPT_CERTSTORE ret = NULL;
929 if (!set)
930 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
931 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
932 (void **)&provOpenFunc, &hFunc);
933 if (provOpenFunc)
935 CERT_STORE_PROV_INFO provInfo = { 0 };
937 provInfo.cbSize = sizeof(provInfo);
938 if (dwFlags & CERT_STORE_DELETE_FLAG)
939 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
940 dwFlags, pvPara, NULL, &provInfo);
941 else
943 HCERTSTORE memStore;
945 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
946 CERT_STORE_CREATE_NEW_FLAG, NULL);
947 if (memStore)
949 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
950 dwFlags, pvPara, memStore, &provInfo))
951 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
952 &provInfo);
953 else
954 CertCloseStore(memStore, 0);
957 CryptFreeOIDFunctionAddress(hFunc, 0);
959 else
960 SetLastError(ERROR_FILE_NOT_FOUND);
961 return ret;
964 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
966 static const WCHAR fmt[] = { '%','0','2','X',0 };
967 DWORD i;
969 assert(hash);
970 assert(asciiHash);
972 for (i = 0; i < 20; i++)
973 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
976 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
977 0 };
978 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
979 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
980 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
982 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO *store, HKEY key,
983 DWORD contextType)
985 LONG rc;
986 DWORD index = 0;
987 WCHAR subKeyName[MAX_PATH];
989 do {
990 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
992 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
993 NULL);
994 if (!rc)
996 HKEY subKey;
998 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
999 if (!rc)
1001 LPBYTE buf = NULL;
1003 size = 0;
1004 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
1005 if (!rc)
1006 buf = CryptMemAlloc(size);
1007 if (buf)
1009 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
1010 &size);
1011 if (!rc)
1013 const void *context;
1014 DWORD addedType;
1016 TRACE("Adding cert with hash %s\n",
1017 debugstr_w(subKeyName));
1018 context = CRYPT_ReadSerializedElement(buf, size,
1019 contextType, &addedType);
1020 if (context)
1022 const WINE_CONTEXT_INTERFACE *contextInterface;
1023 BYTE hash[20];
1025 switch (addedType)
1027 case CERT_STORE_CERTIFICATE_CONTEXT:
1028 contextInterface = &gCertInterface;
1029 break;
1030 case CERT_STORE_CRL_CONTEXT:
1031 contextInterface = &gCRLInterface;
1032 break;
1033 case CERT_STORE_CTL_CONTEXT:
1034 contextInterface = &gCTLInterface;
1035 break;
1036 default:
1037 contextInterface = NULL;
1039 if (contextInterface)
1041 size = sizeof(hash);
1042 if (contextInterface->getProp(context,
1043 CERT_HASH_PROP_ID, hash, &size))
1045 WCHAR asciiHash[20 * 2 + 1];
1047 CRYPT_HashToStr(hash, asciiHash);
1048 TRACE("comparing %s\n",
1049 debugstr_w(asciiHash));
1050 TRACE("with %s\n", debugstr_w(subKeyName));
1051 if (!lstrcmpW(asciiHash, subKeyName))
1053 TRACE("hash matches, adding\n");
1054 contextInterface->addContextToStore(
1055 store->memStore, context,
1056 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1058 else
1059 TRACE("hash doesn't match, ignoring\n");
1061 contextInterface->free(context);
1065 CryptMemFree(buf);
1067 RegCloseKey(subKey);
1069 /* Ignore intermediate errors, continue enumerating */
1070 rc = ERROR_SUCCESS;
1072 } while (!rc);
1075 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO *store)
1077 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1078 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1079 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1080 DWORD i;
1082 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1084 HKEY key;
1085 LONG rc;
1087 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1088 &key, NULL);
1089 if (!rc)
1091 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1092 RegCloseKey(key);
1097 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1098 static BOOL CRYPT_WriteSerializedToReg(HKEY key, const BYTE *hash, const BYTE *buf,
1099 DWORD len)
1101 WCHAR asciiHash[20 * 2 + 1];
1102 LONG rc;
1103 HKEY subKey;
1104 BOOL ret;
1106 CRYPT_HashToStr(hash, asciiHash);
1107 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1108 &subKey, NULL);
1109 if (!rc)
1111 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1112 RegCloseKey(subKey);
1114 if (!rc)
1115 ret = TRUE;
1116 else
1118 SetLastError(rc);
1119 ret = FALSE;
1121 return ret;
1124 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1125 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1127 const void *context = NULL;
1128 BOOL ret;
1130 do {
1131 context = contextInterface->enumContextsInStore(memStore, context);
1132 if (context)
1134 BYTE hash[20];
1135 DWORD hashSize = sizeof(hash);
1137 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1138 &hashSize);
1139 if (ret)
1141 DWORD size = 0;
1142 LPBYTE buf = NULL;
1144 ret = contextInterface->serialize(context, 0, NULL, &size);
1145 if (size)
1146 buf = CryptMemAlloc(size);
1147 if (buf)
1149 ret = contextInterface->serialize(context, 0, buf, &size);
1150 if (ret)
1151 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1153 CryptMemFree(buf);
1156 else
1157 ret = TRUE;
1158 } while (ret && context != NULL);
1159 if (context)
1160 contextInterface->free(context);
1161 return ret;
1164 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1166 static const WCHAR * const subKeys[] = { CertsW, CRLsW, CTLsW };
1167 static const WINE_CONTEXT_INTERFACE * const interfaces[] = { &gCertInterface,
1168 &gCRLInterface, &gCTLInterface };
1169 struct list *listToDelete[] = { &store->certsToDelete, &store->crlsToDelete,
1170 NULL };
1171 BOOL ret = TRUE;
1172 DWORD i;
1174 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1176 HKEY key;
1177 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1178 KEY_ALL_ACCESS, NULL, &key, NULL);
1180 if (!rc)
1182 if (listToDelete[i])
1184 PWINE_HASH_TO_DELETE toDelete, next;
1185 WCHAR asciiHash[20 * 2 + 1];
1187 EnterCriticalSection(&store->cs);
1188 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1189 WINE_HASH_TO_DELETE, entry)
1191 LONG rc;
1193 CRYPT_HashToStr(toDelete->hash, asciiHash);
1194 TRACE("Removing %s\n", debugstr_w(asciiHash));
1195 rc = RegDeleteKeyW(key, asciiHash);
1196 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1198 SetLastError(rc);
1199 ret = FALSE;
1201 list_remove(&toDelete->entry);
1202 CryptMemFree(toDelete);
1204 LeaveCriticalSection(&store->cs);
1206 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1207 store->memStore);
1208 RegCloseKey(key);
1210 else
1212 SetLastError(rc);
1213 ret = FALSE;
1216 return ret;
1219 /* If force is true or the registry store is dirty, writes the contents of the
1220 * store to the registry.
1222 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1224 BOOL ret;
1226 TRACE("(%p, %d)\n", store, force);
1228 if (store->dirty || force)
1229 ret = CRYPT_RegWriteToReg(store);
1230 else
1231 ret = TRUE;
1232 return ret;
1235 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1237 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1239 TRACE("(%p, %08x)\n", store, dwFlags);
1240 if (dwFlags)
1241 FIXME("Unimplemented flags: %08x\n", dwFlags);
1243 CRYPT_RegFlushStore(store, FALSE);
1244 RegCloseKey(store->key);
1245 store->cs.DebugInfo->Spare[0] = 0;
1246 DeleteCriticalSection(&store->cs);
1247 CryptMemFree(store);
1250 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1251 const void *context, DWORD dwFlags)
1253 BOOL ret;
1255 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1257 store->dirty = TRUE;
1258 ret = TRUE;
1260 else
1261 ret = FALSE;
1262 return ret;
1265 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1266 struct list *deleteList, const void *context,
1267 PCWINE_CONTEXT_INTERFACE contextInterface)
1269 BOOL ret;
1271 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1273 SetLastError(ERROR_ACCESS_DENIED);
1274 ret = FALSE;
1276 else
1278 PWINE_HASH_TO_DELETE toDelete =
1279 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1281 if (toDelete)
1283 DWORD size = sizeof(toDelete->hash);
1285 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1286 toDelete->hash, &size);
1287 if (ret)
1289 EnterCriticalSection(&store->cs);
1290 list_add_tail(deleteList, &toDelete->entry);
1291 LeaveCriticalSection(&store->cs);
1293 else
1295 CryptMemFree(toDelete);
1296 ret = FALSE;
1299 else
1300 ret = FALSE;
1301 if (ret)
1302 store->dirty = TRUE;
1304 return ret;
1307 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1308 PCCERT_CONTEXT cert, DWORD dwFlags)
1310 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1312 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1314 return CRYPT_RegWriteContext(store, cert, dwFlags);
1317 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1318 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1320 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1322 TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1324 return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1325 pCertInterface);
1328 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1329 PCCRL_CONTEXT crl, DWORD dwFlags)
1331 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1333 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1335 return CRYPT_RegWriteContext(store, crl, dwFlags);
1338 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1339 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1341 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1343 TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1345 return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1346 pCRLInterface);
1349 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1350 DWORD dwCtrlType, void const *pvCtrlPara)
1352 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1353 BOOL ret;
1355 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1356 pvCtrlPara);
1358 switch (dwCtrlType)
1360 case CERT_STORE_CTRL_RESYNC:
1361 CRYPT_RegFlushStore(store, FALSE);
1362 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1363 CRYPT_RegReadFromReg(store);
1364 ret = TRUE;
1365 break;
1366 case CERT_STORE_CTRL_COMMIT:
1367 ret = CRYPT_RegFlushStore(store,
1368 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1369 break;
1370 default:
1371 FIXME("%d: stub\n", dwCtrlType);
1372 ret = FALSE;
1374 return ret;
1377 static void *regProvFuncs[] = {
1378 CRYPT_RegCloseStore,
1379 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1380 CRYPT_RegWriteCert,
1381 CRYPT_RegDeleteCert,
1382 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1383 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1384 CRYPT_RegWriteCRL,
1385 CRYPT_RegDeleteCRL,
1386 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1387 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1388 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1389 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1390 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1391 CRYPT_RegControl,
1394 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1395 DWORD dwFlags, const void *pvPara)
1397 PWINECRYPT_CERTSTORE store = NULL;
1399 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1401 if (dwFlags & CERT_STORE_DELETE_FLAG)
1403 DWORD rc = RegDeleteTreeW((HKEY)pvPara, CertsW);
1405 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1406 rc = RegDeleteTreeW((HKEY)pvPara, CRLsW);
1407 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1408 rc = RegDeleteTreeW((HKEY)pvPara, CTLsW);
1409 if (rc == ERROR_NO_MORE_ITEMS)
1410 rc = ERROR_SUCCESS;
1411 SetLastError(rc);
1413 else
1415 HKEY key;
1417 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1418 GetCurrentProcess(), (LPHANDLE)&key,
1419 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1420 TRUE, 0))
1422 PWINECRYPT_CERTSTORE memStore;
1424 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1425 if (memStore)
1427 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1428 sizeof(WINE_REGSTOREINFO));
1430 if (regInfo)
1432 CERT_STORE_PROV_INFO provInfo = { 0 };
1434 regInfo->dwOpenFlags = dwFlags;
1435 regInfo->cryptProv = hCryptProv;
1436 regInfo->memStore = memStore;
1437 regInfo->key = key;
1438 InitializeCriticalSection(&regInfo->cs);
1439 regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
1440 list_init(&regInfo->certsToDelete);
1441 list_init(&regInfo->crlsToDelete);
1442 CRYPT_RegReadFromReg(regInfo);
1443 regInfo->dirty = FALSE;
1444 provInfo.cbSize = sizeof(provInfo);
1445 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1446 sizeof(regProvFuncs[0]);
1447 provInfo.rgpvStoreProvFunc = regProvFuncs;
1448 provInfo.hStoreProv = regInfo;
1449 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1450 &provInfo);
1455 TRACE("returning %p\n", store);
1456 return store;
1459 /* FIXME: this isn't complete for the Root store, in which the top-level
1460 * self-signed CA certs reside. Adding a cert to the Root store should present
1461 * the user with a dialog indicating the consequences of doing so, and asking
1462 * the user to confirm whether the cert should be added.
1464 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1465 DWORD dwFlags, const void *pvPara)
1467 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1468 LPCWSTR storeName = (LPCWSTR)pvPara;
1469 LPWSTR storePath;
1470 PWINECRYPT_CERTSTORE store = NULL;
1471 HKEY root;
1472 LPCWSTR base;
1473 BOOL ret;
1475 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1476 debugstr_w((LPCWSTR)pvPara));
1478 if (!pvPara)
1480 SetLastError(E_INVALIDARG);
1481 return NULL;
1484 ret = TRUE;
1485 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1487 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1488 root = HKEY_LOCAL_MACHINE;
1489 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1490 break;
1491 case CERT_SYSTEM_STORE_CURRENT_USER:
1492 root = HKEY_CURRENT_USER;
1493 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1494 break;
1495 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1496 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1497 * SystemCertificates
1499 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1500 debugstr_w(storeName));
1501 return NULL;
1502 case CERT_SYSTEM_STORE_SERVICES:
1503 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1504 * SystemCertificates
1506 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1507 debugstr_w(storeName));
1508 return NULL;
1509 case CERT_SYSTEM_STORE_USERS:
1510 /* hku\user sid\Software\Microsoft\SystemCertificates */
1511 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1512 debugstr_w(storeName));
1513 return NULL;
1514 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1515 root = HKEY_CURRENT_USER;
1516 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1517 break;
1518 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1519 root = HKEY_LOCAL_MACHINE;
1520 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1521 break;
1522 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1523 /* hklm\Software\Microsoft\EnterpriseCertificates */
1524 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1525 debugstr_w(storeName));
1526 return NULL;
1527 default:
1528 SetLastError(E_INVALIDARG);
1529 return NULL;
1532 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1533 sizeof(WCHAR));
1534 if (storePath)
1536 LONG rc;
1537 HKEY key;
1538 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1539 KEY_ALL_ACCESS;
1541 wsprintfW(storePath, fmt, base, storeName);
1542 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1543 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1544 else
1546 DWORD disp;
1548 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1549 &key, &disp);
1550 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1551 disp == REG_OPENED_EXISTING_KEY)
1553 RegCloseKey(key);
1554 rc = ERROR_FILE_EXISTS;
1557 if (!rc)
1559 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1560 RegCloseKey(key);
1562 else
1563 SetLastError(rc);
1564 CryptMemFree(storePath);
1566 return store;
1569 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1570 DWORD dwFlags, const void *pvPara)
1572 int len;
1573 PWINECRYPT_CERTSTORE ret = NULL;
1575 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1576 debugstr_a((LPCSTR)pvPara));
1578 if (!pvPara)
1580 SetLastError(ERROR_FILE_NOT_FOUND);
1581 return NULL;
1583 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1584 if (len)
1586 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1588 if (storeName)
1590 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1591 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1592 CryptMemFree(storeName);
1595 return ret;
1598 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1599 DWORD dwFlags, const void *pvPara)
1601 HCERTSTORE store = 0;
1602 BOOL ret;
1604 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1605 debugstr_w((LPCWSTR)pvPara));
1607 if (!pvPara)
1609 SetLastError(ERROR_FILE_NOT_FOUND);
1610 return NULL;
1612 /* This returns a different error than system registry stores if the
1613 * location is invalid.
1615 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1617 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1618 case CERT_SYSTEM_STORE_CURRENT_USER:
1619 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1620 case CERT_SYSTEM_STORE_SERVICES:
1621 case CERT_SYSTEM_STORE_USERS:
1622 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1623 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1624 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1625 ret = TRUE;
1626 break;
1627 default:
1628 SetLastError(ERROR_FILE_NOT_FOUND);
1629 ret = FALSE;
1631 if (ret)
1633 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1634 0, hCryptProv, dwFlags, pvPara);
1636 if (regStore)
1638 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1639 CERT_STORE_CREATE_NEW_FLAG, NULL);
1640 CertAddStoreToCollection(store, regStore,
1641 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1642 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1643 CertCloseStore(regStore, 0);
1644 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1645 * stores.
1647 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1648 CERT_SYSTEM_STORE_CURRENT_USER)
1650 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1651 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1652 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1653 hCryptProv, dwFlags, pvPara);
1654 if (regStore)
1656 CertAddStoreToCollection(store, regStore,
1657 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1658 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1659 CertCloseStore(regStore, 0);
1664 return (PWINECRYPT_CERTSTORE)store;
1667 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1668 DWORD dwFlags, const void *pvPara)
1670 int len;
1671 PWINECRYPT_CERTSTORE ret = NULL;
1673 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1674 debugstr_a((LPCSTR)pvPara));
1676 if (!pvPara)
1678 SetLastError(ERROR_FILE_NOT_FOUND);
1679 return NULL;
1681 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1682 if (len)
1684 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1686 if (storeName)
1688 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1689 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1690 CryptMemFree(storeName);
1693 return ret;
1696 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1698 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1700 TRACE("(%p, %08x)\n", store, dwFlags);
1701 if (store->dirty)
1702 CRYPT_WriteSerializedFile(store->file, store->memStore);
1703 CertCloseStore(store->memStore, dwFlags);
1704 CloseHandle(store->file);
1705 CryptMemFree(store);
1708 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1709 PCCERT_CONTEXT cert, DWORD dwFlags)
1711 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1713 TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1714 store->dirty = TRUE;
1715 return TRUE;
1718 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1719 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1721 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1723 TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1724 store->dirty = TRUE;
1725 return TRUE;
1728 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1729 PCCRL_CONTEXT crl, DWORD dwFlags)
1731 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1733 TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1734 store->dirty = TRUE;
1735 return TRUE;
1738 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1739 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1741 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1743 TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1744 store->dirty = TRUE;
1745 return TRUE;
1748 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1749 DWORD dwCtrlType, void const *pvCtrlPara)
1751 PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1752 BOOL ret;
1754 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1755 pvCtrlPara);
1757 switch (dwCtrlType)
1759 case CERT_STORE_CTRL_RESYNC:
1760 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1761 CRYPT_ReadSerializedFile(store->file, store);
1762 ret = TRUE;
1763 break;
1764 case CERT_STORE_CTRL_COMMIT:
1765 if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1767 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1768 ret = FALSE;
1770 else if (store->dirty)
1771 ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1772 else
1773 ret = TRUE;
1774 break;
1775 default:
1776 FIXME("%d: stub\n", dwCtrlType);
1777 ret = FALSE;
1779 return ret;
1782 static void *fileProvFuncs[] = {
1783 CRYPT_FileCloseStore,
1784 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1785 CRYPT_FileWriteCert,
1786 CRYPT_FileDeleteCert,
1787 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1788 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1789 CRYPT_FileWriteCRL,
1790 CRYPT_FileDeleteCRL,
1791 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1792 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1793 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1794 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1795 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1796 CRYPT_FileControl,
1799 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1800 DWORD dwFlags, const void *pvPara)
1802 PWINECRYPT_CERTSTORE store = NULL;
1803 HANDLE file = (HANDLE)pvPara;
1805 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1807 if (!pvPara)
1809 SetLastError(ERROR_INVALID_HANDLE);
1810 return NULL;
1812 if (dwFlags & CERT_STORE_DELETE_FLAG)
1814 SetLastError(E_INVALIDARG);
1815 return NULL;
1817 if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1818 (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1820 SetLastError(E_INVALIDARG);
1821 return NULL;
1824 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1825 GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1826 GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1828 PWINECRYPT_CERTSTORE memStore;
1830 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1831 if (memStore)
1833 if (CRYPT_ReadSerializedFile(file, memStore))
1835 PWINE_FILESTOREINFO info = CryptMemAlloc(
1836 sizeof(WINE_FILESTOREINFO));
1838 if (info)
1840 CERT_STORE_PROV_INFO provInfo = { 0 };
1842 info->dwOpenFlags = dwFlags;
1843 info->cryptProv = hCryptProv;
1844 info->memStore = memStore;
1845 info->file = file;
1846 info->dirty = FALSE;
1847 provInfo.cbSize = sizeof(provInfo);
1848 provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1849 sizeof(fileProvFuncs[0]);
1850 provInfo.rgpvStoreProvFunc = fileProvFuncs;
1851 provInfo.hStoreProv = info;
1852 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1853 &provInfo);
1858 TRACE("returning %p\n", store);
1859 return store;
1862 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1863 DWORD dwFlags, const void *pvPara)
1865 HCERTSTORE store = 0;
1866 LPCWSTR fileName = (LPCWSTR)pvPara;
1867 DWORD access, create;
1868 HANDLE file;
1870 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1872 if (!fileName)
1874 SetLastError(ERROR_PATH_NOT_FOUND);
1875 return NULL;
1878 access = GENERIC_READ;
1879 if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1880 access |= GENERIC_WRITE;
1881 if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1882 create = CREATE_NEW;
1883 else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1884 create = OPEN_EXISTING;
1885 else
1886 create = OPEN_ALWAYS;
1887 file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1888 FILE_ATTRIBUTE_NORMAL, NULL);
1889 if (file != INVALID_HANDLE_VALUE)
1891 /* FIXME: need to check whether it's a serialized store; if not, fall
1892 * back to a PKCS#7 signed message, then to a single serialized cert.
1894 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1895 file);
1896 CloseHandle(file);
1898 return (PWINECRYPT_CERTSTORE)store;
1901 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1902 DWORD dwFlags, const void *pvPara)
1904 int len;
1905 PWINECRYPT_CERTSTORE ret = NULL;
1907 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1908 debugstr_a((LPCSTR)pvPara));
1910 if (!pvPara)
1912 SetLastError(ERROR_FILE_NOT_FOUND);
1913 return NULL;
1915 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1916 if (len)
1918 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1920 if (storeName)
1922 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1923 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1924 CryptMemFree(storeName);
1927 return ret;
1930 static void WINAPI CRYPT_MsgCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1932 PWINE_MSGSTOREINFO store = (PWINE_MSGSTOREINFO)hCertStore;
1934 TRACE("(%p, %08x)\n", store, dwFlags);
1935 CertCloseStore(store->memStore, dwFlags);
1936 CryptMsgClose(store->msg);
1937 CryptMemFree(store);
1940 static void *msgProvFuncs[] = {
1941 CRYPT_MsgCloseStore,
1942 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1943 NULL, /* CERT_STORE_PROV_WRITE_CERT_FUNC */
1944 NULL, /* CERT_STORE_PROV_DELETE_CERT_FUNC */
1945 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1946 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1947 NULL, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1948 NULL, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1949 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1950 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1951 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1952 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1953 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1954 NULL, /* CERT_STORE_PROV_CONTROL_FUNC */
1957 static PWINECRYPT_CERTSTORE CRYPT_MsgOpenStore(HCRYPTPROV hCryptProv,
1958 DWORD dwFlags, const void *pvPara)
1960 PWINECRYPT_CERTSTORE store = NULL;
1961 HCRYPTMSG msg = (HCRYPTMSG)pvPara;
1962 PWINECRYPT_CERTSTORE memStore;
1964 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1966 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1967 if (memStore)
1969 BOOL ret;
1970 DWORD size, count, i;
1972 size = sizeof(count);
1973 ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
1974 for (i = 0; ret && i < count; i++)
1976 size = 0;
1977 ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, NULL, &size);
1978 if (ret)
1980 LPBYTE buf = CryptMemAlloc(size);
1982 if (buf)
1984 ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, buf, &size);
1985 if (ret)
1986 ret = CertAddEncodedCertificateToStore(memStore,
1987 X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
1988 NULL);
1989 CryptMemFree(buf);
1993 size = sizeof(count);
1994 ret = CryptMsgGetParam(msg, CMSG_CRL_COUNT_PARAM, 0, &count, &size);
1995 for (i = 0; ret && i < count; i++)
1997 size = 0;
1998 ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, NULL, &size);
1999 if (ret)
2001 LPBYTE buf = CryptMemAlloc(size);
2003 if (buf)
2005 ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, buf, &size);
2006 if (ret)
2007 ret = CertAddEncodedCRLToStore(memStore,
2008 X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
2009 NULL);
2010 CryptMemFree(buf);
2014 if (ret)
2016 PWINE_MSGSTOREINFO info = CryptMemAlloc(sizeof(WINE_MSGSTOREINFO));
2018 if (info)
2020 CERT_STORE_PROV_INFO provInfo = { 0 };
2022 info->dwOpenFlags = dwFlags;
2023 info->cryptProv = hCryptProv;
2024 info->memStore = memStore;
2025 info->msg = CryptMsgDuplicate(msg);
2026 provInfo.cbSize = sizeof(provInfo);
2027 provInfo.cStoreProvFunc = sizeof(msgProvFuncs) /
2028 sizeof(msgProvFuncs[0]);
2029 provInfo.rgpvStoreProvFunc = msgProvFuncs;
2030 provInfo.hStoreProv = info;
2031 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
2032 &provInfo);
2034 else
2035 CertCloseStore(memStore, 0);
2037 else
2038 CertCloseStore(memStore, 0);
2040 TRACE("returning %p\n", store);
2041 return store;
2044 static PWINECRYPT_CERTSTORE CRYPT_PKCSOpenStore(HCRYPTPROV hCryptProv,
2045 DWORD dwFlags, const void *pvPara)
2047 HCRYPTMSG msg;
2048 PWINECRYPT_CERTSTORE store = NULL;
2049 const CRYPT_DATA_BLOB *data = (const CRYPT_DATA_BLOB *)pvPara;
2050 BOOL ret;
2052 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
2054 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, CMSG_SIGNED, 0, NULL,
2055 NULL);
2056 ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
2057 if (!ret)
2059 CryptMsgClose(msg);
2060 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, 0, 0, NULL, NULL);
2061 ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
2062 if (ret)
2064 DWORD type, size = sizeof(type);
2066 /* Only signed messages are allowed, check type */
2067 ret = CryptMsgGetParam(msg, CMSG_TYPE_PARAM, 0, &type, &size);
2068 if (ret && type != CMSG_SIGNED)
2070 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
2071 ret = FALSE;
2075 if (ret)
2076 store = CRYPT_MsgOpenStore(hCryptProv, dwFlags, msg);
2077 CryptMsgClose(msg);
2078 TRACE("returning %p\n", store);
2079 return store;
2082 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
2083 DWORD dwFlags, const void *pvPara)
2085 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
2086 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
2087 else
2088 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
2089 debugstr_w((LPCWSTR)pvPara));
2090 return NULL;
2093 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
2094 DWORD dwMsgAndCertEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags,
2095 const void* pvPara)
2097 WINECRYPT_CERTSTORE *hcs;
2098 StoreOpenFunc openFunc = NULL;
2100 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
2101 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
2103 if (!HIWORD(lpszStoreProvider))
2105 switch (LOWORD(lpszStoreProvider))
2107 case (int)CERT_STORE_PROV_MSG:
2108 openFunc = CRYPT_MsgOpenStore;
2109 break;
2110 case (int)CERT_STORE_PROV_MEMORY:
2111 openFunc = CRYPT_MemOpenStore;
2112 break;
2113 case (int)CERT_STORE_PROV_FILE:
2114 openFunc = CRYPT_FileOpenStore;
2115 break;
2116 case (int)CERT_STORE_PROV_PKCS7:
2117 openFunc = CRYPT_PKCSOpenStore;
2118 break;
2119 case (int)CERT_STORE_PROV_REG:
2120 openFunc = CRYPT_RegOpenStore;
2121 break;
2122 case (int)CERT_STORE_PROV_FILENAME_A:
2123 openFunc = CRYPT_FileNameOpenStoreA;
2124 break;
2125 case (int)CERT_STORE_PROV_FILENAME_W:
2126 openFunc = CRYPT_FileNameOpenStoreW;
2127 break;
2128 case (int)CERT_STORE_PROV_COLLECTION:
2129 openFunc = CRYPT_CollectionOpenStore;
2130 break;
2131 case (int)CERT_STORE_PROV_SYSTEM_A:
2132 openFunc = CRYPT_SysOpenStoreA;
2133 break;
2134 case (int)CERT_STORE_PROV_SYSTEM_W:
2135 openFunc = CRYPT_SysOpenStoreW;
2136 break;
2137 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
2138 openFunc = CRYPT_SysRegOpenStoreA;
2139 break;
2140 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
2141 openFunc = CRYPT_SysRegOpenStoreW;
2142 break;
2143 case (int)CERT_STORE_PROV_PHYSICAL_W:
2144 openFunc = CRYPT_PhysOpenStoreW;
2145 break;
2146 default:
2147 if (LOWORD(lpszStoreProvider))
2148 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
2151 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
2152 openFunc = CRYPT_MemOpenStore;
2153 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
2154 openFunc = CRYPT_FileOpenStore;
2155 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
2156 openFunc = CRYPT_SysOpenStoreW;
2157 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
2158 openFunc = CRYPT_CollectionOpenStore;
2159 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
2160 openFunc = CRYPT_SysRegOpenStoreW;
2161 else
2163 FIXME("unimplemented type %s\n", lpszStoreProvider);
2164 openFunc = NULL;
2167 if (!openFunc)
2168 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2169 hCryptProv, dwFlags, pvPara);
2170 else
2171 hcs = openFunc(hCryptProv, dwFlags, pvPara);
2172 return (HCERTSTORE)hcs;
2175 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV_LEGACY hProv,
2176 LPCSTR szSubSystemProtocol)
2178 if (!szSubSystemProtocol)
2180 SetLastError(E_INVALIDARG);
2181 return 0;
2183 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2184 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2187 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV_LEGACY hProv,
2188 LPCWSTR szSubSystemProtocol)
2190 if (!szSubSystemProtocol)
2192 SetLastError(E_INVALIDARG);
2193 return 0;
2195 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2196 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2199 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2200 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2202 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore,
2203 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2204 return TRUE;
2207 #define CertContext_CopyProperties(to, from) \
2208 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2210 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2211 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2212 PCCERT_CONTEXT *ppStoreContext)
2214 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2215 BOOL ret = TRUE;
2216 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2218 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2219 dwAddDisposition, ppStoreContext);
2221 /* Weird case to pass a test */
2222 if (dwAddDisposition == 0)
2224 SetLastError(STATUS_ACCESS_VIOLATION);
2225 return FALSE;
2227 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2229 BYTE hashToAdd[20];
2230 DWORD size = sizeof(hashToAdd);
2232 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2233 hashToAdd, &size);
2234 if (ret)
2236 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2238 existing = CertFindCertificateInStore(hCertStore,
2239 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2240 NULL);
2244 switch (dwAddDisposition)
2246 case CERT_STORE_ADD_ALWAYS:
2247 toAdd = CertDuplicateCertificateContext(pCertContext);
2248 break;
2249 case CERT_STORE_ADD_NEW:
2250 if (existing)
2252 TRACE("found matching certificate, not adding\n");
2253 SetLastError(CRYPT_E_EXISTS);
2254 ret = FALSE;
2256 else
2257 toAdd = CertDuplicateCertificateContext(pCertContext);
2258 break;
2259 case CERT_STORE_ADD_REPLACE_EXISTING:
2260 toAdd = CertDuplicateCertificateContext(pCertContext);
2261 break;
2262 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2263 toAdd = CertDuplicateCertificateContext(pCertContext);
2264 if (existing)
2265 CertContext_CopyProperties(toAdd, existing);
2266 break;
2267 case CERT_STORE_ADD_USE_EXISTING:
2268 if (existing)
2270 CertContext_CopyProperties(existing, pCertContext);
2271 *ppStoreContext = CertDuplicateCertificateContext(existing);
2273 else
2274 toAdd = CertDuplicateCertificateContext(pCertContext);
2275 break;
2276 default:
2277 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2278 ret = FALSE;
2281 if (toAdd)
2283 if (store)
2284 ret = store->certs.addContext(store, (void *)toAdd,
2285 (void *)existing, (const void **)ppStoreContext);
2286 else if (ppStoreContext)
2287 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2288 CertFreeCertificateContext(toAdd);
2290 CertFreeCertificateContext(existing);
2292 TRACE("returning %d\n", ret);
2293 return ret;
2296 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2297 PCCERT_CONTEXT pPrev)
2299 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2300 PCCERT_CONTEXT ret;
2302 TRACE("(%p, %p)\n", hCertStore, pPrev);
2303 if (!hCertStore)
2304 ret = NULL;
2305 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2306 ret = NULL;
2307 else
2308 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2309 return ret;
2312 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2314 BOOL ret;
2316 TRACE("(%p)\n", pCertContext);
2318 if (!pCertContext)
2319 ret = TRUE;
2320 else if (!pCertContext->hCertStore)
2322 ret = TRUE;
2323 CertFreeCertificateContext(pCertContext);
2325 else
2327 PWINECRYPT_CERTSTORE hcs =
2328 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2330 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2331 ret = FALSE;
2332 else
2333 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2334 CertFreeCertificateContext(pCertContext);
2336 return ret;
2339 #define CrlContext_CopyProperties(to, from) \
2340 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2342 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2343 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2344 PCCRL_CONTEXT* ppStoreContext)
2346 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2347 BOOL ret = TRUE;
2348 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2350 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2351 dwAddDisposition, ppStoreContext);
2353 /* Weird case to pass a test */
2354 if (dwAddDisposition == 0)
2356 SetLastError(STATUS_ACCESS_VIOLATION);
2357 return FALSE;
2359 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2361 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2362 pCrlContext, NULL);
2365 switch (dwAddDisposition)
2367 case CERT_STORE_ADD_ALWAYS:
2368 toAdd = CertDuplicateCRLContext(pCrlContext);
2369 break;
2370 case CERT_STORE_ADD_NEW:
2371 if (existing)
2373 TRACE("found matching CRL, not adding\n");
2374 SetLastError(CRYPT_E_EXISTS);
2375 ret = FALSE;
2377 else
2378 toAdd = CertDuplicateCRLContext(pCrlContext);
2379 break;
2380 case CERT_STORE_ADD_NEWER:
2381 if (existing)
2383 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2384 &pCrlContext->pCrlInfo->ThisUpdate);
2386 if (newer < 0)
2387 toAdd = CertDuplicateCRLContext(pCrlContext);
2388 else
2390 TRACE("existing CRL is newer, not adding\n");
2391 SetLastError(CRYPT_E_EXISTS);
2392 ret = FALSE;
2395 else
2396 toAdd = CertDuplicateCRLContext(pCrlContext);
2397 break;
2398 case CERT_STORE_ADD_REPLACE_EXISTING:
2399 toAdd = CertDuplicateCRLContext(pCrlContext);
2400 break;
2401 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2402 toAdd = CertDuplicateCRLContext(pCrlContext);
2403 if (existing)
2404 CrlContext_CopyProperties(toAdd, existing);
2405 break;
2406 case CERT_STORE_ADD_USE_EXISTING:
2407 if (existing)
2408 CrlContext_CopyProperties(existing, pCrlContext);
2409 break;
2410 default:
2411 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2412 ret = FALSE;
2415 if (toAdd)
2417 if (store)
2418 ret = store->crls.addContext(store, (void *)toAdd,
2419 (void *)existing, (const void **)ppStoreContext);
2420 else if (ppStoreContext)
2421 *ppStoreContext = CertDuplicateCRLContext(toAdd);
2422 CertFreeCRLContext(toAdd);
2424 CertFreeCRLContext(existing);
2426 TRACE("returning %d\n", ret);
2427 return ret;
2430 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2432 BOOL ret;
2434 TRACE("(%p)\n", pCrlContext);
2436 if (!pCrlContext)
2437 ret = TRUE;
2438 else if (!pCrlContext->hCertStore)
2440 ret = TRUE;
2441 CertFreeCRLContext(pCrlContext);
2443 else
2445 PWINECRYPT_CERTSTORE hcs =
2446 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2448 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2449 ret = FALSE;
2450 else
2451 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2452 CertFreeCRLContext(pCrlContext);
2454 return ret;
2457 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2458 PCCRL_CONTEXT pPrev)
2460 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2461 PCCRL_CONTEXT ret;
2463 TRACE("(%p, %p)\n", hCertStore, pPrev);
2464 if (!hCertStore)
2465 ret = NULL;
2466 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2467 ret = NULL;
2468 else
2469 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2470 return ret;
2473 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2474 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2476 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2477 cbCtlEncoded);
2478 return NULL;
2481 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2482 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2483 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2485 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2486 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2487 ppCtlContext);
2488 return FALSE;
2491 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2492 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2493 PCCTL_CONTEXT* ppStoreContext)
2495 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2496 dwAddDisposition, ppStoreContext);
2497 return TRUE;
2500 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2502 FIXME("(%p): stub\n", pCtlContext );
2503 return pCtlContext;
2506 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2508 FIXME("(%p): stub\n", pCtlContext );
2509 return TRUE;
2512 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2514 FIXME("(%p): stub\n", pCtlContext);
2515 return TRUE;
2518 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2519 PCCTL_CONTEXT pPrev)
2521 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2522 return NULL;
2525 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2527 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2529 TRACE("(%p)\n", hCertStore);
2531 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2532 InterlockedIncrement(&hcs->ref);
2533 return hCertStore;
2536 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2538 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2540 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2542 if( ! hCertStore )
2543 return TRUE;
2545 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2546 return FALSE;
2548 if (InterlockedDecrement(&hcs->ref) == 0)
2550 TRACE("%p's ref count is 0, freeing\n", hcs);
2551 hcs->dwMagic = 0;
2552 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2553 CryptReleaseContext(hcs->cryptProv, 0);
2554 hcs->closeStore(hcs, dwFlags);
2556 else
2557 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2558 return TRUE;
2561 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2562 DWORD dwCtrlType, void const *pvCtrlPara)
2564 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2565 BOOL ret;
2567 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2568 pvCtrlPara);
2570 if (!hcs)
2571 ret = FALSE;
2572 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2573 ret = FALSE;
2574 else
2576 if (hcs->control)
2577 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2578 else
2579 ret = TRUE;
2581 return ret;
2584 BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2585 void *pvData, DWORD *pcbData)
2587 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2588 BOOL ret = FALSE;
2590 TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);
2592 switch (dwPropId)
2594 case CERT_ACCESS_STATE_PROP_ID:
2595 if (!pvData)
2597 *pcbData = sizeof(DWORD);
2598 ret = TRUE;
2600 else if (*pcbData < sizeof(DWORD))
2602 SetLastError(ERROR_MORE_DATA);
2603 *pcbData = sizeof(DWORD);
2605 else
2607 DWORD state = 0;
2609 if (store->type != StoreTypeMem &&
2610 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2611 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2612 *(DWORD *)pvData = state;
2613 ret = TRUE;
2615 break;
2616 default:
2617 if (store->properties)
2619 CRYPT_DATA_BLOB blob;
2621 ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
2622 &blob);
2623 if (ret)
2625 if (!pvData)
2626 *pcbData = blob.cbData;
2627 else if (*pcbData < blob.cbData)
2629 SetLastError(ERROR_MORE_DATA);
2630 *pcbData = blob.cbData;
2631 ret = FALSE;
2633 else
2635 memcpy(pvData, blob.pbData, blob.cbData);
2636 *pcbData = blob.cbData;
2639 else
2640 SetLastError(CRYPT_E_NOT_FOUND);
2642 else
2643 SetLastError(CRYPT_E_NOT_FOUND);
2645 return ret;
2648 BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2649 DWORD dwFlags, const void *pvData)
2651 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2652 BOOL ret = FALSE;
2654 TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);
2656 if (!store->properties)
2657 store->properties = ContextPropertyList_Create();
2658 switch (dwPropId)
2660 case CERT_ACCESS_STATE_PROP_ID:
2661 SetLastError(E_INVALIDARG);
2662 break;
2663 default:
2664 if (pvData)
2666 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvData;
2668 ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
2669 blob->pbData, blob->cbData);
2671 else
2673 ContextPropertyList_RemoveProperty(store->properties, dwPropId);
2674 ret = TRUE;
2677 return ret;
2680 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2681 DWORD dwPropId)
2683 FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2684 return 0;
2687 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2688 DWORD dwPropId, void *pvData, DWORD *pcbData)
2690 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2691 return FALSE;
2694 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2695 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2697 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2698 pvData);
2699 return FALSE;
2702 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2703 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2705 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2706 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2707 PWINE_STORE_LIST_ENTRY entry;
2708 BOOL ret;
2710 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2711 dwUpdateFlags, dwPriority);
2713 if (!collection || !sibling)
2714 return TRUE;
2715 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2717 SetLastError(E_INVALIDARG);
2718 return FALSE;
2720 if (collection->hdr.type != StoreTypeCollection)
2722 SetLastError(E_INVALIDARG);
2723 return FALSE;
2725 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2727 SetLastError(E_INVALIDARG);
2728 return FALSE;
2731 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2732 if (entry)
2734 InterlockedIncrement(&sibling->ref);
2735 TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2736 entry->store = sibling;
2737 entry->dwUpdateFlags = dwUpdateFlags;
2738 entry->dwPriority = dwPriority;
2739 list_init(&entry->entry);
2740 TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2741 EnterCriticalSection(&collection->cs);
2742 if (dwPriority)
2744 PWINE_STORE_LIST_ENTRY cursor;
2745 BOOL added = FALSE;
2747 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2748 WINE_STORE_LIST_ENTRY, entry)
2750 if (cursor->dwPriority < dwPriority)
2752 list_add_before(&cursor->entry, &entry->entry);
2753 added = TRUE;
2754 break;
2757 if (!added)
2758 list_add_tail(&collection->stores, &entry->entry);
2760 else
2761 list_add_tail(&collection->stores, &entry->entry);
2762 LeaveCriticalSection(&collection->cs);
2763 ret = TRUE;
2765 else
2766 ret = FALSE;
2767 return ret;
2770 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2771 HCERTSTORE hSiblingStore)
2773 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2774 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2775 PWINE_STORE_LIST_ENTRY store, next;
2777 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2779 if (!collection || !sibling)
2780 return;
2781 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2783 SetLastError(E_INVALIDARG);
2784 return;
2786 if (collection->hdr.type != StoreTypeCollection)
2787 return;
2788 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2790 SetLastError(E_INVALIDARG);
2791 return;
2793 EnterCriticalSection(&collection->cs);
2794 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2795 WINE_STORE_LIST_ENTRY, entry)
2797 if (store->store == sibling)
2799 list_remove(&store->entry);
2800 CertCloseStore(store->store, 0);
2801 CryptMemFree(store);
2802 break;
2805 LeaveCriticalSection(&collection->cs);
2808 static LONG CRYPT_OpenParentStore(DWORD dwFlags,
2809 void *pvSystemStoreLocationPara, HKEY *key)
2811 HKEY root;
2812 LPCWSTR base;
2814 TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);
2816 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
2818 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
2819 root = HKEY_LOCAL_MACHINE;
2820 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2821 break;
2822 case CERT_SYSTEM_STORE_CURRENT_USER:
2823 root = HKEY_CURRENT_USER;
2824 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2825 break;
2826 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
2827 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2828 * SystemCertificates
2830 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
2831 return ERROR_FILE_NOT_FOUND;
2832 case CERT_SYSTEM_STORE_SERVICES:
2833 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2834 * SystemCertificates
2836 FIXME("CERT_SYSTEM_STORE_SERVICES\n");
2837 return ERROR_FILE_NOT_FOUND;
2838 case CERT_SYSTEM_STORE_USERS:
2839 /* hku\user sid\Software\Microsoft\SystemCertificates */
2840 FIXME("CERT_SYSTEM_STORE_USERS\n");
2841 return ERROR_FILE_NOT_FOUND;
2842 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
2843 root = HKEY_CURRENT_USER;
2844 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2845 break;
2846 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
2847 root = HKEY_LOCAL_MACHINE;
2848 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2849 break;
2850 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
2851 /* hklm\Software\Microsoft\EnterpriseCertificates */
2852 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
2853 return ERROR_FILE_NOT_FOUND;
2854 default:
2855 return ERROR_FILE_NOT_FOUND;
2858 return RegOpenKeyExW(root, base, 0, KEY_READ, key);
2861 BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
2862 void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
2864 BOOL ret = FALSE;
2865 LONG rc;
2866 HKEY key;
2868 TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
2869 pfnEnum);
2871 rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
2872 if (!rc)
2874 DWORD index = 0;
2875 CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
2877 ret = TRUE;
2878 do {
2879 WCHAR name[MAX_PATH];
2880 DWORD size = sizeof(name) / sizeof(name[0]);
2882 rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
2883 NULL);
2884 if (!rc)
2885 ret = pfnEnum(name, 0, &info, NULL, pvArg);
2886 } while (ret && !rc);
2887 if (ret && rc != ERROR_NO_MORE_ITEMS)
2888 SetLastError(rc);
2890 else
2891 SetLastError(rc);
2892 return ret;