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
19 #include "crypt32_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
25 typedef struct _WINE_HASH_TO_DELETE
29 } WINE_HASH_TO_DELETE
;
31 typedef struct _WINE_REGSTOREINFO
38 struct list certsToDelete
;
39 struct list crlsToDelete
;
40 struct list ctlsToDelete
;
43 static void CRYPT_HashToStr(const BYTE
*hash
, LPWSTR asciiHash
)
45 static const WCHAR fmt
[] = { '%','0','2','X',0 };
51 for (i
= 0; i
< 20; i
++)
52 wsprintfW(asciiHash
+ i
* 2, fmt
, hash
[i
]);
55 static const WCHAR CertsW
[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
57 static const WCHAR CRLsW
[] = { 'C','R','L','s',0 };
58 static const WCHAR CTLsW
[] = { 'C','T','L','s',0 };
59 static const WCHAR BlobW
[] = { 'B','l','o','b',0 };
61 static void CRYPT_RegReadSerializedFromReg(HKEY key
, DWORD contextType
,
66 WCHAR subKeyName
[MAX_PATH
];
69 DWORD size
= sizeof(subKeyName
) / sizeof(WCHAR
);
71 rc
= RegEnumKeyExW(key
, index
++, subKeyName
, &size
, NULL
, NULL
, NULL
,
77 rc
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
83 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, NULL
, &size
);
85 buf
= CryptMemAlloc(size
);
88 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, buf
,
95 TRACE("Adding cert with hash %s\n",
96 debugstr_w(subKeyName
));
97 context
= CRYPT_ReadSerializedElement(buf
, size
,
98 contextType
, &addedType
);
101 const WINE_CONTEXT_INTERFACE
*contextInterface
;
106 case CERT_STORE_CERTIFICATE_CONTEXT
:
107 contextInterface
= pCertInterface
;
109 case CERT_STORE_CRL_CONTEXT
:
110 contextInterface
= pCRLInterface
;
112 case CERT_STORE_CTL_CONTEXT
:
113 contextInterface
= pCTLInterface
;
116 contextInterface
= NULL
;
118 if (contextInterface
)
121 if (contextInterface
->getProp(context
,
122 CERT_HASH_PROP_ID
, hash
, &size
))
124 WCHAR asciiHash
[20 * 2 + 1];
126 CRYPT_HashToStr(hash
, asciiHash
);
127 TRACE("comparing %s\n",
128 debugstr_w(asciiHash
));
129 TRACE("with %s\n", debugstr_w(subKeyName
));
130 if (!lstrcmpW(asciiHash
, subKeyName
))
132 TRACE("hash matches, adding\n");
133 contextInterface
->addContextToStore(
135 CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
138 TRACE("hash doesn't match, ignoring\n");
140 Context_Release(context_from_ptr(context
));
148 /* Ignore intermediate errors, continue enumerating */
154 static void CRYPT_RegReadFromReg(HKEY key
, HCERTSTORE store
)
156 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
157 static const DWORD contextFlags
[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG
,
158 CERT_STORE_CRL_CONTEXT_FLAG
, CERT_STORE_CTL_CONTEXT_FLAG
};
161 for (i
= 0; i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
166 rc
= RegCreateKeyExW(key
, subKeys
[i
], 0, NULL
, 0, KEY_READ
, NULL
,
170 CRYPT_RegReadSerializedFromReg(hKey
, contextFlags
[i
], store
);
176 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
177 static BOOL
CRYPT_WriteSerializedToReg(HKEY key
, const BYTE
*hash
, const BYTE
*buf
,
180 WCHAR asciiHash
[20 * 2 + 1];
185 CRYPT_HashToStr(hash
, asciiHash
);
186 rc
= RegCreateKeyExW(key
, asciiHash
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
,
190 rc
= RegSetValueExW(subKey
, BlobW
, 0, REG_BINARY
, buf
, len
);
203 static BOOL
CRYPT_SerializeContextsToReg(HKEY key
,
204 const WINE_CONTEXT_INTERFACE
*contextInterface
, HCERTSTORE memStore
)
206 const void *context
= NULL
;
210 context
= contextInterface
->enumContextsInStore(memStore
, context
);
214 DWORD hashSize
= sizeof(hash
);
216 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
, hash
,
223 ret
= contextInterface
->serialize(context
, 0, NULL
, &size
);
225 buf
= CryptMemAlloc(size
);
228 ret
= contextInterface
->serialize(context
, 0, buf
, &size
);
230 ret
= CRYPT_WriteSerializedToReg(key
, hash
, buf
, size
);
237 } while (ret
&& context
!= NULL
);
239 Context_Release(context_from_ptr(context
));
243 static BOOL
CRYPT_RegWriteToReg(WINE_REGSTOREINFO
*store
)
245 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
246 const WINE_CONTEXT_INTERFACE
* const interfaces
[] = { pCertInterface
,
247 pCRLInterface
, pCTLInterface
};
248 struct list
*listToDelete
[] = { &store
->certsToDelete
, &store
->crlsToDelete
,
249 &store
->ctlsToDelete
};
253 for (i
= 0; ret
&& i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
256 LONG rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0,
257 KEY_ALL_ACCESS
, NULL
, &key
, NULL
);
263 WINE_HASH_TO_DELETE
*toDelete
, *next
;
264 WCHAR asciiHash
[20 * 2 + 1];
266 EnterCriticalSection(&store
->cs
);
267 LIST_FOR_EACH_ENTRY_SAFE(toDelete
, next
, listToDelete
[i
],
268 WINE_HASH_TO_DELETE
, entry
)
272 CRYPT_HashToStr(toDelete
->hash
, asciiHash
);
273 TRACE("Removing %s\n", debugstr_w(asciiHash
));
274 rc
= RegDeleteKeyW(key
, asciiHash
);
275 if (rc
!= ERROR_SUCCESS
&& rc
!= ERROR_FILE_NOT_FOUND
)
280 list_remove(&toDelete
->entry
);
281 CryptMemFree(toDelete
);
283 LeaveCriticalSection(&store
->cs
);
285 ret
= CRYPT_SerializeContextsToReg(key
, interfaces
[i
],
298 /* If force is true or the registry store is dirty, writes the contents of the
299 * store to the registry.
301 static BOOL
CRYPT_RegFlushStore(WINE_REGSTOREINFO
*store
, BOOL force
)
305 TRACE("(%p, %d)\n", store
, force
);
307 if (store
->dirty
|| force
)
308 ret
= CRYPT_RegWriteToReg(store
);
314 static void WINAPI
CRYPT_RegCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
316 WINE_REGSTOREINFO
*store
= hCertStore
;
318 TRACE("(%p, %08x)\n", store
, dwFlags
);
320 FIXME("Unimplemented flags: %08x\n", dwFlags
);
322 CRYPT_RegFlushStore(store
, FALSE
);
323 RegCloseKey(store
->key
);
324 store
->cs
.DebugInfo
->Spare
[0] = 0;
325 DeleteCriticalSection(&store
->cs
);
329 static BOOL
CRYPT_RegWriteContext(WINE_REGSTOREINFO
*store
,
330 const void *context
, DWORD dwFlags
)
334 if (dwFlags
& CERT_STORE_PROV_WRITE_ADD_FLAG
)
344 static BOOL
CRYPT_RegDeleteContext(WINE_REGSTOREINFO
*store
,
345 struct list
*deleteList
, const void *context
,
346 const WINE_CONTEXT_INTERFACE
*contextInterface
)
350 if (store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
352 SetLastError(ERROR_ACCESS_DENIED
);
357 WINE_HASH_TO_DELETE
*toDelete
= CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE
));
361 DWORD size
= sizeof(toDelete
->hash
);
363 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
,
364 toDelete
->hash
, &size
);
367 EnterCriticalSection(&store
->cs
);
368 list_add_tail(deleteList
, &toDelete
->entry
);
369 LeaveCriticalSection(&store
->cs
);
373 CryptMemFree(toDelete
);
385 static BOOL WINAPI
CRYPT_RegWriteCert(HCERTSTORE hCertStore
,
386 PCCERT_CONTEXT cert
, DWORD dwFlags
)
388 WINE_REGSTOREINFO
*store
= hCertStore
;
390 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
392 return CRYPT_RegWriteContext(store
, cert
, dwFlags
);
395 static BOOL WINAPI
CRYPT_RegDeleteCert(HCERTSTORE hCertStore
,
396 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
398 WINE_REGSTOREINFO
*store
= hCertStore
;
400 TRACE("(%p, %p, %08x)\n", store
, pCertContext
, dwFlags
);
402 return CRYPT_RegDeleteContext(store
, &store
->certsToDelete
, pCertContext
,
406 static BOOL WINAPI
CRYPT_RegWriteCRL(HCERTSTORE hCertStore
,
407 PCCRL_CONTEXT crl
, DWORD dwFlags
)
409 WINE_REGSTOREINFO
*store
= hCertStore
;
411 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
413 return CRYPT_RegWriteContext(store
, crl
, dwFlags
);
416 static BOOL WINAPI
CRYPT_RegDeleteCRL(HCERTSTORE hCertStore
,
417 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
419 WINE_REGSTOREINFO
*store
= hCertStore
;
421 TRACE("(%p, %p, %08x)\n", store
, pCrlContext
, dwFlags
);
423 return CRYPT_RegDeleteContext(store
, &store
->crlsToDelete
, pCrlContext
,
427 static BOOL WINAPI
CRYPT_RegWriteCTL(HCERTSTORE hCertStore
,
428 PCCTL_CONTEXT ctl
, DWORD dwFlags
)
430 WINE_REGSTOREINFO
*store
= hCertStore
;
432 TRACE("(%p, %p, %d)\n", hCertStore
, ctl
, dwFlags
);
434 return CRYPT_RegWriteContext(store
, ctl
, dwFlags
);
437 static BOOL WINAPI
CRYPT_RegDeleteCTL(HCERTSTORE hCertStore
,
438 PCCTL_CONTEXT pCtlContext
, DWORD dwFlags
)
440 WINE_REGSTOREINFO
*store
= hCertStore
;
442 TRACE("(%p, %p, %08x)\n", store
, pCtlContext
, dwFlags
);
444 return CRYPT_RegDeleteContext(store
, &store
->ctlsToDelete
, pCtlContext
,
448 static BOOL WINAPI
CRYPT_RegControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
449 DWORD dwCtrlType
, void const *pvCtrlPara
)
451 WINE_REGSTOREINFO
*store
= hCertStore
;
454 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
459 case CERT_STORE_CTRL_RESYNC
:
461 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
462 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
464 CRYPT_RegFlushStore(store
, FALSE
);
465 CRYPT_RegReadFromReg(store
->key
, memStore
);
466 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
467 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");
480 FIXME("%d: stub\n", dwCtrlType
);
486 static void *regProvFuncs
[] = {
488 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
491 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
492 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
495 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
496 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
499 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
503 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
506 WINECRYPT_CERTSTORE
*store
= NULL
;
508 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
510 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
512 DWORD rc
= RegDeleteTreeW((HKEY
)pvPara
, CertsW
);
514 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
515 rc
= RegDeleteTreeW((HKEY
)pvPara
, CRLsW
);
516 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
517 rc
= RegDeleteTreeW((HKEY
)pvPara
, CTLsW
);
518 if (rc
== ERROR_NO_MORE_ITEMS
)
526 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
527 GetCurrentProcess(), (LPHANDLE
)&key
,
528 dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
: KEY_ALL_ACCESS
,
531 WINECRYPT_CERTSTORE
*memStore
;
533 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, hCryptProv
,
534 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
537 WINE_REGSTOREINFO
*regInfo
= CryptMemAlloc(
538 sizeof(WINE_REGSTOREINFO
));
542 CERT_STORE_PROV_INFO provInfo
= { 0 };
544 regInfo
->dwOpenFlags
= dwFlags
;
545 regInfo
->memStore
= memStore
;
547 InitializeCriticalSection(®Info
->cs
);
548 regInfo
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PWINE_REGSTOREINFO->cs");
549 list_init(®Info
->certsToDelete
);
550 list_init(®Info
->crlsToDelete
);
551 list_init(®Info
->ctlsToDelete
);
552 CRYPT_RegReadFromReg(regInfo
->key
, regInfo
->memStore
);
553 regInfo
->dirty
= FALSE
;
554 provInfo
.cbSize
= sizeof(provInfo
);
555 provInfo
.cStoreProvFunc
= sizeof(regProvFuncs
) /
556 sizeof(regProvFuncs
[0]);
557 provInfo
.rgpvStoreProvFunc
= regProvFuncs
;
558 provInfo
.hStoreProv
= regInfo
;
559 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
560 /* Reg store doesn't need crypto provider, so close it */
562 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
563 CryptReleaseContext(hCryptProv
, 0);
568 TRACE("returning %p\n", store
);