Sweden-Number/dlls/crypt32/tests/cert.c

1651 lines
70 KiB
C

/*
* crypt32 cert functions tests
*
* Copyright 2005 Juan Lang
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winreg.h>
#include <winerror.h>
#include <wincrypt.h>
#include "wine/test.h"
/* The following aren't defined in wincrypt.h, as they're "reserved" */
#define CERT_CERT_PROP_ID 32
#define CERT_CRL_PROP_ID 33
#define CERT_CTL_PROP_ID 34
struct CertPropIDHeader
{
DWORD propID;
DWORD unknown1;
DWORD cb;
};
static BOOL (WINAPI * pCryptVerifyCertificateSignatureEx)
(HCRYPTPROV, DWORD, DWORD, void *, DWORD, void *, DWORD, void *);
#define CRYPT_GET_PROC(func) \
p ## func = (void *)GetProcAddress(hCrypt32, #func); \
if(!p ## func) \
trace("GetProcAddress(hCrypt32, \"%s\") failed\n", #func); \
static void init_function_pointers(void)
{
HMODULE hCrypt32;
pCryptVerifyCertificateSignatureEx = NULL;
hCrypt32 = GetModuleHandleA("crypt32.dll");
assert(hCrypt32);
CRYPT_GET_PROC(CryptVerifyCertificateSignatureEx);
}
static void testCryptHashCert(void)
{
static const BYTE emptyHash[] = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b,
0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07,
0x09 };
static const BYTE knownHash[] = { 0xae, 0x9d, 0xbf, 0x6d, 0xf5, 0x46, 0xee,
0x8b, 0xc5, 0x7a, 0x13, 0xba, 0xc2, 0xb1, 0x04, 0xf2, 0xbf, 0x52, 0xa8,
0xa2 };
static const BYTE toHash[] = "abcdefghijklmnopqrstuvwxyz0123456789.,;!?:";
BOOL ret;
BYTE hash[20];
DWORD hashLen = sizeof(hash);
/* NULL buffer and nonzero length crashes
ret = CryptHashCertificate(0, 0, 0, NULL, size, hash, &hashLen);
empty hash length also crashes
ret = CryptHashCertificate(0, 0, 0, buf, size, hash, NULL);
*/
/* Test empty hash */
ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), NULL,
&hashLen);
ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
ok(hashLen == sizeof(hash),
"Got unexpected size of hash %ld, expected %d\n", hashLen, sizeof(hash));
/* Test with empty buffer */
ret = CryptHashCertificate(0, 0, 0, NULL, 0, hash, &hashLen);
ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
ok(!memcmp(hash, emptyHash, sizeof(emptyHash)),
"Unexpected hash of nothing\n");
/* Test a known value */
ret = CryptHashCertificate(0, 0, 0, toHash, sizeof(toHash), hash,
&hashLen);
ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
ok(!memcmp(hash, knownHash, sizeof(knownHash)), "Unexpected hash\n");
}
static const BYTE emptyCert[] = { 0x30, 0x00 };
static const BYTE bigCert[] = "\x30\x7a\x02\x01\x01\x30\x02\x06\x00"
"\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e\x20\x4c"
"\x61\x6e\x67\x00\x30\x22\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30"
"\x30\x30\x30\x5a\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30\x30\x30"
"\x30\x5a\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x4a\x75\x61\x6e"
"\x20\x4c\x61\x6e\x67\x00\x30\x07\x30\x02\x06\x00\x03\x01\x00\xa3\x16\x30\x14"
"\x30\x12\x06\x03\x55\x1d\x13\x01\x01\xff\x04\x08\x30\x06\x01\x01\xff\x02\x01"
"\x01";
static const BYTE signedBigCert[] = {
0x30, 0x81, 0x93, 0x30, 0x7a, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06, 0x00, 0x30,
0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a,
0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x22, 0x18, 0x0f,
0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06,
0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61,
0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06, 0x00, 0x03, 0x01, 0x00, 0xa3,
0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x01, 0x30, 0x02, 0x06,
0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07,
0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
static const BYTE serializedCert[] = { 0x20, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x30, 0x7a, 0x02, 0x01, 0x01,
0x30, 0x02, 0x06, 0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67,
0x00, 0x30, 0x22, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x31, 0x36, 0x30, 0x31,
0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x15,
0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0a, 0x4a, 0x75,
0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x30, 0x07, 0x30, 0x02, 0x06,
0x00, 0x03, 0x01, 0x00, 0xa3, 0x16, 0x30, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02,
0x01, 0x01 };
static const BYTE signedCRL[] = { 0x30, 0x45, 0x30, 0x2c, 0x30, 0x02, 0x06,
0x00, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
0x0a, 0x4a, 0x75, 0x61, 0x6e, 0x20, 0x4c, 0x61, 0x6e, 0x67, 0x00, 0x18, 0x0f,
0x31, 0x36, 0x30, 0x31, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x5a, 0x30, 0x02, 0x06, 0x00, 0x03, 0x11, 0x00, 0x0f, 0x0e, 0x0d, 0x0c,
0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
static void testMemStore(void)
{
HCERTSTORE store1, store2;
PCCERT_CONTEXT context;
BOOL ret;
/* NULL provider */
store1 = CertOpenStore(0, 0, 0, 0, NULL);
ok(!store1 && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
/* weird flags */
store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_DELETE_FLAG, NULL);
ok(!store1 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED,
"Expected ERROR_CALL_NOT_IMPLEMENTED, got %ld\n", GetLastError());
/* normal */
store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
/* open existing doesn't */
store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_OPEN_EXISTING_FLAG, NULL);
ok(store2 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
ok(store1 != store2, "Expected different stores\n");
/* add a bogus (empty) cert */
context = NULL;
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, emptyCert,
sizeof(emptyCert), CERT_STORE_ADD_ALWAYS, &context);
/* Windows returns CRYPT_E_ASN1_EOD, but accept CRYPT_E_ASN1_CORRUPT as
* well (because matching errors is tough in this case)
*/
ok(!ret && (GetLastError() == CRYPT_E_ASN1_EOD || GetLastError() ==
CRYPT_E_ASN1_CORRUPT),
"Expected CRYPT_E_ASN1_EOD or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
GetLastError());
/* add a "signed" cert--the signature isn't a real signature, so this adds
* without any check of the signature's validity
*/
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
signedBigCert, sizeof(signedBigCert), CERT_STORE_ADD_ALWAYS, &context);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
ok(context != NULL, "Expected a valid cert context\n");
if (context)
{
ok(context->cbCertEncoded == sizeof(signedBigCert),
"Expected cert of %d bytes, got %ld\n", sizeof(signedBigCert),
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, signedBigCert,
sizeof(signedBigCert)), "Unexpected encoded cert in context\n");
/* remove it, the rest of the tests will work on an unsigned cert */
ret = CertDeleteCertificateFromStore(context);
ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
GetLastError());
}
/* try adding a "signed" CRL as a cert */
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
signedCRL, sizeof(signedCRL), CERT_STORE_ADD_ALWAYS, &context);
ok(!ret && (GetLastError() == CRYPT_E_ASN1_BADTAG || GetLastError() ==
CRYPT_E_ASN1_CORRUPT),
"Expected CRYPT_E_ASN1_BADTAG or CRYPT_E_ASN1_CORRUPT, got %08lx\n",
GetLastError());
/* add a cert to store1 */
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
ok(context != NULL, "Expected a valid cert context\n");
if (context)
{
DWORD size;
BYTE *buf;
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
"Unexpected encoded cert in context\n");
ok(context->hCertStore == store1, "Unexpected store\n");
/* check serializing this element */
/* These crash
ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, NULL);
ret = CertSerializeCertificateStoreElement(context, 0, NULL, NULL);
ret = CertSerializeCertificateStoreElement(NULL, 0, NULL, &size);
*/
/* apparently flags are ignored */
ret = CertSerializeCertificateStoreElement(context, 1, NULL, &size);
ok(ret, "CertSerializeCertificateStoreElement failed: %08lx\n",
GetLastError());
buf = HeapAlloc(GetProcessHeap(), 0, size);
if (buf)
{
ret = CertSerializeCertificateStoreElement(context, 0, buf, &size);
ok(size == sizeof(serializedCert), "Expected size %d, got %ld\n",
sizeof(serializedCert), size);
ok(!memcmp(serializedCert, buf, size),
"Unexpected serialized cert\n");
HeapFree(GetProcessHeap(), 0, buf);
}
ret = CertFreeCertificateContext(context);
ok(ret, "CertFreeCertificateContext failed: %08lx\n", GetLastError());
}
/* verify the cert's in store1 */
context = CertEnumCertificatesInStore(store1, NULL);
ok(context != NULL, "Expected a valid context\n");
context = CertEnumCertificatesInStore(store1, context);
ok(!context && GetLastError() == CRYPT_E_NOT_FOUND,
"Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
/* verify store2 (the "open existing" mem store) is still empty */
context = CertEnumCertificatesInStore(store2, NULL);
ok(!context, "Expected an empty store\n");
/* delete the cert from store1, and check it's empty */
context = CertEnumCertificatesInStore(store1, NULL);
if (context)
{
/* Deleting a bitwise copy crashes with an access to an uninitialized
* pointer, so a cert context has some special data out there in memory
* someplace
CERT_CONTEXT copy;
memcpy(&copy, context, sizeof(copy));
ret = CertDeleteCertificateFromStore(&copy);
*/
PCCERT_CONTEXT copy = CertDuplicateCertificateContext(context);
ok(copy != NULL, "CertDuplicateCertificateContext failed: %08lx\n",
GetLastError());
ret = CertDeleteCertificateFromStore(context);
ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
GetLastError());
/* try deleting a copy */
ret = CertDeleteCertificateFromStore(copy);
ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
GetLastError());
/* check that the store is empty */
context = CertEnumCertificatesInStore(store1, NULL);
ok(!context, "Expected an empty store\n");
}
/* close an empty store */
ret = CertCloseStore(NULL, 0);
ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
ret = CertCloseStore(store1, 0);
ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
ret = CertCloseStore(store2, 0);
ok(ret, "CertCloseStore failed: %ld\n", GetLastError());
/* This seems nonsensical, but you can open a read-only mem store, only
* it isn't read-only
*/
store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_READONLY_FLAG, NULL);
ok(store1 != NULL, "CertOpenStore failed: %ld\n", GetLastError());
/* yep, this succeeds */
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING, bigCert,
sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, &context);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
ok(context != NULL, "Expected a valid cert context\n");
if (context)
{
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected cert of %d bytes, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, sizeof(bigCert) - 1),
"Unexpected encoded cert in context\n");
ok(context->hCertStore == store1, "Unexpected store\n");
ret = CertDeleteCertificateFromStore(context);
ok(ret, "CertDeleteCertificateFromStore failed: %08lx\n",
GetLastError());
}
CertCloseStore(store1, 0);
}
static const BYTE bigCert2[] = "\x30\x7a\x02\x01\x01\x30\x02\x06\x00"
"\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x41\x6c\x65\x78\x20\x4c"
"\x61\x6e\x67\x00\x30\x22\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30"
"\x30\x30\x30\x5a\x18\x0f\x31\x36\x30\x31\x30\x31\x30\x31\x30\x30\x30\x30\x30"
"\x30\x5a\x30\x15\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0a\x41\x6c\x65\x78"
"\x20\x4c\x61\x6e\x67\x00\x30\x07\x30\x02\x06\x00\x03\x01\x00\xa3\x16\x30\x14"
"\x30\x12\x06\x03\x55\x1d\x13\x01\x01\xff\x04\x08\x30\x06\x01\x01\xff\x02\x01"
"\x01";
static void testCollectionStore(void)
{
HCERTSTORE store1, store2, collection, collection2;
PCCERT_CONTEXT context;
BOOL ret;
collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
/* Try adding a cert to any empty collection */
ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
bigCert, sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED),
"Expected HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED), got %08lx\n",
GetLastError());
/* Create and add a cert to a memory store */
store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
bigCert, sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
/* Add the memory store to the collection, without allowing adding */
ret = CertAddStoreToCollection(collection, store1, 0, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
/* Verify the cert is in the collection */
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
CertFreeCertificateContext(context);
}
/* Check that adding to the collection isn't allowed */
ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
bigCert2, sizeof(bigCert2) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED),
"Expected HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED), got %08lx\n",
GetLastError());
/* Create a new memory store */
store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
/* Try adding a store to a non-collection store */
ret = CertAddStoreToCollection(store1, store2,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* Try adding some bogus stores */
/* This crashes in Windows
ret = CertAddStoreToCollection(0, store2,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
*/
/* This "succeeds"... */
ret = CertAddStoreToCollection(collection, 0,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
/* while this crashes.
ret = CertAddStoreToCollection(collection, 1,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
*/
/* Add it to the collection, this time allowing adding */
ret = CertAddStoreToCollection(collection, store2,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
/* Check that adding to the collection is allowed */
ret = CertAddEncodedCertificateToStore(collection, X509_ASN_ENCODING,
bigCert2, sizeof(bigCert2) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
/* Now check that it was actually added to store2 */
context = CertEnumCertificatesInStore(store2, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == store2, "Unexpected store\n");
CertFreeCertificateContext(context);
}
/* Check that the collection has both bigCert and bigCert2. bigCert comes
* first because store1 was added first.
*/
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert2) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert2) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert2,
context->cbCertEncoded), "Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(!context, "Unexpected cert\n");
}
}
/* close store2, and check that the collection is unmodified */
CertCloseStore(store2, 0);
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert2) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert2) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert2,
context->cbCertEncoded), "Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(!context, "Unexpected cert\n");
}
}
/* Adding a collection to a collection is legal */
collection2 = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ret = CertAddStoreToCollection(collection2, collection,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
/* check the contents of collection2 */
context = CertEnumCertificatesInStore(collection2, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection2, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
context = CertEnumCertificatesInStore(collection2, context);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection2, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert2) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert2) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert2,
context->cbCertEncoded), "Unexpected cert\n");
context = CertEnumCertificatesInStore(collection2, context);
ok(!context, "Unexpected cert\n");
}
}
/* I'd like to test closing the collection in the middle of enumeration,
* but my tests have been inconsistent. The first time calling
* CertEnumCertificatesInStore on a closed collection succeeded, while the
* second crashed. So anything appears to be fair game.
* I'd also like to test removing a store from a collection in the middle
* of an enumeration, but my tests in Windows have been inconclusive.
* In one scenario it worked. In another scenario, about a third of the
* time this leads to "random" crashes elsewhere in the code. This
* probably means this is not allowed.
*/
CertCloseStore(store1, 0);
CertCloseStore(collection, 0);
CertCloseStore(collection2, 0);
/* Add the same cert to two memory stores, then put them in a collection */
store1 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ok(store1 != 0, "CertOpenStore failed: %08lx\n", GetLastError());
store2 = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ok(store2 != 0, "CertOpenStore failed: %08lx\n", GetLastError());
ret = CertAddEncodedCertificateToStore(store1, X509_ASN_ENCODING,
bigCert, sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
ret = CertAddEncodedCertificateToStore(store2, X509_ASN_ENCODING,
bigCert, sizeof(bigCert) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n", GetLastError());
collection = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ok(collection != 0, "CertOpenStore failed: %08lx\n", GetLastError());
ret = CertAddStoreToCollection(collection, store1,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
ret = CertAddStoreToCollection(collection, store2,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
/* Check that the collection has two copies of the same cert */
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
context = CertEnumCertificatesInStore(collection, context);
ok(context == NULL, "Unexpected cert\n");
}
}
/* The following would check whether I can delete an identical cert, rather
* than one enumerated from the store. It crashes, so that means I must
* only call CertDeleteCertificateFromStore with contexts enumerated from
* the store.
context = CertCreateCertificateContext(X509_ASN_ENCODING, bigCert,
sizeof(bigCert) - 1);
ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
GetLastError());
if (context)
{
ret = CertDeleteCertificateFromStore(collection, context);
printf("ret is %d, GetLastError is %08lx\n", ret, GetLastError());
CertFreeCertificateContext(context);
}
*/
/* Now check deleting from the collection. */
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid context\n");
if (context)
{
CertDeleteCertificateFromStore(context);
/* store1 should now be empty */
context = CertEnumCertificatesInStore(store1, NULL);
ok(!context, "Unexpected cert\n");
/* and there should be one certificate in the collection */
context = CertEnumCertificatesInStore(collection, NULL);
ok(context != NULL, "Expected a valid cert\n");
if (context)
{
ok(context->hCertStore == collection, "Unexpected store\n");
ok(context->cbCertEncoded == sizeof(bigCert) - 1,
"Expected size %d, got %ld\n", sizeof(bigCert) - 1,
context->cbCertEncoded);
ok(!memcmp(context->pbCertEncoded, bigCert, context->cbCertEncoded),
"Unexpected cert\n");
}
context = CertEnumCertificatesInStore(collection, context);
ok(context == NULL, "Unexpected cert\n");
}
/* Finally, test removing stores from the collection. No return value, so
* it's a bit funny to test.
*/
/* This crashes
CertRemoveStoreFromCollection(NULL, NULL);
*/
/* This "succeeds," no crash, no last error set */
SetLastError(0xdeadbeef);
CertRemoveStoreFromCollection(store2, collection);
ok(GetLastError() == 0xdeadbeef,
"Didn't expect an error to be set: %08lx\n", GetLastError());
/* After removing store2, the collection should be empty */
SetLastError(0xdeadbeef);
CertRemoveStoreFromCollection(collection, store2);
ok(GetLastError() == 0xdeadbeef,
"Didn't expect an error to be set: %08lx\n", GetLastError());
context = CertEnumCertificatesInStore(collection, NULL);
ok(!context, "Unexpected cert\n");
CertCloseStore(collection, 0);
CertCloseStore(store2, 0);
CertCloseStore(store1, 0);
}
/* Looks for the property with ID propID in the buffer buf. Returns a pointer
* to its header if found, NULL if not.
*/
static const struct CertPropIDHeader *findPropID(const BYTE *buf, DWORD size,
DWORD propID)
{
const struct CertPropIDHeader *ret = NULL;
BOOL failed = FALSE;
while (size && !ret && !failed)
{
if (size < sizeof(struct CertPropIDHeader))
failed = TRUE;
else
{
const struct CertPropIDHeader *hdr =
(const struct CertPropIDHeader *)buf;
size -= sizeof(struct CertPropIDHeader);
buf += sizeof(struct CertPropIDHeader);
if (size < hdr->cb)
failed = TRUE;
else if (hdr->propID == propID)
ret = hdr;
else
{
buf += hdr->cb;
size -= hdr->cb;
}
}
}
return ret;
}
typedef DWORD (WINAPI *SHDeleteKeyAFunc)(HKEY, LPCSTR);
static void testRegStore(void)
{
static const char tempKey[] = "Software\\Wine\\CryptTemp";
HCERTSTORE store;
LONG rc;
HKEY key = NULL;
DWORD disp;
store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, NULL);
ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
"Expected ERROR_INVALID_HANDLE, got %ld\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
ok(!store && GetLastError() == ERROR_INVALID_HANDLE,
"Expected ERROR_INVALID_HANDLE, got %ld\n", GetLastError());
/* Opening up any old key works.. */
key = HKEY_CURRENT_USER;
store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
/* Not sure if this is a bug in DuplicateHandle, marking todo_wine for now
*/
todo_wine ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
CertCloseStore(store, 0);
rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
NULL, &key, NULL);
ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
if (key)
{
BOOL ret;
BYTE hash[20];
DWORD size, i;
static const char certificates[] = "Certificates\\";
char subKeyName[sizeof(certificates) + 20 * 2 + 1], *ptr;
HKEY subKey;
PCCERT_CONTEXT context;
store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0, 0, key);
ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
/* Add a certificate. It isn't persisted right away, since it's only
* added to the cache..
*/
ret = CertAddEncodedCertificateToStore(store, X509_ASN_ENCODING,
bigCert2, sizeof(bigCert2) - 1, CERT_STORE_ADD_ALWAYS, NULL);
ok(ret, "CertAddEncodedCertificateToStore failed: %08lx\n",
GetLastError());
/* so flush the cache to force a commit.. */
ret = CertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
/* and check that the expected subkey was written. */
size = sizeof(hash);
ret = CryptHashCertificate(0, 0, 0, bigCert2, sizeof(bigCert2) - 1,
hash, &size);
ok(ret, "CryptHashCertificate failed: %ld\n", GetLastError());
strcpy(subKeyName, certificates);
for (i = 0, ptr = subKeyName + sizeof(certificates) - 1; i < size;
i++, ptr += 2)
sprintf(ptr, "%02X", hash[i]);
rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
&subKey, NULL);
ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
if (subKey)
{
LPBYTE buf;
size = 0;
RegQueryValueExA(subKey, "Blob", NULL, NULL, NULL, &size);
buf = HeapAlloc(GetProcessHeap(), 0, size);
if (buf)
{
rc = RegQueryValueExA(subKey, "Blob", NULL, NULL, buf, &size);
ok(!rc, "RegQueryValueExA failed: %ld\n", rc);
if (!rc)
{
const struct CertPropIDHeader *hdr;
/* Both the hash and the cert should be present */
hdr = findPropID(buf, size, CERT_CERT_PROP_ID);
ok(hdr != NULL, "Expected to find a cert property\n");
if (hdr)
{
ok(hdr->cb == sizeof(bigCert2) - 1,
"Unexpected size %ld of cert property, expected %d\n",
hdr->cb, sizeof(bigCert2) - 1);
ok(!memcmp((BYTE *)hdr + sizeof(*hdr), bigCert2,
hdr->cb), "Unexpected cert in cert property\n");
}
hdr = findPropID(buf, size, CERT_HASH_PROP_ID);
ok(hdr != NULL, "Expected to find a hash property\n");
if (hdr)
{
ok(hdr->cb == sizeof(hash),
"Unexpected size %ld of hash property, expected %d\n",
hdr->cb, sizeof(hash));
ok(!memcmp((BYTE *)hdr + sizeof(*hdr), hash,
hdr->cb), "Unexpected hash in cert property\n");
}
}
HeapFree(GetProcessHeap(), 0, buf);
}
RegCloseKey(subKey);
}
/* Remove the existing context */
context = CertEnumCertificatesInStore(store, NULL);
ok(context != NULL, "Expected a cert context\n");
if (context)
CertDeleteCertificateFromStore(context);
ret = CertControlStore(store, 0, CERT_STORE_CTRL_COMMIT, NULL);
ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
/* Add a serialized cert with a bogus hash directly to the registry */
memset(hash, 0, sizeof(hash));
strcpy(subKeyName, certificates);
for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
i < sizeof(hash); i++, ptr += 2)
sprintf(ptr, "%02X", hash[i]);
rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
&subKey, NULL);
ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
if (subKey)
{
BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
sizeof(bigCert) - 1], *ptr;
DWORD certCount = 0;
struct CertPropIDHeader *hdr;
hdr = (struct CertPropIDHeader *)buf;
hdr->propID = CERT_HASH_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = sizeof(hash);
ptr = buf + sizeof(*hdr);
memcpy(ptr, hash, sizeof(hash));
ptr += sizeof(hash);
hdr = (struct CertPropIDHeader *)ptr;
hdr->propID = CERT_CERT_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = sizeof(bigCert) - 1;
ptr += sizeof(*hdr);
memcpy(ptr, bigCert, sizeof(bigCert) - 1);
rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
sizeof(buf));
ok(!rc, "RegSetValueExA failed: %ld\n", rc);
ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
/* Make sure the bogus hash cert gets loaded. */
certCount = 0;
context = NULL;
do {
context = CertEnumCertificatesInStore(store, context);
if (context)
certCount++;
} while (context != NULL);
ok(certCount == 1, "Expected 1 certificates, got %ld\n", certCount);
RegCloseKey(subKey);
}
/* Add another serialized cert directly to the registry, this time
* under the correct key name (named with the correct hash value).
*/
size = sizeof(hash);
ret = CryptHashCertificate(0, 0, 0, bigCert2,
sizeof(bigCert2) - 1, hash, &size);
ok(ret, "CryptHashCertificate failed: %ld\n", GetLastError());
strcpy(subKeyName, certificates);
for (i = 0, ptr = subKeyName + sizeof(certificates) - 1;
i < sizeof(hash); i++, ptr += 2)
sprintf(ptr, "%02X", hash[i]);
rc = RegCreateKeyExA(key, subKeyName, 0, NULL, 0, KEY_ALL_ACCESS, NULL,
&subKey, NULL);
ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
if (subKey)
{
BYTE buf[sizeof(struct CertPropIDHeader) * 2 + sizeof(hash) +
sizeof(bigCert2) - 1], *ptr;
DWORD certCount = 0;
PCCERT_CONTEXT context;
struct CertPropIDHeader *hdr;
/* First try with a bogus hash... */
hdr = (struct CertPropIDHeader *)buf;
hdr->propID = CERT_HASH_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = sizeof(hash);
ptr = buf + sizeof(*hdr);
memset(ptr, 0, sizeof(hash));
ptr += sizeof(hash);
hdr = (struct CertPropIDHeader *)ptr;
hdr->propID = CERT_CERT_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = sizeof(bigCert2) - 1;
ptr += sizeof(*hdr);
memcpy(ptr, bigCert2, sizeof(bigCert2) - 1);
rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
sizeof(buf));
ok(!rc, "RegSetValueExA failed: %ld\n", rc);
ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
/* and make sure just one cert still gets loaded. */
certCount = 0;
context = NULL;
do {
context = CertEnumCertificatesInStore(store, context);
if (context)
certCount++;
} while (context != NULL);
ok(certCount == 1, "Expected 1 certificates, got %ld\n", certCount);
/* Try again with the correct hash... */
ptr = buf + sizeof(*hdr);
memcpy(ptr, hash, sizeof(hash));
rc = RegSetValueExA(subKey, "Blob", 0, REG_BINARY, buf,
sizeof(buf));
ok(!rc, "RegSetValueExA failed: %ld\n", rc);
ret = CertControlStore(store, 0, CERT_STORE_CTRL_RESYNC, NULL);
ok(ret, "CertControlStore failed: %08lx\n", GetLastError());
/* and make sure two certs get loaded. */
certCount = 0;
context = NULL;
do {
context = CertEnumCertificatesInStore(store, context);
if (context)
certCount++;
} while (context != NULL);
ok(certCount == 2, "Expected 2 certificates, got %ld\n", certCount);
RegCloseKey(subKey);
}
CertCloseStore(store, 0);
/* Is delete allowed on a reg store? */
store = CertOpenStore(CERT_STORE_PROV_REG, 0, 0,
CERT_STORE_DELETE_FLAG, key);
ok(store == NULL, "Expected NULL return from CERT_STORE_DELETE_FLAG\n");
ok(GetLastError() == 0, "CertOpenStore failed: %08lx\n",
GetLastError());
RegCloseKey(key);
}
/* The CertOpenStore with CERT_STORE_DELETE_FLAG above will delete the
* contents of the key, but not the key itself.
*/
rc = RegCreateKeyExA(HKEY_CURRENT_USER, tempKey, 0, NULL, 0, KEY_ALL_ACCESS,
NULL, &key, &disp);
ok(!rc, "RegCreateKeyExA failed: %ld\n", rc);
ok(disp == REG_OPENED_EXISTING_KEY,
"Expected REG_OPENED_EXISTING_KEY, got %ld\n", disp);
if (!rc)
{
RegCloseKey(key);
rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
if (rc)
{
HMODULE shlwapi = LoadLibraryA("shlwapi");
/* Use shlwapi's SHDeleteKeyA to _really_ blow away the key,
* otherwise subsequent tests will fail.
*/
if (shlwapi)
{
SHDeleteKeyAFunc pSHDeleteKeyA =
(SHDeleteKeyAFunc)GetProcAddress(shlwapi, "SHDeleteKeyA");
if (pSHDeleteKeyA)
pSHDeleteKeyA(HKEY_CURRENT_USER, tempKey);
FreeLibrary(shlwapi);
}
}
}
}
static const char MyA[] = { 'M','y',0,0 };
static const WCHAR MyW[] = { 'M','y',0 };
static const WCHAR BogusW[] = { 'B','o','g','u','s',0 };
static const WCHAR BogusPathW[] = { 'S','o','f','t','w','a','r','e','\\',
'M','i','c','r','o','s','o','f','t','\\','S','y','s','t','e','m','C','e','r',
't','i','f','i','c','a','t','e','s','\\','B','o','g','u','s',0 };
static void testSystemRegStore(void)
{
HCERTSTORE store, memStore;
/* Check with a UNICODE name */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
/* Not all OSes support CERT_STORE_PROV_SYSTEM_REGISTRY, so don't continue
* testing if they don't.
*/
if (!store)
return;
/* Check that it isn't a collection store */
memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
if (memStore)
{
BOOL ret = CertAddStoreToCollection(store, memStore, 0, 0);
ok(!ret && GetLastError() ==
HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
CertCloseStore(memStore, 0);
}
CertCloseStore(store, 0);
/* Check opening a bogus store */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
if (store)
CertCloseStore(store, 0);
/* Now check whether deleting is allowed */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0, 0, NULL);
ok(!store && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
ok(!store && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
ok(!store && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* The name is expected to be UNICODE, check with an ASCII name */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
}
static void testSystemStore(void)
{
static const WCHAR baskslashW[] = { '\\',0 };
HCERTSTORE store;
WCHAR keyName[MAX_PATH];
HKEY key;
LONG rc;
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, 0, NULL);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyA);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_LOCAL_MACHINE | CERT_SYSTEM_STORE_CURRENT_USER, MyW);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
/* The name is expected to be UNICODE, first check with an ASCII name */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyA);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
/* Create the expected key */
lstrcpyW(keyName, CERT_LOCAL_MACHINE_SYSTEM_STORE_REGPATH);
lstrcatW(keyName, baskslashW);
lstrcatW(keyName, MyW);
rc = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
NULL, &key, NULL);
ok(!rc, "RegCreateKeyEx failed: %ld\n", rc);
if (!rc)
RegCloseKey(key);
/* Check opening with a UNICODE name, specifying the create new flag */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_CREATE_NEW_FLAG, MyW);
ok(!store && GetLastError() == ERROR_FILE_EXISTS,
"Expected ERROR_FILE_EXISTS, got %08lx\n", GetLastError());
/* Now check opening with a UNICODE name, this time opening existing */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, MyW);
ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
if (store)
{
HCERTSTORE memStore = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
/* Check that it's a collection store */
if (memStore)
{
BOOL ret = CertAddStoreToCollection(store, memStore, 0, 0);
/* FIXME: this'll fail on NT4, but what error will it give? */
ok(ret, "CertAddStoreToCollection failed: %08lx\n", GetLastError());
CertCloseStore(memStore, 0);
}
CertCloseStore(store, 0);
}
/* Check opening a bogus store */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_OPEN_EXISTING_FLAG, BogusW);
ok(!store && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER, BogusW);
ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
if (store)
CertCloseStore(store, 0);
/* Now check whether deleting is allowed */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
}
static void testCertOpenSystemStore(void)
{
HCERTSTORE store;
store = CertOpenSystemStoreW(0, NULL);
ok(!store && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* This succeeds, and on WinXP at least, the Bogus key is created under
* HKCU (but not under HKLM, even when run as an administrator.)
*/
store = CertOpenSystemStoreW(0, BogusW);
ok(store != 0, "CertOpenSystemStore failed: %08lx\n", GetLastError());
if (store)
CertCloseStore(store, 0);
/* Delete it so other tests succeed next time around */
store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_DELETE_FLAG, BogusW);
RegDeleteKeyW(HKEY_CURRENT_USER, BogusPathW);
}
static void checkHash(const BYTE *data, DWORD dataLen, ALG_ID algID,
PCCERT_CONTEXT context, DWORD propID)
{
BYTE hash[20] = { 0 }, hashProperty[20];
BOOL ret;
DWORD size;
memset(hash, 0, sizeof(hash));
memset(hashProperty, 0, sizeof(hashProperty));
size = sizeof(hash);
ret = CryptHashCertificate(0, algID, 0, data, dataLen, hash, &size);
ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
ret = CertGetCertificateContextProperty(context, propID, hashProperty,
&size);
ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
GetLastError());
ok(!memcmp(hash, hashProperty, size), "Unexpected hash for property %ld\n",
propID);
}
static void testCertProperties(void)
{
PCCERT_CONTEXT context = CertCreateCertificateContext(X509_ASN_ENCODING,
bigCert, sizeof(bigCert) - 1);
ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n",
GetLastError());
if (context)
{
DWORD propID, numProps, access, size;
BOOL ret;
BYTE hash[20] = { 0 }, hashProperty[20];
CRYPT_DATA_BLOB blob;
/* This crashes
propID = CertEnumCertificateContextProperties(NULL, 0);
*/
propID = 0;
numProps = 0;
do {
propID = CertEnumCertificateContextProperties(context, propID);
if (propID)
numProps++;
} while (propID != 0);
ok(numProps == 0, "Expected 0 properties, got %ld\n", numProps);
/* Tests with a NULL cert context. Prop ID 0 fails.. */
ret = CertSetCertificateContextProperty(NULL, 0, 0, NULL);
ok(!ret && GetLastError() ==
HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* while this just crashes.
ret = CertSetCertificateContextProperty(NULL,
CERT_KEY_PROV_HANDLE_PROP_ID, 0, NULL);
*/
ret = CertSetCertificateContextProperty(context, 0, 0, NULL);
ok(!ret && GetLastError() ==
HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* Can't set the cert property directly, this crashes.
ret = CertSetCertificateContextProperty(context,
CERT_CERT_PROP_ID, 0, bigCert2);
*/
/* These all crash.
ret = CertGetCertificateContextProperty(context,
CERT_ACCESS_STATE_PROP_ID, 0, NULL);
ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
NULL, NULL);
ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
hashProperty, NULL);
*/
/* A missing prop */
size = 0;
ret = CertGetCertificateContextProperty(context,
CERT_KEY_PROV_INFO_PROP_ID, NULL, &size);
ok(!ret && GetLastError() == CRYPT_E_NOT_FOUND,
"Expected CRYPT_E_NOT_FOUND, got %08lx\n", GetLastError());
/* And, an implicit property */
size = sizeof(access);
ret = CertGetCertificateContextProperty(context,
CERT_ACCESS_STATE_PROP_ID, &access, &size);
ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
GetLastError());
ok(!(access & CERT_ACCESS_STATE_WRITE_PERSIST_FLAG),
"Didn't expect a persisted cert\n");
/* Trying to set this "read only" property crashes.
access |= CERT_ACCESS_STATE_WRITE_PERSIST_FLAG;
ret = CertSetCertificateContextProperty(context,
CERT_ACCESS_STATE_PROP_ID, 0, &access);
*/
/* Can I set the hash to an invalid hash? */
blob.pbData = hash;
blob.cbData = sizeof(hash);
ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
&blob);
ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
GetLastError());
size = sizeof(hashProperty);
ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
hashProperty, &size);
ok(!memcmp(hashProperty, hash, sizeof(hash)), "Unexpected hash\n");
/* Delete the (bogus) hash, and get the real one */
ret = CertSetCertificateContextProperty(context, CERT_HASH_PROP_ID, 0,
NULL);
ok(ret, "CertSetCertificateContextProperty failed: %08lx\n",
GetLastError());
checkHash(bigCert, sizeof(bigCert) - 1, CALG_SHA1, context,
CERT_HASH_PROP_ID);
/* Now that the hash property is set, we should get one property when
* enumerating.
*/
propID = 0;
numProps = 0;
do {
propID = CertEnumCertificateContextProperties(context, propID);
if (propID)
numProps++;
} while (propID != 0);
ok(numProps == 1, "Expected 1 properties, got %ld\n", numProps);
/* Check a few other implicit properties */
checkHash(bigCert, sizeof(bigCert) - 1, CALG_MD5, context,
CERT_MD5_HASH_PROP_ID);
checkHash(
context->pCertInfo->Subject.pbData,
context->pCertInfo->Subject.cbData,
CALG_MD5, context, CERT_SUBJECT_NAME_MD5_HASH_PROP_ID);
checkHash(
context->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
context->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData,
CALG_MD5, context, CERT_SUBJECT_PUBLIC_KEY_MD5_HASH_PROP_ID);
/* Odd: this doesn't fail on other certificates, so there must be
* something weird about this cert that causes it to fail.
*/
size = 0;
ret = CertGetCertificateContextProperty(context,
CERT_KEY_IDENTIFIER_PROP_ID, NULL, &size);
todo_wine ok(!ret && GetLastError() == ERROR_INVALID_DATA,
"Expected ERROR_INVALID_DATA, got %08lx\n", GetLastError());
CertFreeCertificateContext(context);
}
}
static void testAddSerialized(void)
{
BOOL ret;
HCERTSTORE store;
BYTE buf[sizeof(struct CertPropIDHeader) * 2 + 20 + sizeof(bigCert) - 1] =
{ 0 };
BYTE hash[20];
struct CertPropIDHeader *hdr;
PCCERT_CONTEXT context;
ret = CertAddSerializedElementToStore(0, NULL, 0, 0, 0, 0, NULL, NULL);
ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
"Expected ERROR_END_OF_MEDIA, got %08lx\n", GetLastError());
store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
CERT_STORE_CREATE_NEW_FLAG, NULL);
ok(store != 0, "CertOpenStore failed: %08lx\n", GetLastError());
ret = CertAddSerializedElementToStore(store, NULL, 0, 0, 0, 0, NULL, NULL);
ok(!ret && GetLastError() == ERROR_END_OF_MEDIA,
"Expected ERROR_END_OF_MEDIA, got %08lx\n", GetLastError());
/* Test with an empty property */
hdr = (struct CertPropIDHeader *)buf;
hdr->propID = CERT_CERT_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = 0;
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* Test with a bad size in property header */
hdr->cb = sizeof(bigCert) - 2;
memcpy(buf + sizeof(struct CertPropIDHeader), bigCert, sizeof(bigCert) - 1);
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, 0, 0, 0, NULL,
NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, CERT_STORE_ADD_NEW,
0, 0, NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* Kosher size in property header, but no context type */
hdr->cb = sizeof(bigCert) - 1;
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0, 0,
NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, 0, 0, 0, NULL,
NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, CERT_STORE_ADD_NEW,
0, 0, NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* With a bad context type */
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, 0, 0,
CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, CERT_STORE_ADD_NEW,
0, CERT_STORE_CRL_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* Bad unknown field, good type */
hdr->unknown1 = 2;
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, 0, 0,
CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, CERT_STORE_ADD_NEW,
0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
"Expected ERROR_FILE_NOT_FOUND got %08lx\n", GetLastError());
/* Most everything okay, but bad add disposition */
hdr->unknown1 = 1;
/* This crashes
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf), 0, 0,
CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
* as does this
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, 0, 0,
CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
*/
/* Everything okay, but buffer's too big */
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
ok(ret, "CertAddSerializedElementToStore failed: %08lx\n", GetLastError());
/* Everything okay, check it's not re-added */
ret = CertAddSerializedElementToStore(store, buf,
sizeof(struct CertPropIDHeader) + sizeof(bigCert) - 1, CERT_STORE_ADD_NEW,
0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL, NULL);
ok(!ret && GetLastError() == CRYPT_E_EXISTS,
"Expected CRYPT_E_EXISTS, got %08lx\n", GetLastError());
context = CertEnumCertificatesInStore(store, NULL);
ok(context != NULL, "Expected a cert\n");
if (context)
CertDeleteCertificateFromStore(context);
/* Try adding with a bogus hash. Oddly enough, it succeeds, and the hash,
* when queried, is the real hash rather than the bogus hash.
*/
hdr = (struct CertPropIDHeader *)(buf + sizeof(struct CertPropIDHeader) +
sizeof(bigCert) - 1);
hdr->propID = CERT_HASH_PROP_ID;
hdr->unknown1 = 1;
hdr->cb = sizeof(hash);
memset(hash, 0xc, sizeof(hash));
memcpy((LPBYTE)hdr + sizeof(struct CertPropIDHeader), hash, sizeof(hash));
ret = CertAddSerializedElementToStore(store, buf, sizeof(buf),
CERT_STORE_ADD_NEW, 0, CERT_STORE_CERTIFICATE_CONTEXT_FLAG, NULL,
(const void **)&context);
ok(ret, "CertAddSerializedElementToStore failed: %08lx\n", GetLastError());
if (context)
{
BYTE hashVal[20], realHash[20];
DWORD size = sizeof(hashVal);
ret = CryptHashCertificate(0, 0, 0, bigCert, sizeof(bigCert) - 1,
realHash, &size);
ok(ret, "CryptHashCertificate failed: %08lx\n", GetLastError());
ret = CertGetCertificateContextProperty(context, CERT_HASH_PROP_ID,
hashVal, &size);
ok(ret, "CertGetCertificateContextProperty failed: %08lx\n",
GetLastError());
ok(!memcmp(hashVal, realHash, size), "Unexpected hash\n");
CertFreeCertificateContext(context);
}
CertCloseStore(store, 0);
}
static const char cspName[] = "WineCryptTemp";
static void verifySig(HCRYPTPROV csp, const BYTE *toSign, size_t toSignLen,
const BYTE *sig, size_t sigLen)
{
HCRYPTHASH hash;
BOOL ret = CryptCreateHash(csp, CALG_SHA1, 0, 0, &hash);
ok(ret, "CryptCreateHash failed: %08lx\n", GetLastError());
if (ret)
{
BYTE mySig[64];
DWORD mySigSize = sizeof(mySig);
ret = CryptHashData(hash, toSign, toSignLen, 0);
ok(ret, "CryptHashData failed: %08lx\n", GetLastError());
/* use the A variant so the test can run on Win9x */
ret = CryptSignHashA(hash, AT_SIGNATURE, NULL, 0, mySig, &mySigSize);
ok(ret, "CryptSignHash failed: %08lx\n", GetLastError());
if (ret)
{
ok(mySigSize == sigLen, "Expected sig length %d, got %ld\n",
sigLen, mySigSize);
ok(!memcmp(mySig, sig, sigLen), "Unexpected signature\n");
}
CryptDestroyHash(hash);
}
}
/* Tests signing the certificate described by toBeSigned with the CSP passed in,
* using the algorithm with OID sigOID. The CSP is assumed to be empty, and a
* keyset named AT_SIGNATURE will be added to it. The signing key will be
* stored in *key, and the signature will be stored in sig. sigLen should be
* at least 64 bytes.
*/
static void testSignCert(HCRYPTPROV csp, const CRYPT_DATA_BLOB *toBeSigned,
LPCSTR sigOID, HCRYPTKEY *key, BYTE *sig, DWORD *sigLen)
{
BOOL ret;
DWORD size = 0;
CRYPT_ALGORITHM_IDENTIFIER algoID = { NULL, { 0, NULL } };
/* These all crash
ret = CryptSignCertificate(0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL);
ret = CryptSignCertificate(0, 0, 0, NULL, 0, NULL, NULL, NULL, &size);
ret = CryptSignCertificate(0, 0, 0, toBeSigned->pbData, toBeSigned->cbData,
NULL, NULL, NULL, &size);
*/
ret = CryptSignCertificate(0, 0, 0, toBeSigned->pbData, toBeSigned->cbData,
&algoID, NULL, NULL, &size);
ok(!ret && GetLastError() == NTE_BAD_ALGID,
"Expected NTE_BAD_ALGID, got %08lx\n", GetLastError());
algoID.pszObjId = (LPSTR)sigOID;
ret = CryptSignCertificate(0, 0, 0, toBeSigned->pbData, toBeSigned->cbData,
&algoID, NULL, NULL, &size);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %08lx\n", GetLastError());
ret = CryptSignCertificate(0, AT_SIGNATURE, 0, toBeSigned->pbData,
toBeSigned->cbData, &algoID, NULL, NULL, &size);
ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
"Expected ERROR_INVALID_PARAMETER, got %08lx\n", GetLastError());
/* No keys exist in the new CSP yet.. */
ret = CryptSignCertificate(csp, AT_SIGNATURE, 0, toBeSigned->pbData,
toBeSigned->cbData, &algoID, NULL, NULL, &size);
ok(!ret && (GetLastError() == NTE_BAD_KEYSET || GetLastError() ==
NTE_NO_KEY), "Expected NTE_BAD_KEYSET or NTE_NO_KEY, got %08lx\n",
GetLastError());
ret = CryptGenKey(csp, AT_SIGNATURE, 0, key);
ok(ret, "CryptGenKey failed: %08lx\n", GetLastError());
if (ret)
{
ret = CryptSignCertificate(csp, AT_SIGNATURE, 0, toBeSigned->pbData,
toBeSigned->cbData, &algoID, NULL, NULL, &size);
ok(ret, "CryptSignCertificate failed: %08lx\n", GetLastError());
ok(size <= *sigLen, "Expected size <= %ld, got %ld\n", *sigLen, size);
if (ret)
{
ret = CryptSignCertificate(csp, AT_SIGNATURE, 0, toBeSigned->pbData,
toBeSigned->cbData, &algoID, NULL, sig, &size);
ok(ret, "CryptSignCertificate failed: %08lx\n", GetLastError());
if (ret)
{
*sigLen = size;
verifySig(csp, toBeSigned->pbData, toBeSigned->cbData, sig,
size);
}
}
}
}
static void testVerifyCertSig(HCRYPTPROV csp, const CRYPT_DATA_BLOB *toBeSigned,
LPCSTR sigOID, const BYTE *sig, DWORD sigLen)
{
CERT_SIGNED_CONTENT_INFO info;
LPBYTE cert = NULL;
DWORD size = 0;
BOOL ret;
if(pCryptVerifyCertificateSignatureEx) {
ret = pCryptVerifyCertificateSignatureEx(0, 0, 0, NULL, 0, NULL, 0, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = pCryptVerifyCertificateSignatureEx(csp, 0, 0, NULL, 0, NULL, 0, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING, 0, NULL, 0,
NULL, 0, NULL);
ok(!ret && GetLastError() == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* This crashes
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, NULL, 0, NULL, 0, NULL);
*/
}
info.ToBeSigned.cbData = toBeSigned->cbData;
info.ToBeSigned.pbData = toBeSigned->pbData;
info.SignatureAlgorithm.pszObjId = (LPSTR)sigOID;
info.SignatureAlgorithm.Parameters.cbData = 0;
info.Signature.cbData = sigLen;
info.Signature.pbData = (BYTE *)sig;
info.Signature.cUnusedBits = 0;
ret = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_CERT, &info,
CRYPT_ENCODE_ALLOC_FLAG, NULL, (BYTE *)&cert, &size);
ok(ret, "CryptEncodeObjectEx failed: %08lx\n", GetLastError());
if (cert)
{
CRYPT_DATA_BLOB certBlob = { 0, NULL };
PCERT_PUBLIC_KEY_INFO pubKeyInfo = NULL;
if(pCryptVerifyCertificateSignatureEx) {
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob, 0, NULL, 0, NULL);
ok(!ret && GetLastError() == CRYPT_E_ASN1_EOD,
"Expected CRYPT_E_ASN1_EOD, got %08lx\n", GetLastError());
certBlob.cbData = 1;
certBlob.pbData = (void *)0xdeadbeef;
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob, 0, NULL, 0, NULL);
ok(!ret && GetLastError() == STATUS_ACCESS_VIOLATION,
"Expected STATUS_ACCESS_VIOLATION, got %08lx\n", GetLastError());
certBlob.cbData = size;
certBlob.pbData = cert;
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob, 0, NULL, 0, NULL);
ok(!ret && GetLastError() ==
HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob,
CRYPT_VERIFY_CERT_SIGN_ISSUER_NULL, NULL, 0, NULL);
ok(!ret && GetLastError() ==
HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
"Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %08lx\n",
GetLastError());
/* This crashes
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob,
CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, NULL, 0, NULL);
*/
}
CryptExportPublicKeyInfoEx(csp, AT_SIGNATURE, X509_ASN_ENCODING,
(LPSTR)sigOID, 0, NULL, NULL, &size);
pubKeyInfo = HeapAlloc(GetProcessHeap(), 0, size);
if (pubKeyInfo)
{
ret = CryptExportPublicKeyInfoEx(csp, AT_SIGNATURE,
X509_ASN_ENCODING, (LPSTR)sigOID, 0, NULL, pubKeyInfo, &size);
ok(ret, "CryptExportKey failed: %08lx\n", GetLastError());
if (ret && pCryptVerifyCertificateSignatureEx)
{
ret = pCryptVerifyCertificateSignatureEx(csp, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_BLOB, &certBlob,
CRYPT_VERIFY_CERT_SIGN_ISSUER_PUBKEY, pubKeyInfo, 0, NULL);
ok(ret, "CryptVerifyCertificateSignatureEx failed: %08lx\n",
GetLastError());
}
HeapFree(GetProcessHeap(), 0, pubKeyInfo);
}
LocalFree(cert);
}
}
static void testCertSigs(void)
{
HCRYPTPROV csp;
CRYPT_DATA_BLOB toBeSigned = { sizeof(emptyCert), (LPBYTE)emptyCert };
BOOL ret;
HCRYPTKEY key;
BYTE sig[64];
DWORD sigSize = sizeof(sig);
/* Just in case a previous run failed, delete this thing */
CryptAcquireContextA(&csp, cspName, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_DELETEKEYSET);
ret = CryptAcquireContextA(&csp, cspName, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_NEWKEYSET);
ok(ret, "CryptAcquireContext failed: %08lx\n", GetLastError());
testSignCert(csp, &toBeSigned, szOID_RSA_SHA1RSA, &key, sig, &sigSize);
testVerifyCertSig(csp, &toBeSigned, szOID_RSA_SHA1RSA, sig, sigSize);
CryptDestroyKey(key);
CryptReleaseContext(csp, 0);
ret = CryptAcquireContextA(&csp, cspName, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_DELETEKEYSET);
}
START_TEST(cert)
{
init_function_pointers();
testCryptHashCert();
/* various combinations of CertOpenStore */
testMemStore();
testCollectionStore();
testRegStore();
testSystemRegStore();
testSystemStore();
testCertOpenSystemStore();
testCertProperties();
testAddSerialized();
testCertSigs();
}