| 1 |
|
| 2 |
|
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/tchar.h> |
| 5 |
#include <winpr/crypto.h> |
| 6 |
|
| 7 |
#ifdef _WIN32 |
| 8 |
//#define WITH_CRYPTUI 1 |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifdef WITH_CRYPTUI |
| 12 |
#include <cryptuiapi.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
int TestCryptoCertEnumCertificatesInStore(int argc, char* argv[]) |
| 16 |
{ |
| 17 |
int index = 0; |
| 18 |
DWORD status = 0; |
| 19 |
LPTSTR pszNameString = nullptr; |
| 20 |
HCERTSTORE hCertStore = nullptr; |
| 21 |
PCCERT_CONTEXT pCertContext = nullptr; |
| 22 |
|
| 23 |
WINPR_UNUSED(argc); |
| 24 |
WINPR_UNUSED(argv); |
| 25 |
|
| 26 |
/** |
| 27 |
* System Store Locations: |
| 28 |
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa388136/ |
| 29 |
*/ |
| 30 |
|
| 31 |
/** |
| 32 |
* Requires elevated rights: |
| 33 |
* hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, |
| 34 |
* CERT_SYSTEM_STORE_LOCAL_MACHINE, _T("Remote Desktop")); |
| 35 |
*/ |
| 36 |
|
| 37 |
hCertStore = CertOpenSystemStore(0, _T("MY")); |
| 38 |
// hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, |
| 39 |
// CERT_SYSTEM_STORE_CURRENT_USER, _T("MY")); |
| 40 |
|
| 41 |
if (!hCertStore) |
| 42 |
{ |
| 43 |
printf("Failed to open system store\n"); |
| 44 |
return -1; |
| 45 |
} |
| 46 |
|
| 47 |
index = 0; |
| 48 |
|
| 49 |
while ((pCertContext = CertEnumCertificatesInStore(hCertStore, pCertContext))) |
| 50 |
{ |
| 51 |
status = |
| 52 |
CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, nullptr, 0); |
| 53 |
if (status == 0) |
| 54 |
return -1; |
| 55 |
|
| 56 |
pszNameString = (LPTSTR)calloc(status, sizeof(TCHAR)); |
| 57 |
if (!pszNameString) |
| 58 |
{ |
| 59 |
printf("Unable to allocate memory\n"); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
status = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, nullptr, |
| 64 |
pszNameString, status); |
| 65 |
if (status == 0) |
| 66 |
{ |
| 67 |
free(pszNameString); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
_tprintf(_T("Certificate #%d: %s\n"), index++, pszNameString); |
| 72 |
|
| 73 |
free(pszNameString); |
| 74 |
|
| 75 |
#ifdef WITH_CRYPTUI |
| 76 |
CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, pCertContext, nullptr, nullptr, 0, |
| 77 |
nullptr); |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
if (!CertCloseStore(hCertStore, 0)) |
| 82 |
{ |
| 83 |
printf("Failed to close system store\n"); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |