xml dsig, how to save and hash reference elements?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AK

    xml dsig, how to save and hash reference elements?

    Hello,

    I need to do the following with an xml document which has a list of
    assets:
    1. Hash the assets
    2. Hash the element describing the assets
    3. Create a digital signature (using X.509 certificate) over the
    hashes from step 1 and 2

    Most of the examples I've been looking at are doing a digital
    signature in one go, and I'm not sure how to write the references with
    the digest to the xml file without also creating a signature.

    This is the code I currently have:

    // Create a key container
    CspParameters cspParameters = new CspParameters() ;
    cspParameters.K eyContainerName = "XML_DSIG_RSA_K EY";

    // Create an RSA key and save it in the container
    RSACryptoServic eProvider rsaKey = new
    RSACryptoServic eProvider(cspPa rameters);

    // Create a new XML document and load the manifest into it
    XmlDocument xmlDoc = new XmlDocument();
    //xmlDoc.Preserve Whitespace = true;
    xmlDoc.Load(man ifestPath);

    hashAssets(xmlD oc, rsaKey);

    // Save the manifest
    xmlDoc.Save(man ifestPath);


    public void hashAssets(XmlD ocument Doc, RSA Key)
    {
    // Create a SignedXml object
    SignedXml signedXml = new SignedXml(Doc);

    // Add the key
    signedXml.Signi ngKey = Key;

    // Get urls to assets with signed = true
    assetUris = getAssetUris();

    foreach (string assetUri in assetUris)
    {
    // Create a reference to be signed
    Reference reference = new Reference(); reference.Uri = assetUri;

    //// Add an enveloped transformation to the reference
    //XmlDsigEnvelope dSignatureTrans form env = new
    XmlDsigEnvelope dSignatureTrans form();
    //reference.AddTr ansform(env);

    // Add the reference to the SignedXml object
    signedXml.AddRe ference(referen ce);
    }

    // Compute the signature
    signedXml.Compu teSignature();

    // Get the XML representation of the signature and save it to an
    XmlElement object
    XmlElement xmlDigitalSigna ture = signedXml.GetXm l();

    // Append the element to the XML document
    Doc.DocumentEle ment.AppendChil d(Doc.ImportNod e(xmlDigitalSig nature,
    true));
    }

    Is there any way of saving the reference to the xmldoc without also
    creating a signature? And then hash the reference elements and create
    a signature over them? Or am I taking the wrong approach with this
    code to start with?

    Many thanks,

    AK
Working...