Reputation: 3
Well, I have a function:
CRYPT_PRIVATE_KEY_INFO *pPriKeyInfo = NULL;
DWORD cbPriKeyInfo = 0;
SxKS_ExportPKCS8_PriKeyInfo(
pKey,
&pPriKeyInfo,
&cbPriKeyInfo);
which exports storage key (private key) into private key in specific structure:
typedef struct _CRYPT_PRIVATE_KEY_INFO{
DWORD Version;
CRYPT_ALGORITHM_IDENTIFIER Algorithm;
CRYPT_DER_BLOB PrivateKey;
PCRYPT_ATTRIBUTES pAttributes;
} CRYPT_PRIVATE_KEY_INFO, *PCRYPT_PRIVATE_KEY_INFO;
I need to write data in this structure to the file:
FILE *fp;
fp = fopen("c:\\CRYPT_PRIVATE_KEY_INFO.pem", "wb");
fwrite(&pPriKeyInfo, sizeof(pPriKeyInfo), cbPriKeyInfo, fp);
fclose(fp);
question is that, am I doing it right? because I think I am getting wrong data.
Upvotes: 0
Views: 141
Reputation: 72186
fwrite(&pPriKeyInfo, sizeof(pPriKeyInfo), cbPriKeyInfo, fp);
The first three arguments of your call to fwrite()
are incorrect. They tell fwrite()
what to write in the file, so there is no doubt why the data stored in the file doesn't look good.
fwrite()
function, in generalThe first argument of fwrite()
is a pointer to the data to be written. fwrite()
can write an array of objects and its first argument is the address of the first object to write.
pPriKeyInfo
is itself a pointer to the data you want to write in the file. Passing &pPriKeyInfo
to fwrite()
doesn't make any sense. You don't care about the value of pPriKeyInfo
but to the data it points to.
The second argument of fwrite()
is the size of each object to be written in the file. It should be the size of the structure pointed by the first argument (CRYPT_PRIVATE_KEY_INFO
).
The third argument of fwrite()
is the number of objects to write. In your example it should be 1
.
All in all, the call to fwrite()
should look like:
fwrite(pPriKeyInfo, sizeof(CRYPT_PRIVATE_KEY_INFO), 1, fp);
The explanation above assumes the object passed as the first argument to fwrite()
(a struct of type CRYPT_PRIVATE_KEY_INFO
) contains the actual data you want to write in the file.
This is not the case for struct CRYPT_PRIVATE_KEY_INFO
. This is its definition:
typedef struct _CRYPT_PRIVATE_KEY_INFO {
DWORD Version;
CRYPT_ALGORITHM_IDENTIFIER Algorithm;
CRYPT_DER_BLOB PrivateKey;
CRYPT_ATTRIBUTES *pAttributes;
} CRYPT_PRIVATE_KEY_INFO, CRYPT_PRIVATE_KEY_INFO, *PCRYPT_PRIVATE_KEY_INFO;
As you can see, the field pAttributes
is a pointer to a CRYPT_ATTRIBUTES
structure and the other two structure fields (Algorithm
and PrivateKey
) contain pointers themselves.
This is definitely not the correct way to save a CRYPT_PRIVATE_KEY_INFO
structure in a file! And from the large amount of pointers it contains I think this structure is designed to be used in memory, not to be saved.
It's not clear for me what exactly you want to save to the file but from the documentation it seems the actual key is stored in the PrivateKey
member of the structure. To write it into the file you can use:
fwrite(pPriKeyInfo->PrivateKey.pbData, sizeof(BYTE), pPriKeyInfo->PrivateKey.cbData, fp);
Upvotes: 0
Reputation: 409166
You're writing the pointer to the file. Pointers point to memory that is specific to the currently running process. If you load the pointer in another process, even one running the same program, then the pointer will not be valid.
So a quick-fix is to not write the pointer, but the structure to the file.
However that will lead to the very same problem happening again, since one of the members in the structure is a pointer. What you need to do is to serialize the structure.
Upvotes: 1