简体   繁体   English

需要帮助来按模板检索Windows证书吗?

[英]Need help to retrieve a Windows Certificate by Template?

I need to retrieve a Windows Certificate by Template Name (The Template is in the Extension Fields) using Windows API, C++. 我需要使用Windows API,C ++通过模板名称(模板在扩展字段中)检索Windows证书。

My steps: 我的步骤:

  1. Open store: CertOpenStore(..) (Done; I can enumerate the certificates using CertEnumCertificatesInStore(..), but I see only their "Version 1 fields" and not the "Extensions". The Template is in the Extensions, so I cannot find it.) 公开商店:CertOpenStore(..)(完成;我可以使用CertEnumCertificatesInStore(..)枚举证书,但是我只能看到其“版本1字段”,而没有看到“扩展名”。模板位于扩展名中,因此我无法找到它。)

  2. I tried to find it using the CertFindCertificateInStore(), but did not succeed. 我试图使用CertFindCertificateInStore()找到它,但没有成功。 Can anyone help me with the correct find type and parameters or use another function? 谁能为我提供正确的查找类型和参数或使用其他功能?

  3. CertFreeCertificateContext(..), CertCloseStore(..) (Done). CertFreeCertificateContext(..),CertCloseStore(..)(完成)。

I would like to post my code, hope it helps someone. 我想发布我的代码,希望对您有所帮助。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM