FreeRDP/winpr/libwinpr/crypto/test/TestCryptoCertEnumCertificatesInStore.c

87 lines
1.8 KiB
C
Raw Normal View History

2013-01-04 21:16:55 +04:00
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/crypto.h>
#ifdef _WIN32
//#define WITH_CRYPTUI 1
#endif
#ifdef WITH_CRYPTUI
#include <cryptuiapi.h>
#endif
int TestCryptoCertEnumCertificatesInStore(int argc, char* argv[])
2013-01-04 21:16:55 +04:00
{
int index;
DWORD status;
2019-11-06 17:24:51 +03:00
LPTSTR pszNameString;
2013-01-04 21:16:55 +04:00
HCERTSTORE hCertStore = NULL;
PCCERT_CONTEXT pCertContext = NULL;
2021-07-29 11:18:52 +03:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
2013-01-04 21:16:55 +04:00
/**
* System Store Locations:
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa388136/
*/
/**
* Requires elevated rights:
2019-11-06 17:24:51 +03:00
* hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, (HCRYPTPROV_LEGACY) NULL,
* CERT_SYSTEM_STORE_LOCAL_MACHINE, _T("Remote Desktop"));
2013-01-04 21:16:55 +04:00
*/
2019-11-06 17:24:51 +03:00
hCertStore = CertOpenSystemStore((HCRYPTPROV_LEGACY)NULL, _T("MY"));
// hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, (HCRYPTPROV_LEGACY) NULL,
// CERT_SYSTEM_STORE_CURRENT_USER, _T("MY"));
2013-01-04 21:16:55 +04:00
if (!hCertStore)
{
printf("Failed to open system store\n");
return -1;
}
index = 0;
while ((pCertContext = CertEnumCertificatesInStore(hCertStore, pCertContext)))
2013-01-04 21:16:55 +04:00
{
status = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
if (status == 0)
2017-03-28 17:18:00 +03:00
return -1;
2013-01-04 21:16:55 +04:00
2019-11-06 17:24:51 +03:00
pszNameString = (LPTSTR)calloc(status, sizeof(TCHAR));
if (!pszNameString)
{
printf("Unable to allocate memory\n");
return -1;
}
2019-11-06 17:24:51 +03:00
status = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
pszNameString, status);
if (status == 0)
{
2019-11-06 17:24:51 +03:00
free(pszNameString);
2017-03-28 17:18:00 +03:00
return -1;
}
2013-01-04 21:16:55 +04:00
_tprintf(_T("Certificate #%d: %s\n"), index++, pszNameString);
free(pszNameString);
2013-01-04 21:16:55 +04:00
#ifdef WITH_CRYPTUI
CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, pCertContext, NULL, NULL, 0, NULL);
#endif
}
if (!CertCloseStore(hCertStore, 0))
{
printf("Failed to close system store\n");
return -1;
}
return 0;
}