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 %d\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 %d\n", GetLastError());
114 store1
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
115 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
116 ok(store1
!= NULL
, "CertOpenStore failed: %d\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: %d\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 %08x\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: %08x\n", GetLastError());
140 ok(context
!= NULL
, "Expected a valid cert context\n");
143 ok(context
->cbCertEncoded
== sizeof(signedBigCert
),
144 "Wrong cert size %d\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: %08x\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 %08x\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: %08x\n", GetLastError());
163 ok(context
!= NULL
, "Expected a valid cert context\n");
169 ok(context
->cbCertEncoded
== sizeof(bigCert
),
170 "Wrong cert size %d\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: %08x\n",
185 buf
= HeapAlloc(GetProcessHeap(), 0, size
);
188 ret
= CertSerializeCertificateStoreElement(context
, 0, buf
, &size
);
189 ok(size
== sizeof(serializedCert
), "Wrong size %d\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: %08x\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 %08x\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: %08x\n",
222 ret
= CertDeleteCertificateFromStore(context
);
223 ok(ret
, "CertDeleteCertificateFromStore failed: %08x\n",
225 /* try deleting a copy */
226 ret
= CertDeleteCertificateFromStore(copy
);
227 ok(ret
, "CertDeleteCertificateFromStore failed: %08x\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: %d\n", GetLastError());
237 ret
= CertCloseStore(store1
, 0);
238 ok(ret
, "CertCloseStore failed: %d\n", GetLastError());
239 ret
= CertCloseStore(store2
, 0);
240 ok(ret
, "CertCloseStore failed: %d\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: %d\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: %08x\n", GetLastError());
252 ok(context
!= NULL
, "Expected a valid cert context\n");
255 ok(context
->cbCertEncoded
== sizeof(bigCert
),
256 "Wrong cert size %d\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: %08x\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 %08x\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: %08x\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: %08x\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 %08x\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 %08x\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: %08x\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: %08x\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: %08x\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 %d\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 %d\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 %d\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 %d\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: %08x\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 %d\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 %d\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: %08x\n", GetLastError());
442 store2
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
443 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
444 ok(store2
!= 0, "CertOpenStore failed: %08x\n", GetLastError());
446 ret
= CertAddEncodedCertificateToStore(store1
, X509_ASN_ENCODING
,
447 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
448 ok(ret
, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
449 ret
= CertAddEncodedCertificateToStore(store2
, X509_ASN_ENCODING
,
450 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
451 ok(ret
, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
452 collection
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
453 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
454 ok(collection
!= 0, "CertOpenStore failed: %08x\n", GetLastError());
456 ret
= CertAddStoreToCollection(collection
, store1
,
457 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
458 ok(ret
, "CertAddStoreToCollection failed: %08x\n", GetLastError());
459 ret
= CertAddStoreToCollection(collection
, store2
,
460 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG
, 0);
461 ok(ret
, "CertAddStoreToCollection failed: %08x\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 %d\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 %d\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: %08x\n",
497 ret = CertDeleteCertificateFromStore(collection, context);
498 printf("ret is %d, GetLastError is %08x\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 %d\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: %08x\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: %08x\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 %d\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 %d\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: %08x\n", GetLastError());
609 CertCloseStore(store
, 0);
611 rc
= RegCreateKeyExA(HKEY_CURRENT_USER
, tempKey
, 0, NULL
, 0, KEY_ALL_ACCESS
,
613 ok(!rc
, "RegCreateKeyExA failed: %d\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: %08x\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: %08x\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: %08x\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: %d\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: %d\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: %d\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 %d 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 %d 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: %08x\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: %d\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: %d\n", rc
);
730 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_RESYNC
, NULL
);
731 ok(ret
, "CertControlStore failed: %08x\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 %d\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: %d\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: %d\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: %d\n", rc
);
787 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_RESYNC
, NULL
);
788 ok(ret
, "CertControlStore failed: %08x\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 %d\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: %d\n", rc
);
808 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_RESYNC
, NULL
);
809 ok(ret
, "CertControlStore failed: %08x\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 %d\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: %08x\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: %d\n", rc
);
839 ok(disp
== REG_OPENED_EXISTING_KEY
,
840 "Expected REG_OPENED_EXISTING_KEY, got %d\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 %08x\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 %08x\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: %08x\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 %08x\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 %08x\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 %08x\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 %08x\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 %08x\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 %08x\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 %08x\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 %08x\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: %d\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 %08x\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: %08x\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: %08x\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 %08x\n", GetLastError());
995 store
= CertOpenStore(CERT_STORE_PROV_SYSTEM
, 0, 0,
996 CERT_SYSTEM_STORE_CURRENT_USER
, BogusW
);
997 ok(store
!= 0, "CertOpenStore failed: %08x\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 const BYTE serializedStoreWithCert
[] = {
1007 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1008 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1009 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1010 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1011 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1012 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1013 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1014 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1015 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1016 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
1017 0x00,0x00,0x00,0x00,0x00,0x00 };
1018 static const BYTE serializedStoreWithCertAndCRL
[] = {
1019 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1020 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1021 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1022 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1023 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1024 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1025 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1026 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1027 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1028 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x21,0x00,0x00,0x00,0x01,0x00,
1029 0x00,0x00,0x47,0x00,0x00,0x00,0x30,0x45,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,
1030 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1031 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1032 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x03,0x11,
1033 0x00,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,
1034 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1036 static void compareFile(LPCWSTR filename
, const BYTE
*pb
, DWORD cb
)
1041 DWORD cbRead
= 0, totalRead
= 0;
1043 h
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
,
1044 FILE_ATTRIBUTE_NORMAL
, NULL
);
1045 if (h
== INVALID_HANDLE_VALUE
)
1048 ret
= ReadFile(h
, buf
, sizeof(buf
), &cbRead
, NULL
);
1051 ok(totalRead
+ cbRead
<= cb
, "Expected total count %d, see %d\n",
1052 cb
, totalRead
+ cbRead
);
1053 ok(!memcmp(pb
+ totalRead
, buf
, cbRead
),
1054 "Unexpected data in file\n");
1055 totalRead
+= cbRead
;
1057 } while (ret
&& cbRead
);
1061 static void testFileStore(void)
1063 static const WCHAR szPrefix
[] = { 'c','e','r',0 };
1064 static const WCHAR szDot
[] = { '.',0 };
1065 WCHAR filename
[MAX_PATH
];
1068 PCCERT_CONTEXT cert
;
1071 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0, 0, NULL
);
1072 ok(!store
&& GetLastError() == ERROR_INVALID_HANDLE
,
1073 "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1075 if (!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
1078 DeleteFileW(filename
);
1079 file
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1080 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1081 if (file
== INVALID_HANDLE_VALUE
)
1084 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0, CERT_STORE_DELETE_FLAG
,
1086 ok(!store
&& GetLastError() == E_INVALIDARG
,
1087 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1088 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1089 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
| CERT_STORE_READONLY_FLAG
, file
);
1090 ok(!store
&& GetLastError() == E_INVALIDARG
,
1091 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1093 /* A "read-only" file store.. */
1094 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1095 CERT_STORE_OPEN_EXISTING_FLAG
| CERT_STORE_READONLY_FLAG
, file
);
1096 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1101 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
1102 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
1103 /* apparently allows adding certificates.. */
1104 ok(ret
, "CertAddEncodedCertificateToStore failed: %d\n", ret
);
1105 /* but not commits.. */
1106 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_COMMIT
, NULL
);
1107 ok(!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
,
1108 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1109 /* It still has certs in memory.. */
1110 cert
= CertEnumCertificatesInStore(store
, NULL
);
1111 ok(cert
!= NULL
, "CertEnumCertificatesInStore failed: %08x\n",
1113 CertFreeCertificateContext(cert
);
1114 /* but the file size is still 0. */
1115 size
= GetFileSize(file
, NULL
);
1116 ok(size
== 0, "Expected size 0, got %d\n", size
);
1117 CertCloseStore(store
, 0);
1120 /* The create new flag is allowed.. */
1121 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1122 CERT_STORE_CREATE_NEW_FLAG
, file
);
1123 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1126 /* but without the commit enable flag, commits don't happen. */
1127 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
1128 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
1129 ok(ret
, "CertAddEncodedCertificateToStore failed: %d\n", ret
);
1130 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_COMMIT
, NULL
);
1131 ok(!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
,
1132 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1133 CertCloseStore(store
, 0);
1135 /* as is the open existing flag. */
1136 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1137 CERT_STORE_OPEN_EXISTING_FLAG
, file
);
1138 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1141 /* but without the commit enable flag, commits don't happen. */
1142 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
1143 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
1144 ok(ret
, "CertAddEncodedCertificateToStore failed: %d\n", ret
);
1145 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_COMMIT
, NULL
);
1146 ok(!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
,
1147 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1148 CertCloseStore(store
, 0);
1150 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1151 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
, file
);
1152 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1156 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
1157 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
1158 ok(ret
, "CertAddEncodedCertificateToStore failed: %08x\n",
1160 /* with commits enabled, commit is allowed */
1161 ret
= CertControlStore(store
, 0, CERT_STORE_CTRL_COMMIT
, NULL
);
1162 ok(ret
, "CertControlStore failed: %d\n", ret
);
1163 compareFile(filename
, serializedStoreWithCert
,
1164 sizeof(serializedStoreWithCert
));
1165 CertCloseStore(store
, 0);
1167 file
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1168 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1169 if (file
== INVALID_HANDLE_VALUE
)
1171 store
= CertOpenStore(CERT_STORE_PROV_FILE
, 0, 0,
1172 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
, file
);
1173 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1177 ret
= CertAddEncodedCRLToStore(store
, X509_ASN_ENCODING
, signedCRL
,
1178 sizeof(signedCRL
), CERT_STORE_ADD_ALWAYS
, NULL
);
1179 ok(ret
, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1180 CertCloseStore(store
, 0);
1181 compareFile(filename
, serializedStoreWithCertAndCRL
,
1182 sizeof(serializedStoreWithCertAndCRL
));
1185 DeleteFileW(filename
);
1188 static void checkFileStoreFailure(LPCWSTR filename
, DWORD dwEncodingType
,
1189 DWORD dwFlags
, DWORD expectedError
)
1191 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
,
1192 dwEncodingType
, 0, dwFlags
, filename
);
1194 ok(!store
&& GetLastError() == expectedError
,
1195 "Expected %08x, got %08x\n", expectedError
, GetLastError());
1198 static BOOL
initFileFromData(LPCWSTR filename
, const BYTE
*pb
, DWORD cb
)
1200 HANDLE file
= CreateFileW(filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
1201 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1204 if (file
!= INVALID_HANDLE_VALUE
)
1208 ret
= WriteFile(file
, pb
, cb
, &written
, NULL
);
1215 static void testFileNameStore(void)
1217 static const WCHAR szPrefix
[] = { 'c','e','r',0 };
1218 static const WCHAR szDot
[] = { '.',0 };
1219 WCHAR filename
[MAX_PATH
];
1223 checkFileStoreFailure(NULL
, 0, 0, ERROR_PATH_NOT_FOUND
);
1225 if (!GetTempFileNameW(szDot
, szPrefix
, 0, filename
))
1227 DeleteFileW(filename
);
1229 /* The two flags are mutually exclusive */
1230 checkFileStoreFailure(filename
, 0,
1231 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
| CERT_STORE_READONLY_FLAG
,
1233 /* Without an encoding type, these all fail */
1234 checkFileStoreFailure(filename
, 0, 0, ERROR_FILE_NOT_FOUND
);
1235 checkFileStoreFailure(filename
, 0, CERT_STORE_OPEN_EXISTING_FLAG
,
1236 ERROR_FILE_NOT_FOUND
);
1237 checkFileStoreFailure(filename
, 0, CERT_STORE_CREATE_NEW_FLAG
,
1238 ERROR_FILE_NOT_FOUND
);
1239 /* Without a message encoding type, these still fail */
1240 checkFileStoreFailure(filename
, X509_ASN_ENCODING
, 0, ERROR_FILE_NOT_FOUND
);
1241 checkFileStoreFailure(filename
, X509_ASN_ENCODING
,
1242 CERT_STORE_OPEN_EXISTING_FLAG
, ERROR_FILE_NOT_FOUND
);
1243 checkFileStoreFailure(filename
, X509_ASN_ENCODING
,
1244 CERT_STORE_CREATE_NEW_FLAG
, ERROR_FILE_NOT_FOUND
);
1245 /* Without a cert encoding type, they still fail */
1246 checkFileStoreFailure(filename
, PKCS_7_ASN_ENCODING
, 0,
1247 ERROR_FILE_NOT_FOUND
);
1248 checkFileStoreFailure(filename
, PKCS_7_ASN_ENCODING
,
1249 CERT_STORE_OPEN_EXISTING_FLAG
, ERROR_FILE_NOT_FOUND
);
1250 checkFileStoreFailure(filename
, PKCS_7_ASN_ENCODING
,
1251 CERT_STORE_CREATE_NEW_FLAG
, ERROR_FILE_NOT_FOUND
);
1252 /* With both a message and cert encoding type, but without commit enabled,
1255 checkFileStoreFailure(filename
, X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
, 0,
1256 ERROR_FILE_NOT_FOUND
);
1257 checkFileStoreFailure(filename
, X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1258 CERT_STORE_OPEN_EXISTING_FLAG
, ERROR_FILE_NOT_FOUND
);
1259 checkFileStoreFailure(filename
, X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1260 CERT_STORE_CREATE_NEW_FLAG
, ERROR_FILE_NOT_FOUND
);
1262 /* In all of the following tests, the encoding type seems to be ignored */
1263 if (initFileFromData(filename
, bigCert
, sizeof(bigCert
)))
1265 PCCERT_CONTEXT cert
;
1268 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1269 CERT_STORE_READONLY_FLAG
, filename
);
1270 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1272 cert
= CertEnumCertificatesInStore(store
, NULL
);
1273 todo_wine
ok(cert
!= NULL
, "CertEnumCertificatesInStore failed: %08x\n",
1275 cert
= CertEnumCertificatesInStore(store
, cert
);
1276 ok(!cert
, "Expected only one cert\n");
1277 crl
= CertEnumCRLsInStore(store
, NULL
);
1278 ok(!crl
, "Expected no CRLs\n");
1280 CertCloseStore(store
, 0);
1281 DeleteFileW(filename
);
1283 if (initFileFromData(filename
, serializedStoreWithCert
,
1284 sizeof(serializedStoreWithCert
)))
1286 PCCERT_CONTEXT cert
;
1289 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1290 CERT_STORE_READONLY_FLAG
, filename
);
1291 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1293 cert
= CertEnumCertificatesInStore(store
, NULL
);
1294 ok(cert
!= NULL
, "CertEnumCertificatesInStore failed: %08x\n",
1296 cert
= CertEnumCertificatesInStore(store
, cert
);
1297 ok(!cert
, "Expected only one cert\n");
1298 crl
= CertEnumCRLsInStore(store
, NULL
);
1299 ok(!crl
, "Expected no CRLs\n");
1301 CertCloseStore(store
, 0);
1302 DeleteFileW(filename
);
1304 if (initFileFromData(filename
, serializedStoreWithCertAndCRL
,
1305 sizeof(serializedStoreWithCertAndCRL
)))
1307 PCCERT_CONTEXT cert
;
1310 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1311 CERT_STORE_READONLY_FLAG
, filename
);
1312 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1314 cert
= CertEnumCertificatesInStore(store
, NULL
);
1315 ok(cert
!= NULL
, "CertEnumCertificatesInStore failed: %08x\n",
1317 cert
= CertEnumCertificatesInStore(store
, cert
);
1318 ok(!cert
, "Expected only one cert\n");
1319 crl
= CertEnumCRLsInStore(store
, NULL
);
1320 ok(crl
!= NULL
, "CertEnumCRLsInStore failed: %08x\n", GetLastError());
1321 crl
= CertEnumCRLsInStore(store
, crl
);
1322 ok(!crl
, "Expected only one CRL\n");
1324 CertCloseStore(store
, 0);
1325 /* Don't delete it this time, the next test uses it */
1327 /* Now that the file exists, we can open it read-only */
1328 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1329 CERT_STORE_READONLY_FLAG
, filename
);
1330 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1331 CertCloseStore(store
, 0);
1332 DeleteFileW(filename
);
1334 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1335 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
| CERT_STORE_CREATE_NEW_FLAG
, filename
);
1336 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1339 ret
= CertAddEncodedCertificateToStore(store
, X509_ASN_ENCODING
,
1340 bigCert
, sizeof(bigCert
), CERT_STORE_ADD_ALWAYS
, NULL
);
1341 ok(ret
, "CertAddEncodedCertificateToStore failed: %08x\n",
1343 CertCloseStore(store
, 0);
1344 compareFile(filename
, serializedStoreWithCert
,
1345 sizeof(serializedStoreWithCert
));
1347 store
= CertOpenStore(CERT_STORE_PROV_FILENAME_W
, 0, 0,
1348 CERT_FILE_STORE_COMMIT_ENABLE_FLAG
, filename
);
1349 ok(store
!= NULL
, "CertOpenStore failed: %08x\n", GetLastError());
1352 ret
= CertAddEncodedCRLToStore(store
, X509_ASN_ENCODING
,
1353 signedCRL
, sizeof(signedCRL
), CERT_STORE_ADD_ALWAYS
, NULL
);
1354 ok(ret
, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1355 CertCloseStore(store
, 0);
1356 compareFile(filename
, serializedStoreWithCertAndCRL
,
1357 sizeof(serializedStoreWithCertAndCRL
));
1359 DeleteFileW(filename
);
1362 static void testCertOpenSystemStore(void)
1366 store
= CertOpenSystemStoreW(0, NULL
);
1367 ok(!store
&& GetLastError() == E_INVALIDARG
,
1368 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1369 /* This succeeds, and on WinXP at least, the Bogus key is created under
1370 * HKCU (but not under HKLM, even when run as an administrator.)
1372 store
= CertOpenSystemStoreW(0, BogusW
);
1373 ok(store
!= 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
1375 CertCloseStore(store
, 0);
1376 /* Delete it so other tests succeed next time around */
1377 store
= CertOpenStore(CERT_STORE_PROV_SYSTEM
, 0, 0,
1378 CERT_SYSTEM_STORE_CURRENT_USER
| CERT_STORE_DELETE_FLAG
, BogusW
);
1379 RegDeleteKeyW(HKEY_CURRENT_USER
, BogusPathW
);
1382 static void testAddSerialized(void)
1386 BYTE buf
[sizeof(struct CertPropIDHeader
) * 2 + 20 + sizeof(bigCert
)] =
1389 struct CertPropIDHeader
*hdr
;
1390 PCCERT_CONTEXT context
;
1392 ret
= CertAddSerializedElementToStore(0, NULL
, 0, 0, 0, 0, NULL
, NULL
);
1393 ok(!ret
&& GetLastError() == ERROR_END_OF_MEDIA
,
1394 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1396 store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1397 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1398 ok(store
!= 0, "CertOpenStore failed: %08x\n", GetLastError());
1400 ret
= CertAddSerializedElementToStore(store
, NULL
, 0, 0, 0, 0, NULL
, NULL
);
1401 ok(!ret
&& GetLastError() == ERROR_END_OF_MEDIA
,
1402 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1404 /* Test with an empty property */
1405 hdr
= (struct CertPropIDHeader
*)buf
;
1406 hdr
->propID
= CERT_CERT_PROP_ID
;
1409 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
), 0, 0, 0,
1411 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1412 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1413 /* Test with a bad size in property header */
1414 hdr
->cb
= sizeof(bigCert
) - 1;
1415 memcpy(buf
+ sizeof(struct CertPropIDHeader
), bigCert
, sizeof(bigCert
));
1416 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
), 0, 0, 0,
1418 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1419 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1420 ret
= CertAddSerializedElementToStore(store
, buf
,
1421 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), 0, 0, 0, NULL
,
1423 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1424 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1425 ret
= CertAddSerializedElementToStore(store
, buf
,
1426 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), CERT_STORE_ADD_NEW
,
1428 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1429 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1430 /* Kosher size in property header, but no context type */
1431 hdr
->cb
= sizeof(bigCert
);
1432 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
), 0, 0, 0,
1434 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1435 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1436 ret
= CertAddSerializedElementToStore(store
, buf
,
1437 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), 0, 0, 0, NULL
,
1439 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1440 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1441 ret
= CertAddSerializedElementToStore(store
, buf
,
1442 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), CERT_STORE_ADD_NEW
,
1444 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1445 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1446 /* With a bad context type */
1447 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
), 0, 0,
1448 CERT_STORE_CRL_CONTEXT_FLAG
, NULL
, NULL
);
1449 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1450 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1451 ret
= CertAddSerializedElementToStore(store
, buf
,
1452 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), 0, 0,
1453 CERT_STORE_CRL_CONTEXT_FLAG
, NULL
, NULL
);
1454 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1455 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1456 ret
= CertAddSerializedElementToStore(store
, buf
,
1457 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), CERT_STORE_ADD_NEW
,
1458 0, CERT_STORE_CRL_CONTEXT_FLAG
, NULL
, NULL
);
1459 ok(!ret
&& GetLastError() == E_INVALIDARG
,
1460 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1461 /* Bad unknown field, good type */
1463 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
), 0, 0,
1464 CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
, NULL
);
1465 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
1466 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1467 ret
= CertAddSerializedElementToStore(store
, buf
,
1468 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), 0, 0,
1469 CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
, NULL
);
1470 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
1471 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1472 ret
= CertAddSerializedElementToStore(store
, buf
,
1473 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), CERT_STORE_ADD_NEW
,
1474 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
, NULL
);
1475 ok(!ret
&& GetLastError() == ERROR_FILE_NOT_FOUND
,
1476 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1477 /* Most everything okay, but bad add disposition */
1480 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
1481 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1483 ret = CertAddSerializedElementToStore(store, buf,
1484 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
1485 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1487 /* Everything okay, but buffer's too big */
1488 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
),
1489 CERT_STORE_ADD_NEW
, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
, NULL
);
1490 ok(ret
, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1491 /* Everything okay, check it's not re-added */
1492 ret
= CertAddSerializedElementToStore(store
, buf
,
1493 sizeof(struct CertPropIDHeader
) + sizeof(bigCert
), CERT_STORE_ADD_NEW
,
1494 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
, NULL
);
1495 ok(!ret
&& GetLastError() == CRYPT_E_EXISTS
,
1496 "Expected CRYPT_E_EXISTS, got %08x\n", GetLastError());
1498 context
= CertEnumCertificatesInStore(store
, NULL
);
1499 ok(context
!= NULL
, "Expected a cert\n");
1501 CertDeleteCertificateFromStore(context
);
1503 /* Try adding with a bogus hash. Oddly enough, it succeeds, and the hash,
1504 * when queried, is the real hash rather than the bogus hash.
1506 hdr
= (struct CertPropIDHeader
*)(buf
+ sizeof(struct CertPropIDHeader
) +
1508 hdr
->propID
= CERT_HASH_PROP_ID
;
1510 hdr
->cb
= sizeof(hash
);
1511 memset(hash
, 0xc, sizeof(hash
));
1512 memcpy((LPBYTE
)hdr
+ sizeof(struct CertPropIDHeader
), hash
, sizeof(hash
));
1513 ret
= CertAddSerializedElementToStore(store
, buf
, sizeof(buf
),
1514 CERT_STORE_ADD_NEW
, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG
, NULL
,
1515 (const void **)&context
);
1516 ok(ret
, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1519 BYTE hashVal
[20], realHash
[20];
1520 DWORD size
= sizeof(hashVal
);
1522 ret
= CryptHashCertificate(0, 0, 0, bigCert
, sizeof(bigCert
),
1524 ok(ret
, "CryptHashCertificate failed: %08x\n", GetLastError());
1525 ret
= CertGetCertificateContextProperty(context
, CERT_HASH_PROP_ID
,
1527 ok(ret
, "CertGetCertificateContextProperty failed: %08x\n",
1529 ok(!memcmp(hashVal
, realHash
, size
), "Unexpected hash\n");
1530 CertFreeCertificateContext(context
);
1533 CertCloseStore(store
, 0);
1538 /* various combinations of CertOpenStore */
1540 testCollectionStore();
1542 testSystemRegStore();
1545 testFileNameStore();
1547 testCertOpenSystemStore();
1549 testAddSerialized();