The CPExportKey function is used to export cryptographic keys out of the CSP in a secure manner.
BOOL CPExportKey(
HCRYPTPROV hProv, // in
HCRYPTKEY hKey, // in
HCRYPTKEY hExpKey, // in
DWORD dwBlobType, // in
DWORD dwFlags, // in
BYTE *pbData, // out
DWORD *pdwDataLen // in, out
);
Most often, this will be the key exchange public key of the destination user. Certain protocols, however, require that a session key belonging to the destination user be used for this purpose.
If the key blob is not to be encrypted (for example, the key blob type is PUBLICKEYBLOB), then this parameter is unused, and should be zero.
Additional key blob types will be defined as needed.
If this parameter is NULL, then no data should be copied. Instead, the required buffer size (in bytes) should be returned in pdwDataLen. This is not an error.
If the buffer specified by pbData is not large enough to hold the key blob data, the ERROR_MORE_DATA error code should be returned via the SetLastError function. In this case, the required buffer size must be returned in pdwDataLen.
If this function fails with any error code other than ERROR_MORE_DATA, zero should be returned in this parameter.
If the function succeeds, TRUE should be returned; otherwise, return FALSE. When FALSE is returned, the appropriate error code (see the following table) must be set via SetLastError.
Error | Description |
---|---|
ERROR_MORE_DATA | The pbData buffer is not large enough to hold the requested data. |
NTE_BAD_FLAGS | The dwFlags parameter contains an invalid value. |
NTE_BAD_KEY | One or both of the keys specified by hKey and hExpKey are invalid. |
NTE_BAD_KEY_STATE | You do not have permission to export the key. That is, when the hKey key was created, the CRYPT_EXPORTABLE flag was not specified. |
NTE_BAD_PUBLIC_KEY | The key blob type specified by dwBlobType is PUBLICKEYBLOB but hExpKey does not contain a public key handle. |
NTE_BAD_TYPE | The dwBlobType parameter specifies an unknown blob type. |
NTE_BAD_UID | The CSP context that was specified when the hKey key was created cannot now be found. |
NTE_NO_KEY | A session key is being exported and the hExpKey parameter does not specify a public key. |