This function retrieves a handle to a permanent user key pair, such as the user’s signature key pair.
At a Glance
Header file: | Wincrypt.h |
Windows CE versions: | 2.10 and later |
Syntax
BOOL WINAPI CryptGetUserKey( HCRYPTPROV hProv,
DWORD dwKeySpec, HCRYPTKEY *phUserKey );
Parameters
hProv
[in] Handle to the application's CSP. An application obtains this handle by using the CryptAcquireContext function.
dwKeySpec
[in] Specifies the key to retrieve. The following keys are retrievable from almost all providers:
Additionally, some providers allow access to other user-specific keys through this function. See the documentation on the specific provider for details.
phUserKey
[out] Address that the function copies the handle of the retrieved key to.
Return Values
TRUE indicates success. FALSE indicates failure. To get extended error information, call GetLastError. Common values for GetLastError are described in the following table. The error values prefaced by "NTE" are generated by the particular CSP you are using.
Value | Description |
ERROR_INVALID_HANDLE | One of the parameters specifies an invalid handle. |
ERROR_INVALID_PARAMETER | One of the parameters contains an invalid value. This is most often an illegal pointer. |
NTE_BAD_KEY | The dwKeySpec parameter contains an invalid value. |
NTE_BAD_UID | The hProv parameter does not contain a valid context handle. |
NTE_NO_KEY | The key requested by the dwKeySpec parameter does not exist. |
Example
#include <wincrypt.h>
HCRYPTPROV hProv = 0;
HCRYPTKEY hSignKey = 0;
HCRYPTKEY hXchgKey = 0;
// Get a handle to the user default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
printf("Error %x during CryptAcquireContext!\n", GetLastError());
goto done;
}
// Get a handle to the signature key.
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hSignKey)) {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
goto done;
}
// Get a handle to the key exchange key.
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey)) {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
goto done;
}
// Use the 'hSignKey' to verify a signature, or use 'hXchgKey' to export a key to yourself.
...
done:
// Destroy the signature key handle.
if(hSignKey != 0) CryptDestroyKey(hSignKey);
// Destroy the key exchange key handle.
if(hXchgKey != 0) CryptDestroyKey(hXchgKey);
// Release the provider handle.
if(hProv != 0) CryptReleaseContext(hProv, 0);
See Also