#include <stdio.h>
#import <msxml5.dll>
using namespace MSXML2;
#define DSIGNS "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'"
IXMLDOMDocument3Ptr xmldoc = NULL;
IXMLDigitalSignaturePtr xmldsig = NULL;
VARIANT_BOOL objectsAreInitialized = VARIANT_FALSE;
//////////////////////////////////////////////////
// Load a signature document into dom and assign
// it to the xmldsig object.
//
VARIANT_BOOL LoadXML(_bstr_t sigFile)
{
if (!objectsAreInitialized) {
printf("Must initialize objects before loading signature.\n");
return VARIANT_FALSE;
}
if (xmldoc->load(sigFile) == VARIANT_FALSE) {
printf("Can't load %s\n", (LPCSTR)sigFile);
return VARIANT_FALSE;
}
printf("\nInput signature element:\n\n%s\n", (LPCSTR)xmldoc->xml);
xmldoc->setProperty("SelectionNamespaces", DSIGNS);
// Set the signature property to a <ds:Signature> DOM node.
xmldsig->signature = xmldoc->selectSingleNode(".//ds:Signature");
if (xmldsig->signature == NULL) {
printf("Failed to set the signature property.\n");
return VARIANT_FALSE;
}
return VARIANT_TRUE;
}
/////////////////////////////////////////////
// Verify signature with a key created from createKeyFromNode method.
//
VARIANT_BOOL VerifyXML()
{
IXMLDOMNodePtr pKeyInfo;
IXMLDSigKeyPtr pKey, pKeyOut;
pKeyInfo = xmldoc->selectSingleNode(".//ds:KeyInfo/ds:KeyValue");
if (pKeyInfo == NULL) {
printf("Invalid <ds:KeyInfo/KeyValue>\n");
return VARIANT_FALSE;
}
pKey = xmldsig->createKeyFromNode(pKeyInfo);
if (pKey== NULL) {
printf("Invalid key from <ds:KeyInfo>\n");
return VARIANT_FALSE;
}
pKeyOut = xmldsig->verify(pKey);
if (pKeyOut== NULL) {
printf("Invalid signature.\n");
return VARIANT_FALSE;
}
printf("The signature has been verified.\n");
return VARIANT_TRUE;
}
/////////////////////////////////
// Helper function to create dom and dsig objects:
//
VARIANT_BOOL initObjects()
{
if (FAILED(xmldsig.CreateInstance(__uuidof(MXDigitalSignature50)) )) {
printf("Installation of msxml5 is required to run this app.\n");
return VARIANT_FALSE;
}
if (FAILED(xmldoc.CreateInstance(__uuidof(DOMDocument50)) )) {
printf("Installation of msxml5 is required to run this app.\n");
return VARIANT_FALSE;
}
xmldoc->async = VARIANT_FALSE;
xmldoc->validateOnParse = VARIANT_FALSE;
xmldoc->preserveWhiteSpace = VARIANT_TRUE;
objectsAreInitialized = VARIANT_TRUE;
return VARIANT_TRUE;
}
////////////////////////////////
// Helper function to release dom and dsig objects:
//
void cleanObjects()
{
if (xmldoc) xmldoc.Release();
if (xmldsig) xmldsig.Release();
}
/////////////////////////////////
// Main function:
//
void main()
{
if ( CoInitialize(NULL) == E_FAIL) {
printf("can't initialize COM Lib\n");
exit(-1);
}
if (!initObjects()) {
cleanObjects();
exit(-1);
}
if(VARIANT_TRUE == LoadXML("signature.dsa.xml")) {
if (VARIANT_TRUE != VerifyXML()) {
printf("exit with failure.\n");
}
}
cleanObjects();
CoUninitialize();
}
Try It!