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