#include <stdio.h> #import <msxml5.dll> using namespace MSXML2; #define DSIGNS "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'" // Change this key container name to your own if necessary. #define RSA_KEY "MyRSAFullKeys" #define INFILE "signature_template.rsa.xml" IXMLDOMDocument3Ptr xmldoc = NULL; IXMLDigitalSignaturePtr xmldsig = NULL; VARIANT_BOOL objectsAreInitialized = VARIANT_FALSE; 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; } 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; } VARIANT_BOOL SignXML(unsigned short * refID, unsigned short* newSrc) { HRESULT hr; ISAXXMLReaderPtr saxReader=NULL; if (xmldsig->signature == NULL) { printf("Invalid signature template\n"); return VARIANT_FALSE; } if (refID==NULL || refID == L"") { printf("Invalid reference ID\n"); return VARIANT_FALSE; } if (newSrc!=NULL && newSrc != L"") { hr = saxReader.CreateInstance(__uuidof(SAXXMLReader50)); if (FAILED(hr)) { printf("Invalid SAXReader object\n"); return VARIANT_FALSE; } IUnknownPtr saxProxy = xmldsig->createSAXProxy(); if (saxProxy == NULL) { printf("Failed to create SAX proxy\n"); return VARIANT_FALSE; } ISAXContentHandlerPtr ch = NULL; hr = saxProxy.QueryInterface(__uuidof(ISAXContentHandler),(ISAXContentHandler**)&ch); if (FAILED(hr)) { printf("Can't QI ISAXContentHandler\n"); return VARIANT_FALSE; } hr = saxReader->putContentHandler(ch); if (FAILED(hr)) { printf("Can't set saxProxy\n"); return VARIANT_FALSE; } hr = xmldsig->setReferenceData(refID, saxProxy); if (FAILED(hr)) { printf("Can't set reference data\n"); return VARIANT_FALSE; } hr = saxReader->parseURL(newSrc); if (FAILED(hr)) { printf("Failed to parseURL\n"); return VARIANT_FALSE; } } // At this point, saxReader and ch will be out of scope. IXMLDSigKeyPtr pKey = xmldsig->createKeyFromCSP( PROV_RSA_FULL, "", RSA_KEY, 0); if (pKey==NULL) { printf("Invalid key\n"); return VARIANT_FALSE; } IXMLDSigKeyPtr pKeyOut = xmldsig->sign(pKey, PURGE); if (NULL == pKeyOut) { printf("sign failed.\n"); return VARIANT_FALSE; } printf("Signing was successful.\n"); printf("Resultant signature:\n\n"); printf((LPCSTR)xmldoc->xml); printf("\n"); return VARIANT_TRUE; } 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; xmldoc->resolveExternals = VARIANT_FALSE; objectsAreInitialized = VARIANT_TRUE; return VARIANT_TRUE; } void cleanObjects() { if (xmldoc) xmldoc.Release(); if (xmldsig) xmldsig.Release(); } 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(INFILE)) { printf("Signing data referenced in signature...\n"); SignXML(L"#objData", L""); } if(VARIANT_TRUE == LoadXML(INFILE)) { printf("Signing test.xml fed through SAX proxy...\n"); SignXML(L"#objData", L"test.xml"); } cleanObjects(); CoUninitialize(); }
Try It!
Note You can also copy the file into the project's main directory using Windows Explorer (or a command prompt).