The first function code is for a subroutine that enumerates all the certificates in a store until a particular certificate is found with an issuer name that matches a string passed as a parameter. This code demonstrates:
// GetSubjectCert() - A function to enumerate certificates
// in the store and get a handle to one of them based on issuer
// name.
#define _WIN32_WINNT 0x0400
#include <stdio.h>
#include <windows.h>
#include "wincrypt.h"
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
PCCERT_CONTEXT GetSubjectCert(
HCERTSTORE hCertStore, // the store to search
LPCTSTR pszSearchName) // the string to match
{
//********************************************************************
// Declare variables.
PCCERT_CONTEXT pCertContext = NULL;
BOOL fFound = FALSE;
BOOL fMore = TRUE;
DWORD dwSize = 0;
LPSTR pszSubjectName;
LPSTR pszNameFound;
CERT_NAME_BLOB NameBlob;
//********************************************************************
// Enumerate the certificates in the store until a match is found.
while(fMore && !fFound)
{
if(!(pCertContext = CertEnumCertificatesInStore(
hCertStore,
pCertContext)))
{
// The end of the store has been reached. Get out of the loop.
fMore=FALSE;
break;
}
NameBlob = pCertContext->pCertInfo->Issuer;
// Call CertNameToStr to get dwSize, the length of the
// CertNameString
dwSize = CertNameToStr(
MY_ENCODING_TYPE, // Encoding type
&NameBlob, // the subject from the pCertInfo
CERT_SIMPLE_NAME_STR, // Type of string
NULL, // Place to return string.
// In the first pass, NULL since
// space for the name string has not
// yet been allocated.
0); // Size of the name string
if (dwSize<2) // If the string length returned is
// less than 2, the function could not
// determine a valid name string length.
// The function failed.
{
// The call to the function failed. Get out of the loop.
printf("Error First pass of getting Name String1");
fMore = FALSE;
break;
}
// Allocate memory for the subject name string.
if(!(pszSubjectName = (LPSTR)malloc(dwSize)))
{
// Memory allocation failed. Get out of the loop.
printf("Error Allocating Memory");
fMore = FALSE;
break;
}
// Make second call to CertNameToStr to get the string.
dwSize = CertNameToStr(
MY_ENCODING-TYPE, // Encoding type
&NameBlob, // CERT_NAME_BLOB
CERT_SIMPLE_NAME_STR, // Type
pszSubjectName, // Place to return string
dwSize); // Size of string (chars)
if(dwSize<2)
{
// If the length returned is less than 2,
// the function failed. Get out of the loop.
printf("Error in second pass.\n");
fMore = FALSE;
break;
}
pszNameFound = strstr(pszSubjectName,
pszSearchName);
if(pszNameFound)
{
// The string searched for was matched. Get out of the loop.
printf("Target Certificate %s Found \n", pszNameFound);
fFound = TRUE;
break;
} // end while
free(pszSubjectName);
if(!fFound)
{
printf("Target Cert Not Found\n");
CertFreeCertificateContext(pCertContext);
return NULL;
}
else
return (pCertContext); // return the whole context that included
// the subject name string.
} // end of function
The following function is used to print an error message and exit a program. It is used the example code for several CryptoAPI functions.
void handle_error(char *s){
printf("An error occured in running the program.\n");
printf("%s\n\n",s);
printf("Program terminating.\n");
exit(1);
}