Platform SDK: Cryptography |
The CPExportKey function securely exports cryptographic keys from a CSP's key container.
BOOL CPExportKey( HCRYPTPROV hProv, // in HCRYPTKEY hKey, // in HCRYPTKEY hPubKey, // in DWORD dwBlobType, // in DWORD dwFlags, // in BYTE *pbData, // out DWORD *pdwDataLen // in, out );
Most often, this handle will be the handle of the key exchange public key of the destination user. Certain protocols, however, require that this handle be the handle of a session key belonging to the destination user.
If the key BLOB is a PUBLICKEYBLOB and is not to be encrypted, this parameter is not used and must be zero.
BLOB type | Use |
---|---|
SIMPLEBLOB | To transport session keys. |
PUBLICKEYBLOB | To transport public keys. |
PRIVATEKEYBLOB | To transport public/private key pairs. |
OPAQUEKEYBLOB | To store session keys in an Schannel CSP. The OPAQUEKEYBLOB type is non-transferable and must be used within the CSP that generated the BLOB. |
Additional key BLOB types can be defined as needed.
Flag value | Effect |
---|---|
CRYPT_DESTROYKEY | Destroys the original key in the OPAQUEKEYBLOB. |
CRYPT_SSL2_ FALLBACK |
Sets the first eight bytes of the RSA encryption block padding to 0x03 rather than to random data. This prevents version rollback attacks and is discussed in the SSL3 specification. |
If the buffer specified by pbData is not large enough to hold the key BLOB data, the ERROR_MORE_DATA error code is returned with 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 is returned in this parameter.
If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE, and the appropriate error code from the following table must be set using 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 hPubKey are invalid. |
NTE_BAD_KEY_STATE | The key cannot be exported because the CRYPT_EXPORTABLE flag was not specified when the key was created. |
NTE_BAD_PUBLIC_KEY | The key BLOB type specified by dwBlobType is PUBLICKEYBLOB, but hPubKey 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. |
Windows NT/2000: Requires Windows NT 4.0 or later.
Windows 95/98: Requires Windows 95 OSR2 or later (or Windows 95 with Internet Explorer 3.02 or later).
Header: Declared in Wincrypt.h.