FreeRDP
Loading...
Searching...
No Matches
TestCryptoCertEnumCertificatesInStore.c
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
15int TestCryptoCertEnumCertificatesInStore(int argc, char* argv[])
16{
17 int index = 0;
18 DWORD status = 0;
19 LPTSTR pszNameString = NULL;
20 HCERTSTORE hCertStore = NULL;
21 PCCERT_CONTEXT pCertContext = NULL;
22
23 WINPR_UNUSED(argc);
24 WINPR_UNUSED(argv);
25
37 hCertStore = CertOpenSystemStore((HCRYPTPROV_LEGACY)NULL, _T("MY"));
38 // hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, (HCRYPTPROV_LEGACY) NULL,
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 = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
52 if (status == 0)
53 return -1;
54
55 pszNameString = (LPTSTR)calloc(status, sizeof(TCHAR));
56 if (!pszNameString)
57 {
58 printf("Unable to allocate memory\n");
59 return -1;
60 }
61
62 status = CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
63 pszNameString, status);
64 if (status == 0)
65 {
66 free(pszNameString);
67 return -1;
68 }
69
70 _tprintf(_T("Certificate #%d: %s\n"), index++, pszNameString);
71
72 free(pszNameString);
73
74#ifdef WITH_CRYPTUI
75 CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, pCertContext, NULL, NULL, 0, NULL);
76#endif
77 }
78
79 if (!CertCloseStore(hCertStore, 0))
80 {
81 printf("Failed to close system store\n");
82 return -1;
83 }
84
85 return 0;
86}