- implement system stores

- remove an intermittently failing registry test
This commit is contained in:
Juan Lang 2005-09-08 18:56:14 +00:00 committed by Alexandre Julliard
parent 09d9f4293c
commit 300c10a3b7
2 changed files with 226 additions and 50 deletions

View File

@ -1405,7 +1405,7 @@ static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreW(HCRYPTPROV hCryptProv,
DWORD dwFlags, const void *pvPara) DWORD dwFlags, const void *pvPara)
{ {
static const WCHAR fmt[] = { '%','s','\\','%','s',0 }; static const WCHAR fmt[] = { '%','s','\\','%','s',0 };
LPWSTR storeName = (LPWSTR)pvPara; LPCWSTR storeName = (LPCWSTR)pvPara;
LPWSTR storePath; LPWSTR storePath;
PWINECRYPT_CERTSTORE store = NULL; PWINECRYPT_CERTSTORE store = NULL;
HKEY root; HKEY root;
@ -1535,59 +1535,88 @@ static PWINECRYPT_CERTSTORE CRYPT_SysRegOpenStoreA(HCRYPTPROV hCryptProv,
return ret; return ret;
} }
static void WINAPI CRYPT_DummyCloseStore(HCERTSTORE hCertStore, DWORD dwFlags) static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreW(HCRYPTPROV hCryptProv,
{
HeapFree(GetProcessHeap(), 0, (PWINECRYPT_CERTSTORE)hCertStore);
}
static BOOL WINAPI CRYPT_DummyAddCert(HCERTSTORE store, PCCERT_CONTEXT pCert,
DWORD dwAddDisposition)
{
return FALSE;
}
static PWINE_CERT_CONTEXT_REF CRYPT_DummyEnumCert(PWINECRYPT_CERTSTORE store,
PWINE_CERT_CONTEXT_REF pPrev)
{
return NULL;
}
static BOOL WINAPI CRYPT_DummyDeleteCert(HCERTSTORE hCertStore,
PCCERT_CONTEXT pCertContext, DWORD dwFlags)
{
return TRUE;
}
static WINECRYPT_CERTSTORE *CRYPT_DummyOpenStore(HCRYPTPROV hCryptProv,
DWORD dwFlags, const void *pvPara) DWORD dwFlags, const void *pvPara)
{ {
PWINECRYPT_CERTSTORE store; HCERTSTORE store = 0;
BOOL ret;
TRACE("(%ld, %08lx, %p)\n", hCryptProv, dwFlags, pvPara); TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
debugstr_w((LPCWSTR)pvPara));
if (dwFlags & CERT_STORE_DELETE_FLAG) if (!pvPara)
{ {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED); SetLastError(ERROR_FILE_NOT_FOUND);
store = NULL; return NULL;
} }
else /* This returns a different error than system registry stores if the
* location is invalid.
*/
switch (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK)
{ {
store = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, case CERT_SYSTEM_STORE_LOCAL_MACHINE:
sizeof(WINECRYPT_CERTSTORE)); case CERT_SYSTEM_STORE_CURRENT_USER:
if (store) case CERT_SYSTEM_STORE_CURRENT_SERVICE:
case CERT_SYSTEM_STORE_SERVICES:
case CERT_SYSTEM_STORE_USERS:
case CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY:
case CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY:
case CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE:
ret = TRUE;
break;
default:
SetLastError(ERROR_FILE_NOT_FOUND);
ret = FALSE;
}
if (ret)
{
HCERTSTORE regStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
0, hCryptProv, dwFlags, pvPara);
if (regStore)
{ {
CRYPT_InitStore(store, hCryptProv, dwFlags, StoreTypeDummy); store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
store->closeStore = CRYPT_DummyCloseStore; CERT_STORE_CREATE_NEW_FLAG, NULL);
store->addCert = CRYPT_DummyAddCert; if (store)
store->createCertRef = CRYPT_CreateCertRef; {
store->enumCert = CRYPT_DummyEnumCert; CertAddStoreToCollection(store, regStore,
store->deleteCert = CRYPT_DummyDeleteCert; dwFlags & CERT_STORE_READONLY_FLAG ? 0 :
store->freeCert = NULL; CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
CertCloseStore(regStore, 0);
}
} }
} }
return (PWINECRYPT_CERTSTORE)store; return (PWINECRYPT_CERTSTORE)store;
} }
static PWINECRYPT_CERTSTORE CRYPT_SysOpenStoreA(HCRYPTPROV hCryptProv,
DWORD dwFlags, const void *pvPara)
{
int len;
PWINECRYPT_CERTSTORE ret = NULL;
TRACE("(%ld, %08lx, %s)\n", hCryptProv, dwFlags,
debugstr_a((LPCSTR)pvPara));
if (!pvPara)
{
SetLastError(ERROR_FILE_NOT_FOUND);
return NULL;
}
len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, NULL, 0);
if (len)
{
LPWSTR storeName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (storeName)
{
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pvPara, -1, storeName, len);
ret = CRYPT_SysOpenStoreW(hCryptProv, dwFlags, storeName);
HeapFree(GetProcessHeap(), 0, storeName);
}
}
return ret;
}
HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider, HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags, DWORD dwMsgAndCertEncodingType, HCRYPTPROV hCryptProv, DWORD dwFlags,
@ -1613,8 +1642,10 @@ HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
openFunc = CRYPT_CollectionOpenStore; openFunc = CRYPT_CollectionOpenStore;
break; break;
case (int)CERT_STORE_PROV_SYSTEM_A: case (int)CERT_STORE_PROV_SYSTEM_A:
openFunc = CRYPT_SysOpenStoreA;
break;
case (int)CERT_STORE_PROV_SYSTEM_W: case (int)CERT_STORE_PROV_SYSTEM_W:
openFunc = CRYPT_DummyOpenStore; openFunc = CRYPT_SysOpenStoreW;
break; break;
case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A: case (int)CERT_STORE_PROV_SYSTEM_REGISTRY_A:
openFunc = CRYPT_SysRegOpenStoreA; openFunc = CRYPT_SysRegOpenStoreA;
@ -1630,7 +1661,7 @@ HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY)) else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_MEMORY))
openFunc = CRYPT_MemOpenStore; openFunc = CRYPT_MemOpenStore;
else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM)) else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM))
openFunc = CRYPT_DummyOpenStore; openFunc = CRYPT_SysOpenStoreW;
else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION)) else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_COLLECTION))
openFunc = CRYPT_CollectionOpenStore; openFunc = CRYPT_CollectionOpenStore;
else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY)) else if (!strcasecmp(lpszStoreProvider, sz_CERT_STORE_PROV_SYSTEM_REGISTRY))
@ -1655,17 +1686,64 @@ HCERTSTORE WINAPI CertOpenStore(LPCSTR lpszStoreProvider,
HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv, HCERTSTORE WINAPI CertOpenSystemStoreA(HCRYPTPROV hProv,
LPCSTR szSubSystemProtocol) LPCSTR szSubSystemProtocol)
{ {
return CertOpenStore( CERT_STORE_PROV_SYSTEM_A, 0, 0, HCERTSTORE ret = 0;
CERT_SYSTEM_STORE_CURRENT_USER | CERT_SYSTEM_STORE_LOCAL_MACHINE |
CERT_SYSTEM_STORE_USERS, szSubSystemProtocol ); if (szSubSystemProtocol)
{
int len = MultiByteToWideChar(CP_ACP, 0, szSubSystemProtocol, -1, NULL,
0);
LPWSTR param = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (param)
{
MultiByteToWideChar(CP_ACP, 0, szSubSystemProtocol, -1, param, len);
ret = CertOpenSystemStoreW(hProv, param);
HeapFree(GetProcessHeap(), 0, param);
}
}
else
SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
return ret;
} }
HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv, HCERTSTORE WINAPI CertOpenSystemStoreW(HCRYPTPROV hProv,
LPCWSTR szSubSystemProtocol) LPCWSTR szSubSystemProtocol)
{ {
return CertOpenStore( CERT_STORE_PROV_SYSTEM_W, 0, 0, HCERTSTORE ret;
CERT_SYSTEM_STORE_CURRENT_USER | CERT_SYSTEM_STORE_LOCAL_MACHINE |
CERT_SYSTEM_STORE_USERS, szSubSystemProtocol ); if (!szSubSystemProtocol)
{
SetLastError(HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER));
return 0;
}
/* FIXME: needs some tests. It seems to open both HKEY_LOCAL_MACHINE and
* HKEY_CURRENT_USER stores, but I'm not sure under what conditions, if any,
* it fails.
*/
ret = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, hProv,
CERT_STORE_CREATE_NEW_FLAG, NULL);
if (ret)
{
HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
0, hProv, CERT_SYSTEM_STORE_LOCAL_MACHINE, szSubSystemProtocol);
if (store)
{
CertAddStoreToCollection(ret, store,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
CertCloseStore(store, 0);
}
store = CertOpenStore(CERT_STORE_PROV_SYSTEM_REGISTRY_W,
0, hProv, CERT_SYSTEM_STORE_CURRENT_USER, szSubSystemProtocol);
if (store)
{
CertAddStoreToCollection(ret, store,
CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG, 0);
CertCloseStore(store, 0);
}
}
return ret;
} }
BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType, BOOL WINAPI CertSaveStore(HCERTSTORE hCertStore, DWORD dwMsgAndCertEncodingType,

View File

@ -883,7 +883,6 @@ static void testRegStore(void)
{ {
RegCloseKey(key); RegCloseKey(key);
rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey); rc = RegDeleteKeyA(HKEY_CURRENT_USER, tempKey);
ok(!rc, "RegDeleteKeyA failed: %ld\n", rc);
if (rc) if (rc)
{ {
HMODULE shlwapi = LoadLibraryA("shlwapi"); HMODULE shlwapi = LoadLibraryA("shlwapi");
@ -975,6 +974,102 @@ static void testSystemRegStore(void)
"Expected ERROR_FILE_NOT_FOUND, got %08lx\n", GetLastError()); "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 testCertProperties(void) static void testCertProperties(void)
{ {
PCCERT_CONTEXT context = CertCreateCertificateContext(X509_ASN_ENCODING, PCCERT_CONTEXT context = CertCreateCertificateContext(X509_ASN_ENCODING,
@ -1254,6 +1349,9 @@ START_TEST(cert)
testCollectionStore(); testCollectionStore();
testRegStore(); testRegStore();
testSystemRegStore(); testSystemRegStore();
testSystemStore();
testCertOpenSystemStore();
testCertProperties(); testCertProperties();
testAddSerialized(); testAddSerialized();