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