2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2004-2006 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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.
27 #include "wine/port.h"
37 #include "wine/debug.h"
38 #include "wine/list.h"
39 #include "wine/exception.h"
40 #include "crypt32_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
44 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
46 static const WINE_CONTEXT_INTERFACE gCertInterface
= {
47 (CreateContextFunc
)CertCreateCertificateContext
,
48 (AddContextToStoreFunc
)CertAddCertificateContextToStore
,
49 (AddEncodedContextToStoreFunc
)CertAddEncodedCertificateToStore
,
50 (DuplicateContextFunc
)CertDuplicateCertificateContext
,
51 (EnumContextsInStoreFunc
)CertEnumCertificatesInStore
,
52 (EnumPropertiesFunc
)CertEnumCertificateContextProperties
,
53 (GetContextPropertyFunc
)CertGetCertificateContextProperty
,
54 (SetContextPropertyFunc
)CertSetCertificateContextProperty
,
55 (SerializeElementFunc
)CertSerializeCertificateStoreElement
,
56 (FreeContextFunc
)CertFreeCertificateContext
,
57 (DeleteContextFunc
)CertDeleteCertificateFromStore
,
59 PCWINE_CONTEXT_INTERFACE pCertInterface
= &gCertInterface
;
61 static const WINE_CONTEXT_INTERFACE gCRLInterface
= {
62 (CreateContextFunc
)CertCreateCRLContext
,
63 (AddContextToStoreFunc
)CertAddCRLContextToStore
,
64 (AddEncodedContextToStoreFunc
)CertAddEncodedCRLToStore
,
65 (DuplicateContextFunc
)CertDuplicateCRLContext
,
66 (EnumContextsInStoreFunc
)CertEnumCRLsInStore
,
67 (EnumPropertiesFunc
)CertEnumCRLContextProperties
,
68 (GetContextPropertyFunc
)CertGetCRLContextProperty
,
69 (SetContextPropertyFunc
)CertSetCRLContextProperty
,
70 (SerializeElementFunc
)CertSerializeCRLStoreElement
,
71 (FreeContextFunc
)CertFreeCRLContext
,
72 (DeleteContextFunc
)CertDeleteCRLFromStore
,
74 PCWINE_CONTEXT_INTERFACE pCRLInterface
= &gCRLInterface
;
76 static const WINE_CONTEXT_INTERFACE gCTLInterface
= {
77 (CreateContextFunc
)CertCreateCTLContext
,
78 (AddContextToStoreFunc
)CertAddCTLContextToStore
,
79 (AddEncodedContextToStoreFunc
)CertAddEncodedCTLToStore
,
80 (DuplicateContextFunc
)CertDuplicateCTLContext
,
81 (EnumContextsInStoreFunc
)CertEnumCTLsInStore
,
82 (EnumPropertiesFunc
)CertEnumCTLContextProperties
,
83 (GetContextPropertyFunc
)CertGetCTLContextProperty
,
84 (SetContextPropertyFunc
)CertSetCTLContextProperty
,
85 (SerializeElementFunc
)CertSerializeCTLStoreElement
,
86 (FreeContextFunc
)CertFreeCTLContext
,
87 (DeleteContextFunc
)CertDeleteCTLFromStore
,
89 PCWINE_CONTEXT_INTERFACE pCTLInterface
= &gCTLInterface
;
91 struct WINE_CRYPTCERTSTORE
;
93 typedef struct WINE_CRYPTCERTSTORE
* (*StoreOpenFunc
)(HCRYPTPROV hCryptProv
,
94 DWORD dwFlags
, const void *pvPara
);
96 /* Called to enumerate the next context in a store. */
97 typedef void * (*EnumFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *pPrev
);
99 /* Called to add a context to a store. If toReplace is not NULL,
100 * context replaces toReplace in the store, and access checks should not be
101 * performed. Otherwise context is a new context, and it should only be
102 * added if the store allows it. If ppStoreContext is not NULL, the added
103 * context should be returned in *ppStoreContext.
105 typedef BOOL (*AddFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *context
,
106 void *toReplace
, const void **ppStoreContext
);
108 typedef BOOL (*DeleteFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *context
);
110 typedef struct _CONTEXT_STORE
113 EnumFunc enumContext
;
114 DeleteFunc deleteContext
;
115 } CONTEXT_STORE
, *PCONTEXT_STORE
;
117 typedef enum _CertStoreType
{
123 /* A cert store is polymorphic through the use of function pointers. A type
124 * is still needed to distinguish collection stores from other types.
125 * On the function pointers:
126 * - closeStore is called when the store's ref count becomes 0
127 * - control is optional, but should be implemented by any store that supports
130 typedef struct WINE_CRYPTCERTSTORE
135 HCRYPTPROV cryptProv
;
137 PFN_CERT_STORE_PROV_CLOSE closeStore
;
140 PFN_CERT_STORE_PROV_CONTROL control
; /* optional */
141 PCONTEXT_PROPERTY_LIST properties
;
142 } WINECRYPT_CERTSTORE
, *PWINECRYPT_CERTSTORE
;
144 typedef struct _WINE_MEMSTORE
146 WINECRYPT_CERTSTORE hdr
;
147 struct ContextList
*certs
;
148 struct ContextList
*crls
;
149 } WINE_MEMSTORE
, *PWINE_MEMSTORE
;
151 typedef struct _WINE_HASH_TO_DELETE
155 } WINE_HASH_TO_DELETE
, *PWINE_HASH_TO_DELETE
;
157 typedef struct _WINE_REGSTOREINFO
160 HCRYPTPROV cryptProv
;
161 PWINECRYPT_CERTSTORE memStore
;
165 struct list certsToDelete
;
166 struct list crlsToDelete
;
167 } WINE_REGSTOREINFO
, *PWINE_REGSTOREINFO
;
169 typedef struct _WINE_FILESTOREINFO
172 HCRYPTPROV cryptProv
;
173 PWINECRYPT_CERTSTORE memStore
;
176 } WINE_FILESTOREINFO
, *PWINE_FILESTOREINFO
;
178 typedef struct _WINE_MSGSTOREINFO
181 HCRYPTPROV cryptProv
;
182 PWINECRYPT_CERTSTORE memStore
;
184 } WINE_MSGSTOREINFO
, *PWINE_MSGSTOREINFO
;
186 typedef struct _WINE_STORE_LIST_ENTRY
188 PWINECRYPT_CERTSTORE store
;
192 } WINE_STORE_LIST_ENTRY
, *PWINE_STORE_LIST_ENTRY
;
194 typedef struct _WINE_COLLECTIONSTORE
196 WINECRYPT_CERTSTORE hdr
;
199 } WINE_COLLECTIONSTORE
, *PWINE_COLLECTIONSTORE
;
201 typedef struct _WINE_PROVIDERSTORE
203 WINECRYPT_CERTSTORE hdr
;
204 DWORD dwStoreProvFlags
;
205 PWINECRYPT_CERTSTORE memStore
;
206 HCERTSTOREPROV hStoreProv
;
207 PFN_CERT_STORE_PROV_CLOSE provCloseStore
;
208 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert
;
209 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert
;
210 PFN_CERT_STORE_PROV_WRITE_CRL provWriteCrl
;
211 PFN_CERT_STORE_PROV_DELETE_CRL provDeleteCrl
;
212 PFN_CERT_STORE_PROV_CONTROL provControl
;
213 } WINE_PROVIDERSTORE
, *PWINE_PROVIDERSTORE
;
215 static void CRYPT_InitStore(WINECRYPT_CERTSTORE
*store
, HCRYPTPROV hCryptProv
,
216 DWORD dwFlags
, CertStoreType type
)
219 store
->dwMagic
= WINE_CRYPTCERTSTORE_MAGIC
;
223 hCryptProv
= CRYPT_GetDefaultProvider();
224 dwFlags
|= CERT_STORE_NO_CRYPT_RELEASE_FLAG
;
226 store
->cryptProv
= hCryptProv
;
227 store
->dwOpenFlags
= dwFlags
;
228 store
->properties
= NULL
;
231 static void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store
)
233 if (store
->properties
)
234 ContextPropertyList_Free(store
->properties
);
238 static BOOL
CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
239 void *toReplace
, const void **ppStoreContext
)
241 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
242 PCERT_CONTEXT context
;
244 TRACE("(%p, %p, %p, %p)\n", store
, cert
, toReplace
, ppStoreContext
);
246 context
= (PCERT_CONTEXT
)ContextList_Add(ms
->certs
, cert
, toReplace
);
249 context
->hCertStore
= store
;
251 *ppStoreContext
= CertDuplicateCertificateContext(context
);
253 return context
? TRUE
: FALSE
;
256 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
258 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
261 TRACE("(%p, %p)\n", store
, pPrev
);
263 ret
= ContextList_Enum(ms
->certs
, pPrev
);
265 SetLastError(CRYPT_E_NOT_FOUND
);
267 TRACE("returning %p\n", ret
);
271 static BOOL
CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store
, void *pCertContext
)
273 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
275 ContextList_Delete(ms
->certs
, pCertContext
);
279 static BOOL
CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store
, void *crl
,
280 void *toReplace
, const void **ppStoreContext
)
282 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
283 PCRL_CONTEXT context
;
285 TRACE("(%p, %p, %p, %p)\n", store
, crl
, toReplace
, ppStoreContext
);
287 context
= (PCRL_CONTEXT
)ContextList_Add(ms
->crls
, crl
, toReplace
);
290 context
->hCertStore
= store
;
292 *ppStoreContext
= CertDuplicateCRLContext(context
);
294 return context
? TRUE
: FALSE
;
297 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store
, void *pPrev
)
299 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
302 TRACE("(%p, %p)\n", store
, pPrev
);
304 ret
= ContextList_Enum(ms
->crls
, pPrev
);
306 SetLastError(CRYPT_E_NOT_FOUND
);
308 TRACE("returning %p\n", ret
);
312 static BOOL
CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store
, void *pCrlContext
)
314 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
316 ContextList_Delete(ms
->crls
, pCrlContext
);
320 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store
)
322 ContextList_Empty(store
->certs
);
323 ContextList_Empty(store
->crls
);
326 static void WINAPI
CRYPT_MemCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
328 WINE_MEMSTORE
*store
= (WINE_MEMSTORE
*)hCertStore
;
330 TRACE("(%p, %08x)\n", store
, dwFlags
);
332 FIXME("Unimplemented flags: %08x\n", dwFlags
);
334 ContextList_Free(store
->certs
);
335 ContextList_Free(store
->crls
);
336 CRYPT_FreeStore((PWINECRYPT_CERTSTORE
)store
);
339 static WINECRYPT_CERTSTORE
*CRYPT_MemOpenStore(HCRYPTPROV hCryptProv
,
340 DWORD dwFlags
, const void *pvPara
)
342 PWINE_MEMSTORE store
;
344 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
346 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
348 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
353 store
= CryptMemAlloc(sizeof(WINE_MEMSTORE
));
356 memset(store
, 0, sizeof(WINE_MEMSTORE
));
357 CRYPT_InitStore(&store
->hdr
, hCryptProv
, dwFlags
, StoreTypeMem
);
358 store
->hdr
.closeStore
= CRYPT_MemCloseStore
;
359 store
->hdr
.certs
.addContext
= CRYPT_MemAddCert
;
360 store
->hdr
.certs
.enumContext
= CRYPT_MemEnumCert
;
361 store
->hdr
.certs
.deleteContext
= CRYPT_MemDeleteCert
;
362 store
->hdr
.crls
.addContext
= CRYPT_MemAddCrl
;
363 store
->hdr
.crls
.enumContext
= CRYPT_MemEnumCrl
;
364 store
->hdr
.crls
.deleteContext
= CRYPT_MemDeleteCrl
;
365 store
->hdr
.control
= NULL
;
366 store
->certs
= ContextList_Create(pCertInterface
,
367 sizeof(CERT_CONTEXT
));
368 store
->crls
= ContextList_Create(pCRLInterface
,
369 sizeof(CRL_CONTEXT
));
372 return (PWINECRYPT_CERTSTORE
)store
;
375 static void WINAPI
CRYPT_CollectionCloseStore(HCERTSTORE store
, DWORD dwFlags
)
377 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
378 PWINE_STORE_LIST_ENTRY entry
, next
;
380 TRACE("(%p, %08x)\n", store
, dwFlags
);
382 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
, &cs
->stores
, WINE_STORE_LIST_ENTRY
,
385 TRACE("closing %p\n", entry
);
386 CertCloseStore((HCERTSTORE
)entry
->store
, dwFlags
);
389 cs
->cs
.DebugInfo
->Spare
[0] = 0;
390 DeleteCriticalSection(&cs
->cs
);
391 CRYPT_FreeStore((PWINECRYPT_CERTSTORE
)store
);
394 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store
,
395 PWINE_STORE_LIST_ENTRY storeEntry
, void *child
, size_t contextSize
,
398 void *ret
= Context_CreateLinkContext(contextSize
, child
,
399 sizeof(PWINE_STORE_LIST_ENTRY
), addRef
);
402 *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(ret
, contextSize
)
408 static BOOL
CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store
,
409 unsigned int contextStoreOffset
, void *context
, void *toReplace
, unsigned int contextSize
,
410 void **pChildContext
)
413 void *childContext
= NULL
;
414 PWINE_STORE_LIST_ENTRY storeEntry
= NULL
;
416 TRACE("(%p, %d, %p, %p, %d)\n", store
, contextStoreOffset
, context
,
417 toReplace
, contextSize
);
422 void *existingLinked
= Context_GetLinkedContext(toReplace
, contextSize
);
423 PCONTEXT_STORE contextStore
;
425 storeEntry
= *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(toReplace
,
427 contextStore
= (PCONTEXT_STORE
)((LPBYTE
)storeEntry
->store
+
429 ret
= contextStore
->addContext(storeEntry
->store
, context
,
430 existingLinked
, (const void **)&childContext
);
434 PWINE_STORE_LIST_ENTRY entry
, next
;
436 EnterCriticalSection(&store
->cs
);
437 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
, &store
->stores
,
438 WINE_STORE_LIST_ENTRY
, entry
)
440 if (entry
->dwUpdateFlags
& CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
)
442 PCONTEXT_STORE contextStore
= (PCONTEXT_STORE
)(
443 (LPBYTE
)entry
->store
+ contextStoreOffset
);
446 ret
= contextStore
->addContext(entry
->store
, context
, NULL
,
447 (const void **)&childContext
);
451 LeaveCriticalSection(&store
->cs
);
453 SetLastError(E_ACCESSDENIED
);
455 *pChildContext
= childContext
;
459 /* Advances a collection enumeration by one context, if possible, where
461 * - calling the current store's enumeration function once, and returning
462 * the enumerated context if one is returned
463 * - moving to the next store if the current store has no more items, and
464 * recursively calling itself to get the next item.
465 * Returns NULL if the collection contains no more items or on error.
466 * Assumes the collection store's lock is held.
468 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store
,
469 PWINE_STORE_LIST_ENTRY storeEntry
, size_t contextStoreOffset
,
470 PCWINE_CONTEXT_INTERFACE contextInterface
, void *pPrev
, size_t contextSize
)
473 struct list
*storeNext
= list_next(&store
->stores
, &storeEntry
->entry
);
474 PCONTEXT_STORE contextStore
= (PCONTEXT_STORE
)((LPBYTE
)storeEntry
->store
+
477 TRACE("(%p, %p, %p)\n", store
, storeEntry
, pPrev
);
481 /* Ref-counting funny business: "duplicate" (addref) the child, because
482 * the free(pPrev) below can cause the ref count to become negative.
484 child
= Context_GetLinkedContext(pPrev
, contextSize
);
485 contextInterface
->duplicate(child
);
486 child
= contextStore
->enumContext(storeEntry
->store
, child
);
487 contextInterface
->free(pPrev
);
491 child
= storeEntry
->store
->certs
.enumContext(storeEntry
->store
, NULL
);
493 ret
= CRYPT_CollectionCreateContextFromChild(store
, storeEntry
, child
,
498 ret
= CRYPT_CollectionAdvanceEnum(store
, LIST_ENTRY(storeNext
,
499 WINE_STORE_LIST_ENTRY
, entry
), contextStoreOffset
,
500 contextInterface
, NULL
, contextSize
);
503 SetLastError(CRYPT_E_NOT_FOUND
);
507 TRACE("returning %p\n", ret
);
511 static BOOL
CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
512 void *toReplace
, const void **ppStoreContext
)
515 void *childContext
= NULL
;
516 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
518 ret
= CRYPT_CollectionAddContext(cs
, offsetof(WINECRYPT_CERTSTORE
, certs
),
519 cert
, toReplace
, sizeof(CERT_CONTEXT
), &childContext
);
520 if (ppStoreContext
&& childContext
)
522 PWINE_STORE_LIST_ENTRY storeEntry
= *(PWINE_STORE_LIST_ENTRY
*)
523 Context_GetExtra(childContext
, sizeof(CERT_CONTEXT
));
524 PCERT_CONTEXT context
=
525 CRYPT_CollectionCreateContextFromChild(cs
, storeEntry
, childContext
,
526 sizeof(CERT_CONTEXT
), TRUE
);
529 context
->hCertStore
= store
;
530 *ppStoreContext
= context
;
532 CertFreeCertificateContext((PCCERT_CONTEXT
)childContext
);
536 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
538 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
541 TRACE("(%p, %p)\n", store
, pPrev
);
543 EnterCriticalSection(&cs
->cs
);
546 PWINE_STORE_LIST_ENTRY storeEntry
=
547 *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(pPrev
,
548 sizeof(CERT_CONTEXT
));
550 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
551 offsetof(WINECRYPT_CERTSTORE
, certs
), pCertInterface
, pPrev
,
552 sizeof(CERT_CONTEXT
));
556 if (!list_empty(&cs
->stores
))
558 PWINE_STORE_LIST_ENTRY storeEntry
= LIST_ENTRY(cs
->stores
.next
,
559 WINE_STORE_LIST_ENTRY
, entry
);
561 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
562 offsetof(WINECRYPT_CERTSTORE
, certs
), pCertInterface
, NULL
,
563 sizeof(CERT_CONTEXT
));
567 SetLastError(CRYPT_E_NOT_FOUND
);
571 LeaveCriticalSection(&cs
->cs
);
573 ((PCERT_CONTEXT
)ret
)->hCertStore
= store
;
574 TRACE("returning %p\n", ret
);
578 static BOOL
CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store
,
583 TRACE("(%p, %p)\n", store
, pCertContext
);
585 ret
= CertDeleteCertificateFromStore((PCCERT_CONTEXT
)
586 Context_GetLinkedContext(pCertContext
, sizeof(CERT_CONTEXT
)));
590 static BOOL
CRYPT_CollectionAddCRL(PWINECRYPT_CERTSTORE store
, void *crl
,
591 void *toReplace
, const void **ppStoreContext
)
594 void *childContext
= NULL
;
595 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
597 ret
= CRYPT_CollectionAddContext(cs
, offsetof(WINECRYPT_CERTSTORE
, crls
),
598 crl
, toReplace
, sizeof(CRL_CONTEXT
), &childContext
);
599 if (ppStoreContext
&& childContext
)
601 PWINE_STORE_LIST_ENTRY storeEntry
= *(PWINE_STORE_LIST_ENTRY
*)
602 Context_GetExtra(childContext
, sizeof(CRL_CONTEXT
));
603 PCRL_CONTEXT context
=
604 CRYPT_CollectionCreateContextFromChild(cs
, storeEntry
, childContext
,
605 sizeof(CRL_CONTEXT
), TRUE
);
608 context
->hCertStore
= store
;
609 *ppStoreContext
= context
;
611 CertFreeCRLContext((PCCRL_CONTEXT
)childContext
);
615 static void *CRYPT_CollectionEnumCRL(PWINECRYPT_CERTSTORE store
, void *pPrev
)
617 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
620 TRACE("(%p, %p)\n", store
, pPrev
);
622 EnterCriticalSection(&cs
->cs
);
625 PWINE_STORE_LIST_ENTRY storeEntry
=
626 *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(pPrev
,
627 sizeof(CRL_CONTEXT
));
629 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
630 offsetof(WINECRYPT_CERTSTORE
, crls
), pCRLInterface
, pPrev
,
631 sizeof(CRL_CONTEXT
));
635 if (!list_empty(&cs
->stores
))
637 PWINE_STORE_LIST_ENTRY storeEntry
= LIST_ENTRY(cs
->stores
.next
,
638 WINE_STORE_LIST_ENTRY
, entry
);
640 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
641 offsetof(WINECRYPT_CERTSTORE
, crls
), pCRLInterface
, NULL
,
642 sizeof(CRL_CONTEXT
));
646 SetLastError(CRYPT_E_NOT_FOUND
);
650 LeaveCriticalSection(&cs
->cs
);
652 ((PCRL_CONTEXT
)ret
)->hCertStore
= store
;
653 TRACE("returning %p\n", ret
);
657 static BOOL
CRYPT_CollectionDeleteCRL(PWINECRYPT_CERTSTORE store
,
662 TRACE("(%p, %p)\n", store
, pCrlContext
);
664 ret
= CertDeleteCRLFromStore((PCCRL_CONTEXT
)
665 Context_GetLinkedContext(pCrlContext
, sizeof(CRL_CONTEXT
)));
669 static WINECRYPT_CERTSTORE
*CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv
,
670 DWORD dwFlags
, const void *pvPara
)
672 PWINE_COLLECTIONSTORE store
;
674 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
676 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
681 store
= CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE
));
684 memset(store
, 0, sizeof(WINE_COLLECTIONSTORE
));
685 CRYPT_InitStore(&store
->hdr
, hCryptProv
, dwFlags
,
686 StoreTypeCollection
);
687 store
->hdr
.closeStore
= CRYPT_CollectionCloseStore
;
688 store
->hdr
.certs
.addContext
= CRYPT_CollectionAddCert
;
689 store
->hdr
.certs
.enumContext
= CRYPT_CollectionEnumCert
;
690 store
->hdr
.certs
.deleteContext
= CRYPT_CollectionDeleteCert
;
691 store
->hdr
.crls
.addContext
= CRYPT_CollectionAddCRL
;
692 store
->hdr
.crls
.enumContext
= CRYPT_CollectionEnumCRL
;
693 store
->hdr
.crls
.deleteContext
= CRYPT_CollectionDeleteCRL
;
694 InitializeCriticalSection(&store
->cs
);
695 store
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PWINE_COLLECTIONSTORE->cs");
696 list_init(&store
->stores
);
699 return (PWINECRYPT_CERTSTORE
)store
;
702 static void WINAPI
CRYPT_ProvCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
704 PWINE_PROVIDERSTORE store
= (PWINE_PROVIDERSTORE
)hCertStore
;
706 TRACE("(%p, %08x)\n", store
, dwFlags
);
708 if (store
->provCloseStore
)
709 store
->provCloseStore(store
->hStoreProv
, dwFlags
);
710 if (!(store
->dwStoreProvFlags
& CERT_STORE_PROV_EXTERNAL_FLAG
))
711 CertCloseStore(store
->memStore
, dwFlags
);
712 CRYPT_FreeStore((PWINECRYPT_CERTSTORE
)store
);
715 static BOOL
CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
716 void *toReplace
, const void **ppStoreContext
)
718 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
721 TRACE("(%p, %p, %p, %p)\n", store
, cert
, toReplace
, ppStoreContext
);
724 ret
= ps
->memStore
->certs
.addContext(ps
->memStore
, cert
, toReplace
,
725 (const void **)ppStoreContext
);
729 if (ps
->provWriteCert
)
730 ret
= ps
->provWriteCert(ps
->hStoreProv
, (PCCERT_CONTEXT
)cert
,
731 CERT_STORE_PROV_WRITE_ADD_FLAG
);
733 ret
= ps
->memStore
->certs
.addContext(ps
->memStore
, cert
, NULL
,
734 (const void **)ppStoreContext
);
736 /* dirty trick: replace the returned context's hCertStore with
740 (*(PCERT_CONTEXT
*)ppStoreContext
)->hCertStore
= store
;
744 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
746 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
749 ret
= ps
->memStore
->certs
.enumContext(ps
->memStore
, pPrev
);
752 /* same dirty trick: replace the returned context's hCertStore with
755 ((PCERT_CONTEXT
)ret
)->hCertStore
= store
;
760 static BOOL
CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store
, void *cert
)
762 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
765 TRACE("(%p, %p)\n", store
, cert
);
767 if (ps
->provDeleteCert
)
768 ret
= ps
->provDeleteCert(ps
->hStoreProv
, cert
, 0);
770 ret
= ps
->memStore
->certs
.deleteContext(ps
->memStore
, cert
);
774 static BOOL
CRYPT_ProvAddCRL(PWINECRYPT_CERTSTORE store
, void *crl
,
775 void *toReplace
, const void **ppStoreContext
)
777 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
780 TRACE("(%p, %p, %p, %p)\n", store
, crl
, toReplace
, ppStoreContext
);
783 ret
= ps
->memStore
->crls
.addContext(ps
->memStore
, crl
, toReplace
,
784 (const void **)ppStoreContext
);
787 if (ps
->hdr
.dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
789 SetLastError(ERROR_ACCESS_DENIED
);
795 if (ps
->provWriteCrl
)
796 ret
= ps
->provWriteCrl(ps
->hStoreProv
, (PCCRL_CONTEXT
)crl
,
797 CERT_STORE_PROV_WRITE_ADD_FLAG
);
799 ret
= ps
->memStore
->crls
.addContext(ps
->memStore
, crl
, NULL
,
800 (const void **)ppStoreContext
);
803 /* dirty trick: replace the returned context's hCertStore with
807 (*(PCRL_CONTEXT
*)ppStoreContext
)->hCertStore
= store
;
811 static void *CRYPT_ProvEnumCRL(PWINECRYPT_CERTSTORE store
, void *pPrev
)
813 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
816 ret
= ps
->memStore
->crls
.enumContext(ps
->memStore
, pPrev
);
819 /* same dirty trick: replace the returned context's hCertStore with
822 ((PCERT_CONTEXT
)ret
)->hCertStore
= store
;
827 static BOOL
CRYPT_ProvDeleteCRL(PWINECRYPT_CERTSTORE store
, void *crl
)
829 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
832 TRACE("(%p, %p)\n", store
, crl
);
834 if (ps
->provDeleteCrl
)
835 ret
= ps
->provDeleteCrl(ps
->hStoreProv
, crl
, 0);
837 ret
= ps
->memStore
->crls
.deleteContext(ps
->memStore
, crl
);
841 static BOOL WINAPI
CRYPT_ProvControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
842 DWORD dwCtrlType
, void const *pvCtrlPara
)
844 PWINE_PROVIDERSTORE store
= (PWINE_PROVIDERSTORE
)hCertStore
;
847 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
850 if (store
->provControl
)
851 ret
= store
->provControl(store
->hStoreProv
, dwFlags
, dwCtrlType
,
856 static PWINECRYPT_CERTSTORE
CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv
,
857 DWORD dwFlags
, PWINECRYPT_CERTSTORE memStore
, const CERT_STORE_PROV_INFO
*pProvInfo
)
859 PWINE_PROVIDERSTORE ret
= (PWINE_PROVIDERSTORE
)CryptMemAlloc(
860 sizeof(WINE_PROVIDERSTORE
));
864 CRYPT_InitStore(&ret
->hdr
, hCryptProv
, dwFlags
,
866 ret
->dwStoreProvFlags
= pProvInfo
->dwStoreProvFlags
;
867 if (ret
->dwStoreProvFlags
& CERT_STORE_PROV_EXTERNAL_FLAG
)
869 CertCloseStore(memStore
, 0);
870 ret
->memStore
= NULL
;
873 ret
->memStore
= memStore
;
874 ret
->hStoreProv
= pProvInfo
->hStoreProv
;
875 ret
->hdr
.closeStore
= CRYPT_ProvCloseStore
;
876 ret
->hdr
.certs
.addContext
= CRYPT_ProvAddCert
;
877 ret
->hdr
.certs
.enumContext
= CRYPT_ProvEnumCert
;
878 ret
->hdr
.certs
.deleteContext
= CRYPT_ProvDeleteCert
;
879 ret
->hdr
.crls
.addContext
= CRYPT_ProvAddCRL
;
880 ret
->hdr
.crls
.enumContext
= CRYPT_ProvEnumCRL
;
881 ret
->hdr
.crls
.deleteContext
= CRYPT_ProvDeleteCRL
;
882 ret
->hdr
.control
= CRYPT_ProvControl
;
883 if (pProvInfo
->cStoreProvFunc
> CERT_STORE_PROV_CLOSE_FUNC
)
884 ret
->provCloseStore
=
885 pProvInfo
->rgpvStoreProvFunc
[CERT_STORE_PROV_CLOSE_FUNC
];
887 ret
->provCloseStore
= NULL
;
888 if (pProvInfo
->cStoreProvFunc
>
889 CERT_STORE_PROV_WRITE_CERT_FUNC
)
890 ret
->provWriteCert
= pProvInfo
->rgpvStoreProvFunc
[
891 CERT_STORE_PROV_WRITE_CERT_FUNC
];
893 ret
->provWriteCert
= NULL
;
894 if (pProvInfo
->cStoreProvFunc
>
895 CERT_STORE_PROV_DELETE_CERT_FUNC
)
896 ret
->provDeleteCert
= pProvInfo
->rgpvStoreProvFunc
[
897 CERT_STORE_PROV_DELETE_CERT_FUNC
];
899 ret
->provDeleteCert
= NULL
;
900 if (pProvInfo
->cStoreProvFunc
>
901 CERT_STORE_PROV_WRITE_CRL_FUNC
)
902 ret
->provWriteCrl
= pProvInfo
->rgpvStoreProvFunc
[
903 CERT_STORE_PROV_WRITE_CRL_FUNC
];
905 ret
->provWriteCert
= NULL
;
906 if (pProvInfo
->cStoreProvFunc
>
907 CERT_STORE_PROV_DELETE_CRL_FUNC
)
908 ret
->provDeleteCrl
= pProvInfo
->rgpvStoreProvFunc
[
909 CERT_STORE_PROV_DELETE_CRL_FUNC
];
911 ret
->provDeleteCert
= NULL
;
912 if (pProvInfo
->cStoreProvFunc
>
913 CERT_STORE_PROV_CONTROL_FUNC
)
914 ret
->provControl
= pProvInfo
->rgpvStoreProvFunc
[
915 CERT_STORE_PROV_CONTROL_FUNC
];
917 ret
->provControl
= NULL
;
919 return (PWINECRYPT_CERTSTORE
)ret
;
922 static PWINECRYPT_CERTSTORE
CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider
,
923 DWORD dwEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
, const void *pvPara
)
925 static HCRYPTOIDFUNCSET set
= NULL
;
926 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc
;
927 HCRYPTOIDFUNCADDR hFunc
;
928 PWINECRYPT_CERTSTORE ret
= NULL
;
931 set
= CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC
, 0);
932 CryptGetOIDFunctionAddress(set
, dwEncodingType
, lpszStoreProvider
, 0,
933 (void **)&provOpenFunc
, &hFunc
);
936 CERT_STORE_PROV_INFO provInfo
= { 0 };
938 provInfo
.cbSize
= sizeof(provInfo
);
939 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
940 provOpenFunc(lpszStoreProvider
, dwEncodingType
, hCryptProv
,
941 dwFlags
, pvPara
, NULL
, &provInfo
);
946 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
947 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
950 if (provOpenFunc(lpszStoreProvider
, dwEncodingType
, hCryptProv
,
951 dwFlags
, pvPara
, memStore
, &provInfo
))
952 ret
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
955 CertCloseStore(memStore
, 0);
958 CryptFreeOIDFunctionAddress(hFunc
, 0);
961 SetLastError(ERROR_FILE_NOT_FOUND
);
965 static void CRYPT_HashToStr(const BYTE
*hash
, LPWSTR asciiHash
)
967 static const WCHAR fmt
[] = { '%','0','2','X',0 };
973 for (i
= 0; i
< 20; i
++)
974 wsprintfW(asciiHash
+ i
* 2, fmt
, hash
[i
]);
977 static const WCHAR CertsW
[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
979 static const WCHAR CRLsW
[] = { 'C','R','L','s',0 };
980 static const WCHAR CTLsW
[] = { 'C','T','L','s',0 };
981 static const WCHAR BlobW
[] = { 'B','l','o','b',0 };
983 static void CRYPT_RegReadSerializedFromReg(const WINE_REGSTOREINFO
*store
, HKEY key
,
988 WCHAR subKeyName
[MAX_PATH
];
991 DWORD size
= sizeof(subKeyName
) / sizeof(WCHAR
);
993 rc
= RegEnumKeyExW(key
, index
++, subKeyName
, &size
, NULL
, NULL
, NULL
,
999 rc
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
1005 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, NULL
, &size
);
1007 buf
= CryptMemAlloc(size
);
1010 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, buf
,
1014 const void *context
;
1017 TRACE("Adding cert with hash %s\n",
1018 debugstr_w(subKeyName
));
1019 context
= CRYPT_ReadSerializedElement(buf
, size
,
1020 contextType
, &addedType
);
1023 const WINE_CONTEXT_INTERFACE
*contextInterface
;
1028 case CERT_STORE_CERTIFICATE_CONTEXT
:
1029 contextInterface
= &gCertInterface
;
1031 case CERT_STORE_CRL_CONTEXT
:
1032 contextInterface
= &gCRLInterface
;
1034 case CERT_STORE_CTL_CONTEXT
:
1035 contextInterface
= &gCTLInterface
;
1038 contextInterface
= NULL
;
1040 if (contextInterface
)
1042 size
= sizeof(hash
);
1043 if (contextInterface
->getProp(context
,
1044 CERT_HASH_PROP_ID
, hash
, &size
))
1046 WCHAR asciiHash
[20 * 2 + 1];
1048 CRYPT_HashToStr(hash
, asciiHash
);
1049 TRACE("comparing %s\n",
1050 debugstr_w(asciiHash
));
1051 TRACE("with %s\n", debugstr_w(subKeyName
));
1052 if (!lstrcmpW(asciiHash
, subKeyName
))
1054 TRACE("hash matches, adding\n");
1055 contextInterface
->addContextToStore(
1056 store
->memStore
, context
,
1057 CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
1060 TRACE("hash doesn't match, ignoring\n");
1062 contextInterface
->free(context
);
1068 RegCloseKey(subKey
);
1070 /* Ignore intermediate errors, continue enumerating */
1076 static void CRYPT_RegReadFromReg(const WINE_REGSTOREINFO
*store
)
1078 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
1079 static const DWORD contextFlags
[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG
,
1080 CERT_STORE_CRL_CONTEXT_FLAG
, CERT_STORE_CTL_CONTEXT_FLAG
};
1083 for (i
= 0; i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
1088 rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0, KEY_READ
, NULL
,
1092 CRYPT_RegReadSerializedFromReg(store
, key
, contextFlags
[i
]);
1098 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1099 static BOOL
CRYPT_WriteSerializedToReg(HKEY key
, const BYTE
*hash
, const BYTE
*buf
,
1102 WCHAR asciiHash
[20 * 2 + 1];
1107 CRYPT_HashToStr(hash
, asciiHash
);
1108 rc
= RegCreateKeyExW(key
, asciiHash
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
,
1112 rc
= RegSetValueExW(subKey
, BlobW
, 0, REG_BINARY
, buf
, len
);
1113 RegCloseKey(subKey
);
1125 static BOOL
CRYPT_SerializeContextsToReg(HKEY key
,
1126 const WINE_CONTEXT_INTERFACE
*contextInterface
, HCERTSTORE memStore
)
1128 const void *context
= NULL
;
1132 context
= contextInterface
->enumContextsInStore(memStore
, context
);
1136 DWORD hashSize
= sizeof(hash
);
1138 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
, hash
,
1145 ret
= contextInterface
->serialize(context
, 0, NULL
, &size
);
1147 buf
= CryptMemAlloc(size
);
1150 ret
= contextInterface
->serialize(context
, 0, buf
, &size
);
1152 ret
= CRYPT_WriteSerializedToReg(key
, hash
, buf
, size
);
1159 } while (ret
&& context
!= NULL
);
1161 contextInterface
->free(context
);
1165 static BOOL
CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store
)
1167 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
1168 static const WINE_CONTEXT_INTERFACE
* const interfaces
[] = { &gCertInterface
,
1169 &gCRLInterface
, &gCTLInterface
};
1170 struct list
*listToDelete
[] = { &store
->certsToDelete
, &store
->crlsToDelete
,
1175 for (i
= 0; ret
&& i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
1178 LONG rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0,
1179 KEY_ALL_ACCESS
, NULL
, &key
, NULL
);
1183 if (listToDelete
[i
])
1185 PWINE_HASH_TO_DELETE toDelete
, next
;
1186 WCHAR asciiHash
[20 * 2 + 1];
1188 EnterCriticalSection(&store
->cs
);
1189 LIST_FOR_EACH_ENTRY_SAFE(toDelete
, next
, listToDelete
[i
],
1190 WINE_HASH_TO_DELETE
, entry
)
1194 CRYPT_HashToStr(toDelete
->hash
, asciiHash
);
1195 TRACE("Removing %s\n", debugstr_w(asciiHash
));
1196 rc
= RegDeleteKeyW(key
, asciiHash
);
1197 if (rc
!= ERROR_SUCCESS
&& rc
!= ERROR_FILE_NOT_FOUND
)
1202 list_remove(&toDelete
->entry
);
1203 CryptMemFree(toDelete
);
1205 LeaveCriticalSection(&store
->cs
);
1207 ret
= CRYPT_SerializeContextsToReg(key
, interfaces
[i
],
1220 /* If force is true or the registry store is dirty, writes the contents of the
1221 * store to the registry.
1223 static BOOL
CRYPT_RegFlushStore(PWINE_REGSTOREINFO store
, BOOL force
)
1227 TRACE("(%p, %d)\n", store
, force
);
1229 if (store
->dirty
|| force
)
1230 ret
= CRYPT_RegWriteToReg(store
);
1236 static void WINAPI
CRYPT_RegCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
1238 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1240 TRACE("(%p, %08x)\n", store
, dwFlags
);
1242 FIXME("Unimplemented flags: %08x\n", dwFlags
);
1244 CRYPT_RegFlushStore(store
, FALSE
);
1245 RegCloseKey(store
->key
);
1246 store
->cs
.DebugInfo
->Spare
[0] = 0;
1247 DeleteCriticalSection(&store
->cs
);
1248 CryptMemFree(store
);
1251 static BOOL WINAPI
CRYPT_RegWriteContext(PWINE_REGSTOREINFO store
,
1252 const void *context
, DWORD dwFlags
)
1256 if (dwFlags
& CERT_STORE_PROV_WRITE_ADD_FLAG
)
1258 store
->dirty
= TRUE
;
1266 static BOOL
CRYPT_RegDeleteContext(PWINE_REGSTOREINFO store
,
1267 struct list
*deleteList
, const void *context
,
1268 PCWINE_CONTEXT_INTERFACE contextInterface
)
1272 if (store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
1274 SetLastError(ERROR_ACCESS_DENIED
);
1279 PWINE_HASH_TO_DELETE toDelete
=
1280 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE
));
1284 DWORD size
= sizeof(toDelete
->hash
);
1286 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
,
1287 toDelete
->hash
, &size
);
1290 EnterCriticalSection(&store
->cs
);
1291 list_add_tail(deleteList
, &toDelete
->entry
);
1292 LeaveCriticalSection(&store
->cs
);
1296 CryptMemFree(toDelete
);
1303 store
->dirty
= TRUE
;
1308 static BOOL WINAPI
CRYPT_RegWriteCert(HCERTSTORE hCertStore
,
1309 PCCERT_CONTEXT cert
, DWORD dwFlags
)
1311 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1313 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
1315 return CRYPT_RegWriteContext(store
, cert
, dwFlags
);
1318 static BOOL WINAPI
CRYPT_RegDeleteCert(HCERTSTORE hCertStore
,
1319 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
1321 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1323 TRACE("(%p, %p, %08x)\n", store
, pCertContext
, dwFlags
);
1325 return CRYPT_RegDeleteContext(store
, &store
->certsToDelete
, pCertContext
,
1329 static BOOL WINAPI
CRYPT_RegWriteCRL(HCERTSTORE hCertStore
,
1330 PCCRL_CONTEXT crl
, DWORD dwFlags
)
1332 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1334 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
1336 return CRYPT_RegWriteContext(store
, crl
, dwFlags
);
1339 static BOOL WINAPI
CRYPT_RegDeleteCRL(HCERTSTORE hCertStore
,
1340 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
1342 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1344 TRACE("(%p, %p, %08x)\n", store
, pCrlContext
, dwFlags
);
1346 return CRYPT_RegDeleteContext(store
, &store
->crlsToDelete
, pCrlContext
,
1350 static BOOL WINAPI
CRYPT_RegControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
1351 DWORD dwCtrlType
, void const *pvCtrlPara
)
1353 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1356 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
1361 case CERT_STORE_CTRL_RESYNC
:
1362 CRYPT_RegFlushStore(store
, FALSE
);
1363 CRYPT_MemEmptyStore((PWINE_MEMSTORE
)store
->memStore
);
1364 CRYPT_RegReadFromReg(store
);
1367 case CERT_STORE_CTRL_COMMIT
:
1368 ret
= CRYPT_RegFlushStore(store
,
1369 dwFlags
& CERT_STORE_CTRL_COMMIT_FORCE_FLAG
);
1372 FIXME("%d: stub\n", dwCtrlType
);
1378 static void *regProvFuncs
[] = {
1379 CRYPT_RegCloseStore
,
1380 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
1382 CRYPT_RegDeleteCert
,
1383 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1384 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
1387 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1388 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
1389 NULL
, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1390 NULL
, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1391 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1395 static WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
,
1396 DWORD dwFlags
, const void *pvPara
)
1398 PWINECRYPT_CERTSTORE store
= NULL
;
1400 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
1402 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
1404 DWORD rc
= RegDeleteTreeW((HKEY
)pvPara
, CertsW
);
1406 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
1407 rc
= RegDeleteTreeW((HKEY
)pvPara
, CRLsW
);
1408 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
1409 rc
= RegDeleteTreeW((HKEY
)pvPara
, CTLsW
);
1410 if (rc
== ERROR_NO_MORE_ITEMS
)
1418 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
1419 GetCurrentProcess(), (LPHANDLE
)&key
,
1420 dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
: KEY_ALL_ACCESS
,
1423 PWINECRYPT_CERTSTORE memStore
;
1425 memStore
= CRYPT_MemOpenStore(hCryptProv
, dwFlags
, NULL
);
1428 PWINE_REGSTOREINFO regInfo
= CryptMemAlloc(
1429 sizeof(WINE_REGSTOREINFO
));
1433 CERT_STORE_PROV_INFO provInfo
= { 0 };
1435 regInfo
->dwOpenFlags
= dwFlags
;
1436 regInfo
->cryptProv
= hCryptProv
;
1437 regInfo
->memStore
= memStore
;
1439 InitializeCriticalSection(®Info
->cs
);
1440 regInfo
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PWINE_REGSTOREINFO->cs");
1441 list_init(®Info
->certsToDelete
);
1442 list_init(®Info
->crlsToDelete
);
1443 CRYPT_RegReadFromReg(regInfo
);
1444 regInfo
->dirty
= FALSE
;
1445 provInfo
.cbSize
= sizeof(provInfo
);
1446 provInfo
.cStoreProvFunc
= sizeof(regProvFuncs
) /
1447 sizeof(regProvFuncs
[0]);
1448 provInfo
.rgpvStoreProvFunc
= regProvFuncs
;
1449 provInfo
.hStoreProv
= regInfo
;
1450 store
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
1456 TRACE("returning %p\n", store
);
1460 /* FIXME: this isn't complete for the Root store, in which the top-level
1461 * self-signed CA certs reside. Adding a cert to the Root store should present
1462 * the user with a dialog indicating the consequences of doing so, and asking
1463 * the user to confirm whether the cert should be added.
1465 static PWINECRYPT_CERTSTORE
CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv
,
1466 DWORD dwFlags
, const void *pvPara
)
1468 static const WCHAR fmt
[] = { '%','s','\\','%','s',0 };
1469 LPCWSTR storeName
= (LPCWSTR
)pvPara
;
1471 PWINECRYPT_CERTSTORE store
= NULL
;
1476 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
1477 debugstr_w((LPCWSTR
)pvPara
));
1481 SetLastError(E_INVALIDARG
);
1486 switch (dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
)
1488 case CERT_SYSTEM_STORE_LOCAL_MACHINE
:
1489 root
= HKEY_LOCAL_MACHINE
;
1490 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
1492 case CERT_SYSTEM_STORE_CURRENT_USER
:
1493 root
= HKEY_CURRENT_USER
;
1494 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
1496 case CERT_SYSTEM_STORE_CURRENT_SERVICE
:
1497 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1498 * SystemCertificates
1500 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1501 debugstr_w(storeName
));
1503 case CERT_SYSTEM_STORE_SERVICES
:
1504 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1505 * SystemCertificates
1507 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1508 debugstr_w(storeName
));
1510 case CERT_SYSTEM_STORE_USERS
:
1511 /* hku\user sid\Software\Microsoft\SystemCertificates */
1512 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1513 debugstr_w(storeName
));
1515 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY
:
1516 root
= HKEY_CURRENT_USER
;
1517 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
1519 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY
:
1520 root
= HKEY_LOCAL_MACHINE
;
1521 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
1523 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE
:
1524 /* hklm\Software\Microsoft\EnterpriseCertificates */
1525 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1526 debugstr_w(storeName
));
1529 SetLastError(E_INVALIDARG
);
1533 storePath
= CryptMemAlloc((lstrlenW(base
) + lstrlenW(storeName
) + 2) *
1539 REGSAM sam
= dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
:
1542 wsprintfW(storePath
, fmt
, base
, storeName
);
1543 if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
1544 rc
= RegOpenKeyExW(root
, storePath
, 0, sam
, &key
);
1549 rc
= RegCreateKeyExW(root
, storePath
, 0, NULL
, 0, sam
, NULL
,
1551 if (!rc
&& dwFlags
& CERT_STORE_CREATE_NEW_FLAG
&&
1552 disp
== REG_OPENED_EXISTING_KEY
)
1555 rc
= ERROR_FILE_EXISTS
;
1560 store
= CRYPT_RegOpenStore(hCryptProv
, dwFlags
, key
);
1565 CryptMemFree(storePath
);
1570 static PWINECRYPT_CERTSTORE
CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv
,
1571 DWORD dwFlags
, const void *pvPara
)
1574 PWINECRYPT_CERTSTORE ret
= NULL
;
1576 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
1577 debugstr_a((LPCSTR
)pvPara
));
1581 SetLastError(ERROR_FILE_NOT_FOUND
);
1584 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
1587 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
1591 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
1592 ret
= CRYPT_SysRegOpenStoreW(hCryptProv
, dwFlags
, storeName
);
1593 CryptMemFree(storeName
);
1599 static PWINECRYPT_CERTSTORE
CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv
,
1600 DWORD dwFlags
, const void *pvPara
)
1602 HCERTSTORE store
= 0;
1605 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
1606 debugstr_w((LPCWSTR
)pvPara
));
1610 SetLastError(ERROR_FILE_NOT_FOUND
);
1613 /* This returns a different error than system registry stores if the
1614 * location is invalid.
1616 switch (dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
)
1618 case CERT_SYSTEM_STORE_LOCAL_MACHINE
:
1619 case CERT_SYSTEM_STORE_CURRENT_USER
:
1620 case CERT_SYSTEM_STORE_CURRENT_SERVICE
:
1621 case CERT_SYSTEM_STORE_SERVICES
:
1622 case CERT_SYSTEM_STORE_USERS
:
1623 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY
:
1624 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY
:
1625 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE
:
1629 SetLastError(ERROR_FILE_NOT_FOUND
);
1634 HCERTSTORE regStore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W
,
1635 0, hCryptProv
, dwFlags
, pvPara
);
1639 store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
1640 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1641 CertAddStoreToCollection(store
, regStore
,
1642 dwFlags
& CERT_STORE_READONLY_FLAG
? 0 :
1643 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1644 CertCloseStore(regStore
, 0);
1645 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1648 if ((dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
) ==
1649 CERT_SYSTEM_STORE_CURRENT_USER
)
1651 dwFlags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
1652 dwFlags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
1653 regStore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W
, 0,
1654 hCryptProv
, dwFlags
, pvPara
);
1657 CertAddStoreToCollection(store
, regStore
,
1658 dwFlags
& CERT_STORE_READONLY_FLAG
? 0 :
1659 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1660 CertCloseStore(regStore
, 0);
1665 return (PWINECRYPT_CERTSTORE
)store
;
1668 static PWINECRYPT_CERTSTORE
CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv
,
1669 DWORD dwFlags
, const void *pvPara
)
1672 PWINECRYPT_CERTSTORE ret
= NULL
;
1674 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
1675 debugstr_a((LPCSTR
)pvPara
));
1679 SetLastError(ERROR_FILE_NOT_FOUND
);
1682 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
1685 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
1689 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
1690 ret
= CRYPT_SysOpenStoreW(hCryptProv
, dwFlags
, storeName
);
1691 CryptMemFree(storeName
);
1697 static void WINAPI
CRYPT_FileCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
1699 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1701 TRACE("(%p, %08x)\n", store
, dwFlags
);
1703 CRYPT_WriteSerializedFile(store
->file
, store
->memStore
);
1704 CertCloseStore(store
->memStore
, dwFlags
);
1705 CloseHandle(store
->file
);
1706 CryptMemFree(store
);
1709 static BOOL WINAPI
CRYPT_FileWriteCert(HCERTSTORE hCertStore
,
1710 PCCERT_CONTEXT cert
, DWORD dwFlags
)
1712 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1714 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
1715 store
->dirty
= TRUE
;
1719 static BOOL WINAPI
CRYPT_FileDeleteCert(HCERTSTORE hCertStore
,
1720 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
1722 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1724 TRACE("(%p, %p, %08x)\n", hCertStore
, pCertContext
, dwFlags
);
1725 store
->dirty
= TRUE
;
1729 static BOOL WINAPI
CRYPT_FileWriteCRL(HCERTSTORE hCertStore
,
1730 PCCRL_CONTEXT crl
, DWORD dwFlags
)
1732 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1734 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
1735 store
->dirty
= TRUE
;
1739 static BOOL WINAPI
CRYPT_FileDeleteCRL(HCERTSTORE hCertStore
,
1740 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
1742 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1744 TRACE("(%p, %p, %08x)\n", hCertStore
, pCrlContext
, dwFlags
);
1745 store
->dirty
= TRUE
;
1749 static BOOL WINAPI
CRYPT_FileControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
1750 DWORD dwCtrlType
, void const *pvCtrlPara
)
1752 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
1755 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
1760 case CERT_STORE_CTRL_RESYNC
:
1761 CRYPT_MemEmptyStore((PWINE_MEMSTORE
)store
->memStore
);
1762 CRYPT_ReadSerializedFile(store
->file
, store
);
1765 case CERT_STORE_CTRL_COMMIT
:
1766 if (!(store
->dwOpenFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
1768 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1771 else if (store
->dirty
)
1772 ret
= CRYPT_WriteSerializedFile(store
->file
, store
->memStore
);
1777 FIXME("%d: stub\n", dwCtrlType
);
1783 static void *fileProvFuncs
[] = {
1784 CRYPT_FileCloseStore
,
1785 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
1786 CRYPT_FileWriteCert
,
1787 CRYPT_FileDeleteCert
,
1788 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1789 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
1791 CRYPT_FileDeleteCRL
,
1792 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1793 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
1794 NULL
, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1795 NULL
, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1796 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1800 static PWINECRYPT_CERTSTORE
CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
,
1801 DWORD dwFlags
, const void *pvPara
)
1803 PWINECRYPT_CERTSTORE store
= NULL
;
1804 HANDLE file
= (HANDLE
)pvPara
;
1806 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
1810 SetLastError(ERROR_INVALID_HANDLE
);
1813 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
1815 SetLastError(E_INVALIDARG
);
1818 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
1819 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
1821 SetLastError(E_INVALIDARG
);
1825 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
1826 GetCurrentProcess(), &file
, dwFlags
& CERT_STORE_READONLY_FLAG
?
1827 GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
, TRUE
, 0))
1829 PWINECRYPT_CERTSTORE memStore
;
1831 memStore
= CRYPT_MemOpenStore(hCryptProv
, dwFlags
, NULL
);
1834 if (CRYPT_ReadSerializedFile(file
, memStore
))
1836 PWINE_FILESTOREINFO info
= CryptMemAlloc(
1837 sizeof(WINE_FILESTOREINFO
));
1841 CERT_STORE_PROV_INFO provInfo
= { 0 };
1843 info
->dwOpenFlags
= dwFlags
;
1844 info
->cryptProv
= hCryptProv
;
1845 info
->memStore
= memStore
;
1847 info
->dirty
= FALSE
;
1848 provInfo
.cbSize
= sizeof(provInfo
);
1849 provInfo
.cStoreProvFunc
= sizeof(fileProvFuncs
) /
1850 sizeof(fileProvFuncs
[0]);
1851 provInfo
.rgpvStoreProvFunc
= fileProvFuncs
;
1852 provInfo
.hStoreProv
= info
;
1853 store
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
1859 TRACE("returning %p\n", store
);
1863 static PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
1864 DWORD dwFlags
, const void *pvPara
)
1866 HCERTSTORE store
= 0;
1867 LPCWSTR fileName
= (LPCWSTR
)pvPara
;
1868 DWORD access
, create
;
1871 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
, debugstr_w(fileName
));
1875 SetLastError(ERROR_PATH_NOT_FOUND
);
1879 access
= GENERIC_READ
;
1880 if (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
)
1881 access
|= GENERIC_WRITE
;
1882 if (dwFlags
& CERT_STORE_CREATE_NEW_FLAG
)
1883 create
= CREATE_NEW
;
1884 else if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
1885 create
= OPEN_EXISTING
;
1887 create
= OPEN_ALWAYS
;
1888 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
, create
,
1889 FILE_ATTRIBUTE_NORMAL
, NULL
);
1890 if (file
!= INVALID_HANDLE_VALUE
)
1892 /* FIXME: need to check whether it's a serialized store; if not, fall
1893 * back to a PKCS#7 signed message, then to a single serialized cert.
1895 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, hCryptProv
, dwFlags
,
1899 return (PWINECRYPT_CERTSTORE
)store
;
1902 static PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
1903 DWORD dwFlags
, const void *pvPara
)
1906 PWINECRYPT_CERTSTORE ret
= NULL
;
1908 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
1909 debugstr_a((LPCSTR
)pvPara
));
1913 SetLastError(ERROR_FILE_NOT_FOUND
);
1916 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
1919 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
1923 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
1924 ret
= CRYPT_FileNameOpenStoreW(hCryptProv
, dwFlags
, storeName
);
1925 CryptMemFree(storeName
);
1931 static void WINAPI
CRYPT_MsgCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
1933 PWINE_MSGSTOREINFO store
= (PWINE_MSGSTOREINFO
)hCertStore
;
1935 TRACE("(%p, %08x)\n", store
, dwFlags
);
1936 CertCloseStore(store
->memStore
, dwFlags
);
1937 CryptMsgClose(store
->msg
);
1938 CryptMemFree(store
);
1941 static void *msgProvFuncs
[] = {
1942 CRYPT_MsgCloseStore
,
1943 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
1944 NULL
, /* CERT_STORE_PROV_WRITE_CERT_FUNC */
1945 NULL
, /* CERT_STORE_PROV_DELETE_CERT_FUNC */
1946 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1947 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
1948 NULL
, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1949 NULL
, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1950 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1951 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
1952 NULL
, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1953 NULL
, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1954 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1955 NULL
, /* CERT_STORE_PROV_CONTROL_FUNC */
1958 static PWINECRYPT_CERTSTORE
CRYPT_MsgOpenStore(HCRYPTPROV hCryptProv
,
1959 DWORD dwFlags
, const void *pvPara
)
1961 PWINECRYPT_CERTSTORE store
= NULL
;
1962 HCRYPTMSG msg
= (HCRYPTMSG
)pvPara
;
1963 PWINECRYPT_CERTSTORE memStore
;
1965 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
1967 memStore
= CRYPT_MemOpenStore(hCryptProv
, dwFlags
, NULL
);
1971 DWORD size
, count
, i
;
1973 size
= sizeof(count
);
1974 ret
= CryptMsgGetParam(msg
, CMSG_CERT_COUNT_PARAM
, 0, &count
, &size
);
1975 for (i
= 0; ret
&& i
< count
; i
++)
1978 ret
= CryptMsgGetParam(msg
, CMSG_CERT_PARAM
, i
, NULL
, &size
);
1981 LPBYTE buf
= CryptMemAlloc(size
);
1985 ret
= CryptMsgGetParam(msg
, CMSG_CERT_PARAM
, i
, buf
, &size
);
1987 ret
= CertAddEncodedCertificateToStore(memStore
,
1988 X509_ASN_ENCODING
, buf
, size
, CERT_STORE_ADD_ALWAYS
,
1994 size
= sizeof(count
);
1995 ret
= CryptMsgGetParam(msg
, CMSG_CRL_COUNT_PARAM
, 0, &count
, &size
);
1996 for (i
= 0; ret
&& i
< count
; i
++)
1999 ret
= CryptMsgGetParam(msg
, CMSG_CRL_PARAM
, i
, NULL
, &size
);
2002 LPBYTE buf
= CryptMemAlloc(size
);
2006 ret
= CryptMsgGetParam(msg
, CMSG_CRL_PARAM
, i
, buf
, &size
);
2008 ret
= CertAddEncodedCRLToStore(memStore
,
2009 X509_ASN_ENCODING
, buf
, size
, CERT_STORE_ADD_ALWAYS
,
2017 PWINE_MSGSTOREINFO info
= CryptMemAlloc(sizeof(WINE_MSGSTOREINFO
));
2021 CERT_STORE_PROV_INFO provInfo
= { 0 };
2023 info
->dwOpenFlags
= dwFlags
;
2024 info
->cryptProv
= hCryptProv
;
2025 info
->memStore
= memStore
;
2026 info
->msg
= CryptMsgDuplicate(msg
);
2027 provInfo
.cbSize
= sizeof(provInfo
);
2028 provInfo
.cStoreProvFunc
= sizeof(msgProvFuncs
) /
2029 sizeof(msgProvFuncs
[0]);
2030 provInfo
.rgpvStoreProvFunc
= msgProvFuncs
;
2031 provInfo
.hStoreProv
= info
;
2032 store
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
2036 CertCloseStore(memStore
, 0);
2039 CertCloseStore(memStore
, 0);
2041 TRACE("returning %p\n", store
);
2045 static PWINECRYPT_CERTSTORE
CRYPT_PKCSOpenStore(HCRYPTPROV hCryptProv
,
2046 DWORD dwFlags
, const void *pvPara
)
2049 PWINECRYPT_CERTSTORE store
= NULL
;
2050 const CRYPT_DATA_BLOB
*data
= (const CRYPT_DATA_BLOB
*)pvPara
;
2053 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
2055 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
, 0, CMSG_SIGNED
, 0, NULL
,
2057 ret
= CryptMsgUpdate(msg
, data
->pbData
, data
->cbData
, TRUE
);
2061 msg
= CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING
, 0, 0, 0, NULL
, NULL
);
2062 ret
= CryptMsgUpdate(msg
, data
->pbData
, data
->cbData
, TRUE
);
2065 DWORD type
, size
= sizeof(type
);
2067 /* Only signed messages are allowed, check type */
2068 ret
= CryptMsgGetParam(msg
, CMSG_TYPE_PARAM
, 0, &type
, &size
);
2069 if (ret
&& type
!= CMSG_SIGNED
)
2071 SetLastError(CRYPT_E_INVALID_MSG_TYPE
);
2077 store
= CRYPT_MsgOpenStore(hCryptProv
, dwFlags
, msg
);
2079 TRACE("returning %p\n", store
);
2083 static PWINECRYPT_CERTSTORE
CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv
,
2084 DWORD dwFlags
, const void *pvPara
)
2086 if (dwFlags
& CERT_SYSTEM_STORE_RELOCATE_FLAG
)
2087 FIXME("(%ld, %08x, %p): stub\n", hCryptProv
, dwFlags
, pvPara
);
2089 FIXME("(%ld, %08x, %s): stub\n", hCryptProv
, dwFlags
,
2090 debugstr_w((LPCWSTR
)pvPara
));
2094 HCERTSTORE WINAPI
CertOpenStore(LPCSTR lpszStoreProvider
,
2095 DWORD dwMsgAndCertEncodingType
, HCRYPTPROV_LEGACY hCryptProv
, DWORD dwFlags
,
2098 WINECRYPT_CERTSTORE
*hcs
;
2099 StoreOpenFunc openFunc
= NULL
;
2101 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider
),
2102 dwMsgAndCertEncodingType
, hCryptProv
, dwFlags
, pvPara
);
2104 if (!HIWORD(lpszStoreProvider
))
2106 switch (LOWORD(lpszStoreProvider
))
2108 case (int)CERT_STORE_PROV_MSG
:
2109 openFunc
= CRYPT_MsgOpenStore
;
2111 case (int)CERT_STORE_PROV_MEMORY
:
2112 openFunc
= CRYPT_MemOpenStore
;
2114 case (int)CERT_STORE_PROV_FILE
:
2115 openFunc
= CRYPT_FileOpenStore
;
2117 case (int)CERT_STORE_PROV_PKCS7
:
2118 openFunc
= CRYPT_PKCSOpenStore
;
2120 case (int)CERT_STORE_PROV_REG
:
2121 openFunc
= CRYPT_RegOpenStore
;
2123 case (int)CERT_STORE_PROV_FILENAME_A
:
2124 openFunc
= CRYPT_FileNameOpenStoreA
;
2126 case (int)CERT_STORE_PROV_FILENAME_W
:
2127 openFunc
= CRYPT_FileNameOpenStoreW
;
2129 case (int)CERT_STORE_PROV_COLLECTION
:
2130 openFunc
= CRYPT_CollectionOpenStore
;
2132 case (int)CERT_STORE_PROV_SYSTEM_A
:
2133 openFunc
= CRYPT_SysOpenStoreA
;
2135 case (int)CERT_STORE_PROV_SYSTEM_W
:
2136 openFunc
= CRYPT_SysOpenStoreW
;
2138 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A
:
2139 openFunc
= CRYPT_SysRegOpenStoreA
;
2141 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W
:
2142 openFunc
= CRYPT_SysRegOpenStoreW
;
2144 case (int)CERT_STORE_PROV_PHYSICAL_W
:
2145 openFunc
= CRYPT_PhysOpenStoreW
;
2148 if (LOWORD(lpszStoreProvider
))
2149 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider
));
2152 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_MEMORY
))
2153 openFunc
= CRYPT_MemOpenStore
;
2154 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_FILENAME_W
))
2155 openFunc
= CRYPT_FileOpenStore
;
2156 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_SYSTEM
))
2157 openFunc
= CRYPT_SysOpenStoreW
;
2158 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_COLLECTION
))
2159 openFunc
= CRYPT_CollectionOpenStore
;
2160 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_SYSTEM_REGISTRY
))
2161 openFunc
= CRYPT_SysRegOpenStoreW
;
2164 FIXME("unimplemented type %s\n", lpszStoreProvider
);
2169 hcs
= CRYPT_ProvOpenStore(lpszStoreProvider
, dwMsgAndCertEncodingType
,
2170 hCryptProv
, dwFlags
, pvPara
);
2172 hcs
= openFunc(hCryptProv
, dwFlags
, pvPara
);
2173 return (HCERTSTORE
)hcs
;
2176 HCERTSTORE WINAPI
CertOpenSystemStoreA(HCRYPTPROV_LEGACY hProv
,
2177 LPCSTR szSubSystemProtocol
)
2179 if (!szSubSystemProtocol
)
2181 SetLastError(E_INVALIDARG
);
2184 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, hProv
,
2185 CERT_SYSTEM_STORE_CURRENT_USER
, szSubSystemProtocol
);
2188 HCERTSTORE WINAPI
CertOpenSystemStoreW(HCRYPTPROV_LEGACY hProv
,
2189 LPCWSTR szSubSystemProtocol
)
2191 if (!szSubSystemProtocol
)
2193 SetLastError(E_INVALIDARG
);
2196 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, hProv
,
2197 CERT_SYSTEM_STORE_CURRENT_USER
, szSubSystemProtocol
);
2200 BOOL WINAPI
CertSaveStore(HCERTSTORE hCertStore
, DWORD dwMsgAndCertEncodingType
,
2201 DWORD dwSaveAs
, DWORD dwSaveTo
, void* pvSaveToPara
, DWORD dwFlags
)
2203 FIXME("(%p,%d,%d,%d,%p,%08x) stub!\n", hCertStore
,
2204 dwMsgAndCertEncodingType
, dwSaveAs
, dwSaveTo
, pvSaveToPara
, dwFlags
);
2208 #define CertContext_CopyProperties(to, from) \
2209 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
2211 BOOL WINAPI
CertAddCertificateContextToStore(HCERTSTORE hCertStore
,
2212 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
2213 PCCERT_CONTEXT
*ppStoreContext
)
2215 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
2217 PCCERT_CONTEXT toAdd
= NULL
, existing
= NULL
;
2219 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCertContext
,
2220 dwAddDisposition
, ppStoreContext
);
2222 /* Weird case to pass a test */
2223 if (dwAddDisposition
== 0)
2225 SetLastError(STATUS_ACCESS_VIOLATION
);
2228 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
2231 DWORD size
= sizeof(hashToAdd
);
2233 ret
= CertGetCertificateContextProperty(pCertContext
, CERT_HASH_PROP_ID
,
2237 CRYPT_HASH_BLOB blob
= { sizeof(hashToAdd
), hashToAdd
};
2239 existing
= CertFindCertificateInStore(hCertStore
,
2240 pCertContext
->dwCertEncodingType
, 0, CERT_FIND_SHA1_HASH
, &blob
,
2245 switch (dwAddDisposition
)
2247 case CERT_STORE_ADD_ALWAYS
:
2248 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2250 case CERT_STORE_ADD_NEW
:
2253 TRACE("found matching certificate, not adding\n");
2254 SetLastError(CRYPT_E_EXISTS
);
2258 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2260 case CERT_STORE_ADD_REPLACE_EXISTING
:
2261 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2263 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
2264 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2266 CertContext_CopyProperties(toAdd
, existing
);
2268 case CERT_STORE_ADD_USE_EXISTING
:
2271 CertContext_CopyProperties(existing
, pCertContext
);
2272 *ppStoreContext
= CertDuplicateCertificateContext(existing
);
2275 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2278 FIXME("Unimplemented add disposition %d\n", dwAddDisposition
);
2285 ret
= store
->certs
.addContext(store
, (void *)toAdd
,
2286 (void *)existing
, (const void **)ppStoreContext
);
2287 else if (ppStoreContext
)
2288 *ppStoreContext
= CertDuplicateCertificateContext(toAdd
);
2289 CertFreeCertificateContext(toAdd
);
2291 CertFreeCertificateContext(existing
);
2293 TRACE("returning %d\n", ret
);
2297 PCCERT_CONTEXT WINAPI
CertEnumCertificatesInStore(HCERTSTORE hCertStore
,
2298 PCCERT_CONTEXT pPrev
)
2300 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2303 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
2306 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2309 ret
= (PCCERT_CONTEXT
)hcs
->certs
.enumContext(hcs
, (void *)pPrev
);
2313 BOOL WINAPI
CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext
)
2317 TRACE("(%p)\n", pCertContext
);
2321 else if (!pCertContext
->hCertStore
)
2324 CertFreeCertificateContext(pCertContext
);
2328 PWINECRYPT_CERTSTORE hcs
=
2329 (PWINECRYPT_CERTSTORE
)pCertContext
->hCertStore
;
2331 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2334 ret
= hcs
->certs
.deleteContext(hcs
, (void *)pCertContext
);
2335 CertFreeCertificateContext(pCertContext
);
2340 #define CrlContext_CopyProperties(to, from) \
2341 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
2343 BOOL WINAPI
CertAddCRLContextToStore(HCERTSTORE hCertStore
,
2344 PCCRL_CONTEXT pCrlContext
, DWORD dwAddDisposition
,
2345 PCCRL_CONTEXT
* ppStoreContext
)
2347 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
2349 PCCRL_CONTEXT toAdd
= NULL
, existing
= NULL
;
2351 TRACE("(%p, %p, %08x, %p)\n", hCertStore
, pCrlContext
,
2352 dwAddDisposition
, ppStoreContext
);
2354 /* Weird case to pass a test */
2355 if (dwAddDisposition
== 0)
2357 SetLastError(STATUS_ACCESS_VIOLATION
);
2360 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
2362 existing
= CertFindCRLInStore(hCertStore
, 0, 0, CRL_FIND_EXISTING
,
2366 switch (dwAddDisposition
)
2368 case CERT_STORE_ADD_ALWAYS
:
2369 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2371 case CERT_STORE_ADD_NEW
:
2374 TRACE("found matching CRL, not adding\n");
2375 SetLastError(CRYPT_E_EXISTS
);
2379 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2381 case CERT_STORE_ADD_NEWER
:
2384 LONG newer
= CompareFileTime(&existing
->pCrlInfo
->ThisUpdate
,
2385 &pCrlContext
->pCrlInfo
->ThisUpdate
);
2388 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2391 TRACE("existing CRL is newer, not adding\n");
2392 SetLastError(CRYPT_E_EXISTS
);
2397 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2399 case CERT_STORE_ADD_REPLACE_EXISTING
:
2400 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2402 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
2403 toAdd
= CertDuplicateCRLContext(pCrlContext
);
2405 CrlContext_CopyProperties(toAdd
, existing
);
2407 case CERT_STORE_ADD_USE_EXISTING
:
2409 CrlContext_CopyProperties(existing
, pCrlContext
);
2412 FIXME("Unimplemented add disposition %d\n", dwAddDisposition
);
2419 ret
= store
->crls
.addContext(store
, (void *)toAdd
,
2420 (void *)existing
, (const void **)ppStoreContext
);
2421 else if (ppStoreContext
)
2422 *ppStoreContext
= CertDuplicateCRLContext(toAdd
);
2423 CertFreeCRLContext(toAdd
);
2425 CertFreeCRLContext(existing
);
2427 TRACE("returning %d\n", ret
);
2431 BOOL WINAPI
CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext
)
2435 TRACE("(%p)\n", pCrlContext
);
2439 else if (!pCrlContext
->hCertStore
)
2442 CertFreeCRLContext(pCrlContext
);
2446 PWINECRYPT_CERTSTORE hcs
=
2447 (PWINECRYPT_CERTSTORE
)pCrlContext
->hCertStore
;
2449 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2452 ret
= hcs
->crls
.deleteContext(hcs
, (void *)pCrlContext
);
2453 CertFreeCRLContext(pCrlContext
);
2458 PCCRL_CONTEXT WINAPI
CertEnumCRLsInStore(HCERTSTORE hCertStore
,
2459 PCCRL_CONTEXT pPrev
)
2461 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2464 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
2467 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2470 ret
= (PCCRL_CONTEXT
)hcs
->crls
.enumContext(hcs
, (void *)pPrev
);
2474 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwCertEncodingType
,
2475 const BYTE
* pbCtlEncoded
, DWORD cbCtlEncoded
)
2477 FIXME("(%08x, %p, %08x): stub\n", dwCertEncodingType
, pbCtlEncoded
,
2482 BOOL WINAPI
CertAddEncodedCTLToStore(HCERTSTORE hCertStore
,
2483 DWORD dwMsgAndCertEncodingType
, const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
,
2484 DWORD dwAddDisposition
, PCCTL_CONTEXT
*ppCtlContext
)
2486 FIXME("(%p, %08x, %p, %d, %08x, %p): stub\n", hCertStore
,
2487 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
2492 BOOL WINAPI
CertAddCTLContextToStore(HCERTSTORE hCertStore
,
2493 PCCTL_CONTEXT pCtlContext
, DWORD dwAddDisposition
,
2494 PCCTL_CONTEXT
* ppStoreContext
)
2496 FIXME("(%p, %p, %08x, %p): stub\n", hCertStore
, pCtlContext
,
2497 dwAddDisposition
, ppStoreContext
);
2501 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
2503 FIXME("(%p): stub\n", pCtlContext
);
2507 BOOL WINAPI
CertFreeCTLContext(PCCTL_CONTEXT pCtlContext
)
2509 FIXME("(%p): stub\n", pCtlContext
);
2513 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
2515 FIXME("(%p): stub\n", pCtlContext
);
2519 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
,
2520 PCCTL_CONTEXT pPrev
)
2522 FIXME("(%p, %p): stub\n", hCertStore
, pPrev
);
2526 HCERTSTORE WINAPI
CertDuplicateStore(HCERTSTORE hCertStore
)
2528 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2530 TRACE("(%p)\n", hCertStore
);
2532 if (hcs
&& hcs
->dwMagic
== WINE_CRYPTCERTSTORE_MAGIC
)
2533 InterlockedIncrement(&hcs
->ref
);
2537 BOOL WINAPI
CertCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
2539 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*) hCertStore
;
2541 TRACE("(%p, %08x)\n", hCertStore
, dwFlags
);
2546 if ( hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2549 if (InterlockedDecrement(&hcs
->ref
) == 0)
2551 TRACE("%p's ref count is 0, freeing\n", hcs
);
2553 if (!(hcs
->dwOpenFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
2554 CryptReleaseContext(hcs
->cryptProv
, 0);
2555 hcs
->closeStore(hcs
, dwFlags
);
2558 TRACE("%p's ref count is %d\n", hcs
, hcs
->ref
);
2562 BOOL WINAPI
CertControlStore(HCERTSTORE hCertStore
, DWORD dwFlags
,
2563 DWORD dwCtrlType
, void const *pvCtrlPara
)
2565 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2568 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
2573 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2578 ret
= hcs
->control(hCertStore
, dwFlags
, dwCtrlType
, pvCtrlPara
);
2585 BOOL WINAPI
CertGetStoreProperty(HCERTSTORE hCertStore
, DWORD dwPropId
,
2586 void *pvData
, DWORD
*pcbData
)
2588 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
2591 TRACE("(%p, %d, %p, %p)\n", hCertStore
, dwPropId
, pvData
, pcbData
);
2595 case CERT_ACCESS_STATE_PROP_ID
:
2598 *pcbData
= sizeof(DWORD
);
2601 else if (*pcbData
< sizeof(DWORD
))
2603 SetLastError(ERROR_MORE_DATA
);
2604 *pcbData
= sizeof(DWORD
);
2610 if (store
->type
!= StoreTypeMem
&&
2611 !(store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
))
2612 state
|= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG
;
2613 *(DWORD
*)pvData
= state
;
2618 if (store
->properties
)
2620 CRYPT_DATA_BLOB blob
;
2622 ret
= ContextPropertyList_FindProperty(store
->properties
, dwPropId
,
2627 *pcbData
= blob
.cbData
;
2628 else if (*pcbData
< blob
.cbData
)
2630 SetLastError(ERROR_MORE_DATA
);
2631 *pcbData
= blob
.cbData
;
2636 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
2637 *pcbData
= blob
.cbData
;
2641 SetLastError(CRYPT_E_NOT_FOUND
);
2644 SetLastError(CRYPT_E_NOT_FOUND
);
2649 BOOL WINAPI
CertSetStoreProperty(HCERTSTORE hCertStore
, DWORD dwPropId
,
2650 DWORD dwFlags
, const void *pvData
)
2652 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
2655 TRACE("(%p, %d, %08x, %p)\n", hCertStore
, dwPropId
, dwFlags
, pvData
);
2657 if (!store
->properties
)
2658 store
->properties
= ContextPropertyList_Create();
2661 case CERT_ACCESS_STATE_PROP_ID
:
2662 SetLastError(E_INVALIDARG
);
2667 const CRYPT_DATA_BLOB
*blob
= (const CRYPT_DATA_BLOB
*)pvData
;
2669 ret
= ContextPropertyList_SetProperty(store
->properties
, dwPropId
,
2670 blob
->pbData
, blob
->cbData
);
2674 ContextPropertyList_RemoveProperty(store
->properties
, dwPropId
);
2681 DWORD WINAPI
CertEnumCTLContextProperties(PCCTL_CONTEXT pCTLContext
,
2684 FIXME("(%p, %d): stub\n", pCTLContext
, dwPropId
);
2688 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
2689 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
2691 FIXME("(%p, %d, %p, %p): stub\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
2695 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
2696 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
2698 FIXME("(%p, %d, %08x, %p): stub\n", pCTLContext
, dwPropId
, dwFlags
,
2703 BOOL WINAPI
CertAddStoreToCollection(HCERTSTORE hCollectionStore
,
2704 HCERTSTORE hSiblingStore
, DWORD dwUpdateFlags
, DWORD dwPriority
)
2706 PWINE_COLLECTIONSTORE collection
= (PWINE_COLLECTIONSTORE
)hCollectionStore
;
2707 WINECRYPT_CERTSTORE
*sibling
= (WINECRYPT_CERTSTORE
*)hSiblingStore
;
2708 PWINE_STORE_LIST_ENTRY entry
;
2711 TRACE("(%p, %p, %08x, %d)\n", hCollectionStore
, hSiblingStore
,
2712 dwUpdateFlags
, dwPriority
);
2714 if (!collection
|| !sibling
)
2716 if (collection
->hdr
.dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2718 SetLastError(E_INVALIDARG
);
2721 if (collection
->hdr
.type
!= StoreTypeCollection
)
2723 SetLastError(E_INVALIDARG
);
2726 if (sibling
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2728 SetLastError(E_INVALIDARG
);
2732 entry
= CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY
));
2735 InterlockedIncrement(&sibling
->ref
);
2736 TRACE("sibling %p's ref count is %d\n", sibling
, sibling
->ref
);
2737 entry
->store
= sibling
;
2738 entry
->dwUpdateFlags
= dwUpdateFlags
;
2739 entry
->dwPriority
= dwPriority
;
2740 list_init(&entry
->entry
);
2741 TRACE("%p: adding %p, priority %d\n", collection
, entry
, dwPriority
);
2742 EnterCriticalSection(&collection
->cs
);
2745 PWINE_STORE_LIST_ENTRY cursor
;
2748 LIST_FOR_EACH_ENTRY(cursor
, &collection
->stores
,
2749 WINE_STORE_LIST_ENTRY
, entry
)
2751 if (cursor
->dwPriority
< dwPriority
)
2753 list_add_before(&cursor
->entry
, &entry
->entry
);
2759 list_add_tail(&collection
->stores
, &entry
->entry
);
2762 list_add_tail(&collection
->stores
, &entry
->entry
);
2763 LeaveCriticalSection(&collection
->cs
);
2771 void WINAPI
CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore
,
2772 HCERTSTORE hSiblingStore
)
2774 PWINE_COLLECTIONSTORE collection
= (PWINE_COLLECTIONSTORE
)hCollectionStore
;
2775 WINECRYPT_CERTSTORE
*sibling
= (WINECRYPT_CERTSTORE
*)hSiblingStore
;
2776 PWINE_STORE_LIST_ENTRY store
, next
;
2778 TRACE("(%p, %p)\n", hCollectionStore
, hSiblingStore
);
2780 if (!collection
|| !sibling
)
2782 if (collection
->hdr
.dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2784 SetLastError(E_INVALIDARG
);
2787 if (collection
->hdr
.type
!= StoreTypeCollection
)
2789 if (sibling
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2791 SetLastError(E_INVALIDARG
);
2794 EnterCriticalSection(&collection
->cs
);
2795 LIST_FOR_EACH_ENTRY_SAFE(store
, next
, &collection
->stores
,
2796 WINE_STORE_LIST_ENTRY
, entry
)
2798 if (store
->store
== sibling
)
2800 list_remove(&store
->entry
);
2801 CertCloseStore(store
->store
, 0);
2802 CryptMemFree(store
);
2806 LeaveCriticalSection(&collection
->cs
);
2809 static LONG
CRYPT_OpenParentStore(DWORD dwFlags
,
2810 void *pvSystemStoreLocationPara
, HKEY
*key
)
2815 TRACE("(%08x, %p)\n", dwFlags
, pvSystemStoreLocationPara
);
2817 switch (dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
)
2819 case CERT_SYSTEM_STORE_LOCAL_MACHINE
:
2820 root
= HKEY_LOCAL_MACHINE
;
2821 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
2823 case CERT_SYSTEM_STORE_CURRENT_USER
:
2824 root
= HKEY_CURRENT_USER
;
2825 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
2827 case CERT_SYSTEM_STORE_CURRENT_SERVICE
:
2828 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2829 * SystemCertificates
2831 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
2832 return ERROR_FILE_NOT_FOUND
;
2833 case CERT_SYSTEM_STORE_SERVICES
:
2834 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
2835 * SystemCertificates
2837 FIXME("CERT_SYSTEM_STORE_SERVICES\n");
2838 return ERROR_FILE_NOT_FOUND
;
2839 case CERT_SYSTEM_STORE_USERS
:
2840 /* hku\user sid\Software\Microsoft\SystemCertificates */
2841 FIXME("CERT_SYSTEM_STORE_USERS\n");
2842 return ERROR_FILE_NOT_FOUND
;
2843 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY
:
2844 root
= HKEY_CURRENT_USER
;
2845 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
2847 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY
:
2848 root
= HKEY_LOCAL_MACHINE
;
2849 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
2851 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE
:
2852 /* hklm\Software\Microsoft\EnterpriseCertificates */
2853 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
2854 return ERROR_FILE_NOT_FOUND
;
2856 return ERROR_FILE_NOT_FOUND
;
2859 return RegOpenKeyExW(root
, base
, 0, KEY_READ
, key
);
2862 BOOL WINAPI
CertEnumSystemStore(DWORD dwFlags
, void *pvSystemStoreLocationPara
,
2863 void *pvArg
, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum
)
2869 TRACE("(%08x, %p, %p, %p)\n", dwFlags
, pvSystemStoreLocationPara
, pvArg
,
2872 rc
= CRYPT_OpenParentStore(dwFlags
, pvArg
, &key
);
2876 CERT_SYSTEM_STORE_INFO info
= { sizeof(info
) };
2880 WCHAR name
[MAX_PATH
];
2881 DWORD size
= sizeof(name
) / sizeof(name
[0]);
2883 rc
= RegEnumKeyExW(key
, index
++, name
, &size
, NULL
, NULL
, NULL
,
2886 ret
= pfnEnum(name
, 0, &info
, NULL
, pvArg
);
2887 } while (ret
&& !rc
);
2888 if (ret
&& rc
!= ERROR_NO_MORE_ITEMS
)