2

Est-ce que quelqu'un sait comment je ferais l'équivalent du code C# ci-dessous en utilisant non géré C++, c'est-à-dire interroger un certificat du magasin de certificats X509 par thumbprint?Accédez au magasin de certificats X509 avec C++ non géré

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

Merci à l'avance

Dave

Répondre

4

Pour accomplir ce que vous voulez, vous aurez à regarder dans la bibliothèque CryptAPI Win32. Ce ne sera pas aussi facile que .NET. Regardez dans CertOpenStore et CertFindCertificateInStore.

Vous devrez ouvrir un magasin de certificats et le transmettre à CertFindCertificateStore, en créant une structure qui contiendra les critères que vous souhaitez utiliser pour trouver votre certificat. Vous pouvez utiliser un numéro de série, signature, etc.

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 
} 

Vous devrez #include <Wincrypt.h> et #include <windows.h>

Questions connexes