The CertStrToName function converts a NULL-terminated X500 string to an encoded certificate name. The input string is expected to be formatted the same as the output from CertNameToStr.
#include <wincrypt.h>
BOOL WINAPI CertStrToName(
DWORD dwCertEncodingType, // in
LPCTSTR pszX500, // in
DWORD dwStrType, // in
void* pvReserved, // in, optional
BYTE* pbEncoded, // out
DWORD* pcbEncoded, // in/out
LPCTSTR* ppszError // out, optional
);
Encoding type | Value |
---|---|
X509_ASN_ENCODING | 0x00000001 |
When dwStrType is set to 0, CERT_OID_NAME_STR or CERT_X500_NAME_STR, the input is allowed to contain either a case insensitive X500 key (CN=), case insensitive "OID." prefixed object identifier (OID.1.2.3.4.5.6=) or an object identifier (1.2.3.4=).
If no flags are combined with a bitwise OR operation into dwStrType, then, the input is allowed to contain "," or ";" as RDN separators and "+" as the multiple RDN value separator. Quoting is supported. A quote may be included in a quoted value by double quoting, for example (CN="Joe ""Cool"""). A value starting with a "#" is treated as ascii hex and converted to a CERT_RDN_OCTET_STRING. Embedded whitespace is skipped (1.2.3 = # AB CD 01 is the same as 1.2.3=#ABCD01).
Whitespace surrounding the keys, object identifiers and values are removed.
The following X500 Keys are supported:
Key | Object Identifier | RDN Value Type(s) |
---|---|---|
CN | szOID_COMMON_NAME | Printable, T61 |
L | szOID_LOCALITY_NAME | Printable, T61 |
O | szOID_ORGANIZATION_NAME | Printable, T61 |
OU | szOID_ORGANIZATIONAL_UNIT_NAME | Printable, T61 |
szOID_RSA_emailAddr | Only IA5 | |
C | szOID_COUNTRY_NAME | Only Printable |
S | szOID_STATE_OR_PROVINCE_NAME | Printable, T61 |
ST | szOID_STATE_OR_PROVINCE_NAME | Printable, T61 |
STREET | szOID_STREET_ADDRESS | Printable, T61 |
T | szOID_TITLE | Printable, T61 |
Title | szOID_TITLE | Printable, T61 |
G | szOID_GIVEN_NAME | Printable, T61 |
GivenName | szOID_GIVEN_NAME | Printable, T61 |
I | szOID_INITIALS | Printable, T61 |
Initials | szOID_INITIALS | Printable, T61 |
SN | szOID_SUR_NAME | Printable, T61 |
DC | szOID_DOMAIN_COMPONENT | Only IA5 |
If either Printable or T61 is allowed as the RDN Value Type for the key (CN=), then Printable is selected if the name string components are restricted to the following character set:
A, B, …, Z
a, b, …, z
0, 1, …, 9
(space) ' ( ) +, - . / : = ?
This parameter can be NULL to set the size of the key usage for memory allocation purposes. For more information, see the "Common In/Out Parameter Conventions" section at the beginning of this Reference.
If the input string is detected to be invalid, *ppszError is updated by this function to point to the beginning of the invalid character sequence. If no errors are detected in the input string, *ppszError is set to NULL.
ppszError can be set to NULL by the application if it is not interested in getting a pointer to the invalid character sequence, should one be found.
*ppszError is updated with a non-NULL pointer for the following errors:
CRYPT_E_INVALID_X500_STRING
CRYPT_E_INVALID_NUMERIC_STRING
CRYPT_E_INVALID_PRINTABLE_STRING
CRYPT_E_INVALID_IA5_STRING
Returns TRUE if the input string was successfully parsed and the name was encoded without error. Otherwise, FALSE is returned.
Call GetLastError to see the reason for any failures.
The T61 types are UTF-8 encoded.
// EXAMPLE CODE FOR USING CertStrToName().
// Converts a X500 string to an encoded certificate name.
// Assume a pointer to the X500 string is already known.
// Set up the variables.
DWORD dwCertEncodingType = X509_ASN_ENCODING;
// Type of encoding
LPCTSTR pszX500; // Initialized elsewhere
DWORD dwStrType = 0; // Type of input string
void * pvReserved = NULL; // Reserved - set to NULL
BYTE * pbEncoded; // Pointer to the BYTE blob
DWORD cbEncoded; // # of bytes of the BYTE blob
LPCTSTR* ppszError; // Error handling-out, optional
BOOL fResult; // Returned TRUE if name is encoded
// FALSE if error occurred
// Function called the first time to get
// the size of the structure
fResult = CertStrToName(
dwCertEncodingType,
pszX500,
dwStrType,
pvReserved,
pbEncoded,
&cbEncoded,
ppszError);
if (!fResult){
cout<< "First call to CertStrToName failed"<< endl;
}
else {
cout<< "First call to CertStrToName successful"<< endl;
pbEncoded = (BYTE*)malloc (cbEncoded);
cout<< "memory allocated" << endl;
}
// Function call to convert string to name
fResult = CertStrToName(
dwCertEncodingType,// in - Type of encoding
pszX500, // in - Pointer to the X500 string
dwStrType, // in - 0 allows case insensitive key
pvReserved, // in, optional - must be NULL
pbEncoded, // out - Pointer to the structure
&cbEncoded, // in/out - # of bytes in the structure
ppszError); // out, optional - Set to NULL- not
// interested in getting a pointer to
// the invalid character sequence
if (!fResult){ // FALSE
cout<< "String was not parsed "<< endl
<< "error code = "<< GetLastError ()<< endl;
}
else { // TRUE
cout<< "String was parsed"<< endl
<< "encoded structure = "<< &pbEncoded<< endl
<< "of "<< cbEncoded<< " bytes"<< endl;
}
free (&pbEncoded);
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.