CryptExportPublicKeyInfoEx

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
);
 

Parameters

hCryptProv
Specifies the Cryptographic Service Provider to use when exporting the public key information.
dwKeySpec
Identifies the public key to use from the provider's container. For example, AT_KEYEXCHANGE or AT_SIGNATURE.
dwCertEncodingType
The type of encoding used on the certificate. Currently defined encoding types are shown in the following table:
Encoding type Value
X509_ASN_ENCODING 0x00000001

pszPublicKeyObjId
Specifies the public key algorithm. Note that pszPublicKeyObjId and dwCertEncodingType are used together in determining the installable CRYPT_OID_EXPORT_PUBLIC_KEY_INFO_FUNC to call. If an installable function was not found for the pszPublicKeyObjId, an attempt is made to export the key as a RSA Public Key (szOID_RSA_RSA).
dwFlags
The flag values. This parameter is reserved for future use and should be set to zero in the interim.
pvAuxInfo
This parameter is reserved for future use and should be set to NULL in the interim.
pInfo
Ppointer to a buffer that receives the public key information to be exported.

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.

pcbInfo
Pointer to a DWORD that contains the size, in bytes, of the buffer pointed to by the pInfo parameter. When the function returns, the variable pointed to by the pcbInfo parameter contains the number of bytes stored in the buffer. This parameter can be NULL only if pInfo is NULL.

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.

Return Values

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

// 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);
 

QuickInfo

  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.

See Also

CryptImportPublicKeyInfoEx