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
38 static void WINAPI
CRYPT_FileCloseStore(HCERTSTORE hCertStore
, DWORD dwFlags
)
40 WINE_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 CloseHandle(store
->file
);
50 static BOOL WINAPI
CRYPT_FileWriteCert(HCERTSTORE hCertStore
,
51 PCCERT_CONTEXT cert
, DWORD dwFlags
)
53 WINE_FILESTOREINFO
*store
= hCertStore
;
55 TRACE("(%p, %p, %d)\n", hCertStore
, cert
, dwFlags
);
60 static BOOL WINAPI
CRYPT_FileDeleteCert(HCERTSTORE hCertStore
,
61 PCCERT_CONTEXT pCertContext
, DWORD dwFlags
)
63 WINE_FILESTOREINFO
*store
= hCertStore
;
65 TRACE("(%p, %p, %08x)\n", hCertStore
, pCertContext
, dwFlags
);
70 static BOOL WINAPI
CRYPT_FileWriteCRL(HCERTSTORE hCertStore
,
71 PCCRL_CONTEXT crl
, DWORD dwFlags
)
73 WINE_FILESTOREINFO
*store
= hCertStore
;
75 TRACE("(%p, %p, %d)\n", hCertStore
, crl
, dwFlags
);
80 static BOOL WINAPI
CRYPT_FileDeleteCRL(HCERTSTORE hCertStore
,
81 PCCRL_CONTEXT pCrlContext
, DWORD dwFlags
)
83 WINE_FILESTOREINFO
*store
= hCertStore
;
85 TRACE("(%p, %p, %08x)\n", hCertStore
, pCrlContext
, dwFlags
);
90 static BOOL WINAPI
CRYPT_FileWriteCTL(HCERTSTORE hCertStore
,
91 PCCTL_CONTEXT ctl
, DWORD dwFlags
)
93 WINE_FILESTOREINFO
*store
= hCertStore
;
95 TRACE("(%p, %p, %d)\n", hCertStore
, ctl
, dwFlags
);
100 static BOOL WINAPI
CRYPT_FileDeleteCTL(HCERTSTORE hCertStore
,
101 PCCTL_CONTEXT pCtlContext
, DWORD dwFlags
)
103 WINE_FILESTOREINFO
*store
= hCertStore
;
105 TRACE("(%p, %p, %08x)\n", hCertStore
, pCtlContext
, dwFlags
);
110 static BOOL
CRYPT_ReadBlobFromFile(HANDLE file
, PCERT_BLOB blob
)
114 blob
->cbData
= GetFileSize(file
, NULL
);
117 blob
->pbData
= CryptMemAlloc(blob
->cbData
);
122 ret
= ReadFile(file
, blob
->pbData
, blob
->cbData
, &read
, NULL
) && read
== blob
->cbData
;
123 if (!ret
) CryptMemFree(blob
->pbData
);
131 static BOOL WINAPI
CRYPT_FileControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
132 DWORD dwCtrlType
, void const *pvCtrlPara
)
134 WINE_FILESTOREINFO
*store
= hCertStore
;
137 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
142 case CERT_STORE_CTRL_RESYNC
:
143 store
->dirty
= FALSE
;
144 if (store
->type
== CERT_STORE_SAVE_AS_STORE
)
146 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
147 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
149 /* FIXME: if I could translate a handle to a path, I could use
150 * CryptQueryObject instead, but there's no API to do so yet.
152 ret
= CRYPT_ReadSerializedStoreFromFile(store
->file
, memStore
);
154 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
155 CertCloseStore(memStore
, 0);
157 else if (store
->type
== CERT_STORE_SAVE_AS_PKCS7
)
159 CERT_BLOB blob
= { 0, NULL
};
161 ret
= CRYPT_ReadBlobFromFile(store
->file
, &blob
);
164 HCERTSTORE messageStore
;
166 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
167 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
168 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
,
169 &messageStore
, NULL
, NULL
);
170 I_CertUpdateStore(store
->memStore
, messageStore
, 0, 0);
171 CertCloseStore(messageStore
, 0);
172 CryptMemFree(blob
.pbData
);
177 WARN("unknown type %d\n", store
->type
);
181 case CERT_STORE_CTRL_COMMIT
:
182 if (!(store
->dwOpenFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
184 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
187 else if (store
->dirty
)
188 ret
= CertSaveStore(store
->memStore
,
189 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
190 store
->type
, CERT_STORE_SAVE_TO_FILE
, store
->file
, 0);
195 FIXME("%d: stub\n", dwCtrlType
);
201 static void *fileProvFuncs
[] = {
202 CRYPT_FileCloseStore
,
203 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
205 CRYPT_FileDeleteCert
,
206 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
207 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
210 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
211 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
214 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
218 static WINECRYPT_CERTSTORE
*CRYPT_CreateFileStore(DWORD dwFlags
,
219 HCERTSTORE memStore
, HANDLE file
, DWORD type
)
221 WINECRYPT_CERTSTORE
*store
= NULL
;
222 WINE_FILESTOREINFO
*info
= CryptMemAlloc(sizeof(WINE_FILESTOREINFO
));
226 CERT_STORE_PROV_INFO provInfo
= { 0 };
228 info
->dwOpenFlags
= dwFlags
;
229 info
->memStore
= memStore
;
233 provInfo
.cbSize
= sizeof(provInfo
);
234 provInfo
.cStoreProvFunc
= ARRAY_SIZE(fileProvFuncs
);
235 provInfo
.rgpvStoreProvFunc
= fileProvFuncs
;
236 provInfo
.hStoreProv
= info
;
237 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
242 WINECRYPT_CERTSTORE
*CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
245 WINECRYPT_CERTSTORE
*store
= NULL
;
246 HANDLE file
= (HANDLE
)pvPara
;
248 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
252 SetLastError(ERROR_INVALID_HANDLE
);
255 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
257 SetLastError(E_INVALIDARG
);
260 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
261 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
263 SetLastError(E_INVALIDARG
);
267 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
268 GetCurrentProcess(), &file
, dwFlags
& CERT_STORE_READONLY_FLAG
?
269 GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
, TRUE
, 0))
273 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
274 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
277 if (CRYPT_ReadSerializedStoreFromFile(file
, memStore
))
279 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
,
280 CERT_STORE_SAVE_AS_STORE
);
281 /* File store doesn't need crypto provider, so close it */
283 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
284 CryptReleaseContext(hCryptProv
, 0);
288 TRACE("returning %p\n", store
);
292 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
293 DWORD dwFlags
, const void *pvPara
)
295 HCERTSTORE store
= 0;
296 LPCWSTR fileName
= pvPara
;
297 DWORD access
, create
;
300 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
, debugstr_w(fileName
));
304 SetLastError(ERROR_PATH_NOT_FOUND
);
307 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
308 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
310 SetLastError(E_INVALIDARG
);
314 access
= GENERIC_READ
;
315 if (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
)
316 access
|= GENERIC_WRITE
;
317 if (dwFlags
& CERT_STORE_CREATE_NEW_FLAG
)
319 else if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
320 create
= OPEN_EXISTING
;
322 create
= OPEN_ALWAYS
;
323 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
, create
,
324 FILE_ATTRIBUTE_NORMAL
, NULL
);
325 if (file
!= INVALID_HANDLE_VALUE
)
327 HCERTSTORE memStore
= NULL
;
328 DWORD size
= GetFileSize(file
, NULL
), type
= 0;
330 /* If the file isn't empty, try to get the type from the file itself */
336 /* Close the file so CryptQueryObject can succeed.. */
338 ret
= CryptQueryObject(CERT_QUERY_OBJECT_FILE
, fileName
,
339 CERT_QUERY_CONTENT_FLAG_CERT
|
340 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
|
341 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
342 CERT_QUERY_FORMAT_FLAG_ALL
, 0, NULL
, &contentType
, NULL
,
343 &memStore
, NULL
, NULL
);
346 if (contentType
== CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
)
347 type
= CERT_STORE_SAVE_AS_PKCS7
;
349 type
= CERT_STORE_SAVE_AS_STORE
;
350 /* and reopen the file. */
351 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
,
352 create
, FILE_ATTRIBUTE_NORMAL
, NULL
);
357 static const WCHAR spc
[] = { 's','p','c',0 };
358 static const WCHAR p7c
[] = { 'p','7','c',0 };
359 LPCWSTR ext
= strrchrW(fileName
, '.');
364 if (!lstrcmpiW(ext
, spc
) || !lstrcmpiW(ext
, p7c
))
365 type
= CERT_STORE_SAVE_AS_PKCS7
;
368 type
= CERT_STORE_SAVE_AS_STORE
;
369 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
370 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
374 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
, type
);
375 /* File store doesn't need crypto provider, so close it */
376 if (hCryptProv
&& !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
377 CryptReleaseContext(hCryptProv
, 0);
383 WINECRYPT_CERTSTORE
*CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
384 DWORD dwFlags
, const void *pvPara
)
387 WINECRYPT_CERTSTORE
*ret
= NULL
;
389 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
394 SetLastError(ERROR_FILE_NOT_FOUND
);
397 len
= MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, NULL
, 0);
400 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
404 MultiByteToWideChar(CP_ACP
, 0, pvPara
, -1, storeName
, len
);
405 ret
= CRYPT_FileNameOpenStoreW(hCryptProv
, dwFlags
, storeName
);
406 CryptMemFree(storeName
);