wordpad: Allow objects & images to be added with native riched20.
[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 testSerializedStore(void)
1768 {
1769     HCERTSTORE store;
1770     CRYPT_DATA_BLOB blob;
1771
1772     if (0)
1773     {
1774         /* Crash */
1775         store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, NULL);
1776         store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
1777          CERT_STORE_DELETE_FLAG, NULL);
1778     }
1779     blob.cbData = sizeof(serializedStoreWithCert);
1780     blob.pbData = (BYTE *)serializedStoreWithCert;
1781     store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
1782      CERT_STORE_DELETE_FLAG, &blob);
1783     ok(!store && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1784      "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1785     store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
1786     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1787     if (store)
1788     {
1789         PCCERT_CONTEXT cert;
1790         PCCRL_CONTEXT crl;
1791
1792         cert = CertEnumCertificatesInStore(store, NULL);
1793         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1794          GetLastError());
1795         cert = CertEnumCertificatesInStore(store, cert);
1796         ok(!cert, "Expected only one cert\n");
1797         if (pCertEnumCRLsInStore)
1798         {
1799             crl = pCertEnumCRLsInStore(store, NULL);
1800             ok(!crl, "Expected no CRLs\n");
1801         }
1802         CertCloseStore(store, 0);
1803     }
1804     blob.cbData = sizeof(serializedStoreWithCertAndCRL);
1805     blob.pbData = (BYTE *)serializedStoreWithCertAndCRL;
1806     store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
1807     ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1808     if (store)
1809     {
1810         PCCERT_CONTEXT cert;
1811         PCCRL_CONTEXT crl;
1812
1813         cert = CertEnumCertificatesInStore(store, NULL);
1814         ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1815          GetLastError());
1816         cert = CertEnumCertificatesInStore(store, cert);
1817         ok(!cert, "Expected only one cert\n");
1818         if (pCertEnumCRLsInStore)
1819         {
1820             crl = pCertEnumCRLsInStore(store, NULL);
1821             ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n",
1822              GetLastError());
1823             crl = pCertEnumCRLsInStore(store, crl);
1824             ok(!crl, "Expected only one CRL\n");
1825         }
1826         CertCloseStore(store, 0);
1827     }
1828 }
1829
1830 static void testCertOpenSystemStore(void)
1831 {
1832     HCERTSTORE store;
1833
1834     store = CertOpenSystemStoreW(0, NULL);
1835     ok(!store && GetLastError() == E_INVALIDARG,
1836      "Expected E_INVALIDARG, got %08x\n", GetLastError());
1837     /* This succeeds, and on WinXP at least, the Bogus key is created under
1838      * HKCU (but not under HKLM, even when run as an administrator.)
1839      */
1840     store = CertOpenSystemStoreW(0, BogusW);
1841     ok(store != 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
1842     if (store)
1843         CertCloseStore(store, 0);
1844     /* Delete it so other tests succeed next time around */
1845     store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1846      CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1847     RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1848 }
1849
1850 struct EnumSystemStoreInfo
1851 {
1852     BOOL  goOn;
1853     DWORD storeCount;
1854 };
1855
1856 static BOOL CALLBACK enumSystemStoreCB(const void *systemStore, DWORD dwFlags,
1857  PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved, void *pvArg)
1858 {
1859     struct EnumSystemStoreInfo *info = pvArg;
1860
1861     info->storeCount++;
1862     return info->goOn;
1863 }
1864
1865 static void testCertEnumSystemStore(void)
1866 {
1867     BOOL ret;
1868     struct EnumSystemStoreInfo info = { FALSE, 0 };
1869
1870     if (!pCertEnumSystemStore)
1871     {
1872         win_skip("CertEnumSystemStore() is not available\n");
1873         return;
1874     }
1875
1876     SetLastError(0xdeadbeef);
1877     ret = pCertEnumSystemStore(0, NULL, NULL, NULL);
1878     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1879      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1880     /* Crashes
1881     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, NULL,
1882      NULL);
1883      */
1884
1885     SetLastError(0xdeadbeef);
1886     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1887      enumSystemStoreCB);
1888     /* Callback returning FALSE stops enumeration */
1889     ok(!ret, "Expected CertEnumSystemStore to stop\n");
1890     ok(info.storeCount == 0 || info.storeCount == 1,
1891      "Expected 0 or 1 stores\n");
1892
1893     info.goOn = TRUE;
1894     info.storeCount = 0;
1895     ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1896      enumSystemStoreCB);
1897     ok(ret, "CertEnumSystemStore failed: %08x\n", GetLastError());
1898     /* There should always be at least My, Root, and CA stores */
1899     ok(info.storeCount == 0 || info.storeCount >= 3,
1900      "Expected at least 3 stores\n");
1901 }
1902
1903 static void testStoreProperty(void)
1904 {
1905     HCERTSTORE store;
1906     BOOL ret;
1907     DWORD propID, size = 0, state;
1908     CRYPT_DATA_BLOB blob;
1909
1910     if (!pCertGetStoreProperty || !pCertSetStoreProperty)
1911     {
1912         win_skip("CertGet/SetStoreProperty() is not available\n");
1913         return;
1914     }
1915
1916     /* Crash
1917     ret = pCertGetStoreProperty(NULL, 0, NULL, NULL);
1918     ret = pCertGetStoreProperty(NULL, 0, NULL, &size);
1919     ret = pCertGetStoreProperty(store, 0, NULL, NULL);
1920      */
1921
1922     store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1923      CERT_STORE_CREATE_NEW_FLAG, NULL);
1924     /* Check a missing prop ID */
1925     SetLastError(0xdeadbeef);
1926     ret = pCertGetStoreProperty(store, 0, NULL, &size);
1927     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1928      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1929     /* Contrary to MSDN, CERT_ACCESS_STATE_PROP_ID is supported for stores.. */
1930     size = sizeof(state);
1931     ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1932     ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1933      GetLastError());
1934     ok(!state, "Expected a non-persisted store\n");
1935     /* and CERT_STORE_LOCALIZED_NAME_PROP_ID isn't supported by default. */
1936     size = 0;
1937     ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1938      &size);
1939     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1940      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1941     /* Delete an arbitrary property on a store */
1942     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1943     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1944     /* Set an arbitrary property on a store */
1945     blob.pbData = (LPBYTE)&state;
1946     blob.cbData = sizeof(state);
1947     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, &blob);
1948     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1949     /* Get an arbitrary property that's been set */
1950     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1951     ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1952     ok(size == sizeof(state), "Unexpected data size %d\n", size);
1953     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, &propID, &size);
1954     ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1955     ok(propID == state, "CertGetStoreProperty got the wrong value\n");
1956     /* Delete it again */
1957     ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1958     ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1959     /* And check that it's missing */
1960     SetLastError(0xdeadbeef);
1961     ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1962     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1963      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1964     CertCloseStore(store, 0);
1965
1966     /* Recheck on the My store.. */
1967     store = CertOpenSystemStoreW(0, MyW);
1968     size = sizeof(state);
1969     ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1970     ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1971      GetLastError());
1972     ok(state, "Expected a persisted store\n");
1973     SetLastError(0xdeadbeef);
1974     size = 0;
1975     ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1976      &size);
1977     ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1978      "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1979     CertCloseStore(store, 0);
1980 }
1981
1982 static void testAddSerialized(void)
1983 {
1984     BOOL ret;
1985     HCERTSTORE store;
1986     BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
1987      { 0 };
1988     BYTE hash[20];
1989     struct CertPropIDHeader *hdr;
1990     PCCERT_CONTEXT context;
1991
1992     ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
1993     ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
1994      "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
1995
1996     store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1997      CERT_STORE_CREATE_NEW_FLAG, NULL);
1998     ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1999
2000     ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
2001     ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
2002      "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
2003
2004     /* Test with an empty property */
2005     hdr = (struct CertPropIDHeader *)buf;
2006     hdr->propID = CERT_CERT_PROP_ID;
2007     hdr->unknown1 = 1;
2008     hdr->cb = 0;
2009     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2010      NULL, NULL);
2011     ok(!ret && GetLastError() == E_INVALIDARG,
2012      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2013     /* Test with a bad size in property header */
2014     hdr->cb = sizeof(bigCert) - 1;
2015     memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert));
2016     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2017      NULL, NULL);
2018     ok(!ret && GetLastError() == E_INVALIDARG,
2019      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2020     ret = CertAddSerializedElementToStore(store, buf,
2021      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
2022      NULL);
2023     ok(!ret && GetLastError() == E_INVALIDARG,
2024      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2025     ret = CertAddSerializedElementToStore(store, buf,
2026      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2027      0, 0, NULL, NULL);
2028     ok(!ret && GetLastError() == E_INVALIDARG,
2029      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2030     /* Kosher size in property header, but no context type */
2031     hdr->cb = sizeof(bigCert);
2032     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2033      NULL, NULL);
2034     ok(!ret && GetLastError() == E_INVALIDARG,
2035      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2036     ret = CertAddSerializedElementToStore(store, buf,
2037      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
2038      NULL);
2039     ok(!ret && GetLastError() == E_INVALIDARG,
2040      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2041     ret = CertAddSerializedElementToStore(store, buf,
2042      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2043      0, 0, NULL, NULL);
2044     ok(!ret && GetLastError() == E_INVALIDARG,
2045      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2046     /* With a bad context type */
2047     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
2048      CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2049     ok(!ret && GetLastError() == E_INVALIDARG,
2050      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2051     ret = CertAddSerializedElementToStore(store, buf,
2052      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
2053      CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2054     ok(!ret && GetLastError() == E_INVALIDARG,
2055      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2056     ret = CertAddSerializedElementToStore(store, buf,
2057      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2058      0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2059     ok(!ret && GetLastError() == E_INVALIDARG,
2060      "Expected E_INVALIDARG, got %08x\n", GetLastError());
2061     /* Bad unknown field, good type */
2062     hdr->unknown1 = 2;
2063     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
2064      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2065     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2066      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2067     ret = CertAddSerializedElementToStore(store, buf,
2068      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
2069      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2070     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2071      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2072     ret = CertAddSerializedElementToStore(store, buf,
2073      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2074      0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2075     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2076      "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2077     /* Most everything okay, but bad add disposition */
2078     hdr->unknown1 = 1;
2079     /* This crashes
2080     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 
2081      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2082      * as does this
2083     ret = CertAddSerializedElementToStore(store, buf,
2084      sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 
2085      CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2086      */
2087     /* Everything okay, but buffer's too big */
2088     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
2089      CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2090     ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
2091     /* Everything okay, check it's not re-added */
2092     ret = CertAddSerializedElementToStore(store, buf,
2093      sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2094      0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2095     ok(!ret && GetLastError() == CRYPT_E_EXISTS,
2096      "Expected CRYPT_E_EXISTS, got %08x\n", GetLastError());
2097
2098     context = CertEnumCertificatesInStore(store, NULL);
2099     ok(context != NULL, "Expected a cert\n");
2100     if (context)
2101         CertDeleteCertificateFromStore(context);
2102
2103     /* Try adding with a bogus hash.  Oddly enough, it succeeds, and the hash,
2104      * when queried, is the real hash rather than the bogus hash.
2105      */
2106     hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
2107      sizeof(bigCert));
2108     hdr->propID = CERT_HASH_PROP_ID;
2109     hdr->unknown1 = 1;
2110     hdr->cb = sizeof(hash);
2111     memset(hash, 0xc, sizeof(hash));
2112     memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
2113     ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
2114      CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
2115      (const void **)&context);
2116     ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
2117     if (context)
2118     {
2119         BYTE hashVal[20], realHash[20];
2120         DWORD size = sizeof(hashVal);
2121
2122         ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert),
2123          realHash, &size);
2124         ok(ret, "CryptHashCertificate failed: %08x\n", GetLastError());
2125         ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
2126          hashVal, &size);
2127         ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2128          GetLastError());
2129         ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
2130         CertFreeCertificateContext(context);
2131     }
2132
2133     CertCloseStore(store, 0);
2134 }
2135
2136 static const BYTE serializedCertWithFriendlyName[] = {
2137 0x0b,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x57,0x00,0x69,
2138 0x00,0x6e,0x00,0x65,0x00,0x54,0x00,0x65,0x00,0x73,0x00,0x74,0x00,0x00,0x00,
2139 0x20,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,
2140 0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,
2141 0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,
2142 0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,
2143 0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,
2144 0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,
2145 0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,
2146 0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,
2147 0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,
2148 0x01 };
2149 static const BYTE serializedStoreWithCertWithFriendlyName[] = {
2150 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x0b,0x00,0x00,0x00,0x01,0x00,0x00,
2151 0x00,0x12,0x00,0x00,0x00,0x57,0x00,0x69,0x00,0x6e,0x00,0x65,0x00,0x54,0x00,
2152 0x65,0x00,0x73,0x00,0x74,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
2153 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
2154 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
2155 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
2156 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
2157 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
2158 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
2159 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
2160 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
2161 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
2162 0x00,0x00,0x00,0x00,0x00,0x00 };
2163 static const BYTE serializedStoreWithCertAndHash[] = {
2164 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x03,0x00,0x00,0x00,0x01,0x00,0x00,
2165 0x00,0x14,0x00,0x00,0x00,0x6e,0x30,0x90,0x71,0x5f,0xd9,0x23,0x56,0xeb,0xae,
2166 0x25,0x40,0xe6,0x22,0xda,0x19,0x26,0x02,0xa6,0x08,0x20,0x00,0x00,0x00,0x01,
2167 0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,
2168 0x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,
2169 0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,
2170 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,
2171 0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,
2172 0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
2173 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,
2174 0x01,0x00,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,
2175 0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,
2176 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
2177
2178 static void testAddCertificateLink(void)
2179 {
2180     BOOL ret;
2181     HCERTSTORE store1, store2;
2182     PCCERT_CONTEXT source, linked;
2183     DWORD size;
2184     LPBYTE buf;
2185     CERT_NAME_BLOB blob;
2186     static const WCHAR szPrefix[] = { 'c','e','r',0 };
2187     static const WCHAR szDot[] = { '.',0 };
2188     static const WCHAR WineTestW[] = { 'W','i','n','e','T','e','s','t',0 };
2189     WCHAR filename1[MAX_PATH], filename2[MAX_PATH];
2190     HANDLE file;
2191
2192     if (0)
2193     {
2194         /* Crashes, i.e. the store is dereferenced without checking. */
2195         ret = CertAddCertificateLinkToStore(NULL, NULL, 0, NULL);
2196     }
2197
2198     /* Adding a certificate link to a store requires a valid add disposition */
2199     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2200      CERT_STORE_CREATE_NEW_FLAG, NULL);
2201     SetLastError(0xdeadbeef);
2202     ret = CertAddCertificateLinkToStore(store1, NULL, 0, NULL);
2203     ok(!ret && GetLastError() == E_INVALIDARG,
2204      "expected E_INVALIDARG, got %08x\n", GetLastError());
2205     source = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
2206      sizeof(bigCert));
2207     SetLastError(0xdeadbeef);
2208     ret = CertAddCertificateLinkToStore(store1, source, 0, NULL);
2209     ok(!ret && GetLastError() == E_INVALIDARG,
2210      "expected E_INVALIDARG, got %08x\n", GetLastError());
2211     ret = CertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2212      NULL);
2213     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2214     if (0)
2215     {
2216         /* Crashes, i.e. the source certificate is dereferenced without
2217          * checking when a valid add disposition is given.
2218          */
2219         ret = CertAddCertificateLinkToStore(store1, NULL, CERT_STORE_ADD_ALWAYS,
2220          NULL);
2221     }
2222     CertCloseStore(store1, 0);
2223
2224     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2225      CERT_STORE_CREATE_NEW_FLAG, NULL);
2226     ret = CertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2227      &linked);
2228     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2229     if (ret)
2230     {
2231         ok(linked->hCertStore == store1, "unexpected store\n");
2232         ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2233         ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2234          GetLastError());
2235         buf = HeapAlloc(GetProcessHeap(), 0, size);
2236         if (buf)
2237         {
2238             ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2239             /* The serialized linked certificate is identical to the serialized
2240              * original certificate.
2241              */
2242             ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
2243             ok(!memcmp(serializedCert, buf, size),
2244              "Unexpected serialized cert\n");
2245             HeapFree(GetProcessHeap(), 0, buf);
2246         }
2247         /* Set a friendly name on the source certificate... */
2248         blob.pbData = (LPBYTE)WineTestW;
2249         blob.cbData = sizeof(WineTestW);
2250         ret = CertSetCertificateContextProperty(source,
2251          CERT_FRIENDLY_NAME_PROP_ID, 0, &blob);
2252         ok(ret, "CertSetCertificateContextProperty failed: %08x\n",
2253          GetLastError());
2254         /* and the linked certificate has the same friendly name. */
2255         ret = CertGetCertificateContextProperty(linked,
2256          CERT_FRIENDLY_NAME_PROP_ID, NULL, &size);
2257         ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2258          GetLastError());
2259         buf = HeapAlloc(GetProcessHeap(), 0, size);
2260         if (buf)
2261         {
2262             ret = CertGetCertificateContextProperty(linked,
2263              CERT_FRIENDLY_NAME_PROP_ID, buf, &size);
2264             ok(!lstrcmpW((LPCWSTR)buf, WineTestW),
2265              "unexpected friendly name\n");
2266             HeapFree(GetProcessHeap(), 0, buf);
2267         }
2268         CertFreeCertificateContext(linked);
2269     }
2270     CertFreeCertificateContext(source);
2271     CertCloseStore(store1, 0);
2272
2273     /* Test adding a cert to a file store, committing the change to the store,
2274      * and creating a link to the resulting cert.
2275      */
2276     if (!GetTempFileNameW(szDot, szPrefix, 0, filename1))
2277        return;
2278
2279     DeleteFileW(filename1);
2280     file = CreateFileW(filename1, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2281      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2282     if (file == INVALID_HANDLE_VALUE)
2283         return;
2284
2285     store1 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2286      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2287     ok(store1 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2288     CloseHandle(file);
2289
2290     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
2291      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &source);
2292     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
2293      GetLastError());
2294
2295     /* Test adding a link to a memory store. */
2296     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2297      CERT_STORE_CREATE_NEW_FLAG, NULL);
2298     ret = CertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2299      &linked);
2300     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2301     if (ret)
2302     {
2303         ok(linked->hCertStore == store2, "unexpected store\n");
2304         ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2305         ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2306          GetLastError());
2307         buf = HeapAlloc(GetProcessHeap(), 0, size);
2308         if (buf)
2309         {
2310             ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2311             /* The serialized linked certificate is identical to the serialized
2312              * original certificate.
2313              */
2314             ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
2315             ok(!memcmp(serializedCert, buf, size),
2316              "Unexpected serialized cert\n");
2317             HeapFree(GetProcessHeap(), 0, buf);
2318         }
2319         /* Set a friendly name on the source certificate... */
2320         blob.pbData = (LPBYTE)WineTestW;
2321         blob.cbData = sizeof(WineTestW);
2322         ret = CertSetCertificateContextProperty(source,
2323          CERT_FRIENDLY_NAME_PROP_ID, 0, &blob);
2324         ok(ret, "CertSetCertificateContextProperty failed: %08x\n",
2325          GetLastError());
2326         /* and the linked certificate has the same friendly name. */
2327         ret = CertGetCertificateContextProperty(linked,
2328          CERT_FRIENDLY_NAME_PROP_ID, NULL, &size);
2329         ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2330          GetLastError());
2331         buf = HeapAlloc(GetProcessHeap(), 0, size);
2332         if (buf)
2333         {
2334             ret = CertGetCertificateContextProperty(linked,
2335              CERT_FRIENDLY_NAME_PROP_ID, buf, &size);
2336             ok(!lstrcmpW((LPCWSTR)buf, WineTestW),
2337              "unexpected friendly name\n");
2338             HeapFree(GetProcessHeap(), 0, buf);
2339         }
2340         CertFreeCertificateContext(linked);
2341     }
2342     CertCloseStore(store2, 0);
2343
2344     if (!GetTempFileNameW(szDot, szPrefix, 0, filename2))
2345        return;
2346
2347     DeleteFileW(filename2);
2348     file = CreateFileW(filename2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2349      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2350     if (file == INVALID_HANDLE_VALUE)
2351         return;
2352
2353     store2 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2354      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2355     ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2356     CloseHandle(file);
2357     /* Test adding a link to a file store. */
2358     ret = CertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2359      &linked);
2360     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2361     if (ret)
2362     {
2363         ok(linked->hCertStore == store2, "unexpected store\n");
2364         ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2365         ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2366          GetLastError());
2367         buf = HeapAlloc(GetProcessHeap(), 0, size);
2368         if (buf)
2369         {
2370             ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2371             ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2372              GetLastError());
2373             /* The serialized linked certificate now contains the friendly
2374              * name property.
2375              */
2376             ok(size == sizeof(serializedCertWithFriendlyName),
2377              "Wrong size %d\n", size);
2378             ok(!memcmp(serializedCertWithFriendlyName, buf, size),
2379              "Unexpected serialized cert\n");
2380             HeapFree(GetProcessHeap(), 0, buf);
2381         }
2382         CertFreeCertificateContext(linked);
2383         compareStore(store2, "file store -> file store",
2384          serializedStoreWithCertWithFriendlyName,
2385          sizeof(serializedStoreWithCertWithFriendlyName), FALSE);
2386     }
2387     CertCloseStore(store2, 0);
2388     DeleteFileW(filename2);
2389
2390     CertFreeCertificateContext(source);
2391
2392     CertCloseStore(store1, 0);
2393     DeleteFileW(filename1);
2394
2395     /* Test adding a link to a system store (which is a collection store.) */
2396     store1 = CertOpenSystemStoreA(0, "My");
2397     source = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
2398      sizeof(bigCert));
2399     SetLastError(0xdeadbeef);
2400     ret = CertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2401      &linked);
2402     ok(!ret && GetLastError() == E_INVALIDARG,
2403      "expected E_INVALIDARG, got %08x\n", GetLastError());
2404     CertFreeCertificateContext(source);
2405
2406     /* Test adding a link to a file store, where the linked certificate is
2407      * in a system store.
2408      */
2409     ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
2410      bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &source);
2411     ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
2412      GetLastError());
2413     if (!GetTempFileNameW(szDot, szPrefix, 0, filename1))
2414        return;
2415
2416     DeleteFileW(filename1);
2417     file = CreateFileW(filename1, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2418      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2419     if (file == INVALID_HANDLE_VALUE)
2420         return;
2421
2422     store2 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2423      CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2424     ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2425     CloseHandle(file);
2426
2427     ret = CertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2428      &linked);
2429     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2430     if (ret)
2431     {
2432         ok(linked->hCertStore == store2, "unexpected store\n");
2433         ret = pCertControlStore(store2, 0, CERT_STORE_CTRL_COMMIT, NULL);
2434         ok(ret, "CertControlStore failed: %d\n", ret);
2435         compareStore(store2, "file store -> system store",
2436          serializedStoreWithCertAndHash,
2437          sizeof(serializedStoreWithCertAndHash), TRUE);
2438         CertFreeCertificateContext(linked);
2439     }
2440
2441     CertCloseStore(store2, 0);
2442     DeleteFileW(filename1);
2443
2444     /* Test adding a link to a registry store, where the linked certificate is
2445      * in a system store.
2446      */
2447     store2 = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
2448      CERT_SYSTEM_STORE_CURRENT_USER, WineTestW);
2449     ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2450     ret = CertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2451      &linked);
2452     ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2453     if (ret)
2454     {
2455         ok(linked->hCertStore == store2, "unexpected store\n");
2456         CertDeleteCertificateFromStore(linked);
2457     }
2458     CertCloseStore(store2, 0);
2459
2460     CertFreeCertificateContext(source);
2461     CertCloseStore(store1, 0);
2462 }
2463
2464 static DWORD countCertsInStore(HCERTSTORE store)
2465 {
2466     PCCERT_CONTEXT cert = NULL;
2467     DWORD certs = 0;
2468
2469     do {
2470         cert = CertEnumCertificatesInStore(store, cert);
2471         if (cert)
2472             certs++;
2473     } while (cert);
2474     return certs;
2475 }
2476
2477 static DWORD countCRLsInStore(HCERTSTORE store)
2478 {
2479     PCCRL_CONTEXT crl = NULL;
2480     DWORD crls = 0;
2481
2482     do {
2483         crl = pCertEnumCRLsInStore(store, crl);
2484         if (crl)
2485             crls++;
2486     } while (crl);
2487     return crls;
2488 }
2489
2490 static void test_I_UpdateStore(void)
2491 {
2492     HMODULE lib = GetModuleHandleA("crypt32");
2493     BOOL (WINAPI *pI_CertUpdatestore)(HCERTSTORE, HCERTSTORE, DWORD, DWORD) =
2494      (void *)GetProcAddress(lib, "I_CertUpdateStore");
2495     BOOL ret;
2496     HCERTSTORE store1, store2;
2497     PCCERT_CONTEXT cert;
2498     DWORD certs;
2499
2500     if (!pI_CertUpdatestore)
2501     {
2502         win_skip("No I_CertUpdateStore\n");
2503         return;
2504     }
2505     store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2506      CERT_STORE_CREATE_NEW_FLAG, NULL);
2507     store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2508      CERT_STORE_CREATE_NEW_FLAG, NULL);
2509
2510     /* Crash
2511     ret = pI_CertUpdatestore(NULL, NULL, 0, 0);
2512     ret = pI_CertUpdatestore(store1, NULL, 0, 0);
2513     ret = pI_CertUpdatestore(NULL, store2, 0, 0);
2514      */
2515     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2516     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2517
2518     CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING, bigCert,
2519      sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2520     /* I_CertUpdateStore adds the contexts from store2 to store1 */
2521     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2522     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2523     certs = countCertsInStore(store1);
2524     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2525     /* Calling it a second time has no effect */
2526     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2527     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2528     certs = countCertsInStore(store1);
2529     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2530
2531     /* The last parameters to I_CertUpdateStore appear to be ignored */
2532     ret = pI_CertUpdatestore(store1, store2, 1, 0);
2533     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2534     ret = pI_CertUpdatestore(store1, store2, 0, 1);
2535     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2536
2537     CertAddEncodedCRLToStore(store2, X509_ASN_ENCODING, signedCRL,
2538      sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
2539
2540     /* I_CertUpdateStore also adds the CRLs from store2 to store1 */
2541     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2542     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2543     certs = countCertsInStore(store1);
2544     ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2545     if (pCertEnumCRLsInStore)
2546     {
2547         certs = countCRLsInStore(store1);
2548         ok(certs == 1, "Expected 1 CRL, got %d\n", certs);
2549     }
2550
2551     CertDeleteCertificateFromStore(cert);
2552     /* If a context is deleted from store2, I_CertUpdateStore deletes it
2553      * from store1
2554      */
2555     ret = pI_CertUpdatestore(store1, store2, 0, 0);
2556     ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2557     certs = countCertsInStore(store1);
2558     ok(certs == 0, "Expected 0 certs, got %d\n", certs);
2559
2560     CertCloseStore(store1, 0);
2561     CertCloseStore(store2, 0);
2562 }
2563
2564 START_TEST(store)
2565 {
2566     HMODULE hdll;
2567
2568     hdll = GetModuleHandleA("Crypt32.dll");
2569     pCertAddStoreToCollection = (void*)GetProcAddress(hdll, "CertAddStoreToCollection");
2570     pCertControlStore = (void*)GetProcAddress(hdll, "CertControlStore");
2571     pCertEnumCRLsInStore = (void*)GetProcAddress(hdll, "CertEnumCRLsInStore");
2572     pCertEnumSystemStore = (void*)GetProcAddress(hdll, "CertEnumSystemStore");
2573     pCertGetStoreProperty = (void*)GetProcAddress(hdll, "CertGetStoreProperty");
2574     pCertRemoveStoreFromCollection = (void*)GetProcAddress(hdll, "CertRemoveStoreFromCollection");
2575     pCertSetStoreProperty = (void*)GetProcAddress(hdll, "CertSetStoreProperty");
2576
2577     /* various combinations of CertOpenStore */
2578     testMemStore();
2579     testCollectionStore();
2580     testRegStore();
2581     testSystemRegStore();
2582     testSystemStore();
2583     testFileStore();
2584     testFileNameStore();
2585     testMessageStore();
2586     testSerializedStore();
2587
2588     testCertOpenSystemStore();
2589     testCertEnumSystemStore();
2590     testStoreProperty();
2591
2592     testAddSerialized();
2593     testAddCertificateLink();
2594
2595     test_I_UpdateStore();
2596 }