d3drm: Implement D3DRMCreateColorRGB.
[wine] / dlls / crypt32 / store.c
1 /*
2  * Copyright 2002 Mike McCormack for CodeWeavers
3  * Copyright 2004-2006 Juan Lang
4  *
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.
9  *
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.
14  *
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
18  *
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.
24  */
25
26 #include "config.h"
27 #include "wine/port.h"
28
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"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
43
44 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
45
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,
58 };
59 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
60
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,
73 };
74 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
75
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,
88 };
89 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
90
91 struct WINE_CRYPTCERTSTORE;
92
93 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
94  DWORD dwFlags, const void *pvPara);
95
96 /* Called to enumerate the next context in a store. */
97 typedef void * (*EnumFunc)(struct WINE_CRYPTCERTSTORE *store, void *pPrev);
98
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.
104  */
105 typedef BOOL (*AddFunc)(struct WINE_CRYPTCERTSTORE *store, void *context,
106  void *toReplace, const void **ppStoreContext);
107
108 typedef BOOL (*DeleteFunc)(struct WINE_CRYPTCERTSTORE *store, void *context);
109
110 typedef struct _CONTEXT_STORE
111 {
112     AddFunc    addContext;
113     EnumFunc   enumContext;
114     DeleteFunc deleteContext;
115 } CONTEXT_STORE, *PCONTEXT_STORE;
116
117 typedef enum _CertStoreType {
118     StoreTypeMem,
119     StoreTypeCollection,
120     StoreTypeProvider,
121 } CertStoreType;
122
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
129  */
130 typedef struct WINE_CRYPTCERTSTORE
131 {
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;
143
144 typedef struct _WINE_MEMSTORE
145 {
146     WINECRYPT_CERTSTORE hdr;
147     struct ContextList *certs;
148     struct ContextList *crls;
149 } WINE_MEMSTORE, *PWINE_MEMSTORE;
150
151 typedef struct _WINE_HASH_TO_DELETE
152 {
153     BYTE        hash[20];
154     struct list entry;
155 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
156
157 typedef struct _WINE_REGSTOREINFO
158 {
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;
168
169 typedef struct _WINE_FILESTOREINFO
170 {
171     DWORD                dwOpenFlags;
172     HCRYPTPROV           cryptProv;
173     PWINECRYPT_CERTSTORE memStore;
174     HANDLE               file;
175     BOOL                 dirty;
176 } WINE_FILESTOREINFO, *PWINE_FILESTOREINFO;
177
178 typedef struct _WINE_STORE_LIST_ENTRY
179 {
180     PWINECRYPT_CERTSTORE store;
181     DWORD                dwUpdateFlags;
182     DWORD                dwPriority;
183     struct list          entry;
184 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
185
186 typedef struct _WINE_COLLECTIONSTORE
187 {
188     WINECRYPT_CERTSTORE hdr;
189     CRITICAL_SECTION    cs;
190     struct list         stores;
191 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
192
193 typedef struct _WINE_PROVIDERSTORE
194 {
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;
206
207 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
208  DWORD dwFlags, CertStoreType type)
209 {
210     store->ref = 1;
211     store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
212     store->type = type;
213     if (!hCryptProv)
214     {
215         hCryptProv = CRYPT_GetDefaultProvider();
216         dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
217     }
218     store->cryptProv = hCryptProv;
219     store->dwOpenFlags = dwFlags;
220     store->properties = NULL;
221 }
222
223 static void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store)
224 {
225     if (store->properties)
226         ContextPropertyList_Free(store->properties);
227     CryptMemFree(store);
228 }
229
230 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
231  void *toReplace, const void **ppStoreContext)
232 {
233     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
234     PCERT_CONTEXT context;
235
236     TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
237
238     context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
239     if (context)
240     {
241         context->hCertStore = store;
242         if (ppStoreContext)
243             *ppStoreContext = CertDuplicateCertificateContext(context);
244     }
245     return context ? TRUE : FALSE;
246 }
247
248 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
249 {
250     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
251     void *ret;
252
253     TRACE("(%p, %p)\n", store, pPrev);
254
255     ret = ContextList_Enum(ms->certs, pPrev);
256     if (!ret)
257         SetLastError(CRYPT_E_NOT_FOUND);
258
259     TRACE("returning %p\n", ret);
260     return ret;
261 }
262
263 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
264 {
265     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
266
267     ContextList_Delete(ms->certs, pCertContext);
268     return TRUE;
269 }
270
271 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
272  void *toReplace, const void **ppStoreContext)
273 {
274     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
275     PCRL_CONTEXT context;
276
277     TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
278
279     context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
280     if (context)
281     {
282         context->hCertStore = store;
283         if (ppStoreContext)
284             *ppStoreContext = CertDuplicateCRLContext(context);
285     }
286     return context ? TRUE : FALSE;
287 }
288
289 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
290 {
291     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
292     void *ret;
293
294     TRACE("(%p, %p)\n", store, pPrev);
295
296     ret = ContextList_Enum(ms->crls, pPrev);
297     if (!ret)
298         SetLastError(CRYPT_E_NOT_FOUND);
299
300     TRACE("returning %p\n", ret);
301     return ret;
302 }
303
304 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
305 {
306     WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
307
308     ContextList_Delete(ms->crls, pCrlContext);
309     return TRUE;
310 }
311
312 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
313 {
314     ContextList_Empty(store->certs);
315     ContextList_Empty(store->crls);
316 }
317
318 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
319 {
320     WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
321
322     TRACE("(%p, %08x)\n", store, dwFlags);
323     if (dwFlags)
324         FIXME("Unimplemented flags: %08x\n", dwFlags);
325
326     ContextList_Free(store->certs);
327     ContextList_Free(store->crls);
328     CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
329 }
330
331 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
332  DWORD dwFlags, const void *pvPara)
333 {
334     PWINE_MEMSTORE store;
335
336     TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
337
338     if (dwFlags & CERT_STORE_DELETE_FLAG)
339     {
340         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
341         store = NULL;
342     }
343     else
344     {
345         store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
346         if (store)
347         {
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));
362         }
363     }
364     return (PWINECRYPT_CERTSTORE)store;
365 }
366
367 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
368 {
369     PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
370     PWINE_STORE_LIST_ENTRY entry, next;
371
372     TRACE("(%p, %08x)\n", store, dwFlags);
373
374     LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
375      entry)
376     {
377         TRACE("closing %p\n", entry);
378         CertCloseStore((HCERTSTORE)entry->store, dwFlags);
379         CryptMemFree(entry);
380     }
381     cs->cs.DebugInfo->Spare[0] = 0;
382     DeleteCriticalSection(&cs->cs);
383     CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
384 }
385
386 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store,
387  PWINE_STORE_LIST_ENTRY storeEntry, void *child, size_t contextSize,
388  BOOL addRef)
389 {
390     void *ret = Context_CreateLinkContext(contextSize, child,
391      sizeof(PWINE_STORE_LIST_ENTRY), addRef);
392
393     if (ret)
394         *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(ret, contextSize)
395          = storeEntry;
396
397     return ret;
398 }
399
400 static BOOL CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store,
401  unsigned int contextStoreOffset, void *context, void *toReplace, unsigned int contextSize,
402  void **pChildContext)
403 {
404     BOOL ret;
405     void *childContext = NULL;
406     PWINE_STORE_LIST_ENTRY storeEntry = NULL;
407
408     TRACE("(%p, %d, %p, %p, %d)\n", store, contextStoreOffset, context,
409      toReplace, contextSize);
410
411     ret = FALSE;
412     if (toReplace)
413     {
414         void *existingLinked = Context_GetLinkedContext(toReplace, contextSize);
415         PCONTEXT_STORE contextStore;
416
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);
423     }
424     else
425     {
426         PWINE_STORE_LIST_ENTRY entry, next;
427
428         EnterCriticalSection(&store->cs);
429         LIST_FOR_EACH_ENTRY_SAFE(entry, next, &store->stores,
430          WINE_STORE_LIST_ENTRY, entry)
431         {
432             if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
433             {
434                 PCONTEXT_STORE contextStore = (PCONTEXT_STORE)(
435                  (LPBYTE)entry->store + contextStoreOffset);
436
437                 storeEntry = entry;
438                 ret = contextStore->addContext(entry->store, context, NULL,
439                  (const void **)&childContext);
440                 break;
441             }
442         }
443         LeaveCriticalSection(&store->cs);
444         if (!storeEntry)
445             SetLastError(E_ACCESSDENIED);
446     }
447     *pChildContext = childContext;
448     return ret;
449 }
450
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.
459  */
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)
463 {
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);
468
469     TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
470
471     if (pPrev)
472     {
473         /* Ref-counting funny business: "duplicate" (addref) the child, because
474          * the free(pPrev) below can cause the ref count to become negative.
475          */
476         child = Context_GetLinkedContext(pPrev, contextSize);
477         contextInterface->duplicate(child);
478         child = contextStore->enumContext(storeEntry->store, child);
479         contextInterface->free(pPrev);
480         pPrev = NULL;
481     }
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
488     {
489         if (storeNext)
490             ret = CRYPT_CollectionAdvanceEnum(store, LIST_ENTRY(storeNext,
491              WINE_STORE_LIST_ENTRY, entry), contextStoreOffset,
492              contextInterface, NULL, contextSize);
493         else
494         {
495             SetLastError(CRYPT_E_NOT_FOUND);
496             ret = NULL;
497         }
498     }
499     TRACE("returning %p\n", ret);
500     return ret;
501 }
502
503 static BOOL CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store, void *cert,
504  void *toReplace, const void **ppStoreContext)
505 {
506     BOOL ret;
507     void *childContext = NULL;
508     PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
509
510     ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, certs),
511      cert, toReplace, sizeof(CERT_CONTEXT), &childContext);
512     if (ppStoreContext && childContext)
513     {
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);
519
520         if (context)
521             context->hCertStore = store;
522         *ppStoreContext = context;
523     }
524     CertFreeCertificateContext((PCCERT_CONTEXT)childContext);
525     return ret;
526 }
527
528 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
529 {
530     PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
531     void *ret;
532
533     TRACE("(%p, %p)\n", store, pPrev);
534
535     EnterCriticalSection(&cs->cs);
536     if (pPrev)
537     {
538         PWINE_STORE_LIST_ENTRY storeEntry =
539          *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
540          sizeof(CERT_CONTEXT));
541
542         ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
543          offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, pPrev,
544          sizeof(CERT_CONTEXT));
545     }
546     else
547     {
548         if (!list_empty(&cs->stores))
549         {
550             PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
551              WINE_STORE_LIST_ENTRY, entry);
552
553             ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
554              offsetof(WINECRYPT_CERTSTORE, certs), pCertInterface, NULL,
555              sizeof(CERT_CONTEXT));
556         }
557         else
558         {
559             SetLastError(CRYPT_E_NOT_FOUND);
560             ret = NULL;
561         }
562     }
563     LeaveCriticalSection(&cs->cs);
564     if (ret)
565         ((PCERT_CONTEXT)ret)->hCertStore = store;
566     TRACE("returning %p\n", ret);
567     return ret;
568 }
569
570 static BOOL CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store,
571  void *pCertContext)
572 {
573     BOOL ret;
574
575     TRACE("(%p, %p)\n", store, pCertContext);
576
577     ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)
578      Context_GetLinkedContext(pCertContext, sizeof(CERT_CONTEXT)));
579     return ret;
580 }
581
582 static BOOL CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
583  void *toReplace, const void **ppStoreContext)
584 {
585     BOOL ret;
586     void *childContext = NULL;
587     PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
588
589     ret = CRYPT_CollectionAddContext(cs, offsetof(WINECRYPT_CERTSTORE, crls),
590      crl, toReplace, sizeof(CRL_CONTEXT), &childContext);
591     if (ppStoreContext && childContext)
592     {
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);
598
599         if (context)
600             context->hCertStore = store;
601         *ppStoreContext = context;
602     }
603     CertFreeCRLContext((PCCRL_CONTEXT)childContext);
604     return ret;
605 }
606
607 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
608 {
609     PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
610     void *ret;
611
612     TRACE("(%p, %p)\n", store, pPrev);
613
614     EnterCriticalSection(&cs->cs);
615     if (pPrev)
616     {
617         PWINE_STORE_LIST_ENTRY storeEntry =
618          *(PWINE_STORE_LIST_ENTRY *)Context_GetExtra(pPrev,
619          sizeof(CRL_CONTEXT));
620
621         ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
622          offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, pPrev,
623          sizeof(CRL_CONTEXT));
624     }
625     else
626     {
627         if (!list_empty(&cs->stores))
628         {
629             PWINE_STORE_LIST_ENTRY storeEntry = LIST_ENTRY(cs->stores.next,
630              WINE_STORE_LIST_ENTRY, entry);
631
632             ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry,
633              offsetof(WINECRYPT_CERTSTORE, crls), pCRLInterface, NULL,
634              sizeof(CRL_CONTEXT));
635         }
636         else
637         {
638             SetLastError(CRYPT_E_NOT_FOUND);
639             ret = NULL;
640         }
641     }
642     LeaveCriticalSection(&cs->cs);
643     if (ret)
644         ((PCRL_CONTEXT)ret)->hCertStore = store;
645     TRACE("returning %p\n", ret);
646     return ret;
647 }
648
649 static BOOL CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store,
650  void *pCrlContext)
651 {
652     BOOL ret;
653
654     TRACE("(%p, %p)\n", store, pCrlContext);
655
656     ret = CertDeleteCRLFromStore((PCCRL_CONTEXT)
657      Context_GetLinkedContext(pCrlContext, sizeof(CRL_CONTEXT)));
658     return ret;
659 }
660
661 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
662  DWORD dwFlags, const void *pvPara)
663 {
664     PWINE_COLLECTIONSTORE store;
665
666     if (dwFlags & CERT_STORE_DELETE_FLAG)
667     {
668         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
669         store = NULL;
670     }
671     else
672     {
673         store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
674         if (store)
675         {
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);
689         }
690     }
691     return (PWINECRYPT_CERTSTORE)store;
692 }
693
694 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
695 {
696     PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
697
698     TRACE("(%p, %08x)\n", store, dwFlags);
699
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);
705 }
706
707 static BOOL CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store, void *cert,
708  void *toReplace, const void **ppStoreContext)
709 {
710     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
711     BOOL ret;
712
713     TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
714
715     if (toReplace)
716         ret = ps->memStore->certs.addContext(ps->memStore, cert, toReplace,
717          (const void **)ppStoreContext);
718     else
719     {
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);
727     }
728     /* dirty trick: replace the returned context's hCertStore with
729      * store.
730      */
731     if (ppStoreContext)
732         (*(PCERT_CONTEXT *)ppStoreContext)->hCertStore = store;
733     return ret;
734 }
735
736 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
737 {
738     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
739     void *ret;
740
741     ret = ps->memStore->certs.enumContext(ps->memStore, pPrev);
742     if (ret)
743     {
744         /* same dirty trick: replace the returned context's hCertStore with
745          * store.
746          */
747         ((PCERT_CONTEXT)ret)->hCertStore = store;
748     }
749     return ret;
750 }
751
752 static BOOL CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store, void *cert)
753 {
754     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
755     BOOL ret = TRUE;
756
757     TRACE("(%p, %p)\n", store, cert);
758
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;
764 }
765
766 static BOOL CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store, void *crl,
767  void *toReplace, const void **ppStoreContext)
768 {
769     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
770     BOOL ret;
771
772     TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
773
774     if (toReplace)
775         ret = ps->memStore->crls.addContext(ps->memStore, crl, toReplace,
776          (const void **)ppStoreContext);
777     else
778     {
779         if (ps->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
780         {
781             SetLastError(ERROR_ACCESS_DENIED);
782             ret = FALSE;
783         }
784         else
785         {
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);
793         }
794     }
795     /* dirty trick: replace the returned context's hCertStore with
796      * store.
797      */
798     if (ppStoreContext)
799         (*(PCRL_CONTEXT *)ppStoreContext)->hCertStore = store;
800     return ret;
801 }
802
803 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store, void *pPrev)
804 {
805     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
806     void *ret;
807
808     ret = ps->memStore->crls.enumContext(ps->memStore, pPrev);
809     if (ret)
810     {
811         /* same dirty trick: replace the returned context's hCertStore with
812          * store.
813          */
814         ((PCERT_CONTEXT)ret)->hCertStore = store;
815     }
816     return ret;
817 }
818
819 static BOOL CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store, void *crl)
820 {
821     PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
822     BOOL ret = TRUE;
823
824     TRACE("(%p, %p)\n", store, crl);
825
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;
831 }
832
833 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
834  DWORD dwCtrlType, void const *pvCtrlPara)
835 {
836     PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
837     BOOL ret = TRUE;
838
839     TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
840      pvCtrlPara);
841
842     if (store->provControl)
843         ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
844          pvCtrlPara);
845     return ret;
846 }
847
848 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
849  DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, const CERT_STORE_PROV_INFO *pProvInfo)
850 {
851     PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
852      sizeof(WINE_PROVIDERSTORE));
853
854     if (ret)
855     {
856         CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
857          StoreTypeProvider);
858         ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
859         if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
860         {
861             CertCloseStore(memStore, 0);
862             ret->memStore = NULL;
863         }
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;
910     }
911     return (PWINECRYPT_CERTSTORE)ret;
912 }
913
914 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
915  DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
916 {
917     static HCRYPTOIDFUNCSET set = NULL;
918     PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
919     HCRYPTOIDFUNCADDR hFunc;
920     PWINECRYPT_CERTSTORE ret = NULL;
921
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)
927     {
928         CERT_STORE_PROV_INFO provInfo = { 0 };
929
930         provInfo.cbSize = sizeof(provInfo);
931         if (dwFlags & CERT_STORE_DELETE_FLAG)
932             provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
933              dwFlags, pvPara, NULL, &provInfo);
934         else
935         {
936             HCERTSTORE memStore;
937
938             memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
939              CERT_STORE_CREATE_NEW_FLAG, NULL);
940             if (memStore)
941             {
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);
948             }
949         }
950         CryptFreeOIDFunctionAddress(hFunc, 0);
951     }
952     else
953         SetLastError(ERROR_FILE_NOT_FOUND);
954     return ret;
955 }
956
957 static void CRYPT_HashToStr(const BYTE *hash, LPWSTR asciiHash)
958 {
959     static const WCHAR fmt[] = { '%','0','2','X',0 };
960     DWORD i;
961
962     assert(hash);
963     assert(asciiHash);
964
965     for (i = 0; i < 20; i++)
966         wsprintfW(asciiHash + i * 2, fmt, hash[i]);
967 }
968
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 };
974
975 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO *store, HKEY key,
976  DWORD contextType)
977 {
978     LONG rc;
979     DWORD index = 0;
980     WCHAR subKeyName[MAX_PATH];
981
982     do {
983         DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
984
985         rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
986          NULL);
987         if (!rc)
988         {
989             HKEY subKey;
990
991             rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
992             if (!rc)
993             {
994                 LPBYTE buf = NULL;
995
996                 size = 0;
997                 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
998                 if (!rc)
999                     buf = CryptMemAlloc(size);
1000                 if (buf)
1001                 {
1002                     rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
1003                      &size);
1004                     if (!rc)
1005                     {
1006                         const void *context;
1007                         DWORD addedType;
1008
1009                         TRACE("Adding cert with hash %s\n",
1010                          debugstr_w(subKeyName));
1011                         context = CRYPT_ReadSerializedElement(buf, size,
1012                          contextType, &addedType);
1013                         if (context)
1014                         {
1015                             const WINE_CONTEXT_INTERFACE *contextInterface;
1016                             BYTE hash[20];
1017
1018                             switch (addedType)
1019                             {
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;
1031                             }
1032                             if (contextInterface)
1033                             {
1034                                 size = sizeof(hash);
1035                                 if (contextInterface->getProp(context,
1036                                  CERT_HASH_PROP_ID, hash, &size))
1037                                 {
1038                                     WCHAR asciiHash[20 * 2 + 1];
1039
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))
1045                                     {
1046                                         TRACE("hash matches, adding\n");
1047                                         contextInterface->addContextToStore(
1048                                          store->memStore, context,
1049                                          CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1050                                     }
1051                                     else
1052                                         TRACE("hash doesn't match, ignoring\n");
1053                                 }
1054                                 contextInterface->free(context);
1055                             }
1056                         }
1057                     }
1058                     CryptMemFree(buf);
1059                 }
1060                 RegCloseKey(subKey);
1061             }
1062             /* Ignore intermediate errors, continue enumerating */
1063             rc = ERROR_SUCCESS;
1064         }
1065     } while (!rc);
1066 }
1067
1068 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO *store)
1069 {
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;
1074
1075     for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1076     {
1077         HKEY key;
1078         LONG rc;
1079
1080         rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1081          &key, NULL);
1082         if (!rc)
1083         {
1084             CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1085             RegCloseKey(key);
1086         }
1087     }
1088 }
1089
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)
1093 {
1094     WCHAR asciiHash[20 * 2 + 1];
1095     LONG rc;
1096     HKEY subKey;
1097     BOOL ret;
1098
1099     CRYPT_HashToStr(hash, asciiHash);
1100     rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1101      &subKey, NULL);
1102     if (!rc)
1103     {
1104         rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1105         RegCloseKey(subKey);
1106     }
1107     if (!rc)
1108         ret = TRUE;
1109     else
1110     {
1111         SetLastError(rc);
1112         ret = FALSE;
1113     }
1114     return ret;
1115 }
1116
1117 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1118  const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1119 {
1120     const void *context = NULL;
1121     BOOL ret;
1122
1123     do {
1124         context = contextInterface->enumContextsInStore(memStore, context);
1125         if (context)
1126         {
1127             BYTE hash[20];
1128             DWORD hashSize = sizeof(hash);
1129
1130             ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1131              &hashSize);
1132             if (ret)
1133             {
1134                 DWORD size = 0;
1135                 LPBYTE buf = NULL;
1136
1137                 ret = contextInterface->serialize(context, 0, NULL, &size);
1138                 if (size)
1139                     buf = CryptMemAlloc(size);
1140                 if (buf)
1141                 {
1142                     ret = contextInterface->serialize(context, 0, buf, &size);
1143                     if (ret)
1144                         ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1145                 }
1146                 CryptMemFree(buf);
1147             }
1148         }
1149         else
1150             ret = TRUE;
1151     } while (ret && context != NULL);
1152     if (context)
1153         contextInterface->free(context);
1154     return ret;
1155 }
1156
1157 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1158 {
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;
1166
1167     for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1168     {
1169         HKEY key;
1170         LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1171          KEY_ALL_ACCESS, NULL, &key, NULL);
1172
1173         if (!rc)
1174         {
1175             if (listToDelete[i])
1176             {
1177                 PWINE_HASH_TO_DELETE toDelete, next;
1178                 WCHAR asciiHash[20 * 2 + 1];
1179
1180                 EnterCriticalSection(&store->cs);
1181                 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1182                  WINE_HASH_TO_DELETE, entry)
1183                 {
1184                     LONG rc;
1185
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)
1190                     {
1191                         SetLastError(rc);
1192                         ret = FALSE;
1193                     }
1194                     list_remove(&toDelete->entry);
1195                     CryptMemFree(toDelete);
1196                 }
1197                 LeaveCriticalSection(&store->cs);
1198             }
1199             ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1200              store->memStore);
1201             RegCloseKey(key);
1202         }
1203         else
1204         {
1205             SetLastError(rc);
1206             ret = FALSE;
1207         }
1208     }
1209     return ret;
1210 }
1211
1212 /* If force is true or the registry store is dirty, writes the contents of the
1213  * store to the registry.
1214  */
1215 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1216 {
1217     BOOL ret;
1218
1219     TRACE("(%p, %d)\n", store, force);
1220
1221     if (store->dirty || force)
1222         ret = CRYPT_RegWriteToReg(store);
1223     else
1224         ret = TRUE;
1225     return ret;
1226 }
1227
1228 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1229 {
1230     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1231
1232     TRACE("(%p, %08x)\n", store, dwFlags);
1233     if (dwFlags)
1234         FIXME("Unimplemented flags: %08x\n", dwFlags);
1235
1236     CRYPT_RegFlushStore(store, FALSE);
1237     RegCloseKey(store->key);
1238     store->cs.DebugInfo->Spare[0] = 0;
1239     DeleteCriticalSection(&store->cs);
1240     CryptMemFree(store);
1241 }
1242
1243 static BOOL WINAPI CRYPT_RegWriteContext(PWINE_REGSTOREINFO store,
1244  const void *context, DWORD dwFlags)
1245 {
1246     BOOL ret;
1247
1248     if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1249     {
1250         store->dirty = TRUE;
1251         ret = TRUE;
1252     }
1253     else
1254         ret = FALSE;
1255     return ret;
1256 }
1257
1258 static BOOL CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store,
1259  struct list *deleteList, const void *context,
1260  PCWINE_CONTEXT_INTERFACE contextInterface)
1261 {
1262     BOOL ret;
1263
1264     if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1265     {
1266         SetLastError(ERROR_ACCESS_DENIED);
1267         ret = FALSE;
1268     }
1269     else
1270     {
1271         PWINE_HASH_TO_DELETE toDelete =
1272          CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1273
1274         if (toDelete)
1275         {
1276             DWORD size = sizeof(toDelete->hash);
1277
1278             ret = contextInterface->getProp(context, CERT_HASH_PROP_ID,
1279              toDelete->hash, &size);
1280             if (ret)
1281             {
1282                 EnterCriticalSection(&store->cs);
1283                 list_add_tail(deleteList, &toDelete->entry);
1284                 LeaveCriticalSection(&store->cs);
1285             }
1286             else
1287             {
1288                 CryptMemFree(toDelete);
1289                 ret = FALSE;
1290             }
1291         }
1292         else
1293             ret = FALSE;
1294         if (ret)
1295             store->dirty = TRUE;
1296     }
1297     return ret;
1298 }
1299
1300 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1301  PCCERT_CONTEXT cert, DWORD dwFlags)
1302 {
1303     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1304
1305     TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1306
1307     return CRYPT_RegWriteContext(store, cert, dwFlags);
1308 }
1309
1310 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1311  PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1312 {
1313     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1314
1315     TRACE("(%p, %p, %08x)\n", store, pCertContext, dwFlags);
1316
1317     return CRYPT_RegDeleteContext(store, &store->certsToDelete, pCertContext,
1318      pCertInterface);
1319 }
1320
1321 static BOOL WINAPI CRYPT_RegWriteCRL(HCERTSTORE hCertStore,
1322  PCCRL_CONTEXT crl, DWORD dwFlags)
1323 {
1324     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1325
1326     TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1327
1328     return CRYPT_RegWriteContext(store, crl, dwFlags);
1329 }
1330
1331 static BOOL WINAPI CRYPT_RegDeleteCRL(HCERTSTORE hCertStore,
1332  PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1333 {
1334     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1335
1336     TRACE("(%p, %p, %08x)\n", store, pCrlContext, dwFlags);
1337
1338     return CRYPT_RegDeleteContext(store, &store->crlsToDelete, pCrlContext,
1339      pCRLInterface);
1340 }
1341
1342 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1343  DWORD dwCtrlType, void const *pvCtrlPara)
1344 {
1345     PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1346     BOOL ret;
1347
1348     TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1349      pvCtrlPara);
1350
1351     switch (dwCtrlType)
1352     {
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;
1366     }
1367     return ret;
1368 }
1369
1370 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1371 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1372 {
1373     DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1374     WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1375     HKEY hSubKey = 0;
1376
1377     TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1378
1379     dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1380     if (!dwRet)
1381     {
1382         /* Find how many subkeys there are */
1383         dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1384          &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1385         if (!dwRet)
1386         {
1387             dwMaxSubkeyLen++;
1388             if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1389             {
1390                 /* Name too big: alloc a buffer for it */
1391                 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1392             }
1393
1394             if (!lpszName)
1395                 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1396             else
1397             {
1398                 /* Recursively delete all the subkeys */
1399                 for (i = 0; i < dwKeyCount && !dwRet; i++)
1400                 {
1401                     dwSize = dwMaxSubkeyLen;
1402                     dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1403                      NULL, NULL, NULL);
1404                     if (!dwRet)
1405                         dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1406                 }
1407
1408                 if (lpszName != szNameBuf)
1409                 {
1410                     /* Free buffer if allocated */
1411                     CryptMemFree(lpszName);
1412                 }
1413             }
1414         }
1415
1416         RegCloseKey(hSubKey);
1417         if (!dwRet)
1418             dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1419     }
1420     return dwRet;
1421 }
1422
1423 static void *regProvFuncs[] = {
1424     CRYPT_RegCloseStore,
1425     NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1426     CRYPT_RegWriteCert,
1427     CRYPT_RegDeleteCert,
1428     NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1429     NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1430     CRYPT_RegWriteCRL,
1431     CRYPT_RegDeleteCRL,
1432     NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1433     NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1434     NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1435     NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1436     NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1437     CRYPT_RegControl,
1438 };
1439
1440 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1441  DWORD dwFlags, const void *pvPara)
1442 {
1443     PWINECRYPT_CERTSTORE store = NULL;
1444
1445     TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1446
1447     if (dwFlags & CERT_STORE_DELETE_FLAG)
1448     {
1449         DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1450
1451         if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1452             rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1453         if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1454             rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1455         if (rc == ERROR_NO_MORE_ITEMS)
1456             rc = ERROR_SUCCESS;
1457         SetLastError(rc);
1458     }
1459     else
1460     {
1461         HKEY key;
1462
1463         if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1464          GetCurrentProcess(), (LPHANDLE)&key,
1465          dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1466          TRUE, 0))
1467         {
1468             PWINECRYPT_CERTSTORE memStore;
1469
1470             memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1471             if (memStore)
1472             {
1473                 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1474                  sizeof(WINE_REGSTOREINFO));
1475
1476                 if (regInfo)
1477                 {
1478                     CERT_STORE_PROV_INFO provInfo = { 0 };
1479
1480                     regInfo->dwOpenFlags = dwFlags;
1481                     regInfo->cryptProv = hCryptProv;
1482                     regInfo->memStore = memStore;
1483                     regInfo->key = key;
1484                     InitializeCriticalSection(&regInfo->cs);
1485                     regInfo->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": PWINE_REGSTOREINFO->cs");
1486                     list_init(&regInfo->certsToDelete);
1487                     list_init(&regInfo->crlsToDelete);
1488                     CRYPT_RegReadFromReg(regInfo);
1489                     regInfo->dirty = FALSE;
1490                     provInfo.cbSize = sizeof(provInfo);
1491                     provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1492                      sizeof(regProvFuncs[0]);
1493                     provInfo.rgpvStoreProvFunc = regProvFuncs;
1494                     provInfo.hStoreProv = regInfo;
1495                     store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1496                      &provInfo);
1497                 }
1498             }
1499         }
1500     }
1501     TRACE("returning %p\n", store);
1502     return store;
1503 }
1504
1505 /* FIXME: this isn't complete for the Root store, in which the top-level
1506  * self-signed CA certs reside.  Adding a cert to the Root store should present
1507  * the user with a dialog indicating the consequences of doing so, and asking
1508  * the user to confirm whether the cert should be added.
1509  */
1510 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1511  DWORD dwFlags, const void *pvPara)
1512 {
1513     static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1514     LPCWSTR storeName = (LPCWSTR)pvPara;
1515     LPWSTR storePath;
1516     PWINECRYPT_CERTSTORE store = NULL;
1517     HKEY root;
1518     LPCWSTR base;
1519     BOOL ret;
1520
1521     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1522      debugstr_w((LPCWSTR)pvPara));
1523
1524     if (!pvPara)
1525     {
1526         SetLastError(E_INVALIDARG);
1527         return NULL;
1528     }
1529
1530     ret = TRUE;
1531     switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1532     {
1533     case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1534         root = HKEY_LOCAL_MACHINE;
1535         base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1536         break;
1537     case CERT_SYSTEM_STORE_CURRENT_USER:
1538         root = HKEY_CURRENT_USER;
1539         base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1540         break;
1541     case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1542         /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1543          * SystemCertificates
1544          */
1545         FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1546          debugstr_w(storeName));
1547         return NULL;
1548     case CERT_SYSTEM_STORE_SERVICES:
1549         /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1550          * SystemCertificates
1551          */
1552         FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1553          debugstr_w(storeName));
1554         return NULL;
1555     case CERT_SYSTEM_STORE_USERS:
1556         /* hku\user sid\Software\Microsoft\SystemCertificates */
1557         FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1558          debugstr_w(storeName));
1559         return NULL;
1560     case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1561         root = HKEY_CURRENT_USER;
1562         base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1563         break;
1564     case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1565         root = HKEY_LOCAL_MACHINE;
1566         base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1567         break;
1568     case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1569         /* hklm\Software\Microsoft\EnterpriseCertificates */
1570         FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1571          debugstr_w(storeName));
1572         return NULL;
1573     default:
1574         SetLastError(E_INVALIDARG);
1575         return NULL;
1576     }
1577
1578     storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1579      sizeof(WCHAR));
1580     if (storePath)
1581     {
1582         LONG rc;
1583         HKEY key;
1584         REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1585             KEY_ALL_ACCESS;
1586
1587         wsprintfW(storePath, fmt, base, storeName);
1588         if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1589             rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1590         else
1591         {
1592             DWORD disp;
1593
1594             rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1595                                  &key, &disp);
1596             if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1597                 disp == REG_OPENED_EXISTING_KEY)
1598             {
1599                 RegCloseKey(key);
1600                 rc = ERROR_FILE_EXISTS;
1601             }
1602         }
1603         if (!rc)
1604         {
1605             store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1606             RegCloseKey(key);
1607         }
1608         else
1609             SetLastError(rc);
1610         CryptMemFree(storePath);
1611     }
1612     return store;
1613 }
1614
1615 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1616  DWORD dwFlags, const void *pvPara)
1617 {
1618     int len;
1619     PWINECRYPT_CERTSTORE ret = NULL;
1620
1621     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1622      debugstr_a((LPCSTR)pvPara));
1623
1624     if (!pvPara)
1625     {
1626         SetLastError(ERROR_FILE_NOT_FOUND);
1627         return NULL;
1628     }
1629     len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1630     if (len)
1631     {
1632         LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1633
1634         if (storeName)
1635         {
1636             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1637             ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1638             CryptMemFree(storeName);
1639         }
1640     }
1641     return ret;
1642 }
1643
1644 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1645  DWORD dwFlags, const void *pvPara)
1646 {
1647     HCERTSTORE store = 0;
1648     BOOL ret;
1649
1650     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1651      debugstr_w((LPCWSTR)pvPara));
1652
1653     if (!pvPara)
1654     {
1655         SetLastError(ERROR_FILE_NOT_FOUND);
1656         return NULL;
1657     }
1658     /* This returns a different error than system registry stores if the
1659      * location is invalid.
1660      */
1661     switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1662     {
1663     case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1664     case CERT_SYSTEM_STORE_CURRENT_USER:
1665     case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1666     case CERT_SYSTEM_STORE_SERVICES:
1667     case CERT_SYSTEM_STORE_USERS:
1668     case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1669     case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1670     case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1671         ret = TRUE;
1672         break;
1673     default:
1674         SetLastError(ERROR_FILE_NOT_FOUND);
1675         ret = FALSE;
1676     }
1677     if (ret)
1678     {
1679         HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1680          0, hCryptProv, dwFlags, pvPara);
1681
1682         if (regStore)
1683         {
1684             store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1685              CERT_STORE_CREATE_NEW_FLAG, NULL);
1686             CertAddStoreToCollection(store, regStore,
1687              dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1688              CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1689             CertCloseStore(regStore, 0);
1690             /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1691              * stores.
1692              */
1693             if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
1694              CERT_SYSTEM_STORE_CURRENT_USER)
1695             {
1696                 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
1697                 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
1698                 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
1699                  hCryptProv, dwFlags, pvPara);
1700                 if (regStore)
1701                 {
1702                     CertAddStoreToCollection(store, regStore,
1703                      dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1704                      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1705                     CertCloseStore(regStore, 0);
1706                 }
1707             }
1708         }
1709     }
1710     return (PWINECRYPT_CERTSTORE)store;
1711 }
1712
1713 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1714  DWORD dwFlags, const void *pvPara)
1715 {
1716     int len;
1717     PWINECRYPT_CERTSTORE ret = NULL;
1718
1719     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1720      debugstr_a((LPCSTR)pvPara));
1721
1722     if (!pvPara)
1723     {
1724         SetLastError(ERROR_FILE_NOT_FOUND);
1725         return NULL;
1726     }
1727     len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1728     if (len)
1729     {
1730         LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1731
1732         if (storeName)
1733         {
1734             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1735             ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1736             CryptMemFree(storeName);
1737         }
1738     }
1739     return ret;
1740 }
1741
1742 static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1743 {
1744     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1745
1746     TRACE("(%p, %08x)\n", store, dwFlags);
1747     if (store->dirty)
1748         CRYPT_WriteSerializedFile(store->file, store->memStore);
1749     CertCloseStore(store->memStore, dwFlags);
1750     CloseHandle(store->file);
1751     CryptMemFree(store);
1752 }
1753
1754 static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore,
1755  PCCERT_CONTEXT cert, DWORD dwFlags)
1756 {
1757     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1758
1759     TRACE("(%p, %p, %d)\n", hCertStore, cert, dwFlags);
1760     store->dirty = TRUE;
1761     return TRUE;
1762 }
1763
1764 static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore,
1765  PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1766 {
1767     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1768
1769     TRACE("(%p, %p, %08x)\n", hCertStore, pCertContext, dwFlags);
1770     store->dirty = TRUE;
1771     return TRUE;
1772 }
1773
1774 static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore,
1775  PCCRL_CONTEXT crl, DWORD dwFlags)
1776 {
1777     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1778
1779     TRACE("(%p, %p, %d)\n", hCertStore, crl, dwFlags);
1780     store->dirty = TRUE;
1781     return TRUE;
1782 }
1783
1784 static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore,
1785  PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
1786 {
1787     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1788
1789     TRACE("(%p, %p, %08x)\n", hCertStore, pCrlContext, dwFlags);
1790     store->dirty = TRUE;
1791     return TRUE;
1792 }
1793
1794 static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags,
1795  DWORD dwCtrlType, void const *pvCtrlPara)
1796 {
1797     PWINE_FILESTOREINFO store = (PWINE_FILESTOREINFO)hCertStore;
1798     BOOL ret;
1799
1800     TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1801      pvCtrlPara);
1802
1803     switch (dwCtrlType)
1804     {
1805     case CERT_STORE_CTRL_RESYNC:
1806         CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1807         CRYPT_ReadSerializedFile(store->file, store);
1808         ret = TRUE;
1809         break;
1810     case CERT_STORE_CTRL_COMMIT:
1811         if (!(store->dwOpenFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1812         {
1813             SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1814             ret = FALSE;
1815         }
1816         else if (store->dirty)
1817             ret = CRYPT_WriteSerializedFile(store->file, store->memStore);
1818         else
1819             ret = TRUE;
1820         break;
1821     default:
1822         FIXME("%d: stub\n", dwCtrlType);
1823         ret = FALSE;
1824     }
1825     return ret;
1826 }
1827
1828 static void *fileProvFuncs[] = {
1829     CRYPT_FileCloseStore,
1830     NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1831     CRYPT_FileWriteCert,
1832     CRYPT_FileDeleteCert,
1833     NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1834     NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1835     CRYPT_FileWriteCRL,
1836     CRYPT_FileDeleteCRL,
1837     NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1838     NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1839     NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1840     NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1841     NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1842     CRYPT_FileControl,
1843 };
1844
1845 static PWINECRYPT_CERTSTORE CRYPT_FileOpenStore(HCRYPTPROV hCryptProv,
1846  DWORD dwFlags, const void *pvPara)
1847 {
1848     PWINECRYPT_CERTSTORE store = NULL;
1849     HANDLE file = (HANDLE)pvPara;
1850
1851     TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
1852
1853     if (!pvPara)
1854     {
1855         SetLastError(ERROR_INVALID_HANDLE);
1856         return NULL;
1857     }
1858     if (dwFlags & CERT_STORE_DELETE_FLAG)
1859     {
1860         SetLastError(E_INVALIDARG);
1861         return NULL;
1862     }
1863     if ((dwFlags & CERT_STORE_READONLY_FLAG) &&
1864      (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG))
1865     {
1866         SetLastError(E_INVALIDARG);
1867         return NULL;
1868     }
1869
1870     if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1871      GetCurrentProcess(), &file, dwFlags & CERT_STORE_READONLY_FLAG ?
1872      GENERIC_READ : GENERIC_READ | GENERIC_WRITE, TRUE, 0))
1873     {
1874         PWINECRYPT_CERTSTORE memStore;
1875
1876         memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1877         if (memStore)
1878         {
1879             if (CRYPT_ReadSerializedFile(file, memStore))
1880             {
1881                 PWINE_FILESTOREINFO info = CryptMemAlloc(
1882                  sizeof(WINE_FILESTOREINFO));
1883
1884                 if (info)
1885                 {
1886                     CERT_STORE_PROV_INFO provInfo = { 0 };
1887
1888                     info->dwOpenFlags = dwFlags;
1889                     info->cryptProv = hCryptProv;
1890                     info->memStore = memStore;
1891                     info->file = file;
1892                     info->dirty = FALSE;
1893                     provInfo.cbSize = sizeof(provInfo);
1894                     provInfo.cStoreProvFunc = sizeof(fileProvFuncs) /
1895                      sizeof(fileProvFuncs[0]);
1896                     provInfo.rgpvStoreProvFunc = fileProvFuncs;
1897                     provInfo.hStoreProv = info;
1898                     store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1899                      &provInfo);
1900                 }
1901             }
1902         }
1903     }
1904     TRACE("returning %p\n", store);
1905     return store;
1906 }
1907
1908 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv,
1909  DWORD dwFlags, const void *pvPara)
1910 {
1911     HCERTSTORE store = 0;
1912     LPCWSTR fileName = (LPCWSTR)pvPara;
1913     DWORD access, create;
1914     HANDLE file;
1915
1916     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
1917
1918     if (!fileName)
1919     {
1920         SetLastError(ERROR_PATH_NOT_FOUND);
1921         return NULL;
1922     }
1923     if (!(dwFlags & (CERT_FILE_STORE_COMMIT_ENABLE_FLAG |
1924      CERT_STORE_READONLY_FLAG)))
1925     {
1926         SetLastError(ERROR_FILE_NOT_FOUND);
1927         return NULL;
1928     }
1929
1930     access = GENERIC_READ;
1931     if (dwFlags & CERT_FILE_STORE_COMMIT_ENABLE_FLAG)
1932         access |= GENERIC_WRITE;
1933     if (dwFlags & CERT_STORE_CREATE_NEW_FLAG)
1934         create = CREATE_NEW;
1935     else if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1936         create = OPEN_EXISTING;
1937     else
1938         create = OPEN_ALWAYS;
1939     file = CreateFileW(fileName, access, FILE_SHARE_READ, NULL, create,
1940      FILE_ATTRIBUTE_NORMAL, NULL);
1941     if (file != INVALID_HANDLE_VALUE)
1942     {
1943         /* FIXME: need to check whether it's a serialized store; if not, fall
1944          * back to a PKCS#7 signed message, then to a single serialized cert.
1945          */
1946         store = CertOpenStore(CERT_STORE_PROV_FILE, 0, hCryptProv, dwFlags,
1947          file);
1948         CloseHandle(file);
1949     }
1950     return (PWINECRYPT_CERTSTORE)store;
1951 }
1952
1953 static PWINECRYPT_CERTSTORE CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv,
1954  DWORD dwFlags, const void *pvPara)
1955 {
1956     int len;
1957     PWINECRYPT_CERTSTORE ret = NULL;
1958
1959     TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
1960      debugstr_a((LPCSTR)pvPara));
1961
1962     if (!pvPara)
1963     {
1964         SetLastError(ERROR_FILE_NOT_FOUND);
1965         return NULL;
1966     }
1967     len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1968     if (len)
1969     {
1970         LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1971
1972         if (storeName)
1973         {
1974             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1975             ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
1976             CryptMemFree(storeName);
1977         }
1978     }
1979     return ret;
1980 }
1981
1982 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
1983  DWORD dwFlags, const void *pvPara)
1984 {
1985     if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
1986         FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
1987     else
1988         FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
1989          debugstr_w((LPCWSTR)pvPara));
1990     return NULL;
1991 }
1992
1993 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1994  DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1995  const void* pvPara)
1996 {
1997     WINECRYPT_CERTSTORE *hcs;
1998     StoreOpenFunc openFunc = NULL;
1999
2000     TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
2001           dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
2002
2003     if (!HIWORD(lpszStoreProvider))
2004     {
2005         switch (LOWORD(lpszStoreProvider))
2006         {
2007         case (int)CERT_STORE_PROV_MEMORY:
2008             openFunc = CRYPT_MemOpenStore;
2009             break;
2010         case (int)CERT_STORE_PROV_FILE:
2011             openFunc = CRYPT_FileOpenStore;
2012             break;
2013         case (int)CERT_STORE_PROV_REG:
2014             openFunc = CRYPT_RegOpenStore;
2015             break;
2016         case (int)CERT_STORE_PROV_FILENAME_A:
2017             openFunc = CRYPT_FileNameOpenStoreA;
2018             break;
2019         case (int)CERT_STORE_PROV_FILENAME_W:
2020             openFunc = CRYPT_FileNameOpenStoreW;
2021             break;
2022         case (int)CERT_STORE_PROV_COLLECTION:
2023             openFunc = CRYPT_CollectionOpenStore;
2024             break;
2025         case (int)CERT_STORE_PROV_SYSTEM_A:
2026             openFunc = CRYPT_SysOpenStoreA;
2027             break;
2028         case (int)CERT_STORE_PROV_SYSTEM_W:
2029             openFunc = CRYPT_SysOpenStoreW;
2030             break;
2031         case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
2032             openFunc = CRYPT_SysRegOpenStoreA;
2033             break;
2034         case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
2035             openFunc = CRYPT_SysRegOpenStoreW;
2036             break;
2037         case (int)CERT_STORE_PROV_PHYSICAL_W:
2038             openFunc = CRYPT_PhysOpenStoreW;
2039             break;
2040         default:
2041             if (LOWORD(lpszStoreProvider))
2042                 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
2043         }
2044     }
2045     else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
2046         openFunc = CRYPT_MemOpenStore;
2047     else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
2048         openFunc = CRYPT_FileOpenStore;
2049     else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
2050         openFunc = CRYPT_SysOpenStoreW;
2051     else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
2052         openFunc = CRYPT_CollectionOpenStore;
2053     else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
2054         openFunc = CRYPT_SysRegOpenStoreW;
2055     else
2056     {
2057         FIXME("unimplemented type %s\n", lpszStoreProvider);
2058         openFunc = NULL;
2059     }
2060
2061     if (!openFunc)
2062         hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
2063          hCryptProv, dwFlags, pvPara);
2064     else
2065         hcs = openFunc(hCryptProv, dwFlags, pvPara);
2066     return (HCERTSTORE)hcs;
2067 }
2068
2069 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
2070  LPCSTR szSubSystemProtocol)
2071 {
2072     if (!szSubSystemProtocol)
2073     {
2074         SetLastError(E_INVALIDARG);
2075         return 0;
2076     }
2077     return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
2078      CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2079 }
2080
2081 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
2082  LPCWSTR szSubSystemProtocol)
2083 {
2084     if (!szSubSystemProtocol)
2085     {
2086         SetLastError(E_INVALIDARG);
2087         return 0;
2088     }
2089     return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
2090      CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
2091 }
2092
2093 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
2094              DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
2095 {
2096     FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore, 
2097           dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
2098     return TRUE;
2099 }
2100
2101 #define CertContext_CopyProperties(to, from) \
2102  Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2103
2104 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2105  PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2106  PCCERT_CONTEXT *ppStoreContext)
2107 {
2108     PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2109     BOOL ret = TRUE;
2110     PCCERT_CONTEXT toAdd = NULL, existing = NULL;
2111
2112     TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
2113      dwAddDisposition, ppStoreContext);
2114
2115     /* Weird case to pass a test */
2116     if (dwAddDisposition == 0)
2117     {
2118         SetLastError(STATUS_ACCESS_VIOLATION);
2119         return FALSE;
2120     }
2121     if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2122     {
2123         BYTE hashToAdd[20];
2124         DWORD size = sizeof(hashToAdd);
2125
2126         ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
2127          hashToAdd, &size);
2128         if (ret)
2129         {
2130             CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
2131
2132             existing = CertFindCertificateInStore(hCertStore,
2133              pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
2134              NULL);
2135         }
2136     }
2137
2138     switch (dwAddDisposition)
2139     {
2140     case CERT_STORE_ADD_ALWAYS:
2141         toAdd = CertDuplicateCertificateContext(pCertContext);
2142         break;
2143     case CERT_STORE_ADD_NEW:
2144         if (existing)
2145         {
2146             TRACE("found matching certificate, not adding\n");
2147             SetLastError(CRYPT_E_EXISTS);
2148             ret = FALSE;
2149         }
2150         else
2151             toAdd = CertDuplicateCertificateContext(pCertContext);
2152         break;
2153     case CERT_STORE_ADD_REPLACE_EXISTING:
2154         toAdd = CertDuplicateCertificateContext(pCertContext);
2155         break;
2156     case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2157         toAdd = CertDuplicateCertificateContext(pCertContext);
2158         if (existing)
2159             CertContext_CopyProperties(toAdd, existing);
2160         break;
2161     case CERT_STORE_ADD_USE_EXISTING:
2162         if (existing)
2163         {
2164             CertContext_CopyProperties(existing, pCertContext);
2165             *ppStoreContext = CertDuplicateCertificateContext(existing);
2166         }
2167         else
2168             toAdd = CertDuplicateCertificateContext(pCertContext);
2169         break;
2170     default:
2171         FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2172         ret = FALSE;
2173     }
2174
2175     if (toAdd)
2176     {
2177         if (store)
2178             ret = store->certs.addContext(store, (void *)toAdd,
2179              (void *)existing, (const void **)ppStoreContext);
2180         else if (ppStoreContext)
2181             *ppStoreContext = CertDuplicateCertificateContext(toAdd);
2182         CertFreeCertificateContext(toAdd);
2183     }
2184     CertFreeCertificateContext(existing);
2185
2186     TRACE("returning %d\n", ret);
2187     return ret;
2188 }
2189
2190 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2191  PCCERT_CONTEXT pPrev)
2192 {
2193     WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2194     PCCERT_CONTEXT ret;
2195
2196     TRACE("(%p, %p)\n", hCertStore, pPrev);
2197     if (!hCertStore)
2198         ret = NULL;
2199     else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2200         ret = NULL;
2201     else
2202         ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
2203     return ret;
2204 }
2205
2206 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2207 {
2208     BOOL ret;
2209
2210     TRACE("(%p)\n", pCertContext);
2211
2212     if (!pCertContext)
2213         ret = TRUE;
2214     else if (!pCertContext->hCertStore)
2215     {
2216         ret = TRUE;
2217         CertFreeCertificateContext(pCertContext);
2218     }
2219     else
2220     {
2221         PWINECRYPT_CERTSTORE hcs =
2222          (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2223
2224         if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2225             ret = FALSE;
2226         else
2227             ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
2228         CertFreeCertificateContext(pCertContext);
2229     }
2230     return ret;
2231 }
2232
2233 #define CrlContext_CopyProperties(to, from) \
2234  Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2235
2236 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
2237  PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2238  PCCRL_CONTEXT* ppStoreContext)
2239 {
2240     PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2241     BOOL ret = TRUE;
2242     PCCRL_CONTEXT toAdd = NULL, existing = NULL;
2243
2244     TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
2245      dwAddDisposition, ppStoreContext);
2246
2247     /* Weird case to pass a test */
2248     if (dwAddDisposition == 0)
2249     {
2250         SetLastError(STATUS_ACCESS_VIOLATION);
2251         return FALSE;
2252     }
2253     if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
2254     {
2255         existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
2256          pCrlContext, NULL);
2257     }
2258
2259     switch (dwAddDisposition)
2260     {
2261     case CERT_STORE_ADD_ALWAYS:
2262         toAdd = CertDuplicateCRLContext(pCrlContext);
2263         break;
2264     case CERT_STORE_ADD_NEW:
2265         if (existing)
2266         {
2267             TRACE("found matching CRL, not adding\n");
2268             SetLastError(CRYPT_E_EXISTS);
2269             ret = FALSE;
2270         }
2271         else
2272             toAdd = CertDuplicateCRLContext(pCrlContext);
2273         break;
2274     case CERT_STORE_ADD_NEWER:
2275         if (existing)
2276         {
2277             LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
2278              &pCrlContext->pCrlInfo->ThisUpdate);
2279
2280             if (newer < 0)
2281                 toAdd = CertDuplicateCRLContext(pCrlContext);
2282             else
2283             {
2284                 TRACE("existing CRL is newer, not adding\n");
2285                 SetLastError(CRYPT_E_EXISTS);
2286                 ret = FALSE;
2287             }
2288         }
2289         else
2290             toAdd = CertDuplicateCRLContext(pCrlContext);
2291         break;
2292     case CERT_STORE_ADD_REPLACE_EXISTING:
2293         toAdd = CertDuplicateCRLContext(pCrlContext);
2294         break;
2295     case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
2296         toAdd = CertDuplicateCRLContext(pCrlContext);
2297         if (existing)
2298             CrlContext_CopyProperties(toAdd, existing);
2299         break;
2300     case CERT_STORE_ADD_USE_EXISTING:
2301         if (existing)
2302             CrlContext_CopyProperties(existing, pCrlContext);
2303         break;
2304     default:
2305         FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
2306         ret = FALSE;
2307     }
2308
2309     if (toAdd)
2310     {
2311         if (store)
2312             ret = store->crls.addContext(store, (void *)toAdd,
2313              (void *)existing, (const void **)ppStoreContext);
2314         else if (ppStoreContext)
2315             *ppStoreContext = CertDuplicateCRLContext(toAdd);
2316         CertFreeCRLContext(toAdd);
2317     }
2318     CertFreeCRLContext(existing);
2319
2320     TRACE("returning %d\n", ret);
2321     return ret;
2322 }
2323
2324 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2325 {
2326     BOOL ret;
2327
2328     TRACE("(%p)\n", pCrlContext);
2329
2330     if (!pCrlContext)
2331         ret = TRUE;
2332     else if (!pCrlContext->hCertStore)
2333     {
2334         ret = TRUE;
2335         CertFreeCRLContext(pCrlContext);
2336     }
2337     else
2338     {
2339         PWINECRYPT_CERTSTORE hcs =
2340          (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
2341
2342         if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2343             ret = FALSE;
2344         else
2345             ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
2346         CertFreeCRLContext(pCrlContext);
2347     }
2348     return ret;
2349 }
2350
2351 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2352  PCCRL_CONTEXT pPrev)
2353 {
2354     WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2355     PCCRL_CONTEXT ret;
2356
2357     TRACE("(%p, %p)\n", hCertStore, pPrev);
2358     if (!hCertStore)
2359         ret = NULL;
2360     else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2361         ret = NULL;
2362     else
2363         ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
2364     return ret;
2365 }
2366
2367 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2368   const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2369 {
2370     FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType, pbCtlEncoded,
2371      cbCtlEncoded);
2372     return NULL;
2373 }
2374
2375 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2376  DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2377  DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2378 {
2379     FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore,
2380      dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2381      ppCtlContext);
2382     return FALSE;
2383 }
2384
2385 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2386  PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2387  PCCTL_CONTEXT* ppStoreContext)
2388 {
2389     FIXME("(%p, %p, %08x, %p): stub\n", hCertStore, pCtlContext,
2390      dwAddDisposition, ppStoreContext);
2391     return TRUE;
2392 }
2393
2394 PCCTL_CONTEXT WINAPI CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext)
2395 {
2396     FIXME("(%p): stub\n", pCtlContext );
2397     return pCtlContext;
2398 }
2399
2400 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2401 {
2402     FIXME("(%p): stub\n", pCtlContext );
2403     return TRUE;
2404 }
2405
2406 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2407 {
2408     FIXME("(%p): stub\n", pCtlContext);
2409     return TRUE;
2410 }
2411
2412 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2413  PCCTL_CONTEXT pPrev)
2414 {
2415     FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2416     return NULL;
2417 }
2418
2419 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
2420 {
2421     WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2422
2423     TRACE("(%p)\n", hCertStore);
2424
2425     if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
2426         InterlockedIncrement(&hcs->ref);
2427     return hCertStore;
2428 }
2429
2430 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2431 {
2432     WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2433
2434     TRACE("(%p, %08x)\n", hCertStore, dwFlags);
2435
2436     if( ! hCertStore )
2437         return TRUE;
2438
2439     if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2440         return FALSE;
2441
2442     if (InterlockedDecrement(&hcs->ref) == 0)
2443     {
2444         TRACE("%p's ref count is 0, freeing\n", hcs);
2445         hcs->dwMagic = 0;
2446         if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2447             CryptReleaseContext(hcs->cryptProv, 0);
2448         hcs->closeStore(hcs, dwFlags);
2449     }
2450     else
2451         TRACE("%p's ref count is %d\n", hcs, hcs->ref);
2452     return TRUE;
2453 }
2454
2455 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2456  DWORD dwCtrlType, void const *pvCtrlPara)
2457 {
2458     WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2459     BOOL ret;
2460
2461     TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
2462      pvCtrlPara);
2463
2464     if (!hcs)
2465         ret = FALSE;
2466     else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2467         ret = FALSE;
2468     else
2469     {
2470         if (hcs->control)
2471             ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2472         else
2473             ret = TRUE;
2474     }
2475     return ret;
2476 }
2477
2478 BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2479  void *pvData, DWORD *pcbData)
2480 {
2481     PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2482     BOOL ret = FALSE;
2483
2484     TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);
2485
2486     switch (dwPropId)
2487     {
2488     case CERT_ACCESS_STATE_PROP_ID:
2489         if (!pvData)
2490         {
2491             *pcbData = sizeof(DWORD);
2492             ret = TRUE;
2493         }
2494         else if (*pcbData < sizeof(DWORD))
2495         {
2496             SetLastError(ERROR_MORE_DATA);
2497             *pcbData = sizeof(DWORD);
2498         }
2499         else
2500         {
2501             DWORD state = 0;
2502
2503             if (store->type != StoreTypeMem &&
2504              !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2505                 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2506             *(DWORD *)pvData = state;
2507             ret = TRUE;
2508         }
2509         break;
2510     default:
2511         if (store->properties)
2512         {
2513             CRYPT_DATA_BLOB blob;
2514
2515             ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
2516              &blob);
2517             if (ret)
2518             {
2519                 if (!pvData)
2520                     *pcbData = blob.cbData;
2521                 else if (*pcbData < blob.cbData)
2522                 {
2523                     SetLastError(ERROR_MORE_DATA);
2524                     *pcbData = blob.cbData;
2525                     ret = FALSE;
2526                 }
2527                 else
2528                 {
2529                     memcpy(pvData, blob.pbData, blob.cbData);
2530                     *pcbData = blob.cbData;
2531                 }
2532             }
2533             else
2534                 SetLastError(CRYPT_E_NOT_FOUND);
2535         }
2536         else
2537             SetLastError(CRYPT_E_NOT_FOUND);
2538     }
2539     return ret;
2540 }
2541
2542 BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
2543  DWORD dwFlags, const void *pvData)
2544 {
2545     PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2546     BOOL ret = FALSE;
2547
2548     TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);
2549
2550     if (!store->properties)
2551         store->properties = ContextPropertyList_Create();
2552     switch (dwPropId)
2553     {
2554     case CERT_ACCESS_STATE_PROP_ID:
2555         SetLastError(E_INVALIDARG);
2556         break;
2557     default:
2558         if (pvData)
2559         {
2560             const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvData;
2561
2562             ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
2563              blob->pbData, blob->cbData);
2564         }
2565         else
2566         {
2567             ContextPropertyList_RemoveProperty(store->properties, dwPropId);
2568             ret = TRUE;
2569         }
2570     }
2571     return ret;
2572 }
2573
2574 DWORD WINAPI CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext,
2575  DWORD dwPropId)
2576 {
2577     FIXME("(%p, %d): stub\n", pCTLContext, dwPropId);
2578     return 0;
2579 }
2580
2581 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2582  DWORD dwPropId, void *pvData, DWORD *pcbData)
2583 {
2584     FIXME("(%p, %d, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2585     return FALSE;
2586 }
2587
2588 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2589  DWORD dwPropId, DWORD dwFlags, const void *pvData)
2590 {
2591     FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2592      pvData);
2593     return FALSE;
2594 }
2595
2596 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
2597  HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
2598 {
2599     PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2600     WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2601     PWINE_STORE_LIST_ENTRY entry;
2602     BOOL ret;
2603
2604     TRACE("(%p, %p, %08x, %d)\n", hCollectionStore, hSiblingStore,
2605      dwUpdateFlags, dwPriority);
2606
2607     if (!collection || !sibling)
2608         return TRUE;
2609     if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2610     {
2611         SetLastError(E_INVALIDARG);
2612         return FALSE;
2613     }
2614     if (collection->hdr.type != StoreTypeCollection)
2615     {
2616         SetLastError(E_INVALIDARG);
2617         return FALSE;
2618     }
2619     if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2620     {
2621         SetLastError(E_INVALIDARG);
2622         return FALSE;
2623     }
2624
2625     entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
2626     if (entry)
2627     {
2628         InterlockedIncrement(&sibling->ref);
2629         TRACE("sibling %p's ref count is %d\n", sibling, sibling->ref);
2630         entry->store = sibling;
2631         entry->dwUpdateFlags = dwUpdateFlags;
2632         entry->dwPriority = dwPriority;
2633         list_init(&entry->entry);
2634         TRACE("%p: adding %p, priority %d\n", collection, entry, dwPriority);
2635         EnterCriticalSection(&collection->cs);
2636         if (dwPriority)
2637         {
2638             PWINE_STORE_LIST_ENTRY cursor;
2639             BOOL added = FALSE;
2640
2641             LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
2642              WINE_STORE_LIST_ENTRY, entry)
2643             {
2644                 if (cursor->dwPriority < dwPriority)
2645                 {
2646                     list_add_before(&cursor->entry, &entry->entry);
2647                     added = TRUE;
2648                     break;
2649                 }
2650             }
2651             if (!added)
2652                 list_add_tail(&collection->stores, &entry->entry);
2653         }
2654         else
2655             list_add_tail(&collection->stores, &entry->entry);
2656         LeaveCriticalSection(&collection->cs);
2657         ret = TRUE;
2658     }
2659     else
2660         ret = FALSE;
2661     return ret;
2662 }
2663
2664 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
2665  HCERTSTORE hSiblingStore)
2666 {
2667     PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
2668     WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
2669     PWINE_STORE_LIST_ENTRY store, next;
2670
2671     TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
2672
2673     if (!collection || !sibling)
2674         return;
2675     if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2676     {
2677         SetLastError(E_INVALIDARG);
2678         return;
2679     }
2680     if (collection->hdr.type != StoreTypeCollection)
2681         return;
2682     if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2683     {
2684         SetLastError(E_INVALIDARG);
2685         return;
2686     }
2687     EnterCriticalSection(&collection->cs);
2688     LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
2689      WINE_STORE_LIST_ENTRY, entry)
2690     {
2691         if (store->store == sibling)
2692         {
2693             list_remove(&store->entry);
2694             CertCloseStore(store->store, 0);
2695             CryptMemFree(store);
2696             break;
2697         }
2698     }
2699     LeaveCriticalSection(&collection->cs);
2700 }
2701
2702 static LONG CRYPT_OpenParentStore(DWORD dwFlags,
2703     void *pvSystemStoreLocationPara, HKEY *key)
2704 {
2705     HKEY root;
2706     LPCWSTR base;
2707
2708     TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);
2709
2710     switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
2711     {
2712     case CERT_SYSTEM_STORE_LOCAL_MACHINE:
2713         root = HKEY_LOCAL_MACHINE;
2714         base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2715         break;
2716     case CERT_SYSTEM_STORE_CURRENT_USER:
2717         root = HKEY_CURRENT_USER;
2718         base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
2719         break;
2720     case CERT_SYSTEM_STORE_CURRENT_SERVICE:
2721         /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2722          * SystemCertificates
2723          */
2724         FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
2725         return ERROR_FILE_NOT_FOUND;
2726     case CERT_SYSTEM_STORE_SERVICES:
2727         /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2728          * SystemCertificates
2729          */
2730         FIXME("CERT_SYSTEM_STORE_SERVICES\n");
2731         return ERROR_FILE_NOT_FOUND;
2732     case CERT_SYSTEM_STORE_USERS:
2733         /* hku\user sid\Software\Microsoft\SystemCertificates */
2734         FIXME("CERT_SYSTEM_STORE_USERS\n");
2735         return ERROR_FILE_NOT_FOUND;
2736     case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
2737         root = HKEY_CURRENT_USER;
2738         base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2739         break;
2740     case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
2741         root = HKEY_LOCAL_MACHINE;
2742         base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
2743         break;
2744     case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
2745         /* hklm\Software\Microsoft\EnterpriseCertificates */
2746         FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
2747         return ERROR_FILE_NOT_FOUND;
2748     default:
2749         return ERROR_FILE_NOT_FOUND;
2750     }
2751
2752     return RegOpenKeyExW(root, base, 0, KEY_READ, key);
2753 }
2754
2755 BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
2756     void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
2757 {
2758     BOOL ret = FALSE;
2759     LONG rc;
2760     HKEY key;
2761
2762     TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
2763         pfnEnum);
2764
2765     rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
2766     if (!rc)
2767     {
2768         DWORD index = 0;
2769         CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
2770
2771         ret = TRUE;
2772         do {
2773             WCHAR name[MAX_PATH];
2774             DWORD size = sizeof(name) / sizeof(name[0]);
2775
2776             rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
2777                 NULL);
2778             if (!rc)
2779                 ret = pfnEnum(name, 0, &info, NULL, pvArg);
2780         } while (ret && !rc);
2781         if (ret && rc != ERROR_NO_MORE_ITEMS)
2782             SetLastError(rc);
2783     }
2784     else
2785         SetLastError(rc);
2786     return ret;
2787 }