CryptImportPublicKeyInfoEx

The CryptImportPublicKeyInfoEx function converts and imports the public key information into the CSP and returns a handle to the public key. Additional parameters (over those specified by CryptImportPublicKeyInfo) that can be used to override defaults are provided to supplement the CERT_PUBLIC_KEY_INFO.

#include <wincrypt.h>
BOOL WINAPI CryptImportPublicKeyInfoEx(
  HCRYPTPROV hCryptProv,            // in
  DWORD dwCertEncodingType,         // in
  PCERT_PUBLIC_KEY_INFO pInfo,      // in
  ALG_ID aiKeyAlg,                  // in
  DWORD dwFlags,                    // in
  void *pvAuxInfo,                  // in, optional
  HCRYPTKEY *phKey                  // out
);
 

Parameters

hCryptProv
Specifies the Cryptographic Service Provider to use when importing the public key.
dwCertEncodingType
Type of encoding used on the certificate. Currently defined encoding types are shown in the following table:
Encoding type Value
X509_ASN_ENCODING 0x00000001

pInfo
Pointer to the public key to import into the provider. Note that
pInfo->Algorithm.pszObjId and dwCertEncodingType are used in determining the installable CRYPT_OID_IMPORT_PUBLIC_KEY_INFO_FUNC to call. If an installable function was not found, an attempt is made to import the key as a RSA Public Key (szOID_RSA_RSA). For szOID_RSA_RSA, aiKeyAlg may be set to CALG_RSA_SIGN or CALG_RSA_KEYX.
aiKeyAlg
CSP specific algorithm that a user may specify to override the default. Defaults to CALG_RSA_KEYX.
dwFlags
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.
phKey
Pointer to the handle to the imported public key.

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 import function could not be found for the specified dwCertEncodingType and
pInfo->Algorithm.pszObjId.

Example

// EXAMPLE CODE FOR USING CryptImportPublicKeyInfoEx().
// Convert and import the public key information and return
// a handle to the public key.
// Assume that a pointer to the public key (pInfo) has
// already been defined.

// Set up the variables.
HCRYPTPROV hCryptProv = 0;       // Service Provider handle
DWORD dwCertEncodingType = X509_ASN_ENCODING;
                                 // Type of encoding
PCERT_PUBLIC_KEY_INFO pInfo;     // Initialized elsewhere
ALG_ID aiKeyAlg = CALG_RSA_KEYX; // CSP specific algorithm
DWORD dwFlags = 0;               // Flag value
void *pvAuxInfo = NULL;          // Reserved
HCRYPTKEY *phKey;
BOOL fResult;                    // Return TRUE if function succeeded
                                 //   FALSE if function failed
fResult= CryptImportPublicKeyInfoEx(
           hCryptProv, // in - 0 is default RSA or DSS provider
           dwCertEncodingType,
                       // in - X509_ASN_ENCODING
           pInfo,      // in - Pointer to the public key to import
           aiKeyAlg,   // in - Reserved for future use - set to 0
           0,          // in - Reserved for future use - set to NULL
           NULL,       // in/optional
           phKey);     // out- Pointer to the handle to the public key

if (!fResult) {        // FALSE
  cout<< "Function failed"<< endl
     << "error code = "<< GetLastError()<< endl;
}
else {                 // TRUE
  cout<< "Function succeeded"<< endl;
}
 

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

CryptExportPublicKeyInfoEx