CryptImportKey() is returning NTE_BAD_DATA



I'm trying to use CryptoAPI on XP SP3 to authenticate a smart card. I have a public key which is supplied by the smart card vendor. I need to use this to authenticate with the application running in the smart card. I am supposed to use 3DES as the algo. I'm doing the following steps -

1. Acquire context for the CSP
2. Create a public key blob with the public key i have
3. Call CryptImportKey() with this blob. - Here I get an error NTE_BAD_DATA.

What is the problem with my code ?
Please suggest me the steps involved in using 3DES encryption when a public is already available.

My code snippet is
-------------------------------------------------------------------------------
TEST_PUBLICKEYBLOB pubkeyblob;
BYTE Testkey[255] = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
DWORD TestkeyLen = 24;




if(!CryptAcquireContext(&hCryptProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
printf("\nCryptAcquireContext: INVALID context, ERROR 0x%x",
GetLastError());
return FALSE;

}


pubkeyblob.hdr.bType = PUBLICKEYBLOB;
pubkeyblob.hdr.bVersion = 0x02;
pubkeyblob.hdr.reserved = 0;
pubkeyblob.hdr.aiKeyAlg = CALG_3DES;
pubkeyblob.pubkey.magic = 0x31415352;
pubkeyblob.pubkey.bitlen = 24;
pubkeyblob.pubkey.pubexp = 0;
memcpy((pubkeyblob.modulus),Testkey,TestkeyLen);

printf("\nKey: ");

for(i = 0; i < PassKeyLen; i++)
{
printf("0x%x ",pubkeyblob.modulus[i]);
}
printf("\n");

if(!CryptImportKey(hCryptProv, (BYTE *)&pubkeyblob,
sizeof(TEST_PUBLICKEYBLOB), 0, 0, &ctx->hKey))
{
printf("\nError %x during CryptImportKey!\n", GetLastError());
}
.



Relevant Pages

  • Re: Smart card Authentification
    ... > I'm curios about how a smart card and a smart card reader, ... > I'm thinking that they use public key cryptography to exhange the ... > like visa, that an terminal can connect to, and retrieve the ...
    (sci.crypt)
  • Smart card Authentification
    ... I'm curios about how a smart card and a smart card reader, ... I'm thinking that they use public key cryptography to exhange the ... public key of VISA. ...
    (sci.crypt)
  • Re: Smart Cards?
    ... A smart card uses PKI for authentication and the users smart ... require smart card for logon then there is no way for someone else to logon ... A private key can decrypt what a public key encrypts and can be ...
    (microsoft.public.security)
  • Re: Implementation of CardSignData() in smart card modules (minidrivers)
    ... You should not always add the DigestInfo DER into signature in step 3. ... I'm trying to write a smart card minidriver interfacing the Base CSP ... Performing AT_SIGNATURE public key matching test... ... Provider = Microsoft Base Smart Card Crypto Provider ...
    (microsoft.public.platformsdk.security)
  • Implementation of CardSignData() in smart card modules (minidrivers)
    ... I'm trying to write a smart card minidriver interfacing the Base CSP ... Performing AT_SIGNATURE public key matching test... ... Provider = Microsoft Base Smart Card Crypto Provider ...
    (microsoft.public.platformsdk.security)