2012年7月19日 星期四

Certificate Store Operations

Example to get certificate from system.


#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(char *s);

int _tmain(int argc, _TCHAR* argv[])
{
    //--------------------------------------------------------------------
    // Copyright (C) Microsoft.  All rights reserved.
    // Declare and initialize variables.

    HCERTSTORE  hSystemStore;              // System store handle
    PCCERT_CONTEXT  pDesiredCert = NULL;   // Set to NULL for the first call to CertFindCertificateInStore
    PCCERT_CONTEXT  pCertContext;


    //-------------------------------------------------------------------
    // Open the My system store using CertOpenStore.

    if(hSystemStore = CertOpenStore(
         CERT_STORE_PROV_SYSTEM,            // System store will be a virtual store
         0,                                 // Encoding type not needed with this PROV
         NULL,                              // Accept the default HCRYPTPROV
         CERT_SYSTEM_STORE_CURRENT_USER,    // Set the system store location in the registry
         L"MY"))                            // Could have used other predefined  system stores including Trust, CA, or Root
    {
       printf("Opened the MY system store. \n");
    }
    else
    {
       MyHandleError( "Could not open the MY system store.");
    }



    //-------------------------------------------------------------------
    // Find the certificates in the system store. 
    while(pCertContextEnum=CertEnumCertificatesInStore(
          hSystemStore,
          pCertContextEnum)) // on the first call to the function, this parameter is NULL  on all subsequent                calls,  this parameter is the last pointer returned by the function
    {
        if(CertGetNameString(
           pCertContextEnum,
           CERT_NAME_SIMPLE_DISPLAY_TYPE,
           0,
           NULL,
           pszNameString,
           128))
        {
            printf("\nCertificate for %s \n",pszNameString);
        }
        else
           fprintf(stderr,"CertGetName failed. \n");
    } // End of while.



    //-------------------------------------------------------------------
    // Get a certificate that has the string "ninna.tw@gmail.com" in its subject. 

    if(pDesiredCert=CertFindCertificateInStore(
          hSystemStore,
          MY_ENCODING_TYPE,             // Use X509_ASN_ENCODING
          0,                            // No dwFlags needed 
          CERT_FIND_SUBJECT_STR,        // Find a certificate with a subject that matches the string in the next parameter
          L"ninna.tw@gmail.com",     // The Unicode string to be found in a certificate's subject
          NULL))                        // NULL for the first call to the function In all subsequent calls, it is the last pointer returned by the function
    {
      printf("The desired certificate was found. \n");
    }
    else
    {
       MyHandleError("Could not find the desired certificate.");
    }
    //-------------------------------------------------------------------
    // pDesiredCert is a pointer to a certificate with a subject that 
    // includes the string "ninna.tw@ gmail.com ", the string is 
    // passed as parameter #5 to the function.


    //-------------------------------------------------------------------
    // Close the stores.

    if(hSystemStore)
        CertCloseStore(
            hSystemStore,
            CERT_CLOSE_STORE_CHECK_FLAG);

    printf("All of the stores are closed. \n");

return 0;
}

//-------------------------------------------------------------------
// This example uses the function MyHandleError, a simple error
// handling function, to print an error message and exit 
// the program. 
// For most applications, replace this function with one 
// that does more extensive error reporting.

void MyHandleError(char *s)
{
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // end MyHandleError




You should add dependency for using static functions.
Right click on project -> Property -> Linker -> Input ->Additional Dependencies -> add crypt32.lib



Reference:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa382037(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa382363(v=vs.85).aspx