#include <Windows.h>
#include <wchar.h>
#include <Wincrypt.h>
#pragma comment(lib, "Crypt32.lib")
#define CERT_SCCESSED 0;
#define CERT_FAILED -1;
#define CERT_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING);
int wmain(int argc, WCHAR** argv)
{
if (2 != argc)
{
wprintf(TEXT("For example:self.exe CA \n"));
return CERT_FAILED;
}
if ( 0 != wcscmp(argv[1], TEXT("CA")) &&
0 != wcscmp(argv[1], TEXT("Root")) &&
0 != wcscmp(argv[1], TEXT("Trust"))&&
0 != wcscmp(argv[1], TEXT("My")))
{
return CERT_FAILED;
}
DWORD dwSize = 0L;
WCHAR *pCertName = NULL;
HCERTSTORE hCertStore = NULL;
PCCERT_CONTEXT pCertContext = NULL;
hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_CURRENT_USER,
argv[1]);
if (hCertStore)
{
wprintf(TEXT("The %s store has been opened. \n"), argv[1]);
}
else
{
wprintf(TEXT("The store was not opened.\n"));
return CERT_FAILED;
}
while( TRUE )
{
pCertContext= CertEnumCertificatesInStore(hCertStore,
pCertContext);
if (!pCertContext)
{
break;
}
dwSize = CertNameToStr(pCertContext->dwCertEncodingType,
&(pCertContext->pCertInfo->Subject),
CERT_X500_NAME_STR,
NULL,
0);
if (dwSize > 0L)
{
pCertName = new WCHAR[dwSize+1];
if (!pCertName)
{
wprintf(TEXT("Create memory is failed."));
break;
}
ZeroMemory(pCertName, dwSize+1);
if (CertGetNameString(pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
pCertName,
dwSize))
{
wprintf(TEXT("Cert : %s \n"), pCertName);
}
else
{
wprintf(TEXT("Get cert name failed. \n"));
}
delete [] pCertName;
pCertName = NULL;
}
}
if (CertCloseStore(hCertStore, 0))
{
wprintf(TEXT("Close the %s store!"), argv[1]);
}
else
{
wprintf(TEXT("Failed CertCloseStore\n"));
return CERT_FAILED;
}
return CERT_SCCESSED;
}
遍历证书的源代码
最新推荐文章于 2023-03-15 15:37:02 发布