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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 * - Opening a cert store provider should be morphed to support loading
25 * - The concept of physical stores and locations isn't implemented. (This
26 * doesn't mean registry stores et al aren't implemented. See the PSDK for
27 * registering and enumerating physical stores and locations.)
28 * - Many flags, options and whatnot are unimplemented.
32 #include "wine/port.h"
42 #include "wine/debug.h"
43 #include "wine/list.h"
45 #include "wine/exception.h"
46 #include "crypt32_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
50 #define WINE_CRYPTCERTSTORE_MAGIC 0x74726563
51 /* The following aren't defined in wincrypt.h, as they're "reserved" */
52 #define CERT_CERT_PROP_ID 32
53 #define CERT_CRL_PROP_ID 33
54 #define CERT_CTL_PROP_ID 34
56 /* Some typedefs that make it easier to abstract which type of context we're
59 typedef const void *(WINAPI *CreateContextFunc)(DWORD dwCertEncodingType,
60 const BYTE *pbCertEncoded, DWORD cbCertEncoded);
61 typedef BOOL (WINAPI *AddContextToStoreFunc)(HCERTSTORE hCertStore,
62 const void *context, DWORD dwAddDisposition, const void **ppStoreContext);
63 typedef BOOL (WINAPI *AddEncodedContextToStoreFunc)(HCERTSTORE hCertStore,
64 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
65 DWORD dwAddDisposition, const void **ppContext);
66 typedef const void *(WINAPI *EnumContextsInStoreFunc)(HCERTSTORE hCertStore,
67 const void *pPrevContext);
68 typedef BOOL (WINAPI *GetContextPropertyFunc)(const void *context,
69 DWORD dwPropID, void *pvData, DWORD *pcbData);
70 typedef BOOL (WINAPI *SetContextPropertyFunc)(const void *context,
71 DWORD dwPropID, DWORD dwFlags, const void *pvData);
72 typedef BOOL (WINAPI *SerializeElementFunc)(const void *context, DWORD dwFlags,
73 BYTE *pbElement, DWORD *pcbElement);
74 typedef BOOL (WINAPI *FreeContextFunc)(const void *context);
75 typedef BOOL (WINAPI *DeleteContextFunc)(const void *context);
77 /* An abstract context (certificate, CRL, or CTL) interface */
78 typedef struct _WINE_CONTEXT_INTERFACE
80 CreateContextFunc create;
81 AddContextToStoreFunc addContextToStore;
82 AddEncodedContextToStoreFunc addEncodedToStore;
83 EnumContextsInStoreFunc enumContextsInStore;
84 GetContextPropertyFunc getProp;
85 SetContextPropertyFunc setProp;
86 SerializeElementFunc serialize;
88 DeleteContextFunc deleteFromStore;
89 } WINE_CONTEXT_INTERFACE, *PWINE_CONTEXT_INTERFACE;
91 static const WINE_CONTEXT_INTERFACE gCertInterface = {
92 (CreateContextFunc)CertCreateCertificateContext,
93 (AddContextToStoreFunc)CertAddCertificateContextToStore,
94 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
95 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
96 (GetContextPropertyFunc)CertGetCertificateContextProperty,
97 (SetContextPropertyFunc)CertSetCertificateContextProperty,
98 (SerializeElementFunc)CertSerializeCertificateStoreElement,
99 (FreeContextFunc)CertFreeCertificateContext,
100 (DeleteContextFunc)CertDeleteCertificateFromStore,
103 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
104 (CreateContextFunc)CertCreateCRLContext,
105 (AddContextToStoreFunc)CertAddCRLContextToStore,
106 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
107 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
108 (GetContextPropertyFunc)CertGetCRLContextProperty,
109 (SetContextPropertyFunc)CertSetCRLContextProperty,
110 (SerializeElementFunc)CertSerializeCRLStoreElement,
111 (FreeContextFunc)CertFreeCRLContext,
112 (DeleteContextFunc)CertDeleteCRLFromStore,
115 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
116 (CreateContextFunc)CertCreateCTLContext,
117 (AddContextToStoreFunc)CertAddCTLContextToStore,
118 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
119 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
120 (GetContextPropertyFunc)CertGetCTLContextProperty,
121 (SetContextPropertyFunc)CertSetCTLContextProperty,
122 (SerializeElementFunc)CertSerializeCTLStoreElement,
123 (FreeContextFunc)CertFreeCTLContext,
124 (DeleteContextFunc)CertDeleteCTLFromStore,
127 struct WINE_CRYPTCERTSTORE;
129 typedef struct WINE_CRYPTCERTSTORE * (*StoreOpenFunc)(HCRYPTPROV hCryptProv,
130 DWORD dwFlags, const void *pvPara);
132 struct _WINE_CERT_CONTEXT_REF;
134 /* Called to enumerate the next certificate in a store. The returned pointer
135 * must be newly allocated (via CryptMemAlloc): CertFreeCertificateContext
138 typedef struct _WINE_CERT_CONTEXT_REF * (*EnumCertFunc)
139 (struct WINE_CRYPTCERTSTORE *store, struct _WINE_CERT_CONTEXT_REF *pPrev);
141 struct _WINE_CERT_CONTEXT;
143 /* Called to create a new reference to an existing cert context. Should call
144 * CRYPT_InitCertRef to make sure the reference count is properly updated.
145 * If the store does not provide any additional allocated data (that is, does
146 * not need to implement a FreeCertFunc), it may use CRYPT_CreateCertRef for
149 typedef struct _WINE_CERT_CONTEXT_REF * (*CreateRefFunc)
150 (struct _WINE_CERT_CONTEXT *context, HCERTSTORE store);
152 /* Optional, called when a cert context reference is being freed. Don't free
153 * the ref pointer itself, CertFreeCertificateContext does that.
155 typedef void (*FreeCertFunc)(struct _WINE_CERT_CONTEXT_REF *ref);
157 typedef enum _CertStoreType {
163 /* A cert store is polymorphic through the use of function pointers. A type
164 * is still needed to distinguish collection stores from other types.
165 * On the function pointers:
166 * - closeStore is called when the store's ref count becomes 0
167 * - addCert is called with a PWINE_CERT_CONTEXT as the second parameter
168 * - control is optional, but should be implemented by any store that supports
171 typedef struct WINE_CRYPTCERTSTORE
176 HCRYPTPROV cryptProv;
178 PFN_CERT_STORE_PROV_CLOSE closeStore;
179 PFN_CERT_STORE_PROV_WRITE_CERT addCert;
180 CreateRefFunc createCertRef;
181 EnumCertFunc enumCert;
182 PFN_CERT_STORE_PROV_DELETE_CERT deleteCert;
183 FreeCertFunc freeCert; /* optional */
184 PFN_CERT_STORE_PROV_CONTROL control; /* optional */
185 } WINECRYPT_CERTSTORE, *PWINECRYPT_CERTSTORE;
187 /* A certificate context has pointers to data that are owned by this module,
188 * so rather than duplicate the data every time a certificate context is
189 * copied, I keep a reference count to the data. Thus I have two data
190 * structures, the "true" certificate context (that has the reference count)
191 * and a reference certificate context, that has a pointer to the true context.
192 * Each one can be cast to a PCERT_CONTEXT, though you'll usually be dealing
193 * with the reference version.
195 typedef struct _WINE_CERT_CONTEXT
200 struct list extendedProperties;
201 } WINE_CERT_CONTEXT, *PWINE_CERT_CONTEXT;
203 typedef struct _WINE_CERT_CONTEXT_REF
206 WINE_CERT_CONTEXT *context;
207 } WINE_CERT_CONTEXT_REF, *PWINE_CERT_CONTEXT_REF;
209 /* An extended certificate property in serialized form is prefixed by this
212 typedef struct _WINE_CERT_PROP_HEADER
215 DWORD unknown; /* always 1 */
217 } WINE_CERT_PROP_HEADER, *PWINE_CERT_PROP_HEADER;
219 /* Stores an extended property in a cert. */
220 typedef struct _WINE_CERT_PROPERTY
222 WINE_CERT_PROP_HEADER hdr;
225 } WINE_CERT_PROPERTY, *PWINE_CERT_PROPERTY;
227 /* A mem store has a list of these. They're also returned by the mem store
228 * during enumeration.
230 typedef struct _WINE_CERT_LIST_ENTRY
232 WINE_CERT_CONTEXT_REF cert;
234 } WINE_CERT_LIST_ENTRY, *PWINE_CERT_LIST_ENTRY;
236 typedef struct _WINE_MEMSTORE
238 WINECRYPT_CERTSTORE hdr;
241 } WINE_MEMSTORE, *PWINE_MEMSTORE;
243 typedef struct _WINE_HASH_TO_DELETE
247 } WINE_HASH_TO_DELETE, *PWINE_HASH_TO_DELETE;
249 /* Returned by a provider store during enumeration. */
250 typedef struct _WINE_PROV_CERT_CONTEXT
252 WINE_CERT_CONTEXT_REF cert;
253 PWINE_CERT_CONTEXT_REF childContext;
254 } WINE_PROV_CERT_CONTEXT, *PWINE_PROV_CERT_CONTEXT;
256 typedef struct _WINE_REGSTOREINFO
259 HCRYPTPROV cryptProv;
260 PWINECRYPT_CERTSTORE memStore;
264 struct list certsToDelete;
265 } WINE_REGSTOREINFO, *PWINE_REGSTOREINFO;
267 typedef struct _WINE_STORE_LIST_ENTRY
269 PWINECRYPT_CERTSTORE store;
273 } WINE_STORE_LIST_ENTRY, *PWINE_STORE_LIST_ENTRY;
275 /* Returned by a collection store during enumeration.
276 * Note: relies on the list entry being valid after use, which a number of
277 * conditions might make untrue (reentrancy, closing a collection store before
278 * continuing an enumeration on it, ...). The tests seem to indicate this
279 * sort of unsafety is okay, since Windows isn't well-behaved in these
282 typedef struct _WINE_COLLECTION_CERT_CONTEXT
284 WINE_CERT_CONTEXT_REF cert;
285 PWINE_STORE_LIST_ENTRY storeEntry;
286 PWINE_CERT_CONTEXT_REF childContext;
287 } WINE_COLLECTION_CERT_CONTEXT, *PWINE_COLLECTION_CERT_CONTEXT;
289 typedef struct _WINE_COLLECTIONSTORE
291 WINECRYPT_CERTSTORE hdr;
294 } WINE_COLLECTIONSTORE, *PWINE_COLLECTIONSTORE;
296 typedef struct _WINE_PROVIDERSTORE
298 WINECRYPT_CERTSTORE hdr;
299 DWORD dwStoreProvFlags;
300 PWINECRYPT_CERTSTORE memStore;
301 HCERTSTOREPROV hStoreProv;
302 PFN_CERT_STORE_PROV_CLOSE provCloseStore;
303 PFN_CERT_STORE_PROV_WRITE_CERT provWriteCert;
304 PFN_CERT_STORE_PROV_DELETE_CERT provDeleteCert;
305 PFN_CERT_STORE_PROV_CONTROL provControl;
306 } WINE_PROVIDERSTORE, *PWINE_PROVIDERSTORE;
308 /* Like CertGetCertificateContextProperty, but operates directly on the
309 * WINE_CERT_CONTEXT. Doesn't support special-case properties, since they
310 * are handled by CertGetCertificateContextProperty, and are particular to the
311 * store in which the property exists (which is separate from the context.)
313 static BOOL WINAPI CRYPT_GetCertificateContextProperty(
314 PWINE_CERT_CONTEXT context, DWORD dwPropId, void *pvData, DWORD *pcbData);
316 /* Like CertSetCertificateContextProperty, but operates directly on the
317 * WINE_CERT_CONTEXT. Doesn't handle special cases, since they're handled by
318 * CertSetCertificateContextProperty anyway.
320 static BOOL WINAPI CRYPT_SetCertificateContextProperty(
321 PWINE_CERT_CONTEXT context, DWORD dwPropId, DWORD dwFlags, const void *pvData);
323 /* Helper function for store reading functions and
324 * CertAddSerializedElementToStore. Returns a context of the appropriate type
325 * if it can, or NULL otherwise. Doesn't validate any of the properties in
326 * the serialized context (for example, bad hashes are retained.)
327 * *pdwContentType is set to the type of the returned context.
329 static const void * WINAPI CRYPT_ReadSerializedElement(const BYTE *pbElement,
330 DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType);
332 static void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, HCRYPTPROV hCryptProv,
333 DWORD dwFlags, CertStoreType type)
336 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
340 hCryptProv = CRYPT_GetDefaultProvider();
341 dwFlags |= CERT_STORE_NO_CRYPT_RELEASE_FLAG;
343 store->cryptProv = hCryptProv;
344 store->dwOpenFlags = dwFlags;
347 /* Initializes the reference ref to point to pCertContext, which is assumed to
348 * be a PWINE_CERT_CONTEXT, and increments pCertContext's reference count.
349 * Also sets the hCertStore member of the reference to store.
351 static void CRYPT_InitCertRef(PWINE_CERT_CONTEXT_REF ref,
352 PWINE_CERT_CONTEXT context, HCERTSTORE store)
354 TRACE("(%p, %p)\n", ref, context);
355 memcpy(&ref->cert, context, sizeof(ref->cert));
356 ref->context = context;
357 InterlockedIncrement(&context->ref);
358 TRACE("%p's ref count is %ld\n", context, context->ref);
359 ref->cert.hCertStore = store;
362 static PWINE_CERT_CONTEXT_REF CRYPT_CreateCertRef(PWINE_CERT_CONTEXT context,
365 PWINE_CERT_CONTEXT_REF pCertRef = CryptMemAlloc(
366 sizeof(WINE_CERT_CONTEXT_REF));
368 TRACE("(%p, %p)\n", context, store);
370 CRYPT_InitCertRef(pCertRef, context, store);
374 static BOOL WINAPI CRYPT_MemAddCert(HCERTSTORE store, PCCERT_CONTEXT pCert,
375 DWORD dwAddDisposition)
377 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
378 BOOL add = FALSE, ret;
380 TRACE("(%p, %p, %ld)\n", store, pCert, dwAddDisposition);
382 switch (dwAddDisposition)
384 case CERT_STORE_ADD_ALWAYS:
387 case CERT_STORE_ADD_NEW:
389 BYTE hashToAdd[20], hash[20];
390 DWORD size = sizeof(hashToAdd);
392 ret = CRYPT_GetCertificateContextProperty((PWINE_CERT_CONTEXT)pCert,
393 CERT_HASH_PROP_ID, hashToAdd, &size);
396 PWINE_CERT_LIST_ENTRY cursor;
398 /* Add if no cert with the same hash is found. */
400 EnterCriticalSection(&ms->cs);
401 LIST_FOR_EACH_ENTRY(cursor, &ms->certs, WINE_CERT_LIST_ENTRY, entry)
404 ret = CertGetCertificateContextProperty(&cursor->cert.cert,
405 CERT_HASH_PROP_ID, hash, &size);
406 if (ret && !memcmp(hashToAdd, hash, size))
408 TRACE("found matching certificate, not adding\n");
409 SetLastError(CRYPT_E_EXISTS);
414 LeaveCriticalSection(&ms->cs);
418 case CERT_STORE_ADD_REPLACE_EXISTING:
420 BYTE hashToAdd[20], hash[20];
421 DWORD size = sizeof(hashToAdd);
424 ret = CRYPT_GetCertificateContextProperty((PWINE_CERT_CONTEXT)pCert,
425 CERT_HASH_PROP_ID, hashToAdd, &size);
428 PWINE_CERT_LIST_ENTRY cursor, next;
430 /* Look for existing cert to delete */
431 EnterCriticalSection(&ms->cs);
432 LIST_FOR_EACH_ENTRY_SAFE(cursor, next, &ms->certs,
433 WINE_CERT_LIST_ENTRY, entry)
436 ret = CertGetCertificateContextProperty(&cursor->cert.cert,
437 CERT_HASH_PROP_ID, hash, &size);
438 if (ret && !memcmp(hashToAdd, hash, size))
440 TRACE("found matching certificate, replacing\n");
441 list_remove(&cursor->entry);
442 CertFreeCertificateContext((PCCERT_CONTEXT)cursor);
446 LeaveCriticalSection(&ms->cs);
451 FIXME("Unimplemented add disposition %ld\n", dwAddDisposition);
456 PWINE_CERT_LIST_ENTRY entry = CryptMemAlloc(
457 sizeof(WINE_CERT_LIST_ENTRY));
461 TRACE("adding %p\n", entry);
462 CRYPT_InitCertRef(&entry->cert, (PWINE_CERT_CONTEXT)pCert, store);
463 list_init(&entry->entry);
464 EnterCriticalSection(&ms->cs);
465 list_add_tail(&ms->certs, &entry->entry);
466 LeaveCriticalSection(&ms->cs);
474 TRACE("returning %d\n", ret);
478 static PWINE_CERT_CONTEXT_REF CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store,
479 PWINE_CERT_CONTEXT_REF pPrev)
481 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
482 PWINE_CERT_LIST_ENTRY prevEntry = (PWINE_CERT_LIST_ENTRY)pPrev, ret;
483 struct list *listNext;
485 TRACE("(%p, %p)\n", store, pPrev);
486 EnterCriticalSection(&ms->cs);
489 listNext = list_next(&ms->certs, &prevEntry->entry);
490 CertFreeCertificateContext((PCCERT_CONTEXT)pPrev);
493 listNext = list_next(&ms->certs, &ms->certs);
496 ret = CryptMemAlloc(sizeof(WINE_CERT_LIST_ENTRY));
499 memcpy(ret, LIST_ENTRY(listNext, WINE_CERT_LIST_ENTRY, entry),
500 sizeof(WINE_CERT_LIST_ENTRY));
501 InterlockedIncrement(&ret->cert.context->ref);
506 SetLastError(CRYPT_E_NOT_FOUND);
509 LeaveCriticalSection(&ms->cs);
511 TRACE("returning %p\n", ret);
512 return (PWINE_CERT_CONTEXT_REF)ret;
515 static void CRYPT_UnrefCertificateContext(PWINE_CERT_CONTEXT_REF ref);
517 static BOOL WINAPI CRYPT_MemDeleteCert(HCERTSTORE hCertStore,
518 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
520 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
521 WINE_CERT_CONTEXT_REF *ref = (WINE_CERT_CONTEXT_REF *)pCertContext;
522 PWINE_CERT_LIST_ENTRY cert, next;
525 /* Find the entry associated with the passed-in context, since the
526 * passed-in context may not be a list entry itself (e.g. if it came from
527 * CertDuplicateCertificateContext.) Pointing to the same context is
528 * a sufficient test of equality.
530 EnterCriticalSection(&store->cs);
531 LIST_FOR_EACH_ENTRY_SAFE(cert, next, &store->certs, WINE_CERT_LIST_ENTRY,
534 if (cert->cert.context == ref->context)
536 TRACE("removing %p\n", cert);
537 /* FIXME: this isn't entirely thread-safe, the entry itself isn't
540 list_remove(&cert->entry);
541 ret = CertFreeCertificateContext((PCCERT_CONTEXT)cert);
542 cert->entry.prev = cert->entry.next = &store->certs;
546 LeaveCriticalSection(&store->cs);
550 static void CRYPT_MemEmptyStore(PWINE_MEMSTORE store)
552 PWINE_CERT_LIST_ENTRY cert, next;
554 EnterCriticalSection(&store->cs);
555 /* Note that CertFreeCertificateContext calls CryptMemFree on the passed-in
556 * pointer if its ref-count reaches zero. That's okay here because there
557 * aren't any allocated data outside of the WINE_CERT_CONTEXT_REF portion
558 * of the CertListEntry.
560 LIST_FOR_EACH_ENTRY_SAFE(cert, next, &store->certs, WINE_CERT_LIST_ENTRY,
563 TRACE("removing %p\n", cert);
564 list_remove(&cert->entry);
565 CertFreeCertificateContext((PCCERT_CONTEXT)cert);
567 LeaveCriticalSection(&store->cs);
570 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
572 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
574 TRACE("(%p, %08lx)\n", store, dwFlags);
576 FIXME("Unimplemented flags: %08lx\n", dwFlags);
578 CRYPT_MemEmptyStore(store);
579 DeleteCriticalSection(&store->cs);
583 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
584 DWORD dwFlags, const void *pvPara)
586 PWINE_MEMSTORE store;
588 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
590 if (dwFlags & CERT_STORE_DELETE_FLAG)
592 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
597 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
600 memset(store, 0, sizeof(WINE_MEMSTORE));
601 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags, StoreTypeMem);
602 store->hdr.closeStore = CRYPT_MemCloseStore;
603 store->hdr.addCert = CRYPT_MemAddCert;
604 store->hdr.createCertRef = CRYPT_CreateCertRef;
605 store->hdr.enumCert = CRYPT_MemEnumCert;
606 store->hdr.deleteCert = CRYPT_MemDeleteCert;
607 store->hdr.freeCert = NULL;
608 store->hdr.control = NULL;
609 InitializeCriticalSection(&store->cs);
610 list_init(&store->certs);
613 return (PWINECRYPT_CERTSTORE)store;
616 static BOOL WINAPI CRYPT_CollectionAddCert(HCERTSTORE store,
617 PCCERT_CONTEXT pCert, DWORD dwAddDisposition)
619 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
620 PWINE_STORE_LIST_ENTRY entry, next;
623 TRACE("(%p, %p, %ld)\n", store, pCert, dwAddDisposition);
626 EnterCriticalSection(&cs->cs);
627 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
630 if (entry->dwUpdateFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
632 ret = entry->store->addCert(entry->store, pCert, dwAddDisposition);
636 LeaveCriticalSection(&cs->cs);
637 SetLastError(ret ? ERROR_SUCCESS : HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED));
641 static PWINE_CERT_CONTEXT_REF CRYPT_CollectionCreateCertRef(
642 PWINE_CERT_CONTEXT context, HCERTSTORE store)
644 PWINE_COLLECTION_CERT_CONTEXT ret = CryptMemAlloc(
645 sizeof(WINE_COLLECTION_CERT_CONTEXT));
649 /* Initialize to empty for now, just make sure the size is right */
650 CRYPT_InitCertRef((PWINE_CERT_CONTEXT_REF)ret, context, store);
651 ret->storeEntry = NULL;
652 ret->childContext = NULL;
654 return (PWINE_CERT_CONTEXT_REF)ret;
657 static void WINAPI CRYPT_CollectionCloseStore(HCERTSTORE store, DWORD dwFlags)
659 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
660 PWINE_STORE_LIST_ENTRY entry, next;
662 TRACE("(%p, %08lx)\n", store, dwFlags);
664 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &cs->stores, WINE_STORE_LIST_ENTRY,
667 TRACE("closing %p\n", entry);
668 CertCloseStore((HCERTSTORE)entry->store, dwFlags);
671 DeleteCriticalSection(&cs->cs);
675 /* Advances a collection enumeration by one cert, if possible, where advancing
677 * - calling the current store's enumeration function once, and returning
678 * the enumerated cert if one is returned
679 * - moving to the next store if the current store has no more items, and
680 * recursively calling itself to get the next item.
681 * Returns NULL if the collection contains no more items or on error.
682 * Assumes the collection store's lock is held.
684 static PWINE_COLLECTION_CERT_CONTEXT CRYPT_CollectionAdvanceEnum(
685 PWINE_COLLECTIONSTORE store, PWINE_STORE_LIST_ENTRY storeEntry,
686 PWINE_COLLECTION_CERT_CONTEXT pPrev)
688 PWINE_COLLECTION_CERT_CONTEXT ret;
689 PWINE_CERT_CONTEXT_REF child;
690 struct list *storeNext = list_next(&store->stores, &storeEntry->entry);
692 TRACE("(%p, %p, %p)\n", store, storeEntry, pPrev);
694 child = storeEntry->store->enumCert((HCERTSTORE)storeEntry->store,
695 pPrev ? pPrev->childContext : NULL);
698 pPrev->childContext = NULL;
699 CertFreeCertificateContext((PCCERT_CONTEXT)pPrev);
704 ret = (PWINE_COLLECTION_CERT_CONTEXT)CRYPT_CollectionCreateCertRef(
705 child->context, store);
708 ret->storeEntry = storeEntry;
709 ret->childContext = child;
712 CertFreeCertificateContext((PCCERT_CONTEXT)child);
718 storeEntry = LIST_ENTRY(storeNext, WINE_STORE_LIST_ENTRY, entry);
719 ret = CRYPT_CollectionAdvanceEnum(store, storeEntry, NULL);
723 SetLastError(CRYPT_E_NOT_FOUND);
727 TRACE("returning %p\n", ret);
731 static PWINE_CERT_CONTEXT_REF CRYPT_CollectionEnumCert(
732 PWINECRYPT_CERTSTORE store, PWINE_CERT_CONTEXT_REF pPrev)
734 PWINE_COLLECTIONSTORE cs = (PWINE_COLLECTIONSTORE)store;
735 PWINE_COLLECTION_CERT_CONTEXT prevEntry =
736 (PWINE_COLLECTION_CERT_CONTEXT)pPrev, ret;
738 TRACE("(%p, %p)\n", store, pPrev);
742 EnterCriticalSection(&cs->cs);
743 ret = CRYPT_CollectionAdvanceEnum(cs, prevEntry->storeEntry, prevEntry);
744 LeaveCriticalSection(&cs->cs);
748 EnterCriticalSection(&cs->cs);
749 if (!list_empty(&cs->stores))
751 PWINE_STORE_LIST_ENTRY storeEntry;
753 storeEntry = LIST_ENTRY(cs->stores.next, WINE_STORE_LIST_ENTRY,
755 ret = CRYPT_CollectionAdvanceEnum(cs, storeEntry, NULL);
759 SetLastError(CRYPT_E_NOT_FOUND);
762 LeaveCriticalSection(&cs->cs);
764 TRACE("returning %p\n", ret);
765 return (PWINE_CERT_CONTEXT_REF)ret;
768 static BOOL WINAPI CRYPT_CollectionDeleteCert(HCERTSTORE hCertStore,
769 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
771 PWINE_COLLECTION_CERT_CONTEXT context =
772 (PWINE_COLLECTION_CERT_CONTEXT)pCertContext;
775 TRACE("(%p, %p, %08lx)\n", hCertStore, pCertContext, dwFlags);
777 ret = CertDeleteCertificateFromStore((PCCERT_CONTEXT)context->childContext);
779 context->childContext = NULL;
783 static void CRYPT_CollectionFreeCert(PWINE_CERT_CONTEXT_REF ref)
785 PWINE_COLLECTION_CERT_CONTEXT context = (PWINE_COLLECTION_CERT_CONTEXT)ref;
787 TRACE("(%p)\n", ref);
789 if (context->childContext)
790 CertFreeCertificateContext((PCCERT_CONTEXT)context->childContext);
793 static WINECRYPT_CERTSTORE *CRYPT_CollectionOpenStore(HCRYPTPROV hCryptProv,
794 DWORD dwFlags, const void *pvPara)
796 PWINE_COLLECTIONSTORE store;
798 if (dwFlags & CERT_STORE_DELETE_FLAG)
800 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
805 store = CryptMemAlloc(sizeof(WINE_COLLECTIONSTORE));
808 memset(store, 0, sizeof(WINE_COLLECTIONSTORE));
809 CRYPT_InitStore(&store->hdr, hCryptProv, dwFlags,
810 StoreTypeCollection);
811 store->hdr.closeStore = CRYPT_CollectionCloseStore;
812 store->hdr.addCert = CRYPT_CollectionAddCert;
813 store->hdr.createCertRef = CRYPT_CollectionCreateCertRef;
814 store->hdr.enumCert = CRYPT_CollectionEnumCert;
815 store->hdr.deleteCert = CRYPT_CollectionDeleteCert;
816 store->hdr.freeCert = CRYPT_CollectionFreeCert;
817 InitializeCriticalSection(&store->cs);
818 list_init(&store->stores);
821 return (PWINECRYPT_CERTSTORE)store;
824 static void WINAPI CRYPT_ProvCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
826 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
828 TRACE("(%p, %08lx)\n", store, dwFlags);
830 if (store->provCloseStore)
831 store->provCloseStore(store->hStoreProv, dwFlags);
832 if (!(store->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG))
833 CertCloseStore(store->memStore, dwFlags);
837 static BOOL WINAPI CRYPT_ProvAddCert(HCERTSTORE hCertStore, PCCERT_CONTEXT cert,
838 DWORD dwAddDisposition)
840 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
843 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwAddDisposition);
845 if (store->hdr.dwOpenFlags & CERT_STORE_READONLY_FLAG)
847 SetLastError(ERROR_ACCESS_DENIED);
853 if (store->provWriteCert)
854 ret = store->provWriteCert(store->hStoreProv, cert,
855 CERT_STORE_PROV_WRITE_ADD_FLAG);
857 ret = store->memStore->addCert(store->memStore, cert,
863 static PWINE_CERT_CONTEXT_REF CRYPT_ProvCreateCertRef(
864 PWINE_CERT_CONTEXT context, HCERTSTORE store)
866 PWINE_PROV_CERT_CONTEXT ret = CryptMemAlloc(sizeof(WINE_PROV_CERT_CONTEXT));
870 CRYPT_InitCertRef((PWINE_CERT_CONTEXT_REF)ret, context, store);
871 ret->childContext = NULL;
873 return (PWINE_CERT_CONTEXT_REF)ret;
876 static PWINE_CERT_CONTEXT_REF CRYPT_ProvEnumCert(PWINECRYPT_CERTSTORE store,
877 PWINE_CERT_CONTEXT_REF pPrev)
879 PWINE_PROVIDERSTORE ps = (PWINE_PROVIDERSTORE)store;
880 PWINE_CERT_CONTEXT_REF child;
881 PWINE_PROV_CERT_CONTEXT prev = (PWINE_PROV_CERT_CONTEXT)pPrev, ret = NULL;
883 TRACE("(%p, %p)\n", store, pPrev);
885 child = ps->memStore->enumCert(ps->memStore, prev ? prev->childContext
889 prev->childContext = NULL;
890 CertFreeCertificateContext((PCCERT_CONTEXT)prev);
895 ret = (PWINE_PROV_CERT_CONTEXT)CRYPT_ProvCreateCertRef(child->context,
898 ret->childContext = child;
900 CertFreeCertificateContext((PCCERT_CONTEXT)child);
902 return (PWINE_CERT_CONTEXT_REF)ret;
905 static BOOL WINAPI CRYPT_ProvDeleteCert(HCERTSTORE hCertStore,
906 PCCERT_CONTEXT cert, DWORD dwFlags)
908 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
911 TRACE("(%p, %p, %08lx)\n", hCertStore, cert, dwFlags);
913 if (store->provDeleteCert)
914 ret = store->provDeleteCert(store->hStoreProv, cert, dwFlags);
916 ret = store->memStore->deleteCert(store->memStore, cert, dwFlags);
920 static void CRYPT_ProvFreeCert(PWINE_CERT_CONTEXT_REF ref)
922 PWINE_PROV_CERT_CONTEXT context = (PWINE_PROV_CERT_CONTEXT)ref;
924 TRACE("(%p)\n", ref);
926 if (context->childContext)
927 CertFreeCertificateContext((PCCERT_CONTEXT)context->childContext);
930 static BOOL WINAPI CRYPT_ProvControl(HCERTSTORE hCertStore, DWORD dwFlags,
931 DWORD dwCtrlType, void const *pvCtrlPara)
933 PWINE_PROVIDERSTORE store = (PWINE_PROVIDERSTORE)hCertStore;
936 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
939 if (store->provControl)
940 ret = store->provControl(store->hStoreProv, dwFlags, dwCtrlType,
945 static PWINECRYPT_CERTSTORE CRYPT_ProvCreateStore(HCRYPTPROV hCryptProv,
946 DWORD dwFlags, PWINECRYPT_CERTSTORE memStore, PCERT_STORE_PROV_INFO pProvInfo)
948 PWINE_PROVIDERSTORE ret = (PWINE_PROVIDERSTORE)CryptMemAlloc(
949 sizeof(WINE_PROVIDERSTORE));
953 CRYPT_InitStore(&ret->hdr, hCryptProv, dwFlags,
955 ret->dwStoreProvFlags = pProvInfo->dwStoreProvFlags;
956 if (ret->dwStoreProvFlags & CERT_STORE_PROV_EXTERNAL_FLAG)
958 CertCloseStore(memStore, 0);
959 ret->memStore = NULL;
962 ret->memStore = memStore;
963 ret->hStoreProv = pProvInfo->hStoreProv;
964 ret->hdr.closeStore = CRYPT_ProvCloseStore;
965 ret->hdr.addCert = CRYPT_ProvAddCert;
966 ret->hdr.createCertRef = CRYPT_ProvCreateCertRef;
967 ret->hdr.enumCert = CRYPT_ProvEnumCert;
968 ret->hdr.deleteCert = CRYPT_ProvDeleteCert;
969 ret->hdr.freeCert = CRYPT_ProvFreeCert;
970 ret->hdr.control = CRYPT_ProvControl;
971 if (pProvInfo->cStoreProvFunc > CERT_STORE_PROV_CLOSE_FUNC)
972 ret->provCloseStore =
973 pProvInfo->rgpvStoreProvFunc[CERT_STORE_PROV_CLOSE_FUNC];
975 ret->provCloseStore = NULL;
976 if (pProvInfo->cStoreProvFunc >
977 CERT_STORE_PROV_WRITE_CERT_FUNC)
978 ret->provWriteCert = pProvInfo->rgpvStoreProvFunc[
979 CERT_STORE_PROV_WRITE_CERT_FUNC];
981 ret->provWriteCert = NULL;
982 if (pProvInfo->cStoreProvFunc >
983 CERT_STORE_PROV_DELETE_CERT_FUNC)
984 ret->provDeleteCert = pProvInfo->rgpvStoreProvFunc[
985 CERT_STORE_PROV_DELETE_CERT_FUNC];
987 ret->provDeleteCert = NULL;
988 if (pProvInfo->cStoreProvFunc >
989 CERT_STORE_PROV_CONTROL_FUNC)
990 ret->provControl = pProvInfo->rgpvStoreProvFunc[
991 CERT_STORE_PROV_CONTROL_FUNC];
993 ret->provControl = NULL;
995 return (PWINECRYPT_CERTSTORE)ret;
998 static PWINECRYPT_CERTSTORE CRYPT_ProvOpenStore(LPCSTR lpszStoreProvider,
999 DWORD dwEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
1001 static HCRYPTOIDFUNCSET set = NULL;
1002 PFN_CERT_DLL_OPEN_STORE_PROV_FUNC provOpenFunc;
1003 HCRYPTOIDFUNCADDR hFunc;
1004 PWINECRYPT_CERTSTORE ret = NULL;
1007 set = CryptInitOIDFunctionSet(CRYPT_OID_OPEN_STORE_PROV_FUNC, 0);
1008 CryptGetOIDFunctionAddress(set, dwEncodingType, lpszStoreProvider, 0,
1009 (void **)&provOpenFunc, &hFunc);
1012 CERT_STORE_PROV_INFO provInfo = { 0 };
1014 provInfo.cbSize = sizeof(provInfo);
1015 if (dwFlags & CERT_STORE_DELETE_FLAG)
1016 provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
1017 dwFlags, pvPara, NULL, &provInfo);
1020 PWINECRYPT_CERTSTORE memStore;
1022 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1025 if (provOpenFunc(lpszStoreProvider, dwEncodingType, hCryptProv,
1026 dwFlags, pvPara, memStore, &provInfo))
1027 ret = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1030 CertCloseStore(memStore, 0);
1033 CryptFreeOIDFunctionAddress(hFunc, 0);
1036 SetLastError(ERROR_FILE_NOT_FOUND);
1040 static void CRYPT_HashToStr(LPBYTE hash, LPWSTR asciiHash)
1042 static const WCHAR fmt[] = { '%','0','2','X',0 };
1048 for (i = 0; i < 20; i++)
1049 wsprintfW(asciiHash + i * 2, fmt, hash[i]);
1052 static const WCHAR CertsW[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
1054 static const WCHAR CRLsW[] = { 'C','R','L','s',0 };
1055 static const WCHAR CTLsW[] = { 'C','T','L','s',0 };
1056 static const WCHAR BlobW[] = { 'B','l','o','b',0 };
1058 static void CRYPT_RegReadSerializedFromReg(PWINE_REGSTOREINFO store, HKEY key,
1063 WCHAR subKeyName[MAX_PATH];
1066 DWORD size = sizeof(subKeyName) / sizeof(WCHAR);
1068 rc = RegEnumKeyExW(key, index++, subKeyName, &size, NULL, NULL, NULL,
1074 rc = RegOpenKeyExW(key, subKeyName, 0, KEY_READ, &subKey);
1080 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, NULL, &size);
1082 buf = CryptMemAlloc(size);
1085 rc = RegQueryValueExW(subKey, BlobW, NULL, NULL, buf,
1089 const void *context;
1092 TRACE("Adding cert with hash %s\n",
1093 debugstr_w(subKeyName));
1094 context = CRYPT_ReadSerializedElement(buf, size,
1095 contextType, &addedType);
1098 const WINE_CONTEXT_INTERFACE *contextInterface;
1103 case CERT_STORE_CERTIFICATE_CONTEXT:
1104 contextInterface = &gCertInterface;
1106 case CERT_STORE_CRL_CONTEXT:
1107 contextInterface = &gCRLInterface;
1109 case CERT_STORE_CTL_CONTEXT:
1110 contextInterface = &gCTLInterface;
1113 contextInterface = NULL;
1115 if (contextInterface)
1117 size = sizeof(hash);
1118 if (contextInterface->getProp(context,
1119 CERT_HASH_PROP_ID, hash, &size))
1121 WCHAR asciiHash[20 * 2 + 1];
1123 CRYPT_HashToStr(hash, asciiHash);
1124 TRACE("comparing %s\n",
1125 debugstr_w(asciiHash));
1126 TRACE("with %s\n", debugstr_w(subKeyName));
1127 if (!lstrcmpW(asciiHash, subKeyName))
1129 TRACE("hash matches, adding\n");
1130 contextInterface->addContextToStore(
1131 store->memStore, context,
1132 CERT_STORE_ADD_REPLACE_EXISTING, NULL);
1135 TRACE("hash doesn't match, ignoring\n");
1137 contextInterface->free(context);
1143 RegCloseKey(subKey);
1145 /* Ignore intermediate errors, continue enumerating */
1151 static void CRYPT_RegReadFromReg(PWINE_REGSTOREINFO store)
1153 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
1154 static const DWORD contextFlags[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG,
1155 CERT_STORE_CRL_CONTEXT_FLAG, CERT_STORE_CTL_CONTEXT_FLAG };
1158 for (i = 0; i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1163 rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0, KEY_READ, NULL,
1167 CRYPT_RegReadSerializedFromReg(store, key, contextFlags[i]);
1173 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
1174 static BOOL CRYPT_WriteSerializedToReg(HKEY key, LPBYTE hash, LPBYTE buf,
1177 WCHAR asciiHash[20 * 2 + 1];
1182 CRYPT_HashToStr(hash, asciiHash);
1183 rc = RegCreateKeyExW(key, asciiHash, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
1187 rc = RegSetValueExW(subKey, BlobW, 0, REG_BINARY, buf, len);
1188 RegCloseKey(subKey);
1200 static BOOL CRYPT_SerializeContextsToReg(HKEY key,
1201 const WINE_CONTEXT_INTERFACE *contextInterface, HCERTSTORE memStore)
1203 const void *context = NULL;
1207 context = contextInterface->enumContextsInStore(memStore, context);
1211 DWORD hashSize = sizeof(hash);
1213 ret = contextInterface->getProp(context, CERT_HASH_PROP_ID, hash,
1220 ret = contextInterface->serialize(context, 0, NULL, &size);
1222 buf = CryptMemAlloc(size);
1225 ret = contextInterface->serialize(context, 0, buf, &size);
1227 ret = CRYPT_WriteSerializedToReg(key, hash, buf, size);
1234 } while (ret && context != NULL);
1236 contextInterface->free(context);
1240 static BOOL CRYPT_RegWriteToReg(PWINE_REGSTOREINFO store)
1242 static const WCHAR *subKeys[] = { CertsW, CRLsW, CTLsW };
1243 static const WINE_CONTEXT_INTERFACE *interfaces[] = { &gCertInterface,
1244 &gCRLInterface, &gCTLInterface };
1245 struct list *listToDelete[] = { &store->certsToDelete, NULL, NULL };
1249 for (i = 0; ret && i < sizeof(subKeys) / sizeof(subKeys[0]); i++)
1252 LONG rc = RegCreateKeyExW(store->key, subKeys[i], 0, NULL, 0,
1253 KEY_ALL_ACCESS, NULL, &key, NULL);
1257 if (listToDelete[i])
1259 PWINE_HASH_TO_DELETE toDelete, next;
1260 WCHAR asciiHash[20 * 2 + 1];
1262 EnterCriticalSection(&store->cs);
1263 LIST_FOR_EACH_ENTRY_SAFE(toDelete, next, listToDelete[i],
1264 WINE_HASH_TO_DELETE, entry)
1268 CRYPT_HashToStr(toDelete->hash, asciiHash);
1269 TRACE("Removing %s\n", debugstr_w(asciiHash));
1270 rc = RegDeleteKeyW(key, asciiHash);
1271 if (rc != ERROR_SUCCESS && rc != ERROR_FILE_NOT_FOUND)
1276 list_remove(&toDelete->entry);
1277 CryptMemFree(toDelete);
1279 LeaveCriticalSection(&store->cs);
1281 ret = CRYPT_SerializeContextsToReg(key, interfaces[i],
1294 /* If force is true or the registry store is dirty, writes the contents of the
1295 * store to the registry.
1297 static BOOL CRYPT_RegFlushStore(PWINE_REGSTOREINFO store, BOOL force)
1301 if (store->dirty || force)
1302 ret = CRYPT_RegWriteToReg(store);
1308 static void WINAPI CRYPT_RegCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1310 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1312 TRACE("(%p, %08lx)\n", store, dwFlags);
1314 FIXME("Unimplemented flags: %08lx\n", dwFlags);
1316 CRYPT_RegFlushStore(store, FALSE);
1317 RegCloseKey(store->key);
1318 DeleteCriticalSection(&store->cs);
1319 CryptMemFree(store);
1322 static BOOL WINAPI CRYPT_RegWriteCert(HCERTSTORE hCertStore,
1323 PCCERT_CONTEXT cert, DWORD dwFlags)
1325 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1328 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
1330 if (dwFlags & CERT_STORE_PROV_WRITE_ADD_FLAG)
1332 store->dirty = TRUE;
1340 static BOOL WINAPI CRYPT_RegDeleteCert(HCERTSTORE hCertStore,
1341 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
1343 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1346 TRACE("(%p, %p, %08lx)\n", store, pCertContext, dwFlags);
1348 if (store->dwOpenFlags & CERT_STORE_READONLY_FLAG)
1350 SetLastError(ERROR_ACCESS_DENIED);
1355 PWINE_HASH_TO_DELETE toDelete =
1356 CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE));
1360 DWORD size = sizeof(toDelete->hash);
1362 ret = CertGetCertificateContextProperty(pCertContext,
1363 CERT_HASH_PROP_ID, toDelete->hash, &size);
1366 list_init(&toDelete->entry);
1367 EnterCriticalSection(&store->cs);
1368 list_add_tail(&store->certsToDelete, &toDelete->entry);
1369 LeaveCriticalSection(&store->cs);
1372 CryptMemFree(toDelete);
1377 store->dirty = TRUE;
1382 static BOOL WINAPI CRYPT_RegControl(HCERTSTORE hCertStore, DWORD dwFlags,
1383 DWORD dwCtrlType, void const *pvCtrlPara)
1385 PWINE_REGSTOREINFO store = (PWINE_REGSTOREINFO)hCertStore;
1390 case CERT_STORE_CTRL_RESYNC:
1391 CRYPT_RegFlushStore(store, FALSE);
1392 CRYPT_MemEmptyStore((PWINE_MEMSTORE)store->memStore);
1393 CRYPT_RegReadFromReg(store);
1396 case CERT_STORE_CTRL_COMMIT:
1397 ret = CRYPT_RegFlushStore(store,
1398 dwFlags & CERT_STORE_CTRL_COMMIT_FORCE_FLAG);
1401 FIXME("%ld: stub\n", dwCtrlType);
1407 /* Copied from shlwapi's SHDeleteKeyW, and reformatted to match this file. */
1408 static DWORD CRYPT_RecurseDeleteKey(HKEY hKey, LPCWSTR lpszSubKey)
1410 DWORD dwRet, dwKeyCount = 0, dwMaxSubkeyLen = 0, dwSize, i;
1411 WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
1414 TRACE("(hkey=%p,%s)\n", hKey, debugstr_w(lpszSubKey));
1416 dwRet = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
1419 /* Find how many subkeys there are */
1420 dwRet = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, &dwKeyCount,
1421 &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
1425 if (dwMaxSubkeyLen > sizeof(szNameBuf)/sizeof(WCHAR))
1427 /* Name too big: alloc a buffer for it */
1428 lpszName = CryptMemAlloc(dwMaxSubkeyLen*sizeof(WCHAR));
1432 dwRet = ERROR_NOT_ENOUGH_MEMORY;
1435 /* Recursively delete all the subkeys */
1436 for (i = 0; i < dwKeyCount && !dwRet; i++)
1438 dwSize = dwMaxSubkeyLen;
1439 dwRet = RegEnumKeyExW(hSubKey, i, lpszName, &dwSize, NULL,
1442 dwRet = CRYPT_RecurseDeleteKey(hSubKey, lpszName);
1445 if (lpszName != szNameBuf)
1447 /* Free buffer if allocated */
1448 CryptMemFree(lpszName);
1453 RegCloseKey(hSubKey);
1455 dwRet = RegDeleteKeyW(hKey, lpszSubKey);
1460 static void *regProvFuncs[] = {
1461 CRYPT_RegCloseStore,
1462 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
1464 CRYPT_RegDeleteCert,
1465 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
1466 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
1467 NULL, /* CERT_STORE_PROV_WRITE_CRL_FUNC */
1468 NULL, /* CERT_STORE_PROV_DELETE_CRL_FUNC */
1469 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
1470 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
1471 NULL, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
1472 NULL, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
1473 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
1477 static WINECRYPT_CERTSTORE *CRYPT_RegOpenStore(HCRYPTPROV hCryptProv,
1478 DWORD dwFlags, const void *pvPara)
1480 PWINECRYPT_CERTSTORE store = NULL;
1482 TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
1484 if (dwFlags & CERT_STORE_DELETE_FLAG)
1486 DWORD rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CertsW);
1488 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1489 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CRLsW);
1490 if (rc == ERROR_SUCCESS || rc == ERROR_NO_MORE_ITEMS)
1491 rc = CRYPT_RecurseDeleteKey((HKEY)pvPara, CTLsW);
1492 if (rc == ERROR_NO_MORE_ITEMS)
1500 if (DuplicateHandle(GetCurrentProcess(), (HANDLE)pvPara,
1501 GetCurrentProcess(), (LPHANDLE)&key,
1502 dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ : KEY_ALL_ACCESS,
1505 PWINECRYPT_CERTSTORE memStore;
1507 memStore = CRYPT_MemOpenStore(hCryptProv, dwFlags, NULL);
1510 PWINE_REGSTOREINFO regInfo = CryptMemAlloc(
1511 sizeof(WINE_REGSTOREINFO));
1515 CERT_STORE_PROV_INFO provInfo = { 0 };
1517 regInfo->dwOpenFlags = dwFlags;
1518 regInfo->cryptProv = hCryptProv;
1519 regInfo->memStore = memStore;
1521 InitializeCriticalSection(®Info->cs);
1522 list_init(®Info->certsToDelete);
1523 CRYPT_RegReadFromReg(regInfo);
1524 regInfo->dirty = FALSE;
1525 provInfo.cbSize = sizeof(provInfo);
1526 provInfo.cStoreProvFunc = sizeof(regProvFuncs) /
1527 sizeof(regProvFuncs[0]);
1528 provInfo.rgpvStoreProvFunc = regProvFuncs;
1529 provInfo.hStoreProv = regInfo;
1530 store = CRYPT_ProvCreateStore(hCryptProv, dwFlags, memStore,
1536 TRACE("returning %p\n", store);
1540 /* FIXME: this isn't complete for the Root store, in which the top-level
1541 * self-signed CA certs reside. Adding a cert to the Root store should present
1542 * the user with a dialog indicating the consequences of doing so, and asking
1543 * the user to confirm whether the cert should be added.
1545 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
1546 DWORD dwFlags, const void *pvPara)
1548 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
1549 LPCWSTR storeName = (LPCWSTR)pvPara;
1551 PWINECRYPT_CERTSTORE store = NULL;
1556 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1557 debugstr_w((LPCWSTR)pvPara));
1561 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1566 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1568 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1569 root = HKEY_LOCAL_MACHINE;
1570 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1572 case CERT_SYSTEM_STORE_CURRENT_USER:
1573 root = HKEY_CURRENT_USER;
1574 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1576 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1577 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1578 * SystemCertificates
1580 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
1581 debugstr_w(storeName));
1583 case CERT_SYSTEM_STORE_SERVICES:
1584 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1585 * SystemCertificates
1587 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
1588 debugstr_w(storeName));
1590 case CERT_SYSTEM_STORE_USERS:
1591 /* hku\user sid\Software\Microsoft\SystemCertificates */
1592 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
1593 debugstr_w(storeName));
1595 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1596 root = HKEY_CURRENT_USER;
1597 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1599 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1600 root = HKEY_LOCAL_MACHINE;
1601 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1603 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1604 /* hklm\Software\Microsoft\EnterpriseCertificates */
1605 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
1606 debugstr_w(storeName));
1609 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1613 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
1619 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
1622 wsprintfW(storePath, fmt, base, storeName);
1623 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
1624 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
1629 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
1631 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
1632 disp == REG_OPENED_EXISTING_KEY)
1635 rc = ERROR_FILE_EXISTS;
1640 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
1645 CryptMemFree(storePath);
1650 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
1651 DWORD dwFlags, const void *pvPara)
1654 PWINECRYPT_CERTSTORE ret = NULL;
1656 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1657 debugstr_a((LPCSTR)pvPara));
1661 SetLastError(ERROR_FILE_NOT_FOUND);
1664 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1667 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1671 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1672 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
1673 CryptMemFree(storeName);
1679 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
1680 DWORD dwFlags, const void *pvPara)
1682 HCERTSTORE store = 0;
1685 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1686 debugstr_w((LPCWSTR)pvPara));
1690 SetLastError(ERROR_FILE_NOT_FOUND);
1693 /* This returns a different error than system registry stores if the
1694 * location is invalid.
1696 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1698 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1699 case CERT_SYSTEM_STORE_CURRENT_USER:
1700 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1701 case CERT_SYSTEM_STORE_SERVICES:
1702 case CERT_SYSTEM_STORE_USERS:
1703 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1704 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1705 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1709 SetLastError(ERROR_FILE_NOT_FOUND);
1714 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1715 0, hCryptProv, dwFlags, pvPara);
1719 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1720 CERT_STORE_CREATE_NEW_FLAG, NULL);
1723 CertAddStoreToCollection(store, regStore,
1724 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
1725 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1726 CertCloseStore(regStore, 0);
1730 return (PWINECRYPT_CERTSTORE)store;
1733 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
1734 DWORD dwFlags, const void *pvPara)
1737 PWINECRYPT_CERTSTORE ret = NULL;
1739 TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
1740 debugstr_a((LPCSTR)pvPara));
1744 SetLastError(ERROR_FILE_NOT_FOUND);
1747 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
1750 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
1754 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
1755 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
1756 CryptMemFree(storeName);
1762 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
1763 DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
1766 WINECRYPT_CERTSTORE *hcs;
1767 StoreOpenFunc openFunc = NULL;
1769 TRACE("(%s, %08lx, %08lx, %08lx, %p)\n", debugstr_a(lpszStoreProvider),
1770 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
1772 if (!HIWORD(lpszStoreProvider))
1774 switch (LOWORD(lpszStoreProvider))
1776 case (int)CERT_STORE_PROV_MEMORY:
1777 openFunc = CRYPT_MemOpenStore;
1779 case (int)CERT_STORE_PROV_REG:
1780 openFunc = CRYPT_RegOpenStore;
1782 case (int)CERT_STORE_PROV_COLLECTION:
1783 openFunc = CRYPT_CollectionOpenStore;
1785 case (int)CERT_STORE_PROV_SYSTEM_A:
1786 openFunc = CRYPT_SysOpenStoreA;
1788 case (int)CERT_STORE_PROV_SYSTEM_W:
1789 openFunc = CRYPT_SysOpenStoreW;
1791 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
1792 openFunc = CRYPT_SysRegOpenStoreA;
1794 case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_W:
1795 openFunc = CRYPT_SysRegOpenStoreW;
1798 if (LOWORD(lpszStoreProvider))
1799 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
1802 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
1803 openFunc = CRYPT_MemOpenStore;
1804 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
1805 openFunc = CRYPT_SysOpenStoreW;
1806 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
1807 openFunc = CRYPT_CollectionOpenStore;
1808 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
1809 openFunc = CRYPT_SysRegOpenStoreW;
1812 FIXME("unimplemented type %s\n", lpszStoreProvider);
1817 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
1818 hCryptProv, dwFlags, pvPara);
1820 hcs = openFunc(hCryptProv, dwFlags, pvPara);
1821 return (HCERTSTORE)hcs;
1824 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
1825 LPCSTR szSubSystemProtocol)
1829 if (szSubSystemProtocol)
1831 int len = MultiByteToWideChar(CP_ACP, 0, szSubSystemProtocol, -1, NULL,
1833 LPWSTR param = CryptMemAlloc(len * sizeof(WCHAR));
1837 MultiByteToWideChar(CP_ACP, 0, szSubSystemProtocol, -1, param, len);
1838 ret = CertOpenSystemStoreW(hProv, param);
1839 CryptMemFree(param);
1843 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1847 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
1848 LPCWSTR szSubSystemProtocol)
1852 if (!szSubSystemProtocol)
1854 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
1858 /* FIXME: needs some tests. It seems to open both HKEY_LOCAL_MACHINE and
1859 * HKEY_CURRENT_USER stores, but I'm not sure under what conditions, if any,
1862 ret = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, hProv,
1863 CERT_STORE_CREATE_NEW_FLAG, NULL);
1866 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1867 0, hProv, CERT_SYSTEM_STORE_LOCAL_MACHINE, szSubSystemProtocol);
1871 CertAddStoreToCollection(ret, store,
1872 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1873 CertCloseStore(store, 0);
1875 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
1876 0, hProv, CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
1879 CertAddStoreToCollection(ret, store,
1880 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
1881 CertCloseStore(store, 0);
1887 BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,
1888 DWORD dwSaveAs, DWORD dwSaveTo, void* pvSaveToPara, DWORD dwFlags)
1890 FIXME("(%p,%ld,%ld,%ld,%p,%08lx) stub!\n", hCertStore,
1891 dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags);
1895 PCCRL_CONTEXT WINAPI CertCreateCRLContext( DWORD dwCertEncodingType,
1896 const BYTE* pbCrlEncoded, DWORD cbCrlEncoded)
1901 TRACE("%08lx %p %08lx\n", dwCertEncodingType, pbCrlEncoded, cbCrlEncoded);
1903 /* FIXME: semi-stub, need to use CryptDecodeObjectEx to decode the CRL. */
1904 pcrl = CryptMemAlloc( sizeof (CRL_CONTEXT) );
1908 data = CryptMemAlloc( cbCrlEncoded );
1911 CryptMemFree( pcrl );
1915 pcrl->dwCertEncodingType = dwCertEncodingType;
1916 pcrl->pbCrlEncoded = data;
1917 pcrl->cbCrlEncoded = cbCrlEncoded;
1918 pcrl->pCrlInfo = NULL;
1919 pcrl->hCertStore = 0;
1924 /* Decodes the encoded certificate and creates the certificate context for it.
1925 * The reference count is initially zero, so you must create a reference to it
1926 * to avoid leaking memory.
1928 static PWINE_CERT_CONTEXT CRYPT_CreateCertificateContext(
1929 DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded)
1931 PWINE_CERT_CONTEXT cert = NULL;
1933 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
1934 PCERT_INFO certInfo = NULL;
1937 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCertEncoded,
1940 /* First try to decode it as a signed cert. */
1941 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT, pbCertEncoded,
1942 cbCertEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&signedCert, &size);
1946 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1947 signedCert->ToBeSigned.pbData, signedCert->ToBeSigned.cbData,
1948 CRYPT_DECODE_ALLOC_FLAG, NULL, (BYTE *)&certInfo, &size);
1949 LocalFree(signedCert);
1951 /* Failing that, try it as an unsigned cert */
1955 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_TO_BE_SIGNED,
1956 pbCertEncoded, cbCertEncoded,
1957 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
1958 (BYTE *)&certInfo, &size);
1964 cert = CryptMemAlloc(sizeof(WINE_CERT_CONTEXT));
1967 data = CryptMemAlloc(cbCertEncoded);
1974 memcpy(data, pbCertEncoded, cbCertEncoded);
1975 cert->cert.dwCertEncodingType = dwCertEncodingType;
1976 cert->cert.pbCertEncoded = data;
1977 cert->cert.cbCertEncoded = cbCertEncoded;
1978 cert->cert.pCertInfo = certInfo;
1979 cert->cert.hCertStore = 0;
1981 InitializeCriticalSection(&cert->cs);
1982 list_init(&cert->extendedProperties);
1989 static void CRYPT_FreeCert(PWINE_CERT_CONTEXT context)
1991 PWINE_CERT_PROPERTY prop, next;
1993 CryptMemFree(context->cert.pbCertEncoded);
1994 LocalFree(context->cert.pCertInfo);
1995 DeleteCriticalSection(&context->cs);
1996 LIST_FOR_EACH_ENTRY_SAFE(prop, next, &context->extendedProperties,
1997 WINE_CERT_PROPERTY, entry)
1999 list_remove(&prop->entry);
2000 CryptMemFree(prop->pbData);
2003 CryptMemFree(context);
2006 PCCERT_CONTEXT WINAPI CertCreateCertificateContext(DWORD dwCertEncodingType,
2007 const BYTE *pbCertEncoded, DWORD cbCertEncoded)
2009 PWINE_CERT_CONTEXT cert;
2010 PWINE_CERT_CONTEXT_REF ret = NULL;
2012 TRACE("(%08lx, %p, %ld)\n", dwCertEncodingType, pbCertEncoded,
2015 cert = CRYPT_CreateCertificateContext(dwCertEncodingType, pbCertEncoded,
2018 ret = CRYPT_CreateCertRef(cert, 0);
2019 return (PCCERT_CONTEXT)ret;
2022 /* Since the properties are stored in a list, this is a tad inefficient
2023 * (O(n^2)) since I have to find the previous position every time.
2025 DWORD WINAPI CertEnumCertificateContextProperties(PCCERT_CONTEXT pCertContext,
2028 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
2031 TRACE("(%p, %ld)\n", pCertContext, dwPropId);
2033 EnterCriticalSection(&ref->context->cs);
2036 PWINE_CERT_PROPERTY cursor = NULL;
2038 LIST_FOR_EACH_ENTRY(cursor, &ref->context->extendedProperties,
2039 WINE_CERT_PROPERTY, entry)
2041 if (cursor->hdr.propID == dwPropId)
2046 if (cursor->entry.next != &ref->context->extendedProperties)
2047 ret = LIST_ENTRY(cursor->entry.next, WINE_CERT_PROPERTY,
2055 else if (!list_empty(&ref->context->extendedProperties))
2056 ret = LIST_ENTRY(ref->context->extendedProperties.next,
2057 WINE_CERT_PROPERTY, entry)->hdr.propID;
2060 LeaveCriticalSection(&ref->context->cs);
2064 static BOOL CRYPT_GetCertHashProp(PWINE_CERT_CONTEXT context, DWORD dwPropId,
2065 ALG_ID algID, const BYTE *toHash, DWORD toHashLen, void *pvData,
2068 BOOL ret = CryptHashCertificate(0, algID, 0, toHash, toHashLen, pvData,
2072 CRYPT_DATA_BLOB blob = { *pcbData, pvData };
2074 ret = CRYPT_SetCertificateContextProperty(context, dwPropId,
2080 static BOOL WINAPI CRYPT_GetCertificateContextProperty(
2081 PWINE_CERT_CONTEXT context, DWORD dwPropId, void *pvData, DWORD *pcbData)
2083 PWINE_CERT_PROPERTY prop;
2086 TRACE("(%p, %ld, %p, %p)\n", context, dwPropId, pvData, pcbData);
2088 EnterCriticalSection(&context->cs);
2091 LIST_FOR_EACH_ENTRY(prop, &context->extendedProperties,
2092 WINE_CERT_PROPERTY, entry)
2094 if (prop->hdr.propID == dwPropId)
2098 *pcbData = prop->hdr.cb;
2101 else if (*pcbData < prop->hdr.cb)
2103 SetLastError(ERROR_MORE_DATA);
2104 *pcbData = prop->hdr.cb;
2108 memcpy(pvData, prop->pbData, prop->hdr.cb);
2109 *pcbData = prop->hdr.cb;
2118 /* Implicit properties */
2121 case CERT_SHA1_HASH_PROP_ID:
2122 ret = CRYPT_GetCertHashProp(context, dwPropId, CALG_SHA1,
2123 context->cert.pbCertEncoded, context->cert.cbCertEncoded, pvData,
2126 case CERT_MD5_HASH_PROP_ID:
2127 ret = CRYPT_GetCertHashProp(context, dwPropId, CALG_MD5,
2128 context->cert.pbCertEncoded, context->cert.cbCertEncoded, pvData,
2131 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
2132 ret = CRYPT_GetCertHashProp(context, dwPropId, CALG_MD5,
2133 context->cert.pCertInfo->Subject.pbData,
2134 context->cert.pCertInfo->Subject.cbData,
2137 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
2138 ret = CRYPT_GetCertHashProp(context, dwPropId, CALG_MD5,
2139 context->cert.pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
2140 context->cert.pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
2143 case CERT_SIGNATURE_HASH_PROP_ID:
2144 case CERT_ISSUER_SERIAL_NUMBER_MD5_HASH_PROP_ID:
2145 FIXME("implicit property %ld\n", dwPropId);
2146 SetLastError(CRYPT_E_NOT_FOUND);
2149 SetLastError(CRYPT_E_NOT_FOUND);
2152 LeaveCriticalSection(&context->cs);
2153 TRACE("returning %d\n", ret);
2157 BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
2158 DWORD dwPropId, void *pvData, DWORD *pcbData)
2160 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
2163 TRACE("(%p, %ld, %p, %p)\n", pCertContext, dwPropId, pvData, pcbData);
2165 /* Special cases for invalid/special prop IDs.
2170 case CERT_CERT_PROP_ID:
2171 case CERT_CRL_PROP_ID:
2172 case CERT_CTL_PROP_ID:
2173 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2175 case CERT_ACCESS_STATE_PROP_ID:
2178 *pcbData = sizeof(DWORD);
2181 else if (*pcbData < sizeof(DWORD))
2183 SetLastError(ERROR_MORE_DATA);
2184 *pcbData = sizeof(DWORD);
2191 if (pCertContext->hCertStore)
2193 PWINECRYPT_CERTSTORE store =
2194 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2196 if (!(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
2197 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
2199 *(DWORD *)pvData = state;
2204 ret = CRYPT_GetCertificateContextProperty(ref->context, dwPropId,
2206 TRACE("returning %d\n", ret);
2210 /* Copies cbData bytes from pbData to the context's property with ID
2213 static BOOL CRYPT_SaveCertificateContextProperty(PWINE_CERT_CONTEXT context,
2214 DWORD dwPropId, const BYTE *pbData, size_t cbData)
2221 data = CryptMemAlloc(cbData);
2223 memcpy(data, pbData, cbData);
2227 if (!cbData || data)
2229 PWINE_CERT_PROPERTY prop;
2231 EnterCriticalSection(&context->cs);
2232 LIST_FOR_EACH_ENTRY(prop, &context->extendedProperties,
2233 WINE_CERT_PROPERTY, entry)
2235 if (prop->hdr.propID == dwPropId)
2238 if (prop && prop->entry.next != &context->extendedProperties)
2240 CryptMemFree(prop->pbData);
2241 prop->hdr.cb = cbData;
2242 prop->pbData = data;
2247 prop = CryptMemAlloc(sizeof(WINE_CERT_PROPERTY));
2250 prop->hdr.propID = dwPropId;
2251 prop->hdr.unknown = 1;
2252 prop->hdr.cb = cbData;
2253 list_init(&prop->entry);
2254 prop->pbData = data;
2255 list_add_tail(&context->extendedProperties, &prop->entry);
2261 LeaveCriticalSection(&context->cs);
2266 static BOOL WINAPI CRYPT_SetCertificateContextProperty(
2267 PWINE_CERT_CONTEXT context, DWORD dwPropId, DWORD dwFlags, const void *pvData)
2271 TRACE("(%p, %ld, %08lx, %p)\n", context, dwPropId, dwFlags, pvData);
2275 PWINE_CERT_PROPERTY prop, next;
2277 EnterCriticalSection(&context->cs);
2278 LIST_FOR_EACH_ENTRY_SAFE(prop, next, &context->extendedProperties,
2279 WINE_CERT_PROPERTY, entry)
2281 if (prop->hdr.propID == dwPropId)
2283 list_remove(&prop->entry);
2284 CryptMemFree(prop->pbData);
2288 LeaveCriticalSection(&context->cs);
2295 case CERT_AUTO_ENROLL_PROP_ID:
2296 case CERT_CTL_USAGE_PROP_ID:
2297 case CERT_DESCRIPTION_PROP_ID:
2298 case CERT_FRIENDLY_NAME_PROP_ID:
2299 case CERT_HASH_PROP_ID:
2300 case CERT_KEY_IDENTIFIER_PROP_ID:
2301 case CERT_MD5_HASH_PROP_ID:
2302 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
2303 case CERT_PUBKEY_ALG_PARA_PROP_ID:
2304 case CERT_PVK_FILE_PROP_ID:
2305 case CERT_SIGNATURE_HASH_PROP_ID:
2306 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
2307 case CERT_SUBJECT_NAME_MD5_HASH_PROP_ID:
2308 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
2309 case CERT_ENROLLMENT_PROP_ID:
2310 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
2311 case CERT_RENEWAL_PROP_ID:
2313 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvData;
2315 ret = CRYPT_SaveCertificateContextProperty(context, dwPropId,
2316 blob->pbData, blob->cbData);
2319 case CERT_DATE_STAMP_PROP_ID:
2320 ret = CRYPT_SaveCertificateContextProperty(context, dwPropId,
2321 pvData, sizeof(FILETIME));
2324 FIXME("%ld: stub\n", dwPropId);
2327 TRACE("returning %d\n", ret);
2331 BOOL WINAPI CertSetCertificateContextProperty(PCCERT_CONTEXT pCertContext,
2332 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2334 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
2337 TRACE("(%p, %ld, %08lx, %p)\n", pCertContext, dwPropId, dwFlags, pvData);
2339 /* Handle special cases for "read-only"/invalid prop IDs. Windows just
2340 * crashes on most of these, I'll be safer.
2345 case CERT_ACCESS_STATE_PROP_ID:
2346 case CERT_CERT_PROP_ID:
2347 case CERT_CRL_PROP_ID:
2348 case CERT_CTL_PROP_ID:
2349 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2352 ret = CRYPT_SetCertificateContextProperty(ref->context, dwPropId,
2354 TRACE("returning %d\n", ret);
2358 /* Only the reference portion of the context is duplicated. The returned
2359 * context has the cert store set to 0, to prevent the store's certificate free
2360 * function from getting called on partial data.
2361 * FIXME: is this okay? Needs a test.
2363 PCCERT_CONTEXT WINAPI CertDuplicateCertificateContext(
2364 PCCERT_CONTEXT pCertContext)
2366 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext, ret;
2368 TRACE("(%p)\n", pCertContext);
2371 ret = CryptMemAlloc(sizeof(WINE_CERT_CONTEXT_REF));
2374 memcpy(ret, ref, sizeof(*ret));
2375 ret->cert.hCertStore = 0;
2376 InterlockedIncrement(&ret->context->ref);
2381 return (PCCERT_CONTEXT)ret;
2384 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
2385 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
2386 PCCERT_CONTEXT *ppStoreContext)
2388 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
2389 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
2390 PWINE_CERT_CONTEXT cert;
2393 TRACE("(%p, %p, %08lx, %p)\n", hCertStore, pCertContext,
2394 dwAddDisposition, ppStoreContext);
2396 /* FIXME: some tests needed to verify return codes */
2399 SetLastError(ERROR_INVALID_PARAMETER);
2402 if (store->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2404 SetLastError(ERROR_INVALID_PARAMETER);
2408 cert = CRYPT_CreateCertificateContext(ref->context->cert.dwCertEncodingType,
2409 ref->context->cert.pbCertEncoded, ref->context->cert.cbCertEncoded);
2412 PWINE_CERT_PROPERTY prop;
2415 EnterCriticalSection(&ref->context->cs);
2416 LIST_FOR_EACH_ENTRY(prop, &ref->context->extendedProperties,
2417 WINE_CERT_PROPERTY, entry)
2419 ret = CRYPT_SaveCertificateContextProperty(cert, prop->hdr.propID,
2420 prop->pbData, prop->hdr.cb);
2424 LeaveCriticalSection(&ref->context->cs);
2427 ret = store->addCert(store, (PCCERT_CONTEXT)cert, dwAddDisposition);
2428 if (ret && ppStoreContext)
2429 *ppStoreContext = (PCCERT_CONTEXT)store->createCertRef(cert,
2433 CRYPT_FreeCert(cert);
2440 BOOL WINAPI CertAddEncodedCertificateToStore(HCERTSTORE hCertStore,
2441 DWORD dwCertEncodingType, const BYTE *pbCertEncoded, DWORD cbCertEncoded,
2442 DWORD dwAddDisposition, PCCERT_CONTEXT *ppCertContext)
2444 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2447 TRACE("(%p, %08lx, %p, %ld, %08lx, %p)\n", hCertStore, dwCertEncodingType,
2448 pbCertEncoded, cbCertEncoded, dwAddDisposition, ppCertContext);
2452 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2456 PWINE_CERT_CONTEXT cert = CRYPT_CreateCertificateContext(
2457 dwCertEncodingType, pbCertEncoded, cbCertEncoded);
2461 ret = hcs->addCert(hcs, (PCCERT_CONTEXT)cert, dwAddDisposition);
2462 if (ret && ppCertContext)
2463 *ppCertContext = (PCCERT_CONTEXT)hcs->createCertRef(cert,
2466 CRYPT_FreeCert(cert);
2474 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
2475 PCCERT_CONTEXT pPrev)
2477 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2478 PWINE_CERT_CONTEXT_REF prev = (PWINE_CERT_CONTEXT_REF)pPrev;
2481 TRACE("(%p, %p)\n", hCertStore, pPrev);
2484 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2487 ret = (PCCERT_CONTEXT)hcs->enumCert(hcs, prev);
2491 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
2495 TRACE("(%p)\n", pCertContext);
2499 else if (!pCertContext->hCertStore)
2502 CertFreeCertificateContext(pCertContext);
2506 PWINECRYPT_CERTSTORE hcs =
2507 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
2509 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2512 ret = hcs->deleteCert(hcs, pCertContext, 0);
2513 CertFreeCertificateContext(pCertContext);
2518 BOOL WINAPI CertAddEncodedCRLToStore(HCERTSTORE hCertStore,
2519 DWORD dwCertEncodingType, const BYTE *pbCrlEncoded, DWORD cbCrlEncoded,
2520 DWORD dwAddDisposition, PCCRL_CONTEXT *ppCrlContext)
2522 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2523 dwCertEncodingType, pbCrlEncoded, cbCrlEncoded, dwAddDisposition,
2528 BOOL WINAPI CertAddCRLContextToStore( HCERTSTORE hCertStore,
2529 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
2530 PCCRL_CONTEXT* ppStoreContext )
2532 FIXME("%p %p %08lx %p\n", hCertStore, pCrlContext,
2533 dwAddDisposition, ppStoreContext);
2537 BOOL WINAPI CertFreeCRLContext( PCCRL_CONTEXT pCrlContext)
2539 FIXME("%p\n", pCrlContext );
2544 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
2546 FIXME("(%p): stub\n", pCrlContext);
2550 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
2551 PCCRL_CONTEXT pPrev)
2553 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2557 PCCTL_CONTEXT WINAPI CertCreateCTLContext(DWORD dwCertEncodingType,
2558 const BYTE* pbCtlEncoded, DWORD cbCtlEncoded)
2560 FIXME("(%08lx, %p, %08lx): stub\n", dwCertEncodingType, pbCtlEncoded,
2565 BOOL WINAPI CertAddEncodedCTLToStore(HCERTSTORE hCertStore,
2566 DWORD dwMsgAndCertEncodingType, const BYTE *pbCtlEncoded, DWORD cbCtlEncoded,
2567 DWORD dwAddDisposition, PCCTL_CONTEXT *ppCtlContext)
2569 FIXME("(%p, %08lx, %p, %ld, %08lx, %p): stub\n", hCertStore,
2570 dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition,
2575 BOOL WINAPI CertAddCTLContextToStore(HCERTSTORE hCertStore,
2576 PCCTL_CONTEXT pCtlContext, DWORD dwAddDisposition,
2577 PCCTL_CONTEXT* ppStoreContext)
2579 FIXME("(%p, %p, %08lx, %p): stub\n", hCertStore, pCtlContext,
2580 dwAddDisposition, ppStoreContext);
2584 BOOL WINAPI CertFreeCTLContext(PCCTL_CONTEXT pCtlContext)
2586 FIXME("(%p): stub\n", pCtlContext );
2590 BOOL WINAPI CertDeleteCTLFromStore(PCCTL_CONTEXT pCtlContext)
2592 FIXME("(%p): stub\n", pCtlContext);
2596 PCCTL_CONTEXT WINAPI CertEnumCTLsInStore(HCERTSTORE hCertStore,
2597 PCCTL_CONTEXT pPrev)
2599 FIXME("(%p, %p): stub\n", hCertStore, pPrev);
2604 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
2606 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
2608 TRACE("(%p, %08lx)\n", hCertStore, dwFlags);
2613 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
2616 if (InterlockedDecrement(&hcs->ref) == 0)
2618 TRACE("%p's ref count is 0, freeing\n", hcs);
2620 if (!(hcs->dwOpenFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
2621 CryptReleaseContext(hcs->cryptProv, 0);
2622 hcs->closeStore(hcs, dwFlags);
2625 TRACE("%p's ref count is %ld\n", hcs, hcs->ref);
2629 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
2630 DWORD dwCtrlType, void const *pvCtrlPara)
2632 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
2635 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
2640 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
2645 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
2652 BOOL WINAPI CertGetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2653 DWORD dwPropId, void *pvData, DWORD *pcbData)
2655 FIXME("(%p, %ld, %p, %p): stub\n", pCRLContext, dwPropId, pvData, pcbData);
2659 BOOL WINAPI CertSetCRLContextProperty(PCCRL_CONTEXT pCRLContext,
2660 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2662 FIXME("(%p, %ld, %08lx, %p): stub\n", pCRLContext, dwPropId, dwFlags,
2667 BOOL WINAPI CertSerializeCRLStoreElement(PCCRL_CONTEXT pCrlContext,
2668 DWORD dwFlags, BYTE *pbElement, DWORD *pcbElement)
2670 FIXME("(%p, %08lx, %p, %p): stub\n", pCrlContext, dwFlags, pbElement,
2675 BOOL WINAPI CertGetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2676 DWORD dwPropId, void *pvData, DWORD *pcbData)
2678 FIXME("(%p, %ld, %p, %p): stub\n", pCTLContext, dwPropId, pvData, pcbData);
2682 BOOL WINAPI CertSetCTLContextProperty(PCCTL_CONTEXT pCTLContext,
2683 DWORD dwPropId, DWORD dwFlags, const void *pvData)
2685 FIXME("(%p, %ld, %08lx, %p): stub\n", pCTLContext, dwPropId, dwFlags,
2690 BOOL WINAPI CertSerializeCTLStoreElement(PCCTL_CONTEXT pCtlContext,
2691 DWORD dwFlags, BYTE *pbElement, DWORD *pcbElement)
2693 FIXME("(%p, %08lx, %p, %p): stub\n", pCtlContext, dwFlags, pbElement,
2698 BOOL WINAPI CertSerializeCertificateStoreElement(PCCERT_CONTEXT pCertContext,
2699 DWORD dwFlags, BYTE *pbElement, DWORD *pcbElement)
2703 TRACE("(%p, %08lx, %p, %p)\n", pCertContext, dwFlags, pbElement,
2708 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
2709 DWORD bytesNeeded = sizeof(WINE_CERT_PROP_HEADER) +
2710 pCertContext->cbCertEncoded;
2711 PWINE_CERT_PROPERTY prop;
2713 EnterCriticalSection(&ref->context->cs);
2714 LIST_FOR_EACH_ENTRY(prop, &ref->context->extendedProperties,
2715 WINE_CERT_PROPERTY, entry)
2716 bytesNeeded += sizeof(WINE_CERT_PROP_HEADER) + prop->hdr.cb;
2719 *pcbElement = bytesNeeded;
2722 else if (*pcbElement < bytesNeeded)
2724 *pcbElement = bytesNeeded;
2725 SetLastError(ERROR_MORE_DATA);
2730 PWINE_CERT_PROP_HEADER hdr;
2732 LIST_FOR_EACH_ENTRY(prop, &ref->context->extendedProperties,
2733 WINE_CERT_PROPERTY, entry)
2735 memcpy(pbElement, &prop->hdr, sizeof(WINE_CERT_PROP_HEADER));
2736 pbElement += sizeof(WINE_CERT_PROP_HEADER);
2739 memcpy(pbElement, prop->pbData, prop->hdr.cb);
2740 pbElement += prop->hdr.cb;
2743 hdr = (PWINE_CERT_PROP_HEADER)pbElement;
2744 hdr->propID = CERT_CERT_PROP_ID;
2746 hdr->cb = pCertContext->cbCertEncoded;
2747 memcpy(pbElement + sizeof(WINE_CERT_PROP_HEADER),
2748 pCertContext->pbCertEncoded, pCertContext->cbCertEncoded);
2751 LeaveCriticalSection(&ref->context->cs);
2758 /* Looks for the property with ID propID in the buffer buf. Returns a pointer
2759 * to its header if a valid header is found, NULL if not. Valid means the
2760 * length of thte property won't overrun buf, and the unknown field is 1.
2762 static const WINE_CERT_PROP_HEADER *CRYPT_findPropID(const BYTE *buf,
2763 DWORD size, DWORD propID)
2765 const WINE_CERT_PROP_HEADER *ret = NULL;
2768 while (size && !ret && !done)
2770 if (size < sizeof(WINE_CERT_PROP_HEADER))
2772 SetLastError(CRYPT_E_FILE_ERROR);
2777 const WINE_CERT_PROP_HEADER *hdr =
2778 (const WINE_CERT_PROP_HEADER *)buf;
2780 size -= sizeof(WINE_CERT_PROP_HEADER);
2781 buf += sizeof(WINE_CERT_PROP_HEADER);
2784 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2787 else if (!hdr->propID)
2789 /* assume a zero prop ID means the data are uninitialized, so
2794 else if (hdr->unknown != 1)
2796 SetLastError(ERROR_FILE_NOT_FOUND);
2799 else if (hdr->propID == propID)
2811 static const void * WINAPI CRYPT_ReadSerializedElement(const BYTE *pbElement,
2812 DWORD cbElement, DWORD dwContextTypeFlags, DWORD *pdwContentType)
2814 const void *context;
2816 TRACE("(%p, %ld, %08lx, %p)\n", pbElement, cbElement, dwContextTypeFlags,
2821 SetLastError(ERROR_END_OF_MEDIA);
2827 const WINE_CONTEXT_INTERFACE *contextInterface = NULL;
2828 const WINE_CERT_PROP_HEADER *hdr = NULL;
2834 if (dwContextTypeFlags == CERT_STORE_ALL_CONTEXT_FLAG)
2836 hdr = CRYPT_findPropID(pbElement, cbElement, CERT_CERT_PROP_ID);
2838 type = CERT_STORE_CERTIFICATE_CONTEXT;
2841 hdr = CRYPT_findPropID(pbElement, cbElement, CERT_CRL_PROP_ID);
2843 type = CERT_STORE_CRL_CONTEXT;
2846 hdr = CRYPT_findPropID(pbElement, cbElement,
2849 type = CERT_STORE_CTL_CONTEXT;
2853 else if (dwContextTypeFlags & CERT_STORE_CERTIFICATE_CONTEXT_FLAG)
2855 hdr = CRYPT_findPropID(pbElement, cbElement, CERT_CERT_PROP_ID);
2856 type = CERT_STORE_CERTIFICATE_CONTEXT;
2858 else if (dwContextTypeFlags & CERT_STORE_CRL_CONTEXT_FLAG)
2860 hdr = CRYPT_findPropID(pbElement, cbElement, CERT_CRL_PROP_ID);
2861 type = CERT_STORE_CRL_CONTEXT;
2863 else if (dwContextTypeFlags & CERT_STORE_CTL_CONTEXT_FLAG)
2865 hdr = CRYPT_findPropID(pbElement, cbElement, CERT_CTL_PROP_ID);
2866 type = CERT_STORE_CTL_CONTEXT;
2871 case CERT_STORE_CERTIFICATE_CONTEXT:
2872 contextInterface = &gCertInterface;
2874 case CERT_STORE_CRL_CONTEXT:
2875 contextInterface = &gCRLInterface;
2877 case CERT_STORE_CTL_CONTEXT:
2878 contextInterface = &gCTLInterface;
2881 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
2888 context = contextInterface->create(X509_ASN_ENCODING,
2889 (BYTE *)hdr + sizeof(WINE_CERT_PROP_HEADER), hdr->cb);
2892 BOOL noMoreProps = FALSE;
2894 while (!noMoreProps && ret)
2896 if (cbElement < sizeof(WINE_CERT_PROP_HEADER))
2900 const WINE_CERT_PROP_HEADER *hdr =
2901 (const WINE_CERT_PROP_HEADER *)pbElement;
2903 TRACE("prop is %ld\n", hdr->propID);
2904 cbElement -= sizeof(WINE_CERT_PROP_HEADER);
2905 pbElement += sizeof(WINE_CERT_PROP_HEADER);
2906 if (cbElement < hdr->cb)
2908 SetLastError(HRESULT_FROM_WIN32(
2909 ERROR_INVALID_PARAMETER));
2912 else if (!hdr->propID)
2914 /* Like in CRYPT_findPropID, stop if the propID is zero
2918 else if (hdr->unknown != 1)
2920 SetLastError(ERROR_FILE_NOT_FOUND);
2923 else if (hdr->propID != CERT_CERT_PROP_ID &&
2924 hdr->propID != CERT_CRL_PROP_ID && hdr->propID !=
2927 /* Have to create a blob for most types, but not
2930 switch (hdr->propID)
2932 case CERT_AUTO_ENROLL_PROP_ID:
2933 case CERT_CTL_USAGE_PROP_ID:
2934 case CERT_DESCRIPTION_PROP_ID:
2935 case CERT_FRIENDLY_NAME_PROP_ID:
2936 case CERT_HASH_PROP_ID:
2937 case CERT_KEY_IDENTIFIER_PROP_ID:
2938 case CERT_MD5_HASH_PROP_ID:
2939 case CERT_NEXT_UPDATE_LOCATION_PROP_ID:
2940 case CERT_PUBKEY_ALG_PARA_PROP_ID:
2941 case CERT_PVK_FILE_PROP_ID:
2942 case CERT_SIGNATURE_HASH_PROP_ID:
2943 case CERT_ISSUER_PUBLIC_KEY_MD5_HASH_PROP_ID:
2944 case CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID:
2945 case CERT_ENROLLMENT_PROP_ID:
2946 case CERT_CROSS_CERT_DIST_POINTS_PROP_ID:
2947 case CERT_RENEWAL_PROP_ID:
2949 CRYPT_DATA_BLOB blob = { hdr->cb,
2950 (LPBYTE)pbElement };
2952 ret = contextInterface->setProp(context,
2953 hdr->propID, 0, &blob);
2956 case CERT_DATE_STAMP_PROP_ID:
2957 ret = contextInterface->setProp(context,
2958 hdr->propID, 0, pbElement);
2961 FIXME("prop ID %ld: stub\n", hdr->propID);
2964 pbElement += hdr->cb;
2965 cbElement -= hdr->cb;
2973 *pdwContentType = type;
2977 contextInterface->free(context);
2984 SetLastError(STATUS_ACCESS_VIOLATION);
2991 BOOL WINAPI CertAddSerializedElementToStore(HCERTSTORE hCertStore,
2992 const BYTE *pbElement, DWORD cbElement, DWORD dwAddDisposition, DWORD dwFlags,
2993 DWORD dwContextTypeFlags, DWORD *pdwContentType, const void **ppvContext)
2995 const void *context;
2999 TRACE("(%p, %p, %ld, %08lx, %08lx, %08lx, %p, %p)\n", hCertStore,
3000 pbElement, cbElement, dwAddDisposition, dwFlags, dwContextTypeFlags,
3001 pdwContentType, ppvContext);
3003 /* Call the internal function, then delete the hashes. Tests show this
3004 * function uses real hash values, not whatever's stored in the hash
3007 context = CRYPT_ReadSerializedElement(pbElement, cbElement,
3008 dwContextTypeFlags, &type);
3011 const WINE_CONTEXT_INTERFACE *contextInterface = NULL;
3015 case CERT_STORE_CERTIFICATE_CONTEXT:
3016 contextInterface = &gCertInterface;
3018 case CERT_STORE_CRL_CONTEXT:
3019 contextInterface = &gCRLInterface;
3021 case CERT_STORE_CTL_CONTEXT:
3022 contextInterface = &gCTLInterface;
3025 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3027 if (contextInterface)
3029 contextInterface->setProp(context, CERT_HASH_PROP_ID, 0, NULL);
3030 contextInterface->setProp(context, CERT_MD5_HASH_PROP_ID, 0, NULL);
3031 contextInterface->setProp(context, CERT_SIGNATURE_HASH_PROP_ID, 0,
3034 *pdwContentType = type;
3035 ret = contextInterface->addContextToStore(hCertStore, context,
3036 dwAddDisposition, ppvContext);
3037 contextInterface->free(context);
3047 static void CRYPT_UnrefCertificateContext(PWINE_CERT_CONTEXT_REF ref)
3049 if (InterlockedDecrement(&ref->context->ref) == 0)
3051 TRACE("%p's ref count is 0, freeing\n", ref->context);
3052 CRYPT_FreeCert(ref->context);
3055 TRACE("%p's ref count is %ld\n", ref->context, ref->context->ref);
3058 BOOL WINAPI CertFreeCertificateContext(PCCERT_CONTEXT pCertContext)
3060 TRACE("(%p)\n", pCertContext);
3064 PWINE_CERT_CONTEXT_REF ref = (PWINE_CERT_CONTEXT_REF)pCertContext;
3065 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)ref->cert.hCertStore;
3067 CRYPT_UnrefCertificateContext(ref);
3068 if (store && store->dwMagic == WINE_CRYPTCERTSTORE_MAGIC &&
3070 store->freeCert(ref);
3071 TRACE("freeing %p\n", ref);
3077 PCCERT_CONTEXT WINAPI CertFindCertificateInStore(HCERTSTORE hCertStore,
3078 DWORD dwCertEncodingType, DWORD dwFlags, DWORD dwType,
3079 const void *pvPara, PCCERT_CONTEXT pPrevCertContext)
3081 FIXME("stub: %p %ld %ld %ld %p %p\n", hCertStore, dwCertEncodingType,
3082 dwFlags, dwType, pvPara, pPrevCertContext);
3083 SetLastError(CRYPT_E_NOT_FOUND);
3087 BOOL WINAPI CertAddStoreToCollection(HCERTSTORE hCollectionStore,
3088 HCERTSTORE hSiblingStore, DWORD dwUpdateFlags, DWORD dwPriority)
3090 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
3091 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
3092 PWINE_STORE_LIST_ENTRY entry;
3095 TRACE("(%p, %p, %08lx, %ld)\n", hCollectionStore, hSiblingStore,
3096 dwUpdateFlags, dwPriority);
3098 if (!collection || !sibling)
3100 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
3102 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3105 if (collection->hdr.type != StoreTypeCollection)
3107 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3110 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
3112 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3116 entry = CryptMemAlloc(sizeof(WINE_STORE_LIST_ENTRY));
3119 InterlockedIncrement(&sibling->ref);
3120 TRACE("sibling %p's ref count is %ld\n", sibling, sibling->ref);
3121 entry->store = sibling;
3122 entry->dwUpdateFlags = dwUpdateFlags;
3123 entry->dwPriority = dwPriority;
3124 list_init(&entry->entry);
3125 TRACE("%p: adding %p, priority %ld\n", collection, entry, dwPriority);
3126 EnterCriticalSection(&collection->cs);
3129 PWINE_STORE_LIST_ENTRY cursor;
3132 LIST_FOR_EACH_ENTRY(cursor, &collection->stores,
3133 WINE_STORE_LIST_ENTRY, entry)
3135 if (cursor->dwPriority < dwPriority)
3137 list_add_before(&cursor->entry, &entry->entry);
3143 list_add_tail(&collection->stores, &entry->entry);
3146 list_add_tail(&collection->stores, &entry->entry);
3147 LeaveCriticalSection(&collection->cs);
3155 void WINAPI CertRemoveStoreFromCollection(HCERTSTORE hCollectionStore,
3156 HCERTSTORE hSiblingStore)
3158 PWINE_COLLECTIONSTORE collection = (PWINE_COLLECTIONSTORE)hCollectionStore;
3159 WINECRYPT_CERTSTORE *sibling = (WINECRYPT_CERTSTORE *)hSiblingStore;
3160 PWINE_STORE_LIST_ENTRY store, next;
3162 TRACE("(%p, %p)\n", hCollectionStore, hSiblingStore);
3164 if (!collection || !sibling)
3166 if (collection->hdr.dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
3168 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3171 if (collection->hdr.type != StoreTypeCollection)
3173 if (sibling->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
3175 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3178 EnterCriticalSection(&collection->cs);
3179 LIST_FOR_EACH_ENTRY_SAFE(store, next, &collection->stores,
3180 WINE_STORE_LIST_ENTRY, entry)
3182 if (store->store == sibling)
3184 list_remove(&store->entry);
3185 CertCloseStore(store->store, 0);
3186 CryptMemFree(store);
3190 LeaveCriticalSection(&collection->cs);
3193 PCRYPT_ATTRIBUTE WINAPI CertFindAttribute(LPCSTR pszObjId, DWORD cAttr,
3194 CRYPT_ATTRIBUTE rgAttr[])
3196 PCRYPT_ATTRIBUTE ret = NULL;
3199 TRACE("%s %ld %p\n", debugstr_a(pszObjId), cAttr, rgAttr);
3205 SetLastError(ERROR_INVALID_PARAMETER);
3209 for (i = 0; !ret && i < cAttr; i++)
3210 if (rgAttr[i].pszObjId && !strcmp(pszObjId, rgAttr[i].pszObjId))
3215 PCERT_EXTENSION WINAPI CertFindExtension(LPCSTR pszObjId, DWORD cExtensions,
3216 CERT_EXTENSION rgExtensions[])
3218 PCERT_EXTENSION ret = NULL;
3221 TRACE("%s %ld %p\n", debugstr_a(pszObjId), cExtensions, rgExtensions);
3227 SetLastError(ERROR_INVALID_PARAMETER);
3231 for (i = 0; !ret && i < cExtensions; i++)
3232 if (rgExtensions[i].pszObjId && !strcmp(pszObjId,
3233 rgExtensions[i].pszObjId))
3234 ret = &rgExtensions[i];
3238 PCERT_RDN_ATTR WINAPI CertFindRDNAttr(LPCSTR pszObjId, PCERT_NAME_INFO pName)
3240 PCERT_RDN_ATTR ret = NULL;
3243 TRACE("%s %p\n", debugstr_a(pszObjId), pName);
3247 SetLastError(ERROR_INVALID_PARAMETER);
3251 for (i = 0; !ret && i < pName->cRDN; i++)
3252 for (j = 0; !ret && j < pName->rgRDN[i].cRDNAttr; j++)
3253 if (pName->rgRDN[i].rgRDNAttr[j].pszObjId && !strcmp(pszObjId,
3254 pName->rgRDN[i].rgRDNAttr[j].pszObjId))
3255 ret = &pName->rgRDN[i].rgRDNAttr[j];
3259 LONG WINAPI CertVerifyTimeValidity(LPFILETIME pTimeToVerify,
3260 PCERT_INFO pCertInfo)
3269 GetSystemTime(&sysTime);
3270 SystemTimeToFileTime(&sysTime, &fileTime);
3271 pTimeToVerify = &fileTime;
3273 if ((ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotBefore)) >= 0)
3275 ret = CompareFileTime(pTimeToVerify, &pCertInfo->NotAfter);
3282 BOOL WINAPI CryptHashCertificate(HCRYPTPROV hCryptProv, ALG_ID Algid,
3283 DWORD dwFlags, const BYTE *pbEncoded, DWORD cbEncoded, BYTE *pbComputedHash,
3284 DWORD *pcbComputedHash)
3287 HCRYPTHASH hHash = 0;
3289 TRACE("(%ld, %d, %08lx, %p, %ld, %p, %p)\n", hCryptProv, Algid, dwFlags,
3290 pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
3293 hCryptProv = CRYPT_GetDefaultProvider();
3298 ret = CryptCreateHash(hCryptProv, Algid, 0, 0, &hHash);
3301 ret = CryptHashData(hHash, pbEncoded, cbEncoded, 0);
3303 ret = CryptGetHashParam(hHash, HP_HASHVAL, pbComputedHash,
3304 pcbComputedHash, 0);
3305 CryptDestroyHash(hHash);
3311 BOOL WINAPI CryptSignCertificate(HCRYPTPROV hCryptProv, DWORD dwKeySpec,
3312 DWORD dwCertEncodingType, const BYTE *pbEncodedToBeSigned,
3313 DWORD cbEncodedToBeSigned, PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
3314 const void *pvHashAuxInfo, BYTE *pbSignature, DWORD *pcbSignature)
3320 TRACE("(%08lx, %ld, %ld, %p, %ld, %p, %p, %p, %p)\n", hCryptProv,
3321 dwKeySpec, dwCertEncodingType, pbEncodedToBeSigned, cbEncodedToBeSigned,
3322 pSignatureAlgorithm, pvHashAuxInfo, pbSignature, pcbSignature);
3324 algID = CertOIDToAlgId(pSignatureAlgorithm->pszObjId);
3327 SetLastError(NTE_BAD_ALGID);
3332 SetLastError(ERROR_INVALID_PARAMETER);
3336 ret = CryptCreateHash(hCryptProv, algID, 0, 0, &hHash);
3339 ret = CryptHashData(hHash, pbEncodedToBeSigned, cbEncodedToBeSigned, 0);
3341 ret = CryptSignHashW(hHash, dwKeySpec, NULL, 0, pbSignature,
3343 CryptDestroyHash(hHash);
3348 BOOL WINAPI CryptVerifyCertificateSignature(HCRYPTPROV hCryptProv,
3349 DWORD dwCertEncodingType, const BYTE *pbEncoded, DWORD cbEncoded,
3350 PCERT_PUBLIC_KEY_INFO pPublicKey)
3352 return CryptVerifyCertificateSignatureEx(hCryptProv, dwCertEncodingType,
3353 CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, (void *)pbEncoded,
3354 CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, pPublicKey, 0, NULL);
3357 BOOL WINAPI CryptVerifyCertificateSignatureEx(HCRYPTPROV hCryptProv,
3358 DWORD dwCertEncodingType, DWORD dwSubjectType, void *pvSubject,
3359 DWORD dwIssuerType, void *pvIssuer, DWORD dwFlags, void *pvReserved)
3362 CRYPT_DATA_BLOB subjectBlob;
3364 TRACE("(%08lx, %ld, %ld, %p, %ld, %p, %08lx, %p)\n", hCryptProv,
3365 dwCertEncodingType, dwSubjectType, pvSubject, dwIssuerType, pvIssuer,
3366 dwFlags, pvReserved);
3368 switch (dwSubjectType)
3370 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB:
3372 PCRYPT_DATA_BLOB blob = (PCRYPT_DATA_BLOB)pvSubject;
3374 subjectBlob.pbData = blob->pbData;
3375 subjectBlob.cbData = blob->cbData;
3378 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT:
3380 PCERT_CONTEXT context = (PCERT_CONTEXT)pvSubject;
3382 subjectBlob.pbData = context->pbCertEncoded;
3383 subjectBlob.cbData = context->cbCertEncoded;
3386 case CRYPT_VERIFY_CERT_SIGN_SUBJECT_CRL:
3388 PCRL_CONTEXT context = (PCRL_CONTEXT)pvSubject;
3390 subjectBlob.pbData = context->pbCrlEncoded;
3391 subjectBlob.cbData = context->cbCrlEncoded;
3395 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3401 PCERT_SIGNED_CONTENT_INFO signedCert = NULL;
3404 ret = CryptDecodeObjectEx(dwCertEncodingType, X509_CERT,
3405 subjectBlob.pbData, subjectBlob.cbData,
3406 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
3407 (BYTE *)&signedCert, &size);
3410 switch (dwIssuerType)
3412 case CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY:
3414 PCERT_PUBLIC_KEY_INFO pubKeyInfo =
3415 (PCERT_PUBLIC_KEY_INFO)pvIssuer;
3416 ALG_ID algID = CertOIDToAlgId(pubKeyInfo->Algorithm.pszObjId);
3422 ret = CryptImportPublicKeyInfoEx(hCryptProv,
3423 dwCertEncodingType, pubKeyInfo, algID, 0, NULL, &key);
3428 ret = CryptCreateHash(hCryptProv, algID, 0, 0, &hash);
3431 ret = CryptHashData(hash,
3432 signedCert->ToBeSigned.pbData,
3433 signedCert->ToBeSigned.cbData, 0);
3436 ret = CryptVerifySignatureW(hash,
3437 signedCert->Signature.pbData,
3438 signedCert->Signature.cbData, key, NULL, 0);
3440 CryptDestroyHash(hash);
3442 CryptDestroyKey(key);
3447 SetLastError(NTE_BAD_ALGID);
3452 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT:
3453 case CRYPT_VERIFY_CERT_SIGN_ISSUER_CHAIN:
3454 FIXME("issuer type %ld: stub\n", dwIssuerType);
3457 case CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL:
3460 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3465 FIXME("unimplemented for NULL signer\n");
3466 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3471 SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
3474 LocalFree(signedCert);
3480 BOOL WINAPI CryptVerifyMessageSignature(/*PCRYPT_VERIFY_MESSAGE_PARA*/ void* pVerifyPara,
3481 DWORD dwSignerIndex, const BYTE* pbSignedBlob, DWORD cbSignedBlob,
3482 BYTE* pbDecoded, DWORD* pcbDecoded, PCCERT_CONTEXT* ppSignerCert)
3484 FIXME("stub: %p, %ld, %p, %ld, %p, %p, %p\n",
3485 pVerifyPara, dwSignerIndex, pbSignedBlob, cbSignedBlob,
3486 pbDecoded, pcbDecoded, ppSignerCert);