2 * crypt32 cert functions tests
4 * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/test.h"
31 static void testCryptHashCert(void)
33 static const BYTE emptyHash[] = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b,
34 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07,
36 static const BYTE knownHash[] = { 0xae, 0x9d, 0xbf, 0x6d, 0xf5, 0x46, 0xee,
37 0x8b, 0xc5, 0x7a, 0x13, 0xba, 0xc2, 0xb1, 0x04, 0xf2, 0xbf, 0x52, 0xa8,
39 static const BYTE toHash[] = "abcdefghijklmnopqrstuvwxyz0123456789.,;!?:";
42 DWORD hashLen = sizeof(hash);
44 /* NULL buffer and nonzero length crashes
45 ret = CryptHashCertificate(0, 0, 0, NULL, size, hash, &hashLen);
46 empty hash length also crashes
47 ret = CryptHashCertificate(0, 0, 0, buf, size, hash, NULL);
50 ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), NULL,
52 ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
53 ok(hashLen == sizeof(hash),
54 "Got unexpected size of hash %ld, expected %d\n", hashLen, sizeof(hash));
55 /* Test with empty buffer */
56 ret = CryptHashCertificate(0, 0, 0, NULL, 0, hash, &hashLen);
57 ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
58 ok(!memcmp(hash, emptyHash, sizeof(emptyHash)),
59 "Unexpected hash of nothing\n");
60 /* Test a known value */
61 ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), hash,
63 ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
64 ok(!memcmp(hash, knownHash, sizeof(knownHash)), "Unexpected hash\n");
67 static const BYTE emptyCert[] = { 0x30, 0x00 };
68 static const BYTE bigCert[] = "\x30\x7a\x02\x01\x01\x30\x02\x06\x00"
69 "\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e\x20\x4c"
70 "\x61\x6e\x67\x00\x30\x22\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30"
71 "\x30\x30\x30\x5a\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30\x30\x30"
72 "\x30\x5a\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e"
73 "\x20\x4c\x61\x6e\x67\x00\x30\x07\x30\x02\x06\x00\x03\x01\x00\xa3\x16\x30\x14"
74 "\x30\x12\x06\x03\x55\x1d\x13\x01\x01\xff\x04\x08\x30\x06\x01\x01\xff\x02\x01"
76 static const BYTE signedBigCert[] = {
77 0x30, 0x81, 0x93, 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06, 0x00, 0x30,
78 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a,
79 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22, 0x18, 0x0f,
80 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
81 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30,
82 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06,
83 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61,
84 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01, 0x00, 0xa3,
85 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
86 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
87 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
88 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
89 static const BYTE serializedCert[] = { 0x20, 0x00, 0x00, 0x00,
90 0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x30, 0x7a, 0x02, 0x01, 0x01,
91 0x30, 0x02, 0x06, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
92 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67,
93 0x00, 0x30, 0x22, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31,
94 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31,
95 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15,
96 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75,
97 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06,
98 0x00, 0x03, 0x01, 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
99 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02,
102 static void testMemStore(void)
104 HCERTSTORE store1, store2;
105 PCCERT_CONTEXT context;
109 store1 = CertOpenStore(0, 0, 0, 0, NULL);
110 ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
111 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
113 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
114 CERT_STORE_DELETE_FLAG, NULL);
115 ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
116 "Expected ERROR_CALL_NOT_IMPLEMENTED, got %ld\n", GetLastError());
119 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
120 CERT_STORE_CREATE_NEW_FLAG, NULL);
121 ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
122 /* open existing doesn't */
123 store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
124 CERT_STORE_OPEN_EXISTING_FLAG, NULL);
125 ok(store2 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
126 ok(store1 != store2, "Expected different stores\n");
128 /* add a bogus (empty) cert */
130 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
131 sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
132 /* Windows returns CRYPT_E_ASN1_EOD, but accept CRYPT_E_ASN1_CORRUPT as
133 * well (because matching errors is tough in this case)
135 ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
136 CRYPT_E_ASN1_CORRUPT),
137 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
139 /* add a signed cert (this also fails) */
140 ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
141 CRYPT_E_ASN1_CORRUPT),
142 "Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
144 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
145 signedBigCert, sizeof(signedBigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
146 /* add a cert to store1 */
147 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
148 sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
149 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
150 ok(context != NULL, "Expected a valid cert context\n");
156 ok(context->cbCertEncoded == sizeof(bigCert) - 1,
157 "Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
158 context->cbCertEncoded);
159 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
160 "Unexpected encoded cert in context\n");
161 ok(context->hCertStore == store1, "Unexpected store\n");
163 /* check serializing this element */
165 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
166 ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
167 ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
169 /* apparently flags are ignored */
170 ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
171 ok(ret, "CertSerializeCertificateStoreElement failed: %08lx\n",
173 buf = HeapAlloc(GetProcessHeap(), 0, size);
176 ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
177 ok(size == sizeof(serializedCert), "Expected size %d, got %ld\n",
178 sizeof(serializedCert), size);
179 ok(!memcmp(serializedCert, buf, size),
180 "Unexpected serialized cert\n");
181 HeapFree(GetProcessHeap(), 0, buf);
184 ret = CertFreeCertificateContext(context);
185 ok(ret, "CertFreeCertificateContext failed: %08lx\n", GetLastError());
187 /* verify the cert's in store1 */
188 context = CertEnumCertificatesInStore(store1, NULL);
189 ok(context != NULL, "Expected a valid context\n");
190 context = CertEnumCertificatesInStore(store1, context);
191 ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
192 "Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
193 /* verify store2 (the "open existing" mem store) is still empty */
194 context = CertEnumCertificatesInStore(store2, NULL);
195 ok(!context, "Expected an empty store\n");
196 /* delete the cert from store1, and check it's empty */
197 context = CertEnumCertificatesInStore(store1, NULL);
200 /* Deleting a bitwise copy crashes with an access to an uninitialized
201 * pointer, so a cert context has some special data out there in memory
204 memcpy(©, context, sizeof(copy));
205 ret = CertDeleteCertificateFromStore(©);
207 PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
209 ok(copy != NULL, "CertDuplicateCertificateContext failed: %08lx\n",
211 ret = CertDeleteCertificateFromStore(context);
212 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
214 /* try deleting a copy */
215 ret = CertDeleteCertificateFromStore(copy);
216 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
218 /* check that the store is empty */
219 context = CertEnumCertificatesInStore(store1, NULL);
220 ok(!context, "Expected an empty store\n");
223 /* close an empty store */
224 ret = CertCloseStore(NULL, 0);
225 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
226 ret = CertCloseStore(store1, 0);
227 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
228 ret = CertCloseStore(store2, 0);
229 ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
231 /* This seems nonsensical, but you can open a read-only mem store, only
234 store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
235 CERT_STORE_READONLY_FLAG, NULL);
236 ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
237 /* yep, this succeeds */
238 ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
239 sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
240 ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
241 ok(context != NULL, "Expected a valid cert context\n");
244 ok(context->cbCertEncoded == sizeof(bigCert) - 1,
245 "Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
246 context->cbCertEncoded);
247 ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
248 "Unexpected encoded cert in context\n");
249 ok(context->hCertStore == store1, "Unexpected store\n");
250 ret = CertDeleteCertificateFromStore(context);
251 ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
254 CertCloseStore(store1, 0);
257 static void testCertProperties(void)
259 PCCERT_CONTEXT context = CertCreateCertificateContext(X509_ASN_ENCODING,
260 bigCert, sizeof(bigCert) - 1);
262 ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
266 DWORD propID, numProps, access, size;
268 BYTE hash[20] = { 0 }, hashProperty[20];
269 CRYPT_DATA_BLOB blob;
272 propID = CertEnumCertificateContextProperties(NULL, 0);
278 propID = CertEnumCertificateContextProperties(context, propID);
281 } while (propID != 0);
282 ok(numProps == 0, "Expected 0 properties, got %ld\n", numProps);
284 /* Tests with a NULL cert context. Prop ID 0 fails.. */
285 ret = CertSetCertificateContextProperty(NULL, 0, 0, NULL);
286 ok(!ret && GetLastError() ==
287 HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
288 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
290 /* while this just crashes.
291 ret = CertSetCertificateContextProperty(NULL,
292 CERT_KEY_PROV_HANDLE_PROP_ID, 0, NULL);
295 ret = CertSetCertificateContextProperty(context, 0, 0, NULL);
296 ok(!ret && GetLastError() ==
297 HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
298 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
300 /* Can't set the cert property directly, this crashes.
301 ret = CertSetCertificateContextProperty(context,
302 CERT_CERT_PROP_ID, 0, bigCert2);
306 ret = CertGetCertificateContextProperty(context,
307 CERT_ACCESS_STATE_PROP_ID, 0, NULL);
309 size = sizeof(access);
310 ret = CertGetCertificateContextProperty(context,
311 CERT_ACCESS_STATE_PROP_ID, &access, &size);
312 ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
314 ok(!(access & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG),
315 "Didn't expect a persisted cert\n");
316 /* Trying to set this "read only" property crashes.
317 access |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
318 ret = CertSetCertificateContextProperty(context,
319 CERT_ACCESS_STATE_PROP_ID, 0, &access);
322 /* Can I set the hash to an invalid hash? */
324 blob.cbData = sizeof(hash);
325 ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
327 ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
329 size = sizeof(hashProperty);
330 ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
331 hashProperty, &size);
332 ok(!memcmp(hashProperty, hash, sizeof(hash)), "Unexpected hash\n");
333 /* Delete the (bogus) hash, and get the real one */
334 ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
336 ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
339 ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert) - 1,
341 ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
342 ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
343 hashProperty, &size);
344 ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
346 ok(!memcmp(hash, hashProperty, sizeof(hash)), "Unexpected hash\n");
348 /* Now that the hash property is set, we should get one property when
354 propID = CertEnumCertificateContextProperties(context, propID);
357 } while (propID != 0);
358 ok(numProps == 1, "Expected 1 properties, got %ld\n", numProps);
360 CertFreeCertificateContext(context);
368 /* various combinations of CertOpenStore */
371 testCertProperties();