-
Notifications
You must be signed in to change notification settings - Fork 31
/
CUSR.c
105 lines (90 loc) · 2.81 KB
/
CUSR.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "VSTriage.h"
PWSTR pwszCusrBuf = NULL;
size_t stCusrBufSize;
HCERTSTORE CUSROpenRootStore(DWORD dwStore)
{
HCERTSTORE hCertStore;
hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
(HCRYPTPROV_LEGACY)NULL,
dwStore | (DWORD)CERT_STORE_READONLY_FLAG,
L"Root"
);
return hCertStore;
}
BOOL CUSRGetCertThumbprint(PCCERT_CONTEXT pccertContext, PWSTR pwszThumbprint)
{
if ((NULL == pccertContext) || (NULL == pwszThumbprint))
{
return FALSE;
}
BYTE certThumbPrintArr[CERT_SHA1_HASH_LENGTH] = {0};
DWORD certThumbPrintSize = ARRAYSIZE(certThumbPrintArr);
BOOL bRes;
WCHAR pwszT1[2 * CERT_SHA1_HASH_LENGTH + 1] = {0};
DWORD i;
bRes = CertGetCertificateContextProperty(
pccertContext,
CERT_SHA1_HASH_PROP_ID,
certThumbPrintArr,
&certThumbPrintSize);
if (!bRes)
{
StringCchCopy(pwszThumbprint, ARRAYSIZE(certThumbPrintArr), L"\0");
return FALSE;
}
for (i = 0; i < ARRAYSIZE(certThumbPrintArr); i++)
{
WCHAR wcT[4] = {0};
StringCchPrintfW(wcT, ARRAYSIZE(wcT), L"%02x", certThumbPrintArr[i]);
StringCchCatW(pwszT1, ARRAYSIZE(pwszT1), wcT);
}
StringCchCopyW(pwszThumbprint, (ARRAYSIZE(pwszT1) + 1) * sizeof(WCHAR), pwszT1);
return TRUE;
}
BOOL CUSRScan(DWORD dwStore)
{
HCERTSTORE hCertStore;
PCCERT_CONTEXT pccContext = NULL;
DWORD dwTypePara = 0;
DWORD i = 0;
hCertStore = CUSROpenRootStore(dwStore);
if (!hCertStore)
{
return FALSE;
}
while (NULL != (pccContext = CertEnumCertificatesInStore(hCertStore, pccContext)))
{
WCHAR pwszNameString2[SIZE_1KB];
WCHAR pwszNameString[SIZE_1KB];
CUSRGetCertThumbprint(pccContext, pwszNameString);
StringCchPrintfW(pwszNameString2, ARRAYSIZE(pwszNameString2), L"\t%03d: %s\t", ++i, pwszNameString);
StringCchCatW(pwszCusrBuf, stCusrBufSize / sizeof(WCHAR), pwszNameString2);
CertGetNameStringW(pccContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, &dwTypePara, pwszNameString, SIZE_1KB);
StringCchPrintfW(pwszNameString2, ARRAYSIZE(pwszNameString2), L"%s\r\n", pwszNameString);
StringCchCatW(pwszCusrBuf, stCusrBufSize / sizeof(WCHAR), pwszNameString2);
}
CertCloseStore(hCertStore, CERT_CLOSE_STORE_FORCE_FLAG);
return TRUE;
}
BOOL CUSRMain(void)
{
BOOL bRes1;
BOOL bRes2;
StringCchCatW(pwszCusrBuf, stCusrBufSize / sizeof(WCHAR), L"Machine Root\r\n");
bRes1 = CUSRScan(CERT_SYSTEM_STORE_LOCAL_MACHINE);
StringCchCatW(pwszCusrBuf, stCusrBufSize / sizeof(WCHAR), L"Current User Root\r\n");
bRes2 = CUSRScan(CERT_SYSTEM_STORE_CURRENT_USER);
return (bRes1 && bRes2);
}
PWSTR CUSR_Output(void)
{
wprintf(L"Listing Root Certs\r\n");
stCusrBufSize = SIZE_16MB;
ALLOCORCRASH(pwszCusrBuf, stCusrBufSize);
AddCheckHeader(pwszCusrBuf, stCusrBufSize, L"Root Certs", FALSE);
CUSRMain();
ShrinkWcharBuffer(&pwszCusrBuf);
return pwszCusrBuf;
}