CryptImportPublicKeyInfo

The CryptImportPublicKeyInfo function converts and imports the public key information into the provider and returns a handle to the public key.

#include <wincrypt.h>
BOOL WINAPI CryptImportPublicKeyInfo(
  HCRYPTPROV hCryptProv,          // in
  DWORD dwCertEncodingType,       // in
  PCERT_PUBLIC_KEY_INFO pInfo,    // in
  HCRYPTKEY *phKey                // out
);
 

Parameters

hCryptProv
Specifies the Cryptographic Service Provider to use when importing the public key.
dwCertEncodingType
The type of encoding used on the certificate. Currently defined certificate 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.
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 CryptImportPublicKeyInfo().
// From a signed and encoded certificate, computes the hash of the
// encoded content that was originally signed (the original "to be
// signed").
// Assume that a pointer to the Public Key blob (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
HCRYPTKEY *phKey;
BOOL fResult;                 // Return TRUE if function succeeded
                              //   FALSE if function failed

fResult= CryptImportPublicKeyInfo(
           hCryptProv,        // in - 0 is default RSA or DSS provider 
           dwCertEncodingType,// in - X509_ASN_ENCODING
           pInfo,             // in - Pointer to the public key to
                              //   import 
           phKey);            // out- Pointer to the handle to the
                              //   returned 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

CryptExportPublicKeyInfo