The CertGetEnhancedKeyUsage function gets the enhanced key usage extension or property from the certificate.
#include <wincrypt.h>
BOOL WINAPI CertGetEnhancedKeyUsage(
PCCERT_CONTEXT pCertContext, // in
DWORD dwFlags // in
PCERT_ENHKEY_USAGE pUsage // out
DWORD *pcbUsage // in/out
);
If the CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG is set, then, only get the property.
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.
TRUE if the function succeeded, FALSE if the function failed.
Call GetLastError to see the reason for any failures.
// EXAMPLE CODE FOR USING CertGetEnhancedKeyUsage.
// To get and print the intended key usage bytes from
// a certificate. Assume that the pointer to the
// certificate (pCertContext) is already known.
// Set up the variables.
PCCERT_CONTEXT pCertContext;// Initialized elsewhere
DWORD dwFlags = CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG;
// Extension only
PCERT_ENHKEY_USAGE pUsage; // Initialized elsewhere
DWORD cbUsage = 0; // Size of key (in bytes)
BOOL fResult; // Returns TRUE if function succeeded
// FALSE if function failed
// Function called the first time to get
// the size of cbUsage - allocate memory
fResult = CertGetEnhancedKeyUsage(
pCertContext,
dwFlags,
NULL, // NULL on first call
&cbUsage); // in/out - Size of enhanced key usage
if (!fResult) { // FALSE
cout<< "first call to CertGetEnhancedKeyUsage failed"<< endl;
}
else { // TRUE
cout<< "first call to CertGetEnhancedKeyUsage successful"<< endl;
pUsage = (PCERT_ENHKEY_USAGE)malloc(cbUsage);
cout<< "memory allocated"<< endl;
}
// Function call to get usage extension or property.
fResult = CertGetEnhancedKeyUsage(
pCertContext, // in - Pointer to the certificate
dwFlags, // in - Extension only flag
pUsage, // out- Pointer to enhanced key usage
&cbUsage); // in/out - Size of enhanced key usage
if (!fResult) { // FALSE
cout<< "function failed "<< endl
<< "error code = "<< GetLastError()<< endl;
}
else { // TRUE
cout<< "function succeeded "<< endl
<< "extension/ property = "<< dwFlags<< endl
<< "enhanced key usage size = "<< &cbUsage<< endl;
}
free (pUsage);
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.