decryption problem with SC (0x800900d error)



I'm trying to write an application which will decode an enveloped
message using a private key from a smartcard.

The smartcard has 2 pairs of keys ( one AT_SIGNATURE and one
AT_KEYEXCHANGE). With the "signing" pair I can sign/verify &
encrypt/decrypt with no problems.

With the "encryption" pair I can sign/verify & encrypt at will, but
when it comes to decryption I get the 0x800900d error in a call to
CryptMsgControl(CMSG_CTRL_DECRYPT) even though a call to
CryptAcquireCertificatePrivateKey was made before that and ended in
success.

I was able to decrypt the same enveloped message with a commercial
app.!

This is my code:

CryptMsgGetParam(
hMsg,
CMSG_RECIPIENT_INFO_PARAM,
index,
NULL,
&cbInfo))

pCertInfo = (CERT_INFO*) malloc(cbInfo);

if(!CryptMsgGetParam(
hMsg,
CMSG_RECIPIENT_INFO_PARAM,
index,
pCertInfo,
&cbInfo))
{
return FALSE;
}

if(!(pCertContext = CertGetSubjectCertificateFromStore(
hStoreHandle,
MY_ENCODING_TYPE,
pCertInfo)))
{
return FALSE;
}

//--------------------------------------------------------------------
// Get the certificate private key info

DWORD dwKeySpec;
BOOL xx;

if(!CryptAcquireCertificatePrivateKey(
pCertContext,
0,
NULL,
&hCryptProv,
&dwKeySpec,
&xx))
{
return FALSE;
}

//--------------------------------------------------------------------
// Begin decryption

memset(&dPara,0,sizeof(CMSG_CTRL_DECRYPT_PARA));
dPara.cbSize = sizeof(CMSG_CTRL_DECRYPT_PARA);
dPara.dwKeySpec = dwKeySpec;
dPara.hCryptProv = hCryptProv;
dPara.dwRecipientIndex = index;

if(!CryptMsgControl(
hMsg,
0,
CMSG_CTRL_DECRYPT,
&dPara))
{
DWORD xdw = GetLastError();
return FALSE;
}

The code works, because I've tested it with other certs and even with
the "signing" pair!

I ran out of ideas! Any suggestion is welcomed! Thanks!

.



Relevant Pages


Quantcast