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
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25 #include "crypt32_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
29 typedef struct _WINE_FILESTOREINFO
36 } WINE_FILESTOREINFO
, *PWINE_FILESTOREINFO
;
38 static void WINAPI
CRYPT_FileCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
40 PWINE_FILESTOREINFO store
= hCertStore
;
42 TRACE("(%p, %08x)\n", store
, dwFlags
);
44 CertSaveStore(store
->memStore
, X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
45 store
->type
, CERT_STORE_SAVE_TO_FILE
, store
->file
, 0);
46 CertCloseStore(store
->memStore
, dwFlags
);
47 CloseHandle(store
->file
);
51 static BOOL WINAPI
CRYPT_FileWriteCert(HCERTSTORE hCertStore
,
52 PCCERT_CONTEXT cert
, DWORD dwFlags
)
54 PWINE_FILESTOREINFO store
= hCertStore
;
56 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
61 static BOOL WINAPI
CRYPT_FileDeleteCert(HCERTSTORE hCertStore
,
62 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
64 PWINE_FILESTOREINFO store
= hCertStore
;
66 TRACE("(%p, %p, %08x)\n", hCertStore
, pCertContext
, dwFlags
);
71 static BOOL WINAPI
CRYPT_FileWriteCRL(HCERTSTORE hCertStore
,
72 PCCRL_CONTEXT crl
, DWORD dwFlags
)
74 PWINE_FILESTOREINFO store
= hCertStore
;
76 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
81 static BOOL WINAPI
CRYPT_FileDeleteCRL(HCERTSTORE hCertStore
,
82 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
84 PWINE_FILESTOREINFO store
= hCertStore
;
86 TRACE("(%p, %p, %08x)\n", hCertStore
, pCrlContext
, dwFlags
);
91 static BOOL WINAPI
CRYPT_FileWriteCTL(HCERTSTORE hCertStore
,
92 PCCTL_CONTEXT ctl
, DWORD dwFlags
)
94 PWINE_FILESTOREINFO store
= hCertStore
;
96 TRACE("(%p, %p, %d)\n", hCertStore
, ctl
, dwFlags
);
101 static BOOL WINAPI
CRYPT_FileDeleteCTL(HCERTSTORE hCertStore
,
102 PCCTL_CONTEXT pCtlContext
, DWORD dwFlags
)
104 PWINE_FILESTOREINFO store
= hCertStore
;
106 TRACE("(%p, %p, %08x)\n", hCertStore
, pCtlContext
, dwFlags
);
111 static BOOL
CRYPT_ReadBlobFromFile(HANDLE file
, PCERT_BLOB blob
)
115 blob
->cbData
= GetFileSize(file
, NULL
);
118 blob
->pbData
= CryptMemAlloc(blob
->cbData
);
123 ret
= ReadFile(file
, blob
->pbData
, blob
->cbData
, &read
, NULL
);
129 static BOOL WINAPI
CRYPT_FileControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
130 DWORD dwCtrlType
, void const *pvCtrlPara
)
132 PWINE_FILESTOREINFO store
= hCertStore
;
135 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
140 case CERT_STORE_CTRL_RESYNC
:
141 store
->dirty
= FALSE
;
142 if (store
->type
== CERT_STORE_SAVE_AS_STORE
)
144 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
145 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
147 /* FIXME: if I could translate a handle to a path, I could use
148 * CryptQueryObject instead, but there's no API to do so yet.
150 ret
= CRYPT_ReadSerializedStoreFromFile(store
->file
, memStore
);
152 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
153 CertCloseStore(memStore
, 0);
155 else if (store
->type
== CERT_STORE_SAVE_AS_PKCS7
)
157 CERT_BLOB blob
= { 0, NULL
};
159 ret
= CRYPT_ReadBlobFromFile(store
->file
, &blob
);
162 HCERTSTORE messageStore
;
164 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
165 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
166 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
,
167 &messageStore
, NULL
, NULL
);
168 I_CertUpdateStore(store
->memStore
, messageStore
, 0, 0);
169 CertCloseStore(messageStore
, 0);
170 CryptMemFree(blob
.pbData
);
175 WARN("unknown type %d\n", store
->type
);
179 case CERT_STORE_CTRL_COMMIT
:
180 if (!(store
->dwOpenFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
182 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
185 else if (store
->dirty
)
186 ret
= CertSaveStore(store
->memStore
,
187 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
188 store
->type
, CERT_STORE_SAVE_TO_FILE
, store
->file
, 0);
193 FIXME("%d: stub\n", dwCtrlType
);
199 static void *fileProvFuncs
[] = {
200 CRYPT_FileCloseStore
,
201 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
203 CRYPT_FileDeleteCert
,
204 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
205 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
208 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
209 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
212 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
216 static PWINECRYPT_CERTSTORE
CRYPT_CreateFileStore(DWORD dwFlags
,
217 HCERTSTORE memStore
, HANDLE file
, DWORD type
)
219 PWINECRYPT_CERTSTORE store
= NULL
;
220 PWINE_FILESTOREINFO info
= CryptMemAlloc(sizeof(WINE_FILESTOREINFO
));
224 CERT_STORE_PROV_INFO provInfo
= { 0 };
226 info
->dwOpenFlags
= dwFlags
;
227 info
->memStore
= memStore
;
231 provInfo
.cbSize
= sizeof(provInfo
);
232 provInfo
.cStoreProvFunc
= sizeof(fileProvFuncs
) /
233 sizeof(fileProvFuncs
[0]);
234 provInfo
.rgpvStoreProvFunc
= fileProvFuncs
;
235 provInfo
.hStoreProv
= info
;
236 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
241 PWINECRYPT_CERTSTORE
CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
244 PWINECRYPT_CERTSTORE store
= NULL
;
245 HANDLE file
= (HANDLE
)pvPara
;
247 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
251 SetLastError(ERROR_INVALID_HANDLE
);
254 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
256 SetLastError(E_INVALIDARG
);
259 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
260 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
262 SetLastError(E_INVALIDARG
);
266 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
267 GetCurrentProcess(), &file
, dwFlags
& CERT_STORE_READONLY_FLAG
?
268 GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
, TRUE
, 0))
272 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
273 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
276 if (CRYPT_ReadSerializedStoreFromFile(file
, memStore
))
278 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
,
279 CERT_STORE_SAVE_AS_STORE
);
280 /* File store doesn't need crypto provider, so close it */
282 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
283 CryptReleaseContext(hCryptProv
, 0);
287 TRACE("returning %p\n", store
);
291 PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
292 DWORD dwFlags
, const void *pvPara
)
294 HCERTSTORE store
= 0;
295 LPCWSTR fileName
= pvPara
;
296 DWORD access
, create
;
299 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
, debugstr_w(fileName
));
303 SetLastError(ERROR_PATH_NOT_FOUND
);
306 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
307 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
309 SetLastError(E_INVALIDARG
);
313 access
= GENERIC_READ
;
314 if (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
)
315 access
|= GENERIC_WRITE
;
316 if (dwFlags
& CERT_STORE_CREATE_NEW_FLAG
)
318 else if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
319 create
= OPEN_EXISTING
;
321 create
= OPEN_ALWAYS
;
322 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
, create
,
323 FILE_ATTRIBUTE_NORMAL
, NULL
);
324 if (file
!= INVALID_HANDLE_VALUE
)
326 HCERTSTORE memStore
= NULL
;
327 DWORD size
= GetFileSize(file
, NULL
), type
= 0;
329 /* If the file isn't empty, try to get the type from the file itself */
335 /* Close the file so CryptQueryObject can succeed.. */
337 ret
= CryptQueryObject(CERT_QUERY_OBJECT_FILE
, fileName
,
338 CERT_QUERY_CONTENT_FLAG_CERT
|
339 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
|
340 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
341 CERT_QUERY_FORMAT_FLAG_ALL
, 0, NULL
, &contentType
, NULL
,
342 &memStore
, NULL
, NULL
);
345 if (contentType
== CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
)
346 type
= CERT_STORE_SAVE_AS_PKCS7
;
348 type
= CERT_STORE_SAVE_AS_STORE
;
349 /* and reopen the file. */
350 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
,
351 create
, FILE_ATTRIBUTE_NORMAL
, NULL
);
356 static const WCHAR spc
[] = { 's','p','c',0 };
357 static const WCHAR p7c
[] = { 'p','7','c',0 };
358 LPCWSTR ext
= strrchrW(fileName
, '.');
363 if (!lstrcmpiW(ext
, spc
) || !lstrcmpiW(ext
, p7c
))
364 type
= CERT_STORE_SAVE_AS_PKCS7
;
367 type
= CERT_STORE_SAVE_AS_STORE
;
368 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
369 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
373 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
, type
);
374 /* File store doesn't need crypto provider, so close it */
375 if (hCryptProv
&& !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
376 CryptReleaseContext(hCryptProv
, 0);
382 PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
383 DWORD dwFlags
, const void *pvPara
)
386 PWINECRYPT_CERTSTORE ret
= NULL
;
388 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
393 SetLastError(ERROR_FILE_NOT_FOUND
);
396 len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
399 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
403 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, storeName
, len
);
404 ret
= CRYPT_FileNameOpenStoreW(hCryptProv
, dwFlags
, storeName
);
405 CryptMemFree(storeName
);