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 * - As you can see in the stubs below, support for CRLs and CTLs is missing.
21 * Mostly this should be copy-paste work, and some code (e.g. extended
22 * properties) could be shared between them.
23 * - The concept of physical stores and locations isn't implemented. (This
24 * doesn't mean registry stores et al aren't implemented. See the PSDK for
25 * registering and enumerating physical stores and locations.)
26 * - Many flags, options and whatnot are unimplemented.
37 #include "wine/debug.h"
38 #include "wine/list.h"
40 #include "wine/exception.h"
41 #include "crypt32_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
45 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
47 static const WINE_CONTEXT_INTERFACE gCertInterface
= {
48 (CreateContextFunc
)CertCreateCertificateContext
,
49 (AddContextToStoreFunc
)CertAddCertificateContextToStore
,
50 (AddEncodedContextToStoreFunc
)CertAddEncodedCertificateToStore
,
51 (DuplicateContextFunc
)CertDuplicateCertificateContext
,
52 (EnumContextsInStoreFunc
)CertEnumCertificatesInStore
,
53 (GetContextPropertyFunc
)CertGetCertificateContextProperty
,
54 (SetContextPropertyFunc
)CertSetCertificateContextProperty
,
55 (SerializeElementFunc
)CertSerializeCertificateStoreElement
,
56 (FreeContextFunc
)CertFreeCertificateContext
,
57 (DeleteContextFunc
)CertDeleteCertificateFromStore
,
59 PCWINE_CONTEXT_INTERFACE pCertInterface
= &gCertInterface
;
61 static const WINE_CONTEXT_INTERFACE gCRLInterface
= {
62 (CreateContextFunc
)CertCreateCRLContext
,
63 (AddContextToStoreFunc
)CertAddCRLContextToStore
,
64 (AddEncodedContextToStoreFunc
)CertAddEncodedCRLToStore
,
65 (DuplicateContextFunc
)CertDuplicateCRLContext
,
66 (EnumContextsInStoreFunc
)CertEnumCRLsInStore
,
67 (GetContextPropertyFunc
)CertGetCRLContextProperty
,
68 (SetContextPropertyFunc
)CertSetCRLContextProperty
,
69 (SerializeElementFunc
)CertSerializeCRLStoreElement
,
70 (FreeContextFunc
)CertFreeCRLContext
,
71 (DeleteContextFunc
)CertDeleteCRLFromStore
,
73 PCWINE_CONTEXT_INTERFACE pCRLInterface
= &gCRLInterface
;
75 static const WINE_CONTEXT_INTERFACE gCTLInterface
= {
76 (CreateContextFunc
)CertCreateCTLContext
,
77 (AddContextToStoreFunc
)CertAddCTLContextToStore
,
78 (AddEncodedContextToStoreFunc
)CertAddEncodedCTLToStore
,
79 (DuplicateContextFunc
)CertDuplicateCTLContext
,
80 (EnumContextsInStoreFunc
)CertEnumCTLsInStore
,
81 (GetContextPropertyFunc
)CertGetCTLContextProperty
,
82 (SetContextPropertyFunc
)CertSetCTLContextProperty
,
83 (SerializeElementFunc
)CertSerializeCTLStoreElement
,
84 (FreeContextFunc
)CertFreeCTLContext
,
85 (DeleteContextFunc
)CertDeleteCTLFromStore
,
87 PCWINE_CONTEXT_INTERFACE pCTLInterface
= &gCTLInterface
;
89 struct WINE_CRYPTCERTSTORE
;
91 typedef struct WINE_CRYPTCERTSTORE
* (*StoreOpenFunc
)(HCRYPTPROV hCryptProv
,
92 DWORD dwFlags
, const void *pvPara
);
94 /* Called to enumerate the next context in a store. */
95 typedef void * (*EnumFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *pPrev
);
97 /* Called to add a context to a store. If toReplace is not NULL,
98 * context replaces toReplace in the store, and access checks should not be
99 * performed. Otherwise context is a new context, and it should only be
100 * added if the store allows it. If ppStoreContext is not NULL, the added
101 * context should be returned in *ppStoreContext.
103 typedef BOOL (*AddFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *context
,
104 void *toReplace
, const void **ppStoreContext
);
106 typedef BOOL (*DeleteFunc
)(struct WINE_CRYPTCERTSTORE
*store
, void *context
);
108 typedef struct _CONTEXT_STORE
111 EnumFunc enumContext
;
112 DeleteFunc deleteContext
;
113 } CONTEXT_STORE
, *PCONTEXT_STORE
;
115 typedef enum _CertStoreType
{
121 /* A cert store is polymorphic through the use of function pointers. A type
122 * is still needed to distinguish collection stores from other types.
123 * On the function pointers:
124 * - closeStore is called when the store's ref count becomes 0
125 * - control is optional, but should be implemented by any store that supports
128 typedef struct WINE_CRYPTCERTSTORE
133 HCRYPTPROV cryptProv
;
135 PFN_CERT_STORE_PROV_CLOSE closeStore
;
137 PFN_CERT_STORE_PROV_CONTROL control
; /* optional */
138 } WINECRYPT_CERTSTORE
, *PWINECRYPT_CERTSTORE
;
140 typedef struct _WINE_MEMSTORE
142 WINECRYPT_CERTSTORE hdr
;
143 struct ContextList
*certs
;
144 } WINE_MEMSTORE
, *PWINE_MEMSTORE
;
146 typedef struct _WINE_HASH_TO_DELETE
150 } WINE_HASH_TO_DELETE
, *PWINE_HASH_TO_DELETE
;
152 typedef struct _WINE_REGSTOREINFO
155 HCRYPTPROV cryptProv
;
156 PWINECRYPT_CERTSTORE memStore
;
160 struct list certsToDelete
;
161 } WINE_REGSTOREINFO
, *PWINE_REGSTOREINFO
;
163 typedef struct _WINE_STORE_LIST_ENTRY
165 PWINECRYPT_CERTSTORE store
;
169 } WINE_STORE_LIST_ENTRY
, *PWINE_STORE_LIST_ENTRY
;
171 typedef struct _WINE_COLLECTIONSTORE
173 WINECRYPT_CERTSTORE hdr
;
176 } WINE_COLLECTIONSTORE
, *PWINE_COLLECTIONSTORE
;
178 typedef struct _WINE_PROVIDERSTORE
180 WINECRYPT_CERTSTORE hdr
;
181 DWORD dwStoreProvFlags
;
182 PWINECRYPT_CERTSTORE memStore
;
183 HCERTSTOREPROV hStoreProv
;
184 PFN_CERT_STORE_PROV_CLOSE provCloseStore
;
185 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert
;
186 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert
;
187 PFN_CERT_STORE_PROV_CONTROL provControl
;
188 } WINE_PROVIDERSTORE
, *PWINE_PROVIDERSTORE
;
190 /* Internal version of CertGetCertificateContextProperty that gets properties
191 * directly from the context (or the context it's linked to, depending on its
192 * type.) Doesn't handle special-case properties, since they are handled by
193 * CertGetCertificateContextProperty, and are particular to the store in which
194 * the property exists (which is separate from the context.)
196 static BOOL WINAPI
CertContext_GetProperty(void *context
, DWORD dwPropId
,
197 void *pvData
, DWORD
*pcbData
);
199 /* Internal version of CertSetCertificateContextProperty that sets properties
200 * directly on the context (or the context it's linked to, depending on its
201 * type.) Doesn't handle special cases, since they're handled by
202 * CertSetCertificateContextProperty anyway.
204 static BOOL WINAPI
CertContext_SetProperty(void *context
, DWORD dwPropId
,
205 DWORD dwFlags
, const void *pvData
);
207 static void CRYPT_InitStore(WINECRYPT_CERTSTORE
*store
, HCRYPTPROV hCryptProv
,
208 DWORD dwFlags
, CertStoreType type
)
211 store
->dwMagic
= WINE_CRYPTCERTSTORE_MAGIC
;
215 hCryptProv
= CRYPT_GetDefaultProvider();
216 dwFlags
|= CERT_STORE_NO_CRYPT_RELEASE_FLAG
;
218 store
->cryptProv
= hCryptProv
;
219 store
->dwOpenFlags
= dwFlags
;
222 static BOOL
CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
223 void *toReplace
, const void **ppStoreContext
)
225 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
226 PCERT_CONTEXT context
;
228 TRACE("(%p, %p, %p, %p)\n", store
, cert
, toReplace
, ppStoreContext
);
230 context
= (PCERT_CONTEXT
)ContextList_Add(ms
->certs
, cert
, toReplace
);
233 context
->hCertStore
= store
;
236 CertDuplicateCertificateContext(context
);
238 return context
? TRUE
: FALSE
;
241 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
243 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
246 TRACE("(%p, %p)\n", store
, pPrev
);
248 ret
= ContextList_Enum(ms
->certs
, pPrev
);
250 SetLastError(CRYPT_E_NOT_FOUND
);
252 TRACE("returning %p\n", ret
);
256 static BOOL
CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store
, void *pCertContext
)
258 WINE_MEMSTORE
*ms
= (WINE_MEMSTORE
*)store
;
260 ContextList_Delete(ms
->certs
, pCertContext
);
264 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store
)
266 ContextList_Empty(store
->certs
);
269 static void WINAPI
CRYPT_MemCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
271 WINE_MEMSTORE
*store
= (WINE_MEMSTORE
*)hCertStore
;
273 TRACE("(%p, %08lx)\n", store
, dwFlags
);
275 FIXME("Unimplemented flags: %08lx\n", dwFlags
);
277 ContextList_Free(store
->certs
);
281 static WINECRYPT_CERTSTORE
*CRYPT_MemOpenStore(HCRYPTPROV hCryptProv
,
282 DWORD dwFlags
, const void *pvPara
)
284 PWINE_MEMSTORE store
;
286 TRACE("(%ld, %08lx, %p)\n", hCryptProv
, dwFlags
, pvPara
);
288 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
290 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
295 store
= CryptMemAlloc(sizeof(WINE_MEMSTORE
));
298 memset(store
, 0, sizeof(WINE_MEMSTORE
));
299 CRYPT_InitStore(&store
->hdr
, hCryptProv
, dwFlags
, StoreTypeMem
);
300 store
->hdr
.closeStore
= CRYPT_MemCloseStore
;
301 store
->hdr
.certs
.addContext
= CRYPT_MemAddCert
;
302 store
->hdr
.certs
.enumContext
= CRYPT_MemEnumCert
;
303 store
->hdr
.certs
.deleteContext
= CRYPT_MemDeleteCert
;
304 store
->hdr
.control
= NULL
;
305 store
->certs
= ContextList_Create(pCertInterface
,
306 sizeof(CERT_CONTEXT
));
309 return (PWINECRYPT_CERTSTORE
)store
;
312 static void WINAPI
CRYPT_CollectionCloseStore(HCERTSTORE store
, DWORD dwFlags
)
314 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
315 PWINE_STORE_LIST_ENTRY entry
, next
;
317 TRACE("(%p, %08lx)\n", store
, dwFlags
);
319 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
, &cs
->stores
, WINE_STORE_LIST_ENTRY
,
322 TRACE("closing %p\n", entry
);
323 CertCloseStore((HCERTSTORE
)entry
->store
, dwFlags
);
326 DeleteCriticalSection(&cs
->cs
);
330 static void *CRYPT_CollectionCreateContextFromChild(PWINE_COLLECTIONSTORE store
,
331 PWINE_STORE_LIST_ENTRY storeEntry
, void *child
, size_t contextSize
,
334 void *ret
= Context_CreateLinkContext(contextSize
, child
,
335 sizeof(PWINE_STORE_LIST_ENTRY
), addRef
);
338 *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(ret
, contextSize
)
344 static BOOL
CRYPT_CollectionAddContext(PWINE_COLLECTIONSTORE store
,
345 size_t contextStoreOffset
, void *context
, void *toReplace
, size_t contextSize
,
346 void **pChildContext
)
349 void *childContext
= NULL
;
350 PWINE_STORE_LIST_ENTRY storeEntry
= NULL
;
352 TRACE("(%p, %d, %p, %p, %d)\n", store
, contextStoreOffset
, context
,
353 toReplace
, contextSize
);
358 void *existingLinked
= Context_GetLinkedContext(toReplace
, contextSize
);
359 PCONTEXT_STORE contextStore
;
361 storeEntry
= *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(toReplace
,
363 contextStore
= (PCONTEXT_STORE
)((LPBYTE
)storeEntry
->store
+
365 ret
= contextStore
->addContext(storeEntry
->store
, context
,
366 existingLinked
, (const void **)&childContext
);
370 PWINE_STORE_LIST_ENTRY entry
, next
;
372 EnterCriticalSection(&store
->cs
);
373 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
, &store
->stores
,
374 WINE_STORE_LIST_ENTRY
, entry
)
376 if (entry
->dwUpdateFlags
& CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
)
378 PCONTEXT_STORE contextStore
= (PCONTEXT_STORE
)(
379 (LPBYTE
)entry
->store
+ contextStoreOffset
);
382 ret
= contextStore
->addContext(entry
->store
, context
, NULL
,
383 (const void **)&childContext
);
387 LeaveCriticalSection(&store
->cs
);
389 SetLastError(E_ACCESSDENIED
);
391 *pChildContext
= childContext
;
395 static BOOL
CRYPT_CollectionAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
396 void *toReplace
, const void **ppStoreContext
)
399 void *childContext
= NULL
;
400 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
402 ret
= CRYPT_CollectionAddContext(cs
, offsetof(WINECRYPT_CERTSTORE
, certs
),
403 cert
, toReplace
, sizeof(CERT_CONTEXT
), &childContext
);
404 if (ppStoreContext
&& childContext
)
406 PWINE_STORE_LIST_ENTRY storeEntry
= *(PWINE_STORE_LIST_ENTRY
*)
407 Context_GetExtra(childContext
, sizeof(CERT_CONTEXT
));
408 PCERT_CONTEXT context
=
409 CRYPT_CollectionCreateContextFromChild(cs
, storeEntry
, childContext
,
410 sizeof(CERT_CONTEXT
), TRUE
);
413 context
->hCertStore
= store
;
414 *ppStoreContext
= context
;
416 CertFreeCertificateContext((PCCERT_CONTEXT
)childContext
);
420 /* Advances a collection enumeration by one context, if possible, where
422 * - calling the current store's enumeration function once, and returning
423 * the enumerated context if one is returned
424 * - moving to the next store if the current store has no more items, and
425 * recursively calling itself to get the next item.
426 * Returns NULL if the collection contains no more items or on error.
427 * Assumes the collection store's lock is held.
429 static void *CRYPT_CollectionAdvanceEnum(PWINE_COLLECTIONSTORE store
,
430 PWINE_STORE_LIST_ENTRY storeEntry
, size_t contextStoreOffset
,
431 PCWINE_CONTEXT_INTERFACE contextInterface
, void *pPrev
, size_t contextSize
)
434 struct list
*storeNext
= list_next(&store
->stores
, &storeEntry
->entry
);
435 PCONTEXT_STORE contextStore
= (PCONTEXT_STORE
)((LPBYTE
)storeEntry
->store
+
438 TRACE("(%p, %p, %p)\n", store
, storeEntry
, pPrev
);
442 /* Ref-counting funny business: "duplicate" (addref) the child, because
443 * the free(pPrev) below can cause the ref count to become negative.
445 child
= Context_GetLinkedContext(pPrev
, contextSize
);
446 contextInterface
->duplicate(child
);
447 child
= contextStore
->enumContext(storeEntry
->store
, child
);
448 contextInterface
->free(pPrev
);
452 child
= storeEntry
->store
->certs
.enumContext(storeEntry
->store
, NULL
);
454 ret
= CRYPT_CollectionCreateContextFromChild(store
, storeEntry
, child
,
459 ret
= CRYPT_CollectionAdvanceEnum(store
, LIST_ENTRY(storeNext
,
460 WINE_STORE_LIST_ENTRY
, entry
), contextStoreOffset
,
461 contextInterface
, NULL
, contextSize
);
464 SetLastError(CRYPT_E_NOT_FOUND
);
468 TRACE("returning %p\n", ret
);
472 static void *CRYPT_CollectionEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
474 PWINE_COLLECTIONSTORE cs
= (PWINE_COLLECTIONSTORE
)store
;
477 TRACE("(%p, %p)\n", store
, pPrev
);
479 EnterCriticalSection(&cs
->cs
);
482 PWINE_STORE_LIST_ENTRY storeEntry
=
483 *(PWINE_STORE_LIST_ENTRY
*)Context_GetExtra(pPrev
,
484 sizeof(CERT_CONTEXT
));
486 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
487 offsetof(WINECRYPT_CERTSTORE
, certs
), pCertInterface
, pPrev
,
488 sizeof(CERT_CONTEXT
));
492 if (!list_empty(&cs
->stores
))
494 PWINE_STORE_LIST_ENTRY storeEntry
= LIST_ENTRY(cs
->stores
.next
,
495 WINE_STORE_LIST_ENTRY
, entry
);
497 ret
= CRYPT_CollectionAdvanceEnum(cs
, storeEntry
,
498 offsetof(WINECRYPT_CERTSTORE
, certs
), pCertInterface
, NULL
,
499 sizeof(CERT_CONTEXT
));
503 SetLastError(CRYPT_E_NOT_FOUND
);
507 LeaveCriticalSection(&cs
->cs
);
509 ((PCERT_CONTEXT
)ret
)->hCertStore
= store
;
510 TRACE("returning %p\n", ret
);
514 static BOOL
CRYPT_CollectionDeleteCert(PWINECRYPT_CERTSTORE store
,
519 TRACE("(%p, %p)\n", store
, pCertContext
);
521 ret
= CertDeleteCertificateFromStore((PCCERT_CONTEXT
)
522 Context_GetLinkedContext(pCertContext
, sizeof(CERT_CONTEXT
)));
526 static WINECRYPT_CERTSTORE
*CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv
,
527 DWORD dwFlags
, const void *pvPara
)
529 PWINE_COLLECTIONSTORE store
;
531 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
533 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
538 store
= CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE
));
541 memset(store
, 0, sizeof(WINE_COLLECTIONSTORE
));
542 CRYPT_InitStore(&store
->hdr
, hCryptProv
, dwFlags
,
543 StoreTypeCollection
);
544 store
->hdr
.closeStore
= CRYPT_CollectionCloseStore
;
545 store
->hdr
.certs
.addContext
= CRYPT_CollectionAddCert
;
546 store
->hdr
.certs
.enumContext
= CRYPT_CollectionEnumCert
;
547 store
->hdr
.certs
.deleteContext
= CRYPT_CollectionDeleteCert
;
548 InitializeCriticalSection(&store
->cs
);
549 list_init(&store
->stores
);
552 return (PWINECRYPT_CERTSTORE
)store
;
555 static void WINAPI
CRYPT_ProvCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
557 PWINE_PROVIDERSTORE store
= (PWINE_PROVIDERSTORE
)hCertStore
;
559 TRACE("(%p, %08lx)\n", store
, dwFlags
);
561 if (store
->provCloseStore
)
562 store
->provCloseStore(store
->hStoreProv
, dwFlags
);
563 if (!(store
->dwStoreProvFlags
& CERT_STORE_PROV_EXTERNAL_FLAG
))
564 CertCloseStore(store
->memStore
, dwFlags
);
568 static BOOL
CRYPT_ProvAddCert(PWINECRYPT_CERTSTORE store
, void *cert
,
569 void *toReplace
, const void **ppStoreContext
)
571 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
574 TRACE("(%p, %p, %p, %p)\n", store
, cert
, toReplace
, ppStoreContext
);
577 ret
= ps
->memStore
->certs
.addContext(ps
->memStore
, cert
, toReplace
,
578 (const void **)ppStoreContext
);
581 if (ps
->hdr
.dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
583 SetLastError(ERROR_ACCESS_DENIED
);
589 if (ps
->provWriteCert
)
590 ret
= ps
->provWriteCert(ps
->hStoreProv
, (PCCERT_CONTEXT
)cert
,
591 CERT_STORE_PROV_WRITE_ADD_FLAG
);
593 ret
= ps
->memStore
->certs
.addContext(ps
->memStore
, cert
, NULL
,
594 (const void **)ppStoreContext
);
597 /* dirty trick: replace the returned context's hCertStore with
601 (*(PCERT_CONTEXT
*)ppStoreContext
)->hCertStore
= store
;
605 static void *CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store
, void *pPrev
)
607 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
610 ret
= ps
->memStore
->certs
.enumContext(ps
->memStore
, pPrev
);
613 /* same dirty trick: replace the returned context's hCertStore with
616 ((PCERT_CONTEXT
)ret
)->hCertStore
= store
;
621 static BOOL
CRYPT_ProvDeleteCert(PWINECRYPT_CERTSTORE store
,
624 PWINE_PROVIDERSTORE ps
= (PWINE_PROVIDERSTORE
)store
;
627 TRACE("(%p, %p)\n", store
, cert
);
629 if (ps
->provDeleteCert
)
630 ret
= ps
->provDeleteCert(ps
->hStoreProv
, cert
, 0);
632 ret
= ps
->memStore
->certs
.deleteContext(ps
->memStore
, cert
);
636 static BOOL WINAPI
CRYPT_ProvControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
637 DWORD dwCtrlType
, void const *pvCtrlPara
)
639 PWINE_PROVIDERSTORE store
= (PWINE_PROVIDERSTORE
)hCertStore
;
642 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
645 if (store
->provControl
)
646 ret
= store
->provControl(store
->hStoreProv
, dwFlags
, dwCtrlType
,
651 static PWINECRYPT_CERTSTORE
CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv
,
652 DWORD dwFlags
, PWINECRYPT_CERTSTORE memStore
, PCERT_STORE_PROV_INFO pProvInfo
)
654 PWINE_PROVIDERSTORE ret
= (PWINE_PROVIDERSTORE
)CryptMemAlloc(
655 sizeof(WINE_PROVIDERSTORE
));
659 CRYPT_InitStore(&ret
->hdr
, hCryptProv
, dwFlags
,
661 ret
->dwStoreProvFlags
= pProvInfo
->dwStoreProvFlags
;
662 if (ret
->dwStoreProvFlags
& CERT_STORE_PROV_EXTERNAL_FLAG
)
664 CertCloseStore(memStore
, 0);
665 ret
->memStore
= NULL
;
668 ret
->memStore
= memStore
;
669 ret
->hStoreProv
= pProvInfo
->hStoreProv
;
670 ret
->hdr
.closeStore
= CRYPT_ProvCloseStore
;
671 ret
->hdr
.certs
.addContext
= CRYPT_ProvAddCert
;
672 ret
->hdr
.certs
.enumContext
= CRYPT_ProvEnumCert
;
673 ret
->hdr
.certs
.deleteContext
= CRYPT_ProvDeleteCert
;
674 ret
->hdr
.control
= CRYPT_ProvControl
;
675 if (pProvInfo
->cStoreProvFunc
> CERT_STORE_PROV_CLOSE_FUNC
)
676 ret
->provCloseStore
=
677 pProvInfo
->rgpvStoreProvFunc
[CERT_STORE_PROV_CLOSE_FUNC
];
679 ret
->provCloseStore
= NULL
;
680 if (pProvInfo
->cStoreProvFunc
>
681 CERT_STORE_PROV_WRITE_CERT_FUNC
)
682 ret
->provWriteCert
= pProvInfo
->rgpvStoreProvFunc
[
683 CERT_STORE_PROV_WRITE_CERT_FUNC
];
685 ret
->provWriteCert
= NULL
;
686 if (pProvInfo
->cStoreProvFunc
>
687 CERT_STORE_PROV_DELETE_CERT_FUNC
)
688 ret
->provDeleteCert
= pProvInfo
->rgpvStoreProvFunc
[
689 CERT_STORE_PROV_DELETE_CERT_FUNC
];
691 ret
->provDeleteCert
= NULL
;
692 if (pProvInfo
->cStoreProvFunc
>
693 CERT_STORE_PROV_CONTROL_FUNC
)
694 ret
->provControl
= pProvInfo
->rgpvStoreProvFunc
[
695 CERT_STORE_PROV_CONTROL_FUNC
];
697 ret
->provControl
= NULL
;
699 return (PWINECRYPT_CERTSTORE
)ret
;
702 static PWINECRYPT_CERTSTORE
CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider
,
703 DWORD dwEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
, const void *pvPara
)
705 static HCRYPTOIDFUNCSET set
= NULL
;
706 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc
;
707 HCRYPTOIDFUNCADDR hFunc
;
708 PWINECRYPT_CERTSTORE ret
= NULL
;
711 set
= CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC
, 0);
712 CryptGetOIDFunctionAddress(set
, dwEncodingType
, lpszStoreProvider
, 0,
713 (void **)&provOpenFunc
, &hFunc
);
716 CERT_STORE_PROV_INFO provInfo
= { 0 };
718 provInfo
.cbSize
= sizeof(provInfo
);
719 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
720 provOpenFunc(lpszStoreProvider
, dwEncodingType
, hCryptProv
,
721 dwFlags
, pvPara
, NULL
, &provInfo
);
726 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
727 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
730 if (provOpenFunc(lpszStoreProvider
, dwEncodingType
, hCryptProv
,
731 dwFlags
, pvPara
, memStore
, &provInfo
))
732 ret
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
735 CertCloseStore(memStore
, 0);
738 CryptFreeOIDFunctionAddress(hFunc
, 0);
741 SetLastError(ERROR_FILE_NOT_FOUND
);
745 static void CRYPT_HashToStr(LPBYTE hash
, LPWSTR asciiHash
)
747 static const WCHAR fmt
[] = { '%','0','2','X',0 };
753 for (i
= 0; i
< 20; i
++)
754 wsprintfW(asciiHash
+ i
* 2, fmt
, hash
[i
]);
757 static const WCHAR CertsW
[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
759 static const WCHAR CRLsW
[] = { 'C','R','L','s',0 };
760 static const WCHAR CTLsW
[] = { 'C','T','L','s',0 };
761 static const WCHAR BlobW
[] = { 'B','l','o','b',0 };
763 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store
, HKEY key
,
768 WCHAR subKeyName
[MAX_PATH
];
771 DWORD size
= sizeof(subKeyName
) / sizeof(WCHAR
);
773 rc
= RegEnumKeyExW(key
, index
++, subKeyName
, &size
, NULL
, NULL
, NULL
,
779 rc
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
785 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, NULL
, &size
);
787 buf
= CryptMemAlloc(size
);
790 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, buf
,
797 TRACE("Adding cert with hash %s\n",
798 debugstr_w(subKeyName
));
799 context
= CRYPT_ReadSerializedElement(buf
, size
,
800 contextType
, &addedType
);
803 const WINE_CONTEXT_INTERFACE
*contextInterface
;
808 case CERT_STORE_CERTIFICATE_CONTEXT
:
809 contextInterface
= &gCertInterface
;
811 case CERT_STORE_CRL_CONTEXT
:
812 contextInterface
= &gCRLInterface
;
814 case CERT_STORE_CTL_CONTEXT
:
815 contextInterface
= &gCTLInterface
;
818 contextInterface
= NULL
;
820 if (contextInterface
)
823 if (contextInterface
->getProp(context
,
824 CERT_HASH_PROP_ID
, hash
, &size
))
826 WCHAR asciiHash
[20 * 2 + 1];
828 CRYPT_HashToStr(hash
, asciiHash
);
829 TRACE("comparing %s\n",
830 debugstr_w(asciiHash
));
831 TRACE("with %s\n", debugstr_w(subKeyName
));
832 if (!lstrcmpW(asciiHash
, subKeyName
))
834 TRACE("hash matches, adding\n");
835 contextInterface
->addContextToStore(
836 store
->memStore
, context
,
837 CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
840 TRACE("hash doesn't match, ignoring\n");
842 contextInterface
->free(context
);
850 /* Ignore intermediate errors, continue enumerating */
856 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store
)
858 static const WCHAR
*subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
859 static const DWORD contextFlags
[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG
,
860 CERT_STORE_CRL_CONTEXT_FLAG
, CERT_STORE_CTL_CONTEXT_FLAG
};
863 for (i
= 0; i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
868 rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0, KEY_READ
, NULL
,
872 CRYPT_RegReadSerializedFromReg(store
, key
, contextFlags
[i
]);
878 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
879 static BOOL
CRYPT_WriteSerializedToReg(HKEY key
, LPBYTE hash
, LPBYTE buf
,
882 WCHAR asciiHash
[20 * 2 + 1];
887 CRYPT_HashToStr(hash
, asciiHash
);
888 rc
= RegCreateKeyExW(key
, asciiHash
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
,
892 rc
= RegSetValueExW(subKey
, BlobW
, 0, REG_BINARY
, buf
, len
);
905 static BOOL
CRYPT_SerializeContextsToReg(HKEY key
,
906 const WINE_CONTEXT_INTERFACE
*contextInterface
, HCERTSTORE memStore
)
908 const void *context
= NULL
;
912 context
= contextInterface
->enumContextsInStore(memStore
, context
);
916 DWORD hashSize
= sizeof(hash
);
918 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
, hash
,
925 ret
= contextInterface
->serialize(context
, 0, NULL
, &size
);
927 buf
= CryptMemAlloc(size
);
930 ret
= contextInterface
->serialize(context
, 0, buf
, &size
);
932 ret
= CRYPT_WriteSerializedToReg(key
, hash
, buf
, size
);
939 } while (ret
&& context
!= NULL
);
941 contextInterface
->free(context
);
945 static BOOL
CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store
)
947 static const WCHAR
*subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
948 static const WINE_CONTEXT_INTERFACE
*interfaces
[] = { &gCertInterface
,
949 &gCRLInterface
, &gCTLInterface
};
950 struct list
*listToDelete
[] = { &store
->certsToDelete
, NULL
, NULL
};
954 for (i
= 0; ret
&& i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
957 LONG rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0,
958 KEY_ALL_ACCESS
, NULL
, &key
, NULL
);
964 PWINE_HASH_TO_DELETE toDelete
, next
;
965 WCHAR asciiHash
[20 * 2 + 1];
967 EnterCriticalSection(&store
->cs
);
968 LIST_FOR_EACH_ENTRY_SAFE(toDelete
, next
, listToDelete
[i
],
969 WINE_HASH_TO_DELETE
, entry
)
973 CRYPT_HashToStr(toDelete
->hash
, asciiHash
);
974 TRACE("Removing %s\n", debugstr_w(asciiHash
));
975 rc
= RegDeleteKeyW(key
, asciiHash
);
976 if (rc
!= ERROR_SUCCESS
&& rc
!= ERROR_FILE_NOT_FOUND
)
981 list_remove(&toDelete
->entry
);
982 CryptMemFree(toDelete
);
984 LeaveCriticalSection(&store
->cs
);
986 ret
= CRYPT_SerializeContextsToReg(key
, interfaces
[i
],
999 /* If force is true or the registry store is dirty, writes the contents of the
1000 * store to the registry.
1002 static BOOL
CRYPT_RegFlushStore(PWINE_REGSTOREINFO store
, BOOL force
)
1006 TRACE("(%p, %d)\n", store
, force
);
1008 if (store
->dirty
|| force
)
1009 ret
= CRYPT_RegWriteToReg(store
);
1015 static void WINAPI
CRYPT_RegCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
1017 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1019 TRACE("(%p, %08lx)\n", store
, dwFlags
);
1021 FIXME("Unimplemented flags: %08lx\n", dwFlags
);
1023 CRYPT_RegFlushStore(store
, FALSE
);
1024 RegCloseKey(store
->key
);
1025 DeleteCriticalSection(&store
->cs
);
1026 CryptMemFree(store
);
1029 static BOOL WINAPI
CRYPT_RegWriteCert(HCERTSTORE hCertStore
,
1030 PCCERT_CONTEXT cert
, DWORD dwFlags
)
1032 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1035 TRACE("(%p, %p, %ld)\n", hCertStore
, cert
, dwFlags
);
1037 if (dwFlags
& CERT_STORE_PROV_WRITE_ADD_FLAG
)
1039 store
->dirty
= TRUE
;
1047 static BOOL WINAPI
CRYPT_RegDeleteCert(HCERTSTORE hCertStore
,
1048 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
1050 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1053 TRACE("(%p, %p, %08lx)\n", store
, pCertContext
, dwFlags
);
1055 if (store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
1057 SetLastError(ERROR_ACCESS_DENIED
);
1062 PWINE_HASH_TO_DELETE toDelete
=
1063 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE
));
1067 DWORD size
= sizeof(toDelete
->hash
);
1069 ret
= CertGetCertificateContextProperty(pCertContext
,
1070 CERT_HASH_PROP_ID
, toDelete
->hash
, &size
);
1073 EnterCriticalSection(&store
->cs
);
1074 list_add_tail(&store
->certsToDelete
, &toDelete
->entry
);
1075 LeaveCriticalSection(&store
->cs
);
1079 CryptMemFree(toDelete
);
1086 store
->dirty
= TRUE
;
1091 static BOOL WINAPI
CRYPT_RegControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
1092 DWORD dwCtrlType
, void const *pvCtrlPara
)
1094 PWINE_REGSTOREINFO store
= (PWINE_REGSTOREINFO
)hCertStore
;
1097 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
1102 case CERT_STORE_CTRL_RESYNC
:
1103 CRYPT_RegFlushStore(store
, FALSE
);
1104 CRYPT_MemEmptyStore((PWINE_MEMSTORE
)store
->memStore
);
1105 CRYPT_RegReadFromReg(store
);
1108 case CERT_STORE_CTRL_COMMIT
:
1109 ret
= CRYPT_RegFlushStore(store
,
1110 dwFlags
& CERT_STORE_CTRL_COMMIT_FORCE_FLAG
);
1113 FIXME("%ld: stub\n", dwCtrlType
);
1119 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1120 static DWORD
CRYPT_RecurseDeleteKey(HKEY hKey
, LPCWSTR lpszSubKey
)
1122 DWORD dwRet
, dwKeyCount
= 0, dwMaxSubkeyLen
= 0, dwSize
, i
;
1123 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
1126 TRACE("(hkey=%p,%s)\n", hKey
, debugstr_w(lpszSubKey
));
1128 dwRet
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
1131 /* Find how many subkeys there are */
1132 dwRet
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, &dwKeyCount
,
1133 &dwMaxSubkeyLen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1137 if (dwMaxSubkeyLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
1139 /* Name too big: alloc a buffer for it */
1140 lpszName
= CryptMemAlloc(dwMaxSubkeyLen
*sizeof(WCHAR
));
1144 dwRet
= ERROR_NOT_ENOUGH_MEMORY
;
1147 /* Recursively delete all the subkeys */
1148 for (i
= 0; i
< dwKeyCount
&& !dwRet
; i
++)
1150 dwSize
= dwMaxSubkeyLen
;
1151 dwRet
= RegEnumKeyExW(hSubKey
, i
, lpszName
, &dwSize
, NULL
,
1154 dwRet
= CRYPT_RecurseDeleteKey(hSubKey
, lpszName
);
1157 if (lpszName
!= szNameBuf
)
1159 /* Free buffer if allocated */
1160 CryptMemFree(lpszName
);
1165 RegCloseKey(hSubKey
);
1167 dwRet
= RegDeleteKeyW(hKey
, lpszSubKey
);
1172 static void *regProvFuncs
[] = {
1173 CRYPT_RegCloseStore
,
1174 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
1176 CRYPT_RegDeleteCert
,
1177 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1178 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
1179 NULL
, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1180 NULL
, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1181 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1182 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
1183 NULL
, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1184 NULL
, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1185 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1189 static WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
,
1190 DWORD dwFlags
, const void *pvPara
)
1192 PWINECRYPT_CERTSTORE store
= NULL
;
1194 TRACE("(%ld, %08lx, %p)\n", hCryptProv
, dwFlags
, pvPara
);
1196 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
1198 DWORD rc
= CRYPT_RecurseDeleteKey((HKEY
)pvPara
, CertsW
);
1200 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
1201 rc
= CRYPT_RecurseDeleteKey((HKEY
)pvPara
, CRLsW
);
1202 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
1203 rc
= CRYPT_RecurseDeleteKey((HKEY
)pvPara
, CTLsW
);
1204 if (rc
== ERROR_NO_MORE_ITEMS
)
1212 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
1213 GetCurrentProcess(), (LPHANDLE
)&key
,
1214 dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
: KEY_ALL_ACCESS
,
1217 PWINECRYPT_CERTSTORE memStore
;
1219 memStore
= CRYPT_MemOpenStore(hCryptProv
, dwFlags
, NULL
);
1222 PWINE_REGSTOREINFO regInfo
= CryptMemAlloc(
1223 sizeof(WINE_REGSTOREINFO
));
1227 CERT_STORE_PROV_INFO provInfo
= { 0 };
1229 regInfo
->dwOpenFlags
= dwFlags
;
1230 regInfo
->cryptProv
= hCryptProv
;
1231 regInfo
->memStore
= memStore
;
1233 InitializeCriticalSection(®Info
->cs
);
1234 list_init(®Info
->certsToDelete
);
1235 CRYPT_RegReadFromReg(regInfo
);
1236 regInfo
->dirty
= FALSE
;
1237 provInfo
.cbSize
= sizeof(provInfo
);
1238 provInfo
.cStoreProvFunc
= sizeof(regProvFuncs
) /
1239 sizeof(regProvFuncs
[0]);
1240 provInfo
.rgpvStoreProvFunc
= regProvFuncs
;
1241 provInfo
.hStoreProv
= regInfo
;
1242 store
= CRYPT_ProvCreateStore(hCryptProv
, dwFlags
, memStore
,
1248 TRACE("returning %p\n", store
);
1252 /* FIXME: this isn't complete for the Root store, in which the top-level
1253 * self-signed CA certs reside. Adding a cert to the Root store should present
1254 * the user with a dialog indicating the consequences of doing so, and asking
1255 * the user to confirm whether the cert should be added.
1257 static PWINECRYPT_CERTSTORE
CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv
,
1258 DWORD dwFlags
, const void *pvPara
)
1260 static const WCHAR fmt
[] = { '%','s','\\','%','s',0 };
1261 LPCWSTR storeName
= (LPCWSTR
)pvPara
;
1263 PWINECRYPT_CERTSTORE store
= NULL
;
1268 TRACE("(%ld, %08lx, %s)\n", hCryptProv
, dwFlags
,
1269 debugstr_w((LPCWSTR
)pvPara
));
1273 SetLastError(E_INVALIDARG
);
1278 switch (dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
)
1280 case CERT_SYSTEM_STORE_LOCAL_MACHINE
:
1281 root
= HKEY_LOCAL_MACHINE
;
1282 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
1284 case CERT_SYSTEM_STORE_CURRENT_USER
:
1285 root
= HKEY_CURRENT_USER
;
1286 base
= CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH
;
1288 case CERT_SYSTEM_STORE_CURRENT_SERVICE
:
1289 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1290 * SystemCertificates
1292 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1293 debugstr_w(storeName
));
1295 case CERT_SYSTEM_STORE_SERVICES
:
1296 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1297 * SystemCertificates
1299 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1300 debugstr_w(storeName
));
1302 case CERT_SYSTEM_STORE_USERS
:
1303 /* hku\user sid\Software\Microsoft\SystemCertificates */
1304 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1305 debugstr_w(storeName
));
1307 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY
:
1308 root
= HKEY_CURRENT_USER
;
1309 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
1311 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY
:
1312 root
= HKEY_LOCAL_MACHINE
;
1313 base
= CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH
;
1315 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE
:
1316 /* hklm\Software\Microsoft\EnterpriseCertificates */
1317 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1318 debugstr_w(storeName
));
1321 SetLastError(E_INVALIDARG
);
1325 storePath
= CryptMemAlloc((lstrlenW(base
) + lstrlenW(storeName
) + 2) *
1331 REGSAM sam
= dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
:
1334 wsprintfW(storePath
, fmt
, base
, storeName
);
1335 if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
1336 rc
= RegOpenKeyExW(root
, storePath
, 0, sam
, &key
);
1341 rc
= RegCreateKeyExW(root
, storePath
, 0, NULL
, 0, sam
, NULL
,
1343 if (!rc
&& dwFlags
& CERT_STORE_CREATE_NEW_FLAG
&&
1344 disp
== REG_OPENED_EXISTING_KEY
)
1347 rc
= ERROR_FILE_EXISTS
;
1352 store
= CRYPT_RegOpenStore(hCryptProv
, dwFlags
, key
);
1357 CryptMemFree(storePath
);
1362 static PWINECRYPT_CERTSTORE
CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv
,
1363 DWORD dwFlags
, const void *pvPara
)
1366 PWINECRYPT_CERTSTORE ret
= NULL
;
1368 TRACE("(%ld, %08lx, %s)\n", hCryptProv
, dwFlags
,
1369 debugstr_a((LPCSTR
)pvPara
));
1373 SetLastError(ERROR_FILE_NOT_FOUND
);
1376 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
1379 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
1383 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
1384 ret
= CRYPT_SysRegOpenStoreW(hCryptProv
, dwFlags
, storeName
);
1385 CryptMemFree(storeName
);
1391 static PWINECRYPT_CERTSTORE
CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv
,
1392 DWORD dwFlags
, const void *pvPara
)
1394 HCERTSTORE store
= 0;
1397 TRACE("(%ld, %08lx, %s)\n", hCryptProv
, dwFlags
,
1398 debugstr_w((LPCWSTR
)pvPara
));
1402 SetLastError(ERROR_FILE_NOT_FOUND
);
1405 /* This returns a different error than system registry stores if the
1406 * location is invalid.
1408 switch (dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
)
1410 case CERT_SYSTEM_STORE_LOCAL_MACHINE
:
1411 case CERT_SYSTEM_STORE_CURRENT_USER
:
1412 case CERT_SYSTEM_STORE_CURRENT_SERVICE
:
1413 case CERT_SYSTEM_STORE_SERVICES
:
1414 case CERT_SYSTEM_STORE_USERS
:
1415 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY
:
1416 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY
:
1417 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE
:
1421 SetLastError(ERROR_FILE_NOT_FOUND
);
1426 HCERTSTORE regStore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W
,
1427 0, hCryptProv
, dwFlags
, pvPara
);
1431 store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
1432 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1433 CertAddStoreToCollection(store
, regStore
,
1434 dwFlags
& CERT_STORE_READONLY_FLAG
? 0 :
1435 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1436 CertCloseStore(regStore
, 0);
1437 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
1440 if ((dwFlags
& CERT_SYSTEM_STORE_LOCATION_MASK
) ==
1441 CERT_SYSTEM_STORE_CURRENT_USER
)
1443 dwFlags
&= ~CERT_SYSTEM_STORE_CURRENT_USER
;
1444 dwFlags
|= CERT_SYSTEM_STORE_LOCAL_MACHINE
;
1445 regStore
= CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W
, 0,
1446 hCryptProv
, dwFlags
, pvPara
);
1449 CertAddStoreToCollection(store
, regStore
,
1450 dwFlags
& CERT_STORE_READONLY_FLAG
? 0 :
1451 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
1452 CertCloseStore(regStore
, 0);
1457 return (PWINECRYPT_CERTSTORE
)store
;
1460 static PWINECRYPT_CERTSTORE
CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv
,
1461 DWORD dwFlags
, const void *pvPara
)
1464 PWINECRYPT_CERTSTORE ret
= NULL
;
1466 TRACE("(%ld, %08lx, %s)\n", hCryptProv
, dwFlags
,
1467 debugstr_a((LPCSTR
)pvPara
));
1471 SetLastError(ERROR_FILE_NOT_FOUND
);
1474 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
1477 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
1481 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
1482 ret
= CRYPT_SysOpenStoreW(hCryptProv
, dwFlags
, storeName
);
1483 CryptMemFree(storeName
);
1489 HCERTSTORE WINAPI
CertOpenStore(LPCSTR lpszStoreProvider
,
1490 DWORD dwMsgAndCertEncodingType
, HCRYPTPROV hCryptProv
, DWORD dwFlags
,
1493 WINECRYPT_CERTSTORE
*hcs
;
1494 StoreOpenFunc openFunc
= NULL
;
1496 TRACE("(%s, %08lx, %08lx, %08lx, %p)\n", debugstr_a(lpszStoreProvider
),
1497 dwMsgAndCertEncodingType
, hCryptProv
, dwFlags
, pvPara
);
1499 if (!HIWORD(lpszStoreProvider
))
1501 switch (LOWORD(lpszStoreProvider
))
1503 case (int)CERT_STORE_PROV_MEMORY
:
1504 openFunc
= CRYPT_MemOpenStore
;
1506 case (int)CERT_STORE_PROV_REG
:
1507 openFunc
= CRYPT_RegOpenStore
;
1509 case (int)CERT_STORE_PROV_COLLECTION
:
1510 openFunc
= CRYPT_CollectionOpenStore
;
1512 case (int)CERT_STORE_PROV_SYSTEM_A
:
1513 openFunc
= CRYPT_SysOpenStoreA
;
1515 case (int)CERT_STORE_PROV_SYSTEM_W
:
1516 openFunc
= CRYPT_SysOpenStoreW
;
1518 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A
:
1519 openFunc
= CRYPT_SysRegOpenStoreA
;
1521 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W
:
1522 openFunc
= CRYPT_SysRegOpenStoreW
;
1525 if (LOWORD(lpszStoreProvider
))
1526 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider
));
1529 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_MEMORY
))
1530 openFunc
= CRYPT_MemOpenStore
;
1531 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_SYSTEM
))
1532 openFunc
= CRYPT_SysOpenStoreW
;
1533 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_COLLECTION
))
1534 openFunc
= CRYPT_CollectionOpenStore
;
1535 else if (!strcasecmp(lpszStoreProvider
, sz_CERT_STORE_PROV_SYSTEM_REGISTRY
))
1536 openFunc
= CRYPT_SysRegOpenStoreW
;
1539 FIXME("unimplemented type %s\n", lpszStoreProvider
);
1544 hcs
= CRYPT_ProvOpenStore(lpszStoreProvider
, dwMsgAndCertEncodingType
,
1545 hCryptProv
, dwFlags
, pvPara
);
1547 hcs
= openFunc(hCryptProv
, dwFlags
, pvPara
);
1548 return (HCERTSTORE
)hcs
;
1551 HCERTSTORE WINAPI
CertOpenSystemStoreA(HCRYPTPROV hProv
,
1552 LPCSTR szSubSystemProtocol
)
1554 if (!szSubSystemProtocol
)
1556 SetLastError(E_INVALIDARG
);
1559 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A
, 0, hProv
,
1560 CERT_SYSTEM_STORE_CURRENT_USER
, szSubSystemProtocol
);
1563 HCERTSTORE WINAPI
CertOpenSystemStoreW(HCRYPTPROV hProv
,
1564 LPCWSTR szSubSystemProtocol
)
1566 if (!szSubSystemProtocol
)
1568 SetLastError(E_INVALIDARG
);
1571 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, hProv
,
1572 CERT_SYSTEM_STORE_CURRENT_USER
, szSubSystemProtocol
);
1575 BOOL WINAPI
CertSaveStore(HCERTSTORE hCertStore
, DWORD dwMsgAndCertEncodingType
,
1576 DWORD dwSaveAs
, DWORD dwSaveTo
, void* pvSaveToPara
, DWORD dwFlags
)
1578 FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore
,
1579 dwMsgAndCertEncodingType
, dwSaveAs
, dwSaveTo
, pvSaveToPara
, dwFlags
);
1583 PCCRL_CONTEXT WINAPI
CertCreateCRLContext( DWORD dwCertEncodingType
,
1584 const BYTE
* pbCrlEncoded
, DWORD cbCrlEncoded
)
1589 TRACE("%08lx %p %08lx\n", dwCertEncodingType
, pbCrlEncoded
, cbCrlEncoded
);
1591 /* FIXME: semi-stub, need to use CryptDecodeObjectEx to decode the CRL. */
1592 pcrl
= CryptMemAlloc( sizeof (CRL_CONTEXT
) );
1596 data
= CryptMemAlloc( cbCrlEncoded
);
1599 CryptMemFree( pcrl
);
1603 pcrl
->dwCertEncodingType
= dwCertEncodingType
;
1604 pcrl
->pbCrlEncoded
= data
;
1605 pcrl
->cbCrlEncoded
= cbCrlEncoded
;
1606 pcrl
->pCrlInfo
= NULL
;
1607 pcrl
->hCertStore
= 0;
1612 PCCERT_CONTEXT WINAPI
CertCreateCertificateContext(DWORD dwCertEncodingType
,
1613 const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
)
1615 PCERT_CONTEXT cert
= NULL
;
1617 PCERT_SIGNED_CONTENT_INFO signedCert
= NULL
;
1618 PCERT_INFO certInfo
= NULL
;
1621 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType
, pbCertEncoded
,
1624 /* First try to decode it as a signed cert. */
1625 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT
, pbCertEncoded
,
1626 cbCertEncoded
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, (BYTE
*)&signedCert
, &size
);
1630 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
1631 signedCert
->ToBeSigned
.pbData
, signedCert
->ToBeSigned
.cbData
,
1632 CRYPT_DECODE_ALLOC_FLAG
, NULL
, (BYTE
*)&certInfo
, &size
);
1633 LocalFree(signedCert
);
1635 /* Failing that, try it as an unsigned cert */
1639 ret
= CryptDecodeObjectEx(dwCertEncodingType
, X509_CERT_TO_BE_SIGNED
,
1640 pbCertEncoded
, cbCertEncoded
,
1641 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1642 (BYTE
*)&certInfo
, &size
);
1648 cert
= (PCERT_CONTEXT
)Context_CreateDataContext(sizeof(CERT_CONTEXT
));
1651 data
= CryptMemAlloc(cbCertEncoded
);
1658 memcpy(data
, pbCertEncoded
, cbCertEncoded
);
1659 cert
->dwCertEncodingType
= dwCertEncodingType
;
1660 cert
->pbCertEncoded
= data
;
1661 cert
->cbCertEncoded
= cbCertEncoded
;
1662 cert
->pCertInfo
= certInfo
;
1663 cert
->hCertStore
= 0;
1667 return (PCCERT_CONTEXT
)cert
;
1670 DWORD WINAPI
CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext
,
1673 PCONTEXT_PROPERTY_LIST properties
= Context_GetProperties(
1674 (void *)pCertContext
, sizeof(CERT_CONTEXT
));
1677 TRACE("(%p, %ld)\n", pCertContext
, dwPropId
);
1680 ret
= ContextPropertyList_EnumPropIDs(properties
, dwPropId
);
1686 static BOOL
CertContext_GetHashProp(void *context
, DWORD dwPropId
,
1687 ALG_ID algID
, const BYTE
*toHash
, DWORD toHashLen
, void *pvData
,
1690 BOOL ret
= CryptHashCertificate(0, algID
, 0, toHash
, toHashLen
, pvData
,
1694 CRYPT_DATA_BLOB blob
= { *pcbData
, pvData
};
1696 ret
= CertContext_SetProperty(context
, dwPropId
, 0, &blob
);
1701 static BOOL WINAPI
CertContext_GetProperty(void *context
, DWORD dwPropId
,
1702 void *pvData
, DWORD
*pcbData
)
1704 PCCERT_CONTEXT pCertContext
= (PCCERT_CONTEXT
)context
;
1705 PCONTEXT_PROPERTY_LIST properties
=
1706 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
1708 CRYPT_DATA_BLOB blob
;
1710 TRACE("(%p, %ld, %p, %p)\n", context
, dwPropId
, pvData
, pcbData
);
1713 ret
= ContextPropertyList_FindProperty(properties
, dwPropId
,
1721 *pcbData
= blob
.cbData
;
1724 else if (*pcbData
< blob
.cbData
)
1726 SetLastError(ERROR_MORE_DATA
);
1727 *pcbData
= blob
.cbData
;
1731 memcpy(pvData
, blob
.pbData
, blob
.cbData
);
1732 *pcbData
= blob
.cbData
;
1738 /* Implicit properties */
1741 case CERT_SHA1_HASH_PROP_ID
:
1742 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_SHA1
,
1743 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
1746 case CERT_MD5_HASH_PROP_ID
:
1747 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
1748 pCertContext
->pbCertEncoded
, pCertContext
->cbCertEncoded
, pvData
,
1751 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
1752 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
1753 pCertContext
->pCertInfo
->Subject
.pbData
,
1754 pCertContext
->pCertInfo
->Subject
.cbData
,
1757 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
1758 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
1759 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
1760 pCertContext
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
,
1763 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID
:
1764 ret
= CertContext_GetHashProp(context
, dwPropId
, CALG_MD5
,
1765 pCertContext
->pCertInfo
->SerialNumber
.pbData
,
1766 pCertContext
->pCertInfo
->SerialNumber
.cbData
,
1769 case CERT_SIGNATURE_HASH_PROP_ID
:
1770 FIXME("CERT_SIGNATURE_HASH_PROP_ID unimplemented\n");
1771 SetLastError(CRYPT_E_NOT_FOUND
);
1774 SetLastError(CRYPT_E_NOT_FOUND
);
1777 TRACE("returning %d\n", ret
);
1781 /* info is assumed to be a CRYPT_KEY_PROV_INFO, followed by its container name,
1782 * provider name, and any provider parameters, in a contiguous buffer, but
1783 * where info's pointers are assumed to be invalid. Upon return, info's
1784 * pointers point to the appropriate memory locations.
1786 static void CRYPT_FixKeyProvInfoPointers(PCRYPT_KEY_PROV_INFO info
)
1788 DWORD i
, containerLen
, provNameLen
;
1789 LPBYTE data
= (LPBYTE
)info
+ sizeof(CRYPT_KEY_PROV_INFO
);
1791 info
->pwszContainerName
= (LPWSTR
)data
;
1792 containerLen
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
1793 data
+= containerLen
;
1795 info
->pwszProvName
= (LPWSTR
)data
;
1796 provNameLen
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
1797 data
+= provNameLen
;
1799 info
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)data
;
1800 data
+= info
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
1802 for (i
= 0; i
< info
->cProvParam
; i
++)
1804 info
->rgProvParam
[i
].pbData
= data
;
1805 data
+= info
->rgProvParam
[i
].cbData
;
1809 BOOL WINAPI
CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
1810 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
1814 TRACE("(%p, %ld, %p, %p)\n", pCertContext
, dwPropId
, pvData
, pcbData
);
1819 case CERT_CERT_PROP_ID
:
1820 case CERT_CRL_PROP_ID
:
1821 case CERT_CTL_PROP_ID
:
1822 SetLastError(E_INVALIDARG
);
1825 case CERT_ACCESS_STATE_PROP_ID
:
1828 *pcbData
= sizeof(DWORD
);
1831 else if (*pcbData
< sizeof(DWORD
))
1833 SetLastError(ERROR_MORE_DATA
);
1834 *pcbData
= sizeof(DWORD
);
1841 if (pCertContext
->hCertStore
)
1843 PWINECRYPT_CERTSTORE store
=
1844 (PWINECRYPT_CERTSTORE
)pCertContext
->hCertStore
;
1846 if (!(store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
))
1847 state
|= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG
;
1849 *(DWORD
*)pvData
= state
;
1853 case CERT_KEY_PROV_INFO_PROP_ID
:
1854 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
1857 CRYPT_FixKeyProvInfoPointers((PCRYPT_KEY_PROV_INFO
)pvData
);
1860 ret
= CertContext_GetProperty((void *)pCertContext
, dwPropId
, pvData
,
1864 TRACE("returning %d\n", ret
);
1868 /* Copies key provider info from from into to, where to is assumed to be a
1869 * contiguous buffer of memory large enough for from and all its associated
1870 * data, but whose pointers are uninitialized.
1871 * Upon return, to contains a contiguous copy of from, packed in the following
1873 * - CRYPT_KEY_PROV_INFO
1874 * - pwszContainerName
1876 * - rgProvParam[0]...
1878 static void CRYPT_CopyKeyProvInfo(PCRYPT_KEY_PROV_INFO to
,
1879 PCRYPT_KEY_PROV_INFO from
)
1882 LPBYTE nextData
= (LPBYTE
)to
+ sizeof(CRYPT_KEY_PROV_INFO
);
1884 to
->pwszContainerName
= (LPWSTR
)nextData
;
1885 lstrcpyW(to
->pwszContainerName
, from
->pwszContainerName
);
1886 nextData
+= (lstrlenW(from
->pwszContainerName
) + 1) * sizeof(WCHAR
);
1887 to
->pwszProvName
= (LPWSTR
)nextData
;
1888 lstrcpyW(to
->pwszProvName
, from
->pwszProvName
);
1889 nextData
+= (lstrlenW(from
->pwszProvName
) + 1) * sizeof(WCHAR
);
1890 to
->dwProvType
= from
->dwProvType
;
1891 to
->dwFlags
= from
->dwFlags
;
1892 to
->cProvParam
= from
->cProvParam
;
1893 to
->rgProvParam
= (PCRYPT_KEY_PROV_PARAM
)nextData
;
1894 nextData
+= to
->cProvParam
* sizeof(CRYPT_KEY_PROV_PARAM
);
1895 to
->dwKeySpec
= from
->dwKeySpec
;
1896 for (i
= 0; i
< to
->cProvParam
; i
++)
1898 memcpy(&to
->rgProvParam
[i
], &from
->rgProvParam
[i
],
1899 sizeof(CRYPT_KEY_PROV_PARAM
));
1900 to
->rgProvParam
[i
].pbData
= nextData
;
1901 memcpy(to
->rgProvParam
[i
].pbData
, from
->rgProvParam
[i
].pbData
,
1902 from
->rgProvParam
[i
].cbData
);
1903 nextData
+= from
->rgProvParam
[i
].cbData
;
1907 static BOOL
CertContext_SetKeyProvInfoProperty(PCONTEXT_PROPERTY_LIST properties
,
1908 PCRYPT_KEY_PROV_INFO info
)
1912 DWORD size
= sizeof(CRYPT_KEY_PROV_INFO
), i
, containerSize
, provNameSize
;
1914 containerSize
= (lstrlenW(info
->pwszContainerName
) + 1) * sizeof(WCHAR
);
1915 provNameSize
= (lstrlenW(info
->pwszProvName
) + 1) * sizeof(WCHAR
);
1916 size
+= containerSize
+ provNameSize
;
1917 for (i
= 0; i
< info
->cProvParam
; i
++)
1918 size
+= sizeof(CRYPT_KEY_PROV_PARAM
) + info
->rgProvParam
[i
].cbData
;
1919 buf
= CryptMemAlloc(size
);
1922 CRYPT_CopyKeyProvInfo((PCRYPT_KEY_PROV_INFO
)buf
, info
);
1923 ret
= ContextPropertyList_SetProperty(properties
,
1924 CERT_KEY_PROV_INFO_PROP_ID
, buf
, size
);
1932 static BOOL WINAPI
CertContext_SetProperty(void *context
, DWORD dwPropId
,
1933 DWORD dwFlags
, const void *pvData
)
1935 PCONTEXT_PROPERTY_LIST properties
=
1936 Context_GetProperties(context
, sizeof(CERT_CONTEXT
));
1939 TRACE("(%p, %ld, %08lx, %p)\n", context
, dwPropId
, dwFlags
, pvData
);
1945 ContextPropertyList_RemoveProperty(properties
, dwPropId
);
1952 case CERT_AUTO_ENROLL_PROP_ID
:
1953 case CERT_CTL_USAGE_PROP_ID
: /* same as CERT_ENHKEY_USAGE_PROP_ID */
1954 case CERT_DESCRIPTION_PROP_ID
:
1955 case CERT_FRIENDLY_NAME_PROP_ID
:
1956 case CERT_HASH_PROP_ID
:
1957 case CERT_KEY_IDENTIFIER_PROP_ID
:
1958 case CERT_MD5_HASH_PROP_ID
:
1959 case CERT_NEXT_UPDATE_LOCATION_PROP_ID
:
1960 case CERT_PUBKEY_ALG_PARA_PROP_ID
:
1961 case CERT_PVK_FILE_PROP_ID
:
1962 case CERT_SIGNATURE_HASH_PROP_ID
:
1963 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID
:
1964 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID
:
1965 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID
:
1966 case CERT_ENROLLMENT_PROP_ID
:
1967 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID
:
1968 case CERT_RENEWAL_PROP_ID
:
1970 PCRYPT_DATA_BLOB blob
= (PCRYPT_DATA_BLOB
)pvData
;
1972 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
1973 blob
->pbData
, blob
->cbData
);
1976 case CERT_DATE_STAMP_PROP_ID
:
1977 ret
= ContextPropertyList_SetProperty(properties
, dwPropId
,
1978 (LPBYTE
)pvData
, sizeof(FILETIME
));
1980 case CERT_KEY_PROV_INFO_PROP_ID
:
1981 ret
= CertContext_SetKeyProvInfoProperty(properties
,
1982 (PCRYPT_KEY_PROV_INFO
)pvData
);
1985 FIXME("%ld: stub\n", dwPropId
);
1989 TRACE("returning %d\n", ret
);
1993 BOOL WINAPI
CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext
,
1994 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
1998 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext
, dwPropId
, dwFlags
, pvData
);
2000 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
2001 * crashes on most of these, I'll be safer.
2006 case CERT_ACCESS_STATE_PROP_ID
:
2007 case CERT_CERT_PROP_ID
:
2008 case CERT_CRL_PROP_ID
:
2009 case CERT_CTL_PROP_ID
:
2010 SetLastError(E_INVALIDARG
);
2013 ret
= CertContext_SetProperty((void *)pCertContext
, dwPropId
, dwFlags
,
2015 TRACE("returning %d\n", ret
);
2019 PCCERT_CONTEXT WINAPI
CertDuplicateCertificateContext(
2020 PCCERT_CONTEXT pCertContext
)
2022 TRACE("(%p)\n", pCertContext
);
2023 Context_AddRef((void *)pCertContext
, sizeof(CERT_CONTEXT
));
2024 return pCertContext
;
2027 static void CertContext_CopyProperties(PCCERT_CONTEXT to
, PCCERT_CONTEXT from
)
2029 PCONTEXT_PROPERTY_LIST toProperties
, fromProperties
;
2031 toProperties
= Context_GetProperties((void *)to
, sizeof(CERT_CONTEXT
));
2032 fromProperties
= Context_GetProperties((void *)from
, sizeof(CERT_CONTEXT
));
2033 ContextPropertyList_Copy(toProperties
, fromProperties
);
2036 BOOL WINAPI
CertAddCertificateContextToStore(HCERTSTORE hCertStore
,
2037 PCCERT_CONTEXT pCertContext
, DWORD dwAddDisposition
,
2038 PCCERT_CONTEXT
*ppStoreContext
)
2040 PWINECRYPT_CERTSTORE store
= (PWINECRYPT_CERTSTORE
)hCertStore
;
2042 PCCERT_CONTEXT toAdd
= NULL
, existing
= NULL
;
2044 TRACE("(%p, %p, %08lx, %p)\n", hCertStore
, pCertContext
,
2045 dwAddDisposition
, ppStoreContext
);
2047 /* Weird case to pass a test */
2048 if (dwAddDisposition
== 0)
2050 SetLastError(STATUS_ACCESS_VIOLATION
);
2053 if (dwAddDisposition
!= CERT_STORE_ADD_ALWAYS
)
2056 DWORD size
= sizeof(hashToAdd
);
2058 ret
= CertContext_GetProperty((void *)pCertContext
, CERT_HASH_PROP_ID
,
2062 CRYPT_HASH_BLOB blob
= { sizeof(hashToAdd
), hashToAdd
};
2064 existing
= CertFindCertificateInStore(hCertStore
,
2065 pCertContext
->dwCertEncodingType
, 0, CERT_FIND_SHA1_HASH
, &blob
,
2070 switch (dwAddDisposition
)
2072 case CERT_STORE_ADD_ALWAYS
:
2073 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2075 case CERT_STORE_ADD_NEW
:
2078 TRACE("found matching certificate, not adding\n");
2079 SetLastError(CRYPT_E_EXISTS
);
2083 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2085 case CERT_STORE_ADD_REPLACE_EXISTING
:
2086 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2088 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
:
2089 toAdd
= CertDuplicateCertificateContext(pCertContext
);
2091 CertContext_CopyProperties(toAdd
, existing
);
2093 case CERT_STORE_ADD_USE_EXISTING
:
2095 CertContext_CopyProperties(existing
, pCertContext
);
2098 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition
);
2105 ret
= store
->certs
.addContext(store
, (void *)toAdd
,
2106 (void *)existing
, (const void **)ppStoreContext
);
2107 else if (ppStoreContext
)
2108 *ppStoreContext
= CertDuplicateCertificateContext(toAdd
);
2109 CertFreeCertificateContext(toAdd
);
2111 CertFreeCertificateContext(existing
);
2113 TRACE("returning %d\n", ret
);
2117 BOOL WINAPI
CertAddEncodedCertificateToStore(HCERTSTORE hCertStore
,
2118 DWORD dwCertEncodingType
, const BYTE
*pbCertEncoded
, DWORD cbCertEncoded
,
2119 DWORD dwAddDisposition
, PCCERT_CONTEXT
*ppCertContext
)
2121 PCCERT_CONTEXT cert
= CertCreateCertificateContext(dwCertEncodingType
,
2122 pbCertEncoded
, cbCertEncoded
);
2125 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore
, dwCertEncodingType
,
2126 pbCertEncoded
, cbCertEncoded
, dwAddDisposition
, ppCertContext
);
2130 ret
= CertAddCertificateContextToStore(hCertStore
, cert
,
2131 dwAddDisposition
, ppCertContext
);
2132 CertFreeCertificateContext(cert
);
2139 PCCERT_CONTEXT WINAPI
CertEnumCertificatesInStore(HCERTSTORE hCertStore
,
2140 PCCERT_CONTEXT pPrev
)
2142 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2145 TRACE("(%p, %p)\n", hCertStore
, pPrev
);
2148 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2151 ret
= (PCCERT_CONTEXT
)hcs
->certs
.enumContext(hcs
, (void *)pPrev
);
2155 BOOL WINAPI
CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext
)
2159 TRACE("(%p)\n", pCertContext
);
2163 else if (!pCertContext
->hCertStore
)
2166 CertFreeCertificateContext(pCertContext
);
2170 PWINECRYPT_CERTSTORE hcs
=
2171 (PWINECRYPT_CERTSTORE
)pCertContext
->hCertStore
;
2173 if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2176 ret
= hcs
->certs
.deleteContext(hcs
, (void *)pCertContext
);
2177 CertFreeCertificateContext(pCertContext
);
2182 BOOL WINAPI
CertAddEncodedCRLToStore(HCERTSTORE hCertStore
,
2183 DWORD dwCertEncodingType
, const BYTE
*pbCrlEncoded
, DWORD cbCrlEncoded
,
2184 DWORD dwAddDisposition
, PCCRL_CONTEXT
*ppCrlContext
)
2186 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore
,
2187 dwCertEncodingType
, pbCrlEncoded
, cbCrlEncoded
, dwAddDisposition
,
2192 BOOL WINAPI
CertAddCRLContextToStore( HCERTSTORE hCertStore
,
2193 PCCRL_CONTEXT pCrlContext
, DWORD dwAddDisposition
,
2194 PCCRL_CONTEXT
* ppStoreContext
)
2196 FIXME("%p %p %08lx %p\n", hCertStore
, pCrlContext
,
2197 dwAddDisposition
, ppStoreContext
);
2201 PCCRL_CONTEXT WINAPI
CertDuplicateCRLContext(PCCRL_CONTEXT pCrlContext
)
2203 FIXME("(%p): stub\n", pCrlContext
);
2207 BOOL WINAPI
CertFreeCRLContext( PCCRL_CONTEXT pCrlContext
)
2209 FIXME("%p\n", pCrlContext
);
2214 BOOL WINAPI
CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext
)
2216 FIXME("(%p): stub\n", pCrlContext
);
2220 PCCRL_CONTEXT WINAPI
CertEnumCRLsInStore(HCERTSTORE hCertStore
,
2221 PCCRL_CONTEXT pPrev
)
2223 FIXME("(%p, %p): stub\n", hCertStore
, pPrev
);
2227 PCCTL_CONTEXT WINAPI
CertCreateCTLContext(DWORD dwCertEncodingType
,
2228 const BYTE
* pbCtlEncoded
, DWORD cbCtlEncoded
)
2230 FIXME("(%08lx, %p, %08lx): stub\n", dwCertEncodingType
, pbCtlEncoded
,
2235 BOOL WINAPI
CertAddEncodedCTLToStore(HCERTSTORE hCertStore
,
2236 DWORD dwMsgAndCertEncodingType
, const BYTE
*pbCtlEncoded
, DWORD cbCtlEncoded
,
2237 DWORD dwAddDisposition
, PCCTL_CONTEXT
*ppCtlContext
)
2239 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore
,
2240 dwMsgAndCertEncodingType
, pbCtlEncoded
, cbCtlEncoded
, dwAddDisposition
,
2245 BOOL WINAPI
CertAddCTLContextToStore(HCERTSTORE hCertStore
,
2246 PCCTL_CONTEXT pCtlContext
, DWORD dwAddDisposition
,
2247 PCCTL_CONTEXT
* ppStoreContext
)
2249 FIXME("(%p, %p, %08lx, %p): stub\n", hCertStore
, pCtlContext
,
2250 dwAddDisposition
, ppStoreContext
);
2254 PCCTL_CONTEXT WINAPI
CertDuplicateCTLContext(PCCTL_CONTEXT pCtlContext
)
2256 FIXME("(%p): stub\n", pCtlContext
);
2260 BOOL WINAPI
CertFreeCTLContext(PCCTL_CONTEXT pCtlContext
)
2262 FIXME("(%p): stub\n", pCtlContext
);
2266 BOOL WINAPI
CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext
)
2268 FIXME("(%p): stub\n", pCtlContext
);
2272 PCCTL_CONTEXT WINAPI
CertEnumCTLsInStore(HCERTSTORE hCertStore
,
2273 PCCTL_CONTEXT pPrev
)
2275 FIXME("(%p, %p): stub\n", hCertStore
, pPrev
);
2279 HCERTSTORE WINAPI
CertDuplicateStore(HCERTSTORE hCertStore
)
2281 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2283 TRACE("(%p)\n", hCertStore
);
2285 if (hcs
&& hcs
->dwMagic
== WINE_CRYPTCERTSTORE_MAGIC
)
2286 InterlockedIncrement(&hcs
->ref
);
2290 BOOL WINAPI
CertCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
2292 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*) hCertStore
;
2294 TRACE("(%p, %08lx)\n", hCertStore
, dwFlags
);
2299 if ( hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2302 if (InterlockedDecrement(&hcs
->ref
) == 0)
2304 TRACE("%p's ref count is 0, freeing\n", hcs
);
2306 if (!(hcs
->dwOpenFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
2307 CryptReleaseContext(hcs
->cryptProv
, 0);
2308 hcs
->closeStore(hcs
, dwFlags
);
2311 TRACE("%p's ref count is %ld\n", hcs
, hcs
->ref
);
2315 BOOL WINAPI
CertControlStore(HCERTSTORE hCertStore
, DWORD dwFlags
,
2316 DWORD dwCtrlType
, void const *pvCtrlPara
)
2318 WINECRYPT_CERTSTORE
*hcs
= (WINECRYPT_CERTSTORE
*)hCertStore
;
2321 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
2326 else if (hcs
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2331 ret
= hcs
->control(hCertStore
, dwFlags
, dwCtrlType
, pvCtrlPara
);
2338 BOOL WINAPI
CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext
,
2339 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
2341 FIXME("(%p, %ld, %p, %p): stub\n", pCRLContext
, dwPropId
, pvData
, pcbData
);
2345 BOOL WINAPI
CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext
,
2346 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
2348 FIXME("(%p, %ld, %08lx, %p): stub\n", pCRLContext
, dwPropId
, dwFlags
,
2353 BOOL WINAPI
CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
2354 DWORD dwPropId
, void *pvData
, DWORD
*pcbData
)
2356 FIXME("(%p, %ld, %p, %p): stub\n", pCTLContext
, dwPropId
, pvData
, pcbData
);
2360 BOOL WINAPI
CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext
,
2361 DWORD dwPropId
, DWORD dwFlags
, const void *pvData
)
2363 FIXME("(%p, %ld, %08lx, %p): stub\n", pCTLContext
, dwPropId
, dwFlags
,
2368 static void CertDataContext_Free(void *context
)
2370 PCERT_CONTEXT certContext
= (PCERT_CONTEXT
)context
;
2372 CryptMemFree(certContext
->pbCertEncoded
);
2373 LocalFree(certContext
->pCertInfo
);
2376 BOOL WINAPI
CertFreeCertificateContext(PCCERT_CONTEXT pCertContext
)
2378 TRACE("(%p)\n", pCertContext
);
2381 Context_Release((void *)pCertContext
, sizeof(CERT_CONTEXT
),
2382 CertDataContext_Free
);
2386 BOOL WINAPI
CertAddStoreToCollection(HCERTSTORE hCollectionStore
,
2387 HCERTSTORE hSiblingStore
, DWORD dwUpdateFlags
, DWORD dwPriority
)
2389 PWINE_COLLECTIONSTORE collection
= (PWINE_COLLECTIONSTORE
)hCollectionStore
;
2390 WINECRYPT_CERTSTORE
*sibling
= (WINECRYPT_CERTSTORE
*)hSiblingStore
;
2391 PWINE_STORE_LIST_ENTRY entry
;
2394 TRACE("(%p, %p, %08lx, %ld)\n", hCollectionStore
, hSiblingStore
,
2395 dwUpdateFlags
, dwPriority
);
2397 if (!collection
|| !sibling
)
2399 if (collection
->hdr
.dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2401 SetLastError(E_INVALIDARG
);
2404 if (collection
->hdr
.type
!= StoreTypeCollection
)
2406 SetLastError(E_INVALIDARG
);
2409 if (sibling
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2411 SetLastError(E_INVALIDARG
);
2415 entry
= CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY
));
2418 InterlockedIncrement(&sibling
->ref
);
2419 TRACE("sibling %p's ref count is %ld\n", sibling
, sibling
->ref
);
2420 entry
->store
= sibling
;
2421 entry
->dwUpdateFlags
= dwUpdateFlags
;
2422 entry
->dwPriority
= dwPriority
;
2423 list_init(&entry
->entry
);
2424 TRACE("%p: adding %p, priority %ld\n", collection
, entry
, dwPriority
);
2425 EnterCriticalSection(&collection
->cs
);
2428 PWINE_STORE_LIST_ENTRY cursor
;
2431 LIST_FOR_EACH_ENTRY(cursor
, &collection
->stores
,
2432 WINE_STORE_LIST_ENTRY
, entry
)
2434 if (cursor
->dwPriority
< dwPriority
)
2436 list_add_before(&cursor
->entry
, &entry
->entry
);
2442 list_add_tail(&collection
->stores
, &entry
->entry
);
2445 list_add_tail(&collection
->stores
, &entry
->entry
);
2446 LeaveCriticalSection(&collection
->cs
);
2454 void WINAPI
CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore
,
2455 HCERTSTORE hSiblingStore
)
2457 PWINE_COLLECTIONSTORE collection
= (PWINE_COLLECTIONSTORE
)hCollectionStore
;
2458 WINECRYPT_CERTSTORE
*sibling
= (WINECRYPT_CERTSTORE
*)hSiblingStore
;
2459 PWINE_STORE_LIST_ENTRY store
, next
;
2461 TRACE("(%p, %p)\n", hCollectionStore
, hSiblingStore
);
2463 if (!collection
|| !sibling
)
2465 if (collection
->hdr
.dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2467 SetLastError(E_INVALIDARG
);
2470 if (collection
->hdr
.type
!= StoreTypeCollection
)
2472 if (sibling
->dwMagic
!= WINE_CRYPTCERTSTORE_MAGIC
)
2474 SetLastError(E_INVALIDARG
);
2477 EnterCriticalSection(&collection
->cs
);
2478 LIST_FOR_EACH_ENTRY_SAFE(store
, next
, &collection
->stores
,
2479 WINE_STORE_LIST_ENTRY
, entry
)
2481 if (store
->store
== sibling
)
2483 list_remove(&store
->entry
);
2484 CertCloseStore(store
->store
, 0);
2485 CryptMemFree(store
);
2489 LeaveCriticalSection(&collection
->cs
);