The CryptExportPublicKeyInfoEx function exports the public key information associated with the provider's corresponding private key. The main difference from CryptExportPublicKeyInfo is that the user can specify the public key algorithm, overriding the default provided by the CSP.
#include <wincrypt.h>
BOOL WINAPI CryptExportPublicKeyInfoEx(
HCRYPTPROV hCryptProv, // in
DWORD dwKeySpec, // in
DWORD dwCertEncodingType, // in
LPSTR pszPublicKeyObjId, // in
DWORD dwFlags, // in
void *pvAuxInfo, // in, optional
PCERT_PUBLIC_KEY_INFO pInfo, // out
DWORD *pcbInfo // in, out
);
Encoding type | Value |
---|---|
X509_ASN_ENCODING | 0x00000001 |
This parameter can be NULL to set the size of this information for memory allocation purposes. For more information, see Common In/Out Parameter Conventions.
Note When processing the data returned in the buffer, applications need to use the actual size of the data returned. The actual size may be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to insure that the largest possible output data will fit in the buffer.) On output, the variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.
TRUE if the function succeeded, FALSE if the function failed.
Call GetLastError to see the reason for any failures. Note that errors from the called functions CryptGetUserKey and CryptExportKey may be propagated to this function. This function has the following error codes.
Error code | Description |
---|---|
CRYPT_E_OSS_ERROR | Public key ASN.1 encoding error. Note, to get the OSS error subtract CRYPT_E_OSS_ERROR from the returned error and see asn1code.h for details on the error. |
ERROR_FILE_NOT_FOUND | An installable or registerable export function could not be found for the specified dwCertEncodingType and pszPublicKeyObjId. |
ERROR_MORE_DATA | If the buffer specified by the pInfo parameter is not large enough to hold the returned data, the function sets the ERROR_MORE_DATA code, and stores the required buffer size, in bytes, into the variable pointed to by pcbInfo. |
// EXAMPLE CODE FOR USING CryptExportPublicKeyInfoEx().
// Exports the public key information associated with the provider's
// corresponding private key.
// Assume that a pointer to the public key information
// PCERT_PUBLIC_KEY_INFO is already known.
// Set up the variables.
HCRYPTPROV hCryptProv = NULL; // Service Provider handle
DWORD dwKeySpec = AT_KEYEXCHANGE; // Public key spec
DWORD dwCertEncodingType = X509_ASN_ENCODING;
// Type of encoding
LPSTR pszPublicKeyObjId = szOID_RSA_RC4;
// Public key algorithm
DWORD dwFlags = 0; // Future use- set to 0
void * pvAuxInfo = NULL; // Future use- set to NULL
PCERT_PUBLIC_KEY_INFO pInfo; // Initialized elsewhere
DWORD cbInfo; // DWORD for size
BOOL fResult; // Return TRUE if function succeeded
// false if function failed
// Function called the first time to get
// the size of pcbInfo - allocated memory
fResult= CryptExportPublicKeyInfoEx(
hCryptProv,
dwKeySpec,
dwCertEncodingType,
pszPublicKeyObjId,
dwFlags,
pvAuxInfo,
NULL, // NULL on first call
&cbInfo); // Address for the byte count
if (!fResult) { // FALSE
cout<< "first call CryptExportPublicKeyInfoEx failed"<< endl;
}
else { // TRUE
cout<< "first call CryptExportPublicKeyInfoEx successful"<< endl;
pInfo = (PCERT_PUBLIC_KEY_INFO)malloc(cbInfo);
cout<< "memory allocated"<< endl;
}
// Call to CryptExportPublicKeyInfoEx to get the public key
// information.
fResult= CryptExportPublicKeyInfoEx(
hCryptProv, // in - 0 = the default RSA or DSS
// provider
dwKeySpec, // in - CERT_CONTEXT-REVOCATION-TYPE
dwCertEncodingType,// in - X509_ASN_ENCODING
pszPublicKeyObjId, // in - Public key algorithm
dwFlags, // in - set to 0
pvAuxInfo, // in - set to NULL
pInfo, // out- Pointer to returned public key
// information
&cbInfo); // in/out - Size of the public key
// information
if (!fResult) { // FALSE
cout<< "Function failed"<< endl
<< "error code = "<< GetLastError()<< endl;
}
else { // TRUE
cout<< "Function succeeded"<< endl;
}
free (pInfo);
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.