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 };
97 static void testMemStore(void)
99 HCERTSTORE store1, store2;
100 PCCERT_CONTEXT context;
104 store1 = CertOpenStore(0, 0, 0, 0, NULL);
105 ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
106 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
108 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
109 CERT_STORE_DELETE_FLAG, NULL);
110 ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
111 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %ld\n", GetLastError());
114 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
115 CERT_STORE_CREATE_NEW_FLAG, NULL);
116 ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
117 /* open existing doesn't */
118 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
119 CERT_STORE_OPEN_EXISTING_FLAG, NULL);
120 ok(store2 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
121 ok(store1 != store2, "Expected different stores\n");
123 /* add a bogus (empty) cert */
125 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
126 sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
127 /* Windows returns CRYPT_E_ASN1_EOD, but accept CRYPT_E_ASN1_CORRUPT as
128 * well (because matching errors is tough in this case)
130 ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
131 CRYPT_E_ASN1_CORRUPT),
132 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
134 /* add a "signed" cert--the signature isn't a real signature, so this adds
135 * without any check of the signature's validity
137 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
138 signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
139 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
140 ok(context != NULL, "Expected a valid cert context\n");
143 ok(context->cbCertEncoded == sizeof(signedBigCert),
144 "Wrong cert size %ld\n", context->cbCertEncoded);
145 ok(!memcmp(context->pbCertEncoded, signedBigCert,
146 sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
147 /* remove it, the rest of the tests will work on an unsigned cert */
148 ret = CertDeleteCertificateFromStore(context);
149 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
152 /* try adding a "signed" CRL as a cert */
153 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
154 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
155 ok(!ret && (GetLastError() == CRYPT_E_ASN1_BADTAG || GetLastError() ==
156 CRYPT_E_ASN1_CORRUPT),
157 "Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
159 /* add a cert to store1 */
160 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
161 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
162 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
163 ok(context != NULL, "Expected a valid cert context\n");
169 ok(context->cbCertEncoded == sizeof(bigCert),
170 "Wrong cert size %ld\n", context->cbCertEncoded);
171 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
172 "Unexpected encoded cert in context\n");
173 ok(context->hCertStore == store1, "Unexpected store\n");
175 /* check serializing this element */
177 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
178 ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
179 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
181 /* apparently flags are ignored */
182 ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
183 ok(ret, "CertSerializeCertificateStoreElement failed: %08lx\n",
185 buf = HeapAlloc(GetProcessHeap(), 0, size);
188 ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
189 ok(size == sizeof(serializedCert), "Wrong size %ld\n", size);
190 ok(!memcmp(serializedCert, buf, size),
191 "Unexpected serialized cert\n");
192 HeapFree(GetProcessHeap(), 0, buf);
195 ret = CertFreeCertificateContext(context);
196 ok(ret, "CertFreeCertificateContext failed: %08lx\n", GetLastError());
198 /* verify the cert's in store1 */
199 context = CertEnumCertificatesInStore(store1, NULL);
200 ok(context != NULL, "Expected a valid context\n");
201 context = CertEnumCertificatesInStore(store1, context);
202 ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
203 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
204 /* verify store2 (the "open existing" mem store) is still empty */
205 context = CertEnumCertificatesInStore(store2, NULL);
206 ok(!context, "Expected an empty store\n");
207 /* delete the cert from store1, and check it's empty */
208 context = CertEnumCertificatesInStore(store1, NULL);
211 /* Deleting a bitwise copy crashes with an access to an uninitialized
212 * pointer, so a cert context has some special data out there in memory
215 memcpy(©, context, sizeof(copy));
216 ret = CertDeleteCertificateFromStore(©);
218 PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
220 ok(copy != NULL, "CertDuplicateCertificateContext failed: %08lx\n",
222 ret = CertDeleteCertificateFromStore(context);
223 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
225 /* try deleting a copy */
226 ret = CertDeleteCertificateFromStore(copy);
227 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
229 /* check that the store is empty */
230 context = CertEnumCertificatesInStore(store1, NULL);
231 ok(!context, "Expected an empty store\n");
234 /* close an empty store */
235 ret = CertCloseStore(NULL, 0);
236 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
237 ret = CertCloseStore(store1, 0);
238 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
239 ret = CertCloseStore(store2, 0);
240 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
242 /* This seems nonsensical, but you can open a read-only mem store, only
245 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
246 CERT_STORE_READONLY_FLAG, NULL);
247 ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
248 /* yep, this succeeds */
249 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
250 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
251 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
252 ok(context != NULL, "Expected a valid cert context\n");
255 ok(context->cbCertEncoded == sizeof(bigCert),
256 "Wrong cert size %ld\n", context->cbCertEncoded);
257 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
258 "Unexpected encoded cert in context\n");
259 ok(context->hCertStore == store1, "Unexpected store\n");
260 ret = CertDeleteCertificateFromStore(context);
261 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
264 CertCloseStore(store1, 0);
267 static void testCollectionStore(void)
269 HCERTSTORE store1, store2, collection, collection2;
270 PCCERT_CONTEXT context;
273 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
274 CERT_STORE_CREATE_NEW_FLAG, NULL);
276 /* Try adding a cert to any empty collection */
277 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
278 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
279 ok(!ret && GetLastError() == E_ACCESSDENIED,
280 "Expected E_ACCESSDENIED, got %08lx\n", GetLastError());
282 /* Create and add a cert to a memory store */
283 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
284 CERT_STORE_CREATE_NEW_FLAG, NULL);
285 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
286 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
287 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
288 /* Add the memory store to the collection, without allowing adding */
289 ret = CertAddStoreToCollection(collection, store1, 0, 0);
290 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
291 /* Verify the cert is in the collection */
292 context = CertEnumCertificatesInStore(collection, NULL);
293 ok(context != NULL, "Expected a valid context\n");
296 ok(context->hCertStore == collection, "Unexpected store\n");
297 CertFreeCertificateContext(context);
299 /* Check that adding to the collection isn't allowed */
300 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
301 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
302 ok(!ret && GetLastError() == E_ACCESSDENIED,
303 "Expected E_ACCESSDENIED, got %08lx\n", GetLastError());
305 /* Create a new memory store */
306 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
307 CERT_STORE_CREATE_NEW_FLAG, NULL);
308 /* Try adding a store to a non-collection store */
309 ret = CertAddStoreToCollection(store1, store2,
310 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
311 ok(!ret && GetLastError() == E_INVALIDARG,
312 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
313 /* Try adding some bogus stores */
314 /* This crashes in Windows
315 ret = CertAddStoreToCollection(0, store2,
316 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
318 /* This "succeeds"... */
319 ret = CertAddStoreToCollection(collection, 0,
320 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
321 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
322 /* while this crashes.
323 ret = CertAddStoreToCollection(collection, 1,
324 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
327 /* Add it to the collection, this time allowing adding */
328 ret = CertAddStoreToCollection(collection, store2,
329 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
330 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
331 /* Check that adding to the collection is allowed */
332 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
333 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
334 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
335 /* Now check that it was actually added to store2 */
336 context = CertEnumCertificatesInStore(store2, NULL);
337 ok(context != NULL, "Expected a valid context\n");
340 ok(context->hCertStore == store2, "Unexpected store\n");
341 CertFreeCertificateContext(context);
343 /* Check that the collection has both bigCert and bigCert2. bigCert comes
344 * first because store1 was added first.
346 context = CertEnumCertificatesInStore(collection, NULL);
347 ok(context != NULL, "Expected a valid context\n");
350 ok(context->hCertStore == collection, "Unexpected store\n");
351 ok(context->cbCertEncoded == sizeof(bigCert),
352 "Wrong size %ld\n", context->cbCertEncoded);
353 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
354 "Unexpected cert\n");
355 context = CertEnumCertificatesInStore(collection, context);
356 ok(context != NULL, "Expected a valid context\n");
359 ok(context->hCertStore == collection, "Unexpected store\n");
360 ok(context->cbCertEncoded == sizeof(bigCert2),
361 "Wrong size %ld\n", context->cbCertEncoded);
362 ok(!memcmp(context->pbCertEncoded, bigCert2,
363 context->cbCertEncoded), "Unexpected cert\n");
364 context = CertEnumCertificatesInStore(collection, context);
365 ok(!context, "Unexpected cert\n");
368 /* close store2, and check that the collection is unmodified */
369 CertCloseStore(store2, 0);
370 context = CertEnumCertificatesInStore(collection, NULL);
371 ok(context != NULL, "Expected a valid context\n");
374 ok(context->hCertStore == collection, "Unexpected store\n");
375 ok(context->cbCertEncoded == sizeof(bigCert),
376 "Wrong size %ld\n", context->cbCertEncoded);
377 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
378 "Unexpected cert\n");
379 context = CertEnumCertificatesInStore(collection, context);
380 ok(context != NULL, "Expected a valid context\n");
383 ok(context->hCertStore == collection, "Unexpected store\n");
384 ok(context->cbCertEncoded == sizeof(bigCert2),
385 "Wrong size %ld\n", context->cbCertEncoded);
386 ok(!memcmp(context->pbCertEncoded, bigCert2,
387 context->cbCertEncoded), "Unexpected cert\n");
388 context = CertEnumCertificatesInStore(collection, context);
389 ok(!context, "Unexpected cert\n");
393 /* Adding a collection to a collection is legal */
394 collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
395 CERT_STORE_CREATE_NEW_FLAG, NULL);
396 ret = CertAddStoreToCollection(collection2, collection,
397 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
398 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
399 /* check the contents of collection2 */
400 context = CertEnumCertificatesInStore(collection2, NULL);
401 ok(context != NULL, "Expected a valid context\n");
404 ok(context->hCertStore == collection2, "Unexpected store\n");
405 ok(context->cbCertEncoded == sizeof(bigCert),
406 "Wrong size %ld\n", context->cbCertEncoded);
407 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
408 "Unexpected cert\n");
409 context = CertEnumCertificatesInStore(collection2, context);
410 ok(context != NULL, "Expected a valid context\n");
413 ok(context->hCertStore == collection2, "Unexpected store\n");
414 ok(context->cbCertEncoded == sizeof(bigCert2),
415 "Wrong size %ld\n", context->cbCertEncoded);
416 ok(!memcmp(context->pbCertEncoded, bigCert2,
417 context->cbCertEncoded), "Unexpected cert\n");
418 context = CertEnumCertificatesInStore(collection2, context);
419 ok(!context, "Unexpected cert\n");
423 /* I'd like to test closing the collection in the middle of enumeration,
424 * but my tests have been inconsistent. The first time calling
425 * CertEnumCertificatesInStore on a closed collection succeeded, while the
426 * second crashed. So anything appears to be fair game.
427 * I'd also like to test removing a store from a collection in the middle
428 * of an enumeration, but my tests in Windows have been inconclusive.
429 * In one scenario it worked. In another scenario, about a third of the
430 * time this leads to "random" crashes elsewhere in the code. This
431 * probably means this is not allowed.
434 CertCloseStore(store1, 0);
435 CertCloseStore(collection, 0);
436 CertCloseStore(collection2, 0);
438 /* Add the same cert to two memory stores, then put them in a collection */
439 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
440 CERT_STORE_CREATE_NEW_FLAG, NULL);
441 ok(store1 != 0, "CertOpenStore failed: %08lx\n", GetLastError());
442 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
443 CERT_STORE_CREATE_NEW_FLAG, NULL);
444 ok(store2 != 0, "CertOpenStore failed: %08lx\n", GetLastError());
446 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
447 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
448 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
449 ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
450 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
451 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
452 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
453 CERT_STORE_CREATE_NEW_FLAG, NULL);
454 ok(collection != 0, "CertOpenStore failed: %08lx\n", GetLastError());
456 ret = CertAddStoreToCollection(collection, store1,
457 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
458 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
459 ret = CertAddStoreToCollection(collection, store2,
460 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
461 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
463 /* Check that the collection has two copies of the same cert */
464 context = CertEnumCertificatesInStore(collection, NULL);
465 ok(context != NULL, "Expected a valid context\n");
468 ok(context->hCertStore == collection, "Unexpected store\n");
469 ok(context->cbCertEncoded == sizeof(bigCert),
470 "Wrong size %ld\n", context->cbCertEncoded);
471 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
472 "Unexpected cert\n");
473 context = CertEnumCertificatesInStore(collection, context);
474 ok(context != NULL, "Expected a valid context\n");
477 ok(context->hCertStore == collection, "Unexpected store\n");
478 ok(context->cbCertEncoded == sizeof(bigCert),
479 "Wrong size %ld\n", context->cbCertEncoded);
480 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
481 "Unexpected cert\n");
482 context = CertEnumCertificatesInStore(collection, context);
483 ok(context == NULL, "Unexpected cert\n");
487 /* The following would check whether I can delete an identical cert, rather
488 * than one enumerated from the store. It crashes, so that means I must
489 * only call CertDeleteCertificateFromStore with contexts enumerated from
491 context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
493 ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
497 ret = CertDeleteCertificateFromStore(collection, context);
498 printf("ret is %d, GetLastError is %08lx\n", ret, GetLastError());
499 CertFreeCertificateContext(context);
503 /* Now check deleting from the collection. */
504 context = CertEnumCertificatesInStore(collection, NULL);
505 ok(context != NULL, "Expected a valid context\n");
508 CertDeleteCertificateFromStore(context);
509 /* store1 should now be empty */
510 context = CertEnumCertificatesInStore(store1, NULL);
511 ok(!context, "Unexpected cert\n");
512 /* and there should be one certificate in the collection */
513 context = CertEnumCertificatesInStore(collection, NULL);
514 ok(context != NULL, "Expected a valid cert\n");
517 ok(context->hCertStore == collection, "Unexpected store\n");
518 ok(context->cbCertEncoded == sizeof(bigCert),
519 "Wrong size %ld\n", context->cbCertEncoded);
520 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
521 "Unexpected cert\n");
523 context = CertEnumCertificatesInStore(collection, context);
524 ok(context == NULL, "Unexpected cert\n");
527 /* Finally, test removing stores from the collection. No return value, so
528 * it's a bit funny to test.
531 CertRemoveStoreFromCollection(NULL, NULL);
533 /* This "succeeds," no crash, no last error set */
534 SetLastError(0xdeadbeef);
535 CertRemoveStoreFromCollection(store2, collection);
536 ok(GetLastError() == 0xdeadbeef,
537 "Didn't expect an error to be set: %08lx\n", GetLastError());
539 /* After removing store2, the collection should be empty */
540 SetLastError(0xdeadbeef);
541 CertRemoveStoreFromCollection(collection, store2);
542 ok(GetLastError() == 0xdeadbeef,
543 "Didn't expect an error to be set: %08lx\n", GetLastError());
544 context = CertEnumCertificatesInStore(collection, NULL);
545 ok(!context, "Unexpected cert\n");
547 CertCloseStore(collection, 0);
548 CertCloseStore(store2, 0);
549 CertCloseStore(store1, 0);
552 /* Looks for the property with ID propID in the buffer buf. Returns a pointer
553 * to its header if found, NULL if not.
555 static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
558 const struct CertPropIDHeader *ret = NULL;
561 while (size && !ret && !failed)
563 if (size < sizeof(struct CertPropIDHeader))
567 const struct CertPropIDHeader *hdr =
568 (const struct CertPropIDHeader *)buf;
570 size -= sizeof(struct CertPropIDHeader);
571 buf += sizeof(struct CertPropIDHeader);
574 else if (hdr->propID == propID)
586 typedef DWORD (WINAPI *SHDeleteKeyAFunc)(HKEY, LPCSTR);
588 static void testRegStore(void)
590 static const char tempKey[] = "Software\\Wine\\CryptTemp";
596 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
597 ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
598 "Expected ERROR_INVALID_HANDLE, got %ld\n", GetLastError());
599 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
600 ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
601 "Expected ERROR_INVALID_HANDLE, got %ld\n", GetLastError());
603 /* Opening up any old key works.. */
604 key = HKEY_CURRENT_USER;
605 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
606 /* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
608 todo_wine ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
609 CertCloseStore(store, 0);
611 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
613 ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
619 static const char certificates[] = "Certificates\\";
620 char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
622 PCCERT_CONTEXT context;
624 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
625 ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
626 /* Add a certificate. It isn't persisted right away, since it's only
627 * added to the cache..
629 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
630 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
631 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n",
633 /* so flush the cache to force a commit.. */
634 ret = CertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
635 ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
636 /* and check that the expected subkey was written. */
638 ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2),
640 ok(ret, "CryptHashCertificate failed: %ld\n", GetLastError());
641 strcpy(subKeyName, certificates);
642 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
644 sprintf(ptr, "%02X", hash[i]);
645 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
647 ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
653 RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
654 buf = HeapAlloc(GetProcessHeap(), 0, size);
657 rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
658 ok(!rc, "RegQueryValueExA failed: %ld\n", rc);
661 const struct CertPropIDHeader *hdr;
663 /* Both the hash and the cert should be present */
664 hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
665 ok(hdr != NULL, "Expected to find a cert property\n");
668 ok(hdr->cb == sizeof(bigCert2),
669 "Wrong size %ld of cert property\n", hdr->cb);
670 ok(!memcmp((BYTE *)hdr + sizeof(*hdr), bigCert2,
671 hdr->cb), "Unexpected cert in cert property\n");
673 hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
674 ok(hdr != NULL, "Expected to find a hash property\n");
677 ok(hdr->cb == sizeof(hash),
678 "Wrong size %ld of hash property\n", hdr->cb);
679 ok(!memcmp((BYTE *)hdr + sizeof(*hdr), hash,
680 hdr->cb), "Unexpected hash in cert property\n");
683 HeapFree(GetProcessHeap(), 0, buf);
688 /* Remove the existing context */
689 context = CertEnumCertificatesInStore(store, NULL);
690 ok(context != NULL, "Expected a cert context\n");
692 CertDeleteCertificateFromStore(context);
693 ret = CertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
694 ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
696 /* Add a serialized cert with a bogus hash directly to the registry */
697 memset(hash, 0, sizeof(hash));
698 strcpy(subKeyName, certificates);
699 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
700 i < sizeof(hash); i++, ptr += 2)
701 sprintf(ptr, "%02X", hash[i]);
702 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
704 ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
707 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
708 sizeof(bigCert)], *ptr;
710 struct CertPropIDHeader *hdr;
712 hdr = (struct CertPropIDHeader *)buf;
713 hdr->propID = CERT_HASH_PROP_ID;
715 hdr->cb = sizeof(hash);
716 ptr = buf + sizeof(*hdr);
717 memcpy(ptr, hash, sizeof(hash));
719 hdr = (struct CertPropIDHeader *)ptr;
720 hdr->propID = CERT_CERT_PROP_ID;
722 hdr->cb = sizeof(bigCert);
724 memcpy(ptr, bigCert, sizeof(bigCert));
726 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
728 ok(!rc, "RegSetValueExA failed: %ld\n", rc);
730 ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
731 ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
733 /* Make sure the bogus hash cert gets loaded. */
737 context = CertEnumCertificatesInStore(store, context);
740 } while (context != NULL);
741 ok(certCount == 1, "Expected 1 certificates, got %ld\n", certCount);
746 /* Add another serialized cert directly to the registry, this time
747 * under the correct key name (named with the correct hash value).
750 ret = CryptHashCertificate(0, 0, 0, bigCert2,
751 sizeof(bigCert2), hash, &size);
752 ok(ret, "CryptHashCertificate failed: %ld\n", GetLastError());
753 strcpy(subKeyName, certificates);
754 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
755 i < sizeof(hash); i++, ptr += 2)
756 sprintf(ptr, "%02X", hash[i]);
757 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
759 ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
762 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
763 sizeof(bigCert2)], *ptr;
765 PCCERT_CONTEXT context;
766 struct CertPropIDHeader *hdr;
768 /* First try with a bogus hash... */
769 hdr = (struct CertPropIDHeader *)buf;
770 hdr->propID = CERT_HASH_PROP_ID;
772 hdr->cb = sizeof(hash);
773 ptr = buf + sizeof(*hdr);
774 memset(ptr, 0, sizeof(hash));
776 hdr = (struct CertPropIDHeader *)ptr;
777 hdr->propID = CERT_CERT_PROP_ID;
779 hdr->cb = sizeof(bigCert2);
781 memcpy(ptr, bigCert2, sizeof(bigCert2));
783 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
785 ok(!rc, "RegSetValueExA failed: %ld\n", rc);
787 ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
788 ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
790 /* and make sure just one cert still gets loaded. */
794 context = CertEnumCertificatesInStore(store, context);
797 } while (context != NULL);
798 ok(certCount == 1, "Expected 1 certificates, got %ld\n", certCount);
800 /* Try again with the correct hash... */
801 ptr = buf + sizeof(*hdr);
802 memcpy(ptr, hash, sizeof(hash));
804 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
806 ok(!rc, "RegSetValueExA failed: %ld\n", rc);
808 ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
809 ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
811 /* and make sure two certs get loaded. */
815 context = CertEnumCertificatesInStore(store, context);
818 } while (context != NULL);
819 ok(certCount == 2, "Expected 2 certificates, got %ld\n", certCount);
823 CertCloseStore(store, 0);
824 /* Is delete allowed on a reg store? */
825 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
826 CERT_STORE_DELETE_FLAG, key);
827 ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
828 ok(GetLastError() == 0, "CertOpenStore failed: %08lx\n",
833 /* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
834 * contents of the key, but not the key itself.
836 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
838 ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
839 ok(disp == REG_OPENED_EXISTING_KEY,
840 "Expected REG_OPENED_EXISTING_KEY, got %ld\n", disp);
844 rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
847 HMODULE shlwapi = LoadLibraryA("shlwapi");
849 /* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
850 * otherwise subsequent tests will fail.
854 SHDeleteKeyAFunc pSHDeleteKeyA =
855 (SHDeleteKeyAFunc)GetProcAddress(shlwapi, "SHDeleteKeyA");
858 pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
859 FreeLibrary(shlwapi);
865 static const char MyA[] = { 'M','y',0,0 };
866 static const WCHAR MyW[] = { 'M','y',0 };
867 static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
868 static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
869 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
870 't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
872 static void testSystemRegStore(void)
874 HCERTSTORE store, memStore;
876 /* Check with a UNICODE name */
877 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
878 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
879 /* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
880 * testing if they don't.
885 /* Check that it isn't a collection store */
886 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
887 CERT_STORE_CREATE_NEW_FLAG, NULL);
890 BOOL ret = CertAddStoreToCollection(store, memStore, 0, 0);
892 ok(!ret && GetLastError() == E_INVALIDARG,
893 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
894 CertCloseStore(memStore, 0);
896 CertCloseStore(store, 0);
898 /* Check opening a bogus store */
899 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
900 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
901 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
902 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
903 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
904 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
905 ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
907 CertCloseStore(store, 0);
908 /* Now check whether deleting is allowed */
909 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
910 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
911 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
913 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
914 ok(!store && GetLastError() == E_INVALIDARG,
915 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
916 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
917 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
918 ok(!store && GetLastError() == E_INVALIDARG,
919 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
920 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
921 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
922 ok(!store && GetLastError() == E_INVALIDARG,
923 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
924 /* The name is expected to be UNICODE, check with an ASCII name */
925 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
926 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
927 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
928 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
931 static void testSystemStore(void)
933 static const WCHAR baskslashW[] = { '\\',0 };
935 WCHAR keyName[MAX_PATH];
939 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
940 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
941 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
942 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
943 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
944 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
945 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
946 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
947 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
948 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
949 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
950 /* The name is expected to be UNICODE, first check with an ASCII name */
951 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
952 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
953 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
954 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
955 /* Create the expected key */
956 lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
957 lstrcatW(keyName, baskslashW);
958 lstrcatW(keyName, MyW);
959 rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
961 ok(!rc, "RegCreateKeyEx failed: %ld\n", rc);
964 /* Check opening with a UNICODE name, specifying the create new flag */
965 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
966 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
967 ok(!store && GetLastError() == ERROR_FILE_EXISTS,
968 "Expected ERROR_FILE_EXISTS, got %08lx\n", GetLastError());
969 /* Now check opening with a UNICODE name, this time opening existing */
970 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
971 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
972 ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
975 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
976 CERT_STORE_CREATE_NEW_FLAG, NULL);
978 /* Check that it's a collection store */
981 BOOL ret = CertAddStoreToCollection(store, memStore, 0, 0);
983 /* FIXME: this'll fail on NT4, but what error will it give? */
984 ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
985 CertCloseStore(memStore, 0);
987 CertCloseStore(store, 0);
990 /* Check opening a bogus store */
991 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
992 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
993 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
994 "Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
995 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
996 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
997 ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
999 CertCloseStore(store, 0);
1000 /* Now check whether deleting is allowed */
1001 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1002 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1003 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1006 static void testCertOpenSystemStore(void)
1010 store = CertOpenSystemStoreW(0, NULL);
1011 ok(!store && GetLastError() == E_INVALIDARG,
1012 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1013 /* This succeeds, and on WinXP at least, the Bogus key is created under
1014 * HKCU (but not under HKLM, even when run as an administrator.)
1016 store = CertOpenSystemStoreW(0, BogusW);
1017 ok(store != 0, "CertOpenSystemStore failed: %08lx\n", GetLastError());
1019 CertCloseStore(store, 0);
1020 /* Delete it so other tests succeed next time around */
1021 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1022 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1023 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1026 static void testAddSerialized(void)
1030 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
1033 struct CertPropIDHeader *hdr;
1034 PCCERT_CONTEXT context;
1036 ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
1037 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1038 "Expected ERROR_END_OF_MEDIA, got %08lx\n", GetLastError());
1040 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1041 CERT_STORE_CREATE_NEW_FLAG, NULL);
1042 ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
1044 ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
1045 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1046 "Expected ERROR_END_OF_MEDIA, got %08lx\n", GetLastError());
1048 /* Test with an empty property */
1049 hdr = (struct CertPropIDHeader *)buf;
1050 hdr->propID = CERT_CERT_PROP_ID;
1053 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1055 ok(!ret && GetLastError() == E_INVALIDARG,
1056 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1057 /* Test with a bad size in property header */
1058 hdr->cb = sizeof(bigCert) - 1;
1059 memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert));
1060 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1062 ok(!ret && GetLastError() == E_INVALIDARG,
1063 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1064 ret = CertAddSerializedElementToStore(store, buf,
1065 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1067 ok(!ret && GetLastError() == E_INVALIDARG,
1068 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1069 ret = CertAddSerializedElementToStore(store, buf,
1070 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1072 ok(!ret && GetLastError() == E_INVALIDARG,
1073 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1074 /* Kosher size in property header, but no context type */
1075 hdr->cb = sizeof(bigCert);
1076 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1078 ok(!ret && GetLastError() == E_INVALIDARG,
1079 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1080 ret = CertAddSerializedElementToStore(store, buf,
1081 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1083 ok(!ret && GetLastError() == E_INVALIDARG,
1084 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1085 ret = CertAddSerializedElementToStore(store, buf,
1086 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1088 ok(!ret && GetLastError() == E_INVALIDARG,
1089 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1090 /* With a bad context type */
1091 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1092 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1093 ok(!ret && GetLastError() == E_INVALIDARG,
1094 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1095 ret = CertAddSerializedElementToStore(store, buf,
1096 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1097 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1098 ok(!ret && GetLastError() == E_INVALIDARG,
1099 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1100 ret = CertAddSerializedElementToStore(store, buf,
1101 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1102 0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1103 ok(!ret && GetLastError() == E_INVALIDARG,
1104 "Expected E_INVALIDARG, got %08lx\n", GetLastError());
1105 /* Bad unknown field, good type */
1107 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1108 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1109 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1110 "Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
1111 ret = CertAddSerializedElementToStore(store, buf,
1112 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1113 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1114 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1115 "Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
1116 ret = CertAddSerializedElementToStore(store, buf,
1117 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1118 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1119 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1120 "Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
1121 /* Most everything okay, but bad add disposition */
1124 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1125 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1127 ret = CertAddSerializedElementToStore(store, buf,
1128 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1129 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1131 /* Everything okay, but buffer's too big */
1132 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1133 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1134 ok(ret, "CertAddSerializedElementToStore failed: %08lx\n", GetLastError());
1135 /* Everything okay, check it's not re-added */
1136 ret = CertAddSerializedElementToStore(store, buf,
1137 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1138 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1139 ok(!ret && GetLastError() == CRYPT_E_EXISTS,
1140 "Expected CRYPT_E_EXISTS, got %08lx\n", GetLastError());
1142 context = CertEnumCertificatesInStore(store, NULL);
1143 ok(context != NULL, "Expected a cert\n");
1145 CertDeleteCertificateFromStore(context);
1147 /* Try adding with a bogus hash. Oddly enough, it succeeds, and the hash,
1148 * when queried, is the real hash rather than the bogus hash.
1150 hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
1152 hdr->propID = CERT_HASH_PROP_ID;
1154 hdr->cb = sizeof(hash);
1155 memset(hash, 0xc, sizeof(hash));
1156 memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
1157 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1158 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
1159 (const void **)&context);
1160 ok(ret, "CertAddSerializedElementToStore failed: %08lx\n", GetLastError());
1163 BYTE hashVal[20], realHash[20];
1164 DWORD size = sizeof(hashVal);
1166 ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert),
1168 ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
1169 ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
1171 ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
1173 ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
1174 CertFreeCertificateContext(context);
1177 CertCloseStore(store, 0);
1182 /* various combinations of CertOpenStore */
1184 testCollectionStore();
1186 testSystemRegStore();
1189 testCertOpenSystemStore();
1191 testAddSerialized();