Share via


Resource: signature_template.xml

 

[This sample code uses features that were implemented in MSXML 5.0 for Microsoft Office Applications. XML digital signatures are not supported in MXSML 6.0 and later.]

Use this resource file for the signature example.

This simple signature template references embedded text data held in the <ds:Object Id="#objData"> element. It also specifies how the referenced data is to be signed. Notice that the required <ds:SignatureValue> and <ds:DigestValue> elements are empty.

Signature Template (signature_template.xml)

<?xml version="1.0" encoding="UTF-8"?>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <ds:SignedInfo>
    <ds:CanonicalizationMethod 
       Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
    <ds:SignatureMethod 
       Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
    <ds:Reference URI="#objData">
      <ds:DigestMethod 
         Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
      <ds:DigestValue/>
    </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue />
  <ds:Object Id="objData">Hello, World!</ds:Object>
</ds:Signature>
#$JScript Source: signature.js
var xmldoc, xmldsig, dsigKey
var szResult = "";
KEYVALUE = 1;
DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'";
PROV_RSA_FULL = 1;
// Define constant for Diffie-Hillman or other cryptographic 
// provider if not using RSA full provider.
// PROV_DSS_DH   = 13;

if (loadSignature("signature_template.xml")) {
  // Change this key container name to your own if necessary.
  if (signWithKey(PROV_RSA_FULL, "MyRSAFullKeys")) {
     alert(szResult);
  }
}

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

///////// Set signature for signing. ////////
function loadSignature(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;
   xmldoc.resolveExternals = false;
   
   if (!xmldoc.load(file)) {
     alert("Can't load "+ file + "\n");
     return false;
   }
   szResult += "\nInput signature template:\n\n" + xmldoc.xml;

   xmldoc.setProperty("SelectionNamespaces", DSIGNS);

   // Set the signature property value to the first 
   // <ds:Signature> element.
   xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature");


   return true;
}

function signWithKey(dwCspType, szKeyContainer)
{
   // Read the signature property to verify that it's been set.
   if (!xmldsig.signature) {
      alert("Must set signature template before signing.\n");
      return false;
   }
   var oKey = xmldsig.createKeyFromCSP(dwCspType, "", szKeyContainer, 0);
   var oSignedKey = xmldsig.sign(oKey, KEYVALUE); 
   if (oSignedKey == null) {
      alert("Signing failed.\n");
   }
   else {
      szResult += ("\nThe data referenced in the signature template " +
            "was signed successfully.\n" + 
            "Resultant signature:\n\n" +
             xmldoc.xml );
   }
   return true;
}
Try It!