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
)
50 static const WCHAR fmt
[] = { '%','0','2','X',0 };
56 for (i
= 0; i
< 20; i
++)
57 wsprintfW(asciiHash
+ i
* 2, fmt
, hash
[i
]);
60 static const WCHAR CertsW
[] = { 'C','e','r','t','i','f','i','c','a','t','e','s',
62 static const WCHAR CRLsW
[] = { 'C','R','L','s',0 };
63 static const WCHAR CTLsW
[] = { 'C','T','L','s',0 };
64 static const WCHAR BlobW
[] = { 'B','l','o','b',0 };
66 static void CRYPT_RegReadSerializedFromReg(HKEY key
, DWORD contextType
,
71 WCHAR subKeyName
[MAX_PATH
];
74 DWORD size
= sizeof(subKeyName
) / sizeof(WCHAR
);
76 rc
= RegEnumKeyExW(key
, index
++, subKeyName
, &size
, NULL
, NULL
, NULL
,
82 rc
= RegOpenKeyExW(key
, subKeyName
, 0, KEY_READ
, &subKey
);
88 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, NULL
, &size
);
90 buf
= CryptMemAlloc(size
);
93 rc
= RegQueryValueExW(subKey
, BlobW
, NULL
, NULL
, buf
,
100 TRACE("Adding cert with hash %s\n",
101 debugstr_w(subKeyName
));
102 context
= CRYPT_ReadSerializedElement(buf
, size
,
103 contextType
, &addedType
);
106 const WINE_CONTEXT_INTERFACE
*contextInterface
;
111 case CERT_STORE_CERTIFICATE_CONTEXT
:
112 contextInterface
= pCertInterface
;
114 case CERT_STORE_CRL_CONTEXT
:
115 contextInterface
= pCRLInterface
;
117 case CERT_STORE_CTL_CONTEXT
:
118 contextInterface
= pCTLInterface
;
121 contextInterface
= NULL
;
123 if (contextInterface
)
126 if (contextInterface
->getProp(context
,
127 CERT_HASH_PROP_ID
, hash
, &size
))
129 WCHAR asciiHash
[20 * 2 + 1];
131 CRYPT_HashToStr(hash
, asciiHash
);
132 TRACE("comparing %s\n",
133 debugstr_w(asciiHash
));
134 TRACE("with %s\n", debugstr_w(subKeyName
));
135 if (!lstrcmpW(asciiHash
, subKeyName
))
137 TRACE("hash matches, adding\n");
138 contextInterface
->addContextToStore(
140 CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
143 TRACE("hash doesn't match, ignoring\n");
145 Context_Release(context_from_ptr(context
));
153 /* Ignore intermediate errors, continue enumerating */
159 static void CRYPT_RegReadFromReg(HKEY key
, HCERTSTORE store
)
161 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
162 static const DWORD contextFlags
[] = { CERT_STORE_CERTIFICATE_CONTEXT_FLAG
,
163 CERT_STORE_CRL_CONTEXT_FLAG
, CERT_STORE_CTL_CONTEXT_FLAG
};
166 for (i
= 0; i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
171 rc
= RegCreateKeyExW(key
, subKeys
[i
], 0, NULL
, 0, KEY_READ
, NULL
,
175 CRYPT_RegReadSerializedFromReg(hKey
, contextFlags
[i
], store
);
181 /* Hash is assumed to be 20 bytes in length (a SHA-1 hash) */
182 static BOOL
CRYPT_WriteSerializedToReg(HKEY key
, const BYTE
*hash
, const BYTE
*buf
,
185 WCHAR asciiHash
[20 * 2 + 1];
190 CRYPT_HashToStr(hash
, asciiHash
);
191 rc
= RegCreateKeyExW(key
, asciiHash
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
,
195 rc
= RegSetValueExW(subKey
, BlobW
, 0, REG_BINARY
, buf
, len
);
208 static BOOL
CRYPT_SerializeContextsToReg(HKEY key
,
209 const WINE_CONTEXT_INTERFACE
*contextInterface
, HCERTSTORE memStore
)
211 const void *context
= NULL
;
215 context
= contextInterface
->enumContextsInStore(memStore
, context
);
219 DWORD hashSize
= sizeof(hash
);
221 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
, hash
,
228 ret
= contextInterface
->serialize(context
, 0, NULL
, &size
);
230 buf
= CryptMemAlloc(size
);
233 ret
= contextInterface
->serialize(context
, 0, buf
, &size
);
235 ret
= CRYPT_WriteSerializedToReg(key
, hash
, buf
, size
);
242 } while (ret
&& context
!= NULL
);
244 Context_Release(context_from_ptr(context
));
248 static BOOL
CRYPT_RegWriteToReg(WINE_REGSTOREINFO
*store
)
250 static const WCHAR
* const subKeys
[] = { CertsW
, CRLsW
, CTLsW
};
251 const WINE_CONTEXT_INTERFACE
* const interfaces
[] = { pCertInterface
,
252 pCRLInterface
, pCTLInterface
};
253 struct list
*listToDelete
[] = { &store
->certsToDelete
, &store
->crlsToDelete
,
254 &store
->ctlsToDelete
};
258 for (i
= 0; ret
&& i
< sizeof(subKeys
) / sizeof(subKeys
[0]); i
++)
261 LONG rc
= RegCreateKeyExW(store
->key
, subKeys
[i
], 0, NULL
, 0,
262 KEY_ALL_ACCESS
, NULL
, &key
, NULL
);
268 WINE_HASH_TO_DELETE
*toDelete
, *next
;
269 WCHAR asciiHash
[20 * 2 + 1];
271 EnterCriticalSection(&store
->cs
);
272 LIST_FOR_EACH_ENTRY_SAFE(toDelete
, next
, listToDelete
[i
],
273 WINE_HASH_TO_DELETE
, entry
)
277 CRYPT_HashToStr(toDelete
->hash
, asciiHash
);
278 TRACE("Removing %s\n", debugstr_w(asciiHash
));
279 rc
= RegDeleteKeyW(key
, asciiHash
);
280 if (rc
!= ERROR_SUCCESS
&& rc
!= ERROR_FILE_NOT_FOUND
)
285 list_remove(&toDelete
->entry
);
286 CryptMemFree(toDelete
);
288 LeaveCriticalSection(&store
->cs
);
290 ret
= CRYPT_SerializeContextsToReg(key
, interfaces
[i
],
303 /* If force is true or the registry store is dirty, writes the contents of the
304 * store to the registry.
306 static BOOL
CRYPT_RegFlushStore(WINE_REGSTOREINFO
*store
, BOOL force
)
310 TRACE("(%p, %d)\n", store
, force
);
312 if (store
->dirty
|| force
)
313 ret
= CRYPT_RegWriteToReg(store
);
319 static void WINAPI
CRYPT_RegCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
321 WINE_REGSTOREINFO
*store
= hCertStore
;
323 TRACE("(%p, %08x)\n", store
, dwFlags
);
325 FIXME("Unimplemented flags: %08x\n", dwFlags
);
327 CRYPT_RegFlushStore(store
, FALSE
);
328 RegCloseKey(store
->key
);
329 store
->cs
.DebugInfo
->Spare
[0] = 0;
330 DeleteCriticalSection(&store
->cs
);
334 static BOOL
CRYPT_RegWriteContext(WINE_REGSTOREINFO
*store
,
335 const void *context
, DWORD dwFlags
)
339 if (dwFlags
& CERT_STORE_PROV_WRITE_ADD_FLAG
)
349 static BOOL
CRYPT_RegDeleteContext(WINE_REGSTOREINFO
*store
,
350 struct list
*deleteList
, const void *context
,
351 const WINE_CONTEXT_INTERFACE
*contextInterface
)
355 if (store
->dwOpenFlags
& CERT_STORE_READONLY_FLAG
)
357 SetLastError(ERROR_ACCESS_DENIED
);
362 WINE_HASH_TO_DELETE
*toDelete
= CryptMemAlloc(sizeof(WINE_HASH_TO_DELETE
));
366 DWORD size
= sizeof(toDelete
->hash
);
368 ret
= contextInterface
->getProp(context
, CERT_HASH_PROP_ID
,
369 toDelete
->hash
, &size
);
372 EnterCriticalSection(&store
->cs
);
373 list_add_tail(deleteList
, &toDelete
->entry
);
374 LeaveCriticalSection(&store
->cs
);
378 CryptMemFree(toDelete
);
390 static BOOL WINAPI
CRYPT_RegWriteCert(HCERTSTORE hCertStore
,
391 PCCERT_CONTEXT cert
, DWORD dwFlags
)
393 WINE_REGSTOREINFO
*store
= hCertStore
;
395 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
397 return CRYPT_RegWriteContext(store
, cert
, dwFlags
);
400 static BOOL WINAPI
CRYPT_RegDeleteCert(HCERTSTORE hCertStore
,
401 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
403 WINE_REGSTOREINFO
*store
= hCertStore
;
405 TRACE("(%p, %p, %08x)\n", store
, pCertContext
, dwFlags
);
407 return CRYPT_RegDeleteContext(store
, &store
->certsToDelete
, pCertContext
,
411 static BOOL WINAPI
CRYPT_RegWriteCRL(HCERTSTORE hCertStore
,
412 PCCRL_CONTEXT crl
, DWORD dwFlags
)
414 WINE_REGSTOREINFO
*store
= hCertStore
;
416 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
418 return CRYPT_RegWriteContext(store
, crl
, dwFlags
);
421 static BOOL WINAPI
CRYPT_RegDeleteCRL(HCERTSTORE hCertStore
,
422 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
424 WINE_REGSTOREINFO
*store
= hCertStore
;
426 TRACE("(%p, %p, %08x)\n", store
, pCrlContext
, dwFlags
);
428 return CRYPT_RegDeleteContext(store
, &store
->crlsToDelete
, pCrlContext
,
432 static BOOL WINAPI
CRYPT_RegWriteCTL(HCERTSTORE hCertStore
,
433 PCCTL_CONTEXT ctl
, DWORD dwFlags
)
435 WINE_REGSTOREINFO
*store
= hCertStore
;
437 TRACE("(%p, %p, %d)\n", hCertStore
, ctl
, dwFlags
);
439 return CRYPT_RegWriteContext(store
, ctl
, dwFlags
);
442 static BOOL WINAPI
CRYPT_RegDeleteCTL(HCERTSTORE hCertStore
,
443 PCCTL_CONTEXT pCtlContext
, DWORD dwFlags
)
445 WINE_REGSTOREINFO
*store
= hCertStore
;
447 TRACE("(%p, %p, %08x)\n", store
, pCtlContext
, dwFlags
);
449 return CRYPT_RegDeleteContext(store
, &store
->ctlsToDelete
, pCtlContext
,
453 static BOOL WINAPI
CRYPT_RegControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
454 DWORD dwCtrlType
, void const *pvCtrlPara
)
456 WINE_REGSTOREINFO
*store
= hCertStore
;
459 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
464 case CERT_STORE_CTRL_RESYNC
:
466 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
467 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
469 CRYPT_RegFlushStore(store
, FALSE
);
470 CRYPT_RegReadFromReg(store
->key
, memStore
);
471 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
472 CertCloseStore(memStore
, 0);
476 case CERT_STORE_CTRL_COMMIT
:
477 ret
= CRYPT_RegFlushStore(store
,
478 dwFlags
& CERT_STORE_CTRL_COMMIT_FORCE_FLAG
);
480 case CERT_STORE_CTRL_AUTO_RESYNC
:
481 FIXME("CERT_STORE_CTRL_AUTO_RESYNC: stub\n");
485 FIXME("%d: stub\n", dwCtrlType
);
491 static void *regProvFuncs
[] = {
493 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
496 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
497 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
500 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
501 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
504 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
508 WINECRYPT_CERTSTORE
*CRYPT_RegOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
511 WINECRYPT_CERTSTORE
*store
= NULL
;
513 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
515 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
517 DWORD rc
= RegDeleteTreeW((HKEY
)pvPara
, CertsW
);
519 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
520 rc
= RegDeleteTreeW((HKEY
)pvPara
, CRLsW
);
521 if (rc
== ERROR_SUCCESS
|| rc
== ERROR_NO_MORE_ITEMS
)
522 rc
= RegDeleteTreeW((HKEY
)pvPara
, CTLsW
);
523 if (rc
== ERROR_NO_MORE_ITEMS
)
531 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
532 GetCurrentProcess(), (LPHANDLE
)&key
,
533 dwFlags
& CERT_STORE_READONLY_FLAG
? KEY_READ
: KEY_ALL_ACCESS
,
536 WINECRYPT_CERTSTORE
*memStore
;
538 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, hCryptProv
,
539 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
542 WINE_REGSTOREINFO
*regInfo
= CryptMemAlloc(
543 sizeof(WINE_REGSTOREINFO
));
547 CERT_STORE_PROV_INFO provInfo
= { 0 };
549 regInfo
->dwOpenFlags
= dwFlags
;
550 regInfo
->memStore
= memStore
;
552 InitializeCriticalSection(®Info
->cs
);
553 regInfo
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": PWINE_REGSTOREINFO->cs");
554 list_init(®Info
->certsToDelete
);
555 list_init(®Info
->crlsToDelete
);
556 list_init(®Info
->ctlsToDelete
);
557 CRYPT_RegReadFromReg(regInfo
->key
, regInfo
->memStore
);
558 regInfo
->dirty
= FALSE
;
559 provInfo
.cbSize
= sizeof(provInfo
);
560 provInfo
.cStoreProvFunc
= sizeof(regProvFuncs
) /
561 sizeof(regProvFuncs
[0]);
562 provInfo
.rgpvStoreProvFunc
= regProvFuncs
;
563 provInfo
.hStoreProv
= regInfo
;
564 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
565 /* Reg store doesn't need crypto provider, so close it */
567 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
568 CryptReleaseContext(hCryptProv
, 0);
573 TRACE("returning %p\n", store
);