2 * Copyright 2004-2007 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
26 #include "crypt32_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
30 typedef struct _WINE_HASH_TO_DELETE
34 } WINE_HASH_TO_DELETE
;
36 typedef struct _WINE_REGSTOREINFO
43 struct list certsToDelete
;
44 struct list crlsToDelete
;
45 struct list ctlsToDelete
;
48 static void CRYPT_HashToStr(const BYTE
*hash
, LPWSTR asciiHash
)
55 for (i
= 0; i
< 20; i
++)
56 wsprintfW(asciiHash
+ i
* 2, L
"%02X", hash
[i
]);
59 static void CRYPT_RegReadSerializedFromReg(HKEY key
, DWORD contextType
,
64 WCHAR subKeyName
[MAX_PATH
];
67 DWORD size
= ARRAY_SIZE(subKeyName
);
69 rc
= RegEnumKeyExW(key
, index
++, subKeyName
, &size
, NULL
, NULL
, NULL
,
75 rc
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
81 rc
= RegQueryValueExW(subKey
, L
"Blob", NULL
, NULL
, NULL
, &size
);
83 buf
= CryptMemAlloc(size
);
86 rc
= RegQueryValueExW(subKey
, L
"Blob", NULL
, NULL
, buf
,
93 TRACE("Adding cert with hash %s\n",
94 debugstr_w(subKeyName
));
95 context
= CRYPT_ReadSerializedElement(buf
, size
,
96 contextType
, &addedType
);
99 const WINE_CONTEXT_INTERFACE
*contextInterface
;
104 case CERT_STORE_CERTIFICATE_CONTEXT
:
105 contextInterface
= pCertInterface
;
107 case CERT_STORE_CRL_CONTEXT
:
108 contextInterface
= pCRLInterface
;
110 case CERT_STORE_CTL_CONTEXT
:
111 contextInterface
= pCTLInterface
;
114 contextInterface
= NULL
;
116 if (contextInterface
)
119 if (contextInterface
->getProp(context
,
120 CERT_HASH_PROP_ID
, hash
, &size
))
122 WCHAR asciiHash
[20 * 2 + 1];
124 CRYPT_HashToStr(hash
, asciiHash
);
125 TRACE("comparing %s\n",
126 debugstr_w(asciiHash
));
127 TRACE("with %s\n", debugstr_w(subKeyName
));
128 if (!wcscmp(asciiHash
, subKeyName
))
130 TRACE("hash matches, adding\n");
131 contextInterface
->addContextToStore(
133 CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
136 TRACE("hash doesn't match, ignoring\n");
138 Context_Release(context_from_ptr(context
));
146 /* Ignore intermediate errors, continue enumerating */
152 static void CRYPT_RegReadFromReg(HKEY key
, HCERTSTORE store
)
154 static const WCHAR
* const subKeys
[] = { L
"Certificates", L
"CRLs", L
"CTLs" };
155 static const DWORD contextFlags
[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG
,
156 CERT_STORE_CRL_CONTEXT_FLAG
, CERT_STORE_CTL_CONTEXT_FLAG
};
159 for (i
= 0; i
< ARRAY_SIZE(subKeys
); i
++)
164 rc
= RegCreateKeyExW(key
, subKeys
[i
], 0, NULL
, 0, KEY_READ
, NULL
,
168 CRYPT_RegReadSerializedFromReg(hKey
, contextFlags
[i
], store
);
174 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
175 static BOOL
CRYPT_WriteSerializedToReg(HKEY key
, DWORD flags
, const BYTE
*hash
, const BYTE
*buf
,
178 WCHAR asciiHash
[20 * 2 + 1];
183 CRYPT_HashToStr(hash
, asciiHash
);
184 rc
= RegCreateKeyExW(key
, asciiHash
, 0, NULL
, flags
, KEY_ALL_ACCESS
, NULL
,
188 rc
= RegSetValueExW(subKey
, L
"Blob", 0, REG_BINARY
, buf
, len
);
201 BOOL
CRYPT_SerializeContextsToReg(HKEY key
, DWORD flags
,
202 const WINE_CONTEXT_INTERFACE
*contextInterface
, HCERTSTORE memStore
)
204 const void *context
= NULL
;
208 context
= contextInterface
->enumContextsInStore(memStore
, context
);
212 DWORD hashSize
= sizeof(hash
);
214 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
, hash
,
221 ret
= contextInterface
->serialize(context
, 0, NULL
, &size
);
223 buf
= CryptMemAlloc(size
);
226 ret
= contextInterface
->serialize(context
, 0, buf
, &size
);
228 ret
= CRYPT_WriteSerializedToReg(key
, flags
, hash
, buf
, size
);
235 } while (ret
&& context
!= NULL
);
237 Context_Release(context_from_ptr(context
));
241 static BOOL
CRYPT_RegWriteToReg(WINE_REGSTOREINFO
*store
)
243 static const WCHAR
* const subKeys
[] = { L
"Certificates", L
"CRLs", L
"CTLs" };
244 const WINE_CONTEXT_INTERFACE
* const interfaces
[] = { pCertInterface
,
245 pCRLInterface
, pCTLInterface
};
246 struct list
*listToDelete
[] = { &store
->certsToDelete
, &store
->crlsToDelete
,
247 &store
->ctlsToDelete
};
251 for (i
= 0; ret
&& i
< ARRAY_SIZE(subKeys
); i
++)
254 LONG rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0,
255 KEY_ALL_ACCESS
, NULL
, &key
, NULL
);
261 WINE_HASH_TO_DELETE
*toDelete
, *next
;
262 WCHAR asciiHash
[20 * 2 + 1];
264 EnterCriticalSection(&store
->cs
);
265 LIST_FOR_EACH_ENTRY_SAFE(toDelete
, next
, listToDelete
[i
],
266 WINE_HASH_TO_DELETE
, entry
)
270 CRYPT_HashToStr(toDelete
->hash
, asciiHash
);
271 TRACE("Removing %s\n", debugstr_w(asciiHash
));
272 rc
= RegDeleteKeyW(key
, asciiHash
);
273 if (rc
!= ERROR_SUCCESS
&& rc
!= ERROR_FILE_NOT_FOUND
)
278 list_remove(&toDelete
->entry
);
279 CryptMemFree(toDelete
);
281 LeaveCriticalSection(&store
->cs
);
283 ret
= CRYPT_SerializeContextsToReg(key
, 0, interfaces
[i
], store
->memStore
);
295 /* If force is true or the registry store is dirty, writes the contents of the
296 * store to the registry.
298 static BOOL
CRYPT_RegFlushStore(WINE_REGSTOREINFO
*store
, BOOL force
)
302 TRACE("(%p, %d)\n", store
, force
);
304 if (store
->dirty
|| force
)
306 ret
= CRYPT_RegWriteToReg(store
);
308 store
->dirty
= FALSE
;
315 static void WINAPI
CRYPT_RegCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
317 WINE_REGSTOREINFO
*store
= hCertStore
;
319 TRACE("(%p, %08lx)\n", store
, dwFlags
);
321 FIXME("Unimplemented flags: %08lx\n", dwFlags
);
323 CRYPT_RegFlushStore(store
, FALSE
);
324 RegCloseKey(store
->key
);
325 store
->cs
.DebugInfo
->Spare
[0] = 0;
326 DeleteCriticalSection(&store
->cs
);
330 static BOOL
CRYPT_RegWriteContext(WINE_REGSTOREINFO
*store
,
331 const void *context
, DWORD dwFlags
)
335 if (dwFlags
& CERT_STORE_PROV_WRITE_ADD_FLAG
)
345 static BOOL
CRYPT_RegDeleteContext(WINE_REGSTOREINFO
*store
,
346 struct list
*deleteList
, const void *context
,
347 const WINE_CONTEXT_INTERFACE
*contextInterface
)
351 if (store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
353 SetLastError(ERROR_ACCESS_DENIED
);
358 WINE_HASH_TO_DELETE
*toDelete
= CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE
));
362 DWORD size
= sizeof(toDelete
->hash
);
364 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
,
365 toDelete
->hash
, &size
);
368 EnterCriticalSection(&store
->cs
);
369 list_add_tail(deleteList
, &toDelete
->entry
);
370 LeaveCriticalSection(&store
->cs
);
374 CryptMemFree(toDelete
);
386 static BOOL WINAPI
CRYPT_RegWriteCert(HCERTSTORE hCertStore
,
387 PCCERT_CONTEXT cert
, DWORD dwFlags
)
389 WINE_REGSTOREINFO
*store
= hCertStore
;
391 TRACE("(%p, %p, %ld)\n", hCertStore
, cert
, dwFlags
);
393 return CRYPT_RegWriteContext(store
, cert
, dwFlags
);
396 static BOOL WINAPI
CRYPT_RegDeleteCert(HCERTSTORE hCertStore
,
397 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
399 WINE_REGSTOREINFO
*store
= hCertStore
;
401 TRACE("(%p, %p, %08lx)\n", store
, pCertContext
, dwFlags
);
403 return CRYPT_RegDeleteContext(store
, &store
->certsToDelete
, pCertContext
,
407 static BOOL WINAPI
CRYPT_RegWriteCRL(HCERTSTORE hCertStore
,
408 PCCRL_CONTEXT crl
, DWORD dwFlags
)
410 WINE_REGSTOREINFO
*store
= hCertStore
;
412 TRACE("(%p, %p, %ld)\n", hCertStore
, crl
, dwFlags
);
414 return CRYPT_RegWriteContext(store
, crl
, dwFlags
);
417 static BOOL WINAPI
CRYPT_RegDeleteCRL(HCERTSTORE hCertStore
,
418 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
420 WINE_REGSTOREINFO
*store
= hCertStore
;
422 TRACE("(%p, %p, %08lx)\n", store
, pCrlContext
, dwFlags
);
424 return CRYPT_RegDeleteContext(store
, &store
->crlsToDelete
, pCrlContext
,
428 static BOOL WINAPI
CRYPT_RegWriteCTL(HCERTSTORE hCertStore
,
429 PCCTL_CONTEXT ctl
, DWORD dwFlags
)
431 WINE_REGSTOREINFO
*store
= hCertStore
;
433 TRACE("(%p, %p, %ld)\n", hCertStore
, ctl
, dwFlags
);
435 return CRYPT_RegWriteContext(store
, ctl
, dwFlags
);
438 static BOOL WINAPI
CRYPT_RegDeleteCTL(HCERTSTORE hCertStore
,
439 PCCTL_CONTEXT pCtlContext
, DWORD dwFlags
)
441 WINE_REGSTOREINFO
*store
= hCertStore
;
443 TRACE("(%p, %p, %08lx)\n", store
, pCtlContext
, dwFlags
);
445 return CRYPT_RegDeleteContext(store
, &store
->ctlsToDelete
, pCtlContext
,
449 static BOOL WINAPI
CRYPT_RegControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
450 DWORD dwCtrlType
, void const *pvCtrlPara
)
452 WINE_REGSTOREINFO
*store
= hCertStore
;
455 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
460 case CERT_STORE_CTRL_RESYNC
:
462 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
463 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
465 CRYPT_RegFlushStore(store
, FALSE
);
466 CRYPT_RegReadFromReg(store
->key
, memStore
);
467 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
468 CertCloseStore(memStore
, 0);
471 case CERT_STORE_CTRL_COMMIT
:
472 ret
= CRYPT_RegFlushStore(store
,
473 dwFlags
& CERT_STORE_CTRL_COMMIT_FORCE_FLAG
);
475 case CERT_STORE_CTRL_AUTO_RESYNC
:
476 FIXME("CERT_STORE_CTRL_AUTO_RESYNC: stub\n");
478 case CERT_STORE_CTRL_NOTIFY_CHANGE
:
479 FIXME("CERT_STORE_CTRL_NOTIFY_CHANGE: stub\n");
482 FIXME("%lu: stub\n", dwCtrlType
);
488 static void *regProvFuncs
[] = {
490 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
493 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
494 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
497 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
498 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
501 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
505 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
508 WINECRYPT_CERTSTORE
*store
= NULL
;
510 TRACE("(%Id, %08lx, %p)\n", hCryptProv
, dwFlags
, pvPara
);
512 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
514 DWORD rc
= RegDeleteTreeW((HKEY
)pvPara
, L
"Certificates");
516 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
517 rc
= RegDeleteTreeW((HKEY
)pvPara
, L
"CRLs");
518 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
519 rc
= RegDeleteTreeW((HKEY
)pvPara
, L
"CTLs");
520 if (rc
== ERROR_NO_MORE_ITEMS
)
528 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
529 GetCurrentProcess(), (LPHANDLE
)&key
,
530 dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
: KEY_ALL_ACCESS
,
533 WINECRYPT_CERTSTORE
*memStore
;
535 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, hCryptProv
,
536 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
539 WINE_REGSTOREINFO
*regInfo
= CryptMemAlloc(
540 sizeof(WINE_REGSTOREINFO
));
544 CERT_STORE_PROV_INFO provInfo
= { 0 };
546 regInfo
->dwOpenFlags
= dwFlags
;
547 regInfo
->memStore
= memStore
;
549 InitializeCriticalSection(®Info
->cs
);
550 regInfo
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PWINE_REGSTOREINFO->cs");
551 list_init(®Info
->certsToDelete
);
552 list_init(®Info
->crlsToDelete
);
553 list_init(®Info
->ctlsToDelete
);
554 CRYPT_RegReadFromReg(regInfo
->key
, regInfo
->memStore
);
555 regInfo
->dirty
= FALSE
;
556 provInfo
.cbSize
= sizeof(provInfo
);
557 provInfo
.cStoreProvFunc
= ARRAY_SIZE(regProvFuncs
);
558 provInfo
.rgpvStoreProvFunc
= regProvFuncs
;
559 provInfo
.hStoreProv
= regInfo
;
560 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
561 /* Reg store doesn't need crypto provider, so close it */
563 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
564 CryptReleaseContext(hCryptProv
, 0);
569 TRACE("returning %p\n", store
);