Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - Digital Signatures

JScript Source: KeyFromNode.js

var xmldoc, xmldsig
var szResult = "";

DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'";
sigfile = "signature.dsa.xml"

if (LoadXML(sigfile)) {

  if (VerifyXML()) {
     alert(szResult);
  }
}

/////////  Helper functions: /////////
function alert(str) { WScript.echo(str); } 

///////// Set signature for signing: ////////
function LoadXML(file)
{
   try {
     xmldoc = new ActiveXObject("Msxml2.DOMDOcument.5.0");
     xmldsig= new ActiveXObject("Msxml2.MXDigitalSignature.5.0");
   }
   catch (e) {
     alert("Installation of mxsml5 is required to run this app.\n");
     return false;
   }
   
   xmldoc.async = false;
   xmldoc.preserveWhiteSpace = true;
   xmldoc.validateOnParse = false;
   
   if (!xmldoc.load(file)) {
     alert("Can't load "+ file + "\n");
     return false;
   }
   szResult += "\nInput signature element:\n\n" + xmldoc.xml;

   xmldoc.setProperty("SelectionNamespaces", DSIGNS);
   xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature");

   return true;
}

function VerifyXML()
{
   if (!xmldsig.signature) {
      alert("Invalid signature.\n");
      return false;
   }
   var oKeyInfo = xmldoc.selectSingleNode(".//ds:KeyInfo/ds:KeyValue");
   var oKey = xmldsig.createKeyFromNode(oKeyInfo);   
   var oVerifiedKey = xmldsig.verify(oKey); 
   if (oVerifiedKey == null) {
      alert("Signature Verification failed.\n");
   }
   else {
      szResult += ("\nThe signature has been verified.\n");
   }
   return true;
}

Try It!

  1. Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
  2. Copy the XML signature resource file, and paste it into a text file. Save the file as signature.dsa.xml.
  3. Copy the JScript listing above, and paste it into a text file. Save the file as KeyFromNode.js, in the same directory where you saved signature.dsa.xml.
  4. From a command prompt, navigate to this directory, then type "cscript KeyFromNode.js".
    Note   Under operating systems other than Windows 2000 or Windows XP, you might need to install Windows Scripting Host (to run wscript.exe or cscript.exe), if it is not already installed.
  5. Verify that your output is the same as that listed in the Output topic.