This code enumerates the system certificate store locations, the system certificate stores, and the physical stores associated with each system store. The code demonstrates the creation of callback functions and callback functions that call other call back functions.
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
typedef struct _ENUM_ARG {
BOOL fAll;
BOOL fVerbose;
DWORD dwFlags;
const void *pvStoreLocationPara;
HKEY hKeyBase;
} ENUM_ARG, *PENUM_ARG;
// The following function is defined in a separate file.
void handle_error(char *s);
//*****************************************************************
// Declare functions. Definitions of these functions are at the
// end of this file.
static void DisplayPhysicalStoreInfo(
PCERT_PHYSICAL_STORE_INFO pStoreInfo);
static BOOL GetSystemName(
const void *pvSystemStore,
DWORD dwFlags,
PENUM_ARG pEnumArg,
LPCWSTR *ppwszSystemName );
static BOOL WINAPI EnumPhyCallback(
const void *pvSystemStore,
DWORD dwFlags,
LPCWSTR pwszStoreName,
PCERT_PHYSICAL_STORE_INFO pStoreInfo,
void *pvReserved,
void *pvArg);
static BOOL WINAPI EnumSysCallback(
const void *pvSystemStore,
DWORD dwFlags,
PCERT_SYSTEM_STORE_INFO pStoreInfo,
void *pvReserved,
void *pvArg);
static BOOL WINAPI EnumLocCallback(
LPCWSTR pwszStoreLocation,
DWORD dwFlags,
void *pvReserved,
void *pvArg);
//********************************************************************
// Begin main.
void main(void)
{
//********************************************************************
// Declare variables.
BOOL fVerbose = FALSE;
DWORD dwExpectedError = 0;
DWORD dwLocationID = CERT_SYSTEM_STORE_CURRENT_USER_ID;
DWORD dwFlags = 0;
CERT_PHYSICAL_STORE_INFO PhyStoreInfo;
ENUM_ARG EnumArg;
LPSTR pszStoreParameters = NULL;
LPSTR pszStoreLocationPara = NULL;
LPWSTR pwszStoreParameters = NULL;
LPWSTR pwszSystemName = NULL;
LPWSTR pwszPhysicalName = NULL;
LPWSTR pwszStoreLocationPara = NULL;
void *pvSystemName;
void *pvStoreLocationPara;
DWORD dwNameCnt = 0;
LPCSTR pszTestName;
BOOL fRelocate = FALSE;
HKEY hKeyRelocate = HKEY_CURRENT_USER;
LPSTR pszRelocate = NULL;
HKEY hKeyBase = NULL;
char x;
//********************************************************************
// Initalize variables.
memset(&PhyStoreInfo, 0, sizeof(PhyStoreInfo));
PhyStoreInfo.cbSize = sizeof(PhyStoreInfo);
PhyStoreInfo.pszOpenStoreProvider = sz_CERT_STORE_PROV_SYSTEM_W;
pszTestName = "Enum";
pvSystemName = pwszSystemName;
pvStoreLocationPara = pwszStoreLocationPara;
memset(&EnumArg, 0, sizeof(EnumArg));
EnumArg.dwFlags = dwFlags;
EnumArg.hKeyBase = hKeyBase;
if (pwszStoreLocationPara) {
printf("System Store Location Parameter :: %S\n",
pwszStoreLocationPara);
EnumArg.pvStoreLocationPara = pvStoreLocationPara;
}
else if (fRelocate)
EnumArg.pvStoreLocationPara = pvStoreLocationPara;
EnumArg.fAll = TRUE;
dwFlags &= ~CERT_SYSTEM_STORE_LOCATION_MASK;
dwFlags |= (dwLocationID << CERT_SYSTEM_STORE_LOCATION_SHIFT) &
CERT_SYSTEM_STORE_LOCATION_MASK;
printf("Begin enumeration of Store Locations.\n");
if(!(CertEnumSystemStoreLocation(
dwFlags,
&EnumArg,
EnumLocCallback
)))
handle_error("Enum of locations failed.");
printf("\n\nBegin enumeration of system Stores.\n");
printf("Hit enter to begin.");
scanf("%c",&x);
if(!(CertEnumSystemStore(
dwFlags,
pvStoreLocationPara,
&EnumArg,
EnumSysCallback
)))
handle_error("Enum system stores failed.");
printf("\n\nEnumerate the Physical stores for the My system store.\n");
if(!(CertEnumPhysicalStore(
L"MY",
dwFlags,
&EnumArg,
EnumPhyCallback
)))
handle_error("Enumeration of physical stores failed.");
printf("Physical stores enumerated.\n");
} // end of main.
// begin function definitions.
static void DisplayPhysicalStoreInfo(
PCERT_PHYSICAL_STORE_INFO pStoreInfo
)
{
DWORD dwFlags;
LPCSTR pszStoreProvider = pStoreInfo->pszOpenStoreProvider;
if (0xFFFF >= (DWORD) pszStoreProvider)
printf(" OpenStoreProvider: %d", pszStoreProvider);
else
printf(" OpenStoreProvider: %s", pszStoreProvider);
if (0xFFFF >= (DWORD) pszStoreProvider) {
if (CERT_STORE_PROV_FILENAME_A == pszStoreProvider)
printf(" (FILENAME_A)");
else if (CERT_STORE_PROV_FILENAME_W == pszStoreProvider)
printf(" (FILENAME_W)");
else if (CERT_STORE_PROV_SYSTEM_A == pszStoreProvider)
printf(" (SYSTEM_A)");
else if (CERT_STORE_PROV_SYSTEM_W == pszStoreProvider)
printf(" (SYSTEM_W)");
else if (CERT_STORE_PROV_SYSTEM_REGISTRY_A == pszStoreProvider)
printf(" (SYSTEM_REGISTRY_A)");
else if (CERT_STORE_PROV_SYSTEM_REGISTRY_W == pszStoreProvider)
printf(" (SYSTEM_REGISTRY_W)");
else if (CERT_STORE_PROV_PHYSICAL_W == pszStoreProvider)
printf(" (PHYSICAL_W)");
}
printf("\n");
printf(" OpenEncodingType: 0x%x\n",
pStoreInfo->dwOpenEncodingType);
dwFlags = pStoreInfo->dwFlags;
printf(" Flags: 0x%x", dwFlags);
if (dwFlags)
printf(" ::");
if (dwFlags & CERT_PHYSICAL_STORE_ADD_ENABLE_FLAG)
printf(" ADD_ENABLE");
if (dwFlags & CERT_PHYSICAL_STORE_OPEN_DISABLE_FLAG)
printf(" OPEN_DISABLE");
if (dwFlags & CERT_PHYSICAL_STORE_REMOTE_OPEN_DISABLE_FLAG)
printf(" REMOTE_OPEN_DISABLE_FLAG");
if (dwFlags & CERT_PHYSICAL_STORE_INSERT_COMPUTER_NAME_ENABLE_FLAG)
printf(" INSERT_COMPUTER_NAME_ENABLE_FLAG");
printf("\n");
printf(" Priority: %d\n", pStoreInfo->dwPriority);
}
//********************************************************************
// Function definitions
static BOOL GetSystemName(
const void *pvSystemStore,
DWORD dwFlags,
PENUM_ARG pEnumArg,
LPCWSTR *ppwszSystemName ){
*ppwszSystemName = NULL;
if (pEnumArg->hKeyBase && 0 == (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG))
{
printf("failed => RELOCATE_FLAG not set in callback\n");
return FALSE;
}
else
if (dwFlags & CERT_SYSTEM_STORE_RELOCATE_FLAG) {
PCERT_SYSTEM_STORE_RELOCATE_PARA pRelocatePara;
if (NULL == pEnumArg->hKeyBase) {
handle_error("failed => RELOCATE_FLAG is set in callback");
}
pRelocatePara = (PCERT_SYSTEM_STORE_RELOCATE_PARA) pvSystemStore;
if (pRelocatePara->hKeyBase != pEnumArg->hKeyBase) {
handle_error("failed => wrong hKeyBase passed to callback");
}
*ppwszSystemName = pRelocatePara->pwszSystemStore;
}
else
*ppwszSystemName = (LPCWSTR) pvSystemStore;
return TRUE;
}
//********************************************************************
// Callbacks.
static BOOL WINAPI EnumPhyCallback(
const void *pvSystemStore,
DWORD dwFlags,
LPCWSTR pwszStoreName,
PCERT_PHYSICAL_STORE_INFO pStoreInfo,
void *pvReserved,
void *pvArg ){
PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
LPCWSTR pwszSystemStore;
if (!GetSystemName(pvSystemStore, dwFlags, pEnumArg, &pwszSystemStore))
handle_error("GetSystemName failed.");
printf(" %S", pwszStoreName);
if (pEnumArg->fVerbose &&
(dwFlags & CERT_PHYSICAL_STORE_PREDEFINED_ENUM_FLAG))
printf(" (implicitly created)");
printf("\n");
if (pEnumArg->fVerbose){
printf("verbose is on.\n");
DisplayPhysicalStoreInfo(pStoreInfo);
}
return TRUE;
}
static BOOL WINAPI EnumSysCallback(
const void *pvSystemStore,
DWORD dwFlags,
PCERT_SYSTEM_STORE_INFO pStoreInfo,
void *pvReserved,
void *pvArg
)
{
PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
LPCWSTR pwszSystemStore;
static int i=0;
char x;
if(i++ > 6){
printf("Enum System store : Hit any key to continue.");
scanf("%c",&x);
i=0;
}
if (!GetSystemName(pvSystemStore, dwFlags, pEnumArg, &pwszSystemStore))
handle_error("GetSystemName failed");
printf(" %S\n", pwszSystemStore);
if (pEnumArg->fAll || pEnumArg->fVerbose) {
dwFlags &= CERT_SYSTEM_STORE_MASK;
dwFlags |= pEnumArg->dwFlags & ~CERT_SYSTEM_STORE_MASK;
if (!CertEnumPhysicalStore(
pvSystemStore,
dwFlags,
pEnumArg,
EnumPhyCallback
)) {
DWORD dwErr = GetLastError();
if (!(ERROR_FILE_NOT_FOUND == dwErr ||
ERROR_NOT_SUPPORTED == dwErr))
printf(" CertEnumPhysicalStore");
}
}
return TRUE;
}
static BOOL WINAPI EnumLocCallback(
LPCWSTR pwszStoreLocation,
DWORD dwFlags,
void *pvReserved,
void *pvArg
)
{
PENUM_ARG pEnumArg = (PENUM_ARG) pvArg;
DWORD dwLocationID = (dwFlags & CERT_SYSTEM_STORE_LOCATION_MASK) >>
CERT_SYSTEM_STORE_LOCATION_SHIFT;
static int i=0;
char x;
if(i++ > 6){
printf("Enum store locations : Hit any key to continue.");
scanf("%c",&x);
i=0;
}
printf("====== %S ======\n", pwszStoreLocation);
if (pEnumArg->fAll) {
dwFlags &= CERT_SYSTEM_STORE_MASK;
dwFlags |= pEnumArg->dwFlags & ~CERT_SYSTEM_STORE_LOCATION_MASK;
if (!CertEnumSystemStore(
dwFlags,
(void *) pEnumArg->pvStoreLocationPara,
pEnumArg,
EnumSysCallback
)) {
DWORD dwErr = GetLastError();
if (E_INVALIDARG == dwErr && pEnumArg->pvStoreLocationPara)
// \\ComputerName, ServiceName, or \\ComputerName\Service
// not supported for all store locations
;
else if (!(ERROR_FILE_NOT_FOUND == dwErr ||
ERROR_PROC_NOT_FOUND == dwErr))
printf(" CertEnumSystemStore");
}
}
return TRUE;
}