Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NTE_BAD_SIGNATURE with CryptVerifySignature call...

7 views
Skip to first unread message

ula...@gmail.com

unread,
Jan 19, 2006, 9:32:09 PM1/19/06
to
Hello,
I am fairly new to certificates and crypto API. I am having
trouble getting a piece of code to work properly. I want to sign a
message with my cert, and then verify the signed blob with the public
key. This is a sample program where, both signing and verification are
taking place in a single program and in the same cert context.

Here is the code (stripped down some lines to make this compact)... Any
suggestions / thougts on why this would fail with a NTE_BAD_SIGNATURE?


Thanks,

---------------------------------------------

BYTE* pbMessage = (BYTE*)"Message that has to be signed and later
verified";
DWORD cbMessage = strlen((char*) pbMessage)+1;
const BYTE* MessageArray[] = {pbMessage};
DWORD MessageSizeArray[1];
MessageSizeArray[0] = cbMessage;


// Open cert store
CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_CURRENT_USER, // Open the current users store.
CERT_STORE_NAME))

// Open the cert

PCCERT_CONTEXT pSignerCert;
pSignerCert = CertFindCertificateInStore(
hStoreHandle,
MY_ENCODING_TYPE,
0,
CERT_FIND_SUBJECT_STR,
SUBJECT_NAME,
NULL))

CRYPT_SIGN_MESSAGE_PARA SigParams;

SigParams.cbSize = sizeof(CRYPT_SIGN_MESSAGE_PARA);
SigParams.dwMsgEncodingType = MY_ENCODING_TYPE;
SigParams.pSigningCert = pSignerCert;
SigParams.HashAlgorithm.pszObjId = szOID_RSA_SHA1RSA;
SigParams.HashAlgorithm.Parameters.cbData = NULL;
SigParams.cMsgCert = 0;
SigParams.rgpMsgCert = 0;
SigParams.cAuthAttr = 0;
SigParams.dwInnerContentType = 0;
SigParams.cMsgCrl = 0;
SigParams.rgpMsgCrl = 0;
SigParams.cUnauthAttr = 0;
SigParams.dwFlags = 0;
SigParams.pvHashAuxInfo = NULL;
SigParams.rgAuthAttr = NULL;


// Get the size of the signed buffer into cbSignedMessageBlob
CryptSignMessage(
&SigParams,
TRUE,
0,
MessageArray,
MessageSizeArray,
NULL,
&cbSignedMessageBlob)

// Sign the message
CryptSignMessage(
&SigParams,
TRUE,
0,
MessageArray,
MessageSizeArray,
pbSignedMessageBlob,
&cbSignedMessageBlob)

VERIFICATION
-----------------------
HCRYPTHASH hHash = 0;
HCRYPTKEY phPublicKey =0;

if(!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
printf("Error %x during CryptCreateHash!\n", GetLastError());

}

if(!CryptHashData(hHash, pbMessage, strlen((char*)pbMessage),
0)) {
printf("Error %x during CryptHashData!\n", GetLastError());
}

// Get a handle to the publickey by importing from the current context
// NOTE: I am using the same public/private key pair's context... this
is just a sample application.
if(!CryptImportPublicKeyInfo(hProv,MY_ENCODING_TYPE,&(pSignerCert->pCertInfo->SubjectPublicKeyInfo),&phPublicKey)))
{
....
}

// Cross the fingers... verify the signature.
if
(CryptVerifySignature(hHash,pbSignedMessageBlob,cbSignedMessageBlob,phPublicKey,NULL,0))
{
printf("SIGNATURE VERIFIED");
}

Doug Barlow

unread,
Jan 20, 2006, 11:47:14 AM1/20/06
to
<ula...@gmail.com> wrote:
> Here is the code (stripped down some lines to make this compact)... Any
> suggestions / thougts on why this would fail with a NTE_BAD_SIGNATURE?

From a quick glance, it looks like the length of your signed message
includes the trailing null character, but when you verify the message, you
don't incude that trailing null.

The signed and verified messages must be byte for byte identical.

Doug Barlow
The Soft Pedal Shop
CSP Design & Development Consulting
http://www.SoftPedal.net


0 new messages