使用不受管理的C ++访问X509证书存储

Dav*_*ave 2 c++ unmanaged certificate x509certificate2 x509certificate

有谁知道我将如何使用非托管 C ++ 等效于以下C#代码,即通过指纹从X509证书存储区中查询证书?

        var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

        store.Open(OpenFlags.ReadOnly);

        var allCerts = store.Certificates;

        foreach (var certificate in from X509Certificate2 certificate in allCerts
                                    where certificate.Thumbprint != null
                                       && certificate.Thumbprint.Equals(thumbprint, StringComparison.OrdinalIgnoreCase)
                                    select certificate)
        {
            return certificate;
        }
Run Code Online (Sandbox Code Playgroud)

提前致谢

戴夫

Zac*_*chS 5

为了完成所需的操作,您必须查看Win32 CryptAPI库。它不会像.NET那样容易。查看CertOpenStoreCertFindCertificateInStore

您将需要打开证书存储并将其传递到CertFindCertificateStore,创建一个结构来保存要用来查找证书的任何条件。您可以使用序列号,签名等。

    HCERTSTORE hSysStore = NULL;
    PCCERT_CONTEXT  pDesiredCert = NULL;
if(hSysStore = CertOpenStore(
   CERT_STORE_PROV_SYSTEM,          // The store provider type
   0,                               // The encoding type is
                                    // not needed
   NULL,                            // Use the default HCRYPTPROV
   CERT_SYSTEM_STORE_CURRENT_USER,  // Set the store location in a
                                    // registry location
   L"MY"                            // The store name as a Unicode 
                                    // string
   ))
{
    //We have our store, let's do stuff with it
    if (pDesiredCert = CertFindCertificateInStore(.....) {  ..... }
}
else
{
    //Error stuff
}
Run Code Online (Sandbox Code Playgroud)

您将需要#include <Wincrypt.h>#include <windows.h>