gdi32: Reimplement Ellipse in paths to avoid calling imprecise arc helper functions.
[wine.git] / dlls / crypt32 / tests / store.c
blob44839368d81e14d73e7651a62fe3d9e17dadf4b9
1 /*
2 * crypt32 cert store function tests
4 * Copyright 2005-2006 Juan Lang
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdarg.h>
24 #include <windef.h>
25 #include <winbase.h>
26 #include <winreg.h>
27 #include <winerror.h>
28 #include <wincrypt.h>
30 #include "wine/test.h"
32 /* The following aren't defined in wincrypt.h, as they're "reserved" */
33 #define CERT_CERT_PROP_ID 32
34 #define CERT_CRL_PROP_ID 33
35 #define CERT_CTL_PROP_ID 34
37 struct CertPropIDHeader
39 DWORD propID;
40 DWORD unknown1;
41 DWORD cb;
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 static const BYTE signedCTLWithCTLInnerContent[] = {
97 0x30,0x82,0x01,0x0f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
98 0xa0,0x82,0x01,0x00,0x30,0x81,0xfd,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,
99 0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x30,0x06,0x09,
100 0x2b,0x06,0x01,0x04,0x01,0x82,0x37,0x0a,0x01,0xa0,0x23,0x30,0x21,0x30,0x00,
101 0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,
102 0x30,0x5a,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,
103 0x00,0x31,0x81,0xb5,0x30,0x81,0xb2,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,
104 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,
105 0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,
106 0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0xa0,0x3b,0x30,0x18,0x06,0x09,0x2a,0x86,
107 0x48,0x86,0xf7,0x0d,0x01,0x09,0x03,0x31,0x0b,0x06,0x09,0x2b,0x06,0x01,0x04,
108 0x01,0x82,0x37,0x0a,0x01,0x30,0x1f,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,
109 0x01,0x09,0x04,0x31,0x12,0x04,0x10,0x54,0x71,0xbc,0xe1,0x56,0x31,0xa2,0xf9,
110 0x65,0x70,0x34,0xf8,0xe2,0xe9,0xb4,0xf4,0x30,0x04,0x06,0x00,0x05,0x00,0x04,
111 0x40,0x2f,0x1b,0x9f,0x5a,0x4a,0x15,0x73,0xfa,0xb1,0x93,0x3d,0x09,0x52,0xdf,
112 0x6b,0x98,0x4b,0x13,0x5e,0xe7,0xbf,0x65,0xf4,0x9c,0xc2,0xb1,0x77,0x09,0xb1,
113 0x66,0x4d,0x72,0x0d,0xb1,0x1a,0x50,0x20,0xe0,0x57,0xa2,0x39,0xc7,0xcd,0x7f,
114 0x8e,0xe7,0x5f,0x76,0x2b,0xd1,0x6a,0x82,0xb3,0x30,0x25,0x61,0xf6,0x25,0x23,
115 0x57,0x6c,0x0b,0x47,0xb8 };
118 static BOOL (WINAPI *pCertAddStoreToCollection)(HCERTSTORE,HCERTSTORE,DWORD,DWORD);
119 static BOOL (WINAPI *pCertControlStore)(HCERTSTORE,DWORD,DWORD,void const*);
120 static PCCRL_CONTEXT (WINAPI *pCertEnumCRLsInStore)(HCERTSTORE,PCCRL_CONTEXT);
121 static BOOL (WINAPI *pCertEnumSystemStore)(DWORD,void*,void*,PFN_CERT_ENUM_SYSTEM_STORE);
122 static BOOL (WINAPI *pCertGetStoreProperty)(HCERTSTORE,DWORD,void*,DWORD*);
123 static void (WINAPI *pCertRemoveStoreFromCollection)(HCERTSTORE,HCERTSTORE);
124 static BOOL (WINAPI *pCertSetStoreProperty)(HCERTSTORE,DWORD,DWORD,const void*);
125 static BOOL (WINAPI *pCertAddCertificateLinkToStore)(HCERTSTORE,PCCERT_CONTEXT,DWORD,PCCERT_CONTEXT*);
127 #define test_store_is_empty(store) _test_store_is_empty(__LINE__,store)
128 static void _test_store_is_empty(unsigned line, HCERTSTORE store)
130 const CERT_CONTEXT *cert;
132 cert = CertEnumCertificatesInStore(store, NULL);
133 ok_(__FILE__,line)(!cert && GetLastError() == CRYPT_E_NOT_FOUND, "store is not empty\n");
136 static void testMemStore(void)
138 HCERTSTORE store1, store2;
139 PCCERT_CONTEXT context;
140 BOOL ret;
141 DWORD GLE;
143 /* NULL provider */
144 store1 = CertOpenStore(0, 0, 0, 0, NULL);
145 ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
146 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
147 /* weird flags */
148 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
149 CERT_STORE_DELETE_FLAG, NULL);
150 ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
151 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
153 /* normal */
154 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
155 CERT_STORE_CREATE_NEW_FLAG, NULL);
156 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
157 /* open existing doesn't */
158 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
159 CERT_STORE_OPEN_EXISTING_FLAG, NULL);
160 ok(store2 != NULL, "CertOpenStore failed: %d\n", GetLastError());
161 ok(store1 != store2, "Expected different stores\n");
163 /* add a bogus (empty) cert */
164 context = NULL;
165 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
166 sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
167 /* Windows returns CRYPT_E_ASN1_EOD or OSS_DATA_ERROR, but accept
168 * CRYPT_E_ASN1_CORRUPT as well (because matching errors is tough in this
169 * case)
171 GLE = GetLastError();
172 ok(!ret && (GLE == CRYPT_E_ASN1_EOD || GLE == CRYPT_E_ASN1_CORRUPT ||
173 GLE == OSS_DATA_ERROR),
174 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
175 GLE);
176 /* add a "signed" cert--the signature isn't a real signature, so this adds
177 * without any check of the signature's validity
179 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
180 signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
181 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
182 ok(context != NULL, "Expected a valid cert context\n");
183 if (context)
185 ok(context->cbCertEncoded == sizeof(signedBigCert),
186 "Wrong cert size %d\n", context->cbCertEncoded);
187 ok(!memcmp(context->pbCertEncoded, signedBigCert,
188 sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
189 /* remove it, the rest of the tests will work on an unsigned cert */
190 ret = CertDeleteCertificateFromStore(context);
191 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
192 GetLastError());
194 /* try adding a "signed" CRL as a cert */
195 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
196 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
197 GLE = GetLastError();
198 ok(!ret && (GLE == CRYPT_E_ASN1_BADTAG || GLE == CRYPT_E_ASN1_CORRUPT ||
199 GLE == OSS_DATA_ERROR),
200 "Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT or OSS_DATA_ERROR, got %08x\n",
201 GLE);
202 /* add a cert to store1 */
203 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
204 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
205 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
206 ok(context != NULL, "Expected a valid cert context\n");
207 if (context)
209 DWORD size;
210 BYTE *buf;
212 ok(context->cbCertEncoded == sizeof(bigCert),
213 "Wrong cert size %d\n", context->cbCertEncoded);
214 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
215 "Unexpected encoded cert in context\n");
216 ok(context->hCertStore == store1, "Unexpected store\n");
218 /* check serializing this element */
219 /* These crash
220 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
221 ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
222 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
224 /* apparently flags are ignored */
225 ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
226 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
227 GetLastError());
228 buf = HeapAlloc(GetProcessHeap(), 0, size);
229 if (buf)
231 ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
232 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n", GetLastError());
233 ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
234 ok(!memcmp(serializedCert, buf, size),
235 "Unexpected serialized cert\n");
236 HeapFree(GetProcessHeap(), 0, buf);
239 ret = CertFreeCertificateContext(context);
240 ok(ret, "CertFreeCertificateContext failed: %08x\n", GetLastError());
242 /* verify the cert's in store1 */
243 context = CertEnumCertificatesInStore(store1, NULL);
244 ok(context != NULL, "Expected a valid context\n");
245 context = CertEnumCertificatesInStore(store1, context);
246 ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
247 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
248 /* verify store2 (the "open existing" mem store) is still empty */
249 context = CertEnumCertificatesInStore(store2, NULL);
250 ok(!context, "Expected an empty store\n");
251 /* delete the cert from store1, and check it's empty */
252 context = CertEnumCertificatesInStore(store1, NULL);
253 if (context)
255 /* Deleting a bitwise copy crashes with an access to an uninitialized
256 * pointer, so a cert context has some special data out there in memory
257 * someplace
258 CERT_CONTEXT copy;
259 memcpy(&copy, context, sizeof(copy));
260 ret = CertDeleteCertificateFromStore(&copy);
262 PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
264 ok(copy != NULL, "CertDuplicateCertificateContext failed: %08x\n",
265 GetLastError());
266 ret = CertDeleteCertificateFromStore(context);
267 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
268 GetLastError());
269 /* try deleting a copy */
270 ret = CertDeleteCertificateFromStore(copy);
271 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
272 GetLastError());
273 /* check that the store is empty */
274 context = CertEnumCertificatesInStore(store1, NULL);
275 ok(!context, "Expected an empty store\n");
278 /* close an empty store */
279 ret = CertCloseStore(NULL, 0);
280 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
281 ret = CertCloseStore(store1, 0);
282 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
283 ret = CertCloseStore(store2, 0);
284 ok(ret, "CertCloseStore failed: %d\n", GetLastError());
286 /* This seems nonsensical, but you can open a read-only mem store, only
287 * it isn't read-only
289 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
290 CERT_STORE_READONLY_FLAG, NULL);
291 ok(store1 != NULL, "CertOpenStore failed: %d\n", GetLastError());
292 /* yep, this succeeds */
293 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
294 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
295 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
296 ok(context != NULL, "Expected a valid cert context\n");
297 if (context)
299 ok(context->cbCertEncoded == sizeof(bigCert),
300 "Wrong cert size %d\n", context->cbCertEncoded);
301 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert)),
302 "Unexpected encoded cert in context\n");
303 ok(context->hCertStore == store1, "Unexpected store\n");
304 ret = CertDeleteCertificateFromStore(context);
305 ok(ret, "CertDeleteCertificateFromStore failed: %08x\n",
306 GetLastError());
308 CertCloseStore(store1, 0);
311 static void compareStore(HCERTSTORE store, LPCSTR name, const BYTE *pb,
312 DWORD cb, BOOL todo)
314 BOOL ret;
315 CRYPT_DATA_BLOB blob = { 0, NULL };
317 ret = CertSaveStore(store, X509_ASN_ENCODING, CERT_STORE_SAVE_AS_STORE,
318 CERT_STORE_SAVE_TO_MEMORY, &blob, 0);
319 ok(ret, "CertSaveStore failed: %08x\n", GetLastError());
320 todo_wine_if (todo)
321 ok(blob.cbData == cb, "%s: expected size %d, got %d\n", name, cb,
322 blob.cbData);
323 blob.pbData = HeapAlloc(GetProcessHeap(), 0, blob.cbData);
324 if (blob.pbData)
326 ret = CertSaveStore(store, X509_ASN_ENCODING, CERT_STORE_SAVE_AS_STORE,
327 CERT_STORE_SAVE_TO_MEMORY, &blob, 0);
328 ok(ret, "CertSaveStore failed: %08x\n", GetLastError());
329 todo_wine_if (todo)
330 ok(!memcmp(pb, blob.pbData, cb), "%s: unexpected value\n", name);
331 HeapFree(GetProcessHeap(), 0, blob.pbData);
335 static const BYTE serializedStoreWithCert[] = {
336 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
337 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
338 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
339 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
340 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
341 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
342 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
343 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
344 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
345 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
346 0x00,0x00,0x00,0x00,0x00,0x00 };
348 static void testCollectionStore(void)
350 HCERTSTORE store1, store2, collection, collection2;
351 PCCERT_CONTEXT context;
352 BOOL ret;
353 static const WCHAR szPrefix[] = { 'c','e','r',0 };
354 static const WCHAR szDot[] = { '.',0 };
355 WCHAR filename[MAX_PATH];
356 HANDLE file;
358 if (!pCertAddStoreToCollection)
360 win_skip("CertAddStoreToCollection() is not available\n");
361 return;
364 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
365 CERT_STORE_CREATE_NEW_FLAG, NULL);
367 /* Try adding a cert to any empty collection */
368 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
369 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
370 ok(!ret && GetLastError() == E_ACCESSDENIED,
371 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
373 /* Create and add a cert to a memory store */
374 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
375 CERT_STORE_CREATE_NEW_FLAG, NULL);
376 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
377 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
378 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
379 /* Add the memory store to the collection, without allowing adding */
380 ret = pCertAddStoreToCollection(collection, store1, 0, 0);
381 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
382 /* Verify the cert is in the collection */
383 context = CertEnumCertificatesInStore(collection, NULL);
384 ok(context != NULL, "Expected a valid context\n");
385 if (context)
387 ok(context->hCertStore == collection, "Unexpected store\n");
388 CertFreeCertificateContext(context);
390 /* Check that adding to the collection isn't allowed */
391 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
392 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
393 ok(!ret && GetLastError() == E_ACCESSDENIED,
394 "Expected E_ACCESSDENIED, got %08x\n", GetLastError());
396 /* Create a new memory store */
397 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
398 CERT_STORE_CREATE_NEW_FLAG, NULL);
399 /* Try adding a store to a non-collection store */
400 ret = pCertAddStoreToCollection(store1, store2,
401 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
402 ok(!ret && GetLastError() == E_INVALIDARG,
403 "Expected E_INVALIDARG, got %08x\n", GetLastError());
404 /* Try adding some bogus stores */
405 /* This crashes in Windows
406 ret = pCertAddStoreToCollection(0, store2,
407 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
409 /* This "succeeds"... */
410 ret = pCertAddStoreToCollection(collection, 0,
411 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
412 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
413 /* while this crashes.
414 ret = pCertAddStoreToCollection(collection, 1,
415 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
418 /* Add it to the collection, this time allowing adding */
419 ret = pCertAddStoreToCollection(collection, store2,
420 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
421 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
422 /* Check that adding to the collection is allowed */
423 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
424 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
425 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
426 /* Now check that it was actually added to store2 */
427 context = CertEnumCertificatesInStore(store2, NULL);
428 ok(context != NULL, "Expected a valid context\n");
429 if (context)
431 ok(context->hCertStore == store2, "Unexpected store\n");
432 CertFreeCertificateContext(context);
434 /* Check that the collection has both bigCert and bigCert2. bigCert comes
435 * first because store1 was added first.
437 context = CertEnumCertificatesInStore(collection, NULL);
438 ok(context != NULL, "Expected a valid context\n");
439 if (context)
441 ok(context->hCertStore == collection, "Unexpected store\n");
442 ok(context->cbCertEncoded == sizeof(bigCert),
443 "Wrong size %d\n", context->cbCertEncoded);
444 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
445 "Unexpected cert\n");
446 context = CertEnumCertificatesInStore(collection, context);
447 ok(context != NULL, "Expected a valid context\n");
448 if (context)
450 ok(context->hCertStore == collection, "Unexpected store\n");
451 ok(context->cbCertEncoded == sizeof(bigCert2),
452 "Wrong size %d\n", context->cbCertEncoded);
453 ok(!memcmp(context->pbCertEncoded, bigCert2,
454 context->cbCertEncoded), "Unexpected cert\n");
455 context = CertEnumCertificatesInStore(collection, context);
456 ok(!context, "Unexpected cert\n");
459 /* close store2, and check that the collection is unmodified */
460 CertCloseStore(store2, 0);
461 context = CertEnumCertificatesInStore(collection, NULL);
462 ok(context != NULL, "Expected a valid context\n");
463 if (context)
465 ok(context->hCertStore == collection, "Unexpected store\n");
466 ok(context->cbCertEncoded == sizeof(bigCert),
467 "Wrong size %d\n", context->cbCertEncoded);
468 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
469 "Unexpected cert\n");
470 context = CertEnumCertificatesInStore(collection, context);
471 ok(context != NULL, "Expected a valid context\n");
472 if (context)
474 ok(context->hCertStore == collection, "Unexpected store\n");
475 ok(context->cbCertEncoded == sizeof(bigCert2),
476 "Wrong size %d\n", context->cbCertEncoded);
477 ok(!memcmp(context->pbCertEncoded, bigCert2,
478 context->cbCertEncoded), "Unexpected cert\n");
479 context = CertEnumCertificatesInStore(collection, context);
480 ok(!context, "Unexpected cert\n");
484 /* Adding a collection to a collection is legal */
485 collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
486 CERT_STORE_CREATE_NEW_FLAG, NULL);
487 ret = pCertAddStoreToCollection(collection2, collection,
488 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
489 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
490 /* check the contents of collection2 */
491 context = CertEnumCertificatesInStore(collection2, NULL);
492 ok(context != NULL, "Expected a valid context\n");
493 if (context)
495 ok(context->hCertStore == collection2, "Unexpected store\n");
496 ok(context->cbCertEncoded == sizeof(bigCert),
497 "Wrong size %d\n", context->cbCertEncoded);
498 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
499 "Unexpected cert\n");
500 context = CertEnumCertificatesInStore(collection2, context);
501 ok(context != NULL, "Expected a valid context\n");
502 if (context)
504 ok(context->hCertStore == collection2, "Unexpected store\n");
505 ok(context->cbCertEncoded == sizeof(bigCert2),
506 "Wrong size %d\n", context->cbCertEncoded);
507 ok(!memcmp(context->pbCertEncoded, bigCert2,
508 context->cbCertEncoded), "Unexpected cert\n");
509 context = CertEnumCertificatesInStore(collection2, context);
510 ok(!context, "Unexpected cert\n");
514 /* I'd like to test closing the collection in the middle of enumeration,
515 * but my tests have been inconsistent. The first time calling
516 * CertEnumCertificatesInStore on a closed collection succeeded, while the
517 * second crashed. So anything appears to be fair game.
518 * I'd also like to test removing a store from a collection in the middle
519 * of an enumeration, but my tests in Windows have been inconclusive.
520 * In one scenario it worked. In another scenario, about a third of the
521 * time this leads to "random" crashes elsewhere in the code. This
522 * probably means this is not allowed.
525 CertCloseStore(store1, 0);
526 CertCloseStore(collection, 0);
527 CertCloseStore(collection2, 0);
529 /* Add the same cert to two memory stores, then put them in a collection */
530 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
531 CERT_STORE_CREATE_NEW_FLAG, NULL);
532 ok(store1 != 0, "CertOpenStore failed: %08x\n", GetLastError());
533 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
534 CERT_STORE_CREATE_NEW_FLAG, NULL);
535 ok(store2 != 0, "CertOpenStore failed: %08x\n", GetLastError());
537 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
538 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
539 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
540 ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
541 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
542 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
543 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
544 CERT_STORE_CREATE_NEW_FLAG, NULL);
545 ok(collection != 0, "CertOpenStore failed: %08x\n", GetLastError());
547 ret = pCertAddStoreToCollection(collection, store1,
548 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
549 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
550 ret = pCertAddStoreToCollection(collection, store2,
551 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
552 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
554 /* Check that the collection has two copies of the same cert */
555 context = CertEnumCertificatesInStore(collection, NULL);
556 ok(context != NULL, "Expected a valid context\n");
557 if (context)
559 ok(context->hCertStore == collection, "Unexpected store\n");
560 ok(context->cbCertEncoded == sizeof(bigCert),
561 "Wrong size %d\n", context->cbCertEncoded);
562 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
563 "Unexpected cert\n");
564 context = CertEnumCertificatesInStore(collection, context);
565 ok(context != NULL, "Expected a valid context\n");
566 if (context)
568 ok(context->hCertStore == collection, "Unexpected store\n");
569 ok(context->cbCertEncoded == sizeof(bigCert),
570 "Wrong size %d\n", context->cbCertEncoded);
571 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
572 "Unexpected cert\n");
573 context = CertEnumCertificatesInStore(collection, context);
574 ok(context == NULL, "Unexpected cert\n");
578 /* The following would check whether I can delete an identical cert, rather
579 * than one enumerated from the store. It crashes, so that means I must
580 * only call CertDeleteCertificateFromStore with contexts enumerated from
581 * the store.
582 context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
583 sizeof(bigCert));
584 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
585 GetLastError());
586 if (context)
588 ret = CertDeleteCertificateFromStore(collection, context);
589 printf("ret is %d, GetLastError is %08x\n", ret, GetLastError());
590 CertFreeCertificateContext(context);
594 /* Now check deleting from the collection. */
595 context = CertEnumCertificatesInStore(collection, NULL);
596 ok(context != NULL, "Expected a valid context\n");
597 if (context)
599 CertDeleteCertificateFromStore(context);
600 /* store1 should now be empty */
601 context = CertEnumCertificatesInStore(store1, NULL);
602 ok(!context, "Unexpected cert\n");
603 /* and there should be one certificate in the collection */
604 context = CertEnumCertificatesInStore(collection, NULL);
605 ok(context != NULL, "Expected a valid cert\n");
606 if (context)
608 ok(context->hCertStore == collection, "Unexpected store\n");
609 ok(context->cbCertEncoded == sizeof(bigCert),
610 "Wrong size %d\n", context->cbCertEncoded);
611 ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
612 "Unexpected cert\n");
614 context = CertEnumCertificatesInStore(collection, context);
615 ok(context == NULL, "Unexpected cert\n");
618 if (!pCertRemoveStoreFromCollection)
620 win_skip("CertRemoveStoreFromCollection() is not available\n");
622 else
624 /* Finally, test removing stores from the collection. No return
625 * value, so it's a bit funny to test.
627 /* This crashes
628 * pCertRemoveStoreFromCollection(NULL, NULL);
630 /* This "succeeds," no crash, no last error set */
631 SetLastError(0xdeadbeef);
632 pCertRemoveStoreFromCollection(store2, collection);
633 ok(GetLastError() == 0xdeadbeef,
634 "Didn't expect an error to be set: %08x\n", GetLastError());
636 /* After removing store2, the collection should be empty */
637 SetLastError(0xdeadbeef);
638 pCertRemoveStoreFromCollection(collection, store2);
639 ok(GetLastError() == 0xdeadbeef,
640 "Didn't expect an error to be set: %08x\n", GetLastError());
641 context = CertEnumCertificatesInStore(collection, NULL);
642 ok(!context, "Unexpected cert\n");
645 CertCloseStore(collection, 0);
646 CertCloseStore(store2, 0);
647 CertCloseStore(store1, 0);
649 /* Test adding certificates to and deleting certificates from collections.
651 store1 = CertOpenSystemStoreA(0, "My");
652 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
653 CERT_STORE_CREATE_NEW_FLAG, NULL);
655 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
656 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
657 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
658 CertDeleteCertificateFromStore(context);
660 pCertAddStoreToCollection(collection, store1,
661 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
663 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
664 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &context);
665 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n", GetLastError());
666 CertDeleteCertificateFromStore(context);
668 CertCloseStore(collection, 0);
669 CertCloseStore(store1, 0);
671 /* Test whether a collection store can be committed */
672 if (!pCertControlStore)
674 win_skip("CertControlStore() is not available\n");
675 return;
677 collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
678 CERT_STORE_CREATE_NEW_FLAG, NULL);
680 SetLastError(0xdeadbeef);
681 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
682 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
684 /* Adding a mem store that can't be committed prevents a successful commit.
686 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
687 CERT_STORE_CREATE_NEW_FLAG, NULL);
688 pCertAddStoreToCollection(collection, store1, 0, 0);
689 SetLastError(0xdeadbeef);
690 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
691 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
692 "expected ERROR_CALL_NOT_IMPLEMENTED, got %d\n", GetLastError());
693 pCertRemoveStoreFromCollection(collection, store1);
694 CertCloseStore(store1, 0);
696 /* Test adding a cert to a collection with a file store, committing the
697 * change to the collection, and comparing the resulting file.
699 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
700 return;
702 DeleteFileW(filename);
703 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
704 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
705 if (file == INVALID_HANDLE_VALUE)
706 return;
708 store1 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
709 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
710 ok(store1 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
711 CloseHandle(file);
712 pCertAddStoreToCollection(collection, store1,
713 CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
714 CertCloseStore(store1, 0);
716 ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
717 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
718 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
719 GetLastError());
720 ret = pCertControlStore(collection, 0, CERT_STORE_CTRL_COMMIT, NULL);
721 ok(ret, "CertControlStore failed: %d\n", ret);
722 compareStore(collection, "serialized store with cert",
723 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
724 CertCloseStore(collection, 0);
726 DeleteFileW(filename);
729 /* Looks for the property with ID propID in the buffer buf. Returns a pointer
730 * to its header if found, NULL if not.
732 static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
733 DWORD propID)
735 const struct CertPropIDHeader *ret = NULL;
736 BOOL failed = FALSE;
738 while (size && !ret && !failed)
740 if (size < sizeof(struct CertPropIDHeader))
741 failed = TRUE;
742 else
744 const struct CertPropIDHeader *hdr =
745 (const struct CertPropIDHeader *)buf;
747 size -= sizeof(struct CertPropIDHeader);
748 buf += sizeof(struct CertPropIDHeader);
749 if (size < hdr->cb)
750 failed = TRUE;
751 else if (hdr->propID == propID)
752 ret = hdr;
753 else
755 buf += hdr->cb;
756 size -= hdr->cb;
760 return ret;
763 static void testRegStore(void)
765 static const char tempKey[] = "Software\\Wine\\CryptTemp";
766 HCERTSTORE store;
767 LONG rc;
768 HKEY key = NULL;
769 DWORD disp, GLE;
771 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
772 GLE = GetLastError();
773 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
774 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
775 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
776 GLE = GetLastError();
777 ok(!store && (GLE == ERROR_INVALID_HANDLE || GLE == ERROR_BADKEY),
778 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", GLE);
780 /* Opening up any old key works.. */
781 key = HKEY_CURRENT_USER;
782 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
783 /* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
785 todo_wine ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
786 CertCloseStore(store, 0);
788 /* It looks like the remainder pretty much needs CertControlStore() */
789 if (!pCertControlStore)
791 win_skip("CertControlStore() is not available\n");
792 return;
795 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
796 NULL, &key, NULL);
797 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
798 if (key)
800 BOOL ret;
801 BYTE hash[20];
802 DWORD size, i;
803 static const char certificates[] = "Certificates\\";
804 char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
805 HKEY subKey;
806 PCCERT_CONTEXT context;
808 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
809 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
810 /* Add a certificate. It isn't persisted right away, since it's only
811 * added to the cache..
813 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
814 bigCert2, sizeof(bigCert2), CERT_STORE_ADD_ALWAYS, NULL);
815 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
816 GetLastError());
817 /* so flush the cache to force a commit.. */
818 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
819 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
820 /* and check that the expected subkey was written. */
821 size = sizeof(hash);
822 ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2),
823 hash, &size);
824 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
825 strcpy(subKeyName, certificates);
826 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
827 i++, ptr += 2)
828 sprintf(ptr, "%02X", hash[i]);
829 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
830 &subKey, NULL);
831 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
832 if (subKey)
834 LPBYTE buf;
836 size = 0;
837 RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
838 buf = HeapAlloc(GetProcessHeap(), 0, size);
839 if (buf)
841 rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
842 ok(!rc, "RegQueryValueExA failed: %d\n", rc);
843 if (!rc)
845 const struct CertPropIDHeader *hdr;
847 /* Both the hash and the cert should be present */
848 hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
849 ok(hdr != NULL, "Expected to find a cert property\n");
850 if (hdr)
852 ok(hdr->cb == sizeof(bigCert2),
853 "Wrong size %d of cert property\n", hdr->cb);
854 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), bigCert2,
855 hdr->cb), "Unexpected cert in cert property\n");
857 hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
858 ok(hdr != NULL, "Expected to find a hash property\n");
859 if (hdr)
861 ok(hdr->cb == sizeof(hash),
862 "Wrong size %d of hash property\n", hdr->cb);
863 ok(!memcmp((const BYTE *)hdr + sizeof(*hdr), hash,
864 hdr->cb), "Unexpected hash in cert property\n");
867 HeapFree(GetProcessHeap(), 0, buf);
869 RegCloseKey(subKey);
872 /* Remove the existing context */
873 context = CertEnumCertificatesInStore(store, NULL);
874 ok(context != NULL, "Expected a cert context\n");
875 if (context)
876 CertDeleteCertificateFromStore(context);
877 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
878 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
880 /* Add a serialized cert with a bogus hash directly to the registry */
881 memset(hash, 0, sizeof(hash));
882 strcpy(subKeyName, certificates);
883 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
884 i < sizeof(hash); i++, ptr += 2)
885 sprintf(ptr, "%02X", hash[i]);
886 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
887 &subKey, NULL);
888 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
889 if (subKey)
891 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
892 sizeof(bigCert)], *ptr;
893 DWORD certCount = 0;
894 struct CertPropIDHeader *hdr;
896 hdr = (struct CertPropIDHeader *)buf;
897 hdr->propID = CERT_HASH_PROP_ID;
898 hdr->unknown1 = 1;
899 hdr->cb = sizeof(hash);
900 ptr = buf + sizeof(*hdr);
901 memcpy(ptr, hash, sizeof(hash));
902 ptr += sizeof(hash);
903 hdr = (struct CertPropIDHeader *)ptr;
904 hdr->propID = CERT_CERT_PROP_ID;
905 hdr->unknown1 = 1;
906 hdr->cb = sizeof(bigCert);
907 ptr += sizeof(*hdr);
908 memcpy(ptr, bigCert, sizeof(bigCert));
910 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
911 sizeof(buf));
912 ok(!rc, "RegSetValueExA failed: %d\n", rc);
914 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
915 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
917 /* Make sure the bogus hash cert gets loaded. */
918 certCount = 0;
919 context = NULL;
920 do {
921 context = CertEnumCertificatesInStore(store, context);
922 if (context)
923 certCount++;
924 } while (context != NULL);
925 ok(certCount == 1, "Expected 1 certificates, got %d\n", certCount);
927 RegCloseKey(subKey);
930 /* Add another serialized cert directly to the registry, this time
931 * under the correct key name (named with the correct hash value).
933 size = sizeof(hash);
934 ret = CryptHashCertificate(0, 0, 0, bigCert2,
935 sizeof(bigCert2), hash, &size);
936 ok(ret, "CryptHashCertificate failed: %d\n", GetLastError());
937 strcpy(subKeyName, certificates);
938 for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
939 i < sizeof(hash); i++, ptr += 2)
940 sprintf(ptr, "%02X", hash[i]);
941 rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
942 &subKey, NULL);
943 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
944 if (subKey)
946 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
947 sizeof(bigCert2)], *ptr;
948 DWORD certCount = 0;
949 PCCERT_CONTEXT context;
950 struct CertPropIDHeader *hdr;
952 /* First try with a bogus hash... */
953 hdr = (struct CertPropIDHeader *)buf;
954 hdr->propID = CERT_HASH_PROP_ID;
955 hdr->unknown1 = 1;
956 hdr->cb = sizeof(hash);
957 ptr = buf + sizeof(*hdr);
958 memset(ptr, 0, sizeof(hash));
959 ptr += sizeof(hash);
960 hdr = (struct CertPropIDHeader *)ptr;
961 hdr->propID = CERT_CERT_PROP_ID;
962 hdr->unknown1 = 1;
963 hdr->cb = sizeof(bigCert2);
964 ptr += sizeof(*hdr);
965 memcpy(ptr, bigCert2, sizeof(bigCert2));
967 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
968 sizeof(buf));
969 ok(!rc, "RegSetValueExA failed: %d\n", rc);
971 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
972 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
974 /* and make sure just one cert still gets loaded. */
975 certCount = 0;
976 context = NULL;
977 do {
978 context = CertEnumCertificatesInStore(store, context);
979 if (context)
980 certCount++;
981 } while (context != NULL);
982 ok(certCount == 1 ||
983 broken(certCount == 2) /* NT4 */ ,
984 "Expected 1 certificates, got %d\n", certCount);
986 /* Try again with the correct hash... */
987 ptr = buf + sizeof(*hdr);
988 memcpy(ptr, hash, sizeof(hash));
990 rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
991 sizeof(buf));
992 ok(!rc, "RegSetValueExA failed: %d\n", rc);
994 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
995 ok(ret, "CertControlStore failed: %08x\n", GetLastError());
997 /* and make sure two certs get loaded. */
998 certCount = 0;
999 context = NULL;
1000 do {
1001 context = CertEnumCertificatesInStore(store, context);
1002 if (context)
1003 certCount++;
1004 } while (context != NULL);
1005 ok(certCount == 2, "Expected 2 certificates, got %d\n", certCount);
1007 RegCloseKey(subKey);
1009 CertCloseStore(store, 0);
1010 /* Is delete allowed on a reg store? */
1011 store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
1012 CERT_STORE_DELETE_FLAG, key);
1013 ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
1014 ok(GetLastError() == 0, "CertOpenStore failed: %08x\n",
1015 GetLastError());
1017 RegCloseKey(key);
1019 /* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
1020 * contents of the key, but not the key itself.
1022 rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
1023 NULL, &key, &disp);
1024 ok(!rc, "RegCreateKeyExA failed: %d\n", rc);
1025 ok(disp == REG_OPENED_EXISTING_KEY,
1026 "Expected REG_OPENED_EXISTING_KEY, got %d\n", disp);
1027 if (!rc)
1029 RegCloseKey(key);
1030 rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
1031 if (rc)
1033 HMODULE shlwapi = LoadLibraryA("shlwapi");
1035 /* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
1036 * otherwise subsequent tests will fail.
1038 if (shlwapi)
1040 DWORD (WINAPI *pSHDeleteKeyA)(HKEY, LPCSTR);
1042 pSHDeleteKeyA = (void*)GetProcAddress(shlwapi, "SHDeleteKeyA");
1043 if (pSHDeleteKeyA)
1044 pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
1045 FreeLibrary(shlwapi);
1051 static const char MyA[] = { 'M','y',0,0 };
1052 static const WCHAR MyW[] = { 'M','y',0 };
1053 static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
1054 static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
1055 'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
1056 't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
1058 static void testSystemRegStore(void)
1060 HCERTSTORE store, memStore;
1062 /* Check with a UNICODE name */
1063 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1064 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1065 /* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
1066 * testing if they don't.
1068 if (!store)
1069 return;
1071 /* Check that it isn't a collection store */
1072 memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1073 CERT_STORE_CREATE_NEW_FLAG, NULL);
1074 if (memStore)
1076 if (pCertAddStoreToCollection)
1078 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1079 ok(!ret && GetLastError() == E_INVALIDARG,
1080 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1082 CertCloseStore(memStore, 0);
1084 CertCloseStore(store, 0);
1086 /* Check opening a bogus store */
1087 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1088 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1089 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1090 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1091 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1092 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1093 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1094 if (store)
1095 CertCloseStore(store, 0);
1096 /* Now check whether deleting is allowed */
1097 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1098 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1099 ok(!store, "CertOpenStore failed: %08x\n", GetLastError());
1100 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1102 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
1103 ok(!store && GetLastError() == E_INVALIDARG,
1104 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1105 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1106 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1107 ok(!store && GetLastError() == E_INVALIDARG,
1108 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1109 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1110 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1111 ok(!store && GetLastError() == E_INVALIDARG,
1112 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1113 /* The name is expected to be UNICODE, check with an ASCII name */
1114 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
1115 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1116 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1117 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1120 static void testSystemStore(void)
1122 static const WCHAR baskslashW[] = { '\\',0 };
1123 HCERTSTORE store;
1124 WCHAR keyName[MAX_PATH];
1125 HKEY key;
1126 LONG rc;
1128 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
1129 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1130 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1131 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1132 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
1133 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1134 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1135 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1136 CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
1137 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1138 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1139 /* The name is expected to be UNICODE, first check with an ASCII name */
1140 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1141 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
1142 ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
1143 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1144 /* Create the expected key */
1145 lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
1146 lstrcatW(keyName, baskslashW);
1147 lstrcatW(keyName, MyW);
1148 rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
1149 NULL, &key, NULL);
1150 ok(!rc, "RegCreateKeyEx failed: %d\n", rc);
1151 if (!rc)
1152 RegCloseKey(key);
1153 /* Check opening with a UNICODE name, specifying the create new flag */
1154 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1155 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
1156 ok(!store && GetLastError() == ERROR_FILE_EXISTS,
1157 "Expected ERROR_FILE_EXISTS, got %08x\n", GetLastError());
1158 /* Now check opening with a UNICODE name, this time opening existing */
1159 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1160 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
1161 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1162 if (store)
1164 HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1165 CERT_STORE_CREATE_NEW_FLAG, NULL);
1167 /* Check that it's a collection store */
1168 if (memStore)
1170 if (pCertAddStoreToCollection)
1172 BOOL ret = pCertAddStoreToCollection(store, memStore, 0, 0);
1173 /* FIXME: this'll fail on NT4, but what error will it give? */
1174 ok(ret, "CertAddStoreToCollection failed: %08x\n", GetLastError());
1176 CertCloseStore(memStore, 0);
1178 CertCloseStore(store, 0);
1181 /* Check opening a bogus store */
1182 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1183 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
1184 ok(!store, "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1185 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1186 CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
1187 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
1188 if (store)
1189 CertCloseStore(store, 0);
1190 /* Now check whether deleting is allowed */
1191 store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1192 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1193 ok(!store, "Didn't expect a store to be returned when deleting\n");
1194 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1197 static const BYTE serializedStoreWithCertAndCRL[] = {
1198 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
1199 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
1200 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1201 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
1202 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
1203 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
1204 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1205 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
1206 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
1207 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x21,0x00,0x00,0x00,0x01,0x00,
1208 0x00,0x00,0x47,0x00,0x00,0x00,0x30,0x45,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,
1209 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
1210 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1211 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x02,0x06,0x00,0x03,0x11,
1212 0x00,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,
1213 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1215 static void testFileStore(void)
1217 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1218 static const WCHAR szDot[] = { '.',0 };
1219 WCHAR filename[MAX_PATH];
1220 HCERTSTORE store;
1221 BOOL ret;
1222 PCCERT_CONTEXT cert;
1223 HANDLE file;
1225 if (!pCertControlStore)
1227 win_skip("CertControlStore() is not available\n");
1228 return;
1231 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, 0, NULL);
1232 ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
1233 "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1235 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1236 return;
1238 DeleteFileW(filename);
1239 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1240 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1241 if (file == INVALID_HANDLE_VALUE)
1242 return;
1244 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0, CERT_STORE_DELETE_FLAG,
1245 file);
1246 ok(!store && GetLastError() == E_INVALIDARG,
1247 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1248 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1249 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, file);
1250 ok(!store && GetLastError() == E_INVALIDARG,
1251 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1253 /* A "read-only" file store.. */
1254 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1255 CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, file);
1256 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1257 if (store)
1259 DWORD size;
1261 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1262 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1263 /* apparently allows adding certificates.. */
1264 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1265 /* but not commits.. */
1266 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1267 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1268 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1269 /* It still has certs in memory.. */
1270 cert = CertEnumCertificatesInStore(store, NULL);
1271 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1272 GetLastError());
1273 CertFreeCertificateContext(cert);
1274 /* but the file size is still 0. */
1275 size = GetFileSize(file, NULL);
1276 ok(size == 0, "Expected size 0, got %d\n", size);
1277 CertCloseStore(store, 0);
1280 /* The create new flag is allowed.. */
1281 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1282 CERT_STORE_CREATE_NEW_FLAG, file);
1283 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1284 if (store)
1286 /* but without the commit enable flag, commits don't happen. */
1287 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1288 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1289 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1290 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1291 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1292 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1293 CertCloseStore(store, 0);
1295 /* as is the open existing flag. */
1296 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1297 CERT_STORE_OPEN_EXISTING_FLAG, file);
1298 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1299 if (store)
1301 /* but without the commit enable flag, commits don't happen. */
1302 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1303 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1304 ok(ret, "CertAddEncodedCertificateToStore failed: %d\n", ret);
1305 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1306 ok(!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1307 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1308 CertCloseStore(store, 0);
1310 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1311 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1312 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1313 if (store)
1315 CloseHandle(file);
1316 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1317 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1318 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1319 GetLastError());
1320 /* with commits enabled, commit is allowed */
1321 ret = pCertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
1322 ok(ret, "CertControlStore failed: %d\n", ret);
1323 compareStore(store, "serialized store with cert",
1324 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
1325 CertCloseStore(store, 0);
1327 file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1328 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1329 if (file == INVALID_HANDLE_VALUE)
1330 return;
1331 store = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
1332 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
1333 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1334 if (store)
1336 CloseHandle(file);
1337 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
1338 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1339 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1340 compareStore(store, "serialized store with cert and CRL",
1341 serializedStoreWithCertAndCRL, sizeof(serializedStoreWithCertAndCRL),
1342 FALSE);
1343 CertCloseStore(store, 0);
1346 DeleteFileW(filename);
1349 static BOOL initFileFromData(LPCWSTR filename, const BYTE *pb, DWORD cb)
1351 HANDLE file = CreateFileW(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
1352 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1353 BOOL ret;
1355 if (file != INVALID_HANDLE_VALUE)
1357 DWORD written;
1359 ret = WriteFile(file, pb, cb, &written, NULL);
1360 CloseHandle(file);
1362 else
1363 ret = FALSE;
1364 return ret;
1367 static const BYTE base64SPC[] =
1368 "MIICJQYJKoZIhvcNAQcCoIICFjCCAhICAQExADALBgkqhkiG9w0BBwGgggH6MIIB"
1369 "9jCCAV+gAwIBAgIQnP8+EF4opr9OxH7h4uBPWTANBgkqhkiG9w0BAQQFADAUMRIw"
1370 "EAYDVQQDEwlKdWFuIExhbmcwHhcNMDgxMjEyMTcxMDE0WhcNMzkxMjMxMjM1OTU5"
1371 "WjAUMRIwEAYDVQQDEwlKdWFuIExhbmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ"
1372 "AoGBALCgNjyNvOic0FOfjxvi43HbM+D5joDkhiGSXe+gbZlf8f16k07kkObFEunz"
1373 "mdB5coscmA7gyqiWNN4ZUyr2cA3lCbnpGPA/0IblyyOcuGIFmmCzeZaVa5ZG6xZP"
1374 "K7L7o+73Qo6jXVbGhBGnMZ7Q9sAn6s2933olnStnejnqwV0NAgMBAAGjSTBHMEUG"
1375 "A1UdAQQ+MDyAEFKbKEdXYyx+CWKcV6vxM6ShFjAUMRIwEAYDVQQDEwlKdWFuIExh"
1376 "bmeCEJz/PhBeKKa/TsR+4eLgT1kwDQYJKoZIhvcNAQEEBQADgYEALpkgLgW3mEaK"
1377 "idPQ3iPJYLG0Ub1wraqEl9bd42hrhzIdcDzlQgxnm8/5cHYVxIF/C20x/HJplb1R"
1378 "G6U1ipFe/q8byWD/9JpiBKMGPi9YlUTgXHfS9d4S/QWO1h9Z7KeipBYhoslQpHXu"
1379 "y9bUr8Adqi6SzgHpCnMu53dxgxUD1r4xAA==";
1380 /* Same as base64SPC, but as a wide-char string */
1381 static const WCHAR utf16Base64SPC[] = {
1382 'M','I','I','C','J','Q','Y','J','K','o','Z','I','h','v','c','N','A',
1383 'Q','c','C','o','I','I','C','F','j','C','C','A','h','I','C','A','Q',
1384 'E','x','A','D','A','L','B','g','k','q','h','k','i','G','9','w','0',
1385 'B','B','w','G','g','g','g','H','6','M','I','I','B','9','j','C','C',
1386 'A','V','+','g','A','w','I','B','A','g','I','Q','n','P','8','+','E',
1387 'F','4','o','p','r','9','O','x','H','7','h','4','u','B','P','W','T',
1388 'A','N','B','g','k','q','h','k','i','G','9','w','0','B','A','Q','Q',
1389 'F','A','D','A','U','M','R','I','w','E','A','Y','D','V','Q','Q','D',
1390 'E','w','l','K','d','W','F','u','I','E','x','h','b','m','c','w','H',
1391 'h','c','N','M','D','g','x','M','j','E','y','M','T','c','x','M','D',
1392 'E','0','W','h','c','N','M','z','k','x','M','j','M','x','M','j','M',
1393 '1','O','T','U','5','W','j','A','U','M','R','I','w','E','A','Y','D',
1394 'V','Q','Q','D','E','w','l','K','d','W','F','u','I','E','x','h','b',
1395 'm','c','w','g','Z','8','w','D','Q','Y','J','K','o','Z','I','h','v',
1396 'c','N','A','Q','E','B','B','Q','A','D','g','Y','0','A','M','I','G',
1397 'J','A','o','G','B','A','L','C','g','N','j','y','N','v','O','i','c',
1398 '0','F','O','f','j','x','v','i','4','3','H','b','M','+','D','5','j',
1399 'o','D','k','h','i','G','S','X','e','+','g','b','Z','l','f','8','f',
1400 '1','6','k','0','7','k','k','O','b','F','E','u','n','z','m','d','B',
1401 '5','c','o','s','c','m','A','7','g','y','q','i','W','N','N','4','Z',
1402 'U','y','r','2','c','A','3','l','C','b','n','p','G','P','A','/','0',
1403 'I','b','l','y','y','O','c','u','G','I','F','m','m','C','z','e','Z',
1404 'a','V','a','5','Z','G','6','x','Z','P','K','7','L','7','o','+','7',
1405 '3','Q','o','6','j','X','V','b','G','h','B','G','n','M','Z','7','Q',
1406 '9','s','A','n','6','s','2','9','3','3','o','l','n','S','t','n','e',
1407 'j','n','q','w','V','0','N','A','g','M','B','A','A','G','j','S','T',
1408 'B','H','M','E','U','G','A','1','U','d','A','Q','Q','+','M','D','y',
1409 'A','E','F','K','b','K','E','d','X','Y','y','x','+','C','W','K','c',
1410 'V','6','v','x','M','6','S','h','F','j','A','U','M','R','I','w','E',
1411 'A','Y','D','V','Q','Q','D','E','w','l','K','d','W','F','u','I','E',
1412 'x','h','b','m','e','C','E','J','z','/','P','h','B','e','K','K','a',
1413 '/','T','s','R','+','4','e','L','g','T','1','k','w','D','Q','Y','J',
1414 'K','o','Z','I','h','v','c','N','A','Q','E','E','B','Q','A','D','g',
1415 'Y','E','A','L','p','k','g','L','g','W','3','m','E','a','K','i','d',
1416 'P','Q','3','i','P','J','Y','L','G','0','U','b','1','w','r','a','q',
1417 'E','l','9','b','d','4','2','h','r','h','z','I','d','c','D','z','l',
1418 'Q','g','x','n','m','8','/','5','c','H','Y','V','x','I','F','/','C',
1419 '2','0','x','/','H','J','p','l','b','1','R','G','6','U','1','i','p',
1420 'F','e','/','q','8','b','y','W','D','/','9','J','p','i','B','K','M',
1421 'G','P','i','9','Y','l','U','T','g','X','H','f','S','9','d','4','S',
1422 '/','Q','W','O','1','h','9','Z','7','K','e','i','p','B','Y','h','o',
1423 's','l','Q','p','H','X','u','y','9','b','U','r','8','A','d','q','i',
1424 '6','S','z','g','H','p','C','n','M','u','5','3','d','x','g','x','U',
1425 'D','1','r','4','x','A','A','=','=',0 };
1427 static void testFileNameStore(void)
1429 static const WCHAR szPrefix[] = { 'c','e','r',0 };
1430 static const WCHAR spcPrefix[] = { 's','p','c',0 };
1431 static const WCHAR szDot[] = { '.',0 };
1432 WCHAR filename[MAX_PATH];
1433 HCERTSTORE store;
1434 BOOL ret;
1435 DWORD GLE;
1437 if (0)
1439 /* Crashes on NT4 */
1440 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0, 0, NULL);
1441 GLE = GetLastError();
1442 ok(!store && (GLE == ERROR_PATH_NOT_FOUND || GLE == ERROR_INVALID_PARAMETER),
1443 "Expected ERROR_PATH_NOT_FOUND or ERROR_INVALID_PARAMETER, got %08x\n",
1444 GLE);
1447 if (!GetTempFileNameW(szDot, szPrefix, 0, filename))
1448 return;
1449 DeleteFileW(filename);
1451 /* The two flags are mutually exclusive */
1452 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1453 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_READONLY_FLAG, filename);
1454 ok(!store && GetLastError() == E_INVALIDARG,
1455 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1457 /* In all of the following tests, the encoding type seems to be ignored */
1458 if (initFileFromData(filename, bigCert, sizeof(bigCert)))
1460 PCCERT_CONTEXT cert;
1461 PCCRL_CONTEXT crl;
1463 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1464 CERT_STORE_READONLY_FLAG, filename);
1465 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1467 cert = CertEnumCertificatesInStore(store, NULL);
1468 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1469 GetLastError());
1470 cert = CertEnumCertificatesInStore(store, cert);
1471 ok(!cert, "Expected only one cert\n");
1472 if (pCertEnumCRLsInStore)
1474 crl = pCertEnumCRLsInStore(store, NULL);
1475 ok(!crl, "Expected no CRLs\n");
1478 CertCloseStore(store, 0);
1479 DeleteFileW(filename);
1481 if (initFileFromData(filename, serializedStoreWithCert,
1482 sizeof(serializedStoreWithCert)))
1484 PCCERT_CONTEXT cert;
1485 PCCRL_CONTEXT crl;
1487 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1488 CERT_STORE_READONLY_FLAG, filename);
1489 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1491 cert = CertEnumCertificatesInStore(store, NULL);
1492 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1493 GetLastError());
1494 cert = CertEnumCertificatesInStore(store, cert);
1495 ok(!cert, "Expected only one cert\n");
1496 if (pCertEnumCRLsInStore)
1498 crl = pCertEnumCRLsInStore(store, NULL);
1499 ok(!crl, "Expected no CRLs\n");
1502 CertCloseStore(store, 0);
1503 DeleteFileW(filename);
1505 if (initFileFromData(filename, serializedStoreWithCertAndCRL,
1506 sizeof(serializedStoreWithCertAndCRL)))
1508 PCCERT_CONTEXT cert;
1509 PCCRL_CONTEXT crl;
1511 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1512 CERT_STORE_READONLY_FLAG, filename);
1513 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1515 cert = CertEnumCertificatesInStore(store, NULL);
1516 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1517 GetLastError());
1518 cert = CertEnumCertificatesInStore(store, cert);
1519 ok(!cert, "Expected only one cert\n");
1520 if (pCertEnumCRLsInStore)
1522 crl = pCertEnumCRLsInStore(store, NULL);
1523 ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n", GetLastError());
1524 crl = pCertEnumCRLsInStore(store, crl);
1525 ok(!crl, "Expected only one CRL\n");
1528 CertCloseStore(store, 0);
1529 /* Don't delete it this time, the next test uses it */
1531 /* Now that the file exists, we can open it read-only */
1532 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1533 CERT_STORE_READONLY_FLAG, filename);
1534 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1535 CertCloseStore(store, 0);
1536 DeleteFileW(filename);
1538 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1539 CERT_FILE_STORE_COMMIT_ENABLE_FLAG | CERT_STORE_CREATE_NEW_FLAG, filename);
1540 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1541 if (store)
1543 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1544 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1545 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1546 GetLastError());
1547 compareStore(store, "serialized store with cert",
1548 serializedStoreWithCert, sizeof(serializedStoreWithCert), FALSE);
1549 CertCloseStore(store, 0);
1551 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1552 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, filename);
1553 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1554 if (store)
1556 ret = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING,
1557 signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
1558 ok(ret, "CertAddEncodedCRLToStore failed: %08x\n", GetLastError());
1559 compareStore(store, "serialized store with cert and CRL",
1560 serializedStoreWithCertAndCRL, sizeof(serializedStoreWithCertAndCRL),
1561 FALSE);
1562 CertCloseStore(store, 0);
1564 DeleteFileW(filename);
1566 if (!GetTempFileNameW(szDot, spcPrefix, 0, filename))
1567 return;
1568 DeleteFileW(filename);
1570 if (initFileFromData(filename, base64SPC, sizeof(base64SPC)))
1572 PCCERT_CONTEXT cert;
1573 PCCRL_CONTEXT crl;
1575 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1576 CERT_STORE_READONLY_FLAG, filename);
1577 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1579 cert = CertEnumCertificatesInStore(store, NULL);
1580 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1581 GetLastError());
1582 cert = CertEnumCertificatesInStore(store, cert);
1583 ok(!cert, "Expected only one cert\n");
1584 if (pCertEnumCRLsInStore)
1586 crl = pCertEnumCRLsInStore(store, NULL);
1587 ok(!crl, "Expected no CRLs\n");
1590 CertCloseStore(store, 0);
1591 DeleteFileW(filename);
1593 if (initFileFromData(filename, (BYTE *)utf16Base64SPC,
1594 sizeof(utf16Base64SPC)))
1596 PCCERT_CONTEXT cert;
1597 PCCRL_CONTEXT crl;
1599 store = CertOpenStore(CERT_STORE_PROV_FILENAME_W, 0, 0,
1600 CERT_STORE_READONLY_FLAG, filename);
1601 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1603 cert = CertEnumCertificatesInStore(store, NULL);
1604 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1605 GetLastError());
1606 cert = CertEnumCertificatesInStore(store, cert);
1607 ok(!cert, "Expected only one cert\n");
1608 if (pCertEnumCRLsInStore)
1610 crl = pCertEnumCRLsInStore(store, NULL);
1611 ok(!crl, "Expected no CRLs\n");
1614 CertCloseStore(store, 0);
1615 DeleteFileW(filename);
1619 static const BYTE signedContent[] = {
1620 0x30,0x81,0xb2,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,0xa0,
1621 0x81,0xa4,0x30,0x81,0xa1,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,
1622 0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,
1623 0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,
1624 0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,
1625 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1626 0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1627 0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,
1628 0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,
1629 0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,
1630 0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,
1631 0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,
1632 0x0d };
1633 static const BYTE signedWithCertAndCrlBareContent[] = {
1634 0x30,0x82,0x01,0x4f,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,0x06,0x08,0x2a,0x86,
1635 0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,
1636 0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0xa0,
1637 0x7c,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,
1638 0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,
1639 0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,
1640 0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,
1641 0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,
1642 0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,
1643 0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,
1644 0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,
1645 0x01,0xff,0x02,0x01,0x01,0xa1,0x2e,0x30,0x2c,0x30,0x02,0x06,0x00,0x30,0x15,
1646 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
1647 0x20,0x4c,0x61,0x6e,0x67,0x00,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
1648 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x31,0x77,0x30,0x75,0x02,0x01,0x01,
1649 0x30,0x1a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,
1650 0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,
1651 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,
1652 0x00,0x05,0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,
1653 0xc0,0x9a,0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,
1654 0xa0,0x1e,0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,
1655 0x23,0x64,0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,
1656 0x51,0xe4,0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
1657 static const BYTE hashContent[] = {
1658 0x30,0x47,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x05,0xa0,0x3a,
1659 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1660 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1661 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1662 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1663 static const BYTE hashBareContent[] = {
1664 0x30,0x38,0x02,0x01,0x00,0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,
1665 0x02,0x05,0x05,0x00,0x30,0x13,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,
1666 0x07,0x01,0xa0,0x06,0x04,0x04,0x01,0x02,0x03,0x04,0x04,0x10,0x08,0xd6,0xc0,
1667 0x5a,0x21,0x51,0x2a,0x79,0xa1,0xdf,0xeb,0x9d,0x2a,0x8f,0x26,0x2f };
1669 static void testMessageStore(void)
1671 HCERTSTORE store;
1672 HCRYPTMSG msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING, 0, 0, 0, NULL,
1673 NULL);
1674 CRYPT_DATA_BLOB blob = { sizeof(signedWithCertAndCrlBareContent),
1675 (LPBYTE)signedWithCertAndCrlBareContent };
1676 DWORD count, size;
1677 BOOL ret;
1679 /* Crashes
1680 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, NULL);
1682 SetLastError(0xdeadbeef);
1683 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1684 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1685 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1686 CryptMsgUpdate(msg, signedContent, sizeof(signedContent), TRUE);
1687 store = CertOpenStore(CERT_STORE_PROV_MSG, 0, 0, 0, msg);
1688 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1689 if (store)
1691 PCCERT_CONTEXT cert = NULL;
1692 PCCRL_CONTEXT crl = NULL;
1694 count = 0;
1695 do {
1696 cert = CertEnumCertificatesInStore(store, cert);
1697 if (cert)
1698 count++;
1699 } while (cert);
1700 ok(count == 0, "Expected 0 certificates, got %d\n", count);
1702 if (pCertEnumCRLsInStore)
1704 count = 0;
1705 do {
1706 crl = pCertEnumCRLsInStore(store, crl);
1707 if (crl)
1708 count++;
1709 } while (crl);
1710 ok(count == 0, "Expected 0 CRLs, got %d\n", count);
1713 /* Can add certs to a message store */
1714 ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
1715 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, NULL);
1716 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
1717 GetLastError());
1718 count = 0;
1719 do {
1720 cert = CertEnumCertificatesInStore(store, cert);
1721 if (cert)
1722 count++;
1723 } while (cert);
1724 ok(count == 1, "Expected 1 certificate, got %d\n", count);
1726 CertCloseStore(store, 0);
1728 /* but the added certs weren't actually added to the message */
1729 size = sizeof(count);
1730 ret = CryptMsgGetParam(msg, CMSG_CERT_COUNT_PARAM, 0, &count, &size);
1731 ok(ret, "CryptMsgGetParam failed: %08x\n", GetLastError());
1732 ok(count == 0, "Expected 0 certificates, got %d\n", count);
1733 CryptMsgClose(msg);
1735 /* Crashes
1736 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, NULL);
1738 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1739 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1740 if (store)
1742 DWORD count = 0;
1743 PCCERT_CONTEXT cert = NULL;
1744 PCCRL_CONTEXT crl = NULL;
1746 do {
1747 cert = CertEnumCertificatesInStore(store, cert);
1748 if (cert)
1749 count++;
1750 } while (cert);
1751 ok(count == 1, "Expected 1 certificate, got %d\n", count);
1753 if (pCertEnumCRLsInStore)
1755 count = 0;
1756 do {
1757 crl = pCertEnumCRLsInStore(store, crl);
1758 if (crl)
1759 count++;
1760 } while (crl);
1761 ok(count == 1, "Expected 1 CRL, got %d\n", count);
1763 CertCloseStore(store, 0);
1765 /* Encoding appears to be ignored */
1766 store = CertOpenStore(CERT_STORE_PROV_PKCS7, X509_ASN_ENCODING, 0, 0,
1767 &blob);
1768 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1769 if (store)
1770 CertCloseStore(store, 0);
1771 /* Messages other than signed messages aren't allowed */
1772 blob.cbData = sizeof(hashContent);
1773 blob.pbData = (LPBYTE)hashContent;
1774 SetLastError(0xdeadbeef);
1775 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1776 ok(!store && GetLastError() == CRYPT_E_INVALID_MSG_TYPE,
1777 "Expected CRYPT_E_INVALID_MSG_TYPE, got %08x\n", GetLastError());
1778 blob.cbData = sizeof(hashBareContent);
1779 blob.pbData = (LPBYTE)hashBareContent;
1780 SetLastError(0xdeadbeef);
1781 store = CertOpenStore(CERT_STORE_PROV_PKCS7, 0, 0, 0, &blob);
1782 ok(!store &&
1783 (GetLastError() == CRYPT_E_ASN1_BADTAG ||
1784 broken(GetLastError() == OSS_DATA_ERROR)), /* NT4 */
1785 "Expected CRYPT_E_ASN1_BADTAG, got %08x\n", GetLastError());
1788 static void testSerializedStore(void)
1790 HCERTSTORE store;
1791 CRYPT_DATA_BLOB blob;
1793 if (0)
1795 /* Crash */
1796 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, NULL);
1797 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
1798 CERT_STORE_DELETE_FLAG, NULL);
1800 blob.cbData = sizeof(serializedStoreWithCert);
1801 blob.pbData = (BYTE *)serializedStoreWithCert;
1802 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0,
1803 CERT_STORE_DELETE_FLAG, &blob);
1804 ok(!store && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
1805 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %08x\n", GetLastError());
1806 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
1807 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1808 if (store)
1810 PCCERT_CONTEXT cert;
1811 PCCRL_CONTEXT crl;
1813 cert = CertEnumCertificatesInStore(store, NULL);
1814 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1815 GetLastError());
1816 cert = CertEnumCertificatesInStore(store, cert);
1817 ok(!cert, "Expected only one cert\n");
1818 if (pCertEnumCRLsInStore)
1820 crl = pCertEnumCRLsInStore(store, NULL);
1821 ok(!crl, "Expected no CRLs\n");
1823 CertCloseStore(store, 0);
1825 blob.cbData = sizeof(serializedStoreWithCertAndCRL);
1826 blob.pbData = (BYTE *)serializedStoreWithCertAndCRL;
1827 store = CertOpenStore(CERT_STORE_PROV_SERIALIZED, 0, 0, 0, &blob);
1828 ok(store != NULL, "CertOpenStore failed: %08x\n", GetLastError());
1829 if (store)
1831 PCCERT_CONTEXT cert;
1832 PCCRL_CONTEXT crl;
1834 cert = CertEnumCertificatesInStore(store, NULL);
1835 ok(cert != NULL, "CertEnumCertificatesInStore failed: %08x\n",
1836 GetLastError());
1837 cert = CertEnumCertificatesInStore(store, cert);
1838 ok(!cert, "Expected only one cert\n");
1839 if (pCertEnumCRLsInStore)
1841 crl = pCertEnumCRLsInStore(store, NULL);
1842 ok(crl != NULL, "CertEnumCRLsInStore failed: %08x\n",
1843 GetLastError());
1844 crl = pCertEnumCRLsInStore(store, crl);
1845 ok(!crl, "Expected only one CRL\n");
1847 CertCloseStore(store, 0);
1851 static void testCertOpenSystemStore(void)
1853 HCERTSTORE store;
1855 store = CertOpenSystemStoreW(0, NULL);
1856 ok(!store && GetLastError() == E_INVALIDARG,
1857 "Expected E_INVALIDARG, got %08x\n", GetLastError());
1858 /* This succeeds, and on WinXP at least, the Bogus key is created under
1859 * HKCU (but not under HKLM, even when run as an administrator.)
1861 store = CertOpenSystemStoreW(0, BogusW);
1862 ok(store != 0, "CertOpenSystemStore failed: %08x\n", GetLastError());
1863 if (store)
1864 CertCloseStore(store, 0);
1865 /* Delete it so other tests succeed next time around */
1866 CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
1867 CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
1868 RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
1871 struct EnumSystemStoreInfo
1873 BOOL goOn;
1874 DWORD storeCount;
1877 static BOOL CALLBACK enumSystemStoreCB(const void *systemStore, DWORD dwFlags,
1878 PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved, void *pvArg)
1880 struct EnumSystemStoreInfo *info = pvArg;
1882 info->storeCount++;
1883 return info->goOn;
1886 static void testCertEnumSystemStore(void)
1888 BOOL ret;
1889 struct EnumSystemStoreInfo info = { FALSE, 0 };
1891 if (!pCertEnumSystemStore)
1893 win_skip("CertEnumSystemStore() is not available\n");
1894 return;
1897 SetLastError(0xdeadbeef);
1898 ret = pCertEnumSystemStore(0, NULL, NULL, NULL);
1899 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
1900 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
1901 /* Crashes
1902 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, NULL,
1903 NULL);
1906 SetLastError(0xdeadbeef);
1907 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1908 enumSystemStoreCB);
1909 /* Callback returning FALSE stops enumeration */
1910 ok(!ret, "Expected CertEnumSystemStore to stop\n");
1911 ok(info.storeCount == 0 || info.storeCount == 1,
1912 "Expected 0 or 1 stores\n");
1914 info.goOn = TRUE;
1915 info.storeCount = 0;
1916 ret = pCertEnumSystemStore(CERT_SYSTEM_STORE_LOCAL_MACHINE, NULL, &info,
1917 enumSystemStoreCB);
1918 ok(ret, "CertEnumSystemStore failed: %08x\n", GetLastError());
1919 /* There should always be at least My, Root, and CA stores */
1920 ok(info.storeCount == 0 || info.storeCount >= 3,
1921 "Expected at least 3 stores\n");
1924 static void testStoreProperty(void)
1926 HCERTSTORE store;
1927 BOOL ret;
1928 DWORD propID, size = 0, state;
1929 CRYPT_DATA_BLOB blob;
1931 if (!pCertGetStoreProperty || !pCertSetStoreProperty)
1933 win_skip("CertGet/SetStoreProperty() is not available\n");
1934 return;
1937 /* Crash
1938 ret = pCertGetStoreProperty(NULL, 0, NULL, NULL);
1939 ret = pCertGetStoreProperty(NULL, 0, NULL, &size);
1940 ret = pCertGetStoreProperty(store, 0, NULL, NULL);
1943 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1944 CERT_STORE_CREATE_NEW_FLAG, NULL);
1945 /* Check a missing prop ID */
1946 SetLastError(0xdeadbeef);
1947 ret = pCertGetStoreProperty(store, 0, NULL, &size);
1948 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1949 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1950 /* Contrary to MSDN, CERT_ACCESS_STATE_PROP_ID is supported for stores.. */
1951 size = sizeof(state);
1952 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1953 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1954 GetLastError());
1955 ok(!state, "Expected a non-persisted store\n");
1956 /* and CERT_STORE_LOCALIZED_NAME_PROP_ID isn't supported by default. */
1957 size = 0;
1958 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1959 &size);
1960 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1961 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1962 /* Delete an arbitrary property on a store */
1963 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1964 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1965 /* Set an arbitrary property on a store */
1966 blob.pbData = (LPBYTE)&state;
1967 blob.cbData = sizeof(state);
1968 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, &blob);
1969 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1970 /* Get an arbitrary property that's been set */
1971 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1972 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1973 ok(size == sizeof(state), "Unexpected data size %d\n", size);
1974 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, &propID, &size);
1975 ok(ret, "CertGetStoreProperty failed: %08x\n", GetLastError());
1976 ok(propID == state, "CertGetStoreProperty got the wrong value\n");
1977 /* Delete it again */
1978 ret = pCertSetStoreProperty(store, CERT_FIRST_USER_PROP_ID, 0, NULL);
1979 ok(ret, "CertSetStoreProperty failed: %08x\n", GetLastError());
1980 /* And check that it's missing */
1981 SetLastError(0xdeadbeef);
1982 ret = pCertGetStoreProperty(store, CERT_FIRST_USER_PROP_ID, NULL, &size);
1983 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1984 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
1985 CertCloseStore(store, 0);
1987 /* Recheck on the My store.. */
1988 store = CertOpenSystemStoreW(0, MyW);
1989 size = sizeof(state);
1990 ret = pCertGetStoreProperty(store, CERT_ACCESS_STATE_PROP_ID, &state, &size);
1991 ok(ret, "CertGetStoreProperty failed for CERT_ACCESS_STATE_PROP_ID: %08x\n",
1992 GetLastError());
1993 ok(state, "Expected a persisted store\n");
1994 SetLastError(0xdeadbeef);
1995 size = 0;
1996 ret = pCertGetStoreProperty(store, CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL,
1997 &size);
1998 ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
1999 "Expected CRYPT_E_NOT_FOUND, got %08x\n", GetLastError());
2000 CertCloseStore(store, 0);
2003 static void testAddSerialized(void)
2005 BOOL ret;
2006 HCERTSTORE store;
2007 BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert)] =
2008 { 0 };
2009 BYTE hash[20];
2010 struct CertPropIDHeader *hdr;
2011 PCCERT_CONTEXT context;
2013 ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
2014 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
2015 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
2017 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2018 CERT_STORE_CREATE_NEW_FLAG, NULL);
2019 ok(store != 0, "CertOpenStore failed: %08x\n", GetLastError());
2021 ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
2022 ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
2023 "Expected ERROR_END_OF_MEDIA, got %08x\n", GetLastError());
2025 /* Test with an empty property */
2026 hdr = (struct CertPropIDHeader *)buf;
2027 hdr->propID = CERT_CERT_PROP_ID;
2028 hdr->unknown1 = 1;
2029 hdr->cb = 0;
2030 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2031 NULL, NULL);
2032 ok(!ret && GetLastError() == E_INVALIDARG,
2033 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2034 /* Test with a bad size in property header */
2035 hdr->cb = sizeof(bigCert) - 1;
2036 memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert));
2037 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2038 NULL, NULL);
2039 ok(!ret && GetLastError() == E_INVALIDARG,
2040 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2041 ret = CertAddSerializedElementToStore(store, buf,
2042 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
2043 NULL);
2044 ok(!ret && GetLastError() == E_INVALIDARG,
2045 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2046 ret = CertAddSerializedElementToStore(store, buf,
2047 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2048 0, 0, NULL, NULL);
2049 ok(!ret && GetLastError() == E_INVALIDARG,
2050 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2051 /* Kosher size in property header, but no context type */
2052 hdr->cb = sizeof(bigCert);
2053 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
2054 NULL, NULL);
2055 ok(!ret && GetLastError() == E_INVALIDARG,
2056 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2057 ret = CertAddSerializedElementToStore(store, buf,
2058 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0, 0, NULL,
2059 NULL);
2060 ok(!ret && GetLastError() == E_INVALIDARG,
2061 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2062 ret = CertAddSerializedElementToStore(store, buf,
2063 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2064 0, 0, NULL, NULL);
2065 ok(!ret && GetLastError() == E_INVALIDARG,
2066 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2067 /* With a bad context type */
2068 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
2069 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2070 ok(!ret && GetLastError() == E_INVALIDARG,
2071 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2072 ret = CertAddSerializedElementToStore(store, buf,
2073 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
2074 CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2075 ok(!ret && GetLastError() == E_INVALIDARG,
2076 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2077 ret = CertAddSerializedElementToStore(store, buf,
2078 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2079 0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
2080 ok(!ret && GetLastError() == E_INVALIDARG,
2081 "Expected E_INVALIDARG, got %08x\n", GetLastError());
2082 /* Bad unknown field, good type */
2083 hdr->unknown1 = 2;
2084 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
2085 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2086 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2087 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2088 ret = CertAddSerializedElementToStore(store, buf,
2089 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
2090 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2091 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2092 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2093 ret = CertAddSerializedElementToStore(store, buf,
2094 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2095 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2096 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
2097 "Expected ERROR_FILE_NOT_FOUND got %08x\n", GetLastError());
2098 /* Most everything okay, but bad add disposition */
2099 hdr->unknown1 = 1;
2100 /* This crashes
2101 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
2102 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2103 * as does this
2104 ret = CertAddSerializedElementToStore(store, buf,
2105 sizeof(struct CertPropIDHeader) + sizeof(bigCert), 0, 0,
2106 CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2108 /* Everything okay, but buffer's too big */
2109 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
2110 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2111 ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
2112 /* Everything okay, check it's not re-added */
2113 ret = CertAddSerializedElementToStore(store, buf,
2114 sizeof(struct CertPropIDHeader) + sizeof(bigCert), CERT_STORE_ADD_NEW,
2115 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
2116 ok(!ret && GetLastError() == CRYPT_E_EXISTS,
2117 "Expected CRYPT_E_EXISTS, got %08x\n", GetLastError());
2119 context = CertEnumCertificatesInStore(store, NULL);
2120 ok(context != NULL, "Expected a cert\n");
2121 if (context)
2122 CertDeleteCertificateFromStore(context);
2124 /* Try adding with a bogus hash. Oddly enough, it succeeds, and the hash,
2125 * when queried, is the real hash rather than the bogus hash.
2127 hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
2128 sizeof(bigCert));
2129 hdr->propID = CERT_HASH_PROP_ID;
2130 hdr->unknown1 = 1;
2131 hdr->cb = sizeof(hash);
2132 memset(hash, 0xc, sizeof(hash));
2133 memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
2134 ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
2135 CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
2136 (const void **)&context);
2137 ok(ret, "CertAddSerializedElementToStore failed: %08x\n", GetLastError());
2138 if (context)
2140 BYTE hashVal[20], realHash[20];
2141 DWORD size = sizeof(hashVal);
2143 ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert),
2144 realHash, &size);
2145 ok(ret, "CryptHashCertificate failed: %08x\n", GetLastError());
2146 ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
2147 hashVal, &size);
2148 ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2149 GetLastError());
2150 ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
2151 CertFreeCertificateContext(context);
2154 CertCloseStore(store, 0);
2157 static const BYTE serializedCertWithFriendlyName[] = {
2158 0x0b,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x57,0x00,0x69,
2159 0x00,0x6e,0x00,0x65,0x00,0x54,0x00,0x65,0x00,0x73,0x00,0x74,0x00,0x00,0x00,
2160 0x20,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,
2161 0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,
2162 0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,
2163 0x22,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,
2164 0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,
2165 0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,
2166 0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,
2167 0x30,0x02,0x06,0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,
2168 0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,
2169 0x01 };
2170 static const BYTE serializedStoreWithCertWithFriendlyName[] = {
2171 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x0b,0x00,0x00,0x00,0x01,0x00,0x00,
2172 0x00,0x12,0x00,0x00,0x00,0x57,0x00,0x69,0x00,0x6e,0x00,0x65,0x00,0x54,0x00,
2173 0x65,0x00,0x73,0x00,0x74,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x01,0x00,0x00,
2174 0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,
2175 0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
2176 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,0x30,0x31,
2177 0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,
2178 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,
2179 0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,0x6e,
2180 0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,0x01,0x00,
2181 0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,
2182 0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
2183 0x00,0x00,0x00,0x00,0x00,0x00 };
2184 static const BYTE serializedStoreWithCertAndHash[] = {
2185 0x00,0x00,0x00,0x00,0x43,0x45,0x52,0x54,0x03,0x00,0x00,0x00,0x01,0x00,0x00,
2186 0x00,0x14,0x00,0x00,0x00,0x6e,0x30,0x90,0x71,0x5f,0xd9,0x23,0x56,0xeb,0xae,
2187 0x25,0x40,0xe6,0x22,0xda,0x19,0x26,0x02,0xa6,0x08,0x20,0x00,0x00,0x00,0x01,
2188 0x00,0x00,0x00,0x7c,0x00,0x00,0x00,0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,
2189 0x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,
2190 0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,
2191 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,
2192 0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,
2193 0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
2194 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,0x00,0x03,
2195 0x01,0x00,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,
2196 0xff,0x04,0x08,0x30,0x06,0x01,0x01,0xff,0x02,0x01,0x01,0x00,0x00,0x00,0x00,
2197 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
2199 static void delete_test_key(void)
2201 HKEY root_key, test_key;
2202 static const WCHAR SysCertW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2203 'S','y','s','t','e','m','C','e','r','t','i','f','i','c','a','t','e','s',0};
2204 static const WCHAR WineTestW[] = {'W','i','n','e','T','e','s','t',0};
2205 WCHAR subkey_name[32];
2206 DWORD num_subkeys, subkey_name_len;
2207 int idx;
2209 if (RegOpenKeyExW(HKEY_CURRENT_USER, SysCertW, 0, KEY_READ, &root_key))
2210 return;
2211 if (RegOpenKeyExW(root_key, WineTestW, 0, KEY_READ, &test_key))
2213 RegCloseKey(root_key);
2214 return;
2216 RegQueryInfoKeyW(test_key, NULL, NULL, NULL, &num_subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
2217 for (idx = num_subkeys; idx-- > 0;)
2219 subkey_name_len = sizeof(subkey_name)/sizeof(WCHAR);
2220 RegEnumKeyExW(test_key, idx, subkey_name, &subkey_name_len, NULL, NULL, NULL, NULL);
2221 RegDeleteKeyW(test_key, subkey_name);
2223 RegCloseKey(test_key);
2224 RegDeleteKeyW(root_key, WineTestW);
2225 RegCloseKey(root_key);
2228 static void testAddCertificateLink(void)
2230 BOOL ret;
2231 HCERTSTORE store1, store2;
2232 PCCERT_CONTEXT source, linked;
2233 DWORD size;
2234 LPBYTE buf;
2235 CERT_NAME_BLOB blob;
2236 static const WCHAR szPrefix[] = { 'c','e','r',0 };
2237 static const WCHAR szDot[] = { '.',0 };
2238 static const WCHAR WineTestW[] = { 'W','i','n','e','T','e','s','t',0 };
2239 WCHAR filename1[MAX_PATH], filename2[MAX_PATH];
2240 HANDLE file;
2242 if (!pCertAddCertificateLinkToStore)
2244 win_skip("CertAddCertificateLinkToStore not found\n");
2245 return;
2248 if (0)
2250 /* Crashes, i.e. the store is dereferenced without checking. */
2251 ret = pCertAddCertificateLinkToStore(NULL, NULL, 0, NULL);
2254 /* Adding a certificate link to a store requires a valid add disposition */
2255 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2256 CERT_STORE_CREATE_NEW_FLAG, NULL);
2257 SetLastError(0xdeadbeef);
2258 ret = pCertAddCertificateLinkToStore(store1, NULL, 0, NULL);
2259 ok(!ret && GetLastError() == E_INVALIDARG,
2260 "expected E_INVALIDARG, got %08x\n", GetLastError());
2261 source = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
2262 sizeof(bigCert));
2263 SetLastError(0xdeadbeef);
2264 ret = pCertAddCertificateLinkToStore(store1, source, 0, NULL);
2265 ok(!ret && GetLastError() == E_INVALIDARG,
2266 "expected E_INVALIDARG, got %08x\n", GetLastError());
2267 ret = pCertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2268 NULL);
2269 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2270 if (0)
2272 /* Crashes, i.e. the source certificate is dereferenced without
2273 * checking when a valid add disposition is given.
2275 ret = pCertAddCertificateLinkToStore(store1, NULL, CERT_STORE_ADD_ALWAYS,
2276 NULL);
2278 CertCloseStore(store1, 0);
2280 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2281 CERT_STORE_CREATE_NEW_FLAG, NULL);
2282 ret = pCertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2283 &linked);
2284 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2285 if (ret)
2287 ok(linked->hCertStore == store1, "unexpected store\n");
2288 ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2289 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2290 GetLastError());
2291 buf = HeapAlloc(GetProcessHeap(), 0, size);
2292 if (buf)
2294 ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2295 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2296 GetLastError());
2297 /* The serialized linked certificate is identical to the serialized
2298 * original certificate.
2300 ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
2301 ok(!memcmp(serializedCert, buf, size),
2302 "Unexpected serialized cert\n");
2303 HeapFree(GetProcessHeap(), 0, buf);
2305 /* Set a friendly name on the source certificate... */
2306 blob.pbData = (LPBYTE)WineTestW;
2307 blob.cbData = sizeof(WineTestW);
2308 ret = CertSetCertificateContextProperty(source,
2309 CERT_FRIENDLY_NAME_PROP_ID, 0, &blob);
2310 ok(ret, "CertSetCertificateContextProperty failed: %08x\n",
2311 GetLastError());
2312 /* and the linked certificate has the same friendly name. */
2313 ret = CertGetCertificateContextProperty(linked,
2314 CERT_FRIENDLY_NAME_PROP_ID, NULL, &size);
2315 ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2316 GetLastError());
2317 buf = HeapAlloc(GetProcessHeap(), 0, size);
2318 if (buf)
2320 ret = CertGetCertificateContextProperty(linked,
2321 CERT_FRIENDLY_NAME_PROP_ID, buf, &size);
2322 ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2323 GetLastError());
2324 ok(!lstrcmpW((LPCWSTR)buf, WineTestW),
2325 "unexpected friendly name\n");
2326 HeapFree(GetProcessHeap(), 0, buf);
2328 CertFreeCertificateContext(linked);
2330 CertFreeCertificateContext(source);
2331 CertCloseStore(store1, 0);
2333 /* Test adding a cert to a file store, committing the change to the store,
2334 * and creating a link to the resulting cert.
2336 if (!GetTempFileNameW(szDot, szPrefix, 0, filename1))
2337 return;
2339 DeleteFileW(filename1);
2340 file = CreateFileW(filename1, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2341 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2342 if (file == INVALID_HANDLE_VALUE)
2343 return;
2345 store1 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2346 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2347 ok(store1 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2348 CloseHandle(file);
2350 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
2351 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &source);
2352 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
2353 GetLastError());
2355 /* Test adding a link to a memory store. */
2356 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2357 CERT_STORE_CREATE_NEW_FLAG, NULL);
2358 ret = pCertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2359 &linked);
2360 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2361 if (ret)
2363 ok(linked->hCertStore == store2, "unexpected store\n");
2364 ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2365 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2366 GetLastError());
2367 buf = HeapAlloc(GetProcessHeap(), 0, size);
2368 if (buf)
2370 ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2371 /* The serialized linked certificate is identical to the serialized
2372 * original certificate.
2374 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n", GetLastError());
2375 ok(size == sizeof(serializedCert), "Wrong size %d\n", size);
2376 ok(!memcmp(serializedCert, buf, size),
2377 "Unexpected serialized cert\n");
2378 HeapFree(GetProcessHeap(), 0, buf);
2380 /* Set a friendly name on the source certificate... */
2381 blob.pbData = (LPBYTE)WineTestW;
2382 blob.cbData = sizeof(WineTestW);
2383 ret = CertSetCertificateContextProperty(source,
2384 CERT_FRIENDLY_NAME_PROP_ID, 0, &blob);
2385 ok(ret, "CertSetCertificateContextProperty failed: %08x\n",
2386 GetLastError());
2387 /* and the linked certificate has the same friendly name. */
2388 ret = CertGetCertificateContextProperty(linked,
2389 CERT_FRIENDLY_NAME_PROP_ID, NULL, &size);
2390 ok(ret, "CertGetCertificateContextProperty failed: %08x\n",
2391 GetLastError());
2392 buf = HeapAlloc(GetProcessHeap(), 0, size);
2393 if (buf)
2395 ret = CertGetCertificateContextProperty(linked,
2396 CERT_FRIENDLY_NAME_PROP_ID, buf, &size);
2397 ok(ret, "CertGetCertificateContextProperty failed: %08x\n", GetLastError());
2398 ok(!lstrcmpW((LPCWSTR)buf, WineTestW),
2399 "unexpected friendly name\n");
2400 HeapFree(GetProcessHeap(), 0, buf);
2402 CertFreeCertificateContext(linked);
2404 CertCloseStore(store2, 0);
2406 if (!GetTempFileNameW(szDot, szPrefix, 0, filename2))
2407 return;
2409 DeleteFileW(filename2);
2410 file = CreateFileW(filename2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2411 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2412 if (file == INVALID_HANDLE_VALUE)
2413 return;
2415 store2 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2416 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2417 ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2418 CloseHandle(file);
2419 /* Test adding a link to a file store. */
2420 ret = pCertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2421 &linked);
2422 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2423 if (ret)
2425 ok(linked->hCertStore == store2, "unexpected store\n");
2426 ret = CertSerializeCertificateStoreElement(linked, 0, NULL, &size);
2427 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2428 GetLastError());
2429 buf = HeapAlloc(GetProcessHeap(), 0, size);
2430 if (buf)
2432 ret = CertSerializeCertificateStoreElement(linked, 0, buf, &size);
2433 ok(ret, "CertSerializeCertificateStoreElement failed: %08x\n",
2434 GetLastError());
2435 /* The serialized linked certificate now contains the friendly
2436 * name property.
2438 ok(size == sizeof(serializedCertWithFriendlyName),
2439 "Wrong size %d\n", size);
2440 ok(!memcmp(serializedCertWithFriendlyName, buf, size),
2441 "Unexpected serialized cert\n");
2442 HeapFree(GetProcessHeap(), 0, buf);
2444 CertFreeCertificateContext(linked);
2445 compareStore(store2, "file store -> file store",
2446 serializedStoreWithCertWithFriendlyName,
2447 sizeof(serializedStoreWithCertWithFriendlyName), FALSE);
2449 CertCloseStore(store2, 0);
2450 DeleteFileW(filename2);
2452 CertFreeCertificateContext(source);
2454 CertCloseStore(store1, 0);
2455 DeleteFileW(filename1);
2457 /* Test adding a link to a system store (which is a collection store.) */
2458 store1 = CertOpenSystemStoreA(0, "My");
2459 source = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
2460 sizeof(bigCert));
2461 SetLastError(0xdeadbeef);
2462 ret = pCertAddCertificateLinkToStore(store1, source, CERT_STORE_ADD_ALWAYS,
2463 &linked);
2464 ok(!ret && GetLastError() == E_INVALIDARG,
2465 "expected E_INVALIDARG, got %08x\n", GetLastError());
2466 CertFreeCertificateContext(source);
2468 /* Test adding a link to a file store, where the linked certificate is
2469 * in a system store.
2471 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
2472 bigCert, sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &source);
2473 ok(ret, "CertAddEncodedCertificateToStore failed: %08x\n",
2474 GetLastError());
2475 if (!GetTempFileNameW(szDot, szPrefix, 0, filename1))
2476 return;
2478 DeleteFileW(filename1);
2479 file = CreateFileW(filename1, GENERIC_READ | GENERIC_WRITE, 0, NULL,
2480 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2481 if (file == INVALID_HANDLE_VALUE)
2482 return;
2484 store2 = CertOpenStore(CERT_STORE_PROV_FILE, 0, 0,
2485 CERT_FILE_STORE_COMMIT_ENABLE_FLAG, file);
2486 ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2487 CloseHandle(file);
2489 ret = pCertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2490 &linked);
2491 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2492 if (ret)
2494 ok(linked->hCertStore == store2, "unexpected store\n");
2495 ret = pCertControlStore(store2, 0, CERT_STORE_CTRL_COMMIT, NULL);
2496 ok(ret, "CertControlStore failed: %d\n", ret);
2497 compareStore(store2, "file store -> system store",
2498 serializedStoreWithCertAndHash,
2499 sizeof(serializedStoreWithCertAndHash), TRUE);
2500 CertFreeCertificateContext(linked);
2503 CertCloseStore(store2, 0);
2504 DeleteFileW(filename1);
2506 /* Test adding a link to a registry store, where the linked certificate is
2507 * in a system store.
2509 store2 = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
2510 CERT_SYSTEM_STORE_CURRENT_USER, WineTestW);
2511 ok(store2 != NULL, "CertOpenStore failed: %08x\n", GetLastError());
2512 ret = pCertAddCertificateLinkToStore(store2, source, CERT_STORE_ADD_ALWAYS,
2513 &linked);
2514 ok(ret, "CertAddCertificateLinkToStore failed: %08x\n", GetLastError());
2515 if (ret)
2517 ok(linked->hCertStore == store2, "unexpected store\n");
2518 CertDeleteCertificateFromStore(linked);
2520 CertCloseStore(store2, 0);
2522 CertFreeCertificateContext(source);
2523 CertCloseStore(store1, 0);
2525 delete_test_key();
2528 static DWORD countCertsInStore(HCERTSTORE store)
2530 PCCERT_CONTEXT cert = NULL;
2531 DWORD certs = 0;
2533 do {
2534 cert = CertEnumCertificatesInStore(store, cert);
2535 if (cert)
2536 certs++;
2537 } while (cert);
2538 return certs;
2541 static DWORD countCRLsInStore(HCERTSTORE store)
2543 PCCRL_CONTEXT crl = NULL;
2544 DWORD crls = 0;
2546 do {
2547 crl = pCertEnumCRLsInStore(store, crl);
2548 if (crl)
2549 crls++;
2550 } while (crl);
2551 return crls;
2554 static void testEmptyStore(void)
2556 const CERT_CONTEXT *cert, *cert2, *cert3;
2557 const CRL_CONTEXT *crl;
2558 const CTL_CONTEXT *ctl;
2559 HCERTSTORE store;
2560 BOOL res;
2562 cert = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert, sizeof(bigCert));
2563 ok(cert != NULL, "CertCreateCertificateContext failed\n");
2564 ok(cert->hCertStore != NULL, "cert->hCertStore == NULL\n");
2565 if(!cert->hCertStore) {
2566 CertFreeCertificateContext(cert);
2567 return;
2570 test_store_is_empty(cert->hCertStore);
2572 cert2 = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert2, sizeof(bigCert2));
2573 ok(cert2 != NULL, "CertCreateCertificateContext failed\n");
2574 ok(cert2->hCertStore == cert->hCertStore, "Unexpected hCertStore\n");
2576 test_store_is_empty(cert2->hCertStore);
2578 res = CertAddCertificateContextToStore(cert->hCertStore, cert2, CERT_STORE_ADD_NEW, &cert3);
2579 ok(res, "CertAddCertificateContextToStore failed\n");
2580 todo_wine
2581 ok(cert3 && cert3 != cert2, "Unexpected cert3\n");
2582 ok(cert3->hCertStore == cert->hCertStore, "Unexpected hCertStore\n");
2584 test_store_is_empty(cert->hCertStore);
2586 res = CertDeleteCertificateFromStore(cert3);
2587 ok(res, "CertDeleteCertificateContextFromStore failed\n");
2588 ok(cert3->hCertStore == cert->hCertStore, "Unexpected hCertStore\n");
2590 CertFreeCertificateContext(cert3);
2592 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2593 ok(store != NULL, "CertOpenStore failed\n");
2595 res = CertAddCertificateContextToStore(store, cert2, CERT_STORE_ADD_NEW, &cert3);
2596 ok(res, "CertAddCertificateContextToStore failed\n");
2597 ok(cert3 && cert3 != cert2, "Unexpected cert3\n");
2598 ok(cert3->hCertStore == store, "Unexpected hCertStore\n");
2600 res = CertDeleteCertificateFromStore(cert3);
2601 ok(res, "CertDeleteCertificateContextFromStore failed\n");
2602 ok(cert3->hCertStore == store, "Unexpected hCertStore\n");
2604 CertFreeCertificateContext(cert3);
2606 CertCloseStore(store, 0);
2608 res = CertCloseStore(cert->hCertStore, CERT_CLOSE_STORE_CHECK_FLAG);
2609 ok(!res && GetLastError() == E_UNEXPECTED, "CertCloseStore returned: %x(%x)\n", res, GetLastError());
2611 res = CertCloseStore(cert->hCertStore, 0);
2612 ok(!res && GetLastError() == E_UNEXPECTED, "CertCloseStore returned: %x(%x)\n", res, GetLastError());
2614 CertFreeCertificateContext(cert2);
2616 crl = CertCreateCRLContext(X509_ASN_ENCODING, signedCRL, sizeof(signedCRL));
2617 ok(crl != NULL, "CertCreateCRLContext failed\n");
2618 ok(crl->hCertStore == cert->hCertStore, "unexpected hCertStore\n");
2620 CertFreeCRLContext(crl);
2622 ctl = CertCreateCTLContext(X509_ASN_ENCODING, signedCTLWithCTLInnerContent, sizeof(signedCTLWithCTLInnerContent));
2623 ok(ctl != NULL, "CertCreateCTLContext failed\n");
2624 ok(ctl->hCertStore == cert->hCertStore, "unexpected hCertStore\n");
2626 CertFreeCTLContext(ctl);
2628 CertFreeCertificateContext(cert);
2631 static void testCloseStore(void)
2633 const CERT_CONTEXT *cert;
2634 const CRL_CONTEXT *crl;
2635 const CTL_CONTEXT *ctl;
2636 HCERTSTORE store, store2;
2637 BOOL res;
2639 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2640 ok(store != NULL, "CertOpenStore failed\n");
2642 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2643 ok(res, "CertCloseStore failed\n");
2645 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2646 ok(store != NULL, "CertOpenStore failed\n");
2648 store2 = CertDuplicateStore(store);
2649 ok(store2 != NULL, "CertCloneStore failed\n");
2650 ok(store2 == store, "unexpected store2\n");
2652 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2653 ok(!res && GetLastError() == CRYPT_E_PENDING_CLOSE, "CertCloseStore failed\n");
2655 res = CertCloseStore(store2, CERT_CLOSE_STORE_CHECK_FLAG);
2656 ok(res, "CertCloseStore failed\n");
2658 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2659 ok(store != NULL, "CertOpenStore failed\n");
2661 res = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING, bigCert,
2662 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2663 ok(res, "CertAddEncodedCertificateToStore failed\n");
2665 /* There is still a reference from cert */
2666 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2667 ok(!res && GetLastError() == CRYPT_E_PENDING_CLOSE, "CertCloseStore failed\n");
2669 res = CertFreeCertificateContext(cert);
2670 ok(res, "CertFreeCertificateContext failed\n");
2672 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2673 ok(store != NULL, "CertOpenStore failed\n");
2675 res = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
2676 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &crl);
2677 ok(res, "CertAddEncodedCRLToStore failed\n");
2679 /* There is still a reference from CRL */
2680 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2681 ok(!res && GetLastError() == CRYPT_E_PENDING_CLOSE, "CertCloseStore failed\n");
2683 res = CertFreeCRLContext(crl);
2684 ok(res, "CertFreeCRLContext failed\n");
2686 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2687 ok(store != NULL, "CertOpenStore failed\n");
2689 res = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING, signedCTLWithCTLInnerContent,
2690 sizeof(signedCTLWithCTLInnerContent), CERT_STORE_ADD_ALWAYS, &ctl);
2691 ok(res, "CertAddEncodedCTLToStore failed\n");
2693 /* There is still a reference from CTL */
2694 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2695 ok(!res && GetLastError() == CRYPT_E_PENDING_CLOSE, "CertCloseStore returned: %x(%u)\n", res, GetLastError());
2697 res = CertFreeCTLContext(ctl);
2698 ok(res, "CertFreeCTLContext failed\n");
2700 /* Add all kinds of contexts, then release external references and make sure that store is properly closed. */
2701 store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
2702 ok(store != NULL, "CertOpenStore failed\n");
2704 res = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING, bigCert,
2705 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2706 ok(res, "CertAddEncodedCertificateToStore failed\n");
2708 res = CertAddEncodedCRLToStore(store, X509_ASN_ENCODING, signedCRL,
2709 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &crl);
2710 ok(res, "CertAddEncodedCRLToStore failed\n");
2712 res = CertAddEncodedCTLToStore(store, X509_ASN_ENCODING, signedCTLWithCTLInnerContent,
2713 sizeof(signedCTLWithCTLInnerContent), CERT_STORE_ADD_ALWAYS, &ctl);
2714 ok(res, "CertAddEncodedCTLToStore failed\n");
2716 CertFreeCertificateContext(cert);
2717 CertFreeCRLContext(crl);
2718 CertFreeCTLContext(ctl);
2720 res = CertCloseStore(store, CERT_CLOSE_STORE_CHECK_FLAG);
2721 ok(res, "CertCloseStore failed\n");
2724 static void test_I_UpdateStore(void)
2726 HMODULE lib = GetModuleHandleA("crypt32");
2727 BOOL (WINAPI *pI_CertUpdatestore)(HCERTSTORE, HCERTSTORE, DWORD, DWORD) =
2728 (void *)GetProcAddress(lib, "I_CertUpdateStore");
2729 BOOL ret;
2730 HCERTSTORE store1, store2;
2731 PCCERT_CONTEXT cert;
2732 DWORD certs;
2734 if (!pI_CertUpdatestore)
2736 win_skip("No I_CertUpdateStore\n");
2737 return;
2739 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2740 CERT_STORE_CREATE_NEW_FLAG, NULL);
2741 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
2742 CERT_STORE_CREATE_NEW_FLAG, NULL);
2744 /* Crash
2745 ret = pI_CertUpdatestore(NULL, NULL, 0, 0);
2746 ret = pI_CertUpdatestore(store1, NULL, 0, 0);
2747 ret = pI_CertUpdatestore(NULL, store2, 0, 0);
2749 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2750 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2752 CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING, bigCert,
2753 sizeof(bigCert), CERT_STORE_ADD_ALWAYS, &cert);
2754 /* I_CertUpdateStore adds the contexts from store2 to store1 */
2755 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2756 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2757 certs = countCertsInStore(store1);
2758 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2759 /* Calling it a second time has no effect */
2760 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2761 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2762 certs = countCertsInStore(store1);
2763 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2765 /* The last parameters to I_CertUpdateStore appear to be ignored */
2766 ret = pI_CertUpdatestore(store1, store2, 1, 0);
2767 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2768 ret = pI_CertUpdatestore(store1, store2, 0, 1);
2769 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2771 CertAddEncodedCRLToStore(store2, X509_ASN_ENCODING, signedCRL,
2772 sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, NULL);
2774 /* I_CertUpdateStore also adds the CRLs from store2 to store1 */
2775 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2776 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2777 certs = countCertsInStore(store1);
2778 ok(certs == 1, "Expected 1 cert, got %d\n", certs);
2779 if (pCertEnumCRLsInStore)
2781 certs = countCRLsInStore(store1);
2782 ok(certs == 1, "Expected 1 CRL, got %d\n", certs);
2785 CertDeleteCertificateFromStore(cert);
2786 /* If a context is deleted from store2, I_CertUpdateStore deletes it
2787 * from store1
2789 ret = pI_CertUpdatestore(store1, store2, 0, 0);
2790 ok(ret, "I_CertUpdateStore failed: %08x\n", GetLastError());
2791 certs = countCertsInStore(store1);
2792 ok(certs == 0, "Expected 0 certs, got %d\n", certs);
2794 CertCloseStore(store1, 0);
2795 CertCloseStore(store2, 0);
2798 START_TEST(store)
2800 HMODULE hdll;
2802 hdll = GetModuleHandleA("Crypt32.dll");
2803 pCertAddStoreToCollection = (void*)GetProcAddress(hdll, "CertAddStoreToCollection");
2804 pCertControlStore = (void*)GetProcAddress(hdll, "CertControlStore");
2805 pCertEnumCRLsInStore = (void*)GetProcAddress(hdll, "CertEnumCRLsInStore");
2806 pCertEnumSystemStore = (void*)GetProcAddress(hdll, "CertEnumSystemStore");
2807 pCertGetStoreProperty = (void*)GetProcAddress(hdll, "CertGetStoreProperty");
2808 pCertRemoveStoreFromCollection = (void*)GetProcAddress(hdll, "CertRemoveStoreFromCollection");
2809 pCertSetStoreProperty = (void*)GetProcAddress(hdll, "CertSetStoreProperty");
2810 pCertAddCertificateLinkToStore = (void*)GetProcAddress(hdll, "CertAddCertificateLinkToStore");
2812 /* various combinations of CertOpenStore */
2813 testMemStore();
2814 testCollectionStore();
2815 testRegStore();
2816 testSystemRegStore();
2817 testSystemStore();
2818 testFileStore();
2819 testFileNameStore();
2820 testMessageStore();
2821 testSerializedStore();
2822 testCloseStore();
2824 testCertOpenSystemStore();
2825 testCertEnumSystemStore();
2826 testStoreProperty();
2828 testAddSerialized();
2829 testAddCertificateLink();
2831 testEmptyStore();
2833 test_I_UpdateStore();