2 * crypt32 cert store function tests
4 * Copyright 2005-2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
32 /* The following aren't defined in wincrypt.h, as they're "reserved" */
33 #define CERT_CERT_PROP_ID 32
34 #define CERT_CRL_PROP_ID 33
35 #define CERT_CTL_PROP_ID 34
37 struct CertPropIDHeader
44 static const BYTE emptyCert[] = { 0x30, 0x00 };
45 static const BYTE bigCert[] = { 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
46 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
47 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22,
48 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
49 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30,
50 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30,
51 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20,
52 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01,
53 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
54 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01 };
55 static const BYTE signedBigCert[] = {
56 0x30, 0x81, 0x93, 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06, 0x00, 0x30,
57 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a,
58 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22, 0x18, 0x0f,
59 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
60 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30,
61 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06,
62 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61,
63 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01, 0x00, 0xa3,
64 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
65 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
66 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
67 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
68 static const BYTE serializedCert[] = { 0x20, 0x00, 0x00, 0x00,
69 0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x30, 0x7a, 0x02, 0x01, 0x01,
70 0x30, 0x02, 0x06, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
71 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67,
72 0x00, 0x30, 0x22, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31,
73 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31,
74 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15,
75 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75,
76 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06,
77 0x00, 0x03, 0x01, 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
78 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02,
80 static const BYTE signedCRL[] = { 0x30, 0x45, 0x30, 0x2c, 0x30, 0x02, 0x06,
81 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
82 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x18, 0x0f,
83 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
84 0x30, 0x5a, 0x30, 0x02, 0x06, 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c,
85 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
86 static const BYTE bigCert2[] = { 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
87 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
88 0x0a, 0x41, 0x6c, 0x65, 0x78, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22,
89 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30,
90 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30,
91 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30,
92 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x41, 0x6c, 0x65, 0x78, 0x20,
93 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01,
94 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
95 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01 };
98 static BOOL (WINAPI *pCertAddStoreToCollection)(HCERTSTORE,HCERTSTORE,DWORD,DWORD);
99 static BOOL (WINAPI *pCertControlStore)(HCERTSTORE,DWORD,DWORD,void const*);
100 static PCCRL_CONTEXT (WINAPI *pCertEnumCRLsInStore)(HCERTSTORE,PCCRL_CONTEXT);
101 static BOOL (WINAPI *pCertEnumSystemStore)(DWORD,void*,void*,PFN_CERT_ENUM_SYSTEM_STORE);
102 static BOOL (WINAPI *pCertGetStoreProperty)(HCERTSTORE,DWORD,void*,DWORD*);
103 static void (WINAPI *pCertRemoveStoreFromCollection)(HCERTSTORE,HCERTSTORE);
104 static BOOL (WINAPI *pCertSetStoreProperty)(HCERTSTORE,DWORD,DWORD,const void*);
106 static void testMemStore(void)
108 HCERTSTORE store1, store2;
109 PCCERT_CONTEXT context;
114 store1 = CertOpenStore(0, 0, 0, 0, NULL);
115 ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
116 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
118 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
119 CERT_STORE_DELETE_FLAG, NULL);
120 ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
121 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
124 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
125 CERT_STORE_CREATE_NEW_FLAG, NULL);
126 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
127 /* open existing doesn't */
128 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
129 CERT_STORE_OPEN_EXISTING_FLAG, NULL);
130 ok(store2 != NULL, "CertOpenStore failed: %d\n", GetLastError());
131 ok(store1 != store2, "Expected different stores\n");
133 /* add a bogus (empty) cert */
135 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
136 sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
137 /* Windows returns CRYPT_E_ASN1_EOD or OSS_DATA_ERROR, but accept
138 * CRYPT_E_ASN1_CORRUPT as well (because matching errors is tough in this
141 GLE = GetLastError();
142 ok(!ret && (GLE == CRYPT_E_ASN1_EOD || GLE == CRYPT_E_ASN1_CORRUPT ||
143 GLE == OSS_DATA_ERROR),
144 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
146 /* add a "signed" cert--the signature isn't a real signature, so this adds
147 * without any check of the signature's validity
149 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
150 signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
151 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
152 ok(context != NULL, "Expected a valid cert context\n");
155 ok(context->cbCertEncoded == sizeof(signedBigCert),
156 "Wrong cert size %d\n", context->cbCertEncoded);
157 ok(!memcmp(context->pbCertEncoded, signedBigCert,
158 sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
159 /* remove it, the rest of the tests will work on an unsigned cert */
160 ret = CertDeleteCertificateFromStore(context);
161 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
164 /* try adding a "signed" CRL as a cert */
165 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
166 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
167 GLE = GetLastError();
168 ok(!ret && (GLE == CRYPT_E_ASN1_BADTAG || GLE == CRYPT_E_ASN1_CORRUPT ||
169 GLE == OSS_DATA_ERROR),
170 "Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
172 /* add a cert to store1 */
173 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
174 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
175 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
176 ok(context != NULL, "Expected a valid cert context\n");
182 ok(context->cbCertEncoded == sizeof(bigCert),
183 "Wrong cert size %d\n", context->cbCertEncoded);
184 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
185 "Unexpected encoded cert in context\n");
186 ok(context->hCertStore == store1, "Unexpected store\n");
188 /* check serializing this element */
190 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
191 ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
192 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
194 /* apparently flags are ignored */
195 ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
196 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
198 buf = HeapAlloc(GetProcessHeap(), 0, size);
201 ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
202 ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
203 ok(!memcmp(serializedCert, buf, size),
204 "Unexpected serialized cert\n");
205 HeapFree(GetProcessHeap(), 0, buf);
208 ret = CertFreeCertificateContext(context);
209 ok(ret, "CertFreeCertificateContext failed: %08x\n", GetLastError());
211 /* verify the cert's in store1 */
212 context = CertEnumCertificatesInStore(store1, NULL);
213 ok(context != NULL, "Expected a valid context\n");
214 context = CertEnumCertificatesInStore(store1, context);
215 ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
216 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
217 /* verify store2 (the "open existing" mem store) is still empty */
218 context = CertEnumCertificatesInStore(store2, NULL);
219 ok(!context, "Expected an empty store\n");
220 /* delete the cert from store1, and check it's empty */
221 context = CertEnumCertificatesInStore(store1, NULL);
224 /* Deleting a bitwise copy crashes with an access to an uninitialized
225 * pointer, so a cert context has some special data out there in memory
228 memcpy(©, context, sizeof(copy));
229 ret = CertDeleteCertificateFromStore(©);
231 PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
233 ok(copy != NULL, "CertDuplicateCertificateContext failed: %08x\n",
235 ret = CertDeleteCertificateFromStore(context);
236 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
238 /* try deleting a copy */
239 ret = CertDeleteCertificateFromStore(copy);
241 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
243 /* check that the store is empty */
244 context = CertEnumCertificatesInStore(store1, NULL);
245 ok(!context, "Expected an empty store\n");
248 /* close an empty store */
249 ret = CertCloseStore(NULL, 0);
250 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
251 ret = CertCloseStore(store1, 0);
252 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
253 ret = CertCloseStore(store2, 0);
254 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
256 /* This seems nonsensical, but you can open a read-only mem store, only
259 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
260 CERT_STORE_READONLY_FLAG, NULL);
261 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
262 /* yep, this succeeds */
263 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
264 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
265 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
266 ok(context != NULL, "Expected a valid cert context\n");
269 ok(context->cbCertEncoded == sizeof(bigCert),
270 "Wrong cert size %d\n", context->cbCertEncoded);
271 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
272 "Unexpected encoded cert in context\n");
273 ok(context->hCertStore == store1, "Unexpected store\n");
274 ret = CertDeleteCertificateFromStore(context);
275 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
278 CertCloseStore(store1, 0);
281 static void testCollectionStore(void)
283 HCERTSTORE store1, store2, collection, collection2;
284 PCCERT_CONTEXT context;
287 if (!pCertAddStoreToCollection)
289 win_skip("CertAddStoreToCollection() is not available\n");
293 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
294 CERT_STORE_CREATE_NEW_FLAG, NULL);
296 /* Try adding a cert to any empty collection */
297 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
298 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
299 ok(!ret && GetLastError() == E_ACCESSDENIED,
300 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
302 /* Create and add a cert to a memory store */
303 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
304 CERT_STORE_CREATE_NEW_FLAG, NULL);
305 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
306 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
307 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
308 /* Add the memory store to the collection, without allowing adding */
309 ret = pCertAddStoreToCollection(collection, store1, 0, 0);
310 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
311 /* Verify the cert is in the collection */
312 context = CertEnumCertificatesInStore(collection, NULL);
313 ok(context != NULL, "Expected a valid context\n");
316 ok(context->hCertStore == collection, "Unexpected store\n");
317 CertFreeCertificateContext(context);
319 /* Check that adding to the collection isn't allowed */
320 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
321 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
322 ok(!ret && GetLastError() == E_ACCESSDENIED,
323 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
325 /* Create a new memory store */
326 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
327 CERT_STORE_CREATE_NEW_FLAG, NULL);
328 /* Try adding a store to a non-collection store */
329 ret = pCertAddStoreToCollection(store1, store2,
330 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
331 ok(!ret && GetLastError() == E_INVALIDARG,
332 "Expected E_INVALIDARG, got %08x\n", GetLastError());
333 /* Try adding some bogus stores */
334 /* This crashes in Windows
335 ret = pCertAddStoreToCollection(0, store2,
336 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
338 /* This "succeeds"... */
339 ret = pCertAddStoreToCollection(collection, 0,
340 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
341 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
342 /* while this crashes.
343 ret = pCertAddStoreToCollection(collection, 1,
344 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
347 /* Add it to the collection, this time allowing adding */
348 ret = pCertAddStoreToCollection(collection, store2,
349 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
350 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
351 /* Check that adding to the collection is allowed */
352 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
353 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
354 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
355 /* Now check that it was actually added to store2 */
356 context = CertEnumCertificatesInStore(store2, NULL);
357 ok(context != NULL, "Expected a valid context\n");
360 ok(context->hCertStore == store2, "Unexpected store\n");
361 CertFreeCertificateContext(context);
363 /* Check that the collection has both bigCert and bigCert2. bigCert comes
364 * first because store1 was added first.
366 context = CertEnumCertificatesInStore(collection, NULL);
367 ok(context != NULL, "Expected a valid context\n");
370 ok(context->hCertStore == collection, "Unexpected store\n");
371 ok(context->cbCertEncoded == sizeof(bigCert),
372 "Wrong size %d\n", context->cbCertEncoded);
373 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
374 "Unexpected cert\n");
375 context = CertEnumCertificatesInStore(collection, context);
376 ok(context != NULL, "Expected a valid context\n");
379 ok(context->hCertStore == collection, "Unexpected store\n");
380 ok(context->cbCertEncoded == sizeof(bigCert2),
381 "Wrong size %d\n", context->cbCertEncoded);
382 ok(!memcmp(context->pbCertEncoded, bigCert2,
383 context->cbCertEncoded), "Unexpected cert\n");
384 context = CertEnumCertificatesInStore(collection, context);
385 ok(!context, "Unexpected cert\n");
388 /* close store2, and check that the collection is unmodified */
389 CertCloseStore(store2, 0);
390 context = CertEnumCertificatesInStore(collection, NULL);
391 ok(context != NULL, "Expected a valid context\n");
394 ok(context->hCertStore == collection, "Unexpected store\n");
395 ok(context->cbCertEncoded == sizeof(bigCert),
396 "Wrong size %d\n", context->cbCertEncoded);
397 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
398 "Unexpected cert\n");
399 context = CertEnumCertificatesInStore(collection, context);
400 ok(context != NULL, "Expected a valid context\n");
403 ok(context->hCertStore == collection, "Unexpected store\n");
404 ok(context->cbCertEncoded == sizeof(bigCert2),
405 "Wrong size %d\n", context->cbCertEncoded);
406 ok(!memcmp(context->pbCertEncoded, bigCert2,
407 context->cbCertEncoded), "Unexpected cert\n");
408 context = CertEnumCertificatesInStore(collection, context);
409 ok(!context, "Unexpected cert\n");
413 /* Adding a collection to a collection is legal */
414 collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
415 CERT_STORE_CREATE_NEW_FLAG, NULL);
416 ret = pCertAddStoreToCollection(collection2, collection,
417 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
418 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
419 /* check the contents of collection2 */
420 context = CertEnumCertificatesInStore(collection2, NULL);
421 ok(context != NULL, "Expected a valid context\n");
424 ok(context->hCertStore == collection2, "Unexpected store\n");
425 ok(context->cbCertEncoded == sizeof(bigCert),
426 "Wrong size %d\n", context->cbCertEncoded);
427 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
428 "Unexpected cert\n");
429 context = CertEnumCertificatesInStore(collection2, context);
430 ok(context != NULL, "Expected a valid context\n");
433 ok(context->hCertStore == collection2, "Unexpected store\n");
434 ok(context->cbCertEncoded == sizeof(bigCert2),
435 "Wrong size %d\n", context->cbCertEncoded);
436 ok(!memcmp(context->pbCertEncoded, bigCert2,
437 context->cbCertEncoded), "Unexpected cert\n");
438 context = CertEnumCertificatesInStore(collection2, context);
439 ok(!context, "Unexpected cert\n");
443 /* I'd like to test closing the collection in the middle of enumeration,
444 * but my tests have been inconsistent. The first time calling
445 * CertEnumCertificatesInStore on a closed collection succeeded, while the
446 * second crashed. So anything appears to be fair game.
447 * I'd also like to test removing a store from a collection in the middle
448 * of an enumeration, but my tests in Windows have been inconclusive.
449 * In one scenario it worked. In another scenario, about a third of the
450 * time this leads to "random" crashes elsewhere in the code. This
451 * probably means this is not allowed.
454 CertCloseStore(store1, 0);
455 CertCloseStore(collection, 0);
456 CertCloseStore(collection2, 0);
458 /* Add the same cert to two memory stores, then put them in a collection */
459 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
460 CERT_STORE_CREATE_NEW_FLAG, NULL);
461 ok(store1 != 0, "CertOpenStore failed: %08x\n", GetLastError());
462 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
463 CERT_STORE_CREATE_NEW_FLAG, NULL);
464 ok(store2 != 0, "CertOpenStore failed: %08x\n", GetLastError());
466 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
467 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
468 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
469 ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
470 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
471 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
472 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
473 CERT_STORE_CREATE_NEW_FLAG, NULL);
474 ok(collection != 0, "CertOpenStore failed: %08x\n", GetLastError());
476 ret = pCertAddStoreToCollection(collection, store1,
477 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
478 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
479 ret = pCertAddStoreToCollection(collection, store2,
480 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
481 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
483 /* Check that the collection has two copies of the same cert */
484 context = CertEnumCertificatesInStore(collection, NULL);
485 ok(context != NULL, "Expected a valid context\n");
488 ok(context->hCertStore == collection, "Unexpected store\n");
489 ok(context->cbCertEncoded == sizeof(bigCert),
490 "Wrong size %d\n", context->cbCertEncoded);
491 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
492 "Unexpected cert\n");
493 context = CertEnumCertificatesInStore(collection, context);
494 ok(context != NULL, "Expected a valid context\n");
497 ok(context->hCertStore == collection, "Unexpected store\n");
498 ok(context->cbCertEncoded == sizeof(bigCert),
499 "Wrong size %d\n", context->cbCertEncoded);
500 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
501 "Unexpected cert\n");
502 context = CertEnumCertificatesInStore(collection, context);
503 ok(context == NULL, "Unexpected cert\n");
507 /* The following would check whether I can delete an identical cert, rather
508 * than one enumerated from the store. It crashes, so that means I must
509 * only call CertDeleteCertificateFromStore with contexts enumerated from
511 context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
513 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
517 ret = CertDeleteCertificateFromStore(collection, context);
518 printf("ret is %d, GetLastError is %08x\n", ret, GetLastError());
519 CertFreeCertificateContext(context);
523 /* Now check deleting from the collection. */
524 context = CertEnumCertificatesInStore(collection, NULL);
525 ok(context != NULL, "Expected a valid context\n");
528 CertDeleteCertificateFromStore(context);
529 /* store1 should now be empty */
530 context = CertEnumCertificatesInStore(store1, NULL);
531 ok(!context, "Unexpected cert\n");
532 /* and there should be one certificate in the collection */
533 context = CertEnumCertificatesInStore(collection, NULL);
534 ok(context != NULL, "Expected a valid cert\n");
537 ok(context->hCertStore == collection, "Unexpected store\n");
538 ok(context->cbCertEncoded == sizeof(bigCert),
539 "Wrong size %d\n", context->cbCertEncoded);
540 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
541 "Unexpected cert\n");
543 context = CertEnumCertificatesInStore(collection, context);
544 ok(context == NULL, "Unexpected cert\n");
547 if (!pCertRemoveStoreFromCollection)
549 win_skip("CertRemoveStoreFromCollection() is not available\n");
553 /* Finally, test removing stores from the collection. No return
554 * value, so it's a bit funny to test.
557 * pCertRemoveStoreFromCollection(NULL, NULL);
559 /* This "succeeds," no crash, no last error set */
560 SetLastError(0xdeadbeef);
561 pCertRemoveStoreFromCollection(store2, collection);
562 ok(GetLastError() == 0xdeadbeef,
563 "Didn't expect an error to be set: %08x\n", GetLastError());
565 /* After removing store2, the collection should be empty */
566 SetLastError(0xdeadbeef);
567 pCertRemoveStoreFromCollection(collection, store2);
568 ok(GetLastError() == 0xdeadbeef,
569 "Didn't expect an error to be set: %08x\n", GetLastError());
570 context = CertEnumCertificatesInStore(collection, NULL);
571 ok(!context, "Unexpected cert\n");
574 CertCloseStore(collection, 0);
575 CertCloseStore(store2, 0);
576 CertCloseStore(store1, 0);
578 /* Test adding certificates to and deleting certificates from collections.
580 store1 = CertOpenSystemStoreA(0, "My");
581 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
582 CERT_STORE_CREATE_NEW_FLAG, NULL);
584 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
585 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
586 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
587 CertDeleteCertificateFromStore(context);
589 CertAddStoreToCollection(collection, store1,
590 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
592 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
593 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
594 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
595 CertDeleteCertificateFromStore(context);
597 CertCloseStore(collection, 0);
598 CertCloseStore(store1, 0);
601 /* Looks for the property with ID propID in the buffer buf. Returns a pointer
602 * to its header if found, NULL if not.
604 static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
607 const struct CertPropIDHeader *ret = NULL;
610 while (size && !ret && !failed)
612 if (size < sizeof(struct CertPropIDHeader))
616 const struct CertPropIDHeader *hdr =
617 (const struct CertPropIDHeader *)buf;
619 size -= sizeof(struct CertPropIDHeader);
620 buf += sizeof(struct CertPropIDHeader);
623 else if (hdr->propID == propID)
635 typedef DWORD (WINAPI *SHDeleteKeyAFunc)(HKEY, LPCSTR);
637 static void testRegStore(void)
639 static const char tempKey[] = "Software\\Wine\\CryptTemp";
645 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
646 GLE = GetLastError();
647 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
648 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
649 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
650 GLE = GetLastError();
651 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
652 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
654 /* Opening up any old key works.. */
655 key = HKEY_CURRENT_USER;
656 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
657 /* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
659 todo_wine ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
660 CertCloseStore(store, 0);
662 /* It looks like the remainder pretty much needs CertControlStore() */
663 if (!pCertControlStore)
665 win_skip("CertControlStore() is not available\n");
669 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
671 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
677 static const char certificates[] = "Certificates\\";
678 char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
680 PCCERT_CONTEXT context;
682 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
683 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
684 /* Add a certificate. It isn't persisted right away, since it's only
685 * added to the cache..
687 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
688 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
689 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
691 /* so flush the cache to force a commit.. */
692 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
693 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
694 /* and check that the expected subkey was written. */
696 ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2),
698 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
699 strcpy(subKeyName, certificates);
700 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
702 sprintf(ptr, "%02X", hash[i]);
703 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
705 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
711 RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
712 buf = HeapAlloc(GetProcessHeap(), 0, size);
715 rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
716 ok(!rc, "RegQueryValueExA failed: %d\n", rc);
719 const struct CertPropIDHeader *hdr;
721 /* Both the hash and the cert should be present */
722 hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
723 ok(hdr != NULL, "Expected to find a cert property\n");
726 ok(hdr->cb == sizeof(bigCert2),
727 "Wrong size %d of cert property\n", hdr->cb);
728 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), bigCert2,
729 hdr->cb), "Unexpected cert in cert property\n");
731 hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
732 ok(hdr != NULL, "Expected to find a hash property\n");
735 ok(hdr->cb == sizeof(hash),
736 "Wrong size %d of hash property\n", hdr->cb);
737 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), hash,
738 hdr->cb), "Unexpected hash in cert property\n");
741 HeapFree(GetProcessHeap(), 0, buf);
746 /* Remove the existing context */
747 context = CertEnumCertificatesInStore(store, NULL);
748 ok(context != NULL, "Expected a cert context\n");
750 CertDeleteCertificateFromStore(context);
751 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
752 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
754 /* Add a serialized cert with a bogus hash directly to the registry */
755 memset(hash, 0, sizeof(hash));
756 strcpy(subKeyName, certificates);
757 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
758 i < sizeof(hash); i++, ptr += 2)
759 sprintf(ptr, "%02X", hash[i]);
760 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
762 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
765 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
766 sizeof(bigCert)], *ptr;
768 struct CertPropIDHeader *hdr;
770 hdr = (struct CertPropIDHeader *)buf;
771 hdr->propID = CERT_HASH_PROP_ID;
773 hdr->cb = sizeof(hash);
774 ptr = buf + sizeof(*hdr);
775 memcpy(ptr, hash, sizeof(hash));
777 hdr = (struct CertPropIDHeader *)ptr;
778 hdr->propID = CERT_CERT_PROP_ID;
780 hdr->cb = sizeof(bigCert);
782 memcpy(ptr, bigCert, sizeof(bigCert));
784 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
786 ok(!rc, "RegSetValueExA failed: %d\n", rc);
788 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
789 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
791 /* Make sure the bogus hash cert gets loaded. */
795 context = CertEnumCertificatesInStore(store, context);
798 } while (context != NULL);
799 ok(certCount == 1, "Expected 1 certificates, got %d\n", certCount);
804 /* Add another serialized cert directly to the registry, this time
805 * under the correct key name (named with the correct hash value).
808 ret = CryptHashCertificate(0, 0, 0, bigCert2,
809 sizeof(bigCert2), hash, &size);
810 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
811 strcpy(subKeyName, certificates);
812 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
813 i < sizeof(hash); i++, ptr += 2)
814 sprintf(ptr, "%02X", hash[i]);
815 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
817 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
820 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
821 sizeof(bigCert2)], *ptr;
823 PCCERT_CONTEXT context;
824 struct CertPropIDHeader *hdr;
826 /* First try with a bogus hash... */
827 hdr = (struct CertPropIDHeader *)buf;
828 hdr->propID = CERT_HASH_PROP_ID;
830 hdr->cb = sizeof(hash);
831 ptr = buf + sizeof(*hdr);
832 memset(ptr, 0, sizeof(hash));
834 hdr = (struct CertPropIDHeader *)ptr;
835 hdr->propID = CERT_CERT_PROP_ID;
837 hdr->cb = sizeof(bigCert2);
839 memcpy(ptr, bigCert2, sizeof(bigCert2));
841 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
843 ok(!rc, "RegSetValueExA failed: %d\n", rc);
845 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
846 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
848 /* and make sure just one cert still gets loaded. */
852 context = CertEnumCertificatesInStore(store, context);
855 } while (context != NULL);
857 broken(certCount == 2), /* win9x */
858 "Expected 1 certificates, got %d\n", certCount);
860 /* Try again with the correct hash... */
861 ptr = buf + sizeof(*hdr);
862 memcpy(ptr, hash, sizeof(hash));
864 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
866 ok(!rc, "RegSetValueExA failed: %d\n", rc);
868 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
869 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
871 /* and make sure two certs get loaded. */
875 context = CertEnumCertificatesInStore(store, context);
878 } while (context != NULL);
879 ok(certCount == 2, "Expected 2 certificates, got %d\n", certCount);
883 CertCloseStore(store, 0);
884 /* Is delete allowed on a reg store? */
885 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
886 CERT_STORE_DELETE_FLAG, key);
887 ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
888 ok(GetLastError() == 0, "CertOpenStore failed: %08x\n",
893 /* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
894 * contents of the key, but not the key itself.
896 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
898 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
899 ok(disp == REG_OPENED_EXISTING_KEY,
900 "Expected REG_OPENED_EXISTING_KEY, got %d\n", disp);
904 rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
907 HMODULE shlwapi = LoadLibraryA("shlwapi");
909 /* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
910 * otherwise subsequent tests will fail.
914 SHDeleteKeyAFunc pSHDeleteKeyA =
915 (SHDeleteKeyAFunc)GetProcAddress(shlwapi, "SHDeleteKeyA");
918 pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
919 FreeLibrary(shlwapi);
925 static const char MyA[] = { 'M','y',0,0 };
926 static const WCHAR MyW[] = { 'M','y',0 };
927 static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
928 static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
929 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
930 't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
932 static void testSystemRegStore(void)
934 HCERTSTORE store, memStore;
936 /* Check with a UNICODE name */
937 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
938 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
939 /* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
940 * testing if they don't.
945 /* Check that it isn't a collection store */
946 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
947 CERT_STORE_CREATE_NEW_FLAG, NULL);
950 if (pCertAddStoreToCollection)
952 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
953 ok(!ret && GetLastError() == E_INVALIDARG,
954 "Expected E_INVALIDARG, got %08x\n", GetLastError());
956 CertCloseStore(memStore, 0);
958 CertCloseStore(store, 0);
960 /* Check opening a bogus store */
961 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
962 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
963 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
964 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
965 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
966 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
967 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
969 CertCloseStore(store, 0);
970 /* Now check whether deleting is allowed */
971 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
972 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
973 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
975 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
976 ok(!store && GetLastError() == E_INVALIDARG,
977 "Expected E_INVALIDARG, got %08x\n", GetLastError());
978 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
979 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
980 ok(!store && GetLastError() == E_INVALIDARG,
981 "Expected E_INVALIDARG, got %08x\n", GetLastError());
982 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
983 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
984 ok(!store && GetLastError() == E_INVALIDARG,
985 "Expected E_INVALIDARG, got %08x\n", GetLastError());
986 /* The name is expected to be UNICODE, check with an ASCII name */
987 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
988 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
989 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
990 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
993 static void testSystemStore(void)
995 static const WCHAR baskslashW[] = { '\\',0 };
997 WCHAR keyName[MAX_PATH];
1001 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
1002 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1003 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1004 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1005 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1006 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1007 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1008 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1009 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1010 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1011 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1012 /* The name is expected to be UNICODE, first check with an ASCII name */
1013 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1014 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1015 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1016 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1017 /* Create the expected key */
1018 lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
1019 lstrcatW(keyName, baskslashW);
1020 lstrcatW(keyName, MyW);
1021 rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
1023 ok(!rc, "RegCreateKeyEx failed: %d\n", rc);
1026 /* Check opening with a UNICODE name, specifying the create new flag */
1027 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1028 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
1029 ok(!store && GetLastError() == ERROR_FILE_EXISTS,
1030 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
1031 /* Now check opening with a UNICODE name, this time opening existing */
1032 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1033 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1034 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1037 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1038 CERT_STORE_CREATE_NEW_FLAG, NULL);
1040 /* Check that it's a collection store */
1043 if (pCertAddStoreToCollection)
1045 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1046 /* FIXME: this'll fail on NT4, but what error will it give? */
1047 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
1049 CertCloseStore(memStore, 0);
1051 CertCloseStore(store, 0);
1054 /* Check opening a bogus store */
1055 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1056 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1058 broken(store != 0)) && /* win9x */
1059 GetLastError() == ERROR_FILE_NOT_FOUND,
1060 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1061 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1062 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1063 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1065 CertCloseStore(store, 0);
1066 /* Now check whether deleting is allowed */
1067 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1068 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1069 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1072 static const BYTE serializedStoreWithCert[] = {
1073 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1074 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1075 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1076 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1077 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1078 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1079 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1080 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1081 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1082 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
1083 0x00,0x00,0x00,0x00,0x00,0x00 };
1084 static const BYTE serializedStoreWithCertAndCRL[] = {
1085 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1086 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1087 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1088 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1089 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1090 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1091 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1092 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1093 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1094 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x21,0x00,0x00,0x00,0x01,0x00,
1095 0x00,0x00,0x47,0x00,0x00,0x00,0x30,0x45,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,
1096 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1097 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1098 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x03,0x11,
1099 0x00,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,
1100 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1102 static void compareFile(LPCWSTR filename, const BYTE *pb, DWORD cb)
1107 DWORD cbRead = 0, totalRead = 0;
1109 h = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1110 FILE_ATTRIBUTE_NORMAL, NULL);
1111 if (h == INVALID_HANDLE_VALUE)
1114 ret = ReadFile(h, buf, sizeof(buf), &cbRead, NULL);
1117 ok(totalRead + cbRead <= cb, "Expected total count %d, see %d\n",
1118 cb, totalRead + cbRead);
1119 ok(!memcmp(pb + totalRead, buf, cbRead),
1120 "Unexpected data in file\n");
1121 totalRead += cbRead;
1123 } while (ret && cbRead);
1127 static void testFileStore(void)
1129 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1130 static const WCHAR szDot[] = { '.',0 };
1131 WCHAR filename[MAX_PATH];
1134 PCCERT_CONTEXT cert;
1137 if (!pCertControlStore)
1139 win_skip("CertControlStore() is not available\n");
1143 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, 0, NULL);
1144 ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
1145 "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1147 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1150 DeleteFileW(filename);
1151 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1152 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1153 if (file == INVALID_HANDLE_VALUE)
1156 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, CERT_STORE_DELETE_FLAG,
1158 ok(!store && GetLastError() == E_INVALIDARG,
1159 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1160 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1161 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, file);
1162 ok(!store && GetLastError() == E_INVALIDARG,
1163 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1165 /* A "read-only" file store.. */
1166 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1167 CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, file);
1168 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1173 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1174 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1175 /* apparently allows adding certificates.. */
1176 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1177 /* but not commits.. */
1178 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1179 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1180 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1181 /* It still has certs in memory.. */
1182 cert = CertEnumCertificatesInStore(store, NULL);
1183 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1185 CertFreeCertificateContext(cert);
1186 /* but the file size is still 0. */
1187 size = GetFileSize(file, NULL);
1188 ok(size == 0, "Expected size 0, got %d\n", size);
1189 CertCloseStore(store, 0);
1192 /* The create new flag is allowed.. */
1193 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1194 CERT_STORE_CREATE_NEW_FLAG, file);
1195 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1198 /* but without the commit enable flag, commits don't happen. */
1199 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1200 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1201 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1202 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1203 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1204 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1205 CertCloseStore(store, 0);
1207 /* as is the open existing flag. */
1208 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1209 CERT_STORE_OPEN_EXISTING_FLAG, file);
1210 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1213 /* but without the commit enable flag, commits don't happen. */
1214 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1215 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1216 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1217 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1218 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1219 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1220 CertCloseStore(store, 0);
1222 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1223 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1224 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1228 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1229 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1230 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1232 /* with commits enabled, commit is allowed */
1233 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1234 ok(ret, "CertControlStore failed: %d\n", ret);
1235 compareFile(filename, serializedStoreWithCert,
1236 sizeof(serializedStoreWithCert));
1237 CertCloseStore(store, 0);
1239 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1240 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1241 if (file == INVALID_HANDLE_VALUE)
1243 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1244 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1245 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1249 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
1250 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1251 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1252 CertCloseStore(store, 0);
1253 compareFile(filename, serializedStoreWithCertAndCRL,
1254 sizeof(serializedStoreWithCertAndCRL));
1257 DeleteFileW(filename);
1260 static BOOL initFileFromData(LPCWSTR filename, const BYTE *pb, DWORD cb)
1262 HANDLE file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1263 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1266 if (file != INVALID_HANDLE_VALUE)
1270 ret = WriteFile(file, pb, cb, &written, NULL);
1278 static const BYTE base64SPC[] =
1279 "MIICJQYJKoZIhvcNAQcCoIICFjCCAhICAQExADALBgkqhkiG9w0BBwGgggH6MIIB"
1280 "9jCCAV+gAwIBAgIQnP8+EF4opr9OxH7h4uBPWTANBgkqhkiG9w0BAQQFADAUMRIw"
1281 "EAYDVQQDEwlKdWFuIExhbmcwHhcNMDgxMjEyMTcxMDE0WhcNMzkxMjMxMjM1OTU5"
1282 "WjAUMRIwEAYDVQQDEwlKdWFuIExhbmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ"
1283 "AoGBALCgNjyNvOic0FOfjxvi43HbM+D5joDkhiGSXe+gbZlf8f16k07kkObFEunz"
1284 "mdB5coscmA7gyqiWNN4ZUyr2cA3lCbnpGPA/0IblyyOcuGIFmmCzeZaVa5ZG6xZP"
1285 "K7L7o+73Qo6jXVbGhBGnMZ7Q9sAn6s2933olnStnejnqwV0NAgMBAAGjSTBHMEUG"
1286 "A1UdAQQ+MDyAEFKbKEdXYyx+CWKcV6vxM6ShFjAUMRIwEAYDVQQDEwlKdWFuIExh"
1287 "bmeCEJz/PhBeKKa/TsR+4eLgT1kwDQYJKoZIhvcNAQEEBQADgYEALpkgLgW3mEaK"
1288 "idPQ3iPJYLG0Ub1wraqEl9bd42hrhzIdcDzlQgxnm8/5cHYVxIF/C20x/HJplb1R"
1289 "G6U1ipFe/q8byWD/9JpiBKMGPi9YlUTgXHfS9d4S/QWO1h9Z7KeipBYhoslQpHXu"
1290 "y9bUr8Adqi6SzgHpCnMu53dxgxUD1r4xAA==";
1291 /* Same as base64SPC, but as a wide-char string */
1292 static const WCHAR utf16Base64SPC[] = {
1293 'M','I','I','C','J','Q','Y','J','K','o','Z','I','h','v','c','N','A',
1294 'Q','c','C','o','I','I','C','F','j','C','C','A','h','I','C','A','Q',
1295 'E','x','A','D','A','L','B','g','k','q','h','k','i','G','9','w','0',
1296 'B','B','w','G','g','g','g','H','6','M','I','I','B','9','j','C','C',
1297 'A','V','+','g','A','w','I','B','A','g','I','Q','n','P','8','+','E',
1298 'F','4','o','p','r','9','O','x','H','7','h','4','u','B','P','W','T',
1299 'A','N','B','g','k','q','h','k','i','G','9','w','0','B','A','Q','Q',
1300 'F','A','D','A','U','M','R','I','w','E','A','Y','D','V','Q','Q','D',
1301 'E','w','l','K','d','W','F','u','I','E','x','h','b','m','c','w','H',
1302 'h','c','N','M','D','g','x','M','j','E','y','M','T','c','x','M','D',
1303 'E','0','W','h','c','N','M','z','k','x','M','j','M','x','M','j','M',
1304 '1','O','T','U','5','W','j','A','U','M','R','I','w','E','A','Y','D',
1305 'V','Q','Q','D','E','w','l','K','d','W','F','u','I','E','x','h','b',
1306 'm','c','w','g','Z','8','w','D','Q','Y','J','K','o','Z','I','h','v',
1307 'c','N','A','Q','E','B','B','Q','A','D','g','Y','0','A','M','I','G',
1308 'J','A','o','G','B','A','L','C','g','N','j','y','N','v','O','i','c',
1309 '0','F','O','f','j','x','v','i','4','3','H','b','M','+','D','5','j',
1310 'o','D','k','h','i','G','S','X','e','+','g','b','Z','l','f','8','f',
1311 '1','6','k','0','7','k','k','O','b','F','E','u','n','z','m','d','B',
1312 '5','c','o','s','c','m','A','7','g','y','q','i','W','N','N','4','Z',
1313 'U','y','r','2','c','A','3','l','C','b','n','p','G','P','A','/','0',
1314 'I','b','l','y','y','O','c','u','G','I','F','m','m','C','z','e','Z',
1315 'a','V','a','5','Z','G','6','x','Z','P','K','7','L','7','o','+','7',
1316 '3','Q','o','6','j','X','V','b','G','h','B','G','n','M','Z','7','Q',
1317 '9','s','A','n','6','s','2','9','3','3','o','l','n','S','t','n','e',
1318 'j','n','q','w','V','0','N','A','g','M','B','A','A','G','j','S','T',
1319 'B','H','M','E','U','G','A','1','U','d','A','Q','Q','+','M','D','y',
1320 'A','E','F','K','b','K','E','d','X','Y','y','x','+','C','W','K','c',
1321 'V','6','v','x','M','6','S','h','F','j','A','U','M','R','I','w','E',
1322 'A','Y','D','V','Q','Q','D','E','w','l','K','d','W','F','u','I','E',
1323 'x','h','b','m','e','C','E','J','z','/','P','h','B','e','K','K','a',
1324 '/','T','s','R','+','4','e','L','g','T','1','k','w','D','Q','Y','J',
1325 'K','o','Z','I','h','v','c','N','A','Q','E','E','B','Q','A','D','g',
1326 'Y','E','A','L','p','k','g','L','g','W','3','m','E','a','K','i','d',
1327 'P','Q','3','i','P','J','Y','L','G','0','U','b','1','w','r','a','q',
1328 'E','l','9','b','d','4','2','h','r','h','z','I','d','c','D','z','l',
1329 'Q','g','x','n','m','8','/','5','c','H','Y','V','x','I','F','/','C',
1330 '2','0','x','/','H','J','p','l','b','1','R','G','6','U','1','i','p',
1331 'F','e','/','q','8','b','y','W','D','/','9','J','p','i','B','K','M',
1332 'G','P','i','9','Y','l','U','T','g','X','H','f','S','9','d','4','S',
1333 '/','Q','W','O','1','h','9','Z','7','K','e','i','p','B','Y','h','o',
1334 's','l','Q','p','H','X','u','y','9','b','U','r','8','A','d','q','i',
1335 '6','S','z','g','H','p','C','n','M','u','5','3','d','x','g','x','U',
1336 'D','1','r','4','x','A','A','=','=',0 };
1338 static void testFileNameStore(void)
1340 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1341 static const WCHAR spcPrefix[] = { 's','p','c',0 };
1342 static const WCHAR szDot[] = { '.',0 };
1343 WCHAR filename[MAX_PATH];
1350 /* Crashes on NT4 */
1351 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0, 0, NULL);
1352 GLE = GetLastError();
1353 ok(!store && (GLE == ERROR_PATH_NOT_FOUND || GLE == ERROR_INVALID_PARAMETER),
1354 "Expected ERROR_PATH_NOT_FOUND or ERROR_INVALID_PARAMETER, got %08x\n",
1358 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1360 DeleteFileW(filename);
1362 /* The two flags are mutually exclusive */
1363 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1364 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, filename);
1365 ok(!store && GetLastError() == E_INVALIDARG,
1366 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1368 /* In all of the following tests, the encoding type seems to be ignored */
1369 if (initFileFromData(filename, bigCert, sizeof(bigCert)))
1371 PCCERT_CONTEXT cert;
1374 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1375 CERT_STORE_READONLY_FLAG, filename);
1376 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1378 cert = CertEnumCertificatesInStore(store, NULL);
1379 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1381 cert = CertEnumCertificatesInStore(store, cert);
1382 ok(!cert, "Expected only one cert\n");
1383 if (pCertEnumCRLsInStore)
1385 crl = pCertEnumCRLsInStore(store, NULL);
1386 ok(!crl, "Expected no CRLs\n");
1389 CertCloseStore(store, 0);
1390 DeleteFileW(filename);
1392 if (initFileFromData(filename, serializedStoreWithCert,
1393 sizeof(serializedStoreWithCert)))
1395 PCCERT_CONTEXT cert;
1398 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1399 CERT_STORE_READONLY_FLAG, filename);
1400 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1402 cert = CertEnumCertificatesInStore(store, NULL);
1403 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1405 cert = CertEnumCertificatesInStore(store, cert);
1406 ok(!cert, "Expected only one cert\n");
1407 if (pCertEnumCRLsInStore)
1409 crl = pCertEnumCRLsInStore(store, NULL);
1410 ok(!crl, "Expected no CRLs\n");
1413 CertCloseStore(store, 0);
1414 DeleteFileW(filename);
1416 if (initFileFromData(filename, serializedStoreWithCertAndCRL,
1417 sizeof(serializedStoreWithCertAndCRL)))
1419 PCCERT_CONTEXT cert;
1422 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1423 CERT_STORE_READONLY_FLAG, filename);
1424 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1426 cert = CertEnumCertificatesInStore(store, NULL);
1427 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1429 cert = CertEnumCertificatesInStore(store, cert);
1430 ok(!cert, "Expected only one cert\n");
1431 if (pCertEnumCRLsInStore)
1433 crl = pCertEnumCRLsInStore(store, NULL);
1434 ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n", GetLastError());
1435 crl = pCertEnumCRLsInStore(store, crl);
1436 ok(!crl, "Expected only one CRL\n");
1439 CertCloseStore(store, 0);
1440 /* Don't delete it this time, the next test uses it */
1442 /* Now that the file exists, we can open it read-only */
1443 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1444 CERT_STORE_READONLY_FLAG, filename);
1445 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1446 CertCloseStore(store, 0);
1447 DeleteFileW(filename);
1449 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1450 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_CREATE_NEW_FLAG, filename);
1451 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1454 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1455 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1456 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1458 CertCloseStore(store, 0);
1459 compareFile(filename, serializedStoreWithCert,
1460 sizeof(serializedStoreWithCert));
1462 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1463 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, filename);
1464 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1467 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING,
1468 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1469 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1470 CertCloseStore(store, 0);
1471 compareFile(filename, serializedStoreWithCertAndCRL,
1472 sizeof(serializedStoreWithCertAndCRL));
1474 DeleteFileW(filename);
1476 if (!GetTempFileNameW(szDot, spcPrefix, 0, filename))
1478 DeleteFileW(filename);
1480 if (initFileFromData(filename, base64SPC, sizeof(base64SPC)))
1482 PCCERT_CONTEXT cert;
1485 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1486 CERT_STORE_READONLY_FLAG, filename);
1487 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1489 cert = CertEnumCertificatesInStore(store, NULL);
1490 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1492 cert = CertEnumCertificatesInStore(store, cert);
1493 ok(!cert, "Expected only one cert\n");
1494 if (pCertEnumCRLsInStore)
1496 crl = pCertEnumCRLsInStore(store, NULL);
1497 ok(!crl, "Expected no CRLs\n");
1500 CertCloseStore(store, 0);
1501 DeleteFileW(filename);
1503 if (initFileFromData(filename, (BYTE *)utf16Base64SPC,
1504 sizeof(utf16Base64SPC)))
1506 PCCERT_CONTEXT cert;
1509 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1510 CERT_STORE_READONLY_FLAG, filename);
1511 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1513 cert = CertEnumCertificatesInStore(store, NULL);
1514 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1516 cert = CertEnumCertificatesInStore(store, cert);
1517 ok(!cert, "Expected only one cert\n");
1518 if (pCertEnumCRLsInStore)
1520 crl = pCertEnumCRLsInStore(store, NULL);
1521 ok(!crl, "Expected no CRLs\n");
1524 CertCloseStore(store, 0);
1525 DeleteFileW(filename);
1529 static const BYTE signedContent[] = {
1530 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
1531 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
1532 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
1533 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
1534 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
1535 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1536 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1537 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
1538 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
1539 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
1540 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
1541 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
1543 static const BYTE signedWithCertAndCrlBareContent[] = {
1544 0x30,0x82,0x01,0x4f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,
1545 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,
1546 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0xa0,
1547 0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,
1548 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
1549 0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,
1550 0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1551 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,
1552 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1553 0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,
1554 0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,
1555 0x01,0xff,0x02,0x01,0x01,0xa1,0x2e,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,0x15,
1556 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1557 0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
1558 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x31,0x77,0x30,0x75,0x02,0x01,0x01,
1559 0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,
1560 0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,
1561 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,
1562 0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,
1563 0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,
1564 0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,
1565 0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,
1566 0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
1567 static const BYTE hashContent[] = {
1568 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
1569 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1570 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1571 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1572 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1573 static const BYTE hashBareContent[] = {
1574 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1575 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1576 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1577 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1579 static void testMessageStore(void)
1582 HCRYPTMSG msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, 0, 0, NULL,
1584 CRYPT_DATA_BLOB blob = { sizeof(signedWithCertAndCrlBareContent),
1585 (LPBYTE)signedWithCertAndCrlBareContent };
1590 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, NULL);
1592 SetLastError(0xdeadbeef);
1593 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1594 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1595 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1596 CryptMsgUpdate(msg, signedContent, sizeof(signedContent), TRUE);
1597 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1598 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1601 PCCERT_CONTEXT cert = NULL;
1602 PCCRL_CONTEXT crl = NULL;
1606 cert = CertEnumCertificatesInStore(store, cert);
1610 ok(count == 0, "Expected 0 certificates, got %d\n", count);
1612 if (pCertEnumCRLsInStore)
1616 crl = pCertEnumCRLsInStore(store, crl);
1620 ok(count == 0, "Expected 0 CRLs, got %d\n", count);
1623 /* Can add certs to a message store */
1624 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1625 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1626 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1630 cert = CertEnumCertificatesInStore(store, cert);
1634 ok(count == 1, "Expected 1 certificate, got %d\n", count);
1636 CertCloseStore(store, 0);
1638 /* but the added certs weren't actually added to the message */
1639 size = sizeof(count);
1640 ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
1641 ok(ret, "CryptMsgGetParam failed: %08x\n", GetLastError());
1642 ok(count == 0, "Expected 0 certificates, got %d\n", count);
1646 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, NULL);
1648 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1649 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1653 PCCERT_CONTEXT cert = NULL;
1654 PCCRL_CONTEXT crl = NULL;
1657 cert = CertEnumCertificatesInStore(store, cert);
1661 ok(count == 1, "Expected 1 certificate, got %d\n", count);
1663 if (pCertEnumCRLsInStore)
1667 crl = pCertEnumCRLsInStore(store, crl);
1671 ok(count == 1, "Expected 1 CRL, got %d\n", count);
1673 CertCloseStore(store, 0);
1675 /* Encoding appears to be ignored */
1676 store = CertOpenStore(CERT_STORE_PROV_PKCS7, X509_ASN_ENCODING, 0, 0,
1678 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1680 CertCloseStore(store, 0);
1681 /* Messages other than signed messages aren't allowed */
1682 blob.cbData = sizeof(hashContent);
1683 blob.pbData = (LPBYTE)hashContent;
1684 SetLastError(0xdeadbeef);
1685 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1686 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1687 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1688 blob.cbData = sizeof(hashBareContent);
1689 blob.pbData = (LPBYTE)hashBareContent;
1690 SetLastError(0xdeadbeef);
1691 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1693 (GetLastError() == CRYPT_E_ASN1_BADTAG ||
1694 GetLastError() == OSS_DATA_ERROR), /* win9x */
1695 "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
1698 static void testCertOpenSystemStore(void)
1702 store = CertOpenSystemStoreW(0, NULL);
1703 ok(!store && GetLastError() == E_INVALIDARG,
1704 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1705 /* This succeeds, and on WinXP at least, the Bogus key is created under
1706 * HKCU (but not under HKLM, even when run as an administrator.)
1708 store = CertOpenSystemStoreW(0, BogusW);
1709 ok(store != 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
1711 CertCloseStore(store, 0);
1712 /* Delete it so other tests succeed next time around */
1713 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1714 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1715 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1718 struct EnumSystemStoreInfo
1724 static BOOL CALLBACK enumSystemStoreCB(const void *systemStore, DWORD dwFlags,
1725 PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved, void *pvArg)
1727 struct EnumSystemStoreInfo *info = pvArg;
1733 static void testCertEnumSystemStore(void)
1736 struct EnumSystemStoreInfo info = { FALSE, 0 };
1738 if (!pCertEnumSystemStore)
1740 win_skip("CertEnumSystemStore() is not available\n");
1744 SetLastError(0xdeadbeef);
1745 ret = pCertEnumSystemStore(0, NULL, NULL, NULL);
1746 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1747 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1749 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, NULL,
1753 SetLastError(0xdeadbeef);
1754 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1756 /* Callback returning FALSE stops enumeration */
1757 ok(!ret, "Expected CertEnumSystemStore to stop\n");
1758 ok(info.storeCount == 0 || info.storeCount == 1,
1759 "Expected 0 or 1 stores\n");
1762 info.storeCount = 0;
1763 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1765 ok(ret, "CertEnumSystemStore failed: %08x\n", GetLastError());
1766 /* There should always be at least My, Root, and CA stores */
1767 ok(info.storeCount == 0 || info.storeCount >= 3,
1768 "Expected at least 3 stores\n");
1771 static void testStoreProperty(void)
1775 DWORD propID, size = 0, state;
1776 CRYPT_DATA_BLOB blob;
1778 if (!pCertGetStoreProperty || !pCertSetStoreProperty)
1780 win_skip("CertGet/SetStoreProperty() is not available\n");
1785 ret = pCertGetStoreProperty(NULL, 0, NULL, NULL);
1786 ret = pCertGetStoreProperty(NULL, 0, NULL, &size);
1787 ret = pCertGetStoreProperty(store, 0, NULL, NULL);
1790 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1791 CERT_STORE_CREATE_NEW_FLAG, NULL);
1792 /* Check a missing prop ID */
1793 SetLastError(0xdeadbeef);
1794 ret = pCertGetStoreProperty(store, 0, NULL, &size);
1795 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1796 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1797 /* Contrary to MSDN, CERT_ACCESS_STATE_PROP_ID is supported for stores.. */
1798 size = sizeof(state);
1799 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1800 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1802 ok(!state, "Expected a non-persisted store\n");
1803 /* and CERT_STORE_LOCALIZED_NAME_PROP_ID isn't supported by default. */
1805 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1807 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1808 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1809 /* Delete an arbitrary property on a store */
1810 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1811 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1812 /* Set an arbitrary property on a store */
1813 blob.pbData = (LPBYTE)&state;
1814 blob.cbData = sizeof(state);
1815 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, &blob);
1816 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1817 /* Get an arbitrary property that's been set */
1818 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1819 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1820 ok(size == sizeof(state), "Unexpected data size %d\n", size);
1821 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, &propID, &size);
1822 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1823 ok(propID == state, "CertGetStoreProperty got the wrong value\n");
1824 /* Delete it again */
1825 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1826 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1827 /* And check that it's missing */
1828 SetLastError(0xdeadbeef);
1829 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1830 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1831 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1832 CertCloseStore(store, 0);
1834 /* Recheck on the My store.. */
1835 store = CertOpenSystemStoreW(0, MyW);
1836 size = sizeof(state);
1837 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1838 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1840 ok(state, "Expected a persisted store\n");
1841 SetLastError(0xdeadbeef);
1843 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1845 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1846 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1847 CertCloseStore(store, 0);
1850 static void testAddSerialized(void)
1854 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
1857 struct CertPropIDHeader *hdr;
1858 PCCERT_CONTEXT context;
1860 ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
1861 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1862 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1864 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1865 CERT_STORE_CREATE_NEW_FLAG, NULL);
1866 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1868 ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
1869 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1870 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1872 /* Test with an empty property */
1873 hdr = (struct CertPropIDHeader *)buf;
1874 hdr->propID = CERT_CERT_PROP_ID;
1877 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1879 ok(!ret && GetLastError() == E_INVALIDARG,
1880 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1881 /* Test with a bad size in property header */
1882 hdr->cb = sizeof(bigCert) - 1;
1883 memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert));
1884 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1886 ok(!ret && GetLastError() == E_INVALIDARG,
1887 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1888 ret = CertAddSerializedElementToStore(store, buf,
1889 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1891 ok(!ret && GetLastError() == E_INVALIDARG,
1892 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1893 ret = CertAddSerializedElementToStore(store, buf,
1894 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1896 ok(!ret && GetLastError() == E_INVALIDARG,
1897 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1898 /* Kosher size in property header, but no context type */
1899 hdr->cb = sizeof(bigCert);
1900 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1902 ok(!ret && GetLastError() == E_INVALIDARG,
1903 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1904 ret = CertAddSerializedElementToStore(store, buf,
1905 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1907 ok(!ret && GetLastError() == E_INVALIDARG,
1908 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1909 ret = CertAddSerializedElementToStore(store, buf,
1910 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1912 ok(!ret && GetLastError() == E_INVALIDARG,
1913 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1914 /* With a bad context type */
1915 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1916 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1917 ok(!ret && GetLastError() == E_INVALIDARG,
1918 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1919 ret = CertAddSerializedElementToStore(store, buf,
1920 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1921 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1922 ok(!ret && GetLastError() == E_INVALIDARG,
1923 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1924 ret = CertAddSerializedElementToStore(store, buf,
1925 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1926 0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1927 ok(!ret && GetLastError() == E_INVALIDARG,
1928 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1929 /* Bad unknown field, good type */
1931 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1932 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1933 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1934 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1935 ret = CertAddSerializedElementToStore(store, buf,
1936 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1937 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1938 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1939 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1940 ret = CertAddSerializedElementToStore(store, buf,
1941 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1942 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1943 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1944 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1945 /* Most everything okay, but bad add disposition */
1948 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1949 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1951 ret = CertAddSerializedElementToStore(store, buf,
1952 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1953 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1955 /* Everything okay, but buffer's too big */
1956 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1957 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1958 ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1959 /* Everything okay, check it's not re-added */
1960 ret = CertAddSerializedElementToStore(store, buf,
1961 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1962 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1963 ok(!ret && GetLastError() == CRYPT_E_EXISTS,
1964 "Expected CRYPT_E_EXISTS, got %08x\n", GetLastError());
1966 context = CertEnumCertificatesInStore(store, NULL);
1967 ok(context != NULL, "Expected a cert\n");
1969 CertDeleteCertificateFromStore(context);
1971 /* Try adding with a bogus hash. Oddly enough, it succeeds, and the hash,
1972 * when queried, is the real hash rather than the bogus hash.
1974 hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
1976 hdr->propID = CERT_HASH_PROP_ID;
1978 hdr->cb = sizeof(hash);
1979 memset(hash, 0xc, sizeof(hash));
1980 memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
1981 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1982 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
1983 (const void **)&context);
1984 ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1987 BYTE hashVal[20], realHash[20];
1988 DWORD size = sizeof(hashVal);
1990 ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert),
1992 ok(ret, "CryptHashCertificate failed: %08x\n", GetLastError());
1993 ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
1995 ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
1997 ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
1998 CertFreeCertificateContext(context);
2001 CertCloseStore(store, 0);
2004 static DWORD countCertsInStore(HCERTSTORE store)
2006 PCCERT_CONTEXT cert = NULL;
2010 cert = CertEnumCertificatesInStore(store, cert);
2017 static DWORD countCRLsInStore(HCERTSTORE store)
2019 PCCRL_CONTEXT crl = NULL;
2023 crl = pCertEnumCRLsInStore(store, crl);
2030 static void test_I_UpdateStore(void)
2032 HMODULE lib = GetModuleHandleA("crypt32");
2033 BOOL (WINAPI *pI_CertUpdatestore)(HCERTSTORE, HCERTSTORE, DWORD, DWORD) =
2034 (void *)GetProcAddress(lib, "I_CertUpdateStore");
2036 HCERTSTORE store1, store2;
2037 PCCERT_CONTEXT cert;
2040 if (!pI_CertUpdatestore)
2042 win_skip("No I_CertUpdateStore\n");
2045 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2046 CERT_STORE_CREATE_NEW_FLAG, NULL);
2047 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2048 CERT_STORE_CREATE_NEW_FLAG, NULL);
2051 ret = pI_CertUpdatestore(NULL, NULL, 0, 0);
2052 ret = pI_CertUpdatestore(store1, NULL, 0, 0);
2053 ret = pI_CertUpdatestore(NULL, store2, 0, 0);
2055 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2056 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2058 CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING, bigCert,
2059 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2060 /* I_CertUpdateStore adds the contexts from store2 to store1 */
2061 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2062 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2063 certs = countCertsInStore(store1);
2064 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2065 /* Calling it a second time has no effect */
2066 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2067 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2068 certs = countCertsInStore(store1);
2069 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2071 /* The last parameters to I_CertUpdateStore appear to be ignored */
2072 ret = pI_CertUpdatestore(store1, store2, 1, 0);
2073 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2074 ret = pI_CertUpdatestore(store1, store2, 0, 1);
2075 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2077 CertAddEncodedCRLToStore(store2, X509_ASN_ENCODING, signedCRL,
2078 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
2080 /* I_CertUpdateStore also adds the CRLs from store2 to store1 */
2081 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2082 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2083 certs = countCertsInStore(store1);
2084 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2085 if (pCertEnumCRLsInStore)
2087 certs = countCRLsInStore(store1);
2088 ok(certs == 1, "Expected 1 CRL, got %d\n", certs);
2091 CertDeleteCertificateFromStore(cert);
2092 /* If a context is deleted from store2, I_CertUpdateStore deletes it
2095 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2096 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2097 certs = countCertsInStore(store1);
2098 ok(certs == 0, "Expected 0 certs, got %d\n", certs);
2100 CertFreeCertificateContext(cert);
2101 CertCloseStore(store1, 0);
2102 CertCloseStore(store2, 0);
2109 hdll = GetModuleHandleA("Crypt32.dll");
2110 pCertAddStoreToCollection = (void*)GetProcAddress(hdll, "CertAddStoreToCollection");
2111 pCertControlStore = (void*)GetProcAddress(hdll, "CertControlStore");
2112 pCertEnumCRLsInStore = (void*)GetProcAddress(hdll, "CertEnumCRLsInStore");
2113 pCertEnumSystemStore = (void*)GetProcAddress(hdll, "CertEnumSystemStore");
2114 pCertGetStoreProperty = (void*)GetProcAddress(hdll, "CertGetStoreProperty");
2115 pCertRemoveStoreFromCollection = (void*)GetProcAddress(hdll, "CertRemoveStoreFromCollection");
2116 pCertSetStoreProperty = (void*)GetProcAddress(hdll, "CertSetStoreProperty");
2118 /* various combinations of CertOpenStore */
2120 testCollectionStore();
2122 testSystemRegStore();
2125 testFileNameStore();
2128 testCertOpenSystemStore();
2129 testCertEnumSystemStore();
2130 testStoreProperty();
2132 testAddSerialized();
2134 test_I_UpdateStore();