The CertGetNameString function gets the subject or issuer name from the certificate and converts it to a null terminated character string.
#include <wincrypt.h>
DWORD WINAPI CertGetNameString(
PCCERT_CONTEXT pCertContext, // in
DWORD dwType, // in
DWORD dwFlags, // in
void *pvTypePara, // in
LPTSTR pszNameString, // out/optional
DWORD cchNameString // in
);
szOID_COMMON_NAME ("2.5.4.3")
szOID_ORGANIZATIONAL_UNIT_NAME ("2.5.4.11")
szOID_ORGANIZATION_NAME ("2.5.4.10")
szOID_RSA_emailAddr ("1.2.840.113549.1.9.1")
pvTypePara not used and should be set to NULL.
If none of the above attributes is found, then searches the Subject Alternative Name extension for a rfc822Name choice. If still no match, then, returns the first attribute. Note, like CERT_NAME_ATTR_TYPE, searches the RDNs in reverse order.
Returns the number of characters converted, including the terminating zero character. If pszNameString is NULL or cchNameString is zero, returns the required size of the destination string (including the terminating null character). If the specified name type isn't found, returns an empty string with a returned character count of 1.
// EXAMPLE CODE FOR USING CertGetNameString().
// Assume that a pointer to the certificate (pCertContext) is already
// known.
// Set up the variables.
PCCERT_CONTEXT pCertContext; // Pointer to the specified certificate
// Initialized elsewhere
DWORD dwType = CERT_NAME_EMAIL_TYPE;
// Type of CERT_NAME
DWORD dwFlags = CERT_NAME_ISSUER_FLAG;
// Flag is set. Get the issuer's name
void *pvTypePara; // Pointer to the a DWORD containing the
// dwStrType, or Object Identifier
LPTSTR pszNameString; // Pointer to the name string
DWORD cchNameString; // # of characters converted-
// including the terminating null
DWORD dwResult; // Returns # of characters (including
// the terminating NULL).
// Function call to CertGetNameString to get the required size
// of the destination string
dwResult= CertGetNameString(
pCertContext, // in
CERT_NAME_EMAIL_TYPE,
// in - dwType
CERT_NAME_ISSUER_FLAG,
// in - dwFlags to get the issuer's
// name in lieu of the subject's name
NULL, // in - pvTypePara is set to NULL as
// this is CERT_NAME_EMAIL_TYPE
NULL, // out, optional - pszNameString
// set to NULL to get the required
// size of the destination string
0); // in - cchNameString set to 0 to get
// get the required size of the
// destination string
cout<< "CertGetNameString returned a length of;"<< endl
<< "dwResult = "<< dwResult<< endl<< endl;
Windows NT: Requires version 4.0 SP3 or later. Available also in IE 3.02 and later.
Windows: Requires Windows 98 (or Windows 95 with IE 3.02 or later).
Windows CE: Unsupported.
Header: Declared in wincrypt.h.
Import Library: Use crypt32.lib.
Unicode: Defined as Unicode and ANSI prototypes.