JScript Source: storeProp.js

MSXML 5.0 SDK

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

JScript Source: storeProp.js

var xmldoc, xmldsig, certStore, infile;

NOKEYINFO = 0;
KEYVALUE  = 1;
CERTIFICATES = 2;
PURGE     = 4;

CAPICOM_MEMORY_STORE                                    = 0
CAPICOM_LOCAL_MACHINE_STORE                             = 1
CAPICOM_CURRENT_USER_STORE                              = 2
CAPICOM_ACTIVE_DIRECTORY_USER_STORE                     = 3
 
DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'";
PROV_RSA_FULL = 1;
// Change this key container name to your own if necessary.
RSA_KEY = "MyRSAFullKeys";

csp = PROV_RSA_FULL;
key = RSA_KEY;
infile = "signature_template.store.rsa.xml";

if (InitXML()) 
{

  if (LoadXML(infile)) {

   alert("Sign with WriteKeyInfo = CERTIFICATES:");
    SignXML(CERTIFICATES|PURGE);

   if (certStore) {
      xmldsig.store = certStore;
      alert("certStore set on xmldsig.");
      alert("number of certifcates in the store = "
            +certStore.CERTIFICATES.count+"\n");
   }

   alert("Sign with WriteKeyInfo = CERTIFICATES:");
    SignXML(CERTIFICATES|PURGE);

  }

}

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

///////// Set signature for signing. ////////
function InitXML()
{
   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;

   // Create and open a CAPICOM store object.
   certStore = new ActiveXObject("CAPICOM.Store.2");
   certStore.Open(CAPICOM_CURRENT_USER_STORE, "MY", 0);

   return true;
}

function LoadXML(file)
{
   if (xmldoc == null) {
     alert("must instantiate xml dom\n");
     return false;
   }
  
   if (!xmldoc.load(file)) {
     alert("Can't load "+ file + "\n");
     return false;
   }
   xmldoc.setProperty("SelectionNamespaces", DSIGNS);
   xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature");

   return true;
}


function SignXML(fwWriteKeyInfo)
{
   if (!xmldsig.signature) 
   {
      alert("Invalid signature template\n");
      return false;
   }

   var oKey = xmldsig.createKeyFromCSP(csp, "", key, 0);
   if (!oKey)
   {
      alert("Invalid key.\n");
      return false;
   }


   var oSignedKey = xmldsig.sign(oKey,fwWriteKeyInfo);
   if (!oSignedKey) 
   {
      alert("sign failed.\n");
      return false;
   }

   alert("The specified data was signed successfully.\n"+
         "Resultant signature:\n"+
         xmldoc.xml + "\n");
   return true;
}

Try It!

  1. 8Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
  2. Copy the signature_template.store.rsa.xml and paste it into a text file. Save the file as signature_template.store.rsa.xml.
  3. Copy the JScript listing above, and paste it into a text file. Save the file as storeProp.js, in the same directory where you saved signature_template.store.rsa.xml.
  4. From a command prompt, navigate to this directory, then type "cscript storeProp.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 similar to that listed in the Output topic.