需要帮助按模板检索 Windows 证书
Need help to retrieve a Windows Certificate by Template?
我需要使用Windows API C++按模板名称检索Windows证书(模板在扩展字段中)。
我的步骤:
-
Open store: CertOpenStore(..) (完成;我可以使用 CertEnumCertificatesInStore(..) 枚举证书,但我只看到它们的"版本 1 字段",而不是"扩展"。模板在扩展中,所以我找不到它。
-
我试图使用CertFindCertificateInStore()找到它,但没有成功。任何人都可以帮助我正确的查找类型和参数或使用其他函数吗?
-
CertFreeCertificateContext(..), CertCloseStore(..) (完成).
我想发布我的代码,希望它对某人有所帮助。
void GetCertificateByTemplate(char *certificateTemplate)
{
HCERTSTORE hCertStore;
PCCERT_CONTEXT pCertContext = NULL;
BYTE *pbDecoded;
DWORD cbDecoded;
_CERT_TEMPLATE_EXT *pbDecodedTemplate = NULL;
// 1). Open Local Machine certificate store
if (hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_LOCAL_MACHINE,
L"My"))
{
fprintf(stderr, "The store has been opened. n");
}
// 2). Enumerate certificates
while (pCertContext = CertEnumCertificatesInStore(
hCertStore,
pCertContext))
{
// 3). Check certificate extended data
for (int i = 0; i < pCertContext->pCertInfo->cExtension; i++)
{
// 4). Decode certificate extended data
if (CryptDecodeObject(
X509_ASN_ENCODING,
pCertContext->pCertInfo->rgExtension[i].pszObjId,
pCertContext->pCertInfo->rgExtension[i].Value.pbData,
pCertContext->pCertInfo->rgExtension[i].Value.cbData,
0,
NULL,
&cbDecoded))
{
; // error !!!
}
if (!(pbDecoded = (BYTE*)malloc(cbDecoded)))
{
; // error !!!
}
if (CryptDecodeObject(
X509_ASN_ENCODING,
pCertContext->pCertInfo->rgExtension[i].pszObjId,
pCertContext->pCertInfo->rgExtension[i].Value.pbData,
pCertContext->pCertInfo->rgExtension[i].Value.cbData,
0,
pbDecoded,
&cbDecoded))
{
pbDecodedTemplate = (_CERT_TEMPLATE_EXT*)pbDecoded;
char* objectId = pbDecodedTemplate->pszObjId;
// todo: check pDecodeTemplate->pszObjId
// 5). Compare the template string with the search one
if (strcmp(pbDecodedTemplate->pszObjId, certificateTemplate) == 0)
{
// todo: return certificate
printf("nCertificate template found: %s n", pbDecodedTemplate->pszObjId);
break;
}
}
}
}
// 6). Free certificate, close store
if (pCertContext)
{
CertFreeCertificateContext(pCertContext);
}
CertCloseStore(hCertStore, 0);
}
相关文章:
- C 检索可执行文件的图标,用于Windows注册表图标路径
- 需要帮助按模板检索 Windows 证书
- 从Windows注册表中保存的工作区检索窗口放置
- 英特尔PIN例行地址检索:Linux与Windows
- C++如何从TCP套接字(windows)中检索ACK
- 如何使用C 在Windows上检索ISP的DNS IP
- 函数从windows XP中的数据包中检索标头目标地址
- 如何正确检索与 Windows 上的扩展关联的打开命令
- 如何使用C 通过Windows上的HTTP从Internet检索文件
- 我可以在windows上的c++上检索微秒或非常精确的毫秒吗
- 检索在Windows上打开的所有窗口
- 从 std::thread::id 结构中检索 Windows 线程标识符
- 检索 Windows 资源管理器的地址栏编辑控件的句柄
- 在C++中检索Windows中的菜单项列表
- SHGetFolderPath 已弃用:检索 Windows 文件夹路径的替代方法是什么
- 我如何从QT中检索Windows中的文件属性元数据
- 从远程计算机(Windows XP)以编程方式检索Windows事件
- 正在使用C++检索Windows中的CPU负载百分比总数
- 正在检索Windows 8产品密钥
- 无法检索windows更新的CVE-ID