ReactOS 0.4.17-dev-470-gf9e3448
filestore.c
Go to the documentation of this file.
1/*
2 * Copyright 2004-2007 Juan Lang
3 *
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.
8 *
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.
13 *
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
17 */
18#include <stdarg.h>
19#include "windef.h"
20#include "winbase.h"
21#include "wincrypt.h"
22#include "winnls.h"
23#include "wine/debug.h"
24#include "crypt32_private.h"
25
27
28typedef struct _WINE_FILESTOREINFO
29{
36
38{
39 WINE_FILESTOREINFO *store = hCertStore;
40
41 TRACE("(%p, %08lx)\n", store, dwFlags);
42 if (store->dirty)
44 store->type, CERT_STORE_SAVE_TO_FILE, store->file, 0);
45 CloseHandle(store->file);
46 CryptMemFree(store);
47}
48
51{
52 WINE_FILESTOREINFO *store = hCertStore;
53
54 TRACE("(%p, %p, %ld)\n", hCertStore, cert, dwFlags);
55 store->dirty = TRUE;
56 return TRUE;
57}
58
60 PCCERT_CONTEXT pCertContext, DWORD dwFlags)
61{
62 WINE_FILESTOREINFO *store = hCertStore;
63
64 TRACE("(%p, %p, %08lx)\n", hCertStore, pCertContext, dwFlags);
65 store->dirty = TRUE;
66 return TRUE;
67}
68
71{
72 WINE_FILESTOREINFO *store = hCertStore;
73
74 TRACE("(%p, %p, %ld)\n", hCertStore, crl, dwFlags);
75 store->dirty = TRUE;
76 return TRUE;
77}
78
80 PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
81{
82 WINE_FILESTOREINFO *store = hCertStore;
83
84 TRACE("(%p, %p, %08lx)\n", hCertStore, pCrlContext, dwFlags);
85 store->dirty = TRUE;
86 return TRUE;
87}
88
91{
92 WINE_FILESTOREINFO *store = hCertStore;
93
94 TRACE("(%p, %p, %ld)\n", hCertStore, ctl, dwFlags);
95 store->dirty = TRUE;
96 return TRUE;
97}
98
100 PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
101{
102 WINE_FILESTOREINFO *store = hCertStore;
103
104 TRACE("(%p, %p, %08lx)\n", hCertStore, pCtlContext, dwFlags);
105 store->dirty = TRUE;
106 return TRUE;
107}
108
110{
111 BOOL ret = TRUE;
112
113 blob->cbData = GetFileSize(file, NULL);
114 if (blob->cbData)
115 {
116 blob->pbData = CryptMemAlloc(blob->cbData);
117 if (blob->pbData)
118 {
119 DWORD read;
120
121 ret = ReadFile(file, blob->pbData, blob->cbData, &read, NULL) && read == blob->cbData;
122 if (!ret) CryptMemFree(blob->pbData);
123 }
124 else
125 ret = FALSE;
126 }
127 return ret;
128}
129
131 DWORD dwCtrlType, void const *pvCtrlPara)
132{
133 WINE_FILESTOREINFO *store = hCertStore;
134 BOOL ret;
135
136 TRACE("(%p, %08lx, %ld, %p)\n", hCertStore, dwFlags, dwCtrlType,
137 pvCtrlPara);
138
139 switch (dwCtrlType)
140 {
142 store->dirty = FALSE;
143 if (store->type == CERT_STORE_SAVE_AS_STORE)
144 {
147
148 /* FIXME: if I could translate a handle to a path, I could use
149 * CryptQueryObject instead, but there's no API to do so yet.
150 */
151 ret = CRYPT_ReadSerializedStoreFromFile(store->file, memStore);
152 if (ret)
153 I_CertUpdateStore(store->memStore, memStore, 0, 0);
154 CertCloseStore(memStore, 0);
155 }
156 else if (store->type == CERT_STORE_SAVE_AS_PKCS7)
157 {
158 CERT_BLOB blob = { 0, NULL };
159
161 if (ret)
162 {
163 HCERTSTORE messageStore;
164
168 &messageStore, NULL, NULL);
169 I_CertUpdateStore(store->memStore, messageStore, 0, 0);
170 CertCloseStore(messageStore, 0);
171 CryptMemFree(blob.pbData);
172 }
173 }
174 else
175 {
176 WARN("unknown type %ld\n", store->type);
177 ret = FALSE;
178 }
179 break;
182 {
184 ret = FALSE;
185 }
186 else if (store->dirty)
187 ret = CertSaveStore(store->memStore,
189 store->type, CERT_STORE_SAVE_TO_FILE, store->file, 0);
190 else
191 ret = TRUE;
192 break;
193 default:
194 FIXME("%ld: stub\n", dwCtrlType);
195 ret = FALSE;
196 }
197 return ret;
198}
199
200static void *fileProvFuncs[] = {
202 NULL, /* CERT_STORE_PROV_READ_CERT_FUNC */
205 NULL, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
206 NULL, /* CERT_STORE_PROV_READ_CRL_FUNC */
209 NULL, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
210 NULL, /* CERT_STORE_PROV_READ_CTL_FUNC */
213 NULL, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
215};
216
218 HCERTSTORE memStore, HANDLE file, DWORD type)
219{
220 WINECRYPT_CERTSTORE *store = NULL;
222
223 if (info)
224 {
225 CERT_STORE_PROV_INFO provInfo = { 0 };
226
227 info->dwOpenFlags = dwFlags;
228 info->memStore = memStore;
229 info->file = file;
230 info->type = type;
231 info->dirty = FALSE;
232 provInfo.cbSize = sizeof(provInfo);
235 provInfo.hStoreProv = info;
236 store = CRYPT_ProvCreateStore(dwFlags, memStore, &provInfo);
237 }
238 return store;
239}
240
242 const void *pvPara)
243{
244 WINECRYPT_CERTSTORE *store = NULL;
245 HANDLE file = (HANDLE)pvPara;
246
247 TRACE("(%Id, %08lx, %p)\n", hCryptProv, dwFlags, pvPara);
248
249 if (!pvPara)
250 {
252 return NULL;
253 }
255 {
257 return NULL;
258 }
261 {
263 return NULL;
264 }
265
269 {
270 HCERTSTORE memStore;
271
272 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
274 if (memStore)
275 {
277 {
278 store = CRYPT_CreateFileStore(dwFlags, memStore, file,
280 /* File store doesn't need crypto provider, so close it */
281 if (hCryptProv &&
283 CryptReleaseContext(hCryptProv, 0);
284 }
285 }
286 }
287 TRACE("returning %p\n", store);
288 return store;
289}
290
292 DWORD dwFlags, const void *pvPara)
293{
294 HCERTSTORE store = 0;
295 LPCWSTR fileName = pvPara;
297 HANDLE file;
298
299 TRACE("(%Id, %08lx, %s)\n", hCryptProv, dwFlags, debugstr_w(fileName));
300
301 if (!fileName)
302 {
304 return NULL;
305 }
308 {
310 return NULL;
311 }
312
320 else
325 {
326 HCERTSTORE memStore = NULL;
328
329 /* If the file isn't empty, try to get the type from the file itself */
330 if (size)
331 {
332 DWORD contentType;
333 BOOL ret;
334
335 /* Close the file so CryptQueryObject can succeed.. */
341 CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, &contentType, NULL,
342 &memStore, NULL, NULL);
343 if (ret)
344 {
345 if (contentType == CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED)
347 else
349 /* and reopen the file. */
352 }
353 }
354 else
355 {
356 LPCWSTR ext = wcsrchr(fileName, '.');
357
358 if (ext)
359 {
360 ext++;
361 if (!lstrcmpiW(ext, L"spc") || !lstrcmpiW(ext, L"p7c"))
363 }
364 if (!type)
366 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
368 }
369 if (memStore)
370 {
371 store = CRYPT_CreateFileStore(dwFlags, memStore, file, type);
372 /* File store doesn't need crypto provider, so close it */
373 if (hCryptProv && !(dwFlags & CERT_STORE_NO_CRYPT_RELEASE_FLAG))
374 CryptReleaseContext(hCryptProv, 0);
375 }
376 }
377 return store;
378}
379
381 DWORD dwFlags, const void *pvPara)
382{
383 int len;
385
386 TRACE("(%Id, %08lx, %s)\n", hCryptProv, dwFlags,
387 debugstr_a(pvPara));
388
389 if (!pvPara)
390 {
392 return NULL;
393 }
394 len = MultiByteToWideChar(CP_ACP, 0, pvPara, -1, NULL, 0);
395 if (len)
396 {
397 LPWSTR storeName = CryptMemAlloc(len * sizeof(WCHAR));
398
399 if (storeName)
400 {
401 MultiByteToWideChar(CP_ACP, 0, pvPara, -1, storeName, len);
402 ret = CRYPT_FileNameOpenStoreW(hCryptProv, dwFlags, storeName);
403 CryptMemFree(storeName);
404 }
405 }
406 return ret;
407}
#define read
Definition: acwin.h:97
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ARRAY_SIZE(A)
Definition: main.h:20
#define FIXME(fmt,...)
Definition: precomp.h:53
#define WARN(fmt,...)
Definition: precomp.h:61
BOOL CRYPT_ReadSerializedStoreFromFile(HANDLE file, HCERTSTORE store)
Definition: serialize.c:731
WINECRYPT_CERTSTORE * CRYPT_ProvCreateStore(DWORD dwFlags, WINECRYPT_CERTSTORE *memStore, const CERT_STORE_PROV_INFO *pProvInfo)
Definition: provstore.c:307
BOOL WINAPI I_CertUpdateStore(HCERTSTORE store1, HCERTSTORE store2, DWORD unk0, DWORD unk1)
Definition: store.c:106
#define E_INVALIDARG
Definition: ddrawi.h:101
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
Definition: crypt.c:641
LPVOID WINAPI CryptMemAlloc(ULONG cbSize)
Definition: main.c:136
VOID WINAPI CryptMemFree(LPVOID pv)
Definition: main.c:146
BOOL WINAPI CryptQueryObject(DWORD dwObjectType, const void *pvObject, DWORD dwExpectedContentTypeFlags, DWORD dwExpectedFormatTypeFlags, DWORD dwFlags, DWORD *pdwMsgAndCertEncodingType, DWORD *pdwContentType, DWORD *pdwFormatType, HCERTSTORE *phCertStore, HCRYPTMSG *phMsg, const void **ppvContext)
Definition: object.c:770
HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider, DWORD dwMsgAndCertEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags, const void *pvPara)
Definition: store.c:809
BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
Definition: store.c:1121
#define CloseHandle
Definition: compat.h:739
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define wcsrchr
Definition: compat.h:16
#define CP_ACP
Definition: compat.h:109
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetLastError(x)
Definition: compat.h:752
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define GENERIC_READ
Definition: compat.h:135
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define MultiByteToWideChar
Definition: compat.h:110
#define FILE_SHARE_READ
Definition: compat.h:136
static const WCHAR *const ext[]
Definition: module.c:53
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4171
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
static BOOL WINAPI CRYPT_FileControl(HCERTSTORE hCertStore, DWORD dwFlags, DWORD dwCtrlType, void const *pvCtrlPara)
Definition: filestore.c:130
WINECRYPT_CERTSTORE * CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
Definition: filestore.c:380
static void * fileProvFuncs[]
Definition: filestore.c:200
static BOOL WINAPI CRYPT_FileDeleteCert(HCERTSTORE hCertStore, PCCERT_CONTEXT pCertContext, DWORD dwFlags)
Definition: filestore.c:59
static BOOL WINAPI CRYPT_FileWriteCTL(HCERTSTORE hCertStore, PCCTL_CONTEXT ctl, DWORD dwFlags)
Definition: filestore.c:89
static BOOL WINAPI CRYPT_FileWriteCert(HCERTSTORE hCertStore, PCCERT_CONTEXT cert, DWORD dwFlags)
Definition: filestore.c:49
static BOOL WINAPI CRYPT_FileDeleteCTL(HCERTSTORE hCertStore, PCCTL_CONTEXT pCtlContext, DWORD dwFlags)
Definition: filestore.c:99
static BOOL WINAPI CRYPT_FileDeleteCRL(HCERTSTORE hCertStore, PCCRL_CONTEXT pCrlContext, DWORD dwFlags)
Definition: filestore.c:79
WINECRYPT_CERTSTORE * CRYPT_FileOpenStore(HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
Definition: filestore.c:241
struct _WINE_FILESTOREINFO WINE_FILESTOREINFO
static WINECRYPT_CERTSTORE * CRYPT_CreateFileStore(DWORD dwFlags, HCERTSTORE memStore, HANDLE file, DWORD type)
Definition: filestore.c:217
WINECRYPT_CERTSTORE * CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv, DWORD dwFlags, const void *pvPara)
Definition: filestore.c:291
static BOOL CRYPT_ReadBlobFromFile(HANDLE file, PCERT_BLOB blob)
Definition: filestore.c:109
static BOOL WINAPI CRYPT_FileWriteCRL(HCERTSTORE hCertStore, PCCRL_CONTEXT crl, DWORD dwFlags)
Definition: filestore.c:69
static void WINAPI CRYPT_FileCloseStore(HCERTSTORE hCertStore, DWORD dwFlags)
Definition: filestore.c:37
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
GLuint GLint GLboolean GLint GLenum access
Definition: glext.h:7866
GLenum GLsizei len
Definition: glext.h:6722
#define debugstr_a
Definition: kernel32.h:31
#define debugstr_w
Definition: kernel32.h:32
#define CREATE_NEW
Definition: disk.h:69
#define OPEN_ALWAYS
Definition: disk.h:70
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
static const BYTE crl[]
Definition: message.c:817
static BYTE cert[]
Definition: msg.c:1374
static const struct access_res create[16]
Definition: package.c:7505
_In_ LPWSTR _In_ DWORD _In_ DWORD _In_ DWORD dwFlags
Definition: netsh.h:141
#define GENERIC_WRITE
Definition: nt_native.h:90
short WCHAR
Definition: pedump.c:58
BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType, DWORD dwSaveAs, DWORD dwSaveTo, void *pvSaveToPara, DWORD dwFlags)
Definition: serialize.c:1082
#define TRACE(s)
Definition: solgame.cpp:4
void ** rgpvStoreProvFunc
Definition: wincrypt.h:1275
HCERTSTOREPROV hStoreProv
Definition: wincrypt.h:1276
HCERTSTORE memStore
Definition: filestore.c:31
Definition: image.c:134
Definition: fci.c:127
const uint16_t * LPCWSTR
Definition: typedefs.h:57
uint16_t * LPWSTR
Definition: typedefs.h:56
PVOID HANDLE
Definition: typedefs.h:73
#define CERT_STORE_SAVE_AS_STORE
Definition: wincrypt.h:2793
#define CERT_QUERY_OBJECT_FILE
Definition: wincrypt.h:3686
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:55
#define CERT_STORE_SAVE_AS_PKCS7
Definition: wincrypt.h:2794
#define CERT_QUERY_CONTENT_FLAG_CERT
Definition: wincrypt.h:3703
#define CERT_STORE_OPEN_EXISTING_FLAG
Definition: wincrypt.h:2634
#define CERT_STORE_CREATE_NEW_FLAG
Definition: wincrypt.h:2633
#define CERT_QUERY_OBJECT_BLOB
Definition: wincrypt.h:3687
#define CERT_QUERY_FORMAT_FLAG_BINARY
Definition: wincrypt.h:3743
#define CERT_STORE_CTRL_RESYNC
Definition: wincrypt.h:2967
#define X509_ASN_ENCODING
Definition: wincrypt.h:2501
#define CERT_FILE_STORE_COMMIT_ENABLE_FLAG
Definition: wincrypt.h:2644
#define CERT_STORE_PROV_MEMORY
Definition: wincrypt.h:2455
#define CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
Definition: wincrypt.h:3714
#define CERT_QUERY_FORMAT_FLAG_ALL
Definition: wincrypt.h:3749
#define CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
Definition: wincrypt.h:3706
#define CERT_STORE_CTRL_COMMIT
Definition: wincrypt.h:2969
#define CERT_STORE_NO_CRYPT_RELEASE_FLAG
Definition: wincrypt.h:2621
#define PKCS_7_ASN_ENCODING
Definition: wincrypt.h:2503
#define CERT_STORE_SAVE_TO_FILE
Definition: wincrypt.h:2796
#define CERT_STORE_DELETE_FLAG
Definition: wincrypt.h:2624
#define CERT_STORE_READONLY_FLAG
Definition: wincrypt.h:2635
#define WINAPI
Definition: msvc.h:6
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:228