Question

Is there a way to wrap or unwrap keys in CryptoApi? I tryed to set the key params CRYPT_IMPORT_KEY and CRYPT_EXPORT_KEY with the proper function, but I get the error "params undefined".

Other details: The program is written in C++, OS: Win7, IDE: Dev-C++ The main headers are windows.h and wincrypt.h.

When I compile, i get the error " CRYPT_EXPORT_KEY (or CRYPT_IMPORT_KEY) undeclared".

Code I wrote:

flags = CRYPT_ENCRYPT | CRYPT_EXPORTABLE | CRYPT_DECRYPT | CRYPT_EXPORT_KEY;
//set the above permission to the session key "dkey"    
CryptSetKeyParam(dKey,KP_PERMISSIONS,(BYTE*)&flags,0);

Update from the comments:

And it compiles! But the programs terminates with the error NTE_BAD_FLAGS. It is because of CRYPT_EXPORT_KEY flag!

Was it helpful?

Solution

Check the docs - CryptSetKeyParam and CryptGetKeyParam

When you are using KP_PERMISSIONS, the valid set of flags are

CRYPT_ARCHIVE
CRYPT_DECRYPT
CRYPT_ENCRYPT
CRYPT_EXPORT
CRYPT_EXPORT_KEY
CRYPT_IMPORT_KEY
CRYPT_MAC
CRYPT_READ
CRYPT_WRITE

CRYPT_EXPORTABLE is not in this list.

If you check the docs for CryptGenKey, CRYPT_EXPORTABLE is a flag which you use while creating the key, so that it can be exported later. It's not required for CryptSetKeyParam.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top