2 * Copyright 2002 Mike McCormack for CodeWeavers
3 * Copyright 2004-2006 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * - The concept of physical stores and locations isn't implemented. (This
21 * doesn't mean registry stores et al aren't implemented. See the PSDK for
22 * registering and enumerating physical stores and locations.)
23 * - Many flags, options and whatnot are unimplemented.
27 #include "wine/port.h"
37 #include "wine/debug.h"
38 #include "wine/list.h"
39 #include "wine/exception.h"
40 #include "crypt32_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
44 static const WINE_CONTEXT_INTERFACE gCertInterface = {
45 (CreateContextFunc)CertCreateCertificateContext,
46 (AddContextToStoreFunc)CertAddCertificateContextToStore,
47 (AddEncodedContextToStoreFunc)CertAddEncodedCertificateToStore,
48 (DuplicateContextFunc)CertDuplicateCertificateContext,
49 (EnumContextsInStoreFunc)CertEnumCertificatesInStore,
50 (EnumPropertiesFunc)CertEnumCertificateContextProperties,
51 (GetContextPropertyFunc)CertGetCertificateContextProperty,
52 (SetContextPropertyFunc)CertSetCertificateContextProperty,
53 (SerializeElementFunc)CertSerializeCertificateStoreElement,
54 (FreeContextFunc)CertFreeCertificateContext,
55 (DeleteContextFunc)CertDeleteCertificateFromStore,
57 PCWINE_CONTEXT_INTERFACE pCertInterface = &gCertInterface;
59 static const WINE_CONTEXT_INTERFACE gCRLInterface = {
60 (CreateContextFunc)CertCreateCRLContext,
61 (AddContextToStoreFunc)CertAddCRLContextToStore,
62 (AddEncodedContextToStoreFunc)CertAddEncodedCRLToStore,
63 (DuplicateContextFunc)CertDuplicateCRLContext,
64 (EnumContextsInStoreFunc)CertEnumCRLsInStore,
65 (EnumPropertiesFunc)CertEnumCRLContextProperties,
66 (GetContextPropertyFunc)CertGetCRLContextProperty,
67 (SetContextPropertyFunc)CertSetCRLContextProperty,
68 (SerializeElementFunc)CertSerializeCRLStoreElement,
69 (FreeContextFunc)CertFreeCRLContext,
70 (DeleteContextFunc)CertDeleteCRLFromStore,
72 PCWINE_CONTEXT_INTERFACE pCRLInterface = &gCRLInterface;
74 static const WINE_CONTEXT_INTERFACE gCTLInterface = {
75 (CreateContextFunc)CertCreateCTLContext,
76 (AddContextToStoreFunc)CertAddCTLContextToStore,
77 (AddEncodedContextToStoreFunc)CertAddEncodedCTLToStore,
78 (DuplicateContextFunc)CertDuplicateCTLContext,
79 (EnumContextsInStoreFunc)CertEnumCTLsInStore,
80 (EnumPropertiesFunc)CertEnumCTLContextProperties,
81 (GetContextPropertyFunc)CertGetCTLContextProperty,
82 (SetContextPropertyFunc)CertSetCTLContextProperty,
83 (SerializeElementFunc)CertSerializeCTLStoreElement,
84 (FreeContextFunc)CertFreeCTLContext,
85 (DeleteContextFunc)CertDeleteCTLFromStore,
87 PCWINE_CONTEXT_INTERFACE pCTLInterface = &gCTLInterface;
89 typedef struct _WINE_MEMSTORE
91 WINECRYPT_CERTSTORE hdr;
92 struct ContextList *certs;
93 struct ContextList *crls;
94 struct ContextList *ctls;
95 } WINE_MEMSTORE, *PWINE_MEMSTORE;
97 void CRYPT_InitStore(WINECRYPT_CERTSTORE *store, DWORD dwFlags,
101 store->dwMagic = WINE_CRYPTCERTSTORE_MAGIC;
103 store->dwOpenFlags = dwFlags;
104 store->properties = NULL;
107 void CRYPT_FreeStore(PWINECRYPT_CERTSTORE store)
109 if (store->properties)
110 ContextPropertyList_Free(store->properties);
114 BOOL WINAPI I_CertUpdateStore(HCERTSTORE store1, HCERTSTORE store2, DWORD unk0,
117 static BOOL warned = FALSE;
118 const WINE_CONTEXT_INTERFACE * const interfaces[] = { pCertInterface,
119 pCRLInterface, pCTLInterface };
122 TRACE("(%p, %p, %08x, %08x)\n", store1, store2, unk0, unk1);
125 FIXME("semi-stub\n");
129 /* Poor-man's resync: empty first store, then add everything from second
132 for (i = 0; i < sizeof(interfaces) / sizeof(interfaces[0]); i++)
137 context = interfaces[i]->enumContextsInStore(store1, NULL);
139 interfaces[i]->deleteFromStore(context);
142 context = interfaces[i]->enumContextsInStore(store2, context);
144 interfaces[i]->addContextToStore(store1, context,
145 CERT_STORE_ADD_ALWAYS, NULL);
151 static BOOL CRYPT_MemAddCert(PWINECRYPT_CERTSTORE store, void *cert,
152 void *toReplace, const void **ppStoreContext)
154 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
155 PCERT_CONTEXT context;
157 TRACE("(%p, %p, %p, %p)\n", store, cert, toReplace, ppStoreContext);
159 context = (PCERT_CONTEXT)ContextList_Add(ms->certs, cert, toReplace);
162 context->hCertStore = store;
164 *ppStoreContext = CertDuplicateCertificateContext(context);
166 return context ? TRUE : FALSE;
169 static void *CRYPT_MemEnumCert(PWINECRYPT_CERTSTORE store, void *pPrev)
171 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
174 TRACE("(%p, %p)\n", store, pPrev);
176 ret = ContextList_Enum(ms->certs, pPrev);
178 SetLastError(CRYPT_E_NOT_FOUND);
180 TRACE("returning %p\n", ret);
184 static BOOL CRYPT_MemDeleteCert(PWINECRYPT_CERTSTORE store, void *pCertContext)
186 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
188 ContextList_Delete(ms->certs, pCertContext);
192 static BOOL CRYPT_MemAddCrl(PWINECRYPT_CERTSTORE store, void *crl,
193 void *toReplace, const void **ppStoreContext)
195 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
196 PCRL_CONTEXT context;
198 TRACE("(%p, %p, %p, %p)\n", store, crl, toReplace, ppStoreContext);
200 context = (PCRL_CONTEXT)ContextList_Add(ms->crls, crl, toReplace);
203 context->hCertStore = store;
205 *ppStoreContext = CertDuplicateCRLContext(context);
207 return context ? TRUE : FALSE;
210 static void *CRYPT_MemEnumCrl(PWINECRYPT_CERTSTORE store, void *pPrev)
212 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
215 TRACE("(%p, %p)\n", store, pPrev);
217 ret = ContextList_Enum(ms->crls, pPrev);
219 SetLastError(CRYPT_E_NOT_FOUND);
221 TRACE("returning %p\n", ret);
225 static BOOL CRYPT_MemDeleteCrl(PWINECRYPT_CERTSTORE store, void *pCrlContext)
227 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
229 ContextList_Delete(ms->crls, pCrlContext);
233 static BOOL CRYPT_MemAddCtl(PWINECRYPT_CERTSTORE store, void *ctl,
234 void *toReplace, const void **ppStoreContext)
236 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
237 PCTL_CONTEXT context;
239 TRACE("(%p, %p, %p, %p)\n", store, ctl, toReplace, ppStoreContext);
241 context = (PCTL_CONTEXT)ContextList_Add(ms->ctls, ctl, toReplace);
244 context->hCertStore = store;
246 *ppStoreContext = CertDuplicateCTLContext(context);
248 return context ? TRUE : FALSE;
251 static void *CRYPT_MemEnumCtl(PWINECRYPT_CERTSTORE store, void *pPrev)
253 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
256 TRACE("(%p, %p)\n", store, pPrev);
258 ret = ContextList_Enum(ms->ctls, pPrev);
260 SetLastError(CRYPT_E_NOT_FOUND);
262 TRACE("returning %p\n", ret);
266 static BOOL CRYPT_MemDeleteCtl(PWINECRYPT_CERTSTORE store, void *pCtlContext)
268 WINE_MEMSTORE *ms = (WINE_MEMSTORE *)store;
270 ContextList_Delete(ms->ctls, pCtlContext);
274 static void WINAPI CRYPT_MemCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
276 WINE_MEMSTORE *store = (WINE_MEMSTORE *)hCertStore;
278 TRACE("(%p, %08x)\n", store, dwFlags);
280 FIXME("Unimplemented flags: %08x\n", dwFlags);
282 ContextList_Free(store->certs);
283 ContextList_Free(store->crls);
284 ContextList_Free(store->ctls);
285 CRYPT_FreeStore((PWINECRYPT_CERTSTORE)store);
288 static WINECRYPT_CERTSTORE *CRYPT_MemOpenStore(HCRYPTPROV hCryptProv,
289 DWORD dwFlags, const void *pvPara)
291 PWINE_MEMSTORE store;
293 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
295 if (dwFlags & CERT_STORE_DELETE_FLAG)
297 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
302 store = CryptMemAlloc(sizeof(WINE_MEMSTORE));
305 memset(store, 0, sizeof(WINE_MEMSTORE));
306 CRYPT_InitStore(&store->hdr, dwFlags, StoreTypeMem);
307 store->hdr.closeStore = CRYPT_MemCloseStore;
308 store->hdr.certs.addContext = CRYPT_MemAddCert;
309 store->hdr.certs.enumContext = CRYPT_MemEnumCert;
310 store->hdr.certs.deleteContext = CRYPT_MemDeleteCert;
311 store->hdr.crls.addContext = CRYPT_MemAddCrl;
312 store->hdr.crls.enumContext = CRYPT_MemEnumCrl;
313 store->hdr.crls.deleteContext = CRYPT_MemDeleteCrl;
314 store->hdr.ctls.addContext = CRYPT_MemAddCtl;
315 store->hdr.ctls.enumContext = CRYPT_MemEnumCtl;
316 store->hdr.ctls.deleteContext = CRYPT_MemDeleteCtl;
317 store->hdr.control = NULL;
318 store->certs = ContextList_Create(pCertInterface,
319 sizeof(CERT_CONTEXT));
320 store->crls = ContextList_Create(pCRLInterface,
321 sizeof(CRL_CONTEXT));
322 store->ctls = ContextList_Create(pCTLInterface,
323 sizeof(CTL_CONTEXT));
324 /* Mem store doesn't need crypto provider, so close it */
325 if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
326 CryptReleaseContext(hCryptProv, 0);
329 return (PWINECRYPT_CERTSTORE)store;
332 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
333 DWORD dwFlags, const void *pvPara)
335 static const WCHAR rootW[] = { 'R','o','o','t',0 };
336 static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
337 LPCWSTR storeName = (LPCWSTR)pvPara;
339 PWINECRYPT_CERTSTORE store = NULL;
343 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
344 debugstr_w((LPCWSTR)pvPara));
348 SetLastError(E_INVALIDARG);
351 if (!lstrcmpiW(storeName, rootW))
352 return CRYPT_RootOpenStore(hCryptProv, dwFlags);
354 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
356 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
357 root = HKEY_LOCAL_MACHINE;
358 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
360 case CERT_SYSTEM_STORE_CURRENT_USER:
361 root = HKEY_CURRENT_USER;
362 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
364 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
365 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
368 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE, %s: stub\n",
369 debugstr_w(storeName));
371 case CERT_SYSTEM_STORE_SERVICES:
372 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
375 FIXME("CERT_SYSTEM_STORE_SERVICES, %s: stub\n",
376 debugstr_w(storeName));
378 case CERT_SYSTEM_STORE_USERS:
379 /* hku\user sid\Software\Microsoft\SystemCertificates */
380 FIXME("CERT_SYSTEM_STORE_USERS, %s: stub\n",
381 debugstr_w(storeName));
383 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
384 root = HKEY_CURRENT_USER;
385 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
387 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
388 root = HKEY_LOCAL_MACHINE;
389 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
391 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
392 /* hklm\Software\Microsoft\EnterpriseCertificates */
393 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE, %s: stub\n",
394 debugstr_w(storeName));
397 SetLastError(E_INVALIDARG);
401 storePath = CryptMemAlloc((lstrlenW(base) + lstrlenW(storeName) + 2) *
407 REGSAM sam = dwFlags & CERT_STORE_READONLY_FLAG ? KEY_READ :
410 wsprintfW(storePath, fmt, base, storeName);
411 if (dwFlags & CERT_STORE_OPEN_EXISTING_FLAG)
412 rc = RegOpenKeyExW(root, storePath, 0, sam, &key);
417 rc = RegCreateKeyExW(root, storePath, 0, NULL, 0, sam, NULL,
419 if (!rc && dwFlags & CERT_STORE_CREATE_NEW_FLAG &&
420 disp == REG_OPENED_EXISTING_KEY)
423 rc = ERROR_FILE_EXISTS;
428 store = CRYPT_RegOpenStore(hCryptProv, dwFlags, key);
433 CryptMemFree(storePath);
438 static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
439 DWORD dwFlags, const void *pvPara)
442 PWINECRYPT_CERTSTORE ret = NULL;
444 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
445 debugstr_a((LPCSTR)pvPara));
449 SetLastError(ERROR_FILE_NOT_FOUND);
452 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
455 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
459 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
460 ret = CRYPT_SysRegOpenStoreW(hCryptProv, dwFlags, storeName);
461 CryptMemFree(storeName);
467 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
468 DWORD dwFlags, const void *pvPara)
470 HCERTSTORE store = 0;
473 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
474 debugstr_w((LPCWSTR)pvPara));
478 SetLastError(ERROR_FILE_NOT_FOUND);
481 /* This returns a different error than system registry stores if the
482 * location is invalid.
484 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
486 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
487 case CERT_SYSTEM_STORE_CURRENT_USER:
488 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
489 case CERT_SYSTEM_STORE_SERVICES:
490 case CERT_SYSTEM_STORE_USERS:
491 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
492 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
493 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
497 SetLastError(ERROR_FILE_NOT_FOUND);
502 HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
503 0, 0, dwFlags, pvPara);
507 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
508 CERT_STORE_CREATE_NEW_FLAG, NULL);
509 CertAddStoreToCollection(store, regStore,
510 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
511 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
512 CertCloseStore(regStore, 0);
513 /* CERT_SYSTEM_STORE_CURRENT_USER returns both the HKCU and HKLM
516 if ((dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) ==
517 CERT_SYSTEM_STORE_CURRENT_USER)
519 dwFlags &= ~CERT_SYSTEM_STORE_CURRENT_USER;
520 dwFlags |= CERT_SYSTEM_STORE_LOCAL_MACHINE;
521 regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W, 0,
525 CertAddStoreToCollection(store, regStore,
526 dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
527 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
528 CertCloseStore(regStore, 0);
531 /* System store doesn't need crypto provider, so close it */
532 if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
533 CryptReleaseContext(hCryptProv, 0);
536 return (PWINECRYPT_CERTSTORE)store;
539 static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
540 DWORD dwFlags, const void *pvPara)
543 PWINECRYPT_CERTSTORE ret = NULL;
545 TRACE("(%ld, %08x, %s)\n", hCryptProv, dwFlags,
546 debugstr_a((LPCSTR)pvPara));
550 SetLastError(ERROR_FILE_NOT_FOUND);
553 len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
556 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
560 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
561 ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
562 CryptMemFree(storeName);
568 static void WINAPI CRYPT_MsgCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
570 HCRYPTMSG msg = hCertStore;
572 TRACE("(%p, %08x)\n", msg, dwFlags);
576 static void *msgProvFuncs[] = {
580 static PWINECRYPT_CERTSTORE CRYPT_MsgOpenStore(HCRYPTPROV hCryptProv,
581 DWORD dwFlags, const void *pvPara)
583 PWINECRYPT_CERTSTORE store = NULL;
584 HCRYPTMSG msg = (HCRYPTMSG)pvPara;
585 PWINECRYPT_CERTSTORE memStore;
587 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
589 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
590 CERT_STORE_CREATE_NEW_FLAG, NULL);
594 DWORD size, count, i;
596 size = sizeof(count);
597 ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
598 for (i = 0; ret && i < count; i++)
601 ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, NULL, &size);
604 LPBYTE buf = CryptMemAlloc(size);
608 ret = CryptMsgGetParam(msg, CMSG_CERT_PARAM, i, buf, &size);
610 ret = CertAddEncodedCertificateToStore(memStore,
611 X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
617 size = sizeof(count);
618 ret = CryptMsgGetParam(msg, CMSG_CRL_COUNT_PARAM, 0, &count, &size);
619 for (i = 0; ret && i < count; i++)
622 ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, NULL, &size);
625 LPBYTE buf = CryptMemAlloc(size);
629 ret = CryptMsgGetParam(msg, CMSG_CRL_PARAM, i, buf, &size);
631 ret = CertAddEncodedCRLToStore(memStore,
632 X509_ASN_ENCODING, buf, size, CERT_STORE_ADD_ALWAYS,
640 CERT_STORE_PROV_INFO provInfo = { 0 };
642 provInfo.cbSize = sizeof(provInfo);
643 provInfo.cStoreProvFunc = sizeof(msgProvFuncs) /
644 sizeof(msgProvFuncs[0]);
645 provInfo.rgpvStoreProvFunc = msgProvFuncs;
646 provInfo.hStoreProv = CryptMsgDuplicate(msg);
647 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
648 /* Msg store doesn't need crypto provider, so close it */
649 if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
650 CryptReleaseContext(hCryptProv, 0);
653 CertCloseStore(memStore, 0);
655 TRACE("returning %p\n", store);
659 static PWINECRYPT_CERTSTORE CRYPT_PKCSOpenStore(HCRYPTPROV hCryptProv,
660 DWORD dwFlags, const void *pvPara)
663 PWINECRYPT_CERTSTORE store = NULL;
664 const CRYPT_DATA_BLOB *data = (const CRYPT_DATA_BLOB *)pvPara;
666 DWORD msgOpenFlags = dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG ? 0 :
667 CMSG_CRYPT_RELEASE_CONTEXT_FLAG;
669 TRACE("(%ld, %08x, %p)\n", hCryptProv, dwFlags, pvPara);
671 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, msgOpenFlags, CMSG_SIGNED,
672 hCryptProv, NULL, NULL);
673 ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
677 msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, msgOpenFlags, 0,
678 hCryptProv, NULL, NULL);
679 ret = CryptMsgUpdate(msg, data->pbData, data->cbData, TRUE);
682 DWORD type, size = sizeof(type);
684 /* Only signed messages are allowed, check type */
685 ret = CryptMsgGetParam(msg, CMSG_TYPE_PARAM, 0, &type, &size);
686 if (ret && type != CMSG_SIGNED)
688 SetLastError(CRYPT_E_INVALID_MSG_TYPE);
694 store = CRYPT_MsgOpenStore(0, dwFlags, msg);
696 TRACE("returning %p\n", store);
700 static PWINECRYPT_CERTSTORE CRYPT_PhysOpenStoreW(HCRYPTPROV hCryptProv,
701 DWORD dwFlags, const void *pvPara)
703 if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG)
704 FIXME("(%ld, %08x, %p): stub\n", hCryptProv, dwFlags, pvPara);
706 FIXME("(%ld, %08x, %s): stub\n", hCryptProv, dwFlags,
707 debugstr_w((LPCWSTR)pvPara));
711 HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
712 DWORD dwMsgAndCertEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags,
715 WINECRYPT_CERTSTORE *hcs;
716 StoreOpenFunc openFunc = NULL;
718 TRACE("(%s, %08x, %08lx, %08x, %p)\n", debugstr_a(lpszStoreProvider),
719 dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara);
721 if (!HIWORD(lpszStoreProvider))
723 switch (LOWORD(lpszStoreProvider))
725 case LOWORD(CERT_STORE_PROV_MSG):
726 openFunc = CRYPT_MsgOpenStore;
728 case LOWORD(CERT_STORE_PROV_MEMORY):
729 openFunc = CRYPT_MemOpenStore;
731 case LOWORD(CERT_STORE_PROV_FILE):
732 openFunc = CRYPT_FileOpenStore;
734 case LOWORD(CERT_STORE_PROV_PKCS7):
735 openFunc = CRYPT_PKCSOpenStore;
737 case LOWORD(CERT_STORE_PROV_REG):
738 openFunc = CRYPT_RegOpenStore;
740 case LOWORD(CERT_STORE_PROV_FILENAME_A):
741 openFunc = CRYPT_FileNameOpenStoreA;
743 case LOWORD(CERT_STORE_PROV_FILENAME_W):
744 openFunc = CRYPT_FileNameOpenStoreW;
746 case LOWORD(CERT_STORE_PROV_COLLECTION):
747 openFunc = CRYPT_CollectionOpenStore;
749 case LOWORD(CERT_STORE_PROV_SYSTEM_A):
750 openFunc = CRYPT_SysOpenStoreA;
752 case LOWORD(CERT_STORE_PROV_SYSTEM_W):
753 openFunc = CRYPT_SysOpenStoreW;
755 case LOWORD(CERT_STORE_PROV_SYSTEM_REGISTRY_A):
756 openFunc = CRYPT_SysRegOpenStoreA;
758 case LOWORD(CERT_STORE_PROV_SYSTEM_REGISTRY_W):
759 openFunc = CRYPT_SysRegOpenStoreW;
761 case LOWORD(CERT_STORE_PROV_PHYSICAL_W):
762 openFunc = CRYPT_PhysOpenStoreW;
765 if (LOWORD(lpszStoreProvider))
766 FIXME("unimplemented type %d\n", LOWORD(lpszStoreProvider));
769 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
770 openFunc = CRYPT_MemOpenStore;
771 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_FILENAME_W))
772 openFunc = CRYPT_FileOpenStore;
773 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
774 openFunc = CRYPT_SysOpenStoreW;
775 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
776 openFunc = CRYPT_CollectionOpenStore;
777 else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
778 openFunc = CRYPT_SysRegOpenStoreW;
781 FIXME("unimplemented type %s\n", lpszStoreProvider);
786 hcs = CRYPT_ProvOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
787 hCryptProv, dwFlags, pvPara);
789 hcs = openFunc(hCryptProv, dwFlags, pvPara);
790 return (HCERTSTORE)hcs;
793 HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV_LEGACY hProv,
794 LPCSTR szSubSystemProtocol)
796 if (!szSubSystemProtocol)
798 SetLastError(E_INVALIDARG);
801 return CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, hProv,
802 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
805 HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV_LEGACY hProv,
806 LPCWSTR szSubSystemProtocol)
808 if (!szSubSystemProtocol)
810 SetLastError(E_INVALIDARG);
813 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, hProv,
814 CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
817 #define CertContext_CopyProperties(to, from) \
818 Context_CopyProperties((to), (from), sizeof(CERT_CONTEXT))
820 BOOL WINAPI CertAddCertificateContextToStore(HCERTSTORE hCertStore,
821 PCCERT_CONTEXT pCertContext, DWORD dwAddDisposition,
822 PCCERT_CONTEXT *ppStoreContext)
824 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
826 PCCERT_CONTEXT toAdd = NULL, existing = NULL;
828 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCertContext,
829 dwAddDisposition, ppStoreContext);
831 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
834 DWORD size = sizeof(hashToAdd);
836 ret = CertGetCertificateContextProperty(pCertContext, CERT_HASH_PROP_ID,
840 CRYPT_HASH_BLOB blob = { sizeof(hashToAdd), hashToAdd };
842 existing = CertFindCertificateInStore(hCertStore,
843 pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
848 switch (dwAddDisposition)
850 case CERT_STORE_ADD_ALWAYS:
851 toAdd = CertDuplicateCertificateContext(pCertContext);
853 case CERT_STORE_ADD_NEW:
856 TRACE("found matching certificate, not adding\n");
857 SetLastError(CRYPT_E_EXISTS);
861 toAdd = CertDuplicateCertificateContext(pCertContext);
863 case CERT_STORE_ADD_REPLACE_EXISTING:
864 toAdd = CertDuplicateCertificateContext(pCertContext);
866 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
867 toAdd = CertDuplicateCertificateContext(pCertContext);
869 CertContext_CopyProperties(toAdd, existing);
871 case CERT_STORE_ADD_USE_EXISTING:
874 CertContext_CopyProperties(existing, pCertContext);
875 *ppStoreContext = CertDuplicateCertificateContext(existing);
878 toAdd = CertDuplicateCertificateContext(pCertContext);
880 case CERT_STORE_ADD_NEWER:
883 if (CompareFileTime(&existing->pCertInfo->NotBefore,
884 &pCertContext->pCertInfo->NotBefore) >= 0)
886 TRACE("existing certificate is newer, not adding\n");
887 SetLastError(CRYPT_E_EXISTS);
891 toAdd = CertDuplicateCertificateContext(pCertContext);
894 toAdd = CertDuplicateCertificateContext(pCertContext);
897 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
898 SetLastError(E_INVALIDARG);
905 ret = store->certs.addContext(store, (void *)toAdd,
906 (void *)existing, (const void **)ppStoreContext);
907 else if (ppStoreContext)
908 *ppStoreContext = CertDuplicateCertificateContext(toAdd);
909 CertFreeCertificateContext(toAdd);
911 CertFreeCertificateContext(existing);
913 TRACE("returning %d\n", ret);
917 PCCERT_CONTEXT WINAPI CertEnumCertificatesInStore(HCERTSTORE hCertStore,
918 PCCERT_CONTEXT pPrev)
920 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
923 TRACE("(%p, %p)\n", hCertStore, pPrev);
926 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
929 ret = (PCCERT_CONTEXT)hcs->certs.enumContext(hcs, (void *)pPrev);
933 BOOL WINAPI CertDeleteCertificateFromStore(PCCERT_CONTEXT pCertContext)
937 TRACE("(%p)\n", pCertContext);
941 else if (!pCertContext->hCertStore)
944 CertFreeCertificateContext(pCertContext);
948 PWINECRYPT_CERTSTORE hcs =
949 (PWINECRYPT_CERTSTORE)pCertContext->hCertStore;
951 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
954 ret = hcs->certs.deleteContext(hcs, (void *)pCertContext);
955 CertFreeCertificateContext(pCertContext);
960 #define CrlContext_CopyProperties(to, from) \
961 Context_CopyProperties((to), (from), sizeof(CRL_CONTEXT))
963 BOOL WINAPI CertAddCRLContextToStore(HCERTSTORE hCertStore,
964 PCCRL_CONTEXT pCrlContext, DWORD dwAddDisposition,
965 PCCRL_CONTEXT* ppStoreContext)
967 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
969 PCCRL_CONTEXT toAdd = NULL, existing = NULL;
971 TRACE("(%p, %p, %08x, %p)\n", hCertStore, pCrlContext,
972 dwAddDisposition, ppStoreContext);
974 /* Weird case to pass a test */
975 if (dwAddDisposition == 0)
977 SetLastError(STATUS_ACCESS_VIOLATION);
980 if (dwAddDisposition != CERT_STORE_ADD_ALWAYS)
982 existing = CertFindCRLInStore(hCertStore, 0, 0, CRL_FIND_EXISTING,
986 switch (dwAddDisposition)
988 case CERT_STORE_ADD_ALWAYS:
989 toAdd = CertDuplicateCRLContext(pCrlContext);
991 case CERT_STORE_ADD_NEW:
994 TRACE("found matching CRL, not adding\n");
995 SetLastError(CRYPT_E_EXISTS);
999 toAdd = CertDuplicateCRLContext(pCrlContext);
1001 case CERT_STORE_ADD_NEWER:
1004 LONG newer = CompareFileTime(&existing->pCrlInfo->ThisUpdate,
1005 &pCrlContext->pCrlInfo->ThisUpdate);
1008 toAdd = CertDuplicateCRLContext(pCrlContext);
1011 TRACE("existing CRL is newer, not adding\n");
1012 SetLastError(CRYPT_E_EXISTS);
1017 toAdd = CertDuplicateCRLContext(pCrlContext);
1019 case CERT_STORE_ADD_REPLACE_EXISTING:
1020 toAdd = CertDuplicateCRLContext(pCrlContext);
1022 case CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES:
1023 toAdd = CertDuplicateCRLContext(pCrlContext);
1025 CrlContext_CopyProperties(toAdd, existing);
1027 case CERT_STORE_ADD_USE_EXISTING:
1029 CrlContext_CopyProperties(existing, pCrlContext);
1032 FIXME("Unimplemented add disposition %d\n", dwAddDisposition);
1039 ret = store->crls.addContext(store, (void *)toAdd,
1040 (void *)existing, (const void **)ppStoreContext);
1041 else if (ppStoreContext)
1042 *ppStoreContext = CertDuplicateCRLContext(toAdd);
1043 CertFreeCRLContext(toAdd);
1045 CertFreeCRLContext(existing);
1047 TRACE("returning %d\n", ret);
1051 BOOL WINAPI CertDeleteCRLFromStore(PCCRL_CONTEXT pCrlContext)
1055 TRACE("(%p)\n", pCrlContext);
1059 else if (!pCrlContext->hCertStore)
1062 CertFreeCRLContext(pCrlContext);
1066 PWINECRYPT_CERTSTORE hcs =
1067 (PWINECRYPT_CERTSTORE)pCrlContext->hCertStore;
1069 if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
1072 ret = hcs->crls.deleteContext(hcs, (void *)pCrlContext);
1073 CertFreeCRLContext(pCrlContext);
1078 PCCRL_CONTEXT WINAPI CertEnumCRLsInStore(HCERTSTORE hCertStore,
1079 PCCRL_CONTEXT pPrev)
1081 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
1084 TRACE("(%p, %p)\n", hCertStore, pPrev);
1087 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
1090 ret = (PCCRL_CONTEXT)hcs->crls.enumContext(hcs, (void *)pPrev);
1094 HCERTSTORE WINAPI CertDuplicateStore(HCERTSTORE hCertStore)
1096 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
1098 TRACE("(%p)\n", hCertStore);
1100 if (hcs && hcs->dwMagic == WINE_CRYPTCERTSTORE_MAGIC)
1101 InterlockedIncrement(&hcs->ref);
1105 BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
1107 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *) hCertStore;
1109 TRACE("(%p, %08x)\n", hCertStore, dwFlags);
1114 if ( hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC )
1117 if (InterlockedDecrement(&hcs->ref) == 0)
1119 TRACE("%p's ref count is 0, freeing\n", hcs);
1121 hcs->closeStore(hcs, dwFlags);
1124 TRACE("%p's ref count is %d\n", hcs, hcs->ref);
1128 BOOL WINAPI CertControlStore(HCERTSTORE hCertStore, DWORD dwFlags,
1129 DWORD dwCtrlType, void const *pvCtrlPara)
1131 WINECRYPT_CERTSTORE *hcs = (WINECRYPT_CERTSTORE *)hCertStore;
1134 TRACE("(%p, %08x, %d, %p)\n", hCertStore, dwFlags, dwCtrlType,
1139 else if (hcs->dwMagic != WINE_CRYPTCERTSTORE_MAGIC)
1144 ret = hcs->control(hCertStore, dwFlags, dwCtrlType, pvCtrlPara);
1151 BOOL WINAPI CertGetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
1152 void *pvData, DWORD *pcbData)
1154 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
1157 TRACE("(%p, %d, %p, %p)\n", hCertStore, dwPropId, pvData, pcbData);
1161 case CERT_ACCESS_STATE_PROP_ID:
1164 *pcbData = sizeof(DWORD);
1167 else if (*pcbData < sizeof(DWORD))
1169 SetLastError(ERROR_MORE_DATA);
1170 *pcbData = sizeof(DWORD);
1176 if (store->type != StoreTypeMem &&
1177 !(store->dwOpenFlags & CERT_STORE_READONLY_FLAG))
1178 state |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
1179 *(DWORD *)pvData = state;
1184 if (store->properties)
1186 CRYPT_DATA_BLOB blob;
1188 ret = ContextPropertyList_FindProperty(store->properties, dwPropId,
1193 *pcbData = blob.cbData;
1194 else if (*pcbData < blob.cbData)
1196 SetLastError(ERROR_MORE_DATA);
1197 *pcbData = blob.cbData;
1202 memcpy(pvData, blob.pbData, blob.cbData);
1203 *pcbData = blob.cbData;
1207 SetLastError(CRYPT_E_NOT_FOUND);
1210 SetLastError(CRYPT_E_NOT_FOUND);
1215 BOOL WINAPI CertSetStoreProperty(HCERTSTORE hCertStore, DWORD dwPropId,
1216 DWORD dwFlags, const void *pvData)
1218 PWINECRYPT_CERTSTORE store = (PWINECRYPT_CERTSTORE)hCertStore;
1221 TRACE("(%p, %d, %08x, %p)\n", hCertStore, dwPropId, dwFlags, pvData);
1223 if (!store->properties)
1224 store->properties = ContextPropertyList_Create();
1227 case CERT_ACCESS_STATE_PROP_ID:
1228 SetLastError(E_INVALIDARG);
1233 const CRYPT_DATA_BLOB *blob = (const CRYPT_DATA_BLOB *)pvData;
1235 ret = ContextPropertyList_SetProperty(store->properties, dwPropId,
1236 blob->pbData, blob->cbData);
1240 ContextPropertyList_RemoveProperty(store->properties, dwPropId);
1247 static LONG CRYPT_OpenParentStore(DWORD dwFlags,
1248 void *pvSystemStoreLocationPara, HKEY *key)
1253 TRACE("(%08x, %p)\n", dwFlags, pvSystemStoreLocationPara);
1255 switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
1257 case CERT_SYSTEM_STORE_LOCAL_MACHINE:
1258 root = HKEY_LOCAL_MACHINE;
1259 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1261 case CERT_SYSTEM_STORE_CURRENT_USER:
1262 root = HKEY_CURRENT_USER;
1263 base = CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH;
1265 case CERT_SYSTEM_STORE_CURRENT_SERVICE:
1266 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1267 * SystemCertificates
1269 FIXME("CERT_SYSTEM_STORE_CURRENT_SERVICE\n");
1270 return ERROR_FILE_NOT_FOUND;
1271 case CERT_SYSTEM_STORE_SERVICES:
1272 /* hklm\Software\Microsoft\Cryptography\Services\servicename\
1273 * SystemCertificates
1275 FIXME("CERT_SYSTEM_STORE_SERVICES\n");
1276 return ERROR_FILE_NOT_FOUND;
1277 case CERT_SYSTEM_STORE_USERS:
1278 /* hku\user sid\Software\Microsoft\SystemCertificates */
1279 FIXME("CERT_SYSTEM_STORE_USERS\n");
1280 return ERROR_FILE_NOT_FOUND;
1281 case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
1282 root = HKEY_CURRENT_USER;
1283 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1285 case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
1286 root = HKEY_LOCAL_MACHINE;
1287 base = CERT_GROUP_POLICY_SYSTEM_STORE_REGPATH;
1289 case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
1290 /* hklm\Software\Microsoft\EnterpriseCertificates */
1291 FIXME("CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE\n");
1292 return ERROR_FILE_NOT_FOUND;
1294 return ERROR_FILE_NOT_FOUND;
1297 return RegOpenKeyExW(root, base, 0, KEY_READ, key);
1300 BOOL WINAPI CertEnumSystemStore(DWORD dwFlags, void *pvSystemStoreLocationPara,
1301 void *pvArg, PFN_CERT_ENUM_SYSTEM_STORE pfnEnum)
1307 TRACE("(%08x, %p, %p, %p)\n", dwFlags, pvSystemStoreLocationPara, pvArg,
1310 rc = CRYPT_OpenParentStore(dwFlags, pvArg, &key);
1314 CERT_SYSTEM_STORE_INFO info = { sizeof(info) };
1318 WCHAR name[MAX_PATH];
1319 DWORD size = sizeof(name) / sizeof(name[0]);
1321 rc = RegEnumKeyExW(key, index++, name, &size, NULL, NULL, NULL,
1324 ret = pfnEnum(name, 0, &info, NULL, pvArg);
1325 } while (ret && !rc);
1326 if (ret && rc != ERROR_NO_MORE_ITEMS)