Win32 枚举IE浏览器个人证书

#include <windows.h>
#include <Wincrypt.h>
#pragma comment(lib,"Crypt32.lib")

#include <iostream>
#include <string>

// 转化为string
std::string to_string(std::wstring str)
{
	std::string result;

	int len = WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
	char* buffer = new char[len + 1];
	if (buffer == nullptr) return result;

	WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';

	result = buffer;
	delete[] buffer;
	return result;
}

// https://www.zhihu.com/question/26342136
// https://docs.microsoft.com/en-us/windows/win32/seccrypto/example-c-program-certificate-store-operations
void test()
{
	// 打开IE个人证书
	HCERTSTORE cert_store = CertOpenStore(
		CERT_STORE_PROV_SYSTEM, // 使用来自指定系统存储的证书、CRL 和 CTL 初始化存储
		0,
		NULL,
		CERT_SYSTEM_STORE_CURRENT_USER, // 系统当前用户?
		L"MY");
	if (cert_store == NULL) return;

	wchar_t buffer[0x100]{ 0 };

	// 开始遍历证书
	PCCERT_CONTEXT cert_context = NULL;
	while (cert_context = CertEnumCertificatesInStore(cert_store, cert_context))
	{
		memset(buffer, 0, sizeof(wchar_t) * 0x100);

		// 颁发给
		// CERT_NAME_ATTR_TYPE
		// CERT_NAME_SIMPLE_DISPLAY_TYPE
		// CERT_NAME_DNS_TYPE
		if (CertGetNameStringW(cert_context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, buffer, 0x100))
		{
			std::printf("[+] %s \n", to_string(buffer).c_str());
		}
	}

	// 关闭证书句柄
	CertCloseStore(cert_store, 0);
}

int main(int argc, char* argv[])
{
	test();
	return 0;
}
上一篇:160824、ionic添加地图站点


下一篇:13-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-Android以SSL单向认证方式连接MQTT服务器(验证服务器证书)