crypt32: Correct reference counting when deleting contexts from collections.
[wine] / dlls / crypt32 / tests / store.c
1 /*
2  * crypt32 cert store function tests
3  *
4  * Copyright 2005-2006 Juan Lang
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <winreg.h>
27 #include <winerror.h>
28 #include <wincrypt.h>
29
30 #include "wine/test.h"
31
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
36
37 struct CertPropIDHeader
38 {
39     DWORD propID;
40     DWORD unknown1;
41     DWORD cb;
42 };
43
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,
79  0x01, 0x01 };
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 };
96
97
98 static BOOL (WINAPI *pCertAddStoreToCollection)(HCERTSTORE,HCERTSTORE,DWORD,DWORD);
99 static BOOL (WINAPI *pCertControlStore)(HCERTSTORE,DWORD,DWORD,void const*);
100 static PCCRL_CONTEXT (WINAPI *pCertEnumCRLsInStore)(HCERTSTORE,PCCRL_CONTEXT);
101 static BOOL (WINAPI *pCertEnumSystemStore)(DWORD,void*,void*,PFN_CERT_ENUM_SYSTEM_STORE);
102 static BOOL (WINAPI *pCertGetStoreProperty)(HCERTSTORE,DWORD,void*,DWORD*);
103 static void (WINAPI *pCertRemoveStoreFromCollection)(HCERTSTORE,HCERTSTORE);
104 static BOOL (WINAPI *pCertSetStoreProperty)(HCERTSTORE,DWORD,DWORD,const void*);
105
106 static void testMemStore(void)
107 {
108     HCERTSTORE store1, store2;
109     PCCERT_CONTEXT context;
110     BOOL ret;
111     DWORD GLE;
112
113     /* NULL provider */
114     store1 = CertOpenStore(0, 0, 0, 0, NULL);
115     ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
116      "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
117     /* weird flags */
118     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
119      CERT_STORE_DELETE_FLAG, NULL);
120     ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
121      "Expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
122
123     /* normal */
124     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
125      CERT_STORE_CREATE_NEW_FLAG, NULL);
126     ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
127     /* open existing doesn't */
128     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
129      CERT_STORE_OPEN_EXISTING_FLAG, NULL);
130     ok(store2 != NULL, "CertOpenStore failed: %d\n", GetLastError());
131     ok(store1 != store2, "Expected different stores\n");
132
133     /* add a bogus (empty) cert */
134     context = NULL;
135     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
136      sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
137     /* Windows returns CRYPT_E_ASN1_EOD or OSS_DATA_ERROR, but accept
138      * CRYPT_E_ASN1_CORRUPT as well (because matching errors is tough in this
139      * case)
140      */
141     GLE = GetLastError();
142     ok(!ret && (GLE == CRYPT_E_ASN1_EOD || GLE == CRYPT_E_ASN1_CORRUPT ||
143      GLE == OSS_DATA_ERROR),
144      "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
145      GLE);
146     /* add a "signed" cert--the signature isn't a real signature, so this adds
147      * without any check of the signature's validity
148      */
149     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
150      signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
151     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
152     ok(context != NULL, "Expected a valid cert context\n");
153     if (context)
154     {
155         ok(context->cbCertEncoded == sizeof(signedBigCert),
156          "Wrong cert size %d\n", context->cbCertEncoded);
157         ok(!memcmp(context->pbCertEncoded, signedBigCert,
158          sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
159         /* remove it, the rest of the tests will work on an unsigned cert */
160         ret = CertDeleteCertificateFromStore(context);
161         ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
162          GetLastError());
163     }
164     /* try adding a "signed" CRL as a cert */
165     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
166      signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
167     GLE = GetLastError();
168     ok(!ret && (GLE == CRYPT_E_ASN1_BADTAG || GLE == CRYPT_E_ASN1_CORRUPT ||
169      GLE == OSS_DATA_ERROR),
170      "Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
171      GLE);
172     /* add a cert to store1 */
173     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
174      sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
175     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
176     ok(context != NULL, "Expected a valid cert context\n");
177     if (context)
178     {
179         DWORD size;
180         BYTE *buf;
181
182         ok(context->cbCertEncoded == sizeof(bigCert),
183          "Wrong cert size %d\n", context->cbCertEncoded);
184         ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
185          "Unexpected encoded cert in context\n");
186         ok(context->hCertStore == store1, "Unexpected store\n");
187
188         /* check serializing this element */
189         /* These crash
190         ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
191         ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
192         ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
193          */
194         /* apparently flags are ignored */
195         ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
196         ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
197          GetLastError());
198         buf = HeapAlloc(GetProcessHeap(), 0, size);
199         if (buf)
200         {
201             ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
202             ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
203             ok(!memcmp(serializedCert, buf, size),
204              "Unexpected serialized cert\n");
205             HeapFree(GetProcessHeap(), 0, buf);
206         }
207
208         ret = CertFreeCertificateContext(context);
209         ok(ret, "CertFreeCertificateContext failed: %08x\n", GetLastError());
210     }
211     /* verify the cert's in store1 */
212     context = CertEnumCertificatesInStore(store1, NULL);
213     ok(context != NULL, "Expected a valid context\n");
214     context = CertEnumCertificatesInStore(store1, context);
215     ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
216      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
217     /* verify store2 (the "open existing" mem store) is still empty */
218     context = CertEnumCertificatesInStore(store2, NULL);
219     ok(!context, "Expected an empty store\n");
220     /* delete the cert from store1, and check it's empty */
221     context = CertEnumCertificatesInStore(store1, NULL);
222     if (context)
223     {
224         /* Deleting a bitwise copy crashes with an access to an uninitialized
225          * pointer, so a cert context has some special data out there in memory
226          * someplace
227         CERT_CONTEXT copy;
228         memcpy(&copy, context, sizeof(copy));
229         ret = CertDeleteCertificateFromStore(&copy);
230          */
231         PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
232
233         ok(copy != NULL, "CertDuplicateCertificateContext failed: %08x\n",
234          GetLastError());
235         ret = CertDeleteCertificateFromStore(context);
236         ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
237          GetLastError());
238         /* try deleting a copy */
239         ret = CertDeleteCertificateFromStore(copy);
240         todo_wine
241         ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
242          GetLastError());
243         /* check that the store is empty */
244         context = CertEnumCertificatesInStore(store1, NULL);
245         ok(!context, "Expected an empty store\n");
246     }
247
248     /* close an empty store */
249     ret = CertCloseStore(NULL, 0);
250     ok(ret, "CertCloseStore failed: %d\n", GetLastError());
251     ret = CertCloseStore(store1, 0);
252     ok(ret, "CertCloseStore failed: %d\n", GetLastError());
253     ret = CertCloseStore(store2, 0);
254     ok(ret, "CertCloseStore failed: %d\n", GetLastError());
255
256     /* This seems nonsensical, but you can open a read-only mem store, only
257      * it isn't read-only
258      */
259     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
260      CERT_STORE_READONLY_FLAG, NULL);
261     ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
262     /* yep, this succeeds */
263     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
264      sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
265     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
266     ok(context != NULL, "Expected a valid cert context\n");
267     if (context)
268     {
269         ok(context->cbCertEncoded == sizeof(bigCert),
270          "Wrong cert size %d\n", context->cbCertEncoded);
271         ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
272          "Unexpected encoded cert in context\n");
273         ok(context->hCertStore == store1, "Unexpected store\n");
274         ret = CertDeleteCertificateFromStore(context);
275         ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
276          GetLastError());
277     }
278     CertCloseStore(store1, 0);
279 }
280
281 static void testCollectionStore(void)
282 {
283     HCERTSTORE store1, store2, collection, collection2;
284     PCCERT_CONTEXT context;
285     BOOL ret;
286
287     if (!pCertAddStoreToCollection)
288     {
289         win_skip("CertAddStoreToCollection() is not available\n");
290         return;
291     }
292
293     collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
294      CERT_STORE_CREATE_NEW_FLAG, NULL);
295
296     /* Try adding a cert to any empty collection */
297     ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
298      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
299     ok(!ret && GetLastError() == E_ACCESSDENIED,
300      "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
301
302     /* Create and add a cert to a memory store */
303     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
304      CERT_STORE_CREATE_NEW_FLAG, NULL);
305     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
306      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
307     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
308     /* Add the memory store to the collection, without allowing adding */
309     ret = pCertAddStoreToCollection(collection, store1, 0, 0);
310     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
311     /* Verify the cert is in the collection */
312     context = CertEnumCertificatesInStore(collection, NULL);
313     ok(context != NULL, "Expected a valid context\n");
314     if (context)
315     {
316         ok(context->hCertStore == collection, "Unexpected store\n");
317         CertFreeCertificateContext(context);
318     }
319     /* Check that adding to the collection isn't allowed */
320     ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
321      bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
322     ok(!ret && GetLastError() == E_ACCESSDENIED,
323      "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
324
325     /* Create a new memory store */
326     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
327      CERT_STORE_CREATE_NEW_FLAG, NULL);
328     /* Try adding a store to a non-collection store */
329     ret = pCertAddStoreToCollection(store1, store2,
330      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
331     ok(!ret && GetLastError() == E_INVALIDARG,
332      "Expected E_INVALIDARG, got %08x\n", GetLastError());
333     /* Try adding some bogus stores */
334     /* This crashes in Windows
335     ret = pCertAddStoreToCollection(0, store2,
336      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
337      */
338     /* This "succeeds"... */
339     ret = pCertAddStoreToCollection(collection, 0,
340      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
341     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
342     /* while this crashes.
343     ret = pCertAddStoreToCollection(collection, 1,
344      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
345      */
346
347     /* Add it to the collection, this time allowing adding */
348     ret = pCertAddStoreToCollection(collection, store2,
349      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
350     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
351     /* Check that adding to the collection is allowed */
352     ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
353      bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
354     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
355     /* Now check that it was actually added to store2 */
356     context = CertEnumCertificatesInStore(store2, NULL);
357     ok(context != NULL, "Expected a valid context\n");
358     if (context)
359     {
360         ok(context->hCertStore == store2, "Unexpected store\n");
361         CertFreeCertificateContext(context);
362     }
363     /* Check that the collection has both bigCert and bigCert2.  bigCert comes
364      * first because store1 was added first.
365      */
366     context = CertEnumCertificatesInStore(collection, NULL);
367     ok(context != NULL, "Expected a valid context\n");
368     if (context)
369     {
370         ok(context->hCertStore == collection, "Unexpected store\n");
371         ok(context->cbCertEncoded == sizeof(bigCert),
372          "Wrong size %d\n", context->cbCertEncoded);
373         ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
374          "Unexpected cert\n");
375         context = CertEnumCertificatesInStore(collection, context);
376         ok(context != NULL, "Expected a valid context\n");
377         if (context)
378         {
379             ok(context->hCertStore == collection, "Unexpected store\n");
380             ok(context->cbCertEncoded == sizeof(bigCert2),
381              "Wrong size %d\n", context->cbCertEncoded);
382             ok(!memcmp(context->pbCertEncoded, bigCert2,
383              context->cbCertEncoded), "Unexpected cert\n");
384             context = CertEnumCertificatesInStore(collection, context);
385             ok(!context, "Unexpected cert\n");
386         }
387     }
388     /* close store2, and check that the collection is unmodified */
389     CertCloseStore(store2, 0);
390     context = CertEnumCertificatesInStore(collection, NULL);
391     ok(context != NULL, "Expected a valid context\n");
392     if (context)
393     {
394         ok(context->hCertStore == collection, "Unexpected store\n");
395         ok(context->cbCertEncoded == sizeof(bigCert),
396          "Wrong size %d\n", context->cbCertEncoded);
397         ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
398          "Unexpected cert\n");
399         context = CertEnumCertificatesInStore(collection, context);
400         ok(context != NULL, "Expected a valid context\n");
401         if (context)
402         {
403             ok(context->hCertStore == collection, "Unexpected store\n");
404             ok(context->cbCertEncoded == sizeof(bigCert2),
405              "Wrong size %d\n", context->cbCertEncoded);
406             ok(!memcmp(context->pbCertEncoded, bigCert2,
407              context->cbCertEncoded), "Unexpected cert\n");
408             context = CertEnumCertificatesInStore(collection, context);
409             ok(!context, "Unexpected cert\n");
410         }
411     }
412
413     /* Adding a collection to a collection is legal */
414     collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
415      CERT_STORE_CREATE_NEW_FLAG, NULL);
416     ret = pCertAddStoreToCollection(collection2, collection,
417      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
418     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
419     /* check the contents of collection2 */
420     context = CertEnumCertificatesInStore(collection2, NULL);
421     ok(context != NULL, "Expected a valid context\n");
422     if (context)
423     {
424         ok(context->hCertStore == collection2, "Unexpected store\n");
425         ok(context->cbCertEncoded == sizeof(bigCert),
426          "Wrong size %d\n", context->cbCertEncoded);
427         ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
428          "Unexpected cert\n");
429         context = CertEnumCertificatesInStore(collection2, context);
430         ok(context != NULL, "Expected a valid context\n");
431         if (context)
432         {
433             ok(context->hCertStore == collection2, "Unexpected store\n");
434             ok(context->cbCertEncoded == sizeof(bigCert2),
435              "Wrong size %d\n", context->cbCertEncoded);
436             ok(!memcmp(context->pbCertEncoded, bigCert2,
437              context->cbCertEncoded), "Unexpected cert\n");
438             context = CertEnumCertificatesInStore(collection2, context);
439             ok(!context, "Unexpected cert\n");
440         }
441     }
442
443     /* I'd like to test closing the collection in the middle of enumeration,
444      * but my tests have been inconsistent.  The first time calling
445      * CertEnumCertificatesInStore on a closed collection succeeded, while the
446      * second crashed.  So anything appears to be fair game.
447      * I'd also like to test removing a store from a collection in the middle
448      * of an enumeration, but my tests in Windows have been inconclusive.
449      * In one scenario it worked.  In another scenario, about a third of the
450      * time this leads to "random" crashes elsewhere in the code.  This
451      * probably means this is not allowed.
452      */
453
454     CertCloseStore(store1, 0);
455     CertCloseStore(collection, 0);
456     CertCloseStore(collection2, 0);
457
458     /* Add the same cert to two memory stores, then put them in a collection */
459     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
460      CERT_STORE_CREATE_NEW_FLAG, NULL);
461     ok(store1 != 0, "CertOpenStore failed: %08x\n", GetLastError());
462     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
463      CERT_STORE_CREATE_NEW_FLAG, NULL);
464     ok(store2 != 0, "CertOpenStore failed: %08x\n", GetLastError());
465
466     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
467      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
468     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
469     ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
470      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
471     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
472     collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
473      CERT_STORE_CREATE_NEW_FLAG, NULL);
474     ok(collection != 0, "CertOpenStore failed: %08x\n", GetLastError());
475
476     ret = pCertAddStoreToCollection(collection, store1,
477      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
478     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
479     ret = pCertAddStoreToCollection(collection, store2,
480      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
481     ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
482
483     /* Check that the collection has two copies of the same cert */
484     context = CertEnumCertificatesInStore(collection, NULL);
485     ok(context != NULL, "Expected a valid context\n");
486     if (context)
487     {
488         ok(context->hCertStore == collection, "Unexpected store\n");
489         ok(context->cbCertEncoded == sizeof(bigCert),
490          "Wrong size %d\n", context->cbCertEncoded);
491         ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
492          "Unexpected cert\n");
493         context = CertEnumCertificatesInStore(collection, context);
494         ok(context != NULL, "Expected a valid context\n");
495         if (context)
496         {
497             ok(context->hCertStore == collection, "Unexpected store\n");
498             ok(context->cbCertEncoded == sizeof(bigCert),
499              "Wrong size %d\n", context->cbCertEncoded);
500             ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
501              "Unexpected cert\n");
502             context = CertEnumCertificatesInStore(collection, context);
503             ok(context == NULL, "Unexpected cert\n");
504         }
505     }
506
507     /* The following would check whether I can delete an identical cert, rather
508      * than one enumerated from the store.  It crashes, so that means I must
509      * only call CertDeleteCertificateFromStore with contexts enumerated from
510      * the store.
511     context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
512      sizeof(bigCert));
513     ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
514      GetLastError());
515     if (context)
516     {
517         ret = CertDeleteCertificateFromStore(collection, context);
518         printf("ret is %d, GetLastError is %08x\n", ret, GetLastError());
519         CertFreeCertificateContext(context);
520     }
521      */
522
523     /* Now check deleting from the collection. */
524     context = CertEnumCertificatesInStore(collection, NULL);
525     ok(context != NULL, "Expected a valid context\n");
526     if (context)
527     {
528         CertDeleteCertificateFromStore(context);
529         /* store1 should now be empty */
530         context = CertEnumCertificatesInStore(store1, NULL);
531         ok(!context, "Unexpected cert\n");
532         /* and there should be one certificate in the collection */
533         context = CertEnumCertificatesInStore(collection, NULL);
534         ok(context != NULL, "Expected a valid cert\n");
535         if (context)
536         {
537             ok(context->hCertStore == collection, "Unexpected store\n");
538             ok(context->cbCertEncoded == sizeof(bigCert),
539              "Wrong size %d\n", context->cbCertEncoded);
540             ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
541              "Unexpected cert\n");
542         }
543         context = CertEnumCertificatesInStore(collection, context);
544         ok(context == NULL, "Unexpected cert\n");
545     }
546
547     if (!pCertRemoveStoreFromCollection)
548     {
549         win_skip("CertRemoveStoreFromCollection() is not available\n");
550     }
551     else
552     {
553         /* Finally, test removing stores from the collection.  No return
554          *  value, so it's a bit funny to test.
555          */
556         /* This crashes
557          * pCertRemoveStoreFromCollection(NULL, NULL);
558          */
559         /* This "succeeds," no crash, no last error set */
560         SetLastError(0xdeadbeef);
561         pCertRemoveStoreFromCollection(store2, collection);
562         ok(GetLastError() == 0xdeadbeef,
563            "Didn't expect an error to be set: %08x\n", GetLastError());
564
565         /* After removing store2, the collection should be empty */
566         SetLastError(0xdeadbeef);
567         pCertRemoveStoreFromCollection(collection, store2);
568         ok(GetLastError() == 0xdeadbeef,
569            "Didn't expect an error to be set: %08x\n", GetLastError());
570         context = CertEnumCertificatesInStore(collection, NULL);
571         ok(!context, "Unexpected cert\n");
572     }
573
574     CertCloseStore(collection, 0);
575     CertCloseStore(store2, 0);
576     CertCloseStore(store1, 0);
577
578     /* Test adding certificates to and deleting certificates from collections.
579      */
580     store1 = CertOpenSystemStoreA(0, "My");
581     collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
582      CERT_STORE_CREATE_NEW_FLAG, NULL);
583
584     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
585      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
586     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
587     CertDeleteCertificateFromStore(context);
588
589     CertAddStoreToCollection(collection, store1,
590      CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
591
592     ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
593      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
594     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
595     CertDeleteCertificateFromStore(context);
596
597     CertCloseStore(collection, 0);
598     CertCloseStore(store1, 0);
599 }
600
601 /* Looks for the property with ID propID in the buffer buf.  Returns a pointer
602  * to its header if found, NULL if not.
603  */
604 static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
605  DWORD propID)
606 {
607     const struct CertPropIDHeader *ret = NULL;
608     BOOL failed = FALSE;
609
610     while (size && !ret && !failed)
611     {
612         if (size < sizeof(struct CertPropIDHeader))
613             failed = TRUE;
614         else
615         {
616             const struct CertPropIDHeader *hdr =
617              (const struct CertPropIDHeader *)buf;
618
619             size -= sizeof(struct CertPropIDHeader);
620             buf += sizeof(struct CertPropIDHeader);
621             if (size < hdr->cb)
622                 failed = TRUE;
623             else if (hdr->propID == propID)
624                 ret = hdr;
625             else
626             {
627                 buf += hdr->cb;
628                 size -= hdr->cb;
629             }
630         }
631     }
632     return ret;
633 }
634
635 typedef DWORD (WINAPI *SHDeleteKeyAFunc)(HKEY, LPCSTR);
636
637 static void testRegStore(void)
638 {
639     static const char tempKey[] = "Software\\Wine\\CryptTemp";
640     HCERTSTORE store;
641     LONG rc;
642     HKEY key = NULL;
643     DWORD disp, GLE;
644
645     store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
646     GLE = GetLastError();
647     ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
648      "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
649     store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
650     GLE = GetLastError();
651     ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
652      "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
653
654     /* Opening up any old key works.. */
655     key = HKEY_CURRENT_USER;
656     store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
657     /* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
658      */
659     todo_wine ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
660     CertCloseStore(store, 0);
661
662     /* It looks like the remainder pretty much needs CertControlStore() */
663     if (!pCertControlStore)
664     {
665         win_skip("CertControlStore() is not available\n");
666         return;
667     }
668
669     rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
670      NULL, &key, NULL);
671     ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
672     if (key)
673     {
674         BOOL ret;
675         BYTE hash[20];
676         DWORD size, i;
677         static const char certificates[] = "Certificates\\";
678         char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
679         HKEY subKey;
680         PCCERT_CONTEXT context;
681
682         store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
683         ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
684         /* Add a certificate.  It isn't persisted right away, since it's only
685          * added to the cache..
686          */
687         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
688          bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
689         ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
690          GetLastError());
691         /* so flush the cache to force a commit.. */
692         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
693         ok(ret, "CertControlStore failed: %08x\n", GetLastError());
694         /* and check that the expected subkey was written. */
695         size = sizeof(hash);
696         ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2),
697          hash, &size);
698         ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
699         strcpy(subKeyName, certificates);
700         for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
701          i++, ptr += 2)
702             sprintf(ptr, "%02X", hash[i]);
703         rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
704          &subKey, NULL);
705         ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
706         if (subKey)
707         {
708             LPBYTE buf;
709
710             size = 0;
711             RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
712             buf = HeapAlloc(GetProcessHeap(), 0, size);
713             if (buf)
714             {
715                 rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
716                 ok(!rc, "RegQueryValueExA failed: %d\n", rc);
717                 if (!rc)
718                 {
719                     const struct CertPropIDHeader *hdr;
720
721                     /* Both the hash and the cert should be present */
722                     hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
723                     ok(hdr != NULL, "Expected to find a cert property\n");
724                     if (hdr)
725                     {
726                         ok(hdr->cb == sizeof(bigCert2),
727                            "Wrong size %d of cert property\n", hdr->cb);
728                         ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), bigCert2,
729                          hdr->cb), "Unexpected cert in cert property\n");
730                     }
731                     hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
732                     ok(hdr != NULL, "Expected to find a hash property\n");
733                     if (hdr)
734                     {
735                         ok(hdr->cb == sizeof(hash),
736                            "Wrong size %d of hash property\n", hdr->cb);
737                         ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), hash,
738                          hdr->cb), "Unexpected hash in cert property\n");
739                     }
740                 }
741                 HeapFree(GetProcessHeap(), 0, buf);
742             }
743             RegCloseKey(subKey);
744         }
745
746         /* Remove the existing context */
747         context = CertEnumCertificatesInStore(store, NULL);
748         ok(context != NULL, "Expected a cert context\n");
749         if (context)
750             CertDeleteCertificateFromStore(context);
751         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
752         ok(ret, "CertControlStore failed: %08x\n", GetLastError());
753
754         /* Add a serialized cert with a bogus hash directly to the registry */
755         memset(hash, 0, sizeof(hash));
756         strcpy(subKeyName, certificates);
757         for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
758          i < sizeof(hash); i++, ptr += 2)
759             sprintf(ptr, "%02X", hash[i]);
760         rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
761          &subKey, NULL);
762         ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
763         if (subKey)
764         {
765             BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
766              sizeof(bigCert)], *ptr;
767             DWORD certCount = 0;
768             struct CertPropIDHeader *hdr;
769
770             hdr = (struct CertPropIDHeader *)buf;
771             hdr->propID = CERT_HASH_PROP_ID;
772             hdr->unknown1 = 1;
773             hdr->cb = sizeof(hash);
774             ptr = buf + sizeof(*hdr);
775             memcpy(ptr, hash, sizeof(hash));
776             ptr += sizeof(hash);
777             hdr = (struct CertPropIDHeader *)ptr;
778             hdr->propID = CERT_CERT_PROP_ID;
779             hdr->unknown1 = 1;
780             hdr->cb = sizeof(bigCert);
781             ptr += sizeof(*hdr);
782             memcpy(ptr, bigCert, sizeof(bigCert));
783
784             rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
785              sizeof(buf));
786             ok(!rc, "RegSetValueExA failed: %d\n", rc);
787
788             ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
789             ok(ret, "CertControlStore failed: %08x\n", GetLastError());
790
791             /* Make sure the bogus hash cert gets loaded. */
792             certCount = 0;
793             context = NULL;
794             do {
795                 context = CertEnumCertificatesInStore(store, context);
796                 if (context)
797                     certCount++;
798             } while (context != NULL);
799             ok(certCount == 1, "Expected 1 certificates, got %d\n", certCount);
800
801             RegCloseKey(subKey);
802         }
803
804         /* Add another serialized cert directly to the registry, this time
805          * under the correct key name (named with the correct hash value).
806          */
807         size = sizeof(hash);
808         ret = CryptHashCertificate(0, 0, 0, bigCert2,
809          sizeof(bigCert2), hash, &size);
810         ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
811         strcpy(subKeyName, certificates);
812         for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
813          i < sizeof(hash); i++, ptr += 2)
814             sprintf(ptr, "%02X", hash[i]);
815         rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
816          &subKey, NULL);
817         ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
818         if (subKey)
819         {
820             BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
821              sizeof(bigCert2)], *ptr;
822             DWORD certCount = 0;
823             PCCERT_CONTEXT context;
824             struct CertPropIDHeader *hdr;
825
826             /* First try with a bogus hash... */
827             hdr = (struct CertPropIDHeader *)buf;
828             hdr->propID = CERT_HASH_PROP_ID;
829             hdr->unknown1 = 1;
830             hdr->cb = sizeof(hash);
831             ptr = buf + sizeof(*hdr);
832             memset(ptr, 0, sizeof(hash));
833             ptr += sizeof(hash);
834             hdr = (struct CertPropIDHeader *)ptr;
835             hdr->propID = CERT_CERT_PROP_ID;
836             hdr->unknown1 = 1;
837             hdr->cb = sizeof(bigCert2);
838             ptr += sizeof(*hdr);
839             memcpy(ptr, bigCert2, sizeof(bigCert2));
840
841             rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
842              sizeof(buf));
843             ok(!rc, "RegSetValueExA failed: %d\n", rc);
844
845             ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
846             ok(ret, "CertControlStore failed: %08x\n", GetLastError());
847
848             /* and make sure just one cert still gets loaded. */
849             certCount = 0;
850             context = NULL;
851             do {
852                 context = CertEnumCertificatesInStore(store, context);
853                 if (context)
854                     certCount++;
855             } while (context != NULL);
856             ok(certCount == 1 ||
857                broken(certCount == 2), /* win9x */
858                "Expected 1 certificates, got %d\n", certCount);
859
860             /* Try again with the correct hash... */
861             ptr = buf + sizeof(*hdr);
862             memcpy(ptr, hash, sizeof(hash));
863
864             rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
865              sizeof(buf));
866             ok(!rc, "RegSetValueExA failed: %d\n", rc);
867
868             ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
869             ok(ret, "CertControlStore failed: %08x\n", GetLastError());
870
871             /* and make sure two certs get loaded. */
872             certCount = 0;
873             context = NULL;
874             do {
875                 context = CertEnumCertificatesInStore(store, context);
876                 if (context)
877                     certCount++;
878             } while (context != NULL);
879             ok(certCount == 2, "Expected 2 certificates, got %d\n", certCount);
880
881             RegCloseKey(subKey);
882         }
883         CertCloseStore(store, 0);
884         /* Is delete allowed on a reg store? */
885         store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
886          CERT_STORE_DELETE_FLAG, key);
887         ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
888         ok(GetLastError() == 0, "CertOpenStore failed: %08x\n",
889          GetLastError());
890
891         RegCloseKey(key);
892     }
893     /* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
894      * contents of the key, but not the key itself.
895      */
896     rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
897      NULL, &key, &disp);
898     ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
899     ok(disp == REG_OPENED_EXISTING_KEY,
900      "Expected REG_OPENED_EXISTING_KEY, got %d\n", disp);
901     if (!rc)
902     {
903         RegCloseKey(key);
904         rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
905         if (rc)
906         {
907             HMODULE shlwapi = LoadLibraryA("shlwapi");
908
909             /* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
910              * otherwise subsequent tests will fail.
911              */
912             if (shlwapi)
913             {
914                 SHDeleteKeyAFunc pSHDeleteKeyA =
915                  (SHDeleteKeyAFunc)GetProcAddress(shlwapi, "SHDeleteKeyA");
916
917                 if (pSHDeleteKeyA)
918                     pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
919                 FreeLibrary(shlwapi);
920             }
921         }
922     }
923 }
924
925 static const char MyA[] = { 'M','y',0,0 };
926 static const WCHAR MyW[] = { 'M','y',0 };
927 static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
928 static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
929  'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
930  't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
931
932 static void testSystemRegStore(void)
933 {
934     HCERTSTORE store, memStore;
935
936     /* Check with a UNICODE name */
937     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
938      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
939     /* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
940      * testing if they don't.
941      */
942     if (!store)
943         return;
944
945     /* Check that it isn't a collection store */
946     memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
947      CERT_STORE_CREATE_NEW_FLAG, NULL);
948     if (memStore)
949     {
950         if (pCertAddStoreToCollection)
951         {
952             BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
953             ok(!ret && GetLastError() == E_INVALIDARG,
954                "Expected E_INVALIDARG, got %08x\n", GetLastError());
955         }
956         CertCloseStore(memStore, 0);
957     }
958     CertCloseStore(store, 0);
959
960     /* Check opening a bogus store */
961     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
962      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
963     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
964      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
965     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
966      CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
967     ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
968     if (store)
969         CertCloseStore(store, 0);
970     /* Now check whether deleting is allowed */
971     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
972      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
973     RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
974
975     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
976     ok(!store && GetLastError() == E_INVALIDARG,
977      "Expected E_INVALIDARG, got %08x\n", GetLastError());
978     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
979      CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
980     ok(!store && GetLastError() == E_INVALIDARG,
981      "Expected E_INVALIDARG, got %08x\n", GetLastError());
982     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
983      CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
984     ok(!store && GetLastError() == E_INVALIDARG,
985      "Expected E_INVALIDARG, got %08x\n", GetLastError());
986     /* The name is expected to be UNICODE, check with an ASCII name */
987     store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
988      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
989     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
990      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
991 }
992
993 static void testSystemStore(void)
994 {
995     static const WCHAR baskslashW[] = { '\\',0 };
996     HCERTSTORE store;
997     WCHAR keyName[MAX_PATH];
998     HKEY key;
999     LONG rc;
1000
1001     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
1002     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1003      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1004     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1005      CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1006     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1007      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1008     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1009      CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1010     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1011      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1012     /* The name is expected to be UNICODE, first check with an ASCII name */
1013     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1014      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1015     ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1016      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1017     /* Create the expected key */
1018     lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
1019     lstrcatW(keyName, baskslashW);
1020     lstrcatW(keyName, MyW);
1021     rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
1022      NULL, &key, NULL);
1023     ok(!rc, "RegCreateKeyEx failed: %d\n", rc);
1024     if (!rc)
1025         RegCloseKey(key);
1026     /* Check opening with a UNICODE name, specifying the create new flag */
1027     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1028      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
1029     ok(!store && GetLastError() == ERROR_FILE_EXISTS,
1030      "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
1031     /* Now check opening with a UNICODE name, this time opening existing */
1032     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1033      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1034     ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1035     if (store)
1036     {
1037         HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1038          CERT_STORE_CREATE_NEW_FLAG, NULL);
1039
1040         /* Check that it's a collection store */
1041         if (memStore)
1042         {
1043             if (pCertAddStoreToCollection)
1044             {
1045                 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1046                 /* FIXME: this'll fail on NT4, but what error will it give? */
1047                 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
1048             }
1049             CertCloseStore(memStore, 0);
1050         }
1051         CertCloseStore(store, 0);
1052     }
1053
1054     /* Check opening a bogus store */
1055     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1056      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1057     ok((!store ||
1058      broken(store != 0)) && /* win9x */
1059      GetLastError() == ERROR_FILE_NOT_FOUND,
1060      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1061     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1062      CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1063     ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1064     if (store)
1065         CertCloseStore(store, 0);
1066     /* Now check whether deleting is allowed */
1067     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1068      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1069     RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1070 }
1071
1072 static const BYTE serializedStoreWithCert[] = {
1073  0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1074  0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1075  0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1076  0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1077  0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1078  0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1079  0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1080  0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1081  0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1082  0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
1083  0x00,0x00,0x00,0x00,0x00,0x00 };
1084 static const BYTE serializedStoreWithCertAndCRL[] = {
1085  0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1086  0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1087  0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1088  0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1089  0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1090  0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1091  0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1092  0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1093  0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1094  0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x21,0x00,0x00,0x00,0x01,0x00,
1095  0x00,0x00,0x47,0x00,0x00,0x00,0x30,0x45,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,
1096  0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1097  0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1098  0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x03,0x11,
1099  0x00,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,
1100  0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1101
1102 static void compareFile(LPCWSTR filename, const BYTE *pb, DWORD cb)
1103 {
1104     HANDLE h;
1105     BYTE buf[200];
1106     BOOL ret;
1107     DWORD cbRead = 0, totalRead = 0;
1108
1109     h = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
1110      FILE_ATTRIBUTE_NORMAL, NULL);
1111     if (h == INVALID_HANDLE_VALUE)
1112         return;
1113     do {
1114         ret = ReadFile(h, buf, sizeof(buf), &cbRead, NULL);
1115         if (ret && cbRead)
1116         {
1117             ok(totalRead + cbRead <= cb, "Expected total count %d, see %d\n",
1118              cb, totalRead + cbRead);
1119             ok(!memcmp(pb + totalRead, buf, cbRead),
1120              "Unexpected data in file\n");
1121             totalRead += cbRead;
1122         }
1123     } while (ret && cbRead);
1124     CloseHandle(h);
1125 }
1126
1127 static void testFileStore(void)
1128 {
1129     static const WCHAR szPrefix[] = { 'c','e','r',0 };
1130     static const WCHAR szDot[] = { '.',0 };
1131     WCHAR filename[MAX_PATH];
1132     HCERTSTORE store;
1133     BOOL ret;
1134     PCCERT_CONTEXT cert;
1135     HANDLE file;
1136
1137     if (!pCertControlStore)
1138     {
1139         win_skip("CertControlStore() is not available\n");
1140         return;
1141     }
1142
1143     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, 0, NULL);
1144     ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
1145      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1146
1147     if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1148        return;
1149  
1150     DeleteFileW(filename);
1151     file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1152      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1153     if (file == INVALID_HANDLE_VALUE)
1154         return;
1155
1156     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, CERT_STORE_DELETE_FLAG,
1157      file);
1158     ok(!store && GetLastError() == E_INVALIDARG,
1159      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1160     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1161      CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, file);
1162     ok(!store && GetLastError() == E_INVALIDARG,
1163      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1164
1165     /* A "read-only" file store.. */
1166     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1167      CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, file);
1168     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1169     if (store)
1170     {
1171         DWORD size;
1172
1173         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1174          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1175         /* apparently allows adding certificates.. */
1176         ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1177         /* but not commits.. */
1178         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1179         ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1180          "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1181         /* It still has certs in memory.. */
1182         cert = CertEnumCertificatesInStore(store, NULL);
1183         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1184          GetLastError());
1185         CertFreeCertificateContext(cert);
1186         /* but the file size is still 0. */
1187         size = GetFileSize(file, NULL);
1188         ok(size == 0, "Expected size 0, got %d\n", size);
1189         CertCloseStore(store, 0);
1190     }
1191
1192     /* The create new flag is allowed.. */
1193     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1194      CERT_STORE_CREATE_NEW_FLAG, file);
1195     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1196     if (store)
1197     {
1198         /* but without the commit enable flag, commits don't happen. */
1199         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1200          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1201         ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1202         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1203         ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1204          "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1205         CertCloseStore(store, 0);
1206     }
1207     /* as is the open existing flag. */
1208     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1209      CERT_STORE_OPEN_EXISTING_FLAG, file);
1210     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1211     if (store)
1212     {
1213         /* but without the commit enable flag, commits don't happen. */
1214         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1215          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1216         ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1217         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1218         ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1219          "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1220         CertCloseStore(store, 0);
1221     }
1222     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1223      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1224     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1225     if (store)
1226     {
1227         CloseHandle(file);
1228         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1229          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1230         ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1231          GetLastError());
1232         /* with commits enabled, commit is allowed */
1233         ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1234         ok(ret, "CertControlStore failed: %d\n", ret);
1235         compareFile(filename, serializedStoreWithCert,
1236          sizeof(serializedStoreWithCert));
1237         CertCloseStore(store, 0);
1238     }
1239     file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1240      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1241     if (file == INVALID_HANDLE_VALUE)
1242         return;
1243     store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1244      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1245     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1246     if (store)
1247     {
1248         CloseHandle(file);
1249         ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
1250          sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1251         ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1252         CertCloseStore(store, 0);
1253         compareFile(filename, serializedStoreWithCertAndCRL,
1254          sizeof(serializedStoreWithCertAndCRL));
1255     }
1256
1257     DeleteFileW(filename);
1258 }
1259
1260 static BOOL initFileFromData(LPCWSTR filename, const BYTE *pb, DWORD cb)
1261 {
1262     HANDLE file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1263      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1264     BOOL ret;
1265
1266     if (file != INVALID_HANDLE_VALUE)
1267     {
1268         DWORD written;
1269
1270         ret = WriteFile(file, pb, cb, &written, NULL);
1271         CloseHandle(file);
1272     }
1273     else
1274         ret = FALSE;
1275     return ret;
1276 }
1277
1278 static const BYTE base64SPC[] =
1279 "MIICJQYJKoZIhvcNAQcCoIICFjCCAhICAQExADALBgkqhkiG9w0BBwGgggH6MIIB"
1280 "9jCCAV+gAwIBAgIQnP8+EF4opr9OxH7h4uBPWTANBgkqhkiG9w0BAQQFADAUMRIw"
1281 "EAYDVQQDEwlKdWFuIExhbmcwHhcNMDgxMjEyMTcxMDE0WhcNMzkxMjMxMjM1OTU5"
1282 "WjAUMRIwEAYDVQQDEwlKdWFuIExhbmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ"
1283 "AoGBALCgNjyNvOic0FOfjxvi43HbM+D5joDkhiGSXe+gbZlf8f16k07kkObFEunz"
1284 "mdB5coscmA7gyqiWNN4ZUyr2cA3lCbnpGPA/0IblyyOcuGIFmmCzeZaVa5ZG6xZP"
1285 "K7L7o+73Qo6jXVbGhBGnMZ7Q9sAn6s2933olnStnejnqwV0NAgMBAAGjSTBHMEUG"
1286 "A1UdAQQ+MDyAEFKbKEdXYyx+CWKcV6vxM6ShFjAUMRIwEAYDVQQDEwlKdWFuIExh"
1287 "bmeCEJz/PhBeKKa/TsR+4eLgT1kwDQYJKoZIhvcNAQEEBQADgYEALpkgLgW3mEaK"
1288 "idPQ3iPJYLG0Ub1wraqEl9bd42hrhzIdcDzlQgxnm8/5cHYVxIF/C20x/HJplb1R"
1289 "G6U1ipFe/q8byWD/9JpiBKMGPi9YlUTgXHfS9d4S/QWO1h9Z7KeipBYhoslQpHXu"
1290 "y9bUr8Adqi6SzgHpCnMu53dxgxUD1r4xAA==";
1291 /* Same as base64SPC, but as a wide-char string */
1292 static const WCHAR utf16Base64SPC[] = {
1293 'M','I','I','C','J','Q','Y','J','K','o','Z','I','h','v','c','N','A',
1294 'Q','c','C','o','I','I','C','F','j','C','C','A','h','I','C','A','Q',
1295 'E','x','A','D','A','L','B','g','k','q','h','k','i','G','9','w','0',
1296 'B','B','w','G','g','g','g','H','6','M','I','I','B','9','j','C','C',
1297 'A','V','+','g','A','w','I','B','A','g','I','Q','n','P','8','+','E',
1298 'F','4','o','p','r','9','O','x','H','7','h','4','u','B','P','W','T',
1299 'A','N','B','g','k','q','h','k','i','G','9','w','0','B','A','Q','Q',
1300 'F','A','D','A','U','M','R','I','w','E','A','Y','D','V','Q','Q','D',
1301 'E','w','l','K','d','W','F','u','I','E','x','h','b','m','c','w','H',
1302 'h','c','N','M','D','g','x','M','j','E','y','M','T','c','x','M','D',
1303 'E','0','W','h','c','N','M','z','k','x','M','j','M','x','M','j','M',
1304 '1','O','T','U','5','W','j','A','U','M','R','I','w','E','A','Y','D',
1305 'V','Q','Q','D','E','w','l','K','d','W','F','u','I','E','x','h','b',
1306 'm','c','w','g','Z','8','w','D','Q','Y','J','K','o','Z','I','h','v',
1307 'c','N','A','Q','E','B','B','Q','A','D','g','Y','0','A','M','I','G',
1308 'J','A','o','G','B','A','L','C','g','N','j','y','N','v','O','i','c',
1309 '0','F','O','f','j','x','v','i','4','3','H','b','M','+','D','5','j',
1310 'o','D','k','h','i','G','S','X','e','+','g','b','Z','l','f','8','f',
1311 '1','6','k','0','7','k','k','O','b','F','E','u','n','z','m','d','B',
1312 '5','c','o','s','c','m','A','7','g','y','q','i','W','N','N','4','Z',
1313 'U','y','r','2','c','A','3','l','C','b','n','p','G','P','A','/','0',
1314 'I','b','l','y','y','O','c','u','G','I','F','m','m','C','z','e','Z',
1315 'a','V','a','5','Z','G','6','x','Z','P','K','7','L','7','o','+','7',
1316 '3','Q','o','6','j','X','V','b','G','h','B','G','n','M','Z','7','Q',
1317 '9','s','A','n','6','s','2','9','3','3','o','l','n','S','t','n','e',
1318 'j','n','q','w','V','0','N','A','g','M','B','A','A','G','j','S','T',
1319 'B','H','M','E','U','G','A','1','U','d','A','Q','Q','+','M','D','y',
1320 'A','E','F','K','b','K','E','d','X','Y','y','x','+','C','W','K','c',
1321 'V','6','v','x','M','6','S','h','F','j','A','U','M','R','I','w','E',
1322 'A','Y','D','V','Q','Q','D','E','w','l','K','d','W','F','u','I','E',
1323 'x','h','b','m','e','C','E','J','z','/','P','h','B','e','K','K','a',
1324 '/','T','s','R','+','4','e','L','g','T','1','k','w','D','Q','Y','J',
1325 'K','o','Z','I','h','v','c','N','A','Q','E','E','B','Q','A','D','g',
1326 'Y','E','A','L','p','k','g','L','g','W','3','m','E','a','K','i','d',
1327 'P','Q','3','i','P','J','Y','L','G','0','U','b','1','w','r','a','q',
1328 'E','l','9','b','d','4','2','h','r','h','z','I','d','c','D','z','l',
1329 'Q','g','x','n','m','8','/','5','c','H','Y','V','x','I','F','/','C',
1330 '2','0','x','/','H','J','p','l','b','1','R','G','6','U','1','i','p',
1331 'F','e','/','q','8','b','y','W','D','/','9','J','p','i','B','K','M',
1332 'G','P','i','9','Y','l','U','T','g','X','H','f','S','9','d','4','S',
1333 '/','Q','W','O','1','h','9','Z','7','K','e','i','p','B','Y','h','o',
1334 's','l','Q','p','H','X','u','y','9','b','U','r','8','A','d','q','i',
1335 '6','S','z','g','H','p','C','n','M','u','5','3','d','x','g','x','U',
1336 'D','1','r','4','x','A','A','=','=',0 };
1337
1338 static void testFileNameStore(void)
1339 {
1340     static const WCHAR szPrefix[] = { 'c','e','r',0 };
1341     static const WCHAR spcPrefix[] = { 's','p','c',0 };
1342     static const WCHAR szDot[] = { '.',0 };
1343     WCHAR filename[MAX_PATH];
1344     HCERTSTORE store;
1345     BOOL ret;
1346     DWORD GLE;
1347
1348     if (0)
1349     {
1350         /* Crashes on NT4 */
1351         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0, 0, NULL);
1352         GLE = GetLastError();
1353         ok(!store && (GLE == ERROR_PATH_NOT_FOUND || GLE == ERROR_INVALID_PARAMETER),
1354          "Expected ERROR_PATH_NOT_FOUND or ERROR_INVALID_PARAMETER, got %08x\n",
1355          GLE);
1356     }
1357
1358     if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1359        return;
1360     DeleteFileW(filename);
1361
1362     /* The two flags are mutually exclusive */
1363     store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1364      CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, filename);
1365     ok(!store && GetLastError() == E_INVALIDARG,
1366      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1367
1368     /* In all of the following tests, the encoding type seems to be ignored */
1369     if (initFileFromData(filename, bigCert, sizeof(bigCert)))
1370     {
1371         PCCERT_CONTEXT cert;
1372         PCCRL_CONTEXT crl;
1373
1374         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1375          CERT_STORE_READONLY_FLAG, filename);
1376         ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1377
1378         cert = CertEnumCertificatesInStore(store, NULL);
1379         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1380          GetLastError());
1381         cert = CertEnumCertificatesInStore(store, cert);
1382         ok(!cert, "Expected only one cert\n");
1383         if (pCertEnumCRLsInStore)
1384         {
1385             crl = pCertEnumCRLsInStore(store, NULL);
1386             ok(!crl, "Expected no CRLs\n");
1387         }
1388
1389         CertCloseStore(store, 0);
1390         DeleteFileW(filename);
1391     }
1392     if (initFileFromData(filename, serializedStoreWithCert,
1393      sizeof(serializedStoreWithCert)))
1394     {
1395         PCCERT_CONTEXT cert;
1396         PCCRL_CONTEXT crl;
1397
1398         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1399          CERT_STORE_READONLY_FLAG, filename);
1400         ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1401
1402         cert = CertEnumCertificatesInStore(store, NULL);
1403         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1404          GetLastError());
1405         cert = CertEnumCertificatesInStore(store, cert);
1406         ok(!cert, "Expected only one cert\n");
1407         if (pCertEnumCRLsInStore)
1408         {
1409             crl = pCertEnumCRLsInStore(store, NULL);
1410             ok(!crl, "Expected no CRLs\n");
1411         }
1412
1413         CertCloseStore(store, 0);
1414         DeleteFileW(filename);
1415     }
1416     if (initFileFromData(filename, serializedStoreWithCertAndCRL,
1417      sizeof(serializedStoreWithCertAndCRL)))
1418     {
1419         PCCERT_CONTEXT cert;
1420         PCCRL_CONTEXT crl;
1421
1422         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1423          CERT_STORE_READONLY_FLAG, filename);
1424         ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1425
1426         cert = CertEnumCertificatesInStore(store, NULL);
1427         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1428          GetLastError());
1429         cert = CertEnumCertificatesInStore(store, cert);
1430         ok(!cert, "Expected only one cert\n");
1431         if (pCertEnumCRLsInStore)
1432         {
1433             crl = pCertEnumCRLsInStore(store, NULL);
1434             ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n", GetLastError());
1435             crl = pCertEnumCRLsInStore(store, crl);
1436             ok(!crl, "Expected only one CRL\n");
1437         }
1438
1439         CertCloseStore(store, 0);
1440         /* Don't delete it this time, the next test uses it */
1441     }
1442     /* Now that the file exists, we can open it read-only */
1443     store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1444      CERT_STORE_READONLY_FLAG, filename);
1445     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1446     CertCloseStore(store, 0);
1447     DeleteFileW(filename);
1448
1449     store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1450      CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_CREATE_NEW_FLAG, filename);
1451     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1452     if (store)
1453     {
1454         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1455          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1456         ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1457          GetLastError());
1458         CertCloseStore(store, 0);
1459         compareFile(filename, serializedStoreWithCert,
1460          sizeof(serializedStoreWithCert));
1461     }
1462     store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1463      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, filename);
1464     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1465     if (store)
1466     {
1467         ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING,
1468          signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1469         ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1470         CertCloseStore(store, 0);
1471         compareFile(filename, serializedStoreWithCertAndCRL,
1472          sizeof(serializedStoreWithCertAndCRL));
1473     }
1474     DeleteFileW(filename);
1475
1476     if (!GetTempFileNameW(szDot, spcPrefix, 0, filename))
1477        return;
1478     DeleteFileW(filename);
1479
1480     if (initFileFromData(filename, base64SPC, sizeof(base64SPC)))
1481     {
1482         PCCERT_CONTEXT cert;
1483         PCCRL_CONTEXT crl;
1484
1485         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1486          CERT_STORE_READONLY_FLAG, filename);
1487         ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1488
1489         cert = CertEnumCertificatesInStore(store, NULL);
1490         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1491          GetLastError());
1492         cert = CertEnumCertificatesInStore(store, cert);
1493         ok(!cert, "Expected only one cert\n");
1494         if (pCertEnumCRLsInStore)
1495         {
1496             crl = pCertEnumCRLsInStore(store, NULL);
1497             ok(!crl, "Expected no CRLs\n");
1498         }
1499
1500         CertCloseStore(store, 0);
1501         DeleteFileW(filename);
1502     }
1503     if (initFileFromData(filename, (BYTE *)utf16Base64SPC,
1504      sizeof(utf16Base64SPC)))
1505     {
1506         PCCERT_CONTEXT cert;
1507         PCCRL_CONTEXT crl;
1508
1509         store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1510          CERT_STORE_READONLY_FLAG, filename);
1511         ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1512
1513         cert = CertEnumCertificatesInStore(store, NULL);
1514         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1515          GetLastError());
1516         cert = CertEnumCertificatesInStore(store, cert);
1517         ok(!cert, "Expected only one cert\n");
1518         if (pCertEnumCRLsInStore)
1519         {
1520             crl = pCertEnumCRLsInStore(store, NULL);
1521             ok(!crl, "Expected no CRLs\n");
1522         }
1523
1524         CertCloseStore(store, 0);
1525         DeleteFileW(filename);
1526     }
1527 }
1528
1529 static const BYTE signedContent[] = {
1530 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
1531 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
1532 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
1533 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
1534 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
1535 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1536 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1537 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
1538 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
1539 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
1540 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
1541 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
1542 0x0d };
1543 static const BYTE signedWithCertAndCrlBareContent[] = {
1544 0x30,0x82,0x01,0x4f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,
1545 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,
1546 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0xa0,
1547 0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,
1548 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
1549 0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,
1550 0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1551 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,
1552 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1553 0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,
1554 0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,
1555 0x01,0xff,0x02,0x01,0x01,0xa1,0x2e,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,0x15,
1556 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1557 0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
1558 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x31,0x77,0x30,0x75,0x02,0x01,0x01,
1559 0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,
1560 0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,
1561 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,
1562 0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,
1563 0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,
1564 0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,
1565 0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,
1566 0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
1567 static const BYTE hashContent[] = {
1568 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
1569 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1570 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1571 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1572 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1573 static const BYTE hashBareContent[] = {
1574 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1575 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1576 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1577 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1578
1579 static void testMessageStore(void)
1580 {
1581     HCERTSTORE store;
1582     HCRYPTMSG msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, 0, 0, NULL,
1583      NULL);
1584     CRYPT_DATA_BLOB blob = { sizeof(signedWithCertAndCrlBareContent),
1585      (LPBYTE)signedWithCertAndCrlBareContent };
1586     DWORD count, size;
1587     BOOL ret;
1588
1589     /* Crashes
1590     store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, NULL);
1591      */
1592     SetLastError(0xdeadbeef);
1593     store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1594     ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1595      "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1596     CryptMsgUpdate(msg, signedContent, sizeof(signedContent), TRUE);
1597     store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1598     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1599     if (store)
1600     {
1601         PCCERT_CONTEXT cert = NULL;
1602         PCCRL_CONTEXT crl = NULL;
1603
1604         count = 0;
1605         do {
1606             cert = CertEnumCertificatesInStore(store, cert);
1607             if (cert)
1608                 count++;
1609         } while (cert);
1610         ok(count == 0, "Expected 0 certificates, got %d\n", count);
1611
1612         if (pCertEnumCRLsInStore)
1613         {
1614             count = 0;
1615             do {
1616                 crl = pCertEnumCRLsInStore(store, crl);
1617                 if (crl)
1618                     count++;
1619             } while (crl);
1620             ok(count == 0, "Expected 0 CRLs, got %d\n", count);
1621         }
1622
1623         /* Can add certs to a message store */
1624         ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1625          bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1626         ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1627          GetLastError());
1628         count = 0;
1629         do {
1630             cert = CertEnumCertificatesInStore(store, cert);
1631             if (cert)
1632                 count++;
1633         } while (cert);
1634         ok(count == 1, "Expected 1 certificate, got %d\n", count);
1635
1636         CertCloseStore(store, 0);
1637     }
1638     /* but the added certs weren't actually added to the message */
1639     size = sizeof(count);
1640     ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
1641     ok(ret, "CryptMsgGetParam failed: %08x\n", GetLastError());
1642     ok(count == 0, "Expected 0 certificates, got %d\n", count);
1643     CryptMsgClose(msg);
1644
1645     /* Crashes
1646     store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, NULL);
1647      */
1648     store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1649     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1650     if (store)
1651     {
1652         DWORD count = 0;
1653         PCCERT_CONTEXT cert = NULL;
1654         PCCRL_CONTEXT crl = NULL;
1655
1656         do {
1657             cert = CertEnumCertificatesInStore(store, cert);
1658             if (cert)
1659                 count++;
1660         } while (cert);
1661         ok(count == 1, "Expected 1 certificate, got %d\n", count);
1662
1663         if (pCertEnumCRLsInStore)
1664         {
1665             count = 0;
1666             do {
1667                 crl = pCertEnumCRLsInStore(store, crl);
1668                 if (crl)
1669                     count++;
1670             } while (crl);
1671             ok(count == 1, "Expected 1 CRL, got %d\n", count);
1672         }
1673         CertCloseStore(store, 0);
1674     }
1675     /* Encoding appears to be ignored */
1676     store = CertOpenStore(CERT_STORE_PROV_PKCS7, X509_ASN_ENCODING, 0, 0,
1677      &blob);
1678     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1679     if (store)
1680         CertCloseStore(store, 0);
1681     /* Messages other than signed messages aren't allowed */
1682     blob.cbData = sizeof(hashContent);
1683     blob.pbData = (LPBYTE)hashContent;
1684     SetLastError(0xdeadbeef);
1685     store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1686     ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1687      "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1688     blob.cbData = sizeof(hashBareContent);
1689     blob.pbData = (LPBYTE)hashBareContent;
1690     SetLastError(0xdeadbeef);
1691     store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1692     ok(!store &&
1693      (GetLastError() == CRYPT_E_ASN1_BADTAG ||
1694       GetLastError() == OSS_DATA_ERROR), /* win9x */
1695      "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
1696 }
1697
1698 static void testCertOpenSystemStore(void)
1699 {
1700     HCERTSTORE store;
1701
1702     store = CertOpenSystemStoreW(0, NULL);
1703     ok(!store && GetLastError() == E_INVALIDARG,
1704      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1705     /* This succeeds, and on WinXP at least, the Bogus key is created under
1706      * HKCU (but not under HKLM, even when run as an administrator.)
1707      */
1708     store = CertOpenSystemStoreW(0, BogusW);
1709     ok(store != 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
1710     if (store)
1711         CertCloseStore(store, 0);
1712     /* Delete it so other tests succeed next time around */
1713     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1714      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1715     RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1716 }
1717
1718 struct EnumSystemStoreInfo
1719 {
1720     BOOL  goOn;
1721     DWORD storeCount;
1722 };
1723
1724 static BOOL CALLBACK enumSystemStoreCB(const void *systemStore, DWORD dwFlags,
1725  PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved, void *pvArg)
1726 {
1727     struct EnumSystemStoreInfo *info = pvArg;
1728
1729     info->storeCount++;
1730     return info->goOn;
1731 }
1732
1733 static void testCertEnumSystemStore(void)
1734 {
1735     BOOL ret;
1736     struct EnumSystemStoreInfo info = { FALSE, 0 };
1737
1738     if (!pCertEnumSystemStore)
1739     {
1740         win_skip("CertEnumSystemStore() is not available\n");
1741         return;
1742     }
1743
1744     SetLastError(0xdeadbeef);
1745     ret = pCertEnumSystemStore(0, NULL, NULL, NULL);
1746     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1747      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1748     /* Crashes
1749     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, NULL,
1750      NULL);
1751      */
1752
1753     SetLastError(0xdeadbeef);
1754     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1755      enumSystemStoreCB);
1756     /* Callback returning FALSE stops enumeration */
1757     ok(!ret, "Expected CertEnumSystemStore to stop\n");
1758     ok(info.storeCount == 0 || info.storeCount == 1,
1759      "Expected 0 or 1 stores\n");
1760
1761     info.goOn = TRUE;
1762     info.storeCount = 0;
1763     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1764      enumSystemStoreCB);
1765     ok(ret, "CertEnumSystemStore failed: %08x\n", GetLastError());
1766     /* There should always be at least My, Root, and CA stores */
1767     ok(info.storeCount == 0 || info.storeCount >= 3,
1768      "Expected at least 3 stores\n");
1769 }
1770
1771 static void testStoreProperty(void)
1772 {
1773     HCERTSTORE store;
1774     BOOL ret;
1775     DWORD propID, size = 0, state;
1776     CRYPT_DATA_BLOB blob;
1777
1778     if (!pCertGetStoreProperty || !pCertSetStoreProperty)
1779     {
1780         win_skip("CertGet/SetStoreProperty() is not available\n");
1781         return;
1782     }
1783
1784     /* Crash
1785     ret = pCertGetStoreProperty(NULL, 0, NULL, NULL);
1786     ret = pCertGetStoreProperty(NULL, 0, NULL, &size);
1787     ret = pCertGetStoreProperty(store, 0, NULL, NULL);
1788      */
1789
1790     store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1791      CERT_STORE_CREATE_NEW_FLAG, NULL);
1792     /* Check a missing prop ID */
1793     SetLastError(0xdeadbeef);
1794     ret = pCertGetStoreProperty(store, 0, NULL, &size);
1795     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1796      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1797     /* Contrary to MSDN, CERT_ACCESS_STATE_PROP_ID is supported for stores.. */
1798     size = sizeof(state);
1799     ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1800     ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1801      GetLastError());
1802     ok(!state, "Expected a non-persisted store\n");
1803     /* and CERT_STORE_LOCALIZED_NAME_PROP_ID isn't supported by default. */
1804     size = 0;
1805     ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1806      &size);
1807     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1808      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1809     /* Delete an arbitrary property on a store */
1810     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1811     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1812     /* Set an arbitrary property on a store */
1813     blob.pbData = (LPBYTE)&state;
1814     blob.cbData = sizeof(state);
1815     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, &blob);
1816     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1817     /* Get an arbitrary property that's been set */
1818     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1819     ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1820     ok(size == sizeof(state), "Unexpected data size %d\n", size);
1821     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, &propID, &size);
1822     ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1823     ok(propID == state, "CertGetStoreProperty got the wrong value\n");
1824     /* Delete it again */
1825     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1826     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1827     /* And check that it's missing */
1828     SetLastError(0xdeadbeef);
1829     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1830     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1831      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1832     CertCloseStore(store, 0);
1833
1834     /* Recheck on the My store.. */
1835     store = CertOpenSystemStoreW(0, MyW);
1836     size = sizeof(state);
1837     ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1838     ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1839      GetLastError());
1840     ok(state, "Expected a persisted store\n");
1841     SetLastError(0xdeadbeef);
1842     size = 0;
1843     ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1844      &size);
1845     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1846      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1847     CertCloseStore(store, 0);
1848 }
1849
1850 static void testAddSerialized(void)
1851 {
1852     BOOL ret;
1853     HCERTSTORE store;
1854     BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
1855      { 0 };
1856     BYTE hash[20];
1857     struct CertPropIDHeader *hdr;
1858     PCCERT_CONTEXT context;
1859
1860     ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
1861     ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1862      "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1863
1864     store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1865      CERT_STORE_CREATE_NEW_FLAG, NULL);
1866     ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1867
1868     ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
1869     ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1870      "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1871
1872     /* Test with an empty property */
1873     hdr = (struct CertPropIDHeader *)buf;
1874     hdr->propID = CERT_CERT_PROP_ID;
1875     hdr->unknown1 = 1;
1876     hdr->cb = 0;
1877     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1878      NULL, NULL);
1879     ok(!ret && GetLastError() == E_INVALIDARG,
1880      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1881     /* Test with a bad size in property header */
1882     hdr->cb = sizeof(bigCert) - 1;
1883     memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert));
1884     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1885      NULL, NULL);
1886     ok(!ret && GetLastError() == E_INVALIDARG,
1887      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1888     ret = CertAddSerializedElementToStore(store, buf,
1889      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1890      NULL);
1891     ok(!ret && GetLastError() == E_INVALIDARG,
1892      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1893     ret = CertAddSerializedElementToStore(store, buf,
1894      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1895      0, 0, NULL, NULL);
1896     ok(!ret && GetLastError() == E_INVALIDARG,
1897      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1898     /* Kosher size in property header, but no context type */
1899     hdr->cb = sizeof(bigCert);
1900     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
1901      NULL, NULL);
1902     ok(!ret && GetLastError() == E_INVALIDARG,
1903      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1904     ret = CertAddSerializedElementToStore(store, buf,
1905      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
1906      NULL);
1907     ok(!ret && GetLastError() == E_INVALIDARG,
1908      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1909     ret = CertAddSerializedElementToStore(store, buf,
1910      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1911      0, 0, NULL, NULL);
1912     ok(!ret && GetLastError() == E_INVALIDARG,
1913      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1914     /* With a bad context type */
1915     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
1916      CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1917     ok(!ret && GetLastError() == E_INVALIDARG,
1918      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1919     ret = CertAddSerializedElementToStore(store, buf,
1920      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
1921      CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1922     ok(!ret && GetLastError() == E_INVALIDARG,
1923      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1924     ret = CertAddSerializedElementToStore(store, buf,
1925      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1926      0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
1927     ok(!ret && GetLastError() == E_INVALIDARG,
1928      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1929     /* Bad unknown field, good type */
1930     hdr->unknown1 = 2;
1931     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
1932      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1933     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1934      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1935     ret = CertAddSerializedElementToStore(store, buf,
1936      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
1937      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1938     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1939      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1940     ret = CertAddSerializedElementToStore(store, buf,
1941      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1942      0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1943     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1944      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
1945     /* Most everything okay, but bad add disposition */
1946     hdr->unknown1 = 1;
1947     /* This crashes
1948     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
1949      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1950      * as does this
1951     ret = CertAddSerializedElementToStore(store, buf,
1952      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
1953      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1954      */
1955     /* Everything okay, but buffer's too big */
1956     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1957      CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1958     ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1959     /* Everything okay, check it's not re-added */
1960     ret = CertAddSerializedElementToStore(store, buf,
1961      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
1962      0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
1963     ok(!ret && GetLastError() == CRYPT_E_EXISTS,
1964      "Expected CRYPT_E_EXISTS, got %08x\n", GetLastError());
1965
1966     context = CertEnumCertificatesInStore(store, NULL);
1967     ok(context != NULL, "Expected a cert\n");
1968     if (context)
1969         CertDeleteCertificateFromStore(context);
1970
1971     /* Try adding with a bogus hash.  Oddly enough, it succeeds, and the hash,
1972      * when queried, is the real hash rather than the bogus hash.
1973      */
1974     hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
1975      sizeof(bigCert));
1976     hdr->propID = CERT_HASH_PROP_ID;
1977     hdr->unknown1 = 1;
1978     hdr->cb = sizeof(hash);
1979     memset(hash, 0xc, sizeof(hash));
1980     memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
1981     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
1982      CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
1983      (const void **)&context);
1984     ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
1985     if (context)
1986     {
1987         BYTE hashVal[20], realHash[20];
1988         DWORD size = sizeof(hashVal);
1989
1990         ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert),
1991          realHash, &size);
1992         ok(ret, "CryptHashCertificate failed: %08x\n", GetLastError());
1993         ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
1994          hashVal, &size);
1995         ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
1996          GetLastError());
1997         ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
1998         CertFreeCertificateContext(context);
1999     }
2000
2001     CertCloseStore(store, 0);
2002 }
2003
2004 static DWORD countCertsInStore(HCERTSTORE store)
2005 {
2006     PCCERT_CONTEXT cert = NULL;
2007     DWORD certs = 0;
2008
2009     do {
2010         cert = CertEnumCertificatesInStore(store, cert);
2011         if (cert)
2012             certs++;
2013     } while (cert);
2014     return certs;
2015 }
2016
2017 static DWORD countCRLsInStore(HCERTSTORE store)
2018 {
2019     PCCRL_CONTEXT crl = NULL;
2020     DWORD crls = 0;
2021
2022     do {
2023         crl = pCertEnumCRLsInStore(store, crl);
2024         if (crl)
2025             crls++;
2026     } while (crl);
2027     return crls;
2028 }
2029
2030 static void test_I_UpdateStore(void)
2031 {
2032     HMODULE lib = GetModuleHandleA("crypt32");
2033     BOOL (WINAPI *pI_CertUpdatestore)(HCERTSTORE, HCERTSTORE, DWORD, DWORD) =
2034      (void *)GetProcAddress(lib, "I_CertUpdateStore");
2035     BOOL ret;
2036     HCERTSTORE store1, store2;
2037     PCCERT_CONTEXT cert;
2038     DWORD certs;
2039
2040     if (!pI_CertUpdatestore)
2041     {
2042         win_skip("No I_CertUpdateStore\n");
2043         return;
2044     }
2045     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2046      CERT_STORE_CREATE_NEW_FLAG, NULL);
2047     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2048      CERT_STORE_CREATE_NEW_FLAG, NULL);
2049
2050     /* Crash
2051     ret = pI_CertUpdatestore(NULL, NULL, 0, 0);
2052     ret = pI_CertUpdatestore(store1, NULL, 0, 0);
2053     ret = pI_CertUpdatestore(NULL, store2, 0, 0);
2054      */
2055     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2056     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2057
2058     CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING, bigCert,
2059      sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2060     /* I_CertUpdateStore adds the contexts from store2 to store1 */
2061     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2062     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2063     certs = countCertsInStore(store1);
2064     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2065     /* Calling it a second time has no effect */
2066     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2067     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2068     certs = countCertsInStore(store1);
2069     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2070
2071     /* The last parameters to I_CertUpdateStore appear to be ignored */
2072     ret = pI_CertUpdatestore(store1, store2, 1, 0);
2073     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2074     ret = pI_CertUpdatestore(store1, store2, 0, 1);
2075     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2076
2077     CertAddEncodedCRLToStore(store2, X509_ASN_ENCODING, signedCRL,
2078      sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
2079
2080     /* I_CertUpdateStore also adds the CRLs from store2 to store1 */
2081     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2082     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2083     certs = countCertsInStore(store1);
2084     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2085     if (pCertEnumCRLsInStore)
2086     {
2087         certs = countCRLsInStore(store1);
2088         ok(certs == 1, "Expected 1 CRL, got %d\n", certs);
2089     }
2090
2091     CertDeleteCertificateFromStore(cert);
2092     /* If a context is deleted from store2, I_CertUpdateStore deletes it
2093      * from store1
2094      */
2095     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2096     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2097     certs = countCertsInStore(store1);
2098     ok(certs == 0, "Expected 0 certs, got %d\n", certs);
2099
2100     CertFreeCertificateContext(cert);
2101     CertCloseStore(store1, 0);
2102     CertCloseStore(store2, 0);
2103 }
2104
2105 START_TEST(store)
2106 {
2107     HMODULE hdll;
2108
2109     hdll = GetModuleHandleA("Crypt32.dll");
2110     pCertAddStoreToCollection = (void*)GetProcAddress(hdll, "CertAddStoreToCollection");
2111     pCertControlStore = (void*)GetProcAddress(hdll, "CertControlStore");
2112     pCertEnumCRLsInStore = (void*)GetProcAddress(hdll, "CertEnumCRLsInStore");
2113     pCertEnumSystemStore = (void*)GetProcAddress(hdll, "CertEnumSystemStore");
2114     pCertGetStoreProperty = (void*)GetProcAddress(hdll, "CertGetStoreProperty");
2115     pCertRemoveStoreFromCollection = (void*)GetProcAddress(hdll, "CertRemoveStoreFromCollection");
2116     pCertSetStoreProperty = (void*)GetProcAddress(hdll, "CertSetStoreProperty");
2117
2118     /* various combinations of CertOpenStore */
2119     testMemStore();
2120     testCollectionStore();
2121     testRegStore();
2122     testSystemRegStore();
2123     testSystemStore();
2124     testFileStore();
2125     testFileNameStore();
2126     testMessageStore();
2127
2128     testCertOpenSystemStore();
2129     testCertEnumSystemStore();
2130     testStoreProperty();
2131
2132     testAddSerialized();
2133
2134     test_I_UpdateStore();
2135 }