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
= (PWINE_FILESTOREINFO
)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
= (PWINE_FILESTOREINFO
)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
= (PWINE_FILESTOREINFO
)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
= (PWINE_FILESTOREINFO
)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
= (PWINE_FILESTOREINFO
)hCertStore
;
86 TRACE("(%p, %p, %08x)\n", hCertStore
, pCrlContext
, dwFlags
);
91 static BOOL
CRYPT_ReadBlobFromFile(HANDLE file
, PCERT_BLOB blob
)
95 blob
->cbData
= GetFileSize(file
, NULL
);
98 blob
->pbData
= CryptMemAlloc(blob
->cbData
);
103 ret
= ReadFile(file
, blob
->pbData
, blob
->cbData
, &read
, NULL
);
109 static BOOL WINAPI
CRYPT_FileControl(HCERTSTORE hCertStore
, DWORD dwFlags
,
110 DWORD dwCtrlType
, void const *pvCtrlPara
)
112 PWINE_FILESTOREINFO store
= (PWINE_FILESTOREINFO
)hCertStore
;
115 TRACE("(%p, %08x, %d, %p)\n", hCertStore
, dwFlags
, dwCtrlType
,
120 case CERT_STORE_CTRL_RESYNC
:
121 store
->dirty
= FALSE
;
122 if (store
->type
== CERT_STORE_SAVE_AS_STORE
)
124 HCERTSTORE memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
125 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
127 /* FIXME: if I could translate a handle to a path, I could use
128 * CryptQueryObject instead, but there's no API to do so yet.
130 ret
= CRYPT_ReadSerializedStoreFromFile(store
->file
, memStore
);
132 I_CertUpdateStore(store
->memStore
, memStore
, 0, 0);
133 CertCloseStore(memStore
, 0);
135 else if (store
->type
== CERT_STORE_SAVE_AS_PKCS7
)
137 CERT_BLOB blob
= { 0, NULL
};
139 ret
= CRYPT_ReadBlobFromFile(store
->file
, &blob
);
142 HCERTSTORE messageStore
;
144 ret
= CryptQueryObject(CERT_QUERY_OBJECT_BLOB
, &blob
,
145 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
146 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, NULL
, NULL
,
147 &messageStore
, NULL
, NULL
);
148 I_CertUpdateStore(store
->memStore
, messageStore
, 0, 0);
149 CertCloseStore(messageStore
, 0);
150 CryptMemFree(blob
.pbData
);
155 WARN("unknown type %d\n", store
->type
);
159 case CERT_STORE_CTRL_COMMIT
:
160 if (!(store
->dwOpenFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
162 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
165 else if (store
->dirty
)
166 ret
= CertSaveStore(store
->memStore
,
167 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
168 store
->type
, CERT_STORE_SAVE_TO_FILE
, store
->file
, 0);
173 FIXME("%d: stub\n", dwCtrlType
);
179 static void *fileProvFuncs
[] = {
180 CRYPT_FileCloseStore
,
181 NULL
, /* CERT_STORE_PROV_READ_CERT_FUNC */
183 CRYPT_FileDeleteCert
,
184 NULL
, /* CERT_STORE_PROV_SET_CERT_PROPERTY_FUNC */
185 NULL
, /* CERT_STORE_PROV_READ_CRL_FUNC */
188 NULL
, /* CERT_STORE_PROV_SET_CRL_PROPERTY_FUNC */
189 NULL
, /* CERT_STORE_PROV_READ_CTL_FUNC */
190 NULL
, /* CERT_STORE_PROV_WRITE_CTL_FUNC */
191 NULL
, /* CERT_STORE_PROV_DELETE_CTL_FUNC */
192 NULL
, /* CERT_STORE_PROV_SET_CTL_PROPERTY_FUNC */
196 static PWINECRYPT_CERTSTORE
CRYPT_CreateFileStore(DWORD dwFlags
,
197 HCERTSTORE memStore
, HANDLE file
, DWORD type
)
199 PWINECRYPT_CERTSTORE store
= NULL
;
200 PWINE_FILESTOREINFO info
= CryptMemAlloc(sizeof(WINE_FILESTOREINFO
));
204 CERT_STORE_PROV_INFO provInfo
= { 0 };
206 info
->dwOpenFlags
= dwFlags
;
207 info
->memStore
= memStore
;
211 provInfo
.cbSize
= sizeof(provInfo
);
212 provInfo
.cStoreProvFunc
= sizeof(fileProvFuncs
) /
213 sizeof(fileProvFuncs
[0]);
214 provInfo
.rgpvStoreProvFunc
= fileProvFuncs
;
215 provInfo
.hStoreProv
= info
;
216 store
= CRYPT_ProvCreateStore(dwFlags
, memStore
, &provInfo
);
221 PWINECRYPT_CERTSTORE
CRYPT_FileOpenStore(HCRYPTPROV hCryptProv
, DWORD dwFlags
,
224 PWINECRYPT_CERTSTORE store
= NULL
;
225 HANDLE file
= (HANDLE
)pvPara
;
227 TRACE("(%ld, %08x, %p)\n", hCryptProv
, dwFlags
, pvPara
);
231 SetLastError(ERROR_INVALID_HANDLE
);
234 if (dwFlags
& CERT_STORE_DELETE_FLAG
)
236 SetLastError(E_INVALIDARG
);
239 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
240 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
242 SetLastError(E_INVALIDARG
);
246 if (DuplicateHandle(GetCurrentProcess(), (HANDLE
)pvPara
,
247 GetCurrentProcess(), &file
, dwFlags
& CERT_STORE_READONLY_FLAG
?
248 GENERIC_READ
: GENERIC_READ
| GENERIC_WRITE
, TRUE
, 0))
252 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
253 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
256 if (CRYPT_ReadSerializedStoreFromFile(file
, memStore
))
258 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
,
259 CERT_STORE_SAVE_AS_STORE
);
260 /* File store doesn't need crypto provider, so close it */
262 !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
263 CryptReleaseContext(hCryptProv
, 0);
267 TRACE("returning %p\n", store
);
271 PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreW(HCRYPTPROV hCryptProv
,
272 DWORD dwFlags
, const void *pvPara
)
274 HCERTSTORE store
= 0;
275 LPCWSTR fileName
= (LPCWSTR
)pvPara
;
276 DWORD access
, create
;
279 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
, debugstr_w(fileName
));
283 SetLastError(ERROR_PATH_NOT_FOUND
);
286 if ((dwFlags
& CERT_STORE_READONLY_FLAG
) &&
287 (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
))
289 SetLastError(E_INVALIDARG
);
293 access
= GENERIC_READ
;
294 if (dwFlags
& CERT_FILE_STORE_COMMIT_ENABLE_FLAG
)
295 access
|= GENERIC_WRITE
;
296 if (dwFlags
& CERT_STORE_CREATE_NEW_FLAG
)
298 else if (dwFlags
& CERT_STORE_OPEN_EXISTING_FLAG
)
299 create
= OPEN_EXISTING
;
301 create
= OPEN_ALWAYS
;
302 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
, create
,
303 FILE_ATTRIBUTE_NORMAL
, NULL
);
304 if (file
!= INVALID_HANDLE_VALUE
)
306 HCERTSTORE memStore
= NULL
;
307 DWORD size
= GetFileSize(file
, NULL
), type
= 0;
309 /* If the file isn't empty, try to get the type from the file itself */
315 /* Close the file so CryptQueryObject can succeed.. */
317 ret
= CryptQueryObject(CERT_QUERY_OBJECT_FILE
, fileName
,
318 CERT_QUERY_CONTENT_FLAG_CERT
|
319 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
|
320 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
,
321 CERT_QUERY_FORMAT_FLAG_BINARY
, 0, NULL
, &contentType
, NULL
,
322 &memStore
, NULL
, NULL
);
325 if (contentType
== CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
)
326 type
= CERT_STORE_SAVE_AS_PKCS7
;
328 type
= CERT_STORE_SAVE_AS_STORE
;
329 /* and reopen the file. */
330 file
= CreateFileW(fileName
, access
, FILE_SHARE_READ
, NULL
,
331 create
, FILE_ATTRIBUTE_NORMAL
, NULL
);
336 static const WCHAR spc
[] = { 's','p','c',0 };
337 static const WCHAR p7c
[] = { 'p','7','c',0 };
338 LPCWSTR ext
= strrchrW(fileName
, '.');
343 if (!lstrcmpiW(ext
, spc
) || !lstrcmpiW(ext
, p7c
))
344 type
= CERT_STORE_SAVE_AS_PKCS7
;
347 type
= CERT_STORE_SAVE_AS_STORE
;
348 memStore
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
349 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
353 store
= CRYPT_CreateFileStore(dwFlags
, memStore
, file
, type
);
354 /* File store doesn't need crypto provider, so close it */
355 if (hCryptProv
&& !(dwFlags
& CERT_STORE_NO_CRYPT_RELEASE_FLAG
))
356 CryptReleaseContext(hCryptProv
, 0);
359 return (PWINECRYPT_CERTSTORE
)store
;
362 PWINECRYPT_CERTSTORE
CRYPT_FileNameOpenStoreA(HCRYPTPROV hCryptProv
,
363 DWORD dwFlags
, const void *pvPara
)
366 PWINECRYPT_CERTSTORE ret
= NULL
;
368 TRACE("(%ld, %08x, %s)\n", hCryptProv
, dwFlags
,
369 debugstr_a((LPCSTR
)pvPara
));
373 SetLastError(ERROR_FILE_NOT_FOUND
);
376 len
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, NULL
, 0);
379 LPWSTR storeName
= CryptMemAlloc(len
* sizeof(WCHAR
));
383 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)pvPara
, -1, storeName
, len
);
384 ret
= CRYPT_FileNameOpenStoreW(hCryptProv
, dwFlags
, storeName
);
385 CryptMemFree(storeName
);